blob: 705c573d0257e28b5142070caaca17e06e5a25fd [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*****************************************************************************/
3
4/*
5 * devio.c -- User space communication with USB devices.
6 *
7 * Copyright (C) 1999-2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This file implements the usbfs/x/y files, where
10 * x is the bus number and y the device number.
11 *
12 * It allows user space programs/"drivers" to communicate directly
13 * with USB devices without intervening kernel driver.
14 *
15 * Revision history
16 * 22.12.1999 0.1 Initial release (split from proc_usb.c)
17 * 04.01.2000 0.2 Turned into its own filesystem
Harald Welte46113832005-10-10 19:44:29 +020018 * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery
19 * (CAN-2005-3055)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
22/*****************************************************************************/
23
24#include <linux/fs.h>
25#include <linux/mm.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010026#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/signal.h>
29#include <linux/poll.h>
30#include <linux/module.h>
Chen Gangb11b2e12013-02-02 15:57:53 +080031#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/usb.h>
33#include <linux/usbdevice_fs.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020034#include <linux/usb/hcd.h> /* for usbcore internals */
Kay Sieversfbf82fd2005-07-31 01:05:53 +020035#include <linux/cdev.h>
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -070036#include <linux/notifier.h>
David Quigley7a019552006-06-30 01:55:48 -070037#include <linux/security.h>
Serge Hallynd178bc32011-09-26 10:45:18 -050038#include <linux/user_namespace.h>
Hans de Goede3d97ff62012-07-04 09:18:03 +020039#include <linux/scatterlist.h>
Tülin İzere6889b32013-05-17 10:12:34 +030040#include <linux/uaccess.h>
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +010041#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/byteorder.h>
43#include <linux/moduleparam.h>
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "usb.h"
46
Kay Sieversfbf82fd2005-07-31 01:05:53 +020047#define USB_MAXBUS 64
Tülin İzerfa86ad02013-05-17 10:13:24 +030048#define USB_DEVICE_MAX (USB_MAXBUS * 128)
Hans de Goede3d97ff62012-07-04 09:18:03 +020049#define USB_SG_SIZE 16384 /* split-size for large txs */
Kay Sieversfbf82fd2005-07-31 01:05:53 +020050
Alan Stern4a2a8a22006-07-01 22:05:01 -040051/* Mutual exclusion for removal, open, and release */
52DEFINE_MUTEX(usbfs_mutex);
53
Valentina Manea9b6f0c42014-03-10 10:36:40 +020054struct usb_dev_state {
Alan Sterncd9f0372008-06-24 14:47:04 -040055 struct list_head list; /* state list */
56 struct usb_device *dev;
57 struct file *file;
58 spinlock_t lock; /* protects the async urb lists */
59 struct list_head async_pending;
60 struct list_head async_completed;
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +010061 struct list_head memory_list;
Alan Sterncd9f0372008-06-24 14:47:04 -040062 wait_queue_head_t wait; /* wake up if a request completed */
63 unsigned int discsignr;
64 struct pid *disc_pid;
Serge Hallynd178bc32011-09-26 10:45:18 -050065 const struct cred *cred;
Alan Sterncd9f0372008-06-24 14:47:04 -040066 void __user *disccontext;
67 unsigned long ifclaimed;
68 u32 secid;
Alan Stern01c64602009-09-01 11:09:56 -040069 u32 disabled_bulk_eps;
Reilly Grantd883f522016-02-21 18:38:01 -030070 bool privileges_dropped;
71 unsigned long interface_allowed_mask;
Alan Sterncd9f0372008-06-24 14:47:04 -040072};
73
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +010074struct usb_memory {
75 struct list_head memlist;
76 int vma_use_count;
77 int urb_use_count;
78 u32 size;
79 void *mem;
80 dma_addr_t dma_handle;
81 unsigned long vm_start;
82 struct usb_dev_state *ps;
83};
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085struct async {
86 struct list_head asynclist;
Valentina Manea9b6f0c42014-03-10 10:36:40 +020087 struct usb_dev_state *ps;
Eric W. Biederman2425c082006-10-02 02:17:28 -070088 struct pid *pid;
Serge Hallynd178bc32011-09-26 10:45:18 -050089 const struct cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 unsigned int signr;
91 unsigned int ifnum;
92 void __user *userbuffer;
93 void __user *userurb;
94 struct urb *urb;
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +010095 struct usb_memory *usbm;
Alan Sternadd1aae2011-11-17 16:41:25 -050096 unsigned int mem_usage;
Alan Sterne0152682007-08-24 15:42:52 -040097 int status;
David Quigley7a019552006-06-30 01:55:48 -070098 u32 secid;
Alan Stern01c64602009-09-01 11:09:56 -040099 u8 bulk_addr;
100 u8 bulk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030103static bool usbfs_snoop;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800104module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
105MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Alan Stern0290cc92015-11-20 13:53:22 -0500107static unsigned usbfs_snoop_max = 65536;
108module_param(usbfs_snoop_max, uint, S_IRUGO | S_IWUSR);
109MODULE_PARM_DESC(usbfs_snoop_max,
110 "maximum number of bytes to print while snooping");
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define snoop(dev, format, arg...) \
113 do { \
114 if (usbfs_snoop) \
Kris Borerf355e832015-08-07 07:22:44 -0400115 dev_info(dev, format, ## arg); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 } while (0)
117
Alan Stern4c6e8972009-06-29 11:02:04 -0400118enum snoop_when {
119 SUBMIT, COMPLETE
120};
121
Alan Sternfad21bd2005-08-10 15:15:57 -0400122#define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
123
Alan Sternadd1aae2011-11-17 16:41:25 -0500124/* Limit on the total amount of memory we can allocate for transfers */
Mateusz Berezecki1129d272016-12-21 09:19:14 -0800125static u32 usbfs_memory_mb = 16;
Alan Stern3f5eb8d2011-11-17 16:41:35 -0500126module_param(usbfs_memory_mb, uint, 0644);
127MODULE_PARM_DESC(usbfs_memory_mb,
128 "maximum MB allowed for usbfs buffers (0 = no limit)");
129
Dan Carpenter57999d12017-09-22 23:43:25 +0300130/* Hard limit, necessary to avoid arithmetic overflow */
131#define USBFS_XFER_MAX (UINT_MAX / 2 - 1000000)
132
Mateusz Berezecki1129d272016-12-21 09:19:14 -0800133static atomic64_t usbfs_memory_usage; /* Total memory currently allocated */
Alan Sternadd1aae2011-11-17 16:41:25 -0500134
135/* Check whether it's okay to allocate more memory for a transfer */
Mateusz Berezecki1129d272016-12-21 09:19:14 -0800136static int usbfs_increase_memory_usage(u64 amount)
Alan Sternadd1aae2011-11-17 16:41:25 -0500137{
Mateusz Berezecki1129d272016-12-21 09:19:14 -0800138 u64 lim;
Alan Stern3f5eb8d2011-11-17 16:41:35 -0500139
Mark Rutland6aa7de02017-10-23 14:07:29 -0700140 lim = READ_ONCE(usbfs_memory_mb);
Mateusz Berezecki1129d272016-12-21 09:19:14 -0800141 lim <<= 20;
Alan Stern3f5eb8d2011-11-17 16:41:35 -0500142
Mateusz Berezecki1129d272016-12-21 09:19:14 -0800143 atomic64_add(amount, &usbfs_memory_usage);
144
145 if (lim > 0 && atomic64_read(&usbfs_memory_usage) > lim) {
146 atomic64_sub(amount, &usbfs_memory_usage);
147 return -ENOMEM;
148 }
149
150 return 0;
Alan Sternadd1aae2011-11-17 16:41:25 -0500151}
152
153/* Memory for a transfer is being deallocated */
Mateusz Berezecki1129d272016-12-21 09:19:14 -0800154static void usbfs_decrease_memory_usage(u64 amount)
Alan Sternadd1aae2011-11-17 16:41:25 -0500155{
Mateusz Berezecki1129d272016-12-21 09:19:14 -0800156 atomic64_sub(amount, &usbfs_memory_usage);
Alan Sternadd1aae2011-11-17 16:41:25 -0500157}
Alan Stern4c6e8972009-06-29 11:02:04 -0400158
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200159static int connected(struct usb_dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Alan Stern349710c2006-07-01 22:05:56 -0400161 return (!list_empty(&ps->list) &&
162 ps->dev->state != USB_STATE_NOTATTACHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +0100165static void dec_usb_memory_use_count(struct usb_memory *usbm, int *count)
166{
167 struct usb_dev_state *ps = usbm->ps;
168 unsigned long flags;
169
170 spin_lock_irqsave(&ps->lock, flags);
171 --*count;
172 if (usbm->urb_use_count == 0 && usbm->vma_use_count == 0) {
173 list_del(&usbm->memlist);
174 spin_unlock_irqrestore(&ps->lock, flags);
175
176 usb_free_coherent(ps->dev, usbm->size, usbm->mem,
177 usbm->dma_handle);
178 usbfs_decrease_memory_usage(
179 usbm->size + sizeof(struct usb_memory));
180 kfree(usbm);
181 } else {
182 spin_unlock_irqrestore(&ps->lock, flags);
183 }
184}
185
186static void usbdev_vm_open(struct vm_area_struct *vma)
187{
188 struct usb_memory *usbm = vma->vm_private_data;
189 unsigned long flags;
190
191 spin_lock_irqsave(&usbm->ps->lock, flags);
192 ++usbm->vma_use_count;
193 spin_unlock_irqrestore(&usbm->ps->lock, flags);
194}
195
196static void usbdev_vm_close(struct vm_area_struct *vma)
197{
198 struct usb_memory *usbm = vma->vm_private_data;
199
200 dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
201}
202
Arvind Yadavb64d47a2017-08-24 16:47:09 +0530203static const struct vm_operations_struct usbdev_vm_ops = {
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +0100204 .open = usbdev_vm_open,
205 .close = usbdev_vm_close
206};
207
208static int usbdev_mmap(struct file *file, struct vm_area_struct *vma)
209{
210 struct usb_memory *usbm = NULL;
211 struct usb_dev_state *ps = file->private_data;
212 size_t size = vma->vm_end - vma->vm_start;
213 void *mem;
214 unsigned long flags;
215 dma_addr_t dma_handle;
216 int ret;
217
218 ret = usbfs_increase_memory_usage(size + sizeof(struct usb_memory));
219 if (ret)
220 goto error;
221
222 usbm = kzalloc(sizeof(struct usb_memory), GFP_KERNEL);
223 if (!usbm) {
224 ret = -ENOMEM;
225 goto error_decrease_mem;
226 }
227
Jiri Slaby70f7ca92016-06-15 15:56:11 +0200228 mem = usb_alloc_coherent(ps->dev, size, GFP_USER | __GFP_NOWARN,
229 &dma_handle);
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +0100230 if (!mem) {
231 ret = -ENOMEM;
232 goto error_free_usbm;
233 }
234
235 memset(mem, 0, size);
236
237 usbm->mem = mem;
238 usbm->dma_handle = dma_handle;
239 usbm->size = size;
240 usbm->ps = ps;
241 usbm->vm_start = vma->vm_start;
242 usbm->vma_use_count = 1;
243 INIT_LIST_HEAD(&usbm->memlist);
244
245 if (remap_pfn_range(vma, vma->vm_start,
246 virt_to_phys(usbm->mem) >> PAGE_SHIFT,
247 size, vma->vm_page_prot) < 0) {
248 dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
249 return -EAGAIN;
250 }
251
252 vma->vm_flags |= VM_IO;
253 vma->vm_flags |= (VM_DONTEXPAND | VM_DONTDUMP);
254 vma->vm_ops = &usbdev_vm_ops;
255 vma->vm_private_data = usbm;
256
257 spin_lock_irqsave(&ps->lock, flags);
258 list_add_tail(&usbm->memlist, &ps->memory_list);
259 spin_unlock_irqrestore(&ps->lock, flags);
260
261 return 0;
262
263error_free_usbm:
264 kfree(usbm);
265error_decrease_mem:
266 usbfs_decrease_memory_usage(size + sizeof(struct usb_memory));
267error:
268 return ret;
269}
270
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800271static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
272 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200274 struct usb_dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 struct usb_device *dev = ps->dev;
276 ssize_t ret = 0;
277 unsigned len;
278 loff_t pos;
279 int i;
280
281 pos = *ppos;
282 usb_lock_device(dev);
Alan Stern349710c2006-07-01 22:05:56 -0400283 if (!connected(ps)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 ret = -ENODEV;
285 goto err;
286 } else if (pos < 0) {
287 ret = -EINVAL;
288 goto err;
289 }
290
291 if (pos < sizeof(struct usb_device_descriptor)) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800292 /* 18 bytes - fits on the stack */
293 struct usb_device_descriptor temp_desc;
Oliver Neukum8781ba02006-01-06 21:24:25 +0100294
295 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
Andrew Morton9fcd5c32006-01-18 23:55:07 -0800296 le16_to_cpus(&temp_desc.bcdUSB);
297 le16_to_cpus(&temp_desc.idVendor);
298 le16_to_cpus(&temp_desc.idProduct);
299 le16_to_cpus(&temp_desc.bcdDevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 len = sizeof(struct usb_device_descriptor) - pos;
302 if (len > nbytes)
303 len = nbytes;
Oliver Neukum8781ba02006-01-06 21:24:25 +0100304 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 ret = -EFAULT;
306 goto err;
307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 *ppos += len;
310 buf += len;
311 nbytes -= len;
312 ret += len;
313 }
314
315 pos = sizeof(struct usb_device_descriptor);
316 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
317 struct usb_config_descriptor *config =
318 (struct usb_config_descriptor *)dev->rawdescriptors[i];
319 unsigned int length = le16_to_cpu(config->wTotalLength);
320
321 if (*ppos < pos + length) {
322
323 /* The descriptor may claim to be longer than it
324 * really is. Here is the actual allocated length. */
325 unsigned alloclen =
326 le16_to_cpu(dev->config[i].desc.wTotalLength);
327
328 len = length - (*ppos - pos);
329 if (len > nbytes)
330 len = nbytes;
331
332 /* Simply don't write (skip over) unallocated parts */
333 if (alloclen > (*ppos - pos)) {
334 alloclen -= (*ppos - pos);
335 if (copy_to_user(buf,
336 dev->rawdescriptors[i] + (*ppos - pos),
337 min(len, alloclen))) {
338 ret = -EFAULT;
339 goto err;
340 }
341 }
342
343 *ppos += len;
344 buf += len;
345 nbytes -= len;
346 ret += len;
347 }
348
349 pos += length;
350 }
351
352err:
353 usb_unlock_device(dev);
354 return ret;
355}
356
357/*
358 * async list handling
359 */
360
361static struct async *alloc_async(unsigned int numisoframes)
362{
Pete Zaitcevdd95b812008-01-05 02:01:07 -0800363 struct async *as;
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400364
Pete Zaitcevdd95b812008-01-05 02:01:07 -0800365 as = kzalloc(sizeof(struct async), GFP_KERNEL);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800366 if (!as)
367 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
369 if (!as->urb) {
370 kfree(as);
371 return NULL;
372 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800373 return as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376static void free_async(struct async *as)
377{
Hans de Goede3d97ff62012-07-04 09:18:03 +0200378 int i;
379
Eric W. Biederman2425c082006-10-02 02:17:28 -0700380 put_pid(as->pid);
Sarah Sharp1b41c832011-12-16 11:26:30 -0800381 if (as->cred)
382 put_cred(as->cred);
Hans de Goede3d97ff62012-07-04 09:18:03 +0200383 for (i = 0; i < as->urb->num_sgs; i++) {
384 if (sg_page(&as->urb->sg[i]))
385 kfree(sg_virt(&as->urb->sg[i]));
386 }
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +0100387
Hans de Goede3d97ff62012-07-04 09:18:03 +0200388 kfree(as->urb->sg);
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +0100389 if (as->usbm == NULL)
390 kfree(as->urb->transfer_buffer);
391 else
392 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count);
393
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -0700394 kfree(as->urb->setup_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 usb_free_urb(as->urb);
Alan Sternadd1aae2011-11-17 16:41:25 -0500396 usbfs_decrease_memory_usage(as->mem_usage);
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -0700397 kfree(as);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Alan Sternd34d9722009-03-09 13:44:48 -0400400static void async_newpending(struct async *as)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200402 struct usb_dev_state *ps = as->ps;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800403 unsigned long flags;
404
405 spin_lock_irqsave(&ps->lock, flags);
406 list_add_tail(&as->asynclist, &ps->async_pending);
407 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Alan Sternd34d9722009-03-09 13:44:48 -0400410static void async_removepending(struct async *as)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200412 struct usb_dev_state *ps = as->ps;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800413 unsigned long flags;
414
415 spin_lock_irqsave(&ps->lock, flags);
416 list_del_init(&as->asynclist);
417 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200420static struct async *async_getcompleted(struct usb_dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800422 unsigned long flags;
423 struct async *as = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800425 spin_lock_irqsave(&ps->lock, flags);
426 if (!list_empty(&ps->async_completed)) {
427 as = list_entry(ps->async_completed.next, struct async,
428 asynclist);
429 list_del_init(&as->asynclist);
430 }
431 spin_unlock_irqrestore(&ps->lock, flags);
432 return as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200435static struct async *async_getpending(struct usb_dev_state *ps,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800436 void __user *userurb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800438 struct async *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 list_for_each_entry(as, &ps->async_pending, asynclist)
441 if (as->userurb == userurb) {
442 list_del_init(&as->asynclist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return as;
444 }
Huajun Li4e09dcf2012-05-18 20:12:51 +0800445
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800446 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Alan Stern4c6e8972009-06-29 11:02:04 -0400449static void snoop_urb(struct usb_device *udev,
450 void __user *userurb, int pipe, unsigned length,
Chris Frey0880aef2010-01-26 17:07:29 -0500451 int timeout_or_status, enum snoop_when when,
452 unsigned char *data, unsigned data_len)
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700453{
Alan Stern4c6e8972009-06-29 11:02:04 -0400454 static const char *types[] = {"isoc", "int", "ctrl", "bulk"};
455 static const char *dirs[] = {"out", "in"};
456 int ep;
457 const char *t, *d;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700458
459 if (!usbfs_snoop)
460 return;
461
Alan Stern4c6e8972009-06-29 11:02:04 -0400462 ep = usb_pipeendpoint(pipe);
463 t = types[usb_pipetype(pipe)];
464 d = dirs[!!usb_pipein(pipe)];
465
466 if (userurb) { /* Async */
467 if (when == SUBMIT)
Vamsi Krishna Samavedam2f964782017-05-16 14:38:08 +0200468 dev_info(&udev->dev, "userurb %pK, ep%d %s-%s, "
Alan Stern4c6e8972009-06-29 11:02:04 -0400469 "length %u\n",
470 userurb, ep, t, d, length);
471 else
Vamsi Krishna Samavedam2f964782017-05-16 14:38:08 +0200472 dev_info(&udev->dev, "userurb %pK, ep%d %s-%s, "
Alan Stern4c6e8972009-06-29 11:02:04 -0400473 "actual_length %u status %d\n",
474 userurb, ep, t, d, length,
475 timeout_or_status);
476 } else {
477 if (when == SUBMIT)
478 dev_info(&udev->dev, "ep%d %s-%s, length %u, "
479 "timeout %d\n",
480 ep, t, d, length, timeout_or_status);
481 else
482 dev_info(&udev->dev, "ep%d %s-%s, actual_length %u, "
483 "status %d\n",
484 ep, t, d, length, timeout_or_status);
485 }
Chris Frey0880aef2010-01-26 17:07:29 -0500486
Alan Stern0290cc92015-11-20 13:53:22 -0500487 data_len = min(data_len, usbfs_snoop_max);
Chris Frey0880aef2010-01-26 17:07:29 -0500488 if (data && data_len > 0) {
489 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
490 data, data_len, 1);
491 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700492}
493
Hans de Goede3d97ff62012-07-04 09:18:03 +0200494static void snoop_urb_data(struct urb *urb, unsigned len)
495{
496 int i, size;
497
Alan Stern0290cc92015-11-20 13:53:22 -0500498 len = min(len, usbfs_snoop_max);
499 if (!usbfs_snoop || len == 0)
Hans de Goede3d97ff62012-07-04 09:18:03 +0200500 return;
501
502 if (urb->num_sgs == 0) {
503 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
504 urb->transfer_buffer, len, 1);
505 return;
506 }
507
508 for (i = 0; i < urb->num_sgs && len; i++) {
509 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
510 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
511 sg_virt(&urb->sg[i]), size, 1);
512 len -= size;
513 }
514}
515
516static int copy_urb_data_to_user(u8 __user *userbuffer, struct urb *urb)
517{
518 unsigned i, len, size;
519
520 if (urb->number_of_packets > 0) /* Isochronous */
521 len = urb->transfer_buffer_length;
522 else /* Non-Isoc */
523 len = urb->actual_length;
524
525 if (urb->num_sgs == 0) {
526 if (copy_to_user(userbuffer, urb->transfer_buffer, len))
527 return -EFAULT;
528 return 0;
529 }
530
531 for (i = 0; i < urb->num_sgs && len; i++) {
532 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
533 if (copy_to_user(userbuffer, sg_virt(&urb->sg[i]), size))
534 return -EFAULT;
535 userbuffer += size;
536 len -= size;
537 }
538
539 return 0;
540}
541
Alan Stern01c64602009-09-01 11:09:56 -0400542#define AS_CONTINUATION 1
543#define AS_UNLINK 2
544
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200545static void cancel_bulk_urbs(struct usb_dev_state *ps, unsigned bulk_addr)
Alan Stern01c64602009-09-01 11:09:56 -0400546__releases(ps->lock)
547__acquires(ps->lock)
548{
Huajun Li4e09dcf2012-05-18 20:12:51 +0800549 struct urb *urb;
Alan Stern01c64602009-09-01 11:09:56 -0400550 struct async *as;
551
552 /* Mark all the pending URBs that match bulk_addr, up to but not
553 * including the first one without AS_CONTINUATION. If such an
554 * URB is encountered then a new transfer has already started so
555 * the endpoint doesn't need to be disabled; otherwise it does.
556 */
557 list_for_each_entry(as, &ps->async_pending, asynclist) {
558 if (as->bulk_addr == bulk_addr) {
559 if (as->bulk_status != AS_CONTINUATION)
560 goto rescan;
561 as->bulk_status = AS_UNLINK;
562 as->bulk_addr = 0;
563 }
564 }
565 ps->disabled_bulk_eps |= (1 << bulk_addr);
566
567 /* Now carefully unlink all the marked pending URBs */
568 rescan:
569 list_for_each_entry(as, &ps->async_pending, asynclist) {
570 if (as->bulk_status == AS_UNLINK) {
571 as->bulk_status = 0; /* Only once */
Huajun Li4e09dcf2012-05-18 20:12:51 +0800572 urb = as->urb;
573 usb_get_urb(urb);
Alan Stern01c64602009-09-01 11:09:56 -0400574 spin_unlock(&ps->lock); /* Allow completions */
Huajun Li4e09dcf2012-05-18 20:12:51 +0800575 usb_unlink_urb(urb);
576 usb_put_urb(urb);
Alan Stern01c64602009-09-01 11:09:56 -0400577 spin_lock(&ps->lock);
578 goto rescan;
579 }
580 }
581}
582
David Howells7d12e782006-10-05 14:55:46 +0100583static void async_completed(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800585 struct async *as = urb->context;
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200586 struct usb_dev_state *ps = as->ps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct siginfo sinfo;
Oliver Neukum516a1a02009-07-08 19:09:23 +0200588 struct pid *pid = NULL;
Oliver Neukum516a1a02009-07-08 19:09:23 +0200589 u32 secid = 0;
Serge Hallynd178bc32011-09-26 10:45:18 -0500590 const struct cred *cred = NULL;
Oliver Neukum516a1a02009-07-08 19:09:23 +0200591 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800593 spin_lock(&ps->lock);
594 list_move_tail(&as->asynclist, &ps->async_completed);
Alan Sterne0152682007-08-24 15:42:52 -0400595 as->status = urb->status;
Oliver Neukum516a1a02009-07-08 19:09:23 +0200596 signr = as->signr;
597 if (signr) {
Alan Sternf0c2b682015-02-13 10:54:53 -0500598 memset(&sinfo, 0, sizeof(sinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 sinfo.si_signo = as->signr;
Alan Sterne0152682007-08-24 15:42:52 -0400600 sinfo.si_errno = as->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 sinfo.si_code = SI_ASYNCIO;
602 sinfo.si_addr = as->userurb;
Serge Hallynaec01c52011-09-26 10:18:29 -0500603 pid = get_pid(as->pid);
Serge Hallynd178bc32011-09-26 10:45:18 -0500604 cred = get_cred(as->cred);
Oliver Neukum516a1a02009-07-08 19:09:23 +0200605 secid = as->secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700607 snoop(&urb->dev->dev, "urb complete\n");
Alan Stern4c6e8972009-06-29 11:02:04 -0400608 snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length,
Hans de Goede3d97ff62012-07-04 09:18:03 +0200609 as->status, COMPLETE, NULL, 0);
Dan Carpenter83ed07c2015-05-18 15:29:51 +0300610 if ((urb->transfer_flags & URB_DIR_MASK) == URB_DIR_IN)
Hans de Goede3d97ff62012-07-04 09:18:03 +0200611 snoop_urb_data(urb, urb->actual_length);
612
Alan Stern01c64602009-09-01 11:09:56 -0400613 if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET &&
614 as->status != -ENOENT)
615 cancel_bulk_urbs(ps, as->bulk_addr);
Douglas Andersoned62ca22017-08-10 15:42:22 -0700616
617 wake_up(&ps->wait);
Oliver Neukum516a1a02009-07-08 19:09:23 +0200618 spin_unlock(&ps->lock);
619
Serge Hallynaec01c52011-09-26 10:18:29 -0500620 if (signr) {
Serge Hallynd178bc32011-09-26 10:45:18 -0500621 kill_pid_info_as_cred(sinfo.si_signo, &sinfo, pid, cred, secid);
Serge Hallynaec01c52011-09-26 10:18:29 -0500622 put_pid(pid);
Serge Hallynd178bc32011-09-26 10:45:18 -0500623 put_cred(cred);
Serge Hallynaec01c52011-09-26 10:18:29 -0500624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200627static void destroy_async(struct usb_dev_state *ps, struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Huajun Li4e09dcf2012-05-18 20:12:51 +0800629 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct async *as;
631 unsigned long flags;
632
633 spin_lock_irqsave(&ps->lock, flags);
634 while (!list_empty(list)) {
635 as = list_entry(list->next, struct async, asynclist);
636 list_del_init(&as->asynclist);
Huajun Li4e09dcf2012-05-18 20:12:51 +0800637 urb = as->urb;
638 usb_get_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 /* drop the spinlock so the completion handler can run */
641 spin_unlock_irqrestore(&ps->lock, flags);
Huajun Li4e09dcf2012-05-18 20:12:51 +0800642 usb_kill_urb(urb);
643 usb_put_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 spin_lock_irqsave(&ps->lock, flags);
645 }
646 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200649static void destroy_async_on_interface(struct usb_dev_state *ps,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800650 unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 struct list_head *p, *q, hitlist;
653 unsigned long flags;
654
655 INIT_LIST_HEAD(&hitlist);
656 spin_lock_irqsave(&ps->lock, flags);
657 list_for_each_safe(p, q, &ps->async_pending)
658 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
659 list_move_tail(p, &hitlist);
660 spin_unlock_irqrestore(&ps->lock, flags);
661 destroy_async(ps, &hitlist);
662}
663
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200664static void destroy_all_async(struct usb_dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800666 destroy_async(ps, &ps->async_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669/*
670 * interface claims are made only at the request of user level code,
671 * which can also release them (explicitly or by closing files).
672 * they're also undone when devices disconnect.
673 */
674
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800675static int driver_probe(struct usb_interface *intf,
676 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 return -ENODEV;
679}
680
681static void driver_disconnect(struct usb_interface *intf)
682{
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200683 struct usb_dev_state *ps = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
685
686 if (!ps)
687 return;
688
689 /* NOTE: this relies on usbcore having canceled and completed
690 * all pending I/O requests; 2.6 does that.
691 */
692
693 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
694 clear_bit(ifnum, &ps->ifclaimed);
695 else
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -0700696 dev_warn(&intf->dev, "interface number %u out of range\n",
697 ifnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800699 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 /* force async requests to complete */
702 destroy_async_on_interface(ps, ifnum);
703}
704
Alan Stern2e2eb832007-12-04 14:35:15 -0500705/* The following routines are merely placeholders. There is no way
706 * to inform a user task about suspend or resumes.
707 */
708static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
709{
710 return 0;
711}
712
713static int driver_resume(struct usb_interface *intf)
714{
715 return 0;
716}
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718struct usb_driver usbfs_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 .name = "usbfs",
720 .probe = driver_probe,
721 .disconnect = driver_disconnect,
Alan Stern2e2eb832007-12-04 14:35:15 -0500722 .suspend = driver_suspend,
723 .resume = driver_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724};
725
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200726static int claimintf(struct usb_dev_state *ps, unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 struct usb_device *dev = ps->dev;
729 struct usb_interface *intf;
730 int err;
731
732 if (ifnum >= 8*sizeof(ps->ifclaimed))
733 return -EINVAL;
734 /* already claimed */
735 if (test_bit(ifnum, &ps->ifclaimed))
736 return 0;
737
Reilly Grantd883f522016-02-21 18:38:01 -0300738 if (ps->privileges_dropped &&
739 !test_bit(ifnum, &ps->interface_allowed_mask))
740 return -EACCES;
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 intf = usb_ifnum_to_if(dev, ifnum);
743 if (!intf)
744 err = -ENOENT;
745 else
746 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (err == 0)
748 set_bit(ifnum, &ps->ifclaimed);
749 return err;
750}
751
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200752static int releaseintf(struct usb_dev_state *ps, unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 struct usb_device *dev;
755 struct usb_interface *intf;
756 int err;
757
758 err = -EINVAL;
759 if (ifnum >= 8*sizeof(ps->ifclaimed))
760 return err;
761 dev = ps->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 intf = usb_ifnum_to_if(dev, ifnum);
763 if (!intf)
764 err = -ENOENT;
765 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
766 usb_driver_release_interface(&usbfs_driver, intf);
767 err = 0;
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return err;
770}
771
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200772static int checkintf(struct usb_dev_state *ps, unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 if (ps->dev->state != USB_STATE_CONFIGURED)
775 return -EHOSTUNREACH;
776 if (ifnum >= 8*sizeof(ps->ifclaimed))
777 return -EINVAL;
778 if (test_bit(ifnum, &ps->ifclaimed))
779 return 0;
780 /* if not yet claimed, claim it for the driver */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800781 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
782 "interface %u before use\n", task_pid_nr(current),
783 current->comm, ifnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return claimintf(ps, ifnum);
785}
786
787static int findintfep(struct usb_device *dev, unsigned int ep)
788{
789 unsigned int i, j, e;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800790 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 struct usb_host_interface *alts;
792 struct usb_endpoint_descriptor *endpt;
793
794 if (ep & ~(USB_DIR_IN|0xf))
795 return -EINVAL;
796 if (!dev->actconfig)
797 return -ESRCH;
798 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
799 intf = dev->actconfig->interface[i];
800 for (j = 0; j < intf->num_altsetting; j++) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800801 alts = &intf->altsetting[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
803 endpt = &alts->endpoint[e].desc;
804 if (endpt->bEndpointAddress == ep)
805 return alts->desc.bInterfaceNumber;
806 }
807 }
808 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800809 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200812static int check_ctrlrecip(struct usb_dev_state *ps, unsigned int requesttype,
Matthias Dellweg393cbb52011-09-25 14:26:25 +0200813 unsigned int request, unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 int ret = 0;
Matthias Dellweg393cbb52011-09-25 14:26:25 +0200816 struct usb_host_interface *alt_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
David Vrabel6da9c992009-02-18 14:43:47 +0000818 if (ps->dev->state != USB_STATE_UNAUTHENTICATED
819 && ps->dev->state != USB_STATE_ADDRESS
Horst Schirmeier24f8b112006-03-11 00:16:55 -0800820 && ps->dev->state != USB_STATE_CONFIGURED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return -EHOSTUNREACH;
822 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
823 return 0;
824
Matthias Dellweg393cbb52011-09-25 14:26:25 +0200825 /*
826 * check for the special corner case 'get_device_id' in the printer
Hans de Goede5dc50c32013-07-12 10:04:18 +0200827 * class specification, which we always want to allow as it is used
828 * to query things like ink level, etc.
Matthias Dellweg393cbb52011-09-25 14:26:25 +0200829 */
830 if (requesttype == 0xa1 && request == 0) {
831 alt_setting = usb_find_alt_setting(ps->dev->actconfig,
832 index >> 8, index & 0xff);
833 if (alt_setting
834 && alt_setting->desc.bInterfaceClass == USB_CLASS_PRINTER)
Hans de Goede5dc50c32013-07-12 10:04:18 +0200835 return 0;
Matthias Dellweg393cbb52011-09-25 14:26:25 +0200836 }
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 index &= 0xff;
839 switch (requesttype & USB_RECIP_MASK) {
840 case USB_RECIP_ENDPOINT:
Hans de Goede1361bf4b2013-04-16 11:08:33 +0200841 if ((index & ~USB_DIR_IN) == 0)
842 return 0;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800843 ret = findintfep(ps->dev, index);
Kurt Garloff831abf72013-09-24 14:13:48 +0200844 if (ret < 0) {
845 /*
846 * Some not fully compliant Win apps seem to get
847 * index wrong and have the endpoint number here
848 * rather than the endpoint address (with the
849 * correct direction). Win does let this through,
850 * so we'll not reject it here but leave it to
851 * the device to not break KVM. But we warn.
852 */
853 ret = findintfep(ps->dev, index ^ 0x80);
854 if (ret >= 0)
855 dev_info(&ps->dev->dev,
856 "%s: process %i (%s) requesting ep %02x but needs %02x\n",
857 __func__, task_pid_nr(current),
858 current->comm, index, index ^ 0x80);
859 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800860 if (ret >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 ret = checkintf(ps, ret);
862 break;
863
864 case USB_RECIP_INTERFACE:
865 ret = checkintf(ps, index);
866 break;
867 }
868 return ret;
869}
870
Hans de Goede2fec32b2013-10-09 17:19:30 +0200871static struct usb_host_endpoint *ep_to_host_endpoint(struct usb_device *dev,
872 unsigned char ep)
873{
874 if (ep & USB_ENDPOINT_DIR_MASK)
875 return dev->ep_in[ep & USB_ENDPOINT_NUMBER_MASK];
876 else
877 return dev->ep_out[ep & USB_ENDPOINT_NUMBER_MASK];
878}
879
Linus Torvalds3e75c6d2014-04-01 17:06:09 -0700880static int parse_usbdevfs_streams(struct usb_dev_state *ps,
Hans de Goedebcf7f6e2013-10-09 17:19:31 +0200881 struct usbdevfs_streams __user *streams,
882 unsigned int *num_streams_ret,
883 unsigned int *num_eps_ret,
884 struct usb_host_endpoint ***eps_ret,
885 struct usb_interface **intf_ret)
886{
887 unsigned int i, num_streams, num_eps;
888 struct usb_host_endpoint **eps;
889 struct usb_interface *intf = NULL;
890 unsigned char ep;
891 int ifnum, ret;
892
893 if (get_user(num_streams, &streams->num_streams) ||
894 get_user(num_eps, &streams->num_eps))
895 return -EFAULT;
896
897 if (num_eps < 1 || num_eps > USB_MAXENDPOINTS)
898 return -EINVAL;
899
900 /* The XHCI controller allows max 2 ^ 16 streams */
901 if (num_streams_ret && (num_streams < 2 || num_streams > 65536))
902 return -EINVAL;
903
904 eps = kmalloc(num_eps * sizeof(*eps), GFP_KERNEL);
905 if (!eps)
906 return -ENOMEM;
907
908 for (i = 0; i < num_eps; i++) {
909 if (get_user(ep, &streams->eps[i])) {
910 ret = -EFAULT;
911 goto error;
912 }
913 eps[i] = ep_to_host_endpoint(ps->dev, ep);
914 if (!eps[i]) {
915 ret = -EINVAL;
916 goto error;
917 }
918
919 /* usb_alloc/free_streams operate on an usb_interface */
920 ifnum = findintfep(ps->dev, ep);
921 if (ifnum < 0) {
922 ret = ifnum;
923 goto error;
924 }
925
926 if (i == 0) {
927 ret = checkintf(ps, ifnum);
928 if (ret < 0)
929 goto error;
930 intf = usb_ifnum_to_if(ps->dev, ifnum);
931 } else {
932 /* Verify all eps belong to the same interface */
933 if (ifnum != intf->altsetting->desc.bInterfaceNumber) {
934 ret = -EINVAL;
935 goto error;
936 }
937 }
938 }
939
940 if (num_streams_ret)
941 *num_streams_ret = num_streams;
942 *num_eps_ret = num_eps;
943 *eps_ret = eps;
944 *intf_ret = intf;
945
946 return 0;
947
948error:
949 kfree(eps);
950 return ret;
951}
952
Alan Stern61ad04a2008-06-24 14:47:12 -0400953static int match_devt(struct device *dev, void *data)
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700954{
David Howellsa80d5ff2008-07-02 12:28:55 +0100955 return dev->devt == (dev_t) (unsigned long) data;
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100956}
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700957
Alan Stern61ad04a2008-06-24 14:47:12 -0400958static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100959{
960 struct device *dev;
961
David Howellsa80d5ff2008-07-02 12:28:55 +0100962 dev = bus_find_device(&usb_bus_type, NULL,
963 (void *) (unsigned long) devt, match_devt);
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100964 if (!dev)
965 return NULL;
Geliang Tang69ab55d2015-12-23 21:26:50 +0800966 return to_usb_device(dev);
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100967}
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969/*
970 * file operations
971 */
972static int usbdev_open(struct inode *inode, struct file *file)
973{
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200974 struct usb_device *dev = NULL;
Valentina Manea9b6f0c42014-03-10 10:36:40 +0200975 struct usb_dev_state *ps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 int ret;
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 ret = -ENOMEM;
Reilly Grantd883f522016-02-21 18:38:01 -0300979 ps = kzalloc(sizeof(struct usb_dev_state), GFP_KERNEL);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800980 if (!ps)
Alan Stern62e299e2010-01-08 12:56:19 -0500981 goto out_free_ps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Alan Stern01105a22009-07-30 15:28:14 -0400983 ret = -ENODEV;
Alan Stern61ad04a2008-06-24 14:47:12 -0400984
Alan Stern62e299e2010-01-08 12:56:19 -0500985 /* Protect against simultaneous removal or release */
986 mutex_lock(&usbfs_mutex);
987
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100988 /* usbdev device-node */
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200989 if (imajor(inode) == USB_DEVICE_MAJOR)
Alan Stern61ad04a2008-06-24 14:47:12 -0400990 dev = usbdev_lookup_by_devt(inode->i_rdev);
Alan Stern62e299e2010-01-08 12:56:19 -0500991
Alan Stern62e299e2010-01-08 12:56:19 -0500992 mutex_unlock(&usbfs_mutex);
993
994 if (!dev)
995 goto out_free_ps;
996
997 usb_lock_device(dev);
998 if (dev->state == USB_STATE_NOTATTACHED)
999 goto out_unlock_device;
1000
Alan Stern94fcda12006-11-20 11:38:46 -05001001 ret = usb_autoresume_device(dev);
Alan Stern01d883d2006-08-30 15:47:18 -04001002 if (ret)
Alan Stern62e299e2010-01-08 12:56:19 -05001003 goto out_unlock_device;
Alan Stern01d883d2006-08-30 15:47:18 -04001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 ps->dev = dev;
1006 ps->file = file;
Reilly Grantd883f522016-02-21 18:38:01 -03001007 ps->interface_allowed_mask = 0xFFFFFFFF; /* 32 bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 spin_lock_init(&ps->lock);
Dan Carpenter316547f2006-12-13 00:03:38 -08001009 INIT_LIST_HEAD(&ps->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 INIT_LIST_HEAD(&ps->async_pending);
1011 INIT_LIST_HEAD(&ps->async_completed);
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001012 INIT_LIST_HEAD(&ps->memory_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 init_waitqueue_head(&ps->wait);
Eric W. Biederman2425c082006-10-02 02:17:28 -07001014 ps->disc_pid = get_pid(task_pid(current));
Serge Hallynd178bc32011-09-26 10:45:18 -05001015 ps->cred = get_current_cred();
David Quigley7a019552006-06-30 01:55:48 -07001016 security_task_getsecid(current, &ps->secid);
Oliver Neukum527660a2007-04-20 20:50:48 +02001017 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 list_add_tail(&ps->list, &dev->filelist);
1019 file->private_data = ps;
Alan Stern62e299e2010-01-08 12:56:19 -05001020 usb_unlock_device(dev);
Alan Stern2da41d52008-10-06 11:24:26 -04001021 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
1022 current->comm);
Alan Stern62e299e2010-01-08 12:56:19 -05001023 return ret;
1024
1025 out_unlock_device:
1026 usb_unlock_device(dev);
1027 usb_put_dev(dev);
1028 out_free_ps:
1029 kfree(ps);
Alan Stern4a2a8a22006-07-01 22:05:01 -04001030 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033static int usbdev_release(struct inode *inode, struct file *file)
1034{
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001035 struct usb_dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 struct usb_device *dev = ps->dev;
1037 unsigned int ifnum;
Alan Stern6ff10462009-03-09 13:44:02 -04001038 struct async *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 usb_lock_device(dev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001041 usb_hub_release_all_ports(dev, ps);
Alan Stern4a2a8a22006-07-01 22:05:01 -04001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 list_del_init(&ps->list);
Alan Stern4a2a8a22006-07-01 22:05:01 -04001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
1046 ifnum++) {
1047 if (test_bit(ifnum, &ps->ifclaimed))
1048 releaseintf(ps, ifnum);
1049 }
1050 destroy_all_async(ps);
Alan Stern94fcda12006-11-20 11:38:46 -05001051 usb_autosuspend_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 usb_unlock_device(dev);
1053 usb_put_dev(dev);
Eric W. Biederman2425c082006-10-02 02:17:28 -07001054 put_pid(ps->disc_pid);
Serge Hallynd178bc32011-09-26 10:45:18 -05001055 put_cred(ps->cred);
Alan Stern6ff10462009-03-09 13:44:02 -04001056
1057 as = async_getcompleted(ps);
1058 while (as) {
1059 free_async(as);
1060 as = async_getcompleted(ps);
1061 }
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 kfree(ps);
Alan Stern4a2a8a22006-07-01 22:05:01 -04001064 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001067static int proc_control(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 struct usb_device *dev = ps->dev;
1070 struct usbdevfs_ctrltransfer ctrl;
1071 unsigned int tmo;
1072 unsigned char *tbuf;
Andrew Mortonff66e3c2008-03-12 13:32:24 -07001073 unsigned wLength;
Alan Stern4c6e8972009-06-29 11:02:04 -04001074 int i, pipe, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1077 return -EFAULT;
Matthias Dellweg393cbb52011-09-25 14:26:25 +02001078 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.bRequest,
1079 ctrl.wIndex);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001080 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return ret;
Andrew Mortonff66e3c2008-03-12 13:32:24 -07001082 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
1083 if (wLength > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return -EINVAL;
Alan Sternadd1aae2011-11-17 16:41:25 -05001085 ret = usbfs_increase_memory_usage(PAGE_SIZE + sizeof(struct urb) +
1086 sizeof(struct usb_ctrlrequest));
1087 if (ret)
1088 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001089 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
Alan Sternadd1aae2011-11-17 16:41:25 -05001090 if (!tbuf) {
1091 ret = -ENOMEM;
1092 goto done;
1093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 tmo = ctrl.timeout;
Chris Frey0880aef2010-01-26 17:07:29 -05001095 snoop(&dev->dev, "control urb: bRequestType=%02x "
1096 "bRequest=%02x wValue=%04x "
1097 "wIndex=%04x wLength=%04x\n",
Xenia Ragiadakouc8f2efc2013-08-31 18:09:14 +03001098 ctrl.bRequestType, ctrl.bRequest, ctrl.wValue,
1099 ctrl.wIndex, ctrl.wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (ctrl.bRequestType & 0x80) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001101 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
1102 ctrl.wLength)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001103 ret = -EINVAL;
1104 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
Alan Stern4c6e8972009-06-29 11:02:04 -04001106 pipe = usb_rcvctrlpipe(dev, 0);
Chris Frey0880aef2010-01-26 17:07:29 -05001107 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 usb_unlock_device(dev);
Alan Stern4c6e8972009-06-29 11:02:04 -04001110 i = usb_control_msg(dev, pipe, ctrl.bRequest,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001111 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
1112 tbuf, ctrl.wLength, tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 usb_lock_device(dev);
Chris Frey0880aef2010-01-26 17:07:29 -05001114 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE,
Michal Sojka9d02b422011-03-15 16:41:47 +01001115 tbuf, max(i, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if ((i > 0) && ctrl.wLength) {
Alan Sternfe0410c2005-07-29 12:16:58 -07001117 if (copy_to_user(ctrl.data, tbuf, i)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001118 ret = -EFAULT;
1119 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121 }
1122 } else {
1123 if (ctrl.wLength) {
1124 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001125 ret = -EFAULT;
1126 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128 }
Alan Stern4c6e8972009-06-29 11:02:04 -04001129 pipe = usb_sndctrlpipe(dev, 0);
Chris Frey0880aef2010-01-26 17:07:29 -05001130 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT,
1131 tbuf, ctrl.wLength);
Alan Stern4c6e8972009-06-29 11:02:04 -04001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 usb_unlock_device(dev);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001134 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
1135 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
1136 tbuf, ctrl.wLength, tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 usb_lock_device(dev);
Chris Frey0880aef2010-01-26 17:07:29 -05001138 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001140 if (i < 0 && i != -EPIPE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
1142 "failed cmd %s rqt %u rq %u len %u ret %d\n",
1143 current->comm, ctrl.bRequestType, ctrl.bRequest,
1144 ctrl.wLength, i);
1145 }
Alan Stern52fb7432011-11-17 16:41:14 -05001146 ret = i;
1147 done:
1148 free_page((unsigned long) tbuf);
Alan Sternadd1aae2011-11-17 16:41:25 -05001149 usbfs_decrease_memory_usage(PAGE_SIZE + sizeof(struct urb) +
1150 sizeof(struct usb_ctrlrequest));
Alan Stern52fb7432011-11-17 16:41:14 -05001151 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001154static int proc_bulk(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
1156 struct usb_device *dev = ps->dev;
1157 struct usbdevfs_bulktransfer bulk;
1158 unsigned int tmo, len1, pipe;
1159 int len2;
1160 unsigned char *tbuf;
Alan Stern4c6e8972009-06-29 11:02:04 -04001161 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 if (copy_from_user(&bulk, arg, sizeof(bulk)))
1164 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001165 ret = findintfep(ps->dev, bulk.ep);
1166 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001168 ret = checkintf(ps, ret);
1169 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return ret;
1171 if (bulk.ep & USB_DIR_IN)
1172 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
1173 else
1174 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
1175 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
1176 return -EINVAL;
1177 len1 = bulk.len;
Mateusz Berezecki1129d272016-12-21 09:19:14 -08001178 if (len1 >= (INT_MAX - sizeof(struct urb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return -EINVAL;
Alan Sternadd1aae2011-11-17 16:41:25 -05001180 ret = usbfs_increase_memory_usage(len1 + sizeof(struct urb));
1181 if (ret)
1182 return ret;
Kris Borer135551e2015-08-04 08:39:31 -04001183 tbuf = kmalloc(len1, GFP_KERNEL);
1184 if (!tbuf) {
Alan Sternadd1aae2011-11-17 16:41:25 -05001185 ret = -ENOMEM;
1186 goto done;
1187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 tmo = bulk.timeout;
1189 if (bulk.ep & 0x80) {
1190 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001191 ret = -EINVAL;
1192 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
Chris Frey0880aef2010-01-26 17:07:29 -05001194 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, NULL, 0);
Alan Stern4c6e8972009-06-29 11:02:04 -04001195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 usb_unlock_device(dev);
1197 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
1198 usb_lock_device(dev);
Chris Frey0880aef2010-01-26 17:07:29 -05001199 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, tbuf, len2);
Alan Stern4c6e8972009-06-29 11:02:04 -04001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (!i && len2) {
1202 if (copy_to_user(bulk.data, tbuf, len2)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001203 ret = -EFAULT;
1204 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206 }
1207 } else {
1208 if (len1) {
1209 if (copy_from_user(tbuf, bulk.data, len1)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001210 ret = -EFAULT;
1211 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213 }
Chris Frey0880aef2010-01-26 17:07:29 -05001214 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, tbuf, len1);
Alan Stern4c6e8972009-06-29 11:02:04 -04001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 usb_unlock_device(dev);
1217 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
1218 usb_lock_device(dev);
Chris Frey0880aef2010-01-26 17:07:29 -05001219 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
Alan Stern52fb7432011-11-17 16:41:14 -05001221 ret = (i < 0 ? i : len2);
1222 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 kfree(tbuf);
Alan Sternadd1aae2011-11-17 16:41:25 -05001224 usbfs_decrease_memory_usage(len1 + sizeof(struct urb));
Alan Stern52fb7432011-11-17 16:41:14 -05001225 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Alan Sternf080a512014-02-20 10:49:30 -05001228static void check_reset_of_active_ep(struct usb_device *udev,
1229 unsigned int epnum, char *ioctl_name)
1230{
1231 struct usb_host_endpoint **eps;
1232 struct usb_host_endpoint *ep;
1233
1234 eps = (epnum & USB_DIR_IN) ? udev->ep_in : udev->ep_out;
1235 ep = eps[epnum & 0x0f];
1236 if (ep && !list_empty(&ep->urb_list))
1237 dev_warn(&udev->dev, "Process %d (%s) called USBDEVFS_%s for active endpoint 0x%02x\n",
1238 task_pid_nr(current), current->comm,
1239 ioctl_name, epnum);
1240}
1241
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001242static int proc_resetep(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 unsigned int ep;
1245 int ret;
1246
1247 if (get_user(ep, (unsigned int __user *)arg))
1248 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001249 ret = findintfep(ps->dev, ep);
1250 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001252 ret = checkintf(ps, ret);
1253 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return ret;
Alan Sternf080a512014-02-20 10:49:30 -05001255 check_reset_of_active_ep(ps->dev, ep, "RESETEP");
David Vrabel3444b262009-04-08 17:36:28 +00001256 usb_reset_endpoint(ps->dev, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return 0;
1258}
1259
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001260static int proc_clearhalt(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 unsigned int ep;
1263 int pipe;
1264 int ret;
1265
1266 if (get_user(ep, (unsigned int __user *)arg))
1267 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001268 ret = findintfep(ps->dev, ep);
1269 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001271 ret = checkintf(ps, ret);
1272 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return ret;
Alan Sternf080a512014-02-20 10:49:30 -05001274 check_reset_of_active_ep(ps->dev, ep, "CLEAR_HALT");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (ep & USB_DIR_IN)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001276 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
1277 else
1278 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 return usb_clear_halt(ps->dev, pipe);
1281}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001283static int proc_getdriver(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 struct usbdevfs_getdriver gd;
1286 struct usb_interface *intf;
1287 int ret;
1288
1289 if (copy_from_user(&gd, arg, sizeof(gd)))
1290 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 intf = usb_ifnum_to_if(ps->dev, gd.interface);
1292 if (!intf || !intf->dev.driver)
1293 ret = -ENODATA;
1294 else {
Chen Gangb11b2e12013-02-02 15:57:53 +08001295 strlcpy(gd.driver, intf->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 sizeof(gd.driver));
1297 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
1298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return ret;
1300}
1301
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001302static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Kangjie Lu681fef82016-05-03 16:32:16 -04001304 struct usbdevfs_connectinfo ci;
1305
1306 memset(&ci, 0, sizeof(ci));
1307 ci.devnum = ps->dev->devnum;
1308 ci.slow = ps->dev->speed == USB_SPEED_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (copy_to_user(arg, &ci, sizeof(ci)))
1311 return -EFAULT;
1312 return 0;
1313}
1314
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001315static int proc_resetdevice(struct usb_dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Reilly Grantd883f522016-02-21 18:38:01 -03001317 struct usb_host_config *actconfig = ps->dev->actconfig;
1318 struct usb_interface *interface;
1319 int i, number;
1320
1321 /* Don't allow a device reset if the process has dropped the
1322 * privilege to do such things and any of the interfaces are
1323 * currently claimed.
1324 */
1325 if (ps->privileges_dropped && actconfig) {
1326 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
1327 interface = actconfig->interface[i];
1328 number = interface->cur_altsetting->desc.bInterfaceNumber;
1329 if (usb_interface_claimed(interface) &&
1330 !test_bit(number, &ps->ifclaimed)) {
1331 dev_warn(&ps->dev->dev,
1332 "usbfs: interface %d claimed by %s while '%s' resets device\n",
1333 number, interface->dev.driver->name, current->comm);
1334 return -EACCES;
1335 }
1336 }
1337 }
1338
Ming Lei742120c2008-06-18 22:00:29 +08001339 return usb_reset_device(ps->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001342static int proc_setintf(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
1344 struct usbdevfs_setinterface setintf;
1345 int ret;
1346
1347 if (copy_from_user(&setintf, arg, sizeof(setintf)))
1348 return -EFAULT;
Kris Borer135551e2015-08-04 08:39:31 -04001349 ret = checkintf(ps, setintf.interface);
1350 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return ret;
Hans de Goede5ec9c172013-10-09 17:19:27 +02001352
1353 destroy_async_on_interface(ps, setintf.interface);
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return usb_set_interface(ps->dev, setintf.interface,
1356 setintf.altsetting);
1357}
1358
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001359static int proc_setconfig(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Alan Stern3f141e22007-02-08 16:40:43 -05001361 int u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 int status = 0;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001363 struct usb_host_config *actconfig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Alan Stern3f141e22007-02-08 16:40:43 -05001365 if (get_user(u, (int __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return -EFAULT;
1367
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001368 actconfig = ps->dev->actconfig;
1369
1370 /* Don't touch the device if any interfaces are claimed.
1371 * It could interfere with other drivers' operations, and if
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 * an interface is claimed by usbfs it could easily deadlock.
1373 */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001374 if (actconfig) {
1375 int i;
1376
1377 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
1378 if (usb_interface_claimed(actconfig->interface[i])) {
1379 dev_warn(&ps->dev->dev,
David Brownell72ebddb2005-04-11 18:34:17 -07001380 "usbfs: interface %d claimed by %s "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 "while '%s' sets config #%d\n",
1382 actconfig->interface[i]
1383 ->cur_altsetting
1384 ->desc.bInterfaceNumber,
David Brownell72ebddb2005-04-11 18:34:17 -07001385 actconfig->interface[i]
1386 ->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 current->comm, u);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001388 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001391 }
1392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
1395 * so avoid usb_set_configuration()'s kick to sysfs
1396 */
1397 if (status == 0) {
1398 if (actconfig && actconfig->desc.bConfigurationValue == u)
1399 status = usb_reset_configuration(ps->dev);
1400 else
1401 status = usb_set_configuration(ps->dev, u);
1402 }
1403
1404 return status;
1405}
1406
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001407static struct usb_memory *
1408find_memory_area(struct usb_dev_state *ps, const struct usbdevfs_urb *uurb)
1409{
1410 struct usb_memory *usbm = NULL, *iter;
1411 unsigned long flags;
1412 unsigned long uurb_start = (unsigned long)uurb->buffer;
1413
1414 spin_lock_irqsave(&ps->lock, flags);
1415 list_for_each_entry(iter, &ps->memory_list, memlist) {
1416 if (uurb_start >= iter->vm_start &&
1417 uurb_start < iter->vm_start + iter->size) {
1418 if (uurb->buffer_length > iter->vm_start + iter->size -
1419 uurb_start) {
1420 usbm = ERR_PTR(-EINVAL);
1421 } else {
1422 usbm = iter;
1423 usbm->urb_use_count++;
1424 }
1425 break;
1426 }
1427 }
1428 spin_unlock_irqrestore(&ps->lock, flags);
1429 return usbm;
1430}
1431
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001432static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001433 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
1434 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 struct usbdevfs_iso_packet_desc *isopkt = NULL;
1437 struct usb_host_endpoint *ep;
Alan Stern52fb7432011-11-17 16:41:14 -05001438 struct async *as = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 struct usb_ctrlrequest *dr = NULL;
1440 unsigned int u, totlen, isofrmlen;
Hans de Goede3d97ff62012-07-04 09:18:03 +02001441 int i, ret, is_in, num_sgs = 0, ifnum = -1;
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001442 int number_of_packets = 0;
Hans de Goede948cd8c2013-10-09 17:19:29 +02001443 unsigned int stream_id = 0;
Hans de Goede3d97ff62012-07-04 09:18:03 +02001444 void *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Alan Stern14722ef2008-04-17 10:18:11 -04001446 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
1447 USBDEVFS_URB_SHORT_NOT_OK |
Alan Stern01c64602009-09-01 11:09:56 -04001448 USBDEVFS_URB_BULK_CONTINUATION |
Alan Stern14722ef2008-04-17 10:18:11 -04001449 USBDEVFS_URB_NO_FSBR |
1450 USBDEVFS_URB_ZERO_PACKET |
1451 USBDEVFS_URB_NO_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return -EINVAL;
Dan Carpenter57999d12017-09-22 23:43:25 +03001453 if ((unsigned int)uurb->buffer_length >= USBFS_XFER_MAX)
1454 return -EINVAL;
Alan Stern91801352009-06-29 11:04:54 -04001455 if (uurb->buffer_length > 0 && !uurb->buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001457 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
1458 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
1459 ifnum = findintfep(ps->dev, uurb->endpoint);
1460 if (ifnum < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return ifnum;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001462 ret = checkintf(ps, ifnum);
1463 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return ret;
1465 }
Hans de Goede2fec32b2013-10-09 17:19:30 +02001466 ep = ep_to_host_endpoint(ps->dev, uurb->endpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (!ep)
1468 return -ENOENT;
Hans de Goede2fec32b2013-10-09 17:19:30 +02001469 is_in = (uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0;
Alan Sternadd1aae2011-11-17 16:41:25 -05001470
1471 u = 0;
Kris Borerf355e832015-08-07 07:22:44 -04001472 switch (uurb->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 case USBDEVFS_URB_TYPE_CONTROL:
Alan Stern93cf9b92007-07-30 17:09:28 -04001474 if (!usb_endpoint_xfer_control(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return -EINVAL;
Alan Sternadd1aae2011-11-17 16:41:25 -05001476 /* min 8 byte setup packet */
1477 if (uurb->buffer_length < 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001479 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1480 if (!dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return -ENOMEM;
1482 if (copy_from_user(dr, uurb->buffer, 8)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001483 ret = -EFAULT;
1484 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001487 ret = -EINVAL;
1488 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
Matthias Dellweg393cbb52011-09-25 14:26:25 +02001490 ret = check_ctrlrecip(ps, dr->bRequestType, dr->bRequest,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001491 le16_to_cpup(&dr->wIndex));
Alan Stern52fb7432011-11-17 16:41:14 -05001492 if (ret)
1493 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1495 uurb->buffer += 8;
Alan Stern93cf9b92007-07-30 17:09:28 -04001496 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1497 is_in = 1;
1498 uurb->endpoint |= USB_DIR_IN;
1499 } else {
1500 is_in = 0;
1501 uurb->endpoint &= ~USB_DIR_IN;
1502 }
Chris Frey0880aef2010-01-26 17:07:29 -05001503 snoop(&ps->dev->dev, "control urb: bRequestType=%02x "
1504 "bRequest=%02x wValue=%04x "
1505 "wIndex=%04x wLength=%04x\n",
1506 dr->bRequestType, dr->bRequest,
1507 __le16_to_cpup(&dr->wValue),
1508 __le16_to_cpup(&dr->wIndex),
1509 __le16_to_cpup(&dr->wLength));
Alan Sternadd1aae2011-11-17 16:41:25 -05001510 u = sizeof(struct usb_ctrlrequest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 break;
1512
1513 case USBDEVFS_URB_TYPE_BULK:
Alan Stern93cf9b92007-07-30 17:09:28 -04001514 switch (usb_endpoint_type(&ep->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 case USB_ENDPOINT_XFER_CONTROL:
1516 case USB_ENDPOINT_XFER_ISOC:
1517 return -EINVAL;
Alan Sternf661c6f2009-12-11 16:20:20 -05001518 case USB_ENDPOINT_XFER_INT:
1519 /* allow single-shot interrupt transfers */
1520 uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
1521 goto interrupt_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
Hans de Goede3d97ff62012-07-04 09:18:03 +02001523 num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE);
1524 if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize)
1525 num_sgs = 0;
Hans de Goede948cd8c2013-10-09 17:19:29 +02001526 if (ep->streams)
1527 stream_id = uurb->stream_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 break;
1529
Alan Sternf661c6f2009-12-11 16:20:20 -05001530 case USBDEVFS_URB_TYPE_INTERRUPT:
1531 if (!usb_endpoint_xfer_int(&ep->desc))
1532 return -EINVAL;
1533 interrupt_urb:
Alan Sternf661c6f2009-12-11 16:20:20 -05001534 break;
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 case USBDEVFS_URB_TYPE_ISO:
1537 /* arbitrary limit */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001538 if (uurb->number_of_packets < 1 ||
1539 uurb->number_of_packets > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return -EINVAL;
Alan Stern93cf9b92007-07-30 17:09:28 -04001541 if (!usb_endpoint_xfer_isoc(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return -EINVAL;
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001543 number_of_packets = uurb->number_of_packets;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001544 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001545 number_of_packets;
Rahul Pathak73a02d32015-12-11 05:40:51 +00001546 isopkt = memdup_user(iso_frame_desc, isofrmlen);
1547 if (IS_ERR(isopkt)) {
1548 ret = PTR_ERR(isopkt);
1549 isopkt = NULL;
Alan Stern52fb7432011-11-17 16:41:14 -05001550 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001552 for (totlen = u = 0; u < number_of_packets; u++) {
Federico Manzane2e2f0e2013-05-24 18:18:48 +02001553 /*
1554 * arbitrary limit need for USB 3.0
1555 * bMaxBurst (0~15 allowed, 1~16 packets)
1556 * bmAttributes (bit 1:0, mult 0~2, 1~3 packets)
1557 * sizemax: 1024 * 16 * 3 = 49152
1558 */
1559 if (isopkt[u].length > 49152) {
Alan Stern52fb7432011-11-17 16:41:14 -05001560 ret = -EINVAL;
1561 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
1563 totlen += isopkt[u].length;
1564 }
Alan Sternadd1aae2011-11-17 16:41:25 -05001565 u *= sizeof(struct usb_iso_packet_descriptor);
Hans de Goede845d5842017-10-16 16:21:19 +02001566 uurb->buffer_length = totlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 break;
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 default:
1570 return -EINVAL;
1571 }
Alan Sternadd1aae2011-11-17 16:41:25 -05001572
Alan Stern91801352009-06-29 11:04:54 -04001573 if (uurb->buffer_length > 0 &&
1574 !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1575 uurb->buffer, uurb->buffer_length)) {
Alan Stern52fb7432011-11-17 16:41:14 -05001576 ret = -EFAULT;
1577 goto error;
Alan Stern91801352009-06-29 11:04:54 -04001578 }
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001579 as = alloc_async(number_of_packets);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001580 if (!as) {
Alan Stern52fb7432011-11-17 16:41:14 -05001581 ret = -ENOMEM;
1582 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
Hans de Goede3d97ff62012-07-04 09:18:03 +02001584
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001585 as->usbm = find_memory_area(ps, uurb);
1586 if (IS_ERR(as->usbm)) {
1587 ret = PTR_ERR(as->usbm);
1588 as->usbm = NULL;
1589 goto error;
1590 }
1591
1592 /* do not use SG buffers when memory mapped segments
1593 * are in use
1594 */
1595 if (as->usbm)
1596 num_sgs = 0;
1597
Hans de Goede3d97ff62012-07-04 09:18:03 +02001598 u += sizeof(struct async) + sizeof(struct urb) + uurb->buffer_length +
1599 num_sgs * sizeof(struct scatterlist);
Alan Sternadd1aae2011-11-17 16:41:25 -05001600 ret = usbfs_increase_memory_usage(u);
1601 if (ret)
1602 goto error;
1603 as->mem_usage = u;
1604
Hans de Goede3d97ff62012-07-04 09:18:03 +02001605 if (num_sgs) {
1606 as->urb->sg = kmalloc(num_sgs * sizeof(struct scatterlist),
1607 GFP_KERNEL);
1608 if (!as->urb->sg) {
1609 ret = -ENOMEM;
1610 goto error;
1611 }
1612 as->urb->num_sgs = num_sgs;
1613 sg_init_table(as->urb->sg, as->urb->num_sgs);
1614
1615 totlen = uurb->buffer_length;
1616 for (i = 0; i < as->urb->num_sgs; i++) {
1617 u = (totlen > USB_SG_SIZE) ? USB_SG_SIZE : totlen;
1618 buf = kmalloc(u, GFP_KERNEL);
1619 if (!buf) {
1620 ret = -ENOMEM;
1621 goto error;
1622 }
1623 sg_set_buf(&as->urb->sg[i], buf, u);
1624
1625 if (!is_in) {
1626 if (copy_from_user(buf, uurb->buffer, u)) {
1627 ret = -EFAULT;
1628 goto error;
1629 }
Henrik Rydberg01463902012-10-13 12:20:36 +02001630 uurb->buffer += u;
Hans de Goede3d97ff62012-07-04 09:18:03 +02001631 }
1632 totlen -= u;
1633 }
1634 } else if (uurb->buffer_length > 0) {
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001635 if (as->usbm) {
1636 unsigned long uurb_start = (unsigned long)uurb->buffer;
Hans de Goede3d97ff62012-07-04 09:18:03 +02001637
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001638 as->urb->transfer_buffer = as->usbm->mem +
1639 (uurb_start - as->usbm->vm_start);
1640 } else {
1641 as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
1642 GFP_KERNEL);
1643 if (!as->urb->transfer_buffer) {
1644 ret = -ENOMEM;
Hans de Goede3d97ff62012-07-04 09:18:03 +02001645 goto error;
1646 }
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001647 if (!is_in) {
1648 if (copy_from_user(as->urb->transfer_buffer,
1649 uurb->buffer,
1650 uurb->buffer_length)) {
1651 ret = -EFAULT;
1652 goto error;
1653 }
1654 } else if (uurb->type == USBDEVFS_URB_TYPE_ISO) {
1655 /*
1656 * Isochronous input data may end up being
1657 * discontiguous if some of the packets are
1658 * short. Clear the buffer so that the gaps
1659 * don't leak kernel data to userspace.
1660 */
1661 memset(as->urb->transfer_buffer, 0,
1662 uurb->buffer_length);
1663 }
Hans de Goede3d97ff62012-07-04 09:18:03 +02001664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001666 as->urb->dev = ps->dev;
1667 as->urb->pipe = (uurb->type << 30) |
Alan Stern93cf9b92007-07-30 17:09:28 -04001668 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1669 (uurb->endpoint & USB_DIR_IN);
Alan Stern14722ef2008-04-17 10:18:11 -04001670
1671 /* This tedious sequence is necessary because the URB_* flags
1672 * are internal to the kernel and subject to change, whereas
1673 * the USBDEVFS_URB_* flags are a user API and must not be changed.
1674 */
1675 u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
1676 if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
1677 u |= URB_ISO_ASAP;
Oliver Neukumd310d052014-08-01 09:55:20 +02001678 if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK && is_in)
Alan Stern14722ef2008-04-17 10:18:11 -04001679 u |= URB_SHORT_NOT_OK;
1680 if (uurb->flags & USBDEVFS_URB_NO_FSBR)
1681 u |= URB_NO_FSBR;
1682 if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1683 u |= URB_ZERO_PACKET;
1684 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
1685 u |= URB_NO_INTERRUPT;
1686 as->urb->transfer_flags = u;
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 as->urb->transfer_buffer_length = uurb->buffer_length;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001689 as->urb->setup_packet = (unsigned char *)dr;
Alan Stern52fb7432011-11-17 16:41:14 -05001690 dr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 as->urb->start_frame = uurb->start_frame;
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001692 as->urb->number_of_packets = number_of_packets;
Hans de Goede948cd8c2013-10-09 17:19:29 +02001693 as->urb->stream_id = stream_id;
Alan Stern53e5f362016-08-23 15:32:51 -04001694
1695 if (ep->desc.bInterval) {
1696 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1697 ps->dev->speed == USB_SPEED_HIGH ||
1698 ps->dev->speed >= USB_SPEED_SUPER)
1699 as->urb->interval = 1 <<
1700 min(15, ep->desc.bInterval - 1);
1701 else
1702 as->urb->interval = ep->desc.bInterval;
1703 }
1704
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001705 as->urb->context = as;
1706 as->urb->complete = async_completed;
Hans de Goedeb2d03eb2013-10-09 17:19:28 +02001707 for (totlen = u = 0; u < number_of_packets; u++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 as->urb->iso_frame_desc[u].offset = totlen;
1709 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1710 totlen += isopkt[u].length;
1711 }
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -07001712 kfree(isopkt);
Alan Stern52fb7432011-11-17 16:41:14 -05001713 isopkt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 as->ps = ps;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001715 as->userurb = arg;
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001716 if (as->usbm) {
1717 unsigned long uurb_start = (unsigned long)uurb->buffer;
1718
1719 as->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1720 as->urb->transfer_dma = as->usbm->dma_handle +
1721 (uurb_start - as->usbm->vm_start);
1722 } else if (is_in && uurb->buffer_length > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 as->userbuffer = uurb->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 as->signr = uurb->signr;
1725 as->ifnum = ifnum;
Eric W. Biederman2425c082006-10-02 02:17:28 -07001726 as->pid = get_pid(task_pid(current));
Serge Hallynd178bc32011-09-26 10:45:18 -05001727 as->cred = get_current_cred();
David Quigley7a019552006-06-30 01:55:48 -07001728 security_task_getsecid(current, &as->secid);
Alan Stern4c6e8972009-06-29 11:02:04 -04001729 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
Chris Frey0880aef2010-01-26 17:07:29 -05001730 as->urb->transfer_buffer_length, 0, SUBMIT,
Hans de Goede3d97ff62012-07-04 09:18:03 +02001731 NULL, 0);
1732 if (!is_in)
1733 snoop_urb_data(as->urb, as->urb->transfer_buffer_length);
1734
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001735 async_newpending(as);
Alan Stern01c64602009-09-01 11:09:56 -04001736
1737 if (usb_endpoint_xfer_bulk(&ep->desc)) {
1738 spin_lock_irq(&ps->lock);
1739
1740 /* Not exactly the endpoint address; the direction bit is
1741 * shifted to the 0x10 position so that the value will be
1742 * between 0 and 31.
1743 */
1744 as->bulk_addr = usb_endpoint_num(&ep->desc) |
1745 ((ep->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1746 >> 3);
1747
1748 /* If this bulk URB is the start of a new transfer, re-enable
1749 * the endpoint. Otherwise mark it as a continuation URB.
1750 */
1751 if (uurb->flags & USBDEVFS_URB_BULK_CONTINUATION)
1752 as->bulk_status = AS_CONTINUATION;
1753 else
1754 ps->disabled_bulk_eps &= ~(1 << as->bulk_addr);
1755
1756 /* Don't accept continuation URBs if the endpoint is
1757 * disabled because of an earlier error.
1758 */
1759 if (ps->disabled_bulk_eps & (1 << as->bulk_addr))
1760 ret = -EREMOTEIO;
1761 else
1762 ret = usb_submit_urb(as->urb, GFP_ATOMIC);
1763 spin_unlock_irq(&ps->lock);
1764 } else {
1765 ret = usb_submit_urb(as->urb, GFP_KERNEL);
1766 }
1767
1768 if (ret) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001769 dev_printk(KERN_DEBUG, &ps->dev->dev,
1770 "usbfs: usb_submit_urb returned %d\n", ret);
Alan Stern4c6e8972009-06-29 11:02:04 -04001771 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
Chris Frey0880aef2010-01-26 17:07:29 -05001772 0, ret, COMPLETE, NULL, 0);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001773 async_removepending(as);
Alan Stern52fb7432011-11-17 16:41:14 -05001774 goto error;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001775 }
1776 return 0;
Alan Stern52fb7432011-11-17 16:41:14 -05001777
1778 error:
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01001779 if (as && as->usbm)
1780 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count);
Alan Stern52fb7432011-11-17 16:41:14 -05001781 kfree(isopkt);
1782 kfree(dr);
1783 if (as)
1784 free_async(as);
1785 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001788static int proc_submiturb(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
1790 struct usbdevfs_urb uurb;
1791
1792 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1793 return -EFAULT;
1794
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001795 return proc_do_submiturb(ps, &uurb,
1796 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1797 arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001800static int proc_unlinkurb(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Huajun Li4e09dcf2012-05-18 20:12:51 +08001802 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 struct async *as;
Huajun Li4e09dcf2012-05-18 20:12:51 +08001804 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Huajun Li4e09dcf2012-05-18 20:12:51 +08001806 spin_lock_irqsave(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 as = async_getpending(ps, arg);
Huajun Li4e09dcf2012-05-18 20:12:51 +08001808 if (!as) {
1809 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 return -EINVAL;
Huajun Li4e09dcf2012-05-18 20:12:51 +08001811 }
1812
1813 urb = as->urb;
1814 usb_get_urb(urb);
1815 spin_unlock_irqrestore(&ps->lock, flags);
1816
1817 usb_kill_urb(urb);
1818 usb_put_urb(urb);
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return 0;
1821}
1822
Alan Stern2ef47002017-11-08 12:23:17 -05001823static void compute_isochronous_actual_length(struct urb *urb)
1824{
1825 unsigned int i;
1826
1827 if (urb->number_of_packets > 0) {
1828 urb->actual_length = 0;
1829 for (i = 0; i < urb->number_of_packets; i++)
1830 urb->actual_length +=
1831 urb->iso_frame_desc[i].actual_length;
1832 }
1833}
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835static int processcompl(struct async *as, void __user * __user *arg)
1836{
1837 struct urb *urb = as->urb;
1838 struct usbdevfs_urb __user *userurb = as->userurb;
1839 void __user *addr = as->userurb;
1840 unsigned int i;
1841
Alan Stern2ef47002017-11-08 12:23:17 -05001842 compute_isochronous_actual_length(urb);
Alan Stern7152b592010-03-06 15:04:03 -05001843 if (as->userbuffer && urb->actual_length) {
Hans de Goede3d97ff62012-07-04 09:18:03 +02001844 if (copy_urb_data_to_user(as->userbuffer, urb))
Oliver Neukumd794a022009-06-28 23:34:14 +02001845 goto err_out;
Alan Stern7152b592010-03-06 15:04:03 -05001846 }
Alan Sterne0152682007-08-24 15:42:52 -04001847 if (put_user(as->status, &userurb->status))
Oliver Neukumd794a022009-06-28 23:34:14 +02001848 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (put_user(urb->actual_length, &userurb->actual_length))
Oliver Neukumd794a022009-06-28 23:34:14 +02001850 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 if (put_user(urb->error_count, &userurb->error_count))
Oliver Neukumd794a022009-06-28 23:34:14 +02001852 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Alan Stern93cf9b92007-07-30 17:09:28 -04001854 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
Christopher Li668a9542005-04-18 17:39:26 -07001855 for (i = 0; i < urb->number_of_packets; i++) {
1856 if (put_user(urb->iso_frame_desc[i].actual_length,
1857 &userurb->iso_frame_desc[i].actual_length))
Oliver Neukumd794a022009-06-28 23:34:14 +02001858 goto err_out;
Christopher Li668a9542005-04-18 17:39:26 -07001859 if (put_user(urb->iso_frame_desc[i].status,
1860 &userurb->iso_frame_desc[i].status))
Oliver Neukumd794a022009-06-28 23:34:14 +02001861 goto err_out;
Christopher Li668a9542005-04-18 17:39:26 -07001862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (put_user(addr, (void __user * __user *)arg))
1866 return -EFAULT;
1867 return 0;
Oliver Neukumd794a022009-06-28 23:34:14 +02001868
1869err_out:
Oliver Neukumd794a022009-06-28 23:34:14 +02001870 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871}
1872
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001873static struct async *reap_as(struct usb_dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001875 DECLARE_WAITQUEUE(wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 struct async *as = NULL;
1877 struct usb_device *dev = ps->dev;
1878
1879 add_wait_queue(&ps->wait, &wait);
1880 for (;;) {
1881 __set_current_state(TASK_INTERRUPTIBLE);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001882 as = async_getcompleted(ps);
Alan Stern3f2cee72015-01-29 11:29:13 -05001883 if (as || !connected(ps))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 break;
1885 if (signal_pending(current))
1886 break;
1887 usb_unlock_device(dev);
1888 schedule();
1889 usb_lock_device(dev);
1890 }
1891 remove_wait_queue(&ps->wait, &wait);
1892 set_current_state(TASK_RUNNING);
1893 return as;
1894}
1895
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001896static int proc_reapurb(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 struct async *as = reap_as(ps);
Alan Sterna016a812015-11-20 13:53:35 -05001899
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001900 if (as) {
Alan Sterna016a812015-11-20 13:53:35 -05001901 int retval;
1902
Vamsi Krishna Samavedam2f964782017-05-16 14:38:08 +02001903 snoop(&ps->dev->dev, "reap %pK\n", as->userurb);
Alan Sterna016a812015-11-20 13:53:35 -05001904 retval = processcompl(as, (void __user * __user *)arg);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001905 free_async(as);
1906 return retval;
1907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (signal_pending(current))
1909 return -EINTR;
Alan Stern3f2cee72015-01-29 11:29:13 -05001910 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001913static int proc_reapurbnonblock(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001915 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 struct async *as;
1917
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001918 as = async_getcompleted(ps);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001919 if (as) {
Vamsi Krishna Samavedam2f964782017-05-16 14:38:08 +02001920 snoop(&ps->dev->dev, "reap %pK\n", as->userurb);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001921 retval = processcompl(as, (void __user * __user *)arg);
1922 free_async(as);
Alan Stern3f2cee72015-01-29 11:29:13 -05001923 } else {
1924 retval = (connected(ps) ? -EAGAIN : -ENODEV);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08001925 }
1926 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
1929#ifdef CONFIG_COMPAT
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001930static int proc_control_compat(struct usb_dev_state *ps,
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001931 struct usbdevfs_ctrltransfer32 __user *p32)
1932{
Matthias Beyer5b32c382013-10-14 21:46:36 +02001933 struct usbdevfs_ctrltransfer __user *p;
1934 __u32 udata;
1935 p = compat_alloc_user_space(sizeof(*p));
1936 if (copy_in_user(p, p32, (sizeof(*p32) - sizeof(compat_caddr_t))) ||
1937 get_user(udata, &p32->data) ||
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001938 put_user(compat_ptr(udata), &p->data))
1939 return -EFAULT;
Matthias Beyer5b32c382013-10-14 21:46:36 +02001940 return proc_control(ps, p);
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001941}
1942
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001943static int proc_bulk_compat(struct usb_dev_state *ps,
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001944 struct usbdevfs_bulktransfer32 __user *p32)
1945{
Matthias Beyer06793f22013-10-14 21:46:37 +02001946 struct usbdevfs_bulktransfer __user *p;
1947 compat_uint_t n;
1948 compat_caddr_t addr;
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001949
Matthias Beyer06793f22013-10-14 21:46:37 +02001950 p = compat_alloc_user_space(sizeof(*p));
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001951
Matthias Beyer06793f22013-10-14 21:46:37 +02001952 if (get_user(n, &p32->ep) || put_user(n, &p->ep) ||
1953 get_user(n, &p32->len) || put_user(n, &p->len) ||
1954 get_user(n, &p32->timeout) || put_user(n, &p->timeout) ||
1955 get_user(addr, &p32->data) || put_user(compat_ptr(addr), &p->data))
1956 return -EFAULT;
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001957
Matthias Beyer06793f22013-10-14 21:46:37 +02001958 return proc_bulk(ps, p);
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001959}
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001960static int proc_disconnectsignal_compat(struct usb_dev_state *ps, void __user *arg)
Arnd Bergmann637e8a62009-11-14 02:28:05 +01001961{
1962 struct usbdevfs_disconnectsignal32 ds;
1963
1964 if (copy_from_user(&ds, arg, sizeof(ds)))
1965 return -EFAULT;
1966 ps->discsignr = ds.signr;
1967 ps->disccontext = compat_ptr(ds.context);
1968 return 0;
1969}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971static int get_urb32(struct usbdevfs_urb *kurb,
1972 struct usbdevfs_urb32 __user *uurb)
1973{
Al Virocc1a7c42017-06-27 17:46:06 -04001974 struct usbdevfs_urb32 urb32;
1975 if (copy_from_user(&urb32, uurb, sizeof(*uurb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 return -EFAULT;
Al Virocc1a7c42017-06-27 17:46:06 -04001977 kurb->type = urb32.type;
1978 kurb->endpoint = urb32.endpoint;
1979 kurb->status = urb32.status;
1980 kurb->flags = urb32.flags;
1981 kurb->buffer = compat_ptr(urb32.buffer);
1982 kurb->buffer_length = urb32.buffer_length;
1983 kurb->actual_length = urb32.actual_length;
1984 kurb->start_frame = urb32.start_frame;
1985 kurb->number_of_packets = urb32.number_of_packets;
1986 kurb->error_count = urb32.error_count;
1987 kurb->signr = urb32.signr;
1988 kurb->usercontext = compat_ptr(urb32.usercontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return 0;
1990}
1991
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001992static int proc_submiturb_compat(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
1994 struct usbdevfs_urb uurb;
1995
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001996 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return -EFAULT;
1998
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001999 return proc_do_submiturb(ps, &uurb,
2000 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
2001 arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
2003
2004static int processcompl_compat(struct async *as, void __user * __user *arg)
2005{
2006 struct urb *urb = as->urb;
2007 struct usbdevfs_urb32 __user *userurb = as->userurb;
2008 void __user *addr = as->userurb;
2009 unsigned int i;
2010
Alan Stern2ef47002017-11-08 12:23:17 -05002011 compute_isochronous_actual_length(urb);
Hans de Goede2102e062012-07-04 09:18:01 +02002012 if (as->userbuffer && urb->actual_length) {
Hans de Goede3d97ff62012-07-04 09:18:03 +02002013 if (copy_urb_data_to_user(as->userbuffer, urb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return -EFAULT;
Hans de Goede2102e062012-07-04 09:18:01 +02002015 }
Alan Sterne0152682007-08-24 15:42:52 -04002016 if (put_user(as->status, &userurb->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 return -EFAULT;
2018 if (put_user(urb->actual_length, &userurb->actual_length))
2019 return -EFAULT;
2020 if (put_user(urb->error_count, &userurb->error_count))
2021 return -EFAULT;
2022
Alan Stern93cf9b92007-07-30 17:09:28 -04002023 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
Christopher Li668a9542005-04-18 17:39:26 -07002024 for (i = 0; i < urb->number_of_packets; i++) {
2025 if (put_user(urb->iso_frame_desc[i].actual_length,
2026 &userurb->iso_frame_desc[i].actual_length))
2027 return -EFAULT;
2028 if (put_user(urb->iso_frame_desc[i].status,
2029 &userurb->iso_frame_desc[i].status))
2030 return -EFAULT;
2031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 }
2033
Al Viroc714de52006-10-10 22:45:37 +01002034 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 return -EFAULT;
2036 return 0;
2037}
2038
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002039static int proc_reapurb_compat(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
2041 struct async *as = reap_as(ps);
Alan Sterna016a812015-11-20 13:53:35 -05002042
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08002043 if (as) {
Alan Sterna016a812015-11-20 13:53:35 -05002044 int retval;
2045
Vamsi Krishna Samavedam2f964782017-05-16 14:38:08 +02002046 snoop(&ps->dev->dev, "reap %pK\n", as->userurb);
Alan Sterna016a812015-11-20 13:53:35 -05002047 retval = processcompl_compat(as, (void __user * __user *)arg);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08002048 free_async(as);
2049 return retval;
2050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (signal_pending(current))
2052 return -EINTR;
Alan Stern3f2cee72015-01-29 11:29:13 -05002053 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002056static int proc_reapurbnonblock_compat(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08002058 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 struct async *as;
2060
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08002061 as = async_getcompleted(ps);
2062 if (as) {
Vamsi Krishna Samavedam2f964782017-05-16 14:38:08 +02002063 snoop(&ps->dev->dev, "reap %pK\n", as->userurb);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08002064 retval = processcompl_compat(as, (void __user * __user *)arg);
2065 free_async(as);
Alan Stern3f2cee72015-01-29 11:29:13 -05002066 } else {
2067 retval = (connected(ps) ? -EAGAIN : -ENODEV);
Linus Torvaldsddeee0b2010-02-16 12:35:07 -08002068 }
2069 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073#endif
2074
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002075static int proc_disconnectsignal(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
2077 struct usbdevfs_disconnectsignal ds;
2078
2079 if (copy_from_user(&ds, arg, sizeof(ds)))
2080 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 ps->discsignr = ds.signr;
2082 ps->disccontext = ds.context;
2083 return 0;
2084}
2085
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002086static int proc_claiminterface(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
2088 unsigned int ifnum;
2089
2090 if (get_user(ifnum, (unsigned int __user *)arg))
2091 return -EFAULT;
2092 return claimintf(ps, ifnum);
2093}
2094
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002095static int proc_releaseinterface(struct usb_dev_state *ps, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
2097 unsigned int ifnum;
2098 int ret;
2099
2100 if (get_user(ifnum, (unsigned int __user *)arg))
2101 return -EFAULT;
Kris Borer135551e2015-08-04 08:39:31 -04002102 ret = releaseintf(ps, ifnum);
2103 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return ret;
Chase Metzger64f10ed2015-12-22 20:19:45 -08002105 destroy_async_on_interface(ps, ifnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 return 0;
2107}
2108
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002109static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 int size;
2112 void *buf = NULL;
2113 int retval = 0;
2114 struct usb_interface *intf = NULL;
2115 struct usb_driver *driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Reilly Grantd883f522016-02-21 18:38:01 -03002117 if (ps->privileges_dropped)
2118 return -EACCES;
2119
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002120 /* alloc buffer */
Kris Borer135551e2015-08-04 08:39:31 -04002121 size = _IOC_SIZE(ctl->ioctl_code);
2122 if (size > 0) {
Tülin İzer4baf0df2013-05-17 10:13:58 +03002123 buf = kmalloc(size, GFP_KERNEL);
2124 if (buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 return -ENOMEM;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002126 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002127 if (copy_from_user(buf, ctl->data, size)) {
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -07002128 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return -EFAULT;
2130 }
2131 } else {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002132 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134 }
2135
Alan Stern349710c2006-07-01 22:05:56 -04002136 if (!connected(ps)) {
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -07002137 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return -ENODEV;
2139 }
2140
2141 if (ps->dev->state != USB_STATE_CONFIGURED)
2142 retval = -EHOSTUNREACH;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002143 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
2144 retval = -EINVAL;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002145 else switch (ctl->ioctl_code) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 /* disconnect kernel driver from interface */
2148 case USBDEVFS_DISCONNECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (intf->dev.driver) {
2150 driver = to_usb_driver(intf->dev.driver);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002151 dev_dbg(&intf->dev, "disconnect by usbfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 usb_driver_release_interface(driver, intf);
2153 } else
2154 retval = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 break;
2156
2157 /* let kernel drivers try to (re)bind to the interface */
2158 case USBDEVFS_CONNECT:
Alan Stern885e9742007-12-03 15:42:10 -05002159 if (!intf->dev.driver)
2160 retval = device_attach(&intf->dev);
2161 else
2162 retval = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 break;
2164
2165 /* talk directly to the interface's driver */
2166 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (intf->dev.driver)
2168 driver = to_usb_driver(intf->dev.driver);
Andi Kleenc532b292010-06-01 23:04:41 +02002169 if (driver == NULL || driver->unlocked_ioctl == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 retval = -ENOTTY;
2171 } else {
Andi Kleenc532b292010-06-01 23:04:41 +02002172 retval = driver->unlocked_ioctl(intf, ctl->ioctl_code, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 if (retval == -ENOIOCTLCMD)
2174 retval = -ENOTTY;
2175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
2177
2178 /* cleanup and return */
2179 if (retval >= 0
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002180 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 && size > 0
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002182 && copy_to_user(ctl->data, buf, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 retval = -EFAULT;
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -07002184
2185 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return retval;
2187}
2188
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002189static int proc_ioctl_default(struct usb_dev_state *ps, void __user *arg)
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002190{
2191 struct usbdevfs_ioctl ctrl;
2192
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002193 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002194 return -EFAULT;
2195 return proc_ioctl(ps, &ctrl);
2196}
2197
2198#ifdef CONFIG_COMPAT
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002199static int proc_ioctl_compat(struct usb_dev_state *ps, compat_uptr_t arg)
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002200{
Al Virocc1a7c42017-06-27 17:46:06 -04002201 struct usbdevfs_ioctl32 ioc32;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002202 struct usbdevfs_ioctl ctrl;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002203
Al Virocc1a7c42017-06-27 17:46:06 -04002204 if (copy_from_user(&ioc32, compat_ptr(arg), sizeof(ioc32)))
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002205 return -EFAULT;
Al Virocc1a7c42017-06-27 17:46:06 -04002206 ctrl.ifno = ioc32.ifno;
2207 ctrl.ioctl_code = ioc32.ioctl_code;
2208 ctrl.data = compat_ptr(ioc32.data);
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002209 return proc_ioctl(ps, &ctrl);
2210}
2211#endif
2212
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002213static int proc_claim_port(struct usb_dev_state *ps, void __user *arg)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04002214{
2215 unsigned portnum;
2216 int rc;
2217
2218 if (get_user(portnum, (unsigned __user *) arg))
2219 return -EFAULT;
2220 rc = usb_hub_claim_port(ps->dev, portnum, ps);
2221 if (rc == 0)
2222 snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n",
2223 portnum, task_pid_nr(current), current->comm);
2224 return rc;
2225}
2226
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002227static int proc_release_port(struct usb_dev_state *ps, void __user *arg)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04002228{
2229 unsigned portnum;
2230
2231 if (get_user(portnum, (unsigned __user *) arg))
2232 return -EFAULT;
2233 return usb_hub_release_port(ps->dev, portnum, ps);
2234}
2235
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002236static int proc_get_capabilities(struct usb_dev_state *ps, void __user *arg)
Hans de Goede19181bc2012-07-04 09:18:02 +02002237{
2238 __u32 caps;
2239
Alan Stern3f2cee72015-01-29 11:29:13 -05002240 caps = USBDEVFS_CAP_ZERO_PACKET | USBDEVFS_CAP_NO_PACKET_SIZE_LIM |
Reilly Grantd883f522016-02-21 18:38:01 -03002241 USBDEVFS_CAP_REAP_AFTER_DISCONNECT | USBDEVFS_CAP_MMAP |
2242 USBDEVFS_CAP_DROP_PRIVILEGES;
Hans de Goede19181bc2012-07-04 09:18:02 +02002243 if (!ps->dev->bus->no_stop_on_short)
2244 caps |= USBDEVFS_CAP_BULK_CONTINUATION;
Hans de Goede3d97ff62012-07-04 09:18:03 +02002245 if (ps->dev->bus->sg_tablesize)
2246 caps |= USBDEVFS_CAP_BULK_SCATTER_GATHER;
Hans de Goede19181bc2012-07-04 09:18:02 +02002247
2248 if (put_user(caps, (__u32 __user *)arg))
2249 return -EFAULT;
2250
2251 return 0;
2252}
2253
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002254static int proc_disconnect_claim(struct usb_dev_state *ps, void __user *arg)
Hans de Goede0837e7e2012-09-08 20:02:05 +02002255{
2256 struct usbdevfs_disconnect_claim dc;
2257 struct usb_interface *intf;
2258
2259 if (copy_from_user(&dc, arg, sizeof(dc)))
2260 return -EFAULT;
2261
2262 intf = usb_ifnum_to_if(ps->dev, dc.interface);
2263 if (!intf)
2264 return -EINVAL;
2265
2266 if (intf->dev.driver) {
2267 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
2268
Reilly Grantd883f522016-02-21 18:38:01 -03002269 if (ps->privileges_dropped)
2270 return -EACCES;
2271
Hans de Goede0837e7e2012-09-08 20:02:05 +02002272 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_IF_DRIVER) &&
2273 strncmp(dc.driver, intf->dev.driver->name,
2274 sizeof(dc.driver)) != 0)
2275 return -EBUSY;
2276
2277 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_EXCEPT_DRIVER) &&
2278 strncmp(dc.driver, intf->dev.driver->name,
2279 sizeof(dc.driver)) == 0)
2280 return -EBUSY;
2281
2282 dev_dbg(&intf->dev, "disconnect by usbfs\n");
2283 usb_driver_release_interface(driver, intf);
2284 }
2285
2286 return claimintf(ps, dc.interface);
2287}
2288
Linus Torvalds3e75c6d2014-04-01 17:06:09 -07002289static int proc_alloc_streams(struct usb_dev_state *ps, void __user *arg)
Hans de Goedebcf7f6e2013-10-09 17:19:31 +02002290{
2291 unsigned num_streams, num_eps;
2292 struct usb_host_endpoint **eps;
2293 struct usb_interface *intf;
2294 int r;
2295
2296 r = parse_usbdevfs_streams(ps, arg, &num_streams, &num_eps,
2297 &eps, &intf);
2298 if (r)
2299 return r;
2300
2301 destroy_async_on_interface(ps,
2302 intf->altsetting[0].desc.bInterfaceNumber);
2303
2304 r = usb_alloc_streams(intf, eps, num_eps, num_streams, GFP_KERNEL);
2305 kfree(eps);
2306 return r;
2307}
2308
Linus Torvalds3e75c6d2014-04-01 17:06:09 -07002309static int proc_free_streams(struct usb_dev_state *ps, void __user *arg)
Hans de Goedebcf7f6e2013-10-09 17:19:31 +02002310{
2311 unsigned num_eps;
2312 struct usb_host_endpoint **eps;
2313 struct usb_interface *intf;
2314 int r;
2315
2316 r = parse_usbdevfs_streams(ps, arg, NULL, &num_eps, &eps, &intf);
2317 if (r)
2318 return r;
2319
2320 destroy_async_on_interface(ps,
2321 intf->altsetting[0].desc.bInterfaceNumber);
2322
2323 r = usb_free_streams(intf, eps, num_eps, GFP_KERNEL);
2324 kfree(eps);
2325 return r;
2326}
2327
Reilly Grantd883f522016-02-21 18:38:01 -03002328static int proc_drop_privileges(struct usb_dev_state *ps, void __user *arg)
2329{
2330 u32 data;
2331
2332 if (copy_from_user(&data, arg, sizeof(data)))
2333 return -EFAULT;
2334
Masahiro Yamada0f5e1552017-02-27 14:28:52 -08002335 /* This is a one way operation. Once privileges are
Reilly Grantd883f522016-02-21 18:38:01 -03002336 * dropped, you cannot regain them. You may however reissue
2337 * this ioctl to shrink the allowed interfaces mask.
2338 */
2339 ps->interface_allowed_mask &= data;
2340 ps->privileges_dropped = true;
2341
2342 return 0;
2343}
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345/*
2346 * NOTE: All requests here that have interface numbers as parameters
2347 * are assuming that somehow the configuration has been prevented from
2348 * changing. But there's no mechanism to ensure that...
2349 */
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002350static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
2351 void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002353 struct usb_dev_state *ps = file->private_data;
Al Viro496ad9a2013-01-23 17:07:38 -05002354 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 struct usb_device *dev = ps->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 int ret = -ENOTTY;
2357
2358 if (!(file->f_mode & FMODE_WRITE))
2359 return -EPERM;
Oliver Neukum01412a22010-01-13 15:33:43 +01002360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 usb_lock_device(dev);
Alan Stern3f2cee72015-01-29 11:29:13 -05002362
2363 /* Reap operations are allowed even after disconnection */
2364 switch (cmd) {
2365 case USBDEVFS_REAPURB:
2366 snoop(&dev->dev, "%s: REAPURB\n", __func__);
2367 ret = proc_reapurb(ps, p);
2368 goto done;
2369
2370 case USBDEVFS_REAPURBNDELAY:
2371 snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
2372 ret = proc_reapurbnonblock(ps, p);
2373 goto done;
2374
2375#ifdef CONFIG_COMPAT
2376 case USBDEVFS_REAPURB32:
2377 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
2378 ret = proc_reapurb_compat(ps, p);
2379 goto done;
2380
2381 case USBDEVFS_REAPURBNDELAY32:
2382 snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
2383 ret = proc_reapurbnonblock_compat(ps, p);
2384 goto done;
2385#endif
2386 }
2387
Alan Stern349710c2006-07-01 22:05:56 -04002388 if (!connected(ps)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 usb_unlock_device(dev);
2390 return -ENODEV;
2391 }
2392
2393 switch (cmd) {
2394 case USBDEVFS_CONTROL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002395 snoop(&dev->dev, "%s: CONTROL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 ret = proc_control(ps, p);
2397 if (ret >= 0)
Deepa Dinamani078cd822016-09-14 07:48:04 -07002398 inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 break;
2400
2401 case USBDEVFS_BULK:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002402 snoop(&dev->dev, "%s: BULK\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 ret = proc_bulk(ps, p);
2404 if (ret >= 0)
Deepa Dinamani078cd822016-09-14 07:48:04 -07002405 inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 break;
2407
2408 case USBDEVFS_RESETEP:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002409 snoop(&dev->dev, "%s: RESETEP\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 ret = proc_resetep(ps, p);
2411 if (ret >= 0)
Deepa Dinamani078cd822016-09-14 07:48:04 -07002412 inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 break;
2414
2415 case USBDEVFS_RESET:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002416 snoop(&dev->dev, "%s: RESET\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 ret = proc_resetdevice(ps);
2418 break;
2419
2420 case USBDEVFS_CLEAR_HALT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002421 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 ret = proc_clearhalt(ps, p);
2423 if (ret >= 0)
Deepa Dinamani078cd822016-09-14 07:48:04 -07002424 inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 break;
2426
2427 case USBDEVFS_GETDRIVER:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002428 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 ret = proc_getdriver(ps, p);
2430 break;
2431
2432 case USBDEVFS_CONNECTINFO:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002433 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 ret = proc_connectinfo(ps, p);
2435 break;
2436
2437 case USBDEVFS_SETINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002438 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 ret = proc_setintf(ps, p);
2440 break;
2441
2442 case USBDEVFS_SETCONFIGURATION:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002443 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 ret = proc_setconfig(ps, p);
2445 break;
2446
2447 case USBDEVFS_SUBMITURB:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002448 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 ret = proc_submiturb(ps, p);
2450 if (ret >= 0)
Deepa Dinamani078cd822016-09-14 07:48:04 -07002451 inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 break;
2453
2454#ifdef CONFIG_COMPAT
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002455 case USBDEVFS_CONTROL32:
2456 snoop(&dev->dev, "%s: CONTROL32\n", __func__);
2457 ret = proc_control_compat(ps, p);
2458 if (ret >= 0)
Deepa Dinamani078cd822016-09-14 07:48:04 -07002459 inode->i_mtime = current_time(inode);
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002460 break;
2461
2462 case USBDEVFS_BULK32:
2463 snoop(&dev->dev, "%s: BULK32\n", __func__);
2464 ret = proc_bulk_compat(ps, p);
2465 if (ret >= 0)
Deepa Dinamani078cd822016-09-14 07:48:04 -07002466 inode->i_mtime = current_time(inode);
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002467 break;
2468
2469 case USBDEVFS_DISCSIGNAL32:
2470 snoop(&dev->dev, "%s: DISCSIGNAL32\n", __func__);
2471 ret = proc_disconnectsignal_compat(ps, p);
2472 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
2474 case USBDEVFS_SUBMITURB32:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002475 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 ret = proc_submiturb_compat(ps, p);
2477 if (ret >= 0)
Deepa Dinamani078cd822016-09-14 07:48:04 -07002478 inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 break;
2480
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002481 case USBDEVFS_IOCTL32:
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002482 snoop(&dev->dev, "%s: IOCTL32\n", __func__);
Al Viroc714de52006-10-10 22:45:37 +01002483 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002484 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485#endif
2486
2487 case USBDEVFS_DISCARDURB:
Vamsi Krishna Samavedam2f964782017-05-16 14:38:08 +02002488 snoop(&dev->dev, "%s: DISCARDURB %pK\n", __func__, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 ret = proc_unlinkurb(ps, p);
2490 break;
2491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 case USBDEVFS_DISCSIGNAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002493 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 ret = proc_disconnectsignal(ps, p);
2495 break;
2496
2497 case USBDEVFS_CLAIMINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002498 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 ret = proc_claiminterface(ps, p);
2500 break;
2501
2502 case USBDEVFS_RELEASEINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002503 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 ret = proc_releaseinterface(ps, p);
2505 break;
2506
2507 case USBDEVFS_IOCTL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002508 snoop(&dev->dev, "%s: IOCTL\n", __func__);
Pete Zaitcevc36fc882005-10-17 18:15:54 -07002509 ret = proc_ioctl_default(ps, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 break;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04002511
2512 case USBDEVFS_CLAIM_PORT:
2513 snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__);
2514 ret = proc_claim_port(ps, p);
2515 break;
2516
2517 case USBDEVFS_RELEASE_PORT:
2518 snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__);
2519 ret = proc_release_port(ps, p);
2520 break;
Hans de Goede19181bc2012-07-04 09:18:02 +02002521 case USBDEVFS_GET_CAPABILITIES:
2522 ret = proc_get_capabilities(ps, p);
2523 break;
Hans de Goede0837e7e2012-09-08 20:02:05 +02002524 case USBDEVFS_DISCONNECT_CLAIM:
2525 ret = proc_disconnect_claim(ps, p);
2526 break;
Hans de Goedebcf7f6e2013-10-09 17:19:31 +02002527 case USBDEVFS_ALLOC_STREAMS:
2528 ret = proc_alloc_streams(ps, p);
2529 break;
2530 case USBDEVFS_FREE_STREAMS:
2531 ret = proc_free_streams(ps, p);
2532 break;
Reilly Grantd883f522016-02-21 18:38:01 -03002533 case USBDEVFS_DROP_PRIVILEGES:
2534 ret = proc_drop_privileges(ps, p);
2535 break;
Alan Sternc01b2442017-06-05 10:28:01 -04002536 case USBDEVFS_GET_SPEED:
2537 ret = ps->dev->speed;
2538 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 }
Alan Stern3f2cee72015-01-29 11:29:13 -05002540
2541 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 usb_unlock_device(dev);
2543 if (ret >= 0)
Deepa Dinamani078cd822016-09-14 07:48:04 -07002544 inode->i_atime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 return ret;
2546}
2547
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002548static long usbdev_ioctl(struct file *file, unsigned int cmd,
2549 unsigned long arg)
2550{
2551 int ret;
2552
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002553 ret = usbdev_do_ioctl(file, cmd, (void __user *)arg);
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002554
2555 return ret;
2556}
2557
2558#ifdef CONFIG_COMPAT
2559static long usbdev_compat_ioctl(struct file *file, unsigned int cmd,
2560 unsigned long arg)
2561{
2562 int ret;
2563
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002564 ret = usbdev_do_ioctl(file, cmd, compat_ptr(arg));
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002565
2566 return ret;
2567}
2568#endif
2569
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570/* No kernel lock - fine */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002571static unsigned int usbdev_poll(struct file *file,
2572 struct poll_table_struct *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002574 struct usb_dev_state *ps = file->private_data;
Tobias Klauserec17cf12006-09-13 21:38:41 +02002575 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 poll_wait(file, &ps->wait, wait);
2578 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
2579 mask |= POLLOUT | POLLWRNORM;
Alan Stern349710c2006-07-01 22:05:56 -04002580 if (!connected(ps))
Alan Stern5cce4382016-06-10 14:42:55 -04002581 mask |= POLLHUP;
2582 if (list_empty(&ps->list))
2583 mask |= POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 return mask;
2585}
2586
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002587const struct file_operations usbdev_file_operations = {
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002588 .owner = THIS_MODULE,
Al Virob25472f2015-12-05 22:04:48 -05002589 .llseek = no_seek_end_llseek,
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002590 .read = usbdev_read,
2591 .poll = usbdev_poll,
2592 .unlocked_ioctl = usbdev_ioctl,
2593#ifdef CONFIG_COMPAT
2594 .compat_ioctl = usbdev_compat_ioctl,
2595#endif
Steinar H. Gundersonf7d34b42016-02-03 22:58:26 +01002596 .mmap = usbdev_mmap,
Arnd Bergmann637e8a62009-11-14 02:28:05 +01002597 .open = usbdev_open,
2598 .release = usbdev_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599};
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002600
Alan Stern501950d2009-01-13 11:33:42 -05002601static void usbdev_remove(struct usb_device *udev)
Alan Sterncd9f0372008-06-24 14:47:04 -04002602{
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002603 struct usb_dev_state *ps;
Alan Sterncd9f0372008-06-24 14:47:04 -04002604 struct siginfo sinfo;
2605
2606 while (!list_empty(&udev->filelist)) {
Valentina Manea9b6f0c42014-03-10 10:36:40 +02002607 ps = list_entry(udev->filelist.next, struct usb_dev_state, list);
Alan Sterncd9f0372008-06-24 14:47:04 -04002608 destroy_all_async(ps);
2609 wake_up_all(&ps->wait);
2610 list_del_init(&ps->list);
2611 if (ps->discsignr) {
Alan Sternf0c2b682015-02-13 10:54:53 -05002612 memset(&sinfo, 0, sizeof(sinfo));
Alan Sterncd9f0372008-06-24 14:47:04 -04002613 sinfo.si_signo = ps->discsignr;
2614 sinfo.si_errno = EPIPE;
2615 sinfo.si_code = SI_ASYNCIO;
2616 sinfo.si_addr = ps->disccontext;
Serge Hallynd178bc32011-09-26 10:45:18 -05002617 kill_pid_info_as_cred(ps->discsignr, &sinfo,
2618 ps->disc_pid, ps->cred, ps->secid);
Alan Sterncd9f0372008-06-24 14:47:04 -04002619 }
2620 }
2621}
2622
Alan Stern501950d2009-01-13 11:33:42 -05002623static int usbdev_notify(struct notifier_block *self,
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002624 unsigned long action, void *dev)
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002625{
2626 switch (action) {
2627 case USB_DEVICE_ADD:
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002628 break;
2629 case USB_DEVICE_REMOVE:
Alan Stern501950d2009-01-13 11:33:42 -05002630 usbdev_remove(dev);
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002631 break;
2632 }
2633 return NOTIFY_OK;
2634}
2635
2636static struct notifier_block usbdev_nb = {
Chase Metzger00fe52d2015-04-09 21:41:52 -07002637 .notifier_call = usbdev_notify,
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002638};
2639
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -07002640static struct cdev usb_device_cdev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002641
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002642int __init usb_devio_init(void)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002643{
2644 int retval;
2645
Alan Sternfad21bd2005-08-10 15:15:57 -04002646 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08002647 "usb_device");
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002648 if (retval) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -07002649 printk(KERN_ERR "Unable to register minors for usb_device\n");
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002650 goto out;
2651 }
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002652 cdev_init(&usb_device_cdev, &usbdev_file_operations);
Alan Sternfad21bd2005-08-10 15:15:57 -04002653 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002654 if (retval) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -07002655 printk(KERN_ERR "Unable to get usb_device major %d\n",
2656 USB_DEVICE_MAJOR);
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002657 goto error_cdev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002658 }
Alan Stern501950d2009-01-13 11:33:42 -05002659 usb_register_notify(&usbdev_nb);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002660out:
2661 return retval;
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002662
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002663error_cdev:
2664 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
2665 goto out;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002666}
2667
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002668void usb_devio_cleanup(void)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002669{
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07002670 usb_unregister_notify(&usbdev_nb);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002671 cdev_del(&usb_device_cdev);
Alan Sternfad21bd2005-08-10 15:15:57 -04002672 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02002673}