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