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