OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [isdn/] [isdn_common.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1626 jcastillo
/* $Id: isdn_common.c,v 1.1 2005-12-20 10:16:56 jcastillo Exp $
2
 
3
 * Linux ISDN subsystem, common used functions (linklevel).
4
 *
5
 * Copyright 1994-1998  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 program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2, or (at your option)
12
 * any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
 *
23
 * $Log: not supported by cvs2svn $
24
 * Revision 1.1.1.1  2001/09/10 07:44:18  simons
25
 * Initial import
26
 *
27
 * Revision 1.1.1.1  2001/07/02 17:58:31  simons
28
 * Initial revision
29
 *
30
 * Revision 1.44.2.9  1998/11/06 00:07:25  fritz
31
 * Bugfix: loading additional driver while /dev/isdnctrl opened resulted in
32
 *         wrong module usage count after closing /dev/isdnctrl.
33
 *
34
 * Revision 1.44.2.8  1998/11/05 22:11:43  fritz
35
 * Changed mail-address.
36
 *
37
 * Revision 1.44.2.7  1998/11/04 17:22:40  fritz
38
 * Replaced broken lowlevel-driver locking.
39
 *
40
 * Revision 1.44.2.6  1998/11/03 14:30:56  fritz
41
 * Reduced stack usage in various functions.
42
 * Adapted statemachine to work with certified HiSax.
43
 * Some fixes in callback handling.
44
 *
45
 * Revision 1.44.2.5  1998/10/25 15:48:04  fritz
46
 * Misc bugfixes and adaptions to new HiSax
47
 *
48
 * Revision 1.44.2.4  1998/06/07 13:47:44  fritz
49
 * ABC cleanup
50
 *
51
 * Revision 1.44.2.2  1998/03/16 09:55:44  cal
52
 * Merged in TimRu-patches. Still needs validation in conjunction with ABC-patches.
53
 *
54
 * Revision 1.44.2.1  1998/03/07 23:35:03  detabc
55
 * added the abc-extension to the linux isdn-kernel
56
 * for kernel-version 2.0.xx
57
 * DO NOT USE FOR HIGHER KERNELS-VERSIONS
58
 * all source-lines are switched with the define  CONFIG_ISDN_WITH_ABC
59
 * (make config and answer ABC-Ext. Support (Compress,TCP-Keepalive ...) with yes
60
 *
61
 * you need also a modified isdnctrl-source the switch on the
62
 * features of the abc-extension
63
 *
64
 * please use carefully. more detail will be follow.
65
 * thanks
66
 *
67
 * Revision 1.44  1997/05/27 15:17:23  fritz
68
 * Added changes for recent 2.1.x kernels:
69
 *   changed return type of isdn_close
70
 *   queue_task_* -> queue_task
71
 *   clear/set_bit -> test_and_... where apropriate.
72
 *   changed type of hard_header_cache parameter.
73
 *
74
 * Revision 1.43  1997/03/31 14:09:43  fritz
75
 * Fixed memory leak in isdn_close().
76
 *
77
 * Revision 1.42  1997/03/30 16:51:08  calle
78
 * changed calls to copy_from_user/copy_to_user and removed verify_area
79
 * were possible.
80
 *
81
 * Revision 1.41  1997/03/24 22:54:41  fritz
82
 * Some small fixes in debug code.
83
 *
84
 * Revision 1.40  1997/03/08 08:13:51  fritz
85
 * Bugfix: IIOCSETMAP (Set mapping) was broken.
86
 *
87
 * Revision 1.39  1997/03/07 01:32:54  fritz
88
 * Added proper ifdef's for CONFIG_ISDN_AUDIO
89
 *
90
 * Revision 1.38  1997/03/05 21:15:02  fritz
91
 * Fix: reduced stack usage of isdn_ioctl() and isdn_set_allcfg()
92
 *
93
 * Revision 1.37  1997/03/02 14:29:18  fritz
94
 * More ttyI related cleanup.
95
 *
96
 * Revision 1.36  1997/02/28 02:32:40  fritz
97
 * Cleanup: Moved some tty related stuff from isdn_common.c
98
 *          to isdn_tty.c
99
 * Bugfix:  Bisync protocol did not behave like documented.
100
 *
101
 * Revision 1.35  1997/02/21 13:01:19  fritz
102
 * Changes CAUSE message output in kernel log.
103
 *
104
 * Revision 1.34  1997/02/10 20:12:43  fritz
105
 * Changed interface for reporting incoming calls.
106
 *
107
 * Revision 1.33  1997/02/10 10:05:42  fritz
108
 * More changes for Kernel 2.1.X
109
 * Symbol information moved to isdn_syms.c
110
 *
111
 * Revision 1.32  1997/02/03 22:55:26  fritz
112
 * Reformatted according CodingStyle.
113
 * Changed isdn_writebuf_stub static.
114
 * Slow down tty-RING counter.
115
 * skb->free stuff replaced by macro.
116
 * Bugfix in audio-skb locking.
117
 * Bugfix in HL-driver locking.
118
 *
119
 * Revision 1.31  1997/01/17 01:19:18  fritz
120
 * Applied chargeint patch.
121
 *
122
 * Revision 1.30  1997/01/14 01:27:47  fritz
123
 * Changed audio receive not to rely on skb->users and skb->lock.
124
 * Added ATI2 and related variables.
125
 * Started adding full-duplex audio capability.
126
 *
127
 * Revision 1.29  1997/01/12 23:33:03  fritz
128
 * Made isdn_all_eaz foolproof.
129
 *
130
 * Revision 1.28  1996/11/13 02:33:19  fritz
131
 * Fixed a race condition.
132
 *
133
 * Revision 1.27  1996/10/27 22:02:41  keil
134
 * return codes for ISDN_STAT_ICALL
135
 *
136
 * Revision 1.26  1996/10/23 11:59:40  fritz
137
 * More compatibility changes.
138
 *
139
 * Revision 1.25  1996/10/22 23:13:54  fritz
140
 * Changes for compatibility to 2.0.X and 2.1.X kernels.
141
 *
142
 * Revision 1.24  1996/10/11 14:02:03  fritz
143
 * Bugfix: call to isdn_ppp_timer_timeout() never compiled, because of
144
 *         typo in #ifdef.
145
 *
146
 * Revision 1.23  1996/06/25 18:35:38  fritz
147
 * Fixed bogus memory access in isdn_set_allcfg().
148
 *
149
 * Revision 1.22  1996/06/24 17:37:37  fritz
150
 * Bugfix: isdn_timer_ctrl() did restart timer, even if it
151
 *         was already running.
152
 *         lowlevel driver locking did use wrong parameters.
153
 *
154
 * Revision 1.21  1996/06/15 14:58:20  fritz
155
 * Added version signatures for data structures used
156
 * by userlevel programs.
157
 *
158
 * Revision 1.20  1996/06/12 16:01:49  fritz
159
 * Bugfix: Remote B-channel hangup sometimes did not result
160
 *         in a NO CARRIER on tty.
161
 *
162
 * Revision 1.19  1996/06/11 14:52:04  hipp
163
 * minor bugfix in isdn_writebuf_skb_stub()
164
 *
165
 * Revision 1.18  1996/06/06 14:51:51  fritz
166
 * Changed to support DTMF decoding on audio playback also.
167
 *
168
 * Revision 1.17  1996/06/05 02:24:10  fritz
169
 * Added DTMF decoder for audio mode.
170
 *
171
 * Revision 1.16  1996/06/03 20:09:05  fritz
172
 * Bugfix: called wrong function pointer for locking in
173
 *         isdn_get_free_channel().
174
 *
175
 * Revision 1.15  1996/05/31 01:10:54  fritz
176
 * Bugfixes:
177
 *   Lowlevel modules did not get locked correctly.
178
 *   Did show wrong revision when initializing.
179
 *   Minor fixes in ioctl code.
180
 *   sk_buff did not get freed, if error in writebuf_stub.
181
 *
182
 * Revision 1.14  1996/05/18 01:36:55  fritz
183
 * Added spelling corrections and some minor changes
184
 * to stay in sync with kernel.
185
 *
186
 * Revision 1.13  1996/05/17 15:43:30  fritz
187
 * Bugfix: decrement of rcvcount in readbchan() corrected.
188
 *
189
 * Revision 1.12  1996/05/17 03:55:43  fritz
190
 * Changed DLE handling for audio receive.
191
 * Some cleanup.
192
 * Added display of isdn_audio_revision.
193
 *
194
 * Revision 1.11  1996/05/11 21:51:32  fritz
195
 * Changed queue management to use sk_buffs.
196
 *
197
 * Revision 1.10  1996/05/10 08:49:16  fritz
198
 * Checkin before major changes of tty-code.
199
 *
200
 * Revision 1.9  1996/05/07 09:19:41  fritz
201
 * Adapted to changes in isdn_tty.c
202
 *
203
 * Revision 1.8  1996/05/06 11:34:51  hipp
204
 * fixed a few bugs
205
 *
206
 * Revision 1.7  1996/05/02 03:55:17  fritz
207
 * Bugfixes:
208
 *  - B-channel connect message for modem devices
209
 *    sometimes did not result in a CONNECT-message.
210
 *  - register_isdn did not check for driverId-conflicts.
211
 *
212
 * Revision 1.6  1996/04/30 20:57:21  fritz
213
 * Commit test
214
 *
215
 * Revision 1.5  1996/04/20 16:19:07  fritz
216
 * Changed slow timer handlers to increase accuracy.
217
 * Added statistic information for usage by xisdnload.
218
 * Fixed behaviour of isdnctrl-device on non-blocked io.
219
 * Fixed all io to go through generic writebuf-function without
220
 * bypassing. Same for incoming data.
221
 * Fixed bug: Last channel had been unusable.
222
 * Fixed kfree of tty xmit_buf on ppp initialization failure.
223
 *
224
 * Revision 1.4  1996/02/11 02:33:26  fritz
225
 * Fixed bug in main timer-dispatcher.
226
 * Bugfix: Lot of tty-callbacks got called regardless of the events already
227
 * been handled by network-devices.
228
 * Changed ioctl-names.
229
 *
230
 * Revision 1.3  1996/01/22 05:16:11  fritz
231
 * Changed ioctl-names.
232
 * Fixed bugs in isdn_open and isdn_close regarding PPP_MINOR.
233
 *
234
 * Revision 1.2  1996/01/21 16:52:40  fritz
235
 * Support for sk_buffs added, changed header-stuffing.
236
 *
237
 * Revision 1.1  1996/01/09 04:12:52  fritz
238
 * Initial revision
239
 *
240
 */
241
 
242
#include <linux/config.h>
243
#define __NO_VERSION__
244
#include <linux/module.h>
245
#include <linux/version.h>
246
#if (LINUX_VERSION_CODE >= 0x020117)
247
#include <asm/poll.h>
248
#endif
249
#include <linux/isdn.h>
250
#include "isdn_common.h"
251
#include "isdn_tty.h"
252
#include "isdn_net.h"
253
#include "isdn_ppp.h"
254
#ifdef CONFIG_ISDN_AUDIO
255
#include "isdn_audio.h"
256
#endif
257
#include "isdn_cards.h"
258
 
259
/* Debugflags */
260
#undef ISDN_DEBUG_STATCALLB
261
 
262
isdn_dev *dev = (isdn_dev *) 0;
263
 
264
static char *isdn_revision = "$Revision: 1.1 $";
265
 
266
extern char *isdn_net_revision;
267
extern char *isdn_tty_revision;
268
#ifdef CONFIG_ISDN_PPP
269
extern char *isdn_ppp_revision;
270
#else
271
static char *isdn_ppp_revision = ": none $";
272
#endif
273
#ifdef CONFIG_ISDN_AUDIO
274
extern char *isdn_audio_revision;
275
#else
276
static char *isdn_audio_revision = ": none $";
277
#endif
278
 
279
static int isdn_writebuf_stub(int, int, const u_char *, int, int);
280
 
281
void
282
isdn_MOD_INC_USE_COUNT(void)
283
{
284
        int i;
285
 
286
        MOD_INC_USE_COUNT;
287
        for (i = 0; i < dev->drivers; i++) {
288
                isdn_ctrl cmd;
289
 
290
                cmd.driver = i;
291
                cmd.arg = 0;
292
                cmd.command = ISDN_CMD_LOCK;
293
                isdn_command(&cmd);
294
                dev->drv[i]->locks++;
295
        }
296
}
297
 
298
void
299
isdn_MOD_DEC_USE_COUNT(void)
300
{
301
        int i;
302
 
303
        MOD_DEC_USE_COUNT;
304
        for (i = 0; i < dev->drivers; i++)
305
                if (dev->drv[i]->locks > 0) {
306
                        isdn_ctrl cmd;
307
 
308
                        cmd.driver = i;
309
                        cmd.arg = 0;
310
                        cmd.command = ISDN_CMD_UNLOCK;
311
                        isdn_command(&cmd);
312
                        dev->drv[i]->locks--;
313
                }
314
}
315
 
316
#if defined(ISDN_DEBUG_NET_DUMP) || defined(ISDN_DEBUG_MODEM_DUMP) || defined(CONFIG_ISDN_TIMEOUT_RULES)
317
void
318
isdn_dumppkt(char *s, u_char * p, int len, int dumplen)
319
{
320
        int dumpc;
321
 
322
        printk(KERN_DEBUG "%s(%d) ", s, len);
323
        for (dumpc = 0; (dumpc < dumplen) && (len); len--, dumpc++)
324
                printk(" %02x", *p++);
325
        printk("\n");
326
}
327
#endif
328
 
329
static __inline void
330
isdn_trash_skb(struct sk_buff *skb, int rw)
331
{
332
        SET_SKB_FREE(skb);
333
        kfree_skb(skb, rw);
334
}
335
 
336
static void
337
isdn_free_queue(struct sk_buff_head *queue)
338
{
339
        struct sk_buff *skb;
340
        unsigned long flags;
341
 
342
        save_flags(flags);
343
        cli();
344
        if (skb_queue_len(queue))
345
                while ((skb = skb_dequeue(queue)))
346
                        isdn_trash_skb(skb, FREE_READ);
347
        restore_flags(flags);
348
}
349
 
350
int
351
isdn_dc2minor(int di, int ch)
352
{
353
        int i;
354
        for (i = 0; i < ISDN_MAX_CHANNELS; i++)
355
                if (dev->chanmap[i] == ch && dev->drvmap[i] == di)
356
                        return i;
357
        return -1;
358
}
359
 
360
static int isdn_timer_cnt1 = 0;
361
static int isdn_timer_cnt2 = 0;
362
static int isdn_timer_cnt3 = 0;
363
 
364
static void
365
isdn_timer_funct(ulong dummy)
366
{
367
        int tf = dev->tflags;
368
 
369
        if (tf & ISDN_TIMER_FAST) {
370
                if (tf & ISDN_TIMER_MODEMREAD)
371
                        isdn_tty_readmodem();
372
                if (tf & ISDN_TIMER_MODEMPLUS)
373
                        isdn_tty_modem_escape();
374
                if (tf & ISDN_TIMER_MODEMXMIT)
375
                        isdn_tty_modem_xmit();
376
        }
377
        if (tf & ISDN_TIMER_SLOW) {
378
                if (++isdn_timer_cnt1 >= ISDN_TIMER_02SEC) {
379
                        isdn_timer_cnt1 = 0;
380
                        if (tf & ISDN_TIMER_NETDIAL)
381
                                isdn_net_dial();
382
                }
383
                if (++isdn_timer_cnt2 >= ISDN_TIMER_1SEC) {
384
                        isdn_timer_cnt2 = 0;
385
                        if (tf & ISDN_TIMER_NETHANGUP)
386
                                isdn_net_autohup();
387
                        if (++isdn_timer_cnt3 > ISDN_TIMER_RINGING) {
388
                                isdn_timer_cnt3 = 0;
389
                                if (tf & ISDN_TIMER_MODEMRING)
390
                                        isdn_tty_modem_ring();
391
                        }
392
#if (defined CONFIG_ISDN_PPP) && (defined CONFIG_ISDN_MPP)
393
                        if (tf & ISDN_TIMER_IPPP)
394
                                isdn_ppp_timer_timeout();
395
#endif
396
                }
397
        }
398
 
399
        if (tf) {
400
                int flags;
401
 
402
                save_flags(flags);
403
                cli();
404
                del_timer(&dev->timer);
405
                dev->timer.expires = jiffies + ISDN_TIMER_RES;
406
                add_timer(&dev->timer);
407
                restore_flags(flags);
408
        }
409
}
410
 
411
void
412
isdn_timer_ctrl(int tf, int onoff)
413
{
414
        int flags;
415
 
416
        save_flags(flags);
417
        cli();
418
        if ((tf & ISDN_TIMER_SLOW) && (!(dev->tflags & ISDN_TIMER_SLOW))) {
419
                /* If the slow-timer wasn't activated until now */
420
                isdn_timer_cnt1 = 0;
421
                isdn_timer_cnt2 = 0;
422
        }
423
        if (onoff)
424
                dev->tflags |= tf;
425
        else
426
                dev->tflags &= ~tf;
427
        if (dev->tflags) {
428
                if (!del_timer(&dev->timer))    /* del_timer is 1, when active */
429
                        dev->timer.expires = jiffies + ISDN_TIMER_RES;
430
                add_timer(&dev->timer);
431
        }
432
        restore_flags(flags);
433
}
434
 
435
/*
436
 * Receive a packet from B-Channel. (Called from low-level-module)
437
 */
438
static void
439
isdn_receive_skb_callback(int di, int channel, struct sk_buff *skb)
440
{
441
        int i;
442
 
443
        if ((i = isdn_dc2minor(di, channel)) == -1) {
444
                isdn_trash_skb(skb, FREE_READ);
445
                return;
446
        }
447
        /* Update statistics */
448
        dev->ibytes[i] += skb->len;
449
        /* First, try to deliver data to network-device */
450
        if (isdn_net_rcv_skb(i, skb))
451
                return;
452
        /* No network-device found, deliver to tty or raw-channel */
453
        SET_SKB_FREE(skb);
454
        if (skb->len) {
455
                if (isdn_tty_rcv_skb(i, di, channel, skb))
456
                        return;
457
                wake_up_interruptible(&dev->drv[di]->rcv_waitq[channel]);
458
        } else
459
                isdn_trash_skb(skb, FREE_READ);
460
}
461
 
462
/*
463
 * Intercept command from Linklevel to Lowlevel.
464
 */
465
int
466
isdn_command(isdn_ctrl *cmd)
467
{
468
        return dev->drv[cmd->driver]->interface->command(cmd);
469
}
470
 
471
void
472
isdn_all_eaz(int di, int ch)
473
{
474
        isdn_ctrl cmd;
475
 
476
        if (di < 0)
477
                return;
478
        cmd.driver = di;
479
        cmd.arg = ch;
480
        cmd.command = ISDN_CMD_SETEAZ;
481
        cmd.parm.num[0] = '\0';
482
        isdn_command(&cmd);
483
}
484
 
485
static int
486
isdn_status_callback(isdn_ctrl * c)
487
{
488
        int di;
489
        ulong flags;
490
        int i;
491
        int r;
492
        int retval = 0;
493
        isdn_ctrl cmd;
494
 
495
        di = c->driver;
496
        i = isdn_dc2minor(di, c->arg);
497
        switch (c->command) {
498
                case ISDN_STAT_BSENT:
499
                        if (i < 0)
500
                                return -1;
501
                        if (dev->global_flags & ISDN_GLOBAL_STOPPED)
502
                                return 0;
503
                        if (isdn_net_stat_callback(i, c))
504
                                return 0;
505
                        if (isdn_tty_stat_callback(i, c))
506
                                return 0;
507
                        wake_up_interruptible(&dev->drv[di]->snd_waitq[c->arg]);
508
                        break;
509
                case ISDN_STAT_STAVAIL:
510
                        save_flags(flags);
511
                        cli();
512
                        dev->drv[di]->stavail += c->arg;
513
                        restore_flags(flags);
514
                        wake_up_interruptible(&dev->drv[di]->st_waitq);
515
                        break;
516
                case ISDN_STAT_RUN:
517
                        dev->drv[di]->flags |= DRV_FLAG_RUNNING;
518
                        for (i = 0; i < ISDN_MAX_CHANNELS; i++)
519
                                if (dev->drvmap[i] == di)
520
                                        isdn_all_eaz(di, dev->chanmap[i]);
521
                        break;
522
                case ISDN_STAT_STOP:
523
                        dev->drv[di]->flags &= ~DRV_FLAG_RUNNING;
524
                        break;
525
                case ISDN_STAT_ICALL:
526
                        if (i < 0)
527
                                return -1;
528
#ifdef ISDN_DEBUG_STATCALLB
529
                        printk(KERN_DEBUG "ICALL (net): %d %ld %s\n", di, c->arg, c->parm.num);
530
#endif
531
                        if (dev->global_flags & ISDN_GLOBAL_STOPPED) {
532
                                cmd.driver = di;
533
                                cmd.arg = c->arg;
534
                                cmd.command = ISDN_CMD_HANGUP;
535
                                isdn_command(&cmd);
536
                                return 0;
537
                        }
538
                        /* Try to find a network-interface which will accept incoming call */
539
                        r = isdn_net_find_icall(di, c, i);
540
                        switch (r) {
541
                                case 0:
542
                                        /* No network-device replies.
543
                                         * Try ttyI's
544
                                         */
545
                                        if (isdn_tty_find_icall(di, c) >= 0)
546
                                                retval = 1;
547
                                        else if (dev->drv[di]->flags & DRV_FLAG_REJBUS) {
548
                                                cmd.driver = di;
549
                                                cmd.arg = c->arg;
550
                                                cmd.command = ISDN_CMD_HANGUP;
551
                                                isdn_command(&cmd);
552
                                                retval = 2;
553
                                        }
554
                                        break;
555
                                case 1:
556
                                        /* Schedule connection-setup */
557
                                        isdn_net_dial();
558
                                        cmd.driver = di;
559
                                        cmd.arg = c->arg;
560
                                        cmd.command = ISDN_CMD_ACCEPTD;
561
                                        isdn_command(&cmd);
562
                                        retval = 1;
563
                                        break;
564
                                case 2: /* For calling back, first reject incoming call ... */
565
                                case 3: /* Interface found, but down, reject call actively  */
566
                                        retval = 2;
567
                                        printk(KERN_INFO "isdn: Rejecting Call\n");
568
                                        cmd.driver = di;
569
                                        cmd.arg = c->arg;
570
                                        cmd.command = ISDN_CMD_HANGUP;
571
                                        isdn_command(&cmd);
572
                                        if (r == 3)
573
                                                break;
574
                                        /* Fall through */
575
                                case 4:
576
                                        /* ... then start callback. */
577
                                        isdn_net_dial();
578
                                        break;
579
                        }
580
                        return retval;
581
                        break;
582
                case ISDN_STAT_CINF:
583
                        if (i < 0)
584
                                return -1;
585
#ifdef ISDN_DEBUG_STATCALLB
586
                        printk(KERN_DEBUG "CINF: %ld %s\n", c->arg, c->parm.num);
587
#endif
588
                        if (dev->global_flags & ISDN_GLOBAL_STOPPED)
589
                                return 0;
590
                        if (strcmp(c->parm.num, "0"))
591
                                isdn_net_stat_callback(i, c);
592
                        break;
593
                case ISDN_STAT_CAUSE:
594
#ifdef ISDN_DEBUG_STATCALLB
595
                        printk(KERN_DEBUG "CAUSE: %ld %s\n", c->arg, c->parm.num);
596
#endif
597
                        printk(KERN_INFO "isdn: %s,ch%ld cause: %s\n",
598
                               dev->drvid[di], c->arg, c->parm.num);
599
                        isdn_tty_stat_callback(i, c);
600
                        break;
601
                case ISDN_STAT_DCONN:
602
                        if (i < 0)
603
                                return -1;
604
#ifdef ISDN_DEBUG_STATCALLB
605
                        printk(KERN_DEBUG "DCONN: %ld\n", c->arg);
606
#endif
607
                        if (dev->global_flags & ISDN_GLOBAL_STOPPED)
608
                                return 0;
609
                        /* Find any net-device, waiting for D-channel setup */
610
                        if (isdn_net_stat_callback(i, c))
611
                                break;
612
                        /* Find any ttyI, waiting for D-channel setup */
613
                        if (isdn_tty_stat_callback(i, c)) {
614
                                cmd.driver = di;
615
                                cmd.arg = c->arg;
616
                                cmd.command = ISDN_CMD_ACCEPTB;
617
                                isdn_command(&cmd);
618
                                break;
619
                        }
620
                        break;
621
                case ISDN_STAT_DHUP:
622
                        if (i < 0)
623
                                return -1;
624
#ifdef ISDN_DEBUG_STATCALLB
625
                        printk(KERN_DEBUG "DHUP: %ld\n", c->arg);
626
#endif
627
                        if (dev->global_flags & ISDN_GLOBAL_STOPPED)
628
                                return 0;
629
                        dev->drv[di]->online &= ~(1 << (c->arg));
630
                        isdn_info_update();
631
                        /* Signal hangup to network-devices */
632
                        if (isdn_net_stat_callback(i, c))
633
                                break;
634
                        if (isdn_tty_stat_callback(i, c))
635
                                break;
636
                        break;
637
                case ISDN_STAT_BCONN:
638
                        if (i < 0)
639
                                return -1;
640
#ifdef ISDN_DEBUG_STATCALLB
641
                        printk(KERN_DEBUG "BCONN: %ld\n", c->arg);
642
#endif
643
                        /* Signal B-channel-connect to network-devices */
644
                        if (dev->global_flags & ISDN_GLOBAL_STOPPED)
645
                                return 0;
646
                        dev->drv[di]->online |= (1 << (c->arg));
647
                        isdn_info_update();
648
                        if (isdn_net_stat_callback(i, c))
649
                                break;
650
                        if (isdn_tty_stat_callback(i, c))
651
                                break;
652
                        break;
653
                case ISDN_STAT_BHUP:
654
                        if (i < 0)
655
                                return -1;
656
#ifdef ISDN_DEBUG_STATCALLB
657
                        printk(KERN_DEBUG "BHUP: %ld\n", c->arg);
658
#endif
659
                        if (dev->global_flags & ISDN_GLOBAL_STOPPED)
660
                                return 0;
661
                        dev->drv[di]->online &= ~(1 << (c->arg));
662
                        isdn_info_update();
663
                        if (isdn_tty_stat_callback(i, c))
664
                                break;
665
                        break;
666
                case ISDN_STAT_NODCH:
667
                        if (i < 0)
668
                                return -1;
669
#ifdef ISDN_DEBUG_STATCALLB
670
                        printk(KERN_DEBUG "NODCH: %ld\n", c->arg);
671
#endif
672
                        if (dev->global_flags & ISDN_GLOBAL_STOPPED)
673
                                return 0;
674
                        if (isdn_net_stat_callback(i, c))
675
                                break;
676
                        if (isdn_tty_stat_callback(i, c))
677
                                break;
678
                        break;
679
                case ISDN_STAT_ADDCH:
680
                        break;
681
                case ISDN_STAT_UNLOAD:
682
                        save_flags(flags);
683
                        cli();
684
                        isdn_tty_stat_callback(i, c);
685
                        for (i = 0; i < ISDN_MAX_CHANNELS; i++)
686
                                if (dev->drvmap[i] == di) {
687
                                        dev->drvmap[i] = -1;
688
                                        dev->chanmap[i] = -1;
689
                                }
690
                        dev->drivers--;
691
                        dev->channels -= dev->drv[di]->channels;
692
                        kfree(dev->drv[di]->rcverr);
693
                        kfree(dev->drv[di]->rcvcount);
694
                        for (i = 0; i < dev->drv[di]->channels; i++)
695
                                isdn_free_queue(&dev->drv[di]->rpqueue[i]);
696
                        kfree(dev->drv[di]->rpqueue);
697
                        kfree(dev->drv[di]->rcv_waitq);
698
                        kfree(dev->drv[di]->snd_waitq);
699
                        kfree(dev->drv[di]);
700
                        dev->drv[di] = NULL;
701
                        dev->drvid[di][0] = '\0';
702
                        isdn_info_update();
703
                        restore_flags(flags);
704
                        return 0;
705
                default:
706
                        return -1;
707
        }
708
        return 0;
709
}
710
 
711
/*
712
 * Get integer from char-pointer, set pointer to end of number
713
 */
714
int
715
isdn_getnum(char **p)
716
{
717
        int v = -1;
718
 
719
        while (*p[0] >= '0' && *p[0] <= '9')
720
                v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p[0]++) - '0');
