Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / isdn / isdn_common.c
blob615d11fc1d7774d4b7ffce6b31903d377d6dab46
1 /* $Id: isdn_common.c,v 1.114 2000/11/25 17:00:59 kai 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 program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/version.h>
28 #include <linux/poll.h>
29 #include <linux/vmalloc.h>
30 #include <linux/isdn.h>
31 #include <linux/smp_lock.h>
32 #include "isdn_common.h"
33 #include "isdn_tty.h"
34 #include "isdn_net.h"
35 #include "isdn_ppp.h"
36 #ifdef CONFIG_ISDN_AUDIO
37 #include "isdn_audio.h"
38 #endif
39 #ifdef CONFIG_ISDN_DIVERSION_MODULE
40 #define CONFIG_ISDN_DIVERSION
41 #endif
42 #ifdef CONFIG_ISDN_DIVERSION
43 #include <linux/isdn_divertif.h>
44 #endif CONFIG_ISDN_DIVERSION
45 #include "isdn_v110.h"
46 #include "isdn_cards.h"
47 #include <linux/devfs_fs_kernel.h>
49 /* Debugflags */
50 #undef ISDN_DEBUG_STATCALLB
52 isdn_dev *dev;
54 static char *isdn_revision = "$Revision: 1.114 $";
56 extern char *isdn_net_revision;
57 extern char *isdn_tty_revision;
58 #ifdef CONFIG_ISDN_PPP
59 extern char *isdn_ppp_revision;
60 #else
61 static char *isdn_ppp_revision = ": none $";
62 #endif
63 #ifdef CONFIG_ISDN_AUDIO
64 extern char *isdn_audio_revision;
65 #else
66 static char *isdn_audio_revision = ": none $";
67 #endif
68 extern char *isdn_v110_revision;
70 #ifdef CONFIG_ISDN_DIVERSION
71 isdn_divert_if *divert_if; /* interface to diversion module */
72 #endif CONFIG_ISDN_DIVERSION
75 static int isdn_writebuf_stub(int, int, const u_char *, int, int);
76 static void set_global_features(void);
77 static void isdn_register_devfs(int);
78 static void isdn_unregister_devfs(int);
79 static int isdn_wildmat(char *s, char *p);
81 void
82 isdn_lock_drivers(void)
84 int i;
86 for (i = 0; i < dev->drivers; i++) {
87 isdn_ctrl cmd;
89 cmd.driver = i;
90 cmd.arg = 0;
91 cmd.command = ISDN_CMD_LOCK;
92 isdn_command(&cmd);
93 dev->drv[i]->locks++;
97 void
98 isdn_MOD_INC_USE_COUNT(void)
100 MOD_INC_USE_COUNT;
101 isdn_lock_drivers();
104 void
105 isdn_unlock_drivers(void)
107 int i;
109 for (i = 0; i < dev->drivers; i++)
110 if (dev->drv[i]->locks > 0) {
111 isdn_ctrl cmd;
113 cmd.driver = i;
114 cmd.arg = 0;
115 cmd.command = ISDN_CMD_UNLOCK;
116 isdn_command(&cmd);
117 dev->drv[i]->locks--;
121 void
122 isdn_MOD_DEC_USE_COUNT(void)
124 MOD_DEC_USE_COUNT;
125 isdn_unlock_drivers();
128 #if defined(ISDN_DEBUG_NET_DUMP) || defined(ISDN_DEBUG_MODEM_DUMP)
129 void
130 isdn_dumppkt(char *s, u_char * p, int len, int dumplen)
132 int dumpc;
134 printk(KERN_DEBUG "%s(%d) ", s, len);
135 for (dumpc = 0; (dumpc < dumplen) && (len); len--, dumpc++)
136 printk(" %02x", *p++);
137 printk("\n");
139 #endif
142 * I picked the pattern-matching-functions from an old GNU-tar version (1.10)
143 * It was originally written and put to PD by rs@mirror.TMC.COM (Rich Salz)
145 static int
146 isdn_star(char *s, char *p)
148 while (isdn_wildmat(s, p)) {
149 if (*++s == '\0')
150 return (2);
152 return (0);
156 * Shell-type Pattern-matching for incoming caller-Ids
157 * This function gets a string in s and checks, if it matches the pattern
158 * given in p.
160 * Return:
161 * 0 = match.
162 * 1 = no match.
163 * 2 = no match. Would eventually match, if s would be longer.
165 * Possible Patterns:
167 * '?' matches one character
168 * '*' matches zero or more characters
169 * [xyz] matches the set of characters in brackets.
170 * [^xyz] matches any single character not in the set of characters
173 static int
174 isdn_wildmat(char *s, char *p)
176 register int last;
177 register int matched;
178 register int reverse;
179 register int nostar = 1;
181 if (!(*s) && !(*p))
182 return(1);
183 for (; *p; s++, p++)
184 switch (*p) {
185 case '\\':
187 * Literal match with following character,
188 * fall through.
190 p++;
191 default:
192 if (*s != *p)
193 return (*s == '\0')?2:1;
194 continue;
195 case '?':
196 /* Match anything. */
197 if (*s == '\0')
198 return (2);
199 continue;
200 case '*':
201 nostar = 0;
202 /* Trailing star matches everything. */
203 return (*++p ? isdn_star(s, p) : 0);
204 case '[':
205 /* [^....] means inverse character class. */
206 if ((reverse = (p[1] == '^')))
207 p++;
208 for (last = 0, matched = 0; *++p && (*p != ']'); last = *p)
209 /* This next line requires a good C compiler. */
210 if (*p == '-' ? *s <= *++p && *s >= last : *s == *p)
211 matched = 1;
212 if (matched == reverse)
213 return (1);
214 continue;
216 return (*s == '\0')?0:nostar;
219 int isdn_msncmp( const char * msn1, const char * msn2 )
221 char TmpMsn1[ ISDN_MSNLEN ];
222 char TmpMsn2[ ISDN_MSNLEN ];
223 char *p;
225 for ( p = TmpMsn1; *msn1 && *msn1 != ':'; ) // Strip off a SPID
226 *p++ = *msn1++;
227 *p = '\0';
229 for ( p = TmpMsn2; *msn2 && *msn2 != ':'; ) // Strip off a SPID
230 *p++ = *msn2++;
231 *p = '\0';
233 return isdn_wildmat( TmpMsn1, TmpMsn2 );
236 static void
237 isdn_free_queue(struct sk_buff_head *queue)
239 struct sk_buff *skb;
240 unsigned long flags;
242 save_flags(flags);
243 cli();
244 if (skb_queue_len(queue))
245 while ((skb = skb_dequeue(queue)))
246 dev_kfree_skb(skb);
247 restore_flags(flags);
251 isdn_dc2minor(int di, int ch)
253 int i;
254 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
255 if (dev->chanmap[i] == ch && dev->drvmap[i] == di)
256 return i;
257 return -1;
260 static int isdn_timer_cnt1 = 0;
261 static int isdn_timer_cnt2 = 0;
262 static int isdn_timer_cnt3 = 0;
263 static int isdn_timer_cnt4 = 0;
265 static void
266 isdn_timer_funct(ulong dummy)
268 int tf = dev->tflags;
269 if (tf & ISDN_TIMER_FAST) {
270 if (tf & ISDN_TIMER_MODEMREAD)
271 isdn_tty_readmodem();
272 if (tf & ISDN_TIMER_MODEMPLUS)
273 isdn_tty_modem_escape();
274 if (tf & ISDN_TIMER_MODEMXMIT)
275 isdn_tty_modem_xmit();
277 if (tf & ISDN_TIMER_SLOW) {
278 if (++isdn_timer_cnt1 >= ISDN_TIMER_02SEC) {
279 isdn_timer_cnt1 = 0;
280 if (tf & ISDN_TIMER_NETDIAL)
281 isdn_net_dial();
283 if (++isdn_timer_cnt2 >= ISDN_TIMER_1SEC) {
284 isdn_timer_cnt2 = 0;
285 if (tf & ISDN_TIMER_NETHANGUP)
286 isdn_net_autohup();
287 if (++isdn_timer_cnt3 > ISDN_TIMER_RINGING) {
288 isdn_timer_cnt3 = 0;
289 if (tf & ISDN_TIMER_MODEMRING)
290 isdn_tty_modem_ring();
292 if (++isdn_timer_cnt4 > ISDN_TIMER_KEEPINT) {
293 isdn_timer_cnt4 = 0;
294 if (tf & ISDN_TIMER_KEEPALIVE)
295 isdn_net_slarp_out();
297 if (tf & ISDN_TIMER_CARRIER)
298 isdn_tty_carrier_timeout();
301 if (tf)
303 int flags;
305 save_flags(flags);
306 cli();
307 mod_timer(&dev->timer, jiffies+ISDN_TIMER_RES);
308 restore_flags(flags);
312 void
313 isdn_timer_ctrl(int tf, int onoff)
315 int flags;
317 save_flags(flags);
318 cli();
319 if ((tf & ISDN_TIMER_SLOW) && (!(dev->tflags & ISDN_TIMER_SLOW))) {
320 /* If the slow-timer wasn't activated until now */
321 isdn_timer_cnt1 = 0;
322 isdn_timer_cnt2 = 0;
324 if (onoff)
325 dev->tflags |= tf;
326 else
327 dev->tflags &= ~tf;
328 if (dev->tflags)
329 mod_timer(&dev->timer, jiffies+ISDN_TIMER_RES);
330 restore_flags(flags);
334 * Receive a packet from B-Channel. (Called from low-level-module)
336 static void
337 isdn_receive_skb_callback(int di, int channel, struct sk_buff *skb)
339 int i;
341 if ((i = isdn_dc2minor(di, channel)) == -1) {
342 dev_kfree_skb(skb);
343 return;
345 /* Update statistics */
346 dev->ibytes[i] += skb->len;
348 /* First, try to deliver data to network-device */
349 if (isdn_net_rcv_skb(i, skb))
350 return;
352 /* V.110 handling
353 * makes sense for async streams only, so it is
354 * called after possible net-device delivery.
356 if (dev->v110[i]) {
357 atomic_inc(&dev->v110use[i]);
358 skb = isdn_v110_decode(dev->v110[i], skb);
359 atomic_dec(&dev->v110use[i]);
360 if (!skb)
361 return;
364 /* No network-device found, deliver to tty or raw-channel */
365 if (skb->len) {
366 if (isdn_tty_rcv_skb(i, di, channel, skb))
367 return;
368 wake_up_interruptible(&dev->drv[di]->rcv_waitq[channel]);
369 } else
370 dev_kfree_skb(skb);
374 * Intercept command from Linklevel to Lowlevel.
375 * If layer 2 protocol is V.110 and this is not supported by current
376 * lowlevel-driver, use driver's transparent mode and handle V.110 in
377 * linklevel instead.
380 isdn_command(isdn_ctrl *cmd)
382 if (cmd->driver == -1) {
383 printk(KERN_WARNING "isdn_command command(%x) driver -1\n", cmd->command);
384 return(1);
386 if (cmd->command == ISDN_CMD_SETL2) {
387 int idx = isdn_dc2minor(cmd->driver, cmd->arg & 255);
388 unsigned long l2prot = (cmd->arg >> 8) & 255;
389 unsigned long features = (dev->drv[cmd->driver]->interface->features
390 >> ISDN_FEATURE_L2_SHIFT) &
391 ISDN_FEATURE_L2_MASK;
392 unsigned long l2_feature = (1 << l2prot);
394 switch (l2prot) {
395 case ISDN_PROTO_L2_V11096:
396 case ISDN_PROTO_L2_V11019:
397 case ISDN_PROTO_L2_V11038:
398 /* If V.110 requested, but not supported by
399 * HL-driver, set emulator-flag and change
400 * Layer-2 to transparent
402 if (!(features & l2_feature)) {
403 dev->v110emu[idx] = l2prot;
404 cmd->arg = (cmd->arg & 255) |
405 (ISDN_PROTO_L2_TRANS << 8);
406 } else
407 dev->v110emu[idx] = 0;
410 return dev->drv[cmd->driver]->interface->command(cmd);
413 void
414 isdn_all_eaz(int di, int ch)
416 isdn_ctrl cmd;
418 if (di < 0)
419 return;
420 cmd.driver = di;
421 cmd.arg = ch;
422 cmd.command = ISDN_CMD_SETEAZ;
423 cmd.parm.num[0] = '\0';
424 isdn_command(&cmd);
428 * Begin of a CAPI like LL<->HL interface, currently used only for
429 * supplementary service (CAPI 2.0 part III)
431 #include "avmb1/capicmd.h" /* this should be moved in a common place */
434 isdn_capi_rec_hl_msg(capi_msg *cm) {
436 int di;
437 int ch;
439 di = (cm->adr.Controller & 0x7f) -1;
440 ch = isdn_dc2minor(di, (cm->adr.Controller>>8)& 0x7f);
441 switch(cm->Command) {
442 case CAPI_FACILITY:
443 /* in the moment only handled in tty */
444 return(isdn_tty_capi_facility(cm));
445 default:
446 return(-1);
450 static int
451 isdn_status_callback(isdn_ctrl * c)
453 int di;
454 ulong flags;
455 int i;
456 int r;
457 int retval = 0;
458 isdn_ctrl cmd;
459 isdn_net_dev *p;
461 di = c->driver;
462 i = isdn_dc2minor(di, c->arg);
463 switch (c->command) {
464 case ISDN_STAT_BSENT:
465 if (i < 0)
466 return -1;
467 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
468 return 0;
469 if (isdn_net_stat_callback(i, c))
470 return 0;
471 if (isdn_v110_stat_callback(i, c))
472 return 0;
473 if (isdn_tty_stat_callback(i, c))
474 return 0;
475 wake_up_interruptible(&dev->drv[di]->snd_waitq[c->arg]);
476 break;
477 case ISDN_STAT_STAVAIL:
478 save_flags(flags);
479 cli();
480 dev->drv[di]->stavail += c->arg;
481 restore_flags(flags);
482 wake_up_interruptible(&dev->drv[di]->st_waitq);
483 break;
484 case ISDN_STAT_RUN:
485 dev->drv[di]->flags |= DRV_FLAG_RUNNING;
486 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
487 if (dev->drvmap[i] == di)
488 isdn_all_eaz(di, dev->chanmap[i]);
489 set_global_features();
490 break;
491 case ISDN_STAT_STOP:
492 dev->drv[di]->flags &= ~DRV_FLAG_RUNNING;
493 break;
494 case ISDN_STAT_ICALL:
495 if (i < 0)
496 return -1;
497 #ifdef ISDN_DEBUG_STATCALLB
498 printk(KERN_DEBUG "ICALL (net): %d %ld %s\n", di, c->arg, c->parm.num);
499 #endif
500 if (dev->global_flags & ISDN_GLOBAL_STOPPED) {
501 cmd.driver = di;
502 cmd.arg = c->arg;
503 cmd.command = ISDN_CMD_HANGUP;
504 isdn_command(&cmd);
505 return 0;
507 /* Try to find a network-interface which will accept incoming call */
508 r = ((c->command == ISDN_STAT_ICALLW) ? 0 : isdn_net_find_icall(di, c->arg, i, &c->parm.setup));
509 switch (r) {
510 case 0:
511 /* No network-device replies.
512 * Try ttyI's.
513 * These return 0 on no match, 1 on match and
514 * 3 on eventually match, if CID is longer.
516 if (c->command == ISDN_STAT_ICALL)
517 if ((retval = isdn_tty_find_icall(di, c->arg, &c->parm.setup))) return(retval);
518 #ifdef CONFIG_ISDN_DIVERSION
519 if (divert_if)
520 if ((retval = divert_if->stat_callback(c)))
521 return(retval); /* processed */
522 #endif CONFIG_ISDN_DIVERSION
523 if ((!retval) && (dev->drv[di]->flags & DRV_FLAG_REJBUS)) {
524 /* No tty responding */
525 cmd.driver = di;
526 cmd.arg = c->arg;
527 cmd.command = ISDN_CMD_HANGUP;
528 isdn_command(&cmd);
529 retval = 2;
531 break;
532 case 1:
533 /* Schedule connection-setup */
534 isdn_net_dial();
535 cmd.driver = di;
536 cmd.arg = c->arg;
537 cmd.command = ISDN_CMD_ACCEPTD;
538 for ( p = dev->netdev; p; p = p->next )
539 if ( p->local->isdn_channel == cmd.arg )
541 strcpy( cmd.parm.setup.eazmsn, p->local->msn );
542 isdn_command(&cmd);
543 retval = 1;
544 break;
546 break;
548 case 2: /* For calling back, first reject incoming call ... */
549 case 3: /* Interface found, but down, reject call actively */
550 retval = 2;
551 printk(KERN_INFO "isdn: Rejecting Call\n");
552 cmd.driver = di;
553 cmd.arg = c->arg;
554 cmd.command = ISDN_CMD_HANGUP;
555 isdn_command(&cmd);
556 if (r == 3)
557 break;
558 /* Fall through */
559 case 4:
560 /* ... then start callback. */
561 isdn_net_dial();
562 break;
563 case 5:
564 /* Number would eventually match, if longer */
565 retval = 3;
566 break;
568 #ifdef ISDN_DEBUG_STATCALLB
569 printk(KERN_DEBUG "ICALL: ret=%d\n", retval);
570 #endif
571 return retval;
572 break;
573 case ISDN_STAT_CINF:
574 if (i < 0)
575 return -1;
576 #ifdef ISDN_DEBUG_STATCALLB
577 printk(KERN_DEBUG "CINF: %ld %s\n", c->arg, c->parm.num);
578 #endif
579 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
580 return 0;
581 if (strcmp(c->parm.num, "0"))
582 isdn_net_stat_callback(i, c);
583 isdn_tty_stat_callback(i, c);
584 break;
585 case ISDN_STAT_CAUSE:
586 #ifdef ISDN_DEBUG_STATCALLB
587 printk(KERN_DEBUG "CAUSE: %ld %s\n", c->arg, c->parm.num);
588 #endif
589 printk(KERN_INFO "isdn: %s,ch%ld cause: %s\n",
590 dev->drvid[di], c->arg, c->parm.num);
591 isdn_tty_stat_callback(i, c);
592 #ifdef CONFIG_ISDN_DIVERSION
593 if (divert_if)
594 divert_if->stat_callback(c);
595 #endif CONFIG_ISDN_DIVERSION
596 break;
597 case ISDN_STAT_DISPLAY:
598 #ifdef ISDN_DEBUG_STATCALLB
599 printk(KERN_DEBUG "DISPLAY: %ld %s\n", c->arg, c->parm.display);
600 #endif
601 isdn_tty_stat_callback(i, c);
602 #ifdef CONFIG_ISDN_DIVERSION
603 if (divert_if)
604 divert_if->stat_callback(c);
605 #endif CONFIG_ISDN_DIVERSION
606 break;
607 case ISDN_STAT_DCONN:
608 if (i < 0)
609 return -1;
610 #ifdef ISDN_DEBUG_STATCALLB
611 printk(KERN_DEBUG "DCONN: %ld\n", c->arg);
612 #endif
613 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
614 return 0;
615 /* Find any net-device, waiting for D-channel setup */
616 if (isdn_net_stat_callback(i, c))
617 break;
618 isdn_v110_stat_callback(i, c);
619 /* Find any ttyI, waiting for D-channel setup */
620 if (isdn_tty_stat_callback(i, c)) {
621 cmd.driver = di;
622 cmd.arg = c->arg;
623 cmd.command = ISDN_CMD_ACCEPTB;
624 isdn_command(&cmd);
625 break;
627 break;
628 case ISDN_STAT_DHUP:
629 if (i < 0)
630 return -1;
631 #ifdef ISDN_DEBUG_STATCALLB
632 printk(KERN_DEBUG "DHUP: %ld\n", c->arg);
633 #endif
634 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
635 return 0;
636 dev->drv[di]->online &= ~(1 << (c->arg));
637 isdn_info_update();
638 /* Signal hangup to network-devices */
639 if (isdn_net_stat_callback(i, c))
640 break;
641 isdn_v110_stat_callback(i, c);
642 if (isdn_tty_stat_callback(i, c))
643 break;
644 #ifdef CONFIG_ISDN_DIVERSION
645 if (divert_if)
646 divert_if->stat_callback(c);
647 #endif CONFIG_ISDN_DIVERSION
648 break;
649 break;
650 case ISDN_STAT_BCONN:
651 if (i < 0)
652 return -1;
653 #ifdef ISDN_DEBUG_STATCALLB
654 printk(KERN_DEBUG "BCONN: %ld\n", c->arg);
655 #endif
656 /* Signal B-channel-connect to network-devices */
657 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
658 return 0;
659 dev->drv[di]->online |= (1 << (c->arg));
660 isdn_info_update();
661 if (isdn_net_stat_callback(i, c))
662 break;
663 isdn_v110_stat_callback(i, c);
664 if (isdn_tty_stat_callback(i, c))
665 break;
666 break;
667 case ISDN_STAT_BHUP:
668 if (i < 0)
669 return -1;
670 #ifdef ISDN_DEBUG_STATCALLB
671 printk(KERN_DEBUG "BHUP: %ld\n", c->arg);
672 #endif
673 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
674 return 0;
675 dev->drv[di]->online &= ~(1 << (c->arg));
676 isdn_info_update();
677 #ifdef CONFIG_ISDN_X25
678 /* Signal hangup to network-devices */
679 if (isdn_net_stat_callback(i, c))
680 break;
681 #endif
682 isdn_v110_stat_callback(i, c);
683 if (isdn_tty_stat_callback(i, c))
684 break;
685 break;
686 case ISDN_STAT_NODCH:
687 if (i < 0)
688 return -1;
689 #ifdef ISDN_DEBUG_STATCALLB
690 printk(KERN_DEBUG "NODCH: %ld\n", c->arg);
691 #endif
692 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
693 return 0;
694 if (isdn_net_stat_callback(i, c))
695 break;
696 if (isdn_tty_stat_callback(i, c))
697 break;
698 break;
699 case ISDN_STAT_ADDCH:
700 if (isdn_add_channels(dev->drv[di], di, c->arg, 1))
701 return -1;
702 isdn_info_update();
703 break;
704 case ISDN_STAT_DISCH:
705 save_flags(flags);
706 cli();
707 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
708 if ((dev->drvmap[i] == di) &&
709 (dev->chanmap[i] == c->arg)) {
710 if (c->parm.num[0])
711 dev->usage[i] &= ~ISDN_USAGE_DISABLED;
712 else
713 if (USG_NONE(dev->usage[i])) {
714 dev->usage[i] |= ISDN_USAGE_DISABLED;
716 else
717 retval = -1;
718 break;
720 restore_flags(flags);
721 isdn_info_update();
722 break;
723 case ISDN_STAT_UNLOAD:
724 while (dev->drv[di]->locks > 0) {
725 isdn_ctrl cmd;
726 cmd.driver = di;
727 cmd.arg = 0;
728 cmd.command = ISDN_CMD_UNLOCK;
729 isdn_command(&cmd);
730 dev->drv[di]->locks--;
732 save_flags(flags);
733 cli();
734 isdn_tty_stat_callback(i, c);
735 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
736 if (dev->drvmap[i] == di) {
737 dev->drvmap[i] = -1;
738 dev->chanmap[i] = -1;
739 dev->usage[i] &= ~ISDN_USAGE_DISABLED;
740 isdn_unregister_devfs(i);
742 dev->drivers--;
743 dev->channels -= dev->drv[di]->channels;
744 kfree(dev->drv[di]->rcverr);
745 kfree(dev->drv[di]->rcvcount);
746 for (i = 0; i < dev->drv[di]->channels; i++)
747 isdn_free_queue(&dev->drv[di]->rpqueue[i]);
748 kfree(dev->drv[di]->rpqueue);
749 kfree(dev->drv[di]->rcv_waitq);
750 kfree(dev->drv[di]);
751 dev->drv[di] = NULL;
752 dev->drvid[di][0] = '\0';
753 isdn_info_update();
754 set_global_features();
755 restore_flags(flags);
756 return 0;
757 case ISDN_STAT_L1ERR:
758 break;
759 case CAPI_PUT_MESSAGE:
760 return(isdn_capi_rec_hl_msg(&c->parm.cmsg));
761 #ifdef CONFIG_ISDN_TTY_FAX
762 case ISDN_STAT_FAXIND:
763 isdn_tty_stat_callback(i, c);
764 break;
765 #endif
766 #ifdef CONFIG_ISDN_AUDIO
767 case ISDN_STAT_AUDIO:
768 isdn_tty_stat_callback(i, c);
769 break;
770 #endif
771 #ifdef CONFIG_ISDN_DIVERSION
772 case ISDN_STAT_PROT:
773 case ISDN_STAT_REDIR:
774 if (divert_if)
775 return(divert_if->stat_callback(c));
776 #endif CONFIG_ISDN_DIVERSION
777 default:
778 return -1;
780 return 0;
784 * Get integer from char-pointer, set pointer to end of number
787 isdn_getnum(char **p)
789 int v = -1;
791 while (*p[0] >= '0' && *p[0] <= '9')
792 v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p[0]++) - '0');
793 return v;
796 #define DLE 0x10
799 * isdn_readbchan() tries to get data from the read-queue.
800 * It MUST be called with interrupts off.
802 * Be aware that this is not an atomic operation when sleep != 0, even though
803 * interrupts are turned off! Well, like that we are currently only called
804 * on behalf of a read system call on raw device files (which are documented
805 * to be dangerous and for for debugging purpose only). The inode semaphore
806 * takes care that this is not called for the same minor device number while
807 * we are sleeping, but access is not serialized against simultaneous read()
808 * from the corresponding ttyI device. Can other ugly events, like changes
809 * of the mapping (di,ch)<->minor, happen during the sleep? --he
812 isdn_readbchan(int di, int channel, u_char * buf, u_char * fp, int len, wait_queue_head_t *sleep)
814 int left;
815 int count;
816 int count_pull;
817 int count_put;
818 int dflag;
819 struct sk_buff *skb;
820 u_char *cp;
822 if (!dev->drv[di])
823 return 0;
824 if (skb_queue_empty(&dev->drv[di]->rpqueue[channel])) {
825 if (sleep)
826 interruptible_sleep_on(sleep);
827 else
828 return 0;
830 left = MIN(len, dev->drv[di]->rcvcount[channel]);
831 cp = buf;
832 count = 0;
833 while (left) {
834 if (!(skb = skb_peek(&dev->drv[di]->rpqueue[channel])))
835 break;
836 #ifdef CONFIG_ISDN_AUDIO
837 if (ISDN_AUDIO_SKB_LOCK(skb))
838 break;
839 ISDN_AUDIO_SKB_LOCK(skb) = 1;
840 if ((ISDN_AUDIO_SKB_DLECOUNT(skb)) || (dev->drv[di]->DLEflag & (1 << channel))) {
841 char *p = skb->data;
842 unsigned long DLEmask = (1 << channel);
844 dflag = 0;
845 count_pull = count_put = 0;
846 while ((count_pull < skb->len) && (left > 0)) {
847 left--;
848 if (dev->drv[di]->DLEflag & DLEmask) {
849 *cp++ = DLE;
850 dev->drv[di]->DLEflag &= ~DLEmask;
851 } else {
852 *cp++ = *p;
853 if (*p == DLE) {
854 dev->drv[di]->DLEflag |= DLEmask;
855 (ISDN_AUDIO_SKB_DLECOUNT(skb))--;
857 p++;
858 count_pull++;
860 count_put++;
862 if (count_pull >= skb->len)
863 dflag = 1;
864 } else {
865 #endif
866 /* No DLE's in buff, so simply copy it */
867 dflag = 1;
868 if ((count_pull = skb->len) > left) {
869 count_pull = left;
870 dflag = 0;
872 count_put = count_pull;
873 memcpy(cp, skb->data, count_put);
874 cp += count_put;
875 left -= count_put;
876 #ifdef CONFIG_ISDN_AUDIO
878 #endif
879 count += count_put;
880 if (fp) {
881 memset(fp, 0, count_put);
882 fp += count_put;
884 if (dflag) {
885 /* We got all the data in this buff.
886 * Now we can dequeue it.
888 if (fp)
889 *(fp - 1) = 0xff;
890 #ifdef CONFIG_ISDN_AUDIO
891 ISDN_AUDIO_SKB_LOCK(skb) = 0;
892 #endif
893 skb = skb_dequeue(&dev->drv[di]->rpqueue[channel]);
894 dev_kfree_skb(skb);
895 } else {
896 /* Not yet emptied this buff, so it
897 * must stay in the queue, for further calls
898 * but we pull off the data we got until now.
900 skb_pull(skb, count_pull);
901 #ifdef CONFIG_ISDN_AUDIO
902 ISDN_AUDIO_SKB_LOCK(skb) = 0;
903 #endif
905 dev->drv[di]->rcvcount[channel] -= count_put;
907 return count;
910 static __inline int
911 isdn_minor2drv(int minor)
913 return (dev->drvmap[minor]);
916 static __inline int
917 isdn_minor2chan(int minor)
919 return (dev->chanmap[minor]);
922 static char *
923 isdn_statstr(void)
925 static char istatbuf[2048];
926 char *p;
927 int i;
929 sprintf(istatbuf, "idmap:\t");
930 p = istatbuf + strlen(istatbuf);
931 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
932 sprintf(p, "%s ", (dev->drvmap[i] < 0) ? "-" : dev->drvid[dev->drvmap[i]]);
933 p = istatbuf + strlen(istatbuf);
935 sprintf(p, "\nchmap:\t");
936 p = istatbuf + strlen(istatbuf);
937 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
938 sprintf(p, "%d ", dev->chanmap[i]);
939 p = istatbuf + strlen(istatbuf);
941 sprintf(p, "\ndrmap:\t");
942 p = istatbuf + strlen(istatbuf);
943 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
944 sprintf(p, "%d ", dev->drvmap[i]);
945 p = istatbuf + strlen(istatbuf);
947 sprintf(p, "\nusage:\t");
948 p = istatbuf + strlen(istatbuf);
949 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
950 sprintf(p, "%d ", dev->usage[i]);
951 p = istatbuf + strlen(istatbuf);
953 sprintf(p, "\nflags:\t");
954 p = istatbuf + strlen(istatbuf);
955 for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
956 if (dev->drv[i]) {
957 sprintf(p, "%ld ", dev->drv[i]->online);
958 p = istatbuf + strlen(istatbuf);
959 } else {
960 sprintf(p, "? ");
961 p = istatbuf + strlen(istatbuf);
964 sprintf(p, "\nphone:\t");
965 p = istatbuf + strlen(istatbuf);
966 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
967 sprintf(p, "%s ", dev->num[i]);
968 p = istatbuf + strlen(istatbuf);
970 sprintf(p, "\n");
971 return istatbuf;
974 /* Module interface-code */
976 void
977 isdn_info_update(void)
979 infostruct *p = dev->infochain;
981 while (p) {
982 *(p->private) = 1;
983 p = (infostruct *) p->next;
985 wake_up_interruptible(&(dev->info_waitq));
988 static ssize_t
989 isdn_read(struct file *file, char *buf, size_t count, loff_t * off)
991 uint minor = MINOR(file->f_dentry->d_inode->i_rdev);
992 int len = 0;
993 ulong flags;
994 int drvidx;
995 int chidx;
996 int retval;
997 char *p;
999 if (off != &file->f_pos)
1000 return -ESPIPE;
1002 lock_kernel();
1003 if (minor == ISDN_MINOR_STATUS) {
1004 if (!file->private_data) {
1005 if (file->f_flags & O_NONBLOCK) {
1006 retval = -EAGAIN;
1007 goto out;
1009 interruptible_sleep_on(&(dev->info_waitq));
1011 p = isdn_statstr();
1012 file->private_data = 0;
1013 if ((len = strlen(p)) <= count) {
1014 if (copy_to_user(buf, p, len)) {
1015 retval = -EFAULT;
1016 goto out;
1018 *off += len;
1019 retval = len;
1020 goto out;
1022 retval = 0;
1023 goto out;
1025 if (!dev->drivers) {
1026 retval = -ENODEV;
1027 goto out;
1029 if (minor < ISDN_MINOR_CTRL) {
1030 printk(KERN_WARNING "isdn_read minor %d obsolete!\n", minor);
1031 drvidx = isdn_minor2drv(minor);
1032 if (drvidx < 0) {
1033 retval = -ENODEV;
1034 goto out;
1036 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
1037 retval = -ENODEV;
1038 goto out;
1040 chidx = isdn_minor2chan(minor);
1041 if (!(p = kmalloc(count, GFP_KERNEL))) {
1042 retval = -ENOMEM;
1043 goto out;
1045 save_flags(flags);
1046 cli();
1047 len = isdn_readbchan(drvidx, chidx, p, 0, count,
1048 &dev->drv[drvidx]->rcv_waitq[chidx]);
1049 *off += len;
1050 restore_flags(flags);
1051 if (copy_to_user(buf,p,len))
1052 len = -EFAULT;
1053 kfree(p);
1054 retval = len;
1055 goto out;
1057 if (minor <= ISDN_MINOR_CTRLMAX) {
1058 drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1059 if (drvidx < 0) {
1060 retval = -ENODEV;
1061 goto out;
1063 if (!dev->drv[drvidx]->stavail) {
1064 if (file->f_flags & O_NONBLOCK) {
1065 retval = -EAGAIN;
1066 goto out;
1068 interruptible_sleep_on(&(dev->drv[drvidx]->st_waitq));
1070 if (dev->drv[drvidx]->interface->readstat)
1071 len = dev->drv[drvidx]->interface->
1072 readstat(buf, MIN(count, dev->drv[drvidx]->stavail),
1073 1, drvidx, isdn_minor2chan(minor));
1074 else
1075 len = 0;
1076 save_flags(flags);
1077 cli();
1078 if (len)
1079 dev->drv[drvidx]->stavail -= len;
1080 else
1081 dev->drv[drvidx]->stavail = 0;
1082 restore_flags(flags);
1083 *off += len;
1084 retval = len;
1085 goto out;
1087 #ifdef CONFIG_ISDN_PPP
1088 if (minor <= ISDN_MINOR_PPPMAX) {
1089 retval = isdn_ppp_read(minor - ISDN_MINOR_PPP, file, buf, count);
1090 goto out;
1092 #endif
1093 retval = -ENODEV;
1094 out:
1095 unlock_kernel();
1096 return retval;
1099 static loff_t
1100 isdn_llseek(struct file *file, loff_t offset, int orig)
1102 return -ESPIPE;
1105 static ssize_t
1106 isdn_write(struct file *file, const char *buf, size_t count, loff_t * off)
1108 uint minor = MINOR(file->f_dentry->d_inode->i_rdev);
1109 int drvidx;
1110 int chidx;
1111 int retval;
1113 if (off != &file->f_pos)
1114 return -ESPIPE;
1116 if (minor == ISDN_MINOR_STATUS)
1117 return -EPERM;
1118 if (!dev->drivers)
1119 return -ENODEV;
1121 lock_kernel();
1122 if (minor < ISDN_MINOR_CTRL) {
1123 printk(KERN_WARNING "isdn_write minor %d obsolete!\n", minor);
1124 drvidx = isdn_minor2drv(minor);
1125 if (drvidx < 0) {
1126 retval = -ENODEV;
1127 goto out;
1129 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
1130 retval = -ENODEV;
1131 goto out;
1133 chidx = isdn_minor2chan(minor);
1134 while (isdn_writebuf_stub(drvidx, chidx, buf, count, 1) != count)
1135 interruptible_sleep_on(&dev->drv[drvidx]->snd_waitq[chidx]);
1136 retval = count;
1137 goto out;
1139 if (minor <= ISDN_MINOR_CTRLMAX) {
1140 drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1141 if (drvidx < 0) {
1142 retval = -ENODEV;
1143 goto out;
1146 * We want to use the isdnctrl device to load the firmware
1148 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1149 return -ENODEV;
1151 if (dev->drv[drvidx]->interface->writecmd)
1152 retval = dev->drv[drvidx]->interface->
1153 writecmd(buf, count, 1, drvidx, isdn_minor2chan(minor));
1154 else
1155 retval = count;
1156 goto out;
1158 #ifdef CONFIG_ISDN_PPP
1159 if (minor <= ISDN_MINOR_PPPMAX) {
1160 retval = isdn_ppp_write(minor - ISDN_MINOR_PPP, file, buf, count);
1161 goto out;
1163 #endif
1164 retval = -ENODEV;
1165 out:
1166 unlock_kernel();
1167 return retval;
1170 static unsigned int
1171 isdn_poll(struct file *file, poll_table * wait)
1173 unsigned int mask = 0;
1174 unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
1175 int drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1177 lock_kernel();
1178 if (minor == ISDN_MINOR_STATUS) {
1179 poll_wait(file, &(dev->info_waitq), wait);
1180 /* mask = POLLOUT | POLLWRNORM; */
1181 if (file->private_data) {
1182 mask |= POLLIN | POLLRDNORM;
1184 goto out;
1186 if (minor >= ISDN_MINOR_CTRL && minor <= ISDN_MINOR_CTRLMAX) {
1187 if (drvidx < 0) {
1188 /* driver deregistered while file open */
1189 mask = POLLHUP;
1190 goto out;
1192 poll_wait(file, &(dev->drv[drvidx]->st_waitq), wait);
1193 mask = POLLOUT | POLLWRNORM;
1194 if (dev->drv[drvidx]->stavail) {
1195 mask |= POLLIN | POLLRDNORM;
1197 goto out;
1199 #ifdef CONFIG_ISDN_PPP
1200 if (minor <= ISDN_MINOR_PPPMAX) {
1201 mask = isdn_ppp_poll(file, wait);
1202 goto out;
1204 #endif
1205 mask = POLLERR;
1206 out:
1207 unlock_kernel();
1208 return mask;
1212 static int
1213 isdn_ioctl(struct inode *inode, struct file *file, uint cmd, ulong arg)
1215 uint minor = MINOR(inode->i_rdev);
1216 isdn_ctrl c;
1217 int drvidx;
1218 int chidx;
1219 int ret;
1220 int i;
1221 char *p;
1222 char *s;
1223 union iocpar {
1224 char name[10];
1225 char bname[22];
1226 isdn_ioctl_struct iocts;
1227 isdn_net_ioctl_phone phone;
1228 isdn_net_ioctl_cfg cfg;
1229 } iocpar;
1231 #define name iocpar.name
1232 #define bname iocpar.bname
1233 #define iocts iocpar.iocts
1234 #define phone iocpar.phone
1235 #define cfg iocpar.cfg
1237 if (minor == ISDN_MINOR_STATUS) {
1238 switch (cmd) {
1239 case IIOCGETDVR:
1240 return (TTY_DV +
1241 (NET_DV << 8) +
1242 (INF_DV << 16));
1243 case IIOCGETCPS:
1244 if (arg) {
1245 ulong *p = (ulong *) arg;
1246 int i;
1247 if ((ret = verify_area(VERIFY_WRITE, (void *) arg,
1248 sizeof(ulong) * ISDN_MAX_CHANNELS * 2)))
1249 return ret;
1250 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1251 put_user(dev->ibytes[i], p++);
1252 put_user(dev->obytes[i], p++);
1254 return 0;
1255 } else
1256 return -EINVAL;
1257 break;
1258 #ifdef CONFIG_NETDEVICES
1259 case IIOCNETGPN:
1260 /* Get peer phone number of a connected
1261 * isdn network interface */
1262 if (arg) {
1263 if (copy_from_user((char *) &phone, (char *) arg, sizeof(phone)))
1264 return -EFAULT;
1265 return isdn_net_getpeer(&phone, (isdn_net_ioctl_phone *) arg);
1266 } else
1267 return -EINVAL;
1268 #endif
1269 default:
1270 return -EINVAL;
1273 if (!dev->drivers)
1274 return -ENODEV;
1275 if (minor < ISDN_MINOR_CTRL) {
1276 drvidx = isdn_minor2drv(minor);
1277 if (drvidx < 0)
1278 return -ENODEV;
1279 chidx = isdn_minor2chan(minor);
1280 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1281 return -ENODEV;
1282 return 0;
1284 if (minor <= ISDN_MINOR_CTRLMAX) {
1286 * isdn net devices manage lots of configuration variables as linked lists.
1287 * Those lists must only be manipulated from user space. Some of the ioctl's
1288 * service routines access user space and are not atomic. Therefor, ioctl's
1289 * manipulating the lists and ioctl's sleeping while accessing the lists
1290 * are serialized by means of a semaphore.
1292 switch (cmd) {
1293 case IIOCNETDWRSET:
1294 printk(KERN_INFO "INFO: ISDN_DW_ABC_EXTENSION not enabled\n");
1295 return(-EINVAL);
1296 case IIOCNETLCR:
1297 printk(KERN_INFO "INFO: ISDN_ABC_LCR_SUPPORT not enabled\n");
1298 return -ENODEV;
1299 #ifdef CONFIG_NETDEVICES
1300 case IIOCNETAIF:
1301 /* Add a network-interface */
1302 if (arg) {
1303 if (copy_from_user(name, (char *) arg, sizeof(name)))
1304 return -EFAULT;
1305 s = name;
1306 } else {
1307 s = NULL;
1309 ret = down_interruptible(&dev->sem);
1310 if( ret ) return ret;
1311 if ((s = isdn_net_new(s, NULL))) {
1312 if (copy_to_user((char *) arg, s, strlen(s) + 1)){
1313 ret = -EFAULT;
1314 } else {
1315 ret = 0;
1317 } else
1318 ret = -ENODEV;
1319 up(&dev->sem);
1320 return ret;
1321 case IIOCNETASL:
1322 /* Add a slave to a network-interface */
1323 if (arg) {
1324 if (copy_from_user(bname, (char *) arg, sizeof(bname) - 1))
1325 return -EFAULT;
1326 } else
1327 return -EINVAL;
1328 ret = down_interruptible(&dev->sem);
1329 if( ret ) return ret;
1330 if ((s = isdn_net_newslave(bname))) {
1331 if (copy_to_user((char *) arg, s, strlen(s) + 1)){
1332 ret = -EFAULT;
1333 } else {
1334 ret = 0;
1336 } else
1337 ret = -ENODEV;
1338 up(&dev->sem);
1339 return ret;
1340 case IIOCNETDIF:
1341 /* Delete a network-interface */
1342 if (arg) {
1343 if (copy_from_user(name, (char *) arg, sizeof(name)))
1344 return -EFAULT;
1345 ret = down_interruptible(&dev->sem);
1346 if( ret ) return ret;
1347 ret = isdn_net_rm(name);
1348 up(&dev->sem);
1349 return ret;
1350 } else
1351 return -EINVAL;
1352 case IIOCNETSCF:
1353 /* Set configurable parameters of a network-interface */
1354 if (arg) {
1355 if (copy_from_user((char *) &cfg, (char *) arg, sizeof(cfg)))
1356 return -EFAULT;
1357 return isdn_net_setcfg(&cfg);
1358 } else
1359 return -EINVAL;
1360 case IIOCNETGCF:
1361 /* Get configurable parameters of a network-interface */
1362 if (arg) {
1363 if (copy_from_user((char *) &cfg, (char *) arg, sizeof(cfg)))
1364 return -EFAULT;
1365 if (!(ret = isdn_net_getcfg(&cfg))) {
1366 if (copy_to_user((char *) arg, (char *) &cfg, sizeof(cfg)))
1367 return -EFAULT;
1369 return ret;
1370 } else
1371 return -EINVAL;
1372 case IIOCNETANM:
1373 /* Add a phone-number to a network-interface */
1374 if (arg) {
1375 if (copy_from_user((char *) &phone, (char *) arg, sizeof(phone)))
1376 return -EFAULT;
1377 ret = down_interruptible(&dev->sem);
1378 if( ret ) return ret;
1379 ret = isdn_net_addphone(&phone);
1380 up(&dev->sem);
1381 return ret;
1382 } else
1383 return -EINVAL;
1384 case IIOCNETGNM:
1385 /* Get list of phone-numbers of a network-interface */
1386 if (arg) {
1387 if (copy_from_user((char *) &phone, (char *) arg, sizeof(phone)))
1388 return -EFAULT;
1389 ret = down_interruptible(&dev->sem);
1390 if( ret ) return ret;
1391 ret = isdn_net_getphones(&phone, (char *) arg);
1392 up(&dev->sem);
1393 return ret;
1394 } else
1395 return -EINVAL;
1396 case IIOCNETDNM:
1397 /* Delete a phone-number of a network-interface */
1398 if (arg) {
1399 if (copy_from_user((char *) &phone, (char *) arg, sizeof(phone)))
1400 return -EFAULT;
1401 ret = down_interruptible(&dev->sem);
1402 if( ret ) return ret;
1403 ret = isdn_net_delphone(&phone);
1404 up(&dev->sem);
1405 return ret;
1406 } else
1407 return -EINVAL;
1408 case IIOCNETDIL:
1409 /* Force dialing of a network-interface */
1410 if (arg) {
1411 if (copy_from_user(name, (char *) arg, sizeof(name)))
1412 return -EFAULT;
1413 return isdn_net_force_dial(name);
1414 } else
1415 return -EINVAL;
1416 #ifdef CONFIG_ISDN_PPP
1417 case IIOCNETALN:
1418 if (!arg)
1419 return -EINVAL;
1420 if (copy_from_user(name, (char *) arg, sizeof(name)))
1421 return -EFAULT;
1422 return isdn_ppp_dial_slave(name);
1423 case IIOCNETDLN:
1424 if (!arg)
1425 return -EINVAL;
1426 if (copy_from_user(name, (char *) arg, sizeof(name)))
1427 return -EFAULT;
1428 return isdn_ppp_hangup_slave(name);
1429 #endif
1430 case IIOCNETHUP:
1431 /* Force hangup of a network-interface */
1432 if (!arg)
1433 return -EINVAL;
1434 if (copy_from_user(name, (char *) arg, sizeof(name)))
1435 return -EFAULT;
1436 return isdn_net_force_hangup(name);
1437 break;
1438 #endif /* CONFIG_NETDEVICES */
1439 case IIOCSETVER:
1440 dev->net_verbose = arg;
1441 printk(KERN_INFO "isdn: Verbose-Level is %d\n", dev->net_verbose);
1442 return 0;
1443 case IIOCSETGST:
1444 if (arg)
1445 dev->global_flags |= ISDN_GLOBAL_STOPPED;
1446 else
1447 dev->global_flags &= ~ISDN_GLOBAL_STOPPED;
1448 printk(KERN_INFO "isdn: Global Mode %s\n",
1449 (dev->global_flags & ISDN_GLOBAL_STOPPED) ? "stopped" : "running");
1450 return 0;
1451 case IIOCSETBRJ:
1452 drvidx = -1;
1453 if (arg) {
1454 int i;
1455 char *p;
1456 if (copy_from_user((char *) &iocts, (char *) arg,
1457 sizeof(isdn_ioctl_struct)))
1458 return -EFAULT;
1459 if (strlen(iocts.drvid)) {
1460 if ((p = strchr(iocts.drvid, ',')))
1461 *p = 0;
1462 drvidx = -1;
1463 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1464 if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1465 drvidx = i;
1466 break;
1470 if (drvidx == -1)
1471 return -ENODEV;
1472 if (iocts.arg)
1473 dev->drv[drvidx]->flags |= DRV_FLAG_REJBUS;
1474 else
1475 dev->drv[drvidx]->flags &= ~DRV_FLAG_REJBUS;
1476 return 0;
1477 case IIOCSIGPRF:
1478 dev->profd = current;
1479 return 0;
1480 break;
1481 case IIOCGETPRF:
1482 /* Get all Modem-Profiles */
1483 if (arg) {
1484 char *p = (char *) arg;
1485 int i;
1487 if ((ret = verify_area(VERIFY_WRITE, (void *) arg,
1488 (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
1489 * ISDN_MAX_CHANNELS)))
1490 return ret;
1492 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1493 if (copy_to_user(p, dev->mdm.info[i].emu.profile,
1494 ISDN_MODEM_NUMREG))
1495 return -EFAULT;
1496 p += ISDN_MODEM_NUMREG;
1497 if (copy_to_user(p, dev->mdm.info[i].emu.pmsn, ISDN_MSNLEN))
1498 return -EFAULT;
1499 p += ISDN_MSNLEN;
1500 if (copy_to_user(p, dev->mdm.info[i].emu.plmsn, ISDN_LMSNLEN))
1501 return -EFAULT;
1502 p += ISDN_LMSNLEN;
1504 return (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN) * ISDN_MAX_CHANNELS;
1505 } else
1506 return -EINVAL;
1507 break;
1508 case IIOCSETPRF:
1509 /* Set all Modem-Profiles */
1510 if (arg) {
1511 char *p = (char *) arg;
1512 int i;
1514 if ((ret = verify_area(VERIFY_READ, (void *) arg,
1515 (ISDN_MODEM_NUMREG + ISDN_MSNLEN)
1516 * ISDN_MAX_CHANNELS)))
1517 return ret;
1519 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1520 if (copy_from_user(dev->mdm.info[i].emu.profile, p,
1521 ISDN_MODEM_NUMREG))
1522 return -EFAULT;
1523 p += ISDN_MODEM_NUMREG;
1524 if (copy_from_user(dev->mdm.info[i].emu.pmsn, p, ISDN_MSNLEN))
1525 return -EFAULT;
1526 p += ISDN_MSNLEN;
1528 return 0;
1529 } else
1530 return -EINVAL;
1531 break;
1532 case IIOCSETMAP:
1533 case IIOCGETMAP:
1534 /* Set/Get MSN->EAZ-Mapping for a driver */
1535 if (arg) {
1537 if (copy_from_user((char *) &iocts,
1538 (char *) arg,
1539 sizeof(isdn_ioctl_struct)))
1540 return -EFAULT;
1541 if (strlen(iocts.drvid)) {
1542 drvidx = -1;
1543 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1544 if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1545 drvidx = i;
1546 break;
1548 } else
1549 drvidx = 0;
1550 if (drvidx == -1)
1551 return -ENODEV;
1552 if (cmd == IIOCSETMAP) {
1553 int loop = 1;
1555 p = (char *) iocts.arg;
1556 i = 0;
1557 while (loop) {
1558 int j = 0;
1560 while (1) {
1561 if ((ret = verify_area(VERIFY_READ, p, 1)))
1562 return ret;
1563 get_user(bname[j], p++);
1564 switch (bname[j]) {
1565 case '\0':
1566 loop = 0;
1567 /* Fall through */
1568 case ',':
1569 bname[j] = '\0';
1570 strcpy(dev->drv[drvidx]->msn2eaz[i], bname);
1571 j = ISDN_MSNLEN;
1572 break;
1573 default:
1574 j++;
1576 if (j >= ISDN_MSNLEN)
1577 break;
1579 if (++i > 9)
1580 break;
1582 } else {
1583 p = (char *) iocts.arg;
1584 for (i = 0; i < 10; i++) {
1585 sprintf(bname, "%s%s",
1586 strlen(dev->drv[drvidx]->msn2eaz[i]) ?
1587 dev->drv[drvidx]->msn2eaz[i] : "_",
1588 (i < 9) ? "," : "\0");
1589 if (copy_to_user(p, bname, strlen(bname) + 1))
1590 return -EFAULT;
1591 p += strlen(bname);
1594 return 0;
1595 } else
1596 return -EINVAL;
1597 case IIOCDBGVAR:
1598 if (arg) {
1599 if (copy_to_user((char *) arg, (char *) &dev, sizeof(ulong)))
1600 return -EFAULT;
1601 return 0;
1602 } else
1603 return -EINVAL;
1604 break;
1605 default:
1606 if ((cmd & IIOCDRVCTL) == IIOCDRVCTL)
1607 cmd = ((cmd >> _IOC_NRSHIFT) & _IOC_NRMASK) & ISDN_DRVIOCTL_MASK;
1608 else
1609 return -EINVAL;
1610 if (arg) {
1611 int i;
1612 char *p;
1613 if (copy_from_user((char *) &iocts, (char *) arg, sizeof(isdn_ioctl_struct)))
1614 return -EFAULT;
1615 if (strlen(iocts.drvid)) {
1616 if ((p = strchr(iocts.drvid, ',')))
1617 *p = 0;
1618 drvidx = -1;
1619 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1620 if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1621 drvidx = i;
1622 break;
1624 } else
1625 drvidx = 0;
1626 if (drvidx == -1)
1627 return -ENODEV;
1628 if ((ret = verify_area(VERIFY_WRITE, (void *) arg,
1629 sizeof(isdn_ioctl_struct))))
1630 return ret;
1631 c.driver = drvidx;
1632 c.command = ISDN_CMD_IOCTL;
1633 c.arg = cmd;
1634 memcpy(c.parm.num, (char *) &iocts.arg, sizeof(ulong));
1635 ret = isdn_command(&c);
1636 memcpy((char *) &iocts.arg, c.parm.num, sizeof(ulong));
1637 if (copy_to_user((char *) arg, &iocts, sizeof(isdn_ioctl_struct)))
1638 return -EFAULT;
1639 return ret;
1640 } else
1641 return -EINVAL;
1644 #ifdef CONFIG_ISDN_PPP
1645 if (minor <= ISDN_MINOR_PPPMAX)
1646 return (isdn_ppp_ioctl(minor - ISDN_MINOR_PPP, file, cmd, arg));
1647 #endif
1648 return -ENODEV;
1650 #undef name
1651 #undef bname
1652 #undef iocts
1653 #undef phone
1654 #undef cfg
1658 * Open the device code.
1660 static int
1661 isdn_open(struct inode *ino, struct file *filep)
1663 uint minor = MINOR(ino->i_rdev);
1664 int drvidx;
1665 int chidx;
1666 int retval = -ENODEV;
1669 if (minor == ISDN_MINOR_STATUS) {
1670 infostruct *p;
1672 if ((p = (infostruct *) kmalloc(sizeof(infostruct), GFP_KERNEL))) {
1673 p->next = (char *) dev->infochain;
1674 p->private = (char *) &(filep->private_data);
1675 dev->infochain = p;
1676 /* At opening we allow a single update */
1677 filep->private_data = (char *) 1;
1678 retval = 0;
1679 goto out;
1680 } else {
1681 retval = -ENOMEM;
1682 goto out;
1685 if (!dev->channels)
1686 goto out;
1687 if (minor < ISDN_MINOR_CTRL) {
1688 printk(KERN_WARNING "isdn_open minor %d obsolete!\n", minor);
1689 drvidx = isdn_minor2drv(minor);
1690 if (drvidx < 0)
1691 goto out;
1692 chidx = isdn_minor2chan(minor);
1693 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1694 goto out;
1695 if (!(dev->drv[drvidx]->online & (1 << chidx)))
1696 goto out;
1697 isdn_lock_drivers();
1698 retval = 0;
1699 goto out;
1701 if (minor <= ISDN_MINOR_CTRLMAX) {
1702 drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1703 if (drvidx < 0)
1704 goto out;
1705 isdn_lock_drivers();
1706 retval = 0;
1707 goto out;
1709 #ifdef CONFIG_ISDN_PPP
1710 if (minor <= ISDN_MINOR_PPPMAX) {
1711 retval = isdn_ppp_open(minor - ISDN_MINOR_PPP, filep);
1712 if (retval == 0)
1713 isdn_lock_drivers();
1714 goto out;
1716 #endif
1717 out:
1718 return retval;
1721 static int
1722 isdn_close(struct inode *ino, struct file *filep)
1724 uint minor = MINOR(ino->i_rdev);
1726 lock_kernel();
1727 if (minor == ISDN_MINOR_STATUS) {
1728 infostruct *p = dev->infochain;
1729 infostruct *q = NULL;
1731 while (p) {
1732 if (p->private == (char *) &(filep->private_data)) {
1733 if (q)
1734 q->next = p->next;
1735 else
1736 dev->infochain = (infostruct *) (p->next);
1737 kfree(p);
1738 goto out;
1740 q = p;
1741 p = (infostruct *) (p->next);
1743 printk(KERN_WARNING "isdn: No private data while closing isdnctrl\n");
1744 goto out;
1746 isdn_unlock_drivers();
1747 if (minor < ISDN_MINOR_CTRL)
1748 goto out;
1749 if (minor <= ISDN_MINOR_CTRLMAX) {
1750 if (dev->profd == current)
1751 dev->profd = NULL;
1752 goto out;
1754 #ifdef CONFIG_ISDN_PPP
1755 if (minor <= ISDN_MINOR_PPPMAX)
1756 isdn_ppp_release(minor - ISDN_MINOR_PPP, filep);
1757 #endif
1759 out:
1760 unlock_kernel();
1761 return 0;
1764 static struct file_operations isdn_fops =
1766 owner: THIS_MODULE,
1767 llseek: isdn_llseek,
1768 read: isdn_read,
1769 write: isdn_write,
1770 poll: isdn_poll,
1771 ioctl: isdn_ioctl,
1772 open: isdn_open,
1773 release: isdn_close,
1776 char *
1777 isdn_map_eaz2msn(char *msn, int di)
1779 driver *this = dev->drv[di];
1780 int i;
1782 if (strlen(msn) == 1) {
1783 i = msn[0] - '0';
1784 if ((i >= 0) && (i <= 9))
1785 if (strlen(this->msn2eaz[i]))
1786 return (this->msn2eaz[i]);
1788 return (msn);
1792 * Find an unused ISDN-channel, whose feature-flags match the
1793 * given L2- and L3-protocols.
1795 #define L2V (~(ISDN_FEATURE_L2_V11096|ISDN_FEATURE_L2_V11019|ISDN_FEATURE_L2_V11038))
1798 isdn_get_free_channel(int usage, int l2_proto, int l3_proto, int pre_dev
1799 ,int pre_chan, char *msn)
1801 int i;
1802 ulong flags;
1803 ulong features;
1804 ulong vfeatures;
1806 save_flags(flags);
1807 cli();
1808 features = ((1 << l2_proto) | (0x10000 << l3_proto));
1809 vfeatures = (((1 << l2_proto) | (0x10000 << l3_proto)) &
1810 ~(ISDN_FEATURE_L2_V11096|ISDN_FEATURE_L2_V11019|ISDN_FEATURE_L2_V11038));
1811 /* If Layer-2 protocol is V.110, accept drivers with
1812 * transparent feature even if these don't support V.110
1813 * because we can emulate this in linklevel.
1815 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1816 if (USG_NONE(dev->usage[i]) &&
1817 (dev->drvmap[i] != -1)) {
1818 int d = dev->drvmap[i];
1819 if ((dev->usage[i] & ISDN_USAGE_EXCLUSIVE) &&
1820 ((pre_dev != d) || (pre_chan != dev->chanmap[i])))
1821 continue;
1822 if (!strcmp(isdn_map_eaz2msn(msn, d), "-"))
1823 continue;
1824 if (dev->usage[i] & ISDN_USAGE_DISABLED)
1825 continue; /* usage not allowed */
1826 if (dev->drv[d]->flags & DRV_FLAG_RUNNING) {
1827 if (((dev->drv[d]->interface->features & features) == features) ||
1828 (((dev->drv[d]->interface->features & vfeatures) == vfeatures) &&
1829 (dev->drv[d]->interface->features & ISDN_FEATURE_L2_TRANS))) {
1830 if ((pre_dev < 0) || (pre_chan < 0)) {
1831 dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1832 dev->usage[i] |= usage;
1833 isdn_info_update();
1834 restore_flags(flags);
1835 return i;
1836 } else {
1837 if ((pre_dev == d) && (pre_chan == dev->chanmap[i])) {
1838 dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1839 dev->usage[i] |= usage;
1840 isdn_info_update();
1841 restore_flags(flags);
1842 return i;
1848 restore_flags(flags);
1849 return -1;
1853 * Set state of ISDN-channel to 'unused'
1855 void
1856 isdn_free_channel(int di, int ch, int usage)
1858 int i;
1859 ulong flags;
1861 save_flags(flags);
1862 cli();
1863 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1864 if (((!usage) || ((dev->usage[i] & ISDN_USAGE_MASK) == usage)) &&
1865 (dev->drvmap[i] == di) &&
1866 (dev->chanmap[i] == ch)) {
1867 dev->usage[i] &= (ISDN_USAGE_NONE | ISDN_USAGE_EXCLUSIVE);
1868 strcpy(dev->num[i], "???");
1869 dev->ibytes[i] = 0;
1870 dev->obytes[i] = 0;
1871 // 20.10.99 JIM, try to reinitialize v110 !
1872 dev->v110emu[i] = 0;
1873 atomic_set(&(dev->v110use[i]), 0);
1874 isdn_v110_close(dev->v110[i]);
1875 dev->v110[i] = NULL;
1876 // 20.10.99 JIM, try to reinitialize v110 !
1877 isdn_info_update();
1878 isdn_free_queue(&dev->drv[di]->rpqueue[ch]);
1880 restore_flags(flags);
1884 * Cancel Exclusive-Flag for ISDN-channel
1886 void
1887 isdn_unexclusive_channel(int di, int ch)
1889 int i;
1890 ulong flags;
1892 save_flags(flags);
1893 cli();
1894 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1895 if ((dev->drvmap[i] == di) &&
1896 (dev->chanmap[i] == ch)) {
1897 dev->usage[i] &= ~ISDN_USAGE_EXCLUSIVE;
1898 isdn_info_update();
1899 restore_flags(flags);
1900 return;
1902 restore_flags(flags);
1906 * writebuf replacement for SKB_ABLE drivers
1908 static int
1909 isdn_writebuf_stub(int drvidx, int chan, const u_char * buf, int len,
1910 int user)
1912 int ret;
1913 int hl = dev->drv[drvidx]->interface->hl_hdrlen;
1914 struct sk_buff *skb = alloc_skb(hl + len, GFP_ATOMIC);
1916 if (!skb)
1917 return 0;
1918 skb_reserve(skb, hl);
1919 if (user)
1920 copy_from_user(skb_put(skb, len), buf, len);
1921 else
1922 memcpy(skb_put(skb, len), buf, len);
1923 ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, 1, skb);
1924 if (ret <= 0)
1925 dev_kfree_skb(skb);
1926 if (ret > 0)
1927 dev->obytes[isdn_dc2minor(drvidx, chan)] += ret;
1928 return ret;
1932 * Return: length of data on success, -ERRcode on failure.
1935 isdn_writebuf_skb_stub(int drvidx, int chan, int ack, struct sk_buff *skb)
1937 int ret;
1938 struct sk_buff *nskb = NULL;
1939 int v110_ret = skb->len;
1940 int idx = isdn_dc2minor(drvidx, chan);
1942 if (dev->v110[idx]) {
1943 atomic_inc(&dev->v110use[idx]);
1944 nskb = isdn_v110_encode(dev->v110[idx], skb);
1945 atomic_dec(&dev->v110use[idx]);
1946 if (!nskb)
1947 return 0;
1948 v110_ret = *((int *)nskb->data);
1949 skb_pull(nskb, sizeof(int));
1950 if (!nskb->len) {
1951 dev_kfree_skb(nskb);
1952 return v110_ret;
1954 /* V.110 must always be acknowledged */
1955 ack = 1;
1956 ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, nskb);
1957 } else {
1958 int hl = dev->drv[drvidx]->interface->hl_hdrlen;
1960 if( skb_headroom(skb) < hl ){
1962 * This should only occur when new HL driver with
1963 * increased hl_hdrlen was loaded after netdevice
1964 * was created and connected to the new driver.
1966 * The V.110 branch (re-allocates on its own) does
1967 * not need this
1969 struct sk_buff * skb_tmp;
1971 skb_tmp = skb_realloc_headroom(skb, hl);
1972 printk(KERN_DEBUG "isdn_writebuf_skb_stub: reallocating headroom%s\n", skb_tmp ? "" : " failed");
1973 if (!skb_tmp) return -ENOMEM; /* 0 better? */
1974 ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb_tmp);
1975 if( ret > 0 ){
1976 dev_kfree_skb(skb);
1977 } else {
1978 dev_kfree_skb(skb_tmp);
1980 } else {
1981 ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb);
1984 if (ret > 0) {
1985 dev->obytes[idx] += ret;
1986 if (dev->v110[idx]) {
1987 atomic_inc(&dev->v110use[idx]);
1988 dev->v110[idx]->skbuser++;
1989 atomic_dec(&dev->v110use[idx]);
1990 /* For V.110 return unencoded data length */
1991 ret = v110_ret;
1992 /* if the complete frame was send we free the skb;
1993 if not upper function will requeue the skb */
1994 if (ret == skb->len)
1995 dev_kfree_skb(skb);
1997 } else
1998 if (dev->v110[idx])
1999 dev_kfree_skb(nskb);
2000 return ret;
2004 register_isdn_module(isdn_module *m) {
2005 return 0;
2009 unregister_isdn_module(isdn_module *m) {
2010 return 0;
2014 isdn_add_channels(driver *d, int drvidx, int n, int adding)
2016 int j, k, m;
2017 ulong flags;
2019 init_waitqueue_head(&d->st_waitq);
2020 if (d->flags & DRV_FLAG_RUNNING)
2021 return -1;
2022 if (n < 1) return 0;
2024 m = (adding) ? d->channels + n : n;
2026 if (dev->channels + n > ISDN_MAX_CHANNELS) {
2027 printk(KERN_WARNING "register_isdn: Max. %d channels supported\n",
2028 ISDN_MAX_CHANNELS);
2029 return -1;
2032 if ((adding) && (d->rcverr))
2033 kfree(d->rcverr);
2034 if (!(d->rcverr = (int *) kmalloc(sizeof(int) * m, GFP_KERNEL))) {
2035 printk(KERN_WARNING "register_isdn: Could not alloc rcverr\n");
2036 return -1;
2038 memset((char *) d->rcverr, 0, sizeof(int) * m);
2040 if ((adding) && (d->rcvcount))
2041 kfree(d->rcvcount);
2042 if (!(d->rcvcount = (int *) kmalloc(sizeof(int) * m, GFP_KERNEL))) {
2043 printk(KERN_WARNING "register_isdn: Could not alloc rcvcount\n");
2044 if (!adding) kfree(d->rcverr);
2045 return -1;
2047 memset((char *) d->rcvcount, 0, sizeof(int) * m);
2049 if ((adding) && (d->rpqueue)) {
2050 for (j = 0; j < d->channels; j++)
2051 isdn_free_queue(&d->rpqueue[j]);
2052 kfree(d->rpqueue);
2054 if (!(d->rpqueue =
2055 (struct sk_buff_head *) kmalloc(sizeof(struct sk_buff_head) * m, GFP_KERNEL))) {
2056 printk(KERN_WARNING "register_isdn: Could not alloc rpqueue\n");
2057 if (!adding) {
2058 kfree(d->rcvcount);
2059 kfree(d->rcverr);
2061 return -1;
2063 for (j = 0; j < m; j++) {
2064 skb_queue_head_init(&d->rpqueue[j]);
2067 if ((adding) && (d->rcv_waitq))
2068 kfree(d->rcv_waitq);
2069 d->rcv_waitq = (wait_queue_head_t *)
2070 kmalloc(sizeof(wait_queue_head_t) * 2 * m, GFP_KERNEL);
2071 if (!d->rcv_waitq) {
2072 printk(KERN_WARNING "register_isdn: Could not alloc rcv_waitq\n");
2073 if (!adding) {
2074 kfree(d->rpqueue);
2075 kfree(d->rcvcount);
2076 kfree(d->rcverr);
2078 return -1;
2080 d->snd_waitq = d->rcv_waitq + m;
2081 for (j = 0; j < m; j++) {
2082 init_waitqueue_head(&d->rcv_waitq[j]);
2083 init_waitqueue_head(&d->snd_waitq[j]);
2086 dev->channels += n;
2087 save_flags(flags);
2088 cli();
2089 for (j = d->channels; j < m; j++)
2090 for (k = 0; k < ISDN_MAX_CHANNELS; k++)
2091 if (dev->chanmap[k] < 0) {
2092 dev->chanmap[k] = j;
2093 dev->drvmap[k] = drvidx;
2094 isdn_register_devfs(k);
2095 break;
2097 restore_flags(flags);
2098 d->channels = m;
2099 return 0;
2103 * Low-level-driver registration
2106 static void
2107 set_global_features(void)
2109 int drvidx;
2111 dev->global_features = 0;
2112 for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++) {
2113 if (!dev->drv[drvidx])
2114 continue;
2115 if (dev->drv[drvidx]->interface)
2116 dev->global_features |= dev->drv[drvidx]->interface->features;
2120 #ifdef CONFIG_ISDN_DIVERSION
2121 extern isdn_divert_if *divert_if;
2123 static char *map_drvname(int di)
2125 if ((di < 0) || (di >= ISDN_MAX_DRIVERS))
2126 return(NULL);
2127 return(dev->drvid[di]); /* driver name */
2128 } /* map_drvname */
2130 static int map_namedrv(char *id)
2131 { int i;
2133 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
2134 { if (!strcmp(dev->drvid[i],id))
2135 return(i);
2137 return(-1);
2138 } /* map_namedrv */
2140 int DIVERT_REG_NAME(isdn_divert_if *i_div)
2142 if (i_div->if_magic != DIVERT_IF_MAGIC)
2143 return(DIVERT_VER_ERR);
2144 switch (i_div->cmd)
2146 case DIVERT_CMD_REL:
2147 if (divert_if != i_div)
2148 return(DIVERT_REL_ERR);
2149 divert_if = NULL; /* free interface */
2150 MOD_DEC_USE_COUNT;
2151 return(DIVERT_NO_ERR);
2153 case DIVERT_CMD_REG:
2154 if (divert_if)
2155 return(DIVERT_REG_ERR);
2156 i_div->ll_cmd = isdn_command; /* set command function */
2157 i_div->drv_to_name = map_drvname;
2158 i_div->name_to_drv = map_namedrv;
2159 MOD_INC_USE_COUNT;
2160 divert_if = i_div; /* remember interface */
2161 return(DIVERT_NO_ERR);
2163 default:
2164 return(DIVERT_CMD_ERR);
2166 } /* DIVERT_REG_NAME */
2168 EXPORT_SYMBOL(DIVERT_REG_NAME);
2170 #endif CONFIG_ISDN_DIVERSION
2173 EXPORT_SYMBOL(register_isdn);
2174 EXPORT_SYMBOL(register_isdn_module);
2175 EXPORT_SYMBOL(unregister_isdn_module);
2176 #ifdef CONFIG_ISDN_PPP
2177 EXPORT_SYMBOL(isdn_ppp_register_compressor);
2178 EXPORT_SYMBOL(isdn_ppp_unregister_compressor);
2179 #endif
2182 register_isdn(isdn_if * i)
2184 driver *d;
2185 int j;
2186 ulong flags;
2187 int drvidx;
2189 if (dev->drivers >= ISDN_MAX_DRIVERS) {
2190 printk(KERN_WARNING "register_isdn: Max. %d drivers supported\n",
2191 ISDN_MAX_DRIVERS);
2192 return 0;
2194 if (!i->writebuf_skb) {
2195 printk(KERN_WARNING "register_isdn: No write routine given.\n");
2196 return 0;
2198 if (!(d = (driver *) kmalloc(sizeof(driver), GFP_KERNEL))) {
2199 printk(KERN_WARNING "register_isdn: Could not alloc driver-struct\n");
2200 return 0;
2202 memset((char *) d, 0, sizeof(driver));
2204 d->maxbufsize = i->maxbufsize;
2205 d->pktcount = 0;
2206 d->stavail = 0;
2207 d->flags = DRV_FLAG_LOADED;
2208 d->online = 0;
2209 d->interface = i;
2210 d->channels = 0;
2211 for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++)
2212 if (!dev->drv[drvidx])
2213 break;
2214 if (isdn_add_channels(d, drvidx, i->channels, 0)) {
2215 kfree(d);
2216 return 0;
2218 i->channels = drvidx;
2219 i->rcvcallb_skb = isdn_receive_skb_callback;
2220 i->statcallb = isdn_status_callback;
2221 if (!strlen(i->id))
2222 sprintf(i->id, "line%d", drvidx);
2223 save_flags(flags);
2224 cli();
2225 for (j = 0; j < drvidx; j++)
2226 if (!strcmp(i->id, dev->drvid[j]))
2227 sprintf(i->id, "line%d", drvidx);
2228 dev->drv[drvidx] = d;
2229 strcpy(dev->drvid[drvidx], i->id);
2230 isdn_info_update();
2231 dev->drivers++;
2232 set_global_features();
2233 restore_flags(flags);
2234 return 1;
2238 *****************************************************************************
2239 * And now the modules code.
2240 *****************************************************************************
2243 extern int printk(const char *fmt,...);
2245 #ifdef MODULE
2246 #define isdn_init init_module
2247 #endif
2249 static char *
2250 isdn_getrev(const char *revision)
2252 char *rev;
2253 char *p;
2255 if ((p = strchr(revision, ':'))) {
2256 rev = p + 2;
2257 p = strchr(rev, '$');
2258 *--p = 0;
2259 } else
2260 rev = "???";
2261 return rev;
2264 #ifdef CONFIG_DEVFS_FS
2266 static devfs_handle_t devfs_handle;
2268 static void isdn_register_devfs(int k)
2270 char buf[11];
2272 sprintf (buf, "isdn%d", k);
2273 dev->devfs_handle_isdnX[k] =
2274 devfs_register (devfs_handle, buf, DEVFS_FL_DEFAULT,
2275 ISDN_MAJOR, ISDN_MINOR_B + k,0600 | S_IFCHR,
2276 &isdn_fops, NULL);
2277 sprintf (buf, "isdnctrl%d", k);
2278 dev->devfs_handle_isdnctrlX[k] =
2279 devfs_register (devfs_handle, buf, DEVFS_FL_DEFAULT,
2280 ISDN_MAJOR, ISDN_MINOR_CTRL + k, 0600 | S_IFCHR,
2281 &isdn_fops, NULL);
2284 static void isdn_unregister_devfs(int k)
2286 devfs_unregister (dev->devfs_handle_isdnX[k]);
2287 devfs_unregister (dev->devfs_handle_isdnctrlX[k]);
2290 static void isdn_init_devfs(void)
2292 # ifdef CONFIG_ISDN_PPP
2293 int i;
2294 # endif
2296 devfs_handle = devfs_mk_dir (NULL, "isdn", NULL);
2297 # ifdef CONFIG_ISDN_PPP
2298 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2299 char buf[8];
2301 sprintf (buf, "ippp%d", i);
2302 dev->devfs_handle_ipppX[i] =
2303 devfs_register (devfs_handle, buf, DEVFS_FL_DEFAULT,
2304 ISDN_MAJOR, ISDN_MINOR_PPP + i,
2305 0600 | S_IFCHR, &isdn_fops, NULL);
2307 # endif
2309 dev->devfs_handle_isdninfo =
2310 devfs_register (devfs_handle, "isdninfo", DEVFS_FL_DEFAULT,
2311 ISDN_MAJOR, ISDN_MINOR_STATUS, 0600 | S_IFCHR,
2312 &isdn_fops, NULL);
2313 dev->devfs_handle_isdnctrl =
2314 devfs_register (devfs_handle, "isdnctrl", DEVFS_FL_DEFAULT,
2315 ISDN_MAJOR, ISDN_MINOR_CTRL, 0600 | S_IFCHR,
2316 &isdn_fops, NULL);
2319 static void isdn_cleanup_devfs(void)
2321 # ifdef CONFIG_ISDN_PPP
2322 int i;
2323 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
2324 devfs_unregister (dev->devfs_handle_ipppX[i]);
2325 # endif
2326 devfs_unregister (dev->devfs_handle_isdninfo);
2327 devfs_unregister (dev->devfs_handle_isdnctrl);
2328 devfs_unregister (devfs_handle);
2331 #else /* CONFIG_DEVFS_FS */
2332 static void isdn_register_devfs(int dummy)
2334 return;
2337 static void isdn_unregister_devfs(int dummy)
2339 return;
2342 static void isdn_init_devfs(void)
2344 return;
2347 static void isdn_cleanup_devfs(void)
2349 return;
2352 #endif /* CONFIG_DEVFS_FS */
2355 * Allocate and initialize all data, register modem-devices
2358 isdn_init(void)
2360 int i;
2361 char tmprev[50];
2363 if (!(dev = (isdn_dev *) vmalloc(sizeof(isdn_dev)))) {
2364 printk(KERN_WARNING "isdn: Could not allocate device-struct.\n");
2365 return -EIO;
2367 memset((char *) dev, 0, sizeof(isdn_dev));
2368 init_timer(&dev->timer);
2369 dev->timer.function = isdn_timer_funct;
2370 init_MUTEX(&dev->sem);
2371 init_waitqueue_head(&dev->info_waitq);
2372 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2373 dev->drvmap[i] = -1;
2374 dev->chanmap[i] = -1;
2375 dev->m_idx[i] = -1;
2376 strcpy(dev->num[i], "???");
2377 init_waitqueue_head(&dev->mdm.info[i].open_wait);
2378 init_waitqueue_head(&dev->mdm.info[i].close_wait);
2380 if (devfs_register_chrdev(ISDN_MAJOR, "isdn", &isdn_fops)) {
2381 printk(KERN_WARNING "isdn: Could not register control devices\n");
2382 vfree(dev);
2383 return -EIO;
2385 isdn_init_devfs();
2386 if ((i = isdn_tty_modem_init()) < 0) {
2387 printk(KERN_WARNING "isdn: Could not register tty devices\n");
2388 if (i == -3)
2389 tty_unregister_driver(&dev->mdm.cua_modem);
2390 if (i <= -2)
2391 tty_unregister_driver(&dev->mdm.tty_modem);
2392 vfree(dev);
2393 isdn_cleanup_devfs();
2394 devfs_unregister_chrdev(ISDN_MAJOR, "isdn");
2395 return -EIO;
2397 #ifdef CONFIG_ISDN_PPP
2398 if (isdn_ppp_init() < 0) {
2399 printk(KERN_WARNING "isdn: Could not create PPP-device-structs\n");
2400 tty_unregister_driver(&dev->mdm.tty_modem);
2401 tty_unregister_driver(&dev->mdm.cua_modem);
2402 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
2403 kfree(dev->mdm.info[i].xmit_buf - 4);
2404 isdn_cleanup_devfs();
2405 devfs_unregister_chrdev(ISDN_MAJOR, "isdn");
2406 vfree(dev);
2407 return -EIO;
2409 #endif /* CONFIG_ISDN_PPP */
2411 strcpy(tmprev, isdn_revision);
2412 printk(KERN_NOTICE "ISDN subsystem Rev: %s/", isdn_getrev(tmprev));
2413 strcpy(tmprev, isdn_tty_revision);
2414 printk("%s/", isdn_getrev(tmprev));
2415 strcpy(tmprev, isdn_net_revision);
2416 printk("%s/", isdn_getrev(tmprev));
2417 strcpy(tmprev, isdn_ppp_revision);
2418 printk("%s/", isdn_getrev(tmprev));
2419 strcpy(tmprev, isdn_audio_revision);
2420 printk("%s/", isdn_getrev(tmprev));
2421 strcpy(tmprev, isdn_v110_revision);
2422 printk("%s", isdn_getrev(tmprev));
2424 #ifdef MODULE
2425 printk(" loaded\n");
2426 #else
2427 printk("\n");
2428 isdn_cards_init();
2429 #endif
2430 isdn_info_update();
2431 return 0;
2434 #ifdef MODULE
2436 * Unload module
2438 void
2439 cleanup_module(void)
2441 int flags;
2442 int i;
2444 #ifdef CONFIG_ISDN_PPP
2445 isdn_ppp_cleanup();
2446 #endif
2447 save_flags(flags);
2448 cli();
2449 if (isdn_net_rmall() < 0) {
2450 printk(KERN_WARNING "isdn: net-device busy, remove cancelled\n");
2451 restore_flags(flags);
2452 return;
2454 if (tty_unregister_driver(&dev->mdm.tty_modem)) {
2455 printk(KERN_WARNING "isdn: ttyI-device busy, remove cancelled\n");
2456 restore_flags(flags);
2457 return;
2459 if (tty_unregister_driver(&dev->mdm.cua_modem)) {
2460 printk(KERN_WARNING "isdn: cui-device busy, remove cancelled\n");
2461 restore_flags(flags);
2462 return;
2464 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2465 isdn_tty_cleanup_xmit(&dev->mdm.info[i]);
2466 kfree(dev->mdm.info[i].xmit_buf - 4);
2467 #ifdef CONFIG_ISDN_TTY_FAX
2468 kfree(dev->mdm.info[i].fax);
2469 #endif
2471 if (devfs_unregister_chrdev(ISDN_MAJOR, "isdn") != 0) {
2472 printk(KERN_WARNING "isdn: controldevice busy, remove cancelled\n");
2473 restore_flags(flags);
2474 } else {
2475 isdn_cleanup_devfs();
2476 del_timer(&dev->timer);
2477 restore_flags(flags);
2478 /* call vfree with interrupts enabled, else it will hang */
2479 vfree(dev);
2480 printk(KERN_NOTICE "ISDN-subsystem unloaded\n");
2483 #endif