isdn_tty.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772
  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. memcpy(skb_put(skb, buflen), 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(unsigned long data)
  508. {
  509. modem_info *info = (modem_info *) data;
  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. strncpy(&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. strncpy(&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. if (!old_termios)
  1355. isdn_tty_change_speed(info);
  1356. else {
  1357. if (tty->termios.c_cflag == old_termios->c_cflag &&
  1358. tty->termios.c_ispeed == old_termios->c_ispeed &&
  1359. tty->termios.c_ospeed == old_termios->c_ospeed)
  1360. return;
  1361. isdn_tty_change_speed(info);
  1362. }
  1363. }
  1364. /*
  1365. * ------------------------------------------------------------
  1366. * isdn_tty_open() and friends
  1367. * ------------------------------------------------------------
  1368. */
  1369. static int isdn_tty_install(struct tty_driver *driver, struct tty_struct *tty)
  1370. {
  1371. modem_info *info = &dev->mdm.info[tty->index];
  1372. if (isdn_tty_paranoia_check(info, tty->name, __func__))
  1373. return -ENODEV;
  1374. tty->driver_data = info;
  1375. return tty_port_install(&info->port, driver, tty);
  1376. }
  1377. /*
  1378. * This routine is called whenever a serial port is opened. It
  1379. * enables interrupts for a serial port, linking in its async structure into
  1380. * the IRQ chain. It also performs the serial-specific
  1381. * initialization for the tty structure.
  1382. */
  1383. static int
  1384. isdn_tty_open(struct tty_struct *tty, struct file *filp)
  1385. {
  1386. modem_info *info = tty->driver_data;
  1387. struct tty_port *port = &info->port;
  1388. int retval;
  1389. #ifdef ISDN_DEBUG_MODEM_OPEN
  1390. printk(KERN_DEBUG "isdn_tty_open %s, count = %d\n", tty->name,
  1391. port->count);
  1392. #endif
  1393. port->count++;
  1394. port->tty = tty;
  1395. /*
  1396. * Start up serial port
  1397. */
  1398. retval = isdn_tty_startup(info);
  1399. if (retval) {
  1400. #ifdef ISDN_DEBUG_MODEM_OPEN
  1401. printk(KERN_DEBUG "isdn_tty_open return after startup\n");
  1402. #endif
  1403. return retval;
  1404. }
  1405. retval = tty_port_block_til_ready(port, tty, filp);
  1406. if (retval) {
  1407. #ifdef ISDN_DEBUG_MODEM_OPEN
  1408. printk(KERN_DEBUG "isdn_tty_open return after isdn_tty_block_til_ready \n");
  1409. #endif
  1410. return retval;
  1411. }
  1412. #ifdef ISDN_DEBUG_MODEM_OPEN
  1413. printk(KERN_DEBUG "isdn_tty_open ttyi%d successful...\n", info->line);
  1414. #endif
  1415. dev->modempoll++;
  1416. #ifdef ISDN_DEBUG_MODEM_OPEN
  1417. printk(KERN_DEBUG "isdn_tty_open normal exit\n");
  1418. #endif
  1419. return 0;
  1420. }
  1421. static void
  1422. isdn_tty_close(struct tty_struct *tty, struct file *filp)
  1423. {
  1424. modem_info *info = (modem_info *) tty->driver_data;
  1425. struct tty_port *port = &info->port;
  1426. ulong timeout;
  1427. if (!info || isdn_tty_paranoia_check(info, tty->name, "isdn_tty_close"))
  1428. return;
  1429. if (tty_hung_up_p(filp)) {
  1430. #ifdef ISDN_DEBUG_MODEM_OPEN
  1431. printk(KERN_DEBUG "isdn_tty_close return after tty_hung_up_p\n");
  1432. #endif
  1433. return;
  1434. }
  1435. if ((tty->count == 1) && (port->count != 1)) {
  1436. /*
  1437. * Uh, oh. tty->count is 1, which means that the tty
  1438. * structure will be freed. Info->count should always
  1439. * be one in these conditions. If it's greater than
  1440. * one, we've got real problems, since it means the
  1441. * serial port won't be shutdown.
  1442. */
  1443. printk(KERN_ERR "isdn_tty_close: bad port count; tty->count is 1, "
  1444. "info->count is %d\n", port->count);
  1445. port->count = 1;
  1446. }
  1447. if (--port->count < 0) {
  1448. printk(KERN_ERR "isdn_tty_close: bad port count for ttyi%d: %d\n",
  1449. info->line, port->count);
  1450. port->count = 0;
  1451. }
  1452. if (port->count) {
  1453. #ifdef ISDN_DEBUG_MODEM_OPEN
  1454. printk(KERN_DEBUG "isdn_tty_close after info->count != 0\n");
  1455. #endif
  1456. return;
  1457. }
  1458. info->closing = 1;
  1459. tty->closing = 1;
  1460. /*
  1461. * At this point we stop accepting input. To do this, we
  1462. * disable the receive line status interrupts, and tell the
  1463. * interrupt driver to stop checking the data ready bit in the
  1464. * line status register.
  1465. */
  1466. if (tty_port_initialized(port)) {
  1467. tty_wait_until_sent(tty, 3000); /* 30 seconds timeout */
  1468. /*
  1469. * Before we drop DTR, make sure the UART transmitter
  1470. * has completely drained; this is especially
  1471. * important if there is a transmit FIFO!
  1472. */
  1473. timeout = jiffies + HZ;
  1474. while (!(info->lsr & UART_LSR_TEMT)) {
  1475. schedule_timeout_interruptible(20);
  1476. if (time_after(jiffies, timeout))
  1477. break;
  1478. }
  1479. }
  1480. dev->modempoll--;
  1481. isdn_tty_shutdown(info);
  1482. isdn_tty_flush_buffer(tty);
  1483. tty_ldisc_flush(tty);
  1484. port->tty = NULL;
  1485. info->ncarrier = 0;
  1486. tty_port_close_end(port, tty);
  1487. info->closing = 0;
  1488. #ifdef ISDN_DEBUG_MODEM_OPEN
  1489. printk(KERN_DEBUG "isdn_tty_close normal exit\n");
  1490. #endif
  1491. }
  1492. /*
  1493. * isdn_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
  1494. */
  1495. static void
  1496. isdn_tty_hangup(struct tty_struct *tty)
  1497. {
  1498. modem_info *info = (modem_info *) tty->driver_data;
  1499. struct tty_port *port = &info->port;
  1500. if (isdn_tty_paranoia_check(info, tty->name, "isdn_tty_hangup"))
  1501. return;
  1502. isdn_tty_shutdown(info);
  1503. port->count = 0;
  1504. tty_port_set_active(port, 0);
  1505. port->tty = NULL;
  1506. wake_up_interruptible(&port->open_wait);
  1507. }
  1508. /* This routine initializes all emulator-data.
  1509. */
  1510. static void
  1511. isdn_tty_reset_profile(atemu *m)
  1512. {
  1513. m->profile[0] = 0;
  1514. m->profile[1] = 0;
  1515. m->profile[2] = 43;
  1516. m->profile[3] = 13;
  1517. m->profile[4] = 10;
  1518. m->profile[5] = 8;
  1519. m->profile[6] = 3;
  1520. m->profile[7] = 60;
  1521. m->profile[8] = 2;
  1522. m->profile[9] = 6;
  1523. m->profile[10] = 7;
  1524. m->profile[11] = 70;
  1525. m->profile[12] = 0x45;
  1526. m->profile[13] = 4;
  1527. m->profile[14] = ISDN_PROTO_L2_X75I;
  1528. m->profile[15] = ISDN_PROTO_L3_TRANS;
  1529. m->profile[16] = ISDN_SERIAL_XMIT_SIZE / 16;
  1530. m->profile[17] = ISDN_MODEM_WINSIZE;
  1531. m->profile[18] = 4;
  1532. m->profile[19] = 0;
  1533. m->profile[20] = 0;
  1534. m->profile[23] = 0;
  1535. m->pmsn[0] = '\0';
  1536. m->plmsn[0] = '\0';
  1537. }
  1538. #ifdef CONFIG_ISDN_AUDIO
  1539. static void
  1540. isdn_tty_modem_reset_vpar(atemu *m)
  1541. {
  1542. m->vpar[0] = 2; /* Voice-device (2 = phone line) */
  1543. m->vpar[1] = 0; /* Silence detection level (0 = none ) */
  1544. m->vpar[2] = 70; /* Silence interval (7 sec. ) */
  1545. m->vpar[3] = 2; /* Compression type (1 = ADPCM-2 ) */
  1546. m->vpar[4] = 0; /* DTMF detection level (0 = softcode ) */
  1547. m->vpar[5] = 8; /* DTMF interval (8 * 5 ms. ) */
  1548. }
  1549. #endif
  1550. #ifdef CONFIG_ISDN_TTY_FAX
  1551. static void
  1552. isdn_tty_modem_reset_faxpar(modem_info *info)
  1553. {
  1554. T30_s *f = info->fax;
  1555. f->code = 0;
  1556. f->phase = ISDN_FAX_PHASE_IDLE;
  1557. f->direction = 0;
  1558. f->resolution = 1; /* fine */
  1559. f->rate = 5; /* 14400 bit/s */
  1560. f->width = 0;
  1561. f->length = 0;
  1562. f->compression = 0;
  1563. f->ecm = 0;
  1564. f->binary = 0;
  1565. f->scantime = 0;
  1566. memset(&f->id[0], 32, FAXIDLEN - 1);
  1567. f->id[FAXIDLEN - 1] = 0;
  1568. f->badlin = 0;
  1569. f->badmul = 0;
  1570. f->bor = 0;
  1571. f->nbc = 0;
  1572. f->cq = 0;
  1573. f->cr = 0;
  1574. f->ctcrty = 0;
  1575. f->minsp = 0;
  1576. f->phcto = 30;
  1577. f->rel = 0;
  1578. memset(&f->pollid[0], 32, FAXIDLEN - 1);
  1579. f->pollid[FAXIDLEN - 1] = 0;
  1580. }
  1581. #endif
  1582. static void
  1583. isdn_tty_modem_reset_regs(modem_info *info, int force)
  1584. {
  1585. atemu *m = &info->emu;
  1586. if ((m->mdmreg[REG_DTRR] & BIT_DTRR) || force) {
  1587. memcpy(m->mdmreg, m->profile, ISDN_MODEM_NUMREG);
  1588. memcpy(m->msn, m->pmsn, ISDN_MSNLEN);
  1589. memcpy(m->lmsn, m->plmsn, ISDN_LMSNLEN);
  1590. info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
  1591. }
  1592. #ifdef CONFIG_ISDN_AUDIO
  1593. isdn_tty_modem_reset_vpar(m);
  1594. #endif
  1595. #ifdef CONFIG_ISDN_TTY_FAX
  1596. isdn_tty_modem_reset_faxpar(info);
  1597. #endif
  1598. m->mdmcmdl = 0;
  1599. }
  1600. static void
  1601. modem_write_profile(atemu *m)
  1602. {
  1603. memcpy(m->profile, m->mdmreg, ISDN_MODEM_NUMREG);
  1604. memcpy(m->pmsn, m->msn, ISDN_MSNLEN);
  1605. memcpy(m->plmsn, m->lmsn, ISDN_LMSNLEN);
  1606. if (dev->profd)
  1607. send_sig(SIGIO, dev->profd, 1);
  1608. }
  1609. static const struct tty_operations modem_ops = {
  1610. .install = isdn_tty_install,
  1611. .open = isdn_tty_open,
  1612. .close = isdn_tty_close,
  1613. .write = isdn_tty_write,
  1614. .flush_chars = isdn_tty_flush_chars,
  1615. .write_room = isdn_tty_write_room,
  1616. .chars_in_buffer = isdn_tty_chars_in_buffer,
  1617. .flush_buffer = isdn_tty_flush_buffer,
  1618. .ioctl = isdn_tty_ioctl,
  1619. .throttle = isdn_tty_throttle,
  1620. .unthrottle = isdn_tty_unthrottle,
  1621. .set_termios = isdn_tty_set_termios,
  1622. .hangup = isdn_tty_hangup,
  1623. .tiocmget = isdn_tty_tiocmget,
  1624. .tiocmset = isdn_tty_tiocmset,
  1625. };
  1626. static int isdn_tty_carrier_raised(struct tty_port *port)
  1627. {
  1628. modem_info *info = container_of(port, modem_info, port);
  1629. return info->msr & UART_MSR_DCD;
  1630. }
  1631. static const struct tty_port_operations isdn_tty_port_ops = {
  1632. .carrier_raised = isdn_tty_carrier_raised,
  1633. };
  1634. int
  1635. isdn_tty_modem_init(void)
  1636. {
  1637. isdn_modem_t *m;
  1638. int i, retval;
  1639. modem_info *info;
  1640. m = &dev->mdm;
  1641. m->tty_modem = alloc_tty_driver(ISDN_MAX_CHANNELS);
  1642. if (!m->tty_modem)
  1643. return -ENOMEM;
  1644. m->tty_modem->name = "ttyI";
  1645. m->tty_modem->major = ISDN_TTY_MAJOR;
  1646. m->tty_modem->minor_start = 0;
  1647. m->tty_modem->type = TTY_DRIVER_TYPE_SERIAL;
  1648. m->tty_modem->subtype = SERIAL_TYPE_NORMAL;
  1649. m->tty_modem->init_termios = tty_std_termios;
  1650. m->tty_modem->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1651. m->tty_modem->flags = TTY_DRIVER_REAL_RAW;
  1652. m->tty_modem->driver_name = "isdn_tty";
  1653. tty_set_operations(m->tty_modem, &modem_ops);
  1654. retval = tty_register_driver(m->tty_modem);
  1655. if (retval) {
  1656. printk(KERN_WARNING "isdn_tty: Couldn't register modem-device\n");
  1657. goto err;
  1658. }
  1659. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  1660. info = &m->info[i];
  1661. #ifdef CONFIG_ISDN_TTY_FAX
  1662. if (!(info->fax = kmalloc(sizeof(T30_s), GFP_KERNEL))) {
  1663. printk(KERN_ERR "Could not allocate fax t30-buffer\n");
  1664. retval = -ENOMEM;
  1665. goto err_unregister;
  1666. }
  1667. #endif
  1668. tty_port_init(&info->port);
  1669. info->port.ops = &isdn_tty_port_ops;
  1670. spin_lock_init(&info->readlock);
  1671. sprintf(info->last_cause, "0000");
  1672. sprintf(info->last_num, "none");
  1673. info->last_dir = 0;
  1674. info->last_lhup = 1;
  1675. info->last_l2 = -1;
  1676. info->last_si = 0;
  1677. isdn_tty_reset_profile(&info->emu);
  1678. isdn_tty_modem_reset_regs(info, 1);
  1679. info->magic = ISDN_ASYNC_MAGIC;
  1680. info->line = i;
  1681. info->x_char = 0;
  1682. info->isdn_driver = -1;
  1683. info->isdn_channel = -1;
  1684. info->drv_index = -1;
  1685. info->xmit_size = ISDN_SERIAL_XMIT_SIZE;
  1686. setup_timer(&info->nc_timer, isdn_tty_modem_do_ncarrier,
  1687. (unsigned long)info);
  1688. skb_queue_head_init(&info->xmit_queue);
  1689. #ifdef CONFIG_ISDN_AUDIO
  1690. skb_queue_head_init(&info->dtmf_queue);
  1691. #endif
  1692. info->port.xmit_buf = kmalloc(ISDN_SERIAL_XMIT_MAX + 5,
  1693. GFP_KERNEL);
  1694. if (!info->port.xmit_buf) {
  1695. printk(KERN_ERR "Could not allocate modem xmit-buffer\n");
  1696. retval = -ENOMEM;
  1697. goto err_unregister;
  1698. }
  1699. /* Make room for T.70 header */
  1700. info->port.xmit_buf += 4;
  1701. }
  1702. return 0;
  1703. err_unregister:
  1704. for (i--; i >= 0; i--) {
  1705. info = &m->info[i];
  1706. #ifdef CONFIG_ISDN_TTY_FAX
  1707. kfree(info->fax);
  1708. #endif
  1709. kfree(info->port.xmit_buf - 4);
  1710. info->port.xmit_buf = NULL;
  1711. tty_port_destroy(&info->port);
  1712. }
  1713. tty_unregister_driver(m->tty_modem);
  1714. err:
  1715. put_tty_driver(m->tty_modem);
  1716. m->tty_modem = NULL;
  1717. return retval;
  1718. }
  1719. void
  1720. isdn_tty_exit(void)
  1721. {
  1722. modem_info *info;
  1723. int i;
  1724. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  1725. info = &dev->mdm.info[i];
  1726. isdn_tty_cleanup_xmit(info);
  1727. #ifdef CONFIG_ISDN_TTY_FAX
  1728. kfree(info->fax);
  1729. #endif
  1730. kfree(info->port.xmit_buf - 4);
  1731. info->port.xmit_buf = NULL;
  1732. tty_port_destroy(&info->port);
  1733. }
  1734. tty_unregister_driver(dev->mdm.tty_modem);
  1735. put_tty_driver(dev->mdm.tty_modem);
  1736. dev->mdm.tty_modem = NULL;
  1737. }
  1738. /*
  1739. * isdn_tty_match_icall(char *MSN, atemu *tty_emulator, int dev_idx)
  1740. * match the MSN against the MSNs (glob patterns) defined for tty_emulator,
  1741. * and return 0 for match, 1 for no match, 2 if MSN could match if longer.
  1742. */
  1743. static int
  1744. isdn_tty_match_icall(char *cid, atemu *emu, int di)
  1745. {
  1746. #ifdef ISDN_DEBUG_MODEM_ICALL
  1747. printk(KERN_DEBUG "m_fi: msn=%s lmsn=%s mmsn=%s mreg[SI1]=%d mreg[SI2]=%d\n",
  1748. emu->msn, emu->lmsn, isdn_map_eaz2msn(emu->msn, di),
  1749. emu->mdmreg[REG_SI1], emu->mdmreg[REG_SI2]);
  1750. #endif
  1751. if (strlen(emu->lmsn)) {
  1752. char *p = emu->lmsn;
  1753. char *q;
  1754. int tmp;
  1755. int ret = 0;
  1756. while (1) {
  1757. if ((q = strchr(p, ';')))
  1758. *q = '\0';
  1759. if ((tmp = isdn_msncmp(cid, isdn_map_eaz2msn(p, di))) > ret)
  1760. ret = tmp;
  1761. #ifdef ISDN_DEBUG_MODEM_ICALL
  1762. printk(KERN_DEBUG "m_fi: lmsnX=%s mmsn=%s -> tmp=%d\n",
  1763. p, isdn_map_eaz2msn(emu->msn, di), tmp);
  1764. #endif
  1765. if (q) {
  1766. *q = ';';
  1767. p = q;
  1768. p++;
  1769. }
  1770. if (!tmp)
  1771. return 0;
  1772. if (!q)
  1773. break;
  1774. }
  1775. return ret;
  1776. } else {
  1777. int tmp;
  1778. tmp = isdn_msncmp(cid, isdn_map_eaz2msn(emu->msn, di));
  1779. #ifdef ISDN_DEBUG_MODEM_ICALL
  1780. printk(KERN_DEBUG "m_fi: mmsn=%s -> tmp=%d\n",
  1781. isdn_map_eaz2msn(emu->msn, di), tmp);
  1782. #endif
  1783. return tmp;
  1784. }
  1785. }
  1786. /*
  1787. * An incoming call-request has arrived.
  1788. * Search the tty-devices for an appropriate device and bind
  1789. * it to the ISDN-Channel.
  1790. * Return:
  1791. *
  1792. * 0 = No matching device found.
  1793. * 1 = A matching device found.
  1794. * 3 = No match found, but eventually would match, if
  1795. * CID is longer.
  1796. */
  1797. int
  1798. isdn_tty_find_icall(int di, int ch, setup_parm *setup)
  1799. {
  1800. char *eaz;
  1801. int i;
  1802. int wret;
  1803. int idx;
  1804. int si1;
  1805. int si2;
  1806. char *nr;
  1807. ulong flags;
  1808. if (!setup->phone[0]) {
  1809. nr = "0";
  1810. printk(KERN_INFO "isdn_tty: Incoming call without OAD, assuming '0'\n");
  1811. } else
  1812. nr = setup->phone;
  1813. si1 = (int) setup->si1;
  1814. si2 = (int) setup->si2;
  1815. if (!setup->eazmsn[0]) {
  1816. printk(KERN_WARNING "isdn_tty: Incoming call without CPN, assuming '0'\n");
  1817. eaz = "0";
  1818. } else
  1819. eaz = setup->eazmsn;
  1820. #ifdef ISDN_DEBUG_MODEM_ICALL
  1821. printk(KERN_DEBUG "m_fi: eaz=%s si1=%d si2=%d\n", eaz, si1, si2);
  1822. #endif
  1823. wret = 0;
  1824. spin_lock_irqsave(&dev->lock, flags);
  1825. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  1826. modem_info *info = &dev->mdm.info[i];
  1827. if (info->port.count == 0)
  1828. continue;
  1829. if ((info->emu.mdmreg[REG_SI1] & si2bit[si1]) && /* SI1 is matching */
  1830. (info->emu.mdmreg[REG_SI2] == si2)) { /* SI2 is matching */
  1831. idx = isdn_dc2minor(di, ch);
  1832. #ifdef ISDN_DEBUG_MODEM_ICALL
  1833. printk(KERN_DEBUG "m_fi: match1 wret=%d\n", wret);
  1834. printk(KERN_DEBUG "m_fi: idx=%d flags=%08lx drv=%d ch=%d usg=%d\n", idx,
  1835. info->port.flags, info->isdn_driver,
  1836. info->isdn_channel, dev->usage[idx]);
  1837. #endif
  1838. if (
  1839. #ifndef FIX_FILE_TRANSFER
  1840. tty_port_active(&info->port) &&
  1841. #endif
  1842. (info->isdn_driver == -1) &&
  1843. (info->isdn_channel == -1) &&
  1844. (USG_NONE(dev->usage[idx]))) {
  1845. int matchret;
  1846. if ((matchret = isdn_tty_match_icall(eaz, &info->emu, di)) > wret)
  1847. wret = matchret;
  1848. if (!matchret) { /* EAZ is matching */
  1849. info->isdn_driver = di;
  1850. info->isdn_channel = ch;
  1851. info->drv_index = idx;
  1852. dev->m_idx[idx] = info->line;
  1853. dev->usage[idx] &= ISDN_USAGE_EXCLUSIVE;
  1854. dev->usage[idx] |= isdn_calc_usage(si1, info->emu.mdmreg[REG_L2PROT]);
  1855. strcpy(dev->num[idx], nr);
  1856. strcpy(info->emu.cpn, eaz);
  1857. info->emu.mdmreg[REG_SI1I] = si2bit[si1];
  1858. info->emu.mdmreg[REG_PLAN] = setup->plan;
  1859. info->emu.mdmreg[REG_SCREEN] = setup->screen;
  1860. isdn_info_update();
  1861. spin_unlock_irqrestore(&dev->lock, flags);
  1862. printk(KERN_INFO "isdn_tty: call from %s, -> RING on ttyI%d\n", nr,
  1863. info->line);
  1864. info->msr |= UART_MSR_RI;
  1865. isdn_tty_modem_result(RESULT_RING, info);
  1866. isdn_timer_ctrl(ISDN_TIMER_MODEMRING, 1);
  1867. return 1;
  1868. }
  1869. }
  1870. }
  1871. }
  1872. spin_unlock_irqrestore(&dev->lock, flags);
  1873. printk(KERN_INFO "isdn_tty: call from %s -> %s %s\n", nr, eaz,
  1874. ((dev->drv[di]->flags & DRV_FLAG_REJBUS) && (wret != 2)) ? "rejected" : "ignored");
  1875. return (wret == 2) ? 3 : 0;
  1876. }
  1877. int
  1878. isdn_tty_stat_callback(int i, isdn_ctrl *c)
  1879. {
  1880. int mi;
  1881. modem_info *info;
  1882. char *e;
  1883. if (i < 0)
  1884. return 0;
  1885. if ((mi = dev->m_idx[i]) >= 0) {
  1886. info = &dev->mdm.info[mi];
  1887. switch (c->command) {
  1888. case ISDN_STAT_CINF:
  1889. printk(KERN_DEBUG "CHARGEINFO on ttyI%d: %ld %s\n", info->line, c->arg, c->parm.num);
  1890. info->emu.charge = (unsigned) simple_strtoul(c->parm.num, &e, 10);
  1891. if (e == (char *)c->parm.num)
  1892. info->emu.charge = 0;
  1893. break;
  1894. case ISDN_STAT_BSENT:
  1895. #ifdef ISDN_TTY_STAT_DEBUG
  1896. printk(KERN_DEBUG "tty_STAT_BSENT ttyI%d\n", info->line);
  1897. #endif
  1898. if ((info->isdn_driver == c->driver) &&
  1899. (info->isdn_channel == c->arg)) {
  1900. info->msr |= UART_MSR_CTS;
  1901. if (info->send_outstanding)
  1902. if (!(--info->send_outstanding))
  1903. info->lsr |= UART_LSR_TEMT;
  1904. isdn_tty_tint(info);
  1905. return 1;
  1906. }
  1907. break;
  1908. case ISDN_STAT_CAUSE:
  1909. #ifdef ISDN_TTY_STAT_DEBUG
  1910. printk(KERN_DEBUG "tty_STAT_CAUSE ttyI%d\n", info->line);
  1911. #endif
  1912. /* Signal cause to tty-device */
  1913. strncpy(info->last_cause, c->parm.num, 5);
  1914. return 1;
  1915. case ISDN_STAT_DISPLAY:
  1916. #ifdef ISDN_TTY_STAT_DEBUG
  1917. printk(KERN_DEBUG "tty_STAT_DISPLAY ttyI%d\n", info->line);
  1918. #endif
  1919. /* Signal display to tty-device */
  1920. if ((info->emu.mdmreg[REG_DISPLAY] & BIT_DISPLAY) &&
  1921. !(info->emu.mdmreg[REG_RESPNUM] & BIT_RESPNUM)) {
  1922. isdn_tty_at_cout("\r\n", info);
  1923. isdn_tty_at_cout("DISPLAY: ", info);
  1924. isdn_tty_at_cout(c->parm.display, info);
  1925. isdn_tty_at_cout("\r\n", info);
  1926. }
  1927. return 1;
  1928. case ISDN_STAT_DCONN:
  1929. #ifdef ISDN_TTY_STAT_DEBUG
  1930. printk(KERN_DEBUG "tty_STAT_DCONN ttyI%d\n", info->line);
  1931. #endif
  1932. if (tty_port_active(&info->port)) {
  1933. if (info->dialing == 1) {
  1934. info->dialing = 2;
  1935. return 1;
  1936. }
  1937. }
  1938. break;
  1939. case ISDN_STAT_DHUP:
  1940. #ifdef ISDN_TTY_STAT_DEBUG
  1941. printk(KERN_DEBUG "tty_STAT_DHUP ttyI%d\n", info->line);
  1942. #endif
  1943. if (tty_port_active(&info->port)) {
  1944. if (info->dialing == 1)
  1945. isdn_tty_modem_result(RESULT_BUSY, info);
  1946. if (info->dialing > 1)
  1947. isdn_tty_modem_result(RESULT_NO_CARRIER, info);
  1948. info->dialing = 0;
  1949. #ifdef ISDN_DEBUG_MODEM_HUP
  1950. printk(KERN_DEBUG "Mhup in ISDN_STAT_DHUP\n");
  1951. #endif
  1952. isdn_tty_modem_hup(info, 0);
  1953. return 1;
  1954. }
  1955. break;
  1956. case ISDN_STAT_BCONN:
  1957. #ifdef ISDN_TTY_STAT_DEBUG
  1958. printk(KERN_DEBUG "tty_STAT_BCONN ttyI%d\n", info->line);
  1959. #endif
  1960. /* Wake up any processes waiting
  1961. * for incoming call of this device when
  1962. * DCD follow the state of incoming carrier
  1963. */
  1964. if (info->port.blocked_open &&
  1965. (info->emu.mdmreg[REG_DCD] & BIT_DCD)) {
  1966. wake_up_interruptible(&info->port.open_wait);
  1967. }
  1968. /* Schedule CONNECT-Message to any tty
  1969. * waiting for it and
  1970. * set DCD-bit of its modem-status.
  1971. */
  1972. if (tty_port_active(&info->port) ||
  1973. (info->port.blocked_open &&
  1974. (info->emu.mdmreg[REG_DCD] & BIT_DCD))) {
  1975. info->msr |= UART_MSR_DCD;
  1976. info->emu.charge = 0;
  1977. if (info->dialing & 0xf)
  1978. info->last_dir = 1;
  1979. else
  1980. info->last_dir = 0;
  1981. info->dialing = 0;
  1982. info->rcvsched = 1;
  1983. if (USG_MODEM(dev->usage[i])) {
  1984. if (info->emu.mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) {
  1985. strcpy(info->emu.connmsg, c->parm.num);
  1986. isdn_tty_modem_result(RESULT_CONNECT, info);
  1987. } else
  1988. isdn_tty_modem_result(RESULT_CONNECT64000, info);
  1989. }
  1990. if (USG_VOICE(dev->usage[i]))
  1991. isdn_tty_modem_result(RESULT_VCON, info);
  1992. return 1;
  1993. }
  1994. break;
  1995. case ISDN_STAT_BHUP:
  1996. #ifdef ISDN_TTY_STAT_DEBUG
  1997. printk(KERN_DEBUG "tty_STAT_BHUP ttyI%d\n", info->line);
  1998. #endif
  1999. if (tty_port_active(&info->port)) {
  2000. #ifdef ISDN_DEBUG_MODEM_HUP
  2001. printk(KERN_DEBUG "Mhup in ISDN_STAT_BHUP\n");
  2002. #endif
  2003. isdn_tty_modem_hup(info, 0);
  2004. return 1;
  2005. }
  2006. break;
  2007. case ISDN_STAT_NODCH:
  2008. #ifdef ISDN_TTY_STAT_DEBUG
  2009. printk(KERN_DEBUG "tty_STAT_NODCH ttyI%d\n", info->line);
  2010. #endif
  2011. if (tty_port_active(&info->port)) {
  2012. if (info->dialing) {
  2013. info->dialing = 0;
  2014. info->last_l2 = -1;
  2015. info->last_si = 0;
  2016. sprintf(info->last_cause, "0000");
  2017. isdn_tty_modem_result(RESULT_NO_DIALTONE, info);
  2018. }
  2019. isdn_tty_modem_hup(info, 0);
  2020. return 1;
  2021. }
  2022. break;
  2023. case ISDN_STAT_UNLOAD:
  2024. #ifdef ISDN_TTY_STAT_DEBUG
  2025. printk(KERN_DEBUG "tty_STAT_UNLOAD ttyI%d\n", info->line);
  2026. #endif
  2027. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  2028. info = &dev->mdm.info[i];
  2029. if (info->isdn_driver == c->driver) {
  2030. if (info->online)
  2031. isdn_tty_modem_hup(info, 1);
  2032. }
  2033. }
  2034. return 1;
  2035. #ifdef CONFIG_ISDN_TTY_FAX
  2036. case ISDN_STAT_FAXIND:
  2037. if (tty_port_active(&info->port)) {
  2038. isdn_tty_fax_command(info, c);
  2039. }
  2040. break;
  2041. #endif
  2042. #ifdef CONFIG_ISDN_AUDIO
  2043. case ISDN_STAT_AUDIO:
  2044. if (tty_port_active(&info->port)) {
  2045. switch (c->parm.num[0]) {
  2046. case ISDN_AUDIO_DTMF:
  2047. if (info->vonline) {
  2048. isdn_audio_put_dle_code(info,
  2049. c->parm.num[1]);
  2050. }
  2051. break;
  2052. }
  2053. }
  2054. break;
  2055. #endif
  2056. }
  2057. }
  2058. return 0;
  2059. }
  2060. /*********************************************************************
  2061. Modem-Emulator-Routines
  2062. *********************************************************************/
  2063. #define cmdchar(c) ((c >= ' ') && (c <= 0x7f))
  2064. /*
  2065. * Put a message from the AT-emulator into receive-buffer of tty,
  2066. * convert CR, LF, and BS to values in modem-registers 3, 4 and 5.
  2067. */
  2068. void
  2069. isdn_tty_at_cout(char *msg, modem_info *info)
  2070. {
  2071. struct tty_port *port = &info->port;
  2072. atemu *m = &info->emu;
  2073. char *p;
  2074. char c;
  2075. u_long flags;
  2076. struct sk_buff *skb = NULL;
  2077. char *sp = NULL;
  2078. int l;
  2079. if (!msg) {
  2080. printk(KERN_WARNING "isdn_tty: Null-Message in isdn_tty_at_cout\n");
  2081. return;
  2082. }
  2083. l = strlen(msg);
  2084. spin_lock_irqsave(&info->readlock, flags);
  2085. if (info->closing) {
  2086. spin_unlock_irqrestore(&info->readlock, flags);
  2087. return;
  2088. }
  2089. /* use queue instead of direct, if online and */
  2090. /* data is in queue or buffer is full */
  2091. if (info->online && ((tty_buffer_request_room(port, l) < l) ||
  2092. !skb_queue_empty(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel]))) {
  2093. skb = alloc_skb(l, GFP_ATOMIC);
  2094. if (!skb) {
  2095. spin_unlock_irqrestore(&info->readlock, flags);
  2096. return;
  2097. }
  2098. sp = skb_put(skb, l);
  2099. #ifdef CONFIG_ISDN_AUDIO
  2100. ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
  2101. ISDN_AUDIO_SKB_LOCK(skb) = 0;
  2102. #endif
  2103. }
  2104. for (p = msg; *p; p++) {
  2105. switch (*p) {
  2106. case '\r':
  2107. c = m->mdmreg[REG_CR];
  2108. break;
  2109. case '\n':
  2110. c = m->mdmreg[REG_LF];
  2111. break;
  2112. case '\b':
  2113. c = m->mdmreg[REG_BS];
  2114. break;
  2115. default:
  2116. c = *p;
  2117. }
  2118. if (skb) {
  2119. *sp++ = c;
  2120. } else {
  2121. if (tty_insert_flip_char(port, c, TTY_NORMAL) == 0)
  2122. break;
  2123. }
  2124. }
  2125. if (skb) {
  2126. __skb_queue_tail(&dev->drv[info->isdn_driver]->rpqueue[info->isdn_channel], skb);
  2127. dev->drv[info->isdn_driver]->rcvcount[info->isdn_channel] += skb->len;
  2128. spin_unlock_irqrestore(&info->readlock, flags);
  2129. /* Schedule dequeuing */
  2130. if (dev->modempoll && info->rcvsched)
  2131. isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
  2132. } else {
  2133. spin_unlock_irqrestore(&info->readlock, flags);
  2134. tty_flip_buffer_push(port);
  2135. }
  2136. }
  2137. /*
  2138. * Perform ATH Hangup
  2139. */
  2140. static void
  2141. isdn_tty_on_hook(modem_info *info)
  2142. {
  2143. if (info->isdn_channel >= 0) {
  2144. #ifdef ISDN_DEBUG_MODEM_HUP
  2145. printk(KERN_DEBUG "Mhup in isdn_tty_on_hook\n");
  2146. #endif
  2147. isdn_tty_modem_hup(info, 1);
  2148. }
  2149. }
  2150. static void
  2151. isdn_tty_off_hook(void)
  2152. {
  2153. printk(KERN_DEBUG "isdn_tty_off_hook\n");
  2154. }
  2155. #define PLUSWAIT1 (HZ / 2) /* 0.5 sec. */
  2156. #define PLUSWAIT2 (HZ * 3 / 2) /* 1.5 sec */
  2157. /*
  2158. * Check Buffer for Modem-escape-sequence, activate timer-callback to
  2159. * isdn_tty_modem_escape() if sequence found.
  2160. *
  2161. * Parameters:
  2162. * p pointer to databuffer
  2163. * plus escape-character
  2164. * count length of buffer
  2165. * pluscount count of valid escape-characters so far
  2166. * lastplus timestamp of last character
  2167. */
  2168. static void
  2169. isdn_tty_check_esc(const u_char *p, u_char plus, int count, int *pluscount,
  2170. u_long *lastplus)
  2171. {
  2172. if (plus > 127)
  2173. return;
  2174. if (count > 3) {
  2175. p += count - 3;
  2176. count = 3;
  2177. *pluscount = 0;
  2178. }
  2179. while (count > 0) {
  2180. if (*(p++) == plus) {
  2181. if ((*pluscount)++) {
  2182. /* Time since last '+' > 0.5 sec. ? */
  2183. if (time_after(jiffies, *lastplus + PLUSWAIT1))
  2184. *pluscount = 1;
  2185. } else {
  2186. /* Time since last non-'+' < 1.5 sec. ? */
  2187. if (time_before(jiffies, *lastplus + PLUSWAIT2))
  2188. *pluscount = 0;
  2189. }
  2190. if ((*pluscount == 3) && (count == 1))
  2191. isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, 1);
  2192. if (*pluscount > 3)
  2193. *pluscount = 1;
  2194. } else
  2195. *pluscount = 0;
  2196. *lastplus = jiffies;
  2197. count--;
  2198. }
  2199. }
  2200. /*
  2201. * Return result of AT-emulator to tty-receive-buffer, depending on
  2202. * modem-register 12, bit 0 and 1.
  2203. * For CONNECT-messages also switch to online-mode.
  2204. * For RING-message handle auto-ATA if register 0 != 0
  2205. */
  2206. static void
  2207. isdn_tty_modem_result(int code, modem_info *info)
  2208. {
  2209. atemu *m = &info->emu;
  2210. static char *msg[] =
  2211. {"OK", "CONNECT", "RING", "NO CARRIER", "ERROR",
  2212. "CONNECT 64000", "NO DIALTONE", "BUSY", "NO ANSWER",
  2213. "RINGING", "NO MSN/EAZ", "VCON", "RUNG"};
  2214. char s[ISDN_MSNLEN + 10];
  2215. switch (code) {
  2216. case RESULT_RING:
  2217. m->mdmreg[REG_RINGCNT]++;
  2218. if (m->mdmreg[REG_RINGCNT] == m->mdmreg[REG_RINGATA])
  2219. /* Automatically accept incoming call */
  2220. isdn_tty_cmd_ATA(info);
  2221. break;
  2222. case RESULT_NO_CARRIER:
  2223. #ifdef ISDN_DEBUG_MODEM_HUP
  2224. printk(KERN_DEBUG "modem_result: NO CARRIER %d %d\n",
  2225. info->closing, !info->port.tty);
  2226. #endif
  2227. m->mdmreg[REG_RINGCNT] = 0;
  2228. del_timer(&info->nc_timer);
  2229. info->ncarrier = 0;
  2230. if (info->closing || !info->port.tty)
  2231. return;
  2232. #ifdef CONFIG_ISDN_AUDIO
  2233. if (info->vonline & 1) {
  2234. #ifdef ISDN_DEBUG_MODEM_VOICE
  2235. printk(KERN_DEBUG "res3: send DLE-ETX on ttyI%d\n",
  2236. info->line);
  2237. #endif
  2238. /* voice-recording, add DLE-ETX */
  2239. isdn_tty_at_cout("\020\003", info);
  2240. }
  2241. if (info->vonline & 2) {
  2242. #ifdef ISDN_DEBUG_MODEM_VOICE
  2243. printk(KERN_DEBUG "res3: send DLE-DC4 on ttyI%d\n",
  2244. info->line);
  2245. #endif
  2246. /* voice-playing, add DLE-DC4 */
  2247. isdn_tty_at_cout("\020\024", info);
  2248. }
  2249. #endif
  2250. break;
  2251. case RESULT_CONNECT:
  2252. case RESULT_CONNECT64000:
  2253. sprintf(info->last_cause, "0000");
  2254. if (!info->online)
  2255. info->online = 2;
  2256. break;
  2257. case RESULT_VCON:
  2258. #ifdef ISDN_DEBUG_MODEM_VOICE
  2259. printk(KERN_DEBUG "res3: send VCON on ttyI%d\n",
  2260. info->line);
  2261. #endif
  2262. sprintf(info->last_cause, "0000");
  2263. if (!info->online)
  2264. info->online = 1;
  2265. break;
  2266. } /* switch (code) */
  2267. if (m->mdmreg[REG_RESP] & BIT_RESP) {
  2268. /* Show results */
  2269. if (m->mdmreg[REG_RESPNUM] & BIT_RESPNUM) {
  2270. /* Show numeric results only */
  2271. sprintf(s, "\r\n%d\r\n", code);
  2272. isdn_tty_at_cout(s, info);
  2273. } else {
  2274. if (code == RESULT_RING) {
  2275. /* return if "show RUNG" and ringcounter>1 */
  2276. if ((m->mdmreg[REG_RUNG] & BIT_RUNG) &&
  2277. (m->mdmreg[REG_RINGCNT] > 1))
  2278. return;
  2279. /* print CID, _before_ _every_ ring */
  2280. if (!(m->mdmreg[REG_CIDONCE] & BIT_CIDONCE)) {
  2281. isdn_tty_at_cout("\r\nCALLER NUMBER: ", info);
  2282. isdn_tty_at_cout(dev->num[info->drv_index], info);
  2283. if (m->mdmreg[REG_CDN] & BIT_CDN) {
  2284. isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
  2285. isdn_tty_at_cout(info->emu.cpn, info);
  2286. }
  2287. }
  2288. }
  2289. isdn_tty_at_cout("\r\n", info);
  2290. isdn_tty_at_cout(msg[code], info);
  2291. switch (code) {
  2292. case RESULT_CONNECT:
  2293. switch (m->mdmreg[REG_L2PROT]) {
  2294. case ISDN_PROTO_L2_MODEM:
  2295. isdn_tty_at_cout(" ", info);
  2296. isdn_tty_at_cout(m->connmsg, info);
  2297. break;
  2298. }
  2299. break;
  2300. case RESULT_RING:
  2301. /* Append CPN, if enabled */
  2302. if ((m->mdmreg[REG_CPN] & BIT_CPN)) {
  2303. sprintf(s, "/%s", m->cpn);
  2304. isdn_tty_at_cout(s, info);
  2305. }
  2306. /* Print CID only once, _after_ 1st RING */
  2307. if ((m->mdmreg[REG_CIDONCE] & BIT_CIDONCE) &&
  2308. (m->mdmreg[REG_RINGCNT] == 1)) {
  2309. isdn_tty_at_cout("\r\n", info);
  2310. isdn_tty_at_cout("CALLER NUMBER: ", info);
  2311. isdn_tty_at_cout(dev->num[info->drv_index], info);
  2312. if (m->mdmreg[REG_CDN] & BIT_CDN) {
  2313. isdn_tty_at_cout("\r\nCALLED NUMBER: ", info);
  2314. isdn_tty_at_cout(info->emu.cpn, info);
  2315. }
  2316. }
  2317. break;
  2318. case RESULT_NO_CARRIER:
  2319. case RESULT_NO_DIALTONE:
  2320. case RESULT_BUSY:
  2321. case RESULT_NO_ANSWER:
  2322. m->mdmreg[REG_RINGCNT] = 0;
  2323. /* Append Cause-Message if enabled */
  2324. if (m->mdmreg[REG_RESPXT] & BIT_RESPXT) {
  2325. sprintf(s, "/%s", info->last_cause);
  2326. isdn_tty_at_cout(s, info);
  2327. }
  2328. break;
  2329. case RESULT_CONNECT64000:
  2330. /* Append Protocol to CONNECT message */
  2331. switch (m->mdmreg[REG_L2PROT]) {
  2332. case ISDN_PROTO_L2_X75I:
  2333. case ISDN_PROTO_L2_X75UI:
  2334. case ISDN_PROTO_L2_X75BUI:
  2335. isdn_tty_at_cout("/X.75", info);
  2336. break;
  2337. case ISDN_PROTO_L2_HDLC:
  2338. isdn_tty_at_cout("/HDLC", info);
  2339. break;
  2340. case ISDN_PROTO_L2_V11096:
  2341. isdn_tty_at_cout("/V110/9600", info);
  2342. break;
  2343. case ISDN_PROTO_L2_V11019:
  2344. isdn_tty_at_cout("/V110/19200", info);
  2345. break;
  2346. case ISDN_PROTO_L2_V11038:
  2347. isdn_tty_at_cout("/V110/38400", info);
  2348. break;
  2349. }
  2350. if (m->mdmreg[REG_T70] & BIT_T70) {
  2351. isdn_tty_at_cout("/T.70", info);
  2352. if (m->mdmreg[REG_T70] & BIT_T70_EXT)
  2353. isdn_tty_at_cout("+", info);
  2354. }
  2355. break;
  2356. }
  2357. isdn_tty_at_cout("\r\n", info);
  2358. }
  2359. }
  2360. if (code == RESULT_NO_CARRIER) {
  2361. if (info->closing || (!info->port.tty))
  2362. return;
  2363. if (tty_port_check_carrier(&info->port))
  2364. tty_hangup(info->port.tty);
  2365. }
  2366. }
  2367. /*
  2368. * Display a modem-register-value.
  2369. */
  2370. static void
  2371. isdn_tty_show_profile(int ridx, modem_info *info)
  2372. {
  2373. char v[6];
  2374. sprintf(v, "\r\n%d", info->emu.mdmreg[ridx]);
  2375. isdn_tty_at_cout(v, info);
  2376. }
  2377. /*
  2378. * Get MSN-string from char-pointer, set pointer to end of number
  2379. */
  2380. static void
  2381. isdn_tty_get_msnstr(char *n, char **p)
  2382. {
  2383. int limit = ISDN_MSNLEN - 1;
  2384. while (((*p[0] >= '0' && *p[0] <= '9') ||
  2385. /* Why a comma ??? */
  2386. (*p[0] == ',') || (*p[0] == ':')) &&
  2387. (limit--))
  2388. *n++ = *p[0]++;
  2389. *n = '\0';
  2390. }
  2391. /*
  2392. * Get phone-number from modem-commandbuffer
  2393. */
  2394. static void
  2395. isdn_tty_getdial(char *p, char *q, int cnt)
  2396. {
  2397. int first = 1;
  2398. int limit = ISDN_MSNLEN - 1; /* MUST match the size of interface var to avoid
  2399. buffer overflow */
  2400. while (strchr(" 0123456789,#.*WPTSR-", *p) && *p && --cnt > 0) {
  2401. if ((*p >= '0' && *p <= '9') || ((*p == 'S') && first) ||
  2402. ((*p == 'R') && first) ||
  2403. (*p == '*') || (*p == '#')) {
  2404. *q++ = *p;
  2405. limit--;
  2406. }
  2407. if (!limit)
  2408. break;
  2409. p++;
  2410. first = 0;
  2411. }
  2412. *q = 0;
  2413. }
  2414. #define PARSE_ERROR { isdn_tty_modem_result(RESULT_ERROR, info); return; }
  2415. #define PARSE_ERROR1 { isdn_tty_modem_result(RESULT_ERROR, info); return 1; }
  2416. static void
  2417. isdn_tty_report(modem_info *info)
  2418. {
  2419. atemu *m = &info->emu;
  2420. char s[80];
  2421. isdn_tty_at_cout("\r\nStatistics of last connection:\r\n\r\n", info);
  2422. sprintf(s, " Remote Number: %s\r\n", info->last_num);
  2423. isdn_tty_at_cout(s, info);
  2424. sprintf(s, " Direction: %s\r\n", info->last_dir ? "outgoing" : "incoming");
  2425. isdn_tty_at_cout(s, info);
  2426. isdn_tty_at_cout(" Layer-2 Protocol: ", info);
  2427. switch (info->last_l2) {
  2428. case ISDN_PROTO_L2_X75I:
  2429. isdn_tty_at_cout("X.75i", info);
  2430. break;
  2431. case ISDN_PROTO_L2_X75UI:
  2432. isdn_tty_at_cout("X.75ui", info);
  2433. break;
  2434. case ISDN_PROTO_L2_X75BUI:
  2435. isdn_tty_at_cout("X.75bui", info);
  2436. break;
  2437. case ISDN_PROTO_L2_HDLC:
  2438. isdn_tty_at_cout("HDLC", info);
  2439. break;
  2440. case ISDN_PROTO_L2_V11096:
  2441. isdn_tty_at_cout("V.110 9600 Baud", info);
  2442. break;
  2443. case ISDN_PROTO_L2_V11019:
  2444. isdn_tty_at_cout("V.110 19200 Baud", info);
  2445. break;
  2446. case ISDN_PROTO_L2_V11038:
  2447. isdn_tty_at_cout("V.110 38400 Baud", info);
  2448. break;
  2449. case ISDN_PROTO_L2_TRANS:
  2450. isdn_tty_at_cout("transparent", info);
  2451. break;
  2452. case ISDN_PROTO_L2_MODEM:
  2453. isdn_tty_at_cout("modem", info);
  2454. break;
  2455. case ISDN_PROTO_L2_FAX:
  2456. isdn_tty_at_cout("fax", info);
  2457. break;
  2458. default:
  2459. isdn_tty_at_cout("unknown", info);
  2460. break;
  2461. }
  2462. if (m->mdmreg[REG_T70] & BIT_T70) {
  2463. isdn_tty_at_cout("/T.70", info);
  2464. if (m->mdmreg[REG_T70] & BIT_T70_EXT)
  2465. isdn_tty_at_cout("+", info);
  2466. }
  2467. isdn_tty_at_cout("\r\n", info);
  2468. isdn_tty_at_cout(" Service: ", info);
  2469. switch (info->last_si) {
  2470. case 1:
  2471. isdn_tty_at_cout("audio\r\n", info);
  2472. break;
  2473. case 5:
  2474. isdn_tty_at_cout("btx\r\n", info);
  2475. break;
  2476. case 7:
  2477. isdn_tty_at_cout("data\r\n", info);
  2478. break;
  2479. default:
  2480. sprintf(s, "%d\r\n", info->last_si);
  2481. isdn_tty_at_cout(s, info);
  2482. break;
  2483. }
  2484. sprintf(s, " Hangup location: %s\r\n", info->last_lhup ? "local" : "remote");
  2485. isdn_tty_at_cout(s, info);
  2486. sprintf(s, " Last cause: %s\r\n", info->last_cause);
  2487. isdn_tty_at_cout(s, info);
  2488. }
  2489. /*
  2490. * Parse AT&.. commands.
  2491. */
  2492. static int
  2493. isdn_tty_cmd_ATand(char **p, modem_info *info)
  2494. {
  2495. atemu *m = &info->emu;
  2496. int i;
  2497. char rb[100];
  2498. #define MAXRB (sizeof(rb) - 1)
  2499. switch (*p[0]) {
  2500. case 'B':
  2501. /* &B - Set Buffersize */
  2502. p[0]++;
  2503. i = isdn_getnum(p);
  2504. if ((i < 0) || (i > ISDN_SERIAL_XMIT_MAX))
  2505. PARSE_ERROR1;
  2506. #ifdef CONFIG_ISDN_AUDIO
  2507. if ((m->mdmreg[REG_SI1] & 1) && (i > VBUF))
  2508. PARSE_ERROR1;
  2509. #endif
  2510. m->mdmreg[REG_PSIZE] = i / 16;
  2511. info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
  2512. switch (m->mdmreg[REG_L2PROT]) {
  2513. case ISDN_PROTO_L2_V11096:
  2514. case ISDN_PROTO_L2_V11019:
  2515. case ISDN_PROTO_L2_V11038:
  2516. info->xmit_size /= 10;
  2517. }
  2518. break;
  2519. case 'C':
  2520. /* &C - DCD Status */
  2521. p[0]++;
  2522. switch (isdn_getnum(p)) {
  2523. case 0:
  2524. m->mdmreg[REG_DCD] &= ~BIT_DCD;
  2525. break;
  2526. case 1:
  2527. m->mdmreg[REG_DCD] |= BIT_DCD;
  2528. break;
  2529. default:
  2530. PARSE_ERROR1
  2531. }
  2532. break;
  2533. case 'D':
  2534. /* &D - Set DTR-Low-behavior */
  2535. p[0]++;
  2536. switch (isdn_getnum(p)) {
  2537. case 0:
  2538. m->mdmreg[REG_DTRHUP] &= ~BIT_DTRHUP;
  2539. m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
  2540. break;
  2541. case 2:
  2542. m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
  2543. m->mdmreg[REG_DTRR] &= ~BIT_DTRR;
  2544. break;
  2545. case 3:
  2546. m->mdmreg[REG_DTRHUP] |= BIT_DTRHUP;
  2547. m->mdmreg[REG_DTRR] |= BIT_DTRR;
  2548. break;
  2549. default:
  2550. PARSE_ERROR1
  2551. }
  2552. break;
  2553. case 'E':
  2554. /* &E -Set EAZ/MSN */
  2555. p[0]++;
  2556. isdn_tty_get_msnstr(m->msn, p);
  2557. break;
  2558. case 'F':
  2559. /* &F -Set Factory-Defaults */
  2560. p[0]++;
  2561. if (info->msr & UART_MSR_DCD)
  2562. PARSE_ERROR1;
  2563. isdn_tty_reset_profile(m);
  2564. isdn_tty_modem_reset_regs(info, 1);
  2565. break;
  2566. #ifdef DUMMY_HAYES_AT
  2567. case 'K':
  2568. /* only for be compilant with common scripts */
  2569. /* &K Flowcontrol - no function */
  2570. p[0]++;
  2571. isdn_getnum(p);
  2572. break;
  2573. #endif
  2574. case 'L':
  2575. /* &L -Set Numbers to listen on */
  2576. p[0]++;
  2577. i = 0;
  2578. while (*p[0] && (strchr("0123456789,-*[]?;", *p[0])) &&
  2579. (i < ISDN_LMSNLEN - 1))
  2580. m->lmsn[i++] = *p[0]++;
  2581. m->lmsn[i] = '\0';
  2582. break;
  2583. case 'R':
  2584. /* &R - Set V.110 bitrate adaption */
  2585. p[0]++;
  2586. i = isdn_getnum(p);
  2587. switch (i) {
  2588. case 0:
  2589. /* Switch off V.110, back to X.75 */
  2590. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
  2591. m->mdmreg[REG_SI2] = 0;
  2592. info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
  2593. break;
  2594. case 9600:
  2595. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11096;
  2596. m->mdmreg[REG_SI2] = 197;
  2597. info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
  2598. break;
  2599. case 19200:
  2600. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11019;
  2601. m->mdmreg[REG_SI2] = 199;
  2602. info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
  2603. break;
  2604. case 38400:
  2605. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_V11038;
  2606. m->mdmreg[REG_SI2] = 198; /* no existing standard for this */
  2607. info->xmit_size = m->mdmreg[REG_PSIZE] * 16 / 10;
  2608. break;
  2609. default:
  2610. PARSE_ERROR1;
  2611. }
  2612. /* Switch off T.70 */
  2613. m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
  2614. /* Set Service 7 */
  2615. m->mdmreg[REG_SI1] |= 4;
  2616. break;
  2617. case 'S':
  2618. /* &S - Set Windowsize */
  2619. p[0]++;
  2620. i = isdn_getnum(p);
  2621. if ((i > 0) && (i < 9))
  2622. m->mdmreg[REG_WSIZE] = i;
  2623. else
  2624. PARSE_ERROR1;
  2625. break;
  2626. case 'V':
  2627. /* &V - Show registers */
  2628. p[0]++;
  2629. isdn_tty_at_cout("\r\n", info);
  2630. for (i = 0; i < ISDN_MODEM_NUMREG; i++) {
  2631. sprintf(rb, "S%02d=%03d%s", i,
  2632. m->mdmreg[i], ((i + 1) % 10) ? " " : "\r\n");
  2633. isdn_tty_at_cout(rb, info);
  2634. }
  2635. sprintf(rb, "\r\nEAZ/MSN: %.50s\r\n",
  2636. strlen(m->msn) ? m->msn : "None");
  2637. isdn_tty_at_cout(rb, info);
  2638. if (strlen(m->lmsn)) {
  2639. isdn_tty_at_cout("\r\nListen: ", info);
  2640. isdn_tty_at_cout(m->lmsn, info);
  2641. isdn_tty_at_cout("\r\n", info);
  2642. }
  2643. break;
  2644. case 'W':
  2645. /* &W - Write Profile */
  2646. p[0]++;
  2647. switch (*p[0]) {
  2648. case '0':
  2649. p[0]++;
  2650. modem_write_profile(m);
  2651. break;
  2652. default:
  2653. PARSE_ERROR1;
  2654. }
  2655. break;
  2656. case 'X':
  2657. /* &X - Switch to BTX-Mode and T.70 */
  2658. p[0]++;
  2659. switch (isdn_getnum(p)) {
  2660. case 0:
  2661. m->mdmreg[REG_T70] &= ~(BIT_T70 | BIT_T70_EXT);
  2662. info->xmit_size = m->mdmreg[REG_PSIZE] * 16;
  2663. break;
  2664. case 1:
  2665. m->mdmreg[REG_T70] |= BIT_T70;
  2666. m->mdmreg[REG_T70] &= ~BIT_T70_EXT;
  2667. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
  2668. info->xmit_size = 112;
  2669. m->mdmreg[REG_SI1] = 4;
  2670. m->mdmreg[REG_SI2] = 0;
  2671. break;
  2672. case 2:
  2673. m->mdmreg[REG_T70] |= (BIT_T70 | BIT_T70_EXT);
  2674. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
  2675. info->xmit_size = 112;
  2676. m->mdmreg[REG_SI1] = 4;
  2677. m->mdmreg[REG_SI2] = 0;
  2678. break;
  2679. default:
  2680. PARSE_ERROR1;
  2681. }
  2682. break;
  2683. default:
  2684. PARSE_ERROR1;
  2685. }
  2686. return 0;
  2687. }
  2688. static int
  2689. isdn_tty_check_ats(int mreg, int mval, modem_info *info, atemu *m)
  2690. {
  2691. /* Some plausibility checks */
  2692. switch (mreg) {
  2693. case REG_L2PROT:
  2694. if (mval > ISDN_PROTO_L2_MAX)
  2695. return 1;
  2696. break;
  2697. case REG_PSIZE:
  2698. if ((mval * 16) > ISDN_SERIAL_XMIT_MAX)
  2699. return 1;
  2700. #ifdef CONFIG_ISDN_AUDIO
  2701. if ((m->mdmreg[REG_SI1] & 1) && (mval > VBUFX))
  2702. return 1;
  2703. #endif
  2704. info->xmit_size = mval * 16;
  2705. switch (m->mdmreg[REG_L2PROT]) {
  2706. case ISDN_PROTO_L2_V11096:
  2707. case ISDN_PROTO_L2_V11019:
  2708. case ISDN_PROTO_L2_V11038:
  2709. info->xmit_size /= 10;
  2710. }
  2711. break;
  2712. case REG_SI1I:
  2713. case REG_PLAN:
  2714. case REG_SCREEN:
  2715. /* readonly registers */
  2716. return 1;
  2717. }
  2718. return 0;
  2719. }
  2720. /*
  2721. * Perform ATS command
  2722. */
  2723. static int
  2724. isdn_tty_cmd_ATS(char **p, modem_info *info)
  2725. {
  2726. atemu *m = &info->emu;
  2727. int bitpos;
  2728. int mreg;
  2729. int mval;
  2730. int bval;
  2731. mreg = isdn_getnum(p);
  2732. if (mreg < 0 || mreg >= ISDN_MODEM_NUMREG)
  2733. PARSE_ERROR1;
  2734. switch (*p[0]) {
  2735. case '=':
  2736. p[0]++;
  2737. mval = isdn_getnum(p);
  2738. if (mval < 0 || mval > 255)
  2739. PARSE_ERROR1;
  2740. if (isdn_tty_check_ats(mreg, mval, info, m))
  2741. PARSE_ERROR1;
  2742. m->mdmreg[mreg] = mval;
  2743. break;
  2744. case '.':
  2745. /* Set/Clear a single bit */
  2746. p[0]++;
  2747. bitpos = isdn_getnum(p);
  2748. if ((bitpos < 0) || (bitpos > 7))
  2749. PARSE_ERROR1;
  2750. switch (*p[0]) {
  2751. case '=':
  2752. p[0]++;
  2753. bval = isdn_getnum(p);
  2754. if (bval < 0 || bval > 1)
  2755. PARSE_ERROR1;
  2756. if (bval)
  2757. mval = m->mdmreg[mreg] | (1 << bitpos);
  2758. else
  2759. mval = m->mdmreg[mreg] & ~(1 << bitpos);
  2760. if (isdn_tty_check_ats(mreg, mval, info, m))
  2761. PARSE_ERROR1;
  2762. m->mdmreg[mreg] = mval;
  2763. break;
  2764. case '?':
  2765. p[0]++;
  2766. isdn_tty_at_cout("\r\n", info);
  2767. isdn_tty_at_cout((m->mdmreg[mreg] & (1 << bitpos)) ? "1" : "0",
  2768. info);
  2769. break;
  2770. default:
  2771. PARSE_ERROR1;
  2772. }
  2773. break;
  2774. case '?':
  2775. p[0]++;
  2776. isdn_tty_show_profile(mreg, info);
  2777. break;
  2778. default:
  2779. PARSE_ERROR1;
  2780. break;
  2781. }
  2782. return 0;
  2783. }
  2784. /*
  2785. * Perform ATA command
  2786. */
  2787. static void
  2788. isdn_tty_cmd_ATA(modem_info *info)
  2789. {
  2790. atemu *m = &info->emu;
  2791. isdn_ctrl cmd;
  2792. int l2;
  2793. if (info->msr & UART_MSR_RI) {
  2794. /* Accept incoming call */
  2795. info->last_dir = 0;
  2796. strcpy(info->last_num, dev->num[info->drv_index]);
  2797. m->mdmreg[REG_RINGCNT] = 0;
  2798. info->msr &= ~UART_MSR_RI;
  2799. l2 = m->mdmreg[REG_L2PROT];
  2800. #ifdef CONFIG_ISDN_AUDIO
  2801. /* If more than one bit set in reg18, autoselect Layer2 */
  2802. if ((m->mdmreg[REG_SI1] & m->mdmreg[REG_SI1I]) != m->mdmreg[REG_SI1]) {
  2803. if (m->mdmreg[REG_SI1I] == 1) {
  2804. if ((l2 != ISDN_PROTO_L2_MODEM) && (l2 != ISDN_PROTO_L2_FAX))
  2805. l2 = ISDN_PROTO_L2_TRANS;
  2806. } else
  2807. l2 = ISDN_PROTO_L2_X75I;
  2808. }
  2809. #endif
  2810. cmd.driver = info->isdn_driver;
  2811. cmd.command = ISDN_CMD_SETL2;
  2812. cmd.arg = info->isdn_channel + (l2 << 8);
  2813. info->last_l2 = l2;
  2814. isdn_command(&cmd);
  2815. cmd.driver = info->isdn_driver;
  2816. cmd.command = ISDN_CMD_SETL3;
  2817. cmd.arg = info->isdn_channel + (m->mdmreg[REG_L3PROT] << 8);
  2818. #ifdef CONFIG_ISDN_TTY_FAX
  2819. if (l2 == ISDN_PROTO_L2_FAX) {
  2820. cmd.parm.fax = info->fax;
  2821. info->fax->direction = ISDN_TTY_FAX_CONN_IN;
  2822. }
  2823. #endif
  2824. isdn_command(&cmd);
  2825. cmd.driver = info->isdn_driver;
  2826. cmd.arg = info->isdn_channel;
  2827. cmd.command = ISDN_CMD_ACCEPTD;
  2828. info->dialing = 16;
  2829. info->emu.carrierwait = 0;
  2830. isdn_command(&cmd);
  2831. isdn_timer_ctrl(ISDN_TIMER_CARRIER, 1);
  2832. } else
  2833. isdn_tty_modem_result(RESULT_NO_ANSWER, info);
  2834. }
  2835. #ifdef CONFIG_ISDN_AUDIO
  2836. /*
  2837. * Parse AT+F.. commands
  2838. */
  2839. static int
  2840. isdn_tty_cmd_PLUSF(char **p, modem_info *info)
  2841. {
  2842. atemu *m = &info->emu;
  2843. char rs[20];
  2844. if (!strncmp(p[0], "CLASS", 5)) {
  2845. p[0] += 5;
  2846. switch (*p[0]) {
  2847. case '?':
  2848. p[0]++;
  2849. sprintf(rs, "\r\n%d",
  2850. (m->mdmreg[REG_SI1] & 1) ? 8 : 0);
  2851. #ifdef CONFIG_ISDN_TTY_FAX
  2852. if (TTY_IS_FCLASS2(info))
  2853. sprintf(rs, "\r\n2");
  2854. else if (TTY_IS_FCLASS1(info))
  2855. sprintf(rs, "\r\n1");
  2856. #endif
  2857. isdn_tty_at_cout(rs, info);
  2858. break;
  2859. case '=':
  2860. p[0]++;
  2861. switch (*p[0]) {
  2862. case '0':
  2863. p[0]++;
  2864. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
  2865. m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
  2866. m->mdmreg[REG_SI1] = 4;
  2867. info->xmit_size =
  2868. m->mdmreg[REG_PSIZE] * 16;
  2869. break;
  2870. #ifdef CONFIG_ISDN_TTY_FAX
  2871. case '1':
  2872. p[0]++;
  2873. if (!(dev->global_features &
  2874. ISDN_FEATURE_L3_FCLASS1))
  2875. PARSE_ERROR1;
  2876. m->mdmreg[REG_SI1] = 1;
  2877. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
  2878. m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS1;
  2879. info->xmit_size =
  2880. m->mdmreg[REG_PSIZE] * 16;
  2881. break;
  2882. case '2':
  2883. p[0]++;
  2884. if (!(dev->global_features &
  2885. ISDN_FEATURE_L3_FCLASS2))
  2886. PARSE_ERROR1;
  2887. m->mdmreg[REG_SI1] = 1;
  2888. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_FAX;
  2889. m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_FCLASS2;
  2890. info->xmit_size =
  2891. m->mdmreg[REG_PSIZE] * 16;
  2892. break;
  2893. #endif
  2894. case '8':
  2895. p[0]++;
  2896. /* L2 will change on dialout with si=1 */
  2897. m->mdmreg[REG_L2PROT] = ISDN_PROTO_L2_X75I;
  2898. m->mdmreg[REG_L3PROT] = ISDN_PROTO_L3_TRANS;
  2899. m->mdmreg[REG_SI1] = 5;
  2900. info->xmit_size = VBUF;
  2901. break;
  2902. case '?':
  2903. p[0]++;
  2904. strcpy(rs, "\r\n0,");
  2905. #ifdef CONFIG_ISDN_TTY_FAX
  2906. if (dev->global_features &
  2907. ISDN_FEATURE_L3_FCLASS1)
  2908. strcat(rs, "1,");
  2909. if (dev->global_features &
  2910. ISDN_FEATURE_L3_FCLASS2)
  2911. strcat(rs, "2,");
  2912. #endif
  2913. strcat(rs, "8");
  2914. isdn_tty_at_cout(rs, info);
  2915. break;
  2916. default:
  2917. PARSE_ERROR1;
  2918. }
  2919. break;
  2920. default:
  2921. PARSE_ERROR1;
  2922. }
  2923. return 0;
  2924. }
  2925. #ifdef CONFIG_ISDN_TTY_FAX
  2926. return (isdn_tty_cmd_PLUSF_FAX(p, info));
  2927. #else
  2928. PARSE_ERROR1;
  2929. #endif
  2930. }
  2931. /*
  2932. * Parse AT+V.. commands
  2933. */
  2934. static int
  2935. isdn_tty_cmd_PLUSV(char **p, modem_info *info)
  2936. {
  2937. atemu *m = &info->emu;
  2938. isdn_ctrl cmd;
  2939. static char *vcmd[] =
  2940. {"NH", "IP", "LS", "RX", "SD", "SM", "TX", "DD", NULL};
  2941. int i;
  2942. int par1;
  2943. int par2;
  2944. char rs[20];
  2945. i = 0;
  2946. while (vcmd[i]) {
  2947. if (!strncmp(vcmd[i], p[0], 2)) {
  2948. p[0] += 2;
  2949. break;
  2950. }
  2951. i++;
  2952. }
  2953. switch (i) {
  2954. case 0:
  2955. /* AT+VNH - Auto hangup feature */
  2956. switch (*p[0]) {
  2957. case '?':
  2958. p[0]++;
  2959. isdn_tty_at_cout("\r\n1", info);
  2960. break;
  2961. case '=':
  2962. p[0]++;
  2963. switch (*p[0]) {
  2964. case '1':
  2965. p[0]++;
  2966. break;
  2967. case '?':
  2968. p[0]++;
  2969. isdn_tty_at_cout("\r\n1", info);
  2970. break;
  2971. default:
  2972. PARSE_ERROR1;
  2973. }
  2974. break;
  2975. default:
  2976. PARSE_ERROR1;
  2977. }
  2978. break;
  2979. case 1:
  2980. /* AT+VIP - Reset all voice parameters */
  2981. isdn_tty_modem_reset_vpar(m);
  2982. break;
  2983. case 2:
  2984. /* AT+VLS - Select device, accept incoming call */
  2985. switch (*p[0]) {
  2986. case '?':
  2987. p[0]++;
  2988. sprintf(rs, "\r\n%d", m->vpar[0]);
  2989. isdn_tty_at_cout(rs, info);
  2990. break;
  2991. case '=':
  2992. p[0]++;
  2993. switch (*p[0]) {
  2994. case '0':
  2995. p[0]++;
  2996. m->vpar[0] = 0;
  2997. break;
  2998. case '2':
  2999. p[0]++;
  3000. m->vpar[0] = 2;
  3001. break;
  3002. case '?':
  3003. p[0]++;
  3004. isdn_tty_at_cout("\r\n0,2", info);
  3005. break;
  3006. default:
  3007. PARSE_ERROR1;
  3008. }
  3009. break;
  3010. default:
  3011. PARSE_ERROR1;
  3012. }
  3013. break;
  3014. case 3:
  3015. /* AT+VRX - Start recording */
  3016. if (!m->vpar[0])
  3017. PARSE_ERROR1;
  3018. if (info->online != 1) {
  3019. isdn_tty_modem_result(RESULT_NO_ANSWER, info);
  3020. return 1;
  3021. }
  3022. info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
  3023. if (!info->dtmf_state) {
  3024. printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
  3025. PARSE_ERROR1;
  3026. }
  3027. info->silence_state = isdn_audio_silence_init(info->silence_state);
  3028. if (!info->silence_state) {
  3029. printk(KERN_WARNING "isdn_tty: Couldn't malloc silence state\n");
  3030. PARSE_ERROR1;
  3031. }
  3032. if (m->vpar[3] < 5) {
  3033. info->adpcmr = isdn_audio_adpcm_init(info->adpcmr, m->vpar[3]);
  3034. if (!info->adpcmr) {
  3035. printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
  3036. PARSE_ERROR1;
  3037. }
  3038. }
  3039. #ifdef ISDN_DEBUG_AT
  3040. printk(KERN_DEBUG "AT: +VRX\n");
  3041. #endif
  3042. info->vonline |= 1;
  3043. isdn_tty_modem_result(RESULT_CONNECT, info);
  3044. return 0;
  3045. break;
  3046. case 4:
  3047. /* AT+VSD - Silence detection */
  3048. switch (*p[0]) {
  3049. case '?':
  3050. p[0]++;
  3051. sprintf(rs, "\r\n<%d>,<%d>",
  3052. m->vpar[1],
  3053. m->vpar[2]);
  3054. isdn_tty_at_cout(rs, info);
  3055. break;
  3056. case '=':
  3057. p[0]++;
  3058. if ((*p[0] >= '0') && (*p[0] <= '9')) {
  3059. par1 = isdn_getnum(p);
  3060. if ((par1 < 0) || (par1 > 31))
  3061. PARSE_ERROR1;
  3062. if (*p[0] != ',')
  3063. PARSE_ERROR1;
  3064. p[0]++;
  3065. par2 = isdn_getnum(p);
  3066. if ((par2 < 0) || (par2 > 255))
  3067. PARSE_ERROR1;
  3068. m->vpar[1] = par1;
  3069. m->vpar[2] = par2;
  3070. break;
  3071. } else
  3072. if (*p[0] == '?') {
  3073. p[0]++;
  3074. isdn_tty_at_cout("\r\n<0-31>,<0-255>",
  3075. info);
  3076. break;
  3077. } else
  3078. PARSE_ERROR1;
  3079. break;
  3080. default:
  3081. PARSE_ERROR1;
  3082. }
  3083. break;
  3084. case 5:
  3085. /* AT+VSM - Select compression */
  3086. switch (*p[0]) {
  3087. case '?':
  3088. p[0]++;
  3089. sprintf(rs, "\r\n<%d>,<%d><8000>",
  3090. m->vpar[3],
  3091. m->vpar[1]);
  3092. isdn_tty_at_cout(rs, info);
  3093. break;
  3094. case '=':
  3095. p[0]++;
  3096. switch (*p[0]) {
  3097. case '2':
  3098. case '3':
  3099. case '4':
  3100. case '5':
  3101. case '6':
  3102. par1 = isdn_getnum(p);
  3103. if ((par1 < 2) || (par1 > 6))
  3104. PARSE_ERROR1;
  3105. m->vpar[3] = par1;
  3106. break;
  3107. case '?':
  3108. p[0]++;
  3109. isdn_tty_at_cout("\r\n2;ADPCM;2;0;(8000)\r\n",
  3110. info);
  3111. isdn_tty_at_cout("3;ADPCM;3;0;(8000)\r\n",
  3112. info);
  3113. isdn_tty_at_cout("4;ADPCM;4;0;(8000)\r\n",
  3114. info);
  3115. isdn_tty_at_cout("5;ALAW;8;0;(8000)\r\n",
  3116. info);
  3117. isdn_tty_at_cout("6;ULAW;8;0;(8000)\r\n",
  3118. info);
  3119. break;
  3120. default:
  3121. PARSE_ERROR1;
  3122. }
  3123. break;
  3124. default:
  3125. PARSE_ERROR1;
  3126. }
  3127. break;
  3128. case 6:
  3129. /* AT+VTX - Start sending */
  3130. if (!m->vpar[0])
  3131. PARSE_ERROR1;
  3132. if (info->online != 1) {
  3133. isdn_tty_modem_result(RESULT_NO_ANSWER, info);
  3134. return 1;
  3135. }
  3136. info->dtmf_state = isdn_audio_dtmf_init(info->dtmf_state);
  3137. if (!info->dtmf_state) {
  3138. printk(KERN_WARNING "isdn_tty: Couldn't malloc dtmf state\n");
  3139. PARSE_ERROR1;
  3140. }
  3141. if (m->vpar[3] < 5) {
  3142. info->adpcms = isdn_audio_adpcm_init(info->adpcms, m->vpar[3]);
  3143. if (!info->adpcms) {
  3144. printk(KERN_WARNING "isdn_tty: Couldn't malloc adpcm state\n");
  3145. PARSE_ERROR1;
  3146. }
  3147. }
  3148. #ifdef ISDN_DEBUG_AT
  3149. printk(KERN_DEBUG "AT: +VTX\n");
  3150. #endif
  3151. m->lastDLE = 0;
  3152. info->vonline |= 2;
  3153. isdn_tty_modem_result(RESULT_CONNECT, info);
  3154. return 0;
  3155. break;
  3156. case 7:
  3157. /* AT+VDD - DTMF detection */
  3158. switch (*p[0]) {
  3159. case '?':
  3160. p[0]++;
  3161. sprintf(rs, "\r\n<%d>,<%d>",
  3162. m->vpar[4],
  3163. m->vpar[5]);
  3164. isdn_tty_at_cout(rs, info);
  3165. break;
  3166. case '=':
  3167. p[0]++;
  3168. if ((*p[0] >= '0') && (*p[0] <= '9')) {
  3169. if (info->online != 1)
  3170. PARSE_ERROR1;
  3171. par1 = isdn_getnum(p);
  3172. if ((par1 < 0) || (par1 > 15))
  3173. PARSE_ERROR1;
  3174. if (*p[0] != ',')
  3175. PARSE_ERROR1;
  3176. p[0]++;
  3177. par2 = isdn_getnum(p);
  3178. if ((par2 < 0) || (par2 > 255))
  3179. PARSE_ERROR1;
  3180. m->vpar[4] = par1;
  3181. m->vpar[5] = par2;
  3182. cmd.driver = info->isdn_driver;
  3183. cmd.command = ISDN_CMD_AUDIO;
  3184. cmd.arg = info->isdn_channel + (ISDN_AUDIO_SETDD << 8);
  3185. cmd.parm.num[0] = par1;
  3186. cmd.parm.num[1] = par2;
  3187. isdn_command(&cmd);
  3188. break;
  3189. } else
  3190. if (*p[0] == '?') {
  3191. p[0]++;
  3192. isdn_tty_at_cout("\r\n<0-15>,<0-255>",
  3193. info);
  3194. break;
  3195. } else
  3196. PARSE_ERROR1;
  3197. break;
  3198. default:
  3199. PARSE_ERROR1;
  3200. }
  3201. break;
  3202. default:
  3203. PARSE_ERROR1;
  3204. }
  3205. return 0;
  3206. }
  3207. #endif /* CONFIG_ISDN_AUDIO */
  3208. /*
  3209. * Parse and perform an AT-command-line.
  3210. */
  3211. static void
  3212. isdn_tty_parse_at(modem_info *info)
  3213. {
  3214. atemu *m = &info->emu;
  3215. char *p;
  3216. char ds[ISDN_MSNLEN];
  3217. #ifdef ISDN_DEBUG_AT
  3218. printk(KERN_DEBUG "AT: '%s'\n", m->mdmcmd);
  3219. #endif
  3220. for (p = &m->mdmcmd[2]; *p;) {
  3221. switch (*p) {
  3222. case ' ':
  3223. p++;
  3224. break;
  3225. case 'A':
  3226. /* A - Accept incoming call */
  3227. p++;
  3228. isdn_tty_cmd_ATA(info);
  3229. return;
  3230. case 'D':
  3231. /* D - Dial */
  3232. if (info->msr & UART_MSR_DCD)
  3233. PARSE_ERROR;
  3234. if (info->msr & UART_MSR_RI) {
  3235. isdn_tty_modem_result(RESULT_NO_CARRIER, info);
  3236. return;
  3237. }
  3238. isdn_tty_getdial(++p, ds, sizeof ds);
  3239. p += strlen(p);
  3240. if (!strlen(m->msn))
  3241. isdn_tty_modem_result(RESULT_NO_MSN_EAZ, info);
  3242. else if (strlen(ds))
  3243. isdn_tty_dial(ds, info, m);
  3244. else
  3245. PARSE_ERROR;
  3246. return;
  3247. case 'E':
  3248. /* E - Turn Echo on/off */
  3249. p++;
  3250. switch (isdn_getnum(&p)) {
  3251. case 0:
  3252. m->mdmreg[REG_ECHO] &= ~BIT_ECHO;
  3253. break;
  3254. case 1:
  3255. m->mdmreg[REG_ECHO] |= BIT_ECHO;
  3256. break;
  3257. default:
  3258. PARSE_ERROR;
  3259. }
  3260. break;
  3261. case 'H':
  3262. /* H - On/Off-hook */
  3263. p++;
  3264. switch (*p) {
  3265. case '0':
  3266. p++;
  3267. isdn_tty_on_hook(info);
  3268. break;
  3269. case '1':
  3270. p++;
  3271. isdn_tty_off_hook();
  3272. break;
  3273. default:
  3274. isdn_tty_on_hook(info);
  3275. break;
  3276. }
  3277. break;
  3278. case 'I':
  3279. /* I - Information */
  3280. p++;
  3281. isdn_tty_at_cout("\r\nLinux ISDN", info);
  3282. switch (*p) {
  3283. case '0':
  3284. case '1':
  3285. p++;
  3286. break;
  3287. case '2':
  3288. p++;
  3289. isdn_tty_report(info);
  3290. break;
  3291. case '3':
  3292. p++;
  3293. snprintf(ds, sizeof(ds), "\r\n%d", info->emu.charge);
  3294. isdn_tty_at_cout(ds, info);
  3295. break;
  3296. default:;
  3297. }
  3298. break;
  3299. #ifdef DUMMY_HAYES_AT
  3300. case 'L':
  3301. case 'M':
  3302. /* only for be compilant with common scripts */
  3303. /* no function */
  3304. p++;
  3305. isdn_getnum(&p);
  3306. break;
  3307. #endif
  3308. case 'O':
  3309. /* O - Go online */
  3310. p++;
  3311. if (info->msr & UART_MSR_DCD)
  3312. /* if B-Channel is up */
  3313. isdn_tty_modem_result((m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM) ? RESULT_CONNECT : RESULT_CONNECT64000, info);
  3314. else
  3315. isdn_tty_modem_result(RESULT_NO_CARRIER, info);
  3316. return;
  3317. case 'Q':
  3318. /* Q - Turn Emulator messages on/off */
  3319. p++;
  3320. switch (isdn_getnum(&p)) {
  3321. case 0:
  3322. m->mdmreg[REG_RESP] |= BIT_RESP;
  3323. break;
  3324. case 1:
  3325. m->mdmreg[REG_RESP] &= ~BIT_RESP;
  3326. break;
  3327. default:
  3328. PARSE_ERROR;
  3329. }
  3330. break;
  3331. case 'S':
  3332. /* S - Set/Get Register */
  3333. p++;
  3334. if (isdn_tty_cmd_ATS(&p, info))
  3335. return;
  3336. break;
  3337. case 'V':
  3338. /* V - Numeric or ASCII Emulator-messages */
  3339. p++;
  3340. switch (isdn_getnum(&p)) {
  3341. case 0:
  3342. m->mdmreg[REG_RESP] |= BIT_RESPNUM;
  3343. break;
  3344. case 1:
  3345. m->mdmreg[REG_RESP] &= ~BIT_RESPNUM;
  3346. break;
  3347. default:
  3348. PARSE_ERROR;
  3349. }
  3350. break;
  3351. case 'Z':
  3352. /* Z - Load Registers from Profile */
  3353. p++;
  3354. if (info->msr & UART_MSR_DCD) {
  3355. info->online = 0;
  3356. isdn_tty_on_hook(info);
  3357. }
  3358. isdn_tty_modem_reset_regs(info, 1);
  3359. break;
  3360. case '+':
  3361. p++;
  3362. switch (*p) {
  3363. #ifdef CONFIG_ISDN_AUDIO
  3364. case 'F':
  3365. p++;
  3366. if (isdn_tty_cmd_PLUSF(&p, info))
  3367. return;
  3368. break;
  3369. case 'V':
  3370. if ((!(m->mdmreg[REG_SI1] & 1)) ||
  3371. (m->mdmreg[REG_L2PROT] == ISDN_PROTO_L2_MODEM))
  3372. PARSE_ERROR;
  3373. p++;
  3374. if (isdn_tty_cmd_PLUSV(&p, info))
  3375. return;
  3376. break;
  3377. #endif /* CONFIG_ISDN_AUDIO */
  3378. case 'S': /* SUSPEND */
  3379. p++;
  3380. isdn_tty_get_msnstr(ds, &p);
  3381. isdn_tty_suspend(ds, info, m);
  3382. break;
  3383. case 'R': /* RESUME */
  3384. p++;
  3385. isdn_tty_get_msnstr(ds, &p);
  3386. isdn_tty_resume(ds, info, m);
  3387. break;
  3388. case 'M': /* MESSAGE */
  3389. p++;
  3390. isdn_tty_send_msg(info, m, p);
  3391. break;
  3392. default:
  3393. PARSE_ERROR;
  3394. }
  3395. break;
  3396. case '&':
  3397. p++;
  3398. if (isdn_tty_cmd_ATand(&p, info))
  3399. return;
  3400. break;
  3401. default:
  3402. PARSE_ERROR;
  3403. }
  3404. }
  3405. #ifdef CONFIG_ISDN_AUDIO
  3406. if (!info->vonline)
  3407. #endif
  3408. isdn_tty_modem_result(RESULT_OK, info);
  3409. }
  3410. /* Need own toupper() because standard-toupper is not available
  3411. * within modules.
  3412. */
  3413. #define my_toupper(c) (((c >= 'a') && (c <= 'z')) ? (c & 0xdf) : c)
  3414. /*
  3415. * Perform line-editing of AT-commands
  3416. *
  3417. * Parameters:
  3418. * p inputbuffer
  3419. * count length of buffer
  3420. * channel index to line (minor-device)
  3421. */
  3422. static int
  3423. isdn_tty_edit_at(const char *p, int count, modem_info *info)
  3424. {
  3425. atemu *m = &info->emu;
  3426. int total = 0;
  3427. u_char c;
  3428. char eb[2];
  3429. int cnt;
  3430. for (cnt = count; cnt > 0; p++, cnt--) {
  3431. c = *p;
  3432. total++;
  3433. if (c == m->mdmreg[REG_CR] || c == m->mdmreg[REG_LF]) {
  3434. /* Separator (CR or LF) */
  3435. m->mdmcmd[m->mdmcmdl] = 0;
  3436. if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
  3437. eb[0] = c;
  3438. eb[1] = 0;
  3439. isdn_tty_at_cout(eb, info);
  3440. }
  3441. if ((m->mdmcmdl >= 2) && (!(strncmp(m->mdmcmd, "AT", 2))))
  3442. isdn_tty_parse_at(info);
  3443. m->mdmcmdl = 0;
  3444. continue;
  3445. }
  3446. if (c == m->mdmreg[REG_BS] && m->mdmreg[REG_BS] < 128) {
  3447. /* Backspace-Function */
  3448. if ((m->mdmcmdl > 2) || (!m->mdmcmdl)) {
  3449. if (m->mdmcmdl)
  3450. m->mdmcmdl--;
  3451. if (m->mdmreg[REG_ECHO] & BIT_ECHO)
  3452. isdn_tty_at_cout("\b", info);
  3453. }
  3454. continue;
  3455. }
  3456. if (cmdchar(c)) {
  3457. if (m->mdmreg[REG_ECHO] & BIT_ECHO) {
  3458. eb[0] = c;
  3459. eb[1] = 0;
  3460. isdn_tty_at_cout(eb, info);
  3461. }
  3462. if (m->mdmcmdl < 255) {
  3463. c = my_toupper(c);
  3464. switch (m->mdmcmdl) {
  3465. case 1:
  3466. if (c == 'T') {
  3467. m->mdmcmd[m->mdmcmdl] = c;
  3468. m->mdmcmd[++m->mdmcmdl] = 0;
  3469. break;
  3470. } else
  3471. m->mdmcmdl = 0;
  3472. /* Fall through, check for 'A' */
  3473. case 0:
  3474. if (c == 'A') {
  3475. m->mdmcmd[m->mdmcmdl] = c;
  3476. m->mdmcmd[++m->mdmcmdl] = 0;
  3477. }
  3478. break;
  3479. default:
  3480. m->mdmcmd[m->mdmcmdl] = c;
  3481. m->mdmcmd[++m->mdmcmdl] = 0;
  3482. }
  3483. }
  3484. }
  3485. }
  3486. return total;
  3487. }
  3488. /*
  3489. * Switch all modem-channels who are online and got a valid
  3490. * escape-sequence 1.5 seconds ago, to command-mode.
  3491. * This function is called every second via timer-interrupt from within
  3492. * timer-dispatcher isdn_timer_function()
  3493. */
  3494. void
  3495. isdn_tty_modem_escape(void)
  3496. {
  3497. int ton = 0;
  3498. int i;
  3499. int midx;
  3500. for (i = 0; i < ISDN_MAX_CHANNELS; i++)
  3501. if (USG_MODEM(dev->usage[i]) && (midx = dev->m_idx[i]) >= 0) {
  3502. modem_info *info = &dev->mdm.info[midx];
  3503. if (info->online) {
  3504. ton = 1;
  3505. if ((info->emu.pluscount == 3) &&
  3506. time_after(jiffies,
  3507. info->emu.lastplus + PLUSWAIT2)) {
  3508. info->emu.pluscount = 0;
  3509. info->online = 0;
  3510. isdn_tty_modem_result(RESULT_OK, info);
  3511. }
  3512. }
  3513. }
  3514. isdn_timer_ctrl(ISDN_TIMER_MODEMPLUS, ton);
  3515. }
  3516. /*
  3517. * Put a RING-message to all modem-channels who have the RI-bit set.
  3518. * This function is called every second via timer-interrupt from within
  3519. * timer-dispatcher isdn_timer_function()
  3520. */
  3521. void
  3522. isdn_tty_modem_ring(void)
  3523. {
  3524. int ton = 0;
  3525. int i;
  3526. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  3527. modem_info *info = &dev->mdm.info[i];
  3528. if (info->msr & UART_MSR_RI) {
  3529. ton = 1;
  3530. isdn_tty_modem_result(RESULT_RING, info);
  3531. }
  3532. }
  3533. isdn_timer_ctrl(ISDN_TIMER_MODEMRING, ton);
  3534. }
  3535. /*
  3536. * For all online tty's, try sending data to
  3537. * the lower levels.
  3538. */
  3539. void
  3540. isdn_tty_modem_xmit(void)
  3541. {
  3542. int ton = 1;
  3543. int i;
  3544. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  3545. modem_info *info = &dev->mdm.info[i];
  3546. if (info->online) {
  3547. ton = 1;
  3548. isdn_tty_senddown(info);
  3549. isdn_tty_tint(info);
  3550. }
  3551. }
  3552. isdn_timer_ctrl(ISDN_TIMER_MODEMXMIT, ton);
  3553. }
  3554. /*
  3555. * Check all channels if we have a 'no carrier' timeout.
  3556. * Timeout value is set by Register S7.
  3557. */
  3558. void
  3559. isdn_tty_carrier_timeout(void)
  3560. {
  3561. int ton = 0;
  3562. int i;
  3563. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  3564. modem_info *info = &dev->mdm.info[i];
  3565. if (!info->dialing)
  3566. continue;
  3567. if (info->emu.carrierwait++ > info->emu.mdmreg[REG_WAITC]) {
  3568. info->dialing = 0;
  3569. isdn_tty_modem_result(RESULT_NO_CARRIER, info);
  3570. isdn_tty_modem_hup(info, 1);
  3571. } else
  3572. ton = 1;
  3573. }
  3574. isdn_timer_ctrl(ISDN_TIMER_CARRIER, ton);
  3575. }