721
        return v;
722
}
723
 
724
#define DLE 0x10
725
 
726
/*
727
 * isdn_readbchan() tries to get data from the read-queue.
728
 * It MUST be called with interrupts off.
729
 */
730
int
731
isdn_readbchan(int di, int channel, u_char * buf, u_char * fp, int len, int user)
732
{
733
        int left;
734
        int count;
735
        int count_pull;
736
        int count_put;
737
        int dflag;
738
        struct sk_buff *skb;
739
        u_char *cp;
740
 
741
        if (!dev->drv[di])
742
                return 0;
743
        if (skb_queue_empty(&dev->drv[di]->rpqueue[channel])) {
744
                if (user)
745
                        interruptible_sleep_on(&dev->drv[di]->rcv_waitq[channel]);
746
                else
747
                        return 0;
748
        }
749
        left = MIN(len, dev->drv[di]->rcvcount[channel]);
750
        cp = buf;
751
        count = 0;
752
        while (left) {
753
                if (!(skb = skb_peek(&dev->drv[di]->rpqueue[channel])))
754
                        break;
755
#ifdef CONFIG_ISDN_AUDIO
756
                if (ISDN_AUDIO_SKB_LOCK(skb))
757
                        break;
758
                ISDN_AUDIO_SKB_LOCK(skb) = 1;
759
                if (ISDN_AUDIO_SKB_DLECOUNT(skb)) {
760
                        char *p = skb->data;
761
                        unsigned long DLEmask = (1 << channel);
762
 
763
                        dflag = 0;
764
                        count_pull = count_put = 0;
765
                        while ((count_pull < skb->len) && (left-- > 0)) {
766
                                if (dev->drv[di]->DLEflag & DLEmask) {
767
                                        if (user)
768
                                                put_user(DLE, cp++);
769
                                        else
770
                                                *cp++ = DLE;
771
                                        dev->drv[di]->DLEflag &= ~DLEmask;
772
                                } else {
773
                                        if (user)
774
                                                put_user(*p, cp++);
775
                                        else
776
                                                *cp++ = *p;
777
                                        if (*p == DLE) {
778
                                                dev->drv[di]->DLEflag |= DLEmask;
779
                                                (ISDN_AUDIO_SKB_DLECOUNT(skb))--;
780
                                        }
781
                                        p++;
782
                                        count_pull++;
783
                                }
784
                                count_put++;
785
                        }
786
                        if (count_pull >= skb->len)
787
                                dflag = 1;
788
                } else {
789
#endif
790
                        /* No DLE's in buff, so simply copy it */
791
                        dflag = 1;
792
                        if ((count_pull = skb->len) > left) {
793
                                count_pull = left;
794
                                dflag = 0;
795
                        }
796
                        count_put = count_pull;
797
                        if (user)
798
                                copy_to_user(cp, skb->data, count_put);
799
                        else
800
                                memcpy(cp, skb->data, count_put);
801
                        cp += count_put;
802
                        left -= count_put;
803
#ifdef CONFIG_ISDN_AUDIO
804
                }
805
#endif
806
                count += count_put;
807
                if (fp) {
808
                        memset(fp, 0, count_put);
809
                        fp += count_put;
810
                }
811
                if (dflag) {
812
                        /* We got all the data in this buff.
813
                         * Now we can dequeue it.
814
                         */
815
                        if (fp)
816
                                *(fp - 1) = 0xff;
817
#ifdef CONFIG_ISDN_AUDIO
818
                        ISDN_AUDIO_SKB_LOCK(skb) = 0;
819
#endif
820
                        skb = skb_dequeue(&dev->drv[di]->rpqueue[channel]);
821
                        isdn_trash_skb(skb, FREE_READ);
822
                } else {
823
                        /* Not yet emptied this buff, so it
824
                         * must stay in the queue, for further calls
825
                         * but we pull off the data we got until now.
826
                         */
827
                        skb_pull(skb, count_pull);
828
#ifdef CONFIG_ISDN_AUDIO
829
                        ISDN_AUDIO_SKB_LOCK(skb) = 0;
830
#endif
831
                }
832
                dev->drv[di]->rcvcount[channel] -= count_put;
833
        }
834
        return count;
835
}
836
 
