isdn_tty.c 95 KB

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