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