1 // SPDX-License-Identifier: GPL-2.0+
2 /*****************************************************************************/
5 * devio.c -- User space communication with USB devices.
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/cdev.h>
36 #include <linux/notifier.h>
37 #include <linux/security.h>
38 #include <linux/user_namespace.h>
39 #include <linux/scatterlist.h>
40 #include <linux/uaccess.h>
41 #include <linux/dma-mapping.h>
42 #include <asm/byteorder.h>
43 #include <linux/moduleparam.h>
48 #define USB_DEVICE_MAX (USB_MAXBUS * 128)
49 #define USB_SG_SIZE 16384 /* split-size for large txs */
51 /* Mutual exclusion for removal, open, and release */
52 DEFINE_MUTEX(usbfs_mutex);
54 struct usb_dev_state {
55 struct list_head list; /* state list */
56 struct usb_device *dev;
58 spinlock_t lock; /* protects the async urb lists */
59 struct list_head async_pending;
60 struct list_head async_completed;
61 struct list_head memory_list;
62 wait_queue_head_t wait; /* wake up if a request completed */
63 unsigned int discsignr;
65 const struct cred *cred;
66 void __user *disccontext;
67 unsigned long ifclaimed;
69 u32 disabled_bulk_eps;
70 bool privileges_dropped;
71 unsigned long interface_allowed_mask;
75 struct list_head memlist;
80 dma_addr_t dma_handle;
81 unsigned long vm_start;
82 struct usb_dev_state *ps;
86 struct list_head asynclist;
87 struct usb_dev_state *ps;
89 const struct cred *cred;
92 void __user *userbuffer;
95 struct usb_memory *usbm;
96 unsigned int mem_usage;
103 static bool usbfs_snoop;
104 module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
105 MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
107 static unsigned usbfs_snoop_max = 65536;
108 module_param(usbfs_snoop_max, uint, S_IRUGO | S_IWUSR);
109 MODULE_PARM_DESC(usbfs_snoop_max,
110 "maximum number of bytes to print while snooping");
112 #define snoop(dev, format, arg...) \
115 dev_info(dev, format, ## arg); \
122 #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
124 /* Limit on the total amount of memory we can allocate for transfers */
125 static u32 usbfs_memory_mb = 16;
126 module_param(usbfs_memory_mb, uint, 0644);
127 MODULE_PARM_DESC(usbfs_memory_mb,
128 "maximum MB allowed for usbfs buffers (0 = no limit)");
130 /* Hard limit, necessary to avoid arithmetic overflow */
131 #define USBFS_XFER_MAX (UINT_MAX / 2 - 1000000)
133 static atomic64_t usbfs_memory_usage; /* Total memory currently allocated */
135 /* Check whether it's okay to allocate more memory for a transfer */
136 static int usbfs_increase_memory_usage(u64 amount)
140 lim = READ_ONCE(usbfs_memory_mb);
143 atomic64_add(amount, &usbfs_memory_usage);
145 if (lim > 0 && atomic64_read(&usbfs_memory_usage) > lim) {
146 atomic64_sub(amount, &usbfs_memory_usage);
153 /* Memory for a transfer is being deallocated */
154 static void usbfs_decrease_memory_usage(u64 amount)
156 atomic64_sub(amount, &usbfs_memory_usage);
159 static int connected(struct usb_dev_state *ps)
161 return (!list_empty(&ps->list) &&
162 ps->dev->state != USB_STATE_NOTATTACHED);
165 static void dec_usb_memory_use_count(struct usb_memory *usbm, int *count)
167 struct usb_dev_state *ps = usbm->ps;
170 spin_lock_irqsave(&ps->lock, flags);
172 if (usbm->urb_use_count == 0 && usbm->vma_use_count == 0) {
173 list_del(&usbm->memlist);
174 spin_unlock_irqrestore(&ps->lock, flags);
176 usb_free_coherent(ps->dev, usbm->size, usbm->mem,
178 usbfs_decrease_memory_usage(
179 usbm->size + sizeof(struct usb_memory));
182 spin_unlock_irqrestore(&ps->lock, flags);
186 static void usbdev_vm_open(struct vm_area_struct *vma)
188 struct usb_memory *usbm = vma->vm_private_data;
191 spin_lock_irqsave(&usbm->ps->lock, flags);
192 ++usbm->vma_use_count;
193 spin_unlock_irqrestore(&usbm->ps->lock, flags);
196 static void usbdev_vm_close(struct vm_area_struct *vma)
198 struct usb_memory *usbm = vma->vm_private_data;
200 dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
203 static const struct vm_operations_struct usbdev_vm_ops = {
204 .open = usbdev_vm_open,
205 .close = usbdev_vm_close
208 static int usbdev_mmap(struct file *file, struct vm_area_struct *vma)
210 struct usb_memory *usbm = NULL;
211 struct usb_dev_state *ps = file->private_data;
212 size_t size = vma->vm_end - vma->vm_start;
215 dma_addr_t dma_handle;
218 ret = usbfs_increase_memory_usage(size + sizeof(struct usb_memory));
222 usbm = kzalloc(sizeof(struct usb_memory), GFP_KERNEL);
225 goto error_decrease_mem;
228 mem = usb_alloc_coherent(ps->dev, size, GFP_USER | __GFP_NOWARN,
232 goto error_free_usbm;
235 memset(mem, 0, size);
238 usbm->dma_handle = dma_handle;
241 usbm->vm_start = vma->vm_start;
242 usbm->vma_use_count = 1;
243 INIT_LIST_HEAD(&usbm->memlist);
245 if (remap_pfn_range(vma, vma->vm_start,
246 virt_to_phys(usbm->mem) >> PAGE_SHIFT,
247 size, vma->vm_page_prot) < 0) {
248 dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
252 vma->vm_flags |= VM_IO;
253 vma->vm_flags |= (VM_DONTEXPAND | VM_DONTDUMP);
254 vma->vm_ops = &usbdev_vm_ops;
255 vma->vm_private_data = usbm;
257 spin_lock_irqsave(&ps->lock, flags);
258 list_add_tail(&usbm->memlist, &ps->memory_list);
259 spin_unlock_irqrestore(&ps->lock, flags);
266 usbfs_decrease_memory_usage(size + sizeof(struct usb_memory));
271 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
274 struct usb_dev_state *ps = file->private_data;
275 struct usb_device *dev = ps->dev;
282 usb_lock_device(dev);
283 if (!connected(ps)) {
286 } else if (pos < 0) {
291 if (pos < sizeof(struct usb_device_descriptor)) {
292 /* 18 bytes - fits on the stack */
293 struct usb_device_descriptor temp_desc;
295 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
296 le16_to_cpus(&temp_desc.bcdUSB);
297 le16_to_cpus(&temp_desc.idVendor);
298 le16_to_cpus(&temp_desc.idProduct);
299 le16_to_cpus(&temp_desc.bcdDevice);
301 len = sizeof(struct usb_device_descriptor) - pos;
304 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
315 pos = sizeof(struct usb_device_descriptor);
316 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
317 struct usb_config_descriptor *config =
318 (struct usb_config_descriptor *)dev->rawdescriptors[i];
319 unsigned int length = le16_to_cpu(config->wTotalLength);
321 if (*ppos < pos + length) {
323 /* The descriptor may claim to be longer than it
324 * really is. Here is the actual allocated length. */
326 le16_to_cpu(dev->config[i].desc.wTotalLength);
328 len = length - (*ppos - pos);
332 /* Simply don't write (skip over) unallocated parts */
333 if (alloclen > (*ppos - pos)) {
334 alloclen -= (*ppos - pos);
335 if (copy_to_user(buf,
336 dev->rawdescriptors[i] + (*ppos - pos),
337 min(len, alloclen))) {
353 usb_unlock_device(dev);
358 * async list handling
361 static struct async *alloc_async(unsigned int numisoframes)
365 as = kzalloc(sizeof(struct async), GFP_KERNEL);
368 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
376 static void free_async(struct async *as)
383 for (i = 0; i < as->urb->num_sgs; i++) {
384 if (sg_page(&as->urb->sg[i]))
385 kfree(sg_virt(&as->urb->sg[i]));
389 if (as->usbm == NULL)
390 kfree(as->urb->transfer_buffer);
392 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count);
394 kfree(as->urb->setup_packet);
395 usb_free_urb(as->urb);
396 usbfs_decrease_memory_usage(as->mem_usage);
400 static void async_newpending(struct async *as)
402 struct usb_dev_state *ps = as->ps;
405 spin_lock_irqsave(&ps->lock, flags);
406 list_add_tail(&as->asynclist, &ps->async_pending);
407 spin_unlock_irqrestore(&ps->lock, flags);
410 static void async_removepending(struct async *as)
412 struct usb_dev_state *ps = as->ps;
415 spin_lock_irqsave(&ps->lock, flags);
416 list_del_init(&as->asynclist);
417 spin_unlock_irqrestore(&ps->lock, flags);
420 static struct async *async_getcompleted(struct usb_dev_state *ps)
423 struct async *as = NULL;
425 spin_lock_irqsave(&ps->lock, flags);
426 if (!list_empty(&ps->async_completed)) {
427 as = list_entry(ps->async_completed.next, struct async,
429 list_del_init(&as->asynclist);
431 spin_unlock_irqrestore(&ps->lock, flags);
435 static struct async *async_getpending(struct usb_dev_state *ps,
436 void __user *userurb)
440 list_for_each_entry(as, &ps->async_pending, asynclist)
441 if (as->userurb == userurb) {
442 list_del_init(&as->asynclist);
449 static void snoop_urb(struct usb_device *udev,
450 void __user *userurb, int pipe, unsigned length,
451 int timeout_or_status, enum snoop_when when,
452 unsigned char *data, unsigned data_len)
454 static const char *types[] = {"isoc", "int", "ctrl", "bulk"};
455 static const char *dirs[] = {"out", "in"};
462 ep = usb_pipeendpoint(pipe);
463 t = types[usb_pipetype(pipe)];
464 d = dirs[!!usb_pipein(pipe)];
466 if (userurb) { /* Async */
468 dev_info(&udev->dev, "userurb %pK, ep%d %s-%s, "
470 userurb, ep, t, d, length);
472 dev_info(&udev->dev, "userurb %pK, ep%d %s-%s, "
473 "actual_length %u status %d\n",
474 userurb, ep, t, d, length,
478 dev_info(&udev->dev, "ep%d %s-%s, length %u, "
480 ep, t, d, length, timeout_or_status);
482 dev_info(&udev->dev, "ep%d %s-%s, actual_length %u, "
484 ep, t, d, length, timeout_or_status);
487 data_len = min(data_len, usbfs_snoop_max);
488 if (data && data_len > 0) {
489 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
494 static void snoop_urb_data(struct urb *urb, unsigned len)
498 len = min(len, usbfs_snoop_max);
499 if (!usbfs_snoop || len == 0)
502 if (urb->num_sgs == 0) {
503 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
504 urb->transfer_buffer, len, 1);
508 for (i = 0; i < urb->num_sgs && len; i++) {
509 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
510 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
511 sg_virt(&urb->sg[i]), size, 1);
516 static int copy_urb_data_to_user(u8 __user *userbuffer, struct urb *urb)
518 unsigned i, len, size;
520 if (urb->number_of_packets > 0) /* Isochronous */
521 len = urb->transfer_buffer_length;
523 len = urb->actual_length;
525 if (urb->num_sgs == 0) {
526 if (copy_to_user(userbuffer, urb->transfer_buffer, len))
531 for (i = 0; i < urb->num_sgs && len; i++) {
532 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
533 if (copy_to_user(userbuffer, sg_virt(&urb->sg[i]), size))
542 #define AS_CONTINUATION 1
545 static void cancel_bulk_urbs(struct usb_dev_state *ps, unsigned bulk_addr)
552 /* Mark all the pending URBs that match bulk_addr, up to but not
553 * including the first one without AS_CONTINUATION. If such an
554 * URB is encountered then a new transfer has already started so
555 * the endpoint doesn't need to be disabled; otherwise it does.
557 list_for_each_entry(as, &ps->async_pending, asynclist) {
558 if (as->bulk_addr == bulk_addr) {
559 if (as->bulk_status != AS_CONTINUATION)
561 as->bulk_status = AS_UNLINK;
565 ps->disabled_bulk_eps |= (1 << bulk_addr);
567 /* Now carefully unlink all the marked pending URBs */
569 list_for_each_entry(as, &ps->async_pending, asynclist) {
570 if (as->bulk_status == AS_UNLINK) {
571 as->bulk_status = 0; /* Only once */
574 spin_unlock(&ps->lock); /* Allow completions */
577 spin_lock(&ps->lock);
583 static void async_completed(struct urb *urb)
585 struct async *as = urb->context;
586 struct usb_dev_state *ps = as->ps;
587 struct siginfo sinfo;
588 struct pid *pid = NULL;
590 const struct cred *cred = NULL;
593 spin_lock(&ps->lock);
594 list_move_tail(&as->asynclist, &ps->async_completed);
595 as->status = urb->status;
598 memset(&sinfo, 0, sizeof(sinfo));
599 sinfo.si_signo = as->signr;
600 sinfo.si_errno = as->status;
601 sinfo.si_code = SI_ASYNCIO;
602 sinfo.si_addr = as->userurb;
603 pid = get_pid(as->pid);
604 cred = get_cred(as->cred);
607 snoop(&urb->dev->dev, "urb complete\n");
608 snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length,
609 as->status, COMPLETE, NULL, 0);
610 if ((urb->transfer_flags & URB_DIR_MASK) == URB_DIR_IN)
611 snoop_urb_data(urb, urb->actual_length);
613 if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET &&
614 as->status != -ENOENT)
615 cancel_bulk_urbs(ps, as->bulk_addr);
618 spin_unlock(&ps->lock);
621 kill_pid_info_as_cred(sinfo.si_signo, &sinfo, pid, cred, secid);
627 static void destroy_async(struct usb_dev_state *ps, struct list_head *list)
633 spin_lock_irqsave(&ps->lock, flags);
634 while (!list_empty(list)) {
635 as = list_entry(list->next, struct async, asynclist);
636 list_del_init(&as->asynclist);
640 /* drop the spinlock so the completion handler can run */
641 spin_unlock_irqrestore(&ps->lock, flags);
644 spin_lock_irqsave(&ps->lock, flags);
646 spin_unlock_irqrestore(&ps->lock, flags);
649 static void destroy_async_on_interface(struct usb_dev_state *ps,
652 struct list_head *p, *q, hitlist;
655 INIT_LIST_HEAD(&hitlist);
656 spin_lock_irqsave(&ps->lock, flags);
657 list_for_each_safe(p, q, &ps->async_pending)
658 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
659 list_move_tail(p, &hitlist);
660 spin_unlock_irqrestore(&ps->lock, flags);
661 destroy_async(ps, &hitlist);
664 static void destroy_all_async(struct usb_dev_state *ps)
666 destroy_async(ps, &ps->async_pending);
670 * interface claims are made only at the request of user level code,
671 * which can also release them (explicitly or by closing files).
672 * they're also undone when devices disconnect.
675 static int driver_probe(struct usb_interface *intf,
676 const struct usb_device_id *id)
681 static void driver_disconnect(struct usb_interface *intf)
683 struct usb_dev_state *ps = usb_get_intfdata(intf);
684 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
689 /* NOTE: this relies on usbcore having canceled and completed
690 * all pending I/O requests; 2.6 does that.
693 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
694 clear_bit(ifnum, &ps->ifclaimed);
696 dev_warn(&intf->dev, "interface number %u out of range\n",
699 usb_set_intfdata(intf, NULL);
701 /* force async requests to complete */
702 destroy_async_on_interface(ps, ifnum);
705 /* The following routines are merely placeholders. There is no way
706 * to inform a user task about suspend or resumes.
708 static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
713 static int driver_resume(struct usb_interface *intf)
718 struct usb_driver usbfs_driver = {
720 .probe = driver_probe,
721 .disconnect = driver_disconnect,
722 .suspend = driver_suspend,
723 .resume = driver_resume,
726 static int claimintf(struct usb_dev_state *ps, unsigned int ifnum)
728 struct usb_device *dev = ps->dev;
729 struct usb_interface *intf;
732 if (ifnum >= 8*sizeof(ps->ifclaimed))
734 /* already claimed */
735 if (test_bit(ifnum, &ps->ifclaimed))
738 if (ps->privileges_dropped &&
739 !test_bit(ifnum, &ps->interface_allowed_mask))
742 intf = usb_ifnum_to_if(dev, ifnum);
746 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
748 set_bit(ifnum, &ps->ifclaimed);
752 static int releaseintf(struct usb_dev_state *ps, unsigned int ifnum)
754 struct usb_device *dev;
755 struct usb_interface *intf;
759 if (ifnum >= 8*sizeof(ps->ifclaimed))
762 intf = usb_ifnum_to_if(dev, ifnum);
765 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
766 usb_driver_release_interface(&usbfs_driver, intf);
772 static int checkintf(struct usb_dev_state *ps, unsigned int ifnum)
774 if (ps->dev->state != USB_STATE_CONFIGURED)
775 return -EHOSTUNREACH;
776 if (ifnum >= 8*sizeof(ps->ifclaimed))
778 if (test_bit(ifnum, &ps->ifclaimed))
780 /* if not yet claimed, claim it for the driver */
781 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
782 "interface %u before use\n", task_pid_nr(current),
783 current->comm, ifnum);
784 return claimintf(ps, ifnum);
787 static int findintfep(struct usb_device *dev, unsigned int ep)
789 unsigned int i, j, e;
790 struct usb_interface *intf;
791 struct usb_host_interface *alts;
792 struct usb_endpoint_descriptor *endpt;
794 if (ep & ~(USB_DIR_IN|0xf))
798 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
799 intf = dev->actconfig->interface[i];
800 for (j = 0; j < intf->num_altsetting; j++) {
801 alts = &intf->altsetting[j];
802 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
803 endpt = &alts->endpoint[e].desc;
804 if (endpt->bEndpointAddress == ep)
805 return alts->desc.bInterfaceNumber;
812 static int check_ctrlrecip(struct usb_dev_state *ps, unsigned int requesttype,
813 unsigned int request, unsigned int index)
816 struct usb_host_interface *alt_setting;
818 if (ps->dev->state != USB_STATE_UNAUTHENTICATED
819 && ps->dev->state != USB_STATE_ADDRESS
820 && ps->dev->state != USB_STATE_CONFIGURED)
821 return -EHOSTUNREACH;
822 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
826 * check for the special corner case 'get_device_id' in the printer
827 * class specification, which we always want to allow as it is used
828 * to query things like ink level, etc.
830 if (requesttype == 0xa1 && request == 0) {
831 alt_setting = usb_find_alt_setting(ps->dev->actconfig,
832 index >> 8, index & 0xff);
834 && alt_setting->desc.bInterfaceClass == USB_CLASS_PRINTER)
839 switch (requesttype & USB_RECIP_MASK) {
840 case USB_RECIP_ENDPOINT:
841 if ((index & ~USB_DIR_IN) == 0)
843 ret = findintfep(ps->dev, index);
846 * Some not fully compliant Win apps seem to get
847 * index wrong and have the endpoint number here
848 * rather than the endpoint address (with the
849 * correct direction). Win does let this through,
850 * so we'll not reject it here but leave it to
851 * the device to not break KVM. But we warn.
853 ret = findintfep(ps->dev, index ^ 0x80);
855 dev_info(&ps->dev->dev,
856 "%s: process %i (%s) requesting ep %02x but needs %02x\n",
857 __func__, task_pid_nr(current),
858 current->comm, index, index ^ 0x80);
861 ret = checkintf(ps, ret);
864 case USB_RECIP_INTERFACE:
865 ret = checkintf(ps, index);
871 static struct usb_host_endpoint *ep_to_host_endpoint(struct usb_device *dev,
874 if (ep & USB_ENDPOINT_DIR_MASK)
875 return dev->ep_in[ep & USB_ENDPOINT_NUMBER_MASK];
877 return dev->ep_out[ep & USB_ENDPOINT_NUMBER_MASK];
880 static int parse_usbdevfs_streams(struct usb_dev_state *ps,
881 struct usbdevfs_streams __user *streams,
882 unsigned int *num_streams_ret,
883 unsigned int *num_eps_ret,
884 struct usb_host_endpoint ***eps_ret,
885 struct usb_interface **intf_ret)
887 unsigned int i, num_streams, num_eps;
888 struct usb_host_endpoint **eps;
889 struct usb_interface *intf = NULL;
893 if (get_user(num_streams, &streams->num_streams) ||
894 get_user(num_eps, &streams->num_eps))
897 if (num_eps < 1 || num_eps > USB_MAXENDPOINTS)
900 /* The XHCI controller allows max 2 ^ 16 streams */
901 if (num_streams_ret && (num_streams < 2 || num_streams > 65536))
904 eps = kmalloc(num_eps * sizeof(*eps), GFP_KERNEL);
908 for (i = 0; i < num_eps; i++) {
909 if (get_user(ep, &streams->eps[i])) {
913 eps[i] = ep_to_host_endpoint(ps->dev, ep);
919 /* usb_alloc/free_streams operate on an usb_interface */
920 ifnum = findintfep(ps->dev, ep);
927 ret = checkintf(ps, ifnum);
930 intf = usb_ifnum_to_if(ps->dev, ifnum);
932 /* Verify all eps belong to the same interface */
933 if (ifnum != intf->altsetting->desc.bInterfaceNumber) {
941 *num_streams_ret = num_streams;
942 *num_eps_ret = num_eps;
953 static int match_devt(struct device *dev, void *data)
955 return dev->devt == (dev_t) (unsigned long) data;
958 static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
962 dev = bus_find_device(&usb_bus_type, NULL,
963 (void *) (unsigned long) devt, match_devt);
966 return to_usb_device(dev);
972 static int usbdev_open(struct inode *inode, struct file *file)
974 struct usb_device *dev = NULL;
975 struct usb_dev_state *ps;
979 ps = kzalloc(sizeof(struct usb_dev_state), GFP_KERNEL);
985 /* Protect against simultaneous removal or release */
986 mutex_lock(&usbfs_mutex);
988 /* usbdev device-node */
989 if (imajor(inode) == USB_DEVICE_MAJOR)
990 dev = usbdev_lookup_by_devt(inode->i_rdev);
992 mutex_unlock(&usbfs_mutex);
997 usb_lock_device(dev);
998 if (dev->state == USB_STATE_NOTATTACHED)
999 goto out_unlock_device;
1001 ret = usb_autoresume_device(dev);
1003 goto out_unlock_device;
1007 ps->interface_allowed_mask = 0xFFFFFFFF; /* 32 bits */
1008 spin_lock_init(&ps->lock);
1009 INIT_LIST_HEAD(&ps->list);
1010 INIT_LIST_HEAD(&ps->async_pending);
1011 INIT_LIST_HEAD(&ps->async_completed);
1012 INIT_LIST_HEAD(&ps->memory_list);
1013 init_waitqueue_head(&ps->wait);
1014 ps->disc_pid = get_pid(task_pid(current));
1015 ps->cred = get_current_cred();
1016 security_task_getsecid(current, &ps->secid);
1018 list_add_tail(&ps->list, &dev->filelist);
1019 file->private_data = ps;
1020 usb_unlock_device(dev);
1021 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
1026 usb_unlock_device(dev);
1033 static int usbdev_release(struct inode *inode, struct file *file)
1035 struct usb_dev_state *ps = file->private_data;
1036 struct usb_device *dev = ps->dev;
1040 usb_lock_device(dev);
1041 usb_hub_release_all_ports(dev, ps);
1043 list_del_init(&ps->list);
1045 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
1047 if (test_bit(ifnum, &ps->ifclaimed))
1048 releaseintf(ps, ifnum);
1050 destroy_all_async(ps);
1051 usb_autosuspend_device(dev);
1052 usb_unlock_device(dev);
1054 put_pid(ps->disc_pid);
1057 as = async_getcompleted(ps);
1060 as = async_getcompleted(ps);
1067 static int proc_control(struct usb_dev_state *ps, void __user *arg)
1069 struct usb_device *dev = ps->dev;
1070 struct usbdevfs_ctrltransfer ctrl;
1072 unsigned char *tbuf;
1076 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1078 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.bRequest,
1082 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
1083 if (wLength > PAGE_SIZE)
1085 ret = usbfs_increase_memory_usage(PAGE_SIZE + sizeof(struct urb) +
1086 sizeof(struct usb_ctrlrequest));
1089 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
1095 snoop(&dev->dev, "control urb: bRequestType=%02x "
1096 "bRequest=%02x wValue=%04x "
1097 "wIndex=%04x wLength=%04x\n",
1098 ctrl.bRequestType, ctrl.bRequest, ctrl.wValue,
1099 ctrl.wIndex, ctrl.wLength);
1100 if (ctrl.bRequestType & 0x80) {
1101 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
1106 pipe = usb_rcvctrlpipe(dev, 0);
1107 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT, NULL, 0);
1109 usb_unlock_device(dev);
1110 i = usb_control_msg(dev, pipe, ctrl.bRequest,
1111 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
1112 tbuf, ctrl.wLength, tmo);
1113 usb_lock_device(dev);
1114 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE,
1116 if ((i > 0) && ctrl.wLength) {
1117 if (copy_to_user(ctrl.data, tbuf, i)) {
1124 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
1129 pipe = usb_sndctrlpipe(dev, 0);
1130 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT,
1131 tbuf, ctrl.wLength);
1133 usb_unlock_device(dev);
1134 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
1135 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
1136 tbuf, ctrl.wLength, tmo);
1137 usb_lock_device(dev);
1138 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE, NULL, 0);
1140 if (i < 0 && i != -EPIPE) {
1141 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
1142 "failed cmd %s rqt %u rq %u len %u ret %d\n",
1143 current->comm, ctrl.bRequestType, ctrl.bRequest,
1148 free_page((unsigned long) tbuf);
1149 usbfs_decrease_memory_usage(PAGE_SIZE + sizeof(struct urb) +
1150 sizeof(struct usb_ctrlrequest));
1154 static int proc_bulk(struct usb_dev_state *ps, void __user *arg)
1156 struct usb_device *dev = ps->dev;
1157 struct usbdevfs_bulktransfer bulk;
1158 unsigned int tmo, len1, pipe;
1160 unsigned char *tbuf;
1163 if (copy_from_user(&bulk, arg, sizeof(bulk)))
1165 ret = findintfep(ps->dev, bulk.ep);
1168 ret = checkintf(ps, ret);
1171 if (bulk.ep & USB_DIR_IN)
1172 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
1174 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
1175 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
1178 if (len1 >= (INT_MAX - sizeof(struct urb)))
1180 ret = usbfs_increase_memory_usage(len1 + sizeof(struct urb));
1183 tbuf = kmalloc(len1, GFP_KERNEL);
1189 if (bulk.ep & 0x80) {
1190 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
1194 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, NULL, 0);
1196 usb_unlock_device(dev);
1197 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
1198 usb_lock_device(dev);
1199 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, tbuf, len2);
1202 if (copy_to_user(bulk.data, tbuf, len2)) {
1209 if (copy_from_user(tbuf, bulk.data, len1)) {
1214 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, tbuf, len1);
1216 usb_unlock_device(dev);
1217 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
1218 usb_lock_device(dev);
1219 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, NULL, 0);
1221 ret = (i < 0 ? i : len2);
1224 usbfs_decrease_memory_usage(len1 + sizeof(struct urb));
1228 static void check_reset_of_active_ep(struct usb_device *udev,
1229 unsigned int epnum, char *ioctl_name)
1231 struct usb_host_endpoint **eps;
1232 struct usb_host_endpoint *ep;
1234 eps = (epnum & USB_DIR_IN) ? udev->ep_in : udev->ep_out;
1235 ep = eps[epnum & 0x0f];
1236 if (ep && !list_empty(&ep->urb_list))
1237 dev_warn(&udev->dev, "Process %d (%s) called USBDEVFS_%s for active endpoint 0x%02x\n",
1238 task_pid_nr(current), current->comm,
1242 static int proc_resetep(struct usb_dev_state *ps, void __user *arg)
1247 if (get_user(ep, (unsigned int __user *)arg))
1249 ret = findintfep(ps->dev, ep);
1252 ret = checkintf(ps, ret);
1255 check_reset_of_active_ep(ps->dev, ep, "RESETEP");
1256 usb_reset_endpoint(ps->dev, ep);
1260 static int proc_clearhalt(struct usb_dev_state *ps, void __user *arg)
1266 if (get_user(ep, (unsigned int __user *)arg))
1268 ret = findintfep(ps->dev, ep);
1271 ret = checkintf(ps, ret);
1274 check_reset_of_active_ep(ps->dev, ep, "CLEAR_HALT");
1275 if (ep & USB_DIR_IN)
1276 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
1278 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
1280 return usb_clear_halt(ps->dev, pipe);
1283 static int proc_getdriver(struct usb_dev_state *ps, void __user *arg)
1285 struct usbdevfs_getdriver gd;
1286 struct usb_interface *intf;
1289 if (copy_from_user(&gd, arg, sizeof(gd)))
1291 intf = usb_ifnum_to_if(ps->dev, gd.interface);
1292 if (!intf || !intf->dev.driver)
1295 strlcpy(gd.driver, intf->dev.driver->name,
1297 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
1302 static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg)
1304 struct usbdevfs_connectinfo ci;
1306 memset(&ci, 0, sizeof(ci));
1307 ci.devnum = ps->dev->devnum;
1308 ci.slow = ps->dev->speed == USB_SPEED_LOW;
1310 if (copy_to_user(arg, &ci, sizeof(ci)))
1315 static int proc_resetdevice(struct usb_dev_state *ps)
1317 struct usb_host_config *actconfig = ps->dev->actconfig;
1318 struct usb_interface *interface;
1321 /* Don't allow a device reset if the process has dropped the
1322 * privilege to do such things and any of the interfaces are
1323 * currently claimed.
1325 if (ps->privileges_dropped && actconfig) {
1326 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
1327 interface = actconfig->interface[i];
1328 number = interface->cur_altsetting->desc.bInterfaceNumber;
1329 if (usb_interface_claimed(interface) &&
1330 !test_bit(number, &ps->ifclaimed)) {
1331 dev_warn(&ps->dev->dev,
1332 "usbfs: interface %d claimed by %s while '%s' resets device\n",
1333 number, interface->dev.driver->name, current->comm);
1339 return usb_reset_device(ps->dev);
1342 static int proc_setintf(struct usb_dev_state *ps, void __user *arg)
1344 struct usbdevfs_setinterface setintf;
1347 if (copy_from_user(&setintf, arg, sizeof(setintf)))
1349 ret = checkintf(ps, setintf.interface);
1353 destroy_async_on_interface(ps, setintf.interface);
1355 return usb_set_interface(ps->dev, setintf.interface,
1356 setintf.altsetting);
1359 static int proc_setconfig(struct usb_dev_state *ps, void __user *arg)
1363 struct usb_host_config *actconfig;
1365 if (get_user(u, (int __user *)arg))
1368 actconfig = ps->dev->actconfig;
1370 /* Don't touch the device if any interfaces are claimed.
1371 * It could interfere with other drivers' operations, and if
1372 * an interface is claimed by usbfs it could easily deadlock.
1377 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
1378 if (usb_interface_claimed(actconfig->interface[i])) {
1379 dev_warn(&ps->dev->dev,
1380 "usbfs: interface %d claimed by %s "
1381 "while '%s' sets config #%d\n",
1382 actconfig->interface[i]
1384 ->desc.bInterfaceNumber,
1385 actconfig->interface[i]
1394 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
1395 * so avoid usb_set_configuration()'s kick to sysfs
1398 if (actconfig && actconfig->desc.bConfigurationValue == u)
1399 status = usb_reset_configuration(ps->dev);
1401 status = usb_set_configuration(ps->dev, u);
1407 static struct usb_memory *
1408 find_memory_area(struct usb_dev_state *ps, const struct usbdevfs_urb *uurb)
1410 struct usb_memory *usbm = NULL, *iter;
1411 unsigned long flags;
1412 unsigned long uurb_start = (unsigned long)uurb->buffer;
1414 spin_lock_irqsave(&ps->lock, flags);
1415 list_for_each_entry(iter, &ps->memory_list, memlist) {
1416 if (uurb_start >= iter->vm_start &&
1417 uurb_start < iter->vm_start + iter->size) {
1418 if (uurb->buffer_length > iter->vm_start + iter->size -
1420 usbm = ERR_PTR(-EINVAL);
1423 usbm->urb_use_count++;
1428 spin_unlock_irqrestore(&ps->lock, flags);
1432 static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb,
1433 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
1436 struct usbdevfs_iso_packet_desc *isopkt = NULL;
1437 struct usb_host_endpoint *ep;
1438 struct async *as = NULL;
1439 struct usb_ctrlrequest *dr = NULL;
1440 unsigned int u, totlen, isofrmlen;
1441 int i, ret, is_in, num_sgs = 0, ifnum = -1;
1442 int number_of_packets = 0;
1443 unsigned int stream_id = 0;
1445 unsigned long mask = USBDEVFS_URB_SHORT_NOT_OK |
1446 USBDEVFS_URB_BULK_CONTINUATION |
1447 USBDEVFS_URB_NO_FSBR |
1448 USBDEVFS_URB_ZERO_PACKET |
1449 USBDEVFS_URB_NO_INTERRUPT;
1450 /* USBDEVFS_URB_ISO_ASAP is a special case */
1451 if (uurb->type == USBDEVFS_URB_TYPE_ISO)
1452 mask |= USBDEVFS_URB_ISO_ASAP;
1454 if (uurb->flags & ~mask)
1457 if ((unsigned int)uurb->buffer_length >= USBFS_XFER_MAX)
1459 if (uurb->buffer_length > 0 && !uurb->buffer)
1461 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
1462 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
1463 ifnum = findintfep(ps->dev, uurb->endpoint);
1466 ret = checkintf(ps, ifnum);
1470 ep = ep_to_host_endpoint(ps->dev, uurb->endpoint);
1473 is_in = (uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0;
1476 switch (uurb->type) {
1477 case USBDEVFS_URB_TYPE_CONTROL:
1478 if (!usb_endpoint_xfer_control(&ep->desc))
1480 /* min 8 byte setup packet */
1481 if (uurb->buffer_length < 8)
1483 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1486 if (copy_from_user(dr, uurb->buffer, 8)) {
1490 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
1494 ret = check_ctrlrecip(ps, dr->bRequestType, dr->bRequest,
1495 le16_to_cpup(&dr->wIndex));
1498 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1500 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1502 uurb->endpoint |= USB_DIR_IN;
1505 uurb->endpoint &= ~USB_DIR_IN;
1507 snoop(&ps->dev->dev, "control urb: bRequestType=%02x "
1508 "bRequest=%02x wValue=%04x "
1509 "wIndex=%04x wLength=%04x\n",
1510 dr->bRequestType, dr->bRequest,
1511 __le16_to_cpup(&dr->wValue),
1512 __le16_to_cpup(&dr->wIndex),
1513 __le16_to_cpup(&dr->wLength));
1514 u = sizeof(struct usb_ctrlrequest);
1517 case USBDEVFS_URB_TYPE_BULK:
1518 switch (usb_endpoint_type(&ep->desc)) {
1519 case USB_ENDPOINT_XFER_CONTROL:
1520 case USB_ENDPOINT_XFER_ISOC:
1522 case USB_ENDPOINT_XFER_INT:
1523 /* allow single-shot interrupt transfers */
1524 uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
1527 num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE);
1528 if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize)
1531 stream_id = uurb->stream_id;
1534 case USBDEVFS_URB_TYPE_INTERRUPT:
1535 if (!usb_endpoint_xfer_int(&ep->desc))
1540 case USBDEVFS_URB_TYPE_ISO:
1541 /* arbitrary limit */
1542 if (uurb->number_of_packets < 1 ||
1543 uurb->number_of_packets > 128)
1545 if (!usb_endpoint_xfer_isoc(&ep->desc))
1547 number_of_packets = uurb->number_of_packets;
1548 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1550 isopkt = memdup_user(iso_frame_desc, isofrmlen);
1551 if (IS_ERR(isopkt)) {
1552 ret = PTR_ERR(isopkt);
1556 for (totlen = u = 0; u < number_of_packets; u++) {
1558 * arbitrary limit need for USB 3.0
1559 * bMaxBurst (0~15 allowed, 1~16 packets)
1560 * bmAttributes (bit 1:0, mult 0~2, 1~3 packets)
1561 * sizemax: 1024 * 16 * 3 = 49152
1563 if (isopkt[u].length > 49152) {
1567 totlen += isopkt[u].length;
1569 u *= sizeof(struct usb_iso_packet_descriptor);
1570 uurb->buffer_length = totlen;
1577 if (uurb->buffer_length > 0 &&
1578 !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1579 uurb->buffer, uurb->buffer_length)) {
1583 as = alloc_async(number_of_packets);
1589 as->usbm = find_memory_area(ps, uurb);
1590 if (IS_ERR(as->usbm)) {
1591 ret = PTR_ERR(as->usbm);
1596 /* do not use SG buffers when memory mapped segments
1602 u += sizeof(struct async) + sizeof(struct urb) + uurb->buffer_length +
1603 num_sgs * sizeof(struct scatterlist);
1604 ret = usbfs_increase_memory_usage(u);
1610 as->urb->sg = kmalloc(num_sgs * sizeof(struct scatterlist),
1616 as->urb->num_sgs = num_sgs;
1617 sg_init_table(as->urb->sg, as->urb->num_sgs);
1619 totlen = uurb->buffer_length;
1620 for (i = 0; i < as->urb->num_sgs; i++) {
1621 u = (totlen > USB_SG_SIZE) ? USB_SG_SIZE : totlen;
1622 buf = kmalloc(u, GFP_KERNEL);
1627 sg_set_buf(&as->urb->sg[i], buf, u);
1630 if (copy_from_user(buf, uurb->buffer, u)) {
1638 } else if (uurb->buffer_length > 0) {
1640 unsigned long uurb_start = (unsigned long)uurb->buffer;
1642 as->urb->transfer_buffer = as->usbm->mem +
1643 (uurb_start - as->usbm->vm_start);
1645 as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
1647 if (!as->urb->transfer_buffer) {
1652 if (copy_from_user(as->urb->transfer_buffer,
1654 uurb->buffer_length)) {
1658 } else if (uurb->type == USBDEVFS_URB_TYPE_ISO) {
1660 * Isochronous input data may end up being
1661 * discontiguous if some of the packets are
1662 * short. Clear the buffer so that the gaps
1663 * don't leak kernel data to userspace.
1665 memset(as->urb->transfer_buffer, 0,
1666 uurb->buffer_length);
1670 as->urb->dev = ps->dev;
1671 as->urb->pipe = (uurb->type << 30) |
1672 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1673 (uurb->endpoint & USB_DIR_IN);
1675 /* This tedious sequence is necessary because the URB_* flags
1676 * are internal to the kernel and subject to change, whereas
1677 * the USBDEVFS_URB_* flags are a user API and must not be changed.
1679 u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
1680 if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
1682 if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK && is_in)
1683 u |= URB_SHORT_NOT_OK;
1684 if (uurb->flags & USBDEVFS_URB_NO_FSBR)
1686 if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1687 u |= URB_ZERO_PACKET;
1688 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
1689 u |= URB_NO_INTERRUPT;
1690 as->urb->transfer_flags = u;
1692 as->urb->transfer_buffer_length = uurb->buffer_length;
1693 as->urb->setup_packet = (unsigned char *)dr;
1695 as->urb->start_frame = uurb->start_frame;
1696 as->urb->number_of_packets = number_of_packets;
1697 as->urb->stream_id = stream_id;
1699 if (ep->desc.bInterval) {
1700 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1701 ps->dev->speed == USB_SPEED_HIGH ||
1702 ps->dev->speed >= USB_SPEED_SUPER)
1703 as->urb->interval = 1 <<
1704 min(15, ep->desc.bInterval - 1);
1706 as->urb->interval = ep->desc.bInterval;
1709 as->urb->context = as;
1710 as->urb->complete = async_completed;
1711 for (totlen = u = 0; u < number_of_packets; u++) {
1712 as->urb->iso_frame_desc[u].offset = totlen;
1713 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1714 totlen += isopkt[u].length;
1721 unsigned long uurb_start = (unsigned long)uurb->buffer;
1723 as->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1724 as->urb->transfer_dma = as->usbm->dma_handle +
1725 (uurb_start - as->usbm->vm_start);
1726 } else if (is_in && uurb->buffer_length > 0)
1727 as->userbuffer = uurb->buffer;
1728 as->signr = uurb->signr;
1730 as->pid = get_pid(task_pid(current));
1731 as->cred = get_current_cred();
1732 security_task_getsecid(current, &as->secid);
1733 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1734 as->urb->transfer_buffer_length, 0, SUBMIT,
1737 snoop_urb_data(as->urb, as->urb->transfer_buffer_length);
1739 async_newpending(as);
1741 if (usb_endpoint_xfer_bulk(&ep->desc)) {
1742 spin_lock_irq(&ps->lock);
1744 /* Not exactly the endpoint address; the direction bit is
1745 * shifted to the 0x10 position so that the value will be
1748 as->bulk_addr = usb_endpoint_num(&ep->desc) |
1749 ((ep->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1752 /* If this bulk URB is the start of a new transfer, re-enable
1753 * the endpoint. Otherwise mark it as a continuation URB.
1755 if (uurb->flags & USBDEVFS_URB_BULK_CONTINUATION)
1756 as->bulk_status = AS_CONTINUATION;
1758 ps->disabled_bulk_eps &= ~(1 << as->bulk_addr);
1760 /* Don't accept continuation URBs if the endpoint is
1761 * disabled because of an earlier error.
1763 if (ps->disabled_bulk_eps & (1 << as->bulk_addr))
1766 ret = usb_submit_urb(as->urb, GFP_ATOMIC);
1767 spin_unlock_irq(&ps->lock);
1769 ret = usb_submit_urb(as->urb, GFP_KERNEL);
1773 dev_printk(KERN_DEBUG, &ps->dev->dev,
1774 "usbfs: usb_submit_urb returned %d\n", ret);
1775 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1776 0, ret, COMPLETE, NULL, 0);
1777 async_removepending(as);
1784 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count);
1792 static int proc_submiturb(struct usb_dev_state *ps, void __user *arg)
1794 struct usbdevfs_urb uurb;
1796 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1799 return proc_do_submiturb(ps, &uurb,
1800 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1804 static int proc_unlinkurb(struct usb_dev_state *ps, void __user *arg)
1808 unsigned long flags;
1810 spin_lock_irqsave(&ps->lock, flags);
1811 as = async_getpending(ps, arg);
1813 spin_unlock_irqrestore(&ps->lock, flags);
1819 spin_unlock_irqrestore(&ps->lock, flags);
1827 static void compute_isochronous_actual_length(struct urb *urb)
1831 if (urb->number_of_packets > 0) {
1832 urb->actual_length = 0;
1833 for (i = 0; i < urb->number_of_packets; i++)
1834 urb->actual_length +=
1835 urb->iso_frame_desc[i].actual_length;
1839 static int processcompl(struct async *as, void __user * __user *arg)
1841 struct urb *urb = as->urb;
1842 struct usbdevfs_urb __user *userurb = as->userurb;
1843 void __user *addr = as->userurb;
1846 compute_isochronous_actual_length(urb);
1847 if (as->userbuffer && urb->actual_length) {
1848 if (copy_urb_data_to_user(as->userbuffer, urb))
1851 if (put_user(as->status, &userurb->status))
1853 if (put_user(urb->actual_length, &userurb->actual_length))
1855 if (put_user(urb->error_count, &userurb->error_count))
1858 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1859 for (i = 0; i < urb->number_of_packets; i++) {
1860 if (put_user(urb->iso_frame_desc[i].actual_length,
1861 &userurb->iso_frame_desc[i].actual_length))
1863 if (put_user(urb->iso_frame_desc[i].status,
1864 &userurb->iso_frame_desc[i].status))
1869 if (put_user(addr, (void __user * __user *)arg))
1877 static struct async *reap_as(struct usb_dev_state *ps)
1879 DECLARE_WAITQUEUE(wait, current);
1880 struct async *as = NULL;
1881 struct usb_device *dev = ps->dev;
1883 add_wait_queue(&ps->wait, &wait);
1885 __set_current_state(TASK_INTERRUPTIBLE);
1886 as = async_getcompleted(ps);
1887 if (as || !connected(ps))
1889 if (signal_pending(current))
1891 usb_unlock_device(dev);
1893 usb_lock_device(dev);
1895 remove_wait_queue(&ps->wait, &wait);
1896 set_current_state(TASK_RUNNING);
1900 static int proc_reapurb(struct usb_dev_state *ps, void __user *arg)
1902 struct async *as = reap_as(ps);
1907 snoop(&ps->dev->dev, "reap %pK\n", as->userurb);
1908 retval = processcompl(as, (void __user * __user *)arg);
1912 if (signal_pending(current))
1917 static int proc_reapurbnonblock(struct usb_dev_state *ps, void __user *arg)
1922 as = async_getcompleted(ps);
1924 snoop(&ps->dev->dev, "reap %pK\n", as->userurb);
1925 retval = processcompl(as, (void __user * __user *)arg);
1928 retval = (connected(ps) ? -EAGAIN : -ENODEV);
1933 #ifdef CONFIG_COMPAT
1934 static int proc_control_compat(struct usb_dev_state *ps,
1935 struct usbdevfs_ctrltransfer32 __user *p32)
1937 struct usbdevfs_ctrltransfer __user *p;
1939 p = compat_alloc_user_space(sizeof(*p));
1940 if (copy_in_user(p, p32, (sizeof(*p32) - sizeof(compat_caddr_t))) ||
1941 get_user(udata, &p32->data) ||
1942 put_user(compat_ptr(udata), &p->data))
1944 return proc_control(ps, p);
1947 static int proc_bulk_compat(struct usb_dev_state *ps,
1948 struct usbdevfs_bulktransfer32 __user *p32)
1950 struct usbdevfs_bulktransfer __user *p;
1952 compat_caddr_t addr;
1954 p = compat_alloc_user_space(sizeof(*p));
1956 if (get_user(n, &p32->ep) || put_user(n, &p->ep) ||
1957 get_user(n, &p32->len) || put_user(n, &p->len) ||
1958 get_user(n, &p32->timeout) || put_user(n, &p->timeout) ||
1959 get_user(addr, &p32->data) || put_user(compat_ptr(addr), &p->data))
1962 return proc_bulk(ps, p);
1964 static int proc_disconnectsignal_compat(struct usb_dev_state *ps, void __user *arg)
1966 struct usbdevfs_disconnectsignal32 ds;
1968 if (copy_from_user(&ds, arg, sizeof(ds)))
1970 ps->discsignr = ds.signr;
1971 ps->disccontext = compat_ptr(ds.context);
1975 static int get_urb32(struct usbdevfs_urb *kurb,
1976 struct usbdevfs_urb32 __user *uurb)
1978 struct usbdevfs_urb32 urb32;
1979 if (copy_from_user(&urb32, uurb, sizeof(*uurb)))
1981 kurb->type = urb32.type;
1982 kurb->endpoint = urb32.endpoint;
1983 kurb->status = urb32.status;
1984 kurb->flags = urb32.flags;
1985 kurb->buffer = compat_ptr(urb32.buffer);
1986 kurb->buffer_length = urb32.buffer_length;
1987 kurb->actual_length = urb32.actual_length;
1988 kurb->start_frame = urb32.start_frame;
1989 kurb->number_of_packets = urb32.number_of_packets;
1990 kurb->error_count = urb32.error_count;
1991 kurb->signr = urb32.signr;
1992 kurb->usercontext = compat_ptr(urb32.usercontext);
1996 static int proc_submiturb_compat(struct usb_dev_state *ps, void __user *arg)
1998 struct usbdevfs_urb uurb;
2000 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
2003 return proc_do_submiturb(ps, &uurb,
2004 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
2008 static int processcompl_compat(struct async *as, void __user * __user *arg)
2010 struct urb *urb = as->urb;
2011 struct usbdevfs_urb32 __user *userurb = as->userurb;
2012 void __user *addr = as->userurb;
2015 compute_isochronous_actual_length(urb);
2016 if (as->userbuffer && urb->actual_length) {
2017 if (copy_urb_data_to_user(as->userbuffer, urb))
2020 if (put_user(as->status, &userurb->status))
2022 if (put_user(urb->actual_length, &userurb->actual_length))
2024 if (put_user(urb->error_count, &userurb->error_count))
2027 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
2028 for (i = 0; i < urb->number_of_packets; i++) {
2029 if (put_user(urb->iso_frame_desc[i].actual_length,
2030 &userurb->iso_frame_desc[i].actual_length))
2032 if (put_user(urb->iso_frame_desc[i].status,
2033 &userurb->iso_frame_desc[i].status))
2038 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
2043 static int proc_reapurb_compat(struct usb_dev_state *ps, void __user *arg)
2045 struct async *as = reap_as(ps);
2050 snoop(&ps->dev->dev, "reap %pK\n", as->userurb);
2051 retval = processcompl_compat(as, (void __user * __user *)arg);
2055 if (signal_pending(current))
2060 static int proc_reapurbnonblock_compat(struct usb_dev_state *ps, void __user *arg)
2065 as = async_getcompleted(ps);
2067 snoop(&ps->dev->dev, "reap %pK\n", as->userurb);
2068 retval = processcompl_compat(as, (void __user * __user *)arg);
2071 retval = (connected(ps) ? -EAGAIN : -ENODEV);
2079 static int proc_disconnectsignal(struct usb_dev_state *ps, void __user *arg)
2081 struct usbdevfs_disconnectsignal ds;
2083 if (copy_from_user(&ds, arg, sizeof(ds)))
2085 ps->discsignr = ds.signr;
2086 ps->disccontext = ds.context;
2090 static int proc_claiminterface(struct usb_dev_state *ps, void __user *arg)
2094 if (get_user(ifnum, (unsigned int __user *)arg))
2096 return claimintf(ps, ifnum);
2099 static int proc_releaseinterface(struct usb_dev_state *ps, void __user *arg)
2104 if (get_user(ifnum, (unsigned int __user *)arg))
2106 ret = releaseintf(ps, ifnum);
2109 destroy_async_on_interface(ps, ifnum);
2113 static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl)
2118 struct usb_interface *intf = NULL;
2119 struct usb_driver *driver = NULL;
2121 if (ps->privileges_dropped)
2125 size = _IOC_SIZE(ctl->ioctl_code);
2127 buf = kmalloc(size, GFP_KERNEL);
2130 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
2131 if (copy_from_user(buf, ctl->data, size)) {
2136 memset(buf, 0, size);
2140 if (!connected(ps)) {
2145 if (ps->dev->state != USB_STATE_CONFIGURED)
2146 retval = -EHOSTUNREACH;
2147 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
2149 else switch (ctl->ioctl_code) {
2151 /* disconnect kernel driver from interface */
2152 case USBDEVFS_DISCONNECT:
2153 if (intf->dev.driver) {
2154 driver = to_usb_driver(intf->dev.driver);
2155 dev_dbg(&intf->dev, "disconnect by usbfs\n");
2156 usb_driver_release_interface(driver, intf);
2161 /* let kernel drivers try to (re)bind to the interface */
2162 case USBDEVFS_CONNECT:
2163 if (!intf->dev.driver)
2164 retval = device_attach(&intf->dev);
2169 /* talk directly to the interface's driver */
2171 if (intf->dev.driver)
2172 driver = to_usb_driver(intf->dev.driver);
2173 if (driver == NULL || driver->unlocked_ioctl == NULL) {
2176 retval = driver->unlocked_ioctl(intf, ctl->ioctl_code, buf);
2177 if (retval == -ENOIOCTLCMD)
2182 /* cleanup and return */
2184 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
2186 && copy_to_user(ctl->data, buf, size) != 0)
2193 static int proc_ioctl_default(struct usb_dev_state *ps, void __user *arg)
2195 struct usbdevfs_ioctl ctrl;
2197 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
2199 return proc_ioctl(ps, &ctrl);
2202 #ifdef CONFIG_COMPAT
2203 static int proc_ioctl_compat(struct usb_dev_state *ps, compat_uptr_t arg)
2205 struct usbdevfs_ioctl32 ioc32;
2206 struct usbdevfs_ioctl ctrl;
2208 if (copy_from_user(&ioc32, compat_ptr(arg), sizeof(ioc32)))
2210 ctrl.ifno = ioc32.ifno;
2211 ctrl.ioctl_code = ioc32.ioctl_code;
2212 ctrl.data = compat_ptr(ioc32.data);
2213 return proc_ioctl(ps, &ctrl);
2217 static int proc_claim_port(struct usb_dev_state *ps, void __user *arg)
2222 if (get_user(portnum, (unsigned __user *) arg))
2224 rc = usb_hub_claim_port(ps->dev, portnum, ps);
2226 snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n",
2227 portnum, task_pid_nr(current), current->comm);
2231 static int proc_release_port(struct usb_dev_state *ps, void __user *arg)
2235 if (get_user(portnum, (unsigned __user *) arg))
2237 return usb_hub_release_port(ps->dev, portnum, ps);
2240 static int proc_get_capabilities(struct usb_dev_state *ps, void __user *arg)
2244 caps = USBDEVFS_CAP_ZERO_PACKET | USBDEVFS_CAP_NO_PACKET_SIZE_LIM |
2245 USBDEVFS_CAP_REAP_AFTER_DISCONNECT | USBDEVFS_CAP_MMAP |
2246 USBDEVFS_CAP_DROP_PRIVILEGES;
2247 if (!ps->dev->bus->no_stop_on_short)
2248 caps |= USBDEVFS_CAP_BULK_CONTINUATION;
2249 if (ps->dev->bus->sg_tablesize)
2250 caps |= USBDEVFS_CAP_BULK_SCATTER_GATHER;
2252 if (put_user(caps, (__u32 __user *)arg))
2258 static int proc_disconnect_claim(struct usb_dev_state *ps, void __user *arg)
2260 struct usbdevfs_disconnect_claim dc;
2261 struct usb_interface *intf;
2263 if (copy_from_user(&dc, arg, sizeof(dc)))
2266 intf = usb_ifnum_to_if(ps->dev, dc.interface);
2270 if (intf->dev.driver) {
2271 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
2273 if (ps->privileges_dropped)
2276 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_IF_DRIVER) &&
2277 strncmp(dc.driver, intf->dev.driver->name,
2278 sizeof(dc.driver)) != 0)
2281 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_EXCEPT_DRIVER) &&
2282 strncmp(dc.driver, intf->dev.driver->name,
2283 sizeof(dc.driver)) == 0)
2286 dev_dbg(&intf->dev, "disconnect by usbfs\n");
2287 usb_driver_release_interface(driver, intf);
2290 return claimintf(ps, dc.interface);
2293 static int proc_alloc_streams(struct usb_dev_state *ps, void __user *arg)
2295 unsigned num_streams, num_eps;
2296 struct usb_host_endpoint **eps;
2297 struct usb_interface *intf;
2300 r = parse_usbdevfs_streams(ps, arg, &num_streams, &num_eps,
2305 destroy_async_on_interface(ps,
2306 intf->altsetting[0].desc.bInterfaceNumber);
2308 r = usb_alloc_streams(intf, eps, num_eps, num_streams, GFP_KERNEL);
2313 static int proc_free_streams(struct usb_dev_state *ps, void __user *arg)
2316 struct usb_host_endpoint **eps;
2317 struct usb_interface *intf;
2320 r = parse_usbdevfs_streams(ps, arg, NULL, &num_eps, &eps, &intf);
2324 destroy_async_on_interface(ps,
2325 intf->altsetting[0].desc.bInterfaceNumber);
2327 r = usb_free_streams(intf, eps, num_eps, GFP_KERNEL);
2332 static int proc_drop_privileges(struct usb_dev_state *ps, void __user *arg)
2336 if (copy_from_user(&data, arg, sizeof(data)))
2339 /* This is a one way operation. Once privileges are
2340 * dropped, you cannot regain them. You may however reissue
2341 * this ioctl to shrink the allowed interfaces mask.
2343 ps->interface_allowed_mask &= data;
2344 ps->privileges_dropped = true;
2350 * NOTE: All requests here that have interface numbers as parameters
2351 * are assuming that somehow the configuration has been prevented from
2352 * changing. But there's no mechanism to ensure that...
2354 static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
2357 struct usb_dev_state *ps = file->private_data;
2358 struct inode *inode = file_inode(file);
2359 struct usb_device *dev = ps->dev;
2362 if (!(file->f_mode & FMODE_WRITE))
2365 usb_lock_device(dev);
2367 /* Reap operations are allowed even after disconnection */
2369 case USBDEVFS_REAPURB:
2370 snoop(&dev->dev, "%s: REAPURB\n", __func__);
2371 ret = proc_reapurb(ps, p);
2374 case USBDEVFS_REAPURBNDELAY:
2375 snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
2376 ret = proc_reapurbnonblock(ps, p);
2379 #ifdef CONFIG_COMPAT
2380 case USBDEVFS_REAPURB32:
2381 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
2382 ret = proc_reapurb_compat(ps, p);
2385 case USBDEVFS_REAPURBNDELAY32:
2386 snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
2387 ret = proc_reapurbnonblock_compat(ps, p);
2392 if (!connected(ps)) {
2393 usb_unlock_device(dev);
2398 case USBDEVFS_CONTROL:
2399 snoop(&dev->dev, "%s: CONTROL\n", __func__);
2400 ret = proc_control(ps, p);
2402 inode->i_mtime = current_time(inode);
2406 snoop(&dev->dev, "%s: BULK\n", __func__);
2407 ret = proc_bulk(ps, p);
2409 inode->i_mtime = current_time(inode);
2412 case USBDEVFS_RESETEP:
2413 snoop(&dev->dev, "%s: RESETEP\n", __func__);
2414 ret = proc_resetep(ps, p);
2416 inode->i_mtime = current_time(inode);
2419 case USBDEVFS_RESET:
2420 snoop(&dev->dev, "%s: RESET\n", __func__);
2421 ret = proc_resetdevice(ps);
2424 case USBDEVFS_CLEAR_HALT:
2425 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
2426 ret = proc_clearhalt(ps, p);
2428 inode->i_mtime = current_time(inode);
2431 case USBDEVFS_GETDRIVER:
2432 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
2433 ret = proc_getdriver(ps, p);
2436 case USBDEVFS_CONNECTINFO:
2437 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
2438 ret = proc_connectinfo(ps, p);
2441 case USBDEVFS_SETINTERFACE:
2442 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
2443 ret = proc_setintf(ps, p);
2446 case USBDEVFS_SETCONFIGURATION:
2447 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
2448 ret = proc_setconfig(ps, p);
2451 case USBDEVFS_SUBMITURB:
2452 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
2453 ret = proc_submiturb(ps, p);
2455 inode->i_mtime = current_time(inode);
2458 #ifdef CONFIG_COMPAT
2459 case USBDEVFS_CONTROL32:
2460 snoop(&dev->dev, "%s: CONTROL32\n", __func__);
2461 ret = proc_control_compat(ps, p);
2463 inode->i_mtime = current_time(inode);
2466 case USBDEVFS_BULK32:
2467 snoop(&dev->dev, "%s: BULK32\n", __func__);
2468 ret = proc_bulk_compat(ps, p);
2470 inode->i_mtime = current_time(inode);
2473 case USBDEVFS_DISCSIGNAL32:
2474 snoop(&dev->dev, "%s: DISCSIGNAL32\n", __func__);
2475 ret = proc_disconnectsignal_compat(ps, p);
2478 case USBDEVFS_SUBMITURB32:
2479 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
2480 ret = proc_submiturb_compat(ps, p);
2482 inode->i_mtime = current_time(inode);
2485 case USBDEVFS_IOCTL32:
2486 snoop(&dev->dev, "%s: IOCTL32\n", __func__);
2487 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
2491 case USBDEVFS_DISCARDURB:
2492 snoop(&dev->dev, "%s: DISCARDURB %pK\n", __func__, p);
2493 ret = proc_unlinkurb(ps, p);
2496 case USBDEVFS_DISCSIGNAL:
2497 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
2498 ret = proc_disconnectsignal(ps, p);
2501 case USBDEVFS_CLAIMINTERFACE:
2502 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
2503 ret = proc_claiminterface(ps, p);
2506 case USBDEVFS_RELEASEINTERFACE:
2507 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
2508 ret = proc_releaseinterface(ps, p);
2511 case USBDEVFS_IOCTL:
2512 snoop(&dev->dev, "%s: IOCTL\n", __func__);
2513 ret = proc_ioctl_default(ps, p);
2516 case USBDEVFS_CLAIM_PORT:
2517 snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__);
2518 ret = proc_claim_port(ps, p);
2521 case USBDEVFS_RELEASE_PORT:
2522 snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__);
2523 ret = proc_release_port(ps, p);
2525 case USBDEVFS_GET_CAPABILITIES:
2526 ret = proc_get_capabilities(ps, p);
2528 case USBDEVFS_DISCONNECT_CLAIM:
2529 ret = proc_disconnect_claim(ps, p);
2531 case USBDEVFS_ALLOC_STREAMS:
2532 ret = proc_alloc_streams(ps, p);
2534 case USBDEVFS_FREE_STREAMS:
2535 ret = proc_free_streams(ps, p);
2537 case USBDEVFS_DROP_PRIVILEGES:
2538 ret = proc_drop_privileges(ps, p);
2540 case USBDEVFS_GET_SPEED:
2541 ret = ps->dev->speed;
2546 usb_unlock_device(dev);
2548 inode->i_atime = current_time(inode);
2552 static long usbdev_ioctl(struct file *file, unsigned int cmd,
2557 ret = usbdev_do_ioctl(file, cmd, (void __user *)arg);
2562 #ifdef CONFIG_COMPAT
2563 static long usbdev_compat_ioctl(struct file *file, unsigned int cmd,
2568 ret = usbdev_do_ioctl(file, cmd, compat_ptr(arg));
2574 /* No kernel lock - fine */
2575 static unsigned int usbdev_poll(struct file *file,
2576 struct poll_table_struct *wait)
2578 struct usb_dev_state *ps = file->private_data;
2579 unsigned int mask = 0;
2581 poll_wait(file, &ps->wait, wait);
2582 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
2583 mask |= POLLOUT | POLLWRNORM;
2586 if (list_empty(&ps->list))
2591 const struct file_operations usbdev_file_operations = {
2592 .owner = THIS_MODULE,
2593 .llseek = no_seek_end_llseek,
2594 .read = usbdev_read,
2595 .poll = usbdev_poll,
2596 .unlocked_ioctl = usbdev_ioctl,
2597 #ifdef CONFIG_COMPAT
2598 .compat_ioctl = usbdev_compat_ioctl,
2600 .mmap = usbdev_mmap,
2601 .open = usbdev_open,
2602 .release = usbdev_release,
2605 static void usbdev_remove(struct usb_device *udev)
2607 struct usb_dev_state *ps;
2608 struct siginfo sinfo;
2610 while (!list_empty(&udev->filelist)) {
2611 ps = list_entry(udev->filelist.next, struct usb_dev_state, list);
2612 destroy_all_async(ps);
2613 wake_up_all(&ps->wait);
2614 list_del_init(&ps->list);
2615 if (ps->discsignr) {
2616 memset(&sinfo, 0, sizeof(sinfo));
2617 sinfo.si_signo = ps->discsignr;
2618 sinfo.si_errno = EPIPE;
2619 sinfo.si_code = SI_ASYNCIO;
2620 sinfo.si_addr = ps->disccontext;
2621 kill_pid_info_as_cred(ps->discsignr, &sinfo,
2622 ps->disc_pid, ps->cred, ps->secid);
2627 static int usbdev_notify(struct notifier_block *self,
2628 unsigned long action, void *dev)
2631 case USB_DEVICE_ADD:
2633 case USB_DEVICE_REMOVE:
2640 static struct notifier_block usbdev_nb = {
2641 .notifier_call = usbdev_notify,
2644 static struct cdev usb_device_cdev;
2646 int __init usb_devio_init(void)
2650 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
2653 printk(KERN_ERR "Unable to register minors for usb_device\n");
2656 cdev_init(&usb_device_cdev, &usbdev_file_operations);
2657 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
2659 printk(KERN_ERR "Unable to get usb_device major %d\n",
2663 usb_register_notify(&usbdev_nb);
2668 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
2672 void usb_devio_cleanup(void)
2674 usb_unregister_notify(&usbdev_nb);
2675 cdev_del(&usb_device_cdev);
2676 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);