blob: c679867c2ccd3f37682ef75def8481588dfbec9a [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>
Paul Mackerras4b32da2b2012-03-04 12:56:55 +000028#include <linux/ppp-ioctl.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/* -------- driver information -------------------------------------- */
46
Arnd Bergmann76a64922010-07-11 11:18:53 +000047static DEFINE_MUTEX(capi_mutex);
gregkh@suse.de56b22932005-03-23 10:01:41 -080048static struct class *capi_class;
Adrian Bunk408b6642005-05-01 08:59:29 -070049static int capi_major = 68; /* allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51module_param_named(major, capi_major, uint, 0);
Jan Kiszka501c87a2010-02-08 10:12:16 +000052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +000054#define CAPINC_NR_PORTS 32
55#define CAPINC_MAX_PORTS 256
56
Jan Kiszka501c87a2010-02-08 10:12:16 +000057static int capi_ttyminors = CAPINC_NR_PORTS;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059module_param_named(ttyminors, capi_ttyminors, uint, 0);
60#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
61
62/* -------- defines ------------------------------------------------- */
63
64#define CAPINC_MAX_RECVQUEUE 10
65#define CAPINC_MAX_SENDQUEUE 10
66#define CAPI_MAX_BLKSIZE 2048
67
68/* -------- data structures ----------------------------------------- */
69
70struct capidev;
71struct capincci;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072struct capiminor;
73
Jan Kiszka42651b52010-02-08 10:12:37 +000074struct ackqueue_entry {
Michael Buesch6aa65472006-06-26 00:25:30 -070075 struct list_head list;
76 u16 datahandle;
77};
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079struct capiminor {
Jan Kiszka0159d542010-02-08 10:12:27 +000080 struct kref kref;
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 unsigned int minor;
83
Jan Kiszka42792712010-02-08 10:12:38 +000084 struct capi20_appl *ap;
85 u32 ncci;
86 atomic_t datahandle;
87 atomic_t msgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Jan Kiszkafb4b4882010-02-08 10:12:29 +000089 struct tty_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 int ttyinstop;
91 int ttyoutstop;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Jan Kiszkadfbb84f2010-02-08 10:12:40 +000093 struct sk_buff_head inqueue;
94
95 struct sk_buff_head outqueue;
96 int outbytes;
97 struct sk_buff *outskb;
98 spinlock_t outlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 /* transmit path */
Michael Buesch6aa65472006-06-26 00:25:30 -0700101 struct list_head ackqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 int nack;
Michael Buesch6aa65472006-06-26 00:25:30 -0700103 spinlock_t ackqlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106struct capincci {
Jan Kiszka884f5c42010-02-08 10:12:22 +0000107 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 u32 ncci;
109 struct capidev *cdev;
110#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
111 struct capiminor *minorp;
112#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
113};
114
115struct capidev {
116 struct list_head list;
117 struct capi20_appl ap;
118 u16 errcode;
119 unsigned userflags;
120
121 struct sk_buff_head recvqueue;
122 wait_queue_head_t recvwait;
123
Jan Kiszka884f5c42010-02-08 10:12:22 +0000124 struct list_head nccis;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Jan Kiszka05b41492010-02-08 10:12:19 +0000126 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
129/* -------- global variables ---------------------------------------- */
130
Jan Kiszkab8f433d2010-02-08 10:12:17 +0000131static DEFINE_MUTEX(capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static LIST_HEAD(capidev_list);
133
134#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +0000135
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000136static DEFINE_SPINLOCK(capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000137static struct capiminor **capiminors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000139static struct tty_driver *capinc_tty_driver;
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/* -------- datahandles --------------------------------------------- */
142
Jan Kiszka501c87a2010-02-08 10:12:16 +0000143static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Jan Kiszka42651b52010-02-08 10:12:37 +0000145 struct ackqueue_entry *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 n = kmalloc(sizeof(*n), GFP_ATOMIC);
Michael Buesch6aa65472006-06-26 00:25:30 -0700148 if (unlikely(!n)) {
149 printk(KERN_ERR "capi: alloc datahandle failed\n");
150 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 n->datahandle = datahandle;
Michael Buesch6aa65472006-06-26 00:25:30 -0700153 INIT_LIST_HEAD(&n->list);
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000154 spin_lock_bh(&mp->ackqlock);
Michael Buesch6aa65472006-06-26 00:25:30 -0700155 list_add_tail(&n->list, &mp->ackqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 mp->nack++;
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000157 spin_unlock_bh(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return 0;
159}
160
161static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
162{
Jan Kiszka42651b52010-02-08 10:12:37 +0000163 struct ackqueue_entry *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000165 spin_lock_bh(&mp->ackqlock);
Michael Buesch6aa65472006-06-26 00:25:30 -0700166 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
Joe Perches475be4d2012-02-19 19:52:38 -0800167 if (p->datahandle == datahandle) {
Michael Buesch6aa65472006-06-26 00:25:30 -0700168 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 mp->nack--;
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000170 spin_unlock_bh(&mp->ackqlock);
171 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return 0;
173 }
174 }
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000175 spin_unlock_bh(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return -1;
177}
178
179static void capiminor_del_all_ack(struct capiminor *mp)
180{
Jan Kiszka42651b52010-02-08 10:12:37 +0000181 struct ackqueue_entry *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Michael Buesch6aa65472006-06-26 00:25:30 -0700183 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
184 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 kfree(p);
186 mp->nack--;
187 }
188}
189
190
191/* -------- struct capiminor ---------------------------------------- */
192
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000193static const struct tty_port_operations capiminor_port_ops; /* we have none */
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
196{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000197 struct capiminor *mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000198 struct device *dev;
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000199 unsigned int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000201 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
Joe Perches475be4d2012-02-19 19:52:38 -0800202 if (!mp) {
203 printk(KERN_ERR "capi: can't alloc capiminor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return NULL;
205 }
206
Jan Kiszka0159d542010-02-08 10:12:27 +0000207 kref_init(&mp->kref);
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 mp->ap = ap;
210 mp->ncci = ncci;
Michael Buesch6aa65472006-06-26 00:25:30 -0700211 INIT_LIST_HEAD(&mp->ackqueue);
212 spin_lock_init(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 skb_queue_head_init(&mp->inqueue);
215 skb_queue_head_init(&mp->outqueue);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000216 spin_lock_init(&mp->outlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000218 tty_port_init(&mp->port);
219 mp->port.ops = &capiminor_port_ops;
220
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000221 /* Allocate the least unused minor number. */
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000222 spin_lock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000223 for (minor = 0; minor < capi_ttyminors; minor++)
224 if (!capiminors[minor]) {
225 capiminors[minor] = mp;
226 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000228 spin_unlock(&capiminors_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000230 if (minor == capi_ttyminors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 printk(KERN_NOTICE "capi: out of minors\n");
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000232 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000235 mp->minor = minor;
236
Jiri Slaby734cc172012-08-07 21:47:47 +0200237 dev = tty_port_register_device(&mp->port, capinc_tty_driver, minor,
238 NULL);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000239 if (IS_ERR(dev))
240 goto err_out2;
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000243
244err_out2:
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000245 spin_lock(&capiminors_lock);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000246 capiminors[minor] = NULL;
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000247 spin_unlock(&capiminors_lock);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000248
249err_out1:
250 kfree(mp);
251 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Jan Kiszka0159d542010-02-08 10:12:27 +0000254static void capiminor_destroy(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Jan Kiszka0159d542010-02-08 10:12:27 +0000256 struct capiminor *mp = container_of(kref, struct capiminor, kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000258 kfree_skb(mp->outskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 skb_queue_purge(&mp->inqueue);
260 skb_queue_purge(&mp->outqueue);
261 capiminor_del_all_ack(mp);
262 kfree(mp);
263}
264
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000265static struct capiminor *capiminor_get(unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000267 struct capiminor *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000269 spin_lock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000270 mp = capiminors[minor];
Jan Kiszka0159d542010-02-08 10:12:27 +0000271 if (mp)
272 kref_get(&mp->kref);
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000273 spin_unlock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000274
275 return mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Jan Kiszka0159d542010-02-08 10:12:27 +0000278static inline void capiminor_put(struct capiminor *mp)
279{
280 kref_put(&mp->kref, capiminor_destroy);
281}
282
283static void capiminor_free(struct capiminor *mp)
284{
Jan Kiszka0159d542010-02-08 10:12:27 +0000285 tty_unregister_device(capinc_tty_driver, mp->minor);
286
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000287 spin_lock(&capiminors_lock);
Jan Kiszka0159d542010-02-08 10:12:27 +0000288 capiminors[mp->minor] = NULL;
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000289 spin_unlock(&capiminors_lock);
Jan Kiszka0159d542010-02-08 10:12:27 +0000290
291 capiminor_put(mp);
292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/* -------- struct capincci ----------------------------------------- */
295
Jan Kiszka501c87a2010-02-08 10:12:16 +0000296static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Jan Kiszka1f90d662011-04-06 10:58:37 +0000298 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
299 np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000300}
301
302static void capincci_free_minor(struct capincci *np)
303{
304 struct capiminor *mp = np->minorp;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000305 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000306
307 if (mp) {
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000308 tty = tty_port_tty_get(&mp->port);
309 if (tty) {
Jan Kiszka30bced92010-02-08 10:12:31 +0000310 tty_vhangup(tty);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000311 tty_kref_put(tty);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000312 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000313
314 capiminor_free(mp);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000315 }
316}
317
318static inline unsigned int capincci_minor_opencount(struct capincci *np)
319{
320 struct capiminor *mp = np->minorp;
Jan Kiszkaa84fdf42010-02-08 10:12:34 +0000321 unsigned int count = 0;
322 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000323
Jan Kiszkaa84fdf42010-02-08 10:12:34 +0000324 if (mp) {
325 tty = tty_port_tty_get(&mp->port);
326 if (tty) {
327 count = tty->count;
328 tty_kref_put(tty);
329 }
330 }
331 return count;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000332}
333
334#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
335
336static inline void
337capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
338static inline void capincci_free_minor(struct capincci *np) { }
339
Jan Kiszka501c87a2010-02-08 10:12:16 +0000340#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
341
342static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
343{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000344 struct capincci *np;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000345
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000346 np = kzalloc(sizeof(*np), GFP_KERNEL);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000347 if (!np)
348 return NULL;
349 np->ncci = ncci;
350 np->cdev = cdev;
351
352 capincci_alloc_minor(cdev, np);
353
Jan Kiszka884f5c42010-02-08 10:12:22 +0000354 list_add_tail(&np->list, &cdev->nccis);
355
356 return np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359static void capincci_free(struct capidev *cdev, u32 ncci)
360{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000361 struct capincci *np, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Jan Kiszka884f5c42010-02-08 10:12:22 +0000363 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (ncci == 0xffffffff || np->ncci == ncci) {
Jan Kiszka501c87a2010-02-08 10:12:16 +0000365 capincci_free_minor(np);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000366 list_del(&np->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 kfree(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Tilman Schmidt7dc2ce52012-04-25 13:02:20 +0000371#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
373{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000374 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Jan Kiszka884f5c42010-02-08 10:12:22 +0000376 list_for_each_entry(np, &cdev->nccis, list)
377 if (np->ncci == ncci)
378 return np;
379 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/* -------- handle data queue --------------------------------------- */
383
384static struct sk_buff *
385gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
386{
387 struct sk_buff *nskb;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000388 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (nskb) {
Joe Perches475be4d2012-02-19 19:52:38 -0800390 u16 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
392 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
393 capimsg_setu16(s, 2, mp->ap->applid);
394 capimsg_setu8 (s, 4, CAPI_DATA_B3);
395 capimsg_setu8 (s, 5, CAPI_RESP);
Jan Kiszka42792712010-02-08 10:12:38 +0000396 capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 capimsg_setu32(s, 8, mp->ncci);
398 capimsg_setu16(s, 12, datahandle);
399 }
400 return nskb;
401}
402
403static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
404{
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000405 unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000406 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 struct sk_buff *nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 u16 errcode, datahandle;
409 struct tty_ldisc *ld;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000410 int ret = -1;
411
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000412 tty = tty_port_tty_get(&mp->port);
413 if (!tty) {
David S. Miller8fb53b92011-05-19 18:20:29 -0400414 pr_debug("capi: currently no receiver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return -1;
416 }
Joe Perches475be4d2012-02-19 19:52:38 -0800417
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000418 ld = tty_ldisc_ref(tty);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000419 if (!ld) {
420 /* fatal error, do not requeue */
421 ret = 0;
422 kfree_skb(skb);
423 goto deref_tty;
424 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000425
Alan Coxa352def2008-07-16 21:53:12 +0100426 if (ld->ops->receive_buf == NULL) {
David S. Miller8fb53b92011-05-19 18:20:29 -0400427 pr_debug("capi: ldisc has no receive_buf function\n");
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000428 /* fatal error, do not requeue */
429 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431 if (mp->ttyinstop) {
David S. Miller8fb53b92011-05-19 18:20:29 -0400432 pr_debug("capi: recv tty throttled\n");
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000433 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000435
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000436 if (tty->receive_room < datalen) {
David S. Miller8fb53b92011-05-19 18:20:29 -0400437 pr_debug("capi: no room in tty\n");
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000438 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000440
441 nskb = gen_data_b3_resp_for(mp, skb);
442 if (!nskb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000444 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000446
447 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 errcode = capi20_put_message(mp->ap, nskb);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000450
451 if (errcode == CAPI_NOERROR) {
452 skb_pull(skb, CAPIMSG_LEN(skb->data));
David S. Miller8fb53b92011-05-19 18:20:29 -0400453 pr_debug("capi: DATA_B3_RESP %u len=%d => ldisc\n",
454 datahandle, skb->len);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000455 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
456 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800458 errcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 kfree_skb(nskb);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000460
461 if (errcode == CAPI_SENDQUEUEFULL)
462 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000464
465free_skb:
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000466 ret = 0;
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000467 kfree_skb(skb);
468
469deref_ldisc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 tty_ldisc_deref(ld);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000471
472deref_tty:
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000473 tty_kref_put(tty);
474 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477static void handle_minor_recv(struct capiminor *mp)
478{
479 struct sk_buff *skb;
Jan Kiszka68d73472010-02-08 10:12:39 +0000480
481 while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (handle_recv_skb(mp, skb) < 0) {
483 skb_queue_head(&mp->inqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return;
485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000488static void handle_minor_send(struct capiminor *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000490 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 struct sk_buff *skb;
492 u16 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 u16 errcode;
494 u16 datahandle;
495
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000496 tty = tty_port_tty_get(&mp->port);
497 if (!tty)
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000498 return;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000499
500 if (mp->ttyoutstop) {
David S. Miller8fb53b92011-05-19 18:20:29 -0400501 pr_debug("capi: send: tty stopped\n");
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000502 tty_kref_put(tty);
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000503 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000506 while (1) {
507 spin_lock_bh(&mp->outlock);
508 skb = __skb_dequeue(&mp->outqueue);
509 if (!skb) {
510 spin_unlock_bh(&mp->outlock);
511 break;
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 len = (u16)skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000514 mp->outbytes -= len;
515 spin_unlock_bh(&mp->outlock);
516
517 datahandle = atomic_inc_return(&mp->datahandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
519 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
520 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
521 capimsg_setu16(skb->data, 2, mp->ap->applid);
522 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
523 capimsg_setu8 (skb->data, 5, CAPI_REQ);
Jan Kiszka42792712010-02-08 10:12:38 +0000524 capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
Andrew Morton5e6c20a2007-07-17 04:04:22 -0700526 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 capimsg_setu16(skb->data, 16, len); /* Data length */
528 capimsg_setu16(skb->data, 18, datahandle);
529 capimsg_setu16(skb->data, 20, 0); /* Flags */
530
Jan Kiszka501c87a2010-02-08 10:12:16 +0000531 if (capiminor_add_ack(mp, datahandle) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000533
534 spin_lock_bh(&mp->outlock);
535 __skb_queue_head(&mp->outqueue, skb);
536 mp->outbytes += len;
537 spin_unlock_bh(&mp->outlock);
538
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000539 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541 errcode = capi20_put_message(mp->ap, skb);
542 if (errcode == CAPI_NOERROR) {
David S. Miller8fb53b92011-05-19 18:20:29 -0400543 pr_debug("capi: DATA_B3_REQ %u len=%u\n",
544 datahandle, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 continue;
546 }
547 capiminor_del_ack(mp, datahandle);
548
549 if (errcode == CAPI_SENDQUEUEFULL) {
550 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000551
552 spin_lock_bh(&mp->outlock);
553 __skb_queue_head(&mp->outqueue, skb);
554 mp->outbytes += len;
555 spin_unlock_bh(&mp->outlock);
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 break;
558 }
559
560 /* ups, drop packet */
561 printk(KERN_ERR "capi: put_message = %x\n", errcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 kfree_skb(skb);
563 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000564 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
567#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
568/* -------- function called by lower level -------------------------- */
569
570static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
571{
572 struct capidev *cdev = ap->private;
573#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000574 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct capiminor *mp;
576 u16 datahandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 struct capincci *np;
Tilman Schmidt7dc2ce52012-04-25 13:02:20 +0000578#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Jan Kiszka05b41492010-02-08 10:12:19 +0000580 mutex_lock(&cdev->lock);
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
583 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
Jan Kiszka05b41492010-02-08 10:12:19 +0000584 if ((info & 0xff00) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000587 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
591 skb_queue_tail(&cdev->recvqueue, skb);
592 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000593 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000595
Tilman Schmidt7dc2ce52012-04-25 13:02:20 +0000596#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
597 skb_queue_tail(&cdev->recvqueue, skb);
598 wake_up_interruptible(&cdev->recvwait);
599
600#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
601
Jan Kiszka05b41492010-02-08 10:12:19 +0000602 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (!np) {
604 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
605 skb_queue_tail(&cdev->recvqueue, skb);
606 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000607 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 mp = np->minorp;
611 if (!mp) {
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
Joe Perches475be4d2012-02-19 19:52:38 -0800617 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
David S. Miller8fb53b92011-05-19 18:20:29 -0400618 pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
619 datahandle, skb->len-CAPIMSG_LEN(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 skb_queue_tail(&mp->inqueue, skb);
Jan Kiszka68d73472010-02-08 10:12:39 +0000621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 handle_minor_recv(mp);
623
624 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
625
Joe Perches475be4d2012-02-19 19:52:38 -0800626 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
David S. Miller8fb53b92011-05-19 18:20:29 -0400627 pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
628 datahandle,
Joe Perches475be4d2012-02-19 19:52:38 -0800629 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 kfree_skb(skb);
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000631 capiminor_del_ack(mp, datahandle);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000632 tty = tty_port_tty_get(&mp->port);
633 if (tty) {
634 tty_wakeup(tty);
635 tty_kref_put(tty);
636 }
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000637 handle_minor_send(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 } else {
640 /* ups, let capi application handle it :-) */
641 skb_queue_tail(&cdev->recvqueue, skb);
642 wake_up_interruptible(&cdev->recvwait);
643 }
644#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000645
Jan Kiszka05b41492010-02-08 10:12:19 +0000646unlock_out:
Jan Kiszka05b41492010-02-08 10:12:19 +0000647 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650/* -------- file_operations for capidev ----------------------------- */
651
652static ssize_t
653capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
654{
Joe Perches54cbb1c2010-07-12 10:50:02 +0000655 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 struct sk_buff *skb;
657 size_t copied;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000658 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 if (!cdev->ap.applid)
661 return -ENODEV;
662
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000663 skb = skb_dequeue(&cdev->recvqueue);
664 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (file->f_flags & O_NONBLOCK)
666 return -EAGAIN;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000667 err = wait_event_interruptible(cdev->recvwait,
Joe Perches475be4d2012-02-19 19:52:38 -0800668 (skb = skb_dequeue(&cdev->recvqueue)));
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000669 if (err)
670 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672 if (skb->len > count) {
673 skb_queue_head(&cdev->recvqueue, skb);
674 return -EMSGSIZE;
675 }
676 if (copy_to_user(buf, skb->data, skb->len)) {
677 skb_queue_head(&cdev->recvqueue, skb);
678 return -EFAULT;
679 }
680 copied = skb->len;
681
682 kfree_skb(skb);
683
684 return copied;
685}
686
687static ssize_t
688capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
689{
Joe Perches54cbb1c2010-07-12 10:50:02 +0000690 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 struct sk_buff *skb;
692 u16 mlen;
693
694 if (!cdev->ap.applid)
695 return -ENODEV;
696
697 skb = alloc_skb(count, GFP_USER);
698 if (!skb)
699 return -ENOMEM;
700
701 if (copy_from_user(skb_put(skb, count), buf, count)) {
702 kfree_skb(skb);
703 return -EFAULT;
704 }
705 mlen = CAPIMSG_LEN(skb->data);
706 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
707 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
708 kfree_skb(skb);
709 return -EINVAL;
710 }
711 } else {
712 if (mlen != count) {
713 kfree_skb(skb);
714 return -EINVAL;
715 }
716 }
717 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
718
719 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Jan Kiszka05b41492010-02-08 10:12:19 +0000720 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000722 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724
725 cdev->errcode = capi20_put_message(&cdev->ap, skb);
726
727 if (cdev->errcode) {
728 kfree_skb(skb);
729 return -EIO;
730 }
731 return count;
732}
733
734static unsigned int
Joe Perches475be4d2012-02-19 19:52:38 -0800735capi_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Joe Perches54cbb1c2010-07-12 10:50:02 +0000737 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 unsigned int mask = 0;
739
740 if (!cdev->ap.applid)
741 return POLLERR;
742
743 poll_wait(file, &(cdev->recvwait), wait);
744 mask = POLLOUT | POLLWRNORM;
745 if (!skb_queue_empty(&cdev->recvqueue))
746 mask |= POLLIN | POLLRDNORM;
747 return mask;
748}
749
750static int
Arnd Bergmann703c6312010-04-27 00:24:02 +0200751capi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 capi_ioctl_struct data;
755 int retval = -EINVAL;
756 void __user *argp = (void __user *)arg;
757
758 switch (cmd) {
759 case CAPI_REGISTER:
Jan Kiszka05b41492010-02-08 10:12:19 +0000760 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Jan Kiszka05b41492010-02-08 10:12:19 +0000762 if (cdev->ap.applid) {
763 retval = -EEXIST;
764 goto register_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000766 if (copy_from_user(&cdev->ap.rparam, argp,
767 sizeof(struct capi_register_params))) {
768 retval = -EFAULT;
769 goto register_out;
770 }
771 cdev->ap.private = cdev;
772 cdev->ap.recv_message = capi_recv_message;
773 cdev->errcode = capi20_register(&cdev->ap);
774 retval = (int)cdev->ap.applid;
775 if (cdev->errcode) {
776 cdev->ap.applid = 0;
777 retval = -EIO;
778 }
779
780register_out:
781 mutex_unlock(&cdev->lock);
782 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 case CAPI_GET_VERSION:
Joe Perches475be4d2012-02-19 19:52:38 -0800785 if (copy_from_user(&data.contr, argp,
786 sizeof(data.contr)))
787 return -EFAULT;
788 cdev->errcode = capi20_get_version(data.contr, &data.version);
789 if (cdev->errcode)
790 return -EIO;
791 if (copy_to_user(argp, &data.version,
792 sizeof(data.version)))
793 return -EFAULT;
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000794 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 case CAPI_GET_SERIAL:
Joe Perches475be4d2012-02-19 19:52:38 -0800797 if (copy_from_user(&data.contr, argp,
798 sizeof(data.contr)))
799 return -EFAULT;
800 cdev->errcode = capi20_get_serial(data.contr, data.serial);
801 if (cdev->errcode)
802 return -EIO;
803 if (copy_to_user(argp, data.serial,
804 sizeof(data.serial)))
805 return -EFAULT;
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000806 return 0;
807
Joe Perches475be4d2012-02-19 19:52:38 -0800808 case CAPI_GET_PROFILE:
Joe Perches475be4d2012-02-19 19:52:38 -0800809 if (copy_from_user(&data.contr, argp,
810 sizeof(data.contr)))
811 return -EFAULT;
812
813 if (data.contr == 0) {
814 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (cdev->errcode)
816 return -EIO;
Joe Perches475be4d2012-02-19 19:52:38 -0800817
818 retval = copy_to_user(argp,
819 &data.profile.ncontroller,
820 sizeof(data.profile.ncontroller));
821
822 } else {
823 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
824 if (cdev->errcode)
825 return -EIO;
826
827 retval = copy_to_user(argp, &data.profile,
828 sizeof(data.profile));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
Joe Perches475be4d2012-02-19 19:52:38 -0800830 if (retval)
831 return -EFAULT;
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000832 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 case CAPI_GET_MANUFACTURER:
Joe Perches475be4d2012-02-19 19:52:38 -0800835 if (copy_from_user(&data.contr, argp,
836 sizeof(data.contr)))
837 return -EFAULT;
838 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
839 if (cdev->errcode)
840 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Joe Perches475be4d2012-02-19 19:52:38 -0800842 if (copy_to_user(argp, data.manufacturer,
843 sizeof(data.manufacturer)))
844 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000846 return 0;
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 case CAPI_GET_ERRCODE:
849 data.errcode = cdev->errcode;
850 cdev->errcode = CAPI_NOERROR;
851 if (arg) {
852 if (copy_to_user(argp, &data.errcode,
853 sizeof(data.errcode)))
854 return -EFAULT;
855 }
856 return data.errcode;
857
858 case CAPI_INSTALLED:
859 if (capi20_isinstalled() == CAPI_NOERROR)
860 return 0;
861 return -ENXIO;
862
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000863 case CAPI_MANUFACTURER_CMD: {
Joe Perches475be4d2012-02-19 19:52:38 -0800864 struct capi_manufacturer_cmd mcmd;
865 if (!capable(CAP_SYS_ADMIN))
866 return -EPERM;
867 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
868 return -EFAULT;
869 return capi20_manufacturer(mcmd.cmd, mcmd.data);
870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 case CAPI_SET_FLAGS:
Jan Kiszka05b41492010-02-08 10:12:19 +0000872 case CAPI_CLR_FLAGS: {
873 unsigned userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Jan Kiszka05b41492010-02-08 10:12:19 +0000875 if (copy_from_user(&userflags, argp, sizeof(userflags)))
876 return -EFAULT;
877
878 mutex_lock(&cdev->lock);
879 if (cmd == CAPI_SET_FLAGS)
880 cdev->userflags |= userflags;
881 else
882 cdev->userflags &= ~userflags;
883 mutex_unlock(&cdev->lock);
884 return 0;
885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 case CAPI_GET_FLAGS:
887 if (copy_to_user(argp, &cdev->userflags,
888 sizeof(cdev->userflags)))
889 return -EFAULT;
890 return 0;
891
Tilman Schmidt7dc2ce52012-04-25 13:02:20 +0000892#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
893 case CAPI_NCCI_OPENCOUNT:
894 return 0;
895
896#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka05b41492010-02-08 10:12:19 +0000897 case CAPI_NCCI_OPENCOUNT: {
898 struct capincci *nccip;
899 unsigned ncci;
900 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Jan Kiszka05b41492010-02-08 10:12:19 +0000902 if (copy_from_user(&ncci, argp, sizeof(ncci)))
903 return -EFAULT;
904
905 mutex_lock(&cdev->lock);
906 nccip = capincci_find(cdev, (u32)ncci);
907 if (nccip)
908 count = capincci_minor_opencount(nccip);
909 mutex_unlock(&cdev->lock);
910 return count;
911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Jan Kiszka05b41492010-02-08 10:12:19 +0000913 case CAPI_NCCI_GETUNIT: {
914 struct capincci *nccip;
915 struct capiminor *mp;
916 unsigned ncci;
917 int unit = -ESRCH;
918
919 if (copy_from_user(&ncci, argp, sizeof(ncci)))
920 return -EFAULT;
921
922 mutex_lock(&cdev->lock);
923 nccip = capincci_find(cdev, (u32)ncci);
924 if (nccip) {
925 mp = nccip->minorp;
926 if (mp)
927 unit = mp->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000929 mutex_unlock(&cdev->lock);
930 return unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000932#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
933
934 default:
935 return -EINVAL;
936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
Arnd Bergmann703c6312010-04-27 00:24:02 +0200939static long
940capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
941{
942 int ret;
943
Arnd Bergmann76a64922010-07-11 11:18:53 +0000944 mutex_lock(&capi_mutex);
Arnd Bergmann703c6312010-04-27 00:24:02 +0200945 ret = capi_ioctl(file, cmd, arg);
Arnd Bergmann76a64922010-07-11 11:18:53 +0000946 mutex_unlock(&capi_mutex);
Arnd Bergmann703c6312010-04-27 00:24:02 +0200947
948 return ret;
949}
950
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000951static int capi_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000953 struct capidev *cdev;
954
955 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
956 if (!cdev)
957 return -ENOMEM;
958
Jan Kiszka05b41492010-02-08 10:12:19 +0000959 mutex_init(&cdev->lock);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000960 skb_queue_head_init(&cdev->recvqueue);
961 init_waitqueue_head(&cdev->recvwait);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000962 INIT_LIST_HEAD(&cdev->nccis);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000963 file->private_data = cdev;
964
965 mutex_lock(&capidev_list_lock);
966 list_add_tail(&cdev->list, &capidev_list);
967 mutex_unlock(&capidev_list_lock);
968
969 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000972static int capi_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000974 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000976 mutex_lock(&capidev_list_lock);
977 list_del(&cdev->list);
978 mutex_unlock(&capidev_list_lock);
979
Jan Kiszka05b41492010-02-08 10:12:19 +0000980 if (cdev->ap.applid)
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000981 capi20_release(&cdev->ap);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000982 skb_queue_purge(&cdev->recvqueue);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000983 capincci_free(cdev, 0xffffffff);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000984
985 kfree(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return 0;
987}
988
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800989static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 .owner = THIS_MODULE,
992 .llseek = no_llseek,
993 .read = capi_read,
994 .write = capi_write,
995 .poll = capi_poll,
Arnd Bergmann703c6312010-04-27 00:24:02 +0200996 .unlocked_ioctl = capi_unlocked_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 .open = capi_open,
998 .release = capi_release,
999};
1000
1001#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1002/* -------- tty_operations for capincci ----------------------------- */
1003
Jan Kiszka46324512010-02-08 10:12:28 +00001004static int
1005capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1006{
Jiri Slaby410235f2012-03-05 14:52:01 +01001007 struct capiminor *mp = capiminor_get(tty->index);
Jiri Slaby81f58352012-01-30 21:14:30 +01001008 int ret = tty_standard_install(driver, tty);
Jan Kiszka46324512010-02-08 10:12:28 +00001009
Jiri Slaby81f58352012-01-30 21:14:30 +01001010 if (ret == 0)
Jan Kiszka46324512010-02-08 10:12:28 +00001011 tty->driver_data = mp;
Jiri Slaby81f58352012-01-30 21:14:30 +01001012 else
Jan Kiszka46324512010-02-08 10:12:28 +00001013 capiminor_put(mp);
1014 return ret;
1015}
1016
1017static void capinc_tty_cleanup(struct tty_struct *tty)
1018{
1019 struct capiminor *mp = tty->driver_data;
1020 tty->driver_data = NULL;
1021 capiminor_put(mp);
1022}
1023
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001024static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Jan Kiszka46324512010-02-08 10:12:28 +00001026 struct capiminor *mp = tty->driver_data;
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001027 int err;
1028
1029 err = tty_port_open(&mp->port, tty, filp);
1030 if (err)
1031 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 handle_minor_recv(mp);
1034 return 0;
1035}
1036
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001037static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Jan Kiszka46324512010-02-08 10:12:28 +00001039 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001041 tty_port_close(&mp->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
Jan Kiszka6576c282010-02-08 10:12:32 +00001044static int capinc_tty_write(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 const unsigned char *buf, int count)
1046{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001047 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 struct sk_buff *skb;
1049
David S. Miller8fb53b92011-05-19 18:20:29 -04001050 pr_debug("capinc_tty_write(count=%d)\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001052 spin_lock_bh(&mp->outlock);
1053 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (skb) {
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001055 mp->outskb = NULL;
1056 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 mp->outbytes += skb->len;
1058 }
1059
Joe Perches475be4d2012-02-19 19:52:38 -08001060 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + count, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (!skb) {
1062 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001063 spin_unlock_bh(&mp->outlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return -ENOMEM;
1065 }
1066
1067 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1068 memcpy(skb_put(skb, count), buf, count);
1069
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001070 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001072 spin_unlock_bh(&mp->outlock);
1073
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001074 handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return count;
1077}
1078
Alan Coxf2545a72008-04-30 00:54:09 -07001079static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001081 struct capiminor *mp = tty->driver_data;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001082 bool invoke_send = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct sk_buff *skb;
Alan Coxf2545a72008-04-30 00:54:09 -07001084 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
David S. Miller8fb53b92011-05-19 18:20:29 -04001086 pr_debug("capinc_put_char(%u)\n", ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
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) {
1091 if (skb_tailroom(skb) > 0) {
1092 *(skb_put(skb, 1)) = ch;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001093 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001095 mp->outskb = NULL;
1096 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001098 invoke_send = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001100
Joe Perches475be4d2012-02-19 19:52:38 -08001101 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + CAPI_MAX_BLKSIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (skb) {
1103 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1104 *(skb_put(skb, 1)) = ch;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001105 mp->outskb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 } else {
1107 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
Alan Coxf2545a72008-04-30 00:54:09 -07001108 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001110
1111unlock_out:
1112 spin_unlock_bh(&mp->outlock);
1113
1114 if (invoke_send)
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001115 handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001116
Alan Coxf2545a72008-04-30 00:54:09 -07001117 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
1120static void capinc_tty_flush_chars(struct tty_struct *tty)
1121{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001122 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 struct sk_buff *skb;
1124
David S. Miller8fb53b92011-05-19 18:20:29 -04001125 pr_debug("capinc_tty_flush_chars\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001127 spin_lock_bh(&mp->outlock);
1128 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (skb) {
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001130 mp->outskb = NULL;
1131 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001133 spin_unlock_bh(&mp->outlock);
1134
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001135 handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001136 } else
1137 spin_unlock_bh(&mp->outlock);
1138
1139 handle_minor_recv(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
1142static int capinc_tty_write_room(struct tty_struct *tty)
1143{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001144 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 int room;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1148 room *= CAPI_MAX_BLKSIZE;
David S. Miller8fb53b92011-05-19 18:20:29 -04001149 pr_debug("capinc_tty_write_room = %d\n", room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return room;
1151}
1152
Adrian Bunk408b6642005-05-01 08:59:29 -07001153static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001155 struct capiminor *mp = tty->driver_data;
1156
David S. Miller8fb53b92011-05-19 18:20:29 -04001157 pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1158 mp->outbytes, mp->nack,
1159 skb_queue_len(&mp->outqueue),
1160 skb_queue_len(&mp->inqueue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return mp->outbytes;
1162}
1163
Alan Cox6caa76b2011-02-14 16:27:22 +00001164static int capinc_tty_ioctl(struct tty_struct *tty,
Joe Perches475be4d2012-02-19 19:52:38 -08001165 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Alan Cox6caa76b2011-02-14 16:27:22 +00001167 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
Joe Perches475be4d2012-02-19 19:52:38 -08001170static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
David S. Miller8fb53b92011-05-19 18:20:29 -04001172 pr_debug("capinc_tty_set_termios\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
Jan Kiszka2c8df722010-02-08 10:12:30 +00001175static void capinc_tty_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001177 struct capiminor *mp = tty->driver_data;
David S. Miller8fb53b92011-05-19 18:20:29 -04001178 pr_debug("capinc_tty_throttle\n");
Jan Kiszka2c8df722010-02-08 10:12:30 +00001179 mp->ttyinstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
Jan Kiszka2c8df722010-02-08 10:12:30 +00001182static void capinc_tty_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001184 struct capiminor *mp = tty->driver_data;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001185
David S. Miller8fb53b92011-05-19 18:20:29 -04001186 pr_debug("capinc_tty_unthrottle\n");
Jan Kiszka2c8df722010-02-08 10:12:30 +00001187 mp->ttyinstop = 0;
1188 handle_minor_recv(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191static void capinc_tty_stop(struct tty_struct *tty)
1192{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001193 struct capiminor *mp = tty->driver_data;
1194
David S. Miller8fb53b92011-05-19 18:20:29 -04001195 pr_debug("capinc_tty_stop\n");
Jan Kiszka2c8df722010-02-08 10:12:30 +00001196 mp->ttyoutstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
1199static void capinc_tty_start(struct tty_struct *tty)
1200{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001201 struct capiminor *mp = tty->driver_data;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001202
David S. Miller8fb53b92011-05-19 18:20:29 -04001203 pr_debug("capinc_tty_start\n");
Jan Kiszka2c8df722010-02-08 10:12:30 +00001204 mp->ttyoutstop = 0;
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001205 handle_minor_send(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
1208static void capinc_tty_hangup(struct tty_struct *tty)
1209{
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001210 struct capiminor *mp = tty->driver_data;
1211
David S. Miller8fb53b92011-05-19 18:20:29 -04001212 pr_debug("capinc_tty_hangup\n");
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001213 tty_port_hangup(&mp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Alan Cox9e989662008-07-22 11:18:03 +01001216static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
David S. Miller8fb53b92011-05-19 18:20:29 -04001218 pr_debug("capinc_tty_break_ctl(%d)\n", state);
Alan Cox9e989662008-07-22 11:18:03 +01001219 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
1222static void capinc_tty_flush_buffer(struct tty_struct *tty)
1223{
David S. Miller8fb53b92011-05-19 18:20:29 -04001224 pr_debug("capinc_tty_flush_buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
1227static void capinc_tty_set_ldisc(struct tty_struct *tty)
1228{
David S. Miller8fb53b92011-05-19 18:20:29 -04001229 pr_debug("capinc_tty_set_ldisc\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
1232static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1233{
David S. Miller8fb53b92011-05-19 18:20:29 -04001234 pr_debug("capinc_tty_send_xchar(%d)\n", ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001237static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 .open = capinc_tty_open,
1239 .close = capinc_tty_close,
1240 .write = capinc_tty_write,
1241 .put_char = capinc_tty_put_char,
1242 .flush_chars = capinc_tty_flush_chars,
1243 .write_room = capinc_tty_write_room,
1244 .chars_in_buffer = capinc_tty_chars_in_buffer,
1245 .ioctl = capinc_tty_ioctl,
1246 .set_termios = capinc_tty_set_termios,
1247 .throttle = capinc_tty_throttle,
1248 .unthrottle = capinc_tty_unthrottle,
1249 .stop = capinc_tty_stop,
1250 .start = capinc_tty_start,
1251 .hangup = capinc_tty_hangup,
1252 .break_ctl = capinc_tty_break_ctl,
1253 .flush_buffer = capinc_tty_flush_buffer,
1254 .set_ldisc = capinc_tty_set_ldisc,
1255 .send_xchar = capinc_tty_send_xchar,
Jan Kiszka46324512010-02-08 10:12:28 +00001256 .install = capinc_tty_install,
1257 .cleanup = capinc_tty_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258};
1259
Jan Kiszkae76b1542010-02-08 10:12:24 +00001260static int __init capinc_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 struct tty_driver *drv;
Jan Kiszkae76b1542010-02-08 10:12:24 +00001263 int err;
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (capi_ttyminors > CAPINC_MAX_PORTS)
1266 capi_ttyminors = CAPINC_MAX_PORTS;
1267 if (capi_ttyminors <= 0)
1268 capi_ttyminors = CAPINC_NR_PORTS;
1269
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001270 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1271 GFP_KERNEL);
1272 if (!capiminors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return -ENOMEM;
1274
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001275 drv = alloc_tty_driver(capi_ttyminors);
1276 if (!drv) {
1277 kfree(capiminors);
1278 return -ENOMEM;
1279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 drv->driver_name = "capi_nc";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 drv->name = "capi";
Jan Kiszkae95ac142010-02-08 10:12:26 +00001282 drv->major = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 drv->minor_start = 0;
1284 drv->type = TTY_DRIVER_TYPE_SERIAL;
1285 drv->subtype = SERIAL_TYPE_NORMAL;
1286 drv->init_termios = tty_std_termios;
1287 drv->init_termios.c_iflag = ICRNL;
1288 drv->init_termios.c_oflag = OPOST | ONLCR;
1289 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1290 drv->init_termios.c_lflag = 0;
Jan Kiszka40fb2d02010-02-08 10:12:25 +00001291 drv->flags =
1292 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1293 TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 tty_set_operations(drv, &capinc_ops);
Jan Kiszkae76b1542010-02-08 10:12:24 +00001295
1296 err = tty_register_driver(drv);
1297 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 put_tty_driver(drv);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001299 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 printk(KERN_ERR "Couldn't register capi_nc driver\n");
Jan Kiszkae76b1542010-02-08 10:12:24 +00001301 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303 capinc_tty_driver = drv;
1304 return 0;
1305}
1306
Jan Kiszkae76b1542010-02-08 10:12:24 +00001307static void __exit capinc_tty_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Jan Kiszkae76b1542010-02-08 10:12:24 +00001309 tty_unregister_driver(capinc_tty_driver);
1310 put_tty_driver(capinc_tty_driver);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001311 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
Jan Kiszka501c87a2010-02-08 10:12:16 +00001314#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1315
1316static inline int capinc_tty_init(void)
1317{
1318 return 0;
1319}
1320
1321static inline void capinc_tty_exit(void) { }
1322
1323#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325/* -------- /proc functions ----------------------------------------- */
1326
1327/*
1328 * /proc/capi/capi20:
1329 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1330 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001331static int capi20_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Joe Perches475be4d2012-02-19 19:52:38 -08001333 struct capidev *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001336 mutex_lock(&capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 list_for_each(l, &capidev_list) {
1338 cdev = list_entry(l, struct capidev, list);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001339 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001340 cdev->ap.applid,
1341 cdev->ap.nrecvctlpkt,
1342 cdev->ap.nrecvdatapkt,
1343 cdev->ap.nsentctlpkt,
1344 cdev->ap.nsentdatapkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001346 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001347 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001350static int capi20_proc_open(struct inode *inode, struct file *file)
1351{
1352 return single_open(file, capi20_proc_show, NULL);
1353}
1354
1355static const struct file_operations capi20_proc_fops = {
1356 .owner = THIS_MODULE,
1357 .open = capi20_proc_open,
1358 .read = seq_read,
1359 .llseek = seq_lseek,
1360 .release = single_release,
1361};
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363/*
1364 * /proc/capi/capi20ncci:
1365 * applid ncci
1366 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001367static int capi20ncci_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Jan Kiszka884f5c42010-02-08 10:12:22 +00001369 struct capidev *cdev;
1370 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001372 mutex_lock(&capidev_list_lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001373 list_for_each_entry(cdev, &capidev_list, list) {
Jan Kiszka05b41492010-02-08 10:12:19 +00001374 mutex_lock(&cdev->lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001375 list_for_each_entry(np, &cdev->nccis, list)
1376 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
Jan Kiszka05b41492010-02-08 10:12:19 +00001377 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001379 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001380 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001383static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1384{
1385 return single_open(file, capi20ncci_proc_show, NULL);
1386}
1387
1388static const struct file_operations capi20ncci_proc_fops = {
1389 .owner = THIS_MODULE,
1390 .open = capi20ncci_proc_open,
1391 .read = seq_read,
1392 .llseek = seq_lseek,
1393 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394};
1395
1396static void __init proc_init(void)
1397{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001398 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1399 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
1402static void __exit proc_exit(void)
1403{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001404 remove_proc_entry("capi/capi20", NULL);
1405 remove_proc_entry("capi/capi20ncci", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406}
1407
1408/* -------- init function and module interface ---------------------- */
1409
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411static int __init capi_init(void)
1412{
Jan Kiszka88549d62010-02-08 10:12:09 +00001413 const char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001414 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Andrew Morton6d9eac32006-03-28 01:56:19 -08001416 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1417 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001419 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001421 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (IS_ERR(capi_class)) {
1423 unregister_chrdev(capi_major, "capi20");
1424 return PTR_ERR(capi_class);
1425 }
1426
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -07001427 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (capinc_tty_init() < 0) {
Tony Jonesd78b0362007-09-25 02:03:03 +02001430 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001431 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 unregister_chrdev(capi_major, "capi20");
1433 return -ENOMEM;
1434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 proc_init();
1437
Jan Kiszka1f90d662011-04-06 10:58:37 +00001438#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Joe Perches475be4d2012-02-19 19:52:38 -08001439 compileinfo = " (middleware)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440#else
Joe Perches475be4d2012-02-19 19:52:38 -08001441 compileinfo = " (no middleware)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442#endif
Jan Kiszka88549d62010-02-08 10:12:09 +00001443 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1444 capi_major, compileinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 return 0;
1447}
1448
1449static void __exit capi_exit(void)
1450{
1451 proc_exit();
1452
Tony Jonesd78b0362007-09-25 02:03:03 +02001453 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001454 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 capinc_tty_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
1460module_init(capi_init);
1461module_exit(capi_exit);