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