isdn_tty.c 95 KB

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