isdn_tty.c 95 KB

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