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