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