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