blob: 732cdb585b2de53a30062644a34687c47f751e35 [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 {
Jan Kiszka0159d542010-02-08 10:12:27 +000086 struct kref kref;
87
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
Jan Kiszka0159d542010-02-08 10:12:27 +0000228 kref_init(&mp->kref);
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 mp->ap = ap;
231 mp->ncci = ncci;
232 mp->msgid = 0;
233 atomic_set(&mp->ttyopencount,0);
Michael Buesch6aa65472006-06-26 00:25:30 -0700234 INIT_LIST_HEAD(&mp->ackqueue);
235 spin_lock_init(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 skb_queue_head_init(&mp->inqueue);
238 skb_queue_head_init(&mp->outqueue);
239
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000240 /* Allocate the least unused minor number. */
241 write_lock_irqsave(&capiminors_lock, flags);
242 for (minor = 0; minor < capi_ttyminors; minor++)
243 if (!capiminors[minor]) {
244 capiminors[minor] = mp;
245 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000247 write_unlock_irqrestore(&capiminors_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000249 if (minor == capi_ttyminors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 printk(KERN_NOTICE "capi: out of minors\n");
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000251 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000254 mp->minor = minor;
255
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000256 dev = tty_register_device(capinc_tty_driver, minor, NULL);
257 if (IS_ERR(dev))
258 goto err_out2;
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000261
262err_out2:
263 write_lock_irqsave(&capiminors_lock, flags);
264 capiminors[minor] = NULL;
265 write_unlock_irqrestore(&capiminors_lock, flags);
266
267err_out1:
268 kfree(mp);
269 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Jan Kiszka0159d542010-02-08 10:12:27 +0000272static void capiminor_destroy(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Jan Kiszka0159d542010-02-08 10:12:27 +0000274 struct capiminor *mp = container_of(kref, struct capiminor, kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Wei Yongjund46604e2009-02-25 00:12:09 +0000276 kfree_skb(mp->ttyskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 skb_queue_purge(&mp->inqueue);
278 skb_queue_purge(&mp->outqueue);
279 capiminor_del_all_ack(mp);
280 kfree(mp);
281}
282
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000283static struct capiminor *capiminor_get(unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000285 struct capiminor *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000287 read_lock(&capiminors_lock);
288 mp = capiminors[minor];
Jan Kiszka0159d542010-02-08 10:12:27 +0000289 if (mp)
290 kref_get(&mp->kref);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000291 read_unlock(&capiminors_lock);
292
293 return mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Jan Kiszka0159d542010-02-08 10:12:27 +0000296static inline void capiminor_put(struct capiminor *mp)
297{
298 kref_put(&mp->kref, capiminor_destroy);
299}
300
301static void capiminor_free(struct capiminor *mp)
302{
303 unsigned long flags;
304
305 tty_unregister_device(capinc_tty_driver, mp->minor);
306
307 write_lock_irqsave(&capiminors_lock, flags);
308 capiminors[mp->minor] = NULL;
309 write_unlock_irqrestore(&capiminors_lock, flags);
310
311 capiminor_put(mp);
312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/* -------- struct capincci ----------------------------------------- */
315
Jan Kiszka501c87a2010-02-08 10:12:16 +0000316static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Jan Kiszka501c87a2010-02-08 10:12:16 +0000318 struct capiminor *mp;
Jan Kiszkae95ac142010-02-08 10:12:26 +0000319 dev_t device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Jan Kiszka501c87a2010-02-08 10:12:16 +0000321 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
322 return;
323
324 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (mp) {
326 mp->nccip = np;
327#ifdef _DEBUG_REFCOUNT
328 printk(KERN_DEBUG "set mp->nccip\n");
329#endif
Jan Kiszkae95ac142010-02-08 10:12:26 +0000330 device = MKDEV(capinc_tty_driver->major, mp->minor);
331 mp->capifs_dentry = capifs_new_ncci(mp->minor, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000333}
334
335static void capincci_free_minor(struct capincci *np)
336{
337 struct capiminor *mp = np->minorp;
338
339 if (mp) {
340 capifs_free_ncci(mp->capifs_dentry);
341 if (mp->tty) {
342 mp->nccip = NULL;
343#ifdef _DEBUG_REFCOUNT
344 printk(KERN_DEBUG "reset mp->nccip\n");
345#endif
346 tty_hangup(mp->tty);
347 } else {
348 capiminor_free(mp);
349 }
350 }
351}
352
353static inline unsigned int capincci_minor_opencount(struct capincci *np)
354{
355 struct capiminor *mp = np->minorp;
356
357 return mp ? atomic_read(&mp->ttyopencount) : 0;
358}
359
360#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
361
362static inline void
363capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
364static inline void capincci_free_minor(struct capincci *np) { }
365
366static inline unsigned int capincci_minor_opencount(struct capincci *np)
367{
368 return 0;
369}
370
371#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
372
373static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
374{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000375 struct capincci *np;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000376
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000377 np = kzalloc(sizeof(*np), GFP_KERNEL);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000378 if (!np)
379 return NULL;
380 np->ncci = ncci;
381 np->cdev = cdev;
382
383 capincci_alloc_minor(cdev, np);
384
Jan Kiszka884f5c42010-02-08 10:12:22 +0000385 list_add_tail(&np->list, &cdev->nccis);
386
387 return np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390static void capincci_free(struct capidev *cdev, u32 ncci)
391{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000392 struct capincci *np, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Jan Kiszka884f5c42010-02-08 10:12:22 +0000394 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (ncci == 0xffffffff || np->ncci == ncci) {
Jan Kiszka501c87a2010-02-08 10:12:16 +0000396 capincci_free_minor(np);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000397 list_del(&np->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 kfree(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
403{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000404 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Jan Kiszka884f5c42010-02-08 10:12:22 +0000406 list_for_each_entry(np, &cdev->nccis, list)
407 if (np->ncci == ncci)
408 return np;
409 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
413/* -------- handle data queue --------------------------------------- */
414
415static struct sk_buff *
416gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
417{
418 struct sk_buff *nskb;
419 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
420 if (nskb) {
421 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
422 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
423 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
424 capimsg_setu16(s, 2, mp->ap->applid);
425 capimsg_setu8 (s, 4, CAPI_DATA_B3);
426 capimsg_setu8 (s, 5, CAPI_RESP);
427 capimsg_setu16(s, 6, mp->msgid++);
428 capimsg_setu32(s, 8, mp->ncci);
429 capimsg_setu16(s, 12, datahandle);
430 }
431 return nskb;
432}
433
434static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
435{
436 struct sk_buff *nskb;
437 int datalen;
438 u16 errcode, datahandle;
439 struct tty_ldisc *ld;
440
441 datalen = skb->len - CAPIMSG_LEN(skb->data);
442 if (mp->tty == NULL)
443 {
444#ifdef _DEBUG_DATAFLOW
445 printk(KERN_DEBUG "capi: currently no receiver\n");
446#endif
447 return -1;
448 }
449
450 ld = tty_ldisc_ref(mp->tty);
451 if (ld == NULL)
452 return -1;
Alan Coxa352def2008-07-16 21:53:12 +0100453 if (ld->ops->receive_buf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
455 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
456#endif
457 goto bad;
458 }
459 if (mp->ttyinstop) {
460#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
461 printk(KERN_DEBUG "capi: recv tty throttled\n");
462#endif
463 goto bad;
464 }
Alan Cox33f0f882006-01-09 20:54:13 -0800465 if (mp->tty->receive_room < datalen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
467 printk(KERN_DEBUG "capi: no room in tty\n");
468#endif
469 goto bad;
470 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700471 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
473 goto bad;
474 }
475 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
476 errcode = capi20_put_message(mp->ap, nskb);
477 if (errcode != CAPI_NOERROR) {
478 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
479 errcode);
480 kfree_skb(nskb);
481 goto bad;
482 }
483 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
484#ifdef _DEBUG_DATAFLOW
485 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
486 datahandle, skb->len);
487#endif
Alan Coxa352def2008-07-16 21:53:12 +0100488 ld->ops->receive_buf(mp->tty, skb->data, NULL, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 kfree_skb(skb);
490 tty_ldisc_deref(ld);
491 return 0;
492bad:
493 tty_ldisc_deref(ld);
494 return -1;
495}
496
497static void handle_minor_recv(struct capiminor *mp)
498{
499 struct sk_buff *skb;
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700500 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 unsigned int len = skb->len;
502 mp->inbytes -= len;
503 if (handle_recv_skb(mp, skb) < 0) {
504 skb_queue_head(&mp->inqueue, skb);
505 mp->inbytes += len;
506 return;
507 }
508 }
509}
510
511static int handle_minor_send(struct capiminor *mp)
512{
513 struct sk_buff *skb;
514 u16 len;
515 int count = 0;
516 u16 errcode;
517 u16 datahandle;
518
519 if (mp->tty && mp->ttyoutstop) {
520#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
521 printk(KERN_DEBUG "capi: send: tty stopped\n");
522#endif
523 return 0;
524 }
525
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700526 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 datahandle = mp->datahandle;
528 len = (u16)skb->len;
529 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
530 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
531 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
532 capimsg_setu16(skb->data, 2, mp->ap->applid);
533 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
534 capimsg_setu8 (skb->data, 5, CAPI_REQ);
535 capimsg_setu16(skb->data, 6, mp->msgid++);
536 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
Andrew Morton5e6c20a2007-07-17 04:04:22 -0700537 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 capimsg_setu16(skb->data, 16, len); /* Data length */
539 capimsg_setu16(skb->data, 18, datahandle);
540 capimsg_setu16(skb->data, 20, 0); /* Flags */
541
Jan Kiszka501c87a2010-02-08 10:12:16 +0000542 if (capiminor_add_ack(mp, datahandle) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
544 skb_queue_head(&mp->outqueue, skb);
545 return count;
546 }
547 errcode = capi20_put_message(mp->ap, skb);
548 if (errcode == CAPI_NOERROR) {
549 mp->datahandle++;
550 count++;
551 mp->outbytes -= len;
552#ifdef _DEBUG_DATAFLOW
553 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
554 datahandle, len);
555#endif
556 continue;
557 }
558 capiminor_del_ack(mp, datahandle);
559
560 if (errcode == CAPI_SENDQUEUEFULL) {
561 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
562 skb_queue_head(&mp->outqueue, skb);
563 break;
564 }
565
566 /* ups, drop packet */
567 printk(KERN_ERR "capi: put_message = %x\n", errcode);
568 mp->outbytes -= len;
569 kfree_skb(skb);
570 }
571 return count;
572}
573
574#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
575/* -------- function called by lower level -------------------------- */
576
577static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
578{
579 struct capidev *cdev = ap->private;
580#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
581 struct capiminor *mp;
582 u16 datahandle;
583#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
584 struct capincci *np;
Michael Buesch053b47f2007-02-12 00:53:26 -0800585 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Jan Kiszka05b41492010-02-08 10:12:19 +0000587 mutex_lock(&cdev->lock);
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
590 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
Jan Kiszka05b41492010-02-08 10:12:19 +0000591 if ((info & 0xff00) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000594 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000596
Michael Buesch053b47f2007-02-12 00:53:26 -0800597 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
599 skb_queue_tail(&cdev->recvqueue, skb);
600 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000601 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000603
604 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (!np) {
606 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
607 skb_queue_tail(&cdev->recvqueue, skb);
608 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000609 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
613 skb_queue_tail(&cdev->recvqueue, skb);
614 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 mp = np->minorp;
619 if (!mp) {
620 skb_queue_tail(&cdev->recvqueue, skb);
621 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000622 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
626#ifdef _DEBUG_DATAFLOW
627 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
628 datahandle, skb->len-CAPIMSG_LEN(skb->data));
629#endif
630 skb_queue_tail(&mp->inqueue, skb);
631 mp->inbytes += skb->len;
632 handle_minor_recv(mp);
633
634 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
635
636 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
637#ifdef _DEBUG_DATAFLOW
638 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
639 datahandle,
640 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
641#endif
642 kfree_skb(skb);
643 (void)capiminor_del_ack(mp, datahandle);
644 if (mp->tty)
645 tty_wakeup(mp->tty);
646 (void)handle_minor_send(mp);
647
648 } else {
649 /* ups, let capi application handle it :-) */
650 skb_queue_tail(&cdev->recvqueue, skb);
651 wake_up_interruptible(&cdev->recvwait);
652 }
653#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000654
Jan Kiszka05b41492010-02-08 10:12:19 +0000655unlock_out:
Michael Buesch053b47f2007-02-12 00:53:26 -0800656 spin_unlock_irqrestore(&workaround_lock, flags);
Jan Kiszka05b41492010-02-08 10:12:19 +0000657 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660/* -------- file_operations for capidev ----------------------------- */
661
662static ssize_t
663capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
664{
665 struct capidev *cdev = (struct capidev *)file->private_data;
666 struct sk_buff *skb;
667 size_t copied;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000668 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 if (!cdev->ap.applid)
671 return -ENODEV;
672
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000673 skb = skb_dequeue(&cdev->recvqueue);
674 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (file->f_flags & O_NONBLOCK)
676 return -EAGAIN;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000677 err = wait_event_interruptible(cdev->recvwait,
678 (skb = skb_dequeue(&cdev->recvqueue)));
679 if (err)
680 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682 if (skb->len > count) {
683 skb_queue_head(&cdev->recvqueue, skb);
684 return -EMSGSIZE;
685 }
686 if (copy_to_user(buf, skb->data, skb->len)) {
687 skb_queue_head(&cdev->recvqueue, skb);
688 return -EFAULT;
689 }
690 copied = skb->len;
691
692 kfree_skb(skb);
693
694 return copied;
695}
696
697static ssize_t
698capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
699{
700 struct capidev *cdev = (struct capidev *)file->private_data;
701 struct sk_buff *skb;
702 u16 mlen;
703
704 if (!cdev->ap.applid)
705 return -ENODEV;
706
707 skb = alloc_skb(count, GFP_USER);
708 if (!skb)
709 return -ENOMEM;
710
711 if (copy_from_user(skb_put(skb, count), buf, count)) {
712 kfree_skb(skb);
713 return -EFAULT;
714 }
715 mlen = CAPIMSG_LEN(skb->data);
716 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
717 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
718 kfree_skb(skb);
719 return -EINVAL;
720 }
721 } else {
722 if (mlen != count) {
723 kfree_skb(skb);
724 return -EINVAL;
725 }
726 }
727 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
728
729 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Jan Kiszka05b41492010-02-08 10:12:19 +0000730 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000732 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
735 cdev->errcode = capi20_put_message(&cdev->ap, skb);
736
737 if (cdev->errcode) {
738 kfree_skb(skb);
739 return -EIO;
740 }
741 return count;
742}
743
744static unsigned int
745capi_poll(struct file *file, poll_table * wait)
746{
747 struct capidev *cdev = (struct capidev *)file->private_data;
748 unsigned int mask = 0;
749
750 if (!cdev->ap.applid)
751 return POLLERR;
752
753 poll_wait(file, &(cdev->recvwait), wait);
754 mask = POLLOUT | POLLWRNORM;
755 if (!skb_queue_empty(&cdev->recvqueue))
756 mask |= POLLIN | POLLRDNORM;
757 return mask;
758}
759
760static int
761capi_ioctl(struct inode *inode, struct file *file,
762 unsigned int cmd, unsigned long arg)
763{
764 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 capi_ioctl_struct data;
766 int retval = -EINVAL;
767 void __user *argp = (void __user *)arg;
768
769 switch (cmd) {
770 case CAPI_REGISTER:
Jan Kiszka05b41492010-02-08 10:12:19 +0000771 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Jan Kiszka05b41492010-02-08 10:12:19 +0000773 if (cdev->ap.applid) {
774 retval = -EEXIST;
775 goto register_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000777 if (copy_from_user(&cdev->ap.rparam, argp,
778 sizeof(struct capi_register_params))) {
779 retval = -EFAULT;
780 goto register_out;
781 }
782 cdev->ap.private = cdev;
783 cdev->ap.recv_message = capi_recv_message;
784 cdev->errcode = capi20_register(&cdev->ap);
785 retval = (int)cdev->ap.applid;
786 if (cdev->errcode) {
787 cdev->ap.applid = 0;
788 retval = -EIO;
789 }
790
791register_out:
792 mutex_unlock(&cdev->lock);
793 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 case CAPI_GET_VERSION:
796 {
797 if (copy_from_user(&data.contr, argp,
798 sizeof(data.contr)))
799 return -EFAULT;
800 cdev->errcode = capi20_get_version(data.contr, &data.version);
801 if (cdev->errcode)
802 return -EIO;
803 if (copy_to_user(argp, &data.version,
804 sizeof(data.version)))
805 return -EFAULT;
806 }
807 return 0;
808
809 case CAPI_GET_SERIAL:
810 {
811 if (copy_from_user(&data.contr, argp,
812 sizeof(data.contr)))
813 return -EFAULT;
814 cdev->errcode = capi20_get_serial (data.contr, data.serial);
815 if (cdev->errcode)
816 return -EIO;
817 if (copy_to_user(argp, data.serial,
818 sizeof(data.serial)))
819 return -EFAULT;
820 }
821 return 0;
822 case CAPI_GET_PROFILE:
823 {
824 if (copy_from_user(&data.contr, argp,
825 sizeof(data.contr)))
826 return -EFAULT;
827
828 if (data.contr == 0) {
829 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
830 if (cdev->errcode)
831 return -EIO;
832
833 retval = copy_to_user(argp,
834 &data.profile.ncontroller,
835 sizeof(data.profile.ncontroller));
836
837 } else {
838 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
839 if (cdev->errcode)
840 return -EIO;
841
842 retval = copy_to_user(argp, &data.profile,
843 sizeof(data.profile));
844 }
845 if (retval)
846 return -EFAULT;
847 }
848 return 0;
849
850 case CAPI_GET_MANUFACTURER:
851 {
852 if (copy_from_user(&data.contr, argp,
853 sizeof(data.contr)))
854 return -EFAULT;
855 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
856 if (cdev->errcode)
857 return -EIO;
858
859 if (copy_to_user(argp, data.manufacturer,
860 sizeof(data.manufacturer)))
861 return -EFAULT;
862
863 }
864 return 0;
865 case CAPI_GET_ERRCODE:
866 data.errcode = cdev->errcode;
867 cdev->errcode = CAPI_NOERROR;
868 if (arg) {
869 if (copy_to_user(argp, &data.errcode,
870 sizeof(data.errcode)))
871 return -EFAULT;
872 }
873 return data.errcode;
874
875 case CAPI_INSTALLED:
876 if (capi20_isinstalled() == CAPI_NOERROR)
877 return 0;
878 return -ENXIO;
879
880 case CAPI_MANUFACTURER_CMD:
881 {
882 struct capi_manufacturer_cmd mcmd;
883 if (!capable(CAP_SYS_ADMIN))
884 return -EPERM;
885 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
886 return -EFAULT;
887 return capi20_manufacturer(mcmd.cmd, mcmd.data);
888 }
889 return 0;
890
891 case CAPI_SET_FLAGS:
Jan Kiszka05b41492010-02-08 10:12:19 +0000892 case CAPI_CLR_FLAGS: {
893 unsigned userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Jan Kiszka05b41492010-02-08 10:12:19 +0000895 if (copy_from_user(&userflags, argp, sizeof(userflags)))
896 return -EFAULT;
897
898 mutex_lock(&cdev->lock);
899 if (cmd == CAPI_SET_FLAGS)
900 cdev->userflags |= userflags;
901 else
902 cdev->userflags &= ~userflags;
903 mutex_unlock(&cdev->lock);
904 return 0;
905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 case CAPI_GET_FLAGS:
907 if (copy_to_user(argp, &cdev->userflags,
908 sizeof(cdev->userflags)))
909 return -EFAULT;
910 return 0;
911
Jan Kiszka05b41492010-02-08 10:12:19 +0000912 case CAPI_NCCI_OPENCOUNT: {
913 struct capincci *nccip;
914 unsigned ncci;
915 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Jan Kiszka05b41492010-02-08 10:12:19 +0000917 if (copy_from_user(&ncci, argp, sizeof(ncci)))
918 return -EFAULT;
919
920 mutex_lock(&cdev->lock);
921 nccip = capincci_find(cdev, (u32)ncci);
922 if (nccip)
923 count = capincci_minor_opencount(nccip);
924 mutex_unlock(&cdev->lock);
925 return count;
926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka05b41492010-02-08 10:12:19 +0000929 case CAPI_NCCI_GETUNIT: {
930 struct capincci *nccip;
931 struct capiminor *mp;
932 unsigned ncci;
933 int unit = -ESRCH;
934
935 if (copy_from_user(&ncci, argp, sizeof(ncci)))
936 return -EFAULT;
937
938 mutex_lock(&cdev->lock);
939 nccip = capincci_find(cdev, (u32)ncci);
940 if (nccip) {
941 mp = nccip->minorp;
942 if (mp)
943 unit = mp->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000945 mutex_unlock(&cdev->lock);
946 return unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000948#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
949
950 default:
951 return -EINVAL;
952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000955static int capi_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000957 struct capidev *cdev;
958
959 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
960 if (!cdev)
961 return -ENOMEM;
962
Jan Kiszka05b41492010-02-08 10:12:19 +0000963 mutex_init(&cdev->lock);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000964 skb_queue_head_init(&cdev->recvqueue);
965 init_waitqueue_head(&cdev->recvwait);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000966 INIT_LIST_HEAD(&cdev->nccis);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000967 file->private_data = cdev;
968
969 mutex_lock(&capidev_list_lock);
970 list_add_tail(&cdev->list, &capidev_list);
971 mutex_unlock(&capidev_list_lock);
972
973 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000976static int capi_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000978 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000980 mutex_lock(&capidev_list_lock);
981 list_del(&cdev->list);
982 mutex_unlock(&capidev_list_lock);
983
Jan Kiszka05b41492010-02-08 10:12:19 +0000984 if (cdev->ap.applid)
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000985 capi20_release(&cdev->ap);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000986 skb_queue_purge(&cdev->recvqueue);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000987 capincci_free(cdev, 0xffffffff);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000988
989 kfree(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return 0;
991}
992
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800993static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 .owner = THIS_MODULE,
996 .llseek = no_llseek,
997 .read = capi_read,
998 .write = capi_write,
999 .poll = capi_poll,
1000 .ioctl = capi_ioctl,
1001 .open = capi_open,
1002 .release = capi_release,
1003};
1004
1005#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1006/* -------- tty_operations for capincci ----------------------------- */
1007
1008static int capinc_tty_open(struct tty_struct * tty, struct file * file)
1009{
1010 struct capiminor *mp;
Michael Buesch053b47f2007-02-12 00:53:26 -08001011 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Jan Kiszka40fb2d02010-02-08 10:12:25 +00001013 mp = capiminor_get(iminor(file->f_path.dentry->d_inode));
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001014 if (mp->nccip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return -ENXIO;
1016
1017 tty->driver_data = (void *)mp;
1018
Michael Buesch053b47f2007-02-12 00:53:26 -08001019 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (atomic_read(&mp->ttyopencount) == 0)
1021 mp->tty = tty;
1022 atomic_inc(&mp->ttyopencount);
1023#ifdef _DEBUG_REFCOUNT
1024 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1025#endif
1026 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001027 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return 0;
1029}
1030
1031static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1032{
1033 struct capiminor *mp;
1034
1035 mp = (struct capiminor *)tty->driver_data;
1036 if (mp) {
1037 if (atomic_dec_and_test(&mp->ttyopencount)) {
1038#ifdef _DEBUG_REFCOUNT
1039 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1040#endif
1041 tty->driver_data = NULL;
1042 mp->tty = NULL;
1043 }
1044#ifdef _DEBUG_REFCOUNT
1045 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1046#endif
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001047 if (mp->nccip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 capiminor_free(mp);
Jan Kiszka0159d542010-02-08 10:12:27 +00001049
1050 capiminor_put(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052
1053#ifdef _DEBUG_REFCOUNT
1054 printk(KERN_DEBUG "capinc_tty_close\n");
1055#endif
1056}
1057
1058static int capinc_tty_write(struct tty_struct * tty,
1059 const unsigned char *buf, int count)
1060{
1061 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1062 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001063 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065#ifdef _DEBUG_TTYFUNCS
1066 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1067#endif
1068
1069 if (!mp || !mp->nccip) {
1070#ifdef _DEBUG_TTYFUNCS
1071 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1072#endif
1073 return 0;
1074 }
1075
Michael Buesch053b47f2007-02-12 00:53:26 -08001076 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 skb = mp->ttyskb;
1078 if (skb) {
1079 mp->ttyskb = NULL;
1080 skb_queue_tail(&mp->outqueue, skb);
1081 mp->outbytes += skb->len;
1082 }
1083
1084 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1085 if (!skb) {
1086 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
Michael Buesch053b47f2007-02-12 00:53:26 -08001087 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return -ENOMEM;
1089 }
1090
1091 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1092 memcpy(skb_put(skb, count), buf, count);
1093
1094 skb_queue_tail(&mp->outqueue, skb);
1095 mp->outbytes += skb->len;
1096 (void)handle_minor_send(mp);
1097 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001098 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return count;
1100}
1101
Alan Coxf2545a72008-04-30 00:54:09 -07001102static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1105 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001106 unsigned long flags;
Alan Coxf2545a72008-04-30 00:54:09 -07001107 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109#ifdef _DEBUG_TTYFUNCS
1110 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1111#endif
1112
1113 if (!mp || !mp->nccip) {
1114#ifdef _DEBUG_TTYFUNCS
1115 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1116#endif
Alan Coxf2545a72008-04-30 00:54:09 -07001117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119
Michael Buesch053b47f2007-02-12 00:53:26 -08001120 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 skb = mp->ttyskb;
1122 if (skb) {
1123 if (skb_tailroom(skb) > 0) {
1124 *(skb_put(skb, 1)) = ch;
Michael Buesch053b47f2007-02-12 00:53:26 -08001125 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001126 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128 mp->ttyskb = NULL;
1129 skb_queue_tail(&mp->outqueue, skb);
1130 mp->outbytes += skb->len;
1131 (void)handle_minor_send(mp);
1132 }
1133 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1134 if (skb) {
1135 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1136 *(skb_put(skb, 1)) = ch;
1137 mp->ttyskb = skb;
1138 } else {
1139 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
Alan Coxf2545a72008-04-30 00:54:09 -07001140 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
Michael Buesch053b47f2007-02-12 00:53:26 -08001142 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001143 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146static void capinc_tty_flush_chars(struct tty_struct *tty)
1147{
1148 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1149 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001150 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152#ifdef _DEBUG_TTYFUNCS
1153 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1154#endif
1155
1156 if (!mp || !mp->nccip) {
1157#ifdef _DEBUG_TTYFUNCS
1158 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1159#endif
1160 return;
1161 }
1162
Michael Buesch053b47f2007-02-12 00:53:26 -08001163 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 skb = mp->ttyskb;
1165 if (skb) {
1166 mp->ttyskb = NULL;
1167 skb_queue_tail(&mp->outqueue, skb);
1168 mp->outbytes += skb->len;
1169 (void)handle_minor_send(mp);
1170 }
1171 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001172 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
1175static int capinc_tty_write_room(struct tty_struct *tty)
1176{
1177 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1178 int room;
1179 if (!mp || !mp->nccip) {
1180#ifdef _DEBUG_TTYFUNCS
1181 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1182#endif
1183 return 0;
1184 }
1185 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1186 room *= CAPI_MAX_BLKSIZE;
1187#ifdef _DEBUG_TTYFUNCS
1188 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1189#endif
1190 return room;
1191}
1192
Adrian Bunk408b6642005-05-01 08:59:29 -07001193static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1196 if (!mp || !mp->nccip) {
1197#ifdef _DEBUG_TTYFUNCS
1198 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1199#endif
1200 return 0;
1201 }
1202#ifdef _DEBUG_TTYFUNCS
1203 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1204 mp->outbytes, mp->nack,
1205 skb_queue_len(&mp->outqueue),
1206 skb_queue_len(&mp->inqueue));
1207#endif
1208 return mp->outbytes;
1209}
1210
1211static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1212 unsigned int cmd, unsigned long arg)
1213{
1214 int error = 0;
1215 switch (cmd) {
1216 default:
Stephen Rothwell53e86312008-10-13 10:44:33 +01001217 error = n_tty_ioctl_helper(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 break;
1219 }
1220 return error;
1221}
1222
Alan Cox606d0992006-12-08 02:38:45 -08001223static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225#ifdef _DEBUG_TTYFUNCS
1226 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1227#endif
1228}
1229
1230static void capinc_tty_throttle(struct tty_struct * tty)
1231{
1232 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1233#ifdef _DEBUG_TTYFUNCS
1234 printk(KERN_DEBUG "capinc_tty_throttle\n");
1235#endif
1236 if (mp)
1237 mp->ttyinstop = 1;
1238}
1239
1240static void capinc_tty_unthrottle(struct tty_struct * tty)
1241{
1242 struct capiminor *mp = (struct capiminor *)tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001243 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244#ifdef _DEBUG_TTYFUNCS
1245 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1246#endif
1247 if (mp) {
Michael Buesch053b47f2007-02-12 00:53:26 -08001248 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 mp->ttyinstop = 0;
1250 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001251 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253}
1254
1255static void capinc_tty_stop(struct tty_struct *tty)
1256{
1257 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1258#ifdef _DEBUG_TTYFUNCS
1259 printk(KERN_DEBUG "capinc_tty_stop\n");
1260#endif
1261 if (mp) {
1262 mp->ttyoutstop = 1;
1263 }
1264}
1265
1266static void capinc_tty_start(struct tty_struct *tty)
1267{
1268 struct capiminor *mp = (struct capiminor *)tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001269 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270#ifdef _DEBUG_TTYFUNCS
1271 printk(KERN_DEBUG "capinc_tty_start\n");
1272#endif
1273 if (mp) {
Michael Buesch053b47f2007-02-12 00:53:26 -08001274 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 mp->ttyoutstop = 0;
1276 (void)handle_minor_send(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001277 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
1279}
1280
1281static void capinc_tty_hangup(struct tty_struct *tty)
1282{
1283#ifdef _DEBUG_TTYFUNCS
1284 printk(KERN_DEBUG "capinc_tty_hangup\n");
1285#endif
1286}
1287
Alan Cox9e98966c2008-07-22 11:18:03 +01001288static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290#ifdef _DEBUG_TTYFUNCS
1291 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1292#endif
Alan Cox9e98966c2008-07-22 11:18:03 +01001293 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
1296static void capinc_tty_flush_buffer(struct tty_struct *tty)
1297{
1298#ifdef _DEBUG_TTYFUNCS
1299 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1300#endif
1301}
1302
1303static void capinc_tty_set_ldisc(struct tty_struct *tty)
1304{
1305#ifdef _DEBUG_TTYFUNCS
1306 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1307#endif
1308}
1309
1310static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1311{
1312#ifdef _DEBUG_TTYFUNCS
1313 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1314#endif
1315}
1316
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001317static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 .open = capinc_tty_open,
1319 .close = capinc_tty_close,
1320 .write = capinc_tty_write,
1321 .put_char = capinc_tty_put_char,
1322 .flush_chars = capinc_tty_flush_chars,
1323 .write_room = capinc_tty_write_room,
1324 .chars_in_buffer = capinc_tty_chars_in_buffer,
1325 .ioctl = capinc_tty_ioctl,
1326 .set_termios = capinc_tty_set_termios,
1327 .throttle = capinc_tty_throttle,
1328 .unthrottle = capinc_tty_unthrottle,
1329 .stop = capinc_tty_stop,
1330 .start = capinc_tty_start,
1331 .hangup = capinc_tty_hangup,
1332 .break_ctl = capinc_tty_break_ctl,
1333 .flush_buffer = capinc_tty_flush_buffer,
1334 .set_ldisc = capinc_tty_set_ldisc,
1335 .send_xchar = capinc_tty_send_xchar,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336};
1337
Jan Kiszkae76b1542010-02-08 10:12:24 +00001338static int __init capinc_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct tty_driver *drv;
Jan Kiszkae76b1542010-02-08 10:12:24 +00001341 int err;
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (capi_ttyminors > CAPINC_MAX_PORTS)
1344 capi_ttyminors = CAPINC_MAX_PORTS;
1345 if (capi_ttyminors <= 0)
1346 capi_ttyminors = CAPINC_NR_PORTS;
1347
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001348 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1349 GFP_KERNEL);
1350 if (!capiminors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return -ENOMEM;
1352
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001353 drv = alloc_tty_driver(capi_ttyminors);
1354 if (!drv) {
1355 kfree(capiminors);
1356 return -ENOMEM;
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 drv->owner = THIS_MODULE;
1359 drv->driver_name = "capi_nc";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 drv->name = "capi";
Jan Kiszkae95ac142010-02-08 10:12:26 +00001361 drv->major = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 drv->minor_start = 0;
1363 drv->type = TTY_DRIVER_TYPE_SERIAL;
1364 drv->subtype = SERIAL_TYPE_NORMAL;
1365 drv->init_termios = tty_std_termios;
1366 drv->init_termios.c_iflag = ICRNL;
1367 drv->init_termios.c_oflag = OPOST | ONLCR;
1368 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1369 drv->init_termios.c_lflag = 0;
Jan Kiszka40fb2d02010-02-08 10:12:25 +00001370 drv->flags =
1371 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1372 TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 tty_set_operations(drv, &capinc_ops);
Jan Kiszkae76b1542010-02-08 10:12:24 +00001374
1375 err = tty_register_driver(drv);
1376 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 put_tty_driver(drv);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001378 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 printk(KERN_ERR "Couldn't register capi_nc driver\n");
Jan Kiszkae76b1542010-02-08 10:12:24 +00001380 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382 capinc_tty_driver = drv;
1383 return 0;
1384}
1385
Jan Kiszkae76b1542010-02-08 10:12:24 +00001386static void __exit capinc_tty_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Jan Kiszkae76b1542010-02-08 10:12:24 +00001388 tty_unregister_driver(capinc_tty_driver);
1389 put_tty_driver(capinc_tty_driver);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001390 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
Jan Kiszka501c87a2010-02-08 10:12:16 +00001393#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1394
1395static inline int capinc_tty_init(void)
1396{
1397 return 0;
1398}
1399
1400static inline void capinc_tty_exit(void) { }
1401
1402#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404/* -------- /proc functions ----------------------------------------- */
1405
1406/*
1407 * /proc/capi/capi20:
1408 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1409 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001410static int capi20_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 struct capidev *cdev;
1413 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001415 mutex_lock(&capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 list_for_each(l, &capidev_list) {
1417 cdev = list_entry(l, struct capidev, list);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001418 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 cdev->ap.applid,
1420 cdev->ap.nrecvctlpkt,
1421 cdev->ap.nrecvdatapkt,
1422 cdev->ap.nsentctlpkt,
1423 cdev->ap.nsentdatapkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001425 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001429static int capi20_proc_open(struct inode *inode, struct file *file)
1430{
1431 return single_open(file, capi20_proc_show, NULL);
1432}
1433
1434static const struct file_operations capi20_proc_fops = {
1435 .owner = THIS_MODULE,
1436 .open = capi20_proc_open,
1437 .read = seq_read,
1438 .llseek = seq_lseek,
1439 .release = single_release,
1440};
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442/*
1443 * /proc/capi/capi20ncci:
1444 * applid ncci
1445 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001446static int capi20ncci_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Jan Kiszka884f5c42010-02-08 10:12:22 +00001448 struct capidev *cdev;
1449 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001451 mutex_lock(&capidev_list_lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001452 list_for_each_entry(cdev, &capidev_list, list) {
Jan Kiszka05b41492010-02-08 10:12:19 +00001453 mutex_lock(&cdev->lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001454 list_for_each_entry(np, &cdev->nccis, list)
1455 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
Jan Kiszka05b41492010-02-08 10:12:19 +00001456 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001458 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001462static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1463{
1464 return single_open(file, capi20ncci_proc_show, NULL);
1465}
1466
1467static const struct file_operations capi20ncci_proc_fops = {
1468 .owner = THIS_MODULE,
1469 .open = capi20ncci_proc_open,
1470 .read = seq_read,
1471 .llseek = seq_lseek,
1472 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473};
1474
1475static void __init proc_init(void)
1476{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001477 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1478 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
1481static void __exit proc_exit(void)
1482{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001483 remove_proc_entry("capi/capi20", NULL);
1484 remove_proc_entry("capi/capi20ncci", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
1487/* -------- init function and module interface ---------------------- */
1488
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490static int __init capi_init(void)
1491{
Jan Kiszka88549d62010-02-08 10:12:09 +00001492 const char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001493 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Andrew Morton6d9eac32006-03-28 01:56:19 -08001495 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1496 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001498 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001500 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (IS_ERR(capi_class)) {
1502 unregister_chrdev(capi_major, "capi20");
1503 return PTR_ERR(capi_class);
1504 }
1505
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -07001506 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 if (capinc_tty_init() < 0) {
Tony Jonesd78b0362007-09-25 02:03:03 +02001509 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001510 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 unregister_chrdev(capi_major, "capi20");
1512 return -ENOMEM;
1513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 proc_init();
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1518 compileinfo = " (middleware+capifs)";
Jan Kiszka501c87a2010-02-08 10:12:16 +00001519#elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 compileinfo = " (no capifs)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521#else
1522 compileinfo = " (no middleware)";
1523#endif
Jan Kiszka88549d62010-02-08 10:12:09 +00001524 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1525 capi_major, compileinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 return 0;
1528}
1529
1530static void __exit capi_exit(void)
1531{
1532 proc_exit();
1533
Tony Jonesd78b0362007-09-25 02:03:03 +02001534 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001535 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 capinc_tty_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
1541module_init(capi_init);
1542module_exit(capi_exit);