capi: Perform scheduled capifs removal
[firefly-linux-kernel-4.4.55.git] / drivers / isdn / capi / capi.c
1 /* $Id: capi.c,v 1.1.2.7 2004/04/28 09:48:59 armin Exp $
2  *
3  * CAPI 2.0 Interface for Linux
4  *
5  * Copyright 1996 by Carsten Paeth <calle@calle.de>
6  *
7  * This software may be used and distributed according to the terms
8  * of the GNU General Public License, incorporated herein by reference.
9  *
10  */
11
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/if_ppp.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>
40
41 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
42 MODULE_AUTHOR("Carsten Paeth");
43 MODULE_LICENSE("GPL");
44
45 #undef _DEBUG_TTYFUNCS          /* call to tty_driver */
46 #undef _DEBUG_DATAFLOW          /* data flow */
47
48 /* -------- driver information -------------------------------------- */
49
50 static DEFINE_MUTEX(capi_mutex);
51 static struct class *capi_class;
52 static int capi_major = 68;             /* allocated */
53
54 module_param_named(major, capi_major, uint, 0);
55
56 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
57 #define CAPINC_NR_PORTS         32
58 #define CAPINC_MAX_PORTS        256
59
60 static int capi_ttyminors = CAPINC_NR_PORTS;
61
62 module_param_named(ttyminors, capi_ttyminors, uint, 0);
63 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
64
65 /* -------- defines ------------------------------------------------- */
66
67 #define CAPINC_MAX_RECVQUEUE    10
68 #define CAPINC_MAX_SENDQUEUE    10
69 #define CAPI_MAX_BLKSIZE        2048
70
71 /* -------- data structures ----------------------------------------- */
72
73 struct capidev;
74 struct capincci;
75 struct capiminor;
76
77 struct ackqueue_entry {
78         struct list_head        list;
79         u16                     datahandle;
80 };
81
82 struct capiminor {
83         struct kref kref;
84
85         unsigned int      minor;
86
87         struct capi20_appl      *ap;
88         u32                     ncci;
89         atomic_t                datahandle;
90         atomic_t                msgid;
91
92         struct tty_port port;
93         int                ttyinstop;
94         int                ttyoutstop;
95
96         struct sk_buff_head     inqueue;
97
98         struct sk_buff_head     outqueue;
99         int                     outbytes;
100         struct sk_buff          *outskb;
101         spinlock_t              outlock;
102
103         /* transmit path */
104         struct list_head ackqueue;
105         int nack;
106         spinlock_t ackqlock;
107 };
108
109 struct capincci {
110         struct list_head list;
111         u32              ncci;
112         struct capidev  *cdev;
113 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
114         struct capiminor *minorp;
115 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
116 };
117
118 struct capidev {
119         struct list_head list;
120         struct capi20_appl ap;
121         u16             errcode;
122         unsigned        userflags;
123
124         struct sk_buff_head recvqueue;
125         wait_queue_head_t recvwait;
126
127         struct list_head nccis;
128
129         struct mutex lock;
130 };
131
132 /* -------- global variables ---------------------------------------- */
133
134 static DEFINE_MUTEX(capidev_list_lock);
135 static LIST_HEAD(capidev_list);
136
137 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
138
139 static DEFINE_SPINLOCK(capiminors_lock);
140 static struct capiminor **capiminors;
141
142 static struct tty_driver *capinc_tty_driver;
143
144 /* -------- datahandles --------------------------------------------- */
145
146 static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
147 {
148         struct ackqueue_entry *n;
149
150         n = kmalloc(sizeof(*n), GFP_ATOMIC);
151         if (unlikely(!n)) {
152                 printk(KERN_ERR "capi: alloc datahandle failed\n");
153                 return -1;
154         }
155         n->datahandle = datahandle;
156         INIT_LIST_HEAD(&n->list);
157         spin_lock_bh(&mp->ackqlock);
158         list_add_tail(&n->list, &mp->ackqueue);
159         mp->nack++;
160         spin_unlock_bh(&mp->ackqlock);
161         return 0;
162 }
163
164 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
165 {
166         struct ackqueue_entry *p, *tmp;
167
168         spin_lock_bh(&mp->ackqlock);
169         list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
170                 if (p->datahandle == datahandle) {
171                         list_del(&p->list);
172                         mp->nack--;
173                         spin_unlock_bh(&mp->ackqlock);
174                         kfree(p);
175                         return 0;
176                 }
177         }
178         spin_unlock_bh(&mp->ackqlock);
179         return -1;
180 }
181
182 static void capiminor_del_all_ack(struct capiminor *mp)
183 {
184         struct ackqueue_entry *p, *tmp;
185
186         list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
187                 list_del(&p->list);
188                 kfree(p);
189                 mp->nack--;
190         }
191 }
192
193
194 /* -------- struct capiminor ---------------------------------------- */
195
196 static const struct tty_port_operations capiminor_port_ops; /* we have none */
197
198 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
199 {
200         struct capiminor *mp;
201         struct device *dev;
202         unsigned int minor;
203
204         mp = kzalloc(sizeof(*mp), GFP_KERNEL);
205         if (!mp) {
206                 printk(KERN_ERR "capi: can't alloc capiminor\n");
207                 return NULL;
208         }
209
210         kref_init(&mp->kref);
211
212         mp->ap = ap;
213         mp->ncci = ncci;
214         INIT_LIST_HEAD(&mp->ackqueue);
215         spin_lock_init(&mp->ackqlock);
216
217         skb_queue_head_init(&mp->inqueue);
218         skb_queue_head_init(&mp->outqueue);
219         spin_lock_init(&mp->outlock);
220
221         tty_port_init(&mp->port);
222         mp->port.ops = &capiminor_port_ops;
223
224         /* Allocate the least unused minor number. */
225         spin_lock(&capiminors_lock);
226         for (minor = 0; minor < capi_ttyminors; minor++)
227                 if (!capiminors[minor]) {
228                         capiminors[minor] = mp;
229                         break;
230                 }
231         spin_unlock(&capiminors_lock);
232
233         if (minor == capi_ttyminors) {
234                 printk(KERN_NOTICE "capi: out of minors\n");
235                 goto err_out1;
236         }
237
238         mp->minor = minor;
239
240         dev = tty_register_device(capinc_tty_driver, minor, NULL);
241         if (IS_ERR(dev))
242                 goto err_out2;
243
244         return mp;
245
246 err_out2:
247         spin_lock(&capiminors_lock);
248         capiminors[minor] = NULL;
249         spin_unlock(&capiminors_lock);
250
251 err_out1:
252         kfree(mp);
253         return NULL;
254 }
255
256 static void capiminor_destroy(struct kref *kref)
257 {
258         struct capiminor *mp = container_of(kref, struct capiminor, kref);
259
260         kfree_skb(mp->outskb);
261         skb_queue_purge(&mp->inqueue);
262         skb_queue_purge(&mp->outqueue);
263         capiminor_del_all_ack(mp);
264         kfree(mp);
265 }
266
267 static struct capiminor *capiminor_get(unsigned int minor)
268 {
269         struct capiminor *mp;
270
271         spin_lock(&capiminors_lock);
272         mp = capiminors[minor];
273         if (mp)
274                 kref_get(&mp->kref);
275         spin_unlock(&capiminors_lock);
276
277         return mp;
278 }
279
280 static inline void capiminor_put(struct capiminor *mp)
281 {
282         kref_put(&mp->kref, capiminor_destroy);
283 }
284
285 static void capiminor_free(struct capiminor *mp)
286 {
287         tty_unregister_device(capinc_tty_driver, mp->minor);
288
289         spin_lock(&capiminors_lock);
290         capiminors[mp->minor] = NULL;
291         spin_unlock(&capiminors_lock);
292
293         capiminor_put(mp);
294 }
295
296 /* -------- struct capincci ----------------------------------------- */
297
298 static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
299 {
300         if (cdev->userflags & CAPIFLAG_HIGHJACKING)
301                 np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
302 }
303
304 static void capincci_free_minor(struct capincci *np)
305 {
306         struct capiminor *mp = np->minorp;
307         struct tty_struct *tty;
308
309         if (mp) {
310                 tty = tty_port_tty_get(&mp->port);
311                 if (tty) {
312                         tty_vhangup(tty);
313                         tty_kref_put(tty);
314                 }
315
316                 capiminor_free(mp);
317         }
318 }
319
320 static inline unsigned int capincci_minor_opencount(struct capincci *np)
321 {
322         struct capiminor *mp = np->minorp;
323         unsigned int count = 0;
324         struct tty_struct *tty;
325
326         if (mp) {
327                 tty = tty_port_tty_get(&mp->port);
328                 if (tty) {
329                         count = tty->count;
330                         tty_kref_put(tty);
331                 }
332         }
333         return count;
334 }
335
336 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
337
338 static inline void
339 capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
340 static inline void capincci_free_minor(struct capincci *np) { }
341
342 static inline unsigned int capincci_minor_opencount(struct capincci *np)
343 {
344         return 0;
345 }
346
347 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
348
349 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
350 {
351         struct capincci *np;
352
353         np = kzalloc(sizeof(*np), GFP_KERNEL);
354         if (!np)
355                 return NULL;
356         np->ncci = ncci;
357         np->cdev = cdev;
358
359         capincci_alloc_minor(cdev, np);
360
361         list_add_tail(&np->list, &cdev->nccis);
362
363         return np;
364 }
365
366 static void capincci_free(struct capidev *cdev, u32 ncci)
367 {
368         struct capincci *np, *tmp;
369
370         list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
371                 if (ncci == 0xffffffff || np->ncci == ncci) {
372                         capincci_free_minor(np);
373                         list_del(&np->list);
374                         kfree(np);
375                 }
376 }
377
378 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
379 {
380         struct capincci *np;
381
382         list_for_each_entry(np, &cdev->nccis, list)
383                 if (np->ncci == ncci)
384                         return np;
385         return NULL;
386 }
387
388 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
389 /* -------- handle data queue --------------------------------------- */
390
391 static struct sk_buff *
392 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
393 {
394         struct sk_buff *nskb;
395         nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_KERNEL);
396         if (nskb) {
397                 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
398                 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
399                 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
400                 capimsg_setu16(s, 2, mp->ap->applid);
401                 capimsg_setu8 (s, 4, CAPI_DATA_B3);
402                 capimsg_setu8 (s, 5, CAPI_RESP);
403                 capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
404                 capimsg_setu32(s, 8, mp->ncci);
405                 capimsg_setu16(s, 12, datahandle);
406         }
407         return nskb;
408 }
409
410 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
411 {
412         unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
413         struct tty_struct *tty;
414         struct sk_buff *nskb;
415         u16 errcode, datahandle;
416         struct tty_ldisc *ld;
417         int ret = -1;
418
419         tty = tty_port_tty_get(&mp->port);
420         if (!tty) {
421 #ifdef _DEBUG_DATAFLOW
422                 printk(KERN_DEBUG "capi: currently no receiver\n");
423 #endif
424                 return -1;
425         }
426         
427         ld = tty_ldisc_ref(tty);
428         if (!ld) {
429                 /* fatal error, do not requeue */
430                 ret = 0;
431                 kfree_skb(skb);
432                 goto deref_tty;
433         }
434
435         if (ld->ops->receive_buf == NULL) {
436 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
437                 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
438 #endif
439                 /* fatal error, do not requeue */
440                 goto free_skb;
441         }
442         if (mp->ttyinstop) {
443 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
444                 printk(KERN_DEBUG "capi: recv tty throttled\n");
445 #endif
446                 goto deref_ldisc;
447         }
448
449         if (tty->receive_room < datalen) {
450 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
451                 printk(KERN_DEBUG "capi: no room in tty\n");
452 #endif
453                 goto deref_ldisc;
454         }
455
456         nskb = gen_data_b3_resp_for(mp, skb);
457         if (!nskb) {
458                 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
459                 goto deref_ldisc;
460         }
461
462         datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
463
464         errcode = capi20_put_message(mp->ap, nskb);
465
466         if (errcode == CAPI_NOERROR) {
467                 skb_pull(skb, CAPIMSG_LEN(skb->data));
468 #ifdef _DEBUG_DATAFLOW
469                 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
470                                         datahandle, skb->len);
471 #endif
472                 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
473         } else {
474                 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
475                                 errcode);
476                 kfree_skb(nskb);
477
478                 if (errcode == CAPI_SENDQUEUEFULL)
479                         goto deref_ldisc;
480         }
481
482 free_skb:
483         ret = 0;
484         kfree_skb(skb);
485
486 deref_ldisc:
487         tty_ldisc_deref(ld);
488
489 deref_tty:
490         tty_kref_put(tty);
491         return ret;
492 }
493
494 static void handle_minor_recv(struct capiminor *mp)
495 {
496         struct sk_buff *skb;
497
498         while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
499                 if (handle_recv_skb(mp, skb) < 0) {
500                         skb_queue_head(&mp->inqueue, skb);
501                         return;
502                 }
503 }
504
505 static void handle_minor_send(struct capiminor *mp)
506 {
507         struct tty_struct *tty;
508         struct sk_buff *skb;
509         u16 len;
510         u16 errcode;
511         u16 datahandle;
512
513         tty = tty_port_tty_get(&mp->port);
514         if (!tty)
515                 return;
516
517         if (mp->ttyoutstop) {
518 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
519                 printk(KERN_DEBUG "capi: send: tty stopped\n");
520 #endif
521                 tty_kref_put(tty);
522                 return;
523         }
524
525         while (1) {
526                 spin_lock_bh(&mp->outlock);
527                 skb = __skb_dequeue(&mp->outqueue);
528                 if (!skb) {
529                         spin_unlock_bh(&mp->outlock);
530                         break;
531                 }
532                 len = (u16)skb->len;
533                 mp->outbytes -= len;
534                 spin_unlock_bh(&mp->outlock);
535
536                 datahandle = atomic_inc_return(&mp->datahandle);
537                 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
538                 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
539                 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
540                 capimsg_setu16(skb->data, 2, mp->ap->applid);
541                 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
542                 capimsg_setu8 (skb->data, 5, CAPI_REQ);
543                 capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
544                 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
545                 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
546                 capimsg_setu16(skb->data, 16, len);     /* Data length */
547                 capimsg_setu16(skb->data, 18, datahandle);
548                 capimsg_setu16(skb->data, 20, 0);       /* Flags */
549
550                 if (capiminor_add_ack(mp, datahandle) < 0) {
551                         skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
552
553                         spin_lock_bh(&mp->outlock);
554                         __skb_queue_head(&mp->outqueue, skb);
555                         mp->outbytes += len;
556                         spin_unlock_bh(&mp->outlock);
557
558                         break;
559                 }
560                 errcode = capi20_put_message(mp->ap, skb);
561                 if (errcode == CAPI_NOERROR) {
562 #ifdef _DEBUG_DATAFLOW
563                         printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
564                                                         datahandle, len);
565 #endif
566                         continue;
567                 }
568                 capiminor_del_ack(mp, datahandle);
569
570                 if (errcode == CAPI_SENDQUEUEFULL) {
571                         skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
572
573                         spin_lock_bh(&mp->outlock);
574                         __skb_queue_head(&mp->outqueue, skb);
575                         mp->outbytes += len;
576                         spin_unlock_bh(&mp->outlock);
577
578                         break;
579                 }
580
581                 /* ups, drop packet */
582                 printk(KERN_ERR "capi: put_message = %x\n", errcode);
583                 kfree_skb(skb);
584         }
585         tty_kref_put(tty);
586 }
587
588 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
589 /* -------- function called by lower level -------------------------- */
590
591 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
592 {
593         struct capidev *cdev = ap->private;
594 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
595         struct tty_struct *tty;
596         struct capiminor *mp;
597         u16 datahandle;
598 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
599         struct capincci *np;
600
601         mutex_lock(&cdev->lock);
602
603         if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
604                 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
605                 if ((info & 0xff00) == 0)
606                         capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
607         }
608         if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
609                 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
610
611         if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
612                 skb_queue_tail(&cdev->recvqueue, skb);
613                 wake_up_interruptible(&cdev->recvwait);
614                 goto unlock_out;
615         }
616
617         np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
618         if (!np) {
619                 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
620                 skb_queue_tail(&cdev->recvqueue, skb);
621                 wake_up_interruptible(&cdev->recvwait);
622                 goto unlock_out;
623         }
624
625 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
626         skb_queue_tail(&cdev->recvqueue, skb);
627         wake_up_interruptible(&cdev->recvwait);
628
629 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
630
631         mp = np->minorp;
632         if (!mp) {
633                 skb_queue_tail(&cdev->recvqueue, skb);
634                 wake_up_interruptible(&cdev->recvwait);
635                 goto unlock_out;
636         }
637         if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
638                 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
639 #ifdef _DEBUG_DATAFLOW
640                 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
641                                 datahandle, skb->len-CAPIMSG_LEN(skb->data));
642 #endif
643                 skb_queue_tail(&mp->inqueue, skb);
644
645                 handle_minor_recv(mp);
646
647         } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
648
649                 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
650 #ifdef _DEBUG_DATAFLOW
651                 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
652                                 datahandle,
653                                 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
654 #endif
655                 kfree_skb(skb);
656                 capiminor_del_ack(mp, datahandle);
657                 tty = tty_port_tty_get(&mp->port);
658                 if (tty) {
659                         tty_wakeup(tty);
660                         tty_kref_put(tty);
661                 }
662                 handle_minor_send(mp);
663
664         } else {
665                 /* ups, let capi application handle it :-) */
666                 skb_queue_tail(&cdev->recvqueue, skb);
667                 wake_up_interruptible(&cdev->recvwait);
668         }
669 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
670
671 unlock_out:
672         mutex_unlock(&cdev->lock);
673 }
674
675 /* -------- file_operations for capidev ----------------------------- */
676
677 static ssize_t
678 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
679 {
680         struct capidev *cdev = file->private_data;
681         struct sk_buff *skb;
682         size_t copied;
683         int err;
684
685         if (!cdev->ap.applid)
686                 return -ENODEV;
687
688         skb = skb_dequeue(&cdev->recvqueue);
689         if (!skb) {
690                 if (file->f_flags & O_NONBLOCK)
691                         return -EAGAIN;
692                 err = wait_event_interruptible(cdev->recvwait,
693                                 (skb = skb_dequeue(&cdev->recvqueue)));
694                 if (err)
695                         return err;
696         }
697         if (skb->len > count) {
698                 skb_queue_head(&cdev->recvqueue, skb);
699                 return -EMSGSIZE;
700         }
701         if (copy_to_user(buf, skb->data, skb->len)) {
702                 skb_queue_head(&cdev->recvqueue, skb);
703                 return -EFAULT;
704         }
705         copied = skb->len;
706
707         kfree_skb(skb);
708
709         return copied;
710 }
711
712 static ssize_t
713 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
714 {
715         struct capidev *cdev = file->private_data;
716         struct sk_buff *skb;
717         u16 mlen;
718
719         if (!cdev->ap.applid)
720                 return -ENODEV;
721
722         skb = alloc_skb(count, GFP_USER);
723         if (!skb)
724                 return -ENOMEM;
725
726         if (copy_from_user(skb_put(skb, count), buf, count)) {
727                 kfree_skb(skb);
728                 return -EFAULT;
729         }
730         mlen = CAPIMSG_LEN(skb->data);
731         if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
732                 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
733                         kfree_skb(skb);
734                         return -EINVAL;
735                 }
736         } else {
737                 if (mlen != count) {
738                         kfree_skb(skb);
739                         return -EINVAL;
740                 }
741         }
742         CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
743
744         if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
745                 mutex_lock(&cdev->lock);
746                 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
747                 mutex_unlock(&cdev->lock);
748         }
749
750         cdev->errcode = capi20_put_message(&cdev->ap, skb);
751
752         if (cdev->errcode) {
753                 kfree_skb(skb);
754                 return -EIO;
755         }
756         return count;
757 }
758
759 static unsigned int
760 capi_poll(struct file *file, poll_table * wait)
761 {
762         struct capidev *cdev = file->private_data;
763         unsigned int mask = 0;
764
765         if (!cdev->ap.applid)
766                 return POLLERR;
767
768         poll_wait(file, &(cdev->recvwait), wait);
769         mask = POLLOUT | POLLWRNORM;
770         if (!skb_queue_empty(&cdev->recvqueue))
771                 mask |= POLLIN | POLLRDNORM;
772         return mask;
773 }
774
775 static int
776 capi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
777 {
778         struct capidev *cdev = file->private_data;
779         capi_ioctl_struct data;
780         int retval = -EINVAL;
781         void __user *argp = (void __user *)arg;
782
783         switch (cmd) {
784         case CAPI_REGISTER:
785                 mutex_lock(&cdev->lock);
786
787                 if (cdev->ap.applid) {
788                         retval = -EEXIST;
789                         goto register_out;
790                 }
791                 if (copy_from_user(&cdev->ap.rparam, argp,
792                                    sizeof(struct capi_register_params))) {
793                         retval = -EFAULT;
794                         goto register_out;
795                 }
796                 cdev->ap.private = cdev;
797                 cdev->ap.recv_message = capi_recv_message;
798                 cdev->errcode = capi20_register(&cdev->ap);
799                 retval = (int)cdev->ap.applid;
800                 if (cdev->errcode) {
801                         cdev->ap.applid = 0;
802                         retval = -EIO;
803                 }
804
805 register_out:
806                 mutex_unlock(&cdev->lock);
807                 return retval;
808
809         case CAPI_GET_VERSION:
810                 {
811                         if (copy_from_user(&data.contr, argp,
812                                                 sizeof(data.contr)))
813                                 return -EFAULT;
814                         cdev->errcode = capi20_get_version(data.contr, &data.version);
815                         if (cdev->errcode)
816                                 return -EIO;
817                         if (copy_to_user(argp, &data.version,
818                                          sizeof(data.version)))
819                                 return -EFAULT;
820                 }
821                 return 0;
822
823         case CAPI_GET_SERIAL:
824                 {
825                         if (copy_from_user(&data.contr, argp,
826                                            sizeof(data.contr)))
827                                 return -EFAULT;
828                         cdev->errcode = capi20_get_serial (data.contr, data.serial);
829                         if (cdev->errcode)
830                                 return -EIO;
831                         if (copy_to_user(argp, data.serial,
832                                          sizeof(data.serial)))
833                                 return -EFAULT;
834                 }
835                 return 0;
836         case CAPI_GET_PROFILE:
837                 {
838                         if (copy_from_user(&data.contr, argp,
839                                            sizeof(data.contr)))
840                                 return -EFAULT;
841
842                         if (data.contr == 0) {
843                                 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
844                                 if (cdev->errcode)
845                                         return -EIO;
846
847                                 retval = copy_to_user(argp,
848                                       &data.profile.ncontroller,
849                                        sizeof(data.profile.ncontroller));
850
851                         } else {
852                                 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
853                                 if (cdev->errcode)
854                                         return -EIO;
855
856                                 retval = copy_to_user(argp, &data.profile,
857                                                    sizeof(data.profile));
858                         }
859                         if (retval)
860                                 return -EFAULT;
861                 }
862                 return 0;
863
864         case CAPI_GET_MANUFACTURER:
865                 {
866                         if (copy_from_user(&data.contr, argp,
867                                            sizeof(data.contr)))
868                                 return -EFAULT;
869                         cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
870                         if (cdev->errcode)
871                                 return -EIO;
872
873                         if (copy_to_user(argp, data.manufacturer,
874                                          sizeof(data.manufacturer)))
875                                 return -EFAULT;
876
877                 }
878                 return 0;
879         case CAPI_GET_ERRCODE:
880                 data.errcode = cdev->errcode;
881                 cdev->errcode = CAPI_NOERROR;
882                 if (arg) {
883                         if (copy_to_user(argp, &data.errcode,
884                                          sizeof(data.errcode)))
885                                 return -EFAULT;
886                 }
887                 return data.errcode;
888
889         case CAPI_INSTALLED:
890                 if (capi20_isinstalled() == CAPI_NOERROR)
891                         return 0;
892                 return -ENXIO;
893
894         case CAPI_MANUFACTURER_CMD:
895                 {
896                         struct capi_manufacturer_cmd mcmd;
897                         if (!capable(CAP_SYS_ADMIN))
898                                 return -EPERM;
899                         if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
900                                 return -EFAULT;
901                         return capi20_manufacturer(mcmd.cmd, mcmd.data);
902                 }
903                 return 0;
904
905         case CAPI_SET_FLAGS:
906         case CAPI_CLR_FLAGS: {
907                 unsigned userflags;
908
909                 if (copy_from_user(&userflags, argp, sizeof(userflags)))
910                         return -EFAULT;
911
912                 mutex_lock(&cdev->lock);
913                 if (cmd == CAPI_SET_FLAGS)
914                         cdev->userflags |= userflags;
915                 else
916                         cdev->userflags &= ~userflags;
917                 mutex_unlock(&cdev->lock);
918                 return 0;
919         }
920         case CAPI_GET_FLAGS:
921                 if (copy_to_user(argp, &cdev->userflags,
922                                  sizeof(cdev->userflags)))
923                         return -EFAULT;
924                 return 0;
925
926         case CAPI_NCCI_OPENCOUNT: {
927                 struct capincci *nccip;
928                 unsigned ncci;
929                 int count = 0;
930
931                 if (copy_from_user(&ncci, argp, sizeof(ncci)))
932                         return -EFAULT;
933
934                 mutex_lock(&cdev->lock);
935                 nccip = capincci_find(cdev, (u32)ncci);
936                 if (nccip)
937                         count = capincci_minor_opencount(nccip);
938                 mutex_unlock(&cdev->lock);
939                 return count;
940         }
941
942 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
943         case CAPI_NCCI_GETUNIT: {
944                 struct capincci *nccip;
945                 struct capiminor *mp;
946                 unsigned ncci;
947                 int unit = -ESRCH;
948
949                 if (copy_from_user(&ncci, argp, sizeof(ncci)))
950                         return -EFAULT;
951
952                 mutex_lock(&cdev->lock);
953                 nccip = capincci_find(cdev, (u32)ncci);
954                 if (nccip) {
955                         mp = nccip->minorp;
956                         if (mp)
957                                 unit = mp->minor;
958                 }
959                 mutex_unlock(&cdev->lock);
960                 return unit;
961         }
962 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
963
964         default:
965                 return -EINVAL;
966         }
967 }
968
969 static long
970 capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
971 {
972         int ret;
973
974         mutex_lock(&capi_mutex);
975         ret = capi_ioctl(file, cmd, arg);
976         mutex_unlock(&capi_mutex);
977
978         return ret;
979 }
980
981 static int capi_open(struct inode *inode, struct file *file)
982 {
983         struct capidev *cdev;
984
985         cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
986         if (!cdev)
987                 return -ENOMEM;
988
989         mutex_init(&cdev->lock);
990         skb_queue_head_init(&cdev->recvqueue);
991         init_waitqueue_head(&cdev->recvwait);
992         INIT_LIST_HEAD(&cdev->nccis);
993         file->private_data = cdev;
994
995         mutex_lock(&capidev_list_lock);
996         list_add_tail(&cdev->list, &capidev_list);
997         mutex_unlock(&capidev_list_lock);
998
999         return nonseekable_open(inode, file);
1000 }
1001
1002 static int capi_release(struct inode *inode, struct file *file)
1003 {
1004         struct capidev *cdev = file->private_data;
1005
1006         mutex_lock(&capidev_list_lock);
1007         list_del(&cdev->list);
1008         mutex_unlock(&capidev_list_lock);
1009
1010         if (cdev->ap.applid)
1011                 capi20_release(&cdev->ap);
1012         skb_queue_purge(&cdev->recvqueue);
1013         capincci_free(cdev, 0xffffffff);
1014
1015         kfree(cdev);
1016         return 0;
1017 }
1018
1019 static const struct file_operations capi_fops =
1020 {
1021         .owner          = THIS_MODULE,
1022         .llseek         = no_llseek,
1023         .read           = capi_read,
1024         .write          = capi_write,
1025         .poll           = capi_poll,
1026         .unlocked_ioctl = capi_unlocked_ioctl,
1027         .open           = capi_open,
1028         .release        = capi_release,
1029 };
1030
1031 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1032 /* -------- tty_operations for capincci ----------------------------- */
1033
1034 static int
1035 capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1036 {
1037         int idx = tty->index;
1038         struct capiminor *mp = capiminor_get(idx);
1039         int ret = tty_init_termios(tty);
1040
1041         if (ret == 0) {
1042                 tty_driver_kref_get(driver);
1043                 tty->count++;
1044                 tty->driver_data = mp;
1045                 driver->ttys[idx] = tty;
1046         } else
1047                 capiminor_put(mp);
1048         return ret;
1049 }
1050
1051 static void capinc_tty_cleanup(struct tty_struct *tty)
1052 {
1053         struct capiminor *mp = tty->driver_data;
1054         tty->driver_data = NULL;
1055         capiminor_put(mp);
1056 }
1057
1058 static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
1059 {
1060         struct capiminor *mp = tty->driver_data;
1061         int err;
1062
1063         err = tty_port_open(&mp->port, tty, filp);
1064         if (err)
1065                 return err;
1066
1067         handle_minor_recv(mp);
1068         return 0;
1069 }
1070
1071 static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
1072 {
1073         struct capiminor *mp = tty->driver_data;
1074
1075         tty_port_close(&mp->port, tty, filp);
1076 }
1077
1078 static int capinc_tty_write(struct tty_struct *tty,
1079                             const unsigned char *buf, int count)
1080 {
1081         struct capiminor *mp = tty->driver_data;
1082         struct sk_buff *skb;
1083
1084 #ifdef _DEBUG_TTYFUNCS
1085         printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1086 #endif
1087
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;
1094         }
1095
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;
1101         }
1102
1103         skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1104         memcpy(skb_put(skb, count), buf, count);
1105
1106         __skb_queue_tail(&mp->outqueue, skb);
1107         mp->outbytes += skb->len;
1108         spin_unlock_bh(&mp->outlock);
1109
1110         handle_minor_send(mp);
1111
1112         return count;
1113 }
1114
1115 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1116 {
1117         struct capiminor *mp = tty->driver_data;
1118         bool invoke_send = false;
1119         struct sk_buff *skb;
1120         int ret = 1;
1121
1122 #ifdef _DEBUG_TTYFUNCS
1123         printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1124 #endif
1125
1126         spin_lock_bh(&mp->outlock);
1127         skb = mp->outskb;
1128         if (skb) {
1129                 if (skb_tailroom(skb) > 0) {
1130                         *(skb_put(skb, 1)) = ch;
1131                         goto unlock_out;
1132                 }
1133                 mp->outskb = NULL;
1134                 __skb_queue_tail(&mp->outqueue, skb);
1135                 mp->outbytes += skb->len;
1136                 invoke_send = true;
1137         }
1138
1139         skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1140         if (skb) {
1141                 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1142                 *(skb_put(skb, 1)) = ch;
1143                 mp->outskb = skb;
1144         } else {
1145                 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1146                 ret = 0;
1147         }
1148
1149 unlock_out:
1150         spin_unlock_bh(&mp->outlock);
1151
1152         if (invoke_send)
1153                 handle_minor_send(mp);
1154
1155         return ret;
1156 }
1157
1158 static void capinc_tty_flush_chars(struct tty_struct *tty)
1159 {
1160         struct capiminor *mp = tty->driver_data;
1161         struct sk_buff *skb;
1162
1163 #ifdef _DEBUG_TTYFUNCS
1164         printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1165 #endif
1166
1167         spin_lock_bh(&mp->outlock);
1168         skb = mp->outskb;
1169         if (skb) {
1170                 mp->outskb = NULL;
1171                 __skb_queue_tail(&mp->outqueue, skb);
1172                 mp->outbytes += skb->len;
1173                 spin_unlock_bh(&mp->outlock);
1174
1175                 handle_minor_send(mp);
1176         } else
1177                 spin_unlock_bh(&mp->outlock);
1178
1179         handle_minor_recv(mp);
1180 }
1181
1182 static int capinc_tty_write_room(struct tty_struct *tty)
1183 {
1184         struct capiminor *mp = tty->driver_data;
1185         int room;
1186
1187         room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1188         room *= CAPI_MAX_BLKSIZE;
1189 #ifdef _DEBUG_TTYFUNCS
1190         printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1191 #endif
1192         return room;
1193 }
1194
1195 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1196 {
1197         struct capiminor *mp = tty->driver_data;
1198
1199 #ifdef _DEBUG_TTYFUNCS
1200         printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1201                         mp->outbytes, mp->nack,
1202                         skb_queue_len(&mp->outqueue),
1203                         skb_queue_len(&mp->inqueue));
1204 #endif
1205         return mp->outbytes;
1206 }
1207
1208 static int capinc_tty_ioctl(struct tty_struct *tty,
1209                     unsigned int cmd, unsigned long arg)
1210 {
1211         return -ENOIOCTLCMD;
1212 }
1213
1214 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1215 {
1216 #ifdef _DEBUG_TTYFUNCS
1217         printk(KERN_DEBUG "capinc_tty_set_termios\n");
1218 #endif
1219 }
1220
1221 static void capinc_tty_throttle(struct tty_struct *tty)
1222 {
1223         struct capiminor *mp = tty->driver_data;
1224 #ifdef _DEBUG_TTYFUNCS
1225         printk(KERN_DEBUG "capinc_tty_throttle\n");
1226 #endif
1227         mp->ttyinstop = 1;
1228 }
1229
1230 static void capinc_tty_unthrottle(struct tty_struct *tty)
1231 {
1232         struct capiminor *mp = tty->driver_data;
1233
1234 #ifdef _DEBUG_TTYFUNCS
1235         printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1236 #endif
1237         mp->ttyinstop = 0;
1238         handle_minor_recv(mp);
1239 }
1240
1241 static void capinc_tty_stop(struct tty_struct *tty)
1242 {
1243         struct capiminor *mp = tty->driver_data;
1244
1245 #ifdef _DEBUG_TTYFUNCS
1246         printk(KERN_DEBUG "capinc_tty_stop\n");
1247 #endif
1248         mp->ttyoutstop = 1;
1249 }
1250
1251 static void capinc_tty_start(struct tty_struct *tty)
1252 {
1253         struct capiminor *mp = tty->driver_data;
1254
1255 #ifdef _DEBUG_TTYFUNCS
1256         printk(KERN_DEBUG "capinc_tty_start\n");
1257 #endif
1258         mp->ttyoutstop = 0;
1259         handle_minor_send(mp);
1260 }
1261
1262 static void capinc_tty_hangup(struct tty_struct *tty)
1263 {
1264         struct capiminor *mp = tty->driver_data;
1265
1266 #ifdef _DEBUG_TTYFUNCS
1267         printk(KERN_DEBUG "capinc_tty_hangup\n");
1268 #endif
1269         tty_port_hangup(&mp->port);
1270 }
1271
1272 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1273 {
1274 #ifdef _DEBUG_TTYFUNCS
1275         printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1276 #endif
1277         return 0;
1278 }
1279
1280 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1281 {
1282 #ifdef _DEBUG_TTYFUNCS
1283         printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1284 #endif
1285 }
1286
1287 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1288 {
1289 #ifdef _DEBUG_TTYFUNCS
1290         printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1291 #endif
1292 }
1293
1294 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1295 {
1296 #ifdef _DEBUG_TTYFUNCS
1297         printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1298 #endif
1299 }
1300
1301 static const struct tty_operations capinc_ops = {
1302         .open = capinc_tty_open,
1303         .close = capinc_tty_close,
1304         .write = capinc_tty_write,
1305         .put_char = capinc_tty_put_char,
1306         .flush_chars = capinc_tty_flush_chars,
1307         .write_room = capinc_tty_write_room,
1308         .chars_in_buffer = capinc_tty_chars_in_buffer,
1309         .ioctl = capinc_tty_ioctl,
1310         .set_termios = capinc_tty_set_termios,
1311         .throttle = capinc_tty_throttle,
1312         .unthrottle = capinc_tty_unthrottle,
1313         .stop = capinc_tty_stop,
1314         .start = capinc_tty_start,
1315         .hangup = capinc_tty_hangup,
1316         .break_ctl = capinc_tty_break_ctl,
1317         .flush_buffer = capinc_tty_flush_buffer,
1318         .set_ldisc = capinc_tty_set_ldisc,
1319         .send_xchar = capinc_tty_send_xchar,
1320         .install = capinc_tty_install,
1321         .cleanup = capinc_tty_cleanup,
1322 };
1323
1324 static int __init capinc_tty_init(void)
1325 {
1326         struct tty_driver *drv;
1327         int err;
1328
1329         if (capi_ttyminors > CAPINC_MAX_PORTS)
1330                 capi_ttyminors = CAPINC_MAX_PORTS;
1331         if (capi_ttyminors <= 0)
1332                 capi_ttyminors = CAPINC_NR_PORTS;
1333
1334         capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1335                              GFP_KERNEL);
1336         if (!capiminors)
1337                 return -ENOMEM;
1338
1339         drv = alloc_tty_driver(capi_ttyminors);
1340         if (!drv) {
1341                 kfree(capiminors);
1342                 return -ENOMEM;
1343         }
1344         drv->owner = THIS_MODULE;
1345         drv->driver_name = "capi_nc";
1346         drv->name = "capi";
1347         drv->major = 0;
1348         drv->minor_start = 0;
1349         drv->type = TTY_DRIVER_TYPE_SERIAL;
1350         drv->subtype = SERIAL_TYPE_NORMAL;
1351         drv->init_termios = tty_std_termios;
1352         drv->init_termios.c_iflag = ICRNL;
1353         drv->init_termios.c_oflag = OPOST | ONLCR;
1354         drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1355         drv->init_termios.c_lflag = 0;
1356         drv->flags =
1357                 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1358                 TTY_DRIVER_DYNAMIC_DEV;
1359         tty_set_operations(drv, &capinc_ops);
1360
1361         err = tty_register_driver(drv);
1362         if (err) {
1363                 put_tty_driver(drv);
1364                 kfree(capiminors);
1365                 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1366                 return err;
1367         }
1368         capinc_tty_driver = drv;
1369         return 0;
1370 }
1371
1372 static void __exit capinc_tty_exit(void)
1373 {
1374         tty_unregister_driver(capinc_tty_driver);
1375         put_tty_driver(capinc_tty_driver);
1376         kfree(capiminors);
1377 }
1378
1379 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1380
1381 static inline int capinc_tty_init(void)
1382 {
1383         return 0;
1384 }
1385
1386 static inline void capinc_tty_exit(void) { }
1387
1388 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1389
1390 /* -------- /proc functions ----------------------------------------- */
1391
1392 /*
1393  * /proc/capi/capi20:
1394  *  minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1395  */
1396 static int capi20_proc_show(struct seq_file *m, void *v)
1397 {
1398         struct capidev *cdev;
1399         struct list_head *l;
1400
1401         mutex_lock(&capidev_list_lock);
1402         list_for_each(l, &capidev_list) {
1403                 cdev = list_entry(l, struct capidev, list);
1404                 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1405                         cdev->ap.applid,
1406                         cdev->ap.nrecvctlpkt,
1407                         cdev->ap.nrecvdatapkt,
1408                         cdev->ap.nsentctlpkt,
1409                         cdev->ap.nsentdatapkt);
1410         }
1411         mutex_unlock(&capidev_list_lock);
1412         return 0;
1413 }
1414
1415 static int capi20_proc_open(struct inode *inode, struct file *file)
1416 {
1417         return single_open(file, capi20_proc_show, NULL);
1418 }
1419
1420 static const struct file_operations capi20_proc_fops = {
1421         .owner          = THIS_MODULE,
1422         .open           = capi20_proc_open,
1423         .read           = seq_read,
1424         .llseek         = seq_lseek,
1425         .release        = single_release,
1426 };
1427
1428 /*
1429  * /proc/capi/capi20ncci:
1430  *  applid ncci
1431  */
1432 static int capi20ncci_proc_show(struct seq_file *m, void *v)
1433 {
1434         struct capidev *cdev;
1435         struct capincci *np;
1436
1437         mutex_lock(&capidev_list_lock);
1438         list_for_each_entry(cdev, &capidev_list, list) {
1439                 mutex_lock(&cdev->lock);
1440                 list_for_each_entry(np, &cdev->nccis, list)
1441                         seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
1442                 mutex_unlock(&cdev->lock);
1443         }
1444         mutex_unlock(&capidev_list_lock);
1445         return 0;
1446 }
1447
1448 static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1449 {
1450         return single_open(file, capi20ncci_proc_show, NULL);
1451 }
1452
1453 static const struct file_operations capi20ncci_proc_fops = {
1454         .owner          = THIS_MODULE,
1455         .open           = capi20ncci_proc_open,
1456         .read           = seq_read,
1457         .llseek         = seq_lseek,
1458         .release        = single_release,
1459 };
1460
1461 static void __init proc_init(void)
1462 {
1463         proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1464         proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
1465 }
1466
1467 static void __exit proc_exit(void)
1468 {
1469         remove_proc_entry("capi/capi20", NULL);
1470         remove_proc_entry("capi/capi20ncci", NULL);
1471 }
1472
1473 /* -------- init function and module interface ---------------------- */
1474
1475
1476 static int __init capi_init(void)
1477 {
1478         const char *compileinfo;
1479         int major_ret;
1480
1481         major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1482         if (major_ret < 0) {
1483                 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1484                 return major_ret;
1485         }
1486         capi_class = class_create(THIS_MODULE, "capi");
1487         if (IS_ERR(capi_class)) {
1488                 unregister_chrdev(capi_major, "capi20");
1489                 return PTR_ERR(capi_class);
1490         }
1491
1492         device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1493
1494         if (capinc_tty_init() < 0) {
1495                 device_destroy(capi_class, MKDEV(capi_major, 0));
1496                 class_destroy(capi_class);
1497                 unregister_chrdev(capi_major, "capi20");
1498                 return -ENOMEM;
1499         }
1500
1501         proc_init();
1502
1503 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1504         compileinfo = " (middleware)";
1505 #else
1506         compileinfo = " (no middleware)";
1507 #endif
1508         printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1509                capi_major, compileinfo);
1510
1511         return 0;
1512 }
1513
1514 static void __exit capi_exit(void)
1515 {
1516         proc_exit();
1517
1518         device_destroy(capi_class, MKDEV(capi_major, 0));
1519         class_destroy(capi_class);
1520         unregister_chrdev(capi_major, "capi20");
1521
1522         capinc_tty_exit();
1523 }
1524
1525 module_init(capi_init);
1526 module_exit(capi_exit);