blob: 6704b2b004aa6e9e027a083220763850feb430cf [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 {
Jan Kiszka884f5c42010-02-08 10:12:22 +0000125 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 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
Jan Kiszka884f5c42010-02-08 10:12:22 +0000142 struct list_head nccis;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
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
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000220 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
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{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000359 struct capincci *np;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000360
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000361 np = kzalloc(sizeof(*np), GFP_KERNEL);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000362 if (!np)
363 return NULL;
364 np->ncci = ncci;
365 np->cdev = cdev;
366
367 capincci_alloc_minor(cdev, np);
368
Jan Kiszka884f5c42010-02-08 10:12:22 +0000369 list_add_tail(&np->list, &cdev->nccis);
370
371 return np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
374static void capincci_free(struct capidev *cdev, u32 ncci)
375{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000376 struct capincci *np, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Jan Kiszka884f5c42010-02-08 10:12:22 +0000378 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (ncci == 0xffffffff || np->ncci == ncci) {
Jan Kiszka501c87a2010-02-08 10:12:16 +0000380 capincci_free_minor(np);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000381 list_del(&np->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 kfree(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
387{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000388 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Jan Kiszka884f5c42010-02-08 10:12:22 +0000390 list_for_each_entry(np, &cdev->nccis, list)
391 if (np->ncci == ncci)
392 return np;
393 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
397/* -------- handle data queue --------------------------------------- */
398
399static struct sk_buff *
400gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
401{
402 struct sk_buff *nskb;
403 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
404 if (nskb) {
405 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
406 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
407 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
408 capimsg_setu16(s, 2, mp->ap->applid);
409 capimsg_setu8 (s, 4, CAPI_DATA_B3);
410 capimsg_setu8 (s, 5, CAPI_RESP);
411 capimsg_setu16(s, 6, mp->msgid++);
412 capimsg_setu32(s, 8, mp->ncci);
413 capimsg_setu16(s, 12, datahandle);
414 }
415 return nskb;
416}
417
418static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
419{
420 struct sk_buff *nskb;
421 int datalen;
422 u16 errcode, datahandle;
423 struct tty_ldisc *ld;
424
425 datalen = skb->len - CAPIMSG_LEN(skb->data);
426 if (mp->tty == NULL)
427 {
428#ifdef _DEBUG_DATAFLOW
429 printk(KERN_DEBUG "capi: currently no receiver\n");
430#endif
431 return -1;
432 }
433
434 ld = tty_ldisc_ref(mp->tty);
435 if (ld == NULL)
436 return -1;
Alan Coxa352def2008-07-16 21:53:12 +0100437 if (ld->ops->receive_buf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
439 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
440#endif
441 goto bad;
442 }
443 if (mp->ttyinstop) {
444#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
445 printk(KERN_DEBUG "capi: recv tty throttled\n");
446#endif
447 goto bad;
448 }
Alan Cox33f0f882006-01-09 20:54:13 -0800449 if (mp->tty->receive_room < datalen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
451 printk(KERN_DEBUG "capi: no room in tty\n");
452#endif
453 goto bad;
454 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700455 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
457 goto bad;
458 }
459 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
460 errcode = capi20_put_message(mp->ap, nskb);
461 if (errcode != CAPI_NOERROR) {
462 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
463 errcode);
464 kfree_skb(nskb);
465 goto bad;
466 }
467 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
468#ifdef _DEBUG_DATAFLOW
469 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
470 datahandle, skb->len);
471#endif
Alan Coxa352def2008-07-16 21:53:12 +0100472 ld->ops->receive_buf(mp->tty, skb->data, NULL, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 kfree_skb(skb);
474 tty_ldisc_deref(ld);
475 return 0;
476bad:
477 tty_ldisc_deref(ld);
478 return -1;
479}
480
481static void handle_minor_recv(struct capiminor *mp)
482{
483 struct sk_buff *skb;
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700484 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 unsigned int len = skb->len;
486 mp->inbytes -= len;
487 if (handle_recv_skb(mp, skb) < 0) {
488 skb_queue_head(&mp->inqueue, skb);
489 mp->inbytes += len;
490 return;
491 }
492 }
493}
494
495static int handle_minor_send(struct capiminor *mp)
496{
497 struct sk_buff *skb;
498 u16 len;
499 int count = 0;
500 u16 errcode;
501 u16 datahandle;
502
503 if (mp->tty && mp->ttyoutstop) {
504#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
505 printk(KERN_DEBUG "capi: send: tty stopped\n");
506#endif
507 return 0;
508 }
509
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700510 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 datahandle = mp->datahandle;
512 len = (u16)skb->len;
513 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
514 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
515 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
516 capimsg_setu16(skb->data, 2, mp->ap->applid);
517 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
518 capimsg_setu8 (skb->data, 5, CAPI_REQ);
519 capimsg_setu16(skb->data, 6, mp->msgid++);
520 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
Andrew Morton5e6c20a2007-07-17 04:04:22 -0700521 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 capimsg_setu16(skb->data, 16, len); /* Data length */
523 capimsg_setu16(skb->data, 18, datahandle);
524 capimsg_setu16(skb->data, 20, 0); /* Flags */
525
Jan Kiszka501c87a2010-02-08 10:12:16 +0000526 if (capiminor_add_ack(mp, datahandle) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
528 skb_queue_head(&mp->outqueue, skb);
529 return count;
530 }
531 errcode = capi20_put_message(mp->ap, skb);
532 if (errcode == CAPI_NOERROR) {
533 mp->datahandle++;
534 count++;
535 mp->outbytes -= len;
536#ifdef _DEBUG_DATAFLOW
537 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
538 datahandle, len);
539#endif
540 continue;
541 }
542 capiminor_del_ack(mp, datahandle);
543
544 if (errcode == CAPI_SENDQUEUEFULL) {
545 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
546 skb_queue_head(&mp->outqueue, skb);
547 break;
548 }
549
550 /* ups, drop packet */
551 printk(KERN_ERR "capi: put_message = %x\n", errcode);
552 mp->outbytes -= len;
553 kfree_skb(skb);
554 }
555 return count;
556}
557
558#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
559/* -------- function called by lower level -------------------------- */
560
561static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
562{
563 struct capidev *cdev = ap->private;
564#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
565 struct capiminor *mp;
566 u16 datahandle;
567#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
568 struct capincci *np;
Michael Buesch053b47f2007-02-12 00:53:26 -0800569 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Jan Kiszka05b41492010-02-08 10:12:19 +0000571 mutex_lock(&cdev->lock);
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
574 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
Jan Kiszka05b41492010-02-08 10:12:19 +0000575 if ((info & 0xff00) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000578 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000580
Michael Buesch053b47f2007-02-12 00:53:26 -0800581 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
583 skb_queue_tail(&cdev->recvqueue, skb);
584 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000585 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000587
588 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (!np) {
590 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
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 Kiszka501c87a2010-02-08 10:12:16 +0000595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
597 skb_queue_tail(&cdev->recvqueue, skb);
598 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 mp = np->minorp;
603 if (!mp) {
604 skb_queue_tail(&cdev->recvqueue, skb);
605 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000606 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
610#ifdef _DEBUG_DATAFLOW
611 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
612 datahandle, skb->len-CAPIMSG_LEN(skb->data));
613#endif
614 skb_queue_tail(&mp->inqueue, skb);
615 mp->inbytes += skb->len;
616 handle_minor_recv(mp);
617
618 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
619
620 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
621#ifdef _DEBUG_DATAFLOW
622 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
623 datahandle,
624 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
625#endif
626 kfree_skb(skb);
627 (void)capiminor_del_ack(mp, datahandle);
628 if (mp->tty)
629 tty_wakeup(mp->tty);
630 (void)handle_minor_send(mp);
631
632 } else {
633 /* ups, let capi application handle it :-) */
634 skb_queue_tail(&cdev->recvqueue, skb);
635 wake_up_interruptible(&cdev->recvwait);
636 }
637#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000638
Jan Kiszka05b41492010-02-08 10:12:19 +0000639unlock_out:
Michael Buesch053b47f2007-02-12 00:53:26 -0800640 spin_unlock_irqrestore(&workaround_lock, flags);
Jan Kiszka05b41492010-02-08 10:12:19 +0000641 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644/* -------- file_operations for capidev ----------------------------- */
645
646static ssize_t
647capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
648{
649 struct capidev *cdev = (struct capidev *)file->private_data;
650 struct sk_buff *skb;
651 size_t copied;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000652 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 if (!cdev->ap.applid)
655 return -ENODEV;
656
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000657 skb = skb_dequeue(&cdev->recvqueue);
658 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (file->f_flags & O_NONBLOCK)
660 return -EAGAIN;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000661 err = wait_event_interruptible(cdev->recvwait,
662 (skb = skb_dequeue(&cdev->recvqueue)));
663 if (err)
664 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666 if (skb->len > count) {
667 skb_queue_head(&cdev->recvqueue, skb);
668 return -EMSGSIZE;
669 }
670 if (copy_to_user(buf, skb->data, skb->len)) {
671 skb_queue_head(&cdev->recvqueue, skb);
672 return -EFAULT;
673 }
674 copied = skb->len;
675
676 kfree_skb(skb);
677
678 return copied;
679}
680
681static ssize_t
682capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
683{
684 struct capidev *cdev = (struct capidev *)file->private_data;
685 struct sk_buff *skb;
686 u16 mlen;
687
688 if (!cdev->ap.applid)
689 return -ENODEV;
690
691 skb = alloc_skb(count, GFP_USER);
692 if (!skb)
693 return -ENOMEM;
694
695 if (copy_from_user(skb_put(skb, count), buf, count)) {
696 kfree_skb(skb);
697 return -EFAULT;
698 }
699 mlen = CAPIMSG_LEN(skb->data);
700 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
701 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
702 kfree_skb(skb);
703 return -EINVAL;
704 }
705 } else {
706 if (mlen != count) {
707 kfree_skb(skb);
708 return -EINVAL;
709 }
710 }
711 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
712
713 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Jan Kiszka05b41492010-02-08 10:12:19 +0000714 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000716 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718
719 cdev->errcode = capi20_put_message(&cdev->ap, skb);
720
721 if (cdev->errcode) {
722 kfree_skb(skb);
723 return -EIO;
724 }
725 return count;
726}
727
728static unsigned int
729capi_poll(struct file *file, poll_table * wait)
730{
731 struct capidev *cdev = (struct capidev *)file->private_data;
732 unsigned int mask = 0;
733
734 if (!cdev->ap.applid)
735 return POLLERR;
736
737 poll_wait(file, &(cdev->recvwait), wait);
738 mask = POLLOUT | POLLWRNORM;
739 if (!skb_queue_empty(&cdev->recvqueue))
740 mask |= POLLIN | POLLRDNORM;
741 return mask;
742}
743
744static int
745capi_ioctl(struct inode *inode, struct file *file,
746 unsigned int cmd, unsigned long arg)
747{
748 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 capi_ioctl_struct data;
750 int retval = -EINVAL;
751 void __user *argp = (void __user *)arg;
752
753 switch (cmd) {
754 case CAPI_REGISTER:
Jan Kiszka05b41492010-02-08 10:12:19 +0000755 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Jan Kiszka05b41492010-02-08 10:12:19 +0000757 if (cdev->ap.applid) {
758 retval = -EEXIST;
759 goto register_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000761 if (copy_from_user(&cdev->ap.rparam, argp,
762 sizeof(struct capi_register_params))) {
763 retval = -EFAULT;
764 goto register_out;
765 }
766 cdev->ap.private = cdev;
767 cdev->ap.recv_message = capi_recv_message;
768 cdev->errcode = capi20_register(&cdev->ap);
769 retval = (int)cdev->ap.applid;
770 if (cdev->errcode) {
771 cdev->ap.applid = 0;
772 retval = -EIO;
773 }
774
775register_out:
776 mutex_unlock(&cdev->lock);
777 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 case CAPI_GET_VERSION:
780 {
781 if (copy_from_user(&data.contr, argp,
782 sizeof(data.contr)))
783 return -EFAULT;
784 cdev->errcode = capi20_get_version(data.contr, &data.version);
785 if (cdev->errcode)
786 return -EIO;
787 if (copy_to_user(argp, &data.version,
788 sizeof(data.version)))
789 return -EFAULT;
790 }
791 return 0;
792
793 case CAPI_GET_SERIAL:
794 {
795 if (copy_from_user(&data.contr, argp,
796 sizeof(data.contr)))
797 return -EFAULT;
798 cdev->errcode = capi20_get_serial (data.contr, data.serial);
799 if (cdev->errcode)
800 return -EIO;
801 if (copy_to_user(argp, data.serial,
802 sizeof(data.serial)))
803 return -EFAULT;
804 }
805 return 0;
806 case CAPI_GET_PROFILE:
807 {
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 }
832 return 0;
833
834 case CAPI_GET_MANUFACTURER:
835 {
836 if (copy_from_user(&data.contr, argp,
837 sizeof(data.contr)))
838 return -EFAULT;
839 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
840 if (cdev->errcode)
841 return -EIO;
842
843 if (copy_to_user(argp, data.manufacturer,
844 sizeof(data.manufacturer)))
845 return -EFAULT;
846
847 }
848 return 0;
849 case CAPI_GET_ERRCODE:
850 data.errcode = cdev->errcode;
851 cdev->errcode = CAPI_NOERROR;
852 if (arg) {
853 if (copy_to_user(argp, &data.errcode,
854 sizeof(data.errcode)))
855 return -EFAULT;
856 }
857 return data.errcode;
858
859 case CAPI_INSTALLED:
860 if (capi20_isinstalled() == CAPI_NOERROR)
861 return 0;
862 return -ENXIO;
863
864 case CAPI_MANUFACTURER_CMD:
865 {
866 struct capi_manufacturer_cmd mcmd;
867 if (!capable(CAP_SYS_ADMIN))
868 return -EPERM;
869 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
870 return -EFAULT;
871 return capi20_manufacturer(mcmd.cmd, mcmd.data);
872 }
873 return 0;
874
875 case CAPI_SET_FLAGS:
Jan Kiszka05b41492010-02-08 10:12:19 +0000876 case CAPI_CLR_FLAGS: {
877 unsigned userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Jan Kiszka05b41492010-02-08 10:12:19 +0000879 if (copy_from_user(&userflags, argp, sizeof(userflags)))
880 return -EFAULT;
881
882 mutex_lock(&cdev->lock);
883 if (cmd == CAPI_SET_FLAGS)
884 cdev->userflags |= userflags;
885 else
886 cdev->userflags &= ~userflags;
887 mutex_unlock(&cdev->lock);
888 return 0;
889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 case CAPI_GET_FLAGS:
891 if (copy_to_user(argp, &cdev->userflags,
892 sizeof(cdev->userflags)))
893 return -EFAULT;
894 return 0;
895
Jan Kiszka05b41492010-02-08 10:12:19 +0000896 case CAPI_NCCI_OPENCOUNT: {
897 struct capincci *nccip;
898 unsigned ncci;
899 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Jan Kiszka05b41492010-02-08 10:12:19 +0000901 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka05b41492010-02-08 10:12:19 +0000913 case CAPI_NCCI_GETUNIT: {
914 struct capincci *nccip;
915 struct capiminor *mp;
916 unsigned ncci;
917 int unit = -ESRCH;
918
919 if (copy_from_user(&ncci, argp, sizeof(ncci)))
920 return -EFAULT;
921
922 mutex_lock(&cdev->lock);
923 nccip = capincci_find(cdev, (u32)ncci);
924 if (nccip) {
925 mp = nccip->minorp;
926 if (mp)
927 unit = mp->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000929 mutex_unlock(&cdev->lock);
930 return unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000932#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
933
934 default:
935 return -EINVAL;
936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000939static int capi_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000941 struct capidev *cdev;
942
943 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
944 if (!cdev)
945 return -ENOMEM;
946
Jan Kiszka05b41492010-02-08 10:12:19 +0000947 mutex_init(&cdev->lock);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000948 skb_queue_head_init(&cdev->recvqueue);
949 init_waitqueue_head(&cdev->recvwait);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000950 INIT_LIST_HEAD(&cdev->nccis);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000951 file->private_data = cdev;
952
953 mutex_lock(&capidev_list_lock);
954 list_add_tail(&cdev->list, &capidev_list);
955 mutex_unlock(&capidev_list_lock);
956
957 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000960static int capi_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000962 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000964 mutex_lock(&capidev_list_lock);
965 list_del(&cdev->list);
966 mutex_unlock(&capidev_list_lock);
967
Jan Kiszka05b41492010-02-08 10:12:19 +0000968 if (cdev->ap.applid)
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000969 capi20_release(&cdev->ap);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000970 skb_queue_purge(&cdev->recvqueue);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000971 capincci_free(cdev, 0xffffffff);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000972
973 kfree(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return 0;
975}
976
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800977static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 .owner = THIS_MODULE,
980 .llseek = no_llseek,
981 .read = capi_read,
982 .write = capi_write,
983 .poll = capi_poll,
984 .ioctl = capi_ioctl,
985 .open = capi_open,
986 .release = capi_release,
987};
988
989#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
990/* -------- tty_operations for capincci ----------------------------- */
991
992static int capinc_tty_open(struct tty_struct * tty, struct file * file)
993{
994 struct capiminor *mp;
Michael Buesch053b47f2007-02-12 00:53:26 -0800995 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700997 if ((mp = capiminor_find(iminor(file->f_path.dentry->d_inode))) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return -ENXIO;
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700999 if (mp->nccip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return -ENXIO;
1001
1002 tty->driver_data = (void *)mp;
1003
Michael Buesch053b47f2007-02-12 00:53:26 -08001004 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (atomic_read(&mp->ttyopencount) == 0)
1006 mp->tty = tty;
1007 atomic_inc(&mp->ttyopencount);
1008#ifdef _DEBUG_REFCOUNT
1009 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1010#endif
1011 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001012 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return 0;
1014}
1015
1016static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1017{
1018 struct capiminor *mp;
1019
1020 mp = (struct capiminor *)tty->driver_data;
1021 if (mp) {
1022 if (atomic_dec_and_test(&mp->ttyopencount)) {
1023#ifdef _DEBUG_REFCOUNT
1024 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1025#endif
1026 tty->driver_data = NULL;
1027 mp->tty = NULL;
1028 }
1029#ifdef _DEBUG_REFCOUNT
1030 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1031#endif
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001032 if (mp->nccip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 capiminor_free(mp);
1034 }
1035
1036#ifdef _DEBUG_REFCOUNT
1037 printk(KERN_DEBUG "capinc_tty_close\n");
1038#endif
1039}
1040
1041static int capinc_tty_write(struct tty_struct * tty,
1042 const unsigned char *buf, int count)
1043{
1044 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1045 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001046 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048#ifdef _DEBUG_TTYFUNCS
1049 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1050#endif
1051
1052 if (!mp || !mp->nccip) {
1053#ifdef _DEBUG_TTYFUNCS
1054 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1055#endif
1056 return 0;
1057 }
1058
Michael Buesch053b47f2007-02-12 00:53:26 -08001059 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 skb = mp->ttyskb;
1061 if (skb) {
1062 mp->ttyskb = NULL;
1063 skb_queue_tail(&mp->outqueue, skb);
1064 mp->outbytes += skb->len;
1065 }
1066
1067 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1068 if (!skb) {
1069 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
Michael Buesch053b47f2007-02-12 00:53:26 -08001070 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return -ENOMEM;
1072 }
1073
1074 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1075 memcpy(skb_put(skb, count), buf, count);
1076
1077 skb_queue_tail(&mp->outqueue, skb);
1078 mp->outbytes += skb->len;
1079 (void)handle_minor_send(mp);
1080 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001081 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return count;
1083}
1084
Alan Coxf2545a72008-04-30 00:54:09 -07001085static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
1087 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1088 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001089 unsigned long flags;
Alan Coxf2545a72008-04-30 00:54:09 -07001090 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092#ifdef _DEBUG_TTYFUNCS
1093 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1094#endif
1095
1096 if (!mp || !mp->nccip) {
1097#ifdef _DEBUG_TTYFUNCS
1098 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1099#endif
Alan Coxf2545a72008-04-30 00:54:09 -07001100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102
Michael Buesch053b47f2007-02-12 00:53:26 -08001103 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 skb = mp->ttyskb;
1105 if (skb) {
1106 if (skb_tailroom(skb) > 0) {
1107 *(skb_put(skb, 1)) = ch;
Michael Buesch053b47f2007-02-12 00:53:26 -08001108 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001109 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
1111 mp->ttyskb = NULL;
1112 skb_queue_tail(&mp->outqueue, skb);
1113 mp->outbytes += skb->len;
1114 (void)handle_minor_send(mp);
1115 }
1116 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1117 if (skb) {
1118 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1119 *(skb_put(skb, 1)) = ch;
1120 mp->ttyskb = skb;
1121 } else {
1122 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
Alan Coxf2545a72008-04-30 00:54:09 -07001123 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
Michael Buesch053b47f2007-02-12 00:53:26 -08001125 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001126 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
1129static void capinc_tty_flush_chars(struct tty_struct *tty)
1130{
1131 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1132 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001133 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135#ifdef _DEBUG_TTYFUNCS
1136 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1137#endif
1138
1139 if (!mp || !mp->nccip) {
1140#ifdef _DEBUG_TTYFUNCS
1141 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1142#endif
1143 return;
1144 }
1145
Michael Buesch053b47f2007-02-12 00:53:26 -08001146 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 skb = mp->ttyskb;
1148 if (skb) {
1149 mp->ttyskb = NULL;
1150 skb_queue_tail(&mp->outqueue, skb);
1151 mp->outbytes += skb->len;
1152 (void)handle_minor_send(mp);
1153 }
1154 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001155 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
1158static int capinc_tty_write_room(struct tty_struct *tty)
1159{
1160 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1161 int room;
1162 if (!mp || !mp->nccip) {
1163#ifdef _DEBUG_TTYFUNCS
1164 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1165#endif
1166 return 0;
1167 }
1168 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1169 room *= CAPI_MAX_BLKSIZE;
1170#ifdef _DEBUG_TTYFUNCS
1171 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1172#endif
1173 return room;
1174}
1175
Adrian Bunk408b6642005-05-01 08:59:29 -07001176static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1179 if (!mp || !mp->nccip) {
1180#ifdef _DEBUG_TTYFUNCS
1181 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1182#endif
1183 return 0;
1184 }
1185#ifdef _DEBUG_TTYFUNCS
1186 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1187 mp->outbytes, mp->nack,
1188 skb_queue_len(&mp->outqueue),
1189 skb_queue_len(&mp->inqueue));
1190#endif
1191 return mp->outbytes;
1192}
1193
1194static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1195 unsigned int cmd, unsigned long arg)
1196{
1197 int error = 0;
1198 switch (cmd) {
1199 default:
Stephen Rothwell53e86312008-10-13 10:44:33 +01001200 error = n_tty_ioctl_helper(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 break;
1202 }
1203 return error;
1204}
1205
Alan Cox606d0992006-12-08 02:38:45 -08001206static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208#ifdef _DEBUG_TTYFUNCS
1209 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1210#endif
1211}
1212
1213static void capinc_tty_throttle(struct tty_struct * tty)
1214{
1215 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1216#ifdef _DEBUG_TTYFUNCS
1217 printk(KERN_DEBUG "capinc_tty_throttle\n");
1218#endif
1219 if (mp)
1220 mp->ttyinstop = 1;
1221}
1222
1223static void capinc_tty_unthrottle(struct tty_struct * tty)
1224{
1225 struct capiminor *mp = (struct capiminor *)tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001226 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227#ifdef _DEBUG_TTYFUNCS
1228 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1229#endif
1230 if (mp) {
Michael Buesch053b47f2007-02-12 00:53:26 -08001231 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 mp->ttyinstop = 0;
1233 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001234 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236}
1237
1238static void capinc_tty_stop(struct tty_struct *tty)
1239{
1240 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1241#ifdef _DEBUG_TTYFUNCS
1242 printk(KERN_DEBUG "capinc_tty_stop\n");
1243#endif
1244 if (mp) {
1245 mp->ttyoutstop = 1;
1246 }
1247}
1248
1249static void capinc_tty_start(struct tty_struct *tty)
1250{
1251 struct capiminor *mp = (struct capiminor *)tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001252 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253#ifdef _DEBUG_TTYFUNCS
1254 printk(KERN_DEBUG "capinc_tty_start\n");
1255#endif
1256 if (mp) {
Michael Buesch053b47f2007-02-12 00:53:26 -08001257 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 mp->ttyoutstop = 0;
1259 (void)handle_minor_send(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001260 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
1262}
1263
1264static void capinc_tty_hangup(struct tty_struct *tty)
1265{
1266#ifdef _DEBUG_TTYFUNCS
1267 printk(KERN_DEBUG "capinc_tty_hangup\n");
1268#endif
1269}
1270
Alan Cox9e989662008-07-22 11:18:03 +01001271static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273#ifdef _DEBUG_TTYFUNCS
1274 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1275#endif
Alan Cox9e989662008-07-22 11:18:03 +01001276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
1279static void capinc_tty_flush_buffer(struct tty_struct *tty)
1280{
1281#ifdef _DEBUG_TTYFUNCS
1282 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1283#endif
1284}
1285
1286static void capinc_tty_set_ldisc(struct tty_struct *tty)
1287{
1288#ifdef _DEBUG_TTYFUNCS
1289 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1290#endif
1291}
1292
1293static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1294{
1295#ifdef _DEBUG_TTYFUNCS
1296 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1297#endif
1298}
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300static struct tty_driver *capinc_tty_driver;
1301
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001302static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 .open = capinc_tty_open,
1304 .close = capinc_tty_close,
1305 .write = capinc_tty_write,
1306 .put_char = capinc_tty_put_char,
1307 .flush_chars = capinc_tty_flush_chars,
1308 .write_room = capinc_tty_write_room,
1309 .chars_in_buffer = capinc_tty_chars_in_buffer,
1310 .ioctl = capinc_tty_ioctl,
1311 .set_termios = capinc_tty_set_termios,
1312 .throttle = capinc_tty_throttle,
1313 .unthrottle = capinc_tty_unthrottle,
1314 .stop = capinc_tty_stop,
1315 .start = capinc_tty_start,
1316 .hangup = capinc_tty_hangup,
1317 .break_ctl = capinc_tty_break_ctl,
1318 .flush_buffer = capinc_tty_flush_buffer,
1319 .set_ldisc = capinc_tty_set_ldisc,
1320 .send_xchar = capinc_tty_send_xchar,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321};
1322
1323static int capinc_tty_init(void)
1324{
1325 struct tty_driver *drv;
1326
1327 if (capi_ttyminors > CAPINC_MAX_PORTS)
1328 capi_ttyminors = CAPINC_MAX_PORTS;
1329 if (capi_ttyminors <= 0)
1330 capi_ttyminors = CAPINC_NR_PORTS;
1331
1332 drv = alloc_tty_driver(capi_ttyminors);
1333 if (!drv)
1334 return -ENOMEM;
1335
1336 drv->owner = THIS_MODULE;
1337 drv->driver_name = "capi_nc";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 drv->name = "capi";
1339 drv->major = capi_ttymajor;
1340 drv->minor_start = 0;
1341 drv->type = TTY_DRIVER_TYPE_SERIAL;
1342 drv->subtype = SERIAL_TYPE_NORMAL;
1343 drv->init_termios = tty_std_termios;
1344 drv->init_termios.c_iflag = ICRNL;
1345 drv->init_termios.c_oflag = OPOST | ONLCR;
1346 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1347 drv->init_termios.c_lflag = 0;
1348 drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS;
1349 tty_set_operations(drv, &capinc_ops);
1350 if (tty_register_driver(drv)) {
1351 put_tty_driver(drv);
1352 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1353 return -1;
1354 }
1355 capinc_tty_driver = drv;
1356 return 0;
1357}
1358
1359static void capinc_tty_exit(void)
1360{
1361 struct tty_driver *drv = capinc_tty_driver;
1362 int retval;
1363 if ((retval = tty_unregister_driver(drv)))
1364 printk(KERN_ERR "capi: failed to unregister capi_nc driver (%d)\n", retval);
1365 put_tty_driver(drv);
1366}
1367
Jan Kiszka501c87a2010-02-08 10:12:16 +00001368#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1369
1370static inline int capinc_tty_init(void)
1371{
1372 return 0;
1373}
1374
1375static inline void capinc_tty_exit(void) { }
1376
1377#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379/* -------- /proc functions ----------------------------------------- */
1380
1381/*
1382 * /proc/capi/capi20:
1383 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1384 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001385static int capi20_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
1387 struct capidev *cdev;
1388 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001390 mutex_lock(&capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 list_for_each(l, &capidev_list) {
1392 cdev = list_entry(l, struct capidev, list);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001393 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 cdev->ap.applid,
1395 cdev->ap.nrecvctlpkt,
1396 cdev->ap.nrecvdatapkt,
1397 cdev->ap.nsentctlpkt,
1398 cdev->ap.nsentdatapkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001400 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001404static int capi20_proc_open(struct inode *inode, struct file *file)
1405{
1406 return single_open(file, capi20_proc_show, NULL);
1407}
1408
1409static const struct file_operations capi20_proc_fops = {
1410 .owner = THIS_MODULE,
1411 .open = capi20_proc_open,
1412 .read = seq_read,
1413 .llseek = seq_lseek,
1414 .release = single_release,
1415};
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417/*
1418 * /proc/capi/capi20ncci:
1419 * applid ncci
1420 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001421static int capi20ncci_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Jan Kiszka884f5c42010-02-08 10:12:22 +00001423 struct capidev *cdev;
1424 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001426 mutex_lock(&capidev_list_lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001427 list_for_each_entry(cdev, &capidev_list, list) {
Jan Kiszka05b41492010-02-08 10:12:19 +00001428 mutex_lock(&cdev->lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001429 list_for_each_entry(np, &cdev->nccis, list)
1430 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
Jan Kiszka05b41492010-02-08 10:12:19 +00001431 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001433 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
1436
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001437static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1438{
1439 return single_open(file, capi20ncci_proc_show, NULL);
1440}
1441
1442static const struct file_operations capi20ncci_proc_fops = {
1443 .owner = THIS_MODULE,
1444 .open = capi20ncci_proc_open,
1445 .read = seq_read,
1446 .llseek = seq_lseek,
1447 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448};
1449
1450static void __init proc_init(void)
1451{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001452 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1453 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
1456static void __exit proc_exit(void)
1457{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001458 remove_proc_entry("capi/capi20", NULL);
1459 remove_proc_entry("capi/capi20ncci", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
1462/* -------- init function and module interface ---------------------- */
1463
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465static int __init capi_init(void)
1466{
Jan Kiszka88549d62010-02-08 10:12:09 +00001467 const char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001468 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Andrew Morton6d9eac32006-03-28 01:56:19 -08001470 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1471 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001473 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001475 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (IS_ERR(capi_class)) {
1477 unregister_chrdev(capi_major, "capi20");
1478 return PTR_ERR(capi_class);
1479 }
1480
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -07001481 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (capinc_tty_init() < 0) {
Tony Jonesd78b0362007-09-25 02:03:03 +02001484 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001485 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 unregister_chrdev(capi_major, "capi20");
1487 return -ENOMEM;
1488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 proc_init();
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1493 compileinfo = " (middleware+capifs)";
Jan Kiszka501c87a2010-02-08 10:12:16 +00001494#elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 compileinfo = " (no capifs)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496#else
1497 compileinfo = " (no middleware)";
1498#endif
Jan Kiszka88549d62010-02-08 10:12:09 +00001499 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1500 capi_major, compileinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 return 0;
1503}
1504
1505static void __exit capi_exit(void)
1506{
1507 proc_exit();
1508
Tony Jonesd78b0362007-09-25 02:03:03 +02001509 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001510 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 capinc_tty_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
1516module_init(capi_init);
1517module_exit(capi_exit);