kill capinc_tty_ioctl()
[linux/fpc-iii.git] / drivers / isdn / capi / capi.c
blobe1da70a9530c8ce9f64ad78702f3b0a70b9af17a
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 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
43 MODULE_AUTHOR("Carsten Paeth");
44 MODULE_LICENSE("GPL");
46 /* -------- driver information -------------------------------------- */
48 static DEFINE_MUTEX(capi_mutex);
49 static struct class *capi_class;
50 static int capi_major = 68; /* allocated */
52 module_param_named(major, capi_major, uint, 0);
54 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
55 #define CAPINC_NR_PORTS 32
56 #define CAPINC_MAX_PORTS 256
58 static int capi_ttyminors = CAPINC_NR_PORTS;
60 module_param_named(ttyminors, capi_ttyminors, uint, 0);
61 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
63 /* -------- defines ------------------------------------------------- */
65 #define CAPINC_MAX_RECVQUEUE 10
66 #define CAPINC_MAX_SENDQUEUE 10
67 #define CAPI_MAX_BLKSIZE 2048
69 /* -------- data structures ----------------------------------------- */
71 struct capidev;
72 struct capincci;
73 struct capiminor;
75 struct ackqueue_entry {
76 struct list_head list;
77 u16 datahandle;
80 struct capiminor {
81 unsigned int minor;
83 struct capi20_appl *ap;
84 u32 ncci;
85 atomic_t datahandle;
86 atomic_t msgid;
88 struct tty_port port;
89 int ttyinstop;
90 int ttyoutstop;
92 struct sk_buff_head inqueue;
94 struct sk_buff_head outqueue;
95 int outbytes;
96 struct sk_buff *outskb;
97 spinlock_t outlock;
99 /* transmit path */
100 struct list_head ackqueue;
101 int nack;
102 spinlock_t ackqlock;
105 struct capincci {
106 struct list_head list;
107 u32 ncci;
108 struct capidev *cdev;
109 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
110 struct capiminor *minorp;
111 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
114 struct capidev {
115 struct list_head list;
116 struct capi20_appl ap;
117 u16 errcode;
118 unsigned userflags;
120 struct sk_buff_head recvqueue;
121 wait_queue_head_t recvwait;
123 struct list_head nccis;
125 struct mutex lock;
128 /* -------- global variables ---------------------------------------- */
130 static DEFINE_MUTEX(capidev_list_lock);
131 static LIST_HEAD(capidev_list);
133 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
135 static DEFINE_SPINLOCK(capiminors_lock);
136 static struct capiminor **capiminors;
138 static struct tty_driver *capinc_tty_driver;
140 /* -------- datahandles --------------------------------------------- */
142 static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
144 struct ackqueue_entry *n;
146 n = kmalloc(sizeof(*n), GFP_ATOMIC);
147 if (unlikely(!n)) {
148 printk(KERN_ERR "capi: alloc datahandle failed\n");
149 return -1;
151 n->datahandle = datahandle;
152 INIT_LIST_HEAD(&n->list);
153 spin_lock_bh(&mp->ackqlock);
154 list_add_tail(&n->list, &mp->ackqueue);
155 mp->nack++;
156 spin_unlock_bh(&mp->ackqlock);
157 return 0;
160 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
162 struct ackqueue_entry *p, *tmp;
164 spin_lock_bh(&mp->ackqlock);
165 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
166 if (p->datahandle == datahandle) {
167 list_del(&p->list);
168 mp->nack--;
169 spin_unlock_bh(&mp->ackqlock);
170 kfree(p);
171 return 0;
174 spin_unlock_bh(&mp->ackqlock);
175 return -1;
178 static void capiminor_del_all_ack(struct capiminor *mp)
180 struct ackqueue_entry *p, *tmp;
182 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
183 list_del(&p->list);
184 kfree(p);
185 mp->nack--;
190 /* -------- struct capiminor ---------------------------------------- */
192 static void capiminor_destroy(struct tty_port *port)
194 struct capiminor *mp = container_of(port, struct capiminor, port);
196 kfree_skb(mp->outskb);
197 skb_queue_purge(&mp->inqueue);
198 skb_queue_purge(&mp->outqueue);
199 capiminor_del_all_ack(mp);
200 kfree(mp);
203 static const struct tty_port_operations capiminor_port_ops = {
204 .destruct = capiminor_destroy,
207 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
209 struct capiminor *mp;
210 struct device *dev;
211 unsigned int minor;
213 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
214 if (!mp) {
215 printk(KERN_ERR "capi: can't alloc capiminor\n");
216 return NULL;
219 mp->ap = ap;
220 mp->ncci = ncci;
221 INIT_LIST_HEAD(&mp->ackqueue);
222 spin_lock_init(&mp->ackqlock);
224 skb_queue_head_init(&mp->inqueue);
225 skb_queue_head_init(&mp->outqueue);
226 spin_lock_init(&mp->outlock);
228 tty_port_init(&mp->port);
229 mp->port.ops = &capiminor_port_ops;
231 /* Allocate the least unused minor number. */
232 spin_lock(&capiminors_lock);
233 for (minor = 0; minor < capi_ttyminors; minor++)
234 if (!capiminors[minor]) {
235 capiminors[minor] = mp;
236 break;
238 spin_unlock(&capiminors_lock);
240 if (minor == capi_ttyminors) {
241 printk(KERN_NOTICE "capi: out of minors\n");
242 goto err_out1;
245 mp->minor = minor;
247 dev = tty_port_register_device(&mp->port, capinc_tty_driver, minor,
248 NULL);
249 if (IS_ERR(dev))
250 goto err_out2;
252 return mp;
254 err_out2:
255 spin_lock(&capiminors_lock);
256 capiminors[minor] = NULL;
257 spin_unlock(&capiminors_lock);
259 err_out1:
260 tty_port_put(&mp->port);
261 return NULL;
264 static struct capiminor *capiminor_get(unsigned int minor)
266 struct capiminor *mp;
268 spin_lock(&capiminors_lock);
269 mp = capiminors[minor];
270 if (mp)
271 tty_port_get(&mp->port);
272 spin_unlock(&capiminors_lock);
274 return mp;
277 static inline void capiminor_put(struct capiminor *mp)
279 tty_port_put(&mp->port);
282 static void capiminor_free(struct capiminor *mp)
284 tty_unregister_device(capinc_tty_driver, mp->minor);
286 spin_lock(&capiminors_lock);
287 capiminors[mp->minor] = NULL;
288 spin_unlock(&capiminors_lock);
290 capiminor_put(mp);
293 /* -------- struct capincci ----------------------------------------- */
295 static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
297 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
298 np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
301 static void capincci_free_minor(struct capincci *np)
303 struct capiminor *mp = np->minorp;
304 struct tty_struct *tty;
306 if (mp) {
307 tty = tty_port_tty_get(&mp->port);
308 if (tty) {
309 tty_vhangup(tty);
310 tty_kref_put(tty);
313 capiminor_free(mp);
317 static inline unsigned int capincci_minor_opencount(struct capincci *np)
319 struct capiminor *mp = np->minorp;
320 unsigned int count = 0;
321 struct tty_struct *tty;
323 if (mp) {
324 tty = tty_port_tty_get(&mp->port);
325 if (tty) {
326 count = tty->count;
327 tty_kref_put(tty);
330 return count;
333 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
335 static inline void
336 capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
337 static inline void capincci_free_minor(struct capincci *np) { }
339 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
341 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
343 struct capincci *np;
345 np = kzalloc(sizeof(*np), GFP_KERNEL);
346 if (!np)
347 return NULL;
348 np->ncci = ncci;
349 np->cdev = cdev;
351 capincci_alloc_minor(cdev, np);
353 list_add_tail(&np->list, &cdev->nccis);
355 return np;
358 static void capincci_free(struct capidev *cdev, u32 ncci)
360 struct capincci *np, *tmp;
362 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
363 if (ncci == 0xffffffff || np->ncci == ncci) {
364 capincci_free_minor(np);
365 list_del(&np->list);
366 kfree(np);
370 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
371 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
373 struct capincci *np;
375 list_for_each_entry(np, &cdev->nccis, list)
376 if (np->ncci == ncci)
377 return np;
378 return NULL;
381 /* -------- handle data queue --------------------------------------- */
383 static struct sk_buff *
384 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
386 struct sk_buff *nskb;
387 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_KERNEL);
388 if (nskb) {
389 u16 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
390 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
391 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
392 capimsg_setu16(s, 2, mp->ap->applid);
393 capimsg_setu8 (s, 4, CAPI_DATA_B3);
394 capimsg_setu8 (s, 5, CAPI_RESP);
395 capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
396 capimsg_setu32(s, 8, mp->ncci);
397 capimsg_setu16(s, 12, datahandle);
399 return nskb;
402 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
404 unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
405 struct tty_struct *tty;
406 struct sk_buff *nskb;
407 u16 errcode, datahandle;
408 struct tty_ldisc *ld;
409 int ret = -1;
411 tty = tty_port_tty_get(&mp->port);
412 if (!tty) {
413 pr_debug("capi: currently no receiver\n");
414 return -1;
417 ld = tty_ldisc_ref(tty);
418 if (!ld) {
419 /* fatal error, do not requeue */
420 ret = 0;
421 kfree_skb(skb);
422 goto deref_tty;
425 if (ld->ops->receive_buf == NULL) {
426 pr_debug("capi: ldisc has no receive_buf function\n");
427 /* fatal error, do not requeue */
428 goto free_skb;
430 if (mp->ttyinstop) {
431 pr_debug("capi: recv tty throttled\n");
432 goto deref_ldisc;
435 if (tty->receive_room < datalen) {
436 pr_debug("capi: no room in tty\n");
437 goto deref_ldisc;
440 nskb = gen_data_b3_resp_for(mp, skb);
441 if (!nskb) {
442 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
443 goto deref_ldisc;
446 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
448 errcode = capi20_put_message(mp->ap, nskb);
450 if (errcode == CAPI_NOERROR) {
451 skb_pull(skb, CAPIMSG_LEN(skb->data));
452 pr_debug("capi: DATA_B3_RESP %u len=%d => ldisc\n",
453 datahandle, skb->len);
454 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
455 } else {
456 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
457 errcode);
458 kfree_skb(nskb);
460 if (errcode == CAPI_SENDQUEUEFULL)
461 goto deref_ldisc;
464 free_skb:
465 ret = 0;
466 kfree_skb(skb);
468 deref_ldisc:
469 tty_ldisc_deref(ld);
471 deref_tty:
472 tty_kref_put(tty);
473 return ret;
476 static void handle_minor_recv(struct capiminor *mp)
478 struct sk_buff *skb;
480 while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
481 if (handle_recv_skb(mp, skb) < 0) {
482 skb_queue_head(&mp->inqueue, skb);
483 return;
487 static void handle_minor_send(struct capiminor *mp)
489 struct tty_struct *tty;
490 struct sk_buff *skb;
491 u16 len;
492 u16 errcode;
493 u16 datahandle;
495 tty = tty_port_tty_get(&mp->port);
496 if (!tty)
497 return;
499 if (mp->ttyoutstop) {
500 pr_debug("capi: send: tty stopped\n");
501 tty_kref_put(tty);
502 return;
505 while (1) {
506 spin_lock_bh(&mp->outlock);
507 skb = __skb_dequeue(&mp->outqueue);
508 if (!skb) {
509 spin_unlock_bh(&mp->outlock);
510 break;
512 len = (u16)skb->len;
513 mp->outbytes -= len;
514 spin_unlock_bh(&mp->outlock);
516 datahandle = atomic_inc_return(&mp->datahandle);
517 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
518 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
519 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
520 capimsg_setu16(skb->data, 2, mp->ap->applid);
521 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
522 capimsg_setu8 (skb->data, 5, CAPI_REQ);
523 capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
524 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
525 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
526 capimsg_setu16(skb->data, 16, len); /* Data length */
527 capimsg_setu16(skb->data, 18, datahandle);
528 capimsg_setu16(skb->data, 20, 0); /* Flags */
530 if (capiminor_add_ack(mp, datahandle) < 0) {
531 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
533 spin_lock_bh(&mp->outlock);
534 __skb_queue_head(&mp->outqueue, skb);
535 mp->outbytes += len;
536 spin_unlock_bh(&mp->outlock);
538 break;
540 errcode = capi20_put_message(mp->ap, skb);
541 if (errcode == CAPI_NOERROR) {
542 pr_debug("capi: DATA_B3_REQ %u len=%u\n",
543 datahandle, len);
544 continue;
546 capiminor_del_ack(mp, datahandle);
548 if (errcode == CAPI_SENDQUEUEFULL) {
549 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
551 spin_lock_bh(&mp->outlock);
552 __skb_queue_head(&mp->outqueue, skb);
553 mp->outbytes += len;
554 spin_unlock_bh(&mp->outlock);
556 break;
559 /* ups, drop packet */
560 printk(KERN_ERR "capi: put_message = %x\n", errcode);
561 kfree_skb(skb);
563 tty_kref_put(tty);
566 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
567 /* -------- function called by lower level -------------------------- */
569 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
571 struct capidev *cdev = ap->private;
572 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
573 struct capiminor *mp;
574 u16 datahandle;
575 struct capincci *np;
576 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
578 mutex_lock(&cdev->lock);
580 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
581 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
582 if ((info & 0xff00) == 0)
583 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
585 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
586 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
588 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
589 skb_queue_tail(&cdev->recvqueue, skb);
590 wake_up_interruptible(&cdev->recvwait);
591 goto unlock_out;
594 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
595 skb_queue_tail(&cdev->recvqueue, skb);
596 wake_up_interruptible(&cdev->recvwait);
598 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
600 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
601 if (!np) {
602 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
603 skb_queue_tail(&cdev->recvqueue, skb);
604 wake_up_interruptible(&cdev->recvwait);
605 goto unlock_out;
608 mp = np->minorp;
609 if (!mp) {
610 skb_queue_tail(&cdev->recvqueue, skb);
611 wake_up_interruptible(&cdev->recvwait);
612 goto unlock_out;
614 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
615 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
616 pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
617 datahandle, skb->len-CAPIMSG_LEN(skb->data));
618 skb_queue_tail(&mp->inqueue, skb);
620 handle_minor_recv(mp);
622 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
624 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
625 pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
626 datahandle,
627 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 2));
628 kfree_skb(skb);
629 capiminor_del_ack(mp, datahandle);
630 tty_port_tty_wakeup(&mp->port);
631 handle_minor_send(mp);
633 } else {
634 /* ups, let capi application handle it :-) */
635 skb_queue_tail(&cdev->recvqueue, skb);
636 wake_up_interruptible(&cdev->recvwait);
638 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
640 unlock_out:
641 mutex_unlock(&cdev->lock);
644 /* -------- file_operations for capidev ----------------------------- */
646 static ssize_t
647 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
649 struct capidev *cdev = file->private_data;
650 struct sk_buff *skb;
651 size_t copied;
652 int err;
654 if (!cdev->ap.applid)
655 return -ENODEV;
657 skb = skb_dequeue(&cdev->recvqueue);
658 if (!skb) {
659 if (file->f_flags & O_NONBLOCK)
660 return -EAGAIN;
661 err = wait_event_interruptible(cdev->recvwait,
662 (skb = skb_dequeue(&cdev->recvqueue)));
663 if (err)
664 return err;
666 if (skb->len > count) {
667 skb_queue_head(&cdev->recvqueue, skb);
668 return -EMSGSIZE;
670 if (copy_to_user(buf, skb->data, skb->len)) {
671 skb_queue_head(&cdev->recvqueue, skb);
672 return -EFAULT;
674 copied = skb->len;
676 kfree_skb(skb);
678 return copied;
681 static ssize_t
682 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
684 struct capidev *cdev = file->private_data;
685 struct sk_buff *skb;
686 u16 mlen;
688 if (!cdev->ap.applid)
689 return -ENODEV;
691 skb = alloc_skb(count, GFP_USER);
692 if (!skb)
693 return -ENOMEM;
695 if (copy_from_user(skb_put(skb, count), buf, count)) {
696 kfree_skb(skb);
697 return -EFAULT;
699 mlen = CAPIMSG_LEN(skb->data);
700 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
701 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
702 kfree_skb(skb);
703 return -EINVAL;
705 } else {
706 if (mlen != count) {
707 kfree_skb(skb);
708 return -EINVAL;
711 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
713 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
714 mutex_lock(&cdev->lock);
715 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
716 mutex_unlock(&cdev->lock);
719 cdev->errcode = capi20_put_message(&cdev->ap, skb);
721 if (cdev->errcode) {
722 kfree_skb(skb);
723 return -EIO;
725 return count;
728 static __poll_t
729 capi_poll(struct file *file, poll_table *wait)
731 struct capidev *cdev = file->private_data;
732 __poll_t mask = 0;
734 if (!cdev->ap.applid)
735 return EPOLLERR;
737 poll_wait(file, &(cdev->recvwait), wait);
738 mask = EPOLLOUT | EPOLLWRNORM;
739 if (!skb_queue_empty(&cdev->recvqueue))
740 mask |= EPOLLIN | EPOLLRDNORM;
741 return mask;
744 static int
745 capi_ioctl(struct file *file, 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:
779 if (copy_from_user(&data.contr, argp,
780 sizeof(data.contr)))
781 return -EFAULT;
782 cdev->errcode = capi20_get_version(data.contr, &data.version);
783 if (cdev->errcode)
784 return -EIO;
785 if (copy_to_user(argp, &data.version,
786 sizeof(data.version)))
787 return -EFAULT;
788 return 0;
790 case CAPI_GET_SERIAL:
791 if (copy_from_user(&data.contr, argp,
792 sizeof(data.contr)))
793 return -EFAULT;
794 cdev->errcode = capi20_get_serial(data.contr, data.serial);
795 if (cdev->errcode)
796 return -EIO;
797 if (copy_to_user(argp, data.serial,
798 sizeof(data.serial)))
799 return -EFAULT;
800 return 0;
802 case CAPI_GET_PROFILE:
803 if (copy_from_user(&data.contr, argp,
804 sizeof(data.contr)))
805 return -EFAULT;
807 if (data.contr == 0) {
808 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
809 if (cdev->errcode)
810 return -EIO;
812 retval = copy_to_user(argp,
813 &data.profile.ncontroller,
814 sizeof(data.profile.ncontroller));
816 } else {
817 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
818 if (cdev->errcode)
819 return -EIO;
821 retval = copy_to_user(argp, &data.profile,
822 sizeof(data.profile));
824 if (retval)
825 return -EFAULT;
826 return 0;
828 case CAPI_GET_MANUFACTURER:
829 if (copy_from_user(&data.contr, argp,
830 sizeof(data.contr)))
831 return -EFAULT;
832 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
833 if (cdev->errcode)
834 return -EIO;
836 if (copy_to_user(argp, data.manufacturer,
837 sizeof(data.manufacturer)))
838 return -EFAULT;
840 return 0;
842 case CAPI_GET_ERRCODE:
843 data.errcode = cdev->errcode;
844 cdev->errcode = CAPI_NOERROR;
845 if (arg) {
846 if (copy_to_user(argp, &data.errcode,
847 sizeof(data.errcode)))
848 return -EFAULT;
850 return data.errcode;
852 case CAPI_INSTALLED:
853 if (capi20_isinstalled() == CAPI_NOERROR)
854 return 0;
855 return -ENXIO;
857 case CAPI_MANUFACTURER_CMD: {
858 struct capi_manufacturer_cmd mcmd;
859 if (!capable(CAP_SYS_ADMIN))
860 return -EPERM;
861 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
862 return -EFAULT;
863 return capi20_manufacturer(mcmd.cmd, mcmd.data);
865 case CAPI_SET_FLAGS:
866 case CAPI_CLR_FLAGS: {
867 unsigned userflags;
869 if (copy_from_user(&userflags, argp, sizeof(userflags)))
870 return -EFAULT;
872 mutex_lock(&cdev->lock);
873 if (cmd == CAPI_SET_FLAGS)
874 cdev->userflags |= userflags;
875 else
876 cdev->userflags &= ~userflags;
877 mutex_unlock(&cdev->lock);
878 return 0;
880 case CAPI_GET_FLAGS:
881 if (copy_to_user(argp, &cdev->userflags,
882 sizeof(cdev->userflags)))
883 return -EFAULT;
884 return 0;
886 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
887 case CAPI_NCCI_OPENCOUNT:
888 return 0;
890 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
891 case CAPI_NCCI_OPENCOUNT: {
892 struct capincci *nccip;
893 unsigned ncci;
894 int count = 0;
896 if (copy_from_user(&ncci, argp, sizeof(ncci)))
897 return -EFAULT;
899 mutex_lock(&cdev->lock);
900 nccip = capincci_find(cdev, (u32)ncci);
901 if (nccip)
902 count = capincci_minor_opencount(nccip);
903 mutex_unlock(&cdev->lock);
904 return count;
907 case CAPI_NCCI_GETUNIT: {
908 struct capincci *nccip;
909 struct capiminor *mp;
910 unsigned ncci;
911 int unit = -ESRCH;
913 if (copy_from_user(&ncci, argp, sizeof(ncci)))
914 return -EFAULT;
916 mutex_lock(&cdev->lock);
917 nccip = capincci_find(cdev, (u32)ncci);
918 if (nccip) {
919 mp = nccip->minorp;
920 if (mp)
921 unit = mp->minor;
923 mutex_unlock(&cdev->lock);
924 return unit;
926 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
928 default:
929 return -EINVAL;
933 static long
934 capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
936 int ret;
938 mutex_lock(&capi_mutex);
939 ret = capi_ioctl(file, cmd, arg);
940 mutex_unlock(&capi_mutex);
942 return ret;
945 static int capi_open(struct inode *inode, struct file *file)
947 struct capidev *cdev;
949 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
950 if (!cdev)
951 return -ENOMEM;
953 mutex_init(&cdev->lock);
954 skb_queue_head_init(&cdev->recvqueue);
955 init_waitqueue_head(&cdev->recvwait);
956 INIT_LIST_HEAD(&cdev->nccis);
957 file->private_data = cdev;
959 mutex_lock(&capidev_list_lock);
960 list_add_tail(&cdev->list, &capidev_list);
961 mutex_unlock(&capidev_list_lock);
963 return nonseekable_open(inode, file);
966 static int capi_release(struct inode *inode, struct file *file)
968 struct capidev *cdev = file->private_data;
970 mutex_lock(&capidev_list_lock);
971 list_del(&cdev->list);
972 mutex_unlock(&capidev_list_lock);
974 if (cdev->ap.applid)
975 capi20_release(&cdev->ap);
976 skb_queue_purge(&cdev->recvqueue);
977 capincci_free(cdev, 0xffffffff);
979 kfree(cdev);
980 return 0;
983 static const struct file_operations capi_fops =
985 .owner = THIS_MODULE,
986 .llseek = no_llseek,
987 .read = capi_read,
988 .write = capi_write,
989 .poll = capi_poll,
990 .unlocked_ioctl = capi_unlocked_ioctl,
991 .open = capi_open,
992 .release = capi_release,
995 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
996 /* -------- tty_operations for capincci ----------------------------- */
998 static int
999 capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1001 struct capiminor *mp = capiminor_get(tty->index);
1002 int ret = tty_standard_install(driver, tty);
1004 if (ret == 0)
1005 tty->driver_data = mp;
1006 else
1007 capiminor_put(mp);
1008 return ret;
1011 static void capinc_tty_cleanup(struct tty_struct *tty)
1013 struct capiminor *mp = tty->driver_data;
1014 tty->driver_data = NULL;
1015 capiminor_put(mp);
1018 static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
1020 struct capiminor *mp = tty->driver_data;
1021 int err;
1023 err = tty_port_open(&mp->port, tty, filp);
1024 if (err)
1025 return err;
1027 handle_minor_recv(mp);
1028 return 0;
1031 static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
1033 struct capiminor *mp = tty->driver_data;
1035 tty_port_close(&mp->port, tty, filp);
1038 static int capinc_tty_write(struct tty_struct *tty,
1039 const unsigned char *buf, int count)
1041 struct capiminor *mp = tty->driver_data;
1042 struct sk_buff *skb;
1044 pr_debug("capinc_tty_write(count=%d)\n", count);
1046 spin_lock_bh(&mp->outlock);
1047 skb = mp->outskb;
1048 if (skb) {
1049 mp->outskb = NULL;
1050 __skb_queue_tail(&mp->outqueue, skb);
1051 mp->outbytes += skb->len;
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");
1057 spin_unlock_bh(&mp->outlock);
1058 return -ENOMEM;
1061 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1062 skb_put_data(skb, buf, count);
1064 __skb_queue_tail(&mp->outqueue, skb);
1065 mp->outbytes += skb->len;
1066 spin_unlock_bh(&mp->outlock);
1068 handle_minor_send(mp);
1070 return count;
1073 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1075 struct capiminor *mp = tty->driver_data;
1076 bool invoke_send = false;
1077 struct sk_buff *skb;
1078 int ret = 1;
1080 pr_debug("capinc_put_char(%u)\n", ch);
1082 spin_lock_bh(&mp->outlock);
1083 skb = mp->outskb;
1084 if (skb) {
1085 if (skb_tailroom(skb) > 0) {
1086 skb_put_u8(skb, ch);
1087 goto unlock_out;
1089 mp->outskb = NULL;
1090 __skb_queue_tail(&mp->outqueue, skb);
1091 mp->outbytes += skb->len;
1092 invoke_send = true;
1095 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1096 if (skb) {
1097 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1098 skb_put_u8(skb, ch);
1099 mp->outskb = skb;
1100 } else {
1101 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1102 ret = 0;
1105 unlock_out:
1106 spin_unlock_bh(&mp->outlock);
1108 if (invoke_send)
1109 handle_minor_send(mp);
1111 return ret;
1114 static void capinc_tty_flush_chars(struct tty_struct *tty)
1116 struct capiminor *mp = tty->driver_data;
1117 struct sk_buff *skb;
1119 pr_debug("capinc_tty_flush_chars\n");
1121 spin_lock_bh(&mp->outlock);
1122 skb = mp->outskb;
1123 if (skb) {
1124 mp->outskb = NULL;
1125 __skb_queue_tail(&mp->outqueue, skb);
1126 mp->outbytes += skb->len;
1127 spin_unlock_bh(&mp->outlock);
1129 handle_minor_send(mp);
1130 } else
1131 spin_unlock_bh(&mp->outlock);
1133 handle_minor_recv(mp);
1136 static int capinc_tty_write_room(struct tty_struct *tty)
1138 struct capiminor *mp = tty->driver_data;
1139 int room;
1141 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1142 room *= CAPI_MAX_BLKSIZE;
1143 pr_debug("capinc_tty_write_room = %d\n", room);
1144 return room;
1147 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1149 struct capiminor *mp = tty->driver_data;
1151 pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1152 mp->outbytes, mp->nack,
1153 skb_queue_len(&mp->outqueue),
1154 skb_queue_len(&mp->inqueue));
1155 return mp->outbytes;
1158 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1160 pr_debug("capinc_tty_set_termios\n");
1163 static void capinc_tty_throttle(struct tty_struct *tty)
1165 struct capiminor *mp = tty->driver_data;
1166 pr_debug("capinc_tty_throttle\n");
1167 mp->ttyinstop = 1;
1170 static void capinc_tty_unthrottle(struct tty_struct *tty)
1172 struct capiminor *mp = tty->driver_data;
1174 pr_debug("capinc_tty_unthrottle\n");
1175 mp->ttyinstop = 0;
1176 handle_minor_recv(mp);
1179 static void capinc_tty_stop(struct tty_struct *tty)
1181 struct capiminor *mp = tty->driver_data;
1183 pr_debug("capinc_tty_stop\n");
1184 mp->ttyoutstop = 1;
1187 static void capinc_tty_start(struct tty_struct *tty)
1189 struct capiminor *mp = tty->driver_data;
1191 pr_debug("capinc_tty_start\n");
1192 mp->ttyoutstop = 0;
1193 handle_minor_send(mp);
1196 static void capinc_tty_hangup(struct tty_struct *tty)
1198 struct capiminor *mp = tty->driver_data;
1200 pr_debug("capinc_tty_hangup\n");
1201 tty_port_hangup(&mp->port);
1204 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1206 pr_debug("capinc_tty_break_ctl(%d)\n", state);
1207 return 0;
1210 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1212 pr_debug("capinc_tty_flush_buffer\n");
1215 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1217 pr_debug("capinc_tty_set_ldisc\n");
1220 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1222 pr_debug("capinc_tty_send_xchar(%d)\n", ch);
1225 static const struct tty_operations capinc_ops = {
1226 .open = capinc_tty_open,
1227 .close = capinc_tty_close,
1228 .write = capinc_tty_write,
1229 .put_char = capinc_tty_put_char,
1230 .flush_chars = capinc_tty_flush_chars,
1231 .write_room = capinc_tty_write_room,
1232 .chars_in_buffer = capinc_tty_chars_in_buffer,
1233 .set_termios = capinc_tty_set_termios,
1234 .throttle = capinc_tty_throttle,
1235 .unthrottle = capinc_tty_unthrottle,
1236 .stop = capinc_tty_stop,
1237 .start = capinc_tty_start,
1238 .hangup = capinc_tty_hangup,
1239 .break_ctl = capinc_tty_break_ctl,
1240 .flush_buffer = capinc_tty_flush_buffer,
1241 .set_ldisc = capinc_tty_set_ldisc,
1242 .send_xchar = capinc_tty_send_xchar,
1243 .install = capinc_tty_install,
1244 .cleanup = capinc_tty_cleanup,
1247 static int __init capinc_tty_init(void)
1249 struct tty_driver *drv;
1250 int err;
1252 if (capi_ttyminors > CAPINC_MAX_PORTS)
1253 capi_ttyminors = CAPINC_MAX_PORTS;
1254 if (capi_ttyminors <= 0)
1255 capi_ttyminors = CAPINC_NR_PORTS;
1257 capiminors = kcalloc(capi_ttyminors, sizeof(struct capiminor *),
1258 GFP_KERNEL);
1259 if (!capiminors)
1260 return -ENOMEM;
1262 drv = alloc_tty_driver(capi_ttyminors);
1263 if (!drv) {
1264 kfree(capiminors);
1265 return -ENOMEM;
1267 drv->driver_name = "capi_nc";
1268 drv->name = "capi!";
1269 drv->major = 0;
1270 drv->minor_start = 0;
1271 drv->type = TTY_DRIVER_TYPE_SERIAL;
1272 drv->subtype = SERIAL_TYPE_NORMAL;
1273 drv->init_termios = tty_std_termios;
1274 drv->init_termios.c_iflag = ICRNL;
1275 drv->init_termios.c_oflag = OPOST | ONLCR;
1276 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1277 drv->init_termios.c_lflag = 0;
1278 drv->flags =
1279 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1280 TTY_DRIVER_DYNAMIC_DEV;
1281 tty_set_operations(drv, &capinc_ops);
1283 err = tty_register_driver(drv);
1284 if (err) {
1285 put_tty_driver(drv);
1286 kfree(capiminors);
1287 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1288 return err;
1290 capinc_tty_driver = drv;
1291 return 0;
1294 static void __exit capinc_tty_exit(void)
1296 tty_unregister_driver(capinc_tty_driver);
1297 put_tty_driver(capinc_tty_driver);
1298 kfree(capiminors);
1301 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1303 static inline int capinc_tty_init(void)
1305 return 0;
1308 static inline void capinc_tty_exit(void) { }
1310 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1312 /* -------- /proc functions ----------------------------------------- */
1315 * /proc/capi/capi20:
1316 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1318 static int __maybe_unused capi20_proc_show(struct seq_file *m, void *v)
1320 struct capidev *cdev;
1321 struct list_head *l;
1323 mutex_lock(&capidev_list_lock);
1324 list_for_each(l, &capidev_list) {
1325 cdev = list_entry(l, struct capidev, list);
1326 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1327 cdev->ap.applid,
1328 cdev->ap.nrecvctlpkt,
1329 cdev->ap.nrecvdatapkt,
1330 cdev->ap.nsentctlpkt,
1331 cdev->ap.nsentdatapkt);
1333 mutex_unlock(&capidev_list_lock);
1334 return 0;
1338 * /proc/capi/capi20ncci:
1339 * applid ncci
1341 static int __maybe_unused capi20ncci_proc_show(struct seq_file *m, void *v)
1343 struct capidev *cdev;
1344 struct capincci *np;
1346 mutex_lock(&capidev_list_lock);
1347 list_for_each_entry(cdev, &capidev_list, list) {
1348 mutex_lock(&cdev->lock);
1349 list_for_each_entry(np, &cdev->nccis, list)
1350 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
1351 mutex_unlock(&cdev->lock);
1353 mutex_unlock(&capidev_list_lock);
1354 return 0;
1357 static void __init proc_init(void)
1359 proc_create_single("capi/capi20", 0, NULL, capi20_proc_show);
1360 proc_create_single("capi/capi20ncci", 0, NULL, capi20ncci_proc_show);
1363 static void __exit proc_exit(void)
1365 remove_proc_entry("capi/capi20", NULL);
1366 remove_proc_entry("capi/capi20ncci", NULL);
1369 /* -------- init function and module interface ---------------------- */
1372 static int __init capi_init(void)
1374 const char *compileinfo;
1375 int major_ret;
1377 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1378 if (major_ret < 0) {
1379 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1380 return major_ret;
1382 capi_class = class_create(THIS_MODULE, "capi");
1383 if (IS_ERR(capi_class)) {
1384 unregister_chrdev(capi_major, "capi20");
1385 return PTR_ERR(capi_class);
1388 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi20");
1390 if (capinc_tty_init() < 0) {
1391 device_destroy(capi_class, MKDEV(capi_major, 0));
1392 class_destroy(capi_class);
1393 unregister_chrdev(capi_major, "capi20");
1394 return -ENOMEM;
1397 proc_init();
1399 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1400 compileinfo = " (middleware)";
1401 #else
1402 compileinfo = " (no middleware)";
1403 #endif
1404 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1405 capi_major, compileinfo);
1407 return 0;
1410 static void __exit capi_exit(void)
1412 proc_exit();
1414 device_destroy(capi_class, MKDEV(capi_major, 0));
1415 class_destroy(capi_class);
1416 unregister_chrdev(capi_major, "capi20");
1418 capinc_tty_exit();
1421 module_init(capi_init);
1422 module_exit(capi_exit);