Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / isdn / i4l / isdn_common.c
bloba7e3feb581b6eeb26e6e5b2bd5a546e8ce50b42c
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/vmalloc.h>
18 #include <linux/isdn.h>
19 #include <linux/smp_lock.h>
20 #include "isdn_common.h"
21 #include "isdn_tty.h"
22 #include "isdn_net.h"
23 #include "isdn_ppp.h"
24 #ifdef CONFIG_ISDN_AUDIO
25 #include "isdn_audio.h"
26 #endif
27 #ifdef CONFIG_ISDN_DIVERSION_MODULE
28 #define CONFIG_ISDN_DIVERSION
29 #endif
30 #ifdef CONFIG_ISDN_DIVERSION
31 #include <linux/isdn_divertif.h>
32 #endif /* CONFIG_ISDN_DIVERSION */
33 #include "isdn_v110.h"
35 /* Debugflags */
36 #undef ISDN_DEBUG_STATCALLB
38 MODULE_DESCRIPTION("ISDN4Linux: link layer");
39 MODULE_AUTHOR("Fritz Elfert");
40 MODULE_LICENSE("GPL");
42 isdn_dev *dev;
44 static char *isdn_revision = "$Revision: 1.1.2.3 $";
46 extern char *isdn_net_revision;
47 extern char *isdn_tty_revision;
48 #ifdef CONFIG_ISDN_PPP
49 extern char *isdn_ppp_revision;
50 #else
51 static char *isdn_ppp_revision = ": none $";
52 #endif
53 #ifdef CONFIG_ISDN_AUDIO
54 extern char *isdn_audio_revision;
55 #else
56 static char *isdn_audio_revision = ": none $";
57 #endif
58 extern char *isdn_v110_revision;
60 #ifdef CONFIG_ISDN_DIVERSION
61 static isdn_divert_if *divert_if; /* = NULL */
62 #endif /* CONFIG_ISDN_DIVERSION */
65 static int isdn_writebuf_stub(int, int, const u_char __user *, int);
66 static void set_global_features(void);
67 static int isdn_wildmat(char *s, char *p);
68 static int isdn_add_channels(isdn_driver_t *d, int drvidx, int n, int adding);
70 static inline void
71 isdn_lock_driver(isdn_driver_t *drv)
73 try_module_get(drv->interface->owner);
74 drv->locks++;
77 void
78 isdn_lock_drivers(void)
80 int i;
82 for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
83 if (!dev->drv[i])
84 continue;
85 isdn_lock_driver(dev->drv[i]);
89 static inline void
90 isdn_unlock_driver(isdn_driver_t *drv)
92 if (drv->locks > 0) {
93 drv->locks--;
94 module_put(drv->interface->owner);
98 void
99 isdn_unlock_drivers(void)
101 int i;
103 for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
104 if (!dev->drv[i])
105 continue;
106 isdn_unlock_driver(dev->drv[i]);
110 #if defined(ISDN_DEBUG_NET_DUMP) || defined(ISDN_DEBUG_MODEM_DUMP)
111 void
112 isdn_dumppkt(char *s, u_char * p, int len, int dumplen)
114 int dumpc;
116 printk(KERN_DEBUG "%s(%d) ", s, len);
117 for (dumpc = 0; (dumpc < dumplen) && (len); len--, dumpc++)
118 printk(" %02x", *p++);
119 printk("\n");
121 #endif
124 * I picked the pattern-matching-functions from an old GNU-tar version (1.10)
125 * It was originally written and put to PD by rs@mirror.TMC.COM (Rich Salz)
127 static int
128 isdn_star(char *s, char *p)
130 while (isdn_wildmat(s, p)) {
131 if (*++s == '\0')
132 return (2);
134 return (0);
138 * Shell-type Pattern-matching for incoming caller-Ids
139 * This function gets a string in s and checks, if it matches the pattern
140 * given in p.
142 * Return:
143 * 0 = match.
144 * 1 = no match.
145 * 2 = no match. Would eventually match, if s would be longer.
147 * Possible Patterns:
149 * '?' matches one character
150 * '*' matches zero or more characters
151 * [xyz] matches the set of characters in brackets.
152 * [^xyz] matches any single character not in the set of characters
155 static int
156 isdn_wildmat(char *s, char *p)
158 register int last;
159 register int matched;
160 register int reverse;
161 register int nostar = 1;
163 if (!(*s) && !(*p))
164 return(1);
165 for (; *p; s++, p++)
166 switch (*p) {
167 case '\\':
169 * Literal match with following character,
170 * fall through.
172 p++;
173 default:
174 if (*s != *p)
175 return (*s == '\0')?2:1;
176 continue;
177 case '?':
178 /* Match anything. */
179 if (*s == '\0')
180 return (2);
181 continue;
182 case '*':
183 nostar = 0;
184 /* Trailing star matches everything. */
185 return (*++p ? isdn_star(s, p) : 0);
186 case '[':
187 /* [^....] means inverse character class. */
188 if ((reverse = (p[1] == '^')))
189 p++;
190 for (last = 0, matched = 0; *++p && (*p != ']'); last = *p)
191 /* This next line requires a good C compiler. */
192 if (*p == '-' ? *s <= *++p && *s >= last : *s == *p)
193 matched = 1;
194 if (matched == reverse)
195 return (1);
196 continue;
198 return (*s == '\0')?0:nostar;
201 int isdn_msncmp( const char * msn1, const char * msn2 )
203 char TmpMsn1[ ISDN_MSNLEN ];
204 char TmpMsn2[ ISDN_MSNLEN ];
205 char *p;
207 for ( p = TmpMsn1; *msn1 && *msn1 != ':'; ) // Strip off a SPID
208 *p++ = *msn1++;
209 *p = '\0';
211 for ( p = TmpMsn2; *msn2 && *msn2 != ':'; ) // Strip off a SPID
212 *p++ = *msn2++;
213 *p = '\0';
215 return isdn_wildmat( TmpMsn1, TmpMsn2 );
219 isdn_dc2minor(int di, int ch)
221 int i;
222 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
223 if (dev->chanmap[i] == ch && dev->drvmap[i] == di)
224 return i;
225 return -1;
228 static int isdn_timer_cnt1 = 0;
229 static int isdn_timer_cnt2 = 0;
230 static int isdn_timer_cnt3 = 0;
232 static void
233 isdn_timer_funct(ulong dummy)
235 int tf = dev->tflags;
236 if (tf & ISDN_TIMER_FAST) {
237 if (tf & ISDN_TIMER_MODEMREAD)
238 isdn_tty_readmodem();
239 if (tf & ISDN_TIMER_MODEMPLUS)
240 isdn_tty_modem_escape();
241 if (tf & ISDN_TIMER_MODEMXMIT)
242 isdn_tty_modem_xmit();
244 if (tf & ISDN_TIMER_SLOW) {
245 if (++isdn_timer_cnt1 >= ISDN_TIMER_02SEC) {
246 isdn_timer_cnt1 = 0;
247 if (tf & ISDN_TIMER_NETDIAL)
248 isdn_net_dial();
250 if (++isdn_timer_cnt2 >= ISDN_TIMER_1SEC) {
251 isdn_timer_cnt2 = 0;
252 if (tf & ISDN_TIMER_NETHANGUP)
253 isdn_net_autohup();
254 if (++isdn_timer_cnt3 >= ISDN_TIMER_RINGING) {
255 isdn_timer_cnt3 = 0;
256 if (tf & ISDN_TIMER_MODEMRING)
257 isdn_tty_modem_ring();
259 if (tf & ISDN_TIMER_CARRIER)
260 isdn_tty_carrier_timeout();
263 if (tf)
264 mod_timer(&dev->timer, jiffies+ISDN_TIMER_RES);
267 void
268 isdn_timer_ctrl(int tf, int onoff)
270 unsigned long flags;
271 int old_tflags;
273 spin_lock_irqsave(&dev->timerlock, flags);
274 if ((tf & ISDN_TIMER_SLOW) && (!(dev->tflags & ISDN_TIMER_SLOW))) {
275 /* If the slow-timer wasn't activated until now */
276 isdn_timer_cnt1 = 0;
277 isdn_timer_cnt2 = 0;
279 old_tflags = dev->tflags;
280 if (onoff)
281 dev->tflags |= tf;
282 else
283 dev->tflags &= ~tf;
284 if (dev->tflags && !old_tflags)
285 mod_timer(&dev->timer, jiffies+ISDN_TIMER_RES);
286 spin_unlock_irqrestore(&dev->timerlock, flags);
290 * Receive a packet from B-Channel. (Called from low-level-module)
292 static void
293 isdn_receive_skb_callback(int di, int channel, struct sk_buff *skb)
295 int i;
297 if ((i = isdn_dc2minor(di, channel)) == -1) {
298 dev_kfree_skb(skb);
299 return;
301 /* Update statistics */
302 dev->ibytes[i] += skb->len;
304 /* First, try to deliver data to network-device */
305 if (isdn_net_rcv_skb(i, skb))
306 return;
308 /* V.110 handling
309 * makes sense for async streams only, so it is
310 * called after possible net-device delivery.
312 if (dev->v110[i]) {
313 atomic_inc(&dev->v110use[i]);
314 skb = isdn_v110_decode(dev->v110[i], skb);
315 atomic_dec(&dev->v110use[i]);
316 if (!skb)
317 return;
320 /* No network-device found, deliver to tty or raw-channel */
321 if (skb->len) {
322 if (isdn_tty_rcv_skb(i, di, channel, skb))
323 return;
324 wake_up_interruptible(&dev->drv[di]->rcv_waitq[channel]);
325 } else
326 dev_kfree_skb(skb);
330 * Intercept command from Linklevel to Lowlevel.
331 * If layer 2 protocol is V.110 and this is not supported by current
332 * lowlevel-driver, use driver's transparent mode and handle V.110 in
333 * linklevel instead.
336 isdn_command(isdn_ctrl *cmd)
338 if (cmd->driver == -1) {
339 printk(KERN_WARNING "isdn_command command(%x) driver -1\n", cmd->command);
340 return(1);
342 if (!dev->drv[cmd->driver]) {
343 printk(KERN_WARNING "isdn_command command(%x) dev->drv[%d] NULL\n",
344 cmd->command, cmd->driver);
345 return(1);
347 if (!dev->drv[cmd->driver]->interface) {
348 printk(KERN_WARNING "isdn_command command(%x) dev->drv[%d]->interface NULL\n",
349 cmd->command, cmd->driver);
350 return(1);
352 if (cmd->command == ISDN_CMD_SETL2) {
353 int idx = isdn_dc2minor(cmd->driver, cmd->arg & 255);
354 unsigned long l2prot = (cmd->arg >> 8) & 255;
355 unsigned long features = (dev->drv[cmd->driver]->interface->features
356 >> ISDN_FEATURE_L2_SHIFT) &
357 ISDN_FEATURE_L2_MASK;
358 unsigned long l2_feature = (1 << l2prot);
360 switch (l2prot) {
361 case ISDN_PROTO_L2_V11096:
362 case ISDN_PROTO_L2_V11019:
363 case ISDN_PROTO_L2_V11038:
364 /* If V.110 requested, but not supported by
365 * HL-driver, set emulator-flag and change
366 * Layer-2 to transparent
368 if (!(features & l2_feature)) {
369 dev->v110emu[idx] = l2prot;
370 cmd->arg = (cmd->arg & 255) |
371 (ISDN_PROTO_L2_TRANS << 8);
372 } else
373 dev->v110emu[idx] = 0;
376 return dev->drv[cmd->driver]->interface->command(cmd);
379 void
380 isdn_all_eaz(int di, int ch)
382 isdn_ctrl cmd;
384 if (di < 0)
385 return;
386 cmd.driver = di;
387 cmd.arg = ch;
388 cmd.command = ISDN_CMD_SETEAZ;
389 cmd.parm.num[0] = '\0';
390 isdn_command(&cmd);
394 * Begin of a CAPI like LL<->HL interface, currently used only for
395 * supplementary service (CAPI 2.0 part III)
397 #include <linux/isdn/capicmd.h>
399 static int
400 isdn_capi_rec_hl_msg(capi_msg *cm) {
402 int di;
403 int ch;
405 di = (cm->adr.Controller & 0x7f) -1;
406 ch = isdn_dc2minor(di, (cm->adr.Controller>>8)& 0x7f);
407 switch(cm->Command) {
408 case CAPI_FACILITY:
409 /* in the moment only handled in tty */
410 return(isdn_tty_capi_facility(cm));
411 default:
412 return(-1);
416 static int
417 isdn_status_callback(isdn_ctrl * c)
419 int di;
420 u_long flags;
421 int i;
422 int r;
423 int retval = 0;
424 isdn_ctrl cmd;
425 isdn_net_dev *p;
427 di = c->driver;
428 i = isdn_dc2minor(di, c->arg);
429 switch (c->command) {
430 case ISDN_STAT_BSENT:
431 if (i < 0)
432 return -1;
433 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
434 return 0;
435 if (isdn_net_stat_callback(i, c))
436 return 0;
437 if (isdn_v110_stat_callback(i, c))
438 return 0;
439 if (isdn_tty_stat_callback(i, c))
440 return 0;
441 wake_up_interruptible(&dev->drv[di]->snd_waitq[c->arg]);
442 break;
443 case ISDN_STAT_STAVAIL:
444 dev->drv[di]->stavail += c->arg;
445 wake_up_interruptible(&dev->drv[di]->st_waitq);
446 break;
447 case ISDN_STAT_RUN:
448 dev->drv[di]->flags |= DRV_FLAG_RUNNING;
449 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
450 if (dev->drvmap[i] == di)
451 isdn_all_eaz(di, dev->chanmap[i]);
452 set_global_features();
453 break;
454 case ISDN_STAT_STOP:
455 dev->drv[di]->flags &= ~DRV_FLAG_RUNNING;
456 break;
457 case ISDN_STAT_ICALL:
458 if (i < 0)
459 return -1;
460 #ifdef ISDN_DEBUG_STATCALLB
461 printk(KERN_DEBUG "ICALL (net): %d %ld %s\n", di, c->arg, c->parm.num);
462 #endif
463 if (dev->global_flags & ISDN_GLOBAL_STOPPED) {
464 cmd.driver = di;
465 cmd.arg = c->arg;
466 cmd.command = ISDN_CMD_HANGUP;
467 isdn_command(&cmd);
468 return 0;
470 /* Try to find a network-interface which will accept incoming call */
471 r = ((c->command == ISDN_STAT_ICALLW) ? 0 : isdn_net_find_icall(di, c->arg, i, &c->parm.setup));
472 switch (r) {
473 case 0:
474 /* No network-device replies.
475 * Try ttyI's.
476 * These return 0 on no match, 1 on match and
477 * 3 on eventually match, if CID is longer.
479 if (c->command == ISDN_STAT_ICALL)
480 if ((retval = isdn_tty_find_icall(di, c->arg, &c->parm.setup))) return(retval);
481 #ifdef CONFIG_ISDN_DIVERSION
482 if (divert_if)
483 if ((retval = divert_if->stat_callback(c)))
484 return(retval); /* processed */
485 #endif /* CONFIG_ISDN_DIVERSION */
486 if ((!retval) && (dev->drv[di]->flags & DRV_FLAG_REJBUS)) {
487 /* No tty responding */
488 cmd.driver = di;
489 cmd.arg = c->arg;
490 cmd.command = ISDN_CMD_HANGUP;
491 isdn_command(&cmd);
492 retval = 2;
494 break;
495 case 1:
496 /* Schedule connection-setup */
497 isdn_net_dial();
498 cmd.driver = di;
499 cmd.arg = c->arg;
500 cmd.command = ISDN_CMD_ACCEPTD;
501 for ( p = dev->netdev; p; p = p->next )
502 if ( p->local->isdn_channel == cmd.arg )
504 strcpy( cmd.parm.setup.eazmsn, p->local->msn );
505 isdn_command(&cmd);
506 retval = 1;
507 break;
509 break;
511 case 2: /* For calling back, first reject incoming call ... */
512 case 3: /* Interface found, but down, reject call actively */
513 retval = 2;
514 printk(KERN_INFO "isdn: Rejecting Call\n");
515 cmd.driver = di;
516 cmd.arg = c->arg;
517 cmd.command = ISDN_CMD_HANGUP;
518 isdn_command(&cmd);
519 if (r == 3)
520 break;
521 /* Fall through */
522 case 4:
523 /* ... then start callback. */
524 isdn_net_dial();
525 break;
526 case 5:
527 /* Number would eventually match, if longer */
528 retval = 3;
529 break;
531 #ifdef ISDN_DEBUG_STATCALLB
532 printk(KERN_DEBUG "ICALL: ret=%d\n", retval);
533 #endif
534 return retval;
535 break;
536 case ISDN_STAT_CINF:
537 if (i < 0)
538 return -1;
539 #ifdef ISDN_DEBUG_STATCALLB
540 printk(KERN_DEBUG "CINF: %ld %s\n", c->arg, c->parm.num);
541 #endif
542 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
543 return 0;
544 if (strcmp(c->parm.num, "0"))
545 isdn_net_stat_callback(i, c);
546 isdn_tty_stat_callback(i, c);
547 break;
548 case ISDN_STAT_CAUSE:
549 #ifdef ISDN_DEBUG_STATCALLB
550 printk(KERN_DEBUG "CAUSE: %ld %s\n", c->arg, c->parm.num);
551 #endif
552 printk(KERN_INFO "isdn: %s,ch%ld cause: %s\n",
553 dev->drvid[di], c->arg, c->parm.num);
554 isdn_tty_stat_callback(i, c);
555 #ifdef CONFIG_ISDN_DIVERSION
556 if (divert_if)
557 divert_if->stat_callback(c);
558 #endif /* CONFIG_ISDN_DIVERSION */
559 break;
560 case ISDN_STAT_DISPLAY:
561 #ifdef ISDN_DEBUG_STATCALLB
562 printk(KERN_DEBUG "DISPLAY: %ld %s\n", c->arg, c->parm.display);
563 #endif
564 isdn_tty_stat_callback(i, c);
565 #ifdef CONFIG_ISDN_DIVERSION
566 if (divert_if)
567 divert_if->stat_callback(c);
568 #endif /* CONFIG_ISDN_DIVERSION */
569 break;
570 case ISDN_STAT_DCONN:
571 if (i < 0)
572 return -1;
573 #ifdef ISDN_DEBUG_STATCALLB
574 printk(KERN_DEBUG "DCONN: %ld\n", c->arg);
575 #endif
576 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
577 return 0;
578 /* Find any net-device, waiting for D-channel setup */
579 if (isdn_net_stat_callback(i, c))
580 break;
581 isdn_v110_stat_callback(i, c);
582 /* Find any ttyI, waiting for D-channel setup */
583 if (isdn_tty_stat_callback(i, c)) {
584 cmd.driver = di;
585 cmd.arg = c->arg;
586 cmd.command = ISDN_CMD_ACCEPTB;
587 isdn_command(&cmd);
588 break;
590 break;
591 case ISDN_STAT_DHUP:
592 if (i < 0)
593 return -1;
594 #ifdef ISDN_DEBUG_STATCALLB
595 printk(KERN_DEBUG "DHUP: %ld\n", c->arg);
596 #endif
597 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
598 return 0;
599 dev->drv[di]->online &= ~(1 << (c->arg));
600 isdn_info_update();
601 /* Signal hangup to network-devices */
602 if (isdn_net_stat_callback(i, c))
603 break;
604 isdn_v110_stat_callback(i, c);
605 if (isdn_tty_stat_callback(i, c))
606 break;
607 #ifdef CONFIG_ISDN_DIVERSION
608 if (divert_if)
609 divert_if->stat_callback(c);
610 #endif /* CONFIG_ISDN_DIVERSION */
611 break;
612 break;
613 case ISDN_STAT_BCONN:
614 if (i < 0)
615 return -1;
616 #ifdef ISDN_DEBUG_STATCALLB
617 printk(KERN_DEBUG "BCONN: %ld\n", c->arg);
618 #endif
619 /* Signal B-channel-connect to network-devices */
620 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
621 return 0;
622 dev->drv[di]->online |= (1 << (c->arg));
623 isdn_info_update();
624 if (isdn_net_stat_callback(i, c))
625 break;
626 isdn_v110_stat_callback(i, c);
627 if (isdn_tty_stat_callback(i, c))
628 break;
629 break;
630 case ISDN_STAT_BHUP:
631 if (i < 0)
632 return -1;
633 #ifdef ISDN_DEBUG_STATCALLB
634 printk(KERN_DEBUG "BHUP: %ld\n", c->arg);
635 #endif
636 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
637 return 0;
638 dev->drv[di]->online &= ~(1 << (c->arg));
639 isdn_info_update();
640 #ifdef CONFIG_ISDN_X25
641 /* Signal hangup to network-devices */
642 if (isdn_net_stat_callback(i, c))
643 break;
644 #endif
645 isdn_v110_stat_callback(i, c);
646 if (isdn_tty_stat_callback(i, c))
647 break;
648 break;
649 case ISDN_STAT_NODCH:
650 if (i < 0)
651 return -1;
652 #ifdef ISDN_DEBUG_STATCALLB
653 printk(KERN_DEBUG "NODCH: %ld\n", c->arg);
654 #endif
655 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
656 return 0;
657 if (isdn_net_stat_callback(i, c))
658 break;
659 if (isdn_tty_stat_callback(i, c))
660 break;
661 break;
662 case ISDN_STAT_ADDCH:
663 spin_lock_irqsave(&dev->lock, flags);
664 if (isdn_add_channels(dev->drv[di], di, c->arg, 1)) {
665 spin_unlock_irqrestore(&dev->lock, flags);
666 return -1;
668 spin_unlock_irqrestore(&dev->lock, flags);
669 isdn_info_update();
670 break;
671 case ISDN_STAT_DISCH:
672 spin_lock_irqsave(&dev->lock, flags);
673 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
674 if ((dev->drvmap[i] == di) &&
675 (dev->chanmap[i] == c->arg)) {
676 if (c->parm.num[0])
677 dev->usage[i] &= ~ISDN_USAGE_DISABLED;
678 else
679 if (USG_NONE(dev->usage[i])) {
680 dev->usage[i] |= ISDN_USAGE_DISABLED;
682 else
683 retval = -1;
684 break;
686 spin_unlock_irqrestore(&dev->lock, flags);
687 isdn_info_update();
688 break;
689 case ISDN_STAT_UNLOAD:
690 while (dev->drv[di]->locks > 0) {
691 isdn_unlock_driver(dev->drv[di]);
693 spin_lock_irqsave(&dev->lock, flags);
694 isdn_tty_stat_callback(i, c);
695 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
696 if (dev->drvmap[i] == di) {
697 dev->drvmap[i] = -1;
698 dev->chanmap[i] = -1;
699 dev->usage[i] &= ~ISDN_USAGE_DISABLED;
701 dev->drivers--;
702 dev->channels -= dev->drv[di]->channels;
703 kfree(dev->drv[di]->rcverr);
704 kfree(dev->drv[di]->rcvcount);
705 for (i = 0; i < dev->drv[di]->channels; i++)
706 skb_queue_purge(&dev->drv[di]->rpqueue[i]);
707 kfree(dev->drv[di]->rpqueue);
708 kfree(dev->drv[di]->rcv_waitq);
709 kfree(dev->drv[di]);
710 dev->drv[di] = NULL;
711 dev->drvid[di][0] = '\0';
712 isdn_info_update();
713 set_global_features();
714 spin_unlock_irqrestore(&dev->lock, flags);
715 return 0;
716 case ISDN_STAT_L1ERR:
717 break;
718 case CAPI_PUT_MESSAGE:
719 return(isdn_capi_rec_hl_msg(&c->parm.cmsg));
720 #ifdef CONFIG_ISDN_TTY_FAX
721 case ISDN_STAT_FAXIND:
722 isdn_tty_stat_callback(i, c);
723 break;
724 #endif
725 #ifdef CONFIG_ISDN_AUDIO
726 case ISDN_STAT_AUDIO:
727 isdn_tty_stat_callback(i, c);
728 break;
729 #endif
730 #ifdef CONFIG_ISDN_DIVERSION
731 case ISDN_STAT_PROT:
732 case ISDN_STAT_REDIR:
733 if (divert_if)
734 return(divert_if->stat_callback(c));
735 #endif /* CONFIG_ISDN_DIVERSION */
736 default:
737 return -1;
739 return 0;
743 * Get integer from char-pointer, set pointer to end of number
746 isdn_getnum(char **p)
748 int v = -1;
750 while (*p[0] >= '0' && *p[0] <= '9')
751 v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p[0]++) - '0');
752 return v;
755 #define DLE 0x10
758 * isdn_readbchan() tries to get data from the read-queue.
759 * It MUST be called with interrupts off.
761 * Be aware that this is not an atomic operation when sleep != 0, even though
762 * interrupts are turned off! Well, like that we are currently only called
763 * on behalf of a read system call on raw device files (which are documented
764 * to be dangerous and for for debugging purpose only). The inode semaphore
765 * takes care that this is not called for the same minor device number while
766 * we are sleeping, but access is not serialized against simultaneous read()
767 * from the corresponding ttyI device. Can other ugly events, like changes
768 * of the mapping (di,ch)<->minor, happen during the sleep? --he
771 isdn_readbchan(int di, int channel, u_char * buf, u_char * fp, int len, wait_queue_head_t *sleep)
773 int count;
774 int count_pull;
775 int count_put;
776 int dflag;
777 struct sk_buff *skb;
778 u_char *cp;
780 if (!dev->drv[di])
781 return 0;
782 if (skb_queue_empty(&dev->drv[di]->rpqueue[channel])) {
783 if (sleep)
784 interruptible_sleep_on(sleep);
785 else
786 return 0;
788 if (len > dev->drv[di]->rcvcount[channel])
789 len = dev->drv[di]->rcvcount[channel];
790 cp = buf;
791 count = 0;
792 while (len) {
793 if (!(skb = skb_peek(&dev->drv[di]->rpqueue[channel])))
794 break;
795 #ifdef CONFIG_ISDN_AUDIO
796 if (ISDN_AUDIO_SKB_LOCK(skb))
797 break;
798 ISDN_AUDIO_SKB_LOCK(skb) = 1;
799 if ((ISDN_AUDIO_SKB_DLECOUNT(skb)) || (dev->drv[di]->DLEflag & (1 << channel))) {
800 char *p = skb->data;
801 unsigned long DLEmask = (1 << channel);
803 dflag = 0;
804 count_pull = count_put = 0;
805 while ((count_pull < skb->len) && (len > 0)) {
806 len--;
807 if (dev->drv[di]->DLEflag & DLEmask) {
808 *cp++ = DLE;
809 dev->drv[di]->DLEflag &= ~DLEmask;
810 } else {
811 *cp++ = *p;
812 if (*p == DLE) {
813 dev->drv[di]->DLEflag |= DLEmask;
814 (ISDN_AUDIO_SKB_DLECOUNT(skb))--;
816 p++;
817 count_pull++;
819 count_put++;
821 if (count_pull >= skb->len)
822 dflag = 1;
823 } else {
824 #endif
825 /* No DLE's in buff, so simply copy it */
826 dflag = 1;
827 if ((count_pull = skb->len) > len) {
828 count_pull = len;
829 dflag = 0;
831 count_put = count_pull;
832 skb_copy_from_linear_data(skb, cp, count_put);
833 cp += count_put;
834 len -= count_put;
835 #ifdef CONFIG_ISDN_AUDIO
837 #endif
838 count += count_put;
839 if (fp) {
840 memset(fp, 0, count_put);
841 fp += count_put;
843 if (dflag) {
844 /* We got all the data in this buff.
845 * Now we can dequeue it.
847 if (fp)
848 *(fp - 1) = 0xff;
849 #ifdef CONFIG_ISDN_AUDIO
850 ISDN_AUDIO_SKB_LOCK(skb) = 0;
851 #endif
852 skb = skb_dequeue(&dev->drv[di]->rpqueue[channel]);
853 dev_kfree_skb(skb);
854 } else {
855 /* Not yet emptied this buff, so it
856 * must stay in the queue, for further calls
857 * but we pull off the data we got until now.
859 skb_pull(skb, count_pull);
860 #ifdef CONFIG_ISDN_AUDIO
861 ISDN_AUDIO_SKB_LOCK(skb) = 0;
862 #endif
864 dev->drv[di]->rcvcount[channel] -= count_put;
866 return count;
870 * isdn_readbchan_tty() tries to get data from the read-queue.
871 * It MUST be called with interrupts off.
873 * Be aware that this is not an atomic operation when sleep != 0, even though
874 * interrupts are turned off! Well, like that we are currently only called
875 * on behalf of a read system call on raw device files (which are documented
876 * to be dangerous and for for debugging purpose only). The inode semaphore
877 * takes care that this is not called for the same minor device number while
878 * we are sleeping, but access is not serialized against simultaneous read()
879 * from the corresponding ttyI device. Can other ugly events, like changes
880 * of the mapping (di,ch)<->minor, happen during the sleep? --he
883 isdn_readbchan_tty(int di, int channel, struct tty_struct *tty, int cisco_hack)
885 int count;
886 int count_pull;
887 int count_put;
888 int dflag;
889 struct sk_buff *skb;
890 char last = 0;
891 int len;
893 if (!dev->drv[di])
894 return 0;
895 if (skb_queue_empty(&dev->drv[di]->rpqueue[channel]))
896 return 0;
898 len = tty_buffer_request_room(tty, dev->drv[di]->rcvcount[channel]);
899 if(len == 0)
900 return len;
902 count = 0;
903 while (len) {
904 if (!(skb = skb_peek(&dev->drv[di]->rpqueue[channel])))
905 break;
906 #ifdef CONFIG_ISDN_AUDIO
907 if (ISDN_AUDIO_SKB_LOCK(skb))
908 break;
909 ISDN_AUDIO_SKB_LOCK(skb) = 1;
910 if ((ISDN_AUDIO_SKB_DLECOUNT(skb)) || (dev->drv[di]->DLEflag & (1 << channel))) {
911 char *p = skb->data;
912 unsigned long DLEmask = (1 << channel);
914 dflag = 0;
915 count_pull = count_put = 0;
916 while ((count_pull < skb->len) && (len > 0)) {
917 /* push every character but the last to the tty buffer directly */
918 if ( count_put )
919 tty_insert_flip_char(tty, last, TTY_NORMAL);
920 len--;
921 if (dev->drv[di]->DLEflag & DLEmask) {
922 last = DLE;
923 dev->drv[di]->DLEflag &= ~DLEmask;
924 } else {
925 last = *p;
926 if (last == DLE) {
927 dev->drv[di]->DLEflag |= DLEmask;
928 (ISDN_AUDIO_SKB_DLECOUNT(skb))--;
930 p++;
931 count_pull++;
933 count_put++;
935 if (count_pull >= skb->len)
936 dflag = 1;
937 } else {
938 #endif
939 /* No DLE's in buff, so simply copy it */
940 dflag = 1;
941 if ((count_pull = skb->len) > len) {
942 count_pull = len;
943 dflag = 0;
945 count_put = count_pull;
946 if(count_put > 1)
947 tty_insert_flip_string(tty, skb->data, count_put - 1);
948 last = skb->data[count_put - 1];
949 len -= count_put;
950 #ifdef CONFIG_ISDN_AUDIO
952 #endif
953 count += count_put;
954 if (dflag) {
955 /* We got all the data in this buff.
956 * Now we can dequeue it.
958 if(cisco_hack)
959 tty_insert_flip_char(tty, last, 0xFF);
960 else
961 tty_insert_flip_char(tty, last, TTY_NORMAL);
962 #ifdef CONFIG_ISDN_AUDIO
963 ISDN_AUDIO_SKB_LOCK(skb) = 0;
964 #endif
965 skb = skb_dequeue(&dev->drv[di]->rpqueue[channel]);
966 dev_kfree_skb(skb);
967 } else {
968 tty_insert_flip_char(tty, last, TTY_NORMAL);
969 /* Not yet emptied this buff, so it
970 * must stay in the queue, for further calls
971 * but we pull off the data we got until now.
973 skb_pull(skb, count_pull);
974 #ifdef CONFIG_ISDN_AUDIO
975 ISDN_AUDIO_SKB_LOCK(skb) = 0;
976 #endif
978 dev->drv[di]->rcvcount[channel] -= count_put;
980 return count;
984 <<<<<<< HEAD:drivers/isdn/i4l/isdn_common.c
985 static __inline int
986 =======
987 static inline int
988 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/isdn/i4l/isdn_common.c
989 isdn_minor2drv(int minor)
991 return (dev->drvmap[minor]);
994 <<<<<<< HEAD:drivers/isdn/i4l/isdn_common.c
995 static __inline int
996 =======
997 static inline int
998 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/isdn/i4l/isdn_common.c
999 isdn_minor2chan(int minor)
1001 return (dev->chanmap[minor]);
1004 static char *
1005 isdn_statstr(void)
1007 static char istatbuf[2048];
1008 char *p;
1009 int i;
1011 sprintf(istatbuf, "idmap:\t");
1012 p = istatbuf + strlen(istatbuf);
1013 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1014 sprintf(p, "%s ", (dev->drvmap[i] < 0) ? "-" : dev->drvid[dev->drvmap[i]]);
1015 p = istatbuf + strlen(istatbuf);
1017 sprintf(p, "\nchmap:\t");
1018 p = istatbuf + strlen(istatbuf);
1019 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1020 sprintf(p, "%d ", dev->chanmap[i]);
1021 p = istatbuf + strlen(istatbuf);
1023 sprintf(p, "\ndrmap:\t");
1024 p = istatbuf + strlen(istatbuf);
1025 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1026 sprintf(p, "%d ", dev->drvmap[i]);
1027 p = istatbuf + strlen(istatbuf);
1029 sprintf(p, "\nusage:\t");
1030 p = istatbuf + strlen(istatbuf);
1031 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1032 sprintf(p, "%d ", dev->usage[i]);
1033 p = istatbuf + strlen(istatbuf);
1035 sprintf(p, "\nflags:\t");
1036 p = istatbuf + strlen(istatbuf);
1037 for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
1038 if (dev->drv[i]) {
1039 sprintf(p, "%ld ", dev->drv[i]->online);
1040 p = istatbuf + strlen(istatbuf);
1041 } else {
1042 sprintf(p, "? ");
1043 p = istatbuf + strlen(istatbuf);
1046 sprintf(p, "\nphone:\t");
1047 p = istatbuf + strlen(istatbuf);
1048 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1049 sprintf(p, "%s ", dev->num[i]);
1050 p = istatbuf + strlen(istatbuf);
1052 sprintf(p, "\n");
1053 return istatbuf;
1056 /* Module interface-code */
1058 void
1059 isdn_info_update(void)
1061 infostruct *p = dev->infochain;
1063 while (p) {
1064 *(p->private) = 1;
1065 p = (infostruct *) p->next;
1067 wake_up_interruptible(&(dev->info_waitq));
1070 static ssize_t
1071 isdn_read(struct file *file, char __user *buf, size_t count, loff_t * off)
1073 uint minor = iminor(file->f_path.dentry->d_inode);
1074 int len = 0;
1075 int drvidx;
1076 int chidx;
1077 int retval;
1078 char *p;
1080 lock_kernel();
1081 if (minor == ISDN_MINOR_STATUS) {
1082 if (!file->private_data) {
1083 if (file->f_flags & O_NONBLOCK) {
1084 retval = -EAGAIN;
1085 goto out;
1087 interruptible_sleep_on(&(dev->info_waitq));
1089 p = isdn_statstr();
1090 file->private_data = NULL;
1091 if ((len = strlen(p)) <= count) {
1092 if (copy_to_user(buf, p, len)) {
1093 retval = -EFAULT;
1094 goto out;
1096 *off += len;
1097 retval = len;
1098 goto out;
1100 retval = 0;
1101 goto out;
1103 if (!dev->drivers) {
1104 retval = -ENODEV;
1105 goto out;
1107 if (minor <= ISDN_MINOR_BMAX) {
1108 printk(KERN_WARNING "isdn_read minor %d obsolete!\n", minor);
1109 drvidx = isdn_minor2drv(minor);
1110 if (drvidx < 0) {
1111 retval = -ENODEV;
1112 goto out;
1114 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
1115 retval = -ENODEV;
1116 goto out;
1118 chidx = isdn_minor2chan(minor);
1119 if (!(p = kmalloc(count, GFP_KERNEL))) {
1120 retval = -ENOMEM;
1121 goto out;
1123 len = isdn_readbchan(drvidx, chidx, p, NULL, count,
1124 &dev->drv[drvidx]->rcv_waitq[chidx]);
1125 *off += len;
1126 if (copy_to_user(buf,p,len))
1127 len = -EFAULT;
1128 kfree(p);
1129 retval = len;
1130 goto out;
1132 if (minor <= ISDN_MINOR_CTRLMAX) {
1133 drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1134 if (drvidx < 0) {
1135 retval = -ENODEV;
1136 goto out;
1138 if (!dev->drv[drvidx]->stavail) {
1139 if (file->f_flags & O_NONBLOCK) {
1140 retval = -EAGAIN;
1141 goto out;
1143 interruptible_sleep_on(&(dev->drv[drvidx]->st_waitq));
1145 if (dev->drv[drvidx]->interface->readstat) {
1146 if (count > dev->drv[drvidx]->stavail)
1147 count = dev->drv[drvidx]->stavail;
1148 len = dev->drv[drvidx]->interface->readstat(buf, count,
1149 drvidx, isdn_minor2chan(minor - ISDN_MINOR_CTRL));
1150 if (len < 0) {
1151 retval = len;
1152 goto out;
1154 } else {
1155 len = 0;
1157 if (len)
1158 dev->drv[drvidx]->stavail -= len;
1159 else
1160 dev->drv[drvidx]->stavail = 0;
1161 *off += len;
1162 retval = len;
1163 goto out;
1165 #ifdef CONFIG_ISDN_PPP
1166 if (minor <= ISDN_MINOR_PPPMAX) {
1167 retval = isdn_ppp_read(minor - ISDN_MINOR_PPP, file, buf, count);
1168 goto out;
1170 #endif
1171 retval = -ENODEV;
1172 out:
1173 unlock_kernel();
1174 return retval;
1177 static ssize_t
1178 isdn_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
1180 uint minor = iminor(file->f_path.dentry->d_inode);
1181 int drvidx;
1182 int chidx;
1183 int retval;
1185 if (minor == ISDN_MINOR_STATUS)
1186 return -EPERM;
1187 if (!dev->drivers)
1188 return -ENODEV;
1190 lock_kernel();
1191 if (minor <= ISDN_MINOR_BMAX) {
1192 printk(KERN_WARNING "isdn_write minor %d obsolete!\n", minor);
1193 drvidx = isdn_minor2drv(minor);
1194 if (drvidx < 0) {
1195 retval = -ENODEV;
1196 goto out;
1198 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
1199 retval = -ENODEV;
1200 goto out;
1202 chidx = isdn_minor2chan(minor);
1203 while ((retval = isdn_writebuf_stub(drvidx, chidx, buf, count)) == 0)
1204 interruptible_sleep_on(&dev->drv[drvidx]->snd_waitq[chidx]);
1205 goto out;
1207 if (minor <= ISDN_MINOR_CTRLMAX) {
1208 drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1209 if (drvidx < 0) {
1210 retval = -ENODEV;
1211 goto out;
1214 * We want to use the isdnctrl device to load the firmware
1216 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1217 return -ENODEV;
1219 if (dev->drv[drvidx]->interface->writecmd)
1220 retval = dev->drv[drvidx]->interface->
1221 writecmd(buf, count, drvidx,
1222 isdn_minor2chan(minor - ISDN_MINOR_CTRL));
1223 else
1224 retval = count;
1225 goto out;
1227 #ifdef CONFIG_ISDN_PPP
1228 if (minor <= ISDN_MINOR_PPPMAX) {
1229 retval = isdn_ppp_write(minor - ISDN_MINOR_PPP, file, buf, count);
1230 goto out;
1232 #endif
1233 retval = -ENODEV;
1234 out:
1235 unlock_kernel();
1236 return retval;
1239 static unsigned int
1240 isdn_poll(struct file *file, poll_table * wait)
1242 unsigned int mask = 0;
1243 unsigned int minor = iminor(file->f_path.dentry->d_inode);
1244 int drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1246 lock_kernel();
1247 if (minor == ISDN_MINOR_STATUS) {
1248 poll_wait(file, &(dev->info_waitq), wait);
1249 /* mask = POLLOUT | POLLWRNORM; */
1250 if (file->private_data) {
1251 mask |= POLLIN | POLLRDNORM;
1253 goto out;
1255 if (minor >= ISDN_MINOR_CTRL && minor <= ISDN_MINOR_CTRLMAX) {
1256 if (drvidx < 0) {
1257 /* driver deregistered while file open */
1258 mask = POLLHUP;
1259 goto out;
1261 poll_wait(file, &(dev->drv[drvidx]->st_waitq), wait);
1262 mask = POLLOUT | POLLWRNORM;
1263 if (dev->drv[drvidx]->stavail) {
1264 mask |= POLLIN | POLLRDNORM;
1266 goto out;
1268 #ifdef CONFIG_ISDN_PPP
1269 if (minor <= ISDN_MINOR_PPPMAX) {
1270 mask = isdn_ppp_poll(file, wait);
1271 goto out;
1273 #endif
1274 mask = POLLERR;
1275 out:
1276 unlock_kernel();
1277 return mask;
1281 static int
1282 isdn_ioctl(struct inode *inode, struct file *file, uint cmd, ulong arg)
1284 uint minor = iminor(inode);
1285 isdn_ctrl c;
1286 int drvidx;
1287 int chidx;
1288 int ret;
1289 int i;
1290 char __user *p;
1291 char *s;
1292 union iocpar {
1293 char name[10];
1294 char bname[22];
1295 isdn_ioctl_struct iocts;
1296 isdn_net_ioctl_phone phone;
1297 isdn_net_ioctl_cfg cfg;
1298 } iocpar;
1299 void __user *argp = (void __user *)arg;
1301 #define name iocpar.name
1302 #define bname iocpar.bname
1303 #define iocts iocpar.iocts
1304 #define phone iocpar.phone
1305 #define cfg iocpar.cfg
1307 if (minor == ISDN_MINOR_STATUS) {
1308 switch (cmd) {
1309 case IIOCGETDVR:
1310 return (TTY_DV +
1311 (NET_DV << 8) +
1312 (INF_DV << 16));
1313 case IIOCGETCPS:
1314 if (arg) {
1315 ulong __user *p = argp;
1316 int i;
1317 if (!access_ok(VERIFY_WRITE, p,
1318 sizeof(ulong) * ISDN_MAX_CHANNELS * 2))
1319 return -EFAULT;
1320 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1321 put_user(dev->ibytes[i], p++);
1322 put_user(dev->obytes[i], p++);
1324 return 0;
1325 } else
1326 return -EINVAL;
1327 break;
1328 #ifdef CONFIG_NETDEVICES
1329 case IIOCNETGPN:
1330 /* Get peer phone number of a connected
1331 * isdn network interface */
1332 if (arg) {
1333 if (copy_from_user(&phone, argp, sizeof(phone)))
1334 return -EFAULT;
1335 return isdn_net_getpeer(&phone, argp);
1336 } else
1337 return -EINVAL;
1338 #endif
1339 default:
1340 return -EINVAL;
1343 if (!dev->drivers)
1344 return -ENODEV;
1345 if (minor <= ISDN_MINOR_BMAX) {
1346 drvidx = isdn_minor2drv(minor);
1347 if (drvidx < 0)
1348 return -ENODEV;
1349 chidx = isdn_minor2chan(minor);
1350 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1351 return -ENODEV;
1352 return 0;
1354 if (minor <= ISDN_MINOR_CTRLMAX) {
1356 * isdn net devices manage lots of configuration variables as linked lists.
1357 * Those lists must only be manipulated from user space. Some of the ioctl's
1358 * service routines access user space and are not atomic. Therefor, ioctl's
1359 * manipulating the lists and ioctl's sleeping while accessing the lists
1360 * are serialized by means of a semaphore.
1362 switch (cmd) {
1363 case IIOCNETDWRSET:
1364 printk(KERN_INFO "INFO: ISDN_DW_ABC_EXTENSION not enabled\n");
1365 return(-EINVAL);
1366 case IIOCNETLCR:
1367 printk(KERN_INFO "INFO: ISDN_ABC_LCR_SUPPORT not enabled\n");
1368 return -ENODEV;
1369 #ifdef CONFIG_NETDEVICES
1370 case IIOCNETAIF:
1371 /* Add a network-interface */
1372 if (arg) {
1373 if (copy_from_user(name, argp, sizeof(name)))
1374 return -EFAULT;
1375 s = name;
1376 } else {
1377 s = NULL;
1379 ret = mutex_lock_interruptible(&dev->mtx);
1380 if( ret ) return ret;
1381 if ((s = isdn_net_new(s, NULL))) {
1382 if (copy_to_user(argp, s, strlen(s) + 1)){
1383 ret = -EFAULT;
1384 } else {
1385 ret = 0;
1387 } else
1388 ret = -ENODEV;
1389 mutex_unlock(&dev->mtx);
1390 return ret;
1391 case IIOCNETASL:
1392 /* Add a slave to a network-interface */
1393 if (arg) {
1394 if (copy_from_user(bname, argp, sizeof(bname) - 1))
1395 return -EFAULT;
1396 } else
1397 return -EINVAL;
1398 ret = mutex_lock_interruptible(&dev->mtx);
1399 if( ret ) return ret;
1400 if ((s = isdn_net_newslave(bname))) {
1401 if (copy_to_user(argp, s, strlen(s) + 1)){
1402 ret = -EFAULT;
1403 } else {
1404 ret = 0;
1406 } else
1407 ret = -ENODEV;
1408 mutex_unlock(&dev->mtx);
1409 return ret;
1410 case IIOCNETDIF:
1411 /* Delete a network-interface */
1412 if (arg) {
1413 if (copy_from_user(name, argp, sizeof(name)))
1414 return -EFAULT;
1415 ret = mutex_lock_interruptible(&dev->mtx);
1416 if( ret ) return ret;
1417 ret = isdn_net_rm(name);
1418 mutex_unlock(&dev->mtx);
1419 return ret;
1420 } else
1421 return -EINVAL;
1422 case IIOCNETSCF:
1423 /* Set configurable parameters of a network-interface */
1424 if (arg) {
1425 if (copy_from_user(&cfg, argp, sizeof(cfg)))
1426 return -EFAULT;
1427 return isdn_net_setcfg(&cfg);
1428 } else
1429 return -EINVAL;
1430 case IIOCNETGCF:
1431 /* Get configurable parameters of a network-interface */
1432 if (arg) {
1433 if (copy_from_user(&cfg, argp, sizeof(cfg)))
1434 return -EFAULT;
1435 if (!(ret = isdn_net_getcfg(&cfg))) {
1436 if (copy_to_user(argp, &cfg, sizeof(cfg)))
1437 return -EFAULT;
1439 return ret;
1440 } else
1441 return -EINVAL;
1442 case IIOCNETANM:
1443 /* Add a phone-number to a network-interface */
1444 if (arg) {
1445 if (copy_from_user(&phone, argp, sizeof(phone)))
1446 return -EFAULT;
1447 ret = mutex_lock_interruptible(&dev->mtx);
1448 if( ret ) return ret;
1449 ret = isdn_net_addphone(&phone);
1450 mutex_unlock(&dev->mtx);
1451 return ret;
1452 } else
1453 return -EINVAL;
1454 case IIOCNETGNM:
1455 /* Get list of phone-numbers of a network-interface */
1456 if (arg) {
1457 if (copy_from_user(&phone, argp, sizeof(phone)))
1458 return -EFAULT;
1459 ret = mutex_lock_interruptible(&dev->mtx);
1460 if( ret ) return ret;
1461 ret = isdn_net_getphones(&phone, argp);
1462 mutex_unlock(&dev->mtx);
1463 return ret;
1464 } else
1465 return -EINVAL;
1466 case IIOCNETDNM:
1467 /* Delete a phone-number of a network-interface */
1468 if (arg) {
1469 if (copy_from_user(&phone, argp, sizeof(phone)))
1470 return -EFAULT;
1471 ret = mutex_lock_interruptible(&dev->mtx);
1472 if( ret ) return ret;
1473 ret = isdn_net_delphone(&phone);
1474 mutex_unlock(&dev->mtx);
1475 return ret;
1476 } else
1477 return -EINVAL;
1478 case IIOCNETDIL:
1479 /* Force dialing of a network-interface */
1480 if (arg) {
1481 if (copy_from_user(name, argp, sizeof(name)))
1482 return -EFAULT;
1483 return isdn_net_force_dial(name);
1484 } else
1485 return -EINVAL;
1486 #ifdef CONFIG_ISDN_PPP
1487 case IIOCNETALN:
1488 if (!arg)
1489 return -EINVAL;
1490 if (copy_from_user(name, argp, sizeof(name)))
1491 return -EFAULT;
1492 return isdn_ppp_dial_slave(name);
1493 case IIOCNETDLN:
1494 if (!arg)
1495 return -EINVAL;
1496 if (copy_from_user(name, argp, sizeof(name)))
1497 return -EFAULT;
1498 return isdn_ppp_hangup_slave(name);
1499 #endif
1500 case IIOCNETHUP:
1501 /* Force hangup of a network-interface */
1502 if (!arg)
1503 return -EINVAL;
1504 if (copy_from_user(name, argp, sizeof(name)))
1505 return -EFAULT;
1506 return isdn_net_force_hangup(name);
1507 break;
1508 #endif /* CONFIG_NETDEVICES */
1509 case IIOCSETVER:
1510 dev->net_verbose = arg;
1511 printk(KERN_INFO "isdn: Verbose-Level is %d\n", dev->net_verbose);
1512 return 0;
1513 case IIOCSETGST:
1514 if (arg)
1515 dev->global_flags |= ISDN_GLOBAL_STOPPED;
1516 else
1517 dev->global_flags &= ~ISDN_GLOBAL_STOPPED;
1518 printk(KERN_INFO "isdn: Global Mode %s\n",
1519 (dev->global_flags & ISDN_GLOBAL_STOPPED) ? "stopped" : "running");
1520 return 0;
1521 case IIOCSETBRJ:
1522 drvidx = -1;
1523 if (arg) {
1524 int i;
1525 char *p;
1526 if (copy_from_user(&iocts, argp,
1527 sizeof(isdn_ioctl_struct)))
1528 return -EFAULT;
1529 iocts.drvid[sizeof(iocts.drvid)-1] = 0;
1530 if (strlen(iocts.drvid)) {
1531 if ((p = strchr(iocts.drvid, ',')))
1532 *p = 0;
1533 drvidx = -1;
1534 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1535 if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1536 drvidx = i;
1537 break;
1541 if (drvidx == -1)
1542 return -ENODEV;
1543 if (iocts.arg)
1544 dev->drv[drvidx]->flags |= DRV_FLAG_REJBUS;
1545 else
1546 dev->drv[drvidx]->flags &= ~DRV_FLAG_REJBUS;
1547 return 0;
1548 case IIOCSIGPRF:
1549 dev->profd = current;
1550 return 0;
1551 break;
1552 case IIOCGETPRF:
1553 /* Get all Modem-Profiles */
1554 if (arg) {
1555 char __user *p = argp;
1556 int i;
1558 if (!access_ok(VERIFY_WRITE, argp,
1559 (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
1560 * ISDN_MAX_CHANNELS))
1561 return -EFAULT;
1563 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1564 if (copy_to_user(p, dev->mdm.info[i].emu.profile,
1565 ISDN_MODEM_NUMREG))
1566 return -EFAULT;
1567 p += ISDN_MODEM_NUMREG;
1568 if (copy_to_user(p, dev->mdm.info[i].emu.pmsn, ISDN_MSNLEN))
1569 return -EFAULT;
1570 p += ISDN_MSNLEN;
1571 if (copy_to_user(p, dev->mdm.info[i].emu.plmsn, ISDN_LMSNLEN))
1572 return -EFAULT;
1573 p += ISDN_LMSNLEN;
1575 return (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN) * ISDN_MAX_CHANNELS;
1576 } else
1577 return -EINVAL;
1578 break;
1579 case IIOCSETPRF:
1580 /* Set all Modem-Profiles */
1581 if (arg) {
1582 char __user *p = argp;
1583 int i;
1585 if (!access_ok(VERIFY_READ, argp,
1586 (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
1587 * ISDN_MAX_CHANNELS))
1588 return -EFAULT;
1590 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1591 if (copy_from_user(dev->mdm.info[i].emu.profile, p,
1592 ISDN_MODEM_NUMREG))
1593 return -EFAULT;
1594 p += ISDN_MODEM_NUMREG;
1595 if (copy_from_user(dev->mdm.info[i].emu.plmsn, p, ISDN_LMSNLEN))
1596 return -EFAULT;
1597 p += ISDN_LMSNLEN;
1598 if (copy_from_user(dev->mdm.info[i].emu.pmsn, p, ISDN_MSNLEN))
1599 return -EFAULT;
1600 p += ISDN_MSNLEN;
1602 return 0;
1603 } else
1604 return -EINVAL;
1605 break;
1606 case IIOCSETMAP:
1607 case IIOCGETMAP:
1608 /* Set/Get MSN->EAZ-Mapping for a driver */
1609 if (arg) {
1611 if (copy_from_user(&iocts, argp,
1612 sizeof(isdn_ioctl_struct)))
1613 return -EFAULT;
1614 iocts.drvid[sizeof(iocts.drvid)-1] = 0;
1615 if (strlen(iocts.drvid)) {
1616 drvidx = -1;
1617 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1618 if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1619 drvidx = i;
1620 break;
1622 } else
1623 drvidx = 0;
1624 if (drvidx == -1)
1625 return -ENODEV;
1626 if (cmd == IIOCSETMAP) {
1627 int loop = 1;
1629 p = (char __user *) iocts.arg;
1630 i = 0;
1631 while (loop) {
1632 int j = 0;
1634 while (1) {
1635 if (!access_ok(VERIFY_READ, p, 1))
1636 return -EFAULT;
1637 get_user(bname[j], p++);
1638 switch (bname[j]) {
1639 case '\0':
1640 loop = 0;
1641 /* Fall through */
1642 case ',':
1643 bname[j] = '\0';
1644 strcpy(dev->drv[drvidx]->msn2eaz[i], bname);
1645 j = ISDN_MSNLEN;
1646 break;
1647 default:
1648 j++;
1650 if (j >= ISDN_MSNLEN)
1651 break;
1653 if (++i > 9)
1654 break;
1656 } else {
1657 p = (char __user *) iocts.arg;
1658 for (i = 0; i < 10; i++) {
1659 snprintf(bname, sizeof(bname), "%s%s",
1660 strlen(dev->drv[drvidx]->msn2eaz[i]) ?
1661 dev->drv[drvidx]->msn2eaz[i] : "_",
1662 (i < 9) ? "," : "\0");
1663 if (copy_to_user(p, bname, strlen(bname) + 1))
1664 return -EFAULT;
1665 p += strlen(bname);
1668 return 0;
1669 } else
1670 return -EINVAL;
1671 case IIOCDBGVAR:
1672 if (arg) {
1673 if (copy_to_user(argp, &dev, sizeof(ulong)))
1674 return -EFAULT;
1675 return 0;
1676 } else
1677 return -EINVAL;
1678 break;
1679 default:
1680 if ((cmd & IIOCDRVCTL) == IIOCDRVCTL)
1681 cmd = ((cmd >> _IOC_NRSHIFT) & _IOC_NRMASK) & ISDN_DRVIOCTL_MASK;
1682 else
1683 return -EINVAL;
1684 if (arg) {
1685 int i;
1686 char *p;
1687 if (copy_from_user(&iocts, argp, sizeof(isdn_ioctl_struct)))
1688 return -EFAULT;
1689 iocts.drvid[sizeof(iocts.drvid)-1] = 0;
1690 if (strlen(iocts.drvid)) {
1691 if ((p = strchr(iocts.drvid, ',')))
1692 *p = 0;
1693 drvidx = -1;
1694 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1695 if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1696 drvidx = i;
1697 break;
1699 } else
1700 drvidx = 0;
1701 if (drvidx == -1)
1702 return -ENODEV;
1703 if (!access_ok(VERIFY_WRITE, argp,
1704 sizeof(isdn_ioctl_struct)))
1705 return -EFAULT;
1706 c.driver = drvidx;
1707 c.command = ISDN_CMD_IOCTL;
1708 c.arg = cmd;
1709 memcpy(c.parm.num, &iocts.arg, sizeof(ulong));
1710 ret = isdn_command(&c);
1711 memcpy(&iocts.arg, c.parm.num, sizeof(ulong));
1712 if (copy_to_user(argp, &iocts, sizeof(isdn_ioctl_struct)))
1713 return -EFAULT;
1714 return ret;
1715 } else
1716 return -EINVAL;
1719 #ifdef CONFIG_ISDN_PPP
1720 if (minor <= ISDN_MINOR_PPPMAX)
1721 return (isdn_ppp_ioctl(minor - ISDN_MINOR_PPP, file, cmd, arg));
1722 #endif
1723 return -ENODEV;
1725 #undef name
1726 #undef bname
1727 #undef iocts
1728 #undef phone
1729 #undef cfg
1733 * Open the device code.
1735 static int
1736 isdn_open(struct inode *ino, struct file *filep)
1738 uint minor = iminor(ino);
1739 int drvidx;
1740 int chidx;
1741 int retval = -ENODEV;
1744 if (minor == ISDN_MINOR_STATUS) {
1745 infostruct *p;
1747 if ((p = kmalloc(sizeof(infostruct), GFP_KERNEL))) {
1748 p->next = (char *) dev->infochain;
1749 p->private = (char *) &(filep->private_data);
1750 dev->infochain = p;
1751 /* At opening we allow a single update */
1752 filep->private_data = (char *) 1;
1753 retval = 0;
1754 goto out;
1755 } else {
1756 retval = -ENOMEM;
1757 goto out;
1760 if (!dev->channels)
1761 goto out;
1762 if (minor <= ISDN_MINOR_BMAX) {
1763 printk(KERN_WARNING "isdn_open minor %d obsolete!\n", minor);
1764 drvidx = isdn_minor2drv(minor);
1765 if (drvidx < 0)
1766 goto out;
1767 chidx = isdn_minor2chan(minor);
1768 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1769 goto out;
1770 if (!(dev->drv[drvidx]->online & (1 << chidx)))
1771 goto out;
1772 isdn_lock_drivers();
1773 retval = 0;
1774 goto out;
1776 if (minor <= ISDN_MINOR_CTRLMAX) {
1777 drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1778 if (drvidx < 0)
1779 goto out;
1780 isdn_lock_drivers();
1781 retval = 0;
1782 goto out;
1784 #ifdef CONFIG_ISDN_PPP
1785 if (minor <= ISDN_MINOR_PPPMAX) {
1786 retval = isdn_ppp_open(minor - ISDN_MINOR_PPP, filep);
1787 if (retval == 0)
1788 isdn_lock_drivers();
1789 goto out;
1791 #endif
1792 out:
1793 nonseekable_open(ino, filep);
1794 return retval;
1797 static int
1798 isdn_close(struct inode *ino, struct file *filep)
1800 uint minor = iminor(ino);
1802 lock_kernel();
1803 if (minor == ISDN_MINOR_STATUS) {
1804 infostruct *p = dev->infochain;
1805 infostruct *q = NULL;
1807 while (p) {
1808 if (p->private == (char *) &(filep->private_data)) {
1809 if (q)
1810 q->next = p->next;
1811 else
1812 dev->infochain = (infostruct *) (p->next);
1813 kfree(p);
1814 goto out;
1816 q = p;
1817 p = (infostruct *) (p->next);
1819 printk(KERN_WARNING "isdn: No private data while closing isdnctrl\n");
1820 goto out;
1822 isdn_unlock_drivers();
1823 if (minor <= ISDN_MINOR_BMAX)
1824 goto out;
1825 if (minor <= ISDN_MINOR_CTRLMAX) {
1826 if (dev->profd == current)
1827 dev->profd = NULL;
1828 goto out;
1830 #ifdef CONFIG_ISDN_PPP
1831 if (minor <= ISDN_MINOR_PPPMAX)
1832 isdn_ppp_release(minor - ISDN_MINOR_PPP, filep);
1833 #endif
1835 out:
1836 unlock_kernel();
1837 return 0;
1840 static const struct file_operations isdn_fops =
1842 .owner = THIS_MODULE,
1843 .llseek = no_llseek,
1844 .read = isdn_read,
1845 .write = isdn_write,
1846 .poll = isdn_poll,
1847 .ioctl = isdn_ioctl,
1848 .open = isdn_open,
1849 .release = isdn_close,
1852 char *
1853 isdn_map_eaz2msn(char *msn, int di)
1855 isdn_driver_t *this = dev->drv[di];
1856 int i;
1858 if (strlen(msn) == 1) {
1859 i = msn[0] - '0';
1860 if ((i >= 0) && (i <= 9))
1861 if (strlen(this->msn2eaz[i]))
1862 return (this->msn2eaz[i]);
1864 return (msn);
1868 * Find an unused ISDN-channel, whose feature-flags match the
1869 * given L2- and L3-protocols.
1871 #define L2V (~(ISDN_FEATURE_L2_V11096|ISDN_FEATURE_L2_V11019|ISDN_FEATURE_L2_V11038))
1874 * This function must be called with holding the dev->lock.
1877 isdn_get_free_channel(int usage, int l2_proto, int l3_proto, int pre_dev
1878 ,int pre_chan, char *msn)
1880 int i;
1881 ulong features;
1882 ulong vfeatures;
1884 features = ((1 << l2_proto) | (0x10000 << l3_proto));
1885 vfeatures = (((1 << l2_proto) | (0x10000 << l3_proto)) &
1886 ~(ISDN_FEATURE_L2_V11096|ISDN_FEATURE_L2_V11019|ISDN_FEATURE_L2_V11038));
1887 /* If Layer-2 protocol is V.110, accept drivers with
1888 * transparent feature even if these don't support V.110
1889 * because we can emulate this in linklevel.
1891 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1892 if (USG_NONE(dev->usage[i]) &&
1893 (dev->drvmap[i] != -1)) {
1894 int d = dev->drvmap[i];
1895 if ((dev->usage[i] & ISDN_USAGE_EXCLUSIVE) &&
1896 ((pre_dev != d) || (pre_chan != dev->chanmap[i])))
1897 continue;
1898 if (!strcmp(isdn_map_eaz2msn(msn, d), "-"))
1899 continue;
1900 if (dev->usage[i] & ISDN_USAGE_DISABLED)
1901 continue; /* usage not allowed */
1902 if (dev->drv[d]->flags & DRV_FLAG_RUNNING) {
1903 if (((dev->drv[d]->interface->features & features) == features) ||
1904 (((dev->drv[d]->interface->features & vfeatures) == vfeatures) &&
1905 (dev->drv[d]->interface->features & ISDN_FEATURE_L2_TRANS))) {
1906 if ((pre_dev < 0) || (pre_chan < 0)) {
1907 dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1908 dev->usage[i] |= usage;
1909 isdn_info_update();
1910 return i;
1911 } else {
1912 if ((pre_dev == d) && (pre_chan == dev->chanmap[i])) {
1913 dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1914 dev->usage[i] |= usage;
1915 isdn_info_update();
1916 return i;
1922 return -1;
1926 * Set state of ISDN-channel to 'unused'
1928 void
1929 isdn_free_channel(int di, int ch, int usage)
1931 int i;
1933 if ((di < 0) || (ch < 0)) {
1934 printk(KERN_WARNING "%s: called with invalid drv(%d) or channel(%d)\n",
1935 __FUNCTION__, di, ch);
1936 return;
1938 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1939 if (((!usage) || ((dev->usage[i] & ISDN_USAGE_MASK) == usage)) &&
1940 (dev->drvmap[i] == di) &&
1941 (dev->chanmap[i] == ch)) {
1942 dev->usage[i] &= (ISDN_USAGE_NONE | ISDN_USAGE_EXCLUSIVE);
1943 strcpy(dev->num[i], "???");
1944 dev->ibytes[i] = 0;
1945 dev->obytes[i] = 0;
1946 // 20.10.99 JIM, try to reinitialize v110 !
1947 dev->v110emu[i] = 0;
1948 atomic_set(&(dev->v110use[i]), 0);
1949 isdn_v110_close(dev->v110[i]);
1950 dev->v110[i] = NULL;
1951 // 20.10.99 JIM, try to reinitialize v110 !
1952 isdn_info_update();
1953 if (dev->drv[di])
1954 skb_queue_purge(&dev->drv[di]->rpqueue[ch]);
1959 * Cancel Exclusive-Flag for ISDN-channel
1961 void
1962 isdn_unexclusive_channel(int di, int ch)
1964 int i;
1966 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1967 if ((dev->drvmap[i] == di) &&
1968 (dev->chanmap[i] == ch)) {
1969 dev->usage[i] &= ~ISDN_USAGE_EXCLUSIVE;
1970 isdn_info_update();
1971 return;
1976 * writebuf replacement for SKB_ABLE drivers
1978 static int
1979 isdn_writebuf_stub(int drvidx, int chan, const u_char __user * buf, int len)
1981 int ret;
1982 int hl = dev->drv[drvidx]->interface->hl_hdrlen;
1983 struct sk_buff *skb = alloc_skb(hl + len, GFP_ATOMIC);
1985 if (!skb)
1986 return -ENOMEM;
1987 skb_reserve(skb, hl);
1988 if (copy_from_user(skb_put(skb, len), buf, len))
1989 return -EFAULT;
1990 ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, 1, skb);
1991 if (ret <= 0)
1992 dev_kfree_skb(skb);
1993 if (ret > 0)
1994 dev->obytes[isdn_dc2minor(drvidx, chan)] += ret;
1995 return ret;
1999 * Return: length of data on success, -ERRcode on failure.
2002 isdn_writebuf_skb_stub(int drvidx, int chan, int ack, struct sk_buff *skb)
2004 int ret;
2005 struct sk_buff *nskb = NULL;
2006 int v110_ret = skb->len;
2007 int idx = isdn_dc2minor(drvidx, chan);
2009 if (dev->v110[idx]) {
2010 atomic_inc(&dev->v110use[idx]);
2011 nskb = isdn_v110_encode(dev->v110[idx], skb);
2012 atomic_dec(&dev->v110use[idx]);
2013 if (!nskb)
2014 return 0;
2015 v110_ret = *((int *)nskb->data);
2016 skb_pull(nskb, sizeof(int));
2017 if (!nskb->len) {
2018 dev_kfree_skb(nskb);
2019 return v110_ret;
2021 /* V.110 must always be acknowledged */
2022 ack = 1;
2023 ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, nskb);
2024 } else {
2025 int hl = dev->drv[drvidx]->interface->hl_hdrlen;
2027 if( skb_headroom(skb) < hl ){
2029 * This should only occur when new HL driver with
2030 * increased hl_hdrlen was loaded after netdevice
2031 * was created and connected to the new driver.
2033 * The V.110 branch (re-allocates on its own) does
2034 * not need this
2036 struct sk_buff * skb_tmp;
2038 skb_tmp = skb_realloc_headroom(skb, hl);
2039 printk(KERN_DEBUG "isdn_writebuf_skb_stub: reallocating headroom%s\n", skb_tmp ? "" : " failed");
2040 if (!skb_tmp) return -ENOMEM; /* 0 better? */
2041 ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb_tmp);
2042 if( ret > 0 ){
2043 dev_kfree_skb(skb);
2044 } else {
2045 dev_kfree_skb(skb_tmp);
2047 } else {
2048 ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb);
2051 if (ret > 0) {
2052 dev->obytes[idx] += ret;
2053 if (dev->v110[idx]) {
2054 atomic_inc(&dev->v110use[idx]);
2055 dev->v110[idx]->skbuser++;
2056 atomic_dec(&dev->v110use[idx]);
2057 /* For V.110 return unencoded data length */
2058 ret = v110_ret;
2059 /* if the complete frame was send we free the skb;
2060 if not upper function will requeue the skb */
2061 if (ret == skb->len)
2062 dev_kfree_skb(skb);
2064 } else
2065 if (dev->v110[idx])
2066 dev_kfree_skb(nskb);
2067 return ret;
2070 static int
2071 isdn_add_channels(isdn_driver_t *d, int drvidx, int n, int adding)
2073 int j, k, m;
2075 init_waitqueue_head(&d->st_waitq);
2076 if (d->flags & DRV_FLAG_RUNNING)
2077 return -1;
2078 if (n < 1) return 0;
2080 m = (adding) ? d->channels + n : n;
2082 if (dev->channels + n > ISDN_MAX_CHANNELS) {
2083 printk(KERN_WARNING "register_isdn: Max. %d channels supported\n",
2084 ISDN_MAX_CHANNELS);
2085 return -1;
2088 if ((adding) && (d->rcverr))
2089 kfree(d->rcverr);
2090 if (!(d->rcverr = kzalloc(sizeof(int) * m, GFP_ATOMIC))) {
2091 printk(KERN_WARNING "register_isdn: Could not alloc rcverr\n");
2092 return -1;
2095 if ((adding) && (d->rcvcount))
2096 kfree(d->rcvcount);
2097 if (!(d->rcvcount = kzalloc(sizeof(int) * m, GFP_ATOMIC))) {
2098 printk(KERN_WARNING "register_isdn: Could not alloc rcvcount\n");
2099 if (!adding)
2100 kfree(d->rcverr);
2101 return -1;
2104 if ((adding) && (d->rpqueue)) {
2105 for (j = 0; j < d->channels; j++)
2106 skb_queue_purge(&d->rpqueue[j]);
2107 kfree(d->rpqueue);
2109 if (!(d->rpqueue = kmalloc(sizeof(struct sk_buff_head) * m, GFP_ATOMIC))) {
2110 printk(KERN_WARNING "register_isdn: Could not alloc rpqueue\n");
2111 if (!adding) {
2112 kfree(d->rcvcount);
2113 kfree(d->rcverr);
2115 return -1;
2117 for (j = 0; j < m; j++) {
2118 skb_queue_head_init(&d->rpqueue[j]);
2121 if ((adding) && (d->rcv_waitq))
2122 kfree(d->rcv_waitq);
2123 d->rcv_waitq = kmalloc(sizeof(wait_queue_head_t) * 2 * m, GFP_ATOMIC);
2124 if (!d->rcv_waitq) {
2125 printk(KERN_WARNING "register_isdn: Could not alloc rcv_waitq\n");
2126 if (!adding) {
2127 kfree(d->rpqueue);
2128 kfree(d->rcvcount);
2129 kfree(d->rcverr);
2131 return -1;
2133 d->snd_waitq = d->rcv_waitq + m;
2134 for (j = 0; j < m; j++) {
2135 init_waitqueue_head(&d->rcv_waitq[j]);
2136 init_waitqueue_head(&d->snd_waitq[j]);
2139 dev->channels += n;
2140 for (j = d->channels; j < m; j++)
2141 for (k = 0; k < ISDN_MAX_CHANNELS; k++)
2142 if (dev->chanmap[k] < 0) {
2143 dev->chanmap[k] = j;
2144 dev->drvmap[k] = drvidx;
2145 break;
2147 d->channels = m;
2148 return 0;
2152 * Low-level-driver registration
2155 static void
2156 set_global_features(void)
2158 int drvidx;
2160 dev->global_features = 0;
2161 for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++) {
2162 if (!dev->drv[drvidx])
2163 continue;
2164 if (dev->drv[drvidx]->interface)
2165 dev->global_features |= dev->drv[drvidx]->interface->features;
2169 #ifdef CONFIG_ISDN_DIVERSION
2171 static char *map_drvname(int di)
2173 if ((di < 0) || (di >= ISDN_MAX_DRIVERS))
2174 return(NULL);
2175 return(dev->drvid[di]); /* driver name */
2176 } /* map_drvname */
2178 static int map_namedrv(char *id)
2179 { int i;
2181 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
2182 { if (!strcmp(dev->drvid[i],id))
2183 return(i);
2185 return(-1);
2186 } /* map_namedrv */
2188 int DIVERT_REG_NAME(isdn_divert_if *i_div)
2190 if (i_div->if_magic != DIVERT_IF_MAGIC)
2191 return(DIVERT_VER_ERR);
2192 switch (i_div->cmd)
2194 case DIVERT_CMD_REL:
2195 if (divert_if != i_div)
2196 return(DIVERT_REL_ERR);
2197 divert_if = NULL; /* free interface */
2198 return(DIVERT_NO_ERR);
2200 case DIVERT_CMD_REG:
2201 if (divert_if)
2202 return(DIVERT_REG_ERR);
2203 i_div->ll_cmd = isdn_command; /* set command function */
2204 i_div->drv_to_name = map_drvname;
2205 i_div->name_to_drv = map_namedrv;
2206 divert_if = i_div; /* remember interface */
2207 return(DIVERT_NO_ERR);
2209 default:
2210 return(DIVERT_CMD_ERR);
2212 } /* DIVERT_REG_NAME */
2214 EXPORT_SYMBOL(DIVERT_REG_NAME);
2216 #endif /* CONFIG_ISDN_DIVERSION */
2219 EXPORT_SYMBOL(register_isdn);
2220 #ifdef CONFIG_ISDN_PPP
2221 EXPORT_SYMBOL(isdn_ppp_register_compressor);
2222 EXPORT_SYMBOL(isdn_ppp_unregister_compressor);
2223 #endif
2226 register_isdn(isdn_if * i)
2228 isdn_driver_t *d;
2229 int j;
2230 ulong flags;
2231 int drvidx;
2233 if (dev->drivers >= ISDN_MAX_DRIVERS) {
2234 printk(KERN_WARNING "register_isdn: Max. %d drivers supported\n",
2235 ISDN_MAX_DRIVERS);
2236 return 0;
2238 if (!i->writebuf_skb) {
2239 printk(KERN_WARNING "register_isdn: No write routine given.\n");
2240 return 0;
2242 if (!(d = kzalloc(sizeof(isdn_driver_t), GFP_KERNEL))) {
2243 printk(KERN_WARNING "register_isdn: Could not alloc driver-struct\n");
2244 return 0;
2247 d->maxbufsize = i->maxbufsize;
2248 d->pktcount = 0;
2249 d->stavail = 0;
2250 d->flags = DRV_FLAG_LOADED;
2251 d->online = 0;
2252 d->interface = i;
2253 d->channels = 0;
2254 spin_lock_irqsave(&dev->lock, flags);
2255 for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++)
2256 if (!dev->drv[drvidx])
2257 break;
2258 if (isdn_add_channels(d, drvidx, i->channels, 0)) {
2259 spin_unlock_irqrestore(&dev->lock, flags);
2260 kfree(d);
2261 return 0;
2263 i->channels = drvidx;
2264 i->rcvcallb_skb = isdn_receive_skb_callback;
2265 i->statcallb = isdn_status_callback;
2266 if (!strlen(i->id))
2267 sprintf(i->id, "line%d", drvidx);
2268 for (j = 0; j < drvidx; j++)
2269 if (!strcmp(i->id, dev->drvid[j]))
2270 sprintf(i->id, "line%d", drvidx);
2271 dev->drv[drvidx] = d;
2272 strcpy(dev->drvid[drvidx], i->id);
2273 isdn_info_update();
2274 dev->drivers++;
2275 set_global_features();
2276 spin_unlock_irqrestore(&dev->lock, flags);
2277 return 1;
2281 *****************************************************************************
2282 * And now the modules code.
2283 *****************************************************************************
2286 static char *
2287 isdn_getrev(const char *revision)
2289 char *rev;
2290 char *p;
2292 if ((p = strchr(revision, ':'))) {
2293 rev = p + 2;
2294 p = strchr(rev, '$');
2295 *--p = 0;
2296 } else
2297 rev = "???";
2298 return rev;
2302 * Allocate and initialize all data, register modem-devices
2304 static int __init isdn_init(void)
2306 int i;
2307 char tmprev[50];
2309 if (!(dev = vmalloc(sizeof(isdn_dev)))) {
2310 printk(KERN_WARNING "isdn: Could not allocate device-struct.\n");
2311 return -EIO;
2313 memset((char *) dev, 0, sizeof(isdn_dev));
2314 init_timer(&dev->timer);
2315 dev->timer.function = isdn_timer_funct;
2316 spin_lock_init(&dev->lock);
2317 spin_lock_init(&dev->timerlock);
2318 #ifdef MODULE
2319 dev->owner = THIS_MODULE;
2320 #endif
2321 mutex_init(&dev->mtx);
2322 init_waitqueue_head(&dev->info_waitq);
2323 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2324 dev->drvmap[i] = -1;
2325 dev->chanmap[i] = -1;
2326 dev->m_idx[i] = -1;
2327 strcpy(dev->num[i], "???");
2328 init_waitqueue_head(&dev->mdm.info[i].open_wait);
2329 init_waitqueue_head(&dev->mdm.info[i].close_wait);
2331 if (register_chrdev(ISDN_MAJOR, "isdn", &isdn_fops)) {
2332 printk(KERN_WARNING "isdn: Could not register control devices\n");
2333 vfree(dev);
2334 return -EIO;
2336 if ((isdn_tty_modem_init()) < 0) {
2337 printk(KERN_WARNING "isdn: Could not register tty devices\n");
2338 vfree(dev);
2339 unregister_chrdev(ISDN_MAJOR, "isdn");
2340 return -EIO;
2342 #ifdef CONFIG_ISDN_PPP
2343 if (isdn_ppp_init() < 0) {
2344 printk(KERN_WARNING "isdn: Could not create PPP-device-structs\n");
2345 isdn_tty_exit();
2346 unregister_chrdev(ISDN_MAJOR, "isdn");
2347 vfree(dev);
2348 return -EIO;
2350 #endif /* CONFIG_ISDN_PPP */
2352 strcpy(tmprev, isdn_revision);
2353 printk(KERN_NOTICE "ISDN subsystem Rev: %s/", isdn_getrev(tmprev));
2354 strcpy(tmprev, isdn_tty_revision);
2355 printk("%s/", isdn_getrev(tmprev));
2356 strcpy(tmprev, isdn_net_revision);
2357 printk("%s/", isdn_getrev(tmprev));
2358 strcpy(tmprev, isdn_ppp_revision);
2359 printk("%s/", isdn_getrev(tmprev));
2360 strcpy(tmprev, isdn_audio_revision);
2361 printk("%s/", isdn_getrev(tmprev));
2362 strcpy(tmprev, isdn_v110_revision);
2363 printk("%s", isdn_getrev(tmprev));
2365 #ifdef MODULE
2366 printk(" loaded\n");
2367 #else
2368 printk("\n");
2369 #endif
2370 isdn_info_update();
2371 return 0;
2375 * Unload module
2377 static void __exit isdn_exit(void)
2379 #ifdef CONFIG_ISDN_PPP
2380 isdn_ppp_cleanup();
2381 #endif
2382 if (isdn_net_rmall() < 0) {
2383 printk(KERN_WARNING "isdn: net-device busy, remove cancelled\n");
2384 return;
2386 isdn_tty_exit();
2387 unregister_chrdev(ISDN_MAJOR, "isdn");
2388 del_timer(&dev->timer);
2389 /* call vfree with interrupts enabled, else it will hang */
2390 vfree(dev);
2391 printk(KERN_NOTICE "ISDN-subsystem unloaded\n");
2394 module_init(isdn_init);
2395 module_exit(isdn_exit);