837
static __inline int
838
isdn_minor2drv(int minor)
839
{
840
        return (dev->drvmap[minor]);
841
}
842
 
843
static __inline int
844
isdn_minor2chan(int minor)
845
{
846
        return (dev->chanmap[minor]);
847
}
848
 
849
#define INF_DV 0x01             /* Data version for /dev/isdninfo */
850
 
851
static char *
852
isdn_statstr(void)
853
{
854
        static char istatbuf[2048];
855
        char *p;
856
        int i;
857
 
858
        sprintf(istatbuf, "idmap:\t");
859
        p = istatbuf + strlen(istatbuf);
860
        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
861
                sprintf(p, "%s ", (dev->drvmap[i] < 0) ? "-" : dev->drvid[dev->drvmap[i]]);
862
                p = istatbuf + strlen(istatbuf);
863
        }
864
        sprintf(p, "\nchmap:\t");
865
        p = istatbuf + strlen(istatbuf);
866
        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
867
                sprintf(p, "%d ", dev->chanmap[i]);
868
                p = istatbuf + strlen(istatbuf);
869
        }
870
        sprintf(p, "\ndrmap:\t");
871
        p = istatbuf + strlen(istatbuf);
872
        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
873
                sprintf(p, "%d ", dev->drvmap[i]);
