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