isdnloop.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/sched.h>
  16. #include "isdnloop.h"
  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. skb = skb_dequeue(&card->bqueue[ch]);
  55. if (skb) {
  56. len = skb->len;
  57. card->sndcount[ch] -= len;
  58. ack = *(skb->head); /* used as scratch area */
  59. cmd.driver = card->myid;
  60. cmd.arg = ch;
  61. if (rcard) {
  62. rcard->interface.rcvcallb_skb(rcard->myid, rch, skb);
  63. } else {
  64. printk(KERN_WARNING "isdnloop: no rcard, skb dropped\n");
  65. dev_kfree_skb(skb);
  66. };
  67. cmd.command = ISDN_STAT_BSENT;
  68. cmd.parm.length = len;
  69. card->interface.statcallb(&cmd);
  70. } else
  71. card->sndcount[ch] = 0;
  72. }
  73. }
  74. /*
  75. * Send/Receive Data to/from the B-Channel.
  76. * This routine is called via timer-callback.
  77. * It schedules itself while any B-Channel is open.
  78. *
  79. * Parameter:
  80. * data = pointer to card struct, set by kernel timer.data
  81. */
  82. static void
  83. isdnloop_pollbchan(unsigned long data)
  84. {
  85. isdnloop_card *card = (isdnloop_card *) data;
  86. unsigned long flags;
  87. if (card->flags & ISDNLOOP_FLAGS_B1ACTIVE)
  88. isdnloop_bchan_send(card, 0);
  89. if (card->flags & ISDNLOOP_FLAGS_B2ACTIVE)
  90. isdnloop_bchan_send(card, 1);
  91. if (card->flags & (ISDNLOOP_FLAGS_B1ACTIVE | ISDNLOOP_FLAGS_B2ACTIVE)) {
  92. /* schedule b-channel polling again */
  93. spin_lock_irqsave(&card->isdnloop_lock, flags);
  94. card->rb_timer.expires = jiffies + ISDNLOOP_TIMER_BCREAD;
  95. add_timer(&card->rb_timer);
  96. card->flags |= ISDNLOOP_FLAGS_RBTIMER;
  97. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  98. } else
  99. card->flags &= ~ISDNLOOP_FLAGS_RBTIMER;
  100. }
  101. /*
  102. * Parse ICN-type setup string and fill fields of setup-struct
  103. * with parsed data.
  104. *
  105. * Parameter:
  106. * setup = setup string, format: [caller-id],si1,si2,[called-id]
  107. * cmd = pointer to struct to be filled.
  108. */
  109. static void
  110. isdnloop_parse_setup(char *setup, isdn_ctrl *cmd)
  111. {
  112. char *t = setup;
  113. char *s = strchr(t, ',');
  114. *s++ = '\0';
  115. strlcpy(cmd->parm.setup.phone, t, sizeof(cmd->parm.setup.phone));
  116. s = strchr(t = s, ',');
  117. *s++ = '\0';
  118. if (!strlen(t))
  119. cmd->parm.setup.si1 = 0;
  120. else
  121. cmd->parm.setup.si1 = simple_strtoul(t, NULL, 10);
  122. s = strchr(t = s, ',');
  123. *s++ = '\0';
  124. if (!strlen(t))
  125. cmd->parm.setup.si2 = 0;
  126. else
  127. cmd->parm.setup.si2 =
  128. simple_strtoul(t, NULL, 10);
  129. strlcpy(cmd->parm.setup.eazmsn, s, sizeof(cmd->parm.setup.eazmsn));
  130. cmd->parm.setup.plan = 0;
  131. cmd->parm.setup.screen = 0;
  132. }
  133. typedef struct isdnloop_stat {
  134. char *statstr;
  135. int command;
  136. int action;
  137. } isdnloop_stat;
  138. /* *INDENT-OFF* */
  139. static isdnloop_stat isdnloop_stat_table[] = {
  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. skb = skb_dequeue(&card->dqueue);
  300. if (skb)
  301. avail = skb->len;
  302. else
  303. avail = 0;
  304. for (left = avail; left > 0; left--) {
  305. c = *skb->data;
  306. skb_pull(skb, 1);
  307. isdnloop_putmsg(card, c);
  308. card->imsg[card->iptr] = c;
  309. if (card->iptr < 59)
  310. card->iptr++;
  311. if (!skb->len) {
  312. avail++;
  313. isdnloop_putmsg(card, '\n');
  314. card->imsg[card->iptr] = 0;
  315. card->iptr = 0;
  316. if (card->imsg[0] == '0' && card->imsg[1] >= '0' &&
  317. card->imsg[1] <= '2' && card->imsg[2] == ';') {
  318. ch = (card->imsg[1] - '0') - 1;
  319. p = &card->imsg[3];
  320. isdnloop_parse_status(p, ch, card);
  321. } else {
  322. p = card->imsg;
  323. if (!strncmp(p, "DRV1.", 5)) {
  324. printk(KERN_INFO "isdnloop: (%s) %s\n", CID, p);
  325. if (!strncmp(p + 7, "TC", 2)) {
  326. card->ptype = ISDN_PTYPE_1TR6;
  327. card->interface.features |= ISDN_FEATURE_P_1TR6;
  328. printk(KERN_INFO
  329. "isdnloop: (%s) 1TR6-Protocol loaded and running\n", CID);
  330. }
  331. if (!strncmp(p + 7, "EC", 2)) {
  332. card->ptype = ISDN_PTYPE_EURO;
  333. card->interface.features |= ISDN_FEATURE_P_EURO;
  334. printk(KERN_INFO
  335. "isdnloop: (%s) Euro-Protocol loaded and running\n", CID);
  336. }
  337. continue;
  338. }
  339. }
  340. }
  341. }
  342. if (avail) {
  343. cmd.command = ISDN_STAT_STAVAIL;
  344. cmd.driver = card->myid;
  345. cmd.arg = avail;
  346. card->interface.statcallb(&cmd);
  347. }
  348. if (card->flags & (ISDNLOOP_FLAGS_B1ACTIVE | ISDNLOOP_FLAGS_B2ACTIVE))
  349. if (!(card->flags & ISDNLOOP_FLAGS_RBTIMER)) {
  350. /* schedule b-channel polling */
  351. card->flags |= ISDNLOOP_FLAGS_RBTIMER;
  352. spin_lock_irqsave(&card->isdnloop_lock, flags);
  353. del_timer(&card->rb_timer);
  354. card->rb_timer.function = isdnloop_pollbchan;
  355. card->rb_timer.data = (unsigned long) card;
  356. card->rb_timer.expires = jiffies + ISDNLOOP_TIMER_BCREAD;
  357. add_timer(&card->rb_timer);
  358. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  359. }
  360. /* schedule again */
  361. spin_lock_irqsave(&card->isdnloop_lock, flags);
  362. card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
  363. add_timer(&card->st_timer);
  364. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  365. }
  366. /*
  367. * Append a packet to the transmit buffer-queue.
  368. *
  369. * Parameter:
  370. * channel = Number of B-channel
  371. * skb = packet to send.
  372. * card = pointer to card-struct
  373. * Return:
  374. * Number of bytes transferred, -E??? on error
  375. */
  376. static int
  377. isdnloop_sendbuf(int channel, struct sk_buff *skb, isdnloop_card *card)
  378. {
  379. int len = skb->len;
  380. unsigned long flags;
  381. struct sk_buff *nskb;
  382. if (len > 4000) {
  383. printk(KERN_WARNING
  384. "isdnloop: Send packet too large\n");
  385. return -EINVAL;
  386. }
  387. if (len) {
  388. if (!(card->flags & (channel ? ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE)))
  389. return 0;
  390. if (card->sndcount[channel] > ISDNLOOP_MAX_SQUEUE)
  391. return 0;
  392. spin_lock_irqsave(&card->isdnloop_lock, flags);
  393. nskb = dev_alloc_skb(skb->len);
  394. if (nskb) {
  395. skb_copy_from_linear_data(skb,
  396. skb_put(nskb, len), len);
  397. skb_queue_tail(&card->bqueue[channel], nskb);
  398. dev_kfree_skb(skb);
  399. } else
  400. len = 0;
  401. card->sndcount[channel] += len;
  402. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  403. }
  404. return len;
  405. }
  406. /*
  407. * Read the messages from the card's ringbuffer
  408. *
  409. * Parameter:
  410. * buf = pointer to buffer.
  411. * len = number of bytes to read.
  412. * user = flag, 1: called from userlevel 0: called from kernel.
  413. * card = pointer to card struct.
  414. * Return:
  415. * number of bytes actually transferred.
  416. */
  417. static int
  418. isdnloop_readstatus(u_char __user *buf, int len, isdnloop_card *card)
  419. {
  420. int count;
  421. u_char __user *p;
  422. for (p = buf, count = 0; count < len; p++, count++) {
  423. if (card->msg_buf_read == card->msg_buf_write)
  424. return count;
  425. if (put_user(*card->msg_buf_read++, p))
  426. return -EFAULT;
  427. if (card->msg_buf_read > card->msg_buf_end)
  428. card->msg_buf_read = card->msg_buf;
  429. }
  430. return count;
  431. }
  432. /*
  433. * Simulate a card's response by appending it to the cards
  434. * message queue.
  435. *
  436. * Parameter:
  437. * card = pointer to card struct.
  438. * s = pointer to message-string.
  439. * ch = channel: 0 = generic messages, 1 and 2 = D-channel messages.
  440. * Return:
  441. * 0 on success, 1 on memory squeeze.
  442. */
  443. static int
  444. isdnloop_fake(isdnloop_card *card, char *s, int ch)
  445. {
  446. struct sk_buff *skb;
  447. int len = strlen(s) + ((ch >= 0) ? 3 : 0);
  448. skb = dev_alloc_skb(len);
  449. if (!skb) {
  450. printk(KERN_WARNING "isdnloop: Out of memory in isdnloop_fake\n");
  451. return 1;
  452. }
  453. if (ch >= 0)
  454. sprintf(skb_put(skb, 3), "%02d;", ch);
  455. skb_put_data(skb, s, strlen(s));
  456. skb_queue_tail(&card->dqueue, skb);
  457. return 0;
  458. }
  459. /* *INDENT-OFF* */
  460. static isdnloop_stat isdnloop_cmd_table[] = {
  461. {"BCON_R", 0, 1}, /* B-Channel connect */
  462. {"BCON_I", 0, 17}, /* B-Channel connect ind */
  463. {"BDIS_R", 0, 2}, /* B-Channel disconnect */
  464. {"DDIS_R", 0, 3}, /* D-Channel disconnect */
  465. {"DCON_R", 0, 16}, /* D-Channel connect */
  466. {"DSCA_R", 0, 4}, /* Dial 1TR6-SPV */
  467. {"DCAL_R", 0, 5}, /* Dial */
  468. {"EAZC", 0, 6}, /* Clear EAZ listener */
  469. {"EAZ", 0, 7}, /* Set EAZ listener */
  470. {"SEEAZ", 0, 8}, /* Get EAZ listener */
  471. {"MSN", 0, 9}, /* Set/Clear MSN listener */
  472. {"MSALL", 0, 10}, /* Set multi MSN listeners */
  473. {"SETSIL", 0, 11}, /* Set SI list */
  474. {"SEESIL", 0, 12}, /* Get SI list */
  475. {"SILC", 0, 13}, /* Clear SI list */
  476. {"LOCK", 0, -1}, /* LOCK channel */
  477. {"UNLOCK", 0, -1}, /* UNLOCK channel */
  478. {"FV2ON", 1, 14}, /* Leased mode on */
  479. {"FV2OFF", 1, 15}, /* Leased mode off */
  480. {NULL, 0, -1}
  481. };
  482. /* *INDENT-ON* */
  483. /*
  484. * Simulate an error-response from a card.
  485. *
  486. * Parameter:
  487. * card = pointer to card struct.
  488. */
  489. static void
  490. isdnloop_fake_err(isdnloop_card *card)
  491. {
  492. char buf[64];
  493. snprintf(buf, sizeof(buf), "E%s", card->omsg);
  494. isdnloop_fake(card, buf, -1);
  495. isdnloop_fake(card, "NAK", -1);
  496. }
  497. static u_char ctable_eu[] = {0x00, 0x11, 0x01, 0x12};
  498. static u_char ctable_1t[] = {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[] = {0, 1, 0, 0, 0, 2, 0, 4, 0, 0};
  607. static u_char bit2si[] = {1, 5, 7};
  608. /*
  609. * Try finding a listener for an outgoing call.
  610. *
  611. * Parameter:
  612. * card = pointer to calling card.
  613. * p = pointer to ICN-type setup-string.
  614. * lch = channel of calling card.
  615. * cmd = pointer to struct to be filled when parsing setup.
  616. * Return:
  617. * 0 = found match, alerting should happen.
  618. * 1 = found matching number but it is busy.
  619. * 2 = no matching listener.
  620. * 3 = found matching number but SI does not match.
  621. */
  622. static int
  623. isdnloop_try_call(isdnloop_card *card, char *p, int lch, isdn_ctrl *cmd)
  624. {
  625. isdnloop_card *cc = cards;
  626. unsigned long flags;
  627. int ch;
  628. int num_match;
  629. int i;
  630. char *e;
  631. char nbuf[32];
  632. isdnloop_parse_setup(p, cmd);
  633. while (cc) {
  634. for (ch = 0; ch < 2; ch++) {
  635. /* Exclude ourself */
  636. if ((cc == card) && (ch == lch))
  637. continue;
  638. num_match = 0;
  639. switch (cc->ptype) {
  640. case ISDN_PTYPE_EURO:
  641. for (i = 0; i < 3; i++)
  642. if (!(strcmp(cc->s0num[i], cmd->parm.setup.phone)))
  643. num_match = 1;
  644. break;
  645. case ISDN_PTYPE_1TR6:
  646. e = cc->eazlist[ch];
  647. while (*e) {
  648. sprintf(nbuf, "%s%c", cc->s0num[0], *e);
  649. if (!(strcmp(nbuf, cmd->parm.setup.phone)))
  650. num_match = 1;
  651. e++;
  652. }
  653. }
  654. if (num_match) {
  655. spin_lock_irqsave(&card->isdnloop_lock, flags);
  656. /* channel idle? */
  657. if (!(cc->rcard[ch])) {
  658. /* Check SI */
  659. if (!(si2bit[cmd->parm.setup.si1] & cc->sil[ch])) {
  660. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  661. return 3;
  662. }
  663. /* ch is idle, si and number matches */
  664. cc->rcard[ch] = card;
  665. cc->rch[ch] = lch;
  666. card->rcard[lch] = cc;
  667. card->rch[lch] = ch;
  668. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  669. return 0;
  670. } else {
  671. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  672. /* num matches, but busy */
  673. if (ch == 1)
  674. return 1;
  675. }
  676. }
  677. }
  678. cc = cc->next;
  679. }
  680. return 2;
  681. }
  682. /*
  683. * Depending on D-channel protocol and caller/called, modify
  684. * phone number.
  685. *
  686. * Parameter:
  687. * card = pointer to card struct.
  688. * phone = pointer phone number.
  689. * caller = flag: 1 = caller, 0 = called.
  690. * Return:
  691. * pointer to new phone number.
  692. */
  693. static char *
  694. isdnloop_vstphone(isdnloop_card *card, char *phone, int caller)
  695. {
  696. int i;
  697. static char nphone[30];
  698. if (!card) {
  699. printk("BUG!!!\n");
  700. return "";
  701. }
  702. switch (card->ptype) {
  703. case ISDN_PTYPE_EURO:
  704. if (caller) {
  705. for (i = 0; i < 2; i++)
  706. if (!(strcmp(card->s0num[i], phone)))
  707. return phone;
  708. return card->s0num[0];
  709. }
  710. return phone;
  711. break;
  712. case ISDN_PTYPE_1TR6:
  713. if (caller) {
  714. sprintf(nphone, "%s%c", card->s0num[0], phone[0]);
  715. return nphone;
  716. } else
  717. return &phone[strlen(phone) - 1];
  718. break;
  719. }
  720. return "";
  721. }
  722. /*
  723. * Parse an ICN-type command string sent to the 'card'.
  724. * Perform misc. actions depending on the command.
  725. *
  726. * Parameter:
  727. * card = pointer to card struct.
  728. */
  729. static void
  730. isdnloop_parse_cmd(isdnloop_card *card)
  731. {
  732. char *p = card->omsg;
  733. isdn_ctrl cmd;
  734. char buf[60];
  735. isdnloop_stat *s = isdnloop_cmd_table;
  736. int action = -1;
  737. int i;
  738. int ch;
  739. if ((card->omsg[0] != '0') && (card->omsg[2] != ';')) {
  740. isdnloop_fake_err(card);
  741. return;
  742. }
  743. ch = card->omsg[1] - '0';
  744. if ((ch < 0) || (ch > 2)) {
  745. isdnloop_fake_err(card);
  746. return;
  747. }
  748. p += 3;
  749. while (s->statstr) {
  750. if (!strncmp(p, s->statstr, strlen(s->statstr))) {
  751. action = s->action;
  752. if (s->command && (ch != 0)) {
  753. isdnloop_fake_err(card);
  754. return;
  755. }
  756. break;
  757. }
  758. s++;
  759. }
  760. if (action == -1)
  761. return;
  762. switch (action) {
  763. case 1:
  764. /* 0x;BCON_R */
  765. if (card->rcard[ch - 1]) {
  766. isdnloop_fake(card->rcard[ch - 1], "BCON_I",
  767. card->rch[ch - 1] + 1);
  768. isdnloop_fake(card, "BCON_C", ch);
  769. }
  770. break;
  771. case 17:
  772. /* 0x;BCON_I */
  773. if (card->rcard[ch - 1]) {
  774. isdnloop_fake(card->rcard[ch - 1], "BCON_C",
  775. card->rch[ch - 1] + 1);
  776. }
  777. break;
  778. case 2:
  779. /* 0x;BDIS_R */
  780. isdnloop_fake(card, "BDIS_C", ch);
  781. if (card->rcard[ch - 1]) {
  782. isdnloop_fake(card->rcard[ch - 1], "BDIS_I",
  783. card->rch[ch - 1] + 1);
  784. }
  785. break;
  786. case 16:
  787. /* 0x;DCON_R */
  788. isdnloop_kill_ctimer(card, ch - 1);
  789. if (card->rcard[ch - 1]) {
  790. isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
  791. isdnloop_fake(card->rcard[ch - 1], "DCON_C",
  792. card->rch[ch - 1] + 1);
  793. isdnloop_fake(card, "DCON_C", ch);
  794. }
  795. break;
  796. case 3:
  797. /* 0x;DDIS_R */
  798. isdnloop_kill_ctimer(card, ch - 1);
  799. if (card->rcard[ch - 1]) {
  800. isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
  801. isdnloop_fake(card->rcard[ch - 1], "DDIS_I",
  802. card->rch[ch - 1] + 1);
  803. card->rcard[ch - 1] = NULL;
  804. }
  805. isdnloop_fake(card, "DDIS_C", ch);
  806. break;
  807. case 4:
  808. /* 0x;DSCA_Rdd,yy,zz,oo */
  809. if (card->ptype != ISDN_PTYPE_1TR6) {
  810. isdnloop_fake_err(card);
  811. return;
  812. }
  813. /* Fall through */
  814. case 5:
  815. /* 0x;DCAL_Rdd,yy,zz,oo */
  816. p += 6;
  817. switch (isdnloop_try_call(card, p, ch - 1, &cmd)) {
  818. case 0:
  819. /* Alerting */
  820. sprintf(buf, "D%s_I%s,%02d,%02d,%s",
  821. (action == 4) ? "SCA" : "CAL",
  822. isdnloop_vstphone(card, cmd.parm.setup.eazmsn, 1),
  823. cmd.parm.setup.si1,
  824. cmd.parm.setup.si2,
  825. isdnloop_vstphone(card->rcard[ch - 1],
  826. cmd.parm.setup.phone, 0));
  827. isdnloop_fake(card->rcard[ch - 1], buf, card->rch[ch - 1] + 1);
  828. /* Fall through */
  829. case 3:
  830. /* si1 does not match, don't alert but start timer */
  831. isdnloop_start_ctimer(card, ch - 1);
  832. break;
  833. case 1:
  834. /* Remote busy */
  835. isdnloop_fake(card, "DDIS_I", ch);
  836. sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 1));
  837. isdnloop_fake(card, buf, ch);
  838. break;
  839. case 2:
  840. /* No such user */
  841. isdnloop_fake(card, "DDIS_I", ch);
  842. sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 2));
  843. isdnloop_fake(card, buf, ch);
  844. break;
  845. }
  846. break;
  847. case 6:
  848. /* 0x;EAZC */
  849. card->eazlist[ch - 1][0] = '\0';
  850. break;
  851. case 7:
  852. /* 0x;EAZ */
  853. p += 3;
  854. if (strlen(p) >= sizeof(card->eazlist[0]))
  855. break;
  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, preceding 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. for (i = 0; i < 3; i++) {
  1015. if (!memchr(sdef.num[i], 0, sizeof(sdef.num[i])))
  1016. return -EINVAL;
  1017. }
  1018. spin_lock_irqsave(&card->isdnloop_lock, flags);
  1019. switch (sdef.ptype) {
  1020. case ISDN_PTYPE_EURO:
  1021. if (isdnloop_fake(card, "DRV1.23EC-Q.931-CAPI-CNS-BASIS-20.02.96",
  1022. -1)) {
  1023. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  1024. return -ENOMEM;
  1025. }
  1026. card->sil[0] = card->sil[1] = 4;
  1027. if (isdnloop_fake(card, "TEI OK", 0)) {
  1028. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  1029. return -ENOMEM;
  1030. }
  1031. for (i = 0; i < 3; i++) {
  1032. strlcpy(card->s0num[i], sdef.num[i],
  1033. sizeof(card->s0num[0]));
  1034. }
  1035. break;
  1036. case ISDN_PTYPE_1TR6:
  1037. if (isdnloop_fake(card, "DRV1.04TC-1TR6-CAPI-CNS-BASIS-29.11.95",
  1038. -1)) {
  1039. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  1040. return -ENOMEM;
  1041. }
  1042. card->sil[0] = card->sil[1] = 4;
  1043. if (isdnloop_fake(card, "TEI OK", 0)) {
  1044. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  1045. return -ENOMEM;
  1046. }
  1047. strlcpy(card->s0num[0], sdef.num[0], sizeof(card->s0num[0]));
  1048. card->s0num[1][0] = '\0';
  1049. card->s0num[2][0] = '\0';
  1050. break;
  1051. default:
  1052. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  1053. printk(KERN_WARNING "isdnloop: Illegal D-channel protocol %d\n",
  1054. sdef.ptype);
  1055. return -EINVAL;
  1056. }
  1057. init_timer(&card->st_timer);
  1058. card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
  1059. card->st_timer.function = isdnloop_polldchan;
  1060. card->st_timer.data = (unsigned long) card;
  1061. add_timer(&card->st_timer);
  1062. card->flags |= ISDNLOOP_FLAGS_RUNNING;
  1063. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  1064. return 0;
  1065. }
  1066. /*
  1067. * Main handler for commands sent by linklevel.
  1068. */
  1069. static int
  1070. isdnloop_command(isdn_ctrl *c, isdnloop_card *card)
  1071. {
  1072. ulong a;
  1073. int i;
  1074. char cbuf[80];
  1075. isdn_ctrl cmd;
  1076. isdnloop_cdef cdef;
  1077. switch (c->command) {
  1078. case ISDN_CMD_IOCTL:
  1079. memcpy(&a, c->parm.num, sizeof(ulong));
  1080. switch (c->arg) {
  1081. case ISDNLOOP_IOCTL_DEBUGVAR:
  1082. return (ulong) card;
  1083. case ISDNLOOP_IOCTL_STARTUP:
  1084. return isdnloop_start(card, (isdnloop_sdef *) a);
  1085. break;
  1086. case ISDNLOOP_IOCTL_ADDCARD:
  1087. if (copy_from_user((char *)&cdef,
  1088. (char *)a,
  1089. sizeof(cdef)))
  1090. return -EFAULT;
  1091. return isdnloop_addcard(cdef.id1);
  1092. break;
  1093. case ISDNLOOP_IOCTL_LEASEDCFG:
  1094. if (a) {
  1095. if (!card->leased) {
  1096. card->leased = 1;
  1097. while (card->ptype == ISDN_PTYPE_UNKNOWN)
  1098. schedule_timeout_interruptible(10);
  1099. schedule_timeout_interruptible(10);
  1100. sprintf(cbuf, "00;FV2ON\n01;EAZ1\n02;EAZ2\n");
  1101. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1102. printk(KERN_INFO
  1103. "isdnloop: (%s) Leased-line mode enabled\n",
  1104. CID);
  1105. cmd.command = ISDN_STAT_RUN;
  1106. cmd.driver = card->myid;
  1107. cmd.arg = 0;
  1108. card->interface.statcallb(&cmd);
  1109. }
  1110. } else {
  1111. if (card->leased) {
  1112. card->leased = 0;
  1113. sprintf(cbuf, "00;FV2OFF\n");
  1114. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1115. printk(KERN_INFO
  1116. "isdnloop: (%s) Leased-line mode disabled\n",
  1117. CID);
  1118. cmd.command = ISDN_STAT_RUN;
  1119. cmd.driver = card->myid;
  1120. cmd.arg = 0;
  1121. card->interface.statcallb(&cmd);
  1122. }
  1123. }
  1124. return 0;
  1125. default:
  1126. return -EINVAL;
  1127. }
  1128. break;
  1129. case ISDN_CMD_DIAL:
  1130. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1131. return -ENODEV;
  1132. if (card->leased)
  1133. break;
  1134. if ((c->arg & 255) < ISDNLOOP_BCH) {
  1135. char *p;
  1136. char dcode[4];
  1137. a = c->arg;
  1138. p = c->parm.setup.phone;
  1139. if (*p == 's' || *p == 'S') {
  1140. /* Dial for SPV */
  1141. p++;
  1142. strcpy(dcode, "SCA");
  1143. } else
  1144. /* Normal Dial */
  1145. strcpy(dcode, "CAL");
  1146. snprintf(cbuf, sizeof(cbuf),
  1147. "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a + 1),
  1148. dcode, p, c->parm.setup.si1,
  1149. c->parm.setup.si2, c->parm.setup.eazmsn);
  1150. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1151. }
  1152. break;
  1153. case ISDN_CMD_ACCEPTD:
  1154. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1155. return -ENODEV;
  1156. if (c->arg < ISDNLOOP_BCH) {
  1157. a = c->arg + 1;
  1158. cbuf[0] = 0;
  1159. switch (card->l2_proto[a - 1]) {
  1160. case ISDN_PROTO_L2_X75I:
  1161. sprintf(cbuf, "%02d;BX75\n", (int) a);
  1162. break;
  1163. #ifdef CONFIG_ISDN_X25
  1164. case ISDN_PROTO_L2_X25DTE:
  1165. sprintf(cbuf, "%02d;BX2T\n", (int) a);
  1166. break;
  1167. case ISDN_PROTO_L2_X25DCE:
  1168. sprintf(cbuf, "%02d;BX2C\n", (int) a);
  1169. break;
  1170. #endif
  1171. case ISDN_PROTO_L2_HDLC:
  1172. sprintf(cbuf, "%02d;BTRA\n", (int) a);
  1173. break;
  1174. }
  1175. if (strlen(cbuf))
  1176. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1177. sprintf(cbuf, "%02d;DCON_R\n", (int) a);
  1178. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1179. }
  1180. break;
  1181. case ISDN_CMD_ACCEPTB:
  1182. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1183. return -ENODEV;
  1184. if (c->arg < ISDNLOOP_BCH) {
  1185. a = c->arg + 1;
  1186. switch (card->l2_proto[a - 1]) {
  1187. case ISDN_PROTO_L2_X75I:
  1188. sprintf(cbuf, "%02d;BCON_R,BX75\n", (int) a);
  1189. break;
  1190. #ifdef CONFIG_ISDN_X25
  1191. case ISDN_PROTO_L2_X25DTE:
  1192. sprintf(cbuf, "%02d;BCON_R,BX2T\n", (int) a);
  1193. break;
  1194. case ISDN_PROTO_L2_X25DCE:
  1195. sprintf(cbuf, "%02d;BCON_R,BX2C\n", (int) a);
  1196. break;
  1197. #endif
  1198. case ISDN_PROTO_L2_HDLC:
  1199. sprintf(cbuf, "%02d;BCON_R,BTRA\n", (int) a);
  1200. break;
  1201. default:
  1202. sprintf(cbuf, "%02d;BCON_R\n", (int) a);
  1203. }
  1204. printk(KERN_DEBUG "isdnloop writecmd '%s'\n", cbuf);
  1205. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1206. break;
  1207. case ISDN_CMD_HANGUP:
  1208. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1209. return -ENODEV;
  1210. if (c->arg < ISDNLOOP_BCH) {
  1211. a = c->arg + 1;
  1212. sprintf(cbuf, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a, (int) a);
  1213. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1214. }
  1215. break;
  1216. case ISDN_CMD_SETEAZ:
  1217. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1218. return -ENODEV;
  1219. if (card->leased)
  1220. break;
  1221. if (c->arg < ISDNLOOP_BCH) {
  1222. a = c->arg + 1;
  1223. if (card->ptype == ISDN_PTYPE_EURO) {
  1224. sprintf(cbuf, "%02d;MS%s%s\n", (int) a,
  1225. c->parm.num[0] ? "N" : "ALL", c->parm.num);
  1226. } else
  1227. sprintf(cbuf, "%02d;EAZ%s\n", (int) a,
  1228. c->parm.num[0] ? c->parm.num : (u_char *) "0123456789");
  1229. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1230. }
  1231. break;
  1232. case ISDN_CMD_CLREAZ:
  1233. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1234. return -ENODEV;
  1235. if (card->leased)
  1236. break;
  1237. if (c->arg < ISDNLOOP_BCH) {
  1238. a = c->arg + 1;
  1239. if (card->ptype == ISDN_PTYPE_EURO)
  1240. sprintf(cbuf, "%02d;MSNC\n", (int) a);
  1241. else
  1242. sprintf(cbuf, "%02d;EAZC\n", (int) a);
  1243. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1244. }
  1245. break;
  1246. case ISDN_CMD_SETL2:
  1247. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1248. return -ENODEV;
  1249. if ((c->arg & 255) < ISDNLOOP_BCH) {
  1250. a = c->arg;
  1251. switch (a >> 8) {
  1252. case ISDN_PROTO_L2_X75I:
  1253. sprintf(cbuf, "%02d;BX75\n", (int) (a & 255) + 1);
  1254. break;
  1255. #ifdef CONFIG_ISDN_X25
  1256. case ISDN_PROTO_L2_X25DTE:
  1257. sprintf(cbuf, "%02d;BX2T\n", (int) (a & 255) + 1);
  1258. break;
  1259. case ISDN_PROTO_L2_X25DCE:
  1260. sprintf(cbuf, "%02d;BX2C\n", (int) (a & 255) + 1);
  1261. break;
  1262. #endif
  1263. case ISDN_PROTO_L2_HDLC:
  1264. sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
  1265. break;
  1266. case ISDN_PROTO_L2_TRANS:
  1267. sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
  1268. break;
  1269. default:
  1270. return -EINVAL;
  1271. }
  1272. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1273. card->l2_proto[a & 255] = (a >> 8);
  1274. }
  1275. break;
  1276. case ISDN_CMD_SETL3:
  1277. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1278. return -ENODEV;
  1279. return 0;
  1280. default:
  1281. return -EINVAL;
  1282. }
  1283. }
  1284. return 0;
  1285. }
  1286. /*
  1287. * Find card with given driverId
  1288. */
  1289. static inline isdnloop_card *
  1290. isdnloop_findcard(int driverid)
  1291. {
  1292. isdnloop_card *p = cards;
  1293. while (p) {
  1294. if (p->myid == driverid)
  1295. return p;
  1296. p = p->next;
  1297. }
  1298. return (isdnloop_card *) 0;
  1299. }
  1300. /*
  1301. * Wrapper functions for interface to linklevel
  1302. */
  1303. static int
  1304. if_command(isdn_ctrl *c)
  1305. {
  1306. isdnloop_card *card = isdnloop_findcard(c->driver);
  1307. if (card)
  1308. return isdnloop_command(c, card);
  1309. printk(KERN_ERR
  1310. "isdnloop: if_command called with invalid driverId!\n");
  1311. return -ENODEV;
  1312. }
  1313. static int
  1314. if_writecmd(const u_char __user *buf, int len, int id, int channel)
  1315. {
  1316. isdnloop_card *card = isdnloop_findcard(id);
  1317. if (card) {
  1318. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1319. return -ENODEV;
  1320. return isdnloop_writecmd(buf, len, 1, card);
  1321. }
  1322. printk(KERN_ERR
  1323. "isdnloop: if_writecmd called with invalid driverId!\n");
  1324. return -ENODEV;
  1325. }
  1326. static int
  1327. if_readstatus(u_char __user *buf, int len, int id, int channel)
  1328. {
  1329. isdnloop_card *card = isdnloop_findcard(id);
  1330. if (card) {
  1331. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1332. return -ENODEV;
  1333. return isdnloop_readstatus(buf, len, card);
  1334. }
  1335. printk(KERN_ERR
  1336. "isdnloop: if_readstatus called with invalid driverId!\n");
  1337. return -ENODEV;
  1338. }
  1339. static int
  1340. if_sendbuf(int id, int channel, int ack, struct sk_buff *skb)
  1341. {
  1342. isdnloop_card *card = isdnloop_findcard(id);
  1343. if (card) {
  1344. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1345. return -ENODEV;
  1346. /* ack request stored in skb scratch area */
  1347. *(skb->head) = ack;
  1348. return isdnloop_sendbuf(channel, skb, card);
  1349. }
  1350. printk(KERN_ERR
  1351. "isdnloop: if_sendbuf called with invalid driverId!\n");
  1352. return -ENODEV;
  1353. }
  1354. /*
  1355. * Allocate a new card-struct, initialize it
  1356. * link it into cards-list and register it at linklevel.
  1357. */
  1358. static isdnloop_card *
  1359. isdnloop_initcard(char *id)
  1360. {
  1361. isdnloop_card *card;
  1362. int i;
  1363. card = kzalloc(sizeof(isdnloop_card), GFP_KERNEL);
  1364. if (!card) {
  1365. printk(KERN_WARNING
  1366. "isdnloop: (%s) Could not allocate card-struct.\n", id);
  1367. return (isdnloop_card *) 0;
  1368. }
  1369. card->interface.owner = THIS_MODULE;
  1370. card->interface.channels = ISDNLOOP_BCH;
  1371. card->interface.hl_hdrlen = 1; /* scratch area for storing ack flag*/
  1372. card->interface.maxbufsize = 4000;
  1373. card->interface.command = if_command;
  1374. card->interface.writebuf_skb = if_sendbuf;
  1375. card->interface.writecmd = if_writecmd;
  1376. card->interface.readstat = if_readstatus;
  1377. card->interface.features = ISDN_FEATURE_L2_X75I |
  1378. #ifdef CONFIG_ISDN_X25
  1379. ISDN_FEATURE_L2_X25DTE |
  1380. ISDN_FEATURE_L2_X25DCE |
  1381. #endif
  1382. ISDN_FEATURE_L2_HDLC |
  1383. ISDN_FEATURE_L3_TRANS |
  1384. ISDN_FEATURE_P_UNKNOWN;
  1385. card->ptype = ISDN_PTYPE_UNKNOWN;
  1386. strlcpy(card->interface.id, id, sizeof(card->interface.id));
  1387. card->msg_buf_write = card->msg_buf;
  1388. card->msg_buf_read = card->msg_buf;
  1389. card->msg_buf_end = &card->msg_buf[sizeof(card->msg_buf) - 1];
  1390. for (i = 0; i < ISDNLOOP_BCH; i++) {
  1391. card->l2_proto[i] = ISDN_PROTO_L2_X75I;
  1392. skb_queue_head_init(&card->bqueue[i]);
  1393. }
  1394. skb_queue_head_init(&card->dqueue);
  1395. spin_lock_init(&card->isdnloop_lock);
  1396. card->next = cards;
  1397. cards = card;
  1398. if (!register_isdn(&card->interface)) {
  1399. cards = cards->next;
  1400. printk(KERN_WARNING
  1401. "isdnloop: Unable to register %s\n", id);
  1402. kfree(card);
  1403. return (isdnloop_card *) 0;
  1404. }
  1405. card->myid = card->interface.channels;
  1406. return card;
  1407. }
  1408. static int
  1409. isdnloop_addcard(char *id1)
  1410. {
  1411. isdnloop_card *card;
  1412. card = isdnloop_initcard(id1);
  1413. if (!card) {
  1414. return -EIO;
  1415. }
  1416. printk(KERN_INFO
  1417. "isdnloop: (%s) virtual card added\n",
  1418. card->interface.id);
  1419. return 0;
  1420. }
  1421. static int __init
  1422. isdnloop_init(void)
  1423. {
  1424. if (isdnloop_id)
  1425. return isdnloop_addcard(isdnloop_id);
  1426. return 0;
  1427. }
  1428. static void __exit
  1429. isdnloop_exit(void)
  1430. {
  1431. isdn_ctrl cmd;
  1432. isdnloop_card *card = cards;
  1433. isdnloop_card *last;
  1434. int i;
  1435. isdnloop_stopallcards();
  1436. while (card) {
  1437. cmd.command = ISDN_STAT_UNLOAD;
  1438. cmd.driver = card->myid;
  1439. card->interface.statcallb(&cmd);
  1440. for (i = 0; i < ISDNLOOP_BCH; i++)
  1441. isdnloop_free_queue(card, i);
  1442. card = card->next;
  1443. }
  1444. card = cards;
  1445. while (card) {
  1446. last = card;
  1447. skb_queue_purge(&card->dqueue);
  1448. card = card->next;
  1449. kfree(last);
  1450. }
  1451. printk(KERN_NOTICE "isdnloop-ISDN-driver unloaded\n");
  1452. }
  1453. module_init(isdnloop_init);
  1454. module_exit(isdnloop_exit);