isdnloop.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  1. /* $Id: isdnloop.c,v 1.11.6.7 2001/11/11 19:54:31 kai Exp $
  2. *
  3. * ISDN low-level module implementing a dummy loop driver.
  4. *
  5. * Copyright 1997 by Fritz Elfert (fritz@isdn4linux.de)
  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. #include <linux/module.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/init.h>
  14. #include <linux/sched.h>
  15. #include "isdnloop.h"
  16. static char *revision = "$Revision: 1.11.6.7 $";
  17. static char *isdnloop_id = "loop0";
  18. MODULE_DESCRIPTION("ISDN4Linux: Pseudo Driver that simulates an ISDN card");
  19. MODULE_AUTHOR("Fritz Elfert");
  20. MODULE_LICENSE("GPL");
  21. module_param(isdnloop_id, charp, 0);
  22. MODULE_PARM_DESC(isdnloop_id, "ID-String of first card");
  23. static int isdnloop_addcard(char *);
  24. /*
  25. * Free queue completely.
  26. *
  27. * Parameter:
  28. * card = pointer to card struct
  29. * channel = channel number
  30. */
  31. static void
  32. isdnloop_free_queue(isdnloop_card * card, int channel)
  33. {
  34. struct sk_buff_head *queue = &card->bqueue[channel];
  35. skb_queue_purge(queue);
  36. card->sndcount[channel] = 0;
  37. }
  38. /*
  39. * Send B-Channel data to another virtual card.
  40. * This routine is called via timer-callback from isdnloop_pollbchan().
  41. *
  42. * Parameter:
  43. * card = pointer to card struct.
  44. * ch = channel number (0-based)
  45. */
  46. static void
  47. isdnloop_bchan_send(isdnloop_card * card, int ch)
  48. {
  49. isdnloop_card *rcard = card->rcard[ch];
  50. int rch = card->rch[ch], len, ack;
  51. struct sk_buff *skb;
  52. isdn_ctrl cmd;
  53. while (card->sndcount[ch]) {
  54. if ((skb = skb_dequeue(&card->bqueue[ch]))) {
  55. len = skb->len;
  56. card->sndcount[ch] -= len;
  57. ack = *(skb->head); /* used as scratch area */
  58. cmd.driver = card->myid;
  59. cmd.arg = ch;
  60. if (rcard){
  61. rcard->interface.rcvcallb_skb(rcard->myid, rch, skb);
  62. } else {
  63. printk(KERN_WARNING "isdnloop: no rcard, skb dropped\n");
  64. dev_kfree_skb(skb);
  65. };
  66. cmd.command = ISDN_STAT_BSENT;
  67. cmd.parm.length = len;
  68. card->interface.statcallb(&cmd);
  69. } else
  70. card->sndcount[ch] = 0;
  71. }
  72. }
  73. /*
  74. * Send/Receive Data to/from the B-Channel.
  75. * This routine is called via timer-callback.
  76. * It schedules itself while any B-Channel is open.
  77. *
  78. * Parameter:
  79. * data = pointer to card struct, set by kernel timer.data
  80. */
  81. static void
  82. isdnloop_pollbchan(unsigned long data)
  83. {
  84. isdnloop_card *card = (isdnloop_card *) data;
  85. unsigned long flags;
  86. if (card->flags & ISDNLOOP_FLAGS_B1ACTIVE)
  87. isdnloop_bchan_send(card, 0);
  88. if (card->flags & ISDNLOOP_FLAGS_B2ACTIVE)
  89. isdnloop_bchan_send(card, 1);
  90. if (card->flags & (ISDNLOOP_FLAGS_B1ACTIVE | ISDNLOOP_FLAGS_B2ACTIVE)) {
  91. /* schedule b-channel polling again */
  92. spin_lock_irqsave(&card->isdnloop_lock, flags);
  93. card->rb_timer.expires = jiffies + ISDNLOOP_TIMER_BCREAD;
  94. add_timer(&card->rb_timer);
  95. card->flags |= ISDNLOOP_FLAGS_RBTIMER;
  96. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  97. } else
  98. card->flags &= ~ISDNLOOP_FLAGS_RBTIMER;
  99. }
  100. /*
  101. * Parse ICN-type setup string and fill fields of setup-struct
  102. * with parsed data.
  103. *
  104. * Parameter:
  105. * setup = setup string, format: [caller-id],si1,si2,[called-id]
  106. * cmd = pointer to struct to be filled.
  107. */
  108. static void
  109. isdnloop_parse_setup(char *setup, isdn_ctrl * cmd)
  110. {
  111. char *t = setup;
  112. char *s = strchr(t, ',');
  113. *s++ = '\0';
  114. strlcpy(cmd->parm.setup.phone, t, sizeof(cmd->parm.setup.phone));
  115. s = strchr(t = s, ',');
  116. *s++ = '\0';
  117. if (!strlen(t))
  118. cmd->parm.setup.si1 = 0;
  119. else
  120. cmd->parm.setup.si1 = simple_strtoul(t, NULL, 10);
  121. s = strchr(t = s, ',');
  122. *s++ = '\0';
  123. if (!strlen(t))
  124. cmd->parm.setup.si2 = 0;
  125. else
  126. cmd->parm.setup.si2 =
  127. simple_strtoul(t, NULL, 10);
  128. strlcpy(cmd->parm.setup.eazmsn, s, sizeof(cmd->parm.setup.eazmsn));
  129. cmd->parm.setup.plan = 0;
  130. cmd->parm.setup.screen = 0;
  131. }
  132. typedef struct isdnloop_stat {
  133. char *statstr;
  134. int command;
  135. int action;
  136. } isdnloop_stat;
  137. /* *INDENT-OFF* */
  138. static isdnloop_stat isdnloop_stat_table[] =
  139. {
  140. {"BCON_", ISDN_STAT_BCONN, 1}, /* B-Channel connected */
  141. {"BDIS_", ISDN_STAT_BHUP, 2}, /* B-Channel disconnected */
  142. {"DCON_", ISDN_STAT_DCONN, 0}, /* D-Channel connected */
  143. {"DDIS_", ISDN_STAT_DHUP, 0}, /* D-Channel disconnected */
  144. {"DCAL_I", ISDN_STAT_ICALL, 3}, /* Incoming call dialup-line */
  145. {"DSCA_I", ISDN_STAT_ICALL, 3}, /* Incoming call 1TR6-SPV */
  146. {"FCALL", ISDN_STAT_ICALL, 4}, /* Leased line connection up */
  147. {"CIF", ISDN_STAT_CINF, 5}, /* Charge-info, 1TR6-type */
  148. {"AOC", ISDN_STAT_CINF, 6}, /* Charge-info, DSS1-type */
  149. {"CAU", ISDN_STAT_CAUSE, 7}, /* Cause code */
  150. {"TEI OK", ISDN_STAT_RUN, 0}, /* Card connected to wallplug */
  151. {"E_L1: ACT FAIL", ISDN_STAT_BHUP, 8}, /* Layer-1 activation failed */
  152. {"E_L2: DATA LIN", ISDN_STAT_BHUP, 8}, /* Layer-2 data link lost */
  153. {"E_L1: ACTIVATION FAILED",
  154. ISDN_STAT_BHUP, 8}, /* Layer-1 activation failed */
  155. {NULL, 0, -1}
  156. };
  157. /* *INDENT-ON* */
  158. /*
  159. * Parse Status message-strings from virtual card.
  160. * Depending on status, call statcallb for sending messages to upper
  161. * levels. Also set/reset B-Channel active-flags.
  162. *
  163. * Parameter:
  164. * status = status string to parse.
  165. * channel = channel where message comes from.
  166. * card = card where message comes from.
  167. */
  168. static void
  169. isdnloop_parse_status(u_char * status, int channel, isdnloop_card * card)
  170. {
  171. isdnloop_stat *s = isdnloop_stat_table;
  172. int action = -1;
  173. isdn_ctrl cmd;
  174. while (s->statstr) {
  175. if (!strncmp(status, s->statstr, strlen(s->statstr))) {
  176. cmd.command = s->command;
  177. action = s->action;
  178. break;
  179. }
  180. s++;
  181. }
  182. if (action == -1)
  183. return;
  184. cmd.driver = card->myid;
  185. cmd.arg = channel;
  186. switch (action) {
  187. case 1:
  188. /* BCON_x */
  189. card->flags |= (channel) ?
  190. ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE;
  191. break;
  192. case 2:
  193. /* BDIS_x */
  194. card->flags &= ~((channel) ?
  195. ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE);
  196. isdnloop_free_queue(card, channel);
  197. break;
  198. case 3:
  199. /* DCAL_I and DSCA_I */
  200. isdnloop_parse_setup(status + 6, &cmd);
  201. break;
  202. case 4:
  203. /* FCALL */
  204. sprintf(cmd.parm.setup.phone, "LEASED%d", card->myid);
  205. sprintf(cmd.parm.setup.eazmsn, "%d", channel + 1);
  206. cmd.parm.setup.si1 = 7;
  207. cmd.parm.setup.si2 = 0;
  208. cmd.parm.setup.plan = 0;
  209. cmd.parm.setup.screen = 0;
  210. break;
  211. case 5:
  212. /* CIF */
  213. strlcpy(cmd.parm.num, status + 3, sizeof(cmd.parm.num));
  214. break;
  215. case 6:
  216. /* AOC */
  217. snprintf(cmd.parm.num, sizeof(cmd.parm.num), "%d",
  218. (int) simple_strtoul(status + 7, NULL, 16));
  219. break;
  220. case 7:
  221. /* CAU */
  222. status += 3;
  223. if (strlen(status) == 4)
  224. snprintf(cmd.parm.num, sizeof(cmd.parm.num), "%s%c%c",
  225. status + 2, *status, *(status + 1));
  226. else
  227. strlcpy(cmd.parm.num, status + 1, sizeof(cmd.parm.num));
  228. break;
  229. case 8:
  230. /* Misc Errors on L1 and L2 */
  231. card->flags &= ~ISDNLOOP_FLAGS_B1ACTIVE;
  232. isdnloop_free_queue(card, 0);
  233. cmd.arg = 0;
  234. cmd.driver = card->myid;
  235. card->interface.statcallb(&cmd);
  236. cmd.command = ISDN_STAT_DHUP;
  237. cmd.arg = 0;
  238. cmd.driver = card->myid;
  239. card->interface.statcallb(&cmd);
  240. cmd.command = ISDN_STAT_BHUP;
  241. card->flags &= ~ISDNLOOP_FLAGS_B2ACTIVE;
  242. isdnloop_free_queue(card, 1);
  243. cmd.arg = 1;
  244. cmd.driver = card->myid;
  245. card->interface.statcallb(&cmd);
  246. cmd.command = ISDN_STAT_DHUP;
  247. cmd.arg = 1;
  248. cmd.driver = card->myid;
  249. break;
  250. }
  251. card->interface.statcallb(&cmd);
  252. }
  253. /*
  254. * Store a cwcharacter into ringbuffer for reading from /dev/isdnctrl
  255. *
  256. * Parameter:
  257. * card = pointer to card struct.
  258. * c = char to store.
  259. */
  260. static void
  261. isdnloop_putmsg(isdnloop_card * card, unsigned char c)
  262. {
  263. ulong flags;
  264. spin_lock_irqsave(&card->isdnloop_lock, flags);
  265. *card->msg_buf_write++ = (c == 0xff) ? '\n' : c;
  266. if (card->msg_buf_write == card->msg_buf_read) {
  267. if (++card->msg_buf_read > card->msg_buf_end)
  268. card->msg_buf_read = card->msg_buf;
  269. }
  270. if (card->msg_buf_write > card->msg_buf_end)
  271. card->msg_buf_write = card->msg_buf;
  272. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  273. }
  274. /*
  275. * Poll a virtual cards message queue.
  276. * If there are new status-replies from the card, copy them to
  277. * ringbuffer for reading on /dev/isdnctrl and call
  278. * isdnloop_parse_status() for processing them. Watch for special
  279. * Firmware bootmessage and parse it, to get the D-Channel protocol.
  280. * If there are B-Channels open, initiate a timer-callback to
  281. * isdnloop_pollbchan().
  282. * This routine is called periodically via timer interrupt.
  283. *
  284. * Parameter:
  285. * data = pointer to card struct
  286. */
  287. static void
  288. isdnloop_polldchan(unsigned long data)
  289. {
  290. isdnloop_card *card = (isdnloop_card *) data;
  291. struct sk_buff *skb;
  292. int avail;
  293. int left;
  294. u_char c;
  295. int ch;
  296. unsigned long flags;
  297. u_char *p;
  298. isdn_ctrl cmd;
  299. if ((skb = skb_dequeue(&card->dqueue)))
  300. avail = skb->len;
  301. else
  302. avail = 0;
  303. for (left = avail; left > 0; left--) {
  304. c = *skb->data;
  305. skb_pull(skb, 1);
  306. isdnloop_putmsg(card, c);
  307. card->imsg[card->iptr] = c;
  308. if (card->iptr < 59)
  309. card->iptr++;
  310. if (!skb->len) {
  311. avail++;
  312. isdnloop_putmsg(card, '\n');
  313. card->imsg[card->iptr] = 0;
  314. card->iptr = 0;
  315. if (card->imsg[0] == '0' && card->imsg[1] >= '0' &&
  316. card->imsg[1] <= '2' && card->imsg[2] == ';') {
  317. ch = (card->imsg[1] - '0') - 1;
  318. p = &card->imsg[3];
  319. isdnloop_parse_status(p, ch, card);
  320. } else {
  321. p = card->imsg;
  322. if (!strncmp(p, "DRV1.", 5)) {
  323. printk(KERN_INFO "isdnloop: (%s) %s\n", CID, p);
  324. if (!strncmp(p + 7, "TC", 2)) {
  325. card->ptype = ISDN_PTYPE_1TR6;
  326. card->interface.features |= ISDN_FEATURE_P_1TR6;
  327. printk(KERN_INFO
  328. "isdnloop: (%s) 1TR6-Protocol loaded and running\n", CID);
  329. }
  330. if (!strncmp(p + 7, "EC", 2)) {
  331. card->ptype = ISDN_PTYPE_EURO;
  332. card->interface.features |= ISDN_FEATURE_P_EURO;
  333. printk(KERN_INFO
  334. "isdnloop: (%s) Euro-Protocol loaded and running\n", CID);
  335. }
  336. continue;
  337. }
  338. }
  339. }
  340. }
  341. if (avail) {
  342. cmd.command = ISDN_STAT_STAVAIL;
  343. cmd.driver = card->myid;
  344. cmd.arg = avail;
  345. card->interface.statcallb(&cmd);
  346. }
  347. if (card->flags & (ISDNLOOP_FLAGS_B1ACTIVE | ISDNLOOP_FLAGS_B2ACTIVE))
  348. if (!(card->flags & ISDNLOOP_FLAGS_RBTIMER)) {
  349. /* schedule b-channel polling */
  350. card->flags |= ISDNLOOP_FLAGS_RBTIMER;
  351. spin_lock_irqsave(&card->isdnloop_lock, flags);
  352. del_timer(&card->rb_timer);
  353. card->rb_timer.function = isdnloop_pollbchan;
  354. card->rb_timer.data = (unsigned long) card;
  355. card->rb_timer.expires = jiffies + ISDNLOOP_TIMER_BCREAD;
  356. add_timer(&card->rb_timer);
  357. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  358. }
  359. /* schedule again */
  360. spin_lock_irqsave(&card->isdnloop_lock, flags);
  361. card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
  362. add_timer(&card->st_timer);
  363. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  364. }
  365. /*
  366. * Append a packet to the transmit buffer-queue.
  367. *
  368. * Parameter:
  369. * channel = Number of B-channel
  370. * skb = packet to send.
  371. * card = pointer to card-struct
  372. * Return:
  373. * Number of bytes transferred, -E??? on error
  374. */
  375. static int
  376. isdnloop_sendbuf(int channel, struct sk_buff *skb, isdnloop_card * card)
  377. {
  378. int len = skb->len;
  379. unsigned long flags;
  380. struct sk_buff *nskb;
  381. if (len > 4000) {
  382. printk(KERN_WARNING
  383. "isdnloop: Send packet too large\n");
  384. return -EINVAL;
  385. }
  386. if (len) {
  387. if (!(card->flags & (channel) ? ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE))
  388. return 0;
  389. if (card->sndcount[channel] > ISDNLOOP_MAX_SQUEUE)
  390. return 0;
  391. spin_lock_irqsave(&card->isdnloop_lock, flags);
  392. nskb = dev_alloc_skb(skb->len);
  393. if (nskb) {
  394. memcpy(skb_put(nskb, len), skb->data, len);
  395. skb_queue_tail(&card->bqueue[channel], nskb);
  396. dev_kfree_skb(skb);
  397. } else
  398. len = 0;
  399. card->sndcount[channel] += len;
  400. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  401. }
  402. return len;
  403. }
  404. /*
  405. * Read the messages from the card's ringbuffer
  406. *
  407. * Parameter:
  408. * buf = pointer to buffer.
  409. * len = number of bytes to read.
  410. * user = flag, 1: called from userlevel 0: called from kernel.
  411. * card = pointer to card struct.
  412. * Return:
  413. * number of bytes actually transferred.
  414. */
  415. static int
  416. isdnloop_readstatus(u_char __user *buf, int len, isdnloop_card * card)
  417. {
  418. int count;
  419. u_char __user *p;
  420. for (p = buf, count = 0; count < len; p++, count++) {
  421. if (card->msg_buf_read == card->msg_buf_write)
  422. return count;
  423. if (put_user(*card->msg_buf_read++, p))
  424. return -EFAULT;
  425. if (card->msg_buf_read > card->msg_buf_end)
  426. card->msg_buf_read = card->msg_buf;
  427. }
  428. return count;
  429. }
  430. /*
  431. * Simulate a card's response by appending it to the cards
  432. * message queue.
  433. *
  434. * Parameter:
  435. * card = pointer to card struct.
  436. * s = pointer to message-string.
  437. * ch = channel: 0 = generic messages, 1 and 2 = D-channel messages.
  438. * Return:
  439. * 0 on success, 1 on memory squeeze.
  440. */
  441. static int
  442. isdnloop_fake(isdnloop_card * card, char *s, int ch)
  443. {
  444. struct sk_buff *skb;
  445. int len = strlen(s) + ((ch >= 0) ? 3 : 0);
  446. if (!(skb = dev_alloc_skb(len))) {
  447. printk(KERN_WARNING "isdnloop: Out of memory in isdnloop_fake\n");
  448. return 1;
  449. }
  450. if (ch >= 0)
  451. sprintf(skb_put(skb, 3), "%02d;", ch);
  452. memcpy(skb_put(skb, strlen(s)), s, strlen(s));
  453. skb_queue_tail(&card->dqueue, skb);
  454. return 0;
  455. }
  456. /* *INDENT-OFF* */
  457. static isdnloop_stat isdnloop_cmd_table[] =
  458. {
  459. {"BCON_R", 0, 1}, /* B-Channel connect */
  460. {"BCON_I", 0, 17}, /* B-Channel connect ind */
  461. {"BDIS_R", 0, 2}, /* B-Channel disconnect */
  462. {"DDIS_R", 0, 3}, /* D-Channel disconnect */
  463. {"DCON_R", 0, 16}, /* D-Channel connect */
  464. {"DSCA_R", 0, 4}, /* Dial 1TR6-SPV */
  465. {"DCAL_R", 0, 5}, /* Dial */
  466. {"EAZC", 0, 6}, /* Clear EAZ listener */
  467. {"EAZ", 0, 7}, /* Set EAZ listener */
  468. {"SEEAZ", 0, 8}, /* Get EAZ listener */
  469. {"MSN", 0, 9}, /* Set/Clear MSN listener */
  470. {"MSALL", 0, 10}, /* Set multi MSN listeners */
  471. {"SETSIL", 0, 11}, /* Set SI list */
  472. {"SEESIL", 0, 12}, /* Get SI list */
  473. {"SILC", 0, 13}, /* Clear SI list */
  474. {"LOCK", 0, -1}, /* LOCK channel */
  475. {"UNLOCK", 0, -1}, /* UNLOCK channel */
  476. {"FV2ON", 1, 14}, /* Leased mode on */
  477. {"FV2OFF", 1, 15}, /* Leased mode off */
  478. {NULL, 0, -1}
  479. };
  480. /* *INDENT-ON* */
  481. /*
  482. * Simulate an error-response from a card.
  483. *
  484. * Parameter:
  485. * card = pointer to card struct.
  486. */
  487. static void
  488. isdnloop_fake_err(isdnloop_card * card)
  489. {
  490. char buf[60];
  491. sprintf(buf, "E%s", card->omsg);
  492. isdnloop_fake(card, buf, -1);
  493. isdnloop_fake(card, "NAK", -1);
  494. }
  495. static u_char ctable_eu[] =
  496. {0x00, 0x11, 0x01, 0x12};
  497. static u_char ctable_1t[] =
  498. {0x00, 0x3b, 0x01, 0x3a};
  499. /*
  500. * Assemble a simplified cause message depending on the
  501. * D-channel protocol used.
  502. *
  503. * Parameter:
  504. * card = pointer to card struct.
  505. * loc = location: 0 = local, 1 = remote.
  506. * cau = cause: 1 = busy, 2 = nonexistent callerid, 3 = no user responding.
  507. * Return:
  508. * Pointer to buffer containing the assembled message.
  509. */
  510. static char *
  511. isdnloop_unicause(isdnloop_card * card, int loc, int cau)
  512. {
  513. static char buf[6];
  514. switch (card->ptype) {
  515. case ISDN_PTYPE_EURO:
  516. sprintf(buf, "E%02X%02X", (loc) ? 4 : 2, ctable_eu[cau]);
  517. break;
  518. case ISDN_PTYPE_1TR6:
  519. sprintf(buf, "%02X44", ctable_1t[cau]);
  520. break;
  521. default:
  522. return ("0000");
  523. }
  524. return (buf);
  525. }
  526. /*
  527. * Release a virtual connection. Called from timer interrupt, when
  528. * called party did not respond.
  529. *
  530. * Parameter:
  531. * card = pointer to card struct.
  532. * ch = channel (0-based)
  533. */
  534. static void
  535. isdnloop_atimeout(isdnloop_card * card, int ch)
  536. {
  537. unsigned long flags;
  538. char buf[60];
  539. spin_lock_irqsave(&card->isdnloop_lock, flags);
  540. if (card->rcard) {
  541. isdnloop_fake(card->rcard[ch], "DDIS_I", card->rch[ch] + 1);
  542. card->rcard[ch]->rcard[card->rch[ch]] = NULL;
  543. card->rcard[ch] = NULL;
  544. }
  545. isdnloop_fake(card, "DDIS_I", ch + 1);
  546. /* No user responding */
  547. sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 3));
  548. isdnloop_fake(card, buf, ch + 1);
  549. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  550. }
  551. /*
  552. * Wrapper for isdnloop_atimeout().
  553. */
  554. static void
  555. isdnloop_atimeout0(unsigned long data)
  556. {
  557. isdnloop_card *card = (isdnloop_card *) data;
  558. isdnloop_atimeout(card, 0);
  559. }
  560. /*
  561. * Wrapper for isdnloop_atimeout().
  562. */
  563. static void
  564. isdnloop_atimeout1(unsigned long data)
  565. {
  566. isdnloop_card *card = (isdnloop_card *) data;
  567. isdnloop_atimeout(card, 1);
  568. }
  569. /*
  570. * Install a watchdog for a user, not responding.
  571. *
  572. * Parameter:
  573. * card = pointer to card struct.
  574. * ch = channel to watch for.
  575. */
  576. static void
  577. isdnloop_start_ctimer(isdnloop_card * card, int ch)
  578. {
  579. unsigned long flags;
  580. spin_lock_irqsave(&card->isdnloop_lock, flags);
  581. init_timer(&card->c_timer[ch]);
  582. card->c_timer[ch].expires = jiffies + ISDNLOOP_TIMER_ALERTWAIT;
  583. if (ch)
  584. card->c_timer[ch].function = isdnloop_atimeout1;
  585. else
  586. card->c_timer[ch].function = isdnloop_atimeout0;
  587. card->c_timer[ch].data = (unsigned long) card;
  588. add_timer(&card->c_timer[ch]);
  589. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  590. }
  591. /*
  592. * Kill a pending channel watchdog.
  593. *
  594. * Parameter:
  595. * card = pointer to card struct.
  596. * ch = channel (0-based).
  597. */
  598. static void
  599. isdnloop_kill_ctimer(isdnloop_card * card, int ch)
  600. {
  601. unsigned long flags;
  602. spin_lock_irqsave(&card->isdnloop_lock, flags);
  603. del_timer(&card->c_timer[ch]);
  604. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  605. }
  606. static u_char si2bit[] =
  607. {0, 1, 0, 0, 0, 2, 0, 4, 0, 0};
  608. static u_char bit2si[] =
  609. {1, 5, 7};
  610. /*
  611. * Try finding a listener for an outgoing call.
  612. *
  613. * Parameter:
  614. * card = pointer to calling card.
  615. * p = pointer to ICN-type setup-string.
  616. * lch = channel of calling card.
  617. * cmd = pointer to struct to be filled when parsing setup.
  618. * Return:
  619. * 0 = found match, alerting should happen.
  620. * 1 = found matching number but it is busy.
  621. * 2 = no matching listener.
  622. * 3 = found matching number but SI does not match.
  623. */
  624. static int
  625. isdnloop_try_call(isdnloop_card * card, char *p, int lch, isdn_ctrl * cmd)
  626. {
  627. isdnloop_card *cc = cards;
  628. unsigned long flags;
  629. int ch;
  630. int num_match;
  631. int i;
  632. char *e;
  633. char nbuf[32];
  634. isdnloop_parse_setup(p, cmd);
  635. while (cc) {
  636. for (ch = 0; ch < 2; ch++) {
  637. /* Exclude ourself */
  638. if ((cc == card) && (ch == lch))
  639. continue;
  640. num_match = 0;
  641. switch (cc->ptype) {
  642. case ISDN_PTYPE_EURO:
  643. for (i = 0; i < 3; i++)
  644. if (!(strcmp(cc->s0num[i], cmd->parm.setup.phone)))
  645. num_match = 1;
  646. break;
  647. case ISDN_PTYPE_1TR6:
  648. e = cc->eazlist[ch];
  649. while (*e) {
  650. sprintf(nbuf, "%s%c", cc->s0num[0], *e);
  651. if (!(strcmp(nbuf, cmd->parm.setup.phone)))
  652. num_match = 1;
  653. e++;
  654. }
  655. }
  656. if (num_match) {
  657. spin_lock_irqsave(&card->isdnloop_lock, flags);
  658. /* channel idle? */
  659. if (!(cc->rcard[ch])) {
  660. /* Check SI */
  661. if (!(si2bit[cmd->parm.setup.si1] & cc->sil[ch])) {
  662. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  663. return 3;
  664. }
  665. /* ch is idle, si and number matches */
  666. cc->rcard[ch] = card;
  667. cc->rch[ch] = lch;
  668. card->rcard[lch] = cc;
  669. card->rch[lch] = ch;
  670. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  671. return 0;
  672. } else {
  673. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  674. /* num matches, but busy */
  675. if (ch == 1)
  676. return 1;
  677. }
  678. }
  679. }
  680. cc = cc->next;
  681. }
  682. return 2;
  683. }
  684. /*
  685. * Depending on D-channel protocol and caller/called, modify
  686. * phone number.
  687. *
  688. * Parameter:
  689. * card = pointer to card struct.
  690. * phone = pointer phone number.
  691. * caller = flag: 1 = caller, 0 = called.
  692. * Return:
  693. * pointer to new phone number.
  694. */
  695. static char *
  696. isdnloop_vstphone(isdnloop_card * card, char *phone, int caller)
  697. {
  698. int i;
  699. static char nphone[30];
  700. if (!card) {
  701. printk("BUG!!!\n");
  702. return "";
  703. }
  704. switch (card->ptype) {
  705. case ISDN_PTYPE_EURO:
  706. if (caller) {
  707. for (i = 0; i < 2; i++)
  708. if (!(strcmp(card->s0num[i], phone)))
  709. return (phone);
  710. return (card->s0num[0]);
  711. }
  712. return (phone);
  713. break;
  714. case ISDN_PTYPE_1TR6:
  715. if (caller) {
  716. sprintf(nphone, "%s%c", card->s0num[0], phone[0]);
  717. return (nphone);
  718. } else
  719. return (&phone[strlen(phone) - 1]);
  720. break;
  721. }
  722. return "";
  723. }
  724. /*
  725. * Parse an ICN-type command string sent to the 'card'.
  726. * Perform misc. actions depending on the command.
  727. *
  728. * Parameter:
  729. * card = pointer to card struct.
  730. */
  731. static void
  732. isdnloop_parse_cmd(isdnloop_card * card)
  733. {
  734. char *p = card->omsg;
  735. isdn_ctrl cmd;
  736. char buf[60];
  737. isdnloop_stat *s = isdnloop_cmd_table;
  738. int action = -1;
  739. int i;
  740. int ch;
  741. if ((card->omsg[0] != '0') && (card->omsg[2] != ';')) {
  742. isdnloop_fake_err(card);
  743. return;
  744. }
  745. ch = card->omsg[1] - '0';
  746. if ((ch < 0) || (ch > 2)) {
  747. isdnloop_fake_err(card);
  748. return;
  749. }
  750. p += 3;
  751. while (s->statstr) {
  752. if (!strncmp(p, s->statstr, strlen(s->statstr))) {
  753. action = s->action;
  754. if (s->command && (ch != 0)) {
  755. isdnloop_fake_err(card);
  756. return;
  757. }
  758. break;
  759. }
  760. s++;
  761. }
  762. if (action == -1)
  763. return;
  764. switch (action) {
  765. case 1:
  766. /* 0x;BCON_R */
  767. if (card->rcard[ch - 1]) {
  768. isdnloop_fake(card->rcard[ch - 1], "BCON_I",
  769. card->rch[ch - 1] + 1);
  770. isdnloop_fake(card, "BCON_C", ch);
  771. }
  772. break;
  773. case 17:
  774. /* 0x;BCON_I */
  775. if (card->rcard[ch - 1]) {
  776. isdnloop_fake(card->rcard[ch - 1], "BCON_C",
  777. card->rch[ch - 1] + 1);
  778. }
  779. break;
  780. case 2:
  781. /* 0x;BDIS_R */
  782. isdnloop_fake(card, "BDIS_C", ch);
  783. if (card->rcard[ch - 1]) {
  784. isdnloop_fake(card->rcard[ch - 1], "BDIS_I",
  785. card->rch[ch - 1] + 1);
  786. }
  787. break;
  788. case 16:
  789. /* 0x;DCON_R */
  790. isdnloop_kill_ctimer(card, ch - 1);
  791. if (card->rcard[ch - 1]) {
  792. isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
  793. isdnloop_fake(card->rcard[ch - 1], "DCON_C",
  794. card->rch[ch - 1] + 1);
  795. isdnloop_fake(card, "DCON_C", ch);
  796. }
  797. break;
  798. case 3:
  799. /* 0x;DDIS_R */
  800. isdnloop_kill_ctimer(card, ch - 1);
  801. if (card->rcard[ch - 1]) {
  802. isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
  803. isdnloop_fake(card->rcard[ch - 1], "DDIS_I",
  804. card->rch[ch - 1] + 1);
  805. card->rcard[ch - 1] = NULL;
  806. }
  807. isdnloop_fake(card, "DDIS_C", ch);
  808. break;
  809. case 4:
  810. /* 0x;DSCA_Rdd,yy,zz,oo */
  811. if (card->ptype != ISDN_PTYPE_1TR6) {
  812. isdnloop_fake_err(card);
  813. return;
  814. }
  815. /* Fall through */
  816. case 5:
  817. /* 0x;DCAL_Rdd,yy,zz,oo */
  818. p += 6;
  819. switch (isdnloop_try_call(card, p, ch - 1, &cmd)) {
  820. case 0:
  821. /* Alerting */
  822. sprintf(buf, "D%s_I%s,%02d,%02d,%s",
  823. (action == 4) ? "SCA" : "CAL",
  824. isdnloop_vstphone(card, cmd.parm.setup.eazmsn, 1),
  825. cmd.parm.setup.si1,
  826. cmd.parm.setup.si2,
  827. isdnloop_vstphone(card->rcard[ch - 1],
  828. cmd.parm.setup.phone, 0));
  829. isdnloop_fake(card->rcard[ch - 1], buf, card->rch[ch - 1] + 1);
  830. /* Fall through */
  831. case 3:
  832. /* si1 does not match, don't alert but start timer */
  833. isdnloop_start_ctimer(card, ch - 1);
  834. break;
  835. case 1:
  836. /* Remote busy */
  837. isdnloop_fake(card, "DDIS_I", ch);
  838. sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 1));
  839. isdnloop_fake(card, buf, ch);
  840. break;
  841. case 2:
  842. /* No such user */
  843. isdnloop_fake(card, "DDIS_I", ch);
  844. sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 2));
  845. isdnloop_fake(card, buf, ch);
  846. break;
  847. }
  848. break;
  849. case 6:
  850. /* 0x;EAZC */
  851. card->eazlist[ch - 1][0] = '\0';
  852. break;
  853. case 7:
  854. /* 0x;EAZ */
  855. p += 3;
  856. strcpy(card->eazlist[ch - 1], p);
  857. break;
  858. case 8:
  859. /* 0x;SEEAZ */
  860. sprintf(buf, "EAZ-LIST: %s", card->eazlist[ch - 1]);
  861. isdnloop_fake(card, buf, ch + 1);
  862. break;
  863. case 9:
  864. /* 0x;MSN */
  865. break;
  866. case 10:
  867. /* 0x;MSNALL */
  868. break;
  869. case 11:
  870. /* 0x;SETSIL */
  871. p += 6;
  872. i = 0;
  873. while (strchr("0157", *p)) {
  874. if (i)
  875. card->sil[ch - 1] |= si2bit[*p - '0'];
  876. i = (*p++ == '0');
  877. }
  878. if (*p)
  879. isdnloop_fake_err(card);
  880. break;
  881. case 12:
  882. /* 0x;SEESIL */
  883. sprintf(buf, "SIN-LIST: ");
  884. p = buf + 10;
  885. for (i = 0; i < 3; i++)
  886. if (card->sil[ch - 1] & (1 << i))
  887. p += sprintf(p, "%02d", bit2si[i]);
  888. isdnloop_fake(card, buf, ch + 1);
  889. break;
  890. case 13:
  891. /* 0x;SILC */
  892. card->sil[ch - 1] = 0;
  893. break;
  894. case 14:
  895. /* 00;FV2ON */
  896. break;
  897. case 15:
  898. /* 00;FV2OFF */
  899. break;
  900. }
  901. }
  902. /*
  903. * Put command-strings into the of the 'card'. In reality, execute them
  904. * right in place by calling isdnloop_parse_cmd(). Also copy every
  905. * command to the read message ringbuffer, preceeding it with a '>'.
  906. * These mesagges can be read at /dev/isdnctrl.
  907. *
  908. * Parameter:
  909. * buf = pointer to command buffer.
  910. * len = length of buffer data.
  911. * user = flag: 1 = called form userlevel, 0 called from kernel.
  912. * card = pointer to card struct.
  913. * Return:
  914. * number of bytes transferred (currently always equals len).
  915. */
  916. static int
  917. isdnloop_writecmd(const u_char * buf, int len, int user, isdnloop_card * card)
  918. {
  919. int xcount = 0;
  920. int ocount = 1;
  921. isdn_ctrl cmd;
  922. while (len) {
  923. int count = len;
  924. u_char *p;
  925. u_char msg[0x100];
  926. if (count > 255)
  927. count = 255;
  928. if (user) {
  929. if (copy_from_user(msg, buf, count))
  930. return -EFAULT;
  931. } else
  932. memcpy(msg, buf, count);
  933. isdnloop_putmsg(card, '>');
  934. for (p = msg; count > 0; count--, p++) {
  935. len--;
  936. xcount++;
  937. isdnloop_putmsg(card, *p);
  938. card->omsg[card->optr] = *p;
  939. if (*p == '\n') {
  940. card->omsg[card->optr] = '\0';
  941. card->optr = 0;
  942. isdnloop_parse_cmd(card);
  943. if (len) {
  944. isdnloop_putmsg(card, '>');
  945. ocount++;
  946. }
  947. } else {
  948. if (card->optr < 59)
  949. card->optr++;
  950. }
  951. ocount++;
  952. }
  953. }
  954. cmd.command = ISDN_STAT_STAVAIL;
  955. cmd.driver = card->myid;
  956. cmd.arg = ocount;
  957. card->interface.statcallb(&cmd);
  958. return xcount;
  959. }
  960. /*
  961. * Delete card's pending timers, send STOP to linklevel
  962. */
  963. static void
  964. isdnloop_stopcard(isdnloop_card * card)
  965. {
  966. unsigned long flags;
  967. isdn_ctrl cmd;
  968. spin_lock_irqsave(&card->isdnloop_lock, flags);
  969. if (card->flags & ISDNLOOP_FLAGS_RUNNING) {
  970. card->flags &= ~ISDNLOOP_FLAGS_RUNNING;
  971. del_timer(&card->st_timer);
  972. del_timer(&card->rb_timer);
  973. del_timer(&card->c_timer[0]);
  974. del_timer(&card->c_timer[1]);
  975. cmd.command = ISDN_STAT_STOP;
  976. cmd.driver = card->myid;
  977. card->interface.statcallb(&cmd);
  978. }
  979. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  980. }
  981. /*
  982. * Stop all cards before unload.
  983. */
  984. static void
  985. isdnloop_stopallcards(void)
  986. {
  987. isdnloop_card *p = cards;
  988. while (p) {
  989. isdnloop_stopcard(p);
  990. p = p->next;
  991. }
  992. }
  993. /*
  994. * Start a 'card'. Simulate card's boot message and set the phone
  995. * number(s) of the virtual 'S0-Interface'. Install D-channel
  996. * poll timer.
  997. *
  998. * Parameter:
  999. * card = pointer to card struct.
  1000. * sdefp = pointer to struct holding ioctl parameters.
  1001. * Return:
  1002. * 0 on success, -E??? otherwise.
  1003. */
  1004. static int
  1005. isdnloop_start(isdnloop_card * card, isdnloop_sdef * sdefp)
  1006. {
  1007. unsigned long flags;
  1008. isdnloop_sdef sdef;
  1009. int i;
  1010. if (card->flags & ISDNLOOP_FLAGS_RUNNING)
  1011. return -EBUSY;
  1012. if (copy_from_user((char *) &sdef, (char *) sdefp, sizeof(sdef)))
  1013. return -EFAULT;
  1014. spin_lock_irqsave(&card->isdnloop_lock, flags);
  1015. switch (sdef.ptype) {
  1016. case ISDN_PTYPE_EURO:
  1017. if (isdnloop_fake(card, "DRV1.23EC-Q.931-CAPI-CNS-BASIS-20.02.96",
  1018. -1)) {
  1019. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  1020. return -ENOMEM;
  1021. }
  1022. card->sil[0] = card->sil[1] = 4;
  1023. if (isdnloop_fake(card, "TEI OK", 0)) {
  1024. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  1025. return -ENOMEM;
  1026. }
  1027. for (i = 0; i < 3; i++)
  1028. strcpy(card->s0num[i], sdef.num[i]);
  1029. break;
  1030. case ISDN_PTYPE_1TR6:
  1031. if (isdnloop_fake(card, "DRV1.04TC-1TR6-CAPI-CNS-BASIS-29.11.95",
  1032. -1)) {
  1033. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  1034. return -ENOMEM;
  1035. }
  1036. card->sil[0] = card->sil[1] = 4;
  1037. if (isdnloop_fake(card, "TEI OK", 0)) {
  1038. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  1039. return -ENOMEM;
  1040. }
  1041. strcpy(card->s0num[0], sdef.num[0]);
  1042. card->s0num[1][0] = '\0';
  1043. card->s0num[2][0] = '\0';
  1044. break;
  1045. default:
  1046. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  1047. printk(KERN_WARNING "isdnloop: Illegal D-channel protocol %d\n",
  1048. sdef.ptype);
  1049. return -EINVAL;
  1050. }
  1051. init_timer(&card->st_timer);
  1052. card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
  1053. card->st_timer.function = isdnloop_polldchan;
  1054. card->st_timer.data = (unsigned long) card;
  1055. add_timer(&card->st_timer);
  1056. card->flags |= ISDNLOOP_FLAGS_RUNNING;
  1057. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  1058. return 0;
  1059. }
  1060. /*
  1061. * Main handler for commands sent by linklevel.
  1062. */
  1063. static int
  1064. isdnloop_command(isdn_ctrl * c, isdnloop_card * card)
  1065. {
  1066. ulong a;
  1067. int i;
  1068. char cbuf[60];
  1069. isdn_ctrl cmd;
  1070. isdnloop_cdef cdef;
  1071. switch (c->command) {
  1072. case ISDN_CMD_IOCTL:
  1073. memcpy(&a, c->parm.num, sizeof(ulong));
  1074. switch (c->arg) {
  1075. case ISDNLOOP_IOCTL_DEBUGVAR:
  1076. return (ulong) card;
  1077. case ISDNLOOP_IOCTL_STARTUP:
  1078. if (!access_ok(VERIFY_READ, (void *) a, sizeof(isdnloop_sdef)))
  1079. return -EFAULT;
  1080. return (isdnloop_start(card, (isdnloop_sdef *) a));
  1081. break;
  1082. case ISDNLOOP_IOCTL_ADDCARD:
  1083. if (copy_from_user((char *)&cdef,
  1084. (char *)a,
  1085. sizeof(cdef)))
  1086. return -EFAULT;
  1087. return (isdnloop_addcard(cdef.id1));
  1088. break;
  1089. case ISDNLOOP_IOCTL_LEASEDCFG:
  1090. if (a) {
  1091. if (!card->leased) {
  1092. card->leased = 1;
  1093. while (card->ptype == ISDN_PTYPE_UNKNOWN)
  1094. schedule_timeout_interruptible(10);
  1095. schedule_timeout_interruptible(10);
  1096. sprintf(cbuf, "00;FV2ON\n01;EAZ1\n02;EAZ2\n");
  1097. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1098. printk(KERN_INFO
  1099. "isdnloop: (%s) Leased-line mode enabled\n",
  1100. CID);
  1101. cmd.command = ISDN_STAT_RUN;
  1102. cmd.driver = card->myid;
  1103. cmd.arg = 0;
  1104. card->interface.statcallb(&cmd);
  1105. }
  1106. } else {
  1107. if (card->leased) {
  1108. card->leased = 0;
  1109. sprintf(cbuf, "00;FV2OFF\n");
  1110. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1111. printk(KERN_INFO
  1112. "isdnloop: (%s) Leased-line mode disabled\n",
  1113. CID);
  1114. cmd.command = ISDN_STAT_RUN;
  1115. cmd.driver = card->myid;
  1116. cmd.arg = 0;
  1117. card->interface.statcallb(&cmd);
  1118. }
  1119. }
  1120. return 0;
  1121. default:
  1122. return -EINVAL;
  1123. }
  1124. break;
  1125. case ISDN_CMD_DIAL:
  1126. if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
  1127. return -ENODEV;
  1128. if (card->leased)
  1129. break;
  1130. if ((c->arg & 255) < ISDNLOOP_BCH) {
  1131. char *p;
  1132. char dial[50];
  1133. char dcode[4];
  1134. a = c->arg;
  1135. p = c->parm.setup.phone;
  1136. if (*p == 's' || *p == 'S') {
  1137. /* Dial for SPV */
  1138. p++;
  1139. strcpy(dcode, "SCA");
  1140. } else
  1141. /* Normal Dial */
  1142. strcpy(dcode, "CAL");
  1143. strcpy(dial, p);
  1144. sprintf(cbuf, "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a + 1),
  1145. dcode, dial, c->parm.setup.si1,
  1146. c->parm.setup.si2, c->parm.setup.eazmsn);
  1147. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1148. }
  1149. break;
  1150. case ISDN_CMD_ACCEPTD:
  1151. if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
  1152. return -ENODEV;
  1153. if (c->arg < ISDNLOOP_BCH) {
  1154. a = c->arg + 1;
  1155. cbuf[0] = 0;
  1156. switch (card->l2_proto[a - 1]) {
  1157. case ISDN_PROTO_L2_X75I:
  1158. sprintf(cbuf, "%02d;BX75\n", (int) a);
  1159. break;
  1160. #ifdef CONFIG_ISDN_X25
  1161. case ISDN_PROTO_L2_X25DTE:
  1162. sprintf(cbuf, "%02d;BX2T\n", (int) a);
  1163. break;
  1164. case ISDN_PROTO_L2_X25DCE:
  1165. sprintf(cbuf, "%02d;BX2C\n", (int) a);
  1166. break;
  1167. #endif
  1168. case ISDN_PROTO_L2_HDLC:
  1169. sprintf(cbuf, "%02d;BTRA\n", (int) a);
  1170. break;
  1171. }
  1172. if (strlen(cbuf))
  1173. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1174. sprintf(cbuf, "%02d;DCON_R\n", (int) a);
  1175. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1176. }
  1177. break;
  1178. case ISDN_CMD_ACCEPTB:
  1179. if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
  1180. return -ENODEV;
  1181. if (c->arg < ISDNLOOP_BCH) {
  1182. a = c->arg + 1;
  1183. switch (card->l2_proto[a - 1]) {
  1184. case ISDN_PROTO_L2_X75I:
  1185. sprintf(cbuf, "%02d;BCON_R,BX75\n", (int) a);
  1186. break;
  1187. #ifdef CONFIG_ISDN_X25
  1188. case ISDN_PROTO_L2_X25DTE:
  1189. sprintf(cbuf, "%02d;BCON_R,BX2T\n", (int) a);
  1190. break;
  1191. case ISDN_PROTO_L2_X25DCE:
  1192. sprintf(cbuf, "%02d;BCON_R,BX2C\n", (int) a);
  1193. break;
  1194. #endif
  1195. case ISDN_PROTO_L2_HDLC:
  1196. sprintf(cbuf, "%02d;BCON_R,BTRA\n", (int) a);
  1197. break;
  1198. default:
  1199. sprintf(cbuf, "%02d;BCON_R\n", (int) a);
  1200. }
  1201. printk(KERN_DEBUG "isdnloop writecmd '%s'\n", cbuf);
  1202. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1203. break;
  1204. case ISDN_CMD_HANGUP:
  1205. if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
  1206. return -ENODEV;
  1207. if (c->arg < ISDNLOOP_BCH) {
  1208. a = c->arg + 1;
  1209. sprintf(cbuf, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a, (int) a);
  1210. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1211. }
  1212. break;
  1213. case ISDN_CMD_SETEAZ:
  1214. if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
  1215. return -ENODEV;
  1216. if (card->leased)
  1217. break;
  1218. if (c->arg < ISDNLOOP_BCH) {
  1219. a = c->arg + 1;
  1220. if (card->ptype == ISDN_PTYPE_EURO) {
  1221. sprintf(cbuf, "%02d;MS%s%s\n", (int) a,
  1222. c->parm.num[0] ? "N" : "ALL", c->parm.num);
  1223. } else
  1224. sprintf(cbuf, "%02d;EAZ%s\n", (int) a,
  1225. c->parm.num[0] ? c->parm.num : (u_char *) "0123456789");
  1226. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1227. }
  1228. break;
  1229. case ISDN_CMD_CLREAZ:
  1230. if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
  1231. return -ENODEV;
  1232. if (card->leased)
  1233. break;
  1234. if (c->arg < ISDNLOOP_BCH) {
  1235. a = c->arg + 1;
  1236. if (card->ptype == ISDN_PTYPE_EURO)
  1237. sprintf(cbuf, "%02d;MSNC\n", (int) a);
  1238. else
  1239. sprintf(cbuf, "%02d;EAZC\n", (int) a);
  1240. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1241. }
  1242. break;
  1243. case ISDN_CMD_SETL2:
  1244. if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
  1245. return -ENODEV;
  1246. if ((c->arg & 255) < ISDNLOOP_BCH) {
  1247. a = c->arg;
  1248. switch (a >> 8) {
  1249. case ISDN_PROTO_L2_X75I:
  1250. sprintf(cbuf, "%02d;BX75\n", (int) (a & 255) + 1);
  1251. break;
  1252. #ifdef CONFIG_ISDN_X25
  1253. case ISDN_PROTO_L2_X25DTE:
  1254. sprintf(cbuf, "%02d;BX2T\n", (int) (a & 255) + 1);
  1255. break;
  1256. case ISDN_PROTO_L2_X25DCE:
  1257. sprintf(cbuf, "%02d;BX2C\n", (int) (a & 255) + 1);
  1258. break;
  1259. #endif
  1260. case ISDN_PROTO_L2_HDLC:
  1261. sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
  1262. break;
  1263. case ISDN_PROTO_L2_TRANS:
  1264. sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
  1265. break;
  1266. default:
  1267. return -EINVAL;
  1268. }
  1269. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1270. card->l2_proto[a & 255] = (a >> 8);
  1271. }
  1272. break;
  1273. case ISDN_CMD_SETL3:
  1274. if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
  1275. return -ENODEV;
  1276. return 0;
  1277. default:
  1278. return -EINVAL;
  1279. }
  1280. }
  1281. return 0;
  1282. }
  1283. /*
  1284. * Find card with given driverId
  1285. */
  1286. static inline isdnloop_card *
  1287. isdnloop_findcard(int driverid)
  1288. {
  1289. isdnloop_card *p = cards;
  1290. while (p) {
  1291. if (p->myid == driverid)
  1292. return p;
  1293. p = p->next;
  1294. }
  1295. return (isdnloop_card *) 0;
  1296. }
  1297. /*
  1298. * Wrapper functions for interface to linklevel
  1299. */
  1300. static int
  1301. if_command(isdn_ctrl * c)
  1302. {
  1303. isdnloop_card *card = isdnloop_findcard(c->driver);
  1304. if (card)
  1305. return (isdnloop_command(c, card));
  1306. printk(KERN_ERR
  1307. "isdnloop: if_command called with invalid driverId!\n");
  1308. return -ENODEV;
  1309. }
  1310. static int
  1311. if_writecmd(const u_char __user *buf, int len, int id, int channel)
  1312. {
  1313. isdnloop_card *card = isdnloop_findcard(id);
  1314. if (card) {
  1315. if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
  1316. return -ENODEV;
  1317. return (isdnloop_writecmd(buf, len, 1, card));
  1318. }
  1319. printk(KERN_ERR
  1320. "isdnloop: if_writecmd called with invalid driverId!\n");
  1321. return -ENODEV;
  1322. }
  1323. static int
  1324. if_readstatus(u_char __user *buf, int len, int id, int channel)
  1325. {
  1326. isdnloop_card *card = isdnloop_findcard(id);
  1327. if (card) {
  1328. if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
  1329. return -ENODEV;
  1330. return (isdnloop_readstatus(buf, len, card));
  1331. }
  1332. printk(KERN_ERR
  1333. "isdnloop: if_readstatus called with invalid driverId!\n");
  1334. return -ENODEV;
  1335. }
  1336. static int
  1337. if_sendbuf(int id, int channel, int ack, struct sk_buff *skb)
  1338. {
  1339. isdnloop_card *card = isdnloop_findcard(id);
  1340. if (card) {
  1341. if (!card->flags & ISDNLOOP_FLAGS_RUNNING)
  1342. return -ENODEV;
  1343. /* ack request stored in skb scratch area */
  1344. *(skb->head) = ack;
  1345. return (isdnloop_sendbuf(channel, skb, card));
  1346. }
  1347. printk(KERN_ERR
  1348. "isdnloop: if_sendbuf called with invalid driverId!\n");
  1349. return -ENODEV;
  1350. }
  1351. /*
  1352. * Allocate a new card-struct, initialize it
  1353. * link it into cards-list and register it at linklevel.
  1354. */
  1355. static isdnloop_card *
  1356. isdnloop_initcard(char *id)
  1357. {
  1358. isdnloop_card *card;
  1359. int i;
  1360. if (!(card = (isdnloop_card *) kmalloc(sizeof(isdnloop_card), GFP_KERNEL))) {
  1361. printk(KERN_WARNING
  1362. "isdnloop: (%s) Could not allocate card-struct.\n", id);
  1363. return (isdnloop_card *) 0;
  1364. }
  1365. memset((char *) card, 0, sizeof(isdnloop_card));
  1366. card->interface.owner = THIS_MODULE;
  1367. card->interface.channels = ISDNLOOP_BCH;
  1368. card->interface.hl_hdrlen = 1; /* scratch area for storing ack flag*/
  1369. card->interface.maxbufsize = 4000;
  1370. card->interface.command = if_command;
  1371. card->interface.writebuf_skb = if_sendbuf;
  1372. card->interface.writecmd = if_writecmd;
  1373. card->interface.readstat = if_readstatus;
  1374. card->interface.features = ISDN_FEATURE_L2_X75I |
  1375. #ifdef CONFIG_ISDN_X25
  1376. ISDN_FEATURE_L2_X25DTE |
  1377. ISDN_FEATURE_L2_X25DCE |
  1378. #endif
  1379. ISDN_FEATURE_L2_HDLC |
  1380. ISDN_FEATURE_L3_TRANS |
  1381. ISDN_FEATURE_P_UNKNOWN;
  1382. card->ptype = ISDN_PTYPE_UNKNOWN;
  1383. strlcpy(card->interface.id, id, sizeof(card->interface.id));
  1384. card->msg_buf_write = card->msg_buf;
  1385. card->msg_buf_read = card->msg_buf;
  1386. card->msg_buf_end = &card->msg_buf[sizeof(card->msg_buf) - 1];
  1387. for (i = 0; i < ISDNLOOP_BCH; i++) {
  1388. card->l2_proto[i] = ISDN_PROTO_L2_X75I;
  1389. skb_queue_head_init(&card->bqueue[i]);
  1390. }
  1391. skb_queue_head_init(&card->dqueue);
  1392. card->isdnloop_lock = SPIN_LOCK_UNLOCKED;
  1393. card->next = cards;
  1394. cards = card;
  1395. if (!register_isdn(&card->interface)) {
  1396. cards = cards->next;
  1397. printk(KERN_WARNING
  1398. "isdnloop: Unable to register %s\n", id);
  1399. kfree(card);
  1400. return (isdnloop_card *) 0;
  1401. }
  1402. card->myid = card->interface.channels;
  1403. return card;
  1404. }
  1405. static int
  1406. isdnloop_addcard(char *id1)
  1407. {
  1408. isdnloop_card *card;
  1409. if (!(card = isdnloop_initcard(id1))) {
  1410. return -EIO;
  1411. }
  1412. printk(KERN_INFO
  1413. "isdnloop: (%s) virtual card added\n",
  1414. card->interface.id);
  1415. return 0;
  1416. }
  1417. static int __init
  1418. isdnloop_init(void)
  1419. {
  1420. char *p;
  1421. char rev[10];
  1422. if ((p = strchr(revision, ':'))) {
  1423. strcpy(rev, p + 1);
  1424. p = strchr(rev, '$');
  1425. *p = 0;
  1426. } else
  1427. strcpy(rev, " ??? ");
  1428. printk(KERN_NOTICE "isdnloop-ISDN-driver Rev%s\n", rev);
  1429. if (isdnloop_id)
  1430. return (isdnloop_addcard(isdnloop_id));
  1431. return 0;
  1432. }
  1433. static void __exit
  1434. isdnloop_exit(void)
  1435. {
  1436. isdn_ctrl cmd;
  1437. isdnloop_card *card = cards;
  1438. isdnloop_card *last;
  1439. int i;
  1440. isdnloop_stopallcards();
  1441. while (card) {
  1442. cmd.command = ISDN_STAT_UNLOAD;
  1443. cmd.driver = card->myid;
  1444. card->interface.statcallb(&cmd);
  1445. for (i = 0; i < ISDNLOOP_BCH; i++)
  1446. isdnloop_free_queue(card, i);
  1447. card = card->next;
  1448. }
  1449. card = cards;
  1450. while (card) {
  1451. last = card;
  1452. skb_queue_purge(&card->dqueue);
  1453. card = card->next;
  1454. kfree(last);
  1455. }
  1456. printk(KERN_NOTICE "isdnloop-ISDN-driver unloaded\n");
  1457. }
  1458. module_init(isdnloop_init);
  1459. module_exit(isdnloop_exit);