powercap: restrict energy meter to root access
[linux/fpc-iii.git] / drivers / isdn / capi / capi.c
blob691ad069444d49beb9185ce6ee73ab6af046b52b
1 /* $Id: capi.c,v 1.1.2.7 2004/04/28 09:48:59 armin Exp $
3 * CAPI 2.0 Interface for Linux
5 * Copyright 1996 by Carsten Paeth <calle@calle.de>
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
12 #include <linux/module.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/major.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/fcntl.h>
19 #include <linux/fs.h>
20 #include <linux/signal.h>
21 #include <linux/mutex.h>
22 #include <linux/mm.h>
23 #include <linux/timer.h>
24 #include <linux/wait.h>
25 #include <linux/tty.h>
26 #include <linux/netdevice.h>
27 #include <linux/ppp_defs.h>
28 #include <linux/ppp-ioctl.h>
29 #include <linux/skbuff.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/poll.h>
33 #include <linux/capi.h>
34 #include <linux/kernelcapi.h>
35 #include <linux/init.h>
36 #include <linux/device.h>
37 #include <linux/moduleparam.h>
38 #include <linux/isdn/capiutil.h>
39 #include <linux/isdn/capicmd.h>
41 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
42 MODULE_AUTHOR("Carsten Paeth");
43 MODULE_LICENSE("GPL");
45 /* -------- driver information -------------------------------------- */
47 static DEFINE_MUTEX(capi_mutex);
48 static struct class *capi_class;
49 static int capi_major = 68; /* allocated */
51 module_param_named(major, capi_major, uint, 0);
53 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
54 #define CAPINC_NR_PORTS 32
55 #define CAPINC_MAX_PORTS 256
57 static int capi_ttyminors = CAPINC_NR_PORTS;
59 module_param_named(ttyminors, capi_ttyminors, uint, 0);
60 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
62 /* -------- defines ------------------------------------------------- */
64 #define CAPINC_MAX_RECVQUEUE 10
65 #define CAPINC_MAX_SENDQUEUE 10
66 #define CAPI_MAX_BLKSIZE 2048
68 /* -------- data structures ----------------------------------------- */
70 struct capidev;
71 struct capincci;
72 struct capiminor;
74 struct ackqueue_entry {
75 struct list_head list;
76 u16 datahandle;
79 struct capiminor {
80 unsigned int minor;
82 struct capi20_appl *ap;
83 u32 ncci;
84 atomic_t datahandle;
85 atomic_t msgid;
87 struct tty_port port;
88 int ttyinstop;
89 int ttyoutstop;
91 struct sk_buff_head inqueue;
93 struct sk_buff_head outqueue;
94 int outbytes;
95 struct sk_buff *outskb;
96 spinlock_t outlock;
98 /* transmit path */
99 struct list_head ackqueue;
100 int nack;
101 spinlock_t ackqlock;
104 struct capincci {
105 struct list_head list;
106 u32 ncci;
107 struct capidev *cdev;
108 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
109 struct capiminor *minorp;
110 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
113 struct capidev {
114 struct list_head list;
115 struct capi20_appl ap;
116 u16 errcode;
117 unsigned userflags;
119 struct sk_buff_head recvqueue;
120 wait_queue_head_t recvwait;
122 struct list_head nccis;
124 struct mutex lock;
127 /* -------- global variables ---------------------------------------- */
129 static DEFINE_MUTEX(capidev_list_lock);
130 static LIST_HEAD(capidev_list);
132 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
134 static DEFINE_SPINLOCK(capiminors_lock);
135 static struct capiminor **capiminors;
137 static struct tty_driver *capinc_tty_driver;
139 /* -------- datahandles --------------------------------------------- */
141 static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
143 struct ackqueue_entry *n;
145 n = kmalloc(sizeof(*n), GFP_ATOMIC);
146 if (unlikely(!n)) {
147 printk(KERN_ERR "capi: alloc datahandle failed\n");
148 return -1;
150 n->datahandle = datahandle;
151 INIT_LIST_HEAD(&n->list);
152 spin_lock_bh(&mp->ackqlock);
153 list_add_tail(&n->list, &mp->ackqueue);
154 mp->nack++;
155 spin_unlock_bh(&mp->ackqlock);
156 return 0;
159 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
161 struct ackqueue_entry *p, *tmp;
163 spin_lock_bh(&mp->ackqlock);
164 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
165 if (p->datahandle == datahandle) {
166 list_del(&p->list);
167 mp->nack--;
168 spin_unlock_bh(&mp->ackqlock);
169 kfree(p);
170 return 0;
173 spin_unlock_bh(&mp->ackqlock);
174 return -1;
177 static void capiminor_del_all_ack(struct capiminor *mp)
179 struct ackqueue_entry *p, *tmp;
181 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
182 list_del(&p->list);
183 kfree(p);
184 mp->nack--;
189 /* -------- struct capiminor ---------------------------------------- */
191 static void capiminor_destroy(struct tty_port *port)
193 struct capiminor *mp = container_of(port, struct capiminor, port);
195 kfree_skb(mp->outskb);
196 skb_queue_purge(&mp->inqueue);
197 skb_queue_purge(&mp->outqueue);
198 capiminor_del_all_ack(mp);
199 kfree(mp);
202 static const struct tty_port_operations capiminor_port_ops = {
203 .destruct = capiminor_destroy,
206 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
208 struct capiminor *mp;
209 struct device *dev;
210 unsigned int minor;
212 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
213 if (!mp) {
214 printk(KERN_ERR "capi: can't alloc capiminor\n");
215 return NULL;
218 mp->ap = ap;
219 mp->ncci = ncci;
220 INIT_LIST_HEAD(&mp->ackqueue);
221 spin_lock_init(&mp->ackqlock);
223 skb_queue_head_init(&mp->inqueue);
224 skb_queue_head_init(&mp->outqueue);
225 spin_lock_init(&mp->outlock);
227 tty_port_init(&mp->port);
228 mp->port.ops = &capiminor_port_ops;
230 /* Allocate the least unused minor number. */
231 spin_lock(&capiminors_lock);
232 for (minor = 0; minor < capi_ttyminors; minor++)
233 if (!capiminors[minor]) {
234 capiminors[minor] = mp;
235 break;
237 spin_unlock(&capiminors_lock);
239 if (minor == capi_ttyminors) {
240 printk(KERN_NOTICE "capi: out of minors\n");
241 goto err_out1;
244 mp->minor = minor;
246 dev = tty_port_register_device(&mp->port, capinc_tty_driver, minor,
247 NULL);
248 if (IS_ERR(dev))
249 goto err_out2;
251 return mp;
253 err_out2:
254 spin_lock(&capiminors_lock);
255 capiminors[minor] = NULL;
256 spin_unlock(&capiminors_lock);
258 err_out1:
259 tty_port_put(&mp->port);
260 return NULL;
263 static struct capiminor *capiminor_get(unsigned int minor)
265 struct capiminor *mp;
267 spin_lock(&capiminors_lock);
268 mp = capiminors[minor];
269 if (mp)
270 tty_port_get(&mp->port);
271 spin_unlock(&capiminors_lock);
273 return mp;
276 static inline void capiminor_put(struct capiminor *mp)
278 tty_port_put(&mp->port);
281 static void capiminor_free(struct capiminor *mp)
283 tty_unregister_device(capinc_tty_driver, mp->minor);
285 spin_lock(&capiminors_lock);
286 capiminors[mp->minor] = NULL;
287 spin_unlock(&capiminors_lock);
289 capiminor_put(mp);
292 /* -------- struct capincci ----------------------------------------- */
294 static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
296 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
297 np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
300 static void capincci_free_minor(struct capincci *np)
302 struct capiminor *mp = np->minorp;
303 struct tty_struct *tty;
305 if (mp) {
306 tty = tty_port_tty_get(&mp->port);
307 if (tty) {
308 tty_vhangup(tty);
309 tty_kref_put(tty);
312 capiminor_free(mp);
316 static inline unsigned int capincci_minor_opencount(struct capincci *np)
318 struct capiminor *mp = np->minorp;
319 unsigned int count = 0;
320 struct tty_struct *tty;
322 if (mp) {
323 tty = tty_port_tty_get(&mp->port);
324 if (tty) {
325 count = tty->count;
326 tty_kref_put(tty);
329 return count;
332 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
334 static inline void
335 capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
336 static inline void capincci_free_minor(struct capincci *np) { }
338 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
340 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
342 struct capincci *np;
344 np = kzalloc(sizeof(*np), GFP_KERNEL);
345 if (!np)
346 return NULL;
347 np->ncci = ncci;
348 np->cdev = cdev;
350 capincci_alloc_minor(cdev, np);
352 list_add_tail(&np->list, &cdev->nccis);
354 return np;
357 static void capincci_free(struct capidev *cdev, u32 ncci)
359 struct capincci *np, *tmp;
361 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
362 if (ncci == 0xffffffff || np->ncci == ncci) {
363 capincci_free_minor(np);
364 list_del(&np->list);
365 kfree(np);
369 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
370 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
372 struct capincci *np;
374 list_for_each_entry(np, &cdev->nccis, list)
375 if (np->ncci == ncci)
376 return np;
377 return NULL;
380 /* -------- handle data queue --------------------------------------- */
382 static struct sk_buff *
383 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
385 struct sk_buff *nskb;
386 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_KERNEL);
387 if (nskb) {
388 u16 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
389 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
390 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
391 capimsg_setu16(s, 2, mp->ap->applid);
392 capimsg_setu8 (s, 4, CAPI_DATA_B3);
393 capimsg_setu8 (s, 5, CAPI_RESP);
394 capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
395 capimsg_setu32(s, 8, mp->ncci);
396 capimsg_setu16(s, 12, datahandle);
398 return nskb;
401 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
403 unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
404 struct tty_struct *tty;
405 struct sk_buff *nskb;
406 u16 errcode, datahandle;
407 struct tty_ldisc *ld;
408 int ret = -1;
410 tty = tty_port_tty_get(&mp->port);
411 if (!tty) {
412 pr_debug("capi: currently no receiver\n");
413 return -1;
416 ld = tty_ldisc_ref(tty);
417 if (!ld) {
418 /* fatal error, do not requeue */
419 ret = 0;
420 kfree_skb(skb);
421 goto deref_tty;
424 if (ld->ops->receive_buf == NULL) {
425 pr_debug("capi: ldisc has no receive_buf function\n");
426 /* fatal error, do not requeue */
427 goto free_skb;
429 if (mp->ttyinstop) {
430 pr_debug("capi: recv tty throttled\n");
431 goto deref_ldisc;
434 if (tty->receive_room < datalen) {
435 pr_debug("capi: no room in tty\n");
436 goto deref_ldisc;
439 nskb = gen_data_b3_resp_for(mp, skb);
440 if (!nskb) {
441 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
442 goto deref_ldisc;
445 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
447 errcode = capi20_put_message(mp->ap, nskb);
449 if (errcode == CAPI_NOERROR) {
450 skb_pull(skb, CAPIMSG_LEN(skb->data));
451 pr_debug("capi: DATA_B3_RESP %u len=%d => ldisc\n",
452 datahandle, skb->len);
453 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
454 } else {
455 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
456 errcode);
457 kfree_skb(nskb);
459 if (errcode == CAPI_SENDQUEUEFULL)
460 goto deref_ldisc;
463 free_skb:
464 ret = 0;
465 kfree_skb(skb);
467 deref_ldisc:
468 tty_ldisc_deref(ld);
470 deref_tty:
471 tty_kref_put(tty);
472 return ret;
475 static void handle_minor_recv(struct capiminor *mp)
477 struct sk_buff *skb;
479 while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
480 if (handle_recv_skb(mp, skb) < 0) {
481 skb_queue_head(&mp->inqueue, skb);
482 return;
486 static void handle_minor_send(struct capiminor *mp)
488 struct tty_struct *tty;
489 struct sk_buff *skb;
490 u16 len;
491 u16 errcode;
492 u16 datahandle;
494 tty = tty_port_tty_get(&mp->port);
495 if (!tty)
496 return;
498 if (mp->ttyoutstop) {
499 pr_debug("capi: send: tty stopped\n");
500 tty_kref_put(tty);
501 return;
504 while (1) {
505 spin_lock_bh(&mp->outlock);
506 skb = __skb_dequeue(&mp->outqueue);
507 if (!skb) {
508 spin_unlock_bh(&mp->outlock);
509 break;
511 len = (u16)skb->len;
512 mp->outbytes -= len;
513 spin_unlock_bh(&mp->outlock);
515 datahandle = atomic_inc_return(&mp->datahandle);
516 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
517 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
518 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
519 capimsg_setu16(skb->data, 2, mp->ap->applid);
520 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
521 capimsg_setu8 (skb->data, 5, CAPI_REQ);
522 capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
523 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
524 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
525 capimsg_setu16(skb->data, 16, len); /* Data length */
526 capimsg_setu16(skb->data, 18, datahandle);
527 capimsg_setu16(skb->data, 20, 0); /* Flags */
529 if (capiminor_add_ack(mp, datahandle) < 0) {
530 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
532 spin_lock_bh(&mp->outlock);
533 __skb_queue_head(&mp->outqueue, skb);
534 mp->outbytes += len;
535 spin_unlock_bh(&mp->outlock);
537 break;
539 errcode = capi20_put_message(mp->ap, skb);
540 if (errcode == CAPI_NOERROR) {
541 pr_debug("capi: DATA_B3_REQ %u len=%u\n",
542 datahandle, len);
543 continue;
545 capiminor_del_ack(mp, datahandle);
547 if (errcode == CAPI_SENDQUEUEFULL) {
548 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
550 spin_lock_bh(&mp->outlock);
551 __skb_queue_head(&mp->outqueue, skb);
552 mp->outbytes += len;
553 spin_unlock_bh(&mp->outlock);
555 break;
558 /* ups, drop packet */
559 printk(KERN_ERR "capi: put_message = %x\n", errcode);
560 kfree_skb(skb);
562 tty_kref_put(tty);
565 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
566 /* -------- function called by lower level -------------------------- */
568 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
570 struct capidev *cdev = ap->private;
571 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
572 struct capiminor *mp;
573 u16 datahandle;
574 struct capincci *np;
575 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
577 mutex_lock(&cdev->lock);
579 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
580 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
581 if ((info & 0xff00) == 0)
582 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
584 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
585 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
587 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
588 skb_queue_tail(&cdev->recvqueue, skb);
589 wake_up_interruptible(&cdev->recvwait);
590 goto unlock_out;
593 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
594 skb_queue_tail(&cdev->recvqueue, skb);
595 wake_up_interruptible(&cdev->recvwait);
597 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
599 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
600 if (!np) {
601 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
602 skb_queue_tail(&cdev->recvqueue, skb);
603 wake_up_interruptible(&cdev->recvwait);
604 goto unlock_out;
607 mp = np->minorp;
608 if (!mp) {
609 skb_queue_tail(&cdev->recvqueue, skb);
610 wake_up_interruptible(&cdev->recvwait);
611 goto unlock_out;
613 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
614 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
615 pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
616 datahandle, skb->len-CAPIMSG_LEN(skb->data));
617 skb_queue_tail(&mp->inqueue, skb);
619 handle_minor_recv(mp);
621 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
623 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
624 pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
625 datahandle,
626 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 2));
627 kfree_skb(skb);
628 capiminor_del_ack(mp, datahandle);
629 tty_port_tty_wakeup(&mp->port);
630 handle_minor_send(mp);
632 } else {
633 /* ups, let capi application handle it :-) */
634 skb_queue_tail(&cdev->recvqueue, skb);
635 wake_up_interruptible(&cdev->recvwait);
637 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
639 unlock_out:
640 mutex_unlock(&cdev->lock);
643 /* -------- file_operations for capidev ----------------------------- */
645 static ssize_t
646 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
648 struct capidev *cdev = file->private_data;
649 struct sk_buff *skb;
650 size_t copied;
651 int err;
653 if (!cdev->ap.applid)
654 return -ENODEV;
656 skb = skb_dequeue(&cdev->recvqueue);
657 if (!skb) {
658 if (file->f_flags & O_NONBLOCK)
659 return -EAGAIN;
660 err = wait_event_interruptible(cdev->recvwait,
661 (skb = skb_dequeue(&cdev->recvqueue)));
662 if (err)
663 return err;
665 if (skb->len > count) {
666 skb_queue_head(&cdev->recvqueue, skb);
667 return -EMSGSIZE;
669 if (copy_to_user(buf, skb->data, skb->len)) {
670 skb_queue_head(&cdev->recvqueue, skb);
671 return -EFAULT;
673 copied = skb->len;
675 kfree_skb(skb);
677 return copied;
680 static ssize_t
681 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
683 struct capidev *cdev = file->private_data;
684 struct sk_buff *skb;
685 u16 mlen;
687 if (!cdev->ap.applid)
688 return -ENODEV;
690 if (count < CAPIMSG_BASELEN)
691 return -EINVAL;
693 skb = alloc_skb(count, GFP_USER);
694 if (!skb)
695 return -ENOMEM;
697 if (copy_from_user(skb_put(skb, count), buf, count)) {
698 kfree_skb(skb);
699 return -EFAULT;
701 mlen = CAPIMSG_LEN(skb->data);
702 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
703 if (count < CAPI_DATA_B3_REQ_LEN ||
704 (size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
705 kfree_skb(skb);
706 return -EINVAL;
708 } else {
709 if (mlen != count) {
710 kfree_skb(skb);
711 return -EINVAL;
714 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
716 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
717 if (count < CAPI_DISCONNECT_B3_RESP_LEN) {
718 kfree_skb(skb);
719 return -EINVAL;
721 mutex_lock(&cdev->lock);
722 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
723 mutex_unlock(&cdev->lock);
726 cdev->errcode = capi20_put_message(&cdev->ap, skb);
728 if (cdev->errcode) {
729 kfree_skb(skb);
730 return -EIO;
732 return count;
735 static unsigned int
736 capi_poll(struct file *file, poll_table *wait)
738 struct capidev *cdev = file->private_data;
739 unsigned int mask = 0;
741 if (!cdev->ap.applid)
742 return POLLERR;
744 poll_wait(file, &(cdev->recvwait), wait);
745 mask = POLLOUT | POLLWRNORM;
746 if (!skb_queue_empty(&cdev->recvqueue))
747 mask |= POLLIN | POLLRDNORM;
748 return mask;
751 static int
752 capi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
754 struct capidev *cdev = file->private_data;
755 capi_ioctl_struct data;
756 int retval = -EINVAL;
757 void __user *argp = (void __user *)arg;
759 switch (cmd) {
760 case CAPI_REGISTER:
761 mutex_lock(&cdev->lock);
763 if (cdev->ap.applid) {
764 retval = -EEXIST;
765 goto register_out;
767 if (copy_from_user(&cdev->ap.rparam, argp,
768 sizeof(struct capi_register_params))) {
769 retval = -EFAULT;
770 goto register_out;
772 cdev->ap.private = cdev;
773 cdev->ap.recv_message = capi_recv_message;
774 cdev->errcode = capi20_register(&cdev->ap);
775 retval = (int)cdev->ap.applid;
776 if (cdev->errcode) {
777 cdev->ap.applid = 0;
778 retval = -EIO;
781 register_out:
782 mutex_unlock(&cdev->lock);
783 return retval;
785 case CAPI_GET_VERSION:
786 if (copy_from_user(&data.contr, argp,
787 sizeof(data.contr)))
788 return -EFAULT;
789 cdev->errcode = capi20_get_version(data.contr, &data.version);
790 if (cdev->errcode)
791 return -EIO;
792 if (copy_to_user(argp, &data.version,
793 sizeof(data.version)))
794 return -EFAULT;
795 return 0;
797 case CAPI_GET_SERIAL:
798 if (copy_from_user(&data.contr, argp,
799 sizeof(data.contr)))
800 return -EFAULT;
801 cdev->errcode = capi20_get_serial(data.contr, data.serial);
802 if (cdev->errcode)
803 return -EIO;
804 if (copy_to_user(argp, data.serial,
805 sizeof(data.serial)))
806 return -EFAULT;
807 return 0;
809 case CAPI_GET_PROFILE:
810 if (copy_from_user(&data.contr, argp,
811 sizeof(data.contr)))
812 return -EFAULT;
814 if (data.contr == 0) {
815 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
816 if (cdev->errcode)
817 return -EIO;
819 retval = copy_to_user(argp,
820 &data.profile.ncontroller,
821 sizeof(data.profile.ncontroller));
823 } else {
824 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
825 if (cdev->errcode)
826 return -EIO;
828 retval = copy_to_user(argp, &data.profile,
829 sizeof(data.profile));
831 if (retval)
832 return -EFAULT;
833 return 0;
835 case CAPI_GET_MANUFACTURER:
836 if (copy_from_user(&data.contr, argp,
837 sizeof(data.contr)))
838 return -EFAULT;
839 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
840 if (cdev->errcode)
841 return -EIO;
843 if (copy_to_user(argp, data.manufacturer,
844 sizeof(data.manufacturer)))
845 return -EFAULT;
847 return 0;
849 case CAPI_GET_ERRCODE:
850 data.errcode = cdev->errcode;
851 cdev->errcode = CAPI_NOERROR;
852 if (arg) {
853 if (copy_to_user(argp, &data.errcode,
854 sizeof(data.errcode)))
855 return -EFAULT;
857 return data.errcode;
859 case CAPI_INSTALLED:
860 if (capi20_isinstalled() == CAPI_NOERROR)
861 return 0;
862 return -ENXIO;
864 case CAPI_MANUFACTURER_CMD: {
865 struct capi_manufacturer_cmd mcmd;
866 if (!capable(CAP_SYS_ADMIN))
867 return -EPERM;
868 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
869 return -EFAULT;
870 return capi20_manufacturer(mcmd.cmd, mcmd.data);
872 case CAPI_SET_FLAGS:
873 case CAPI_CLR_FLAGS: {
874 unsigned userflags;
876 if (copy_from_user(&userflags, argp, sizeof(userflags)))
877 return -EFAULT;
879 mutex_lock(&cdev->lock);
880 if (cmd == CAPI_SET_FLAGS)
881 cdev->userflags |= userflags;
882 else
883 cdev->userflags &= ~userflags;
884 mutex_unlock(&cdev->lock);
885 return 0;
887 case CAPI_GET_FLAGS:
888 if (copy_to_user(argp, &cdev->userflags,
889 sizeof(cdev->userflags)))
890 return -EFAULT;
891 return 0;
893 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
894 case CAPI_NCCI_OPENCOUNT:
895 return 0;
897 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
898 case CAPI_NCCI_OPENCOUNT: {
899 struct capincci *nccip;
900 unsigned ncci;
901 int count = 0;
903 if (copy_from_user(&ncci, argp, sizeof(ncci)))
904 return -EFAULT;
906 mutex_lock(&cdev->lock);
907 nccip = capincci_find(cdev, (u32)ncci);
908 if (nccip)
909 count = capincci_minor_opencount(nccip);
910 mutex_unlock(&cdev->lock);
911 return count;
914 case CAPI_NCCI_GETUNIT: {
915 struct capincci *nccip;
916 struct capiminor *mp;
917 unsigned ncci;
918 int unit = -ESRCH;
920 if (copy_from_user(&ncci, argp, sizeof(ncci)))
921 return -EFAULT;
923 mutex_lock(&cdev->lock);
924 nccip = capincci_find(cdev, (u32)ncci);
925 if (nccip) {
926 mp = nccip->minorp;
927 if (mp)
928 unit = mp->minor;
930 mutex_unlock(&cdev->lock);
931 return unit;
933 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
935 default:
936 return -EINVAL;
940 static long
941 capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
943 int ret;
945 mutex_lock(&capi_mutex);
946 ret = capi_ioctl(file, cmd, arg);
947 mutex_unlock(&capi_mutex);
949 return ret;
952 static int capi_open(struct inode *inode, struct file *file)
954 struct capidev *cdev;
956 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
957 if (!cdev)
958 return -ENOMEM;
960 mutex_init(&cdev->lock);
961 skb_queue_head_init(&cdev->recvqueue);
962 init_waitqueue_head(&cdev->recvwait);
963 INIT_LIST_HEAD(&cdev->nccis);
964 file->private_data = cdev;
966 mutex_lock(&capidev_list_lock);
967 list_add_tail(&cdev->list, &capidev_list);
968 mutex_unlock(&capidev_list_lock);
970 return nonseekable_open(inode, file);
973 static int capi_release(struct inode *inode, struct file *file)
975 struct capidev *cdev = file->private_data;
977 mutex_lock(&capidev_list_lock);
978 list_del(&cdev->list);
979 mutex_unlock(&capidev_list_lock);
981 if (cdev->ap.applid)
982 capi20_release(&cdev->ap);
983 skb_queue_purge(&cdev->recvqueue);
984 capincci_free(cdev, 0xffffffff);
986 kfree(cdev);
987 return 0;
990 static const struct file_operations capi_fops =
992 .owner = THIS_MODULE,
993 .llseek = no_llseek,
994 .read = capi_read,
995 .write = capi_write,
996 .poll = capi_poll,
997 .unlocked_ioctl = capi_unlocked_ioctl,
998 .open = capi_open,
999 .release = capi_release,
1002 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1003 /* -------- tty_operations for capincci ----------------------------- */
1005 static int
1006 capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1008 struct capiminor *mp = capiminor_get(tty->index);
1009 int ret = tty_standard_install(driver, tty);
1011 if (ret == 0)
1012 tty->driver_data = mp;
1013 else
1014 capiminor_put(mp);
1015 return ret;
1018 static void capinc_tty_cleanup(struct tty_struct *tty)
1020 struct capiminor *mp = tty->driver_data;
1021 tty->driver_data = NULL;
1022 capiminor_put(mp);
1025 static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
1027 struct capiminor *mp = tty->driver_data;
1028 int err;
1030 err = tty_port_open(&mp->port, tty, filp);
1031 if (err)
1032 return err;
1034 handle_minor_recv(mp);
1035 return 0;
1038 static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
1040 struct capiminor *mp = tty->driver_data;
1042 tty_port_close(&mp->port, tty, filp);
1045 static int capinc_tty_write(struct tty_struct *tty,
1046 const unsigned char *buf, int count)
1048 struct capiminor *mp = tty->driver_data;
1049 struct sk_buff *skb;
1051 pr_debug("capinc_tty_write(count=%d)\n", count);
1053 spin_lock_bh(&mp->outlock);
1054 skb = mp->outskb;
1055 if (skb) {
1056 mp->outskb = NULL;
1057 __skb_queue_tail(&mp->outqueue, skb);
1058 mp->outbytes += skb->len;
1061 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + count, GFP_ATOMIC);
1062 if (!skb) {
1063 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1064 spin_unlock_bh(&mp->outlock);
1065 return -ENOMEM;
1068 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1069 memcpy(skb_put(skb, count), buf, count);
1071 __skb_queue_tail(&mp->outqueue, skb);
1072 mp->outbytes += skb->len;
1073 spin_unlock_bh(&mp->outlock);
1075 handle_minor_send(mp);
1077 return count;
1080 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1082 struct capiminor *mp = tty->driver_data;
1083 bool invoke_send = false;
1084 struct sk_buff *skb;
1085 int ret = 1;
1087 pr_debug("capinc_put_char(%u)\n", ch);
1089 spin_lock_bh(&mp->outlock);
1090 skb = mp->outskb;
1091 if (skb) {
1092 if (skb_tailroom(skb) > 0) {
1093 *(skb_put(skb, 1)) = ch;
1094 goto unlock_out;
1096 mp->outskb = NULL;
1097 __skb_queue_tail(&mp->outqueue, skb);
1098 mp->outbytes += skb->len;
1099 invoke_send = true;
1102 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1103 if (skb) {
1104 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1105 *(skb_put(skb, 1)) = ch;
1106 mp->outskb = skb;
1107 } else {
1108 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1109 ret = 0;
1112 unlock_out:
1113 spin_unlock_bh(&mp->outlock);
1115 if (invoke_send)
1116 handle_minor_send(mp);
1118 return ret;
1121 static void capinc_tty_flush_chars(struct tty_struct *tty)
1123 struct capiminor *mp = tty->driver_data;
1124 struct sk_buff *skb;
1126 pr_debug("capinc_tty_flush_chars\n");
1128 spin_lock_bh(&mp->outlock);
1129 skb = mp->outskb;
1130 if (skb) {
1131 mp->outskb = NULL;
1132 __skb_queue_tail(&mp->outqueue, skb);
1133 mp->outbytes += skb->len;
1134 spin_unlock_bh(&mp->outlock);
1136 handle_minor_send(mp);
1137 } else
1138 spin_unlock_bh(&mp->outlock);
1140 handle_minor_recv(mp);
1143 static int capinc_tty_write_room(struct tty_struct *tty)
1145 struct capiminor *mp = tty->driver_data;
1146 int room;
1148 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1149 room *= CAPI_MAX_BLKSIZE;
1150 pr_debug("capinc_tty_write_room = %d\n", room);
1151 return room;
1154 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1156 struct capiminor *mp = tty->driver_data;
1158 pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1159 mp->outbytes, mp->nack,
1160 skb_queue_len(&mp->outqueue),
1161 skb_queue_len(&mp->inqueue));
1162 return mp->outbytes;
1165 static int capinc_tty_ioctl(struct tty_struct *tty,
1166 unsigned int cmd, unsigned long arg)
1168 return -ENOIOCTLCMD;
1171 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1173 pr_debug("capinc_tty_set_termios\n");
1176 static void capinc_tty_throttle(struct tty_struct *tty)
1178 struct capiminor *mp = tty->driver_data;
1179 pr_debug("capinc_tty_throttle\n");
1180 mp->ttyinstop = 1;
1183 static void capinc_tty_unthrottle(struct tty_struct *tty)
1185 struct capiminor *mp = tty->driver_data;
1187 pr_debug("capinc_tty_unthrottle\n");
1188 mp->ttyinstop = 0;
1189 handle_minor_recv(mp);
1192 static void capinc_tty_stop(struct tty_struct *tty)
1194 struct capiminor *mp = tty->driver_data;
1196 pr_debug("capinc_tty_stop\n");
1197 mp->ttyoutstop = 1;
1200 static void capinc_tty_start(struct tty_struct *tty)
1202 struct capiminor *mp = tty->driver_data;
1204 pr_debug("capinc_tty_start\n");
1205 mp->ttyoutstop = 0;
1206 handle_minor_send(mp);
1209 static void capinc_tty_hangup(struct tty_struct *tty)
1211 struct capiminor *mp = tty->driver_data;
1213 pr_debug("capinc_tty_hangup\n");
1214 tty_port_hangup(&mp->port);
1217 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1219 pr_debug("capinc_tty_break_ctl(%d)\n", state);
1220 return 0;
1223 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1225 pr_debug("capinc_tty_flush_buffer\n");
1228 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1230 pr_debug("capinc_tty_set_ldisc\n");
1233 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1235 pr_debug("capinc_tty_send_xchar(%d)\n", ch);
1238 static const struct tty_operations capinc_ops = {
1239 .open = capinc_tty_open,
1240 .close = capinc_tty_close,
1241 .write = capinc_tty_write,
1242 .put_char = capinc_tty_put_char,
1243 .flush_chars = capinc_tty_flush_chars,
1244 .write_room = capinc_tty_write_room,
1245 .chars_in_buffer = capinc_tty_chars_in_buffer,
1246 .ioctl = capinc_tty_ioctl,
1247 .set_termios = capinc_tty_set_termios,
1248 .throttle = capinc_tty_throttle,
1249 .unthrottle = capinc_tty_unthrottle,
1250 .stop = capinc_tty_stop,
1251 .start = capinc_tty_start,
1252 .hangup = capinc_tty_hangup,
1253 .break_ctl = capinc_tty_break_ctl,
1254 .flush_buffer = capinc_tty_flush_buffer,
1255 .set_ldisc = capinc_tty_set_ldisc,
1256 .send_xchar = capinc_tty_send_xchar,
1257 .install = capinc_tty_install,
1258 .cleanup = capinc_tty_cleanup,
1261 static int __init capinc_tty_init(void)
1263 struct tty_driver *drv;
1264 int err;
1266 if (capi_ttyminors > CAPINC_MAX_PORTS)
1267 capi_ttyminors = CAPINC_MAX_PORTS;
1268 if (capi_ttyminors <= 0)
1269 capi_ttyminors = CAPINC_NR_PORTS;
1271 capiminors = kzalloc(sizeof(struct capiminor *) * capi_ttyminors,
1272 GFP_KERNEL);
1273 if (!capiminors)
1274 return -ENOMEM;
1276 drv = alloc_tty_driver(capi_ttyminors);
1277 if (!drv) {
1278 kfree(capiminors);
1279 return -ENOMEM;
1281 drv->driver_name = "capi_nc";
1282 drv->name = "capi!";
1283 drv->major = 0;
1284 drv->minor_start = 0;
1285 drv->type = TTY_DRIVER_TYPE_SERIAL;
1286 drv->subtype = SERIAL_TYPE_NORMAL;
1287 drv->init_termios = tty_std_termios;
1288 drv->init_termios.c_iflag = ICRNL;
1289 drv->init_termios.c_oflag = OPOST | ONLCR;
1290 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1291 drv->init_termios.c_lflag = 0;
1292 drv->flags =
1293 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1294 TTY_DRIVER_DYNAMIC_DEV;
1295 tty_set_operations(drv, &capinc_ops);
1297 err = tty_register_driver(drv);
1298 if (err) {
1299 put_tty_driver(drv);
1300 kfree(capiminors);
1301 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1302 return err;
1304 capinc_tty_driver = drv;
1305 return 0;
1308 static void __exit capinc_tty_exit(void)
1310 tty_unregister_driver(capinc_tty_driver);
1311 put_tty_driver(capinc_tty_driver);
1312 kfree(capiminors);
1315 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1317 static inline int capinc_tty_init(void)
1319 return 0;
1322 static inline void capinc_tty_exit(void) { }
1324 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1326 /* -------- /proc functions ----------------------------------------- */
1329 * /proc/capi/capi20:
1330 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1332 static int capi20_proc_show(struct seq_file *m, void *v)
1334 struct capidev *cdev;
1335 struct list_head *l;
1337 mutex_lock(&capidev_list_lock);
1338 list_for_each(l, &capidev_list) {
1339 cdev = list_entry(l, struct capidev, list);
1340 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1341 cdev->ap.applid,
1342 cdev->ap.nrecvctlpkt,
1343 cdev->ap.nrecvdatapkt,
1344 cdev->ap.nsentctlpkt,
1345 cdev->ap.nsentdatapkt);
1347 mutex_unlock(&capidev_list_lock);
1348 return 0;
1351 static int capi20_proc_open(struct inode *inode, struct file *file)
1353 return single_open(file, capi20_proc_show, NULL);
1356 static const struct file_operations capi20_proc_fops = {
1357 .owner = THIS_MODULE,
1358 .open = capi20_proc_open,
1359 .read = seq_read,
1360 .llseek = seq_lseek,
1361 .release = single_release,
1365 * /proc/capi/capi20ncci:
1366 * applid ncci
1368 static int capi20ncci_proc_show(struct seq_file *m, void *v)
1370 struct capidev *cdev;
1371 struct capincci *np;
1373 mutex_lock(&capidev_list_lock);
1374 list_for_each_entry(cdev, &capidev_list, list) {
1375 mutex_lock(&cdev->lock);
1376 list_for_each_entry(np, &cdev->nccis, list)
1377 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
1378 mutex_unlock(&cdev->lock);
1380 mutex_unlock(&capidev_list_lock);
1381 return 0;
1384 static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1386 return single_open(file, capi20ncci_proc_show, NULL);
1389 static const struct file_operations capi20ncci_proc_fops = {
1390 .owner = THIS_MODULE,
1391 .open = capi20ncci_proc_open,
1392 .read = seq_read,
1393 .llseek = seq_lseek,
1394 .release = single_release,
1397 static void __init proc_init(void)
1399 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1400 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
1403 static void __exit proc_exit(void)
1405 remove_proc_entry("capi/capi20", NULL);
1406 remove_proc_entry("capi/capi20ncci", NULL);
1409 /* -------- init function and module interface ---------------------- */
1412 static int __init capi_init(void)
1414 const char *compileinfo;
1415 int major_ret;
1417 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1418 if (major_ret < 0) {
1419 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1420 return major_ret;
1422 capi_class = class_create(THIS_MODULE, "capi");
1423 if (IS_ERR(capi_class)) {
1424 unregister_chrdev(capi_major, "capi20");
1425 return PTR_ERR(capi_class);
1428 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi20");
1430 if (capinc_tty_init() < 0) {
1431 device_destroy(capi_class, MKDEV(capi_major, 0));
1432 class_destroy(capi_class);
1433 unregister_chrdev(capi_major, "capi20");
1434 return -ENOMEM;
1437 proc_init();
1439 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1440 compileinfo = " (middleware)";
1441 #else
1442 compileinfo = " (no middleware)";
1443 #endif
1444 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1445 capi_major, compileinfo);
1447 return 0;
1450 static void __exit capi_exit(void)
1452 proc_exit();
1454 device_destroy(capi_class, MKDEV(capi_major, 0));
1455 class_destroy(capi_class);
1456 unregister_chrdev(capi_major, "capi20");
1458 capinc_tty_exit();
1461 module_init(capi_init);
1462 module_exit(capi_exit);