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