874
                p = istatbuf + strlen(istatbuf);
875
        }
876
        sprintf(p, "\nusage:\t");
877
        p = istatbuf + strlen(istatbuf);
878
        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
879
                sprintf(p, "%d ", dev->usage[i]);
880
                p = istatbuf + strlen(istatbuf);
881
        }
882
        sprintf(p, "\nflags:\t");
883
        p = istatbuf + strlen(istatbuf);
884
        for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
885
                if (dev->drv[i]) {
886
                        sprintf(p, "%ld ", dev->drv[i]->online);
887
                        p = istatbuf + strlen(istatbuf);
888
                } else {
889
                        sprintf(p, "? ");
890
                        p = istatbuf + strlen(istatbuf);
891
                }
892
        }
893
        sprintf(p, "\nphone:\t");
894
        p = istatbuf + strlen(istatbuf);
895
        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
896
                sprintf(p, "%s ", dev->num[i]);
897
                p = istatbuf + strlen(istatbuf);
898
        }
899
        sprintf(p, "\n");
900
        return istatbuf;
901
}
902
 
903
/* Module interface-code */
904
 
905
void
906
isdn_info_update(void)
907
{
908
        infostruct *p = dev->infochain;
909
 
910
        while (p) {
911
                *(p->private) = 1;
912
                p = (infostruct *) p->next;
913
        }
914
        wake_up_interruptible(&(dev->info_waitq));
915
}
916
 
917
static RWTYPE
918
isdn_read(struct inode *inode, struct file *file, char *buf, RWARG count)
919
{
920
        uint minor = MINOR(inode->i_rdev);
921
        int len = 0;
922
        ulong flags;
923
        int drvidx;
924
        int chidx;
925
 
926
        if (minor == ISDN_MINOR_STATUS) {
927
                char *p;
928
                if (!file->private_data) {
929
                        if (file->f_flags & O_NONBLOCK)
930
                                return -EAGAIN;
931
                        interruptible_sleep_on(&(dev->info_waitq));
932
                }
933
                p = isdn_statstr();
934
                file->private_data = 0;
935
                if ((len = strlen(p)) <= count) {
936
                        if (copy_to_user(buf, p, len))
937
                                return -EFAULT;
938
                        file->f_pos += len;
939
                        return len;
940
                }
941
                return 0;
942
        }
943
        if (!dev->drivers)
944
                return -ENODEV;
945
        if (minor < ISDN_MINOR_CTRL) {
946
                drvidx = isdn_minor2drv(minor);
947
                if (drvidx < 0)
948
                        return -ENODEV;
949
                if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
950
                        return -ENODEV;
951
                chidx = isdn_minor2chan(minor);
952
                save_flags(flags);
953
                cli();
954
                len = isdn_readbchan(drvidx, chidx, buf, 0, count, 1);
955
                file->f_pos += len;
956
                restore_flags(flags);
957
                return len;
958
        }
959
        if (minor <= ISDN_MINOR_CTRLMAX) {
960
                drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
961
                if (drvidx < 0)
962
                        return -ENODEV;
963
                if (!dev->drv[drvidx]->stavail) {
964
                        if (file->f_flags & O_NONBLOCK)
965
                                return -EAGAIN;
966
                        interruptible_sleep_on(&(dev->drv[drvidx]->st_waitq));
967
                }
968
                if (dev->drv[drvidx]->interface->readstat)
969
                        len = dev->drv[drvidx]->interface->
970
                            readstat(buf, MIN(count, dev->drv[drvidx]->stavail),
971
                                     1, drvidx, isdn_minor2chan(minor));
972
                else
973
                        len = 0;
974
                save_flags(flags);
975
                cli();
976
                if (len)
977
                        dev->drv[drvidx]->stavail -= len;
978
                else
979
                        dev->drv[drvidx]->stavail = 0;
980
                restore_flags(flags);
981
                file->f_pos += len;
982
                return len;
983
        }
984
#ifdef CONFIG_ISDN_PPP
985
        if (minor <= ISDN_MINOR_PPPMAX)
986
                return (isdn_ppp_read(minor - ISDN_MINOR_PPP, file, buf, count));
987
#endif
988
        return -ENODEV;
989
}
990
 
991
static LSTYPE
992
isdn_lseek(struct inode *inode, struct file *file, LSARG offset, int orig)
993
{
994
        return -ESPIPE;
995
}
996
 
997
static RWTYPE
998
isdn_write(struct inode *inode, struct file *file, const char *buf, RWARG count)
999
{
1000
        uint minor = MINOR(inode->i_rdev);
1001
        int drvidx;
1002
        int chidx;
1003
 
1004
        if (minor == ISDN_MINOR_STATUS)
1005
                return -EPERM;
1006
        if (!dev->drivers)
1007
                return -ENODEV;
1008
        if (minor < ISDN_MINOR_CTRL) {
1009
                drvidx = isdn_minor2drv(minor);
1010
                if (drvidx < 0)
1011
                        return -ENODEV;
1012
                if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1013
                        return -ENODEV;
1014
                chidx = isdn_minor2chan(minor);
1015
                while (isdn_writebuf_stub(drvidx, chidx, buf, count, 1) != count)
1016
                        interruptible_sleep_on(&dev->drv[drvidx]->snd_waitq[chidx]);
1017
                return count;
1018
        }
1019
        if (minor <= ISDN_MINOR_CTRLMAX) {
1020
                drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1021
                if (drvidx < 0)
1022
                        return -ENODEV;
1023
                /*
1024
                 * We want to use the isdnctrl device to load the firmware
1025
                 *
1026
                if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1027
                 return -ENODEV;
1028
                 */
1029
                if (dev->drv[drvidx]->interface->writecmd)
1030
                        return (dev->drv[drvidx]->interface->
1031
                                writecmd(buf, count, 1, drvidx, isdn_minor2chan(minor)));
1032
                else
1033
                        return count;
1034
        }
1035
#ifdef CONFIG_ISDN_PPP
1036
        if (minor <= ISDN_MINOR_PPPMAX)
1037
                return (isdn_ppp_write(minor - ISDN_MINOR_PPP, file, buf, count));
1038
#endif
1039
        return -ENODEV;
1040
}
1041
 
1042
#if (LINUX_VERSION_CODE < 0x020117)
1043
static int
1044
isdn_select(struct inode *inode, struct file *file, int type, select_table * st)
1045
{
1046
        uint minor = MINOR(inode->i_rdev);
1047
        int drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1048
 
1049
        if (minor == ISDN_MINOR_STATUS) {
1050
                if (file->private_data)
1051
                        return 1;
1052
                else {
1053
                        if (st)
1054
                                select_wait(&(dev->info_waitq), st);
1055
                        return 0;
1056
                }
1057
        }
1058
        if (minor >= ISDN_MINOR_CTRL && minor <= ISDN_MINOR_CTRLMAX) {
1059
                if (drvidx < 0)
1060
                        return -ENODEV;
1061
                if (dev->drv[drvidx]->stavail)
1062
                        return 1;
1063
                else {
1064
                        if (st)
1065
                                select_wait(&(dev->drv[drvidx]->st_waitq), st);
1066
                        return 0;
1067
                }
1068
                return 1;
1069
        }
1070
#ifdef CONFIG_ISDN_PPP
1071
        if (minor <= ISDN_MINOR_PPPMAX)
1072
                return (isdn_ppp_select(minor - ISDN_MINOR_PPP, file, type, st));
1073
#endif
1074
        return -ENODEV;
1075
}
1076
#else
1077
static unsigned int
1078
isdn_poll(struct file *file, poll_table * wait)
1079
{
1080
        unsigned int mask = 0;
1081
        unsigned int minor = MINOR(file->f_inode->i_rdev);
1082
        int drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1083
 
1084
        if (minor == ISDN_MINOR_STATUS) {
1085
                poll_wait(&(dev->info_waitq), wait);
1086
                /* mask = POLLOUT | POLLWRNORM; */
1087
                if (file->private_data) {
1088
                        mask |= POLLIN | POLLRDNORM;
1089
                }
1090
                return mask;
1091
        }
1092
        if (minor >= ISDN_MINOR_CTRL && minor <= ISDN_MINOR_CTRLMAX) {
1093
                poll_wait(&(dev->drv[drvidx]->st_waitq), wait);
1094
                if (drvidx < 0) {
1095
                        printk(KERN_ERR "isdn_common: isdn_poll 1 -> what the hell\n");
1096
                        return POLLERR;
1097
                }
1098
                mask = POLLOUT | POLLWRNORM;
1099
                if (dev->drv[drvidx]->stavail) {
1100
                        mask |= POLLIN | POLLRDNORM;
1101
                }
1102
                return mask;
1103
        }
1104
#ifdef CONFIG_ISDN_PPP
1105
        if (minor <= ISDN_MINOR_PPPMAX)
1106
                return (isdn_ppp_poll(file, wait));
1107
#endif
1108
        printk(KERN_ERR "isdn_common: isdn_poll 2 -> what the hell\n");
1109
        return POLLERR;
1110
}
1111
#endif
1112
 
