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