x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / drivers / usb / core / devio.c
blob492977f78fdef00d9c048c8b59d57a9ca4ca9dc9
1 /*****************************************************************************/
3 /*
4 * devio.c -- User space communication with USB devices.
6 * Copyright (C) 1999-2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * This file implements the usbfs/x/y files, where
23 * x is the bus number and y the device number.
25 * It allows user space programs/"drivers" to communicate directly
26 * with USB devices without intervening kernel driver.
28 * Revision history
29 * 22.12.1999 0.1 Initial release (split from proc_usb.c)
30 * 04.01.2000 0.2 Turned into its own filesystem
31 * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery
32 * (CAN-2005-3055)
35 /*****************************************************************************/
37 #include <linux/fs.h>
38 #include <linux/mm.h>
39 #include <linux/sched/signal.h>
40 #include <linux/slab.h>
41 #include <linux/signal.h>
42 #include <linux/poll.h>
43 #include <linux/module.h>
44 #include <linux/string.h>
45 #include <linux/usb.h>
46 #include <linux/usbdevice_fs.h>
47 #include <linux/usb/hcd.h> /* for usbcore internals */
48 #include <linux/cdev.h>
49 #include <linux/notifier.h>
50 #include <linux/security.h>
51 #include <linux/user_namespace.h>
52 #include <linux/scatterlist.h>
53 #include <linux/uaccess.h>
54 #include <linux/dma-mapping.h>
55 #include <asm/byteorder.h>
56 #include <linux/moduleparam.h>
58 #include "usb.h"
60 #define USB_MAXBUS 64
61 #define USB_DEVICE_MAX (USB_MAXBUS * 128)
62 #define USB_SG_SIZE 16384 /* split-size for large txs */
64 /* Mutual exclusion for removal, open, and release */
65 DEFINE_MUTEX(usbfs_mutex);
67 struct usb_dev_state {
68 struct list_head list; /* state list */
69 struct usb_device *dev;
70 struct file *file;
71 spinlock_t lock; /* protects the async urb lists */
72 struct list_head async_pending;
73 struct list_head async_completed;
74 struct list_head memory_list;
75 wait_queue_head_t wait; /* wake up if a request completed */
76 unsigned int discsignr;
77 struct pid *disc_pid;
78 const struct cred *cred;
79 void __user *disccontext;
80 unsigned long ifclaimed;
81 u32 secid;
82 u32 disabled_bulk_eps;
83 bool privileges_dropped;
84 unsigned long interface_allowed_mask;
87 struct usb_memory {
88 struct list_head memlist;
89 int vma_use_count;
90 int urb_use_count;
91 u32 size;
92 void *mem;
93 dma_addr_t dma_handle;
94 unsigned long vm_start;
95 struct usb_dev_state *ps;
98 struct async {
99 struct list_head asynclist;
100 struct usb_dev_state *ps;
101 struct pid *pid;
102 const struct cred *cred;
103 unsigned int signr;
104 unsigned int ifnum;
105 void __user *userbuffer;
106 void __user *userurb;
107 struct urb *urb;
108 struct usb_memory *usbm;
109 unsigned int mem_usage;
110 int status;
111 u32 secid;
112 u8 bulk_addr;
113 u8 bulk_status;
116 static bool usbfs_snoop;
117 module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
118 MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
120 static unsigned usbfs_snoop_max = 65536;
121 module_param(usbfs_snoop_max, uint, S_IRUGO | S_IWUSR);
122 MODULE_PARM_DESC(usbfs_snoop_max,
123 "maximum number of bytes to print while snooping");
125 #define snoop(dev, format, arg...) \
126 do { \
127 if (usbfs_snoop) \
128 dev_info(dev, format, ## arg); \
129 } while (0)
131 enum snoop_when {
132 SUBMIT, COMPLETE
135 #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
137 /* Limit on the total amount of memory we can allocate for transfers */
138 static u32 usbfs_memory_mb = 16;
139 module_param(usbfs_memory_mb, uint, 0644);
140 MODULE_PARM_DESC(usbfs_memory_mb,
141 "maximum MB allowed for usbfs buffers (0 = no limit)");
143 /* Hard limit, necessary to avoid arithmetic overflow */
144 #define USBFS_XFER_MAX (UINT_MAX / 2 - 1000000)
146 static atomic64_t usbfs_memory_usage; /* Total memory currently allocated */
148 /* Check whether it's okay to allocate more memory for a transfer */
149 static int usbfs_increase_memory_usage(u64 amount)
151 u64 lim;
153 lim = ACCESS_ONCE(usbfs_memory_mb);
154 lim <<= 20;
156 atomic64_add(amount, &usbfs_memory_usage);
158 if (lim > 0 && atomic64_read(&usbfs_memory_usage) > lim) {
159 atomic64_sub(amount, &usbfs_memory_usage);
160 return -ENOMEM;
163 return 0;
166 /* Memory for a transfer is being deallocated */
167 static void usbfs_decrease_memory_usage(u64 amount)
169 atomic64_sub(amount, &usbfs_memory_usage);
172 static int connected(struct usb_dev_state *ps)
174 return (!list_empty(&ps->list) &&
175 ps->dev->state != USB_STATE_NOTATTACHED);
178 static void dec_usb_memory_use_count(struct usb_memory *usbm, int *count)
180 struct usb_dev_state *ps = usbm->ps;
181 unsigned long flags;
183 spin_lock_irqsave(&ps->lock, flags);
184 --*count;
185 if (usbm->urb_use_count == 0 && usbm->vma_use_count == 0) {
186 list_del(&usbm->memlist);
187 spin_unlock_irqrestore(&ps->lock, flags);
189 usb_free_coherent(ps->dev, usbm->size, usbm->mem,
190 usbm->dma_handle);
191 usbfs_decrease_memory_usage(
192 usbm->size + sizeof(struct usb_memory));
193 kfree(usbm);
194 } else {
195 spin_unlock_irqrestore(&ps->lock, flags);
199 static void usbdev_vm_open(struct vm_area_struct *vma)
201 struct usb_memory *usbm = vma->vm_private_data;
202 unsigned long flags;
204 spin_lock_irqsave(&usbm->ps->lock, flags);
205 ++usbm->vma_use_count;
206 spin_unlock_irqrestore(&usbm->ps->lock, flags);
209 static void usbdev_vm_close(struct vm_area_struct *vma)
211 struct usb_memory *usbm = vma->vm_private_data;
213 dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
216 static const struct vm_operations_struct usbdev_vm_ops = {
217 .open = usbdev_vm_open,
218 .close = usbdev_vm_close
221 static int usbdev_mmap(struct file *file, struct vm_area_struct *vma)
223 struct usb_memory *usbm = NULL;
224 struct usb_dev_state *ps = file->private_data;
225 size_t size = vma->vm_end - vma->vm_start;
226 void *mem;
227 unsigned long flags;
228 dma_addr_t dma_handle;
229 int ret;
231 ret = usbfs_increase_memory_usage(size + sizeof(struct usb_memory));
232 if (ret)
233 goto error;
235 usbm = kzalloc(sizeof(struct usb_memory), GFP_KERNEL);
236 if (!usbm) {
237 ret = -ENOMEM;
238 goto error_decrease_mem;
241 mem = usb_alloc_coherent(ps->dev, size, GFP_USER | __GFP_NOWARN,
242 &dma_handle);
243 if (!mem) {
244 ret = -ENOMEM;
245 goto error_free_usbm;
248 memset(mem, 0, size);
250 usbm->mem = mem;
251 usbm->dma_handle = dma_handle;
252 usbm->size = size;
253 usbm->ps = ps;
254 usbm->vm_start = vma->vm_start;
255 usbm->vma_use_count = 1;
256 INIT_LIST_HEAD(&usbm->memlist);
258 if (remap_pfn_range(vma, vma->vm_start,
259 virt_to_phys(usbm->mem) >> PAGE_SHIFT,
260 size, vma->vm_page_prot) < 0) {
261 dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
262 return -EAGAIN;
265 vma->vm_flags |= VM_IO;
266 vma->vm_flags |= (VM_DONTEXPAND | VM_DONTDUMP);
267 vma->vm_ops = &usbdev_vm_ops;
268 vma->vm_private_data = usbm;
270 spin_lock_irqsave(&ps->lock, flags);
271 list_add_tail(&usbm->memlist, &ps->memory_list);
272 spin_unlock_irqrestore(&ps->lock, flags);
274 return 0;
276 error_free_usbm:
277 kfree(usbm);
278 error_decrease_mem:
279 usbfs_decrease_memory_usage(size + sizeof(struct usb_memory));
280 error:
281 return ret;
284 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
285 loff_t *ppos)
287 struct usb_dev_state *ps = file->private_data;
288 struct usb_device *dev = ps->dev;
289 ssize_t ret = 0;
290 unsigned len;
291 loff_t pos;
292 int i;
294 pos = *ppos;
295 usb_lock_device(dev);
296 if (!connected(ps)) {
297 ret = -ENODEV;
298 goto err;
299 } else if (pos < 0) {
300 ret = -EINVAL;
301 goto err;
304 if (pos < sizeof(struct usb_device_descriptor)) {
305 /* 18 bytes - fits on the stack */
306 struct usb_device_descriptor temp_desc;
308 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
309 le16_to_cpus(&temp_desc.bcdUSB);
310 le16_to_cpus(&temp_desc.idVendor);
311 le16_to_cpus(&temp_desc.idProduct);
312 le16_to_cpus(&temp_desc.bcdDevice);
314 len = sizeof(struct usb_device_descriptor) - pos;
315 if (len > nbytes)
316 len = nbytes;
317 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
318 ret = -EFAULT;
319 goto err;
322 *ppos += len;
323 buf += len;
324 nbytes -= len;
325 ret += len;
328 pos = sizeof(struct usb_device_descriptor);
329 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
330 struct usb_config_descriptor *config =
331 (struct usb_config_descriptor *)dev->rawdescriptors[i];
332 unsigned int length = le16_to_cpu(config->wTotalLength);
334 if (*ppos < pos + length) {
336 /* The descriptor may claim to be longer than it
337 * really is. Here is the actual allocated length. */
338 unsigned alloclen =
339 le16_to_cpu(dev->config[i].desc.wTotalLength);
341 len = length - (*ppos - pos);
342 if (len > nbytes)
343 len = nbytes;
345 /* Simply don't write (skip over) unallocated parts */
346 if (alloclen > (*ppos - pos)) {
347 alloclen -= (*ppos - pos);
348 if (copy_to_user(buf,
349 dev->rawdescriptors[i] + (*ppos - pos),
350 min(len, alloclen))) {
351 ret = -EFAULT;
352 goto err;
356 *ppos += len;
357 buf += len;
358 nbytes -= len;
359 ret += len;
362 pos += length;
365 err:
366 usb_unlock_device(dev);
367 return ret;
371 * async list handling
374 static struct async *alloc_async(unsigned int numisoframes)
376 struct async *as;
378 as = kzalloc(sizeof(struct async), GFP_KERNEL);
379 if (!as)
380 return NULL;
381 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
382 if (!as->urb) {
383 kfree(as);
384 return NULL;
386 return as;
389 static void free_async(struct async *as)
391 int i;
393 put_pid(as->pid);
394 if (as->cred)
395 put_cred(as->cred);
396 for (i = 0; i < as->urb->num_sgs; i++) {
397 if (sg_page(&as->urb->sg[i]))
398 kfree(sg_virt(&as->urb->sg[i]));
401 kfree(as->urb->sg);
402 if (as->usbm == NULL)
403 kfree(as->urb->transfer_buffer);
404 else
405 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count);
407 kfree(as->urb->setup_packet);
408 usb_free_urb(as->urb);
409 usbfs_decrease_memory_usage(as->mem_usage);
410 kfree(as);
413 static void async_newpending(struct async *as)
415 struct usb_dev_state *ps = as->ps;
416 unsigned long flags;
418 spin_lock_irqsave(&ps->lock, flags);
419 list_add_tail(&as->asynclist, &ps->async_pending);
420 spin_unlock_irqrestore(&ps->lock, flags);
423 static void async_removepending(struct async *as)
425 struct usb_dev_state *ps = as->ps;
426 unsigned long flags;
428 spin_lock_irqsave(&ps->lock, flags);
429 list_del_init(&as->asynclist);
430 spin_unlock_irqrestore(&ps->lock, flags);
433 static struct async *async_getcompleted(struct usb_dev_state *ps)
435 unsigned long flags;
436 struct async *as = NULL;
438 spin_lock_irqsave(&ps->lock, flags);
439 if (!list_empty(&ps->async_completed)) {
440 as = list_entry(ps->async_completed.next, struct async,
441 asynclist);
442 list_del_init(&as->asynclist);
444 spin_unlock_irqrestore(&ps->lock, flags);
445 return as;
448 static struct async *async_getpending(struct usb_dev_state *ps,
449 void __user *userurb)
451 struct async *as;
453 list_for_each_entry(as, &ps->async_pending, asynclist)
454 if (as->userurb == userurb) {
455 list_del_init(&as->asynclist);
456 return as;
459 return NULL;
462 static void snoop_urb(struct usb_device *udev,
463 void __user *userurb, int pipe, unsigned length,
464 int timeout_or_status, enum snoop_when when,
465 unsigned char *data, unsigned data_len)
467 static const char *types[] = {"isoc", "int", "ctrl", "bulk"};
468 static const char *dirs[] = {"out", "in"};
469 int ep;
470 const char *t, *d;
472 if (!usbfs_snoop)
473 return;
475 ep = usb_pipeendpoint(pipe);
476 t = types[usb_pipetype(pipe)];
477 d = dirs[!!usb_pipein(pipe)];
479 if (userurb) { /* Async */
480 if (when == SUBMIT)
481 dev_info(&udev->dev, "userurb %pK, ep%d %s-%s, "
482 "length %u\n",
483 userurb, ep, t, d, length);
484 else
485 dev_info(&udev->dev, "userurb %pK, ep%d %s-%s, "
486 "actual_length %u status %d\n",
487 userurb, ep, t, d, length,
488 timeout_or_status);
489 } else {
490 if (when == SUBMIT)
491 dev_info(&udev->dev, "ep%d %s-%s, length %u, "
492 "timeout %d\n",
493 ep, t, d, length, timeout_or_status);
494 else
495 dev_info(&udev->dev, "ep%d %s-%s, actual_length %u, "
496 "status %d\n",
497 ep, t, d, length, timeout_or_status);
500 data_len = min(data_len, usbfs_snoop_max);
501 if (data && data_len > 0) {
502 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
503 data, data_len, 1);
507 static void snoop_urb_data(struct urb *urb, unsigned len)
509 int i, size;
511 len = min(len, usbfs_snoop_max);
512 if (!usbfs_snoop || len == 0)
513 return;
515 if (urb->num_sgs == 0) {
516 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
517 urb->transfer_buffer, len, 1);
518 return;
521 for (i = 0; i < urb->num_sgs && len; i++) {
522 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
523 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
524 sg_virt(&urb->sg[i]), size, 1);
525 len -= size;
529 static int copy_urb_data_to_user(u8 __user *userbuffer, struct urb *urb)
531 unsigned i, len, size;
533 if (urb->number_of_packets > 0) /* Isochronous */
534 len = urb->transfer_buffer_length;
535 else /* Non-Isoc */
536 len = urb->actual_length;
538 if (urb->num_sgs == 0) {
539 if (copy_to_user(userbuffer, urb->transfer_buffer, len))
540 return -EFAULT;
541 return 0;
544 for (i = 0; i < urb->num_sgs && len; i++) {
545 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
546 if (copy_to_user(userbuffer, sg_virt(&urb->sg[i]), size))
547 return -EFAULT;
548 userbuffer += size;
549 len -= size;
552 return 0;
555 #define AS_CONTINUATION 1
556 #define AS_UNLINK 2
558 static void cancel_bulk_urbs(struct usb_dev_state *ps, unsigned bulk_addr)
559 __releases(ps->lock)
560 __acquires(ps->lock)
562 struct urb *urb;
563 struct async *as;
565 /* Mark all the pending URBs that match bulk_addr, up to but not
566 * including the first one without AS_CONTINUATION. If such an
567 * URB is encountered then a new transfer has already started so
568 * the endpoint doesn't need to be disabled; otherwise it does.
570 list_for_each_entry(as, &ps->async_pending, asynclist) {
571 if (as->bulk_addr == bulk_addr) {
572 if (as->bulk_status != AS_CONTINUATION)
573 goto rescan;
574 as->bulk_status = AS_UNLINK;
575 as->bulk_addr = 0;
578 ps->disabled_bulk_eps |= (1 << bulk_addr);
580 /* Now carefully unlink all the marked pending URBs */
581 rescan:
582 list_for_each_entry(as, &ps->async_pending, asynclist) {
583 if (as->bulk_status == AS_UNLINK) {
584 as->bulk_status = 0; /* Only once */
585 urb = as->urb;
586 usb_get_urb(urb);
587 spin_unlock(&ps->lock); /* Allow completions */
588 usb_unlink_urb(urb);
589 usb_put_urb(urb);
590 spin_lock(&ps->lock);
591 goto rescan;
596 static void async_completed(struct urb *urb)
598 struct async *as = urb->context;
599 struct usb_dev_state *ps = as->ps;
600 struct siginfo sinfo;
601 struct pid *pid = NULL;
602 u32 secid = 0;
603 const struct cred *cred = NULL;
604 int signr;
606 spin_lock(&ps->lock);
607 list_move_tail(&as->asynclist, &ps->async_completed);
608 as->status = urb->status;
609 signr = as->signr;
610 if (signr) {
611 memset(&sinfo, 0, sizeof(sinfo));
612 sinfo.si_signo = as->signr;
613 sinfo.si_errno = as->status;
614 sinfo.si_code = SI_ASYNCIO;
615 sinfo.si_addr = as->userurb;
616 pid = get_pid(as->pid);
617 cred = get_cred(as->cred);
618 secid = as->secid;
620 snoop(&urb->dev->dev, "urb complete\n");
621 snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length,
622 as->status, COMPLETE, NULL, 0);
623 if ((urb->transfer_flags & URB_DIR_MASK) == URB_DIR_IN)
624 snoop_urb_data(urb, urb->actual_length);
626 if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET &&
627 as->status != -ENOENT)
628 cancel_bulk_urbs(ps, as->bulk_addr);
630 wake_up(&ps->wait);
631 spin_unlock(&ps->lock);
633 if (signr) {
634 kill_pid_info_as_cred(sinfo.si_signo, &sinfo, pid, cred, secid);
635 put_pid(pid);
636 put_cred(cred);
640 static void destroy_async(struct usb_dev_state *ps, struct list_head *list)
642 struct urb *urb;
643 struct async *as;
644 unsigned long flags;
646 spin_lock_irqsave(&ps->lock, flags);
647 while (!list_empty(list)) {
648 as = list_entry(list->next, struct async, asynclist);
649 list_del_init(&as->asynclist);
650 urb = as->urb;
651 usb_get_urb(urb);
653 /* drop the spinlock so the completion handler can run */
654 spin_unlock_irqrestore(&ps->lock, flags);
655 usb_kill_urb(urb);
656 usb_put_urb(urb);
657 spin_lock_irqsave(&ps->lock, flags);
659 spin_unlock_irqrestore(&ps->lock, flags);
662 static void destroy_async_on_interface(struct usb_dev_state *ps,
663 unsigned int ifnum)
665 struct list_head *p, *q, hitlist;
666 unsigned long flags;
668 INIT_LIST_HEAD(&hitlist);
669 spin_lock_irqsave(&ps->lock, flags);
670 list_for_each_safe(p, q, &ps->async_pending)
671 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
672 list_move_tail(p, &hitlist);
673 spin_unlock_irqrestore(&ps->lock, flags);
674 destroy_async(ps, &hitlist);
677 static void destroy_all_async(struct usb_dev_state *ps)
679 destroy_async(ps, &ps->async_pending);
683 * interface claims are made only at the request of user level code,
684 * which can also release them (explicitly or by closing files).
685 * they're also undone when devices disconnect.
688 static int driver_probe(struct usb_interface *intf,
689 const struct usb_device_id *id)
691 return -ENODEV;
694 static void driver_disconnect(struct usb_interface *intf)
696 struct usb_dev_state *ps = usb_get_intfdata(intf);
697 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
699 if (!ps)
700 return;
702 /* NOTE: this relies on usbcore having canceled and completed
703 * all pending I/O requests; 2.6 does that.
706 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
707 clear_bit(ifnum, &ps->ifclaimed);
708 else
709 dev_warn(&intf->dev, "interface number %u out of range\n",
710 ifnum);
712 usb_set_intfdata(intf, NULL);
714 /* force async requests to complete */
715 destroy_async_on_interface(ps, ifnum);
718 /* The following routines are merely placeholders. There is no way
719 * to inform a user task about suspend or resumes.
721 static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
723 return 0;
726 static int driver_resume(struct usb_interface *intf)
728 return 0;
731 struct usb_driver usbfs_driver = {
732 .name = "usbfs",
733 .probe = driver_probe,
734 .disconnect = driver_disconnect,
735 .suspend = driver_suspend,
736 .resume = driver_resume,
739 static int claimintf(struct usb_dev_state *ps, unsigned int ifnum)
741 struct usb_device *dev = ps->dev;
742 struct usb_interface *intf;
743 int err;
745 if (ifnum >= 8*sizeof(ps->ifclaimed))
746 return -EINVAL;
747 /* already claimed */
748 if (test_bit(ifnum, &ps->ifclaimed))
749 return 0;
751 if (ps->privileges_dropped &&
752 !test_bit(ifnum, &ps->interface_allowed_mask))
753 return -EACCES;
755 intf = usb_ifnum_to_if(dev, ifnum);
756 if (!intf)
757 err = -ENOENT;
758 else
759 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
760 if (err == 0)
761 set_bit(ifnum, &ps->ifclaimed);
762 return err;
765 static int releaseintf(struct usb_dev_state *ps, unsigned int ifnum)
767 struct usb_device *dev;
768 struct usb_interface *intf;
769 int err;
771 err = -EINVAL;
772 if (ifnum >= 8*sizeof(ps->ifclaimed))
773 return err;
774 dev = ps->dev;
775 intf = usb_ifnum_to_if(dev, ifnum);
776 if (!intf)
777 err = -ENOENT;
778 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
779 usb_driver_release_interface(&usbfs_driver, intf);
780 err = 0;
782 return err;
785 static int checkintf(struct usb_dev_state *ps, unsigned int ifnum)
787 if (ps->dev->state != USB_STATE_CONFIGURED)
788 return -EHOSTUNREACH;
789 if (ifnum >= 8*sizeof(ps->ifclaimed))
790 return -EINVAL;
791 if (test_bit(ifnum, &ps->ifclaimed))
792 return 0;
793 /* if not yet claimed, claim it for the driver */
794 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
795 "interface %u before use\n", task_pid_nr(current),
796 current->comm, ifnum);
797 return claimintf(ps, ifnum);
800 static int findintfep(struct usb_device *dev, unsigned int ep)
802 unsigned int i, j, e;
803 struct usb_interface *intf;
804 struct usb_host_interface *alts;
805 struct usb_endpoint_descriptor *endpt;
807 if (ep & ~(USB_DIR_IN|0xf))
808 return -EINVAL;
809 if (!dev->actconfig)
810 return -ESRCH;
811 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
812 intf = dev->actconfig->interface[i];
813 for (j = 0; j < intf->num_altsetting; j++) {
814 alts = &intf->altsetting[j];
815 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
816 endpt = &alts->endpoint[e].desc;
817 if (endpt->bEndpointAddress == ep)
818 return alts->desc.bInterfaceNumber;
822 return -ENOENT;
825 static int check_ctrlrecip(struct usb_dev_state *ps, unsigned int requesttype,
826 unsigned int request, unsigned int index)
828 int ret = 0;
829 struct usb_host_interface *alt_setting;
831 if (ps->dev->state != USB_STATE_UNAUTHENTICATED
832 && ps->dev->state != USB_STATE_ADDRESS
833 && ps->dev->state != USB_STATE_CONFIGURED)
834 return -EHOSTUNREACH;
835 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
836 return 0;
839 * check for the special corner case 'get_device_id' in the printer
840 * class specification, which we always want to allow as it is used
841 * to query things like ink level, etc.
843 if (requesttype == 0xa1 && request == 0) {
844 alt_setting = usb_find_alt_setting(ps->dev->actconfig,
845 index >> 8, index & 0xff);
846 if (alt_setting
847 && alt_setting->desc.bInterfaceClass == USB_CLASS_PRINTER)
848 return 0;
851 index &= 0xff;
852 switch (requesttype & USB_RECIP_MASK) {
853 case USB_RECIP_ENDPOINT:
854 if ((index & ~USB_DIR_IN) == 0)
855 return 0;
856 ret = findintfep(ps->dev, index);
857 if (ret < 0) {
859 * Some not fully compliant Win apps seem to get
860 * index wrong and have the endpoint number here
861 * rather than the endpoint address (with the
862 * correct direction). Win does let this through,
863 * so we'll not reject it here but leave it to
864 * the device to not break KVM. But we warn.
866 ret = findintfep(ps->dev, index ^ 0x80);
867 if (ret >= 0)
868 dev_info(&ps->dev->dev,
869 "%s: process %i (%s) requesting ep %02x but needs %02x\n",
870 __func__, task_pid_nr(current),
871 current->comm, index, index ^ 0x80);
873 if (ret >= 0)
874 ret = checkintf(ps, ret);
875 break;
877 case USB_RECIP_INTERFACE:
878 ret = checkintf(ps, index);
879 break;
881 return ret;
884 static struct usb_host_endpoint *ep_to_host_endpoint(struct usb_device *dev,
885 unsigned char ep)
887 if (ep & USB_ENDPOINT_DIR_MASK)
888 return dev->ep_in[ep & USB_ENDPOINT_NUMBER_MASK];
889 else
890 return dev->ep_out[ep & USB_ENDPOINT_NUMBER_MASK];
893 static int parse_usbdevfs_streams(struct usb_dev_state *ps,
894 struct usbdevfs_streams __user *streams,
895 unsigned int *num_streams_ret,
896 unsigned int *num_eps_ret,
897 struct usb_host_endpoint ***eps_ret,
898 struct usb_interface **intf_ret)
900 unsigned int i, num_streams, num_eps;
901 struct usb_host_endpoint **eps;
902 struct usb_interface *intf = NULL;
903 unsigned char ep;
904 int ifnum, ret;
906 if (get_user(num_streams, &streams->num_streams) ||
907 get_user(num_eps, &streams->num_eps))
908 return -EFAULT;
910 if (num_eps < 1 || num_eps > USB_MAXENDPOINTS)
911 return -EINVAL;
913 /* The XHCI controller allows max 2 ^ 16 streams */
914 if (num_streams_ret && (num_streams < 2 || num_streams > 65536))
915 return -EINVAL;
917 eps = kmalloc(num_eps * sizeof(*eps), GFP_KERNEL);
918 if (!eps)
919 return -ENOMEM;
921 for (i = 0; i < num_eps; i++) {
922 if (get_user(ep, &streams->eps[i])) {
923 ret = -EFAULT;
924 goto error;
926 eps[i] = ep_to_host_endpoint(ps->dev, ep);
927 if (!eps[i]) {
928 ret = -EINVAL;
929 goto error;
932 /* usb_alloc/free_streams operate on an usb_interface */
933 ifnum = findintfep(ps->dev, ep);
934 if (ifnum < 0) {
935 ret = ifnum;
936 goto error;
939 if (i == 0) {
940 ret = checkintf(ps, ifnum);
941 if (ret < 0)
942 goto error;
943 intf = usb_ifnum_to_if(ps->dev, ifnum);
944 } else {
945 /* Verify all eps belong to the same interface */
946 if (ifnum != intf->altsetting->desc.bInterfaceNumber) {
947 ret = -EINVAL;
948 goto error;
953 if (num_streams_ret)
954 *num_streams_ret = num_streams;
955 *num_eps_ret = num_eps;
956 *eps_ret = eps;
957 *intf_ret = intf;
959 return 0;
961 error:
962 kfree(eps);
963 return ret;
966 static int match_devt(struct device *dev, void *data)
968 return dev->devt == (dev_t) (unsigned long) data;
971 static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
973 struct device *dev;
975 dev = bus_find_device(&usb_bus_type, NULL,
976 (void *) (unsigned long) devt, match_devt);
977 if (!dev)
978 return NULL;
979 return to_usb_device(dev);
983 * file operations
985 static int usbdev_open(struct inode *inode, struct file *file)
987 struct usb_device *dev = NULL;
988 struct usb_dev_state *ps;
989 int ret;
991 ret = -ENOMEM;
992 ps = kzalloc(sizeof(struct usb_dev_state), GFP_KERNEL);
993 if (!ps)
994 goto out_free_ps;
996 ret = -ENODEV;
998 /* Protect against simultaneous removal or release */
999 mutex_lock(&usbfs_mutex);
1001 /* usbdev device-node */
1002 if (imajor(inode) == USB_DEVICE_MAJOR)
1003 dev = usbdev_lookup_by_devt(inode->i_rdev);
1005 mutex_unlock(&usbfs_mutex);
1007 if (!dev)
1008 goto out_free_ps;
1010 usb_lock_device(dev);
1011 if (dev->state == USB_STATE_NOTATTACHED)
1012 goto out_unlock_device;
1014 ret = usb_autoresume_device(dev);
1015 if (ret)
1016 goto out_unlock_device;
1018 ps->dev = dev;
1019 ps->file = file;
1020 ps->interface_allowed_mask = 0xFFFFFFFF; /* 32 bits */
1021 spin_lock_init(&ps->lock);
1022 INIT_LIST_HEAD(&ps->list);
1023 INIT_LIST_HEAD(&ps->async_pending);
1024 INIT_LIST_HEAD(&ps->async_completed);
1025 INIT_LIST_HEAD(&ps->memory_list);
1026 init_waitqueue_head(&ps->wait);
1027 ps->disc_pid = get_pid(task_pid(current));
1028 ps->cred = get_current_cred();
1029 security_task_getsecid(current, &ps->secid);
1030 smp_wmb();
1031 list_add_tail(&ps->list, &dev->filelist);
1032 file->private_data = ps;
1033 usb_unlock_device(dev);
1034 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
1035 current->comm);
1036 return ret;
1038 out_unlock_device:
1039 usb_unlock_device(dev);
1040 usb_put_dev(dev);
1041 out_free_ps:
1042 kfree(ps);
1043 return ret;
1046 static int usbdev_release(struct inode *inode, struct file *file)
1048 struct usb_dev_state *ps = file->private_data;
1049 struct usb_device *dev = ps->dev;
1050 unsigned int ifnum;
1051 struct async *as;
1053 usb_lock_device(dev);
1054 usb_hub_release_all_ports(dev, ps);
1056 list_del_init(&ps->list);
1058 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
1059 ifnum++) {
1060 if (test_bit(ifnum, &ps->ifclaimed))
1061 releaseintf(ps, ifnum);
1063 destroy_all_async(ps);
1064 usb_autosuspend_device(dev);
1065 usb_unlock_device(dev);
1066 usb_put_dev(dev);
1067 put_pid(ps->disc_pid);
1068 put_cred(ps->cred);
1070 as = async_getcompleted(ps);
1071 while (as) {
1072 free_async(as);
1073 as = async_getcompleted(ps);
1076 kfree(ps);
1077 return 0;
1080 static int proc_control(struct usb_dev_state *ps, void __user *arg)
1082 struct usb_device *dev = ps->dev;
1083 struct usbdevfs_ctrltransfer ctrl;
1084 unsigned int tmo;
1085 unsigned char *tbuf;
1086 unsigned wLength;
1087 int i, pipe, ret;
1089 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1090 return -EFAULT;
1091 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.bRequest,
1092 ctrl.wIndex);
1093 if (ret)
1094 return ret;
1095 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
1096 if (wLength > PAGE_SIZE)
1097 return -EINVAL;
1098 ret = usbfs_increase_memory_usage(PAGE_SIZE + sizeof(struct urb) +
1099 sizeof(struct usb_ctrlrequest));
1100 if (ret)
1101 return ret;
1102 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
1103 if (!tbuf) {
1104 ret = -ENOMEM;
1105 goto done;
1107 tmo = ctrl.timeout;
1108 snoop(&dev->dev, "control urb: bRequestType=%02x "
1109 "bRequest=%02x wValue=%04x "
1110 "wIndex=%04x wLength=%04x\n",
1111 ctrl.bRequestType, ctrl.bRequest, ctrl.wValue,
1112 ctrl.wIndex, ctrl.wLength);
1113 if (ctrl.bRequestType & 0x80) {
1114 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
1115 ctrl.wLength)) {
1116 ret = -EINVAL;
1117 goto done;
1119 pipe = usb_rcvctrlpipe(dev, 0);
1120 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT, NULL, 0);
1122 usb_unlock_device(dev);
1123 i = usb_control_msg(dev, pipe, ctrl.bRequest,
1124 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
1125 tbuf, ctrl.wLength, tmo);
1126 usb_lock_device(dev);
1127 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE,
1128 tbuf, max(i, 0));
1129 if ((i > 0) && ctrl.wLength) {
1130 if (copy_to_user(ctrl.data, tbuf, i)) {
1131 ret = -EFAULT;
1132 goto done;
1135 } else {
1136 if (ctrl.wLength) {
1137 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
1138 ret = -EFAULT;
1139 goto done;
1142 pipe = usb_sndctrlpipe(dev, 0);
1143 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT,
1144 tbuf, ctrl.wLength);
1146 usb_unlock_device(dev);
1147 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
1148 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
1149 tbuf, ctrl.wLength, tmo);
1150 usb_lock_device(dev);
1151 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE, NULL, 0);
1153 if (i < 0 && i != -EPIPE) {
1154 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
1155 "failed cmd %s rqt %u rq %u len %u ret %d\n",
1156 current->comm, ctrl.bRequestType, ctrl.bRequest,
1157 ctrl.wLength, i);
1159 ret = i;
1160 done:
1161 free_page((unsigned long) tbuf);
1162 usbfs_decrease_memory_usage(PAGE_SIZE + sizeof(struct urb) +
1163 sizeof(struct usb_ctrlrequest));
1164 return ret;
1167 static int proc_bulk(struct usb_dev_state *ps, void __user *arg)
1169 struct usb_device *dev = ps->dev;
1170 struct usbdevfs_bulktransfer bulk;
1171 unsigned int tmo, len1, pipe;
1172 int len2;
1173 unsigned char *tbuf;
1174 int i, ret;
1176 if (copy_from_user(&bulk, arg, sizeof(bulk)))
1177 return -EFAULT;
1178 ret = findintfep(ps->dev, bulk.ep);
1179 if (ret < 0)
1180 return ret;
1181 ret = checkintf(ps, ret);
1182 if (ret)
1183 return ret;
1184 if (bulk.ep & USB_DIR_IN)
1185 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
1186 else
1187 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
1188 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
1189 return -EINVAL;
1190 len1 = bulk.len;
1191 if (len1 >= (INT_MAX - sizeof(struct urb)))
1192 return -EINVAL;
1193 ret = usbfs_increase_memory_usage(len1 + sizeof(struct urb));
1194 if (ret)
1195 return ret;
1196 tbuf = kmalloc(len1, GFP_KERNEL);
1197 if (!tbuf) {
1198 ret = -ENOMEM;
1199 goto done;
1201 tmo = bulk.timeout;
1202 if (bulk.ep & 0x80) {
1203 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
1204 ret = -EINVAL;
1205 goto done;
1207 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, NULL, 0);
1209 usb_unlock_device(dev);
1210 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
1211 usb_lock_device(dev);
1212 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, tbuf, len2);
1214 if (!i && len2) {
1215 if (copy_to_user(bulk.data, tbuf, len2)) {
1216 ret = -EFAULT;
1217 goto done;
1220 } else {
1221 if (len1) {
1222 if (copy_from_user(tbuf, bulk.data, len1)) {
1223 ret = -EFAULT;
1224 goto done;
1227 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, tbuf, len1);
1229 usb_unlock_device(dev);
1230 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
1231 usb_lock_device(dev);
1232 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, NULL, 0);
1234 ret = (i < 0 ? i : len2);
1235 done:
1236 kfree(tbuf);
1237 usbfs_decrease_memory_usage(len1 + sizeof(struct urb));
1238 return ret;
1241 static void check_reset_of_active_ep(struct usb_device *udev,
1242 unsigned int epnum, char *ioctl_name)
1244 struct usb_host_endpoint **eps;
1245 struct usb_host_endpoint *ep;
1247 eps = (epnum & USB_DIR_IN) ? udev->ep_in : udev->ep_out;
1248 ep = eps[epnum & 0x0f];
1249 if (ep && !list_empty(&ep->urb_list))
1250 dev_warn(&udev->dev, "Process %d (%s) called USBDEVFS_%s for active endpoint 0x%02x\n",
1251 task_pid_nr(current), current->comm,
1252 ioctl_name, epnum);
1255 static int proc_resetep(struct usb_dev_state *ps, void __user *arg)
1257 unsigned int ep;
1258 int ret;
1260 if (get_user(ep, (unsigned int __user *)arg))
1261 return -EFAULT;
1262 ret = findintfep(ps->dev, ep);
1263 if (ret < 0)
1264 return ret;
1265 ret = checkintf(ps, ret);
1266 if (ret)
1267 return ret;
1268 check_reset_of_active_ep(ps->dev, ep, "RESETEP");
1269 usb_reset_endpoint(ps->dev, ep);
1270 return 0;
1273 static int proc_clearhalt(struct usb_dev_state *ps, void __user *arg)
1275 unsigned int ep;
1276 int pipe;
1277 int ret;
1279 if (get_user(ep, (unsigned int __user *)arg))
1280 return -EFAULT;
1281 ret = findintfep(ps->dev, ep);
1282 if (ret < 0)
1283 return ret;
1284 ret = checkintf(ps, ret);
1285 if (ret)
1286 return ret;
1287 check_reset_of_active_ep(ps->dev, ep, "CLEAR_HALT");
1288 if (ep & USB_DIR_IN)
1289 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
1290 else
1291 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
1293 return usb_clear_halt(ps->dev, pipe);
1296 static int proc_getdriver(struct usb_dev_state *ps, void __user *arg)
1298 struct usbdevfs_getdriver gd;
1299 struct usb_interface *intf;
1300 int ret;
1302 if (copy_from_user(&gd, arg, sizeof(gd)))
1303 return -EFAULT;
1304 intf = usb_ifnum_to_if(ps->dev, gd.interface);
1305 if (!intf || !intf->dev.driver)
1306 ret = -ENODATA;
1307 else {
1308 strlcpy(gd.driver, intf->dev.driver->name,
1309 sizeof(gd.driver));
1310 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
1312 return ret;
1315 static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg)
1317 struct usbdevfs_connectinfo ci;
1319 memset(&ci, 0, sizeof(ci));
1320 ci.devnum = ps->dev->devnum;
1321 ci.slow = ps->dev->speed == USB_SPEED_LOW;
1323 if (copy_to_user(arg, &ci, sizeof(ci)))
1324 return -EFAULT;
1325 return 0;
1328 static int proc_resetdevice(struct usb_dev_state *ps)
1330 struct usb_host_config *actconfig = ps->dev->actconfig;
1331 struct usb_interface *interface;
1332 int i, number;
1334 /* Don't allow a device reset if the process has dropped the
1335 * privilege to do such things and any of the interfaces are
1336 * currently claimed.
1338 if (ps->privileges_dropped && actconfig) {
1339 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
1340 interface = actconfig->interface[i];
1341 number = interface->cur_altsetting->desc.bInterfaceNumber;
1342 if (usb_interface_claimed(interface) &&
1343 !test_bit(number, &ps->ifclaimed)) {
1344 dev_warn(&ps->dev->dev,
1345 "usbfs: interface %d claimed by %s while '%s' resets device\n",
1346 number, interface->dev.driver->name, current->comm);
1347 return -EACCES;
1352 return usb_reset_device(ps->dev);
1355 static int proc_setintf(struct usb_dev_state *ps, void __user *arg)
1357 struct usbdevfs_setinterface setintf;
1358 int ret;
1360 if (copy_from_user(&setintf, arg, sizeof(setintf)))
1361 return -EFAULT;
1362 ret = checkintf(ps, setintf.interface);
1363 if (ret)
1364 return ret;
1366 destroy_async_on_interface(ps, setintf.interface);
1368 return usb_set_interface(ps->dev, setintf.interface,
1369 setintf.altsetting);
1372 static int proc_setconfig(struct usb_dev_state *ps, void __user *arg)
1374 int u;
1375 int status = 0;
1376 struct usb_host_config *actconfig;
1378 if (get_user(u, (int __user *)arg))
1379 return -EFAULT;
1381 actconfig = ps->dev->actconfig;
1383 /* Don't touch the device if any interfaces are claimed.
1384 * It could interfere with other drivers' operations, and if
1385 * an interface is claimed by usbfs it could easily deadlock.
1387 if (actconfig) {
1388 int i;
1390 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
1391 if (usb_interface_claimed(actconfig->interface[i])) {
1392 dev_warn(&ps->dev->dev,
1393 "usbfs: interface %d claimed by %s "
1394 "while '%s' sets config #%d\n",
1395 actconfig->interface[i]
1396 ->cur_altsetting
1397 ->desc.bInterfaceNumber,
1398 actconfig->interface[i]
1399 ->dev.driver->name,
1400 current->comm, u);
1401 status = -EBUSY;
1402 break;
1407 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
1408 * so avoid usb_set_configuration()'s kick to sysfs
1410 if (status == 0) {
1411 if (actconfig && actconfig->desc.bConfigurationValue == u)
1412 status = usb_reset_configuration(ps->dev);
1413 else
1414 status = usb_set_configuration(ps->dev, u);
1417 return status;
1420 static struct usb_memory *
1421 find_memory_area(struct usb_dev_state *ps, const struct usbdevfs_urb *uurb)
1423 struct usb_memory *usbm = NULL, *iter;
1424 unsigned long flags;
1425 unsigned long uurb_start = (unsigned long)uurb->buffer;
1427 spin_lock_irqsave(&ps->lock, flags);
1428 list_for_each_entry(iter, &ps->memory_list, memlist) {
1429 if (uurb_start >= iter->vm_start &&
1430 uurb_start < iter->vm_start + iter->size) {
1431 if (uurb->buffer_length > iter->vm_start + iter->size -
1432 uurb_start) {
1433 usbm = ERR_PTR(-EINVAL);
1434 } else {
1435 usbm = iter;
1436 usbm->urb_use_count++;
1438 break;
1441 spin_unlock_irqrestore(&ps->lock, flags);
1442 return usbm;
1445 static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb,
1446 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
1447 void __user *arg)
1449 struct usbdevfs_iso_packet_desc *isopkt = NULL;
1450 struct usb_host_endpoint *ep;
1451 struct async *as = NULL;
1452 struct usb_ctrlrequest *dr = NULL;
1453 unsigned int u, totlen, isofrmlen;
1454 int i, ret, num_sgs = 0, ifnum = -1;
1455 int number_of_packets = 0;
1456 unsigned int stream_id = 0;
1457 void *buf;
1458 bool is_in;
1459 bool allow_short = false;
1460 bool allow_zero = false;
1461 unsigned long mask = USBDEVFS_URB_SHORT_NOT_OK |
1462 USBDEVFS_URB_BULK_CONTINUATION |
1463 USBDEVFS_URB_NO_FSBR |
1464 USBDEVFS_URB_ZERO_PACKET |
1465 USBDEVFS_URB_NO_INTERRUPT;
1466 /* USBDEVFS_URB_ISO_ASAP is a special case */
1467 if (uurb->type == USBDEVFS_URB_TYPE_ISO)
1468 mask |= USBDEVFS_URB_ISO_ASAP;
1470 if (uurb->flags & ~mask)
1471 return -EINVAL;
1473 if ((unsigned int)uurb->buffer_length >= USBFS_XFER_MAX)
1474 return -EINVAL;
1475 if (uurb->buffer_length > 0 && !uurb->buffer)
1476 return -EINVAL;
1477 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
1478 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
1479 ifnum = findintfep(ps->dev, uurb->endpoint);
1480 if (ifnum < 0)
1481 return ifnum;
1482 ret = checkintf(ps, ifnum);
1483 if (ret)
1484 return ret;
1486 ep = ep_to_host_endpoint(ps->dev, uurb->endpoint);
1487 if (!ep)
1488 return -ENOENT;
1489 is_in = (uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0;
1491 u = 0;
1492 switch (uurb->type) {
1493 case USBDEVFS_URB_TYPE_CONTROL:
1494 if (!usb_endpoint_xfer_control(&ep->desc))
1495 return -EINVAL;
1496 /* min 8 byte setup packet */
1497 if (uurb->buffer_length < 8)
1498 return -EINVAL;
1499 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1500 if (!dr)
1501 return -ENOMEM;
1502 if (copy_from_user(dr, uurb->buffer, 8)) {
1503 ret = -EFAULT;
1504 goto error;
1506 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
1507 ret = -EINVAL;
1508 goto error;
1510 ret = check_ctrlrecip(ps, dr->bRequestType, dr->bRequest,
1511 le16_to_cpup(&dr->wIndex));
1512 if (ret)
1513 goto error;
1514 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1515 uurb->buffer += 8;
1516 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1517 is_in = 1;
1518 uurb->endpoint |= USB_DIR_IN;
1519 } else {
1520 is_in = 0;
1521 uurb->endpoint &= ~USB_DIR_IN;
1523 if (is_in)
1524 allow_short = true;
1525 snoop(&ps->dev->dev, "control urb: bRequestType=%02x "
1526 "bRequest=%02x wValue=%04x "
1527 "wIndex=%04x wLength=%04x\n",
1528 dr->bRequestType, dr->bRequest,
1529 __le16_to_cpup(&dr->wValue),
1530 __le16_to_cpup(&dr->wIndex),
1531 __le16_to_cpup(&dr->wLength));
1532 u = sizeof(struct usb_ctrlrequest);
1533 break;
1535 case USBDEVFS_URB_TYPE_BULK:
1536 if (!is_in)
1537 allow_zero = true;
1538 else
1539 allow_short = true;
1540 switch (usb_endpoint_type(&ep->desc)) {
1541 case USB_ENDPOINT_XFER_CONTROL:
1542 case USB_ENDPOINT_XFER_ISOC:
1543 return -EINVAL;
1544 case USB_ENDPOINT_XFER_INT:
1545 /* allow single-shot interrupt transfers */
1546 uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
1547 goto interrupt_urb;
1549 num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE);
1550 if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize)
1551 num_sgs = 0;
1552 if (ep->streams)
1553 stream_id = uurb->stream_id;
1554 break;
1556 case USBDEVFS_URB_TYPE_INTERRUPT:
1557 if (!usb_endpoint_xfer_int(&ep->desc))
1558 return -EINVAL;
1559 interrupt_urb:
1560 if (!is_in)
1561 allow_zero = true;
1562 else
1563 allow_short = true;
1564 break;
1566 case USBDEVFS_URB_TYPE_ISO:
1567 /* arbitrary limit */
1568 if (uurb->number_of_packets < 1 ||
1569 uurb->number_of_packets > 128)
1570 return -EINVAL;
1571 if (!usb_endpoint_xfer_isoc(&ep->desc))
1572 return -EINVAL;
1573 number_of_packets = uurb->number_of_packets;
1574 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1575 number_of_packets;
1576 isopkt = memdup_user(iso_frame_desc, isofrmlen);
1577 if (IS_ERR(isopkt)) {
1578 ret = PTR_ERR(isopkt);
1579 isopkt = NULL;
1580 goto error;
1582 for (totlen = u = 0; u < number_of_packets; u++) {
1584 * arbitrary limit need for USB 3.0
1585 * bMaxBurst (0~15 allowed, 1~16 packets)
1586 * bmAttributes (bit 1:0, mult 0~2, 1~3 packets)
1587 * sizemax: 1024 * 16 * 3 = 49152
1589 if (isopkt[u].length > 49152) {
1590 ret = -EINVAL;
1591 goto error;
1593 totlen += isopkt[u].length;
1595 u *= sizeof(struct usb_iso_packet_descriptor);
1596 uurb->buffer_length = totlen;
1597 break;
1599 default:
1600 return -EINVAL;
1603 if (uurb->buffer_length > 0 &&
1604 !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1605 uurb->buffer, uurb->buffer_length)) {
1606 ret = -EFAULT;
1607 goto error;
1609 as = alloc_async(number_of_packets);
1610 if (!as) {
1611 ret = -ENOMEM;
1612 goto error;
1615 as->usbm = find_memory_area(ps, uurb);
1616 if (IS_ERR(as->usbm)) {
1617 ret = PTR_ERR(as->usbm);
1618 as->usbm = NULL;
1619 goto error;
1622 /* do not use SG buffers when memory mapped segments
1623 * are in use
1625 if (as->usbm)
1626 num_sgs = 0;
1628 u += sizeof(struct async) + sizeof(struct urb) + uurb->buffer_length +
1629 num_sgs * sizeof(struct scatterlist);
1630 ret = usbfs_increase_memory_usage(u);
1631 if (ret)
1632 goto error;
1633 as->mem_usage = u;
1635 if (num_sgs) {
1636 as->urb->sg = kmalloc(num_sgs * sizeof(struct scatterlist),
1637 GFP_KERNEL);
1638 if (!as->urb->sg) {
1639 ret = -ENOMEM;
1640 goto error;
1642 as->urb->num_sgs = num_sgs;
1643 sg_init_table(as->urb->sg, as->urb->num_sgs);
1645 totlen = uurb->buffer_length;
1646 for (i = 0; i < as->urb->num_sgs; i++) {
1647 u = (totlen > USB_SG_SIZE) ? USB_SG_SIZE : totlen;
1648 buf = kmalloc(u, GFP_KERNEL);
1649 if (!buf) {
1650 ret = -ENOMEM;
1651 goto error;
1653 sg_set_buf(&as->urb->sg[i], buf, u);
1655 if (!is_in) {
1656 if (copy_from_user(buf, uurb->buffer, u)) {
1657 ret = -EFAULT;
1658 goto error;
1660 uurb->buffer += u;
1662 totlen -= u;
1664 } else if (uurb->buffer_length > 0) {
1665 if (as->usbm) {
1666 unsigned long uurb_start = (unsigned long)uurb->buffer;
1668 as->urb->transfer_buffer = as->usbm->mem +
1669 (uurb_start - as->usbm->vm_start);
1670 } else {
1671 as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
1672 GFP_KERNEL);
1673 if (!as->urb->transfer_buffer) {
1674 ret = -ENOMEM;
1675 goto error;
1677 if (!is_in) {
1678 if (copy_from_user(as->urb->transfer_buffer,
1679 uurb->buffer,
1680 uurb->buffer_length)) {
1681 ret = -EFAULT;
1682 goto error;
1684 } else if (uurb->type == USBDEVFS_URB_TYPE_ISO) {
1686 * Isochronous input data may end up being
1687 * discontiguous if some of the packets are
1688 * short. Clear the buffer so that the gaps
1689 * don't leak kernel data to userspace.
1691 memset(as->urb->transfer_buffer, 0,
1692 uurb->buffer_length);
1696 as->urb->dev = ps->dev;
1697 as->urb->pipe = (uurb->type << 30) |
1698 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1699 (uurb->endpoint & USB_DIR_IN);
1701 /* This tedious sequence is necessary because the URB_* flags
1702 * are internal to the kernel and subject to change, whereas
1703 * the USBDEVFS_URB_* flags are a user API and must not be changed.
1705 u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
1706 if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
1707 u |= URB_ISO_ASAP;
1708 if (allow_short && uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
1709 u |= URB_SHORT_NOT_OK;
1710 if (uurb->flags & USBDEVFS_URB_NO_FSBR)
1711 u |= URB_NO_FSBR;
1712 if (allow_zero && uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1713 u |= URB_ZERO_PACKET;
1714 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
1715 u |= URB_NO_INTERRUPT;
1716 as->urb->transfer_flags = u;
1718 if (!allow_short && uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
1719 dev_warn(&ps->dev->dev, "Requested nonsensical USBDEVFS_URB_SHORT_NOT_OK.\n");
1720 if (!allow_zero && uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1721 dev_warn(&ps->dev->dev, "Requested nonsensical USBDEVFS_URB_ZERO_PACKET.\n");
1723 as->urb->transfer_buffer_length = uurb->buffer_length;
1724 as->urb->setup_packet = (unsigned char *)dr;
1725 dr = NULL;
1726 as->urb->start_frame = uurb->start_frame;
1727 as->urb->number_of_packets = number_of_packets;
1728 as->urb->stream_id = stream_id;
1730 if (ep->desc.bInterval) {
1731 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1732 ps->dev->speed == USB_SPEED_HIGH ||
1733 ps->dev->speed >= USB_SPEED_SUPER)
1734 as->urb->interval = 1 <<
1735 min(15, ep->desc.bInterval - 1);
1736 else
1737 as->urb->interval = ep->desc.bInterval;
1740 as->urb->context = as;
1741 as->urb->complete = async_completed;
1742 for (totlen = u = 0; u < number_of_packets; u++) {
1743 as->urb->iso_frame_desc[u].offset = totlen;
1744 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1745 totlen += isopkt[u].length;
1747 kfree(isopkt);
1748 isopkt = NULL;
1749 as->ps = ps;
1750 as->userurb = arg;
1751 if (as->usbm) {
1752 unsigned long uurb_start = (unsigned long)uurb->buffer;
1754 as->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1755 as->urb->transfer_dma = as->usbm->dma_handle +
1756 (uurb_start - as->usbm->vm_start);
1757 } else if (is_in && uurb->buffer_length > 0)
1758 as->userbuffer = uurb->buffer;
1759 as->signr = uurb->signr;
1760 as->ifnum = ifnum;
1761 as->pid = get_pid(task_pid(current));
1762 as->cred = get_current_cred();
1763 security_task_getsecid(current, &as->secid);
1764 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1765 as->urb->transfer_buffer_length, 0, SUBMIT,
1766 NULL, 0);
1767 if (!is_in)
1768 snoop_urb_data(as->urb, as->urb->transfer_buffer_length);
1770 async_newpending(as);
1772 if (usb_endpoint_xfer_bulk(&ep->desc)) {
1773 spin_lock_irq(&ps->lock);
1775 /* Not exactly the endpoint address; the direction bit is
1776 * shifted to the 0x10 position so that the value will be
1777 * between 0 and 31.
1779 as->bulk_addr = usb_endpoint_num(&ep->desc) |
1780 ((ep->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1781 >> 3);
1783 /* If this bulk URB is the start of a new transfer, re-enable
1784 * the endpoint. Otherwise mark it as a continuation URB.
1786 if (uurb->flags & USBDEVFS_URB_BULK_CONTINUATION)
1787 as->bulk_status = AS_CONTINUATION;
1788 else
1789 ps->disabled_bulk_eps &= ~(1 << as->bulk_addr);
1791 /* Don't accept continuation URBs if the endpoint is
1792 * disabled because of an earlier error.
1794 if (ps->disabled_bulk_eps & (1 << as->bulk_addr))
1795 ret = -EREMOTEIO;
1796 else
1797 ret = usb_submit_urb(as->urb, GFP_ATOMIC);
1798 spin_unlock_irq(&ps->lock);
1799 } else {
1800 ret = usb_submit_urb(as->urb, GFP_KERNEL);
1803 if (ret) {
1804 dev_printk(KERN_DEBUG, &ps->dev->dev,
1805 "usbfs: usb_submit_urb returned %d\n", ret);
1806 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1807 0, ret, COMPLETE, NULL, 0);
1808 async_removepending(as);
1809 goto error;
1811 return 0;
1813 error:
1814 if (as && as->usbm)
1815 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count);
1816 kfree(isopkt);
1817 kfree(dr);
1818 if (as)
1819 free_async(as);
1820 return ret;
1823 static int proc_submiturb(struct usb_dev_state *ps, void __user *arg)
1825 struct usbdevfs_urb uurb;
1827 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1828 return -EFAULT;
1830 return proc_do_submiturb(ps, &uurb,
1831 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1832 arg);
1835 static int proc_unlinkurb(struct usb_dev_state *ps, void __user *arg)
1837 struct urb *urb;
1838 struct async *as;
1839 unsigned long flags;
1841 spin_lock_irqsave(&ps->lock, flags);
1842 as = async_getpending(ps, arg);
1843 if (!as) {
1844 spin_unlock_irqrestore(&ps->lock, flags);
1845 return -EINVAL;
1848 urb = as->urb;
1849 usb_get_urb(urb);
1850 spin_unlock_irqrestore(&ps->lock, flags);
1852 usb_kill_urb(urb);
1853 usb_put_urb(urb);
1855 return 0;
1858 static void compute_isochronous_actual_length(struct urb *urb)
1860 unsigned int i;
1862 if (urb->number_of_packets > 0) {
1863 urb->actual_length = 0;
1864 for (i = 0; i < urb->number_of_packets; i++)
1865 urb->actual_length +=
1866 urb->iso_frame_desc[i].actual_length;
1870 static int processcompl(struct async *as, void __user * __user *arg)
1872 struct urb *urb = as->urb;
1873 struct usbdevfs_urb __user *userurb = as->userurb;
1874 void __user *addr = as->userurb;
1875 unsigned int i;
1877 compute_isochronous_actual_length(urb);
1878 if (as->userbuffer && urb->actual_length) {
1879 if (copy_urb_data_to_user(as->userbuffer, urb))
1880 goto err_out;
1882 if (put_user(as->status, &userurb->status))
1883 goto err_out;
1884 if (put_user(urb->actual_length, &userurb->actual_length))
1885 goto err_out;
1886 if (put_user(urb->error_count, &userurb->error_count))
1887 goto err_out;
1889 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1890 for (i = 0; i < urb->number_of_packets; i++) {
1891 if (put_user(urb->iso_frame_desc[i].actual_length,
1892 &userurb->iso_frame_desc[i].actual_length))
1893 goto err_out;
1894 if (put_user(urb->iso_frame_desc[i].status,
1895 &userurb->iso_frame_desc[i].status))
1896 goto err_out;
1900 if (put_user(addr, (void __user * __user *)arg))
1901 return -EFAULT;
1902 return 0;
1904 err_out:
1905 return -EFAULT;
1908 static struct async *reap_as(struct usb_dev_state *ps)
1910 DECLARE_WAITQUEUE(wait, current);
1911 struct async *as = NULL;
1912 struct usb_device *dev = ps->dev;
1914 add_wait_queue(&ps->wait, &wait);
1915 for (;;) {
1916 __set_current_state(TASK_INTERRUPTIBLE);
1917 as = async_getcompleted(ps);
1918 if (as || !connected(ps))
1919 break;
1920 if (signal_pending(current))
1921 break;
1922 usb_unlock_device(dev);
1923 schedule();
1924 usb_lock_device(dev);
1926 remove_wait_queue(&ps->wait, &wait);
1927 set_current_state(TASK_RUNNING);
1928 return as;
1931 static int proc_reapurb(struct usb_dev_state *ps, void __user *arg)
1933 struct async *as = reap_as(ps);
1935 if (as) {
1936 int retval;
1938 snoop(&ps->dev->dev, "reap %pK\n", as->userurb);
1939 retval = processcompl(as, (void __user * __user *)arg);
1940 free_async(as);
1941 return retval;
1943 if (signal_pending(current))
1944 return -EINTR;
1945 return -ENODEV;
1948 static int proc_reapurbnonblock(struct usb_dev_state *ps, void __user *arg)
1950 int retval;
1951 struct async *as;
1953 as = async_getcompleted(ps);
1954 if (as) {
1955 snoop(&ps->dev->dev, "reap %pK\n", as->userurb);
1956 retval = processcompl(as, (void __user * __user *)arg);
1957 free_async(as);
1958 } else {
1959 retval = (connected(ps) ? -EAGAIN : -ENODEV);
1961 return retval;
1964 #ifdef CONFIG_COMPAT
1965 static int proc_control_compat(struct usb_dev_state *ps,
1966 struct usbdevfs_ctrltransfer32 __user *p32)
1968 struct usbdevfs_ctrltransfer __user *p;
1969 __u32 udata;
1970 p = compat_alloc_user_space(sizeof(*p));
1971 if (copy_in_user(p, p32, (sizeof(*p32) - sizeof(compat_caddr_t))) ||
1972 get_user(udata, &p32->data) ||
1973 put_user(compat_ptr(udata), &p->data))
1974 return -EFAULT;
1975 return proc_control(ps, p);
1978 static int proc_bulk_compat(struct usb_dev_state *ps,
1979 struct usbdevfs_bulktransfer32 __user *p32)
1981 struct usbdevfs_bulktransfer __user *p;
1982 compat_uint_t n;
1983 compat_caddr_t addr;
1985 p = compat_alloc_user_space(sizeof(*p));
1987 if (get_user(n, &p32->ep) || put_user(n, &p->ep) ||
1988 get_user(n, &p32->len) || put_user(n, &p->len) ||
1989 get_user(n, &p32->timeout) || put_user(n, &p->timeout) ||
1990 get_user(addr, &p32->data) || put_user(compat_ptr(addr), &p->data))
1991 return -EFAULT;
1993 return proc_bulk(ps, p);
1995 static int proc_disconnectsignal_compat(struct usb_dev_state *ps, void __user *arg)
1997 struct usbdevfs_disconnectsignal32 ds;
1999 if (copy_from_user(&ds, arg, sizeof(ds)))
2000 return -EFAULT;
2001 ps->discsignr = ds.signr;
2002 ps->disccontext = compat_ptr(ds.context);
2003 return 0;
2006 static int get_urb32(struct usbdevfs_urb *kurb,
2007 struct usbdevfs_urb32 __user *uurb)
2009 struct usbdevfs_urb32 urb32;
2010 if (copy_from_user(&urb32, uurb, sizeof(*uurb)))
2011 return -EFAULT;
2012 kurb->type = urb32.type;
2013 kurb->endpoint = urb32.endpoint;
2014 kurb->status = urb32.status;
2015 kurb->flags = urb32.flags;
2016 kurb->buffer = compat_ptr(urb32.buffer);
2017 kurb->buffer_length = urb32.buffer_length;
2018 kurb->actual_length = urb32.actual_length;
2019 kurb->start_frame = urb32.start_frame;
2020 kurb->number_of_packets = urb32.number_of_packets;
2021 kurb->error_count = urb32.error_count;
2022 kurb->signr = urb32.signr;
2023 kurb->usercontext = compat_ptr(urb32.usercontext);
2024 return 0;
2027 static int proc_submiturb_compat(struct usb_dev_state *ps, void __user *arg)
2029 struct usbdevfs_urb uurb;
2031 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
2032 return -EFAULT;
2034 return proc_do_submiturb(ps, &uurb,
2035 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
2036 arg);
2039 static int processcompl_compat(struct async *as, void __user * __user *arg)
2041 struct urb *urb = as->urb;
2042 struct usbdevfs_urb32 __user *userurb = as->userurb;
2043 void __user *addr = as->userurb;
2044 unsigned int i;
2046 compute_isochronous_actual_length(urb);
2047 if (as->userbuffer && urb->actual_length) {
2048 if (copy_urb_data_to_user(as->userbuffer, urb))
2049 return -EFAULT;
2051 if (put_user(as->status, &userurb->status))
2052 return -EFAULT;
2053 if (put_user(urb->actual_length, &userurb->actual_length))
2054 return -EFAULT;
2055 if (put_user(urb->error_count, &userurb->error_count))
2056 return -EFAULT;
2058 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
2059 for (i = 0; i < urb->number_of_packets; i++) {
2060 if (put_user(urb->iso_frame_desc[i].actual_length,
2061 &userurb->iso_frame_desc[i].actual_length))
2062 return -EFAULT;
2063 if (put_user(urb->iso_frame_desc[i].status,
2064 &userurb->iso_frame_desc[i].status))
2065 return -EFAULT;
2069 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
2070 return -EFAULT;
2071 return 0;
2074 static int proc_reapurb_compat(struct usb_dev_state *ps, void __user *arg)
2076 struct async *as = reap_as(ps);
2078 if (as) {
2079 int retval;
2081 snoop(&ps->dev->dev, "reap %pK\n", as->userurb);
2082 retval = processcompl_compat(as, (void __user * __user *)arg);
2083 free_async(as);
2084 return retval;
2086 if (signal_pending(current))
2087 return -EINTR;
2088 return -ENODEV;
2091 static int proc_reapurbnonblock_compat(struct usb_dev_state *ps, void __user *arg)
2093 int retval;
2094 struct async *as;
2096 as = async_getcompleted(ps);
2097 if (as) {
2098 snoop(&ps->dev->dev, "reap %pK\n", as->userurb);
2099 retval = processcompl_compat(as, (void __user * __user *)arg);
2100 free_async(as);
2101 } else {
2102 retval = (connected(ps) ? -EAGAIN : -ENODEV);
2104 return retval;
2108 #endif
2110 static int proc_disconnectsignal(struct usb_dev_state *ps, void __user *arg)
2112 struct usbdevfs_disconnectsignal ds;
2114 if (copy_from_user(&ds, arg, sizeof(ds)))
2115 return -EFAULT;
2116 ps->discsignr = ds.signr;
2117 ps->disccontext = ds.context;
2118 return 0;
2121 static int proc_claiminterface(struct usb_dev_state *ps, void __user *arg)
2123 unsigned int ifnum;
2125 if (get_user(ifnum, (unsigned int __user *)arg))
2126 return -EFAULT;
2127 return claimintf(ps, ifnum);
2130 static int proc_releaseinterface(struct usb_dev_state *ps, void __user *arg)
2132 unsigned int ifnum;
2133 int ret;
2135 if (get_user(ifnum, (unsigned int __user *)arg))
2136 return -EFAULT;
2137 ret = releaseintf(ps, ifnum);
2138 if (ret < 0)
2139 return ret;
2140 destroy_async_on_interface(ps, ifnum);
2141 return 0;
2144 static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl)
2146 int size;
2147 void *buf = NULL;
2148 int retval = 0;
2149 struct usb_interface *intf = NULL;
2150 struct usb_driver *driver = NULL;
2152 if (ps->privileges_dropped)
2153 return -EACCES;
2155 /* alloc buffer */
2156 size = _IOC_SIZE(ctl->ioctl_code);
2157 if (size > 0) {
2158 buf = kmalloc(size, GFP_KERNEL);
2159 if (buf == NULL)
2160 return -ENOMEM;
2161 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
2162 if (copy_from_user(buf, ctl->data, size)) {
2163 kfree(buf);
2164 return -EFAULT;
2166 } else {
2167 memset(buf, 0, size);
2171 if (!connected(ps)) {
2172 kfree(buf);
2173 return -ENODEV;
2176 if (ps->dev->state != USB_STATE_CONFIGURED)
2177 retval = -EHOSTUNREACH;
2178 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
2179 retval = -EINVAL;
2180 else switch (ctl->ioctl_code) {
2182 /* disconnect kernel driver from interface */
2183 case USBDEVFS_DISCONNECT:
2184 if (intf->dev.driver) {
2185 driver = to_usb_driver(intf->dev.driver);
2186 dev_dbg(&intf->dev, "disconnect by usbfs\n");
2187 usb_driver_release_interface(driver, intf);
2188 } else
2189 retval = -ENODATA;
2190 break;
2192 /* let kernel drivers try to (re)bind to the interface */
2193 case USBDEVFS_CONNECT:
2194 if (!intf->dev.driver)
2195 retval = device_attach(&intf->dev);
2196 else
2197 retval = -EBUSY;
2198 break;
2200 /* talk directly to the interface's driver */
2201 default:
2202 if (intf->dev.driver)
2203 driver = to_usb_driver(intf->dev.driver);
2204 if (driver == NULL || driver->unlocked_ioctl == NULL) {
2205 retval = -ENOTTY;
2206 } else {
2207 retval = driver->unlocked_ioctl(intf, ctl->ioctl_code, buf);
2208 if (retval == -ENOIOCTLCMD)
2209 retval = -ENOTTY;
2213 /* cleanup and return */
2214 if (retval >= 0
2215 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
2216 && size > 0
2217 && copy_to_user(ctl->data, buf, size) != 0)
2218 retval = -EFAULT;
2220 kfree(buf);
2221 return retval;
2224 static int proc_ioctl_default(struct usb_dev_state *ps, void __user *arg)
2226 struct usbdevfs_ioctl ctrl;
2228 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
2229 return -EFAULT;
2230 return proc_ioctl(ps, &ctrl);
2233 #ifdef CONFIG_COMPAT
2234 static int proc_ioctl_compat(struct usb_dev_state *ps, compat_uptr_t arg)
2236 struct usbdevfs_ioctl32 ioc32;
2237 struct usbdevfs_ioctl ctrl;
2239 if (copy_from_user(&ioc32, compat_ptr(arg), sizeof(ioc32)))
2240 return -EFAULT;
2241 ctrl.ifno = ioc32.ifno;
2242 ctrl.ioctl_code = ioc32.ioctl_code;
2243 ctrl.data = compat_ptr(ioc32.data);
2244 return proc_ioctl(ps, &ctrl);
2246 #endif
2248 static int proc_claim_port(struct usb_dev_state *ps, void __user *arg)
2250 unsigned portnum;
2251 int rc;
2253 if (get_user(portnum, (unsigned __user *) arg))
2254 return -EFAULT;
2255 rc = usb_hub_claim_port(ps->dev, portnum, ps);
2256 if (rc == 0)
2257 snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n",
2258 portnum, task_pid_nr(current), current->comm);
2259 return rc;
2262 static int proc_release_port(struct usb_dev_state *ps, void __user *arg)
2264 unsigned portnum;
2266 if (get_user(portnum, (unsigned __user *) arg))
2267 return -EFAULT;
2268 return usb_hub_release_port(ps->dev, portnum, ps);
2271 static int proc_get_capabilities(struct usb_dev_state *ps, void __user *arg)
2273 __u32 caps;
2275 caps = USBDEVFS_CAP_ZERO_PACKET | USBDEVFS_CAP_NO_PACKET_SIZE_LIM |
2276 USBDEVFS_CAP_REAP_AFTER_DISCONNECT | USBDEVFS_CAP_MMAP |
2277 USBDEVFS_CAP_DROP_PRIVILEGES;
2278 if (!ps->dev->bus->no_stop_on_short)
2279 caps |= USBDEVFS_CAP_BULK_CONTINUATION;
2280 if (ps->dev->bus->sg_tablesize)
2281 caps |= USBDEVFS_CAP_BULK_SCATTER_GATHER;
2283 if (put_user(caps, (__u32 __user *)arg))
2284 return -EFAULT;
2286 return 0;
2289 static int proc_disconnect_claim(struct usb_dev_state *ps, void __user *arg)
2291 struct usbdevfs_disconnect_claim dc;
2292 struct usb_interface *intf;
2294 if (copy_from_user(&dc, arg, sizeof(dc)))
2295 return -EFAULT;
2297 intf = usb_ifnum_to_if(ps->dev, dc.interface);
2298 if (!intf)
2299 return -EINVAL;
2301 if (intf->dev.driver) {
2302 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
2304 if (ps->privileges_dropped)
2305 return -EACCES;
2307 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_IF_DRIVER) &&
2308 strncmp(dc.driver, intf->dev.driver->name,
2309 sizeof(dc.driver)) != 0)
2310 return -EBUSY;
2312 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_EXCEPT_DRIVER) &&
2313 strncmp(dc.driver, intf->dev.driver->name,
2314 sizeof(dc.driver)) == 0)
2315 return -EBUSY;
2317 dev_dbg(&intf->dev, "disconnect by usbfs\n");
2318 usb_driver_release_interface(driver, intf);
2321 return claimintf(ps, dc.interface);
2324 static int proc_alloc_streams(struct usb_dev_state *ps, void __user *arg)
2326 unsigned num_streams, num_eps;
2327 struct usb_host_endpoint **eps;
2328 struct usb_interface *intf;
2329 int r;
2331 r = parse_usbdevfs_streams(ps, arg, &num_streams, &num_eps,
2332 &eps, &intf);
2333 if (r)
2334 return r;
2336 destroy_async_on_interface(ps,
2337 intf->altsetting[0].desc.bInterfaceNumber);
2339 r = usb_alloc_streams(intf, eps, num_eps, num_streams, GFP_KERNEL);
2340 kfree(eps);
2341 return r;
2344 static int proc_free_streams(struct usb_dev_state *ps, void __user *arg)
2346 unsigned num_eps;
2347 struct usb_host_endpoint **eps;
2348 struct usb_interface *intf;
2349 int r;
2351 r = parse_usbdevfs_streams(ps, arg, NULL, &num_eps, &eps, &intf);
2352 if (r)
2353 return r;
2355 destroy_async_on_interface(ps,
2356 intf->altsetting[0].desc.bInterfaceNumber);
2358 r = usb_free_streams(intf, eps, num_eps, GFP_KERNEL);
2359 kfree(eps);
2360 return r;
2363 static int proc_drop_privileges(struct usb_dev_state *ps, void __user *arg)
2365 u32 data;
2367 if (copy_from_user(&data, arg, sizeof(data)))
2368 return -EFAULT;
2370 /* This is a one way operation. Once privileges are
2371 * dropped, you cannot regain them. You may however reissue
2372 * this ioctl to shrink the allowed interfaces mask.
2374 ps->interface_allowed_mask &= data;
2375 ps->privileges_dropped = true;
2377 return 0;
2381 * NOTE: All requests here that have interface numbers as parameters
2382 * are assuming that somehow the configuration has been prevented from
2383 * changing. But there's no mechanism to ensure that...
2385 static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
2386 void __user *p)
2388 struct usb_dev_state *ps = file->private_data;
2389 struct inode *inode = file_inode(file);
2390 struct usb_device *dev = ps->dev;
2391 int ret = -ENOTTY;
2393 if (!(file->f_mode & FMODE_WRITE))
2394 return -EPERM;
2396 usb_lock_device(dev);
2398 /* Reap operations are allowed even after disconnection */
2399 switch (cmd) {
2400 case USBDEVFS_REAPURB:
2401 snoop(&dev->dev, "%s: REAPURB\n", __func__);
2402 ret = proc_reapurb(ps, p);
2403 goto done;
2405 case USBDEVFS_REAPURBNDELAY:
2406 snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
2407 ret = proc_reapurbnonblock(ps, p);
2408 goto done;
2410 #ifdef CONFIG_COMPAT
2411 case USBDEVFS_REAPURB32:
2412 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
2413 ret = proc_reapurb_compat(ps, p);
2414 goto done;
2416 case USBDEVFS_REAPURBNDELAY32:
2417 snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
2418 ret = proc_reapurbnonblock_compat(ps, p);
2419 goto done;
2420 #endif
2423 if (!connected(ps)) {
2424 usb_unlock_device(dev);
2425 return -ENODEV;
2428 switch (cmd) {
2429 case USBDEVFS_CONTROL:
2430 snoop(&dev->dev, "%s: CONTROL\n", __func__);
2431 ret = proc_control(ps, p);
2432 if (ret >= 0)
2433 inode->i_mtime = current_time(inode);
2434 break;
2436 case USBDEVFS_BULK:
2437 snoop(&dev->dev, "%s: BULK\n", __func__);
2438 ret = proc_bulk(ps, p);
2439 if (ret >= 0)
2440 inode->i_mtime = current_time(inode);
2441 break;
2443 case USBDEVFS_RESETEP:
2444 snoop(&dev->dev, "%s: RESETEP\n", __func__);
2445 ret = proc_resetep(ps, p);
2446 if (ret >= 0)
2447 inode->i_mtime = current_time(inode);
2448 break;
2450 case USBDEVFS_RESET:
2451 snoop(&dev->dev, "%s: RESET\n", __func__);
2452 ret = proc_resetdevice(ps);
2453 break;
2455 case USBDEVFS_CLEAR_HALT:
2456 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
2457 ret = proc_clearhalt(ps, p);
2458 if (ret >= 0)
2459 inode->i_mtime = current_time(inode);
2460 break;
2462 case USBDEVFS_GETDRIVER:
2463 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
2464 ret = proc_getdriver(ps, p);
2465 break;
2467 case USBDEVFS_CONNECTINFO:
2468 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
2469 ret = proc_connectinfo(ps, p);
2470 break;
2472 case USBDEVFS_SETINTERFACE:
2473 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
2474 ret = proc_setintf(ps, p);
2475 break;
2477 case USBDEVFS_SETCONFIGURATION:
2478 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
2479 ret = proc_setconfig(ps, p);
2480 break;
2482 case USBDEVFS_SUBMITURB:
2483 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
2484 ret = proc_submiturb(ps, p);
2485 if (ret >= 0)
2486 inode->i_mtime = current_time(inode);
2487 break;
2489 #ifdef CONFIG_COMPAT
2490 case USBDEVFS_CONTROL32:
2491 snoop(&dev->dev, "%s: CONTROL32\n", __func__);
2492 ret = proc_control_compat(ps, p);
2493 if (ret >= 0)
2494 inode->i_mtime = current_time(inode);
2495 break;
2497 case USBDEVFS_BULK32:
2498 snoop(&dev->dev, "%s: BULK32\n", __func__);
2499 ret = proc_bulk_compat(ps, p);
2500 if (ret >= 0)
2501 inode->i_mtime = current_time(inode);
2502 break;
2504 case USBDEVFS_DISCSIGNAL32:
2505 snoop(&dev->dev, "%s: DISCSIGNAL32\n", __func__);
2506 ret = proc_disconnectsignal_compat(ps, p);
2507 break;
2509 case USBDEVFS_SUBMITURB32:
2510 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
2511 ret = proc_submiturb_compat(ps, p);
2512 if (ret >= 0)
2513 inode->i_mtime = current_time(inode);
2514 break;
2516 case USBDEVFS_IOCTL32:
2517 snoop(&dev->dev, "%s: IOCTL32\n", __func__);
2518 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
2519 break;
2520 #endif
2522 case USBDEVFS_DISCARDURB:
2523 snoop(&dev->dev, "%s: DISCARDURB %pK\n", __func__, p);
2524 ret = proc_unlinkurb(ps, p);
2525 break;
2527 case USBDEVFS_DISCSIGNAL:
2528 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
2529 ret = proc_disconnectsignal(ps, p);
2530 break;
2532 case USBDEVFS_CLAIMINTERFACE:
2533 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
2534 ret = proc_claiminterface(ps, p);
2535 break;
2537 case USBDEVFS_RELEASEINTERFACE:
2538 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
2539 ret = proc_releaseinterface(ps, p);
2540 break;
2542 case USBDEVFS_IOCTL:
2543 snoop(&dev->dev, "%s: IOCTL\n", __func__);
2544 ret = proc_ioctl_default(ps, p);
2545 break;
2547 case USBDEVFS_CLAIM_PORT:
2548 snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__);
2549 ret = proc_claim_port(ps, p);
2550 break;
2552 case USBDEVFS_RELEASE_PORT:
2553 snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__);
2554 ret = proc_release_port(ps, p);
2555 break;
2556 case USBDEVFS_GET_CAPABILITIES:
2557 ret = proc_get_capabilities(ps, p);
2558 break;
2559 case USBDEVFS_DISCONNECT_CLAIM:
2560 ret = proc_disconnect_claim(ps, p);
2561 break;
2562 case USBDEVFS_ALLOC_STREAMS:
2563 ret = proc_alloc_streams(ps, p);
2564 break;
2565 case USBDEVFS_FREE_STREAMS:
2566 ret = proc_free_streams(ps, p);
2567 break;
2568 case USBDEVFS_DROP_PRIVILEGES:
2569 ret = proc_drop_privileges(ps, p);
2570 break;
2571 case USBDEVFS_GET_SPEED:
2572 ret = ps->dev->speed;
2573 break;
2576 done:
2577 usb_unlock_device(dev);
2578 if (ret >= 0)
2579 inode->i_atime = current_time(inode);
2580 return ret;
2583 static long usbdev_ioctl(struct file *file, unsigned int cmd,
2584 unsigned long arg)
2586 int ret;
2588 ret = usbdev_do_ioctl(file, cmd, (void __user *)arg);
2590 return ret;
2593 #ifdef CONFIG_COMPAT
2594 static long usbdev_compat_ioctl(struct file *file, unsigned int cmd,
2595 unsigned long arg)
2597 int ret;
2599 ret = usbdev_do_ioctl(file, cmd, compat_ptr(arg));
2601 return ret;
2603 #endif
2605 /* No kernel lock - fine */
2606 static unsigned int usbdev_poll(struct file *file,
2607 struct poll_table_struct *wait)
2609 struct usb_dev_state *ps = file->private_data;
2610 unsigned int mask = 0;
2612 poll_wait(file, &ps->wait, wait);
2613 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
2614 mask |= POLLOUT | POLLWRNORM;
2615 if (!connected(ps))
2616 mask |= POLLHUP;
2617 if (list_empty(&ps->list))
2618 mask |= POLLERR;
2619 return mask;
2622 const struct file_operations usbdev_file_operations = {
2623 .owner = THIS_MODULE,
2624 .llseek = no_seek_end_llseek,
2625 .read = usbdev_read,
2626 .poll = usbdev_poll,
2627 .unlocked_ioctl = usbdev_ioctl,
2628 #ifdef CONFIG_COMPAT
2629 .compat_ioctl = usbdev_compat_ioctl,
2630 #endif
2631 .mmap = usbdev_mmap,
2632 .open = usbdev_open,
2633 .release = usbdev_release,
2636 static void usbdev_remove(struct usb_device *udev)
2638 struct usb_dev_state *ps;
2639 struct siginfo sinfo;
2641 while (!list_empty(&udev->filelist)) {
2642 ps = list_entry(udev->filelist.next, struct usb_dev_state, list);
2643 destroy_all_async(ps);
2644 wake_up_all(&ps->wait);
2645 list_del_init(&ps->list);
2646 if (ps->discsignr) {
2647 memset(&sinfo, 0, sizeof(sinfo));
2648 sinfo.si_signo = ps->discsignr;
2649 sinfo.si_errno = EPIPE;
2650 sinfo.si_code = SI_ASYNCIO;
2651 sinfo.si_addr = ps->disccontext;
2652 kill_pid_info_as_cred(ps->discsignr, &sinfo,
2653 ps->disc_pid, ps->cred, ps->secid);
2658 static int usbdev_notify(struct notifier_block *self,
2659 unsigned long action, void *dev)
2661 switch (action) {
2662 case USB_DEVICE_ADD:
2663 break;
2664 case USB_DEVICE_REMOVE:
2665 usbdev_remove(dev);
2666 break;
2668 return NOTIFY_OK;
2671 static struct notifier_block usbdev_nb = {
2672 .notifier_call = usbdev_notify,
2675 static struct cdev usb_device_cdev;
2677 int __init usb_devio_init(void)
2679 int retval;
2681 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
2682 "usb_device");
2683 if (retval) {
2684 printk(KERN_ERR "Unable to register minors for usb_device\n");
2685 goto out;
2687 cdev_init(&usb_device_cdev, &usbdev_file_operations);
2688 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
2689 if (retval) {
2690 printk(KERN_ERR "Unable to get usb_device major %d\n",
2691 USB_DEVICE_MAJOR);
2692 goto error_cdev;
2694 usb_register_notify(&usbdev_nb);
2695 out:
2696 return retval;
2698 error_cdev:
2699 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
2700 goto out;
2703 void usb_devio_cleanup(void)
2705 usb_unregister_notify(&usbdev_nb);
2706 cdev_del(&usb_device_cdev);
2707 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);