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