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