isdn_tty.c 95 KB

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