isdn_tty.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787
  1. /*
  2. * Linux ISDN subsystem, tty functions and AT-command emulator (linklevel).
  3. *
  4. * Copyright 1994-1999 by Fritz Elfert (fritz@isdn4linux.de)
  5. * Copyright 1995,96 by Thinking Objects Software GmbH Wuerzburg
  6. *
  7. * This software may be used and distributed according to the terms
  8. * of the GNU General Public License, incorporated herein by reference.
  9. *
  10. */
  11. #undef ISDN_TTY_STAT_DEBUG
  12. #include <linux/isdn.h>
  13. #include <linux/serial.h> /* ASYNC_* flags */
  14. #include <linux/slab.h>
  15. #include <linux/delay.h>
  16. #include <linux/mutex.h>
  17. #include "isdn_common.h"
  18. #include "isdn_tty.h"
  19. #ifdef CONFIG_ISDN_AUDIO
  20. #include "isdn_audio.h"
  21. #define VBUF 0x3e0
  22. #define VBUFX (VBUF/16)
  23. #endif
  24. #define FIX_FILE_TRANSFER
  25. #define DUMMY_HAYES_AT
  26. /* Prototypes */
  27. static DEFINE_MUTEX(modem_info_mutex);
  28. static int isdn_tty_edit_at(const char *, int, modem_info *);
  29. static void isdn_tty_check_esc(const u_char *, u_char, int, int *, u_long *);
  30. static void isdn_tty_modem_reset_regs(modem_info *, int);
  31. static void isdn_tty_cmd_ATA(modem_info *);
  32. static void isdn_tty_flush_buffer(struct tty_struct *);
  33. static void isdn_tty_modem_result(int, modem_info *);
  34. #ifdef CONFIG_ISDN_AUDIO
  35. static int isdn_tty_countDLE(unsigned char *, int);
  36. #endif
  37. /* Leave this unchanged unless you know what you do! */
  38. #define MODEM_PARANOIA_CHECK
  39. #define MODEM_DO_RESTART
  40. static int bit2si[8] =
  41. {1, 5, 7, 7, 7, 7, 7, 7};
  42. static int si2bit[8] =
  43. {4, 1, 4, 4, 4, 4, 4, 4};
  44. /* isdn_tty_try_read() is called from within isdn_tty_rcv_skb()
  45. * to stuff incoming data directly into a tty's flip-buffer. This
  46. * is done to speed up tty-receiving if the receive-queue is empty.
  47. * This routine MUST be called with interrupts off.
  48. * Return:
  49. * 1 = Success
  50. * 0 = Failure, data has to be buffered and later processed by
  51. * isdn_tty_readmodem().
  52. */
  53. static int
  54. isdn_tty_try_read(modem_info *info, struct sk_buff *skb)
  55. {
  56. struct tty_port *port = &info->port;
  57. int c;
  58. int len;
  59. char last;
  60. if (!info->online)
  61. return 0;
  62. if (!(info->mcr & UART_MCR_RTS))
  63. return 0;
  64. len = skb->len
  65. #ifdef CONFIG_ISDN_AUDIO
  66. + ISDN_AUDIO_SKB_DLECOUNT(skb)
  67. #endif
  68. ;
  69. c = tty_buffer_request_room(port, len);
  70. if (c < len)
  71. return 0;
  72. #ifdef CONFIG_ISDN_AUDIO
  73. if (ISDN_AUDIO_SKB_DLECOUNT(skb)) {
  74. int l = skb->len;
  75. unsigned char *dp = skb->data;
  76. while (--l) {
  77. if (*dp == DLE)
  78. tty_insert_flip_char(port, DLE, 0);
  79. tty_insert_flip_char(port, *dp++, 0);
  80. }
  81. if (*dp == DLE)
  82. tty_insert_flip_char(port, DLE, 0);
  83. last = *dp;
  84. } else {
  85. #endif
  86. if (len > 1)
  87. tty_insert_flip_string(port, skb->data, len - 1);
  88. last = skb->data[len - 1];
  89. #ifdef CONFIG_ISDN_AUDIO
  90. }
  91. #endif
  92. if (info->emu.mdmreg[REG_CPPP] & BIT_CPPP)
  93. tty_insert_flip_char(port, last, 0xFF);
  94. else
  95. tty_insert_flip_char(port, last, TTY_NORMAL);
  96. tty_flip_buffer_push(port);
  97. kfree_skb(skb);
  98. return 1;
  99. }
  100. /* isdn_tty_readmodem() is called periodically from within timer-interrupt.
  101. * It tries getting received data from the receive queue an stuff it into
  102. * the tty's flip-buffer.
  103. */
  104. void
  105. isdn_tty_readmodem(void)
  106. {
  107. int resched = 0;
  108. int midx;
  109. int i;
  110. int r;
  111. modem_info *info;
  112. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  113. midx = dev->m_idx[i];
  114. if (midx < 0)
  115. continue;
  116. info = &dev->mdm.info[midx];
  117. if (!info->online)
  118. continue;
  119. r = 0;
  120. #ifdef CONFIG_ISDN_AUDIO
  121. isdn_audio_eval_dtmf(info);
  122. if ((info->vonline & 1) && (info->emu.vpar[1]))
  123. isdn_audio_eval_silence(info);
  124. #endif
  125. if (info->mcr & UART_MCR_RTS) {
  126. /* CISCO AsyncPPP Hack */
  127. if (!(info->emu.mdmreg[REG_CPPP] & BIT_CPPP))
  128. r = isdn_readbchan_tty(info->isdn_driver,
  129. info->isdn_channel,
  130. &info->port, 0);
  131. else
  132. r = isdn_readbchan_tty(info->isdn_driver,
  133. info->isdn_channel,
  134. &info->port, 1);
  135. if (r)
  136. tty_flip_buffer_push(&info->port);
  137. } else
  138. r = 1;
  139. if (r) {
  140. info->rcvsched = 0;
  141. resched = 1;
  142. } else
  143. info->rcvsched = 1;
  144. }
  145. if (!resched)
  146. isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 0);
  147. }
  148. int
  149. isdn_tty_rcv_skb(int i, int di, int channel, struct sk_buff *skb)
  150. {
  151. ulong flags;
  152. int midx;
  153. #ifdef CONFIG_ISDN_AUDIO
  154. int ifmt;
  155. #endif
  156. modem_info *info;
  157. if ((midx = dev->m_idx[i]) < 0) {
  158. /* if midx is invalid, packet is not for tty */
  159. return 0;
  160. }
  161. info = &dev->mdm.info[midx];
  162. #ifdef CONFIG_ISDN_AUDIO
  163. ifmt = 1;
  164. if ((info->vonline) && (!info->emu.vpar[4]))
  165. isdn_audio_calc_dtmf(info, skb->data, skb->len, ifmt);
  166. if ((info->vonline & 1) && (info->emu.vpar[1]))
  167. isdn_audio_calc_silence(info, skb->data, skb->len, ifmt);
  168. #endif
  169. if ((info->online < 2)
  170. #ifdef CONFIG_ISDN_AUDIO
  171. && (!(info->vonline & 1))
  172. #endif
  173. ) {
  174. /* If Modem not listening, drop data */
  175. kfree_skb(skb);
  176. return 1;
  177. }
  178. if (info->emu.mdmreg[REG_T70] & BIT_T70) {
  179. if (info->emu.mdmreg[REG_T70] & BIT_T70_EXT) {
  180. /* T.70 decoding: throw away the T.70 header (2 or 4 bytes) */
  181. if (skb->data[0] == 3) /* pure data packet -> 4 byte headers */
  182. skb_pull(skb, 4);
  183. else
  184. if (skb->data[0] == 1) /* keepalive packet -> 2 byte hdr */
  185. skb_pull(skb, 2);
  186. } else
  187. /* T.70 decoding: Simply throw away the T.70 header (4 bytes) */
  188. if ((skb->data[0] == 1) && ((skb->data[1] == 0) || (skb->data[1] == 1)))
  189. skb_pull(skb, 4);
  190. }
  191. #ifdef CONFIG_ISDN_AUDIO
  192. ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
  193. ISDN_AUDIO_SKB_LOCK(skb) = 0;
  194. if (info->vonline & 1) {
  195. /* voice conversion/compression */
  196. switch (info->emu.vpar[3]) {
  197. case 2:
  198. case 3:
  199. case 4:
  200. /* adpcm
  201. * Since compressed data takes less
  202. * space, we can overwrite the buffer.
  203. */
  204. skb_trim(skb, isdn_audio_xlaw2adpcm(info->adpcmr,
  205. ifmt,
  206. skb->data,
  207. skb->data,
  208. skb->len));
  209. break;
  210. case 5:
  211. /* a-law */
  212. if (!ifmt)
  213. isdn_audio_ulaw2alaw(skb->data, skb->len);
  214. break;
  215. case 6:
  216. /* u-law */
  217. if (ifmt)
  218. isdn_audio_alaw2ulaw(skb->data, skb->len);
  219. break;
  220. }
  221. ISDN_AUDIO_SKB_DLECOUNT(skb) =
  222. isdn_tty_countDLE(skb->data, skb->len);
  223. }
  224. #ifdef CONFIG_ISDN_TTY_FAX
  225. else {
  226. if (info->faxonline & 2) {
  227. isdn_tty_fax_bitorder(info, skb);
  228. ISDN_AUDIO_SKB_DLECOUNT(skb) =
  229. isdn_tty_countDLE(skb->data, skb->len);
  230. }
  231. }
  232. #endif
  233. #endif
  234. /* Try to deliver directly via tty-buf if queue is empty */
  235. spin_lock_irqsave(&info->readlock, flags);
  236. if (skb_queue_empty(&dev->drv[di]->rpqueue[channel]))
  237. if (isdn_tty_try_read(info, skb)) {
  238. spin_unlock_irqrestore(&info->readlock, flags);
  239. return 1;
  240. }
  241. /* Direct deliver failed or queue wasn't empty.
  242. * Queue up for later dequeueing via timer-irq.
  243. */
  244. __skb_queue_tail(&dev->drv[di]->rpqueue[channel], skb);
  245. dev->drv[di]->rcvcount[channel] +=
  246. (skb->len
  247. #ifdef CONFIG_ISDN_AUDIO
  248. + ISDN_AUDIO_SKB_DLECOUNT(skb)
  249. #endif
  250. );
  251. spin_unlock_irqrestore(&info->readlock, flags);
  252. /* Schedule dequeuing */
  253. if ((dev->modempoll) && (info->rcvsched))
  254. isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
  255. return 1;
  256. }
  257. static void
  258. isdn_tty_cleanup_xmit(modem_info *info)
  259. {
  260. skb_queue_purge(&info->xmit_queue);
  261. #ifdef CONFIG_ISDN_AUDIO
  262. skb_queue_purge(&info->dtmf_queue);
  263. #endif
  264. }
  265. static void
  266. isdn_tty_tint(modem_info *info)
  267. {
  268. struct sk_buff *skb = skb_dequeue(&info->xmit_queue);
  269. int len, slen;
  270. if (!skb)
  271. return;
  272. len = skb->len;
  273. if ((slen = isdn_writebuf_skb_stub(info->isdn_driver,
  274. info->isdn_channel, 1, skb)) == len) {
  275. struct tty_struct *tty = info->port.tty;
  276. info->send_outstanding++;
  277. info->msr &= ~UART_MSR_CTS;
  278. info->lsr &= ~UART_LSR_TEMT;
  279. tty_wakeup(tty);
  280. return;
  281. }
  282. if (slen < 0) {
  283. /* Error: no channel, already shutdown, or wrong parameter */
  284. dev_kfree_skb(skb);
  285. return;
  286. }
  287. skb_queue_head(&info->xmit_queue, skb);
  288. }
  289. #ifdef CONFIG_ISDN_AUDIO
  290. static int
  291. isdn_tty_countDLE(unsigned char *buf, int len)
  292. {
  293. int count = 0;
  294. while (len--)
  295. if (*buf++ == DLE)
  296. count++;
  297. return count;
  298. }
  299. /* This routine is called from within isdn_tty_write() to perform
  300. * DLE-decoding when sending audio-data.
  301. */
  302. static int
  303. isdn_tty_handleDLEdown(modem_info *info, atemu *m, int len)
  304. {
  305. unsigned char *p = &info->port.xmit_buf[info->xmit_count];
  306. int count = 0;
  307. while (len > 0) {
  308. if (m->lastDLE) {
  309. m->lastDLE = 0;
  310. switch (*p) {
  311. case DLE:
  312. /* Escape code */
  313. if (len > 1)
  314. memmove(p, p + 1, len - 1);
  315. p--;
  316. count++;
  317. break;
  318. case ETX:
  319. /* End of data */
  320. info->vonline |= 4;
  321. return count;
  322. case DC4:
  323. /* Abort RX */
  324. info->vonline &= ~1;
  325. #ifdef ISDN_DEBUG_MODEM_VOICE
  326. printk(KERN_DEBUG
  327. "DLEdown: got DLE-DC4, send DLE-ETX on ttyI%d\n",
  328. info->line);
  329. #endif
  330. isdn_tty_at_cout("\020\003", info);
  331. if (!info->vonline) {
  332. #ifdef ISDN_DEBUG_MODEM_VOICE
  333. printk(KERN_DEBUG
  334. "DLEdown: send VCON on ttyI%d\n",
  335. info->line);
  336. #endif
  337. isdn_tty_at_cout("\r\nVCON\r\n", info);
  338. }
  339. /* Fall through */
  340. case 'q':
  341. case 's':
  342. /* Silence */
  343. if (len > 1)
  344. memmove(p, p + 1, len - 1);
  345. p--;
  346. break;
  347. }
  348. } else {
  349. if (*p == DLE)
  350. m->lastDLE = 1;
  351. else
  352. count++;
  353. }
  354. p++;
  355. len--;
  356. }
  357. if (len < 0) {
  358. printk(KERN_WARNING "isdn_tty: len<0 in DLEdown\n");
  359. return 0;
  360. }
  361. return count;
  362. }
  363. /* This routine is called from within isdn_tty_write() when receiving
  364. * audio-data. It interrupts receiving, if an character other than
  365. * ^S or ^Q is sent.
  366. */
  367. static int
  368. isdn_tty_end_vrx(const char *buf, int c)
  369. {
  370. char ch;
  371. while (c--) {
  372. ch = *buf;
  373. if ((ch != 0x11) && (ch != 0x13))
  374. return 1;
  375. buf++;
  376. }
  377. return 0;
  378. }
  379. static int voice_cf[7] =
  380. {0, 0, 4, 3, 2, 0, 0};
  381. #endif /* CONFIG_ISDN_AUDIO */
  382. /* isdn_tty_senddown() is called either directly from within isdn_tty_write()
  383. * or via timer-interrupt from within isdn_tty_modem_xmit(). It pulls
  384. * outgoing data from the tty's xmit-buffer, handles voice-decompression or
  385. * T.70 if necessary, and finally queues it up for sending via isdn_tty_tint.
  386. */
  387. static void
  388. isdn_tty_senddown(modem_info *info)
  389. {
  390. int buflen;
  391. int skb_res;
  392. #ifdef CONFIG_ISDN_AUDIO
  393. int audio_len;
  394. #endif
  395. struct sk_buff *skb;
  396. #ifdef CONFIG_ISDN_AUDIO
  397. if (info->vonline & 4) {
  398. info->vonline &= ~6;
  399. if (!info->vonline) {
  400. #ifdef ISDN_DEBUG_MODEM_VOICE
  401. printk(KERN_DEBUG
  402. "senddown: send VCON on ttyI%d\n",
  403. info->line);
  404. #endif
  405. isdn_tty_at_cout("\r\nVCON\r\n", info);
  406. }
  407. }
  408. #endif
  409. if (!(buflen = info->xmit_count))
  410. return;
  411. if ((info->emu.mdmreg[REG_CTS] & BIT_CTS) != 0)
  412. info->msr &= ~UART_MSR_CTS;
  413. info->lsr &= ~UART_LSR_TEMT;
  414. /* info->xmit_count is modified here and in isdn_tty_write().
  415. * So we return here if isdn_tty_write() is in the
  416. * critical section.
  417. */
  418. atomic_inc(&info->xmit_lock);
  419. if (!(atomic_dec_and_test(&info->xmit_lock)))
  420. return;
  421. if (info->isdn_driver < 0) {
  422. info->xmit_count = 0;
  423. return;
  424. }
  425. skb_res = dev->drv[info->isdn_driver]->interface->hl_hdrlen + 4;
  426. #ifdef CONFIG_ISDN_AUDIO
  427. if (info->vonline & 2)
  428. audio_len = buflen * voice_cf[info->emu.vpar[3]];
  429. else
  430. audio_len = 0;
  431. skb = dev_alloc_skb(skb_res + buflen + audio_len);
  432. #else
  433. skb = dev_alloc_skb(skb_res + buflen);
  434. #endif
  435. if (!skb) {
  436. printk(KERN_WARNING
  437. "isdn_tty: Out of memory in ttyI%d senddown\n",
  438. info->line);
  439. return;
  440. }
  441. skb_reserve(skb, skb_res);
  442. memcpy(skb_put(skb, buflen), info->port.xmit_buf, buflen);
  443. info->xmit_count = 0;
  444. #ifdef CONFIG_ISDN_AUDIO
  445. if (info->vonline & 2) {
  446. /* For now, ifmt is fixed to 1 (alaw), since this
  447. * is used with ISDN everywhere in the world, except
  448. * US, Canada and Japan.
  449. * Later, when US-ISDN protocols are implemented,
  450. * this setting will depend on the D-channel protocol.
  451. */
  452. int ifmt = 1;
  453. /* voice conversion/decompression */
  454. switch (info->emu.vpar[3]) {
  455. case 2:
  456. case 3:
  457. case 4:
  458. /* adpcm, compatible to ZyXel 1496 modem
  459. * with ROM revision 6.01
  460. */
  461. audio_len = isdn_audio_adpcm2xlaw(info->adpcms,
  462. ifmt,
  463. skb->data,
  464. skb_put(skb, audio_len),
  465. buflen);
  466. skb_pull(skb, buflen);
  467. skb_trim(skb, audio_len);
  468. break;
  469. case 5:
  470. /* a-law */
  471. if (!ifmt)
  472. isdn_audio_alaw2ulaw(skb->data,
  473. buflen);
  474. break;
  475. case 6:
  476. /* u-law */
  477. if (ifmt)
  478. isdn_audio_ulaw2alaw(skb->data,
  479. buflen);
  480. break;
  481. }
  482. }
  483. #endif /* CONFIG_ISDN_AUDIO */
  484. if (info->emu.mdmreg[REG_T70] & BIT_T70) {
  485. /* Add T.70 simplified header */
  486. if (info->emu.mdmreg[REG_T70] & BIT_T70_EXT)
  487. memcpy(skb_push(skb, 2), "\1\0", 2);
  488. else
  489. memcpy(skb_push(skb, 4), "\1\0\1\0", 4);
  490. }
  491. skb_queue_tail(&info->xmit_queue, skb);
  492. }
  493. /************************************************************
  494. *
  495. * Modem-functions
  496. *
  497. * mostly "stolen" from original Linux-serial.c and friends.
  498. *
  499. ************************************************************/
  500. /* The next routine is called once from within timer-interrupt
  501. * triggered within isdn_tty_modem_ncarrier(). It calls
  502. * isdn_tty_modem_result() to stuff a "NO CARRIER" Message
  503. * into the tty's buffer.
  504. */
  505. static void
  506. isdn_tty_modem_do_ncarrier(unsigned long data)
  507. {
  508. modem_info *info = (modem_info *) data;
  509. isdn_tty_modem_result(RESULT_NO_CARRIER, info);
  510. }
  511. /* Next routine is called, whenever the DTR-signal is raised.
  512. * It checks the ncarrier-flag, and triggers the above routine
  513. * when necessary. The ncarrier-flag is set, whenever DTR goes
  514. * low.
  515. */
  516. static void
  517. isdn_tty_modem_ncarrier(modem_info *info)
  518. {
  519. if (info->ncarrier) {
  520. info->nc_timer.expires = jiffies + HZ;
  521. add_timer(&info->nc_timer);
  522. }
  523. }
  524. /*
  525. * return the usage calculated by si and layer 2 protocol
  526. */
  527. static int
  528. isdn_calc_usage(int si, int l2)
  529. {
  530. int usg = ISDN_USAGE_MODEM;
  531. #ifdef CONFIG_ISDN_AUDIO
  532. if (si == 1) {
  533. switch (l2) {
  534. case ISDN_PROTO_L2_MODEM:
  535. usg = ISDN_USAGE_MODEM;
  536. break;
  537. #ifdef CONFIG_ISDN_TTY_FAX
  538. case ISDN_PROTO_L2_FAX:
  539. usg = ISDN_USAGE_FAX;
  540. break;
  541. #endif
  542. case ISDN_PROTO_L2_TRANS:
  543. default:
  544. usg = ISDN_USAGE_VOICE;
  545. break;
  546. }
  547. }
  548. #endif
  549. return (usg);
  550. }
  551. /* isdn_tty_dial() performs dialing of a tty an the necessary
  552. * setup of the lower levels before that.
  553. */
  554. static void
  555. isdn_tty_dial(char *n, modem_info *info, atemu *m)
  556. {
  557. int usg = ISDN_USAGE_MODEM;
  558. int si = 7;
  559. int l2 = m->mdmreg[REG_L2PROT];
  560. u_long flags;
  561. isdn_ctrl cmd;
  562. int i;
  563. int j;
  564. for (j = 7; j >= 0; j--)
  565. if (m->mdmreg[REG_SI1] & (1 << j)) {
  566. si = bit2si[j];
  567. break;
  568. }
  569. usg = isdn_calc_usage(si, l2);
  570. #ifdef CONFIG_ISDN_AUDIO
  571. if ((si == 1) &&
  572. (l2 != ISDN_PROTO_L2_MODEM)
  573. #ifdef CONFIG_ISDN_TTY_FAX
  574. && (l2 != ISDN_PROTO_L2_FAX)
  575. #endif
  576. ) {
  577. l2 = ISDN_PROTO_L2_TRANS;
  578. usg = ISDN_USAGE_VOICE;
  579. }
  580. #endif
  581. m->mdmreg[REG_SI1I] = si2bit[si];
  582. spin_lock_irqsave(&dev->lock, flags);
  583. i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
  584. if (i < 0) {
  585. spin_unlock_irqrestore(&dev->lock, flags);
  586. isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
  587. } else {
  588. info->isdn_driver = dev->drvmap[i];
  589. info->isdn_channel = dev->chanmap[i];
  590. info->drv_index = i;
  591. dev->m_idx[i] = info->line;
  592. dev->usage[i] |= ISDN_USAGE_OUTGOING;
  593. info->last_dir = 1;
  594. strcpy(info->last_num, n);
  595. isdn_info_update();
  596. spin_unlock_irqrestore(&dev->lock, flags);
  597. cmd.driver = info->isdn_driver;
  598. cmd.arg = info->isdn_channel;
  599. cmd.command = ISDN_CMD_CLREAZ;
  600. isdn_command(&cmd);
  601. strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
  602. cmd.driver = info->isdn_driver;
  603. cmd.command = ISDN_CMD_SETEAZ;
  604. isdn_command(&cmd);
  605. cmd.driver = info->isdn_driver;
  606. cmd.command = ISDN_CMD_SETL2;
  607. info->last_l2 = l2;
  608. cmd.arg = info->isdn_channel + (l2 << 8);
  609. isdn_command(&cmd);
  610. cmd.driver = info->isdn_driver;
  611. cmd.command = ISDN_CMD_SETL3;
  612. cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
  613. #ifdef CONFIG_ISDN_TTY_FAX
  614. if (l2 == ISDN_PROTO_L2_FAX) {
  615. cmd.parm.fax = info->fax;
  616. info->fax->direction = ISDN_TTY_FAX_CONN_OUT;
  617. }
  618. #endif
  619. isdn_command(&cmd);
  620. cmd.driver = info->isdn_driver;
  621. cmd.arg = info->isdn_channel;
  622. sprintf(cmd.parm.setup.phone, "%s", n);
  623. sprintf(cmd.parm.setup.eazmsn, "%s",
  624. isdn_map_eaz2msn(m->msn, info->isdn_driver));
  625. cmd.parm.setup.si1 = si;
  626. cmd.parm.setup.si2 = m->mdmreg[REG_SI2];
  627. cmd.command = ISDN_CMD_DIAL;
  628. info->dialing = 1;
  629. info->emu.carrierwait = 0;
  630. strcpy(dev->num[i], n);
  631. isdn_info_update();
  632. isdn_command(&cmd);
  633. isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
  634. }
  635. }
  636. /* isdn_tty_hangup() disassociates a tty from the real
  637. * ISDN-line (hangup). The usage-status is cleared
  638. * and some cleanup is done also.
  639. */
  640. void
  641. isdn_tty_modem_hup(modem_info *info, int local)
  642. {
  643. isdn_ctrl cmd;
  644. int di, ch;
  645. if (!info)
  646. return;
  647. di = info->isdn_driver;
  648. ch = info->isdn_channel;
  649. if (di < 0 || ch < 0)
  650. return;
  651. info->isdn_driver = -1;
  652. info->isdn_channel = -1;
  653. #ifdef ISDN_DEBUG_MODEM_HUP
  654. printk(KERN_DEBUG "Mhup ttyI%d\n", info->line);
  655. #endif
  656. info->rcvsched = 0;
  657. isdn_tty_flush_buffer(info->port.tty);
  658. if (info->online) {
  659. info->last_lhup = local;
  660. info->online = 0;
  661. isdn_tty_modem_result(RESULT_NO_CARRIER, info);
  662. }
  663. #ifdef CONFIG_ISDN_AUDIO
  664. info->vonline = 0;
  665. #ifdef CONFIG_ISDN_TTY_FAX
  666. info->faxonline = 0;
  667. info->fax->phase = ISDN_FAX_PHASE_IDLE;
  668. #endif
  669. info->emu.vpar[4] = 0;
  670. info->emu.vpar[5] = 8;
  671. kfree(info->dtmf_state);
  672. info->dtmf_state = NULL;
  673. kfree(info->silence_state);
  674. info->silence_state = NULL;
  675. kfree(info->adpcms);
  676. info->adpcms = NULL;
  677. kfree(info->adpcmr);
  678. info->adpcmr = NULL;
  679. #endif
  680. if ((info->msr & UART_MSR_RI) &&
  681. (info->emu.mdmreg[REG_RUNG] & BIT_RUNG))
  682. isdn_tty_modem_result(RESULT_RUNG, info);
  683. info->msr &= ~(UART_MSR_DCD | UART_MSR_RI);
  684. info->lsr |= UART_LSR_TEMT;
  685. if (local) {
  686. cmd.driver = di;
  687. cmd.command = ISDN_CMD_HANGUP;
  688. cmd.arg = ch;
  689. isdn_command(&cmd);
  690. }
  691. isdn_all_eaz(di, ch);
  692. info->emu.mdmreg[REG_RINGCNT] = 0;
  693. isdn_free_channel(di, ch, 0);
  694. if (info->drv_index >= 0) {
  695. dev->m_idx[info->drv_index] = -1;
  696. info->drv_index = -1;
  697. }
  698. }
  699. /*
  700. * Begin of a CAPI like interface, currently used only for
  701. * supplementary service (CAPI 2.0 part III)
  702. */
  703. #include <linux/isdn/capicmd.h>
  704. #include <linux/module.h>
  705. int
  706. isdn_tty_capi_facility(capi_msg *cm) {
  707. return (-1); /* dummy */
  708. }
  709. /* isdn_tty_suspend() tries to suspend the current tty connection
  710. */
  711. static void
  712. isdn_tty_suspend(char *id, modem_info *info, atemu *m)
  713. {
  714. isdn_ctrl cmd;
  715. int l;
  716. if (!info)
  717. return;
  718. #ifdef ISDN_DEBUG_MODEM_SERVICES
  719. printk(KERN_DEBUG "Msusp ttyI%d\n", info->line);
  720. #endif
  721. l = strlen(id);
  722. if ((info->isdn_driver >= 0)) {
  723. cmd.parm.cmsg.Length = l + 18;
  724. cmd.parm.cmsg.Command = CAPI_FACILITY;
  725. cmd.parm.cmsg.Subcommand = CAPI_REQ;
  726. cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
  727. cmd.parm.cmsg.para[0] = 3; /* 16 bit 0x0003 suplementary service */
  728. cmd.parm.cmsg.para[1] = 0;
  729. cmd.parm.cmsg.para[2] = l + 3;
  730. cmd.parm.cmsg.para[3] = 4; /* 16 bit 0x0004 Suspend */
  731. cmd.parm.cmsg.para[4] = 0;
  732. cmd.parm.cmsg.para[5] = l;
  733. strncpy(&cmd.parm.cmsg.para[6], id, l);
  734. cmd.command = CAPI_PUT_MESSAGE;
  735. cmd.driver = info->isdn_driver;
  736. cmd.arg = info->isdn_channel;
  737. isdn_command(&cmd);
  738. }
  739. }
  740. /* isdn_tty_resume() tries to resume a suspended call
  741. * setup of the lower levels before that. unfortunately here is no
  742. * checking for compatibility of used protocols implemented by Q931
  743. * It does the same things like isdn_tty_dial, the last command
  744. * is different, may be we can merge it.
  745. */
  746. static void
  747. isdn_tty_resume(char *id, modem_info *info, atemu *m)
  748. {
  749. int usg = ISDN_USAGE_MODEM;
  750. int si = 7;
  751. int l2 = m->mdmreg[REG_L2PROT];
  752. isdn_ctrl cmd;
  753. ulong flags;
  754. int i;
  755. int j;
  756. int l;
  757. l = strlen(id);
  758. for (j = 7; j >= 0; j--)
  759. if (m->mdmreg[REG_SI1] & (1 << j)) {
  760. si = bit2si[j];
  761. break;
  762. }
  763. usg = isdn_calc_usage(si, l2);
  764. #ifdef CONFIG_ISDN_AUDIO
  765. if ((si == 1) &&
  766. (l2 != ISDN_PROTO_L2_MODEM)
  767. #ifdef CONFIG_ISDN_TTY_FAX
  768. && (l2 != ISDN_PROTO_L2_FAX)
  769. #endif
  770. ) {
  771. l2 = ISDN_PROTO_L2_TRANS;
  772. usg = ISDN_USAGE_VOICE;
  773. }
  774. #endif
  775. m->mdmreg[REG_SI1I] = si2bit[si];
  776. spin_lock_irqsave(&dev->lock, flags);
  777. i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
  778. if (i < 0) {
  779. spin_unlock_irqrestore(&dev->lock, flags);
  780. isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
  781. } else {
  782. info->isdn_driver = dev->drvmap[i];
  783. info->isdn_channel = dev->chanmap[i];
  784. info->drv_index = i;
  785. dev->m_idx[i] = info->line;
  786. dev->usage[i] |= ISDN_USAGE_OUTGOING;
  787. info->last_dir = 1;
  788. // strcpy(info->last_num, n);
  789. isdn_info_update();
  790. spin_unlock_irqrestore(&dev->lock, flags);
  791. cmd.driver = info->isdn_driver;
  792. cmd.arg = info->isdn_channel;
  793. cmd.command = ISDN_CMD_CLREAZ;
  794. isdn_command(&cmd);
  795. strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
  796. cmd.driver = info->isdn_driver;
  797. cmd.command = ISDN_CMD_SETEAZ;
  798. isdn_command(&cmd);
  799. cmd.driver = info->isdn_driver;
  800. cmd.command = ISDN_CMD_SETL2;
  801. info->last_l2 = l2;
  802. cmd.arg = info->isdn_channel + (l2 << 8);
  803. isdn_command(&cmd);
  804. cmd.driver = info->isdn_driver;
  805. cmd.command = ISDN_CMD_SETL3;
  806. cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
  807. isdn_command(&cmd);
  808. cmd.driver = info->isdn_driver;
  809. cmd.arg = info->isdn_channel;
  810. cmd.parm.cmsg.Length = l + 18;
  811. cmd.parm.cmsg.Command = CAPI_FACILITY;
  812. cmd.parm.cmsg.Subcommand = CAPI_REQ;
  813. cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
  814. cmd.parm.cmsg.para[0] = 3; /* 16 bit 0x0003 suplementary service */
  815. cmd.parm.cmsg.para[1] = 0;
  816. cmd.parm.cmsg.para[2] = l + 3;
  817. cmd.parm.cmsg.para[3] = 5; /* 16 bit 0x0005 Resume */
  818. cmd.parm.cmsg.para[4] = 0;
  819. cmd.parm.cmsg.para[5] = l;
  820. strncpy(&cmd.parm.cmsg.para[6], id, l);
  821. cmd.command = CAPI_PUT_MESSAGE;
  822. info->dialing = 1;
  823. // strcpy(dev->num[i], n);
  824. isdn_info_update();
  825. isdn_command(&cmd);
  826. isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
  827. }
  828. }
  829. /* isdn_tty_send_msg() sends a message to a HL driver
  830. * This is used for hybrid modem cards to send AT commands to it
  831. */
  832. static void
  833. isdn_tty_send_msg(modem_info *info, atemu *m, char *msg)
  834. {
  835. int usg = ISDN_USAGE_MODEM;
  836. int si = 7;
  837. int l2 = m->mdmreg[REG_L2PROT];
  838. isdn_ctrl cmd;
  839. ulong flags;
  840. int i;
  841. int j;
  842. int l;
  843. l = min(strlen(msg), sizeof(cmd.parm) - sizeof(cmd.parm.cmsg)
  844. + sizeof(cmd.parm.cmsg.para) - 2);
  845. if (!l) {
  846. isdn_tty_modem_result(RESULT_ERROR, info);
  847. return;
  848. }
  849. for (j = 7; j >= 0; j--)
  850. if (m->mdmreg[REG_SI1] & (1 << j)) {
  851. si = bit2si[j];
  852. break;
  853. }
  854. usg = isdn_calc_usage(si, l2);
  855. #ifdef CONFIG_ISDN_AUDIO
  856. if ((si == 1) &&
  857. (l2 != ISDN_PROTO_L2_MODEM)
  858. #ifdef CONFIG_ISDN_TTY_FAX
  859. && (l2 != ISDN_PROTO_L2_FAX)
  860. #endif
  861. ) {
  862. l2 = ISDN_PROTO_L2_TRANS;
  863. usg = ISDN_USAGE_VOICE;
  864. }
  865. #endif
  866. m->mdmreg[REG_SI1I] = si2bit[si];
  867. spin_lock_irqsave(&dev->lock, flags);
  868. i = isdn_get_free_channel(usg, l2, m->mdmreg[REG_L3PROT], -1, -1, m->msn);
  869. if (i < 0) {
  870. spin_unlock_irqrestore(&dev->lock, flags);
  871. isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
  872. } else {
  873. info->isdn_driver = dev->drvmap[i];
  874. info->isdn_channel = dev->chanmap[i];
  875. info->drv_index = i;
  876. dev->m_idx[i] = info->line;
  877. dev->usage[i] |= ISDN_USAGE_OUTGOING;
  878. info->last_dir = 1;
  879. isdn_info_update();
  880. spin_unlock_irqrestore(&dev->lock, flags);
  881. cmd.driver = info->isdn_driver;
  882. cmd.arg = info->isdn_channel;
  883. cmd.command = ISDN_CMD_CLREAZ;
  884. isdn_command(&cmd);
  885. strcpy(cmd.parm.num, isdn_map_eaz2msn(m->msn, info->isdn_driver));
  886. cmd.driver = info->isdn_driver;
  887. cmd.command = ISDN_CMD_SETEAZ;
  888. isdn_command(&cmd);
  889. cmd.driver = info->isdn_driver;
  890. cmd.command = ISDN_CMD_SETL2;
  891. info->last_l2 = l2;
  892. cmd.arg = info->isdn_channel + (l2 << 8);
  893. isdn_command(&cmd);
  894. cmd.driver = info->isdn_driver;
  895. cmd.command = ISDN_CMD_SETL3;
  896. cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
  897. isdn_command(&cmd);
  898. cmd.driver = info->isdn_driver;
  899. cmd.arg = info->isdn_channel;
  900. cmd.parm.cmsg.Length = l + 14;
  901. cmd.parm.cmsg.Command = CAPI_MANUFACTURER;
  902. cmd.parm.cmsg.Subcommand = CAPI_REQ;
  903. cmd.parm.cmsg.adr.Controller = info->isdn_driver + 1;
  904. cmd.parm.cmsg.para[0] = l + 1;
  905. strncpy(&cmd.parm.cmsg.para[1], msg, l);
  906. cmd.parm.cmsg.para[l + 1] = 0xd;
  907. cmd.command = CAPI_PUT_MESSAGE;
  908. /* info->dialing = 1;
  909. strcpy(dev->num[i], n);
  910. isdn_info_update();
  911. */
  912. isdn_command(&cmd);
  913. }
  914. }
  915. static inline int
  916. isdn_tty_paranoia_check(modem_info *info, char *name, const char *routine)
  917. {
  918. #ifdef MODEM_PARANOIA_CHECK
  919. if (!info) {
  920. printk(KERN_WARNING "isdn_tty: null info_struct for %s in %s\n",
  921. name, routine);
  922. return 1;
  923. }
  924. if (info->magic != ISDN_ASYNC_MAGIC) {
  925. printk(KERN_WARNING "isdn_tty: bad magic for modem struct %s in %s\n",
  926. name, routine);
  927. return 1;
  928. }
  929. #endif
  930. return 0;
  931. }
  932. /*
  933. * This routine is called to set the UART divisor registers to match
  934. * the specified baud rate for a serial port.
  935. */
  936. static void
  937. isdn_tty_change_speed(modem_info *info)
  938. {
  939. struct tty_port *port = &info->port;
  940. uint cflag,
  941. cval,
  942. quot;
  943. int i;
  944. if (!port->tty)
  945. return;
  946. cflag = port->tty->termios.c_cflag;
  947. quot = i = cflag & CBAUD;
  948. if (i & CBAUDEX) {
  949. i &= ~CBAUDEX;
  950. if (i < 1 || i > 2)
  951. port->tty->termios.c_cflag &= ~CBAUDEX;
  952. else
  953. i += 15;
  954. }
  955. if (quot) {
  956. info->mcr |= UART_MCR_DTR;
  957. isdn_tty_modem_ncarrier(info);
  958. } else {
  959. info->mcr &= ~UART_MCR_DTR;
  960. if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
  961. #ifdef ISDN_DEBUG_MODEM_HUP
  962. printk(KERN_DEBUG "Mhup in changespeed\n");
  963. #endif
  964. if (info->online)
  965. info->ncarrier = 1;
  966. isdn_tty_modem_reset_regs(info, 0);
  967. isdn_tty_modem_hup(info, 1);
  968. }
  969. return;
  970. }
  971. /* byte size and parity */
  972. cval = cflag & (CSIZE | CSTOPB);
  973. cval >>= 4;
  974. if (cflag & PARENB)
  975. cval |= UART_LCR_PARITY;
  976. if (!(cflag & PARODD))
  977. cval |= UART_LCR_EPAR;
  978. /* CTS flow control flag and modem status interrupts */
  979. if (cflag & CRTSCTS) {
  980. port->flags |= ASYNC_CTS_FLOW;
  981. } else
  982. port->flags &= ~ASYNC_CTS_FLOW;
  983. if (cflag & CLOCAL)
  984. port->flags &= ~ASYNC_CHECK_CD;
  985. else {
  986. port->flags |= ASYNC_CHECK_CD;
  987. }
  988. }
  989. static int
  990. isdn_tty_startup(modem_info *info)
  991. {
  992. if (info->port.flags & ASYNC_INITIALIZED)
  993. return 0;
  994. isdn_lock_drivers();
  995. #ifdef ISDN_DEBUG_MODEM_OPEN
  996. printk(KERN_DEBUG "starting up ttyi%d ...\n", info->line);
  997. #endif
  998. /*
  999. * Now, initialize the UART
  1000. */
  1001. info->mcr = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
  1002. if (info->port.tty)
  1003. clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
  1004. /*
  1005. * and set the speed of the serial port
  1006. */
  1007. isdn_tty_change_speed(info);
  1008. info->port.flags |= ASYNC_INITIALIZED;
  1009. info->msr |= (UART_MSR_DSR | UART_MSR_CTS);
  1010. info->send_outstanding = 0;
  1011. return 0;
  1012. }
  1013. /*
  1014. * This routine will shutdown a serial port; interrupts are disabled, and
  1015. * DTR is dropped if the hangup on close termio flag is on.
  1016. */
  1017. static void
  1018. isdn_tty_shutdown(modem_info *info)
  1019. {
  1020. if (!(info->port.flags & ASYNC_INITIALIZED))
  1021. return;
  1022. #ifdef ISDN_DEBUG_MODEM_OPEN
  1023. printk(KERN_DEBUG "Shutting down isdnmodem port %d ....\n", info->line);
  1024. #endif
  1025. isdn_unlock_drivers();
  1026. info->msr &= ~UART_MSR_RI;
  1027. if (!info->port.tty || (info->port.tty->termios.c_cflag & HUPCL)) {
  1028. info->mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
  1029. if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
  1030. isdn_tty_modem_reset_regs(info, 0);
  1031. #ifdef ISDN_DEBUG_MODEM_HUP
  1032. printk(KERN_DEBUG "Mhup in isdn_tty_shutdown\n");
  1033. #endif
  1034. isdn_tty_modem_hup(info, 1);
  1035. }
  1036. }
  1037. if (info->port.tty)
  1038. set_bit(TTY_IO_ERROR, &info->port.tty->flags);
  1039. info->port.flags &= ~ASYNC_INITIALIZED;
  1040. }
  1041. /* isdn_tty_write() is the main send-routine. It is called from the upper
  1042. * levels within the kernel to perform sending data. Depending on the
  1043. * online-flag it either directs output to the at-command-interpreter or
  1044. * to the lower level. Additional tasks done here:
  1045. * - If online, check for escape-sequence (+++)
  1046. * - If sending audio-data, call isdn_tty_DLEdown() to parse DLE-codes.
  1047. * - If receiving audio-data, call isdn_tty_end_vrx() to abort if needed.
  1048. * - If dialing, abort dial.
  1049. */
  1050. static int
  1051. isdn_tty_write(struct tty_struct *tty, const u_char *buf, int count)
  1052. {
  1053. int c;
  1054. int total = 0;
  1055. modem_info *info = (modem_info *) tty->driver_data;
  1056. atemu *m = &info->emu;
  1057. if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_write"))
  1058. return 0;
  1059. /* See isdn_tty_senddown() */
  1060. atomic_inc(&info->xmit_lock);
  1061. while (1) {
  1062. c = count;
  1063. if (c > info->xmit_size - info->xmit_count)
  1064. c = info->xmit_size - info->xmit_count;
  1065. if (info->isdn_driver >= 0 && c > dev->drv[info->isdn_driver]->maxbufsize)
  1066. c = dev->drv[info->isdn_driver]->maxbufsize;
  1067. if (c <= 0)
  1068. break;
  1069. if ((info->online > 1)
  1070. #ifdef CONFIG_ISDN_AUDIO
  1071. || (info->vonline & 3)
  1072. #endif
  1073. ) {
  1074. #ifdef CONFIG_ISDN_AUDIO
  1075. if (!info->vonline)
  1076. #endif
  1077. isdn_tty_check_esc(buf, m->mdmreg[REG_ESC], c,
  1078. &(m->pluscount),
  1079. &(m->lastplus));
  1080. memcpy(&info->port.xmit_buf[info->xmit_count], buf, c);
  1081. #ifdef CONFIG_ISDN_AUDIO
  1082. if (info->vonline) {
  1083. int cc = isdn_tty_handleDLEdown(info, m, c);
  1084. if (info->vonline & 2) {
  1085. if (!cc) {
  1086. /* If DLE decoding results in zero-transmit, but
  1087. * c originally was non-zero, do a wakeup.
  1088. */
  1089. tty_wakeup(tty);
  1090. info->msr |= UART_MSR_CTS;
  1091. info->lsr |= UART_LSR_TEMT;
  1092. }
  1093. info->xmit_count += cc;
  1094. }
  1095. if ((info->vonline & 3) == 1) {
  1096. /* Do NOT handle Ctrl-Q or Ctrl-S
  1097. * when in full-duplex audio mode.
  1098. */
  1099. if (isdn_tty_end_vrx(buf, c)) {
  1100. info->vonline &= ~1;
  1101. #ifdef ISDN_DEBUG_MODEM_VOICE
  1102. printk(KERN_DEBUG
  1103. "got !^Q/^S, send DLE-ETX,VCON on ttyI%d\n",
  1104. info->line);
  1105. #endif
  1106. isdn_tty_at_cout("\020\003\r\nVCON\r\n", info);
  1107. }
  1108. }
  1109. } else
  1110. if (TTY_IS_FCLASS1(info)) {
  1111. int cc = isdn_tty_handleDLEdown(info, m, c);
  1112. if (info->vonline & 4) { /* ETX seen */
  1113. isdn_ctrl c;
  1114. c.command = ISDN_CMD_FAXCMD;
  1115. c.driver = info->isdn_driver;
  1116. c.arg = info->isdn_channel;
  1117. c.parm.aux.cmd = ISDN_FAX_CLASS1_CTRL;
  1118. c.parm.aux.subcmd = ETX;
  1119. isdn_command(&c);
  1120. }
  1121. info->vonline = 0;
  1122. #ifdef ISDN_DEBUG_MODEM_VOICE
  1123. printk(KERN_DEBUG "fax dle cc/c %d/%d\n", cc, c);
  1124. #endif
  1125. info->xmit_count += cc;
  1126. } else
  1127. #endif
  1128. info->xmit_count += c;
  1129. } else {
  1130. info->msr |= UART_MSR_CTS;
  1131. info->lsr |= UART_LSR_TEMT;
  1132. if (info->dialing) {
  1133. info->dialing = 0;
  1134. #ifdef ISDN_DEBUG_MODEM_HUP
  1135. printk(KERN_DEBUG "Mhup in isdn_tty_write\n");
  1136. #endif
  1137. isdn_tty_modem_result(RESULT_NO_CARRIER, info);
  1138. isdn_tty_modem_hup(info, 1);
  1139. } else
  1140. c = isdn_tty_edit_at(buf, c, info);
  1141. }
  1142. buf += c;
  1143. count -= c;
  1144. total += c;
  1145. }
  1146. atomic_dec(&info->xmit_lock);
  1147. if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue)) {
  1148. if (m->mdmreg[REG_DXMT] & BIT_DXMT) {
  1149. isdn_tty_senddown(info);
  1150. isdn_tty_tint(info);
  1151. }
  1152. isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, 1);
  1153. }
  1154. return total;
  1155. }
  1156. static int
  1157. isdn_tty_write_room(struct tty_struct *tty)
  1158. {
  1159. modem_info *info = (modem_info *) tty->driver_data;
  1160. int ret;
  1161. if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_write_room"))
  1162. return 0;
  1163. if (!info->online)
  1164. return info->xmit_size;
  1165. ret = info->xmit_size - info->xmit_count;
  1166. return (ret < 0) ? 0 : ret;
  1167. }
  1168. static int
  1169. isdn_tty_chars_in_buffer(struct tty_struct *tty)
  1170. {
  1171. modem_info *info = (modem_info *) tty->driver_data;
  1172. if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_chars_in_buffer"))
  1173. return 0;
  1174. if (!info->online)
  1175. return 0;
  1176. return (info->xmit_count);
  1177. }
  1178. static void
  1179. isdn_tty_flush_buffer(struct tty_struct *tty)
  1180. {
  1181. modem_info *info;
  1182. if (!tty) {
  1183. return;
  1184. }
  1185. info = (modem_info *) tty->driver_data;
  1186. if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_flush_buffer")) {
  1187. return;
  1188. }
  1189. isdn_tty_cleanup_xmit(info);
  1190. info->xmit_count = 0;
  1191. tty_wakeup(tty);
  1192. }
  1193. static void
  1194. isdn_tty_flush_chars(struct tty_struct *tty)
  1195. {
  1196. modem_info *info = (modem_info *) tty->driver_data;
  1197. if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_flush_chars"))
  1198. return;
  1199. if ((info->xmit_count) || !skb_queue_empty(&info->xmit_queue))
  1200. isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, 1);
  1201. }
  1202. /*
  1203. * ------------------------------------------------------------
  1204. * isdn_tty_throttle()
  1205. *
  1206. * This routine is called by the upper-layer tty layer to signal that
  1207. * incoming characters should be throttled.
  1208. * ------------------------------------------------------------
  1209. */
  1210. static void
  1211. isdn_tty_throttle(struct tty_struct *tty)
  1212. {
  1213. modem_info *info = (modem_info *) tty->driver_data;
  1214. if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_throttle"))
  1215. return;
  1216. if (I_IXOFF(tty))
  1217. info->x_char = STOP_CHAR(tty);
  1218. info->mcr &= ~UART_MCR_RTS;
  1219. }
  1220. static void
  1221. isdn_tty_unthrottle(struct tty_struct *tty)
  1222. {
  1223. modem_info *info = (modem_info *) tty->driver_data;
  1224. if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_unthrottle"))
  1225. return;
  1226. if (I_IXOFF(tty)) {
  1227. if (info->x_char)
  1228. info->x_char = 0;
  1229. else
  1230. info->x_char = START_CHAR(tty);
  1231. }
  1232. info->mcr |= UART_MCR_RTS;
  1233. }
  1234. /*
  1235. * ------------------------------------------------------------
  1236. * isdn_tty_ioctl() and friends
  1237. * ------------------------------------------------------------
  1238. */
  1239. /*
  1240. * isdn_tty_get_lsr_info - get line status register info
  1241. *
  1242. * Purpose: Let user call ioctl() to get info when the UART physically
  1243. * is emptied. On bus types like RS485, the transmitter must
  1244. * release the bus after transmitting. This must be done when
  1245. * the transmit shift register is empty, not be done when the
  1246. * transmit holding register is empty. This functionality
  1247. * allows RS485 driver to be written in user space.
  1248. */
  1249. static int
  1250. isdn_tty_get_lsr_info(modem_info *info, uint __user *value)
  1251. {
  1252. u_char status;
  1253. uint result;
  1254. status = info->lsr;
  1255. result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
  1256. return put_user(result, value);
  1257. }
  1258. static int
  1259. isdn_tty_tiocmget(struct tty_struct *tty)
  1260. {
  1261. modem_info *info = (modem_info *) tty->driver_data;
  1262. u_char control, status;
  1263. if (isdn_tty_paranoia_check(info, tty->name, __func__))
  1264. return -ENODEV;
  1265. if (tty->flags & (1 << TTY_IO_ERROR))
  1266. return -EIO;
  1267. mutex_lock(&modem_info_mutex);
  1268. #ifdef ISDN_DEBUG_MODEM_IOCTL
  1269. printk(KERN_DEBUG "ttyI%d ioctl TIOCMGET\n", info->line);
  1270. #endif
  1271. control = info->mcr;
  1272. status = info->msr;
  1273. mutex_unlock(&modem_info_mutex);
  1274. return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
  1275. | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
  1276. | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
  1277. | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
  1278. | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
  1279. | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
  1280. }
  1281. static int
  1282. isdn_tty_tiocmset(struct tty_struct *tty,
  1283. unsigned int set, unsigned int clear)
  1284. {
  1285. modem_info *info = (modem_info *) tty->driver_data;
  1286. if (isdn_tty_paranoia_check(info, tty->name, __func__))
  1287. return -ENODEV;
  1288. if (tty->flags & (1 << TTY_IO_ERROR))
  1289. return -EIO;
  1290. #ifdef ISDN_DEBUG_MODEM_IOCTL
  1291. printk(KERN_DEBUG "ttyI%d ioctl TIOCMxxx: %x %x\n", info->line, set, clear);
  1292. #endif
  1293. mutex_lock(&modem_info_mutex);
  1294. if (set & TIOCM_RTS)
  1295. info->mcr |= UART_MCR_RTS;
  1296. if (set & TIOCM_DTR) {
  1297. info->mcr |= UART_MCR_DTR;
  1298. isdn_tty_modem_ncarrier(info);
  1299. }
  1300. if (clear & TIOCM_RTS)
  1301. info->mcr &= ~UART_MCR_RTS;
  1302. if (clear & TIOCM_DTR) {
  1303. info->mcr &= ~UART_MCR_DTR;
  1304. if (info->emu.mdmreg[REG_DTRHUP] & BIT_DTRHUP) {
  1305. isdn_tty_modem_reset_regs(info, 0);
  1306. #ifdef ISDN_DEBUG_MODEM_HUP
  1307. printk(KERN_DEBUG "Mhup in TIOCMSET\n");
  1308. #endif
  1309. if (info->online)
  1310. info->ncarrier = 1;
  1311. isdn_tty_modem_hup(info, 1);
  1312. }
  1313. }
  1314. mutex_unlock(&modem_info_mutex);
  1315. return 0;
  1316. }
  1317. static int
  1318. isdn_tty_ioctl(struct tty_struct *tty, uint cmd, ulong arg)
  1319. {
  1320. modem_info *info = (modem_info *) tty->driver_data;
  1321. int retval;
  1322. if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_ioctl"))
  1323. return -ENODEV;
  1324. if (tty->flags & (1 << TTY_IO_ERROR))
  1325. return -EIO;
  1326. switch (cmd) {
  1327. case TCSBRK: /* SVID version: non-zero arg --> no break */
  1328. #ifdef ISDN_DEBUG_MODEM_IOCTL
  1329. printk(KERN_DEBUG "ttyI%d ioctl TCSBRK\n", info->line);
  1330. #endif
  1331. retval = tty_check_change(tty);
  1332. if (retval)
  1333. return retval;
  1334. tty_wait_until_sent(tty, 0);
  1335. return 0;
  1336. case TCSBRKP: /* support for POSIX tcsendbreak() */
  1337. #ifdef ISDN_DEBUG_MODEM_IOCTL
  1338. printk(KERN_DEBUG "ttyI%d ioctl TCSBRKP\n", info->line);
  1339. #endif
  1340. retval = tty_check_change(tty);
  1341. if (retval)
  1342. return retval;
  1343. tty_wait_until_sent(tty, 0);
  1344. return 0;
  1345. case TIOCSERGETLSR: /* Get line status register */
  1346. #ifdef ISDN_DEBUG_MODEM_IOCTL
  1347. printk(KERN_DEBUG "ttyI%d ioctl TIOCSERGETLSR\n", info->line);
  1348. #endif
  1349. return isdn_tty_get_lsr_info(info, (uint __user *) arg);
  1350. default:
  1351. #ifdef ISDN_DEBUG_MODEM_IOCTL
  1352. printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on ttyi%d\n", cmd, info->line);
  1353. #endif
  1354. return -ENOIOCTLCMD;
  1355. }
  1356. return 0;
  1357. }
  1358. static void
  1359. isdn_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  1360. {
  1361. modem_info *info = (modem_info *) tty->driver_data;
  1362. if (!old_termios)
  1363. isdn_tty_change_speed(info);
  1364. else {
  1365. if (tty->termios.c_cflag == old_termios->c_cflag &&
  1366. tty->termios.c_ispeed == old_termios->c_ispeed &&
  1367. tty->termios.c_ospeed == old_termios->c_ospeed)
  1368. return;
  1369. isdn_tty_change_speed(info);
  1370. if ((old_termios->c_cflag & CRTSCTS) &&
  1371. !(tty->termios.c_cflag & CRTSCTS))
  1372. tty->hw_stopped = 0;
  1373. }
  1374. }
  1375. /*
  1376. * ------------------------------------------------------------
  1377. * isdn_tty_open() and friends
  1378. * ------------------------------------------------------------
  1379. */
  1380. static int isdn_tty_install(struct tty_driver *driver, struct tty_struct *tty)
  1381. {
  1382. modem_info *info = &dev->mdm.info[tty->index];
  1383. if (isdn_tty_paranoia_check(info, tty->name, __func__))
  1384. return -ENODEV;
  1385. tty->driver_data = info;
  1386. return tty_port_install(&info->port, driver, tty);
  1387. }
  1388. /*
  1389. * This routine is called whenever a serial port is opened. It
  1390. * enables interrupts for a serial port, linking in its async structure into
  1391. * the IRQ chain. It also performs the serial-specific
  1392. * initialization for the tty structure.
  1393. */
  1394. static int
  1395. isdn_tty_open(struct tty_struct *tty, struct file *filp)
  1396. {
  1397. modem_info *info = tty->driver_data;
  1398. struct tty_port *port = &info->port;
  1399. int retval;
  1400. #ifdef ISDN_DEBUG_MODEM_OPEN
  1401. printk(KERN_DEBUG "isdn_tty_open %s, count = %d\n", tty->name,
  1402. port->count);
  1403. #endif
  1404. port->count++;
  1405. port->tty = tty;
  1406. /*
  1407. * Start up serial port
  1408. */
  1409. retval = isdn_tty_startup(info);
  1410. if (retval) {
  1411. #ifdef ISDN_DEBUG_MODEM_OPEN
  1412. printk(KERN_DEBUG "isdn_tty_open return after startup\n");
  1413. #endif
  1414. return retval;
  1415. }
  1416. retval = tty_port_block_til_ready(port, tty, filp);
  1417. if (retval) {
  1418. #ifdef ISDN_DEBUG_MODEM_OPEN
  1419. printk(KERN_DEBUG "isdn_tty_open return after isdn_tty_block_til_ready \n");
  1420. #endif
  1421. return retval;
  1422. }
  1423. #ifdef ISDN_DEBUG_MODEM_OPEN
  1424. printk(KERN_DEBUG "isdn_tty_open ttyi%d successful...\n", info->line);
  1425. #endif
  1426. dev->modempoll++;
  1427. #ifdef ISDN_DEBUG_MODEM_OPEN
  1428. printk(KERN_DEBUG "isdn_tty_open normal exit\n");
  1429. #endif
  1430. return 0;
  1431. }
  1432. static void
  1433. isdn_tty_close(struct tty_struct *tty, struct file *filp)
  1434. {
  1435. modem_info *info = (modem_info *) tty->driver_data;
  1436. struct tty_port *port = &info->port;
  1437. ulong timeout;
  1438. if (!info || isdn_tty_paranoia_check(info, tty->name, "isdn_tty_close"))
  1439. return;
  1440. if (tty_hung_up_p(filp)) {
  1441. #ifdef ISDN_DEBUG_MODEM_OPEN
  1442. printk(KERN_DEBUG "isdn_tty_close return after tty_hung_up_p\n");
  1443. #endif
  1444. return;
  1445. }
  1446. if ((tty->count == 1) && (port->count != 1)) {
  1447. /*
  1448. * Uh, oh. tty->count is 1, which means that the tty
  1449. * structure will be freed. Info->count should always
  1450. * be one in these conditions. If it's greater than
  1451. * one, we've got real problems, since it means the
  1452. * serial port won't be shutdown.
  1453. */
  1454. printk(KERN_ERR "isdn_tty_close: bad port count; tty->count is 1, "
  1455. "info->count is %d\n", port->count);
  1456. port->count = 1;
  1457. }
  1458. if (--port->count < 0) {
  1459. printk(KERN_ERR "isdn_tty_close: bad port count for ttyi%d: %d\n",
  1460. info->line, port->count);
  1461. port->count = 0;
  1462. }
  1463. if (port->count) {
  1464. #ifdef ISDN_DEBUG_MODEM_OPEN
  1465. printk(KERN_DEBUG "isdn_tty_close after info->count != 0\n");
  1466. #endif
  1467. return;
  1468. }
  1469. port->flags |= ASYNC_CLOSING;
  1470. tty->closing = 1;
  1471. /*
  1472. * At this point we stop accepting input. To do this, we
  1473. * disable the receive line status interrupts, and tell the
  1474. * interrupt driver to stop checking the data ready bit in the
  1475. * line status register.
  1476. */
  1477. if (port->flags & ASYNC_INITIALIZED) {
  1478. tty_wait_until_sent_from_close(tty, 3000); /* 30 seconds timeout */
  1479. /*
  1480. * Before we drop DTR, make sure the UART transmitter
  1481. * has completely drained; this is especially
  1482. * important if there is a transmit FIFO!
  1483. */
  1484. timeout = jiffies + HZ;
  1485. while (!(info->lsr & UART_LSR_TEMT)) {
  1486. schedule_timeout_interruptible(20);
  1487. if (time_after(jiffies, timeout))
  1488. break;
  1489. }
  1490. }
  1491. dev->modempoll--;
  1492. isdn_tty_shutdown(info);
  1493. isdn_tty_flush_buffer(tty);
  1494. tty_ldisc_flush(tty);
  1495. port->tty = NULL;
  1496. info->ncarrier = 0;
  1497. tty_port_close_end(port, tty);
  1498. #ifdef ISDN_DEBUG_MODEM_OPEN
  1499. printk(KERN_DEBUG "isdn_tty_close normal exit\n");
  1500. #endif
  1501. }
  1502. /*
  1503. * isdn_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
  1504. */
  1505. static void
  1506. isdn_tty_hangup(struct tty_struct *tty)
  1507. {
  1508. modem_info *info = (modem_info *) tty->driver_data;
  1509. struct tty_port *port = &info->port;
  1510. if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_hangup"))
  1511. return;
  1512. isdn_tty_shutdown(info);
  1513. port->count = 0;
  1514. port->flags &= ~ASYNC_NORMAL_ACTIVE;
  1515. port->tty = NULL;
  1516. wake_up_interruptible(&port->open_wait);
  1517. }
  1518. /* This routine initializes all emulator-data.
  1519. */
  1520. static void
  1521. isdn_tty_reset_profile(atemu *m)
  1522. {
  1523. m->profile[0] = 0;
  1524. m->profile[1] = 0;
  1525. m->profile[2] = 43;
  1526. m->profile[3] = 13;
  1527. m->profile[4] = 10;
  1528. m->profile[5] = 8;
  1529. m->profile[6] = 3;
  1530. m->profile[7] = 60;
  1531. m->profile[8] = 2;
  1532. m->profile[9] = 6;
  1533. m->profile[10] = 7;
  1534. m->profile[11] = 70;
  1535. m->profile[12] = 0x45;
  1536. m->profile[13] = 4;
  1537. m->profile[14] = ISDN_PROTO_L2_X75I;
  1538. m->profile[15] = ISDN_PROTO_L3_TRANS;
  1539. m->profile[16] = ISDN_SERIAL_XMIT_SIZE / 16;
  1540. m->profile[17] = ISDN_MODEM_WINSIZE;
  1541. m->profile[18] = 4;
  1542. m->profile[19] = 0;
  1543. m->profile[20] = 0;
  1544. m->profile[23] = 0;
  1545. m->pmsn[0] = '\0';
  1546. m->plmsn[0] = '\0';
  1547. }
  1548. #ifdef CONFIG_ISDN_AUDIO
  1549. static void
  1550. isdn_tty_modem_reset_vpar(atemu *m)
  1551. {
  1552. m->vpar[0] = 2; /* Voice-device (2 = phone line) */
  1553. m->vpar[1] = 0; /* Silence detection level (0 = none ) */
  1554. m->vpar[2] = 70; /* Silence interval (7 sec. ) */
  1555. m->vpar[3] = 2; /* Compression type (1 = ADPCM-2 ) */
  1556. m->vpar[4] = 0; /* DTMF detection level (0 = softcode ) */
  1557. m->vpar[5] = 8; /* DTMF interval (8 * 5 ms. ) */
  1558. }
  1559. #endif
  1560. #ifdef CONFIG_ISDN_TTY_FAX
  1561. static void
  1562. isdn_tty_modem_reset_faxpar(modem_info *info)
  1563. {
  1564. T30_s *f = info->fax;
  1565. f->code = 0;
  1566. f->phase = ISDN_FAX_PHASE_IDLE;
  1567. f->direction = 0;
  1568. f->resolution = 1; /* fine */
  1569. f->rate = 5; /* 14400 bit/s */
  1570. f->width = 0;
  1571. f->length = 0;
  1572. f->compression = 0;
  1573. f->ecm = 0;
  1574. f->binary = 0;
  1575. f->scantime = 0;
  1576. memset(&f->id[0], 32, FAXIDLEN - 1);
  1577. f->id[FAXIDLEN - 1] = 0;
  1578. f->badlin = 0;
  1579. f->badmul = 0;
  1580. f->bor = 0;
  1581. f->nbc = 0;
  1582. f->cq = 0;
  1583. f->cr = 0;
  1584. f->ctcrty = 0;
  1585. f->minsp = 0;
  1586. f->phcto = 30;
  1587. f->rel = 0;
  1588. memset(&f->pollid[0], 32, FAXIDLEN - 1);
  1589. f->pollid[FAXIDLEN - 1] = 0;
  1590. }
  1591. #endif
  1592. static void
  1593. isdn_tty_modem_reset_regs(modem_info *info, int force)
  1594. {
  1595. atemu *m = &info->emu;
  1596. if ((m->mdmreg[REG_DTRR] & BIT_DTRR) || force) {
  1597. memcpy(m->mdmreg, m->profile, ISDN_MODEM_NUMREG);
  1598. memcpy(m->msn, m->pmsn, ISDN_MSNLEN);
  1599. memcpy(m->lmsn, m->plmsn, ISDN_LMSNLEN);
  1600. info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
  1601. }
  1602. #ifdef CONFIG_ISDN_AUDIO
  1603. isdn_tty_modem_reset_vpar(m);
  1604. #endif
  1605. #ifdef CONFIG_ISDN_TTY_FAX
  1606. isdn_tty_modem_reset_faxpar(info);
  1607. #endif
  1608. m->mdmcmdl = 0;
  1609. }
  1610. static void
  1611. modem_write_profile(atemu *m)
  1612. {
  1613. memcpy(m->profile, m->mdmreg, ISDN_MODEM_NUMREG);
  1614. memcpy(m->pmsn, m->msn, ISDN_MSNLEN);
  1615. memcpy(m->plmsn, m->lmsn, ISDN_LMSNLEN);
  1616. if (dev->profd)
  1617. send_sig(SIGIO, dev->profd, 1);
  1618. }
  1619. static const struct tty_operations modem_ops = {
  1620. .install = isdn_tty_install,
  1621. .open = isdn_tty_open,
  1622. .close = isdn_tty_close,
  1623. .write = isdn_tty_write,
  1624. .flush_chars = isdn_tty_flush_chars,
  1625. .write_room = isdn_tty_write_room,
  1626. .chars_in_buffer = isdn_tty_chars_in_buffer,
  1627. .flush_buffer = isdn_tty_flush_buffer,
  1628. .ioctl = isdn_tty_ioctl,
  1629. .throttle = isdn_tty_throttle,
  1630. .unthrottle = isdn_tty_unthrottle,
  1631. .set_termios = isdn_tty_set_termios,
  1632. .hangup = isdn_tty_hangup,
  1633. .tiocmget = isdn_tty_tiocmget,
  1634. .tiocmset = isdn_tty_tiocmset,
  1635. };
  1636. static int isdn_tty_carrier_raised(struct tty_port *port)
  1637. {
  1638. modem_info *info = container_of(port, modem_info, port);
  1639. return info->msr & UART_MSR_DCD;
  1640. }
  1641. static const struct tty_port_operations isdn_tty_port_ops = {
  1642. .carrier_raised = isdn_tty_carrier_raised,
  1643. };
  1644. int
  1645. isdn_tty_modem_init(void)
  1646. {
  1647. isdn_modem_t *m;
  1648. int i, retval;
  1649. modem_info *info;
  1650. m = &dev->mdm;
  1651. m->tty_modem = alloc_tty_driver(ISDN_MAX_CHANNELS);
  1652. if (!m->tty_modem)
  1653. return -ENOMEM;
  1654. m->tty_modem->name = "ttyI";
  1655. m->tty_modem->major = ISDN_TTY_MAJOR;
  1656. m->tty_modem->minor_start = 0;
  1657. m->tty_modem->type = TTY_DRIVER_TYPE_SERIAL;
  1658. m->tty_modem->subtype = SERIAL_TYPE_NORMAL;
  1659. m->tty_modem->init_termios = tty_std_termios;
  1660. m->tty_modem->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1661. m->tty_modem->flags = TTY_DRIVER_REAL_RAW;
  1662. m->tty_modem->driver_name = "isdn_tty";
  1663. tty_set_operations(m->tty_modem, &modem_ops);
  1664. retval = tty_register_driver(m->tty_modem);
  1665. if (retval) {
  1666. printk(KERN_WARNING "isdn_tty: Couldn't register modem-device\n");
  1667. goto err;
  1668. }
  1669. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  1670. info = &m->info[i];
  1671. #ifdef CONFIG_ISDN_TTY_FAX
  1672. if (!(info->fax = kmalloc(sizeof(T30_s), GFP_KERNEL))) {
  1673. printk(KERN_ERR "Could not allocate fax t30-buffer\n");
  1674. retval = -ENOMEM;
  1675. goto err_unregister;
  1676. }
  1677. #endif
  1678. tty_port_init(&info->port);
  1679. info->port.ops = &isdn_tty_port_ops;
  1680. spin_lock_init(&info->readlock);
  1681. sprintf(info->last_cause, "0000");
  1682. sprintf(info->last_num, "none");
  1683. info->last_dir = 0;
  1684. info->last_lhup = 1;
  1685. info->last_l2 = -1;
  1686. info->last_si = 0;
  1687. isdn_tty_reset_profile(&info->emu);
  1688. isdn_tty_modem_reset_regs(info, 1);
  1689. info->magic = ISDN_ASYNC_MAGIC;
  1690. info->line = i;
  1691. info->x_char = 0;
  1692. info->isdn_driver = -1;
  1693. info->isdn_channel = -1;
  1694. info->drv_index = -1;
  1695. info->xmit_size = ISDN_SERIAL_XMIT_SIZE;
  1696. init_timer(&info->nc_timer);
  1697. info->nc_timer.function = isdn_tty_modem_do_ncarrier;
  1698. info->nc_timer.data = (unsigned long) info;
  1699. skb_queue_head_init(&info->xmit_queue);
  1700. #ifdef CONFIG_ISDN_AUDIO
  1701. skb_queue_head_init(&info->dtmf_queue);
  1702. #endif
  1703. info->port.xmit_buf = kmalloc(ISDN_SERIAL_XMIT_MAX + 5,
  1704. GFP_KERNEL);
  1705. if (!info->port.xmit_buf) {
  1706. printk(KERN_ERR "Could not allocate modem xmit-buffer\n");
  1707. retval = -ENOMEM;
  1708. goto err_unregister;
  1709. }
  1710. /* Make room for T.70 header */
  1711. info->port.xmit_buf += 4;
  1712. }
  1713. return 0;
  1714. err_unregister:
  1715. for (i--; i >= 0; i--) {
  1716. info = &m->info[i];
  1717. #ifdef CONFIG_ISDN_TTY_FAX
  1718. kfree(info->fax);
  1719. #endif
  1720. kfree(info->port.xmit_buf - 4);
  1721. info->port.xmit_buf = NULL;
  1722. tty_port_destroy(&info->port);
  1723. }
  1724. tty_unregister_driver(m->tty_modem);
  1725. err:
  1726. put_tty_driver(m->tty_modem);
  1727. m->tty_modem = NULL;
  1728. return retval;
  1729. }
  1730. void
  1731. isdn_tty_exit(void)
  1732. {
  1733. modem_info *info;
  1734. int i;
  1735. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  1736. info = &dev->mdm.info[i];
  1737. isdn_tty_cleanup_xmit(info);
  1738. #ifdef CONFIG_ISDN_TTY_FAX
  1739. kfree(info->fax);
  1740. #endif
  1741. kfree(info->port.xmit_buf - 4);
  1742. info->port.xmit_buf = NULL;
  1743. tty_port_destroy(&info->port);
  1744. }
  1745. tty_unregister_driver(dev->mdm.tty_modem);
  1746. put_tty_driver(dev->mdm.tty_modem);
  1747. dev->mdm.tty_modem = NULL;
  1748. }
  1749. /*
  1750. * isdn_tty_match_icall(char *MSN, atemu *tty_emulator, int dev_idx)
  1751. * match the MSN against the MSNs (glob patterns) defined for tty_emulator,
  1752. * and return 0 for match, 1 for no match, 2 if MSN could match if longer.
  1753. */
  1754. static int
  1755. isdn_tty_match_icall(char *cid, atemu *emu, int di)
  1756. {
  1757. #ifdef ISDN_DEBUG_MODEM_ICALL
  1758. printk(KERN_DEBUG "m_fi: msn=%s lmsn=%s mmsn=%s mreg[SI1]=%d mreg[SI2]=%d\n",
  1759. emu->msn, emu->lmsn, isdn_map_eaz2msn(emu->msn, di),
  1760. emu->mdmreg[REG_SI1], emu->mdmreg[REG_SI2]);
  1761. #endif
  1762. if (strlen(emu->lmsn)) {
  1763. char *p = emu->lmsn;
  1764. char *q;
  1765. int tmp;
  1766. int ret = 0;
  1767. while (1) {
  1768. if ((q = strchr(p, ';')))
  1769. *q = '\0';
  1770. if ((tmp = isdn_msncmp(cid, isdn_map_eaz2msn(p, di))) > ret)
  1771. ret = tmp;
  1772. #ifdef ISDN_DEBUG_MODEM_ICALL
  1773. printk(KERN_DEBUG "m_fi: lmsnX=%s mmsn=%s -> tmp=%d\n",
  1774. p, isdn_map_eaz2msn(emu->msn, di), tmp);
  1775. #endif
  1776. if (q) {
  1777. *q = ';';
  1778. p = q;
  1779. p++;
  1780. }
  1781. if (!tmp)
  1782. return 0;
  1783. if (!q)
  1784. break;
  1785. }
  1786. return ret;
  1787. } else {
  1788. int tmp;
  1789. tmp = isdn_msncmp(cid, isdn_map_eaz2msn(emu->msn, di));
  1790. #ifdef ISDN_DEBUG_MODEM_ICALL
  1791. printk(KERN_DEBUG "m_fi: mmsn=%s -> tmp=%d\n",
  1792. isdn_map_eaz2msn(emu->msn, di), tmp);
  1793. #endif
  1794. return tmp;
  1795. }
  1796. }
  1797. /*
  1798. * An incoming call-request has arrived.
  1799. * Search the tty-devices for an appropriate device and bind
  1800. * it to the ISDN-Channel.
  1801. * Return:
  1802. *
  1803. * 0 = No matching device found.
  1804. * 1 = A matching device found.
  1805. * 3 = No match found, but eventually would match, if
  1806. * CID is longer.
  1807. */
  1808. int
  1809. isdn_tty_find_icall(int di, int ch, setup_parm *setup)
  1810. {
  1811. char *eaz;
  1812. int i;
  1813. int wret;
  1814. int idx;
  1815. int si1;
  1816. int si2;
  1817. char *nr;
  1818. ulong flags;
  1819. if (!setup->phone[0]) {
  1820. nr = "0";
  1821. printk(KERN_INFO "isdn_tty: Incoming call without OAD, assuming '0'\n");
  1822. } else
  1823. nr = setup->phone;
  1824. si1 = (int) setup->si1;
  1825. si2 = (int) setup->si2;
  1826. if (!setup->eazmsn[0]) {
  1827. printk(KERN_WARNING "isdn_tty: Incoming call without CPN, assuming '0'\n");
  1828. eaz = "0";
  1829. } else
  1830. eaz = setup->eazmsn;
  1831. #ifdef ISDN_DEBUG_MODEM_ICALL
  1832. printk(KERN_DEBUG "m_fi: eaz=%s si1=%d si2=%d\n", eaz, si1, si2);
  1833. #endif
  1834. wret = 0;
  1835. spin_lock_irqsave(&dev->lock, flags);
  1836. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  1837. modem_info *info = &dev->mdm.info[i];
  1838. if (info->port.count == 0)
  1839. continue;
  1840. if ((info->emu.mdmreg[REG_SI1] & si2bit[si1]) && /* SI1 is matching */
  1841. (info->emu.mdmreg[REG_SI2] == si2)) { /* SI2 is matching */
  1842. idx = isdn_dc2minor(di, ch);
  1843. #ifdef ISDN_DEBUG_MODEM_ICALL
  1844. printk(KERN_DEBUG "m_fi: match1 wret=%d\n", wret);
  1845. printk(KERN_DEBUG "m_fi: idx=%d flags=%08lx drv=%d ch=%d usg=%d\n", idx,
  1846. info->port.flags, info->isdn_driver,
  1847. info->isdn_channel, dev->usage[idx]);
  1848. #endif
  1849. if (
  1850. #ifndef FIX_FILE_TRANSFER
  1851. (info->port.flags & ASYNC_NORMAL_ACTIVE) &&
  1852. #endif
  1853. (info->isdn_driver == -1) &&
  1854. (info->isdn_channel == -1) &&
  1855. (USG_NONE(dev->usage[idx]))) {
  1856. int matchret;
  1857. if ((matchret = isdn_tty_match_icall(eaz, &info->emu, di)) > wret)
  1858. wret = matchret;
  1859. if (!matchret) { /* EAZ is matching */
  1860. info->isdn_driver = di;
  1861. info->isdn_channel = ch;
  1862. info->drv_index = idx;
  1863. dev->m_idx[idx] = info->line;
  1864. dev->usage[idx] &= ISDN_USAGE_EXCLUSIVE;
  1865. dev->usage[idx] |= isdn_calc_usage(si1, info->emu.mdmreg[REG_L2PROT]);
  1866. strcpy(dev->num[idx], nr);
  1867. strcpy(info->emu.cpn, eaz);
  1868. info->emu.mdmreg[REG_SI1I] = si2bit[si1];
  1869. info->emu.mdmreg[REG_PLAN] = setup->plan;
  1870. info->emu.mdmreg[REG_SCREEN] = setup->screen;
  1871. isdn_info_update();
  1872. spin_unlock_irqrestore(&dev->lock, flags);
  1873. printk(KERN_INFO "isdn_tty: call from %s, -> RING on ttyI%d\n", nr,
  1874. info->line);
  1875. info->msr |= UART_MSR_RI;
  1876. isdn_tty_modem_result(RESULT_RING, info);
  1877. isdn_timer_ctrl(ISDN_TIMER_MODEMRING, 1);
  1878. return 1;
  1879. }
  1880. }
  1881. }
  1882. }
  1883. spin_unlock_irqrestore(&dev->lock, flags);
  1884. printk(KERN_INFO "isdn_tty: call from %s -> %s %s\n", nr, eaz,
  1885. ((dev->drv[di]->flags & DRV_FLAG_REJBUS) && (wret != 2)) ? "rejected" : "ignored");
  1886. return (wret == 2) ? 3 : 0;
  1887. }
  1888. #define TTY_IS_ACTIVE(info) (info->port.flags & ASYNC_NORMAL_ACTIVE)
  1889. int
  1890. isdn_tty_stat_callback(int i, isdn_ctrl *c)
  1891. {
  1892. int mi;
  1893. modem_info *info;
  1894. char *e;
  1895. if (i < 0)
  1896. return 0;
  1897. if ((mi = dev->m_idx[i]) >= 0) {
  1898. info = &dev->mdm.info[mi];
  1899. switch (c->command) {
  1900. case ISDN_STAT_CINF:
  1901. printk(KERN_DEBUG "CHARGEINFO on ttyI%d: %ld %s\n", info->line, c->arg, c->parm.num);
  1902. info->emu.charge = (unsigned) simple_strtoul(c->parm.num, &e, 10);
  1903. if (e == (char *)c->parm.num)
  1904. info->emu.charge = 0;
  1905. break;
  1906. case ISDN_STAT_BSENT:
  1907. #ifdef ISDN_TTY_STAT_DEBUG
  1908. printk(KERN_DEBUG "tty_STAT_BSENT ttyI%d\n", info->line);
  1909. #endif
  1910. if ((info->isdn_driver == c->driver) &&
  1911. (info->isdn_channel == c->arg)) {
  1912. info->msr |= UART_MSR_CTS;
  1913. if (info->send_outstanding)
  1914. if (!(--info->send_outstanding))
  1915. info->lsr |= UART_LSR_TEMT;
  1916. isdn_tty_tint(info);
  1917. return 1;
  1918. }
  1919. break;
  1920. case ISDN_STAT_CAUSE:
  1921. #ifdef ISDN_TTY_STAT_DEBUG
  1922. printk(KERN_DEBUG "tty_STAT_CAUSE ttyI%d\n", info->line);
  1923. #endif
  1924. /* Signal cause to tty-device */
  1925. strncpy(info->last_cause, c->parm.num, 5);
  1926. return 1;
  1927. case ISDN_STAT_DISPLAY:
  1928. #ifdef ISDN_TTY_STAT_DEBUG
  1929. printk(KERN_DEBUG "tty_STAT_DISPLAY ttyI%d\n", info->line);
  1930. #endif
  1931. /* Signal display to tty-device */
  1932. if ((info->emu.mdmreg[REG_DISPLAY] & BIT_DISPLAY) &&
  1933. !(info->emu.mdmreg[REG_RESPNUM] & BIT_RESPNUM)) {
  1934. isdn_tty_at_cout("\r\n", info);
  1935. isdn_tty_at_cout("DISPLAY: ", info);
  1936. isdn_tty_at_cout(c->parm.display, info);
  1937. isdn_tty_at_cout("\r\n", info);
  1938. }
  1939. return 1;
  1940. case ISDN_STAT_DCONN:
  1941. #ifdef ISDN_TTY_STAT_DEBUG
  1942. printk(KERN_DEBUG "tty_STAT_DCONN ttyI%d\n", info->line);
  1943. #endif
  1944. if (TTY_IS_ACTIVE(info)) {
  1945. if (info->dialing == 1) {
  1946. info->dialing = 2;
  1947. return 1;
  1948. }
  1949. }
  1950. break;
  1951. case ISDN_STAT_DHUP:
  1952. #ifdef ISDN_TTY_STAT_DEBUG
  1953. printk(KERN_DEBUG "tty_STAT_DHUP ttyI%d\n", info->line);
  1954. #endif
  1955. if (TTY_IS_ACTIVE(info)) {
  1956. if (info->dialing == 1)
  1957. isdn_tty_modem_result(RESULT_BUSY, info);
  1958. if (info->dialing > 1)
  1959. isdn_tty_modem_result(RESULT_NO_CARRIER, info);
  1960. info->dialing = 0;
  1961. #ifdef ISDN_DEBUG_MODEM_HUP
  1962. printk(KERN_DEBUG "Mhup in ISDN_STAT_DHUP\n");
  1963. #endif
  1964. isdn_tty_modem_hup(info, 0);
  1965. return 1;
  1966. }
  1967. break;
  1968. case ISDN_STAT_BCONN:
  1969. #ifdef ISDN_TTY_STAT_DEBUG
  1970. printk(KERN_DEBUG "tty_STAT_BCONN ttyI%d\n", info->line);
  1971. #endif
  1972. /* Wake up any processes waiting
  1973. * for incoming call of this device when
  1974. * DCD follow the state of incoming carrier
  1975. */
  1976. if (info->port.blocked_open &&
  1977. (info->emu.mdmreg[REG_DCD] & BIT_DCD)) {
  1978. wake_up_interruptible(&info->port.open_wait);
  1979. }
  1980. /* Schedule CONNECT-Message to any tty
  1981. * waiting for it and
  1982. * set DCD-bit of its modem-status.
  1983. */
  1984. if (TTY_IS_ACTIVE(info) ||
  1985. (info->port.blocked_open &&
  1986. (info->emu.mdmreg[REG_DCD] & BIT_DCD))) {
  1987. info->msr |= UART_MSR_DCD;
  1988. info->emu.charge = 0;
  1989. if (info->dialing & 0xf)
  1990. info->last_dir = 1;
  1991. else
  1992. info->last_dir = 0;
  1993. info->dialing = 0;
  1994. info->rcvsched = 1;
  1995. if (USG_MODEM(dev->usage[i])) {
  1996. if (info->emu.mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) {
  1997. strcpy(info->emu.connmsg, c->parm.num);
  1998. isdn_tty_modem_result(RESULT_CONNECT, info);
  1999. } else
  2000. isdn_tty_modem_result(RESULT_CONNECT64000, info);
  2001. }
  2002. if (USG_VOICE(dev->usage[i]))
  2003. isdn_tty_modem_result(RESULT_VCON, info);
  2004. return 1;
  2005. }
  2006. break;
  2007. case ISDN_STAT_BHUP:
  2008. #ifdef ISDN_TTY_STAT_DEBUG
  2009. printk(KERN_DEBUG "tty_STAT_BHUP ttyI%d\n", info->line);
  2010. #endif
  2011. if (TTY_IS_ACTIVE(info)) {
  2012. #ifdef ISDN_DEBUG_MODEM_HUP
  2013. printk(KERN_DEBUG "Mhup in ISDN_STAT_BHUP\n");
  2014. #endif
  2015. isdn_tty_modem_hup(info, 0);
  2016. return 1;
  2017. }
  2018. break;
  2019. case ISDN_STAT_NODCH:
  2020. #ifdef ISDN_TTY_STAT_DEBUG
  2021. printk(KERN_DEBUG "tty_STAT_NODCH ttyI%d\n", info->line);
  2022. #endif
  2023. if (TTY_IS_ACTIVE(info)) {
  2024. if (info->dialing) {
  2025. info->dialing = 0;
  2026. info->last_l2 = -1;
  2027. info->last_si = 0;
  2028. sprintf(info->last_cause, "0000");
  2029. isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
  2030. }
  2031. isdn_tty_modem_hup(info, 0);
  2032. return 1;
  2033. }
  2034. break;
  2035. case ISDN_STAT_UNLOAD:
  2036. #ifdef ISDN_TTY_STAT_DEBUG
  2037. printk(KERN_DEBUG "tty_STAT_UNLOAD ttyI%d\n", info->line);
  2038. #endif
  2039. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  2040. info = &dev->mdm.info[i];
  2041. if (info->isdn_driver == c->driver) {
  2042. if (info->online)
  2043. isdn_tty_modem_hup(info, 1);
  2044. }
  2045. }
  2046. return 1;
  2047. #ifdef CONFIG_ISDN_TTY_FAX
  2048. case ISDN_STAT_FAXIND:
  2049. if (TTY_IS_ACTIVE(info)) {
  2050. isdn_tty_fax_command(info, c);
  2051. }
  2052. break;
  2053. #endif
  2054. #ifdef CONFIG_ISDN_AUDIO
  2055. case ISDN_STAT_AUDIO:
  2056. if (TTY_IS_ACTIVE(info)) {
  2057. switch (c->parm.num[0]) {
  2058. case ISDN_AUDIO_DTMF:
  2059. if (info->vonline) {
  2060. isdn_audio_put_dle_code(info,
  2061. c->parm.num[1]);
  2062. }
  2063. break;
  2064. }
  2065. }
  2066. break;
  2067. #endif
  2068. }
  2069. }
  2070. return 0;
  2071. }
  2072. /*********************************************************************
  2073. Modem-Emulator-Routines
  2074. *********************************************************************/
  2075. #define cmdchar(c) ((c >= ' ') && (c <= 0x7f))
  2076. /*
  2077. * Put a message from the AT-emulator into receive-buffer of tty,
  2078. * convert CR, LF, and BS to values in modem-registers 3, 4 and 5.
  2079. */
  2080. void
  2081. isdn_tty_at_cout(char *msg, modem_info *info)
  2082. {
  2083. struct tty_port *port = &info->port;
  2084. atemu *m = &info->emu;
  2085. char *p;
  2086. char c;
  2087. u_long flags;
  2088. struct sk_buff *skb = NULL;
  2089. char *sp = NULL;
  2090. int l;
  2091. if (!msg) {
  2092. printk(KERN_WARNING "isdn_tty: Null-Message in isdn_tty_at_cout\n");
  2093. return;
  2094. }
  2095. l = strlen(msg);
  2096. spin_lock_irqsave(&info->readlock, flags);
  2097. if (port->flags & ASYNC_CLOSING) {
  2098. spin_unlock_irqrestore(&info->readlock, flags);
  2099. return;
  2100. }
  2101. /* use queue instead of direct, if online and */
  2102. /* data is in queue or buffer is full */
  2103. if (info->online && ((tty_buffer_request_room(port, l) < l) ||
  2104. !skb_queue_empty(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel]))) {
  2105. skb = alloc_skb(l, GFP_ATOMIC);
  2106. if (!skb) {
  2107. spin_unlock_irqrestore(&info->readlock, flags);
  2108. return;
  2109. }
  2110. sp = skb_put(skb, l);
  2111. #ifdef CONFIG_ISDN_AUDIO
  2112. ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
  2113. ISDN_AUDIO_SKB_LOCK(skb) = 0;
  2114. #endif
  2115. }
  2116. for (p = msg; *p; p++) {
  2117. switch (*p) {
  2118. case '\r':
  2119. c = m->mdmreg[REG_CR];
  2120. break;
  2121. case '\n':
  2122. c = m->mdmreg[REG_LF];
  2123. break;
  2124. case '\b':
  2125. c = m->mdmreg[REG_BS];
  2126. break;
  2127. default:
  2128. c = *p;
  2129. }
  2130. if (skb) {
  2131. *sp++ = c;
  2132. } else {
  2133. if (tty_insert_flip_char(port, c, TTY_NORMAL) == 0)
  2134. break;
  2135. }
  2136. }
  2137. if (skb) {
  2138. __skb_queue_tail(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel], skb);
  2139. dev->drv[info->isdn_driver]->rcvcount[info->isdn_channel] += skb->len;
  2140. spin_unlock_irqrestore(&info->readlock, flags);
  2141. /* Schedule dequeuing */
  2142. if (dev->modempoll && info->rcvsched)
  2143. isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
  2144. } else {
  2145. spin_unlock_irqrestore(&info->readlock, flags);
  2146. tty_flip_buffer_push(port);
  2147. }
  2148. }
  2149. /*
  2150. * Perform ATH Hangup
  2151. */
  2152. static void
  2153. isdn_tty_on_hook(modem_info *info)
  2154. {
  2155. if (info->isdn_channel >= 0) {
  2156. #ifdef ISDN_DEBUG_MODEM_HUP
  2157. printk(KERN_DEBUG "Mhup in isdn_tty_on_hook\n");
  2158. #endif
  2159. isdn_tty_modem_hup(info, 1);
  2160. }
  2161. }
  2162. static void
  2163. isdn_tty_off_hook(void)
  2164. {
  2165. printk(KERN_DEBUG "isdn_tty_off_hook\n");
  2166. }
  2167. #define PLUSWAIT1 (HZ / 2) /* 0.5 sec. */
  2168. #define PLUSWAIT2 (HZ * 3 / 2) /* 1.5 sec */
  2169. /*
  2170. * Check Buffer for Modem-escape-sequence, activate timer-callback to
  2171. * isdn_tty_modem_escape() if sequence found.
  2172. *
  2173. * Parameters:
  2174. * p pointer to databuffer
  2175. * plus escape-character
  2176. * count length of buffer
  2177. * pluscount count of valid escape-characters so far
  2178. * lastplus timestamp of last character
  2179. */
  2180. static void
  2181. isdn_tty_check_esc(const u_char *p, u_char plus, int count, int *pluscount,
  2182. u_long *lastplus)
  2183. {
  2184. if (plus > 127)
  2185. return;
  2186. if (count > 3) {
  2187. p += count - 3;
  2188. count = 3;
  2189. *pluscount = 0;
  2190. }
  2191. while (count > 0) {
  2192. if (*(p++) == plus) {
  2193. if ((*pluscount)++) {
  2194. /* Time since last '+' > 0.5 sec. ? */
  2195. if (time_after(jiffies, *lastplus + PLUSWAIT1))
  2196. *pluscount = 1;
  2197. } else {
  2198. /* Time since last non-'+' < 1.5 sec. ? */
  2199. if (time_before(jiffies, *lastplus + PLUSWAIT2))
  2200. *pluscount = 0;
  2201. }
  2202. if ((*pluscount == 3) && (count == 1))
  2203. isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, 1);
  2204. if (*pluscount > 3)
  2205. *pluscount = 1;
  2206. } else
  2207. *pluscount = 0;
  2208. *lastplus = jiffies;
  2209. count--;
  2210. }
  2211. }
  2212. /*
  2213. * Return result of AT-emulator to tty-receive-buffer, depending on
  2214. * modem-register 12, bit 0 and 1.
  2215. * For CONNECT-messages also switch to online-mode.
  2216. * For RING-message handle auto-ATA if register 0 != 0
  2217. */
  2218. static void
  2219. isdn_tty_modem_result(int code, modem_info *info)
  2220. {
  2221. atemu *m = &info->emu;
  2222. static char *msg[] =
  2223. {"OK", "CONNECT", "RING", "NO CARRIER", "ERROR",
  2224. "CONNECT 64000", "NO DIALTONE", "BUSY", "NO ANSWER",
  2225. "RINGING", "NO MSN/EAZ", "VCON", "RUNG"};
  2226. char s[ISDN_MSNLEN + 10];
  2227. switch (code) {
  2228. case RESULT_RING:
  2229. m->mdmreg[REG_RINGCNT]++;
  2230. if (m->mdmreg[REG_RINGCNT] == m->mdmreg[REG_RINGATA])
  2231. /* Automatically accept incoming call */
  2232. isdn_tty_cmd_ATA(info);
  2233. break;
  2234. case RESULT_NO_CARRIER:
  2235. #ifdef ISDN_DEBUG_MODEM_HUP
  2236. printk(KERN_DEBUG "modem_result: NO CARRIER %d %d\n",
  2237. (info->port.flags & ASYNC_CLOSING),
  2238. (!info->port.tty));
  2239. #endif
  2240. m->mdmreg[REG_RINGCNT] = 0;
  2241. del_timer(&info->nc_timer);
  2242. info->ncarrier = 0;
  2243. if ((info->port.flags & ASYNC_CLOSING) || (!info->port.tty))
  2244. return;
  2245. #ifdef CONFIG_ISDN_AUDIO
  2246. if (info->vonline & 1) {
  2247. #ifdef ISDN_DEBUG_MODEM_VOICE
  2248. printk(KERN_DEBUG "res3: send DLE-ETX on ttyI%d\n",
  2249. info->line);
  2250. #endif
  2251. /* voice-recording, add DLE-ETX */
  2252. isdn_tty_at_cout("\020\003", info);
  2253. }
  2254. if (info->vonline & 2) {
  2255. #ifdef ISDN_DEBUG_MODEM_VOICE
  2256. printk(KERN_DEBUG "res3: send DLE-DC4 on ttyI%d\n",
  2257. info->line);
  2258. #endif
  2259. /* voice-playing, add DLE-DC4 */
  2260. isdn_tty_at_cout("\020\024", info);
  2261. }
  2262. #endif
  2263. break;
  2264. case RESULT_CONNECT:
  2265. case RESULT_CONNECT64000:
  2266. sprintf(info->last_cause, "0000");
  2267. if (!info->online)
  2268. info->online = 2;
  2269. break;
  2270. case RESULT_VCON:
  2271. #ifdef ISDN_DEBUG_MODEM_VOICE
  2272. printk(KERN_DEBUG "res3: send VCON on ttyI%d\n",
  2273. info->line);
  2274. #endif
  2275. sprintf(info->last_cause, "0000");
  2276. if (!info->online)
  2277. info->online = 1;
  2278. break;
  2279. } /* switch (code) */
  2280. if (m->mdmreg[REG_RESP] & BIT_RESP) {
  2281. /* Show results */
  2282. if (m->mdmreg[REG_RESPNUM] & BIT_RESPNUM) {
  2283. /* Show numeric results only */
  2284. sprintf(s, "\r\n%d\r\n", code);
  2285. isdn_tty_at_cout(s, info);
  2286. } else {
  2287. if (code == RESULT_RING) {
  2288. /* return if "show RUNG" and ringcounter>1 */
  2289. if ((m->mdmreg[REG_RUNG] & BIT_RUNG) &&
  2290. (m->mdmreg[REG_RINGCNT] > 1))
  2291. return;
  2292. /* print CID, _before_ _every_ ring */
  2293. if (!(m->mdmreg[REG_CIDONCE] & BIT_CIDONCE)) {
  2294. isdn_tty_at_cout("\r\nCALLER NUMBER: ", info);
  2295. isdn_tty_at_cout(dev->num[info->drv_index], info);
  2296. if (m->mdmreg[REG_CDN] & BIT_CDN) {
  2297. isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
  2298. isdn_tty_at_cout(info->emu.cpn, info);
  2299. }
  2300. }
  2301. }
  2302. isdn_tty_at_cout("\r\n", info);
  2303. isdn_tty_at_cout(msg[code], info);
  2304. switch (code) {
  2305. case RESULT_CONNECT:
  2306. switch (m->mdmreg[REG_L2PROT]) {
  2307. case ISDN_PROTO_L2_MODEM:
  2308. isdn_tty_at_cout(" ", info);
  2309. isdn_tty_at_cout(m->connmsg, info);
  2310. break;
  2311. }
  2312. break;
  2313. case RESULT_RING:
  2314. /* Append CPN, if enabled */
  2315. if ((m->mdmreg[REG_CPN] & BIT_CPN)) {
  2316. sprintf(s, "/%s", m->cpn);
  2317. isdn_tty_at_cout(s, info);
  2318. }
  2319. /* Print CID only once, _after_ 1st RING */
  2320. if ((m->mdmreg[REG_CIDONCE] & BIT_CIDONCE) &&
  2321. (m->mdmreg[REG_RINGCNT] == 1)) {
  2322. isdn_tty_at_cout("\r\n", info);
  2323. isdn_tty_at_cout("CALLER NUMBER: ", info);
  2324. isdn_tty_at_cout(dev->num[info->drv_index], info);
  2325. if (m->mdmreg[REG_CDN] & BIT_CDN) {
  2326. isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
  2327. isdn_tty_at_cout(info->emu.cpn, info);
  2328. }
  2329. }
  2330. break;
  2331. case RESULT_NO_CARRIER:
  2332. case RESULT_NO_DIALTONE:
  2333. case RESULT_BUSY:
  2334. case RESULT_NO_ANSWER:
  2335. m->mdmreg[REG_RINGCNT] = 0;
  2336. /* Append Cause-Message if enabled */
  2337. if (m->mdmreg[REG_RESPXT] & BIT_RESPXT) {
  2338. sprintf(s, "/%s", info->last_cause);
  2339. isdn_tty_at_cout(s, info);
  2340. }
  2341. break;
  2342. case RESULT_CONNECT64000:
  2343. /* Append Protocol to CONNECT message */
  2344. switch (m->mdmreg[REG_L2PROT]) {
  2345. case ISDN_PROTO_L2_X75I:
  2346. case ISDN_PROTO_L2_X75UI:
  2347. case ISDN_PROTO_L2_X75BUI:
  2348. isdn_tty_at_cout("/X.75", info);
  2349. break;
  2350. case ISDN_PROTO_L2_HDLC:
  2351. isdn_tty_at_cout("/HDLC", info);
  2352. break;
  2353. case ISDN_PROTO_L2_V11096:
  2354. isdn_tty_at_cout("/V110/9600", info);
  2355. break;
  2356. case ISDN_PROTO_L2_V11019:
  2357. isdn_tty_at_cout("/V110/19200", info);
  2358. break;
  2359. case ISDN_PROTO_L2_V11038:
  2360. isdn_tty_at_cout("/V110/38400", info);
  2361. break;
  2362. }
  2363. if (m->mdmreg[REG_T70] & BIT_T70) {
  2364. isdn_tty_at_cout("/T.70", info);
  2365. if (m->mdmreg[REG_T70] & BIT_T70_EXT)
  2366. isdn_tty_at_cout("+", info);
  2367. }
  2368. break;
  2369. }
  2370. isdn_tty_at_cout("\r\n", info);
  2371. }
  2372. }
  2373. if (code == RESULT_NO_CARRIER) {
  2374. if ((info->port.flags & ASYNC_CLOSING) || (!info->port.tty))
  2375. return;
  2376. if (info->port.flags & ASYNC_CHECK_CD)
  2377. tty_hangup(info->port.tty);
  2378. }
  2379. }
  2380. /*
  2381. * Display a modem-register-value.
  2382. */
  2383. static void
  2384. isdn_tty_show_profile(int ridx, modem_info *info)
  2385. {
  2386. char v[6];
  2387. sprintf(v, "\r\n%d", info->emu.mdmreg[ridx]);
  2388. isdn_tty_at_cout(v, info);
  2389. }
  2390. /*
  2391. * Get MSN-string from char-pointer, set pointer to end of number
  2392. */
  2393. static void
  2394. isdn_tty_get_msnstr(char *n, char **p)
  2395. {
  2396. int limit = ISDN_MSNLEN - 1;
  2397. while (((*p[0] >= '0' && *p[0] <= '9') ||
  2398. /* Why a comma ??? */
  2399. (*p[0] == ',') || (*p[0] == ':')) &&
  2400. (limit--))
  2401. *n++ = *p[0]++;
  2402. *n = '\0';
  2403. }
  2404. /*
  2405. * Get phone-number from modem-commandbuffer
  2406. */
  2407. static void
  2408. isdn_tty_getdial(char *p, char *q, int cnt)
  2409. {
  2410. int first = 1;
  2411. int limit = ISDN_MSNLEN - 1; /* MUST match the size of interface var to avoid
  2412. buffer overflow */
  2413. while (strchr(" 0123456789,#.*WPTSR-", *p) && *p && --cnt > 0) {
  2414. if ((*p >= '0' && *p <= '9') || ((*p == 'S') && first) ||
  2415. ((*p == 'R') && first) ||
  2416. (*p == '*') || (*p == '#')) {
  2417. *q++ = *p;
  2418. limit--;
  2419. }
  2420. if (!limit)
  2421. break;
  2422. p++;
  2423. first = 0;
  2424. }
  2425. *q = 0;
  2426. }
  2427. #define PARSE_ERROR { isdn_tty_modem_result(RESULT_ERROR, info); return; }
  2428. #define PARSE_ERROR1 { isdn_tty_modem_result(RESULT_ERROR, info); return 1; }
  2429. static void
  2430. isdn_tty_report(modem_info *info)
  2431. {
  2432. atemu *m = &info->emu;
  2433. char s[80];
  2434. isdn_tty_at_cout("\r\nStatistics of last connection:\r\n\r\n", info);
  2435. sprintf(s, " Remote Number: %s\r\n", info->last_num);
  2436. isdn_tty_at_cout(s, info);
  2437. sprintf(s, " Direction: %s\r\n", info->last_dir ? "outgoing" : "incoming");
  2438. isdn_tty_at_cout(s, info);
  2439. isdn_tty_at_cout(" Layer-2 Protocol: ", info);
  2440. switch (info->last_l2) {
  2441. case ISDN_PROTO_L2_X75I:
  2442. isdn_tty_at_cout("X.75i", info);
  2443. break;
  2444. case ISDN_PROTO_L2_X75UI:
  2445. isdn_tty_at_cout("X.75ui", info);
  2446. break;
  2447. case ISDN_PROTO_L2_X75BUI:
  2448. isdn_tty_at_cout("X.75bui", info);
  2449. break;
  2450. case ISDN_PROTO_L2_HDLC:
  2451. isdn_tty_at_cout("HDLC", info);
  2452. break;
  2453. case ISDN_PROTO_L2_V11096:
  2454. isdn_tty_at_cout("V.110 9600 Baud", info);
  2455. break;
  2456. case ISDN_PROTO_L2_V11019:
  2457. isdn_tty_at_cout("V.110 19200 Baud", info);
  2458. break;
  2459. case ISDN_PROTO_L2_V11038:
  2460. isdn_tty_at_cout("V.110 38400 Baud", info);
  2461. break;
  2462. case ISDN_PROTO_L2_TRANS:
  2463. isdn_tty_at_cout("transparent", info);
  2464. break;
  2465. case ISDN_PROTO_L2_MODEM:
  2466. isdn_tty_at_cout("modem", info);
  2467. break;
  2468. case ISDN_PROTO_L2_FAX:
  2469. isdn_tty_at_cout("fax", info);
  2470. break;
  2471. default:
  2472. isdn_tty_at_cout("unknown", info);
  2473. break;
  2474. }
  2475. if (m->mdmreg[REG_T70] & BIT_T70) {
  2476. isdn_tty_at_cout("/T.70", info);
  2477. if (m->mdmreg[REG_T70] & BIT_T70_EXT)
  2478. isdn_tty_at_cout("+", info);
  2479. }
  2480. isdn_tty_at_cout("\r\n", info);
  2481. isdn_tty_at_cout(" Service: ", info);
  2482. switch (info->last_si) {
  2483. case 1:
  2484. isdn_tty_at_cout("audio\r\n", info);
  2485. break;
  2486. case 5:
  2487. isdn_tty_at_cout("btx\r\n", info);
  2488. break;
  2489. case 7:
  2490. isdn_tty_at_cout("data\r\n", info);
  2491. break;
  2492. default:
  2493. sprintf(s, "%d\r\n", info->last_si);
  2494. isdn_tty_at_cout(s, info);
  2495. break;
  2496. }
  2497. sprintf(s, " Hangup location: %s\r\n", info->last_lhup ? "local" : "remote");
  2498. isdn_tty_at_cout(s, info);
  2499. sprintf(s, " Last cause: %s\r\n", info->last_cause);
  2500. isdn_tty_at_cout(s, info);
  2501. }
  2502. /*
  2503. * Parse AT&.. commands.
  2504. */
  2505. static int
  2506. isdn_tty_cmd_ATand(char **p, modem_info *info)
  2507. {
  2508. atemu *m = &info->emu;
  2509. int i;
  2510. char rb[100];
  2511. #define MAXRB (sizeof(rb) - 1)
  2512. switch (*p[0]) {
  2513. case 'B':
  2514. /* &B - Set Buffersize */
  2515. p[0]++;
  2516. i = isdn_getnum(p);
  2517. if ((i < 0) || (i > ISDN_SERIAL_XMIT_MAX))
  2518. PARSE_ERROR1;
  2519. #ifdef CONFIG_ISDN_AUDIO
  2520. if ((m->mdmreg[REG_SI1] & 1) && (i > VBUF))
  2521. PARSE_ERROR1;
  2522. #endif
  2523. m->mdmreg[REG_PSIZE] = i / 16;
  2524. info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
  2525. switch (m->mdmreg[REG_L2PROT]) {
  2526. case ISDN_PROTO_L2_V11096:
  2527. case ISDN_PROTO_L2_V11019:
  2528. case ISDN_PROTO_L2_V11038:
  2529. info->xmit_size /= 10;
  2530. }
  2531. break;
  2532. case 'C':
  2533. /* &C - DCD Status */
  2534. p[0]++;
  2535. switch (isdn_getnum(p)) {
  2536. case 0:
  2537. m->mdmreg[REG_DCD] &= ~BIT_DCD;
  2538. break;
  2539. case 1:
  2540. m->mdmreg[REG_DCD] |= BIT_DCD;
  2541. break;
  2542. default:
  2543. PARSE_ERROR1
  2544. }
  2545. break;
  2546. case 'D':
  2547. /* &D - Set DTR-Low-behavior */
  2548. p[0]++;
  2549. switch (isdn_getnum(p)) {
  2550. case 0:
  2551. m->mdmreg[REG_DTRHUP] &= ~BIT_DTRHUP;
  2552. m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
  2553. break;
  2554. case 2:
  2555. m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
  2556. m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
  2557. break;
  2558. case 3:
  2559. m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
  2560. m->mdmreg[REG_DTRR] |= BIT_DTRR;
  2561. break;
  2562. default:
  2563. PARSE_ERROR1
  2564. }
  2565. break;
  2566. case 'E':
  2567. /* &E -Set EAZ/MSN */
  2568. p[0]++;
  2569. isdn_tty_get_msnstr(m->msn, p);
  2570. break;
  2571. case 'F':
  2572. /* &F -Set Factory-Defaults */
  2573. p[0]++;
  2574. if (info->msr & UART_MSR_DCD)
  2575. PARSE_ERROR1;
  2576. isdn_tty_reset_profile(m);
  2577. isdn_tty_modem_reset_regs(info, 1);
  2578. break;
  2579. #ifdef DUMMY_HAYES_AT
  2580. case 'K':
  2581. /* only for be compilant with common scripts */
  2582. /* &K Flowcontrol - no function */
  2583. p[0]++;
  2584. isdn_getnum(p);
  2585. break;
  2586. #endif
  2587. case 'L':
  2588. /* &L -Set Numbers to listen on */
  2589. p[0]++;
  2590. i = 0;
  2591. while (*p[0] && (strchr("0123456789,-*[]?;", *p[0])) &&
  2592. (i < ISDN_LMSNLEN - 1))
  2593. m->lmsn[i++] = *p[0]++;
  2594. m->lmsn[i] = '\0';
  2595. break;
  2596. case 'R':
  2597. /* &R - Set V.110 bitrate adaption */
  2598. p[0]++;
  2599. i = isdn_getnum(p);
  2600. switch (i) {
  2601. case 0:
  2602. /* Switch off V.110, back to X.75 */
  2603. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
  2604. m->mdmreg[REG_SI2] = 0;
  2605. info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
  2606. break;
  2607. case 9600:
  2608. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11096;
  2609. m->mdmreg[REG_SI2] = 197;
  2610. info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
  2611. break;
  2612. case 19200:
  2613. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11019;
  2614. m->mdmreg[REG_SI2] = 199;
  2615. info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
  2616. break;
  2617. case 38400:
  2618. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11038;
  2619. m->mdmreg[REG_SI2] = 198; /* no existing standard for this */
  2620. info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
  2621. break;
  2622. default:
  2623. PARSE_ERROR1;
  2624. }
  2625. /* Switch off T.70 */
  2626. m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
  2627. /* Set Service 7 */
  2628. m->mdmreg[REG_SI1] |= 4;
  2629. break;
  2630. case 'S':
  2631. /* &S - Set Windowsize */
  2632. p[0]++;
  2633. i = isdn_getnum(p);
  2634. if ((i > 0) && (i < 9))
  2635. m->mdmreg[REG_WSIZE] = i;
  2636. else
  2637. PARSE_ERROR1;
  2638. break;
  2639. case 'V':
  2640. /* &V - Show registers */
  2641. p[0]++;
  2642. isdn_tty_at_cout("\r\n", info);
  2643. for (i = 0; i < ISDN_MODEM_NUMREG; i++) {
  2644. sprintf(rb, "S%02d=%03d%s", i,
  2645. m->mdmreg[i], ((i + 1) % 10) ? " " : "\r\n");
  2646. isdn_tty_at_cout(rb, info);
  2647. }
  2648. sprintf(rb, "\r\nEAZ/MSN: %.50s\r\n",
  2649. strlen(m->msn) ? m->msn : "None");
  2650. isdn_tty_at_cout(rb, info);
  2651. if (strlen(m->lmsn)) {
  2652. isdn_tty_at_cout("\r\nListen: ", info);
  2653. isdn_tty_at_cout(m->lmsn, info);
  2654. isdn_tty_at_cout("\r\n", info);
  2655. }
  2656. break;
  2657. case 'W':
  2658. /* &W - Write Profile */
  2659. p[0]++;
  2660. switch (*p[0]) {
  2661. case '0':
  2662. p[0]++;
  2663. modem_write_profile(m);
  2664. break;
  2665. default:
  2666. PARSE_ERROR1;
  2667. }
  2668. break;
  2669. case 'X':
  2670. /* &X - Switch to BTX-Mode and T.70 */
  2671. p[0]++;
  2672. switch (isdn_getnum(p)) {
  2673. case 0:
  2674. m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
  2675. info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
  2676. break;
  2677. case 1:
  2678. m->mdmreg[REG_T70] |= BIT_T70;
  2679. m->mdmreg[REG_T70] &= ~BIT_T70_EXT;
  2680. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
  2681. info->xmit_size = 112;
  2682. m->mdmreg[REG_SI1] = 4;
  2683. m->mdmreg[REG_SI2] = 0;
  2684. break;
  2685. case 2:
  2686. m->mdmreg[REG_T70] |= (BIT_T70 | BIT_T70_EXT);
  2687. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
  2688. info->xmit_size = 112;
  2689. m->mdmreg[REG_SI1] = 4;
  2690. m->mdmreg[REG_SI2] = 0;
  2691. break;
  2692. default:
  2693. PARSE_ERROR1;
  2694. }
  2695. break;
  2696. default:
  2697. PARSE_ERROR1;
  2698. }
  2699. return 0;
  2700. }
  2701. static int
  2702. isdn_tty_check_ats(int mreg, int mval, modem_info *info, atemu *m)
  2703. {
  2704. /* Some plausibility checks */
  2705. switch (mreg) {
  2706. case REG_L2PROT:
  2707. if (mval > ISDN_PROTO_L2_MAX)
  2708. return 1;
  2709. break;
  2710. case REG_PSIZE:
  2711. if ((mval * 16) > ISDN_SERIAL_XMIT_MAX)
  2712. return 1;
  2713. #ifdef CONFIG_ISDN_AUDIO
  2714. if ((m->mdmreg[REG_SI1] & 1) && (mval > VBUFX))
  2715. return 1;
  2716. #endif
  2717. info->xmit_size = mval * 16;
  2718. switch (m->mdmreg[REG_L2PROT]) {
  2719. case ISDN_PROTO_L2_V11096:
  2720. case ISDN_PROTO_L2_V11019:
  2721. case ISDN_PROTO_L2_V11038:
  2722. info->xmit_size /= 10;
  2723. }
  2724. break;
  2725. case REG_SI1I:
  2726. case REG_PLAN:
  2727. case REG_SCREEN:
  2728. /* readonly registers */
  2729. return 1;
  2730. }
  2731. return 0;
  2732. }
  2733. /*
  2734. * Perform ATS command
  2735. */
  2736. static int
  2737. isdn_tty_cmd_ATS(char **p, modem_info *info)
  2738. {
  2739. atemu *m = &info->emu;
  2740. int bitpos;
  2741. int mreg;
  2742. int mval;
  2743. int bval;
  2744. mreg = isdn_getnum(p);
  2745. if (mreg < 0 || mreg >= ISDN_MODEM_NUMREG)
  2746. PARSE_ERROR1;
  2747. switch (*p[0]) {
  2748. case '=':
  2749. p[0]++;
  2750. mval = isdn_getnum(p);
  2751. if (mval < 0 || mval > 255)
  2752. PARSE_ERROR1;
  2753. if (isdn_tty_check_ats(mreg, mval, info, m))
  2754. PARSE_ERROR1;
  2755. m->mdmreg[mreg] = mval;
  2756. break;
  2757. case '.':
  2758. /* Set/Clear a single bit */
  2759. p[0]++;
  2760. bitpos = isdn_getnum(p);
  2761. if ((bitpos < 0) || (bitpos > 7))
  2762. PARSE_ERROR1;
  2763. switch (*p[0]) {
  2764. case '=':
  2765. p[0]++;
  2766. bval = isdn_getnum(p);
  2767. if (bval < 0 || bval > 1)
  2768. PARSE_ERROR1;
  2769. if (bval)
  2770. mval = m->mdmreg[mreg] | (1 << bitpos);
  2771. else
  2772. mval = m->mdmreg[mreg] & ~(1 << bitpos);
  2773. if (isdn_tty_check_ats(mreg, mval, info, m))
  2774. PARSE_ERROR1;
  2775. m->mdmreg[mreg] = mval;
  2776. break;
  2777. case '?':
  2778. p[0]++;
  2779. isdn_tty_at_cout("\r\n", info);
  2780. isdn_tty_at_cout((m->mdmreg[mreg] & (1 << bitpos)) ? "1" : "0",
  2781. info);
  2782. break;
  2783. default:
  2784. PARSE_ERROR1;
  2785. }
  2786. break;
  2787. case '?':
  2788. p[0]++;
  2789. isdn_tty_show_profile(mreg, info);
  2790. break;
  2791. default:
  2792. PARSE_ERROR1;
  2793. break;
  2794. }
  2795. return 0;
  2796. }
  2797. /*
  2798. * Perform ATA command
  2799. */
  2800. static void
  2801. isdn_tty_cmd_ATA(modem_info *info)
  2802. {
  2803. atemu *m = &info->emu;
  2804. isdn_ctrl cmd;
  2805. int l2;
  2806. if (info->msr & UART_MSR_RI) {
  2807. /* Accept incoming call */
  2808. info->last_dir = 0;
  2809. strcpy(info->last_num, dev->num[info->drv_index]);
  2810. m->mdmreg[REG_RINGCNT] = 0;
  2811. info->msr &= ~UART_MSR_RI;
  2812. l2 = m->mdmreg[REG_L2PROT];
  2813. #ifdef CONFIG_ISDN_AUDIO
  2814. /* If more than one bit set in reg18, autoselect Layer2 */
  2815. if ((m->mdmreg[REG_SI1] & m->mdmreg[REG_SI1I]) != m->mdmreg[REG_SI1]) {
  2816. if (m->mdmreg[REG_SI1I] == 1) {
  2817. if ((l2 != ISDN_PROTO_L2_MODEM) && (l2 != ISDN_PROTO_L2_FAX))
  2818. l2 = ISDN_PROTO_L2_TRANS;
  2819. } else
  2820. l2 = ISDN_PROTO_L2_X75I;
  2821. }
  2822. #endif
  2823. cmd.driver = info->isdn_driver;
  2824. cmd.command = ISDN_CMD_SETL2;
  2825. cmd.arg = info->isdn_channel + (l2 << 8);
  2826. info->last_l2 = l2;
  2827. isdn_command(&cmd);
  2828. cmd.driver = info->isdn_driver;
  2829. cmd.command = ISDN_CMD_SETL3;
  2830. cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
  2831. #ifdef CONFIG_ISDN_TTY_FAX
  2832. if (l2 == ISDN_PROTO_L2_FAX) {
  2833. cmd.parm.fax = info->fax;
  2834. info->fax->direction = ISDN_TTY_FAX_CONN_IN;
  2835. }
  2836. #endif
  2837. isdn_command(&cmd);
  2838. cmd.driver = info->isdn_driver;
  2839. cmd.arg = info->isdn_channel;
  2840. cmd.command = ISDN_CMD_ACCEPTD;
  2841. info->dialing = 16;
  2842. info->emu.carrierwait = 0;
  2843. isdn_command(&cmd);
  2844. isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
  2845. } else
  2846. isdn_tty_modem_result(RESULT_NO_ANSWER, info);
  2847. }
  2848. #ifdef CONFIG_ISDN_AUDIO
  2849. /*
  2850. * Parse AT+F.. commands
  2851. */
  2852. static int
  2853. isdn_tty_cmd_PLUSF(char **p, modem_info *info)
  2854. {
  2855. atemu *m = &info->emu;
  2856. char rs[20];
  2857. if (!strncmp(p[0], "CLASS", 5)) {
  2858. p[0] += 5;
  2859. switch (*p[0]) {
  2860. case '?':
  2861. p[0]++;
  2862. sprintf(rs, "\r\n%d",
  2863. (m->mdmreg[REG_SI1] & 1) ? 8 : 0);
  2864. #ifdef CONFIG_ISDN_TTY_FAX
  2865. if (TTY_IS_FCLASS2(info))
  2866. sprintf(rs, "\r\n2");
  2867. else if (TTY_IS_FCLASS1(info))
  2868. sprintf(rs, "\r\n1");
  2869. #endif
  2870. isdn_tty_at_cout(rs, info);
  2871. break;
  2872. case '=':
  2873. p[0]++;
  2874. switch (*p[0]) {
  2875. case '0':
  2876. p[0]++;
  2877. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
  2878. m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
  2879. m->mdmreg[REG_SI1] = 4;
  2880. info->xmit_size =
  2881. m->mdmreg[REG_PSIZE] * 16;
  2882. break;
  2883. #ifdef CONFIG_ISDN_TTY_FAX
  2884. case '1':
  2885. p[0]++;
  2886. if (!(dev->global_features &
  2887. ISDN_FEATURE_L3_FCLASS1))
  2888. PARSE_ERROR1;
  2889. m->mdmreg[REG_SI1] = 1;
  2890. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
  2891. m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS1;
  2892. info->xmit_size =
  2893. m->mdmreg[REG_PSIZE] * 16;
  2894. break;
  2895. case '2':
  2896. p[0]++;
  2897. if (!(dev->global_features &
  2898. ISDN_FEATURE_L3_FCLASS2))
  2899. PARSE_ERROR1;
  2900. m->mdmreg[REG_SI1] = 1;
  2901. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
  2902. m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS2;
  2903. info->xmit_size =
  2904. m->mdmreg[REG_PSIZE] * 16;
  2905. break;
  2906. #endif
  2907. case '8':
  2908. p[0]++;
  2909. /* L2 will change on dialout with si=1 */
  2910. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
  2911. m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
  2912. m->mdmreg[REG_SI1] = 5;
  2913. info->xmit_size = VBUF;
  2914. break;
  2915. case '?':
  2916. p[0]++;
  2917. strcpy(rs, "\r\n0,");
  2918. #ifdef CONFIG_ISDN_TTY_FAX
  2919. if (dev->global_features &
  2920. ISDN_FEATURE_L3_FCLASS1)
  2921. strcat(rs, "1,");
  2922. if (dev->global_features &
  2923. ISDN_FEATURE_L3_FCLASS2)
  2924. strcat(rs, "2,");
  2925. #endif
  2926. strcat(rs, "8");
  2927. isdn_tty_at_cout(rs, info);
  2928. break;
  2929. default:
  2930. PARSE_ERROR1;
  2931. }
  2932. break;
  2933. default:
  2934. PARSE_ERROR1;
  2935. }
  2936. return 0;
  2937. }
  2938. #ifdef CONFIG_ISDN_TTY_FAX
  2939. return (isdn_tty_cmd_PLUSF_FAX(p, info));
  2940. #else
  2941. PARSE_ERROR1;
  2942. #endif
  2943. }
  2944. /*
  2945. * Parse AT+V.. commands
  2946. */
  2947. static int
  2948. isdn_tty_cmd_PLUSV(char **p, modem_info *info)
  2949. {
  2950. atemu *m = &info->emu;
  2951. isdn_ctrl cmd;
  2952. static char *vcmd[] =
  2953. {"NH", "IP", "LS", "RX", "SD", "SM", "TX", "DD", NULL};
  2954. int i;
  2955. int par1;
  2956. int par2;
  2957. char rs[20];
  2958. i = 0;
  2959. while (vcmd[i]) {
  2960. if (!strncmp(vcmd[i], p[0], 2)) {
  2961. p[0] += 2;
  2962. break;
  2963. }
  2964. i++;
  2965. }
  2966. switch (i) {
  2967. case 0:
  2968. /* AT+VNH - Auto hangup feature */
  2969. switch (*p[0]) {
  2970. case '?':
  2971. p[0]++;
  2972. isdn_tty_at_cout("\r\n1", info);
  2973. break;
  2974. case '=':
  2975. p[0]++;
  2976. switch (*p[0]) {
  2977. case '1':
  2978. p[0]++;
  2979. break;
  2980. case '?':
  2981. p[0]++;
  2982. isdn_tty_at_cout("\r\n1", info);
  2983. break;
  2984. default:
  2985. PARSE_ERROR1;
  2986. }
  2987. break;
  2988. default:
  2989. PARSE_ERROR1;
  2990. }
  2991. break;
  2992. case 1:
  2993. /* AT+VIP - Reset all voice parameters */
  2994. isdn_tty_modem_reset_vpar(m);
  2995. break;
  2996. case 2:
  2997. /* AT+VLS - Select device, accept incoming call */
  2998. switch (*p[0]) {
  2999. case '?':
  3000. p[0]++;
  3001. sprintf(rs, "\r\n%d", m->vpar[0]);
  3002. isdn_tty_at_cout(rs, info);
  3003. break;
  3004. case '=':
  3005. p[0]++;
  3006. switch (*p[0]) {
  3007. case '0':
  3008. p[0]++;
  3009. m->vpar[0] = 0;
  3010. break;
  3011. case '2':
  3012. p[0]++;
  3013. m->vpar[0] = 2;
  3014. break;
  3015. case '?':
  3016. p[0]++;
  3017. isdn_tty_at_cout("\r\n0,2", info);
  3018. break;
  3019. default:
  3020. PARSE_ERROR1;
  3021. }
  3022. break;
  3023. default:
  3024. PARSE_ERROR1;
  3025. }
  3026. break;
  3027. case 3:
  3028. /* AT+VRX - Start recording */
  3029. if (!m->vpar[0])
  3030. PARSE_ERROR1;
  3031. if (info->online != 1) {
  3032. isdn_tty_modem_result(RESULT_NO_ANSWER, info);
  3033. return 1;
  3034. }
  3035. info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
  3036. if (!info->dtmf_state) {
  3037. printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
  3038. PARSE_ERROR1;
  3039. }
  3040. info->silence_state = isdn_audio_silence_init(info->silence_state);
  3041. if (!info->silence_state) {
  3042. printk(KERN_WARNING "isdn_tty: Couldn't malloc silence state\n");
  3043. PARSE_ERROR1;
  3044. }
  3045. if (m->vpar[3] < 5) {
  3046. info->adpcmr = isdn_audio_adpcm_init(info->adpcmr, m->vpar[3]);
  3047. if (!info->adpcmr) {
  3048. printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
  3049. PARSE_ERROR1;
  3050. }
  3051. }
  3052. #ifdef ISDN_DEBUG_AT
  3053. printk(KERN_DEBUG "AT: +VRX\n");
  3054. #endif
  3055. info->vonline |= 1;
  3056. isdn_tty_modem_result(RESULT_CONNECT, info);
  3057. return 0;
  3058. break;
  3059. case 4:
  3060. /* AT+VSD - Silence detection */
  3061. switch (*p[0]) {
  3062. case '?':
  3063. p[0]++;
  3064. sprintf(rs, "\r\n<%d>,<%d>",
  3065. m->vpar[1],
  3066. m->vpar[2]);
  3067. isdn_tty_at_cout(rs, info);
  3068. break;
  3069. case '=':
  3070. p[0]++;
  3071. if ((*p[0] >= '0') && (*p[0] <= '9')) {
  3072. par1 = isdn_getnum(p);
  3073. if ((par1 < 0) || (par1 > 31))
  3074. PARSE_ERROR1;
  3075. if (*p[0] != ',')
  3076. PARSE_ERROR1;
  3077. p[0]++;
  3078. par2 = isdn_getnum(p);
  3079. if ((par2 < 0) || (par2 > 255))
  3080. PARSE_ERROR1;
  3081. m->vpar[1] = par1;
  3082. m->vpar[2] = par2;
  3083. break;
  3084. } else
  3085. if (*p[0] == '?') {
  3086. p[0]++;
  3087. isdn_tty_at_cout("\r\n<0-31>,<0-255>",
  3088. info);
  3089. break;
  3090. } else
  3091. PARSE_ERROR1;
  3092. break;
  3093. default:
  3094. PARSE_ERROR1;
  3095. }
  3096. break;
  3097. case 5:
  3098. /* AT+VSM - Select compression */
  3099. switch (*p[0]) {
  3100. case '?':
  3101. p[0]++;
  3102. sprintf(rs, "\r\n<%d>,<%d><8000>",
  3103. m->vpar[3],
  3104. m->vpar[1]);
  3105. isdn_tty_at_cout(rs, info);
  3106. break;
  3107. case '=':
  3108. p[0]++;
  3109. switch (*p[0]) {
  3110. case '2':
  3111. case '3':
  3112. case '4':
  3113. case '5':
  3114. case '6':
  3115. par1 = isdn_getnum(p);
  3116. if ((par1 < 2) || (par1 > 6))
  3117. PARSE_ERROR1;
  3118. m->vpar[3] = par1;
  3119. break;
  3120. case '?':
  3121. p[0]++;
  3122. isdn_tty_at_cout("\r\n2;ADPCM;2;0;(8000)\r\n",
  3123. info);
  3124. isdn_tty_at_cout("3;ADPCM;3;0;(8000)\r\n",
  3125. info);
  3126. isdn_tty_at_cout("4;ADPCM;4;0;(8000)\r\n",
  3127. info);
  3128. isdn_tty_at_cout("5;ALAW;8;0;(8000)\r\n",
  3129. info);
  3130. isdn_tty_at_cout("6;ULAW;8;0;(8000)\r\n",
  3131. info);
  3132. break;
  3133. default:
  3134. PARSE_ERROR1;
  3135. }
  3136. break;
  3137. default:
  3138. PARSE_ERROR1;
  3139. }
  3140. break;
  3141. case 6:
  3142. /* AT+VTX - Start sending */
  3143. if (!m->vpar[0])
  3144. PARSE_ERROR1;
  3145. if (info->online != 1) {
  3146. isdn_tty_modem_result(RESULT_NO_ANSWER, info);
  3147. return 1;
  3148. }
  3149. info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
  3150. if (!info->dtmf_state) {
  3151. printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
  3152. PARSE_ERROR1;
  3153. }
  3154. if (m->vpar[3] < 5) {
  3155. info->adpcms = isdn_audio_adpcm_init(info->adpcms, m->vpar[3]);
  3156. if (!info->adpcms) {
  3157. printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
  3158. PARSE_ERROR1;
  3159. }
  3160. }
  3161. #ifdef ISDN_DEBUG_AT
  3162. printk(KERN_DEBUG "AT: +VTX\n");
  3163. #endif
  3164. m->lastDLE = 0;
  3165. info->vonline |= 2;
  3166. isdn_tty_modem_result(RESULT_CONNECT, info);
  3167. return 0;
  3168. break;
  3169. case 7:
  3170. /* AT+VDD - DTMF detection */
  3171. switch (*p[0]) {
  3172. case '?':
  3173. p[0]++;
  3174. sprintf(rs, "\r\n<%d>,<%d>",
  3175. m->vpar[4],
  3176. m->vpar[5]);
  3177. isdn_tty_at_cout(rs, info);
  3178. break;
  3179. case '=':
  3180. p[0]++;
  3181. if ((*p[0] >= '0') && (*p[0] <= '9')) {
  3182. if (info->online != 1)
  3183. PARSE_ERROR1;
  3184. par1 = isdn_getnum(p);
  3185. if ((par1 < 0) || (par1 > 15))
  3186. PARSE_ERROR1;
  3187. if (*p[0] != ',')
  3188. PARSE_ERROR1;
  3189. p[0]++;
  3190. par2 = isdn_getnum(p);
  3191. if ((par2 < 0) || (par2 > 255))
  3192. PARSE_ERROR1;
  3193. m->vpar[4] = par1;
  3194. m->vpar[5] = par2;
  3195. cmd.driver = info->isdn_driver;
  3196. cmd.command = ISDN_CMD_AUDIO;
  3197. cmd.arg = info->isdn_channel + (ISDN_AUDIO_SETDD << 8);
  3198. cmd.parm.num[0] = par1;
  3199. cmd.parm.num[1] = par2;
  3200. isdn_command(&cmd);
  3201. break;
  3202. } else
  3203. if (*p[0] == '?') {
  3204. p[0]++;
  3205. isdn_tty_at_cout("\r\n<0-15>,<0-255>",
  3206. info);
  3207. break;
  3208. } else
  3209. PARSE_ERROR1;
  3210. break;
  3211. default:
  3212. PARSE_ERROR1;
  3213. }
  3214. break;
  3215. default:
  3216. PARSE_ERROR1;
  3217. }
  3218. return 0;
  3219. }
  3220. #endif /* CONFIG_ISDN_AUDIO */
  3221. /*
  3222. * Parse and perform an AT-command-line.
  3223. */
  3224. static void
  3225. isdn_tty_parse_at(modem_info *info)
  3226. {
  3227. atemu *m = &info->emu;
  3228. char *p;
  3229. char ds[ISDN_MSNLEN];
  3230. #ifdef ISDN_DEBUG_AT
  3231. printk(KERN_DEBUG "AT: '%s'\n", m->mdmcmd);
  3232. #endif
  3233. for (p = &m->mdmcmd[2]; *p;) {
  3234. switch (*p) {
  3235. case ' ':
  3236. p++;
  3237. break;
  3238. case 'A':
  3239. /* A - Accept incoming call */
  3240. p++;
  3241. isdn_tty_cmd_ATA(info);
  3242. return;
  3243. break;
  3244. case 'D':
  3245. /* D - Dial */
  3246. if (info->msr & UART_MSR_DCD)
  3247. PARSE_ERROR;
  3248. if (info->msr & UART_MSR_RI) {
  3249. isdn_tty_modem_result(RESULT_NO_CARRIER, info);
  3250. return;
  3251. }
  3252. isdn_tty_getdial(++p, ds, sizeof ds);
  3253. p += strlen(p);
  3254. if (!strlen(m->msn))
  3255. isdn_tty_modem_result(RESULT_NO_MSN_EAZ, info);
  3256. else if (strlen(ds))
  3257. isdn_tty_dial(ds, info, m);
  3258. else
  3259. PARSE_ERROR;
  3260. return;
  3261. case 'E':
  3262. /* E - Turn Echo on/off */
  3263. p++;
  3264. switch (isdn_getnum(&p)) {
  3265. case 0:
  3266. m->mdmreg[REG_ECHO] &= ~BIT_ECHO;
  3267. break;
  3268. case 1:
  3269. m->mdmreg[REG_ECHO] |= BIT_ECHO;
  3270. break;
  3271. default:
  3272. PARSE_ERROR;
  3273. }
  3274. break;
  3275. case 'H':
  3276. /* H - On/Off-hook */
  3277. p++;
  3278. switch (*p) {
  3279. case '0':
  3280. p++;
  3281. isdn_tty_on_hook(info);
  3282. break;
  3283. case '1':
  3284. p++;
  3285. isdn_tty_off_hook();
  3286. break;
  3287. default:
  3288. isdn_tty_on_hook(info);
  3289. break;
  3290. }
  3291. break;
  3292. case 'I':
  3293. /* I - Information */
  3294. p++;
  3295. isdn_tty_at_cout("\r\nLinux ISDN", info);
  3296. switch (*p) {
  3297. case '0':
  3298. case '1':
  3299. p++;
  3300. break;
  3301. case '2':
  3302. p++;
  3303. isdn_tty_report(info);
  3304. break;
  3305. case '3':
  3306. p++;
  3307. snprintf(ds, sizeof(ds), "\r\n%d", info->emu.charge);
  3308. isdn_tty_at_cout(ds, info);
  3309. break;
  3310. default:;
  3311. }
  3312. break;
  3313. #ifdef DUMMY_HAYES_AT
  3314. case 'L':
  3315. case 'M':
  3316. /* only for be compilant with common scripts */
  3317. /* no function */
  3318. p++;
  3319. isdn_getnum(&p);
  3320. break;
  3321. #endif
  3322. case 'O':
  3323. /* O - Go online */
  3324. p++;
  3325. if (info->msr & UART_MSR_DCD)
  3326. /* if B-Channel is up */
  3327. isdn_tty_modem_result((m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) ? RESULT_CONNECT : RESULT_CONNECT64000, info);
  3328. else
  3329. isdn_tty_modem_result(RESULT_NO_CARRIER, info);
  3330. return;
  3331. case 'Q':
  3332. /* Q - Turn Emulator messages on/off */
  3333. p++;
  3334. switch (isdn_getnum(&p)) {
  3335. case 0:
  3336. m->mdmreg[REG_RESP] |= BIT_RESP;
  3337. break;
  3338. case 1:
  3339. m->mdmreg[REG_RESP] &= ~BIT_RESP;
  3340. break;
  3341. default:
  3342. PARSE_ERROR;
  3343. }
  3344. break;
  3345. case 'S':
  3346. /* S - Set/Get Register */
  3347. p++;
  3348. if (isdn_tty_cmd_ATS(&p, info))
  3349. return;
  3350. break;
  3351. case 'V':
  3352. /* V - Numeric or ASCII Emulator-messages */
  3353. p++;
  3354. switch (isdn_getnum(&p)) {
  3355. case 0:
  3356. m->mdmreg[REG_RESP] |= BIT_RESPNUM;
  3357. break;
  3358. case 1:
  3359. m->mdmreg[REG_RESP] &= ~BIT_RESPNUM;
  3360. break;
  3361. default:
  3362. PARSE_ERROR;
  3363. }
  3364. break;
  3365. case 'Z':
  3366. /* Z - Load Registers from Profile */
  3367. p++;
  3368. if (info->msr & UART_MSR_DCD) {
  3369. info->online = 0;
  3370. isdn_tty_on_hook(info);
  3371. }
  3372. isdn_tty_modem_reset_regs(info, 1);
  3373. break;
  3374. case '+':
  3375. p++;
  3376. switch (*p) {
  3377. #ifdef CONFIG_ISDN_AUDIO
  3378. case 'F':
  3379. p++;
  3380. if (isdn_tty_cmd_PLUSF(&p, info))
  3381. return;
  3382. break;
  3383. case 'V':
  3384. if ((!(m->mdmreg[REG_SI1] & 1)) ||
  3385. (m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM))
  3386. PARSE_ERROR;
  3387. p++;
  3388. if (isdn_tty_cmd_PLUSV(&p, info))
  3389. return;
  3390. break;
  3391. #endif /* CONFIG_ISDN_AUDIO */
  3392. case 'S': /* SUSPEND */
  3393. p++;
  3394. isdn_tty_get_msnstr(ds, &p);
  3395. isdn_tty_suspend(ds, info, m);
  3396. break;
  3397. case 'R': /* RESUME */
  3398. p++;
  3399. isdn_tty_get_msnstr(ds, &p);
  3400. isdn_tty_resume(ds, info, m);
  3401. break;
  3402. case 'M': /* MESSAGE */
  3403. p++;
  3404. isdn_tty_send_msg(info, m, p);
  3405. break;
  3406. default:
  3407. PARSE_ERROR;
  3408. }
  3409. break;
  3410. case '&':
  3411. p++;
  3412. if (isdn_tty_cmd_ATand(&p, info))
  3413. return;
  3414. break;
  3415. default:
  3416. PARSE_ERROR;
  3417. }
  3418. }
  3419. #ifdef CONFIG_ISDN_AUDIO
  3420. if (!info->vonline)
  3421. #endif
  3422. isdn_tty_modem_result(RESULT_OK, info);
  3423. }
  3424. /* Need own toupper() because standard-toupper is not available
  3425. * within modules.
  3426. */
  3427. #define my_toupper(c) (((c >= 'a') && (c <= 'z')) ? (c & 0xdf) : c)
  3428. /*
  3429. * Perform line-editing of AT-commands
  3430. *
  3431. * Parameters:
  3432. * p inputbuffer
  3433. * count length of buffer
  3434. * channel index to line (minor-device)
  3435. */
  3436. static int
  3437. isdn_tty_edit_at(const char *p, int count, modem_info *info)
  3438. {
  3439. atemu *m = &info->emu;
  3440. int total = 0;
  3441. u_char c;
  3442. char eb[2];
  3443. int cnt;
  3444. for (cnt = count; cnt > 0; p++, cnt--) {
  3445. c = *p;
  3446. total++;
  3447. if (c == m->mdmreg[REG_CR] || c == m->mdmreg[REG_LF]) {
  3448. /* Separator (CR or LF) */
  3449. m->mdmcmd[m->mdmcmdl] = 0;
  3450. if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
  3451. eb[0] = c;
  3452. eb[1] = 0;
  3453. isdn_tty_at_cout(eb, info);
  3454. }
  3455. if ((m->mdmcmdl >= 2) && (!(strncmp(m->mdmcmd, "AT", 2))))
  3456. isdn_tty_parse_at(info);
  3457. m->mdmcmdl = 0;
  3458. continue;
  3459. }
  3460. if (c == m->mdmreg[REG_BS] && m->mdmreg[REG_BS] < 128) {
  3461. /* Backspace-Function */
  3462. if ((m->mdmcmdl > 2) || (!m->mdmcmdl)) {
  3463. if (m->mdmcmdl)
  3464. m->mdmcmdl--;
  3465. if (m->mdmreg[REG_ECHO] & BIT_ECHO)
  3466. isdn_tty_at_cout("\b", info);
  3467. }
  3468. continue;
  3469. }
  3470. if (cmdchar(c)) {
  3471. if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
  3472. eb[0] = c;
  3473. eb[1] = 0;
  3474. isdn_tty_at_cout(eb, info);
  3475. }
  3476. if (m->mdmcmdl < 255) {
  3477. c = my_toupper(c);
  3478. switch (m->mdmcmdl) {
  3479. case 1:
  3480. if (c == 'T') {
  3481. m->mdmcmd[m->mdmcmdl] = c;
  3482. m->mdmcmd[++m->mdmcmdl] = 0;
  3483. break;
  3484. } else
  3485. m->mdmcmdl = 0;
  3486. /* Fall through, check for 'A' */
  3487. case 0:
  3488. if (c == 'A') {
  3489. m->mdmcmd[m->mdmcmdl] = c;
  3490. m->mdmcmd[++m->mdmcmdl] = 0;
  3491. }
  3492. break;
  3493. default:
  3494. m->mdmcmd[m->mdmcmdl] = c;
  3495. m->mdmcmd[++m->mdmcmdl] = 0;
  3496. }
  3497. }
  3498. }
  3499. }
  3500. return total;
  3501. }
  3502. /*
  3503. * Switch all modem-channels who are online and got a valid
  3504. * escape-sequence 1.5 seconds ago, to command-mode.
  3505. * This function is called every second via timer-interrupt from within
  3506. * timer-dispatcher isdn_timer_function()
  3507. */
  3508. void
  3509. isdn_tty_modem_escape(void)
  3510. {
  3511. int ton = 0;
  3512. int i;
  3513. int midx;
  3514. for (i = 0; i < ISDN_MAX_CHANNELS; i++)
  3515. if (USG_MODEM(dev->usage[i]) && (midx = dev->m_idx[i]) >= 0) {
  3516. modem_info *info = &dev->mdm.info[midx];
  3517. if (info->online) {
  3518. ton = 1;
  3519. if ((info->emu.pluscount == 3) &&
  3520. time_after(jiffies,
  3521. info->emu.lastplus + PLUSWAIT2)) {
  3522. info->emu.pluscount = 0;
  3523. info->online = 0;
  3524. isdn_tty_modem_result(RESULT_OK, info);
  3525. }
  3526. }
  3527. }
  3528. isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, ton);
  3529. }
  3530. /*
  3531. * Put a RING-message to all modem-channels who have the RI-bit set.
  3532. * This function is called every second via timer-interrupt from within
  3533. * timer-dispatcher isdn_timer_function()
  3534. */
  3535. void
  3536. isdn_tty_modem_ring(void)
  3537. {
  3538. int ton = 0;
  3539. int i;
  3540. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  3541. modem_info *info = &dev->mdm.info[i];
  3542. if (info->msr & UART_MSR_RI) {
  3543. ton = 1;
  3544. isdn_tty_modem_result(RESULT_RING, info);
  3545. }
  3546. }
  3547. isdn_timer_ctrl(ISDN_TIMER_MODEMRING, ton);
  3548. }
  3549. /*
  3550. * For all online tty's, try sending data to
  3551. * the lower levels.
  3552. */
  3553. void
  3554. isdn_tty_modem_xmit(void)
  3555. {
  3556. int ton = 1;
  3557. int i;
  3558. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  3559. modem_info *info = &dev->mdm.info[i];
  3560. if (info->online) {
  3561. ton = 1;
  3562. isdn_tty_senddown(info);
  3563. isdn_tty_tint(info);
  3564. }
  3565. }
  3566. isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, ton);
  3567. }
  3568. /*
  3569. * Check all channels if we have a 'no carrier' timeout.
  3570. * Timeout value is set by Register S7.
  3571. */
  3572. void
  3573. isdn_tty_carrier_timeout(void)
  3574. {
  3575. int ton = 0;
  3576. int i;
  3577. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  3578. modem_info *info = &dev->mdm.info[i];
  3579. if (!info->dialing)
  3580. continue;
  3581. if (info->emu.carrierwait++ > info->emu.mdmreg[REG_WAITC]) {
  3582. info->dialing = 0;
  3583. isdn_tty_modem_result(RESULT_NO_CARRIER, info);
  3584. isdn_tty_modem_hup(info, 1);
  3585. } else
  3586. ton = 1;
  3587. }
  3588. isdn_timer_ctrl(ISDN_TIMER_CARRIER, ton);
  3589. }