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/slab.h>
40 #include <linux/signal.h>
41 #include <linux/poll.h>
42 #include <linux/module.h>
43 #include <linux/usb.h>
44 #include <linux/usbdevice_fs.h>
45 #include <linux/usb/hcd.h> /* for usbcore internals */
46 #include <linux/cdev.h>
47 #include <linux/notifier.h>
48 #include <linux/security.h>
49 #include <linux/user_namespace.h>
50 #include <linux/scatterlist.h>
51 #include <asm/uaccess.h>
52 #include <asm/byteorder.h>
53 #include <linux/moduleparam.h>
58 #define USB_DEVICE_MAX USB_MAXBUS * 128
59 #define USB_SG_SIZE 16384 /* split-size for large txs */
61 /* Mutual exclusion for removal, open, and release */
62 DEFINE_MUTEX(usbfs_mutex);
65 struct list_head list; /* state list */
66 struct usb_device *dev;
68 spinlock_t lock; /* protects the async urb lists */
69 struct list_head async_pending;
70 struct list_head async_completed;
71 wait_queue_head_t wait; /* wake up if a request completed */
72 unsigned int discsignr;
74 const struct cred *cred;
75 void __user *disccontext;
76 unsigned long ifclaimed;
78 u32 disabled_bulk_eps;
82 struct list_head asynclist;
85 const struct cred *cred;
88 void __user *userbuffer;
91 unsigned int mem_usage;
98 static bool usbfs_snoop;
99 module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
100 MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
102 #define snoop(dev, format, arg...) \
105 dev_info(dev , format , ## arg); \
112 #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
114 /* Limit on the total amount of memory we can allocate for transfers */
115 static unsigned usbfs_memory_mb = 16;
116 module_param(usbfs_memory_mb, uint, 0644);
117 MODULE_PARM_DESC(usbfs_memory_mb,
118 "maximum MB allowed for usbfs buffers (0 = no limit)");
120 /* Hard limit, necessary to avoid aithmetic overflow */
121 #define USBFS_XFER_MAX (UINT_MAX / 2 - 1000000)
123 static atomic_t usbfs_memory_usage; /* Total memory currently allocated */
125 /* Check whether it's okay to allocate more memory for a transfer */
126 static int usbfs_increase_memory_usage(unsigned amount)
131 * Convert usbfs_memory_mb to bytes, avoiding overflows.
132 * 0 means use the hard limit (effectively unlimited).
134 lim = ACCESS_ONCE(usbfs_memory_mb);
135 if (lim == 0 || lim > (USBFS_XFER_MAX >> 20))
136 lim = USBFS_XFER_MAX;
140 atomic_add(amount, &usbfs_memory_usage);
141 if (atomic_read(&usbfs_memory_usage) <= lim)
143 atomic_sub(amount, &usbfs_memory_usage);
147 /* Memory for a transfer is being deallocated */
148 static void usbfs_decrease_memory_usage(unsigned amount)
150 atomic_sub(amount, &usbfs_memory_usage);
153 static int connected(struct dev_state *ps)
155 return (!list_empty(&ps->list) &&
156 ps->dev->state != USB_STATE_NOTATTACHED);
159 static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
163 mutex_lock(&file->f_dentry->d_inode->i_mutex);
167 file->f_pos = offset;
171 file->f_pos += offset;
179 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
183 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
186 struct dev_state *ps = file->private_data;
187 struct usb_device *dev = ps->dev;
194 usb_lock_device(dev);
195 if (!connected(ps)) {
198 } else if (pos < 0) {
203 if (pos < sizeof(struct usb_device_descriptor)) {
204 /* 18 bytes - fits on the stack */
205 struct usb_device_descriptor temp_desc;
207 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
208 le16_to_cpus(&temp_desc.bcdUSB);
209 le16_to_cpus(&temp_desc.idVendor);
210 le16_to_cpus(&temp_desc.idProduct);
211 le16_to_cpus(&temp_desc.bcdDevice);
213 len = sizeof(struct usb_device_descriptor) - pos;
216 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
227 pos = sizeof(struct usb_device_descriptor);
228 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
229 struct usb_config_descriptor *config =
230 (struct usb_config_descriptor *)dev->rawdescriptors[i];
231 unsigned int length = le16_to_cpu(config->wTotalLength);
233 if (*ppos < pos + length) {
235 /* The descriptor may claim to be longer than it
236 * really is. Here is the actual allocated length. */
238 le16_to_cpu(dev->config[i].desc.wTotalLength);
240 len = length - (*ppos - pos);
244 /* Simply don't write (skip over) unallocated parts */
245 if (alloclen > (*ppos - pos)) {
246 alloclen -= (*ppos - pos);
247 if (copy_to_user(buf,
248 dev->rawdescriptors[i] + (*ppos - pos),
249 min(len, alloclen))) {
265 usb_unlock_device(dev);
270 * async list handling
273 static struct async *alloc_async(unsigned int numisoframes)
277 as = kzalloc(sizeof(struct async), GFP_KERNEL);
280 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
288 static void free_async(struct async *as)
295 for (i = 0; i < as->urb->num_sgs; i++) {
296 if (sg_page(&as->urb->sg[i]))
297 kfree(sg_virt(&as->urb->sg[i]));
300 kfree(as->urb->transfer_buffer);
301 kfree(as->urb->setup_packet);
302 usb_free_urb(as->urb);
303 usbfs_decrease_memory_usage(as->mem_usage);
307 static void async_newpending(struct async *as)
309 struct dev_state *ps = as->ps;
312 spin_lock_irqsave(&ps->lock, flags);
313 list_add_tail(&as->asynclist, &ps->async_pending);
314 spin_unlock_irqrestore(&ps->lock, flags);
317 static void async_removepending(struct async *as)
319 struct dev_state *ps = as->ps;
322 spin_lock_irqsave(&ps->lock, flags);
323 list_del_init(&as->asynclist);
324 spin_unlock_irqrestore(&ps->lock, flags);
327 static struct async *async_getcompleted(struct dev_state *ps)
330 struct async *as = NULL;
332 spin_lock_irqsave(&ps->lock, flags);
333 if (!list_empty(&ps->async_completed)) {
334 as = list_entry(ps->async_completed.next, struct async,
336 list_del_init(&as->asynclist);
338 spin_unlock_irqrestore(&ps->lock, flags);
342 static struct async *async_getpending(struct dev_state *ps,
343 void __user *userurb)
347 list_for_each_entry(as, &ps->async_pending, asynclist)
348 if (as->userurb == userurb) {
349 list_del_init(&as->asynclist);
356 static void snoop_urb(struct usb_device *udev,
357 void __user *userurb, int pipe, unsigned length,
358 int timeout_or_status, enum snoop_when when,
359 unsigned char *data, unsigned data_len)
361 static const char *types[] = {"isoc", "int", "ctrl", "bulk"};
362 static const char *dirs[] = {"out", "in"};
369 ep = usb_pipeendpoint(pipe);
370 t = types[usb_pipetype(pipe)];
371 d = dirs[!!usb_pipein(pipe)];
373 if (userurb) { /* Async */
375 dev_info(&udev->dev, "userurb %p, ep%d %s-%s, "
377 userurb, ep, t, d, length);
379 dev_info(&udev->dev, "userurb %p, ep%d %s-%s, "
380 "actual_length %u status %d\n",
381 userurb, ep, t, d, length,
385 dev_info(&udev->dev, "ep%d %s-%s, length %u, "
387 ep, t, d, length, timeout_or_status);
389 dev_info(&udev->dev, "ep%d %s-%s, actual_length %u, "
391 ep, t, d, length, timeout_or_status);
394 if (data && data_len > 0) {
395 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
400 static void snoop_urb_data(struct urb *urb, unsigned len)
407 if (urb->num_sgs == 0) {
408 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
409 urb->transfer_buffer, len, 1);
413 for (i = 0; i < urb->num_sgs && len; i++) {
414 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
415 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
416 sg_virt(&urb->sg[i]), size, 1);
421 static int copy_urb_data_to_user(u8 __user *userbuffer, struct urb *urb)
423 unsigned i, len, size;
425 if (urb->number_of_packets > 0) /* Isochronous */
426 len = urb->transfer_buffer_length;
428 len = urb->actual_length;
430 if (urb->num_sgs == 0) {
431 if (copy_to_user(userbuffer, urb->transfer_buffer, len))
436 for (i = 0; i < urb->num_sgs && len; i++) {
437 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
438 if (copy_to_user(userbuffer, sg_virt(&urb->sg[i]), size))
447 #define AS_CONTINUATION 1
450 static void cancel_bulk_urbs(struct dev_state *ps, unsigned bulk_addr)
457 /* Mark all the pending URBs that match bulk_addr, up to but not
458 * including the first one without AS_CONTINUATION. If such an
459 * URB is encountered then a new transfer has already started so
460 * the endpoint doesn't need to be disabled; otherwise it does.
462 list_for_each_entry(as, &ps->async_pending, asynclist) {
463 if (as->bulk_addr == bulk_addr) {
464 if (as->bulk_status != AS_CONTINUATION)
466 as->bulk_status = AS_UNLINK;
470 ps->disabled_bulk_eps |= (1 << bulk_addr);
472 /* Now carefully unlink all the marked pending URBs */
474 list_for_each_entry(as, &ps->async_pending, asynclist) {
475 if (as->bulk_status == AS_UNLINK) {
476 as->bulk_status = 0; /* Only once */
479 spin_unlock(&ps->lock); /* Allow completions */
482 spin_lock(&ps->lock);
488 static void async_completed(struct urb *urb)
490 struct async *as = urb->context;
491 struct dev_state *ps = as->ps;
492 struct siginfo sinfo;
493 struct pid *pid = NULL;
495 const struct cred *cred = NULL;
498 spin_lock(&ps->lock);
499 list_move_tail(&as->asynclist, &ps->async_completed);
500 as->status = urb->status;
503 sinfo.si_signo = as->signr;
504 sinfo.si_errno = as->status;
505 sinfo.si_code = SI_ASYNCIO;
506 sinfo.si_addr = as->userurb;
507 pid = get_pid(as->pid);
508 cred = get_cred(as->cred);
511 snoop(&urb->dev->dev, "urb complete\n");
512 snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length,
513 as->status, COMPLETE, NULL, 0);
514 if ((urb->transfer_flags & URB_DIR_MASK) == USB_DIR_IN)
515 snoop_urb_data(urb, urb->actual_length);
517 if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET &&
518 as->status != -ENOENT)
519 cancel_bulk_urbs(ps, as->bulk_addr);
520 spin_unlock(&ps->lock);
523 kill_pid_info_as_cred(sinfo.si_signo, &sinfo, pid, cred, secid);
531 static void destroy_async(struct dev_state *ps, struct list_head *list)
537 spin_lock_irqsave(&ps->lock, flags);
538 while (!list_empty(list)) {
539 as = list_entry(list->next, struct async, asynclist);
540 list_del_init(&as->asynclist);
544 /* drop the spinlock so the completion handler can run */
545 spin_unlock_irqrestore(&ps->lock, flags);
548 spin_lock_irqsave(&ps->lock, flags);
550 spin_unlock_irqrestore(&ps->lock, flags);
553 static void destroy_async_on_interface(struct dev_state *ps,
556 struct list_head *p, *q, hitlist;
559 INIT_LIST_HEAD(&hitlist);
560 spin_lock_irqsave(&ps->lock, flags);
561 list_for_each_safe(p, q, &ps->async_pending)
562 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
563 list_move_tail(p, &hitlist);
564 spin_unlock_irqrestore(&ps->lock, flags);
565 destroy_async(ps, &hitlist);
568 static void destroy_all_async(struct dev_state *ps)
570 destroy_async(ps, &ps->async_pending);
574 * interface claims are made only at the request of user level code,
575 * which can also release them (explicitly or by closing files).
576 * they're also undone when devices disconnect.
579 static int driver_probe(struct usb_interface *intf,
580 const struct usb_device_id *id)
585 static void driver_disconnect(struct usb_interface *intf)
587 struct dev_state *ps = usb_get_intfdata(intf);
588 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
593 /* NOTE: this relies on usbcore having canceled and completed
594 * all pending I/O requests; 2.6 does that.
597 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
598 clear_bit(ifnum, &ps->ifclaimed);
600 dev_warn(&intf->dev, "interface number %u out of range\n",
603 usb_set_intfdata(intf, NULL);
605 /* force async requests to complete */
606 destroy_async_on_interface(ps, ifnum);
609 /* The following routines are merely placeholders. There is no way
610 * to inform a user task about suspend or resumes.
612 static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
617 static int driver_resume(struct usb_interface *intf)
622 struct usb_driver usbfs_driver = {
624 .probe = driver_probe,
625 .disconnect = driver_disconnect,
626 .suspend = driver_suspend,
627 .resume = driver_resume,
630 static int claimintf(struct dev_state *ps, unsigned int ifnum)
632 struct usb_device *dev = ps->dev;
633 struct usb_interface *intf;
636 if (ifnum >= 8*sizeof(ps->ifclaimed))
638 /* already claimed */
639 if (test_bit(ifnum, &ps->ifclaimed))
642 intf = usb_ifnum_to_if(dev, ifnum);
646 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
648 set_bit(ifnum, &ps->ifclaimed);
652 static int releaseintf(struct dev_state *ps, unsigned int ifnum)
654 struct usb_device *dev;
655 struct usb_interface *intf;
659 if (ifnum >= 8*sizeof(ps->ifclaimed))
662 intf = usb_ifnum_to_if(dev, ifnum);
665 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
666 usb_driver_release_interface(&usbfs_driver, intf);
672 static int checkintf(struct dev_state *ps, unsigned int ifnum)
674 if (ps->dev->state != USB_STATE_CONFIGURED)
675 return -EHOSTUNREACH;
676 if (ifnum >= 8*sizeof(ps->ifclaimed))
678 if (test_bit(ifnum, &ps->ifclaimed))
680 /* if not yet claimed, claim it for the driver */
681 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
682 "interface %u before use\n", task_pid_nr(current),
683 current->comm, ifnum);
684 return claimintf(ps, ifnum);
687 static int findintfep(struct usb_device *dev, unsigned int ep)
689 unsigned int i, j, e;
690 struct usb_interface *intf;
691 struct usb_host_interface *alts;
692 struct usb_endpoint_descriptor *endpt;
694 if (ep & ~(USB_DIR_IN|0xf))
698 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
699 intf = dev->actconfig->interface[i];
700 for (j = 0; j < intf->num_altsetting; j++) {
701 alts = &intf->altsetting[j];
702 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
703 endpt = &alts->endpoint[e].desc;
704 if (endpt->bEndpointAddress == ep)
705 return alts->desc.bInterfaceNumber;
712 static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
713 unsigned int request, unsigned int index)
716 struct usb_host_interface *alt_setting;
718 if (ps->dev->state != USB_STATE_UNAUTHENTICATED
719 && ps->dev->state != USB_STATE_ADDRESS
720 && ps->dev->state != USB_STATE_CONFIGURED)
721 return -EHOSTUNREACH;
722 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
726 * check for the special corner case 'get_device_id' in the printer
727 * class specification, where wIndex is (interface << 8 | altsetting)
728 * instead of just interface
730 if (requesttype == 0xa1 && request == 0) {
731 alt_setting = usb_find_alt_setting(ps->dev->actconfig,
732 index >> 8, index & 0xff);
734 && alt_setting->desc.bInterfaceClass == USB_CLASS_PRINTER)
739 switch (requesttype & USB_RECIP_MASK) {
740 case USB_RECIP_ENDPOINT:
741 ret = findintfep(ps->dev, index);
743 ret = checkintf(ps, ret);
746 case USB_RECIP_INTERFACE:
747 ret = checkintf(ps, index);
753 static int match_devt(struct device *dev, void *data)
755 return dev->devt == (dev_t) (unsigned long) data;
758 static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
762 dev = bus_find_device(&usb_bus_type, NULL,
763 (void *) (unsigned long) devt, match_devt);
766 return container_of(dev, struct usb_device, dev);
772 static int usbdev_open(struct inode *inode, struct file *file)
774 struct usb_device *dev = NULL;
775 struct dev_state *ps;
779 ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
785 /* Protect against simultaneous removal or release */
786 mutex_lock(&usbfs_mutex);
788 /* usbdev device-node */
789 if (imajor(inode) == USB_DEVICE_MAJOR)
790 dev = usbdev_lookup_by_devt(inode->i_rdev);
792 mutex_unlock(&usbfs_mutex);
797 usb_lock_device(dev);
798 if (dev->state == USB_STATE_NOTATTACHED)
799 goto out_unlock_device;
801 ret = usb_autoresume_device(dev);
803 goto out_unlock_device;
807 spin_lock_init(&ps->lock);
808 INIT_LIST_HEAD(&ps->list);
809 INIT_LIST_HEAD(&ps->async_pending);
810 INIT_LIST_HEAD(&ps->async_completed);
811 init_waitqueue_head(&ps->wait);
813 ps->disc_pid = get_pid(task_pid(current));
814 ps->cred = get_current_cred();
815 ps->disccontext = NULL;
817 security_task_getsecid(current, &ps->secid);
819 list_add_tail(&ps->list, &dev->filelist);
820 file->private_data = ps;
821 usb_unlock_device(dev);
822 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
827 usb_unlock_device(dev);
834 static int usbdev_release(struct inode *inode, struct file *file)
836 struct dev_state *ps = file->private_data;
837 struct usb_device *dev = ps->dev;
841 usb_lock_device(dev);
842 usb_hub_release_all_ports(dev, ps);
844 list_del_init(&ps->list);
846 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
848 if (test_bit(ifnum, &ps->ifclaimed))
849 releaseintf(ps, ifnum);
851 destroy_all_async(ps);
852 usb_autosuspend_device(dev);
853 usb_unlock_device(dev);
855 put_pid(ps->disc_pid);
858 as = async_getcompleted(ps);
861 as = async_getcompleted(ps);
867 static int proc_control(struct dev_state *ps, void __user *arg)
869 struct usb_device *dev = ps->dev;
870 struct usbdevfs_ctrltransfer ctrl;
876 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
878 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.bRequest,
882 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
883 if (wLength > PAGE_SIZE)
885 ret = usbfs_increase_memory_usage(PAGE_SIZE + sizeof(struct urb) +
886 sizeof(struct usb_ctrlrequest));
889 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
895 snoop(&dev->dev, "control urb: bRequestType=%02x "
896 "bRequest=%02x wValue=%04x "
897 "wIndex=%04x wLength=%04x\n",
898 ctrl.bRequestType, ctrl.bRequest,
899 __le16_to_cpup(&ctrl.wValue),
900 __le16_to_cpup(&ctrl.wIndex),
901 __le16_to_cpup(&ctrl.wLength));
902 if (ctrl.bRequestType & 0x80) {
903 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
908 pipe = usb_rcvctrlpipe(dev, 0);
909 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT, NULL, 0);
911 usb_unlock_device(dev);
912 i = usb_control_msg(dev, pipe, ctrl.bRequest,
913 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
914 tbuf, ctrl.wLength, tmo);
915 usb_lock_device(dev);
916 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE,
918 if ((i > 0) && ctrl.wLength) {
919 if (copy_to_user(ctrl.data, tbuf, i)) {
926 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
931 pipe = usb_sndctrlpipe(dev, 0);
932 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT,
935 usb_unlock_device(dev);
936 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
937 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
938 tbuf, ctrl.wLength, tmo);
939 usb_lock_device(dev);
940 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE, NULL, 0);
942 if (i < 0 && i != -EPIPE) {
943 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
944 "failed cmd %s rqt %u rq %u len %u ret %d\n",
945 current->comm, ctrl.bRequestType, ctrl.bRequest,
950 free_page((unsigned long) tbuf);
951 usbfs_decrease_memory_usage(PAGE_SIZE + sizeof(struct urb) +
952 sizeof(struct usb_ctrlrequest));
956 static int proc_bulk(struct dev_state *ps, void __user *arg)
958 struct usb_device *dev = ps->dev;
959 struct usbdevfs_bulktransfer bulk;
960 unsigned int tmo, len1, pipe;
965 if (copy_from_user(&bulk, arg, sizeof(bulk)))
967 ret = findintfep(ps->dev, bulk.ep);
970 ret = checkintf(ps, ret);
973 if (bulk.ep & USB_DIR_IN)
974 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
976 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
977 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
980 if (len1 >= USBFS_XFER_MAX)
982 ret = usbfs_increase_memory_usage(len1 + sizeof(struct urb));
985 if (!(tbuf = kmalloc(len1, GFP_KERNEL))) {
990 if (bulk.ep & 0x80) {
991 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
995 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, NULL, 0);
997 usb_unlock_device(dev);
998 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
999 usb_lock_device(dev);
1000 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, tbuf, len2);
1003 if (copy_to_user(bulk.data, tbuf, len2)) {
1010 if (copy_from_user(tbuf, bulk.data, len1)) {
1015 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, tbuf, len1);
1017 usb_unlock_device(dev);
1018 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
1019 usb_lock_device(dev);
1020 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, NULL, 0);
1022 ret = (i < 0 ? i : len2);
1025 usbfs_decrease_memory_usage(len1 + sizeof(struct urb));
1029 static int proc_resetep(struct dev_state *ps, void __user *arg)
1034 if (get_user(ep, (unsigned int __user *)arg))
1036 ret = findintfep(ps->dev, ep);
1039 ret = checkintf(ps, ret);
1042 usb_reset_endpoint(ps->dev, ep);
1046 static int proc_clearhalt(struct dev_state *ps, void __user *arg)
1052 if (get_user(ep, (unsigned int __user *)arg))
1054 ret = findintfep(ps->dev, ep);
1057 ret = checkintf(ps, ret);
1060 if (ep & USB_DIR_IN)
1061 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
1063 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
1065 return usb_clear_halt(ps->dev, pipe);
1068 static int proc_getdriver(struct dev_state *ps, void __user *arg)
1070 struct usbdevfs_getdriver gd;
1071 struct usb_interface *intf;
1074 if (copy_from_user(&gd, arg, sizeof(gd)))
1076 intf = usb_ifnum_to_if(ps->dev, gd.interface);
1077 if (!intf || !intf->dev.driver)
1080 strncpy(gd.driver, intf->dev.driver->name,
1082 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
1087 static int proc_connectinfo(struct dev_state *ps, void __user *arg)
1089 struct usbdevfs_connectinfo ci = {
1090 .devnum = ps->dev->devnum,
1091 .slow = ps->dev->speed == USB_SPEED_LOW
1094 if (copy_to_user(arg, &ci, sizeof(ci)))
1099 static int proc_resetdevice(struct dev_state *ps)
1101 return usb_reset_device(ps->dev);
1104 static int proc_setintf(struct dev_state *ps, void __user *arg)
1106 struct usbdevfs_setinterface setintf;
1109 if (copy_from_user(&setintf, arg, sizeof(setintf)))
1111 if ((ret = checkintf(ps, setintf.interface)))
1113 return usb_set_interface(ps->dev, setintf.interface,
1114 setintf.altsetting);
1117 static int proc_setconfig(struct dev_state *ps, void __user *arg)
1121 struct usb_host_config *actconfig;
1123 if (get_user(u, (int __user *)arg))
1126 actconfig = ps->dev->actconfig;
1128 /* Don't touch the device if any interfaces are claimed.
1129 * It could interfere with other drivers' operations, and if
1130 * an interface is claimed by usbfs it could easily deadlock.
1135 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
1136 if (usb_interface_claimed(actconfig->interface[i])) {
1137 dev_warn(&ps->dev->dev,
1138 "usbfs: interface %d claimed by %s "
1139 "while '%s' sets config #%d\n",
1140 actconfig->interface[i]
1142 ->desc.bInterfaceNumber,
1143 actconfig->interface[i]
1152 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
1153 * so avoid usb_set_configuration()'s kick to sysfs
1156 if (actconfig && actconfig->desc.bConfigurationValue == u)
1157 status = usb_reset_configuration(ps->dev);
1159 status = usb_set_configuration(ps->dev, u);
1165 static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1166 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
1169 struct usbdevfs_iso_packet_desc *isopkt = NULL;
1170 struct usb_host_endpoint *ep;
1171 struct async *as = NULL;
1172 struct usb_ctrlrequest *dr = NULL;
1173 unsigned int u, totlen, isofrmlen;
1174 int i, ret, is_in, num_sgs = 0, ifnum = -1;
1177 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
1178 USBDEVFS_URB_SHORT_NOT_OK |
1179 USBDEVFS_URB_BULK_CONTINUATION |
1180 USBDEVFS_URB_NO_FSBR |
1181 USBDEVFS_URB_ZERO_PACKET |
1182 USBDEVFS_URB_NO_INTERRUPT))
1184 if (uurb->buffer_length > 0 && !uurb->buffer)
1186 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
1187 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
1188 ifnum = findintfep(ps->dev, uurb->endpoint);
1191 ret = checkintf(ps, ifnum);
1195 if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
1197 ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
1200 ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
1206 switch(uurb->type) {
1207 case USBDEVFS_URB_TYPE_CONTROL:
1208 if (!usb_endpoint_xfer_control(&ep->desc))
1210 /* min 8 byte setup packet */
1211 if (uurb->buffer_length < 8)
1213 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1216 if (copy_from_user(dr, uurb->buffer, 8)) {
1220 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
1224 ret = check_ctrlrecip(ps, dr->bRequestType, dr->bRequest,
1225 le16_to_cpup(&dr->wIndex));
1228 uurb->number_of_packets = 0;
1229 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1231 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1233 uurb->endpoint |= USB_DIR_IN;
1236 uurb->endpoint &= ~USB_DIR_IN;
1238 snoop(&ps->dev->dev, "control urb: bRequestType=%02x "
1239 "bRequest=%02x wValue=%04x "
1240 "wIndex=%04x wLength=%04x\n",
1241 dr->bRequestType, dr->bRequest,
1242 __le16_to_cpup(&dr->wValue),
1243 __le16_to_cpup(&dr->wIndex),
1244 __le16_to_cpup(&dr->wLength));
1245 u = sizeof(struct usb_ctrlrequest);
1248 case USBDEVFS_URB_TYPE_BULK:
1249 switch (usb_endpoint_type(&ep->desc)) {
1250 case USB_ENDPOINT_XFER_CONTROL:
1251 case USB_ENDPOINT_XFER_ISOC:
1253 case USB_ENDPOINT_XFER_INT:
1254 /* allow single-shot interrupt transfers */
1255 uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
1258 uurb->number_of_packets = 0;
1259 num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE);
1260 if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize)
1264 case USBDEVFS_URB_TYPE_INTERRUPT:
1265 if (!usb_endpoint_xfer_int(&ep->desc))
1268 uurb->number_of_packets = 0;
1271 case USBDEVFS_URB_TYPE_ISO:
1272 /* arbitrary limit */
1273 if (uurb->number_of_packets < 1 ||
1274 uurb->number_of_packets > 128)
1276 if (!usb_endpoint_xfer_isoc(&ep->desc))
1278 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1279 uurb->number_of_packets;
1280 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
1282 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
1286 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1288 * sufficient for USB 2.0 high-bandwidth iso */
1289 if (isopkt[u].length > 8192) {
1293 totlen += isopkt[u].length;
1295 u *= sizeof(struct usb_iso_packet_descriptor);
1296 uurb->buffer_length = totlen;
1303 if (uurb->buffer_length >= USBFS_XFER_MAX) {
1307 if (uurb->buffer_length > 0 &&
1308 !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1309 uurb->buffer, uurb->buffer_length)) {
1313 as = alloc_async(uurb->number_of_packets);
1319 u += sizeof(struct async) + sizeof(struct urb) + uurb->buffer_length +
1320 num_sgs * sizeof(struct scatterlist);
1321 ret = usbfs_increase_memory_usage(u);
1327 as->urb->sg = kmalloc(num_sgs * sizeof(struct scatterlist),
1333 as->urb->num_sgs = num_sgs;
1334 sg_init_table(as->urb->sg, as->urb->num_sgs);
1336 totlen = uurb->buffer_length;
1337 for (i = 0; i < as->urb->num_sgs; i++) {
1338 u = (totlen > USB_SG_SIZE) ? USB_SG_SIZE : totlen;
1339 buf = kmalloc(u, GFP_KERNEL);
1344 sg_set_buf(&as->urb->sg[i], buf, u);
1347 if (copy_from_user(buf, uurb->buffer, u)) {
1354 } else if (uurb->buffer_length > 0) {
1355 as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
1357 if (!as->urb->transfer_buffer) {
1363 if (copy_from_user(as->urb->transfer_buffer,
1365 uurb->buffer_length)) {
1369 } else if (uurb->type == USBDEVFS_URB_TYPE_ISO) {
1371 * Isochronous input data may end up being
1372 * discontiguous if some of the packets are short.
1373 * Clear the buffer so that the gaps don't leak
1374 * kernel data to userspace.
1376 memset(as->urb->transfer_buffer, 0,
1377 uurb->buffer_length);
1380 as->urb->dev = ps->dev;
1381 as->urb->pipe = (uurb->type << 30) |
1382 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1383 (uurb->endpoint & USB_DIR_IN);
1385 /* This tedious sequence is necessary because the URB_* flags
1386 * are internal to the kernel and subject to change, whereas
1387 * the USBDEVFS_URB_* flags are a user API and must not be changed.
1389 u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
1390 if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
1392 if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
1393 u |= URB_SHORT_NOT_OK;
1394 if (uurb->flags & USBDEVFS_URB_NO_FSBR)
1396 if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1397 u |= URB_ZERO_PACKET;
1398 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
1399 u |= URB_NO_INTERRUPT;
1400 as->urb->transfer_flags = u;
1402 as->urb->transfer_buffer_length = uurb->buffer_length;
1403 as->urb->setup_packet = (unsigned char *)dr;
1405 as->urb->start_frame = uurb->start_frame;
1406 as->urb->number_of_packets = uurb->number_of_packets;
1407 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1408 ps->dev->speed == USB_SPEED_HIGH)
1409 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
1411 as->urb->interval = ep->desc.bInterval;
1412 as->urb->context = as;
1413 as->urb->complete = async_completed;
1414 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1415 as->urb->iso_frame_desc[u].offset = totlen;
1416 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1417 totlen += isopkt[u].length;
1423 if (is_in && uurb->buffer_length > 0)
1424 as->userbuffer = uurb->buffer;
1426 as->userbuffer = NULL;
1427 as->signr = uurb->signr;
1429 as->pid = get_pid(task_pid(current));
1430 as->cred = get_current_cred();
1431 security_task_getsecid(current, &as->secid);
1432 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1433 as->urb->transfer_buffer_length, 0, SUBMIT,
1436 snoop_urb_data(as->urb, as->urb->transfer_buffer_length);
1438 async_newpending(as);
1440 if (usb_endpoint_xfer_bulk(&ep->desc)) {
1441 spin_lock_irq(&ps->lock);
1443 /* Not exactly the endpoint address; the direction bit is
1444 * shifted to the 0x10 position so that the value will be
1447 as->bulk_addr = usb_endpoint_num(&ep->desc) |
1448 ((ep->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1451 /* If this bulk URB is the start of a new transfer, re-enable
1452 * the endpoint. Otherwise mark it as a continuation URB.
1454 if (uurb->flags & USBDEVFS_URB_BULK_CONTINUATION)
1455 as->bulk_status = AS_CONTINUATION;
1457 ps->disabled_bulk_eps &= ~(1 << as->bulk_addr);
1459 /* Don't accept continuation URBs if the endpoint is
1460 * disabled because of an earlier error.
1462 if (ps->disabled_bulk_eps & (1 << as->bulk_addr))
1465 ret = usb_submit_urb(as->urb, GFP_ATOMIC);
1466 spin_unlock_irq(&ps->lock);
1468 ret = usb_submit_urb(as->urb, GFP_KERNEL);
1472 dev_printk(KERN_DEBUG, &ps->dev->dev,
1473 "usbfs: usb_submit_urb returned %d\n", ret);
1474 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1475 0, ret, COMPLETE, NULL, 0);
1476 async_removepending(as);
1489 static int proc_submiturb(struct dev_state *ps, void __user *arg)
1491 struct usbdevfs_urb uurb;
1493 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1496 return proc_do_submiturb(ps, &uurb,
1497 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1501 static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
1505 unsigned long flags;
1507 spin_lock_irqsave(&ps->lock, flags);
1508 as = async_getpending(ps, arg);
1510 spin_unlock_irqrestore(&ps->lock, flags);
1516 spin_unlock_irqrestore(&ps->lock, flags);
1524 static int processcompl(struct async *as, void __user * __user *arg)
1526 struct urb *urb = as->urb;
1527 struct usbdevfs_urb __user *userurb = as->userurb;
1528 void __user *addr = as->userurb;
1531 if (as->userbuffer && urb->actual_length) {
1532 if (copy_urb_data_to_user(as->userbuffer, urb))
1535 if (put_user(as->status, &userurb->status))
1537 if (put_user(urb->actual_length, &userurb->actual_length))
1539 if (put_user(urb->error_count, &userurb->error_count))
1542 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1543 for (i = 0; i < urb->number_of_packets; i++) {
1544 if (put_user(urb->iso_frame_desc[i].actual_length,
1545 &userurb->iso_frame_desc[i].actual_length))
1547 if (put_user(urb->iso_frame_desc[i].status,
1548 &userurb->iso_frame_desc[i].status))
1553 if (put_user(addr, (void __user * __user *)arg))
1561 static struct async *reap_as(struct dev_state *ps)
1563 DECLARE_WAITQUEUE(wait, current);
1564 struct async *as = NULL;
1565 struct usb_device *dev = ps->dev;
1567 add_wait_queue(&ps->wait, &wait);
1569 __set_current_state(TASK_INTERRUPTIBLE);
1570 as = async_getcompleted(ps);
1573 if (signal_pending(current))
1575 usb_unlock_device(dev);
1577 usb_lock_device(dev);
1579 remove_wait_queue(&ps->wait, &wait);
1580 set_current_state(TASK_RUNNING);
1584 static int proc_reapurb(struct dev_state *ps, void __user *arg)
1586 struct async *as = reap_as(ps);
1588 int retval = processcompl(as, (void __user * __user *)arg);
1592 if (signal_pending(current))
1597 static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
1602 as = async_getcompleted(ps);
1605 retval = processcompl(as, (void __user * __user *)arg);
1611 #ifdef CONFIG_COMPAT
1612 static int proc_control_compat(struct dev_state *ps,
1613 struct usbdevfs_ctrltransfer32 __user *p32)
1615 struct usbdevfs_ctrltransfer __user *p;
1617 p = compat_alloc_user_space(sizeof(*p));
1618 if (copy_in_user(p, p32, (sizeof(*p32) - sizeof(compat_caddr_t))) ||
1619 get_user(udata, &p32->data) ||
1620 put_user(compat_ptr(udata), &p->data))
1622 return proc_control(ps, p);
1625 static int proc_bulk_compat(struct dev_state *ps,
1626 struct usbdevfs_bulktransfer32 __user *p32)
1628 struct usbdevfs_bulktransfer __user *p;
1630 compat_caddr_t addr;
1632 p = compat_alloc_user_space(sizeof(*p));
1634 if (get_user(n, &p32->ep) || put_user(n, &p->ep) ||
1635 get_user(n, &p32->len) || put_user(n, &p->len) ||
1636 get_user(n, &p32->timeout) || put_user(n, &p->timeout) ||
1637 get_user(addr, &p32->data) || put_user(compat_ptr(addr), &p->data))
1640 return proc_bulk(ps, p);
1642 static int proc_disconnectsignal_compat(struct dev_state *ps, void __user *arg)
1644 struct usbdevfs_disconnectsignal32 ds;
1646 if (copy_from_user(&ds, arg, sizeof(ds)))
1648 ps->discsignr = ds.signr;
1649 ps->disccontext = compat_ptr(ds.context);
1653 static int get_urb32(struct usbdevfs_urb *kurb,
1654 struct usbdevfs_urb32 __user *uurb)
1657 if (!access_ok(VERIFY_READ, uurb, sizeof(*uurb)) ||
1658 __get_user(kurb->type, &uurb->type) ||
1659 __get_user(kurb->endpoint, &uurb->endpoint) ||
1660 __get_user(kurb->status, &uurb->status) ||
1661 __get_user(kurb->flags, &uurb->flags) ||
1662 __get_user(kurb->buffer_length, &uurb->buffer_length) ||
1663 __get_user(kurb->actual_length, &uurb->actual_length) ||
1664 __get_user(kurb->start_frame, &uurb->start_frame) ||
1665 __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
1666 __get_user(kurb->error_count, &uurb->error_count) ||
1667 __get_user(kurb->signr, &uurb->signr))
1670 if (__get_user(uptr, &uurb->buffer))
1672 kurb->buffer = compat_ptr(uptr);
1673 if (__get_user(uptr, &uurb->usercontext))
1675 kurb->usercontext = compat_ptr(uptr);
1680 static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
1682 struct usbdevfs_urb uurb;
1684 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
1687 return proc_do_submiturb(ps, &uurb,
1688 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
1692 static int processcompl_compat(struct async *as, void __user * __user *arg)
1694 struct urb *urb = as->urb;
1695 struct usbdevfs_urb32 __user *userurb = as->userurb;
1696 void __user *addr = as->userurb;
1699 if (as->userbuffer && urb->actual_length) {
1700 if (copy_urb_data_to_user(as->userbuffer, urb))
1703 if (put_user(as->status, &userurb->status))
1705 if (put_user(urb->actual_length, &userurb->actual_length))
1707 if (put_user(urb->error_count, &userurb->error_count))
1710 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1711 for (i = 0; i < urb->number_of_packets; i++) {
1712 if (put_user(urb->iso_frame_desc[i].actual_length,
1713 &userurb->iso_frame_desc[i].actual_length))
1715 if (put_user(urb->iso_frame_desc[i].status,
1716 &userurb->iso_frame_desc[i].status))
1721 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
1726 static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
1728 struct async *as = reap_as(ps);
1730 int retval = processcompl_compat(as, (void __user * __user *)arg);
1734 if (signal_pending(current))
1739 static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
1745 as = async_getcompleted(ps);
1747 retval = processcompl_compat(as, (void __user * __user *)arg);
1756 static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
1758 struct usbdevfs_disconnectsignal ds;
1760 if (copy_from_user(&ds, arg, sizeof(ds)))
1762 ps->discsignr = ds.signr;
1763 ps->disccontext = ds.context;
1767 static int proc_claiminterface(struct dev_state *ps, void __user *arg)
1771 if (get_user(ifnum, (unsigned int __user *)arg))
1773 return claimintf(ps, ifnum);
1776 static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
1781 if (get_user(ifnum, (unsigned int __user *)arg))
1783 if ((ret = releaseintf(ps, ifnum)) < 0)
1785 destroy_async_on_interface (ps, ifnum);
1789 static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
1794 struct usb_interface *intf = NULL;
1795 struct usb_driver *driver = NULL;
1798 if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
1799 if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
1801 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
1802 if (copy_from_user(buf, ctl->data, size)) {
1807 memset(buf, 0, size);
1811 if (!connected(ps)) {
1816 if (ps->dev->state != USB_STATE_CONFIGURED)
1817 retval = -EHOSTUNREACH;
1818 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
1820 else switch (ctl->ioctl_code) {
1822 /* disconnect kernel driver from interface */
1823 case USBDEVFS_DISCONNECT:
1824 if (intf->dev.driver) {
1825 driver = to_usb_driver(intf->dev.driver);
1826 dev_dbg(&intf->dev, "disconnect by usbfs\n");
1827 usb_driver_release_interface(driver, intf);
1832 /* let kernel drivers try to (re)bind to the interface */
1833 case USBDEVFS_CONNECT:
1834 if (!intf->dev.driver)
1835 retval = device_attach(&intf->dev);
1840 /* talk directly to the interface's driver */
1842 if (intf->dev.driver)
1843 driver = to_usb_driver(intf->dev.driver);
1844 if (driver == NULL || driver->unlocked_ioctl == NULL) {
1847 retval = driver->unlocked_ioctl(intf, ctl->ioctl_code, buf);
1848 if (retval == -ENOIOCTLCMD)
1853 /* cleanup and return */
1855 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
1857 && copy_to_user(ctl->data, buf, size) != 0)
1864 static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
1866 struct usbdevfs_ioctl ctrl;
1868 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1870 return proc_ioctl(ps, &ctrl);
1873 #ifdef CONFIG_COMPAT
1874 static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
1876 struct usbdevfs_ioctl32 __user *uioc;
1877 struct usbdevfs_ioctl ctrl;
1880 uioc = compat_ptr((long)arg);
1881 if (!access_ok(VERIFY_READ, uioc, sizeof(*uioc)) ||
1882 __get_user(ctrl.ifno, &uioc->ifno) ||
1883 __get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
1884 __get_user(udata, &uioc->data))
1886 ctrl.data = compat_ptr(udata);
1888 return proc_ioctl(ps, &ctrl);
1892 static int proc_claim_port(struct dev_state *ps, void __user *arg)
1897 if (get_user(portnum, (unsigned __user *) arg))
1899 rc = usb_hub_claim_port(ps->dev, portnum, ps);
1901 snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n",
1902 portnum, task_pid_nr(current), current->comm);
1906 static int proc_release_port(struct dev_state *ps, void __user *arg)
1910 if (get_user(portnum, (unsigned __user *) arg))
1912 return usb_hub_release_port(ps->dev, portnum, ps);
1915 static int proc_get_capabilities(struct dev_state *ps, void __user *arg)
1919 caps = USBDEVFS_CAP_ZERO_PACKET | USBDEVFS_CAP_NO_PACKET_SIZE_LIM;
1920 if (!ps->dev->bus->no_stop_on_short)
1921 caps |= USBDEVFS_CAP_BULK_CONTINUATION;
1922 if (ps->dev->bus->sg_tablesize)
1923 caps |= USBDEVFS_CAP_BULK_SCATTER_GATHER;
1925 if (put_user(caps, (__u32 __user *)arg))
1931 static int proc_disconnect_claim(struct dev_state *ps, void __user *arg)
1933 struct usbdevfs_disconnect_claim dc;
1934 struct usb_interface *intf;
1936 if (copy_from_user(&dc, arg, sizeof(dc)))
1939 intf = usb_ifnum_to_if(ps->dev, dc.interface);
1943 if (intf->dev.driver) {
1944 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
1946 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_IF_DRIVER) &&
1947 strncmp(dc.driver, intf->dev.driver->name,
1948 sizeof(dc.driver)) != 0)
1951 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_EXCEPT_DRIVER) &&
1952 strncmp(dc.driver, intf->dev.driver->name,
1953 sizeof(dc.driver)) == 0)
1956 dev_dbg(&intf->dev, "disconnect by usbfs\n");
1957 usb_driver_release_interface(driver, intf);
1960 return claimintf(ps, dc.interface);
1964 * NOTE: All requests here that have interface numbers as parameters
1965 * are assuming that somehow the configuration has been prevented from
1966 * changing. But there's no mechanism to ensure that...
1968 static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
1971 struct dev_state *ps = file->private_data;
1972 struct inode *inode = file->f_path.dentry->d_inode;
1973 struct usb_device *dev = ps->dev;
1976 if (!(file->f_mode & FMODE_WRITE))
1979 usb_lock_device(dev);
1980 if (!connected(ps)) {
1981 usb_unlock_device(dev);
1986 case USBDEVFS_CONTROL:
1987 snoop(&dev->dev, "%s: CONTROL\n", __func__);
1988 ret = proc_control(ps, p);
1990 inode->i_mtime = CURRENT_TIME;
1994 snoop(&dev->dev, "%s: BULK\n", __func__);
1995 ret = proc_bulk(ps, p);
1997 inode->i_mtime = CURRENT_TIME;
2000 case USBDEVFS_RESETEP:
2001 snoop(&dev->dev, "%s: RESETEP\n", __func__);
2002 ret = proc_resetep(ps, p);
2004 inode->i_mtime = CURRENT_TIME;
2007 case USBDEVFS_RESET:
2008 snoop(&dev->dev, "%s: RESET\n", __func__);
2009 ret = proc_resetdevice(ps);
2012 case USBDEVFS_CLEAR_HALT:
2013 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
2014 ret = proc_clearhalt(ps, p);
2016 inode->i_mtime = CURRENT_TIME;
2019 case USBDEVFS_GETDRIVER:
2020 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
2021 ret = proc_getdriver(ps, p);
2024 case USBDEVFS_CONNECTINFO:
2025 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
2026 ret = proc_connectinfo(ps, p);
2029 case USBDEVFS_SETINTERFACE:
2030 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
2031 ret = proc_setintf(ps, p);
2034 case USBDEVFS_SETCONFIGURATION:
2035 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
2036 ret = proc_setconfig(ps, p);
2039 case USBDEVFS_SUBMITURB:
2040 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
2041 ret = proc_submiturb(ps, p);
2043 inode->i_mtime = CURRENT_TIME;
2046 #ifdef CONFIG_COMPAT
2047 case USBDEVFS_CONTROL32:
2048 snoop(&dev->dev, "%s: CONTROL32\n", __func__);
2049 ret = proc_control_compat(ps, p);
2051 inode->i_mtime = CURRENT_TIME;
2054 case USBDEVFS_BULK32:
2055 snoop(&dev->dev, "%s: BULK32\n", __func__);
2056 ret = proc_bulk_compat(ps, p);
2058 inode->i_mtime = CURRENT_TIME;
2061 case USBDEVFS_DISCSIGNAL32:
2062 snoop(&dev->dev, "%s: DISCSIGNAL32\n", __func__);
2063 ret = proc_disconnectsignal_compat(ps, p);
2066 case USBDEVFS_SUBMITURB32:
2067 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
2068 ret = proc_submiturb_compat(ps, p);
2070 inode->i_mtime = CURRENT_TIME;
2073 case USBDEVFS_REAPURB32:
2074 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
2075 ret = proc_reapurb_compat(ps, p);
2078 case USBDEVFS_REAPURBNDELAY32:
2079 snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
2080 ret = proc_reapurbnonblock_compat(ps, p);
2083 case USBDEVFS_IOCTL32:
2084 snoop(&dev->dev, "%s: IOCTL32\n", __func__);
2085 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
2089 case USBDEVFS_DISCARDURB:
2090 snoop(&dev->dev, "%s: DISCARDURB\n", __func__);
2091 ret = proc_unlinkurb(ps, p);
2094 case USBDEVFS_REAPURB:
2095 snoop(&dev->dev, "%s: REAPURB\n", __func__);
2096 ret = proc_reapurb(ps, p);
2099 case USBDEVFS_REAPURBNDELAY:
2100 snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
2101 ret = proc_reapurbnonblock(ps, p);
2104 case USBDEVFS_DISCSIGNAL:
2105 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
2106 ret = proc_disconnectsignal(ps, p);
2109 case USBDEVFS_CLAIMINTERFACE:
2110 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
2111 ret = proc_claiminterface(ps, p);
2114 case USBDEVFS_RELEASEINTERFACE:
2115 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
2116 ret = proc_releaseinterface(ps, p);
2119 case USBDEVFS_IOCTL:
2120 snoop(&dev->dev, "%s: IOCTL\n", __func__);
2121 ret = proc_ioctl_default(ps, p);
2124 case USBDEVFS_CLAIM_PORT:
2125 snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__);
2126 ret = proc_claim_port(ps, p);
2129 case USBDEVFS_RELEASE_PORT:
2130 snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__);
2131 ret = proc_release_port(ps, p);
2133 case USBDEVFS_GET_CAPABILITIES:
2134 ret = proc_get_capabilities(ps, p);
2136 case USBDEVFS_DISCONNECT_CLAIM:
2137 ret = proc_disconnect_claim(ps, p);
2140 usb_unlock_device(dev);
2142 inode->i_atime = CURRENT_TIME;
2146 static long usbdev_ioctl(struct file *file, unsigned int cmd,
2151 ret = usbdev_do_ioctl(file, cmd, (void __user *)arg);
2156 #ifdef CONFIG_COMPAT
2157 static long usbdev_compat_ioctl(struct file *file, unsigned int cmd,
2162 ret = usbdev_do_ioctl(file, cmd, compat_ptr(arg));
2168 /* No kernel lock - fine */
2169 static unsigned int usbdev_poll(struct file *file,
2170 struct poll_table_struct *wait)
2172 struct dev_state *ps = file->private_data;
2173 unsigned int mask = 0;
2175 poll_wait(file, &ps->wait, wait);
2176 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
2177 mask |= POLLOUT | POLLWRNORM;
2179 mask |= POLLERR | POLLHUP;
2183 const struct file_operations usbdev_file_operations = {
2184 .owner = THIS_MODULE,
2185 .llseek = usbdev_lseek,
2186 .read = usbdev_read,
2187 .poll = usbdev_poll,
2188 .unlocked_ioctl = usbdev_ioctl,
2189 #ifdef CONFIG_COMPAT
2190 .compat_ioctl = usbdev_compat_ioctl,
2192 .open = usbdev_open,
2193 .release = usbdev_release,
2196 static void usbdev_remove(struct usb_device *udev)
2198 struct dev_state *ps;
2199 struct siginfo sinfo;
2201 while (!list_empty(&udev->filelist)) {
2202 ps = list_entry(udev->filelist.next, struct dev_state, list);
2203 destroy_all_async(ps);
2204 wake_up_all(&ps->wait);
2205 list_del_init(&ps->list);
2206 if (ps->discsignr) {
2207 sinfo.si_signo = ps->discsignr;
2208 sinfo.si_errno = EPIPE;
2209 sinfo.si_code = SI_ASYNCIO;
2210 sinfo.si_addr = ps->disccontext;
2211 kill_pid_info_as_cred(ps->discsignr, &sinfo,
2212 ps->disc_pid, ps->cred, ps->secid);
2217 static int usbdev_notify(struct notifier_block *self,
2218 unsigned long action, void *dev)
2221 case USB_DEVICE_ADD:
2223 case USB_DEVICE_REMOVE:
2230 static struct notifier_block usbdev_nb = {
2231 .notifier_call = usbdev_notify,
2234 static struct cdev usb_device_cdev;
2236 int __init usb_devio_init(void)
2240 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
2243 printk(KERN_ERR "Unable to register minors for usb_device\n");
2246 cdev_init(&usb_device_cdev, &usbdev_file_operations);
2247 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
2249 printk(KERN_ERR "Unable to get usb_device major %d\n",
2253 usb_register_notify(&usbdev_nb);
2258 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
2262 void usb_devio_cleanup(void)
2264 usb_unregister_notify(&usbdev_nb);
2265 cdev_del(&usb_device_cdev);
2266 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);