isdnloop.c 38 KB

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