isdnloop.c 39 KB

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