]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
1da177e4 LT |
12 | #include <linux/module.h> |
13 | #include <linux/interrupt.h> | |
5a0e3ad6 | 14 | #include <linux/slab.h> |
1da177e4 LT |
15 | #include <linux/init.h> |
16 | #include <linux/sched.h> | |
17 | #include "isdnloop.h" | |
18 | ||
1da177e4 LT |
19 | static char *isdnloop_id = "loop0"; |
20 | ||
21 | MODULE_DESCRIPTION("ISDN4Linux: Pseudo Driver that simulates an ISDN card"); | |
22 | MODULE_AUTHOR("Fritz Elfert"); | |
23 | MODULE_LICENSE("GPL"); | |
8d3b33f6 | 24 | module_param(isdnloop_id, charp, 0); |
1da177e4 LT |
25 | MODULE_PARM_DESC(isdnloop_id, "ID-String of first card"); |
26 | ||
27 | static int isdnloop_addcard(char *); | |
28 | ||
29 | /* | |
30 | * Free queue completely. | |
31 | * | |
32 | * Parameter: | |
33 | * card = pointer to card struct | |
34 | * channel = channel number | |
35 | */ | |
36 | static void | |
475be4d8 | 37 | isdnloop_free_queue(isdnloop_card *card, int channel) |
1da177e4 LT |
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 | */ | |
53 | static void | |
475be4d8 | 54 | isdnloop_bchan_send(isdnloop_card *card, int ch) |
1da177e4 LT |
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]) { | |
50a58b6b BP |
62 | skb = skb_dequeue(&card->bqueue[ch]); |
63 | if (skb) { | |
1da177e4 LT |
64 | len = skb->len; |
65 | card->sndcount[ch] -= len; | |
66 | ack = *(skb->head); /* used as scratch area */ | |
67 | cmd.driver = card->myid; | |
68 | cmd.arg = ch; | |
475be4d8 | 69 | if (rcard) { |
1da177e4 LT |
70 | rcard->interface.rcvcallb_skb(rcard->myid, rch, skb); |
71 | } else { | |
72 | printk(KERN_WARNING "isdnloop: no rcard, skb dropped\n"); | |
73 | dev_kfree_skb(skb); | |
74 | ||
75 | }; | |
76 | cmd.command = ISDN_STAT_BSENT; | |
77 | cmd.parm.length = len; | |
78 | card->interface.statcallb(&cmd); | |
79 | } else | |
80 | card->sndcount[ch] = 0; | |
81 | } | |
82 | } | |
83 | ||
84 | /* | |
85 | * Send/Receive Data to/from the B-Channel. | |
86 | * This routine is called via timer-callback. | |
87 | * It schedules itself while any B-Channel is open. | |
88 | * | |
89 | * Parameter: | |
90 | * data = pointer to card struct, set by kernel timer.data | |
91 | */ | |
92 | static void | |
93 | isdnloop_pollbchan(unsigned long data) | |
94 | { | |
95 | isdnloop_card *card = (isdnloop_card *) data; | |
96 | unsigned long flags; | |
97 | ||
98 | if (card->flags & ISDNLOOP_FLAGS_B1ACTIVE) | |
99 | isdnloop_bchan_send(card, 0); | |
100 | if (card->flags & ISDNLOOP_FLAGS_B2ACTIVE) | |
101 | isdnloop_bchan_send(card, 1); | |
102 | if (card->flags & (ISDNLOOP_FLAGS_B1ACTIVE | ISDNLOOP_FLAGS_B2ACTIVE)) { | |
103 | /* schedule b-channel polling again */ | |
078d3965 | 104 | spin_lock_irqsave(&card->isdnloop_lock, flags); |
1da177e4 LT |
105 | card->rb_timer.expires = jiffies + ISDNLOOP_TIMER_BCREAD; |
106 | add_timer(&card->rb_timer); | |
107 | card->flags |= ISDNLOOP_FLAGS_RBTIMER; | |
078d3965 | 108 | spin_unlock_irqrestore(&card->isdnloop_lock, flags); |
1da177e4 LT |
109 | } else |
110 | card->flags &= ~ISDNLOOP_FLAGS_RBTIMER; | |
111 | } | |
112 | ||
113 | /* | |
114 | * Parse ICN-type setup string and fill fields of setup-struct | |
115 | * with parsed data. | |
116 | * | |
117 | * Parameter: | |
118 | * setup = setup string, format: [caller-id],si1,si2,[called-id] | |
119 | * cmd = pointer to struct to be filled. | |
120 | */ | |
121 | static void | |
475be4d8 | 122 | isdnloop_parse_setup(char *setup, isdn_ctrl *cmd) |
1da177e4 LT |
123 | { |
124 | char *t = setup; | |
125 | char *s = strchr(t, ','); | |
126 | ||
127 | *s++ = '\0'; | |
128 | strlcpy(cmd->parm.setup.phone, t, sizeof(cmd->parm.setup.phone)); | |
129 | s = strchr(t = s, ','); | |
130 | *s++ = '\0'; | |
131 | if (!strlen(t)) | |
132 | cmd->parm.setup.si1 = 0; | |
133 | else | |
134 | cmd->parm.setup.si1 = simple_strtoul(t, NULL, 10); | |
135 | s = strchr(t = s, ','); | |
136 | *s++ = '\0'; | |
137 | if (!strlen(t)) | |
138 | cmd->parm.setup.si2 = 0; | |
139 | else | |
140 | cmd->parm.setup.si2 = | |
475be4d8 | 141 | simple_strtoul(t, NULL, 10); |
1da177e4 LT |
142 | strlcpy(cmd->parm.setup.eazmsn, s, sizeof(cmd->parm.setup.eazmsn)); |
143 | cmd->parm.setup.plan = 0; | |
144 | cmd->parm.setup.screen = 0; | |
145 | } | |
146 | ||
147 | typedef struct isdnloop_stat { | |
148 | char *statstr; | |
149 | int command; | |
150 | int action; | |
151 | } isdnloop_stat; | |
152 | /* *INDENT-OFF* */ | |
cacccb06 | 153 | static isdnloop_stat isdnloop_stat_table[] = { |
1da177e4 LT |
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", | |
475be4d8 | 168 | ISDN_STAT_BHUP, 8}, /* Layer-1 activation failed */ |
1da177e4 LT |
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 | */ | |
184 | static void | |
475be4d8 | 185 | isdnloop_parse_status(u_char *status, int channel, isdnloop_card *card) |
1da177e4 LT |
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) { | |
475be4d8 JP |
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; | |
1da177e4 LT |
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 | */ | |
278 | static void | |
475be4d8 | 279 | isdnloop_putmsg(isdnloop_card *card, unsigned char c) |
1da177e4 LT |
280 | { |
281 | ulong flags; | |
282 | ||
078d3965 | 283 | spin_lock_irqsave(&card->isdnloop_lock, flags); |
1da177e4 LT |
284 | *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; | |
078d3965 | 291 | spin_unlock_irqrestore(&card->isdnloop_lock, flags); |
1da177e4 LT |
292 | } |
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 | */ | |
307 | static void | |
308 | isdnloop_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 | ||
50a58b6b BP |
320 | skb = skb_dequeue(&card->dqueue); |
321 | if (skb) | |
1da177e4 LT |
322 | avail = skb->len; |
323 | else | |
324 | avail = 0; | |
325 | for (left = avail; left > 0; left--) { | |
326 | c = *skb->data; | |
327 | skb_pull(skb, 1); | |
328 | isdnloop_putmsg(card, c); | |
329 | card->imsg[card->iptr] = c; | |
330 | if (card->iptr < 59) | |
331 | card->iptr++; | |
332 | if (!skb->len) { | |
333 | avail++; | |
334 | isdnloop_putmsg(card, '\n'); | |
335 | card->imsg[card->iptr] = 0; | |
336 | card->iptr = 0; | |
337 | if (card->imsg[0] == '0' && card->imsg[1] >= '0' && | |
475be4d8 | 338 | card->imsg[1] <= '2' && card->imsg[2] == ';') { |
1da177e4 LT |
339 | ch = (card->imsg[1] - '0') - 1; |
340 | p = &card->imsg[3]; | |
341 | isdnloop_parse_status(p, ch, card); | |
342 | } else { | |
343 | p = card->imsg; | |
344 | if (!strncmp(p, "DRV1.", 5)) { | |
345 | printk(KERN_INFO "isdnloop: (%s) %s\n", CID, p); | |
346 | if (!strncmp(p + 7, "TC", 2)) { | |
347 | card->ptype = ISDN_PTYPE_1TR6; | |
348 | card->interface.features |= ISDN_FEATURE_P_1TR6; | |
349 | printk(KERN_INFO | |
350 | "isdnloop: (%s) 1TR6-Protocol loaded and running\n", CID); | |
351 | } | |
352 | if (!strncmp(p + 7, "EC", 2)) { | |
353 | card->ptype = ISDN_PTYPE_EURO; | |
354 | card->interface.features |= ISDN_FEATURE_P_EURO; | |
355 | printk(KERN_INFO | |
356 | "isdnloop: (%s) Euro-Protocol loaded and running\n", CID); | |
357 | } | |
358 | continue; | |
359 | ||
360 | } | |
361 | } | |
362 | } | |
363 | } | |
364 | if (avail) { | |
365 | cmd.command = ISDN_STAT_STAVAIL; | |
366 | cmd.driver = card->myid; | |
367 | cmd.arg = avail; | |
368 | card->interface.statcallb(&cmd); | |
369 | } | |
370 | if (card->flags & (ISDNLOOP_FLAGS_B1ACTIVE | ISDNLOOP_FLAGS_B2ACTIVE)) | |
371 | if (!(card->flags & ISDNLOOP_FLAGS_RBTIMER)) { | |
372 | /* schedule b-channel polling */ | |
373 | card->flags |= ISDNLOOP_FLAGS_RBTIMER; | |
078d3965 | 374 | spin_lock_irqsave(&card->isdnloop_lock, flags); |
1da177e4 LT |
375 | del_timer(&card->rb_timer); |
376 | card->rb_timer.function = isdnloop_pollbchan; | |
377 | card->rb_timer.data = (unsigned long) card; | |
378 | card->rb_timer.expires = jiffies + ISDNLOOP_TIMER_BCREAD; | |
379 | add_timer(&card->rb_timer); | |
078d3965 | 380 | spin_unlock_irqrestore(&card->isdnloop_lock, flags); |
1da177e4 LT |
381 | } |
382 | /* schedule again */ | |
078d3965 | 383 | spin_lock_irqsave(&card->isdnloop_lock, flags); |
1da177e4 LT |
384 | card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD; |
385 | add_timer(&card->st_timer); | |
078d3965 | 386 | spin_unlock_irqrestore(&card->isdnloop_lock, flags); |
1da177e4 LT |
387 | } |
388 | ||
389 | /* | |
390 | * Append a packet to the transmit buffer-queue. | |
391 | * | |
392 | * Parameter: | |
393 | * channel = Number of B-channel | |
394 | * skb = packet to send. | |
395 | * card = pointer to card-struct | |
396 | * Return: | |
397 | * Number of bytes transferred, -E??? on error | |
398 | */ | |
399 | static int | |
475be4d8 | 400 | isdnloop_sendbuf(int channel, struct sk_buff *skb, isdnloop_card *card) |
1da177e4 LT |
401 | { |
402 | int len = skb->len; | |
403 | unsigned long flags; | |
404 | struct sk_buff *nskb; | |
405 | ||
406 | if (len > 4000) { | |
407 | printk(KERN_WARNING | |
408 | "isdnloop: Send packet too large\n"); | |
409 | return -EINVAL; | |
410 | } | |
411 | if (len) { | |
412 | if (!(card->flags & (channel) ? ISDNLOOP_FLAGS_B2ACTIVE : ISDNLOOP_FLAGS_B1ACTIVE)) | |
413 | return 0; | |
414 | if (card->sndcount[channel] > ISDNLOOP_MAX_SQUEUE) | |
415 | return 0; | |
078d3965 | 416 | spin_lock_irqsave(&card->isdnloop_lock, flags); |
1da177e4 LT |
417 | nskb = dev_alloc_skb(skb->len); |
418 | if (nskb) { | |
d626f62b ACM |
419 | skb_copy_from_linear_data(skb, |
420 | skb_put(nskb, len), len); | |
1da177e4 LT |
421 | skb_queue_tail(&card->bqueue[channel], nskb); |
422 | dev_kfree_skb(skb); | |
423 | } else | |
424 | len = 0; | |
425 | card->sndcount[channel] += len; | |
078d3965 | 426 | spin_unlock_irqrestore(&card->isdnloop_lock, flags); |
1da177e4 LT |
427 | } |
428 | return len; | |
429 | } | |
430 | ||
431 | /* | |
432 | * Read the messages from the card's ringbuffer | |
433 | * | |
434 | * Parameter: | |
435 | * buf = pointer to buffer. | |
436 | * len = number of bytes to read. | |
437 | * user = flag, 1: called from userlevel 0: called from kernel. | |
438 | * card = pointer to card struct. | |
439 | * Return: | |
440 | * number of bytes actually transferred. | |
441 | */ | |
442 | static int | |
475be4d8 | 443 | isdnloop_readstatus(u_char __user *buf, int len, isdnloop_card *card) |
1da177e4 LT |
444 | { |
445 | int count; | |
446 | u_char __user *p; | |
447 | ||
448 | for (p = buf, count = 0; count < len; p++, count++) { | |
449 | if (card->msg_buf_read == card->msg_buf_write) | |
450 | return count; | |
7786ce19 JG |
451 | if (put_user(*card->msg_buf_read++, p)) |
452 | return -EFAULT; | |
1da177e4 LT |
453 | if (card->msg_buf_read > card->msg_buf_end) |
454 | card->msg_buf_read = card->msg_buf; | |
455 | } | |
456 | return count; | |
457 | } | |
458 | ||
459 | /* | |
460 | * Simulate a card's response by appending it to the cards | |
461 | * message queue. | |
462 | * | |
463 | * Parameter: | |
464 | * card = pointer to card struct. | |
465 | * s = pointer to message-string. | |
466 | * ch = channel: 0 = generic messages, 1 and 2 = D-channel messages. | |
467 | * Return: | |
468 | * 0 on success, 1 on memory squeeze. | |
469 | */ | |
470 | static int | |
475be4d8 | 471 | isdnloop_fake(isdnloop_card *card, char *s, int ch) |
1da177e4 LT |
472 | { |
473 | struct sk_buff *skb; | |
474 | int len = strlen(s) + ((ch >= 0) ? 3 : 0); | |
50a58b6b BP |
475 | skb = dev_alloc_skb(len); |
476 | if (!skb) { | |
1da177e4 LT |
477 | printk(KERN_WARNING "isdnloop: Out of memory in isdnloop_fake\n"); |
478 | return 1; | |
479 | } | |
480 | if (ch >= 0) | |
481 | sprintf(skb_put(skb, 3), "%02d;", ch); | |
59ae1d12 | 482 | skb_put_data(skb, s, strlen(s)); |
1da177e4 LT |
483 | skb_queue_tail(&card->dqueue, skb); |
484 | return 0; | |
485 | } | |
486 | /* *INDENT-OFF* */ | |
cacccb06 | 487 | static isdnloop_stat isdnloop_cmd_table[] = { |
1da177e4 LT |
488 | {"BCON_R", 0, 1}, /* B-Channel connect */ |
489 | {"BCON_I", 0, 17}, /* B-Channel connect ind */ | |
490 | {"BDIS_R", 0, 2}, /* B-Channel disconnect */ | |
491 | {"DDIS_R", 0, 3}, /* D-Channel disconnect */ | |
492 | {"DCON_R", 0, 16}, /* D-Channel connect */ | |
493 | {"DSCA_R", 0, 4}, /* Dial 1TR6-SPV */ | |
494 | {"DCAL_R", 0, 5}, /* Dial */ | |
495 | {"EAZC", 0, 6}, /* Clear EAZ listener */ | |
496 | {"EAZ", 0, 7}, /* Set EAZ listener */ | |
497 | {"SEEAZ", 0, 8}, /* Get EAZ listener */ | |
498 | {"MSN", 0, 9}, /* Set/Clear MSN listener */ | |
499 | {"MSALL", 0, 10}, /* Set multi MSN listeners */ | |
500 | {"SETSIL", 0, 11}, /* Set SI list */ | |
501 | {"SEESIL", 0, 12}, /* Get SI list */ | |
502 | {"SILC", 0, 13}, /* Clear SI list */ | |
503 | {"LOCK", 0, -1}, /* LOCK channel */ | |
504 | {"UNLOCK", 0, -1}, /* UNLOCK channel */ | |
505 | {"FV2ON", 1, 14}, /* Leased mode on */ | |
506 | {"FV2OFF", 1, 15}, /* Leased mode off */ | |
507 | {NULL, 0, -1} | |
508 | }; | |
509 | /* *INDENT-ON* */ | |
510 | ||
511 | ||
512 | /* | |
513 | * Simulate an error-response from a card. | |
514 | * | |
515 | * Parameter: | |
516 | * card = pointer to card struct. | |
517 | */ | |
518 | static void | |
475be4d8 | 519 | isdnloop_fake_err(isdnloop_card *card) |
1da177e4 | 520 | { |
7563487c | 521 | char buf[64]; |
1da177e4 | 522 | |
7563487c | 523 | snprintf(buf, sizeof(buf), "E%s", card->omsg); |
1da177e4 LT |
524 | isdnloop_fake(card, buf, -1); |
525 | isdnloop_fake(card, "NAK", -1); | |
526 | } | |
527 | ||
cacccb06 BP |
528 | static u_char ctable_eu[] = {0x00, 0x11, 0x01, 0x12}; |
529 | static u_char ctable_1t[] = {0x00, 0x3b, 0x01, 0x3a}; | |
1da177e4 LT |
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 | */ | |
542 | static char * | |
475be4d8 | 543 | isdnloop_unicause(isdnloop_card *card, int loc, int cau) |
1da177e4 LT |
544 | { |
545 | static char buf[6]; | |
546 | ||
547 | switch (card->ptype) { | |
475be4d8 JP |
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: | |
3581ec58 | 555 | return "0000"; |
1da177e4 | 556 | } |
3581ec58 | 557 | return buf; |
1da177e4 LT |
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 | */ | |
568 | static void | |
475be4d8 | 569 | isdnloop_atimeout(isdnloop_card *card, int ch) |
1da177e4 LT |
570 | { |
571 | unsigned long flags; | |
572 | char buf[60]; | |
573 | ||
078d3965 | 574 | spin_lock_irqsave(&card->isdnloop_lock, flags); |
1da177e4 LT |
575 | 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); | |
078d3965 | 584 | spin_unlock_irqrestore(&card->isdnloop_lock, flags); |
1da177e4 LT |
585 | } |
586 | ||
587 | /* | |
588 | * Wrapper for isdnloop_atimeout(). | |
589 | */ | |
590 | static void | |
591 | isdnloop_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 | */ | |
600 | static void | |
601 | isdnloop_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 | */ | |
614 | static void | |
475be4d8 | 615 | isdnloop_start_ctimer(isdnloop_card *card, int ch) |
1da177e4 LT |
616 | { |
617 | unsigned long flags; | |
618 | ||
078d3965 | 619 | spin_lock_irqsave(&card->isdnloop_lock, flags); |
1da177e4 LT |
620 | 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]); | |
078d3965 | 628 | spin_unlock_irqrestore(&card->isdnloop_lock, flags); |
1da177e4 LT |
629 | } |
630 | ||
631 | /* | |
632 | * Kill a pending channel watchdog. | |
633 | * | |
634 | * Parameter: | |
635 | * card = pointer to card struct. | |
636 | * ch = channel (0-based). | |
637 | */ | |
638 | static void | |
475be4d8 | 639 | isdnloop_kill_ctimer(isdnloop_card *card, int ch) |
1da177e4 LT |
640 | { |
641 | unsigned long flags; | |
642 | ||
078d3965 | 643 | spin_lock_irqsave(&card->isdnloop_lock, flags); |
1da177e4 | 644 | del_timer(&card->c_timer[ch]); |
078d3965 | 645 | spin_unlock_irqrestore(&card->isdnloop_lock, flags); |
1da177e4 LT |
646 | } |
647 | ||
cacccb06 BP |
648 | static u_char si2bit[] = {0, 1, 0, 0, 0, 2, 0, 4, 0, 0}; |
649 | static u_char bit2si[] = {1, 5, 7}; | |
1da177e4 LT |
650 | |
651 | /* | |
652 | * Try finding a listener for an outgoing call. | |
653 | * | |
654 | * Parameter: | |
655 | * card = pointer to calling card. | |
656 | * p = pointer to ICN-type setup-string. | |
657 | * lch = channel of calling card. | |
658 | * cmd = pointer to struct to be filled when parsing setup. | |
659 | * Return: | |
660 | * 0 = found match, alerting should happen. | |
661 | * 1 = found matching number but it is busy. | |
662 | * 2 = no matching listener. | |
663 | * 3 = found matching number but SI does not match. | |
664 | */ | |
665 | static int | |
475be4d8 | 666 | isdnloop_try_call(isdnloop_card *card, char *p, int lch, isdn_ctrl *cmd) |
1da177e4 LT |
667 | { |
668 | isdnloop_card *cc = cards; | |
669 | unsigned long flags; | |
670 | int ch; | |
671 | int num_match; | |
672 | int i; | |
673 | char *e; | |
674 | char nbuf[32]; | |
675 | ||
676 | isdnloop_parse_setup(p, cmd); | |
677 | while (cc) { | |
678 | for (ch = 0; ch < 2; ch++) { | |
679 | /* Exclude ourself */ | |
680 | if ((cc == card) && (ch == lch)) | |
681 | continue; | |
682 | num_match = 0; | |
683 | switch (cc->ptype) { | |
475be4d8 JP |
684 | case ISDN_PTYPE_EURO: |
685 | for (i = 0; i < 3; i++) | |
686 | if (!(strcmp(cc->s0num[i], cmd->parm.setup.phone))) | |
687 | num_match = 1; | |
688 | break; | |
689 | case ISDN_PTYPE_1TR6: | |
690 | e = cc->eazlist[ch]; | |
691 | while (*e) { | |
692 | sprintf(nbuf, "%s%c", cc->s0num[0], *e); | |
693 | if (!(strcmp(nbuf, cmd->parm.setup.phone))) | |
694 | num_match = 1; | |
695 | e++; | |
696 | } | |
1da177e4 LT |
697 | } |
698 | if (num_match) { | |
078d3965 | 699 | spin_lock_irqsave(&card->isdnloop_lock, flags); |
1da177e4 LT |
700 | /* channel idle? */ |
701 | if (!(cc->rcard[ch])) { | |
702 | /* Check SI */ | |
703 | if (!(si2bit[cmd->parm.setup.si1] & cc->sil[ch])) { | |
078d3965 | 704 | spin_unlock_irqrestore(&card->isdnloop_lock, flags); |
1da177e4 LT |
705 | return 3; |
706 | } | |
707 | /* ch is idle, si and number matches */ | |
708 | cc->rcard[ch] = card; | |
709 | cc->rch[ch] = lch; | |
710 | card->rcard[lch] = cc; | |
711 | card->rch[lch] = ch; | |
078d3965 | 712 | spin_unlock_irqrestore(&card->isdnloop_lock, flags); |
1da177e4 LT |
713 | return 0; |
714 | } else { | |
078d3965 | 715 | spin_unlock_irqrestore(&card->isdnloop_lock, flags); |
1da177e4 LT |
716 | /* num matches, but busy */ |
717 | if (ch == 1) | |
718 | return 1; | |
719 | } | |
720 | } | |
721 | } | |
722 | cc = cc->next; | |
723 | } | |
724 | return 2; | |
725 | } | |
726 | ||
727 | /* | |
728 | * Depending on D-channel protocol and caller/called, modify | |
729 | * phone number. | |
730 | * | |
731 | * Parameter: | |
732 | * card = pointer to card struct. | |
733 | * phone = pointer phone number. | |
734 | * caller = flag: 1 = caller, 0 = called. | |
735 | * Return: | |
736 | * pointer to new phone number. | |
737 | */ | |
738 | static char * | |
475be4d8 | 739 | isdnloop_vstphone(isdnloop_card *card, char *phone, int caller) |
1da177e4 LT |
740 | { |
741 | int i; | |
742 | static char nphone[30]; | |
743 | ||
744 | if (!card) { | |
745 | printk("BUG!!!\n"); | |
746 | return ""; | |
747 | } | |
748 | switch (card->ptype) { | |
475be4d8 JP |
749 | case ISDN_PTYPE_EURO: |
750 | if (caller) { | |
751 | for (i = 0; i < 2; i++) | |
752 | if (!(strcmp(card->s0num[i], phone))) | |
3581ec58 BP |
753 | return phone; |
754 | return card->s0num[0]; | |
475be4d8 | 755 | } |
3581ec58 | 756 | return phone; |
475be4d8 JP |
757 | break; |
758 | case ISDN_PTYPE_1TR6: | |
759 | if (caller) { | |
760 | sprintf(nphone, "%s%c", card->s0num[0], phone[0]); | |
3581ec58 | 761 | return nphone; |
475be4d8 | 762 | } else |
3581ec58 | 763 | return &phone[strlen(phone) - 1]; |
475be4d8 | 764 | break; |
1da177e4 LT |
765 | } |
766 | return ""; | |
767 | } | |
768 | ||
769 | /* | |
770 | * Parse an ICN-type command string sent to the 'card'. | |
771 | * Perform misc. actions depending on the command. | |
772 | * | |
773 | * Parameter: | |
774 | * card = pointer to card struct. | |
775 | */ | |
776 | static void | |
475be4d8 | 777 | isdnloop_parse_cmd(isdnloop_card *card) |
1da177e4 LT |
778 | { |
779 | char *p = card->omsg; | |
780 | isdn_ctrl cmd; | |
781 | char buf[60]; | |
782 | isdnloop_stat *s = isdnloop_cmd_table; | |
783 | int action = -1; | |
784 | int i; | |
785 | int ch; | |
786 | ||
787 | if ((card->omsg[0] != '0') && (card->omsg[2] != ';')) { | |
788 | isdnloop_fake_err(card); | |
789 | return; | |
790 | } | |
791 | ch = card->omsg[1] - '0'; | |
792 | if ((ch < 0) || (ch > 2)) { | |
793 | isdnloop_fake_err(card); | |
794 | return; | |
795 | } | |
796 | p += 3; | |
797 | while (s->statstr) { | |
798 | if (!strncmp(p, s->statstr, strlen(s->statstr))) { | |
799 | action = s->action; | |
800 | if (s->command && (ch != 0)) { | |
801 | isdnloop_fake_err(card); | |
802 | return; | |
803 | } | |
804 | break; | |
805 | } | |
806 | s++; | |
807 | } | |
808 | if (action == -1) | |
809 | return; | |
810 | switch (action) { | |
475be4d8 JP |
811 | case 1: |
812 | /* 0x;BCON_R */ | |
813 | if (card->rcard[ch - 1]) { | |
814 | isdnloop_fake(card->rcard[ch - 1], "BCON_I", | |
815 | card->rch[ch - 1] + 1); | |
816 | isdnloop_fake(card, "BCON_C", ch); | |
817 | } | |
818 | break; | |
819 | case 17: | |
820 | /* 0x;BCON_I */ | |
821 | if (card->rcard[ch - 1]) { | |
822 | isdnloop_fake(card->rcard[ch - 1], "BCON_C", | |
823 | card->rch[ch - 1] + 1); | |
824 | } | |
825 | break; | |
826 | case 2: | |
827 | /* 0x;BDIS_R */ | |
828 | isdnloop_fake(card, "BDIS_C", ch); | |
829 | if (card->rcard[ch - 1]) { | |
830 | isdnloop_fake(card->rcard[ch - 1], "BDIS_I", | |
831 | card->rch[ch - 1] + 1); | |
832 | } | |
833 | break; | |
834 | case 16: | |
835 | /* 0x;DCON_R */ | |
836 | isdnloop_kill_ctimer(card, ch - 1); | |
837 | if (card->rcard[ch - 1]) { | |
838 | isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]); | |
839 | isdnloop_fake(card->rcard[ch - 1], "DCON_C", | |
840 | card->rch[ch - 1] + 1); | |
841 | isdnloop_fake(card, "DCON_C", ch); | |
842 | } | |
843 | break; | |
844 | case 3: | |
845 | /* 0x;DDIS_R */ | |
846 | isdnloop_kill_ctimer(card, ch - 1); | |
847 | if (card->rcard[ch - 1]) { | |
848 | isdnloop_kill_ctimer(card->rcard[ch - 1], card->rch[ch - 1]); | |
849 | isdnloop_fake(card->rcard[ch - 1], "DDIS_I", | |
850 | card->rch[ch - 1] + 1); | |
851 | card->rcard[ch - 1] = NULL; | |
852 | } | |
853 | isdnloop_fake(card, "DDIS_C", ch); | |
854 | break; | |
855 | case 4: | |
856 | /* 0x;DSCA_Rdd,yy,zz,oo */ | |
857 | if (card->ptype != ISDN_PTYPE_1TR6) { | |
858 | isdnloop_fake_err(card); | |
859 | return; | |
860 | } | |
861 | /* Fall through */ | |
862 | case 5: | |
863 | /* 0x;DCAL_Rdd,yy,zz,oo */ | |
864 | p += 6; | |
865 | switch (isdnloop_try_call(card, p, ch - 1, &cmd)) { | |
866 | case 0: | |
867 | /* Alerting */ | |
868 | sprintf(buf, "D%s_I%s,%02d,%02d,%s", | |
869 | (action == 4) ? "SCA" : "CAL", | |
870 | isdnloop_vstphone(card, cmd.parm.setup.eazmsn, 1), | |
871 | cmd.parm.setup.si1, | |
872 | cmd.parm.setup.si2, | |
873 | isdnloop_vstphone(card->rcard[ch - 1], | |
874 | cmd.parm.setup.phone, 0)); | |
875 | isdnloop_fake(card->rcard[ch - 1], buf, card->rch[ch - 1] + 1); | |
1da177e4 | 876 | /* Fall through */ |
475be4d8 JP |
877 | case 3: |
878 | /* si1 does not match, don't alert but start timer */ | |
879 | isdnloop_start_ctimer(card, ch - 1); | |
1da177e4 | 880 | break; |
475be4d8 JP |
881 | case 1: |
882 | /* Remote busy */ | |
883 | isdnloop_fake(card, "DDIS_I", ch); | |
884 | sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 1)); | |
885 | isdnloop_fake(card, buf, ch); | |
1da177e4 | 886 | break; |
475be4d8 JP |
887 | case 2: |
888 | /* No such user */ | |
889 | isdnloop_fake(card, "DDIS_I", ch); | |
890 | sprintf(buf, "CAU%s", isdnloop_unicause(card, 1, 2)); | |
891 | isdnloop_fake(card, buf, ch); | |
1da177e4 | 892 | break; |
475be4d8 JP |
893 | } |
894 | break; | |
895 | case 6: | |
896 | /* 0x;EAZC */ | |
897 | card->eazlist[ch - 1][0] = '\0'; | |
898 | break; | |
899 | case 7: | |
900 | /* 0x;EAZ */ | |
901 | p += 3; | |
7563487c DC |
902 | if (strlen(p) >= sizeof(card->eazlist[0])) |
903 | break; | |
475be4d8 JP |
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; | |
1da177e4 LT |
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 | |
25985edc | 954 | * command to the read message ringbuffer, preceding it with a '>'. |
1da177e4 LT |
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 | */ | |
965 | static int | |
475be4d8 | 966 | isdnloop_writecmd(const u_char *buf, int len, int user, isdnloop_card *card) |
1da177e4 LT |
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 | */ | |
1015 | static void | |
475be4d8 | 1016 | isdnloop_stopcard(isdnloop_card *card) |
1da177e4 LT |
1017 | { |
1018 | unsigned long flags; | |
1019 | isdn_ctrl cmd; | |
1020 | ||
078d3965 | 1021 | spin_lock_irqsave(&card->isdnloop_lock, flags); |
1da177e4 LT |
1022 | 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 | } | |
078d3965 | 1032 | spin_unlock_irqrestore(&card->isdnloop_lock, flags); |
1da177e4 LT |
1033 | } |
1034 | ||
1035 | /* | |
1036 | * Stop all cards before unload. | |
1037 | */ | |
1038 | static void | |
1039 | isdnloop_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 | */ | |
1060 | static int | |
475be4d8 | 1061 | isdnloop_start(isdnloop_card *card, isdnloop_sdef *sdefp) |
1da177e4 LT |
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; | |
77bc6bed YH |
1071 | |
1072 | for (i = 0; i < 3; i++) { | |
1073 | if (!memchr(sdef.num[i], 0, sizeof(sdef.num[i]))) | |
1074 | return -EINVAL; | |
1075 | } | |
1076 | ||
078d3965 | 1077 | spin_lock_irqsave(&card->isdnloop_lock, flags); |
1da177e4 | 1078 | switch (sdef.ptype) { |
475be4d8 JP |
1079 | case ISDN_PTYPE_EURO: |
1080 | if (isdnloop_fake(card, "DRV1.23EC-Q.931-CAPI-CNS-BASIS-20.02.96", | |
1081 | -1)) { | |
078d3965 | 1082 | spin_unlock_irqrestore(&card->isdnloop_lock, flags); |
475be4d8 JP |
1083 | return -ENOMEM; |
1084 | } | |
1085 | card->sil[0] = card->sil[1] = 4; | |
1086 | if (isdnloop_fake(card, "TEI OK", 0)) { | |
1087 | spin_unlock_irqrestore(&card->isdnloop_lock, flags); | |
1088 | return -ENOMEM; | |
1089 | } | |
f9a23c84 DC |
1090 | for (i = 0; i < 3; i++) { |
1091 | strlcpy(card->s0num[i], sdef.num[i], | |
1092 | sizeof(card->s0num[0])); | |
1093 | } | |
475be4d8 JP |
1094 | break; |
1095 | case ISDN_PTYPE_1TR6: | |
1096 | if (isdnloop_fake(card, "DRV1.04TC-1TR6-CAPI-CNS-BASIS-29.11.95", | |
1097 | -1)) { | |
1098 | spin_unlock_irqrestore(&card->isdnloop_lock, flags); | |
1099 | return -ENOMEM; | |
1100 | } | |
1101 | card->sil[0] = card->sil[1] = 4; | |
1102 | if (isdnloop_fake(card, "TEI OK", 0)) { | |
1103 | spin_unlock_irqrestore(&card->isdnloop_lock, flags); | |
1104 | return -ENOMEM; | |
1105 | } | |
f9a23c84 | 1106 | strlcpy(card->s0num[0], sdef.num[0], sizeof(card->s0num[0])); |
475be4d8 JP |
1107 | card->s0num[1][0] = '\0'; |
1108 | card->s0num[2][0] = '\0'; | |
1109 | break; | |
1110 | default: | |
1111 | spin_unlock_irqrestore(&card->isdnloop_lock, flags); | |
1112 | printk(KERN_WARNING "isdnloop: Illegal D-channel protocol %d\n", | |
1113 | sdef.ptype); | |
1114 | return -EINVAL; | |
1da177e4 LT |
1115 | } |
1116 | init_timer(&card->st_timer); | |
1117 | card->st_timer.expires = jiffies + ISDNLOOP_TIMER_DCREAD; | |
1118 | card->st_timer.function = isdnloop_polldchan; | |
1119 | card->st_timer.data = (unsigned long) card; | |
1120 | add_timer(&card->st_timer); | |
1121 | card->flags |= ISDNLOOP_FLAGS_RUNNING; | |
078d3965 | 1122 | spin_unlock_irqrestore(&card->isdnloop_lock, flags); |
1da177e4 LT |
1123 | return 0; |
1124 | } | |
1125 | ||
1126 | /* | |
1127 | * Main handler for commands sent by linklevel. | |
1128 | */ | |
1129 | static int | |
475be4d8 | 1130 | isdnloop_command(isdn_ctrl *c, isdnloop_card *card) |
1da177e4 LT |
1131 | { |
1132 | ulong a; | |
1133 | int i; | |
7563487c | 1134 | char cbuf[80]; |
1da177e4 LT |
1135 | isdn_ctrl cmd; |
1136 | isdnloop_cdef cdef; | |
1137 | ||
1138 | switch (c->command) { | |
475be4d8 JP |
1139 | case ISDN_CMD_IOCTL: |
1140 | memcpy(&a, c->parm.num, sizeof(ulong)); | |
1141 | switch (c->arg) { | |
1142 | case ISDNLOOP_IOCTL_DEBUGVAR: | |
1143 | return (ulong) card; | |
1144 | case ISDNLOOP_IOCTL_STARTUP: | |
3581ec58 | 1145 | return isdnloop_start(card, (isdnloop_sdef *) a); |
475be4d8 JP |
1146 | break; |
1147 | case ISDNLOOP_IOCTL_ADDCARD: | |
1148 | if (copy_from_user((char *)&cdef, | |
1149 | (char *)a, | |
1150 | sizeof(cdef))) | |
1151 | return -EFAULT; | |
3581ec58 | 1152 | return isdnloop_addcard(cdef.id1); |
475be4d8 JP |
1153 | break; |
1154 | case ISDNLOOP_IOCTL_LEASEDCFG: | |
1155 | if (a) { | |
1156 | if (!card->leased) { | |
1157 | card->leased = 1; | |
1158 | while (card->ptype == ISDN_PTYPE_UNKNOWN) | |
1159 | schedule_timeout_interruptible(10); | |
1160 | schedule_timeout_interruptible(10); | |
1161 | sprintf(cbuf, "00;FV2ON\n01;EAZ1\n02;EAZ2\n"); | |
1162 | i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card); | |
1163 | printk(KERN_INFO | |
1164 | "isdnloop: (%s) Leased-line mode enabled\n", | |
1165 | CID); | |
1166 | cmd.command = ISDN_STAT_RUN; | |
1167 | cmd.driver = card->myid; | |
1168 | cmd.arg = 0; | |
1169 | card->interface.statcallb(&cmd); | |
1170 | } | |
1171 | } else { | |
1172 | if (card->leased) { | |
1173 | card->leased = 0; | |
1174 | sprintf(cbuf, "00;FV2OFF\n"); | |
1175 | i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card); | |
1176 | printk(KERN_INFO | |
1177 | "isdnloop: (%s) Leased-line mode disabled\n", | |
1178 | CID); | |
1179 | cmd.command = ISDN_STAT_RUN; | |
1180 | cmd.driver = card->myid; | |
1181 | cmd.arg = 0; | |
1182 | card->interface.statcallb(&cmd); | |
1183 | } | |
1da177e4 | 1184 | } |
475be4d8 JP |
1185 | return 0; |
1186 | default: | |
1187 | return -EINVAL; | |
1188 | } | |
1189 | break; | |
1190 | case ISDN_CMD_DIAL: | |
1191 | if (!(card->flags & ISDNLOOP_FLAGS_RUNNING)) | |
1192 | return -ENODEV; | |
1193 | if (card->leased) | |
1da177e4 | 1194 | break; |
475be4d8 JP |
1195 | if ((c->arg & 255) < ISDNLOOP_BCH) { |
1196 | char *p; | |
475be4d8 JP |
1197 | char dcode[4]; |
1198 | ||
1199 | a = c->arg; | |
1200 | p = c->parm.setup.phone; | |
1201 | if (*p == 's' || *p == 'S') { | |
1202 | /* Dial for SPV */ | |
1203 | p++; | |
1204 | strcpy(dcode, "SCA"); | |
1205 | } else | |
1206 | /* Normal Dial */ | |
1207 | strcpy(dcode, "CAL"); | |
7563487c DC |
1208 | snprintf(cbuf, sizeof(cbuf), |
1209 | "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a + 1), | |
1210 | dcode, p, c->parm.setup.si1, | |
1211 | c->parm.setup.si2, c->parm.setup.eazmsn); | |
475be4d8 JP |
1212 | i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card); |
1213 | } | |
1214 | break; | |
1215 | case ISDN_CMD_ACCEPTD: | |
1216 | if (!(card->flags & ISDNLOOP_FLAGS_RUNNING)) | |
1217 | return -ENODEV; | |
1218 | if (c->arg < ISDNLOOP_BCH) { | |
1219 | a = c->arg + 1; | |
1220 | cbuf[0] = 0; | |
1221 | switch (card->l2_proto[a - 1]) { | |
1222 | case ISDN_PROTO_L2_X75I: | |
1223 | sprintf(cbuf, "%02d;BX75\n", (int) a); | |
1224 | break; | |
1225 | #ifdef CONFIG_ISDN_X25 | |
1226 | case ISDN_PROTO_L2_X25DTE: | |
1227 | sprintf(cbuf, "%02d;BX2T\n", (int) a); | |
1228 | break; | |
1229 | case ISDN_PROTO_L2_X25DCE: | |
1230 | sprintf(cbuf, "%02d;BX2C\n", (int) a); | |
1231 | break; | |
1232 | #endif | |
1233 | case ISDN_PROTO_L2_HDLC: | |
1234 | sprintf(cbuf, "%02d;BTRA\n", (int) a); | |
1235 | break; | |
1236 | } | |
1237 | if (strlen(cbuf)) | |
1da177e4 | 1238 | i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card); |
475be4d8 JP |
1239 | sprintf(cbuf, "%02d;DCON_R\n", (int) a); |
1240 | i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card); | |
1241 | } | |
1242 | break; | |
1243 | case ISDN_CMD_ACCEPTB: | |
1244 | if (!(card->flags & ISDNLOOP_FLAGS_RUNNING)) | |
1245 | return -ENODEV; | |
1246 | if (c->arg < ISDNLOOP_BCH) { | |
1247 | a = c->arg + 1; | |
1248 | switch (card->l2_proto[a - 1]) { | |
1249 | case ISDN_PROTO_L2_X75I: | |
1250 | sprintf(cbuf, "%02d;BCON_R,BX75\n", (int) a); | |
1251 | break; | |
1252 | #ifdef CONFIG_ISDN_X25 | |
1253 | case ISDN_PROTO_L2_X25DTE: | |
1254 | sprintf(cbuf, "%02d;BCON_R,BX2T\n", (int) a); | |
1255 | break; | |
1256 | case ISDN_PROTO_L2_X25DCE: | |
1257 | sprintf(cbuf, "%02d;BCON_R,BX2C\n", (int) a); | |
1258 | break; | |
1259 | #endif | |
1260 | case ISDN_PROTO_L2_HDLC: | |
1261 | sprintf(cbuf, "%02d;BCON_R,BTRA\n", (int) a); | |
1262 | break; | |
1263 | default: | |
1264 | sprintf(cbuf, "%02d;BCON_R\n", (int) a); | |
1da177e4 | 1265 | } |
475be4d8 JP |
1266 | printk(KERN_DEBUG "isdnloop writecmd '%s'\n", cbuf); |
1267 | i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card); | |
1da177e4 | 1268 | break; |
475be4d8 | 1269 | case ISDN_CMD_HANGUP: |
eaa0ff15 | 1270 | if (!(card->flags & ISDNLOOP_FLAGS_RUNNING)) |
1da177e4 LT |
1271 | return -ENODEV; |
1272 | if (c->arg < ISDNLOOP_BCH) { | |
1273 | a = c->arg + 1; | |
475be4d8 | 1274 | sprintf(cbuf, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a, (int) a); |
1da177e4 LT |
1275 | i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card); |
1276 | } | |
1277 | break; | |
475be4d8 | 1278 | case ISDN_CMD_SETEAZ: |
eaa0ff15 | 1279 | if (!(card->flags & ISDNLOOP_FLAGS_RUNNING)) |
1da177e4 | 1280 | return -ENODEV; |
475be4d8 JP |
1281 | if (card->leased) |
1282 | break; | |
1da177e4 LT |
1283 | if (c->arg < ISDNLOOP_BCH) { |
1284 | a = c->arg + 1; | |
475be4d8 JP |
1285 | if (card->ptype == ISDN_PTYPE_EURO) { |
1286 | sprintf(cbuf, "%02d;MS%s%s\n", (int) a, | |
1287 | c->parm.num[0] ? "N" : "ALL", c->parm.num); | |
1288 | } else | |
1289 | sprintf(cbuf, "%02d;EAZ%s\n", (int) a, | |
1290 | c->parm.num[0] ? c->parm.num : (u_char *) "0123456789"); | |
1da177e4 | 1291 | i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card); |
475be4d8 JP |
1292 | } |
1293 | break; | |
1da177e4 | 1294 | case ISDN_CMD_CLREAZ: |
475be4d8 JP |
1295 | if (!(card->flags & ISDNLOOP_FLAGS_RUNNING)) |
1296 | return -ENODEV; | |
1297 | if (card->leased) | |
1da177e4 | 1298 | break; |
475be4d8 JP |
1299 | if (c->arg < ISDNLOOP_BCH) { |
1300 | a = c->arg + 1; | |
1301 | if (card->ptype == ISDN_PTYPE_EURO) | |
1302 | sprintf(cbuf, "%02d;MSNC\n", (int) a); | |
1303 | else | |
1304 | sprintf(cbuf, "%02d;EAZC\n", (int) a); | |
1305 | i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card); | |
1306 | } | |
1307 | break; | |
1da177e4 | 1308 | case ISDN_CMD_SETL2: |
475be4d8 JP |
1309 | if (!(card->flags & ISDNLOOP_FLAGS_RUNNING)) |
1310 | return -ENODEV; | |
1311 | if ((c->arg & 255) < ISDNLOOP_BCH) { | |
1312 | a = c->arg; | |
1313 | switch (a >> 8) { | |
1314 | case ISDN_PROTO_L2_X75I: | |
1315 | sprintf(cbuf, "%02d;BX75\n", (int) (a & 255) + 1); | |
1316 | break; | |
1da177e4 | 1317 | #ifdef CONFIG_ISDN_X25 |
475be4d8 JP |
1318 | case ISDN_PROTO_L2_X25DTE: |
1319 | sprintf(cbuf, "%02d;BX2T\n", (int) (a & 255) + 1); | |
1320 | break; | |
1321 | case ISDN_PROTO_L2_X25DCE: | |
1322 | sprintf(cbuf, "%02d;BX2C\n", (int) (a & 255) + 1); | |
1323 | break; | |
1da177e4 | 1324 | #endif |
475be4d8 JP |
1325 | case ISDN_PROTO_L2_HDLC: |
1326 | sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1); | |
1327 | break; | |
1328 | case ISDN_PROTO_L2_TRANS: | |
1329 | sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1); | |
1330 | break; | |
1331 | default: | |
1332 | return -EINVAL; | |
1da177e4 | 1333 | } |
475be4d8 JP |
1334 | i = isdnloop_writecmd(cbuf, strlen(cbuf), 0, card); |
1335 | card->l2_proto[a & 255] = (a >> 8); | |
1336 | } | |
1337 | break; | |
1da177e4 | 1338 | case ISDN_CMD_SETL3: |
475be4d8 JP |
1339 | if (!(card->flags & ISDNLOOP_FLAGS_RUNNING)) |
1340 | return -ENODEV; | |
1341 | return 0; | |
1da177e4 | 1342 | default: |
475be4d8 JP |
1343 | return -EINVAL; |
1344 | } | |
1da177e4 LT |
1345 | } |
1346 | return 0; | |
1347 | } | |
1348 | ||
1349 | /* | |
1350 | * Find card with given driverId | |
1351 | */ | |
1352 | static inline isdnloop_card * | |
1353 | isdnloop_findcard(int driverid) | |
1354 | { | |
1355 | isdnloop_card *p = cards; | |
1356 | ||
1357 | while (p) { | |
1358 | if (p->myid == driverid) | |
1359 | return p; | |
1360 | p = p->next; | |
1361 | } | |
1362 | return (isdnloop_card *) 0; | |
1363 | } | |
1364 | ||
1365 | /* | |
1366 | * Wrapper functions for interface to linklevel | |
1367 | */ | |
1368 | static int | |
475be4d8 | 1369 | if_command(isdn_ctrl *c) |
1da177e4 LT |
1370 | { |
1371 | isdnloop_card *card = isdnloop_findcard(c->driver); | |
1372 | ||
1373 | if (card) | |
3581ec58 | 1374 | return isdnloop_command(c, card); |
1da177e4 LT |
1375 | printk(KERN_ERR |
1376 | "isdnloop: if_command called with invalid driverId!\n"); | |
1377 | return -ENODEV; | |
1378 | } | |
1379 | ||
1380 | static int | |
1381 | if_writecmd(const u_char __user *buf, int len, int id, int channel) | |
1382 | { | |
1383 | isdnloop_card *card = isdnloop_findcard(id); | |
1384 | ||
1385 | if (card) { | |
ae91d60b | 1386 | if (!(card->flags & ISDNLOOP_FLAGS_RUNNING)) |
1da177e4 | 1387 | return -ENODEV; |
3581ec58 | 1388 | return isdnloop_writecmd(buf, len, 1, card); |
1da177e4 LT |
1389 | } |
1390 | printk(KERN_ERR | |
1391 | "isdnloop: if_writecmd called with invalid driverId!\n"); | |
1392 | return -ENODEV; | |
1393 | } | |
1394 | ||
1395 | static int | |
1396 | if_readstatus(u_char __user *buf, int len, int id, int channel) | |
1397 | { | |
1398 | isdnloop_card *card = isdnloop_findcard(id); | |
1399 | ||
1400 | if (card) { | |
eaa0ff15 | 1401 | if (!(card->flags & ISDNLOOP_FLAGS_RUNNING)) |
1da177e4 | 1402 | return -ENODEV; |
3581ec58 | 1403 | return isdnloop_readstatus(buf, len, card); |
1da177e4 LT |
1404 | } |
1405 | printk(KERN_ERR | |
1406 | "isdnloop: if_readstatus called with invalid driverId!\n"); | |
1407 | return -ENODEV; | |
1408 | } | |
1409 | ||
1410 | static int | |
1411 | if_sendbuf(int id, int channel, int ack, struct sk_buff *skb) | |
1412 | { | |
1413 | isdnloop_card *card = isdnloop_findcard(id); | |
1414 | ||
1415 | if (card) { | |
eaa0ff15 | 1416 | if (!(card->flags & ISDNLOOP_FLAGS_RUNNING)) |
1da177e4 LT |
1417 | return -ENODEV; |
1418 | /* ack request stored in skb scratch area */ | |
1419 | *(skb->head) = ack; | |
3581ec58 | 1420 | return isdnloop_sendbuf(channel, skb, card); |
1da177e4 LT |
1421 | } |
1422 | printk(KERN_ERR | |
1423 | "isdnloop: if_sendbuf called with invalid driverId!\n"); | |
1424 | return -ENODEV; | |
1425 | } | |
1426 | ||
1427 | /* | |
1428 | * Allocate a new card-struct, initialize it | |
1429 | * link it into cards-list and register it at linklevel. | |
1430 | */ | |
1431 | static isdnloop_card * | |
1432 | isdnloop_initcard(char *id) | |
1433 | { | |
1434 | isdnloop_card *card; | |
1435 | int i; | |
50a58b6b BP |
1436 | card = kzalloc(sizeof(isdnloop_card), GFP_KERNEL); |
1437 | if (!card) { | |
1da177e4 | 1438 | printk(KERN_WARNING |
475be4d8 | 1439 | "isdnloop: (%s) Could not allocate card-struct.\n", id); |
1da177e4 LT |
1440 | return (isdnloop_card *) 0; |
1441 | } | |
1da177e4 LT |
1442 | card->interface.owner = THIS_MODULE; |
1443 | card->interface.channels = ISDNLOOP_BCH; | |
475be4d8 | 1444 | card->interface.hl_hdrlen = 1; /* scratch area for storing ack flag*/ |
1da177e4 LT |
1445 | card->interface.maxbufsize = 4000; |
1446 | card->interface.command = if_command; | |
1447 | card->interface.writebuf_skb = if_sendbuf; | |
1448 | card->interface.writecmd = if_writecmd; | |
1449 | card->interface.readstat = if_readstatus; | |
1450 | card->interface.features = ISDN_FEATURE_L2_X75I | | |
1451 | #ifdef CONFIG_ISDN_X25 | |
475be4d8 JP |
1452 | ISDN_FEATURE_L2_X25DTE | |
1453 | ISDN_FEATURE_L2_X25DCE | | |
1da177e4 | 1454 | #endif |
475be4d8 JP |
1455 | ISDN_FEATURE_L2_HDLC | |
1456 | ISDN_FEATURE_L3_TRANS | | |
1457 | ISDN_FEATURE_P_UNKNOWN; | |
1da177e4 LT |
1458 | card->ptype = ISDN_PTYPE_UNKNOWN; |
1459 | strlcpy(card->interface.id, id, sizeof(card->interface.id)); | |
1460 | card->msg_buf_write = card->msg_buf; | |
1461 | card->msg_buf_read = card->msg_buf; | |
1462 | card->msg_buf_end = &card->msg_buf[sizeof(card->msg_buf) - 1]; | |
1463 | for (i = 0; i < ISDNLOOP_BCH; i++) { | |
1464 | card->l2_proto[i] = ISDN_PROTO_L2_X75I; | |
1465 | skb_queue_head_init(&card->bqueue[i]); | |
1466 | } | |
1467 | skb_queue_head_init(&card->dqueue); | |
2bd7e20e | 1468 | spin_lock_init(&card->isdnloop_lock); |
1da177e4 LT |
1469 | card->next = cards; |
1470 | cards = card; | |
1471 | if (!register_isdn(&card->interface)) { | |
1472 | cards = cards->next; | |
1473 | printk(KERN_WARNING | |
1474 | "isdnloop: Unable to register %s\n", id); | |
1475 | kfree(card); | |
1476 | return (isdnloop_card *) 0; | |
1477 | } | |
1478 | card->myid = card->interface.channels; | |
1479 | return card; | |
1480 | } | |
1481 | ||
1482 | static int | |
1483 | isdnloop_addcard(char *id1) | |
1484 | { | |
1485 | isdnloop_card *card; | |
50a58b6b BP |
1486 | card = isdnloop_initcard(id1); |
1487 | if (!card) { | |
1da177e4 LT |
1488 | return -EIO; |
1489 | } | |
1490 | printk(KERN_INFO | |
1491 | "isdnloop: (%s) virtual card added\n", | |
1492 | card->interface.id); | |
1493 | return 0; | |
1494 | } | |
1495 | ||
1496 | static int __init | |
1497 | isdnloop_init(void) | |
1498 | { | |
1da177e4 | 1499 | if (isdnloop_id) |
3581ec58 | 1500 | return isdnloop_addcard(isdnloop_id); |
1da177e4 LT |
1501 | |
1502 | return 0; | |
1503 | } | |
1504 | ||
1505 | static void __exit | |
1506 | isdnloop_exit(void) | |
1507 | { | |
1508 | isdn_ctrl cmd; | |
1509 | isdnloop_card *card = cards; | |
1510 | isdnloop_card *last; | |
1511 | int i; | |
1512 | ||
1513 | isdnloop_stopallcards(); | |
1514 | while (card) { | |
1515 | cmd.command = ISDN_STAT_UNLOAD; | |
1516 | cmd.driver = card->myid; | |
1517 | card->interface.statcallb(&cmd); | |
1518 | for (i = 0; i < ISDNLOOP_BCH; i++) | |
1519 | isdnloop_free_queue(card, i); | |
1520 | card = card->next; | |
1521 | } | |
1522 | card = cards; | |
1523 | while (card) { | |
1524 | last = card; | |
1525 | skb_queue_purge(&card->dqueue); | |
1526 | card = card->next; | |
1527 | kfree(last); | |
1528 | } | |
1529 | printk(KERN_NOTICE "isdnloop-ISDN-driver unloaded\n"); | |
1530 | } | |
1531 | ||
1532 | module_init(isdnloop_init); | |
1533 | module_exit(isdnloop_exit); |