isdnloop.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  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. 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. skb_copy_from_linear_data(skb,
  395. skb_put(nskb, len), len);
  396. skb_queue_tail(&card->bqueue[channel], nskb);
  397. dev_kfree_skb(skb);
  398. } else
  399. len = 0;
  400. card->sndcount[channel] += len;
  401. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  402. }
  403. return len;
  404. }
  405. /*
  406. * Read the messages from the card's ringbuffer
  407. *
  408. * Parameter:
  409. * buf = pointer to buffer.
  410. * len = number of bytes to read.
  411. * user = flag, 1: called from userlevel 0: called from kernel.
  412. * card = pointer to card struct.
  413. * Return:
  414. * number of bytes actually transferred.
  415. */
  416. static int
  417. isdnloop_readstatus(u_char __user *buf, int len, isdnloop_card *card)
  418. {
  419. int count;
  420. u_char __user *p;
  421. for (p = buf, count = 0; count < len; p++, count++) {
  422. if (card->msg_buf_read == card->msg_buf_write)
  423. return count;
  424. if (put_user(*card->msg_buf_read++, p))
  425. return -EFAULT;
  426. if (card->msg_buf_read > card->msg_buf_end)
  427. card->msg_buf_read = card->msg_buf;
  428. }
  429. return count;
  430. }
  431. /*
  432. * Simulate a card's response by appending it to the cards
  433. * message queue.
  434. *
  435. * Parameter:
  436. * card = pointer to card struct.
  437. * s = pointer to message-string.
  438. * ch = channel: 0 = generic messages, 1 and 2 = D-channel messages.
  439. * Return:
  440. * 0 on success, 1 on memory squeeze.
  441. */
  442. static int
  443. isdnloop_fake(isdnloop_card *card, char *s, int ch)
  444. {
  445. struct sk_buff *skb;
  446. int len = strlen(s) + ((ch >= 0) ? 3 : 0);
  447. if (!(skb = dev_alloc_skb(len))) {
  448. printk(KERN_WARNING "isdnloop: Out of memory in isdnloop_fake\n");
  449. return 1;
  450. }
  451. if (ch >= 0)
  452. sprintf(skb_put(skb, 3), "%02d;", ch);
  453. memcpy(skb_put(skb, strlen(s)), s, strlen(s));
  454. skb_queue_tail(&card->dqueue, skb);
  455. return 0;
  456. }
  457. /* *INDENT-OFF* */
  458. static isdnloop_stat isdnloop_cmd_table[] =
  459. {
  460. {"BCON_R", 0, 1}, /* B-Channel connect */
  461. {"BCON_I", 0, 17}, /* B-Channel connect ind */
  462. {"BDIS_R", 0, 2}, /* B-Channel disconnect */
  463. {"DDIS_R", 0, 3}, /* D-Channel disconnect */
  464. {"DCON_R", 0, 16}, /* D-Channel connect */
  465. {"DSCA_R", 0, 4}, /* Dial 1TR6-SPV */
  466. {"DCAL_R", 0, 5}, /* Dial */
  467. {"EAZC", 0, 6}, /* Clear EAZ listener */
  468. {"EAZ", 0, 7}, /* Set EAZ listener */
  469. {"SEEAZ", 0, 8}, /* Get EAZ listener */
  470. {"MSN", 0, 9}, /* Set/Clear MSN listener */
  471. {"MSALL", 0, 10}, /* Set multi MSN listeners */
  472. {"SETSIL", 0, 11}, /* Set SI list */
  473. {"SEESIL", 0, 12}, /* Get SI list */
  474. {"SILC", 0, 13}, /* Clear SI list */
  475. {"LOCK", 0, -1}, /* LOCK channel */
  476. {"UNLOCK", 0, -1}, /* UNLOCK channel */
  477. {"FV2ON", 1, 14}, /* Leased mode on */
  478. {"FV2OFF", 1, 15}, /* Leased mode off */
  479. {NULL, 0, -1}
  480. };
  481. /* *INDENT-ON* */
  482. /*
  483. * Simulate an error-response from a card.
  484. *
  485. * Parameter:
  486. * card = pointer to card struct.
  487. */
  488. static void
  489. isdnloop_fake_err(isdnloop_card *card)
  490. {
  491. char buf[64];
  492. snprintf(buf, sizeof(buf), "E%s", card->omsg);
  493. isdnloop_fake(card, buf, -1);
  494. isdnloop_fake(card, "NAK", -1);
  495. }
  496. static u_char ctable_eu[] =
  497. {0x00, 0x11, 0x01, 0x12};
  498. static u_char ctable_1t[] =
  499. {0x00, 0x3b, 0x01, 0x3a};
  500. /*
  501. * Assemble a simplified cause message depending on the
  502. * D-channel protocol used.
  503. *
  504. * Parameter:
  505. * card = pointer to card struct.
  506. * loc = location: 0 = local, 1 = remote.
  507. * cau = cause: 1 = busy, 2 = nonexistent callerid, 3 = no user responding.
  508. * Return:
  509. * Pointer to buffer containing the assembled message.
  510. */
  511. static char *
  512. isdnloop_unicause(isdnloop_card *card, int loc, int cau)
  513. {
  514. static char buf[6];
  515. switch (card->ptype) {
  516. case ISDN_PTYPE_EURO:
  517. sprintf(buf, "E%02X%02X", (loc) ? 4 : 2, ctable_eu[cau]);
  518. break;
  519. case ISDN_PTYPE_1TR6:
  520. sprintf(buf, "%02X44", ctable_1t[cau]);
  521. break;
  522. default:
  523. return ("0000");
  524. }
  525. return (buf);
  526. }
  527. /*
  528. * Release a virtual connection. Called from timer interrupt, when
  529. * called party did not respond.
  530. *
  531. * Parameter:
  532. * card = pointer to card struct.
  533. * ch = channel (0-based)
  534. */
  535. static void
  536. isdnloop_atimeout(isdnloop_card *card, int ch)
  537. {
  538. unsigned long flags;
  539. char buf[60];
  540. spin_lock_irqsave(&card->isdnloop_lock, flags);
  541. if (card->rcard) {
  542. isdnloop_fake(card->rcard[ch], "DDIS_I", card->rch[ch] + 1);
  543. card->rcard[ch]->rcard[card->rch[ch]] = NULL;
  544. card->rcard[ch] = NULL;
  545. }
  546. isdnloop_fake(card, "DDIS_I", ch + 1);
  547. /* No user responding */
  548. sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 3));
  549. isdnloop_fake(card, buf, ch + 1);
  550. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  551. }
  552. /*
  553. * Wrapper for isdnloop_atimeout().
  554. */
  555. static void
  556. isdnloop_atimeout0(unsigned long data)
  557. {
  558. isdnloop_card *card = (isdnloop_card *) data;
  559. isdnloop_atimeout(card, 0);
  560. }
  561. /*
  562. * Wrapper for isdnloop_atimeout().
  563. */
  564. static void
  565. isdnloop_atimeout1(unsigned long data)
  566. {
  567. isdnloop_card *card = (isdnloop_card *) data;
  568. isdnloop_atimeout(card, 1);
  569. }
  570. /*
  571. * Install a watchdog for a user, not responding.
  572. *
  573. * Parameter:
  574. * card = pointer to card struct.
  575. * ch = channel to watch for.
  576. */
  577. static void
  578. isdnloop_start_ctimer(isdnloop_card *card, int ch)
  579. {
  580. unsigned long flags;
  581. spin_lock_irqsave(&card->isdnloop_lock, flags);
  582. init_timer(&card->c_timer[ch]);
  583. card->c_timer[ch].expires = jiffies + ISDNLOOP_TIMER_ALERTWAIT;
  584. if (ch)
  585. card->c_timer[ch].function = isdnloop_atimeout1;
  586. else
  587. card->c_timer[ch].function = isdnloop_atimeout0;
  588. card->c_timer[ch].data = (unsigned long) card;
  589. add_timer(&card->c_timer[ch]);
  590. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  591. }
  592. /*
  593. * Kill a pending channel watchdog.
  594. *
  595. * Parameter:
  596. * card = pointer to card struct.
  597. * ch = channel (0-based).
  598. */
  599. static void
  600. isdnloop_kill_ctimer(isdnloop_card *card, int ch)
  601. {
  602. unsigned long flags;
  603. spin_lock_irqsave(&card->isdnloop_lock, flags);
  604. del_timer(&card->c_timer[ch]);
  605. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  606. }
  607. static u_char si2bit[] =
  608. {0, 1, 0, 0, 0, 2, 0, 4, 0, 0};
  609. static u_char bit2si[] =
  610. {1, 5, 7};
  611. /*
  612. * Try finding a listener for an outgoing call.
  613. *
  614. * Parameter:
  615. * card = pointer to calling card.
  616. * p = pointer to ICN-type setup-string.
  617. * lch = channel of calling card.
  618. * cmd = pointer to struct to be filled when parsing setup.
  619. * Return:
  620. * 0 = found match, alerting should happen.
  621. * 1 = found matching number but it is busy.
  622. * 2 = no matching listener.
  623. * 3 = found matching number but SI does not match.
  624. */
  625. static int
  626. isdnloop_try_call(isdnloop_card *card, char *p, int lch, isdn_ctrl *cmd)
  627. {
  628. isdnloop_card *cc = cards;
  629. unsigned long flags;
  630. int ch;
  631. int num_match;
  632. int i;
  633. char *e;
  634. char nbuf[32];
  635. isdnloop_parse_setup(p, cmd);
  636. while (cc) {
  637. for (ch = 0; ch < 2; ch++) {
  638. /* Exclude ourself */
  639. if ((cc == card) && (ch == lch))
  640. continue;
  641. num_match = 0;
  642. switch (cc->ptype) {
  643. case ISDN_PTYPE_EURO:
  644. for (i = 0; i < 3; i++)
  645. if (!(strcmp(cc->s0num[i], cmd->parm.setup.phone)))
  646. num_match = 1;
  647. break;
  648. case ISDN_PTYPE_1TR6:
  649. e = cc->eazlist[ch];
  650. while (*e) {
  651. sprintf(nbuf, "%s%c", cc->s0num[0], *e);
  652. if (!(strcmp(nbuf, cmd->parm.setup.phone)))
  653. num_match = 1;
  654. e++;
  655. }
  656. }
  657. if (num_match) {
  658. spin_lock_irqsave(&card->isdnloop_lock, flags);
  659. /* channel idle? */
  660. if (!(cc->rcard[ch])) {
  661. /* Check SI */
  662. if (!(si2bit[cmd->parm.setup.si1] & cc->sil[ch])) {
  663. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  664. return 3;
  665. }
  666. /* ch is idle, si and number matches */
  667. cc->rcard[ch] = card;
  668. cc->rch[ch] = lch;
  669. card->rcard[lch] = cc;
  670. card->rch[lch] = ch;
  671. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  672. return 0;
  673. } else {
  674. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  675. /* num matches, but busy */
  676. if (ch == 1)
  677. return 1;
  678. }
  679. }
  680. }
  681. cc = cc->next;
  682. }
  683. return 2;
  684. }
  685. /*
  686. * Depending on D-channel protocol and caller/called, modify
  687. * phone number.
  688. *
  689. * Parameter:
  690. * card = pointer to card struct.
  691. * phone = pointer phone number.
  692. * caller = flag: 1 = caller, 0 = called.
  693. * Return:
  694. * pointer to new phone number.
  695. */
  696. static char *
  697. isdnloop_vstphone(isdnloop_card *card, char *phone, int caller)
  698. {
  699. int i;
  700. static char nphone[30];
  701. if (!card) {
  702. printk("BUG!!!\n");
  703. return "";
  704. }
  705. switch (card->ptype) {
  706. case ISDN_PTYPE_EURO:
  707. if (caller) {
  708. for (i = 0; i < 2; i++)
  709. if (!(strcmp(card->s0num[i], phone)))
  710. return (phone);
  711. return (card->s0num[0]);
  712. }
  713. return (phone);
  714. break;
  715. case ISDN_PTYPE_1TR6:
  716. if (caller) {
  717. sprintf(nphone, "%s%c", card->s0num[0], phone[0]);
  718. return (nphone);
  719. } else
  720. return (&phone[strlen(phone) - 1]);
  721. break;
  722. }
  723. return "";
  724. }
  725. /*
  726. * Parse an ICN-type command string sent to the 'card'.
  727. * Perform misc. actions depending on the command.
  728. *
  729. * Parameter:
  730. * card = pointer to card struct.
  731. */
  732. static void
  733. isdnloop_parse_cmd(isdnloop_card *card)
  734. {
  735. char *p = card->omsg;
  736. isdn_ctrl cmd;
  737. char buf[60];
  738. isdnloop_stat *s = isdnloop_cmd_table;
  739. int action = -1;
  740. int i;
  741. int ch;
  742. if ((card->omsg[0] != '0') && (card->omsg[2] != ';')) {
  743. isdnloop_fake_err(card);
  744. return;
  745. }
  746. ch = card->omsg[1] - '0';
  747. if ((ch < 0) || (ch > 2)) {
  748. isdnloop_fake_err(card);
  749. return;
  750. }
  751. p += 3;
  752. while (s->statstr) {
  753. if (!strncmp(p, s->statstr, strlen(s->statstr))) {
  754. action = s->action;
  755. if (s->command && (ch != 0)) {
  756. isdnloop_fake_err(card);
  757. return;
  758. }
  759. break;
  760. }
  761. s++;
  762. }
  763. if (action == -1)
  764. return;
  765. switch (action) {
  766. case 1:
  767. /* 0x;BCON_R */
  768. if (card->rcard[ch - 1]) {
  769. isdnloop_fake(card->rcard[ch - 1], "BCON_I",
  770. card->rch[ch - 1] + 1);
  771. isdnloop_fake(card, "BCON_C", ch);
  772. }
  773. break;
  774. case 17:
  775. /* 0x;BCON_I */
  776. if (card->rcard[ch - 1]) {
  777. isdnloop_fake(card->rcard[ch - 1], "BCON_C",
  778. card->rch[ch - 1] + 1);
  779. }
  780. break;
  781. case 2:
  782. /* 0x;BDIS_R */
  783. isdnloop_fake(card, "BDIS_C", ch);
  784. if (card->rcard[ch - 1]) {
  785. isdnloop_fake(card->rcard[ch - 1], "BDIS_I",
  786. card->rch[ch - 1] + 1);
  787. }
  788. break;
  789. case 16:
  790. /* 0x;DCON_R */
  791. isdnloop_kill_ctimer(card, ch - 1);
  792. if (card->rcard[ch - 1]) {
  793. isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
  794. isdnloop_fake(card->rcard[ch - 1], "DCON_C",
  795. card->rch[ch - 1] + 1);
  796. isdnloop_fake(card, "DCON_C", ch);
  797. }
  798. break;
  799. case 3:
  800. /* 0x;DDIS_R */
  801. isdnloop_kill_ctimer(card, ch - 1);
  802. if (card->rcard[ch - 1]) {
  803. isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]);
  804. isdnloop_fake(card->rcard[ch - 1], "DDIS_I",
  805. card->rch[ch - 1] + 1);
  806. card->rcard[ch - 1] = NULL;
  807. }
  808. isdnloop_fake(card, "DDIS_C", ch);
  809. break;
  810. case 4:
  811. /* 0x;DSCA_Rdd,yy,zz,oo */
  812. if (card->ptype != ISDN_PTYPE_1TR6) {
  813. isdnloop_fake_err(card);
  814. return;
  815. }
  816. /* Fall through */
  817. case 5:
  818. /* 0x;DCAL_Rdd,yy,zz,oo */
  819. p += 6;
  820. switch (isdnloop_try_call(card, p, ch - 1, &cmd)) {
  821. case 0:
  822. /* Alerting */
  823. sprintf(buf, "D%s_I%s,%02d,%02d,%s",
  824. (action == 4) ? "SCA" : "CAL",
  825. isdnloop_vstphone(card, cmd.parm.setup.eazmsn, 1),
  826. cmd.parm.setup.si1,
  827. cmd.parm.setup.si2,
  828. isdnloop_vstphone(card->rcard[ch - 1],
  829. cmd.parm.setup.phone, 0));
  830. isdnloop_fake(card->rcard[ch - 1], buf, card->rch[ch - 1] + 1);
  831. /* Fall through */
  832. case 3:
  833. /* si1 does not match, don't alert but start timer */
  834. isdnloop_start_ctimer(card, ch - 1);
  835. break;
  836. case 1:
  837. /* Remote busy */
  838. isdnloop_fake(card, "DDIS_I", ch);
  839. sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 1));
  840. isdnloop_fake(card, buf, ch);
  841. break;
  842. case 2:
  843. /* No such user */
  844. isdnloop_fake(card, "DDIS_I", ch);
  845. sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 2));
  846. isdnloop_fake(card, buf, ch);
  847. break;
  848. }
  849. break;
  850. case 6:
  851. /* 0x;EAZC */
  852. card->eazlist[ch - 1][0] = '\0';
  853. break;
  854. case 7:
  855. /* 0x;EAZ */
  856. p += 3;
  857. if (strlen(p) >= sizeof(card->eazlist[0]))
  858. break;
  859. strcpy(card->eazlist[ch - 1], p);
  860. break;
  861. case 8:
  862. /* 0x;SEEAZ */
  863. sprintf(buf, "EAZ-LIST: %s", card->eazlist[ch - 1]);
  864. isdnloop_fake(card, buf, ch + 1);
  865. break;
  866. case 9:
  867. /* 0x;MSN */
  868. break;
  869. case 10:
  870. /* 0x;MSNALL */
  871. break;
  872. case 11:
  873. /* 0x;SETSIL */
  874. p += 6;
  875. i = 0;
  876. while (strchr("0157", *p)) {
  877. if (i)
  878. card->sil[ch - 1] |= si2bit[*p - '0'];
  879. i = (*p++ == '0');
  880. }
  881. if (*p)
  882. isdnloop_fake_err(card);
  883. break;
  884. case 12:
  885. /* 0x;SEESIL */
  886. sprintf(buf, "SIN-LIST: ");
  887. p = buf + 10;
  888. for (i = 0; i < 3; i++)
  889. if (card->sil[ch - 1] & (1 << i))
  890. p += sprintf(p, "%02d", bit2si[i]);
  891. isdnloop_fake(card, buf, ch + 1);
  892. break;
  893. case 13:
  894. /* 0x;SILC */
  895. card->sil[ch - 1] = 0;
  896. break;
  897. case 14:
  898. /* 00;FV2ON */
  899. break;
  900. case 15:
  901. /* 00;FV2OFF */
  902. break;
  903. }
  904. }
  905. /*
  906. * Put command-strings into the of the 'card'. In reality, execute them
  907. * right in place by calling isdnloop_parse_cmd(). Also copy every
  908. * command to the read message ringbuffer, preceding it with a '>'.
  909. * These mesagges can be read at /dev/isdnctrl.
  910. *
  911. * Parameter:
  912. * buf = pointer to command buffer.
  913. * len = length of buffer data.
  914. * user = flag: 1 = called form userlevel, 0 called from kernel.
  915. * card = pointer to card struct.
  916. * Return:
  917. * number of bytes transferred (currently always equals len).
  918. */
  919. static int
  920. isdnloop_writecmd(const u_char *buf, int len, int user, isdnloop_card *card)
  921. {
  922. int xcount = 0;
  923. int ocount = 1;
  924. isdn_ctrl cmd;
  925. while (len) {
  926. int count = len;
  927. u_char *p;
  928. u_char msg[0x100];
  929. if (count > 255)
  930. count = 255;
  931. if (user) {
  932. if (copy_from_user(msg, buf, count))
  933. return -EFAULT;
  934. } else
  935. memcpy(msg, buf, count);
  936. isdnloop_putmsg(card, '>');
  937. for (p = msg; count > 0; count--, p++) {
  938. len--;
  939. xcount++;
  940. isdnloop_putmsg(card, *p);
  941. card->omsg[card->optr] = *p;
  942. if (*p == '\n') {
  943. card->omsg[card->optr] = '\0';
  944. card->optr = 0;
  945. isdnloop_parse_cmd(card);
  946. if (len) {
  947. isdnloop_putmsg(card, '>');
  948. ocount++;
  949. }
  950. } else {
  951. if (card->optr < 59)
  952. card->optr++;
  953. }
  954. ocount++;
  955. }
  956. }
  957. cmd.command = ISDN_STAT_STAVAIL;
  958. cmd.driver = card->myid;
  959. cmd.arg = ocount;
  960. card->interface.statcallb(&cmd);
  961. return xcount;
  962. }
  963. /*
  964. * Delete card's pending timers, send STOP to linklevel
  965. */
  966. static void
  967. isdnloop_stopcard(isdnloop_card *card)
  968. {
  969. unsigned long flags;
  970. isdn_ctrl cmd;
  971. spin_lock_irqsave(&card->isdnloop_lock, flags);
  972. if (card->flags & ISDNLOOP_FLAGS_RUNNING) {
  973. card->flags &= ~ISDNLOOP_FLAGS_RUNNING;
  974. del_timer(&card->st_timer);
  975. del_timer(&card->rb_timer);
  976. del_timer(&card->c_timer[0]);
  977. del_timer(&card->c_timer[1]);
  978. cmd.command = ISDN_STAT_STOP;
  979. cmd.driver = card->myid;
  980. card->interface.statcallb(&cmd);
  981. }
  982. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  983. }
  984. /*
  985. * Stop all cards before unload.
  986. */
  987. static void
  988. isdnloop_stopallcards(void)
  989. {
  990. isdnloop_card *p = cards;
  991. while (p) {
  992. isdnloop_stopcard(p);
  993. p = p->next;
  994. }
  995. }
  996. /*
  997. * Start a 'card'. Simulate card's boot message and set the phone
  998. * number(s) of the virtual 'S0-Interface'. Install D-channel
  999. * poll timer.
  1000. *
  1001. * Parameter:
  1002. * card = pointer to card struct.
  1003. * sdefp = pointer to struct holding ioctl parameters.
  1004. * Return:
  1005. * 0 on success, -E??? otherwise.
  1006. */
  1007. static int
  1008. isdnloop_start(isdnloop_card *card, isdnloop_sdef *sdefp)
  1009. {
  1010. unsigned long flags;
  1011. isdnloop_sdef sdef;
  1012. int i;
  1013. if (card->flags & ISDNLOOP_FLAGS_RUNNING)
  1014. return -EBUSY;
  1015. if (copy_from_user((char *) &sdef, (char *) sdefp, sizeof(sdef)))
  1016. return -EFAULT;
  1017. for (i = 0; i < 3; i++) {
  1018. if (!memchr(sdef.num[i], 0, sizeof(sdef.num[i])))
  1019. return -EINVAL;
  1020. }
  1021. spin_lock_irqsave(&card->isdnloop_lock, flags);
  1022. switch (sdef.ptype) {
  1023. case ISDN_PTYPE_EURO:
  1024. if (isdnloop_fake(card, "DRV1.23EC-Q.931-CAPI-CNS-BASIS-20.02.96",
  1025. -1)) {
  1026. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  1027. return -ENOMEM;
  1028. }
  1029. card->sil[0] = card->sil[1] = 4;
  1030. if (isdnloop_fake(card, "TEI OK", 0)) {
  1031. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  1032. return -ENOMEM;
  1033. }
  1034. for (i = 0; i < 3; i++) {
  1035. strlcpy(card->s0num[i], sdef.num[i],
  1036. sizeof(card->s0num[0]));
  1037. }
  1038. break;
  1039. case ISDN_PTYPE_1TR6:
  1040. if (isdnloop_fake(card, "DRV1.04TC-1TR6-CAPI-CNS-BASIS-29.11.95",
  1041. -1)) {
  1042. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  1043. return -ENOMEM;
  1044. }
  1045. card->sil[0] = card->sil[1] = 4;
  1046. if (isdnloop_fake(card, "TEI OK", 0)) {
  1047. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  1048. return -ENOMEM;
  1049. }
  1050. strlcpy(card->s0num[0], sdef.num[0], sizeof(card->s0num[0]));
  1051. card->s0num[1][0] = '\0';
  1052. card->s0num[2][0] = '\0';
  1053. break;
  1054. default:
  1055. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  1056. printk(KERN_WARNING "isdnloop: Illegal D-channel protocol %d\n",
  1057. sdef.ptype);
  1058. return -EINVAL;
  1059. }
  1060. init_timer(&card->st_timer);
  1061. card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD;
  1062. card->st_timer.function = isdnloop_polldchan;
  1063. card->st_timer.data = (unsigned long) card;
  1064. add_timer(&card->st_timer);
  1065. card->flags |= ISDNLOOP_FLAGS_RUNNING;
  1066. spin_unlock_irqrestore(&card->isdnloop_lock, flags);
  1067. return 0;
  1068. }
  1069. /*
  1070. * Main handler for commands sent by linklevel.
  1071. */
  1072. static int
  1073. isdnloop_command(isdn_ctrl *c, isdnloop_card *card)
  1074. {
  1075. ulong a;
  1076. int i;
  1077. char cbuf[80];
  1078. isdn_ctrl cmd;
  1079. isdnloop_cdef cdef;
  1080. switch (c->command) {
  1081. case ISDN_CMD_IOCTL:
  1082. memcpy(&a, c->parm.num, sizeof(ulong));
  1083. switch (c->arg) {
  1084. case ISDNLOOP_IOCTL_DEBUGVAR:
  1085. return (ulong) card;
  1086. case ISDNLOOP_IOCTL_STARTUP:
  1087. if (!access_ok(VERIFY_READ, (void *) a, sizeof(isdnloop_sdef)))
  1088. return -EFAULT;
  1089. return (isdnloop_start(card, (isdnloop_sdef *) a));
  1090. break;
  1091. case ISDNLOOP_IOCTL_ADDCARD:
  1092. if (copy_from_user((char *)&cdef,
  1093. (char *)a,
  1094. sizeof(cdef)))
  1095. return -EFAULT;
  1096. return (isdnloop_addcard(cdef.id1));
  1097. break;
  1098. case ISDNLOOP_IOCTL_LEASEDCFG:
  1099. if (a) {
  1100. if (!card->leased) {
  1101. card->leased = 1;
  1102. while (card->ptype == ISDN_PTYPE_UNKNOWN)
  1103. schedule_timeout_interruptible(10);
  1104. schedule_timeout_interruptible(10);
  1105. sprintf(cbuf, "00;FV2ON\n01;EAZ1\n02;EAZ2\n");
  1106. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1107. printk(KERN_INFO
  1108. "isdnloop: (%s) Leased-line mode enabled\n",
  1109. CID);
  1110. cmd.command = ISDN_STAT_RUN;
  1111. cmd.driver = card->myid;
  1112. cmd.arg = 0;
  1113. card->interface.statcallb(&cmd);
  1114. }
  1115. } else {
  1116. if (card->leased) {
  1117. card->leased = 0;
  1118. sprintf(cbuf, "00;FV2OFF\n");
  1119. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1120. printk(KERN_INFO
  1121. "isdnloop: (%s) Leased-line mode disabled\n",
  1122. CID);
  1123. cmd.command = ISDN_STAT_RUN;
  1124. cmd.driver = card->myid;
  1125. cmd.arg = 0;
  1126. card->interface.statcallb(&cmd);
  1127. }
  1128. }
  1129. return 0;
  1130. default:
  1131. return -EINVAL;
  1132. }
  1133. break;
  1134. case ISDN_CMD_DIAL:
  1135. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1136. return -ENODEV;
  1137. if (card->leased)
  1138. break;
  1139. if ((c->arg & 255) < ISDNLOOP_BCH) {
  1140. char *p;
  1141. char dcode[4];
  1142. a = c->arg;
  1143. p = c->parm.setup.phone;
  1144. if (*p == 's' || *p == 'S') {
  1145. /* Dial for SPV */
  1146. p++;
  1147. strcpy(dcode, "SCA");
  1148. } else
  1149. /* Normal Dial */
  1150. strcpy(dcode, "CAL");
  1151. snprintf(cbuf, sizeof(cbuf),
  1152. "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a + 1),
  1153. dcode, p, c->parm.setup.si1,
  1154. c->parm.setup.si2, c->parm.setup.eazmsn);
  1155. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1156. }
  1157. break;
  1158. case ISDN_CMD_ACCEPTD:
  1159. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1160. return -ENODEV;
  1161. if (c->arg < ISDNLOOP_BCH) {
  1162. a = c->arg + 1;
  1163. cbuf[0] = 0;
  1164. switch (card->l2_proto[a - 1]) {
  1165. case ISDN_PROTO_L2_X75I:
  1166. sprintf(cbuf, "%02d;BX75\n", (int) a);
  1167. break;
  1168. #ifdef CONFIG_ISDN_X25
  1169. case ISDN_PROTO_L2_X25DTE:
  1170. sprintf(cbuf, "%02d;BX2T\n", (int) a);
  1171. break;
  1172. case ISDN_PROTO_L2_X25DCE:
  1173. sprintf(cbuf, "%02d;BX2C\n", (int) a);
  1174. break;
  1175. #endif
  1176. case ISDN_PROTO_L2_HDLC:
  1177. sprintf(cbuf, "%02d;BTRA\n", (int) a);
  1178. break;
  1179. }
  1180. if (strlen(cbuf))
  1181. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1182. sprintf(cbuf, "%02d;DCON_R\n", (int) a);
  1183. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1184. }
  1185. break;
  1186. case ISDN_CMD_ACCEPTB:
  1187. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1188. return -ENODEV;
  1189. if (c->arg < ISDNLOOP_BCH) {
  1190. a = c->arg + 1;
  1191. switch (card->l2_proto[a - 1]) {
  1192. case ISDN_PROTO_L2_X75I:
  1193. sprintf(cbuf, "%02d;BCON_R,BX75\n", (int) a);
  1194. break;
  1195. #ifdef CONFIG_ISDN_X25
  1196. case ISDN_PROTO_L2_X25DTE:
  1197. sprintf(cbuf, "%02d;BCON_R,BX2T\n", (int) a);
  1198. break;
  1199. case ISDN_PROTO_L2_X25DCE:
  1200. sprintf(cbuf, "%02d;BCON_R,BX2C\n", (int) a);
  1201. break;
  1202. #endif
  1203. case ISDN_PROTO_L2_HDLC:
  1204. sprintf(cbuf, "%02d;BCON_R,BTRA\n", (int) a);
  1205. break;
  1206. default:
  1207. sprintf(cbuf, "%02d;BCON_R\n", (int) a);
  1208. }
  1209. printk(KERN_DEBUG "isdnloop writecmd '%s'\n", cbuf);
  1210. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1211. break;
  1212. case ISDN_CMD_HANGUP:
  1213. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1214. return -ENODEV;
  1215. if (c->arg < ISDNLOOP_BCH) {
  1216. a = c->arg + 1;
  1217. sprintf(cbuf, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a, (int) a);
  1218. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1219. }
  1220. break;
  1221. case ISDN_CMD_SETEAZ:
  1222. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1223. return -ENODEV;
  1224. if (card->leased)
  1225. break;
  1226. if (c->arg < ISDNLOOP_BCH) {
  1227. a = c->arg + 1;
  1228. if (card->ptype == ISDN_PTYPE_EURO) {
  1229. sprintf(cbuf, "%02d;MS%s%s\n", (int) a,
  1230. c->parm.num[0] ? "N" : "ALL", c->parm.num);
  1231. } else
  1232. sprintf(cbuf, "%02d;EAZ%s\n", (int) a,
  1233. c->parm.num[0] ? c->parm.num : (u_char *) "0123456789");
  1234. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1235. }
  1236. break;
  1237. case ISDN_CMD_CLREAZ:
  1238. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1239. return -ENODEV;
  1240. if (card->leased)
  1241. break;
  1242. if (c->arg < ISDNLOOP_BCH) {
  1243. a = c->arg + 1;
  1244. if (card->ptype == ISDN_PTYPE_EURO)
  1245. sprintf(cbuf, "%02d;MSNC\n", (int) a);
  1246. else
  1247. sprintf(cbuf, "%02d;EAZC\n", (int) a);
  1248. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1249. }
  1250. break;
  1251. case ISDN_CMD_SETL2:
  1252. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1253. return -ENODEV;
  1254. if ((c->arg & 255) < ISDNLOOP_BCH) {
  1255. a = c->arg;
  1256. switch (a >> 8) {
  1257. case ISDN_PROTO_L2_X75I:
  1258. sprintf(cbuf, "%02d;BX75\n", (int) (a & 255) + 1);
  1259. break;
  1260. #ifdef CONFIG_ISDN_X25
  1261. case ISDN_PROTO_L2_X25DTE:
  1262. sprintf(cbuf, "%02d;BX2T\n", (int) (a & 255) + 1);
  1263. break;
  1264. case ISDN_PROTO_L2_X25DCE:
  1265. sprintf(cbuf, "%02d;BX2C\n", (int) (a & 255) + 1);
  1266. break;
  1267. #endif
  1268. case ISDN_PROTO_L2_HDLC:
  1269. sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
  1270. break;
  1271. case ISDN_PROTO_L2_TRANS:
  1272. sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
  1273. break;
  1274. default:
  1275. return -EINVAL;
  1276. }
  1277. i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card);
  1278. card->l2_proto[a & 255] = (a >> 8);
  1279. }
  1280. break;
  1281. case ISDN_CMD_SETL3:
  1282. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1283. return -ENODEV;
  1284. return 0;
  1285. default:
  1286. return -EINVAL;
  1287. }
  1288. }
  1289. return 0;
  1290. }
  1291. /*
  1292. * Find card with given driverId
  1293. */
  1294. static inline isdnloop_card *
  1295. isdnloop_findcard(int driverid)
  1296. {
  1297. isdnloop_card *p = cards;
  1298. while (p) {
  1299. if (p->myid == driverid)
  1300. return p;
  1301. p = p->next;
  1302. }
  1303. return (isdnloop_card *) 0;
  1304. }
  1305. /*
  1306. * Wrapper functions for interface to linklevel
  1307. */
  1308. static int
  1309. if_command(isdn_ctrl *c)
  1310. {
  1311. isdnloop_card *card = isdnloop_findcard(c->driver);
  1312. if (card)
  1313. return (isdnloop_command(c, card));
  1314. printk(KERN_ERR
  1315. "isdnloop: if_command called with invalid driverId!\n");
  1316. return -ENODEV;
  1317. }
  1318. static int
  1319. if_writecmd(const u_char __user *buf, int len, int id, int channel)
  1320. {
  1321. isdnloop_card *card = isdnloop_findcard(id);
  1322. if (card) {
  1323. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1324. return -ENODEV;
  1325. return (isdnloop_writecmd(buf, len, 1, card));
  1326. }
  1327. printk(KERN_ERR
  1328. "isdnloop: if_writecmd called with invalid driverId!\n");
  1329. return -ENODEV;
  1330. }
  1331. static int
  1332. if_readstatus(u_char __user *buf, int len, int id, int channel)
  1333. {
  1334. isdnloop_card *card = isdnloop_findcard(id);
  1335. if (card) {
  1336. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1337. return -ENODEV;
  1338. return (isdnloop_readstatus(buf, len, card));
  1339. }
  1340. printk(KERN_ERR
  1341. "isdnloop: if_readstatus called with invalid driverId!\n");
  1342. return -ENODEV;
  1343. }
  1344. static int
  1345. if_sendbuf(int id, int channel, int ack, struct sk_buff *skb)
  1346. {
  1347. isdnloop_card *card = isdnloop_findcard(id);
  1348. if (card) {
  1349. if (!(card->flags & ISDNLOOP_FLAGS_RUNNING))
  1350. return -ENODEV;
  1351. /* ack request stored in skb scratch area */
  1352. *(skb->head) = ack;
  1353. return (isdnloop_sendbuf(channel, skb, card));
  1354. }
  1355. printk(KERN_ERR
  1356. "isdnloop: if_sendbuf called with invalid driverId!\n");
  1357. return -ENODEV;
  1358. }
  1359. /*
  1360. * Allocate a new card-struct, initialize it
  1361. * link it into cards-list and register it at linklevel.
  1362. */
  1363. static isdnloop_card *
  1364. isdnloop_initcard(char *id)
  1365. {
  1366. isdnloop_card *card;
  1367. int i;
  1368. if (!(card = kzalloc(sizeof(isdnloop_card), GFP_KERNEL))) {
  1369. printk(KERN_WARNING
  1370. "isdnloop: (%s) Could not allocate card-struct.\n", id);
  1371. return (isdnloop_card *) 0;
  1372. }
  1373. card->interface.owner = THIS_MODULE;
  1374. card->interface.channels = ISDNLOOP_BCH;
  1375. card->interface.hl_hdrlen = 1; /* scratch area for storing ack flag*/
  1376. card->interface.maxbufsize = 4000;
  1377. card->interface.command = if_command;
  1378. card->interface.writebuf_skb = if_sendbuf;
  1379. card->interface.writecmd = if_writecmd;
  1380. card->interface.readstat = if_readstatus;
  1381. card->interface.features = ISDN_FEATURE_L2_X75I |
  1382. #ifdef CONFIG_ISDN_X25
  1383. ISDN_FEATURE_L2_X25DTE |
  1384. ISDN_FEATURE_L2_X25DCE |
  1385. #endif
  1386. ISDN_FEATURE_L2_HDLC |
  1387. ISDN_FEATURE_L3_TRANS |
  1388. ISDN_FEATURE_P_UNKNOWN;
  1389. card->ptype = ISDN_PTYPE_UNKNOWN;
  1390. strlcpy(card->interface.id, id, sizeof(card->interface.id));
  1391. card->msg_buf_write = card->msg_buf;
  1392. card->msg_buf_read = card->msg_buf;
  1393. card->msg_buf_end = &card->msg_buf[sizeof(card->msg_buf) - 1];
  1394. for (i = 0; i < ISDNLOOP_BCH; i++) {
  1395. card->l2_proto[i] = ISDN_PROTO_L2_X75I;
  1396. skb_queue_head_init(&card->bqueue[i]);
  1397. }
  1398. skb_queue_head_init(&card->dqueue);
  1399. spin_lock_init(&card->isdnloop_lock);
  1400. card->next = cards;
  1401. cards = card;
  1402. if (!register_isdn(&card->interface)) {
  1403. cards = cards->next;
  1404. printk(KERN_WARNING
  1405. "isdnloop: Unable to register %s\n", id);
  1406. kfree(card);
  1407. return (isdnloop_card *) 0;
  1408. }
  1409. card->myid = card->interface.channels;
  1410. return card;
  1411. }
  1412. static int
  1413. isdnloop_addcard(char *id1)
  1414. {
  1415. isdnloop_card *card;
  1416. if (!(card = isdnloop_initcard(id1))) {
  1417. return -EIO;
  1418. }
  1419. printk(KERN_INFO
  1420. "isdnloop: (%s) virtual card added\n",
  1421. card->interface.id);
  1422. return 0;
  1423. }
  1424. static int __init
  1425. isdnloop_init(void)
  1426. {
  1427. if (isdnloop_id)
  1428. return (isdnloop_addcard(isdnloop_id));
  1429. return 0;
  1430. }
  1431. static void __exit
  1432. isdnloop_exit(void)
  1433. {
  1434. isdn_ctrl cmd;
  1435. isdnloop_card *card = cards;
  1436. isdnloop_card *last;
  1437. int i;
  1438. isdnloop_stopallcards();
  1439. while (card) {
  1440. cmd.command = ISDN_STAT_UNLOAD;
  1441. cmd.driver = card->myid;
  1442. card->interface.statcallb(&cmd);
  1443. for (i = 0; i < ISDNLOOP_BCH; i++)
  1444. isdnloop_free_queue(card, i);
  1445. card = card->next;
  1446. }
  1447. card = cards;
  1448. while (card) {
  1449. last = card;
  1450. skb_queue_purge(&card->dqueue);
  1451. card = card->next;
  1452. kfree(last);
  1453. }
  1454. printk(KERN_NOTICE "isdnloop-ISDN-driver unloaded\n");
  1455. }
  1456. module_init(isdnloop_init);
  1457. module_exit(isdnloop_exit);