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