blob: 403bf8fcb285255fb708e66fe00a4fb5a3160bdb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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>
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -070021#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mm.h>
Jonathan Corbeta237f3b2008-05-16 14:15:33 -060023#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/timer.h>
25#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/tty.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/netdevice.h>
28#include <linux/ppp_defs.h>
29#include <linux/if_ppp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/skbuff.h>
31#include <linux/proc_fs.h>
Alexey Dobriyan9a58a802010-01-14 03:10:54 -080032#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/isdn/capiutil.h>
40#include <linux/isdn/capicmd.h>
Jan Kiszka90926f02010-02-08 10:12:06 +000041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "capifs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
45MODULE_AUTHOR("Carsten Paeth");
46MODULE_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
gregkh@suse.de56b22932005-03-23 10:01:41 -080054static struct class *capi_class;
Adrian Bunk408b6642005-05-01 08:59:29 -070055static int capi_major = 68; /* allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57module_param_named(major, capi_major, uint, 0);
Jan Kiszka501c87a2010-02-08 10:12:16 +000058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +000060#define CAPINC_NR_PORTS 32
61#define CAPINC_MAX_PORTS 256
62
63static int capi_ttymajor = 191;
64static int capi_ttyminors = CAPINC_NR_PORTS;
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066module_param_named(ttymajor, capi_ttymajor, uint, 0);
67module_param_named(ttyminors, capi_ttyminors, uint, 0);
68#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
69
70/* -------- defines ------------------------------------------------- */
71
72#define CAPINC_MAX_RECVQUEUE 10
73#define CAPINC_MAX_SENDQUEUE 10
74#define CAPI_MAX_BLKSIZE 2048
75
76/* -------- data structures ----------------------------------------- */
77
78struct capidev;
79struct capincci;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080struct capiminor;
81
Michael Buesch6aa65472006-06-26 00:25:30 -070082struct datahandle_queue {
83 struct list_head list;
84 u16 datahandle;
85};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087struct capiminor {
88 struct list_head list;
89 struct capincci *nccip;
90 unsigned int minor;
Jan Kiszka90926f02010-02-08 10:12:06 +000091 struct dentry *capifs_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 struct capi20_appl *ap;
94 u32 ncci;
95 u16 datahandle;
96 u16 msgid;
97
98 struct tty_struct *tty;
99 int ttyinstop;
100 int ttyoutstop;
101 struct sk_buff *ttyskb;
102 atomic_t ttyopencount;
103
104 struct sk_buff_head inqueue;
105 int inbytes;
106 struct sk_buff_head outqueue;
107 int outbytes;
108
109 /* transmit path */
Michael Buesch6aa65472006-06-26 00:25:30 -0700110 struct list_head ackqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 int nack;
Michael Buesch6aa65472006-06-26 00:25:30 -0700112 spinlock_t ackqlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Michael Buesch053b47f2007-02-12 00:53:26 -0800115/* FIXME: The following lock is a sledgehammer-workaround to a
116 * locking issue with the capiminor (and maybe other) data structure(s).
117 * Access to this data is done in a racy way and crashes the machine with
118 * a FritzCard DSL driver; sooner or later. This is a workaround
119 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
120 * The correct (and scalable) fix for the issue seems to require
121 * an API change to the drivers... . */
122static DEFINE_SPINLOCK(workaround_lock);
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124struct capincci {
125 struct capincci *next;
126 u32 ncci;
127 struct capidev *cdev;
128#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
129 struct capiminor *minorp;
130#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
131};
132
133struct capidev {
134 struct list_head list;
135 struct capi20_appl ap;
136 u16 errcode;
137 unsigned userflags;
138
139 struct sk_buff_head recvqueue;
140 wait_queue_head_t recvwait;
141
142 struct capincci *nccis;
143
Jan Kiszka05b41492010-02-08 10:12:19 +0000144 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
147/* -------- global variables ---------------------------------------- */
148
Jan Kiszkab8f433d2010-02-08 10:12:17 +0000149static DEFINE_MUTEX(capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static LIST_HEAD(capidev_list);
151
152#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +0000153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static DEFINE_RWLOCK(capiminor_list_lock);
155static LIST_HEAD(capiminor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* -------- datahandles --------------------------------------------- */
158
Jan Kiszka501c87a2010-02-08 10:12:16 +0000159static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Michael Buesch6aa65472006-06-26 00:25:30 -0700161 struct datahandle_queue *n;
162 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 n = kmalloc(sizeof(*n), GFP_ATOMIC);
Michael Buesch6aa65472006-06-26 00:25:30 -0700165 if (unlikely(!n)) {
166 printk(KERN_ERR "capi: alloc datahandle failed\n");
167 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 n->datahandle = datahandle;
Michael Buesch6aa65472006-06-26 00:25:30 -0700170 INIT_LIST_HEAD(&n->list);
171 spin_lock_irqsave(&mp->ackqlock, flags);
172 list_add_tail(&n->list, &mp->ackqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 mp->nack++;
Michael Buesch6aa65472006-06-26 00:25:30 -0700174 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return 0;
176}
177
178static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
179{
Michael Buesch6aa65472006-06-26 00:25:30 -0700180 struct datahandle_queue *p, *tmp;
181 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Michael Buesch6aa65472006-06-26 00:25:30 -0700183 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 kfree(p);
188 mp->nack--;
Michael Buesch6aa65472006-06-26 00:25:30 -0700189 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return 0;
191 }
192 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700193 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return -1;
195}
196
197static void capiminor_del_all_ack(struct capiminor *mp)
198{
Michael Buesch6aa65472006-06-26 00:25:30 -0700199 struct datahandle_queue *p, *tmp;
200 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Michael Buesch6aa65472006-06-26 00:25:30 -0700202 spin_lock_irqsave(&mp->ackqlock, flags);
203 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
204 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 kfree(p);
206 mp->nack--;
207 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700208 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211
212/* -------- struct capiminor ---------------------------------------- */
213
214static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
215{
216 struct capiminor *mp, *p;
217 unsigned int minor = 0;
218 unsigned long flags;
219
Burman Yan41f96932006-12-08 02:39:35 -0800220 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (!mp) {
222 printk(KERN_ERR "capi: can't alloc capiminor\n");
223 return NULL;
224 }
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 mp->ap = ap;
227 mp->ncci = ncci;
228 mp->msgid = 0;
229 atomic_set(&mp->ttyopencount,0);
Michael Buesch6aa65472006-06-26 00:25:30 -0700230 INIT_LIST_HEAD(&mp->ackqueue);
231 spin_lock_init(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 skb_queue_head_init(&mp->inqueue);
234 skb_queue_head_init(&mp->outqueue);
235
236 /* Allocate the least unused minor number.
237 */
238 write_lock_irqsave(&capiminor_list_lock, flags);
239 if (list_empty(&capiminor_list))
240 list_add(&mp->list, &capiminor_list);
241 else {
242 list_for_each_entry(p, &capiminor_list, list) {
243 if (p->minor > minor)
244 break;
245 minor++;
246 }
247
248 if (minor < capi_ttyminors) {
249 mp->minor = minor;
250 list_add(&mp->list, p->list.prev);
251 }
252 }
253 write_unlock_irqrestore(&capiminor_list_lock, flags);
254
255 if (!(minor < capi_ttyminors)) {
256 printk(KERN_NOTICE "capi: out of minors\n");
257 kfree(mp);
258 return NULL;
259 }
260
261 return mp;
262}
263
264static void capiminor_free(struct capiminor *mp)
265{
266 unsigned long flags;
267
268 write_lock_irqsave(&capiminor_list_lock, flags);
269 list_del(&mp->list);
270 write_unlock_irqrestore(&capiminor_list_lock, flags);
271
Wei Yongjund46604e2009-02-25 00:12:09 +0000272 kfree_skb(mp->ttyskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 mp->ttyskb = NULL;
274 skb_queue_purge(&mp->inqueue);
275 skb_queue_purge(&mp->outqueue);
276 capiminor_del_all_ack(mp);
277 kfree(mp);
278}
279
Adrian Bunk408b6642005-05-01 08:59:29 -0700280static struct capiminor *capiminor_find(unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 struct list_head *l;
283 struct capiminor *p = NULL;
284
285 read_lock(&capiminor_list_lock);
286 list_for_each(l, &capiminor_list) {
287 p = list_entry(l, struct capiminor, list);
288 if (p->minor == minor)
289 break;
290 }
291 read_unlock(&capiminor_list_lock);
292 if (l == &capiminor_list)
293 return NULL;
294
295 return p;
296}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298/* -------- struct capincci ----------------------------------------- */
299
Jan Kiszka501c87a2010-02-08 10:12:16 +0000300static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Jan Kiszka501c87a2010-02-08 10:12:16 +0000302 struct capiminor *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Jan Kiszka501c87a2010-02-08 10:12:16 +0000304 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
305 return;
306
307 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (mp) {
309 mp->nccip = np;
310#ifdef _DEBUG_REFCOUNT
311 printk(KERN_DEBUG "set mp->nccip\n");
312#endif
Jan Kiszka90926f02010-02-08 10:12:06 +0000313 mp->capifs_dentry =
314 capifs_new_ncci(mp->minor,
315 MKDEV(capi_ttymajor, mp->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000317}
318
319static void capincci_free_minor(struct capincci *np)
320{
321 struct capiminor *mp = np->minorp;
322
323 if (mp) {
324 capifs_free_ncci(mp->capifs_dentry);
325 if (mp->tty) {
326 mp->nccip = NULL;
327#ifdef _DEBUG_REFCOUNT
328 printk(KERN_DEBUG "reset mp->nccip\n");
329#endif
330 tty_hangup(mp->tty);
331 } else {
332 capiminor_free(mp);
333 }
334 }
335}
336
337static inline unsigned int capincci_minor_opencount(struct capincci *np)
338{
339 struct capiminor *mp = np->minorp;
340
341 return mp ? atomic_read(&mp->ttyopencount) : 0;
342}
343
344#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
345
346static inline void
347capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
348static inline void capincci_free_minor(struct capincci *np) { }
349
350static inline unsigned int capincci_minor_opencount(struct capincci *np)
351{
352 return 0;
353}
354
355#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
356
357static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
358{
359 struct capincci *np, **pp;
360
361 np = kzalloc(sizeof(*np), GFP_ATOMIC);
362 if (!np)
363 return NULL;
364 np->ncci = ncci;
365 np->cdev = cdev;
366
367 capincci_alloc_minor(cdev, np);
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 for (pp=&cdev->nccis; *pp; pp = &(*pp)->next)
370 ;
371 *pp = np;
372 return np;
373}
374
375static void capincci_free(struct capidev *cdev, u32 ncci)
376{
377 struct capincci *np, **pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 pp=&cdev->nccis;
380 while (*pp) {
381 np = *pp;
382 if (ncci == 0xffffffff || np->ncci == ncci) {
383 *pp = (*pp)->next;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000384 capincci_free_minor(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 kfree(np);
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700386 if (*pp == NULL) return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 } else {
388 pp = &(*pp)->next;
389 }
390 }
391}
392
393static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
394{
395 struct capincci *p;
396
397 for (p=cdev->nccis; p ; p = p->next) {
398 if (p->ncci == ncci)
399 break;
400 }
401 return p;
402}
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
405/* -------- handle data queue --------------------------------------- */
406
407static struct sk_buff *
408gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
409{
410 struct sk_buff *nskb;
411 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
412 if (nskb) {
413 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
414 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
415 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
416 capimsg_setu16(s, 2, mp->ap->applid);
417 capimsg_setu8 (s, 4, CAPI_DATA_B3);
418 capimsg_setu8 (s, 5, CAPI_RESP);
419 capimsg_setu16(s, 6, mp->msgid++);
420 capimsg_setu32(s, 8, mp->ncci);
421 capimsg_setu16(s, 12, datahandle);
422 }
423 return nskb;
424}
425
426static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
427{
428 struct sk_buff *nskb;
429 int datalen;
430 u16 errcode, datahandle;
431 struct tty_ldisc *ld;
432
433 datalen = skb->len - CAPIMSG_LEN(skb->data);
434 if (mp->tty == NULL)
435 {
436#ifdef _DEBUG_DATAFLOW
437 printk(KERN_DEBUG "capi: currently no receiver\n");
438#endif
439 return -1;
440 }
441
442 ld = tty_ldisc_ref(mp->tty);
443 if (ld == NULL)
444 return -1;
Alan Coxa352def2008-07-16 21:53:12 +0100445 if (ld->ops->receive_buf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
447 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
448#endif
449 goto bad;
450 }
451 if (mp->ttyinstop) {
452#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
453 printk(KERN_DEBUG "capi: recv tty throttled\n");
454#endif
455 goto bad;
456 }
Alan Cox33f0f882006-01-09 20:54:13 -0800457 if (mp->tty->receive_room < datalen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
459 printk(KERN_DEBUG "capi: no room in tty\n");
460#endif
461 goto bad;
462 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700463 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
465 goto bad;
466 }
467 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
468 errcode = capi20_put_message(mp->ap, nskb);
469 if (errcode != CAPI_NOERROR) {
470 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
471 errcode);
472 kfree_skb(nskb);
473 goto bad;
474 }
475 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
476#ifdef _DEBUG_DATAFLOW
477 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
478 datahandle, skb->len);
479#endif
Alan Coxa352def2008-07-16 21:53:12 +0100480 ld->ops->receive_buf(mp->tty, skb->data, NULL, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 kfree_skb(skb);
482 tty_ldisc_deref(ld);
483 return 0;
484bad:
485 tty_ldisc_deref(ld);
486 return -1;
487}
488
489static void handle_minor_recv(struct capiminor *mp)
490{
491 struct sk_buff *skb;
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700492 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 unsigned int len = skb->len;
494 mp->inbytes -= len;
495 if (handle_recv_skb(mp, skb) < 0) {
496 skb_queue_head(&mp->inqueue, skb);
497 mp->inbytes += len;
498 return;
499 }
500 }
501}
502
503static int handle_minor_send(struct capiminor *mp)
504{
505 struct sk_buff *skb;
506 u16 len;
507 int count = 0;
508 u16 errcode;
509 u16 datahandle;
510
511 if (mp->tty && mp->ttyoutstop) {
512#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
513 printk(KERN_DEBUG "capi: send: tty stopped\n");
514#endif
515 return 0;
516 }
517
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700518 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 datahandle = mp->datahandle;
520 len = (u16)skb->len;
521 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
522 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
523 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
524 capimsg_setu16(skb->data, 2, mp->ap->applid);
525 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
526 capimsg_setu8 (skb->data, 5, CAPI_REQ);
527 capimsg_setu16(skb->data, 6, mp->msgid++);
528 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
Andrew Morton5e6c20a2007-07-17 04:04:22 -0700529 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 capimsg_setu16(skb->data, 16, len); /* Data length */
531 capimsg_setu16(skb->data, 18, datahandle);
532 capimsg_setu16(skb->data, 20, 0); /* Flags */
533
Jan Kiszka501c87a2010-02-08 10:12:16 +0000534 if (capiminor_add_ack(mp, datahandle) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
536 skb_queue_head(&mp->outqueue, skb);
537 return count;
538 }
539 errcode = capi20_put_message(mp->ap, skb);
540 if (errcode == CAPI_NOERROR) {
541 mp->datahandle++;
542 count++;
543 mp->outbytes -= len;
544#ifdef _DEBUG_DATAFLOW
545 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
546 datahandle, len);
547#endif
548 continue;
549 }
550 capiminor_del_ack(mp, datahandle);
551
552 if (errcode == CAPI_SENDQUEUEFULL) {
553 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
554 skb_queue_head(&mp->outqueue, skb);
555 break;
556 }
557
558 /* ups, drop packet */
559 printk(KERN_ERR "capi: put_message = %x\n", errcode);
560 mp->outbytes -= len;
561 kfree_skb(skb);
562 }
563 return count;
564}
565
566#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
567/* -------- function called by lower level -------------------------- */
568
569static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
570{
571 struct capidev *cdev = ap->private;
572#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
573 struct capiminor *mp;
574 u16 datahandle;
575#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
576 struct capincci *np;
Michael Buesch053b47f2007-02-12 00:53:26 -0800577 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Jan Kiszka05b41492010-02-08 10:12:19 +0000579 mutex_lock(&cdev->lock);
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
582 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
Jan Kiszka05b41492010-02-08 10:12:19 +0000583 if ((info & 0xff00) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000586 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000588
Michael Buesch053b47f2007-02-12 00:53:26 -0800589 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
591 skb_queue_tail(&cdev->recvqueue, skb);
592 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000593 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000595
596 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (!np) {
598 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
599 skb_queue_tail(&cdev->recvqueue, skb);
600 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000601 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
605 skb_queue_tail(&cdev->recvqueue, skb);
606 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 mp = np->minorp;
611 if (!mp) {
612 skb_queue_tail(&cdev->recvqueue, skb);
613 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000614 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
618#ifdef _DEBUG_DATAFLOW
619 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
620 datahandle, skb->len-CAPIMSG_LEN(skb->data));
621#endif
622 skb_queue_tail(&mp->inqueue, skb);
623 mp->inbytes += skb->len;
624 handle_minor_recv(mp);
625
626 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
627
628 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
629#ifdef _DEBUG_DATAFLOW
630 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
631 datahandle,
632 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
633#endif
634 kfree_skb(skb);
635 (void)capiminor_del_ack(mp, datahandle);
636 if (mp->tty)
637 tty_wakeup(mp->tty);
638 (void)handle_minor_send(mp);
639
640 } else {
641 /* ups, let capi application handle it :-) */
642 skb_queue_tail(&cdev->recvqueue, skb);
643 wake_up_interruptible(&cdev->recvwait);
644 }
645#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000646
Jan Kiszka05b41492010-02-08 10:12:19 +0000647unlock_out:
Michael Buesch053b47f2007-02-12 00:53:26 -0800648 spin_unlock_irqrestore(&workaround_lock, flags);
Jan Kiszka05b41492010-02-08 10:12:19 +0000649 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652/* -------- file_operations for capidev ----------------------------- */
653
654static ssize_t
655capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
656{
657 struct capidev *cdev = (struct capidev *)file->private_data;
658 struct sk_buff *skb;
659 size_t copied;
660
661 if (!cdev->ap.applid)
662 return -ENODEV;
663
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700664 if ((skb = skb_dequeue(&cdev->recvqueue)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 if (file->f_flags & O_NONBLOCK)
667 return -EAGAIN;
668
669 for (;;) {
670 interruptible_sleep_on(&cdev->recvwait);
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700671 if ((skb = skb_dequeue(&cdev->recvqueue)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 break;
673 if (signal_pending(current))
674 break;
675 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700676 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -ERESTARTNOHAND;
678 }
679 if (skb->len > count) {
680 skb_queue_head(&cdev->recvqueue, skb);
681 return -EMSGSIZE;
682 }
683 if (copy_to_user(buf, skb->data, skb->len)) {
684 skb_queue_head(&cdev->recvqueue, skb);
685 return -EFAULT;
686 }
687 copied = skb->len;
688
689 kfree_skb(skb);
690
691 return copied;
692}
693
694static ssize_t
695capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
696{
697 struct capidev *cdev = (struct capidev *)file->private_data;
698 struct sk_buff *skb;
699 u16 mlen;
700
701 if (!cdev->ap.applid)
702 return -ENODEV;
703
704 skb = alloc_skb(count, GFP_USER);
705 if (!skb)
706 return -ENOMEM;
707
708 if (copy_from_user(skb_put(skb, count), buf, count)) {
709 kfree_skb(skb);
710 return -EFAULT;
711 }
712 mlen = CAPIMSG_LEN(skb->data);
713 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
714 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
715 kfree_skb(skb);
716 return -EINVAL;
717 }
718 } else {
719 if (mlen != count) {
720 kfree_skb(skb);
721 return -EINVAL;
722 }
723 }
724 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
725
726 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Jan Kiszka05b41492010-02-08 10:12:19 +0000727 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000729 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731
732 cdev->errcode = capi20_put_message(&cdev->ap, skb);
733
734 if (cdev->errcode) {
735 kfree_skb(skb);
736 return -EIO;
737 }
738 return count;
739}
740
741static unsigned int
742capi_poll(struct file *file, poll_table * wait)
743{
744 struct capidev *cdev = (struct capidev *)file->private_data;
745 unsigned int mask = 0;
746
747 if (!cdev->ap.applid)
748 return POLLERR;
749
750 poll_wait(file, &(cdev->recvwait), wait);
751 mask = POLLOUT | POLLWRNORM;
752 if (!skb_queue_empty(&cdev->recvqueue))
753 mask |= POLLIN | POLLRDNORM;
754 return mask;
755}
756
757static int
758capi_ioctl(struct inode *inode, struct file *file,
759 unsigned int cmd, unsigned long arg)
760{
761 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 capi_ioctl_struct data;
763 int retval = -EINVAL;
764 void __user *argp = (void __user *)arg;
765
766 switch (cmd) {
767 case CAPI_REGISTER:
Jan Kiszka05b41492010-02-08 10:12:19 +0000768 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Jan Kiszka05b41492010-02-08 10:12:19 +0000770 if (cdev->ap.applid) {
771 retval = -EEXIST;
772 goto register_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000774 if (copy_from_user(&cdev->ap.rparam, argp,
775 sizeof(struct capi_register_params))) {
776 retval = -EFAULT;
777 goto register_out;
778 }
779 cdev->ap.private = cdev;
780 cdev->ap.recv_message = capi_recv_message;
781 cdev->errcode = capi20_register(&cdev->ap);
782 retval = (int)cdev->ap.applid;
783 if (cdev->errcode) {
784 cdev->ap.applid = 0;
785 retval = -EIO;
786 }
787
788register_out:
789 mutex_unlock(&cdev->lock);
790 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 case CAPI_GET_VERSION:
793 {
794 if (copy_from_user(&data.contr, argp,
795 sizeof(data.contr)))
796 return -EFAULT;
797 cdev->errcode = capi20_get_version(data.contr, &data.version);
798 if (cdev->errcode)
799 return -EIO;
800 if (copy_to_user(argp, &data.version,
801 sizeof(data.version)))
802 return -EFAULT;
803 }
804 return 0;
805
806 case CAPI_GET_SERIAL:
807 {
808 if (copy_from_user(&data.contr, argp,
809 sizeof(data.contr)))
810 return -EFAULT;
811 cdev->errcode = capi20_get_serial (data.contr, data.serial);
812 if (cdev->errcode)
813 return -EIO;
814 if (copy_to_user(argp, data.serial,
815 sizeof(data.serial)))
816 return -EFAULT;
817 }
818 return 0;
819 case CAPI_GET_PROFILE:
820 {
821 if (copy_from_user(&data.contr, argp,
822 sizeof(data.contr)))
823 return -EFAULT;
824
825 if (data.contr == 0) {
826 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
827 if (cdev->errcode)
828 return -EIO;
829
830 retval = copy_to_user(argp,
831 &data.profile.ncontroller,
832 sizeof(data.profile.ncontroller));
833
834 } else {
835 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
836 if (cdev->errcode)
837 return -EIO;
838
839 retval = copy_to_user(argp, &data.profile,
840 sizeof(data.profile));
841 }
842 if (retval)
843 return -EFAULT;
844 }
845 return 0;
846
847 case CAPI_GET_MANUFACTURER:
848 {
849 if (copy_from_user(&data.contr, argp,
850 sizeof(data.contr)))
851 return -EFAULT;
852 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
853 if (cdev->errcode)
854 return -EIO;
855
856 if (copy_to_user(argp, data.manufacturer,
857 sizeof(data.manufacturer)))
858 return -EFAULT;
859
860 }
861 return 0;
862 case CAPI_GET_ERRCODE:
863 data.errcode = cdev->errcode;
864 cdev->errcode = CAPI_NOERROR;
865 if (arg) {
866 if (copy_to_user(argp, &data.errcode,
867 sizeof(data.errcode)))
868 return -EFAULT;
869 }
870 return data.errcode;
871
872 case CAPI_INSTALLED:
873 if (capi20_isinstalled() == CAPI_NOERROR)
874 return 0;
875 return -ENXIO;
876
877 case CAPI_MANUFACTURER_CMD:
878 {
879 struct capi_manufacturer_cmd mcmd;
880 if (!capable(CAP_SYS_ADMIN))
881 return -EPERM;
882 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
883 return -EFAULT;
884 return capi20_manufacturer(mcmd.cmd, mcmd.data);
885 }
886 return 0;
887
888 case CAPI_SET_FLAGS:
Jan Kiszka05b41492010-02-08 10:12:19 +0000889 case CAPI_CLR_FLAGS: {
890 unsigned userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Jan Kiszka05b41492010-02-08 10:12:19 +0000892 if (copy_from_user(&userflags, argp, sizeof(userflags)))
893 return -EFAULT;
894
895 mutex_lock(&cdev->lock);
896 if (cmd == CAPI_SET_FLAGS)
897 cdev->userflags |= userflags;
898 else
899 cdev->userflags &= ~userflags;
900 mutex_unlock(&cdev->lock);
901 return 0;
902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 case CAPI_GET_FLAGS:
904 if (copy_to_user(argp, &cdev->userflags,
905 sizeof(cdev->userflags)))
906 return -EFAULT;
907 return 0;
908
Jan Kiszka05b41492010-02-08 10:12:19 +0000909 case CAPI_NCCI_OPENCOUNT: {
910 struct capincci *nccip;
911 unsigned ncci;
912 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Jan Kiszka05b41492010-02-08 10:12:19 +0000914 if (copy_from_user(&ncci, argp, sizeof(ncci)))
915 return -EFAULT;
916
917 mutex_lock(&cdev->lock);
918 nccip = capincci_find(cdev, (u32)ncci);
919 if (nccip)
920 count = capincci_minor_opencount(nccip);
921 mutex_unlock(&cdev->lock);
922 return count;
923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka05b41492010-02-08 10:12:19 +0000926 case CAPI_NCCI_GETUNIT: {
927 struct capincci *nccip;
928 struct capiminor *mp;
929 unsigned ncci;
930 int unit = -ESRCH;
931
932 if (copy_from_user(&ncci, argp, sizeof(ncci)))
933 return -EFAULT;
934
935 mutex_lock(&cdev->lock);
936 nccip = capincci_find(cdev, (u32)ncci);
937 if (nccip) {
938 mp = nccip->minorp;
939 if (mp)
940 unit = mp->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000942 mutex_unlock(&cdev->lock);
943 return unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000945#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
946
947 default:
948 return -EINVAL;
949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000952static int capi_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000954 struct capidev *cdev;
955
956 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
957 if (!cdev)
958 return -ENOMEM;
959
Jan Kiszka05b41492010-02-08 10:12:19 +0000960 mutex_init(&cdev->lock);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000961 skb_queue_head_init(&cdev->recvqueue);
962 init_waitqueue_head(&cdev->recvwait);
963 file->private_data = cdev;
964
965 mutex_lock(&capidev_list_lock);
966 list_add_tail(&cdev->list, &capidev_list);
967 mutex_unlock(&capidev_list_lock);
968
969 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000972static int capi_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000974 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000976 mutex_lock(&capidev_list_lock);
977 list_del(&cdev->list);
978 mutex_unlock(&capidev_list_lock);
979
Jan Kiszka05b41492010-02-08 10:12:19 +0000980 if (cdev->ap.applid)
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000981 capi20_release(&cdev->ap);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000982 skb_queue_purge(&cdev->recvqueue);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000983 capincci_free(cdev, 0xffffffff);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000984
985 kfree(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return 0;
987}
988
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800989static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 .owner = THIS_MODULE,
992 .llseek = no_llseek,
993 .read = capi_read,
994 .write = capi_write,
995 .poll = capi_poll,
996 .ioctl = capi_ioctl,
997 .open = capi_open,
998 .release = capi_release,
999};
1000
1001#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1002/* -------- tty_operations for capincci ----------------------------- */
1003
1004static int capinc_tty_open(struct tty_struct * tty, struct file * file)
1005{
1006 struct capiminor *mp;
Michael Buesch053b47f2007-02-12 00:53:26 -08001007 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001009 if ((mp = capiminor_find(iminor(file->f_path.dentry->d_inode))) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return -ENXIO;
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001011 if (mp->nccip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return -ENXIO;
1013
1014 tty->driver_data = (void *)mp;
1015
Michael Buesch053b47f2007-02-12 00:53:26 -08001016 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (atomic_read(&mp->ttyopencount) == 0)
1018 mp->tty = tty;
1019 atomic_inc(&mp->ttyopencount);
1020#ifdef _DEBUG_REFCOUNT
1021 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1022#endif
1023 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001024 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return 0;
1026}
1027
1028static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1029{
1030 struct capiminor *mp;
1031
1032 mp = (struct capiminor *)tty->driver_data;
1033 if (mp) {
1034 if (atomic_dec_and_test(&mp->ttyopencount)) {
1035#ifdef _DEBUG_REFCOUNT
1036 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1037#endif
1038 tty->driver_data = NULL;
1039 mp->tty = NULL;
1040 }
1041#ifdef _DEBUG_REFCOUNT
1042 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1043#endif
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001044 if (mp->nccip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 capiminor_free(mp);
1046 }
1047
1048#ifdef _DEBUG_REFCOUNT
1049 printk(KERN_DEBUG "capinc_tty_close\n");
1050#endif
1051}
1052
1053static int capinc_tty_write(struct tty_struct * tty,
1054 const unsigned char *buf, int count)
1055{
1056 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1057 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001058 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060#ifdef _DEBUG_TTYFUNCS
1061 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1062#endif
1063
1064 if (!mp || !mp->nccip) {
1065#ifdef _DEBUG_TTYFUNCS
1066 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1067#endif
1068 return 0;
1069 }
1070
Michael Buesch053b47f2007-02-12 00:53:26 -08001071 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 skb = mp->ttyskb;
1073 if (skb) {
1074 mp->ttyskb = NULL;
1075 skb_queue_tail(&mp->outqueue, skb);
1076 mp->outbytes += skb->len;
1077 }
1078
1079 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1080 if (!skb) {
1081 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
Michael Buesch053b47f2007-02-12 00:53:26 -08001082 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return -ENOMEM;
1084 }
1085
1086 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1087 memcpy(skb_put(skb, count), buf, count);
1088
1089 skb_queue_tail(&mp->outqueue, skb);
1090 mp->outbytes += skb->len;
1091 (void)handle_minor_send(mp);
1092 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001093 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return count;
1095}
1096
Alan Coxf2545a72008-04-30 00:54:09 -07001097static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
1099 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1100 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001101 unsigned long flags;
Alan Coxf2545a72008-04-30 00:54:09 -07001102 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104#ifdef _DEBUG_TTYFUNCS
1105 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1106#endif
1107
1108 if (!mp || !mp->nccip) {
1109#ifdef _DEBUG_TTYFUNCS
1110 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1111#endif
Alan Coxf2545a72008-04-30 00:54:09 -07001112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
1114
Michael Buesch053b47f2007-02-12 00:53:26 -08001115 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 skb = mp->ttyskb;
1117 if (skb) {
1118 if (skb_tailroom(skb) > 0) {
1119 *(skb_put(skb, 1)) = ch;
Michael Buesch053b47f2007-02-12 00:53:26 -08001120 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001121 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123 mp->ttyskb = NULL;
1124 skb_queue_tail(&mp->outqueue, skb);
1125 mp->outbytes += skb->len;
1126 (void)handle_minor_send(mp);
1127 }
1128 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1129 if (skb) {
1130 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1131 *(skb_put(skb, 1)) = ch;
1132 mp->ttyskb = skb;
1133 } else {
1134 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
Alan Coxf2545a72008-04-30 00:54:09 -07001135 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
Michael Buesch053b47f2007-02-12 00:53:26 -08001137 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001138 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
1141static void capinc_tty_flush_chars(struct tty_struct *tty)
1142{
1143 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1144 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001145 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147#ifdef _DEBUG_TTYFUNCS
1148 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1149#endif
1150
1151 if (!mp || !mp->nccip) {
1152#ifdef _DEBUG_TTYFUNCS
1153 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1154#endif
1155 return;
1156 }
1157
Michael Buesch053b47f2007-02-12 00:53:26 -08001158 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 skb = mp->ttyskb;
1160 if (skb) {
1161 mp->ttyskb = NULL;
1162 skb_queue_tail(&mp->outqueue, skb);
1163 mp->outbytes += skb->len;
1164 (void)handle_minor_send(mp);
1165 }
1166 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001167 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
1170static int capinc_tty_write_room(struct tty_struct *tty)
1171{
1172 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1173 int room;
1174 if (!mp || !mp->nccip) {
1175#ifdef _DEBUG_TTYFUNCS
1176 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1177#endif
1178 return 0;
1179 }
1180 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1181 room *= CAPI_MAX_BLKSIZE;
1182#ifdef _DEBUG_TTYFUNCS
1183 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1184#endif
1185 return room;
1186}
1187
Adrian Bunk408b6642005-05-01 08:59:29 -07001188static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
1190 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1191 if (!mp || !mp->nccip) {
1192#ifdef _DEBUG_TTYFUNCS
1193 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1194#endif
1195 return 0;
1196 }
1197#ifdef _DEBUG_TTYFUNCS
1198 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1199 mp->outbytes, mp->nack,
1200 skb_queue_len(&mp->outqueue),
1201 skb_queue_len(&mp->inqueue));
1202#endif
1203 return mp->outbytes;
1204}
1205
1206static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1207 unsigned int cmd, unsigned long arg)
1208{
1209 int error = 0;
1210 switch (cmd) {
1211 default:
Stephen Rothwell53e86312008-10-13 10:44:33 +01001212 error = n_tty_ioctl_helper(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 break;
1214 }
1215 return error;
1216}
1217
Alan Cox606d0992006-12-08 02:38:45 -08001218static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220#ifdef _DEBUG_TTYFUNCS
1221 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1222#endif
1223}
1224
1225static void capinc_tty_throttle(struct tty_struct * tty)
1226{
1227 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1228#ifdef _DEBUG_TTYFUNCS
1229 printk(KERN_DEBUG "capinc_tty_throttle\n");
1230#endif
1231 if (mp)
1232 mp->ttyinstop = 1;
1233}
1234
1235static void capinc_tty_unthrottle(struct tty_struct * tty)
1236{
1237 struct capiminor *mp = (struct capiminor *)tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001238 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239#ifdef _DEBUG_TTYFUNCS
1240 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1241#endif
1242 if (mp) {
Michael Buesch053b47f2007-02-12 00:53:26 -08001243 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 mp->ttyinstop = 0;
1245 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001246 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248}
1249
1250static void capinc_tty_stop(struct tty_struct *tty)
1251{
1252 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1253#ifdef _DEBUG_TTYFUNCS
1254 printk(KERN_DEBUG "capinc_tty_stop\n");
1255#endif
1256 if (mp) {
1257 mp->ttyoutstop = 1;
1258 }
1259}
1260
1261static void capinc_tty_start(struct tty_struct *tty)
1262{
1263 struct capiminor *mp = (struct capiminor *)tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001264 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265#ifdef _DEBUG_TTYFUNCS
1266 printk(KERN_DEBUG "capinc_tty_start\n");
1267#endif
1268 if (mp) {
Michael Buesch053b47f2007-02-12 00:53:26 -08001269 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 mp->ttyoutstop = 0;
1271 (void)handle_minor_send(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001272 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274}
1275
1276static void capinc_tty_hangup(struct tty_struct *tty)
1277{
1278#ifdef _DEBUG_TTYFUNCS
1279 printk(KERN_DEBUG "capinc_tty_hangup\n");
1280#endif
1281}
1282
Alan Cox9e989662008-07-22 11:18:03 +01001283static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285#ifdef _DEBUG_TTYFUNCS
1286 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1287#endif
Alan Cox9e989662008-07-22 11:18:03 +01001288 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
1291static 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
1298static 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
1305static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312static struct tty_driver *capinc_tty_driver;
1313
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001314static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 .open = capinc_tty_open,
1316 .close = capinc_tty_close,
1317 .write = capinc_tty_write,
1318 .put_char = capinc_tty_put_char,
1319 .flush_chars = capinc_tty_flush_chars,
1320 .write_room = capinc_tty_write_room,
1321 .chars_in_buffer = capinc_tty_chars_in_buffer,
1322 .ioctl = capinc_tty_ioctl,
1323 .set_termios = capinc_tty_set_termios,
1324 .throttle = capinc_tty_throttle,
1325 .unthrottle = capinc_tty_unthrottle,
1326 .stop = capinc_tty_stop,
1327 .start = capinc_tty_start,
1328 .hangup = capinc_tty_hangup,
1329 .break_ctl = capinc_tty_break_ctl,
1330 .flush_buffer = capinc_tty_flush_buffer,
1331 .set_ldisc = capinc_tty_set_ldisc,
1332 .send_xchar = capinc_tty_send_xchar,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333};
1334
1335static int capinc_tty_init(void)
1336{
1337 struct tty_driver *drv;
1338
1339 if (capi_ttyminors > CAPINC_MAX_PORTS)
1340 capi_ttyminors = CAPINC_MAX_PORTS;
1341 if (capi_ttyminors <= 0)
1342 capi_ttyminors = CAPINC_NR_PORTS;
1343
1344 drv = alloc_tty_driver(capi_ttyminors);
1345 if (!drv)
1346 return -ENOMEM;
1347
1348 drv->owner = THIS_MODULE;
1349 drv->driver_name = "capi_nc";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 drv->name = "capi";
1351 drv->major = capi_ttymajor;
1352 drv->minor_start = 0;
1353 drv->type = TTY_DRIVER_TYPE_SERIAL;
1354 drv->subtype = SERIAL_TYPE_NORMAL;
1355 drv->init_termios = tty_std_termios;
1356 drv->init_termios.c_iflag = ICRNL;
1357 drv->init_termios.c_oflag = OPOST | ONLCR;
1358 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1359 drv->init_termios.c_lflag = 0;
1360 drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS;
1361 tty_set_operations(drv, &capinc_ops);
1362 if (tty_register_driver(drv)) {
1363 put_tty_driver(drv);
1364 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1365 return -1;
1366 }
1367 capinc_tty_driver = drv;
1368 return 0;
1369}
1370
1371static void capinc_tty_exit(void)
1372{
1373 struct tty_driver *drv = capinc_tty_driver;
1374 int retval;
1375 if ((retval = tty_unregister_driver(drv)))
1376 printk(KERN_ERR "capi: failed to unregister capi_nc driver (%d)\n", retval);
1377 put_tty_driver(drv);
1378}
1379
Jan Kiszka501c87a2010-02-08 10:12:16 +00001380#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1381
1382static inline int capinc_tty_init(void)
1383{
1384 return 0;
1385}
1386
1387static inline void capinc_tty_exit(void) { }
1388
1389#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391/* -------- /proc functions ----------------------------------------- */
1392
1393/*
1394 * /proc/capi/capi20:
1395 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1396 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001397static int capi20_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 struct capidev *cdev;
1400 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001402 mutex_lock(&capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 list_for_each(l, &capidev_list) {
1404 cdev = list_entry(l, struct capidev, list);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001405 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 cdev->ap.applid,
1407 cdev->ap.nrecvctlpkt,
1408 cdev->ap.nrecvdatapkt,
1409 cdev->ap.nsentctlpkt,
1410 cdev->ap.nsentdatapkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001412 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001416static int capi20_proc_open(struct inode *inode, struct file *file)
1417{
1418 return single_open(file, capi20_proc_show, NULL);
1419}
1420
1421static const struct file_operations capi20_proc_fops = {
1422 .owner = THIS_MODULE,
1423 .open = capi20_proc_open,
1424 .read = seq_read,
1425 .llseek = seq_lseek,
1426 .release = single_release,
1427};
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429/*
1430 * /proc/capi/capi20ncci:
1431 * applid ncci
1432 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001433static int capi20ncci_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
1435 struct capidev *cdev;
1436 struct capincci *np;
1437 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001439 mutex_lock(&capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 list_for_each(l, &capidev_list) {
1441 cdev = list_entry(l, struct capidev, list);
Jan Kiszka05b41492010-02-08 10:12:19 +00001442 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 for (np=cdev->nccis; np; np = np->next) {
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001444 seq_printf(m, "%d 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 cdev->ap.applid,
1446 np->ncci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
Jan Kiszka05b41492010-02-08 10:12:19 +00001448 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001450 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001454static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1455{
1456 return single_open(file, capi20ncci_proc_show, NULL);
1457}
1458
1459static const struct file_operations capi20ncci_proc_fops = {
1460 .owner = THIS_MODULE,
1461 .open = capi20ncci_proc_open,
1462 .read = seq_read,
1463 .llseek = seq_lseek,
1464 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465};
1466
1467static void __init proc_init(void)
1468{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001469 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1470 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
1472
1473static void __exit proc_exit(void)
1474{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001475 remove_proc_entry("capi/capi20", NULL);
1476 remove_proc_entry("capi/capi20ncci", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
1479/* -------- init function and module interface ---------------------- */
1480
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482static int __init capi_init(void)
1483{
Jan Kiszka88549d62010-02-08 10:12:09 +00001484 const char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001485 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Andrew Morton6d9eac32006-03-28 01:56:19 -08001487 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1488 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001490 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001492 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (IS_ERR(capi_class)) {
1494 unregister_chrdev(capi_major, "capi20");
1495 return PTR_ERR(capi_class);
1496 }
1497
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -07001498 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (capinc_tty_init() < 0) {
Tony Jonesd78b0362007-09-25 02:03:03 +02001501 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001502 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 unregister_chrdev(capi_major, "capi20");
1504 return -ENOMEM;
1505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 proc_init();
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1510 compileinfo = " (middleware+capifs)";
Jan Kiszka501c87a2010-02-08 10:12:16 +00001511#elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 compileinfo = " (no capifs)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513#else
1514 compileinfo = " (no middleware)";
1515#endif
Jan Kiszka88549d62010-02-08 10:12:09 +00001516 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1517 capi_major, compileinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 return 0;
1520}
1521
1522static void __exit capi_exit(void)
1523{
1524 proc_exit();
1525
Tony Jonesd78b0362007-09-25 02:03:03 +02001526 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001527 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 capinc_tty_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
1533module_init(capi_init);
1534module_exit(capi_exit);