vmscan: split LRU lists into anon & file sets
[linux-2.6/linux-2.6-openrd.git] / drivers / isdn / capi / capi.c
blob1b5bf87c4cf4719cd66e75575d3b97250e368e06
1 /* $Id: capi.c,v 1.1.2.7 2004/04/28 09:48:59 armin Exp $
3 * CAPI 2.0 Interface for Linux
5 * Copyright 1996 by Carsten Paeth <calle@calle.de>
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
12 #include <linux/module.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/major.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/fcntl.h>
19 #include <linux/fs.h>
20 #include <linux/signal.h>
21 #include <linux/mutex.h>
22 #include <linux/mm.h>
23 #include <linux/smp_lock.h>
24 #include <linux/timer.h>
25 #include <linux/wait.h>
26 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
27 #include <linux/tty.h>
28 #ifdef CONFIG_PPP
29 #include <linux/netdevice.h>
30 #include <linux/ppp_defs.h>
31 #include <linux/if_ppp.h>
32 #endif /* CONFIG_PPP */
33 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
34 #include <linux/skbuff.h>
35 #include <linux/proc_fs.h>
36 #include <linux/poll.h>
37 #include <linux/capi.h>
38 #include <linux/kernelcapi.h>
39 #include <linux/init.h>
40 #include <linux/device.h>
41 #include <linux/moduleparam.h>
42 #include <linux/isdn/capiutil.h>
43 #include <linux/isdn/capicmd.h>
44 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
45 #include "capifs.h"
46 #endif
48 static char *revision = "$Revision: 1.1.2.7 $";
50 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
51 MODULE_AUTHOR("Carsten Paeth");
52 MODULE_LICENSE("GPL");
54 #undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
55 #undef _DEBUG_TTYFUNCS /* call to tty_driver */
56 #undef _DEBUG_DATAFLOW /* data flow */
58 /* -------- driver information -------------------------------------- */
60 static struct class *capi_class;
62 static int capi_major = 68; /* allocated */
63 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
64 #define CAPINC_NR_PORTS 32
65 #define CAPINC_MAX_PORTS 256
66 static int capi_ttymajor = 191;
67 static int capi_ttyminors = CAPINC_NR_PORTS;
68 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
70 module_param_named(major, capi_major, uint, 0);
71 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
72 module_param_named(ttymajor, capi_ttymajor, uint, 0);
73 module_param_named(ttyminors, capi_ttyminors, uint, 0);
74 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
76 /* -------- defines ------------------------------------------------- */
78 #define CAPINC_MAX_RECVQUEUE 10
79 #define CAPINC_MAX_SENDQUEUE 10
80 #define CAPI_MAX_BLKSIZE 2048
82 /* -------- data structures ----------------------------------------- */
84 struct capidev;
85 struct capincci;
86 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
87 struct capiminor;
89 struct datahandle_queue {
90 struct list_head list;
91 u16 datahandle;
94 struct capiminor {
95 struct list_head list;
96 struct capincci *nccip;
97 unsigned int minor;
99 struct capi20_appl *ap;
100 u32 ncci;
101 u16 datahandle;
102 u16 msgid;
104 struct tty_struct *tty;
105 int ttyinstop;
106 int ttyoutstop;
107 struct sk_buff *ttyskb;
108 atomic_t ttyopencount;
110 struct sk_buff_head inqueue;
111 int inbytes;
112 struct sk_buff_head outqueue;
113 int outbytes;
115 /* transmit path */
116 struct list_head ackqueue;
117 int nack;
118 spinlock_t ackqlock;
120 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
122 /* FIXME: The following lock is a sledgehammer-workaround to a
123 * locking issue with the capiminor (and maybe other) data structure(s).
124 * Access to this data is done in a racy way and crashes the machine with
125 * a FritzCard DSL driver; sooner or later. This is a workaround
126 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
127 * The correct (and scalable) fix for the issue seems to require
128 * an API change to the drivers... . */
129 static DEFINE_SPINLOCK(workaround_lock);
131 struct capincci {
132 struct capincci *next;
133 u32 ncci;
134 struct capidev *cdev;
135 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
136 struct capiminor *minorp;
137 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
140 struct capidev {
141 struct list_head list;
142 struct capi20_appl ap;
143 u16 errcode;
144 unsigned userflags;
146 struct sk_buff_head recvqueue;
147 wait_queue_head_t recvwait;
149 struct capincci *nccis;
151 struct mutex ncci_list_mtx;
154 /* -------- global variables ---------------------------------------- */
156 static DEFINE_RWLOCK(capidev_list_lock);
157 static LIST_HEAD(capidev_list);
159 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
160 static DEFINE_RWLOCK(capiminor_list_lock);
161 static LIST_HEAD(capiminor_list);
162 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
164 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
165 /* -------- datahandles --------------------------------------------- */
167 static int capincci_add_ack(struct capiminor *mp, u16 datahandle)
169 struct datahandle_queue *n;
170 unsigned long flags;
172 n = kmalloc(sizeof(*n), GFP_ATOMIC);
173 if (unlikely(!n)) {
174 printk(KERN_ERR "capi: alloc datahandle failed\n");
175 return -1;
177 n->datahandle = datahandle;
178 INIT_LIST_HEAD(&n->list);
179 spin_lock_irqsave(&mp->ackqlock, flags);
180 list_add_tail(&n->list, &mp->ackqueue);
181 mp->nack++;
182 spin_unlock_irqrestore(&mp->ackqlock, flags);
183 return 0;
186 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
188 struct datahandle_queue *p, *tmp;
189 unsigned long flags;
191 spin_lock_irqsave(&mp->ackqlock, flags);
192 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
193 if (p->datahandle == datahandle) {
194 list_del(&p->list);
195 kfree(p);
196 mp->nack--;
197 spin_unlock_irqrestore(&mp->ackqlock, flags);
198 return 0;
201 spin_unlock_irqrestore(&mp->ackqlock, flags);
202 return -1;
205 static void capiminor_del_all_ack(struct capiminor *mp)
207 struct datahandle_queue *p, *tmp;
208 unsigned long flags;
210 spin_lock_irqsave(&mp->ackqlock, flags);
211 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
212 list_del(&p->list);
213 kfree(p);
214 mp->nack--;
216 spin_unlock_irqrestore(&mp->ackqlock, flags);
220 /* -------- struct capiminor ---------------------------------------- */
222 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
224 struct capiminor *mp, *p;
225 unsigned int minor = 0;
226 unsigned long flags;
228 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
229 if (!mp) {
230 printk(KERN_ERR "capi: can't alloc capiminor\n");
231 return NULL;
234 mp->ap = ap;
235 mp->ncci = ncci;
236 mp->msgid = 0;
237 atomic_set(&mp->ttyopencount,0);
238 INIT_LIST_HEAD(&mp->ackqueue);
239 spin_lock_init(&mp->ackqlock);
241 skb_queue_head_init(&mp->inqueue);
242 skb_queue_head_init(&mp->outqueue);
244 /* Allocate the least unused minor number.
246 write_lock_irqsave(&capiminor_list_lock, flags);
247 if (list_empty(&capiminor_list))
248 list_add(&mp->list, &capiminor_list);
249 else {
250 list_for_each_entry(p, &capiminor_list, list) {
251 if (p->minor > minor)
252 break;
253 minor++;
256 if (minor < capi_ttyminors) {
257 mp->minor = minor;
258 list_add(&mp->list, p->list.prev);
261 write_unlock_irqrestore(&capiminor_list_lock, flags);
263 if (!(minor < capi_ttyminors)) {
264 printk(KERN_NOTICE "capi: out of minors\n");
265 kfree(mp);
266 return NULL;
269 return mp;
272 static void capiminor_free(struct capiminor *mp)
274 unsigned long flags;
276 write_lock_irqsave(&capiminor_list_lock, flags);
277 list_del(&mp->list);
278 write_unlock_irqrestore(&capiminor_list_lock, flags);
280 if (mp->ttyskb) kfree_skb(mp->ttyskb);
281 mp->ttyskb = NULL;
282 skb_queue_purge(&mp->inqueue);
283 skb_queue_purge(&mp->outqueue);
284 capiminor_del_all_ack(mp);
285 kfree(mp);
288 static struct capiminor *capiminor_find(unsigned int minor)
290 struct list_head *l;
291 struct capiminor *p = NULL;
293 read_lock(&capiminor_list_lock);
294 list_for_each(l, &capiminor_list) {
295 p = list_entry(l, struct capiminor, list);
296 if (p->minor == minor)
297 break;
299 read_unlock(&capiminor_list_lock);
300 if (l == &capiminor_list)
301 return NULL;
303 return p;
305 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
307 /* -------- struct capincci ----------------------------------------- */
309 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
311 struct capincci *np, **pp;
312 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
313 struct capiminor *mp = NULL;
314 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
316 np = kzalloc(sizeof(*np), GFP_ATOMIC);
317 if (!np)
318 return NULL;
319 np->ncci = ncci;
320 np->cdev = cdev;
321 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
322 mp = NULL;
323 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
324 mp = np->minorp = capiminor_alloc(&cdev->ap, ncci);
325 if (mp) {
326 mp->nccip = np;
327 #ifdef _DEBUG_REFCOUNT
328 printk(KERN_DEBUG "set mp->nccip\n");
329 #endif
330 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
331 capifs_new_ncci(mp->minor, MKDEV(capi_ttymajor, mp->minor));
332 #endif
334 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
335 for (pp=&cdev->nccis; *pp; pp = &(*pp)->next)
337 *pp = np;
338 return np;
341 static void capincci_free(struct capidev *cdev, u32 ncci)
343 struct capincci *np, **pp;
344 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
345 struct capiminor *mp;
346 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
348 pp=&cdev->nccis;
349 while (*pp) {
350 np = *pp;
351 if (ncci == 0xffffffff || np->ncci == ncci) {
352 *pp = (*pp)->next;
353 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
354 if ((mp = np->minorp) != NULL) {
355 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
356 capifs_free_ncci(mp->minor);
357 #endif
358 if (mp->tty) {
359 mp->nccip = NULL;
360 #ifdef _DEBUG_REFCOUNT
361 printk(KERN_DEBUG "reset mp->nccip\n");
362 #endif
363 tty_hangup(mp->tty);
364 } else {
365 capiminor_free(mp);
368 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
369 kfree(np);
370 if (*pp == NULL) return;
371 } else {
372 pp = &(*pp)->next;
377 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
379 struct capincci *p;
381 for (p=cdev->nccis; p ; p = p->next) {
382 if (p->ncci == ncci)
383 break;
385 return p;
388 /* -------- struct capidev ------------------------------------------ */
390 static struct capidev *capidev_alloc(void)
392 struct capidev *cdev;
393 unsigned long flags;
395 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
396 if (!cdev)
397 return NULL;
399 mutex_init(&cdev->ncci_list_mtx);
400 skb_queue_head_init(&cdev->recvqueue);
401 init_waitqueue_head(&cdev->recvwait);
402 write_lock_irqsave(&capidev_list_lock, flags);
403 list_add_tail(&cdev->list, &capidev_list);
404 write_unlock_irqrestore(&capidev_list_lock, flags);
405 return cdev;
408 static void capidev_free(struct capidev *cdev)
410 unsigned long flags;
412 if (cdev->ap.applid) {
413 capi20_release(&cdev->ap);
414 cdev->ap.applid = 0;
416 skb_queue_purge(&cdev->recvqueue);
418 mutex_lock(&cdev->ncci_list_mtx);
419 capincci_free(cdev, 0xffffffff);
420 mutex_unlock(&cdev->ncci_list_mtx);
422 write_lock_irqsave(&capidev_list_lock, flags);
423 list_del(&cdev->list);
424 write_unlock_irqrestore(&capidev_list_lock, flags);
425 kfree(cdev);
428 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
429 /* -------- handle data queue --------------------------------------- */
431 static struct sk_buff *
432 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
434 struct sk_buff *nskb;
435 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
436 if (nskb) {
437 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
438 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
439 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
440 capimsg_setu16(s, 2, mp->ap->applid);
441 capimsg_setu8 (s, 4, CAPI_DATA_B3);
442 capimsg_setu8 (s, 5, CAPI_RESP);
443 capimsg_setu16(s, 6, mp->msgid++);
444 capimsg_setu32(s, 8, mp->ncci);
445 capimsg_setu16(s, 12, datahandle);
447 return nskb;
450 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
452 struct sk_buff *nskb;
453 int datalen;
454 u16 errcode, datahandle;
455 struct tty_ldisc *ld;
457 datalen = skb->len - CAPIMSG_LEN(skb->data);
458 if (mp->tty == NULL)
460 #ifdef _DEBUG_DATAFLOW
461 printk(KERN_DEBUG "capi: currently no receiver\n");
462 #endif
463 return -1;
466 ld = tty_ldisc_ref(mp->tty);
467 if (ld == NULL)
468 return -1;
469 if (ld->ops->receive_buf == NULL) {
470 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
471 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
472 #endif
473 goto bad;
475 if (mp->ttyinstop) {
476 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
477 printk(KERN_DEBUG "capi: recv tty throttled\n");
478 #endif
479 goto bad;
481 if (mp->tty->receive_room < datalen) {
482 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
483 printk(KERN_DEBUG "capi: no room in tty\n");
484 #endif
485 goto bad;
487 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
488 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
489 goto bad;
491 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
492 errcode = capi20_put_message(mp->ap, nskb);
493 if (errcode != CAPI_NOERROR) {
494 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
495 errcode);
496 kfree_skb(nskb);
497 goto bad;
499 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
500 #ifdef _DEBUG_DATAFLOW
501 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
502 datahandle, skb->len);
503 #endif
504 ld->ops->receive_buf(mp->tty, skb->data, NULL, skb->len);
505 kfree_skb(skb);
506 tty_ldisc_deref(ld);
507 return 0;
508 bad:
509 tty_ldisc_deref(ld);
510 return -1;
513 static void handle_minor_recv(struct capiminor *mp)
515 struct sk_buff *skb;
516 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
517 unsigned int len = skb->len;
518 mp->inbytes -= len;
519 if (handle_recv_skb(mp, skb) < 0) {
520 skb_queue_head(&mp->inqueue, skb);
521 mp->inbytes += len;
522 return;
527 static int handle_minor_send(struct capiminor *mp)
529 struct sk_buff *skb;
530 u16 len;
531 int count = 0;
532 u16 errcode;
533 u16 datahandle;
535 if (mp->tty && mp->ttyoutstop) {
536 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
537 printk(KERN_DEBUG "capi: send: tty stopped\n");
538 #endif
539 return 0;
542 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
543 datahandle = mp->datahandle;
544 len = (u16)skb->len;
545 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
546 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
547 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
548 capimsg_setu16(skb->data, 2, mp->ap->applid);
549 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
550 capimsg_setu8 (skb->data, 5, CAPI_REQ);
551 capimsg_setu16(skb->data, 6, mp->msgid++);
552 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
553 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
554 capimsg_setu16(skb->data, 16, len); /* Data length */
555 capimsg_setu16(skb->data, 18, datahandle);
556 capimsg_setu16(skb->data, 20, 0); /* Flags */
558 if (capincci_add_ack(mp, datahandle) < 0) {
559 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
560 skb_queue_head(&mp->outqueue, skb);
561 return count;
563 errcode = capi20_put_message(mp->ap, skb);
564 if (errcode == CAPI_NOERROR) {
565 mp->datahandle++;
566 count++;
567 mp->outbytes -= len;
568 #ifdef _DEBUG_DATAFLOW
569 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
570 datahandle, len);
571 #endif
572 continue;
574 capiminor_del_ack(mp, datahandle);
576 if (errcode == CAPI_SENDQUEUEFULL) {
577 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
578 skb_queue_head(&mp->outqueue, skb);
579 break;
582 /* ups, drop packet */
583 printk(KERN_ERR "capi: put_message = %x\n", errcode);
584 mp->outbytes -= len;
585 kfree_skb(skb);
587 return count;
590 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
591 /* -------- function called by lower level -------------------------- */
593 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
595 struct capidev *cdev = ap->private;
596 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
597 struct capiminor *mp;
598 u16 datahandle;
599 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
600 struct capincci *np;
601 u32 ncci;
602 unsigned long flags;
604 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
605 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
606 if (info == 0) {
607 mutex_lock(&cdev->ncci_list_mtx);
608 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
609 mutex_unlock(&cdev->ncci_list_mtx);
612 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) {
613 mutex_lock(&cdev->ncci_list_mtx);
614 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
615 mutex_unlock(&cdev->ncci_list_mtx);
617 spin_lock_irqsave(&workaround_lock, flags);
618 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
619 skb_queue_tail(&cdev->recvqueue, skb);
620 wake_up_interruptible(&cdev->recvwait);
621 spin_unlock_irqrestore(&workaround_lock, flags);
622 return;
624 ncci = CAPIMSG_CONTROL(skb->data);
625 for (np = cdev->nccis; np && np->ncci != ncci; np = np->next)
627 if (!np) {
628 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
629 skb_queue_tail(&cdev->recvqueue, skb);
630 wake_up_interruptible(&cdev->recvwait);
631 spin_unlock_irqrestore(&workaround_lock, flags);
632 return;
634 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
635 skb_queue_tail(&cdev->recvqueue, skb);
636 wake_up_interruptible(&cdev->recvwait);
637 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
638 mp = np->minorp;
639 if (!mp) {
640 skb_queue_tail(&cdev->recvqueue, skb);
641 wake_up_interruptible(&cdev->recvwait);
642 spin_unlock_irqrestore(&workaround_lock, flags);
643 return;
647 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
649 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
650 #ifdef _DEBUG_DATAFLOW
651 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
652 datahandle, skb->len-CAPIMSG_LEN(skb->data));
653 #endif
654 skb_queue_tail(&mp->inqueue, skb);
655 mp->inbytes += skb->len;
656 handle_minor_recv(mp);
658 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
660 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
661 #ifdef _DEBUG_DATAFLOW
662 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
663 datahandle,
664 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
665 #endif
666 kfree_skb(skb);
667 (void)capiminor_del_ack(mp, datahandle);
668 if (mp->tty)
669 tty_wakeup(mp->tty);
670 (void)handle_minor_send(mp);
672 } else {
673 /* ups, let capi application handle it :-) */
674 skb_queue_tail(&cdev->recvqueue, skb);
675 wake_up_interruptible(&cdev->recvwait);
677 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
678 spin_unlock_irqrestore(&workaround_lock, flags);
681 /* -------- file_operations for capidev ----------------------------- */
683 static ssize_t
684 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
686 struct capidev *cdev = (struct capidev *)file->private_data;
687 struct sk_buff *skb;
688 size_t copied;
690 if (!cdev->ap.applid)
691 return -ENODEV;
693 if ((skb = skb_dequeue(&cdev->recvqueue)) == NULL) {
695 if (file->f_flags & O_NONBLOCK)
696 return -EAGAIN;
698 for (;;) {
699 interruptible_sleep_on(&cdev->recvwait);
700 if ((skb = skb_dequeue(&cdev->recvqueue)) != NULL)
701 break;
702 if (signal_pending(current))
703 break;
705 if (skb == NULL)
706 return -ERESTARTNOHAND;
708 if (skb->len > count) {
709 skb_queue_head(&cdev->recvqueue, skb);
710 return -EMSGSIZE;
712 if (copy_to_user(buf, skb->data, skb->len)) {
713 skb_queue_head(&cdev->recvqueue, skb);
714 return -EFAULT;
716 copied = skb->len;
718 kfree_skb(skb);
720 return copied;
723 static ssize_t
724 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
726 struct capidev *cdev = (struct capidev *)file->private_data;
727 struct sk_buff *skb;
728 u16 mlen;
730 if (!cdev->ap.applid)
731 return -ENODEV;
733 skb = alloc_skb(count, GFP_USER);
734 if (!skb)
735 return -ENOMEM;
737 if (copy_from_user(skb_put(skb, count), buf, count)) {
738 kfree_skb(skb);
739 return -EFAULT;
741 mlen = CAPIMSG_LEN(skb->data);
742 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
743 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
744 kfree_skb(skb);
745 return -EINVAL;
747 } else {
748 if (mlen != count) {
749 kfree_skb(skb);
750 return -EINVAL;
753 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
755 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
756 mutex_lock(&cdev->ncci_list_mtx);
757 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
758 mutex_unlock(&cdev->ncci_list_mtx);
761 cdev->errcode = capi20_put_message(&cdev->ap, skb);
763 if (cdev->errcode) {
764 kfree_skb(skb);
765 return -EIO;
767 return count;
770 static unsigned int
771 capi_poll(struct file *file, poll_table * wait)
773 struct capidev *cdev = (struct capidev *)file->private_data;
774 unsigned int mask = 0;
776 if (!cdev->ap.applid)
777 return POLLERR;
779 poll_wait(file, &(cdev->recvwait), wait);
780 mask = POLLOUT | POLLWRNORM;
781 if (!skb_queue_empty(&cdev->recvqueue))
782 mask |= POLLIN | POLLRDNORM;
783 return mask;
786 static int
787 capi_ioctl(struct inode *inode, struct file *file,
788 unsigned int cmd, unsigned long arg)
790 struct capidev *cdev = file->private_data;
791 struct capi20_appl *ap = &cdev->ap;
792 capi_ioctl_struct data;
793 int retval = -EINVAL;
794 void __user *argp = (void __user *)arg;
796 switch (cmd) {
797 case CAPI_REGISTER:
799 if (ap->applid)
800 return -EEXIST;
802 if (copy_from_user(&cdev->ap.rparam, argp,
803 sizeof(struct capi_register_params)))
804 return -EFAULT;
806 cdev->ap.private = cdev;
807 cdev->ap.recv_message = capi_recv_message;
808 cdev->errcode = capi20_register(ap);
809 if (cdev->errcode) {
810 ap->applid = 0;
811 return -EIO;
814 return (int)ap->applid;
816 case CAPI_GET_VERSION:
818 if (copy_from_user(&data.contr, argp,
819 sizeof(data.contr)))
820 return -EFAULT;
821 cdev->errcode = capi20_get_version(data.contr, &data.version);
822 if (cdev->errcode)
823 return -EIO;
824 if (copy_to_user(argp, &data.version,
825 sizeof(data.version)))
826 return -EFAULT;
828 return 0;
830 case CAPI_GET_SERIAL:
832 if (copy_from_user(&data.contr, argp,
833 sizeof(data.contr)))
834 return -EFAULT;
835 cdev->errcode = capi20_get_serial (data.contr, data.serial);
836 if (cdev->errcode)
837 return -EIO;
838 if (copy_to_user(argp, data.serial,
839 sizeof(data.serial)))
840 return -EFAULT;
842 return 0;
843 case CAPI_GET_PROFILE:
845 if (copy_from_user(&data.contr, argp,
846 sizeof(data.contr)))
847 return -EFAULT;
849 if (data.contr == 0) {
850 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
851 if (cdev->errcode)
852 return -EIO;
854 retval = copy_to_user(argp,
855 &data.profile.ncontroller,
856 sizeof(data.profile.ncontroller));
858 } else {
859 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
860 if (cdev->errcode)
861 return -EIO;
863 retval = copy_to_user(argp, &data.profile,
864 sizeof(data.profile));
866 if (retval)
867 return -EFAULT;
869 return 0;
871 case CAPI_GET_MANUFACTURER:
873 if (copy_from_user(&data.contr, argp,
874 sizeof(data.contr)))
875 return -EFAULT;
876 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
877 if (cdev->errcode)
878 return -EIO;
880 if (copy_to_user(argp, data.manufacturer,
881 sizeof(data.manufacturer)))
882 return -EFAULT;
885 return 0;
886 case CAPI_GET_ERRCODE:
887 data.errcode = cdev->errcode;
888 cdev->errcode = CAPI_NOERROR;
889 if (arg) {
890 if (copy_to_user(argp, &data.errcode,
891 sizeof(data.errcode)))
892 return -EFAULT;
894 return data.errcode;
896 case CAPI_INSTALLED:
897 if (capi20_isinstalled() == CAPI_NOERROR)
898 return 0;
899 return -ENXIO;
901 case CAPI_MANUFACTURER_CMD:
903 struct capi_manufacturer_cmd mcmd;
904 if (!capable(CAP_SYS_ADMIN))
905 return -EPERM;
906 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
907 return -EFAULT;
908 return capi20_manufacturer(mcmd.cmd, mcmd.data);
910 return 0;
912 case CAPI_SET_FLAGS:
913 case CAPI_CLR_FLAGS:
915 unsigned userflags;
916 if (copy_from_user(&userflags, argp,
917 sizeof(userflags)))
918 return -EFAULT;
919 if (cmd == CAPI_SET_FLAGS)
920 cdev->userflags |= userflags;
921 else
922 cdev->userflags &= ~userflags;
924 return 0;
926 case CAPI_GET_FLAGS:
927 if (copy_to_user(argp, &cdev->userflags,
928 sizeof(cdev->userflags)))
929 return -EFAULT;
930 return 0;
932 case CAPI_NCCI_OPENCOUNT:
934 struct capincci *nccip;
935 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
936 struct capiminor *mp;
937 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
938 unsigned ncci;
939 int count = 0;
940 if (copy_from_user(&ncci, argp, sizeof(ncci)))
941 return -EFAULT;
943 mutex_lock(&cdev->ncci_list_mtx);
944 if ((nccip = capincci_find(cdev, (u32) ncci)) == NULL) {
945 mutex_unlock(&cdev->ncci_list_mtx);
946 return 0;
948 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
949 if ((mp = nccip->minorp) != NULL) {
950 count += atomic_read(&mp->ttyopencount);
952 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
953 mutex_unlock(&cdev->ncci_list_mtx);
954 return count;
956 return 0;
958 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
959 case CAPI_NCCI_GETUNIT:
961 struct capincci *nccip;
962 struct capiminor *mp;
963 unsigned ncci;
964 int unit = 0;
965 if (copy_from_user(&ncci, argp,
966 sizeof(ncci)))
967 return -EFAULT;
968 mutex_lock(&cdev->ncci_list_mtx);
969 nccip = capincci_find(cdev, (u32) ncci);
970 if (!nccip || (mp = nccip->minorp) == NULL) {
971 mutex_unlock(&cdev->ncci_list_mtx);
972 return -ESRCH;
974 unit = mp->minor;
975 mutex_unlock(&cdev->ncci_list_mtx);
976 return unit;
978 return 0;
979 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
981 return -EINVAL;
984 static int
985 capi_open(struct inode *inode, struct file *file)
987 int ret;
989 lock_kernel();
990 if (file->private_data)
991 ret = -EEXIST;
992 else if ((file->private_data = capidev_alloc()) == NULL)
993 ret = -ENOMEM;
994 else
995 ret = nonseekable_open(inode, file);
996 unlock_kernel();
997 return ret;
1000 static int
1001 capi_release(struct inode *inode, struct file *file)
1003 struct capidev *cdev = (struct capidev *)file->private_data;
1005 capidev_free(cdev);
1006 file->private_data = NULL;
1008 return 0;
1011 static const struct file_operations capi_fops =
1013 .owner = THIS_MODULE,
1014 .llseek = no_llseek,
1015 .read = capi_read,
1016 .write = capi_write,
1017 .poll = capi_poll,
1018 .ioctl = capi_ioctl,
1019 .open = capi_open,
1020 .release = capi_release,
1023 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1024 /* -------- tty_operations for capincci ----------------------------- */
1026 static int capinc_tty_open(struct tty_struct * tty, struct file * file)
1028 struct capiminor *mp;
1029 unsigned long flags;
1031 if ((mp = capiminor_find(iminor(file->f_path.dentry->d_inode))) == NULL)
1032 return -ENXIO;
1033 if (mp->nccip == NULL)
1034 return -ENXIO;
1036 tty->driver_data = (void *)mp;
1038 spin_lock_irqsave(&workaround_lock, flags);
1039 if (atomic_read(&mp->ttyopencount) == 0)
1040 mp->tty = tty;
1041 atomic_inc(&mp->ttyopencount);
1042 #ifdef _DEBUG_REFCOUNT
1043 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1044 #endif
1045 handle_minor_recv(mp);
1046 spin_unlock_irqrestore(&workaround_lock, flags);
1047 return 0;
1050 static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1052 struct capiminor *mp;
1054 mp = (struct capiminor *)tty->driver_data;
1055 if (mp) {
1056 if (atomic_dec_and_test(&mp->ttyopencount)) {
1057 #ifdef _DEBUG_REFCOUNT
1058 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1059 #endif
1060 tty->driver_data = NULL;
1061 mp->tty = NULL;
1063 #ifdef _DEBUG_REFCOUNT
1064 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1065 #endif
1066 if (mp->nccip == NULL)
1067 capiminor_free(mp);
1070 #ifdef _DEBUG_REFCOUNT
1071 printk(KERN_DEBUG "capinc_tty_close\n");
1072 #endif
1075 static int capinc_tty_write(struct tty_struct * tty,
1076 const unsigned char *buf, int count)
1078 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1079 struct sk_buff *skb;
1080 unsigned long flags;
1082 #ifdef _DEBUG_TTYFUNCS
1083 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1084 #endif
1086 if (!mp || !mp->nccip) {
1087 #ifdef _DEBUG_TTYFUNCS
1088 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1089 #endif
1090 return 0;
1093 spin_lock_irqsave(&workaround_lock, flags);
1094 skb = mp->ttyskb;
1095 if (skb) {
1096 mp->ttyskb = NULL;
1097 skb_queue_tail(&mp->outqueue, skb);
1098 mp->outbytes += skb->len;
1101 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1102 if (!skb) {
1103 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1104 spin_unlock_irqrestore(&workaround_lock, flags);
1105 return -ENOMEM;
1108 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1109 memcpy(skb_put(skb, count), buf, count);
1111 skb_queue_tail(&mp->outqueue, skb);
1112 mp->outbytes += skb->len;
1113 (void)handle_minor_send(mp);
1114 (void)handle_minor_recv(mp);
1115 spin_unlock_irqrestore(&workaround_lock, flags);
1116 return count;
1119 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1121 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1122 struct sk_buff *skb;
1123 unsigned long flags;
1124 int ret = 1;
1126 #ifdef _DEBUG_TTYFUNCS
1127 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1128 #endif
1130 if (!mp || !mp->nccip) {
1131 #ifdef _DEBUG_TTYFUNCS
1132 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1133 #endif
1134 return 0;
1137 spin_lock_irqsave(&workaround_lock, flags);
1138 skb = mp->ttyskb;
1139 if (skb) {
1140 if (skb_tailroom(skb) > 0) {
1141 *(skb_put(skb, 1)) = ch;
1142 spin_unlock_irqrestore(&workaround_lock, flags);
1143 return 1;
1145 mp->ttyskb = NULL;
1146 skb_queue_tail(&mp->outqueue, skb);
1147 mp->outbytes += skb->len;
1148 (void)handle_minor_send(mp);
1150 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1151 if (skb) {
1152 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1153 *(skb_put(skb, 1)) = ch;
1154 mp->ttyskb = skb;
1155 } else {
1156 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1157 ret = 0;
1159 spin_unlock_irqrestore(&workaround_lock, flags);
1160 return ret;
1163 static void capinc_tty_flush_chars(struct tty_struct *tty)
1165 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1166 struct sk_buff *skb;
1167 unsigned long flags;
1169 #ifdef _DEBUG_TTYFUNCS
1170 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1171 #endif
1173 if (!mp || !mp->nccip) {
1174 #ifdef _DEBUG_TTYFUNCS
1175 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1176 #endif
1177 return;
1180 spin_lock_irqsave(&workaround_lock, flags);
1181 skb = mp->ttyskb;
1182 if (skb) {
1183 mp->ttyskb = NULL;
1184 skb_queue_tail(&mp->outqueue, skb);
1185 mp->outbytes += skb->len;
1186 (void)handle_minor_send(mp);
1188 (void)handle_minor_recv(mp);
1189 spin_unlock_irqrestore(&workaround_lock, flags);
1192 static int capinc_tty_write_room(struct tty_struct *tty)
1194 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1195 int room;
1196 if (!mp || !mp->nccip) {
1197 #ifdef _DEBUG_TTYFUNCS
1198 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1199 #endif
1200 return 0;
1202 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1203 room *= CAPI_MAX_BLKSIZE;
1204 #ifdef _DEBUG_TTYFUNCS
1205 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1206 #endif
1207 return room;
1210 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1212 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1213 if (!mp || !mp->nccip) {
1214 #ifdef _DEBUG_TTYFUNCS
1215 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1216 #endif
1217 return 0;
1219 #ifdef _DEBUG_TTYFUNCS
1220 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1221 mp->outbytes, mp->nack,
1222 skb_queue_len(&mp->outqueue),
1223 skb_queue_len(&mp->inqueue));
1224 #endif
1225 return mp->outbytes;
1228 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1229 unsigned int cmd, unsigned long arg)
1231 int error = 0;
1232 switch (cmd) {
1233 default:
1234 error = n_tty_ioctl_helper(tty, file, cmd, arg);
1235 break;
1237 return error;
1240 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1242 #ifdef _DEBUG_TTYFUNCS
1243 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1244 #endif
1247 static void capinc_tty_throttle(struct tty_struct * tty)
1249 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1250 #ifdef _DEBUG_TTYFUNCS
1251 printk(KERN_DEBUG "capinc_tty_throttle\n");
1252 #endif
1253 if (mp)
1254 mp->ttyinstop = 1;
1257 static void capinc_tty_unthrottle(struct tty_struct * tty)
1259 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1260 unsigned long flags;
1261 #ifdef _DEBUG_TTYFUNCS
1262 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1263 #endif
1264 if (mp) {
1265 spin_lock_irqsave(&workaround_lock, flags);
1266 mp->ttyinstop = 0;
1267 handle_minor_recv(mp);
1268 spin_unlock_irqrestore(&workaround_lock, flags);
1272 static void capinc_tty_stop(struct tty_struct *tty)
1274 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1275 #ifdef _DEBUG_TTYFUNCS
1276 printk(KERN_DEBUG "capinc_tty_stop\n");
1277 #endif
1278 if (mp) {
1279 mp->ttyoutstop = 1;
1283 static void capinc_tty_start(struct tty_struct *tty)
1285 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1286 unsigned long flags;
1287 #ifdef _DEBUG_TTYFUNCS
1288 printk(KERN_DEBUG "capinc_tty_start\n");
1289 #endif
1290 if (mp) {
1291 spin_lock_irqsave(&workaround_lock, flags);
1292 mp->ttyoutstop = 0;
1293 (void)handle_minor_send(mp);
1294 spin_unlock_irqrestore(&workaround_lock, flags);
1298 static void capinc_tty_hangup(struct tty_struct *tty)
1300 #ifdef _DEBUG_TTYFUNCS
1301 printk(KERN_DEBUG "capinc_tty_hangup\n");
1302 #endif
1305 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1307 #ifdef _DEBUG_TTYFUNCS
1308 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1309 #endif
1310 return 0;
1313 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1315 #ifdef _DEBUG_TTYFUNCS
1316 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1317 #endif
1320 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1322 #ifdef _DEBUG_TTYFUNCS
1323 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1324 #endif
1327 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1329 #ifdef _DEBUG_TTYFUNCS
1330 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1331 #endif
1334 static int capinc_tty_read_proc(char *page, char **start, off_t off,
1335 int count, int *eof, void *data)
1337 return 0;
1340 static struct tty_driver *capinc_tty_driver;
1342 static const struct tty_operations capinc_ops = {
1343 .open = capinc_tty_open,
1344 .close = capinc_tty_close,
1345 .write = capinc_tty_write,
1346 .put_char = capinc_tty_put_char,
1347 .flush_chars = capinc_tty_flush_chars,
1348 .write_room = capinc_tty_write_room,
1349 .chars_in_buffer = capinc_tty_chars_in_buffer,
1350 .ioctl = capinc_tty_ioctl,
1351 .set_termios = capinc_tty_set_termios,
1352 .throttle = capinc_tty_throttle,
1353 .unthrottle = capinc_tty_unthrottle,
1354 .stop = capinc_tty_stop,
1355 .start = capinc_tty_start,
1356 .hangup = capinc_tty_hangup,
1357 .break_ctl = capinc_tty_break_ctl,
1358 .flush_buffer = capinc_tty_flush_buffer,
1359 .set_ldisc = capinc_tty_set_ldisc,
1360 .send_xchar = capinc_tty_send_xchar,
1361 .read_proc = capinc_tty_read_proc,
1364 static int capinc_tty_init(void)
1366 struct tty_driver *drv;
1368 if (capi_ttyminors > CAPINC_MAX_PORTS)
1369 capi_ttyminors = CAPINC_MAX_PORTS;
1370 if (capi_ttyminors <= 0)
1371 capi_ttyminors = CAPINC_NR_PORTS;
1373 drv = alloc_tty_driver(capi_ttyminors);
1374 if (!drv)
1375 return -ENOMEM;
1377 drv->owner = THIS_MODULE;
1378 drv->driver_name = "capi_nc";
1379 drv->name = "capi";
1380 drv->major = capi_ttymajor;
1381 drv->minor_start = 0;
1382 drv->type = TTY_DRIVER_TYPE_SERIAL;
1383 drv->subtype = SERIAL_TYPE_NORMAL;
1384 drv->init_termios = tty_std_termios;
1385 drv->init_termios.c_iflag = ICRNL;
1386 drv->init_termios.c_oflag = OPOST | ONLCR;
1387 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1388 drv->init_termios.c_lflag = 0;
1389 drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS;
1390 tty_set_operations(drv, &capinc_ops);
1391 if (tty_register_driver(drv)) {
1392 put_tty_driver(drv);
1393 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1394 return -1;
1396 capinc_tty_driver = drv;
1397 return 0;
1400 static void capinc_tty_exit(void)
1402 struct tty_driver *drv = capinc_tty_driver;
1403 int retval;
1404 if ((retval = tty_unregister_driver(drv)))
1405 printk(KERN_ERR "capi: failed to unregister capi_nc driver (%d)\n", retval);
1406 put_tty_driver(drv);
1409 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1411 /* -------- /proc functions ----------------------------------------- */
1414 * /proc/capi/capi20:
1415 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1417 static int proc_capidev_read_proc(char *page, char **start, off_t off,
1418 int count, int *eof, void *data)
1420 struct capidev *cdev;
1421 struct list_head *l;
1422 int len = 0;
1424 read_lock(&capidev_list_lock);
1425 list_for_each(l, &capidev_list) {
1426 cdev = list_entry(l, struct capidev, list);
1427 len += sprintf(page+len, "0 %d %lu %lu %lu %lu\n",
1428 cdev->ap.applid,
1429 cdev->ap.nrecvctlpkt,
1430 cdev->ap.nrecvdatapkt,
1431 cdev->ap.nsentctlpkt,
1432 cdev->ap.nsentdatapkt);
1433 if (len <= off) {
1434 off -= len;
1435 len = 0;
1436 } else {
1437 if (len-off > count)
1438 goto endloop;
1442 endloop:
1443 read_unlock(&capidev_list_lock);
1444 if (len < count)
1445 *eof = 1;
1446 if (len > count) len = count;
1447 if (len < 0) len = 0;
1448 return len;
1452 * /proc/capi/capi20ncci:
1453 * applid ncci
1455 static int proc_capincci_read_proc(char *page, char **start, off_t off,
1456 int count, int *eof, void *data)
1458 struct capidev *cdev;
1459 struct capincci *np;
1460 struct list_head *l;
1461 int len = 0;
1463 read_lock(&capidev_list_lock);
1464 list_for_each(l, &capidev_list) {
1465 cdev = list_entry(l, struct capidev, list);
1466 for (np=cdev->nccis; np; np = np->next) {
1467 len += sprintf(page+len, "%d 0x%x\n",
1468 cdev->ap.applid,
1469 np->ncci);
1470 if (len <= off) {
1471 off -= len;
1472 len = 0;
1473 } else {
1474 if (len-off > count)
1475 goto endloop;
1479 endloop:
1480 read_unlock(&capidev_list_lock);
1481 *start = page+off;
1482 if (len < count)
1483 *eof = 1;
1484 if (len>count) len = count;
1485 if (len<0) len = 0;
1486 return len;
1489 static struct procfsentries {
1490 char *name;
1491 mode_t mode;
1492 int (*read_proc)(char *page, char **start, off_t off,
1493 int count, int *eof, void *data);
1494 struct proc_dir_entry *procent;
1495 } procfsentries[] = {
1496 /* { "capi", S_IFDIR, 0 }, */
1497 { "capi/capi20", 0 , proc_capidev_read_proc },
1498 { "capi/capi20ncci", 0 , proc_capincci_read_proc },
1501 static void __init proc_init(void)
1503 int nelem = ARRAY_SIZE(procfsentries);
1504 int i;
1506 for (i=0; i < nelem; i++) {
1507 struct procfsentries *p = procfsentries + i;
1508 p->procent = create_proc_entry(p->name, p->mode, NULL);
1509 if (p->procent) p->procent->read_proc = p->read_proc;
1513 static void __exit proc_exit(void)
1515 int nelem = ARRAY_SIZE(procfsentries);
1516 int i;
1518 for (i=nelem-1; i >= 0; i--) {
1519 struct procfsentries *p = procfsentries + i;
1520 if (p->procent) {
1521 remove_proc_entry(p->name, NULL);
1522 p->procent = NULL;
1527 /* -------- init function and module interface ---------------------- */
1530 static char rev[32];
1532 static int __init capi_init(void)
1534 char *p;
1535 char *compileinfo;
1536 int major_ret;
1538 if ((p = strchr(revision, ':')) != NULL && p[1]) {
1539 strlcpy(rev, p + 2, sizeof(rev));
1540 if ((p = strchr(rev, '$')) != NULL && p > rev)
1541 *(p-1) = 0;
1542 } else
1543 strcpy(rev, "1.0");
1545 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1546 if (major_ret < 0) {
1547 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1548 return major_ret;
1550 capi_class = class_create(THIS_MODULE, "capi");
1551 if (IS_ERR(capi_class)) {
1552 unregister_chrdev(capi_major, "capi20");
1553 return PTR_ERR(capi_class);
1556 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1558 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1559 if (capinc_tty_init() < 0) {
1560 device_destroy(capi_class, MKDEV(capi_major, 0));
1561 class_destroy(capi_class);
1562 unregister_chrdev(capi_major, "capi20");
1563 return -ENOMEM;
1565 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1567 proc_init();
1569 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1570 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1571 compileinfo = " (middleware+capifs)";
1572 #else
1573 compileinfo = " (no capifs)";
1574 #endif
1575 #else
1576 compileinfo = " (no middleware)";
1577 #endif
1578 printk(KERN_NOTICE "capi20: Rev %s: started up with major %d%s\n",
1579 rev, capi_major, compileinfo);
1581 return 0;
1584 static void __exit capi_exit(void)
1586 proc_exit();
1588 device_destroy(capi_class, MKDEV(capi_major, 0));
1589 class_destroy(capi_class);
1590 unregister_chrdev(capi_major, "capi20");
1592 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1593 capinc_tty_exit();
1594 #endif
1595 printk(KERN_NOTICE "capi: Rev %s: unloaded\n", rev);
1598 module_init(capi_init);
1599 module_exit(capi_exit);