1113
typedef union {
1114
        char name[10];
1115
        char bname[22];
1116
        isdn_ioctl_struct iocts;
1117
        isdn_net_ioctl_phone phone;
1118
        isdn_net_ioctl_cfg cfg;
1119
} iocpar_t;
1120
 
1121
static int
1122
isdn_ioctl(struct inode *inode, struct file *file, uint cmd, ulong arg)
1123
{
1124
        uint minor = MINOR(inode->i_rdev);
1125
        isdn_ctrl c;
1126
        int drvidx;
1127
        int chidx;
1128
        int ret;
1129
        int i;
1130
        char *p;
1131
        char *s;
1132
        iocpar_t *iocpar;
1133
 
1134
#define name  iocpar->name
1135
#define bname iocpar->bname
1136
#define iocts iocpar->iocts
1137
#define phone iocpar->phone
1138
#define cfg   iocpar->cfg
1139
 
1140
#define RETURN(r) ({ ret = r; kfree(iocpar); return ret; })
1141
 
1142
        if (minor == ISDN_MINOR_STATUS) {
1143
                switch (cmd) {
1144
                        case IIOCGETDVR:
1145
                                return (TTY_DV +
1146
                                        (NET_DV << 8) +
1147
                                        (INF_DV << 16));
1148
                        case IIOCGETCPS:
1149
                                if (arg) {
1150
                                        ulong *p = (ulong *) arg;
1151
                                        int i;
1152
                                        if ((ret = verify_area(VERIFY_WRITE, (void *) arg,
1153
                                                               sizeof(ulong) * ISDN_MAX_CHANNELS * 2)))
1154
                                                return ret;
1155
                                        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1156
                                                put_user(dev->ibytes[i], p++);
1157
                                                put_user(dev->obytes[i], p++);
1158
                                        }
1159
                                        return 0;
1160
                                } else
1161
                                        return -EINVAL;
1162
                                break;
1163
                        default:
1164
                                return -EINVAL;
1165
                }
1166
        }
1167
        if (!dev->drivers)
1168
                return -ENODEV;
1169
        if (minor < ISDN_MINOR_CTRL) {
1170
                drvidx = isdn_minor2drv(minor);
1171
                if (drvidx < 0)
1172
                        return -ENODEV;
1173
                chidx = isdn_minor2chan(minor);
1174
                if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1175
                        return -ENODEV;
1176
                return 0;
1177
        }
1178
        if (minor <= ISDN_MINOR_CTRLMAX) {
1179
                if ((iocpar = (iocpar_t *)kmalloc(sizeof(iocpar_t), GFP_KERNEL)) == NULL) {
1180
                        printk(KERN_WARNING "isdn: Out of memory in isdn_ioctl\n");
1181
                        return -ENOMEM;
1182
                }
1183
                switch (cmd) {
1184
#ifdef CONFIG_NETDEVICES
1185
                        case IIOCNETAIF:
1186
                                /* Add a network-interface */
1187
                                if (arg) {
1188
                                        if ((ret = copy_from_user(name, (char *) arg, sizeof(name))))
1189
                                                RETURN(ret);
1190
                                        s = name;
1191
                                } else
1192
                                        s = NULL;
1193
                                if ((s = isdn_net_new(s, NULL))) {
1194
                                        if ((ret = copy_to_user((char *) arg, s, strlen(s) + 1)))
1195
                                                RETURN(ret);
1196
                                        RETURN(0);
1197
                                } else
1198
                                        RETURN(-ENODEV);
1199
                        case IIOCNETASL:
1200
                                /* Add a slave to a network-interface */
1201
                                if (arg) {
1202
                                        if ((ret = copy_from_user(bname, (char *) arg, sizeof(bname) - 1)))
1203
                                                RETURN(ret);
1204
                                } else
1205
                                        RETURN(-EINVAL);
1206
                                if ((s = isdn_net_newslave(bname))) {
1207
                                        if ((ret = copy_to_user((char *) arg, s, strlen(s) + 1)))
1208
                                                RETURN(ret);
1209
                                        RETURN(0);
1210
                                } else
1211
                                        RETURN(-ENODEV);
1212
                        case IIOCNETDIF:
1213
                                /* Delete a network-interface */
1214
                                if (arg) {
1215
                                        if ((ret = copy_from_user(name, (char *) arg, sizeof(name))))
1216
                                                RETURN(ret);
1217
                                        RETURN(isdn_net_rm(name));
1218
                                } else
1219
                                        RETURN(-EINVAL);
1220
                        case IIOCNETSCF:
1221
                                /* Set configurable parameters of a network-interface */
1222
                                if (arg) {
1223
                                        if ((ret = copy_from_user((char *) &cfg, (char *) arg, sizeof(cfg))))
1224
                                                RETURN(ret);
1225
                                        RETURN(isdn_net_setcfg(&cfg));
1226
                                } else
1227
                                        RETURN(-EINVAL);
1228
                        case IIOCNETGCF:
1229
                                /* Get configurable parameters of a network-interface */
1230
                                if (arg) {
1231
                                        if ((ret = copy_from_user((char *) &cfg, (char *) arg, sizeof(cfg))))
1232
                                                RETURN(ret);
1233
                                        if (!(ret = isdn_net_getcfg(&cfg))) {
1234
                                                if ((ret = copy_to_user((char *) arg, (char *) &cfg, sizeof(cfg))))
1235
                                                        RETURN(ret);
1236
                                        }
1237
                                        RETURN(ret);
1238
                                } else
1239
                                        RETURN(-EINVAL);
1240
                        case IIOCNETANM:
1241
                                /* Add a phone-number to a network-interface */
1242
                                if (arg) {
1243
                                        if ((ret = copy_from_user((char *) &phone, (char *) arg, sizeof(phone))))
1244
                                                RETURN(ret);
1245
                                        RETURN(isdn_net_addphone(&phone));
1246
                                } else
1247
                                        RETURN(-EINVAL);
1248
                        case IIOCNETGNM:
1249
                                /* Get list of phone-numbers of a network-interface */
1250
                                if (arg) {
1251
                                        if ((ret = copy_from_user((char *) &phone, (char *) arg, sizeof(phone))))
1252
                                                RETURN(ret);
1253
                                        RETURN(isdn_net_getphones(&phone, (char *) arg));
1254
                                } else
1255
                                        RETURN(-EINVAL);
1256
                        case IIOCNETDNM:
1257
                                /* Delete a phone-number of a network-interface */
1258
                                if (arg) {
1259
                                        if ((ret = copy_from_user((char *) &phone, (char *) arg, sizeof(phone))))
1260
                                                RETURN(ret);
1261
                                        RETURN(isdn_net_delphone(&phone));
1262
                                } else
1263
                                        RETURN(-EINVAL);
1264
                        case IIOCNETDIL:
1265
                                /* Force dialing of a network-interface */
1266
                                if (arg) {
1267
                                        if ((ret = copy_from_user(name, (char *) arg, sizeof(name))))
1268
                                                RETURN(ret);
1269
                                        RETURN(isdn_net_force_dial(name));
1270
                                } else
1271
                                        RETURN(-EINVAL);
1272
#ifdef CONFIG_ISDN_PPP
1273
                        case IIOCNETALN:
1274
                                if (!arg)
1275
                                        RETURN(-EINVAL);
1276
                                if ((ret = copy_from_user(name, (char *) arg, sizeof(name))))
1277
                                        RETURN(ret);
1278
                                RETURN(isdn_ppp_dial_slave(name));
1279
                        case IIOCNETDLN:
1280
                                if (!arg)
1281
                                        RETURN(-EINVAL);
1282
                                if ((ret = copy_from_user(name, (char *) arg, sizeof(name))))
1283
                                        RETURN(ret);
1284
                                RETURN(isdn_ppp_hangup_slave(name));
1285
#endif
1286
                        case IIOCNETHUP:
1287
                                /* Force hangup of a network-interface */
1288
                                if (!arg)
1289
                                        RETURN(-EINVAL);
1290
                                if ((ret = copy_from_user(name, (char *) arg, sizeof(name))))
1291
                                        RETURN(ret);
1292
                                RETURN(isdn_net_force_hangup(name));
1293
                                break;
1294
#endif                          /* CONFIG_NETDEVICES */
1295
                        case IIOCSETVER:
1296
                                dev->net_verbose = arg;
1297
                                printk(KERN_INFO "isdn: Verbose-Level is %d\n", dev->net_verbose);
1298
                                RETURN(0);
1299
                        case IIOCSETGST:
1300
                                if (arg)
1301
                                        dev->global_flags |= ISDN_GLOBAL_STOPPED;
1302
                                else
1303
                                        dev->global_flags &= ~ISDN_GLOBAL_STOPPED;
1304
                                printk(KERN_INFO "isdn: Global Mode %s\n",
1305
                                       (dev->global_flags & ISDN_GLOBAL_STOPPED) ? "stopped" : "running");
1306
                                RETURN(0);
1307
                        case IIOCSETBRJ:
1308
                                drvidx = -1;
1309
                                if (arg) {
1310
                                        int i;
1311
                                        char *p;
1312
                                        if ((ret = copy_from_user((char *) &iocts, (char *) arg,
1313
                                              sizeof(isdn_ioctl_struct))))
1314
                                                RETURN(ret);
1315
                                        if (strlen(iocts.drvid)) {
1316
                                                if ((p = strchr(iocts.drvid, ',')))
1317
                                                        *p = 0;
1318
                                                drvidx = -1;
1319
                                                for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1320
                                                        if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1321
                                                                drvidx = i;
1322
                                                                break;
1323
                                                        }
1324
                                        }
1325
                                }
1326
                                if (drvidx == -1)
1327
                                        RETURN(-ENODEV);
1328
                                if (iocts.arg)
1329
                                        dev->drv[drvidx]->flags |= DRV_FLAG_REJBUS;
1330
                                else
1331
                                        dev->drv[drvidx]->flags &= ~DRV_FLAG_REJBUS;
1332
                                RETURN(0);
1333
                        case IIOCSIGPRF:
1334
                                dev->profd = current;
1335
                                RETURN(0);
1336
                                break;
1337
                        case IIOCGETPRF:
1338
                                /* Get all Modem-Profiles */
1339
                                if (arg) {
1340
                                        char *p = (char *) arg;
1341
                                        int i;
1342
 
1343
                                        if ((ret = verify_area(VERIFY_WRITE, (void *) arg,
1344
                                        (ISDN_MODEM_ANZREG + ISDN_MSNLEN)
1345
                                                   * ISDN_MAX_CHANNELS)))
1346
                                                RETURN(ret);
1347
 
1348
                                        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1349
                                                if (copy_to_user(p, dev->mdm.info[i].emu.profile,
1350
                                                      ISDN_MODEM_ANZREG))
1351
                                                        RETURN(-EFAULT);
1352
                                                p += ISDN_MODEM_ANZREG;
1353
                                                if (copy_to_user(p, dev->mdm.info[i].emu.pmsn, ISDN_MSNLEN))
1354
                                                        RETURN(-EFAULT);
1355
                                                p += ISDN_MSNLEN;
1356
                                        }
1357
                                        RETURN((ISDN_MODEM_ANZREG + ISDN_MSNLEN) * ISDN_MAX_CHANNELS);
1358
                                } else
