isdn_tty.c 89 KB

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