Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / isdn / capi / capi.c
blobfdf87acccd067a0b47dea01274585117ad75240b
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/ethtool.h>
15 #include <linux/errno.h>
16 #include <linux/kernel.h>
17 #include <linux/major.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/fcntl.h>
21 #include <linux/fs.h>
22 #include <linux/signal.h>
23 #include <linux/mutex.h>
24 #include <linux/mm.h>
25 #include <linux/timer.h>
26 #include <linux/wait.h>
27 #include <linux/tty.h>
28 #include <linux/netdevice.h>
29 #include <linux/ppp_defs.h>
30 #include <linux/ppp-ioctl.h>
31 #include <linux/skbuff.h>
32 #include <linux/proc_fs.h>
33 #include <linux/seq_file.h>
34 #include <linux/poll.h>
35 #include <linux/capi.h>
36 #include <linux/kernelcapi.h>
37 #include <linux/init.h>
38 #include <linux/device.h>
39 #include <linux/moduleparam.h>
40 #include <linux/isdn/capiutil.h>
41 #include <linux/isdn/capicmd.h>
43 #include "kcapi.h"
45 MODULE_DESCRIPTION("CAPI4Linux: kernel CAPI layer and /dev/capi20 interface");
46 MODULE_AUTHOR("Carsten Paeth");
47 MODULE_LICENSE("GPL");
49 /* -------- driver information -------------------------------------- */
51 static DEFINE_MUTEX(capi_mutex);
52 static struct class *capi_class;
53 static int capi_major = 68; /* allocated */
55 module_param_named(major, capi_major, uint, 0);
57 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
58 #define CAPINC_NR_PORTS 32
59 #define CAPINC_MAX_PORTS 256
61 static int capi_ttyminors = CAPINC_NR_PORTS;
63 module_param_named(ttyminors, capi_ttyminors, uint, 0);
64 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
66 /* -------- defines ------------------------------------------------- */
68 #define CAPINC_MAX_RECVQUEUE 10
69 #define CAPINC_MAX_SENDQUEUE 10
70 #define CAPI_MAX_BLKSIZE 2048
72 /* -------- data structures ----------------------------------------- */
74 struct capidev;
75 struct capincci;
76 struct capiminor;
78 struct ackqueue_entry {
79 struct list_head list;
80 u16 datahandle;
83 struct capiminor {
84 unsigned int minor;
86 struct capi20_appl *ap;
87 u32 ncci;
88 atomic_t datahandle;
89 atomic_t msgid;
91 struct tty_port port;
92 int ttyinstop;
93 int ttyoutstop;
95 struct sk_buff_head inqueue;
97 struct sk_buff_head outqueue;
98 int outbytes;
99 struct sk_buff *outskb;
100 spinlock_t outlock;
102 /* transmit path */
103 struct list_head ackqueue;
104 int nack;
105 spinlock_t ackqlock;
108 struct capincci {
109 struct list_head list;
110 u32 ncci;
111 struct capidev *cdev;
112 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
113 struct capiminor *minorp;
114 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
117 struct capidev {
118 struct list_head list;
119 struct capi20_appl ap;
120 u16 errcode;
121 unsigned userflags;
123 struct sk_buff_head recvqueue;
124 wait_queue_head_t recvwait;
126 struct list_head nccis;
128 struct mutex lock;
131 /* -------- global variables ---------------------------------------- */
133 static DEFINE_MUTEX(capidev_list_lock);
134 static LIST_HEAD(capidev_list);
136 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
138 static DEFINE_SPINLOCK(capiminors_lock);
139 static struct capiminor **capiminors;
141 static struct tty_driver *capinc_tty_driver;
143 /* -------- datahandles --------------------------------------------- */
145 static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
147 struct ackqueue_entry *n;
149 n = kmalloc(sizeof(*n), GFP_ATOMIC);
150 if (unlikely(!n)) {
151 printk(KERN_ERR "capi: alloc datahandle failed\n");
152 return -1;
154 n->datahandle = datahandle;
155 INIT_LIST_HEAD(&n->list);
156 spin_lock_bh(&mp->ackqlock);
157 list_add_tail(&n->list, &mp->ackqueue);
158 mp->nack++;
159 spin_unlock_bh(&mp->ackqlock);
160 return 0;
163 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
165 struct ackqueue_entry *p, *tmp;
167 spin_lock_bh(&mp->ackqlock);
168 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
169 if (p->datahandle == datahandle) {
170 list_del(&p->list);
171 mp->nack--;
172 spin_unlock_bh(&mp->ackqlock);
173 kfree(p);
174 return 0;
177 spin_unlock_bh(&mp->ackqlock);
178 return -1;
181 static void capiminor_del_all_ack(struct capiminor *mp)
183 struct ackqueue_entry *p, *tmp;
185 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
186 list_del(&p->list);
187 kfree(p);
188 mp->nack--;
193 /* -------- struct capiminor ---------------------------------------- */
195 static void capiminor_destroy(struct tty_port *port)
197 struct capiminor *mp = container_of(port, struct capiminor, port);
199 kfree_skb(mp->outskb);
200 skb_queue_purge(&mp->inqueue);
201 skb_queue_purge(&mp->outqueue);
202 capiminor_del_all_ack(mp);
203 kfree(mp);
206 static const struct tty_port_operations capiminor_port_ops = {
207 .destruct = capiminor_destroy,
210 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
212 struct capiminor *mp;
213 struct device *dev;
214 unsigned int minor;
216 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
217 if (!mp) {
218 printk(KERN_ERR "capi: can't alloc capiminor\n");
219 return NULL;
222 mp->ap = ap;
223 mp->ncci = ncci;
224 INIT_LIST_HEAD(&mp->ackqueue);
225 spin_lock_init(&mp->ackqlock);
227 skb_queue_head_init(&mp->inqueue);
228 skb_queue_head_init(&mp->outqueue);
229 spin_lock_init(&mp->outlock);
231 tty_port_init(&mp->port);
232 mp->port.ops = &capiminor_port_ops;
234 /* Allocate the least unused minor number. */
235 spin_lock(&capiminors_lock);
236 for (minor = 0; minor < capi_ttyminors; minor++)
237 if (!capiminors[minor]) {
238 capiminors[minor] = mp;
239 break;
241 spin_unlock(&capiminors_lock);
243 if (minor == capi_ttyminors) {
244 printk(KERN_NOTICE "capi: out of minors\n");
245 goto err_out1;
248 mp->minor = minor;
250 dev = tty_port_register_device(&mp->port, capinc_tty_driver, minor,
251 NULL);
252 if (IS_ERR(dev))
253 goto err_out2;
255 return mp;
257 err_out2:
258 spin_lock(&capiminors_lock);
259 capiminors[minor] = NULL;
260 spin_unlock(&capiminors_lock);
262 err_out1:
263 tty_port_put(&mp->port);
264 return NULL;
267 static struct capiminor *capiminor_get(unsigned int minor)
269 struct capiminor *mp;
271 spin_lock(&capiminors_lock);
272 mp = capiminors[minor];
273 if (mp)
274 tty_port_get(&mp->port);
275 spin_unlock(&capiminors_lock);
277 return mp;
280 static inline void capiminor_put(struct capiminor *mp)
282 tty_port_put(&mp->port);
285 static void capiminor_free(struct capiminor *mp)
287 tty_unregister_device(capinc_tty_driver, mp->minor);
289 spin_lock(&capiminors_lock);
290 capiminors[mp->minor] = NULL;
291 spin_unlock(&capiminors_lock);
293 capiminor_put(mp);
296 /* -------- struct capincci ----------------------------------------- */
298 static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
300 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
301 np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
304 static void capincci_free_minor(struct capincci *np)
306 struct capiminor *mp = np->minorp;
307 struct tty_struct *tty;
309 if (mp) {
310 tty = tty_port_tty_get(&mp->port);
311 if (tty) {
312 tty_vhangup(tty);
313 tty_kref_put(tty);
316 capiminor_free(mp);
320 static inline unsigned int capincci_minor_opencount(struct capincci *np)
322 struct capiminor *mp = np->minorp;
323 unsigned int count = 0;
324 struct tty_struct *tty;
326 if (mp) {
327 tty = tty_port_tty_get(&mp->port);
328 if (tty) {
329 count = tty->count;
330 tty_kref_put(tty);
333 return count;
336 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
338 static inline void
339 capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
340 static inline void capincci_free_minor(struct capincci *np) { }
342 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
344 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
346 struct capincci *np;
348 np = kzalloc(sizeof(*np), GFP_KERNEL);
349 if (!np)
350 return NULL;
351 np->ncci = ncci;
352 np->cdev = cdev;
354 capincci_alloc_minor(cdev, np);
356 list_add_tail(&np->list, &cdev->nccis);
358 return np;
361 static void capincci_free(struct capidev *cdev, u32 ncci)
363 struct capincci *np, *tmp;
365 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
366 if (ncci == 0xffffffff || np->ncci == ncci) {
367 capincci_free_minor(np);
368 list_del(&np->list);
369 kfree(np);
373 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
374 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
376 struct capincci *np;
378 list_for_each_entry(np, &cdev->nccis, list)
379 if (np->ncci == ncci)
380 return np;
381 return NULL;
384 /* -------- handle data queue --------------------------------------- */
386 static struct sk_buff *
387 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
389 struct sk_buff *nskb;
390 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_KERNEL);
391 if (nskb) {
392 u16 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
393 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
394 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
395 capimsg_setu16(s, 2, mp->ap->applid);
396 capimsg_setu8 (s, 4, CAPI_DATA_B3);
397 capimsg_setu8 (s, 5, CAPI_RESP);
398 capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
399 capimsg_setu32(s, 8, mp->ncci);
400 capimsg_setu16(s, 12, datahandle);
402 return nskb;
405 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
407 unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
408 struct tty_struct *tty;
409 struct sk_buff *nskb;
410 u16 errcode, datahandle;
411 struct tty_ldisc *ld;
412 int ret = -1;
414 tty = tty_port_tty_get(&mp->port);
415 if (!tty) {
416 pr_debug("capi: currently no receiver\n");
417 return -1;
420 ld = tty_ldisc_ref(tty);
421 if (!ld) {
422 /* fatal error, do not requeue */
423 ret = 0;
424 kfree_skb(skb);
425 goto deref_tty;
428 if (ld->ops->receive_buf == NULL) {
429 pr_debug("capi: ldisc has no receive_buf function\n");
430 /* fatal error, do not requeue */
431 goto free_skb;
433 if (mp->ttyinstop) {
434 pr_debug("capi: recv tty throttled\n");
435 goto deref_ldisc;
438 if (tty->receive_room < datalen) {
439 pr_debug("capi: no room in tty\n");
440 goto deref_ldisc;
443 nskb = gen_data_b3_resp_for(mp, skb);
444 if (!nskb) {
445 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
446 goto deref_ldisc;
449 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
451 errcode = capi20_put_message(mp->ap, nskb);
453 if (errcode == CAPI_NOERROR) {
454 skb_pull(skb, CAPIMSG_LEN(skb->data));
455 pr_debug("capi: DATA_B3_RESP %u len=%d => ldisc\n",
456 datahandle, skb->len);
457 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
458 } else {
459 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
460 errcode);
461 kfree_skb(nskb);
463 if (errcode == CAPI_SENDQUEUEFULL)
464 goto deref_ldisc;
467 free_skb:
468 ret = 0;
469 kfree_skb(skb);
471 deref_ldisc:
472 tty_ldisc_deref(ld);
474 deref_tty:
475 tty_kref_put(tty);
476 return ret;
479 static void handle_minor_recv(struct capiminor *mp)
481 struct sk_buff *skb;
483 while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
484 if (handle_recv_skb(mp, skb) < 0) {
485 skb_queue_head(&mp->inqueue, skb);
486 return;
490 static void handle_minor_send(struct capiminor *mp)
492 struct tty_struct *tty;
493 struct sk_buff *skb;
494 u16 len;
495 u16 errcode;
496 u16 datahandle;
498 tty = tty_port_tty_get(&mp->port);
499 if (!tty)
500 return;
502 if (mp->ttyoutstop) {
503 pr_debug("capi: send: tty stopped\n");
504 tty_kref_put(tty);
505 return;
508 while (1) {
509 spin_lock_bh(&mp->outlock);
510 skb = __skb_dequeue(&mp->outqueue);
511 if (!skb) {
512 spin_unlock_bh(&mp->outlock);
513 break;
515 len = (u16)skb->len;
516 mp->outbytes -= len;
517 spin_unlock_bh(&mp->outlock);
519 datahandle = atomic_inc_return(&mp->datahandle);
520 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
521 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
522 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
523 capimsg_setu16(skb->data, 2, mp->ap->applid);
524 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
525 capimsg_setu8 (skb->data, 5, CAPI_REQ);
526 capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
527 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
528 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
529 capimsg_setu16(skb->data, 16, len); /* Data length */
530 capimsg_setu16(skb->data, 18, datahandle);
531 capimsg_setu16(skb->data, 20, 0); /* Flags */
533 if (capiminor_add_ack(mp, datahandle) < 0) {
534 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
536 spin_lock_bh(&mp->outlock);
537 __skb_queue_head(&mp->outqueue, skb);
538 mp->outbytes += len;
539 spin_unlock_bh(&mp->outlock);
541 break;
543 errcode = capi20_put_message(mp->ap, skb);
544 if (errcode == CAPI_NOERROR) {
545 pr_debug("capi: DATA_B3_REQ %u len=%u\n",
546 datahandle, len);
547 continue;
549 capiminor_del_ack(mp, datahandle);
551 if (errcode == CAPI_SENDQUEUEFULL) {
552 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
554 spin_lock_bh(&mp->outlock);
555 __skb_queue_head(&mp->outqueue, skb);
556 mp->outbytes += len;
557 spin_unlock_bh(&mp->outlock);
559 break;
562 /* ups, drop packet */
563 printk(KERN_ERR "capi: put_message = %x\n", errcode);
564 kfree_skb(skb);
566 tty_kref_put(tty);
569 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
570 /* -------- function called by lower level -------------------------- */
572 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
574 struct capidev *cdev = ap->private;
575 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
576 struct capiminor *mp;
577 u16 datahandle;
578 struct capincci *np;
579 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
581 mutex_lock(&cdev->lock);
583 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
584 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
585 if ((info & 0xff00) == 0)
586 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
588 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
589 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
591 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
592 skb_queue_tail(&cdev->recvqueue, skb);
593 wake_up_interruptible(&cdev->recvwait);
594 goto unlock_out;
597 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
598 skb_queue_tail(&cdev->recvqueue, skb);
599 wake_up_interruptible(&cdev->recvwait);
601 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
603 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
604 if (!np) {
605 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
606 skb_queue_tail(&cdev->recvqueue, skb);
607 wake_up_interruptible(&cdev->recvwait);
608 goto unlock_out;
611 mp = np->minorp;
612 if (!mp) {
613 skb_queue_tail(&cdev->recvqueue, skb);
614 wake_up_interruptible(&cdev->recvwait);
615 goto unlock_out;
617 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
618 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
619 pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
620 datahandle, skb->len-CAPIMSG_LEN(skb->data));
621 skb_queue_tail(&mp->inqueue, skb);
623 handle_minor_recv(mp);
625 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
627 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
628 pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
629 datahandle,
630 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 2));
631 kfree_skb(skb);
632 capiminor_del_ack(mp, datahandle);
633 tty_port_tty_wakeup(&mp->port);
634 handle_minor_send(mp);
636 } else {
637 /* ups, let capi application handle it :-) */
638 skb_queue_tail(&cdev->recvqueue, skb);
639 wake_up_interruptible(&cdev->recvwait);
641 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
643 unlock_out:
644 mutex_unlock(&cdev->lock);
647 /* -------- file_operations for capidev ----------------------------- */
649 static ssize_t
650 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
652 struct capidev *cdev = file->private_data;
653 struct sk_buff *skb;
654 size_t copied;
655 int err;
657 if (!cdev->ap.applid)
658 return -ENODEV;
660 skb = skb_dequeue(&cdev->recvqueue);
661 if (!skb) {
662 if (file->f_flags & O_NONBLOCK)
663 return -EAGAIN;
664 err = wait_event_interruptible(cdev->recvwait,
665 (skb = skb_dequeue(&cdev->recvqueue)));
666 if (err)
667 return err;
669 if (skb->len > count) {
670 skb_queue_head(&cdev->recvqueue, skb);
671 return -EMSGSIZE;
673 if (copy_to_user(buf, skb->data, skb->len)) {
674 skb_queue_head(&cdev->recvqueue, skb);
675 return -EFAULT;
677 copied = skb->len;
679 kfree_skb(skb);
681 return copied;
684 static ssize_t
685 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
687 struct capidev *cdev = file->private_data;
688 struct sk_buff *skb;
689 u16 mlen;
691 if (!cdev->ap.applid)
692 return -ENODEV;
694 if (count < CAPIMSG_BASELEN)
695 return -EINVAL;
697 skb = alloc_skb(count, GFP_USER);
698 if (!skb)
699 return -ENOMEM;
701 if (copy_from_user(skb_put(skb, count), buf, count)) {
702 kfree_skb(skb);
703 return -EFAULT;
705 mlen = CAPIMSG_LEN(skb->data);
706 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
707 if (count < CAPI_DATA_B3_REQ_LEN ||
708 (size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
709 kfree_skb(skb);
710 return -EINVAL;
712 } else {
713 if (mlen != count) {
714 kfree_skb(skb);
715 return -EINVAL;
718 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
720 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
721 if (count < CAPI_DISCONNECT_B3_RESP_LEN) {
722 kfree_skb(skb);
723 return -EINVAL;
725 mutex_lock(&cdev->lock);
726 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
727 mutex_unlock(&cdev->lock);
730 cdev->errcode = capi20_put_message(&cdev->ap, skb);
732 if (cdev->errcode) {
733 kfree_skb(skb);
734 return -EIO;
736 return count;
739 static __poll_t
740 capi_poll(struct file *file, poll_table *wait)
742 struct capidev *cdev = file->private_data;
743 __poll_t mask = 0;
745 if (!cdev->ap.applid)
746 return EPOLLERR;
748 poll_wait(file, &(cdev->recvwait), wait);
749 mask = EPOLLOUT | EPOLLWRNORM;
750 if (!skb_queue_empty_lockless(&cdev->recvqueue))
751 mask |= EPOLLIN | EPOLLRDNORM;
752 return mask;
755 static int
756 capi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
758 struct capidev *cdev = file->private_data;
759 capi_ioctl_struct data;
760 int retval = -EINVAL;
761 void __user *argp = (void __user *)arg;
763 switch (cmd) {
764 case CAPI_REGISTER:
765 mutex_lock(&cdev->lock);
767 if (cdev->ap.applid) {
768 retval = -EEXIST;
769 goto register_out;
771 if (copy_from_user(&cdev->ap.rparam, argp,
772 sizeof(struct capi_register_params))) {
773 retval = -EFAULT;
774 goto register_out;
776 cdev->ap.private = cdev;
777 cdev->ap.recv_message = capi_recv_message;
778 cdev->errcode = capi20_register(&cdev->ap);
779 retval = (int)cdev->ap.applid;
780 if (cdev->errcode) {
781 cdev->ap.applid = 0;
782 retval = -EIO;
785 register_out:
786 mutex_unlock(&cdev->lock);
787 return retval;
789 case CAPI_GET_VERSION:
790 if (copy_from_user(&data.contr, argp,
791 sizeof(data.contr)))
792 return -EFAULT;
793 cdev->errcode = capi20_get_version(data.contr, &data.version);
794 if (cdev->errcode)
795 return -EIO;
796 if (copy_to_user(argp, &data.version,
797 sizeof(data.version)))
798 return -EFAULT;
799 return 0;
801 case CAPI_GET_SERIAL:
802 if (copy_from_user(&data.contr, argp,
803 sizeof(data.contr)))
804 return -EFAULT;
805 cdev->errcode = capi20_get_serial(data.contr, data.serial);
806 if (cdev->errcode)
807 return -EIO;
808 if (copy_to_user(argp, data.serial,
809 sizeof(data.serial)))
810 return -EFAULT;
811 return 0;
813 case CAPI_GET_PROFILE:
814 if (copy_from_user(&data.contr, argp,
815 sizeof(data.contr)))
816 return -EFAULT;
818 if (data.contr == 0) {
819 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
820 if (cdev->errcode)
821 return -EIO;
823 retval = copy_to_user(argp,
824 &data.profile.ncontroller,
825 sizeof(data.profile.ncontroller));
827 } else {
828 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
829 if (cdev->errcode)
830 return -EIO;
832 retval = copy_to_user(argp, &data.profile,
833 sizeof(data.profile));
835 if (retval)
836 return -EFAULT;
837 return 0;
839 case CAPI_GET_MANUFACTURER:
840 if (copy_from_user(&data.contr, argp,
841 sizeof(data.contr)))
842 return -EFAULT;
843 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
844 if (cdev->errcode)
845 return -EIO;
847 if (copy_to_user(argp, data.manufacturer,
848 sizeof(data.manufacturer)))
849 return -EFAULT;
851 return 0;
853 case CAPI_GET_ERRCODE:
854 data.errcode = cdev->errcode;
855 cdev->errcode = CAPI_NOERROR;
856 if (arg) {
857 if (copy_to_user(argp, &data.errcode,
858 sizeof(data.errcode)))
859 return -EFAULT;
861 return data.errcode;
863 case CAPI_INSTALLED:
864 if (capi20_isinstalled() == CAPI_NOERROR)
865 return 0;
866 return -ENXIO;
868 case CAPI_MANUFACTURER_CMD: {
869 struct capi_manufacturer_cmd mcmd;
870 if (!capable(CAP_SYS_ADMIN))
871 return -EPERM;
872 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
873 return -EFAULT;
874 return capi20_manufacturer(mcmd.cmd, mcmd.data);
876 case CAPI_SET_FLAGS:
877 case CAPI_CLR_FLAGS: {
878 unsigned userflags;
880 if (copy_from_user(&userflags, argp, sizeof(userflags)))
881 return -EFAULT;
883 mutex_lock(&cdev->lock);
884 if (cmd == CAPI_SET_FLAGS)
885 cdev->userflags |= userflags;
886 else
887 cdev->userflags &= ~userflags;
888 mutex_unlock(&cdev->lock);
889 return 0;
891 case CAPI_GET_FLAGS:
892 if (copy_to_user(argp, &cdev->userflags,
893 sizeof(cdev->userflags)))
894 return -EFAULT;
895 return 0;
897 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
898 case CAPI_NCCI_OPENCOUNT:
899 return 0;
901 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
902 case CAPI_NCCI_OPENCOUNT: {
903 struct capincci *nccip;
904 unsigned ncci;
905 int count = 0;
907 if (copy_from_user(&ncci, argp, sizeof(ncci)))
908 return -EFAULT;
910 mutex_lock(&cdev->lock);
911 nccip = capincci_find(cdev, (u32)ncci);
912 if (nccip)
913 count = capincci_minor_opencount(nccip);
914 mutex_unlock(&cdev->lock);
915 return count;
918 case CAPI_NCCI_GETUNIT: {
919 struct capincci *nccip;
920 struct capiminor *mp;
921 unsigned ncci;
922 int unit = -ESRCH;
924 if (copy_from_user(&ncci, argp, sizeof(ncci)))
925 return -EFAULT;
927 mutex_lock(&cdev->lock);
928 nccip = capincci_find(cdev, (u32)ncci);
929 if (nccip) {
930 mp = nccip->minorp;
931 if (mp)
932 unit = mp->minor;
934 mutex_unlock(&cdev->lock);
935 return unit;
937 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
939 default:
940 return -EINVAL;
944 static long
945 capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
947 int ret;
949 mutex_lock(&capi_mutex);
950 ret = capi_ioctl(file, cmd, arg);
951 mutex_unlock(&capi_mutex);
953 return ret;
956 #ifdef CONFIG_COMPAT
957 static long
958 capi_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
960 int ret;
962 if (cmd == CAPI_MANUFACTURER_CMD) {
963 struct {
964 compat_ulong_t cmd;
965 compat_uptr_t data;
966 } mcmd32;
968 if (!capable(CAP_SYS_ADMIN))
969 return -EPERM;
970 if (copy_from_user(&mcmd32, compat_ptr(arg), sizeof(mcmd32)))
971 return -EFAULT;
973 mutex_lock(&capi_mutex);
974 ret = capi20_manufacturer(mcmd32.cmd, compat_ptr(mcmd32.data));
975 mutex_unlock(&capi_mutex);
977 return ret;
980 return capi_unlocked_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
982 #endif
984 static int capi_open(struct inode *inode, struct file *file)
986 struct capidev *cdev;
988 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
989 if (!cdev)
990 return -ENOMEM;
992 mutex_init(&cdev->lock);
993 skb_queue_head_init(&cdev->recvqueue);
994 init_waitqueue_head(&cdev->recvwait);
995 INIT_LIST_HEAD(&cdev->nccis);
996 file->private_data = cdev;
998 mutex_lock(&capidev_list_lock);
999 list_add_tail(&cdev->list, &capidev_list);
1000 mutex_unlock(&capidev_list_lock);
1002 return stream_open(inode, file);
1005 static int capi_release(struct inode *inode, struct file *file)
1007 struct capidev *cdev = file->private_data;
1009 mutex_lock(&capidev_list_lock);
1010 list_del(&cdev->list);
1011 mutex_unlock(&capidev_list_lock);
1013 if (cdev->ap.applid)
1014 capi20_release(&cdev->ap);
1015 skb_queue_purge(&cdev->recvqueue);
1016 capincci_free(cdev, 0xffffffff);
1018 kfree(cdev);
1019 return 0;
1022 static const struct file_operations capi_fops =
1024 .owner = THIS_MODULE,
1025 .llseek = no_llseek,
1026 .read = capi_read,
1027 .write = capi_write,
1028 .poll = capi_poll,
1029 .unlocked_ioctl = capi_unlocked_ioctl,
1030 #ifdef CONFIG_COMPAT
1031 .compat_ioctl = capi_compat_ioctl,
1032 #endif
1033 .open = capi_open,
1034 .release = capi_release,
1037 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1038 /* -------- tty_operations for capincci ----------------------------- */
1040 static int
1041 capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1043 struct capiminor *mp = capiminor_get(tty->index);
1044 int ret = tty_standard_install(driver, tty);
1046 if (ret == 0)
1047 tty->driver_data = mp;
1048 else
1049 capiminor_put(mp);
1050 return ret;
1053 static void capinc_tty_cleanup(struct tty_struct *tty)
1055 struct capiminor *mp = tty->driver_data;
1056 tty->driver_data = NULL;
1057 capiminor_put(mp);
1060 static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
1062 struct capiminor *mp = tty->driver_data;
1063 int err;
1065 err = tty_port_open(&mp->port, tty, filp);
1066 if (err)
1067 return err;
1069 handle_minor_recv(mp);
1070 return 0;
1073 static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
1075 struct capiminor *mp = tty->driver_data;
1077 tty_port_close(&mp->port, tty, filp);
1080 static int capinc_tty_write(struct tty_struct *tty,
1081 const unsigned char *buf, int count)
1083 struct capiminor *mp = tty->driver_data;
1084 struct sk_buff *skb;
1086 pr_debug("capinc_tty_write(count=%d)\n", count);
1088 spin_lock_bh(&mp->outlock);
1089 skb = mp->outskb;
1090 if (skb) {
1091 mp->outskb = NULL;
1092 __skb_queue_tail(&mp->outqueue, skb);
1093 mp->outbytes += skb->len;
1096 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + count, GFP_ATOMIC);
1097 if (!skb) {
1098 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1099 spin_unlock_bh(&mp->outlock);
1100 return -ENOMEM;
1103 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1104 skb_put_data(skb, buf, count);
1106 __skb_queue_tail(&mp->outqueue, skb);
1107 mp->outbytes += skb->len;
1108 spin_unlock_bh(&mp->outlock);
1110 handle_minor_send(mp);
1112 return count;
1115 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1117 struct capiminor *mp = tty->driver_data;
1118 bool invoke_send = false;
1119 struct sk_buff *skb;
1120 int ret = 1;
1122 pr_debug("capinc_put_char(%u)\n", ch);
1124 spin_lock_bh(&mp->outlock);
1125 skb = mp->outskb;
1126 if (skb) {
1127 if (skb_tailroom(skb) > 0) {
1128 skb_put_u8(skb, ch);
1129 goto unlock_out;
1131 mp->outskb = NULL;
1132 __skb_queue_tail(&mp->outqueue, skb);
1133 mp->outbytes += skb->len;
1134 invoke_send = true;
1137 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1138 if (skb) {
1139 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1140 skb_put_u8(skb, ch);
1141 mp->outskb = skb;
1142 } else {
1143 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1144 ret = 0;
1147 unlock_out:
1148 spin_unlock_bh(&mp->outlock);
1150 if (invoke_send)
1151 handle_minor_send(mp);
1153 return ret;
1156 static void capinc_tty_flush_chars(struct tty_struct *tty)
1158 struct capiminor *mp = tty->driver_data;
1159 struct sk_buff *skb;
1161 pr_debug("capinc_tty_flush_chars\n");
1163 spin_lock_bh(&mp->outlock);
1164 skb = mp->outskb;
1165 if (skb) {
1166 mp->outskb = NULL;
1167 __skb_queue_tail(&mp->outqueue, skb);
1168 mp->outbytes += skb->len;
1169 spin_unlock_bh(&mp->outlock);
1171 handle_minor_send(mp);
1172 } else
1173 spin_unlock_bh(&mp->outlock);
1175 handle_minor_recv(mp);
1178 static int capinc_tty_write_room(struct tty_struct *tty)
1180 struct capiminor *mp = tty->driver_data;
1181 int room;
1183 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1184 room *= CAPI_MAX_BLKSIZE;
1185 pr_debug("capinc_tty_write_room = %d\n", room);
1186 return room;
1189 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1191 struct capiminor *mp = tty->driver_data;
1193 pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1194 mp->outbytes, mp->nack,
1195 skb_queue_len(&mp->outqueue),
1196 skb_queue_len(&mp->inqueue));
1197 return mp->outbytes;
1200 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1202 pr_debug("capinc_tty_set_termios\n");
1205 static void capinc_tty_throttle(struct tty_struct *tty)
1207 struct capiminor *mp = tty->driver_data;
1208 pr_debug("capinc_tty_throttle\n");
1209 mp->ttyinstop = 1;
1212 static void capinc_tty_unthrottle(struct tty_struct *tty)
1214 struct capiminor *mp = tty->driver_data;
1216 pr_debug("capinc_tty_unthrottle\n");
1217 mp->ttyinstop = 0;
1218 handle_minor_recv(mp);
1221 static void capinc_tty_stop(struct tty_struct *tty)
1223 struct capiminor *mp = tty->driver_data;
1225 pr_debug("capinc_tty_stop\n");
1226 mp->ttyoutstop = 1;
1229 static void capinc_tty_start(struct tty_struct *tty)
1231 struct capiminor *mp = tty->driver_data;
1233 pr_debug("capinc_tty_start\n");
1234 mp->ttyoutstop = 0;
1235 handle_minor_send(mp);
1238 static void capinc_tty_hangup(struct tty_struct *tty)
1240 struct capiminor *mp = tty->driver_data;
1242 pr_debug("capinc_tty_hangup\n");
1243 tty_port_hangup(&mp->port);
1246 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1248 pr_debug("capinc_tty_break_ctl(%d)\n", state);
1249 return 0;
1252 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1254 pr_debug("capinc_tty_flush_buffer\n");
1257 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1259 pr_debug("capinc_tty_set_ldisc\n");
1262 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1264 pr_debug("capinc_tty_send_xchar(%d)\n", ch);
1267 static const struct tty_operations capinc_ops = {
1268 .open = capinc_tty_open,
1269 .close = capinc_tty_close,
1270 .write = capinc_tty_write,
1271 .put_char = capinc_tty_put_char,
1272 .flush_chars = capinc_tty_flush_chars,
1273 .write_room = capinc_tty_write_room,
1274 .chars_in_buffer = capinc_tty_chars_in_buffer,
1275 .set_termios = capinc_tty_set_termios,
1276 .throttle = capinc_tty_throttle,
1277 .unthrottle = capinc_tty_unthrottle,
1278 .stop = capinc_tty_stop,
1279 .start = capinc_tty_start,
1280 .hangup = capinc_tty_hangup,
1281 .break_ctl = capinc_tty_break_ctl,
1282 .flush_buffer = capinc_tty_flush_buffer,
1283 .set_ldisc = capinc_tty_set_ldisc,
1284 .send_xchar = capinc_tty_send_xchar,
1285 .install = capinc_tty_install,
1286 .cleanup = capinc_tty_cleanup,
1289 static int __init capinc_tty_init(void)
1291 struct tty_driver *drv;
1292 int err;
1294 if (capi_ttyminors > CAPINC_MAX_PORTS)
1295 capi_ttyminors = CAPINC_MAX_PORTS;
1296 if (capi_ttyminors <= 0)
1297 capi_ttyminors = CAPINC_NR_PORTS;
1299 capiminors = kcalloc(capi_ttyminors, sizeof(struct capiminor *),
1300 GFP_KERNEL);
1301 if (!capiminors)
1302 return -ENOMEM;
1304 drv = alloc_tty_driver(capi_ttyminors);
1305 if (!drv) {
1306 kfree(capiminors);
1307 return -ENOMEM;
1309 drv->driver_name = "capi_nc";
1310 drv->name = "capi!";
1311 drv->major = 0;
1312 drv->minor_start = 0;
1313 drv->type = TTY_DRIVER_TYPE_SERIAL;
1314 drv->subtype = SERIAL_TYPE_NORMAL;
1315 drv->init_termios = tty_std_termios;
1316 drv->init_termios.c_iflag = ICRNL;
1317 drv->init_termios.c_oflag = OPOST | ONLCR;
1318 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1319 drv->init_termios.c_lflag = 0;
1320 drv->flags =
1321 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1322 TTY_DRIVER_DYNAMIC_DEV;
1323 tty_set_operations(drv, &capinc_ops);
1325 err = tty_register_driver(drv);
1326 if (err) {
1327 put_tty_driver(drv);
1328 kfree(capiminors);
1329 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1330 return err;
1332 capinc_tty_driver = drv;
1333 return 0;
1336 static void __exit capinc_tty_exit(void)
1338 tty_unregister_driver(capinc_tty_driver);
1339 put_tty_driver(capinc_tty_driver);
1340 kfree(capiminors);
1343 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1345 static inline int capinc_tty_init(void)
1347 return 0;
1350 static inline void capinc_tty_exit(void) { }
1352 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1354 /* -------- /proc functions ----------------------------------------- */
1357 * /proc/capi/capi20:
1358 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1360 static int __maybe_unused capi20_proc_show(struct seq_file *m, void *v)
1362 struct capidev *cdev;
1363 struct list_head *l;
1365 mutex_lock(&capidev_list_lock);
1366 list_for_each(l, &capidev_list) {
1367 cdev = list_entry(l, struct capidev, list);
1368 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1369 cdev->ap.applid,
1370 cdev->ap.nrecvctlpkt,
1371 cdev->ap.nrecvdatapkt,
1372 cdev->ap.nsentctlpkt,
1373 cdev->ap.nsentdatapkt);
1375 mutex_unlock(&capidev_list_lock);
1376 return 0;
1380 * /proc/capi/capi20ncci:
1381 * applid ncci
1383 static int __maybe_unused capi20ncci_proc_show(struct seq_file *m, void *v)
1385 struct capidev *cdev;
1386 struct capincci *np;
1388 mutex_lock(&capidev_list_lock);
1389 list_for_each_entry(cdev, &capidev_list, list) {
1390 mutex_lock(&cdev->lock);
1391 list_for_each_entry(np, &cdev->nccis, list)
1392 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
1393 mutex_unlock(&cdev->lock);
1395 mutex_unlock(&capidev_list_lock);
1396 return 0;
1399 static void __init proc_init(void)
1401 proc_create_single("capi/capi20", 0, NULL, capi20_proc_show);
1402 proc_create_single("capi/capi20ncci", 0, NULL, capi20ncci_proc_show);
1405 static void __exit proc_exit(void)
1407 remove_proc_entry("capi/capi20", NULL);
1408 remove_proc_entry("capi/capi20ncci", NULL);
1411 /* -------- init function and module interface ---------------------- */
1414 static int __init capi_init(void)
1416 const char *compileinfo;
1417 int major_ret;
1418 int ret;
1420 ret = kcapi_init();
1421 if (ret)
1422 return ret;
1424 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1425 if (major_ret < 0) {
1426 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1427 kcapi_exit();
1428 return major_ret;
1430 capi_class = class_create(THIS_MODULE, "capi");
1431 if (IS_ERR(capi_class)) {
1432 unregister_chrdev(capi_major, "capi20");
1433 kcapi_exit();
1434 return PTR_ERR(capi_class);
1437 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi20");
1439 if (capinc_tty_init() < 0) {
1440 device_destroy(capi_class, MKDEV(capi_major, 0));
1441 class_destroy(capi_class);
1442 unregister_chrdev(capi_major, "capi20");
1443 kcapi_exit();
1444 return -ENOMEM;
1447 proc_init();
1449 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1450 compileinfo = " (middleware)";
1451 #else
1452 compileinfo = " (no middleware)";
1453 #endif
1454 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1455 capi_major, compileinfo);
1457 return 0;
1460 static void __exit capi_exit(void)
1462 proc_exit();
1464 device_destroy(capi_class, MKDEV(capi_major, 0));
1465 class_destroy(capi_class);
1466 unregister_chrdev(capi_major, "capi20");
1468 capinc_tty_exit();
1470 kcapi_exit();
1473 module_init(capi_init);
1474 module_exit(capi_exit);