1 /*****************************************************************************/
4 * devio.c -- User space communication with USB devices.
6 * Copyright (C) 1999-2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * This file implements the usbfs/x/y files, where
23 * x is the bus number and y the device number.
25 * It allows user space programs/"drivers" to communicate directly
26 * with USB devices without intervening kernel driver.
29 * 22.12.1999 0.1 Initial release (split from proc_usb.c)
30 * 04.01.2000 0.2 Turned into its own filesystem
31 * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery
35 /*****************************************************************************/
39 #include <linux/sched/signal.h>
40 #include <linux/slab.h>
41 #include <linux/signal.h>
42 #include <linux/poll.h>
43 #include <linux/module.h>
44 #include <linux/string.h>
45 #include <linux/usb.h>
46 #include <linux/usbdevice_fs.h>
47 #include <linux/usb/hcd.h> /* for usbcore internals */
48 #include <linux/cdev.h>
49 #include <linux/notifier.h>
50 #include <linux/security.h>
51 #include <linux/user_namespace.h>
52 #include <linux/scatterlist.h>
53 #include <linux/uaccess.h>
54 #include <linux/dma-mapping.h>
55 #include <asm/byteorder.h>
56 #include <linux/moduleparam.h>
61 #define USB_DEVICE_MAX (USB_MAXBUS * 128)
62 #define USB_SG_SIZE 16384 /* split-size for large txs */
64 /* Mutual exclusion for removal, open, and release */
65 DEFINE_MUTEX(usbfs_mutex);
67 struct usb_dev_state {
68 struct list_head list; /* state list */
69 struct usb_device *dev;
71 spinlock_t lock; /* protects the async urb lists */
72 struct list_head async_pending;
73 struct list_head async_completed;
74 struct list_head memory_list;
75 wait_queue_head_t wait; /* wake up if a request completed */
76 unsigned int discsignr;
78 const struct cred *cred;
79 void __user *disccontext;
80 unsigned long ifclaimed;
82 u32 disabled_bulk_eps;
83 bool privileges_dropped;
84 unsigned long interface_allowed_mask;
88 struct list_head memlist;
93 dma_addr_t dma_handle;
94 unsigned long vm_start;
95 struct usb_dev_state *ps;
99 struct list_head asynclist;
100 struct usb_dev_state *ps;
102 const struct cred *cred;
105 void __user *userbuffer;
106 void __user *userurb;
108 struct usb_memory *usbm;
109 unsigned int mem_usage;
116 static bool usbfs_snoop;
117 module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
118 MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
120 static unsigned usbfs_snoop_max = 65536;
121 module_param(usbfs_snoop_max, uint, S_IRUGO | S_IWUSR);
122 MODULE_PARM_DESC(usbfs_snoop_max,
123 "maximum number of bytes to print while snooping");
125 #define snoop(dev, format, arg...) \
128 dev_info(dev, format, ## arg); \
135 #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
137 /* Limit on the total amount of memory we can allocate for transfers */
138 static u32 usbfs_memory_mb = 16;
139 module_param(usbfs_memory_mb, uint, 0644);
140 MODULE_PARM_DESC(usbfs_memory_mb,
141 "maximum MB allowed for usbfs buffers (0 = no limit)");
143 /* Hard limit, necessary to avoid arithmetic overflow */
144 #define USBFS_XFER_MAX (UINT_MAX / 2 - 1000000)
146 static atomic64_t usbfs_memory_usage; /* Total memory currently allocated */
148 /* Check whether it's okay to allocate more memory for a transfer */
149 static int usbfs_increase_memory_usage(u64 amount)
153 lim = ACCESS_ONCE(usbfs_memory_mb);
156 atomic64_add(amount, &usbfs_memory_usage);
158 if (lim > 0 && atomic64_read(&usbfs_memory_usage) > lim) {
159 atomic64_sub(amount, &usbfs_memory_usage);
166 /* Memory for a transfer is being deallocated */
167 static void usbfs_decrease_memory_usage(u64 amount)
169 atomic64_sub(amount, &usbfs_memory_usage);
172 static int connected(struct usb_dev_state *ps)
174 return (!list_empty(&ps->list) &&
175 ps->dev->state != USB_STATE_NOTATTACHED);
178 static void dec_usb_memory_use_count(struct usb_memory *usbm, int *count)
180 struct usb_dev_state *ps = usbm->ps;
183 spin_lock_irqsave(&ps->lock, flags);
185 if (usbm->urb_use_count == 0 && usbm->vma_use_count == 0) {
186 list_del(&usbm->memlist);
187 spin_unlock_irqrestore(&ps->lock, flags);
189 usb_free_coherent(ps->dev, usbm->size, usbm->mem,
191 usbfs_decrease_memory_usage(
192 usbm->size + sizeof(struct usb_memory));
195 spin_unlock_irqrestore(&ps->lock, flags);
199 static void usbdev_vm_open(struct vm_area_struct *vma)
201 struct usb_memory *usbm = vma->vm_private_data;
204 spin_lock_irqsave(&usbm->ps->lock, flags);
205 ++usbm->vma_use_count;
206 spin_unlock_irqrestore(&usbm->ps->lock, flags);
209 static void usbdev_vm_close(struct vm_area_struct *vma)
211 struct usb_memory *usbm = vma->vm_private_data;
213 dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
216 static const struct vm_operations_struct usbdev_vm_ops = {
217 .open = usbdev_vm_open,
218 .close = usbdev_vm_close
221 static int usbdev_mmap(struct file *file, struct vm_area_struct *vma)
223 struct usb_memory *usbm = NULL;
224 struct usb_dev_state *ps = file->private_data;
225 size_t size = vma->vm_end - vma->vm_start;
228 dma_addr_t dma_handle;
231 ret = usbfs_increase_memory_usage(size + sizeof(struct usb_memory));
235 usbm = kzalloc(sizeof(struct usb_memory), GFP_KERNEL);
238 goto error_decrease_mem;
241 mem = usb_alloc_coherent(ps->dev, size, GFP_USER | __GFP_NOWARN,
245 goto error_free_usbm;
248 memset(mem, 0, size);
251 usbm->dma_handle = dma_handle;
254 usbm->vm_start = vma->vm_start;
255 usbm->vma_use_count = 1;
256 INIT_LIST_HEAD(&usbm->memlist);
258 if (remap_pfn_range(vma, vma->vm_start,
259 virt_to_phys(usbm->mem) >> PAGE_SHIFT,
260 size, vma->vm_page_prot) < 0) {
261 dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
265 vma->vm_flags |= VM_IO;
266 vma->vm_flags |= (VM_DONTEXPAND | VM_DONTDUMP);
267 vma->vm_ops = &usbdev_vm_ops;
268 vma->vm_private_data = usbm;
270 spin_lock_irqsave(&ps->lock, flags);
271 list_add_tail(&usbm->memlist, &ps->memory_list);
272 spin_unlock_irqrestore(&ps->lock, flags);
279 usbfs_decrease_memory_usage(size + sizeof(struct usb_memory));
284 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
287 struct usb_dev_state *ps = file->private_data;
288 struct usb_device *dev = ps->dev;
295 usb_lock_device(dev);
296 if (!connected(ps)) {
299 } else if (pos < 0) {
304 if (pos < sizeof(struct usb_device_descriptor)) {
305 /* 18 bytes - fits on the stack */
306 struct usb_device_descriptor temp_desc;
308 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
309 le16_to_cpus(&temp_desc.bcdUSB);
310 le16_to_cpus(&temp_desc.idVendor);
311 le16_to_cpus(&temp_desc.idProduct);
312 le16_to_cpus(&temp_desc.bcdDevice);
314 len = sizeof(struct usb_device_descriptor) - pos;
317 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
328 pos = sizeof(struct usb_device_descriptor);
329 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
330 struct usb_config_descriptor *config =
331 (struct usb_config_descriptor *)dev->rawdescriptors[i];
332 unsigned int length = le16_to_cpu(config->wTotalLength);
334 if (*ppos < pos + length) {
336 /* The descriptor may claim to be longer than it
337 * really is. Here is the actual allocated length. */
339 le16_to_cpu(dev->config[i].desc.wTotalLength);
341 len = length - (*ppos - pos);
345 /* Simply don't write (skip over) unallocated parts */
346 if (alloclen > (*ppos - pos)) {
347 alloclen -= (*ppos - pos);
348 if (copy_to_user(buf,
349 dev->rawdescriptors[i] + (*ppos - pos),
350 min(len, alloclen))) {
366 usb_unlock_device(dev);
371 * async list handling
374 static struct async *alloc_async(unsigned int numisoframes)
378 as = kzalloc(sizeof(struct async), GFP_KERNEL);
381 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
389 static void free_async(struct async *as)
396 for (i = 0; i < as->urb->num_sgs; i++) {
397 if (sg_page(&as->urb->sg[i]))
398 kfree(sg_virt(&as->urb->sg[i]));
402 if (as->usbm == NULL)
403 kfree(as->urb->transfer_buffer);
405 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count);
407 kfree(as->urb->setup_packet);
408 usb_free_urb(as->urb);
409 usbfs_decrease_memory_usage(as->mem_usage);
413 static void async_newpending(struct async *as)
415 struct usb_dev_state *ps = as->ps;
418 spin_lock_irqsave(&ps->lock, flags);
419 list_add_tail(&as->asynclist, &ps->async_pending);
420 spin_unlock_irqrestore(&ps->lock, flags);
423 static void async_removepending(struct async *as)
425 struct usb_dev_state *ps = as->ps;
428 spin_lock_irqsave(&ps->lock, flags);
429 list_del_init(&as->asynclist);
430 spin_unlock_irqrestore(&ps->lock, flags);
433 static struct async *async_getcompleted(struct usb_dev_state *ps)
436 struct async *as = NULL;
438 spin_lock_irqsave(&ps->lock, flags);
439 if (!list_empty(&ps->async_completed)) {
440 as = list_entry(ps->async_completed.next, struct async,
442 list_del_init(&as->asynclist);
444 spin_unlock_irqrestore(&ps->lock, flags);
448 static struct async *async_getpending(struct usb_dev_state *ps,
449 void __user *userurb)
453 list_for_each_entry(as, &ps->async_pending, asynclist)
454 if (as->userurb == userurb) {
455 list_del_init(&as->asynclist);
462 static void snoop_urb(struct usb_device *udev,
463 void __user *userurb, int pipe, unsigned length,
464 int timeout_or_status, enum snoop_when when,
465 unsigned char *data, unsigned data_len)
467 static const char *types[] = {"isoc", "int", "ctrl", "bulk"};
468 static const char *dirs[] = {"out", "in"};
475 ep = usb_pipeendpoint(pipe);
476 t = types[usb_pipetype(pipe)];
477 d = dirs[!!usb_pipein(pipe)];
479 if (userurb) { /* Async */
481 dev_info(&udev->dev, "userurb %px, ep%d %s-%s, "
483 userurb, ep, t, d, length);
485 dev_info(&udev->dev, "userurb %px, ep%d %s-%s, "
486 "actual_length %u status %d\n",
487 userurb, ep, t, d, length,
491 dev_info(&udev->dev, "ep%d %s-%s, length %u, "
493 ep, t, d, length, timeout_or_status);
495 dev_info(&udev->dev, "ep%d %s-%s, actual_length %u, "
497 ep, t, d, length, timeout_or_status);
500 data_len = min(data_len, usbfs_snoop_max);
501 if (data && data_len > 0) {
502 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
507 static void snoop_urb_data(struct urb *urb, unsigned len)
511 len = min(len, usbfs_snoop_max);
512 if (!usbfs_snoop || len == 0)
515 if (urb->num_sgs == 0) {
516 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
517 urb->transfer_buffer, len, 1);
521 for (i = 0; i < urb->num_sgs && len; i++) {
522 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
523 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
524 sg_virt(&urb->sg[i]), size, 1);
529 static int copy_urb_data_to_user(u8 __user *userbuffer, struct urb *urb)
531 unsigned i, len, size;
533 if (urb->number_of_packets > 0) /* Isochronous */
534 len = urb->transfer_buffer_length;
536 len = urb->actual_length;
538 if (urb->num_sgs == 0) {
539 if (copy_to_user(userbuffer, urb->transfer_buffer, len))
544 for (i = 0; i < urb->num_sgs && len; i++) {
545 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
546 if (copy_to_user(userbuffer, sg_virt(&urb->sg[i]), size))
555 #define AS_CONTINUATION 1
558 static void cancel_bulk_urbs(struct usb_dev_state *ps, unsigned bulk_addr)
565 /* Mark all the pending URBs that match bulk_addr, up to but not
566 * including the first one without AS_CONTINUATION. If such an
567 * URB is encountered then a new transfer has already started so
568 * the endpoint doesn't need to be disabled; otherwise it does.
570 list_for_each_entry(as, &ps->async_pending, asynclist) {
571 if (as->bulk_addr == bulk_addr) {
572 if (as->bulk_status != AS_CONTINUATION)
574 as->bulk_status = AS_UNLINK;
578 ps->disabled_bulk_eps |= (1 << bulk_addr);
580 /* Now carefully unlink all the marked pending URBs */
582 list_for_each_entry(as, &ps->async_pending, asynclist) {
583 if (as->bulk_status == AS_UNLINK) {
584 as->bulk_status = 0; /* Only once */
587 spin_unlock(&ps->lock); /* Allow completions */
590 spin_lock(&ps->lock);
596 static void async_completed(struct urb *urb)
598 struct async *as = urb->context;
599 struct usb_dev_state *ps = as->ps;
600 struct siginfo sinfo;
601 struct pid *pid = NULL;
603 const struct cred *cred = NULL;
606 spin_lock(&ps->lock);
607 list_move_tail(&as->asynclist, &ps->async_completed);
608 as->status = urb->status;
611 memset(&sinfo, 0, sizeof(sinfo));
612 sinfo.si_signo = as->signr;
613 sinfo.si_errno = as->status;
614 sinfo.si_code = SI_ASYNCIO;
615 sinfo.si_addr = as->userurb;
616 pid = get_pid(as->pid);
617 cred = get_cred(as->cred);
620 snoop(&urb->dev->dev, "urb complete\n");
621 snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length,
622 as->status, COMPLETE, NULL, 0);
623 if ((urb->transfer_flags & URB_DIR_MASK) == URB_DIR_IN)
624 snoop_urb_data(urb, urb->actual_length);
626 if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET &&
627 as->status != -ENOENT)
628 cancel_bulk_urbs(ps, as->bulk_addr);
631 spin_unlock(&ps->lock);
634 kill_pid_info_as_cred(sinfo.si_signo, &sinfo, pid, cred, secid);
640 static void destroy_async(struct usb_dev_state *ps, struct list_head *list)
646 spin_lock_irqsave(&ps->lock, flags);
647 while (!list_empty(list)) {
648 as = list_entry(list->next, struct async, asynclist);
649 list_del_init(&as->asynclist);
653 /* drop the spinlock so the completion handler can run */
654 spin_unlock_irqrestore(&ps->lock, flags);
657 spin_lock_irqsave(&ps->lock, flags);
659 spin_unlock_irqrestore(&ps->lock, flags);
662 static void destroy_async_on_interface(struct usb_dev_state *ps,
665 struct list_head *p, *q, hitlist;
668 INIT_LIST_HEAD(&hitlist);
669 spin_lock_irqsave(&ps->lock, flags);
670 list_for_each_safe(p, q, &ps->async_pending)
671 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
672 list_move_tail(p, &hitlist);
673 spin_unlock_irqrestore(&ps->lock, flags);
674 destroy_async(ps, &hitlist);
677 static void destroy_all_async(struct usb_dev_state *ps)
679 destroy_async(ps, &ps->async_pending);
683 * interface claims are made only at the request of user level code,
684 * which can also release them (explicitly or by closing files).
685 * they're also undone when devices disconnect.
688 static int driver_probe(struct usb_interface *intf,
689 const struct usb_device_id *id)
694 static void driver_disconnect(struct usb_interface *intf)
696 struct usb_dev_state *ps = usb_get_intfdata(intf);
697 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
702 /* NOTE: this relies on usbcore having canceled and completed
703 * all pending I/O requests; 2.6 does that.
706 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
707 clear_bit(ifnum, &ps->ifclaimed);
709 dev_warn(&intf->dev, "interface number %u out of range\n",
712 usb_set_intfdata(intf, NULL);
714 /* force async requests to complete */
715 destroy_async_on_interface(ps, ifnum);
718 /* The following routines are merely placeholders. There is no way
719 * to inform a user task about suspend or resumes.
721 static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
726 static int driver_resume(struct usb_interface *intf)
731 struct usb_driver usbfs_driver = {
733 .probe = driver_probe,
734 .disconnect = driver_disconnect,
735 .suspend = driver_suspend,
736 .resume = driver_resume,
739 static int claimintf(struct usb_dev_state *ps, unsigned int ifnum)
741 struct usb_device *dev = ps->dev;
742 struct usb_interface *intf;
745 if (ifnum >= 8*sizeof(ps->ifclaimed))
747 /* already claimed */
748 if (test_bit(ifnum, &ps->ifclaimed))
751 if (ps->privileges_dropped &&
752 !test_bit(ifnum, &ps->interface_allowed_mask))
755 intf = usb_ifnum_to_if(dev, ifnum);
759 unsigned int old_suppress;
761 /* suppress uevents while claiming interface */
762 old_suppress = dev_get_uevent_suppress(&intf->dev);
763 dev_set_uevent_suppress(&intf->dev, 1);
764 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
765 dev_set_uevent_suppress(&intf->dev, old_suppress);
768 set_bit(ifnum, &ps->ifclaimed);
772 static int releaseintf(struct usb_dev_state *ps, unsigned int ifnum)
774 struct usb_device *dev;
775 struct usb_interface *intf;
779 if (ifnum >= 8*sizeof(ps->ifclaimed))
782 intf = usb_ifnum_to_if(dev, ifnum);
785 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
786 unsigned int old_suppress;
788 /* suppress uevents while releasing interface */
789 old_suppress = dev_get_uevent_suppress(&intf->dev);
790 dev_set_uevent_suppress(&intf->dev, 1);
791 usb_driver_release_interface(&usbfs_driver, intf);
792 dev_set_uevent_suppress(&intf->dev, old_suppress);
798 static int checkintf(struct usb_dev_state *ps, unsigned int ifnum)
800 if (ps->dev->state != USB_STATE_CONFIGURED)
801 return -EHOSTUNREACH;
802 if (ifnum >= 8*sizeof(ps->ifclaimed))
804 if (test_bit(ifnum, &ps->ifclaimed))
806 /* if not yet claimed, claim it for the driver */
807 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
808 "interface %u before use\n", task_pid_nr(current),
809 current->comm, ifnum);
810 return claimintf(ps, ifnum);
813 static int findintfep(struct usb_device *dev, unsigned int ep)
815 unsigned int i, j, e;
816 struct usb_interface *intf;
817 struct usb_host_interface *alts;
818 struct usb_endpoint_descriptor *endpt;
820 if (ep & ~(USB_DIR_IN|0xf))
824 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
825 intf = dev->actconfig->interface[i];
826 for (j = 0; j < intf->num_altsetting; j++) {
827 alts = &intf->altsetting[j];
828 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
829 endpt = &alts->endpoint[e].desc;
830 if (endpt->bEndpointAddress == ep)
831 return alts->desc.bInterfaceNumber;
838 static int check_ctrlrecip(struct usb_dev_state *ps, unsigned int requesttype,
839 unsigned int request, unsigned int index)
842 struct usb_host_interface *alt_setting;
844 if (ps->dev->state != USB_STATE_UNAUTHENTICATED
845 && ps->dev->state != USB_STATE_ADDRESS
846 && ps->dev->state != USB_STATE_CONFIGURED)
847 return -EHOSTUNREACH;
848 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
852 * check for the special corner case 'get_device_id' in the printer
853 * class specification, which we always want to allow as it is used
854 * to query things like ink level, etc.
856 if (requesttype == 0xa1 && request == 0) {
857 alt_setting = usb_find_alt_setting(ps->dev->actconfig,
858 index >> 8, index & 0xff);
860 && alt_setting->desc.bInterfaceClass == USB_CLASS_PRINTER)
865 switch (requesttype & USB_RECIP_MASK) {
866 case USB_RECIP_ENDPOINT:
867 if ((index & ~USB_DIR_IN) == 0)
869 ret = findintfep(ps->dev, index);
872 * Some not fully compliant Win apps seem to get
873 * index wrong and have the endpoint number here
874 * rather than the endpoint address (with the
875 * correct direction). Win does let this through,
876 * so we'll not reject it here but leave it to
877 * the device to not break KVM. But we warn.
879 ret = findintfep(ps->dev, index ^ 0x80);
881 dev_info(&ps->dev->dev,
882 "%s: process %i (%s) requesting ep %02x but needs %02x\n",
883 __func__, task_pid_nr(current),
884 current->comm, index, index ^ 0x80);
887 ret = checkintf(ps, ret);
890 case USB_RECIP_INTERFACE:
891 ret = checkintf(ps, index);
897 static struct usb_host_endpoint *ep_to_host_endpoint(struct usb_device *dev,
900 if (ep & USB_ENDPOINT_DIR_MASK)
901 return dev->ep_in[ep & USB_ENDPOINT_NUMBER_MASK];
903 return dev->ep_out[ep & USB_ENDPOINT_NUMBER_MASK];
906 static int parse_usbdevfs_streams(struct usb_dev_state *ps,
907 struct usbdevfs_streams __user *streams,
908 unsigned int *num_streams_ret,
909 unsigned int *num_eps_ret,
910 struct usb_host_endpoint ***eps_ret,
911 struct usb_interface **intf_ret)
913 unsigned int i, num_streams, num_eps;
914 struct usb_host_endpoint **eps;
915 struct usb_interface *intf = NULL;
919 if (get_user(num_streams, &streams->num_streams) ||
920 get_user(num_eps, &streams->num_eps))
923 if (num_eps < 1 || num_eps > USB_MAXENDPOINTS)
926 /* The XHCI controller allows max 2 ^ 16 streams */
927 if (num_streams_ret && (num_streams < 2 || num_streams > 65536))
930 eps = kmalloc(num_eps * sizeof(*eps), GFP_KERNEL);
934 for (i = 0; i < num_eps; i++) {
935 if (get_user(ep, &streams->eps[i])) {
939 eps[i] = ep_to_host_endpoint(ps->dev, ep);
945 /* usb_alloc/free_streams operate on an usb_interface */
946 ifnum = findintfep(ps->dev, ep);
953 ret = checkintf(ps, ifnum);
956 intf = usb_ifnum_to_if(ps->dev, ifnum);
958 /* Verify all eps belong to the same interface */
959 if (ifnum != intf->altsetting->desc.bInterfaceNumber) {
967 *num_streams_ret = num_streams;
968 *num_eps_ret = num_eps;
979 static int match_devt(struct device *dev, void *data)
981 return dev->devt == (dev_t) (unsigned long) data;
984 static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
988 dev = bus_find_device(&usb_bus_type, NULL,
989 (void *) (unsigned long) devt, match_devt);
992 return to_usb_device(dev);
998 static int usbdev_open(struct inode *inode, struct file *file)
1000 struct usb_device *dev = NULL;
1001 struct usb_dev_state *ps;
1005 ps = kzalloc(sizeof(struct usb_dev_state), GFP_KERNEL);
1011 /* Protect against simultaneous removal or release */
1012 mutex_lock(&usbfs_mutex);
1014 /* usbdev device-node */
1015 if (imajor(inode) == USB_DEVICE_MAJOR)
1016 dev = usbdev_lookup_by_devt(inode->i_rdev);
1018 mutex_unlock(&usbfs_mutex);
1023 usb_lock_device(dev);
1024 if (dev->state == USB_STATE_NOTATTACHED)
1025 goto out_unlock_device;
1027 ret = usb_autoresume_device(dev);
1029 goto out_unlock_device;
1033 ps->interface_allowed_mask = 0xFFFFFFFF; /* 32 bits */
1034 spin_lock_init(&ps->lock);
1035 INIT_LIST_HEAD(&ps->list);
1036 INIT_LIST_HEAD(&ps->async_pending);
1037 INIT_LIST_HEAD(&ps->async_completed);
1038 INIT_LIST_HEAD(&ps->memory_list);
1039 init_waitqueue_head(&ps->wait);
1040 ps->disc_pid = get_pid(task_pid(current));
1041 ps->cred = get_current_cred();
1042 security_task_getsecid(current, &ps->secid);
1044 list_add_tail(&ps->list, &dev->filelist);
1045 file->private_data = ps;
1046 usb_unlock_device(dev);
1047 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
1052 usb_unlock_device(dev);
1059 static int usbdev_release(struct inode *inode, struct file *file)
1061 struct usb_dev_state *ps = file->private_data;
1062 struct usb_device *dev = ps->dev;
1066 usb_lock_device(dev);
1067 usb_hub_release_all_ports(dev, ps);
1069 list_del_init(&ps->list);
1071 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
1073 if (test_bit(ifnum, &ps->ifclaimed))
1074 releaseintf(ps, ifnum);
1076 destroy_all_async(ps);
1077 usb_autosuspend_device(dev);
1078 usb_unlock_device(dev);
1080 put_pid(ps->disc_pid);
1083 as = async_getcompleted(ps);
1086 as = async_getcompleted(ps);
1093 static int proc_control(struct usb_dev_state *ps, void __user *arg)
1095 struct usb_device *dev = ps->dev;
1096 struct usbdevfs_ctrltransfer ctrl;
1098 unsigned char *tbuf;
1102 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1104 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.bRequest,
1108 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
1109 if (wLength > PAGE_SIZE)
1111 ret = usbfs_increase_memory_usage(PAGE_SIZE + sizeof(struct urb) +
1112 sizeof(struct usb_ctrlrequest));
1115 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
1121 snoop(&dev->dev, "control urb: bRequestType=%02x "
1122 "bRequest=%02x wValue=%04x "
1123 "wIndex=%04x wLength=%04x\n",
1124 ctrl.bRequestType, ctrl.bRequest, ctrl.wValue,
1125 ctrl.wIndex, ctrl.wLength);
1126 if (ctrl.bRequestType & 0x80) {
1127 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
1132 pipe = usb_rcvctrlpipe(dev, 0);
1133 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT, NULL, 0);
1135 usb_unlock_device(dev);
1136 i = usb_control_msg(dev, pipe, ctrl.bRequest,
1137 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
1138 tbuf, ctrl.wLength, tmo);
1139 usb_lock_device(dev);
1140 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE,
1142 if ((i > 0) && ctrl.wLength) {
1143 if (copy_to_user(ctrl.data, tbuf, i)) {
1150 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
1155 pipe = usb_sndctrlpipe(dev, 0);
1156 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT,
1157 tbuf, ctrl.wLength);
1159 usb_unlock_device(dev);
1160 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
1161 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
1162 tbuf, ctrl.wLength, tmo);
1163 usb_lock_device(dev);
1164 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE, NULL, 0);
1166 if (i < 0 && i != -EPIPE) {
1167 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
1168 "failed cmd %s rqt %u rq %u len %u ret %d\n",
1169 current->comm, ctrl.bRequestType, ctrl.bRequest,
1174 free_page((unsigned long) tbuf);
1175 usbfs_decrease_memory_usage(PAGE_SIZE + sizeof(struct urb) +
1176 sizeof(struct usb_ctrlrequest));
1180 static int proc_bulk(struct usb_dev_state *ps, void __user *arg)
1182 struct usb_device *dev = ps->dev;
1183 struct usbdevfs_bulktransfer bulk;
1184 unsigned int tmo, len1, pipe;
1186 unsigned char *tbuf;
1189 if (copy_from_user(&bulk, arg, sizeof(bulk)))
1191 ret = findintfep(ps->dev, bulk.ep);
1194 ret = checkintf(ps, ret);
1197 if (bulk.ep & USB_DIR_IN)
1198 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
1200 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
1201 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
1204 if (len1 >= (INT_MAX - sizeof(struct urb)))
1206 ret = usbfs_increase_memory_usage(len1 + sizeof(struct urb));
1209 tbuf = kmalloc(len1, GFP_KERNEL);
1215 if (bulk.ep & 0x80) {
1216 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
1220 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, NULL, 0);
1222 usb_unlock_device(dev);
1223 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
1224 usb_lock_device(dev);
1225 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, tbuf, len2);
1228 if (copy_to_user(bulk.data, tbuf, len2)) {
1235 if (copy_from_user(tbuf, bulk.data, len1)) {
1240 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, tbuf, len1);
1242 usb_unlock_device(dev);
1243 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
1244 usb_lock_device(dev);
1245 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, NULL, 0);
1247 ret = (i < 0 ? i : len2);
1250 usbfs_decrease_memory_usage(len1 + sizeof(struct urb));
1254 static void check_reset_of_active_ep(struct usb_device *udev,
1255 unsigned int epnum, char *ioctl_name)
1257 struct usb_host_endpoint **eps;
1258 struct usb_host_endpoint *ep;
1260 eps = (epnum & USB_DIR_IN) ? udev->ep_in : udev->ep_out;
1261 ep = eps[epnum & 0x0f];
1262 if (ep && !list_empty(&ep->urb_list))
1263 dev_warn(&udev->dev, "Process %d (%s) called USBDEVFS_%s for active endpoint 0x%02x\n",
1264 task_pid_nr(current), current->comm,
1268 static int proc_resetep(struct usb_dev_state *ps, void __user *arg)
1273 if (get_user(ep, (unsigned int __user *)arg))
1275 ret = findintfep(ps->dev, ep);
1278 ret = checkintf(ps, ret);
1281 check_reset_of_active_ep(ps->dev, ep, "RESETEP");
1282 usb_reset_endpoint(ps->dev, ep);
1286 static int proc_clearhalt(struct usb_dev_state *ps, void __user *arg)
1292 if (get_user(ep, (unsigned int __user *)arg))
1294 ret = findintfep(ps->dev, ep);
1297 ret = checkintf(ps, ret);
1300 check_reset_of_active_ep(ps->dev, ep, "CLEAR_HALT");
1301 if (ep & USB_DIR_IN)
1302 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
1304 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
1306 return usb_clear_halt(ps->dev, pipe);
1309 static int proc_getdriver(struct usb_dev_state *ps, void __user *arg)
1311 struct usbdevfs_getdriver gd;
1312 struct usb_interface *intf;
1315 if (copy_from_user(&gd, arg, sizeof(gd)))
1317 intf = usb_ifnum_to_if(ps->dev, gd.interface);
1318 if (!intf || !intf->dev.driver)
1321 strlcpy(gd.driver, intf->dev.driver->name,
1323 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
1328 static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg)
1330 struct usbdevfs_connectinfo ci;
1332 memset(&ci, 0, sizeof(ci));
1333 ci.devnum = ps->dev->devnum;
1334 ci.slow = ps->dev->speed == USB_SPEED_LOW;
1336 if (copy_to_user(arg, &ci, sizeof(ci)))
1341 static int proc_resetdevice(struct usb_dev_state *ps)
1343 struct usb_host_config *actconfig = ps->dev->actconfig;
1344 struct usb_interface *interface;
1347 /* Don't allow a device reset if the process has dropped the
1348 * privilege to do such things and any of the interfaces are
1349 * currently claimed.
1351 if (ps->privileges_dropped && actconfig) {
1352 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
1353 interface = actconfig->interface[i];
1354 number = interface->cur_altsetting->desc.bInterfaceNumber;
1355 if (usb_interface_claimed(interface) &&
1356 !test_bit(number, &ps->ifclaimed)) {
1357 dev_warn(&ps->dev->dev,
1358 "usbfs: interface %d claimed by %s while '%s' resets device\n",
1359 number, interface->dev.driver->name, current->comm);
1365 return usb_reset_device(ps->dev);
1368 static int proc_setintf(struct usb_dev_state *ps, void __user *arg)
1370 struct usbdevfs_setinterface setintf;
1373 if (copy_from_user(&setintf, arg, sizeof(setintf)))
1375 ret = checkintf(ps, setintf.interface);
1379 destroy_async_on_interface(ps, setintf.interface);
1381 return usb_set_interface(ps->dev, setintf.interface,
1382 setintf.altsetting);
1385 static int proc_setconfig(struct usb_dev_state *ps, void __user *arg)
1389 struct usb_host_config *actconfig;
1391 if (get_user(u, (int __user *)arg))
1394 actconfig = ps->dev->actconfig;
1396 /* Don't touch the device if any interfaces are claimed.
1397 * It could interfere with other drivers' operations, and if
1398 * an interface is claimed by usbfs it could easily deadlock.
1403 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
1404 if (usb_interface_claimed(actconfig->interface[i])) {
1405 dev_warn(&ps->dev->dev,
1406 "usbfs: interface %d claimed by %s "
1407 "while '%s' sets config #%d\n",
1408 actconfig->interface[i]
1410 ->desc.bInterfaceNumber,
1411 actconfig->interface[i]
1420 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
1421 * so avoid usb_set_configuration()'s kick to sysfs
1424 if (actconfig && actconfig->desc.bConfigurationValue == u)
1425 status = usb_reset_configuration(ps->dev);
1427 status = usb_set_configuration(ps->dev, u);
1433 static struct usb_memory *
1434 find_memory_area(struct usb_dev_state *ps, const struct usbdevfs_urb *uurb)
1436 struct usb_memory *usbm = NULL, *iter;
1437 unsigned long flags;
1438 unsigned long uurb_start = (unsigned long)uurb->buffer;
1440 spin_lock_irqsave(&ps->lock, flags);
1441 list_for_each_entry(iter, &ps->memory_list, memlist) {
1442 if (uurb_start >= iter->vm_start &&
1443 uurb_start < iter->vm_start + iter->size) {
1444 if (uurb->buffer_length > iter->vm_start + iter->size -
1446 usbm = ERR_PTR(-EINVAL);
1449 usbm->urb_use_count++;
1454 spin_unlock_irqrestore(&ps->lock, flags);
1458 static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb,
1459 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
1462 struct usbdevfs_iso_packet_desc *isopkt = NULL;
1463 struct usb_host_endpoint *ep;
1464 struct async *as = NULL;
1465 struct usb_ctrlrequest *dr = NULL;
1466 unsigned int u, totlen, isofrmlen;
1467 int i, ret, num_sgs = 0, ifnum = -1;
1468 int number_of_packets = 0;
1469 unsigned int stream_id = 0;
1472 bool allow_short = false;
1473 bool allow_zero = false;
1474 unsigned long mask = USBDEVFS_URB_SHORT_NOT_OK |
1475 USBDEVFS_URB_BULK_CONTINUATION |
1476 USBDEVFS_URB_NO_FSBR |
1477 USBDEVFS_URB_ZERO_PACKET |
1478 USBDEVFS_URB_NO_INTERRUPT;
1479 /* USBDEVFS_URB_ISO_ASAP is a special case */
1480 if (uurb->type == USBDEVFS_URB_TYPE_ISO)
1481 mask |= USBDEVFS_URB_ISO_ASAP;
1483 if (uurb->flags & ~mask)
1486 if ((unsigned int)uurb->buffer_length >= USBFS_XFER_MAX)
1488 if (uurb->buffer_length > 0 && !uurb->buffer)
1490 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
1491 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
1492 ifnum = findintfep(ps->dev, uurb->endpoint);
1495 ret = checkintf(ps, ifnum);
1499 ep = ep_to_host_endpoint(ps->dev, uurb->endpoint);
1502 is_in = (uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0;
1505 switch (uurb->type) {
1506 case USBDEVFS_URB_TYPE_CONTROL:
1507 if (!usb_endpoint_xfer_control(&ep->desc))
1509 /* min 8 byte setup packet */
1510 if (uurb->buffer_length < 8)
1512 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1515 if (copy_from_user(dr, uurb->buffer, 8)) {
1519 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
1523 ret = check_ctrlrecip(ps, dr->bRequestType, dr->bRequest,
1524 le16_to_cpup(&dr->wIndex));
1527 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1529 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1531 uurb->endpoint |= USB_DIR_IN;
1534 uurb->endpoint &= ~USB_DIR_IN;
1538 snoop(&ps->dev->dev, "control urb: bRequestType=%02x "
1539 "bRequest=%02x wValue=%04x "
1540 "wIndex=%04x wLength=%04x\n",
1541 dr->bRequestType, dr->bRequest,
1542 __le16_to_cpup(&dr->wValue),
1543 __le16_to_cpup(&dr->wIndex),
1544 __le16_to_cpup(&dr->wLength));
1545 u = sizeof(struct usb_ctrlrequest);
1548 case USBDEVFS_URB_TYPE_BULK:
1553 switch (usb_endpoint_type(&ep->desc)) {
1554 case USB_ENDPOINT_XFER_CONTROL:
1555 case USB_ENDPOINT_XFER_ISOC:
1557 case USB_ENDPOINT_XFER_INT:
1558 /* allow single-shot interrupt transfers */
1559 uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
1562 num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE);
1563 if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize)
1566 stream_id = uurb->stream_id;
1569 case USBDEVFS_URB_TYPE_INTERRUPT:
1570 if (!usb_endpoint_xfer_int(&ep->desc))
1579 case USBDEVFS_URB_TYPE_ISO:
1580 /* arbitrary limit */
1581 if (uurb->number_of_packets < 1 ||
1582 uurb->number_of_packets > 128)
1584 if (!usb_endpoint_xfer_isoc(&ep->desc))
1586 number_of_packets = uurb->number_of_packets;
1587 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1589 isopkt = memdup_user(iso_frame_desc, isofrmlen);
1590 if (IS_ERR(isopkt)) {
1591 ret = PTR_ERR(isopkt);
1595 for (totlen = u = 0; u < number_of_packets; u++) {
1597 * arbitrary limit need for USB 3.0
1598 * bMaxBurst (0~15 allowed, 1~16 packets)
1599 * bmAttributes (bit 1:0, mult 0~2, 1~3 packets)
1600 * sizemax: 1024 * 16 * 3 = 49152
1602 if (isopkt[u].length > 49152) {
1606 totlen += isopkt[u].length;
1608 u *= sizeof(struct usb_iso_packet_descriptor);
1609 uurb->buffer_length = totlen;
1616 if (uurb->buffer_length > 0 &&
1617 !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1618 uurb->buffer, uurb->buffer_length)) {
1622 as = alloc_async(number_of_packets);
1628 as->usbm = find_memory_area(ps, uurb);
1629 if (IS_ERR(as->usbm)) {
1630 ret = PTR_ERR(as->usbm);
1635 /* do not use SG buffers when memory mapped segments
1641 u += sizeof(struct async) + sizeof(struct urb) + uurb->buffer_length +
1642 num_sgs * sizeof(struct scatterlist);
1643 ret = usbfs_increase_memory_usage(u);
1649 as->urb->sg = kmalloc(num_sgs * sizeof(struct scatterlist),
1655 as->urb->num_sgs = num_sgs;
1656 sg_init_table(as->urb->sg, as->urb->num_sgs);
1658 totlen = uurb->buffer_length;
1659 for (i = 0; i < as->urb->num_sgs; i++) {
1660 u = (totlen > USB_SG_SIZE) ? USB_SG_SIZE : totlen;
1661 buf = kmalloc(u, GFP_KERNEL);
1666 sg_set_buf(&as->urb->sg[i], buf, u);
1669 if (copy_from_user(buf, uurb->buffer, u)) {
1677 } else if (uurb->buffer_length > 0) {
1679 unsigned long uurb_start = (unsigned long)uurb->buffer;
1681 as->urb->transfer_buffer = as->usbm->mem +
1682 (uurb_start - as->usbm->vm_start);
1684 as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
1686 if (!as->urb->transfer_buffer) {
1691 if (copy_from_user(as->urb->transfer_buffer,
1693 uurb->buffer_length)) {
1697 } else if (uurb->type == USBDEVFS_URB_TYPE_ISO) {
1699 * Isochronous input data may end up being
1700 * discontiguous if some of the packets are
1701 * short. Clear the buffer so that the gaps
1702 * don't leak kernel data to userspace.
1704 memset(as->urb->transfer_buffer, 0,
1705 uurb->buffer_length);
1709 as->urb->dev = ps->dev;
1710 as->urb->pipe = (uurb->type << 30) |
1711 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1712 (uurb->endpoint & USB_DIR_IN);
1714 /* This tedious sequence is necessary because the URB_* flags
1715 * are internal to the kernel and subject to change, whereas
1716 * the USBDEVFS_URB_* flags are a user API and must not be changed.
1718 u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
1719 if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
1721 if (allow_short && uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
1722 u |= URB_SHORT_NOT_OK;
1723 if (uurb->flags & USBDEVFS_URB_NO_FSBR)
1725 if (allow_zero && uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1726 u |= URB_ZERO_PACKET;
1727 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
1728 u |= URB_NO_INTERRUPT;
1729 as->urb->transfer_flags = u;
1731 if (!allow_short && uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
1732 dev_warn(&ps->dev->dev, "Requested nonsensical USBDEVFS_URB_SHORT_NOT_OK.\n");
1733 if (!allow_zero && uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1734 dev_warn(&ps->dev->dev, "Requested nonsensical USBDEVFS_URB_ZERO_PACKET.\n");
1736 as->urb->transfer_buffer_length = uurb->buffer_length;
1737 as->urb->setup_packet = (unsigned char *)dr;
1739 as->urb->start_frame = uurb->start_frame;
1740 as->urb->number_of_packets = number_of_packets;
1741 as->urb->stream_id = stream_id;
1743 if (ep->desc.bInterval) {
1744 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1745 ps->dev->speed == USB_SPEED_HIGH ||
1746 ps->dev->speed >= USB_SPEED_SUPER)
1747 as->urb->interval = 1 <<
1748 min(15, ep->desc.bInterval - 1);
1750 as->urb->interval = ep->desc.bInterval;
1753 as->urb->context = as;
1754 as->urb->complete = async_completed;
1755 for (totlen = u = 0; u < number_of_packets; u++) {
1756 as->urb->iso_frame_desc[u].offset = totlen;
1757 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1758 totlen += isopkt[u].length;
1765 unsigned long uurb_start = (unsigned long)uurb->buffer;
1767 as->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1768 as->urb->transfer_dma = as->usbm->dma_handle +
1769 (uurb_start - as->usbm->vm_start);
1770 } else if (is_in && uurb->buffer_length > 0)
1771 as->userbuffer = uurb->buffer;
1772 as->signr = uurb->signr;
1774 as->pid = get_pid(task_pid(current));
1775 as->cred = get_current_cred();
1776 security_task_getsecid(current, &as->secid);
1777 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1778 as->urb->transfer_buffer_length, 0, SUBMIT,
1781 snoop_urb_data(as->urb, as->urb->transfer_buffer_length);
1783 async_newpending(as);
1785 if (usb_endpoint_xfer_bulk(&ep->desc)) {
1786 spin_lock_irq(&ps->lock);
1788 /* Not exactly the endpoint address; the direction bit is
1789 * shifted to the 0x10 position so that the value will be
1792 as->bulk_addr = usb_endpoint_num(&ep->desc) |
1793 ((ep->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1796 /* If this bulk URB is the start of a new transfer, re-enable
1797 * the endpoint. Otherwise mark it as a continuation URB.
1799 if (uurb->flags & USBDEVFS_URB_BULK_CONTINUATION)
1800 as->bulk_status = AS_CONTINUATION;
1802 ps->disabled_bulk_eps &= ~(1 << as->bulk_addr);
1804 /* Don't accept continuation URBs if the endpoint is
1805 * disabled because of an earlier error.
1807 if (ps->disabled_bulk_eps & (1 << as->bulk_addr))
1810 ret = usb_submit_urb(as->urb, GFP_ATOMIC);
1811 spin_unlock_irq(&ps->lock);
1813 ret = usb_submit_urb(as->urb, GFP_KERNEL);
1817 dev_printk(KERN_DEBUG, &ps->dev->dev,
1818 "usbfs: usb_submit_urb returned %d\n", ret);
1819 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1820 0, ret, COMPLETE, NULL, 0);
1821 async_removepending(as);
1834 static int proc_submiturb(struct usb_dev_state *ps, void __user *arg)
1836 struct usbdevfs_urb uurb;
1838 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1841 return proc_do_submiturb(ps, &uurb,
1842 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1846 static int proc_unlinkurb(struct usb_dev_state *ps, void __user *arg)
1850 unsigned long flags;
1852 spin_lock_irqsave(&ps->lock, flags);
1853 as = async_getpending(ps, arg);
1855 spin_unlock_irqrestore(&ps->lock, flags);
1861 spin_unlock_irqrestore(&ps->lock, flags);
1869 static void compute_isochronous_actual_length(struct urb *urb)
1873 if (urb->number_of_packets > 0) {
1874 urb->actual_length = 0;
1875 for (i = 0; i < urb->number_of_packets; i++)
1876 urb->actual_length +=
1877 urb->iso_frame_desc[i].actual_length;
1881 static int processcompl(struct async *as, void __user * __user *arg)
1883 struct urb *urb = as->urb;
1884 struct usbdevfs_urb __user *userurb = as->userurb;
1885 void __user *addr = as->userurb;
1888 compute_isochronous_actual_length(urb);
1889 if (as->userbuffer && urb->actual_length) {
1890 if (copy_urb_data_to_user(as->userbuffer, urb))
1893 if (put_user(as->status, &userurb->status))
1895 if (put_user(urb->actual_length, &userurb->actual_length))
1897 if (put_user(urb->error_count, &userurb->error_count))
1900 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1901 for (i = 0; i < urb->number_of_packets; i++) {
1902 if (put_user(urb->iso_frame_desc[i].actual_length,
1903 &userurb->iso_frame_desc[i].actual_length))
1905 if (put_user(urb->iso_frame_desc[i].status,
1906 &userurb->iso_frame_desc[i].status))
1911 if (put_user(addr, (void __user * __user *)arg))
1919 static struct async *reap_as(struct usb_dev_state *ps)
1921 DECLARE_WAITQUEUE(wait, current);
1922 struct async *as = NULL;
1923 struct usb_device *dev = ps->dev;
1925 add_wait_queue(&ps->wait, &wait);
1927 __set_current_state(TASK_INTERRUPTIBLE);
1928 as = async_getcompleted(ps);
1929 if (as || !connected(ps))
1931 if (signal_pending(current))
1933 usb_unlock_device(dev);
1935 usb_lock_device(dev);
1937 remove_wait_queue(&ps->wait, &wait);
1938 set_current_state(TASK_RUNNING);
1942 static int proc_reapurb(struct usb_dev_state *ps, void __user *arg)
1944 struct async *as = reap_as(ps);
1949 snoop(&ps->dev->dev, "reap %px\n", as->userurb);
1950 retval = processcompl(as, (void __user * __user *)arg);
1954 if (signal_pending(current))
1959 static int proc_reapurbnonblock(struct usb_dev_state *ps, void __user *arg)
1964 as = async_getcompleted(ps);
1966 snoop(&ps->dev->dev, "reap %px\n", as->userurb);
1967 retval = processcompl(as, (void __user * __user *)arg);
1970 retval = (connected(ps) ? -EAGAIN : -ENODEV);
1975 #ifdef CONFIG_COMPAT
1976 static int proc_control_compat(struct usb_dev_state *ps,
1977 struct usbdevfs_ctrltransfer32 __user *p32)
1979 struct usbdevfs_ctrltransfer __user *p;
1981 p = compat_alloc_user_space(sizeof(*p));
1982 if (copy_in_user(p, p32, (sizeof(*p32) - sizeof(compat_caddr_t))) ||
1983 get_user(udata, &p32->data) ||
1984 put_user(compat_ptr(udata), &p->data))
1986 return proc_control(ps, p);
1989 static int proc_bulk_compat(struct usb_dev_state *ps,
1990 struct usbdevfs_bulktransfer32 __user *p32)
1992 struct usbdevfs_bulktransfer __user *p;
1994 compat_caddr_t addr;
1996 p = compat_alloc_user_space(sizeof(*p));
1998 if (get_user(n, &p32->ep) || put_user(n, &p->ep) ||
1999 get_user(n, &p32->len) || put_user(n, &p->len) ||
2000 get_user(n, &p32->timeout) || put_user(n, &p->timeout) ||
2001 get_user(addr, &p32->data) || put_user(compat_ptr(addr), &p->data))
2004 return proc_bulk(ps, p);
2006 static int proc_disconnectsignal_compat(struct usb_dev_state *ps, void __user *arg)
2008 struct usbdevfs_disconnectsignal32 ds;
2010 if (copy_from_user(&ds, arg, sizeof(ds)))
2012 ps->discsignr = ds.signr;
2013 ps->disccontext = compat_ptr(ds.context);
2017 static int get_urb32(struct usbdevfs_urb *kurb,
2018 struct usbdevfs_urb32 __user *uurb)
2020 struct usbdevfs_urb32 urb32;
2021 if (copy_from_user(&urb32, uurb, sizeof(*uurb)))
2023 kurb->type = urb32.type;
2024 kurb->endpoint = urb32.endpoint;
2025 kurb->status = urb32.status;
2026 kurb->flags = urb32.flags;
2027 kurb->buffer = compat_ptr(urb32.buffer);
2028 kurb->buffer_length = urb32.buffer_length;
2029 kurb->actual_length = urb32.actual_length;
2030 kurb->start_frame = urb32.start_frame;
2031 kurb->number_of_packets = urb32.number_of_packets;
2032 kurb->error_count = urb32.error_count;
2033 kurb->signr = urb32.signr;
2034 kurb->usercontext = compat_ptr(urb32.usercontext);
2038 static int proc_submiturb_compat(struct usb_dev_state *ps, void __user *arg)
2040 struct usbdevfs_urb uurb;
2042 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
2045 return proc_do_submiturb(ps, &uurb,
2046 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
2050 static int processcompl_compat(struct async *as, void __user * __user *arg)
2052 struct urb *urb = as->urb;
2053 struct usbdevfs_urb32 __user *userurb = as->userurb;
2054 void __user *addr = as->userurb;
2057 compute_isochronous_actual_length(urb);
2058 if (as->userbuffer && urb->actual_length) {
2059 if (copy_urb_data_to_user(as->userbuffer, urb))
2062 if (put_user(as->status, &userurb->status))
2064 if (put_user(urb->actual_length, &userurb->actual_length))
2066 if (put_user(urb->error_count, &userurb->error_count))
2069 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
2070 for (i = 0; i < urb->number_of_packets; i++) {
2071 if (put_user(urb->iso_frame_desc[i].actual_length,
2072 &userurb->iso_frame_desc[i].actual_length))
2074 if (put_user(urb->iso_frame_desc[i].status,
2075 &userurb->iso_frame_desc[i].status))
2080 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
2085 static int proc_reapurb_compat(struct usb_dev_state *ps, void __user *arg)
2087 struct async *as = reap_as(ps);
2092 snoop(&ps->dev->dev, "reap %px\n", as->userurb);
2093 retval = processcompl_compat(as, (void __user * __user *)arg);
2097 if (signal_pending(current))
2102 static int proc_reapurbnonblock_compat(struct usb_dev_state *ps, void __user *arg)
2107 as = async_getcompleted(ps);
2109 snoop(&ps->dev->dev, "reap %px\n", as->userurb);
2110 retval = processcompl_compat(as, (void __user * __user *)arg);
2113 retval = (connected(ps) ? -EAGAIN : -ENODEV);
2121 static int proc_disconnectsignal(struct usb_dev_state *ps, void __user *arg)
2123 struct usbdevfs_disconnectsignal ds;
2125 if (copy_from_user(&ds, arg, sizeof(ds)))
2127 ps->discsignr = ds.signr;
2128 ps->disccontext = ds.context;
2132 static int proc_claiminterface(struct usb_dev_state *ps, void __user *arg)
2136 if (get_user(ifnum, (unsigned int __user *)arg))
2138 return claimintf(ps, ifnum);
2141 static int proc_releaseinterface(struct usb_dev_state *ps, void __user *arg)
2146 if (get_user(ifnum, (unsigned int __user *)arg))
2148 ret = releaseintf(ps, ifnum);
2151 destroy_async_on_interface(ps, ifnum);
2155 static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl)
2160 struct usb_interface *intf = NULL;
2161 struct usb_driver *driver = NULL;
2163 if (ps->privileges_dropped)
2167 size = _IOC_SIZE(ctl->ioctl_code);
2169 buf = kmalloc(size, GFP_KERNEL);
2172 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
2173 if (copy_from_user(buf, ctl->data, size)) {
2178 memset(buf, 0, size);
2182 if (!connected(ps)) {
2187 if (ps->dev->state != USB_STATE_CONFIGURED)
2188 retval = -EHOSTUNREACH;
2189 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
2191 else switch (ctl->ioctl_code) {
2193 /* disconnect kernel driver from interface */
2194 case USBDEVFS_DISCONNECT:
2195 if (intf->dev.driver) {
2196 driver = to_usb_driver(intf->dev.driver);
2197 dev_dbg(&intf->dev, "disconnect by usbfs\n");
2198 usb_driver_release_interface(driver, intf);
2203 /* let kernel drivers try to (re)bind to the interface */
2204 case USBDEVFS_CONNECT:
2205 if (!intf->dev.driver)
2206 retval = device_attach(&intf->dev);
2211 /* talk directly to the interface's driver */
2213 if (intf->dev.driver)
2214 driver = to_usb_driver(intf->dev.driver);
2215 if (driver == NULL || driver->unlocked_ioctl == NULL) {
2218 retval = driver->unlocked_ioctl(intf, ctl->ioctl_code, buf);
2219 if (retval == -ENOIOCTLCMD)
2224 /* cleanup and return */
2226 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
2228 && copy_to_user(ctl->data, buf, size) != 0)
2235 static int proc_ioctl_default(struct usb_dev_state *ps, void __user *arg)
2237 struct usbdevfs_ioctl ctrl;
2239 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
2241 return proc_ioctl(ps, &ctrl);
2244 #ifdef CONFIG_COMPAT
2245 static int proc_ioctl_compat(struct usb_dev_state *ps, compat_uptr_t arg)
2247 struct usbdevfs_ioctl32 ioc32;
2248 struct usbdevfs_ioctl ctrl;
2250 if (copy_from_user(&ioc32, compat_ptr(arg), sizeof(ioc32)))
2252 ctrl.ifno = ioc32.ifno;
2253 ctrl.ioctl_code = ioc32.ioctl_code;
2254 ctrl.data = compat_ptr(ioc32.data);
2255 return proc_ioctl(ps, &ctrl);
2259 static int proc_claim_port(struct usb_dev_state *ps, void __user *arg)
2264 if (get_user(portnum, (unsigned __user *) arg))
2266 rc = usb_hub_claim_port(ps->dev, portnum, ps);
2268 snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n",
2269 portnum, task_pid_nr(current), current->comm);
2273 static int proc_release_port(struct usb_dev_state *ps, void __user *arg)
2277 if (get_user(portnum, (unsigned __user *) arg))
2279 return usb_hub_release_port(ps->dev, portnum, ps);
2282 static int proc_get_capabilities(struct usb_dev_state *ps, void __user *arg)
2286 caps = USBDEVFS_CAP_ZERO_PACKET | USBDEVFS_CAP_NO_PACKET_SIZE_LIM |
2287 USBDEVFS_CAP_REAP_AFTER_DISCONNECT | USBDEVFS_CAP_MMAP |
2288 USBDEVFS_CAP_DROP_PRIVILEGES;
2289 if (!ps->dev->bus->no_stop_on_short)
2290 caps |= USBDEVFS_CAP_BULK_CONTINUATION;
2291 if (ps->dev->bus->sg_tablesize)
2292 caps |= USBDEVFS_CAP_BULK_SCATTER_GATHER;
2294 if (put_user(caps, (__u32 __user *)arg))
2300 static int proc_disconnect_claim(struct usb_dev_state *ps, void __user *arg)
2302 struct usbdevfs_disconnect_claim dc;
2303 struct usb_interface *intf;
2305 if (copy_from_user(&dc, arg, sizeof(dc)))
2308 intf = usb_ifnum_to_if(ps->dev, dc.interface);
2312 if (intf->dev.driver) {
2313 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
2315 if (ps->privileges_dropped)
2318 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_IF_DRIVER) &&
2319 strncmp(dc.driver, intf->dev.driver->name,
2320 sizeof(dc.driver)) != 0)
2323 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_EXCEPT_DRIVER) &&
2324 strncmp(dc.driver, intf->dev.driver->name,
2325 sizeof(dc.driver)) == 0)
2328 dev_dbg(&intf->dev, "disconnect by usbfs\n");
2329 usb_driver_release_interface(driver, intf);
2332 return claimintf(ps, dc.interface);
2335 static int proc_alloc_streams(struct usb_dev_state *ps, void __user *arg)
2337 unsigned num_streams, num_eps;
2338 struct usb_host_endpoint **eps;
2339 struct usb_interface *intf;
2342 r = parse_usbdevfs_streams(ps, arg, &num_streams, &num_eps,
2347 destroy_async_on_interface(ps,
2348 intf->altsetting[0].desc.bInterfaceNumber);
2350 r = usb_alloc_streams(intf, eps, num_eps, num_streams, GFP_KERNEL);
2355 static int proc_free_streams(struct usb_dev_state *ps, void __user *arg)
2358 struct usb_host_endpoint **eps;
2359 struct usb_interface *intf;
2362 r = parse_usbdevfs_streams(ps, arg, NULL, &num_eps, &eps, &intf);
2366 destroy_async_on_interface(ps,
2367 intf->altsetting[0].desc.bInterfaceNumber);
2369 r = usb_free_streams(intf, eps, num_eps, GFP_KERNEL);
2374 static int proc_drop_privileges(struct usb_dev_state *ps, void __user *arg)
2378 if (copy_from_user(&data, arg, sizeof(data)))
2381 /* This is a one way operation. Once privileges are
2382 * dropped, you cannot regain them. You may however reissue
2383 * this ioctl to shrink the allowed interfaces mask.
2385 ps->interface_allowed_mask &= data;
2386 ps->privileges_dropped = true;
2392 * NOTE: All requests here that have interface numbers as parameters
2393 * are assuming that somehow the configuration has been prevented from
2394 * changing. But there's no mechanism to ensure that...
2396 static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
2399 struct usb_dev_state *ps = file->private_data;
2400 struct inode *inode = file_inode(file);
2401 struct usb_device *dev = ps->dev;
2404 if (!(file->f_mode & FMODE_WRITE))
2407 usb_lock_device(dev);
2409 /* Reap operations are allowed even after disconnection */
2411 case USBDEVFS_REAPURB:
2412 snoop(&dev->dev, "%s: REAPURB\n", __func__);
2413 ret = proc_reapurb(ps, p);
2416 case USBDEVFS_REAPURBNDELAY:
2417 snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
2418 ret = proc_reapurbnonblock(ps, p);
2421 #ifdef CONFIG_COMPAT
2422 case USBDEVFS_REAPURB32:
2423 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
2424 ret = proc_reapurb_compat(ps, p);
2427 case USBDEVFS_REAPURBNDELAY32:
2428 snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
2429 ret = proc_reapurbnonblock_compat(ps, p);
2434 if (!connected(ps)) {
2435 usb_unlock_device(dev);
2440 case USBDEVFS_CONTROL:
2441 snoop(&dev->dev, "%s: CONTROL\n", __func__);
2442 ret = proc_control(ps, p);
2444 inode->i_mtime = current_time(inode);
2448 snoop(&dev->dev, "%s: BULK\n", __func__);
2449 ret = proc_bulk(ps, p);
2451 inode->i_mtime = current_time(inode);
2454 case USBDEVFS_RESETEP:
2455 snoop(&dev->dev, "%s: RESETEP\n", __func__);
2456 ret = proc_resetep(ps, p);
2458 inode->i_mtime = current_time(inode);
2461 case USBDEVFS_RESET:
2462 snoop(&dev->dev, "%s: RESET\n", __func__);
2463 ret = proc_resetdevice(ps);
2466 case USBDEVFS_CLEAR_HALT:
2467 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
2468 ret = proc_clearhalt(ps, p);
2470 inode->i_mtime = current_time(inode);
2473 case USBDEVFS_GETDRIVER:
2474 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
2475 ret = proc_getdriver(ps, p);
2478 case USBDEVFS_CONNECTINFO:
2479 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
2480 ret = proc_connectinfo(ps, p);
2483 case USBDEVFS_SETINTERFACE:
2484 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
2485 ret = proc_setintf(ps, p);
2488 case USBDEVFS_SETCONFIGURATION:
2489 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
2490 ret = proc_setconfig(ps, p);
2493 case USBDEVFS_SUBMITURB:
2494 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
2495 ret = proc_submiturb(ps, p);
2497 inode->i_mtime = current_time(inode);
2500 #ifdef CONFIG_COMPAT
2501 case USBDEVFS_CONTROL32:
2502 snoop(&dev->dev, "%s: CONTROL32\n", __func__);
2503 ret = proc_control_compat(ps, p);
2505 inode->i_mtime = current_time(inode);
2508 case USBDEVFS_BULK32:
2509 snoop(&dev->dev, "%s: BULK32\n", __func__);
2510 ret = proc_bulk_compat(ps, p);
2512 inode->i_mtime = current_time(inode);
2515 case USBDEVFS_DISCSIGNAL32:
2516 snoop(&dev->dev, "%s: DISCSIGNAL32\n", __func__);
2517 ret = proc_disconnectsignal_compat(ps, p);
2520 case USBDEVFS_SUBMITURB32:
2521 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
2522 ret = proc_submiturb_compat(ps, p);
2524 inode->i_mtime = current_time(inode);
2527 case USBDEVFS_IOCTL32:
2528 snoop(&dev->dev, "%s: IOCTL32\n", __func__);
2529 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
2533 case USBDEVFS_DISCARDURB:
2534 snoop(&dev->dev, "%s: DISCARDURB %px\n", __func__, p);
2535 ret = proc_unlinkurb(ps, p);
2538 case USBDEVFS_DISCSIGNAL:
2539 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
2540 ret = proc_disconnectsignal(ps, p);
2543 case USBDEVFS_CLAIMINTERFACE:
2544 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
2545 ret = proc_claiminterface(ps, p);
2548 case USBDEVFS_RELEASEINTERFACE:
2549 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
2550 ret = proc_releaseinterface(ps, p);
2553 case USBDEVFS_IOCTL:
2554 snoop(&dev->dev, "%s: IOCTL\n", __func__);
2555 ret = proc_ioctl_default(ps, p);
2558 case USBDEVFS_CLAIM_PORT:
2559 snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__);
2560 ret = proc_claim_port(ps, p);
2563 case USBDEVFS_RELEASE_PORT:
2564 snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__);
2565 ret = proc_release_port(ps, p);
2567 case USBDEVFS_GET_CAPABILITIES:
2568 ret = proc_get_capabilities(ps, p);
2570 case USBDEVFS_DISCONNECT_CLAIM:
2571 ret = proc_disconnect_claim(ps, p);
2573 case USBDEVFS_ALLOC_STREAMS:
2574 ret = proc_alloc_streams(ps, p);
2576 case USBDEVFS_FREE_STREAMS:
2577 ret = proc_free_streams(ps, p);
2579 case USBDEVFS_DROP_PRIVILEGES:
2580 ret = proc_drop_privileges(ps, p);
2582 case USBDEVFS_GET_SPEED:
2583 ret = ps->dev->speed;
2588 usb_unlock_device(dev);
2590 inode->i_atime = current_time(inode);
2594 static long usbdev_ioctl(struct file *file, unsigned int cmd,
2599 ret = usbdev_do_ioctl(file, cmd, (void __user *)arg);
2604 #ifdef CONFIG_COMPAT
2605 static long usbdev_compat_ioctl(struct file *file, unsigned int cmd,
2610 ret = usbdev_do_ioctl(file, cmd, compat_ptr(arg));
2616 /* No kernel lock - fine */
2617 static unsigned int usbdev_poll(struct file *file,
2618 struct poll_table_struct *wait)
2620 struct usb_dev_state *ps = file->private_data;
2621 unsigned int mask = 0;
2623 poll_wait(file, &ps->wait, wait);
2624 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
2625 mask |= POLLOUT | POLLWRNORM;
2628 if (list_empty(&ps->list))
2633 const struct file_operations usbdev_file_operations = {
2634 .owner = THIS_MODULE,
2635 .llseek = no_seek_end_llseek,
2636 .read = usbdev_read,
2637 .poll = usbdev_poll,
2638 .unlocked_ioctl = usbdev_ioctl,
2639 #ifdef CONFIG_COMPAT
2640 .compat_ioctl = usbdev_compat_ioctl,
2642 .mmap = usbdev_mmap,
2643 .open = usbdev_open,
2644 .release = usbdev_release,
2647 static void usbdev_remove(struct usb_device *udev)
2649 struct usb_dev_state *ps;
2650 struct siginfo sinfo;
2652 while (!list_empty(&udev->filelist)) {
2653 ps = list_entry(udev->filelist.next, struct usb_dev_state, list);
2654 destroy_all_async(ps);
2655 wake_up_all(&ps->wait);
2656 list_del_init(&ps->list);
2657 if (ps->discsignr) {
2658 memset(&sinfo, 0, sizeof(sinfo));
2659 sinfo.si_signo = ps->discsignr;
2660 sinfo.si_errno = EPIPE;
2661 sinfo.si_code = SI_ASYNCIO;
2662 sinfo.si_addr = ps->disccontext;
2663 kill_pid_info_as_cred(ps->discsignr, &sinfo,
2664 ps->disc_pid, ps->cred, ps->secid);
2669 static int usbdev_notify(struct notifier_block *self,
2670 unsigned long action, void *dev)
2673 case USB_DEVICE_ADD:
2675 case USB_DEVICE_REMOVE:
2682 static struct notifier_block usbdev_nb = {
2683 .notifier_call = usbdev_notify,
2686 static struct cdev usb_device_cdev;
2688 int __init usb_devio_init(void)
2692 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
2695 printk(KERN_ERR "Unable to register minors for usb_device\n");
2698 cdev_init(&usb_device_cdev, &usbdev_file_operations);
2699 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
2701 printk(KERN_ERR "Unable to get usb_device major %d\n",
2705 usb_register_notify(&usbdev_nb);
2710 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
2714 void usb_devio_cleanup(void)
2716 usb_unregister_notify(&usbdev_nb);
2717 cdev_del(&usb_device_cdev);
2718 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);