Linux 5.1.15
[linux/fpc-iii.git] / drivers / isdn / i4l / isdn_common.c
blob74ee00f5b310922aaf9307fb6ae141c84e17998d
1 /* $Id: isdn_common.c,v 1.1.2.3 2004/02/10 01:07:13 keil Exp $
3 * Linux ISDN subsystem, common used functions (linklevel).
5 * Copyright 1994-1999 by Fritz Elfert (fritz@isdn4linux.de)
6 * Copyright 1995,96 Thinking Objects Software GmbH Wuerzburg
7 * Copyright 1995,96 by Michael Hipp (Michael.Hipp@student.uni-tuebingen.de)
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/poll.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
19 #include <linux/isdn.h>
20 #include <linux/mutex.h>
21 #include "isdn_common.h"
22 #include "isdn_tty.h"
23 #include "isdn_net.h"
24 #include "isdn_ppp.h"
25 #ifdef CONFIG_ISDN_AUDIO
26 #include "isdn_audio.h"
27 #endif
28 #ifdef CONFIG_ISDN_DIVERSION_MODULE
29 #define CONFIG_ISDN_DIVERSION
30 #endif
31 #ifdef CONFIG_ISDN_DIVERSION
32 #include <linux/isdn_divertif.h>
33 #endif /* CONFIG_ISDN_DIVERSION */
34 #include "isdn_v110.h"
36 /* Debugflags */
37 #undef ISDN_DEBUG_STATCALLB
39 MODULE_DESCRIPTION("ISDN4Linux: link layer");
40 MODULE_AUTHOR("Fritz Elfert");
41 MODULE_LICENSE("GPL");
43 isdn_dev *dev;
45 static DEFINE_MUTEX(isdn_mutex);
46 static char *isdn_revision = "$Revision: 1.1.2.3 $";
48 extern char *isdn_net_revision;
49 #ifdef CONFIG_ISDN_PPP
50 extern char *isdn_ppp_revision;
51 #else
52 static char *isdn_ppp_revision = ": none $";
53 #endif
54 #ifdef CONFIG_ISDN_AUDIO
55 extern char *isdn_audio_revision;
56 #else
57 static char *isdn_audio_revision = ": none $";
58 #endif
59 extern char *isdn_v110_revision;
61 #ifdef CONFIG_ISDN_DIVERSION
62 static isdn_divert_if *divert_if; /* = NULL */
63 #endif /* CONFIG_ISDN_DIVERSION */
66 static int isdn_writebuf_stub(int, int, const u_char __user *, int);
67 static void set_global_features(void);
68 static int isdn_wildmat(char *s, char *p);
69 static int isdn_add_channels(isdn_driver_t *d, int drvidx, int n, int adding);
71 static inline void
72 isdn_lock_driver(isdn_driver_t *drv)
74 try_module_get(drv->interface->owner);
75 drv->locks++;
78 void
79 isdn_lock_drivers(void)
81 int i;
83 for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
84 if (!dev->drv[i])
85 continue;
86 isdn_lock_driver(dev->drv[i]);
90 static inline void
91 isdn_unlock_driver(isdn_driver_t *drv)
93 if (drv->locks > 0) {
94 drv->locks--;
95 module_put(drv->interface->owner);
99 void
100 isdn_unlock_drivers(void)
102 int i;
104 for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
105 if (!dev->drv[i])
106 continue;
107 isdn_unlock_driver(dev->drv[i]);
111 #if defined(ISDN_DEBUG_NET_DUMP) || defined(ISDN_DEBUG_MODEM_DUMP)
112 void
113 isdn_dumppkt(char *s, u_char *p, int len, int dumplen)
115 int dumpc;
117 printk(KERN_DEBUG "%s(%d) ", s, len);
118 for (dumpc = 0; (dumpc < dumplen) && (len); len--, dumpc++)
119 printk(" %02x", *p++);
120 printk("\n");
122 #endif
125 * I picked the pattern-matching-functions from an old GNU-tar version (1.10)
126 * It was originally written and put to PD by rs@mirror.TMC.COM (Rich Salz)
128 static int
129 isdn_star(char *s, char *p)
131 while (isdn_wildmat(s, p)) {
132 if (*++s == '\0')
133 return (2);
135 return (0);
139 * Shell-type Pattern-matching for incoming caller-Ids
140 * This function gets a string in s and checks, if it matches the pattern
141 * given in p.
143 * Return:
144 * 0 = match.
145 * 1 = no match.
146 * 2 = no match. Would eventually match, if s would be longer.
148 * Possible Patterns:
150 * '?' matches one character
151 * '*' matches zero or more characters
152 * [xyz] matches the set of characters in brackets.
153 * [^xyz] matches any single character not in the set of characters
156 static int
157 isdn_wildmat(char *s, char *p)
159 register int last;
160 register int matched;
161 register int reverse;
162 register int nostar = 1;
164 if (!(*s) && !(*p))
165 return (1);
166 for (; *p; s++, p++)
167 switch (*p) {
168 case '\\':
169 /* Literal match with following character. */
170 p++;
171 /* fall through */
172 default:
173 if (*s != *p)
174 return (*s == '\0') ? 2 : 1;
175 continue;
176 case '?':
177 /* Match anything. */
178 if (*s == '\0')
179 return (2);
180 continue;
181 case '*':
182 nostar = 0;
183 /* Trailing star matches everything. */
184 return (*++p ? isdn_star(s, p) : 0);
185 case '[':
186 /* [^....] means inverse character class. */
187 if ((reverse = (p[1] == '^')))
188 p++;
189 for (last = 0, matched = 0; *++p && (*p != ']'); last = *p)
190 /* This next line requires a good C compiler. */
191 if (*p == '-' ? *s <= *++p && *s >= last : *s == *p)
192 matched = 1;
193 if (matched == reverse)
194 return (1);
195 continue;
197 return (*s == '\0') ? 0 : nostar;
200 int isdn_msncmp(const char *msn1, const char *msn2)
202 char TmpMsn1[ISDN_MSNLEN];
203 char TmpMsn2[ISDN_MSNLEN];
204 char *p;
206 for (p = TmpMsn1; *msn1 && *msn1 != ':';) // Strip off a SPID
207 *p++ = *msn1++;
208 *p = '\0';
210 for (p = TmpMsn2; *msn2 && *msn2 != ':';) // Strip off a SPID
211 *p++ = *msn2++;
212 *p = '\0';
214 return isdn_wildmat(TmpMsn1, TmpMsn2);
218 isdn_dc2minor(int di, int ch)
220 int i;
221 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
222 if (dev->chanmap[i] == ch && dev->drvmap[i] == di)
223 return i;
224 return -1;
227 static int isdn_timer_cnt1 = 0;
228 static int isdn_timer_cnt2 = 0;
229 static int isdn_timer_cnt3 = 0;
231 static void
232 isdn_timer_funct(struct timer_list *unused)
234 int tf = dev->tflags;
235 if (tf & ISDN_TIMER_FAST) {
236 if (tf & ISDN_TIMER_MODEMREAD)
237 isdn_tty_readmodem();
238 if (tf & ISDN_TIMER_MODEMPLUS)
239 isdn_tty_modem_escape();
240 if (tf & ISDN_TIMER_MODEMXMIT)
241 isdn_tty_modem_xmit();
243 if (tf & ISDN_TIMER_SLOW) {
244 if (++isdn_timer_cnt1 >= ISDN_TIMER_02SEC) {
245 isdn_timer_cnt1 = 0;
246 if (tf & ISDN_TIMER_NETDIAL)
247 isdn_net_dial();
249 if (++isdn_timer_cnt2 >= ISDN_TIMER_1SEC) {
250 isdn_timer_cnt2 = 0;
251 if (tf & ISDN_TIMER_NETHANGUP)
252 isdn_net_autohup();
253 if (++isdn_timer_cnt3 >= ISDN_TIMER_RINGING) {
254 isdn_timer_cnt3 = 0;
255 if (tf & ISDN_TIMER_MODEMRING)
256 isdn_tty_modem_ring();
258 if (tf & ISDN_TIMER_CARRIER)
259 isdn_tty_carrier_timeout();
262 if (tf)
263 mod_timer(&dev->timer, jiffies + ISDN_TIMER_RES);
266 void
267 isdn_timer_ctrl(int tf, int onoff)
269 unsigned long flags;
270 int old_tflags;
272 spin_lock_irqsave(&dev->timerlock, flags);
273 if ((tf & ISDN_TIMER_SLOW) && (!(dev->tflags & ISDN_TIMER_SLOW))) {
274 /* If the slow-timer wasn't activated until now */
275 isdn_timer_cnt1 = 0;
276 isdn_timer_cnt2 = 0;
278 old_tflags = dev->tflags;
279 if (onoff)
280 dev->tflags |= tf;
281 else
282 dev->tflags &= ~tf;
283 if (dev->tflags && !old_tflags)
284 mod_timer(&dev->timer, jiffies + ISDN_TIMER_RES);
285 spin_unlock_irqrestore(&dev->timerlock, flags);
289 * Receive a packet from B-Channel. (Called from low-level-module)
291 static void
292 isdn_receive_skb_callback(int di, int channel, struct sk_buff *skb)
294 int i;
296 if ((i = isdn_dc2minor(di, channel)) == -1) {
297 dev_kfree_skb(skb);
298 return;
300 /* Update statistics */
301 dev->ibytes[i] += skb->len;
303 /* First, try to deliver data to network-device */
304 if (isdn_net_rcv_skb(i, skb))
305 return;
307 /* V.110 handling
308 * makes sense for async streams only, so it is
309 * called after possible net-device delivery.
311 if (dev->v110[i]) {
312 atomic_inc(&dev->v110use[i]);
313 skb = isdn_v110_decode(dev->v110[i], skb);
314 atomic_dec(&dev->v110use[i]);
315 if (!skb)
316 return;
319 /* No network-device found, deliver to tty or raw-channel */
320 if (skb->len) {
321 if (isdn_tty_rcv_skb(i, di, channel, skb))
322 return;
323 wake_up_interruptible(&dev->drv[di]->rcv_waitq[channel]);
324 } else
325 dev_kfree_skb(skb);
329 * Intercept command from Linklevel to Lowlevel.
330 * If layer 2 protocol is V.110 and this is not supported by current
331 * lowlevel-driver, use driver's transparent mode and handle V.110 in
332 * linklevel instead.
335 isdn_command(isdn_ctrl *cmd)
337 if (cmd->driver == -1) {
338 printk(KERN_WARNING "isdn_command command(%x) driver -1\n", cmd->command);
339 return (1);
341 if (!dev->drv[cmd->driver]) {
342 printk(KERN_WARNING "isdn_command command(%x) dev->drv[%d] NULL\n",
343 cmd->command, cmd->driver);
344 return (1);
346 if (!dev->drv[cmd->driver]->interface) {
347 printk(KERN_WARNING "isdn_command command(%x) dev->drv[%d]->interface NULL\n",
348 cmd->command, cmd->driver);
349 return (1);
351 if (cmd->command == ISDN_CMD_SETL2) {
352 int idx = isdn_dc2minor(cmd->driver, cmd->arg & 255);
353 unsigned long l2prot = (cmd->arg >> 8) & 255;
354 unsigned long features = (dev->drv[cmd->driver]->interface->features
355 >> ISDN_FEATURE_L2_SHIFT) &
356 ISDN_FEATURE_L2_MASK;
357 unsigned long l2_feature = (1 << l2prot);
359 switch (l2prot) {
360 case ISDN_PROTO_L2_V11096:
361 case ISDN_PROTO_L2_V11019:
362 case ISDN_PROTO_L2_V11038:
363 /* If V.110 requested, but not supported by
364 * HL-driver, set emulator-flag and change
365 * Layer-2 to transparent
367 if (!(features & l2_feature)) {
368 dev->v110emu[idx] = l2prot;
369 cmd->arg = (cmd->arg & 255) |
370 (ISDN_PROTO_L2_TRANS << 8);
371 } else
372 dev->v110emu[idx] = 0;
375 return dev->drv[cmd->driver]->interface->command(cmd);
378 void
379 isdn_all_eaz(int di, int ch)
381 isdn_ctrl cmd;
383 if (di < 0)
384 return;
385 cmd.driver = di;
386 cmd.arg = ch;
387 cmd.command = ISDN_CMD_SETEAZ;
388 cmd.parm.num[0] = '\0';
389 isdn_command(&cmd);
393 * Begin of a CAPI like LL<->HL interface, currently used only for
394 * supplementary service (CAPI 2.0 part III)
396 #include <linux/isdn/capicmd.h>
398 static int
399 isdn_capi_rec_hl_msg(capi_msg *cm)
401 switch (cm->Command) {
402 case CAPI_FACILITY:
403 /* in the moment only handled in tty */
404 return (isdn_tty_capi_facility(cm));
405 default:
406 return (-1);
410 static int
411 isdn_status_callback(isdn_ctrl *c)
413 int di;
414 u_long flags;
415 int i;
416 int r;
417 int retval = 0;
418 isdn_ctrl cmd;
419 isdn_net_dev *p;
421 di = c->driver;
422 i = isdn_dc2minor(di, c->arg);
423 switch (c->command) {
424 case ISDN_STAT_BSENT:
425 if (i < 0)
426 return -1;
427 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
428 return 0;
429 if (isdn_net_stat_callback(i, c))
430 return 0;
431 if (isdn_v110_stat_callback(i, c))
432 return 0;
433 if (isdn_tty_stat_callback(i, c))
434 return 0;
435 wake_up_interruptible(&dev->drv[di]->snd_waitq[c->arg]);
436 break;
437 case ISDN_STAT_STAVAIL:
438 dev->drv[di]->stavail += c->arg;
439 wake_up_interruptible(&dev->drv[di]->st_waitq);
440 break;
441 case ISDN_STAT_RUN:
442 dev->drv[di]->flags |= DRV_FLAG_RUNNING;
443 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
444 if (dev->drvmap[i] == di)
445 isdn_all_eaz(di, dev->chanmap[i]);
446 set_global_features();
447 break;
448 case ISDN_STAT_STOP:
449 dev->drv[di]->flags &= ~DRV_FLAG_RUNNING;
450 break;
451 case ISDN_STAT_ICALL:
452 if (i < 0)
453 return -1;
454 #ifdef ISDN_DEBUG_STATCALLB
455 printk(KERN_DEBUG "ICALL (net): %d %ld %s\n", di, c->arg, c->parm.num);
456 #endif
457 if (dev->global_flags & ISDN_GLOBAL_STOPPED) {
458 cmd.driver = di;
459 cmd.arg = c->arg;
460 cmd.command = ISDN_CMD_HANGUP;
461 isdn_command(&cmd);
462 return 0;
464 /* Try to find a network-interface which will accept incoming call */
465 r = ((c->command == ISDN_STAT_ICALLW) ? 0 : isdn_net_find_icall(di, c->arg, i, &c->parm.setup));
466 switch (r) {
467 case 0:
468 /* No network-device replies.
469 * Try ttyI's.
470 * These return 0 on no match, 1 on match and
471 * 3 on eventually match, if CID is longer.
473 if (c->command == ISDN_STAT_ICALL)
474 if ((retval = isdn_tty_find_icall(di, c->arg, &c->parm.setup))) return (retval);
475 #ifdef CONFIG_ISDN_DIVERSION
476 if (divert_if)
477 if ((retval = divert_if->stat_callback(c)))
478 return (retval); /* processed */
479 #endif /* CONFIG_ISDN_DIVERSION */
480 if ((!retval) && (dev->drv[di]->flags & DRV_FLAG_REJBUS)) {
481 /* No tty responding */
482 cmd.driver = di;
483 cmd.arg = c->arg;
484 cmd.command = ISDN_CMD_HANGUP;
485 isdn_command(&cmd);
486 retval = 2;
488 break;
489 case 1:
490 /* Schedule connection-setup */
491 isdn_net_dial();
492 cmd.driver = di;
493 cmd.arg = c->arg;
494 cmd.command = ISDN_CMD_ACCEPTD;
495 for (p = dev->netdev; p; p = p->next)
496 if (p->local->isdn_channel == cmd.arg)
498 strcpy(cmd.parm.setup.eazmsn, p->local->msn);
499 isdn_command(&cmd);
500 retval = 1;
501 break;
503 break;
505 case 2: /* For calling back, first reject incoming call ... */
506 case 3: /* Interface found, but down, reject call actively */
507 retval = 2;
508 printk(KERN_INFO "isdn: Rejecting Call\n");
509 cmd.driver = di;
510 cmd.arg = c->arg;
511 cmd.command = ISDN_CMD_HANGUP;
512 isdn_command(&cmd);
513 if (r == 3)
514 break;
515 /* Fall through */
516 case 4:
517 /* ... then start callback. */
518 isdn_net_dial();
519 break;
520 case 5:
521 /* Number would eventually match, if longer */
522 retval = 3;
523 break;
525 #ifdef ISDN_DEBUG_STATCALLB
526 printk(KERN_DEBUG "ICALL: ret=%d\n", retval);
527 #endif
528 return retval;
529 break;
530 case ISDN_STAT_CINF:
531 if (i < 0)
532 return -1;
533 #ifdef ISDN_DEBUG_STATCALLB
534 printk(KERN_DEBUG "CINF: %ld %s\n", c->arg, c->parm.num);
535 #endif
536 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
537 return 0;
538 if (strcmp(c->parm.num, "0"))
539 isdn_net_stat_callback(i, c);
540 isdn_tty_stat_callback(i, c);
541 break;
542 case ISDN_STAT_CAUSE:
543 #ifdef ISDN_DEBUG_STATCALLB
544 printk(KERN_DEBUG "CAUSE: %ld %s\n", c->arg, c->parm.num);
545 #endif
546 printk(KERN_INFO "isdn: %s,ch%ld cause: %s\n",
547 dev->drvid[di], c->arg, c->parm.num);
548 isdn_tty_stat_callback(i, c);
549 #ifdef CONFIG_ISDN_DIVERSION
550 if (divert_if)
551 divert_if->stat_callback(c);
552 #endif /* CONFIG_ISDN_DIVERSION */
553 break;
554 case ISDN_STAT_DISPLAY:
555 #ifdef ISDN_DEBUG_STATCALLB
556 printk(KERN_DEBUG "DISPLAY: %ld %s\n", c->arg, c->parm.display);
557 #endif
558 isdn_tty_stat_callback(i, c);
559 #ifdef CONFIG_ISDN_DIVERSION
560 if (divert_if)
561 divert_if->stat_callback(c);
562 #endif /* CONFIG_ISDN_DIVERSION */
563 break;
564 case ISDN_STAT_DCONN:
565 if (i < 0)
566 return -1;
567 #ifdef ISDN_DEBUG_STATCALLB
568 printk(KERN_DEBUG "DCONN: %ld\n", c->arg);
569 #endif
570 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
571 return 0;
572 /* Find any net-device, waiting for D-channel setup */
573 if (isdn_net_stat_callback(i, c))
574 break;
575 isdn_v110_stat_callback(i, c);
576 /* Find any ttyI, waiting for D-channel setup */
577 if (isdn_tty_stat_callback(i, c)) {
578 cmd.driver = di;
579 cmd.arg = c->arg;
580 cmd.command = ISDN_CMD_ACCEPTB;
581 isdn_command(&cmd);
582 break;
584 break;
585 case ISDN_STAT_DHUP:
586 if (i < 0)
587 return -1;
588 #ifdef ISDN_DEBUG_STATCALLB
589 printk(KERN_DEBUG "DHUP: %ld\n", c->arg);
590 #endif
591 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
592 return 0;
593 dev->drv[di]->online &= ~(1 << (c->arg));
594 isdn_info_update();
595 /* Signal hangup to network-devices */
596 if (isdn_net_stat_callback(i, c))
597 break;
598 isdn_v110_stat_callback(i, c);
599 if (isdn_tty_stat_callback(i, c))
600 break;
601 #ifdef CONFIG_ISDN_DIVERSION
602 if (divert_if)
603 divert_if->stat_callback(c);
604 #endif /* CONFIG_ISDN_DIVERSION */
605 break;
606 break;
607 case ISDN_STAT_BCONN:
608 if (i < 0)
609 return -1;
610 #ifdef ISDN_DEBUG_STATCALLB
611 printk(KERN_DEBUG "BCONN: %ld\n", c->arg);
612 #endif
613 /* Signal B-channel-connect to network-devices */
614 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
615 return 0;
616 dev->drv[di]->online |= (1 << (c->arg));
617 isdn_info_update();
618 if (isdn_net_stat_callback(i, c))
619 break;
620 isdn_v110_stat_callback(i, c);
621 if (isdn_tty_stat_callback(i, c))
622 break;
623 break;
624 case ISDN_STAT_BHUP:
625 if (i < 0)
626 return -1;
627 #ifdef ISDN_DEBUG_STATCALLB
628 printk(KERN_DEBUG "BHUP: %ld\n", c->arg);
629 #endif
630 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
631 return 0;
632 dev->drv[di]->online &= ~(1 << (c->arg));
633 isdn_info_update();
634 #ifdef CONFIG_ISDN_X25
635 /* Signal hangup to network-devices */
636 if (isdn_net_stat_callback(i, c))
637 break;
638 #endif
639 isdn_v110_stat_callback(i, c);
640 if (isdn_tty_stat_callback(i, c))
641 break;
642 break;
643 case ISDN_STAT_NODCH:
644 if (i < 0)
645 return -1;
646 #ifdef ISDN_DEBUG_STATCALLB
647 printk(KERN_DEBUG "NODCH: %ld\n", c->arg);
648 #endif
649 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
650 return 0;
651 if (isdn_net_stat_callback(i, c))
652 break;
653 if (isdn_tty_stat_callback(i, c))
654 break;
655 break;
656 case ISDN_STAT_ADDCH:
657 spin_lock_irqsave(&dev->lock, flags);
658 if (isdn_add_channels(dev->drv[di], di, c->arg, 1)) {
659 spin_unlock_irqrestore(&dev->lock, flags);
660 return -1;
662 spin_unlock_irqrestore(&dev->lock, flags);
663 isdn_info_update();
664 break;
665 case ISDN_STAT_DISCH:
666 spin_lock_irqsave(&dev->lock, flags);
667 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
668 if ((dev->drvmap[i] == di) &&
669 (dev->chanmap[i] == c->arg)) {
670 if (c->parm.num[0])
671 dev->usage[i] &= ~ISDN_USAGE_DISABLED;
672 else
673 if (USG_NONE(dev->usage[i])) {
674 dev->usage[i] |= ISDN_USAGE_DISABLED;
676 else
677 retval = -1;
678 break;
680 spin_unlock_irqrestore(&dev->lock, flags);
681 isdn_info_update();
682 break;
683 case ISDN_STAT_UNLOAD:
684 while (dev->drv[di]->locks > 0) {
685 isdn_unlock_driver(dev->drv[di]);
687 spin_lock_irqsave(&dev->lock, flags);
688 isdn_tty_stat_callback(i, c);
689 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
690 if (dev->drvmap[i] == di) {
691 dev->drvmap[i] = -1;
692 dev->chanmap[i] = -1;
693 dev->usage[i] &= ~ISDN_USAGE_DISABLED;
695 dev->drivers--;
696 dev->channels -= dev->drv[di]->channels;
697 kfree(dev->drv[di]->rcverr);
698 kfree(dev->drv[di]->rcvcount);
699 for (i = 0; i < dev->drv[di]->channels; i++)
700 skb_queue_purge(&dev->drv[di]->rpqueue[i]);
701 kfree(dev->drv[di]->rpqueue);
702 kfree(dev->drv[di]->rcv_waitq);
703 kfree(dev->drv[di]);
704 dev->drv[di] = NULL;
705 dev->drvid[di][0] = '\0';
706 isdn_info_update();
707 set_global_features();
708 spin_unlock_irqrestore(&dev->lock, flags);
709 return 0;
710 case ISDN_STAT_L1ERR:
711 break;
712 case CAPI_PUT_MESSAGE:
713 return (isdn_capi_rec_hl_msg(&c->parm.cmsg));
714 #ifdef CONFIG_ISDN_TTY_FAX
715 case ISDN_STAT_FAXIND:
716 isdn_tty_stat_callback(i, c);
717 break;
718 #endif
719 #ifdef CONFIG_ISDN_AUDIO
720 case ISDN_STAT_AUDIO:
721 isdn_tty_stat_callback(i, c);
722 break;
723 #endif
724 #ifdef CONFIG_ISDN_DIVERSION
725 case ISDN_STAT_PROT:
726 case ISDN_STAT_REDIR:
727 if (divert_if)
728 return (divert_if->stat_callback(c));
729 #endif /* CONFIG_ISDN_DIVERSION */
730 /* fall through */
731 default:
732 return -1;
734 return 0;
738 * Get integer from char-pointer, set pointer to end of number
741 isdn_getnum(char **p)
743 int v = -1;
745 while (*p[0] >= '0' && *p[0] <= '9')
746 v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p[0]++) - '0');
747 return v;
750 #define DLE 0x10
753 * isdn_readbchan() tries to get data from the read-queue.
754 * It MUST be called with interrupts off.
756 * Be aware that this is not an atomic operation when sleep != 0, even though
757 * interrupts are turned off! Well, like that we are currently only called
758 * on behalf of a read system call on raw device files (which are documented
759 * to be dangerous and for debugging purpose only). The inode semaphore
760 * takes care that this is not called for the same minor device number while
761 * we are sleeping, but access is not serialized against simultaneous read()
762 * from the corresponding ttyI device. Can other ugly events, like changes
763 * of the mapping (di,ch)<->minor, happen during the sleep? --he
766 isdn_readbchan(int di, int channel, u_char *buf, u_char *fp, int len, wait_queue_head_t *sleep)
768 int count;
769 int count_pull;
770 int count_put;
771 int dflag;
772 struct sk_buff *skb;
773 u_char *cp;
775 if (!dev->drv[di])
776 return 0;
777 if (skb_queue_empty(&dev->drv[di]->rpqueue[channel])) {
778 if (sleep)
779 wait_event_interruptible(*sleep,
780 !skb_queue_empty(&dev->drv[di]->rpqueue[channel]));
781 else
782 return 0;
784 if (len > dev->drv[di]->rcvcount[channel])
785 len = dev->drv[di]->rcvcount[channel];
786 cp = buf;
787 count = 0;
788 while (len) {
789 if (!(skb = skb_peek(&dev->drv[di]->rpqueue[channel])))
790 break;
791 #ifdef CONFIG_ISDN_AUDIO
792 if (ISDN_AUDIO_SKB_LOCK(skb))
793 break;
794 ISDN_AUDIO_SKB_LOCK(skb) = 1;
795 if ((ISDN_AUDIO_SKB_DLECOUNT(skb)) || (dev->drv[di]->DLEflag & (1 << channel))) {
796 char *p = skb->data;
797 unsigned long DLEmask = (1 << channel);
799 dflag = 0;
800 count_pull = count_put = 0;
801 while ((count_pull < skb->len) && (len > 0)) {
802 len--;
803 if (dev->drv[di]->DLEflag & DLEmask) {
804 *cp++ = DLE;
805 dev->drv[di]->DLEflag &= ~DLEmask;
806 } else {
807 *cp++ = *p;
808 if (*p == DLE) {
809 dev->drv[di]->DLEflag |= DLEmask;
810 (ISDN_AUDIO_SKB_DLECOUNT(skb))--;
812 p++;
813 count_pull++;
815 count_put++;
817 if (count_pull >= skb->len)
818 dflag = 1;
819 } else {
820 #endif
821 /* No DLE's in buff, so simply copy it */
822 dflag = 1;
823 if ((count_pull = skb->len) > len) {
824 count_pull = len;
825 dflag = 0;
827 count_put = count_pull;
828 skb_copy_from_linear_data(skb, cp, count_put);
829 cp += count_put;
830 len -= count_put;
831 #ifdef CONFIG_ISDN_AUDIO
833 #endif
834 count += count_put;
835 if (fp) {
836 memset(fp, 0, count_put);
837 fp += count_put;
839 if (dflag) {
840 /* We got all the data in this buff.
841 * Now we can dequeue it.
843 if (fp)
844 *(fp - 1) = 0xff;
845 #ifdef CONFIG_ISDN_AUDIO
846 ISDN_AUDIO_SKB_LOCK(skb) = 0;
847 #endif
848 skb = skb_dequeue(&dev->drv[di]->rpqueue[channel]);
849 dev_kfree_skb(skb);
850 } else {
851 /* Not yet emptied this buff, so it
852 * must stay in the queue, for further calls
853 * but we pull off the data we got until now.
855 skb_pull(skb, count_pull);
856 #ifdef CONFIG_ISDN_AUDIO
857 ISDN_AUDIO_SKB_LOCK(skb) = 0;
858 #endif
860 dev->drv[di]->rcvcount[channel] -= count_put;
862 return count;
866 * isdn_readbchan_tty() tries to get data from the read-queue.
867 * It MUST be called with interrupts off.
869 * Be aware that this is not an atomic operation when sleep != 0, even though
870 * interrupts are turned off! Well, like that we are currently only called
871 * on behalf of a read system call on raw device files (which are documented
872 * to be dangerous and for debugging purpose only). The inode semaphore
873 * takes care that this is not called for the same minor device number while
874 * we are sleeping, but access is not serialized against simultaneous read()
875 * from the corresponding ttyI device. Can other ugly events, like changes
876 * of the mapping (di,ch)<->minor, happen during the sleep? --he
879 isdn_readbchan_tty(int di, int channel, struct tty_port *port, int cisco_hack)
881 int count;
882 int count_pull;
883 int count_put;
884 int dflag;
885 struct sk_buff *skb;
886 char last = 0;
887 int len;
889 if (!dev->drv[di])
890 return 0;
891 if (skb_queue_empty(&dev->drv[di]->rpqueue[channel]))
892 return 0;
894 len = tty_buffer_request_room(port, dev->drv[di]->rcvcount[channel]);
895 if (len == 0)
896 return len;
898 count = 0;
899 while (len) {
900 if (!(skb = skb_peek(&dev->drv[di]->rpqueue[channel])))
901 break;
902 #ifdef CONFIG_ISDN_AUDIO
903 if (ISDN_AUDIO_SKB_LOCK(skb))
904 break;
905 ISDN_AUDIO_SKB_LOCK(skb) = 1;
906 if ((ISDN_AUDIO_SKB_DLECOUNT(skb)) || (dev->drv[di]->DLEflag & (1 << channel))) {
907 char *p = skb->data;
908 unsigned long DLEmask = (1 << channel);
910 dflag = 0;
911 count_pull = count_put = 0;
912 while ((count_pull < skb->len) && (len > 0)) {
913 /* push every character but the last to the tty buffer directly */
914 if (count_put)
915 tty_insert_flip_char(port, last, TTY_NORMAL);
916 len--;
917 if (dev->drv[di]->DLEflag & DLEmask) {
918 last = DLE;
919 dev->drv[di]->DLEflag &= ~DLEmask;
920 } else {
921 last = *p;
922 if (last == DLE) {
923 dev->drv[di]->DLEflag |= DLEmask;
924 (ISDN_AUDIO_SKB_DLECOUNT(skb))--;
926 p++;
927 count_pull++;
929 count_put++;
931 if (count_pull >= skb->len)
932 dflag = 1;
933 } else {
934 #endif
935 /* No DLE's in buff, so simply copy it */
936 dflag = 1;
937 if ((count_pull = skb->len) > len) {
938 count_pull = len;
939 dflag = 0;
941 count_put = count_pull;
942 if (count_put > 1)
943 tty_insert_flip_string(port, skb->data, count_put - 1);
944 last = skb->data[count_put - 1];
945 len -= count_put;
946 #ifdef CONFIG_ISDN_AUDIO
948 #endif
949 count += count_put;
950 if (dflag) {
951 /* We got all the data in this buff.
952 * Now we can dequeue it.
954 if (cisco_hack)
955 tty_insert_flip_char(port, last, 0xFF);
956 else
957 tty_insert_flip_char(port, last, TTY_NORMAL);
958 #ifdef CONFIG_ISDN_AUDIO
959 ISDN_AUDIO_SKB_LOCK(skb) = 0;
960 #endif
961 skb = skb_dequeue(&dev->drv[di]->rpqueue[channel]);
962 dev_kfree_skb(skb);
963 } else {
964 tty_insert_flip_char(port, last, TTY_NORMAL);
965 /* Not yet emptied this buff, so it
966 * must stay in the queue, for further calls
967 * but we pull off the data we got until now.
969 skb_pull(skb, count_pull);
970 #ifdef CONFIG_ISDN_AUDIO
971 ISDN_AUDIO_SKB_LOCK(skb) = 0;
972 #endif
974 dev->drv[di]->rcvcount[channel] -= count_put;
976 return count;
980 static inline int
981 isdn_minor2drv(int minor)
983 return (dev->drvmap[minor]);
986 static inline int
987 isdn_minor2chan(int minor)
989 return (dev->chanmap[minor]);
992 static char *
993 isdn_statstr(void)
995 static char istatbuf[2048];
996 char *p;
997 int i;
999 sprintf(istatbuf, "idmap:\t");
1000 p = istatbuf + strlen(istatbuf);
1001 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1002 sprintf(p, "%s ", (dev->drvmap[i] < 0) ? "-" : dev->drvid[dev->drvmap[i]]);
1003 p = istatbuf + strlen(istatbuf);
1005 sprintf(p, "\nchmap:\t");
1006 p = istatbuf + strlen(istatbuf);
1007 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1008 sprintf(p, "%d ", dev->chanmap[i]);
1009 p = istatbuf + strlen(istatbuf);
1011 sprintf(p, "\ndrmap:\t");
1012 p = istatbuf + strlen(istatbuf);
1013 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1014 sprintf(p, "%d ", dev->drvmap[i]);
1015 p = istatbuf + strlen(istatbuf);
1017 sprintf(p, "\nusage:\t");
1018 p = istatbuf + strlen(istatbuf);
1019 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1020 sprintf(p, "%d ", dev->usage[i]);
1021 p = istatbuf + strlen(istatbuf);
1023 sprintf(p, "\nflags:\t");
1024 p = istatbuf + strlen(istatbuf);
1025 for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
1026 if (dev->drv[i]) {
1027 sprintf(p, "%ld ", dev->drv[i]->online);
1028 p = istatbuf + strlen(istatbuf);
1029 } else {
1030 sprintf(p, "? ");
1031 p = istatbuf + strlen(istatbuf);
1034 sprintf(p, "\nphone:\t");
1035 p = istatbuf + strlen(istatbuf);
1036 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1037 sprintf(p, "%s ", dev->num[i]);
1038 p = istatbuf + strlen(istatbuf);
1040 sprintf(p, "\n");
1041 return istatbuf;
1044 /* Module interface-code */
1046 void
1047 isdn_info_update(void)
1049 infostruct *p = dev->infochain;
1051 while (p) {
1052 *(p->private) = 1;
1053 p = (infostruct *) p->next;
1055 wake_up_interruptible(&(dev->info_waitq));
1058 static ssize_t
1059 isdn_read(struct file *file, char __user *buf, size_t count, loff_t *off)
1061 uint minor = iminor(file_inode(file));
1062 int len = 0;
1063 int drvidx;
1064 int chidx;
1065 int retval;
1066 char *p;
1068 mutex_lock(&isdn_mutex);
1069 if (minor == ISDN_MINOR_STATUS) {
1070 if (!file->private_data) {
1071 if (file->f_flags & O_NONBLOCK) {
1072 retval = -EAGAIN;
1073 goto out;
1075 wait_event_interruptible(dev->info_waitq,
1076 file->private_data);
1078 p = isdn_statstr();
1079 file->private_data = NULL;
1080 if ((len = strlen(p)) <= count) {
1081 if (copy_to_user(buf, p, len)) {
1082 retval = -EFAULT;
1083 goto out;
1085 *off += len;
1086 retval = len;
1087 goto out;
1089 retval = 0;
1090 goto out;
1092 if (!dev->drivers) {
1093 retval = -ENODEV;
1094 goto out;
1096 if (minor <= ISDN_MINOR_BMAX) {
1097 printk(KERN_WARNING "isdn_read minor %d obsolete!\n", minor);
1098 drvidx = isdn_minor2drv(minor);
1099 if (drvidx < 0) {
1100 retval = -ENODEV;
1101 goto out;
1103 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
1104 retval = -ENODEV;
1105 goto out;
1107 chidx = isdn_minor2chan(minor);
1108 if (!(p = kmalloc(count, GFP_KERNEL))) {
1109 retval = -ENOMEM;
1110 goto out;
1112 len = isdn_readbchan(drvidx, chidx, p, NULL, count,
1113 &dev->drv[drvidx]->rcv_waitq[chidx]);
1114 *off += len;
1115 if (copy_to_user(buf, p, len))
1116 len = -EFAULT;
1117 kfree(p);
1118 retval = len;
1119 goto out;
1121 if (minor <= ISDN_MINOR_CTRLMAX) {
1122 drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1123 if (drvidx < 0) {
1124 retval = -ENODEV;
1125 goto out;
1127 if (!dev->drv[drvidx]->stavail) {
1128 if (file->f_flags & O_NONBLOCK) {
1129 retval = -EAGAIN;
1130 goto out;
1132 wait_event_interruptible(dev->drv[drvidx]->st_waitq,
1133 dev->drv[drvidx]->stavail);
1135 if (dev->drv[drvidx]->interface->readstat) {
1136 if (count > dev->drv[drvidx]->stavail)
1137 count = dev->drv[drvidx]->stavail;
1138 len = dev->drv[drvidx]->interface->readstat(buf, count,
1139 drvidx, isdn_minor2chan(minor - ISDN_MINOR_CTRL));
1140 if (len < 0) {
1141 retval = len;
1142 goto out;
1144 } else {
1145 len = 0;
1147 if (len)
1148 dev->drv[drvidx]->stavail -= len;
1149 else
1150 dev->drv[drvidx]->stavail = 0;
1151 *off += len;
1152 retval = len;
1153 goto out;
1155 #ifdef CONFIG_ISDN_PPP
1156 if (minor <= ISDN_MINOR_PPPMAX) {
1157 retval = isdn_ppp_read(minor - ISDN_MINOR_PPP, file, buf, count);
1158 goto out;
1160 #endif
1161 retval = -ENODEV;
1162 out:
1163 mutex_unlock(&isdn_mutex);
1164 return retval;
1167 static ssize_t
1168 isdn_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
1170 uint minor = iminor(file_inode(file));
1171 int drvidx;
1172 int chidx;
1173 int retval;
1175 if (minor == ISDN_MINOR_STATUS)
1176 return -EPERM;
1177 if (!dev->drivers)
1178 return -ENODEV;
1180 mutex_lock(&isdn_mutex);
1181 if (minor <= ISDN_MINOR_BMAX) {
1182 printk(KERN_WARNING "isdn_write minor %d obsolete!\n", minor);
1183 drvidx = isdn_minor2drv(minor);
1184 if (drvidx < 0) {
1185 retval = -ENODEV;
1186 goto out;
1188 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
1189 retval = -ENODEV;
1190 goto out;
1192 chidx = isdn_minor2chan(minor);
1193 wait_event_interruptible(dev->drv[drvidx]->snd_waitq[chidx],
1194 (retval = isdn_writebuf_stub(drvidx, chidx, buf, count)));
1195 goto out;
1197 if (minor <= ISDN_MINOR_CTRLMAX) {
1198 drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1199 if (drvidx < 0) {
1200 retval = -ENODEV;
1201 goto out;
1204 * We want to use the isdnctrl device to load the firmware
1206 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1207 return -ENODEV;
1209 if (dev->drv[drvidx]->interface->writecmd)
1210 retval = dev->drv[drvidx]->interface->
1211 writecmd(buf, count, drvidx,
1212 isdn_minor2chan(minor - ISDN_MINOR_CTRL));
1213 else
1214 retval = count;
1215 goto out;
1217 #ifdef CONFIG_ISDN_PPP
1218 if (minor <= ISDN_MINOR_PPPMAX) {
1219 retval = isdn_ppp_write(minor - ISDN_MINOR_PPP, file, buf, count);
1220 goto out;
1222 #endif
1223 retval = -ENODEV;
1224 out:
1225 mutex_unlock(&isdn_mutex);
1226 return retval;
1229 static __poll_t
1230 isdn_poll(struct file *file, poll_table *wait)
1232 __poll_t mask = 0;
1233 unsigned int minor = iminor(file_inode(file));
1234 int drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1236 mutex_lock(&isdn_mutex);
1237 if (minor == ISDN_MINOR_STATUS) {
1238 poll_wait(file, &(dev->info_waitq), wait);
1239 /* mask = EPOLLOUT | EPOLLWRNORM; */
1240 if (file->private_data) {
1241 mask |= EPOLLIN | EPOLLRDNORM;
1243 goto out;
1245 if (minor >= ISDN_MINOR_CTRL && minor <= ISDN_MINOR_CTRLMAX) {
1246 if (drvidx < 0) {
1247 /* driver deregistered while file open */
1248 mask = EPOLLHUP;
1249 goto out;
1251 poll_wait(file, &(dev->drv[drvidx]->st_waitq), wait);
1252 mask = EPOLLOUT | EPOLLWRNORM;
1253 if (dev->drv[drvidx]->stavail) {
1254 mask |= EPOLLIN | EPOLLRDNORM;
1256 goto out;
1258 #ifdef CONFIG_ISDN_PPP
1259 if (minor <= ISDN_MINOR_PPPMAX) {
1260 mask = isdn_ppp_poll(file, wait);
1261 goto out;
1263 #endif
1264 mask = EPOLLERR;
1265 out:
1266 mutex_unlock(&isdn_mutex);
1267 return mask;
1271 static int
1272 isdn_ioctl(struct file *file, uint cmd, ulong arg)
1274 uint minor = iminor(file_inode(file));
1275 isdn_ctrl c;
1276 int drvidx;
1277 int ret;
1278 int i;
1279 char __user *p;
1280 char *s;
1281 union iocpar {
1282 char name[10];
1283 char bname[22];
1284 isdn_ioctl_struct iocts;
1285 isdn_net_ioctl_phone phone;
1286 isdn_net_ioctl_cfg cfg;
1287 } iocpar;
1288 void __user *argp = (void __user *)arg;
1290 #define name iocpar.name
1291 #define bname iocpar.bname
1292 #define iocts iocpar.iocts
1293 #define phone iocpar.phone
1294 #define cfg iocpar.cfg
1296 if (minor == ISDN_MINOR_STATUS) {
1297 switch (cmd) {
1298 case IIOCGETDVR:
1299 return (TTY_DV +
1300 (NET_DV << 8) +
1301 (INF_DV << 16));
1302 case IIOCGETCPS:
1303 if (arg) {
1304 ulong __user *p = argp;
1305 int i;
1306 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1307 put_user(dev->ibytes[i], p++);
1308 put_user(dev->obytes[i], p++);
1310 return 0;
1311 } else
1312 return -EINVAL;
1313 break;
1314 case IIOCNETGPN:
1315 /* Get peer phone number of a connected
1316 * isdn network interface */
1317 if (arg) {
1318 if (copy_from_user(&phone, argp, sizeof(phone)))
1319 return -EFAULT;
1320 return isdn_net_getpeer(&phone, argp);
1321 } else
1322 return -EINVAL;
1323 default:
1324 return -EINVAL;
1327 if (!dev->drivers)
1328 return -ENODEV;
1329 if (minor <= ISDN_MINOR_BMAX) {
1330 drvidx = isdn_minor2drv(minor);
1331 if (drvidx < 0)
1332 return -ENODEV;
1333 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1334 return -ENODEV;
1335 return 0;
1337 if (minor <= ISDN_MINOR_CTRLMAX) {
1339 * isdn net devices manage lots of configuration variables as linked lists.
1340 * Those lists must only be manipulated from user space. Some of the ioctl's
1341 * service routines access user space and are not atomic. Therefore, ioctl's
1342 * manipulating the lists and ioctl's sleeping while accessing the lists
1343 * are serialized by means of a semaphore.
1345 switch (cmd) {
1346 case IIOCNETDWRSET:
1347 printk(KERN_INFO "INFO: ISDN_DW_ABC_EXTENSION not enabled\n");
1348 return (-EINVAL);
1349 case IIOCNETLCR:
1350 printk(KERN_INFO "INFO: ISDN_ABC_LCR_SUPPORT not enabled\n");
1351 return -ENODEV;
1352 case IIOCNETAIF:
1353 /* Add a network-interface */
1354 if (arg) {
1355 if (copy_from_user(name, argp, sizeof(name)))
1356 return -EFAULT;
1357 s = name;
1358 } else {
1359 s = NULL;
1361 ret = mutex_lock_interruptible(&dev->mtx);
1362 if (ret) return ret;
1363 if ((s = isdn_net_new(s, NULL))) {
1364 if (copy_to_user(argp, s, strlen(s) + 1)) {
1365 ret = -EFAULT;
1366 } else {
1367 ret = 0;
1369 } else
1370 ret = -ENODEV;
1371 mutex_unlock(&dev->mtx);
1372 return ret;
1373 case IIOCNETASL:
1374 /* Add a slave to a network-interface */
1375 if (arg) {
1376 if (copy_from_user(bname, argp, sizeof(bname) - 1))
1377 return -EFAULT;
1378 bname[sizeof(bname)-1] = 0;
1379 } else
1380 return -EINVAL;
1381 ret = mutex_lock_interruptible(&dev->mtx);
1382 if (ret) return ret;
1383 if ((s = isdn_net_newslave(bname))) {
1384 if (copy_to_user(argp, s, strlen(s) + 1)) {
1385 ret = -EFAULT;
1386 } else {
1387 ret = 0;
1389 } else
1390 ret = -ENODEV;
1391 mutex_unlock(&dev->mtx);
1392 return ret;
1393 case IIOCNETDIF:
1394 /* Delete a network-interface */
1395 if (arg) {
1396 if (copy_from_user(name, argp, sizeof(name)))
1397 return -EFAULT;
1398 ret = mutex_lock_interruptible(&dev->mtx);
1399 if (ret) return ret;
1400 ret = isdn_net_rm(name);
1401 mutex_unlock(&dev->mtx);
1402 return ret;
1403 } else
1404 return -EINVAL;
1405 case IIOCNETSCF:
1406 /* Set configurable parameters of a network-interface */
1407 if (arg) {
1408 if (copy_from_user(&cfg, argp, sizeof(cfg)))
1409 return -EFAULT;
1410 return isdn_net_setcfg(&cfg);
1411 } else
1412 return -EINVAL;
1413 case IIOCNETGCF:
1414 /* Get configurable parameters of a network-interface */
1415 if (arg) {
1416 if (copy_from_user(&cfg, argp, sizeof(cfg)))
1417 return -EFAULT;
1418 if (!(ret = isdn_net_getcfg(&cfg))) {
1419 if (copy_to_user(argp, &cfg, sizeof(cfg)))
1420 return -EFAULT;
1422 return ret;
1423 } else
1424 return -EINVAL;
1425 case IIOCNETANM:
1426 /* Add a phone-number to a network-interface */
1427 if (arg) {
1428 if (copy_from_user(&phone, argp, sizeof(phone)))
1429 return -EFAULT;
1430 ret = mutex_lock_interruptible(&dev->mtx);
1431 if (ret) return ret;
1432 ret = isdn_net_addphone(&phone);
1433 mutex_unlock(&dev->mtx);
1434 return ret;
1435 } else
1436 return -EINVAL;
1437 case IIOCNETGNM:
1438 /* Get list of phone-numbers of a network-interface */
1439 if (arg) {
1440 if (copy_from_user(&phone, argp, sizeof(phone)))
1441 return -EFAULT;
1442 ret = mutex_lock_interruptible(&dev->mtx);
1443 if (ret) return ret;
1444 ret = isdn_net_getphones(&phone, argp);
1445 mutex_unlock(&dev->mtx);
1446 return ret;
1447 } else
1448 return -EINVAL;
1449 case IIOCNETDNM:
1450 /* Delete a phone-number of a network-interface */
1451 if (arg) {
1452 if (copy_from_user(&phone, argp, sizeof(phone)))
1453 return -EFAULT;
1454 ret = mutex_lock_interruptible(&dev->mtx);
1455 if (ret) return ret;
1456 ret = isdn_net_delphone(&phone);
1457 mutex_unlock(&dev->mtx);
1458 return ret;
1459 } else
1460 return -EINVAL;
1461 case IIOCNETDIL:
1462 /* Force dialing of a network-interface */
1463 if (arg) {
1464 if (copy_from_user(name, argp, sizeof(name)))
1465 return -EFAULT;
1466 return isdn_net_force_dial(name);
1467 } else
1468 return -EINVAL;
1469 #ifdef CONFIG_ISDN_PPP
1470 case IIOCNETALN:
1471 if (!arg)
1472 return -EINVAL;
1473 if (copy_from_user(name, argp, sizeof(name)))
1474 return -EFAULT;
1475 return isdn_ppp_dial_slave(name);
1476 case IIOCNETDLN:
1477 if (!arg)
1478 return -EINVAL;
1479 if (copy_from_user(name, argp, sizeof(name)))
1480 return -EFAULT;
1481 return isdn_ppp_hangup_slave(name);
1482 #endif
1483 case IIOCNETHUP:
1484 /* Force hangup of a network-interface */
1485 if (!arg)
1486 return -EINVAL;
1487 if (copy_from_user(name, argp, sizeof(name)))
1488 return -EFAULT;
1489 return isdn_net_force_hangup(name);
1490 break;
1491 case IIOCSETVER:
1492 dev->net_verbose = arg;
1493 printk(KERN_INFO "isdn: Verbose-Level is %d\n", dev->net_verbose);
1494 return 0;
1495 case IIOCSETGST:
1496 if (arg)
1497 dev->global_flags |= ISDN_GLOBAL_STOPPED;
1498 else
1499 dev->global_flags &= ~ISDN_GLOBAL_STOPPED;
1500 printk(KERN_INFO "isdn: Global Mode %s\n",
1501 (dev->global_flags & ISDN_GLOBAL_STOPPED) ? "stopped" : "running");
1502 return 0;
1503 case IIOCSETBRJ:
1504 drvidx = -1;
1505 if (arg) {
1506 int i;
1507 char *p;
1508 if (copy_from_user(&iocts, argp,
1509 sizeof(isdn_ioctl_struct)))
1510 return -EFAULT;
1511 iocts.drvid[sizeof(iocts.drvid) - 1] = 0;
1512 if (strlen(iocts.drvid)) {
1513 if ((p = strchr(iocts.drvid, ',')))
1514 *p = 0;
1515 drvidx = -1;
1516 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1517 if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1518 drvidx = i;
1519 break;
1523 if (drvidx == -1)
1524 return -ENODEV;
1525 if (iocts.arg)
1526 dev->drv[drvidx]->flags |= DRV_FLAG_REJBUS;
1527 else
1528 dev->drv[drvidx]->flags &= ~DRV_FLAG_REJBUS;
1529 return 0;
1530 case IIOCSIGPRF:
1531 dev->profd = current;
1532 return 0;
1533 break;
1534 case IIOCGETPRF:
1535 /* Get all Modem-Profiles */
1536 if (arg) {
1537 char __user *p = argp;
1538 int i;
1540 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1541 if (copy_to_user(p, dev->mdm.info[i].emu.profile,
1542 ISDN_MODEM_NUMREG))
1543 return -EFAULT;
1544 p += ISDN_MODEM_NUMREG;
1545 if (copy_to_user(p, dev->mdm.info[i].emu.pmsn, ISDN_MSNLEN))
1546 return -EFAULT;
1547 p += ISDN_MSNLEN;
1548 if (copy_to_user(p, dev->mdm.info[i].emu.plmsn, ISDN_LMSNLEN))
1549 return -EFAULT;
1550 p += ISDN_LMSNLEN;
1552 return (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN) * ISDN_MAX_CHANNELS;
1553 } else
1554 return -EINVAL;
1555 break;
1556 case IIOCSETPRF:
1557 /* Set all Modem-Profiles */
1558 if (arg) {
1559 char __user *p = argp;
1560 int i;
1562 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1563 if (copy_from_user(dev->mdm.info[i].emu.profile, p,
1564 ISDN_MODEM_NUMREG))
1565 return -EFAULT;
1566 p += ISDN_MODEM_NUMREG;
1567 if (copy_from_user(dev->mdm.info[i].emu.plmsn, p, ISDN_LMSNLEN))
1568 return -EFAULT;
1569 p += ISDN_LMSNLEN;
1570 if (copy_from_user(dev->mdm.info[i].emu.pmsn, p, ISDN_MSNLEN))
1571 return -EFAULT;
1572 p += ISDN_MSNLEN;
1574 return 0;
1575 } else
1576 return -EINVAL;
1577 break;
1578 case IIOCSETMAP:
1579 case IIOCGETMAP:
1580 /* Set/Get MSN->EAZ-Mapping for a driver */
1581 if (arg) {
1583 if (copy_from_user(&iocts, argp,
1584 sizeof(isdn_ioctl_struct)))
1585 return -EFAULT;
1586 iocts.drvid[sizeof(iocts.drvid) - 1] = 0;
1587 if (strlen(iocts.drvid)) {
1588 drvidx = -1;
1589 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1590 if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1591 drvidx = i;
1592 break;
1594 } else
1595 drvidx = 0;
1596 if (drvidx == -1)
1597 return -ENODEV;
1598 if (cmd == IIOCSETMAP) {
1599 int loop = 1;
1601 p = (char __user *) iocts.arg;
1602 i = 0;
1603 while (loop) {
1604 int j = 0;
1606 while (1) {
1607 get_user(bname[j], p++);
1608 switch (bname[j]) {
1609 case '\0':
1610 loop = 0;
1611 /* Fall through */
1612 case ',':
1613 bname[j] = '\0';
1614 strcpy(dev->drv[drvidx]->msn2eaz[i], bname);
1615 j = ISDN_MSNLEN;
1616 break;
1617 default:
1618 j++;
1620 if (j >= ISDN_MSNLEN)
1621 break;
1623 if (++i > 9)
1624 break;
1626 } else {
1627 p = (char __user *) iocts.arg;
1628 for (i = 0; i < 10; i++) {
1629 snprintf(bname, sizeof(bname), "%s%s",
1630 strlen(dev->drv[drvidx]->msn2eaz[i]) ?
1631 dev->drv[drvidx]->msn2eaz[i] : "_",
1632 (i < 9) ? "," : "\0");
1633 if (copy_to_user(p, bname, strlen(bname) + 1))
1634 return -EFAULT;
1635 p += strlen(bname);
1638 return 0;
1639 } else
1640 return -EINVAL;
1641 case IIOCDBGVAR:
1642 return -EINVAL;
1643 default:
1644 if ((cmd & IIOCDRVCTL) == IIOCDRVCTL)
1645 cmd = ((cmd >> _IOC_NRSHIFT) & _IOC_NRMASK) & ISDN_DRVIOCTL_MASK;
1646 else
1647 return -EINVAL;
1648 if (arg) {
1649 int i;
1650 char *p;
1651 if (copy_from_user(&iocts, argp, sizeof(isdn_ioctl_struct)))
1652 return -EFAULT;
1653 iocts.drvid[sizeof(iocts.drvid) - 1] = 0;
1654 if (strlen(iocts.drvid)) {
1655 if ((p = strchr(iocts.drvid, ',')))
1656 *p = 0;
1657 drvidx = -1;
1658 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1659 if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1660 drvidx = i;
1661 break;
1663 } else
1664 drvidx = 0;
1665 if (drvidx == -1)
1666 return -ENODEV;
1667 c.driver = drvidx;
1668 c.command = ISDN_CMD_IOCTL;
1669 c.arg = cmd;
1670 memcpy(c.parm.num, &iocts.arg, sizeof(ulong));
1671 ret = isdn_command(&c);
1672 memcpy(&iocts.arg, c.parm.num, sizeof(ulong));
1673 if (copy_to_user(argp, &iocts, sizeof(isdn_ioctl_struct)))
1674 return -EFAULT;
1675 return ret;
1676 } else
1677 return -EINVAL;
1680 #ifdef CONFIG_ISDN_PPP
1681 if (minor <= ISDN_MINOR_PPPMAX)
1682 return (isdn_ppp_ioctl(minor - ISDN_MINOR_PPP, file, cmd, arg));
1683 #endif
1684 return -ENODEV;
1686 #undef name
1687 #undef bname
1688 #undef iocts
1689 #undef phone
1690 #undef cfg
1693 static long
1694 isdn_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1696 int ret;
1698 mutex_lock(&isdn_mutex);
1699 ret = isdn_ioctl(file, cmd, arg);
1700 mutex_unlock(&isdn_mutex);
1702 return ret;
1706 * Open the device code.
1708 static int
1709 isdn_open(struct inode *ino, struct file *filep)
1711 uint minor = iminor(ino);
1712 int drvidx;
1713 int chidx;
1714 int retval = -ENODEV;
1716 mutex_lock(&isdn_mutex);
1717 if (minor == ISDN_MINOR_STATUS) {
1718 infostruct *p;
1720 if ((p = kmalloc(sizeof(infostruct), GFP_KERNEL))) {
1721 p->next = (char *) dev->infochain;
1722 p->private = (char *) &(filep->private_data);
1723 dev->infochain = p;
1724 /* At opening we allow a single update */
1725 filep->private_data = (char *) 1;
1726 retval = 0;
1727 goto out;
1728 } else {
1729 retval = -ENOMEM;
1730 goto out;
1733 if (!dev->channels)
1734 goto out;
1735 if (minor <= ISDN_MINOR_BMAX) {
1736 printk(KERN_WARNING "isdn_open minor %d obsolete!\n", minor);
1737 drvidx = isdn_minor2drv(minor);
1738 if (drvidx < 0)
1739 goto out;
1740 chidx = isdn_minor2chan(minor);
1741 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1742 goto out;
1743 if (!(dev->drv[drvidx]->online & (1 << chidx)))
1744 goto out;
1745 isdn_lock_drivers();
1746 retval = 0;
1747 goto out;
1749 if (minor <= ISDN_MINOR_CTRLMAX) {
1750 drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1751 if (drvidx < 0)
1752 goto out;
1753 isdn_lock_drivers();
1754 retval = 0;
1755 goto out;
1757 #ifdef CONFIG_ISDN_PPP
1758 if (minor <= ISDN_MINOR_PPPMAX) {
1759 retval = isdn_ppp_open(minor - ISDN_MINOR_PPP, filep);
1760 if (retval == 0)
1761 isdn_lock_drivers();
1762 goto out;
1764 #endif
1765 out:
1766 nonseekable_open(ino, filep);
1767 mutex_unlock(&isdn_mutex);
1768 return retval;
1771 static int
1772 isdn_close(struct inode *ino, struct file *filep)
1774 uint minor = iminor(ino);
1776 mutex_lock(&isdn_mutex);
1777 if (minor == ISDN_MINOR_STATUS) {
1778 infostruct *p = dev->infochain;
1779 infostruct *q = NULL;
1781 while (p) {
1782 if (p->private == (char *) &(filep->private_data)) {
1783 if (q)
1784 q->next = p->next;
1785 else
1786 dev->infochain = (infostruct *) (p->next);
1787 kfree(p);
1788 goto out;
1790 q = p;
1791 p = (infostruct *) (p->next);
1793 printk(KERN_WARNING "isdn: No private data while closing isdnctrl\n");
1794 goto out;
1796 isdn_unlock_drivers();
1797 if (minor <= ISDN_MINOR_BMAX)
1798 goto out;
1799 if (minor <= ISDN_MINOR_CTRLMAX) {
1800 if (dev->profd == current)
1801 dev->profd = NULL;
1802 goto out;
1804 #ifdef CONFIG_ISDN_PPP
1805 if (minor <= ISDN_MINOR_PPPMAX)
1806 isdn_ppp_release(minor - ISDN_MINOR_PPP, filep);
1807 #endif
1809 out:
1810 mutex_unlock(&isdn_mutex);
1811 return 0;
1814 static const struct file_operations isdn_fops =
1816 .owner = THIS_MODULE,
1817 .llseek = no_llseek,
1818 .read = isdn_read,
1819 .write = isdn_write,
1820 .poll = isdn_poll,
1821 .unlocked_ioctl = isdn_unlocked_ioctl,
1822 .open = isdn_open,
1823 .release = isdn_close,
1826 char *
1827 isdn_map_eaz2msn(char *msn, int di)
1829 isdn_driver_t *this = dev->drv[di];
1830 int i;
1832 if (strlen(msn) == 1) {
1833 i = msn[0] - '0';
1834 if ((i >= 0) && (i <= 9))
1835 if (strlen(this->msn2eaz[i]))
1836 return (this->msn2eaz[i]);
1838 return (msn);
1842 * Find an unused ISDN-channel, whose feature-flags match the
1843 * given L2- and L3-protocols.
1845 #define L2V (~(ISDN_FEATURE_L2_V11096 | ISDN_FEATURE_L2_V11019 | ISDN_FEATURE_L2_V11038))
1848 * This function must be called with holding the dev->lock.
1851 isdn_get_free_channel(int usage, int l2_proto, int l3_proto, int pre_dev
1852 , int pre_chan, char *msn)
1854 int i;
1855 ulong features;
1856 ulong vfeatures;
1858 features = ((1 << l2_proto) | (0x10000 << l3_proto));
1859 vfeatures = (((1 << l2_proto) | (0x10000 << l3_proto)) &
1860 ~(ISDN_FEATURE_L2_V11096 | ISDN_FEATURE_L2_V11019 | ISDN_FEATURE_L2_V11038));
1861 /* If Layer-2 protocol is V.110, accept drivers with
1862 * transparent feature even if these don't support V.110
1863 * because we can emulate this in linklevel.
1865 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1866 if (USG_NONE(dev->usage[i]) &&
1867 (dev->drvmap[i] != -1)) {
1868 int d = dev->drvmap[i];
1869 if ((dev->usage[i] & ISDN_USAGE_EXCLUSIVE) &&
1870 ((pre_dev != d) || (pre_chan != dev->chanmap[i])))
1871 continue;
1872 if (!strcmp(isdn_map_eaz2msn(msn, d), "-"))
1873 continue;
1874 if (dev->usage[i] & ISDN_USAGE_DISABLED)
1875 continue; /* usage not allowed */
1876 if (dev->drv[d]->flags & DRV_FLAG_RUNNING) {
1877 if (((dev->drv[d]->interface->features & features) == features) ||
1878 (((dev->drv[d]->interface->features & vfeatures) == vfeatures) &&
1879 (dev->drv[d]->interface->features & ISDN_FEATURE_L2_TRANS))) {
1880 if ((pre_dev < 0) || (pre_chan < 0)) {
1881 dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1882 dev->usage[i] |= usage;
1883 isdn_info_update();
1884 return i;
1885 } else {
1886 if ((pre_dev == d) && (pre_chan == dev->chanmap[i])) {
1887 dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1888 dev->usage[i] |= usage;
1889 isdn_info_update();
1890 return i;
1896 return -1;
1900 * Set state of ISDN-channel to 'unused'
1902 void
1903 isdn_free_channel(int di, int ch, int usage)
1905 int i;
1907 if ((di < 0) || (ch < 0)) {
1908 printk(KERN_WARNING "%s: called with invalid drv(%d) or channel(%d)\n",
1909 __func__, di, ch);
1910 return;
1912 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1913 if (((!usage) || ((dev->usage[i] & ISDN_USAGE_MASK) == usage)) &&
1914 (dev->drvmap[i] == di) &&
1915 (dev->chanmap[i] == ch)) {
1916 dev->usage[i] &= (ISDN_USAGE_NONE | ISDN_USAGE_EXCLUSIVE);
1917 strcpy(dev->num[i], "???");
1918 dev->ibytes[i] = 0;
1919 dev->obytes[i] = 0;
1920 // 20.10.99 JIM, try to reinitialize v110 !
1921 dev->v110emu[i] = 0;
1922 atomic_set(&(dev->v110use[i]), 0);
1923 isdn_v110_close(dev->v110[i]);
1924 dev->v110[i] = NULL;
1925 // 20.10.99 JIM, try to reinitialize v110 !
1926 isdn_info_update();
1927 if (dev->drv[di])
1928 skb_queue_purge(&dev->drv[di]->rpqueue[ch]);
1933 * Cancel Exclusive-Flag for ISDN-channel
1935 void
1936 isdn_unexclusive_channel(int di, int ch)
1938 int i;
1940 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1941 if ((dev->drvmap[i] == di) &&
1942 (dev->chanmap[i] == ch)) {
1943 dev->usage[i] &= ~ISDN_USAGE_EXCLUSIVE;
1944 isdn_info_update();
1945 return;
1950 * writebuf replacement for SKB_ABLE drivers
1952 static int
1953 isdn_writebuf_stub(int drvidx, int chan, const u_char __user *buf, int len)
1955 int ret;
1956 int hl = dev->drv[drvidx]->interface->hl_hdrlen;
1957 struct sk_buff *skb = alloc_skb(hl + len, GFP_ATOMIC);
1959 if (!skb)
1960 return -ENOMEM;
1961 skb_reserve(skb, hl);
1962 if (copy_from_user(skb_put(skb, len), buf, len)) {
1963 dev_kfree_skb(skb);
1964 return -EFAULT;
1966 ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, 1, skb);
1967 if (ret <= 0)
1968 dev_kfree_skb(skb);
1969 if (ret > 0)
1970 dev->obytes[isdn_dc2minor(drvidx, chan)] += ret;
1971 return ret;
1975 * Return: length of data on success, -ERRcode on failure.
1978 isdn_writebuf_skb_stub(int drvidx, int chan, int ack, struct sk_buff *skb)
1980 int ret;
1981 struct sk_buff *nskb = NULL;
1982 int v110_ret = skb->len;
1983 int idx = isdn_dc2minor(drvidx, chan);
1985 if (dev->v110[idx]) {
1986 atomic_inc(&dev->v110use[idx]);
1987 nskb = isdn_v110_encode(dev->v110[idx], skb);
1988 atomic_dec(&dev->v110use[idx]);
1989 if (!nskb)
1990 return 0;
1991 v110_ret = *((int *)nskb->data);
1992 skb_pull(nskb, sizeof(int));
1993 if (!nskb->len) {
1994 dev_kfree_skb(nskb);
1995 return v110_ret;
1997 /* V.110 must always be acknowledged */
1998 ack = 1;
1999 ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, nskb);
2000 } else {
2001 int hl = dev->drv[drvidx]->interface->hl_hdrlen;
2003 if (skb_headroom(skb) < hl) {
2005 * This should only occur when new HL driver with
2006 * increased hl_hdrlen was loaded after netdevice
2007 * was created and connected to the new driver.
2009 * The V.110 branch (re-allocates on its own) does
2010 * not need this
2012 struct sk_buff *skb_tmp;
2014 skb_tmp = skb_realloc_headroom(skb, hl);
2015 printk(KERN_DEBUG "isdn_writebuf_skb_stub: reallocating headroom%s\n", skb_tmp ? "" : " failed");
2016 if (!skb_tmp) return -ENOMEM; /* 0 better? */
2017 ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb_tmp);
2018 if (ret > 0) {
2019 dev_kfree_skb(skb);
2020 } else {
2021 dev_kfree_skb(skb_tmp);
2023 } else {
2024 ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb);
2027 if (ret > 0) {
2028 dev->obytes[idx] += ret;
2029 if (dev->v110[idx]) {
2030 atomic_inc(&dev->v110use[idx]);
2031 dev->v110[idx]->skbuser++;
2032 atomic_dec(&dev->v110use[idx]);
2033 /* For V.110 return unencoded data length */
2034 ret = v110_ret;
2035 /* if the complete frame was send we free the skb;
2036 if not upper function will requeue the skb */
2037 if (ret == skb->len)
2038 dev_kfree_skb(skb);
2040 } else
2041 if (dev->v110[idx])
2042 dev_kfree_skb(nskb);
2043 return ret;
2046 static int
2047 isdn_add_channels(isdn_driver_t *d, int drvidx, int n, int adding)
2049 int j, k, m;
2051 init_waitqueue_head(&d->st_waitq);
2052 if (d->flags & DRV_FLAG_RUNNING)
2053 return -1;
2054 if (n < 1) return 0;
2056 m = (adding) ? d->channels + n : n;
2058 if (dev->channels + n > ISDN_MAX_CHANNELS) {
2059 printk(KERN_WARNING "register_isdn: Max. %d channels supported\n",
2060 ISDN_MAX_CHANNELS);
2061 return -1;
2064 if ((adding) && (d->rcverr))
2065 kfree(d->rcverr);
2066 if (!(d->rcverr = kcalloc(m, sizeof(int), GFP_ATOMIC))) {
2067 printk(KERN_WARNING "register_isdn: Could not alloc rcverr\n");
2068 return -1;
2071 if ((adding) && (d->rcvcount))
2072 kfree(d->rcvcount);
2073 if (!(d->rcvcount = kcalloc(m, sizeof(int), GFP_ATOMIC))) {
2074 printk(KERN_WARNING "register_isdn: Could not alloc rcvcount\n");
2075 if (!adding)
2076 kfree(d->rcverr);
2077 return -1;
2080 if ((adding) && (d->rpqueue)) {
2081 for (j = 0; j < d->channels; j++)
2082 skb_queue_purge(&d->rpqueue[j]);
2083 kfree(d->rpqueue);
2085 d->rpqueue = kmalloc_array(m, sizeof(struct sk_buff_head), GFP_ATOMIC);
2086 if (!d->rpqueue) {
2087 printk(KERN_WARNING "register_isdn: Could not alloc rpqueue\n");
2088 if (!adding) {
2089 kfree(d->rcvcount);
2090 kfree(d->rcverr);
2092 return -1;
2094 for (j = 0; j < m; j++) {
2095 skb_queue_head_init(&d->rpqueue[j]);
2098 if ((adding) && (d->rcv_waitq))
2099 kfree(d->rcv_waitq);
2100 d->rcv_waitq = kmalloc(array3_size(sizeof(wait_queue_head_t), 2, m),
2101 GFP_ATOMIC);
2102 if (!d->rcv_waitq) {
2103 printk(KERN_WARNING "register_isdn: Could not alloc rcv_waitq\n");
2104 if (!adding) {
2105 kfree(d->rpqueue);
2106 kfree(d->rcvcount);
2107 kfree(d->rcverr);
2109 return -1;
2111 d->snd_waitq = d->rcv_waitq + m;
2112 for (j = 0; j < m; j++) {
2113 init_waitqueue_head(&d->rcv_waitq[j]);
2114 init_waitqueue_head(&d->snd_waitq[j]);
2117 dev->channels += n;
2118 for (j = d->channels; j < m; j++)
2119 for (k = 0; k < ISDN_MAX_CHANNELS; k++)
2120 if (dev->chanmap[k] < 0) {
2121 dev->chanmap[k] = j;
2122 dev->drvmap[k] = drvidx;
2123 break;
2125 d->channels = m;
2126 return 0;
2130 * Low-level-driver registration
2133 static void
2134 set_global_features(void)
2136 int drvidx;
2138 dev->global_features = 0;
2139 for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++) {
2140 if (!dev->drv[drvidx])
2141 continue;
2142 if (dev->drv[drvidx]->interface)
2143 dev->global_features |= dev->drv[drvidx]->interface->features;
2147 #ifdef CONFIG_ISDN_DIVERSION
2149 static char *map_drvname(int di)
2151 if ((di < 0) || (di >= ISDN_MAX_DRIVERS))
2152 return (NULL);
2153 return (dev->drvid[di]); /* driver name */
2154 } /* map_drvname */
2156 static int map_namedrv(char *id)
2157 { int i;
2159 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
2160 { if (!strcmp(dev->drvid[i], id))
2161 return (i);
2163 return (-1);
2164 } /* map_namedrv */
2166 int DIVERT_REG_NAME(isdn_divert_if *i_div)
2168 if (i_div->if_magic != DIVERT_IF_MAGIC)
2169 return (DIVERT_VER_ERR);
2170 switch (i_div->cmd)
2172 case DIVERT_CMD_REL:
2173 if (divert_if != i_div)
2174 return (DIVERT_REL_ERR);
2175 divert_if = NULL; /* free interface */
2176 return (DIVERT_NO_ERR);
2178 case DIVERT_CMD_REG:
2179 if (divert_if)
2180 return (DIVERT_REG_ERR);
2181 i_div->ll_cmd = isdn_command; /* set command function */
2182 i_div->drv_to_name = map_drvname;
2183 i_div->name_to_drv = map_namedrv;
2184 divert_if = i_div; /* remember interface */
2185 return (DIVERT_NO_ERR);
2187 default:
2188 return (DIVERT_CMD_ERR);
2190 } /* DIVERT_REG_NAME */
2192 EXPORT_SYMBOL(DIVERT_REG_NAME);
2194 #endif /* CONFIG_ISDN_DIVERSION */
2197 EXPORT_SYMBOL(register_isdn);
2198 #ifdef CONFIG_ISDN_PPP
2199 EXPORT_SYMBOL(isdn_ppp_register_compressor);
2200 EXPORT_SYMBOL(isdn_ppp_unregister_compressor);
2201 #endif
2204 register_isdn(isdn_if *i)
2206 isdn_driver_t *d;
2207 int j;
2208 ulong flags;
2209 int drvidx;
2211 if (dev->drivers >= ISDN_MAX_DRIVERS) {
2212 printk(KERN_WARNING "register_isdn: Max. %d drivers supported\n",
2213 ISDN_MAX_DRIVERS);
2214 return 0;
2216 if (!i->writebuf_skb) {
2217 printk(KERN_WARNING "register_isdn: No write routine given.\n");
2218 return 0;
2220 if (!(d = kzalloc(sizeof(isdn_driver_t), GFP_KERNEL))) {
2221 printk(KERN_WARNING "register_isdn: Could not alloc driver-struct\n");
2222 return 0;
2225 d->maxbufsize = i->maxbufsize;
2226 d->pktcount = 0;
2227 d->stavail = 0;
2228 d->flags = DRV_FLAG_LOADED;
2229 d->online = 0;
2230 d->interface = i;
2231 d->channels = 0;
2232 spin_lock_irqsave(&dev->lock, flags);
2233 for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++)
2234 if (!dev->drv[drvidx])
2235 break;
2236 if (isdn_add_channels(d, drvidx, i->channels, 0)) {
2237 spin_unlock_irqrestore(&dev->lock, flags);
2238 kfree(d);
2239 return 0;
2241 i->channels = drvidx;
2242 i->rcvcallb_skb = isdn_receive_skb_callback;
2243 i->statcallb = isdn_status_callback;
2244 if (!strlen(i->id))
2245 sprintf(i->id, "line%d", drvidx);
2246 for (j = 0; j < drvidx; j++)
2247 if (!strcmp(i->id, dev->drvid[j]))
2248 sprintf(i->id, "line%d", drvidx);
2249 dev->drv[drvidx] = d;
2250 strcpy(dev->drvid[drvidx], i->id);
2251 isdn_info_update();
2252 dev->drivers++;
2253 set_global_features();
2254 spin_unlock_irqrestore(&dev->lock, flags);
2255 return 1;
2259 *****************************************************************************
2260 * And now the modules code.
2261 *****************************************************************************
2264 static char *
2265 isdn_getrev(const char *revision)
2267 char *rev;
2268 char *p;
2270 if ((p = strchr(revision, ':'))) {
2271 rev = p + 2;
2272 p = strchr(rev, '$');
2273 *--p = 0;
2274 } else
2275 rev = "???";
2276 return rev;
2280 * Allocate and initialize all data, register modem-devices
2282 static int __init isdn_init(void)
2284 int i;
2285 char tmprev[50];
2287 dev = vzalloc(sizeof(isdn_dev));
2288 if (!dev) {
2289 printk(KERN_WARNING "isdn: Could not allocate device-struct.\n");
2290 return -EIO;
2292 timer_setup(&dev->timer, isdn_timer_funct, 0);
2293 spin_lock_init(&dev->lock);
2294 spin_lock_init(&dev->timerlock);
2295 #ifdef MODULE
2296 dev->owner = THIS_MODULE;
2297 #endif
2298 mutex_init(&dev->mtx);
2299 init_waitqueue_head(&dev->info_waitq);
2300 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2301 dev->drvmap[i] = -1;
2302 dev->chanmap[i] = -1;
2303 dev->m_idx[i] = -1;
2304 strcpy(dev->num[i], "???");
2306 if (register_chrdev(ISDN_MAJOR, "isdn", &isdn_fops)) {
2307 printk(KERN_WARNING "isdn: Could not register control devices\n");
2308 vfree(dev);
2309 return -EIO;
2311 if ((isdn_tty_modem_init()) < 0) {
2312 printk(KERN_WARNING "isdn: Could not register tty devices\n");
2313 vfree(dev);
2314 unregister_chrdev(ISDN_MAJOR, "isdn");
2315 return -EIO;
2317 #ifdef CONFIG_ISDN_PPP
2318 if (isdn_ppp_init() < 0) {
2319 printk(KERN_WARNING "isdn: Could not create PPP-device-structs\n");
2320 isdn_tty_exit();
2321 unregister_chrdev(ISDN_MAJOR, "isdn");
2322 vfree(dev);
2323 return -EIO;
2325 #endif /* CONFIG_ISDN_PPP */
2327 strcpy(tmprev, isdn_revision);
2328 printk(KERN_NOTICE "ISDN subsystem Rev: %s/", isdn_getrev(tmprev));
2329 strcpy(tmprev, isdn_net_revision);
2330 printk("%s/", isdn_getrev(tmprev));
2331 strcpy(tmprev, isdn_ppp_revision);
2332 printk("%s/", isdn_getrev(tmprev));
2333 strcpy(tmprev, isdn_audio_revision);
2334 printk("%s/", isdn_getrev(tmprev));
2335 strcpy(tmprev, isdn_v110_revision);
2336 printk("%s", isdn_getrev(tmprev));
2338 #ifdef MODULE
2339 printk(" loaded\n");
2340 #else
2341 printk("\n");
2342 #endif
2343 isdn_info_update();
2344 return 0;
2348 * Unload module
2350 static void __exit isdn_exit(void)
2352 #ifdef CONFIG_ISDN_PPP
2353 isdn_ppp_cleanup();
2354 #endif
2355 if (isdn_net_rmall() < 0) {
2356 printk(KERN_WARNING "isdn: net-device busy, remove cancelled\n");
2357 return;
2359 isdn_tty_exit();
2360 unregister_chrdev(ISDN_MAJOR, "isdn");
2361 del_timer_sync(&dev->timer);
2362 /* call vfree with interrupts enabled, else it will hang */
2363 vfree(dev);
2364 printk(KERN_NOTICE "ISDN-subsystem unloaded\n");
2367 module_init(isdn_init);
2368 module_exit(isdn_exit);