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