blob: 901b79bc3b569ca2228f46f760fb0d6d00041f27 [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 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 struct capincci *nccip;
89 unsigned int minor;
Jan Kiszka90926f02010-02-08 10:12:06 +000090 struct dentry *capifs_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 struct capi20_appl *ap;
93 u32 ncci;
94 u16 datahandle;
95 u16 msgid;
96
97 struct tty_struct *tty;
98 int ttyinstop;
99 int ttyoutstop;
100 struct sk_buff *ttyskb;
101 atomic_t ttyopencount;
102
103 struct sk_buff_head inqueue;
104 int inbytes;
105 struct sk_buff_head outqueue;
106 int outbytes;
107
108 /* transmit path */
Michael Buesch6aa65472006-06-26 00:25:30 -0700109 struct list_head ackqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 int nack;
Michael Buesch6aa65472006-06-26 00:25:30 -0700111 spinlock_t ackqlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Michael Buesch053b47f2007-02-12 00:53:26 -0800114/* FIXME: The following lock is a sledgehammer-workaround to a
115 * locking issue with the capiminor (and maybe other) data structure(s).
116 * Access to this data is done in a racy way and crashes the machine with
117 * a FritzCard DSL driver; sooner or later. This is a workaround
118 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
119 * The correct (and scalable) fix for the issue seems to require
120 * an API change to the drivers... . */
121static DEFINE_SPINLOCK(workaround_lock);
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123struct capincci {
Jan Kiszka884f5c42010-02-08 10:12:22 +0000124 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 u32 ncci;
126 struct capidev *cdev;
127#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
128 struct capiminor *minorp;
129#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
130};
131
132struct capidev {
133 struct list_head list;
134 struct capi20_appl ap;
135 u16 errcode;
136 unsigned userflags;
137
138 struct sk_buff_head recvqueue;
139 wait_queue_head_t recvwait;
140
Jan Kiszka884f5c42010-02-08 10:12:22 +0000141 struct list_head nccis;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jan Kiszka05b41492010-02-08 10:12:19 +0000143 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
146/* -------- global variables ---------------------------------------- */
147
Jan Kiszkab8f433d2010-02-08 10:12:17 +0000148static DEFINE_MUTEX(capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static LIST_HEAD(capidev_list);
150
151#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +0000152
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000153static DEFINE_RWLOCK(capiminors_lock);
154static struct capiminor **capiminors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000156static struct tty_driver *capinc_tty_driver;
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/* -------- datahandles --------------------------------------------- */
159
Jan Kiszka501c87a2010-02-08 10:12:16 +0000160static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Michael Buesch6aa65472006-06-26 00:25:30 -0700162 struct datahandle_queue *n;
163 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 n = kmalloc(sizeof(*n), GFP_ATOMIC);
Michael Buesch6aa65472006-06-26 00:25:30 -0700166 if (unlikely(!n)) {
167 printk(KERN_ERR "capi: alloc datahandle failed\n");
168 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 n->datahandle = datahandle;
Michael Buesch6aa65472006-06-26 00:25:30 -0700171 INIT_LIST_HEAD(&n->list);
172 spin_lock_irqsave(&mp->ackqlock, flags);
173 list_add_tail(&n->list, &mp->ackqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 mp->nack++;
Michael Buesch6aa65472006-06-26 00:25:30 -0700175 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return 0;
177}
178
179static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
180{
Michael Buesch6aa65472006-06-26 00:25:30 -0700181 struct datahandle_queue *p, *tmp;
182 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Michael Buesch6aa65472006-06-26 00:25:30 -0700184 spin_lock_irqsave(&mp->ackqlock, flags);
185 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
186 if (p->datahandle == datahandle) {
187 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 kfree(p);
189 mp->nack--;
Michael Buesch6aa65472006-06-26 00:25:30 -0700190 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return 0;
192 }
193 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700194 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -1;
196}
197
198static void capiminor_del_all_ack(struct capiminor *mp)
199{
Michael Buesch6aa65472006-06-26 00:25:30 -0700200 struct datahandle_queue *p, *tmp;
201 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Michael Buesch6aa65472006-06-26 00:25:30 -0700203 spin_lock_irqsave(&mp->ackqlock, flags);
204 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
205 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 kfree(p);
207 mp->nack--;
208 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700209 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212
213/* -------- struct capiminor ---------------------------------------- */
214
215static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
216{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000217 struct capiminor *mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000218 struct device *dev;
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000219 unsigned int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 unsigned long flags;
221
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000222 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (!mp) {
224 printk(KERN_ERR "capi: can't alloc capiminor\n");
225 return NULL;
226 }
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 mp->ap = ap;
229 mp->ncci = ncci;
230 mp->msgid = 0;
231 atomic_set(&mp->ttyopencount,0);
Michael Buesch6aa65472006-06-26 00:25:30 -0700232 INIT_LIST_HEAD(&mp->ackqueue);
233 spin_lock_init(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 skb_queue_head_init(&mp->inqueue);
236 skb_queue_head_init(&mp->outqueue);
237
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000238 /* Allocate the least unused minor number. */
239 write_lock_irqsave(&capiminors_lock, flags);
240 for (minor = 0; minor < capi_ttyminors; minor++)
241 if (!capiminors[minor]) {
242 capiminors[minor] = mp;
243 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000245 write_unlock_irqrestore(&capiminors_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000247 if (minor == capi_ttyminors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 printk(KERN_NOTICE "capi: out of minors\n");
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000249 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000252 mp->minor = minor;
253
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000254 dev = tty_register_device(capinc_tty_driver, minor, NULL);
255 if (IS_ERR(dev))
256 goto err_out2;
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000259
260err_out2:
261 write_lock_irqsave(&capiminors_lock, flags);
262 capiminors[minor] = NULL;
263 write_unlock_irqrestore(&capiminors_lock, flags);
264
265err_out1:
266 kfree(mp);
267 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270static void capiminor_free(struct capiminor *mp)
271{
272 unsigned long flags;
273
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000274 tty_unregister_device(capinc_tty_driver, mp->minor);
275
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000276 write_lock_irqsave(&capiminors_lock, flags);
277 capiminors[mp->minor] = NULL;
278 write_unlock_irqrestore(&capiminors_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Wei Yongjund46604e2009-02-25 00:12:09 +0000280 kfree_skb(mp->ttyskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 mp->ttyskb = NULL;
282 skb_queue_purge(&mp->inqueue);
283 skb_queue_purge(&mp->outqueue);
284 capiminor_del_all_ack(mp);
285 kfree(mp);
286}
287
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000288static struct capiminor *capiminor_get(unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000290 struct capiminor *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000292 read_lock(&capiminors_lock);
293 mp = capiminors[minor];
294 read_unlock(&capiminors_lock);
295
296 return mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299/* -------- struct capincci ----------------------------------------- */
300
Jan Kiszka501c87a2010-02-08 10:12:16 +0000301static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Jan Kiszka501c87a2010-02-08 10:12:16 +0000303 struct capiminor *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Jan Kiszka501c87a2010-02-08 10:12:16 +0000305 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
306 return;
307
308 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (mp) {
310 mp->nccip = np;
311#ifdef _DEBUG_REFCOUNT
312 printk(KERN_DEBUG "set mp->nccip\n");
313#endif
Jan Kiszka90926f02010-02-08 10:12:06 +0000314 mp->capifs_dentry =
315 capifs_new_ncci(mp->minor,
316 MKDEV(capi_ttymajor, mp->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000318}
319
320static void capincci_free_minor(struct capincci *np)
321{
322 struct capiminor *mp = np->minorp;
323
324 if (mp) {
325 capifs_free_ncci(mp->capifs_dentry);
326 if (mp->tty) {
327 mp->nccip = NULL;
328#ifdef _DEBUG_REFCOUNT
329 printk(KERN_DEBUG "reset mp->nccip\n");
330#endif
331 tty_hangup(mp->tty);
332 } else {
333 capiminor_free(mp);
334 }
335 }
336}
337
338static inline unsigned int capincci_minor_opencount(struct capincci *np)
339{
340 struct capiminor *mp = np->minorp;
341
342 return mp ? atomic_read(&mp->ttyopencount) : 0;
343}
344
345#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
346
347static inline void
348capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
349static inline void capincci_free_minor(struct capincci *np) { }
350
351static inline unsigned int capincci_minor_opencount(struct capincci *np)
352{
353 return 0;
354}
355
356#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
357
358static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
359{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000360 struct capincci *np;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000361
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000362 np = kzalloc(sizeof(*np), GFP_KERNEL);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000363 if (!np)
364 return NULL;
365 np->ncci = ncci;
366 np->cdev = cdev;
367
368 capincci_alloc_minor(cdev, np);
369
Jan Kiszka884f5c42010-02-08 10:12:22 +0000370 list_add_tail(&np->list, &cdev->nccis);
371
372 return np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375static void capincci_free(struct capidev *cdev, u32 ncci)
376{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000377 struct capincci *np, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Jan Kiszka884f5c42010-02-08 10:12:22 +0000379 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (ncci == 0xffffffff || np->ncci == ncci) {
Jan Kiszka501c87a2010-02-08 10:12:16 +0000381 capincci_free_minor(np);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000382 list_del(&np->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 kfree(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
387static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
388{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000389 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Jan Kiszka884f5c42010-02-08 10:12:22 +0000391 list_for_each_entry(np, &cdev->nccis, list)
392 if (np->ncci == ncci)
393 return np;
394 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
398/* -------- handle data queue --------------------------------------- */
399
400static struct sk_buff *
401gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
402{
403 struct sk_buff *nskb;
404 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
405 if (nskb) {
406 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
407 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
408 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
409 capimsg_setu16(s, 2, mp->ap->applid);
410 capimsg_setu8 (s, 4, CAPI_DATA_B3);
411 capimsg_setu8 (s, 5, CAPI_RESP);
412 capimsg_setu16(s, 6, mp->msgid++);
413 capimsg_setu32(s, 8, mp->ncci);
414 capimsg_setu16(s, 12, datahandle);
415 }
416 return nskb;
417}
418
419static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
420{
421 struct sk_buff *nskb;
422 int datalen;
423 u16 errcode, datahandle;
424 struct tty_ldisc *ld;
425
426 datalen = skb->len - CAPIMSG_LEN(skb->data);
427 if (mp->tty == NULL)
428 {
429#ifdef _DEBUG_DATAFLOW
430 printk(KERN_DEBUG "capi: currently no receiver\n");
431#endif
432 return -1;
433 }
434
435 ld = tty_ldisc_ref(mp->tty);
436 if (ld == NULL)
437 return -1;
Alan Coxa352def2008-07-16 21:53:12 +0100438 if (ld->ops->receive_buf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
440 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
441#endif
442 goto bad;
443 }
444 if (mp->ttyinstop) {
445#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
446 printk(KERN_DEBUG "capi: recv tty throttled\n");
447#endif
448 goto bad;
449 }
Alan Cox33f0f882006-01-09 20:54:13 -0800450 if (mp->tty->receive_room < datalen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
452 printk(KERN_DEBUG "capi: no room in tty\n");
453#endif
454 goto bad;
455 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700456 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
458 goto bad;
459 }
460 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
461 errcode = capi20_put_message(mp->ap, nskb);
462 if (errcode != CAPI_NOERROR) {
463 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
464 errcode);
465 kfree_skb(nskb);
466 goto bad;
467 }
468 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
469#ifdef _DEBUG_DATAFLOW
470 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
471 datahandle, skb->len);
472#endif
Alan Coxa352def2008-07-16 21:53:12 +0100473 ld->ops->receive_buf(mp->tty, skb->data, NULL, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 kfree_skb(skb);
475 tty_ldisc_deref(ld);
476 return 0;
477bad:
478 tty_ldisc_deref(ld);
479 return -1;
480}
481
482static void handle_minor_recv(struct capiminor *mp)
483{
484 struct sk_buff *skb;
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700485 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 unsigned int len = skb->len;
487 mp->inbytes -= len;
488 if (handle_recv_skb(mp, skb) < 0) {
489 skb_queue_head(&mp->inqueue, skb);
490 mp->inbytes += len;
491 return;
492 }
493 }
494}
495
496static int handle_minor_send(struct capiminor *mp)
497{
498 struct sk_buff *skb;
499 u16 len;
500 int count = 0;
501 u16 errcode;
502 u16 datahandle;
503
504 if (mp->tty && mp->ttyoutstop) {
505#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
506 printk(KERN_DEBUG "capi: send: tty stopped\n");
507#endif
508 return 0;
509 }
510
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700511 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 datahandle = mp->datahandle;
513 len = (u16)skb->len;
514 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
515 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
516 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
517 capimsg_setu16(skb->data, 2, mp->ap->applid);
518 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
519 capimsg_setu8 (skb->data, 5, CAPI_REQ);
520 capimsg_setu16(skb->data, 6, mp->msgid++);
521 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
Andrew Morton5e6c20a2007-07-17 04:04:22 -0700522 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 capimsg_setu16(skb->data, 16, len); /* Data length */
524 capimsg_setu16(skb->data, 18, datahandle);
525 capimsg_setu16(skb->data, 20, 0); /* Flags */
526
Jan Kiszka501c87a2010-02-08 10:12:16 +0000527 if (capiminor_add_ack(mp, datahandle) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
529 skb_queue_head(&mp->outqueue, skb);
530 return count;
531 }
532 errcode = capi20_put_message(mp->ap, skb);
533 if (errcode == CAPI_NOERROR) {
534 mp->datahandle++;
535 count++;
536 mp->outbytes -= len;
537#ifdef _DEBUG_DATAFLOW
538 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
539 datahandle, len);
540#endif
541 continue;
542 }
543 capiminor_del_ack(mp, datahandle);
544
545 if (errcode == CAPI_SENDQUEUEFULL) {
546 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
547 skb_queue_head(&mp->outqueue, skb);
548 break;
549 }
550
551 /* ups, drop packet */
552 printk(KERN_ERR "capi: put_message = %x\n", errcode);
553 mp->outbytes -= len;
554 kfree_skb(skb);
555 }
556 return count;
557}
558
559#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
560/* -------- function called by lower level -------------------------- */
561
562static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
563{
564 struct capidev *cdev = ap->private;
565#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
566 struct capiminor *mp;
567 u16 datahandle;
568#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
569 struct capincci *np;
Michael Buesch053b47f2007-02-12 00:53:26 -0800570 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Jan Kiszka05b41492010-02-08 10:12:19 +0000572 mutex_lock(&cdev->lock);
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
575 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
Jan Kiszka05b41492010-02-08 10:12:19 +0000576 if ((info & 0xff00) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000579 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000581
Michael Buesch053b47f2007-02-12 00:53:26 -0800582 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
584 skb_queue_tail(&cdev->recvqueue, skb);
585 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000586 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000588
589 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (!np) {
591 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
592 skb_queue_tail(&cdev->recvqueue, skb);
593 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000594 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
598 skb_queue_tail(&cdev->recvqueue, skb);
599 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 mp = np->minorp;
604 if (!mp) {
605 skb_queue_tail(&cdev->recvqueue, skb);
606 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000607 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
611#ifdef _DEBUG_DATAFLOW
612 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
613 datahandle, skb->len-CAPIMSG_LEN(skb->data));
614#endif
615 skb_queue_tail(&mp->inqueue, skb);
616 mp->inbytes += skb->len;
617 handle_minor_recv(mp);
618
619 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
620
621 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
622#ifdef _DEBUG_DATAFLOW
623 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
624 datahandle,
625 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
626#endif
627 kfree_skb(skb);
628 (void)capiminor_del_ack(mp, datahandle);
629 if (mp->tty)
630 tty_wakeup(mp->tty);
631 (void)handle_minor_send(mp);
632
633 } else {
634 /* ups, let capi application handle it :-) */
635 skb_queue_tail(&cdev->recvqueue, skb);
636 wake_up_interruptible(&cdev->recvwait);
637 }
638#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000639
Jan Kiszka05b41492010-02-08 10:12:19 +0000640unlock_out:
Michael Buesch053b47f2007-02-12 00:53:26 -0800641 spin_unlock_irqrestore(&workaround_lock, flags);
Jan Kiszka05b41492010-02-08 10:12:19 +0000642 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645/* -------- file_operations for capidev ----------------------------- */
646
647static ssize_t
648capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
649{
650 struct capidev *cdev = (struct capidev *)file->private_data;
651 struct sk_buff *skb;
652 size_t copied;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000653 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 if (!cdev->ap.applid)
656 return -ENODEV;
657
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000658 skb = skb_dequeue(&cdev->recvqueue);
659 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (file->f_flags & O_NONBLOCK)
661 return -EAGAIN;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000662 err = wait_event_interruptible(cdev->recvwait,
663 (skb = skb_dequeue(&cdev->recvqueue)));
664 if (err)
665 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667 if (skb->len > count) {
668 skb_queue_head(&cdev->recvqueue, skb);
669 return -EMSGSIZE;
670 }
671 if (copy_to_user(buf, skb->data, skb->len)) {
672 skb_queue_head(&cdev->recvqueue, skb);
673 return -EFAULT;
674 }
675 copied = skb->len;
676
677 kfree_skb(skb);
678
679 return copied;
680}
681
682static ssize_t
683capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
684{
685 struct capidev *cdev = (struct capidev *)file->private_data;
686 struct sk_buff *skb;
687 u16 mlen;
688
689 if (!cdev->ap.applid)
690 return -ENODEV;
691
692 skb = alloc_skb(count, GFP_USER);
693 if (!skb)
694 return -ENOMEM;
695
696 if (copy_from_user(skb_put(skb, count), buf, count)) {
697 kfree_skb(skb);
698 return -EFAULT;
699 }
700 mlen = CAPIMSG_LEN(skb->data);
701 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
702 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
703 kfree_skb(skb);
704 return -EINVAL;
705 }
706 } else {
707 if (mlen != count) {
708 kfree_skb(skb);
709 return -EINVAL;
710 }
711 }
712 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
713
714 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Jan Kiszka05b41492010-02-08 10:12:19 +0000715 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000717 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
720 cdev->errcode = capi20_put_message(&cdev->ap, skb);
721
722 if (cdev->errcode) {
723 kfree_skb(skb);
724 return -EIO;
725 }
726 return count;
727}
728
729static unsigned int
730capi_poll(struct file *file, poll_table * wait)
731{
732 struct capidev *cdev = (struct capidev *)file->private_data;
733 unsigned int mask = 0;
734
735 if (!cdev->ap.applid)
736 return POLLERR;
737
738 poll_wait(file, &(cdev->recvwait), wait);
739 mask = POLLOUT | POLLWRNORM;
740 if (!skb_queue_empty(&cdev->recvqueue))
741 mask |= POLLIN | POLLRDNORM;
742 return mask;
743}
744
745static int
746capi_ioctl(struct inode *inode, struct file *file,
747 unsigned int cmd, unsigned long arg)
748{
749 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 capi_ioctl_struct data;
751 int retval = -EINVAL;
752 void __user *argp = (void __user *)arg;
753
754 switch (cmd) {
755 case CAPI_REGISTER:
Jan Kiszka05b41492010-02-08 10:12:19 +0000756 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Jan Kiszka05b41492010-02-08 10:12:19 +0000758 if (cdev->ap.applid) {
759 retval = -EEXIST;
760 goto register_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000762 if (copy_from_user(&cdev->ap.rparam, argp,
763 sizeof(struct capi_register_params))) {
764 retval = -EFAULT;
765 goto register_out;
766 }
767 cdev->ap.private = cdev;
768 cdev->ap.recv_message = capi_recv_message;
769 cdev->errcode = capi20_register(&cdev->ap);
770 retval = (int)cdev->ap.applid;
771 if (cdev->errcode) {
772 cdev->ap.applid = 0;
773 retval = -EIO;
774 }
775
776register_out:
777 mutex_unlock(&cdev->lock);
778 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 case CAPI_GET_VERSION:
781 {
782 if (copy_from_user(&data.contr, argp,
783 sizeof(data.contr)))
784 return -EFAULT;
785 cdev->errcode = capi20_get_version(data.contr, &data.version);
786 if (cdev->errcode)
787 return -EIO;
788 if (copy_to_user(argp, &data.version,
789 sizeof(data.version)))
790 return -EFAULT;
791 }
792 return 0;
793
794 case CAPI_GET_SERIAL:
795 {
796 if (copy_from_user(&data.contr, argp,
797 sizeof(data.contr)))
798 return -EFAULT;
799 cdev->errcode = capi20_get_serial (data.contr, data.serial);
800 if (cdev->errcode)
801 return -EIO;
802 if (copy_to_user(argp, data.serial,
803 sizeof(data.serial)))
804 return -EFAULT;
805 }
806 return 0;
807 case CAPI_GET_PROFILE:
808 {
809 if (copy_from_user(&data.contr, argp,
810 sizeof(data.contr)))
811 return -EFAULT;
812
813 if (data.contr == 0) {
814 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
815 if (cdev->errcode)
816 return -EIO;
817
818 retval = copy_to_user(argp,
819 &data.profile.ncontroller,
820 sizeof(data.profile.ncontroller));
821
822 } else {
823 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
824 if (cdev->errcode)
825 return -EIO;
826
827 retval = copy_to_user(argp, &data.profile,
828 sizeof(data.profile));
829 }
830 if (retval)
831 return -EFAULT;
832 }
833 return 0;
834
835 case CAPI_GET_MANUFACTURER:
836 {
837 if (copy_from_user(&data.contr, argp,
838 sizeof(data.contr)))
839 return -EFAULT;
840 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
841 if (cdev->errcode)
842 return -EIO;
843
844 if (copy_to_user(argp, data.manufacturer,
845 sizeof(data.manufacturer)))
846 return -EFAULT;
847
848 }
849 return 0;
850 case CAPI_GET_ERRCODE:
851 data.errcode = cdev->errcode;
852 cdev->errcode = CAPI_NOERROR;
853 if (arg) {
854 if (copy_to_user(argp, &data.errcode,
855 sizeof(data.errcode)))
856 return -EFAULT;
857 }
858 return data.errcode;
859
860 case CAPI_INSTALLED:
861 if (capi20_isinstalled() == CAPI_NOERROR)
862 return 0;
863 return -ENXIO;
864
865 case CAPI_MANUFACTURER_CMD:
866 {
867 struct capi_manufacturer_cmd mcmd;
868 if (!capable(CAP_SYS_ADMIN))
869 return -EPERM;
870 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
871 return -EFAULT;
872 return capi20_manufacturer(mcmd.cmd, mcmd.data);
873 }
874 return 0;
875
876 case CAPI_SET_FLAGS:
Jan Kiszka05b41492010-02-08 10:12:19 +0000877 case CAPI_CLR_FLAGS: {
878 unsigned userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Jan Kiszka05b41492010-02-08 10:12:19 +0000880 if (copy_from_user(&userflags, argp, sizeof(userflags)))
881 return -EFAULT;
882
883 mutex_lock(&cdev->lock);
884 if (cmd == CAPI_SET_FLAGS)
885 cdev->userflags |= userflags;
886 else
887 cdev->userflags &= ~userflags;
888 mutex_unlock(&cdev->lock);
889 return 0;
890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 case CAPI_GET_FLAGS:
892 if (copy_to_user(argp, &cdev->userflags,
893 sizeof(cdev->userflags)))
894 return -EFAULT;
895 return 0;
896
Jan Kiszka05b41492010-02-08 10:12:19 +0000897 case CAPI_NCCI_OPENCOUNT: {
898 struct capincci *nccip;
899 unsigned ncci;
900 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Jan Kiszka05b41492010-02-08 10:12:19 +0000902 if (copy_from_user(&ncci, argp, sizeof(ncci)))
903 return -EFAULT;
904
905 mutex_lock(&cdev->lock);
906 nccip = capincci_find(cdev, (u32)ncci);
907 if (nccip)
908 count = capincci_minor_opencount(nccip);
909 mutex_unlock(&cdev->lock);
910 return count;
911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka05b41492010-02-08 10:12:19 +0000914 case CAPI_NCCI_GETUNIT: {
915 struct capincci *nccip;
916 struct capiminor *mp;
917 unsigned ncci;
918 int unit = -ESRCH;
919
920 if (copy_from_user(&ncci, argp, sizeof(ncci)))
921 return -EFAULT;
922
923 mutex_lock(&cdev->lock);
924 nccip = capincci_find(cdev, (u32)ncci);
925 if (nccip) {
926 mp = nccip->minorp;
927 if (mp)
928 unit = mp->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000930 mutex_unlock(&cdev->lock);
931 return unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000933#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
934
935 default:
936 return -EINVAL;
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000940static int capi_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000942 struct capidev *cdev;
943
944 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
945 if (!cdev)
946 return -ENOMEM;
947
Jan Kiszka05b41492010-02-08 10:12:19 +0000948 mutex_init(&cdev->lock);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000949 skb_queue_head_init(&cdev->recvqueue);
950 init_waitqueue_head(&cdev->recvwait);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000951 INIT_LIST_HEAD(&cdev->nccis);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000952 file->private_data = cdev;
953
954 mutex_lock(&capidev_list_lock);
955 list_add_tail(&cdev->list, &capidev_list);
956 mutex_unlock(&capidev_list_lock);
957
958 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000961static int capi_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000963 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000965 mutex_lock(&capidev_list_lock);
966 list_del(&cdev->list);
967 mutex_unlock(&capidev_list_lock);
968
Jan Kiszka05b41492010-02-08 10:12:19 +0000969 if (cdev->ap.applid)
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000970 capi20_release(&cdev->ap);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000971 skb_queue_purge(&cdev->recvqueue);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000972 capincci_free(cdev, 0xffffffff);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000973
974 kfree(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return 0;
976}
977
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800978static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 .owner = THIS_MODULE,
981 .llseek = no_llseek,
982 .read = capi_read,
983 .write = capi_write,
984 .poll = capi_poll,
985 .ioctl = capi_ioctl,
986 .open = capi_open,
987 .release = capi_release,
988};
989
990#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
991/* -------- tty_operations for capincci ----------------------------- */
992
993static int capinc_tty_open(struct tty_struct * tty, struct file * file)
994{
995 struct capiminor *mp;
Michael Buesch053b47f2007-02-12 00:53:26 -0800996 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000998 mp = capiminor_get(iminor(file->f_path.dentry->d_inode));
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
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001300static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 .open = capinc_tty_open,
1302 .close = capinc_tty_close,
1303 .write = capinc_tty_write,
1304 .put_char = capinc_tty_put_char,
1305 .flush_chars = capinc_tty_flush_chars,
1306 .write_room = capinc_tty_write_room,
1307 .chars_in_buffer = capinc_tty_chars_in_buffer,
1308 .ioctl = capinc_tty_ioctl,
1309 .set_termios = capinc_tty_set_termios,
1310 .throttle = capinc_tty_throttle,
1311 .unthrottle = capinc_tty_unthrottle,
1312 .stop = capinc_tty_stop,
1313 .start = capinc_tty_start,
1314 .hangup = capinc_tty_hangup,
1315 .break_ctl = capinc_tty_break_ctl,
1316 .flush_buffer = capinc_tty_flush_buffer,
1317 .set_ldisc = capinc_tty_set_ldisc,
1318 .send_xchar = capinc_tty_send_xchar,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319};
1320
Jan Kiszkae76b1542010-02-08 10:12:24 +00001321static int __init capinc_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 struct tty_driver *drv;
Jan Kiszkae76b1542010-02-08 10:12:24 +00001324 int err;
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (capi_ttyminors > CAPINC_MAX_PORTS)
1327 capi_ttyminors = CAPINC_MAX_PORTS;
1328 if (capi_ttyminors <= 0)
1329 capi_ttyminors = CAPINC_NR_PORTS;
1330
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001331 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1332 GFP_KERNEL);
1333 if (!capiminors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return -ENOMEM;
1335
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001336 drv = alloc_tty_driver(capi_ttyminors);
1337 if (!drv) {
1338 kfree(capiminors);
1339 return -ENOMEM;
1340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 drv->owner = THIS_MODULE;
1342 drv->driver_name = "capi_nc";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 drv->name = "capi";
1344 drv->major = capi_ttymajor;
1345 drv->minor_start = 0;
1346 drv->type = TTY_DRIVER_TYPE_SERIAL;
1347 drv->subtype = SERIAL_TYPE_NORMAL;
1348 drv->init_termios = tty_std_termios;
1349 drv->init_termios.c_iflag = ICRNL;
1350 drv->init_termios.c_oflag = OPOST | ONLCR;
1351 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1352 drv->init_termios.c_lflag = 0;
Jan Kiszka40fb2d02010-02-08 10:12:25 +00001353 drv->flags =
1354 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1355 TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 tty_set_operations(drv, &capinc_ops);
Jan Kiszkae76b1542010-02-08 10:12:24 +00001357
1358 err = tty_register_driver(drv);
1359 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 put_tty_driver(drv);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001361 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 printk(KERN_ERR "Couldn't register capi_nc driver\n");
Jan Kiszkae76b1542010-02-08 10:12:24 +00001363 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365 capinc_tty_driver = drv;
1366 return 0;
1367}
1368
Jan Kiszkae76b1542010-02-08 10:12:24 +00001369static void __exit capinc_tty_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Jan Kiszkae76b1542010-02-08 10:12:24 +00001371 tty_unregister_driver(capinc_tty_driver);
1372 put_tty_driver(capinc_tty_driver);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001373 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
Jan Kiszka501c87a2010-02-08 10:12:16 +00001376#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1377
1378static inline int capinc_tty_init(void)
1379{
1380 return 0;
1381}
1382
1383static inline void capinc_tty_exit(void) { }
1384
1385#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387/* -------- /proc functions ----------------------------------------- */
1388
1389/*
1390 * /proc/capi/capi20:
1391 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1392 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001393static int capi20_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 struct capidev *cdev;
1396 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001398 mutex_lock(&capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 list_for_each(l, &capidev_list) {
1400 cdev = list_entry(l, struct capidev, list);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001401 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 cdev->ap.applid,
1403 cdev->ap.nrecvctlpkt,
1404 cdev->ap.nrecvdatapkt,
1405 cdev->ap.nsentctlpkt,
1406 cdev->ap.nsentdatapkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001408 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001412static int capi20_proc_open(struct inode *inode, struct file *file)
1413{
1414 return single_open(file, capi20_proc_show, NULL);
1415}
1416
1417static const struct file_operations capi20_proc_fops = {
1418 .owner = THIS_MODULE,
1419 .open = capi20_proc_open,
1420 .read = seq_read,
1421 .llseek = seq_lseek,
1422 .release = single_release,
1423};
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425/*
1426 * /proc/capi/capi20ncci:
1427 * applid ncci
1428 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001429static int capi20ncci_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Jan Kiszka884f5c42010-02-08 10:12:22 +00001431 struct capidev *cdev;
1432 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001434 mutex_lock(&capidev_list_lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001435 list_for_each_entry(cdev, &capidev_list, list) {
Jan Kiszka05b41492010-02-08 10:12:19 +00001436 mutex_lock(&cdev->lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001437 list_for_each_entry(np, &cdev->nccis, list)
1438 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
Jan Kiszka05b41492010-02-08 10:12:19 +00001439 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001441 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001445static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1446{
1447 return single_open(file, capi20ncci_proc_show, NULL);
1448}
1449
1450static const struct file_operations capi20ncci_proc_fops = {
1451 .owner = THIS_MODULE,
1452 .open = capi20ncci_proc_open,
1453 .read = seq_read,
1454 .llseek = seq_lseek,
1455 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456};
1457
1458static void __init proc_init(void)
1459{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001460 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1461 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
1464static void __exit proc_exit(void)
1465{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001466 remove_proc_entry("capi/capi20", NULL);
1467 remove_proc_entry("capi/capi20ncci", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
1470/* -------- init function and module interface ---------------------- */
1471
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473static int __init capi_init(void)
1474{
Jan Kiszka88549d62010-02-08 10:12:09 +00001475 const char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001476 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Andrew Morton6d9eac32006-03-28 01:56:19 -08001478 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1479 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001481 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001483 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (IS_ERR(capi_class)) {
1485 unregister_chrdev(capi_major, "capi20");
1486 return PTR_ERR(capi_class);
1487 }
1488
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -07001489 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (capinc_tty_init() < 0) {
Tony Jonesd78b0362007-09-25 02:03:03 +02001492 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001493 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 unregister_chrdev(capi_major, "capi20");
1495 return -ENOMEM;
1496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 proc_init();
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1501 compileinfo = " (middleware+capifs)";
Jan Kiszka501c87a2010-02-08 10:12:16 +00001502#elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 compileinfo = " (no capifs)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504#else
1505 compileinfo = " (no middleware)";
1506#endif
Jan Kiszka88549d62010-02-08 10:12:09 +00001507 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1508 capi_major, compileinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 return 0;
1511}
1512
1513static void __exit capi_exit(void)
1514{
1515 proc_exit();
1516
Tony Jonesd78b0362007-09-25 02:03:03 +02001517 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001518 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 capinc_tty_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
1524module_init(capi_init);
1525module_exit(capi_exit);