Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /*****************************************************************************/ |
| 2 | |
| 3 | /* |
| 4 | * devio.c -- User space communication with USB devices. |
| 5 | * |
| 6 | * Copyright (C) 1999-2000 Thomas Sailer (sailer@ife.ee.ethz.ch) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * This file implements the usbfs/x/y files, where |
| 23 | * x is the bus number and y the device number. |
| 24 | * |
| 25 | * It allows user space programs/"drivers" to communicate directly |
| 26 | * with USB devices without intervening kernel driver. |
| 27 | * |
| 28 | * Revision history |
| 29 | * 22.12.1999 0.1 Initial release (split from proc_usb.c) |
| 30 | * 04.01.2000 0.2 Turned into its own filesystem |
Harald Welte | 4611383 | 2005-10-10 19:44:29 +0200 | [diff] [blame] | 31 | * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery |
| 32 | * (CAN-2005-3055) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | /*****************************************************************************/ |
| 36 | |
| 37 | #include <linux/fs.h> |
| 38 | #include <linux/mm.h> |
| 39 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/signal.h> |
| 41 | #include <linux/poll.h> |
| 42 | #include <linux/module.h> |
| 43 | #include <linux/usb.h> |
| 44 | #include <linux/usbdevice_fs.h> |
Eric Lescouet | 27729aa | 2010-04-24 23:21:52 +0200 | [diff] [blame] | 45 | #include <linux/usb/hcd.h> /* for usbcore internals */ |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 46 | #include <linux/cdev.h> |
Greg Kroah-Hartman | a7b986b | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 47 | #include <linux/notifier.h> |
David Quigley | 7a01955 | 2006-06-30 01:55:48 -0700 | [diff] [blame] | 48 | #include <linux/security.h> |
Serge Hallyn | d178bc3 | 2011-09-26 10:45:18 -0500 | [diff] [blame] | 49 | #include <linux/user_namespace.h> |
Hans de Goede | 3d97ff6 | 2012-07-04 09:18:03 +0200 | [diff] [blame^] | 50 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <asm/uaccess.h> |
| 52 | #include <asm/byteorder.h> |
| 53 | #include <linux/moduleparam.h> |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include "usb.h" |
| 56 | |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 57 | #define USB_MAXBUS 64 |
| 58 | #define USB_DEVICE_MAX USB_MAXBUS * 128 |
Hans de Goede | 3d97ff6 | 2012-07-04 09:18:03 +0200 | [diff] [blame^] | 59 | #define USB_SG_SIZE 16384 /* split-size for large txs */ |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 60 | |
Alan Stern | 4a2a8a2 | 2006-07-01 22:05:01 -0400 | [diff] [blame] | 61 | /* Mutual exclusion for removal, open, and release */ |
| 62 | DEFINE_MUTEX(usbfs_mutex); |
| 63 | |
Alan Stern | cd9f037 | 2008-06-24 14:47:04 -0400 | [diff] [blame] | 64 | struct dev_state { |
| 65 | struct list_head list; /* state list */ |
| 66 | struct usb_device *dev; |
| 67 | struct file *file; |
| 68 | spinlock_t lock; /* protects the async urb lists */ |
| 69 | struct list_head async_pending; |
| 70 | struct list_head async_completed; |
| 71 | wait_queue_head_t wait; /* wake up if a request completed */ |
| 72 | unsigned int discsignr; |
| 73 | struct pid *disc_pid; |
Serge Hallyn | d178bc3 | 2011-09-26 10:45:18 -0500 | [diff] [blame] | 74 | const struct cred *cred; |
Alan Stern | cd9f037 | 2008-06-24 14:47:04 -0400 | [diff] [blame] | 75 | void __user *disccontext; |
| 76 | unsigned long ifclaimed; |
| 77 | u32 secid; |
Alan Stern | 01c6460 | 2009-09-01 11:09:56 -0400 | [diff] [blame] | 78 | u32 disabled_bulk_eps; |
Alan Stern | cd9f037 | 2008-06-24 14:47:04 -0400 | [diff] [blame] | 79 | }; |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | struct async { |
| 82 | struct list_head asynclist; |
| 83 | struct dev_state *ps; |
Eric W. Biederman | 2425c08 | 2006-10-02 02:17:28 -0700 | [diff] [blame] | 84 | struct pid *pid; |
Serge Hallyn | d178bc3 | 2011-09-26 10:45:18 -0500 | [diff] [blame] | 85 | const struct cred *cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | unsigned int signr; |
| 87 | unsigned int ifnum; |
| 88 | void __user *userbuffer; |
| 89 | void __user *userurb; |
| 90 | struct urb *urb; |
Alan Stern | add1aae | 2011-11-17 16:41:25 -0500 | [diff] [blame] | 91 | unsigned int mem_usage; |
Alan Stern | e015268 | 2007-08-24 15:42:52 -0400 | [diff] [blame] | 92 | int status; |
David Quigley | 7a01955 | 2006-06-30 01:55:48 -0700 | [diff] [blame] | 93 | u32 secid; |
Alan Stern | 01c6460 | 2009-09-01 11:09:56 -0400 | [diff] [blame] | 94 | u8 bulk_addr; |
| 95 | u8 bulk_status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 98 | static bool usbfs_snoop; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 99 | module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR); |
| 100 | MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | #define snoop(dev, format, arg...) \ |
| 103 | do { \ |
| 104 | if (usbfs_snoop) \ |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 105 | dev_info(dev , format , ## arg); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } while (0) |
| 107 | |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 108 | enum snoop_when { |
| 109 | SUBMIT, COMPLETE |
| 110 | }; |
| 111 | |
Alan Stern | fad21bd | 2005-08-10 15:15:57 -0400 | [diff] [blame] | 112 | #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0) |
| 113 | |
Alan Stern | add1aae | 2011-11-17 16:41:25 -0500 | [diff] [blame] | 114 | /* Limit on the total amount of memory we can allocate for transfers */ |
Alan Stern | 3f5eb8d | 2011-11-17 16:41:35 -0500 | [diff] [blame] | 115 | static unsigned usbfs_memory_mb = 16; |
| 116 | module_param(usbfs_memory_mb, uint, 0644); |
| 117 | MODULE_PARM_DESC(usbfs_memory_mb, |
| 118 | "maximum MB allowed for usbfs buffers (0 = no limit)"); |
| 119 | |
| 120 | /* Hard limit, necessary to avoid aithmetic overflow */ |
| 121 | #define USBFS_XFER_MAX (UINT_MAX / 2 - 1000000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Alan Stern | add1aae | 2011-11-17 16:41:25 -0500 | [diff] [blame] | 123 | static atomic_t usbfs_memory_usage; /* Total memory currently allocated */ |
| 124 | |
| 125 | /* Check whether it's okay to allocate more memory for a transfer */ |
| 126 | static int usbfs_increase_memory_usage(unsigned amount) |
| 127 | { |
Alan Stern | 3f5eb8d | 2011-11-17 16:41:35 -0500 | [diff] [blame] | 128 | unsigned lim; |
| 129 | |
| 130 | /* |
| 131 | * Convert usbfs_memory_mb to bytes, avoiding overflows. |
| 132 | * 0 means use the hard limit (effectively unlimited). |
| 133 | */ |
| 134 | lim = ACCESS_ONCE(usbfs_memory_mb); |
| 135 | if (lim == 0 || lim > (USBFS_XFER_MAX >> 20)) |
| 136 | lim = USBFS_XFER_MAX; |
| 137 | else |
| 138 | lim <<= 20; |
| 139 | |
Alan Stern | add1aae | 2011-11-17 16:41:25 -0500 | [diff] [blame] | 140 | atomic_add(amount, &usbfs_memory_usage); |
Alan Stern | 3f5eb8d | 2011-11-17 16:41:35 -0500 | [diff] [blame] | 141 | if (atomic_read(&usbfs_memory_usage) <= lim) |
Alan Stern | add1aae | 2011-11-17 16:41:25 -0500 | [diff] [blame] | 142 | return 0; |
| 143 | atomic_sub(amount, &usbfs_memory_usage); |
| 144 | return -ENOMEM; |
| 145 | } |
| 146 | |
| 147 | /* Memory for a transfer is being deallocated */ |
| 148 | static void usbfs_decrease_memory_usage(unsigned amount) |
| 149 | { |
| 150 | atomic_sub(amount, &usbfs_memory_usage); |
| 151 | } |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 152 | |
Alan Stern | d34d972 | 2009-03-09 13:44:48 -0400 | [diff] [blame] | 153 | static int connected(struct dev_state *ps) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
Alan Stern | 349710c | 2006-07-01 22:05:56 -0400 | [diff] [blame] | 155 | return (!list_empty(&ps->list) && |
| 156 | ps->dev->state != USB_STATE_NOTATTACHED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig) |
| 160 | { |
| 161 | loff_t ret; |
| 162 | |
Oliver Neukum | f9de332 | 2010-01-13 15:32:21 +0100 | [diff] [blame] | 163 | mutex_lock(&file->f_dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | switch (orig) { |
| 166 | case 0: |
| 167 | file->f_pos = offset; |
| 168 | ret = file->f_pos; |
| 169 | break; |
| 170 | case 1: |
| 171 | file->f_pos += offset; |
| 172 | ret = file->f_pos; |
| 173 | break; |
| 174 | case 2: |
| 175 | default: |
| 176 | ret = -EINVAL; |
| 177 | } |
| 178 | |
Oliver Neukum | f9de332 | 2010-01-13 15:32:21 +0100 | [diff] [blame] | 179 | mutex_unlock(&file->f_dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | return ret; |
| 181 | } |
| 182 | |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 183 | static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes, |
| 184 | loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
Tobias Klauser | ec17cf1 | 2006-09-13 21:38:41 +0200 | [diff] [blame] | 186 | struct dev_state *ps = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | struct usb_device *dev = ps->dev; |
| 188 | ssize_t ret = 0; |
| 189 | unsigned len; |
| 190 | loff_t pos; |
| 191 | int i; |
| 192 | |
| 193 | pos = *ppos; |
| 194 | usb_lock_device(dev); |
Alan Stern | 349710c | 2006-07-01 22:05:56 -0400 | [diff] [blame] | 195 | if (!connected(ps)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | ret = -ENODEV; |
| 197 | goto err; |
| 198 | } else if (pos < 0) { |
| 199 | ret = -EINVAL; |
| 200 | goto err; |
| 201 | } |
| 202 | |
| 203 | if (pos < sizeof(struct usb_device_descriptor)) { |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 204 | /* 18 bytes - fits on the stack */ |
| 205 | struct usb_device_descriptor temp_desc; |
Oliver Neukum | 8781ba0 | 2006-01-06 21:24:25 +0100 | [diff] [blame] | 206 | |
| 207 | memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor)); |
Andrew Morton | 9fcd5c3 | 2006-01-18 23:55:07 -0800 | [diff] [blame] | 208 | le16_to_cpus(&temp_desc.bcdUSB); |
| 209 | le16_to_cpus(&temp_desc.idVendor); |
| 210 | le16_to_cpus(&temp_desc.idProduct); |
| 211 | le16_to_cpus(&temp_desc.bcdDevice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | len = sizeof(struct usb_device_descriptor) - pos; |
| 214 | if (len > nbytes) |
| 215 | len = nbytes; |
Oliver Neukum | 8781ba0 | 2006-01-06 21:24:25 +0100 | [diff] [blame] | 216 | if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | ret = -EFAULT; |
| 218 | goto err; |
| 219 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
| 221 | *ppos += len; |
| 222 | buf += len; |
| 223 | nbytes -= len; |
| 224 | ret += len; |
| 225 | } |
| 226 | |
| 227 | pos = sizeof(struct usb_device_descriptor); |
| 228 | for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) { |
| 229 | struct usb_config_descriptor *config = |
| 230 | (struct usb_config_descriptor *)dev->rawdescriptors[i]; |
| 231 | unsigned int length = le16_to_cpu(config->wTotalLength); |
| 232 | |
| 233 | if (*ppos < pos + length) { |
| 234 | |
| 235 | /* The descriptor may claim to be longer than it |
| 236 | * really is. Here is the actual allocated length. */ |
| 237 | unsigned alloclen = |
| 238 | le16_to_cpu(dev->config[i].desc.wTotalLength); |
| 239 | |
| 240 | len = length - (*ppos - pos); |
| 241 | if (len > nbytes) |
| 242 | len = nbytes; |
| 243 | |
| 244 | /* Simply don't write (skip over) unallocated parts */ |
| 245 | if (alloclen > (*ppos - pos)) { |
| 246 | alloclen -= (*ppos - pos); |
| 247 | if (copy_to_user(buf, |
| 248 | dev->rawdescriptors[i] + (*ppos - pos), |
| 249 | min(len, alloclen))) { |
| 250 | ret = -EFAULT; |
| 251 | goto err; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | *ppos += len; |
| 256 | buf += len; |
| 257 | nbytes -= len; |
| 258 | ret += len; |
| 259 | } |
| 260 | |
| 261 | pos += length; |
| 262 | } |
| 263 | |
| 264 | err: |
| 265 | usb_unlock_device(dev); |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * async list handling |
| 271 | */ |
| 272 | |
| 273 | static struct async *alloc_async(unsigned int numisoframes) |
| 274 | { |
Pete Zaitcev | dd95b81 | 2008-01-05 02:01:07 -0800 | [diff] [blame] | 275 | struct async *as; |
Alan Stern | 0a1ef3b | 2005-10-24 15:38:24 -0400 | [diff] [blame] | 276 | |
Pete Zaitcev | dd95b81 | 2008-01-05 02:01:07 -0800 | [diff] [blame] | 277 | as = kzalloc(sizeof(struct async), GFP_KERNEL); |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 278 | if (!as) |
| 279 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL); |
| 281 | if (!as->urb) { |
| 282 | kfree(as); |
| 283 | return NULL; |
| 284 | } |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 285 | return as; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static void free_async(struct async *as) |
| 289 | { |
Hans de Goede | 3d97ff6 | 2012-07-04 09:18:03 +0200 | [diff] [blame^] | 290 | int i; |
| 291 | |
Eric W. Biederman | 2425c08 | 2006-10-02 02:17:28 -0700 | [diff] [blame] | 292 | put_pid(as->pid); |
Sarah Sharp | 1b41c83 | 2011-12-16 11:26:30 -0800 | [diff] [blame] | 293 | if (as->cred) |
| 294 | put_cred(as->cred); |
Hans de Goede | 3d97ff6 | 2012-07-04 09:18:03 +0200 | [diff] [blame^] | 295 | for (i = 0; i < as->urb->num_sgs; i++) { |
| 296 | if (sg_page(&as->urb->sg[i])) |
| 297 | kfree(sg_virt(&as->urb->sg[i])); |
| 298 | } |
| 299 | kfree(as->urb->sg); |
Jesper Juhl | 6fd19f4 | 2005-04-18 17:39:33 -0700 | [diff] [blame] | 300 | kfree(as->urb->transfer_buffer); |
| 301 | kfree(as->urb->setup_packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | usb_free_urb(as->urb); |
Alan Stern | add1aae | 2011-11-17 16:41:25 -0500 | [diff] [blame] | 303 | usbfs_decrease_memory_usage(as->mem_usage); |
Jesper Juhl | 6fd19f4 | 2005-04-18 17:39:33 -0700 | [diff] [blame] | 304 | kfree(as); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Alan Stern | d34d972 | 2009-03-09 13:44:48 -0400 | [diff] [blame] | 307 | static void async_newpending(struct async *as) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | { |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 309 | struct dev_state *ps = as->ps; |
| 310 | unsigned long flags; |
| 311 | |
| 312 | spin_lock_irqsave(&ps->lock, flags); |
| 313 | list_add_tail(&as->asynclist, &ps->async_pending); |
| 314 | spin_unlock_irqrestore(&ps->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Alan Stern | d34d972 | 2009-03-09 13:44:48 -0400 | [diff] [blame] | 317 | static void async_removepending(struct async *as) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | { |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 319 | struct dev_state *ps = as->ps; |
| 320 | unsigned long flags; |
| 321 | |
| 322 | spin_lock_irqsave(&ps->lock, flags); |
| 323 | list_del_init(&as->asynclist); |
| 324 | spin_unlock_irqrestore(&ps->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Alan Stern | d34d972 | 2009-03-09 13:44:48 -0400 | [diff] [blame] | 327 | static struct async *async_getcompleted(struct dev_state *ps) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | { |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 329 | unsigned long flags; |
| 330 | struct async *as = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 332 | spin_lock_irqsave(&ps->lock, flags); |
| 333 | if (!list_empty(&ps->async_completed)) { |
| 334 | as = list_entry(ps->async_completed.next, struct async, |
| 335 | asynclist); |
| 336 | list_del_init(&as->asynclist); |
| 337 | } |
| 338 | spin_unlock_irqrestore(&ps->lock, flags); |
| 339 | return as; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Alan Stern | d34d972 | 2009-03-09 13:44:48 -0400 | [diff] [blame] | 342 | static struct async *async_getpending(struct dev_state *ps, |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 343 | void __user *userurb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | { |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 345 | struct async *as; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | list_for_each_entry(as, &ps->async_pending, asynclist) |
| 348 | if (as->userurb == userurb) { |
| 349 | list_del_init(&as->asynclist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | return as; |
| 351 | } |
Huajun Li | 4e09dcf | 2012-05-18 20:12:51 +0800 | [diff] [blame] | 352 | |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 353 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 356 | static void snoop_urb(struct usb_device *udev, |
| 357 | void __user *userurb, int pipe, unsigned length, |
Chris Frey | 0880aef | 2010-01-26 17:07:29 -0500 | [diff] [blame] | 358 | int timeout_or_status, enum snoop_when when, |
| 359 | unsigned char *data, unsigned data_len) |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 360 | { |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 361 | static const char *types[] = {"isoc", "int", "ctrl", "bulk"}; |
| 362 | static const char *dirs[] = {"out", "in"}; |
| 363 | int ep; |
| 364 | const char *t, *d; |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 365 | |
| 366 | if (!usbfs_snoop) |
| 367 | return; |
| 368 | |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 369 | ep = usb_pipeendpoint(pipe); |
| 370 | t = types[usb_pipetype(pipe)]; |
| 371 | d = dirs[!!usb_pipein(pipe)]; |
| 372 | |
| 373 | if (userurb) { /* Async */ |
| 374 | if (when == SUBMIT) |
| 375 | dev_info(&udev->dev, "userurb %p, ep%d %s-%s, " |
| 376 | "length %u\n", |
| 377 | userurb, ep, t, d, length); |
| 378 | else |
| 379 | dev_info(&udev->dev, "userurb %p, ep%d %s-%s, " |
| 380 | "actual_length %u status %d\n", |
| 381 | userurb, ep, t, d, length, |
| 382 | timeout_or_status); |
| 383 | } else { |
| 384 | if (when == SUBMIT) |
| 385 | dev_info(&udev->dev, "ep%d %s-%s, length %u, " |
| 386 | "timeout %d\n", |
| 387 | ep, t, d, length, timeout_or_status); |
| 388 | else |
| 389 | dev_info(&udev->dev, "ep%d %s-%s, actual_length %u, " |
| 390 | "status %d\n", |
| 391 | ep, t, d, length, timeout_or_status); |
| 392 | } |
Chris Frey | 0880aef | 2010-01-26 17:07:29 -0500 | [diff] [blame] | 393 | |
| 394 | if (data && data_len > 0) { |
| 395 | print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1, |
| 396 | data, data_len, 1); |
| 397 | } |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Hans de Goede | 3d97ff6 | 2012-07-04 09:18:03 +0200 | [diff] [blame^] | 400 | static void snoop_urb_data(struct urb *urb, unsigned len) |
| 401 | { |
| 402 | int i, size; |
| 403 | |
| 404 | if (!usbfs_snoop) |
| 405 | return; |
| 406 | |
| 407 | if (urb->num_sgs == 0) { |
| 408 | print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1, |
| 409 | urb->transfer_buffer, len, 1); |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | for (i = 0; i < urb->num_sgs && len; i++) { |
| 414 | size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len; |
| 415 | print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1, |
| 416 | sg_virt(&urb->sg[i]), size, 1); |
| 417 | len -= size; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | static int copy_urb_data_to_user(u8 __user *userbuffer, struct urb *urb) |
| 422 | { |
| 423 | unsigned i, len, size; |
| 424 | |
| 425 | if (urb->number_of_packets > 0) /* Isochronous */ |
| 426 | len = urb->transfer_buffer_length; |
| 427 | else /* Non-Isoc */ |
| 428 | len = urb->actual_length; |
| 429 | |
| 430 | if (urb->num_sgs == 0) { |
| 431 | if (copy_to_user(userbuffer, urb->transfer_buffer, len)) |
| 432 | return -EFAULT; |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | for (i = 0; i < urb->num_sgs && len; i++) { |
| 437 | size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len; |
| 438 | if (copy_to_user(userbuffer, sg_virt(&urb->sg[i]), size)) |
| 439 | return -EFAULT; |
| 440 | userbuffer += size; |
| 441 | len -= size; |
| 442 | } |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
Alan Stern | 01c6460 | 2009-09-01 11:09:56 -0400 | [diff] [blame] | 447 | #define AS_CONTINUATION 1 |
| 448 | #define AS_UNLINK 2 |
| 449 | |
| 450 | static void cancel_bulk_urbs(struct dev_state *ps, unsigned bulk_addr) |
| 451 | __releases(ps->lock) |
| 452 | __acquires(ps->lock) |
| 453 | { |
Huajun Li | 4e09dcf | 2012-05-18 20:12:51 +0800 | [diff] [blame] | 454 | struct urb *urb; |
Alan Stern | 01c6460 | 2009-09-01 11:09:56 -0400 | [diff] [blame] | 455 | struct async *as; |
| 456 | |
| 457 | /* Mark all the pending URBs that match bulk_addr, up to but not |
| 458 | * including the first one without AS_CONTINUATION. If such an |
| 459 | * URB is encountered then a new transfer has already started so |
| 460 | * the endpoint doesn't need to be disabled; otherwise it does. |
| 461 | */ |
| 462 | list_for_each_entry(as, &ps->async_pending, asynclist) { |
| 463 | if (as->bulk_addr == bulk_addr) { |
| 464 | if (as->bulk_status != AS_CONTINUATION) |
| 465 | goto rescan; |
| 466 | as->bulk_status = AS_UNLINK; |
| 467 | as->bulk_addr = 0; |
| 468 | } |
| 469 | } |
| 470 | ps->disabled_bulk_eps |= (1 << bulk_addr); |
| 471 | |
| 472 | /* Now carefully unlink all the marked pending URBs */ |
| 473 | rescan: |
| 474 | list_for_each_entry(as, &ps->async_pending, asynclist) { |
| 475 | if (as->bulk_status == AS_UNLINK) { |
| 476 | as->bulk_status = 0; /* Only once */ |
Huajun Li | 4e09dcf | 2012-05-18 20:12:51 +0800 | [diff] [blame] | 477 | urb = as->urb; |
| 478 | usb_get_urb(urb); |
Alan Stern | 01c6460 | 2009-09-01 11:09:56 -0400 | [diff] [blame] | 479 | spin_unlock(&ps->lock); /* Allow completions */ |
Huajun Li | 4e09dcf | 2012-05-18 20:12:51 +0800 | [diff] [blame] | 480 | usb_unlink_urb(urb); |
| 481 | usb_put_urb(urb); |
Alan Stern | 01c6460 | 2009-09-01 11:09:56 -0400 | [diff] [blame] | 482 | spin_lock(&ps->lock); |
| 483 | goto rescan; |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 488 | static void async_completed(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | { |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 490 | struct async *as = urb->context; |
| 491 | struct dev_state *ps = as->ps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | struct siginfo sinfo; |
Oliver Neukum | 516a1a0 | 2009-07-08 19:09:23 +0200 | [diff] [blame] | 493 | struct pid *pid = NULL; |
Oliver Neukum | 516a1a0 | 2009-07-08 19:09:23 +0200 | [diff] [blame] | 494 | u32 secid = 0; |
Serge Hallyn | d178bc3 | 2011-09-26 10:45:18 -0500 | [diff] [blame] | 495 | const struct cred *cred = NULL; |
Oliver Neukum | 516a1a0 | 2009-07-08 19:09:23 +0200 | [diff] [blame] | 496 | int signr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 498 | spin_lock(&ps->lock); |
| 499 | list_move_tail(&as->asynclist, &ps->async_completed); |
Alan Stern | e015268 | 2007-08-24 15:42:52 -0400 | [diff] [blame] | 500 | as->status = urb->status; |
Oliver Neukum | 516a1a0 | 2009-07-08 19:09:23 +0200 | [diff] [blame] | 501 | signr = as->signr; |
| 502 | if (signr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | sinfo.si_signo = as->signr; |
Alan Stern | e015268 | 2007-08-24 15:42:52 -0400 | [diff] [blame] | 504 | sinfo.si_errno = as->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | sinfo.si_code = SI_ASYNCIO; |
| 506 | sinfo.si_addr = as->userurb; |
Serge Hallyn | aec01c5 | 2011-09-26 10:18:29 -0500 | [diff] [blame] | 507 | pid = get_pid(as->pid); |
Serge Hallyn | d178bc3 | 2011-09-26 10:45:18 -0500 | [diff] [blame] | 508 | cred = get_cred(as->cred); |
Oliver Neukum | 516a1a0 | 2009-07-08 19:09:23 +0200 | [diff] [blame] | 509 | secid = as->secid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | } |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 511 | snoop(&urb->dev->dev, "urb complete\n"); |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 512 | snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length, |
Hans de Goede | 3d97ff6 | 2012-07-04 09:18:03 +0200 | [diff] [blame^] | 513 | as->status, COMPLETE, NULL, 0); |
| 514 | if ((urb->transfer_flags & URB_DIR_MASK) == USB_DIR_IN) |
| 515 | snoop_urb_data(urb, urb->actual_length); |
| 516 | |
Alan Stern | 01c6460 | 2009-09-01 11:09:56 -0400 | [diff] [blame] | 517 | if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET && |
| 518 | as->status != -ENOENT) |
| 519 | cancel_bulk_urbs(ps, as->bulk_addr); |
Oliver Neukum | 516a1a0 | 2009-07-08 19:09:23 +0200 | [diff] [blame] | 520 | spin_unlock(&ps->lock); |
| 521 | |
Serge Hallyn | aec01c5 | 2011-09-26 10:18:29 -0500 | [diff] [blame] | 522 | if (signr) { |
Serge Hallyn | d178bc3 | 2011-09-26 10:45:18 -0500 | [diff] [blame] | 523 | kill_pid_info_as_cred(sinfo.si_signo, &sinfo, pid, cred, secid); |
Serge Hallyn | aec01c5 | 2011-09-26 10:18:29 -0500 | [diff] [blame] | 524 | put_pid(pid); |
Serge Hallyn | d178bc3 | 2011-09-26 10:45:18 -0500 | [diff] [blame] | 525 | put_cred(cred); |
Serge Hallyn | aec01c5 | 2011-09-26 10:18:29 -0500 | [diff] [blame] | 526 | } |
Oliver Neukum | 516a1a0 | 2009-07-08 19:09:23 +0200 | [diff] [blame] | 527 | |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 528 | wake_up(&ps->wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 531 | static void destroy_async(struct dev_state *ps, struct list_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | { |
Huajun Li | 4e09dcf | 2012-05-18 20:12:51 +0800 | [diff] [blame] | 533 | struct urb *urb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | struct async *as; |
| 535 | unsigned long flags; |
| 536 | |
| 537 | spin_lock_irqsave(&ps->lock, flags); |
| 538 | while (!list_empty(list)) { |
| 539 | as = list_entry(list->next, struct async, asynclist); |
| 540 | list_del_init(&as->asynclist); |
Huajun Li | 4e09dcf | 2012-05-18 20:12:51 +0800 | [diff] [blame] | 541 | urb = as->urb; |
| 542 | usb_get_urb(urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |
| 544 | /* drop the spinlock so the completion handler can run */ |
| 545 | spin_unlock_irqrestore(&ps->lock, flags); |
Huajun Li | 4e09dcf | 2012-05-18 20:12:51 +0800 | [diff] [blame] | 546 | usb_kill_urb(urb); |
| 547 | usb_put_urb(urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | spin_lock_irqsave(&ps->lock, flags); |
| 549 | } |
| 550 | spin_unlock_irqrestore(&ps->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 553 | static void destroy_async_on_interface(struct dev_state *ps, |
| 554 | unsigned int ifnum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | { |
| 556 | struct list_head *p, *q, hitlist; |
| 557 | unsigned long flags; |
| 558 | |
| 559 | INIT_LIST_HEAD(&hitlist); |
| 560 | spin_lock_irqsave(&ps->lock, flags); |
| 561 | list_for_each_safe(p, q, &ps->async_pending) |
| 562 | if (ifnum == list_entry(p, struct async, asynclist)->ifnum) |
| 563 | list_move_tail(p, &hitlist); |
| 564 | spin_unlock_irqrestore(&ps->lock, flags); |
| 565 | destroy_async(ps, &hitlist); |
| 566 | } |
| 567 | |
Alan Stern | d34d972 | 2009-03-09 13:44:48 -0400 | [diff] [blame] | 568 | static void destroy_all_async(struct dev_state *ps) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | { |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 570 | destroy_async(ps, &ps->async_pending); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /* |
| 574 | * interface claims are made only at the request of user level code, |
| 575 | * which can also release them (explicitly or by closing files). |
| 576 | * they're also undone when devices disconnect. |
| 577 | */ |
| 578 | |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 579 | static int driver_probe(struct usb_interface *intf, |
| 580 | const struct usb_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | { |
| 582 | return -ENODEV; |
| 583 | } |
| 584 | |
| 585 | static void driver_disconnect(struct usb_interface *intf) |
| 586 | { |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 587 | struct dev_state *ps = usb_get_intfdata(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber; |
| 589 | |
| 590 | if (!ps) |
| 591 | return; |
| 592 | |
| 593 | /* NOTE: this relies on usbcore having canceled and completed |
| 594 | * all pending I/O requests; 2.6 does that. |
| 595 | */ |
| 596 | |
| 597 | if (likely(ifnum < 8*sizeof(ps->ifclaimed))) |
| 598 | clear_bit(ifnum, &ps->ifclaimed); |
| 599 | else |
Greg Kroah-Hartman | 3b6004f | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 600 | dev_warn(&intf->dev, "interface number %u out of range\n", |
| 601 | ifnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 603 | usb_set_intfdata(intf, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
| 605 | /* force async requests to complete */ |
| 606 | destroy_async_on_interface(ps, ifnum); |
| 607 | } |
| 608 | |
Alan Stern | 2e2eb83 | 2007-12-04 14:35:15 -0500 | [diff] [blame] | 609 | /* The following routines are merely placeholders. There is no way |
| 610 | * to inform a user task about suspend or resumes. |
| 611 | */ |
| 612 | static int driver_suspend(struct usb_interface *intf, pm_message_t msg) |
| 613 | { |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | static int driver_resume(struct usb_interface *intf) |
| 618 | { |
| 619 | return 0; |
| 620 | } |
| 621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | struct usb_driver usbfs_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | .name = "usbfs", |
| 624 | .probe = driver_probe, |
| 625 | .disconnect = driver_disconnect, |
Alan Stern | 2e2eb83 | 2007-12-04 14:35:15 -0500 | [diff] [blame] | 626 | .suspend = driver_suspend, |
| 627 | .resume = driver_resume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | }; |
| 629 | |
| 630 | static int claimintf(struct dev_state *ps, unsigned int ifnum) |
| 631 | { |
| 632 | struct usb_device *dev = ps->dev; |
| 633 | struct usb_interface *intf; |
| 634 | int err; |
| 635 | |
| 636 | if (ifnum >= 8*sizeof(ps->ifclaimed)) |
| 637 | return -EINVAL; |
| 638 | /* already claimed */ |
| 639 | if (test_bit(ifnum, &ps->ifclaimed)) |
| 640 | return 0; |
| 641 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | intf = usb_ifnum_to_if(dev, ifnum); |
| 643 | if (!intf) |
| 644 | err = -ENOENT; |
| 645 | else |
| 646 | err = usb_driver_claim_interface(&usbfs_driver, intf, ps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | if (err == 0) |
| 648 | set_bit(ifnum, &ps->ifclaimed); |
| 649 | return err; |
| 650 | } |
| 651 | |
| 652 | static int releaseintf(struct dev_state *ps, unsigned int ifnum) |
| 653 | { |
| 654 | struct usb_device *dev; |
| 655 | struct usb_interface *intf; |
| 656 | int err; |
| 657 | |
| 658 | err = -EINVAL; |
| 659 | if (ifnum >= 8*sizeof(ps->ifclaimed)) |
| 660 | return err; |
| 661 | dev = ps->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | intf = usb_ifnum_to_if(dev, ifnum); |
| 663 | if (!intf) |
| 664 | err = -ENOENT; |
| 665 | else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) { |
| 666 | usb_driver_release_interface(&usbfs_driver, intf); |
| 667 | err = 0; |
| 668 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | return err; |
| 670 | } |
| 671 | |
| 672 | static int checkintf(struct dev_state *ps, unsigned int ifnum) |
| 673 | { |
| 674 | if (ps->dev->state != USB_STATE_CONFIGURED) |
| 675 | return -EHOSTUNREACH; |
| 676 | if (ifnum >= 8*sizeof(ps->ifclaimed)) |
| 677 | return -EINVAL; |
| 678 | if (test_bit(ifnum, &ps->ifclaimed)) |
| 679 | return 0; |
| 680 | /* if not yet claimed, claim it for the driver */ |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 681 | dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim " |
| 682 | "interface %u before use\n", task_pid_nr(current), |
| 683 | current->comm, ifnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | return claimintf(ps, ifnum); |
| 685 | } |
| 686 | |
| 687 | static int findintfep(struct usb_device *dev, unsigned int ep) |
| 688 | { |
| 689 | unsigned int i, j, e; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 690 | struct usb_interface *intf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | struct usb_host_interface *alts; |
| 692 | struct usb_endpoint_descriptor *endpt; |
| 693 | |
| 694 | if (ep & ~(USB_DIR_IN|0xf)) |
| 695 | return -EINVAL; |
| 696 | if (!dev->actconfig) |
| 697 | return -ESRCH; |
| 698 | for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { |
| 699 | intf = dev->actconfig->interface[i]; |
| 700 | for (j = 0; j < intf->num_altsetting; j++) { |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 701 | alts = &intf->altsetting[j]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | for (e = 0; e < alts->desc.bNumEndpoints; e++) { |
| 703 | endpt = &alts->endpoint[e].desc; |
| 704 | if (endpt->bEndpointAddress == ep) |
| 705 | return alts->desc.bInterfaceNumber; |
| 706 | } |
| 707 | } |
| 708 | } |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 709 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 712 | static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype, |
Matthias Dellweg | 393cbb5 | 2011-09-25 14:26:25 +0200 | [diff] [blame] | 713 | unsigned int request, unsigned int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | { |
| 715 | int ret = 0; |
Matthias Dellweg | 393cbb5 | 2011-09-25 14:26:25 +0200 | [diff] [blame] | 716 | struct usb_host_interface *alt_setting; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
David Vrabel | 6da9c99 | 2009-02-18 14:43:47 +0000 | [diff] [blame] | 718 | if (ps->dev->state != USB_STATE_UNAUTHENTICATED |
| 719 | && ps->dev->state != USB_STATE_ADDRESS |
Horst Schirmeier | 24f8b11 | 2006-03-11 00:16:55 -0800 | [diff] [blame] | 720 | && ps->dev->state != USB_STATE_CONFIGURED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | return -EHOSTUNREACH; |
| 722 | if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype)) |
| 723 | return 0; |
| 724 | |
Matthias Dellweg | 393cbb5 | 2011-09-25 14:26:25 +0200 | [diff] [blame] | 725 | /* |
| 726 | * check for the special corner case 'get_device_id' in the printer |
| 727 | * class specification, where wIndex is (interface << 8 | altsetting) |
| 728 | * instead of just interface |
| 729 | */ |
| 730 | if (requesttype == 0xa1 && request == 0) { |
| 731 | alt_setting = usb_find_alt_setting(ps->dev->actconfig, |
| 732 | index >> 8, index & 0xff); |
| 733 | if (alt_setting |
| 734 | && alt_setting->desc.bInterfaceClass == USB_CLASS_PRINTER) |
| 735 | index >>= 8; |
| 736 | } |
| 737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | index &= 0xff; |
| 739 | switch (requesttype & USB_RECIP_MASK) { |
| 740 | case USB_RECIP_ENDPOINT: |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 741 | ret = findintfep(ps->dev, index); |
| 742 | if (ret >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | ret = checkintf(ps, ret); |
| 744 | break; |
| 745 | |
| 746 | case USB_RECIP_INTERFACE: |
| 747 | ret = checkintf(ps, index); |
| 748 | break; |
| 749 | } |
| 750 | return ret; |
| 751 | } |
| 752 | |
Alan Stern | 61ad04a | 2008-06-24 14:47:12 -0400 | [diff] [blame] | 753 | static int match_devt(struct device *dev, void *data) |
Greg Kroah-Hartman | 4592bf5 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 754 | { |
David Howells | a80d5ff | 2008-07-02 12:28:55 +0100 | [diff] [blame] | 755 | return dev->devt == (dev_t) (unsigned long) data; |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 756 | } |
Greg Kroah-Hartman | 4592bf5 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 757 | |
Alan Stern | 61ad04a | 2008-06-24 14:47:12 -0400 | [diff] [blame] | 758 | static struct usb_device *usbdev_lookup_by_devt(dev_t devt) |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 759 | { |
| 760 | struct device *dev; |
| 761 | |
David Howells | a80d5ff | 2008-07-02 12:28:55 +0100 | [diff] [blame] | 762 | dev = bus_find_device(&usb_bus_type, NULL, |
| 763 | (void *) (unsigned long) devt, match_devt); |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 764 | if (!dev) |
| 765 | return NULL; |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 766 | return container_of(dev, struct usb_device, dev); |
| 767 | } |
Greg Kroah-Hartman | 4592bf5 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | /* |
| 770 | * file operations |
| 771 | */ |
| 772 | static int usbdev_open(struct inode *inode, struct file *file) |
| 773 | { |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 774 | struct usb_device *dev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | struct dev_state *ps; |
| 776 | int ret; |
| 777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | ret = -ENOMEM; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 779 | ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL); |
| 780 | if (!ps) |
Alan Stern | 62e299e | 2010-01-08 12:56:19 -0500 | [diff] [blame] | 781 | goto out_free_ps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
Alan Stern | 01105a2 | 2009-07-30 15:28:14 -0400 | [diff] [blame] | 783 | ret = -ENODEV; |
Alan Stern | 61ad04a | 2008-06-24 14:47:12 -0400 | [diff] [blame] | 784 | |
Alan Stern | 62e299e | 2010-01-08 12:56:19 -0500 | [diff] [blame] | 785 | /* Protect against simultaneous removal or release */ |
| 786 | mutex_lock(&usbfs_mutex); |
| 787 | |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 788 | /* usbdev device-node */ |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 789 | if (imajor(inode) == USB_DEVICE_MAJOR) |
Alan Stern | 61ad04a | 2008-06-24 14:47:12 -0400 | [diff] [blame] | 790 | dev = usbdev_lookup_by_devt(inode->i_rdev); |
Alan Stern | 62e299e | 2010-01-08 12:56:19 -0500 | [diff] [blame] | 791 | |
Alan Stern | 62e299e | 2010-01-08 12:56:19 -0500 | [diff] [blame] | 792 | mutex_unlock(&usbfs_mutex); |
| 793 | |
| 794 | if (!dev) |
| 795 | goto out_free_ps; |
| 796 | |
| 797 | usb_lock_device(dev); |
| 798 | if (dev->state == USB_STATE_NOTATTACHED) |
| 799 | goto out_unlock_device; |
| 800 | |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 801 | ret = usb_autoresume_device(dev); |
Alan Stern | 01d883d | 2006-08-30 15:47:18 -0400 | [diff] [blame] | 802 | if (ret) |
Alan Stern | 62e299e | 2010-01-08 12:56:19 -0500 | [diff] [blame] | 803 | goto out_unlock_device; |
Alan Stern | 01d883d | 2006-08-30 15:47:18 -0400 | [diff] [blame] | 804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | ps->dev = dev; |
| 806 | ps->file = file; |
| 807 | spin_lock_init(&ps->lock); |
Dan Carpenter | 316547f | 2006-12-13 00:03:38 -0800 | [diff] [blame] | 808 | INIT_LIST_HEAD(&ps->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | INIT_LIST_HEAD(&ps->async_pending); |
| 810 | INIT_LIST_HEAD(&ps->async_completed); |
| 811 | init_waitqueue_head(&ps->wait); |
| 812 | ps->discsignr = 0; |
Eric W. Biederman | 2425c08 | 2006-10-02 02:17:28 -0700 | [diff] [blame] | 813 | ps->disc_pid = get_pid(task_pid(current)); |
Serge Hallyn | d178bc3 | 2011-09-26 10:45:18 -0500 | [diff] [blame] | 814 | ps->cred = get_current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | ps->disccontext = NULL; |
| 816 | ps->ifclaimed = 0; |
David Quigley | 7a01955 | 2006-06-30 01:55:48 -0700 | [diff] [blame] | 817 | security_task_getsecid(current, &ps->secid); |
Oliver Neukum | 527660a | 2007-04-20 20:50:48 +0200 | [diff] [blame] | 818 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | list_add_tail(&ps->list, &dev->filelist); |
| 820 | file->private_data = ps; |
Alan Stern | 62e299e | 2010-01-08 12:56:19 -0500 | [diff] [blame] | 821 | usb_unlock_device(dev); |
Alan Stern | 2da41d5 | 2008-10-06 11:24:26 -0400 | [diff] [blame] | 822 | snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current), |
| 823 | current->comm); |
Alan Stern | 62e299e | 2010-01-08 12:56:19 -0500 | [diff] [blame] | 824 | return ret; |
| 825 | |
| 826 | out_unlock_device: |
| 827 | usb_unlock_device(dev); |
| 828 | usb_put_dev(dev); |
| 829 | out_free_ps: |
| 830 | kfree(ps); |
Alan Stern | 4a2a8a2 | 2006-07-01 22:05:01 -0400 | [diff] [blame] | 831 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | static int usbdev_release(struct inode *inode, struct file *file) |
| 835 | { |
Tobias Klauser | ec17cf1 | 2006-09-13 21:38:41 +0200 | [diff] [blame] | 836 | struct dev_state *ps = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | struct usb_device *dev = ps->dev; |
| 838 | unsigned int ifnum; |
Alan Stern | 6ff1046 | 2009-03-09 13:44:02 -0400 | [diff] [blame] | 839 | struct async *as; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
| 841 | usb_lock_device(dev); |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 842 | usb_hub_release_all_ports(dev, ps); |
Alan Stern | 4a2a8a2 | 2006-07-01 22:05:01 -0400 | [diff] [blame] | 843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | list_del_init(&ps->list); |
Alan Stern | 4a2a8a2 | 2006-07-01 22:05:01 -0400 | [diff] [blame] | 845 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed); |
| 847 | ifnum++) { |
| 848 | if (test_bit(ifnum, &ps->ifclaimed)) |
| 849 | releaseintf(ps, ifnum); |
| 850 | } |
| 851 | destroy_all_async(ps); |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 852 | usb_autosuspend_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | usb_unlock_device(dev); |
| 854 | usb_put_dev(dev); |
Eric W. Biederman | 2425c08 | 2006-10-02 02:17:28 -0700 | [diff] [blame] | 855 | put_pid(ps->disc_pid); |
Serge Hallyn | d178bc3 | 2011-09-26 10:45:18 -0500 | [diff] [blame] | 856 | put_cred(ps->cred); |
Alan Stern | 6ff1046 | 2009-03-09 13:44:02 -0400 | [diff] [blame] | 857 | |
| 858 | as = async_getcompleted(ps); |
| 859 | while (as) { |
| 860 | free_async(as); |
| 861 | as = async_getcompleted(ps); |
| 862 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | kfree(ps); |
Alan Stern | 4a2a8a2 | 2006-07-01 22:05:01 -0400 | [diff] [blame] | 864 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | static int proc_control(struct dev_state *ps, void __user *arg) |
| 868 | { |
| 869 | struct usb_device *dev = ps->dev; |
| 870 | struct usbdevfs_ctrltransfer ctrl; |
| 871 | unsigned int tmo; |
| 872 | unsigned char *tbuf; |
Andrew Morton | ff66e3c | 2008-03-12 13:32:24 -0700 | [diff] [blame] | 873 | unsigned wLength; |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 874 | int i, pipe, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
| 876 | if (copy_from_user(&ctrl, arg, sizeof(ctrl))) |
| 877 | return -EFAULT; |
Matthias Dellweg | 393cbb5 | 2011-09-25 14:26:25 +0200 | [diff] [blame] | 878 | ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.bRequest, |
| 879 | ctrl.wIndex); |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 880 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | return ret; |
Andrew Morton | ff66e3c | 2008-03-12 13:32:24 -0700 | [diff] [blame] | 882 | wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */ |
| 883 | if (wLength > PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | return -EINVAL; |
Alan Stern | add1aae | 2011-11-17 16:41:25 -0500 | [diff] [blame] | 885 | ret = usbfs_increase_memory_usage(PAGE_SIZE + sizeof(struct urb) + |
| 886 | sizeof(struct usb_ctrlrequest)); |
| 887 | if (ret) |
| 888 | return ret; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 889 | tbuf = (unsigned char *)__get_free_page(GFP_KERNEL); |
Alan Stern | add1aae | 2011-11-17 16:41:25 -0500 | [diff] [blame] | 890 | if (!tbuf) { |
| 891 | ret = -ENOMEM; |
| 892 | goto done; |
| 893 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | tmo = ctrl.timeout; |
Chris Frey | 0880aef | 2010-01-26 17:07:29 -0500 | [diff] [blame] | 895 | snoop(&dev->dev, "control urb: bRequestType=%02x " |
| 896 | "bRequest=%02x wValue=%04x " |
| 897 | "wIndex=%04x wLength=%04x\n", |
| 898 | ctrl.bRequestType, ctrl.bRequest, |
| 899 | __le16_to_cpup(&ctrl.wValue), |
| 900 | __le16_to_cpup(&ctrl.wIndex), |
| 901 | __le16_to_cpup(&ctrl.wLength)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | if (ctrl.bRequestType & 0x80) { |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 903 | if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data, |
| 904 | ctrl.wLength)) { |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 905 | ret = -EINVAL; |
| 906 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | } |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 908 | pipe = usb_rcvctrlpipe(dev, 0); |
Chris Frey | 0880aef | 2010-01-26 17:07:29 -0500 | [diff] [blame] | 909 | snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
| 911 | usb_unlock_device(dev); |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 912 | i = usb_control_msg(dev, pipe, ctrl.bRequest, |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 913 | ctrl.bRequestType, ctrl.wValue, ctrl.wIndex, |
| 914 | tbuf, ctrl.wLength, tmo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | usb_lock_device(dev); |
Chris Frey | 0880aef | 2010-01-26 17:07:29 -0500 | [diff] [blame] | 916 | snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE, |
Michal Sojka | 9d02b42 | 2011-03-15 16:41:47 +0100 | [diff] [blame] | 917 | tbuf, max(i, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | if ((i > 0) && ctrl.wLength) { |
Alan Stern | fe0410c | 2005-07-29 12:16:58 -0700 | [diff] [blame] | 919 | if (copy_to_user(ctrl.data, tbuf, i)) { |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 920 | ret = -EFAULT; |
| 921 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | } else { |
| 925 | if (ctrl.wLength) { |
| 926 | if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) { |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 927 | ret = -EFAULT; |
| 928 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | } |
| 930 | } |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 931 | pipe = usb_sndctrlpipe(dev, 0); |
Chris Frey | 0880aef | 2010-01-26 17:07:29 -0500 | [diff] [blame] | 932 | snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT, |
| 933 | tbuf, ctrl.wLength); |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | usb_unlock_device(dev); |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 936 | i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest, |
| 937 | ctrl.bRequestType, ctrl.wValue, ctrl.wIndex, |
| 938 | tbuf, ctrl.wLength, tmo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | usb_lock_device(dev); |
Chris Frey | 0880aef | 2010-01-26 17:07:29 -0500 | [diff] [blame] | 940 | snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | } |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 942 | if (i < 0 && i != -EPIPE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL " |
| 944 | "failed cmd %s rqt %u rq %u len %u ret %d\n", |
| 945 | current->comm, ctrl.bRequestType, ctrl.bRequest, |
| 946 | ctrl.wLength, i); |
| 947 | } |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 948 | ret = i; |
| 949 | done: |
| 950 | free_page((unsigned long) tbuf); |
Alan Stern | add1aae | 2011-11-17 16:41:25 -0500 | [diff] [blame] | 951 | usbfs_decrease_memory_usage(PAGE_SIZE + sizeof(struct urb) + |
| 952 | sizeof(struct usb_ctrlrequest)); |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 953 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | static int proc_bulk(struct dev_state *ps, void __user *arg) |
| 957 | { |
| 958 | struct usb_device *dev = ps->dev; |
| 959 | struct usbdevfs_bulktransfer bulk; |
| 960 | unsigned int tmo, len1, pipe; |
| 961 | int len2; |
| 962 | unsigned char *tbuf; |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 963 | int i, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
| 965 | if (copy_from_user(&bulk, arg, sizeof(bulk))) |
| 966 | return -EFAULT; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 967 | ret = findintfep(ps->dev, bulk.ep); |
| 968 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | return ret; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 970 | ret = checkintf(ps, ret); |
| 971 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | return ret; |
| 973 | if (bulk.ep & USB_DIR_IN) |
| 974 | pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f); |
| 975 | else |
| 976 | pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f); |
| 977 | if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN))) |
| 978 | return -EINVAL; |
| 979 | len1 = bulk.len; |
Alan Stern | 3f5eb8d | 2011-11-17 16:41:35 -0500 | [diff] [blame] | 980 | if (len1 >= USBFS_XFER_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | return -EINVAL; |
Alan Stern | add1aae | 2011-11-17 16:41:25 -0500 | [diff] [blame] | 982 | ret = usbfs_increase_memory_usage(len1 + sizeof(struct urb)); |
| 983 | if (ret) |
| 984 | return ret; |
| 985 | if (!(tbuf = kmalloc(len1, GFP_KERNEL))) { |
| 986 | ret = -ENOMEM; |
| 987 | goto done; |
| 988 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | tmo = bulk.timeout; |
| 990 | if (bulk.ep & 0x80) { |
| 991 | if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) { |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 992 | ret = -EINVAL; |
| 993 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | } |
Chris Frey | 0880aef | 2010-01-26 17:07:29 -0500 | [diff] [blame] | 995 | snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, NULL, 0); |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | usb_unlock_device(dev); |
| 998 | i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo); |
| 999 | usb_lock_device(dev); |
Chris Frey | 0880aef | 2010-01-26 17:07:29 -0500 | [diff] [blame] | 1000 | snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, tbuf, len2); |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 1001 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | if (!i && len2) { |
| 1003 | if (copy_to_user(bulk.data, tbuf, len2)) { |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 1004 | ret = -EFAULT; |
| 1005 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | } |
| 1007 | } |
| 1008 | } else { |
| 1009 | if (len1) { |
| 1010 | if (copy_from_user(tbuf, bulk.data, len1)) { |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 1011 | ret = -EFAULT; |
| 1012 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | } |
| 1014 | } |
Chris Frey | 0880aef | 2010-01-26 17:07:29 -0500 | [diff] [blame] | 1015 | snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, tbuf, len1); |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 1016 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | usb_unlock_device(dev); |
| 1018 | i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo); |
| 1019 | usb_lock_device(dev); |
Chris Frey | 0880aef | 2010-01-26 17:07:29 -0500 | [diff] [blame] | 1020 | snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | } |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 1022 | ret = (i < 0 ? i : len2); |
| 1023 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | kfree(tbuf); |
Alan Stern | add1aae | 2011-11-17 16:41:25 -0500 | [diff] [blame] | 1025 | usbfs_decrease_memory_usage(len1 + sizeof(struct urb)); |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 1026 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | static int proc_resetep(struct dev_state *ps, void __user *arg) |
| 1030 | { |
| 1031 | unsigned int ep; |
| 1032 | int ret; |
| 1033 | |
| 1034 | if (get_user(ep, (unsigned int __user *)arg)) |
| 1035 | return -EFAULT; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1036 | ret = findintfep(ps->dev, ep); |
| 1037 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | return ret; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1039 | ret = checkintf(ps, ret); |
| 1040 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | return ret; |
David Vrabel | 3444b26 | 2009-04-08 17:36:28 +0000 | [diff] [blame] | 1042 | usb_reset_endpoint(ps->dev, ep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | return 0; |
| 1044 | } |
| 1045 | |
| 1046 | static int proc_clearhalt(struct dev_state *ps, void __user *arg) |
| 1047 | { |
| 1048 | unsigned int ep; |
| 1049 | int pipe; |
| 1050 | int ret; |
| 1051 | |
| 1052 | if (get_user(ep, (unsigned int __user *)arg)) |
| 1053 | return -EFAULT; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1054 | ret = findintfep(ps->dev, ep); |
| 1055 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | return ret; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1057 | ret = checkintf(ps, ret); |
| 1058 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | return ret; |
| 1060 | if (ep & USB_DIR_IN) |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1061 | pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f); |
| 1062 | else |
| 1063 | pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | |
| 1065 | return usb_clear_halt(ps->dev, pipe); |
| 1066 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | |
| 1068 | static int proc_getdriver(struct dev_state *ps, void __user *arg) |
| 1069 | { |
| 1070 | struct usbdevfs_getdriver gd; |
| 1071 | struct usb_interface *intf; |
| 1072 | int ret; |
| 1073 | |
| 1074 | if (copy_from_user(&gd, arg, sizeof(gd))) |
| 1075 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | intf = usb_ifnum_to_if(ps->dev, gd.interface); |
| 1077 | if (!intf || !intf->dev.driver) |
| 1078 | ret = -ENODATA; |
| 1079 | else { |
| 1080 | strncpy(gd.driver, intf->dev.driver->name, |
| 1081 | sizeof(gd.driver)); |
| 1082 | ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0); |
| 1083 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | return ret; |
| 1085 | } |
| 1086 | |
| 1087 | static int proc_connectinfo(struct dev_state *ps, void __user *arg) |
| 1088 | { |
Vasiliy Kulikov | 886ccd4 | 2010-11-06 17:41:28 +0300 | [diff] [blame] | 1089 | struct usbdevfs_connectinfo ci = { |
| 1090 | .devnum = ps->dev->devnum, |
| 1091 | .slow = ps->dev->speed == USB_SPEED_LOW |
| 1092 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | if (copy_to_user(arg, &ci, sizeof(ci))) |
| 1095 | return -EFAULT; |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | static int proc_resetdevice(struct dev_state *ps) |
| 1100 | { |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 1101 | return usb_reset_device(ps->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | static int proc_setintf(struct dev_state *ps, void __user *arg) |
| 1105 | { |
| 1106 | struct usbdevfs_setinterface setintf; |
| 1107 | int ret; |
| 1108 | |
| 1109 | if (copy_from_user(&setintf, arg, sizeof(setintf))) |
| 1110 | return -EFAULT; |
| 1111 | if ((ret = checkintf(ps, setintf.interface))) |
| 1112 | return ret; |
| 1113 | return usb_set_interface(ps->dev, setintf.interface, |
| 1114 | setintf.altsetting); |
| 1115 | } |
| 1116 | |
| 1117 | static int proc_setconfig(struct dev_state *ps, void __user *arg) |
| 1118 | { |
Alan Stern | 3f141e2 | 2007-02-08 16:40:43 -0500 | [diff] [blame] | 1119 | int u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | int status = 0; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1121 | struct usb_host_config *actconfig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | |
Alan Stern | 3f141e2 | 2007-02-08 16:40:43 -0500 | [diff] [blame] | 1123 | if (get_user(u, (int __user *)arg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | return -EFAULT; |
| 1125 | |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1126 | actconfig = ps->dev->actconfig; |
| 1127 | |
| 1128 | /* Don't touch the device if any interfaces are claimed. |
| 1129 | * It could interfere with other drivers' operations, and if |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | * an interface is claimed by usbfs it could easily deadlock. |
| 1131 | */ |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1132 | if (actconfig) { |
| 1133 | int i; |
| 1134 | |
| 1135 | for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) { |
| 1136 | if (usb_interface_claimed(actconfig->interface[i])) { |
| 1137 | dev_warn(&ps->dev->dev, |
David Brownell | 72ebddb | 2005-04-11 18:34:17 -0700 | [diff] [blame] | 1138 | "usbfs: interface %d claimed by %s " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | "while '%s' sets config #%d\n", |
| 1140 | actconfig->interface[i] |
| 1141 | ->cur_altsetting |
| 1142 | ->desc.bInterfaceNumber, |
David Brownell | 72ebddb | 2005-04-11 18:34:17 -0700 | [diff] [blame] | 1143 | actconfig->interface[i] |
| 1144 | ->dev.driver->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | current->comm, u); |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1146 | status = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | } |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1149 | } |
| 1150 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | |
| 1152 | /* SET_CONFIGURATION is often abused as a "cheap" driver reset, |
| 1153 | * so avoid usb_set_configuration()'s kick to sysfs |
| 1154 | */ |
| 1155 | if (status == 0) { |
| 1156 | if (actconfig && actconfig->desc.bConfigurationValue == u) |
| 1157 | status = usb_reset_configuration(ps->dev); |
| 1158 | else |
| 1159 | status = usb_set_configuration(ps->dev, u); |
| 1160 | } |
| 1161 | |
| 1162 | return status; |
| 1163 | } |
| 1164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb, |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1166 | struct usbdevfs_iso_packet_desc __user *iso_frame_desc, |
| 1167 | void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | { |
| 1169 | struct usbdevfs_iso_packet_desc *isopkt = NULL; |
| 1170 | struct usb_host_endpoint *ep; |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 1171 | struct async *as = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | struct usb_ctrlrequest *dr = NULL; |
| 1173 | unsigned int u, totlen, isofrmlen; |
Hans de Goede | 3d97ff6 | 2012-07-04 09:18:03 +0200 | [diff] [blame^] | 1174 | int i, ret, is_in, num_sgs = 0, ifnum = -1; |
| 1175 | void *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | |
Alan Stern | 14722ef | 2008-04-17 10:18:11 -0400 | [diff] [blame] | 1177 | if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP | |
| 1178 | USBDEVFS_URB_SHORT_NOT_OK | |
Alan Stern | 01c6460 | 2009-09-01 11:09:56 -0400 | [diff] [blame] | 1179 | USBDEVFS_URB_BULK_CONTINUATION | |
Alan Stern | 14722ef | 2008-04-17 10:18:11 -0400 | [diff] [blame] | 1180 | USBDEVFS_URB_NO_FSBR | |
| 1181 | USBDEVFS_URB_ZERO_PACKET | |
| 1182 | USBDEVFS_URB_NO_INTERRUPT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | return -EINVAL; |
Alan Stern | 9180135 | 2009-06-29 11:04:54 -0400 | [diff] [blame] | 1184 | if (uurb->buffer_length > 0 && !uurb->buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | return -EINVAL; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1186 | if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL && |
| 1187 | (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) { |
| 1188 | ifnum = findintfep(ps->dev, uurb->endpoint); |
| 1189 | if (ifnum < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | return ifnum; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1191 | ret = checkintf(ps, ifnum); |
| 1192 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | return ret; |
| 1194 | } |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1195 | if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) { |
| 1196 | is_in = 1; |
| 1197 | ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK]; |
| 1198 | } else { |
| 1199 | is_in = 0; |
| 1200 | ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK]; |
| 1201 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | if (!ep) |
| 1203 | return -ENOENT; |
Alan Stern | add1aae | 2011-11-17 16:41:25 -0500 | [diff] [blame] | 1204 | |
| 1205 | u = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | switch(uurb->type) { |
| 1207 | case USBDEVFS_URB_TYPE_CONTROL: |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1208 | if (!usb_endpoint_xfer_control(&ep->desc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | return -EINVAL; |
Alan Stern | add1aae | 2011-11-17 16:41:25 -0500 | [diff] [blame] | 1210 | /* min 8 byte setup packet */ |
| 1211 | if (uurb->buffer_length < 8) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | return -EINVAL; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1213 | dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); |
| 1214 | if (!dr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | return -ENOMEM; |
| 1216 | if (copy_from_user(dr, uurb->buffer, 8)) { |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 1217 | ret = -EFAULT; |
| 1218 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | } |
| 1220 | if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) { |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 1221 | ret = -EINVAL; |
| 1222 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | } |
Matthias Dellweg | 393cbb5 | 2011-09-25 14:26:25 +0200 | [diff] [blame] | 1224 | ret = check_ctrlrecip(ps, dr->bRequestType, dr->bRequest, |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1225 | le16_to_cpup(&dr->wIndex)); |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 1226 | if (ret) |
| 1227 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | uurb->number_of_packets = 0; |
| 1229 | uurb->buffer_length = le16_to_cpup(&dr->wLength); |
| 1230 | uurb->buffer += 8; |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1231 | if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) { |
| 1232 | is_in = 1; |
| 1233 | uurb->endpoint |= USB_DIR_IN; |
| 1234 | } else { |
| 1235 | is_in = 0; |
| 1236 | uurb->endpoint &= ~USB_DIR_IN; |
| 1237 | } |
Chris Frey | 0880aef | 2010-01-26 17:07:29 -0500 | [diff] [blame] | 1238 | snoop(&ps->dev->dev, "control urb: bRequestType=%02x " |
| 1239 | "bRequest=%02x wValue=%04x " |
| 1240 | "wIndex=%04x wLength=%04x\n", |
| 1241 | dr->bRequestType, dr->bRequest, |
| 1242 | __le16_to_cpup(&dr->wValue), |
| 1243 | __le16_to_cpup(&dr->wIndex), |
| 1244 | __le16_to_cpup(&dr->wLength)); |
Alan Stern | add1aae | 2011-11-17 16:41:25 -0500 | [diff] [blame] | 1245 | u = sizeof(struct usb_ctrlrequest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | break; |
| 1247 | |
| 1248 | case USBDEVFS_URB_TYPE_BULK: |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1249 | switch (usb_endpoint_type(&ep->desc)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | case USB_ENDPOINT_XFER_CONTROL: |
| 1251 | case USB_ENDPOINT_XFER_ISOC: |
| 1252 | return -EINVAL; |
Alan Stern | f661c6f | 2009-12-11 16:20:20 -0500 | [diff] [blame] | 1253 | case USB_ENDPOINT_XFER_INT: |
| 1254 | /* allow single-shot interrupt transfers */ |
| 1255 | uurb->type = USBDEVFS_URB_TYPE_INTERRUPT; |
| 1256 | goto interrupt_urb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | } |
| 1258 | uurb->number_of_packets = 0; |
Hans de Goede | 3d97ff6 | 2012-07-04 09:18:03 +0200 | [diff] [blame^] | 1259 | num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE); |
| 1260 | if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize) |
| 1261 | num_sgs = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | break; |
| 1263 | |
Alan Stern | f661c6f | 2009-12-11 16:20:20 -0500 | [diff] [blame] | 1264 | case USBDEVFS_URB_TYPE_INTERRUPT: |
| 1265 | if (!usb_endpoint_xfer_int(&ep->desc)) |
| 1266 | return -EINVAL; |
| 1267 | interrupt_urb: |
| 1268 | uurb->number_of_packets = 0; |
Alan Stern | f661c6f | 2009-12-11 16:20:20 -0500 | [diff] [blame] | 1269 | break; |
| 1270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | case USBDEVFS_URB_TYPE_ISO: |
| 1272 | /* arbitrary limit */ |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1273 | if (uurb->number_of_packets < 1 || |
| 1274 | uurb->number_of_packets > 128) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | return -EINVAL; |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1276 | if (!usb_endpoint_xfer_isoc(&ep->desc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | return -EINVAL; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1278 | isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) * |
| 1279 | uurb->number_of_packets; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL))) |
| 1281 | return -ENOMEM; |
| 1282 | if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) { |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 1283 | ret = -EFAULT; |
| 1284 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | } |
| 1286 | for (totlen = u = 0; u < uurb->number_of_packets; u++) { |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1287 | /* arbitrary limit, |
| 1288 | * sufficient for USB 2.0 high-bandwidth iso */ |
Micah Dowty | 3612242 | 2006-05-19 11:26:24 -0700 | [diff] [blame] | 1289 | if (isopkt[u].length > 8192) { |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 1290 | ret = -EINVAL; |
| 1291 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | } |
| 1293 | totlen += isopkt[u].length; |
| 1294 | } |
Alan Stern | add1aae | 2011-11-17 16:41:25 -0500 | [diff] [blame] | 1295 | u *= sizeof(struct usb_iso_packet_descriptor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | uurb->buffer_length = totlen; |
| 1297 | break; |
| 1298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | default: |
| 1300 | return -EINVAL; |
| 1301 | } |
Alan Stern | add1aae | 2011-11-17 16:41:25 -0500 | [diff] [blame] | 1302 | |
Alan Stern | 3f5eb8d | 2011-11-17 16:41:35 -0500 | [diff] [blame] | 1303 | if (uurb->buffer_length >= USBFS_XFER_MAX) { |
Alan Stern | add1aae | 2011-11-17 16:41:25 -0500 | [diff] [blame] | 1304 | ret = -EINVAL; |
| 1305 | goto error; |
| 1306 | } |
Alan Stern | 9180135 | 2009-06-29 11:04:54 -0400 | [diff] [blame] | 1307 | if (uurb->buffer_length > 0 && |
| 1308 | !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ, |
| 1309 | uurb->buffer, uurb->buffer_length)) { |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 1310 | ret = -EFAULT; |
| 1311 | goto error; |
Alan Stern | 9180135 | 2009-06-29 11:04:54 -0400 | [diff] [blame] | 1312 | } |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1313 | as = alloc_async(uurb->number_of_packets); |
| 1314 | if (!as) { |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 1315 | ret = -ENOMEM; |
| 1316 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | } |
Hans de Goede | 3d97ff6 | 2012-07-04 09:18:03 +0200 | [diff] [blame^] | 1318 | |
| 1319 | u += sizeof(struct async) + sizeof(struct urb) + uurb->buffer_length + |
| 1320 | num_sgs * sizeof(struct scatterlist); |
Alan Stern | add1aae | 2011-11-17 16:41:25 -0500 | [diff] [blame] | 1321 | ret = usbfs_increase_memory_usage(u); |
| 1322 | if (ret) |
| 1323 | goto error; |
| 1324 | as->mem_usage = u; |
| 1325 | |
Hans de Goede | 3d97ff6 | 2012-07-04 09:18:03 +0200 | [diff] [blame^] | 1326 | if (num_sgs) { |
| 1327 | as->urb->sg = kmalloc(num_sgs * sizeof(struct scatterlist), |
| 1328 | GFP_KERNEL); |
| 1329 | if (!as->urb->sg) { |
| 1330 | ret = -ENOMEM; |
| 1331 | goto error; |
| 1332 | } |
| 1333 | as->urb->num_sgs = num_sgs; |
| 1334 | sg_init_table(as->urb->sg, as->urb->num_sgs); |
| 1335 | |
| 1336 | totlen = uurb->buffer_length; |
| 1337 | for (i = 0; i < as->urb->num_sgs; i++) { |
| 1338 | u = (totlen > USB_SG_SIZE) ? USB_SG_SIZE : totlen; |
| 1339 | buf = kmalloc(u, GFP_KERNEL); |
| 1340 | if (!buf) { |
| 1341 | ret = -ENOMEM; |
| 1342 | goto error; |
| 1343 | } |
| 1344 | sg_set_buf(&as->urb->sg[i], buf, u); |
| 1345 | |
| 1346 | if (!is_in) { |
| 1347 | if (copy_from_user(buf, uurb->buffer, u)) { |
| 1348 | ret = -EFAULT; |
| 1349 | goto error; |
| 1350 | } |
| 1351 | } |
| 1352 | totlen -= u; |
| 1353 | } |
| 1354 | } else if (uurb->buffer_length > 0) { |
Alan Stern | 9180135 | 2009-06-29 11:04:54 -0400 | [diff] [blame] | 1355 | as->urb->transfer_buffer = kmalloc(uurb->buffer_length, |
| 1356 | GFP_KERNEL); |
| 1357 | if (!as->urb->transfer_buffer) { |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 1358 | ret = -ENOMEM; |
| 1359 | goto error; |
Alan Stern | 9180135 | 2009-06-29 11:04:54 -0400 | [diff] [blame] | 1360 | } |
Hans de Goede | 3d97ff6 | 2012-07-04 09:18:03 +0200 | [diff] [blame^] | 1361 | |
| 1362 | if (!is_in) { |
| 1363 | if (copy_from_user(as->urb->transfer_buffer, |
| 1364 | uurb->buffer, |
| 1365 | uurb->buffer_length)) { |
| 1366 | ret = -EFAULT; |
| 1367 | goto error; |
| 1368 | } |
| 1369 | } else if (uurb->type == USBDEVFS_URB_TYPE_ISO) { |
| 1370 | /* |
| 1371 | * Isochronous input data may end up being |
| 1372 | * discontiguous if some of the packets are short. |
| 1373 | * Clear the buffer so that the gaps don't leak |
| 1374 | * kernel data to userspace. |
| 1375 | */ |
Alan Stern | 7152b59 | 2010-03-06 15:04:03 -0500 | [diff] [blame] | 1376 | memset(as->urb->transfer_buffer, 0, |
| 1377 | uurb->buffer_length); |
Hans de Goede | 3d97ff6 | 2012-07-04 09:18:03 +0200 | [diff] [blame^] | 1378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | } |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1380 | as->urb->dev = ps->dev; |
| 1381 | as->urb->pipe = (uurb->type << 30) | |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1382 | __create_pipe(ps->dev, uurb->endpoint & 0xf) | |
| 1383 | (uurb->endpoint & USB_DIR_IN); |
Alan Stern | 14722ef | 2008-04-17 10:18:11 -0400 | [diff] [blame] | 1384 | |
| 1385 | /* This tedious sequence is necessary because the URB_* flags |
| 1386 | * are internal to the kernel and subject to change, whereas |
| 1387 | * the USBDEVFS_URB_* flags are a user API and must not be changed. |
| 1388 | */ |
| 1389 | u = (is_in ? URB_DIR_IN : URB_DIR_OUT); |
| 1390 | if (uurb->flags & USBDEVFS_URB_ISO_ASAP) |
| 1391 | u |= URB_ISO_ASAP; |
| 1392 | if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK) |
| 1393 | u |= URB_SHORT_NOT_OK; |
| 1394 | if (uurb->flags & USBDEVFS_URB_NO_FSBR) |
| 1395 | u |= URB_NO_FSBR; |
| 1396 | if (uurb->flags & USBDEVFS_URB_ZERO_PACKET) |
| 1397 | u |= URB_ZERO_PACKET; |
| 1398 | if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT) |
| 1399 | u |= URB_NO_INTERRUPT; |
| 1400 | as->urb->transfer_flags = u; |
| 1401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | as->urb->transfer_buffer_length = uurb->buffer_length; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1403 | as->urb->setup_packet = (unsigned char *)dr; |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 1404 | dr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | as->urb->start_frame = uurb->start_frame; |
| 1406 | as->urb->number_of_packets = uurb->number_of_packets; |
Alan Stern | 97b9eb9 | 2007-02-26 14:56:14 -0500 | [diff] [blame] | 1407 | if (uurb->type == USBDEVFS_URB_TYPE_ISO || |
| 1408 | ps->dev->speed == USB_SPEED_HIGH) |
| 1409 | as->urb->interval = 1 << min(15, ep->desc.bInterval - 1); |
| 1410 | else |
| 1411 | as->urb->interval = ep->desc.bInterval; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1412 | as->urb->context = as; |
| 1413 | as->urb->complete = async_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | for (totlen = u = 0; u < uurb->number_of_packets; u++) { |
| 1415 | as->urb->iso_frame_desc[u].offset = totlen; |
| 1416 | as->urb->iso_frame_desc[u].length = isopkt[u].length; |
| 1417 | totlen += isopkt[u].length; |
| 1418 | } |
Jesper Juhl | 6fd19f4 | 2005-04-18 17:39:33 -0700 | [diff] [blame] | 1419 | kfree(isopkt); |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 1420 | isopkt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | as->ps = ps; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1422 | as->userurb = arg; |
Alan Stern | 9180135 | 2009-06-29 11:04:54 -0400 | [diff] [blame] | 1423 | if (is_in && uurb->buffer_length > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | as->userbuffer = uurb->buffer; |
| 1425 | else |
| 1426 | as->userbuffer = NULL; |
| 1427 | as->signr = uurb->signr; |
| 1428 | as->ifnum = ifnum; |
Eric W. Biederman | 2425c08 | 2006-10-02 02:17:28 -0700 | [diff] [blame] | 1429 | as->pid = get_pid(task_pid(current)); |
Serge Hallyn | d178bc3 | 2011-09-26 10:45:18 -0500 | [diff] [blame] | 1430 | as->cred = get_current_cred(); |
David Quigley | 7a01955 | 2006-06-30 01:55:48 -0700 | [diff] [blame] | 1431 | security_task_getsecid(current, &as->secid); |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 1432 | snoop_urb(ps->dev, as->userurb, as->urb->pipe, |
Chris Frey | 0880aef | 2010-01-26 17:07:29 -0500 | [diff] [blame] | 1433 | as->urb->transfer_buffer_length, 0, SUBMIT, |
Hans de Goede | 3d97ff6 | 2012-07-04 09:18:03 +0200 | [diff] [blame^] | 1434 | NULL, 0); |
| 1435 | if (!is_in) |
| 1436 | snoop_urb_data(as->urb, as->urb->transfer_buffer_length); |
| 1437 | |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1438 | async_newpending(as); |
Alan Stern | 01c6460 | 2009-09-01 11:09:56 -0400 | [diff] [blame] | 1439 | |
| 1440 | if (usb_endpoint_xfer_bulk(&ep->desc)) { |
| 1441 | spin_lock_irq(&ps->lock); |
| 1442 | |
| 1443 | /* Not exactly the endpoint address; the direction bit is |
| 1444 | * shifted to the 0x10 position so that the value will be |
| 1445 | * between 0 and 31. |
| 1446 | */ |
| 1447 | as->bulk_addr = usb_endpoint_num(&ep->desc) | |
| 1448 | ((ep->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK) |
| 1449 | >> 3); |
| 1450 | |
| 1451 | /* If this bulk URB is the start of a new transfer, re-enable |
| 1452 | * the endpoint. Otherwise mark it as a continuation URB. |
| 1453 | */ |
| 1454 | if (uurb->flags & USBDEVFS_URB_BULK_CONTINUATION) |
| 1455 | as->bulk_status = AS_CONTINUATION; |
| 1456 | else |
| 1457 | ps->disabled_bulk_eps &= ~(1 << as->bulk_addr); |
| 1458 | |
| 1459 | /* Don't accept continuation URBs if the endpoint is |
| 1460 | * disabled because of an earlier error. |
| 1461 | */ |
| 1462 | if (ps->disabled_bulk_eps & (1 << as->bulk_addr)) |
| 1463 | ret = -EREMOTEIO; |
| 1464 | else |
| 1465 | ret = usb_submit_urb(as->urb, GFP_ATOMIC); |
| 1466 | spin_unlock_irq(&ps->lock); |
| 1467 | } else { |
| 1468 | ret = usb_submit_urb(as->urb, GFP_KERNEL); |
| 1469 | } |
| 1470 | |
| 1471 | if (ret) { |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1472 | dev_printk(KERN_DEBUG, &ps->dev->dev, |
| 1473 | "usbfs: usb_submit_urb returned %d\n", ret); |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 1474 | snoop_urb(ps->dev, as->userurb, as->urb->pipe, |
Chris Frey | 0880aef | 2010-01-26 17:07:29 -0500 | [diff] [blame] | 1475 | 0, ret, COMPLETE, NULL, 0); |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1476 | async_removepending(as); |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 1477 | goto error; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1478 | } |
| 1479 | return 0; |
Alan Stern | 52fb743 | 2011-11-17 16:41:14 -0500 | [diff] [blame] | 1480 | |
| 1481 | error: |
| 1482 | kfree(isopkt); |
| 1483 | kfree(dr); |
| 1484 | if (as) |
| 1485 | free_async(as); |
| 1486 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | static int proc_submiturb(struct dev_state *ps, void __user *arg) |
| 1490 | { |
| 1491 | struct usbdevfs_urb uurb; |
| 1492 | |
| 1493 | if (copy_from_user(&uurb, arg, sizeof(uurb))) |
| 1494 | return -EFAULT; |
| 1495 | |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1496 | return proc_do_submiturb(ps, &uurb, |
| 1497 | (((struct usbdevfs_urb __user *)arg)->iso_frame_desc), |
| 1498 | arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | static int proc_unlinkurb(struct dev_state *ps, void __user *arg) |
| 1502 | { |
Huajun Li | 4e09dcf | 2012-05-18 20:12:51 +0800 | [diff] [blame] | 1503 | struct urb *urb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | struct async *as; |
Huajun Li | 4e09dcf | 2012-05-18 20:12:51 +0800 | [diff] [blame] | 1505 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | |
Huajun Li | 4e09dcf | 2012-05-18 20:12:51 +0800 | [diff] [blame] | 1507 | spin_lock_irqsave(&ps->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | as = async_getpending(ps, arg); |
Huajun Li | 4e09dcf | 2012-05-18 20:12:51 +0800 | [diff] [blame] | 1509 | if (!as) { |
| 1510 | spin_unlock_irqrestore(&ps->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | return -EINVAL; |
Huajun Li | 4e09dcf | 2012-05-18 20:12:51 +0800 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | urb = as->urb; |
| 1515 | usb_get_urb(urb); |
| 1516 | spin_unlock_irqrestore(&ps->lock, flags); |
| 1517 | |
| 1518 | usb_kill_urb(urb); |
| 1519 | usb_put_urb(urb); |
| 1520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | return 0; |
| 1522 | } |
| 1523 | |
| 1524 | static int processcompl(struct async *as, void __user * __user *arg) |
| 1525 | { |
| 1526 | struct urb *urb = as->urb; |
| 1527 | struct usbdevfs_urb __user *userurb = as->userurb; |
| 1528 | void __user *addr = as->userurb; |
| 1529 | unsigned int i; |
| 1530 | |
Alan Stern | 7152b59 | 2010-03-06 15:04:03 -0500 | [diff] [blame] | 1531 | if (as->userbuffer && urb->actual_length) { |
Hans de Goede | 3d97ff6 | 2012-07-04 09:18:03 +0200 | [diff] [blame^] | 1532 | if (copy_urb_data_to_user(as->userbuffer, urb)) |
Oliver Neukum | d794a02 | 2009-06-28 23:34:14 +0200 | [diff] [blame] | 1533 | goto err_out; |
Alan Stern | 7152b59 | 2010-03-06 15:04:03 -0500 | [diff] [blame] | 1534 | } |
Alan Stern | e015268 | 2007-08-24 15:42:52 -0400 | [diff] [blame] | 1535 | if (put_user(as->status, &userurb->status)) |
Oliver Neukum | d794a02 | 2009-06-28 23:34:14 +0200 | [diff] [blame] | 1536 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | if (put_user(urb->actual_length, &userurb->actual_length)) |
Oliver Neukum | d794a02 | 2009-06-28 23:34:14 +0200 | [diff] [blame] | 1538 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | if (put_user(urb->error_count, &userurb->error_count)) |
Oliver Neukum | d794a02 | 2009-06-28 23:34:14 +0200 | [diff] [blame] | 1540 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1542 | if (usb_endpoint_xfer_isoc(&urb->ep->desc)) { |
Christopher Li | 668a954 | 2005-04-18 17:39:26 -0700 | [diff] [blame] | 1543 | for (i = 0; i < urb->number_of_packets; i++) { |
| 1544 | if (put_user(urb->iso_frame_desc[i].actual_length, |
| 1545 | &userurb->iso_frame_desc[i].actual_length)) |
Oliver Neukum | d794a02 | 2009-06-28 23:34:14 +0200 | [diff] [blame] | 1546 | goto err_out; |
Christopher Li | 668a954 | 2005-04-18 17:39:26 -0700 | [diff] [blame] | 1547 | if (put_user(urb->iso_frame_desc[i].status, |
| 1548 | &userurb->iso_frame_desc[i].status)) |
Oliver Neukum | d794a02 | 2009-06-28 23:34:14 +0200 | [diff] [blame] | 1549 | goto err_out; |
Christopher Li | 668a954 | 2005-04-18 17:39:26 -0700 | [diff] [blame] | 1550 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | } |
| 1552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | if (put_user(addr, (void __user * __user *)arg)) |
| 1554 | return -EFAULT; |
| 1555 | return 0; |
Oliver Neukum | d794a02 | 2009-06-28 23:34:14 +0200 | [diff] [blame] | 1556 | |
| 1557 | err_out: |
Oliver Neukum | d794a02 | 2009-06-28 23:34:14 +0200 | [diff] [blame] | 1558 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1561 | static struct async *reap_as(struct dev_state *ps) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | { |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1563 | DECLARE_WAITQUEUE(wait, current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | struct async *as = NULL; |
| 1565 | struct usb_device *dev = ps->dev; |
| 1566 | |
| 1567 | add_wait_queue(&ps->wait, &wait); |
| 1568 | for (;;) { |
| 1569 | __set_current_state(TASK_INTERRUPTIBLE); |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1570 | as = async_getcompleted(ps); |
| 1571 | if (as) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | break; |
| 1573 | if (signal_pending(current)) |
| 1574 | break; |
| 1575 | usb_unlock_device(dev); |
| 1576 | schedule(); |
| 1577 | usb_lock_device(dev); |
| 1578 | } |
| 1579 | remove_wait_queue(&ps->wait, &wait); |
| 1580 | set_current_state(TASK_RUNNING); |
| 1581 | return as; |
| 1582 | } |
| 1583 | |
| 1584 | static int proc_reapurb(struct dev_state *ps, void __user *arg) |
| 1585 | { |
| 1586 | struct async *as = reap_as(ps); |
Linus Torvalds | ddeee0b | 2010-02-16 12:35:07 -0800 | [diff] [blame] | 1587 | if (as) { |
| 1588 | int retval = processcompl(as, (void __user * __user *)arg); |
| 1589 | free_async(as); |
| 1590 | return retval; |
| 1591 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | if (signal_pending(current)) |
| 1593 | return -EINTR; |
| 1594 | return -EIO; |
| 1595 | } |
| 1596 | |
| 1597 | static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg) |
| 1598 | { |
Linus Torvalds | ddeee0b | 2010-02-16 12:35:07 -0800 | [diff] [blame] | 1599 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | struct async *as; |
| 1601 | |
Linus Torvalds | ddeee0b | 2010-02-16 12:35:07 -0800 | [diff] [blame] | 1602 | as = async_getcompleted(ps); |
| 1603 | retval = -EAGAIN; |
| 1604 | if (as) { |
| 1605 | retval = processcompl(as, (void __user * __user *)arg); |
| 1606 | free_async(as); |
| 1607 | } |
| 1608 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | #ifdef CONFIG_COMPAT |
Arnd Bergmann | 637e8a6 | 2009-11-14 02:28:05 +0100 | [diff] [blame] | 1612 | static int proc_control_compat(struct dev_state *ps, |
| 1613 | struct usbdevfs_ctrltransfer32 __user *p32) |
| 1614 | { |
| 1615 | struct usbdevfs_ctrltransfer __user *p; |
| 1616 | __u32 udata; |
| 1617 | p = compat_alloc_user_space(sizeof(*p)); |
| 1618 | if (copy_in_user(p, p32, (sizeof(*p32) - sizeof(compat_caddr_t))) || |
| 1619 | get_user(udata, &p32->data) || |
| 1620 | put_user(compat_ptr(udata), &p->data)) |
| 1621 | return -EFAULT; |
| 1622 | return proc_control(ps, p); |
| 1623 | } |
| 1624 | |
| 1625 | static int proc_bulk_compat(struct dev_state *ps, |
| 1626 | struct usbdevfs_bulktransfer32 __user *p32) |
| 1627 | { |
| 1628 | struct usbdevfs_bulktransfer __user *p; |
| 1629 | compat_uint_t n; |
| 1630 | compat_caddr_t addr; |
| 1631 | |
| 1632 | p = compat_alloc_user_space(sizeof(*p)); |
| 1633 | |
| 1634 | if (get_user(n, &p32->ep) || put_user(n, &p->ep) || |
| 1635 | get_user(n, &p32->len) || put_user(n, &p->len) || |
| 1636 | get_user(n, &p32->timeout) || put_user(n, &p->timeout) || |
| 1637 | get_user(addr, &p32->data) || put_user(compat_ptr(addr), &p->data)) |
| 1638 | return -EFAULT; |
| 1639 | |
| 1640 | return proc_bulk(ps, p); |
| 1641 | } |
| 1642 | static int proc_disconnectsignal_compat(struct dev_state *ps, void __user *arg) |
| 1643 | { |
| 1644 | struct usbdevfs_disconnectsignal32 ds; |
| 1645 | |
| 1646 | if (copy_from_user(&ds, arg, sizeof(ds))) |
| 1647 | return -EFAULT; |
| 1648 | ps->discsignr = ds.signr; |
| 1649 | ps->disccontext = compat_ptr(ds.context); |
| 1650 | return 0; |
| 1651 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | |
| 1653 | static int get_urb32(struct usbdevfs_urb *kurb, |
| 1654 | struct usbdevfs_urb32 __user *uurb) |
| 1655 | { |
| 1656 | __u32 uptr; |
Michael Buesch | 18753eb | 2009-07-29 11:39:03 +0200 | [diff] [blame] | 1657 | if (!access_ok(VERIFY_READ, uurb, sizeof(*uurb)) || |
| 1658 | __get_user(kurb->type, &uurb->type) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | __get_user(kurb->endpoint, &uurb->endpoint) || |
| 1660 | __get_user(kurb->status, &uurb->status) || |
| 1661 | __get_user(kurb->flags, &uurb->flags) || |
| 1662 | __get_user(kurb->buffer_length, &uurb->buffer_length) || |
| 1663 | __get_user(kurb->actual_length, &uurb->actual_length) || |
| 1664 | __get_user(kurb->start_frame, &uurb->start_frame) || |
| 1665 | __get_user(kurb->number_of_packets, &uurb->number_of_packets) || |
| 1666 | __get_user(kurb->error_count, &uurb->error_count) || |
| 1667 | __get_user(kurb->signr, &uurb->signr)) |
| 1668 | return -EFAULT; |
| 1669 | |
| 1670 | if (__get_user(uptr, &uurb->buffer)) |
| 1671 | return -EFAULT; |
| 1672 | kurb->buffer = compat_ptr(uptr); |
Mark Lord | ed0c772 | 2009-01-02 02:48:24 -0500 | [diff] [blame] | 1673 | if (__get_user(uptr, &uurb->usercontext)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | return -EFAULT; |
| 1675 | kurb->usercontext = compat_ptr(uptr); |
| 1676 | |
| 1677 | return 0; |
| 1678 | } |
| 1679 | |
| 1680 | static int proc_submiturb_compat(struct dev_state *ps, void __user *arg) |
| 1681 | { |
| 1682 | struct usbdevfs_urb uurb; |
| 1683 | |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1684 | if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | return -EFAULT; |
| 1686 | |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1687 | return proc_do_submiturb(ps, &uurb, |
| 1688 | ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc, |
| 1689 | arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | static int processcompl_compat(struct async *as, void __user * __user *arg) |
| 1693 | { |
| 1694 | struct urb *urb = as->urb; |
| 1695 | struct usbdevfs_urb32 __user *userurb = as->userurb; |
| 1696 | void __user *addr = as->userurb; |
| 1697 | unsigned int i; |
| 1698 | |
Hans de Goede | 2102e06 | 2012-07-04 09:18:01 +0200 | [diff] [blame] | 1699 | if (as->userbuffer && urb->actual_length) { |
Hans de Goede | 3d97ff6 | 2012-07-04 09:18:03 +0200 | [diff] [blame^] | 1700 | if (copy_urb_data_to_user(as->userbuffer, urb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | return -EFAULT; |
Hans de Goede | 2102e06 | 2012-07-04 09:18:01 +0200 | [diff] [blame] | 1702 | } |
Alan Stern | e015268 | 2007-08-24 15:42:52 -0400 | [diff] [blame] | 1703 | if (put_user(as->status, &userurb->status)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | return -EFAULT; |
| 1705 | if (put_user(urb->actual_length, &userurb->actual_length)) |
| 1706 | return -EFAULT; |
| 1707 | if (put_user(urb->error_count, &userurb->error_count)) |
| 1708 | return -EFAULT; |
| 1709 | |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1710 | if (usb_endpoint_xfer_isoc(&urb->ep->desc)) { |
Christopher Li | 668a954 | 2005-04-18 17:39:26 -0700 | [diff] [blame] | 1711 | for (i = 0; i < urb->number_of_packets; i++) { |
| 1712 | if (put_user(urb->iso_frame_desc[i].actual_length, |
| 1713 | &userurb->iso_frame_desc[i].actual_length)) |
| 1714 | return -EFAULT; |
| 1715 | if (put_user(urb->iso_frame_desc[i].status, |
| 1716 | &userurb->iso_frame_desc[i].status)) |
| 1717 | return -EFAULT; |
| 1718 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | } |
| 1720 | |
Al Viro | c714de5 | 2006-10-10 22:45:37 +0100 | [diff] [blame] | 1721 | if (put_user(ptr_to_compat(addr), (u32 __user *)arg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | return -EFAULT; |
| 1723 | return 0; |
| 1724 | } |
| 1725 | |
| 1726 | static int proc_reapurb_compat(struct dev_state *ps, void __user *arg) |
| 1727 | { |
| 1728 | struct async *as = reap_as(ps); |
Linus Torvalds | ddeee0b | 2010-02-16 12:35:07 -0800 | [diff] [blame] | 1729 | if (as) { |
| 1730 | int retval = processcompl_compat(as, (void __user * __user *)arg); |
| 1731 | free_async(as); |
| 1732 | return retval; |
| 1733 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | if (signal_pending(current)) |
| 1735 | return -EINTR; |
| 1736 | return -EIO; |
| 1737 | } |
| 1738 | |
| 1739 | static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg) |
| 1740 | { |
Linus Torvalds | ddeee0b | 2010-02-16 12:35:07 -0800 | [diff] [blame] | 1741 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | struct async *as; |
| 1743 | |
Linus Torvalds | ddeee0b | 2010-02-16 12:35:07 -0800 | [diff] [blame] | 1744 | retval = -EAGAIN; |
| 1745 | as = async_getcompleted(ps); |
| 1746 | if (as) { |
| 1747 | retval = processcompl_compat(as, (void __user * __user *)arg); |
| 1748 | free_async(as); |
| 1749 | } |
| 1750 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | } |
| 1752 | |
Arnd Bergmann | 637e8a6 | 2009-11-14 02:28:05 +0100 | [diff] [blame] | 1753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | #endif |
| 1755 | |
| 1756 | static int proc_disconnectsignal(struct dev_state *ps, void __user *arg) |
| 1757 | { |
| 1758 | struct usbdevfs_disconnectsignal ds; |
| 1759 | |
| 1760 | if (copy_from_user(&ds, arg, sizeof(ds))) |
| 1761 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | ps->discsignr = ds.signr; |
| 1763 | ps->disccontext = ds.context; |
| 1764 | return 0; |
| 1765 | } |
| 1766 | |
| 1767 | static int proc_claiminterface(struct dev_state *ps, void __user *arg) |
| 1768 | { |
| 1769 | unsigned int ifnum; |
| 1770 | |
| 1771 | if (get_user(ifnum, (unsigned int __user *)arg)) |
| 1772 | return -EFAULT; |
| 1773 | return claimintf(ps, ifnum); |
| 1774 | } |
| 1775 | |
| 1776 | static int proc_releaseinterface(struct dev_state *ps, void __user *arg) |
| 1777 | { |
| 1778 | unsigned int ifnum; |
| 1779 | int ret; |
| 1780 | |
| 1781 | if (get_user(ifnum, (unsigned int __user *)arg)) |
| 1782 | return -EFAULT; |
| 1783 | if ((ret = releaseintf(ps, ifnum)) < 0) |
| 1784 | return ret; |
| 1785 | destroy_async_on_interface (ps, ifnum); |
| 1786 | return 0; |
| 1787 | } |
| 1788 | |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1789 | static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | int size; |
| 1792 | void *buf = NULL; |
| 1793 | int retval = 0; |
| 1794 | struct usb_interface *intf = NULL; |
| 1795 | struct usb_driver *driver = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1797 | /* alloc buffer */ |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1798 | if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) { |
| 1799 | if ((buf = kmalloc(size, GFP_KERNEL)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | return -ENOMEM; |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1801 | if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) { |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1802 | if (copy_from_user(buf, ctl->data, size)) { |
Jesper Juhl | 6fd19f4 | 2005-04-18 17:39:33 -0700 | [diff] [blame] | 1803 | kfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | return -EFAULT; |
| 1805 | } |
| 1806 | } else { |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1807 | memset(buf, 0, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | } |
| 1809 | } |
| 1810 | |
Alan Stern | 349710c | 2006-07-01 22:05:56 -0400 | [diff] [blame] | 1811 | if (!connected(ps)) { |
Jesper Juhl | 6fd19f4 | 2005-04-18 17:39:33 -0700 | [diff] [blame] | 1812 | kfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | return -ENODEV; |
| 1814 | } |
| 1815 | |
| 1816 | if (ps->dev->state != USB_STATE_CONFIGURED) |
| 1817 | retval = -EHOSTUNREACH; |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1818 | else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno))) |
| 1819 | retval = -EINVAL; |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1820 | else switch (ctl->ioctl_code) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | |
| 1822 | /* disconnect kernel driver from interface */ |
| 1823 | case USBDEVFS_DISCONNECT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | if (intf->dev.driver) { |
| 1825 | driver = to_usb_driver(intf->dev.driver); |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1826 | dev_dbg(&intf->dev, "disconnect by usbfs\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | usb_driver_release_interface(driver, intf); |
| 1828 | } else |
| 1829 | retval = -ENODATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | break; |
| 1831 | |
| 1832 | /* let kernel drivers try to (re)bind to the interface */ |
| 1833 | case USBDEVFS_CONNECT: |
Alan Stern | 885e974 | 2007-12-03 15:42:10 -0500 | [diff] [blame] | 1834 | if (!intf->dev.driver) |
| 1835 | retval = device_attach(&intf->dev); |
| 1836 | else |
| 1837 | retval = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | break; |
| 1839 | |
| 1840 | /* talk directly to the interface's driver */ |
| 1841 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | if (intf->dev.driver) |
| 1843 | driver = to_usb_driver(intf->dev.driver); |
Andi Kleen | c532b29 | 2010-06-01 23:04:41 +0200 | [diff] [blame] | 1844 | if (driver == NULL || driver->unlocked_ioctl == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | retval = -ENOTTY; |
| 1846 | } else { |
Andi Kleen | c532b29 | 2010-06-01 23:04:41 +0200 | [diff] [blame] | 1847 | retval = driver->unlocked_ioctl(intf, ctl->ioctl_code, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | if (retval == -ENOIOCTLCMD) |
| 1849 | retval = -ENOTTY; |
| 1850 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | /* cleanup and return */ |
| 1854 | if (retval >= 0 |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1855 | && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | && size > 0 |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1857 | && copy_to_user(ctl->data, buf, size) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | retval = -EFAULT; |
Jesper Juhl | 6fd19f4 | 2005-04-18 17:39:33 -0700 | [diff] [blame] | 1859 | |
| 1860 | kfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | return retval; |
| 1862 | } |
| 1863 | |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1864 | static int proc_ioctl_default(struct dev_state *ps, void __user *arg) |
| 1865 | { |
| 1866 | struct usbdevfs_ioctl ctrl; |
| 1867 | |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1868 | if (copy_from_user(&ctrl, arg, sizeof(ctrl))) |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1869 | return -EFAULT; |
| 1870 | return proc_ioctl(ps, &ctrl); |
| 1871 | } |
| 1872 | |
| 1873 | #ifdef CONFIG_COMPAT |
Andrew Morton | 777da59 | 2005-11-17 09:47:02 -0800 | [diff] [blame] | 1874 | static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg) |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1875 | { |
| 1876 | struct usbdevfs_ioctl32 __user *uioc; |
| 1877 | struct usbdevfs_ioctl ctrl; |
| 1878 | u32 udata; |
| 1879 | |
Andrew Morton | 058120d | 2005-11-17 09:47:49 -0800 | [diff] [blame] | 1880 | uioc = compat_ptr((long)arg); |
Michael Buesch | 18753eb | 2009-07-29 11:39:03 +0200 | [diff] [blame] | 1881 | if (!access_ok(VERIFY_READ, uioc, sizeof(*uioc)) || |
| 1882 | __get_user(ctrl.ifno, &uioc->ifno) || |
| 1883 | __get_user(ctrl.ioctl_code, &uioc->ioctl_code) || |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1884 | __get_user(udata, &uioc->data)) |
| 1885 | return -EFAULT; |
| 1886 | ctrl.data = compat_ptr(udata); |
| 1887 | |
| 1888 | return proc_ioctl(ps, &ctrl); |
| 1889 | } |
| 1890 | #endif |
| 1891 | |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1892 | static int proc_claim_port(struct dev_state *ps, void __user *arg) |
| 1893 | { |
| 1894 | unsigned portnum; |
| 1895 | int rc; |
| 1896 | |
| 1897 | if (get_user(portnum, (unsigned __user *) arg)) |
| 1898 | return -EFAULT; |
| 1899 | rc = usb_hub_claim_port(ps->dev, portnum, ps); |
| 1900 | if (rc == 0) |
| 1901 | snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n", |
| 1902 | portnum, task_pid_nr(current), current->comm); |
| 1903 | return rc; |
| 1904 | } |
| 1905 | |
| 1906 | static int proc_release_port(struct dev_state *ps, void __user *arg) |
| 1907 | { |
| 1908 | unsigned portnum; |
| 1909 | |
| 1910 | if (get_user(portnum, (unsigned __user *) arg)) |
| 1911 | return -EFAULT; |
| 1912 | return usb_hub_release_port(ps->dev, portnum, ps); |
| 1913 | } |
| 1914 | |
Hans de Goede | 19181bc | 2012-07-04 09:18:02 +0200 | [diff] [blame] | 1915 | static int proc_get_capabilities(struct dev_state *ps, void __user *arg) |
| 1916 | { |
| 1917 | __u32 caps; |
| 1918 | |
| 1919 | caps = USBDEVFS_CAP_ZERO_PACKET | USBDEVFS_CAP_NO_PACKET_SIZE_LIM; |
| 1920 | if (!ps->dev->bus->no_stop_on_short) |
| 1921 | caps |= USBDEVFS_CAP_BULK_CONTINUATION; |
Hans de Goede | 3d97ff6 | 2012-07-04 09:18:03 +0200 | [diff] [blame^] | 1922 | if (ps->dev->bus->sg_tablesize) |
| 1923 | caps |= USBDEVFS_CAP_BULK_SCATTER_GATHER; |
Hans de Goede | 19181bc | 2012-07-04 09:18:02 +0200 | [diff] [blame] | 1924 | |
| 1925 | if (put_user(caps, (__u32 __user *)arg)) |
| 1926 | return -EFAULT; |
| 1927 | |
| 1928 | return 0; |
| 1929 | } |
| 1930 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | /* |
| 1932 | * NOTE: All requests here that have interface numbers as parameters |
| 1933 | * are assuming that somehow the configuration has been prevented from |
| 1934 | * changing. But there's no mechanism to ensure that... |
| 1935 | */ |
Arnd Bergmann | 637e8a6 | 2009-11-14 02:28:05 +0100 | [diff] [blame] | 1936 | static long usbdev_do_ioctl(struct file *file, unsigned int cmd, |
| 1937 | void __user *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | { |
Tobias Klauser | ec17cf1 | 2006-09-13 21:38:41 +0200 | [diff] [blame] | 1939 | struct dev_state *ps = file->private_data; |
Arnd Bergmann | 637e8a6 | 2009-11-14 02:28:05 +0100 | [diff] [blame] | 1940 | struct inode *inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | struct usb_device *dev = ps->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | int ret = -ENOTTY; |
| 1943 | |
| 1944 | if (!(file->f_mode & FMODE_WRITE)) |
| 1945 | return -EPERM; |
Oliver Neukum | 01412a2 | 2010-01-13 15:33:43 +0100 | [diff] [blame] | 1946 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | usb_lock_device(dev); |
Alan Stern | 349710c | 2006-07-01 22:05:56 -0400 | [diff] [blame] | 1948 | if (!connected(ps)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | usb_unlock_device(dev); |
| 1950 | return -ENODEV; |
| 1951 | } |
| 1952 | |
| 1953 | switch (cmd) { |
| 1954 | case USBDEVFS_CONTROL: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1955 | snoop(&dev->dev, "%s: CONTROL\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | ret = proc_control(ps, p); |
| 1957 | if (ret >= 0) |
| 1958 | inode->i_mtime = CURRENT_TIME; |
| 1959 | break; |
| 1960 | |
| 1961 | case USBDEVFS_BULK: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1962 | snoop(&dev->dev, "%s: BULK\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | ret = proc_bulk(ps, p); |
| 1964 | if (ret >= 0) |
| 1965 | inode->i_mtime = CURRENT_TIME; |
| 1966 | break; |
| 1967 | |
| 1968 | case USBDEVFS_RESETEP: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1969 | snoop(&dev->dev, "%s: RESETEP\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | ret = proc_resetep(ps, p); |
| 1971 | if (ret >= 0) |
| 1972 | inode->i_mtime = CURRENT_TIME; |
| 1973 | break; |
| 1974 | |
| 1975 | case USBDEVFS_RESET: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1976 | snoop(&dev->dev, "%s: RESET\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | ret = proc_resetdevice(ps); |
| 1978 | break; |
| 1979 | |
| 1980 | case USBDEVFS_CLEAR_HALT: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1981 | snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | ret = proc_clearhalt(ps, p); |
| 1983 | if (ret >= 0) |
| 1984 | inode->i_mtime = CURRENT_TIME; |
| 1985 | break; |
| 1986 | |
| 1987 | case USBDEVFS_GETDRIVER: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1988 | snoop(&dev->dev, "%s: GETDRIVER\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | ret = proc_getdriver(ps, p); |
| 1990 | break; |
| 1991 | |
| 1992 | case USBDEVFS_CONNECTINFO: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1993 | snoop(&dev->dev, "%s: CONNECTINFO\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | ret = proc_connectinfo(ps, p); |
| 1995 | break; |
| 1996 | |
| 1997 | case USBDEVFS_SETINTERFACE: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1998 | snoop(&dev->dev, "%s: SETINTERFACE\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | ret = proc_setintf(ps, p); |
| 2000 | break; |
| 2001 | |
| 2002 | case USBDEVFS_SETCONFIGURATION: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 2003 | snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | ret = proc_setconfig(ps, p); |
| 2005 | break; |
| 2006 | |
| 2007 | case USBDEVFS_SUBMITURB: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 2008 | snoop(&dev->dev, "%s: SUBMITURB\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | ret = proc_submiturb(ps, p); |
| 2010 | if (ret >= 0) |
| 2011 | inode->i_mtime = CURRENT_TIME; |
| 2012 | break; |
| 2013 | |
| 2014 | #ifdef CONFIG_COMPAT |
Arnd Bergmann | 637e8a6 | 2009-11-14 02:28:05 +0100 | [diff] [blame] | 2015 | case USBDEVFS_CONTROL32: |
| 2016 | snoop(&dev->dev, "%s: CONTROL32\n", __func__); |
| 2017 | ret = proc_control_compat(ps, p); |
| 2018 | if (ret >= 0) |
| 2019 | inode->i_mtime = CURRENT_TIME; |
| 2020 | break; |
| 2021 | |
| 2022 | case USBDEVFS_BULK32: |
| 2023 | snoop(&dev->dev, "%s: BULK32\n", __func__); |
| 2024 | ret = proc_bulk_compat(ps, p); |
| 2025 | if (ret >= 0) |
| 2026 | inode->i_mtime = CURRENT_TIME; |
| 2027 | break; |
| 2028 | |
| 2029 | case USBDEVFS_DISCSIGNAL32: |
| 2030 | snoop(&dev->dev, "%s: DISCSIGNAL32\n", __func__); |
| 2031 | ret = proc_disconnectsignal_compat(ps, p); |
| 2032 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | |
| 2034 | case USBDEVFS_SUBMITURB32: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 2035 | snoop(&dev->dev, "%s: SUBMITURB32\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | ret = proc_submiturb_compat(ps, p); |
| 2037 | if (ret >= 0) |
| 2038 | inode->i_mtime = CURRENT_TIME; |
| 2039 | break; |
| 2040 | |
| 2041 | case USBDEVFS_REAPURB32: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 2042 | snoop(&dev->dev, "%s: REAPURB32\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | ret = proc_reapurb_compat(ps, p); |
| 2044 | break; |
| 2045 | |
| 2046 | case USBDEVFS_REAPURBNDELAY32: |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 2047 | snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | ret = proc_reapurbnonblock_compat(ps, p); |
| 2049 | break; |
| 2050 | |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 2051 | case USBDEVFS_IOCTL32: |
Arnd Bergmann | 637e8a6 | 2009-11-14 02:28:05 +0100 | [diff] [blame] | 2052 | snoop(&dev->dev, "%s: IOCTL32\n", __func__); |
Al Viro | c714de5 | 2006-10-10 22:45:37 +0100 | [diff] [blame] | 2053 | ret = proc_ioctl_compat(ps, ptr_to_compat(p)); |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 2054 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | #endif |
| 2056 | |
| 2057 | case USBDEVFS_DISCARDURB: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 2058 | snoop(&dev->dev, "%s: DISCARDURB\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | ret = proc_unlinkurb(ps, p); |
| 2060 | break; |
| 2061 | |
| 2062 | case USBDEVFS_REAPURB: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 2063 | snoop(&dev->dev, "%s: REAPURB\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | ret = proc_reapurb(ps, p); |
| 2065 | break; |
| 2066 | |
| 2067 | case USBDEVFS_REAPURBNDELAY: |
Alan Stern | 4c6e897 | 2009-06-29 11:02:04 -0400 | [diff] [blame] | 2068 | snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | ret = proc_reapurbnonblock(ps, p); |
| 2070 | break; |
| 2071 | |
| 2072 | case USBDEVFS_DISCSIGNAL: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 2073 | snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | ret = proc_disconnectsignal(ps, p); |
| 2075 | break; |
| 2076 | |
| 2077 | case USBDEVFS_CLAIMINTERFACE: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 2078 | snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | ret = proc_claiminterface(ps, p); |
| 2080 | break; |
| 2081 | |
| 2082 | case USBDEVFS_RELEASEINTERFACE: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 2083 | snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | ret = proc_releaseinterface(ps, p); |
| 2085 | break; |
| 2086 | |
| 2087 | case USBDEVFS_IOCTL: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 2088 | snoop(&dev->dev, "%s: IOCTL\n", __func__); |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 2089 | ret = proc_ioctl_default(ps, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | break; |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 2091 | |
| 2092 | case USBDEVFS_CLAIM_PORT: |
| 2093 | snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__); |
| 2094 | ret = proc_claim_port(ps, p); |
| 2095 | break; |
| 2096 | |
| 2097 | case USBDEVFS_RELEASE_PORT: |
| 2098 | snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__); |
| 2099 | ret = proc_release_port(ps, p); |
| 2100 | break; |
Hans de Goede | 19181bc | 2012-07-04 09:18:02 +0200 | [diff] [blame] | 2101 | case USBDEVFS_GET_CAPABILITIES: |
| 2102 | ret = proc_get_capabilities(ps, p); |
| 2103 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | } |
| 2105 | usb_unlock_device(dev); |
| 2106 | if (ret >= 0) |
| 2107 | inode->i_atime = CURRENT_TIME; |
| 2108 | return ret; |
| 2109 | } |
| 2110 | |
Arnd Bergmann | 637e8a6 | 2009-11-14 02:28:05 +0100 | [diff] [blame] | 2111 | static long usbdev_ioctl(struct file *file, unsigned int cmd, |
| 2112 | unsigned long arg) |
| 2113 | { |
| 2114 | int ret; |
| 2115 | |
Arnd Bergmann | 637e8a6 | 2009-11-14 02:28:05 +0100 | [diff] [blame] | 2116 | ret = usbdev_do_ioctl(file, cmd, (void __user *)arg); |
Arnd Bergmann | 637e8a6 | 2009-11-14 02:28:05 +0100 | [diff] [blame] | 2117 | |
| 2118 | return ret; |
| 2119 | } |
| 2120 | |
| 2121 | #ifdef CONFIG_COMPAT |
| 2122 | static long usbdev_compat_ioctl(struct file *file, unsigned int cmd, |
| 2123 | unsigned long arg) |
| 2124 | { |
| 2125 | int ret; |
| 2126 | |
Arnd Bergmann | 637e8a6 | 2009-11-14 02:28:05 +0100 | [diff] [blame] | 2127 | ret = usbdev_do_ioctl(file, cmd, compat_ptr(arg)); |
Arnd Bergmann | 637e8a6 | 2009-11-14 02:28:05 +0100 | [diff] [blame] | 2128 | |
| 2129 | return ret; |
| 2130 | } |
| 2131 | #endif |
| 2132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | /* No kernel lock - fine */ |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 2134 | static unsigned int usbdev_poll(struct file *file, |
| 2135 | struct poll_table_struct *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | { |
Tobias Klauser | ec17cf1 | 2006-09-13 21:38:41 +0200 | [diff] [blame] | 2137 | struct dev_state *ps = file->private_data; |
| 2138 | unsigned int mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | |
| 2140 | poll_wait(file, &ps->wait, wait); |
| 2141 | if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed)) |
| 2142 | mask |= POLLOUT | POLLWRNORM; |
Alan Stern | 349710c | 2006-07-01 22:05:56 -0400 | [diff] [blame] | 2143 | if (!connected(ps)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | mask |= POLLERR | POLLHUP; |
| 2145 | return mask; |
| 2146 | } |
| 2147 | |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 2148 | const struct file_operations usbdev_file_operations = { |
Arnd Bergmann | 637e8a6 | 2009-11-14 02:28:05 +0100 | [diff] [blame] | 2149 | .owner = THIS_MODULE, |
| 2150 | .llseek = usbdev_lseek, |
| 2151 | .read = usbdev_read, |
| 2152 | .poll = usbdev_poll, |
| 2153 | .unlocked_ioctl = usbdev_ioctl, |
| 2154 | #ifdef CONFIG_COMPAT |
| 2155 | .compat_ioctl = usbdev_compat_ioctl, |
| 2156 | #endif |
| 2157 | .open = usbdev_open, |
| 2158 | .release = usbdev_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | }; |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 2160 | |
Alan Stern | 501950d | 2009-01-13 11:33:42 -0500 | [diff] [blame] | 2161 | static void usbdev_remove(struct usb_device *udev) |
Alan Stern | cd9f037 | 2008-06-24 14:47:04 -0400 | [diff] [blame] | 2162 | { |
| 2163 | struct dev_state *ps; |
| 2164 | struct siginfo sinfo; |
| 2165 | |
| 2166 | while (!list_empty(&udev->filelist)) { |
| 2167 | ps = list_entry(udev->filelist.next, struct dev_state, list); |
| 2168 | destroy_all_async(ps); |
| 2169 | wake_up_all(&ps->wait); |
| 2170 | list_del_init(&ps->list); |
| 2171 | if (ps->discsignr) { |
| 2172 | sinfo.si_signo = ps->discsignr; |
| 2173 | sinfo.si_errno = EPIPE; |
| 2174 | sinfo.si_code = SI_ASYNCIO; |
| 2175 | sinfo.si_addr = ps->disccontext; |
Serge Hallyn | d178bc3 | 2011-09-26 10:45:18 -0500 | [diff] [blame] | 2176 | kill_pid_info_as_cred(ps->discsignr, &sinfo, |
| 2177 | ps->disc_pid, ps->cred, ps->secid); |
Alan Stern | cd9f037 | 2008-06-24 14:47:04 -0400 | [diff] [blame] | 2178 | } |
| 2179 | } |
| 2180 | } |
| 2181 | |
Alan Stern | 501950d | 2009-01-13 11:33:42 -0500 | [diff] [blame] | 2182 | static int usbdev_notify(struct notifier_block *self, |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 2183 | unsigned long action, void *dev) |
Greg Kroah-Hartman | a7b986b | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 2184 | { |
| 2185 | switch (action) { |
| 2186 | case USB_DEVICE_ADD: |
Greg Kroah-Hartman | a7b986b | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 2187 | break; |
| 2188 | case USB_DEVICE_REMOVE: |
Alan Stern | 501950d | 2009-01-13 11:33:42 -0500 | [diff] [blame] | 2189 | usbdev_remove(dev); |
Greg Kroah-Hartman | a7b986b | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 2190 | break; |
| 2191 | } |
| 2192 | return NOTIFY_OK; |
| 2193 | } |
| 2194 | |
| 2195 | static struct notifier_block usbdev_nb = { |
Alan Stern | 501950d | 2009-01-13 11:33:42 -0500 | [diff] [blame] | 2196 | .notifier_call = usbdev_notify, |
Greg Kroah-Hartman | a7b986b | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 2197 | }; |
| 2198 | |
Greg Kroah-Hartman | 7e7654a | 2007-09-12 15:06:57 -0700 | [diff] [blame] | 2199 | static struct cdev usb_device_cdev; |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 2200 | |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 2201 | int __init usb_devio_init(void) |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 2202 | { |
| 2203 | int retval; |
| 2204 | |
Alan Stern | fad21bd | 2005-08-10 15:15:57 -0400 | [diff] [blame] | 2205 | retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX, |
Greg Kroah-Hartman | 04e482ff | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 2206 | "usb_device"); |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 2207 | if (retval) { |
Greg Kroah-Hartman | 69a8594 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 2208 | printk(KERN_ERR "Unable to register minors for usb_device\n"); |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 2209 | goto out; |
| 2210 | } |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 2211 | cdev_init(&usb_device_cdev, &usbdev_file_operations); |
Alan Stern | fad21bd | 2005-08-10 15:15:57 -0400 | [diff] [blame] | 2212 | retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX); |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 2213 | if (retval) { |
Greg Kroah-Hartman | 69a8594 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 2214 | printk(KERN_ERR "Unable to get usb_device major %d\n", |
| 2215 | USB_DEVICE_MAJOR); |
Greg Kroah-Hartman | a7b986b | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 2216 | goto error_cdev; |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 2217 | } |
Alan Stern | 501950d | 2009-01-13 11:33:42 -0500 | [diff] [blame] | 2218 | usb_register_notify(&usbdev_nb); |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 2219 | out: |
| 2220 | return retval; |
Greg Kroah-Hartman | a7b986b | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 2221 | |
Greg Kroah-Hartman | a7b986b | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 2222 | error_cdev: |
| 2223 | unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX); |
| 2224 | goto out; |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 2225 | } |
| 2226 | |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 2227 | void usb_devio_cleanup(void) |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 2228 | { |
Greg Kroah-Hartman | a7b986b | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 2229 | usb_unregister_notify(&usbdev_nb); |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 2230 | cdev_del(&usb_device_cdev); |
Alan Stern | fad21bd | 2005-08-10 15:15:57 -0400 | [diff] [blame] | 2231 | unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX); |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 2232 | } |