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