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