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