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 atomic64_t usbfs_memory_usage; /* Total memory currently allocated */
144 /* Check whether it's okay to allocate more memory for a transfer */
145 static int usbfs_increase_memory_usage(u64 amount)
149 lim = READ_ONCE(usbfs_memory_mb);
152 atomic64_add(amount, &usbfs_memory_usage);
154 if (lim > 0 && atomic64_read(&usbfs_memory_usage) > lim) {
155 atomic64_sub(amount, &usbfs_memory_usage);
162 /* Memory for a transfer is being deallocated */
163 static void usbfs_decrease_memory_usage(u64 amount)
165 atomic64_sub(amount, &usbfs_memory_usage);
168 static int connected(struct usb_dev_state *ps)
170 return (!list_empty(&ps->list) &&
171 ps->dev->state != USB_STATE_NOTATTACHED);
174 static void dec_usb_memory_use_count(struct usb_memory *usbm, int *count)
176 struct usb_dev_state *ps = usbm->ps;
179 spin_lock_irqsave(&ps->lock, flags);
181 if (usbm->urb_use_count == 0 && usbm->vma_use_count == 0) {
182 list_del(&usbm->memlist);
183 spin_unlock_irqrestore(&ps->lock, flags);
185 usb_free_coherent(ps->dev, usbm->size, usbm->mem,
187 usbfs_decrease_memory_usage(
188 usbm->size + sizeof(struct usb_memory));
191 spin_unlock_irqrestore(&ps->lock, flags);
195 static void usbdev_vm_open(struct vm_area_struct *vma)
197 struct usb_memory *usbm = vma->vm_private_data;
200 spin_lock_irqsave(&usbm->ps->lock, flags);
201 ++usbm->vma_use_count;
202 spin_unlock_irqrestore(&usbm->ps->lock, flags);
205 static void usbdev_vm_close(struct vm_area_struct *vma)
207 struct usb_memory *usbm = vma->vm_private_data;
209 dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
212 static const struct vm_operations_struct usbdev_vm_ops = {
213 .open = usbdev_vm_open,
214 .close = usbdev_vm_close
217 static int usbdev_mmap(struct file *file, struct vm_area_struct *vma)
219 struct usb_memory *usbm = NULL;
220 struct usb_dev_state *ps = file->private_data;
221 struct usb_hcd *hcd = bus_to_hcd(ps->dev->bus);
222 size_t size = vma->vm_end - vma->vm_start;
225 dma_addr_t dma_handle;
228 ret = usbfs_increase_memory_usage(size + sizeof(struct usb_memory));
232 usbm = kzalloc(sizeof(struct usb_memory), GFP_KERNEL);
235 goto error_decrease_mem;
238 mem = usb_alloc_coherent(ps->dev, size, GFP_USER | __GFP_NOWARN,
242 goto error_free_usbm;
245 memset(mem, 0, size);
248 usbm->dma_handle = dma_handle;
251 usbm->vm_start = vma->vm_start;
252 usbm->vma_use_count = 1;
253 INIT_LIST_HEAD(&usbm->memlist);
255 if (hcd->localmem_pool || !hcd_uses_dma(hcd)) {
256 if (remap_pfn_range(vma, vma->vm_start,
257 virt_to_phys(usbm->mem) >> PAGE_SHIFT,
258 size, vma->vm_page_prot) < 0) {
259 dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
263 if (dma_mmap_coherent(hcd->self.sysdev, vma, mem, dma_handle,
265 dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
270 vma->vm_flags |= VM_IO;
271 vma->vm_flags |= (VM_DONTEXPAND | VM_DONTDUMP);
272 vma->vm_ops = &usbdev_vm_ops;
273 vma->vm_private_data = usbm;
275 spin_lock_irqsave(&ps->lock, flags);
276 list_add_tail(&usbm->memlist, &ps->memory_list);
277 spin_unlock_irqrestore(&ps->lock, flags);
284 usbfs_decrease_memory_usage(size + sizeof(struct usb_memory));
289 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
292 struct usb_dev_state *ps = file->private_data;
293 struct usb_device *dev = ps->dev;
300 usb_lock_device(dev);
301 if (!connected(ps)) {
304 } else if (pos < 0) {
309 if (pos < sizeof(struct usb_device_descriptor)) {
310 /* 18 bytes - fits on the stack */
311 struct usb_device_descriptor temp_desc;
313 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
314 le16_to_cpus(&temp_desc.bcdUSB);
315 le16_to_cpus(&temp_desc.idVendor);
316 le16_to_cpus(&temp_desc.idProduct);
317 le16_to_cpus(&temp_desc.bcdDevice);
319 len = sizeof(struct usb_device_descriptor) - pos;
322 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
333 pos = sizeof(struct usb_device_descriptor);
334 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
335 struct usb_config_descriptor *config =
336 (struct usb_config_descriptor *)dev->rawdescriptors[i];
337 unsigned int length = le16_to_cpu(config->wTotalLength);
339 if (*ppos < pos + length) {
341 /* The descriptor may claim to be longer than it
342 * really is. Here is the actual allocated length. */
344 le16_to_cpu(dev->config[i].desc.wTotalLength);
346 len = length - (*ppos - pos);
350 /* Simply don't write (skip over) unallocated parts */
351 if (alloclen > (*ppos - pos)) {
352 alloclen -= (*ppos - pos);
353 if (copy_to_user(buf,
354 dev->rawdescriptors[i] + (*ppos - pos),
355 min(len, alloclen))) {
371 usb_unlock_device(dev);
376 * async list handling
379 static struct async *alloc_async(unsigned int numisoframes)
383 as = kzalloc(sizeof(struct async), GFP_KERNEL);
386 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
394 static void free_async(struct async *as)
401 for (i = 0; i < as->urb->num_sgs; i++) {
402 if (sg_page(&as->urb->sg[i]))
403 kfree(sg_virt(&as->urb->sg[i]));
407 if (as->usbm == NULL)
408 kfree(as->urb->transfer_buffer);
410 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count);
412 kfree(as->urb->setup_packet);
413 usb_free_urb(as->urb);
414 usbfs_decrease_memory_usage(as->mem_usage);
418 static void async_newpending(struct async *as)
420 struct usb_dev_state *ps = as->ps;
423 spin_lock_irqsave(&ps->lock, flags);
424 list_add_tail(&as->asynclist, &ps->async_pending);
425 spin_unlock_irqrestore(&ps->lock, flags);
428 static void async_removepending(struct async *as)
430 struct usb_dev_state *ps = as->ps;
433 spin_lock_irqsave(&ps->lock, flags);
434 list_del_init(&as->asynclist);
435 spin_unlock_irqrestore(&ps->lock, flags);
438 static struct async *async_getcompleted(struct usb_dev_state *ps)
441 struct async *as = NULL;
443 spin_lock_irqsave(&ps->lock, flags);
444 if (!list_empty(&ps->async_completed)) {
445 as = list_entry(ps->async_completed.next, struct async,
447 list_del_init(&as->asynclist);
449 spin_unlock_irqrestore(&ps->lock, flags);
453 static struct async *async_getpending(struct usb_dev_state *ps,
454 void __user *userurb)
458 list_for_each_entry(as, &ps->async_pending, asynclist)
459 if (as->userurb == userurb) {
460 list_del_init(&as->asynclist);
467 static void snoop_urb(struct usb_device *udev,
468 void __user *userurb, int pipe, unsigned length,
469 int timeout_or_status, enum snoop_when when,
470 unsigned char *data, unsigned data_len)
472 static const char *types[] = {"isoc", "int", "ctrl", "bulk"};
473 static const char *dirs[] = {"out", "in"};
480 ep = usb_pipeendpoint(pipe);
481 t = types[usb_pipetype(pipe)];
482 d = dirs[!!usb_pipein(pipe)];
484 if (userurb) { /* Async */
486 dev_info(&udev->dev, "userurb %px, ep%d %s-%s, "
488 userurb, ep, t, d, length);
490 dev_info(&udev->dev, "userurb %px, ep%d %s-%s, "
491 "actual_length %u status %d\n",
492 userurb, ep, t, d, length,
496 dev_info(&udev->dev, "ep%d %s-%s, length %u, "
498 ep, t, d, length, timeout_or_status);
500 dev_info(&udev->dev, "ep%d %s-%s, actual_length %u, "
502 ep, t, d, length, timeout_or_status);
505 data_len = min(data_len, usbfs_snoop_max);
506 if (data && data_len > 0) {
507 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
512 static void snoop_urb_data(struct urb *urb, unsigned len)
516 len = min(len, usbfs_snoop_max);
517 if (!usbfs_snoop || len == 0)
520 if (urb->num_sgs == 0) {
521 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
522 urb->transfer_buffer, len, 1);
526 for (i = 0; i < urb->num_sgs && len; i++) {
527 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
528 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
529 sg_virt(&urb->sg[i]), size, 1);
534 static int copy_urb_data_to_user(u8 __user *userbuffer, struct urb *urb)
536 unsigned i, len, size;
538 if (urb->number_of_packets > 0) /* Isochronous */
539 len = urb->transfer_buffer_length;
541 len = urb->actual_length;
543 if (urb->num_sgs == 0) {
544 if (copy_to_user(userbuffer, urb->transfer_buffer, len))
549 for (i = 0; i < urb->num_sgs && len; i++) {
550 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
551 if (copy_to_user(userbuffer, sg_virt(&urb->sg[i]), size))
560 #define AS_CONTINUATION 1
563 static void cancel_bulk_urbs(struct usb_dev_state *ps, unsigned bulk_addr)
570 /* Mark all the pending URBs that match bulk_addr, up to but not
571 * including the first one without AS_CONTINUATION. If such an
572 * URB is encountered then a new transfer has already started so
573 * the endpoint doesn't need to be disabled; otherwise it does.
575 list_for_each_entry(as, &ps->async_pending, asynclist) {
576 if (as->bulk_addr == bulk_addr) {
577 if (as->bulk_status != AS_CONTINUATION)
579 as->bulk_status = AS_UNLINK;
583 ps->disabled_bulk_eps |= (1 << bulk_addr);
585 /* Now carefully unlink all the marked pending URBs */
587 list_for_each_entry_reverse(as, &ps->async_pending, asynclist) {
588 if (as->bulk_status == AS_UNLINK) {
589 as->bulk_status = 0; /* Only once */
592 spin_unlock(&ps->lock); /* Allow completions */
595 spin_lock(&ps->lock);
601 static void async_completed(struct urb *urb)
603 struct async *as = urb->context;
604 struct usb_dev_state *ps = as->ps;
605 struct pid *pid = NULL;
606 const struct cred *cred = NULL;
611 spin_lock_irqsave(&ps->lock, flags);
612 list_move_tail(&as->asynclist, &ps->async_completed);
613 as->status = urb->status;
617 addr = as->userurb_sigval;
618 pid = get_pid(as->pid);
619 cred = get_cred(as->cred);
621 snoop(&urb->dev->dev, "urb complete\n");
622 snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length,
623 as->status, COMPLETE, NULL, 0);
624 if (usb_urb_dir_in(urb))
625 snoop_urb_data(urb, urb->actual_length);
627 if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET &&
628 as->status != -ENOENT)
629 cancel_bulk_urbs(ps, as->bulk_addr);
632 spin_unlock_irqrestore(&ps->lock, flags);
635 kill_pid_usb_asyncio(signr, errno, addr, pid, cred);
641 static void destroy_async(struct usb_dev_state *ps, struct list_head *list)
647 spin_lock_irqsave(&ps->lock, flags);
648 while (!list_empty(list)) {
649 as = list_last_entry(list, struct async, asynclist);
650 list_del_init(&as->asynclist);
654 /* drop the spinlock so the completion handler can run */
655 spin_unlock_irqrestore(&ps->lock, flags);
658 spin_lock_irqsave(&ps->lock, flags);
660 spin_unlock_irqrestore(&ps->lock, flags);
663 static void destroy_async_on_interface(struct usb_dev_state *ps,
666 struct list_head *p, *q, hitlist;
669 INIT_LIST_HEAD(&hitlist);
670 spin_lock_irqsave(&ps->lock, flags);
671 list_for_each_safe(p, q, &ps->async_pending)
672 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
673 list_move_tail(p, &hitlist);
674 spin_unlock_irqrestore(&ps->lock, flags);
675 destroy_async(ps, &hitlist);
678 static void destroy_all_async(struct usb_dev_state *ps)
680 destroy_async(ps, &ps->async_pending);
684 * interface claims are made only at the request of user level code,
685 * which can also release them (explicitly or by closing files).
686 * they're also undone when devices disconnect.
689 static int driver_probe(struct usb_interface *intf,
690 const struct usb_device_id *id)
695 static void driver_disconnect(struct usb_interface *intf)
697 struct usb_dev_state *ps = usb_get_intfdata(intf);
698 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
703 /* NOTE: this relies on usbcore having canceled and completed
704 * all pending I/O requests; 2.6 does that.
707 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
708 clear_bit(ifnum, &ps->ifclaimed);
710 dev_warn(&intf->dev, "interface number %u out of range\n",
713 usb_set_intfdata(intf, NULL);
715 /* force async requests to complete */
716 destroy_async_on_interface(ps, ifnum);
719 /* We don't care about suspend/resume of claimed interfaces */
720 static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
725 static int driver_resume(struct usb_interface *intf)
730 /* The following routines apply to the entire device, not interfaces */
731 void usbfs_notify_suspend(struct usb_device *udev)
733 /* We don't need to handle this */
736 void usbfs_notify_resume(struct usb_device *udev)
738 struct usb_dev_state *ps;
740 /* Protect against simultaneous remove or release */
741 mutex_lock(&usbfs_mutex);
742 list_for_each_entry(ps, &udev->filelist, list) {
743 WRITE_ONCE(ps->not_yet_resumed, 0);
744 wake_up_all(&ps->wait_for_resume);
746 mutex_unlock(&usbfs_mutex);
749 struct usb_driver usbfs_driver = {
751 .probe = driver_probe,
752 .disconnect = driver_disconnect,
753 .suspend = driver_suspend,
754 .resume = driver_resume,
755 .supports_autosuspend = 1,
758 static int claimintf(struct usb_dev_state *ps, unsigned int ifnum)
760 struct usb_device *dev = ps->dev;
761 struct usb_interface *intf;
764 if (ifnum >= 8*sizeof(ps->ifclaimed))
766 /* already claimed */
767 if (test_bit(ifnum, &ps->ifclaimed))
770 if (ps->privileges_dropped &&
771 !test_bit(ifnum, &ps->interface_allowed_mask))
774 intf = usb_ifnum_to_if(dev, ifnum);
778 unsigned int old_suppress;
780 /* suppress uevents while claiming interface */
781 old_suppress = dev_get_uevent_suppress(&intf->dev);
782 dev_set_uevent_suppress(&intf->dev, 1);
783 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
784 dev_set_uevent_suppress(&intf->dev, old_suppress);
787 set_bit(ifnum, &ps->ifclaimed);
791 static int releaseintf(struct usb_dev_state *ps, unsigned int ifnum)
793 struct usb_device *dev;
794 struct usb_interface *intf;
798 if (ifnum >= 8*sizeof(ps->ifclaimed))
801 intf = usb_ifnum_to_if(dev, ifnum);
804 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
805 unsigned int old_suppress;
807 /* suppress uevents while releasing interface */
808 old_suppress = dev_get_uevent_suppress(&intf->dev);
809 dev_set_uevent_suppress(&intf->dev, 1);
810 usb_driver_release_interface(&usbfs_driver, intf);
811 dev_set_uevent_suppress(&intf->dev, old_suppress);
817 static int checkintf(struct usb_dev_state *ps, unsigned int ifnum)
819 if (ps->dev->state != USB_STATE_CONFIGURED)
820 return -EHOSTUNREACH;
821 if (ifnum >= 8*sizeof(ps->ifclaimed))
823 if (test_bit(ifnum, &ps->ifclaimed))
825 /* if not yet claimed, claim it for the driver */
826 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
827 "interface %u before use\n", task_pid_nr(current),
828 current->comm, ifnum);
829 return claimintf(ps, ifnum);
832 static int findintfep(struct usb_device *dev, unsigned int ep)
834 unsigned int i, j, e;
835 struct usb_interface *intf;
836 struct usb_host_interface *alts;
837 struct usb_endpoint_descriptor *endpt;
839 if (ep & ~(USB_DIR_IN|0xf))
843 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
844 intf = dev->actconfig->interface[i];
845 for (j = 0; j < intf->num_altsetting; j++) {
846 alts = &intf->altsetting[j];
847 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
848 endpt = &alts->endpoint[e].desc;
849 if (endpt->bEndpointAddress == ep)
850 return alts->desc.bInterfaceNumber;
857 static int check_ctrlrecip(struct usb_dev_state *ps, unsigned int requesttype,
858 unsigned int request, unsigned int index)
861 struct usb_host_interface *alt_setting;
863 if (ps->dev->state != USB_STATE_UNAUTHENTICATED
864 && ps->dev->state != USB_STATE_ADDRESS
865 && ps->dev->state != USB_STATE_CONFIGURED)
866 return -EHOSTUNREACH;
867 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
871 * check for the special corner case 'get_device_id' in the printer
872 * class specification, which we always want to allow as it is used
873 * to query things like ink level, etc.
875 if (requesttype == 0xa1 && request == 0) {
876 alt_setting = usb_find_alt_setting(ps->dev->actconfig,
877 index >> 8, index & 0xff);
879 && alt_setting->desc.bInterfaceClass == USB_CLASS_PRINTER)
884 switch (requesttype & USB_RECIP_MASK) {
885 case USB_RECIP_ENDPOINT:
886 if ((index & ~USB_DIR_IN) == 0)
888 ret = findintfep(ps->dev, index);
891 * Some not fully compliant Win apps seem to get
892 * index wrong and have the endpoint number here
893 * rather than the endpoint address (with the
894 * correct direction). Win does let this through,
895 * so we'll not reject it here but leave it to
896 * the device to not break KVM. But we warn.
898 ret = findintfep(ps->dev, index ^ 0x80);
900 dev_info(&ps->dev->dev,
901 "%s: process %i (%s) requesting ep %02x but needs %02x\n",
902 __func__, task_pid_nr(current),
903 current->comm, index, index ^ 0x80);
906 ret = checkintf(ps, ret);
909 case USB_RECIP_INTERFACE:
910 ret = checkintf(ps, index);
916 static struct usb_host_endpoint *ep_to_host_endpoint(struct usb_device *dev,
919 if (ep & USB_ENDPOINT_DIR_MASK)
920 return dev->ep_in[ep & USB_ENDPOINT_NUMBER_MASK];
922 return dev->ep_out[ep & USB_ENDPOINT_NUMBER_MASK];
925 static int parse_usbdevfs_streams(struct usb_dev_state *ps,
926 struct usbdevfs_streams __user *streams,
927 unsigned int *num_streams_ret,
928 unsigned int *num_eps_ret,
929 struct usb_host_endpoint ***eps_ret,
930 struct usb_interface **intf_ret)
932 unsigned int i, num_streams, num_eps;
933 struct usb_host_endpoint **eps;
934 struct usb_interface *intf = NULL;
938 if (get_user(num_streams, &streams->num_streams) ||
939 get_user(num_eps, &streams->num_eps))
942 if (num_eps < 1 || num_eps > USB_MAXENDPOINTS)
945 /* The XHCI controller allows max 2 ^ 16 streams */
946 if (num_streams_ret && (num_streams < 2 || num_streams > 65536))
949 eps = kmalloc_array(num_eps, sizeof(*eps), GFP_KERNEL);
953 for (i = 0; i < num_eps; i++) {
954 if (get_user(ep, &streams->eps[i])) {
958 eps[i] = ep_to_host_endpoint(ps->dev, ep);
964 /* usb_alloc/free_streams operate on an usb_interface */
965 ifnum = findintfep(ps->dev, ep);
972 ret = checkintf(ps, ifnum);
975 intf = usb_ifnum_to_if(ps->dev, ifnum);
977 /* Verify all eps belong to the same interface */
978 if (ifnum != intf->altsetting->desc.bInterfaceNumber) {
986 *num_streams_ret = num_streams;
987 *num_eps_ret = num_eps;
998 static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
1002 dev = bus_find_device_by_devt(&usb_bus_type, devt);
1005 return to_usb_device(dev);
1011 static int usbdev_open(struct inode *inode, struct file *file)
1013 struct usb_device *dev = NULL;
1014 struct usb_dev_state *ps;
1018 ps = kzalloc(sizeof(struct usb_dev_state), GFP_KERNEL);
1024 /* usbdev device-node */
1025 if (imajor(inode) == USB_DEVICE_MAJOR)
1026 dev = usbdev_lookup_by_devt(inode->i_rdev);
1030 usb_lock_device(dev);
1031 if (dev->state == USB_STATE_NOTATTACHED)
1032 goto out_unlock_device;
1034 ret = usb_autoresume_device(dev);
1036 goto out_unlock_device;
1040 ps->interface_allowed_mask = 0xFFFFFFFF; /* 32 bits */
1041 spin_lock_init(&ps->lock);
1042 INIT_LIST_HEAD(&ps->list);
1043 INIT_LIST_HEAD(&ps->async_pending);
1044 INIT_LIST_HEAD(&ps->async_completed);
1045 INIT_LIST_HEAD(&ps->memory_list);
1046 init_waitqueue_head(&ps->wait);
1047 init_waitqueue_head(&ps->wait_for_resume);
1048 ps->disc_pid = get_pid(task_pid(current));
1049 ps->cred = get_current_cred();
1052 /* Can't race with resume; the device is already active */
1053 list_add_tail(&ps->list, &dev->filelist);
1054 file->private_data = ps;
1055 usb_unlock_device(dev);
1056 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
1061 usb_unlock_device(dev);
1068 static int usbdev_release(struct inode *inode, struct file *file)
1070 struct usb_dev_state *ps = file->private_data;
1071 struct usb_device *dev = ps->dev;
1075 usb_lock_device(dev);
1076 usb_hub_release_all_ports(dev, ps);
1078 /* Protect against simultaneous resume */
1079 mutex_lock(&usbfs_mutex);
1080 list_del_init(&ps->list);
1081 mutex_unlock(&usbfs_mutex);
1083 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
1085 if (test_bit(ifnum, &ps->ifclaimed))
1086 releaseintf(ps, ifnum);
1088 destroy_all_async(ps);
1089 if (!ps->suspend_allowed)
1090 usb_autosuspend_device(dev);
1091 usb_unlock_device(dev);
1093 put_pid(ps->disc_pid);
1096 as = async_getcompleted(ps);
1099 as = async_getcompleted(ps);
1106 static void usbfs_blocking_completion(struct urb *urb)
1108 complete((struct completion *) urb->context);
1112 * Much like usb_start_wait_urb, but returns status separately from
1113 * actual_length and uses a killable wait.
1115 static int usbfs_start_wait_urb(struct urb *urb, int timeout,
1116 unsigned int *actlen)
1118 DECLARE_COMPLETION_ONSTACK(ctx);
1119 unsigned long expire;
1122 urb->context = &ctx;
1123 urb->complete = usbfs_blocking_completion;
1125 rc = usb_submit_urb(urb, GFP_KERNEL);
1129 expire = (timeout ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT);
1130 rc = wait_for_completion_killable_timeout(&ctx, expire);
1133 *actlen = urb->actual_length;
1134 if (urb->status != -ENOENT)
1135 ; /* Completed before it was killed */
1141 *actlen = urb->actual_length;
1145 static int do_proc_control(struct usb_dev_state *ps,
1146 struct usbdevfs_ctrltransfer *ctrl)
1148 struct usb_device *dev = ps->dev;
1150 unsigned char *tbuf;
1151 unsigned int wLength, actlen;
1153 struct urb *urb = NULL;
1154 struct usb_ctrlrequest *dr = NULL;
1156 ret = check_ctrlrecip(ps, ctrl->bRequestType, ctrl->bRequest,
1160 wLength = ctrl->wLength; /* To suppress 64k PAGE_SIZE warning */
1161 if (wLength > PAGE_SIZE)
1163 ret = usbfs_increase_memory_usage(PAGE_SIZE + sizeof(struct urb) +
1164 sizeof(struct usb_ctrlrequest));
1169 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
1172 urb = usb_alloc_urb(0, GFP_NOIO);
1175 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
1179 dr->bRequestType = ctrl->bRequestType;
1180 dr->bRequest = ctrl->bRequest;
1181 dr->wValue = cpu_to_le16(ctrl->wValue);
1182 dr->wIndex = cpu_to_le16(ctrl->wIndex);
1183 dr->wLength = cpu_to_le16(ctrl->wLength);
1185 tmo = ctrl->timeout;
1186 snoop(&dev->dev, "control urb: bRequestType=%02x "
1187 "bRequest=%02x wValue=%04x "
1188 "wIndex=%04x wLength=%04x\n",
1189 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1190 ctrl->wIndex, ctrl->wLength);
1192 if ((ctrl->bRequestType & USB_DIR_IN) && wLength) {
1193 pipe = usb_rcvctrlpipe(dev, 0);
1194 usb_fill_control_urb(urb, dev, pipe, (unsigned char *) dr, tbuf,
1195 wLength, NULL, NULL);
1196 snoop_urb(dev, NULL, pipe, wLength, tmo, SUBMIT, NULL, 0);
1198 usb_unlock_device(dev);
1199 i = usbfs_start_wait_urb(urb, tmo, &actlen);
1200 usb_lock_device(dev);
1201 snoop_urb(dev, NULL, pipe, actlen, i, COMPLETE, tbuf, actlen);
1203 if (copy_to_user(ctrl->data, tbuf, actlen)) {
1210 if (copy_from_user(tbuf, ctrl->data, wLength)) {
1215 pipe = usb_sndctrlpipe(dev, 0);
1216 usb_fill_control_urb(urb, dev, pipe, (unsigned char *) dr, tbuf,
1217 wLength, NULL, NULL);
1218 snoop_urb(dev, NULL, pipe, wLength, tmo, SUBMIT, tbuf, wLength);
1220 usb_unlock_device(dev);
1221 i = usbfs_start_wait_urb(urb, tmo, &actlen);
1222 usb_lock_device(dev);
1223 snoop_urb(dev, NULL, pipe, actlen, i, COMPLETE, NULL, 0);
1225 if (i < 0 && i != -EPIPE) {
1226 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
1227 "failed cmd %s rqt %u rq %u len %u ret %d\n",
1228 current->comm, ctrl->bRequestType, ctrl->bRequest,
1231 ret = (i < 0 ? i : actlen);
1234 /* Linger a bit, prior to the next control message. */
1235 if (dev->quirks & USB_QUIRK_DELAY_CTRL_MSG)
1240 free_page((unsigned long) tbuf);
1241 usbfs_decrease_memory_usage(PAGE_SIZE + sizeof(struct urb) +
1242 sizeof(struct usb_ctrlrequest));
1246 static int proc_control(struct usb_dev_state *ps, void __user *arg)
1248 struct usbdevfs_ctrltransfer ctrl;
1250 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1252 return do_proc_control(ps, &ctrl);
1255 static int do_proc_bulk(struct usb_dev_state *ps,
1256 struct usbdevfs_bulktransfer *bulk)
1258 struct usb_device *dev = ps->dev;
1259 unsigned int tmo, len1, len2, pipe;
1260 unsigned char *tbuf;
1262 struct urb *urb = NULL;
1263 struct usb_host_endpoint *ep;
1265 ret = findintfep(ps->dev, bulk->ep);
1268 ret = checkintf(ps, ret);
1273 if (len1 < 0 || len1 >= (INT_MAX - sizeof(struct urb)))
1276 if (bulk->ep & USB_DIR_IN)
1277 pipe = usb_rcvbulkpipe(dev, bulk->ep & 0x7f);
1279 pipe = usb_sndbulkpipe(dev, bulk->ep & 0x7f);
1280 ep = usb_pipe_endpoint(dev, pipe);
1281 if (!ep || !usb_endpoint_maxp(&ep->desc))
1283 ret = usbfs_increase_memory_usage(len1 + sizeof(struct urb));
1288 * len1 can be almost arbitrarily large. Don't WARN if it's
1289 * too big, just fail the request.
1292 tbuf = kmalloc(len1, GFP_KERNEL | __GFP_NOWARN);
1295 urb = usb_alloc_urb(0, GFP_KERNEL);
1299 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
1300 USB_ENDPOINT_XFER_INT) {
1301 pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
1302 usb_fill_int_urb(urb, dev, pipe, tbuf, len1,
1303 NULL, NULL, ep->desc.bInterval);
1305 usb_fill_bulk_urb(urb, dev, pipe, tbuf, len1, NULL, NULL);
1308 tmo = bulk->timeout;
1309 if (bulk->ep & 0x80) {
1310 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, NULL, 0);
1312 usb_unlock_device(dev);
1313 i = usbfs_start_wait_urb(urb, tmo, &len2);
1314 usb_lock_device(dev);
1315 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, tbuf, len2);
1318 if (copy_to_user(bulk->data, tbuf, len2)) {
1325 if (copy_from_user(tbuf, bulk->data, len1)) {
1330 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, tbuf, len1);
1332 usb_unlock_device(dev);
1333 i = usbfs_start_wait_urb(urb, tmo, &len2);
1334 usb_lock_device(dev);
1335 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, NULL, 0);
1337 ret = (i < 0 ? i : len2);
1341 usbfs_decrease_memory_usage(len1 + sizeof(struct urb));
1345 static int proc_bulk(struct usb_dev_state *ps, void __user *arg)
1347 struct usbdevfs_bulktransfer bulk;
1349 if (copy_from_user(&bulk, arg, sizeof(bulk)))
1351 return do_proc_bulk(ps, &bulk);
1354 static void check_reset_of_active_ep(struct usb_device *udev,
1355 unsigned int epnum, char *ioctl_name)
1357 struct usb_host_endpoint **eps;
1358 struct usb_host_endpoint *ep;
1360 eps = (epnum & USB_DIR_IN) ? udev->ep_in : udev->ep_out;
1361 ep = eps[epnum & 0x0f];
1362 if (ep && !list_empty(&ep->urb_list))
1363 dev_warn(&udev->dev, "Process %d (%s) called USBDEVFS_%s for active endpoint 0x%02x\n",
1364 task_pid_nr(current), current->comm,
1368 static int proc_resetep(struct usb_dev_state *ps, void __user *arg)
1373 if (get_user(ep, (unsigned int __user *)arg))
1375 ret = findintfep(ps->dev, ep);
1378 ret = checkintf(ps, ret);
1381 check_reset_of_active_ep(ps->dev, ep, "RESETEP");
1382 usb_reset_endpoint(ps->dev, ep);
1386 static int proc_clearhalt(struct usb_dev_state *ps, void __user *arg)
1392 if (get_user(ep, (unsigned int __user *)arg))
1394 ret = findintfep(ps->dev, ep);
1397 ret = checkintf(ps, ret);
1400 check_reset_of_active_ep(ps->dev, ep, "CLEAR_HALT");
1401 if (ep & USB_DIR_IN)
1402 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
1404 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
1406 return usb_clear_halt(ps->dev, pipe);
1409 static int proc_getdriver(struct usb_dev_state *ps, void __user *arg)
1411 struct usbdevfs_getdriver gd;
1412 struct usb_interface *intf;
1415 if (copy_from_user(&gd, arg, sizeof(gd)))
1417 intf = usb_ifnum_to_if(ps->dev, gd.interface);
1418 if (!intf || !intf->dev.driver)
1421 strlcpy(gd.driver, intf->dev.driver->name,
1423 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
1428 static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg)
1430 struct usbdevfs_connectinfo ci;
1432 memset(&ci, 0, sizeof(ci));
1433 ci.devnum = ps->dev->devnum;
1434 ci.slow = ps->dev->speed == USB_SPEED_LOW;
1436 if (copy_to_user(arg, &ci, sizeof(ci)))
1441 static int proc_conninfo_ex(struct usb_dev_state *ps,
1442 void __user *arg, size_t size)
1444 struct usbdevfs_conninfo_ex ci;
1445 struct usb_device *udev = ps->dev;
1447 if (size < sizeof(ci.size))
1450 memset(&ci, 0, sizeof(ci));
1451 ci.size = sizeof(ci);
1452 ci.busnum = udev->bus->busnum;
1453 ci.devnum = udev->devnum;
1454 ci.speed = udev->speed;
1456 while (udev && udev->portnum != 0) {
1457 if (++ci.num_ports <= ARRAY_SIZE(ci.ports))
1458 ci.ports[ARRAY_SIZE(ci.ports) - ci.num_ports] =
1460 udev = udev->parent;
1463 if (ci.num_ports < ARRAY_SIZE(ci.ports))
1464 memmove(&ci.ports[0],
1465 &ci.ports[ARRAY_SIZE(ci.ports) - ci.num_ports],
1468 if (copy_to_user(arg, &ci, min(sizeof(ci), size)))
1474 static int proc_resetdevice(struct usb_dev_state *ps)
1476 struct usb_host_config *actconfig = ps->dev->actconfig;
1477 struct usb_interface *interface;
1480 /* Don't allow a device reset if the process has dropped the
1481 * privilege to do such things and any of the interfaces are
1482 * currently claimed.
1484 if (ps->privileges_dropped && actconfig) {
1485 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
1486 interface = actconfig->interface[i];
1487 number = interface->cur_altsetting->desc.bInterfaceNumber;
1488 if (usb_interface_claimed(interface) &&
1489 !test_bit(number, &ps->ifclaimed)) {
1490 dev_warn(&ps->dev->dev,
1491 "usbfs: interface %d claimed by %s while '%s' resets device\n",
1492 number, interface->dev.driver->name, current->comm);
1498 return usb_reset_device(ps->dev);
1501 static int proc_setintf(struct usb_dev_state *ps, void __user *arg)
1503 struct usbdevfs_setinterface setintf;
1506 if (copy_from_user(&setintf, arg, sizeof(setintf)))
1508 ret = checkintf(ps, setintf.interface);
1512 destroy_async_on_interface(ps, setintf.interface);
1514 return usb_set_interface(ps->dev, setintf.interface,
1515 setintf.altsetting);
1518 static int proc_setconfig(struct usb_dev_state *ps, void __user *arg)
1522 struct usb_host_config *actconfig;
1524 if (get_user(u, (int __user *)arg))
1527 actconfig = ps->dev->actconfig;
1529 /* Don't touch the device if any interfaces are claimed.
1530 * It could interfere with other drivers' operations, and if
1531 * an interface is claimed by usbfs it could easily deadlock.
1536 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
1537 if (usb_interface_claimed(actconfig->interface[i])) {
1538 dev_warn(&ps->dev->dev,
1539 "usbfs: interface %d claimed by %s "
1540 "while '%s' sets config #%d\n",
1541 actconfig->interface[i]
1543 ->desc.bInterfaceNumber,
1544 actconfig->interface[i]
1553 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
1554 * so avoid usb_set_configuration()'s kick to sysfs
1557 if (actconfig && actconfig->desc.bConfigurationValue == u)
1558 status = usb_reset_configuration(ps->dev);
1560 status = usb_set_configuration(ps->dev, u);
1566 static struct usb_memory *
1567 find_memory_area(struct usb_dev_state *ps, const struct usbdevfs_urb *uurb)
1569 struct usb_memory *usbm = NULL, *iter;
1570 unsigned long flags;
1571 unsigned long uurb_start = (unsigned long)uurb->buffer;
1573 spin_lock_irqsave(&ps->lock, flags);
1574 list_for_each_entry(iter, &ps->memory_list, memlist) {
1575 if (uurb_start >= iter->vm_start &&
1576 uurb_start < iter->vm_start + iter->size) {
1577 if (uurb->buffer_length > iter->vm_start + iter->size -
1579 usbm = ERR_PTR(-EINVAL);
1582 usbm->urb_use_count++;
1587 spin_unlock_irqrestore(&ps->lock, flags);
1591 static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb,
1592 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
1593 void __user *arg, sigval_t userurb_sigval)
1595 struct usbdevfs_iso_packet_desc *isopkt = NULL;
1596 struct usb_host_endpoint *ep;
1597 struct async *as = NULL;
1598 struct usb_ctrlrequest *dr = NULL;
1599 unsigned int u, totlen, isofrmlen;
1600 int i, ret, num_sgs = 0, ifnum = -1;
1601 int number_of_packets = 0;
1602 unsigned int stream_id = 0;
1605 bool allow_short = false;
1606 bool allow_zero = false;
1607 unsigned long mask = USBDEVFS_URB_SHORT_NOT_OK |
1608 USBDEVFS_URB_BULK_CONTINUATION |
1609 USBDEVFS_URB_NO_FSBR |
1610 USBDEVFS_URB_ZERO_PACKET |
1611 USBDEVFS_URB_NO_INTERRUPT;
1612 /* USBDEVFS_URB_ISO_ASAP is a special case */
1613 if (uurb->type == USBDEVFS_URB_TYPE_ISO)
1614 mask |= USBDEVFS_URB_ISO_ASAP;
1616 if (uurb->flags & ~mask)
1619 if ((unsigned int)uurb->buffer_length >= USBFS_XFER_MAX)
1621 if (uurb->buffer_length > 0 && !uurb->buffer)
1623 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
1624 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
1625 ifnum = findintfep(ps->dev, uurb->endpoint);
1628 ret = checkintf(ps, ifnum);
1632 ep = ep_to_host_endpoint(ps->dev, uurb->endpoint);
1635 is_in = (uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0;
1638 switch (uurb->type) {
1639 case USBDEVFS_URB_TYPE_CONTROL:
1640 if (!usb_endpoint_xfer_control(&ep->desc))
1642 /* min 8 byte setup packet */
1643 if (uurb->buffer_length < 8)
1645 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1648 if (copy_from_user(dr, uurb->buffer, 8)) {
1652 if (uurb->buffer_length < (le16_to_cpu(dr->wLength) + 8)) {
1656 ret = check_ctrlrecip(ps, dr->bRequestType, dr->bRequest,
1657 le16_to_cpu(dr->wIndex));
1660 uurb->buffer_length = le16_to_cpu(dr->wLength);
1662 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1664 uurb->endpoint |= USB_DIR_IN;
1667 uurb->endpoint &= ~USB_DIR_IN;
1671 snoop(&ps->dev->dev, "control urb: bRequestType=%02x "
1672 "bRequest=%02x wValue=%04x "
1673 "wIndex=%04x wLength=%04x\n",
1674 dr->bRequestType, dr->bRequest,
1675 __le16_to_cpu(dr->wValue),
1676 __le16_to_cpu(dr->wIndex),
1677 __le16_to_cpu(dr->wLength));
1678 u = sizeof(struct usb_ctrlrequest);
1681 case USBDEVFS_URB_TYPE_BULK:
1686 switch (usb_endpoint_type(&ep->desc)) {
1687 case USB_ENDPOINT_XFER_CONTROL:
1688 case USB_ENDPOINT_XFER_ISOC:
1690 case USB_ENDPOINT_XFER_INT:
1691 /* allow single-shot interrupt transfers */
1692 uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
1695 num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE);
1696 if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize)
1699 stream_id = uurb->stream_id;
1702 case USBDEVFS_URB_TYPE_INTERRUPT:
1703 if (!usb_endpoint_xfer_int(&ep->desc))
1712 case USBDEVFS_URB_TYPE_ISO:
1713 /* arbitrary limit */
1714 if (uurb->number_of_packets < 1 ||
1715 uurb->number_of_packets > 128)
1717 if (!usb_endpoint_xfer_isoc(&ep->desc))
1719 number_of_packets = uurb->number_of_packets;
1720 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1722 isopkt = memdup_user(iso_frame_desc, isofrmlen);
1723 if (IS_ERR(isopkt)) {
1724 ret = PTR_ERR(isopkt);
1728 for (totlen = u = 0; u < number_of_packets; u++) {
1730 * arbitrary limit need for USB 3.1 Gen2
1731 * sizemax: 96 DPs at SSP, 96 * 1024 = 98304
1733 if (isopkt[u].length > 98304) {
1737 totlen += isopkt[u].length;
1739 u *= sizeof(struct usb_iso_packet_descriptor);
1740 uurb->buffer_length = totlen;
1747 if (uurb->buffer_length > 0 &&
1748 !access_ok(uurb->buffer, uurb->buffer_length)) {
1752 as = alloc_async(number_of_packets);
1758 as->usbm = find_memory_area(ps, uurb);
1759 if (IS_ERR(as->usbm)) {
1760 ret = PTR_ERR(as->usbm);
1765 /* do not use SG buffers when memory mapped segments
1771 u += sizeof(struct async) + sizeof(struct urb) +
1772 (as->usbm ? 0 : uurb->buffer_length) +
1773 num_sgs * sizeof(struct scatterlist);
1774 ret = usbfs_increase_memory_usage(u);
1780 as->urb->sg = kmalloc_array(num_sgs,
1781 sizeof(struct scatterlist),
1782 GFP_KERNEL | __GFP_NOWARN);
1787 as->urb->num_sgs = num_sgs;
1788 sg_init_table(as->urb->sg, as->urb->num_sgs);
1790 totlen = uurb->buffer_length;
1791 for (i = 0; i < as->urb->num_sgs; i++) {
1792 u = (totlen > USB_SG_SIZE) ? USB_SG_SIZE : totlen;
1793 buf = kmalloc(u, GFP_KERNEL);
1798 sg_set_buf(&as->urb->sg[i], buf, u);
1801 if (copy_from_user(buf, uurb->buffer, u)) {
1809 } else if (uurb->buffer_length > 0) {
1811 unsigned long uurb_start = (unsigned long)uurb->buffer;
1813 as->urb->transfer_buffer = as->usbm->mem +
1814 (uurb_start - as->usbm->vm_start);
1816 as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
1817 GFP_KERNEL | __GFP_NOWARN);
1818 if (!as->urb->transfer_buffer) {
1823 if (copy_from_user(as->urb->transfer_buffer,
1825 uurb->buffer_length)) {
1829 } else if (uurb->type == USBDEVFS_URB_TYPE_ISO) {
1831 * Isochronous input data may end up being
1832 * discontiguous if some of the packets are
1833 * short. Clear the buffer so that the gaps
1834 * don't leak kernel data to userspace.
1836 memset(as->urb->transfer_buffer, 0,
1837 uurb->buffer_length);
1841 as->urb->dev = ps->dev;
1842 as->urb->pipe = (uurb->type << 30) |
1843 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1844 (uurb->endpoint & USB_DIR_IN);
1846 /* This tedious sequence is necessary because the URB_* flags
1847 * are internal to the kernel and subject to change, whereas
1848 * the USBDEVFS_URB_* flags are a user API and must not be changed.
1850 u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
1851 if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
1853 if (allow_short && uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
1854 u |= URB_SHORT_NOT_OK;
1855 if (allow_zero && uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1856 u |= URB_ZERO_PACKET;
1857 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
1858 u |= URB_NO_INTERRUPT;
1859 as->urb->transfer_flags = u;
1861 if (!allow_short && uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
1862 dev_warn(&ps->dev->dev, "Requested nonsensical USBDEVFS_URB_SHORT_NOT_OK.\n");
1863 if (!allow_zero && uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1864 dev_warn(&ps->dev->dev, "Requested nonsensical USBDEVFS_URB_ZERO_PACKET.\n");
1866 as->urb->transfer_buffer_length = uurb->buffer_length;
1867 as->urb->setup_packet = (unsigned char *)dr;
1869 as->urb->start_frame = uurb->start_frame;
1870 as->urb->number_of_packets = number_of_packets;
1871 as->urb->stream_id = stream_id;
1873 if (ep->desc.bInterval) {
1874 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1875 ps->dev->speed == USB_SPEED_HIGH ||
1876 ps->dev->speed >= USB_SPEED_SUPER)
1877 as->urb->interval = 1 <<
1878 min(15, ep->desc.bInterval - 1);
1880 as->urb->interval = ep->desc.bInterval;
1883 as->urb->context = as;
1884 as->urb->complete = async_completed;
1885 for (totlen = u = 0; u < number_of_packets; u++) {
1886 as->urb->iso_frame_desc[u].offset = totlen;
1887 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1888 totlen += isopkt[u].length;
1894 as->userurb_sigval = userurb_sigval;
1896 unsigned long uurb_start = (unsigned long)uurb->buffer;
1898 as->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1899 as->urb->transfer_dma = as->usbm->dma_handle +
1900 (uurb_start - as->usbm->vm_start);
1901 } else if (is_in && uurb->buffer_length > 0)
1902 as->userbuffer = uurb->buffer;
1903 as->signr = uurb->signr;
1905 as->pid = get_pid(task_pid(current));
1906 as->cred = get_current_cred();
1907 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1908 as->urb->transfer_buffer_length, 0, SUBMIT,
1911 snoop_urb_data(as->urb, as->urb->transfer_buffer_length);
1913 async_newpending(as);
1915 if (usb_endpoint_xfer_bulk(&ep->desc)) {
1916 spin_lock_irq(&ps->lock);
1918 /* Not exactly the endpoint address; the direction bit is
1919 * shifted to the 0x10 position so that the value will be
1922 as->bulk_addr = usb_endpoint_num(&ep->desc) |
1923 ((ep->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1926 /* If this bulk URB is the start of a new transfer, re-enable
1927 * the endpoint. Otherwise mark it as a continuation URB.
1929 if (uurb->flags & USBDEVFS_URB_BULK_CONTINUATION)
1930 as->bulk_status = AS_CONTINUATION;
1932 ps->disabled_bulk_eps &= ~(1 << as->bulk_addr);
1934 /* Don't accept continuation URBs if the endpoint is
1935 * disabled because of an earlier error.
1937 if (ps->disabled_bulk_eps & (1 << as->bulk_addr))
1940 ret = usb_submit_urb(as->urb, GFP_ATOMIC);
1941 spin_unlock_irq(&ps->lock);
1943 ret = usb_submit_urb(as->urb, GFP_KERNEL);
1947 dev_printk(KERN_DEBUG, &ps->dev->dev,
1948 "usbfs: usb_submit_urb returned %d\n", ret);
1949 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1950 0, ret, COMPLETE, NULL, 0);
1951 async_removepending(as);
1964 static int proc_submiturb(struct usb_dev_state *ps, void __user *arg)
1966 struct usbdevfs_urb uurb;
1967 sigval_t userurb_sigval;
1969 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1972 memset(&userurb_sigval, 0, sizeof(userurb_sigval));
1973 userurb_sigval.sival_ptr = arg;
1975 return proc_do_submiturb(ps, &uurb,
1976 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1977 arg, userurb_sigval);
1980 static int proc_unlinkurb(struct usb_dev_state *ps, void __user *arg)
1984 unsigned long flags;
1986 spin_lock_irqsave(&ps->lock, flags);
1987 as = async_getpending(ps, arg);
1989 spin_unlock_irqrestore(&ps->lock, flags);
1995 spin_unlock_irqrestore(&ps->lock, flags);
2003 static void compute_isochronous_actual_length(struct urb *urb)
2007 if (urb->number_of_packets > 0) {
2008 urb->actual_length = 0;
2009 for (i = 0; i < urb->number_of_packets; i++)
2010 urb->actual_length +=
2011 urb->iso_frame_desc[i].actual_length;
2015 static int processcompl(struct async *as, void __user * __user *arg)
2017 struct urb *urb = as->urb;
2018 struct usbdevfs_urb __user *userurb = as->userurb;
2019 void __user *addr = as->userurb;
2022 compute_isochronous_actual_length(urb);
2023 if (as->userbuffer && urb->actual_length) {
2024 if (copy_urb_data_to_user(as->userbuffer, urb))
2027 if (put_user(as->status, &userurb->status))
2029 if (put_user(urb->actual_length, &userurb->actual_length))
2031 if (put_user(urb->error_count, &userurb->error_count))
2034 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
2035 for (i = 0; i < urb->number_of_packets; i++) {
2036 if (put_user(urb->iso_frame_desc[i].actual_length,
2037 &userurb->iso_frame_desc[i].actual_length))
2039 if (put_user(urb->iso_frame_desc[i].status,
2040 &userurb->iso_frame_desc[i].status))
2045 if (put_user(addr, (void __user * __user *)arg))
2053 static struct async *reap_as(struct usb_dev_state *ps)
2055 DECLARE_WAITQUEUE(wait, current);
2056 struct async *as = NULL;
2057 struct usb_device *dev = ps->dev;
2059 add_wait_queue(&ps->wait, &wait);
2061 __set_current_state(TASK_INTERRUPTIBLE);
2062 as = async_getcompleted(ps);
2063 if (as || !connected(ps))
2065 if (signal_pending(current))
2067 usb_unlock_device(dev);
2069 usb_lock_device(dev);
2071 remove_wait_queue(&ps->wait, &wait);
2072 set_current_state(TASK_RUNNING);
2076 static int proc_reapurb(struct usb_dev_state *ps, void __user *arg)
2078 struct async *as = reap_as(ps);
2083 snoop(&ps->dev->dev, "reap %px\n", as->userurb);
2084 retval = processcompl(as, (void __user * __user *)arg);
2088 if (signal_pending(current))
2093 static int proc_reapurbnonblock(struct usb_dev_state *ps, void __user *arg)
2098 as = async_getcompleted(ps);
2100 snoop(&ps->dev->dev, "reap %px\n", as->userurb);
2101 retval = processcompl(as, (void __user * __user *)arg);
2104 retval = (connected(ps) ? -EAGAIN : -ENODEV);
2109 #ifdef CONFIG_COMPAT
2110 static int proc_control_compat(struct usb_dev_state *ps,
2111 struct usbdevfs_ctrltransfer32 __user *p32)
2113 struct usbdevfs_ctrltransfer ctrl;
2116 if (copy_from_user(&ctrl, p32, sizeof(*p32) - sizeof(compat_caddr_t)) ||
2117 get_user(udata, &p32->data))
2119 ctrl.data = compat_ptr(udata);
2120 return do_proc_control(ps, &ctrl);
2123 static int proc_bulk_compat(struct usb_dev_state *ps,
2124 struct usbdevfs_bulktransfer32 __user *p32)
2126 struct usbdevfs_bulktransfer bulk;
2127 compat_caddr_t addr;
2129 if (get_user(bulk.ep, &p32->ep) ||
2130 get_user(bulk.len, &p32->len) ||
2131 get_user(bulk.timeout, &p32->timeout) ||
2132 get_user(addr, &p32->data))
2134 bulk.data = compat_ptr(addr);
2135 return do_proc_bulk(ps, &bulk);
2138 static int proc_disconnectsignal_compat(struct usb_dev_state *ps, void __user *arg)
2140 struct usbdevfs_disconnectsignal32 ds;
2142 if (copy_from_user(&ds, arg, sizeof(ds)))
2144 ps->discsignr = ds.signr;
2145 ps->disccontext.sival_int = ds.context;
2149 static int get_urb32(struct usbdevfs_urb *kurb,
2150 struct usbdevfs_urb32 __user *uurb)
2152 struct usbdevfs_urb32 urb32;
2153 if (copy_from_user(&urb32, uurb, sizeof(*uurb)))
2155 kurb->type = urb32.type;
2156 kurb->endpoint = urb32.endpoint;
2157 kurb->status = urb32.status;
2158 kurb->flags = urb32.flags;
2159 kurb->buffer = compat_ptr(urb32.buffer);
2160 kurb->buffer_length = urb32.buffer_length;
2161 kurb->actual_length = urb32.actual_length;
2162 kurb->start_frame = urb32.start_frame;
2163 kurb->number_of_packets = urb32.number_of_packets;
2164 kurb->error_count = urb32.error_count;
2165 kurb->signr = urb32.signr;
2166 kurb->usercontext = compat_ptr(urb32.usercontext);
2170 static int proc_submiturb_compat(struct usb_dev_state *ps, void __user *arg)
2172 struct usbdevfs_urb uurb;
2173 sigval_t userurb_sigval;
2175 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
2178 memset(&userurb_sigval, 0, sizeof(userurb_sigval));
2179 userurb_sigval.sival_int = ptr_to_compat(arg);
2181 return proc_do_submiturb(ps, &uurb,
2182 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
2183 arg, userurb_sigval);
2186 static int processcompl_compat(struct async *as, void __user * __user *arg)
2188 struct urb *urb = as->urb;
2189 struct usbdevfs_urb32 __user *userurb = as->userurb;
2190 void __user *addr = as->userurb;
2193 compute_isochronous_actual_length(urb);
2194 if (as->userbuffer && urb->actual_length) {
2195 if (copy_urb_data_to_user(as->userbuffer, urb))
2198 if (put_user(as->status, &userurb->status))
2200 if (put_user(urb->actual_length, &userurb->actual_length))
2202 if (put_user(urb->error_count, &userurb->error_count))
2205 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
2206 for (i = 0; i < urb->number_of_packets; i++) {
2207 if (put_user(urb->iso_frame_desc[i].actual_length,
2208 &userurb->iso_frame_desc[i].actual_length))
2210 if (put_user(urb->iso_frame_desc[i].status,
2211 &userurb->iso_frame_desc[i].status))
2216 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
2221 static int proc_reapurb_compat(struct usb_dev_state *ps, void __user *arg)
2223 struct async *as = reap_as(ps);
2228 snoop(&ps->dev->dev, "reap %px\n", as->userurb);
2229 retval = processcompl_compat(as, (void __user * __user *)arg);
2233 if (signal_pending(current))
2238 static int proc_reapurbnonblock_compat(struct usb_dev_state *ps, void __user *arg)
2243 as = async_getcompleted(ps);
2245 snoop(&ps->dev->dev, "reap %px\n", as->userurb);
2246 retval = processcompl_compat(as, (void __user * __user *)arg);
2249 retval = (connected(ps) ? -EAGAIN : -ENODEV);
2257 static int proc_disconnectsignal(struct usb_dev_state *ps, void __user *arg)
2259 struct usbdevfs_disconnectsignal ds;
2261 if (copy_from_user(&ds, arg, sizeof(ds)))
2263 ps->discsignr = ds.signr;
2264 ps->disccontext.sival_ptr = ds.context;
2268 static int proc_claiminterface(struct usb_dev_state *ps, void __user *arg)
2272 if (get_user(ifnum, (unsigned int __user *)arg))
2274 return claimintf(ps, ifnum);
2277 static int proc_releaseinterface(struct usb_dev_state *ps, void __user *arg)
2282 if (get_user(ifnum, (unsigned int __user *)arg))
2284 ret = releaseintf(ps, ifnum);
2287 destroy_async_on_interface(ps, ifnum);
2291 static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl)
2296 struct usb_interface *intf = NULL;
2297 struct usb_driver *driver = NULL;
2299 if (ps->privileges_dropped)
2306 size = _IOC_SIZE(ctl->ioctl_code);
2308 buf = kmalloc(size, GFP_KERNEL);
2311 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
2312 if (copy_from_user(buf, ctl->data, size)) {
2317 memset(buf, 0, size);
2321 if (ps->dev->state != USB_STATE_CONFIGURED)
2322 retval = -EHOSTUNREACH;
2323 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
2325 else switch (ctl->ioctl_code) {
2327 /* disconnect kernel driver from interface */
2328 case USBDEVFS_DISCONNECT:
2329 if (intf->dev.driver) {
2330 driver = to_usb_driver(intf->dev.driver);
2331 dev_dbg(&intf->dev, "disconnect by usbfs\n");
2332 usb_driver_release_interface(driver, intf);
2337 /* let kernel drivers try to (re)bind to the interface */
2338 case USBDEVFS_CONNECT:
2339 if (!intf->dev.driver)
2340 retval = device_attach(&intf->dev);
2345 /* talk directly to the interface's driver */
2347 if (intf->dev.driver)
2348 driver = to_usb_driver(intf->dev.driver);
2349 if (driver == NULL || driver->unlocked_ioctl == NULL) {
2352 retval = driver->unlocked_ioctl(intf, ctl->ioctl_code, buf);
2353 if (retval == -ENOIOCTLCMD)
2358 /* cleanup and return */
2360 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
2362 && copy_to_user(ctl->data, buf, size) != 0)
2369 static int proc_ioctl_default(struct usb_dev_state *ps, void __user *arg)
2371 struct usbdevfs_ioctl ctrl;
2373 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
2375 return proc_ioctl(ps, &ctrl);
2378 #ifdef CONFIG_COMPAT
2379 static int proc_ioctl_compat(struct usb_dev_state *ps, compat_uptr_t arg)
2381 struct usbdevfs_ioctl32 ioc32;
2382 struct usbdevfs_ioctl ctrl;
2384 if (copy_from_user(&ioc32, compat_ptr(arg), sizeof(ioc32)))
2386 ctrl.ifno = ioc32.ifno;
2387 ctrl.ioctl_code = ioc32.ioctl_code;
2388 ctrl.data = compat_ptr(ioc32.data);
2389 return proc_ioctl(ps, &ctrl);
2393 static int proc_claim_port(struct usb_dev_state *ps, void __user *arg)
2398 if (get_user(portnum, (unsigned __user *) arg))
2400 rc = usb_hub_claim_port(ps->dev, portnum, ps);
2402 snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n",
2403 portnum, task_pid_nr(current), current->comm);
2407 static int proc_release_port(struct usb_dev_state *ps, void __user *arg)
2411 if (get_user(portnum, (unsigned __user *) arg))
2413 return usb_hub_release_port(ps->dev, portnum, ps);
2416 static int proc_get_capabilities(struct usb_dev_state *ps, void __user *arg)
2420 caps = USBDEVFS_CAP_ZERO_PACKET | USBDEVFS_CAP_NO_PACKET_SIZE_LIM |
2421 USBDEVFS_CAP_REAP_AFTER_DISCONNECT | USBDEVFS_CAP_MMAP |
2422 USBDEVFS_CAP_DROP_PRIVILEGES |
2423 USBDEVFS_CAP_CONNINFO_EX | MAYBE_CAP_SUSPEND;
2424 if (!ps->dev->bus->no_stop_on_short)
2425 caps |= USBDEVFS_CAP_BULK_CONTINUATION;
2426 if (ps->dev->bus->sg_tablesize)
2427 caps |= USBDEVFS_CAP_BULK_SCATTER_GATHER;
2429 if (put_user(caps, (__u32 __user *)arg))
2435 static int proc_disconnect_claim(struct usb_dev_state *ps, void __user *arg)
2437 struct usbdevfs_disconnect_claim dc;
2438 struct usb_interface *intf;
2440 if (copy_from_user(&dc, arg, sizeof(dc)))
2443 intf = usb_ifnum_to_if(ps->dev, dc.interface);
2447 if (intf->dev.driver) {
2448 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
2450 if (ps->privileges_dropped)
2453 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_IF_DRIVER) &&
2454 strncmp(dc.driver, intf->dev.driver->name,
2455 sizeof(dc.driver)) != 0)
2458 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_EXCEPT_DRIVER) &&
2459 strncmp(dc.driver, intf->dev.driver->name,
2460 sizeof(dc.driver)) == 0)
2463 dev_dbg(&intf->dev, "disconnect by usbfs\n");
2464 usb_driver_release_interface(driver, intf);
2467 return claimintf(ps, dc.interface);
2470 static int proc_alloc_streams(struct usb_dev_state *ps, void __user *arg)
2472 unsigned num_streams, num_eps;
2473 struct usb_host_endpoint **eps;
2474 struct usb_interface *intf;
2477 r = parse_usbdevfs_streams(ps, arg, &num_streams, &num_eps,
2482 destroy_async_on_interface(ps,
2483 intf->altsetting[0].desc.bInterfaceNumber);
2485 r = usb_alloc_streams(intf, eps, num_eps, num_streams, GFP_KERNEL);
2490 static int proc_free_streams(struct usb_dev_state *ps, void __user *arg)
2493 struct usb_host_endpoint **eps;
2494 struct usb_interface *intf;
2497 r = parse_usbdevfs_streams(ps, arg, NULL, &num_eps, &eps, &intf);
2501 destroy_async_on_interface(ps,
2502 intf->altsetting[0].desc.bInterfaceNumber);
2504 r = usb_free_streams(intf, eps, num_eps, GFP_KERNEL);
2509 static int proc_drop_privileges(struct usb_dev_state *ps, void __user *arg)
2513 if (copy_from_user(&data, arg, sizeof(data)))
2516 /* This is a one way operation. Once privileges are
2517 * dropped, you cannot regain them. You may however reissue
2518 * this ioctl to shrink the allowed interfaces mask.
2520 ps->interface_allowed_mask &= data;
2521 ps->privileges_dropped = true;
2526 static int proc_forbid_suspend(struct usb_dev_state *ps)
2530 if (ps->suspend_allowed) {
2531 ret = usb_autoresume_device(ps->dev);
2533 ps->suspend_allowed = false;
2534 else if (ret != -ENODEV)
2540 static int proc_allow_suspend(struct usb_dev_state *ps)
2545 WRITE_ONCE(ps->not_yet_resumed, 1);
2546 if (!ps->suspend_allowed) {
2547 usb_autosuspend_device(ps->dev);
2548 ps->suspend_allowed = true;
2553 static int proc_wait_for_resume(struct usb_dev_state *ps)
2557 usb_unlock_device(ps->dev);
2558 ret = wait_event_interruptible(ps->wait_for_resume,
2559 READ_ONCE(ps->not_yet_resumed) == 0);
2560 usb_lock_device(ps->dev);
2564 return proc_forbid_suspend(ps);
2568 * NOTE: All requests here that have interface numbers as parameters
2569 * are assuming that somehow the configuration has been prevented from
2570 * changing. But there's no mechanism to ensure that...
2572 static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
2575 struct usb_dev_state *ps = file->private_data;
2576 struct inode *inode = file_inode(file);
2577 struct usb_device *dev = ps->dev;
2580 if (!(file->f_mode & FMODE_WRITE))
2583 usb_lock_device(dev);
2585 /* Reap operations are allowed even after disconnection */
2587 case USBDEVFS_REAPURB:
2588 snoop(&dev->dev, "%s: REAPURB\n", __func__);
2589 ret = proc_reapurb(ps, p);
2592 case USBDEVFS_REAPURBNDELAY:
2593 snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
2594 ret = proc_reapurbnonblock(ps, p);
2597 #ifdef CONFIG_COMPAT
2598 case USBDEVFS_REAPURB32:
2599 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
2600 ret = proc_reapurb_compat(ps, p);
2603 case USBDEVFS_REAPURBNDELAY32:
2604 snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
2605 ret = proc_reapurbnonblock_compat(ps, p);
2610 if (!connected(ps)) {
2611 usb_unlock_device(dev);
2616 case USBDEVFS_CONTROL:
2617 snoop(&dev->dev, "%s: CONTROL\n", __func__);
2618 ret = proc_control(ps, p);
2620 inode->i_mtime = current_time(inode);
2624 snoop(&dev->dev, "%s: BULK\n", __func__);
2625 ret = proc_bulk(ps, p);
2627 inode->i_mtime = current_time(inode);
2630 case USBDEVFS_RESETEP:
2631 snoop(&dev->dev, "%s: RESETEP\n", __func__);
2632 ret = proc_resetep(ps, p);
2634 inode->i_mtime = current_time(inode);
2637 case USBDEVFS_RESET:
2638 snoop(&dev->dev, "%s: RESET\n", __func__);
2639 ret = proc_resetdevice(ps);
2642 case USBDEVFS_CLEAR_HALT:
2643 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
2644 ret = proc_clearhalt(ps, p);
2646 inode->i_mtime = current_time(inode);
2649 case USBDEVFS_GETDRIVER:
2650 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
2651 ret = proc_getdriver(ps, p);
2654 case USBDEVFS_CONNECTINFO:
2655 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
2656 ret = proc_connectinfo(ps, p);
2659 case USBDEVFS_SETINTERFACE:
2660 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
2661 ret = proc_setintf(ps, p);
2664 case USBDEVFS_SETCONFIGURATION:
2665 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
2666 ret = proc_setconfig(ps, p);
2669 case USBDEVFS_SUBMITURB:
2670 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
2671 ret = proc_submiturb(ps, p);
2673 inode->i_mtime = current_time(inode);
2676 #ifdef CONFIG_COMPAT
2677 case USBDEVFS_CONTROL32:
2678 snoop(&dev->dev, "%s: CONTROL32\n", __func__);
2679 ret = proc_control_compat(ps, p);
2681 inode->i_mtime = current_time(inode);
2684 case USBDEVFS_BULK32:
2685 snoop(&dev->dev, "%s: BULK32\n", __func__);
2686 ret = proc_bulk_compat(ps, p);
2688 inode->i_mtime = current_time(inode);
2691 case USBDEVFS_DISCSIGNAL32:
2692 snoop(&dev->dev, "%s: DISCSIGNAL32\n", __func__);
2693 ret = proc_disconnectsignal_compat(ps, p);
2696 case USBDEVFS_SUBMITURB32:
2697 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
2698 ret = proc_submiturb_compat(ps, p);
2700 inode->i_mtime = current_time(inode);
2703 case USBDEVFS_IOCTL32:
2704 snoop(&dev->dev, "%s: IOCTL32\n", __func__);
2705 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
2709 case USBDEVFS_DISCARDURB:
2710 snoop(&dev->dev, "%s: DISCARDURB %px\n", __func__, p);
2711 ret = proc_unlinkurb(ps, p);
2714 case USBDEVFS_DISCSIGNAL:
2715 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
2716 ret = proc_disconnectsignal(ps, p);
2719 case USBDEVFS_CLAIMINTERFACE:
2720 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
2721 ret = proc_claiminterface(ps, p);
2724 case USBDEVFS_RELEASEINTERFACE:
2725 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
2726 ret = proc_releaseinterface(ps, p);
2729 case USBDEVFS_IOCTL:
2730 snoop(&dev->dev, "%s: IOCTL\n", __func__);
2731 ret = proc_ioctl_default(ps, p);
2734 case USBDEVFS_CLAIM_PORT:
2735 snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__);
2736 ret = proc_claim_port(ps, p);
2739 case USBDEVFS_RELEASE_PORT:
2740 snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__);
2741 ret = proc_release_port(ps, p);
2743 case USBDEVFS_GET_CAPABILITIES:
2744 ret = proc_get_capabilities(ps, p);
2746 case USBDEVFS_DISCONNECT_CLAIM:
2747 ret = proc_disconnect_claim(ps, p);
2749 case USBDEVFS_ALLOC_STREAMS:
2750 ret = proc_alloc_streams(ps, p);
2752 case USBDEVFS_FREE_STREAMS:
2753 ret = proc_free_streams(ps, p);
2755 case USBDEVFS_DROP_PRIVILEGES:
2756 ret = proc_drop_privileges(ps, p);
2758 case USBDEVFS_GET_SPEED:
2759 ret = ps->dev->speed;
2761 case USBDEVFS_FORBID_SUSPEND:
2762 ret = proc_forbid_suspend(ps);
2764 case USBDEVFS_ALLOW_SUSPEND:
2765 ret = proc_allow_suspend(ps);
2767 case USBDEVFS_WAIT_FOR_RESUME:
2768 ret = proc_wait_for_resume(ps);
2772 /* Handle variable-length commands */
2773 switch (cmd & ~IOCSIZE_MASK) {
2774 case USBDEVFS_CONNINFO_EX(0):
2775 ret = proc_conninfo_ex(ps, p, _IOC_SIZE(cmd));
2780 usb_unlock_device(dev);
2782 inode->i_atime = current_time(inode);
2786 static long usbdev_ioctl(struct file *file, unsigned int cmd,
2791 ret = usbdev_do_ioctl(file, cmd, (void __user *)arg);
2796 /* No kernel lock - fine */
2797 static __poll_t usbdev_poll(struct file *file,
2798 struct poll_table_struct *wait)
2800 struct usb_dev_state *ps = file->private_data;
2803 poll_wait(file, &ps->wait, wait);
2804 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
2805 mask |= EPOLLOUT | EPOLLWRNORM;
2808 if (list_empty(&ps->list))
2813 const struct file_operations usbdev_file_operations = {
2814 .owner = THIS_MODULE,
2815 .llseek = no_seek_end_llseek,
2816 .read = usbdev_read,
2817 .poll = usbdev_poll,
2818 .unlocked_ioctl = usbdev_ioctl,
2819 .compat_ioctl = compat_ptr_ioctl,
2820 .mmap = usbdev_mmap,
2821 .open = usbdev_open,
2822 .release = usbdev_release,
2825 static void usbdev_remove(struct usb_device *udev)
2827 struct usb_dev_state *ps;
2829 /* Protect against simultaneous resume */
2830 mutex_lock(&usbfs_mutex);
2831 while (!list_empty(&udev->filelist)) {
2832 ps = list_entry(udev->filelist.next, struct usb_dev_state, list);
2833 destroy_all_async(ps);
2834 wake_up_all(&ps->wait);
2835 WRITE_ONCE(ps->not_yet_resumed, 0);
2836 wake_up_all(&ps->wait_for_resume);
2837 list_del_init(&ps->list);
2839 kill_pid_usb_asyncio(ps->discsignr, EPIPE, ps->disccontext,
2840 ps->disc_pid, ps->cred);
2842 mutex_unlock(&usbfs_mutex);
2845 static int usbdev_notify(struct notifier_block *self,
2846 unsigned long action, void *dev)
2849 case USB_DEVICE_ADD:
2851 case USB_DEVICE_REMOVE:
2858 static struct notifier_block usbdev_nb = {
2859 .notifier_call = usbdev_notify,
2862 static struct cdev usb_device_cdev;
2864 int __init usb_devio_init(void)
2868 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
2871 printk(KERN_ERR "Unable to register minors for usb_device\n");
2874 cdev_init(&usb_device_cdev, &usbdev_file_operations);
2875 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
2877 printk(KERN_ERR "Unable to get usb_device major %d\n",
2881 usb_register_notify(&usbdev_nb);
2886 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
2890 void usb_devio_cleanup(void)
2892 usb_unregister_notify(&usbdev_nb);
2893 cdev_del(&usb_device_cdev);
2894 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);