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