Pull esi-support into release branch
[linux-2.6] / drivers / isdn / i4l / isdn_common.c
1 /* $Id: isdn_common.c,v 1.1.2.3 2004/02/10 01:07:13 keil Exp $
2  *
3  * Linux ISDN subsystem, common used functions (linklevel).
4  *
5  * Copyright 1994-1999  by Fritz Elfert (fritz@isdn4linux.de)
6  * Copyright 1995,96    Thinking Objects Software GmbH Wuerzburg
7  * Copyright 1995,96    by Michael Hipp (Michael.Hipp@student.uni-tuebingen.de)
8  *
9  * This software may be used and distributed according to the terms
10  * of the GNU General Public License, incorporated herein by reference.
11  *
12  */
13
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/poll.h>
17 #include <linux/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                         memcpy(cp, skb->data, 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_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->
1138                                 readstat(buf, count, drvidx,
1139                                          isdn_minor2chan(minor));
1140                 } else {
1141                         len = 0;
1142                 }
1143                 if (len)
1144                         dev->drv[drvidx]->stavail -= len;
1145                 else
1146                         dev->drv[drvidx]->stavail = 0;
1147                 *off += len;
1148                 retval = len;
1149                 goto out;
1150         }
1151 #ifdef CONFIG_ISDN_PPP
1152         if (minor <= ISDN_MINOR_PPPMAX) {
1153                 retval = isdn_ppp_read(minor - ISDN_MINOR_PPP, file, buf, count);
1154                 goto out;
1155         }
1156 #endif
1157         retval = -ENODEV;
1158  out:
1159         unlock_kernel();
1160         return retval;
1161 }
1162
1163 static ssize_t
1164 isdn_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
1165 {
1166         uint minor = iminor(file->f_dentry->d_inode);
1167         int drvidx;
1168         int chidx;
1169         int retval;
1170
1171         if (minor == ISDN_MINOR_STATUS)
1172                 return -EPERM;
1173         if (!dev->drivers)
1174                 return -ENODEV;
1175
1176         lock_kernel();
1177         if (minor <= ISDN_MINOR_BMAX) {
1178                 printk(KERN_WARNING "isdn_write minor %d obsolete!\n", minor);
1179                 drvidx = isdn_minor2drv(minor);
1180                 if (drvidx < 0) {
1181                         retval = -ENODEV;
1182                         goto out;
1183                 }
1184                 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
1185                         retval = -ENODEV;
1186                         goto out;
1187                 }
1188                 chidx = isdn_minor2chan(minor);
1189                 while ((retval = isdn_writebuf_stub(drvidx, chidx, buf, count)) == 0)
1190                         interruptible_sleep_on(&dev->drv[drvidx]->snd_waitq[chidx]);
1191                 goto out;
1192         }
1193         if (minor <= ISDN_MINOR_CTRLMAX) {
1194                 drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1195                 if (drvidx < 0) {
1196                         retval = -ENODEV;
1197                         goto out;
1198                 }
1199                 /*
1200                  * We want to use the isdnctrl device to load the firmware
1201                  *
1202                  if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1203                  return -ENODEV;
1204                  */
1205                 if (dev->drv[drvidx]->interface->writecmd)
1206                         retval = dev->drv[drvidx]->interface->
1207                                 writecmd(buf, count, drvidx, isdn_minor2chan(minor));
1208                 else
1209                         retval = count;
1210                 goto out;
1211         }
1212 #ifdef CONFIG_ISDN_PPP
1213         if (minor <= ISDN_MINOR_PPPMAX) {
1214                 retval = isdn_ppp_write(minor - ISDN_MINOR_PPP, file, buf, count);
1215                 goto out;
1216         }
1217 #endif
1218         retval = -ENODEV;
1219  out:
1220         unlock_kernel();
1221         return retval;
1222 }
1223
1224 static unsigned int
1225 isdn_poll(struct file *file, poll_table * wait)
1226 {
1227         unsigned int mask = 0;
1228         unsigned int minor = iminor(file->f_dentry->d_inode);
1229         int drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1230
1231         lock_kernel();
1232         if (minor == ISDN_MINOR_STATUS) {
1233                 poll_wait(file, &(dev->info_waitq), wait);
1234                 /* mask = POLLOUT | POLLWRNORM; */
1235                 if (file->private_data) {
1236                         mask |= POLLIN | POLLRDNORM;
1237                 }
1238                 goto out;
1239         }
1240         if (minor >= ISDN_MINOR_CTRL && minor <= ISDN_MINOR_CTRLMAX) {
1241                 if (drvidx < 0) {
1242                         /* driver deregistered while file open */
1243                         mask = POLLHUP;
1244                         goto out;
1245                 }
1246                 poll_wait(file, &(dev->drv[drvidx]->st_waitq), wait);
1247                 mask = POLLOUT | POLLWRNORM;
1248                 if (dev->drv[drvidx]->stavail) {
1249                         mask |= POLLIN | POLLRDNORM;
1250                 }
1251                 goto out;
1252         }
1253 #ifdef CONFIG_ISDN_PPP
1254         if (minor <= ISDN_MINOR_PPPMAX) {
1255                 mask = isdn_ppp_poll(file, wait);
1256                 goto out;
1257         }
1258 #endif
1259         mask = POLLERR;
1260  out:
1261         unlock_kernel();
1262         return mask;
1263 }
1264
1265
1266 static int
1267 isdn_ioctl(struct inode *inode, struct file *file, uint cmd, ulong arg)
1268 {
1269         uint minor = iminor(inode);
1270         isdn_ctrl c;
1271         int drvidx;
1272         int chidx;
1273         int ret;
1274         int i;
1275         char __user *p;
1276         char *s;
1277         union iocpar {
1278                 char name[10];
1279                 char bname[22];
1280                 isdn_ioctl_struct iocts;
1281                 isdn_net_ioctl_phone phone;
1282                 isdn_net_ioctl_cfg cfg;
1283         } iocpar;
1284         void __user *argp = (void __user *)arg;
1285
1286 #define name  iocpar.name
1287 #define bname iocpar.bname
1288 #define iocts iocpar.iocts
1289 #define phone iocpar.phone
1290 #define cfg   iocpar.cfg
1291
1292         if (minor == ISDN_MINOR_STATUS) {
1293                 switch (cmd) {
1294                         case IIOCGETDVR:
1295                                 return (TTY_DV +
1296                                         (NET_DV << 8) +
1297                                         (INF_DV << 16));
1298                         case IIOCGETCPS:
1299                                 if (arg) {
1300                                         ulong __user *p = argp;
1301                                         int i;
1302                                         if (!access_ok(VERIFY_WRITE, p,
1303                                                         sizeof(ulong) * ISDN_MAX_CHANNELS * 2))
1304                                                 return -EFAULT;
1305                                         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1306                                                 put_user(dev->ibytes[i], p++);
1307                                                 put_user(dev->obytes[i], p++);
1308                                         }
1309                                         return 0;
1310                                 } else
1311                                         return -EINVAL;
1312                                 break;
1313 #ifdef CONFIG_NETDEVICES
1314                         case IIOCNETGPN:
1315                                 /* Get peer phone number of a connected 
1316                                  * isdn network interface */
1317                                 if (arg) {
1318                                         if (copy_from_user(&phone, argp, sizeof(phone)))
1319                                                 return -EFAULT;
1320                                         return isdn_net_getpeer(&phone, argp);
1321                                 } else
1322                                         return -EINVAL;
1323 #endif
1324                         default:
1325                                 return -EINVAL;
1326                 }
1327         }
1328         if (!dev->drivers)
1329                 return -ENODEV;
1330         if (minor <= ISDN_MINOR_BMAX) {
1331                 drvidx = isdn_minor2drv(minor);
1332                 if (drvidx < 0)
1333                         return -ENODEV;
1334                 chidx = isdn_minor2chan(minor);
1335                 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1336                         return -ENODEV;
1337                 return 0;
1338         }
1339         if (minor <= ISDN_MINOR_CTRLMAX) {
1340 /*
1341  * isdn net devices manage lots of configuration variables as linked lists.
1342  * Those lists must only be manipulated from user space. Some of the ioctl's
1343  * service routines access user space and are not atomic. Therefor, ioctl's
1344  * manipulating the lists and ioctl's sleeping while accessing the lists
1345  * are serialized by means of a semaphore.
1346  */
1347                 switch (cmd) {
1348                         case IIOCNETDWRSET:
1349                                 printk(KERN_INFO "INFO: ISDN_DW_ABC_EXTENSION not enabled\n");
1350                                 return(-EINVAL);
1351                         case IIOCNETLCR:
1352                                 printk(KERN_INFO "INFO: ISDN_ABC_LCR_SUPPORT not enabled\n");
1353                                 return -ENODEV;
1354 #ifdef CONFIG_NETDEVICES
1355                         case IIOCNETAIF:
1356                                 /* Add a network-interface */
1357                                 if (arg) {
1358                                         if (copy_from_user(name, argp, sizeof(name)))
1359                                                 return -EFAULT;
1360                                         s = name;
1361                                 } else {
1362                                         s = NULL;
1363                                 }
1364                                 ret = down_interruptible(&dev->sem);
1365                                 if( ret ) return ret;
1366                                 if ((s = isdn_net_new(s, NULL))) {
1367                                         if (copy_to_user(argp, s, strlen(s) + 1)){
1368                                                 ret = -EFAULT;
1369                                         } else {
1370                                                 ret = 0;
1371                                         }
1372                                 } else
1373                                         ret = -ENODEV;
1374                                 up(&dev->sem);
1375                                 return ret;
1376                         case IIOCNETASL:
1377                                 /* Add a slave to a network-interface */
1378                                 if (arg) {
1379                                         if (copy_from_user(bname, argp, sizeof(bname) - 1))
1380                                                 return -EFAULT;
1381                                 } else
1382                                         return -EINVAL;
1383                                 ret = down_interruptible(&dev->sem);
1384                                 if( ret ) return ret;
1385                                 if ((s = isdn_net_newslave(bname))) {
1386                                         if (copy_to_user(argp, s, strlen(s) + 1)){
1387                                                 ret = -EFAULT;
1388                                         } else {
1389                                                 ret = 0;
1390                                         }
1391                                 } else
1392                                         ret = -ENODEV;
1393                                 up(&dev->sem);
1394                                 return ret;
1395                         case IIOCNETDIF:
1396                                 /* Delete a network-interface */
1397                                 if (arg) {
1398                                         if (copy_from_user(name, argp, sizeof(name)))
1399                                                 return -EFAULT;
1400                                         ret = down_interruptible(&dev->sem);
1401                                         if( ret ) return ret;
1402                                         ret = isdn_net_rm(name);
1403                                         up(&dev->sem);
1404                                         return ret;
1405                                 } else
1406                                         return -EINVAL;
1407                         case IIOCNETSCF:
1408                                 /* Set configurable parameters of a network-interface */
1409                                 if (arg) {
1410                                         if (copy_from_user(&cfg, argp, sizeof(cfg)))
1411                                                 return -EFAULT;
1412                                         return isdn_net_setcfg(&cfg);
1413                                 } else
1414                                         return -EINVAL;
1415                         case IIOCNETGCF:
1416                                 /* Get configurable parameters of a network-interface */
1417                                 if (arg) {
1418                                         if (copy_from_user(&cfg, argp, sizeof(cfg)))
1419                                                 return -EFAULT;
1420                                         if (!(ret = isdn_net_getcfg(&cfg))) {
1421                                                 if (copy_to_user(argp, &cfg, sizeof(cfg)))
1422                                                         return -EFAULT;
1423                                         }
1424                                         return ret;
1425                                 } else
1426                                         return -EINVAL;
1427                         case IIOCNETANM:
1428                                 /* Add a phone-number to a network-interface */
1429                                 if (arg) {
1430                                         if (copy_from_user(&phone, argp, sizeof(phone)))
1431                                                 return -EFAULT;
1432                                         ret = down_interruptible(&dev->sem);
1433                                         if( ret ) return ret;
1434                                         ret = isdn_net_addphone(&phone);
1435                                         up(&dev->sem);
1436                                         return ret;
1437                                 } else
1438                                         return -EINVAL;
1439                         case IIOCNETGNM:
1440                                 /* Get list of phone-numbers of a network-interface */
1441                                 if (arg) {
1442                                         if (copy_from_user(&phone, argp, sizeof(phone)))
1443                                                 return -EFAULT;
1444                                         ret = down_interruptible(&dev->sem);
1445                                         if( ret ) return ret;
1446                                         ret = isdn_net_getphones(&phone, argp);
1447                                         up(&dev->sem);
1448                                         return ret;
1449                                 } else
1450                                         return -EINVAL;
1451                         case IIOCNETDNM:
1452                                 /* Delete a phone-number of a network-interface */
1453                                 if (arg) {
1454                                         if (copy_from_user(&phone, argp, sizeof(phone)))
1455                                                 return -EFAULT;
1456                                         ret = down_interruptible(&dev->sem);
1457                                         if( ret ) return ret;
1458                                         ret = isdn_net_delphone(&phone);
1459                                         up(&dev->sem);
1460                                         return ret;
1461                                 } else
1462                                         return -EINVAL;
1463                         case IIOCNETDIL:
1464                                 /* Force dialing of a network-interface */
1465                                 if (arg) {
1466                                         if (copy_from_user(name, argp, sizeof(name)))
1467                                                 return -EFAULT;
1468                                         return isdn_net_force_dial(name);
1469                                 } else
1470                                         return -EINVAL;
1471 #ifdef CONFIG_ISDN_PPP
1472                         case IIOCNETALN:
1473                                 if (!arg)
1474                                         return -EINVAL;
1475                                 if (copy_from_user(name, argp, sizeof(name)))
1476                                         return -EFAULT;
1477                                 return isdn_ppp_dial_slave(name);
1478                         case IIOCNETDLN:
1479                                 if (!arg)
1480                                         return -EINVAL;
1481                                 if (copy_from_user(name, argp, sizeof(name)))
1482                                         return -EFAULT;
1483                                 return isdn_ppp_hangup_slave(name);
1484 #endif
1485                         case IIOCNETHUP:
1486                                 /* Force hangup of a network-interface */
1487                                 if (!arg)
1488                                         return -EINVAL;
1489                                 if (copy_from_user(name, argp, sizeof(name)))
1490                                         return -EFAULT;
1491                                 return isdn_net_force_hangup(name);
1492                                 break;
1493 #endif                          /* CONFIG_NETDEVICES */
1494                         case IIOCSETVER:
1495                                 dev->net_verbose = arg;
1496                                 printk(KERN_INFO "isdn: Verbose-Level is %d\n", dev->net_verbose);
1497                                 return 0;
1498                         case IIOCSETGST:
1499                                 if (arg)
1500                                         dev->global_flags |= ISDN_GLOBAL_STOPPED;
1501                                 else
1502                                         dev->global_flags &= ~ISDN_GLOBAL_STOPPED;
1503                                 printk(KERN_INFO "isdn: Global Mode %s\n",
1504                                        (dev->global_flags & ISDN_GLOBAL_STOPPED) ? "stopped" : "running");
1505                                 return 0;
1506                         case IIOCSETBRJ:
1507                                 drvidx = -1;
1508                                 if (arg) {
1509                                         int i;
1510                                         char *p;
1511                                         if (copy_from_user(&iocts, argp,
1512                                              sizeof(isdn_ioctl_struct)))
1513                                                 return -EFAULT;
1514                                         if (strlen(iocts.drvid)) {
1515                                                 if ((p = strchr(iocts.drvid, ',')))
1516                                                         *p = 0;
1517                                                 drvidx = -1;
1518                                                 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1519                                                         if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1520                                                                 drvidx = i;
1521                                                                 break;
1522                                                         }
1523                                         }
1524                                 }
1525                                 if (drvidx == -1)
1526                                         return -ENODEV;
1527                                 if (iocts.arg)
1528                                         dev->drv[drvidx]->flags |= DRV_FLAG_REJBUS;
1529                                 else
1530                                         dev->drv[drvidx]->flags &= ~DRV_FLAG_REJBUS;
1531                                 return 0;
1532                         case IIOCSIGPRF:
1533                                 dev->profd = current;
1534                                 return 0;
1535                                 break;
1536                         case IIOCGETPRF:
1537                                 /* Get all Modem-Profiles */
1538                                 if (arg) {
1539                                         char __user *p = argp;
1540                                         int i;
1541
1542                                         if (!access_ok(VERIFY_WRITE, argp,
1543                                         (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
1544                                                    * ISDN_MAX_CHANNELS))
1545                                                 return -EFAULT;
1546
1547                                         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1548                                                 if (copy_to_user(p, dev->mdm.info[i].emu.profile,
1549                                                       ISDN_MODEM_NUMREG))
1550                                                         return -EFAULT;
1551                                                 p += ISDN_MODEM_NUMREG;
1552                                                 if (copy_to_user(p, dev->mdm.info[i].emu.pmsn, ISDN_MSNLEN))
1553                                                         return -EFAULT;
1554                                                 p += ISDN_MSNLEN;
1555                                                 if (copy_to_user(p, dev->mdm.info[i].emu.plmsn, ISDN_LMSNLEN))
1556                                                         return -EFAULT;
1557                                                 p += ISDN_LMSNLEN;
1558                                         }
1559                                         return (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN) * ISDN_MAX_CHANNELS;
1560                                 } else
1561                                         return -EINVAL;
1562                                 break;
1563                         case IIOCSETPRF:
1564                                 /* Set all Modem-Profiles */
1565                                 if (arg) {
1566                                         char __user *p = argp;
1567                                         int i;
1568
1569                                         if (!access_ok(VERIFY_READ, argp,
1570                                         (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
1571                                                    * ISDN_MAX_CHANNELS))
1572                                                 return -EFAULT;
1573
1574                                         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1575                                                 if (copy_from_user(dev->mdm.info[i].emu.profile, p,
1576                                                      ISDN_MODEM_NUMREG))
1577                                                         return -EFAULT;
1578                                                 p += ISDN_MODEM_NUMREG;
1579                                                 if (copy_from_user(dev->mdm.info[i].emu.plmsn, p, ISDN_LMSNLEN))
1580                                                         return -EFAULT;
1581                                                 p += ISDN_LMSNLEN;
1582                                                 if (copy_from_user(dev->mdm.info[i].emu.pmsn, p, ISDN_MSNLEN))
1583                                                         return -EFAULT;
1584                                                 p += ISDN_MSNLEN;
1585                                         }
1586                                         return 0;
1587                                 } else
1588                                         return -EINVAL;
1589                                 break;
1590                         case IIOCSETMAP:
1591                         case IIOCGETMAP:
1592                                 /* Set/Get MSN->EAZ-Mapping for a driver */
1593                                 if (arg) {
1594
1595                                         if (copy_from_user(&iocts, argp,
1596                                              sizeof(isdn_ioctl_struct)))
1597                                                 return -EFAULT;
1598                                         if (strlen(iocts.drvid)) {
1599                                                 drvidx = -1;
1600                                                 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1601                                                         if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1602                                                                 drvidx = i;
1603                                                                 break;
1604                                                         }
1605                                         } else
1606                                                 drvidx = 0;
1607                                         if (drvidx == -1)
1608                                                 return -ENODEV;
1609                                         if (cmd == IIOCSETMAP) {
1610                                                 int loop = 1;
1611
1612                                                 p = (char __user *) iocts.arg;
1613                                                 i = 0;
1614                                                 while (loop) {
1615                                                         int j = 0;
1616
1617                                                         while (1) {
1618                                                                 if (!access_ok(VERIFY_READ, p, 1))
1619                                                                         return -EFAULT;
1620                                                                 get_user(bname[j], p++);
1621                                                                 switch (bname[j]) {
1622                                                                         case '\0':
1623                                                                                 loop = 0;
1624                                                                                 /* Fall through */
1625                                                                         case ',':
1626                                                                                 bname[j] = '\0';
1627                                                                                 strcpy(dev->drv[drvidx]->msn2eaz[i], bname);
1628                                                                                 j = ISDN_MSNLEN;
1629                                                                                 break;
1630                                                                         default:
1631                                                                                 j++;
1632                                                                 }
1633                                                                 if (j >= ISDN_MSNLEN)
1634                                                                         break;
1635                                                         }
1636                                                         if (++i > 9)
1637                                                                 break;
1638                                                 }
1639                                         } else {
1640                                                 p = (char __user *) iocts.arg;
1641                                                 for (i = 0; i < 10; i++) {
1642                                                         sprintf(bname, "%s%s",
1643                                                                 strlen(dev->drv[drvidx]->msn2eaz[i]) ?
1644                                                                 dev->drv[drvidx]->msn2eaz[i] : "_",
1645                                                                 (i < 9) ? "," : "\0");
1646                                                         if (copy_to_user(p, bname, strlen(bname) + 1))
1647                                                                 return -EFAULT;
1648                                                         p += strlen(bname);
1649                                                 }
1650                                         }
1651                                         return 0;
1652                                 } else
1653                                         return -EINVAL;
1654                         case IIOCDBGVAR:
1655                                 if (arg) {
1656                                         if (copy_to_user(argp, &dev, sizeof(ulong)))
1657                                                 return -EFAULT;
1658                                         return 0;
1659                                 } else
1660                                         return -EINVAL;
1661                                 break;
1662                         default:
1663                                 if ((cmd & IIOCDRVCTL) == IIOCDRVCTL)
1664                                         cmd = ((cmd >> _IOC_NRSHIFT) & _IOC_NRMASK) & ISDN_DRVIOCTL_MASK;
1665                                 else
1666                                         return -EINVAL;
1667                                 if (arg) {
1668                                         int i;
1669                                         char *p;
1670                                         if (copy_from_user(&iocts, argp, sizeof(isdn_ioctl_struct)))
1671                                                 return -EFAULT;
1672                                         if (strlen(iocts.drvid)) {
1673                                                 if ((p = strchr(iocts.drvid, ',')))
1674                                                         *p = 0;
1675                                                 drvidx = -1;
1676                                                 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1677                                                         if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1678                                                                 drvidx = i;
1679                                                                 break;
1680                                                         }
1681                                         } else
1682                                                 drvidx = 0;
1683                                         if (drvidx == -1)
1684                                                 return -ENODEV;
1685                                         if (!access_ok(VERIFY_WRITE, argp,
1686                                              sizeof(isdn_ioctl_struct)))
1687                                                 return -EFAULT;
1688                                         c.driver = drvidx;
1689                                         c.command = ISDN_CMD_IOCTL;
1690                                         c.arg = cmd;
1691                                         memcpy(c.parm.num, &iocts.arg, sizeof(ulong));
1692                                         ret = isdn_command(&c);
1693                                         memcpy(&iocts.arg, c.parm.num, sizeof(ulong));
1694                                         if (copy_to_user(argp, &iocts, sizeof(isdn_ioctl_struct)))
1695                                                 return -EFAULT;
1696                                         return ret;
1697                                 } else
1698                                         return -EINVAL;
1699                 }
1700         }
1701 #ifdef CONFIG_ISDN_PPP
1702         if (minor <= ISDN_MINOR_PPPMAX)
1703                 return (isdn_ppp_ioctl(minor - ISDN_MINOR_PPP, file, cmd, arg));
1704 #endif
1705         return -ENODEV;
1706
1707 #undef name
1708 #undef bname
1709 #undef iocts
1710 #undef phone
1711 #undef cfg
1712 }
1713
1714 /*
1715  * Open the device code.
1716  */
1717 static int
1718 isdn_open(struct inode *ino, struct file *filep)
1719 {
1720         uint minor = iminor(ino);
1721         int drvidx;
1722         int chidx;
1723         int retval = -ENODEV;
1724
1725
1726         if (minor == ISDN_MINOR_STATUS) {
1727                 infostruct *p;
1728
1729                 if ((p = kmalloc(sizeof(infostruct), GFP_KERNEL))) {
1730                         p->next = (char *) dev->infochain;
1731                         p->private = (char *) &(filep->private_data);
1732                         dev->infochain = p;
1733                         /* At opening we allow a single update */
1734                         filep->private_data = (char *) 1;
1735                         retval = 0;
1736                         goto out;
1737                 } else {
1738                         retval = -ENOMEM;
1739                         goto out;
1740                 }
1741         }
1742         if (!dev->channels)
1743                 goto out;
1744         if (minor <= ISDN_MINOR_BMAX) {
1745                 printk(KERN_WARNING "isdn_open minor %d obsolete!\n", minor);
1746                 drvidx = isdn_minor2drv(minor);
1747                 if (drvidx < 0)
1748                         goto out;
1749                 chidx = isdn_minor2chan(minor);
1750                 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1751                         goto out;
1752                 if (!(dev->drv[drvidx]->online & (1 << chidx)))
1753                         goto out;
1754                 isdn_lock_drivers();
1755                 retval = 0;
1756                 goto out;
1757         }
1758         if (minor <= ISDN_MINOR_CTRLMAX) {
1759                 drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1760                 if (drvidx < 0)
1761                         goto out;
1762                 isdn_lock_drivers();
1763                 retval = 0;
1764                 goto out;
1765         }
1766 #ifdef CONFIG_ISDN_PPP
1767         if (minor <= ISDN_MINOR_PPPMAX) {
1768                 retval = isdn_ppp_open(minor - ISDN_MINOR_PPP, filep);
1769                 if (retval == 0)
1770                         isdn_lock_drivers();
1771                 goto out;
1772         }
1773 #endif
1774  out:
1775         nonseekable_open(ino, filep);
1776         return retval;
1777 }
1778
1779 static int
1780 isdn_close(struct inode *ino, struct file *filep)
1781 {
1782         uint minor = iminor(ino);
1783
1784         lock_kernel();
1785         if (minor == ISDN_MINOR_STATUS) {
1786                 infostruct *p = dev->infochain;
1787                 infostruct *q = NULL;
1788
1789                 while (p) {
1790                         if (p->private == (char *) &(filep->private_data)) {
1791                                 if (q)
1792                                         q->next = p->next;
1793                                 else
1794                                         dev->infochain = (infostruct *) (p->next);
1795                                 kfree(p);
1796                                 goto out;
1797                         }
1798                         q = p;
1799                         p = (infostruct *) (p->next);
1800                 }
1801                 printk(KERN_WARNING "isdn: No private data while closing isdnctrl\n");
1802                 goto out;
1803         }
1804         isdn_unlock_drivers();
1805         if (minor <= ISDN_MINOR_BMAX)
1806                 goto out;
1807         if (minor <= ISDN_MINOR_CTRLMAX) {
1808                 if (dev->profd == current)
1809                         dev->profd = NULL;
1810                 goto out;
1811         }
1812 #ifdef CONFIG_ISDN_PPP
1813         if (minor <= ISDN_MINOR_PPPMAX)
1814                 isdn_ppp_release(minor - ISDN_MINOR_PPP, filep);
1815 #endif
1816
1817  out:
1818         unlock_kernel();
1819         return 0;
1820 }
1821
1822 static struct file_operations isdn_fops =
1823 {
1824         .owner          = THIS_MODULE,
1825         .llseek         = no_llseek,
1826         .read           = isdn_read,
1827         .write          = isdn_write,
1828         .poll           = isdn_poll,
1829         .ioctl          = isdn_ioctl,
1830         .open           = isdn_open,
1831         .release        = isdn_close,
1832 };
1833
1834 char *
1835 isdn_map_eaz2msn(char *msn, int di)
1836 {
1837         isdn_driver_t *this = dev->drv[di];
1838         int i;
1839
1840         if (strlen(msn) == 1) {
1841                 i = msn[0] - '0';
1842                 if ((i >= 0) && (i <= 9))
1843                         if (strlen(this->msn2eaz[i]))
1844                                 return (this->msn2eaz[i]);
1845         }
1846         return (msn);
1847 }
1848
1849 /*
1850  * Find an unused ISDN-channel, whose feature-flags match the
1851  * given L2- and L3-protocols.
1852  */
1853 #define L2V (~(ISDN_FEATURE_L2_V11096|ISDN_FEATURE_L2_V11019|ISDN_FEATURE_L2_V11038))
1854
1855 /*
1856  * This function must be called with holding the dev->lock.
1857  */
1858 int
1859 isdn_get_free_channel(int usage, int l2_proto, int l3_proto, int pre_dev
1860                       ,int pre_chan, char *msn)
1861 {
1862         int i;
1863         ulong features;
1864         ulong vfeatures;
1865
1866         features = ((1 << l2_proto) | (0x10000 << l3_proto));
1867         vfeatures = (((1 << l2_proto) | (0x10000 << l3_proto)) &
1868                      ~(ISDN_FEATURE_L2_V11096|ISDN_FEATURE_L2_V11019|ISDN_FEATURE_L2_V11038));
1869         /* If Layer-2 protocol is V.110, accept drivers with
1870          * transparent feature even if these don't support V.110
1871          * because we can emulate this in linklevel.
1872          */
1873         for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1874                 if (USG_NONE(dev->usage[i]) &&
1875                     (dev->drvmap[i] != -1)) {
1876                         int d = dev->drvmap[i];
1877                         if ((dev->usage[i] & ISDN_USAGE_EXCLUSIVE) &&
1878                         ((pre_dev != d) || (pre_chan != dev->chanmap[i])))
1879                                 continue;
1880                         if (!strcmp(isdn_map_eaz2msn(msn, d), "-"))
1881                                 continue;
1882                         if (dev->usage[i] & ISDN_USAGE_DISABLED)
1883                                 continue; /* usage not allowed */
1884                         if (dev->drv[d]->flags & DRV_FLAG_RUNNING) {
1885                                 if (((dev->drv[d]->interface->features & features) == features) ||
1886                                     (((dev->drv[d]->interface->features & vfeatures) == vfeatures) &&
1887                                      (dev->drv[d]->interface->features & ISDN_FEATURE_L2_TRANS))) {
1888                                         if ((pre_dev < 0) || (pre_chan < 0)) {
1889                                                 dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1890                                                 dev->usage[i] |= usage;
1891                                                 isdn_info_update();
1892                                                 return i;
1893                                         } else {
1894                                                 if ((pre_dev == d) && (pre_chan == dev->chanmap[i])) {
1895                                                         dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1896                                                         dev->usage[i] |= usage;
1897                                                         isdn_info_update();
1898                                                         return i;
1899                                                 }
1900                                         }
1901                                 }
1902                         }
1903                 }
1904         return -1;
1905 }
1906
1907 /*
1908  * Set state of ISDN-channel to 'unused'
1909  */
1910 void
1911 isdn_free_channel(int di, int ch, int usage)
1912 {
1913         int i;
1914
1915         if ((di < 0) || (ch < 0)) {
1916                 printk(KERN_WARNING "%s: called with invalid drv(%d) or channel(%d)\n",
1917                         __FUNCTION__, di, ch);
1918                 return;
1919         }
1920         for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1921                 if (((!usage) || ((dev->usage[i] & ISDN_USAGE_MASK) == usage)) &&
1922                     (dev->drvmap[i] == di) &&
1923                     (dev->chanmap[i] == ch)) {
1924                         dev->usage[i] &= (ISDN_USAGE_NONE | ISDN_USAGE_EXCLUSIVE);
1925                         strcpy(dev->num[i], "???");
1926                         dev->ibytes[i] = 0;
1927                         dev->obytes[i] = 0;
1928 // 20.10.99 JIM, try to reinitialize v110 !
1929                         dev->v110emu[i] = 0;
1930                         atomic_set(&(dev->v110use[i]), 0);
1931                         isdn_v110_close(dev->v110[i]);
1932                         dev->v110[i] = NULL;
1933 // 20.10.99 JIM, try to reinitialize v110 !
1934                         isdn_info_update();
1935                         if (dev->drv[di])
1936                                 skb_queue_purge(&dev->drv[di]->rpqueue[ch]);
1937                 }
1938 }
1939
1940 /*
1941  * Cancel Exclusive-Flag for ISDN-channel
1942  */
1943 void
1944 isdn_unexclusive_channel(int di, int ch)
1945 {
1946         int i;
1947
1948         for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1949                 if ((dev->drvmap[i] == di) &&
1950                     (dev->chanmap[i] == ch)) {
1951                         dev->usage[i] &= ~ISDN_USAGE_EXCLUSIVE;
1952                         isdn_info_update();
1953                         return;
1954                 }
1955 }
1956
1957 /*
1958  *  writebuf replacement for SKB_ABLE drivers
1959  */
1960 static int
1961 isdn_writebuf_stub(int drvidx, int chan, const u_char __user * buf, int len)
1962 {
1963         int ret;
1964         int hl = dev->drv[drvidx]->interface->hl_hdrlen;
1965         struct sk_buff *skb = alloc_skb(hl + len, GFP_ATOMIC);
1966
1967         if (!skb)
1968                 return -ENOMEM;
1969         skb_reserve(skb, hl);
1970         if (copy_from_user(skb_put(skb, len), buf, len))
1971                 return -EFAULT;
1972         ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, 1, skb);
1973         if (ret <= 0)
1974                 dev_kfree_skb(skb);
1975         if (ret > 0)
1976                 dev->obytes[isdn_dc2minor(drvidx, chan)] += ret;
1977         return ret;
1978 }
1979
1980 /*
1981  * Return: length of data on success, -ERRcode on failure.
1982  */
1983 int
1984 isdn_writebuf_skb_stub(int drvidx, int chan, int ack, struct sk_buff *skb)
1985 {
1986         int ret;
1987         struct sk_buff *nskb = NULL;
1988         int v110_ret = skb->len;
1989         int idx = isdn_dc2minor(drvidx, chan);
1990
1991         if (dev->v110[idx]) {
1992                 atomic_inc(&dev->v110use[idx]);
1993                 nskb = isdn_v110_encode(dev->v110[idx], skb);
1994                 atomic_dec(&dev->v110use[idx]);
1995                 if (!nskb)
1996                         return 0;
1997                 v110_ret = *((int *)nskb->data);
1998                 skb_pull(nskb, sizeof(int));
1999                 if (!nskb->len) {
2000                         dev_kfree_skb(nskb);
2001                         return v110_ret;
2002                 }
2003                 /* V.110 must always be acknowledged */
2004                 ack = 1;
2005                 ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, nskb);
2006         } else {
2007                 int hl = dev->drv[drvidx]->interface->hl_hdrlen;
2008
2009                 if( skb_headroom(skb) < hl ){
2010                         /* 
2011                          * This should only occur when new HL driver with
2012                          * increased hl_hdrlen was loaded after netdevice
2013                          * was created and connected to the new driver.
2014                          *
2015                          * The V.110 branch (re-allocates on its own) does
2016                          * not need this
2017                          */
2018                         struct sk_buff * skb_tmp;
2019
2020                         skb_tmp = skb_realloc_headroom(skb, hl);
2021                         printk(KERN_DEBUG "isdn_writebuf_skb_stub: reallocating headroom%s\n", skb_tmp ? "" : " failed");
2022                         if (!skb_tmp) return -ENOMEM; /* 0 better? */
2023                         ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb_tmp);
2024                         if( ret > 0 ){
2025                                 dev_kfree_skb(skb);
2026                         } else {
2027                                 dev_kfree_skb(skb_tmp);
2028                         }
2029                 } else {
2030                         ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb);
2031                 }
2032         }
2033         if (ret > 0) {
2034                 dev->obytes[idx] += ret;
2035                 if (dev->v110[idx]) {
2036                         atomic_inc(&dev->v110use[idx]);
2037                         dev->v110[idx]->skbuser++;
2038                         atomic_dec(&dev->v110use[idx]);
2039                         /* For V.110 return unencoded data length */
2040                         ret = v110_ret;
2041                         /* if the complete frame was send we free the skb;
2042                            if not upper function will requeue the skb */ 
2043                         if (ret == skb->len)
2044                                 dev_kfree_skb(skb);
2045                 }
2046         } else
2047                 if (dev->v110[idx])
2048                         dev_kfree_skb(nskb);
2049         return ret;
2050 }
2051
2052 static int
2053 isdn_add_channels(isdn_driver_t *d, int drvidx, int n, int adding)
2054 {
2055         int j, k, m;
2056
2057         init_waitqueue_head(&d->st_waitq);
2058         if (d->flags & DRV_FLAG_RUNNING)
2059                 return -1;
2060         if (n < 1) return 0;
2061
2062         m = (adding) ? d->channels + n : n;
2063
2064         if (dev->channels + n > ISDN_MAX_CHANNELS) {
2065                 printk(KERN_WARNING "register_isdn: Max. %d channels supported\n",
2066                        ISDN_MAX_CHANNELS);
2067                 return -1;
2068         }
2069
2070         if ((adding) && (d->rcverr))
2071                 kfree(d->rcverr);
2072         if (!(d->rcverr = kmalloc(sizeof(int) * m, GFP_ATOMIC))) {
2073                 printk(KERN_WARNING "register_isdn: Could not alloc rcverr\n");
2074                 return -1;
2075         }
2076         memset((char *) d->rcverr, 0, sizeof(int) * m);
2077
2078         if ((adding) && (d->rcvcount))
2079                 kfree(d->rcvcount);
2080         if (!(d->rcvcount = kmalloc(sizeof(int) * m, GFP_ATOMIC))) {
2081                 printk(KERN_WARNING "register_isdn: Could not alloc rcvcount\n");
2082                 if (!adding)
2083                         kfree(d->rcverr);
2084                 return -1;
2085         }
2086         memset((char *) d->rcvcount, 0, sizeof(int) * m);
2087
2088         if ((adding) && (d->rpqueue)) {
2089                 for (j = 0; j < d->channels; j++)
2090                         skb_queue_purge(&d->rpqueue[j]);
2091                 kfree(d->rpqueue);
2092         }
2093         if (!(d->rpqueue = kmalloc(sizeof(struct sk_buff_head) * m, GFP_ATOMIC))) {
2094                 printk(KERN_WARNING "register_isdn: Could not alloc rpqueue\n");
2095                 if (!adding) {
2096                         kfree(d->rcvcount);
2097                         kfree(d->rcverr);
2098                 }
2099                 return -1; 
2100         }
2101         for (j = 0; j < m; j++) {
2102                 skb_queue_head_init(&d->rpqueue[j]);
2103         }
2104
2105         if ((adding) && (d->rcv_waitq))
2106                 kfree(d->rcv_waitq);
2107         d->rcv_waitq = kmalloc(sizeof(wait_queue_head_t) * 2 * m, GFP_ATOMIC);
2108         if (!d->rcv_waitq) {
2109                 printk(KERN_WARNING "register_isdn: Could not alloc rcv_waitq\n");
2110                 if (!adding) {
2111                         kfree(d->rpqueue);
2112                         kfree(d->rcvcount);
2113                         kfree(d->rcverr);
2114                 }
2115                 return -1;
2116         }
2117         d->snd_waitq = d->rcv_waitq + m;
2118         for (j = 0; j < m; j++) {
2119                 init_waitqueue_head(&d->rcv_waitq[j]);
2120                 init_waitqueue_head(&d->snd_waitq[j]);
2121         }
2122
2123         dev->channels += n;
2124         for (j = d->channels; j < m; j++)
2125                 for (k = 0; k < ISDN_MAX_CHANNELS; k++)
2126                         if (dev->chanmap[k] < 0) {
2127                                 dev->chanmap[k] = j;
2128                                 dev->drvmap[k] = drvidx;
2129                                 break;
2130                         }
2131         d->channels = m;
2132         return 0;
2133 }
2134
2135 /*
2136  * Low-level-driver registration
2137  */
2138
2139 static void
2140 set_global_features(void)
2141 {
2142         int drvidx;
2143
2144         dev->global_features = 0;
2145         for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++) {
2146                 if (!dev->drv[drvidx])
2147                         continue;
2148                 if (dev->drv[drvidx]->interface)
2149                         dev->global_features |= dev->drv[drvidx]->interface->features;
2150         }
2151 }
2152
2153 #ifdef CONFIG_ISDN_DIVERSION
2154
2155 static char *map_drvname(int di)
2156 {
2157   if ((di < 0) || (di >= ISDN_MAX_DRIVERS)) 
2158     return(NULL);
2159   return(dev->drvid[di]); /* driver name */
2160 } /* map_drvname */
2161
2162 static int map_namedrv(char *id)
2163 {  int i;
2164
2165    for (i = 0; i < ISDN_MAX_DRIVERS; i++)
2166     { if (!strcmp(dev->drvid[i],id)) 
2167         return(i);
2168     }
2169    return(-1);
2170 } /* map_namedrv */
2171
2172 int DIVERT_REG_NAME(isdn_divert_if *i_div)
2173 {
2174   if (i_div->if_magic != DIVERT_IF_MAGIC) 
2175     return(DIVERT_VER_ERR);
2176   switch (i_div->cmd)
2177     {
2178       case DIVERT_CMD_REL:
2179         if (divert_if != i_div) 
2180           return(DIVERT_REL_ERR);
2181         divert_if = NULL; /* free interface */
2182         return(DIVERT_NO_ERR);
2183
2184       case DIVERT_CMD_REG:
2185         if (divert_if) 
2186           return(DIVERT_REG_ERR);
2187         i_div->ll_cmd = isdn_command; /* set command function */
2188         i_div->drv_to_name = map_drvname; 
2189         i_div->name_to_drv = map_namedrv; 
2190         divert_if = i_div; /* remember interface */
2191         return(DIVERT_NO_ERR);
2192
2193       default:
2194         return(DIVERT_CMD_ERR);   
2195     }
2196 } /* DIVERT_REG_NAME */
2197
2198 EXPORT_SYMBOL(DIVERT_REG_NAME);
2199
2200 #endif /* CONFIG_ISDN_DIVERSION */
2201
2202
2203 EXPORT_SYMBOL(register_isdn);
2204 #ifdef CONFIG_ISDN_PPP
2205 EXPORT_SYMBOL(isdn_ppp_register_compressor);
2206 EXPORT_SYMBOL(isdn_ppp_unregister_compressor);
2207 #endif
2208
2209 int
2210 register_isdn(isdn_if * i)
2211 {
2212         isdn_driver_t *d;
2213         int j;
2214         ulong flags;
2215         int drvidx;
2216
2217         if (dev->drivers >= ISDN_MAX_DRIVERS) {
2218                 printk(KERN_WARNING "register_isdn: Max. %d drivers supported\n",
2219                        ISDN_MAX_DRIVERS);
2220                 return 0;
2221         }
2222         if (!i->writebuf_skb) {
2223                 printk(KERN_WARNING "register_isdn: No write routine given.\n");
2224                 return 0;
2225         }
2226         if (!(d = kmalloc(sizeof(isdn_driver_t), GFP_KERNEL))) {
2227                 printk(KERN_WARNING "register_isdn: Could not alloc driver-struct\n");
2228                 return 0;
2229         }
2230         memset((char *) d, 0, sizeof(isdn_driver_t));
2231
2232         d->maxbufsize = i->maxbufsize;
2233         d->pktcount = 0;
2234         d->stavail = 0;
2235         d->flags = DRV_FLAG_LOADED;
2236         d->online = 0;
2237         d->interface = i;
2238         d->channels = 0;
2239         spin_lock_irqsave(&dev->lock, flags);
2240         for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++)
2241                 if (!dev->drv[drvidx])
2242                         break;
2243         if (isdn_add_channels(d, drvidx, i->channels, 0)) {
2244                 spin_unlock_irqrestore(&dev->lock, flags);
2245                 kfree(d);
2246                 return 0;
2247         }
2248         i->channels = drvidx;
2249         i->rcvcallb_skb = isdn_receive_skb_callback;
2250         i->statcallb = isdn_status_callback;
2251         if (!strlen(i->id))
2252                 sprintf(i->id, "line%d", drvidx);
2253         for (j = 0; j < drvidx; j++)
2254                 if (!strcmp(i->id, dev->drvid[j]))
2255                         sprintf(i->id, "line%d", drvidx);
2256         dev->drv[drvidx] = d;
2257         strcpy(dev->drvid[drvidx], i->id);
2258         isdn_info_update();
2259         dev->drivers++;
2260         set_global_features();
2261         spin_unlock_irqrestore(&dev->lock, flags);
2262         return 1;
2263 }
2264
2265 /*
2266  *****************************************************************************
2267  * And now the modules code.
2268  *****************************************************************************
2269  */
2270
2271 static char *
2272 isdn_getrev(const char *revision)
2273 {
2274         char *rev;
2275         char *p;
2276
2277         if ((p = strchr(revision, ':'))) {
2278                 rev = p + 2;
2279                 p = strchr(rev, '$');
2280                 *--p = 0;
2281         } else
2282                 rev = "???";
2283         return rev;
2284 }
2285
2286 /*
2287  * Allocate and initialize all data, register modem-devices
2288  */
2289 static int __init isdn_init(void)
2290 {
2291         int i;
2292         char tmprev[50];
2293
2294         if (!(dev = (isdn_dev *) vmalloc(sizeof(isdn_dev)))) {
2295                 printk(KERN_WARNING "isdn: Could not allocate device-struct.\n");
2296                 return -EIO;
2297         }
2298         memset((char *) dev, 0, sizeof(isdn_dev));
2299         init_timer(&dev->timer);
2300         dev->timer.function = isdn_timer_funct;
2301         spin_lock_init(&dev->lock);
2302         spin_lock_init(&dev->timerlock);
2303 #ifdef MODULE
2304         dev->owner = THIS_MODULE;
2305 #endif
2306         init_MUTEX(&dev->sem);
2307         init_waitqueue_head(&dev->info_waitq);
2308         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2309                 dev->drvmap[i] = -1;
2310                 dev->chanmap[i] = -1;
2311                 dev->m_idx[i] = -1;
2312                 strcpy(dev->num[i], "???");
2313                 init_waitqueue_head(&dev->mdm.info[i].open_wait);
2314                 init_waitqueue_head(&dev->mdm.info[i].close_wait);
2315         }
2316         if (register_chrdev(ISDN_MAJOR, "isdn", &isdn_fops)) {
2317                 printk(KERN_WARNING "isdn: Could not register control devices\n");
2318                 vfree(dev);
2319                 return -EIO;
2320         }
2321         if ((isdn_tty_modem_init()) < 0) {
2322                 printk(KERN_WARNING "isdn: Could not register tty devices\n");
2323                 vfree(dev);
2324                 unregister_chrdev(ISDN_MAJOR, "isdn");
2325                 return -EIO;
2326         }
2327 #ifdef CONFIG_ISDN_PPP
2328         if (isdn_ppp_init() < 0) {
2329                 printk(KERN_WARNING "isdn: Could not create PPP-device-structs\n");
2330                 isdn_tty_exit();
2331                 unregister_chrdev(ISDN_MAJOR, "isdn");
2332                 vfree(dev);
2333                 return -EIO;
2334         }
2335 #endif                          /* CONFIG_ISDN_PPP */
2336
2337         strcpy(tmprev, isdn_revision);
2338         printk(KERN_NOTICE "ISDN subsystem Rev: %s/", isdn_getrev(tmprev));
2339         strcpy(tmprev, isdn_tty_revision);
2340         printk("%s/", isdn_getrev(tmprev));
2341         strcpy(tmprev, isdn_net_revision);
2342         printk("%s/", isdn_getrev(tmprev));
2343         strcpy(tmprev, isdn_ppp_revision);
2344         printk("%s/", isdn_getrev(tmprev));
2345         strcpy(tmprev, isdn_audio_revision);
2346         printk("%s/", isdn_getrev(tmprev));
2347         strcpy(tmprev, isdn_v110_revision);
2348         printk("%s", isdn_getrev(tmprev));
2349
2350 #ifdef MODULE
2351         printk(" loaded\n");
2352 #else
2353         printk("\n");
2354 #endif
2355         isdn_info_update();
2356         return 0;
2357 }
2358
2359 /*
2360  * Unload module
2361  */
2362 static void __exit isdn_exit(void)
2363 {
2364 #ifdef CONFIG_ISDN_PPP
2365         isdn_ppp_cleanup();
2366 #endif
2367         if (isdn_net_rmall() < 0) {
2368                 printk(KERN_WARNING "isdn: net-device busy, remove cancelled\n");
2369                 return;
2370         }
2371         isdn_tty_exit();
2372         unregister_chrdev(ISDN_MAJOR, "isdn");
2373         del_timer(&dev->timer);
2374         /* call vfree with interrupts enabled, else it will hang */
2375         vfree(dev);
2376         printk(KERN_NOTICE "ISDN-subsystem unloaded\n");
2377 }
2378
2379 module_init(isdn_init);
2380 module_exit(isdn_exit);