]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/isdn/capi/capi.c
CAPI: Switch capiminor list to array
[net-next-2.6.git] / drivers / isdn / capi / capi.c
CommitLineData
1da177e4
LT
1/* $Id: capi.c,v 1.1.2.7 2004/04/28 09:48:59 armin Exp $
2 *
3 * CAPI 2.0 Interface for Linux
4 *
5 * Copyright 1996 by Carsten Paeth <calle@calle.de>
6 *
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
9 *
10 */
11
1da177e4
LT
12#include <linux/module.h>
13#include <linux/errno.h>
14#include <linux/kernel.h>
15#include <linux/major.h>
16#include <linux/sched.h>
17#include <linux/slab.h>
18#include <linux/fcntl.h>
19#include <linux/fs.h>
20#include <linux/signal.h>
9ea6e5d8 21#include <linux/mutex.h>
1da177e4 22#include <linux/mm.h>
a237f3bb 23#include <linux/smp_lock.h>
1da177e4
LT
24#include <linux/timer.h>
25#include <linux/wait.h>
1da177e4 26#include <linux/tty.h>
1da177e4
LT
27#include <linux/netdevice.h>
28#include <linux/ppp_defs.h>
29#include <linux/if_ppp.h>
1da177e4
LT
30#include <linux/skbuff.h>
31#include <linux/proc_fs.h>
9a58a80a 32#include <linux/seq_file.h>
1da177e4
LT
33#include <linux/poll.h>
34#include <linux/capi.h>
35#include <linux/kernelcapi.h>
36#include <linux/init.h>
37#include <linux/device.h>
38#include <linux/moduleparam.h>
1da177e4
LT
39#include <linux/isdn/capiutil.h>
40#include <linux/isdn/capicmd.h>
90926f0e 41
1da177e4 42#include "capifs.h"
1da177e4 43
1da177e4
LT
44MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
45MODULE_AUTHOR("Carsten Paeth");
46MODULE_LICENSE("GPL");
47
48#undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
49#undef _DEBUG_TTYFUNCS /* call to tty_driver */
50#undef _DEBUG_DATAFLOW /* data flow */
51
52/* -------- driver information -------------------------------------- */
53
56b22935 54static struct class *capi_class;
408b664a 55static int capi_major = 68; /* allocated */
501c87a9
JK
56
57module_param_named(major, capi_major, uint, 0);
58
1da177e4 59#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
501c87a9 60#define CAPINC_NR_PORTS 32
1da177e4 61#define CAPINC_MAX_PORTS 256
501c87a9 62
408b664a
AB
63static int capi_ttymajor = 191;
64static int capi_ttyminors = CAPINC_NR_PORTS;
1da177e4 65
1da177e4
LT
66module_param_named(ttymajor, capi_ttymajor, uint, 0);
67module_param_named(ttyminors, capi_ttyminors, uint, 0);
68#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
69
70/* -------- defines ------------------------------------------------- */
71
72#define CAPINC_MAX_RECVQUEUE 10
73#define CAPINC_MAX_SENDQUEUE 10
74#define CAPI_MAX_BLKSIZE 2048
75
76/* -------- data structures ----------------------------------------- */
77
78struct capidev;
79struct capincci;
1da177e4
LT
80struct capiminor;
81
6aa65472
MB
82struct datahandle_queue {
83 struct list_head list;
84 u16 datahandle;
85};
86
1da177e4 87struct capiminor {
1da177e4
LT
88 struct capincci *nccip;
89 unsigned int minor;
90926f0e 90 struct dentry *capifs_dentry;
1da177e4
LT
91
92 struct capi20_appl *ap;
93 u32 ncci;
94 u16 datahandle;
95 u16 msgid;
96
97 struct tty_struct *tty;
98 int ttyinstop;
99 int ttyoutstop;
100 struct sk_buff *ttyskb;
101 atomic_t ttyopencount;
102
103 struct sk_buff_head inqueue;
104 int inbytes;
105 struct sk_buff_head outqueue;
106 int outbytes;
107
108 /* transmit path */
6aa65472 109 struct list_head ackqueue;
1da177e4 110 int nack;
6aa65472 111 spinlock_t ackqlock;
1da177e4 112};
1da177e4 113
053b47ff
MB
114/* FIXME: The following lock is a sledgehammer-workaround to a
115 * locking issue with the capiminor (and maybe other) data structure(s).
116 * Access to this data is done in a racy way and crashes the machine with
117 * a FritzCard DSL driver; sooner or later. This is a workaround
118 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
119 * The correct (and scalable) fix for the issue seems to require
120 * an API change to the drivers... . */
121static DEFINE_SPINLOCK(workaround_lock);
122
1da177e4 123struct capincci {
884f5c44 124 struct list_head list;
1da177e4
LT
125 u32 ncci;
126 struct capidev *cdev;
127#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
128 struct capiminor *minorp;
129#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
130};
131
132struct capidev {
133 struct list_head list;
134 struct capi20_appl ap;
135 u16 errcode;
136 unsigned userflags;
137
138 struct sk_buff_head recvqueue;
139 wait_queue_head_t recvwait;
140
884f5c44 141 struct list_head nccis;
1da177e4 142
05b41494 143 struct mutex lock;
1da177e4
LT
144};
145
146/* -------- global variables ---------------------------------------- */
147
b8f433dc 148static DEFINE_MUTEX(capidev_list_lock);
1da177e4
LT
149static LIST_HEAD(capidev_list);
150
151#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
501c87a9 152
81d17fe5
JK
153static DEFINE_RWLOCK(capiminors_lock);
154static struct capiminor **capiminors;
1da177e4 155
1da177e4
LT
156/* -------- datahandles --------------------------------------------- */
157
501c87a9 158static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
1da177e4 159{
6aa65472
MB
160 struct datahandle_queue *n;
161 unsigned long flags;
1da177e4
LT
162
163 n = kmalloc(sizeof(*n), GFP_ATOMIC);
6aa65472
MB
164 if (unlikely(!n)) {
165 printk(KERN_ERR "capi: alloc datahandle failed\n");
166 return -1;
1da177e4 167 }
1da177e4 168 n->datahandle = datahandle;
6aa65472
MB
169 INIT_LIST_HEAD(&n->list);
170 spin_lock_irqsave(&mp->ackqlock, flags);
171 list_add_tail(&n->list, &mp->ackqueue);
1da177e4 172 mp->nack++;
6aa65472 173 spin_unlock_irqrestore(&mp->ackqlock, flags);
1da177e4
LT
174 return 0;
175}
176
177static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
178{
6aa65472
MB
179 struct datahandle_queue *p, *tmp;
180 unsigned long flags;
1da177e4 181
6aa65472
MB
182 spin_lock_irqsave(&mp->ackqlock, flags);
183 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
184 if (p->datahandle == datahandle) {
185 list_del(&p->list);
1da177e4
LT
186 kfree(p);
187 mp->nack--;
6aa65472 188 spin_unlock_irqrestore(&mp->ackqlock, flags);
1da177e4
LT
189 return 0;
190 }
191 }
6aa65472 192 spin_unlock_irqrestore(&mp->ackqlock, flags);
1da177e4
LT
193 return -1;
194}
195
196static void capiminor_del_all_ack(struct capiminor *mp)
197{
6aa65472
MB
198 struct datahandle_queue *p, *tmp;
199 unsigned long flags;
1da177e4 200
6aa65472
MB
201 spin_lock_irqsave(&mp->ackqlock, flags);
202 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
203 list_del(&p->list);
1da177e4
LT
204 kfree(p);
205 mp->nack--;
206 }
6aa65472 207 spin_unlock_irqrestore(&mp->ackqlock, flags);
1da177e4
LT
208}
209
210
211/* -------- struct capiminor ---------------------------------------- */
212
213static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
214{
81d17fe5
JK
215 struct capiminor *mp;
216 unsigned int minor;
1da177e4
LT
217 unsigned long flags;
218
54f0fad3 219 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
1da177e4
LT
220 if (!mp) {
221 printk(KERN_ERR "capi: can't alloc capiminor\n");
222 return NULL;
223 }
224
1da177e4
LT
225 mp->ap = ap;
226 mp->ncci = ncci;
227 mp->msgid = 0;
228 atomic_set(&mp->ttyopencount,0);
6aa65472
MB
229 INIT_LIST_HEAD(&mp->ackqueue);
230 spin_lock_init(&mp->ackqlock);
1da177e4
LT
231
232 skb_queue_head_init(&mp->inqueue);
233 skb_queue_head_init(&mp->outqueue);
234
81d17fe5
JK
235 /* Allocate the least unused minor number. */
236 write_lock_irqsave(&capiminors_lock, flags);
237 for (minor = 0; minor < capi_ttyminors; minor++)
238 if (!capiminors[minor]) {
239 capiminors[minor] = mp;
240 break;
1da177e4 241 }
81d17fe5 242 write_unlock_irqrestore(&capiminors_lock, flags);
1da177e4 243
81d17fe5 244 if (minor == capi_ttyminors) {
1da177e4 245 printk(KERN_NOTICE "capi: out of minors\n");
81d17fe5 246 kfree(mp);
1da177e4
LT
247 return NULL;
248 }
249
81d17fe5
JK
250 mp->minor = minor;
251
1da177e4
LT
252 return mp;
253}
254
255static void capiminor_free(struct capiminor *mp)
256{
257 unsigned long flags;
258
81d17fe5
JK
259 write_lock_irqsave(&capiminors_lock, flags);
260 capiminors[mp->minor] = NULL;
261 write_unlock_irqrestore(&capiminors_lock, flags);
1da177e4 262
d46604e1 263 kfree_skb(mp->ttyskb);
1da177e4
LT
264 mp->ttyskb = NULL;
265 skb_queue_purge(&mp->inqueue);
266 skb_queue_purge(&mp->outqueue);
267 capiminor_del_all_ack(mp);
268 kfree(mp);
269}
270
408b664a 271static struct capiminor *capiminor_find(unsigned int minor)
1da177e4 272{
81d17fe5 273 struct capiminor *mp;
1da177e4 274
81d17fe5 275 if (minor >= capi_ttyminors)
1da177e4
LT
276 return NULL;
277
81d17fe5
JK
278 read_lock(&capiminors_lock);
279 mp = capiminors[minor];
280 read_unlock(&capiminors_lock);
281
282 return mp;
1da177e4 283}
1da177e4
LT
284
285/* -------- struct capincci ----------------------------------------- */
286
501c87a9 287static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
1da177e4 288{
501c87a9 289 struct capiminor *mp;
1da177e4 290
501c87a9
JK
291 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
292 return;
293
294 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
1da177e4
LT
295 if (mp) {
296 mp->nccip = np;
297#ifdef _DEBUG_REFCOUNT
298 printk(KERN_DEBUG "set mp->nccip\n");
299#endif
90926f0e
JK
300 mp->capifs_dentry =
301 capifs_new_ncci(mp->minor,
302 MKDEV(capi_ttymajor, mp->minor));
1da177e4 303 }
501c87a9
JK
304}
305
306static void capincci_free_minor(struct capincci *np)
307{
308 struct capiminor *mp = np->minorp;
309
310 if (mp) {
311 capifs_free_ncci(mp->capifs_dentry);
312 if (mp->tty) {
313 mp->nccip = NULL;
314#ifdef _DEBUG_REFCOUNT
315 printk(KERN_DEBUG "reset mp->nccip\n");
316#endif
317 tty_hangup(mp->tty);
318 } else {
319 capiminor_free(mp);
320 }
321 }
322}
323
324static inline unsigned int capincci_minor_opencount(struct capincci *np)
325{
326 struct capiminor *mp = np->minorp;
327
328 return mp ? atomic_read(&mp->ttyopencount) : 0;
329}
330
331#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
332
333static inline void
334capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
335static inline void capincci_free_minor(struct capincci *np) { }
336
337static inline unsigned int capincci_minor_opencount(struct capincci *np)
338{
339 return 0;
340}
341
342#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
343
344static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
345{
884f5c44 346 struct capincci *np;
501c87a9 347
54f0fad3 348 np = kzalloc(sizeof(*np), GFP_KERNEL);
501c87a9
JK
349 if (!np)
350 return NULL;
351 np->ncci = ncci;
352 np->cdev = cdev;
353
354 capincci_alloc_minor(cdev, np);
355
884f5c44
JK
356 list_add_tail(&np->list, &cdev->nccis);
357
358 return np;
1da177e4
LT
359}
360
361static void capincci_free(struct capidev *cdev, u32 ncci)
362{
884f5c44 363 struct capincci *np, *tmp;
1da177e4 364
884f5c44 365 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
1da177e4 366 if (ncci == 0xffffffff || np->ncci == ncci) {
501c87a9 367 capincci_free_minor(np);
884f5c44 368 list_del(&np->list);
1da177e4 369 kfree(np);
1da177e4 370 }
1da177e4
LT
371}
372
373static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
374{
884f5c44 375 struct capincci *np;
1da177e4 376
884f5c44
JK
377 list_for_each_entry(np, &cdev->nccis, list)
378 if (np->ncci == ncci)
379 return np;
380 return NULL;
1da177e4
LT
381}
382
1da177e4
LT
383#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
384/* -------- handle data queue --------------------------------------- */
385
386static struct sk_buff *
387gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
388{
389 struct sk_buff *nskb;
390 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
391 if (nskb) {
392 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
393 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
394 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
395 capimsg_setu16(s, 2, mp->ap->applid);
396 capimsg_setu8 (s, 4, CAPI_DATA_B3);
397 capimsg_setu8 (s, 5, CAPI_RESP);
398 capimsg_setu16(s, 6, mp->msgid++);
399 capimsg_setu32(s, 8, mp->ncci);
400 capimsg_setu16(s, 12, datahandle);
401 }
402 return nskb;
403}
404
405static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
406{
407 struct sk_buff *nskb;
408 int datalen;
409 u16 errcode, datahandle;
410 struct tty_ldisc *ld;
411
412 datalen = skb->len - CAPIMSG_LEN(skb->data);
413 if (mp->tty == NULL)
414 {
415#ifdef _DEBUG_DATAFLOW
416 printk(KERN_DEBUG "capi: currently no receiver\n");
417#endif
418 return -1;
419 }
420
421 ld = tty_ldisc_ref(mp->tty);
422 if (ld == NULL)
423 return -1;
a352def2 424 if (ld->ops->receive_buf == NULL) {
1da177e4
LT
425#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
426 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
427#endif
428 goto bad;
429 }
430 if (mp->ttyinstop) {
431#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
432 printk(KERN_DEBUG "capi: recv tty throttled\n");
433#endif
434 goto bad;
435 }
33f0f88f 436 if (mp->tty->receive_room < datalen) {
1da177e4
LT
437#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
438 printk(KERN_DEBUG "capi: no room in tty\n");
439#endif
440 goto bad;
441 }
2f9e9b6d 442 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
1da177e4
LT
443 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
444 goto bad;
445 }
446 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
447 errcode = capi20_put_message(mp->ap, nskb);
448 if (errcode != CAPI_NOERROR) {
449 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
450 errcode);
451 kfree_skb(nskb);
452 goto bad;
453 }
454 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
455#ifdef _DEBUG_DATAFLOW
456 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
457 datahandle, skb->len);
458#endif
a352def2 459 ld->ops->receive_buf(mp->tty, skb->data, NULL, skb->len);
1da177e4
LT
460 kfree_skb(skb);
461 tty_ldisc_deref(ld);
462 return 0;
463bad:
464 tty_ldisc_deref(ld);
465 return -1;
466}
467
468static void handle_minor_recv(struct capiminor *mp)
469{
470 struct sk_buff *skb;
2f9e9b6d 471 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
1da177e4
LT
472 unsigned int len = skb->len;
473 mp->inbytes -= len;
474 if (handle_recv_skb(mp, skb) < 0) {
475 skb_queue_head(&mp->inqueue, skb);
476 mp->inbytes += len;
477 return;
478 }
479 }
480}
481
482static int handle_minor_send(struct capiminor *mp)
483{
484 struct sk_buff *skb;
485 u16 len;
486 int count = 0;
487 u16 errcode;
488 u16 datahandle;
489
490 if (mp->tty && mp->ttyoutstop) {
491#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
492 printk(KERN_DEBUG "capi: send: tty stopped\n");
493#endif
494 return 0;
495 }
496
2f9e9b6d 497 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
1da177e4
LT
498 datahandle = mp->datahandle;
499 len = (u16)skb->len;
500 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
501 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
502 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
503 capimsg_setu16(skb->data, 2, mp->ap->applid);
504 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
505 capimsg_setu8 (skb->data, 5, CAPI_REQ);
506 capimsg_setu16(skb->data, 6, mp->msgid++);
507 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
5e6c20a9 508 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
1da177e4
LT
509 capimsg_setu16(skb->data, 16, len); /* Data length */
510 capimsg_setu16(skb->data, 18, datahandle);
511 capimsg_setu16(skb->data, 20, 0); /* Flags */
512
501c87a9 513 if (capiminor_add_ack(mp, datahandle) < 0) {
1da177e4
LT
514 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
515 skb_queue_head(&mp->outqueue, skb);
516 return count;
517 }
518 errcode = capi20_put_message(mp->ap, skb);
519 if (errcode == CAPI_NOERROR) {
520 mp->datahandle++;
521 count++;
522 mp->outbytes -= len;
523#ifdef _DEBUG_DATAFLOW
524 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
525 datahandle, len);
526#endif
527 continue;
528 }
529 capiminor_del_ack(mp, datahandle);
530
531 if (errcode == CAPI_SENDQUEUEFULL) {
532 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
533 skb_queue_head(&mp->outqueue, skb);
534 break;
535 }
536
537 /* ups, drop packet */
538 printk(KERN_ERR "capi: put_message = %x\n", errcode);
539 mp->outbytes -= len;
540 kfree_skb(skb);
541 }
542 return count;
543}
544
545#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
546/* -------- function called by lower level -------------------------- */
547
548static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
549{
550 struct capidev *cdev = ap->private;
551#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
552 struct capiminor *mp;
553 u16 datahandle;
554#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
555 struct capincci *np;
053b47ff 556 unsigned long flags;
1da177e4 557
05b41494
JK
558 mutex_lock(&cdev->lock);
559
1da177e4
LT
560 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
561 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
05b41494 562 if ((info & 0xff00) == 0)
1da177e4 563 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
1da177e4 564 }
05b41494 565 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
1da177e4 566 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
05b41494 567
053b47ff 568 spin_lock_irqsave(&workaround_lock, flags);
1da177e4
LT
569 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
570 skb_queue_tail(&cdev->recvqueue, skb);
571 wake_up_interruptible(&cdev->recvwait);
05b41494 572 goto unlock_out;
1da177e4 573 }
05b41494
JK
574
575 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
1da177e4
LT
576 if (!np) {
577 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
578 skb_queue_tail(&cdev->recvqueue, skb);
579 wake_up_interruptible(&cdev->recvwait);
05b41494 580 goto unlock_out;
1da177e4 581 }
501c87a9 582
1da177e4
LT
583#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
584 skb_queue_tail(&cdev->recvqueue, skb);
585 wake_up_interruptible(&cdev->recvwait);
501c87a9 586
1da177e4 587#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
501c87a9 588
1da177e4
LT
589 mp = np->minorp;
590 if (!mp) {
591 skb_queue_tail(&cdev->recvqueue, skb);
592 wake_up_interruptible(&cdev->recvwait);
05b41494 593 goto unlock_out;
1da177e4 594 }
1da177e4 595 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
1da177e4
LT
596 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
597#ifdef _DEBUG_DATAFLOW
598 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
599 datahandle, skb->len-CAPIMSG_LEN(skb->data));
600#endif
601 skb_queue_tail(&mp->inqueue, skb);
602 mp->inbytes += skb->len;
603 handle_minor_recv(mp);
604
605 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
606
607 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
608#ifdef _DEBUG_DATAFLOW
609 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
610 datahandle,
611 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
612#endif
613 kfree_skb(skb);
614 (void)capiminor_del_ack(mp, datahandle);
615 if (mp->tty)
616 tty_wakeup(mp->tty);
617 (void)handle_minor_send(mp);
618
619 } else {
620 /* ups, let capi application handle it :-) */
621 skb_queue_tail(&cdev->recvqueue, skb);
622 wake_up_interruptible(&cdev->recvwait);
623 }
624#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
501c87a9 625
05b41494 626unlock_out:
053b47ff 627 spin_unlock_irqrestore(&workaround_lock, flags);
05b41494 628 mutex_unlock(&cdev->lock);
1da177e4
LT
629}
630
631/* -------- file_operations for capidev ----------------------------- */
632
633static ssize_t
634capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
635{
636 struct capidev *cdev = (struct capidev *)file->private_data;
637 struct sk_buff *skb;
638 size_t copied;
28a1dbb6 639 int err;
1da177e4
LT
640
641 if (!cdev->ap.applid)
642 return -ENODEV;
643
28a1dbb6
JK
644 skb = skb_dequeue(&cdev->recvqueue);
645 if (!skb) {
1da177e4
LT
646 if (file->f_flags & O_NONBLOCK)
647 return -EAGAIN;
28a1dbb6
JK
648 err = wait_event_interruptible(cdev->recvwait,
649 (skb = skb_dequeue(&cdev->recvqueue)));
650 if (err)
651 return err;
1da177e4
LT
652 }
653 if (skb->len > count) {
654 skb_queue_head(&cdev->recvqueue, skb);
655 return -EMSGSIZE;
656 }
657 if (copy_to_user(buf, skb->data, skb->len)) {
658 skb_queue_head(&cdev->recvqueue, skb);
659 return -EFAULT;
660 }
661 copied = skb->len;
662
663 kfree_skb(skb);
664
665 return copied;
666}
667
668static ssize_t
669capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
670{
671 struct capidev *cdev = (struct capidev *)file->private_data;
672 struct sk_buff *skb;
673 u16 mlen;
674
675 if (!cdev->ap.applid)
676 return -ENODEV;
677
678 skb = alloc_skb(count, GFP_USER);
679 if (!skb)
680 return -ENOMEM;
681
682 if (copy_from_user(skb_put(skb, count), buf, count)) {
683 kfree_skb(skb);
684 return -EFAULT;
685 }
686 mlen = CAPIMSG_LEN(skb->data);
687 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
688 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
689 kfree_skb(skb);
690 return -EINVAL;
691 }
692 } else {
693 if (mlen != count) {
694 kfree_skb(skb);
695 return -EINVAL;
696 }
697 }
698 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
699
700 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
05b41494 701 mutex_lock(&cdev->lock);
1da177e4 702 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
05b41494 703 mutex_unlock(&cdev->lock);
1da177e4
LT
704 }
705
706 cdev->errcode = capi20_put_message(&cdev->ap, skb);
707
708 if (cdev->errcode) {
709 kfree_skb(skb);
710 return -EIO;
711 }
712 return count;
713}
714
715static unsigned int
716capi_poll(struct file *file, poll_table * wait)
717{
718 struct capidev *cdev = (struct capidev *)file->private_data;
719 unsigned int mask = 0;
720
721 if (!cdev->ap.applid)
722 return POLLERR;
723
724 poll_wait(file, &(cdev->recvwait), wait);
725 mask = POLLOUT | POLLWRNORM;
726 if (!skb_queue_empty(&cdev->recvqueue))
727 mask |= POLLIN | POLLRDNORM;
728 return mask;
729}
730
731static int
732capi_ioctl(struct inode *inode, struct file *file,
733 unsigned int cmd, unsigned long arg)
734{
735 struct capidev *cdev = file->private_data;
1da177e4
LT
736 capi_ioctl_struct data;
737 int retval = -EINVAL;
738 void __user *argp = (void __user *)arg;
739
740 switch (cmd) {
741 case CAPI_REGISTER:
05b41494 742 mutex_lock(&cdev->lock);
1da177e4 743
05b41494
JK
744 if (cdev->ap.applid) {
745 retval = -EEXIST;
746 goto register_out;
747 }
748 if (copy_from_user(&cdev->ap.rparam, argp,
749 sizeof(struct capi_register_params))) {
750 retval = -EFAULT;
751 goto register_out;
752 }
753 cdev->ap.private = cdev;
754 cdev->ap.recv_message = capi_recv_message;
755 cdev->errcode = capi20_register(&cdev->ap);
756 retval = (int)cdev->ap.applid;
757 if (cdev->errcode) {
758 cdev->ap.applid = 0;
759 retval = -EIO;
1da177e4 760 }
05b41494
JK
761
762register_out:
763 mutex_unlock(&cdev->lock);
764 return retval;
1da177e4
LT
765
766 case CAPI_GET_VERSION:
767 {
768 if (copy_from_user(&data.contr, argp,
769 sizeof(data.contr)))
770 return -EFAULT;
771 cdev->errcode = capi20_get_version(data.contr, &data.version);
772 if (cdev->errcode)
773 return -EIO;
774 if (copy_to_user(argp, &data.version,
775 sizeof(data.version)))
776 return -EFAULT;
777 }
778 return 0;
779
780 case CAPI_GET_SERIAL:
781 {
782 if (copy_from_user(&data.contr, argp,
783 sizeof(data.contr)))
784 return -EFAULT;
785 cdev->errcode = capi20_get_serial (data.contr, data.serial);
786 if (cdev->errcode)
787 return -EIO;
788 if (copy_to_user(argp, data.serial,
789 sizeof(data.serial)))
790 return -EFAULT;
791 }
792 return 0;
793 case CAPI_GET_PROFILE:
794 {
795 if (copy_from_user(&data.contr, argp,
796 sizeof(data.contr)))
797 return -EFAULT;
798
799 if (data.contr == 0) {
800 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
801 if (cdev->errcode)
802 return -EIO;
803
804 retval = copy_to_user(argp,
805 &data.profile.ncontroller,
806 sizeof(data.profile.ncontroller));
807
808 } else {
809 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
810 if (cdev->errcode)
811 return -EIO;
812
813 retval = copy_to_user(argp, &data.profile,
814 sizeof(data.profile));
815 }
816 if (retval)
817 return -EFAULT;
818 }
819 return 0;
820
821 case CAPI_GET_MANUFACTURER:
822 {
823 if (copy_from_user(&data.contr, argp,
824 sizeof(data.contr)))
825 return -EFAULT;
826 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
827 if (cdev->errcode)
828 return -EIO;
829
830 if (copy_to_user(argp, data.manufacturer,
831 sizeof(data.manufacturer)))
832 return -EFAULT;
833
834 }
835 return 0;
836 case CAPI_GET_ERRCODE:
837 data.errcode = cdev->errcode;
838 cdev->errcode = CAPI_NOERROR;
839 if (arg) {
840 if (copy_to_user(argp, &data.errcode,
841 sizeof(data.errcode)))
842 return -EFAULT;
843 }
844 return data.errcode;
845
846 case CAPI_INSTALLED:
847 if (capi20_isinstalled() == CAPI_NOERROR)
848 return 0;
849 return -ENXIO;
850
851 case CAPI_MANUFACTURER_CMD:
852 {
853 struct capi_manufacturer_cmd mcmd;
854 if (!capable(CAP_SYS_ADMIN))
855 return -EPERM;
856 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
857 return -EFAULT;
858 return capi20_manufacturer(mcmd.cmd, mcmd.data);
859 }
860 return 0;
861
862 case CAPI_SET_FLAGS:
05b41494
JK
863 case CAPI_CLR_FLAGS: {
864 unsigned userflags;
865
866 if (copy_from_user(&userflags, argp, sizeof(userflags)))
867 return -EFAULT;
1da177e4 868
05b41494
JK
869 mutex_lock(&cdev->lock);
870 if (cmd == CAPI_SET_FLAGS)
871 cdev->userflags |= userflags;
872 else
873 cdev->userflags &= ~userflags;
874 mutex_unlock(&cdev->lock);
875 return 0;
876 }
1da177e4
LT
877 case CAPI_GET_FLAGS:
878 if (copy_to_user(argp, &cdev->userflags,
879 sizeof(cdev->userflags)))
880 return -EFAULT;
881 return 0;
882
05b41494
JK
883 case CAPI_NCCI_OPENCOUNT: {
884 struct capincci *nccip;
885 unsigned ncci;
886 int count = 0;
1da177e4 887
05b41494
JK
888 if (copy_from_user(&ncci, argp, sizeof(ncci)))
889 return -EFAULT;
890
891 mutex_lock(&cdev->lock);
892 nccip = capincci_find(cdev, (u32)ncci);
893 if (nccip)
894 count = capincci_minor_opencount(nccip);
895 mutex_unlock(&cdev->lock);
896 return count;
897 }
1da177e4
LT
898
899#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
05b41494
JK
900 case CAPI_NCCI_GETUNIT: {
901 struct capincci *nccip;
902 struct capiminor *mp;
903 unsigned ncci;
904 int unit = -ESRCH;
905
906 if (copy_from_user(&ncci, argp, sizeof(ncci)))
907 return -EFAULT;
908
909 mutex_lock(&cdev->lock);
910 nccip = capincci_find(cdev, (u32)ncci);
911 if (nccip) {
912 mp = nccip->minorp;
913 if (mp)
914 unit = mp->minor;
1da177e4 915 }
05b41494
JK
916 mutex_unlock(&cdev->lock);
917 return unit;
918 }
1da177e4 919#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
05b41494
JK
920
921 default:
922 return -EINVAL;
1da177e4 923 }
1da177e4
LT
924}
925
eca39dd8 926static int capi_open(struct inode *inode, struct file *file)
1da177e4 927{
eca39dd8
JK
928 struct capidev *cdev;
929
930 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
931 if (!cdev)
932 return -ENOMEM;
933
05b41494 934 mutex_init(&cdev->lock);
eca39dd8
JK
935 skb_queue_head_init(&cdev->recvqueue);
936 init_waitqueue_head(&cdev->recvwait);
884f5c44 937 INIT_LIST_HEAD(&cdev->nccis);
eca39dd8
JK
938 file->private_data = cdev;
939
940 mutex_lock(&capidev_list_lock);
941 list_add_tail(&cdev->list, &capidev_list);
942 mutex_unlock(&capidev_list_lock);
943
944 return nonseekable_open(inode, file);
1da177e4
LT
945}
946
eca39dd8 947static int capi_release(struct inode *inode, struct file *file)
1da177e4 948{
eca39dd8 949 struct capidev *cdev = file->private_data;
1da177e4 950
eca39dd8
JK
951 mutex_lock(&capidev_list_lock);
952 list_del(&cdev->list);
953 mutex_unlock(&capidev_list_lock);
954
05b41494 955 if (cdev->ap.applid)
eca39dd8 956 capi20_release(&cdev->ap);
eca39dd8 957 skb_queue_purge(&cdev->recvqueue);
eca39dd8 958 capincci_free(cdev, 0xffffffff);
eca39dd8
JK
959
960 kfree(cdev);
1da177e4
LT
961 return 0;
962}
963
2b8693c0 964static const struct file_operations capi_fops =
1da177e4
LT
965{
966 .owner = THIS_MODULE,
967 .llseek = no_llseek,
968 .read = capi_read,
969 .write = capi_write,
970 .poll = capi_poll,
971 .ioctl = capi_ioctl,
972 .open = capi_open,
973 .release = capi_release,
974};
975
976#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
977/* -------- tty_operations for capincci ----------------------------- */
978
979static int capinc_tty_open(struct tty_struct * tty, struct file * file)
980{
981 struct capiminor *mp;
053b47ff 982 unsigned long flags;
1da177e4 983
2f9e9b6d 984 if ((mp = capiminor_find(iminor(file->f_path.dentry->d_inode))) == NULL)
1da177e4 985 return -ENXIO;
2f9e9b6d 986 if (mp->nccip == NULL)
1da177e4
LT
987 return -ENXIO;
988
989 tty->driver_data = (void *)mp;
990
053b47ff 991 spin_lock_irqsave(&workaround_lock, flags);
1da177e4
LT
992 if (atomic_read(&mp->ttyopencount) == 0)
993 mp->tty = tty;
994 atomic_inc(&mp->ttyopencount);
995#ifdef _DEBUG_REFCOUNT
996 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
997#endif
998 handle_minor_recv(mp);
053b47ff 999 spin_unlock_irqrestore(&workaround_lock, flags);
1da177e4
LT
1000 return 0;
1001}
1002
1003static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1004{
1005 struct capiminor *mp;
1006
1007 mp = (struct capiminor *)tty->driver_data;
1008 if (mp) {
1009 if (atomic_dec_and_test(&mp->ttyopencount)) {
1010#ifdef _DEBUG_REFCOUNT
1011 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1012#endif
1013 tty->driver_data = NULL;
1014 mp->tty = NULL;
1015 }
1016#ifdef _DEBUG_REFCOUNT
1017 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1018#endif
2f9e9b6d 1019 if (mp->nccip == NULL)
1da177e4
LT
1020 capiminor_free(mp);
1021 }
1022
1023#ifdef _DEBUG_REFCOUNT
1024 printk(KERN_DEBUG "capinc_tty_close\n");
1025#endif
1026}
1027
1028static int capinc_tty_write(struct tty_struct * tty,
1029 const unsigned char *buf, int count)
1030{
1031 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1032 struct sk_buff *skb;
053b47ff 1033 unsigned long flags;
1da177e4
LT
1034
1035#ifdef _DEBUG_TTYFUNCS
1036 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1037#endif
1038
1039 if (!mp || !mp->nccip) {
1040#ifdef _DEBUG_TTYFUNCS
1041 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1042#endif
1043 return 0;
1044 }
1045
053b47ff 1046 spin_lock_irqsave(&workaround_lock, flags);
1da177e4
LT
1047 skb = mp->ttyskb;
1048 if (skb) {
1049 mp->ttyskb = NULL;
1050 skb_queue_tail(&mp->outqueue, skb);
1051 mp->outbytes += skb->len;
1052 }
1053
1054 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1055 if (!skb) {
1056 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
053b47ff 1057 spin_unlock_irqrestore(&workaround_lock, flags);
1da177e4
LT
1058 return -ENOMEM;
1059 }
1060
1061 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1062 memcpy(skb_put(skb, count), buf, count);
1063
1064 skb_queue_tail(&mp->outqueue, skb);
1065 mp->outbytes += skb->len;
1066 (void)handle_minor_send(mp);
1067 (void)handle_minor_recv(mp);
053b47ff 1068 spin_unlock_irqrestore(&workaround_lock, flags);
1da177e4
LT
1069 return count;
1070}
1071
f2545a75 1072static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1da177e4
LT
1073{
1074 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1075 struct sk_buff *skb;
053b47ff 1076 unsigned long flags;
f2545a75 1077 int ret = 1;
1da177e4
LT
1078
1079#ifdef _DEBUG_TTYFUNCS
1080 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1081#endif
1082
1083 if (!mp || !mp->nccip) {
1084#ifdef _DEBUG_TTYFUNCS
1085 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1086#endif
f2545a75 1087 return 0;
1da177e4
LT
1088 }
1089
053b47ff 1090 spin_lock_irqsave(&workaround_lock, flags);
1da177e4
LT
1091 skb = mp->ttyskb;
1092 if (skb) {
1093 if (skb_tailroom(skb) > 0) {
1094 *(skb_put(skb, 1)) = ch;
053b47ff 1095 spin_unlock_irqrestore(&workaround_lock, flags);
f2545a75 1096 return 1;
1da177e4
LT
1097 }
1098 mp->ttyskb = NULL;
1099 skb_queue_tail(&mp->outqueue, skb);
1100 mp->outbytes += skb->len;
1101 (void)handle_minor_send(mp);
1102 }
1103 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1104 if (skb) {
1105 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1106 *(skb_put(skb, 1)) = ch;
1107 mp->ttyskb = skb;
1108 } else {
1109 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
f2545a75 1110 ret = 0;
1da177e4 1111 }
053b47ff 1112 spin_unlock_irqrestore(&workaround_lock, flags);
f2545a75 1113 return ret;
1da177e4
LT
1114}
1115
1116static void capinc_tty_flush_chars(struct tty_struct *tty)
1117{
1118 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1119 struct sk_buff *skb;
053b47ff 1120 unsigned long flags;
1da177e4
LT
1121
1122#ifdef _DEBUG_TTYFUNCS
1123 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1124#endif
1125
1126 if (!mp || !mp->nccip) {
1127#ifdef _DEBUG_TTYFUNCS
1128 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1129#endif
1130 return;
1131 }
1132
053b47ff 1133 spin_lock_irqsave(&workaround_lock, flags);
1da177e4
LT
1134 skb = mp->ttyskb;
1135 if (skb) {
1136 mp->ttyskb = NULL;
1137 skb_queue_tail(&mp->outqueue, skb);
1138 mp->outbytes += skb->len;
1139 (void)handle_minor_send(mp);
1140 }
1141 (void)handle_minor_recv(mp);
053b47ff 1142 spin_unlock_irqrestore(&workaround_lock, flags);
1da177e4
LT
1143}
1144
1145static int capinc_tty_write_room(struct tty_struct *tty)
1146{
1147 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1148 int room;
1149 if (!mp || !mp->nccip) {
1150#ifdef _DEBUG_TTYFUNCS
1151 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1152#endif
1153 return 0;
1154 }
1155 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1156 room *= CAPI_MAX_BLKSIZE;
1157#ifdef _DEBUG_TTYFUNCS
1158 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1159#endif
1160 return room;
1161}
1162
408b664a 1163static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1da177e4
LT
1164{
1165 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1166 if (!mp || !mp->nccip) {
1167#ifdef _DEBUG_TTYFUNCS
1168 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1169#endif
1170 return 0;
1171 }
1172#ifdef _DEBUG_TTYFUNCS
1173 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1174 mp->outbytes, mp->nack,
1175 skb_queue_len(&mp->outqueue),
1176 skb_queue_len(&mp->inqueue));
1177#endif
1178 return mp->outbytes;
1179}
1180
1181static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1182 unsigned int cmd, unsigned long arg)
1183{
1184 int error = 0;
1185 switch (cmd) {
1186 default:
53e86317 1187 error = n_tty_ioctl_helper(tty, file, cmd, arg);
1da177e4
LT
1188 break;
1189 }
1190 return error;
1191}
1192
606d099c 1193static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1da177e4
LT
1194{
1195#ifdef _DEBUG_TTYFUNCS
1196 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1197#endif
1198}
1199
1200static void capinc_tty_throttle(struct tty_struct * tty)
1201{
1202 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1203#ifdef _DEBUG_TTYFUNCS
1204 printk(KERN_DEBUG "capinc_tty_throttle\n");
1205#endif
1206 if (mp)
1207 mp->ttyinstop = 1;
1208}
1209
1210static void capinc_tty_unthrottle(struct tty_struct * tty)
1211{
1212 struct capiminor *mp = (struct capiminor *)tty->driver_data;
053b47ff 1213 unsigned long flags;
1da177e4
LT
1214#ifdef _DEBUG_TTYFUNCS
1215 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1216#endif
1217 if (mp) {
053b47ff 1218 spin_lock_irqsave(&workaround_lock, flags);
1da177e4
LT
1219 mp->ttyinstop = 0;
1220 handle_minor_recv(mp);
053b47ff 1221 spin_unlock_irqrestore(&workaround_lock, flags);
1da177e4
LT
1222 }
1223}
1224
1225static void capinc_tty_stop(struct tty_struct *tty)
1226{
1227 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1228#ifdef _DEBUG_TTYFUNCS
1229 printk(KERN_DEBUG "capinc_tty_stop\n");
1230#endif
1231 if (mp) {
1232 mp->ttyoutstop = 1;
1233 }
1234}
1235
1236static void capinc_tty_start(struct tty_struct *tty)
1237{
1238 struct capiminor *mp = (struct capiminor *)tty->driver_data;
053b47ff 1239 unsigned long flags;
1da177e4
LT
1240#ifdef _DEBUG_TTYFUNCS
1241 printk(KERN_DEBUG "capinc_tty_start\n");
1242#endif
1243 if (mp) {
053b47ff 1244 spin_lock_irqsave(&workaround_lock, flags);
1da177e4
LT
1245 mp->ttyoutstop = 0;
1246 (void)handle_minor_send(mp);
053b47ff 1247 spin_unlock_irqrestore(&workaround_lock, flags);
1da177e4
LT
1248 }
1249}
1250
1251static void capinc_tty_hangup(struct tty_struct *tty)
1252{
1253#ifdef _DEBUG_TTYFUNCS
1254 printk(KERN_DEBUG "capinc_tty_hangup\n");
1255#endif
1256}
1257
9e98966c 1258static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1da177e4
LT
1259{
1260#ifdef _DEBUG_TTYFUNCS
1261 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1262#endif
9e98966c 1263 return 0;
1da177e4
LT
1264}
1265
1266static void capinc_tty_flush_buffer(struct tty_struct *tty)
1267{
1268#ifdef _DEBUG_TTYFUNCS
1269 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1270#endif
1271}
1272
1273static void capinc_tty_set_ldisc(struct tty_struct *tty)
1274{
1275#ifdef _DEBUG_TTYFUNCS
1276 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1277#endif
1278}
1279
1280static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1281{
1282#ifdef _DEBUG_TTYFUNCS
1283 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1284#endif
1285}
1286
1da177e4
LT
1287static struct tty_driver *capinc_tty_driver;
1288
b68e31d0 1289static const struct tty_operations capinc_ops = {
1da177e4
LT
1290 .open = capinc_tty_open,
1291 .close = capinc_tty_close,
1292 .write = capinc_tty_write,
1293 .put_char = capinc_tty_put_char,
1294 .flush_chars = capinc_tty_flush_chars,
1295 .write_room = capinc_tty_write_room,
1296 .chars_in_buffer = capinc_tty_chars_in_buffer,
1297 .ioctl = capinc_tty_ioctl,
1298 .set_termios = capinc_tty_set_termios,
1299 .throttle = capinc_tty_throttle,
1300 .unthrottle = capinc_tty_unthrottle,
1301 .stop = capinc_tty_stop,
1302 .start = capinc_tty_start,
1303 .hangup = capinc_tty_hangup,
1304 .break_ctl = capinc_tty_break_ctl,
1305 .flush_buffer = capinc_tty_flush_buffer,
1306 .set_ldisc = capinc_tty_set_ldisc,
1307 .send_xchar = capinc_tty_send_xchar,
1da177e4
LT
1308};
1309
1310static int capinc_tty_init(void)
1311{
1312 struct tty_driver *drv;
1313
1314 if (capi_ttyminors > CAPINC_MAX_PORTS)
1315 capi_ttyminors = CAPINC_MAX_PORTS;
1316 if (capi_ttyminors <= 0)
1317 capi_ttyminors = CAPINC_NR_PORTS;
1318
81d17fe5
JK
1319 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1320 GFP_KERNEL);
1321 if (!capiminors)
1da177e4
LT
1322 return -ENOMEM;
1323
81d17fe5
JK
1324 drv = alloc_tty_driver(capi_ttyminors);
1325 if (!drv) {
1326 kfree(capiminors);
1327 return -ENOMEM;
1328 }
1da177e4
LT
1329 drv->owner = THIS_MODULE;
1330 drv->driver_name = "capi_nc";
1da177e4
LT
1331 drv->name = "capi";
1332 drv->major = capi_ttymajor;
1333 drv->minor_start = 0;
1334 drv->type = TTY_DRIVER_TYPE_SERIAL;
1335 drv->subtype = SERIAL_TYPE_NORMAL;
1336 drv->init_termios = tty_std_termios;
1337 drv->init_termios.c_iflag = ICRNL;
1338 drv->init_termios.c_oflag = OPOST | ONLCR;
1339 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1340 drv->init_termios.c_lflag = 0;
1341 drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS;
1342 tty_set_operations(drv, &capinc_ops);
1343 if (tty_register_driver(drv)) {
1344 put_tty_driver(drv);
81d17fe5 1345 kfree(capiminors);
1da177e4
LT
1346 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1347 return -1;
1348 }
1349 capinc_tty_driver = drv;
1350 return 0;
1351}
1352
1353static void capinc_tty_exit(void)
1354{
1355 struct tty_driver *drv = capinc_tty_driver;
1356 int retval;
1357 if ((retval = tty_unregister_driver(drv)))
1358 printk(KERN_ERR "capi: failed to unregister capi_nc driver (%d)\n", retval);
1359 put_tty_driver(drv);
81d17fe5 1360 kfree(capiminors);
1da177e4
LT
1361}
1362
501c87a9
JK
1363#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1364
1365static inline int capinc_tty_init(void)
1366{
1367 return 0;
1368}
1369
1370static inline void capinc_tty_exit(void) { }
1371
1372#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1da177e4
LT
1373
1374/* -------- /proc functions ----------------------------------------- */
1375
1376/*
1377 * /proc/capi/capi20:
1378 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1379 */
9a58a80a 1380static int capi20_proc_show(struct seq_file *m, void *v)
1da177e4
LT
1381{
1382 struct capidev *cdev;
1383 struct list_head *l;
1da177e4 1384
b8f433dc 1385 mutex_lock(&capidev_list_lock);
1da177e4
LT
1386 list_for_each(l, &capidev_list) {
1387 cdev = list_entry(l, struct capidev, list);
9a58a80a 1388 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1da177e4
LT
1389 cdev->ap.applid,
1390 cdev->ap.nrecvctlpkt,
1391 cdev->ap.nrecvdatapkt,
1392 cdev->ap.nsentctlpkt,
1393 cdev->ap.nsentdatapkt);
1da177e4 1394 }
b8f433dc 1395 mutex_unlock(&capidev_list_lock);
9a58a80a 1396 return 0;
1da177e4
LT
1397}
1398
9a58a80a
AD
1399static int capi20_proc_open(struct inode *inode, struct file *file)
1400{
1401 return single_open(file, capi20_proc_show, NULL);
1402}
1403
1404static const struct file_operations capi20_proc_fops = {
1405 .owner = THIS_MODULE,
1406 .open = capi20_proc_open,
1407 .read = seq_read,
1408 .llseek = seq_lseek,
1409 .release = single_release,
1410};
1411
1da177e4
LT
1412/*
1413 * /proc/capi/capi20ncci:
1414 * applid ncci
1415 */
9a58a80a 1416static int capi20ncci_proc_show(struct seq_file *m, void *v)
1da177e4 1417{
884f5c44
JK
1418 struct capidev *cdev;
1419 struct capincci *np;
1da177e4 1420
b8f433dc 1421 mutex_lock(&capidev_list_lock);
884f5c44 1422 list_for_each_entry(cdev, &capidev_list, list) {
05b41494 1423 mutex_lock(&cdev->lock);
884f5c44
JK
1424 list_for_each_entry(np, &cdev->nccis, list)
1425 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
05b41494 1426 mutex_unlock(&cdev->lock);
1da177e4 1427 }
b8f433dc 1428 mutex_unlock(&capidev_list_lock);
9a58a80a 1429 return 0;
1da177e4
LT
1430}
1431
9a58a80a
AD
1432static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1433{
1434 return single_open(file, capi20ncci_proc_show, NULL);
1435}
1436
1437static const struct file_operations capi20ncci_proc_fops = {
1438 .owner = THIS_MODULE,
1439 .open = capi20ncci_proc_open,
1440 .read = seq_read,
1441 .llseek = seq_lseek,
1442 .release = single_release,
1da177e4
LT
1443};
1444
1445static void __init proc_init(void)
1446{
9a58a80a
AD
1447 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1448 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
1da177e4
LT
1449}
1450
1451static void __exit proc_exit(void)
1452{
9a58a80a
AD
1453 remove_proc_entry("capi/capi20", NULL);
1454 remove_proc_entry("capi/capi20ncci", NULL);
1da177e4
LT
1455}
1456
1457/* -------- init function and module interface ---------------------- */
1458
1459
1da177e4
LT
1460static int __init capi_init(void)
1461{
88549d6b 1462 const char *compileinfo;
6d9eac34 1463 int major_ret;
1da177e4 1464
6d9eac34
AM
1465 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1466 if (major_ret < 0) {
1da177e4 1467 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
6d9eac34 1468 return major_ret;
1da177e4 1469 }
56b22935 1470 capi_class = class_create(THIS_MODULE, "capi");
1da177e4
LT
1471 if (IS_ERR(capi_class)) {
1472 unregister_chrdev(capi_major, "capi20");
1473 return PTR_ERR(capi_class);
1474 }
1475
a9b12619 1476 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1da177e4 1477
1da177e4 1478 if (capinc_tty_init() < 0) {
d78b0368 1479 device_destroy(capi_class, MKDEV(capi_major, 0));
56b22935 1480 class_destroy(capi_class);
1da177e4
LT
1481 unregister_chrdev(capi_major, "capi20");
1482 return -ENOMEM;
1483 }
1da177e4
LT
1484
1485 proc_init();
1486
1da177e4
LT
1487#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1488 compileinfo = " (middleware+capifs)";
501c87a9 1489#elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
1da177e4 1490 compileinfo = " (no capifs)";
1da177e4
LT
1491#else
1492 compileinfo = " (no middleware)";
1493#endif
88549d6b
JK
1494 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1495 capi_major, compileinfo);
1da177e4
LT
1496
1497 return 0;
1498}
1499
1500static void __exit capi_exit(void)
1501{
1502 proc_exit();
1503
d78b0368 1504 device_destroy(capi_class, MKDEV(capi_major, 0));
56b22935 1505 class_destroy(capi_class);
1da177e4 1506 unregister_chrdev(capi_major, "capi20");
1da177e4 1507
1da177e4 1508 capinc_tty_exit();
1da177e4
LT
1509}
1510
1511module_init(capi_init);
1512module_exit(capi_exit);