blob: 0b264b4565c83b2090153fad7a4292b4af5f35ec [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 unsigned int minor;
Jan Kiszka90926f02010-02-08 10:12:06 +000089 struct dentry *capifs_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 struct capi20_appl *ap;
92 u32 ncci;
93 u16 datahandle;
94 u16 msgid;
95
Jan Kiszkafb4b4882010-02-08 10:12:29 +000096 struct tty_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 int ttyinstop;
98 int ttyoutstop;
99 struct sk_buff *ttyskb;
100 atomic_t ttyopencount;
101
102 struct sk_buff_head inqueue;
103 int inbytes;
104 struct sk_buff_head outqueue;
105 int outbytes;
106
107 /* transmit path */
Michael Buesch6aa65472006-06-26 00:25:30 -0700108 struct list_head ackqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int nack;
Michael Buesch6aa65472006-06-26 00:25:30 -0700110 spinlock_t ackqlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Michael Buesch053b47f2007-02-12 00:53:26 -0800113/* FIXME: The following lock is a sledgehammer-workaround to a
114 * locking issue with the capiminor (and maybe other) data structure(s).
115 * Access to this data is done in a racy way and crashes the machine with
116 * a FritzCard DSL driver; sooner or later. This is a workaround
117 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
118 * The correct (and scalable) fix for the issue seems to require
119 * an API change to the drivers... . */
120static DEFINE_SPINLOCK(workaround_lock);
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122struct capincci {
Jan Kiszka884f5c42010-02-08 10:12:22 +0000123 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 u32 ncci;
125 struct capidev *cdev;
126#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
127 struct capiminor *minorp;
128#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
129};
130
131struct capidev {
132 struct list_head list;
133 struct capi20_appl ap;
134 u16 errcode;
135 unsigned userflags;
136
137 struct sk_buff_head recvqueue;
138 wait_queue_head_t recvwait;
139
Jan Kiszka884f5c42010-02-08 10:12:22 +0000140 struct list_head nccis;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Jan Kiszka05b41492010-02-08 10:12:19 +0000142 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
145/* -------- global variables ---------------------------------------- */
146
Jan Kiszkab8f433d2010-02-08 10:12:17 +0000147static DEFINE_MUTEX(capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static LIST_HEAD(capidev_list);
149
150#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +0000151
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000152static DEFINE_SPINLOCK(capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000153static struct capiminor **capiminors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000155static struct tty_driver *capinc_tty_driver;
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* -------- datahandles --------------------------------------------- */
158
Jan Kiszka501c87a2010-02-08 10:12:16 +0000159static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Michael Buesch6aa65472006-06-26 00:25:30 -0700161 struct datahandle_queue *n;
162 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 n = kmalloc(sizeof(*n), GFP_ATOMIC);
Michael Buesch6aa65472006-06-26 00:25:30 -0700165 if (unlikely(!n)) {
166 printk(KERN_ERR "capi: alloc datahandle failed\n");
167 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 n->datahandle = datahandle;
Michael Buesch6aa65472006-06-26 00:25:30 -0700170 INIT_LIST_HEAD(&n->list);
171 spin_lock_irqsave(&mp->ackqlock, flags);
172 list_add_tail(&n->list, &mp->ackqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 mp->nack++;
Michael Buesch6aa65472006-06-26 00:25:30 -0700174 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return 0;
176}
177
178static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
179{
Michael Buesch6aa65472006-06-26 00:25:30 -0700180 struct datahandle_queue *p, *tmp;
181 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Michael Buesch6aa65472006-06-26 00:25:30 -0700183 spin_lock_irqsave(&mp->ackqlock, flags);
184 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
185 if (p->datahandle == datahandle) {
186 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 kfree(p);
188 mp->nack--;
Michael Buesch6aa65472006-06-26 00:25:30 -0700189 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return 0;
191 }
192 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700193 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return -1;
195}
196
197static void capiminor_del_all_ack(struct capiminor *mp)
198{
Michael Buesch6aa65472006-06-26 00:25:30 -0700199 struct datahandle_queue *p, *tmp;
200 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Michael Buesch6aa65472006-06-26 00:25:30 -0700202 spin_lock_irqsave(&mp->ackqlock, flags);
203 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
204 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 kfree(p);
206 mp->nack--;
207 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700208 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211
212/* -------- struct capiminor ---------------------------------------- */
213
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000214static const struct tty_port_operations capiminor_port_ops; /* we have none */
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
217{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000218 struct capiminor *mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000219 struct device *dev;
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000220 unsigned int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
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 Kiszkafb4b4882010-02-08 10:12:29 +0000240 tty_port_init(&mp->port);
241 mp->port.ops = &capiminor_port_ops;
242
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000243 /* Allocate the least unused minor number. */
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000244 spin_lock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000245 for (minor = 0; minor < capi_ttyminors; minor++)
246 if (!capiminors[minor]) {
247 capiminors[minor] = mp;
248 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000250 spin_unlock(&capiminors_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000252 if (minor == capi_ttyminors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 printk(KERN_NOTICE "capi: out of minors\n");
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000254 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000257 mp->minor = minor;
258
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000259 dev = tty_register_device(capinc_tty_driver, minor, NULL);
260 if (IS_ERR(dev))
261 goto err_out2;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000264
265err_out2:
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000266 spin_lock(&capiminors_lock);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000267 capiminors[minor] = NULL;
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000268 spin_unlock(&capiminors_lock);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000269
270err_out1:
271 kfree(mp);
272 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Jan Kiszka0159d542010-02-08 10:12:27 +0000275static void capiminor_destroy(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Jan Kiszka0159d542010-02-08 10:12:27 +0000277 struct capiminor *mp = container_of(kref, struct capiminor, kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Wei Yongjund46604e2009-02-25 00:12:09 +0000279 kfree_skb(mp->ttyskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 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 Kiszka3d5d30f2010-02-08 10:12:33 +0000290 spin_lock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000291 mp = capiminors[minor];
Jan Kiszka0159d542010-02-08 10:12:27 +0000292 if (mp)
293 kref_get(&mp->kref);
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000294 spin_unlock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000295
296 return mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Jan Kiszka0159d542010-02-08 10:12:27 +0000299static inline void capiminor_put(struct capiminor *mp)
300{
301 kref_put(&mp->kref, capiminor_destroy);
302}
303
304static void capiminor_free(struct capiminor *mp)
305{
Jan Kiszka0159d542010-02-08 10:12:27 +0000306 tty_unregister_device(capinc_tty_driver, mp->minor);
307
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000308 spin_lock(&capiminors_lock);
Jan Kiszka0159d542010-02-08 10:12:27 +0000309 capiminors[mp->minor] = NULL;
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000310 spin_unlock(&capiminors_lock);
Jan Kiszka0159d542010-02-08 10:12:27 +0000311
312 capiminor_put(mp);
313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/* -------- struct capincci ----------------------------------------- */
316
Jan Kiszka501c87a2010-02-08 10:12:16 +0000317static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Jan Kiszka501c87a2010-02-08 10:12:16 +0000319 struct capiminor *mp;
Jan Kiszkae95ac142010-02-08 10:12:26 +0000320 dev_t device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Jan Kiszka501c87a2010-02-08 10:12:16 +0000322 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
323 return;
324
325 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (mp) {
Jan Kiszkae95ac142010-02-08 10:12:26 +0000327 device = MKDEV(capinc_tty_driver->major, mp->minor);
328 mp->capifs_dentry = capifs_new_ncci(mp->minor, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000330}
331
332static void capincci_free_minor(struct capincci *np)
333{
334 struct capiminor *mp = np->minorp;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000335 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000336
337 if (mp) {
338 capifs_free_ncci(mp->capifs_dentry);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000339
340 tty = tty_port_tty_get(&mp->port);
341 if (tty) {
Jan Kiszka30bced92010-02-08 10:12:31 +0000342 tty_vhangup(tty);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000343 tty_kref_put(tty);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000344 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000345
346 capiminor_free(mp);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000347 }
348}
349
350static inline unsigned int capincci_minor_opencount(struct capincci *np)
351{
352 struct capiminor *mp = np->minorp;
353
354 return mp ? atomic_read(&mp->ttyopencount) : 0;
355}
356
357#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
358
359static inline void
360capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
361static inline void capincci_free_minor(struct capincci *np) { }
362
363static inline unsigned int capincci_minor_opencount(struct capincci *np)
364{
365 return 0;
366}
367
368#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
369
370static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
371{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000372 struct capincci *np;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000373
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000374 np = kzalloc(sizeof(*np), GFP_KERNEL);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000375 if (!np)
376 return NULL;
377 np->ncci = ncci;
378 np->cdev = cdev;
379
380 capincci_alloc_minor(cdev, np);
381
Jan Kiszka884f5c42010-02-08 10:12:22 +0000382 list_add_tail(&np->list, &cdev->nccis);
383
384 return np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
387static void capincci_free(struct capidev *cdev, u32 ncci)
388{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000389 struct capincci *np, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Jan Kiszka884f5c42010-02-08 10:12:22 +0000391 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (ncci == 0xffffffff || np->ncci == ncci) {
Jan Kiszka501c87a2010-02-08 10:12:16 +0000393 capincci_free_minor(np);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000394 list_del(&np->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 kfree(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
400{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000401 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Jan Kiszka884f5c42010-02-08 10:12:22 +0000403 list_for_each_entry(np, &cdev->nccis, list)
404 if (np->ncci == ncci)
405 return np;
406 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
410/* -------- handle data queue --------------------------------------- */
411
412static struct sk_buff *
413gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
414{
415 struct sk_buff *nskb;
416 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
417 if (nskb) {
418 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
419 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
420 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
421 capimsg_setu16(s, 2, mp->ap->applid);
422 capimsg_setu8 (s, 4, CAPI_DATA_B3);
423 capimsg_setu8 (s, 5, CAPI_RESP);
424 capimsg_setu16(s, 6, mp->msgid++);
425 capimsg_setu32(s, 8, mp->ncci);
426 capimsg_setu16(s, 12, datahandle);
427 }
428 return nskb;
429}
430
431static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
432{
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000433 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct sk_buff *nskb;
435 int datalen;
436 u16 errcode, datahandle;
437 struct tty_ldisc *ld;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000438 int ret = -1;
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 datalen = skb->len - CAPIMSG_LEN(skb->data);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000441
442 tty = tty_port_tty_get(&mp->port);
443 if (!tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444#ifdef _DEBUG_DATAFLOW
445 printk(KERN_DEBUG "capi: currently no receiver\n");
446#endif
447 return -1;
448 }
449
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000450 ld = tty_ldisc_ref(tty);
451 if (!ld)
452 goto out1;
453
Alan Coxa352def2008-07-16 21:53:12 +0100454 if (ld->ops->receive_buf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
456 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
457#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000458 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460 if (mp->ttyinstop) {
461#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
462 printk(KERN_DEBUG "capi: recv tty throttled\n");
463#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000464 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000466 if (tty->receive_room < datalen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
468 printk(KERN_DEBUG "capi: no room in tty\n");
469#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000470 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700472 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000474 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
477 errcode = capi20_put_message(mp->ap, nskb);
478 if (errcode != CAPI_NOERROR) {
479 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
480 errcode);
481 kfree_skb(nskb);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000482 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
485#ifdef _DEBUG_DATAFLOW
486 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
487 datahandle, skb->len);
488#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000489 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 kfree_skb(skb);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000491 ret = 0;
492out2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 tty_ldisc_deref(ld);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000494out1:
495 tty_kref_put(tty);
496 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499static void handle_minor_recv(struct capiminor *mp)
500{
501 struct sk_buff *skb;
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700502 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 unsigned int len = skb->len;
504 mp->inbytes -= len;
505 if (handle_recv_skb(mp, skb) < 0) {
506 skb_queue_head(&mp->inqueue, skb);
507 mp->inbytes += len;
508 return;
509 }
510 }
511}
512
513static int handle_minor_send(struct capiminor *mp)
514{
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000515 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct sk_buff *skb;
517 u16 len;
518 int count = 0;
519 u16 errcode;
520 u16 datahandle;
521
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000522 tty = tty_port_tty_get(&mp->port);
523 if (!tty)
524 return 0;
525
526 if (mp->ttyoutstop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
528 printk(KERN_DEBUG "capi: send: tty stopped\n");
529#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000530 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return 0;
532 }
533
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700534 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 datahandle = mp->datahandle;
536 len = (u16)skb->len;
537 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
538 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
539 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
540 capimsg_setu16(skb->data, 2, mp->ap->applid);
541 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
542 capimsg_setu8 (skb->data, 5, CAPI_REQ);
543 capimsg_setu16(skb->data, 6, mp->msgid++);
544 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
Andrew Morton5e6c20a2007-07-17 04:04:22 -0700545 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 capimsg_setu16(skb->data, 16, len); /* Data length */
547 capimsg_setu16(skb->data, 18, datahandle);
548 capimsg_setu16(skb->data, 20, 0); /* Flags */
549
Jan Kiszka501c87a2010-02-08 10:12:16 +0000550 if (capiminor_add_ack(mp, datahandle) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
552 skb_queue_head(&mp->outqueue, skb);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000553 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return count;
555 }
556 errcode = capi20_put_message(mp->ap, skb);
557 if (errcode == CAPI_NOERROR) {
558 mp->datahandle++;
559 count++;
560 mp->outbytes -= len;
561#ifdef _DEBUG_DATAFLOW
562 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
563 datahandle, len);
564#endif
565 continue;
566 }
567 capiminor_del_ack(mp, datahandle);
568
569 if (errcode == CAPI_SENDQUEUEFULL) {
570 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
571 skb_queue_head(&mp->outqueue, skb);
572 break;
573 }
574
575 /* ups, drop packet */
576 printk(KERN_ERR "capi: put_message = %x\n", errcode);
577 mp->outbytes -= len;
578 kfree_skb(skb);
579 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000580 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return count;
582}
583
584#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
585/* -------- function called by lower level -------------------------- */
586
587static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
588{
589 struct capidev *cdev = ap->private;
590#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000591 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 struct capiminor *mp;
593 u16 datahandle;
594#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
595 struct capincci *np;
Michael Buesch053b47f2007-02-12 00:53:26 -0800596 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Jan Kiszka05b41492010-02-08 10:12:19 +0000598 mutex_lock(&cdev->lock);
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
601 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
Jan Kiszka05b41492010-02-08 10:12:19 +0000602 if ((info & 0xff00) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000605 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000607
Michael Buesch053b47f2007-02-12 00:53:26 -0800608 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
610 skb_queue_tail(&cdev->recvqueue, skb);
611 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000612 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000614
615 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (!np) {
617 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
618 skb_queue_tail(&cdev->recvqueue, skb);
619 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000620 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
624 skb_queue_tail(&cdev->recvqueue, skb);
625 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 mp = np->minorp;
630 if (!mp) {
631 skb_queue_tail(&cdev->recvqueue, skb);
632 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000633 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
637#ifdef _DEBUG_DATAFLOW
638 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
639 datahandle, skb->len-CAPIMSG_LEN(skb->data));
640#endif
641 skb_queue_tail(&mp->inqueue, skb);
642 mp->inbytes += skb->len;
643 handle_minor_recv(mp);
644
645 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
646
647 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
648#ifdef _DEBUG_DATAFLOW
649 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
650 datahandle,
651 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
652#endif
653 kfree_skb(skb);
654 (void)capiminor_del_ack(mp, datahandle);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000655 tty = tty_port_tty_get(&mp->port);
656 if (tty) {
657 tty_wakeup(tty);
658 tty_kref_put(tty);
659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 (void)handle_minor_send(mp);
661
662 } else {
663 /* ups, let capi application handle it :-) */
664 skb_queue_tail(&cdev->recvqueue, skb);
665 wake_up_interruptible(&cdev->recvwait);
666 }
667#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000668
Jan Kiszka05b41492010-02-08 10:12:19 +0000669unlock_out:
Michael Buesch053b47f2007-02-12 00:53:26 -0800670 spin_unlock_irqrestore(&workaround_lock, flags);
Jan Kiszka05b41492010-02-08 10:12:19 +0000671 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674/* -------- file_operations for capidev ----------------------------- */
675
676static ssize_t
677capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
678{
679 struct capidev *cdev = (struct capidev *)file->private_data;
680 struct sk_buff *skb;
681 size_t copied;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000682 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 if (!cdev->ap.applid)
685 return -ENODEV;
686
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000687 skb = skb_dequeue(&cdev->recvqueue);
688 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (file->f_flags & O_NONBLOCK)
690 return -EAGAIN;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000691 err = wait_event_interruptible(cdev->recvwait,
692 (skb = skb_dequeue(&cdev->recvqueue)));
693 if (err)
694 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696 if (skb->len > count) {
697 skb_queue_head(&cdev->recvqueue, skb);
698 return -EMSGSIZE;
699 }
700 if (copy_to_user(buf, skb->data, skb->len)) {
701 skb_queue_head(&cdev->recvqueue, skb);
702 return -EFAULT;
703 }
704 copied = skb->len;
705
706 kfree_skb(skb);
707
708 return copied;
709}
710
711static ssize_t
712capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
713{
714 struct capidev *cdev = (struct capidev *)file->private_data;
715 struct sk_buff *skb;
716 u16 mlen;
717
718 if (!cdev->ap.applid)
719 return -ENODEV;
720
721 skb = alloc_skb(count, GFP_USER);
722 if (!skb)
723 return -ENOMEM;
724
725 if (copy_from_user(skb_put(skb, count), buf, count)) {
726 kfree_skb(skb);
727 return -EFAULT;
728 }
729 mlen = CAPIMSG_LEN(skb->data);
730 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
731 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
732 kfree_skb(skb);
733 return -EINVAL;
734 }
735 } else {
736 if (mlen != count) {
737 kfree_skb(skb);
738 return -EINVAL;
739 }
740 }
741 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
742
743 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Jan Kiszka05b41492010-02-08 10:12:19 +0000744 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000746 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
749 cdev->errcode = capi20_put_message(&cdev->ap, skb);
750
751 if (cdev->errcode) {
752 kfree_skb(skb);
753 return -EIO;
754 }
755 return count;
756}
757
758static unsigned int
759capi_poll(struct file *file, poll_table * wait)
760{
761 struct capidev *cdev = (struct capidev *)file->private_data;
762 unsigned int mask = 0;
763
764 if (!cdev->ap.applid)
765 return POLLERR;
766
767 poll_wait(file, &(cdev->recvwait), wait);
768 mask = POLLOUT | POLLWRNORM;
769 if (!skb_queue_empty(&cdev->recvqueue))
770 mask |= POLLIN | POLLRDNORM;
771 return mask;
772}
773
774static int
775capi_ioctl(struct inode *inode, struct file *file,
776 unsigned int cmd, unsigned long arg)
777{
778 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 capi_ioctl_struct data;
780 int retval = -EINVAL;
781 void __user *argp = (void __user *)arg;
782
783 switch (cmd) {
784 case CAPI_REGISTER:
Jan Kiszka05b41492010-02-08 10:12:19 +0000785 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Jan Kiszka05b41492010-02-08 10:12:19 +0000787 if (cdev->ap.applid) {
788 retval = -EEXIST;
789 goto register_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000791 if (copy_from_user(&cdev->ap.rparam, argp,
792 sizeof(struct capi_register_params))) {
793 retval = -EFAULT;
794 goto register_out;
795 }
796 cdev->ap.private = cdev;
797 cdev->ap.recv_message = capi_recv_message;
798 cdev->errcode = capi20_register(&cdev->ap);
799 retval = (int)cdev->ap.applid;
800 if (cdev->errcode) {
801 cdev->ap.applid = 0;
802 retval = -EIO;
803 }
804
805register_out:
806 mutex_unlock(&cdev->lock);
807 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 case CAPI_GET_VERSION:
810 {
811 if (copy_from_user(&data.contr, argp,
812 sizeof(data.contr)))
813 return -EFAULT;
814 cdev->errcode = capi20_get_version(data.contr, &data.version);
815 if (cdev->errcode)
816 return -EIO;
817 if (copy_to_user(argp, &data.version,
818 sizeof(data.version)))
819 return -EFAULT;
820 }
821 return 0;
822
823 case CAPI_GET_SERIAL:
824 {
825 if (copy_from_user(&data.contr, argp,
826 sizeof(data.contr)))
827 return -EFAULT;
828 cdev->errcode = capi20_get_serial (data.contr, data.serial);
829 if (cdev->errcode)
830 return -EIO;
831 if (copy_to_user(argp, data.serial,
832 sizeof(data.serial)))
833 return -EFAULT;
834 }
835 return 0;
836 case CAPI_GET_PROFILE:
837 {
838 if (copy_from_user(&data.contr, argp,
839 sizeof(data.contr)))
840 return -EFAULT;
841
842 if (data.contr == 0) {
843 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
844 if (cdev->errcode)
845 return -EIO;
846
847 retval = copy_to_user(argp,
848 &data.profile.ncontroller,
849 sizeof(data.profile.ncontroller));
850
851 } else {
852 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
853 if (cdev->errcode)
854 return -EIO;
855
856 retval = copy_to_user(argp, &data.profile,
857 sizeof(data.profile));
858 }
859 if (retval)
860 return -EFAULT;
861 }
862 return 0;
863
864 case CAPI_GET_MANUFACTURER:
865 {
866 if (copy_from_user(&data.contr, argp,
867 sizeof(data.contr)))
868 return -EFAULT;
869 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
870 if (cdev->errcode)
871 return -EIO;
872
873 if (copy_to_user(argp, data.manufacturer,
874 sizeof(data.manufacturer)))
875 return -EFAULT;
876
877 }
878 return 0;
879 case CAPI_GET_ERRCODE:
880 data.errcode = cdev->errcode;
881 cdev->errcode = CAPI_NOERROR;
882 if (arg) {
883 if (copy_to_user(argp, &data.errcode,
884 sizeof(data.errcode)))
885 return -EFAULT;
886 }
887 return data.errcode;
888
889 case CAPI_INSTALLED:
890 if (capi20_isinstalled() == CAPI_NOERROR)
891 return 0;
892 return -ENXIO;
893
894 case CAPI_MANUFACTURER_CMD:
895 {
896 struct capi_manufacturer_cmd mcmd;
897 if (!capable(CAP_SYS_ADMIN))
898 return -EPERM;
899 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
900 return -EFAULT;
901 return capi20_manufacturer(mcmd.cmd, mcmd.data);
902 }
903 return 0;
904
905 case CAPI_SET_FLAGS:
Jan Kiszka05b41492010-02-08 10:12:19 +0000906 case CAPI_CLR_FLAGS: {
907 unsigned userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Jan Kiszka05b41492010-02-08 10:12:19 +0000909 if (copy_from_user(&userflags, argp, sizeof(userflags)))
910 return -EFAULT;
911
912 mutex_lock(&cdev->lock);
913 if (cmd == CAPI_SET_FLAGS)
914 cdev->userflags |= userflags;
915 else
916 cdev->userflags &= ~userflags;
917 mutex_unlock(&cdev->lock);
918 return 0;
919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 case CAPI_GET_FLAGS:
921 if (copy_to_user(argp, &cdev->userflags,
922 sizeof(cdev->userflags)))
923 return -EFAULT;
924 return 0;
925
Jan Kiszka05b41492010-02-08 10:12:19 +0000926 case CAPI_NCCI_OPENCOUNT: {
927 struct capincci *nccip;
928 unsigned ncci;
929 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Jan Kiszka05b41492010-02-08 10:12:19 +0000931 if (copy_from_user(&ncci, argp, sizeof(ncci)))
932 return -EFAULT;
933
934 mutex_lock(&cdev->lock);
935 nccip = capincci_find(cdev, (u32)ncci);
936 if (nccip)
937 count = capincci_minor_opencount(nccip);
938 mutex_unlock(&cdev->lock);
939 return count;
940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka05b41492010-02-08 10:12:19 +0000943 case CAPI_NCCI_GETUNIT: {
944 struct capincci *nccip;
945 struct capiminor *mp;
946 unsigned ncci;
947 int unit = -ESRCH;
948
949 if (copy_from_user(&ncci, argp, sizeof(ncci)))
950 return -EFAULT;
951
952 mutex_lock(&cdev->lock);
953 nccip = capincci_find(cdev, (u32)ncci);
954 if (nccip) {
955 mp = nccip->minorp;
956 if (mp)
957 unit = mp->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000959 mutex_unlock(&cdev->lock);
960 return unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000962#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
963
964 default:
965 return -EINVAL;
966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000969static int capi_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000971 struct capidev *cdev;
972
973 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
974 if (!cdev)
975 return -ENOMEM;
976
Jan Kiszka05b41492010-02-08 10:12:19 +0000977 mutex_init(&cdev->lock);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000978 skb_queue_head_init(&cdev->recvqueue);
979 init_waitqueue_head(&cdev->recvwait);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000980 INIT_LIST_HEAD(&cdev->nccis);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000981 file->private_data = cdev;
982
983 mutex_lock(&capidev_list_lock);
984 list_add_tail(&cdev->list, &capidev_list);
985 mutex_unlock(&capidev_list_lock);
986
987 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000990static int capi_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000992 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000994 mutex_lock(&capidev_list_lock);
995 list_del(&cdev->list);
996 mutex_unlock(&capidev_list_lock);
997
Jan Kiszka05b41492010-02-08 10:12:19 +0000998 if (cdev->ap.applid)
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000999 capi20_release(&cdev->ap);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001000 skb_queue_purge(&cdev->recvqueue);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001001 capincci_free(cdev, 0xffffffff);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001002
1003 kfree(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return 0;
1005}
1006
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001007static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 .owner = THIS_MODULE,
1010 .llseek = no_llseek,
1011 .read = capi_read,
1012 .write = capi_write,
1013 .poll = capi_poll,
1014 .ioctl = capi_ioctl,
1015 .open = capi_open,
1016 .release = capi_release,
1017};
1018
1019#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1020/* -------- tty_operations for capincci ----------------------------- */
1021
Jan Kiszka46324512010-02-08 10:12:28 +00001022static int
1023capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1024{
1025 int idx = tty->index;
1026 struct capiminor *mp = capiminor_get(idx);
1027 int ret = tty_init_termios(tty);
1028
1029 if (ret == 0) {
1030 tty_driver_kref_get(driver);
1031 tty->count++;
1032 tty->driver_data = mp;
1033 driver->ttys[idx] = tty;
1034 } else
1035 capiminor_put(mp);
1036 return ret;
1037}
1038
1039static void capinc_tty_cleanup(struct tty_struct *tty)
1040{
1041 struct capiminor *mp = tty->driver_data;
1042 tty->driver_data = NULL;
1043 capiminor_put(mp);
1044}
1045
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001046static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Jan Kiszka46324512010-02-08 10:12:28 +00001048 struct capiminor *mp = tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001049 unsigned long flags;
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001050 int err;
1051
1052 err = tty_port_open(&mp->port, tty, filp);
1053 if (err)
1054 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Michael Buesch053b47f2007-02-12 00:53:26 -08001056 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 atomic_inc(&mp->ttyopencount);
1058#ifdef _DEBUG_REFCOUNT
1059 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1060#endif
1061 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001062 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return 0;
1064}
1065
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001066static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Jan Kiszka46324512010-02-08 10:12:28 +00001068 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (atomic_dec_and_test(&mp->ttyopencount)) {
1071#ifdef _DEBUG_REFCOUNT
1072 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1073#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075#ifdef _DEBUG_REFCOUNT
1076 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1077#endif
Jan Kiszka0159d542010-02-08 10:12:27 +00001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079#ifdef _DEBUG_REFCOUNT
1080 printk(KERN_DEBUG "capinc_tty_close\n");
1081#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001082 tty_port_close(&mp->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Jan Kiszka6576c282010-02-08 10:12:32 +00001085static int capinc_tty_write(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 const unsigned char *buf, int count)
1087{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001088 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001090 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092#ifdef _DEBUG_TTYFUNCS
1093 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1094#endif
1095
Michael Buesch053b47f2007-02-12 00:53:26 -08001096 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 skb = mp->ttyskb;
1098 if (skb) {
1099 mp->ttyskb = NULL;
1100 skb_queue_tail(&mp->outqueue, skb);
1101 mp->outbytes += skb->len;
1102 }
1103
1104 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1105 if (!skb) {
1106 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
Michael Buesch053b47f2007-02-12 00:53:26 -08001107 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return -ENOMEM;
1109 }
1110
1111 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1112 memcpy(skb_put(skb, count), buf, count);
1113
1114 skb_queue_tail(&mp->outqueue, skb);
1115 mp->outbytes += skb->len;
1116 (void)handle_minor_send(mp);
1117 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001118 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return count;
1120}
1121
Alan Coxf2545a72008-04-30 00:54:09 -07001122static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001124 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001126 unsigned long flags;
Alan Coxf2545a72008-04-30 00:54:09 -07001127 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129#ifdef _DEBUG_TTYFUNCS
1130 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1131#endif
1132
Michael Buesch053b47f2007-02-12 00:53:26 -08001133 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 skb = mp->ttyskb;
1135 if (skb) {
1136 if (skb_tailroom(skb) > 0) {
1137 *(skb_put(skb, 1)) = ch;
Michael Buesch053b47f2007-02-12 00:53:26 -08001138 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001139 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141 mp->ttyskb = NULL;
1142 skb_queue_tail(&mp->outqueue, skb);
1143 mp->outbytes += skb->len;
1144 (void)handle_minor_send(mp);
1145 }
1146 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1147 if (skb) {
1148 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1149 *(skb_put(skb, 1)) = ch;
1150 mp->ttyskb = skb;
1151 } else {
1152 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
Alan Coxf2545a72008-04-30 00:54:09 -07001153 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
Michael Buesch053b47f2007-02-12 00:53:26 -08001155 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001156 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
1159static void capinc_tty_flush_chars(struct tty_struct *tty)
1160{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001161 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001163 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165#ifdef _DEBUG_TTYFUNCS
1166 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1167#endif
1168
Michael Buesch053b47f2007-02-12 00:53:26 -08001169 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 skb = mp->ttyskb;
1171 if (skb) {
1172 mp->ttyskb = NULL;
1173 skb_queue_tail(&mp->outqueue, skb);
1174 mp->outbytes += skb->len;
1175 (void)handle_minor_send(mp);
1176 }
1177 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001178 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
1181static int capinc_tty_write_room(struct tty_struct *tty)
1182{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001183 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 int room;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1187 room *= CAPI_MAX_BLKSIZE;
1188#ifdef _DEBUG_TTYFUNCS
1189 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1190#endif
1191 return room;
1192}
1193
Adrian Bunk408b6642005-05-01 08:59:29 -07001194static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001196 struct capiminor *mp = tty->driver_data;
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198#ifdef _DEBUG_TTYFUNCS
1199 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1200 mp->outbytes, mp->nack,
1201 skb_queue_len(&mp->outqueue),
1202 skb_queue_len(&mp->inqueue));
1203#endif
1204 return mp->outbytes;
1205}
1206
1207static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1208 unsigned int cmd, unsigned long arg)
1209{
1210 int error = 0;
1211 switch (cmd) {
1212 default:
Stephen Rothwell53e86312008-10-13 10:44:33 +01001213 error = n_tty_ioctl_helper(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 break;
1215 }
1216 return error;
1217}
1218
Alan Cox606d0992006-12-08 02:38:45 -08001219static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221#ifdef _DEBUG_TTYFUNCS
1222 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1223#endif
1224}
1225
Jan Kiszka2c8df722010-02-08 10:12:30 +00001226static void capinc_tty_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001228 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229#ifdef _DEBUG_TTYFUNCS
1230 printk(KERN_DEBUG "capinc_tty_throttle\n");
1231#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001232 mp->ttyinstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
Jan Kiszka2c8df722010-02-08 10:12:30 +00001235static void capinc_tty_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001237 struct capiminor *mp = tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001238 unsigned long flags;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240#ifdef _DEBUG_TTYFUNCS
1241 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1242#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001243 spin_lock_irqsave(&workaround_lock, flags);
1244 mp->ttyinstop = 0;
1245 handle_minor_recv(mp);
1246 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
1249static void capinc_tty_stop(struct tty_struct *tty)
1250{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001251 struct capiminor *mp = tty->driver_data;
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253#ifdef _DEBUG_TTYFUNCS
1254 printk(KERN_DEBUG "capinc_tty_stop\n");
1255#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001256 mp->ttyoutstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
1259static void capinc_tty_start(struct tty_struct *tty)
1260{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001261 struct capiminor *mp = tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001262 unsigned long flags;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264#ifdef _DEBUG_TTYFUNCS
1265 printk(KERN_DEBUG "capinc_tty_start\n");
1266#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001267 spin_lock_irqsave(&workaround_lock, flags);
1268 mp->ttyoutstop = 0;
1269 (void)handle_minor_send(mp);
1270 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
1273static void capinc_tty_hangup(struct tty_struct *tty)
1274{
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001275 struct capiminor *mp = tty->driver_data;
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277#ifdef _DEBUG_TTYFUNCS
1278 printk(KERN_DEBUG "capinc_tty_hangup\n");
1279#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001280 tty_port_hangup(&mp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Alan Cox9e989662008-07-22 11:18:03 +01001283static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285#ifdef _DEBUG_TTYFUNCS
1286 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1287#endif
Alan Cox9e989662008-07-22 11:18:03 +01001288 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
1291static void capinc_tty_flush_buffer(struct tty_struct *tty)
1292{
1293#ifdef _DEBUG_TTYFUNCS
1294 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1295#endif
1296}
1297
1298static void capinc_tty_set_ldisc(struct tty_struct *tty)
1299{
1300#ifdef _DEBUG_TTYFUNCS
1301 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1302#endif
1303}
1304
1305static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1306{
1307#ifdef _DEBUG_TTYFUNCS
1308 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1309#endif
1310}
1311
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001312static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 .open = capinc_tty_open,
1314 .close = capinc_tty_close,
1315 .write = capinc_tty_write,
1316 .put_char = capinc_tty_put_char,
1317 .flush_chars = capinc_tty_flush_chars,
1318 .write_room = capinc_tty_write_room,
1319 .chars_in_buffer = capinc_tty_chars_in_buffer,
1320 .ioctl = capinc_tty_ioctl,
1321 .set_termios = capinc_tty_set_termios,
1322 .throttle = capinc_tty_throttle,
1323 .unthrottle = capinc_tty_unthrottle,
1324 .stop = capinc_tty_stop,
1325 .start = capinc_tty_start,
1326 .hangup = capinc_tty_hangup,
1327 .break_ctl = capinc_tty_break_ctl,
1328 .flush_buffer = capinc_tty_flush_buffer,
1329 .set_ldisc = capinc_tty_set_ldisc,
1330 .send_xchar = capinc_tty_send_xchar,
Jan Kiszka46324512010-02-08 10:12:28 +00001331 .install = capinc_tty_install,
1332 .cleanup = capinc_tty_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333};
1334
Jan Kiszkae76b1542010-02-08 10:12:24 +00001335static int __init capinc_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 struct tty_driver *drv;
Jan Kiszkae76b1542010-02-08 10:12:24 +00001338 int err;
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (capi_ttyminors > CAPINC_MAX_PORTS)
1341 capi_ttyminors = CAPINC_MAX_PORTS;
1342 if (capi_ttyminors <= 0)
1343 capi_ttyminors = CAPINC_NR_PORTS;
1344
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001345 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1346 GFP_KERNEL);
1347 if (!capiminors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return -ENOMEM;
1349
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001350 drv = alloc_tty_driver(capi_ttyminors);
1351 if (!drv) {
1352 kfree(capiminors);
1353 return -ENOMEM;
1354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 drv->owner = THIS_MODULE;
1356 drv->driver_name = "capi_nc";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 drv->name = "capi";
Jan Kiszkae95ac142010-02-08 10:12:26 +00001358 drv->major = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 drv->minor_start = 0;
1360 drv->type = TTY_DRIVER_TYPE_SERIAL;
1361 drv->subtype = SERIAL_TYPE_NORMAL;
1362 drv->init_termios = tty_std_termios;
1363 drv->init_termios.c_iflag = ICRNL;
1364 drv->init_termios.c_oflag = OPOST | ONLCR;
1365 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1366 drv->init_termios.c_lflag = 0;
Jan Kiszka40fb2d02010-02-08 10:12:25 +00001367 drv->flags =
1368 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1369 TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 tty_set_operations(drv, &capinc_ops);
Jan Kiszkae76b1542010-02-08 10:12:24 +00001371
1372 err = tty_register_driver(drv);
1373 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 put_tty_driver(drv);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001375 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 printk(KERN_ERR "Couldn't register capi_nc driver\n");
Jan Kiszkae76b1542010-02-08 10:12:24 +00001377 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
1379 capinc_tty_driver = drv;
1380 return 0;
1381}
1382
Jan Kiszkae76b1542010-02-08 10:12:24 +00001383static void __exit capinc_tty_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Jan Kiszkae76b1542010-02-08 10:12:24 +00001385 tty_unregister_driver(capinc_tty_driver);
1386 put_tty_driver(capinc_tty_driver);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001387 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
Jan Kiszka501c87a2010-02-08 10:12:16 +00001390#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1391
1392static inline int capinc_tty_init(void)
1393{
1394 return 0;
1395}
1396
1397static inline void capinc_tty_exit(void) { }
1398
1399#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401/* -------- /proc functions ----------------------------------------- */
1402
1403/*
1404 * /proc/capi/capi20:
1405 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1406 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001407static int capi20_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 struct capidev *cdev;
1410 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001412 mutex_lock(&capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 list_for_each(l, &capidev_list) {
1414 cdev = list_entry(l, struct capidev, list);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001415 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 cdev->ap.applid,
1417 cdev->ap.nrecvctlpkt,
1418 cdev->ap.nrecvdatapkt,
1419 cdev->ap.nsentctlpkt,
1420 cdev->ap.nsentdatapkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001422 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001423 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001426static int capi20_proc_open(struct inode *inode, struct file *file)
1427{
1428 return single_open(file, capi20_proc_show, NULL);
1429}
1430
1431static const struct file_operations capi20_proc_fops = {
1432 .owner = THIS_MODULE,
1433 .open = capi20_proc_open,
1434 .read = seq_read,
1435 .llseek = seq_lseek,
1436 .release = single_release,
1437};
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439/*
1440 * /proc/capi/capi20ncci:
1441 * applid ncci
1442 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001443static int capi20ncci_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Jan Kiszka884f5c42010-02-08 10:12:22 +00001445 struct capidev *cdev;
1446 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001448 mutex_lock(&capidev_list_lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001449 list_for_each_entry(cdev, &capidev_list, list) {
Jan Kiszka05b41492010-02-08 10:12:19 +00001450 mutex_lock(&cdev->lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001451 list_for_each_entry(np, &cdev->nccis, list)
1452 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
Jan Kiszka05b41492010-02-08 10:12:19 +00001453 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001455 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001459static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1460{
1461 return single_open(file, capi20ncci_proc_show, NULL);
1462}
1463
1464static const struct file_operations capi20ncci_proc_fops = {
1465 .owner = THIS_MODULE,
1466 .open = capi20ncci_proc_open,
1467 .read = seq_read,
1468 .llseek = seq_lseek,
1469 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470};
1471
1472static void __init proc_init(void)
1473{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001474 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1475 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
1478static void __exit proc_exit(void)
1479{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001480 remove_proc_entry("capi/capi20", NULL);
1481 remove_proc_entry("capi/capi20ncci", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
1484/* -------- init function and module interface ---------------------- */
1485
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487static int __init capi_init(void)
1488{
Jan Kiszka88549d62010-02-08 10:12:09 +00001489 const char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001490 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Andrew Morton6d9eac32006-03-28 01:56:19 -08001492 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1493 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001495 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001497 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (IS_ERR(capi_class)) {
1499 unregister_chrdev(capi_major, "capi20");
1500 return PTR_ERR(capi_class);
1501 }
1502
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -07001503 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (capinc_tty_init() < 0) {
Tony Jonesd78b0362007-09-25 02:03:03 +02001506 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001507 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 unregister_chrdev(capi_major, "capi20");
1509 return -ENOMEM;
1510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 proc_init();
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1515 compileinfo = " (middleware+capifs)";
Jan Kiszka501c87a2010-02-08 10:12:16 +00001516#elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 compileinfo = " (no capifs)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518#else
1519 compileinfo = " (no middleware)";
1520#endif
Jan Kiszka88549d62010-02-08 10:12:09 +00001521 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1522 capi_major, compileinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 return 0;
1525}
1526
1527static void __exit capi_exit(void)
1528{
1529 proc_exit();
1530
Tony Jonesd78b0362007-09-25 02:03:03 +02001531 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001532 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 capinc_tty_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
1538module_init(capi_init);
1539module_exit(capi_exit);