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