blob: 3c3ad42f22bfd29da879255593fe3cb7a21e1b3e [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
Randy Dunlap20fbdc32018-07-07 08:31:15 -070012#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/errno.h>
15#include <linux/kernel.h>
16#include <linux/major.h>
17#include <linux/sched.h>
18#include <linux/slab.h>
19#include <linux/fcntl.h>
20#include <linux/fs.h>
21#include <linux/signal.h>
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -070022#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/timer.h>
25#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/tty.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/netdevice.h>
28#include <linux/ppp_defs.h>
Paul Mackerras4b32da2b2012-03-04 12:56:55 +000029#include <linux/ppp-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/skbuff.h>
31#include <linux/proc_fs.h>
Alexey Dobriyan9a58a802010-01-14 03:10:54 -080032#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/poll.h>
34#include <linux/capi.h>
35#include <linux/kernelcapi.h>
36#include <linux/init.h>
37#include <linux/device.h>
38#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/isdn/capiutil.h>
40#include <linux/isdn/capicmd.h>
Jan Kiszka90926f02010-02-08 10:12:06 +000041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
43MODULE_AUTHOR("Carsten Paeth");
44MODULE_LICENSE("GPL");
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* -------- driver information -------------------------------------- */
47
Arnd Bergmann76a64922010-07-11 11:18:53 +000048static DEFINE_MUTEX(capi_mutex);
gregkh@suse.de56b22932005-03-23 10:01:41 -080049static struct class *capi_class;
Adrian Bunk408b6642005-05-01 08:59:29 -070050static int capi_major = 68; /* allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52module_param_named(major, capi_major, uint, 0);
Jan Kiszka501c87a2010-02-08 10:12:16 +000053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +000055#define CAPINC_NR_PORTS 32
56#define CAPINC_MAX_PORTS 256
57
Jan Kiszka501c87a2010-02-08 10:12:16 +000058static int capi_ttyminors = CAPINC_NR_PORTS;
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060module_param_named(ttyminors, capi_ttyminors, uint, 0);
61#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
62
63/* -------- defines ------------------------------------------------- */
64
65#define CAPINC_MAX_RECVQUEUE 10
66#define CAPINC_MAX_SENDQUEUE 10
67#define CAPI_MAX_BLKSIZE 2048
68
69/* -------- data structures ----------------------------------------- */
70
71struct capidev;
72struct capincci;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073struct capiminor;
74
Jan Kiszka42651b52010-02-08 10:12:37 +000075struct ackqueue_entry {
Michael Buesch6aa65472006-06-26 00:25:30 -070076 struct list_head list;
77 u16 datahandle;
78};
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080struct capiminor {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 unsigned int minor;
82
Jan Kiszka42792712010-02-08 10:12:38 +000083 struct capi20_appl *ap;
84 u32 ncci;
85 atomic_t datahandle;
86 atomic_t msgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Jan Kiszkafb4b4882010-02-08 10:12:29 +000088 struct tty_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 int ttyinstop;
90 int ttyoutstop;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Jan Kiszkadfbb84f2010-02-08 10:12:40 +000092 struct sk_buff_head inqueue;
93
94 struct sk_buff_head outqueue;
95 int outbytes;
96 struct sk_buff *outskb;
97 spinlock_t outlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 /* transmit path */
Michael Buesch6aa65472006-06-26 00:25:30 -0700100 struct list_head ackqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 int nack;
Michael Buesch6aa65472006-06-26 00:25:30 -0700102 spinlock_t ackqlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105struct capincci {
Jan Kiszka884f5c42010-02-08 10:12:22 +0000106 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 u32 ncci;
108 struct capidev *cdev;
109#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
110 struct capiminor *minorp;
111#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
112};
113
114struct capidev {
115 struct list_head list;
116 struct capi20_appl ap;
117 u16 errcode;
118 unsigned userflags;
119
120 struct sk_buff_head recvqueue;
121 wait_queue_head_t recvwait;
122
Jan Kiszka884f5c42010-02-08 10:12:22 +0000123 struct list_head nccis;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Jan Kiszka05b41492010-02-08 10:12:19 +0000125 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
128/* -------- global variables ---------------------------------------- */
129
Jan Kiszkab8f433d2010-02-08 10:12:17 +0000130static DEFINE_MUTEX(capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static LIST_HEAD(capidev_list);
132
133#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +0000134
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000135static DEFINE_SPINLOCK(capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000136static struct capiminor **capiminors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000138static struct tty_driver *capinc_tty_driver;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/* -------- datahandles --------------------------------------------- */
141
Jan Kiszka501c87a2010-02-08 10:12:16 +0000142static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Jan Kiszka42651b52010-02-08 10:12:37 +0000144 struct ackqueue_entry *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 n = kmalloc(sizeof(*n), GFP_ATOMIC);
Michael Buesch6aa65472006-06-26 00:25:30 -0700147 if (unlikely(!n)) {
148 printk(KERN_ERR "capi: alloc datahandle failed\n");
149 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 n->datahandle = datahandle;
Michael Buesch6aa65472006-06-26 00:25:30 -0700152 INIT_LIST_HEAD(&n->list);
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000153 spin_lock_bh(&mp->ackqlock);
Michael Buesch6aa65472006-06-26 00:25:30 -0700154 list_add_tail(&n->list, &mp->ackqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 mp->nack++;
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000156 spin_unlock_bh(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return 0;
158}
159
160static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
161{
Jan Kiszka42651b52010-02-08 10:12:37 +0000162 struct ackqueue_entry *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000164 spin_lock_bh(&mp->ackqlock);
Michael Buesch6aa65472006-06-26 00:25:30 -0700165 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
Joe Perches475be4d2012-02-19 19:52:38 -0800166 if (p->datahandle == datahandle) {
Michael Buesch6aa65472006-06-26 00:25:30 -0700167 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 mp->nack--;
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000169 spin_unlock_bh(&mp->ackqlock);
170 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return 0;
172 }
173 }
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000174 spin_unlock_bh(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return -1;
176}
177
178static void capiminor_del_all_ack(struct capiminor *mp)
179{
Jan Kiszka42651b52010-02-08 10:12:37 +0000180 struct ackqueue_entry *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Michael Buesch6aa65472006-06-26 00:25:30 -0700182 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
183 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 kfree(p);
185 mp->nack--;
186 }
187}
188
189
190/* -------- struct capiminor ---------------------------------------- */
191
Jiri Slaby55bef83c2012-11-15 09:49:50 +0100192static void capiminor_destroy(struct tty_port *port)
193{
194 struct capiminor *mp = container_of(port, struct capiminor, port);
195
196 kfree_skb(mp->outskb);
197 skb_queue_purge(&mp->inqueue);
198 skb_queue_purge(&mp->outqueue);
199 capiminor_del_all_ack(mp);
200 kfree(mp);
201}
202
203static const struct tty_port_operations capiminor_port_ops = {
204 .destruct = capiminor_destroy,
205};
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
208{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000209 struct capiminor *mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000210 struct device *dev;
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000211 unsigned int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000213 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
Joe Perches475be4d2012-02-19 19:52:38 -0800214 if (!mp) {
215 printk(KERN_ERR "capi: can't alloc capiminor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return NULL;
217 }
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 mp->ap = ap;
220 mp->ncci = ncci;
Michael Buesch6aa65472006-06-26 00:25:30 -0700221 INIT_LIST_HEAD(&mp->ackqueue);
222 spin_lock_init(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 skb_queue_head_init(&mp->inqueue);
225 skb_queue_head_init(&mp->outqueue);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000226 spin_lock_init(&mp->outlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000228 tty_port_init(&mp->port);
229 mp->port.ops = &capiminor_port_ops;
230
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000231 /* Allocate the least unused minor number. */
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000232 spin_lock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000233 for (minor = 0; minor < capi_ttyminors; minor++)
234 if (!capiminors[minor]) {
235 capiminors[minor] = mp;
236 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000238 spin_unlock(&capiminors_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000240 if (minor == capi_ttyminors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 printk(KERN_NOTICE "capi: out of minors\n");
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000242 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000245 mp->minor = minor;
246
Jiri Slaby734cc172012-08-07 21:47:47 +0200247 dev = tty_port_register_device(&mp->port, capinc_tty_driver, minor,
248 NULL);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000249 if (IS_ERR(dev))
250 goto err_out2;
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000253
254err_out2:
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000255 spin_lock(&capiminors_lock);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000256 capiminors[minor] = NULL;
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000257 spin_unlock(&capiminors_lock);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000258
259err_out1:
Jiri Slaby55bef83c2012-11-15 09:49:50 +0100260 tty_port_put(&mp->port);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000261 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000264static struct capiminor *capiminor_get(unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000266 struct capiminor *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000268 spin_lock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000269 mp = capiminors[minor];
Jan Kiszka0159d542010-02-08 10:12:27 +0000270 if (mp)
Jiri Slaby55bef83c2012-11-15 09:49:50 +0100271 tty_port_get(&mp->port);
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000272 spin_unlock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000273
274 return mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Jan Kiszka0159d542010-02-08 10:12:27 +0000277static inline void capiminor_put(struct capiminor *mp)
278{
Jiri Slaby55bef83c2012-11-15 09:49:50 +0100279 tty_port_put(&mp->port);
Jan Kiszka0159d542010-02-08 10:12:27 +0000280}
281
282static void capiminor_free(struct capiminor *mp)
283{
Jan Kiszka0159d542010-02-08 10:12:27 +0000284 tty_unregister_device(capinc_tty_driver, mp->minor);
285
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000286 spin_lock(&capiminors_lock);
Jan Kiszka0159d542010-02-08 10:12:27 +0000287 capiminors[mp->minor] = NULL;
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000288 spin_unlock(&capiminors_lock);
Jan Kiszka0159d542010-02-08 10:12:27 +0000289
290 capiminor_put(mp);
291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/* -------- struct capincci ----------------------------------------- */
294
Jan Kiszka501c87a2010-02-08 10:12:16 +0000295static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Jan Kiszka1f90d662011-04-06 10:58:37 +0000297 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
298 np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000299}
300
301static void capincci_free_minor(struct capincci *np)
302{
303 struct capiminor *mp = np->minorp;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000304 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000305
306 if (mp) {
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000307 tty = tty_port_tty_get(&mp->port);
308 if (tty) {
Jan Kiszka30bced92010-02-08 10:12:31 +0000309 tty_vhangup(tty);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000310 tty_kref_put(tty);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000311 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000312
313 capiminor_free(mp);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000314 }
315}
316
317static inline unsigned int capincci_minor_opencount(struct capincci *np)
318{
319 struct capiminor *mp = np->minorp;
Jan Kiszkaa84fdf42010-02-08 10:12:34 +0000320 unsigned int count = 0;
321 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000322
Jan Kiszkaa84fdf42010-02-08 10:12:34 +0000323 if (mp) {
324 tty = tty_port_tty_get(&mp->port);
325 if (tty) {
326 count = tty->count;
327 tty_kref_put(tty);
328 }
329 }
330 return count;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000331}
332
333#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
334
335static inline void
336capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
337static inline void capincci_free_minor(struct capincci *np) { }
338
Jan Kiszka501c87a2010-02-08 10:12:16 +0000339#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
340
341static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
342{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000343 struct capincci *np;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000344
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000345 np = kzalloc(sizeof(*np), GFP_KERNEL);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000346 if (!np)
347 return NULL;
348 np->ncci = ncci;
349 np->cdev = cdev;
350
351 capincci_alloc_minor(cdev, np);
352
Jan Kiszka884f5c42010-02-08 10:12:22 +0000353 list_add_tail(&np->list, &cdev->nccis);
354
355 return np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358static void capincci_free(struct capidev *cdev, u32 ncci)
359{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000360 struct capincci *np, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Jan Kiszka884f5c42010-02-08 10:12:22 +0000362 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (ncci == 0xffffffff || np->ncci == ncci) {
Jan Kiszka501c87a2010-02-08 10:12:16 +0000364 capincci_free_minor(np);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000365 list_del(&np->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 kfree(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Tilman Schmidt7dc2ce52012-04-25 13:02:20 +0000370#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
372{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000373 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Jan Kiszka884f5c42010-02-08 10:12:22 +0000375 list_for_each_entry(np, &cdev->nccis, list)
376 if (np->ncci == ncci)
377 return np;
378 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/* -------- handle data queue --------------------------------------- */
382
383static struct sk_buff *
384gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
385{
386 struct sk_buff *nskb;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000387 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (nskb) {
Joe Perches475be4d2012-02-19 19:52:38 -0800389 u16 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
391 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
392 capimsg_setu16(s, 2, mp->ap->applid);
393 capimsg_setu8 (s, 4, CAPI_DATA_B3);
394 capimsg_setu8 (s, 5, CAPI_RESP);
Jan Kiszka42792712010-02-08 10:12:38 +0000395 capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 capimsg_setu32(s, 8, mp->ncci);
397 capimsg_setu16(s, 12, datahandle);
398 }
399 return nskb;
400}
401
402static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
403{
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000404 unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000405 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 struct sk_buff *nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 u16 errcode, datahandle;
408 struct tty_ldisc *ld;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000409 int ret = -1;
410
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000411 tty = tty_port_tty_get(&mp->port);
412 if (!tty) {
David S. Miller8fb53b92011-05-19 18:20:29 -0400413 pr_debug("capi: currently no receiver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return -1;
415 }
Joe Perches475be4d2012-02-19 19:52:38 -0800416
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000417 ld = tty_ldisc_ref(tty);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000418 if (!ld) {
419 /* fatal error, do not requeue */
420 ret = 0;
421 kfree_skb(skb);
422 goto deref_tty;
423 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000424
Alan Coxa352def2008-07-16 21:53:12 +0100425 if (ld->ops->receive_buf == NULL) {
David S. Miller8fb53b92011-05-19 18:20:29 -0400426 pr_debug("capi: ldisc has no receive_buf function\n");
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000427 /* fatal error, do not requeue */
428 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430 if (mp->ttyinstop) {
David S. Miller8fb53b92011-05-19 18:20:29 -0400431 pr_debug("capi: recv tty throttled\n");
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000432 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000434
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000435 if (tty->receive_room < datalen) {
David S. Miller8fb53b92011-05-19 18:20:29 -0400436 pr_debug("capi: no room in tty\n");
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000437 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000439
440 nskb = gen_data_b3_resp_for(mp, skb);
441 if (!nskb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000443 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000445
446 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 errcode = capi20_put_message(mp->ap, nskb);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000449
450 if (errcode == CAPI_NOERROR) {
451 skb_pull(skb, CAPIMSG_LEN(skb->data));
David S. Miller8fb53b92011-05-19 18:20:29 -0400452 pr_debug("capi: DATA_B3_RESP %u len=%d => ldisc\n",
453 datahandle, skb->len);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000454 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
455 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800457 errcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 kfree_skb(nskb);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000459
460 if (errcode == CAPI_SENDQUEUEFULL)
461 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000463
464free_skb:
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000465 ret = 0;
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000466 kfree_skb(skb);
467
468deref_ldisc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 tty_ldisc_deref(ld);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000470
471deref_tty:
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000472 tty_kref_put(tty);
473 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476static void handle_minor_recv(struct capiminor *mp)
477{
478 struct sk_buff *skb;
Jan Kiszka68d73472010-02-08 10:12:39 +0000479
480 while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (handle_recv_skb(mp, skb) < 0) {
482 skb_queue_head(&mp->inqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return;
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000487static void handle_minor_send(struct capiminor *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000489 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct sk_buff *skb;
491 u16 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 u16 errcode;
493 u16 datahandle;
494
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000495 tty = tty_port_tty_get(&mp->port);
496 if (!tty)
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000497 return;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000498
499 if (mp->ttyoutstop) {
David S. Miller8fb53b92011-05-19 18:20:29 -0400500 pr_debug("capi: send: tty stopped\n");
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000501 tty_kref_put(tty);
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000502 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000505 while (1) {
506 spin_lock_bh(&mp->outlock);
507 skb = __skb_dequeue(&mp->outqueue);
508 if (!skb) {
509 spin_unlock_bh(&mp->outlock);
510 break;
511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 len = (u16)skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000513 mp->outbytes -= len;
514 spin_unlock_bh(&mp->outlock);
515
516 datahandle = atomic_inc_return(&mp->datahandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
518 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
519 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
520 capimsg_setu16(skb->data, 2, mp->ap->applid);
521 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
522 capimsg_setu8 (skb->data, 5, CAPI_REQ);
Jan Kiszka42792712010-02-08 10:12:38 +0000523 capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
Andrew Morton5e6c20a2007-07-17 04:04:22 -0700525 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 capimsg_setu16(skb->data, 16, len); /* Data length */
527 capimsg_setu16(skb->data, 18, datahandle);
528 capimsg_setu16(skb->data, 20, 0); /* Flags */
529
Jan Kiszka501c87a2010-02-08 10:12:16 +0000530 if (capiminor_add_ack(mp, datahandle) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000532
533 spin_lock_bh(&mp->outlock);
534 __skb_queue_head(&mp->outqueue, skb);
535 mp->outbytes += len;
536 spin_unlock_bh(&mp->outlock);
537
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000538 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540 errcode = capi20_put_message(mp->ap, skb);
541 if (errcode == CAPI_NOERROR) {
David S. Miller8fb53b92011-05-19 18:20:29 -0400542 pr_debug("capi: DATA_B3_REQ %u len=%u\n",
543 datahandle, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 continue;
545 }
546 capiminor_del_ack(mp, datahandle);
547
548 if (errcode == CAPI_SENDQUEUEFULL) {
549 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000550
551 spin_lock_bh(&mp->outlock);
552 __skb_queue_head(&mp->outqueue, skb);
553 mp->outbytes += len;
554 spin_unlock_bh(&mp->outlock);
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 break;
557 }
558
559 /* ups, drop packet */
560 printk(KERN_ERR "capi: put_message = %x\n", errcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 kfree_skb(skb);
562 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000563 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
566#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
567/* -------- function called by lower level -------------------------- */
568
569static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
570{
571 struct capidev *cdev = ap->private;
572#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
573 struct capiminor *mp;
574 u16 datahandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct capincci *np;
Tilman Schmidt7dc2ce52012-04-25 13:02:20 +0000576#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Jan Kiszka05b41492010-02-08 10:12:19 +0000578 mutex_lock(&cdev->lock);
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
581 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
Jan Kiszka05b41492010-02-08 10:12:19 +0000582 if ((info & 0xff00) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000585 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
589 skb_queue_tail(&cdev->recvqueue, skb);
590 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000591 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000593
Tilman Schmidt7dc2ce52012-04-25 13:02:20 +0000594#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
595 skb_queue_tail(&cdev->recvqueue, skb);
596 wake_up_interruptible(&cdev->recvwait);
597
598#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
599
Jan Kiszka05b41492010-02-08 10:12:19 +0000600 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (!np) {
602 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
603 skb_queue_tail(&cdev->recvqueue, skb);
604 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000605 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 mp = np->minorp;
609 if (!mp) {
610 skb_queue_tail(&cdev->recvqueue, skb);
611 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000612 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
Joe Perches475be4d2012-02-19 19:52:38 -0800615 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
David S. Miller8fb53b92011-05-19 18:20:29 -0400616 pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
617 datahandle, skb->len-CAPIMSG_LEN(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 skb_queue_tail(&mp->inqueue, skb);
Jan Kiszka68d73472010-02-08 10:12:39 +0000619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 handle_minor_recv(mp);
621
622 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
623
Joe Perches475be4d2012-02-19 19:52:38 -0800624 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
David S. Miller8fb53b92011-05-19 18:20:29 -0400625 pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
626 datahandle,
Joe Perches475be4d2012-02-19 19:52:38 -0800627 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 kfree_skb(skb);
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000629 capiminor_del_ack(mp, datahandle);
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100630 tty_port_tty_wakeup(&mp->port);
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000631 handle_minor_send(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 } else {
634 /* ups, let capi application handle it :-) */
635 skb_queue_tail(&cdev->recvqueue, skb);
636 wake_up_interruptible(&cdev->recvwait);
637 }
638#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000639
Jan Kiszka05b41492010-02-08 10:12:19 +0000640unlock_out:
Jan Kiszka05b41492010-02-08 10:12:19 +0000641 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644/* -------- file_operations for capidev ----------------------------- */
645
646static ssize_t
647capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
648{
Joe Perches54cbb1c2010-07-12 10:50:02 +0000649 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct sk_buff *skb;
651 size_t copied;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000652 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 if (!cdev->ap.applid)
655 return -ENODEV;
656
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000657 skb = skb_dequeue(&cdev->recvqueue);
658 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (file->f_flags & O_NONBLOCK)
660 return -EAGAIN;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000661 err = wait_event_interruptible(cdev->recvwait,
Joe Perches475be4d2012-02-19 19:52:38 -0800662 (skb = skb_dequeue(&cdev->recvqueue)));
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000663 if (err)
664 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666 if (skb->len > count) {
667 skb_queue_head(&cdev->recvqueue, skb);
668 return -EMSGSIZE;
669 }
670 if (copy_to_user(buf, skb->data, skb->len)) {
671 skb_queue_head(&cdev->recvqueue, skb);
672 return -EFAULT;
673 }
674 copied = skb->len;
675
676 kfree_skb(skb);
677
678 return copied;
679}
680
681static ssize_t
682capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
683{
Joe Perches54cbb1c2010-07-12 10:50:02 +0000684 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 struct sk_buff *skb;
686 u16 mlen;
687
688 if (!cdev->ap.applid)
689 return -ENODEV;
690
691 skb = alloc_skb(count, GFP_USER);
692 if (!skb)
693 return -ENOMEM;
694
695 if (copy_from_user(skb_put(skb, count), buf, count)) {
696 kfree_skb(skb);
697 return -EFAULT;
698 }
699 mlen = CAPIMSG_LEN(skb->data);
700 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
701 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
702 kfree_skb(skb);
703 return -EINVAL;
704 }
705 } else {
706 if (mlen != count) {
707 kfree_skb(skb);
708 return -EINVAL;
709 }
710 }
711 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
712
713 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Jan Kiszka05b41492010-02-08 10:12:19 +0000714 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000716 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718
719 cdev->errcode = capi20_put_message(&cdev->ap, skb);
720
721 if (cdev->errcode) {
722 kfree_skb(skb);
723 return -EIO;
724 }
725 return count;
726}
727
Al Viroafc9a422017-07-03 06:39:46 -0400728static __poll_t
Joe Perches475be4d2012-02-19 19:52:38 -0800729capi_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Joe Perches54cbb1c2010-07-12 10:50:02 +0000731 struct capidev *cdev = file->private_data;
Al Viroafc9a422017-07-03 06:39:46 -0400732 __poll_t mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 if (!cdev->ap.applid)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800735 return EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 poll_wait(file, &(cdev->recvwait), wait);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800738 mask = EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (!skb_queue_empty(&cdev->recvqueue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800740 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return mask;
742}
743
744static int
Arnd Bergmann703c6312010-04-27 00:24:02 +0200745capi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 capi_ioctl_struct data;
749 int retval = -EINVAL;
750 void __user *argp = (void __user *)arg;
751
752 switch (cmd) {
753 case CAPI_REGISTER:
Jan Kiszka05b41492010-02-08 10:12:19 +0000754 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Jan Kiszka05b41492010-02-08 10:12:19 +0000756 if (cdev->ap.applid) {
757 retval = -EEXIST;
758 goto register_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000760 if (copy_from_user(&cdev->ap.rparam, argp,
761 sizeof(struct capi_register_params))) {
762 retval = -EFAULT;
763 goto register_out;
764 }
765 cdev->ap.private = cdev;
766 cdev->ap.recv_message = capi_recv_message;
767 cdev->errcode = capi20_register(&cdev->ap);
768 retval = (int)cdev->ap.applid;
769 if (cdev->errcode) {
770 cdev->ap.applid = 0;
771 retval = -EIO;
772 }
773
774register_out:
775 mutex_unlock(&cdev->lock);
776 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 case CAPI_GET_VERSION:
Joe Perches475be4d2012-02-19 19:52:38 -0800779 if (copy_from_user(&data.contr, argp,
780 sizeof(data.contr)))
781 return -EFAULT;
782 cdev->errcode = capi20_get_version(data.contr, &data.version);
783 if (cdev->errcode)
784 return -EIO;
785 if (copy_to_user(argp, &data.version,
786 sizeof(data.version)))
787 return -EFAULT;
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000788 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 case CAPI_GET_SERIAL:
Joe Perches475be4d2012-02-19 19:52:38 -0800791 if (copy_from_user(&data.contr, argp,
792 sizeof(data.contr)))
793 return -EFAULT;
794 cdev->errcode = capi20_get_serial(data.contr, data.serial);
795 if (cdev->errcode)
796 return -EIO;
797 if (copy_to_user(argp, data.serial,
798 sizeof(data.serial)))
799 return -EFAULT;
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000800 return 0;
801
Joe Perches475be4d2012-02-19 19:52:38 -0800802 case CAPI_GET_PROFILE:
Joe Perches475be4d2012-02-19 19:52:38 -0800803 if (copy_from_user(&data.contr, argp,
804 sizeof(data.contr)))
805 return -EFAULT;
806
807 if (data.contr == 0) {
808 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (cdev->errcode)
810 return -EIO;
Joe Perches475be4d2012-02-19 19:52:38 -0800811
812 retval = copy_to_user(argp,
813 &data.profile.ncontroller,
814 sizeof(data.profile.ncontroller));
815
816 } else {
817 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
818 if (cdev->errcode)
819 return -EIO;
820
821 retval = copy_to_user(argp, &data.profile,
822 sizeof(data.profile));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Joe Perches475be4d2012-02-19 19:52:38 -0800824 if (retval)
825 return -EFAULT;
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000826 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 case CAPI_GET_MANUFACTURER:
Joe Perches475be4d2012-02-19 19:52:38 -0800829 if (copy_from_user(&data.contr, argp,
830 sizeof(data.contr)))
831 return -EFAULT;
832 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
833 if (cdev->errcode)
834 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Joe Perches475be4d2012-02-19 19:52:38 -0800836 if (copy_to_user(argp, data.manufacturer,
837 sizeof(data.manufacturer)))
838 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000840 return 0;
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 case CAPI_GET_ERRCODE:
843 data.errcode = cdev->errcode;
844 cdev->errcode = CAPI_NOERROR;
845 if (arg) {
846 if (copy_to_user(argp, &data.errcode,
847 sizeof(data.errcode)))
848 return -EFAULT;
849 }
850 return data.errcode;
851
852 case CAPI_INSTALLED:
853 if (capi20_isinstalled() == CAPI_NOERROR)
854 return 0;
855 return -ENXIO;
856
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000857 case CAPI_MANUFACTURER_CMD: {
Joe Perches475be4d2012-02-19 19:52:38 -0800858 struct capi_manufacturer_cmd mcmd;
859 if (!capable(CAP_SYS_ADMIN))
860 return -EPERM;
861 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
862 return -EFAULT;
863 return capi20_manufacturer(mcmd.cmd, mcmd.data);
864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 case CAPI_SET_FLAGS:
Jan Kiszka05b41492010-02-08 10:12:19 +0000866 case CAPI_CLR_FLAGS: {
867 unsigned userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Jan Kiszka05b41492010-02-08 10:12:19 +0000869 if (copy_from_user(&userflags, argp, sizeof(userflags)))
870 return -EFAULT;
871
872 mutex_lock(&cdev->lock);
873 if (cmd == CAPI_SET_FLAGS)
874 cdev->userflags |= userflags;
875 else
876 cdev->userflags &= ~userflags;
877 mutex_unlock(&cdev->lock);
878 return 0;
879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 case CAPI_GET_FLAGS:
881 if (copy_to_user(argp, &cdev->userflags,
882 sizeof(cdev->userflags)))
883 return -EFAULT;
884 return 0;
885
Tilman Schmidt7dc2ce52012-04-25 13:02:20 +0000886#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
887 case CAPI_NCCI_OPENCOUNT:
888 return 0;
889
890#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka05b41492010-02-08 10:12:19 +0000891 case CAPI_NCCI_OPENCOUNT: {
892 struct capincci *nccip;
893 unsigned ncci;
894 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Jan Kiszka05b41492010-02-08 10:12:19 +0000896 if (copy_from_user(&ncci, argp, sizeof(ncci)))
897 return -EFAULT;
898
899 mutex_lock(&cdev->lock);
900 nccip = capincci_find(cdev, (u32)ncci);
901 if (nccip)
902 count = capincci_minor_opencount(nccip);
903 mutex_unlock(&cdev->lock);
904 return count;
905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Jan Kiszka05b41492010-02-08 10:12:19 +0000907 case CAPI_NCCI_GETUNIT: {
908 struct capincci *nccip;
909 struct capiminor *mp;
910 unsigned ncci;
911 int unit = -ESRCH;
912
913 if (copy_from_user(&ncci, argp, sizeof(ncci)))
914 return -EFAULT;
915
916 mutex_lock(&cdev->lock);
917 nccip = capincci_find(cdev, (u32)ncci);
918 if (nccip) {
919 mp = nccip->minorp;
920 if (mp)
921 unit = mp->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000923 mutex_unlock(&cdev->lock);
924 return unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000926#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
927
928 default:
929 return -EINVAL;
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Arnd Bergmann703c6312010-04-27 00:24:02 +0200933static long
934capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
935{
936 int ret;
937
Arnd Bergmann76a64922010-07-11 11:18:53 +0000938 mutex_lock(&capi_mutex);
Arnd Bergmann703c6312010-04-27 00:24:02 +0200939 ret = capi_ioctl(file, cmd, arg);
Arnd Bergmann76a64922010-07-11 11:18:53 +0000940 mutex_unlock(&capi_mutex);
Arnd Bergmann703c6312010-04-27 00:24:02 +0200941
942 return ret;
943}
944
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000945static int capi_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000947 struct capidev *cdev;
948
949 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
950 if (!cdev)
951 return -ENOMEM;
952
Jan Kiszka05b41492010-02-08 10:12:19 +0000953 mutex_init(&cdev->lock);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000954 skb_queue_head_init(&cdev->recvqueue);
955 init_waitqueue_head(&cdev->recvwait);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000956 INIT_LIST_HEAD(&cdev->nccis);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000957 file->private_data = cdev;
958
959 mutex_lock(&capidev_list_lock);
960 list_add_tail(&cdev->list, &capidev_list);
961 mutex_unlock(&capidev_list_lock);
962
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300963 return stream_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000966static int capi_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000968 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000970 mutex_lock(&capidev_list_lock);
971 list_del(&cdev->list);
972 mutex_unlock(&capidev_list_lock);
973
Jan Kiszka05b41492010-02-08 10:12:19 +0000974 if (cdev->ap.applid)
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000975 capi20_release(&cdev->ap);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000976 skb_queue_purge(&cdev->recvqueue);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000977 capincci_free(cdev, 0xffffffff);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000978
979 kfree(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return 0;
981}
982
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800983static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 .owner = THIS_MODULE,
986 .llseek = no_llseek,
987 .read = capi_read,
988 .write = capi_write,
989 .poll = capi_poll,
Arnd Bergmann703c6312010-04-27 00:24:02 +0200990 .unlocked_ioctl = capi_unlocked_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 .open = capi_open,
992 .release = capi_release,
993};
994
995#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
996/* -------- tty_operations for capincci ----------------------------- */
997
Jan Kiszka46324512010-02-08 10:12:28 +0000998static int
999capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1000{
Jiri Slaby410235f2012-03-05 14:52:01 +01001001 struct capiminor *mp = capiminor_get(tty->index);
Jiri Slaby81f58352012-01-30 21:14:30 +01001002 int ret = tty_standard_install(driver, tty);
Jan Kiszka46324512010-02-08 10:12:28 +00001003
Jiri Slaby81f58352012-01-30 21:14:30 +01001004 if (ret == 0)
Jan Kiszka46324512010-02-08 10:12:28 +00001005 tty->driver_data = mp;
Jiri Slaby81f58352012-01-30 21:14:30 +01001006 else
Jan Kiszka46324512010-02-08 10:12:28 +00001007 capiminor_put(mp);
1008 return ret;
1009}
1010
1011static void capinc_tty_cleanup(struct tty_struct *tty)
1012{
1013 struct capiminor *mp = tty->driver_data;
1014 tty->driver_data = NULL;
1015 capiminor_put(mp);
1016}
1017
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001018static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Jan Kiszka46324512010-02-08 10:12:28 +00001020 struct capiminor *mp = tty->driver_data;
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001021 int err;
1022
1023 err = tty_port_open(&mp->port, tty, filp);
1024 if (err)
1025 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 handle_minor_recv(mp);
1028 return 0;
1029}
1030
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001031static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Jan Kiszka46324512010-02-08 10:12:28 +00001033 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001035 tty_port_close(&mp->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Jan Kiszka6576c282010-02-08 10:12:32 +00001038static int capinc_tty_write(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 const unsigned char *buf, int count)
1040{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001041 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 struct sk_buff *skb;
1043
David S. Miller8fb53b92011-05-19 18:20:29 -04001044 pr_debug("capinc_tty_write(count=%d)\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001046 spin_lock_bh(&mp->outlock);
1047 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (skb) {
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001049 mp->outskb = NULL;
1050 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 mp->outbytes += skb->len;
1052 }
1053
Joe Perches475be4d2012-02-19 19:52:38 -08001054 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + count, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (!skb) {
1056 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001057 spin_unlock_bh(&mp->outlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return -ENOMEM;
1059 }
1060
1061 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
Johannes Berg59ae1d12017-06-16 14:29:20 +02001062 skb_put_data(skb, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001064 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001066 spin_unlock_bh(&mp->outlock);
1067
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001068 handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return count;
1071}
1072
Alan Coxf2545a72008-04-30 00:54:09 -07001073static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001075 struct capiminor *mp = tty->driver_data;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001076 bool invoke_send = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 struct sk_buff *skb;
Alan Coxf2545a72008-04-30 00:54:09 -07001078 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
David S. Miller8fb53b92011-05-19 18:20:29 -04001080 pr_debug("capinc_put_char(%u)\n", ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001082 spin_lock_bh(&mp->outlock);
1083 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (skb) {
1085 if (skb_tailroom(skb) > 0) {
Johannes Berg634fef62017-06-16 14:29:24 +02001086 skb_put_u8(skb, ch);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001087 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001089 mp->outskb = NULL;
1090 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001092 invoke_send = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001094
Joe Perches475be4d2012-02-19 19:52:38 -08001095 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + CAPI_MAX_BLKSIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (skb) {
1097 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
Johannes Berg634fef62017-06-16 14:29:24 +02001098 skb_put_u8(skb, ch);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001099 mp->outskb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 } else {
1101 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
Alan Coxf2545a72008-04-30 00:54:09 -07001102 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001104
1105unlock_out:
1106 spin_unlock_bh(&mp->outlock);
1107
1108 if (invoke_send)
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001109 handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001110
Alan Coxf2545a72008-04-30 00:54:09 -07001111 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
1114static void capinc_tty_flush_chars(struct tty_struct *tty)
1115{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001116 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 struct sk_buff *skb;
1118
David S. Miller8fb53b92011-05-19 18:20:29 -04001119 pr_debug("capinc_tty_flush_chars\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001121 spin_lock_bh(&mp->outlock);
1122 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (skb) {
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001124 mp->outskb = NULL;
1125 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001127 spin_unlock_bh(&mp->outlock);
1128
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001129 handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001130 } else
1131 spin_unlock_bh(&mp->outlock);
1132
1133 handle_minor_recv(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
1136static int capinc_tty_write_room(struct tty_struct *tty)
1137{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001138 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 int room;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1142 room *= CAPI_MAX_BLKSIZE;
David S. Miller8fb53b92011-05-19 18:20:29 -04001143 pr_debug("capinc_tty_write_room = %d\n", room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return room;
1145}
1146
Adrian Bunk408b6642005-05-01 08:59:29 -07001147static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001149 struct capiminor *mp = tty->driver_data;
1150
David S. Miller8fb53b92011-05-19 18:20:29 -04001151 pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1152 mp->outbytes, mp->nack,
1153 skb_queue_len(&mp->outqueue),
1154 skb_queue_len(&mp->inqueue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return mp->outbytes;
1156}
1157
Joe Perches475be4d2012-02-19 19:52:38 -08001158static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
David S. Miller8fb53b92011-05-19 18:20:29 -04001160 pr_debug("capinc_tty_set_termios\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Jan Kiszka2c8df722010-02-08 10:12:30 +00001163static void capinc_tty_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001165 struct capiminor *mp = tty->driver_data;
David S. Miller8fb53b92011-05-19 18:20:29 -04001166 pr_debug("capinc_tty_throttle\n");
Jan Kiszka2c8df722010-02-08 10:12:30 +00001167 mp->ttyinstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
Jan Kiszka2c8df722010-02-08 10:12:30 +00001170static void capinc_tty_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001172 struct capiminor *mp = tty->driver_data;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001173
David S. Miller8fb53b92011-05-19 18:20:29 -04001174 pr_debug("capinc_tty_unthrottle\n");
Jan Kiszka2c8df722010-02-08 10:12:30 +00001175 mp->ttyinstop = 0;
1176 handle_minor_recv(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
1179static void capinc_tty_stop(struct tty_struct *tty)
1180{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001181 struct capiminor *mp = tty->driver_data;
1182
David S. Miller8fb53b92011-05-19 18:20:29 -04001183 pr_debug("capinc_tty_stop\n");
Jan Kiszka2c8df722010-02-08 10:12:30 +00001184 mp->ttyoutstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
1187static void capinc_tty_start(struct tty_struct *tty)
1188{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001189 struct capiminor *mp = tty->driver_data;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001190
David S. Miller8fb53b92011-05-19 18:20:29 -04001191 pr_debug("capinc_tty_start\n");
Jan Kiszka2c8df722010-02-08 10:12:30 +00001192 mp->ttyoutstop = 0;
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001193 handle_minor_send(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
1196static void capinc_tty_hangup(struct tty_struct *tty)
1197{
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001198 struct capiminor *mp = tty->driver_data;
1199
David S. Miller8fb53b92011-05-19 18:20:29 -04001200 pr_debug("capinc_tty_hangup\n");
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001201 tty_port_hangup(&mp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
Alan Cox9e98966c2008-07-22 11:18:03 +01001204static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
David S. Miller8fb53b92011-05-19 18:20:29 -04001206 pr_debug("capinc_tty_break_ctl(%d)\n", state);
Alan Cox9e98966c2008-07-22 11:18:03 +01001207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210static void capinc_tty_flush_buffer(struct tty_struct *tty)
1211{
David S. Miller8fb53b92011-05-19 18:20:29 -04001212 pr_debug("capinc_tty_flush_buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
1215static void capinc_tty_set_ldisc(struct tty_struct *tty)
1216{
David S. Miller8fb53b92011-05-19 18:20:29 -04001217 pr_debug("capinc_tty_set_ldisc\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
1220static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1221{
David S. Miller8fb53b92011-05-19 18:20:29 -04001222 pr_debug("capinc_tty_send_xchar(%d)\n", ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001225static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 .open = capinc_tty_open,
1227 .close = capinc_tty_close,
1228 .write = capinc_tty_write,
1229 .put_char = capinc_tty_put_char,
1230 .flush_chars = capinc_tty_flush_chars,
1231 .write_room = capinc_tty_write_room,
1232 .chars_in_buffer = capinc_tty_chars_in_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 .set_termios = capinc_tty_set_termios,
1234 .throttle = capinc_tty_throttle,
1235 .unthrottle = capinc_tty_unthrottle,
1236 .stop = capinc_tty_stop,
1237 .start = capinc_tty_start,
1238 .hangup = capinc_tty_hangup,
1239 .break_ctl = capinc_tty_break_ctl,
1240 .flush_buffer = capinc_tty_flush_buffer,
1241 .set_ldisc = capinc_tty_set_ldisc,
1242 .send_xchar = capinc_tty_send_xchar,
Jan Kiszka46324512010-02-08 10:12:28 +00001243 .install = capinc_tty_install,
1244 .cleanup = capinc_tty_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245};
1246
Jan Kiszkae76b1542010-02-08 10:12:24 +00001247static int __init capinc_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
1249 struct tty_driver *drv;
Jan Kiszkae76b1542010-02-08 10:12:24 +00001250 int err;
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (capi_ttyminors > CAPINC_MAX_PORTS)
1253 capi_ttyminors = CAPINC_MAX_PORTS;
1254 if (capi_ttyminors <= 0)
1255 capi_ttyminors = CAPINC_NR_PORTS;
1256
Kees Cook6396bb22018-06-12 14:03:40 -07001257 capiminors = kcalloc(capi_ttyminors, sizeof(struct capiminor *),
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001258 GFP_KERNEL);
1259 if (!capiminors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return -ENOMEM;
1261
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001262 drv = alloc_tty_driver(capi_ttyminors);
1263 if (!drv) {
1264 kfree(capiminors);
1265 return -ENOMEM;
1266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 drv->driver_name = "capi_nc";
Paul Bolled1cadce2014-06-01 23:47:24 +02001268 drv->name = "capi!";
Jan Kiszkae95ac142010-02-08 10:12:26 +00001269 drv->major = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 drv->minor_start = 0;
1271 drv->type = TTY_DRIVER_TYPE_SERIAL;
1272 drv->subtype = SERIAL_TYPE_NORMAL;
1273 drv->init_termios = tty_std_termios;
1274 drv->init_termios.c_iflag = ICRNL;
1275 drv->init_termios.c_oflag = OPOST | ONLCR;
1276 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1277 drv->init_termios.c_lflag = 0;
Jan Kiszka40fb2d02010-02-08 10:12:25 +00001278 drv->flags =
1279 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1280 TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 tty_set_operations(drv, &capinc_ops);
Jan Kiszkae76b1542010-02-08 10:12:24 +00001282
1283 err = tty_register_driver(drv);
1284 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 put_tty_driver(drv);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001286 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 printk(KERN_ERR "Couldn't register capi_nc driver\n");
Jan Kiszkae76b1542010-02-08 10:12:24 +00001288 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290 capinc_tty_driver = drv;
1291 return 0;
1292}
1293
Jan Kiszkae76b1542010-02-08 10:12:24 +00001294static void __exit capinc_tty_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Jan Kiszkae76b1542010-02-08 10:12:24 +00001296 tty_unregister_driver(capinc_tty_driver);
1297 put_tty_driver(capinc_tty_driver);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001298 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
Jan Kiszka501c87a2010-02-08 10:12:16 +00001301#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1302
1303static inline int capinc_tty_init(void)
1304{
1305 return 0;
1306}
1307
1308static inline void capinc_tty_exit(void) { }
1309
1310#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312/* -------- /proc functions ----------------------------------------- */
1313
1314/*
1315 * /proc/capi/capi20:
1316 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1317 */
Randy Dunlap20fbdc32018-07-07 08:31:15 -07001318static int __maybe_unused capi20_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Joe Perches475be4d2012-02-19 19:52:38 -08001320 struct capidev *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001323 mutex_lock(&capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 list_for_each(l, &capidev_list) {
1325 cdev = list_entry(l, struct capidev, list);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001326 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001327 cdev->ap.applid,
1328 cdev->ap.nrecvctlpkt,
1329 cdev->ap.nrecvdatapkt,
1330 cdev->ap.nsentctlpkt,
1331 cdev->ap.nsentdatapkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001333 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
1337/*
1338 * /proc/capi/capi20ncci:
1339 * applid ncci
1340 */
Randy Dunlap20fbdc32018-07-07 08:31:15 -07001341static int __maybe_unused capi20ncci_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Jan Kiszka884f5c42010-02-08 10:12:22 +00001343 struct capidev *cdev;
1344 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001346 mutex_lock(&capidev_list_lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001347 list_for_each_entry(cdev, &capidev_list, list) {
Jan Kiszka05b41492010-02-08 10:12:19 +00001348 mutex_lock(&cdev->lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001349 list_for_each_entry(np, &cdev->nccis, list)
1350 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
Jan Kiszka05b41492010-02-08 10:12:19 +00001351 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001353 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357static void __init proc_init(void)
1358{
Christoph Hellwig3f3942a2018-05-15 15:57:23 +02001359 proc_create_single("capi/capi20", 0, NULL, capi20_proc_show);
1360 proc_create_single("capi/capi20ncci", 0, NULL, capi20ncci_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
1363static void __exit proc_exit(void)
1364{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001365 remove_proc_entry("capi/capi20", NULL);
1366 remove_proc_entry("capi/capi20ncci", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
1369/* -------- init function and module interface ---------------------- */
1370
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372static int __init capi_init(void)
1373{
Jan Kiszka88549d62010-02-08 10:12:09 +00001374 const char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001375 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Andrew Morton6d9eac32006-03-28 01:56:19 -08001377 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1378 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001380 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001382 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (IS_ERR(capi_class)) {
1384 unregister_chrdev(capi_major, "capi20");
1385 return PTR_ERR(capi_class);
1386 }
1387
Paul Bolled1cadce2014-06-01 23:47:24 +02001388 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (capinc_tty_init() < 0) {
Tony Jonesd78b0362007-09-25 02:03:03 +02001391 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001392 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 unregister_chrdev(capi_major, "capi20");
1394 return -ENOMEM;
1395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 proc_init();
1398
Jan Kiszka1f90d662011-04-06 10:58:37 +00001399#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Joe Perches475be4d2012-02-19 19:52:38 -08001400 compileinfo = " (middleware)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401#else
Joe Perches475be4d2012-02-19 19:52:38 -08001402 compileinfo = " (no middleware)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403#endif
Jan Kiszka88549d62010-02-08 10:12:09 +00001404 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1405 capi_major, compileinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 return 0;
1408}
1409
1410static void __exit capi_exit(void)
1411{
1412 proc_exit();
1413
Tony Jonesd78b0362007-09-25 02:03:03 +02001414 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001415 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 capinc_tty_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
1420
1421module_init(capi_init);
1422module_exit(capi_exit);