isdnloop.c 38 KB

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