CAPI: Rework capiminor RX handler
[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         unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
440         struct tty_struct *tty;
441         struct sk_buff *nskb;
442         u16 errcode, datahandle;
443         struct tty_ldisc *ld;
444         int ret = -1;
445
446         tty = tty_port_tty_get(&mp->port);
447         if (!tty) {
448 #ifdef _DEBUG_DATAFLOW
449                 printk(KERN_DEBUG "capi: currently no receiver\n");
450 #endif
451                 return -1;
452         }
453         
454         ld = tty_ldisc_ref(tty);
455         if (!ld) {
456                 /* fatal error, do not requeue */
457                 ret = 0;
458                 kfree_skb(skb);
459                 goto deref_tty;
460         }
461
462         if (ld->ops->receive_buf == NULL) {
463 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
464                 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
465 #endif
466                 /* fatal error, do not requeue */
467                 goto free_skb;
468         }
469         if (mp->ttyinstop) {
470 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
471                 printk(KERN_DEBUG "capi: recv tty throttled\n");
472 #endif
473                 goto deref_ldisc;
474         }
475
476         if (tty->receive_room < datalen) {
477 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
478                 printk(KERN_DEBUG "capi: no room in tty\n");
479 #endif
480                 goto deref_ldisc;
481         }
482
483         nskb = gen_data_b3_resp_for(mp, skb);
484         if (!nskb) {
485                 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
486                 goto deref_ldisc;
487         }
488
489         datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
490
491         errcode = capi20_put_message(mp->ap, nskb);
492
493         if (errcode == CAPI_NOERROR) {
494                 skb_pull(skb, CAPIMSG_LEN(skb->data));
495 #ifdef _DEBUG_DATAFLOW
496                 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
497                                         datahandle, skb->len);
498 #endif
499                 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
500         } else {
501                 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
502                                 errcode);
503                 kfree_skb(nskb);
504
505                 if (errcode == CAPI_SENDQUEUEFULL)
506                         goto deref_ldisc;
507         }
508
509 free_skb:
510         ret = 0;
511         kfree_skb(skb);
512
513 deref_ldisc:
514         tty_ldisc_deref(ld);
515
516 deref_tty:
517         tty_kref_put(tty);
518         return ret;
519 }
520
521 static void handle_minor_recv(struct capiminor *mp)
522 {
523         struct sk_buff *skb;
524         while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
525                 unsigned int len = skb->len;
526                 mp->inbytes -= len;
527                 if (handle_recv_skb(mp, skb) < 0) {
528                         skb_queue_head(&mp->inqueue, skb);
529                         mp->inbytes += len;
530                         return;
531                 }
532         }
533 }
534
535 static int handle_minor_send(struct capiminor *mp)
536 {
537         struct tty_struct *tty;
538         struct sk_buff *skb;
539         u16 len;
540         int count = 0;
541         u16 errcode;
542         u16 datahandle;
543
544         tty = tty_port_tty_get(&mp->port);
545         if (!tty)
546                 return 0;
547
548         if (mp->ttyoutstop) {
549 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
550                 printk(KERN_DEBUG "capi: send: tty stopped\n");
551 #endif
552                 tty_kref_put(tty);
553                 return 0;
554         }
555
556         while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
557                 datahandle = mp->datahandle;
558                 len = (u16)skb->len;
559                 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
560                 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
561                 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
562                 capimsg_setu16(skb->data, 2, mp->ap->applid);
563                 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
564                 capimsg_setu8 (skb->data, 5, CAPI_REQ);
565                 capimsg_setu16(skb->data, 6, mp->msgid++);
566                 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
567                 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
568                 capimsg_setu16(skb->data, 16, len);     /* Data length */
569                 capimsg_setu16(skb->data, 18, datahandle);
570                 capimsg_setu16(skb->data, 20, 0);       /* Flags */
571
572                 if (capiminor_add_ack(mp, datahandle) < 0) {
573                         skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
574                         skb_queue_head(&mp->outqueue, skb);
575                         tty_kref_put(tty);
576                         return count;
577                 }
578                 errcode = capi20_put_message(mp->ap, skb);
579                 if (errcode == CAPI_NOERROR) {
580                         mp->datahandle++;
581                         count++;
582                         mp->outbytes -= len;
583 #ifdef _DEBUG_DATAFLOW
584                         printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
585                                                         datahandle, len);
586 #endif
587                         continue;
588                 }
589                 capiminor_del_ack(mp, datahandle);
590
591                 if (errcode == CAPI_SENDQUEUEFULL) {
592                         skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
593                         skb_queue_head(&mp->outqueue, skb);
594                         break;
595                 }
596
597                 /* ups, drop packet */
598                 printk(KERN_ERR "capi: put_message = %x\n", errcode);
599                 mp->outbytes -= len;
600                 kfree_skb(skb);
601         }
602         tty_kref_put(tty);
603         return count;
604 }
605
606 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
607 /* -------- function called by lower level -------------------------- */
608
609 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
610 {
611         struct capidev *cdev = ap->private;
612 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
613         struct tty_struct *tty;
614         struct capiminor *mp;
615         u16 datahandle;
616 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
617         struct capincci *np;
618         unsigned long flags;
619
620         mutex_lock(&cdev->lock);
621
622         if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
623                 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
624                 if ((info & 0xff00) == 0)
625                         capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
626         }
627         if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
628                 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
629
630         spin_lock_irqsave(&workaround_lock, flags);
631         if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
632                 skb_queue_tail(&cdev->recvqueue, skb);
633                 wake_up_interruptible(&cdev->recvwait);
634                 goto unlock_out;
635         }
636
637         np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
638         if (!np) {
639                 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
640                 skb_queue_tail(&cdev->recvqueue, skb);
641                 wake_up_interruptible(&cdev->recvwait);
642                 goto unlock_out;
643         }
644
645 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
646         skb_queue_tail(&cdev->recvqueue, skb);
647         wake_up_interruptible(&cdev->recvwait);
648
649 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
650
651         mp = np->minorp;
652         if (!mp) {
653                 skb_queue_tail(&cdev->recvqueue, skb);
654                 wake_up_interruptible(&cdev->recvwait);
655                 goto unlock_out;
656         }
657         if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
658                 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
659 #ifdef _DEBUG_DATAFLOW
660                 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
661                                 datahandle, skb->len-CAPIMSG_LEN(skb->data));
662 #endif
663                 skb_queue_tail(&mp->inqueue, skb);
664                 mp->inbytes += skb->len;
665                 handle_minor_recv(mp);
666
667         } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
668
669                 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
670 #ifdef _DEBUG_DATAFLOW
671                 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
672                                 datahandle,
673                                 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
674 #endif
675                 kfree_skb(skb);
676                 (void)capiminor_del_ack(mp, datahandle);
677                 tty = tty_port_tty_get(&mp->port);
678                 if (tty) {
679                         tty_wakeup(tty);
680                         tty_kref_put(tty);
681                 }
682                 (void)handle_minor_send(mp);
683
684         } else {
685                 /* ups, let capi application handle it :-) */
686                 skb_queue_tail(&cdev->recvqueue, skb);
687                 wake_up_interruptible(&cdev->recvwait);
688         }
689 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
690
691 unlock_out:
692         spin_unlock_irqrestore(&workaround_lock, flags);
693         mutex_unlock(&cdev->lock);
694 }
695
696 /* -------- file_operations for capidev ----------------------------- */
697
698 static ssize_t
699 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
700 {
701         struct capidev *cdev = (struct capidev *)file->private_data;
702         struct sk_buff *skb;
703         size_t copied;
704         int err;
705
706         if (!cdev->ap.applid)
707                 return -ENODEV;
708
709         skb = skb_dequeue(&cdev->recvqueue);
710         if (!skb) {
711                 if (file->f_flags & O_NONBLOCK)
712                         return -EAGAIN;
713                 err = wait_event_interruptible(cdev->recvwait,
714                                 (skb = skb_dequeue(&cdev->recvqueue)));
715                 if (err)
716                         return err;
717         }
718         if (skb->len > count) {
719                 skb_queue_head(&cdev->recvqueue, skb);
720                 return -EMSGSIZE;
721         }
722         if (copy_to_user(buf, skb->data, skb->len)) {
723                 skb_queue_head(&cdev->recvqueue, skb);
724                 return -EFAULT;
725         }
726         copied = skb->len;
727
728         kfree_skb(skb);
729
730         return copied;
731 }
732
733 static ssize_t
734 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
735 {
736         struct capidev *cdev = (struct capidev *)file->private_data;
737         struct sk_buff *skb;
738         u16 mlen;
739
740         if (!cdev->ap.applid)
741                 return -ENODEV;
742
743         skb = alloc_skb(count, GFP_USER);
744         if (!skb)
745                 return -ENOMEM;
746
747         if (copy_from_user(skb_put(skb, count), buf, count)) {
748                 kfree_skb(skb);
749                 return -EFAULT;
750         }
751         mlen = CAPIMSG_LEN(skb->data);
752         if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
753                 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
754                         kfree_skb(skb);
755                         return -EINVAL;
756                 }
757         } else {
758                 if (mlen != count) {
759                         kfree_skb(skb);
760                         return -EINVAL;
761                 }
762         }
763         CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
764
765         if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
766                 mutex_lock(&cdev->lock);
767                 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
768                 mutex_unlock(&cdev->lock);
769         }
770
771         cdev->errcode = capi20_put_message(&cdev->ap, skb);
772
773         if (cdev->errcode) {
774                 kfree_skb(skb);
775                 return -EIO;
776         }
777         return count;
778 }
779
780 static unsigned int
781 capi_poll(struct file *file, poll_table * wait)
782 {
783         struct capidev *cdev = (struct capidev *)file->private_data;
784         unsigned int mask = 0;
785
786         if (!cdev->ap.applid)
787                 return POLLERR;
788
789         poll_wait(file, &(cdev->recvwait), wait);
790         mask = POLLOUT | POLLWRNORM;
791         if (!skb_queue_empty(&cdev->recvqueue))
792                 mask |= POLLIN | POLLRDNORM;
793         return mask;
794 }
795
796 static int
797 capi_ioctl(struct inode *inode, struct file *file,
798            unsigned int cmd, unsigned long arg)
799 {
800         struct capidev *cdev = file->private_data;
801         capi_ioctl_struct data;
802         int retval = -EINVAL;
803         void __user *argp = (void __user *)arg;
804
805         switch (cmd) {
806         case CAPI_REGISTER:
807                 mutex_lock(&cdev->lock);
808
809                 if (cdev->ap.applid) {
810                         retval = -EEXIST;
811                         goto register_out;
812                 }
813                 if (copy_from_user(&cdev->ap.rparam, argp,
814                                    sizeof(struct capi_register_params))) {
815                         retval = -EFAULT;
816                         goto register_out;
817                 }
818                 cdev->ap.private = cdev;
819                 cdev->ap.recv_message = capi_recv_message;
820                 cdev->errcode = capi20_register(&cdev->ap);
821                 retval = (int)cdev->ap.applid;
822                 if (cdev->errcode) {
823                         cdev->ap.applid = 0;
824                         retval = -EIO;
825                 }
826
827 register_out:
828                 mutex_unlock(&cdev->lock);
829                 return retval;
830
831         case CAPI_GET_VERSION:
832                 {
833                         if (copy_from_user(&data.contr, argp,
834                                                 sizeof(data.contr)))
835                                 return -EFAULT;
836                         cdev->errcode = capi20_get_version(data.contr, &data.version);
837                         if (cdev->errcode)
838                                 return -EIO;
839                         if (copy_to_user(argp, &data.version,
840                                          sizeof(data.version)))
841                                 return -EFAULT;
842                 }
843                 return 0;
844
845         case CAPI_GET_SERIAL:
846                 {
847                         if (copy_from_user(&data.contr, argp,
848                                            sizeof(data.contr)))
849                                 return -EFAULT;
850                         cdev->errcode = capi20_get_serial (data.contr, data.serial);
851                         if (cdev->errcode)
852                                 return -EIO;
853                         if (copy_to_user(argp, data.serial,
854                                          sizeof(data.serial)))
855                                 return -EFAULT;
856                 }
857                 return 0;
858         case CAPI_GET_PROFILE:
859                 {
860                         if (copy_from_user(&data.contr, argp,
861                                            sizeof(data.contr)))
862                                 return -EFAULT;
863
864                         if (data.contr == 0) {
865                                 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
866                                 if (cdev->errcode)
867                                         return -EIO;
868
869                                 retval = copy_to_user(argp,
870                                       &data.profile.ncontroller,
871                                        sizeof(data.profile.ncontroller));
872
873                         } else {
874                                 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
875                                 if (cdev->errcode)
876                                         return -EIO;
877
878                                 retval = copy_to_user(argp, &data.profile,
879                                                    sizeof(data.profile));
880                         }
881                         if (retval)
882                                 return -EFAULT;
883                 }
884                 return 0;
885
886         case CAPI_GET_MANUFACTURER:
887                 {
888                         if (copy_from_user(&data.contr, argp,
889                                            sizeof(data.contr)))
890                                 return -EFAULT;
891                         cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
892                         if (cdev->errcode)
893                                 return -EIO;
894
895                         if (copy_to_user(argp, data.manufacturer,
896                                          sizeof(data.manufacturer)))
897                                 return -EFAULT;
898
899                 }
900                 return 0;
901         case CAPI_GET_ERRCODE:
902                 data.errcode = cdev->errcode;
903                 cdev->errcode = CAPI_NOERROR;
904                 if (arg) {
905                         if (copy_to_user(argp, &data.errcode,
906                                          sizeof(data.errcode)))
907                                 return -EFAULT;
908                 }
909                 return data.errcode;
910
911         case CAPI_INSTALLED:
912                 if (capi20_isinstalled() == CAPI_NOERROR)
913                         return 0;
914                 return -ENXIO;
915
916         case CAPI_MANUFACTURER_CMD:
917                 {
918                         struct capi_manufacturer_cmd mcmd;
919                         if (!capable(CAP_SYS_ADMIN))
920                                 return -EPERM;
921                         if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
922                                 return -EFAULT;
923                         return capi20_manufacturer(mcmd.cmd, mcmd.data);
924                 }
925                 return 0;
926
927         case CAPI_SET_FLAGS:
928         case CAPI_CLR_FLAGS: {
929                 unsigned userflags;
930
931                 if (copy_from_user(&userflags, argp, sizeof(userflags)))
932                         return -EFAULT;
933
934                 mutex_lock(&cdev->lock);
935                 if (cmd == CAPI_SET_FLAGS)
936                         cdev->userflags |= userflags;
937                 else
938                         cdev->userflags &= ~userflags;
939                 mutex_unlock(&cdev->lock);
940                 return 0;
941         }
942         case CAPI_GET_FLAGS:
943                 if (copy_to_user(argp, &cdev->userflags,
944                                  sizeof(cdev->userflags)))
945                         return -EFAULT;
946                 return 0;
947
948         case CAPI_NCCI_OPENCOUNT: {
949                 struct capincci *nccip;
950                 unsigned ncci;
951                 int count = 0;
952
953                 if (copy_from_user(&ncci, argp, sizeof(ncci)))
954                         return -EFAULT;
955
956                 mutex_lock(&cdev->lock);
957                 nccip = capincci_find(cdev, (u32)ncci);
958                 if (nccip)
959                         count = capincci_minor_opencount(nccip);
960                 mutex_unlock(&cdev->lock);
961                 return count;
962         }
963
964 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
965         case CAPI_NCCI_GETUNIT: {
966                 struct capincci *nccip;
967                 struct capiminor *mp;
968                 unsigned ncci;
969                 int unit = -ESRCH;
970
971                 if (copy_from_user(&ncci, argp, sizeof(ncci)))
972                         return -EFAULT;
973
974                 mutex_lock(&cdev->lock);
975                 nccip = capincci_find(cdev, (u32)ncci);
976                 if (nccip) {
977                         mp = nccip->minorp;
978                         if (mp)
979                                 unit = mp->minor;
980                 }
981                 mutex_unlock(&cdev->lock);
982                 return unit;
983         }
984 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
985
986         default:
987                 return -EINVAL;
988         }
989 }
990
991 static int capi_open(struct inode *inode, struct file *file)
992 {
993         struct capidev *cdev;
994
995         cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
996         if (!cdev)
997                 return -ENOMEM;
998
999         mutex_init(&cdev->lock);
1000         skb_queue_head_init(&cdev->recvqueue);
1001         init_waitqueue_head(&cdev->recvwait);
1002         INIT_LIST_HEAD(&cdev->nccis);
1003         file->private_data = cdev;
1004
1005         mutex_lock(&capidev_list_lock);
1006         list_add_tail(&cdev->list, &capidev_list);
1007         mutex_unlock(&capidev_list_lock);
1008
1009         return nonseekable_open(inode, file);
1010 }
1011
1012 static int capi_release(struct inode *inode, struct file *file)
1013 {
1014         struct capidev *cdev = file->private_data;
1015
1016         mutex_lock(&capidev_list_lock);
1017         list_del(&cdev->list);
1018         mutex_unlock(&capidev_list_lock);
1019
1020         if (cdev->ap.applid)
1021                 capi20_release(&cdev->ap);
1022         skb_queue_purge(&cdev->recvqueue);
1023         capincci_free(cdev, 0xffffffff);
1024
1025         kfree(cdev);
1026         return 0;
1027 }
1028
1029 static const struct file_operations capi_fops =
1030 {
1031         .owner          = THIS_MODULE,
1032         .llseek         = no_llseek,
1033         .read           = capi_read,
1034         .write          = capi_write,
1035         .poll           = capi_poll,
1036         .ioctl          = capi_ioctl,
1037         .open           = capi_open,
1038         .release        = capi_release,
1039 };
1040
1041 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1042 /* -------- tty_operations for capincci ----------------------------- */
1043
1044 static int
1045 capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1046 {
1047         int idx = tty->index;
1048         struct capiminor *mp = capiminor_get(idx);
1049         int ret = tty_init_termios(tty);
1050
1051         if (ret == 0) {
1052                 tty_driver_kref_get(driver);
1053                 tty->count++;
1054                 tty->driver_data = mp;
1055                 driver->ttys[idx] = tty;
1056         } else
1057                 capiminor_put(mp);
1058         return ret;
1059 }
1060
1061 static void capinc_tty_cleanup(struct tty_struct *tty)
1062 {
1063         struct capiminor *mp = tty->driver_data;
1064         tty->driver_data = NULL;
1065         capiminor_put(mp);
1066 }
1067
1068 static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
1069 {
1070         struct capiminor *mp = tty->driver_data;
1071         unsigned long flags;
1072         int err;
1073
1074         err = tty_port_open(&mp->port, tty, filp);
1075         if (err)
1076                 return err;
1077
1078         spin_lock_irqsave(&workaround_lock, flags);
1079         handle_minor_recv(mp);
1080         spin_unlock_irqrestore(&workaround_lock, flags);
1081         return 0;
1082 }
1083
1084 static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
1085 {
1086         struct capiminor *mp = tty->driver_data;
1087
1088         tty_port_close(&mp->port, tty, filp);
1089 }
1090
1091 static int capinc_tty_write(struct tty_struct *tty,
1092                             const unsigned char *buf, int count)
1093 {
1094         struct capiminor *mp = tty->driver_data;
1095         struct sk_buff *skb;
1096         unsigned long flags;
1097
1098 #ifdef _DEBUG_TTYFUNCS
1099         printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1100 #endif
1101
1102         spin_lock_irqsave(&workaround_lock, flags);
1103         skb = mp->ttyskb;
1104         if (skb) {
1105                 mp->ttyskb = NULL;
1106                 skb_queue_tail(&mp->outqueue, skb);
1107                 mp->outbytes += skb->len;
1108         }
1109
1110         skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1111         if (!skb) {
1112                 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1113                 spin_unlock_irqrestore(&workaround_lock, flags);
1114                 return -ENOMEM;
1115         }
1116
1117         skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1118         memcpy(skb_put(skb, count), buf, count);
1119
1120         skb_queue_tail(&mp->outqueue, skb);
1121         mp->outbytes += skb->len;
1122         (void)handle_minor_send(mp);
1123         spin_unlock_irqrestore(&workaround_lock, flags);
1124         return count;
1125 }
1126
1127 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1128 {
1129         struct capiminor *mp = tty->driver_data;
1130         struct sk_buff *skb;
1131         unsigned long flags;
1132         int ret = 1;
1133
1134 #ifdef _DEBUG_TTYFUNCS
1135         printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1136 #endif
1137
1138         spin_lock_irqsave(&workaround_lock, flags);
1139         skb = mp->ttyskb;
1140         if (skb) {
1141                 if (skb_tailroom(skb) > 0) {
1142                         *(skb_put(skb, 1)) = ch;
1143                         spin_unlock_irqrestore(&workaround_lock, flags);
1144                         return 1;
1145                 }
1146                 mp->ttyskb = NULL;
1147                 skb_queue_tail(&mp->outqueue, skb);
1148                 mp->outbytes += skb->len;
1149                 (void)handle_minor_send(mp);
1150         }
1151         skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1152         if (skb) {
1153                 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1154                 *(skb_put(skb, 1)) = ch;
1155                 mp->ttyskb = skb;
1156         } else {
1157                 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1158                 ret = 0;
1159         }
1160         spin_unlock_irqrestore(&workaround_lock, flags);
1161         return ret;
1162 }
1163
1164 static void capinc_tty_flush_chars(struct tty_struct *tty)
1165 {
1166         struct capiminor *mp = tty->driver_data;
1167         struct sk_buff *skb;
1168         unsigned long flags;
1169
1170 #ifdef _DEBUG_TTYFUNCS
1171         printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1172 #endif
1173
1174         spin_lock_irqsave(&workaround_lock, flags);
1175         skb = mp->ttyskb;
1176         if (skb) {
1177                 mp->ttyskb = NULL;
1178                 skb_queue_tail(&mp->outqueue, skb);
1179                 mp->outbytes += skb->len;
1180                 (void)handle_minor_send(mp);
1181         }
1182         (void)handle_minor_recv(mp);
1183         spin_unlock_irqrestore(&workaround_lock, flags);
1184 }
1185
1186 static int capinc_tty_write_room(struct tty_struct *tty)
1187 {
1188         struct capiminor *mp = tty->driver_data;
1189         int room;
1190
1191         room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1192         room *= CAPI_MAX_BLKSIZE;
1193 #ifdef _DEBUG_TTYFUNCS
1194         printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1195 #endif
1196         return room;
1197 }
1198
1199 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1200 {
1201         struct capiminor *mp = tty->driver_data;
1202
1203 #ifdef _DEBUG_TTYFUNCS
1204         printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1205                         mp->outbytes, mp->nack,
1206                         skb_queue_len(&mp->outqueue),
1207                         skb_queue_len(&mp->inqueue));
1208 #endif
1209         return mp->outbytes;
1210 }
1211
1212 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1213                     unsigned int cmd, unsigned long arg)
1214 {
1215         int error = 0;
1216         switch (cmd) {
1217         default:
1218                 error = n_tty_ioctl_helper(tty, file, cmd, arg);
1219                 break;
1220         }
1221         return error;
1222 }
1223
1224 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1225 {
1226 #ifdef _DEBUG_TTYFUNCS
1227         printk(KERN_DEBUG "capinc_tty_set_termios\n");
1228 #endif
1229 }
1230
1231 static void capinc_tty_throttle(struct tty_struct *tty)
1232 {
1233         struct capiminor *mp = tty->driver_data;
1234 #ifdef _DEBUG_TTYFUNCS
1235         printk(KERN_DEBUG "capinc_tty_throttle\n");
1236 #endif
1237         mp->ttyinstop = 1;
1238 }
1239
1240 static void capinc_tty_unthrottle(struct tty_struct *tty)
1241 {
1242         struct capiminor *mp = tty->driver_data;
1243         unsigned long flags;
1244
1245 #ifdef _DEBUG_TTYFUNCS
1246         printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1247 #endif
1248         spin_lock_irqsave(&workaround_lock, flags);
1249         mp->ttyinstop = 0;
1250         handle_minor_recv(mp);
1251         spin_unlock_irqrestore(&workaround_lock, flags);
1252 }
1253
1254 static void capinc_tty_stop(struct tty_struct *tty)
1255 {
1256         struct capiminor *mp = tty->driver_data;
1257
1258 #ifdef _DEBUG_TTYFUNCS
1259         printk(KERN_DEBUG "capinc_tty_stop\n");
1260 #endif
1261         mp->ttyoutstop = 1;
1262 }
1263
1264 static void capinc_tty_start(struct tty_struct *tty)
1265 {
1266         struct capiminor *mp = tty->driver_data;
1267         unsigned long flags;
1268
1269 #ifdef _DEBUG_TTYFUNCS
1270         printk(KERN_DEBUG "capinc_tty_start\n");
1271 #endif
1272         spin_lock_irqsave(&workaround_lock, flags);
1273         mp->ttyoutstop = 0;
1274         (void)handle_minor_send(mp);
1275         spin_unlock_irqrestore(&workaround_lock, flags);
1276 }
1277
1278 static void capinc_tty_hangup(struct tty_struct *tty)
1279 {
1280         struct capiminor *mp = tty->driver_data;
1281
1282 #ifdef _DEBUG_TTYFUNCS
1283         printk(KERN_DEBUG "capinc_tty_hangup\n");
1284 #endif
1285         tty_port_hangup(&mp->port);
1286 }
1287
1288 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1289 {
1290 #ifdef _DEBUG_TTYFUNCS
1291         printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1292 #endif
1293         return 0;
1294 }
1295
1296 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1297 {
1298 #ifdef _DEBUG_TTYFUNCS
1299         printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1300 #endif
1301 }
1302
1303 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1304 {
1305 #ifdef _DEBUG_TTYFUNCS
1306         printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1307 #endif
1308 }
1309
1310 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1311 {
1312 #ifdef _DEBUG_TTYFUNCS
1313         printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1314 #endif
1315 }
1316
1317 static const struct tty_operations capinc_ops = {
1318         .open = capinc_tty_open,
1319         .close = capinc_tty_close,
1320         .write = capinc_tty_write,
1321         .put_char = capinc_tty_put_char,
1322         .flush_chars = capinc_tty_flush_chars,
1323         .write_room = capinc_tty_write_room,
1324         .chars_in_buffer = capinc_tty_chars_in_buffer,
1325         .ioctl = capinc_tty_ioctl,
1326         .set_termios = capinc_tty_set_termios,
1327         .throttle = capinc_tty_throttle,
1328         .unthrottle = capinc_tty_unthrottle,
1329         .stop = capinc_tty_stop,
1330         .start = capinc_tty_start,
1331         .hangup = capinc_tty_hangup,
1332         .break_ctl = capinc_tty_break_ctl,
1333         .flush_buffer = capinc_tty_flush_buffer,
1334         .set_ldisc = capinc_tty_set_ldisc,
1335         .send_xchar = capinc_tty_send_xchar,
1336         .install = capinc_tty_install,
1337         .cleanup = capinc_tty_cleanup,
1338 };
1339
1340 static int __init capinc_tty_init(void)
1341 {
1342         struct tty_driver *drv;
1343         int err;
1344
1345         if (capi_ttyminors > CAPINC_MAX_PORTS)
1346                 capi_ttyminors = CAPINC_MAX_PORTS;
1347         if (capi_ttyminors <= 0)
1348                 capi_ttyminors = CAPINC_NR_PORTS;
1349
1350         capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1351                              GFP_KERNEL);
1352         if (!capiminors)
1353                 return -ENOMEM;
1354
1355         drv = alloc_tty_driver(capi_ttyminors);
1356         if (!drv) {
1357                 kfree(capiminors);
1358                 return -ENOMEM;
1359         }
1360         drv->owner = THIS_MODULE;
1361         drv->driver_name = "capi_nc";
1362         drv->name = "capi";
1363         drv->major = 0;
1364         drv->minor_start = 0;
1365         drv->type = TTY_DRIVER_TYPE_SERIAL;
1366         drv->subtype = SERIAL_TYPE_NORMAL;
1367         drv->init_termios = tty_std_termios;
1368         drv->init_termios.c_iflag = ICRNL;
1369         drv->init_termios.c_oflag = OPOST | ONLCR;
1370         drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1371         drv->init_termios.c_lflag = 0;
1372         drv->flags =
1373                 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1374                 TTY_DRIVER_DYNAMIC_DEV;
1375         tty_set_operations(drv, &capinc_ops);
1376
1377         err = tty_register_driver(drv);
1378         if (err) {
1379                 put_tty_driver(drv);
1380                 kfree(capiminors);
1381                 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1382                 return err;
1383         }
1384         capinc_tty_driver = drv;
1385         return 0;
1386 }
1387
1388 static void __exit capinc_tty_exit(void)
1389 {
1390         tty_unregister_driver(capinc_tty_driver);
1391         put_tty_driver(capinc_tty_driver);
1392         kfree(capiminors);
1393 }
1394
1395 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1396
1397 static inline int capinc_tty_init(void)
1398 {
1399         return 0;
1400 }
1401
1402 static inline void capinc_tty_exit(void) { }
1403
1404 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1405
1406 /* -------- /proc functions ----------------------------------------- */
1407
1408 /*
1409  * /proc/capi/capi20:
1410  *  minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1411  */
1412 static int capi20_proc_show(struct seq_file *m, void *v)
1413 {
1414         struct capidev *cdev;
1415         struct list_head *l;
1416
1417         mutex_lock(&capidev_list_lock);
1418         list_for_each(l, &capidev_list) {
1419                 cdev = list_entry(l, struct capidev, list);
1420                 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1421                         cdev->ap.applid,
1422                         cdev->ap.nrecvctlpkt,
1423                         cdev->ap.nrecvdatapkt,
1424                         cdev->ap.nsentctlpkt,
1425                         cdev->ap.nsentdatapkt);
1426         }
1427         mutex_unlock(&capidev_list_lock);
1428         return 0;
1429 }
1430
1431 static int capi20_proc_open(struct inode *inode, struct file *file)
1432 {
1433         return single_open(file, capi20_proc_show, NULL);
1434 }
1435
1436 static const struct file_operations capi20_proc_fops = {
1437         .owner          = THIS_MODULE,
1438         .open           = capi20_proc_open,
1439         .read           = seq_read,
1440         .llseek         = seq_lseek,
1441         .release        = single_release,
1442 };
1443
1444 /*
1445  * /proc/capi/capi20ncci:
1446  *  applid ncci
1447  */
1448 static int capi20ncci_proc_show(struct seq_file *m, void *v)
1449 {
1450         struct capidev *cdev;
1451         struct capincci *np;
1452
1453         mutex_lock(&capidev_list_lock);
1454         list_for_each_entry(cdev, &capidev_list, list) {
1455                 mutex_lock(&cdev->lock);
1456                 list_for_each_entry(np, &cdev->nccis, list)
1457                         seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
1458                 mutex_unlock(&cdev->lock);
1459         }
1460         mutex_unlock(&capidev_list_lock);
1461         return 0;
1462 }
1463
1464 static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1465 {
1466         return single_open(file, capi20ncci_proc_show, NULL);
1467 }
1468
1469 static const struct file_operations capi20ncci_proc_fops = {
1470         .owner          = THIS_MODULE,
1471         .open           = capi20ncci_proc_open,
1472         .read           = seq_read,
1473         .llseek         = seq_lseek,
1474         .release        = single_release,
1475 };
1476
1477 static void __init proc_init(void)
1478 {
1479         proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1480         proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
1481 }
1482
1483 static void __exit proc_exit(void)
1484 {
1485         remove_proc_entry("capi/capi20", NULL);
1486         remove_proc_entry("capi/capi20ncci", NULL);
1487 }
1488
1489 /* -------- init function and module interface ---------------------- */
1490
1491
1492 static int __init capi_init(void)
1493 {
1494         const char *compileinfo;
1495         int major_ret;
1496
1497         major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1498         if (major_ret < 0) {
1499                 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1500                 return major_ret;
1501         }
1502         capi_class = class_create(THIS_MODULE, "capi");
1503         if (IS_ERR(capi_class)) {
1504                 unregister_chrdev(capi_major, "capi20");
1505                 return PTR_ERR(capi_class);
1506         }
1507
1508         device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1509
1510         if (capinc_tty_init() < 0) {
1511                 device_destroy(capi_class, MKDEV(capi_major, 0));
1512                 class_destroy(capi_class);
1513                 unregister_chrdev(capi_major, "capi20");
1514                 return -ENOMEM;
1515         }
1516
1517         proc_init();
1518
1519 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1520         compileinfo = " (middleware+capifs)";
1521 #elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
1522         compileinfo = " (no capifs)";
1523 #else
1524         compileinfo = " (no middleware)";
1525 #endif
1526         printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1527                capi_major, compileinfo);
1528
1529         return 0;
1530 }
1531
1532 static void __exit capi_exit(void)
1533 {
1534         proc_exit();
1535
1536         device_destroy(capi_class, MKDEV(capi_major, 0));
1537         class_destroy(capi_class);
1538         unregister_chrdev(capi_major, "capi20");
1539
1540         capinc_tty_exit();
1541 }
1542
1543 module_init(capi_init);
1544 module_exit(capi_exit);