1359
                                        RETURN(-EINVAL);
1360
                                break;
1361
                        case IIOCSETPRF:
1362
                                /* Set all Modem-Profiles */
1363
                                if (arg) {
1364
                                        char *p = (char *) arg;
1365
                                        int i;
1366
 
1367
                                        if ((ret = verify_area(VERIFY_READ, (void *) arg,
1368
                                        (ISDN_MODEM_ANZREG + ISDN_MSNLEN)
1369
                                                   * ISDN_MAX_CHANNELS)))
1370
                                                RETURN(ret);
1371
 
1372
                                        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1373
                                                if ((ret = copy_from_user(dev->mdm.info[i].emu.profile, p,
1374
                                                      ISDN_MODEM_ANZREG)))
1375
                                                        RETURN(ret);
1376
                                                p += ISDN_MODEM_ANZREG;
1377
                                                if ((ret = copy_from_user(dev->mdm.info[i].emu.pmsn, p, ISDN_MSNLEN)))
1378
                                                        RETURN(ret);
1379
                                                p += ISDN_MSNLEN;
1380
                                        }
1381
                                        RETURN(0);
1382
                                } else
1383
                                        RETURN(-EINVAL);
1384
                                break;
1385
                        case IIOCSETMAP:
1386
                        case IIOCGETMAP:
1387
                                /* Set/Get MSN->EAZ-Mapping for a driver */
1388
                                if (arg) {
1389
 
1390
                                        if ((ret = copy_from_user((char *) &iocts,
1391
                                                            (char *) arg,
1392
                                             sizeof(isdn_ioctl_struct))))
1393
                                                RETURN(ret);
1394
                                        if (strlen(iocts.drvid)) {
1395
                                                drvidx = -1;
1396
                                                for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1397
                                                        if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1398
                                                                drvidx = i;
1399
                                                                break;
1400
                                                        }
1401
                                        } else
1402
                                                drvidx = 0;
1403
                                        if (drvidx == -1)
1404
                                                RETURN(-ENODEV);
1405
                                        if (cmd == IIOCSETMAP) {
1406
                                                int loop = 1;
1407
 
1408
                                                p = (char *) iocts.arg;
1409
                                                i = 0;
1410
                                                while (loop) {
1411
                                                        int j = 0;
1412
 
1413
                                                        while (1) {
1414
                                                                if ((ret = verify_area(VERIFY_READ, p, 1)))
1415
                                                                        RETURN(ret);
1416
                                                                GET_USER(bname[j], p++);
1417
                                                                switch (bname[j]) {
1418
                                                                        case '\0':
1419
                                                                                loop = 0;
1420
                                                                                /* Fall through */
1421
                                                                        case ',':
1422
                                                                                bname[j] = '\0';
1423
                                                                                strcpy(dev->drv[drvidx]->msn2eaz[i], bname);
1424
                                                                                j = ISDN_MSNLEN;
1425
                                                                                break;
1426
                                                                        default:
1427
                                                                                j++;
1428
                                                                }
1429
                                                                if (j >= ISDN_MSNLEN)
1430
                                                                        break;
1431
                                                        }
1432
                                                        if (++i > 9)
1433
                                                                break;
1434
                                                }
1435
                                        } else {
1436
                                                p = (char *) iocts.arg;
1437
                                                for (i = 0; i < 10; i++) {
1438
                                                        sprintf(bname, "%s%s",
1439
                                                                strlen(dev->drv[drvidx]->msn2eaz[i]) ?
1440
                                                                dev->drv[drvidx]->msn2eaz[i] : "-",
1441
                                                                (i < 9) ? "," : "\0");
1442
                                                        if ((ret = copy_to_user(p, bname, strlen(bname) + 1)))
1443
                                                                RETURN(ret);
1444
                                                        p += strlen(bname);
1445
                                                }
1446
                                        }
1447
                                        RETURN(0);
1448
                                } else
1449
                                        RETURN(-EINVAL);
1450
                        case IIOCDBGVAR:
1451
                                if (arg) {
1452
                                        if ((ret = copy_to_user((char *) arg, (char *) &dev, sizeof(ulong))))
1453
                                                RETURN(ret);
1454
                                        RETURN(0);
1455
                                } else
1456
                                        RETURN(-EINVAL);
1457
                                break;
1458
                        default:
1459
                                if ((cmd & IIOCDRVCTL) == IIOCDRVCTL)
1460
                                        cmd = ((cmd >> _IOC_NRSHIFT) & _IOC_NRMASK) & ISDN_DRVIOCTL_MASK;
1461
                                else
1462
                                        RETURN(-EINVAL);
1463
                                if (arg) {
1464
                                        int i;
1465
                                        char *p;
1466
                                        if ((ret = copy_from_user((char *) &iocts, (char *) arg, sizeof(isdn_ioctl_struct))))
1467
                                                RETURN(ret);
1468
                                        if (strlen(iocts.drvid)) {
1469
                                                if ((p = strchr(iocts.drvid, ',')))
1470
                                                        *p = 0;
1471
                                                drvidx = -1;
1472
                                                for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1473
                                                        if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1474
                                                                drvidx = i;
1475
                                                                break;
1476
                                                        }
1477
                                        } else
1478
                                                drvidx = 0;
1479
                                        if (drvidx == -1)
1480
                                                RETURN(-ENODEV);
1481
                                        if ((ret = verify_area(VERIFY_WRITE, (void *) arg,
1482
                                             sizeof(isdn_ioctl_struct))))
1483
                                                RETURN(ret);
1484
                                        c.driver = drvidx;
1485
                                        c.command = ISDN_CMD_IOCTL;
1486
                                        c.arg = cmd;
1487
                                        memcpy(c.parm.num, (char *) &iocts.arg, sizeof(ulong));
1488
                                        ret = isdn_command(&c);
1489
                                        memcpy((char *) &iocts.arg, c.parm.num, sizeof(ulong));
1490
                                        if ((copy_to_user((char *) arg, &iocts, sizeof(isdn_ioctl_struct))))
1491
                                                RETURN(-EFAULT);
1492
                                        RETURN(ret);
1493
                                } else
1494
                                        RETURN(-EINVAL);
1495
                }
1496
        }
1497
#ifdef CONFIG_ISDN_PPP
1498
        if (minor <= ISDN_MINOR_PPPMAX)
1499
                return (isdn_ppp_ioctl(minor - ISDN_MINOR_PPP, file, cmd, arg));
1500
#endif
1501
        return -ENODEV;
1502
 
1503
#undef name
1504
#undef bname
1505
#undef iocts
1506
#undef phone
1507
#undef cfg
1508
 
1509
#undef RETURN
1510
}
1511
 
1512
/*
1513
 * Open the device code.
1514
 * MOD_INC_USE_COUNT make sure that the driver memory is not freed
1515
 * while the device is in use.
1516
 */
1517
static int
1518
isdn_open(struct inode *ino, struct file *filep)
1519
{
1520
        uint minor = MINOR(ino->i_rdev);
1521
        int drvidx;
1522
        int chidx;
1523
 
1524
        if (minor == ISDN_MINOR_STATUS) {
1525
                infostruct *p;
1526
 
1527
                if ((p = (infostruct *) kmalloc(sizeof(infostruct), GFP_KERNEL))) {
1528
                        MOD_INC_USE_COUNT;
1529
                        p->next = (char *) dev->infochain;
1530
                        p->private = (char *) &(filep->private_data);
1531
                        dev->infochain = p;
1532
                        /* At opening we allow a single update */
1533
                        filep->private_data = (char *) 1;
1534
                        return 0;
1535
                } else
1536
                        return -ENOMEM;
1537
        }
1538
        if (!dev->channels)
1539
                return -ENODEV;
1540
        if (minor < ISDN_MINOR_CTRL) {
1541
                drvidx = isdn_minor2drv(minor);
1542
                if (drvidx < 0)
1543
                        return -ENODEV;
1544
                chidx = isdn_minor2chan(minor);
1545
                if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1546
                        return -ENODEV;
1547
                if (!(dev->drv[drvidx]->online & (1 << chidx)))
1548
                        return -ENODEV;
1549
                isdn_MOD_INC_USE_COUNT();
1550
                return 0;
1551
        }
1552
        if (minor <= ISDN_MINOR_CTRLMAX) {
1553
                drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1554
                if (drvidx < 0)
1555
                        return -ENODEV;
1556
                isdn_MOD_INC_USE_COUNT();
1557
                return 0;
1558
        }
1559
#ifdef CONFIG_ISDN_PPP
1560
        if (minor <= ISDN_MINOR_PPPMAX) {
1561
                int ret;
1562
                if (!(ret = isdn_ppp_open(minor - ISDN_MINOR_PPP, filep)))
1563
                        isdn_MOD_INC_USE_COUNT();
1564
                return ret;
1565
        }
1566
#endif
1567
        return -ENODEV;
1568
}
1569
 
