blob: bea100983336d678deba56d2b4b9bb0ed3e4455b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/timer.h>
24#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/tty.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/netdevice.h>
27#include <linux/ppp_defs.h>
28#include <linux/if_ppp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/skbuff.h>
30#include <linux/proc_fs.h>
Alexey Dobriyan9a58a802010-01-14 03:10:54 -080031#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/poll.h>
33#include <linux/capi.h>
34#include <linux/kernelcapi.h>
35#include <linux/init.h>
36#include <linux/device.h>
37#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/isdn/capiutil.h>
39#include <linux/isdn/capicmd.h>
Jan Kiszka90926f02010-02-08 10:12:06 +000040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
42MODULE_AUTHOR("Carsten Paeth");
43MODULE_LICENSE("GPL");
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#undef _DEBUG_TTYFUNCS /* call to tty_driver */
46#undef _DEBUG_DATAFLOW /* data flow */
47
48/* -------- driver information -------------------------------------- */
49
Arnd Bergmann76a64922010-07-11 11:18:53 +000050static DEFINE_MUTEX(capi_mutex);
gregkh@suse.de56b22932005-03-23 10:01:41 -080051static struct class *capi_class;
Adrian Bunk408b6642005-05-01 08:59:29 -070052static int capi_major = 68; /* allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54module_param_named(major, capi_major, uint, 0);
Jan Kiszka501c87a2010-02-08 10:12:16 +000055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +000057#define CAPINC_NR_PORTS 32
58#define CAPINC_MAX_PORTS 256
59
Jan Kiszka501c87a2010-02-08 10:12:16 +000060static int capi_ttyminors = CAPINC_NR_PORTS;
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062module_param_named(ttyminors, capi_ttyminors, uint, 0);
63#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
64
65/* -------- defines ------------------------------------------------- */
66
67#define CAPINC_MAX_RECVQUEUE 10
68#define CAPINC_MAX_SENDQUEUE 10
69#define CAPI_MAX_BLKSIZE 2048
70
71/* -------- data structures ----------------------------------------- */
72
73struct capidev;
74struct capincci;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075struct capiminor;
76
Jan Kiszka42651b52010-02-08 10:12:37 +000077struct ackqueue_entry {
Michael Buesch6aa65472006-06-26 00:25:30 -070078 struct list_head list;
79 u16 datahandle;
80};
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082struct capiminor {
Jan Kiszka0159d542010-02-08 10:12:27 +000083 struct kref kref;
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 unsigned int minor;
86
Jan Kiszka42792712010-02-08 10:12:38 +000087 struct capi20_appl *ap;
88 u32 ncci;
89 atomic_t datahandle;
90 atomic_t msgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Jan Kiszkafb4b4882010-02-08 10:12:29 +000092 struct tty_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 int ttyinstop;
94 int ttyoutstop;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Jan Kiszkadfbb84f2010-02-08 10:12:40 +000096 struct sk_buff_head inqueue;
97
98 struct sk_buff_head outqueue;
99 int outbytes;
100 struct sk_buff *outskb;
101 spinlock_t outlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 /* transmit path */
Michael Buesch6aa65472006-06-26 00:25:30 -0700104 struct list_head ackqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int nack;
Michael Buesch6aa65472006-06-26 00:25:30 -0700106 spinlock_t ackqlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109struct capincci {
Jan Kiszka884f5c42010-02-08 10:12:22 +0000110 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 u32 ncci;
112 struct capidev *cdev;
113#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
114 struct capiminor *minorp;
115#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
116};
117
118struct capidev {
119 struct list_head list;
120 struct capi20_appl ap;
121 u16 errcode;
122 unsigned userflags;
123
124 struct sk_buff_head recvqueue;
125 wait_queue_head_t recvwait;
126
Jan Kiszka884f5c42010-02-08 10:12:22 +0000127 struct list_head nccis;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Jan Kiszka05b41492010-02-08 10:12:19 +0000129 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
132/* -------- global variables ---------------------------------------- */
133
Jan Kiszkab8f433d2010-02-08 10:12:17 +0000134static DEFINE_MUTEX(capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static LIST_HEAD(capidev_list);
136
137#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +0000138
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000139static DEFINE_SPINLOCK(capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000140static struct capiminor **capiminors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000142static struct tty_driver *capinc_tty_driver;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* -------- datahandles --------------------------------------------- */
145
Jan Kiszka501c87a2010-02-08 10:12:16 +0000146static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Jan Kiszka42651b52010-02-08 10:12:37 +0000148 struct ackqueue_entry *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 n = kmalloc(sizeof(*n), GFP_ATOMIC);
Michael Buesch6aa65472006-06-26 00:25:30 -0700151 if (unlikely(!n)) {
152 printk(KERN_ERR "capi: alloc datahandle failed\n");
153 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 n->datahandle = datahandle;
Michael Buesch6aa65472006-06-26 00:25:30 -0700156 INIT_LIST_HEAD(&n->list);
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000157 spin_lock_bh(&mp->ackqlock);
Michael Buesch6aa65472006-06-26 00:25:30 -0700158 list_add_tail(&n->list, &mp->ackqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 mp->nack++;
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000160 spin_unlock_bh(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return 0;
162}
163
164static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
165{
Jan Kiszka42651b52010-02-08 10:12:37 +0000166 struct ackqueue_entry *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000168 spin_lock_bh(&mp->ackqlock);
Michael Buesch6aa65472006-06-26 00:25:30 -0700169 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
170 if (p->datahandle == datahandle) {
171 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 mp->nack--;
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000173 spin_unlock_bh(&mp->ackqlock);
174 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return 0;
176 }
177 }
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000178 spin_unlock_bh(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return -1;
180}
181
182static void capiminor_del_all_ack(struct capiminor *mp)
183{
Jan Kiszka42651b52010-02-08 10:12:37 +0000184 struct ackqueue_entry *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Michael Buesch6aa65472006-06-26 00:25:30 -0700186 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
187 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 kfree(p);
189 mp->nack--;
190 }
191}
192
193
194/* -------- struct capiminor ---------------------------------------- */
195
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000196static const struct tty_port_operations capiminor_port_ops; /* we have none */
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
199{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000200 struct capiminor *mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000201 struct device *dev;
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000202 unsigned int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000204 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (!mp) {
206 printk(KERN_ERR "capi: can't alloc capiminor\n");
207 return NULL;
208 }
209
Jan Kiszka0159d542010-02-08 10:12:27 +0000210 kref_init(&mp->kref);
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 mp->ap = ap;
213 mp->ncci = ncci;
Michael Buesch6aa65472006-06-26 00:25:30 -0700214 INIT_LIST_HEAD(&mp->ackqueue);
215 spin_lock_init(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 skb_queue_head_init(&mp->inqueue);
218 skb_queue_head_init(&mp->outqueue);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000219 spin_lock_init(&mp->outlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000221 tty_port_init(&mp->port);
222 mp->port.ops = &capiminor_port_ops;
223
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000224 /* Allocate the least unused minor number. */
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000225 spin_lock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000226 for (minor = 0; minor < capi_ttyminors; minor++)
227 if (!capiminors[minor]) {
228 capiminors[minor] = mp;
229 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000231 spin_unlock(&capiminors_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000233 if (minor == capi_ttyminors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 printk(KERN_NOTICE "capi: out of minors\n");
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000235 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000238 mp->minor = minor;
239
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000240 dev = tty_register_device(capinc_tty_driver, minor, NULL);
241 if (IS_ERR(dev))
242 goto err_out2;
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000245
246err_out2:
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000247 spin_lock(&capiminors_lock);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000248 capiminors[minor] = NULL;
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000249 spin_unlock(&capiminors_lock);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000250
251err_out1:
252 kfree(mp);
253 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Jan Kiszka0159d542010-02-08 10:12:27 +0000256static void capiminor_destroy(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Jan Kiszka0159d542010-02-08 10:12:27 +0000258 struct capiminor *mp = container_of(kref, struct capiminor, kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000260 kfree_skb(mp->outskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 skb_queue_purge(&mp->inqueue);
262 skb_queue_purge(&mp->outqueue);
263 capiminor_del_all_ack(mp);
264 kfree(mp);
265}
266
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000267static struct capiminor *capiminor_get(unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000269 struct capiminor *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000271 spin_lock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000272 mp = capiminors[minor];
Jan Kiszka0159d542010-02-08 10:12:27 +0000273 if (mp)
274 kref_get(&mp->kref);
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000275 spin_unlock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000276
277 return mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Jan Kiszka0159d542010-02-08 10:12:27 +0000280static inline void capiminor_put(struct capiminor *mp)
281{
282 kref_put(&mp->kref, capiminor_destroy);
283}
284
285static void capiminor_free(struct capiminor *mp)
286{
Jan Kiszka0159d542010-02-08 10:12:27 +0000287 tty_unregister_device(capinc_tty_driver, mp->minor);
288
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000289 spin_lock(&capiminors_lock);
Jan Kiszka0159d542010-02-08 10:12:27 +0000290 capiminors[mp->minor] = NULL;
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000291 spin_unlock(&capiminors_lock);
Jan Kiszka0159d542010-02-08 10:12:27 +0000292
293 capiminor_put(mp);
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296/* -------- struct capincci ----------------------------------------- */
297
Jan Kiszka501c87a2010-02-08 10:12:16 +0000298static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Jan Kiszka1f90d662011-04-06 10:58:37 +0000300 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
301 np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000302}
303
304static void capincci_free_minor(struct capincci *np)
305{
306 struct capiminor *mp = np->minorp;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000307 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000308
309 if (mp) {
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000310 tty = tty_port_tty_get(&mp->port);
311 if (tty) {
Jan Kiszka30bced92010-02-08 10:12:31 +0000312 tty_vhangup(tty);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000313 tty_kref_put(tty);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000314 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000315
316 capiminor_free(mp);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000317 }
318}
319
320static inline unsigned int capincci_minor_opencount(struct capincci *np)
321{
322 struct capiminor *mp = np->minorp;
Jan Kiszkaa84fdf42010-02-08 10:12:34 +0000323 unsigned int count = 0;
324 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000325
Jan Kiszkaa84fdf42010-02-08 10:12:34 +0000326 if (mp) {
327 tty = tty_port_tty_get(&mp->port);
328 if (tty) {
329 count = tty->count;
330 tty_kref_put(tty);
331 }
332 }
333 return count;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000334}
335
336#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
337
338static inline void
339capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
340static inline void capincci_free_minor(struct capincci *np) { }
341
342static inline unsigned int capincci_minor_opencount(struct capincci *np)
343{
344 return 0;
345}
346
347#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
348
349static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
350{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000351 struct capincci *np;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000352
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000353 np = kzalloc(sizeof(*np), GFP_KERNEL);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000354 if (!np)
355 return NULL;
356 np->ncci = ncci;
357 np->cdev = cdev;
358
359 capincci_alloc_minor(cdev, np);
360
Jan Kiszka884f5c42010-02-08 10:12:22 +0000361 list_add_tail(&np->list, &cdev->nccis);
362
363 return np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366static void capincci_free(struct capidev *cdev, u32 ncci)
367{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000368 struct capincci *np, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Jan Kiszka884f5c42010-02-08 10:12:22 +0000370 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (ncci == 0xffffffff || np->ncci == ncci) {
Jan Kiszka501c87a2010-02-08 10:12:16 +0000372 capincci_free_minor(np);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000373 list_del(&np->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 kfree(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
379{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000380 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Jan Kiszka884f5c42010-02-08 10:12:22 +0000382 list_for_each_entry(np, &cdev->nccis, list)
383 if (np->ncci == ncci)
384 return np;
385 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
389/* -------- handle data queue --------------------------------------- */
390
391static struct sk_buff *
392gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
393{
394 struct sk_buff *nskb;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000395 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (nskb) {
397 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
398 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
399 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
400 capimsg_setu16(s, 2, mp->ap->applid);
401 capimsg_setu8 (s, 4, CAPI_DATA_B3);
402 capimsg_setu8 (s, 5, CAPI_RESP);
Jan Kiszka42792712010-02-08 10:12:38 +0000403 capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 capimsg_setu32(s, 8, mp->ncci);
405 capimsg_setu16(s, 12, datahandle);
406 }
407 return nskb;
408}
409
410static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
411{
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000412 unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000413 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct sk_buff *nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 u16 errcode, datahandle;
416 struct tty_ldisc *ld;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000417 int ret = -1;
418
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000419 tty = tty_port_tty_get(&mp->port);
420 if (!tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421#ifdef _DEBUG_DATAFLOW
422 printk(KERN_DEBUG "capi: currently no receiver\n");
423#endif
424 return -1;
425 }
426
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000427 ld = tty_ldisc_ref(tty);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000428 if (!ld) {
429 /* fatal error, do not requeue */
430 ret = 0;
431 kfree_skb(skb);
432 goto deref_tty;
433 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000434
Alan Coxa352def2008-07-16 21:53:12 +0100435 if (ld->ops->receive_buf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
437 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
438#endif
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000439 /* fatal error, do not requeue */
440 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442 if (mp->ttyinstop) {
443#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
444 printk(KERN_DEBUG "capi: recv tty throttled\n");
445#endif
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000446 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000448
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000449 if (tty->receive_room < datalen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
451 printk(KERN_DEBUG "capi: no room in tty\n");
452#endif
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000453 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000455
456 nskb = gen_data_b3_resp_for(mp, skb);
457 if (!nskb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000459 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000461
462 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 errcode = capi20_put_message(mp->ap, nskb);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000465
466 if (errcode == CAPI_NOERROR) {
467 skb_pull(skb, CAPIMSG_LEN(skb->data));
468#ifdef _DEBUG_DATAFLOW
469 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
470 datahandle, skb->len);
471#endif
472 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
473 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
475 errcode);
476 kfree_skb(nskb);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000477
478 if (errcode == CAPI_SENDQUEUEFULL)
479 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000481
482free_skb:
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000483 ret = 0;
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000484 kfree_skb(skb);
485
486deref_ldisc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 tty_ldisc_deref(ld);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000488
489deref_tty:
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000490 tty_kref_put(tty);
491 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
494static void handle_minor_recv(struct capiminor *mp)
495{
496 struct sk_buff *skb;
Jan Kiszka68d73472010-02-08 10:12:39 +0000497
498 while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (handle_recv_skb(mp, skb) < 0) {
500 skb_queue_head(&mp->inqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return;
502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000505static void handle_minor_send(struct capiminor *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000507 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 struct sk_buff *skb;
509 u16 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 u16 errcode;
511 u16 datahandle;
512
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000513 tty = tty_port_tty_get(&mp->port);
514 if (!tty)
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000515 return;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000516
517 if (mp->ttyoutstop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
519 printk(KERN_DEBUG "capi: send: tty stopped\n");
520#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000521 tty_kref_put(tty);
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000522 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000525 while (1) {
526 spin_lock_bh(&mp->outlock);
527 skb = __skb_dequeue(&mp->outqueue);
528 if (!skb) {
529 spin_unlock_bh(&mp->outlock);
530 break;
531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 len = (u16)skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000533 mp->outbytes -= len;
534 spin_unlock_bh(&mp->outlock);
535
536 datahandle = atomic_inc_return(&mp->datahandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 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);
Jan Kiszka42792712010-02-08 10:12:38 +0000543 capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 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);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000552
553 spin_lock_bh(&mp->outlock);
554 __skb_queue_head(&mp->outqueue, skb);
555 mp->outbytes += len;
556 spin_unlock_bh(&mp->outlock);
557
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000558 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560 errcode = capi20_put_message(mp->ap, skb);
561 if (errcode == CAPI_NOERROR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562#ifdef _DEBUG_DATAFLOW
563 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
564 datahandle, len);
565#endif
566 continue;
567 }
568 capiminor_del_ack(mp, datahandle);
569
570 if (errcode == CAPI_SENDQUEUEFULL) {
571 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000572
573 spin_lock_bh(&mp->outlock);
574 __skb_queue_head(&mp->outqueue, skb);
575 mp->outbytes += len;
576 spin_unlock_bh(&mp->outlock);
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 break;
579 }
580
581 /* ups, drop packet */
582 printk(KERN_ERR "capi: put_message = %x\n", errcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 kfree_skb(skb);
584 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000585 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
588#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
589/* -------- function called by lower level -------------------------- */
590
591static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
592{
593 struct capidev *cdev = ap->private;
594#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000595 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 struct capiminor *mp;
597 u16 datahandle;
598#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
599 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Jan Kiszka05b41492010-02-08 10:12:19 +0000601 mutex_lock(&cdev->lock);
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
604 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
Jan Kiszka05b41492010-02-08 10:12:19 +0000605 if ((info & 0xff00) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000608 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
612 skb_queue_tail(&cdev->recvqueue, skb);
613 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000614 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000616
617 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (!np) {
619 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
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 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
626 skb_queue_tail(&cdev->recvqueue, skb);
627 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 mp = np->minorp;
632 if (!mp) {
633 skb_queue_tail(&cdev->recvqueue, skb);
634 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000635 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
639#ifdef _DEBUG_DATAFLOW
640 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
641 datahandle, skb->len-CAPIMSG_LEN(skb->data));
642#endif
643 skb_queue_tail(&mp->inqueue, skb);
Jan Kiszka68d73472010-02-08 10:12:39 +0000644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 handle_minor_recv(mp);
646
647 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
648
649 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
650#ifdef _DEBUG_DATAFLOW
651 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
652 datahandle,
653 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
654#endif
655 kfree_skb(skb);
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000656 capiminor_del_ack(mp, datahandle);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000657 tty = tty_port_tty_get(&mp->port);
658 if (tty) {
659 tty_wakeup(tty);
660 tty_kref_put(tty);
661 }
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000662 handle_minor_send(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 } else {
665 /* ups, let capi application handle it :-) */
666 skb_queue_tail(&cdev->recvqueue, skb);
667 wake_up_interruptible(&cdev->recvwait);
668 }
669#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000670
Jan Kiszka05b41492010-02-08 10:12:19 +0000671unlock_out:
Jan Kiszka05b41492010-02-08 10:12:19 +0000672 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675/* -------- file_operations for capidev ----------------------------- */
676
677static ssize_t
678capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
679{
Joe Perches54cbb1c2010-07-12 10:50:02 +0000680 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 struct sk_buff *skb;
682 size_t copied;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000683 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 if (!cdev->ap.applid)
686 return -ENODEV;
687
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000688 skb = skb_dequeue(&cdev->recvqueue);
689 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (file->f_flags & O_NONBLOCK)
691 return -EAGAIN;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000692 err = wait_event_interruptible(cdev->recvwait,
693 (skb = skb_dequeue(&cdev->recvqueue)));
694 if (err)
695 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697 if (skb->len > count) {
698 skb_queue_head(&cdev->recvqueue, skb);
699 return -EMSGSIZE;
700 }
701 if (copy_to_user(buf, skb->data, skb->len)) {
702 skb_queue_head(&cdev->recvqueue, skb);
703 return -EFAULT;
704 }
705 copied = skb->len;
706
707 kfree_skb(skb);
708
709 return copied;
710}
711
712static ssize_t
713capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
714{
Joe Perches54cbb1c2010-07-12 10:50:02 +0000715 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 struct sk_buff *skb;
717 u16 mlen;
718
719 if (!cdev->ap.applid)
720 return -ENODEV;
721
722 skb = alloc_skb(count, GFP_USER);
723 if (!skb)
724 return -ENOMEM;
725
726 if (copy_from_user(skb_put(skb, count), buf, count)) {
727 kfree_skb(skb);
728 return -EFAULT;
729 }
730 mlen = CAPIMSG_LEN(skb->data);
731 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
732 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
733 kfree_skb(skb);
734 return -EINVAL;
735 }
736 } else {
737 if (mlen != count) {
738 kfree_skb(skb);
739 return -EINVAL;
740 }
741 }
742 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
743
744 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Jan Kiszka05b41492010-02-08 10:12:19 +0000745 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000747 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749
750 cdev->errcode = capi20_put_message(&cdev->ap, skb);
751
752 if (cdev->errcode) {
753 kfree_skb(skb);
754 return -EIO;
755 }
756 return count;
757}
758
759static unsigned int
760capi_poll(struct file *file, poll_table * wait)
761{
Joe Perches54cbb1c2010-07-12 10:50:02 +0000762 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 unsigned int mask = 0;
764
765 if (!cdev->ap.applid)
766 return POLLERR;
767
768 poll_wait(file, &(cdev->recvwait), wait);
769 mask = POLLOUT | POLLWRNORM;
770 if (!skb_queue_empty(&cdev->recvqueue))
771 mask |= POLLIN | POLLRDNORM;
772 return mask;
773}
774
775static int
Arnd Bergmann703c6312010-04-27 00:24:02 +0200776capi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
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
Arnd Bergmann703c6312010-04-27 00:24:02 +0200969static long
970capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
971{
972 int ret;
973
Arnd Bergmann76a64922010-07-11 11:18:53 +0000974 mutex_lock(&capi_mutex);
Arnd Bergmann703c6312010-04-27 00:24:02 +0200975 ret = capi_ioctl(file, cmd, arg);
Arnd Bergmann76a64922010-07-11 11:18:53 +0000976 mutex_unlock(&capi_mutex);
Arnd Bergmann703c6312010-04-27 00:24:02 +0200977
978 return ret;
979}
980
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000981static int capi_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000983 struct capidev *cdev;
984
985 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
986 if (!cdev)
987 return -ENOMEM;
988
Jan Kiszka05b41492010-02-08 10:12:19 +0000989 mutex_init(&cdev->lock);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000990 skb_queue_head_init(&cdev->recvqueue);
991 init_waitqueue_head(&cdev->recvwait);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000992 INIT_LIST_HEAD(&cdev->nccis);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000993 file->private_data = cdev;
994
995 mutex_lock(&capidev_list_lock);
996 list_add_tail(&cdev->list, &capidev_list);
997 mutex_unlock(&capidev_list_lock);
998
999 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001002static int capi_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001004 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001006 mutex_lock(&capidev_list_lock);
1007 list_del(&cdev->list);
1008 mutex_unlock(&capidev_list_lock);
1009
Jan Kiszka05b41492010-02-08 10:12:19 +00001010 if (cdev->ap.applid)
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001011 capi20_release(&cdev->ap);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001012 skb_queue_purge(&cdev->recvqueue);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001013 capincci_free(cdev, 0xffffffff);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001014
1015 kfree(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return 0;
1017}
1018
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001019static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 .owner = THIS_MODULE,
1022 .llseek = no_llseek,
1023 .read = capi_read,
1024 .write = capi_write,
1025 .poll = capi_poll,
Arnd Bergmann703c6312010-04-27 00:24:02 +02001026 .unlocked_ioctl = capi_unlocked_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 .open = capi_open,
1028 .release = capi_release,
1029};
1030
1031#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1032/* -------- tty_operations for capincci ----------------------------- */
1033
Jan Kiszka46324512010-02-08 10:12:28 +00001034static int
1035capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1036{
1037 int idx = tty->index;
1038 struct capiminor *mp = capiminor_get(idx);
1039 int ret = tty_init_termios(tty);
1040
1041 if (ret == 0) {
1042 tty_driver_kref_get(driver);
1043 tty->count++;
1044 tty->driver_data = mp;
1045 driver->ttys[idx] = tty;
1046 } else
1047 capiminor_put(mp);
1048 return ret;
1049}
1050
1051static void capinc_tty_cleanup(struct tty_struct *tty)
1052{
1053 struct capiminor *mp = tty->driver_data;
1054 tty->driver_data = NULL;
1055 capiminor_put(mp);
1056}
1057
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001058static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Jan Kiszka46324512010-02-08 10:12:28 +00001060 struct capiminor *mp = tty->driver_data;
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001061 int err;
1062
1063 err = tty_port_open(&mp->port, tty, filp);
1064 if (err)
1065 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 handle_minor_recv(mp);
1068 return 0;
1069}
1070
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001071static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Jan Kiszka46324512010-02-08 10:12:28 +00001073 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001075 tty_port_close(&mp->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
Jan Kiszka6576c282010-02-08 10:12:32 +00001078static int capinc_tty_write(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 const unsigned char *buf, int count)
1080{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001081 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 struct sk_buff *skb;
1083
1084#ifdef _DEBUG_TTYFUNCS
1085 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1086#endif
1087
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001088 spin_lock_bh(&mp->outlock);
1089 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (skb) {
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001091 mp->outskb = NULL;
1092 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 mp->outbytes += skb->len;
1094 }
1095
1096 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1097 if (!skb) {
1098 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001099 spin_unlock_bh(&mp->outlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return -ENOMEM;
1101 }
1102
1103 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1104 memcpy(skb_put(skb, count), buf, count);
1105
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001106 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001108 spin_unlock_bh(&mp->outlock);
1109
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001110 handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return count;
1113}
1114
Alan Coxf2545a72008-04-30 00:54:09 -07001115static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001117 struct capiminor *mp = tty->driver_data;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001118 bool invoke_send = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 struct sk_buff *skb;
Alan Coxf2545a72008-04-30 00:54:09 -07001120 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122#ifdef _DEBUG_TTYFUNCS
1123 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1124#endif
1125
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001126 spin_lock_bh(&mp->outlock);
1127 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (skb) {
1129 if (skb_tailroom(skb) > 0) {
1130 *(skb_put(skb, 1)) = ch;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001131 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001133 mp->outskb = NULL;
1134 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001136 invoke_send = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1140 if (skb) {
1141 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1142 *(skb_put(skb, 1)) = ch;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001143 mp->outskb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 } else {
1145 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
Alan Coxf2545a72008-04-30 00:54:09 -07001146 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001148
1149unlock_out:
1150 spin_unlock_bh(&mp->outlock);
1151
1152 if (invoke_send)
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001153 handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001154
Alan Coxf2545a72008-04-30 00:54:09 -07001155 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
1158static void capinc_tty_flush_chars(struct tty_struct *tty)
1159{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001160 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 struct sk_buff *skb;
1162
1163#ifdef _DEBUG_TTYFUNCS
1164 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1165#endif
1166
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001167 spin_lock_bh(&mp->outlock);
1168 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (skb) {
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001170 mp->outskb = NULL;
1171 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001173 spin_unlock_bh(&mp->outlock);
1174
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001175 handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001176 } else
1177 spin_unlock_bh(&mp->outlock);
1178
1179 handle_minor_recv(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
1182static int capinc_tty_write_room(struct tty_struct *tty)
1183{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001184 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 int room;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1188 room *= CAPI_MAX_BLKSIZE;
1189#ifdef _DEBUG_TTYFUNCS
1190 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1191#endif
1192 return room;
1193}
1194
Adrian Bunk408b6642005-05-01 08:59:29 -07001195static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001197 struct capiminor *mp = tty->driver_data;
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199#ifdef _DEBUG_TTYFUNCS
1200 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1201 mp->outbytes, mp->nack,
1202 skb_queue_len(&mp->outqueue),
1203 skb_queue_len(&mp->inqueue));
1204#endif
1205 return mp->outbytes;
1206}
1207
Alan Cox6caa76b2011-02-14 16:27:22 +00001208static int capinc_tty_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 unsigned int cmd, unsigned long arg)
1210{
Alan Cox6caa76b2011-02-14 16:27:22 +00001211 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Alan Cox606d0992006-12-08 02:38:45 -08001214static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
1216#ifdef _DEBUG_TTYFUNCS
1217 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1218#endif
1219}
1220
Jan Kiszka2c8df722010-02-08 10:12:30 +00001221static void capinc_tty_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001223 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224#ifdef _DEBUG_TTYFUNCS
1225 printk(KERN_DEBUG "capinc_tty_throttle\n");
1226#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001227 mp->ttyinstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Jan Kiszka2c8df722010-02-08 10:12:30 +00001230static void capinc_tty_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001232 struct capiminor *mp = tty->driver_data;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234#ifdef _DEBUG_TTYFUNCS
1235 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1236#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001237 mp->ttyinstop = 0;
1238 handle_minor_recv(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
1241static void capinc_tty_stop(struct tty_struct *tty)
1242{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001243 struct capiminor *mp = tty->driver_data;
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245#ifdef _DEBUG_TTYFUNCS
1246 printk(KERN_DEBUG "capinc_tty_stop\n");
1247#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001248 mp->ttyoutstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
1251static void capinc_tty_start(struct tty_struct *tty)
1252{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001253 struct capiminor *mp = tty->driver_data;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255#ifdef _DEBUG_TTYFUNCS
1256 printk(KERN_DEBUG "capinc_tty_start\n");
1257#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001258 mp->ttyoutstop = 0;
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001259 handle_minor_send(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
1262static void capinc_tty_hangup(struct tty_struct *tty)
1263{
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001264 struct capiminor *mp = tty->driver_data;
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266#ifdef _DEBUG_TTYFUNCS
1267 printk(KERN_DEBUG "capinc_tty_hangup\n");
1268#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001269 tty_port_hangup(&mp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
Alan Cox9e98966c2008-07-22 11:18:03 +01001272static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274#ifdef _DEBUG_TTYFUNCS
1275 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1276#endif
Alan Cox9e98966c2008-07-22 11:18:03 +01001277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
1280static void capinc_tty_flush_buffer(struct tty_struct *tty)
1281{
1282#ifdef _DEBUG_TTYFUNCS
1283 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1284#endif
1285}
1286
1287static void capinc_tty_set_ldisc(struct tty_struct *tty)
1288{
1289#ifdef _DEBUG_TTYFUNCS
1290 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1291#endif
1292}
1293
1294static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1295{
1296#ifdef _DEBUG_TTYFUNCS
1297 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1298#endif
1299}
1300
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001301static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 .open = capinc_tty_open,
1303 .close = capinc_tty_close,
1304 .write = capinc_tty_write,
1305 .put_char = capinc_tty_put_char,
1306 .flush_chars = capinc_tty_flush_chars,
1307 .write_room = capinc_tty_write_room,
1308 .chars_in_buffer = capinc_tty_chars_in_buffer,
1309 .ioctl = capinc_tty_ioctl,
1310 .set_termios = capinc_tty_set_termios,
1311 .throttle = capinc_tty_throttle,
1312 .unthrottle = capinc_tty_unthrottle,
1313 .stop = capinc_tty_stop,
1314 .start = capinc_tty_start,
1315 .hangup = capinc_tty_hangup,
1316 .break_ctl = capinc_tty_break_ctl,
1317 .flush_buffer = capinc_tty_flush_buffer,
1318 .set_ldisc = capinc_tty_set_ldisc,
1319 .send_xchar = capinc_tty_send_xchar,
Jan Kiszka46324512010-02-08 10:12:28 +00001320 .install = capinc_tty_install,
1321 .cleanup = capinc_tty_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322};
1323
Jan Kiszkae76b1542010-02-08 10:12:24 +00001324static int __init capinc_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
1326 struct tty_driver *drv;
Jan Kiszkae76b1542010-02-08 10:12:24 +00001327 int err;
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (capi_ttyminors > CAPINC_MAX_PORTS)
1330 capi_ttyminors = CAPINC_MAX_PORTS;
1331 if (capi_ttyminors <= 0)
1332 capi_ttyminors = CAPINC_NR_PORTS;
1333
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001334 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1335 GFP_KERNEL);
1336 if (!capiminors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return -ENOMEM;
1338
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001339 drv = alloc_tty_driver(capi_ttyminors);
1340 if (!drv) {
1341 kfree(capiminors);
1342 return -ENOMEM;
1343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 drv->owner = THIS_MODULE;
1345 drv->driver_name = "capi_nc";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 drv->name = "capi";
Jan Kiszkae95ac142010-02-08 10:12:26 +00001347 drv->major = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 drv->minor_start = 0;
1349 drv->type = TTY_DRIVER_TYPE_SERIAL;
1350 drv->subtype = SERIAL_TYPE_NORMAL;
1351 drv->init_termios = tty_std_termios;
1352 drv->init_termios.c_iflag = ICRNL;
1353 drv->init_termios.c_oflag = OPOST | ONLCR;
1354 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1355 drv->init_termios.c_lflag = 0;
Jan Kiszka40fb2d02010-02-08 10:12:25 +00001356 drv->flags =
1357 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1358 TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 tty_set_operations(drv, &capinc_ops);
Jan Kiszkae76b1542010-02-08 10:12:24 +00001360
1361 err = tty_register_driver(drv);
1362 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 put_tty_driver(drv);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001364 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 printk(KERN_ERR "Couldn't register capi_nc driver\n");
Jan Kiszkae76b1542010-02-08 10:12:24 +00001366 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368 capinc_tty_driver = drv;
1369 return 0;
1370}
1371
Jan Kiszkae76b1542010-02-08 10:12:24 +00001372static void __exit capinc_tty_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Jan Kiszkae76b1542010-02-08 10:12:24 +00001374 tty_unregister_driver(capinc_tty_driver);
1375 put_tty_driver(capinc_tty_driver);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001376 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
Jan Kiszka501c87a2010-02-08 10:12:16 +00001379#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1380
1381static inline int capinc_tty_init(void)
1382{
1383 return 0;
1384}
1385
1386static inline void capinc_tty_exit(void) { }
1387
1388#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390/* -------- /proc functions ----------------------------------------- */
1391
1392/*
1393 * /proc/capi/capi20:
1394 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1395 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001396static int capi20_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
1398 struct capidev *cdev;
1399 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001401 mutex_lock(&capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 list_for_each(l, &capidev_list) {
1403 cdev = list_entry(l, struct capidev, list);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001404 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 cdev->ap.applid,
1406 cdev->ap.nrecvctlpkt,
1407 cdev->ap.nrecvdatapkt,
1408 cdev->ap.nsentctlpkt,
1409 cdev->ap.nsentdatapkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001411 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001412 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001415static int capi20_proc_open(struct inode *inode, struct file *file)
1416{
1417 return single_open(file, capi20_proc_show, NULL);
1418}
1419
1420static const struct file_operations capi20_proc_fops = {
1421 .owner = THIS_MODULE,
1422 .open = capi20_proc_open,
1423 .read = seq_read,
1424 .llseek = seq_lseek,
1425 .release = single_release,
1426};
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428/*
1429 * /proc/capi/capi20ncci:
1430 * applid ncci
1431 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001432static int capi20ncci_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Jan Kiszka884f5c42010-02-08 10:12:22 +00001434 struct capidev *cdev;
1435 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001437 mutex_lock(&capidev_list_lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001438 list_for_each_entry(cdev, &capidev_list, list) {
Jan Kiszka05b41492010-02-08 10:12:19 +00001439 mutex_lock(&cdev->lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001440 list_for_each_entry(np, &cdev->nccis, list)
1441 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
Jan Kiszka05b41492010-02-08 10:12:19 +00001442 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001444 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001445 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001448static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1449{
1450 return single_open(file, capi20ncci_proc_show, NULL);
1451}
1452
1453static const struct file_operations capi20ncci_proc_fops = {
1454 .owner = THIS_MODULE,
1455 .open = capi20ncci_proc_open,
1456 .read = seq_read,
1457 .llseek = seq_lseek,
1458 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459};
1460
1461static void __init proc_init(void)
1462{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001463 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1464 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
1467static void __exit proc_exit(void)
1468{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001469 remove_proc_entry("capi/capi20", NULL);
1470 remove_proc_entry("capi/capi20ncci", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
1472
1473/* -------- init function and module interface ---------------------- */
1474
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476static int __init capi_init(void)
1477{
Jan Kiszka88549d62010-02-08 10:12:09 +00001478 const char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001479 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Andrew Morton6d9eac32006-03-28 01:56:19 -08001481 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1482 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001484 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001486 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 if (IS_ERR(capi_class)) {
1488 unregister_chrdev(capi_major, "capi20");
1489 return PTR_ERR(capi_class);
1490 }
1491
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -07001492 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (capinc_tty_init() < 0) {
Tony Jonesd78b0362007-09-25 02:03:03 +02001495 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001496 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 unregister_chrdev(capi_major, "capi20");
1498 return -ENOMEM;
1499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 proc_init();
1502
Jan Kiszka1f90d662011-04-06 10:58:37 +00001503#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1504 compileinfo = " (middleware)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505#else
1506 compileinfo = " (no middleware)";
1507#endif
Jan Kiszka88549d62010-02-08 10:12:09 +00001508 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1509 capi_major, compileinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 return 0;
1512}
1513
1514static void __exit capi_exit(void)
1515{
1516 proc_exit();
1517
Tony Jonesd78b0362007-09-25 02:03:03 +02001518 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001519 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 capinc_tty_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
1525module_init(capi_init);
1526module_exit(capi_exit);