blob: 89562a845f6aee86af1bb5bd54f1ceef228eb2c8 [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
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000572 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 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);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000630 tty = tty_port_tty_get(&mp->port);
631 if (tty) {
632 tty_wakeup(tty);
633 tty_kref_put(tty);
634 }
Jan Kiszkaeef0ced2010-02-08 10:12:42 +0000635 handle_minor_send(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 } else {
638 /* ups, let capi application handle it :-) */
639 skb_queue_tail(&cdev->recvqueue, skb);
640 wake_up_interruptible(&cdev->recvwait);
641 }
642#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000643
Jan Kiszka05b41492010-02-08 10:12:19 +0000644unlock_out:
Jan Kiszka05b41492010-02-08 10:12:19 +0000645 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648/* -------- file_operations for capidev ----------------------------- */
649
650static ssize_t
651capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
652{
Joe Perches54cbb1c2010-07-12 10:50:02 +0000653 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 struct sk_buff *skb;
655 size_t copied;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000656 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 if (!cdev->ap.applid)
659 return -ENODEV;
660
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000661 skb = skb_dequeue(&cdev->recvqueue);
662 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (file->f_flags & O_NONBLOCK)
664 return -EAGAIN;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000665 err = wait_event_interruptible(cdev->recvwait,
Joe Perches475be4d2012-02-19 19:52:38 -0800666 (skb = skb_dequeue(&cdev->recvqueue)));
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000667 if (err)
668 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670 if (skb->len > count) {
671 skb_queue_head(&cdev->recvqueue, skb);
672 return -EMSGSIZE;
673 }
674 if (copy_to_user(buf, skb->data, skb->len)) {
675 skb_queue_head(&cdev->recvqueue, skb);
676 return -EFAULT;
677 }
678 copied = skb->len;
679
680 kfree_skb(skb);
681
682 return copied;
683}
684
685static ssize_t
686capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
687{
Joe Perches54cbb1c2010-07-12 10:50:02 +0000688 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct sk_buff *skb;
690 u16 mlen;
691
692 if (!cdev->ap.applid)
693 return -ENODEV;
694
695 skb = alloc_skb(count, GFP_USER);
696 if (!skb)
697 return -ENOMEM;
698
699 if (copy_from_user(skb_put(skb, count), buf, count)) {
700 kfree_skb(skb);
701 return -EFAULT;
702 }
703 mlen = CAPIMSG_LEN(skb->data);
704 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
705 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
706 kfree_skb(skb);
707 return -EINVAL;
708 }
709 } else {
710 if (mlen != count) {
711 kfree_skb(skb);
712 return -EINVAL;
713 }
714 }
715 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
716
717 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Jan Kiszka05b41492010-02-08 10:12:19 +0000718 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000720 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722
723 cdev->errcode = capi20_put_message(&cdev->ap, skb);
724
725 if (cdev->errcode) {
726 kfree_skb(skb);
727 return -EIO;
728 }
729 return count;
730}
731
732static unsigned int
Joe Perches475be4d2012-02-19 19:52:38 -0800733capi_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Joe Perches54cbb1c2010-07-12 10:50:02 +0000735 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 unsigned int mask = 0;
737
738 if (!cdev->ap.applid)
739 return POLLERR;
740
741 poll_wait(file, &(cdev->recvwait), wait);
742 mask = POLLOUT | POLLWRNORM;
743 if (!skb_queue_empty(&cdev->recvqueue))
744 mask |= POLLIN | POLLRDNORM;
745 return mask;
746}
747
748static int
Arnd Bergmann703c6312010-04-27 00:24:02 +0200749capi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 capi_ioctl_struct data;
753 int retval = -EINVAL;
754 void __user *argp = (void __user *)arg;
755
756 switch (cmd) {
757 case CAPI_REGISTER:
Jan Kiszka05b41492010-02-08 10:12:19 +0000758 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Jan Kiszka05b41492010-02-08 10:12:19 +0000760 if (cdev->ap.applid) {
761 retval = -EEXIST;
762 goto register_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000764 if (copy_from_user(&cdev->ap.rparam, argp,
765 sizeof(struct capi_register_params))) {
766 retval = -EFAULT;
767 goto register_out;
768 }
769 cdev->ap.private = cdev;
770 cdev->ap.recv_message = capi_recv_message;
771 cdev->errcode = capi20_register(&cdev->ap);
772 retval = (int)cdev->ap.applid;
773 if (cdev->errcode) {
774 cdev->ap.applid = 0;
775 retval = -EIO;
776 }
777
778register_out:
779 mutex_unlock(&cdev->lock);
780 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 case CAPI_GET_VERSION:
Joe Perches475be4d2012-02-19 19:52:38 -0800783 if (copy_from_user(&data.contr, argp,
784 sizeof(data.contr)))
785 return -EFAULT;
786 cdev->errcode = capi20_get_version(data.contr, &data.version);
787 if (cdev->errcode)
788 return -EIO;
789 if (copy_to_user(argp, &data.version,
790 sizeof(data.version)))
791 return -EFAULT;
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000792 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 case CAPI_GET_SERIAL:
Joe Perches475be4d2012-02-19 19:52:38 -0800795 if (copy_from_user(&data.contr, argp,
796 sizeof(data.contr)))
797 return -EFAULT;
798 cdev->errcode = capi20_get_serial(data.contr, data.serial);
799 if (cdev->errcode)
800 return -EIO;
801 if (copy_to_user(argp, data.serial,
802 sizeof(data.serial)))
803 return -EFAULT;
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000804 return 0;
805
Joe Perches475be4d2012-02-19 19:52:38 -0800806 case CAPI_GET_PROFILE:
Joe Perches475be4d2012-02-19 19:52:38 -0800807 if (copy_from_user(&data.contr, argp,
808 sizeof(data.contr)))
809 return -EFAULT;
810
811 if (data.contr == 0) {
812 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (cdev->errcode)
814 return -EIO;
Joe Perches475be4d2012-02-19 19:52:38 -0800815
816 retval = copy_to_user(argp,
817 &data.profile.ncontroller,
818 sizeof(data.profile.ncontroller));
819
820 } else {
821 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
822 if (cdev->errcode)
823 return -EIO;
824
825 retval = copy_to_user(argp, &data.profile,
826 sizeof(data.profile));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
Joe Perches475be4d2012-02-19 19:52:38 -0800828 if (retval)
829 return -EFAULT;
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 case CAPI_GET_MANUFACTURER:
Joe Perches475be4d2012-02-19 19:52:38 -0800833 if (copy_from_user(&data.contr, argp,
834 sizeof(data.contr)))
835 return -EFAULT;
836 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
837 if (cdev->errcode)
838 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Joe Perches475be4d2012-02-19 19:52:38 -0800840 if (copy_to_user(argp, data.manufacturer,
841 sizeof(data.manufacturer)))
842 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000844 return 0;
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 case CAPI_GET_ERRCODE:
847 data.errcode = cdev->errcode;
848 cdev->errcode = CAPI_NOERROR;
849 if (arg) {
850 if (copy_to_user(argp, &data.errcode,
851 sizeof(data.errcode)))
852 return -EFAULT;
853 }
854 return data.errcode;
855
856 case CAPI_INSTALLED:
857 if (capi20_isinstalled() == CAPI_NOERROR)
858 return 0;
859 return -ENXIO;
860
Tilman Schmidt7fdaadc2012-04-25 13:02:20 +0000861 case CAPI_MANUFACTURER_CMD: {
Joe Perches475be4d2012-02-19 19:52:38 -0800862 struct capi_manufacturer_cmd mcmd;
863 if (!capable(CAP_SYS_ADMIN))
864 return -EPERM;
865 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
866 return -EFAULT;
867 return capi20_manufacturer(mcmd.cmd, mcmd.data);
868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 case CAPI_SET_FLAGS:
Jan Kiszka05b41492010-02-08 10:12:19 +0000870 case CAPI_CLR_FLAGS: {
871 unsigned userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Jan Kiszka05b41492010-02-08 10:12:19 +0000873 if (copy_from_user(&userflags, argp, sizeof(userflags)))
874 return -EFAULT;
875
876 mutex_lock(&cdev->lock);
877 if (cmd == CAPI_SET_FLAGS)
878 cdev->userflags |= userflags;
879 else
880 cdev->userflags &= ~userflags;
881 mutex_unlock(&cdev->lock);
882 return 0;
883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 case CAPI_GET_FLAGS:
885 if (copy_to_user(argp, &cdev->userflags,
886 sizeof(cdev->userflags)))
887 return -EFAULT;
888 return 0;
889
Tilman Schmidt7dc2ce52012-04-25 13:02:20 +0000890#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
891 case CAPI_NCCI_OPENCOUNT:
892 return 0;
893
894#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka05b41492010-02-08 10:12:19 +0000895 case CAPI_NCCI_OPENCOUNT: {
896 struct capincci *nccip;
897 unsigned ncci;
898 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Jan Kiszka05b41492010-02-08 10:12:19 +0000900 if (copy_from_user(&ncci, argp, sizeof(ncci)))
901 return -EFAULT;
902
903 mutex_lock(&cdev->lock);
904 nccip = capincci_find(cdev, (u32)ncci);
905 if (nccip)
906 count = capincci_minor_opencount(nccip);
907 mutex_unlock(&cdev->lock);
908 return count;
909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Jan Kiszka05b41492010-02-08 10:12:19 +0000911 case CAPI_NCCI_GETUNIT: {
912 struct capincci *nccip;
913 struct capiminor *mp;
914 unsigned ncci;
915 int unit = -ESRCH;
916
917 if (copy_from_user(&ncci, argp, sizeof(ncci)))
918 return -EFAULT;
919
920 mutex_lock(&cdev->lock);
921 nccip = capincci_find(cdev, (u32)ncci);
922 if (nccip) {
923 mp = nccip->minorp;
924 if (mp)
925 unit = mp->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000927 mutex_unlock(&cdev->lock);
928 return unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000930#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
931
932 default:
933 return -EINVAL;
934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Arnd Bergmann703c6312010-04-27 00:24:02 +0200937static long
938capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
939{
940 int ret;
941
Arnd Bergmann76a64922010-07-11 11:18:53 +0000942 mutex_lock(&capi_mutex);
Arnd Bergmann703c6312010-04-27 00:24:02 +0200943 ret = capi_ioctl(file, cmd, arg);
Arnd Bergmann76a64922010-07-11 11:18:53 +0000944 mutex_unlock(&capi_mutex);
Arnd Bergmann703c6312010-04-27 00:24:02 +0200945
946 return ret;
947}
948
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000949static int capi_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000951 struct capidev *cdev;
952
953 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
954 if (!cdev)
955 return -ENOMEM;
956
Jan Kiszka05b41492010-02-08 10:12:19 +0000957 mutex_init(&cdev->lock);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000958 skb_queue_head_init(&cdev->recvqueue);
959 init_waitqueue_head(&cdev->recvwait);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000960 INIT_LIST_HEAD(&cdev->nccis);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000961 file->private_data = cdev;
962
963 mutex_lock(&capidev_list_lock);
964 list_add_tail(&cdev->list, &capidev_list);
965 mutex_unlock(&capidev_list_lock);
966
967 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000970static int capi_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000972 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000974 mutex_lock(&capidev_list_lock);
975 list_del(&cdev->list);
976 mutex_unlock(&capidev_list_lock);
977
Jan Kiszka05b41492010-02-08 10:12:19 +0000978 if (cdev->ap.applid)
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000979 capi20_release(&cdev->ap);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000980 skb_queue_purge(&cdev->recvqueue);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000981 capincci_free(cdev, 0xffffffff);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000982
983 kfree(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return 0;
985}
986
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800987static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 .owner = THIS_MODULE,
990 .llseek = no_llseek,
991 .read = capi_read,
992 .write = capi_write,
993 .poll = capi_poll,
Arnd Bergmann703c6312010-04-27 00:24:02 +0200994 .unlocked_ioctl = capi_unlocked_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 .open = capi_open,
996 .release = capi_release,
997};
998
999#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1000/* -------- tty_operations for capincci ----------------------------- */
1001
Jan Kiszka46324512010-02-08 10:12:28 +00001002static int
1003capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1004{
Jiri Slaby410235f2012-03-05 14:52:01 +01001005 struct capiminor *mp = capiminor_get(tty->index);
Jiri Slaby81f58352012-01-30 21:14:30 +01001006 int ret = tty_standard_install(driver, tty);
Jan Kiszka46324512010-02-08 10:12:28 +00001007
Jiri Slaby81f58352012-01-30 21:14:30 +01001008 if (ret == 0)
Jan Kiszka46324512010-02-08 10:12:28 +00001009 tty->driver_data = mp;
Jiri Slaby81f58352012-01-30 21:14:30 +01001010 else
Jan Kiszka46324512010-02-08 10:12:28 +00001011 capiminor_put(mp);
1012 return ret;
1013}
1014
1015static void capinc_tty_cleanup(struct tty_struct *tty)
1016{
1017 struct capiminor *mp = tty->driver_data;
1018 tty->driver_data = NULL;
1019 capiminor_put(mp);
1020}
1021
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001022static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Jan Kiszka46324512010-02-08 10:12:28 +00001024 struct capiminor *mp = tty->driver_data;
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001025 int err;
1026
1027 err = tty_port_open(&mp->port, tty, filp);
1028 if (err)
1029 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 handle_minor_recv(mp);
1032 return 0;
1033}
1034
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001035static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Jan Kiszka46324512010-02-08 10:12:28 +00001037 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001039 tty_port_close(&mp->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Jan Kiszka6576c282010-02-08 10:12:32 +00001042static int capinc_tty_write(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 const unsigned char *buf, int count)
1044{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001045 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 struct sk_buff *skb;
1047
David S. Miller8fb53b92011-05-19 18:20:29 -04001048 pr_debug("capinc_tty_write(count=%d)\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001050 spin_lock_bh(&mp->outlock);
1051 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (skb) {
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001053 mp->outskb = NULL;
1054 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 mp->outbytes += skb->len;
1056 }
1057
Joe Perches475be4d2012-02-19 19:52:38 -08001058 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + count, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (!skb) {
1060 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001061 spin_unlock_bh(&mp->outlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return -ENOMEM;
1063 }
1064
1065 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1066 memcpy(skb_put(skb, count), buf, count);
1067
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001068 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001070 spin_unlock_bh(&mp->outlock);
1071
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001072 handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return count;
1075}
1076
Alan Coxf2545a72008-04-30 00:54:09 -07001077static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001079 struct capiminor *mp = tty->driver_data;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001080 bool invoke_send = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 struct sk_buff *skb;
Alan Coxf2545a72008-04-30 00:54:09 -07001082 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
David S. Miller8fb53b92011-05-19 18:20:29 -04001084 pr_debug("capinc_put_char(%u)\n", ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001086 spin_lock_bh(&mp->outlock);
1087 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (skb) {
1089 if (skb_tailroom(skb) > 0) {
1090 *(skb_put(skb, 1)) = ch;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001091 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001093 mp->outskb = NULL;
1094 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001096 invoke_send = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001098
Joe Perches475be4d2012-02-19 19:52:38 -08001099 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + CAPI_MAX_BLKSIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (skb) {
1101 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1102 *(skb_put(skb, 1)) = ch;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001103 mp->outskb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 } else {
1105 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
Alan Coxf2545a72008-04-30 00:54:09 -07001106 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001108
1109unlock_out:
1110 spin_unlock_bh(&mp->outlock);
1111
1112 if (invoke_send)
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001113 handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001114
Alan Coxf2545a72008-04-30 00:54:09 -07001115 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
1118static void capinc_tty_flush_chars(struct tty_struct *tty)
1119{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001120 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 struct sk_buff *skb;
1122
David S. Miller8fb53b92011-05-19 18:20:29 -04001123 pr_debug("capinc_tty_flush_chars\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001125 spin_lock_bh(&mp->outlock);
1126 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (skb) {
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001128 mp->outskb = NULL;
1129 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001131 spin_unlock_bh(&mp->outlock);
1132
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001133 handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001134 } else
1135 spin_unlock_bh(&mp->outlock);
1136
1137 handle_minor_recv(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
1140static int capinc_tty_write_room(struct tty_struct *tty)
1141{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001142 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 int room;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1146 room *= CAPI_MAX_BLKSIZE;
David S. Miller8fb53b92011-05-19 18:20:29 -04001147 pr_debug("capinc_tty_write_room = %d\n", room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return room;
1149}
1150
Adrian Bunk408b6642005-05-01 08:59:29 -07001151static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001153 struct capiminor *mp = tty->driver_data;
1154
David S. Miller8fb53b92011-05-19 18:20:29 -04001155 pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1156 mp->outbytes, mp->nack,
1157 skb_queue_len(&mp->outqueue),
1158 skb_queue_len(&mp->inqueue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return mp->outbytes;
1160}
1161
Alan Cox6caa76b2011-02-14 16:27:22 +00001162static int capinc_tty_ioctl(struct tty_struct *tty,
Joe Perches475be4d2012-02-19 19:52:38 -08001163 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Alan Cox6caa76b2011-02-14 16:27:22 +00001165 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
Joe Perches475be4d2012-02-19 19:52:38 -08001168static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
David S. Miller8fb53b92011-05-19 18:20:29 -04001170 pr_debug("capinc_tty_set_termios\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
Jan Kiszka2c8df722010-02-08 10:12:30 +00001173static void capinc_tty_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001175 struct capiminor *mp = tty->driver_data;
David S. Miller8fb53b92011-05-19 18:20:29 -04001176 pr_debug("capinc_tty_throttle\n");
Jan Kiszka2c8df722010-02-08 10:12:30 +00001177 mp->ttyinstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
Jan Kiszka2c8df722010-02-08 10:12:30 +00001180static void capinc_tty_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001182 struct capiminor *mp = tty->driver_data;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001183
David S. Miller8fb53b92011-05-19 18:20:29 -04001184 pr_debug("capinc_tty_unthrottle\n");
Jan Kiszka2c8df722010-02-08 10:12:30 +00001185 mp->ttyinstop = 0;
1186 handle_minor_recv(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
1189static void capinc_tty_stop(struct tty_struct *tty)
1190{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001191 struct capiminor *mp = tty->driver_data;
1192
David S. Miller8fb53b92011-05-19 18:20:29 -04001193 pr_debug("capinc_tty_stop\n");
Jan Kiszka2c8df722010-02-08 10:12:30 +00001194 mp->ttyoutstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
1197static void capinc_tty_start(struct tty_struct *tty)
1198{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001199 struct capiminor *mp = tty->driver_data;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001200
David S. Miller8fb53b92011-05-19 18:20:29 -04001201 pr_debug("capinc_tty_start\n");
Jan Kiszka2c8df722010-02-08 10:12:30 +00001202 mp->ttyoutstop = 0;
Jan Kiszkaeef0ced2010-02-08 10:12:42 +00001203 handle_minor_send(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
1206static void capinc_tty_hangup(struct tty_struct *tty)
1207{
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001208 struct capiminor *mp = tty->driver_data;
1209
David S. Miller8fb53b92011-05-19 18:20:29 -04001210 pr_debug("capinc_tty_hangup\n");
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001211 tty_port_hangup(&mp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Alan Cox9e989662008-07-22 11:18:03 +01001214static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
David S. Miller8fb53b92011-05-19 18:20:29 -04001216 pr_debug("capinc_tty_break_ctl(%d)\n", state);
Alan Cox9e989662008-07-22 11:18:03 +01001217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
1220static void capinc_tty_flush_buffer(struct tty_struct *tty)
1221{
David S. Miller8fb53b92011-05-19 18:20:29 -04001222 pr_debug("capinc_tty_flush_buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
1225static void capinc_tty_set_ldisc(struct tty_struct *tty)
1226{
David S. Miller8fb53b92011-05-19 18:20:29 -04001227 pr_debug("capinc_tty_set_ldisc\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
1230static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1231{
David S. Miller8fb53b92011-05-19 18:20:29 -04001232 pr_debug("capinc_tty_send_xchar(%d)\n", ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001235static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 .open = capinc_tty_open,
1237 .close = capinc_tty_close,
1238 .write = capinc_tty_write,
1239 .put_char = capinc_tty_put_char,
1240 .flush_chars = capinc_tty_flush_chars,
1241 .write_room = capinc_tty_write_room,
1242 .chars_in_buffer = capinc_tty_chars_in_buffer,
1243 .ioctl = capinc_tty_ioctl,
1244 .set_termios = capinc_tty_set_termios,
1245 .throttle = capinc_tty_throttle,
1246 .unthrottle = capinc_tty_unthrottle,
1247 .stop = capinc_tty_stop,
1248 .start = capinc_tty_start,
1249 .hangup = capinc_tty_hangup,
1250 .break_ctl = capinc_tty_break_ctl,
1251 .flush_buffer = capinc_tty_flush_buffer,
1252 .set_ldisc = capinc_tty_set_ldisc,
1253 .send_xchar = capinc_tty_send_xchar,
Jan Kiszka46324512010-02-08 10:12:28 +00001254 .install = capinc_tty_install,
1255 .cleanup = capinc_tty_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256};
1257
Jan Kiszkae76b1542010-02-08 10:12:24 +00001258static int __init capinc_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
1260 struct tty_driver *drv;
Jan Kiszkae76b1542010-02-08 10:12:24 +00001261 int err;
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (capi_ttyminors > CAPINC_MAX_PORTS)
1264 capi_ttyminors = CAPINC_MAX_PORTS;
1265 if (capi_ttyminors <= 0)
1266 capi_ttyminors = CAPINC_NR_PORTS;
1267
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001268 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1269 GFP_KERNEL);
1270 if (!capiminors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return -ENOMEM;
1272
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001273 drv = alloc_tty_driver(capi_ttyminors);
1274 if (!drv) {
1275 kfree(capiminors);
1276 return -ENOMEM;
1277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 drv->driver_name = "capi_nc";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 drv->name = "capi";
Jan Kiszkae95ac142010-02-08 10:12:26 +00001280 drv->major = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 drv->minor_start = 0;
1282 drv->type = TTY_DRIVER_TYPE_SERIAL;
1283 drv->subtype = SERIAL_TYPE_NORMAL;
1284 drv->init_termios = tty_std_termios;
1285 drv->init_termios.c_iflag = ICRNL;
1286 drv->init_termios.c_oflag = OPOST | ONLCR;
1287 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1288 drv->init_termios.c_lflag = 0;
Jan Kiszka40fb2d02010-02-08 10:12:25 +00001289 drv->flags =
1290 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1291 TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 tty_set_operations(drv, &capinc_ops);
Jan Kiszkae76b1542010-02-08 10:12:24 +00001293
1294 err = tty_register_driver(drv);
1295 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 put_tty_driver(drv);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001297 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 printk(KERN_ERR "Couldn't register capi_nc driver\n");
Jan Kiszkae76b1542010-02-08 10:12:24 +00001299 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301 capinc_tty_driver = drv;
1302 return 0;
1303}
1304
Jan Kiszkae76b1542010-02-08 10:12:24 +00001305static void __exit capinc_tty_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Jan Kiszkae76b1542010-02-08 10:12:24 +00001307 tty_unregister_driver(capinc_tty_driver);
1308 put_tty_driver(capinc_tty_driver);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001309 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
Jan Kiszka501c87a2010-02-08 10:12:16 +00001312#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1313
1314static inline int capinc_tty_init(void)
1315{
1316 return 0;
1317}
1318
1319static inline void capinc_tty_exit(void) { }
1320
1321#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323/* -------- /proc functions ----------------------------------------- */
1324
1325/*
1326 * /proc/capi/capi20:
1327 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1328 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001329static int capi20_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Joe Perches475be4d2012-02-19 19:52:38 -08001331 struct capidev *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001334 mutex_lock(&capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 list_for_each(l, &capidev_list) {
1336 cdev = list_entry(l, struct capidev, list);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001337 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001338 cdev->ap.applid,
1339 cdev->ap.nrecvctlpkt,
1340 cdev->ap.nrecvdatapkt,
1341 cdev->ap.nsentctlpkt,
1342 cdev->ap.nsentdatapkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001344 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001348static int capi20_proc_open(struct inode *inode, struct file *file)
1349{
1350 return single_open(file, capi20_proc_show, NULL);
1351}
1352
1353static const struct file_operations capi20_proc_fops = {
1354 .owner = THIS_MODULE,
1355 .open = capi20_proc_open,
1356 .read = seq_read,
1357 .llseek = seq_lseek,
1358 .release = single_release,
1359};
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361/*
1362 * /proc/capi/capi20ncci:
1363 * applid ncci
1364 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001365static int capi20ncci_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
Jan Kiszka884f5c42010-02-08 10:12:22 +00001367 struct capidev *cdev;
1368 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001370 mutex_lock(&capidev_list_lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001371 list_for_each_entry(cdev, &capidev_list, list) {
Jan Kiszka05b41492010-02-08 10:12:19 +00001372 mutex_lock(&cdev->lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001373 list_for_each_entry(np, &cdev->nccis, list)
1374 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
Jan Kiszka05b41492010-02-08 10:12:19 +00001375 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001377 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001381static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1382{
1383 return single_open(file, capi20ncci_proc_show, NULL);
1384}
1385
1386static const struct file_operations capi20ncci_proc_fops = {
1387 .owner = THIS_MODULE,
1388 .open = capi20ncci_proc_open,
1389 .read = seq_read,
1390 .llseek = seq_lseek,
1391 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392};
1393
1394static void __init proc_init(void)
1395{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001396 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1397 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
1400static void __exit proc_exit(void)
1401{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001402 remove_proc_entry("capi/capi20", NULL);
1403 remove_proc_entry("capi/capi20ncci", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
1406/* -------- init function and module interface ---------------------- */
1407
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409static int __init capi_init(void)
1410{
Jan Kiszka88549d62010-02-08 10:12:09 +00001411 const char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001412 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Andrew Morton6d9eac32006-03-28 01:56:19 -08001414 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1415 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001417 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001419 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (IS_ERR(capi_class)) {
1421 unregister_chrdev(capi_major, "capi20");
1422 return PTR_ERR(capi_class);
1423 }
1424
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -07001425 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (capinc_tty_init() < 0) {
Tony Jonesd78b0362007-09-25 02:03:03 +02001428 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001429 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 unregister_chrdev(capi_major, "capi20");
1431 return -ENOMEM;
1432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 proc_init();
1435
Jan Kiszka1f90d662011-04-06 10:58:37 +00001436#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Joe Perches475be4d2012-02-19 19:52:38 -08001437 compileinfo = " (middleware)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438#else
Joe Perches475be4d2012-02-19 19:52:38 -08001439 compileinfo = " (no middleware)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440#endif
Jan Kiszka88549d62010-02-08 10:12:09 +00001441 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1442 capi_major, compileinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 return 0;
1445}
1446
1447static void __exit capi_exit(void)
1448{
1449 proc_exit();
1450
Tony Jonesd78b0362007-09-25 02:03:03 +02001451 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001452 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 capinc_tty_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
1458module_init(capi_init);
1459module_exit(capi_exit);