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