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