isdn_tty.c 92 KB

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