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