blob: 6a2df3297e77c92e4ca47d460ad41c9363425842 [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 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 unsigned int minor;
81
Jan Kiszka42792712010-02-08 10:12:38 +000082 struct capi20_appl *ap;
83 u32 ncci;
84 atomic_t datahandle;
85 atomic_t msgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Jan Kiszkafb4b4882010-02-08 10:12:29 +000087 struct tty_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 int ttyinstop;
89 int ttyoutstop;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Jan Kiszkadfbb84f2010-02-08 10:12:40 +000091 struct sk_buff_head inqueue;
92
93 struct sk_buff_head outqueue;
94 int outbytes;
95 struct sk_buff *outskb;
96 spinlock_t outlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 /* transmit path */
Michael Buesch6aa65472006-06-26 00:25:30 -070099 struct list_head ackqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 int nack;
Michael Buesch6aa65472006-06-26 00:25:30 -0700101 spinlock_t ackqlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104struct capincci {
Jan Kiszka884f5c42010-02-08 10:12:22 +0000105 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 u32 ncci;
107 struct capidev *cdev;
108#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
109 struct capiminor *minorp;
110#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
111};
112
113struct capidev {
114 struct list_head list;
115 struct capi20_appl ap;
116 u16 errcode;
117 unsigned userflags;
118
119 struct sk_buff_head recvqueue;
120 wait_queue_head_t recvwait;
121
Jan Kiszka884f5c42010-02-08 10:12:22 +0000122 struct list_head nccis;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Jan Kiszka05b41492010-02-08 10:12:19 +0000124 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
127/* -------- global variables ---------------------------------------- */
128
Jan Kiszkab8f433d2010-02-08 10:12:17 +0000129static DEFINE_MUTEX(capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static LIST_HEAD(capidev_list);
131
132#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +0000133
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000134static DEFINE_SPINLOCK(capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000135static struct capiminor **capiminors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000137static struct tty_driver *capinc_tty_driver;
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/* -------- datahandles --------------------------------------------- */
140
Jan Kiszka501c87a2010-02-08 10:12:16 +0000141static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Jan Kiszka42651b52010-02-08 10:12:37 +0000143 struct ackqueue_entry *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 n = kmalloc(sizeof(*n), GFP_ATOMIC);
Michael Buesch6aa65472006-06-26 00:25:30 -0700146 if (unlikely(!n)) {
147 printk(KERN_ERR "capi: alloc datahandle failed\n");
148 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 n->datahandle = datahandle;
Michael Buesch6aa65472006-06-26 00:25:30 -0700151 INIT_LIST_HEAD(&n->list);
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000152 spin_lock_bh(&mp->ackqlock);
Michael Buesch6aa65472006-06-26 00:25:30 -0700153 list_add_tail(&n->list, &mp->ackqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 mp->nack++;
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000155 spin_unlock_bh(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return 0;
157}
158
159static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
160{
Jan Kiszka42651b52010-02-08 10:12:37 +0000161 struct ackqueue_entry *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000163 spin_lock_bh(&mp->ackqlock);
Michael Buesch6aa65472006-06-26 00:25:30 -0700164 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
Joe Perches475be4d2012-02-19 19:52:38 -0800165 if (p->datahandle == datahandle) {
Michael Buesch6aa65472006-06-26 00:25:30 -0700166 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 mp->nack--;
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000168 spin_unlock_bh(&mp->ackqlock);
169 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return 0;
171 }
172 }
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000173 spin_unlock_bh(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return -1;
175}
176
177static void capiminor_del_all_ack(struct capiminor *mp)
178{
Jan Kiszka42651b52010-02-08 10:12:37 +0000179 struct ackqueue_entry *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Michael Buesch6aa65472006-06-26 00:25:30 -0700181 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
182 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 kfree(p);
184 mp->nack--;
185 }
186}
187
188
189/* -------- struct capiminor ---------------------------------------- */
190
Jiri Slaby55bef832012-11-15 09:49:50 +0100191static void capiminor_destroy(struct tty_port *port)
192{
193 struct capiminor *mp = container_of(port, struct capiminor, port);
194
195 kfree_skb(mp->outskb);
196 skb_queue_purge(&mp->inqueue);
197 skb_queue_purge(&mp->outqueue);
198 capiminor_del_all_ack(mp);
199 kfree(mp);
200}
201
202static const struct tty_port_operations capiminor_port_ops = {
203 .destruct = capiminor_destroy,
204};
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
207{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000208 struct capiminor *mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000209 struct device *dev;
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000210 unsigned int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000212 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
Joe Perches475be4d2012-02-19 19:52:38 -0800213 if (!mp) {
214 printk(KERN_ERR "capi: can't alloc capiminor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return NULL;
216 }
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 mp->ap = ap;
219 mp->ncci = ncci;
Michael Buesch6aa65472006-06-26 00:25:30 -0700220 INIT_LIST_HEAD(&mp->ackqueue);
221 spin_lock_init(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 skb_queue_head_init(&mp->inqueue);
224 skb_queue_head_init(&mp->outqueue);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000225 spin_lock_init(&mp->outlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000227 tty_port_init(&mp->port);
228 mp->port.ops = &capiminor_port_ops;
229
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000230 /* Allocate the least unused minor number. */
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000231 spin_lock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000232 for (minor = 0; minor < capi_ttyminors; minor++)
233 if (!capiminors[minor]) {
234 capiminors[minor] = mp;
235 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000237 spin_unlock(&capiminors_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000239 if (minor == capi_ttyminors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 printk(KERN_NOTICE "capi: out of minors\n");
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000241 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000244 mp->minor = minor;
245
Jiri Slaby734cc172012-08-07 21:47:47 +0200246 dev = tty_port_register_device(&mp->port, capinc_tty_driver, minor,
247 NULL);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000248 if (IS_ERR(dev))
249 goto err_out2;
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000252
253err_out2:
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000254 spin_lock(&capiminors_lock);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000255 capiminors[minor] = NULL;
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000256 spin_unlock(&capiminors_lock);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000257
258err_out1:
Jiri Slaby55bef832012-11-15 09:49:50 +0100259 tty_port_put(&mp->port);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000260 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000263static struct capiminor *capiminor_get(unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000265 struct capiminor *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000267 spin_lock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000268 mp = capiminors[minor];
Jan Kiszka0159d542010-02-08 10:12:27 +0000269 if (mp)
Jiri Slaby55bef832012-11-15 09:49:50 +0100270 tty_port_get(&mp->port);
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000271 spin_unlock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000272
273 return mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Jan Kiszka0159d542010-02-08 10:12:27 +0000276static inline void capiminor_put(struct capiminor *mp)
277{
Jiri Slaby55bef832012-11-15 09:49:50 +0100278 tty_port_put(&mp->port);
Jan Kiszka0159d542010-02-08 10:12:27 +0000279}
280
281static void capiminor_free(struct capiminor *mp)
282{
Jan Kiszka0159d542010-02-08 10:12:27 +0000283 tty_unregister_device(capinc_tty_driver, mp->minor);
284
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000285 spin_lock(&capiminors_lock);
Jan Kiszka0159d542010-02-08 10:12:27 +0000286 capiminors[mp->minor] = NULL;
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000287 spin_unlock(&capiminors_lock);
Jan Kiszka0159d542010-02-08 10:12:27 +0000288
289 capiminor_put(mp);
290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/* -------- struct capincci ----------------------------------------- */
293
Jan Kiszka501c87a2010-02-08 10:12:16 +0000294static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Jan Kiszka1f90d662011-04-06 10:58:37 +0000296 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
297 np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000298}
299
300static void capincci_free_minor(struct capincci *np)
301{
302 struct capiminor *mp = np->minorp;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000303 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000304
305 if (mp) {
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000306 tty = tty_port_tty_get(&mp->port);
307 if (tty) {
Jan Kiszka30bced92010-02-08 10:12:31 +0000308 tty_vhangup(tty);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000309 tty_kref_put(tty);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000310 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000311
312 capiminor_free(mp);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000313 }
314}
315
316static inline unsigned int capincci_minor_opencount(struct capincci *np)
317{
318 struct capiminor *mp = np->minorp;
Jan Kiszkaa84fdf42010-02-08 10:12:34 +0000319 unsigned int count = 0;
320 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000321
Jan Kiszkaa84fdf42010-02-08 10:12:34 +0000322 if (mp) {
323 tty = tty_port_tty_get(&mp->port);
324 if (tty) {
325 count = tty->count;
326 tty_kref_put(tty);
327 }
328 }
329 return count;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000330}
331
332#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
333
334static inline void
335capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
336static inline void capincci_free_minor(struct capincci *np) { }
337
Jan Kiszka501c87a2010-02-08 10:12:16 +0000338#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
339
340static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
341{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000342 struct capincci *np;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000343
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000344 np = kzalloc(sizeof(*np), GFP_KERNEL);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000345 if (!np)
346 return NULL;
347 np->ncci = ncci;
348 np->cdev = cdev;
349
350 capincci_alloc_minor(cdev, np);
351
Jan Kiszka884f5c42010-02-08 10:12:22 +0000352 list_add_tail(&np->list, &cdev->nccis);
353
354 return np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357static void capincci_free(struct capidev *cdev, u32 ncci)
358{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000359 struct capincci *np, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Jan Kiszka884f5c42010-02-08 10:12:22 +0000361 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (ncci == 0xffffffff || np->ncci == ncci) {
Jan Kiszka501c87a2010-02-08 10:12:16 +0000363 capincci_free_minor(np);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000364 list_del(&np->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 kfree(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Tilman Schmidt7dc2ce52012-04-25 13:02:20 +0000369#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
371{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000372 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Jan Kiszka884f5c42010-02-08 10:12:22 +0000374 list_for_each_entry(np, &cdev->nccis, list)
375 if (np->ncci == ncci)
376 return np;
377 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/* -------- handle data queue --------------------------------------- */
381
382static struct sk_buff *
383gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
384{
385 struct sk_buff *nskb;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000386 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (nskb) {
Joe Perches475be4d2012-02-19 19:52:38 -0800388 u16 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
390 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
391 capimsg_setu16(s, 2, mp->ap->applid);
392 capimsg_setu8 (s, 4, CAPI_DATA_B3);
393 capimsg_setu8 (s, 5, CAPI_RESP);
Jan Kiszka42792712010-02-08 10:12:38 +0000394 capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 capimsg_setu32(s, 8, mp->ncci);
396 capimsg_setu16(s, 12, datahandle);
397 }
398 return nskb;
399}
400
401static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
402{
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000403 unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000404 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 struct sk_buff *nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 u16 errcode, datahandle;
407 struct tty_ldisc *ld;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000408 int ret = -1;
409
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000410 tty = tty_port_tty_get(&mp->port);
411 if (!tty) {
David S. Miller8fb53b92011-05-19 18:20:29 -0400412 pr_debug("capi: currently no receiver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return -1;
414 }
Joe Perches475be4d2012-02-19 19:52:38 -0800415
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000416 ld = tty_ldisc_ref(tty);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000417 if (!ld) {
418 /* fatal error, do not requeue */
419 ret = 0;
420 kfree_skb(skb);
421 goto deref_tty;
422 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000423
Alan Coxa352def2008-07-16 21:53:12 +0100424 if (ld->ops->receive_buf == NULL) {
David S. Miller8fb53b92011-05-19 18:20:29 -0400425 pr_debug("capi: ldisc has no receive_buf function\n");
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000426 /* fatal error, do not requeue */
427 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 if (mp->ttyinstop) {
David S. Miller8fb53b92011-05-19 18:20:29 -0400430 pr_debug("capi: recv tty throttled\n");
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000431 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000433
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000434 if (tty->receive_room < datalen) {
David S. Miller8fb53b92011-05-19 18:20:29 -0400435 pr_debug("capi: no room in tty\n");
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000436 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000438
439 nskb = gen_data_b3_resp_for(mp, skb);
440 if (!nskb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000442 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000444
445 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 errcode = capi20_put_message(mp->ap, nskb);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000448
449 if (errcode == CAPI_NOERROR) {
450 skb_pull(skb, CAPIMSG_LEN(skb->data));
David S. Miller8fb53b92011-05-19 18:20:29 -0400451 pr_debug("capi: DATA_B3_RESP %u len=%d => ldisc\n",
452 datahandle, skb->len);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000453 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
454 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800456 errcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 kfree_skb(nskb);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000458
459 if (errcode == CAPI_SENDQUEUEFULL)
460 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000462
463free_skb:
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000464 ret = 0;
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000465 kfree_skb(skb);
466
467deref_ldisc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 tty_ldisc_deref(ld);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000469
470deref_tty:
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000471 tty_kref_put(tty);
472 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475static void handle_minor_recv(struct capiminor *mp)
476{
477 struct sk_buff *skb;
Jan Kiszka68d73472010-02-08 10:12:39 +0000478
479 while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (handle_recv_skb(mp, skb) < 0) {
481 skb_queue_head(&mp->inqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return;
483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000486static void handle_minor_send(struct capiminor *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000488 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 struct sk_buff *skb;
490 u16 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 u16 errcode;
492 u16 datahandle;
493
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000494 tty = tty_port_tty_get(&mp->port);
495 if (!tty)
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000496 return;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000497
498 if (mp->ttyoutstop) {
David S. Miller8fb53b92011-05-19 18:20:29 -0400499 pr_debug("capi: send: tty stopped\n");
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000500 tty_kref_put(tty);
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000501 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000504 while (1) {
505 spin_lock_bh(&mp->outlock);
506 skb = __skb_dequeue(&mp->outqueue);
507 if (!skb) {
508 spin_unlock_bh(&mp->outlock);
509 break;
510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 len = (u16)skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000512 mp->outbytes -= len;
513 spin_unlock_bh(&mp->outlock);
514
515 datahandle = atomic_inc_return(&mp->datahandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
517 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
518 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
519 capimsg_setu16(skb->data, 2, mp->ap->applid);
520 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
521 capimsg_setu8 (skb->data, 5, CAPI_REQ);
Jan Kiszka42792712010-02-08 10:12:38 +0000522 capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
Andrew Morton5e6c20a2007-07-17 04:04:22 -0700524 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 capimsg_setu16(skb->data, 16, len); /* Data length */
526 capimsg_setu16(skb->data, 18, datahandle);
527 capimsg_setu16(skb->data, 20, 0); /* Flags */
528
Jan Kiszka501c87a2010-02-08 10:12:16 +0000529 if (capiminor_add_ack(mp, datahandle) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000531
532 spin_lock_bh(&mp->outlock);
533 __skb_queue_head(&mp->outqueue, skb);
534 mp->outbytes += len;
535 spin_unlock_bh(&mp->outlock);
536
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000537 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539 errcode = capi20_put_message(mp->ap, skb);
540 if (errcode == CAPI_NOERROR) {
David S. Miller8fb53b92011-05-19 18:20:29 -0400541 pr_debug("capi: DATA_B3_REQ %u len=%u\n",
542 datahandle, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 continue;
544 }
545 capiminor_del_ack(mp, datahandle);
546
547 if (errcode == CAPI_SENDQUEUEFULL) {
548 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000549
550 spin_lock_bh(&mp->outlock);
551 __skb_queue_head(&mp->outqueue, skb);
552 mp->outbytes += len;
553 spin_unlock_bh(&mp->outlock);
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 break;
556 }
557
558 /* ups, drop packet */
559 printk(KERN_ERR "capi: put_message = %x\n", errcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 kfree_skb(skb);
561 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000562 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
566/* -------- function called by lower level -------------------------- */
567
568static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
569{
570 struct capidev *cdev = ap->private;
571#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
572 struct capiminor *mp;
573 u16 datahandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 struct capincci *np;
Tilman Schmidt7dc2ce52012-04-25 13:02:20 +0000575#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Jan Kiszka05b41492010-02-08 10:12:19 +0000577 mutex_lock(&cdev->lock);
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
580 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
Jan Kiszka05b41492010-02-08 10:12:19 +0000581 if ((info & 0xff00) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000584 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
588 skb_queue_tail(&cdev->recvqueue, skb);
589 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000590 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000592
Tilman Schmidt7dc2ce52012-04-25 13:02:20 +0000593#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
594 skb_queue_tail(&cdev->recvqueue, skb);
595 wake_up_interruptible(&cdev->recvwait);
596
597#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
598
Jan Kiszka05b41492010-02-08 10:12:19 +0000599 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (!np) {
601 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
602 skb_queue_tail(&cdev->recvqueue, skb);
603 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000604 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 mp = np->minorp;
608 if (!mp) {
609 skb_queue_tail(&cdev->recvqueue, skb);
610 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000611 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
Joe Perches475be4d2012-02-19 19:52:38 -0800614 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
David S. Miller8fb53b92011-05-19 18:20:29 -0400615 pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
616 datahandle, skb->len-CAPIMSG_LEN(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 skb_queue_tail(&mp->inqueue, skb);
Jan Kiszka68d73472010-02-08 10:12:39 +0000618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 handle_minor_recv(mp);
620
621 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
622
Joe Perches475be4d2012-02-19 19:52:38 -0800623 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
David S. Miller8fb53b92011-05-19 18:20:29 -0400624 pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
625 datahandle,
Joe Perches475be4d2012-02-19 19:52:38 -0800626 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 kfree_skb(skb);
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000628 capiminor_del_ack(mp, datahandle);
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100629 tty_port_tty_wakeup(&mp->port);
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000630 handle_minor_send(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 } else {
633 /* ups, let capi application handle it :-) */
634 skb_queue_tail(&cdev->recvqueue, skb);
635 wake_up_interruptible(&cdev->recvwait);
636 }
637#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000638
Jan Kiszka05b41492010-02-08 10:12:19 +0000639unlock_out:
Jan Kiszka05b41492010-02-08 10:12:19 +0000640 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643/* -------- file_operations for capidev ----------------------------- */
644
645static ssize_t
646capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
647{
Joe Perches54cbb1c2010-07-12 10:50:02 +0000648 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct sk_buff *skb;
650 size_t copied;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000651 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 if (!cdev->ap.applid)
654 return -ENODEV;
655
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000656 skb = skb_dequeue(&cdev->recvqueue);
657 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (file->f_flags & O_NONBLOCK)
659 return -EAGAIN;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000660 err = wait_event_interruptible(cdev->recvwait,
Joe Perches475be4d2012-02-19 19:52:38 -0800661 (skb = skb_dequeue(&cdev->recvqueue)));
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000662 if (err)
663 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665 if (skb->len > count) {
666 skb_queue_head(&cdev->recvqueue, skb);
667 return -EMSGSIZE;
668 }
669 if (copy_to_user(buf, skb->data, skb->len)) {
670 skb_queue_head(&cdev->recvqueue, skb);
671 return -EFAULT;
672 }
673 copied = skb->len;
674
675 kfree_skb(skb);
676
677 return copied;
678}
679
680static ssize_t
681capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
682{
Joe Perches54cbb1c2010-07-12 10:50:02 +0000683 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct sk_buff *skb;
685 u16 mlen;
686
687 if (!cdev->ap.applid)
688 return -ENODEV;
689
690 skb = alloc_skb(count, GFP_USER);
691 if (!skb)
692 return -ENOMEM;
693
694 if (copy_from_user(skb_put(skb, count), buf, count)) {
695 kfree_skb(skb);
696 return -EFAULT;
697 }
698 mlen = CAPIMSG_LEN(skb->data);
699 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
700 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
701 kfree_skb(skb);
702 return -EINVAL;
703 }
704 } else {
705 if (mlen != count) {
706 kfree_skb(skb);
707 return -EINVAL;
708 }
709 }
710 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
711
712 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Jan Kiszka05b41492010-02-08 10:12:19 +0000713 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000715 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717
718 cdev->errcode = capi20_put_message(&cdev->ap, skb);
719
720 if (cdev->errcode) {
721 kfree_skb(skb);
722 return -EIO;
723 }
724 return count;
725}
726
727static unsigned int
Joe Perches475be4d2012-02-19 19:52:38 -0800728capi_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Joe Perches54cbb1c2010-07-12 10:50:02 +0000730 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 unsigned int mask = 0;
732
733 if (!cdev->ap.applid)
734 return POLLERR;
735
736 poll_wait(file, &(cdev->recvwait), wait);
737 mask = POLLOUT | POLLWRNORM;
738 if (!skb_queue_empty(&cdev->recvqueue))
739 mask |= POLLIN | POLLRDNORM;
740 return mask;
741}
742
743static int
Arnd Bergmann703c6312010-04-27 00:24:02 +0200744capi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 capi_ioctl_struct data;
748 int retval = -EINVAL;
749 void __user *argp = (void __user *)arg;
750
751 switch (cmd) {
752 case CAPI_REGISTER:
Jan Kiszka05b41492010-02-08 10:12:19 +0000753 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Jan Kiszka05b41492010-02-08 10:12:19 +0000755 if (cdev->ap.applid) {
756 retval = -EEXIST;
757 goto register_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000759 if (copy_from_user(&cdev->ap.rparam, argp,
760 sizeof(struct capi_register_params))) {
761 retval = -EFAULT;
762 goto register_out;
763 }
764 cdev->ap.private = cdev;
765 cdev->ap.recv_message = capi_recv_message;
766 cdev->errcode = capi20_register(&cdev->ap);
767 retval = (int)cdev->ap.applid;
768 if (cdev->errcode) {
769 cdev->ap.applid = 0;
770 retval = -EIO;
771 }
772
773register_out:
774 mutex_unlock(&cdev->lock);
775 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 case CAPI_GET_VERSION:
Joe Perches475be4d2012-02-19 19:52:38 -0800778 if (copy_from_user(&data.contr, argp,
779 sizeof(data.contr)))
780 return -EFAULT;
781 cdev->errcode = capi20_get_version(data.contr, &data.version);
782 if (cdev->errcode)
783 return -EIO;
784 if (copy_to_user(argp, &data.version,
785 sizeof(data.version)))
786 return -EFAULT;
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 case CAPI_GET_SERIAL:
Joe Perches475be4d2012-02-19 19:52:38 -0800790 if (copy_from_user(&data.contr, argp,
791 sizeof(data.contr)))
792 return -EFAULT;
793 cdev->errcode = capi20_get_serial(data.contr, data.serial);
794 if (cdev->errcode)
795 return -EIO;
796 if (copy_to_user(argp, data.serial,
797 sizeof(data.serial)))
798 return -EFAULT;
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000799 return 0;
800
Joe Perches475be4d2012-02-19 19:52:38 -0800801 case CAPI_GET_PROFILE:
Joe Perches475be4d2012-02-19 19:52:38 -0800802 if (copy_from_user(&data.contr, argp,
803 sizeof(data.contr)))
804 return -EFAULT;
805
806 if (data.contr == 0) {
807 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (cdev->errcode)
809 return -EIO;
Joe Perches475be4d2012-02-19 19:52:38 -0800810
811 retval = copy_to_user(argp,
812 &data.profile.ncontroller,
813 sizeof(data.profile.ncontroller));
814
815 } else {
816 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
817 if (cdev->errcode)
818 return -EIO;
819
820 retval = copy_to_user(argp, &data.profile,
821 sizeof(data.profile));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
Joe Perches475be4d2012-02-19 19:52:38 -0800823 if (retval)
824 return -EFAULT;
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000825 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 case CAPI_GET_MANUFACTURER:
Joe Perches475be4d2012-02-19 19:52:38 -0800828 if (copy_from_user(&data.contr, argp,
829 sizeof(data.contr)))
830 return -EFAULT;
831 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
832 if (cdev->errcode)
833 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Joe Perches475be4d2012-02-19 19:52:38 -0800835 if (copy_to_user(argp, data.manufacturer,
836 sizeof(data.manufacturer)))
837 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000839 return 0;
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 case CAPI_GET_ERRCODE:
842 data.errcode = cdev->errcode;
843 cdev->errcode = CAPI_NOERROR;
844 if (arg) {
845 if (copy_to_user(argp, &data.errcode,
846 sizeof(data.errcode)))
847 return -EFAULT;
848 }
849 return data.errcode;
850
851 case CAPI_INSTALLED:
852 if (capi20_isinstalled() == CAPI_NOERROR)
853 return 0;
854 return -ENXIO;
855
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000856 case CAPI_MANUFACTURER_CMD: {
Joe Perches475be4d2012-02-19 19:52:38 -0800857 struct capi_manufacturer_cmd mcmd;
858 if (!capable(CAP_SYS_ADMIN))
859 return -EPERM;
860 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
861 return -EFAULT;
862 return capi20_manufacturer(mcmd.cmd, mcmd.data);
863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 case CAPI_SET_FLAGS:
Jan Kiszka05b41492010-02-08 10:12:19 +0000865 case CAPI_CLR_FLAGS: {
866 unsigned userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Jan Kiszka05b41492010-02-08 10:12:19 +0000868 if (copy_from_user(&userflags, argp, sizeof(userflags)))
869 return -EFAULT;
870
871 mutex_lock(&cdev->lock);
872 if (cmd == CAPI_SET_FLAGS)
873 cdev->userflags |= userflags;
874 else
875 cdev->userflags &= ~userflags;
876 mutex_unlock(&cdev->lock);
877 return 0;
878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 case CAPI_GET_FLAGS:
880 if (copy_to_user(argp, &cdev->userflags,
881 sizeof(cdev->userflags)))
882 return -EFAULT;
883 return 0;
884
Tilman Schmidt7dc2ce52012-04-25 13:02:20 +0000885#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
886 case CAPI_NCCI_OPENCOUNT:
887 return 0;
888
889#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka05b41492010-02-08 10:12:19 +0000890 case CAPI_NCCI_OPENCOUNT: {
891 struct capincci *nccip;
892 unsigned ncci;
893 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Jan Kiszka05b41492010-02-08 10:12:19 +0000895 if (copy_from_user(&ncci, argp, sizeof(ncci)))
896 return -EFAULT;
897
898 mutex_lock(&cdev->lock);
899 nccip = capincci_find(cdev, (u32)ncci);
900 if (nccip)
901 count = capincci_minor_opencount(nccip);
902 mutex_unlock(&cdev->lock);
903 return count;
904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Jan Kiszka05b41492010-02-08 10:12:19 +0000906 case CAPI_NCCI_GETUNIT: {
907 struct capincci *nccip;
908 struct capiminor *mp;
909 unsigned ncci;
910 int unit = -ESRCH;
911
912 if (copy_from_user(&ncci, argp, sizeof(ncci)))
913 return -EFAULT;
914
915 mutex_lock(&cdev->lock);
916 nccip = capincci_find(cdev, (u32)ncci);
917 if (nccip) {
918 mp = nccip->minorp;
919 if (mp)
920 unit = mp->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000922 mutex_unlock(&cdev->lock);
923 return unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000925#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
926
927 default:
928 return -EINVAL;
929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
Arnd Bergmann703c6312010-04-27 00:24:02 +0200932static long
933capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
934{
935 int ret;
936
Arnd Bergmann76a64922010-07-11 11:18:53 +0000937 mutex_lock(&capi_mutex);
Arnd Bergmann703c6312010-04-27 00:24:02 +0200938 ret = capi_ioctl(file, cmd, arg);
Arnd Bergmann76a64922010-07-11 11:18:53 +0000939 mutex_unlock(&capi_mutex);
Arnd Bergmann703c6312010-04-27 00:24:02 +0200940
941 return ret;
942}
943
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000944static int capi_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000946 struct capidev *cdev;
947
948 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
949 if (!cdev)
950 return -ENOMEM;
951
Jan Kiszka05b41492010-02-08 10:12:19 +0000952 mutex_init(&cdev->lock);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000953 skb_queue_head_init(&cdev->recvqueue);
954 init_waitqueue_head(&cdev->recvwait);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000955 INIT_LIST_HEAD(&cdev->nccis);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000956 file->private_data = cdev;
957
958 mutex_lock(&capidev_list_lock);
959 list_add_tail(&cdev->list, &capidev_list);
960 mutex_unlock(&capidev_list_lock);
961
962 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000965static int capi_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000967 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000969 mutex_lock(&capidev_list_lock);
970 list_del(&cdev->list);
971 mutex_unlock(&capidev_list_lock);
972
Jan Kiszka05b41492010-02-08 10:12:19 +0000973 if (cdev->ap.applid)
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000974 capi20_release(&cdev->ap);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000975 skb_queue_purge(&cdev->recvqueue);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000976 capincci_free(cdev, 0xffffffff);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000977
978 kfree(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return 0;
980}
981
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800982static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
984 .owner = THIS_MODULE,
985 .llseek = no_llseek,
986 .read = capi_read,
987 .write = capi_write,
988 .poll = capi_poll,
Arnd Bergmann703c6312010-04-27 00:24:02 +0200989 .unlocked_ioctl = capi_unlocked_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 .open = capi_open,
991 .release = capi_release,
992};
993
994#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
995/* -------- tty_operations for capincci ----------------------------- */
996
Jan Kiszka46324512010-02-08 10:12:28 +0000997static int
998capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
999{
Jiri Slaby410235f2012-03-05 14:52:01 +01001000 struct capiminor *mp = capiminor_get(tty->index);
Jiri Slaby81f58352012-01-30 21:14:30 +01001001 int ret = tty_standard_install(driver, tty);
Jan Kiszka46324512010-02-08 10:12:28 +00001002
Jiri Slaby81f58352012-01-30 21:14:30 +01001003 if (ret == 0)
Jan Kiszka46324512010-02-08 10:12:28 +00001004 tty->driver_data = mp;
Jiri Slaby81f58352012-01-30 21:14:30 +01001005 else
Jan Kiszka46324512010-02-08 10:12:28 +00001006 capiminor_put(mp);
1007 return ret;
1008}
1009
1010static void capinc_tty_cleanup(struct tty_struct *tty)
1011{
1012 struct capiminor *mp = tty->driver_data;
1013 tty->driver_data = NULL;
1014 capiminor_put(mp);
1015}
1016
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001017static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Jan Kiszka46324512010-02-08 10:12:28 +00001019 struct capiminor *mp = tty->driver_data;
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001020 int err;
1021
1022 err = tty_port_open(&mp->port, tty, filp);
1023 if (err)
1024 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 handle_minor_recv(mp);
1027 return 0;
1028}
1029
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001030static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Jan Kiszka46324512010-02-08 10:12:28 +00001032 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001034 tty_port_close(&mp->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
Jan Kiszka6576c282010-02-08 10:12:32 +00001037static int capinc_tty_write(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 const unsigned char *buf, int count)
1039{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001040 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 struct sk_buff *skb;
1042
David S. Miller8fb53b92011-05-19 18:20:29 -04001043 pr_debug("capinc_tty_write(count=%d)\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001045 spin_lock_bh(&mp->outlock);
1046 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (skb) {
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001048 mp->outskb = NULL;
1049 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 mp->outbytes += skb->len;
1051 }
1052
Joe Perches475be4d2012-02-19 19:52:38 -08001053 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + count, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (!skb) {
1055 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001056 spin_unlock_bh(&mp->outlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return -ENOMEM;
1058 }
1059
1060 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1061 memcpy(skb_put(skb, count), buf, count);
1062
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001063 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001065 spin_unlock_bh(&mp->outlock);
1066
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001067 handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return count;
1070}
1071
Alan Coxf2545a72008-04-30 00:54:09 -07001072static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001074 struct capiminor *mp = tty->driver_data;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001075 bool invoke_send = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 struct sk_buff *skb;
Alan Coxf2545a72008-04-30 00:54:09 -07001077 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
David S. Miller8fb53b92011-05-19 18:20:29 -04001079 pr_debug("capinc_put_char(%u)\n", ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001081 spin_lock_bh(&mp->outlock);
1082 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (skb) {
1084 if (skb_tailroom(skb) > 0) {
1085 *(skb_put(skb, 1)) = ch;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001086 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001088 mp->outskb = NULL;
1089 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001091 invoke_send = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001093
Joe Perches475be4d2012-02-19 19:52:38 -08001094 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + CAPI_MAX_BLKSIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (skb) {
1096 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1097 *(skb_put(skb, 1)) = ch;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001098 mp->outskb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 } else {
1100 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
Alan Coxf2545a72008-04-30 00:54:09 -07001101 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001103
1104unlock_out:
1105 spin_unlock_bh(&mp->outlock);
1106
1107 if (invoke_send)
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001108 handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001109
Alan Coxf2545a72008-04-30 00:54:09 -07001110 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
1113static void capinc_tty_flush_chars(struct tty_struct *tty)
1114{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001115 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 struct sk_buff *skb;
1117
David S. Miller8fb53b92011-05-19 18:20:29 -04001118 pr_debug("capinc_tty_flush_chars\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001120 spin_lock_bh(&mp->outlock);
1121 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (skb) {
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001123 mp->outskb = NULL;
1124 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001126 spin_unlock_bh(&mp->outlock);
1127
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001128 handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001129 } else
1130 spin_unlock_bh(&mp->outlock);
1131
1132 handle_minor_recv(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
1135static int capinc_tty_write_room(struct tty_struct *tty)
1136{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001137 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 int room;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1141 room *= CAPI_MAX_BLKSIZE;
David S. Miller8fb53b92011-05-19 18:20:29 -04001142 pr_debug("capinc_tty_write_room = %d\n", room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return room;
1144}
1145
Adrian Bunk408b6642005-05-01 08:59:29 -07001146static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001148 struct capiminor *mp = tty->driver_data;
1149
David S. Miller8fb53b92011-05-19 18:20:29 -04001150 pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1151 mp->outbytes, mp->nack,
1152 skb_queue_len(&mp->outqueue),
1153 skb_queue_len(&mp->inqueue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return mp->outbytes;
1155}
1156
Alan Cox6caa76b2011-02-14 16:27:22 +00001157static int capinc_tty_ioctl(struct tty_struct *tty,
Joe Perches475be4d2012-02-19 19:52:38 -08001158 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Alan Cox6caa76b2011-02-14 16:27:22 +00001160 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Joe Perches475be4d2012-02-19 19:52:38 -08001163static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
David S. Miller8fb53b92011-05-19 18:20:29 -04001165 pr_debug("capinc_tty_set_termios\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
Jan Kiszka2c8df722010-02-08 10:12:30 +00001168static void capinc_tty_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001170 struct capiminor *mp = tty->driver_data;
David S. Miller8fb53b92011-05-19 18:20:29 -04001171 pr_debug("capinc_tty_throttle\n");
Jan Kiszka2c8df722010-02-08 10:12:30 +00001172 mp->ttyinstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
Jan Kiszka2c8df722010-02-08 10:12:30 +00001175static void capinc_tty_unthrottle(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;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001178
David S. Miller8fb53b92011-05-19 18:20:29 -04001179 pr_debug("capinc_tty_unthrottle\n");
Jan Kiszka2c8df722010-02-08 10:12:30 +00001180 mp->ttyinstop = 0;
1181 handle_minor_recv(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
1184static void capinc_tty_stop(struct tty_struct *tty)
1185{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001186 struct capiminor *mp = tty->driver_data;
1187
David S. Miller8fb53b92011-05-19 18:20:29 -04001188 pr_debug("capinc_tty_stop\n");
Jan Kiszka2c8df722010-02-08 10:12:30 +00001189 mp->ttyoutstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
1192static void capinc_tty_start(struct tty_struct *tty)
1193{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001194 struct capiminor *mp = tty->driver_data;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001195
David S. Miller8fb53b92011-05-19 18:20:29 -04001196 pr_debug("capinc_tty_start\n");
Jan Kiszka2c8df722010-02-08 10:12:30 +00001197 mp->ttyoutstop = 0;
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001198 handle_minor_send(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
1201static void capinc_tty_hangup(struct tty_struct *tty)
1202{
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001203 struct capiminor *mp = tty->driver_data;
1204
David S. Miller8fb53b92011-05-19 18:20:29 -04001205 pr_debug("capinc_tty_hangup\n");
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001206 tty_port_hangup(&mp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
Alan Cox9e989662008-07-22 11:18:03 +01001209static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
David S. Miller8fb53b92011-05-19 18:20:29 -04001211 pr_debug("capinc_tty_break_ctl(%d)\n", state);
Alan Cox9e989662008-07-22 11:18:03 +01001212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
1215static void capinc_tty_flush_buffer(struct tty_struct *tty)
1216{
David S. Miller8fb53b92011-05-19 18:20:29 -04001217 pr_debug("capinc_tty_flush_buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
1220static void capinc_tty_set_ldisc(struct tty_struct *tty)
1221{
David S. Miller8fb53b92011-05-19 18:20:29 -04001222 pr_debug("capinc_tty_set_ldisc\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
1225static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1226{
David S. Miller8fb53b92011-05-19 18:20:29 -04001227 pr_debug("capinc_tty_send_xchar(%d)\n", ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001230static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 .open = capinc_tty_open,
1232 .close = capinc_tty_close,
1233 .write = capinc_tty_write,
1234 .put_char = capinc_tty_put_char,
1235 .flush_chars = capinc_tty_flush_chars,
1236 .write_room = capinc_tty_write_room,
1237 .chars_in_buffer = capinc_tty_chars_in_buffer,
1238 .ioctl = capinc_tty_ioctl,
1239 .set_termios = capinc_tty_set_termios,
1240 .throttle = capinc_tty_throttle,
1241 .unthrottle = capinc_tty_unthrottle,
1242 .stop = capinc_tty_stop,
1243 .start = capinc_tty_start,
1244 .hangup = capinc_tty_hangup,
1245 .break_ctl = capinc_tty_break_ctl,
1246 .flush_buffer = capinc_tty_flush_buffer,
1247 .set_ldisc = capinc_tty_set_ldisc,
1248 .send_xchar = capinc_tty_send_xchar,
Jan Kiszka46324512010-02-08 10:12:28 +00001249 .install = capinc_tty_install,
1250 .cleanup = capinc_tty_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251};
1252
Jan Kiszkae76b1542010-02-08 10:12:24 +00001253static int __init capinc_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
1255 struct tty_driver *drv;
Jan Kiszkae76b1542010-02-08 10:12:24 +00001256 int err;
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (capi_ttyminors > CAPINC_MAX_PORTS)
1259 capi_ttyminors = CAPINC_MAX_PORTS;
1260 if (capi_ttyminors <= 0)
1261 capi_ttyminors = CAPINC_NR_PORTS;
1262
Julia Lawall5160ee92014-07-29 17:16:44 +02001263 capiminors = kzalloc(sizeof(struct capiminor *) * capi_ttyminors,
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001264 GFP_KERNEL);
1265 if (!capiminors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return -ENOMEM;
1267
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001268 drv = alloc_tty_driver(capi_ttyminors);
1269 if (!drv) {
1270 kfree(capiminors);
1271 return -ENOMEM;
1272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 drv->driver_name = "capi_nc";
Paul Bolled1cadce2014-06-01 23:47:24 +02001274 drv->name = "capi!";
Jan Kiszkae95ac142010-02-08 10:12:26 +00001275 drv->major = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 drv->minor_start = 0;
1277 drv->type = TTY_DRIVER_TYPE_SERIAL;
1278 drv->subtype = SERIAL_TYPE_NORMAL;
1279 drv->init_termios = tty_std_termios;
1280 drv->init_termios.c_iflag = ICRNL;
1281 drv->init_termios.c_oflag = OPOST | ONLCR;
1282 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1283 drv->init_termios.c_lflag = 0;
Jan Kiszka40fb2d02010-02-08 10:12:25 +00001284 drv->flags =
1285 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1286 TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 tty_set_operations(drv, &capinc_ops);
Jan Kiszkae76b1542010-02-08 10:12:24 +00001288
1289 err = tty_register_driver(drv);
1290 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 put_tty_driver(drv);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001292 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 printk(KERN_ERR "Couldn't register capi_nc driver\n");
Jan Kiszkae76b1542010-02-08 10:12:24 +00001294 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296 capinc_tty_driver = drv;
1297 return 0;
1298}
1299
Jan Kiszkae76b1542010-02-08 10:12:24 +00001300static void __exit capinc_tty_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Jan Kiszkae76b1542010-02-08 10:12:24 +00001302 tty_unregister_driver(capinc_tty_driver);
1303 put_tty_driver(capinc_tty_driver);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001304 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
Jan Kiszka501c87a2010-02-08 10:12:16 +00001307#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1308
1309static inline int capinc_tty_init(void)
1310{
1311 return 0;
1312}
1313
1314static inline void capinc_tty_exit(void) { }
1315
1316#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318/* -------- /proc functions ----------------------------------------- */
1319
1320/*
1321 * /proc/capi/capi20:
1322 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1323 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001324static int capi20_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Joe Perches475be4d2012-02-19 19:52:38 -08001326 struct capidev *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001329 mutex_lock(&capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 list_for_each(l, &capidev_list) {
1331 cdev = list_entry(l, struct capidev, list);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001332 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001333 cdev->ap.applid,
1334 cdev->ap.nrecvctlpkt,
1335 cdev->ap.nrecvdatapkt,
1336 cdev->ap.nsentctlpkt,
1337 cdev->ap.nsentdatapkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001339 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001343static int capi20_proc_open(struct inode *inode, struct file *file)
1344{
1345 return single_open(file, capi20_proc_show, NULL);
1346}
1347
1348static const struct file_operations capi20_proc_fops = {
1349 .owner = THIS_MODULE,
1350 .open = capi20_proc_open,
1351 .read = seq_read,
1352 .llseek = seq_lseek,
1353 .release = single_release,
1354};
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356/*
1357 * /proc/capi/capi20ncci:
1358 * applid ncci
1359 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001360static int capi20ncci_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Jan Kiszka884f5c42010-02-08 10:12:22 +00001362 struct capidev *cdev;
1363 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001365 mutex_lock(&capidev_list_lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001366 list_for_each_entry(cdev, &capidev_list, list) {
Jan Kiszka05b41492010-02-08 10:12:19 +00001367 mutex_lock(&cdev->lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001368 list_for_each_entry(np, &cdev->nccis, list)
1369 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
Jan Kiszka05b41492010-02-08 10:12:19 +00001370 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001372 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001373 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001376static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1377{
1378 return single_open(file, capi20ncci_proc_show, NULL);
1379}
1380
1381static const struct file_operations capi20ncci_proc_fops = {
1382 .owner = THIS_MODULE,
1383 .open = capi20ncci_proc_open,
1384 .read = seq_read,
1385 .llseek = seq_lseek,
1386 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387};
1388
1389static void __init proc_init(void)
1390{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001391 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1392 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
1395static void __exit proc_exit(void)
1396{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001397 remove_proc_entry("capi/capi20", NULL);
1398 remove_proc_entry("capi/capi20ncci", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
1401/* -------- init function and module interface ---------------------- */
1402
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404static int __init capi_init(void)
1405{
Jan Kiszka88549d62010-02-08 10:12:09 +00001406 const char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001407 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Andrew Morton6d9eac32006-03-28 01:56:19 -08001409 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1410 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001412 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001414 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (IS_ERR(capi_class)) {
1416 unregister_chrdev(capi_major, "capi20");
1417 return PTR_ERR(capi_class);
1418 }
1419
Paul Bolled1cadce2014-06-01 23:47:24 +02001420 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (capinc_tty_init() < 0) {
Tony Jonesd78b0362007-09-25 02:03:03 +02001423 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001424 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 unregister_chrdev(capi_major, "capi20");
1426 return -ENOMEM;
1427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 proc_init();
1430
Jan Kiszka1f90d662011-04-06 10:58:37 +00001431#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Joe Perches475be4d2012-02-19 19:52:38 -08001432 compileinfo = " (middleware)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433#else
Joe Perches475be4d2012-02-19 19:52:38 -08001434 compileinfo = " (no middleware)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435#endif
Jan Kiszka88549d62010-02-08 10:12:09 +00001436 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1437 capi_major, compileinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 return 0;
1440}
1441
1442static void __exit capi_exit(void)
1443{
1444 proc_exit();
1445
Tony Jonesd78b0362007-09-25 02:03:03 +02001446 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001447 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 capinc_tty_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
1453module_init(capi_init);
1454module_exit(capi_exit);