blob: b87d9e577be21f75d812adc1992a2685de1bb73a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $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
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/vmalloc.h>
19#include <linux/isdn.h>
Arnd Bergmann76a64922010-07-11 11:18:53 +000020#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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
39MODULE_DESCRIPTION("ISDN4Linux: link layer");
40MODULE_AUTHOR("Fritz Elfert");
41MODULE_LICENSE("GPL");
42
43isdn_dev *dev;
44
Arnd Bergmann76a64922010-07-11 11:18:53 +000045static DEFINE_MUTEX(isdn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static char *isdn_revision = "$Revision: 1.1.2.3 $";
47
48extern char *isdn_net_revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#ifdef CONFIG_ISDN_PPP
50extern char *isdn_ppp_revision;
51#else
52static char *isdn_ppp_revision = ": none $";
53#endif
54#ifdef CONFIG_ISDN_AUDIO
55extern char *isdn_audio_revision;
56#else
57static char *isdn_audio_revision = ": none $";
58#endif
59extern char *isdn_v110_revision;
60
61#ifdef CONFIG_ISDN_DIVERSION
62static isdn_divert_if *divert_if; /* = NULL */
63#endif /* CONFIG_ISDN_DIVERSION */
64
65
66static int isdn_writebuf_stub(int, int, const u_char __user *, int);
67static void set_global_features(void);
68static int isdn_wildmat(char *s, char *p);
Adrian Bunk3e206b02005-06-25 14:58:35 -070069static int isdn_add_channels(isdn_driver_t *d, int drvidx, int n, int adding);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71static inline void
72isdn_lock_driver(isdn_driver_t *drv)
73{
74 try_module_get(drv->interface->owner);
75 drv->locks++;
76}
77
78void
79isdn_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
90static inline void
91isdn_unlock_driver(isdn_driver_t *drv)
92{
93 if (drv->locks > 0) {
94 drv->locks--;
95 module_put(drv->interface->owner);
96 }
97}
98
99void
100isdn_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)
112void
Joe Perches475be4d2012-02-19 19:52:38 -0800113isdn_dumppkt(char *s, u_char *p, int len, int dumplen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
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 */
128static int
129isdn_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
156static int
157isdn_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))
Joe Perches475be4d2012-02-19 19:52:38 -0800165 return (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 for (; *p; s++, p++)
167 switch (*p) {
Joe Perches475be4d2012-02-19 19:52:38 -0800168 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] == '^')))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 p++;
Joe Perches475be4d2012-02-19 19:52:38 -0800191 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Joe Perches475be4d2012-02-19 19:52:38 -0800199 return (*s == '\0') ? 0 : nostar;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Joe Perches475be4d2012-02-19 19:52:38 -0800202int isdn_msncmp(const char *msn1, const char *msn2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Joe Perches475be4d2012-02-19 19:52:38 -0800204 char TmpMsn1[ISDN_MSNLEN];
205 char TmpMsn2[ISDN_MSNLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 char *p;
207
Joe Perches475be4d2012-02-19 19:52:38 -0800208 for (p = TmpMsn1; *msn1 && *msn1 != ':';) // Strip off a SPID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 *p++ = *msn1++;
210 *p = '\0';
211
Joe Perches475be4d2012-02-19 19:52:38 -0800212 for (p = TmpMsn2; *msn2 && *msn2 != ':';) // Strip off a SPID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 *p++ = *msn2++;
214 *p = '\0';
215
Joe Perches475be4d2012-02-19 19:52:38 -0800216 return isdn_wildmat(TmpMsn1, TmpMsn2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219int
220isdn_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
229static int isdn_timer_cnt1 = 0;
230static int isdn_timer_cnt2 = 0;
231static int isdn_timer_cnt3 = 0;
232
233static void
234isdn_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 }
Joe Perches475be4d2012-02-19 19:52:38 -0800264 if (tf)
265 mod_timer(&dev->timer, jiffies + ISDN_TIMER_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268void
269isdn_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)
Joe Perches475be4d2012-02-19 19:52:38 -0800286 mod_timer(&dev->timer, jiffies + ISDN_TIMER_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 spin_unlock_irqrestore(&dev->timerlock, flags);
288}
289
290/*
291 * Receive a packet from B-Channel. (Called from low-level-module)
292 */
293static void
294isdn_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;
Joe Perches475be4d2012-02-19 19:52:38 -0800304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /* 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 */
336int
337isdn_command(isdn_ctrl *cmd)
338{
339 if (cmd->driver == -1) {
340 printk(KERN_WARNING "isdn_command command(%x) driver -1\n", cmd->command);
Joe Perches475be4d2012-02-19 19:52:38 -0800341 return (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Karsten Keil09fca292006-06-29 13:16:29 +0200343 if (!dev->drv[cmd->driver]) {
344 printk(KERN_WARNING "isdn_command command(%x) dev->drv[%d] NULL\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800345 cmd->command, cmd->driver);
346 return (1);
Karsten Keil09fca292006-06-29 13:16:29 +0200347 }
348 if (!dev->drv[cmd->driver]->interface) {
349 printk(KERN_WARNING "isdn_command command(%x) dev->drv[%d]->interface NULL\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800350 cmd->command, cmd->driver);
351 return (1);
Karsten Keil09fca292006-06-29 13:16:29 +0200352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 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
Joe Perches475be4d2012-02-19 19:52:38 -0800357 >> ISDN_FEATURE_L2_SHIFT) &
358 ISDN_FEATURE_L2_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 unsigned long l2_feature = (1 << l2prot);
360
361 switch (l2prot) {
Joe Perches475be4d2012-02-19 19:52:38 -0800362 case ISDN_PROTO_L2_V11096:
363 case ISDN_PROTO_L2_V11019:
364 case ISDN_PROTO_L2_V11038:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /* If V.110 requested, but not supported by
366 * HL-driver, set emulator-flag and change
367 * Layer-2 to transparent
368 */
Joe Perches475be4d2012-02-19 19:52:38 -0800369 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376 }
377 return dev->drv[cmd->driver]->interface->command(cmd);
378}
379
380void
381isdn_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/*
Joe Perches475be4d2012-02-19 19:52:38 -0800395 * Begin of a CAPI like LL<->HL interface, currently used only for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 * supplementary service (CAPI 2.0 part III)
397 */
398#include <linux/isdn/capicmd.h>
399
Adrian Bunk3e206b02005-06-25 14:58:35 -0700400static int
David S. Miller50a7c112011-04-17 16:45:51 -0700401isdn_capi_rec_hl_msg(capi_msg *cm)
402{
Joe Perches475be4d2012-02-19 19:52:38 -0800403 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410}
411
412static int
Joe Perches475be4d2012-02-19 19:52:38 -0800413isdn_status_callback(isdn_ctrl *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
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) {
Joe Perches475be4d2012-02-19 19:52:38 -0800426 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456#ifdef ISDN_DEBUG_STATCALLB
Joe Perches475be4d2012-02-19 19:52:38 -0800457 printk(KERN_DEBUG "ICALL (net): %d %ld %s\n", di, c->arg, c->parm.num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458#endif
Joe Perches475be4d2012-02-19 19:52:38 -0800459 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 cmd.driver = di;
485 cmd.arg = c->arg;
486 cmd.command = ISDN_CMD_HANGUP;
487 isdn_command(&cmd);
Joe Perches475be4d2012-02-19 19:52:38 -0800488 retval = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Joe Perches475be4d2012-02-19 19:52:38 -0800490 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 isdn_command(&cmd);
Joe Perches475be4d2012-02-19 19:52:38 -0800502 retval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 break;
Joe Perches475be4d2012-02-19 19:52:38 -0800506
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return 0;
Joe Perches475be4d2012-02-19 19:52:38 -0800540 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))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 break;
Joe Perches475be4d2012-02-19 19:52:38 -0800577 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716#ifdef CONFIG_ISDN_TTY_FAX
Joe Perches475be4d2012-02-19 19:52:38 -0800717 case ISDN_STAT_FAXIND:
718 isdn_tty_stat_callback(i, c);
719 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720#endif
721#ifdef CONFIG_ISDN_AUDIO
Joe Perches475be4d2012-02-19 19:52:38 -0800722 case ISDN_STAT_AUDIO:
723 isdn_tty_stat_callback(i, c);
724 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725#endif
726#ifdef CONFIG_ISDN_DIVERSION
Joe Perches475be4d2012-02-19 19:52:38 -0800727 case ISDN_STAT_PROT:
728 case ISDN_STAT_REDIR:
729 if (divert_if)
730 return (divert_if->stat_callback(c));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731#endif /* CONFIG_ISDN_DIVERSION */
Joe Perches475be4d2012-02-19 19:52:38 -0800732 default:
733 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735 return 0;
736}
737
738/*
739 * Get integer from char-pointer, set pointer to end of number
740 */
741int
742isdn_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 *
Joe Perches475be4d2012-02-19 19:52:38 -0800757 * Be aware that this is not an atomic operation when sleep != 0, even though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 * 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
Anand Gadiyar411c9402009-07-07 15:24:23 +0530760 * to be dangerous and for debugging purpose only). The inode semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * 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
Joe Perches475be4d2012-02-19 19:52:38 -0800764 * of the mapping (di,ch)<->minor, happen during the sleep? --he
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 */
766int
Joe Perches475be4d2012-02-19 19:52:38 -0800767isdn_readbchan(int di, int channel, u_char *buf, u_char *fp, int len, wait_queue_head_t *sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
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;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300828 skb_copy_from_linear_data(skb, cp, count_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 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
Alan Cox33f0f882006-01-09 20:54:13 -0800865/*
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
Anand Gadiyar411c9402009-07-07 15:24:23 +0530872 * to be dangerous and for debugging purpose only). The inode semaphore
Alan Cox33f0f882006-01-09 20:54:13 -0800873 * 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 */
878int
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100879isdn_readbchan_tty(int di, int channel, struct tty_port *port, int cisco_hack)
Alan Cox33f0f882006-01-09 20:54:13 -0800880{
881 int count;
882 int count_pull;
883 int count_put;
884 int dflag;
885 struct sk_buff *skb;
886 char last = 0;
887 int len;
888
889 if (!dev->drv[di])
890 return 0;
891 if (skb_queue_empty(&dev->drv[di]->rpqueue[channel]))
Joe Perches475be4d2012-02-19 19:52:38 -0800892 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -0800893
Jiri Slaby227434f2013-01-03 15:53:01 +0100894 len = tty_buffer_request_room(port, dev->drv[di]->rcvcount[channel]);
Joe Perches475be4d2012-02-19 19:52:38 -0800895 if (len == 0)
Alan Cox33f0f882006-01-09 20:54:13 -0800896 return len;
897
898 count = 0;
899 while (len) {
900 if (!(skb = skb_peek(&dev->drv[di]->rpqueue[channel])))
901 break;
902#ifdef CONFIG_ISDN_AUDIO
903 if (ISDN_AUDIO_SKB_LOCK(skb))
904 break;
905 ISDN_AUDIO_SKB_LOCK(skb) = 1;
906 if ((ISDN_AUDIO_SKB_DLECOUNT(skb)) || (dev->drv[di]->DLEflag & (1 << channel))) {
907 char *p = skb->data;
908 unsigned long DLEmask = (1 << channel);
909
910 dflag = 0;
911 count_pull = count_put = 0;
912 while ((count_pull < skb->len) && (len > 0)) {
Matthias Goebl7fde4d72008-01-04 03:45:28 -0800913 /* push every character but the last to the tty buffer directly */
Joe Perches475be4d2012-02-19 19:52:38 -0800914 if (count_put)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100915 tty_insert_flip_char(port, last, TTY_NORMAL);
Alan Cox33f0f882006-01-09 20:54:13 -0800916 len--;
917 if (dev->drv[di]->DLEflag & DLEmask) {
918 last = DLE;
919 dev->drv[di]->DLEflag &= ~DLEmask;
920 } else {
921 last = *p;
922 if (last == DLE) {
923 dev->drv[di]->DLEflag |= DLEmask;
924 (ISDN_AUDIO_SKB_DLECOUNT(skb))--;
925 }
926 p++;
927 count_pull++;
928 }
929 count_put++;
930 }
931 if (count_pull >= skb->len)
932 dflag = 1;
933 } else {
934#endif
935 /* No DLE's in buff, so simply copy it */
936 dflag = 1;
937 if ((count_pull = skb->len) > len) {
938 count_pull = len;
939 dflag = 0;
940 }
941 count_put = count_pull;
Joe Perches475be4d2012-02-19 19:52:38 -0800942 if (count_put > 1)
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100943 tty_insert_flip_string(port, skb->data, count_put - 1);
Karsten Keil916d1542006-06-26 20:21:01 +0200944 last = skb->data[count_put - 1];
Alan Cox33f0f882006-01-09 20:54:13 -0800945 len -= count_put;
946#ifdef CONFIG_ISDN_AUDIO
947 }
948#endif
949 count += count_put;
950 if (dflag) {
951 /* We got all the data in this buff.
952 * Now we can dequeue it.
953 */
Joe Perches475be4d2012-02-19 19:52:38 -0800954 if (cisco_hack)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100955 tty_insert_flip_char(port, last, 0xFF);
Alan Cox33f0f882006-01-09 20:54:13 -0800956 else
Jiri Slaby92a19f92013-01-03 15:53:03 +0100957 tty_insert_flip_char(port, last, TTY_NORMAL);
Alan Cox33f0f882006-01-09 20:54:13 -0800958#ifdef CONFIG_ISDN_AUDIO
959 ISDN_AUDIO_SKB_LOCK(skb) = 0;
960#endif
961 skb = skb_dequeue(&dev->drv[di]->rpqueue[channel]);
962 dev_kfree_skb(skb);
963 } else {
Jiri Slaby92a19f92013-01-03 15:53:03 +0100964 tty_insert_flip_char(port, last, TTY_NORMAL);
Alan Cox33f0f882006-01-09 20:54:13 -0800965 /* Not yet emptied this buff, so it
966 * must stay in the queue, for further calls
967 * but we pull off the data we got until now.
968 */
969 skb_pull(skb, count_pull);
970#ifdef CONFIG_ISDN_AUDIO
971 ISDN_AUDIO_SKB_LOCK(skb) = 0;
972#endif
973 }
974 dev->drv[di]->rcvcount[channel] -= count_put;
975 }
976 return count;
977}
978
979
Harvey Harrisond4f77512008-03-05 18:37:39 -0800980static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981isdn_minor2drv(int minor)
982{
983 return (dev->drvmap[minor]);
984}
985
Harvey Harrisond4f77512008-03-05 18:37:39 -0800986static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987isdn_minor2chan(int minor)
988{
989 return (dev->chanmap[minor]);
990}
991
992static char *
993isdn_statstr(void)
994{
995 static char istatbuf[2048];
996 char *p;
997 int i;
998
999 sprintf(istatbuf, "idmap:\t");
1000 p = istatbuf + strlen(istatbuf);
1001 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1002 sprintf(p, "%s ", (dev->drvmap[i] < 0) ? "-" : dev->drvid[dev->drvmap[i]]);
1003 p = istatbuf + strlen(istatbuf);
1004 }
1005 sprintf(p, "\nchmap:\t");
1006 p = istatbuf + strlen(istatbuf);
1007 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1008 sprintf(p, "%d ", dev->chanmap[i]);
1009 p = istatbuf + strlen(istatbuf);
1010 }
1011 sprintf(p, "\ndrmap:\t");
1012 p = istatbuf + strlen(istatbuf);
1013 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1014 sprintf(p, "%d ", dev->drvmap[i]);
1015 p = istatbuf + strlen(istatbuf);
1016 }
1017 sprintf(p, "\nusage:\t");
1018 p = istatbuf + strlen(istatbuf);
1019 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1020 sprintf(p, "%d ", dev->usage[i]);
1021 p = istatbuf + strlen(istatbuf);
1022 }
1023 sprintf(p, "\nflags:\t");
1024 p = istatbuf + strlen(istatbuf);
1025 for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
1026 if (dev->drv[i]) {
1027 sprintf(p, "%ld ", dev->drv[i]->online);
1028 p = istatbuf + strlen(istatbuf);
1029 } else {
1030 sprintf(p, "? ");
1031 p = istatbuf + strlen(istatbuf);
1032 }
1033 }
1034 sprintf(p, "\nphone:\t");
1035 p = istatbuf + strlen(istatbuf);
1036 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1037 sprintf(p, "%s ", dev->num[i]);
1038 p = istatbuf + strlen(istatbuf);
1039 }
1040 sprintf(p, "\n");
1041 return istatbuf;
1042}
1043
1044/* Module interface-code */
1045
1046void
1047isdn_info_update(void)
1048{
1049 infostruct *p = dev->infochain;
1050
1051 while (p) {
1052 *(p->private) = 1;
1053 p = (infostruct *) p->next;
1054 }
1055 wake_up_interruptible(&(dev->info_waitq));
1056}
1057
1058static ssize_t
Joe Perches475be4d2012-02-19 19:52:38 -08001059isdn_read(struct file *file, char __user *buf, size_t count, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Josef Sipek4482dfa2006-12-08 02:37:13 -08001061 uint minor = iminor(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 int len = 0;
1063 int drvidx;
1064 int chidx;
1065 int retval;
1066 char *p;
1067
Arnd Bergmann76a64922010-07-11 11:18:53 +00001068 mutex_lock(&isdn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (minor == ISDN_MINOR_STATUS) {
1070 if (!file->private_data) {
1071 if (file->f_flags & O_NONBLOCK) {
1072 retval = -EAGAIN;
1073 goto out;
1074 }
1075 interruptible_sleep_on(&(dev->info_waitq));
1076 }
1077 p = isdn_statstr();
1078 file->private_data = NULL;
1079 if ((len = strlen(p)) <= count) {
1080 if (copy_to_user(buf, p, len)) {
1081 retval = -EFAULT;
1082 goto out;
1083 }
1084 *off += len;
1085 retval = len;
1086 goto out;
1087 }
1088 retval = 0;
1089 goto out;
1090 }
1091 if (!dev->drivers) {
1092 retval = -ENODEV;
1093 goto out;
1094 }
1095 if (minor <= ISDN_MINOR_BMAX) {
1096 printk(KERN_WARNING "isdn_read minor %d obsolete!\n", minor);
1097 drvidx = isdn_minor2drv(minor);
1098 if (drvidx < 0) {
1099 retval = -ENODEV;
1100 goto out;
1101 }
1102 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
1103 retval = -ENODEV;
1104 goto out;
1105 }
1106 chidx = isdn_minor2chan(minor);
1107 if (!(p = kmalloc(count, GFP_KERNEL))) {
1108 retval = -ENOMEM;
1109 goto out;
1110 }
1111 len = isdn_readbchan(drvidx, chidx, p, NULL, count,
1112 &dev->drv[drvidx]->rcv_waitq[chidx]);
1113 *off += len;
Joe Perches475be4d2012-02-19 19:52:38 -08001114 if (copy_to_user(buf, p, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 len = -EFAULT;
1116 kfree(p);
1117 retval = len;
1118 goto out;
1119 }
1120 if (minor <= ISDN_MINOR_CTRLMAX) {
1121 drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1122 if (drvidx < 0) {
1123 retval = -ENODEV;
1124 goto out;
1125 }
1126 if (!dev->drv[drvidx]->stavail) {
1127 if (file->f_flags & O_NONBLOCK) {
1128 retval = -EAGAIN;
1129 goto out;
1130 }
1131 interruptible_sleep_on(&(dev->drv[drvidx]->st_waitq));
1132 }
1133 if (dev->drv[drvidx]->interface->readstat) {
1134 if (count > dev->drv[drvidx]->stavail)
1135 count = dev->drv[drvidx]->stavail;
Jeff Garzik04518bf2006-10-17 00:10:39 -07001136 len = dev->drv[drvidx]->interface->readstat(buf, count,
Joe Perches475be4d2012-02-19 19:52:38 -08001137 drvidx, isdn_minor2chan(minor - ISDN_MINOR_CTRL));
Jeff Garzik04518bf2006-10-17 00:10:39 -07001138 if (len < 0) {
1139 retval = len;
1140 goto out;
1141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 } else {
1143 len = 0;
1144 }
1145 if (len)
1146 dev->drv[drvidx]->stavail -= len;
1147 else
1148 dev->drv[drvidx]->stavail = 0;
1149 *off += len;
1150 retval = len;
1151 goto out;
1152 }
1153#ifdef CONFIG_ISDN_PPP
1154 if (minor <= ISDN_MINOR_PPPMAX) {
1155 retval = isdn_ppp_read(minor - ISDN_MINOR_PPP, file, buf, count);
1156 goto out;
1157 }
1158#endif
1159 retval = -ENODEV;
Joe Perches475be4d2012-02-19 19:52:38 -08001160out:
Arnd Bergmann76a64922010-07-11 11:18:53 +00001161 mutex_unlock(&isdn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return retval;
1163}
1164
1165static ssize_t
Joe Perches475be4d2012-02-19 19:52:38 -08001166isdn_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Josef Sipek4482dfa2006-12-08 02:37:13 -08001168 uint minor = iminor(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 int drvidx;
1170 int chidx;
1171 int retval;
1172
1173 if (minor == ISDN_MINOR_STATUS)
1174 return -EPERM;
1175 if (!dev->drivers)
1176 return -ENODEV;
1177
Arnd Bergmann76a64922010-07-11 11:18:53 +00001178 mutex_lock(&isdn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (minor <= ISDN_MINOR_BMAX) {
1180 printk(KERN_WARNING "isdn_write minor %d obsolete!\n", minor);
1181 drvidx = isdn_minor2drv(minor);
1182 if (drvidx < 0) {
1183 retval = -ENODEV;
1184 goto out;
1185 }
1186 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
1187 retval = -ENODEV;
1188 goto out;
1189 }
1190 chidx = isdn_minor2chan(minor);
Jesper Juhld20d04b2006-06-23 02:05:28 -07001191 while ((retval = isdn_writebuf_stub(drvidx, chidx, buf, count)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 interruptible_sleep_on(&dev->drv[drvidx]->snd_waitq[chidx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 goto out;
1194 }
1195 if (minor <= ISDN_MINOR_CTRLMAX) {
1196 drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1197 if (drvidx < 0) {
1198 retval = -ENODEV;
1199 goto out;
1200 }
1201 /*
1202 * We want to use the isdnctrl device to load the firmware
1203 *
1204 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1205 return -ENODEV;
Joe Perches475be4d2012-02-19 19:52:38 -08001206 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (dev->drv[drvidx]->interface->writecmd)
1208 retval = dev->drv[drvidx]->interface->
Karsten Keild39d5ed2007-10-08 12:52:09 +02001209 writecmd(buf, count, drvidx,
Joe Perches475be4d2012-02-19 19:52:38 -08001210 isdn_minor2chan(minor - ISDN_MINOR_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 else
1212 retval = count;
1213 goto out;
1214 }
1215#ifdef CONFIG_ISDN_PPP
1216 if (minor <= ISDN_MINOR_PPPMAX) {
1217 retval = isdn_ppp_write(minor - ISDN_MINOR_PPP, file, buf, count);
1218 goto out;
1219 }
1220#endif
1221 retval = -ENODEV;
Joe Perches475be4d2012-02-19 19:52:38 -08001222out:
Arnd Bergmann76a64922010-07-11 11:18:53 +00001223 mutex_unlock(&isdn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return retval;
1225}
1226
1227static unsigned int
Joe Perches475be4d2012-02-19 19:52:38 -08001228isdn_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
1230 unsigned int mask = 0;
Josef Sipek4482dfa2006-12-08 02:37:13 -08001231 unsigned int minor = iminor(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 int drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1233
Arnd Bergmann76a64922010-07-11 11:18:53 +00001234 mutex_lock(&isdn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (minor == ISDN_MINOR_STATUS) {
1236 poll_wait(file, &(dev->info_waitq), wait);
1237 /* mask = POLLOUT | POLLWRNORM; */
1238 if (file->private_data) {
1239 mask |= POLLIN | POLLRDNORM;
1240 }
1241 goto out;
1242 }
1243 if (minor >= ISDN_MINOR_CTRL && minor <= ISDN_MINOR_CTRLMAX) {
1244 if (drvidx < 0) {
1245 /* driver deregistered while file open */
1246 mask = POLLHUP;
1247 goto out;
1248 }
1249 poll_wait(file, &(dev->drv[drvidx]->st_waitq), wait);
1250 mask = POLLOUT | POLLWRNORM;
1251 if (dev->drv[drvidx]->stavail) {
1252 mask |= POLLIN | POLLRDNORM;
1253 }
1254 goto out;
1255 }
1256#ifdef CONFIG_ISDN_PPP
1257 if (minor <= ISDN_MINOR_PPPMAX) {
1258 mask = isdn_ppp_poll(file, wait);
1259 goto out;
1260 }
1261#endif
1262 mask = POLLERR;
Joe Perches475be4d2012-02-19 19:52:38 -08001263out:
Arnd Bergmann76a64922010-07-11 11:18:53 +00001264 mutex_unlock(&isdn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return mask;
1266}
1267
1268
1269static int
Arnd Bergmann703c6312010-04-27 00:24:02 +02001270isdn_ioctl(struct file *file, uint cmd, ulong arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Arnd Bergmann703c6312010-04-27 00:24:02 +02001272 uint minor = iminor(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 isdn_ctrl c;
1274 int drvidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int ret;
1276 int i;
1277 char __user *p;
1278 char *s;
1279 union iocpar {
1280 char name[10];
1281 char bname[22];
1282 isdn_ioctl_struct iocts;
1283 isdn_net_ioctl_phone phone;
1284 isdn_net_ioctl_cfg cfg;
1285 } iocpar;
1286 void __user *argp = (void __user *)arg;
1287
1288#define name iocpar.name
1289#define bname iocpar.bname
1290#define iocts iocpar.iocts
1291#define phone iocpar.phone
1292#define cfg iocpar.cfg
1293
1294 if (minor == ISDN_MINOR_STATUS) {
1295 switch (cmd) {
Joe Perches475be4d2012-02-19 19:52:38 -08001296 case IIOCGETDVR:
1297 return (TTY_DV +
1298 (NET_DV << 8) +
1299 (INF_DV << 16));
1300 case IIOCGETCPS:
1301 if (arg) {
1302 ulong __user *p = argp;
1303 int i;
1304 if (!access_ok(VERIFY_WRITE, p,
1305 sizeof(ulong) * ISDN_MAX_CHANNELS * 2))
1306 return -EFAULT;
1307 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1308 put_user(dev->ibytes[i], p++);
1309 put_user(dev->obytes[i], p++);
1310 }
1311 return 0;
1312 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return -EINVAL;
Joe Perches475be4d2012-02-19 19:52:38 -08001314 break;
Joe Perches475be4d2012-02-19 19:52:38 -08001315 case IIOCNETGPN:
1316 /* Get peer phone number of a connected
1317 * isdn network interface */
1318 if (arg) {
1319 if (copy_from_user(&phone, argp, sizeof(phone)))
1320 return -EFAULT;
1321 return isdn_net_getpeer(&phone, argp);
1322 } else
1323 return -EINVAL;
Joe Perches475be4d2012-02-19 19:52:38 -08001324 default:
1325 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327 }
1328 if (!dev->drivers)
1329 return -ENODEV;
1330 if (minor <= ISDN_MINOR_BMAX) {
1331 drvidx = isdn_minor2drv(minor);
1332 if (drvidx < 0)
1333 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1335 return -ENODEV;
1336 return 0;
1337 }
1338 if (minor <= ISDN_MINOR_CTRLMAX) {
1339/*
1340 * isdn net devices manage lots of configuration variables as linked lists.
1341 * Those lists must only be manipulated from user space. Some of the ioctl's
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001342 * service routines access user space and are not atomic. Therefore, ioctl's
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 * manipulating the lists and ioctl's sleeping while accessing the lists
1344 * are serialized by means of a semaphore.
1345 */
1346 switch (cmd) {
Joe Perches475be4d2012-02-19 19:52:38 -08001347 case IIOCNETDWRSET:
1348 printk(KERN_INFO "INFO: ISDN_DW_ABC_EXTENSION not enabled\n");
1349 return (-EINVAL);
1350 case IIOCNETLCR:
1351 printk(KERN_INFO "INFO: ISDN_ABC_LCR_SUPPORT not enabled\n");
1352 return -ENODEV;
Joe Perches475be4d2012-02-19 19:52:38 -08001353 case IIOCNETAIF:
1354 /* Add a network-interface */
1355 if (arg) {
1356 if (copy_from_user(name, argp, sizeof(name)))
1357 return -EFAULT;
1358 s = name;
1359 } else {
1360 s = NULL;
1361 }
1362 ret = mutex_lock_interruptible(&dev->mtx);
1363 if (ret) return ret;
1364 if ((s = isdn_net_new(s, NULL))) {
1365 if (copy_to_user(argp, s, strlen(s) + 1)) {
1366 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 } else {
Joe Perches475be4d2012-02-19 19:52:38 -08001368 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
Joe Perches475be4d2012-02-19 19:52:38 -08001370 } else
1371 ret = -ENODEV;
1372 mutex_unlock(&dev->mtx);
1373 return ret;
1374 case IIOCNETASL:
1375 /* Add a slave to a network-interface */
1376 if (arg) {
1377 if (copy_from_user(bname, argp, sizeof(bname) - 1))
1378 return -EFAULT;
1379 } else
1380 return -EINVAL;
1381 ret = mutex_lock_interruptible(&dev->mtx);
1382 if (ret) return ret;
1383 if ((s = isdn_net_newslave(bname))) {
1384 if (copy_to_user(argp, s, strlen(s) + 1)) {
1385 ret = -EFAULT;
1386 } else {
1387 ret = 0;
1388 }
1389 } else
1390 ret = -ENODEV;
1391 mutex_unlock(&dev->mtx);
1392 return ret;
1393 case IIOCNETDIF:
1394 /* Delete a network-interface */
1395 if (arg) {
1396 if (copy_from_user(name, argp, sizeof(name)))
1397 return -EFAULT;
Matthias Kaehlcke4e3dfac2007-10-16 01:27:50 -07001398 ret = mutex_lock_interruptible(&dev->mtx);
Joe Perches475be4d2012-02-19 19:52:38 -08001399 if (ret) return ret;
1400 ret = isdn_net_rm(name);
Matthias Kaehlcke4e3dfac2007-10-16 01:27:50 -07001401 mutex_unlock(&dev->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return ret;
Joe Perches475be4d2012-02-19 19:52:38 -08001403 } else
1404 return -EINVAL;
1405 case IIOCNETSCF:
1406 /* Set configurable parameters of a network-interface */
1407 if (arg) {
1408 if (copy_from_user(&cfg, argp, sizeof(cfg)))
1409 return -EFAULT;
1410 return isdn_net_setcfg(&cfg);
1411 } else
1412 return -EINVAL;
1413 case IIOCNETGCF:
1414 /* Get configurable parameters of a network-interface */
1415 if (arg) {
1416 if (copy_from_user(&cfg, argp, sizeof(cfg)))
1417 return -EFAULT;
1418 if (!(ret = isdn_net_getcfg(&cfg))) {
1419 if (copy_to_user(argp, &cfg, sizeof(cfg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return -EFAULT;
Joe Perches475be4d2012-02-19 19:52:38 -08001421 }
1422 return ret;
1423 } else
1424 return -EINVAL;
1425 case IIOCNETANM:
1426 /* Add a phone-number to a network-interface */
1427 if (arg) {
1428 if (copy_from_user(&phone, argp, sizeof(phone)))
1429 return -EFAULT;
Matthias Kaehlcke4e3dfac2007-10-16 01:27:50 -07001430 ret = mutex_lock_interruptible(&dev->mtx);
Joe Perches475be4d2012-02-19 19:52:38 -08001431 if (ret) return ret;
1432 ret = isdn_net_addphone(&phone);
Matthias Kaehlcke4e3dfac2007-10-16 01:27:50 -07001433 mutex_unlock(&dev->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return ret;
Joe Perches475be4d2012-02-19 19:52:38 -08001435 } else
1436 return -EINVAL;
1437 case IIOCNETGNM:
1438 /* Get list of phone-numbers of a network-interface */
1439 if (arg) {
1440 if (copy_from_user(&phone, argp, sizeof(phone)))
1441 return -EFAULT;
1442 ret = mutex_lock_interruptible(&dev->mtx);
1443 if (ret) return ret;
1444 ret = isdn_net_getphones(&phone, argp);
1445 mutex_unlock(&dev->mtx);
1446 return ret;
1447 } else
1448 return -EINVAL;
1449 case IIOCNETDNM:
1450 /* Delete a phone-number of a network-interface */
1451 if (arg) {
1452 if (copy_from_user(&phone, argp, sizeof(phone)))
1453 return -EFAULT;
1454 ret = mutex_lock_interruptible(&dev->mtx);
1455 if (ret) return ret;
1456 ret = isdn_net_delphone(&phone);
1457 mutex_unlock(&dev->mtx);
1458 return ret;
1459 } else
1460 return -EINVAL;
1461 case IIOCNETDIL:
1462 /* Force dialing of a network-interface */
1463 if (arg) {
1464 if (copy_from_user(name, argp, sizeof(name)))
1465 return -EFAULT;
1466 return isdn_net_force_dial(name);
1467 } else
1468 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469#ifdef CONFIG_ISDN_PPP
Joe Perches475be4d2012-02-19 19:52:38 -08001470 case IIOCNETALN:
1471 if (!arg)
1472 return -EINVAL;
1473 if (copy_from_user(name, argp, sizeof(name)))
1474 return -EFAULT;
1475 return isdn_ppp_dial_slave(name);
1476 case IIOCNETDLN:
1477 if (!arg)
1478 return -EINVAL;
1479 if (copy_from_user(name, argp, sizeof(name)))
1480 return -EFAULT;
1481 return isdn_ppp_hangup_slave(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482#endif
Joe Perches475be4d2012-02-19 19:52:38 -08001483 case IIOCNETHUP:
1484 /* Force hangup of a network-interface */
1485 if (!arg)
1486 return -EINVAL;
1487 if (copy_from_user(name, argp, sizeof(name)))
1488 return -EFAULT;
1489 return isdn_net_force_hangup(name);
1490 break;
Joe Perches475be4d2012-02-19 19:52:38 -08001491 case IIOCSETVER:
1492 dev->net_verbose = arg;
1493 printk(KERN_INFO "isdn: Verbose-Level is %d\n", dev->net_verbose);
1494 return 0;
1495 case IIOCSETGST:
1496 if (arg)
1497 dev->global_flags |= ISDN_GLOBAL_STOPPED;
1498 else
1499 dev->global_flags &= ~ISDN_GLOBAL_STOPPED;
1500 printk(KERN_INFO "isdn: Global Mode %s\n",
1501 (dev->global_flags & ISDN_GLOBAL_STOPPED) ? "stopped" : "running");
1502 return 0;
1503 case IIOCSETBRJ:
1504 drvidx = -1;
1505 if (arg) {
1506 int i;
1507 char *p;
1508 if (copy_from_user(&iocts, argp,
1509 sizeof(isdn_ioctl_struct)))
1510 return -EFAULT;
1511 iocts.drvid[sizeof(iocts.drvid) - 1] = 0;
1512 if (strlen(iocts.drvid)) {
1513 if ((p = strchr(iocts.drvid, ',')))
1514 *p = 0;
1515 drvidx = -1;
1516 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1517 if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1518 drvidx = i;
1519 break;
1520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
Joe Perches475be4d2012-02-19 19:52:38 -08001522 }
1523 if (drvidx == -1)
1524 return -ENODEV;
1525 if (iocts.arg)
1526 dev->drv[drvidx]->flags |= DRV_FLAG_REJBUS;
1527 else
1528 dev->drv[drvidx]->flags &= ~DRV_FLAG_REJBUS;
1529 return 0;
1530 case IIOCSIGPRF:
1531 dev->profd = current;
1532 return 0;
1533 break;
1534 case IIOCGETPRF:
1535 /* Get all Modem-Profiles */
1536 if (arg) {
1537 char __user *p = argp;
1538 int i;
1539
1540 if (!access_ok(VERIFY_WRITE, argp,
1541 (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
1542 * ISDN_MAX_CHANNELS))
1543 return -EFAULT;
1544
1545 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1546 if (copy_to_user(p, dev->mdm.info[i].emu.profile,
1547 ISDN_MODEM_NUMREG))
1548 return -EFAULT;
1549 p += ISDN_MODEM_NUMREG;
1550 if (copy_to_user(p, dev->mdm.info[i].emu.pmsn, ISDN_MSNLEN))
1551 return -EFAULT;
1552 p += ISDN_MSNLEN;
1553 if (copy_to_user(p, dev->mdm.info[i].emu.plmsn, ISDN_LMSNLEN))
1554 return -EFAULT;
1555 p += ISDN_LMSNLEN;
1556 }
1557 return (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN) * ISDN_MAX_CHANNELS;
1558 } else
1559 return -EINVAL;
1560 break;
1561 case IIOCSETPRF:
1562 /* Set all Modem-Profiles */
1563 if (arg) {
1564 char __user *p = argp;
1565 int i;
1566
1567 if (!access_ok(VERIFY_READ, argp,
1568 (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
1569 * ISDN_MAX_CHANNELS))
1570 return -EFAULT;
1571
1572 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1573 if (copy_from_user(dev->mdm.info[i].emu.profile, p,
1574 ISDN_MODEM_NUMREG))
1575 return -EFAULT;
1576 p += ISDN_MODEM_NUMREG;
1577 if (copy_from_user(dev->mdm.info[i].emu.plmsn, p, ISDN_LMSNLEN))
1578 return -EFAULT;
1579 p += ISDN_LMSNLEN;
1580 if (copy_from_user(dev->mdm.info[i].emu.pmsn, p, ISDN_MSNLEN))
1581 return -EFAULT;
1582 p += ISDN_MSNLEN;
1583 }
1584 return 0;
1585 } else
1586 return -EINVAL;
1587 break;
1588 case IIOCSETMAP:
1589 case IIOCGETMAP:
1590 /* Set/Get MSN->EAZ-Mapping for a driver */
1591 if (arg) {
1592
1593 if (copy_from_user(&iocts, argp,
1594 sizeof(isdn_ioctl_struct)))
1595 return -EFAULT;
1596 iocts.drvid[sizeof(iocts.drvid) - 1] = 0;
1597 if (strlen(iocts.drvid)) {
1598 drvidx = -1;
1599 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1600 if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1601 drvidx = i;
1602 break;
1603 }
1604 } else
1605 drvidx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (drvidx == -1)
1607 return -ENODEV;
Joe Perches475be4d2012-02-19 19:52:38 -08001608 if (cmd == IIOCSETMAP) {
1609 int loop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Joe Perches475be4d2012-02-19 19:52:38 -08001611 p = (char __user *) iocts.arg;
1612 i = 0;
1613 while (loop) {
1614 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Joe Perches475be4d2012-02-19 19:52:38 -08001616 while (1) {
1617 if (!access_ok(VERIFY_READ, p, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return -EFAULT;
Joe Perches475be4d2012-02-19 19:52:38 -08001619 get_user(bname[j], p++);
1620 switch (bname[j]) {
1621 case '\0':
1622 loop = 0;
1623 /* Fall through */
1624 case ',':
1625 bname[j] = '\0';
1626 strcpy(dev->drv[drvidx]->msn2eaz[i], bname);
1627 j = ISDN_MSNLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 break;
Joe Perches475be4d2012-02-19 19:52:38 -08001629 default:
1630 j++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
Joe Perches475be4d2012-02-19 19:52:38 -08001632 if (j >= ISDN_MSNLEN)
1633 break;
1634 }
1635 if (++i > 9)
1636 break;
1637 }
1638 } else {
1639 p = (char __user *) iocts.arg;
1640 for (i = 0; i < 10; i++) {
1641 snprintf(bname, sizeof(bname), "%s%s",
1642 strlen(dev->drv[drvidx]->msn2eaz[i]) ?
1643 dev->drv[drvidx]->msn2eaz[i] : "_",
1644 (i < 9) ? "," : "\0");
1645 if (copy_to_user(p, bname, strlen(bname) + 1))
1646 return -EFAULT;
1647 p += strlen(bname);
1648 }
1649 }
1650 return 0;
1651 } else
1652 return -EINVAL;
1653 case IIOCDBGVAR:
1654 if (arg) {
1655 if (copy_to_user(argp, &dev, sizeof(ulong)))
1656 return -EFAULT;
1657 return 0;
1658 } else
1659 return -EINVAL;
1660 break;
1661 default:
1662 if ((cmd & IIOCDRVCTL) == IIOCDRVCTL)
1663 cmd = ((cmd >> _IOC_NRSHIFT) & _IOC_NRMASK) & ISDN_DRVIOCTL_MASK;
1664 else
1665 return -EINVAL;
1666 if (arg) {
1667 int i;
1668 char *p;
1669 if (copy_from_user(&iocts, argp, sizeof(isdn_ioctl_struct)))
1670 return -EFAULT;
1671 iocts.drvid[sizeof(iocts.drvid) - 1] = 0;
1672 if (strlen(iocts.drvid)) {
1673 if ((p = strchr(iocts.drvid, ',')))
1674 *p = 0;
1675 drvidx = -1;
1676 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1677 if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1678 drvidx = i;
1679 break;
1680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 } else
Joe Perches475be4d2012-02-19 19:52:38 -08001682 drvidx = 0;
1683 if (drvidx == -1)
1684 return -ENODEV;
1685 if (!access_ok(VERIFY_WRITE, argp,
1686 sizeof(isdn_ioctl_struct)))
1687 return -EFAULT;
1688 c.driver = drvidx;
1689 c.command = ISDN_CMD_IOCTL;
1690 c.arg = cmd;
1691 memcpy(c.parm.num, &iocts.arg, sizeof(ulong));
1692 ret = isdn_command(&c);
1693 memcpy(&iocts.arg, c.parm.num, sizeof(ulong));
1694 if (copy_to_user(argp, &iocts, sizeof(isdn_ioctl_struct)))
1695 return -EFAULT;
1696 return ret;
1697 } else
1698 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700 }
1701#ifdef CONFIG_ISDN_PPP
1702 if (minor <= ISDN_MINOR_PPPMAX)
1703 return (isdn_ppp_ioctl(minor - ISDN_MINOR_PPP, file, cmd, arg));
1704#endif
1705 return -ENODEV;
1706
1707#undef name
1708#undef bname
1709#undef iocts
1710#undef phone
1711#undef cfg
1712}
1713
Arnd Bergmann703c6312010-04-27 00:24:02 +02001714static long
1715isdn_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1716{
1717 int ret;
1718
Arnd Bergmann76a64922010-07-11 11:18:53 +00001719 mutex_lock(&isdn_mutex);
Arnd Bergmann703c6312010-04-27 00:24:02 +02001720 ret = isdn_ioctl(file, cmd, arg);
Arnd Bergmann76a64922010-07-11 11:18:53 +00001721 mutex_unlock(&isdn_mutex);
Arnd Bergmann703c6312010-04-27 00:24:02 +02001722
1723 return ret;
1724}
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726/*
1727 * Open the device code.
1728 */
1729static int
1730isdn_open(struct inode *ino, struct file *filep)
1731{
Eric Sesterhenncd6b3952006-07-10 04:45:43 -07001732 uint minor = iminor(ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 int drvidx;
1734 int chidx;
1735 int retval = -ENODEV;
1736
Arnd Bergmann76a64922010-07-11 11:18:53 +00001737 mutex_lock(&isdn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (minor == ISDN_MINOR_STATUS) {
1739 infostruct *p;
1740
1741 if ((p = kmalloc(sizeof(infostruct), GFP_KERNEL))) {
1742 p->next = (char *) dev->infochain;
1743 p->private = (char *) &(filep->private_data);
1744 dev->infochain = p;
1745 /* At opening we allow a single update */
1746 filep->private_data = (char *) 1;
1747 retval = 0;
1748 goto out;
1749 } else {
1750 retval = -ENOMEM;
1751 goto out;
1752 }
1753 }
1754 if (!dev->channels)
1755 goto out;
1756 if (minor <= ISDN_MINOR_BMAX) {
1757 printk(KERN_WARNING "isdn_open minor %d obsolete!\n", minor);
1758 drvidx = isdn_minor2drv(minor);
1759 if (drvidx < 0)
1760 goto out;
1761 chidx = isdn_minor2chan(minor);
1762 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1763 goto out;
1764 if (!(dev->drv[drvidx]->online & (1 << chidx)))
1765 goto out;
1766 isdn_lock_drivers();
1767 retval = 0;
1768 goto out;
1769 }
1770 if (minor <= ISDN_MINOR_CTRLMAX) {
1771 drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1772 if (drvidx < 0)
1773 goto out;
1774 isdn_lock_drivers();
1775 retval = 0;
1776 goto out;
1777 }
1778#ifdef CONFIG_ISDN_PPP
1779 if (minor <= ISDN_MINOR_PPPMAX) {
1780 retval = isdn_ppp_open(minor - ISDN_MINOR_PPP, filep);
1781 if (retval == 0)
1782 isdn_lock_drivers();
1783 goto out;
1784 }
1785#endif
Joe Perches475be4d2012-02-19 19:52:38 -08001786out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 nonseekable_open(ino, filep);
Arnd Bergmann76a64922010-07-11 11:18:53 +00001788 mutex_unlock(&isdn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return retval;
1790}
1791
1792static int
1793isdn_close(struct inode *ino, struct file *filep)
1794{
Eric Sesterhenncd6b3952006-07-10 04:45:43 -07001795 uint minor = iminor(ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Arnd Bergmann76a64922010-07-11 11:18:53 +00001797 mutex_lock(&isdn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (minor == ISDN_MINOR_STATUS) {
1799 infostruct *p = dev->infochain;
1800 infostruct *q = NULL;
1801
1802 while (p) {
1803 if (p->private == (char *) &(filep->private_data)) {
1804 if (q)
1805 q->next = p->next;
1806 else
1807 dev->infochain = (infostruct *) (p->next);
1808 kfree(p);
1809 goto out;
1810 }
1811 q = p;
1812 p = (infostruct *) (p->next);
1813 }
1814 printk(KERN_WARNING "isdn: No private data while closing isdnctrl\n");
1815 goto out;
1816 }
1817 isdn_unlock_drivers();
1818 if (minor <= ISDN_MINOR_BMAX)
1819 goto out;
1820 if (minor <= ISDN_MINOR_CTRLMAX) {
1821 if (dev->profd == current)
1822 dev->profd = NULL;
1823 goto out;
1824 }
1825#ifdef CONFIG_ISDN_PPP
1826 if (minor <= ISDN_MINOR_PPPMAX)
1827 isdn_ppp_release(minor - ISDN_MINOR_PPP, filep);
1828#endif
1829
Joe Perches475be4d2012-02-19 19:52:38 -08001830out:
Arnd Bergmann76a64922010-07-11 11:18:53 +00001831 mutex_unlock(&isdn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return 0;
1833}
1834
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001835static const struct file_operations isdn_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
1837 .owner = THIS_MODULE,
1838 .llseek = no_llseek,
1839 .read = isdn_read,
1840 .write = isdn_write,
1841 .poll = isdn_poll,
Arnd Bergmann703c6312010-04-27 00:24:02 +02001842 .unlocked_ioctl = isdn_unlocked_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 .open = isdn_open,
1844 .release = isdn_close,
1845};
1846
1847char *
1848isdn_map_eaz2msn(char *msn, int di)
1849{
1850 isdn_driver_t *this = dev->drv[di];
1851 int i;
1852
1853 if (strlen(msn) == 1) {
1854 i = msn[0] - '0';
1855 if ((i >= 0) && (i <= 9))
1856 if (strlen(this->msn2eaz[i]))
1857 return (this->msn2eaz[i]);
1858 }
1859 return (msn);
1860}
1861
1862/*
1863 * Find an unused ISDN-channel, whose feature-flags match the
1864 * given L2- and L3-protocols.
1865 */
Joe Perches475be4d2012-02-19 19:52:38 -08001866#define L2V (~(ISDN_FEATURE_L2_V11096 | ISDN_FEATURE_L2_V11019 | ISDN_FEATURE_L2_V11038))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868/*
1869 * This function must be called with holding the dev->lock.
1870 */
1871int
1872isdn_get_free_channel(int usage, int l2_proto, int l3_proto, int pre_dev
Joe Perches475be4d2012-02-19 19:52:38 -08001873 , int pre_chan, char *msn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
1875 int i;
1876 ulong features;
1877 ulong vfeatures;
1878
1879 features = ((1 << l2_proto) | (0x10000 << l3_proto));
1880 vfeatures = (((1 << l2_proto) | (0x10000 << l3_proto)) &
Joe Perches475be4d2012-02-19 19:52:38 -08001881 ~(ISDN_FEATURE_L2_V11096 | ISDN_FEATURE_L2_V11019 | ISDN_FEATURE_L2_V11038));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 /* If Layer-2 protocol is V.110, accept drivers with
1883 * transparent feature even if these don't support V.110
1884 * because we can emulate this in linklevel.
1885 */
1886 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1887 if (USG_NONE(dev->usage[i]) &&
1888 (dev->drvmap[i] != -1)) {
1889 int d = dev->drvmap[i];
1890 if ((dev->usage[i] & ISDN_USAGE_EXCLUSIVE) &&
Joe Perches475be4d2012-02-19 19:52:38 -08001891 ((pre_dev != d) || (pre_chan != dev->chanmap[i])))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 continue;
1893 if (!strcmp(isdn_map_eaz2msn(msn, d), "-"))
1894 continue;
1895 if (dev->usage[i] & ISDN_USAGE_DISABLED)
Joe Perches475be4d2012-02-19 19:52:38 -08001896 continue; /* usage not allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (dev->drv[d]->flags & DRV_FLAG_RUNNING) {
1898 if (((dev->drv[d]->interface->features & features) == features) ||
1899 (((dev->drv[d]->interface->features & vfeatures) == vfeatures) &&
1900 (dev->drv[d]->interface->features & ISDN_FEATURE_L2_TRANS))) {
1901 if ((pre_dev < 0) || (pre_chan < 0)) {
1902 dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1903 dev->usage[i] |= usage;
1904 isdn_info_update();
1905 return i;
1906 } else {
1907 if ((pre_dev == d) && (pre_chan == dev->chanmap[i])) {
1908 dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1909 dev->usage[i] |= usage;
1910 isdn_info_update();
1911 return i;
1912 }
1913 }
1914 }
1915 }
1916 }
1917 return -1;
1918}
1919
1920/*
1921 * Set state of ISDN-channel to 'unused'
1922 */
1923void
1924isdn_free_channel(int di, int ch, int usage)
1925{
1926 int i;
1927
Karsten Keil09fca292006-06-29 13:16:29 +02001928 if ((di < 0) || (ch < 0)) {
1929 printk(KERN_WARNING "%s: called with invalid drv(%d) or channel(%d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001930 __func__, di, ch);
Karsten Keil09fca292006-06-29 13:16:29 +02001931 return;
1932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1934 if (((!usage) || ((dev->usage[i] & ISDN_USAGE_MASK) == usage)) &&
1935 (dev->drvmap[i] == di) &&
1936 (dev->chanmap[i] == ch)) {
1937 dev->usage[i] &= (ISDN_USAGE_NONE | ISDN_USAGE_EXCLUSIVE);
1938 strcpy(dev->num[i], "???");
1939 dev->ibytes[i] = 0;
1940 dev->obytes[i] = 0;
1941// 20.10.99 JIM, try to reinitialize v110 !
1942 dev->v110emu[i] = 0;
1943 atomic_set(&(dev->v110use[i]), 0);
1944 isdn_v110_close(dev->v110[i]);
1945 dev->v110[i] = NULL;
1946// 20.10.99 JIM, try to reinitialize v110 !
1947 isdn_info_update();
Karsten Keil09fca292006-06-29 13:16:29 +02001948 if (dev->drv[di])
1949 skb_queue_purge(&dev->drv[di]->rpqueue[ch]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951}
1952
1953/*
1954 * Cancel Exclusive-Flag for ISDN-channel
1955 */
1956void
1957isdn_unexclusive_channel(int di, int ch)
1958{
1959 int i;
1960
1961 for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1962 if ((dev->drvmap[i] == di) &&
1963 (dev->chanmap[i] == ch)) {
1964 dev->usage[i] &= ~ISDN_USAGE_EXCLUSIVE;
1965 isdn_info_update();
1966 return;
1967 }
1968}
1969
1970/*
1971 * writebuf replacement for SKB_ABLE drivers
1972 */
1973static int
Joe Perches475be4d2012-02-19 19:52:38 -08001974isdn_writebuf_stub(int drvidx, int chan, const u_char __user *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
1976 int ret;
1977 int hl = dev->drv[drvidx]->interface->hl_hdrlen;
1978 struct sk_buff *skb = alloc_skb(hl + len, GFP_ATOMIC);
1979
1980 if (!skb)
Jesper Juhld20d04b2006-06-23 02:05:28 -07001981 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 skb_reserve(skb, hl);
Darren Jenkins4fc89e32008-07-12 13:47:50 -07001983 if (copy_from_user(skb_put(skb, len), buf, len)) {
1984 dev_kfree_skb(skb);
Jesper Juhld20d04b2006-06-23 02:05:28 -07001985 return -EFAULT;
Darren Jenkins4fc89e32008-07-12 13:47:50 -07001986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, 1, skb);
1988 if (ret <= 0)
1989 dev_kfree_skb(skb);
1990 if (ret > 0)
1991 dev->obytes[isdn_dc2minor(drvidx, chan)] += ret;
1992 return ret;
1993}
1994
1995/*
1996 * Return: length of data on success, -ERRcode on failure.
1997 */
1998int
1999isdn_writebuf_skb_stub(int drvidx, int chan, int ack, struct sk_buff *skb)
2000{
2001 int ret;
2002 struct sk_buff *nskb = NULL;
2003 int v110_ret = skb->len;
2004 int idx = isdn_dc2minor(drvidx, chan);
2005
2006 if (dev->v110[idx]) {
2007 atomic_inc(&dev->v110use[idx]);
2008 nskb = isdn_v110_encode(dev->v110[idx], skb);
2009 atomic_dec(&dev->v110use[idx]);
2010 if (!nskb)
2011 return 0;
2012 v110_ret = *((int *)nskb->data);
2013 skb_pull(nskb, sizeof(int));
2014 if (!nskb->len) {
2015 dev_kfree_skb(nskb);
2016 return v110_ret;
2017 }
2018 /* V.110 must always be acknowledged */
2019 ack = 1;
2020 ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, nskb);
2021 } else {
2022 int hl = dev->drv[drvidx]->interface->hl_hdrlen;
2023
Joe Perches475be4d2012-02-19 19:52:38 -08002024 if (skb_headroom(skb) < hl) {
2025 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 * This should only occur when new HL driver with
2027 * increased hl_hdrlen was loaded after netdevice
2028 * was created and connected to the new driver.
2029 *
2030 * The V.110 branch (re-allocates on its own) does
2031 * not need this
2032 */
Joe Perches475be4d2012-02-19 19:52:38 -08002033 struct sk_buff *skb_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 skb_tmp = skb_realloc_headroom(skb, hl);
2036 printk(KERN_DEBUG "isdn_writebuf_skb_stub: reallocating headroom%s\n", skb_tmp ? "" : " failed");
2037 if (!skb_tmp) return -ENOMEM; /* 0 better? */
2038 ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb_tmp);
Joe Perches475be4d2012-02-19 19:52:38 -08002039 if (ret > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 dev_kfree_skb(skb);
2041 } else {
2042 dev_kfree_skb(skb_tmp);
2043 }
2044 } else {
2045 ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb);
2046 }
2047 }
2048 if (ret > 0) {
2049 dev->obytes[idx] += ret;
2050 if (dev->v110[idx]) {
2051 atomic_inc(&dev->v110use[idx]);
2052 dev->v110[idx]->skbuser++;
2053 atomic_dec(&dev->v110use[idx]);
2054 /* For V.110 return unencoded data length */
2055 ret = v110_ret;
2056 /* if the complete frame was send we free the skb;
Joe Perches475be4d2012-02-19 19:52:38 -08002057 if not upper function will requeue the skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 if (ret == skb->len)
2059 dev_kfree_skb(skb);
2060 }
2061 } else
2062 if (dev->v110[idx])
2063 dev_kfree_skb(nskb);
2064 return ret;
2065}
2066
Adrian Bunk3e206b02005-06-25 14:58:35 -07002067static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068isdn_add_channels(isdn_driver_t *d, int drvidx, int n, int adding)
2069{
2070 int j, k, m;
2071
2072 init_waitqueue_head(&d->st_waitq);
2073 if (d->flags & DRV_FLAG_RUNNING)
2074 return -1;
Joe Perches475be4d2012-02-19 19:52:38 -08002075 if (n < 1) return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 m = (adding) ? d->channels + n : n;
2078
2079 if (dev->channels + n > ISDN_MAX_CHANNELS) {
2080 printk(KERN_WARNING "register_isdn: Max. %d channels supported\n",
2081 ISDN_MAX_CHANNELS);
2082 return -1;
2083 }
2084
2085 if ((adding) && (d->rcverr))
2086 kfree(d->rcverr);
Burman Yan41f96932006-12-08 02:39:35 -08002087 if (!(d->rcverr = kzalloc(sizeof(int) * m, GFP_ATOMIC))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 printk(KERN_WARNING "register_isdn: Could not alloc rcverr\n");
2089 return -1;
2090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
2092 if ((adding) && (d->rcvcount))
2093 kfree(d->rcvcount);
Burman Yan41f96932006-12-08 02:39:35 -08002094 if (!(d->rcvcount = kzalloc(sizeof(int) * m, GFP_ATOMIC))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 printk(KERN_WARNING "register_isdn: Could not alloc rcvcount\n");
Jesper Juhlf91012102005-09-10 00:26:54 -07002096 if (!adding)
2097 kfree(d->rcverr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 return -1;
2099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 if ((adding) && (d->rpqueue)) {
2102 for (j = 0; j < d->channels; j++)
2103 skb_queue_purge(&d->rpqueue[j]);
2104 kfree(d->rpqueue);
2105 }
2106 if (!(d->rpqueue = kmalloc(sizeof(struct sk_buff_head) * m, GFP_ATOMIC))) {
2107 printk(KERN_WARNING "register_isdn: Could not alloc rpqueue\n");
2108 if (!adding) {
2109 kfree(d->rcvcount);
2110 kfree(d->rcverr);
2111 }
Joe Perches475be4d2012-02-19 19:52:38 -08002112 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114 for (j = 0; j < m; j++) {
2115 skb_queue_head_init(&d->rpqueue[j]);
2116 }
2117
2118 if ((adding) && (d->rcv_waitq))
2119 kfree(d->rcv_waitq);
2120 d->rcv_waitq = kmalloc(sizeof(wait_queue_head_t) * 2 * m, GFP_ATOMIC);
2121 if (!d->rcv_waitq) {
2122 printk(KERN_WARNING "register_isdn: Could not alloc rcv_waitq\n");
2123 if (!adding) {
2124 kfree(d->rpqueue);
2125 kfree(d->rcvcount);
2126 kfree(d->rcverr);
2127 }
2128 return -1;
2129 }
2130 d->snd_waitq = d->rcv_waitq + m;
2131 for (j = 0; j < m; j++) {
2132 init_waitqueue_head(&d->rcv_waitq[j]);
2133 init_waitqueue_head(&d->snd_waitq[j]);
2134 }
2135
2136 dev->channels += n;
2137 for (j = d->channels; j < m; j++)
2138 for (k = 0; k < ISDN_MAX_CHANNELS; k++)
2139 if (dev->chanmap[k] < 0) {
2140 dev->chanmap[k] = j;
2141 dev->drvmap[k] = drvidx;
2142 break;
2143 }
2144 d->channels = m;
2145 return 0;
2146}
2147
2148/*
2149 * Low-level-driver registration
2150 */
2151
2152static void
2153set_global_features(void)
2154{
2155 int drvidx;
2156
2157 dev->global_features = 0;
2158 for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++) {
2159 if (!dev->drv[drvidx])
2160 continue;
2161 if (dev->drv[drvidx]->interface)
2162 dev->global_features |= dev->drv[drvidx]->interface->features;
2163 }
2164}
2165
2166#ifdef CONFIG_ISDN_DIVERSION
2167
2168static char *map_drvname(int di)
2169{
Joe Perches475be4d2012-02-19 19:52:38 -08002170 if ((di < 0) || (di >= ISDN_MAX_DRIVERS))
2171 return (NULL);
2172 return (dev->drvid[di]); /* driver name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173} /* map_drvname */
2174
2175static int map_namedrv(char *id)
2176{ int i;
2177
Joe Perches475be4d2012-02-19 19:52:38 -08002178 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
2179 { if (!strcmp(dev->drvid[i], id))
2180 return (i);
2181 }
2182 return (-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183} /* map_namedrv */
2184
2185int DIVERT_REG_NAME(isdn_divert_if *i_div)
2186{
Joe Perches475be4d2012-02-19 19:52:38 -08002187 if (i_div->if_magic != DIVERT_IF_MAGIC)
2188 return (DIVERT_VER_ERR);
2189 switch (i_div->cmd)
2190 {
2191 case DIVERT_CMD_REL:
2192 if (divert_if != i_div)
2193 return (DIVERT_REL_ERR);
2194 divert_if = NULL; /* free interface */
2195 return (DIVERT_NO_ERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Joe Perches475be4d2012-02-19 19:52:38 -08002197 case DIVERT_CMD_REG:
2198 if (divert_if)
2199 return (DIVERT_REG_ERR);
2200 i_div->ll_cmd = isdn_command; /* set command function */
2201 i_div->drv_to_name = map_drvname;
2202 i_div->name_to_drv = map_namedrv;
2203 divert_if = i_div; /* remember interface */
2204 return (DIVERT_NO_ERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Joe Perches475be4d2012-02-19 19:52:38 -08002206 default:
2207 return (DIVERT_CMD_ERR);
2208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209} /* DIVERT_REG_NAME */
2210
2211EXPORT_SYMBOL(DIVERT_REG_NAME);
2212
2213#endif /* CONFIG_ISDN_DIVERSION */
2214
2215
2216EXPORT_SYMBOL(register_isdn);
2217#ifdef CONFIG_ISDN_PPP
2218EXPORT_SYMBOL(isdn_ppp_register_compressor);
2219EXPORT_SYMBOL(isdn_ppp_unregister_compressor);
2220#endif
2221
2222int
Joe Perches475be4d2012-02-19 19:52:38 -08002223register_isdn(isdn_if *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
2225 isdn_driver_t *d;
2226 int j;
2227 ulong flags;
2228 int drvidx;
2229
2230 if (dev->drivers >= ISDN_MAX_DRIVERS) {
2231 printk(KERN_WARNING "register_isdn: Max. %d drivers supported\n",
2232 ISDN_MAX_DRIVERS);
2233 return 0;
2234 }
2235 if (!i->writebuf_skb) {
2236 printk(KERN_WARNING "register_isdn: No write routine given.\n");
2237 return 0;
2238 }
Burman Yan41f96932006-12-08 02:39:35 -08002239 if (!(d = kzalloc(sizeof(isdn_driver_t), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 printk(KERN_WARNING "register_isdn: Could not alloc driver-struct\n");
2241 return 0;
2242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
2244 d->maxbufsize = i->maxbufsize;
2245 d->pktcount = 0;
2246 d->stavail = 0;
2247 d->flags = DRV_FLAG_LOADED;
2248 d->online = 0;
2249 d->interface = i;
2250 d->channels = 0;
2251 spin_lock_irqsave(&dev->lock, flags);
2252 for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++)
2253 if (!dev->drv[drvidx])
2254 break;
2255 if (isdn_add_channels(d, drvidx, i->channels, 0)) {
2256 spin_unlock_irqrestore(&dev->lock, flags);
2257 kfree(d);
2258 return 0;
2259 }
2260 i->channels = drvidx;
2261 i->rcvcallb_skb = isdn_receive_skb_callback;
2262 i->statcallb = isdn_status_callback;
2263 if (!strlen(i->id))
2264 sprintf(i->id, "line%d", drvidx);
2265 for (j = 0; j < drvidx; j++)
2266 if (!strcmp(i->id, dev->drvid[j]))
2267 sprintf(i->id, "line%d", drvidx);
2268 dev->drv[drvidx] = d;
2269 strcpy(dev->drvid[drvidx], i->id);
2270 isdn_info_update();
2271 dev->drivers++;
2272 set_global_features();
2273 spin_unlock_irqrestore(&dev->lock, flags);
2274 return 1;
2275}
2276
2277/*
Joe Perches475be4d2012-02-19 19:52:38 -08002278*****************************************************************************
2279* And now the modules code.
2280*****************************************************************************
2281*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283static char *
2284isdn_getrev(const char *revision)
2285{
2286 char *rev;
2287 char *p;
2288
2289 if ((p = strchr(revision, ':'))) {
2290 rev = p + 2;
2291 p = strchr(rev, '$');
2292 *--p = 0;
2293 } else
2294 rev = "???";
2295 return rev;
2296}
2297
2298/*
2299 * Allocate and initialize all data, register modem-devices
2300 */
2301static int __init isdn_init(void)
2302{
2303 int i;
2304 char tmprev[50];
2305
Joe Perches1ac45942011-05-28 10:36:26 -07002306 dev = vzalloc(sizeof(isdn_dev));
2307 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 printk(KERN_WARNING "isdn: Could not allocate device-struct.\n");
2309 return -EIO;
2310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 init_timer(&dev->timer);
2312 dev->timer.function = isdn_timer_funct;
2313 spin_lock_init(&dev->lock);
2314 spin_lock_init(&dev->timerlock);
2315#ifdef MODULE
2316 dev->owner = THIS_MODULE;
2317#endif
Matthias Kaehlcke4e3dfac2007-10-16 01:27:50 -07002318 mutex_init(&dev->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 init_waitqueue_head(&dev->info_waitq);
2320 for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2321 dev->drvmap[i] = -1;
2322 dev->chanmap[i] = -1;
2323 dev->m_idx[i] = -1;
2324 strcpy(dev->num[i], "???");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 }
2326 if (register_chrdev(ISDN_MAJOR, "isdn", &isdn_fops)) {
2327 printk(KERN_WARNING "isdn: Could not register control devices\n");
2328 vfree(dev);
2329 return -EIO;
2330 }
2331 if ((isdn_tty_modem_init()) < 0) {
2332 printk(KERN_WARNING "isdn: Could not register tty devices\n");
2333 vfree(dev);
2334 unregister_chrdev(ISDN_MAJOR, "isdn");
2335 return -EIO;
2336 }
2337#ifdef CONFIG_ISDN_PPP
2338 if (isdn_ppp_init() < 0) {
2339 printk(KERN_WARNING "isdn: Could not create PPP-device-structs\n");
2340 isdn_tty_exit();
2341 unregister_chrdev(ISDN_MAJOR, "isdn");
2342 vfree(dev);
2343 return -EIO;
2344 }
2345#endif /* CONFIG_ISDN_PPP */
2346
2347 strcpy(tmprev, isdn_revision);
2348 printk(KERN_NOTICE "ISDN subsystem Rev: %s/", isdn_getrev(tmprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 strcpy(tmprev, isdn_net_revision);
2350 printk("%s/", isdn_getrev(tmprev));
2351 strcpy(tmprev, isdn_ppp_revision);
2352 printk("%s/", isdn_getrev(tmprev));
2353 strcpy(tmprev, isdn_audio_revision);
2354 printk("%s/", isdn_getrev(tmprev));
2355 strcpy(tmprev, isdn_v110_revision);
2356 printk("%s", isdn_getrev(tmprev));
2357
2358#ifdef MODULE
2359 printk(" loaded\n");
2360#else
2361 printk("\n");
2362#endif
2363 isdn_info_update();
2364 return 0;
2365}
2366
2367/*
2368 * Unload module
2369 */
2370static void __exit isdn_exit(void)
2371{
2372#ifdef CONFIG_ISDN_PPP
2373 isdn_ppp_cleanup();
2374#endif
2375 if (isdn_net_rmall() < 0) {
2376 printk(KERN_WARNING "isdn: net-device busy, remove cancelled\n");
2377 return;
2378 }
2379 isdn_tty_exit();
2380 unregister_chrdev(ISDN_MAJOR, "isdn");
2381 del_timer(&dev->timer);
2382 /* call vfree with interrupts enabled, else it will hang */
2383 vfree(dev);
2384 printk(KERN_NOTICE "ISDN-subsystem unloaded\n");
2385}
2386
2387module_init(isdn_init);
2388module_exit(isdn_exit);