gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / isdn / capi / capi.c
blob85767f52fe3c72c940aefa60fc8a229e4bb99cc5
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/compiler.h>
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/major.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/fcntl.h>
20 #include <linux/fs.h>
21 #include <linux/signal.h>
22 #include <linux/mutex.h>
23 #include <linux/mm.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/ppp-ioctl.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 "kcapi.h"
44 MODULE_DESCRIPTION("CAPI4Linux: kernel CAPI layer and /dev/capi20 interface");
45 MODULE_AUTHOR("Carsten Paeth");
46 MODULE_LICENSE("GPL");
48 /* -------- driver information -------------------------------------- */
50 static DEFINE_MUTEX(capi_mutex);
51 static struct class *capi_class;
52 static int capi_major = 68; /* allocated */
54 module_param_named(major, capi_major, uint, 0);
56 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
57 #define CAPINC_NR_PORTS 32
58 #define CAPINC_MAX_PORTS 256
60 static int capi_ttyminors = CAPINC_NR_PORTS;
62 module_param_named(ttyminors, capi_ttyminors, uint, 0);
63 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
65 /* -------- defines ------------------------------------------------- */
67 #define CAPINC_MAX_RECVQUEUE 10
68 #define CAPINC_MAX_SENDQUEUE 10
69 #define CAPI_MAX_BLKSIZE 2048
71 /* -------- data structures ----------------------------------------- */
73 struct capidev;
74 struct capincci;
75 struct capiminor;
77 struct ackqueue_entry {
78 struct list_head list;
79 u16 datahandle;
82 struct capiminor {
83 unsigned int minor;
85 struct capi20_appl *ap;
86 u32 ncci;
87 atomic_t datahandle;
88 atomic_t msgid;
90 struct tty_port port;
91 int ttyinstop;
92 int ttyoutstop;
94 struct sk_buff_head inqueue;
96 struct sk_buff_head outqueue;
97 int outbytes;
98 struct sk_buff *outskb;
99 spinlock_t outlock;
101 /* transmit path */
102 struct list_head ackqueue;
103 int nack;
104 spinlock_t ackqlock;
107 struct capincci {
108 struct list_head list;
109 u32 ncci;
110 struct capidev *cdev;
111 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
112 struct capiminor *minorp;
113 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
116 struct capidev {
117 struct list_head list;
118 struct capi20_appl ap;
119 u16 errcode;
120 unsigned userflags;
122 struct sk_buff_head recvqueue;
123 wait_queue_head_t recvwait;
125 struct list_head nccis;
127 struct mutex lock;
130 /* -------- global variables ---------------------------------------- */
132 static DEFINE_MUTEX(capidev_list_lock);
133 static LIST_HEAD(capidev_list);
135 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
137 static DEFINE_SPINLOCK(capiminors_lock);
138 static struct capiminor **capiminors;
140 static struct tty_driver *capinc_tty_driver;
142 /* -------- datahandles --------------------------------------------- */
144 static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
146 struct ackqueue_entry *n;
148 n = kmalloc(sizeof(*n), GFP_ATOMIC);
149 if (unlikely(!n)) {
150 printk(KERN_ERR "capi: alloc datahandle failed\n");
151 return -1;
153 n->datahandle = datahandle;
154 INIT_LIST_HEAD(&n->list);
155 spin_lock_bh(&mp->ackqlock);
156 list_add_tail(&n->list, &mp->ackqueue);
157 mp->nack++;
158 spin_unlock_bh(&mp->ackqlock);
159 return 0;
162 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
164 struct ackqueue_entry *p, *tmp;
166 spin_lock_bh(&mp->ackqlock);
167 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
168 if (p->datahandle == datahandle) {
169 list_del(&p->list);
170 mp->nack--;
171 spin_unlock_bh(&mp->ackqlock);
172 kfree(p);
173 return 0;
176 spin_unlock_bh(&mp->ackqlock);
177 return -1;
180 static void capiminor_del_all_ack(struct capiminor *mp)
182 struct ackqueue_entry *p, *tmp;
184 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
185 list_del(&p->list);
186 kfree(p);
187 mp->nack--;
192 /* -------- struct capiminor ---------------------------------------- */
194 static void capiminor_destroy(struct tty_port *port)
196 struct capiminor *mp = container_of(port, struct capiminor, port);
198 kfree_skb(mp->outskb);
199 skb_queue_purge(&mp->inqueue);
200 skb_queue_purge(&mp->outqueue);
201 capiminor_del_all_ack(mp);
202 kfree(mp);
205 static const struct tty_port_operations capiminor_port_ops = {
206 .destruct = capiminor_destroy,
209 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
211 struct capiminor *mp;
212 struct device *dev;
213 unsigned int minor;
215 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
216 if (!mp) {
217 printk(KERN_ERR "capi: can't alloc capiminor\n");
218 return NULL;
221 mp->ap = ap;
222 mp->ncci = ncci;
223 INIT_LIST_HEAD(&mp->ackqueue);
224 spin_lock_init(&mp->ackqlock);
226 skb_queue_head_init(&mp->inqueue);
227 skb_queue_head_init(&mp->outqueue);
228 spin_lock_init(&mp->outlock);
230 tty_port_init(&mp->port);
231 mp->port.ops = &capiminor_port_ops;
233 /* Allocate the least unused minor number. */
234 spin_lock(&capiminors_lock);
235 for (minor = 0; minor < capi_ttyminors; minor++)
236 if (!capiminors[minor]) {
237 capiminors[minor] = mp;
238 break;
240 spin_unlock(&capiminors_lock);
242 if (minor == capi_ttyminors) {
243 printk(KERN_NOTICE "capi: out of minors\n");
244 goto err_out1;
247 mp->minor = minor;
249 dev = tty_port_register_device(&mp->port, capinc_tty_driver, minor,
250 NULL);
251 if (IS_ERR(dev))
252 goto err_out2;
254 return mp;
256 err_out2:
257 spin_lock(&capiminors_lock);
258 capiminors[minor] = NULL;
259 spin_unlock(&capiminors_lock);
261 err_out1:
262 tty_port_put(&mp->port);
263 return NULL;
266 static struct capiminor *capiminor_get(unsigned int minor)
268 struct capiminor *mp;
270 spin_lock(&capiminors_lock);
271 mp = capiminors[minor];
272 if (mp)
273 tty_port_get(&mp->port);
274 spin_unlock(&capiminors_lock);
276 return mp;
279 static inline void capiminor_put(struct capiminor *mp)
281 tty_port_put(&mp->port);
284 static void capiminor_free(struct capiminor *mp)
286 tty_unregister_device(capinc_tty_driver, mp->minor);
288 spin_lock(&capiminors_lock);
289 capiminors[mp->minor] = NULL;
290 spin_unlock(&capiminors_lock);
292 capiminor_put(mp);
295 /* -------- struct capincci ----------------------------------------- */
297 static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
299 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
300 np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
303 static void capincci_free_minor(struct capincci *np)
305 struct capiminor *mp = np->minorp;
306 struct tty_struct *tty;
308 if (mp) {
309 tty = tty_port_tty_get(&mp->port);
310 if (tty) {
311 tty_vhangup(tty);
312 tty_kref_put(tty);
315 capiminor_free(mp);
319 static inline unsigned int capincci_minor_opencount(struct capincci *np)
321 struct capiminor *mp = np->minorp;
322 unsigned int count = 0;
323 struct tty_struct *tty;
325 if (mp) {
326 tty = tty_port_tty_get(&mp->port);
327 if (tty) {
328 count = tty->count;
329 tty_kref_put(tty);
332 return count;
335 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
337 static inline void
338 capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
339 static inline void capincci_free_minor(struct capincci *np) { }
341 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
343 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
345 struct capincci *np;
347 np = kzalloc(sizeof(*np), GFP_KERNEL);
348 if (!np)
349 return NULL;
350 np->ncci = ncci;
351 np->cdev = cdev;
353 capincci_alloc_minor(cdev, np);
355 list_add_tail(&np->list, &cdev->nccis);
357 return np;
360 static void capincci_free(struct capidev *cdev, u32 ncci)
362 struct capincci *np, *tmp;
364 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
365 if (ncci == 0xffffffff || np->ncci == ncci) {
366 capincci_free_minor(np);
367 list_del(&np->list);
368 kfree(np);
372 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
373 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
375 struct capincci *np;
377 list_for_each_entry(np, &cdev->nccis, list)
378 if (np->ncci == ncci)
379 return np;
380 return NULL;
383 /* -------- handle data queue --------------------------------------- */
385 static struct sk_buff *
386 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
388 struct sk_buff *nskb;
389 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_KERNEL);
390 if (nskb) {
391 u16 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
392 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
393 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
394 capimsg_setu16(s, 2, mp->ap->applid);
395 capimsg_setu8 (s, 4, CAPI_DATA_B3);
396 capimsg_setu8 (s, 5, CAPI_RESP);
397 capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
398 capimsg_setu32(s, 8, mp->ncci);
399 capimsg_setu16(s, 12, datahandle);
401 return nskb;
404 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
406 unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
407 struct tty_struct *tty;
408 struct sk_buff *nskb;
409 u16 errcode, datahandle;
410 struct tty_ldisc *ld;
411 int ret = -1;
413 tty = tty_port_tty_get(&mp->port);
414 if (!tty) {
415 pr_debug("capi: currently no receiver\n");
416 return -1;
419 ld = tty_ldisc_ref(tty);
420 if (!ld) {
421 /* fatal error, do not requeue */
422 ret = 0;
423 kfree_skb(skb);
424 goto deref_tty;
427 if (ld->ops->receive_buf == NULL) {
428 pr_debug("capi: ldisc has no receive_buf function\n");
429 /* fatal error, do not requeue */
430 goto free_skb;
432 if (mp->ttyinstop) {
433 pr_debug("capi: recv tty throttled\n");
434 goto deref_ldisc;
437 if (tty->receive_room < datalen) {
438 pr_debug("capi: no room in tty\n");
439 goto deref_ldisc;
442 nskb = gen_data_b3_resp_for(mp, skb);
443 if (!nskb) {
444 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
445 goto deref_ldisc;
448 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
450 errcode = capi20_put_message(mp->ap, nskb);
452 if (errcode == CAPI_NOERROR) {
453 skb_pull(skb, CAPIMSG_LEN(skb->data));
454 pr_debug("capi: DATA_B3_RESP %u len=%d => ldisc\n",
455 datahandle, skb->len);
456 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
457 } else {
458 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
459 errcode);
460 kfree_skb(nskb);
462 if (errcode == CAPI_SENDQUEUEFULL)
463 goto deref_ldisc;
466 free_skb:
467 ret = 0;
468 kfree_skb(skb);
470 deref_ldisc:
471 tty_ldisc_deref(ld);
473 deref_tty:
474 tty_kref_put(tty);
475 return ret;
478 static void handle_minor_recv(struct capiminor *mp)
480 struct sk_buff *skb;
482 while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
483 if (handle_recv_skb(mp, skb) < 0) {
484 skb_queue_head(&mp->inqueue, skb);
485 return;
489 static void handle_minor_send(struct capiminor *mp)
491 struct tty_struct *tty;
492 struct sk_buff *skb;
493 u16 len;
494 u16 errcode;
495 u16 datahandle;
497 tty = tty_port_tty_get(&mp->port);
498 if (!tty)
499 return;
501 if (mp->ttyoutstop) {
502 pr_debug("capi: send: tty stopped\n");
503 tty_kref_put(tty);
504 return;
507 while (1) {
508 spin_lock_bh(&mp->outlock);
509 skb = __skb_dequeue(&mp->outqueue);
510 if (!skb) {
511 spin_unlock_bh(&mp->outlock);
512 break;
514 len = (u16)skb->len;
515 mp->outbytes -= len;
516 spin_unlock_bh(&mp->outlock);
518 datahandle = atomic_inc_return(&mp->datahandle);
519 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
520 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
521 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
522 capimsg_setu16(skb->data, 2, mp->ap->applid);
523 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
524 capimsg_setu8 (skb->data, 5, CAPI_REQ);
525 capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
526 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
527 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
528 capimsg_setu16(skb->data, 16, len); /* Data length */
529 capimsg_setu16(skb->data, 18, datahandle);
530 capimsg_setu16(skb->data, 20, 0); /* Flags */
532 if (capiminor_add_ack(mp, datahandle) < 0) {
533 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
535 spin_lock_bh(&mp->outlock);
536 __skb_queue_head(&mp->outqueue, skb);
537 mp->outbytes += len;
538 spin_unlock_bh(&mp->outlock);
540 break;
542 errcode = capi20_put_message(mp->ap, skb);
543 if (errcode == CAPI_NOERROR) {
544 pr_debug("capi: DATA_B3_REQ %u len=%u\n",
545 datahandle, len);
546 continue;
548 capiminor_del_ack(mp, datahandle);
550 if (errcode == CAPI_SENDQUEUEFULL) {
551 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
553 spin_lock_bh(&mp->outlock);
554 __skb_queue_head(&mp->outqueue, skb);
555 mp->outbytes += len;
556 spin_unlock_bh(&mp->outlock);
558 break;
561 /* ups, drop packet */
562 printk(KERN_ERR "capi: put_message = %x\n", errcode);
563 kfree_skb(skb);
565 tty_kref_put(tty);
568 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
569 /* -------- function called by lower level -------------------------- */
571 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
573 struct capidev *cdev = ap->private;
574 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
575 struct capiminor *mp;
576 u16 datahandle;
577 struct capincci *np;
578 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
580 mutex_lock(&cdev->lock);
582 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
583 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
584 if ((info & 0xff00) == 0)
585 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
587 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
588 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
590 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
591 skb_queue_tail(&cdev->recvqueue, skb);
592 wake_up_interruptible(&cdev->recvwait);
593 goto unlock_out;
596 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
597 skb_queue_tail(&cdev->recvqueue, skb);
598 wake_up_interruptible(&cdev->recvwait);
600 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
602 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
603 if (!np) {
604 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
605 skb_queue_tail(&cdev->recvqueue, skb);
606 wake_up_interruptible(&cdev->recvwait);
607 goto unlock_out;
610 mp = np->minorp;
611 if (!mp) {
612 skb_queue_tail(&cdev->recvqueue, skb);
613 wake_up_interruptible(&cdev->recvwait);
614 goto unlock_out;
616 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
617 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
618 pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
619 datahandle, skb->len-CAPIMSG_LEN(skb->data));
620 skb_queue_tail(&mp->inqueue, skb);
622 handle_minor_recv(mp);
624 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
626 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
627 pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
628 datahandle,
629 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 2));
630 kfree_skb(skb);
631 capiminor_del_ack(mp, datahandle);
632 tty_port_tty_wakeup(&mp->port);
633 handle_minor_send(mp);
635 } else {
636 /* ups, let capi application handle it :-) */
637 skb_queue_tail(&cdev->recvqueue, skb);
638 wake_up_interruptible(&cdev->recvwait);
640 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
642 unlock_out:
643 mutex_unlock(&cdev->lock);
646 /* -------- file_operations for capidev ----------------------------- */
648 static ssize_t
649 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
651 struct capidev *cdev = file->private_data;
652 struct sk_buff *skb;
653 size_t copied;
654 int err;
656 if (!cdev->ap.applid)
657 return -ENODEV;
659 skb = skb_dequeue(&cdev->recvqueue);
660 if (!skb) {
661 if (file->f_flags & O_NONBLOCK)
662 return -EAGAIN;
663 err = wait_event_interruptible(cdev->recvwait,
664 (skb = skb_dequeue(&cdev->recvqueue)));
665 if (err)
666 return err;
668 if (skb->len > count) {
669 skb_queue_head(&cdev->recvqueue, skb);
670 return -EMSGSIZE;
672 if (copy_to_user(buf, skb->data, skb->len)) {
673 skb_queue_head(&cdev->recvqueue, skb);
674 return -EFAULT;
676 copied = skb->len;
678 kfree_skb(skb);
680 return copied;
683 static ssize_t
684 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
686 struct capidev *cdev = file->private_data;
687 struct sk_buff *skb;
688 u16 mlen;
690 if (!cdev->ap.applid)
691 return -ENODEV;
693 if (count < CAPIMSG_BASELEN)
694 return -EINVAL;
696 skb = alloc_skb(count, GFP_USER);
697 if (!skb)
698 return -ENOMEM;
700 if (copy_from_user(skb_put(skb, count), buf, count)) {
701 kfree_skb(skb);
702 return -EFAULT;
704 mlen = CAPIMSG_LEN(skb->data);
705 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
706 if (count < CAPI_DATA_B3_REQ_LEN ||
707 (size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
708 kfree_skb(skb);
709 return -EINVAL;
711 } else {
712 if (mlen != count) {
713 kfree_skb(skb);
714 return -EINVAL;
717 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
719 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
720 if (count < CAPI_DISCONNECT_B3_RESP_LEN) {
721 kfree_skb(skb);
722 return -EINVAL;
724 mutex_lock(&cdev->lock);
725 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
726 mutex_unlock(&cdev->lock);
729 cdev->errcode = capi20_put_message(&cdev->ap, skb);
731 if (cdev->errcode) {
732 kfree_skb(skb);
733 return -EIO;
735 return count;
738 static __poll_t
739 capi_poll(struct file *file, poll_table *wait)
741 struct capidev *cdev = file->private_data;
742 __poll_t mask = 0;
744 if (!cdev->ap.applid)
745 return EPOLLERR;
747 poll_wait(file, &(cdev->recvwait), wait);
748 mask = EPOLLOUT | EPOLLWRNORM;
749 if (!skb_queue_empty_lockless(&cdev->recvqueue))
750 mask |= EPOLLIN | EPOLLRDNORM;
751 return mask;
754 static int
755 capi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
757 struct capidev *cdev = file->private_data;
758 capi_ioctl_struct data;
759 int retval = -EINVAL;
760 void __user *argp = (void __user *)arg;
762 switch (cmd) {
763 case CAPI_REGISTER:
764 mutex_lock(&cdev->lock);
766 if (cdev->ap.applid) {
767 retval = -EEXIST;
768 goto register_out;
770 if (copy_from_user(&cdev->ap.rparam, argp,
771 sizeof(struct capi_register_params))) {
772 retval = -EFAULT;
773 goto register_out;
775 cdev->ap.private = cdev;
776 cdev->ap.recv_message = capi_recv_message;
777 cdev->errcode = capi20_register(&cdev->ap);
778 retval = (int)cdev->ap.applid;
779 if (cdev->errcode) {
780 cdev->ap.applid = 0;
781 retval = -EIO;
784 register_out:
785 mutex_unlock(&cdev->lock);
786 return retval;
788 case CAPI_GET_VERSION:
789 if (copy_from_user(&data.contr, argp,
790 sizeof(data.contr)))
791 return -EFAULT;
792 cdev->errcode = capi20_get_version(data.contr, &data.version);
793 if (cdev->errcode)
794 return -EIO;
795 if (copy_to_user(argp, &data.version,
796 sizeof(data.version)))
797 return -EFAULT;
798 return 0;
800 case CAPI_GET_SERIAL:
801 if (copy_from_user(&data.contr, argp,
802 sizeof(data.contr)))
803 return -EFAULT;
804 cdev->errcode = capi20_get_serial(data.contr, data.serial);
805 if (cdev->errcode)
806 return -EIO;
807 if (copy_to_user(argp, data.serial,
808 sizeof(data.serial)))
809 return -EFAULT;
810 return 0;
812 case CAPI_GET_PROFILE:
813 if (copy_from_user(&data.contr, argp,
814 sizeof(data.contr)))
815 return -EFAULT;
817 if (data.contr == 0) {
818 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
819 if (cdev->errcode)
820 return -EIO;
822 retval = copy_to_user(argp,
823 &data.profile.ncontroller,
824 sizeof(data.profile.ncontroller));
826 } else {
827 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
828 if (cdev->errcode)
829 return -EIO;
831 retval = copy_to_user(argp, &data.profile,
832 sizeof(data.profile));
834 if (retval)
835 return -EFAULT;
836 return 0;
838 case CAPI_GET_MANUFACTURER:
839 if (copy_from_user(&data.contr, argp,
840 sizeof(data.contr)))
841 return -EFAULT;
842 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
843 if (cdev->errcode)
844 return -EIO;
846 if (copy_to_user(argp, data.manufacturer,
847 sizeof(data.manufacturer)))
848 return -EFAULT;
850 return 0;
852 case CAPI_GET_ERRCODE:
853 data.errcode = cdev->errcode;
854 cdev->errcode = CAPI_NOERROR;
855 if (arg) {
856 if (copy_to_user(argp, &data.errcode,
857 sizeof(data.errcode)))
858 return -EFAULT;
860 return data.errcode;
862 case CAPI_INSTALLED:
863 if (capi20_isinstalled() == CAPI_NOERROR)
864 return 0;
865 return -ENXIO;
867 case CAPI_MANUFACTURER_CMD: {
868 struct capi_manufacturer_cmd mcmd;
869 if (!capable(CAP_SYS_ADMIN))
870 return -EPERM;
871 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
872 return -EFAULT;
873 return capi20_manufacturer(mcmd.cmd, mcmd.data);
875 case CAPI_SET_FLAGS:
876 case CAPI_CLR_FLAGS: {
877 unsigned userflags;
879 if (copy_from_user(&userflags, argp, sizeof(userflags)))
880 return -EFAULT;
882 mutex_lock(&cdev->lock);
883 if (cmd == CAPI_SET_FLAGS)
884 cdev->userflags |= userflags;
885 else
886 cdev->userflags &= ~userflags;
887 mutex_unlock(&cdev->lock);
888 return 0;
890 case CAPI_GET_FLAGS:
891 if (copy_to_user(argp, &cdev->userflags,
892 sizeof(cdev->userflags)))
893 return -EFAULT;
894 return 0;
896 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
897 case CAPI_NCCI_OPENCOUNT:
898 return 0;
900 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
901 case CAPI_NCCI_OPENCOUNT: {
902 struct capincci *nccip;
903 unsigned ncci;
904 int count = 0;
906 if (copy_from_user(&ncci, argp, sizeof(ncci)))
907 return -EFAULT;
909 mutex_lock(&cdev->lock);
910 nccip = capincci_find(cdev, (u32)ncci);
911 if (nccip)
912 count = capincci_minor_opencount(nccip);
913 mutex_unlock(&cdev->lock);
914 return count;
917 case CAPI_NCCI_GETUNIT: {
918 struct capincci *nccip;
919 struct capiminor *mp;
920 unsigned ncci;
921 int unit = -ESRCH;
923 if (copy_from_user(&ncci, argp, sizeof(ncci)))
924 return -EFAULT;
926 mutex_lock(&cdev->lock);
927 nccip = capincci_find(cdev, (u32)ncci);
928 if (nccip) {
929 mp = nccip->minorp;
930 if (mp)
931 unit = mp->minor;
933 mutex_unlock(&cdev->lock);
934 return unit;
936 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
938 default:
939 return -EINVAL;
943 static long
944 capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
946 int ret;
948 mutex_lock(&capi_mutex);
949 ret = capi_ioctl(file, cmd, arg);
950 mutex_unlock(&capi_mutex);
952 return ret;
955 #ifdef CONFIG_COMPAT
956 static long
957 capi_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
959 int ret;
961 if (cmd == CAPI_MANUFACTURER_CMD) {
962 struct {
963 compat_ulong_t cmd;
964 compat_uptr_t data;
965 } mcmd32;
967 if (!capable(CAP_SYS_ADMIN))
968 return -EPERM;
969 if (copy_from_user(&mcmd32, compat_ptr(arg), sizeof(mcmd32)))
970 return -EFAULT;
972 mutex_lock(&capi_mutex);
973 ret = capi20_manufacturer(mcmd32.cmd, compat_ptr(mcmd32.data));
974 mutex_unlock(&capi_mutex);
976 return ret;
979 return capi_unlocked_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
981 #endif
983 static int capi_open(struct inode *inode, struct file *file)
985 struct capidev *cdev;
987 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
988 if (!cdev)
989 return -ENOMEM;
991 mutex_init(&cdev->lock);
992 skb_queue_head_init(&cdev->recvqueue);
993 init_waitqueue_head(&cdev->recvwait);
994 INIT_LIST_HEAD(&cdev->nccis);
995 file->private_data = cdev;
997 mutex_lock(&capidev_list_lock);
998 list_add_tail(&cdev->list, &capidev_list);
999 mutex_unlock(&capidev_list_lock);
1001 return stream_open(inode, file);
1004 static int capi_release(struct inode *inode, struct file *file)
1006 struct capidev *cdev = file->private_data;
1008 mutex_lock(&capidev_list_lock);
1009 list_del(&cdev->list);
1010 mutex_unlock(&capidev_list_lock);
1012 if (cdev->ap.applid)
1013 capi20_release(&cdev->ap);
1014 skb_queue_purge(&cdev->recvqueue);
1015 capincci_free(cdev, 0xffffffff);
1017 kfree(cdev);
1018 return 0;
1021 static const struct file_operations capi_fops =
1023 .owner = THIS_MODULE,
1024 .llseek = no_llseek,
1025 .read = capi_read,
1026 .write = capi_write,
1027 .poll = capi_poll,
1028 .unlocked_ioctl = capi_unlocked_ioctl,
1029 #ifdef CONFIG_COMPAT
1030 .compat_ioctl = capi_compat_ioctl,
1031 #endif
1032 .open = capi_open,
1033 .release = capi_release,
1036 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1037 /* -------- tty_operations for capincci ----------------------------- */
1039 static int
1040 capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1042 struct capiminor *mp = capiminor_get(tty->index);
1043 int ret = tty_standard_install(driver, tty);
1045 if (ret == 0)
1046 tty->driver_data = mp;
1047 else
1048 capiminor_put(mp);
1049 return ret;
1052 static void capinc_tty_cleanup(struct tty_struct *tty)
1054 struct capiminor *mp = tty->driver_data;
1055 tty->driver_data = NULL;
1056 capiminor_put(mp);
1059 static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
1061 struct capiminor *mp = tty->driver_data;
1062 int err;
1064 err = tty_port_open(&mp->port, tty, filp);
1065 if (err)
1066 return err;
1068 handle_minor_recv(mp);
1069 return 0;
1072 static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
1074 struct capiminor *mp = tty->driver_data;
1076 tty_port_close(&mp->port, tty, filp);
1079 static int capinc_tty_write(struct tty_struct *tty,
1080 const unsigned char *buf, int count)
1082 struct capiminor *mp = tty->driver_data;
1083 struct sk_buff *skb;
1085 pr_debug("capinc_tty_write(count=%d)\n", count);
1087 spin_lock_bh(&mp->outlock);
1088 skb = mp->outskb;
1089 if (skb) {
1090 mp->outskb = NULL;
1091 __skb_queue_tail(&mp->outqueue, skb);
1092 mp->outbytes += skb->len;
1095 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + count, GFP_ATOMIC);
1096 if (!skb) {
1097 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1098 spin_unlock_bh(&mp->outlock);
1099 return -ENOMEM;
1102 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1103 skb_put_data(skb, buf, count);
1105 __skb_queue_tail(&mp->outqueue, skb);
1106 mp->outbytes += skb->len;
1107 spin_unlock_bh(&mp->outlock);
1109 handle_minor_send(mp);
1111 return count;
1114 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1116 struct capiminor *mp = tty->driver_data;
1117 bool invoke_send = false;
1118 struct sk_buff *skb;
1119 int ret = 1;
1121 pr_debug("capinc_put_char(%u)\n", ch);
1123 spin_lock_bh(&mp->outlock);
1124 skb = mp->outskb;
1125 if (skb) {
1126 if (skb_tailroom(skb) > 0) {
1127 skb_put_u8(skb, ch);
1128 goto unlock_out;
1130 mp->outskb = NULL;
1131 __skb_queue_tail(&mp->outqueue, skb);
1132 mp->outbytes += skb->len;
1133 invoke_send = true;
1136 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1137 if (skb) {
1138 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1139 skb_put_u8(skb, ch);
1140 mp->outskb = skb;
1141 } else {
1142 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1143 ret = 0;
1146 unlock_out:
1147 spin_unlock_bh(&mp->outlock);
1149 if (invoke_send)
1150 handle_minor_send(mp);
1152 return ret;
1155 static void capinc_tty_flush_chars(struct tty_struct *tty)
1157 struct capiminor *mp = tty->driver_data;
1158 struct sk_buff *skb;
1160 pr_debug("capinc_tty_flush_chars\n");
1162 spin_lock_bh(&mp->outlock);
1163 skb = mp->outskb;
1164 if (skb) {
1165 mp->outskb = NULL;
1166 __skb_queue_tail(&mp->outqueue, skb);
1167 mp->outbytes += skb->len;
1168 spin_unlock_bh(&mp->outlock);
1170 handle_minor_send(mp);
1171 } else
1172 spin_unlock_bh(&mp->outlock);
1174 handle_minor_recv(mp);
1177 static int capinc_tty_write_room(struct tty_struct *tty)
1179 struct capiminor *mp = tty->driver_data;
1180 int room;
1182 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1183 room *= CAPI_MAX_BLKSIZE;
1184 pr_debug("capinc_tty_write_room = %d\n", room);
1185 return room;
1188 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1190 struct capiminor *mp = tty->driver_data;
1192 pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1193 mp->outbytes, mp->nack,
1194 skb_queue_len(&mp->outqueue),
1195 skb_queue_len(&mp->inqueue));
1196 return mp->outbytes;
1199 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1201 pr_debug("capinc_tty_set_termios\n");
1204 static void capinc_tty_throttle(struct tty_struct *tty)
1206 struct capiminor *mp = tty->driver_data;
1207 pr_debug("capinc_tty_throttle\n");
1208 mp->ttyinstop = 1;
1211 static void capinc_tty_unthrottle(struct tty_struct *tty)
1213 struct capiminor *mp = tty->driver_data;
1215 pr_debug("capinc_tty_unthrottle\n");
1216 mp->ttyinstop = 0;
1217 handle_minor_recv(mp);
1220 static void capinc_tty_stop(struct tty_struct *tty)
1222 struct capiminor *mp = tty->driver_data;
1224 pr_debug("capinc_tty_stop\n");
1225 mp->ttyoutstop = 1;
1228 static void capinc_tty_start(struct tty_struct *tty)
1230 struct capiminor *mp = tty->driver_data;
1232 pr_debug("capinc_tty_start\n");
1233 mp->ttyoutstop = 0;
1234 handle_minor_send(mp);
1237 static void capinc_tty_hangup(struct tty_struct *tty)
1239 struct capiminor *mp = tty->driver_data;
1241 pr_debug("capinc_tty_hangup\n");
1242 tty_port_hangup(&mp->port);
1245 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1247 pr_debug("capinc_tty_break_ctl(%d)\n", state);
1248 return 0;
1251 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1253 pr_debug("capinc_tty_flush_buffer\n");
1256 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1258 pr_debug("capinc_tty_set_ldisc\n");
1261 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1263 pr_debug("capinc_tty_send_xchar(%d)\n", ch);
1266 static const struct tty_operations capinc_ops = {
1267 .open = capinc_tty_open,
1268 .close = capinc_tty_close,
1269 .write = capinc_tty_write,
1270 .put_char = capinc_tty_put_char,
1271 .flush_chars = capinc_tty_flush_chars,
1272 .write_room = capinc_tty_write_room,
1273 .chars_in_buffer = capinc_tty_chars_in_buffer,
1274 .set_termios = capinc_tty_set_termios,
1275 .throttle = capinc_tty_throttle,
1276 .unthrottle = capinc_tty_unthrottle,
1277 .stop = capinc_tty_stop,
1278 .start = capinc_tty_start,
1279 .hangup = capinc_tty_hangup,
1280 .break_ctl = capinc_tty_break_ctl,
1281 .flush_buffer = capinc_tty_flush_buffer,
1282 .set_ldisc = capinc_tty_set_ldisc,
1283 .send_xchar = capinc_tty_send_xchar,
1284 .install = capinc_tty_install,
1285 .cleanup = capinc_tty_cleanup,
1288 static int __init capinc_tty_init(void)
1290 struct tty_driver *drv;
1291 int err;
1293 if (capi_ttyminors > CAPINC_MAX_PORTS)
1294 capi_ttyminors = CAPINC_MAX_PORTS;
1295 if (capi_ttyminors <= 0)
1296 capi_ttyminors = CAPINC_NR_PORTS;
1298 capiminors = kcalloc(capi_ttyminors, sizeof(struct capiminor *),
1299 GFP_KERNEL);
1300 if (!capiminors)
1301 return -ENOMEM;
1303 drv = alloc_tty_driver(capi_ttyminors);
1304 if (!drv) {
1305 kfree(capiminors);
1306 return -ENOMEM;
1308 drv->driver_name = "capi_nc";
1309 drv->name = "capi!";
1310 drv->major = 0;
1311 drv->minor_start = 0;
1312 drv->type = TTY_DRIVER_TYPE_SERIAL;
1313 drv->subtype = SERIAL_TYPE_NORMAL;
1314 drv->init_termios = tty_std_termios;
1315 drv->init_termios.c_iflag = ICRNL;
1316 drv->init_termios.c_oflag = OPOST | ONLCR;
1317 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1318 drv->init_termios.c_lflag = 0;
1319 drv->flags =
1320 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1321 TTY_DRIVER_DYNAMIC_DEV;
1322 tty_set_operations(drv, &capinc_ops);
1324 err = tty_register_driver(drv);
1325 if (err) {
1326 put_tty_driver(drv);
1327 kfree(capiminors);
1328 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1329 return err;
1331 capinc_tty_driver = drv;
1332 return 0;
1335 static void __exit capinc_tty_exit(void)
1337 tty_unregister_driver(capinc_tty_driver);
1338 put_tty_driver(capinc_tty_driver);
1339 kfree(capiminors);
1342 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1344 static inline int capinc_tty_init(void)
1346 return 0;
1349 static inline void capinc_tty_exit(void) { }
1351 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1353 /* -------- /proc functions ----------------------------------------- */
1356 * /proc/capi/capi20:
1357 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1359 static int __maybe_unused capi20_proc_show(struct seq_file *m, void *v)
1361 struct capidev *cdev;
1362 struct list_head *l;
1364 mutex_lock(&capidev_list_lock);
1365 list_for_each(l, &capidev_list) {
1366 cdev = list_entry(l, struct capidev, list);
1367 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1368 cdev->ap.applid,
1369 cdev->ap.nrecvctlpkt,
1370 cdev->ap.nrecvdatapkt,
1371 cdev->ap.nsentctlpkt,
1372 cdev->ap.nsentdatapkt);
1374 mutex_unlock(&capidev_list_lock);
1375 return 0;
1379 * /proc/capi/capi20ncci:
1380 * applid ncci
1382 static int __maybe_unused capi20ncci_proc_show(struct seq_file *m, void *v)
1384 struct capidev *cdev;
1385 struct capincci *np;
1387 mutex_lock(&capidev_list_lock);
1388 list_for_each_entry(cdev, &capidev_list, list) {
1389 mutex_lock(&cdev->lock);
1390 list_for_each_entry(np, &cdev->nccis, list)
1391 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
1392 mutex_unlock(&cdev->lock);
1394 mutex_unlock(&capidev_list_lock);
1395 return 0;
1398 static void __init proc_init(void)
1400 proc_create_single("capi/capi20", 0, NULL, capi20_proc_show);
1401 proc_create_single("capi/capi20ncci", 0, NULL, capi20ncci_proc_show);
1404 static void __exit proc_exit(void)
1406 remove_proc_entry("capi/capi20", NULL);
1407 remove_proc_entry("capi/capi20ncci", NULL);
1410 /* -------- init function and module interface ---------------------- */
1413 static int __init capi_init(void)
1415 const char *compileinfo;
1416 int major_ret;
1417 int ret;
1419 ret = kcapi_init();
1420 if (ret)
1421 return ret;
1423 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1424 if (major_ret < 0) {
1425 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1426 kcapi_exit();
1427 return major_ret;
1429 capi_class = class_create(THIS_MODULE, "capi");
1430 if (IS_ERR(capi_class)) {
1431 unregister_chrdev(capi_major, "capi20");
1432 kcapi_exit();
1433 return PTR_ERR(capi_class);
1436 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi20");
1438 if (capinc_tty_init() < 0) {
1439 device_destroy(capi_class, MKDEV(capi_major, 0));
1440 class_destroy(capi_class);
1441 unregister_chrdev(capi_major, "capi20");
1442 kcapi_exit();
1443 return -ENOMEM;
1446 proc_init();
1448 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1449 compileinfo = " (middleware)";
1450 #else
1451 compileinfo = " (no middleware)";
1452 #endif
1453 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1454 capi_major, compileinfo);
1456 return 0;
1459 static void __exit capi_exit(void)
1461 proc_exit();
1463 device_destroy(capi_class, MKDEV(capi_major, 0));
1464 class_destroy(capi_class);
1465 unregister_chrdev(capi_major, "capi20");
1467 capinc_tty_exit();
1469 kcapi_exit();
1472 module_init(capi_init);
1473 module_exit(capi_exit);