Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* $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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #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 Kaehlcke | 9ea6e5d | 2007-05-08 00:32:43 -0700 | [diff] [blame] | 21 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/mm.h> |
Jonathan Corbet | a237f3b | 2008-05-16 14:15:33 -0600 | [diff] [blame] | 23 | #include <linux/smp_lock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/timer.h> |
| 25 | #include <linux/wait.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/tty.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/netdevice.h> |
| 28 | #include <linux/ppp_defs.h> |
| 29 | #include <linux/if_ppp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/skbuff.h> |
| 31 | #include <linux/proc_fs.h> |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 32 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/poll.h> |
| 34 | #include <linux/capi.h> |
| 35 | #include <linux/kernelcapi.h> |
| 36 | #include <linux/init.h> |
| 37 | #include <linux/device.h> |
| 38 | #include <linux/moduleparam.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/isdn/capiutil.h> |
| 40 | #include <linux/isdn/capicmd.h> |
Jan Kiszka | 90926f0 | 2010-02-08 10:12:06 +0000 | [diff] [blame] | 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "capifs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface"); |
| 45 | MODULE_AUTHOR("Carsten Paeth"); |
| 46 | MODULE_LICENSE("GPL"); |
| 47 | |
| 48 | #undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */ |
| 49 | #undef _DEBUG_TTYFUNCS /* call to tty_driver */ |
| 50 | #undef _DEBUG_DATAFLOW /* data flow */ |
| 51 | |
| 52 | /* -------- driver information -------------------------------------- */ |
| 53 | |
gregkh@suse.de | 56b2293 | 2005-03-23 10:01:41 -0800 | [diff] [blame] | 54 | static struct class *capi_class; |
Adrian Bunk | 408b664 | 2005-05-01 08:59:29 -0700 | [diff] [blame] | 55 | static int capi_major = 68; /* allocated */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | module_param_named(major, capi_major, uint, 0); |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 60 | #define CAPINC_NR_PORTS 32 |
| 61 | #define CAPINC_MAX_PORTS 256 |
| 62 | |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 63 | static int capi_ttyminors = CAPINC_NR_PORTS; |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | module_param_named(ttyminors, capi_ttyminors, uint, 0); |
| 66 | #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ |
| 67 | |
| 68 | /* -------- defines ------------------------------------------------- */ |
| 69 | |
| 70 | #define CAPINC_MAX_RECVQUEUE 10 |
| 71 | #define CAPINC_MAX_SENDQUEUE 10 |
| 72 | #define CAPI_MAX_BLKSIZE 2048 |
| 73 | |
| 74 | /* -------- data structures ----------------------------------------- */ |
| 75 | |
| 76 | struct capidev; |
| 77 | struct capincci; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | struct capiminor; |
| 79 | |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 80 | struct datahandle_queue { |
| 81 | struct list_head list; |
| 82 | u16 datahandle; |
| 83 | }; |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | struct capiminor { |
Jan Kiszka | 0159d54 | 2010-02-08 10:12:27 +0000 | [diff] [blame] | 86 | struct kref kref; |
| 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | struct capincci *nccip; |
| 89 | unsigned int minor; |
Jan Kiszka | 90926f0 | 2010-02-08 10:12:06 +0000 | [diff] [blame] | 90 | struct dentry *capifs_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | struct capi20_appl *ap; |
| 93 | u32 ncci; |
| 94 | u16 datahandle; |
| 95 | u16 msgid; |
| 96 | |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 97 | struct tty_port port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | int ttyinstop; |
| 99 | int ttyoutstop; |
| 100 | struct sk_buff *ttyskb; |
| 101 | atomic_t ttyopencount; |
| 102 | |
| 103 | struct sk_buff_head inqueue; |
| 104 | int inbytes; |
| 105 | struct sk_buff_head outqueue; |
| 106 | int outbytes; |
| 107 | |
| 108 | /* transmit path */ |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 109 | struct list_head ackqueue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | int nack; |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 111 | spinlock_t ackqlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 114 | /* FIXME: The following lock is a sledgehammer-workaround to a |
| 115 | * locking issue with the capiminor (and maybe other) data structure(s). |
| 116 | * Access to this data is done in a racy way and crashes the machine with |
| 117 | * a FritzCard DSL driver; sooner or later. This is a workaround |
| 118 | * which trades scalability vs stability, so it doesn't crash the kernel anymore. |
| 119 | * The correct (and scalable) fix for the issue seems to require |
| 120 | * an API change to the drivers... . */ |
| 121 | static DEFINE_SPINLOCK(workaround_lock); |
| 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | struct capincci { |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 124 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | u32 ncci; |
| 126 | struct capidev *cdev; |
| 127 | #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE |
| 128 | struct capiminor *minorp; |
| 129 | #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ |
| 130 | }; |
| 131 | |
| 132 | struct capidev { |
| 133 | struct list_head list; |
| 134 | struct capi20_appl ap; |
| 135 | u16 errcode; |
| 136 | unsigned userflags; |
| 137 | |
| 138 | struct sk_buff_head recvqueue; |
| 139 | wait_queue_head_t recvwait; |
| 140 | |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 141 | struct list_head nccis; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 143 | struct mutex lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | /* -------- global variables ---------------------------------------- */ |
| 147 | |
Jan Kiszka | b8f433d | 2010-02-08 10:12:17 +0000 | [diff] [blame] | 148 | static DEFINE_MUTEX(capidev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | static LIST_HEAD(capidev_list); |
| 150 | |
| 151 | #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 152 | |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 153 | static DEFINE_RWLOCK(capiminors_lock); |
| 154 | static struct capiminor **capiminors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Jan Kiszka | 40fb2d0 | 2010-02-08 10:12:25 +0000 | [diff] [blame] | 156 | static struct tty_driver *capinc_tty_driver; |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | /* -------- datahandles --------------------------------------------- */ |
| 159 | |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 160 | static int capiminor_add_ack(struct capiminor *mp, u16 datahandle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 162 | struct datahandle_queue *n; |
| 163 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | n = kmalloc(sizeof(*n), GFP_ATOMIC); |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 166 | if (unlikely(!n)) { |
| 167 | printk(KERN_ERR "capi: alloc datahandle failed\n"); |
| 168 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | n->datahandle = datahandle; |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 171 | INIT_LIST_HEAD(&n->list); |
| 172 | spin_lock_irqsave(&mp->ackqlock, flags); |
| 173 | list_add_tail(&n->list, &mp->ackqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | mp->nack++; |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 175 | spin_unlock_irqrestore(&mp->ackqlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static int capiminor_del_ack(struct capiminor *mp, u16 datahandle) |
| 180 | { |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 181 | struct datahandle_queue *p, *tmp; |
| 182 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 184 | spin_lock_irqsave(&mp->ackqlock, flags); |
| 185 | list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) { |
| 186 | if (p->datahandle == datahandle) { |
| 187 | list_del(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | kfree(p); |
| 189 | mp->nack--; |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 190 | spin_unlock_irqrestore(&mp->ackqlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | } |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 194 | spin_unlock_irqrestore(&mp->ackqlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | static void capiminor_del_all_ack(struct capiminor *mp) |
| 199 | { |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 200 | struct datahandle_queue *p, *tmp; |
| 201 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 203 | spin_lock_irqsave(&mp->ackqlock, flags); |
| 204 | list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) { |
| 205 | list_del(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | kfree(p); |
| 207 | mp->nack--; |
| 208 | } |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 209 | spin_unlock_irqrestore(&mp->ackqlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | |
| 213 | /* -------- struct capiminor ---------------------------------------- */ |
| 214 | |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 215 | static const struct tty_port_operations capiminor_port_ops; /* we have none */ |
| 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci) |
| 218 | { |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 219 | struct capiminor *mp; |
Jan Kiszka | 40fb2d0 | 2010-02-08 10:12:25 +0000 | [diff] [blame] | 220 | struct device *dev; |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 221 | unsigned int minor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | unsigned long flags; |
| 223 | |
Jan Kiszka | 54f0fad | 2010-02-08 10:12:20 +0000 | [diff] [blame] | 224 | mp = kzalloc(sizeof(*mp), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | if (!mp) { |
| 226 | printk(KERN_ERR "capi: can't alloc capiminor\n"); |
| 227 | return NULL; |
| 228 | } |
| 229 | |
Jan Kiszka | 0159d54 | 2010-02-08 10:12:27 +0000 | [diff] [blame] | 230 | kref_init(&mp->kref); |
| 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | mp->ap = ap; |
| 233 | mp->ncci = ncci; |
| 234 | mp->msgid = 0; |
| 235 | atomic_set(&mp->ttyopencount,0); |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 236 | INIT_LIST_HEAD(&mp->ackqueue); |
| 237 | spin_lock_init(&mp->ackqlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
| 239 | skb_queue_head_init(&mp->inqueue); |
| 240 | skb_queue_head_init(&mp->outqueue); |
| 241 | |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 242 | tty_port_init(&mp->port); |
| 243 | mp->port.ops = &capiminor_port_ops; |
| 244 | |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 245 | /* Allocate the least unused minor number. */ |
| 246 | write_lock_irqsave(&capiminors_lock, flags); |
| 247 | for (minor = 0; minor < capi_ttyminors; minor++) |
| 248 | if (!capiminors[minor]) { |
| 249 | capiminors[minor] = mp; |
| 250 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 252 | write_unlock_irqrestore(&capiminors_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 254 | if (minor == capi_ttyminors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | printk(KERN_NOTICE "capi: out of minors\n"); |
Jan Kiszka | 40fb2d0 | 2010-02-08 10:12:25 +0000 | [diff] [blame] | 256 | goto err_out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 259 | mp->minor = minor; |
| 260 | |
Jan Kiszka | 40fb2d0 | 2010-02-08 10:12:25 +0000 | [diff] [blame] | 261 | dev = tty_register_device(capinc_tty_driver, minor, NULL); |
| 262 | if (IS_ERR(dev)) |
| 263 | goto err_out2; |
| 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | return mp; |
Jan Kiszka | 40fb2d0 | 2010-02-08 10:12:25 +0000 | [diff] [blame] | 266 | |
| 267 | err_out2: |
| 268 | write_lock_irqsave(&capiminors_lock, flags); |
| 269 | capiminors[minor] = NULL; |
| 270 | write_unlock_irqrestore(&capiminors_lock, flags); |
| 271 | |
| 272 | err_out1: |
| 273 | kfree(mp); |
| 274 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Jan Kiszka | 0159d54 | 2010-02-08 10:12:27 +0000 | [diff] [blame] | 277 | static void capiminor_destroy(struct kref *kref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | { |
Jan Kiszka | 0159d54 | 2010-02-08 10:12:27 +0000 | [diff] [blame] | 279 | struct capiminor *mp = container_of(kref, struct capiminor, kref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
Wei Yongjun | d46604e | 2009-02-25 00:12:09 +0000 | [diff] [blame] | 281 | kfree_skb(mp->ttyskb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | skb_queue_purge(&mp->inqueue); |
| 283 | skb_queue_purge(&mp->outqueue); |
| 284 | capiminor_del_all_ack(mp); |
| 285 | kfree(mp); |
| 286 | } |
| 287 | |
Jan Kiszka | 40fb2d0 | 2010-02-08 10:12:25 +0000 | [diff] [blame] | 288 | static struct capiminor *capiminor_get(unsigned int minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 290 | struct capiminor *mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 292 | read_lock(&capiminors_lock); |
| 293 | mp = capiminors[minor]; |
Jan Kiszka | 0159d54 | 2010-02-08 10:12:27 +0000 | [diff] [blame] | 294 | if (mp) |
| 295 | kref_get(&mp->kref); |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 296 | read_unlock(&capiminors_lock); |
| 297 | |
| 298 | return mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Jan Kiszka | 0159d54 | 2010-02-08 10:12:27 +0000 | [diff] [blame] | 301 | static inline void capiminor_put(struct capiminor *mp) |
| 302 | { |
| 303 | kref_put(&mp->kref, capiminor_destroy); |
| 304 | } |
| 305 | |
| 306 | static void capiminor_free(struct capiminor *mp) |
| 307 | { |
| 308 | unsigned long flags; |
| 309 | |
| 310 | tty_unregister_device(capinc_tty_driver, mp->minor); |
| 311 | |
| 312 | write_lock_irqsave(&capiminors_lock, flags); |
| 313 | capiminors[mp->minor] = NULL; |
| 314 | write_unlock_irqrestore(&capiminors_lock, flags); |
| 315 | |
| 316 | capiminor_put(mp); |
| 317 | } |
| 318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | /* -------- struct capincci ----------------------------------------- */ |
| 320 | |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 321 | static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | { |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 323 | struct capiminor *mp; |
Jan Kiszka | e95ac14 | 2010-02-08 10:12:26 +0000 | [diff] [blame] | 324 | dev_t device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 326 | if (!(cdev->userflags & CAPIFLAG_HIGHJACKING)) |
| 327 | return; |
| 328 | |
| 329 | mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | if (mp) { |
| 331 | mp->nccip = np; |
| 332 | #ifdef _DEBUG_REFCOUNT |
| 333 | printk(KERN_DEBUG "set mp->nccip\n"); |
| 334 | #endif |
Jan Kiszka | e95ac14 | 2010-02-08 10:12:26 +0000 | [diff] [blame] | 335 | device = MKDEV(capinc_tty_driver->major, mp->minor); |
| 336 | mp->capifs_dentry = capifs_new_ncci(mp->minor, device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static void capincci_free_minor(struct capincci *np) |
| 341 | { |
| 342 | struct capiminor *mp = np->minorp; |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 343 | struct tty_struct *tty; |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 344 | |
| 345 | if (mp) { |
| 346 | capifs_free_ncci(mp->capifs_dentry); |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 347 | |
| 348 | tty = tty_port_tty_get(&mp->port); |
| 349 | if (tty) { |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 350 | mp->nccip = NULL; |
| 351 | #ifdef _DEBUG_REFCOUNT |
| 352 | printk(KERN_DEBUG "reset mp->nccip\n"); |
| 353 | #endif |
Jan Kiszka | 30bced9 | 2010-02-08 10:12:31 +0000 | [diff] [blame^] | 354 | tty_vhangup(tty); |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 355 | tty_kref_put(tty); |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 356 | } |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 357 | |
| 358 | capiminor_free(mp); |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
| 362 | static inline unsigned int capincci_minor_opencount(struct capincci *np) |
| 363 | { |
| 364 | struct capiminor *mp = np->minorp; |
| 365 | |
| 366 | return mp ? atomic_read(&mp->ttyopencount) : 0; |
| 367 | } |
| 368 | |
| 369 | #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */ |
| 370 | |
| 371 | static inline void |
| 372 | capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { } |
| 373 | static inline void capincci_free_minor(struct capincci *np) { } |
| 374 | |
| 375 | static inline unsigned int capincci_minor_opencount(struct capincci *np) |
| 376 | { |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */ |
| 381 | |
| 382 | static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci) |
| 383 | { |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 384 | struct capincci *np; |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 385 | |
Jan Kiszka | 54f0fad | 2010-02-08 10:12:20 +0000 | [diff] [blame] | 386 | np = kzalloc(sizeof(*np), GFP_KERNEL); |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 387 | if (!np) |
| 388 | return NULL; |
| 389 | np->ncci = ncci; |
| 390 | np->cdev = cdev; |
| 391 | |
| 392 | capincci_alloc_minor(cdev, np); |
| 393 | |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 394 | list_add_tail(&np->list, &cdev->nccis); |
| 395 | |
| 396 | return np; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | static void capincci_free(struct capidev *cdev, u32 ncci) |
| 400 | { |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 401 | struct capincci *np, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 403 | list_for_each_entry_safe(np, tmp, &cdev->nccis, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | if (ncci == 0xffffffff || np->ncci == ncci) { |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 405 | capincci_free_minor(np); |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 406 | list_del(&np->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | kfree(np); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | static struct capincci *capincci_find(struct capidev *cdev, u32 ncci) |
| 412 | { |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 413 | struct capincci *np; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 415 | list_for_each_entry(np, &cdev->nccis, list) |
| 416 | if (np->ncci == ncci) |
| 417 | return np; |
| 418 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE |
| 422 | /* -------- handle data queue --------------------------------------- */ |
| 423 | |
| 424 | static struct sk_buff * |
| 425 | gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb) |
| 426 | { |
| 427 | struct sk_buff *nskb; |
| 428 | nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC); |
| 429 | if (nskb) { |
| 430 | u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2); |
| 431 | unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN); |
| 432 | capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN); |
| 433 | capimsg_setu16(s, 2, mp->ap->applid); |
| 434 | capimsg_setu8 (s, 4, CAPI_DATA_B3); |
| 435 | capimsg_setu8 (s, 5, CAPI_RESP); |
| 436 | capimsg_setu16(s, 6, mp->msgid++); |
| 437 | capimsg_setu32(s, 8, mp->ncci); |
| 438 | capimsg_setu16(s, 12, datahandle); |
| 439 | } |
| 440 | return nskb; |
| 441 | } |
| 442 | |
| 443 | static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb) |
| 444 | { |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 445 | struct tty_struct *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | struct sk_buff *nskb; |
| 447 | int datalen; |
| 448 | u16 errcode, datahandle; |
| 449 | struct tty_ldisc *ld; |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 450 | int ret = -1; |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | datalen = skb->len - CAPIMSG_LEN(skb->data); |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 453 | |
| 454 | tty = tty_port_tty_get(&mp->port); |
| 455 | if (!tty) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | #ifdef _DEBUG_DATAFLOW |
| 457 | printk(KERN_DEBUG "capi: currently no receiver\n"); |
| 458 | #endif |
| 459 | return -1; |
| 460 | } |
| 461 | |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 462 | ld = tty_ldisc_ref(tty); |
| 463 | if (!ld) |
| 464 | goto out1; |
| 465 | |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 466 | if (ld->ops->receive_buf == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS) |
| 468 | printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n"); |
| 469 | #endif |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 470 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | } |
| 472 | if (mp->ttyinstop) { |
| 473 | #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS) |
| 474 | printk(KERN_DEBUG "capi: recv tty throttled\n"); |
| 475 | #endif |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 476 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 478 | if (tty->receive_room < datalen) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS) |
| 480 | printk(KERN_DEBUG "capi: no room in tty\n"); |
| 481 | #endif |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 482 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | } |
Harvey Harrison | 2f9e9b6 | 2008-04-28 02:14:37 -0700 | [diff] [blame] | 484 | if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | printk(KERN_ERR "capi: gen_data_b3_resp failed\n"); |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 486 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
| 488 | datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4); |
| 489 | errcode = capi20_put_message(mp->ap, nskb); |
| 490 | if (errcode != CAPI_NOERROR) { |
| 491 | printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n", |
| 492 | errcode); |
| 493 | kfree_skb(nskb); |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 494 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | } |
| 496 | (void)skb_pull(skb, CAPIMSG_LEN(skb->data)); |
| 497 | #ifdef _DEBUG_DATAFLOW |
| 498 | printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n", |
| 499 | datahandle, skb->len); |
| 500 | #endif |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 501 | ld->ops->receive_buf(tty, skb->data, NULL, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | kfree_skb(skb); |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 503 | ret = 0; |
| 504 | out2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | tty_ldisc_deref(ld); |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 506 | out1: |
| 507 | tty_kref_put(tty); |
| 508 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | static void handle_minor_recv(struct capiminor *mp) |
| 512 | { |
| 513 | struct sk_buff *skb; |
Harvey Harrison | 2f9e9b6 | 2008-04-28 02:14:37 -0700 | [diff] [blame] | 514 | while ((skb = skb_dequeue(&mp->inqueue)) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | unsigned int len = skb->len; |
| 516 | mp->inbytes -= len; |
| 517 | if (handle_recv_skb(mp, skb) < 0) { |
| 518 | skb_queue_head(&mp->inqueue, skb); |
| 519 | mp->inbytes += len; |
| 520 | return; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | static int handle_minor_send(struct capiminor *mp) |
| 526 | { |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 527 | struct tty_struct *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | struct sk_buff *skb; |
| 529 | u16 len; |
| 530 | int count = 0; |
| 531 | u16 errcode; |
| 532 | u16 datahandle; |
| 533 | |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 534 | tty = tty_port_tty_get(&mp->port); |
| 535 | if (!tty) |
| 536 | return 0; |
| 537 | |
| 538 | if (mp->ttyoutstop) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS) |
| 540 | printk(KERN_DEBUG "capi: send: tty stopped\n"); |
| 541 | #endif |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 542 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | return 0; |
| 544 | } |
| 545 | |
Harvey Harrison | 2f9e9b6 | 2008-04-28 02:14:37 -0700 | [diff] [blame] | 546 | while ((skb = skb_dequeue(&mp->outqueue)) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | datahandle = mp->datahandle; |
| 548 | len = (u16)skb->len; |
| 549 | skb_push(skb, CAPI_DATA_B3_REQ_LEN); |
| 550 | memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN); |
| 551 | capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN); |
| 552 | capimsg_setu16(skb->data, 2, mp->ap->applid); |
| 553 | capimsg_setu8 (skb->data, 4, CAPI_DATA_B3); |
| 554 | capimsg_setu8 (skb->data, 5, CAPI_REQ); |
| 555 | capimsg_setu16(skb->data, 6, mp->msgid++); |
| 556 | capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */ |
Andrew Morton | 5e6c20a | 2007-07-17 04:04:22 -0700 | [diff] [blame] | 557 | capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | capimsg_setu16(skb->data, 16, len); /* Data length */ |
| 559 | capimsg_setu16(skb->data, 18, datahandle); |
| 560 | capimsg_setu16(skb->data, 20, 0); /* Flags */ |
| 561 | |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 562 | if (capiminor_add_ack(mp, datahandle) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | skb_pull(skb, CAPI_DATA_B3_REQ_LEN); |
| 564 | skb_queue_head(&mp->outqueue, skb); |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 565 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | return count; |
| 567 | } |
| 568 | errcode = capi20_put_message(mp->ap, skb); |
| 569 | if (errcode == CAPI_NOERROR) { |
| 570 | mp->datahandle++; |
| 571 | count++; |
| 572 | mp->outbytes -= len; |
| 573 | #ifdef _DEBUG_DATAFLOW |
| 574 | printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n", |
| 575 | datahandle, len); |
| 576 | #endif |
| 577 | continue; |
| 578 | } |
| 579 | capiminor_del_ack(mp, datahandle); |
| 580 | |
| 581 | if (errcode == CAPI_SENDQUEUEFULL) { |
| 582 | skb_pull(skb, CAPI_DATA_B3_REQ_LEN); |
| 583 | skb_queue_head(&mp->outqueue, skb); |
| 584 | break; |
| 585 | } |
| 586 | |
| 587 | /* ups, drop packet */ |
| 588 | printk(KERN_ERR "capi: put_message = %x\n", errcode); |
| 589 | mp->outbytes -= len; |
| 590 | kfree_skb(skb); |
| 591 | } |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 592 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | return count; |
| 594 | } |
| 595 | |
| 596 | #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ |
| 597 | /* -------- function called by lower level -------------------------- */ |
| 598 | |
| 599 | static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb) |
| 600 | { |
| 601 | struct capidev *cdev = ap->private; |
| 602 | #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 603 | struct tty_struct *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | struct capiminor *mp; |
| 605 | u16 datahandle; |
| 606 | #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ |
| 607 | struct capincci *np; |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 608 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 610 | mutex_lock(&cdev->lock); |
| 611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) { |
| 613 | u16 info = CAPIMSG_U16(skb->data, 12); // Info field |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 614 | if ((info & 0xff00) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | capincci_alloc(cdev, CAPIMSG_NCCI(skb->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | } |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 617 | if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | capincci_alloc(cdev, CAPIMSG_NCCI(skb->data)); |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 619 | |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 620 | spin_lock_irqsave(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) { |
| 622 | skb_queue_tail(&cdev->recvqueue, skb); |
| 623 | wake_up_interruptible(&cdev->recvwait); |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 624 | goto unlock_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 626 | |
| 627 | np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | if (!np) { |
| 629 | printk(KERN_ERR "BUG: capi_signal: ncci not found\n"); |
| 630 | skb_queue_tail(&cdev->recvqueue, skb); |
| 631 | wake_up_interruptible(&cdev->recvwait); |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 632 | goto unlock_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE |
| 636 | skb_queue_tail(&cdev->recvqueue, skb); |
| 637 | wake_up_interruptible(&cdev->recvwait); |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */ |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | mp = np->minorp; |
| 642 | if (!mp) { |
| 643 | skb_queue_tail(&cdev->recvqueue, skb); |
| 644 | wake_up_interruptible(&cdev->recvwait); |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 645 | goto unlock_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2); |
| 649 | #ifdef _DEBUG_DATAFLOW |
| 650 | printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n", |
| 651 | datahandle, skb->len-CAPIMSG_LEN(skb->data)); |
| 652 | #endif |
| 653 | skb_queue_tail(&mp->inqueue, skb); |
| 654 | mp->inbytes += skb->len; |
| 655 | handle_minor_recv(mp); |
| 656 | |
| 657 | } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) { |
| 658 | |
| 659 | datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4); |
| 660 | #ifdef _DEBUG_DATAFLOW |
| 661 | printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n", |
| 662 | datahandle, |
| 663 | CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2)); |
| 664 | #endif |
| 665 | kfree_skb(skb); |
| 666 | (void)capiminor_del_ack(mp, datahandle); |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 667 | tty = tty_port_tty_get(&mp->port); |
| 668 | if (tty) { |
| 669 | tty_wakeup(tty); |
| 670 | tty_kref_put(tty); |
| 671 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | (void)handle_minor_send(mp); |
| 673 | |
| 674 | } else { |
| 675 | /* ups, let capi application handle it :-) */ |
| 676 | skb_queue_tail(&cdev->recvqueue, skb); |
| 677 | wake_up_interruptible(&cdev->recvwait); |
| 678 | } |
| 679 | #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 680 | |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 681 | unlock_out: |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 682 | spin_unlock_irqrestore(&workaround_lock, flags); |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 683 | mutex_unlock(&cdev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /* -------- file_operations for capidev ----------------------------- */ |
| 687 | |
| 688 | static ssize_t |
| 689 | capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 690 | { |
| 691 | struct capidev *cdev = (struct capidev *)file->private_data; |
| 692 | struct sk_buff *skb; |
| 693 | size_t copied; |
Jan Kiszka | 28a1dbb | 2010-02-08 10:12:21 +0000 | [diff] [blame] | 694 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | |
| 696 | if (!cdev->ap.applid) |
| 697 | return -ENODEV; |
| 698 | |
Jan Kiszka | 28a1dbb | 2010-02-08 10:12:21 +0000 | [diff] [blame] | 699 | skb = skb_dequeue(&cdev->recvqueue); |
| 700 | if (!skb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | if (file->f_flags & O_NONBLOCK) |
| 702 | return -EAGAIN; |
Jan Kiszka | 28a1dbb | 2010-02-08 10:12:21 +0000 | [diff] [blame] | 703 | err = wait_event_interruptible(cdev->recvwait, |
| 704 | (skb = skb_dequeue(&cdev->recvqueue))); |
| 705 | if (err) |
| 706 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | } |
| 708 | if (skb->len > count) { |
| 709 | skb_queue_head(&cdev->recvqueue, skb); |
| 710 | return -EMSGSIZE; |
| 711 | } |
| 712 | if (copy_to_user(buf, skb->data, skb->len)) { |
| 713 | skb_queue_head(&cdev->recvqueue, skb); |
| 714 | return -EFAULT; |
| 715 | } |
| 716 | copied = skb->len; |
| 717 | |
| 718 | kfree_skb(skb); |
| 719 | |
| 720 | return copied; |
| 721 | } |
| 722 | |
| 723 | static ssize_t |
| 724 | capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) |
| 725 | { |
| 726 | struct capidev *cdev = (struct capidev *)file->private_data; |
| 727 | struct sk_buff *skb; |
| 728 | u16 mlen; |
| 729 | |
| 730 | if (!cdev->ap.applid) |
| 731 | return -ENODEV; |
| 732 | |
| 733 | skb = alloc_skb(count, GFP_USER); |
| 734 | if (!skb) |
| 735 | return -ENOMEM; |
| 736 | |
| 737 | if (copy_from_user(skb_put(skb, count), buf, count)) { |
| 738 | kfree_skb(skb); |
| 739 | return -EFAULT; |
| 740 | } |
| 741 | mlen = CAPIMSG_LEN(skb->data); |
| 742 | if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) { |
| 743 | if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) { |
| 744 | kfree_skb(skb); |
| 745 | return -EINVAL; |
| 746 | } |
| 747 | } else { |
| 748 | if (mlen != count) { |
| 749 | kfree_skb(skb); |
| 750 | return -EINVAL; |
| 751 | } |
| 752 | } |
| 753 | CAPIMSG_SETAPPID(skb->data, cdev->ap.applid); |
| 754 | |
| 755 | if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) { |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 756 | mutex_lock(&cdev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | capincci_free(cdev, CAPIMSG_NCCI(skb->data)); |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 758 | mutex_unlock(&cdev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | cdev->errcode = capi20_put_message(&cdev->ap, skb); |
| 762 | |
| 763 | if (cdev->errcode) { |
| 764 | kfree_skb(skb); |
| 765 | return -EIO; |
| 766 | } |
| 767 | return count; |
| 768 | } |
| 769 | |
| 770 | static unsigned int |
| 771 | capi_poll(struct file *file, poll_table * wait) |
| 772 | { |
| 773 | struct capidev *cdev = (struct capidev *)file->private_data; |
| 774 | unsigned int mask = 0; |
| 775 | |
| 776 | if (!cdev->ap.applid) |
| 777 | return POLLERR; |
| 778 | |
| 779 | poll_wait(file, &(cdev->recvwait), wait); |
| 780 | mask = POLLOUT | POLLWRNORM; |
| 781 | if (!skb_queue_empty(&cdev->recvqueue)) |
| 782 | mask |= POLLIN | POLLRDNORM; |
| 783 | return mask; |
| 784 | } |
| 785 | |
| 786 | static int |
| 787 | capi_ioctl(struct inode *inode, struct file *file, |
| 788 | unsigned int cmd, unsigned long arg) |
| 789 | { |
| 790 | struct capidev *cdev = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | capi_ioctl_struct data; |
| 792 | int retval = -EINVAL; |
| 793 | void __user *argp = (void __user *)arg; |
| 794 | |
| 795 | switch (cmd) { |
| 796 | case CAPI_REGISTER: |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 797 | mutex_lock(&cdev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 799 | if (cdev->ap.applid) { |
| 800 | retval = -EEXIST; |
| 801 | goto register_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | } |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 803 | if (copy_from_user(&cdev->ap.rparam, argp, |
| 804 | sizeof(struct capi_register_params))) { |
| 805 | retval = -EFAULT; |
| 806 | goto register_out; |
| 807 | } |
| 808 | cdev->ap.private = cdev; |
| 809 | cdev->ap.recv_message = capi_recv_message; |
| 810 | cdev->errcode = capi20_register(&cdev->ap); |
| 811 | retval = (int)cdev->ap.applid; |
| 812 | if (cdev->errcode) { |
| 813 | cdev->ap.applid = 0; |
| 814 | retval = -EIO; |
| 815 | } |
| 816 | |
| 817 | register_out: |
| 818 | mutex_unlock(&cdev->lock); |
| 819 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
| 821 | case CAPI_GET_VERSION: |
| 822 | { |
| 823 | if (copy_from_user(&data.contr, argp, |
| 824 | sizeof(data.contr))) |
| 825 | return -EFAULT; |
| 826 | cdev->errcode = capi20_get_version(data.contr, &data.version); |
| 827 | if (cdev->errcode) |
| 828 | return -EIO; |
| 829 | if (copy_to_user(argp, &data.version, |
| 830 | sizeof(data.version))) |
| 831 | return -EFAULT; |
| 832 | } |
| 833 | return 0; |
| 834 | |
| 835 | case CAPI_GET_SERIAL: |
| 836 | { |
| 837 | if (copy_from_user(&data.contr, argp, |
| 838 | sizeof(data.contr))) |
| 839 | return -EFAULT; |
| 840 | cdev->errcode = capi20_get_serial (data.contr, data.serial); |
| 841 | if (cdev->errcode) |
| 842 | return -EIO; |
| 843 | if (copy_to_user(argp, data.serial, |
| 844 | sizeof(data.serial))) |
| 845 | return -EFAULT; |
| 846 | } |
| 847 | return 0; |
| 848 | case CAPI_GET_PROFILE: |
| 849 | { |
| 850 | if (copy_from_user(&data.contr, argp, |
| 851 | sizeof(data.contr))) |
| 852 | return -EFAULT; |
| 853 | |
| 854 | if (data.contr == 0) { |
| 855 | cdev->errcode = capi20_get_profile(data.contr, &data.profile); |
| 856 | if (cdev->errcode) |
| 857 | return -EIO; |
| 858 | |
| 859 | retval = copy_to_user(argp, |
| 860 | &data.profile.ncontroller, |
| 861 | sizeof(data.profile.ncontroller)); |
| 862 | |
| 863 | } else { |
| 864 | cdev->errcode = capi20_get_profile(data.contr, &data.profile); |
| 865 | if (cdev->errcode) |
| 866 | return -EIO; |
| 867 | |
| 868 | retval = copy_to_user(argp, &data.profile, |
| 869 | sizeof(data.profile)); |
| 870 | } |
| 871 | if (retval) |
| 872 | return -EFAULT; |
| 873 | } |
| 874 | return 0; |
| 875 | |
| 876 | case CAPI_GET_MANUFACTURER: |
| 877 | { |
| 878 | if (copy_from_user(&data.contr, argp, |
| 879 | sizeof(data.contr))) |
| 880 | return -EFAULT; |
| 881 | cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer); |
| 882 | if (cdev->errcode) |
| 883 | return -EIO; |
| 884 | |
| 885 | if (copy_to_user(argp, data.manufacturer, |
| 886 | sizeof(data.manufacturer))) |
| 887 | return -EFAULT; |
| 888 | |
| 889 | } |
| 890 | return 0; |
| 891 | case CAPI_GET_ERRCODE: |
| 892 | data.errcode = cdev->errcode; |
| 893 | cdev->errcode = CAPI_NOERROR; |
| 894 | if (arg) { |
| 895 | if (copy_to_user(argp, &data.errcode, |
| 896 | sizeof(data.errcode))) |
| 897 | return -EFAULT; |
| 898 | } |
| 899 | return data.errcode; |
| 900 | |
| 901 | case CAPI_INSTALLED: |
| 902 | if (capi20_isinstalled() == CAPI_NOERROR) |
| 903 | return 0; |
| 904 | return -ENXIO; |
| 905 | |
| 906 | case CAPI_MANUFACTURER_CMD: |
| 907 | { |
| 908 | struct capi_manufacturer_cmd mcmd; |
| 909 | if (!capable(CAP_SYS_ADMIN)) |
| 910 | return -EPERM; |
| 911 | if (copy_from_user(&mcmd, argp, sizeof(mcmd))) |
| 912 | return -EFAULT; |
| 913 | return capi20_manufacturer(mcmd.cmd, mcmd.data); |
| 914 | } |
| 915 | return 0; |
| 916 | |
| 917 | case CAPI_SET_FLAGS: |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 918 | case CAPI_CLR_FLAGS: { |
| 919 | unsigned userflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 921 | if (copy_from_user(&userflags, argp, sizeof(userflags))) |
| 922 | return -EFAULT; |
| 923 | |
| 924 | mutex_lock(&cdev->lock); |
| 925 | if (cmd == CAPI_SET_FLAGS) |
| 926 | cdev->userflags |= userflags; |
| 927 | else |
| 928 | cdev->userflags &= ~userflags; |
| 929 | mutex_unlock(&cdev->lock); |
| 930 | return 0; |
| 931 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | case CAPI_GET_FLAGS: |
| 933 | if (copy_to_user(argp, &cdev->userflags, |
| 934 | sizeof(cdev->userflags))) |
| 935 | return -EFAULT; |
| 936 | return 0; |
| 937 | |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 938 | case CAPI_NCCI_OPENCOUNT: { |
| 939 | struct capincci *nccip; |
| 940 | unsigned ncci; |
| 941 | int count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 943 | if (copy_from_user(&ncci, argp, sizeof(ncci))) |
| 944 | return -EFAULT; |
| 945 | |
| 946 | mutex_lock(&cdev->lock); |
| 947 | nccip = capincci_find(cdev, (u32)ncci); |
| 948 | if (nccip) |
| 949 | count = capincci_minor_opencount(nccip); |
| 950 | mutex_unlock(&cdev->lock); |
| 951 | return count; |
| 952 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
| 954 | #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 955 | case CAPI_NCCI_GETUNIT: { |
| 956 | struct capincci *nccip; |
| 957 | struct capiminor *mp; |
| 958 | unsigned ncci; |
| 959 | int unit = -ESRCH; |
| 960 | |
| 961 | if (copy_from_user(&ncci, argp, sizeof(ncci))) |
| 962 | return -EFAULT; |
| 963 | |
| 964 | mutex_lock(&cdev->lock); |
| 965 | nccip = capincci_find(cdev, (u32)ncci); |
| 966 | if (nccip) { |
| 967 | mp = nccip->minorp; |
| 968 | if (mp) |
| 969 | unit = mp->minor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | } |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 971 | mutex_unlock(&cdev->lock); |
| 972 | return unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | } |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 974 | #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ |
| 975 | |
| 976 | default: |
| 977 | return -EINVAL; |
| 978 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | } |
| 980 | |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 981 | static int capi_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | { |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 983 | struct capidev *cdev; |
| 984 | |
| 985 | cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); |
| 986 | if (!cdev) |
| 987 | return -ENOMEM; |
| 988 | |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 989 | mutex_init(&cdev->lock); |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 990 | skb_queue_head_init(&cdev->recvqueue); |
| 991 | init_waitqueue_head(&cdev->recvwait); |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 992 | INIT_LIST_HEAD(&cdev->nccis); |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 993 | file->private_data = cdev; |
| 994 | |
| 995 | mutex_lock(&capidev_list_lock); |
| 996 | list_add_tail(&cdev->list, &capidev_list); |
| 997 | mutex_unlock(&capidev_list_lock); |
| 998 | |
| 999 | return nonseekable_open(inode, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 1002 | static int capi_release(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | { |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 1004 | struct capidev *cdev = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 1006 | mutex_lock(&capidev_list_lock); |
| 1007 | list_del(&cdev->list); |
| 1008 | mutex_unlock(&capidev_list_lock); |
| 1009 | |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 1010 | if (cdev->ap.applid) |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 1011 | capi20_release(&cdev->ap); |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 1012 | skb_queue_purge(&cdev->recvqueue); |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 1013 | capincci_free(cdev, 0xffffffff); |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 1014 | |
| 1015 | kfree(cdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | return 0; |
| 1017 | } |
| 1018 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 1019 | static const struct file_operations capi_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | { |
| 1021 | .owner = THIS_MODULE, |
| 1022 | .llseek = no_llseek, |
| 1023 | .read = capi_read, |
| 1024 | .write = capi_write, |
| 1025 | .poll = capi_poll, |
| 1026 | .ioctl = capi_ioctl, |
| 1027 | .open = capi_open, |
| 1028 | .release = capi_release, |
| 1029 | }; |
| 1030 | |
| 1031 | #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE |
| 1032 | /* -------- tty_operations for capincci ----------------------------- */ |
| 1033 | |
Jan Kiszka | 4632451 | 2010-02-08 10:12:28 +0000 | [diff] [blame] | 1034 | static int |
| 1035 | capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty) |
| 1036 | { |
| 1037 | int idx = tty->index; |
| 1038 | struct capiminor *mp = capiminor_get(idx); |
| 1039 | int ret = tty_init_termios(tty); |
| 1040 | |
| 1041 | if (ret == 0) { |
| 1042 | tty_driver_kref_get(driver); |
| 1043 | tty->count++; |
| 1044 | tty->driver_data = mp; |
| 1045 | driver->ttys[idx] = tty; |
| 1046 | } else |
| 1047 | capiminor_put(mp); |
| 1048 | return ret; |
| 1049 | } |
| 1050 | |
| 1051 | static void capinc_tty_cleanup(struct tty_struct *tty) |
| 1052 | { |
| 1053 | struct capiminor *mp = tty->driver_data; |
| 1054 | tty->driver_data = NULL; |
| 1055 | capiminor_put(mp); |
| 1056 | } |
| 1057 | |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 1058 | static int capinc_tty_open(struct tty_struct *tty, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | { |
Jan Kiszka | 4632451 | 2010-02-08 10:12:28 +0000 | [diff] [blame] | 1060 | struct capiminor *mp = tty->driver_data; |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1061 | unsigned long flags; |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 1062 | int err; |
| 1063 | |
| 1064 | err = tty_port_open(&mp->port, tty, filp); |
| 1065 | if (err) |
| 1066 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1068 | spin_lock_irqsave(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | atomic_inc(&mp->ttyopencount); |
| 1070 | #ifdef _DEBUG_REFCOUNT |
| 1071 | printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount)); |
| 1072 | #endif |
| 1073 | handle_minor_recv(mp); |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1074 | spin_unlock_irqrestore(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | return 0; |
| 1076 | } |
| 1077 | |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 1078 | static void capinc_tty_close(struct tty_struct *tty, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | { |
Jan Kiszka | 4632451 | 2010-02-08 10:12:28 +0000 | [diff] [blame] | 1080 | struct capiminor *mp = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | if (atomic_dec_and_test(&mp->ttyopencount)) { |
| 1083 | #ifdef _DEBUG_REFCOUNT |
| 1084 | printk(KERN_DEBUG "capinc_tty_close lastclose\n"); |
| 1085 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | } |
| 1087 | #ifdef _DEBUG_REFCOUNT |
| 1088 | printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount)); |
| 1089 | #endif |
Jan Kiszka | 0159d54 | 2010-02-08 10:12:27 +0000 | [diff] [blame] | 1090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | #ifdef _DEBUG_REFCOUNT |
| 1092 | printk(KERN_DEBUG "capinc_tty_close\n"); |
| 1093 | #endif |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 1094 | tty_port_close(&mp->port, tty, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | static int capinc_tty_write(struct tty_struct * tty, |
| 1098 | const unsigned char *buf, int count) |
| 1099 | { |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1100 | struct capiminor *mp = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | struct sk_buff *skb; |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1102 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | |
| 1104 | #ifdef _DEBUG_TTYFUNCS |
| 1105 | printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count); |
| 1106 | #endif |
| 1107 | |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1108 | if (!mp->nccip) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | #ifdef _DEBUG_TTYFUNCS |
| 1110 | printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n"); |
| 1111 | #endif |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1115 | spin_lock_irqsave(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | skb = mp->ttyskb; |
| 1117 | if (skb) { |
| 1118 | mp->ttyskb = NULL; |
| 1119 | skb_queue_tail(&mp->outqueue, skb); |
| 1120 | mp->outbytes += skb->len; |
| 1121 | } |
| 1122 | |
| 1123 | skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC); |
| 1124 | if (!skb) { |
| 1125 | printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n"); |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1126 | spin_unlock_irqrestore(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | return -ENOMEM; |
| 1128 | } |
| 1129 | |
| 1130 | skb_reserve(skb, CAPI_DATA_B3_REQ_LEN); |
| 1131 | memcpy(skb_put(skb, count), buf, count); |
| 1132 | |
| 1133 | skb_queue_tail(&mp->outqueue, skb); |
| 1134 | mp->outbytes += skb->len; |
| 1135 | (void)handle_minor_send(mp); |
| 1136 | (void)handle_minor_recv(mp); |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1137 | spin_unlock_irqrestore(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | return count; |
| 1139 | } |
| 1140 | |
Alan Cox | f2545a7 | 2008-04-30 00:54:09 -0700 | [diff] [blame] | 1141 | static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | { |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1143 | struct capiminor *mp = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | struct sk_buff *skb; |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1145 | unsigned long flags; |
Alan Cox | f2545a7 | 2008-04-30 00:54:09 -0700 | [diff] [blame] | 1146 | int ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | |
| 1148 | #ifdef _DEBUG_TTYFUNCS |
| 1149 | printk(KERN_DEBUG "capinc_put_char(%u)\n", ch); |
| 1150 | #endif |
| 1151 | |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1152 | if (!mp->nccip) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | #ifdef _DEBUG_TTYFUNCS |
| 1154 | printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n"); |
| 1155 | #endif |
Alan Cox | f2545a7 | 2008-04-30 00:54:09 -0700 | [diff] [blame] | 1156 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1159 | spin_lock_irqsave(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | skb = mp->ttyskb; |
| 1161 | if (skb) { |
| 1162 | if (skb_tailroom(skb) > 0) { |
| 1163 | *(skb_put(skb, 1)) = ch; |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1164 | spin_unlock_irqrestore(&workaround_lock, flags); |
Alan Cox | f2545a7 | 2008-04-30 00:54:09 -0700 | [diff] [blame] | 1165 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | } |
| 1167 | mp->ttyskb = NULL; |
| 1168 | skb_queue_tail(&mp->outqueue, skb); |
| 1169 | mp->outbytes += skb->len; |
| 1170 | (void)handle_minor_send(mp); |
| 1171 | } |
| 1172 | skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC); |
| 1173 | if (skb) { |
| 1174 | skb_reserve(skb, CAPI_DATA_B3_REQ_LEN); |
| 1175 | *(skb_put(skb, 1)) = ch; |
| 1176 | mp->ttyskb = skb; |
| 1177 | } else { |
| 1178 | printk(KERN_ERR "capinc_put_char: char %u lost\n", ch); |
Alan Cox | f2545a7 | 2008-04-30 00:54:09 -0700 | [diff] [blame] | 1179 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | } |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1181 | spin_unlock_irqrestore(&workaround_lock, flags); |
Alan Cox | f2545a7 | 2008-04-30 00:54:09 -0700 | [diff] [blame] | 1182 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | static void capinc_tty_flush_chars(struct tty_struct *tty) |
| 1186 | { |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1187 | struct capiminor *mp = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | struct sk_buff *skb; |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1189 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
| 1191 | #ifdef _DEBUG_TTYFUNCS |
| 1192 | printk(KERN_DEBUG "capinc_tty_flush_chars\n"); |
| 1193 | #endif |
| 1194 | |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1195 | if (!mp->nccip) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | #ifdef _DEBUG_TTYFUNCS |
| 1197 | printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n"); |
| 1198 | #endif |
| 1199 | return; |
| 1200 | } |
| 1201 | |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1202 | spin_lock_irqsave(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | skb = mp->ttyskb; |
| 1204 | if (skb) { |
| 1205 | mp->ttyskb = NULL; |
| 1206 | skb_queue_tail(&mp->outqueue, skb); |
| 1207 | mp->outbytes += skb->len; |
| 1208 | (void)handle_minor_send(mp); |
| 1209 | } |
| 1210 | (void)handle_minor_recv(mp); |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1211 | spin_unlock_irqrestore(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | static int capinc_tty_write_room(struct tty_struct *tty) |
| 1215 | { |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1216 | struct capiminor *mp = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | int room; |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1218 | |
| 1219 | if (!mp->nccip) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | #ifdef _DEBUG_TTYFUNCS |
| 1221 | printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n"); |
| 1222 | #endif |
| 1223 | return 0; |
| 1224 | } |
| 1225 | room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue); |
| 1226 | room *= CAPI_MAX_BLKSIZE; |
| 1227 | #ifdef _DEBUG_TTYFUNCS |
| 1228 | printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room); |
| 1229 | #endif |
| 1230 | return room; |
| 1231 | } |
| 1232 | |
Adrian Bunk | 408b664 | 2005-05-01 08:59:29 -0700 | [diff] [blame] | 1233 | static int capinc_tty_chars_in_buffer(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | { |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1235 | struct capiminor *mp = tty->driver_data; |
| 1236 | |
| 1237 | if (!mp->nccip) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | #ifdef _DEBUG_TTYFUNCS |
| 1239 | printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n"); |
| 1240 | #endif |
| 1241 | return 0; |
| 1242 | } |
| 1243 | #ifdef _DEBUG_TTYFUNCS |
| 1244 | printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n", |
| 1245 | mp->outbytes, mp->nack, |
| 1246 | skb_queue_len(&mp->outqueue), |
| 1247 | skb_queue_len(&mp->inqueue)); |
| 1248 | #endif |
| 1249 | return mp->outbytes; |
| 1250 | } |
| 1251 | |
| 1252 | static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file, |
| 1253 | unsigned int cmd, unsigned long arg) |
| 1254 | { |
| 1255 | int error = 0; |
| 1256 | switch (cmd) { |
| 1257 | default: |
Stephen Rothwell | 53e8631 | 2008-10-13 10:44:33 +0100 | [diff] [blame] | 1258 | error = n_tty_ioctl_helper(tty, file, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | break; |
| 1260 | } |
| 1261 | return error; |
| 1262 | } |
| 1263 | |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 1264 | static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | { |
| 1266 | #ifdef _DEBUG_TTYFUNCS |
| 1267 | printk(KERN_DEBUG "capinc_tty_set_termios\n"); |
| 1268 | #endif |
| 1269 | } |
| 1270 | |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1271 | static void capinc_tty_throttle(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | { |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1273 | struct capiminor *mp = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | #ifdef _DEBUG_TTYFUNCS |
| 1275 | printk(KERN_DEBUG "capinc_tty_throttle\n"); |
| 1276 | #endif |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1277 | mp->ttyinstop = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1280 | static void capinc_tty_unthrottle(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | { |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1282 | struct capiminor *mp = tty->driver_data; |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1283 | unsigned long flags; |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | #ifdef _DEBUG_TTYFUNCS |
| 1286 | printk(KERN_DEBUG "capinc_tty_unthrottle\n"); |
| 1287 | #endif |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1288 | spin_lock_irqsave(&workaround_lock, flags); |
| 1289 | mp->ttyinstop = 0; |
| 1290 | handle_minor_recv(mp); |
| 1291 | spin_unlock_irqrestore(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | static void capinc_tty_stop(struct tty_struct *tty) |
| 1295 | { |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1296 | struct capiminor *mp = tty->driver_data; |
| 1297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | #ifdef _DEBUG_TTYFUNCS |
| 1299 | printk(KERN_DEBUG "capinc_tty_stop\n"); |
| 1300 | #endif |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1301 | mp->ttyoutstop = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | static void capinc_tty_start(struct tty_struct *tty) |
| 1305 | { |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1306 | struct capiminor *mp = tty->driver_data; |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1307 | unsigned long flags; |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | #ifdef _DEBUG_TTYFUNCS |
| 1310 | printk(KERN_DEBUG "capinc_tty_start\n"); |
| 1311 | #endif |
Jan Kiszka | 2c8df72 | 2010-02-08 10:12:30 +0000 | [diff] [blame] | 1312 | spin_lock_irqsave(&workaround_lock, flags); |
| 1313 | mp->ttyoutstop = 0; |
| 1314 | (void)handle_minor_send(mp); |
| 1315 | spin_unlock_irqrestore(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | static void capinc_tty_hangup(struct tty_struct *tty) |
| 1319 | { |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 1320 | struct capiminor *mp = tty->driver_data; |
| 1321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | #ifdef _DEBUG_TTYFUNCS |
| 1323 | printk(KERN_DEBUG "capinc_tty_hangup\n"); |
| 1324 | #endif |
Jan Kiszka | fb4b488 | 2010-02-08 10:12:29 +0000 | [diff] [blame] | 1325 | tty_port_hangup(&mp->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
Alan Cox | 9e98966c | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 1328 | static int capinc_tty_break_ctl(struct tty_struct *tty, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | { |
| 1330 | #ifdef _DEBUG_TTYFUNCS |
| 1331 | printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state); |
| 1332 | #endif |
Alan Cox | 9e98966c | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 1333 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | static void capinc_tty_flush_buffer(struct tty_struct *tty) |
| 1337 | { |
| 1338 | #ifdef _DEBUG_TTYFUNCS |
| 1339 | printk(KERN_DEBUG "capinc_tty_flush_buffer\n"); |
| 1340 | #endif |
| 1341 | } |
| 1342 | |
| 1343 | static void capinc_tty_set_ldisc(struct tty_struct *tty) |
| 1344 | { |
| 1345 | #ifdef _DEBUG_TTYFUNCS |
| 1346 | printk(KERN_DEBUG "capinc_tty_set_ldisc\n"); |
| 1347 | #endif |
| 1348 | } |
| 1349 | |
| 1350 | static void capinc_tty_send_xchar(struct tty_struct *tty, char ch) |
| 1351 | { |
| 1352 | #ifdef _DEBUG_TTYFUNCS |
| 1353 | printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch); |
| 1354 | #endif |
| 1355 | } |
| 1356 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 1357 | static const struct tty_operations capinc_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | .open = capinc_tty_open, |
| 1359 | .close = capinc_tty_close, |
| 1360 | .write = capinc_tty_write, |
| 1361 | .put_char = capinc_tty_put_char, |
| 1362 | .flush_chars = capinc_tty_flush_chars, |
| 1363 | .write_room = capinc_tty_write_room, |
| 1364 | .chars_in_buffer = capinc_tty_chars_in_buffer, |
| 1365 | .ioctl = capinc_tty_ioctl, |
| 1366 | .set_termios = capinc_tty_set_termios, |
| 1367 | .throttle = capinc_tty_throttle, |
| 1368 | .unthrottle = capinc_tty_unthrottle, |
| 1369 | .stop = capinc_tty_stop, |
| 1370 | .start = capinc_tty_start, |
| 1371 | .hangup = capinc_tty_hangup, |
| 1372 | .break_ctl = capinc_tty_break_ctl, |
| 1373 | .flush_buffer = capinc_tty_flush_buffer, |
| 1374 | .set_ldisc = capinc_tty_set_ldisc, |
| 1375 | .send_xchar = capinc_tty_send_xchar, |
Jan Kiszka | 4632451 | 2010-02-08 10:12:28 +0000 | [diff] [blame] | 1376 | .install = capinc_tty_install, |
| 1377 | .cleanup = capinc_tty_cleanup, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | }; |
| 1379 | |
Jan Kiszka | e76b154 | 2010-02-08 10:12:24 +0000 | [diff] [blame] | 1380 | static int __init capinc_tty_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | { |
| 1382 | struct tty_driver *drv; |
Jan Kiszka | e76b154 | 2010-02-08 10:12:24 +0000 | [diff] [blame] | 1383 | int err; |
| 1384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | if (capi_ttyminors > CAPINC_MAX_PORTS) |
| 1386 | capi_ttyminors = CAPINC_MAX_PORTS; |
| 1387 | if (capi_ttyminors <= 0) |
| 1388 | capi_ttyminors = CAPINC_NR_PORTS; |
| 1389 | |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 1390 | capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors, |
| 1391 | GFP_KERNEL); |
| 1392 | if (!capiminors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | return -ENOMEM; |
| 1394 | |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 1395 | drv = alloc_tty_driver(capi_ttyminors); |
| 1396 | if (!drv) { |
| 1397 | kfree(capiminors); |
| 1398 | return -ENOMEM; |
| 1399 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | drv->owner = THIS_MODULE; |
| 1401 | drv->driver_name = "capi_nc"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | drv->name = "capi"; |
Jan Kiszka | e95ac14 | 2010-02-08 10:12:26 +0000 | [diff] [blame] | 1403 | drv->major = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | drv->minor_start = 0; |
| 1405 | drv->type = TTY_DRIVER_TYPE_SERIAL; |
| 1406 | drv->subtype = SERIAL_TYPE_NORMAL; |
| 1407 | drv->init_termios = tty_std_termios; |
| 1408 | drv->init_termios.c_iflag = ICRNL; |
| 1409 | drv->init_termios.c_oflag = OPOST | ONLCR; |
| 1410 | drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; |
| 1411 | drv->init_termios.c_lflag = 0; |
Jan Kiszka | 40fb2d0 | 2010-02-08 10:12:25 +0000 | [diff] [blame] | 1412 | drv->flags = |
| 1413 | TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS | |
| 1414 | TTY_DRIVER_DYNAMIC_DEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | tty_set_operations(drv, &capinc_ops); |
Jan Kiszka | e76b154 | 2010-02-08 10:12:24 +0000 | [diff] [blame] | 1416 | |
| 1417 | err = tty_register_driver(drv); |
| 1418 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | put_tty_driver(drv); |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 1420 | kfree(capiminors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | printk(KERN_ERR "Couldn't register capi_nc driver\n"); |
Jan Kiszka | e76b154 | 2010-02-08 10:12:24 +0000 | [diff] [blame] | 1422 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | } |
| 1424 | capinc_tty_driver = drv; |
| 1425 | return 0; |
| 1426 | } |
| 1427 | |
Jan Kiszka | e76b154 | 2010-02-08 10:12:24 +0000 | [diff] [blame] | 1428 | static void __exit capinc_tty_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | { |
Jan Kiszka | e76b154 | 2010-02-08 10:12:24 +0000 | [diff] [blame] | 1430 | tty_unregister_driver(capinc_tty_driver); |
| 1431 | put_tty_driver(capinc_tty_driver); |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 1432 | kfree(capiminors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | } |
| 1434 | |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 1435 | #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */ |
| 1436 | |
| 1437 | static inline int capinc_tty_init(void) |
| 1438 | { |
| 1439 | return 0; |
| 1440 | } |
| 1441 | |
| 1442 | static inline void capinc_tty_exit(void) { } |
| 1443 | |
| 1444 | #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | |
| 1446 | /* -------- /proc functions ----------------------------------------- */ |
| 1447 | |
| 1448 | /* |
| 1449 | * /proc/capi/capi20: |
| 1450 | * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt |
| 1451 | */ |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 1452 | static int capi20_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | { |
| 1454 | struct capidev *cdev; |
| 1455 | struct list_head *l; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | |
Jan Kiszka | b8f433d | 2010-02-08 10:12:17 +0000 | [diff] [blame] | 1457 | mutex_lock(&capidev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | list_for_each(l, &capidev_list) { |
| 1459 | cdev = list_entry(l, struct capidev, list); |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 1460 | seq_printf(m, "0 %d %lu %lu %lu %lu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | cdev->ap.applid, |
| 1462 | cdev->ap.nrecvctlpkt, |
| 1463 | cdev->ap.nrecvdatapkt, |
| 1464 | cdev->ap.nsentctlpkt, |
| 1465 | cdev->ap.nsentdatapkt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | } |
Jan Kiszka | b8f433d | 2010-02-08 10:12:17 +0000 | [diff] [blame] | 1467 | mutex_unlock(&capidev_list_lock); |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 1468 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | } |
| 1470 | |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 1471 | static int capi20_proc_open(struct inode *inode, struct file *file) |
| 1472 | { |
| 1473 | return single_open(file, capi20_proc_show, NULL); |
| 1474 | } |
| 1475 | |
| 1476 | static const struct file_operations capi20_proc_fops = { |
| 1477 | .owner = THIS_MODULE, |
| 1478 | .open = capi20_proc_open, |
| 1479 | .read = seq_read, |
| 1480 | .llseek = seq_lseek, |
| 1481 | .release = single_release, |
| 1482 | }; |
| 1483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | /* |
| 1485 | * /proc/capi/capi20ncci: |
| 1486 | * applid ncci |
| 1487 | */ |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 1488 | static int capi20ncci_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | { |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 1490 | struct capidev *cdev; |
| 1491 | struct capincci *np; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | |
Jan Kiszka | b8f433d | 2010-02-08 10:12:17 +0000 | [diff] [blame] | 1493 | mutex_lock(&capidev_list_lock); |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 1494 | list_for_each_entry(cdev, &capidev_list, list) { |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 1495 | mutex_lock(&cdev->lock); |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 1496 | list_for_each_entry(np, &cdev->nccis, list) |
| 1497 | seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci); |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 1498 | mutex_unlock(&cdev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | } |
Jan Kiszka | b8f433d | 2010-02-08 10:12:17 +0000 | [diff] [blame] | 1500 | mutex_unlock(&capidev_list_lock); |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 1501 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 1504 | static int capi20ncci_proc_open(struct inode *inode, struct file *file) |
| 1505 | { |
| 1506 | return single_open(file, capi20ncci_proc_show, NULL); |
| 1507 | } |
| 1508 | |
| 1509 | static const struct file_operations capi20ncci_proc_fops = { |
| 1510 | .owner = THIS_MODULE, |
| 1511 | .open = capi20ncci_proc_open, |
| 1512 | .read = seq_read, |
| 1513 | .llseek = seq_lseek, |
| 1514 | .release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | }; |
| 1516 | |
| 1517 | static void __init proc_init(void) |
| 1518 | { |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 1519 | proc_create("capi/capi20", 0, NULL, &capi20_proc_fops); |
| 1520 | proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | static void __exit proc_exit(void) |
| 1524 | { |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 1525 | remove_proc_entry("capi/capi20", NULL); |
| 1526 | remove_proc_entry("capi/capi20ncci", NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | /* -------- init function and module interface ---------------------- */ |
| 1530 | |
| 1531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | static int __init capi_init(void) |
| 1533 | { |
Jan Kiszka | 88549d6 | 2010-02-08 10:12:09 +0000 | [diff] [blame] | 1534 | const char *compileinfo; |
Andrew Morton | 6d9eac3 | 2006-03-28 01:56:19 -0800 | [diff] [blame] | 1535 | int major_ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | |
Andrew Morton | 6d9eac3 | 2006-03-28 01:56:19 -0800 | [diff] [blame] | 1537 | major_ret = register_chrdev(capi_major, "capi20", &capi_fops); |
| 1538 | if (major_ret < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | printk(KERN_ERR "capi20: unable to get major %d\n", capi_major); |
Andrew Morton | 6d9eac3 | 2006-03-28 01:56:19 -0800 | [diff] [blame] | 1540 | return major_ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | } |
gregkh@suse.de | 56b2293 | 2005-03-23 10:01:41 -0800 | [diff] [blame] | 1542 | capi_class = class_create(THIS_MODULE, "capi"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | if (IS_ERR(capi_class)) { |
| 1544 | unregister_chrdev(capi_major, "capi20"); |
| 1545 | return PTR_ERR(capi_class); |
| 1546 | } |
| 1547 | |
Greg Kroah-Hartman | a9b1261 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 1548 | device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | if (capinc_tty_init() < 0) { |
Tony Jones | d78b036 | 2007-09-25 02:03:03 +0200 | [diff] [blame] | 1551 | device_destroy(capi_class, MKDEV(capi_major, 0)); |
gregkh@suse.de | 56b2293 | 2005-03-23 10:01:41 -0800 | [diff] [blame] | 1552 | class_destroy(capi_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | unregister_chrdev(capi_major, "capi20"); |
| 1554 | return -ENOMEM; |
| 1555 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | |
| 1557 | proc_init(); |
| 1558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE) |
| 1560 | compileinfo = " (middleware+capifs)"; |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 1561 | #elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | compileinfo = " (no capifs)"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | #else |
| 1564 | compileinfo = " (no middleware)"; |
| 1565 | #endif |
Jan Kiszka | 88549d6 | 2010-02-08 10:12:09 +0000 | [diff] [blame] | 1566 | printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n", |
| 1567 | capi_major, compileinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | |
| 1569 | return 0; |
| 1570 | } |
| 1571 | |
| 1572 | static void __exit capi_exit(void) |
| 1573 | { |
| 1574 | proc_exit(); |
| 1575 | |
Tony Jones | d78b036 | 2007-09-25 02:03:03 +0200 | [diff] [blame] | 1576 | device_destroy(capi_class, MKDEV(capi_major, 0)); |
gregkh@suse.de | 56b2293 | 2005-03-23 10:01:41 -0800 | [diff] [blame] | 1577 | class_destroy(capi_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | unregister_chrdev(capi_major, "capi20"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | capinc_tty_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | module_init(capi_init); |
| 1584 | module_exit(capi_exit); |