blob: 39da1662b76fb1c6ff60b199d2136c599794625e [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>
39#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/signal.h>
41#include <linux/poll.h>
42#include <linux/module.h>
Chen Gangb11b2e12013-02-02 15:57:53 +080043#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/usb.h>
45#include <linux/usbdevice_fs.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020046#include <linux/usb/hcd.h> /* for usbcore internals */
Kay Sieversfbf82fd2005-07-31 01:05:53 +020047#include <linux/cdev.h>
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -070048#include <linux/notifier.h>
David Quigley7a019552006-06-30 01:55:48 -070049#include <linux/security.h>
Serge Hallynd178bc32011-09-26 10:45:18 -050050#include <linux/user_namespace.h>
Hans de Goede3d97ff62012-07-04 09:18:03 +020051#include <linux/scatterlist.h>
Tülin İzere6889b32013-05-17 10:12:34 +030052#include <linux/uaccess.h>
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +010053#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/byteorder.h>
55#include <linux/moduleparam.h>
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include "usb.h"
58
Kay Sieversfbf82fd2005-07-31 01:05:53 +020059#define USB_MAXBUS 64
Tülin İzerfa86ad02013-05-17 10:13:24 +030060#define USB_DEVICE_MAX (USB_MAXBUS * 128)
Hans de Goede3d97ff62012-07-04 09:18:03 +020061#define USB_SG_SIZE 16384 /* split-size for large txs */
Kay Sieversfbf82fd2005-07-31 01:05:53 +020062
Alan Stern4a2a8a22006-07-01 22:05:01 -040063/* Mutual exclusion for removal, open, and release */
64DEFINE_MUTEX(usbfs_mutex);
65
Valentina Manea9b6f0c42014-03-10 10:36:40 +020066struct usb_dev_state {
Alan Sterncd9f0372008-06-24 14:47:04 -040067 struct list_head list; /* state list */
68 struct usb_device *dev;
69 struct file *file;
70 spinlock_t lock; /* protects the async urb lists */
71 struct list_head async_pending;
72 struct list_head async_completed;
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +010073 struct list_head memory_list;
Alan Sterncd9f0372008-06-24 14:47:04 -040074 wait_queue_head_t wait; /* wake up if a request completed */
75 unsigned int discsignr;
76 struct pid *disc_pid;
Serge Hallynd178bc32011-09-26 10:45:18 -050077 const struct cred *cred;
Alan Sterncd9f0372008-06-24 14:47:04 -040078 void __user *disccontext;
79 unsigned long ifclaimed;
80 u32 secid;
Alan Stern01c64602009-09-01 11:09:56 -040081 u32 disabled_bulk_eps;
Alan Sterncd9f0372008-06-24 14:47:04 -040082};
83
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +010084struct usb_memory {
85 struct list_head memlist;
86 int vma_use_count;
87 int urb_use_count;
88 u32 size;
89 void *mem;
90 dma_addr_t dma_handle;
91 unsigned long vm_start;
92 struct usb_dev_state *ps;
93};
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095struct async {
96 struct list_head asynclist;
Valentina Manea9b6f0c42014-03-10 10:36:40 +020097 struct usb_dev_state *ps;
Eric W. Biederman2425c082006-10-02 02:17:28 -070098 struct pid *pid;
Serge Hallynd178bc32011-09-26 10:45:18 -050099 const struct cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 unsigned int signr;
101 unsigned int ifnum;
102 void __user *userbuffer;
103 void __user *userurb;
104 struct urb *urb;
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +0100105 struct usb_memory *usbm;
Alan Sternadd1aae2011-11-17 16:41:25 -0500106 unsigned int mem_usage;
Alan Sterne0152682007-08-24 15:42:52 -0400107 int status;
David Quigley7a019552006-06-30 01:55:48 -0700108 u32 secid;
Alan Stern01c64602009-09-01 11:09:56 -0400109 u8 bulk_addr;
110 u8 bulk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030113static bool usbfs_snoop;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800114module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
115MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Alan Stern0290cc92015-11-20 13:53:22 -0500117static unsigned usbfs_snoop_max = 65536;
118module_param(usbfs_snoop_max, uint, S_IRUGO | S_IWUSR);
119MODULE_PARM_DESC(usbfs_snoop_max,
120 "maximum number of bytes to print while snooping");
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#define snoop(dev, format, arg...) \
123 do { \
124 if (usbfs_snoop) \
Kris Borerf355e832015-08-07 07:22:44 -0400125 dev_info(dev, format, ## arg); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 } while (0)
127
Alan Stern4c6e8972009-06-29 11:02:04 -0400128enum snoop_when {
129 SUBMIT, COMPLETE
130};
131
Alan Sternfad21bd2005-08-10 15:15:57 -0400132#define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
133
Alan Sternadd1aae2011-11-17 16:41:25 -0500134/* Limit on the total amount of memory we can allocate for transfers */
Alan Stern3f5eb8d2011-11-17 16:41:35 -0500135static unsigned usbfs_memory_mb = 16;
136module_param(usbfs_memory_mb, uint, 0644);
137MODULE_PARM_DESC(usbfs_memory_mb,
138 "maximum MB allowed for usbfs buffers (0 = no limit)");
139
Rahul Bedarkar025d4432014-01-04 11:24:41 +0530140/* Hard limit, necessary to avoid arithmetic overflow */
Alan Stern3f5eb8d2011-11-17 16:41:35 -0500141#define USBFS_XFER_MAX (UINT_MAX / 2 - 1000000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Alan Sternadd1aae2011-11-17 16:41:25 -0500143static atomic_t usbfs_memory_usage; /* Total memory currently allocated */
144
145/* Check whether it's okay to allocate more memory for a transfer */
146static int usbfs_increase_memory_usage(unsigned amount)
147{
Alan Stern3f5eb8d2011-11-17 16:41:35 -0500148 unsigned lim;
149
150 /*
151 * Convert usbfs_memory_mb to bytes, avoiding overflows.
152 * 0 means use the hard limit (effectively unlimited).
153 */
154 lim = ACCESS_ONCE(usbfs_memory_mb);
155 if (lim == 0 || lim > (USBFS_XFER_MAX >> 20))
156 lim = USBFS_XFER_MAX;
157 else
158 lim <<= 20;
159
Alan Sternadd1aae2011-11-17 16:41:25 -0500160 atomic_add(amount, &usbfs_memory_usage);
Alan Stern3f5eb8d2011-11-17 16:41:35 -0500161 if (atomic_read(&usbfs_memory_usage) <= lim)
Alan Sternadd1aae2011-11-17 16:41:25 -0500162 return 0;
163 atomic_sub(amount, &usbfs_memory_usage);
164 return -ENOMEM;
165}
166
167/* Memory for a transfer is being deallocated */
168static void usbfs_decrease_memory_usage(unsigned amount)
169{
170 atomic_sub(amount, &usbfs_memory_usage);
171}
Alan Stern4c6e8972009-06-29 11:02:04 -0400172
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200173static int connected(struct usb_dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Alan Stern349710c2006-07-01 22:05:56 -0400175 return (!list_empty(&ps->list) &&
176 ps->dev->state != USB_STATE_NOTATTACHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +0100179static void dec_usb_memory_use_count(struct usb_memory *usbm, int *count)
180{
181 struct usb_dev_state *ps = usbm->ps;
182 unsigned long flags;
183
184 spin_lock_irqsave(&ps->lock, flags);
185 --*count;
186 if (usbm->urb_use_count == 0 && usbm->vma_use_count == 0) {
187 list_del(&usbm->memlist);
188 spin_unlock_irqrestore(&ps->lock, flags);
189
190 usb_free_coherent(ps->dev, usbm->size, usbm->mem,
191 usbm->dma_handle);
192 usbfs_decrease_memory_usage(
193 usbm->size + sizeof(struct usb_memory));
194 kfree(usbm);
195 } else {
196 spin_unlock_irqrestore(&ps->lock, flags);
197 }
198}
199
200static void usbdev_vm_open(struct vm_area_struct *vma)
201{
202 struct usb_memory *usbm = vma->vm_private_data;
203 unsigned long flags;
204
205 spin_lock_irqsave(&usbm->ps->lock, flags);
206 ++usbm->vma_use_count;
207 spin_unlock_irqrestore(&usbm->ps->lock, flags);
208}
209
210static void usbdev_vm_close(struct vm_area_struct *vma)
211{
212 struct usb_memory *usbm = vma->vm_private_data;
213
214 dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
215}
216
217struct vm_operations_struct usbdev_vm_ops = {
218 .open = usbdev_vm_open,
219 .close = usbdev_vm_close
220};
221
222static int usbdev_mmap(struct file *file, struct vm_area_struct *vma)
223{
224 struct usb_memory *usbm = NULL;
225 struct usb_dev_state *ps = file->private_data;
226 size_t size = vma->vm_end - vma->vm_start;
227 void *mem;
228 unsigned long flags;
229 dma_addr_t dma_handle;
230 int ret;
231
232 ret = usbfs_increase_memory_usage(size + sizeof(struct usb_memory));
233 if (ret)
234 goto error;
235
236 usbm = kzalloc(sizeof(struct usb_memory), GFP_KERNEL);
237 if (!usbm) {
238 ret = -ENOMEM;
239 goto error_decrease_mem;
240 }
241
242 mem = usb_alloc_coherent(ps->dev, size, GFP_USER, &dma_handle);
243 if (!mem) {
244 ret = -ENOMEM;
245 goto error_free_usbm;
246 }
247
248 memset(mem, 0, size);
249
250 usbm->mem = mem;
251 usbm->dma_handle = dma_handle;
252 usbm->size = size;
253 usbm->ps = ps;
254 usbm->vm_start = vma->vm_start;
255 usbm->vma_use_count = 1;
256 INIT_LIST_HEAD(&usbm->memlist);
257
258 if (remap_pfn_range(vma, vma->vm_start,
259 virt_to_phys(usbm->mem) >> PAGE_SHIFT,
260 size, vma->vm_page_prot) < 0) {
261 dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
262 return -EAGAIN;
263 }
264
265 vma->vm_flags |= VM_IO;
266 vma->vm_flags |= (VM_DONTEXPAND | VM_DONTDUMP);
267 vma->vm_ops = &usbdev_vm_ops;
268 vma->vm_private_data = usbm;
269
270 spin_lock_irqsave(&ps->lock, flags);
271 list_add_tail(&usbm->memlist, &ps->memory_list);
272 spin_unlock_irqrestore(&ps->lock, flags);
273
274 return 0;
275
276error_free_usbm:
277 kfree(usbm);
278error_decrease_mem:
279 usbfs_decrease_memory_usage(size + sizeof(struct usb_memory));
280error:
281 return ret;
282}
283
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800284static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
285 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200287 struct usb_dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct usb_device *dev = ps->dev;
289 ssize_t ret = 0;
290 unsigned len;
291 loff_t pos;
292 int i;
293
294 pos = *ppos;
295 usb_lock_device(dev);
Alan Stern349710c2006-07-01 22:05:56 -0400296 if (!connected(ps)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 ret = -ENODEV;
298 goto err;
299 } else if (pos < 0) {
300 ret = -EINVAL;
301 goto err;
302 }
303
304 if (pos < sizeof(struct usb_device_descriptor)) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800305 /* 18 bytes - fits on the stack */
306 struct usb_device_descriptor temp_desc;
Oliver Neukum8781ba02006-01-06 21:24:25 +0100307
308 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
Andrew Morton9fcd5c32006-01-18 23:55:07 -0800309 le16_to_cpus(&temp_desc.bcdUSB);
310 le16_to_cpus(&temp_desc.idVendor);
311 le16_to_cpus(&temp_desc.idProduct);
312 le16_to_cpus(&temp_desc.bcdDevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 len = sizeof(struct usb_device_descriptor) - pos;
315 if (len > nbytes)
316 len = nbytes;
Oliver Neukum8781ba02006-01-06 21:24:25 +0100317 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 ret = -EFAULT;
319 goto err;
320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 *ppos += len;
323 buf += len;
324 nbytes -= len;
325 ret += len;
326 }
327
328 pos = sizeof(struct usb_device_descriptor);
329 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
330 struct usb_config_descriptor *config =
331 (struct usb_config_descriptor *)dev->rawdescriptors[i];
332 unsigned int length = le16_to_cpu(config->wTotalLength);
333
334 if (*ppos < pos + length) {
335
336 /* The descriptor may claim to be longer than it
337 * really is. Here is the actual allocated length. */
338 unsigned alloclen =
339 le16_to_cpu(dev->config[i].desc.wTotalLength);
340
341 len = length - (*ppos - pos);
342 if (len > nbytes)
343 len = nbytes;
344
345 /* Simply don't write (skip over) unallocated parts */
346 if (alloclen > (*ppos - pos)) {
347 alloclen -= (*ppos - pos);
348 if (copy_to_user(buf,
349 dev->rawdescriptors[i] + (*ppos - pos),
350 min(len, alloclen))) {
351 ret = -EFAULT;
352 goto err;
353 }
354 }
355
356 *ppos += len;
357 buf += len;
358 nbytes -= len;
359 ret += len;
360 }
361
362 pos += length;
363 }
364
365err:
366 usb_unlock_device(dev);
367 return ret;
368}
369
370/*
371 * async list handling
372 */
373
374static struct async *alloc_async(unsigned int numisoframes)
375{
Pete Zaitcevdd95b812008-01-05 02:01:07 -0800376 struct async *as;
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400377
Pete Zaitcevdd95b812008-01-05 02:01:07 -0800378 as = kzalloc(sizeof(struct async), GFP_KERNEL);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800379 if (!as)
380 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
382 if (!as->urb) {
383 kfree(as);
384 return NULL;
385 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800386 return as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389static void free_async(struct async *as)
390{
Hans de Goede3d97ff62012-07-04 09:18:03 +0200391 int i;
392
Eric W. Biederman2425c082006-10-02 02:17:28 -0700393 put_pid(as->pid);
Sarah Sharp1b41c832011-12-16 11:26:30 -0800394 if (as->cred)
395 put_cred(as->cred);
Hans de Goede3d97ff62012-07-04 09:18:03 +0200396 for (i = 0; i < as->urb->num_sgs; i++) {
397 if (sg_page(&as->urb->sg[i]))
398 kfree(sg_virt(&as->urb->sg[i]));
399 }
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +0100400
Hans de Goede3d97ff62012-07-04 09:18:03 +0200401 kfree(as->urb->sg);
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +0100402 if (as->usbm == NULL)
403 kfree(as->urb->transfer_buffer);
404 else
405 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count);
406
Jesper Juhl6fd19f42005-04-18 17:39:33 -0700407 kfree(as->urb->setup_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 usb_free_urb(as->urb);
Alan Sternadd1aae2011-11-17 16:41:25 -0500409 usbfs_decrease_memory_usage(as->mem_usage);
Jesper Juhl6fd19f42005-04-18 17:39:33 -0700410 kfree(as);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Alan Sternd34d9722009-03-09 13:44:48 -0400413static void async_newpending(struct async *as)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200415 struct usb_dev_state *ps = as->ps;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800416 unsigned long flags;
417
418 spin_lock_irqsave(&ps->lock, flags);
419 list_add_tail(&as->asynclist, &ps->async_pending);
420 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Alan Sternd34d9722009-03-09 13:44:48 -0400423static void async_removepending(struct async *as)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200425 struct usb_dev_state *ps = as->ps;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800426 unsigned long flags;
427
428 spin_lock_irqsave(&ps->lock, flags);
429 list_del_init(&as->asynclist);
430 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200433static struct async *async_getcompleted(struct usb_dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800435 unsigned long flags;
436 struct async *as = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800438 spin_lock_irqsave(&ps->lock, flags);
439 if (!list_empty(&ps->async_completed)) {
440 as = list_entry(ps->async_completed.next, struct async,
441 asynclist);
442 list_del_init(&as->asynclist);
443 }
444 spin_unlock_irqrestore(&ps->lock, flags);
445 return as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200448static struct async *async_getpending(struct usb_dev_state *ps,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800449 void __user *userurb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800451 struct async *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 list_for_each_entry(as, &ps->async_pending, asynclist)
454 if (as->userurb == userurb) {
455 list_del_init(&as->asynclist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return as;
457 }
Huajun Li4e09dcf2012-05-18 20:12:51 +0800458
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800459 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Alan Stern4c6e8972009-06-29 11:02:04 -0400462static void snoop_urb(struct usb_device *udev,
463 void __user *userurb, int pipe, unsigned length,
Chris Frey0880aef2010-01-26 17:07:29 -0500464 int timeout_or_status, enum snoop_when when,
465 unsigned char *data, unsigned data_len)
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700466{
Alan Stern4c6e8972009-06-29 11:02:04 -0400467 static const char *types[] = {"isoc", "int", "ctrl", "bulk"};
468 static const char *dirs[] = {"out", "in"};
469 int ep;
470 const char *t, *d;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700471
472 if (!usbfs_snoop)
473 return;
474
Alan Stern4c6e8972009-06-29 11:02:04 -0400475 ep = usb_pipeendpoint(pipe);
476 t = types[usb_pipetype(pipe)];
477 d = dirs[!!usb_pipein(pipe)];
478
479 if (userurb) { /* Async */
480 if (when == SUBMIT)
481 dev_info(&udev->dev, "userurb %p, ep%d %s-%s, "
482 "length %u\n",
483 userurb, ep, t, d, length);
484 else
485 dev_info(&udev->dev, "userurb %p, ep%d %s-%s, "
486 "actual_length %u status %d\n",
487 userurb, ep, t, d, length,
488 timeout_or_status);
489 } else {
490 if (when == SUBMIT)
491 dev_info(&udev->dev, "ep%d %s-%s, length %u, "
492 "timeout %d\n",
493 ep, t, d, length, timeout_or_status);
494 else
495 dev_info(&udev->dev, "ep%d %s-%s, actual_length %u, "
496 "status %d\n",
497 ep, t, d, length, timeout_or_status);
498 }
Chris Frey0880aef2010-01-26 17:07:29 -0500499
Alan Stern0290cc92015-11-20 13:53:22 -0500500 data_len = min(data_len, usbfs_snoop_max);
Chris Frey0880aef2010-01-26 17:07:29 -0500501 if (data && data_len > 0) {
502 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
503 data, data_len, 1);
504 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700505}
506
Hans de Goede3d97ff62012-07-04 09:18:03 +0200507static void snoop_urb_data(struct urb *urb, unsigned len)
508{
509 int i, size;
510
Alan Stern0290cc92015-11-20 13:53:22 -0500511 len = min(len, usbfs_snoop_max);
512 if (!usbfs_snoop || len == 0)
Hans de Goede3d97ff62012-07-04 09:18:03 +0200513 return;
514
515 if (urb->num_sgs == 0) {
516 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
517 urb->transfer_buffer, len, 1);
518 return;
519 }
520
521 for (i = 0; i < urb->num_sgs && len; i++) {
522 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
523 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
524 sg_virt(&urb->sg[i]), size, 1);
525 len -= size;
526 }
527}
528
529static int copy_urb_data_to_user(u8 __user *userbuffer, struct urb *urb)
530{
531 unsigned i, len, size;
532
533 if (urb->number_of_packets > 0) /* Isochronous */
534 len = urb->transfer_buffer_length;
535 else /* Non-Isoc */
536 len = urb->actual_length;
537
538 if (urb->num_sgs == 0) {
539 if (copy_to_user(userbuffer, urb->transfer_buffer, len))
540 return -EFAULT;
541 return 0;
542 }
543
544 for (i = 0; i < urb->num_sgs && len; i++) {
545 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
546 if (copy_to_user(userbuffer, sg_virt(&urb->sg[i]), size))
547 return -EFAULT;
548 userbuffer += size;
549 len -= size;
550 }
551
552 return 0;
553}
554
Alan Stern01c64602009-09-01 11:09:56 -0400555#define AS_CONTINUATION 1
556#define AS_UNLINK 2
557
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200558static void cancel_bulk_urbs(struct usb_dev_state *ps, unsigned bulk_addr)
Alan Stern01c64602009-09-01 11:09:56 -0400559__releases(ps->lock)
560__acquires(ps->lock)
561{
Huajun Li4e09dcf2012-05-18 20:12:51 +0800562 struct urb *urb;
Alan Stern01c64602009-09-01 11:09:56 -0400563 struct async *as;
564
565 /* Mark all the pending URBs that match bulk_addr, up to but not
566 * including the first one without AS_CONTINUATION. If such an
567 * URB is encountered then a new transfer has already started so
568 * the endpoint doesn't need to be disabled; otherwise it does.
569 */
570 list_for_each_entry(as, &ps->async_pending, asynclist) {
571 if (as->bulk_addr == bulk_addr) {
572 if (as->bulk_status != AS_CONTINUATION)
573 goto rescan;
574 as->bulk_status = AS_UNLINK;
575 as->bulk_addr = 0;
576 }
577 }
578 ps->disabled_bulk_eps |= (1 << bulk_addr);
579
580 /* Now carefully unlink all the marked pending URBs */
581 rescan:
582 list_for_each_entry(as, &ps->async_pending, asynclist) {
583 if (as->bulk_status == AS_UNLINK) {
584 as->bulk_status = 0; /* Only once */
Huajun Li4e09dcf2012-05-18 20:12:51 +0800585 urb = as->urb;
586 usb_get_urb(urb);
Alan Stern01c64602009-09-01 11:09:56 -0400587 spin_unlock(&ps->lock); /* Allow completions */
Huajun Li4e09dcf2012-05-18 20:12:51 +0800588 usb_unlink_urb(urb);
589 usb_put_urb(urb);
Alan Stern01c64602009-09-01 11:09:56 -0400590 spin_lock(&ps->lock);
591 goto rescan;
592 }
593 }
594}
595
David Howells7d12e782006-10-05 14:55:46 +0100596static void async_completed(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800598 struct async *as = urb->context;
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200599 struct usb_dev_state *ps = as->ps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 struct siginfo sinfo;
Oliver Neukum516a1a02009-07-08 19:09:23 +0200601 struct pid *pid = NULL;
Oliver Neukum516a1a02009-07-08 19:09:23 +0200602 u32 secid = 0;
Serge Hallynd178bc32011-09-26 10:45:18 -0500603 const struct cred *cred = NULL;
Oliver Neukum516a1a02009-07-08 19:09:23 +0200604 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800606 spin_lock(&ps->lock);
607 list_move_tail(&as->asynclist, &ps->async_completed);
Alan Sterne0152682007-08-24 15:42:52 -0400608 as->status = urb->status;
Oliver Neukum516a1a02009-07-08 19:09:23 +0200609 signr = as->signr;
610 if (signr) {
Alan Sternf0c2b682015-02-13 10:54:53 -0500611 memset(&sinfo, 0, sizeof(sinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 sinfo.si_signo = as->signr;
Alan Sterne0152682007-08-24 15:42:52 -0400613 sinfo.si_errno = as->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 sinfo.si_code = SI_ASYNCIO;
615 sinfo.si_addr = as->userurb;
Serge Hallynaec01c52011-09-26 10:18:29 -0500616 pid = get_pid(as->pid);
Serge Hallynd178bc32011-09-26 10:45:18 -0500617 cred = get_cred(as->cred);
Oliver Neukum516a1a02009-07-08 19:09:23 +0200618 secid = as->secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700620 snoop(&urb->dev->dev, "urb complete\n");
Alan Stern4c6e8972009-06-29 11:02:04 -0400621 snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length,
Hans de Goede3d97ff62012-07-04 09:18:03 +0200622 as->status, COMPLETE, NULL, 0);
Dan Carpenter83ed07c2015-05-18 15:29:51 +0300623 if ((urb->transfer_flags & URB_DIR_MASK) == URB_DIR_IN)
Hans de Goede3d97ff62012-07-04 09:18:03 +0200624 snoop_urb_data(urb, urb->actual_length);
625
Alan Stern01c64602009-09-01 11:09:56 -0400626 if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET &&
627 as->status != -ENOENT)
628 cancel_bulk_urbs(ps, as->bulk_addr);
Oliver Neukum516a1a02009-07-08 19:09:23 +0200629 spin_unlock(&ps->lock);
630
Serge Hallynaec01c52011-09-26 10:18:29 -0500631 if (signr) {
Serge Hallynd178bc32011-09-26 10:45:18 -0500632 kill_pid_info_as_cred(sinfo.si_signo, &sinfo, pid, cred, secid);
Serge Hallynaec01c52011-09-26 10:18:29 -0500633 put_pid(pid);
Serge Hallynd178bc32011-09-26 10:45:18 -0500634 put_cred(cred);
Serge Hallynaec01c52011-09-26 10:18:29 -0500635 }
Oliver Neukum516a1a02009-07-08 19:09:23 +0200636
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700637 wake_up(&ps->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200640static void destroy_async(struct usb_dev_state *ps, struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Huajun Li4e09dcf2012-05-18 20:12:51 +0800642 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 struct async *as;
644 unsigned long flags;
645
646 spin_lock_irqsave(&ps->lock, flags);
647 while (!list_empty(list)) {
648 as = list_entry(list->next, struct async, asynclist);
649 list_del_init(&as->asynclist);
Huajun Li4e09dcf2012-05-18 20:12:51 +0800650 urb = as->urb;
651 usb_get_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 /* drop the spinlock so the completion handler can run */
654 spin_unlock_irqrestore(&ps->lock, flags);
Huajun Li4e09dcf2012-05-18 20:12:51 +0800655 usb_kill_urb(urb);
656 usb_put_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 spin_lock_irqsave(&ps->lock, flags);
658 }
659 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200662static void destroy_async_on_interface(struct usb_dev_state *ps,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800663 unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct list_head *p, *q, hitlist;
666 unsigned long flags;
667
668 INIT_LIST_HEAD(&hitlist);
669 spin_lock_irqsave(&ps->lock, flags);
670 list_for_each_safe(p, q, &ps->async_pending)
671 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
672 list_move_tail(p, &hitlist);
673 spin_unlock_irqrestore(&ps->lock, flags);
674 destroy_async(ps, &hitlist);
675}
676
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200677static void destroy_all_async(struct usb_dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800679 destroy_async(ps, &ps->async_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
682/*
683 * interface claims are made only at the request of user level code,
684 * which can also release them (explicitly or by closing files).
685 * they're also undone when devices disconnect.
686 */
687
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800688static int driver_probe(struct usb_interface *intf,
689 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 return -ENODEV;
692}
693
694static void driver_disconnect(struct usb_interface *intf)
695{
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200696 struct usb_dev_state *ps = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
698
699 if (!ps)
700 return;
701
702 /* NOTE: this relies on usbcore having canceled and completed
703 * all pending I/O requests; 2.6 does that.
704 */
705
706 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
707 clear_bit(ifnum, &ps->ifclaimed);
708 else
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -0700709 dev_warn(&intf->dev, "interface number %u out of range\n",
710 ifnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800712 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 /* force async requests to complete */
715 destroy_async_on_interface(ps, ifnum);
716}
717
Alan Stern2e2eb832007-12-04 14:35:15 -0500718/* The following routines are merely placeholders. There is no way
719 * to inform a user task about suspend or resumes.
720 */
721static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
722{
723 return 0;
724}
725
726static int driver_resume(struct usb_interface *intf)
727{
728 return 0;
729}
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731struct usb_driver usbfs_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 .name = "usbfs",
733 .probe = driver_probe,
734 .disconnect = driver_disconnect,
Alan Stern2e2eb832007-12-04 14:35:15 -0500735 .suspend = driver_suspend,
736 .resume = driver_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737};
738
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200739static int claimintf(struct usb_dev_state *ps, unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 struct usb_device *dev = ps->dev;
742 struct usb_interface *intf;
743 int err;
744
745 if (ifnum >= 8*sizeof(ps->ifclaimed))
746 return -EINVAL;
747 /* already claimed */
748 if (test_bit(ifnum, &ps->ifclaimed))
749 return 0;
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 intf = usb_ifnum_to_if(dev, ifnum);
752 if (!intf)
753 err = -ENOENT;
754 else
755 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (err == 0)
757 set_bit(ifnum, &ps->ifclaimed);
758 return err;
759}
760
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200761static int releaseintf(struct usb_dev_state *ps, unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 struct usb_device *dev;
764 struct usb_interface *intf;
765 int err;
766
767 err = -EINVAL;
768 if (ifnum >= 8*sizeof(ps->ifclaimed))
769 return err;
770 dev = ps->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 intf = usb_ifnum_to_if(dev, ifnum);
772 if (!intf)
773 err = -ENOENT;
774 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
775 usb_driver_release_interface(&usbfs_driver, intf);
776 err = 0;
777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return err;
779}
780
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200781static int checkintf(struct usb_dev_state *ps, unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 if (ps->dev->state != USB_STATE_CONFIGURED)
784 return -EHOSTUNREACH;
785 if (ifnum >= 8*sizeof(ps->ifclaimed))
786 return -EINVAL;
787 if (test_bit(ifnum, &ps->ifclaimed))
788 return 0;
789 /* if not yet claimed, claim it for the driver */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800790 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
791 "interface %u before use\n", task_pid_nr(current),
792 current->comm, ifnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return claimintf(ps, ifnum);
794}
795
796static int findintfep(struct usb_device *dev, unsigned int ep)
797{
798 unsigned int i, j, e;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800799 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 struct usb_host_interface *alts;
801 struct usb_endpoint_descriptor *endpt;
802
803 if (ep & ~(USB_DIR_IN|0xf))
804 return -EINVAL;
805 if (!dev->actconfig)
806 return -ESRCH;
807 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
808 intf = dev->actconfig->interface[i];
809 for (j = 0; j < intf->num_altsetting; j++) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800810 alts = &intf->altsetting[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
812 endpt = &alts->endpoint[e].desc;
813 if (endpt->bEndpointAddress == ep)
814 return alts->desc.bInterfaceNumber;
815 }
816 }
817 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800818 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200821static int check_ctrlrecip(struct usb_dev_state *ps, unsigned int requesttype,
Matthias Dellweg393cbb52011-09-25 14:26:25 +0200822 unsigned int request, unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 int ret = 0;
Matthias Dellweg393cbb52011-09-25 14:26:25 +0200825 struct usb_host_interface *alt_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
David Vrabel6da9c992009-02-18 14:43:47 +0000827 if (ps->dev->state != USB_STATE_UNAUTHENTICATED
828 && ps->dev->state != USB_STATE_ADDRESS
Horst Schirmeier24f8b112006-03-11 00:16:55 -0800829 && ps->dev->state != USB_STATE_CONFIGURED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return -EHOSTUNREACH;
831 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
832 return 0;
833
Matthias Dellweg393cbb52011-09-25 14:26:25 +0200834 /*
835 * check for the special corner case 'get_device_id' in the printer
Hans de Goede5dc50c32013-07-12 10:04:18 +0200836 * class specification, which we always want to allow as it is used
837 * to query things like ink level, etc.
Matthias Dellweg393cbb52011-09-25 14:26:25 +0200838 */
839 if (requesttype == 0xa1 && request == 0) {
840 alt_setting = usb_find_alt_setting(ps->dev->actconfig,
841 index >> 8, index & 0xff);
842 if (alt_setting
843 && alt_setting->desc.bInterfaceClass == USB_CLASS_PRINTER)
Hans de Goede5dc50c32013-07-12 10:04:18 +0200844 return 0;
Matthias Dellweg393cbb52011-09-25 14:26:25 +0200845 }
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 index &= 0xff;
848 switch (requesttype & USB_RECIP_MASK) {
849 case USB_RECIP_ENDPOINT:
Hans de Goede1361bf4b2013-04-16 11:08:33 +0200850 if ((index & ~USB_DIR_IN) == 0)
851 return 0;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800852 ret = findintfep(ps->dev, index);
Kurt Garloff831abf72013-09-24 14:13:48 +0200853 if (ret < 0) {
854 /*
855 * Some not fully compliant Win apps seem to get
856 * index wrong and have the endpoint number here
857 * rather than the endpoint address (with the
858 * correct direction). Win does let this through,
859 * so we'll not reject it here but leave it to
860 * the device to not break KVM. But we warn.
861 */
862 ret = findintfep(ps->dev, index ^ 0x80);
863 if (ret >= 0)
864 dev_info(&ps->dev->dev,
865 "%s: process %i (%s) requesting ep %02x but needs %02x\n",
866 __func__, task_pid_nr(current),
867 current->comm, index, index ^ 0x80);
868 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800869 if (ret >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 ret = checkintf(ps, ret);
871 break;
872
873 case USB_RECIP_INTERFACE:
874 ret = checkintf(ps, index);
875 break;
876 }
877 return ret;
878}
879
Hans de Goede2fec32b2013-10-09 17:19:30 +0200880static struct usb_host_endpoint *ep_to_host_endpoint(struct usb_device *dev,
881 unsigned char ep)
882{
883 if (ep & USB_ENDPOINT_DIR_MASK)
884 return dev->ep_in[ep & USB_ENDPOINT_NUMBER_MASK];
885 else
886 return dev->ep_out[ep & USB_ENDPOINT_NUMBER_MASK];
887}
888
Linus Torvalds3e75c6d2014-04-01 17:06:09 -0700889static int parse_usbdevfs_streams(struct usb_dev_state *ps,
Hans de Goedebcf7f6e2013-10-09 17:19:31 +0200890 struct usbdevfs_streams __user *streams,
891 unsigned int *num_streams_ret,
892 unsigned int *num_eps_ret,
893 struct usb_host_endpoint ***eps_ret,
894 struct usb_interface **intf_ret)
895{
896 unsigned int i, num_streams, num_eps;
897 struct usb_host_endpoint **eps;
898 struct usb_interface *intf = NULL;
899 unsigned char ep;
900 int ifnum, ret;
901
902 if (get_user(num_streams, &streams->num_streams) ||
903 get_user(num_eps, &streams->num_eps))
904 return -EFAULT;
905
906 if (num_eps < 1 || num_eps > USB_MAXENDPOINTS)
907 return -EINVAL;
908
909 /* The XHCI controller allows max 2 ^ 16 streams */
910 if (num_streams_ret && (num_streams < 2 || num_streams > 65536))
911 return -EINVAL;
912
913 eps = kmalloc(num_eps * sizeof(*eps), GFP_KERNEL);
914 if (!eps)
915 return -ENOMEM;
916
917 for (i = 0; i < num_eps; i++) {
918 if (get_user(ep, &streams->eps[i])) {
919 ret = -EFAULT;
920 goto error;
921 }
922 eps[i] = ep_to_host_endpoint(ps->dev, ep);
923 if (!eps[i]) {
924 ret = -EINVAL;
925 goto error;
926 }
927
928 /* usb_alloc/free_streams operate on an usb_interface */
929 ifnum = findintfep(ps->dev, ep);
930 if (ifnum < 0) {
931 ret = ifnum;
932 goto error;
933 }
934
935 if (i == 0) {
936 ret = checkintf(ps, ifnum);
937 if (ret < 0)
938 goto error;
939 intf = usb_ifnum_to_if(ps->dev, ifnum);
940 } else {
941 /* Verify all eps belong to the same interface */
942 if (ifnum != intf->altsetting->desc.bInterfaceNumber) {
943 ret = -EINVAL;
944 goto error;
945 }
946 }
947 }
948
949 if (num_streams_ret)
950 *num_streams_ret = num_streams;
951 *num_eps_ret = num_eps;
952 *eps_ret = eps;
953 *intf_ret = intf;
954
955 return 0;
956
957error:
958 kfree(eps);
959 return ret;
960}
961
Alan Stern61ad04a2008-06-24 14:47:12 -0400962static int match_devt(struct device *dev, void *data)
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700963{
David Howellsa80d5ff2008-07-02 12:28:55 +0100964 return dev->devt == (dev_t) (unsigned long) data;
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100965}
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700966
Alan Stern61ad04a2008-06-24 14:47:12 -0400967static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100968{
969 struct device *dev;
970
David Howellsa80d5ff2008-07-02 12:28:55 +0100971 dev = bus_find_device(&usb_bus_type, NULL,
972 (void *) (unsigned long) devt, match_devt);
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100973 if (!dev)
974 return NULL;
Geliang Tang69ab55d2015-12-23 21:26:50 +0800975 return to_usb_device(dev);
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100976}
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978/*
979 * file operations
980 */
981static int usbdev_open(struct inode *inode, struct file *file)
982{
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200983 struct usb_device *dev = NULL;
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200984 struct usb_dev_state *ps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 int ret;
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 ret = -ENOMEM;
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200988 ps = kmalloc(sizeof(struct usb_dev_state), GFP_KERNEL);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800989 if (!ps)
Alan Stern62e299e2010-01-08 12:56:19 -0500990 goto out_free_ps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Alan Stern01105a22009-07-30 15:28:14 -0400992 ret = -ENODEV;
Alan Stern61ad04a2008-06-24 14:47:12 -0400993
Alan Stern62e299e2010-01-08 12:56:19 -0500994 /* Protect against simultaneous removal or release */
995 mutex_lock(&usbfs_mutex);
996
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100997 /* usbdev device-node */
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200998 if (imajor(inode) == USB_DEVICE_MAJOR)
Alan Stern61ad04a2008-06-24 14:47:12 -0400999 dev = usbdev_lookup_by_devt(inode->i_rdev);
Alan Stern62e299e2010-01-08 12:56:19 -05001000
Alan Stern62e299e2010-01-08 12:56:19 -05001001 mutex_unlock(&usbfs_mutex);
1002
1003 if (!dev)
1004 goto out_free_ps;
1005
1006 usb_lock_device(dev);
1007 if (dev->state == USB_STATE_NOTATTACHED)
1008 goto out_unlock_device;
1009
Alan Stern94fcda12006-11-20 11:38:46 -05001010 ret = usb_autoresume_device(dev);
Alan Stern01d883d2006-08-30 15:47:18 -04001011 if (ret)
Alan Stern62e299e2010-01-08 12:56:19 -05001012 goto out_unlock_device;
Alan Stern01d883d2006-08-30 15:47:18 -04001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 ps->dev = dev;
1015 ps->file = file;
1016 spin_lock_init(&ps->lock);
Dan Carpenter316547f2006-12-13 00:03:38 -08001017 INIT_LIST_HEAD(&ps->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 INIT_LIST_HEAD(&ps->async_pending);
1019 INIT_LIST_HEAD(&ps->async_completed);
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001020 INIT_LIST_HEAD(&ps->memory_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 init_waitqueue_head(&ps->wait);
1022 ps->discsignr = 0;
Eric W. Biederman2425c082006-10-02 02:17:28 -07001023 ps->disc_pid = get_pid(task_pid(current));
Serge Hallynd178bc32011-09-26 10:45:18 -05001024 ps->cred = get_current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 ps->disccontext = NULL;
1026 ps->ifclaimed = 0;
David Quigley7a019552006-06-30 01:55:48 -07001027 security_task_getsecid(current, &ps->secid);
Oliver Neukum527660a2007-04-20 20:50:48 +02001028 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 list_add_tail(&ps->list, &dev->filelist);
1030 file->private_data = ps;
Alan Stern62e299e2010-01-08 12:56:19 -05001031 usb_unlock_device(dev);
Alan Stern2da41d52008-10-06 11:24:26 -04001032 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
1033 current->comm);
Alan Stern62e299e2010-01-08 12:56:19 -05001034 return ret;
1035
1036 out_unlock_device:
1037 usb_unlock_device(dev);
1038 usb_put_dev(dev);
1039 out_free_ps:
1040 kfree(ps);
Alan Stern4a2a8a22006-07-01 22:05:01 -04001041 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044static int usbdev_release(struct inode *inode, struct file *file)
1045{
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001046 struct usb_dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 struct usb_device *dev = ps->dev;
1048 unsigned int ifnum;
Alan Stern6ff10462009-03-09 13:44:02 -04001049 struct async *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 usb_lock_device(dev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001052 usb_hub_release_all_ports(dev, ps);
Alan Stern4a2a8a22006-07-01 22:05:01 -04001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 list_del_init(&ps->list);
Alan Stern4a2a8a22006-07-01 22:05:01 -04001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
1057 ifnum++) {
1058 if (test_bit(ifnum, &ps->ifclaimed))
1059 releaseintf(ps, ifnum);
1060 }
1061 destroy_all_async(ps);
Alan Stern94fcda12006-11-20 11:38:46 -05001062 usb_autosuspend_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 usb_unlock_device(dev);
1064 usb_put_dev(dev);
Eric W. Biederman2425c082006-10-02 02:17:28 -07001065 put_pid(ps->disc_pid);
Serge Hallynd178bc32011-09-26 10:45:18 -05001066 put_cred(ps->cred);
Alan Stern6ff10462009-03-09 13:44:02 -04001067
1068 as = async_getcompleted(ps);
1069 while (as) {
1070 free_async(as);
1071 as = async_getcompleted(ps);
1072 }
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 kfree(ps);
Alan Stern4a2a8a22006-07-01 22:05:01 -04001075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001078static int proc_control(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080 struct usb_device *dev = ps->dev;
1081 struct usbdevfs_ctrltransfer ctrl;
1082 unsigned int tmo;
1083 unsigned char *tbuf;
Andrew Mortonff66e3c2008-03-12 13:32:24 -07001084 unsigned wLength;
Alan Stern4c6e8972009-06-29 11:02:04 -04001085 int i, pipe, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1088 return -EFAULT;
Matthias Dellweg393cbb52011-09-25 14:26:25 +02001089 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.bRequest,
1090 ctrl.wIndex);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001091 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return ret;
Andrew Mortonff66e3c2008-03-12 13:32:24 -07001093 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
1094 if (wLength > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return -EINVAL;
Alan Sternadd1aae2011-11-17 16:41:25 -05001096 ret = usbfs_increase_memory_usage(PAGE_SIZE + sizeof(struct urb) +
1097 sizeof(struct usb_ctrlrequest));
1098 if (ret)
1099 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001100 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
Alan Sternadd1aae2011-11-17 16:41:25 -05001101 if (!tbuf) {
1102 ret = -ENOMEM;
1103 goto done;
1104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 tmo = ctrl.timeout;
Chris Frey0880aef2010-01-26 17:07:29 -05001106 snoop(&dev->dev, "control urb: bRequestType=%02x "
1107 "bRequest=%02x wValue=%04x "
1108 "wIndex=%04x wLength=%04x\n",
Xenia Ragiadakouc8f2efc2013-08-31 18:09:14 +03001109 ctrl.bRequestType, ctrl.bRequest, ctrl.wValue,
1110 ctrl.wIndex, ctrl.wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (ctrl.bRequestType & 0x80) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001112 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
1113 ctrl.wLength)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001114 ret = -EINVAL;
1115 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
Alan Stern4c6e8972009-06-29 11:02:04 -04001117 pipe = usb_rcvctrlpipe(dev, 0);
Chris Frey0880aef2010-01-26 17:07:29 -05001118 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 usb_unlock_device(dev);
Alan Stern4c6e8972009-06-29 11:02:04 -04001121 i = usb_control_msg(dev, pipe, ctrl.bRequest,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001122 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
1123 tbuf, ctrl.wLength, tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 usb_lock_device(dev);
Chris Frey0880aef2010-01-26 17:07:29 -05001125 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE,
Michal Sojka9d02b422011-03-15 16:41:47 +01001126 tbuf, max(i, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if ((i > 0) && ctrl.wLength) {
Alan Sternfe0410c2005-07-29 12:16:58 -07001128 if (copy_to_user(ctrl.data, tbuf, i)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001129 ret = -EFAULT;
1130 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132 }
1133 } else {
1134 if (ctrl.wLength) {
1135 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001136 ret = -EFAULT;
1137 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139 }
Alan Stern4c6e8972009-06-29 11:02:04 -04001140 pipe = usb_sndctrlpipe(dev, 0);
Chris Frey0880aef2010-01-26 17:07:29 -05001141 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT,
1142 tbuf, ctrl.wLength);
Alan Stern4c6e8972009-06-29 11:02:04 -04001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 usb_unlock_device(dev);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001145 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
1146 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
1147 tbuf, ctrl.wLength, tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 usb_lock_device(dev);
Chris Frey0880aef2010-01-26 17:07:29 -05001149 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001151 if (i < 0 && i != -EPIPE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
1153 "failed cmd %s rqt %u rq %u len %u ret %d\n",
1154 current->comm, ctrl.bRequestType, ctrl.bRequest,
1155 ctrl.wLength, i);
1156 }
Alan Stern52fb7432011-11-17 16:41:14 -05001157 ret = i;
1158 done:
1159 free_page((unsigned long) tbuf);
Alan Sternadd1aae2011-11-17 16:41:25 -05001160 usbfs_decrease_memory_usage(PAGE_SIZE + sizeof(struct urb) +
1161 sizeof(struct usb_ctrlrequest));
Alan Stern52fb7432011-11-17 16:41:14 -05001162 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001165static int proc_bulk(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 struct usb_device *dev = ps->dev;
1168 struct usbdevfs_bulktransfer bulk;
1169 unsigned int tmo, len1, pipe;
1170 int len2;
1171 unsigned char *tbuf;
Alan Stern4c6e8972009-06-29 11:02:04 -04001172 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 if (copy_from_user(&bulk, arg, sizeof(bulk)))
1175 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001176 ret = findintfep(ps->dev, bulk.ep);
1177 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001179 ret = checkintf(ps, ret);
1180 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return ret;
1182 if (bulk.ep & USB_DIR_IN)
1183 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
1184 else
1185 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
1186 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
1187 return -EINVAL;
1188 len1 = bulk.len;
Alan Stern3f5eb8d2011-11-17 16:41:35 -05001189 if (len1 >= USBFS_XFER_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return -EINVAL;
Alan Sternadd1aae2011-11-17 16:41:25 -05001191 ret = usbfs_increase_memory_usage(len1 + sizeof(struct urb));
1192 if (ret)
1193 return ret;
Kris Borer135551e2015-08-04 08:39:31 -04001194 tbuf = kmalloc(len1, GFP_KERNEL);
1195 if (!tbuf) {
Alan Sternadd1aae2011-11-17 16:41:25 -05001196 ret = -ENOMEM;
1197 goto done;
1198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 tmo = bulk.timeout;
1200 if (bulk.ep & 0x80) {
1201 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001202 ret = -EINVAL;
1203 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
Chris Frey0880aef2010-01-26 17:07:29 -05001205 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, NULL, 0);
Alan Stern4c6e8972009-06-29 11:02:04 -04001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 usb_unlock_device(dev);
1208 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
1209 usb_lock_device(dev);
Chris Frey0880aef2010-01-26 17:07:29 -05001210 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, tbuf, len2);
Alan Stern4c6e8972009-06-29 11:02:04 -04001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (!i && len2) {
1213 if (copy_to_user(bulk.data, tbuf, len2)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001214 ret = -EFAULT;
1215 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217 }
1218 } else {
1219 if (len1) {
1220 if (copy_from_user(tbuf, bulk.data, len1)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001221 ret = -EFAULT;
1222 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224 }
Chris Frey0880aef2010-01-26 17:07:29 -05001225 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, tbuf, len1);
Alan Stern4c6e8972009-06-29 11:02:04 -04001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 usb_unlock_device(dev);
1228 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
1229 usb_lock_device(dev);
Chris Frey0880aef2010-01-26 17:07:29 -05001230 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
Alan Stern52fb7432011-11-17 16:41:14 -05001232 ret = (i < 0 ? i : len2);
1233 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 kfree(tbuf);
Alan Sternadd1aae2011-11-17 16:41:25 -05001235 usbfs_decrease_memory_usage(len1 + sizeof(struct urb));
Alan Stern52fb7432011-11-17 16:41:14 -05001236 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
Alan Sternf080a512014-02-20 10:49:30 -05001239static void check_reset_of_active_ep(struct usb_device *udev,
1240 unsigned int epnum, char *ioctl_name)
1241{
1242 struct usb_host_endpoint **eps;
1243 struct usb_host_endpoint *ep;
1244
1245 eps = (epnum & USB_DIR_IN) ? udev->ep_in : udev->ep_out;
1246 ep = eps[epnum & 0x0f];
1247 if (ep && !list_empty(&ep->urb_list))
1248 dev_warn(&udev->dev, "Process %d (%s) called USBDEVFS_%s for active endpoint 0x%02x\n",
1249 task_pid_nr(current), current->comm,
1250 ioctl_name, epnum);
1251}
1252
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001253static int proc_resetep(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
1255 unsigned int ep;
1256 int ret;
1257
1258 if (get_user(ep, (unsigned int __user *)arg))
1259 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001260 ret = findintfep(ps->dev, ep);
1261 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001263 ret = checkintf(ps, ret);
1264 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return ret;
Alan Sternf080a512014-02-20 10:49:30 -05001266 check_reset_of_active_ep(ps->dev, ep, "RESETEP");
David Vrabel3444b262009-04-08 17:36:28 +00001267 usb_reset_endpoint(ps->dev, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return 0;
1269}
1270
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001271static int proc_clearhalt(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 unsigned int ep;
1274 int pipe;
1275 int ret;
1276
1277 if (get_user(ep, (unsigned int __user *)arg))
1278 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001279 ret = findintfep(ps->dev, ep);
1280 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001282 ret = checkintf(ps, ret);
1283 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return ret;
Alan Sternf080a512014-02-20 10:49:30 -05001285 check_reset_of_active_ep(ps->dev, ep, "CLEAR_HALT");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (ep & USB_DIR_IN)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001287 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
1288 else
1289 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 return usb_clear_halt(ps->dev, pipe);
1292}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001294static int proc_getdriver(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 struct usbdevfs_getdriver gd;
1297 struct usb_interface *intf;
1298 int ret;
1299
1300 if (copy_from_user(&gd, arg, sizeof(gd)))
1301 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 intf = usb_ifnum_to_if(ps->dev, gd.interface);
1303 if (!intf || !intf->dev.driver)
1304 ret = -ENODATA;
1305 else {
Chen Gangb11b2e12013-02-02 15:57:53 +08001306 strlcpy(gd.driver, intf->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 sizeof(gd.driver));
1308 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
1309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return ret;
1311}
1312
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001313static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Vasiliy Kulikov886ccd42010-11-06 17:41:28 +03001315 struct usbdevfs_connectinfo ci = {
1316 .devnum = ps->dev->devnum,
1317 .slow = ps->dev->speed == USB_SPEED_LOW
1318 };
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{
Ming Lei742120c2008-06-18 22:00:29 +08001327 return usb_reset_device(ps->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001330static int proc_setintf(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
1332 struct usbdevfs_setinterface setintf;
1333 int ret;
1334
1335 if (copy_from_user(&setintf, arg, sizeof(setintf)))
1336 return -EFAULT;
Kris Borer135551e2015-08-04 08:39:31 -04001337 ret = checkintf(ps, setintf.interface);
1338 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return ret;
Hans de Goede5ec9c172013-10-09 17:19:27 +02001340
1341 destroy_async_on_interface(ps, setintf.interface);
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return usb_set_interface(ps->dev, setintf.interface,
1344 setintf.altsetting);
1345}
1346
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001347static int proc_setconfig(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Alan Stern3f141e22007-02-08 16:40:43 -05001349 int u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 int status = 0;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001351 struct usb_host_config *actconfig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Alan Stern3f141e22007-02-08 16:40:43 -05001353 if (get_user(u, (int __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return -EFAULT;
1355
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001356 actconfig = ps->dev->actconfig;
1357
1358 /* Don't touch the device if any interfaces are claimed.
1359 * It could interfere with other drivers' operations, and if
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 * an interface is claimed by usbfs it could easily deadlock.
1361 */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001362 if (actconfig) {
1363 int i;
1364
1365 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
1366 if (usb_interface_claimed(actconfig->interface[i])) {
1367 dev_warn(&ps->dev->dev,
David Brownell72ebddb2005-04-11 18:34:17 -07001368 "usbfs: interface %d claimed by %s "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 "while '%s' sets config #%d\n",
1370 actconfig->interface[i]
1371 ->cur_altsetting
1372 ->desc.bInterfaceNumber,
David Brownell72ebddb2005-04-11 18:34:17 -07001373 actconfig->interface[i]
1374 ->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 current->comm, u);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001376 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001379 }
1380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
1383 * so avoid usb_set_configuration()'s kick to sysfs
1384 */
1385 if (status == 0) {
1386 if (actconfig && actconfig->desc.bConfigurationValue == u)
1387 status = usb_reset_configuration(ps->dev);
1388 else
1389 status = usb_set_configuration(ps->dev, u);
1390 }
1391
1392 return status;
1393}
1394
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001395static struct usb_memory *
1396find_memory_area(struct usb_dev_state *ps, const struct usbdevfs_urb *uurb)
1397{
1398 struct usb_memory *usbm = NULL, *iter;
1399 unsigned long flags;
1400 unsigned long uurb_start = (unsigned long)uurb->buffer;
1401
1402 spin_lock_irqsave(&ps->lock, flags);
1403 list_for_each_entry(iter, &ps->memory_list, memlist) {
1404 if (uurb_start >= iter->vm_start &&
1405 uurb_start < iter->vm_start + iter->size) {
1406 if (uurb->buffer_length > iter->vm_start + iter->size -
1407 uurb_start) {
1408 usbm = ERR_PTR(-EINVAL);
1409 } else {
1410 usbm = iter;
1411 usbm->urb_use_count++;
1412 }
1413 break;
1414 }
1415 }
1416 spin_unlock_irqrestore(&ps->lock, flags);
1417 return usbm;
1418}
1419
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001420static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001421 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
1422 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424 struct usbdevfs_iso_packet_desc *isopkt = NULL;
1425 struct usb_host_endpoint *ep;
Alan Stern52fb7432011-11-17 16:41:14 -05001426 struct async *as = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 struct usb_ctrlrequest *dr = NULL;
1428 unsigned int u, totlen, isofrmlen;
Hans de Goede3d97ff62012-07-04 09:18:03 +02001429 int i, ret, is_in, num_sgs = 0, ifnum = -1;
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001430 int number_of_packets = 0;
Hans de Goede948cd8c2013-10-09 17:19:29 +02001431 unsigned int stream_id = 0;
Hans de Goede3d97ff62012-07-04 09:18:03 +02001432 void *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Alan Stern14722ef2008-04-17 10:18:11 -04001434 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
1435 USBDEVFS_URB_SHORT_NOT_OK |
Alan Stern01c64602009-09-01 11:09:56 -04001436 USBDEVFS_URB_BULK_CONTINUATION |
Alan Stern14722ef2008-04-17 10:18:11 -04001437 USBDEVFS_URB_NO_FSBR |
1438 USBDEVFS_URB_ZERO_PACKET |
1439 USBDEVFS_URB_NO_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return -EINVAL;
Alan Stern91801352009-06-29 11:04:54 -04001441 if (uurb->buffer_length > 0 && !uurb->buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001443 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
1444 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
1445 ifnum = findintfep(ps->dev, uurb->endpoint);
1446 if (ifnum < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return ifnum;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001448 ret = checkintf(ps, ifnum);
1449 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return ret;
1451 }
Hans de Goede2fec32b2013-10-09 17:19:30 +02001452 ep = ep_to_host_endpoint(ps->dev, uurb->endpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (!ep)
1454 return -ENOENT;
Hans de Goede2fec32b2013-10-09 17:19:30 +02001455 is_in = (uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0;
Alan Sternadd1aae2011-11-17 16:41:25 -05001456
1457 u = 0;
Kris Borerf355e832015-08-07 07:22:44 -04001458 switch (uurb->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 case USBDEVFS_URB_TYPE_CONTROL:
Alan Stern93cf9b92007-07-30 17:09:28 -04001460 if (!usb_endpoint_xfer_control(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return -EINVAL;
Alan Sternadd1aae2011-11-17 16:41:25 -05001462 /* min 8 byte setup packet */
1463 if (uurb->buffer_length < 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001465 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1466 if (!dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return -ENOMEM;
1468 if (copy_from_user(dr, uurb->buffer, 8)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001469 ret = -EFAULT;
1470 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001473 ret = -EINVAL;
1474 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
Matthias Dellweg393cbb52011-09-25 14:26:25 +02001476 ret = check_ctrlrecip(ps, dr->bRequestType, dr->bRequest,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001477 le16_to_cpup(&dr->wIndex));
Alan Stern52fb7432011-11-17 16:41:14 -05001478 if (ret)
1479 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1481 uurb->buffer += 8;
Alan Stern93cf9b92007-07-30 17:09:28 -04001482 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1483 is_in = 1;
1484 uurb->endpoint |= USB_DIR_IN;
1485 } else {
1486 is_in = 0;
1487 uurb->endpoint &= ~USB_DIR_IN;
1488 }
Chris Frey0880aef2010-01-26 17:07:29 -05001489 snoop(&ps->dev->dev, "control urb: bRequestType=%02x "
1490 "bRequest=%02x wValue=%04x "
1491 "wIndex=%04x wLength=%04x\n",
1492 dr->bRequestType, dr->bRequest,
1493 __le16_to_cpup(&dr->wValue),
1494 __le16_to_cpup(&dr->wIndex),
1495 __le16_to_cpup(&dr->wLength));
Alan Sternadd1aae2011-11-17 16:41:25 -05001496 u = sizeof(struct usb_ctrlrequest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 break;
1498
1499 case USBDEVFS_URB_TYPE_BULK:
Alan Stern93cf9b92007-07-30 17:09:28 -04001500 switch (usb_endpoint_type(&ep->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 case USB_ENDPOINT_XFER_CONTROL:
1502 case USB_ENDPOINT_XFER_ISOC:
1503 return -EINVAL;
Alan Sternf661c6f2009-12-11 16:20:20 -05001504 case USB_ENDPOINT_XFER_INT:
1505 /* allow single-shot interrupt transfers */
1506 uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
1507 goto interrupt_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
Hans de Goede3d97ff62012-07-04 09:18:03 +02001509 num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE);
1510 if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize)
1511 num_sgs = 0;
Hans de Goede948cd8c2013-10-09 17:19:29 +02001512 if (ep->streams)
1513 stream_id = uurb->stream_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 break;
1515
Alan Sternf661c6f2009-12-11 16:20:20 -05001516 case USBDEVFS_URB_TYPE_INTERRUPT:
1517 if (!usb_endpoint_xfer_int(&ep->desc))
1518 return -EINVAL;
1519 interrupt_urb:
Alan Sternf661c6f2009-12-11 16:20:20 -05001520 break;
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 case USBDEVFS_URB_TYPE_ISO:
1523 /* arbitrary limit */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001524 if (uurb->number_of_packets < 1 ||
1525 uurb->number_of_packets > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return -EINVAL;
Alan Stern93cf9b92007-07-30 17:09:28 -04001527 if (!usb_endpoint_xfer_isoc(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return -EINVAL;
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001529 number_of_packets = uurb->number_of_packets;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001530 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001531 number_of_packets;
Rahul Pathak73a02d32015-12-11 05:40:51 +00001532 isopkt = memdup_user(iso_frame_desc, isofrmlen);
1533 if (IS_ERR(isopkt)) {
1534 ret = PTR_ERR(isopkt);
1535 isopkt = NULL;
Alan Stern52fb7432011-11-17 16:41:14 -05001536 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001538 for (totlen = u = 0; u < number_of_packets; u++) {
Federico Manzane2e2f0e2013-05-24 18:18:48 +02001539 /*
1540 * arbitrary limit need for USB 3.0
1541 * bMaxBurst (0~15 allowed, 1~16 packets)
1542 * bmAttributes (bit 1:0, mult 0~2, 1~3 packets)
1543 * sizemax: 1024 * 16 * 3 = 49152
1544 */
1545 if (isopkt[u].length > 49152) {
Alan Stern52fb7432011-11-17 16:41:14 -05001546 ret = -EINVAL;
1547 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549 totlen += isopkt[u].length;
1550 }
Alan Sternadd1aae2011-11-17 16:41:25 -05001551 u *= sizeof(struct usb_iso_packet_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 uurb->buffer_length = totlen;
1553 break;
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 default:
1556 return -EINVAL;
1557 }
Alan Sternadd1aae2011-11-17 16:41:25 -05001558
Alan Stern3f5eb8d2011-11-17 16:41:35 -05001559 if (uurb->buffer_length >= USBFS_XFER_MAX) {
Alan Sternadd1aae2011-11-17 16:41:25 -05001560 ret = -EINVAL;
1561 goto error;
1562 }
Alan Stern91801352009-06-29 11:04:54 -04001563 if (uurb->buffer_length > 0 &&
1564 !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1565 uurb->buffer, uurb->buffer_length)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001566 ret = -EFAULT;
1567 goto error;
Alan Stern91801352009-06-29 11:04:54 -04001568 }
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001569 as = alloc_async(number_of_packets);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001570 if (!as) {
Alan Stern52fb7432011-11-17 16:41:14 -05001571 ret = -ENOMEM;
1572 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
Hans de Goede3d97ff62012-07-04 09:18:03 +02001574
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001575 as->usbm = find_memory_area(ps, uurb);
1576 if (IS_ERR(as->usbm)) {
1577 ret = PTR_ERR(as->usbm);
1578 as->usbm = NULL;
1579 goto error;
1580 }
1581
1582 /* do not use SG buffers when memory mapped segments
1583 * are in use
1584 */
1585 if (as->usbm)
1586 num_sgs = 0;
1587
Hans de Goede3d97ff62012-07-04 09:18:03 +02001588 u += sizeof(struct async) + sizeof(struct urb) + uurb->buffer_length +
1589 num_sgs * sizeof(struct scatterlist);
Alan Sternadd1aae2011-11-17 16:41:25 -05001590 ret = usbfs_increase_memory_usage(u);
1591 if (ret)
1592 goto error;
1593 as->mem_usage = u;
1594
Hans de Goede3d97ff62012-07-04 09:18:03 +02001595 if (num_sgs) {
1596 as->urb->sg = kmalloc(num_sgs * sizeof(struct scatterlist),
1597 GFP_KERNEL);
1598 if (!as->urb->sg) {
1599 ret = -ENOMEM;
1600 goto error;
1601 }
1602 as->urb->num_sgs = num_sgs;
1603 sg_init_table(as->urb->sg, as->urb->num_sgs);
1604
1605 totlen = uurb->buffer_length;
1606 for (i = 0; i < as->urb->num_sgs; i++) {
1607 u = (totlen > USB_SG_SIZE) ? USB_SG_SIZE : totlen;
1608 buf = kmalloc(u, GFP_KERNEL);
1609 if (!buf) {
1610 ret = -ENOMEM;
1611 goto error;
1612 }
1613 sg_set_buf(&as->urb->sg[i], buf, u);
1614
1615 if (!is_in) {
1616 if (copy_from_user(buf, uurb->buffer, u)) {
1617 ret = -EFAULT;
1618 goto error;
1619 }
Henrik Rydberg01463902012-10-13 12:20:36 +02001620 uurb->buffer += u;
Hans de Goede3d97ff62012-07-04 09:18:03 +02001621 }
1622 totlen -= u;
1623 }
1624 } else if (uurb->buffer_length > 0) {
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001625 if (as->usbm) {
1626 unsigned long uurb_start = (unsigned long)uurb->buffer;
Hans de Goede3d97ff62012-07-04 09:18:03 +02001627
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001628 as->urb->transfer_buffer = as->usbm->mem +
1629 (uurb_start - as->usbm->vm_start);
1630 } else {
1631 as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
1632 GFP_KERNEL);
1633 if (!as->urb->transfer_buffer) {
1634 ret = -ENOMEM;
Hans de Goede3d97ff62012-07-04 09:18:03 +02001635 goto error;
1636 }
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001637 if (!is_in) {
1638 if (copy_from_user(as->urb->transfer_buffer,
1639 uurb->buffer,
1640 uurb->buffer_length)) {
1641 ret = -EFAULT;
1642 goto error;
1643 }
1644 } else if (uurb->type == USBDEVFS_URB_TYPE_ISO) {
1645 /*
1646 * Isochronous input data may end up being
1647 * discontiguous if some of the packets are
1648 * short. Clear the buffer so that the gaps
1649 * don't leak kernel data to userspace.
1650 */
1651 memset(as->urb->transfer_buffer, 0,
1652 uurb->buffer_length);
1653 }
Hans de Goede3d97ff62012-07-04 09:18:03 +02001654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001656 as->urb->dev = ps->dev;
1657 as->urb->pipe = (uurb->type << 30) |
Alan Stern93cf9b92007-07-30 17:09:28 -04001658 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1659 (uurb->endpoint & USB_DIR_IN);
Alan Stern14722ef2008-04-17 10:18:11 -04001660
1661 /* This tedious sequence is necessary because the URB_* flags
1662 * are internal to the kernel and subject to change, whereas
1663 * the USBDEVFS_URB_* flags are a user API and must not be changed.
1664 */
1665 u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
1666 if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
1667 u |= URB_ISO_ASAP;
Oliver Neukumd310d052014-08-01 09:55:20 +02001668 if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK && is_in)
Alan Stern14722ef2008-04-17 10:18:11 -04001669 u |= URB_SHORT_NOT_OK;
1670 if (uurb->flags & USBDEVFS_URB_NO_FSBR)
1671 u |= URB_NO_FSBR;
1672 if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1673 u |= URB_ZERO_PACKET;
1674 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
1675 u |= URB_NO_INTERRUPT;
1676 as->urb->transfer_flags = u;
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 as->urb->transfer_buffer_length = uurb->buffer_length;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001679 as->urb->setup_packet = (unsigned char *)dr;
Alan Stern52fb7432011-11-17 16:41:14 -05001680 dr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 as->urb->start_frame = uurb->start_frame;
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001682 as->urb->number_of_packets = number_of_packets;
Hans de Goede948cd8c2013-10-09 17:19:29 +02001683 as->urb->stream_id = stream_id;
Alan Stern97b9eb92007-02-26 14:56:14 -05001684 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1685 ps->dev->speed == USB_SPEED_HIGH)
1686 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
1687 else
1688 as->urb->interval = ep->desc.bInterval;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001689 as->urb->context = as;
1690 as->urb->complete = async_completed;
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001691 for (totlen = u = 0; u < number_of_packets; u++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 as->urb->iso_frame_desc[u].offset = totlen;
1693 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1694 totlen += isopkt[u].length;
1695 }
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001696 kfree(isopkt);
Alan Stern52fb7432011-11-17 16:41:14 -05001697 isopkt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 as->ps = ps;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001699 as->userurb = arg;
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001700 if (as->usbm) {
1701 unsigned long uurb_start = (unsigned long)uurb->buffer;
1702
1703 as->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1704 as->urb->transfer_dma = as->usbm->dma_handle +
1705 (uurb_start - as->usbm->vm_start);
1706 } else if (is_in && uurb->buffer_length > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 as->userbuffer = uurb->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 as->signr = uurb->signr;
1709 as->ifnum = ifnum;
Eric W. Biederman2425c082006-10-02 02:17:28 -07001710 as->pid = get_pid(task_pid(current));
Serge Hallynd178bc32011-09-26 10:45:18 -05001711 as->cred = get_current_cred();
David Quigley7a019552006-06-30 01:55:48 -07001712 security_task_getsecid(current, &as->secid);
Alan Stern4c6e8972009-06-29 11:02:04 -04001713 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
Chris Frey0880aef2010-01-26 17:07:29 -05001714 as->urb->transfer_buffer_length, 0, SUBMIT,
Hans de Goede3d97ff62012-07-04 09:18:03 +02001715 NULL, 0);
1716 if (!is_in)
1717 snoop_urb_data(as->urb, as->urb->transfer_buffer_length);
1718
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001719 async_newpending(as);
Alan Stern01c64602009-09-01 11:09:56 -04001720
1721 if (usb_endpoint_xfer_bulk(&ep->desc)) {
1722 spin_lock_irq(&ps->lock);
1723
1724 /* Not exactly the endpoint address; the direction bit is
1725 * shifted to the 0x10 position so that the value will be
1726 * between 0 and 31.
1727 */
1728 as->bulk_addr = usb_endpoint_num(&ep->desc) |
1729 ((ep->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1730 >> 3);
1731
1732 /* If this bulk URB is the start of a new transfer, re-enable
1733 * the endpoint. Otherwise mark it as a continuation URB.
1734 */
1735 if (uurb->flags & USBDEVFS_URB_BULK_CONTINUATION)
1736 as->bulk_status = AS_CONTINUATION;
1737 else
1738 ps->disabled_bulk_eps &= ~(1 << as->bulk_addr);
1739
1740 /* Don't accept continuation URBs if the endpoint is
1741 * disabled because of an earlier error.
1742 */
1743 if (ps->disabled_bulk_eps & (1 << as->bulk_addr))
1744 ret = -EREMOTEIO;
1745 else
1746 ret = usb_submit_urb(as->urb, GFP_ATOMIC);
1747 spin_unlock_irq(&ps->lock);
1748 } else {
1749 ret = usb_submit_urb(as->urb, GFP_KERNEL);
1750 }
1751
1752 if (ret) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001753 dev_printk(KERN_DEBUG, &ps->dev->dev,
1754 "usbfs: usb_submit_urb returned %d\n", ret);
Alan Stern4c6e8972009-06-29 11:02:04 -04001755 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
Chris Frey0880aef2010-01-26 17:07:29 -05001756 0, ret, COMPLETE, NULL, 0);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001757 async_removepending(as);
Alan Stern52fb7432011-11-17 16:41:14 -05001758 goto error;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001759 }
1760 return 0;
Alan Stern52fb7432011-11-17 16:41:14 -05001761
1762 error:
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001763 if (as && as->usbm)
1764 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count);
Alan Stern52fb7432011-11-17 16:41:14 -05001765 kfree(isopkt);
1766 kfree(dr);
1767 if (as)
1768 free_async(as);
1769 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001772static int proc_submiturb(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
1774 struct usbdevfs_urb uurb;
1775
1776 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1777 return -EFAULT;
1778
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001779 return proc_do_submiturb(ps, &uurb,
1780 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1781 arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001784static int proc_unlinkurb(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Huajun Li4e09dcf2012-05-18 20:12:51 +08001786 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 struct async *as;
Huajun Li4e09dcf2012-05-18 20:12:51 +08001788 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Huajun Li4e09dcf2012-05-18 20:12:51 +08001790 spin_lock_irqsave(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 as = async_getpending(ps, arg);
Huajun Li4e09dcf2012-05-18 20:12:51 +08001792 if (!as) {
1793 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return -EINVAL;
Huajun Li4e09dcf2012-05-18 20:12:51 +08001795 }
1796
1797 urb = as->urb;
1798 usb_get_urb(urb);
1799 spin_unlock_irqrestore(&ps->lock, flags);
1800
1801 usb_kill_urb(urb);
1802 usb_put_urb(urb);
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return 0;
1805}
1806
1807static int processcompl(struct async *as, void __user * __user *arg)
1808{
1809 struct urb *urb = as->urb;
1810 struct usbdevfs_urb __user *userurb = as->userurb;
1811 void __user *addr = as->userurb;
1812 unsigned int i;
1813
Alan Stern7152b592010-03-06 15:04:03 -05001814 if (as->userbuffer && urb->actual_length) {
Hans de Goede3d97ff62012-07-04 09:18:03 +02001815 if (copy_urb_data_to_user(as->userbuffer, urb))
Oliver Neukumd794a022009-06-28 23:34:14 +02001816 goto err_out;
Alan Stern7152b592010-03-06 15:04:03 -05001817 }
Alan Sterne0152682007-08-24 15:42:52 -04001818 if (put_user(as->status, &userurb->status))
Oliver Neukumd794a022009-06-28 23:34:14 +02001819 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (put_user(urb->actual_length, &userurb->actual_length))
Oliver Neukumd794a022009-06-28 23:34:14 +02001821 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (put_user(urb->error_count, &userurb->error_count))
Oliver Neukumd794a022009-06-28 23:34:14 +02001823 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Alan Stern93cf9b92007-07-30 17:09:28 -04001825 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
Christopher Li668a9542005-04-18 17:39:26 -07001826 for (i = 0; i < urb->number_of_packets; i++) {
1827 if (put_user(urb->iso_frame_desc[i].actual_length,
1828 &userurb->iso_frame_desc[i].actual_length))
Oliver Neukumd794a022009-06-28 23:34:14 +02001829 goto err_out;
Christopher Li668a9542005-04-18 17:39:26 -07001830 if (put_user(urb->iso_frame_desc[i].status,
1831 &userurb->iso_frame_desc[i].status))
Oliver Neukumd794a022009-06-28 23:34:14 +02001832 goto err_out;
Christopher Li668a9542005-04-18 17:39:26 -07001833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (put_user(addr, (void __user * __user *)arg))
1837 return -EFAULT;
1838 return 0;
Oliver Neukumd794a022009-06-28 23:34:14 +02001839
1840err_out:
Oliver Neukumd794a022009-06-28 23:34:14 +02001841 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001844static struct async *reap_as(struct usb_dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001846 DECLARE_WAITQUEUE(wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 struct async *as = NULL;
1848 struct usb_device *dev = ps->dev;
1849
1850 add_wait_queue(&ps->wait, &wait);
1851 for (;;) {
1852 __set_current_state(TASK_INTERRUPTIBLE);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001853 as = async_getcompleted(ps);
Alan Stern3f2cee72015-01-29 11:29:13 -05001854 if (as || !connected(ps))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 break;
1856 if (signal_pending(current))
1857 break;
1858 usb_unlock_device(dev);
1859 schedule();
1860 usb_lock_device(dev);
1861 }
1862 remove_wait_queue(&ps->wait, &wait);
1863 set_current_state(TASK_RUNNING);
1864 return as;
1865}
1866
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001867static int proc_reapurb(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
1869 struct async *as = reap_as(ps);
Alan Sterna016a812015-11-20 13:53:35 -05001870
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001871 if (as) {
Alan Sterna016a812015-11-20 13:53:35 -05001872 int retval;
1873
1874 snoop(&ps->dev->dev, "reap %p\n", as->userurb);
1875 retval = processcompl(as, (void __user * __user *)arg);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001876 free_async(as);
1877 return retval;
1878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (signal_pending(current))
1880 return -EINTR;
Alan Stern3f2cee72015-01-29 11:29:13 -05001881 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001884static int proc_reapurbnonblock(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001886 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 struct async *as;
1888
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001889 as = async_getcompleted(ps);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001890 if (as) {
Alan Sterna016a812015-11-20 13:53:35 -05001891 snoop(&ps->dev->dev, "reap %p\n", as->userurb);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001892 retval = processcompl(as, (void __user * __user *)arg);
1893 free_async(as);
Alan Stern3f2cee72015-01-29 11:29:13 -05001894 } else {
1895 retval = (connected(ps) ? -EAGAIN : -ENODEV);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001896 }
1897 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898}
1899
1900#ifdef CONFIG_COMPAT
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001901static int proc_control_compat(struct usb_dev_state *ps,
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001902 struct usbdevfs_ctrltransfer32 __user *p32)
1903{
Matthias Beyer5b32c382013-10-14 21:46:36 +02001904 struct usbdevfs_ctrltransfer __user *p;
1905 __u32 udata;
1906 p = compat_alloc_user_space(sizeof(*p));
1907 if (copy_in_user(p, p32, (sizeof(*p32) - sizeof(compat_caddr_t))) ||
1908 get_user(udata, &p32->data) ||
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001909 put_user(compat_ptr(udata), &p->data))
1910 return -EFAULT;
Matthias Beyer5b32c382013-10-14 21:46:36 +02001911 return proc_control(ps, p);
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001912}
1913
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001914static int proc_bulk_compat(struct usb_dev_state *ps,
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001915 struct usbdevfs_bulktransfer32 __user *p32)
1916{
Matthias Beyer06793f22013-10-14 21:46:37 +02001917 struct usbdevfs_bulktransfer __user *p;
1918 compat_uint_t n;
1919 compat_caddr_t addr;
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001920
Matthias Beyer06793f22013-10-14 21:46:37 +02001921 p = compat_alloc_user_space(sizeof(*p));
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001922
Matthias Beyer06793f22013-10-14 21:46:37 +02001923 if (get_user(n, &p32->ep) || put_user(n, &p->ep) ||
1924 get_user(n, &p32->len) || put_user(n, &p->len) ||
1925 get_user(n, &p32->timeout) || put_user(n, &p->timeout) ||
1926 get_user(addr, &p32->data) || put_user(compat_ptr(addr), &p->data))
1927 return -EFAULT;
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001928
Matthias Beyer06793f22013-10-14 21:46:37 +02001929 return proc_bulk(ps, p);
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001930}
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001931static int proc_disconnectsignal_compat(struct usb_dev_state *ps, void __user *arg)
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001932{
1933 struct usbdevfs_disconnectsignal32 ds;
1934
1935 if (copy_from_user(&ds, arg, sizeof(ds)))
1936 return -EFAULT;
1937 ps->discsignr = ds.signr;
1938 ps->disccontext = compat_ptr(ds.context);
1939 return 0;
1940}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942static int get_urb32(struct usbdevfs_urb *kurb,
1943 struct usbdevfs_urb32 __user *uurb)
1944{
1945 __u32 uptr;
Michael Buesch18753eb2009-07-29 11:39:03 +02001946 if (!access_ok(VERIFY_READ, uurb, sizeof(*uurb)) ||
1947 __get_user(kurb->type, &uurb->type) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 __get_user(kurb->endpoint, &uurb->endpoint) ||
1949 __get_user(kurb->status, &uurb->status) ||
1950 __get_user(kurb->flags, &uurb->flags) ||
1951 __get_user(kurb->buffer_length, &uurb->buffer_length) ||
1952 __get_user(kurb->actual_length, &uurb->actual_length) ||
1953 __get_user(kurb->start_frame, &uurb->start_frame) ||
1954 __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
1955 __get_user(kurb->error_count, &uurb->error_count) ||
1956 __get_user(kurb->signr, &uurb->signr))
1957 return -EFAULT;
1958
1959 if (__get_user(uptr, &uurb->buffer))
1960 return -EFAULT;
1961 kurb->buffer = compat_ptr(uptr);
Mark Lorded0c7722009-01-02 02:48:24 -05001962 if (__get_user(uptr, &uurb->usercontext))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return -EFAULT;
1964 kurb->usercontext = compat_ptr(uptr);
1965
1966 return 0;
1967}
1968
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001969static int proc_submiturb_compat(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 struct usbdevfs_urb uurb;
1972
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001973 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 return -EFAULT;
1975
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001976 return proc_do_submiturb(ps, &uurb,
1977 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
1978 arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
1981static int processcompl_compat(struct async *as, void __user * __user *arg)
1982{
1983 struct urb *urb = as->urb;
1984 struct usbdevfs_urb32 __user *userurb = as->userurb;
1985 void __user *addr = as->userurb;
1986 unsigned int i;
1987
Hans de Goede2102e062012-07-04 09:18:01 +02001988 if (as->userbuffer && urb->actual_length) {
Hans de Goede3d97ff62012-07-04 09:18:03 +02001989 if (copy_urb_data_to_user(as->userbuffer, urb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return -EFAULT;
Hans de Goede2102e062012-07-04 09:18:01 +02001991 }
Alan Sterne0152682007-08-24 15:42:52 -04001992 if (put_user(as->status, &userurb->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return -EFAULT;
1994 if (put_user(urb->actual_length, &userurb->actual_length))
1995 return -EFAULT;
1996 if (put_user(urb->error_count, &userurb->error_count))
1997 return -EFAULT;
1998
Alan Stern93cf9b92007-07-30 17:09:28 -04001999 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
Christopher Li668a9542005-04-18 17:39:26 -07002000 for (i = 0; i < urb->number_of_packets; i++) {
2001 if (put_user(urb->iso_frame_desc[i].actual_length,
2002 &userurb->iso_frame_desc[i].actual_length))
2003 return -EFAULT;
2004 if (put_user(urb->iso_frame_desc[i].status,
2005 &userurb->iso_frame_desc[i].status))
2006 return -EFAULT;
2007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
2009
Al Viroc714de52006-10-10 22:45:37 +01002010 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 return -EFAULT;
2012 return 0;
2013}
2014
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002015static int proc_reapurb_compat(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
2017 struct async *as = reap_as(ps);
Alan Sterna016a812015-11-20 13:53:35 -05002018
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08002019 if (as) {
Alan Sterna016a812015-11-20 13:53:35 -05002020 int retval;
2021
2022 snoop(&ps->dev->dev, "reap %p\n", as->userurb);
2023 retval = processcompl_compat(as, (void __user * __user *)arg);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08002024 free_async(as);
2025 return retval;
2026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 if (signal_pending(current))
2028 return -EINTR;
Alan Stern3f2cee72015-01-29 11:29:13 -05002029 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002032static int proc_reapurbnonblock_compat(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08002034 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 struct async *as;
2036
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08002037 as = async_getcompleted(ps);
2038 if (as) {
Alan Sterna016a812015-11-20 13:53:35 -05002039 snoop(&ps->dev->dev, "reap %p\n", as->userurb);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08002040 retval = processcompl_compat(as, (void __user * __user *)arg);
2041 free_async(as);
Alan Stern3f2cee72015-01-29 11:29:13 -05002042 } else {
2043 retval = (connected(ps) ? -EAGAIN : -ENODEV);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08002044 }
2045 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049#endif
2050
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002051static int proc_disconnectsignal(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
2053 struct usbdevfs_disconnectsignal ds;
2054
2055 if (copy_from_user(&ds, arg, sizeof(ds)))
2056 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 ps->discsignr = ds.signr;
2058 ps->disccontext = ds.context;
2059 return 0;
2060}
2061
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002062static int proc_claiminterface(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
2064 unsigned int ifnum;
2065
2066 if (get_user(ifnum, (unsigned int __user *)arg))
2067 return -EFAULT;
2068 return claimintf(ps, ifnum);
2069}
2070
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002071static int proc_releaseinterface(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
2073 unsigned int ifnum;
2074 int ret;
2075
2076 if (get_user(ifnum, (unsigned int __user *)arg))
2077 return -EFAULT;
Kris Borer135551e2015-08-04 08:39:31 -04002078 ret = releaseintf(ps, ifnum);
2079 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return ret;
Chase Metzger64f10ed2015-12-22 20:19:45 -08002081 destroy_async_on_interface(ps, ifnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return 0;
2083}
2084
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002085static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 int size;
2088 void *buf = NULL;
2089 int retval = 0;
2090 struct usb_interface *intf = NULL;
2091 struct usb_driver *driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002093 /* alloc buffer */
Kris Borer135551e2015-08-04 08:39:31 -04002094 size = _IOC_SIZE(ctl->ioctl_code);
2095 if (size > 0) {
Tülin İzer4baf0df2013-05-17 10:13:58 +03002096 buf = kmalloc(size, GFP_KERNEL);
2097 if (buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 return -ENOMEM;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002099 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002100 if (copy_from_user(buf, ctl->data, size)) {
Jesper Juhl6fd19f42005-04-18 17:39:33 -07002101 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 return -EFAULT;
2103 }
2104 } else {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002105 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
2107 }
2108
Alan Stern349710c2006-07-01 22:05:56 -04002109 if (!connected(ps)) {
Jesper Juhl6fd19f42005-04-18 17:39:33 -07002110 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return -ENODEV;
2112 }
2113
2114 if (ps->dev->state != USB_STATE_CONFIGURED)
2115 retval = -EHOSTUNREACH;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002116 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
2117 retval = -EINVAL;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002118 else switch (ctl->ioctl_code) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 /* disconnect kernel driver from interface */
2121 case USBDEVFS_DISCONNECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (intf->dev.driver) {
2123 driver = to_usb_driver(intf->dev.driver);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002124 dev_dbg(&intf->dev, "disconnect by usbfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 usb_driver_release_interface(driver, intf);
2126 } else
2127 retval = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 break;
2129
2130 /* let kernel drivers try to (re)bind to the interface */
2131 case USBDEVFS_CONNECT:
Alan Stern885e9742007-12-03 15:42:10 -05002132 if (!intf->dev.driver)
2133 retval = device_attach(&intf->dev);
2134 else
2135 retval = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 break;
2137
2138 /* talk directly to the interface's driver */
2139 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (intf->dev.driver)
2141 driver = to_usb_driver(intf->dev.driver);
Andi Kleenc532b292010-06-01 23:04:41 +02002142 if (driver == NULL || driver->unlocked_ioctl == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 retval = -ENOTTY;
2144 } else {
Andi Kleenc532b292010-06-01 23:04:41 +02002145 retval = driver->unlocked_ioctl(intf, ctl->ioctl_code, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if (retval == -ENOIOCTLCMD)
2147 retval = -ENOTTY;
2148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
2150
2151 /* cleanup and return */
2152 if (retval >= 0
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002153 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 && size > 0
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002155 && copy_to_user(ctl->data, buf, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 retval = -EFAULT;
Jesper Juhl6fd19f42005-04-18 17:39:33 -07002157
2158 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 return retval;
2160}
2161
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002162static int proc_ioctl_default(struct usb_dev_state *ps, void __user *arg)
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002163{
2164 struct usbdevfs_ioctl ctrl;
2165
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002166 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002167 return -EFAULT;
2168 return proc_ioctl(ps, &ctrl);
2169}
2170
2171#ifdef CONFIG_COMPAT
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002172static int proc_ioctl_compat(struct usb_dev_state *ps, compat_uptr_t arg)
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002173{
2174 struct usbdevfs_ioctl32 __user *uioc;
2175 struct usbdevfs_ioctl ctrl;
2176 u32 udata;
2177
Andrew Morton058120d2005-11-17 09:47:49 -08002178 uioc = compat_ptr((long)arg);
Michael Buesch18753eb2009-07-29 11:39:03 +02002179 if (!access_ok(VERIFY_READ, uioc, sizeof(*uioc)) ||
2180 __get_user(ctrl.ifno, &uioc->ifno) ||
2181 __get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002182 __get_user(udata, &uioc->data))
2183 return -EFAULT;
2184 ctrl.data = compat_ptr(udata);
2185
2186 return proc_ioctl(ps, &ctrl);
2187}
2188#endif
2189
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002190static int proc_claim_port(struct usb_dev_state *ps, void __user *arg)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04002191{
2192 unsigned portnum;
2193 int rc;
2194
2195 if (get_user(portnum, (unsigned __user *) arg))
2196 return -EFAULT;
2197 rc = usb_hub_claim_port(ps->dev, portnum, ps);
2198 if (rc == 0)
2199 snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n",
2200 portnum, task_pid_nr(current), current->comm);
2201 return rc;
2202}
2203
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002204static int proc_release_port(struct usb_dev_state *ps, void __user *arg)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04002205{
2206 unsigned portnum;
2207
2208 if (get_user(portnum, (unsigned __user *) arg))
2209 return -EFAULT;
2210 return usb_hub_release_port(ps->dev, portnum, ps);
2211}
2212
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002213static int proc_get_capabilities(struct usb_dev_state *ps, void __user *arg)
Hans de Goede19181bc2012-07-04 09:18:02 +02002214{
2215 __u32 caps;
2216
Alan Stern3f2cee72015-01-29 11:29:13 -05002217 caps = USBDEVFS_CAP_ZERO_PACKET | USBDEVFS_CAP_NO_PACKET_SIZE_LIM |
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01002218 USBDEVFS_CAP_REAP_AFTER_DISCONNECT | USBDEVFS_CAP_MMAP;
Hans de Goede19181bc2012-07-04 09:18:02 +02002219 if (!ps->dev->bus->no_stop_on_short)
2220 caps |= USBDEVFS_CAP_BULK_CONTINUATION;
Hans de Goede3d97ff62012-07-04 09:18:03 +02002221 if (ps->dev->bus->sg_tablesize)
2222 caps |= USBDEVFS_CAP_BULK_SCATTER_GATHER;
Hans de Goede19181bc2012-07-04 09:18:02 +02002223
2224 if (put_user(caps, (__u32 __user *)arg))
2225 return -EFAULT;
2226
2227 return 0;
2228}
2229
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002230static int proc_disconnect_claim(struct usb_dev_state *ps, void __user *arg)
Hans de Goede0837e7e2012-09-08 20:02:05 +02002231{
2232 struct usbdevfs_disconnect_claim dc;
2233 struct usb_interface *intf;
2234
2235 if (copy_from_user(&dc, arg, sizeof(dc)))
2236 return -EFAULT;
2237
2238 intf = usb_ifnum_to_if(ps->dev, dc.interface);
2239 if (!intf)
2240 return -EINVAL;
2241
2242 if (intf->dev.driver) {
2243 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
2244
2245 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_IF_DRIVER) &&
2246 strncmp(dc.driver, intf->dev.driver->name,
2247 sizeof(dc.driver)) != 0)
2248 return -EBUSY;
2249
2250 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_EXCEPT_DRIVER) &&
2251 strncmp(dc.driver, intf->dev.driver->name,
2252 sizeof(dc.driver)) == 0)
2253 return -EBUSY;
2254
2255 dev_dbg(&intf->dev, "disconnect by usbfs\n");
2256 usb_driver_release_interface(driver, intf);
2257 }
2258
2259 return claimintf(ps, dc.interface);
2260}
2261
Linus Torvalds3e75c6d2014-04-01 17:06:09 -07002262static int proc_alloc_streams(struct usb_dev_state *ps, void __user *arg)
Hans de Goedebcf7f6e2013-10-09 17:19:31 +02002263{
2264 unsigned num_streams, num_eps;
2265 struct usb_host_endpoint **eps;
2266 struct usb_interface *intf;
2267 int r;
2268
2269 r = parse_usbdevfs_streams(ps, arg, &num_streams, &num_eps,
2270 &eps, &intf);
2271 if (r)
2272 return r;
2273
2274 destroy_async_on_interface(ps,
2275 intf->altsetting[0].desc.bInterfaceNumber);
2276
2277 r = usb_alloc_streams(intf, eps, num_eps, num_streams, GFP_KERNEL);
2278 kfree(eps);
2279 return r;
2280}
2281
Linus Torvalds3e75c6d2014-04-01 17:06:09 -07002282static int proc_free_streams(struct usb_dev_state *ps, void __user *arg)
Hans de Goedebcf7f6e2013-10-09 17:19:31 +02002283{
2284 unsigned num_eps;
2285 struct usb_host_endpoint **eps;
2286 struct usb_interface *intf;
2287 int r;
2288
2289 r = parse_usbdevfs_streams(ps, arg, NULL, &num_eps, &eps, &intf);
2290 if (r)
2291 return r;
2292
2293 destroy_async_on_interface(ps,
2294 intf->altsetting[0].desc.bInterfaceNumber);
2295
2296 r = usb_free_streams(intf, eps, num_eps, GFP_KERNEL);
2297 kfree(eps);
2298 return r;
2299}
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301/*
2302 * NOTE: All requests here that have interface numbers as parameters
2303 * are assuming that somehow the configuration has been prevented from
2304 * changing. But there's no mechanism to ensure that...
2305 */
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002306static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
2307 void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002309 struct usb_dev_state *ps = file->private_data;
Al Viro496ad9a2013-01-23 17:07:38 -05002310 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 struct usb_device *dev = ps->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 int ret = -ENOTTY;
2313
2314 if (!(file->f_mode & FMODE_WRITE))
2315 return -EPERM;
Oliver Neukum01412a22010-01-13 15:33:43 +01002316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 usb_lock_device(dev);
Alan Stern3f2cee72015-01-29 11:29:13 -05002318
2319 /* Reap operations are allowed even after disconnection */
2320 switch (cmd) {
2321 case USBDEVFS_REAPURB:
2322 snoop(&dev->dev, "%s: REAPURB\n", __func__);
2323 ret = proc_reapurb(ps, p);
2324 goto done;
2325
2326 case USBDEVFS_REAPURBNDELAY:
2327 snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
2328 ret = proc_reapurbnonblock(ps, p);
2329 goto done;
2330
2331#ifdef CONFIG_COMPAT
2332 case USBDEVFS_REAPURB32:
2333 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
2334 ret = proc_reapurb_compat(ps, p);
2335 goto done;
2336
2337 case USBDEVFS_REAPURBNDELAY32:
2338 snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
2339 ret = proc_reapurbnonblock_compat(ps, p);
2340 goto done;
2341#endif
2342 }
2343
Alan Stern349710c2006-07-01 22:05:56 -04002344 if (!connected(ps)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 usb_unlock_device(dev);
2346 return -ENODEV;
2347 }
2348
2349 switch (cmd) {
2350 case USBDEVFS_CONTROL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002351 snoop(&dev->dev, "%s: CONTROL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 ret = proc_control(ps, p);
2353 if (ret >= 0)
2354 inode->i_mtime = CURRENT_TIME;
2355 break;
2356
2357 case USBDEVFS_BULK:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002358 snoop(&dev->dev, "%s: BULK\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 ret = proc_bulk(ps, p);
2360 if (ret >= 0)
2361 inode->i_mtime = CURRENT_TIME;
2362 break;
2363
2364 case USBDEVFS_RESETEP:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002365 snoop(&dev->dev, "%s: RESETEP\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 ret = proc_resetep(ps, p);
2367 if (ret >= 0)
2368 inode->i_mtime = CURRENT_TIME;
2369 break;
2370
2371 case USBDEVFS_RESET:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002372 snoop(&dev->dev, "%s: RESET\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 ret = proc_resetdevice(ps);
2374 break;
2375
2376 case USBDEVFS_CLEAR_HALT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002377 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 ret = proc_clearhalt(ps, p);
2379 if (ret >= 0)
2380 inode->i_mtime = CURRENT_TIME;
2381 break;
2382
2383 case USBDEVFS_GETDRIVER:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002384 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 ret = proc_getdriver(ps, p);
2386 break;
2387
2388 case USBDEVFS_CONNECTINFO:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002389 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 ret = proc_connectinfo(ps, p);
2391 break;
2392
2393 case USBDEVFS_SETINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002394 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 ret = proc_setintf(ps, p);
2396 break;
2397
2398 case USBDEVFS_SETCONFIGURATION:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002399 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 ret = proc_setconfig(ps, p);
2401 break;
2402
2403 case USBDEVFS_SUBMITURB:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002404 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 ret = proc_submiturb(ps, p);
2406 if (ret >= 0)
2407 inode->i_mtime = CURRENT_TIME;
2408 break;
2409
2410#ifdef CONFIG_COMPAT
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002411 case USBDEVFS_CONTROL32:
2412 snoop(&dev->dev, "%s: CONTROL32\n", __func__);
2413 ret = proc_control_compat(ps, p);
2414 if (ret >= 0)
2415 inode->i_mtime = CURRENT_TIME;
2416 break;
2417
2418 case USBDEVFS_BULK32:
2419 snoop(&dev->dev, "%s: BULK32\n", __func__);
2420 ret = proc_bulk_compat(ps, p);
2421 if (ret >= 0)
2422 inode->i_mtime = CURRENT_TIME;
2423 break;
2424
2425 case USBDEVFS_DISCSIGNAL32:
2426 snoop(&dev->dev, "%s: DISCSIGNAL32\n", __func__);
2427 ret = proc_disconnectsignal_compat(ps, p);
2428 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430 case USBDEVFS_SUBMITURB32:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002431 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 ret = proc_submiturb_compat(ps, p);
2433 if (ret >= 0)
2434 inode->i_mtime = CURRENT_TIME;
2435 break;
2436
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002437 case USBDEVFS_IOCTL32:
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002438 snoop(&dev->dev, "%s: IOCTL32\n", __func__);
Al Viroc714de52006-10-10 22:45:37 +01002439 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002440 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441#endif
2442
2443 case USBDEVFS_DISCARDURB:
Alan Sterna016a812015-11-20 13:53:35 -05002444 snoop(&dev->dev, "%s: DISCARDURB %p\n", __func__, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 ret = proc_unlinkurb(ps, p);
2446 break;
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 case USBDEVFS_DISCSIGNAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002449 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 ret = proc_disconnectsignal(ps, p);
2451 break;
2452
2453 case USBDEVFS_CLAIMINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002454 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 ret = proc_claiminterface(ps, p);
2456 break;
2457
2458 case USBDEVFS_RELEASEINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002459 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 ret = proc_releaseinterface(ps, p);
2461 break;
2462
2463 case USBDEVFS_IOCTL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002464 snoop(&dev->dev, "%s: IOCTL\n", __func__);
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002465 ret = proc_ioctl_default(ps, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 break;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04002467
2468 case USBDEVFS_CLAIM_PORT:
2469 snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__);
2470 ret = proc_claim_port(ps, p);
2471 break;
2472
2473 case USBDEVFS_RELEASE_PORT:
2474 snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__);
2475 ret = proc_release_port(ps, p);
2476 break;
Hans de Goede19181bc2012-07-04 09:18:02 +02002477 case USBDEVFS_GET_CAPABILITIES:
2478 ret = proc_get_capabilities(ps, p);
2479 break;
Hans de Goede0837e7e2012-09-08 20:02:05 +02002480 case USBDEVFS_DISCONNECT_CLAIM:
2481 ret = proc_disconnect_claim(ps, p);
2482 break;
Hans de Goedebcf7f6e2013-10-09 17:19:31 +02002483 case USBDEVFS_ALLOC_STREAMS:
2484 ret = proc_alloc_streams(ps, p);
2485 break;
2486 case USBDEVFS_FREE_STREAMS:
2487 ret = proc_free_streams(ps, p);
2488 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
Alan Stern3f2cee72015-01-29 11:29:13 -05002490
2491 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 usb_unlock_device(dev);
2493 if (ret >= 0)
2494 inode->i_atime = CURRENT_TIME;
2495 return ret;
2496}
2497
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002498static long usbdev_ioctl(struct file *file, unsigned int cmd,
2499 unsigned long arg)
2500{
2501 int ret;
2502
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002503 ret = usbdev_do_ioctl(file, cmd, (void __user *)arg);
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002504
2505 return ret;
2506}
2507
2508#ifdef CONFIG_COMPAT
2509static long usbdev_compat_ioctl(struct file *file, unsigned int cmd,
2510 unsigned long arg)
2511{
2512 int ret;
2513
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002514 ret = usbdev_do_ioctl(file, cmd, compat_ptr(arg));
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002515
2516 return ret;
2517}
2518#endif
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520/* No kernel lock - fine */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002521static unsigned int usbdev_poll(struct file *file,
2522 struct poll_table_struct *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002524 struct usb_dev_state *ps = file->private_data;
Tobias Klauserec17cf12006-09-13 21:38:41 +02002525 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527 poll_wait(file, &ps->wait, wait);
2528 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
2529 mask |= POLLOUT | POLLWRNORM;
Alan Stern349710c2006-07-01 22:05:56 -04002530 if (!connected(ps))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 mask |= POLLERR | POLLHUP;
2532 return mask;
2533}
2534
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002535const struct file_operations usbdev_file_operations = {
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002536 .owner = THIS_MODULE,
Al Virob25472f2015-12-05 22:04:48 -05002537 .llseek = no_seek_end_llseek,
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002538 .read = usbdev_read,
2539 .poll = usbdev_poll,
2540 .unlocked_ioctl = usbdev_ioctl,
2541#ifdef CONFIG_COMPAT
2542 .compat_ioctl = usbdev_compat_ioctl,
2543#endif
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01002544 .mmap = usbdev_mmap,
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002545 .open = usbdev_open,
2546 .release = usbdev_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547};
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002548
Alan Stern501950d2009-01-13 11:33:42 -05002549static void usbdev_remove(struct usb_device *udev)
Alan Sterncd9f0372008-06-24 14:47:04 -04002550{
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002551 struct usb_dev_state *ps;
Alan Sterncd9f0372008-06-24 14:47:04 -04002552 struct siginfo sinfo;
2553
2554 while (!list_empty(&udev->filelist)) {
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002555 ps = list_entry(udev->filelist.next, struct usb_dev_state, list);
Alan Sterncd9f0372008-06-24 14:47:04 -04002556 destroy_all_async(ps);
2557 wake_up_all(&ps->wait);
2558 list_del_init(&ps->list);
2559 if (ps->discsignr) {
Alan Sternf0c2b682015-02-13 10:54:53 -05002560 memset(&sinfo, 0, sizeof(sinfo));
Alan Sterncd9f0372008-06-24 14:47:04 -04002561 sinfo.si_signo = ps->discsignr;
2562 sinfo.si_errno = EPIPE;
2563 sinfo.si_code = SI_ASYNCIO;
2564 sinfo.si_addr = ps->disccontext;
Serge Hallynd178bc32011-09-26 10:45:18 -05002565 kill_pid_info_as_cred(ps->discsignr, &sinfo,
2566 ps->disc_pid, ps->cred, ps->secid);
Alan Sterncd9f0372008-06-24 14:47:04 -04002567 }
2568 }
2569}
2570
Alan Stern501950d2009-01-13 11:33:42 -05002571static int usbdev_notify(struct notifier_block *self,
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002572 unsigned long action, void *dev)
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002573{
2574 switch (action) {
2575 case USB_DEVICE_ADD:
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002576 break;
2577 case USB_DEVICE_REMOVE:
Alan Stern501950d2009-01-13 11:33:42 -05002578 usbdev_remove(dev);
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002579 break;
2580 }
2581 return NOTIFY_OK;
2582}
2583
2584static struct notifier_block usbdev_nb = {
Chase Metzger00fe52d2015-04-09 21:41:52 -07002585 .notifier_call = usbdev_notify,
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002586};
2587
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -07002588static struct cdev usb_device_cdev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002589
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002590int __init usb_devio_init(void)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002591{
2592 int retval;
2593
Alan Sternfad21bd2005-08-10 15:15:57 -04002594 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002595 "usb_device");
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002596 if (retval) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -07002597 printk(KERN_ERR "Unable to register minors for usb_device\n");
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002598 goto out;
2599 }
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002600 cdev_init(&usb_device_cdev, &usbdev_file_operations);
Alan Sternfad21bd2005-08-10 15:15:57 -04002601 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002602 if (retval) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -07002603 printk(KERN_ERR "Unable to get usb_device major %d\n",
2604 USB_DEVICE_MAJOR);
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002605 goto error_cdev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002606 }
Alan Stern501950d2009-01-13 11:33:42 -05002607 usb_register_notify(&usbdev_nb);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002608out:
2609 return retval;
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002610
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002611error_cdev:
2612 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
2613 goto out;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002614}
2615
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002616void usb_devio_cleanup(void)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002617{
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002618 usb_unregister_notify(&usbdev_nb);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002619 cdev_del(&usb_device_cdev);
Alan Sternfad21bd2005-08-10 15:15:57 -04002620 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002621}