CAPI: Use dynamic major for NCCI TTYs by default
[linux-2.6.git] / drivers / isdn / capi / capi.c
blobb1de0cbea69e0c5b11df48facb34d0255ba52d6b
1 /* $Id: capi.c,v 1.1.2.7 2004/04/28 09:48:59 armin Exp $
3 * CAPI 2.0 Interface for Linux
5 * Copyright 1996 by Carsten Paeth <calle@calle.de>
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
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>
21 #include <linux/mutex.h>
22 #include <linux/mm.h>
23 #include <linux/smp_lock.h>
24 #include <linux/timer.h>
25 #include <linux/wait.h>
26 #include <linux/tty.h>
27 #include <linux/netdevice.h>
28 #include <linux/ppp_defs.h>
29 #include <linux/if_ppp.h>
30 #include <linux/skbuff.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
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>
39 #include <linux/isdn/capiutil.h>
40 #include <linux/isdn/capicmd.h>
42 #include "capifs.h"
44 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
45 MODULE_AUTHOR("Carsten Paeth");
46 MODULE_LICENSE("GPL");
48 #undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
49 #undef _DEBUG_TTYFUNCS /* call to tty_driver */
50 #undef _DEBUG_DATAFLOW /* data flow */
52 /* -------- driver information -------------------------------------- */
54 static struct class *capi_class;
55 static int capi_major = 68; /* allocated */
57 module_param_named(major, capi_major, uint, 0);
59 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
60 #define CAPINC_NR_PORTS 32
61 #define CAPINC_MAX_PORTS 256
63 static int capi_ttyminors = CAPINC_NR_PORTS;
65 module_param_named(ttyminors, capi_ttyminors, uint, 0);
66 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
68 /* -------- defines ------------------------------------------------- */
70 #define CAPINC_MAX_RECVQUEUE 10
71 #define CAPINC_MAX_SENDQUEUE 10
72 #define CAPI_MAX_BLKSIZE 2048
74 /* -------- data structures ----------------------------------------- */
76 struct capidev;
77 struct capincci;
78 struct capiminor;
80 struct datahandle_queue {
81 struct list_head list;
82 u16 datahandle;
85 struct capiminor {
86 struct capincci *nccip;
87 unsigned int minor;
88 struct dentry *capifs_dentry;
90 struct capi20_appl *ap;
91 u32 ncci;
92 u16 datahandle;
93 u16 msgid;
95 struct tty_struct *tty;
96 int ttyinstop;
97 int ttyoutstop;
98 struct sk_buff *ttyskb;
99 atomic_t ttyopencount;
101 struct sk_buff_head inqueue;
102 int inbytes;
103 struct sk_buff_head outqueue;
104 int outbytes;
106 /* transmit path */
107 struct list_head ackqueue;
108 int nack;
109 spinlock_t ackqlock;
112 /* FIXME: The following lock is a sledgehammer-workaround to a
113 * locking issue with the capiminor (and maybe other) data structure(s).
114 * Access to this data is done in a racy way and crashes the machine with
115 * a FritzCard DSL driver; sooner or later. This is a workaround
116 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
117 * The correct (and scalable) fix for the issue seems to require
118 * an API change to the drivers... . */
119 static DEFINE_SPINLOCK(workaround_lock);
121 struct capincci {
122 struct list_head list;
123 u32 ncci;
124 struct capidev *cdev;
125 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
126 struct capiminor *minorp;
127 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
130 struct capidev {
131 struct list_head list;
132 struct capi20_appl ap;
133 u16 errcode;
134 unsigned userflags;
136 struct sk_buff_head recvqueue;
137 wait_queue_head_t recvwait;
139 struct list_head nccis;
141 struct mutex lock;
144 /* -------- global variables ---------------------------------------- */
146 static DEFINE_MUTEX(capidev_list_lock);
147 static LIST_HEAD(capidev_list);
149 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
151 static DEFINE_RWLOCK(capiminors_lock);
152 static struct capiminor **capiminors;
154 static struct tty_driver *capinc_tty_driver;
156 /* -------- datahandles --------------------------------------------- */
158 static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
160 struct datahandle_queue *n;
161 unsigned long flags;
163 n = kmalloc(sizeof(*n), GFP_ATOMIC);
164 if (unlikely(!n)) {
165 printk(KERN_ERR "capi: alloc datahandle failed\n");
166 return -1;
168 n->datahandle = datahandle;
169 INIT_LIST_HEAD(&n->list);
170 spin_lock_irqsave(&mp->ackqlock, flags);
171 list_add_tail(&n->list, &mp->ackqueue);
172 mp->nack++;
173 spin_unlock_irqrestore(&mp->ackqlock, flags);
174 return 0;
177 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
179 struct datahandle_queue *p, *tmp;
180 unsigned long flags;
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);
186 kfree(p);
187 mp->nack--;
188 spin_unlock_irqrestore(&mp->ackqlock, flags);
189 return 0;
192 spin_unlock_irqrestore(&mp->ackqlock, flags);
193 return -1;
196 static void capiminor_del_all_ack(struct capiminor *mp)
198 struct datahandle_queue *p, *tmp;
199 unsigned long flags;
201 spin_lock_irqsave(&mp->ackqlock, flags);
202 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
203 list_del(&p->list);
204 kfree(p);
205 mp->nack--;
207 spin_unlock_irqrestore(&mp->ackqlock, flags);
211 /* -------- struct capiminor ---------------------------------------- */
213 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
215 struct capiminor *mp;
216 struct device *dev;
217 unsigned int minor;
218 unsigned long flags;
220 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
221 if (!mp) {
222 printk(KERN_ERR "capi: can't alloc capiminor\n");
223 return NULL;
226 mp->ap = ap;
227 mp->ncci = ncci;
228 mp->msgid = 0;
229 atomic_set(&mp->ttyopencount,0);
230 INIT_LIST_HEAD(&mp->ackqueue);
231 spin_lock_init(&mp->ackqlock);
233 skb_queue_head_init(&mp->inqueue);
234 skb_queue_head_init(&mp->outqueue);
236 /* Allocate the least unused minor number. */
237 write_lock_irqsave(&capiminors_lock, flags);
238 for (minor = 0; minor < capi_ttyminors; minor++)
239 if (!capiminors[minor]) {
240 capiminors[minor] = mp;
241 break;
243 write_unlock_irqrestore(&capiminors_lock, flags);
245 if (minor == capi_ttyminors) {
246 printk(KERN_NOTICE "capi: out of minors\n");
247 goto err_out1;
250 mp->minor = minor;
252 dev = tty_register_device(capinc_tty_driver, minor, NULL);
253 if (IS_ERR(dev))
254 goto err_out2;
256 return mp;
258 err_out2:
259 write_lock_irqsave(&capiminors_lock, flags);
260 capiminors[minor] = NULL;
261 write_unlock_irqrestore(&capiminors_lock, flags);
263 err_out1:
264 kfree(mp);
265 return NULL;
268 static void capiminor_free(struct capiminor *mp)
270 unsigned long flags;
272 tty_unregister_device(capinc_tty_driver, mp->minor);
274 write_lock_irqsave(&capiminors_lock, flags);
275 capiminors[mp->minor] = NULL;
276 write_unlock_irqrestore(&capiminors_lock, flags);
278 kfree_skb(mp->ttyskb);
279 mp->ttyskb = NULL;
280 skb_queue_purge(&mp->inqueue);
281 skb_queue_purge(&mp->outqueue);
282 capiminor_del_all_ack(mp);
283 kfree(mp);
286 static struct capiminor *capiminor_get(unsigned int minor)
288 struct capiminor *mp;
290 read_lock(&capiminors_lock);
291 mp = capiminors[minor];
292 read_unlock(&capiminors_lock);
294 return mp;
297 /* -------- struct capincci ----------------------------------------- */
299 static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
301 struct capiminor *mp;
302 dev_t device;
304 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
305 return;
307 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
308 if (mp) {
309 mp->nccip = np;
310 #ifdef _DEBUG_REFCOUNT
311 printk(KERN_DEBUG "set mp->nccip\n");
312 #endif
313 device = MKDEV(capinc_tty_driver->major, mp->minor);
314 mp->capifs_dentry = capifs_new_ncci(mp->minor, device);
318 static void capincci_free_minor(struct capincci *np)
320 struct capiminor *mp = np->minorp;
322 if (mp) {
323 capifs_free_ncci(mp->capifs_dentry);
324 if (mp->tty) {
325 mp->nccip = NULL;
326 #ifdef _DEBUG_REFCOUNT
327 printk(KERN_DEBUG "reset mp->nccip\n");
328 #endif
329 tty_hangup(mp->tty);
330 } else {
331 capiminor_free(mp);
336 static inline unsigned int capincci_minor_opencount(struct capincci *np)
338 struct capiminor *mp = np->minorp;
340 return mp ? atomic_read(&mp->ttyopencount) : 0;
343 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
345 static inline void
346 capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
347 static inline void capincci_free_minor(struct capincci *np) { }
349 static inline unsigned int capincci_minor_opencount(struct capincci *np)
351 return 0;
354 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
356 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
358 struct capincci *np;
360 np = kzalloc(sizeof(*np), GFP_KERNEL);
361 if (!np)
362 return NULL;
363 np->ncci = ncci;
364 np->cdev = cdev;
366 capincci_alloc_minor(cdev, np);
368 list_add_tail(&np->list, &cdev->nccis);
370 return np;
373 static void capincci_free(struct capidev *cdev, u32 ncci)
375 struct capincci *np, *tmp;
377 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
378 if (ncci == 0xffffffff || np->ncci == ncci) {
379 capincci_free_minor(np);
380 list_del(&np->list);
381 kfree(np);
385 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
387 struct capincci *np;
389 list_for_each_entry(np, &cdev->nccis, list)
390 if (np->ncci == ncci)
391 return np;
392 return NULL;
395 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
396 /* -------- handle data queue --------------------------------------- */
398 static struct sk_buff *
399 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
401 struct sk_buff *nskb;
402 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
403 if (nskb) {
404 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
405 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
406 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
407 capimsg_setu16(s, 2, mp->ap->applid);
408 capimsg_setu8 (s, 4, CAPI_DATA_B3);
409 capimsg_setu8 (s, 5, CAPI_RESP);
410 capimsg_setu16(s, 6, mp->msgid++);
411 capimsg_setu32(s, 8, mp->ncci);
412 capimsg_setu16(s, 12, datahandle);
414 return nskb;
417 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
419 struct sk_buff *nskb;
420 int datalen;
421 u16 errcode, datahandle;
422 struct tty_ldisc *ld;
424 datalen = skb->len - CAPIMSG_LEN(skb->data);
425 if (mp->tty == NULL)
427 #ifdef _DEBUG_DATAFLOW
428 printk(KERN_DEBUG "capi: currently no receiver\n");
429 #endif
430 return -1;
433 ld = tty_ldisc_ref(mp->tty);
434 if (ld == NULL)
435 return -1;
436 if (ld->ops->receive_buf == NULL) {
437 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
438 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
439 #endif
440 goto bad;
442 if (mp->ttyinstop) {
443 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
444 printk(KERN_DEBUG "capi: recv tty throttled\n");
445 #endif
446 goto bad;
448 if (mp->tty->receive_room < datalen) {
449 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
450 printk(KERN_DEBUG "capi: no room in tty\n");
451 #endif
452 goto bad;
454 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
455 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
456 goto bad;
458 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
459 errcode = capi20_put_message(mp->ap, nskb);
460 if (errcode != CAPI_NOERROR) {
461 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
462 errcode);
463 kfree_skb(nskb);
464 goto bad;
466 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
467 #ifdef _DEBUG_DATAFLOW
468 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
469 datahandle, skb->len);
470 #endif
471 ld->ops->receive_buf(mp->tty, skb->data, NULL, skb->len);
472 kfree_skb(skb);
473 tty_ldisc_deref(ld);
474 return 0;
475 bad:
476 tty_ldisc_deref(ld);
477 return -1;
480 static void handle_minor_recv(struct capiminor *mp)
482 struct sk_buff *skb;
483 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
484 unsigned int len = skb->len;
485 mp->inbytes -= len;
486 if (handle_recv_skb(mp, skb) < 0) {
487 skb_queue_head(&mp->inqueue, skb);
488 mp->inbytes += len;
489 return;
494 static int handle_minor_send(struct capiminor *mp)
496 struct sk_buff *skb;
497 u16 len;
498 int count = 0;
499 u16 errcode;
500 u16 datahandle;
502 if (mp->tty && mp->ttyoutstop) {
503 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
504 printk(KERN_DEBUG "capi: send: tty stopped\n");
505 #endif
506 return 0;
509 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
510 datahandle = mp->datahandle;
511 len = (u16)skb->len;
512 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
513 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
514 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
515 capimsg_setu16(skb->data, 2, mp->ap->applid);
516 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
517 capimsg_setu8 (skb->data, 5, CAPI_REQ);
518 capimsg_setu16(skb->data, 6, mp->msgid++);
519 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
520 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
521 capimsg_setu16(skb->data, 16, len); /* Data length */
522 capimsg_setu16(skb->data, 18, datahandle);
523 capimsg_setu16(skb->data, 20, 0); /* Flags */
525 if (capiminor_add_ack(mp, datahandle) < 0) {
526 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
527 skb_queue_head(&mp->outqueue, skb);
528 return count;
530 errcode = capi20_put_message(mp->ap, skb);
531 if (errcode == CAPI_NOERROR) {
532 mp->datahandle++;
533 count++;
534 mp->outbytes -= len;
535 #ifdef _DEBUG_DATAFLOW
536 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
537 datahandle, len);
538 #endif
539 continue;
541 capiminor_del_ack(mp, datahandle);
543 if (errcode == CAPI_SENDQUEUEFULL) {
544 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
545 skb_queue_head(&mp->outqueue, skb);
546 break;
549 /* ups, drop packet */
550 printk(KERN_ERR "capi: put_message = %x\n", errcode);
551 mp->outbytes -= len;
552 kfree_skb(skb);
554 return count;
557 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
558 /* -------- function called by lower level -------------------------- */
560 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
562 struct capidev *cdev = ap->private;
563 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
564 struct capiminor *mp;
565 u16 datahandle;
566 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
567 struct capincci *np;
568 unsigned long flags;
570 mutex_lock(&cdev->lock);
572 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
573 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
574 if ((info & 0xff00) == 0)
575 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
577 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
578 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
580 spin_lock_irqsave(&workaround_lock, flags);
581 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
582 skb_queue_tail(&cdev->recvqueue, skb);
583 wake_up_interruptible(&cdev->recvwait);
584 goto unlock_out;
587 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
588 if (!np) {
589 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
590 skb_queue_tail(&cdev->recvqueue, skb);
591 wake_up_interruptible(&cdev->recvwait);
592 goto unlock_out;
595 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
596 skb_queue_tail(&cdev->recvqueue, skb);
597 wake_up_interruptible(&cdev->recvwait);
599 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
601 mp = np->minorp;
602 if (!mp) {
603 skb_queue_tail(&cdev->recvqueue, skb);
604 wake_up_interruptible(&cdev->recvwait);
605 goto unlock_out;
607 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
608 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
609 #ifdef _DEBUG_DATAFLOW
610 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
611 datahandle, skb->len-CAPIMSG_LEN(skb->data));
612 #endif
613 skb_queue_tail(&mp->inqueue, skb);
614 mp->inbytes += skb->len;
615 handle_minor_recv(mp);
617 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
619 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
620 #ifdef _DEBUG_DATAFLOW
621 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
622 datahandle,
623 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
624 #endif
625 kfree_skb(skb);
626 (void)capiminor_del_ack(mp, datahandle);
627 if (mp->tty)
628 tty_wakeup(mp->tty);
629 (void)handle_minor_send(mp);
631 } else {
632 /* ups, let capi application handle it :-) */
633 skb_queue_tail(&cdev->recvqueue, skb);
634 wake_up_interruptible(&cdev->recvwait);
636 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
638 unlock_out:
639 spin_unlock_irqrestore(&workaround_lock, flags);
640 mutex_unlock(&cdev->lock);
643 /* -------- file_operations for capidev ----------------------------- */
645 static ssize_t
646 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
648 struct capidev *cdev = (struct capidev *)file->private_data;
649 struct sk_buff *skb;
650 size_t copied;
651 int err;
653 if (!cdev->ap.applid)
654 return -ENODEV;
656 skb = skb_dequeue(&cdev->recvqueue);
657 if (!skb) {
658 if (file->f_flags & O_NONBLOCK)
659 return -EAGAIN;
660 err = wait_event_interruptible(cdev->recvwait,
661 (skb = skb_dequeue(&cdev->recvqueue)));
662 if (err)
663 return err;
665 if (skb->len > count) {
666 skb_queue_head(&cdev->recvqueue, skb);
667 return -EMSGSIZE;
669 if (copy_to_user(buf, skb->data, skb->len)) {
670 skb_queue_head(&cdev->recvqueue, skb);
671 return -EFAULT;
673 copied = skb->len;
675 kfree_skb(skb);
677 return copied;
680 static ssize_t
681 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
683 struct capidev *cdev = (struct capidev *)file->private_data;
684 struct sk_buff *skb;
685 u16 mlen;
687 if (!cdev->ap.applid)
688 return -ENODEV;
690 skb = alloc_skb(count, GFP_USER);
691 if (!skb)
692 return -ENOMEM;
694 if (copy_from_user(skb_put(skb, count), buf, count)) {
695 kfree_skb(skb);
696 return -EFAULT;
698 mlen = CAPIMSG_LEN(skb->data);
699 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
700 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
701 kfree_skb(skb);
702 return -EINVAL;
704 } else {
705 if (mlen != count) {
706 kfree_skb(skb);
707 return -EINVAL;
710 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
712 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
713 mutex_lock(&cdev->lock);
714 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
715 mutex_unlock(&cdev->lock);
718 cdev->errcode = capi20_put_message(&cdev->ap, skb);
720 if (cdev->errcode) {
721 kfree_skb(skb);
722 return -EIO;
724 return count;
727 static unsigned int
728 capi_poll(struct file *file, poll_table * wait)
730 struct capidev *cdev = (struct capidev *)file->private_data;
731 unsigned int mask = 0;
733 if (!cdev->ap.applid)
734 return POLLERR;
736 poll_wait(file, &(cdev->recvwait), wait);
737 mask = POLLOUT | POLLWRNORM;
738 if (!skb_queue_empty(&cdev->recvqueue))
739 mask |= POLLIN | POLLRDNORM;
740 return mask;
743 static int
744 capi_ioctl(struct inode *inode, struct file *file,
745 unsigned int cmd, unsigned long arg)
747 struct capidev *cdev = file->private_data;
748 capi_ioctl_struct data;
749 int retval = -EINVAL;
750 void __user *argp = (void __user *)arg;
752 switch (cmd) {
753 case CAPI_REGISTER:
754 mutex_lock(&cdev->lock);
756 if (cdev->ap.applid) {
757 retval = -EEXIST;
758 goto register_out;
760 if (copy_from_user(&cdev->ap.rparam, argp,
761 sizeof(struct capi_register_params))) {
762 retval = -EFAULT;
763 goto register_out;
765 cdev->ap.private = cdev;
766 cdev->ap.recv_message = capi_recv_message;
767 cdev->errcode = capi20_register(&cdev->ap);
768 retval = (int)cdev->ap.applid;
769 if (cdev->errcode) {
770 cdev->ap.applid = 0;
771 retval = -EIO;
774 register_out:
775 mutex_unlock(&cdev->lock);
776 return retval;
778 case CAPI_GET_VERSION:
780 if (copy_from_user(&data.contr, argp,
781 sizeof(data.contr)))
782 return -EFAULT;
783 cdev->errcode = capi20_get_version(data.contr, &data.version);
784 if (cdev->errcode)
785 return -EIO;
786 if (copy_to_user(argp, &data.version,
787 sizeof(data.version)))
788 return -EFAULT;
790 return 0;
792 case CAPI_GET_SERIAL:
794 if (copy_from_user(&data.contr, argp,
795 sizeof(data.contr)))
796 return -EFAULT;
797 cdev->errcode = capi20_get_serial (data.contr, data.serial);
798 if (cdev->errcode)
799 return -EIO;
800 if (copy_to_user(argp, data.serial,
801 sizeof(data.serial)))
802 return -EFAULT;
804 return 0;
805 case CAPI_GET_PROFILE:
807 if (copy_from_user(&data.contr, argp,
808 sizeof(data.contr)))
809 return -EFAULT;
811 if (data.contr == 0) {
812 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
813 if (cdev->errcode)
814 return -EIO;
816 retval = copy_to_user(argp,
817 &data.profile.ncontroller,
818 sizeof(data.profile.ncontroller));
820 } else {
821 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
822 if (cdev->errcode)
823 return -EIO;
825 retval = copy_to_user(argp, &data.profile,
826 sizeof(data.profile));
828 if (retval)
829 return -EFAULT;
831 return 0;
833 case CAPI_GET_MANUFACTURER:
835 if (copy_from_user(&data.contr, argp,
836 sizeof(data.contr)))
837 return -EFAULT;
838 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
839 if (cdev->errcode)
840 return -EIO;
842 if (copy_to_user(argp, data.manufacturer,
843 sizeof(data.manufacturer)))
844 return -EFAULT;
847 return 0;
848 case CAPI_GET_ERRCODE:
849 data.errcode = cdev->errcode;
850 cdev->errcode = CAPI_NOERROR;
851 if (arg) {
852 if (copy_to_user(argp, &data.errcode,
853 sizeof(data.errcode)))
854 return -EFAULT;
856 return data.errcode;
858 case CAPI_INSTALLED:
859 if (capi20_isinstalled() == CAPI_NOERROR)
860 return 0;
861 return -ENXIO;
863 case CAPI_MANUFACTURER_CMD:
865 struct capi_manufacturer_cmd mcmd;
866 if (!capable(CAP_SYS_ADMIN))
867 return -EPERM;
868 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
869 return -EFAULT;
870 return capi20_manufacturer(mcmd.cmd, mcmd.data);
872 return 0;
874 case CAPI_SET_FLAGS:
875 case CAPI_CLR_FLAGS: {
876 unsigned userflags;
878 if (copy_from_user(&userflags, argp, sizeof(userflags)))
879 return -EFAULT;
881 mutex_lock(&cdev->lock);
882 if (cmd == CAPI_SET_FLAGS)
883 cdev->userflags |= userflags;
884 else
885 cdev->userflags &= ~userflags;
886 mutex_unlock(&cdev->lock);
887 return 0;
889 case CAPI_GET_FLAGS:
890 if (copy_to_user(argp, &cdev->userflags,
891 sizeof(cdev->userflags)))
892 return -EFAULT;
893 return 0;
895 case CAPI_NCCI_OPENCOUNT: {
896 struct capincci *nccip;
897 unsigned ncci;
898 int count = 0;
900 if (copy_from_user(&ncci, argp, sizeof(ncci)))
901 return -EFAULT;
903 mutex_lock(&cdev->lock);
904 nccip = capincci_find(cdev, (u32)ncci);
905 if (nccip)
906 count = capincci_minor_opencount(nccip);
907 mutex_unlock(&cdev->lock);
908 return count;
911 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
912 case CAPI_NCCI_GETUNIT: {
913 struct capincci *nccip;
914 struct capiminor *mp;
915 unsigned ncci;
916 int unit = -ESRCH;
918 if (copy_from_user(&ncci, argp, sizeof(ncci)))
919 return -EFAULT;
921 mutex_lock(&cdev->lock);
922 nccip = capincci_find(cdev, (u32)ncci);
923 if (nccip) {
924 mp = nccip->minorp;
925 if (mp)
926 unit = mp->minor;
928 mutex_unlock(&cdev->lock);
929 return unit;
931 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
933 default:
934 return -EINVAL;
938 static int capi_open(struct inode *inode, struct file *file)
940 struct capidev *cdev;
942 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
943 if (!cdev)
944 return -ENOMEM;
946 mutex_init(&cdev->lock);
947 skb_queue_head_init(&cdev->recvqueue);
948 init_waitqueue_head(&cdev->recvwait);
949 INIT_LIST_HEAD(&cdev->nccis);
950 file->private_data = cdev;
952 mutex_lock(&capidev_list_lock);
953 list_add_tail(&cdev->list, &capidev_list);
954 mutex_unlock(&capidev_list_lock);
956 return nonseekable_open(inode, file);
959 static int capi_release(struct inode *inode, struct file *file)
961 struct capidev *cdev = file->private_data;
963 mutex_lock(&capidev_list_lock);
964 list_del(&cdev->list);
965 mutex_unlock(&capidev_list_lock);
967 if (cdev->ap.applid)
968 capi20_release(&cdev->ap);
969 skb_queue_purge(&cdev->recvqueue);
970 capincci_free(cdev, 0xffffffff);
972 kfree(cdev);
973 return 0;
976 static const struct file_operations capi_fops =
978 .owner = THIS_MODULE,
979 .llseek = no_llseek,
980 .read = capi_read,
981 .write = capi_write,
982 .poll = capi_poll,
983 .ioctl = capi_ioctl,
984 .open = capi_open,
985 .release = capi_release,
988 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
989 /* -------- tty_operations for capincci ----------------------------- */
991 static int capinc_tty_open(struct tty_struct * tty, struct file * file)
993 struct capiminor *mp;
994 unsigned long flags;
996 mp = capiminor_get(iminor(file->f_path.dentry->d_inode));
997 if (mp->nccip == NULL)
998 return -ENXIO;
1000 tty->driver_data = (void *)mp;
1002 spin_lock_irqsave(&workaround_lock, flags);
1003 if (atomic_read(&mp->ttyopencount) == 0)
1004 mp->tty = tty;
1005 atomic_inc(&mp->ttyopencount);
1006 #ifdef _DEBUG_REFCOUNT
1007 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1008 #endif
1009 handle_minor_recv(mp);
1010 spin_unlock_irqrestore(&workaround_lock, flags);
1011 return 0;
1014 static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1016 struct capiminor *mp;
1018 mp = (struct capiminor *)tty->driver_data;
1019 if (mp) {
1020 if (atomic_dec_and_test(&mp->ttyopencount)) {
1021 #ifdef _DEBUG_REFCOUNT
1022 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1023 #endif
1024 tty->driver_data = NULL;
1025 mp->tty = NULL;
1027 #ifdef _DEBUG_REFCOUNT
1028 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1029 #endif
1030 if (mp->nccip == NULL)
1031 capiminor_free(mp);
1034 #ifdef _DEBUG_REFCOUNT
1035 printk(KERN_DEBUG "capinc_tty_close\n");
1036 #endif
1039 static int capinc_tty_write(struct tty_struct * tty,
1040 const unsigned char *buf, int count)
1042 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1043 struct sk_buff *skb;
1044 unsigned long flags;
1046 #ifdef _DEBUG_TTYFUNCS
1047 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1048 #endif
1050 if (!mp || !mp->nccip) {
1051 #ifdef _DEBUG_TTYFUNCS
1052 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1053 #endif
1054 return 0;
1057 spin_lock_irqsave(&workaround_lock, flags);
1058 skb = mp->ttyskb;
1059 if (skb) {
1060 mp->ttyskb = NULL;
1061 skb_queue_tail(&mp->outqueue, skb);
1062 mp->outbytes += skb->len;
1065 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1066 if (!skb) {
1067 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1068 spin_unlock_irqrestore(&workaround_lock, flags);
1069 return -ENOMEM;
1072 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1073 memcpy(skb_put(skb, count), buf, count);
1075 skb_queue_tail(&mp->outqueue, skb);
1076 mp->outbytes += skb->len;
1077 (void)handle_minor_send(mp);
1078 (void)handle_minor_recv(mp);
1079 spin_unlock_irqrestore(&workaround_lock, flags);
1080 return count;
1083 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1085 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1086 struct sk_buff *skb;
1087 unsigned long flags;
1088 int ret = 1;
1090 #ifdef _DEBUG_TTYFUNCS
1091 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1092 #endif
1094 if (!mp || !mp->nccip) {
1095 #ifdef _DEBUG_TTYFUNCS
1096 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1097 #endif
1098 return 0;
1101 spin_lock_irqsave(&workaround_lock, flags);
1102 skb = mp->ttyskb;
1103 if (skb) {
1104 if (skb_tailroom(skb) > 0) {
1105 *(skb_put(skb, 1)) = ch;
1106 spin_unlock_irqrestore(&workaround_lock, flags);
1107 return 1;
1109 mp->ttyskb = NULL;
1110 skb_queue_tail(&mp->outqueue, skb);
1111 mp->outbytes += skb->len;
1112 (void)handle_minor_send(mp);
1114 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1115 if (skb) {
1116 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1117 *(skb_put(skb, 1)) = ch;
1118 mp->ttyskb = skb;
1119 } else {
1120 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1121 ret = 0;
1123 spin_unlock_irqrestore(&workaround_lock, flags);
1124 return ret;
1127 static void capinc_tty_flush_chars(struct tty_struct *tty)
1129 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1130 struct sk_buff *skb;
1131 unsigned long flags;
1133 #ifdef _DEBUG_TTYFUNCS
1134 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1135 #endif
1137 if (!mp || !mp->nccip) {
1138 #ifdef _DEBUG_TTYFUNCS
1139 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1140 #endif
1141 return;
1144 spin_lock_irqsave(&workaround_lock, flags);
1145 skb = mp->ttyskb;
1146 if (skb) {
1147 mp->ttyskb = NULL;
1148 skb_queue_tail(&mp->outqueue, skb);
1149 mp->outbytes += skb->len;
1150 (void)handle_minor_send(mp);
1152 (void)handle_minor_recv(mp);
1153 spin_unlock_irqrestore(&workaround_lock, flags);
1156 static int capinc_tty_write_room(struct tty_struct *tty)
1158 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1159 int room;
1160 if (!mp || !mp->nccip) {
1161 #ifdef _DEBUG_TTYFUNCS
1162 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1163 #endif
1164 return 0;
1166 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1167 room *= CAPI_MAX_BLKSIZE;
1168 #ifdef _DEBUG_TTYFUNCS
1169 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1170 #endif
1171 return room;
1174 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1176 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1177 if (!mp || !mp->nccip) {
1178 #ifdef _DEBUG_TTYFUNCS
1179 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1180 #endif
1181 return 0;
1183 #ifdef _DEBUG_TTYFUNCS
1184 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1185 mp->outbytes, mp->nack,
1186 skb_queue_len(&mp->outqueue),
1187 skb_queue_len(&mp->inqueue));
1188 #endif
1189 return mp->outbytes;
1192 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1193 unsigned int cmd, unsigned long arg)
1195 int error = 0;
1196 switch (cmd) {
1197 default:
1198 error = n_tty_ioctl_helper(tty, file, cmd, arg);
1199 break;
1201 return error;
1204 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1206 #ifdef _DEBUG_TTYFUNCS
1207 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1208 #endif
1211 static void capinc_tty_throttle(struct tty_struct * tty)
1213 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1214 #ifdef _DEBUG_TTYFUNCS
1215 printk(KERN_DEBUG "capinc_tty_throttle\n");
1216 #endif
1217 if (mp)
1218 mp->ttyinstop = 1;
1221 static void capinc_tty_unthrottle(struct tty_struct * tty)
1223 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1224 unsigned long flags;
1225 #ifdef _DEBUG_TTYFUNCS
1226 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1227 #endif
1228 if (mp) {
1229 spin_lock_irqsave(&workaround_lock, flags);
1230 mp->ttyinstop = 0;
1231 handle_minor_recv(mp);
1232 spin_unlock_irqrestore(&workaround_lock, flags);
1236 static void capinc_tty_stop(struct tty_struct *tty)
1238 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1239 #ifdef _DEBUG_TTYFUNCS
1240 printk(KERN_DEBUG "capinc_tty_stop\n");
1241 #endif
1242 if (mp) {
1243 mp->ttyoutstop = 1;
1247 static void capinc_tty_start(struct tty_struct *tty)
1249 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1250 unsigned long flags;
1251 #ifdef _DEBUG_TTYFUNCS
1252 printk(KERN_DEBUG "capinc_tty_start\n");
1253 #endif
1254 if (mp) {
1255 spin_lock_irqsave(&workaround_lock, flags);
1256 mp->ttyoutstop = 0;
1257 (void)handle_minor_send(mp);
1258 spin_unlock_irqrestore(&workaround_lock, flags);
1262 static void capinc_tty_hangup(struct tty_struct *tty)
1264 #ifdef _DEBUG_TTYFUNCS
1265 printk(KERN_DEBUG "capinc_tty_hangup\n");
1266 #endif
1269 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1271 #ifdef _DEBUG_TTYFUNCS
1272 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1273 #endif
1274 return 0;
1277 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1279 #ifdef _DEBUG_TTYFUNCS
1280 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1281 #endif
1284 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1286 #ifdef _DEBUG_TTYFUNCS
1287 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1288 #endif
1291 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1293 #ifdef _DEBUG_TTYFUNCS
1294 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1295 #endif
1298 static const struct tty_operations capinc_ops = {
1299 .open = capinc_tty_open,
1300 .close = capinc_tty_close,
1301 .write = capinc_tty_write,
1302 .put_char = capinc_tty_put_char,
1303 .flush_chars = capinc_tty_flush_chars,
1304 .write_room = capinc_tty_write_room,
1305 .chars_in_buffer = capinc_tty_chars_in_buffer,
1306 .ioctl = capinc_tty_ioctl,
1307 .set_termios = capinc_tty_set_termios,
1308 .throttle = capinc_tty_throttle,
1309 .unthrottle = capinc_tty_unthrottle,
1310 .stop = capinc_tty_stop,
1311 .start = capinc_tty_start,
1312 .hangup = capinc_tty_hangup,
1313 .break_ctl = capinc_tty_break_ctl,
1314 .flush_buffer = capinc_tty_flush_buffer,
1315 .set_ldisc = capinc_tty_set_ldisc,
1316 .send_xchar = capinc_tty_send_xchar,
1319 static int __init capinc_tty_init(void)
1321 struct tty_driver *drv;
1322 int err;
1324 if (capi_ttyminors > CAPINC_MAX_PORTS)
1325 capi_ttyminors = CAPINC_MAX_PORTS;
1326 if (capi_ttyminors <= 0)
1327 capi_ttyminors = CAPINC_NR_PORTS;
1329 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1330 GFP_KERNEL);
1331 if (!capiminors)
1332 return -ENOMEM;
1334 drv = alloc_tty_driver(capi_ttyminors);
1335 if (!drv) {
1336 kfree(capiminors);
1337 return -ENOMEM;
1339 drv->owner = THIS_MODULE;
1340 drv->driver_name = "capi_nc";
1341 drv->name = "capi";
1342 drv->major = 0;
1343 drv->minor_start = 0;
1344 drv->type = TTY_DRIVER_TYPE_SERIAL;
1345 drv->subtype = SERIAL_TYPE_NORMAL;
1346 drv->init_termios = tty_std_termios;
1347 drv->init_termios.c_iflag = ICRNL;
1348 drv->init_termios.c_oflag = OPOST | ONLCR;
1349 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1350 drv->init_termios.c_lflag = 0;
1351 drv->flags =
1352 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1353 TTY_DRIVER_DYNAMIC_DEV;
1354 tty_set_operations(drv, &capinc_ops);
1356 err = tty_register_driver(drv);
1357 if (err) {
1358 put_tty_driver(drv);
1359 kfree(capiminors);
1360 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1361 return err;
1363 capinc_tty_driver = drv;
1364 return 0;
1367 static void __exit capinc_tty_exit(void)
1369 tty_unregister_driver(capinc_tty_driver);
1370 put_tty_driver(capinc_tty_driver);
1371 kfree(capiminors);
1374 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1376 static inline int capinc_tty_init(void)
1378 return 0;
1381 static inline void capinc_tty_exit(void) { }
1383 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1385 /* -------- /proc functions ----------------------------------------- */
1388 * /proc/capi/capi20:
1389 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1391 static int capi20_proc_show(struct seq_file *m, void *v)
1393 struct capidev *cdev;
1394 struct list_head *l;
1396 mutex_lock(&capidev_list_lock);
1397 list_for_each(l, &capidev_list) {
1398 cdev = list_entry(l, struct capidev, list);
1399 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1400 cdev->ap.applid,
1401 cdev->ap.nrecvctlpkt,
1402 cdev->ap.nrecvdatapkt,
1403 cdev->ap.nsentctlpkt,
1404 cdev->ap.nsentdatapkt);
1406 mutex_unlock(&capidev_list_lock);
1407 return 0;
1410 static int capi20_proc_open(struct inode *inode, struct file *file)
1412 return single_open(file, capi20_proc_show, NULL);
1415 static const struct file_operations capi20_proc_fops = {
1416 .owner = THIS_MODULE,
1417 .open = capi20_proc_open,
1418 .read = seq_read,
1419 .llseek = seq_lseek,
1420 .release = single_release,
1424 * /proc/capi/capi20ncci:
1425 * applid ncci
1427 static int capi20ncci_proc_show(struct seq_file *m, void *v)
1429 struct capidev *cdev;
1430 struct capincci *np;
1432 mutex_lock(&capidev_list_lock);
1433 list_for_each_entry(cdev, &capidev_list, list) {
1434 mutex_lock(&cdev->lock);
1435 list_for_each_entry(np, &cdev->nccis, list)
1436 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
1437 mutex_unlock(&cdev->lock);
1439 mutex_unlock(&capidev_list_lock);
1440 return 0;
1443 static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1445 return single_open(file, capi20ncci_proc_show, NULL);
1448 static const struct file_operations capi20ncci_proc_fops = {
1449 .owner = THIS_MODULE,
1450 .open = capi20ncci_proc_open,
1451 .read = seq_read,
1452 .llseek = seq_lseek,
1453 .release = single_release,
1456 static void __init proc_init(void)
1458 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1459 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
1462 static void __exit proc_exit(void)
1464 remove_proc_entry("capi/capi20", NULL);
1465 remove_proc_entry("capi/capi20ncci", NULL);
1468 /* -------- init function and module interface ---------------------- */
1471 static int __init capi_init(void)
1473 const char *compileinfo;
1474 int major_ret;
1476 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1477 if (major_ret < 0) {
1478 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1479 return major_ret;
1481 capi_class = class_create(THIS_MODULE, "capi");
1482 if (IS_ERR(capi_class)) {
1483 unregister_chrdev(capi_major, "capi20");
1484 return PTR_ERR(capi_class);
1487 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1489 if (capinc_tty_init() < 0) {
1490 device_destroy(capi_class, MKDEV(capi_major, 0));
1491 class_destroy(capi_class);
1492 unregister_chrdev(capi_major, "capi20");
1493 return -ENOMEM;
1496 proc_init();
1498 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1499 compileinfo = " (middleware+capifs)";
1500 #elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
1501 compileinfo = " (no capifs)";
1502 #else
1503 compileinfo = " (no middleware)";
1504 #endif
1505 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1506 capi_major, compileinfo);
1508 return 0;
1511 static void __exit capi_exit(void)
1513 proc_exit();
1515 device_destroy(capi_class, MKDEV(capi_major, 0));
1516 class_destroy(capi_class);
1517 unregister_chrdev(capi_major, "capi20");
1519 capinc_tty_exit();
1522 module_init(capi_init);
1523 module_exit(capi_exit);