1570
static CLOSETYPE
1571
isdn_close(struct inode *ino, struct file *filep)
1572
{
1573
        uint minor = MINOR(ino->i_rdev);
1574
 
1575
        if (minor == ISDN_MINOR_STATUS) {
1576
                infostruct *p = dev->infochain;
1577
                infostruct *q = NULL;
1578
 
1579
 
1580
                MOD_DEC_USE_COUNT;
1581
                while (p) {
1582
                        if (p->private == (char *) &(filep->private_data)) {
1583
                                if (q)
1584
                                        q->next = p->next;
1585
                                else
1586
                                        dev->infochain = (infostruct *) (p->next);
1587
                                kfree(p);
1588
                                return CLOSEVAL;
1589
                        }
1590
                        q = p;
1591
                        p = (infostruct *) (p->next);
1592
                }
1593
                printk(KERN_WARNING "isdn: No private data while closing isdnctrl\n");
1594
                return CLOSEVAL;
1595
        }
1596
        isdn_MOD_DEC_USE_COUNT();
1597
        if (minor < ISDN_MINOR_CTRL)
1598
                return CLOSEVAL;
1599
        if (minor <= ISDN_MINOR_CTRLMAX) {
1600
                if (dev->profd == current)
1601
                        dev->profd = NULL;
1602
                return CLOSEVAL;
1603
        }
1604
#ifdef CONFIG_ISDN_PPP
1605
        if (minor <= ISDN_MINOR_PPPMAX)
1606
                isdn_ppp_release(minor - ISDN_MINOR_PPP, filep);
1607
#endif
1608
        return CLOSEVAL;
1609
}
1610
 
1611
static struct file_operations isdn_fops =
1612
{
1613
        isdn_lseek,
1614
        isdn_read,
1615
        isdn_write,
1616
        NULL,                   /* isdn_readdir */
1617
#if (LINUX_VERSION_CODE < 0x020117)
1618
        isdn_select,            /* isdn_select */
1619
#else
1620
        isdn_poll,              /* isdn_poll */
1621
#endif
1622
        isdn_ioctl,             /* isdn_ioctl */
1623
        NULL,                   /* isdn_mmap */
1624
        isdn_open,
1625
        isdn_close,
1626
        NULL                    /* fsync */
1627
};
1628
 
1629
char *
1630
isdn_map_eaz2msn(char *msn, int di)
1631
{
1632
        driver *this = dev->drv[di];
1633
        int i;
1634
 
1635
        if (strlen(msn) == 1) {
1636
                i = msn[0] - '0';
1637
                if ((i >= 0) && (i <= 9))
1638
                        if (strlen(this->msn2eaz[i]))
1639
                                return (this->msn2eaz[i]);
1640
        }
1641
        return (msn);
1642
}
1643
 
1644
/*
1645
 * Find an unused ISDN-channel, whose feature-flags match the
1646
 * given L2- and L3-protocols.
1647
 */
1648
int
1649
isdn_get_free_channel(int usage, int l2_proto, int l3_proto, int pre_dev
1650
                      ,int pre_chan)
1651
{
1652
        int i;
1653
        ulong flags;
1654
        ulong features;
1655
 
1656
        save_flags(flags);
1657
        cli();
1658
        features = (1 << l2_proto) | (0x100 << l3_proto);
1659
        for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1660
                if (USG_NONE(dev->usage[i]) &&
1661
                    (dev->drvmap[i] != -1)) {
1662
                        int d = dev->drvmap[i];
1663
                        if ((dev->usage[i] & ISDN_USAGE_EXCLUSIVE) &&
1664
                        ((pre_dev != d) || (pre_chan != dev->chanmap[i])))
1665
                                continue;
1666
                        if (dev->drv[d]->flags & DRV_FLAG_RUNNING) {
1667
                                if ((dev->drv[d]->interface->features & features) == features) {
1668
                                        if ((pre_dev < 0) || (pre_chan < 0)) {
1669
                                                dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1670
                                                dev->usage[i] |= usage;
1671
                                                isdn_info_update();
1672
                                                restore_flags(flags);
1673
                                                return i;
1674
                                        } else {
1675
                                                if ((pre_dev == d) && (pre_chan == dev->chanmap[i])) {
1676
                                                        dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1677
                                                        dev->usage[i] |= usage;
1678
                                                        isdn_info_update();
1679
                                                        restore_flags(flags);
1680
                                                        return i;
1681
                                                }
1682
                                        }
1683
                                }
1684
                        }
1685
                }
1686
        restore_flags(flags);
1687
        return -1;
1688
}
1689
 
1690
/*
1691
 * Set state of ISDN-channel to 'unused'
1692
 */
1693
void
1694
isdn_free_channel(int di, int ch, int usage)
1695
{
1696
        int i;
1697
        ulong flags;
1698
        isdn_ctrl cmd;
1699
 
1700
        save_flags(flags);
1701
        cli();
1702
        for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1703
                if (((dev->usage[i] & ISDN_USAGE_MASK) == usage) &&
1704
                    (dev->drvmap[i] == di) &&
1705
                    (dev->chanmap[i] == ch)) {
1706
                        dev->usage[i] &= (ISDN_USAGE_NONE | ISDN_USAGE_EXCLUSIVE);
1707
                        strcpy(dev->num[i], "???");
1708
                        dev->ibytes[i] = 0;
1709
                        dev->obytes[i] = 0;
1710
                        isdn_info_update();
1711
                        isdn_free_queue(&dev->drv[di]->rpqueue[ch]);
1712
                        cmd.driver = di;
1713
                        cmd.arg = ch;
1714
                        cmd.command = ISDN_CMD_HANGUP;
1715
                        restore_flags(flags);
1716
                        isdn_command(&cmd);
1717
                        return;
1718
                }
1719
        printk(KERN_DEBUG "Unmatched free_channel d=%d c=%d u=%d\n", di, ch, usage);
1720
        restore_flags(flags);
1721
}
1722
 
1723
/*
1724
 * Cancel Exclusive-Flag for ISDN-channel
1725
 */
1726
void
1727
isdn_unexclusive_channel(int di, int ch)
1728
{
1729
        int i;
1730
        ulong flags;
1731
 
1732
        save_flags(flags);
1733
        cli();
1734
        for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1735
                if ((dev->drvmap[i] == di) &&
1736
                    (dev->chanmap[i] == ch)) {
1737
                        dev->usage[i] &= ~ISDN_USAGE_EXCLUSIVE;
1738
                        isdn_info_update();
1739
                        restore_flags(flags);
1740
                        return;
1741
                }
1742
        restore_flags(flags);
1743
}
1744
 
1745
/*
1746
 * receive callback handler for drivers not supporting sk_buff's.
1747
 * Parameters:
1748
 *
1749
 * di      = Driver-Index.
1750
 * channel = Number of B-Channel (0...)
1751
 * buf     = pointer to packet-data
1752
 * len     = Length of packet-data
1753
 *
1754
 */
1755
static void
1756
isdn_receive_callback(int drvidx, int chan, u_char * buf, int len)
1757
{
1758
        struct sk_buff *skb;
1759
 
1760
        if (dev->global_flags & ISDN_GLOBAL_STOPPED)
1761
                return;
1762
        skb = dev_alloc_skb(len);
1763
        if (skb) {
1764
                memcpy(skb_put(skb, len), buf, len);
1765
                isdn_receive_skb_callback(drvidx, chan, skb);
1766
        } else
1767
                printk(KERN_WARNING "isdn: rcv alloc_skb failed, packet dropped.\n");
1768
}
1769
 
1770
/*
1771
 *  writebuf replacement for SKB_ABLE drivers
1772
 */
1773
static int
1774
isdn_writebuf_stub(int drvidx, int chan, const u_char * buf, int len,
1775
                   int user)
1776
{
1777
        int ret;
1778
 
1779
        if (dev->drv[drvidx]->interface->writebuf)
1780
                ret = dev->drv[drvidx]->interface->writebuf(drvidx, chan, buf,
1781
                                                            len, user);
1782
        else {
1783
                struct sk_buff *skb;
1784
 
1785
                skb = alloc_skb(dev->drv[drvidx]->interface->hl_hdrlen + len,
1786
                                GFP_ATOMIC);
1787
                if (skb == NULL)
1788
                        return 0;
1789
 
1790
                skb_reserve(skb, dev->drv[drvidx]->interface->hl_hdrlen);
1791
                SET_SKB_FREE(skb);
1792
 
1793
                if (user)
1794
                        copy_from_user(skb_put(skb, len), buf, len);
1795
                else
1796
                        memcpy(skb_put(skb, len), buf, len);
1797
 
1798
                ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx,
1799
                                                              chan, skb);
1800
                if (ret <= 0)
1801
                        kfree_skb(skb, FREE_WRITE);
1802
        }
1803
        if (ret > 0)
1804
                dev->obytes[isdn_dc2minor(drvidx, chan)] += ret;
1805
        return ret;
1806
}
1807
 
1808
/*
1809
 * writebuf_skb replacement for NON SKB_ABLE drivers
1810
 * If lowlevel-device does not support supports skbufs, use
1811
 * standard send-routine, else sind directly.
1812
 *
1813
 * Return: length of data on success, -ERRcode on failure.
1814
 */
1815
 
1816
int
1817
isdn_writebuf_skb_stub(int drvidx, int chan, struct sk_buff *skb)
1818
{
1819
        int ret;
1820
        int len = skb->len;     /* skb pointer no longer valid after free */
1821
 
1822
        if (dev->drv[drvidx]->interface->writebuf_skb)
1823
                ret = dev->drv[drvidx]->interface->
1824
                    writebuf_skb(drvidx, chan, skb);
1825
        else {
1826
                if ((ret = dev->drv[drvidx]->interface->
1827
                  writebuf(drvidx, chan, skb->data, skb->len, 0)) == len)
1828
                        dev_kfree_skb(skb, FREE_WRITE);
1829
        }
1830
        if (ret > 0)
1831
                dev->obytes[isdn_dc2minor(drvidx, chan)] += len;
1832
        return ret;
1833
}
1834
 
1835
/*
1836
 * Low-level-driver registration
1837
 */
1838
 
