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