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