isdn_tty.c 95 KB

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