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