isdn_tty.c 89 KB

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