isdn_tty.c 89 KB

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