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