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