1839
int
1840
register_isdn(isdn_if * i)
1841
{
1842
        driver *d;
1843
        int n,
1844
         j,
1845
         k;
1846
        ulong flags;
1847
        int drvidx;
1848
 
1849
        if (dev->drivers >= ISDN_MAX_DRIVERS) {
1850
                printk(KERN_WARNING "register_isdn: Max. %d drivers supported\n",
1851
                       ISDN_MAX_DRIVERS);
1852
                return 0;
1853
        }
1854
        n = i->channels;
1855
        if (dev->channels + n > ISDN_MAX_CHANNELS) {
1856
                printk(KERN_WARNING "register_isdn: Max. %d channels supported\n",
1857
                       ISDN_MAX_CHANNELS);
1858
                return 0;
1859
        }
1860
        if ((!i->writebuf_skb) && (!i->writebuf)) {
1861
                printk(KERN_WARNING "register_isdn: No write routine given.\n");
1862
                return 0;
1863
        }
1864
        if (!(d = (driver *) kmalloc(sizeof(driver), GFP_KERNEL))) {
1865
                printk(KERN_WARNING "register_isdn: Could not alloc driver-struct\n");
1866
                return 0;
1867
        }
1868
        memset((char *) d, 0, sizeof(driver));
1869
        if (!(d->rcverr = (int *) kmalloc(sizeof(int) * n, GFP_KERNEL))) {
1870
                printk(KERN_WARNING "register_isdn: Could not alloc rcverr\n");
1871
                kfree(d);
1872
                return 0;
1873
        }
1874
        memset((char *) d->rcverr, 0, sizeof(int) * n);
1875
        if (!(d->rcvcount = (int *) kmalloc(sizeof(int) * n, GFP_KERNEL))) {
1876
                printk(KERN_WARNING "register_isdn: Could not alloc rcvcount\n");
1877
                kfree(d->rcverr);
1878
                kfree(d);
1879
                return 0;
1880
        }
1881
        memset((char *) d->rcvcount, 0, sizeof(int) * n);
1882
        if (!(d->rpqueue =
1883
              (struct sk_buff_head *) kmalloc(sizeof(struct sk_buff_head) * n, GFP_KERNEL))) {
1884
                printk(KERN_WARNING "register_isdn: Could not alloc rpqueue\n");
1885
                kfree(d->rcvcount);
1886
                kfree(d->rcverr);
1887
                kfree(d);
1888
                return 0;
1889
        }
1890
        for (j = 0; j < n; j++) {
1891
                skb_queue_head_init(&d->rpqueue[j]);
1892
        }
1893
        if (!(d->rcv_waitq = (struct wait_queue **)
1894
              kmalloc(sizeof(struct wait_queue *) * n, GFP_KERNEL))) {
1895
                printk(KERN_WARNING "register_isdn: Could not alloc rcv_waitq\n");
1896
                kfree(d->rpqueue);
1897
                kfree(d->rcvcount);
1898
                kfree(d->rcverr);
1899
                kfree(d);
1900
                return 0;
1901
        }
1902
        memset((char *) d->rcv_waitq, 0, sizeof(struct wait_queue *) * n);
1903
        if (!(d->snd_waitq = (struct wait_queue **)
1904
              kmalloc(sizeof(struct wait_queue *) * n, GFP_KERNEL))) {
1905
                printk(KERN_WARNING "register_isdn: Could not alloc snd_waitq\n");
1906
                kfree(d->rcv_waitq);
1907
                kfree(d->rpqueue);
1908
                kfree(d->rcvcount);
1909
                kfree(d->rcverr);
1910
                kfree(d);
1911
                return 0;
1912
        }
1913
        memset((char *) d->snd_waitq, 0, sizeof(struct wait_queue *) * n);
1914
        d->channels = n;
1915
        d->maxbufsize = i->maxbufsize;
1916
        d->pktcount = 0;
1917
        d->stavail = 0;
1918
        d->flags = DRV_FLAG_LOADED;
1919
        d->online = 0;
1920
        d->interface = i;
1921
        for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++)
1922
                if (!dev->drv[drvidx])
1923
                        break;
1924
        i->channels = drvidx;
1925
 
1926
        i->rcvcallb_skb = isdn_receive_skb_callback;
1927
        i->rcvcallb = isdn_receive_callback;
1928
        i->statcallb = isdn_status_callback;
1929
        if (!strlen(i->id))
1930
                sprintf(i->id, "line%d", drvidx);
1931
        save_flags(flags);
1932
        cli();
1933
        for (j = 0; j < drvidx; j++)
1934
                if (!strcmp(i->id, dev->drvid[j]))
1935
                        sprintf(i->id, "line%d", drvidx);
1936
        for (j = 0; j < n; j++)
1937
                for (k = 0; k < ISDN_MAX_CHANNELS; k++)
1938
                        if (dev->chanmap[k] < 0) {
1939
                                dev->chanmap[k] = j;
1940
                                dev->drvmap[k] = drvidx;
1941
                                break;
1942
                        }
1943
        dev->drv[drvidx] = d;
1944
        dev->channels += n;
1945
        strcpy(dev->drvid[drvidx], i->id);
1946
        isdn_info_update();
1947
        dev->drivers++;
1948
        restore_flags(flags);
1949
        return 1;
1950
}
1951
 
1952
/*
1953
 *****************************************************************************
1954
 * And now the modules code.
1955
 *****************************************************************************
1956
 */
1957
 
1958
extern int printk(const char *fmt,...);
1959
 
1960
#ifdef MODULE
1961
#define isdn_init init_module
1962
#endif
1963
 
1964
static char *
1965
isdn_getrev(const char *revision)
1966
{
1967
        char *rev;
1968
        char *p;
1969
 
1970
        if ((p = strchr(revision, ':'))) {
1971
                rev = p + 2;
1972
                p = strchr(rev, '$');
1973
                *--p = 0;
1974
        } else
1975
                rev = "???";
1976
        return rev;
1977
}
1978
 
1979
/*
1980
 * Allocate and initialize all data, register modem-devices
1981
 */
1982
int
1983
isdn_init(void)
1984
{
1985
        int i;
1986
        char irev[50];
1987
        char trev[50];
1988
        char nrev[50];
1989
        char prev[50];
1990
        char arev[50];
1991
 
1992
        sti();
1993
        if (!(dev = (isdn_dev *) kmalloc(sizeof(isdn_dev), GFP_KERNEL))) {
1994
                printk(KERN_WARNING "isdn: Could not allocate device-struct.\n");
1995
                return -EIO;
1996
        }
1997
        memset((char *) dev, 0, sizeof(isdn_dev));
1998
        init_timer(&dev->timer);
1999
        dev->timer.function = isdn_timer_funct;
2000
        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2001
                dev->drvmap[i] = -1;
2002
                dev->chanmap[i] = -1;
2003
                dev->m_idx[i] = -1;
2004
                strcpy(dev->num[i], "???");
2005
        }
2006
        if (register_chrdev(ISDN_MAJOR, "isdn", &isdn_fops)) {
2007
                printk(KERN_WARNING "isdn: Could not register control devices\n");
2008
                kfree(dev);
2009
                return -EIO;
2010
        }
2011
        if ((i = isdn_tty_modem_init()) < 0) {
2012
                printk(KERN_WARNING "isdn: Could not register tty devices\n");
2013
                if (i == -3)
2014
                        tty_unregister_driver(&dev->mdm.cua_modem);
2015
                if (i <= -2)
2016
                        tty_unregister_driver(&dev->mdm.tty_modem);
2017
                kfree(dev);
2018
                unregister_chrdev(ISDN_MAJOR, "isdn");
2019
                return -EIO;
2020
        }
2021
#ifdef CONFIG_ISDN_PPP
2022
        if (isdn_ppp_init() < 0) {
2023
                printk(KERN_WARNING "isdn: Could not create PPP-device-structs\n");
2024
                tty_unregister_driver(&dev->mdm.tty_modem);
2025
                tty_unregister_driver(&dev->mdm.cua_modem);
2026
                for (i = 0; i < ISDN_MAX_CHANNELS; i++)
2027
                        kfree(dev->mdm.info[i].xmit_buf - 4);
2028
                unregister_chrdev(ISDN_MAJOR, "isdn");
2029
                kfree(dev);
2030
                return -EIO;
2031
        }
2032
#endif                          /* CONFIG_ISDN_PPP */
2033
 
2034
        isdn_export_syms();
2035
 
2036
        strcpy(irev, isdn_revision);
2037
        strcpy(trev, isdn_tty_revision);
2038
        strcpy(nrev, isdn_net_revision);
2039
        strcpy(prev, isdn_ppp_revision);
2040
        strcpy(arev, isdn_audio_revision);
2041
        printk(KERN_NOTICE "ISDN subsystem Rev: %s/", isdn_getrev(irev));
2042
        printk("%s/", isdn_getrev(trev));
2043
        printk("%s/", isdn_getrev(nrev));
2044
        printk("%s/", isdn_getrev(prev));
2045
        printk("%s", isdn_getrev(arev));
2046
 
2047
#ifdef MODULE
2048
        printk(" loaded\n");
2049
#else
2050
        printk("\n");
2051
        isdn_cards_init();
2052
#endif
2053
        isdn_info_update();
2054
        return 0;
2055
}
2056
 
2057
#ifdef MODULE
2058
/*
2059
 * Unload module
2060
 */
2061
void
2062
cleanup_module(void)
2063
{
2064
        int flags;
2065
        int i;
2066
 
2067
#ifdef CONFIG_ISDN_PPP
2068
        isdn_ppp_cleanup();
2069
#endif
2070
        save_flags(flags);
2071
        cli();
2072
        if (isdn_net_rmall() < 0) {
2073
                printk(KERN_WARNING "isdn: net-device busy, remove cancelled\n");
2074
                restore_flags(flags);
2075
                return;
2076
        }
2077
        if (tty_unregister_driver(&dev->mdm.tty_modem)) {
2078
                printk(KERN_WARNING "isdn: ttyI-device busy, remove cancelled\n");
2079
                restore_flags(flags);
2080
                return;
2081
        }
2082
        if (tty_unregister_driver(&dev->mdm.cua_modem)) {
2083
                printk(KERN_WARNING "isdn: cui-device busy, remove cancelled\n");
2084
                restore_flags(flags);
2085
                return;
2086
        }
2087
        for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2088
                isdn_tty_cleanup_xmit(&dev->mdm.info[i]);
2089
                kfree(dev->mdm.info[i].xmit_buf - 4);
2090
        }
2091
        if (unregister_chrdev(ISDN_MAJOR, "isdn") != 0) {
2092
                printk(KERN_WARNING "isdn: controldevice busy, remove cancelled\n");
2093
        } else {
2094
                del_timer(&dev->timer);
2095
                kfree(dev);
2096
                printk(KERN_NOTICE "ISDN-subsystem unloaded\n");
2097
        }
2098
        restore_flags(flags);
2099
}
2100
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.