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