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