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