isdn_tty.c 95 KB

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