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