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/smp_lock.h>
41 #include <linux/signal.h>
42 #include <linux/poll.h>
43 #include <linux/module.h>
44 #include <linux/usb.h>
45 #include <linux/usbdevice_fs.h>
46 #include <linux/cdev.h>
47 #include <linux/notifier.h>
48 #include <linux/security.h>
49 #include <asm/uaccess.h>
50 #include <asm/byteorder.h>
51 #include <linux/moduleparam.h>
53 #include "hcd.h" /* for usbcore internals */
57 #define USB_DEVICE_MAX USB_MAXBUS * 128
59 /* Mutual exclusion for removal, open, and release */
60 DEFINE_MUTEX(usbfs_mutex);
63 struct list_head asynclist;
69 void __user *userbuffer;
76 static int usbfs_snoop;
77 module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
78 MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
80 #define snoop(dev, format, arg...) \
83 dev_info(dev , format , ## arg); \
86 #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
89 #define MAX_USBFS_BUFFER_SIZE 16384
91 static inline int connected(struct dev_state *ps)
93 return (!list_empty(&ps->list) &&
94 ps->dev->state != USB_STATE_NOTATTACHED);
97 static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
105 file->f_pos = offset;
109 file->f_pos += offset;
121 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
124 struct dev_state *ps = file->private_data;
125 struct usb_device *dev = ps->dev;
132 usb_lock_device(dev);
133 if (!connected(ps)) {
136 } else if (pos < 0) {
141 if (pos < sizeof(struct usb_device_descriptor)) {
142 /* 18 bytes - fits on the stack */
143 struct usb_device_descriptor temp_desc;
145 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
146 le16_to_cpus(&temp_desc.bcdUSB);
147 le16_to_cpus(&temp_desc.idVendor);
148 le16_to_cpus(&temp_desc.idProduct);
149 le16_to_cpus(&temp_desc.bcdDevice);
151 len = sizeof(struct usb_device_descriptor) - pos;
154 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
165 pos = sizeof(struct usb_device_descriptor);
166 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
167 struct usb_config_descriptor *config =
168 (struct usb_config_descriptor *)dev->rawdescriptors[i];
169 unsigned int length = le16_to_cpu(config->wTotalLength);
171 if (*ppos < pos + length) {
173 /* The descriptor may claim to be longer than it
174 * really is. Here is the actual allocated length. */
176 le16_to_cpu(dev->config[i].desc.wTotalLength);
178 len = length - (*ppos - pos);
182 /* Simply don't write (skip over) unallocated parts */
183 if (alloclen > (*ppos - pos)) {
184 alloclen -= (*ppos - pos);
185 if (copy_to_user(buf,
186 dev->rawdescriptors[i] + (*ppos - pos),
187 min(len, alloclen))) {
203 usb_unlock_device(dev);
208 * async list handling
211 static struct async *alloc_async(unsigned int numisoframes)
215 as = kzalloc(sizeof(struct async), GFP_KERNEL);
218 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
226 static void free_async(struct async *as)
229 kfree(as->urb->transfer_buffer);
230 kfree(as->urb->setup_packet);
231 usb_free_urb(as->urb);
235 static inline void async_newpending(struct async *as)
237 struct dev_state *ps = as->ps;
240 spin_lock_irqsave(&ps->lock, flags);
241 list_add_tail(&as->asynclist, &ps->async_pending);
242 spin_unlock_irqrestore(&ps->lock, flags);
245 static inline void async_removepending(struct async *as)
247 struct dev_state *ps = as->ps;
250 spin_lock_irqsave(&ps->lock, flags);
251 list_del_init(&as->asynclist);
252 spin_unlock_irqrestore(&ps->lock, flags);
255 static inline struct async *async_getcompleted(struct dev_state *ps)
258 struct async *as = NULL;
260 spin_lock_irqsave(&ps->lock, flags);
261 if (!list_empty(&ps->async_completed)) {
262 as = list_entry(ps->async_completed.next, struct async,
264 list_del_init(&as->asynclist);
266 spin_unlock_irqrestore(&ps->lock, flags);
270 static inline struct async *async_getpending(struct dev_state *ps,
271 void __user *userurb)
276 spin_lock_irqsave(&ps->lock, flags);
277 list_for_each_entry(as, &ps->async_pending, asynclist)
278 if (as->userurb == userurb) {
279 list_del_init(&as->asynclist);
280 spin_unlock_irqrestore(&ps->lock, flags);
283 spin_unlock_irqrestore(&ps->lock, flags);
287 static void snoop_urb(struct urb *urb, void __user *userurb)
290 unsigned char *data = urb->transfer_buffer;
295 dev_info(&urb->dev->dev, "direction=%s\n",
296 usb_urb_dir_in(urb) ? "IN" : "OUT");
297 dev_info(&urb->dev->dev, "userurb=%p\n", userurb);
298 dev_info(&urb->dev->dev, "transfer_buffer_length=%d\n",
299 urb->transfer_buffer_length);
300 dev_info(&urb->dev->dev, "actual_length=%d\n", urb->actual_length);
301 dev_info(&urb->dev->dev, "data: ");
302 for (j = 0; j < urb->transfer_buffer_length; ++j)
303 printk("%02x ", data[j]);
307 static void async_completed(struct urb *urb)
309 struct async *as = urb->context;
310 struct dev_state *ps = as->ps;
311 struct siginfo sinfo;
313 spin_lock(&ps->lock);
314 list_move_tail(&as->asynclist, &ps->async_completed);
315 spin_unlock(&ps->lock);
316 as->status = urb->status;
318 sinfo.si_signo = as->signr;
319 sinfo.si_errno = as->status;
320 sinfo.si_code = SI_ASYNCIO;
321 sinfo.si_addr = as->userurb;
322 kill_pid_info_as_uid(as->signr, &sinfo, as->pid, as->uid,
323 as->euid, as->secid);
325 snoop(&urb->dev->dev, "urb complete\n");
326 snoop_urb(urb, as->userurb);
330 static void destroy_async(struct dev_state *ps, struct list_head *list)
335 spin_lock_irqsave(&ps->lock, flags);
336 while (!list_empty(list)) {
337 as = list_entry(list->next, struct async, asynclist);
338 list_del_init(&as->asynclist);
340 /* drop the spinlock so the completion handler can run */
341 spin_unlock_irqrestore(&ps->lock, flags);
342 usb_kill_urb(as->urb);
343 spin_lock_irqsave(&ps->lock, flags);
345 spin_unlock_irqrestore(&ps->lock, flags);
346 as = async_getcompleted(ps);
349 as = async_getcompleted(ps);
353 static void destroy_async_on_interface(struct dev_state *ps,
356 struct list_head *p, *q, hitlist;
359 INIT_LIST_HEAD(&hitlist);
360 spin_lock_irqsave(&ps->lock, flags);
361 list_for_each_safe(p, q, &ps->async_pending)
362 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
363 list_move_tail(p, &hitlist);
364 spin_unlock_irqrestore(&ps->lock, flags);
365 destroy_async(ps, &hitlist);
368 static inline void destroy_all_async(struct dev_state *ps)
370 destroy_async(ps, &ps->async_pending);
374 * interface claims are made only at the request of user level code,
375 * which can also release them (explicitly or by closing files).
376 * they're also undone when devices disconnect.
379 static int driver_probe(struct usb_interface *intf,
380 const struct usb_device_id *id)
385 static void driver_disconnect(struct usb_interface *intf)
387 struct dev_state *ps = usb_get_intfdata(intf);
388 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
393 /* NOTE: this relies on usbcore having canceled and completed
394 * all pending I/O requests; 2.6 does that.
397 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
398 clear_bit(ifnum, &ps->ifclaimed);
400 warn("interface number %u out of range", ifnum);
402 usb_set_intfdata(intf, NULL);
404 /* force async requests to complete */
405 destroy_async_on_interface(ps, ifnum);
408 /* The following routines are merely placeholders. There is no way
409 * to inform a user task about suspend or resumes.
411 static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
416 static int driver_resume(struct usb_interface *intf)
421 struct usb_driver usbfs_driver = {
423 .probe = driver_probe,
424 .disconnect = driver_disconnect,
425 .suspend = driver_suspend,
426 .resume = driver_resume,
429 static int claimintf(struct dev_state *ps, unsigned int ifnum)
431 struct usb_device *dev = ps->dev;
432 struct usb_interface *intf;
435 if (ifnum >= 8*sizeof(ps->ifclaimed))
437 /* already claimed */
438 if (test_bit(ifnum, &ps->ifclaimed))
441 intf = usb_ifnum_to_if(dev, ifnum);
445 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
447 set_bit(ifnum, &ps->ifclaimed);
451 static int releaseintf(struct dev_state *ps, unsigned int ifnum)
453 struct usb_device *dev;
454 struct usb_interface *intf;
458 if (ifnum >= 8*sizeof(ps->ifclaimed))
461 intf = usb_ifnum_to_if(dev, ifnum);
464 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
465 usb_driver_release_interface(&usbfs_driver, intf);
471 static int checkintf(struct dev_state *ps, unsigned int ifnum)
473 if (ps->dev->state != USB_STATE_CONFIGURED)
474 return -EHOSTUNREACH;
475 if (ifnum >= 8*sizeof(ps->ifclaimed))
477 if (test_bit(ifnum, &ps->ifclaimed))
479 /* if not yet claimed, claim it for the driver */
480 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
481 "interface %u before use\n", task_pid_nr(current),
482 current->comm, ifnum);
483 return claimintf(ps, ifnum);
486 static int findintfep(struct usb_device *dev, unsigned int ep)
488 unsigned int i, j, e;
489 struct usb_interface *intf;
490 struct usb_host_interface *alts;
491 struct usb_endpoint_descriptor *endpt;
493 if (ep & ~(USB_DIR_IN|0xf))
497 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
498 intf = dev->actconfig->interface[i];
499 for (j = 0; j < intf->num_altsetting; j++) {
500 alts = &intf->altsetting[j];
501 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
502 endpt = &alts->endpoint[e].desc;
503 if (endpt->bEndpointAddress == ep)
504 return alts->desc.bInterfaceNumber;
511 static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
516 if (ps->dev->state != USB_STATE_ADDRESS
517 && ps->dev->state != USB_STATE_CONFIGURED)
518 return -EHOSTUNREACH;
519 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
523 switch (requesttype & USB_RECIP_MASK) {
524 case USB_RECIP_ENDPOINT:
525 ret = findintfep(ps->dev, index);
527 ret = checkintf(ps, ret);
530 case USB_RECIP_INTERFACE:
531 ret = checkintf(ps, index);
537 static int __match_minor(struct device *dev, void *data)
539 int minor = *((int *)data);
541 if (dev->devt == MKDEV(USB_DEVICE_MAJOR, minor))
546 static struct usb_device *usbdev_lookup_by_minor(int minor)
550 dev = bus_find_device(&usb_bus_type, NULL, &minor, __match_minor);
554 return container_of(dev, struct usb_device, dev);
560 static int usbdev_open(struct inode *inode, struct file *file)
562 struct usb_device *dev = NULL;
563 struct dev_state *ps;
567 /* Protect against simultaneous removal or release */
568 mutex_lock(&usbfs_mutex);
571 ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
576 /* usbdev device-node */
577 if (imajor(inode) == USB_DEVICE_MAJOR)
578 dev = usbdev_lookup_by_minor(iminor(inode));
579 #ifdef CONFIG_USB_DEVICEFS
582 dev = inode->i_private;
586 ret = usb_autoresume_device(dev);
594 spin_lock_init(&ps->lock);
595 INIT_LIST_HEAD(&ps->list);
596 INIT_LIST_HEAD(&ps->async_pending);
597 INIT_LIST_HEAD(&ps->async_completed);
598 init_waitqueue_head(&ps->wait);
600 ps->disc_pid = get_pid(task_pid(current));
601 ps->disc_uid = current->uid;
602 ps->disc_euid = current->euid;
603 ps->disccontext = NULL;
605 security_task_getsecid(current, &ps->secid);
607 list_add_tail(&ps->list, &dev->filelist);
608 file->private_data = ps;
612 mutex_unlock(&usbfs_mutex);
617 static int usbdev_release(struct inode *inode, struct file *file)
619 struct dev_state *ps = file->private_data;
620 struct usb_device *dev = ps->dev;
623 usb_lock_device(dev);
625 /* Protect against simultaneous open */
626 mutex_lock(&usbfs_mutex);
627 list_del_init(&ps->list);
628 mutex_unlock(&usbfs_mutex);
630 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
632 if (test_bit(ifnum, &ps->ifclaimed))
633 releaseintf(ps, ifnum);
635 destroy_all_async(ps);
636 usb_autosuspend_device(dev);
637 usb_unlock_device(dev);
639 put_pid(ps->disc_pid);
644 static int proc_control(struct dev_state *ps, void __user *arg)
646 struct usb_device *dev = ps->dev;
647 struct usbdevfs_ctrltransfer ctrl;
653 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
655 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex);
658 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
659 if (wLength > PAGE_SIZE)
661 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
665 if (ctrl.bRequestType & 0x80) {
666 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
668 free_page((unsigned long)tbuf);
671 snoop(&dev->dev, "control read: bRequest=%02x "
672 "bRrequestType=%02x wValue=%04x "
673 "wIndex=%04x wLength=%04x\n",
674 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
675 ctrl.wIndex, ctrl.wLength);
677 usb_unlock_device(dev);
678 i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest,
679 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
680 tbuf, ctrl.wLength, tmo);
681 usb_lock_device(dev);
682 if ((i > 0) && ctrl.wLength) {
684 dev_info(&dev->dev, "control read: data ");
685 for (j = 0; j < i; ++j)
686 printk("%02x ", (u8)(tbuf)[j]);
689 if (copy_to_user(ctrl.data, tbuf, i)) {
690 free_page((unsigned long)tbuf);
696 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
697 free_page((unsigned long)tbuf);
701 snoop(&dev->dev, "control write: bRequest=%02x "
702 "bRrequestType=%02x wValue=%04x "
703 "wIndex=%04x wLength=%04x\n",
704 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
705 ctrl.wIndex, ctrl.wLength);
707 dev_info(&dev->dev, "control write: data: ");
708 for (j = 0; j < ctrl.wLength; ++j)
709 printk("%02x ", (unsigned char)(tbuf)[j]);
712 usb_unlock_device(dev);
713 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
714 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
715 tbuf, ctrl.wLength, tmo);
716 usb_lock_device(dev);
718 free_page((unsigned long)tbuf);
719 if (i < 0 && i != -EPIPE) {
720 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
721 "failed cmd %s rqt %u rq %u len %u ret %d\n",
722 current->comm, ctrl.bRequestType, ctrl.bRequest,
728 static int proc_bulk(struct dev_state *ps, void __user *arg)
730 struct usb_device *dev = ps->dev;
731 struct usbdevfs_bulktransfer bulk;
732 unsigned int tmo, len1, pipe;
737 if (copy_from_user(&bulk, arg, sizeof(bulk)))
739 ret = findintfep(ps->dev, bulk.ep);
742 ret = checkintf(ps, ret);
745 if (bulk.ep & USB_DIR_IN)
746 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
748 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
749 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
752 if (len1 > MAX_USBFS_BUFFER_SIZE)
754 if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
757 if (bulk.ep & 0x80) {
758 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
762 snoop(&dev->dev, "bulk read: len=0x%02x timeout=%04d\n",
763 bulk.len, bulk.timeout);
764 usb_unlock_device(dev);
765 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
766 usb_lock_device(dev);
769 dev_info(&dev->dev, "bulk read: data ");
770 for (j = 0; j < len2; ++j)
771 printk("%02x ", (u8)(tbuf)[j]);
774 if (copy_to_user(bulk.data, tbuf, len2)) {
781 if (copy_from_user(tbuf, bulk.data, len1)) {
786 snoop(&dev->dev, "bulk write: len=0x%02x timeout=%04d\n",
787 bulk.len, bulk.timeout);
789 dev_info(&dev->dev, "bulk write: data: ");
790 for (j = 0; j < len1; ++j)
791 printk("%02x ", (unsigned char)(tbuf)[j]);
794 usb_unlock_device(dev);
795 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
796 usb_lock_device(dev);
804 static int proc_resetep(struct dev_state *ps, void __user *arg)
809 if (get_user(ep, (unsigned int __user *)arg))
811 ret = findintfep(ps->dev, ep);
814 ret = checkintf(ps, ret);
817 usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0);
821 static int proc_clearhalt(struct dev_state *ps, void __user *arg)
827 if (get_user(ep, (unsigned int __user *)arg))
829 ret = findintfep(ps->dev, ep);
832 ret = checkintf(ps, ret);
836 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
838 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
840 return usb_clear_halt(ps->dev, pipe);
843 static int proc_getdriver(struct dev_state *ps, void __user *arg)
845 struct usbdevfs_getdriver gd;
846 struct usb_interface *intf;
849 if (copy_from_user(&gd, arg, sizeof(gd)))
851 intf = usb_ifnum_to_if(ps->dev, gd.interface);
852 if (!intf || !intf->dev.driver)
855 strncpy(gd.driver, intf->dev.driver->name,
857 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
862 static int proc_connectinfo(struct dev_state *ps, void __user *arg)
864 struct usbdevfs_connectinfo ci;
866 ci.devnum = ps->dev->devnum;
867 ci.slow = ps->dev->speed == USB_SPEED_LOW;
868 if (copy_to_user(arg, &ci, sizeof(ci)))
873 static int proc_resetdevice(struct dev_state *ps)
875 return usb_reset_composite_device(ps->dev);
878 static int proc_setintf(struct dev_state *ps, void __user *arg)
880 struct usbdevfs_setinterface setintf;
883 if (copy_from_user(&setintf, arg, sizeof(setintf)))
885 if ((ret = checkintf(ps, setintf.interface)))
887 return usb_set_interface(ps->dev, setintf.interface,
891 static int proc_setconfig(struct dev_state *ps, void __user *arg)
895 struct usb_host_config *actconfig;
897 if (get_user(u, (int __user *)arg))
900 actconfig = ps->dev->actconfig;
902 /* Don't touch the device if any interfaces are claimed.
903 * It could interfere with other drivers' operations, and if
904 * an interface is claimed by usbfs it could easily deadlock.
909 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
910 if (usb_interface_claimed(actconfig->interface[i])) {
911 dev_warn(&ps->dev->dev,
912 "usbfs: interface %d claimed by %s "
913 "while '%s' sets config #%d\n",
914 actconfig->interface[i]
916 ->desc.bInterfaceNumber,
917 actconfig->interface[i]
926 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
927 * so avoid usb_set_configuration()'s kick to sysfs
930 if (actconfig && actconfig->desc.bConfigurationValue == u)
931 status = usb_reset_configuration(ps->dev);
933 status = usb_set_configuration(ps->dev, u);
939 static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
940 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
943 struct usbdevfs_iso_packet_desc *isopkt = NULL;
944 struct usb_host_endpoint *ep;
946 struct usb_ctrlrequest *dr = NULL;
947 unsigned int u, totlen, isofrmlen;
951 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
952 USBDEVFS_URB_SHORT_NOT_OK |
953 USBDEVFS_URB_NO_FSBR |
954 USBDEVFS_URB_ZERO_PACKET |
955 USBDEVFS_URB_NO_INTERRUPT))
959 if (uurb->signr != 0 && (uurb->signr < SIGRTMIN ||
960 uurb->signr > SIGRTMAX))
962 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
963 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
964 ifnum = findintfep(ps->dev, uurb->endpoint);
967 ret = checkintf(ps, ifnum);
971 if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
973 ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
976 ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
981 case USBDEVFS_URB_TYPE_CONTROL:
982 if (!usb_endpoint_xfer_control(&ep->desc))
984 /* min 8 byte setup packet,
985 * max 8 byte setup plus an arbitrary data stage */
986 if (uurb->buffer_length < 8 ||
987 uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE))
989 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
992 if (copy_from_user(dr, uurb->buffer, 8)) {
996 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
1000 ret = check_ctrlrecip(ps, dr->bRequestType,
1001 le16_to_cpup(&dr->wIndex));
1006 uurb->number_of_packets = 0;
1007 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1009 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1011 uurb->endpoint |= USB_DIR_IN;
1014 uurb->endpoint &= ~USB_DIR_IN;
1016 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1017 uurb->buffer, uurb->buffer_length)) {
1021 snoop(&ps->dev->dev, "control urb: bRequest=%02x "
1022 "bRrequestType=%02x wValue=%04x "
1023 "wIndex=%04x wLength=%04x\n",
1024 dr->bRequest, dr->bRequestType,
1025 __le16_to_cpup(&dr->wValue),
1026 __le16_to_cpup(&dr->wIndex),
1027 __le16_to_cpup(&dr->wLength));
1030 case USBDEVFS_URB_TYPE_BULK:
1031 switch (usb_endpoint_type(&ep->desc)) {
1032 case USB_ENDPOINT_XFER_CONTROL:
1033 case USB_ENDPOINT_XFER_ISOC:
1035 /* allow single-shot interrupt transfers, at bogus rates */
1037 uurb->number_of_packets = 0;
1038 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1040 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1041 uurb->buffer, uurb->buffer_length))
1043 snoop(&ps->dev->dev, "bulk urb\n");
1046 case USBDEVFS_URB_TYPE_ISO:
1047 /* arbitrary limit */
1048 if (uurb->number_of_packets < 1 ||
1049 uurb->number_of_packets > 128)
1051 if (!usb_endpoint_xfer_isoc(&ep->desc))
1053 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1054 uurb->number_of_packets;
1055 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
1057 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
1061 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1063 * sufficient for USB 2.0 high-bandwidth iso */
1064 if (isopkt[u].length > 8192) {
1068 totlen += isopkt[u].length;
1070 if (totlen > 32768) {
1074 uurb->buffer_length = totlen;
1075 snoop(&ps->dev->dev, "iso urb\n");
1078 case USBDEVFS_URB_TYPE_INTERRUPT:
1079 uurb->number_of_packets = 0;
1080 if (!usb_endpoint_xfer_int(&ep->desc))
1082 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1084 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1085 uurb->buffer, uurb->buffer_length))
1087 snoop(&ps->dev->dev, "interrupt urb\n");
1093 as = alloc_async(uurb->number_of_packets);
1099 as->urb->transfer_buffer = kmalloc(uurb->buffer_length, GFP_KERNEL);
1100 if (!as->urb->transfer_buffer) {
1106 as->urb->dev = ps->dev;
1107 as->urb->pipe = (uurb->type << 30) |
1108 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1109 (uurb->endpoint & USB_DIR_IN);
1111 /* This tedious sequence is necessary because the URB_* flags
1112 * are internal to the kernel and subject to change, whereas
1113 * the USBDEVFS_URB_* flags are a user API and must not be changed.
1115 u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
1116 if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
1118 if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
1119 u |= URB_SHORT_NOT_OK;
1120 if (uurb->flags & USBDEVFS_URB_NO_FSBR)
1122 if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1123 u |= URB_ZERO_PACKET;
1124 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
1125 u |= URB_NO_INTERRUPT;
1126 as->urb->transfer_flags = u;
1128 as->urb->transfer_buffer_length = uurb->buffer_length;
1129 as->urb->setup_packet = (unsigned char *)dr;
1130 as->urb->start_frame = uurb->start_frame;
1131 as->urb->number_of_packets = uurb->number_of_packets;
1132 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1133 ps->dev->speed == USB_SPEED_HIGH)
1134 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
1136 as->urb->interval = ep->desc.bInterval;
1137 as->urb->context = as;
1138 as->urb->complete = async_completed;
1139 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1140 as->urb->iso_frame_desc[u].offset = totlen;
1141 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1142 totlen += isopkt[u].length;
1147 if (uurb->endpoint & USB_DIR_IN)
1148 as->userbuffer = uurb->buffer;
1150 as->userbuffer = NULL;
1151 as->signr = uurb->signr;
1153 as->pid = get_pid(task_pid(current));
1154 as->uid = current->uid;
1155 as->euid = current->euid;
1156 security_task_getsecid(current, &as->secid);
1158 if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
1159 as->urb->transfer_buffer_length)) {
1164 snoop_urb(as->urb, as->userurb);
1165 async_newpending(as);
1166 if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
1167 dev_printk(KERN_DEBUG, &ps->dev->dev,
1168 "usbfs: usb_submit_urb returned %d\n", ret);
1169 async_removepending(as);
1176 static int proc_submiturb(struct dev_state *ps, void __user *arg)
1178 struct usbdevfs_urb uurb;
1180 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1183 return proc_do_submiturb(ps, &uurb,
1184 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1188 static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
1192 as = async_getpending(ps, arg);
1195 usb_kill_urb(as->urb);
1199 static int processcompl(struct async *as, void __user * __user *arg)
1201 struct urb *urb = as->urb;
1202 struct usbdevfs_urb __user *userurb = as->userurb;
1203 void __user *addr = as->userurb;
1207 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1208 urb->transfer_buffer_length))
1210 if (put_user(as->status, &userurb->status))
1212 if (put_user(urb->actual_length, &userurb->actual_length))
1214 if (put_user(urb->error_count, &userurb->error_count))
1217 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1218 for (i = 0; i < urb->number_of_packets; i++) {
1219 if (put_user(urb->iso_frame_desc[i].actual_length,
1220 &userurb->iso_frame_desc[i].actual_length))
1222 if (put_user(urb->iso_frame_desc[i].status,
1223 &userurb->iso_frame_desc[i].status))
1230 if (put_user(addr, (void __user * __user *)arg))
1235 static struct async *reap_as(struct dev_state *ps)
1237 DECLARE_WAITQUEUE(wait, current);
1238 struct async *as = NULL;
1239 struct usb_device *dev = ps->dev;
1241 add_wait_queue(&ps->wait, &wait);
1243 __set_current_state(TASK_INTERRUPTIBLE);
1244 as = async_getcompleted(ps);
1247 if (signal_pending(current))
1249 usb_unlock_device(dev);
1251 usb_lock_device(dev);
1253 remove_wait_queue(&ps->wait, &wait);
1254 set_current_state(TASK_RUNNING);
1258 static int proc_reapurb(struct dev_state *ps, void __user *arg)
1260 struct async *as = reap_as(ps);
1262 return processcompl(as, (void __user * __user *)arg);
1263 if (signal_pending(current))
1268 static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
1272 if (!(as = async_getcompleted(ps)))
1274 return processcompl(as, (void __user * __user *)arg);
1277 #ifdef CONFIG_COMPAT
1279 static int get_urb32(struct usbdevfs_urb *kurb,
1280 struct usbdevfs_urb32 __user *uurb)
1283 if (get_user(kurb->type, &uurb->type) ||
1284 __get_user(kurb->endpoint, &uurb->endpoint) ||
1285 __get_user(kurb->status, &uurb->status) ||
1286 __get_user(kurb->flags, &uurb->flags) ||
1287 __get_user(kurb->buffer_length, &uurb->buffer_length) ||
1288 __get_user(kurb->actual_length, &uurb->actual_length) ||
1289 __get_user(kurb->start_frame, &uurb->start_frame) ||
1290 __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
1291 __get_user(kurb->error_count, &uurb->error_count) ||
1292 __get_user(kurb->signr, &uurb->signr))
1295 if (__get_user(uptr, &uurb->buffer))
1297 kurb->buffer = compat_ptr(uptr);
1298 if (__get_user(uptr, &uurb->buffer))
1300 kurb->usercontext = compat_ptr(uptr);
1305 static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
1307 struct usbdevfs_urb uurb;
1309 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
1312 return proc_do_submiturb(ps, &uurb,
1313 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
1317 static int processcompl_compat(struct async *as, void __user * __user *arg)
1319 struct urb *urb = as->urb;
1320 struct usbdevfs_urb32 __user *userurb = as->userurb;
1321 void __user *addr = as->userurb;
1325 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1326 urb->transfer_buffer_length))
1328 if (put_user(as->status, &userurb->status))
1330 if (put_user(urb->actual_length, &userurb->actual_length))
1332 if (put_user(urb->error_count, &userurb->error_count))
1335 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1336 for (i = 0; i < urb->number_of_packets; i++) {
1337 if (put_user(urb->iso_frame_desc[i].actual_length,
1338 &userurb->iso_frame_desc[i].actual_length))
1340 if (put_user(urb->iso_frame_desc[i].status,
1341 &userurb->iso_frame_desc[i].status))
1347 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
1352 static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
1354 struct async *as = reap_as(ps);
1356 return processcompl_compat(as, (void __user * __user *)arg);
1357 if (signal_pending(current))
1362 static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
1366 if (!(as = async_getcompleted(ps)))
1368 return processcompl_compat(as, (void __user * __user *)arg);
1373 static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
1375 struct usbdevfs_disconnectsignal ds;
1377 if (copy_from_user(&ds, arg, sizeof(ds)))
1379 if (ds.signr != 0 && (ds.signr < SIGRTMIN || ds.signr > SIGRTMAX))
1381 ps->discsignr = ds.signr;
1382 ps->disccontext = ds.context;
1386 static int proc_claiminterface(struct dev_state *ps, void __user *arg)
1390 if (get_user(ifnum, (unsigned int __user *)arg))
1392 return claimintf(ps, ifnum);
1395 static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
1400 if (get_user(ifnum, (unsigned int __user *)arg))
1402 if ((ret = releaseintf(ps, ifnum)) < 0)
1404 destroy_async_on_interface (ps, ifnum);
1408 static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
1413 struct usb_interface *intf = NULL;
1414 struct usb_driver *driver = NULL;
1417 if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
1418 if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
1420 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
1421 if (copy_from_user(buf, ctl->data, size)) {
1426 memset(buf, 0, size);
1430 if (!connected(ps)) {
1435 if (ps->dev->state != USB_STATE_CONFIGURED)
1436 retval = -EHOSTUNREACH;
1437 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
1439 else switch (ctl->ioctl_code) {
1441 /* disconnect kernel driver from interface */
1442 case USBDEVFS_DISCONNECT:
1443 if (intf->dev.driver) {
1444 driver = to_usb_driver(intf->dev.driver);
1445 dev_dbg(&intf->dev, "disconnect by usbfs\n");
1446 usb_driver_release_interface(driver, intf);
1451 /* let kernel drivers try to (re)bind to the interface */
1452 case USBDEVFS_CONNECT:
1453 if (!intf->dev.driver)
1454 retval = device_attach(&intf->dev);
1459 /* talk directly to the interface's driver */
1461 if (intf->dev.driver)
1462 driver = to_usb_driver(intf->dev.driver);
1463 if (driver == NULL || driver->ioctl == NULL) {
1466 retval = driver->ioctl(intf, ctl->ioctl_code, buf);
1467 if (retval == -ENOIOCTLCMD)
1472 /* cleanup and return */
1474 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
1476 && copy_to_user(ctl->data, buf, size) != 0)
1483 static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
1485 struct usbdevfs_ioctl ctrl;
1487 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1489 return proc_ioctl(ps, &ctrl);
1492 #ifdef CONFIG_COMPAT
1493 static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
1495 struct usbdevfs_ioctl32 __user *uioc;
1496 struct usbdevfs_ioctl ctrl;
1499 uioc = compat_ptr((long)arg);
1500 if (get_user(ctrl.ifno, &uioc->ifno) ||
1501 get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
1502 __get_user(udata, &uioc->data))
1504 ctrl.data = compat_ptr(udata);
1506 return proc_ioctl(ps, &ctrl);
1511 * NOTE: All requests here that have interface numbers as parameters
1512 * are assuming that somehow the configuration has been prevented from
1513 * changing. But there's no mechanism to ensure that...
1515 static int usbdev_ioctl(struct inode *inode, struct file *file,
1516 unsigned int cmd, unsigned long arg)
1518 struct dev_state *ps = file->private_data;
1519 struct usb_device *dev = ps->dev;
1520 void __user *p = (void __user *)arg;
1523 if (!(file->f_mode & FMODE_WRITE))
1525 usb_lock_device(dev);
1526 if (!connected(ps)) {
1527 usb_unlock_device(dev);
1532 case USBDEVFS_CONTROL:
1533 snoop(&dev->dev, "%s: CONTROL\n", __func__);
1534 ret = proc_control(ps, p);
1536 inode->i_mtime = CURRENT_TIME;
1540 snoop(&dev->dev, "%s: BULK\n", __func__);
1541 ret = proc_bulk(ps, p);
1543 inode->i_mtime = CURRENT_TIME;
1546 case USBDEVFS_RESETEP:
1547 snoop(&dev->dev, "%s: RESETEP\n", __func__);
1548 ret = proc_resetep(ps, p);
1550 inode->i_mtime = CURRENT_TIME;
1553 case USBDEVFS_RESET:
1554 snoop(&dev->dev, "%s: RESET\n", __func__);
1555 ret = proc_resetdevice(ps);
1558 case USBDEVFS_CLEAR_HALT:
1559 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
1560 ret = proc_clearhalt(ps, p);
1562 inode->i_mtime = CURRENT_TIME;
1565 case USBDEVFS_GETDRIVER:
1566 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
1567 ret = proc_getdriver(ps, p);
1570 case USBDEVFS_CONNECTINFO:
1571 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
1572 ret = proc_connectinfo(ps, p);
1575 case USBDEVFS_SETINTERFACE:
1576 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
1577 ret = proc_setintf(ps, p);
1580 case USBDEVFS_SETCONFIGURATION:
1581 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
1582 ret = proc_setconfig(ps, p);
1585 case USBDEVFS_SUBMITURB:
1586 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
1587 ret = proc_submiturb(ps, p);
1589 inode->i_mtime = CURRENT_TIME;
1592 #ifdef CONFIG_COMPAT
1594 case USBDEVFS_SUBMITURB32:
1595 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
1596 ret = proc_submiturb_compat(ps, p);
1598 inode->i_mtime = CURRENT_TIME;
1601 case USBDEVFS_REAPURB32:
1602 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
1603 ret = proc_reapurb_compat(ps, p);
1606 case USBDEVFS_REAPURBNDELAY32:
1607 snoop(&dev->dev, "%s: REAPURBDELAY32\n", __func__);
1608 ret = proc_reapurbnonblock_compat(ps, p);
1611 case USBDEVFS_IOCTL32:
1612 snoop(&dev->dev, "%s: IOCTL\n", __func__);
1613 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
1617 case USBDEVFS_DISCARDURB:
1618 snoop(&dev->dev, "%s: DISCARDURB\n", __func__);
1619 ret = proc_unlinkurb(ps, p);
1622 case USBDEVFS_REAPURB:
1623 snoop(&dev->dev, "%s: REAPURB\n", __func__);
1624 ret = proc_reapurb(ps, p);
1627 case USBDEVFS_REAPURBNDELAY:
1628 snoop(&dev->dev, "%s: REAPURBDELAY\n", __func__);
1629 ret = proc_reapurbnonblock(ps, p);
1632 case USBDEVFS_DISCSIGNAL:
1633 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
1634 ret = proc_disconnectsignal(ps, p);
1637 case USBDEVFS_CLAIMINTERFACE:
1638 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
1639 ret = proc_claiminterface(ps, p);
1642 case USBDEVFS_RELEASEINTERFACE:
1643 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
1644 ret = proc_releaseinterface(ps, p);
1647 case USBDEVFS_IOCTL:
1648 snoop(&dev->dev, "%s: IOCTL\n", __func__);
1649 ret = proc_ioctl_default(ps, p);
1652 usb_unlock_device(dev);
1654 inode->i_atime = CURRENT_TIME;
1658 /* No kernel lock - fine */
1659 static unsigned int usbdev_poll(struct file *file,
1660 struct poll_table_struct *wait)
1662 struct dev_state *ps = file->private_data;
1663 unsigned int mask = 0;
1665 poll_wait(file, &ps->wait, wait);
1666 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
1667 mask |= POLLOUT | POLLWRNORM;
1669 mask |= POLLERR | POLLHUP;
1673 const struct file_operations usbdev_file_operations = {
1674 .owner = THIS_MODULE,
1675 .llseek = usbdev_lseek,
1676 .read = usbdev_read,
1677 .poll = usbdev_poll,
1678 .ioctl = usbdev_ioctl,
1679 .open = usbdev_open,
1680 .release = usbdev_release,
1683 #ifdef CONFIG_USB_DEVICE_CLASS
1684 static struct class *usb_classdev_class;
1686 static int usb_classdev_add(struct usb_device *dev)
1688 int minor = ((dev->bus->busnum-1) * 128) + (dev->devnum-1);
1690 dev->usb_classdev = device_create(usb_classdev_class, &dev->dev,
1691 MKDEV(USB_DEVICE_MAJOR, minor),
1692 "usbdev%d.%d", dev->bus->busnum, dev->devnum);
1693 if (IS_ERR(dev->usb_classdev))
1694 return PTR_ERR(dev->usb_classdev);
1699 static void usb_classdev_remove(struct usb_device *dev)
1701 device_unregister(dev->usb_classdev);
1704 static int usb_classdev_notify(struct notifier_block *self,
1705 unsigned long action, void *dev)
1708 case USB_DEVICE_ADD:
1709 if (usb_classdev_add(dev))
1712 case USB_DEVICE_REMOVE:
1713 usb_classdev_remove(dev);
1719 static struct notifier_block usbdev_nb = {
1720 .notifier_call = usb_classdev_notify,
1724 static struct cdev usb_device_cdev;
1726 int __init usb_devio_init(void)
1730 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
1733 err("unable to register minors for usb_device");
1736 cdev_init(&usb_device_cdev, &usbdev_file_operations);
1737 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
1739 err("unable to get usb_device major %d", USB_DEVICE_MAJOR);
1742 #ifdef CONFIG_USB_DEVICE_CLASS
1743 usb_classdev_class = class_create(THIS_MODULE, "usb_device");
1744 if (IS_ERR(usb_classdev_class)) {
1745 err("unable to register usb_device class");
1746 retval = PTR_ERR(usb_classdev_class);
1747 cdev_del(&usb_device_cdev);
1748 usb_classdev_class = NULL;
1752 usb_register_notify(&usbdev_nb);
1758 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
1762 void usb_devio_cleanup(void)
1764 #ifdef CONFIG_USB_DEVICE_CLASS
1765 usb_unregister_notify(&usbdev_nb);
1766 class_destroy(usb_classdev_class);
1768 cdev_del(&usb_device_cdev);
1769 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);