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