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