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