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