]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/isdn/i4l/isdn_common.c
[PATCH] i4l:add some checks for valid drvid and driver pointer
[linux-2.6-omap-h63xx.git] / drivers / isdn / i4l / isdn_common.c
1 /* $Id: isdn_common.c,v 1.1.2.3 2004/02/10 01:07:13 keil Exp $
2  *
3  * Linux ISDN subsystem, common used functions (linklevel).
4  *
5  * Copyright 1994-1999  by Fritz Elfert (fritz@isdn4linux.de)
6  * Copyright 1995,96    Thinking Objects Software GmbH Wuerzburg
7  * Copyright 1995,96    by Michael Hipp (Michael.Hipp@student.uni-tuebingen.de)
8  *
9  * This software may be used and distributed according to the terms
10  * of the GNU General Public License, incorporated herein by reference.
11  *
12  */
13
14 #include <linux/config.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/poll.h>
18 #include <linux/vmalloc.h>
19 #include <linux/isdn.h>
20 #include <linux/smp_lock.h>
21 #include "isdn_common.h"
22 #include "isdn_tty.h"
23 #include "isdn_net.h"
24 #include "isdn_ppp.h"
25 #ifdef CONFIG_ISDN_AUDIO
26 #include "isdn_audio.h"
27 #endif
28 #ifdef CONFIG_ISDN_DIVERSION_MODULE
29 #define CONFIG_ISDN_DIVERSION
30 #endif
31 #ifdef CONFIG_ISDN_DIVERSION
32 #include <linux/isdn_divertif.h>
33 #endif /* CONFIG_ISDN_DIVERSION */
34 #include "isdn_v110.h"
35
36 /* Debugflags */
37 #undef ISDN_DEBUG_STATCALLB
38
39 MODULE_DESCRIPTION("ISDN4Linux: link layer");
40 MODULE_AUTHOR("Fritz Elfert");
41 MODULE_LICENSE("GPL");
42
43 isdn_dev *dev;
44
45 static char *isdn_revision = "$Revision: 1.1.2.3 $";
46
47 extern char *isdn_net_revision;
48 extern char *isdn_tty_revision;
49 #ifdef CONFIG_ISDN_PPP
50 extern char *isdn_ppp_revision;
51 #else
52 static char *isdn_ppp_revision = ": none $";
53 #endif
54 #ifdef CONFIG_ISDN_AUDIO
55 extern char *isdn_audio_revision;
56 #else
57 static char *isdn_audio_revision = ": none $";
58 #endif
59 extern char *isdn_v110_revision;
60
61 #ifdef CONFIG_ISDN_DIVERSION
62 static isdn_divert_if *divert_if; /* = NULL */
63 #endif /* CONFIG_ISDN_DIVERSION */
64
65
66 static int isdn_writebuf_stub(int, int, const u_char __user *, int);
67 static void set_global_features(void);
68 static int isdn_wildmat(char *s, char *p);
69 static int isdn_add_channels(isdn_driver_t *d, int drvidx, int n, int adding);
70
71 static inline void
72 isdn_lock_driver(isdn_driver_t *drv)
73 {
74         try_module_get(drv->interface->owner);
75         drv->locks++;
76 }
77
78 void
79 isdn_lock_drivers(void)
80 {
81         int i;
82
83         for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
84                 if (!dev->drv[i])
85                         continue;
86                 isdn_lock_driver(dev->drv[i]);
87         }
88 }
89
90 static inline void
91 isdn_unlock_driver(isdn_driver_t *drv)
92 {
93         if (drv->locks > 0) {
94                 drv->locks--;
95                 module_put(drv->interface->owner);
96         }
97 }
98
99 void
100 isdn_unlock_drivers(void)
101 {
102         int i;
103
104         for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
105                 if (!dev->drv[i])
106                         continue;
107                 isdn_unlock_driver(dev->drv[i]);
108         }
109 }
110
111 #if defined(ISDN_DEBUG_NET_DUMP) || defined(ISDN_DEBUG_MODEM_DUMP)
112 void
113 isdn_dumppkt(char *s, u_char * p, int len, int dumplen)
114 {
115         int dumpc;
116
117         printk(KERN_DEBUG "%s(%d) ", s, len);
118         for (dumpc = 0; (dumpc < dumplen) && (len); len--, dumpc++)
119                 printk(" %02x", *p++);
120         printk("\n");
121 }
122 #endif
123
124 /*
125  * I picked the pattern-matching-functions from an old GNU-tar version (1.10)
126  * It was originally written and put to PD by rs@mirror.TMC.COM (Rich Salz)
127  */
128 static int
129 isdn_star(char *s, char *p)
130 {
131         while (isdn_wildmat(s, p)) {
132                 if (*++s == '\0')
133                         return (2);
134         }
135         return (0);
136 }
137
138 /*
139  * Shell-type Pattern-matching for incoming caller-Ids
140  * This function gets a string in s and checks, if it matches the pattern
141  * given in p.
142  *
143  * Return:
144  *   0 = match.
145  *   1 = no match.
146  *   2 = no match. Would eventually match, if s would be longer.
147  *
148  * Possible Patterns:
149  *
150  * '?'     matches one character
151  * '*'     matches zero or more characters
152  * [xyz]   matches the set of characters in brackets.
153  * [^xyz]  matches any single character not in the set of characters
154  */
155
156 static int
157 isdn_wildmat(char *s, char *p)
158 {
159         register int last;
160         register int matched;
161         register int reverse;
162         register int nostar = 1;
163
164         if (!(*s) && !(*p))
165                 return(1);
166         for (; *p; s++, p++)
167                 switch (*p) {
168                         case '\\':
169                                 /*
170                                  * Literal match with following character,
171                                  * fall through.
172                                  */
173                                 p++;
174                         default:
175                                 if (*s != *p)
176                                         return (*s == '\0')?2:1;
177                                 continue;
178                         case '?':
179                                 /* Match anything. */
180                                 if (*s == '\0')
181                                         return (2);
182                                 continue;
183                         case '*':
184                                 nostar = 0;     
185                                 /* Trailing star matches everything. */
186                                 return (*++p ? isdn_star(s, p) : 0);
187                         case '[':
188                                 /* [^....] means inverse character class. */
189                                 if ((reverse = (p[1] == '^')))
190                                         p++;
191                                 for (last = 0, matched = 0; *++p && (*p != ']'); last = *p)
192                                         /* This next line requires a good C compiler. */
193                                         if (*p == '-' ? *s <= *++p && *s >= last : *s == *p)
194                                                 matched = 1;
195                                 if (matched == reverse)
196                                         return (1);
197                                 continue;
198                 }
199         return (*s == '\0')?0:nostar;
200 }
201
202 int isdn_msncmp( const char * msn1, const char * msn2 )
203 {
204         char TmpMsn1[ ISDN_MSNLEN ];
205         char TmpMsn2[ ISDN_MSNLEN ];
206         char *p;
207
208         for ( p = TmpMsn1; *msn1 && *msn1 != ':'; )  // Strip off a SPID
209                 *p++ = *msn1++;
210         *p = '\0';
211
212         for ( p = TmpMsn2; *msn2 && *msn2 != ':'; )  // Strip off a SPID
213                 *p++ = *msn2++;
214         *p = '\0';
215
216         return isdn_wildmat( TmpMsn1, TmpMsn2 );
217 }
218
219 int
220 isdn_dc2minor(int di, int ch)
221 {
222         int i;
223         for (i = 0; i < ISDN_MAX_CHANNELS; i++)
224                 if (dev->chanmap[i] == ch && dev->drvmap[i] == di)
225                         return i;
226         return -1;
227 }
228
229 static int isdn_timer_cnt1 = 0;
230 static int isdn_timer_cnt2 = 0;
231 static int isdn_timer_cnt3 = 0;
232
233 static void
234 isdn_timer_funct(ulong dummy)
235 {
236         int tf = dev->tflags;
237         if (tf & ISDN_TIMER_FAST) {
238                 if (tf & ISDN_TIMER_MODEMREAD)
239                         isdn_tty_readmodem();
240                 if (tf & ISDN_TIMER_MODEMPLUS)
241                         isdn_tty_modem_escape();
242                 if (tf & ISDN_TIMER_MODEMXMIT)
243                         isdn_tty_modem_xmit();
244         }
245         if (tf & ISDN_TIMER_SLOW) {
246                 if (++isdn_timer_cnt1 >= ISDN_TIMER_02SEC) {
247                         isdn_timer_cnt1 = 0;
248                         if (tf & ISDN_TIMER_NETDIAL)
249                                 isdn_net_dial();
250                 }
251                 if (++isdn_timer_cnt2 >= ISDN_TIMER_1SEC) {
252                         isdn_timer_cnt2 = 0;
253                         if (tf & ISDN_TIMER_NETHANGUP)
254                                 isdn_net_autohup();
255                         if (++isdn_timer_cnt3 >= ISDN_TIMER_RINGING) {
256                                 isdn_timer_cnt3 = 0;
257                                 if (tf & ISDN_TIMER_MODEMRING)
258                                         isdn_tty_modem_ring();
259                         }
260                         if (tf & ISDN_TIMER_CARRIER)
261                                 isdn_tty_carrier_timeout();
262                 }
263         }
264         if (tf) 
265                 mod_timer(&dev->timer, jiffies+ISDN_TIMER_RES);
266 }
267
268 void
269 isdn_timer_ctrl(int tf, int onoff)
270 {
271         unsigned long flags;
272         int old_tflags;
273
274         spin_lock_irqsave(&dev->timerlock, flags);
275         if ((tf & ISDN_TIMER_SLOW) && (!(dev->tflags & ISDN_TIMER_SLOW))) {
276                 /* If the slow-timer wasn't activated until now */
277                 isdn_timer_cnt1 = 0;
278                 isdn_timer_cnt2 = 0;
279         }
280         old_tflags = dev->tflags;
281         if (onoff)
282                 dev->tflags |= tf;
283         else
284                 dev->tflags &= ~tf;
285         if (dev->tflags && !old_tflags)
286                 mod_timer(&dev->timer, jiffies+ISDN_TIMER_RES);
287         spin_unlock_irqrestore(&dev->timerlock, flags);
288 }
289
290 /*
291  * Receive a packet from B-Channel. (Called from low-level-module)
292  */
293 static void
294 isdn_receive_skb_callback(int di, int channel, struct sk_buff *skb)
295 {
296         int i;
297
298         if ((i = isdn_dc2minor(di, channel)) == -1) {
299                 dev_kfree_skb(skb);
300                 return;
301         }
302         /* Update statistics */
303         dev->ibytes[i] += skb->len;
304         
305         /* First, try to deliver data to network-device */
306         if (isdn_net_rcv_skb(i, skb))
307                 return;
308
309         /* V.110 handling
310          * makes sense for async streams only, so it is
311          * called after possible net-device delivery.
312          */
313         if (dev->v110[i]) {
314                 atomic_inc(&dev->v110use[i]);
315                 skb = isdn_v110_decode(dev->v110[i], skb);
316                 atomic_dec(&dev->v110use[i]);
317                 if (!skb)
318                         return;
319         }
320
321         /* No network-device found, deliver to tty or raw-channel */
322         if (skb->len) {
323                 if (isdn_tty_rcv_skb(i, di, channel, skb))
324                         return;
325                 wake_up_interruptible(&dev->drv[di]->rcv_waitq[channel]);
326         } else
327                 dev_kfree_skb(skb);
328 }
329
330 /*
331  * Intercept command from Linklevel to Lowlevel.
332  * If layer 2 protocol is V.110 and this is not supported by current
333  * lowlevel-driver, use driver's transparent mode and handle V.110 in
334  * linklevel instead.
335  */
336 int
337 isdn_command(isdn_ctrl *cmd)
338 {
339         if (cmd->driver == -1) {
340                 printk(KERN_WARNING "isdn_command command(%x) driver -1\n", cmd->command);
341                 return(1);
342         }
343         if (!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 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                         memcpy(cp, skb->data, 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 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                                 len--;
919                                 if (dev->drv[di]->DLEflag & DLEmask) {
920                                         last = DLE;
921                                         dev->drv[di]->DLEflag &= ~DLEmask;
922                                 } else {
923                                         last = *p;
924                                         if (last == DLE) {
925                                                 dev->drv[di]->DLEflag |= DLEmask;
926                                                 (ISDN_AUDIO_SKB_DLECOUNT(skb))--;
927                                         }
928                                         p++;
929                                         count_pull++;
930                                 }
931                                 count_put++;
932                         }
933                         if (count_pull >= skb->len)
934                                 dflag = 1;
935                 } else {
936 #endif
937                         /* No DLE's in buff, so simply copy it */
938                         dflag = 1;
939                         if ((count_pull = skb->len) > len) {
940                                 count_pull = len;
941                                 dflag = 0;
942                         }
943                         count_put = count_pull;
944                         if(count_put > 1)
945                                 tty_insert_flip_string(tty, skb->data, count_put - 1);
946                         last = skb->data[count_put - 1];
947                         len -= count_put;
948 #ifdef CONFIG_ISDN_AUDIO
949                 }
950 #endif
951                 count += count_put;
952                 if (dflag) {
953                         /* We got all the data in this buff.
954                          * Now we can dequeue it.
955                          */
956                         if(cisco_hack)
957                                 tty_insert_flip_char(tty, last, 0xFF);
958                         else
959                                 tty_insert_flip_char(tty, last, TTY_NORMAL);
960 #ifdef CONFIG_ISDN_AUDIO
961                         ISDN_AUDIO_SKB_LOCK(skb) = 0;
962 #endif
963                         skb = skb_dequeue(&dev->drv[di]->rpqueue[channel]);
964                         dev_kfree_skb(skb);
965                 } else {
966                         tty_insert_flip_char(tty, last, TTY_NORMAL);
967                         /* Not yet emptied this buff, so it
968                          * must stay in the queue, for further calls
969                          * but we pull off the data we got until now.
970                          */
971                         skb_pull(skb, count_pull);
972 #ifdef CONFIG_ISDN_AUDIO
973                         ISDN_AUDIO_SKB_LOCK(skb) = 0;
974 #endif
975                 }
976                 dev->drv[di]->rcvcount[channel] -= count_put;
977         }
978         return count;
979 }
980
981
982 static __inline int
983 isdn_minor2drv(int minor)
984 {
985         return (dev->drvmap[minor]);
986 }
987
988 static __inline int
989 isdn_minor2chan(int minor)
990 {
991         return (dev->chanmap[minor]);
992 }
993
994 static char *
995 isdn_statstr(void)
996 {
997         static char istatbuf[2048];
998         char *p;
999         int i;
1000
1001         sprintf(istatbuf, "idmap:\t");
1002         p = istatbuf + strlen(istatbuf);
1003         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1004                 sprintf(p, "%s ", (dev->drvmap[i] < 0) ? "-" : dev->drvid[dev->drvmap[i]]);
1005                 p = istatbuf + strlen(istatbuf);
1006         }
1007         sprintf(p, "\nchmap:\t");
1008         p = istatbuf + strlen(istatbuf);
1009         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1010                 sprintf(p, "%d ", dev->chanmap[i]);
1011                 p = istatbuf + strlen(istatbuf);
1012         }
1013         sprintf(p, "\ndrmap:\t");
1014         p = istatbuf + strlen(istatbuf);
1015         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1016                 sprintf(p, "%d ", dev->drvmap[i]);
1017                 p = istatbuf + strlen(istatbuf);
1018         }
1019         sprintf(p, "\nusage:\t");
1020         p = istatbuf + strlen(istatbuf);
1021         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1022                 sprintf(p, "%d ", dev->usage[i]);
1023                 p = istatbuf + strlen(istatbuf);
1024         }
1025         sprintf(p, "\nflags:\t");
1026         p = istatbuf + strlen(istatbuf);
1027         for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
1028                 if (dev->drv[i]) {
1029                         sprintf(p, "%ld ", dev->drv[i]->online);
1030                         p = istatbuf + strlen(istatbuf);
1031                 } else {
1032                         sprintf(p, "? ");
1033                         p = istatbuf + strlen(istatbuf);
1034                 }
1035         }
1036         sprintf(p, "\nphone:\t");
1037         p = istatbuf + strlen(istatbuf);
1038         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1039                 sprintf(p, "%s ", dev->num[i]);
1040                 p = istatbuf + strlen(istatbuf);
1041         }
1042         sprintf(p, "\n");
1043         return istatbuf;
1044 }
1045
1046 /* Module interface-code */
1047
1048 void
1049 isdn_info_update(void)
1050 {
1051         infostruct *p = dev->infochain;
1052
1053         while (p) {
1054                 *(p->private) = 1;
1055                 p = (infostruct *) p->next;
1056         }
1057         wake_up_interruptible(&(dev->info_waitq));
1058 }
1059
1060 static ssize_t
1061 isdn_read(struct file *file, char __user *buf, size_t count, loff_t * off)
1062 {
1063         uint minor = MINOR(file->f_dentry->d_inode->i_rdev);
1064         int len = 0;
1065         int drvidx;
1066         int chidx;
1067         int retval;
1068         char *p;
1069
1070         lock_kernel();
1071         if (minor == ISDN_MINOR_STATUS) {
1072                 if (!file->private_data) {
1073                         if (file->f_flags & O_NONBLOCK) {
1074                                 retval = -EAGAIN;
1075                                 goto out;
1076                         }
1077                         interruptible_sleep_on(&(dev->info_waitq));
1078                 }
1079                 p = isdn_statstr();
1080                 file->private_data = NULL;
1081                 if ((len = strlen(p)) <= count) {
1082                         if (copy_to_user(buf, p, len)) {
1083                                 retval = -EFAULT;
1084                                 goto out;
1085                         }
1086                         *off += len;
1087                         retval = len;
1088                         goto out;
1089                 }
1090                 retval = 0;
1091                 goto out;
1092         }
1093         if (!dev->drivers) {
1094                 retval = -ENODEV;
1095                 goto out;
1096         }
1097         if (minor <= ISDN_MINOR_BMAX) {
1098                 printk(KERN_WARNING "isdn_read minor %d obsolete!\n", minor);
1099                 drvidx = isdn_minor2drv(minor);
1100                 if (drvidx < 0) {
1101                         retval = -ENODEV;
1102                         goto out;
1103                 }
1104                 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
1105                         retval = -ENODEV;
1106                         goto out;
1107                 }
1108                 chidx = isdn_minor2chan(minor);
1109                 if (!(p = kmalloc(count, GFP_KERNEL))) {
1110                         retval = -ENOMEM;
1111                         goto out;
1112                 }
1113                 len = isdn_readbchan(drvidx, chidx, p, NULL, count,
1114                                      &dev->drv[drvidx]->rcv_waitq[chidx]);
1115                 *off += len;
1116                 if (copy_to_user(buf,p,len)) 
1117                         len = -EFAULT;
1118                 kfree(p);
1119                 retval = len;
1120                 goto out;
1121         }
1122         if (minor <= ISDN_MINOR_CTRLMAX) {
1123                 drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1124                 if (drvidx < 0) {
1125                         retval = -ENODEV;
1126                         goto out;
1127                 }
1128                 if (!dev->drv[drvidx]->stavail) {
1129                         if (file->f_flags & O_NONBLOCK) {
1130                                 retval = -EAGAIN;
1131                                 goto out;
1132                         }
1133                         interruptible_sleep_on(&(dev->drv[drvidx]->st_waitq));
1134                 }
1135                 if (dev->drv[drvidx]->interface->readstat) {
1136                         if (count > dev->drv[drvidx]->stavail)
1137                                 count = dev->drv[drvidx]->stavail;
1138                         len = dev->drv[drvidx]->interface->
1139                                 readstat(buf, count, drvidx,
1140                                          isdn_minor2chan(minor));
1141                 } else {
1142                         len = 0;
1143                 }
1144                 if (len)
1145                         dev->drv[drvidx]->stavail -= len;
1146                 else
1147                         dev->drv[drvidx]->stavail = 0;
1148                 *off += len;
1149                 retval = len;
1150                 goto out;
1151         }
1152 #ifdef CONFIG_ISDN_PPP
1153         if (minor <= ISDN_MINOR_PPPMAX) {
1154                 retval = isdn_ppp_read(minor - ISDN_MINOR_PPP, file, buf, count);
1155                 goto out;
1156         }
1157 #endif
1158         retval = -ENODEV;
1159  out:
1160         unlock_kernel();
1161         return retval;
1162 }
1163
1164 static ssize_t
1165 isdn_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
1166 {
1167         uint minor = MINOR(file->f_dentry->d_inode->i_rdev);
1168         int drvidx;
1169         int chidx;
1170         int retval;
1171
1172         if (minor == ISDN_MINOR_STATUS)
1173                 return -EPERM;
1174         if (!dev->drivers)
1175                 return -ENODEV;
1176
1177         lock_kernel();
1178         if (minor <= ISDN_MINOR_BMAX) {
1179                 printk(KERN_WARNING "isdn_write minor %d obsolete!\n", minor);
1180                 drvidx = isdn_minor2drv(minor);
1181                 if (drvidx < 0) {
1182                         retval = -ENODEV;
1183                         goto out;
1184                 }
1185                 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
1186                         retval = -ENODEV;
1187                         goto out;
1188                 }
1189                 chidx = isdn_minor2chan(minor);
1190                 while ((retval = isdn_writebuf_stub(drvidx, chidx, buf, count)) == 0)
1191                         interruptible_sleep_on(&dev->drv[drvidx]->snd_waitq[chidx]);
1192                 goto out;
1193         }
1194         if (minor <= ISDN_MINOR_CTRLMAX) {
1195                 drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1196                 if (drvidx < 0) {
1197                         retval = -ENODEV;
1198                         goto out;
1199                 }
1200                 /*
1201                  * We want to use the isdnctrl device to load the firmware
1202                  *
1203                  if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1204                  return -ENODEV;
1205                  */
1206                 if (dev->drv[drvidx]->interface->writecmd)
1207                         retval = dev->drv[drvidx]->interface->
1208                                 writecmd(buf, count, drvidx, isdn_minor2chan(minor));
1209                 else
1210                         retval = count;
1211                 goto out;
1212         }
1213 #ifdef CONFIG_ISDN_PPP
1214         if (minor <= ISDN_MINOR_PPPMAX) {
1215                 retval = isdn_ppp_write(minor - ISDN_MINOR_PPP, file, buf, count);
1216                 goto out;
1217         }
1218 #endif
1219         retval = -ENODEV;
1220  out:
1221         unlock_kernel();
1222         return retval;
1223 }
1224
1225 static unsigned int
1226 isdn_poll(struct file *file, poll_table * wait)
1227 {
1228         unsigned int mask = 0;
1229         unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
1230         int drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1231
1232         lock_kernel();
1233         if (minor == ISDN_MINOR_STATUS) {
1234                 poll_wait(file, &(dev->info_waitq), wait);
1235                 /* mask = POLLOUT | POLLWRNORM; */
1236                 if (file->private_data) {
1237                         mask |= POLLIN | POLLRDNORM;
1238                 }
1239                 goto out;
1240         }
1241         if (minor >= ISDN_MINOR_CTRL && minor <= ISDN_MINOR_CTRLMAX) {
1242                 if (drvidx < 0) {
1243                         /* driver deregistered while file open */
1244                         mask = POLLHUP;
1245                         goto out;
1246                 }
1247                 poll_wait(file, &(dev->drv[drvidx]->st_waitq), wait);
1248                 mask = POLLOUT | POLLWRNORM;
1249                 if (dev->drv[drvidx]->stavail) {
1250                         mask |= POLLIN | POLLRDNORM;
1251                 }
1252                 goto out;
1253         }
1254 #ifdef CONFIG_ISDN_PPP
1255         if (minor <= ISDN_MINOR_PPPMAX) {
1256                 mask = isdn_ppp_poll(file, wait);
1257                 goto out;
1258         }
1259 #endif
1260         mask = POLLERR;
1261  out:
1262         unlock_kernel();
1263         return mask;
1264 }
1265
1266
1267 static int
1268 isdn_ioctl(struct inode *inode, struct file *file, uint cmd, ulong arg)
1269 {
1270         uint minor = MINOR(inode->i_rdev);
1271         isdn_ctrl c;
1272         int drvidx;
1273         int chidx;
1274         int ret;
1275         int i;
1276         char __user *p;
1277         char *s;
1278         union iocpar {
1279                 char name[10];
1280                 char bname[22];
1281                 isdn_ioctl_struct iocts;
1282                 isdn_net_ioctl_phone phone;
1283                 isdn_net_ioctl_cfg cfg;
1284         } iocpar;
1285         void __user *argp = (void __user *)arg;
1286
1287 #define name  iocpar.name
1288 #define bname iocpar.bname
1289 #define iocts iocpar.iocts
1290 #define phone iocpar.phone
1291 #define cfg   iocpar.cfg
1292
1293         if (minor == ISDN_MINOR_STATUS) {
1294                 switch (cmd) {
1295                         case IIOCGETDVR:
1296                                 return (TTY_DV +
1297                                         (NET_DV << 8) +
1298                                         (INF_DV << 16));
1299                         case IIOCGETCPS:
1300                                 if (arg) {
1301                                         ulong __user *p = argp;
1302                                         int i;
1303                                         if (!access_ok(VERIFY_WRITE, p,
1304                                                         sizeof(ulong) * ISDN_MAX_CHANNELS * 2))
1305                                                 return -EFAULT;
1306                                         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1307                                                 put_user(dev->ibytes[i], p++);
1308                                                 put_user(dev->obytes[i], p++);
1309                                         }
1310                                         return 0;
1311                                 } else
1312                                         return -EINVAL;
1313                                 break;
1314 #ifdef CONFIG_NETDEVICES
1315                         case IIOCNETGPN:
1316                                 /* Get peer phone number of a connected 
1317                                  * isdn network interface */
1318                                 if (arg) {
1319                                         if (copy_from_user(&phone, argp, sizeof(phone)))
1320                                                 return -EFAULT;
1321                                         return isdn_net_getpeer(&phone, argp);
1322                                 } else
1323                                         return -EINVAL;
1324 #endif
1325                         default:
1326                                 return -EINVAL;
1327                 }
1328         }
1329         if (!dev->drivers)
1330                 return -ENODEV;
1331         if (minor <= ISDN_MINOR_BMAX) {
1332                 drvidx = isdn_minor2drv(minor);
1333                 if (drvidx < 0)
1334                         return -ENODEV;
1335                 chidx = isdn_minor2chan(minor);
1336                 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1337                         return -ENODEV;
1338                 return 0;
1339         }
1340         if (minor <= ISDN_MINOR_CTRLMAX) {
1341 /*
1342  * isdn net devices manage lots of configuration variables as linked lists.
1343  * Those lists must only be manipulated from user space. Some of the ioctl's
1344  * service routines access user space and are not atomic. Therefor, ioctl's
1345  * manipulating the lists and ioctl's sleeping while accessing the lists
1346  * are serialized by means of a semaphore.
1347  */
1348                 switch (cmd) {
1349                         case IIOCNETDWRSET:
1350                                 printk(KERN_INFO "INFO: ISDN_DW_ABC_EXTENSION not enabled\n");
1351                                 return(-EINVAL);
1352                         case IIOCNETLCR:
1353                                 printk(KERN_INFO "INFO: ISDN_ABC_LCR_SUPPORT not enabled\n");
1354                                 return -ENODEV;
1355 #ifdef CONFIG_NETDEVICES
1356                         case IIOCNETAIF:
1357                                 /* Add a network-interface */
1358                                 if (arg) {
1359                                         if (copy_from_user(name, argp, sizeof(name)))
1360                                                 return -EFAULT;
1361                                         s = name;
1362                                 } else {
1363                                         s = NULL;
1364                                 }
1365                                 ret = down_interruptible(&dev->sem);
1366                                 if( ret ) return ret;
1367                                 if ((s = isdn_net_new(s, NULL))) {
1368                                         if (copy_to_user(argp, s, strlen(s) + 1)){
1369                                                 ret = -EFAULT;
1370                                         } else {
1371                                                 ret = 0;
1372                                         }
1373                                 } else
1374                                         ret = -ENODEV;
1375                                 up(&dev->sem);
1376                                 return ret;
1377                         case IIOCNETASL:
1378                                 /* Add a slave to a network-interface */
1379                                 if (arg) {
1380                                         if (copy_from_user(bname, argp, sizeof(bname) - 1))
1381                                                 return -EFAULT;
1382                                 } else
1383                                         return -EINVAL;
1384                                 ret = down_interruptible(&dev->sem);
1385                                 if( ret ) return ret;
1386                                 if ((s = isdn_net_newslave(bname))) {
1387                                         if (copy_to_user(argp, s, strlen(s) + 1)){
1388                                                 ret = -EFAULT;
1389                                         } else {
1390                                                 ret = 0;
1391                                         }
1392                                 } else
1393                                         ret = -ENODEV;
1394                                 up(&dev->sem);
1395                                 return ret;
1396                         case IIOCNETDIF:
1397                                 /* Delete a network-interface */
1398                                 if (arg) {
1399                                         if (copy_from_user(name, argp, sizeof(name)))
1400                                                 return -EFAULT;
1401                                         ret = down_interruptible(&dev->sem);
1402                                         if( ret ) return ret;
1403                                         ret = isdn_net_rm(name);
1404                                         up(&dev->sem);
1405                                         return ret;
1406                                 } else
1407                                         return -EINVAL;
1408                         case IIOCNETSCF:
1409                                 /* Set configurable parameters of a network-interface */
1410                                 if (arg) {
1411                                         if (copy_from_user(&cfg, argp, sizeof(cfg)))
1412                                                 return -EFAULT;
1413                                         return isdn_net_setcfg(&cfg);
1414                                 } else
1415                                         return -EINVAL;
1416                         case IIOCNETGCF:
1417                                 /* Get configurable parameters of a network-interface */
1418                                 if (arg) {
1419                                         if (copy_from_user(&cfg, argp, sizeof(cfg)))
1420                                                 return -EFAULT;
1421                                         if (!(ret = isdn_net_getcfg(&cfg))) {
1422                                                 if (copy_to_user(argp, &cfg, sizeof(cfg)))
1423                                                         return -EFAULT;
1424                                         }
1425                                         return ret;
1426                                 } else
1427                                         return -EINVAL;
1428                         case IIOCNETANM:
1429                                 /* Add a phone-number to a network-interface */
1430                                 if (arg) {
1431                                         if (copy_from_user(&phone, argp, sizeof(phone)))
1432                                                 return -EFAULT;
1433                                         ret = down_interruptible(&dev->sem);
1434                                         if( ret ) return ret;
1435                                         ret = isdn_net_addphone(&phone);
1436                                         up(&dev->sem);
1437                                         return ret;
1438                                 } else
1439                                         return -EINVAL;
1440                         case IIOCNETGNM:
1441                                 /* Get list of phone-numbers of a network-interface */
1442                                 if (arg) {
1443                                         if (copy_from_user(&phone, argp, sizeof(phone)))
1444                                                 return -EFAULT;
1445                                         ret = down_interruptible(&dev->sem);
1446                                         if( ret ) return ret;
1447                                         ret = isdn_net_getphones(&phone, argp);
1448                                         up(&dev->sem);
1449                                         return ret;
1450                                 } else
1451                                         return -EINVAL;
1452                         case IIOCNETDNM:
1453                                 /* Delete a phone-number of a network-interface */
1454                                 if (arg) {
1455                                         if (copy_from_user(&phone, argp, sizeof(phone)))
1456                                                 return -EFAULT;
1457                                         ret = down_interruptible(&dev->sem);
1458                                         if( ret ) return ret;
1459                                         ret = isdn_net_delphone(&phone);
1460                                         up(&dev->sem);
1461                                         return ret;
1462                                 } else
1463                                         return -EINVAL;
1464                         case IIOCNETDIL:
1465                                 /* Force dialing of a network-interface */
1466                                 if (arg) {
1467                                         if (copy_from_user(name, argp, sizeof(name)))
1468                                                 return -EFAULT;
1469                                         return isdn_net_force_dial(name);
1470                                 } else
1471                                         return -EINVAL;
1472 #ifdef CONFIG_ISDN_PPP
1473                         case IIOCNETALN:
1474                                 if (!arg)
1475                                         return -EINVAL;
1476                                 if (copy_from_user(name, argp, sizeof(name)))
1477                                         return -EFAULT;
1478                                 return isdn_ppp_dial_slave(name);
1479                         case IIOCNETDLN:
1480                                 if (!arg)
1481                                         return -EINVAL;
1482                                 if (copy_from_user(name, argp, sizeof(name)))
1483                                         return -EFAULT;
1484                                 return isdn_ppp_hangup_slave(name);
1485 #endif
1486                         case IIOCNETHUP:
1487                                 /* Force hangup of a network-interface */
1488                                 if (!arg)
1489                                         return -EINVAL;
1490                                 if (copy_from_user(name, argp, sizeof(name)))
1491                                         return -EFAULT;
1492                                 return isdn_net_force_hangup(name);
1493                                 break;
1494 #endif                          /* CONFIG_NETDEVICES */
1495                         case IIOCSETVER:
1496                                 dev->net_verbose = arg;
1497                                 printk(KERN_INFO "isdn: Verbose-Level is %d\n", dev->net_verbose);
1498                                 return 0;
1499                         case IIOCSETGST:
1500                                 if (arg)
1501                                         dev->global_flags |= ISDN_GLOBAL_STOPPED;
1502                                 else
1503                                         dev->global_flags &= ~ISDN_GLOBAL_STOPPED;
1504                                 printk(KERN_INFO "isdn: Global Mode %s\n",
1505                                        (dev->global_flags & ISDN_GLOBAL_STOPPED) ? "stopped" : "running");
1506                                 return 0;
1507                         case IIOCSETBRJ:
1508                                 drvidx = -1;
1509                                 if (arg) {
1510                                         int i;
1511                                         char *p;
1512                                         if (copy_from_user(&iocts, argp,
1513                                              sizeof(isdn_ioctl_struct)))
1514                                                 return -EFAULT;
1515                                         if (strlen(iocts.drvid)) {
1516                                                 if ((p = strchr(iocts.drvid, ',')))
1517                                                         *p = 0;
1518                                                 drvidx = -1;
1519                                                 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1520                                                         if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1521                                                                 drvidx = i;
1522                                                                 break;
1523                                                         }
1524                                         }
1525                                 }
1526                                 if (drvidx == -1)
1527                                         return -ENODEV;
1528                                 if (iocts.arg)
1529                                         dev->drv[drvidx]->flags |= DRV_FLAG_REJBUS;
1530                                 else
1531                                         dev->drv[drvidx]->flags &= ~DRV_FLAG_REJBUS;
1532                                 return 0;
1533                         case IIOCSIGPRF:
1534                                 dev->profd = current;
1535                                 return 0;
1536                                 break;
1537                         case IIOCGETPRF:
1538                                 /* Get all Modem-Profiles */
1539                                 if (arg) {
1540                                         char __user *p = argp;
1541                                         int i;
1542
1543                                         if (!access_ok(VERIFY_WRITE, argp,
1544                                         (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
1545                                                    * ISDN_MAX_CHANNELS))
1546                                                 return -EFAULT;
1547
1548                                         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1549                                                 if (copy_to_user(p, dev->mdm.info[i].emu.profile,
1550                                                       ISDN_MODEM_NUMREG))
1551                                                         return -EFAULT;
1552                                                 p += ISDN_MODEM_NUMREG;
1553                                                 if (copy_to_user(p, dev->mdm.info[i].emu.pmsn, ISDN_MSNLEN))
1554                                                         return -EFAULT;
1555                                                 p += ISDN_MSNLEN;
1556                                                 if (copy_to_user(p, dev->mdm.info[i].emu.plmsn, ISDN_LMSNLEN))
1557                                                         return -EFAULT;
1558                                                 p += ISDN_LMSNLEN;
1559                                         }
1560                                         return (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN) * ISDN_MAX_CHANNELS;
1561                                 } else
1562                                         return -EINVAL;
1563                                 break;
1564                         case IIOCSETPRF:
1565                                 /* Set all Modem-Profiles */
1566                                 if (arg) {
1567                                         char __user *p = argp;
1568                                         int i;
1569
1570                                         if (!access_ok(VERIFY_READ, argp,
1571                                         (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
1572                                                    * ISDN_MAX_CHANNELS))
1573                                                 return -EFAULT;
1574
1575                                         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1576                                                 if (copy_from_user(dev->mdm.info[i].emu.profile, p,
1577                                                      ISDN_MODEM_NUMREG))
1578                                                         return -EFAULT;
1579                                                 p += ISDN_MODEM_NUMREG;
1580                                                 if (copy_from_user(dev->mdm.info[i].emu.plmsn, p, ISDN_LMSNLEN))
1581                                                         return -EFAULT;
1582                                                 p += ISDN_LMSNLEN;
1583                                                 if (copy_from_user(dev->mdm.info[i].emu.pmsn, p, ISDN_MSNLEN))
1584                                                         return -EFAULT;
1585                                                 p += ISDN_MSNLEN;
1586                                         }
1587                                         return 0;
1588                                 } else
1589                                         return -EINVAL;
1590                                 break;
1591                         case IIOCSETMAP:
1592                         case IIOCGETMAP:
1593                                 /* Set/Get MSN->EAZ-Mapping for a driver */
1594                                 if (arg) {
1595
1596                                         if (copy_from_user(&iocts, argp,
1597                                              sizeof(isdn_ioctl_struct)))
1598                                                 return -EFAULT;
1599                                         if (strlen(iocts.drvid)) {
1600                                                 drvidx = -1;
1601                                                 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1602                                                         if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1603                                                                 drvidx = i;
1604                                                                 break;
1605                                                         }
1606                                         } else
1607                                                 drvidx = 0;
1608                                         if (drvidx == -1)
1609                                                 return -ENODEV;
1610                                         if (cmd == IIOCSETMAP) {
1611                                                 int loop = 1;
1612
1613                                                 p = (char __user *) iocts.arg;
1614                                                 i = 0;
1615                                                 while (loop) {
1616                                                         int j = 0;
1617
1618                                                         while (1) {
1619                                                                 if (!access_ok(VERIFY_READ, p, 1))
1620                                                                         return -EFAULT;
1621                                                                 get_user(bname[j], p++);
1622                                                                 switch (bname[j]) {
1623                                                                         case '\0':
1624                                                                                 loop = 0;
1625                                                                                 /* Fall through */
1626                                                                         case ',':
1627                                                                                 bname[j] = '\0';
1628                                                                                 strcpy(dev->drv[drvidx]->msn2eaz[i], bname);
1629                                                                                 j = ISDN_MSNLEN;
1630                                                                                 break;
1631                                                                         default:
1632                                                                                 j++;
1633                                                                 }
1634                                                                 if (j >= ISDN_MSNLEN)
1635                                                                         break;
1636                                                         }
1637                                                         if (++i > 9)
1638                                                                 break;
1639                                                 }
1640                                         } else {
1641                                                 p = (char __user *) iocts.arg;
1642                                                 for (i = 0; i < 10; i++) {
1643                                                         sprintf(bname, "%s%s",
1644                                                                 strlen(dev->drv[drvidx]->msn2eaz[i]) ?
1645                                                                 dev->drv[drvidx]->msn2eaz[i] : "_",
1646                                                                 (i < 9) ? "," : "\0");
1647                                                         if (copy_to_user(p, bname, strlen(bname) + 1))
1648                                                                 return -EFAULT;
1649                                                         p += strlen(bname);
1650                                                 }
1651                                         }
1652                                         return 0;
1653                                 } else
1654                                         return -EINVAL;
1655                         case IIOCDBGVAR:
1656                                 if (arg) {
1657                                         if (copy_to_user(argp, &dev, sizeof(ulong)))
1658                                                 return -EFAULT;
1659                                         return 0;
1660                                 } else
1661                                         return -EINVAL;
1662                                 break;
1663                         default:
1664                                 if ((cmd & IIOCDRVCTL) == IIOCDRVCTL)
1665                                         cmd = ((cmd >> _IOC_NRSHIFT) & _IOC_NRMASK) & ISDN_DRVIOCTL_MASK;
1666                                 else
1667                                         return -EINVAL;
1668                                 if (arg) {
1669                                         int i;
1670                                         char *p;
1671                                         if (copy_from_user(&iocts, argp, sizeof(isdn_ioctl_struct)))
1672                                                 return -EFAULT;
1673                                         if (strlen(iocts.drvid)) {
1674                                                 if ((p = strchr(iocts.drvid, ',')))
1675                                                         *p = 0;
1676                                                 drvidx = -1;
1677                                                 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1678                                                         if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1679                                                                 drvidx = i;
1680                                                                 break;
1681                                                         }
1682                                         } else
1683                                                 drvidx = 0;
1684                                         if (drvidx == -1)
1685                                                 return -ENODEV;
1686                                         if (!access_ok(VERIFY_WRITE, argp,
1687                                              sizeof(isdn_ioctl_struct)))
1688                                                 return -EFAULT;
1689                                         c.driver = drvidx;
1690                                         c.command = ISDN_CMD_IOCTL;
1691                                         c.arg = cmd;
1692                                         memcpy(c.parm.num, &iocts.arg, sizeof(ulong));
1693                                         ret = isdn_command(&c);
1694                                         memcpy(&iocts.arg, c.parm.num, sizeof(ulong));
1695                                         if (copy_to_user(argp, &iocts, sizeof(isdn_ioctl_struct)))
1696                                                 return -EFAULT;
1697                                         return ret;
1698                                 } else
1699                                         return -EINVAL;
1700                 }
1701         }
1702 #ifdef CONFIG_ISDN_PPP
1703         if (minor <= ISDN_MINOR_PPPMAX)
1704                 return (isdn_ppp_ioctl(minor - ISDN_MINOR_PPP, file, cmd, arg));
1705 #endif
1706         return -ENODEV;
1707
1708 #undef name
1709 #undef bname
1710 #undef iocts
1711 #undef phone
1712 #undef cfg
1713 }
1714
1715 /*
1716  * Open the device code.
1717  */
1718 static int
1719 isdn_open(struct inode *ino, struct file *filep)
1720 {
1721         uint minor = MINOR(ino->i_rdev);
1722         int drvidx;
1723         int chidx;
1724         int retval = -ENODEV;
1725
1726
1727         if (minor == ISDN_MINOR_STATUS) {
1728                 infostruct *p;
1729
1730                 if ((p = kmalloc(sizeof(infostruct), GFP_KERNEL))) {
1731                         p->next = (char *) dev->infochain;
1732                         p->private = (char *) &(filep->private_data);
1733                         dev->infochain = p;
1734                         /* At opening we allow a single update */
1735                         filep->private_data = (char *) 1;
1736                         retval = 0;
1737                         goto out;
1738                 } else {
1739                         retval = -ENOMEM;
1740                         goto out;
1741                 }
1742         }
1743         if (!dev->channels)
1744                 goto out;
1745         if (minor <= ISDN_MINOR_BMAX) {
1746                 printk(KERN_WARNING "isdn_open minor %d obsolete!\n", minor);
1747                 drvidx = isdn_minor2drv(minor);
1748                 if (drvidx < 0)
1749                         goto out;
1750                 chidx = isdn_minor2chan(minor);
1751                 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1752                         goto out;
1753                 if (!(dev->drv[drvidx]->online & (1 << chidx)))
1754                         goto out;
1755                 isdn_lock_drivers();
1756                 retval = 0;
1757                 goto out;
1758         }
1759         if (minor <= ISDN_MINOR_CTRLMAX) {
1760                 drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1761                 if (drvidx < 0)
1762                         goto out;
1763                 isdn_lock_drivers();
1764                 retval = 0;
1765                 goto out;
1766         }
1767 #ifdef CONFIG_ISDN_PPP
1768         if (minor <= ISDN_MINOR_PPPMAX) {
1769                 retval = isdn_ppp_open(minor - ISDN_MINOR_PPP, filep);
1770                 if (retval == 0)
1771                         isdn_lock_drivers();
1772                 goto out;
1773         }
1774 #endif
1775  out:
1776         nonseekable_open(ino, filep);
1777         return retval;
1778 }
1779
1780 static int
1781 isdn_close(struct inode *ino, struct file *filep)
1782 {
1783         uint minor = MINOR(ino->i_rdev);
1784
1785         lock_kernel();
1786         if (minor == ISDN_MINOR_STATUS) {
1787                 infostruct *p = dev->infochain;
1788                 infostruct *q = NULL;
1789
1790                 while (p) {
1791                         if (p->private == (char *) &(filep->private_data)) {
1792                                 if (q)
1793                                         q->next = p->next;
1794                                 else
1795                                         dev->infochain = (infostruct *) (p->next);
1796                                 kfree(p);
1797                                 goto out;
1798                         }
1799                         q = p;
1800                         p = (infostruct *) (p->next);
1801                 }
1802                 printk(KERN_WARNING "isdn: No private data while closing isdnctrl\n");
1803                 goto out;
1804         }
1805         isdn_unlock_drivers();
1806         if (minor <= ISDN_MINOR_BMAX)
1807                 goto out;
1808         if (minor <= ISDN_MINOR_CTRLMAX) {
1809                 if (dev->profd == current)
1810                         dev->profd = NULL;
1811                 goto out;
1812         }
1813 #ifdef CONFIG_ISDN_PPP
1814         if (minor <= ISDN_MINOR_PPPMAX)
1815                 isdn_ppp_release(minor - ISDN_MINOR_PPP, filep);
1816 #endif
1817
1818  out:
1819         unlock_kernel();
1820         return 0;
1821 }
1822
1823 static struct file_operations isdn_fops =
1824 {
1825         .owner          = THIS_MODULE,
1826         .llseek         = no_llseek,
1827         .read           = isdn_read,
1828         .write          = isdn_write,
1829         .poll           = isdn_poll,
1830         .ioctl          = isdn_ioctl,
1831         .open           = isdn_open,
1832         .release        = isdn_close,
1833 };
1834
1835 char *
1836 isdn_map_eaz2msn(char *msn, int di)
1837 {
1838         isdn_driver_t *this = dev->drv[di];
1839         int i;
1840
1841         if (strlen(msn) == 1) {
1842                 i = msn[0] - '0';
1843                 if ((i >= 0) && (i <= 9))
1844                         if (strlen(this->msn2eaz[i]))
1845                                 return (this->msn2eaz[i]);
1846         }
1847         return (msn);
1848 }
1849
1850 /*
1851  * Find an unused ISDN-channel, whose feature-flags match the
1852  * given L2- and L3-protocols.
1853  */
1854 #define L2V (~(ISDN_FEATURE_L2_V11096|ISDN_FEATURE_L2_V11019|ISDN_FEATURE_L2_V11038))
1855
1856 /*
1857  * This function must be called with holding the dev->lock.
1858  */
1859 int
1860 isdn_get_free_channel(int usage, int l2_proto, int l3_proto, int pre_dev
1861                       ,int pre_chan, char *msn)
1862 {
1863         int i;
1864         ulong features;
1865         ulong vfeatures;
1866
1867         features = ((1 << l2_proto) | (0x10000 << l3_proto));
1868         vfeatures = (((1 << l2_proto) | (0x10000 << l3_proto)) &
1869                      ~(ISDN_FEATURE_L2_V11096|ISDN_FEATURE_L2_V11019|ISDN_FEATURE_L2_V11038));
1870         /* If Layer-2 protocol is V.110, accept drivers with
1871          * transparent feature even if these don't support V.110
1872          * because we can emulate this in linklevel.
1873          */
1874         for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1875                 if (USG_NONE(dev->usage[i]) &&
1876                     (dev->drvmap[i] != -1)) {
1877                         int d = dev->drvmap[i];
1878                         if ((dev->usage[i] & ISDN_USAGE_EXCLUSIVE) &&
1879                         ((pre_dev != d) || (pre_chan != dev->chanmap[i])))
1880                                 continue;
1881                         if (!strcmp(isdn_map_eaz2msn(msn, d), "-"))
1882                                 continue;
1883                         if (dev->usage[i] & ISDN_USAGE_DISABLED)
1884                                 continue; /* usage not allowed */
1885                         if (dev->drv[d]->flags & DRV_FLAG_RUNNING) {
1886                                 if (((dev->drv[d]->interface->features & features) == features) ||
1887                                     (((dev->drv[d]->interface->features & vfeatures) == vfeatures) &&
1888                                      (dev->drv[d]->interface->features & ISDN_FEATURE_L2_TRANS))) {
1889                                         if ((pre_dev < 0) || (pre_chan < 0)) {
1890                                                 dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1891                                                 dev->usage[i] |= usage;
1892                                                 isdn_info_update();
1893                                                 return i;
1894                                         } else {
1895                                                 if ((pre_dev == d) && (pre_chan == dev->chanmap[i])) {
1896                                                         dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1897                                                         dev->usage[i] |= usage;
1898                                                         isdn_info_update();
1899                                                         return i;
1900                                                 }
1901                                         }
1902                                 }
1903                         }
1904                 }
1905         return -1;
1906 }
1907
1908 /*
1909  * Set state of ISDN-channel to 'unused'
1910  */
1911 void
1912 isdn_free_channel(int di, int ch, int usage)
1913 {
1914         int i;
1915
1916         if ((di < 0) || (ch < 0)) {
1917                 printk(KERN_WARNING "%s: called with invalid drv(%d) or channel(%d)\n",
1918                         __FUNCTION__, di, ch);
1919                 return;
1920         }
1921         for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1922                 if (((!usage) || ((dev->usage[i] & ISDN_USAGE_MASK) == usage)) &&
1923                     (dev->drvmap[i] == di) &&
1924                     (dev->chanmap[i] == ch)) {
1925                         dev->usage[i] &= (ISDN_USAGE_NONE | ISDN_USAGE_EXCLUSIVE);
1926                         strcpy(dev->num[i], "???");
1927                         dev->ibytes[i] = 0;
1928                         dev->obytes[i] = 0;
1929 // 20.10.99 JIM, try to reinitialize v110 !
1930                         dev->v110emu[i] = 0;
1931                         atomic_set(&(dev->v110use[i]), 0);
1932                         isdn_v110_close(dev->v110[i]);
1933                         dev->v110[i] = NULL;
1934 // 20.10.99 JIM, try to reinitialize v110 !
1935                         isdn_info_update();
1936                         if (dev->drv[di])
1937                                 skb_queue_purge(&dev->drv[di]->rpqueue[ch]);
1938                 }
1939 }
1940
1941 /*
1942  * Cancel Exclusive-Flag for ISDN-channel
1943  */
1944 void
1945 isdn_unexclusive_channel(int di, int ch)
1946 {
1947         int i;
1948
1949         for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1950                 if ((dev->drvmap[i] == di) &&
1951                     (dev->chanmap[i] == ch)) {
1952                         dev->usage[i] &= ~ISDN_USAGE_EXCLUSIVE;
1953                         isdn_info_update();
1954                         return;
1955                 }
1956 }
1957
1958 /*
1959  *  writebuf replacement for SKB_ABLE drivers
1960  */
1961 static int
1962 isdn_writebuf_stub(int drvidx, int chan, const u_char __user * buf, int len)
1963 {
1964         int ret;
1965         int hl = dev->drv[drvidx]->interface->hl_hdrlen;
1966         struct sk_buff *skb = alloc_skb(hl + len, GFP_ATOMIC);
1967
1968         if (!skb)
1969                 return -ENOMEM;
1970         skb_reserve(skb, hl);
1971         if (copy_from_user(skb_put(skb, len), buf, len))
1972                 return -EFAULT;
1973         ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, 1, skb);
1974         if (ret <= 0)
1975                 dev_kfree_skb(skb);
1976         if (ret > 0)
1977                 dev->obytes[isdn_dc2minor(drvidx, chan)] += ret;
1978         return ret;
1979 }
1980
1981 /*
1982  * Return: length of data on success, -ERRcode on failure.
1983  */
1984 int
1985 isdn_writebuf_skb_stub(int drvidx, int chan, int ack, struct sk_buff *skb)
1986 {
1987         int ret;
1988         struct sk_buff *nskb = NULL;
1989         int v110_ret = skb->len;
1990         int idx = isdn_dc2minor(drvidx, chan);
1991
1992         if (dev->v110[idx]) {
1993                 atomic_inc(&dev->v110use[idx]);
1994                 nskb = isdn_v110_encode(dev->v110[idx], skb);
1995                 atomic_dec(&dev->v110use[idx]);
1996                 if (!nskb)
1997                         return 0;
1998                 v110_ret = *((int *)nskb->data);
1999                 skb_pull(nskb, sizeof(int));
2000                 if (!nskb->len) {
2001                         dev_kfree_skb(nskb);
2002                         return v110_ret;
2003                 }
2004                 /* V.110 must always be acknowledged */
2005                 ack = 1;
2006                 ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, nskb);
2007         } else {
2008                 int hl = dev->drv[drvidx]->interface->hl_hdrlen;
2009
2010                 if( skb_headroom(skb) < hl ){
2011                         /* 
2012                          * This should only occur when new HL driver with
2013                          * increased hl_hdrlen was loaded after netdevice
2014                          * was created and connected to the new driver.
2015                          *
2016                          * The V.110 branch (re-allocates on its own) does
2017                          * not need this
2018                          */
2019                         struct sk_buff * skb_tmp;
2020
2021                         skb_tmp = skb_realloc_headroom(skb, hl);
2022                         printk(KERN_DEBUG "isdn_writebuf_skb_stub: reallocating headroom%s\n", skb_tmp ? "" : " failed");
2023                         if (!skb_tmp) return -ENOMEM; /* 0 better? */
2024                         ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb_tmp);
2025                         if( ret > 0 ){
2026                                 dev_kfree_skb(skb);
2027                         } else {
2028                                 dev_kfree_skb(skb_tmp);
2029                         }
2030                 } else {
2031                         ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb);
2032                 }
2033         }
2034         if (ret > 0) {
2035                 dev->obytes[idx] += ret;
2036                 if (dev->v110[idx]) {
2037                         atomic_inc(&dev->v110use[idx]);
2038                         dev->v110[idx]->skbuser++;
2039                         atomic_dec(&dev->v110use[idx]);
2040                         /* For V.110 return unencoded data length */
2041                         ret = v110_ret;
2042                         /* if the complete frame was send we free the skb;
2043                            if not upper function will requeue the skb */ 
2044                         if (ret == skb->len)
2045                                 dev_kfree_skb(skb);
2046                 }
2047         } else
2048                 if (dev->v110[idx])
2049                         dev_kfree_skb(nskb);
2050         return ret;
2051 }
2052
2053 static int
2054 isdn_add_channels(isdn_driver_t *d, int drvidx, int n, int adding)
2055 {
2056         int j, k, m;
2057
2058         init_waitqueue_head(&d->st_waitq);
2059         if (d->flags & DRV_FLAG_RUNNING)
2060                 return -1;
2061         if (n < 1) return 0;
2062
2063         m = (adding) ? d->channels + n : n;
2064
2065         if (dev->channels + n > ISDN_MAX_CHANNELS) {
2066                 printk(KERN_WARNING "register_isdn: Max. %d channels supported\n",
2067                        ISDN_MAX_CHANNELS);
2068                 return -1;
2069         }
2070
2071         if ((adding) && (d->rcverr))
2072                 kfree(d->rcverr);
2073         if (!(d->rcverr = kmalloc(sizeof(int) * m, GFP_ATOMIC))) {
2074                 printk(KERN_WARNING "register_isdn: Could not alloc rcverr\n");
2075                 return -1;
2076         }
2077         memset((char *) d->rcverr, 0, sizeof(int) * m);
2078
2079         if ((adding) && (d->rcvcount))
2080                 kfree(d->rcvcount);
2081         if (!(d->rcvcount = kmalloc(sizeof(int) * m, GFP_ATOMIC))) {
2082                 printk(KERN_WARNING "register_isdn: Could not alloc rcvcount\n");
2083                 if (!adding)
2084                         kfree(d->rcverr);
2085                 return -1;
2086         }
2087         memset((char *) d->rcvcount, 0, sizeof(int) * m);
2088
2089         if ((adding) && (d->rpqueue)) {
2090                 for (j = 0; j < d->channels; j++)
2091                         skb_queue_purge(&d->rpqueue[j]);
2092                 kfree(d->rpqueue);
2093         }
2094         if (!(d->rpqueue = kmalloc(sizeof(struct sk_buff_head) * m, GFP_ATOMIC))) {
2095                 printk(KERN_WARNING "register_isdn: Could not alloc rpqueue\n");
2096                 if (!adding) {
2097                         kfree(d->rcvcount);
2098                         kfree(d->rcverr);
2099                 }
2100                 return -1; 
2101         }
2102         for (j = 0; j < m; j++) {
2103                 skb_queue_head_init(&d->rpqueue[j]);
2104         }
2105
2106         if ((adding) && (d->rcv_waitq))
2107                 kfree(d->rcv_waitq);
2108         d->rcv_waitq = kmalloc(sizeof(wait_queue_head_t) * 2 * m, GFP_ATOMIC);
2109         if (!d->rcv_waitq) {
2110                 printk(KERN_WARNING "register_isdn: Could not alloc rcv_waitq\n");
2111                 if (!adding) {
2112                         kfree(d->rpqueue);
2113                         kfree(d->rcvcount);
2114                         kfree(d->rcverr);
2115                 }
2116                 return -1;
2117         }
2118         d->snd_waitq = d->rcv_waitq + m;
2119         for (j = 0; j < m; j++) {
2120                 init_waitqueue_head(&d->rcv_waitq[j]);
2121                 init_waitqueue_head(&d->snd_waitq[j]);
2122         }
2123
2124         dev->channels += n;
2125         for (j = d->channels; j < m; j++)
2126                 for (k = 0; k < ISDN_MAX_CHANNELS; k++)
2127                         if (dev->chanmap[k] < 0) {
2128                                 dev->chanmap[k] = j;
2129                                 dev->drvmap[k] = drvidx;
2130                                 break;
2131                         }
2132         d->channels = m;
2133         return 0;
2134 }
2135
2136 /*
2137  * Low-level-driver registration
2138  */
2139
2140 static void
2141 set_global_features(void)
2142 {
2143         int drvidx;
2144
2145         dev->global_features = 0;
2146         for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++) {
2147                 if (!dev->drv[drvidx])
2148                         continue;
2149                 if (dev->drv[drvidx]->interface)
2150                         dev->global_features |= dev->drv[drvidx]->interface->features;
2151         }
2152 }
2153
2154 #ifdef CONFIG_ISDN_DIVERSION
2155
2156 static char *map_drvname(int di)
2157 {
2158   if ((di < 0) || (di >= ISDN_MAX_DRIVERS)) 
2159     return(NULL);
2160   return(dev->drvid[di]); /* driver name */
2161 } /* map_drvname */
2162
2163 static int map_namedrv(char *id)
2164 {  int i;
2165
2166    for (i = 0; i < ISDN_MAX_DRIVERS; i++)
2167     { if (!strcmp(dev->drvid[i],id)) 
2168         return(i);
2169     }
2170    return(-1);
2171 } /* map_namedrv */
2172
2173 int DIVERT_REG_NAME(isdn_divert_if *i_div)
2174 {
2175   if (i_div->if_magic != DIVERT_IF_MAGIC) 
2176     return(DIVERT_VER_ERR);
2177   switch (i_div->cmd)
2178     {
2179       case DIVERT_CMD_REL:
2180         if (divert_if != i_div) 
2181           return(DIVERT_REL_ERR);
2182         divert_if = NULL; /* free interface */
2183         return(DIVERT_NO_ERR);
2184
2185       case DIVERT_CMD_REG:
2186         if (divert_if) 
2187           return(DIVERT_REG_ERR);
2188         i_div->ll_cmd = isdn_command; /* set command function */
2189         i_div->drv_to_name = map_drvname; 
2190         i_div->name_to_drv = map_namedrv; 
2191         divert_if = i_div; /* remember interface */
2192         return(DIVERT_NO_ERR);
2193
2194       default:
2195         return(DIVERT_CMD_ERR);   
2196     }
2197 } /* DIVERT_REG_NAME */
2198
2199 EXPORT_SYMBOL(DIVERT_REG_NAME);
2200
2201 #endif /* CONFIG_ISDN_DIVERSION */
2202
2203
2204 EXPORT_SYMBOL(register_isdn);
2205 #ifdef CONFIG_ISDN_PPP
2206 EXPORT_SYMBOL(isdn_ppp_register_compressor);
2207 EXPORT_SYMBOL(isdn_ppp_unregister_compressor);
2208 #endif
2209
2210 int
2211 register_isdn(isdn_if * i)
2212 {
2213         isdn_driver_t *d;
2214         int j;
2215         ulong flags;
2216         int drvidx;
2217
2218         if (dev->drivers >= ISDN_MAX_DRIVERS) {
2219                 printk(KERN_WARNING "register_isdn: Max. %d drivers supported\n",
2220                        ISDN_MAX_DRIVERS);
2221                 return 0;
2222         }
2223         if (!i->writebuf_skb) {
2224                 printk(KERN_WARNING "register_isdn: No write routine given.\n");
2225                 return 0;
2226         }
2227         if (!(d = kmalloc(sizeof(isdn_driver_t), GFP_KERNEL))) {
2228                 printk(KERN_WARNING "register_isdn: Could not alloc driver-struct\n");
2229                 return 0;
2230         }
2231         memset((char *) d, 0, sizeof(isdn_driver_t));
2232
2233         d->maxbufsize = i->maxbufsize;
2234         d->pktcount = 0;
2235         d->stavail = 0;
2236         d->flags = DRV_FLAG_LOADED;
2237         d->online = 0;
2238         d->interface = i;
2239         d->channels = 0;
2240         spin_lock_irqsave(&dev->lock, flags);
2241         for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++)
2242                 if (!dev->drv[drvidx])
2243                         break;
2244         if (isdn_add_channels(d, drvidx, i->channels, 0)) {
2245                 spin_unlock_irqrestore(&dev->lock, flags);
2246                 kfree(d);
2247                 return 0;
2248         }
2249         i->channels = drvidx;
2250         i->rcvcallb_skb = isdn_receive_skb_callback;
2251         i->statcallb = isdn_status_callback;
2252         if (!strlen(i->id))
2253                 sprintf(i->id, "line%d", drvidx);
2254         for (j = 0; j < drvidx; j++)
2255                 if (!strcmp(i->id, dev->drvid[j]))
2256                         sprintf(i->id, "line%d", drvidx);
2257         dev->drv[drvidx] = d;
2258         strcpy(dev->drvid[drvidx], i->id);
2259         isdn_info_update();
2260         dev->drivers++;
2261         set_global_features();
2262         spin_unlock_irqrestore(&dev->lock, flags);
2263         return 1;
2264 }
2265
2266 /*
2267  *****************************************************************************
2268  * And now the modules code.
2269  *****************************************************************************
2270  */
2271
2272 static char *
2273 isdn_getrev(const char *revision)
2274 {
2275         char *rev;
2276         char *p;
2277
2278         if ((p = strchr(revision, ':'))) {
2279                 rev = p + 2;
2280                 p = strchr(rev, '$');
2281                 *--p = 0;
2282         } else
2283                 rev = "???";
2284         return rev;
2285 }
2286
2287 /*
2288  * Allocate and initialize all data, register modem-devices
2289  */
2290 static int __init isdn_init(void)
2291 {
2292         int i;
2293         char tmprev[50];
2294
2295         if (!(dev = (isdn_dev *) vmalloc(sizeof(isdn_dev)))) {
2296                 printk(KERN_WARNING "isdn: Could not allocate device-struct.\n");
2297                 return -EIO;
2298         }
2299         memset((char *) dev, 0, sizeof(isdn_dev));
2300         init_timer(&dev->timer);
2301         dev->timer.function = isdn_timer_funct;
2302         spin_lock_init(&dev->lock);
2303         spin_lock_init(&dev->timerlock);
2304 #ifdef MODULE
2305         dev->owner = THIS_MODULE;
2306 #endif
2307         init_MUTEX(&dev->sem);
2308         init_waitqueue_head(&dev->info_waitq);
2309         for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2310                 dev->drvmap[i] = -1;
2311                 dev->chanmap[i] = -1;
2312                 dev->m_idx[i] = -1;
2313                 strcpy(dev->num[i], "???");
2314                 init_waitqueue_head(&dev->mdm.info[i].open_wait);
2315                 init_waitqueue_head(&dev->mdm.info[i].close_wait);
2316         }
2317         if (register_chrdev(ISDN_MAJOR, "isdn", &isdn_fops)) {
2318                 printk(KERN_WARNING "isdn: Could not register control devices\n");
2319                 vfree(dev);
2320                 return -EIO;
2321         }
2322         if ((isdn_tty_modem_init()) < 0) {
2323                 printk(KERN_WARNING "isdn: Could not register tty devices\n");
2324                 vfree(dev);
2325                 unregister_chrdev(ISDN_MAJOR, "isdn");
2326                 return -EIO;
2327         }
2328 #ifdef CONFIG_ISDN_PPP
2329         if (isdn_ppp_init() < 0) {
2330                 printk(KERN_WARNING "isdn: Could not create PPP-device-structs\n");
2331                 isdn_tty_exit();
2332                 unregister_chrdev(ISDN_MAJOR, "isdn");
2333                 vfree(dev);
2334                 return -EIO;
2335         }
2336 #endif                          /* CONFIG_ISDN_PPP */
2337
2338         strcpy(tmprev, isdn_revision);
2339         printk(KERN_NOTICE "ISDN subsystem Rev: %s/", isdn_getrev(tmprev));
2340         strcpy(tmprev, isdn_tty_revision);
2341         printk("%s/", isdn_getrev(tmprev));
2342         strcpy(tmprev, isdn_net_revision);
2343         printk("%s/", isdn_getrev(tmprev));
2344         strcpy(tmprev, isdn_ppp_revision);
2345         printk("%s/", isdn_getrev(tmprev));
2346         strcpy(tmprev, isdn_audio_revision);
2347         printk("%s/", isdn_getrev(tmprev));
2348         strcpy(tmprev, isdn_v110_revision);
2349         printk("%s", isdn_getrev(tmprev));
2350
2351 #ifdef MODULE
2352         printk(" loaded\n");
2353 #else
2354         printk("\n");
2355 #endif
2356         isdn_info_update();
2357         return 0;
2358 }
2359
2360 /*
2361  * Unload module
2362  */
2363 static void __exit isdn_exit(void)
2364 {
2365 #ifdef CONFIG_ISDN_PPP
2366         isdn_ppp_cleanup();
2367 #endif
2368         if (isdn_net_rmall() < 0) {
2369                 printk(KERN_WARNING "isdn: net-device busy, remove cancelled\n");
2370                 return;
2371         }
2372         isdn_tty_exit();
2373         unregister_chrdev(ISDN_MAJOR, "isdn");
2374         del_timer(&dev->timer);
2375         /* call vfree with interrupts enabled, else it will hang */
2376         vfree(dev);
2377         printk(KERN_NOTICE "ISDN-subsystem unloaded\n");
2378 }
2379
2380 module_init(isdn_init);
2381 module_exit(isdn_exit);