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