1 // SPDX-License-Identifier: GPL-2.0+
2 /*****************************************************************************/
5 * devio.c -- User space communication with USB devices.
7 * Copyright (C) 1999-2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
9 * This file implements the usbfs/x/y files, where
10 * x is the bus number and y the device number.
12 * It allows user space programs/"drivers" to communicate directly
13 * with USB devices without intervening kernel driver.
16 * 22.12.1999 0.1 Initial release (split from proc_usb.c)
17 * 04.01.2000 0.2 Turned into its own filesystem
18 * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery
22 /*****************************************************************************/
26 #include <linux/sched/signal.h>
27 #include <linux/slab.h>
28 #include <linux/signal.h>
29 #include <linux/poll.h>
30 #include <linux/module.h>
31 #include <linux/string.h>
32 #include <linux/usb.h>
33 #include <linux/usbdevice_fs.h>
34 #include <linux/usb/hcd.h> /* for usbcore internals */
35 #include <linux/usb/quirks.h>
36 #include <linux/cdev.h>
37 #include <linux/notifier.h>
38 #include <linux/security.h>
39 #include <linux/user_namespace.h>
40 #include <linux/scatterlist.h>
41 #include <linux/uaccess.h>
42 #include <linux/dma-mapping.h>
43 #include <asm/byteorder.h>
44 #include <linux/moduleparam.h>
49 #define MAYBE_CAP_SUSPEND USBDEVFS_CAP_SUSPEND
51 #define MAYBE_CAP_SUSPEND 0
55 #define USB_DEVICE_MAX (USB_MAXBUS * 128)
56 #define USB_SG_SIZE 16384 /* split-size for large txs */
58 /* Mutual exclusion for ps->list in resume vs. release and remove */
59 static DEFINE_MUTEX(usbfs_mutex);
61 struct usb_dev_state {
62 struct list_head list; /* state list */
63 struct usb_device *dev;
65 spinlock_t lock; /* protects the async urb lists */
66 struct list_head async_pending;
67 struct list_head async_completed;
68 struct list_head memory_list;
69 wait_queue_head_t wait; /* wake up if a request completed */
70 wait_queue_head_t wait_for_resume; /* wake up upon runtime resume */
71 unsigned int discsignr;
73 const struct cred *cred;
75 unsigned long ifclaimed;
76 u32 disabled_bulk_eps;
77 unsigned long interface_allowed_mask;
80 bool privileges_dropped;
84 struct list_head memlist;
89 dma_addr_t dma_handle;
90 unsigned long vm_start;
91 struct usb_dev_state *ps;
95 struct list_head asynclist;
96 struct usb_dev_state *ps;
98 const struct cred *cred;
101 void __user *userbuffer;
102 void __user *userurb;
103 sigval_t userurb_sigval;
105 struct usb_memory *usbm;
106 unsigned int mem_usage;
112 static bool usbfs_snoop;
113 module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
114 MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
116 static unsigned usbfs_snoop_max = 65536;
117 module_param(usbfs_snoop_max, uint, S_IRUGO | S_IWUSR);
118 MODULE_PARM_DESC(usbfs_snoop_max,
119 "maximum number of bytes to print while snooping");
121 #define snoop(dev, format, arg...) \
124 dev_info(dev, format, ## arg); \
131 #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
133 /* Limit on the total amount of memory we can allocate for transfers */
134 static u32 usbfs_memory_mb = 16;
135 module_param(usbfs_memory_mb, uint, 0644);
136 MODULE_PARM_DESC(usbfs_memory_mb,
137 "maximum MB allowed for usbfs buffers (0 = no limit)");
139 /* Hard limit, necessary to avoid arithmetic overflow */
140 #define USBFS_XFER_MAX (UINT_MAX / 2 - 1000000)
142 static DEFINE_SPINLOCK(usbfs_memory_usage_lock);
143 static u64 usbfs_memory_usage; /* Total memory currently allocated */
145 /* Check whether it's okay to allocate more memory for a transfer */
146 static int usbfs_increase_memory_usage(u64 amount)
152 lim = READ_ONCE(usbfs_memory_mb);
156 spin_lock_irqsave(&usbfs_memory_usage_lock, flags);
157 total_mem = usbfs_memory_usage + amount;
158 if (lim > 0 && total_mem > lim)
161 usbfs_memory_usage = total_mem;
162 spin_unlock_irqrestore(&usbfs_memory_usage_lock, flags);
167 /* Memory for a transfer is being deallocated */
168 static void usbfs_decrease_memory_usage(u64 amount)
172 spin_lock_irqsave(&usbfs_memory_usage_lock, flags);
173 if (amount > usbfs_memory_usage)
174 usbfs_memory_usage = 0;
176 usbfs_memory_usage -= amount;
177 spin_unlock_irqrestore(&usbfs_memory_usage_lock, flags);
180 static int connected(struct usb_dev_state *ps)
182 return (!list_empty(&ps->list) &&
183 ps->dev->state != USB_STATE_NOTATTACHED);
186 static void dec_usb_memory_use_count(struct usb_memory *usbm, int *count)
188 struct usb_dev_state *ps = usbm->ps;
191 spin_lock_irqsave(&ps->lock, flags);
193 if (usbm->urb_use_count == 0 && usbm->vma_use_count == 0) {
194 list_del(&usbm->memlist);
195 spin_unlock_irqrestore(&ps->lock, flags);
197 usb_free_coherent(ps->dev, usbm->size, usbm->mem,
199 usbfs_decrease_memory_usage(
200 usbm->size + sizeof(struct usb_memory));
203 spin_unlock_irqrestore(&ps->lock, flags);
207 static void usbdev_vm_open(struct vm_area_struct *vma)
209 struct usb_memory *usbm = vma->vm_private_data;
212 spin_lock_irqsave(&usbm->ps->lock, flags);
213 ++usbm->vma_use_count;
214 spin_unlock_irqrestore(&usbm->ps->lock, flags);
217 static void usbdev_vm_close(struct vm_area_struct *vma)
219 struct usb_memory *usbm = vma->vm_private_data;
221 dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
224 static const struct vm_operations_struct usbdev_vm_ops = {
225 .open = usbdev_vm_open,
226 .close = usbdev_vm_close
229 static int usbdev_mmap(struct file *file, struct vm_area_struct *vma)
231 struct usb_memory *usbm = NULL;
232 struct usb_dev_state *ps = file->private_data;
233 struct usb_hcd *hcd = bus_to_hcd(ps->dev->bus);
234 size_t size = vma->vm_end - vma->vm_start;
237 dma_addr_t dma_handle;
240 ret = usbfs_increase_memory_usage(size + sizeof(struct usb_memory));
244 usbm = kzalloc(sizeof(struct usb_memory), GFP_KERNEL);
247 goto error_decrease_mem;
250 mem = usb_alloc_coherent(ps->dev, size, GFP_USER | __GFP_NOWARN,
254 goto error_free_usbm;
257 memset(mem, 0, size);
260 usbm->dma_handle = dma_handle;
263 usbm->vm_start = vma->vm_start;
264 usbm->vma_use_count = 1;
265 INIT_LIST_HEAD(&usbm->memlist);
267 if (hcd->localmem_pool || !hcd_uses_dma(hcd)) {
268 if (remap_pfn_range(vma, vma->vm_start,
269 virt_to_phys(usbm->mem) >> PAGE_SHIFT,
270 size, vma->vm_page_prot) < 0) {
271 dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
275 if (dma_mmap_coherent(hcd->self.sysdev, vma, mem, dma_handle,
277 dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
282 vm_flags_set(vma, VM_IO | VM_DONTEXPAND | VM_DONTDUMP);
283 vma->vm_ops = &usbdev_vm_ops;
284 vma->vm_private_data = usbm;
286 spin_lock_irqsave(&ps->lock, flags);
287 list_add_tail(&usbm->memlist, &ps->memory_list);
288 spin_unlock_irqrestore(&ps->lock, flags);
295 usbfs_decrease_memory_usage(size + sizeof(struct usb_memory));
300 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
303 struct usb_dev_state *ps = file->private_data;
304 struct usb_device *dev = ps->dev;
311 usb_lock_device(dev);
312 if (!connected(ps)) {
315 } else if (pos < 0) {
320 if (pos < sizeof(struct usb_device_descriptor)) {
321 /* 18 bytes - fits on the stack */
322 struct usb_device_descriptor temp_desc;
324 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
325 le16_to_cpus(&temp_desc.bcdUSB);
326 le16_to_cpus(&temp_desc.idVendor);
327 le16_to_cpus(&temp_desc.idProduct);
328 le16_to_cpus(&temp_desc.bcdDevice);
330 len = sizeof(struct usb_device_descriptor) - pos;
333 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
344 pos = sizeof(struct usb_device_descriptor);
345 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
346 struct usb_config_descriptor *config =
347 (struct usb_config_descriptor *)dev->rawdescriptors[i];
348 unsigned int length = le16_to_cpu(config->wTotalLength);
350 if (*ppos < pos + length) {
352 /* The descriptor may claim to be longer than it
353 * really is. Here is the actual allocated length. */
355 le16_to_cpu(dev->config[i].desc.wTotalLength);
357 len = length - (*ppos - pos);
361 /* Simply don't write (skip over) unallocated parts */
362 if (alloclen > (*ppos - pos)) {
363 alloclen -= (*ppos - pos);
364 if (copy_to_user(buf,
365 dev->rawdescriptors[i] + (*ppos - pos),
366 min(len, alloclen))) {
382 usb_unlock_device(dev);
387 * async list handling
390 static struct async *alloc_async(unsigned int numisoframes)
394 as = kzalloc(sizeof(struct async), GFP_KERNEL);
397 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
405 static void free_async(struct async *as)
412 for (i = 0; i < as->urb->num_sgs; i++) {
413 if (sg_page(&as->urb->sg[i]))
414 kfree(sg_virt(&as->urb->sg[i]));
418 if (as->usbm == NULL)
419 kfree(as->urb->transfer_buffer);
421 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count);
423 kfree(as->urb->setup_packet);
424 usb_free_urb(as->urb);
425 usbfs_decrease_memory_usage(as->mem_usage);
429 static void async_newpending(struct async *as)
431 struct usb_dev_state *ps = as->ps;
434 spin_lock_irqsave(&ps->lock, flags);
435 list_add_tail(&as->asynclist, &ps->async_pending);
436 spin_unlock_irqrestore(&ps->lock, flags);
439 static void async_removepending(struct async *as)
441 struct usb_dev_state *ps = as->ps;
444 spin_lock_irqsave(&ps->lock, flags);
445 list_del_init(&as->asynclist);
446 spin_unlock_irqrestore(&ps->lock, flags);
449 static struct async *async_getcompleted(struct usb_dev_state *ps)
452 struct async *as = NULL;
454 spin_lock_irqsave(&ps->lock, flags);
455 if (!list_empty(&ps->async_completed)) {
456 as = list_entry(ps->async_completed.next, struct async,
458 list_del_init(&as->asynclist);
460 spin_unlock_irqrestore(&ps->lock, flags);
464 static struct async *async_getpending(struct usb_dev_state *ps,
465 void __user *userurb)
469 list_for_each_entry(as, &ps->async_pending, asynclist)
470 if (as->userurb == userurb) {
471 list_del_init(&as->asynclist);
478 static void snoop_urb(struct usb_device *udev,
479 void __user *userurb, int pipe, unsigned length,
480 int timeout_or_status, enum snoop_when when,
481 unsigned char *data, unsigned data_len)
483 static const char *types[] = {"isoc", "int", "ctrl", "bulk"};
484 static const char *dirs[] = {"out", "in"};
491 ep = usb_pipeendpoint(pipe);
492 t = types[usb_pipetype(pipe)];
493 d = dirs[!!usb_pipein(pipe)];
495 if (userurb) { /* Async */
497 dev_info(&udev->dev, "userurb %px, ep%d %s-%s, "
499 userurb, ep, t, d, length);
501 dev_info(&udev->dev, "userurb %px, ep%d %s-%s, "
502 "actual_length %u status %d\n",
503 userurb, ep, t, d, length,
507 dev_info(&udev->dev, "ep%d %s-%s, length %u, "
509 ep, t, d, length, timeout_or_status);
511 dev_info(&udev->dev, "ep%d %s-%s, actual_length %u, "
513 ep, t, d, length, timeout_or_status);
516 data_len = min(data_len, usbfs_snoop_max);
517 if (data && data_len > 0) {
518 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
523 static void snoop_urb_data(struct urb *urb, unsigned len)
527 len = min(len, usbfs_snoop_max);
528 if (!usbfs_snoop || len == 0)
531 if (urb->num_sgs == 0) {
532 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
533 urb->transfer_buffer, len, 1);
537 for (i = 0; i < urb->num_sgs && len; i++) {
538 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
539 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
540 sg_virt(&urb->sg[i]), size, 1);
545 static int copy_urb_data_to_user(u8 __user *userbuffer, struct urb *urb)
547 unsigned i, len, size;
549 if (urb->number_of_packets > 0) /* Isochronous */
550 len = urb->transfer_buffer_length;
552 len = urb->actual_length;
554 if (urb->num_sgs == 0) {
555 if (copy_to_user(userbuffer, urb->transfer_buffer, len))
560 for (i = 0; i < urb->num_sgs && len; i++) {
561 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
562 if (copy_to_user(userbuffer, sg_virt(&urb->sg[i]), size))
571 #define AS_CONTINUATION 1
574 static void cancel_bulk_urbs(struct usb_dev_state *ps, unsigned bulk_addr)
581 /* Mark all the pending URBs that match bulk_addr, up to but not
582 * including the first one without AS_CONTINUATION. If such an
583 * URB is encountered then a new transfer has already started so
584 * the endpoint doesn't need to be disabled; otherwise it does.
586 list_for_each_entry(as, &ps->async_pending, asynclist) {
587 if (as->bulk_addr == bulk_addr) {
588 if (as->bulk_status != AS_CONTINUATION)
590 as->bulk_status = AS_UNLINK;
594 ps->disabled_bulk_eps |= (1 << bulk_addr);
596 /* Now carefully unlink all the marked pending URBs */
598 list_for_each_entry_reverse(as, &ps->async_pending, asynclist) {
599 if (as->bulk_status == AS_UNLINK) {
600 as->bulk_status = 0; /* Only once */
603 spin_unlock(&ps->lock); /* Allow completions */
606 spin_lock(&ps->lock);
612 static void async_completed(struct urb *urb)
614 struct async *as = urb->context;
615 struct usb_dev_state *ps = as->ps;
616 struct pid *pid = NULL;
617 const struct cred *cred = NULL;
622 spin_lock_irqsave(&ps->lock, flags);
623 list_move_tail(&as->asynclist, &ps->async_completed);
624 as->status = urb->status;
628 addr = as->userurb_sigval;
629 pid = get_pid(as->pid);
630 cred = get_cred(as->cred);
632 snoop(&urb->dev->dev, "urb complete\n");
633 snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length,
634 as->status, COMPLETE, NULL, 0);
635 if (usb_urb_dir_in(urb))
636 snoop_urb_data(urb, urb->actual_length);
638 if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET &&
639 as->status != -ENOENT)
640 cancel_bulk_urbs(ps, as->bulk_addr);
643 spin_unlock_irqrestore(&ps->lock, flags);
646 kill_pid_usb_asyncio(signr, errno, addr, pid, cred);
652 static void destroy_async(struct usb_dev_state *ps, struct list_head *list)
658 spin_lock_irqsave(&ps->lock, flags);
659 while (!list_empty(list)) {
660 as = list_last_entry(list, struct async, asynclist);
661 list_del_init(&as->asynclist);
665 /* drop the spinlock so the completion handler can run */
666 spin_unlock_irqrestore(&ps->lock, flags);
669 spin_lock_irqsave(&ps->lock, flags);
671 spin_unlock_irqrestore(&ps->lock, flags);
674 static void destroy_async_on_interface(struct usb_dev_state *ps,
677 struct list_head *p, *q, hitlist;
680 INIT_LIST_HEAD(&hitlist);
681 spin_lock_irqsave(&ps->lock, flags);
682 list_for_each_safe(p, q, &ps->async_pending)
683 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
684 list_move_tail(p, &hitlist);
685 spin_unlock_irqrestore(&ps->lock, flags);
686 destroy_async(ps, &hitlist);
689 static void destroy_all_async(struct usb_dev_state *ps)
691 destroy_async(ps, &ps->async_pending);
695 * interface claims are made only at the request of user level code,
696 * which can also release them (explicitly or by closing files).
697 * they're also undone when devices disconnect.
700 static int driver_probe(struct usb_interface *intf,
701 const struct usb_device_id *id)
706 static void driver_disconnect(struct usb_interface *intf)
708 struct usb_dev_state *ps = usb_get_intfdata(intf);
709 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
714 /* NOTE: this relies on usbcore having canceled and completed
715 * all pending I/O requests; 2.6 does that.
718 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
719 clear_bit(ifnum, &ps->ifclaimed);
721 dev_warn(&intf->dev, "interface number %u out of range\n",
724 usb_set_intfdata(intf, NULL);
726 /* force async requests to complete */
727 destroy_async_on_interface(ps, ifnum);
730 /* We don't care about suspend/resume of claimed interfaces */
731 static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
736 static int driver_resume(struct usb_interface *intf)
741 /* The following routines apply to the entire device, not interfaces */
742 void usbfs_notify_suspend(struct usb_device *udev)
744 /* We don't need to handle this */
747 void usbfs_notify_resume(struct usb_device *udev)
749 struct usb_dev_state *ps;
751 /* Protect against simultaneous remove or release */
752 mutex_lock(&usbfs_mutex);
753 list_for_each_entry(ps, &udev->filelist, list) {
754 WRITE_ONCE(ps->not_yet_resumed, 0);
755 wake_up_all(&ps->wait_for_resume);
757 mutex_unlock(&usbfs_mutex);
760 struct usb_driver usbfs_driver = {
762 .probe = driver_probe,
763 .disconnect = driver_disconnect,
764 .suspend = driver_suspend,
765 .resume = driver_resume,
766 .supports_autosuspend = 1,
769 static int claimintf(struct usb_dev_state *ps, unsigned int ifnum)
771 struct usb_device *dev = ps->dev;
772 struct usb_interface *intf;
775 if (ifnum >= 8*sizeof(ps->ifclaimed))
777 /* already claimed */
778 if (test_bit(ifnum, &ps->ifclaimed))
781 if (ps->privileges_dropped &&
782 !test_bit(ifnum, &ps->interface_allowed_mask))
785 intf = usb_ifnum_to_if(dev, ifnum);
789 unsigned int old_suppress;
791 /* suppress uevents while claiming interface */
792 old_suppress = dev_get_uevent_suppress(&intf->dev);
793 dev_set_uevent_suppress(&intf->dev, 1);
794 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
795 dev_set_uevent_suppress(&intf->dev, old_suppress);
798 set_bit(ifnum, &ps->ifclaimed);
802 static int releaseintf(struct usb_dev_state *ps, unsigned int ifnum)
804 struct usb_device *dev;
805 struct usb_interface *intf;
809 if (ifnum >= 8*sizeof(ps->ifclaimed))
812 intf = usb_ifnum_to_if(dev, ifnum);
815 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
816 unsigned int old_suppress;
818 /* suppress uevents while releasing interface */
819 old_suppress = dev_get_uevent_suppress(&intf->dev);
820 dev_set_uevent_suppress(&intf->dev, 1);
821 usb_driver_release_interface(&usbfs_driver, intf);
822 dev_set_uevent_suppress(&intf->dev, old_suppress);
828 static int checkintf(struct usb_dev_state *ps, unsigned int ifnum)
830 if (ps->dev->state != USB_STATE_CONFIGURED)
831 return -EHOSTUNREACH;
832 if (ifnum >= 8*sizeof(ps->ifclaimed))
834 if (test_bit(ifnum, &ps->ifclaimed))
836 /* if not yet claimed, claim it for the driver */
837 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
838 "interface %u before use\n", task_pid_nr(current),
839 current->comm, ifnum);
840 return claimintf(ps, ifnum);
843 static int findintfep(struct usb_device *dev, unsigned int ep)
845 unsigned int i, j, e;
846 struct usb_interface *intf;
847 struct usb_host_interface *alts;
848 struct usb_endpoint_descriptor *endpt;
850 if (ep & ~(USB_DIR_IN|0xf))
854 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
855 intf = dev->actconfig->interface[i];
856 for (j = 0; j < intf->num_altsetting; j++) {
857 alts = &intf->altsetting[j];
858 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
859 endpt = &alts->endpoint[e].desc;
860 if (endpt->bEndpointAddress == ep)
861 return alts->desc.bInterfaceNumber;
868 static int check_ctrlrecip(struct usb_dev_state *ps, unsigned int requesttype,
869 unsigned int request, unsigned int index)
872 struct usb_host_interface *alt_setting;
874 if (ps->dev->state != USB_STATE_UNAUTHENTICATED
875 && ps->dev->state != USB_STATE_ADDRESS
876 && ps->dev->state != USB_STATE_CONFIGURED)
877 return -EHOSTUNREACH;
878 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
882 * check for the special corner case 'get_device_id' in the printer
883 * class specification, which we always want to allow as it is used
884 * to query things like ink level, etc.
886 if (requesttype == 0xa1 && request == 0) {
887 alt_setting = usb_find_alt_setting(ps->dev->actconfig,
888 index >> 8, index & 0xff);
890 && alt_setting->desc.bInterfaceClass == USB_CLASS_PRINTER)
895 switch (requesttype & USB_RECIP_MASK) {
896 case USB_RECIP_ENDPOINT:
897 if ((index & ~USB_DIR_IN) == 0)
899 ret = findintfep(ps->dev, index);
902 * Some not fully compliant Win apps seem to get
903 * index wrong and have the endpoint number here
904 * rather than the endpoint address (with the
905 * correct direction). Win does let this through,
906 * so we'll not reject it here but leave it to
907 * the device to not break KVM. But we warn.
909 ret = findintfep(ps->dev, index ^ 0x80);
911 dev_info(&ps->dev->dev,
912 "%s: process %i (%s) requesting ep %02x but needs %02x\n",
913 __func__, task_pid_nr(current),
914 current->comm, index, index ^ 0x80);
917 ret = checkintf(ps, ret);
920 case USB_RECIP_INTERFACE:
921 ret = checkintf(ps, index);
927 static struct usb_host_endpoint *ep_to_host_endpoint(struct usb_device *dev,
930 if (ep & USB_ENDPOINT_DIR_MASK)
931 return dev->ep_in[ep & USB_ENDPOINT_NUMBER_MASK];
933 return dev->ep_out[ep & USB_ENDPOINT_NUMBER_MASK];
936 static int parse_usbdevfs_streams(struct usb_dev_state *ps,
937 struct usbdevfs_streams __user *streams,
938 unsigned int *num_streams_ret,
939 unsigned int *num_eps_ret,
940 struct usb_host_endpoint ***eps_ret,
941 struct usb_interface **intf_ret)
943 unsigned int i, num_streams, num_eps;
944 struct usb_host_endpoint **eps;
945 struct usb_interface *intf = NULL;
949 if (get_user(num_streams, &streams->num_streams) ||
950 get_user(num_eps, &streams->num_eps))
953 if (num_eps < 1 || num_eps > USB_MAXENDPOINTS)
956 /* The XHCI controller allows max 2 ^ 16 streams */
957 if (num_streams_ret && (num_streams < 2 || num_streams > 65536))
960 eps = kmalloc_array(num_eps, sizeof(*eps), GFP_KERNEL);
964 for (i = 0; i < num_eps; i++) {
965 if (get_user(ep, &streams->eps[i])) {
969 eps[i] = ep_to_host_endpoint(ps->dev, ep);
975 /* usb_alloc/free_streams operate on an usb_interface */
976 ifnum = findintfep(ps->dev, ep);
983 ret = checkintf(ps, ifnum);
986 intf = usb_ifnum_to_if(ps->dev, ifnum);
988 /* Verify all eps belong to the same interface */
989 if (ifnum != intf->altsetting->desc.bInterfaceNumber) {
997 *num_streams_ret = num_streams;
998 *num_eps_ret = num_eps;
1009 static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
1013 dev = bus_find_device_by_devt(&usb_bus_type, devt);
1016 return to_usb_device(dev);
1022 static int usbdev_open(struct inode *inode, struct file *file)
1024 struct usb_device *dev = NULL;
1025 struct usb_dev_state *ps;
1029 ps = kzalloc(sizeof(struct usb_dev_state), GFP_KERNEL);
1035 /* usbdev device-node */
1036 if (imajor(inode) == USB_DEVICE_MAJOR)
1037 dev = usbdev_lookup_by_devt(inode->i_rdev);
1041 usb_lock_device(dev);
1042 if (dev->state == USB_STATE_NOTATTACHED)
1043 goto out_unlock_device;
1045 ret = usb_autoresume_device(dev);
1047 goto out_unlock_device;
1051 ps->interface_allowed_mask = 0xFFFFFFFF; /* 32 bits */
1052 spin_lock_init(&ps->lock);
1053 INIT_LIST_HEAD(&ps->list);
1054 INIT_LIST_HEAD(&ps->async_pending);
1055 INIT_LIST_HEAD(&ps->async_completed);
1056 INIT_LIST_HEAD(&ps->memory_list);
1057 init_waitqueue_head(&ps->wait);
1058 init_waitqueue_head(&ps->wait_for_resume);
1059 ps->disc_pid = get_pid(task_pid(current));
1060 ps->cred = get_current_cred();
1063 /* Can't race with resume; the device is already active */
1064 list_add_tail(&ps->list, &dev->filelist);
1065 file->private_data = ps;
1066 usb_unlock_device(dev);
1067 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
1072 usb_unlock_device(dev);
1079 static int usbdev_release(struct inode *inode, struct file *file)
1081 struct usb_dev_state *ps = file->private_data;
1082 struct usb_device *dev = ps->dev;
1086 usb_lock_device(dev);
1087 usb_hub_release_all_ports(dev, ps);
1089 /* Protect against simultaneous resume */
1090 mutex_lock(&usbfs_mutex);
1091 list_del_init(&ps->list);
1092 mutex_unlock(&usbfs_mutex);
1094 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
1096 if (test_bit(ifnum, &ps->ifclaimed))
1097 releaseintf(ps, ifnum);
1099 destroy_all_async(ps);
1100 if (!ps->suspend_allowed)
1101 usb_autosuspend_device(dev);
1102 usb_unlock_device(dev);
1104 put_pid(ps->disc_pid);
1107 as = async_getcompleted(ps);
1110 as = async_getcompleted(ps);
1117 static void usbfs_blocking_completion(struct urb *urb)
1119 complete((struct completion *) urb->context);
1123 * Much like usb_start_wait_urb, but returns status separately from
1124 * actual_length and uses a killable wait.
1126 static int usbfs_start_wait_urb(struct urb *urb, int timeout,
1127 unsigned int *actlen)
1129 DECLARE_COMPLETION_ONSTACK(ctx);
1130 unsigned long expire;
1133 urb->context = &ctx;
1134 urb->complete = usbfs_blocking_completion;
1136 rc = usb_submit_urb(urb, GFP_KERNEL);
1140 expire = (timeout ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT);
1141 rc = wait_for_completion_killable_timeout(&ctx, expire);
1144 *actlen = urb->actual_length;
1145 if (urb->status != -ENOENT)
1146 ; /* Completed before it was killed */
1152 *actlen = urb->actual_length;
1156 static int do_proc_control(struct usb_dev_state *ps,
1157 struct usbdevfs_ctrltransfer *ctrl)
1159 struct usb_device *dev = ps->dev;
1161 unsigned char *tbuf;
1162 unsigned int wLength, actlen;
1164 struct urb *urb = NULL;
1165 struct usb_ctrlrequest *dr = NULL;
1167 ret = check_ctrlrecip(ps, ctrl->bRequestType, ctrl->bRequest,
1171 wLength = ctrl->wLength; /* To suppress 64k PAGE_SIZE warning */
1172 if (wLength > PAGE_SIZE)
1174 ret = usbfs_increase_memory_usage(PAGE_SIZE + sizeof(struct urb) +
1175 sizeof(struct usb_ctrlrequest));
1180 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
1183 urb = usb_alloc_urb(0, GFP_NOIO);
1186 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
1190 dr->bRequestType = ctrl->bRequestType;
1191 dr->bRequest = ctrl->bRequest;
1192 dr->wValue = cpu_to_le16(ctrl->wValue);
1193 dr->wIndex = cpu_to_le16(ctrl->wIndex);
1194 dr->wLength = cpu_to_le16(ctrl->wLength);
1196 tmo = ctrl->timeout;
1197 snoop(&dev->dev, "control urb: bRequestType=%02x "
1198 "bRequest=%02x wValue=%04x "
1199 "wIndex=%04x wLength=%04x\n",
1200 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1201 ctrl->wIndex, ctrl->wLength);
1203 if ((ctrl->bRequestType & USB_DIR_IN) && wLength) {
1204 pipe = usb_rcvctrlpipe(dev, 0);
1205 usb_fill_control_urb(urb, dev, pipe, (unsigned char *) dr, tbuf,
1206 wLength, NULL, NULL);
1207 snoop_urb(dev, NULL, pipe, wLength, tmo, SUBMIT, NULL, 0);
1209 usb_unlock_device(dev);
1210 i = usbfs_start_wait_urb(urb, tmo, &actlen);
1212 /* Linger a bit, prior to the next control message. */
1213 if (dev->quirks & USB_QUIRK_DELAY_CTRL_MSG)
1215 usb_lock_device(dev);
1216 snoop_urb(dev, NULL, pipe, actlen, i, COMPLETE, tbuf, actlen);
1218 if (copy_to_user(ctrl->data, tbuf, actlen)) {
1225 if (copy_from_user(tbuf, ctrl->data, wLength)) {
1230 pipe = usb_sndctrlpipe(dev, 0);
1231 usb_fill_control_urb(urb, dev, pipe, (unsigned char *) dr, tbuf,
1232 wLength, NULL, NULL);
1233 snoop_urb(dev, NULL, pipe, wLength, tmo, SUBMIT, tbuf, wLength);
1235 usb_unlock_device(dev);
1236 i = usbfs_start_wait_urb(urb, tmo, &actlen);
1238 /* Linger a bit, prior to the next control message. */
1239 if (dev->quirks & USB_QUIRK_DELAY_CTRL_MSG)
1241 usb_lock_device(dev);
1242 snoop_urb(dev, NULL, pipe, actlen, i, COMPLETE, NULL, 0);
1244 if (i < 0 && i != -EPIPE) {
1245 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
1246 "failed cmd %s rqt %u rq %u len %u ret %d\n",
1247 current->comm, ctrl->bRequestType, ctrl->bRequest,
1250 ret = (i < 0 ? i : actlen);
1255 free_page((unsigned long) tbuf);
1256 usbfs_decrease_memory_usage(PAGE_SIZE + sizeof(struct urb) +
1257 sizeof(struct usb_ctrlrequest));
1261 static int proc_control(struct usb_dev_state *ps, void __user *arg)
1263 struct usbdevfs_ctrltransfer ctrl;
1265 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1267 return do_proc_control(ps, &ctrl);
1270 static int do_proc_bulk(struct usb_dev_state *ps,
1271 struct usbdevfs_bulktransfer *bulk)
1273 struct usb_device *dev = ps->dev;
1274 unsigned int tmo, len1, len2, pipe;
1275 unsigned char *tbuf;
1277 struct urb *urb = NULL;
1278 struct usb_host_endpoint *ep;
1280 ret = findintfep(ps->dev, bulk->ep);
1283 ret = checkintf(ps, ret);
1288 if (len1 < 0 || len1 >= (INT_MAX - sizeof(struct urb)))
1291 if (bulk->ep & USB_DIR_IN)
1292 pipe = usb_rcvbulkpipe(dev, bulk->ep & 0x7f);
1294 pipe = usb_sndbulkpipe(dev, bulk->ep & 0x7f);
1295 ep = usb_pipe_endpoint(dev, pipe);
1296 if (!ep || !usb_endpoint_maxp(&ep->desc))
1298 ret = usbfs_increase_memory_usage(len1 + sizeof(struct urb));
1303 * len1 can be almost arbitrarily large. Don't WARN if it's
1304 * too big, just fail the request.
1307 tbuf = kmalloc(len1, GFP_KERNEL | __GFP_NOWARN);
1310 urb = usb_alloc_urb(0, GFP_KERNEL);
1314 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
1315 USB_ENDPOINT_XFER_INT) {
1316 pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
1317 usb_fill_int_urb(urb, dev, pipe, tbuf, len1,
1318 NULL, NULL, ep->desc.bInterval);
1320 usb_fill_bulk_urb(urb, dev, pipe, tbuf, len1, NULL, NULL);
1323 tmo = bulk->timeout;
1324 if (bulk->ep & 0x80) {
1325 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, NULL, 0);
1327 usb_unlock_device(dev);
1328 i = usbfs_start_wait_urb(urb, tmo, &len2);
1329 usb_lock_device(dev);
1330 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, tbuf, len2);
1333 if (copy_to_user(bulk->data, tbuf, len2)) {
1340 if (copy_from_user(tbuf, bulk->data, len1)) {
1345 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, tbuf, len1);
1347 usb_unlock_device(dev);
1348 i = usbfs_start_wait_urb(urb, tmo, &len2);
1349 usb_lock_device(dev);
1350 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, NULL, 0);
1352 ret = (i < 0 ? i : len2);
1356 usbfs_decrease_memory_usage(len1 + sizeof(struct urb));
1360 static int proc_bulk(struct usb_dev_state *ps, void __user *arg)
1362 struct usbdevfs_bulktransfer bulk;
1364 if (copy_from_user(&bulk, arg, sizeof(bulk)))
1366 return do_proc_bulk(ps, &bulk);
1369 static void check_reset_of_active_ep(struct usb_device *udev,
1370 unsigned int epnum, char *ioctl_name)
1372 struct usb_host_endpoint **eps;
1373 struct usb_host_endpoint *ep;
1375 eps = (epnum & USB_DIR_IN) ? udev->ep_in : udev->ep_out;
1376 ep = eps[epnum & 0x0f];
1377 if (ep && !list_empty(&ep->urb_list))
1378 dev_warn(&udev->dev, "Process %d (%s) called USBDEVFS_%s for active endpoint 0x%02x\n",
1379 task_pid_nr(current), current->comm,
1383 static int proc_resetep(struct usb_dev_state *ps, void __user *arg)
1388 if (get_user(ep, (unsigned int __user *)arg))
1390 ret = findintfep(ps->dev, ep);
1393 ret = checkintf(ps, ret);
1396 check_reset_of_active_ep(ps->dev, ep, "RESETEP");
1397 usb_reset_endpoint(ps->dev, ep);
1401 static int proc_clearhalt(struct usb_dev_state *ps, void __user *arg)
1407 if (get_user(ep, (unsigned int __user *)arg))
1409 ret = findintfep(ps->dev, ep);
1412 ret = checkintf(ps, ret);
1415 check_reset_of_active_ep(ps->dev, ep, "CLEAR_HALT");
1416 if (ep & USB_DIR_IN)
1417 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
1419 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
1421 return usb_clear_halt(ps->dev, pipe);
1424 static int proc_getdriver(struct usb_dev_state *ps, void __user *arg)
1426 struct usbdevfs_getdriver gd;
1427 struct usb_interface *intf;
1430 if (copy_from_user(&gd, arg, sizeof(gd)))
1432 intf = usb_ifnum_to_if(ps->dev, gd.interface);
1433 if (!intf || !intf->dev.driver)
1436 strscpy(gd.driver, intf->dev.driver->name,
1438 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
1443 static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg)
1445 struct usbdevfs_connectinfo ci;
1447 memset(&ci, 0, sizeof(ci));
1448 ci.devnum = ps->dev->devnum;
1449 ci.slow = ps->dev->speed == USB_SPEED_LOW;
1451 if (copy_to_user(arg, &ci, sizeof(ci)))
1456 static int proc_conninfo_ex(struct usb_dev_state *ps,
1457 void __user *arg, size_t size)
1459 struct usbdevfs_conninfo_ex ci;
1460 struct usb_device *udev = ps->dev;
1462 if (size < sizeof(ci.size))
1465 memset(&ci, 0, sizeof(ci));
1466 ci.size = sizeof(ci);
1467 ci.busnum = udev->bus->busnum;
1468 ci.devnum = udev->devnum;
1469 ci.speed = udev->speed;
1471 while (udev && udev->portnum != 0) {
1472 if (++ci.num_ports <= ARRAY_SIZE(ci.ports))
1473 ci.ports[ARRAY_SIZE(ci.ports) - ci.num_ports] =
1475 udev = udev->parent;
1478 if (ci.num_ports < ARRAY_SIZE(ci.ports))
1479 memmove(&ci.ports[0],
1480 &ci.ports[ARRAY_SIZE(ci.ports) - ci.num_ports],
1483 if (copy_to_user(arg, &ci, min(sizeof(ci), size)))
1489 static int proc_resetdevice(struct usb_dev_state *ps)
1491 struct usb_host_config *actconfig = ps->dev->actconfig;
1492 struct usb_interface *interface;
1495 /* Don't allow a device reset if the process has dropped the
1496 * privilege to do such things and any of the interfaces are
1497 * currently claimed.
1499 if (ps->privileges_dropped && actconfig) {
1500 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
1501 interface = actconfig->interface[i];
1502 number = interface->cur_altsetting->desc.bInterfaceNumber;
1503 if (usb_interface_claimed(interface) &&
1504 !test_bit(number, &ps->ifclaimed)) {
1505 dev_warn(&ps->dev->dev,
1506 "usbfs: interface %d claimed by %s while '%s' resets device\n",
1507 number, interface->dev.driver->name, current->comm);
1513 return usb_reset_device(ps->dev);
1516 static int proc_setintf(struct usb_dev_state *ps, void __user *arg)
1518 struct usbdevfs_setinterface setintf;
1521 if (copy_from_user(&setintf, arg, sizeof(setintf)))
1523 ret = checkintf(ps, setintf.interface);
1527 destroy_async_on_interface(ps, setintf.interface);
1529 return usb_set_interface(ps->dev, setintf.interface,
1530 setintf.altsetting);
1533 static int proc_setconfig(struct usb_dev_state *ps, void __user *arg)
1537 struct usb_host_config *actconfig;
1539 if (get_user(u, (int __user *)arg))
1542 actconfig = ps->dev->actconfig;
1544 /* Don't touch the device if any interfaces are claimed.
1545 * It could interfere with other drivers' operations, and if
1546 * an interface is claimed by usbfs it could easily deadlock.
1551 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
1552 if (usb_interface_claimed(actconfig->interface[i])) {
1553 dev_warn(&ps->dev->dev,
1554 "usbfs: interface %d claimed by %s "
1555 "while '%s' sets config #%d\n",
1556 actconfig->interface[i]
1558 ->desc.bInterfaceNumber,
1559 actconfig->interface[i]
1568 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
1569 * so avoid usb_set_configuration()'s kick to sysfs
1572 if (actconfig && actconfig->desc.bConfigurationValue == u)
1573 status = usb_reset_configuration(ps->dev);
1575 status = usb_set_configuration(ps->dev, u);
1581 static struct usb_memory *
1582 find_memory_area(struct usb_dev_state *ps, const struct usbdevfs_urb *uurb)
1584 struct usb_memory *usbm = NULL, *iter;
1585 unsigned long flags;
1586 unsigned long uurb_start = (unsigned long)uurb->buffer;
1588 spin_lock_irqsave(&ps->lock, flags);
1589 list_for_each_entry(iter, &ps->memory_list, memlist) {
1590 if (uurb_start >= iter->vm_start &&
1591 uurb_start < iter->vm_start + iter->size) {
1592 if (uurb->buffer_length > iter->vm_start + iter->size -
1594 usbm = ERR_PTR(-EINVAL);
1597 usbm->urb_use_count++;
1602 spin_unlock_irqrestore(&ps->lock, flags);
1606 static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb,
1607 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
1608 void __user *arg, sigval_t userurb_sigval)
1610 struct usbdevfs_iso_packet_desc *isopkt = NULL;
1611 struct usb_host_endpoint *ep;
1612 struct async *as = NULL;
1613 struct usb_ctrlrequest *dr = NULL;
1614 unsigned int u, totlen, isofrmlen;
1615 int i, ret, num_sgs = 0, ifnum = -1;
1616 int number_of_packets = 0;
1617 unsigned int stream_id = 0;
1620 bool allow_short = false;
1621 bool allow_zero = false;
1622 unsigned long mask = USBDEVFS_URB_SHORT_NOT_OK |
1623 USBDEVFS_URB_BULK_CONTINUATION |
1624 USBDEVFS_URB_NO_FSBR |
1625 USBDEVFS_URB_ZERO_PACKET |
1626 USBDEVFS_URB_NO_INTERRUPT;
1627 /* USBDEVFS_URB_ISO_ASAP is a special case */
1628 if (uurb->type == USBDEVFS_URB_TYPE_ISO)
1629 mask |= USBDEVFS_URB_ISO_ASAP;
1631 if (uurb->flags & ~mask)
1634 if ((unsigned int)uurb->buffer_length >= USBFS_XFER_MAX)
1636 if (uurb->buffer_length > 0 && !uurb->buffer)
1638 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
1639 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
1640 ifnum = findintfep(ps->dev, uurb->endpoint);
1643 ret = checkintf(ps, ifnum);
1647 ep = ep_to_host_endpoint(ps->dev, uurb->endpoint);
1650 is_in = (uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0;
1653 switch (uurb->type) {
1654 case USBDEVFS_URB_TYPE_CONTROL:
1655 if (!usb_endpoint_xfer_control(&ep->desc))
1657 /* min 8 byte setup packet */
1658 if (uurb->buffer_length < 8)
1660 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1663 if (copy_from_user(dr, uurb->buffer, 8)) {
1667 if (uurb->buffer_length < (le16_to_cpu(dr->wLength) + 8)) {
1671 ret = check_ctrlrecip(ps, dr->bRequestType, dr->bRequest,
1672 le16_to_cpu(dr->wIndex));
1675 uurb->buffer_length = le16_to_cpu(dr->wLength);
1677 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1679 uurb->endpoint |= USB_DIR_IN;
1682 uurb->endpoint &= ~USB_DIR_IN;
1686 snoop(&ps->dev->dev, "control urb: bRequestType=%02x "
1687 "bRequest=%02x wValue=%04x "
1688 "wIndex=%04x wLength=%04x\n",
1689 dr->bRequestType, dr->bRequest,
1690 __le16_to_cpu(dr->wValue),
1691 __le16_to_cpu(dr->wIndex),
1692 __le16_to_cpu(dr->wLength));
1693 u = sizeof(struct usb_ctrlrequest);
1696 case USBDEVFS_URB_TYPE_BULK:
1701 switch (usb_endpoint_type(&ep->desc)) {
1702 case USB_ENDPOINT_XFER_CONTROL:
1703 case USB_ENDPOINT_XFER_ISOC:
1705 case USB_ENDPOINT_XFER_INT:
1706 /* allow single-shot interrupt transfers */
1707 uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
1710 num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE);
1711 if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize)
1714 stream_id = uurb->stream_id;
1717 case USBDEVFS_URB_TYPE_INTERRUPT:
1718 if (!usb_endpoint_xfer_int(&ep->desc))
1727 case USBDEVFS_URB_TYPE_ISO:
1728 /* arbitrary limit */
1729 if (uurb->number_of_packets < 1 ||
1730 uurb->number_of_packets > 128)
1732 if (!usb_endpoint_xfer_isoc(&ep->desc))
1734 number_of_packets = uurb->number_of_packets;
1735 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1737 isopkt = memdup_user(iso_frame_desc, isofrmlen);
1738 if (IS_ERR(isopkt)) {
1739 ret = PTR_ERR(isopkt);
1743 for (totlen = u = 0; u < number_of_packets; u++) {
1745 * arbitrary limit need for USB 3.1 Gen2
1746 * sizemax: 96 DPs at SSP, 96 * 1024 = 98304
1748 if (isopkt[u].length > 98304) {
1752 totlen += isopkt[u].length;
1754 u *= sizeof(struct usb_iso_packet_descriptor);
1755 uurb->buffer_length = totlen;
1762 if (uurb->buffer_length > 0 &&
1763 !access_ok(uurb->buffer, uurb->buffer_length)) {
1767 as = alloc_async(number_of_packets);
1773 as->usbm = find_memory_area(ps, uurb);
1774 if (IS_ERR(as->usbm)) {
1775 ret = PTR_ERR(as->usbm);
1780 /* do not use SG buffers when memory mapped segments
1786 u += sizeof(struct async) + sizeof(struct urb) +
1787 (as->usbm ? 0 : uurb->buffer_length) +
1788 num_sgs * sizeof(struct scatterlist);
1789 ret = usbfs_increase_memory_usage(u);
1795 as->urb->sg = kmalloc_array(num_sgs,
1796 sizeof(struct scatterlist),
1797 GFP_KERNEL | __GFP_NOWARN);
1802 as->urb->num_sgs = num_sgs;
1803 sg_init_table(as->urb->sg, as->urb->num_sgs);
1805 totlen = uurb->buffer_length;
1806 for (i = 0; i < as->urb->num_sgs; i++) {
1807 u = (totlen > USB_SG_SIZE) ? USB_SG_SIZE : totlen;
1808 buf = kmalloc(u, GFP_KERNEL);
1813 sg_set_buf(&as->urb->sg[i], buf, u);
1816 if (copy_from_user(buf, uurb->buffer, u)) {
1824 } else if (uurb->buffer_length > 0) {
1826 unsigned long uurb_start = (unsigned long)uurb->buffer;
1828 as->urb->transfer_buffer = as->usbm->mem +
1829 (uurb_start - as->usbm->vm_start);
1831 as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
1832 GFP_KERNEL | __GFP_NOWARN);
1833 if (!as->urb->transfer_buffer) {
1838 if (copy_from_user(as->urb->transfer_buffer,
1840 uurb->buffer_length)) {
1844 } else if (uurb->type == USBDEVFS_URB_TYPE_ISO) {
1846 * Isochronous input data may end up being
1847 * discontiguous if some of the packets are
1848 * short. Clear the buffer so that the gaps
1849 * don't leak kernel data to userspace.
1851 memset(as->urb->transfer_buffer, 0,
1852 uurb->buffer_length);
1856 as->urb->dev = ps->dev;
1857 as->urb->pipe = (uurb->type << 30) |
1858 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1859 (uurb->endpoint & USB_DIR_IN);
1861 /* This tedious sequence is necessary because the URB_* flags
1862 * are internal to the kernel and subject to change, whereas
1863 * the USBDEVFS_URB_* flags are a user API and must not be changed.
1865 u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
1866 if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
1868 if (allow_short && uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
1869 u |= URB_SHORT_NOT_OK;
1870 if (allow_zero && uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1871 u |= URB_ZERO_PACKET;
1872 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
1873 u |= URB_NO_INTERRUPT;
1874 as->urb->transfer_flags = u;
1876 if (!allow_short && uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
1877 dev_warn(&ps->dev->dev, "Requested nonsensical USBDEVFS_URB_SHORT_NOT_OK.\n");
1878 if (!allow_zero && uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1879 dev_warn(&ps->dev->dev, "Requested nonsensical USBDEVFS_URB_ZERO_PACKET.\n");
1881 as->urb->transfer_buffer_length = uurb->buffer_length;
1882 as->urb->setup_packet = (unsigned char *)dr;
1884 as->urb->start_frame = uurb->start_frame;
1885 as->urb->number_of_packets = number_of_packets;
1886 as->urb->stream_id = stream_id;
1888 if (ep->desc.bInterval) {
1889 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1890 ps->dev->speed == USB_SPEED_HIGH ||
1891 ps->dev->speed >= USB_SPEED_SUPER)
1892 as->urb->interval = 1 <<
1893 min(15, ep->desc.bInterval - 1);
1895 as->urb->interval = ep->desc.bInterval;
1898 as->urb->context = as;
1899 as->urb->complete = async_completed;
1900 for (totlen = u = 0; u < number_of_packets; u++) {
1901 as->urb->iso_frame_desc[u].offset = totlen;
1902 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1903 totlen += isopkt[u].length;
1909 as->userurb_sigval = userurb_sigval;
1911 unsigned long uurb_start = (unsigned long)uurb->buffer;
1913 as->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1914 as->urb->transfer_dma = as->usbm->dma_handle +
1915 (uurb_start - as->usbm->vm_start);
1916 } else if (is_in && uurb->buffer_length > 0)
1917 as->userbuffer = uurb->buffer;
1918 as->signr = uurb->signr;
1920 as->pid = get_pid(task_pid(current));
1921 as->cred = get_current_cred();
1922 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1923 as->urb->transfer_buffer_length, 0, SUBMIT,
1926 snoop_urb_data(as->urb, as->urb->transfer_buffer_length);
1928 async_newpending(as);
1930 if (usb_endpoint_xfer_bulk(&ep->desc)) {
1931 spin_lock_irq(&ps->lock);
1933 /* Not exactly the endpoint address; the direction bit is
1934 * shifted to the 0x10 position so that the value will be
1937 as->bulk_addr = usb_endpoint_num(&ep->desc) |
1938 ((ep->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1941 /* If this bulk URB is the start of a new transfer, re-enable
1942 * the endpoint. Otherwise mark it as a continuation URB.
1944 if (uurb->flags & USBDEVFS_URB_BULK_CONTINUATION)
1945 as->bulk_status = AS_CONTINUATION;
1947 ps->disabled_bulk_eps &= ~(1 << as->bulk_addr);
1949 /* Don't accept continuation URBs if the endpoint is
1950 * disabled because of an earlier error.
1952 if (ps->disabled_bulk_eps & (1 << as->bulk_addr))
1955 ret = usb_submit_urb(as->urb, GFP_ATOMIC);
1956 spin_unlock_irq(&ps->lock);
1958 ret = usb_submit_urb(as->urb, GFP_KERNEL);
1962 dev_printk(KERN_DEBUG, &ps->dev->dev,
1963 "usbfs: usb_submit_urb returned %d\n", ret);
1964 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1965 0, ret, COMPLETE, NULL, 0);
1966 async_removepending(as);
1979 static int proc_submiturb(struct usb_dev_state *ps, void __user *arg)
1981 struct usbdevfs_urb uurb;
1982 sigval_t userurb_sigval;
1984 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1987 memset(&userurb_sigval, 0, sizeof(userurb_sigval));
1988 userurb_sigval.sival_ptr = arg;
1990 return proc_do_submiturb(ps, &uurb,
1991 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1992 arg, userurb_sigval);
1995 static int proc_unlinkurb(struct usb_dev_state *ps, void __user *arg)
1999 unsigned long flags;
2001 spin_lock_irqsave(&ps->lock, flags);
2002 as = async_getpending(ps, arg);
2004 spin_unlock_irqrestore(&ps->lock, flags);
2010 spin_unlock_irqrestore(&ps->lock, flags);
2018 static void compute_isochronous_actual_length(struct urb *urb)
2022 if (urb->number_of_packets > 0) {
2023 urb->actual_length = 0;
2024 for (i = 0; i < urb->number_of_packets; i++)
2025 urb->actual_length +=
2026 urb->iso_frame_desc[i].actual_length;
2030 static int processcompl(struct async *as, void __user * __user *arg)
2032 struct urb *urb = as->urb;
2033 struct usbdevfs_urb __user *userurb = as->userurb;
2034 void __user *addr = as->userurb;
2037 compute_isochronous_actual_length(urb);
2038 if (as->userbuffer && urb->actual_length) {
2039 if (copy_urb_data_to_user(as->userbuffer, urb))
2042 if (put_user(as->status, &userurb->status))
2044 if (put_user(urb->actual_length, &userurb->actual_length))
2046 if (put_user(urb->error_count, &userurb->error_count))
2049 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
2050 for (i = 0; i < urb->number_of_packets; i++) {
2051 if (put_user(urb->iso_frame_desc[i].actual_length,
2052 &userurb->iso_frame_desc[i].actual_length))
2054 if (put_user(urb->iso_frame_desc[i].status,
2055 &userurb->iso_frame_desc[i].status))
2060 if (put_user(addr, (void __user * __user *)arg))
2068 static struct async *reap_as(struct usb_dev_state *ps)
2070 DECLARE_WAITQUEUE(wait, current);
2071 struct async *as = NULL;
2072 struct usb_device *dev = ps->dev;
2074 add_wait_queue(&ps->wait, &wait);
2076 __set_current_state(TASK_INTERRUPTIBLE);
2077 as = async_getcompleted(ps);
2078 if (as || !connected(ps))
2080 if (signal_pending(current))
2082 usb_unlock_device(dev);
2084 usb_lock_device(dev);
2086 remove_wait_queue(&ps->wait, &wait);
2087 set_current_state(TASK_RUNNING);
2091 static int proc_reapurb(struct usb_dev_state *ps, void __user *arg)
2093 struct async *as = reap_as(ps);
2098 snoop(&ps->dev->dev, "reap %px\n", as->userurb);
2099 retval = processcompl(as, (void __user * __user *)arg);
2103 if (signal_pending(current))
2108 static int proc_reapurbnonblock(struct usb_dev_state *ps, void __user *arg)
2113 as = async_getcompleted(ps);
2115 snoop(&ps->dev->dev, "reap %px\n", as->userurb);
2116 retval = processcompl(as, (void __user * __user *)arg);
2119 retval = (connected(ps) ? -EAGAIN : -ENODEV);
2124 #ifdef CONFIG_COMPAT
2125 static int proc_control_compat(struct usb_dev_state *ps,
2126 struct usbdevfs_ctrltransfer32 __user *p32)
2128 struct usbdevfs_ctrltransfer ctrl;
2131 if (copy_from_user(&ctrl, p32, sizeof(*p32) - sizeof(compat_caddr_t)) ||
2132 get_user(udata, &p32->data))
2134 ctrl.data = compat_ptr(udata);
2135 return do_proc_control(ps, &ctrl);
2138 static int proc_bulk_compat(struct usb_dev_state *ps,
2139 struct usbdevfs_bulktransfer32 __user *p32)
2141 struct usbdevfs_bulktransfer bulk;
2142 compat_caddr_t addr;
2144 if (get_user(bulk.ep, &p32->ep) ||
2145 get_user(bulk.len, &p32->len) ||
2146 get_user(bulk.timeout, &p32->timeout) ||
2147 get_user(addr, &p32->data))
2149 bulk.data = compat_ptr(addr);
2150 return do_proc_bulk(ps, &bulk);
2153 static int proc_disconnectsignal_compat(struct usb_dev_state *ps, void __user *arg)
2155 struct usbdevfs_disconnectsignal32 ds;
2157 if (copy_from_user(&ds, arg, sizeof(ds)))
2159 ps->discsignr = ds.signr;
2160 ps->disccontext.sival_int = ds.context;
2164 static int get_urb32(struct usbdevfs_urb *kurb,
2165 struct usbdevfs_urb32 __user *uurb)
2167 struct usbdevfs_urb32 urb32;
2168 if (copy_from_user(&urb32, uurb, sizeof(*uurb)))
2170 kurb->type = urb32.type;
2171 kurb->endpoint = urb32.endpoint;
2172 kurb->status = urb32.status;
2173 kurb->flags = urb32.flags;
2174 kurb->buffer = compat_ptr(urb32.buffer);
2175 kurb->buffer_length = urb32.buffer_length;
2176 kurb->actual_length = urb32.actual_length;
2177 kurb->start_frame = urb32.start_frame;
2178 kurb->number_of_packets = urb32.number_of_packets;
2179 kurb->error_count = urb32.error_count;
2180 kurb->signr = urb32.signr;
2181 kurb->usercontext = compat_ptr(urb32.usercontext);
2185 static int proc_submiturb_compat(struct usb_dev_state *ps, void __user *arg)
2187 struct usbdevfs_urb uurb;
2188 sigval_t userurb_sigval;
2190 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
2193 memset(&userurb_sigval, 0, sizeof(userurb_sigval));
2194 userurb_sigval.sival_int = ptr_to_compat(arg);
2196 return proc_do_submiturb(ps, &uurb,
2197 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
2198 arg, userurb_sigval);
2201 static int processcompl_compat(struct async *as, void __user * __user *arg)
2203 struct urb *urb = as->urb;
2204 struct usbdevfs_urb32 __user *userurb = as->userurb;
2205 void __user *addr = as->userurb;
2208 compute_isochronous_actual_length(urb);
2209 if (as->userbuffer && urb->actual_length) {
2210 if (copy_urb_data_to_user(as->userbuffer, urb))
2213 if (put_user(as->status, &userurb->status))
2215 if (put_user(urb->actual_length, &userurb->actual_length))
2217 if (put_user(urb->error_count, &userurb->error_count))
2220 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
2221 for (i = 0; i < urb->number_of_packets; i++) {
2222 if (put_user(urb->iso_frame_desc[i].actual_length,
2223 &userurb->iso_frame_desc[i].actual_length))
2225 if (put_user(urb->iso_frame_desc[i].status,
2226 &userurb->iso_frame_desc[i].status))
2231 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
2236 static int proc_reapurb_compat(struct usb_dev_state *ps, void __user *arg)
2238 struct async *as = reap_as(ps);
2243 snoop(&ps->dev->dev, "reap %px\n", as->userurb);
2244 retval = processcompl_compat(as, (void __user * __user *)arg);
2248 if (signal_pending(current))
2253 static int proc_reapurbnonblock_compat(struct usb_dev_state *ps, void __user *arg)
2258 as = async_getcompleted(ps);
2260 snoop(&ps->dev->dev, "reap %px\n", as->userurb);
2261 retval = processcompl_compat(as, (void __user * __user *)arg);
2264 retval = (connected(ps) ? -EAGAIN : -ENODEV);
2272 static int proc_disconnectsignal(struct usb_dev_state *ps, void __user *arg)
2274 struct usbdevfs_disconnectsignal ds;
2276 if (copy_from_user(&ds, arg, sizeof(ds)))
2278 ps->discsignr = ds.signr;
2279 ps->disccontext.sival_ptr = ds.context;
2283 static int proc_claiminterface(struct usb_dev_state *ps, void __user *arg)
2287 if (get_user(ifnum, (unsigned int __user *)arg))
2289 return claimintf(ps, ifnum);
2292 static int proc_releaseinterface(struct usb_dev_state *ps, void __user *arg)
2297 if (get_user(ifnum, (unsigned int __user *)arg))
2299 ret = releaseintf(ps, ifnum);
2302 destroy_async_on_interface(ps, ifnum);
2306 static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl)
2311 struct usb_interface *intf = NULL;
2312 struct usb_driver *driver = NULL;
2314 if (ps->privileges_dropped)
2321 size = _IOC_SIZE(ctl->ioctl_code);
2323 buf = kmalloc(size, GFP_KERNEL);
2326 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
2327 if (copy_from_user(buf, ctl->data, size)) {
2332 memset(buf, 0, size);
2336 if (ps->dev->state != USB_STATE_CONFIGURED)
2337 retval = -EHOSTUNREACH;
2338 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
2340 else switch (ctl->ioctl_code) {
2342 /* disconnect kernel driver from interface */
2343 case USBDEVFS_DISCONNECT:
2344 if (intf->dev.driver) {
2345 driver = to_usb_driver(intf->dev.driver);
2346 dev_dbg(&intf->dev, "disconnect by usbfs\n");
2347 usb_driver_release_interface(driver, intf);
2352 /* let kernel drivers try to (re)bind to the interface */
2353 case USBDEVFS_CONNECT:
2354 if (!intf->dev.driver)
2355 retval = device_attach(&intf->dev);
2360 /* talk directly to the interface's driver */
2362 if (intf->dev.driver)
2363 driver = to_usb_driver(intf->dev.driver);
2364 if (driver == NULL || driver->unlocked_ioctl == NULL) {
2367 retval = driver->unlocked_ioctl(intf, ctl->ioctl_code, buf);
2368 if (retval == -ENOIOCTLCMD)
2373 /* cleanup and return */
2375 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
2377 && copy_to_user(ctl->data, buf, size) != 0)
2384 static int proc_ioctl_default(struct usb_dev_state *ps, void __user *arg)
2386 struct usbdevfs_ioctl ctrl;
2388 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
2390 return proc_ioctl(ps, &ctrl);
2393 #ifdef CONFIG_COMPAT
2394 static int proc_ioctl_compat(struct usb_dev_state *ps, compat_uptr_t arg)
2396 struct usbdevfs_ioctl32 ioc32;
2397 struct usbdevfs_ioctl ctrl;
2399 if (copy_from_user(&ioc32, compat_ptr(arg), sizeof(ioc32)))
2401 ctrl.ifno = ioc32.ifno;
2402 ctrl.ioctl_code = ioc32.ioctl_code;
2403 ctrl.data = compat_ptr(ioc32.data);
2404 return proc_ioctl(ps, &ctrl);
2408 static int proc_claim_port(struct usb_dev_state *ps, void __user *arg)
2413 if (get_user(portnum, (unsigned __user *) arg))
2415 rc = usb_hub_claim_port(ps->dev, portnum, ps);
2417 snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n",
2418 portnum, task_pid_nr(current), current->comm);
2422 static int proc_release_port(struct usb_dev_state *ps, void __user *arg)
2426 if (get_user(portnum, (unsigned __user *) arg))
2428 return usb_hub_release_port(ps->dev, portnum, ps);
2431 static int proc_get_capabilities(struct usb_dev_state *ps, void __user *arg)
2435 caps = USBDEVFS_CAP_ZERO_PACKET | USBDEVFS_CAP_NO_PACKET_SIZE_LIM |
2436 USBDEVFS_CAP_REAP_AFTER_DISCONNECT | USBDEVFS_CAP_MMAP |
2437 USBDEVFS_CAP_DROP_PRIVILEGES |
2438 USBDEVFS_CAP_CONNINFO_EX | MAYBE_CAP_SUSPEND;
2439 if (!ps->dev->bus->no_stop_on_short)
2440 caps |= USBDEVFS_CAP_BULK_CONTINUATION;
2441 if (ps->dev->bus->sg_tablesize)
2442 caps |= USBDEVFS_CAP_BULK_SCATTER_GATHER;
2444 if (put_user(caps, (__u32 __user *)arg))
2450 static int proc_disconnect_claim(struct usb_dev_state *ps, void __user *arg)
2452 struct usbdevfs_disconnect_claim dc;
2453 struct usb_interface *intf;
2455 if (copy_from_user(&dc, arg, sizeof(dc)))
2458 intf = usb_ifnum_to_if(ps->dev, dc.interface);
2462 if (intf->dev.driver) {
2463 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
2465 if (ps->privileges_dropped)
2468 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_IF_DRIVER) &&
2469 strncmp(dc.driver, intf->dev.driver->name,
2470 sizeof(dc.driver)) != 0)
2473 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_EXCEPT_DRIVER) &&
2474 strncmp(dc.driver, intf->dev.driver->name,
2475 sizeof(dc.driver)) == 0)
2478 dev_dbg(&intf->dev, "disconnect by usbfs\n");
2479 usb_driver_release_interface(driver, intf);
2482 return claimintf(ps, dc.interface);
2485 static int proc_alloc_streams(struct usb_dev_state *ps, void __user *arg)
2487 unsigned num_streams, num_eps;
2488 struct usb_host_endpoint **eps;
2489 struct usb_interface *intf;
2492 r = parse_usbdevfs_streams(ps, arg, &num_streams, &num_eps,
2497 destroy_async_on_interface(ps,
2498 intf->altsetting[0].desc.bInterfaceNumber);
2500 r = usb_alloc_streams(intf, eps, num_eps, num_streams, GFP_KERNEL);
2505 static int proc_free_streams(struct usb_dev_state *ps, void __user *arg)
2508 struct usb_host_endpoint **eps;
2509 struct usb_interface *intf;
2512 r = parse_usbdevfs_streams(ps, arg, NULL, &num_eps, &eps, &intf);
2516 destroy_async_on_interface(ps,
2517 intf->altsetting[0].desc.bInterfaceNumber);
2519 r = usb_free_streams(intf, eps, num_eps, GFP_KERNEL);
2524 static int proc_drop_privileges(struct usb_dev_state *ps, void __user *arg)
2528 if (copy_from_user(&data, arg, sizeof(data)))
2531 /* This is a one way operation. Once privileges are
2532 * dropped, you cannot regain them. You may however reissue
2533 * this ioctl to shrink the allowed interfaces mask.
2535 ps->interface_allowed_mask &= data;
2536 ps->privileges_dropped = true;
2541 static int proc_forbid_suspend(struct usb_dev_state *ps)
2545 if (ps->suspend_allowed) {
2546 ret = usb_autoresume_device(ps->dev);
2548 ps->suspend_allowed = false;
2549 else if (ret != -ENODEV)
2555 static int proc_allow_suspend(struct usb_dev_state *ps)
2560 WRITE_ONCE(ps->not_yet_resumed, 1);
2561 if (!ps->suspend_allowed) {
2562 usb_autosuspend_device(ps->dev);
2563 ps->suspend_allowed = true;
2568 static int proc_wait_for_resume(struct usb_dev_state *ps)
2572 usb_unlock_device(ps->dev);
2573 ret = wait_event_interruptible(ps->wait_for_resume,
2574 READ_ONCE(ps->not_yet_resumed) == 0);
2575 usb_lock_device(ps->dev);
2579 return proc_forbid_suspend(ps);
2583 * NOTE: All requests here that have interface numbers as parameters
2584 * are assuming that somehow the configuration has been prevented from
2585 * changing. But there's no mechanism to ensure that...
2587 static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
2590 struct usb_dev_state *ps = file->private_data;
2591 struct inode *inode = file_inode(file);
2592 struct usb_device *dev = ps->dev;
2595 if (!(file->f_mode & FMODE_WRITE))
2598 usb_lock_device(dev);
2600 /* Reap operations are allowed even after disconnection */
2602 case USBDEVFS_REAPURB:
2603 snoop(&dev->dev, "%s: REAPURB\n", __func__);
2604 ret = proc_reapurb(ps, p);
2607 case USBDEVFS_REAPURBNDELAY:
2608 snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
2609 ret = proc_reapurbnonblock(ps, p);
2612 #ifdef CONFIG_COMPAT
2613 case USBDEVFS_REAPURB32:
2614 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
2615 ret = proc_reapurb_compat(ps, p);
2618 case USBDEVFS_REAPURBNDELAY32:
2619 snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
2620 ret = proc_reapurbnonblock_compat(ps, p);
2625 if (!connected(ps)) {
2626 usb_unlock_device(dev);
2631 case USBDEVFS_CONTROL:
2632 snoop(&dev->dev, "%s: CONTROL\n", __func__);
2633 ret = proc_control(ps, p);
2635 inode->i_mtime = current_time(inode);
2639 snoop(&dev->dev, "%s: BULK\n", __func__);
2640 ret = proc_bulk(ps, p);
2642 inode->i_mtime = current_time(inode);
2645 case USBDEVFS_RESETEP:
2646 snoop(&dev->dev, "%s: RESETEP\n", __func__);
2647 ret = proc_resetep(ps, p);
2649 inode->i_mtime = current_time(inode);
2652 case USBDEVFS_RESET:
2653 snoop(&dev->dev, "%s: RESET\n", __func__);
2654 ret = proc_resetdevice(ps);
2657 case USBDEVFS_CLEAR_HALT:
2658 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
2659 ret = proc_clearhalt(ps, p);
2661 inode->i_mtime = current_time(inode);
2664 case USBDEVFS_GETDRIVER:
2665 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
2666 ret = proc_getdriver(ps, p);
2669 case USBDEVFS_CONNECTINFO:
2670 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
2671 ret = proc_connectinfo(ps, p);
2674 case USBDEVFS_SETINTERFACE:
2675 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
2676 ret = proc_setintf(ps, p);
2679 case USBDEVFS_SETCONFIGURATION:
2680 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
2681 ret = proc_setconfig(ps, p);
2684 case USBDEVFS_SUBMITURB:
2685 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
2686 ret = proc_submiturb(ps, p);
2688 inode->i_mtime = current_time(inode);
2691 #ifdef CONFIG_COMPAT
2692 case USBDEVFS_CONTROL32:
2693 snoop(&dev->dev, "%s: CONTROL32\n", __func__);
2694 ret = proc_control_compat(ps, p);
2696 inode->i_mtime = current_time(inode);
2699 case USBDEVFS_BULK32:
2700 snoop(&dev->dev, "%s: BULK32\n", __func__);
2701 ret = proc_bulk_compat(ps, p);
2703 inode->i_mtime = current_time(inode);
2706 case USBDEVFS_DISCSIGNAL32:
2707 snoop(&dev->dev, "%s: DISCSIGNAL32\n", __func__);
2708 ret = proc_disconnectsignal_compat(ps, p);
2711 case USBDEVFS_SUBMITURB32:
2712 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
2713 ret = proc_submiturb_compat(ps, p);
2715 inode->i_mtime = current_time(inode);
2718 case USBDEVFS_IOCTL32:
2719 snoop(&dev->dev, "%s: IOCTL32\n", __func__);
2720 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
2724 case USBDEVFS_DISCARDURB:
2725 snoop(&dev->dev, "%s: DISCARDURB %px\n", __func__, p);
2726 ret = proc_unlinkurb(ps, p);
2729 case USBDEVFS_DISCSIGNAL:
2730 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
2731 ret = proc_disconnectsignal(ps, p);
2734 case USBDEVFS_CLAIMINTERFACE:
2735 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
2736 ret = proc_claiminterface(ps, p);
2739 case USBDEVFS_RELEASEINTERFACE:
2740 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
2741 ret = proc_releaseinterface(ps, p);
2744 case USBDEVFS_IOCTL:
2745 snoop(&dev->dev, "%s: IOCTL\n", __func__);
2746 ret = proc_ioctl_default(ps, p);
2749 case USBDEVFS_CLAIM_PORT:
2750 snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__);
2751 ret = proc_claim_port(ps, p);
2754 case USBDEVFS_RELEASE_PORT:
2755 snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__);
2756 ret = proc_release_port(ps, p);
2758 case USBDEVFS_GET_CAPABILITIES:
2759 ret = proc_get_capabilities(ps, p);
2761 case USBDEVFS_DISCONNECT_CLAIM:
2762 ret = proc_disconnect_claim(ps, p);
2764 case USBDEVFS_ALLOC_STREAMS:
2765 ret = proc_alloc_streams(ps, p);
2767 case USBDEVFS_FREE_STREAMS:
2768 ret = proc_free_streams(ps, p);
2770 case USBDEVFS_DROP_PRIVILEGES:
2771 ret = proc_drop_privileges(ps, p);
2773 case USBDEVFS_GET_SPEED:
2774 ret = ps->dev->speed;
2776 case USBDEVFS_FORBID_SUSPEND:
2777 ret = proc_forbid_suspend(ps);
2779 case USBDEVFS_ALLOW_SUSPEND:
2780 ret = proc_allow_suspend(ps);
2782 case USBDEVFS_WAIT_FOR_RESUME:
2783 ret = proc_wait_for_resume(ps);
2787 /* Handle variable-length commands */
2788 switch (cmd & ~IOCSIZE_MASK) {
2789 case USBDEVFS_CONNINFO_EX(0):
2790 ret = proc_conninfo_ex(ps, p, _IOC_SIZE(cmd));
2795 usb_unlock_device(dev);
2797 inode->i_atime = current_time(inode);
2801 static long usbdev_ioctl(struct file *file, unsigned int cmd,
2806 ret = usbdev_do_ioctl(file, cmd, (void __user *)arg);
2811 /* No kernel lock - fine */
2812 static __poll_t usbdev_poll(struct file *file,
2813 struct poll_table_struct *wait)
2815 struct usb_dev_state *ps = file->private_data;
2818 poll_wait(file, &ps->wait, wait);
2819 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
2820 mask |= EPOLLOUT | EPOLLWRNORM;
2823 if (list_empty(&ps->list))
2828 const struct file_operations usbdev_file_operations = {
2829 .owner = THIS_MODULE,
2830 .llseek = no_seek_end_llseek,
2831 .read = usbdev_read,
2832 .poll = usbdev_poll,
2833 .unlocked_ioctl = usbdev_ioctl,
2834 .compat_ioctl = compat_ptr_ioctl,
2835 .mmap = usbdev_mmap,
2836 .open = usbdev_open,
2837 .release = usbdev_release,
2840 static void usbdev_remove(struct usb_device *udev)
2842 struct usb_dev_state *ps;
2844 /* Protect against simultaneous resume */
2845 mutex_lock(&usbfs_mutex);
2846 while (!list_empty(&udev->filelist)) {
2847 ps = list_entry(udev->filelist.next, struct usb_dev_state, list);
2848 destroy_all_async(ps);
2849 wake_up_all(&ps->wait);
2850 WRITE_ONCE(ps->not_yet_resumed, 0);
2851 wake_up_all(&ps->wait_for_resume);
2852 list_del_init(&ps->list);
2854 kill_pid_usb_asyncio(ps->discsignr, EPIPE, ps->disccontext,
2855 ps->disc_pid, ps->cred);
2857 mutex_unlock(&usbfs_mutex);
2860 static int usbdev_notify(struct notifier_block *self,
2861 unsigned long action, void *dev)
2864 case USB_DEVICE_ADD:
2866 case USB_DEVICE_REMOVE:
2873 static struct notifier_block usbdev_nb = {
2874 .notifier_call = usbdev_notify,
2877 static struct cdev usb_device_cdev;
2879 int __init usb_devio_init(void)
2883 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
2886 printk(KERN_ERR "Unable to register minors for usb_device\n");
2889 cdev_init(&usb_device_cdev, &usbdev_file_operations);
2890 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
2892 printk(KERN_ERR "Unable to get usb_device major %d\n",
2896 usb_register_notify(&usbdev_nb);
2901 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
2905 void usb_devio_cleanup(void)
2907 usb_unregister_notify(&usbdev_nb);
2908 cdev_del(&usb_device_cdev);
2909 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);