blob: 0ff0feddfd1f9df26a59ac62cc4be07b16c37f6b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************/
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 Torvalds1da177e2005-04-16 15:20:36 -070022 * 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 Welte46113832005-10-10 19:44:29 +020031 * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery
32 * (CAN-2005-3055)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35/*****************************************************************************/
36
37#include <linux/fs.h>
38#include <linux/mm.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010039#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/signal.h>
42#include <linux/poll.h>
43#include <linux/module.h>
Chen Gangb11b2e12013-02-02 15:57:53 +080044#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/usb.h>
46#include <linux/usbdevice_fs.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020047#include <linux/usb/hcd.h> /* for usbcore internals */
Kay Sieversfbf82fd2005-07-31 01:05:53 +020048#include <linux/cdev.h>
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -070049#include <linux/notifier.h>
David Quigley7a019552006-06-30 01:55:48 -070050#include <linux/security.h>
Serge Hallynd178bc32011-09-26 10:45:18 -050051#include <linux/user_namespace.h>
Hans de Goede3d97ff62012-07-04 09:18:03 +020052#include <linux/scatterlist.h>
Tülin İzere6889b32013-05-17 10:12:34 +030053#include <linux/uaccess.h>
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +010054#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/byteorder.h>
56#include <linux/moduleparam.h>
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include "usb.h"
59
Kay Sieversfbf82fd2005-07-31 01:05:53 +020060#define USB_MAXBUS 64
Tülin İzerfa86ad02013-05-17 10:13:24 +030061#define USB_DEVICE_MAX (USB_MAXBUS * 128)
Hans de Goede3d97ff62012-07-04 09:18:03 +020062#define USB_SG_SIZE 16384 /* split-size for large txs */
Kay Sieversfbf82fd2005-07-31 01:05:53 +020063
Alan Stern4a2a8a22006-07-01 22:05:01 -040064/* Mutual exclusion for removal, open, and release */
65DEFINE_MUTEX(usbfs_mutex);
66
Valentina Manea9b6f0c42014-03-10 10:36:40 +020067struct usb_dev_state {
Alan Sterncd9f0372008-06-24 14:47:04 -040068 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. Gundersonf7d34b42016-02-03 22:58:26 +010074 struct list_head memory_list;
Alan Sterncd9f0372008-06-24 14:47:04 -040075 wait_queue_head_t wait; /* wake up if a request completed */
76 unsigned int discsignr;
77 struct pid *disc_pid;
Serge Hallynd178bc32011-09-26 10:45:18 -050078 const struct cred *cred;
Alan Sterncd9f0372008-06-24 14:47:04 -040079 void __user *disccontext;
80 unsigned long ifclaimed;
81 u32 secid;
Alan Stern01c64602009-09-01 11:09:56 -040082 u32 disabled_bulk_eps;
Reilly Grantd883f522016-02-21 18:38:01 -030083 bool privileges_dropped;
84 unsigned long interface_allowed_mask;
Alan Sterncd9f0372008-06-24 14:47:04 -040085};
86
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +010087struct 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 Torvalds1da177e2005-04-16 15:20:36 -070098struct async {
99 struct list_head asynclist;
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200100 struct usb_dev_state *ps;
Eric W. Biederman2425c082006-10-02 02:17:28 -0700101 struct pid *pid;
Serge Hallynd178bc32011-09-26 10:45:18 -0500102 const struct cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned int signr;
104 unsigned int ifnum;
105 void __user *userbuffer;
106 void __user *userurb;
107 struct urb *urb;
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +0100108 struct usb_memory *usbm;
Alan Sternadd1aae2011-11-17 16:41:25 -0500109 unsigned int mem_usage;
Alan Sterne0152682007-08-24 15:42:52 -0400110 int status;
David Quigley7a019552006-06-30 01:55:48 -0700111 u32 secid;
Alan Stern01c64602009-09-01 11:09:56 -0400112 u8 bulk_addr;
113 u8 bulk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030116static bool usbfs_snoop;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800117module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
118MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Alan Stern0290cc92015-11-20 13:53:22 -0500120static unsigned usbfs_snoop_max = 65536;
121module_param(usbfs_snoop_max, uint, S_IRUGO | S_IWUSR);
122MODULE_PARM_DESC(usbfs_snoop_max,
123 "maximum number of bytes to print while snooping");
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define snoop(dev, format, arg...) \
126 do { \
127 if (usbfs_snoop) \
Kris Borerf355e832015-08-07 07:22:44 -0400128 dev_info(dev, format, ## arg); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 } while (0)
130
Alan Stern4c6e8972009-06-29 11:02:04 -0400131enum snoop_when {
132 SUBMIT, COMPLETE
133};
134
Alan Sternfad21bd2005-08-10 15:15:57 -0400135#define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
136
Alan Sternadd1aae2011-11-17 16:41:25 -0500137/* Limit on the total amount of memory we can allocate for transfers */
Mateusz Berezecki1129d272016-12-21 09:19:14 -0800138static u32 usbfs_memory_mb = 16;
Alan Stern3f5eb8d2011-11-17 16:41:35 -0500139module_param(usbfs_memory_mb, uint, 0644);
140MODULE_PARM_DESC(usbfs_memory_mb,
141 "maximum MB allowed for usbfs buffers (0 = no limit)");
142
Mateusz Berezecki1129d272016-12-21 09:19:14 -0800143static atomic64_t usbfs_memory_usage; /* Total memory currently allocated */
Alan Sternadd1aae2011-11-17 16:41:25 -0500144
145/* Check whether it's okay to allocate more memory for a transfer */
Mateusz Berezecki1129d272016-12-21 09:19:14 -0800146static int usbfs_increase_memory_usage(u64 amount)
Alan Sternadd1aae2011-11-17 16:41:25 -0500147{
Mateusz Berezecki1129d272016-12-21 09:19:14 -0800148 u64 lim;
Alan Stern3f5eb8d2011-11-17 16:41:35 -0500149
Alan Stern3f5eb8d2011-11-17 16:41:35 -0500150 lim = ACCESS_ONCE(usbfs_memory_mb);
Mateusz Berezecki1129d272016-12-21 09:19:14 -0800151 lim <<= 20;
Alan Stern3f5eb8d2011-11-17 16:41:35 -0500152
Mateusz Berezecki1129d272016-12-21 09:19:14 -0800153 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 Sternadd1aae2011-11-17 16:41:25 -0500161}
162
163/* Memory for a transfer is being deallocated */
Mateusz Berezecki1129d272016-12-21 09:19:14 -0800164static void usbfs_decrease_memory_usage(u64 amount)
Alan Sternadd1aae2011-11-17 16:41:25 -0500165{
Mateusz Berezecki1129d272016-12-21 09:19:14 -0800166 atomic64_sub(amount, &usbfs_memory_usage);
Alan Sternadd1aae2011-11-17 16:41:25 -0500167}
Alan Stern4c6e8972009-06-29 11:02:04 -0400168
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200169static int connected(struct usb_dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Alan Stern349710c2006-07-01 22:05:56 -0400171 return (!list_empty(&ps->list) &&
172 ps->dev->state != USB_STATE_NOTATTACHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +0100175static 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
196static 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
206static 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 Curti10871c12016-04-27 21:23:07 +0200213static struct vm_operations_struct usbdev_vm_ops = {
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +0100214 .open = usbdev_vm_open,
215 .close = usbdev_vm_close
216};
217
218static 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 Slaby70f7ca92016-06-15 15:56:11 +0200238 mem = usb_alloc_coherent(ps->dev, size, GFP_USER | __GFP_NOWARN,
239 &dma_handle);
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +0100240 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
273error_free_usbm:
274 kfree(usbm);
275error_decrease_mem:
276 usbfs_decrease_memory_usage(size + sizeof(struct usb_memory));
277error:
278 return ret;
279}
280
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800281static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
282 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200284 struct usb_dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 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 Stern349710c2006-07-01 22:05:56 -0400293 if (!connected(ps)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 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-Hartman04e482ff2008-01-30 15:21:33 -0800302 /* 18 bytes - fits on the stack */
303 struct usb_device_descriptor temp_desc;
Oliver Neukum8781ba02006-01-06 21:24:25 +0100304
305 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
Andrew Morton9fcd5c32006-01-18 23:55:07 -0800306 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 Torvalds1da177e2005-04-16 15:20:36 -0700310
311 len = sizeof(struct usb_device_descriptor) - pos;
312 if (len > nbytes)
313 len = nbytes;
Oliver Neukum8781ba02006-01-06 21:24:25 +0100314 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 ret = -EFAULT;
316 goto err;
317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
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
362err:
363 usb_unlock_device(dev);
364 return ret;
365}
366
367/*
368 * async list handling
369 */
370
371static struct async *alloc_async(unsigned int numisoframes)
372{
Pete Zaitcevdd95b812008-01-05 02:01:07 -0800373 struct async *as;
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400374
Pete Zaitcevdd95b812008-01-05 02:01:07 -0800375 as = kzalloc(sizeof(struct async), GFP_KERNEL);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800376 if (!as)
377 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
379 if (!as->urb) {
380 kfree(as);
381 return NULL;
382 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800383 return as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386static void free_async(struct async *as)
387{
Hans de Goede3d97ff62012-07-04 09:18:03 +0200388 int i;
389
Eric W. Biederman2425c082006-10-02 02:17:28 -0700390 put_pid(as->pid);
Sarah Sharp1b41c832011-12-16 11:26:30 -0800391 if (as->cred)
392 put_cred(as->cred);
Hans de Goede3d97ff62012-07-04 09:18:03 +0200393 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. Gundersonf7d34b42016-02-03 22:58:26 +0100397
Hans de Goede3d97ff62012-07-04 09:18:03 +0200398 kfree(as->urb->sg);
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +0100399 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 Juhl6fd19f42005-04-18 17:39:33 -0700404 kfree(as->urb->setup_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 usb_free_urb(as->urb);
Alan Sternadd1aae2011-11-17 16:41:25 -0500406 usbfs_decrease_memory_usage(as->mem_usage);
Jesper Juhl6fd19f42005-04-18 17:39:33 -0700407 kfree(as);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Alan Sternd34d9722009-03-09 13:44:48 -0400410static void async_newpending(struct async *as)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200412 struct usb_dev_state *ps = as->ps;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800413 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 Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Alan Sternd34d9722009-03-09 13:44:48 -0400420static void async_removepending(struct async *as)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200422 struct usb_dev_state *ps = as->ps;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800423 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 Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200430static struct async *async_getcompleted(struct usb_dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800432 unsigned long flags;
433 struct async *as = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800435 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 Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200445static struct async *async_getpending(struct usb_dev_state *ps,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800446 void __user *userurb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800448 struct async *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 list_for_each_entry(as, &ps->async_pending, asynclist)
451 if (as->userurb == userurb) {
452 list_del_init(&as->asynclist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return as;
454 }
Huajun Li4e09dcf2012-05-18 20:12:51 +0800455
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800456 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Alan Stern4c6e8972009-06-29 11:02:04 -0400459static void snoop_urb(struct usb_device *udev,
460 void __user *userurb, int pipe, unsigned length,
Chris Frey0880aef2010-01-26 17:07:29 -0500461 int timeout_or_status, enum snoop_when when,
462 unsigned char *data, unsigned data_len)
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700463{
Alan Stern4c6e8972009-06-29 11:02:04 -0400464 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-Hartmane639dd32005-06-20 21:15:16 -0700468
469 if (!usbfs_snoop)
470 return;
471
Alan Stern4c6e8972009-06-29 11:02:04 -0400472 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 Samavedam2f964782017-05-16 14:38:08 +0200478 dev_info(&udev->dev, "userurb %pK, ep%d %s-%s, "
Alan Stern4c6e8972009-06-29 11:02:04 -0400479 "length %u\n",
480 userurb, ep, t, d, length);
481 else
Vamsi Krishna Samavedam2f964782017-05-16 14:38:08 +0200482 dev_info(&udev->dev, "userurb %pK, ep%d %s-%s, "
Alan Stern4c6e8972009-06-29 11:02:04 -0400483 "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 Frey0880aef2010-01-26 17:07:29 -0500496
Alan Stern0290cc92015-11-20 13:53:22 -0500497 data_len = min(data_len, usbfs_snoop_max);
Chris Frey0880aef2010-01-26 17:07:29 -0500498 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-Hartmane639dd32005-06-20 21:15:16 -0700502}
503
Hans de Goede3d97ff62012-07-04 09:18:03 +0200504static void snoop_urb_data(struct urb *urb, unsigned len)
505{
506 int i, size;
507
Alan Stern0290cc92015-11-20 13:53:22 -0500508 len = min(len, usbfs_snoop_max);
509 if (!usbfs_snoop || len == 0)
Hans de Goede3d97ff62012-07-04 09:18:03 +0200510 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
526static 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 Stern01c64602009-09-01 11:09:56 -0400552#define AS_CONTINUATION 1
553#define AS_UNLINK 2
554
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200555static void cancel_bulk_urbs(struct usb_dev_state *ps, unsigned bulk_addr)
Alan Stern01c64602009-09-01 11:09:56 -0400556__releases(ps->lock)
557__acquires(ps->lock)
558{
Huajun Li4e09dcf2012-05-18 20:12:51 +0800559 struct urb *urb;
Alan Stern01c64602009-09-01 11:09:56 -0400560 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 Li4e09dcf2012-05-18 20:12:51 +0800582 urb = as->urb;
583 usb_get_urb(urb);
Alan Stern01c64602009-09-01 11:09:56 -0400584 spin_unlock(&ps->lock); /* Allow completions */
Huajun Li4e09dcf2012-05-18 20:12:51 +0800585 usb_unlink_urb(urb);
586 usb_put_urb(urb);
Alan Stern01c64602009-09-01 11:09:56 -0400587 spin_lock(&ps->lock);
588 goto rescan;
589 }
590 }
591}
592
David Howells7d12e782006-10-05 14:55:46 +0100593static void async_completed(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800595 struct async *as = urb->context;
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200596 struct usb_dev_state *ps = as->ps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 struct siginfo sinfo;
Oliver Neukum516a1a02009-07-08 19:09:23 +0200598 struct pid *pid = NULL;
Oliver Neukum516a1a02009-07-08 19:09:23 +0200599 u32 secid = 0;
Serge Hallynd178bc32011-09-26 10:45:18 -0500600 const struct cred *cred = NULL;
Oliver Neukum516a1a02009-07-08 19:09:23 +0200601 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800603 spin_lock(&ps->lock);
604 list_move_tail(&as->asynclist, &ps->async_completed);
Alan Sterne0152682007-08-24 15:42:52 -0400605 as->status = urb->status;
Oliver Neukum516a1a02009-07-08 19:09:23 +0200606 signr = as->signr;
607 if (signr) {
Alan Sternf0c2b682015-02-13 10:54:53 -0500608 memset(&sinfo, 0, sizeof(sinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 sinfo.si_signo = as->signr;
Alan Sterne0152682007-08-24 15:42:52 -0400610 sinfo.si_errno = as->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 sinfo.si_code = SI_ASYNCIO;
612 sinfo.si_addr = as->userurb;
Serge Hallynaec01c52011-09-26 10:18:29 -0500613 pid = get_pid(as->pid);
Serge Hallynd178bc32011-09-26 10:45:18 -0500614 cred = get_cred(as->cred);
Oliver Neukum516a1a02009-07-08 19:09:23 +0200615 secid = as->secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700617 snoop(&urb->dev->dev, "urb complete\n");
Alan Stern4c6e8972009-06-29 11:02:04 -0400618 snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length,
Hans de Goede3d97ff62012-07-04 09:18:03 +0200619 as->status, COMPLETE, NULL, 0);
Dan Carpenter83ed07c2015-05-18 15:29:51 +0300620 if ((urb->transfer_flags & URB_DIR_MASK) == URB_DIR_IN)
Hans de Goede3d97ff62012-07-04 09:18:03 +0200621 snoop_urb_data(urb, urb->actual_length);
622
Alan Stern01c64602009-09-01 11:09:56 -0400623 if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET &&
624 as->status != -ENOENT)
625 cancel_bulk_urbs(ps, as->bulk_addr);
Douglas Andersoned62ca22017-08-10 15:42:22 -0700626
627 wake_up(&ps->wait);
Oliver Neukum516a1a02009-07-08 19:09:23 +0200628 spin_unlock(&ps->lock);
629
Serge Hallynaec01c52011-09-26 10:18:29 -0500630 if (signr) {
Serge Hallynd178bc32011-09-26 10:45:18 -0500631 kill_pid_info_as_cred(sinfo.si_signo, &sinfo, pid, cred, secid);
Serge Hallynaec01c52011-09-26 10:18:29 -0500632 put_pid(pid);
Serge Hallynd178bc32011-09-26 10:45:18 -0500633 put_cred(cred);
Serge Hallynaec01c52011-09-26 10:18:29 -0500634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200637static void destroy_async(struct usb_dev_state *ps, struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Huajun Li4e09dcf2012-05-18 20:12:51 +0800639 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 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 Li4e09dcf2012-05-18 20:12:51 +0800647 urb = as->urb;
648 usb_get_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /* drop the spinlock so the completion handler can run */
651 spin_unlock_irqrestore(&ps->lock, flags);
Huajun Li4e09dcf2012-05-18 20:12:51 +0800652 usb_kill_urb(urb);
653 usb_put_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 spin_lock_irqsave(&ps->lock, flags);
655 }
656 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200659static void destroy_async_on_interface(struct usb_dev_state *ps,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800660 unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
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 Manea9b6f0c42014-03-10 10:36:40 +0200674static void destroy_all_async(struct usb_dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800676 destroy_async(ps, &ps->async_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
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-Hartman04e482ff2008-01-30 15:21:33 -0800685static int driver_probe(struct usb_interface *intf,
686 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 return -ENODEV;
689}
690
691static void driver_disconnect(struct usb_interface *intf)
692{
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200693 struct usb_dev_state *ps = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 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-Hartman3b6004f2008-08-14 09:37:34 -0700706 dev_warn(&intf->dev, "interface number %u out of range\n",
707 ifnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800709 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 /* force async requests to complete */
712 destroy_async_on_interface(ps, ifnum);
713}
714
Alan Stern2e2eb832007-12-04 14:35:15 -0500715/* The following routines are merely placeholders. There is no way
716 * to inform a user task about suspend or resumes.
717 */
718static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
719{
720 return 0;
721}
722
723static int driver_resume(struct usb_interface *intf)
724{
725 return 0;
726}
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728struct usb_driver usbfs_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 .name = "usbfs",
730 .probe = driver_probe,
731 .disconnect = driver_disconnect,
Alan Stern2e2eb832007-12-04 14:35:15 -0500732 .suspend = driver_suspend,
733 .resume = driver_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734};
735
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200736static int claimintf(struct usb_dev_state *ps, unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
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 Grantd883f522016-02-21 18:38:01 -0300748 if (ps->privileges_dropped &&
749 !test_bit(ifnum, &ps->interface_allowed_mask))
750 return -EACCES;
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 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 Torvalds1da177e2005-04-16 15:20:36 -0700757 if (err == 0)
758 set_bit(ifnum, &ps->ifclaimed);
759 return err;
760}
761
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200762static int releaseintf(struct usb_dev_state *ps, unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
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 Torvalds1da177e2005-04-16 15:20:36 -0700772 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 Torvalds1da177e2005-04-16 15:20:36 -0700779 return err;
780}
781
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200782static int checkintf(struct usb_dev_state *ps, unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
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-Hartman04e482ff2008-01-30 15:21:33 -0800791 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 Torvalds1da177e2005-04-16 15:20:36 -0700794 return claimintf(ps, ifnum);
795}
796
797static int findintfep(struct usb_device *dev, unsigned int ep)
798{
799 unsigned int i, j, e;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800800 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 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-Hartman04e482ff2008-01-30 15:21:33 -0800811 alts = &intf->altsetting[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 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-Hartman04e482ff2008-01-30 15:21:33 -0800819 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200822static int check_ctrlrecip(struct usb_dev_state *ps, unsigned int requesttype,
Matthias Dellweg393cbb52011-09-25 14:26:25 +0200823 unsigned int request, unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 int ret = 0;
Matthias Dellweg393cbb52011-09-25 14:26:25 +0200826 struct usb_host_interface *alt_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
David Vrabel6da9c992009-02-18 14:43:47 +0000828 if (ps->dev->state != USB_STATE_UNAUTHENTICATED
829 && ps->dev->state != USB_STATE_ADDRESS
Horst Schirmeier24f8b112006-03-11 00:16:55 -0800830 && ps->dev->state != USB_STATE_CONFIGURED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return -EHOSTUNREACH;
832 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
833 return 0;
834
Matthias Dellweg393cbb52011-09-25 14:26:25 +0200835 /*
836 * check for the special corner case 'get_device_id' in the printer
Hans de Goede5dc50c32013-07-12 10:04:18 +0200837 * class specification, which we always want to allow as it is used
838 * to query things like ink level, etc.
Matthias Dellweg393cbb52011-09-25 14:26:25 +0200839 */
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 Goede5dc50c32013-07-12 10:04:18 +0200845 return 0;
Matthias Dellweg393cbb52011-09-25 14:26:25 +0200846 }
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 index &= 0xff;
849 switch (requesttype & USB_RECIP_MASK) {
850 case USB_RECIP_ENDPOINT:
Hans de Goede1361bf4b2013-04-16 11:08:33 +0200851 if ((index & ~USB_DIR_IN) == 0)
852 return 0;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800853 ret = findintfep(ps->dev, index);
Kurt Garloff831abf72013-09-24 14:13:48 +0200854 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-Hartman04e482ff2008-01-30 15:21:33 -0800870 if (ret >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 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 Goede2fec32b02013-10-09 17:19:30 +0200881static 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 Torvalds3e75c6d2014-04-01 17:06:09 -0700890static int parse_usbdevfs_streams(struct usb_dev_state *ps,
Hans de Goedebcf7f6e2013-10-09 17:19:31 +0200891 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
958error:
959 kfree(eps);
960 return ret;
961}
962
Alan Stern61ad04a2008-06-24 14:47:12 -0400963static int match_devt(struct device *dev, void *data)
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700964{
David Howellsa80d5ff2008-07-02 12:28:55 +0100965 return dev->devt == (dev_t) (unsigned long) data;
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100966}
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700967
Alan Stern61ad04a2008-06-24 14:47:12 -0400968static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100969{
970 struct device *dev;
971
David Howellsa80d5ff2008-07-02 12:28:55 +0100972 dev = bus_find_device(&usb_bus_type, NULL,
973 (void *) (unsigned long) devt, match_devt);
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100974 if (!dev)
975 return NULL;
Geliang Tang69ab55d2015-12-23 21:26:50 +0800976 return to_usb_device(dev);
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100977}
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979/*
980 * file operations
981 */
982static int usbdev_open(struct inode *inode, struct file *file)
983{
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200984 struct usb_device *dev = NULL;
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200985 struct usb_dev_state *ps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 int ret;
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 ret = -ENOMEM;
Reilly Grantd883f522016-02-21 18:38:01 -0300989 ps = kzalloc(sizeof(struct usb_dev_state), GFP_KERNEL);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800990 if (!ps)
Alan Stern62e299e2010-01-08 12:56:19 -0500991 goto out_free_ps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Alan Stern01105a22009-07-30 15:28:14 -0400993 ret = -ENODEV;
Alan Stern61ad04a2008-06-24 14:47:12 -0400994
Alan Stern62e299e2010-01-08 12:56:19 -0500995 /* Protect against simultaneous removal or release */
996 mutex_lock(&usbfs_mutex);
997
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100998 /* usbdev device-node */
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200999 if (imajor(inode) == USB_DEVICE_MAJOR)
Alan Stern61ad04a2008-06-24 14:47:12 -04001000 dev = usbdev_lookup_by_devt(inode->i_rdev);
Alan Stern62e299e2010-01-08 12:56:19 -05001001
Alan Stern62e299e2010-01-08 12:56:19 -05001002 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 Stern94fcda12006-11-20 11:38:46 -05001011 ret = usb_autoresume_device(dev);
Alan Stern01d883d2006-08-30 15:47:18 -04001012 if (ret)
Alan Stern62e299e2010-01-08 12:56:19 -05001013 goto out_unlock_device;
Alan Stern01d883d2006-08-30 15:47:18 -04001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 ps->dev = dev;
1016 ps->file = file;
Reilly Grantd883f522016-02-21 18:38:01 -03001017 ps->interface_allowed_mask = 0xFFFFFFFF; /* 32 bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 spin_lock_init(&ps->lock);
Dan Carpenter316547f2006-12-13 00:03:38 -08001019 INIT_LIST_HEAD(&ps->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 INIT_LIST_HEAD(&ps->async_pending);
1021 INIT_LIST_HEAD(&ps->async_completed);
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001022 INIT_LIST_HEAD(&ps->memory_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 init_waitqueue_head(&ps->wait);
Eric W. Biederman2425c082006-10-02 02:17:28 -07001024 ps->disc_pid = get_pid(task_pid(current));
Serge Hallynd178bc32011-09-26 10:45:18 -05001025 ps->cred = get_current_cred();
David Quigley7a019552006-06-30 01:55:48 -07001026 security_task_getsecid(current, &ps->secid);
Oliver Neukum527660a2007-04-20 20:50:48 +02001027 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 list_add_tail(&ps->list, &dev->filelist);
1029 file->private_data = ps;
Alan Stern62e299e2010-01-08 12:56:19 -05001030 usb_unlock_device(dev);
Alan Stern2da41d52008-10-06 11:24:26 -04001031 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
1032 current->comm);
Alan Stern62e299e2010-01-08 12:56:19 -05001033 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 Stern4a2a8a22006-07-01 22:05:01 -04001040 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043static int usbdev_release(struct inode *inode, struct file *file)
1044{
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001045 struct usb_dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 struct usb_device *dev = ps->dev;
1047 unsigned int ifnum;
Alan Stern6ff10462009-03-09 13:44:02 -04001048 struct async *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 usb_lock_device(dev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001051 usb_hub_release_all_ports(dev, ps);
Alan Stern4a2a8a22006-07-01 22:05:01 -04001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 list_del_init(&ps->list);
Alan Stern4a2a8a22006-07-01 22:05:01 -04001054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 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 Stern94fcda12006-11-20 11:38:46 -05001061 usb_autosuspend_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 usb_unlock_device(dev);
1063 usb_put_dev(dev);
Eric W. Biederman2425c082006-10-02 02:17:28 -07001064 put_pid(ps->disc_pid);
Serge Hallynd178bc32011-09-26 10:45:18 -05001065 put_cred(ps->cred);
Alan Stern6ff10462009-03-09 13:44:02 -04001066
1067 as = async_getcompleted(ps);
1068 while (as) {
1069 free_async(as);
1070 as = async_getcompleted(ps);
1071 }
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 kfree(ps);
Alan Stern4a2a8a22006-07-01 22:05:01 -04001074 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001077static int proc_control(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
1079 struct usb_device *dev = ps->dev;
1080 struct usbdevfs_ctrltransfer ctrl;
1081 unsigned int tmo;
1082 unsigned char *tbuf;
Andrew Mortonff66e3c2008-03-12 13:32:24 -07001083 unsigned wLength;
Alan Stern4c6e8972009-06-29 11:02:04 -04001084 int i, pipe, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1087 return -EFAULT;
Matthias Dellweg393cbb52011-09-25 14:26:25 +02001088 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.bRequest,
1089 ctrl.wIndex);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001090 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return ret;
Andrew Mortonff66e3c2008-03-12 13:32:24 -07001092 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
1093 if (wLength > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return -EINVAL;
Alan Sternadd1aae2011-11-17 16:41:25 -05001095 ret = usbfs_increase_memory_usage(PAGE_SIZE + sizeof(struct urb) +
1096 sizeof(struct usb_ctrlrequest));
1097 if (ret)
1098 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001099 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
Alan Sternadd1aae2011-11-17 16:41:25 -05001100 if (!tbuf) {
1101 ret = -ENOMEM;
1102 goto done;
1103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 tmo = ctrl.timeout;
Chris Frey0880aef2010-01-26 17:07:29 -05001105 snoop(&dev->dev, "control urb: bRequestType=%02x "
1106 "bRequest=%02x wValue=%04x "
1107 "wIndex=%04x wLength=%04x\n",
Xenia Ragiadakouc8f2efc2013-08-31 18:09:14 +03001108 ctrl.bRequestType, ctrl.bRequest, ctrl.wValue,
1109 ctrl.wIndex, ctrl.wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (ctrl.bRequestType & 0x80) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001111 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
1112 ctrl.wLength)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001113 ret = -EINVAL;
1114 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
Alan Stern4c6e8972009-06-29 11:02:04 -04001116 pipe = usb_rcvctrlpipe(dev, 0);
Chris Frey0880aef2010-01-26 17:07:29 -05001117 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 usb_unlock_device(dev);
Alan Stern4c6e8972009-06-29 11:02:04 -04001120 i = usb_control_msg(dev, pipe, ctrl.bRequest,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001121 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
1122 tbuf, ctrl.wLength, tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 usb_lock_device(dev);
Chris Frey0880aef2010-01-26 17:07:29 -05001124 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE,
Michal Sojka9d02b422011-03-15 16:41:47 +01001125 tbuf, max(i, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if ((i > 0) && ctrl.wLength) {
Alan Sternfe0410c2005-07-29 12:16:58 -07001127 if (copy_to_user(ctrl.data, tbuf, i)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001128 ret = -EFAULT;
1129 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131 }
1132 } else {
1133 if (ctrl.wLength) {
1134 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001135 ret = -EFAULT;
1136 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138 }
Alan Stern4c6e8972009-06-29 11:02:04 -04001139 pipe = usb_sndctrlpipe(dev, 0);
Chris Frey0880aef2010-01-26 17:07:29 -05001140 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT,
1141 tbuf, ctrl.wLength);
Alan Stern4c6e8972009-06-29 11:02:04 -04001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 usb_unlock_device(dev);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001144 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
1145 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
1146 tbuf, ctrl.wLength, tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 usb_lock_device(dev);
Chris Frey0880aef2010-01-26 17:07:29 -05001148 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001150 if (i < 0 && i != -EPIPE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 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 Stern52fb7432011-11-17 16:41:14 -05001156 ret = i;
1157 done:
1158 free_page((unsigned long) tbuf);
Alan Sternadd1aae2011-11-17 16:41:25 -05001159 usbfs_decrease_memory_usage(PAGE_SIZE + sizeof(struct urb) +
1160 sizeof(struct usb_ctrlrequest));
Alan Stern52fb7432011-11-17 16:41:14 -05001161 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001164static int proc_bulk(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
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 Stern4c6e8972009-06-29 11:02:04 -04001171 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 if (copy_from_user(&bulk, arg, sizeof(bulk)))
1174 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001175 ret = findintfep(ps->dev, bulk.ep);
1176 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001178 ret = checkintf(ps, ret);
1179 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 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 Berezecki1129d272016-12-21 09:19:14 -08001188 if (len1 >= (INT_MAX - sizeof(struct urb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return -EINVAL;
Alan Sternadd1aae2011-11-17 16:41:25 -05001190 ret = usbfs_increase_memory_usage(len1 + sizeof(struct urb));
1191 if (ret)
1192 return ret;
Kris Borer135551e2015-08-04 08:39:31 -04001193 tbuf = kmalloc(len1, GFP_KERNEL);
1194 if (!tbuf) {
Alan Sternadd1aae2011-11-17 16:41:25 -05001195 ret = -ENOMEM;
1196 goto done;
1197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 tmo = bulk.timeout;
1199 if (bulk.ep & 0x80) {
1200 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001201 ret = -EINVAL;
1202 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
Chris Frey0880aef2010-01-26 17:07:29 -05001204 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, NULL, 0);
Alan Stern4c6e8972009-06-29 11:02:04 -04001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 usb_unlock_device(dev);
1207 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
1208 usb_lock_device(dev);
Chris Frey0880aef2010-01-26 17:07:29 -05001209 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, tbuf, len2);
Alan Stern4c6e8972009-06-29 11:02:04 -04001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (!i && len2) {
1212 if (copy_to_user(bulk.data, tbuf, len2)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001213 ret = -EFAULT;
1214 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216 }
1217 } else {
1218 if (len1) {
1219 if (copy_from_user(tbuf, bulk.data, len1)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001220 ret = -EFAULT;
1221 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223 }
Chris Frey0880aef2010-01-26 17:07:29 -05001224 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, tbuf, len1);
Alan Stern4c6e8972009-06-29 11:02:04 -04001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 usb_unlock_device(dev);
1227 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
1228 usb_lock_device(dev);
Chris Frey0880aef2010-01-26 17:07:29 -05001229 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
Alan Stern52fb7432011-11-17 16:41:14 -05001231 ret = (i < 0 ? i : len2);
1232 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 kfree(tbuf);
Alan Sternadd1aae2011-11-17 16:41:25 -05001234 usbfs_decrease_memory_usage(len1 + sizeof(struct urb));
Alan Stern52fb7432011-11-17 16:41:14 -05001235 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Alan Sternf080a512014-02-20 10:49:30 -05001238static 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 Manea9b6f0c42014-03-10 10:36:40 +02001252static int proc_resetep(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
1254 unsigned int ep;
1255 int ret;
1256
1257 if (get_user(ep, (unsigned int __user *)arg))
1258 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001259 ret = findintfep(ps->dev, ep);
1260 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001262 ret = checkintf(ps, ret);
1263 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return ret;
Alan Sternf080a512014-02-20 10:49:30 -05001265 check_reset_of_active_ep(ps->dev, ep, "RESETEP");
David Vrabel3444b262009-04-08 17:36:28 +00001266 usb_reset_endpoint(ps->dev, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return 0;
1268}
1269
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001270static int proc_clearhalt(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
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-Hartman04e482ff2008-01-30 15:21:33 -08001278 ret = findintfep(ps->dev, ep);
1279 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001281 ret = checkintf(ps, ret);
1282 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return ret;
Alan Sternf080a512014-02-20 10:49:30 -05001284 check_reset_of_active_ep(ps->dev, ep, "CLEAR_HALT");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (ep & USB_DIR_IN)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001286 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
1287 else
1288 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 return usb_clear_halt(ps->dev, pipe);
1291}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001293static int proc_getdriver(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
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 Torvalds1da177e2005-04-16 15:20:36 -07001301 intf = usb_ifnum_to_if(ps->dev, gd.interface);
1302 if (!intf || !intf->dev.driver)
1303 ret = -ENODATA;
1304 else {
Chen Gangb11b2e12013-02-02 15:57:53 +08001305 strlcpy(gd.driver, intf->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 sizeof(gd.driver));
1307 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
1308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return ret;
1310}
1311
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001312static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Kangjie Lu681fef82016-05-03 16:32:16 -04001314 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 Torvalds1da177e2005-04-16 15:20:36 -07001319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (copy_to_user(arg, &ci, sizeof(ci)))
1321 return -EFAULT;
1322 return 0;
1323}
1324
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001325static int proc_resetdevice(struct usb_dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Reilly Grantd883f522016-02-21 18:38:01 -03001327 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 Lei742120c2008-06-18 22:00:29 +08001349 return usb_reset_device(ps->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001352static int proc_setintf(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 struct usbdevfs_setinterface setintf;
1355 int ret;
1356
1357 if (copy_from_user(&setintf, arg, sizeof(setintf)))
1358 return -EFAULT;
Kris Borer135551e2015-08-04 08:39:31 -04001359 ret = checkintf(ps, setintf.interface);
1360 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return ret;
Hans de Goede5ec9c172013-10-09 17:19:27 +02001362
1363 destroy_async_on_interface(ps, setintf.interface);
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return usb_set_interface(ps->dev, setintf.interface,
1366 setintf.altsetting);
1367}
1368
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001369static int proc_setconfig(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Alan Stern3f141e22007-02-08 16:40:43 -05001371 int u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 int status = 0;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001373 struct usb_host_config *actconfig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Alan Stern3f141e22007-02-08 16:40:43 -05001375 if (get_user(u, (int __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return -EFAULT;
1377
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001378 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 Torvalds1da177e2005-04-16 15:20:36 -07001382 * an interface is claimed by usbfs it could easily deadlock.
1383 */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001384 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 Brownell72ebddb2005-04-11 18:34:17 -07001390 "usbfs: interface %d claimed by %s "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 "while '%s' sets config #%d\n",
1392 actconfig->interface[i]
1393 ->cur_altsetting
1394 ->desc.bInterfaceNumber,
David Brownell72ebddb2005-04-11 18:34:17 -07001395 actconfig->interface[i]
1396 ->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 current->comm, u);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001398 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001401 }
1402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
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. Gundersonf7d34b42016-02-03 22:58:26 +01001417static struct usb_memory *
1418find_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 Manea9b6f0c42014-03-10 10:36:40 +02001442static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001443 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
1444 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 struct usbdevfs_iso_packet_desc *isopkt = NULL;
1447 struct usb_host_endpoint *ep;
Alan Stern52fb7432011-11-17 16:41:14 -05001448 struct async *as = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 struct usb_ctrlrequest *dr = NULL;
1450 unsigned int u, totlen, isofrmlen;
Hans de Goede3d97ff62012-07-04 09:18:03 +02001451 int i, ret, is_in, num_sgs = 0, ifnum = -1;
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001452 int number_of_packets = 0;
Hans de Goede948cd8c2013-10-09 17:19:29 +02001453 unsigned int stream_id = 0;
Hans de Goede3d97ff62012-07-04 09:18:03 +02001454 void *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Alan Stern14722ef2008-04-17 10:18:11 -04001456 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
1457 USBDEVFS_URB_SHORT_NOT_OK |
Alan Stern01c64602009-09-01 11:09:56 -04001458 USBDEVFS_URB_BULK_CONTINUATION |
Alan Stern14722ef2008-04-17 10:18:11 -04001459 USBDEVFS_URB_NO_FSBR |
1460 USBDEVFS_URB_ZERO_PACKET |
1461 USBDEVFS_URB_NO_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return -EINVAL;
Alan Stern91801352009-06-29 11:04:54 -04001463 if (uurb->buffer_length > 0 && !uurb->buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001465 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 Torvalds1da177e2005-04-16 15:20:36 -07001469 return ifnum;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001470 ret = checkintf(ps, ifnum);
1471 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return ret;
1473 }
Hans de Goede2fec32b02013-10-09 17:19:30 +02001474 ep = ep_to_host_endpoint(ps->dev, uurb->endpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 if (!ep)
1476 return -ENOENT;
Hans de Goede2fec32b02013-10-09 17:19:30 +02001477 is_in = (uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0;
Alan Sternadd1aae2011-11-17 16:41:25 -05001478
1479 u = 0;
Kris Borerf355e832015-08-07 07:22:44 -04001480 switch (uurb->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 case USBDEVFS_URB_TYPE_CONTROL:
Alan Stern93cf9b92007-07-30 17:09:28 -04001482 if (!usb_endpoint_xfer_control(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return -EINVAL;
Alan Sternadd1aae2011-11-17 16:41:25 -05001484 /* min 8 byte setup packet */
1485 if (uurb->buffer_length < 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001487 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1488 if (!dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return -ENOMEM;
1490 if (copy_from_user(dr, uurb->buffer, 8)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001491 ret = -EFAULT;
1492 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001495 ret = -EINVAL;
1496 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
Matthias Dellweg393cbb52011-09-25 14:26:25 +02001498 ret = check_ctrlrecip(ps, dr->bRequestType, dr->bRequest,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001499 le16_to_cpup(&dr->wIndex));
Alan Stern52fb7432011-11-17 16:41:14 -05001500 if (ret)
1501 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1503 uurb->buffer += 8;
Alan Stern93cf9b92007-07-30 17:09:28 -04001504 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 Frey0880aef2010-01-26 17:07:29 -05001511 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 Sternadd1aae2011-11-17 16:41:25 -05001518 u = sizeof(struct usb_ctrlrequest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 break;
1520
1521 case USBDEVFS_URB_TYPE_BULK:
Alan Stern93cf9b92007-07-30 17:09:28 -04001522 switch (usb_endpoint_type(&ep->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 case USB_ENDPOINT_XFER_CONTROL:
1524 case USB_ENDPOINT_XFER_ISOC:
1525 return -EINVAL;
Alan Sternf661c6f2009-12-11 16:20:20 -05001526 case USB_ENDPOINT_XFER_INT:
1527 /* allow single-shot interrupt transfers */
1528 uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
1529 goto interrupt_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
Hans de Goede3d97ff62012-07-04 09:18:03 +02001531 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 Goede948cd8c2013-10-09 17:19:29 +02001534 if (ep->streams)
1535 stream_id = uurb->stream_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 break;
1537
Alan Sternf661c6f2009-12-11 16:20:20 -05001538 case USBDEVFS_URB_TYPE_INTERRUPT:
1539 if (!usb_endpoint_xfer_int(&ep->desc))
1540 return -EINVAL;
1541 interrupt_urb:
Alan Sternf661c6f2009-12-11 16:20:20 -05001542 break;
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 case USBDEVFS_URB_TYPE_ISO:
1545 /* arbitrary limit */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001546 if (uurb->number_of_packets < 1 ||
1547 uurb->number_of_packets > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return -EINVAL;
Alan Stern93cf9b92007-07-30 17:09:28 -04001549 if (!usb_endpoint_xfer_isoc(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return -EINVAL;
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001551 number_of_packets = uurb->number_of_packets;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001552 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001553 number_of_packets;
Rahul Pathak73a02d32015-12-11 05:40:51 +00001554 isopkt = memdup_user(iso_frame_desc, isofrmlen);
1555 if (IS_ERR(isopkt)) {
1556 ret = PTR_ERR(isopkt);
1557 isopkt = NULL;
Alan Stern52fb7432011-11-17 16:41:14 -05001558 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001560 for (totlen = u = 0; u < number_of_packets; u++) {
Federico Manzane2e2f0e2013-05-24 18:18:48 +02001561 /*
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 Stern52fb7432011-11-17 16:41:14 -05001568 ret = -EINVAL;
1569 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571 totlen += isopkt[u].length;
1572 }
Alan Sternadd1aae2011-11-17 16:41:25 -05001573 u *= sizeof(struct usb_iso_packet_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 uurb->buffer_length = totlen;
1575 break;
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 default:
1578 return -EINVAL;
1579 }
Alan Sternadd1aae2011-11-17 16:41:25 -05001580
Alan Stern91801352009-06-29 11:04:54 -04001581 if (uurb->buffer_length > 0 &&
1582 !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1583 uurb->buffer, uurb->buffer_length)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001584 ret = -EFAULT;
1585 goto error;
Alan Stern91801352009-06-29 11:04:54 -04001586 }
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001587 as = alloc_async(number_of_packets);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001588 if (!as) {
Alan Stern52fb7432011-11-17 16:41:14 -05001589 ret = -ENOMEM;
1590 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
Hans de Goede3d97ff62012-07-04 09:18:03 +02001592
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001593 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 Goede3d97ff62012-07-04 09:18:03 +02001606 u += sizeof(struct async) + sizeof(struct urb) + uurb->buffer_length +
1607 num_sgs * sizeof(struct scatterlist);
Alan Sternadd1aae2011-11-17 16:41:25 -05001608 ret = usbfs_increase_memory_usage(u);
1609 if (ret)
1610 goto error;
1611 as->mem_usage = u;
1612
Hans de Goede3d97ff62012-07-04 09:18:03 +02001613 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 Rydberg01463902012-10-13 12:20:36 +02001638 uurb->buffer += u;
Hans de Goede3d97ff62012-07-04 09:18:03 +02001639 }
1640 totlen -= u;
1641 }
1642 } else if (uurb->buffer_length > 0) {
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001643 if (as->usbm) {
1644 unsigned long uurb_start = (unsigned long)uurb->buffer;
Hans de Goede3d97ff62012-07-04 09:18:03 +02001645
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001646 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 Goede3d97ff62012-07-04 09:18:03 +02001653 goto error;
1654 }
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001655 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 Goede3d97ff62012-07-04 09:18:03 +02001672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001674 as->urb->dev = ps->dev;
1675 as->urb->pipe = (uurb->type << 30) |
Alan Stern93cf9b92007-07-30 17:09:28 -04001676 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1677 (uurb->endpoint & USB_DIR_IN);
Alan Stern14722ef2008-04-17 10:18:11 -04001678
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 Neukumd310d052014-08-01 09:55:20 +02001686 if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK && is_in)
Alan Stern14722ef2008-04-17 10:18:11 -04001687 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 Torvalds1da177e2005-04-16 15:20:36 -07001696 as->urb->transfer_buffer_length = uurb->buffer_length;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001697 as->urb->setup_packet = (unsigned char *)dr;
Alan Stern52fb7432011-11-17 16:41:14 -05001698 dr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 as->urb->start_frame = uurb->start_frame;
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001700 as->urb->number_of_packets = number_of_packets;
Hans de Goede948cd8c2013-10-09 17:19:29 +02001701 as->urb->stream_id = stream_id;
Alan Stern53e5f362016-08-23 15:32:51 -04001702
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-Hartman04e482ff2008-01-30 15:21:33 -08001713 as->urb->context = as;
1714 as->urb->complete = async_completed;
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001715 for (totlen = u = 0; u < number_of_packets; u++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 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 Juhl6fd19f42005-04-18 17:39:33 -07001720 kfree(isopkt);
Alan Stern52fb7432011-11-17 16:41:14 -05001721 isopkt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 as->ps = ps;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001723 as->userurb = arg;
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001724 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 Torvalds1da177e2005-04-16 15:20:36 -07001731 as->userbuffer = uurb->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 as->signr = uurb->signr;
1733 as->ifnum = ifnum;
Eric W. Biederman2425c082006-10-02 02:17:28 -07001734 as->pid = get_pid(task_pid(current));
Serge Hallynd178bc32011-09-26 10:45:18 -05001735 as->cred = get_current_cred();
David Quigley7a019552006-06-30 01:55:48 -07001736 security_task_getsecid(current, &as->secid);
Alan Stern4c6e8972009-06-29 11:02:04 -04001737 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
Chris Frey0880aef2010-01-26 17:07:29 -05001738 as->urb->transfer_buffer_length, 0, SUBMIT,
Hans de Goede3d97ff62012-07-04 09:18:03 +02001739 NULL, 0);
1740 if (!is_in)
1741 snoop_urb_data(as->urb, as->urb->transfer_buffer_length);
1742
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001743 async_newpending(as);
Alan Stern01c64602009-09-01 11:09:56 -04001744
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-Hartman04e482ff2008-01-30 15:21:33 -08001777 dev_printk(KERN_DEBUG, &ps->dev->dev,
1778 "usbfs: usb_submit_urb returned %d\n", ret);
Alan Stern4c6e8972009-06-29 11:02:04 -04001779 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
Chris Frey0880aef2010-01-26 17:07:29 -05001780 0, ret, COMPLETE, NULL, 0);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001781 async_removepending(as);
Alan Stern52fb7432011-11-17 16:41:14 -05001782 goto error;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001783 }
1784 return 0;
Alan Stern52fb7432011-11-17 16:41:14 -05001785
1786 error:
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001787 if (as && as->usbm)
1788 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count);
Alan Stern52fb7432011-11-17 16:41:14 -05001789 kfree(isopkt);
1790 kfree(dr);
1791 if (as)
1792 free_async(as);
1793 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
1795
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001796static int proc_submiturb(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
1798 struct usbdevfs_urb uurb;
1799
1800 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1801 return -EFAULT;
1802
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001803 return proc_do_submiturb(ps, &uurb,
1804 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1805 arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001808static int proc_unlinkurb(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Huajun Li4e09dcf2012-05-18 20:12:51 +08001810 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 struct async *as;
Huajun Li4e09dcf2012-05-18 20:12:51 +08001812 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Huajun Li4e09dcf2012-05-18 20:12:51 +08001814 spin_lock_irqsave(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 as = async_getpending(ps, arg);
Huajun Li4e09dcf2012-05-18 20:12:51 +08001816 if (!as) {
1817 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return -EINVAL;
Huajun Li4e09dcf2012-05-18 20:12:51 +08001819 }
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 Torvalds1da177e2005-04-16 15:20:36 -07001828 return 0;
1829}
1830
1831static 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 Stern7152b592010-03-06 15:04:03 -05001838 if (as->userbuffer && urb->actual_length) {
Hans de Goede3d97ff62012-07-04 09:18:03 +02001839 if (copy_urb_data_to_user(as->userbuffer, urb))
Oliver Neukumd794a022009-06-28 23:34:14 +02001840 goto err_out;
Alan Stern7152b592010-03-06 15:04:03 -05001841 }
Alan Sterne0152682007-08-24 15:42:52 -04001842 if (put_user(as->status, &userurb->status))
Oliver Neukumd794a022009-06-28 23:34:14 +02001843 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (put_user(urb->actual_length, &userurb->actual_length))
Oliver Neukumd794a022009-06-28 23:34:14 +02001845 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (put_user(urb->error_count, &userurb->error_count))
Oliver Neukumd794a022009-06-28 23:34:14 +02001847 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Alan Stern93cf9b92007-07-30 17:09:28 -04001849 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
Christopher Li668a9542005-04-18 17:39:26 -07001850 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 Neukumd794a022009-06-28 23:34:14 +02001853 goto err_out;
Christopher Li668a9542005-04-18 17:39:26 -07001854 if (put_user(urb->iso_frame_desc[i].status,
1855 &userurb->iso_frame_desc[i].status))
Oliver Neukumd794a022009-06-28 23:34:14 +02001856 goto err_out;
Christopher Li668a9542005-04-18 17:39:26 -07001857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (put_user(addr, (void __user * __user *)arg))
1861 return -EFAULT;
1862 return 0;
Oliver Neukumd794a022009-06-28 23:34:14 +02001863
1864err_out:
Oliver Neukumd794a022009-06-28 23:34:14 +02001865 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001868static struct async *reap_as(struct usb_dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001870 DECLARE_WAITQUEUE(wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 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-Hartman04e482ff2008-01-30 15:21:33 -08001877 as = async_getcompleted(ps);
Alan Stern3f2cee72015-01-29 11:29:13 -05001878 if (as || !connected(ps))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 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 Manea9b6f0c42014-03-10 10:36:40 +02001891static int proc_reapurb(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 struct async *as = reap_as(ps);
Alan Sterna016a812015-11-20 13:53:35 -05001894
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001895 if (as) {
Alan Sterna016a812015-11-20 13:53:35 -05001896 int retval;
1897
Vamsi Krishna Samavedam2f964782017-05-16 14:38:08 +02001898 snoop(&ps->dev->dev, "reap %pK\n", as->userurb);
Alan Sterna016a812015-11-20 13:53:35 -05001899 retval = processcompl(as, (void __user * __user *)arg);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001900 free_async(as);
1901 return retval;
1902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (signal_pending(current))
1904 return -EINTR;
Alan Stern3f2cee72015-01-29 11:29:13 -05001905 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001908static int proc_reapurbnonblock(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001910 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 struct async *as;
1912
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001913 as = async_getcompleted(ps);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001914 if (as) {
Vamsi Krishna Samavedam2f964782017-05-16 14:38:08 +02001915 snoop(&ps->dev->dev, "reap %pK\n", as->userurb);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001916 retval = processcompl(as, (void __user * __user *)arg);
1917 free_async(as);
Alan Stern3f2cee72015-01-29 11:29:13 -05001918 } else {
1919 retval = (connected(ps) ? -EAGAIN : -ENODEV);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001920 }
1921 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
1923
1924#ifdef CONFIG_COMPAT
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001925static int proc_control_compat(struct usb_dev_state *ps,
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001926 struct usbdevfs_ctrltransfer32 __user *p32)
1927{
Matthias Beyer5b32c382013-10-14 21:46:36 +02001928 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 Bergmann637e8a62009-11-14 02:28:05 +01001933 put_user(compat_ptr(udata), &p->data))
1934 return -EFAULT;
Matthias Beyer5b32c382013-10-14 21:46:36 +02001935 return proc_control(ps, p);
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001936}
1937
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001938static int proc_bulk_compat(struct usb_dev_state *ps,
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001939 struct usbdevfs_bulktransfer32 __user *p32)
1940{
Matthias Beyer06793f22013-10-14 21:46:37 +02001941 struct usbdevfs_bulktransfer __user *p;
1942 compat_uint_t n;
1943 compat_caddr_t addr;
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001944
Matthias Beyer06793f22013-10-14 21:46:37 +02001945 p = compat_alloc_user_space(sizeof(*p));
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001946
Matthias Beyer06793f22013-10-14 21:46:37 +02001947 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 Bergmann637e8a62009-11-14 02:28:05 +01001952
Matthias Beyer06793f22013-10-14 21:46:37 +02001953 return proc_bulk(ps, p);
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001954}
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001955static int proc_disconnectsignal_compat(struct usb_dev_state *ps, void __user *arg)
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001956{
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 Torvalds1da177e2005-04-16 15:20:36 -07001965
1966static int get_urb32(struct usbdevfs_urb *kurb,
1967 struct usbdevfs_urb32 __user *uurb)
1968{
Al Virocc1a7c42017-06-27 17:46:06 -04001969 struct usbdevfs_urb32 urb32;
1970 if (copy_from_user(&urb32, uurb, sizeof(*uurb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return -EFAULT;
Al Virocc1a7c42017-06-27 17:46:06 -04001972 kurb->type = urb32.type;
1973 kurb->endpoint = urb32.endpoint;
1974 kurb->status = urb32.status;
1975 kurb->flags = urb32.flags;
1976 kurb->buffer = compat_ptr(urb32.buffer);
1977 kurb->buffer_length = urb32.buffer_length;
1978 kurb->actual_length = urb32.actual_length;
1979 kurb->start_frame = urb32.start_frame;
1980 kurb->number_of_packets = urb32.number_of_packets;
1981 kurb->error_count = urb32.error_count;
1982 kurb->signr = urb32.signr;
1983 kurb->usercontext = compat_ptr(urb32.usercontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 return 0;
1985}
1986
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001987static int proc_submiturb_compat(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 struct usbdevfs_urb uurb;
1990
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001991 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return -EFAULT;
1993
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001994 return proc_do_submiturb(ps, &uurb,
1995 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
1996 arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
1999static int processcompl_compat(struct async *as, void __user * __user *arg)
2000{
2001 struct urb *urb = as->urb;
2002 struct usbdevfs_urb32 __user *userurb = as->userurb;
2003 void __user *addr = as->userurb;
2004 unsigned int i;
2005
Hans de Goede2102e062012-07-04 09:18:01 +02002006 if (as->userbuffer && urb->actual_length) {
Hans de Goede3d97ff62012-07-04 09:18:03 +02002007 if (copy_urb_data_to_user(as->userbuffer, urb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return -EFAULT;
Hans de Goede2102e062012-07-04 09:18:01 +02002009 }
Alan Sterne0152682007-08-24 15:42:52 -04002010 if (put_user(as->status, &userurb->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 return -EFAULT;
2012 if (put_user(urb->actual_length, &userurb->actual_length))
2013 return -EFAULT;
2014 if (put_user(urb->error_count, &userurb->error_count))
2015 return -EFAULT;
2016
Alan Stern93cf9b92007-07-30 17:09:28 -04002017 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
Christopher Li668a9542005-04-18 17:39:26 -07002018 for (i = 0; i < urb->number_of_packets; i++) {
2019 if (put_user(urb->iso_frame_desc[i].actual_length,
2020 &userurb->iso_frame_desc[i].actual_length))
2021 return -EFAULT;
2022 if (put_user(urb->iso_frame_desc[i].status,
2023 &userurb->iso_frame_desc[i].status))
2024 return -EFAULT;
2025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027
Al Viroc714de52006-10-10 22:45:37 +01002028 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 return -EFAULT;
2030 return 0;
2031}
2032
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002033static int proc_reapurb_compat(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
2035 struct async *as = reap_as(ps);
Alan Sterna016a812015-11-20 13:53:35 -05002036
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08002037 if (as) {
Alan Sterna016a812015-11-20 13:53:35 -05002038 int retval;
2039
Vamsi Krishna Samavedam2f964782017-05-16 14:38:08 +02002040 snoop(&ps->dev->dev, "reap %pK\n", as->userurb);
Alan Sterna016a812015-11-20 13:53:35 -05002041 retval = processcompl_compat(as, (void __user * __user *)arg);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08002042 free_async(as);
2043 return retval;
2044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 if (signal_pending(current))
2046 return -EINTR;
Alan Stern3f2cee72015-01-29 11:29:13 -05002047 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002050static int proc_reapurbnonblock_compat(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08002052 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 struct async *as;
2054
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08002055 as = async_getcompleted(ps);
2056 if (as) {
Vamsi Krishna Samavedam2f964782017-05-16 14:38:08 +02002057 snoop(&ps->dev->dev, "reap %pK\n", as->userurb);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08002058 retval = processcompl_compat(as, (void __user * __user *)arg);
2059 free_async(as);
Alan Stern3f2cee72015-01-29 11:29:13 -05002060 } else {
2061 retval = (connected(ps) ? -EAGAIN : -ENODEV);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08002062 }
2063 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067#endif
2068
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002069static int proc_disconnectsignal(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
2071 struct usbdevfs_disconnectsignal ds;
2072
2073 if (copy_from_user(&ds, arg, sizeof(ds)))
2074 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 ps->discsignr = ds.signr;
2076 ps->disccontext = ds.context;
2077 return 0;
2078}
2079
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002080static int proc_claiminterface(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 unsigned int ifnum;
2083
2084 if (get_user(ifnum, (unsigned int __user *)arg))
2085 return -EFAULT;
2086 return claimintf(ps, ifnum);
2087}
2088
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002089static int proc_releaseinterface(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
2091 unsigned int ifnum;
2092 int ret;
2093
2094 if (get_user(ifnum, (unsigned int __user *)arg))
2095 return -EFAULT;
Kris Borer135551e2015-08-04 08:39:31 -04002096 ret = releaseintf(ps, ifnum);
2097 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 return ret;
Chase Metzger64f10ed2015-12-22 20:19:45 -08002099 destroy_async_on_interface(ps, ifnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 return 0;
2101}
2102
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002103static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 int size;
2106 void *buf = NULL;
2107 int retval = 0;
2108 struct usb_interface *intf = NULL;
2109 struct usb_driver *driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Reilly Grantd883f522016-02-21 18:38:01 -03002111 if (ps->privileges_dropped)
2112 return -EACCES;
2113
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002114 /* alloc buffer */
Kris Borer135551e2015-08-04 08:39:31 -04002115 size = _IOC_SIZE(ctl->ioctl_code);
2116 if (size > 0) {
Tülin İzer4baf0df2013-05-17 10:13:58 +03002117 buf = kmalloc(size, GFP_KERNEL);
2118 if (buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 return -ENOMEM;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002120 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002121 if (copy_from_user(buf, ctl->data, size)) {
Jesper Juhl6fd19f42005-04-18 17:39:33 -07002122 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return -EFAULT;
2124 }
2125 } else {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002126 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
2128 }
2129
Alan Stern349710c2006-07-01 22:05:56 -04002130 if (!connected(ps)) {
Jesper Juhl6fd19f42005-04-18 17:39:33 -07002131 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return -ENODEV;
2133 }
2134
2135 if (ps->dev->state != USB_STATE_CONFIGURED)
2136 retval = -EHOSTUNREACH;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002137 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
2138 retval = -EINVAL;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002139 else switch (ctl->ioctl_code) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 /* disconnect kernel driver from interface */
2142 case USBDEVFS_DISCONNECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (intf->dev.driver) {
2144 driver = to_usb_driver(intf->dev.driver);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002145 dev_dbg(&intf->dev, "disconnect by usbfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 usb_driver_release_interface(driver, intf);
2147 } else
2148 retval = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 break;
2150
2151 /* let kernel drivers try to (re)bind to the interface */
2152 case USBDEVFS_CONNECT:
Alan Stern885e9742007-12-03 15:42:10 -05002153 if (!intf->dev.driver)
2154 retval = device_attach(&intf->dev);
2155 else
2156 retval = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 break;
2158
2159 /* talk directly to the interface's driver */
2160 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 if (intf->dev.driver)
2162 driver = to_usb_driver(intf->dev.driver);
Andi Kleenc532b292010-06-01 23:04:41 +02002163 if (driver == NULL || driver->unlocked_ioctl == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 retval = -ENOTTY;
2165 } else {
Andi Kleenc532b292010-06-01 23:04:41 +02002166 retval = driver->unlocked_ioctl(intf, ctl->ioctl_code, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (retval == -ENOIOCTLCMD)
2168 retval = -ENOTTY;
2169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 }
2171
2172 /* cleanup and return */
2173 if (retval >= 0
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002174 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 && size > 0
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002176 && copy_to_user(ctl->data, buf, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 retval = -EFAULT;
Jesper Juhl6fd19f42005-04-18 17:39:33 -07002178
2179 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return retval;
2181}
2182
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002183static int proc_ioctl_default(struct usb_dev_state *ps, void __user *arg)
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002184{
2185 struct usbdevfs_ioctl ctrl;
2186
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002187 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002188 return -EFAULT;
2189 return proc_ioctl(ps, &ctrl);
2190}
2191
2192#ifdef CONFIG_COMPAT
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002193static int proc_ioctl_compat(struct usb_dev_state *ps, compat_uptr_t arg)
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002194{
Al Virocc1a7c42017-06-27 17:46:06 -04002195 struct usbdevfs_ioctl32 ioc32;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002196 struct usbdevfs_ioctl ctrl;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002197
Al Virocc1a7c42017-06-27 17:46:06 -04002198 if (copy_from_user(&ioc32, compat_ptr(arg), sizeof(ioc32)))
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002199 return -EFAULT;
Al Virocc1a7c42017-06-27 17:46:06 -04002200 ctrl.ifno = ioc32.ifno;
2201 ctrl.ioctl_code = ioc32.ioctl_code;
2202 ctrl.data = compat_ptr(ioc32.data);
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002203 return proc_ioctl(ps, &ctrl);
2204}
2205#endif
2206
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002207static int proc_claim_port(struct usb_dev_state *ps, void __user *arg)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04002208{
2209 unsigned portnum;
2210 int rc;
2211
2212 if (get_user(portnum, (unsigned __user *) arg))
2213 return -EFAULT;
2214 rc = usb_hub_claim_port(ps->dev, portnum, ps);
2215 if (rc == 0)
2216 snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n",
2217 portnum, task_pid_nr(current), current->comm);
2218 return rc;
2219}
2220
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002221static int proc_release_port(struct usb_dev_state *ps, void __user *arg)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04002222{
2223 unsigned portnum;
2224
2225 if (get_user(portnum, (unsigned __user *) arg))
2226 return -EFAULT;
2227 return usb_hub_release_port(ps->dev, portnum, ps);
2228}
2229
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002230static int proc_get_capabilities(struct usb_dev_state *ps, void __user *arg)
Hans de Goede19181bc2012-07-04 09:18:02 +02002231{
2232 __u32 caps;
2233
Alan Stern3f2cee72015-01-29 11:29:13 -05002234 caps = USBDEVFS_CAP_ZERO_PACKET | USBDEVFS_CAP_NO_PACKET_SIZE_LIM |
Reilly Grantd883f522016-02-21 18:38:01 -03002235 USBDEVFS_CAP_REAP_AFTER_DISCONNECT | USBDEVFS_CAP_MMAP |
2236 USBDEVFS_CAP_DROP_PRIVILEGES;
Hans de Goede19181bc2012-07-04 09:18:02 +02002237 if (!ps->dev->bus->no_stop_on_short)
2238 caps |= USBDEVFS_CAP_BULK_CONTINUATION;
Hans de Goede3d97ff62012-07-04 09:18:03 +02002239 if (ps->dev->bus->sg_tablesize)
2240 caps |= USBDEVFS_CAP_BULK_SCATTER_GATHER;
Hans de Goede19181bc2012-07-04 09:18:02 +02002241
2242 if (put_user(caps, (__u32 __user *)arg))
2243 return -EFAULT;
2244
2245 return 0;
2246}
2247
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002248static int proc_disconnect_claim(struct usb_dev_state *ps, void __user *arg)
Hans de Goede0837e7e2012-09-08 20:02:05 +02002249{
2250 struct usbdevfs_disconnect_claim dc;
2251 struct usb_interface *intf;
2252
2253 if (copy_from_user(&dc, arg, sizeof(dc)))
2254 return -EFAULT;
2255
2256 intf = usb_ifnum_to_if(ps->dev, dc.interface);
2257 if (!intf)
2258 return -EINVAL;
2259
2260 if (intf->dev.driver) {
2261 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
2262
Reilly Grantd883f522016-02-21 18:38:01 -03002263 if (ps->privileges_dropped)
2264 return -EACCES;
2265
Hans de Goede0837e7e2012-09-08 20:02:05 +02002266 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_IF_DRIVER) &&
2267 strncmp(dc.driver, intf->dev.driver->name,
2268 sizeof(dc.driver)) != 0)
2269 return -EBUSY;
2270
2271 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_EXCEPT_DRIVER) &&
2272 strncmp(dc.driver, intf->dev.driver->name,
2273 sizeof(dc.driver)) == 0)
2274 return -EBUSY;
2275
2276 dev_dbg(&intf->dev, "disconnect by usbfs\n");
2277 usb_driver_release_interface(driver, intf);
2278 }
2279
2280 return claimintf(ps, dc.interface);
2281}
2282
Linus Torvalds3e75c6d2014-04-01 17:06:09 -07002283static int proc_alloc_streams(struct usb_dev_state *ps, void __user *arg)
Hans de Goedebcf7f6e2013-10-09 17:19:31 +02002284{
2285 unsigned num_streams, num_eps;
2286 struct usb_host_endpoint **eps;
2287 struct usb_interface *intf;
2288 int r;
2289
2290 r = parse_usbdevfs_streams(ps, arg, &num_streams, &num_eps,
2291 &eps, &intf);
2292 if (r)
2293 return r;
2294
2295 destroy_async_on_interface(ps,
2296 intf->altsetting[0].desc.bInterfaceNumber);
2297
2298 r = usb_alloc_streams(intf, eps, num_eps, num_streams, GFP_KERNEL);
2299 kfree(eps);
2300 return r;
2301}
2302
Linus Torvalds3e75c6d2014-04-01 17:06:09 -07002303static int proc_free_streams(struct usb_dev_state *ps, void __user *arg)
Hans de Goedebcf7f6e2013-10-09 17:19:31 +02002304{
2305 unsigned num_eps;
2306 struct usb_host_endpoint **eps;
2307 struct usb_interface *intf;
2308 int r;
2309
2310 r = parse_usbdevfs_streams(ps, arg, NULL, &num_eps, &eps, &intf);
2311 if (r)
2312 return r;
2313
2314 destroy_async_on_interface(ps,
2315 intf->altsetting[0].desc.bInterfaceNumber);
2316
2317 r = usb_free_streams(intf, eps, num_eps, GFP_KERNEL);
2318 kfree(eps);
2319 return r;
2320}
2321
Reilly Grantd883f522016-02-21 18:38:01 -03002322static int proc_drop_privileges(struct usb_dev_state *ps, void __user *arg)
2323{
2324 u32 data;
2325
2326 if (copy_from_user(&data, arg, sizeof(data)))
2327 return -EFAULT;
2328
Masahiro Yamada0f5e1552017-02-27 14:28:52 -08002329 /* This is a one way operation. Once privileges are
Reilly Grantd883f522016-02-21 18:38:01 -03002330 * dropped, you cannot regain them. You may however reissue
2331 * this ioctl to shrink the allowed interfaces mask.
2332 */
2333 ps->interface_allowed_mask &= data;
2334 ps->privileges_dropped = true;
2335
2336 return 0;
2337}
2338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339/*
2340 * NOTE: All requests here that have interface numbers as parameters
2341 * are assuming that somehow the configuration has been prevented from
2342 * changing. But there's no mechanism to ensure that...
2343 */
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002344static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
2345 void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002347 struct usb_dev_state *ps = file->private_data;
Al Viro496ad9a2013-01-23 17:07:38 -05002348 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 struct usb_device *dev = ps->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 int ret = -ENOTTY;
2351
2352 if (!(file->f_mode & FMODE_WRITE))
2353 return -EPERM;
Oliver Neukum01412a22010-01-13 15:33:43 +01002354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 usb_lock_device(dev);
Alan Stern3f2cee72015-01-29 11:29:13 -05002356
2357 /* Reap operations are allowed even after disconnection */
2358 switch (cmd) {
2359 case USBDEVFS_REAPURB:
2360 snoop(&dev->dev, "%s: REAPURB\n", __func__);
2361 ret = proc_reapurb(ps, p);
2362 goto done;
2363
2364 case USBDEVFS_REAPURBNDELAY:
2365 snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
2366 ret = proc_reapurbnonblock(ps, p);
2367 goto done;
2368
2369#ifdef CONFIG_COMPAT
2370 case USBDEVFS_REAPURB32:
2371 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
2372 ret = proc_reapurb_compat(ps, p);
2373 goto done;
2374
2375 case USBDEVFS_REAPURBNDELAY32:
2376 snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
2377 ret = proc_reapurbnonblock_compat(ps, p);
2378 goto done;
2379#endif
2380 }
2381
Alan Stern349710c2006-07-01 22:05:56 -04002382 if (!connected(ps)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 usb_unlock_device(dev);
2384 return -ENODEV;
2385 }
2386
2387 switch (cmd) {
2388 case USBDEVFS_CONTROL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002389 snoop(&dev->dev, "%s: CONTROL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 ret = proc_control(ps, p);
2391 if (ret >= 0)
Deepa Dinamani078cd822016-09-14 07:48:04 -07002392 inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 break;
2394
2395 case USBDEVFS_BULK:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002396 snoop(&dev->dev, "%s: BULK\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 ret = proc_bulk(ps, p);
2398 if (ret >= 0)
Deepa Dinamani078cd822016-09-14 07:48:04 -07002399 inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 break;
2401
2402 case USBDEVFS_RESETEP:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002403 snoop(&dev->dev, "%s: RESETEP\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 ret = proc_resetep(ps, p);
2405 if (ret >= 0)
Deepa Dinamani078cd822016-09-14 07:48:04 -07002406 inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 break;
2408
2409 case USBDEVFS_RESET:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002410 snoop(&dev->dev, "%s: RESET\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 ret = proc_resetdevice(ps);
2412 break;
2413
2414 case USBDEVFS_CLEAR_HALT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002415 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 ret = proc_clearhalt(ps, p);
2417 if (ret >= 0)
Deepa Dinamani078cd822016-09-14 07:48:04 -07002418 inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 break;
2420
2421 case USBDEVFS_GETDRIVER:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002422 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 ret = proc_getdriver(ps, p);
2424 break;
2425
2426 case USBDEVFS_CONNECTINFO:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002427 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 ret = proc_connectinfo(ps, p);
2429 break;
2430
2431 case USBDEVFS_SETINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002432 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 ret = proc_setintf(ps, p);
2434 break;
2435
2436 case USBDEVFS_SETCONFIGURATION:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002437 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 ret = proc_setconfig(ps, p);
2439 break;
2440
2441 case USBDEVFS_SUBMITURB:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002442 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 ret = proc_submiturb(ps, p);
2444 if (ret >= 0)
Deepa Dinamani078cd822016-09-14 07:48:04 -07002445 inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 break;
2447
2448#ifdef CONFIG_COMPAT
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002449 case USBDEVFS_CONTROL32:
2450 snoop(&dev->dev, "%s: CONTROL32\n", __func__);
2451 ret = proc_control_compat(ps, p);
2452 if (ret >= 0)
Deepa Dinamani078cd822016-09-14 07:48:04 -07002453 inode->i_mtime = current_time(inode);
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002454 break;
2455
2456 case USBDEVFS_BULK32:
2457 snoop(&dev->dev, "%s: BULK32\n", __func__);
2458 ret = proc_bulk_compat(ps, p);
2459 if (ret >= 0)
Deepa Dinamani078cd822016-09-14 07:48:04 -07002460 inode->i_mtime = current_time(inode);
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002461 break;
2462
2463 case USBDEVFS_DISCSIGNAL32:
2464 snoop(&dev->dev, "%s: DISCSIGNAL32\n", __func__);
2465 ret = proc_disconnectsignal_compat(ps, p);
2466 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 case USBDEVFS_SUBMITURB32:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002469 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 ret = proc_submiturb_compat(ps, p);
2471 if (ret >= 0)
Deepa Dinamani078cd822016-09-14 07:48:04 -07002472 inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 break;
2474
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002475 case USBDEVFS_IOCTL32:
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002476 snoop(&dev->dev, "%s: IOCTL32\n", __func__);
Al Viroc714de52006-10-10 22:45:37 +01002477 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002478 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479#endif
2480
2481 case USBDEVFS_DISCARDURB:
Vamsi Krishna Samavedam2f964782017-05-16 14:38:08 +02002482 snoop(&dev->dev, "%s: DISCARDURB %pK\n", __func__, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 ret = proc_unlinkurb(ps, p);
2484 break;
2485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 case USBDEVFS_DISCSIGNAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002487 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 ret = proc_disconnectsignal(ps, p);
2489 break;
2490
2491 case USBDEVFS_CLAIMINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002492 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 ret = proc_claiminterface(ps, p);
2494 break;
2495
2496 case USBDEVFS_RELEASEINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002497 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 ret = proc_releaseinterface(ps, p);
2499 break;
2500
2501 case USBDEVFS_IOCTL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002502 snoop(&dev->dev, "%s: IOCTL\n", __func__);
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002503 ret = proc_ioctl_default(ps, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 break;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04002505
2506 case USBDEVFS_CLAIM_PORT:
2507 snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__);
2508 ret = proc_claim_port(ps, p);
2509 break;
2510
2511 case USBDEVFS_RELEASE_PORT:
2512 snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__);
2513 ret = proc_release_port(ps, p);
2514 break;
Hans de Goede19181bc2012-07-04 09:18:02 +02002515 case USBDEVFS_GET_CAPABILITIES:
2516 ret = proc_get_capabilities(ps, p);
2517 break;
Hans de Goede0837e7e2012-09-08 20:02:05 +02002518 case USBDEVFS_DISCONNECT_CLAIM:
2519 ret = proc_disconnect_claim(ps, p);
2520 break;
Hans de Goedebcf7f6e2013-10-09 17:19:31 +02002521 case USBDEVFS_ALLOC_STREAMS:
2522 ret = proc_alloc_streams(ps, p);
2523 break;
2524 case USBDEVFS_FREE_STREAMS:
2525 ret = proc_free_streams(ps, p);
2526 break;
Reilly Grantd883f522016-02-21 18:38:01 -03002527 case USBDEVFS_DROP_PRIVILEGES:
2528 ret = proc_drop_privileges(ps, p);
2529 break;
Alan Sternc01b2442017-06-05 10:28:01 -04002530 case USBDEVFS_GET_SPEED:
2531 ret = ps->dev->speed;
2532 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 }
Alan Stern3f2cee72015-01-29 11:29:13 -05002534
2535 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 usb_unlock_device(dev);
2537 if (ret >= 0)
Deepa Dinamani078cd822016-09-14 07:48:04 -07002538 inode->i_atime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 return ret;
2540}
2541
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002542static long usbdev_ioctl(struct file *file, unsigned int cmd,
2543 unsigned long arg)
2544{
2545 int ret;
2546
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002547 ret = usbdev_do_ioctl(file, cmd, (void __user *)arg);
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002548
2549 return ret;
2550}
2551
2552#ifdef CONFIG_COMPAT
2553static long usbdev_compat_ioctl(struct file *file, unsigned int cmd,
2554 unsigned long arg)
2555{
2556 int ret;
2557
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002558 ret = usbdev_do_ioctl(file, cmd, compat_ptr(arg));
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002559
2560 return ret;
2561}
2562#endif
2563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564/* No kernel lock - fine */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002565static unsigned int usbdev_poll(struct file *file,
2566 struct poll_table_struct *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002568 struct usb_dev_state *ps = file->private_data;
Tobias Klauserec17cf12006-09-13 21:38:41 +02002569 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
2571 poll_wait(file, &ps->wait, wait);
2572 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
2573 mask |= POLLOUT | POLLWRNORM;
Alan Stern349710c2006-07-01 22:05:56 -04002574 if (!connected(ps))
Alan Stern5cce4382016-06-10 14:42:55 -04002575 mask |= POLLHUP;
2576 if (list_empty(&ps->list))
2577 mask |= POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 return mask;
2579}
2580
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002581const struct file_operations usbdev_file_operations = {
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002582 .owner = THIS_MODULE,
Al Virob25472f2015-12-05 22:04:48 -05002583 .llseek = no_seek_end_llseek,
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002584 .read = usbdev_read,
2585 .poll = usbdev_poll,
2586 .unlocked_ioctl = usbdev_ioctl,
2587#ifdef CONFIG_COMPAT
2588 .compat_ioctl = usbdev_compat_ioctl,
2589#endif
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01002590 .mmap = usbdev_mmap,
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002591 .open = usbdev_open,
2592 .release = usbdev_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593};
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002594
Alan Stern501950d2009-01-13 11:33:42 -05002595static void usbdev_remove(struct usb_device *udev)
Alan Sterncd9f0372008-06-24 14:47:04 -04002596{
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002597 struct usb_dev_state *ps;
Alan Sterncd9f0372008-06-24 14:47:04 -04002598 struct siginfo sinfo;
2599
2600 while (!list_empty(&udev->filelist)) {
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002601 ps = list_entry(udev->filelist.next, struct usb_dev_state, list);
Alan Sterncd9f0372008-06-24 14:47:04 -04002602 destroy_all_async(ps);
2603 wake_up_all(&ps->wait);
2604 list_del_init(&ps->list);
2605 if (ps->discsignr) {
Alan Sternf0c2b682015-02-13 10:54:53 -05002606 memset(&sinfo, 0, sizeof(sinfo));
Alan Sterncd9f0372008-06-24 14:47:04 -04002607 sinfo.si_signo = ps->discsignr;
2608 sinfo.si_errno = EPIPE;
2609 sinfo.si_code = SI_ASYNCIO;
2610 sinfo.si_addr = ps->disccontext;
Serge Hallynd178bc32011-09-26 10:45:18 -05002611 kill_pid_info_as_cred(ps->discsignr, &sinfo,
2612 ps->disc_pid, ps->cred, ps->secid);
Alan Sterncd9f0372008-06-24 14:47:04 -04002613 }
2614 }
2615}
2616
Alan Stern501950d2009-01-13 11:33:42 -05002617static int usbdev_notify(struct notifier_block *self,
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002618 unsigned long action, void *dev)
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002619{
2620 switch (action) {
2621 case USB_DEVICE_ADD:
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002622 break;
2623 case USB_DEVICE_REMOVE:
Alan Stern501950d2009-01-13 11:33:42 -05002624 usbdev_remove(dev);
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002625 break;
2626 }
2627 return NOTIFY_OK;
2628}
2629
2630static struct notifier_block usbdev_nb = {
Chase Metzger00fe52d2015-04-09 21:41:52 -07002631 .notifier_call = usbdev_notify,
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002632};
2633
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -07002634static struct cdev usb_device_cdev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002635
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002636int __init usb_devio_init(void)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002637{
2638 int retval;
2639
Alan Sternfad21bd2005-08-10 15:15:57 -04002640 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002641 "usb_device");
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002642 if (retval) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -07002643 printk(KERN_ERR "Unable to register minors for usb_device\n");
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002644 goto out;
2645 }
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002646 cdev_init(&usb_device_cdev, &usbdev_file_operations);
Alan Sternfad21bd2005-08-10 15:15:57 -04002647 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002648 if (retval) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -07002649 printk(KERN_ERR "Unable to get usb_device major %d\n",
2650 USB_DEVICE_MAJOR);
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002651 goto error_cdev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002652 }
Alan Stern501950d2009-01-13 11:33:42 -05002653 usb_register_notify(&usbdev_nb);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002654out:
2655 return retval;
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002656
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002657error_cdev:
2658 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
2659 goto out;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002660}
2661
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002662void usb_devio_cleanup(void)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002663{
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002664 usb_unregister_notify(&usbdev_nb);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002665 cdev_del(&usb_device_cdev);
Alan Sternfad21bd2005-08-10 15:15:57 -04002666 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002667}