blob: b1de0cbea69e0c5b11df48facb34d0255ba52d6b [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
Jan Kiszka501c87a2010-02-08 10:12:16 +000063static int capi_ttyminors = CAPINC_NR_PORTS;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065module_param_named(ttyminors, capi_ttyminors, uint, 0);
66#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
67
68/* -------- defines ------------------------------------------------- */
69
70#define CAPINC_MAX_RECVQUEUE 10
71#define CAPINC_MAX_SENDQUEUE 10
72#define CAPI_MAX_BLKSIZE 2048
73
74/* -------- data structures ----------------------------------------- */
75
76struct capidev;
77struct capincci;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078struct capiminor;
79
Michael Buesch6aa65472006-06-26 00:25:30 -070080struct datahandle_queue {
81 struct list_head list;
82 u16 datahandle;
83};
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085struct capiminor {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 struct capincci *nccip;
87 unsigned int minor;
Jan Kiszka90926f02010-02-08 10:12:06 +000088 struct dentry *capifs_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 struct capi20_appl *ap;
91 u32 ncci;
92 u16 datahandle;
93 u16 msgid;
94
95 struct tty_struct *tty;
96 int ttyinstop;
97 int ttyoutstop;
98 struct sk_buff *ttyskb;
99 atomic_t ttyopencount;
100
101 struct sk_buff_head inqueue;
102 int inbytes;
103 struct sk_buff_head outqueue;
104 int outbytes;
105
106 /* transmit path */
Michael Buesch6aa65472006-06-26 00:25:30 -0700107 struct list_head ackqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 int nack;
Michael Buesch6aa65472006-06-26 00:25:30 -0700109 spinlock_t ackqlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Michael Buesch053b47f2007-02-12 00:53:26 -0800112/* FIXME: The following lock is a sledgehammer-workaround to a
113 * locking issue with the capiminor (and maybe other) data structure(s).
114 * Access to this data is done in a racy way and crashes the machine with
115 * a FritzCard DSL driver; sooner or later. This is a workaround
116 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
117 * The correct (and scalable) fix for the issue seems to require
118 * an API change to the drivers... . */
119static DEFINE_SPINLOCK(workaround_lock);
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121struct capincci {
Jan Kiszka884f5c42010-02-08 10:12:22 +0000122 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 u32 ncci;
124 struct capidev *cdev;
125#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
126 struct capiminor *minorp;
127#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
128};
129
130struct capidev {
131 struct list_head list;
132 struct capi20_appl ap;
133 u16 errcode;
134 unsigned userflags;
135
136 struct sk_buff_head recvqueue;
137 wait_queue_head_t recvwait;
138
Jan Kiszka884f5c42010-02-08 10:12:22 +0000139 struct list_head nccis;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Jan Kiszka05b41492010-02-08 10:12:19 +0000141 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
144/* -------- global variables ---------------------------------------- */
145
Jan Kiszkab8f433d2010-02-08 10:12:17 +0000146static DEFINE_MUTEX(capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static LIST_HEAD(capidev_list);
148
149#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +0000150
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000151static DEFINE_RWLOCK(capiminors_lock);
152static struct capiminor **capiminors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000154static struct tty_driver *capinc_tty_driver;
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* -------- datahandles --------------------------------------------- */
157
Jan Kiszka501c87a2010-02-08 10:12:16 +0000158static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Michael Buesch6aa65472006-06-26 00:25:30 -0700160 struct datahandle_queue *n;
161 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 n = kmalloc(sizeof(*n), GFP_ATOMIC);
Michael Buesch6aa65472006-06-26 00:25:30 -0700164 if (unlikely(!n)) {
165 printk(KERN_ERR "capi: alloc datahandle failed\n");
166 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 n->datahandle = datahandle;
Michael Buesch6aa65472006-06-26 00:25:30 -0700169 INIT_LIST_HEAD(&n->list);
170 spin_lock_irqsave(&mp->ackqlock, flags);
171 list_add_tail(&n->list, &mp->ackqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 mp->nack++;
Michael Buesch6aa65472006-06-26 00:25:30 -0700173 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return 0;
175}
176
177static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
178{
Michael Buesch6aa65472006-06-26 00:25:30 -0700179 struct datahandle_queue *p, *tmp;
180 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Michael Buesch6aa65472006-06-26 00:25:30 -0700182 spin_lock_irqsave(&mp->ackqlock, flags);
183 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
184 if (p->datahandle == datahandle) {
185 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 kfree(p);
187 mp->nack--;
Michael Buesch6aa65472006-06-26 00:25:30 -0700188 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return 0;
190 }
191 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700192 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return -1;
194}
195
196static void capiminor_del_all_ack(struct capiminor *mp)
197{
Michael Buesch6aa65472006-06-26 00:25:30 -0700198 struct datahandle_queue *p, *tmp;
199 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Michael Buesch6aa65472006-06-26 00:25:30 -0700201 spin_lock_irqsave(&mp->ackqlock, flags);
202 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
203 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 kfree(p);
205 mp->nack--;
206 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700207 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210
211/* -------- struct capiminor ---------------------------------------- */
212
213static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
214{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000215 struct capiminor *mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000216 struct device *dev;
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000217 unsigned int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 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
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000236 /* Allocate the least unused minor number. */
237 write_lock_irqsave(&capiminors_lock, flags);
238 for (minor = 0; minor < capi_ttyminors; minor++)
239 if (!capiminors[minor]) {
240 capiminors[minor] = mp;
241 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000243 write_unlock_irqrestore(&capiminors_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000245 if (minor == capi_ttyminors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 printk(KERN_NOTICE "capi: out of minors\n");
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000247 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000250 mp->minor = minor;
251
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000252 dev = tty_register_device(capinc_tty_driver, minor, NULL);
253 if (IS_ERR(dev))
254 goto err_out2;
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000257
258err_out2:
259 write_lock_irqsave(&capiminors_lock, flags);
260 capiminors[minor] = NULL;
261 write_unlock_irqrestore(&capiminors_lock, flags);
262
263err_out1:
264 kfree(mp);
265 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268static void capiminor_free(struct capiminor *mp)
269{
270 unsigned long flags;
271
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000272 tty_unregister_device(capinc_tty_driver, mp->minor);
273
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000274 write_lock_irqsave(&capiminors_lock, flags);
275 capiminors[mp->minor] = NULL;
276 write_unlock_irqrestore(&capiminors_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Wei Yongjund46604e2009-02-25 00:12:09 +0000278 kfree_skb(mp->ttyskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 mp->ttyskb = NULL;
280 skb_queue_purge(&mp->inqueue);
281 skb_queue_purge(&mp->outqueue);
282 capiminor_del_all_ack(mp);
283 kfree(mp);
284}
285
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000286static struct capiminor *capiminor_get(unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000288 struct capiminor *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000290 read_lock(&capiminors_lock);
291 mp = capiminors[minor];
292 read_unlock(&capiminors_lock);
293
294 return mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297/* -------- struct capincci ----------------------------------------- */
298
Jan Kiszka501c87a2010-02-08 10:12:16 +0000299static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Jan Kiszka501c87a2010-02-08 10:12:16 +0000301 struct capiminor *mp;
Jan Kiszkae95ac142010-02-08 10:12:26 +0000302 dev_t device;
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 Kiszkae95ac142010-02-08 10:12:26 +0000313 device = MKDEV(capinc_tty_driver->major, mp->minor);
314 mp->capifs_dentry = capifs_new_ncci(mp->minor, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000316}
317
318static void capincci_free_minor(struct capincci *np)
319{
320 struct capiminor *mp = np->minorp;
321
322 if (mp) {
323 capifs_free_ncci(mp->capifs_dentry);
324 if (mp->tty) {
325 mp->nccip = NULL;
326#ifdef _DEBUG_REFCOUNT
327 printk(KERN_DEBUG "reset mp->nccip\n");
328#endif
329 tty_hangup(mp->tty);
330 } else {
331 capiminor_free(mp);
332 }
333 }
334}
335
336static inline unsigned int capincci_minor_opencount(struct capincci *np)
337{
338 struct capiminor *mp = np->minorp;
339
340 return mp ? atomic_read(&mp->ttyopencount) : 0;
341}
342
343#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
344
345static inline void
346capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
347static inline void capincci_free_minor(struct capincci *np) { }
348
349static inline unsigned int capincci_minor_opencount(struct capincci *np)
350{
351 return 0;
352}
353
354#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
355
356static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
357{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000358 struct capincci *np;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000359
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000360 np = kzalloc(sizeof(*np), GFP_KERNEL);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000361 if (!np)
362 return NULL;
363 np->ncci = ncci;
364 np->cdev = cdev;
365
366 capincci_alloc_minor(cdev, np);
367
Jan Kiszka884f5c42010-02-08 10:12:22 +0000368 list_add_tail(&np->list, &cdev->nccis);
369
370 return np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373static void capincci_free(struct capidev *cdev, u32 ncci)
374{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000375 struct capincci *np, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Jan Kiszka884f5c42010-02-08 10:12:22 +0000377 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (ncci == 0xffffffff || np->ncci == ncci) {
Jan Kiszka501c87a2010-02-08 10:12:16 +0000379 capincci_free_minor(np);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000380 list_del(&np->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 kfree(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
386{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000387 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Jan Kiszka884f5c42010-02-08 10:12:22 +0000389 list_for_each_entry(np, &cdev->nccis, list)
390 if (np->ncci == ncci)
391 return np;
392 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
396/* -------- handle data queue --------------------------------------- */
397
398static struct sk_buff *
399gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
400{
401 struct sk_buff *nskb;
402 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
403 if (nskb) {
404 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
405 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
406 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
407 capimsg_setu16(s, 2, mp->ap->applid);
408 capimsg_setu8 (s, 4, CAPI_DATA_B3);
409 capimsg_setu8 (s, 5, CAPI_RESP);
410 capimsg_setu16(s, 6, mp->msgid++);
411 capimsg_setu32(s, 8, mp->ncci);
412 capimsg_setu16(s, 12, datahandle);
413 }
414 return nskb;
415}
416
417static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
418{
419 struct sk_buff *nskb;
420 int datalen;
421 u16 errcode, datahandle;
422 struct tty_ldisc *ld;
423
424 datalen = skb->len - CAPIMSG_LEN(skb->data);
425 if (mp->tty == NULL)
426 {
427#ifdef _DEBUG_DATAFLOW
428 printk(KERN_DEBUG "capi: currently no receiver\n");
429#endif
430 return -1;
431 }
432
433 ld = tty_ldisc_ref(mp->tty);
434 if (ld == NULL)
435 return -1;
Alan Coxa352def2008-07-16 21:53:12 +0100436 if (ld->ops->receive_buf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
438 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
439#endif
440 goto bad;
441 }
442 if (mp->ttyinstop) {
443#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
444 printk(KERN_DEBUG "capi: recv tty throttled\n");
445#endif
446 goto bad;
447 }
Alan Cox33f0f882006-01-09 20:54:13 -0800448 if (mp->tty->receive_room < datalen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
450 printk(KERN_DEBUG "capi: no room in tty\n");
451#endif
452 goto bad;
453 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700454 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
456 goto bad;
457 }
458 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
459 errcode = capi20_put_message(mp->ap, nskb);
460 if (errcode != CAPI_NOERROR) {
461 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
462 errcode);
463 kfree_skb(nskb);
464 goto bad;
465 }
466 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
467#ifdef _DEBUG_DATAFLOW
468 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
469 datahandle, skb->len);
470#endif
Alan Coxa352def2008-07-16 21:53:12 +0100471 ld->ops->receive_buf(mp->tty, skb->data, NULL, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 kfree_skb(skb);
473 tty_ldisc_deref(ld);
474 return 0;
475bad:
476 tty_ldisc_deref(ld);
477 return -1;
478}
479
480static void handle_minor_recv(struct capiminor *mp)
481{
482 struct sk_buff *skb;
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700483 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 unsigned int len = skb->len;
485 mp->inbytes -= len;
486 if (handle_recv_skb(mp, skb) < 0) {
487 skb_queue_head(&mp->inqueue, skb);
488 mp->inbytes += len;
489 return;
490 }
491 }
492}
493
494static int handle_minor_send(struct capiminor *mp)
495{
496 struct sk_buff *skb;
497 u16 len;
498 int count = 0;
499 u16 errcode;
500 u16 datahandle;
501
502 if (mp->tty && mp->ttyoutstop) {
503#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
504 printk(KERN_DEBUG "capi: send: tty stopped\n");
505#endif
506 return 0;
507 }
508
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700509 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 datahandle = mp->datahandle;
511 len = (u16)skb->len;
512 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
513 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
514 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
515 capimsg_setu16(skb->data, 2, mp->ap->applid);
516 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
517 capimsg_setu8 (skb->data, 5, CAPI_REQ);
518 capimsg_setu16(skb->data, 6, mp->msgid++);
519 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
Andrew Morton5e6c20a2007-07-17 04:04:22 -0700520 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 capimsg_setu16(skb->data, 16, len); /* Data length */
522 capimsg_setu16(skb->data, 18, datahandle);
523 capimsg_setu16(skb->data, 20, 0); /* Flags */
524
Jan Kiszka501c87a2010-02-08 10:12:16 +0000525 if (capiminor_add_ack(mp, datahandle) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
527 skb_queue_head(&mp->outqueue, skb);
528 return count;
529 }
530 errcode = capi20_put_message(mp->ap, skb);
531 if (errcode == CAPI_NOERROR) {
532 mp->datahandle++;
533 count++;
534 mp->outbytes -= len;
535#ifdef _DEBUG_DATAFLOW
536 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
537 datahandle, len);
538#endif
539 continue;
540 }
541 capiminor_del_ack(mp, datahandle);
542
543 if (errcode == CAPI_SENDQUEUEFULL) {
544 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
545 skb_queue_head(&mp->outqueue, skb);
546 break;
547 }
548
549 /* ups, drop packet */
550 printk(KERN_ERR "capi: put_message = %x\n", errcode);
551 mp->outbytes -= len;
552 kfree_skb(skb);
553 }
554 return count;
555}
556
557#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
558/* -------- function called by lower level -------------------------- */
559
560static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
561{
562 struct capidev *cdev = ap->private;
563#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
564 struct capiminor *mp;
565 u16 datahandle;
566#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
567 struct capincci *np;
Michael Buesch053b47f2007-02-12 00:53:26 -0800568 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Jan Kiszka05b41492010-02-08 10:12:19 +0000570 mutex_lock(&cdev->lock);
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
573 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
Jan Kiszka05b41492010-02-08 10:12:19 +0000574 if ((info & 0xff00) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000577 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000579
Michael Buesch053b47f2007-02-12 00:53:26 -0800580 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
582 skb_queue_tail(&cdev->recvqueue, skb);
583 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000584 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000586
587 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (!np) {
589 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
590 skb_queue_tail(&cdev->recvqueue, skb);
591 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000592 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
596 skb_queue_tail(&cdev->recvqueue, skb);
597 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 mp = np->minorp;
602 if (!mp) {
603 skb_queue_tail(&cdev->recvqueue, skb);
604 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000605 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
609#ifdef _DEBUG_DATAFLOW
610 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
611 datahandle, skb->len-CAPIMSG_LEN(skb->data));
612#endif
613 skb_queue_tail(&mp->inqueue, skb);
614 mp->inbytes += skb->len;
615 handle_minor_recv(mp);
616
617 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
618
619 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
620#ifdef _DEBUG_DATAFLOW
621 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
622 datahandle,
623 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
624#endif
625 kfree_skb(skb);
626 (void)capiminor_del_ack(mp, datahandle);
627 if (mp->tty)
628 tty_wakeup(mp->tty);
629 (void)handle_minor_send(mp);
630
631 } else {
632 /* ups, let capi application handle it :-) */
633 skb_queue_tail(&cdev->recvqueue, skb);
634 wake_up_interruptible(&cdev->recvwait);
635 }
636#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000637
Jan Kiszka05b41492010-02-08 10:12:19 +0000638unlock_out:
Michael Buesch053b47f2007-02-12 00:53:26 -0800639 spin_unlock_irqrestore(&workaround_lock, flags);
Jan Kiszka05b41492010-02-08 10:12:19 +0000640 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643/* -------- file_operations for capidev ----------------------------- */
644
645static ssize_t
646capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
647{
648 struct capidev *cdev = (struct capidev *)file->private_data;
649 struct sk_buff *skb;
650 size_t copied;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000651 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 if (!cdev->ap.applid)
654 return -ENODEV;
655
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000656 skb = skb_dequeue(&cdev->recvqueue);
657 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (file->f_flags & O_NONBLOCK)
659 return -EAGAIN;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000660 err = wait_event_interruptible(cdev->recvwait,
661 (skb = skb_dequeue(&cdev->recvqueue)));
662 if (err)
663 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665 if (skb->len > count) {
666 skb_queue_head(&cdev->recvqueue, skb);
667 return -EMSGSIZE;
668 }
669 if (copy_to_user(buf, skb->data, skb->len)) {
670 skb_queue_head(&cdev->recvqueue, skb);
671 return -EFAULT;
672 }
673 copied = skb->len;
674
675 kfree_skb(skb);
676
677 return copied;
678}
679
680static ssize_t
681capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
682{
683 struct capidev *cdev = (struct capidev *)file->private_data;
684 struct sk_buff *skb;
685 u16 mlen;
686
687 if (!cdev->ap.applid)
688 return -ENODEV;
689
690 skb = alloc_skb(count, GFP_USER);
691 if (!skb)
692 return -ENOMEM;
693
694 if (copy_from_user(skb_put(skb, count), buf, count)) {
695 kfree_skb(skb);
696 return -EFAULT;
697 }
698 mlen = CAPIMSG_LEN(skb->data);
699 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
700 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
701 kfree_skb(skb);
702 return -EINVAL;
703 }
704 } else {
705 if (mlen != count) {
706 kfree_skb(skb);
707 return -EINVAL;
708 }
709 }
710 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
711
712 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Jan Kiszka05b41492010-02-08 10:12:19 +0000713 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000715 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717
718 cdev->errcode = capi20_put_message(&cdev->ap, skb);
719
720 if (cdev->errcode) {
721 kfree_skb(skb);
722 return -EIO;
723 }
724 return count;
725}
726
727static unsigned int
728capi_poll(struct file *file, poll_table * wait)
729{
730 struct capidev *cdev = (struct capidev *)file->private_data;
731 unsigned int mask = 0;
732
733 if (!cdev->ap.applid)
734 return POLLERR;
735
736 poll_wait(file, &(cdev->recvwait), wait);
737 mask = POLLOUT | POLLWRNORM;
738 if (!skb_queue_empty(&cdev->recvqueue))
739 mask |= POLLIN | POLLRDNORM;
740 return mask;
741}
742
743static int
744capi_ioctl(struct inode *inode, struct file *file,
745 unsigned int cmd, unsigned long arg)
746{
747 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 capi_ioctl_struct data;
749 int retval = -EINVAL;
750 void __user *argp = (void __user *)arg;
751
752 switch (cmd) {
753 case CAPI_REGISTER:
Jan Kiszka05b41492010-02-08 10:12:19 +0000754 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Jan Kiszka05b41492010-02-08 10:12:19 +0000756 if (cdev->ap.applid) {
757 retval = -EEXIST;
758 goto register_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000760 if (copy_from_user(&cdev->ap.rparam, argp,
761 sizeof(struct capi_register_params))) {
762 retval = -EFAULT;
763 goto register_out;
764 }
765 cdev->ap.private = cdev;
766 cdev->ap.recv_message = capi_recv_message;
767 cdev->errcode = capi20_register(&cdev->ap);
768 retval = (int)cdev->ap.applid;
769 if (cdev->errcode) {
770 cdev->ap.applid = 0;
771 retval = -EIO;
772 }
773
774register_out:
775 mutex_unlock(&cdev->lock);
776 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 case CAPI_GET_VERSION:
779 {
780 if (copy_from_user(&data.contr, argp,
781 sizeof(data.contr)))
782 return -EFAULT;
783 cdev->errcode = capi20_get_version(data.contr, &data.version);
784 if (cdev->errcode)
785 return -EIO;
786 if (copy_to_user(argp, &data.version,
787 sizeof(data.version)))
788 return -EFAULT;
789 }
790 return 0;
791
792 case CAPI_GET_SERIAL:
793 {
794 if (copy_from_user(&data.contr, argp,
795 sizeof(data.contr)))
796 return -EFAULT;
797 cdev->errcode = capi20_get_serial (data.contr, data.serial);
798 if (cdev->errcode)
799 return -EIO;
800 if (copy_to_user(argp, data.serial,
801 sizeof(data.serial)))
802 return -EFAULT;
803 }
804 return 0;
805 case CAPI_GET_PROFILE:
806 {
807 if (copy_from_user(&data.contr, argp,
808 sizeof(data.contr)))
809 return -EFAULT;
810
811 if (data.contr == 0) {
812 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
813 if (cdev->errcode)
814 return -EIO;
815
816 retval = copy_to_user(argp,
817 &data.profile.ncontroller,
818 sizeof(data.profile.ncontroller));
819
820 } else {
821 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
822 if (cdev->errcode)
823 return -EIO;
824
825 retval = copy_to_user(argp, &data.profile,
826 sizeof(data.profile));
827 }
828 if (retval)
829 return -EFAULT;
830 }
831 return 0;
832
833 case CAPI_GET_MANUFACTURER:
834 {
835 if (copy_from_user(&data.contr, argp,
836 sizeof(data.contr)))
837 return -EFAULT;
838 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
839 if (cdev->errcode)
840 return -EIO;
841
842 if (copy_to_user(argp, data.manufacturer,
843 sizeof(data.manufacturer)))
844 return -EFAULT;
845
846 }
847 return 0;
848 case CAPI_GET_ERRCODE:
849 data.errcode = cdev->errcode;
850 cdev->errcode = CAPI_NOERROR;
851 if (arg) {
852 if (copy_to_user(argp, &data.errcode,
853 sizeof(data.errcode)))
854 return -EFAULT;
855 }
856 return data.errcode;
857
858 case CAPI_INSTALLED:
859 if (capi20_isinstalled() == CAPI_NOERROR)
860 return 0;
861 return -ENXIO;
862
863 case CAPI_MANUFACTURER_CMD:
864 {
865 struct capi_manufacturer_cmd mcmd;
866 if (!capable(CAP_SYS_ADMIN))
867 return -EPERM;
868 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
869 return -EFAULT;
870 return capi20_manufacturer(mcmd.cmd, mcmd.data);
871 }
872 return 0;
873
874 case CAPI_SET_FLAGS:
Jan Kiszka05b41492010-02-08 10:12:19 +0000875 case CAPI_CLR_FLAGS: {
876 unsigned userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Jan Kiszka05b41492010-02-08 10:12:19 +0000878 if (copy_from_user(&userflags, argp, sizeof(userflags)))
879 return -EFAULT;
880
881 mutex_lock(&cdev->lock);
882 if (cmd == CAPI_SET_FLAGS)
883 cdev->userflags |= userflags;
884 else
885 cdev->userflags &= ~userflags;
886 mutex_unlock(&cdev->lock);
887 return 0;
888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 case CAPI_GET_FLAGS:
890 if (copy_to_user(argp, &cdev->userflags,
891 sizeof(cdev->userflags)))
892 return -EFAULT;
893 return 0;
894
Jan Kiszka05b41492010-02-08 10:12:19 +0000895 case CAPI_NCCI_OPENCOUNT: {
896 struct capincci *nccip;
897 unsigned ncci;
898 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Jan Kiszka05b41492010-02-08 10:12:19 +0000900 if (copy_from_user(&ncci, argp, sizeof(ncci)))
901 return -EFAULT;
902
903 mutex_lock(&cdev->lock);
904 nccip = capincci_find(cdev, (u32)ncci);
905 if (nccip)
906 count = capincci_minor_opencount(nccip);
907 mutex_unlock(&cdev->lock);
908 return count;
909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka05b41492010-02-08 10:12:19 +0000912 case CAPI_NCCI_GETUNIT: {
913 struct capincci *nccip;
914 struct capiminor *mp;
915 unsigned ncci;
916 int unit = -ESRCH;
917
918 if (copy_from_user(&ncci, argp, sizeof(ncci)))
919 return -EFAULT;
920
921 mutex_lock(&cdev->lock);
922 nccip = capincci_find(cdev, (u32)ncci);
923 if (nccip) {
924 mp = nccip->minorp;
925 if (mp)
926 unit = mp->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000928 mutex_unlock(&cdev->lock);
929 return unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000931#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
932
933 default:
934 return -EINVAL;
935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000938static int capi_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000940 struct capidev *cdev;
941
942 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
943 if (!cdev)
944 return -ENOMEM;
945
Jan Kiszka05b41492010-02-08 10:12:19 +0000946 mutex_init(&cdev->lock);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000947 skb_queue_head_init(&cdev->recvqueue);
948 init_waitqueue_head(&cdev->recvwait);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000949 INIT_LIST_HEAD(&cdev->nccis);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000950 file->private_data = cdev;
951
952 mutex_lock(&capidev_list_lock);
953 list_add_tail(&cdev->list, &capidev_list);
954 mutex_unlock(&capidev_list_lock);
955
956 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000959static int capi_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000961 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000963 mutex_lock(&capidev_list_lock);
964 list_del(&cdev->list);
965 mutex_unlock(&capidev_list_lock);
966
Jan Kiszka05b41492010-02-08 10:12:19 +0000967 if (cdev->ap.applid)
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000968 capi20_release(&cdev->ap);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000969 skb_queue_purge(&cdev->recvqueue);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000970 capincci_free(cdev, 0xffffffff);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000971
972 kfree(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return 0;
974}
975
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800976static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 .owner = THIS_MODULE,
979 .llseek = no_llseek,
980 .read = capi_read,
981 .write = capi_write,
982 .poll = capi_poll,
983 .ioctl = capi_ioctl,
984 .open = capi_open,
985 .release = capi_release,
986};
987
988#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
989/* -------- tty_operations for capincci ----------------------------- */
990
991static int capinc_tty_open(struct tty_struct * tty, struct file * file)
992{
993 struct capiminor *mp;
Michael Buesch053b47f2007-02-12 00:53:26 -0800994 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000996 mp = capiminor_get(iminor(file->f_path.dentry->d_inode));
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700997 if (mp->nccip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return -ENXIO;
999
1000 tty->driver_data = (void *)mp;
1001
Michael Buesch053b47f2007-02-12 00:53:26 -08001002 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (atomic_read(&mp->ttyopencount) == 0)
1004 mp->tty = tty;
1005 atomic_inc(&mp->ttyopencount);
1006#ifdef _DEBUG_REFCOUNT
1007 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1008#endif
1009 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001010 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return 0;
1012}
1013
1014static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1015{
1016 struct capiminor *mp;
1017
1018 mp = (struct capiminor *)tty->driver_data;
1019 if (mp) {
1020 if (atomic_dec_and_test(&mp->ttyopencount)) {
1021#ifdef _DEBUG_REFCOUNT
1022 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1023#endif
1024 tty->driver_data = NULL;
1025 mp->tty = NULL;
1026 }
1027#ifdef _DEBUG_REFCOUNT
1028 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1029#endif
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001030 if (mp->nccip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 capiminor_free(mp);
1032 }
1033
1034#ifdef _DEBUG_REFCOUNT
1035 printk(KERN_DEBUG "capinc_tty_close\n");
1036#endif
1037}
1038
1039static int capinc_tty_write(struct tty_struct * tty,
1040 const unsigned char *buf, int count)
1041{
1042 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1043 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001044 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046#ifdef _DEBUG_TTYFUNCS
1047 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1048#endif
1049
1050 if (!mp || !mp->nccip) {
1051#ifdef _DEBUG_TTYFUNCS
1052 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1053#endif
1054 return 0;
1055 }
1056
Michael Buesch053b47f2007-02-12 00:53:26 -08001057 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 skb = mp->ttyskb;
1059 if (skb) {
1060 mp->ttyskb = NULL;
1061 skb_queue_tail(&mp->outqueue, skb);
1062 mp->outbytes += skb->len;
1063 }
1064
1065 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1066 if (!skb) {
1067 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
Michael Buesch053b47f2007-02-12 00:53:26 -08001068 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return -ENOMEM;
1070 }
1071
1072 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1073 memcpy(skb_put(skb, count), buf, count);
1074
1075 skb_queue_tail(&mp->outqueue, skb);
1076 mp->outbytes += skb->len;
1077 (void)handle_minor_send(mp);
1078 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001079 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return count;
1081}
1082
Alan Coxf2545a72008-04-30 00:54:09 -07001083static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1086 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001087 unsigned long flags;
Alan Coxf2545a72008-04-30 00:54:09 -07001088 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090#ifdef _DEBUG_TTYFUNCS
1091 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1092#endif
1093
1094 if (!mp || !mp->nccip) {
1095#ifdef _DEBUG_TTYFUNCS
1096 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1097#endif
Alan Coxf2545a72008-04-30 00:54:09 -07001098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100
Michael Buesch053b47f2007-02-12 00:53:26 -08001101 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 skb = mp->ttyskb;
1103 if (skb) {
1104 if (skb_tailroom(skb) > 0) {
1105 *(skb_put(skb, 1)) = ch;
Michael Buesch053b47f2007-02-12 00:53:26 -08001106 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001107 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
1109 mp->ttyskb = NULL;
1110 skb_queue_tail(&mp->outqueue, skb);
1111 mp->outbytes += skb->len;
1112 (void)handle_minor_send(mp);
1113 }
1114 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1115 if (skb) {
1116 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1117 *(skb_put(skb, 1)) = ch;
1118 mp->ttyskb = skb;
1119 } else {
1120 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
Alan Coxf2545a72008-04-30 00:54:09 -07001121 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
Michael Buesch053b47f2007-02-12 00:53:26 -08001123 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001124 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
1127static void capinc_tty_flush_chars(struct tty_struct *tty)
1128{
1129 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1130 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001131 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133#ifdef _DEBUG_TTYFUNCS
1134 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1135#endif
1136
1137 if (!mp || !mp->nccip) {
1138#ifdef _DEBUG_TTYFUNCS
1139 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1140#endif
1141 return;
1142 }
1143
Michael Buesch053b47f2007-02-12 00:53:26 -08001144 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 skb = mp->ttyskb;
1146 if (skb) {
1147 mp->ttyskb = NULL;
1148 skb_queue_tail(&mp->outqueue, skb);
1149 mp->outbytes += skb->len;
1150 (void)handle_minor_send(mp);
1151 }
1152 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001153 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
1156static int capinc_tty_write_room(struct tty_struct *tty)
1157{
1158 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1159 int room;
1160 if (!mp || !mp->nccip) {
1161#ifdef _DEBUG_TTYFUNCS
1162 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1163#endif
1164 return 0;
1165 }
1166 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1167 room *= CAPI_MAX_BLKSIZE;
1168#ifdef _DEBUG_TTYFUNCS
1169 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1170#endif
1171 return room;
1172}
1173
Adrian Bunk408b6642005-05-01 08:59:29 -07001174static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
1176 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1177 if (!mp || !mp->nccip) {
1178#ifdef _DEBUG_TTYFUNCS
1179 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1180#endif
1181 return 0;
1182 }
1183#ifdef _DEBUG_TTYFUNCS
1184 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1185 mp->outbytes, mp->nack,
1186 skb_queue_len(&mp->outqueue),
1187 skb_queue_len(&mp->inqueue));
1188#endif
1189 return mp->outbytes;
1190}
1191
1192static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1193 unsigned int cmd, unsigned long arg)
1194{
1195 int error = 0;
1196 switch (cmd) {
1197 default:
Stephen Rothwell53e86312008-10-13 10:44:33 +01001198 error = n_tty_ioctl_helper(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 break;
1200 }
1201 return error;
1202}
1203
Alan Cox606d0992006-12-08 02:38:45 -08001204static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
1206#ifdef _DEBUG_TTYFUNCS
1207 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1208#endif
1209}
1210
1211static void capinc_tty_throttle(struct tty_struct * tty)
1212{
1213 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1214#ifdef _DEBUG_TTYFUNCS
1215 printk(KERN_DEBUG "capinc_tty_throttle\n");
1216#endif
1217 if (mp)
1218 mp->ttyinstop = 1;
1219}
1220
1221static void capinc_tty_unthrottle(struct tty_struct * tty)
1222{
1223 struct capiminor *mp = (struct capiminor *)tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001224 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225#ifdef _DEBUG_TTYFUNCS
1226 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1227#endif
1228 if (mp) {
Michael Buesch053b47f2007-02-12 00:53:26 -08001229 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 mp->ttyinstop = 0;
1231 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001232 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234}
1235
1236static void capinc_tty_stop(struct tty_struct *tty)
1237{
1238 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1239#ifdef _DEBUG_TTYFUNCS
1240 printk(KERN_DEBUG "capinc_tty_stop\n");
1241#endif
1242 if (mp) {
1243 mp->ttyoutstop = 1;
1244 }
1245}
1246
1247static void capinc_tty_start(struct tty_struct *tty)
1248{
1249 struct capiminor *mp = (struct capiminor *)tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001250 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251#ifdef _DEBUG_TTYFUNCS
1252 printk(KERN_DEBUG "capinc_tty_start\n");
1253#endif
1254 if (mp) {
Michael Buesch053b47f2007-02-12 00:53:26 -08001255 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 mp->ttyoutstop = 0;
1257 (void)handle_minor_send(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001258 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260}
1261
1262static void capinc_tty_hangup(struct tty_struct *tty)
1263{
1264#ifdef _DEBUG_TTYFUNCS
1265 printk(KERN_DEBUG "capinc_tty_hangup\n");
1266#endif
1267}
1268
Alan Cox9e989662008-07-22 11:18:03 +01001269static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271#ifdef _DEBUG_TTYFUNCS
1272 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1273#endif
Alan Cox9e989662008-07-22 11:18:03 +01001274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
1277static void capinc_tty_flush_buffer(struct tty_struct *tty)
1278{
1279#ifdef _DEBUG_TTYFUNCS
1280 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1281#endif
1282}
1283
1284static void capinc_tty_set_ldisc(struct tty_struct *tty)
1285{
1286#ifdef _DEBUG_TTYFUNCS
1287 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1288#endif
1289}
1290
1291static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1292{
1293#ifdef _DEBUG_TTYFUNCS
1294 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1295#endif
1296}
1297
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001298static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 .open = capinc_tty_open,
1300 .close = capinc_tty_close,
1301 .write = capinc_tty_write,
1302 .put_char = capinc_tty_put_char,
1303 .flush_chars = capinc_tty_flush_chars,
1304 .write_room = capinc_tty_write_room,
1305 .chars_in_buffer = capinc_tty_chars_in_buffer,
1306 .ioctl = capinc_tty_ioctl,
1307 .set_termios = capinc_tty_set_termios,
1308 .throttle = capinc_tty_throttle,
1309 .unthrottle = capinc_tty_unthrottle,
1310 .stop = capinc_tty_stop,
1311 .start = capinc_tty_start,
1312 .hangup = capinc_tty_hangup,
1313 .break_ctl = capinc_tty_break_ctl,
1314 .flush_buffer = capinc_tty_flush_buffer,
1315 .set_ldisc = capinc_tty_set_ldisc,
1316 .send_xchar = capinc_tty_send_xchar,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317};
1318
Jan Kiszkae76b1542010-02-08 10:12:24 +00001319static int __init capinc_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 struct tty_driver *drv;
Jan Kiszkae76b1542010-02-08 10:12:24 +00001322 int err;
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (capi_ttyminors > CAPINC_MAX_PORTS)
1325 capi_ttyminors = CAPINC_MAX_PORTS;
1326 if (capi_ttyminors <= 0)
1327 capi_ttyminors = CAPINC_NR_PORTS;
1328
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001329 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1330 GFP_KERNEL);
1331 if (!capiminors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return -ENOMEM;
1333
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001334 drv = alloc_tty_driver(capi_ttyminors);
1335 if (!drv) {
1336 kfree(capiminors);
1337 return -ENOMEM;
1338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 drv->owner = THIS_MODULE;
1340 drv->driver_name = "capi_nc";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 drv->name = "capi";
Jan Kiszkae95ac142010-02-08 10:12:26 +00001342 drv->major = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 drv->minor_start = 0;
1344 drv->type = TTY_DRIVER_TYPE_SERIAL;
1345 drv->subtype = SERIAL_TYPE_NORMAL;
1346 drv->init_termios = tty_std_termios;
1347 drv->init_termios.c_iflag = ICRNL;
1348 drv->init_termios.c_oflag = OPOST | ONLCR;
1349 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1350 drv->init_termios.c_lflag = 0;
Jan Kiszka40fb2d02010-02-08 10:12:25 +00001351 drv->flags =
1352 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1353 TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 tty_set_operations(drv, &capinc_ops);
Jan Kiszkae76b1542010-02-08 10:12:24 +00001355
1356 err = tty_register_driver(drv);
1357 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 put_tty_driver(drv);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001359 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 printk(KERN_ERR "Couldn't register capi_nc driver\n");
Jan Kiszkae76b1542010-02-08 10:12:24 +00001361 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363 capinc_tty_driver = drv;
1364 return 0;
1365}
1366
Jan Kiszkae76b1542010-02-08 10:12:24 +00001367static void __exit capinc_tty_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Jan Kiszkae76b1542010-02-08 10:12:24 +00001369 tty_unregister_driver(capinc_tty_driver);
1370 put_tty_driver(capinc_tty_driver);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001371 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
1373
Jan Kiszka501c87a2010-02-08 10:12:16 +00001374#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1375
1376static inline int capinc_tty_init(void)
1377{
1378 return 0;
1379}
1380
1381static inline void capinc_tty_exit(void) { }
1382
1383#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385/* -------- /proc functions ----------------------------------------- */
1386
1387/*
1388 * /proc/capi/capi20:
1389 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1390 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001391static int capi20_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 struct capidev *cdev;
1394 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001396 mutex_lock(&capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 list_for_each(l, &capidev_list) {
1398 cdev = list_entry(l, struct capidev, list);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001399 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 cdev->ap.applid,
1401 cdev->ap.nrecvctlpkt,
1402 cdev->ap.nrecvdatapkt,
1403 cdev->ap.nsentctlpkt,
1404 cdev->ap.nsentdatapkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001406 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001410static int capi20_proc_open(struct inode *inode, struct file *file)
1411{
1412 return single_open(file, capi20_proc_show, NULL);
1413}
1414
1415static const struct file_operations capi20_proc_fops = {
1416 .owner = THIS_MODULE,
1417 .open = capi20_proc_open,
1418 .read = seq_read,
1419 .llseek = seq_lseek,
1420 .release = single_release,
1421};
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423/*
1424 * /proc/capi/capi20ncci:
1425 * applid ncci
1426 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001427static int capi20ncci_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Jan Kiszka884f5c42010-02-08 10:12:22 +00001429 struct capidev *cdev;
1430 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001432 mutex_lock(&capidev_list_lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001433 list_for_each_entry(cdev, &capidev_list, list) {
Jan Kiszka05b41492010-02-08 10:12:19 +00001434 mutex_lock(&cdev->lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001435 list_for_each_entry(np, &cdev->nccis, list)
1436 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
Jan Kiszka05b41492010-02-08 10:12:19 +00001437 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001439 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001440 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001443static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1444{
1445 return single_open(file, capi20ncci_proc_show, NULL);
1446}
1447
1448static const struct file_operations capi20ncci_proc_fops = {
1449 .owner = THIS_MODULE,
1450 .open = capi20ncci_proc_open,
1451 .read = seq_read,
1452 .llseek = seq_lseek,
1453 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454};
1455
1456static void __init proc_init(void)
1457{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001458 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1459 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
1462static void __exit proc_exit(void)
1463{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001464 remove_proc_entry("capi/capi20", NULL);
1465 remove_proc_entry("capi/capi20ncci", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
1468/* -------- init function and module interface ---------------------- */
1469
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471static int __init capi_init(void)
1472{
Jan Kiszka88549d62010-02-08 10:12:09 +00001473 const char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001474 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Andrew Morton6d9eac32006-03-28 01:56:19 -08001476 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1477 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001479 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001481 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if (IS_ERR(capi_class)) {
1483 unregister_chrdev(capi_major, "capi20");
1484 return PTR_ERR(capi_class);
1485 }
1486
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -07001487 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (capinc_tty_init() < 0) {
Tony Jonesd78b0362007-09-25 02:03:03 +02001490 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001491 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 unregister_chrdev(capi_major, "capi20");
1493 return -ENOMEM;
1494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 proc_init();
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1499 compileinfo = " (middleware+capifs)";
Jan Kiszka501c87a2010-02-08 10:12:16 +00001500#elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 compileinfo = " (no capifs)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502#else
1503 compileinfo = " (no middleware)";
1504#endif
Jan Kiszka88549d62010-02-08 10:12:09 +00001505 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1506 capi_major, compileinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 return 0;
1509}
1510
1511static void __exit capi_exit(void)
1512{
1513 proc_exit();
1514
Tony Jonesd78b0362007-09-25 02:03:03 +02001515 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001516 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 capinc_tty_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
1522module_init(capi_init);
1523module_exit(capi_exit);