isdn_tty.c 89 KB

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