isdnloop.c 38 KB

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