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 * $Id: devio.c,v 1.7 2000/02/01 17:28:48 fliegl Exp $
24 * This file implements the usbfs/x/y files, where
25 * x is the bus number and y the device number.
27 * It allows user space programs/"drivers" to communicate directly
28 * with USB devices without intervening kernel driver.
31 * 22.12.1999 0.1 Initial release (split from proc_usb.c)
32 * 04.01.2000 0.2 Turned into its own filesystem
33 * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery
37 /*****************************************************************************/
41 #include <linux/slab.h>
42 #include <linux/smp_lock.h>
43 #include <linux/signal.h>
44 #include <linux/poll.h>
45 #include <linux/module.h>
46 #include <linux/usb.h>
47 #include <linux/usbdevice_fs.h>
48 #include <linux/cdev.h>
49 #include <linux/notifier.h>
50 #include <linux/security.h>
51 #include <asm/uaccess.h>
52 #include <asm/byteorder.h>
53 #include <linux/moduleparam.h>
55 #include "hcd.h" /* for usbcore internals */
59 #define USB_DEVICE_MAX USB_MAXBUS * 128
61 /* Mutual exclusion for removal, open, and release */
62 DEFINE_MUTEX(usbfs_mutex);
65 struct list_head asynclist;
71 void __user *userbuffer;
78 static int usbfs_snoop;
79 module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
80 MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
82 #define snoop(dev, format, arg...) \
85 dev_info(dev , format , ## arg); \
88 #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
91 #define MAX_USBFS_BUFFER_SIZE 16384
93 static inline int connected(struct dev_state *ps)
95 return (!list_empty(&ps->list) &&
96 ps->dev->state != USB_STATE_NOTATTACHED);
99 static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
107 file->f_pos = offset;
111 file->f_pos += offset;
123 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
126 struct dev_state *ps = file->private_data;
127 struct usb_device *dev = ps->dev;
134 usb_lock_device(dev);
135 if (!connected(ps)) {
138 } else if (pos < 0) {
143 if (pos < sizeof(struct usb_device_descriptor)) {
144 /* 18 bytes - fits on the stack */
145 struct usb_device_descriptor temp_desc;
147 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
148 le16_to_cpus(&temp_desc.bcdUSB);
149 le16_to_cpus(&temp_desc.idVendor);
150 le16_to_cpus(&temp_desc.idProduct);
151 le16_to_cpus(&temp_desc.bcdDevice);
153 len = sizeof(struct usb_device_descriptor) - pos;
156 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
167 pos = sizeof(struct usb_device_descriptor);
168 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
169 struct usb_config_descriptor *config =
170 (struct usb_config_descriptor *)dev->rawdescriptors[i];
171 unsigned int length = le16_to_cpu(config->wTotalLength);
173 if (*ppos < pos + length) {
175 /* The descriptor may claim to be longer than it
176 * really is. Here is the actual allocated length. */
178 le16_to_cpu(dev->config[i].desc.wTotalLength);
180 len = length - (*ppos - pos);
184 /* Simply don't write (skip over) unallocated parts */
185 if (alloclen > (*ppos - pos)) {
186 alloclen -= (*ppos - pos);
187 if (copy_to_user(buf,
188 dev->rawdescriptors[i] + (*ppos - pos),
189 min(len, alloclen))) {
205 usb_unlock_device(dev);
210 * async list handling
213 static struct async *alloc_async(unsigned int numisoframes)
217 as = kzalloc(sizeof(struct async), GFP_KERNEL);
220 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
228 static void free_async(struct async *as)
231 kfree(as->urb->transfer_buffer);
232 kfree(as->urb->setup_packet);
233 usb_free_urb(as->urb);
237 static inline void async_newpending(struct async *as)
239 struct dev_state *ps = as->ps;
242 spin_lock_irqsave(&ps->lock, flags);
243 list_add_tail(&as->asynclist, &ps->async_pending);
244 spin_unlock_irqrestore(&ps->lock, flags);
247 static inline void async_removepending(struct async *as)
249 struct dev_state *ps = as->ps;
252 spin_lock_irqsave(&ps->lock, flags);
253 list_del_init(&as->asynclist);
254 spin_unlock_irqrestore(&ps->lock, flags);
257 static inline struct async *async_getcompleted(struct dev_state *ps)
260 struct async *as = NULL;
262 spin_lock_irqsave(&ps->lock, flags);
263 if (!list_empty(&ps->async_completed)) {
264 as = list_entry(ps->async_completed.next, struct async,
266 list_del_init(&as->asynclist);
268 spin_unlock_irqrestore(&ps->lock, flags);
272 static inline struct async *async_getpending(struct dev_state *ps,
273 void __user *userurb)
278 spin_lock_irqsave(&ps->lock, flags);
279 list_for_each_entry(as, &ps->async_pending, asynclist)
280 if (as->userurb == userurb) {
281 list_del_init(&as->asynclist);
282 spin_unlock_irqrestore(&ps->lock, flags);
285 spin_unlock_irqrestore(&ps->lock, flags);
289 static void snoop_urb(struct urb *urb, void __user *userurb)
292 unsigned char *data = urb->transfer_buffer;
297 dev_info(&urb->dev->dev, "direction=%s\n",
298 usb_urb_dir_in(urb) ? "IN" : "OUT");
299 dev_info(&urb->dev->dev, "userurb=%p\n", userurb);
300 dev_info(&urb->dev->dev, "transfer_buffer_length=%d\n",
301 urb->transfer_buffer_length);
302 dev_info(&urb->dev->dev, "actual_length=%d\n", urb->actual_length);
303 dev_info(&urb->dev->dev, "data: ");
304 for (j = 0; j < urb->transfer_buffer_length; ++j)
305 printk("%02x ", data[j]);
309 static void async_completed(struct urb *urb)
311 struct async *as = urb->context;
312 struct dev_state *ps = as->ps;
313 struct siginfo sinfo;
315 spin_lock(&ps->lock);
316 list_move_tail(&as->asynclist, &ps->async_completed);
317 spin_unlock(&ps->lock);
318 as->status = urb->status;
320 sinfo.si_signo = as->signr;
321 sinfo.si_errno = as->status;
322 sinfo.si_code = SI_ASYNCIO;
323 sinfo.si_addr = as->userurb;
324 kill_pid_info_as_uid(as->signr, &sinfo, as->pid, as->uid,
325 as->euid, as->secid);
327 snoop(&urb->dev->dev, "urb complete\n");
328 snoop_urb(urb, as->userurb);
332 static void destroy_async(struct dev_state *ps, struct list_head *list)
337 spin_lock_irqsave(&ps->lock, flags);
338 while (!list_empty(list)) {
339 as = list_entry(list->next, struct async, asynclist);
340 list_del_init(&as->asynclist);
342 /* drop the spinlock so the completion handler can run */
343 spin_unlock_irqrestore(&ps->lock, flags);
344 usb_kill_urb(as->urb);
345 spin_lock_irqsave(&ps->lock, flags);
347 spin_unlock_irqrestore(&ps->lock, flags);
348 as = async_getcompleted(ps);
351 as = async_getcompleted(ps);
355 static void destroy_async_on_interface(struct dev_state *ps,
358 struct list_head *p, *q, hitlist;
361 INIT_LIST_HEAD(&hitlist);
362 spin_lock_irqsave(&ps->lock, flags);
363 list_for_each_safe(p, q, &ps->async_pending)
364 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
365 list_move_tail(p, &hitlist);
366 spin_unlock_irqrestore(&ps->lock, flags);
367 destroy_async(ps, &hitlist);
370 static inline void destroy_all_async(struct dev_state *ps)
372 destroy_async(ps, &ps->async_pending);
376 * interface claims are made only at the request of user level code,
377 * which can also release them (explicitly or by closing files).
378 * they're also undone when devices disconnect.
381 static int driver_probe(struct usb_interface *intf,
382 const struct usb_device_id *id)
387 static void driver_disconnect(struct usb_interface *intf)
389 struct dev_state *ps = usb_get_intfdata(intf);
390 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
395 /* NOTE: this relies on usbcore having canceled and completed
396 * all pending I/O requests; 2.6 does that.
399 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
400 clear_bit(ifnum, &ps->ifclaimed);
402 warn("interface number %u out of range", ifnum);
404 usb_set_intfdata(intf, NULL);
406 /* force async requests to complete */
407 destroy_async_on_interface(ps, ifnum);
410 /* The following routines are merely placeholders. There is no way
411 * to inform a user task about suspend or resumes.
413 static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
418 static int driver_resume(struct usb_interface *intf)
423 struct usb_driver usbfs_driver = {
425 .probe = driver_probe,
426 .disconnect = driver_disconnect,
427 .suspend = driver_suspend,
428 .resume = driver_resume,
431 static int claimintf(struct dev_state *ps, unsigned int ifnum)
433 struct usb_device *dev = ps->dev;
434 struct usb_interface *intf;
437 if (ifnum >= 8*sizeof(ps->ifclaimed))
439 /* already claimed */
440 if (test_bit(ifnum, &ps->ifclaimed))
443 intf = usb_ifnum_to_if(dev, ifnum);
447 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
449 set_bit(ifnum, &ps->ifclaimed);
453 static int releaseintf(struct dev_state *ps, unsigned int ifnum)
455 struct usb_device *dev;
456 struct usb_interface *intf;
460 if (ifnum >= 8*sizeof(ps->ifclaimed))
463 intf = usb_ifnum_to_if(dev, ifnum);
466 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
467 usb_driver_release_interface(&usbfs_driver, intf);
473 static int checkintf(struct dev_state *ps, unsigned int ifnum)
475 if (ps->dev->state != USB_STATE_CONFIGURED)
476 return -EHOSTUNREACH;
477 if (ifnum >= 8*sizeof(ps->ifclaimed))
479 if (test_bit(ifnum, &ps->ifclaimed))
481 /* if not yet claimed, claim it for the driver */
482 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
483 "interface %u before use\n", task_pid_nr(current),
484 current->comm, ifnum);
485 return claimintf(ps, ifnum);
488 static int findintfep(struct usb_device *dev, unsigned int ep)
490 unsigned int i, j, e;
491 struct usb_interface *intf;
492 struct usb_host_interface *alts;
493 struct usb_endpoint_descriptor *endpt;
495 if (ep & ~(USB_DIR_IN|0xf))
499 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
500 intf = dev->actconfig->interface[i];
501 for (j = 0; j < intf->num_altsetting; j++) {
502 alts = &intf->altsetting[j];
503 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
504 endpt = &alts->endpoint[e].desc;
505 if (endpt->bEndpointAddress == ep)
506 return alts->desc.bInterfaceNumber;
513 static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
518 if (ps->dev->state != USB_STATE_ADDRESS
519 && ps->dev->state != USB_STATE_CONFIGURED)
520 return -EHOSTUNREACH;
521 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
525 switch (requesttype & USB_RECIP_MASK) {
526 case USB_RECIP_ENDPOINT:
527 ret = findintfep(ps->dev, index);
529 ret = checkintf(ps, ret);
532 case USB_RECIP_INTERFACE:
533 ret = checkintf(ps, index);
539 static int __match_minor(struct device *dev, void *data)
541 int minor = *((int *)data);
543 if (dev->devt == MKDEV(USB_DEVICE_MAJOR, minor))
548 static struct usb_device *usbdev_lookup_by_minor(int minor)
552 dev = bus_find_device(&usb_bus_type, NULL, &minor, __match_minor);
556 return container_of(dev, struct usb_device, dev);
562 static int usbdev_open(struct inode *inode, struct file *file)
564 struct usb_device *dev = NULL;
565 struct dev_state *ps;
568 /* Protect against simultaneous removal or release */
569 mutex_lock(&usbfs_mutex);
572 ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
577 /* usbdev device-node */
578 if (imajor(inode) == USB_DEVICE_MAJOR)
579 dev = usbdev_lookup_by_minor(iminor(inode));
580 #ifdef CONFIG_USB_DEVICEFS
583 dev = inode->i_private;
587 ret = usb_autoresume_device(dev);
595 spin_lock_init(&ps->lock);
596 INIT_LIST_HEAD(&ps->list);
597 INIT_LIST_HEAD(&ps->async_pending);
598 INIT_LIST_HEAD(&ps->async_completed);
599 init_waitqueue_head(&ps->wait);
601 ps->disc_pid = get_pid(task_pid(current));
602 ps->disc_uid = current->uid;
603 ps->disc_euid = current->euid;
604 ps->disccontext = NULL;
606 security_task_getsecid(current, &ps->secid);
608 list_add_tail(&ps->list, &dev->filelist);
609 file->private_data = ps;
613 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;
652 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
654 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex);
657 if (ctrl.wLength > PAGE_SIZE)
659 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
663 if (ctrl.bRequestType & 0x80) {
664 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
666 free_page((unsigned long)tbuf);
669 snoop(&dev->dev, "control read: bRequest=%02x "
670 "bRrequestType=%02x wValue=%04x "
671 "wIndex=%04x wLength=%04x\n",
672 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
673 ctrl.wIndex, ctrl.wLength);
675 usb_unlock_device(dev);
676 i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest,
677 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
678 tbuf, ctrl.wLength, tmo);
679 usb_lock_device(dev);
680 if ((i > 0) && ctrl.wLength) {
682 dev_info(&dev->dev, "control read: data ");
683 for (j = 0; j < i; ++j)
684 printk("%02x ", (u8)(tbuf)[j]);
687 if (copy_to_user(ctrl.data, tbuf, i)) {
688 free_page((unsigned long)tbuf);
694 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
695 free_page((unsigned long)tbuf);
699 snoop(&dev->dev, "control write: bRequest=%02x "
700 "bRrequestType=%02x wValue=%04x "
701 "wIndex=%04x wLength=%04x\n",
702 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
703 ctrl.wIndex, ctrl.wLength);
705 dev_info(&dev->dev, "control write: data: ");
706 for (j = 0; j < ctrl.wLength; ++j)
707 printk("%02x ", (unsigned char)(tbuf)[j]);
710 usb_unlock_device(dev);
711 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
712 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
713 tbuf, ctrl.wLength, tmo);
714 usb_lock_device(dev);
716 free_page((unsigned long)tbuf);
717 if (i < 0 && i != -EPIPE) {
718 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
719 "failed cmd %s rqt %u rq %u len %u ret %d\n",
720 current->comm, ctrl.bRequestType, ctrl.bRequest,
726 static int proc_bulk(struct dev_state *ps, void __user *arg)
728 struct usb_device *dev = ps->dev;
729 struct usbdevfs_bulktransfer bulk;
730 unsigned int tmo, len1, pipe;
735 if (copy_from_user(&bulk, arg, sizeof(bulk)))
737 ret = findintfep(ps->dev, bulk.ep);
740 ret = checkintf(ps, ret);
743 if (bulk.ep & USB_DIR_IN)
744 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
746 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
747 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
750 if (len1 > MAX_USBFS_BUFFER_SIZE)
752 if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
755 if (bulk.ep & 0x80) {
756 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
760 snoop(&dev->dev, "bulk read: len=0x%02x timeout=%04d\n",
761 bulk.len, bulk.timeout);
762 usb_unlock_device(dev);
763 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
764 usb_lock_device(dev);
767 dev_info(&dev->dev, "bulk read: data ");
768 for (j = 0; j < len2; ++j)
769 printk("%02x ", (u8)(tbuf)[j]);
772 if (copy_to_user(bulk.data, tbuf, len2)) {
779 if (copy_from_user(tbuf, bulk.data, len1)) {
784 snoop(&dev->dev, "bulk write: len=0x%02x timeout=%04d\n",
785 bulk.len, bulk.timeout);
787 dev_info(&dev->dev, "bulk write: data: ");
788 for (j = 0; j < len1; ++j)
789 printk("%02x ", (unsigned char)(tbuf)[j]);
792 usb_unlock_device(dev);
793 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
794 usb_lock_device(dev);
802 static int proc_resetep(struct dev_state *ps, void __user *arg)
807 if (get_user(ep, (unsigned int __user *)arg))
809 ret = findintfep(ps->dev, ep);
812 ret = checkintf(ps, ret);
815 usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0);
819 static int proc_clearhalt(struct dev_state *ps, void __user *arg)
825 if (get_user(ep, (unsigned int __user *)arg))
827 ret = findintfep(ps->dev, ep);
830 ret = checkintf(ps, ret);
834 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
836 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
838 return usb_clear_halt(ps->dev, pipe);
841 static int proc_getdriver(struct dev_state *ps, void __user *arg)
843 struct usbdevfs_getdriver gd;
844 struct usb_interface *intf;
847 if (copy_from_user(&gd, arg, sizeof(gd)))
849 intf = usb_ifnum_to_if(ps->dev, gd.interface);
850 if (!intf || !intf->dev.driver)
853 strncpy(gd.driver, intf->dev.driver->name,
855 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
860 static int proc_connectinfo(struct dev_state *ps, void __user *arg)
862 struct usbdevfs_connectinfo ci;
864 ci.devnum = ps->dev->devnum;
865 ci.slow = ps->dev->speed == USB_SPEED_LOW;
866 if (copy_to_user(arg, &ci, sizeof(ci)))
871 static int proc_resetdevice(struct dev_state *ps)
873 return usb_reset_composite_device(ps->dev, NULL);
876 static int proc_setintf(struct dev_state *ps, void __user *arg)
878 struct usbdevfs_setinterface setintf;
881 if (copy_from_user(&setintf, arg, sizeof(setintf)))
883 if ((ret = checkintf(ps, setintf.interface)))
885 return usb_set_interface(ps->dev, setintf.interface,
889 static int proc_setconfig(struct dev_state *ps, void __user *arg)
893 struct usb_host_config *actconfig;
895 if (get_user(u, (int __user *)arg))
898 actconfig = ps->dev->actconfig;
900 /* Don't touch the device if any interfaces are claimed.
901 * It could interfere with other drivers' operations, and if
902 * an interface is claimed by usbfs it could easily deadlock.
907 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
908 if (usb_interface_claimed(actconfig->interface[i])) {
909 dev_warn(&ps->dev->dev,
910 "usbfs: interface %d claimed by %s "
911 "while '%s' sets config #%d\n",
912 actconfig->interface[i]
914 ->desc.bInterfaceNumber,
915 actconfig->interface[i]
924 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
925 * so avoid usb_set_configuration()'s kick to sysfs
928 if (actconfig && actconfig->desc.bConfigurationValue == u)
929 status = usb_reset_configuration(ps->dev);
931 status = usb_set_configuration(ps->dev, u);
937 static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
938 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
941 struct usbdevfs_iso_packet_desc *isopkt = NULL;
942 struct usb_host_endpoint *ep;
944 struct usb_ctrlrequest *dr = NULL;
945 unsigned int u, totlen, isofrmlen;
949 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP|USBDEVFS_URB_SHORT_NOT_OK|
950 URB_NO_FSBR|URB_ZERO_PACKET))
954 if (uurb->signr != 0 && (uurb->signr < SIGRTMIN ||
955 uurb->signr > SIGRTMAX))
957 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
958 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
959 ifnum = findintfep(ps->dev, uurb->endpoint);
962 ret = checkintf(ps, ifnum);
966 if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
968 ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
971 ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
976 case USBDEVFS_URB_TYPE_CONTROL:
977 if (!usb_endpoint_xfer_control(&ep->desc))
979 /* min 8 byte setup packet,
980 * max 8 byte setup plus an arbitrary data stage */
981 if (uurb->buffer_length < 8 ||
982 uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE))
984 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
987 if (copy_from_user(dr, uurb->buffer, 8)) {
991 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
995 ret = check_ctrlrecip(ps, dr->bRequestType,
996 le16_to_cpup(&dr->wIndex));
1001 uurb->number_of_packets = 0;
1002 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1004 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1006 uurb->endpoint |= USB_DIR_IN;
1009 uurb->endpoint &= ~USB_DIR_IN;
1011 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1012 uurb->buffer, uurb->buffer_length)) {
1016 snoop(&ps->dev->dev, "control urb: bRequest=%02x "
1017 "bRrequestType=%02x wValue=%04x "
1018 "wIndex=%04x wLength=%04x\n",
1019 dr->bRequest, dr->bRequestType,
1020 __le16_to_cpup(&dr->wValue),
1021 __le16_to_cpup(&dr->wIndex),
1022 __le16_to_cpup(&dr->wLength));
1025 case USBDEVFS_URB_TYPE_BULK:
1026 switch (usb_endpoint_type(&ep->desc)) {
1027 case USB_ENDPOINT_XFER_CONTROL:
1028 case USB_ENDPOINT_XFER_ISOC:
1030 /* allow single-shot interrupt transfers, at bogus rates */
1032 uurb->number_of_packets = 0;
1033 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1035 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1036 uurb->buffer, uurb->buffer_length))
1038 snoop(&ps->dev->dev, "bulk urb\n");
1041 case USBDEVFS_URB_TYPE_ISO:
1042 /* arbitrary limit */
1043 if (uurb->number_of_packets < 1 ||
1044 uurb->number_of_packets > 128)
1046 if (!usb_endpoint_xfer_isoc(&ep->desc))
1048 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1049 uurb->number_of_packets;
1050 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
1052 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
1056 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1058 * sufficient for USB 2.0 high-bandwidth iso */
1059 if (isopkt[u].length > 8192) {
1063 totlen += isopkt[u].length;
1065 if (totlen > 32768) {
1069 uurb->buffer_length = totlen;
1070 snoop(&ps->dev->dev, "iso urb\n");
1073 case USBDEVFS_URB_TYPE_INTERRUPT:
1074 uurb->number_of_packets = 0;
1075 if (!usb_endpoint_xfer_int(&ep->desc))
1077 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1079 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1080 uurb->buffer, uurb->buffer_length))
1082 snoop(&ps->dev->dev, "interrupt urb\n");
1088 as = alloc_async(uurb->number_of_packets);
1094 as->urb->transfer_buffer = kmalloc(uurb->buffer_length, GFP_KERNEL);
1095 if (!as->urb->transfer_buffer) {
1101 as->urb->dev = ps->dev;
1102 as->urb->pipe = (uurb->type << 30) |
1103 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1104 (uurb->endpoint & USB_DIR_IN);
1105 as->urb->transfer_flags = uurb->flags |
1106 (is_in ? URB_DIR_IN : URB_DIR_OUT);
1107 as->urb->transfer_buffer_length = uurb->buffer_length;
1108 as->urb->setup_packet = (unsigned char *)dr;
1109 as->urb->start_frame = uurb->start_frame;
1110 as->urb->number_of_packets = uurb->number_of_packets;
1111 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1112 ps->dev->speed == USB_SPEED_HIGH)
1113 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
1115 as->urb->interval = ep->desc.bInterval;
1116 as->urb->context = as;
1117 as->urb->complete = async_completed;
1118 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1119 as->urb->iso_frame_desc[u].offset = totlen;
1120 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1121 totlen += isopkt[u].length;
1126 if (uurb->endpoint & USB_DIR_IN)
1127 as->userbuffer = uurb->buffer;
1129 as->userbuffer = NULL;
1130 as->signr = uurb->signr;
1132 as->pid = get_pid(task_pid(current));
1133 as->uid = current->uid;
1134 as->euid = current->euid;
1135 security_task_getsecid(current, &as->secid);
1137 if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
1138 as->urb->transfer_buffer_length)) {
1143 snoop_urb(as->urb, as->userurb);
1144 async_newpending(as);
1145 if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
1146 dev_printk(KERN_DEBUG, &ps->dev->dev,
1147 "usbfs: usb_submit_urb returned %d\n", ret);
1148 async_removepending(as);
1155 static int proc_submiturb(struct dev_state *ps, void __user *arg)
1157 struct usbdevfs_urb uurb;
1159 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1162 return proc_do_submiturb(ps, &uurb,
1163 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1167 static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
1171 as = async_getpending(ps, arg);
1174 usb_kill_urb(as->urb);
1178 static int processcompl(struct async *as, void __user * __user *arg)
1180 struct urb *urb = as->urb;
1181 struct usbdevfs_urb __user *userurb = as->userurb;
1182 void __user *addr = as->userurb;
1186 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1187 urb->transfer_buffer_length))
1189 if (put_user(as->status, &userurb->status))
1191 if (put_user(urb->actual_length, &userurb->actual_length))
1193 if (put_user(urb->error_count, &userurb->error_count))
1196 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1197 for (i = 0; i < urb->number_of_packets; i++) {
1198 if (put_user(urb->iso_frame_desc[i].actual_length,
1199 &userurb->iso_frame_desc[i].actual_length))
1201 if (put_user(urb->iso_frame_desc[i].status,
1202 &userurb->iso_frame_desc[i].status))
1209 if (put_user(addr, (void __user * __user *)arg))
1214 static struct async *reap_as(struct dev_state *ps)
1216 DECLARE_WAITQUEUE(wait, current);
1217 struct async *as = NULL;
1218 struct usb_device *dev = ps->dev;
1220 add_wait_queue(&ps->wait, &wait);
1222 __set_current_state(TASK_INTERRUPTIBLE);
1223 as = async_getcompleted(ps);
1226 if (signal_pending(current))
1228 usb_unlock_device(dev);
1230 usb_lock_device(dev);
1232 remove_wait_queue(&ps->wait, &wait);
1233 set_current_state(TASK_RUNNING);
1237 static int proc_reapurb(struct dev_state *ps, void __user *arg)
1239 struct async *as = reap_as(ps);
1241 return processcompl(as, (void __user * __user *)arg);
1242 if (signal_pending(current))
1247 static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
1251 if (!(as = async_getcompleted(ps)))
1253 return processcompl(as, (void __user * __user *)arg);
1256 #ifdef CONFIG_COMPAT
1258 static int get_urb32(struct usbdevfs_urb *kurb,
1259 struct usbdevfs_urb32 __user *uurb)
1262 if (get_user(kurb->type, &uurb->type) ||
1263 __get_user(kurb->endpoint, &uurb->endpoint) ||
1264 __get_user(kurb->status, &uurb->status) ||
1265 __get_user(kurb->flags, &uurb->flags) ||
1266 __get_user(kurb->buffer_length, &uurb->buffer_length) ||
1267 __get_user(kurb->actual_length, &uurb->actual_length) ||
1268 __get_user(kurb->start_frame, &uurb->start_frame) ||
1269 __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
1270 __get_user(kurb->error_count, &uurb->error_count) ||
1271 __get_user(kurb->signr, &uurb->signr))
1274 if (__get_user(uptr, &uurb->buffer))
1276 kurb->buffer = compat_ptr(uptr);
1277 if (__get_user(uptr, &uurb->buffer))
1279 kurb->usercontext = compat_ptr(uptr);
1284 static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
1286 struct usbdevfs_urb uurb;
1288 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
1291 return proc_do_submiturb(ps, &uurb,
1292 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
1296 static int processcompl_compat(struct async *as, void __user * __user *arg)
1298 struct urb *urb = as->urb;
1299 struct usbdevfs_urb32 __user *userurb = as->userurb;
1300 void __user *addr = as->userurb;
1304 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1305 urb->transfer_buffer_length))
1307 if (put_user(as->status, &userurb->status))
1309 if (put_user(urb->actual_length, &userurb->actual_length))
1311 if (put_user(urb->error_count, &userurb->error_count))
1314 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1315 for (i = 0; i < urb->number_of_packets; i++) {
1316 if (put_user(urb->iso_frame_desc[i].actual_length,
1317 &userurb->iso_frame_desc[i].actual_length))
1319 if (put_user(urb->iso_frame_desc[i].status,
1320 &userurb->iso_frame_desc[i].status))
1326 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
1331 static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
1333 struct async *as = reap_as(ps);
1335 return processcompl_compat(as, (void __user * __user *)arg);
1336 if (signal_pending(current))
1341 static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
1345 if (!(as = async_getcompleted(ps)))
1347 return processcompl_compat(as, (void __user * __user *)arg);
1352 static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
1354 struct usbdevfs_disconnectsignal ds;
1356 if (copy_from_user(&ds, arg, sizeof(ds)))
1358 if (ds.signr != 0 && (ds.signr < SIGRTMIN || ds.signr > SIGRTMAX))
1360 ps->discsignr = ds.signr;
1361 ps->disccontext = ds.context;
1365 static int proc_claiminterface(struct dev_state *ps, void __user *arg)
1369 if (get_user(ifnum, (unsigned int __user *)arg))
1371 return claimintf(ps, ifnum);
1374 static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
1379 if (get_user(ifnum, (unsigned int __user *)arg))
1381 if ((ret = releaseintf(ps, ifnum)) < 0)
1383 destroy_async_on_interface (ps, ifnum);
1387 static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
1392 struct usb_interface *intf = NULL;
1393 struct usb_driver *driver = NULL;
1396 if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
1397 if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
1399 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
1400 if (copy_from_user(buf, ctl->data, size)) {
1405 memset(buf, 0, size);
1409 if (!connected(ps)) {
1414 if (ps->dev->state != USB_STATE_CONFIGURED)
1415 retval = -EHOSTUNREACH;
1416 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
1418 else switch (ctl->ioctl_code) {
1420 /* disconnect kernel driver from interface */
1421 case USBDEVFS_DISCONNECT:
1422 if (intf->dev.driver) {
1423 driver = to_usb_driver(intf->dev.driver);
1424 dev_dbg(&intf->dev, "disconnect by usbfs\n");
1425 usb_driver_release_interface(driver, intf);
1430 /* let kernel drivers try to (re)bind to the interface */
1431 case USBDEVFS_CONNECT:
1432 if (!intf->dev.driver)
1433 retval = device_attach(&intf->dev);
1438 /* talk directly to the interface's driver */
1440 if (intf->dev.driver)
1441 driver = to_usb_driver(intf->dev.driver);
1442 if (driver == NULL || driver->ioctl == NULL) {
1445 retval = driver->ioctl(intf, ctl->ioctl_code, buf);
1446 if (retval == -ENOIOCTLCMD)
1451 /* cleanup and return */
1453 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
1455 && copy_to_user(ctl->data, buf, size) != 0)
1462 static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
1464 struct usbdevfs_ioctl ctrl;
1466 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1468 return proc_ioctl(ps, &ctrl);
1471 #ifdef CONFIG_COMPAT
1472 static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
1474 struct usbdevfs_ioctl32 __user *uioc;
1475 struct usbdevfs_ioctl ctrl;
1478 uioc = compat_ptr((long)arg);
1479 if (get_user(ctrl.ifno, &uioc->ifno) ||
1480 get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
1481 __get_user(udata, &uioc->data))
1483 ctrl.data = compat_ptr(udata);
1485 return proc_ioctl(ps, &ctrl);
1490 * NOTE: All requests here that have interface numbers as parameters
1491 * are assuming that somehow the configuration has been prevented from
1492 * changing. But there's no mechanism to ensure that...
1494 static int usbdev_ioctl(struct inode *inode, struct file *file,
1495 unsigned int cmd, unsigned long arg)
1497 struct dev_state *ps = file->private_data;
1498 struct usb_device *dev = ps->dev;
1499 void __user *p = (void __user *)arg;
1502 if (!(file->f_mode & FMODE_WRITE))
1504 usb_lock_device(dev);
1505 if (!connected(ps)) {
1506 usb_unlock_device(dev);
1511 case USBDEVFS_CONTROL:
1512 snoop(&dev->dev, "%s: CONTROL\n", __FUNCTION__);
1513 ret = proc_control(ps, p);
1515 inode->i_mtime = CURRENT_TIME;
1519 snoop(&dev->dev, "%s: BULK\n", __FUNCTION__);
1520 ret = proc_bulk(ps, p);
1522 inode->i_mtime = CURRENT_TIME;
1525 case USBDEVFS_RESETEP:
1526 snoop(&dev->dev, "%s: RESETEP\n", __FUNCTION__);
1527 ret = proc_resetep(ps, p);
1529 inode->i_mtime = CURRENT_TIME;
1532 case USBDEVFS_RESET:
1533 snoop(&dev->dev, "%s: RESET\n", __FUNCTION__);
1534 ret = proc_resetdevice(ps);
1537 case USBDEVFS_CLEAR_HALT:
1538 snoop(&dev->dev, "%s: CLEAR_HALT\n", __FUNCTION__);
1539 ret = proc_clearhalt(ps, p);
1541 inode->i_mtime = CURRENT_TIME;
1544 case USBDEVFS_GETDRIVER:
1545 snoop(&dev->dev, "%s: GETDRIVER\n", __FUNCTION__);
1546 ret = proc_getdriver(ps, p);
1549 case USBDEVFS_CONNECTINFO:
1550 snoop(&dev->dev, "%s: CONNECTINFO\n", __FUNCTION__);
1551 ret = proc_connectinfo(ps, p);
1554 case USBDEVFS_SETINTERFACE:
1555 snoop(&dev->dev, "%s: SETINTERFACE\n", __FUNCTION__);
1556 ret = proc_setintf(ps, p);
1559 case USBDEVFS_SETCONFIGURATION:
1560 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __FUNCTION__);
1561 ret = proc_setconfig(ps, p);
1564 case USBDEVFS_SUBMITURB:
1565 snoop(&dev->dev, "%s: SUBMITURB\n", __FUNCTION__);
1566 ret = proc_submiturb(ps, p);
1568 inode->i_mtime = CURRENT_TIME;
1571 #ifdef CONFIG_COMPAT
1573 case USBDEVFS_SUBMITURB32:
1574 snoop(&dev->dev, "%s: SUBMITURB32\n", __FUNCTION__);
1575 ret = proc_submiturb_compat(ps, p);
1577 inode->i_mtime = CURRENT_TIME;
1580 case USBDEVFS_REAPURB32:
1581 snoop(&dev->dev, "%s: REAPURB32\n", __FUNCTION__);
1582 ret = proc_reapurb_compat(ps, p);
1585 case USBDEVFS_REAPURBNDELAY32:
1586 snoop(&dev->dev, "%s: REAPURBDELAY32\n", __FUNCTION__);
1587 ret = proc_reapurbnonblock_compat(ps, p);
1590 case USBDEVFS_IOCTL32:
1591 snoop(&dev->dev, "%s: IOCTL\n", __FUNCTION__);
1592 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
1596 case USBDEVFS_DISCARDURB:
1597 snoop(&dev->dev, "%s: DISCARDURB\n", __FUNCTION__);
1598 ret = proc_unlinkurb(ps, p);
1601 case USBDEVFS_REAPURB:
1602 snoop(&dev->dev, "%s: REAPURB\n", __FUNCTION__);
1603 ret = proc_reapurb(ps, p);
1606 case USBDEVFS_REAPURBNDELAY:
1607 snoop(&dev->dev, "%s: REAPURBDELAY\n", __FUNCTION__);
1608 ret = proc_reapurbnonblock(ps, p);
1611 case USBDEVFS_DISCSIGNAL:
1612 snoop(&dev->dev, "%s: DISCSIGNAL\n", __FUNCTION__);
1613 ret = proc_disconnectsignal(ps, p);
1616 case USBDEVFS_CLAIMINTERFACE:
1617 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __FUNCTION__);
1618 ret = proc_claiminterface(ps, p);
1621 case USBDEVFS_RELEASEINTERFACE:
1622 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __FUNCTION__);
1623 ret = proc_releaseinterface(ps, p);
1626 case USBDEVFS_IOCTL:
1627 snoop(&dev->dev, "%s: IOCTL\n", __FUNCTION__);
1628 ret = proc_ioctl_default(ps, p);
1631 usb_unlock_device(dev);
1633 inode->i_atime = CURRENT_TIME;
1637 /* No kernel lock - fine */
1638 static unsigned int usbdev_poll(struct file *file,
1639 struct poll_table_struct *wait)
1641 struct dev_state *ps = file->private_data;
1642 unsigned int mask = 0;
1644 poll_wait(file, &ps->wait, wait);
1645 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
1646 mask |= POLLOUT | POLLWRNORM;
1648 mask |= POLLERR | POLLHUP;
1652 const struct file_operations usbdev_file_operations = {
1653 .owner = THIS_MODULE,
1654 .llseek = usbdev_lseek,
1655 .read = usbdev_read,
1656 .poll = usbdev_poll,
1657 .ioctl = usbdev_ioctl,
1658 .open = usbdev_open,
1659 .release = usbdev_release,
1662 #ifdef CONFIG_USB_DEVICE_CLASS
1663 static struct class *usb_classdev_class;
1665 static int usb_classdev_add(struct usb_device *dev)
1667 int minor = ((dev->bus->busnum-1) * 128) + (dev->devnum-1);
1669 dev->usb_classdev = device_create(usb_classdev_class, &dev->dev,
1670 MKDEV(USB_DEVICE_MAJOR, minor),
1671 "usbdev%d.%d", dev->bus->busnum, dev->devnum);
1672 if (IS_ERR(dev->usb_classdev))
1673 return PTR_ERR(dev->usb_classdev);
1678 static void usb_classdev_remove(struct usb_device *dev)
1680 device_unregister(dev->usb_classdev);
1683 static int usb_classdev_notify(struct notifier_block *self,
1684 unsigned long action, void *dev)
1687 case USB_DEVICE_ADD:
1688 if (usb_classdev_add(dev))
1691 case USB_DEVICE_REMOVE:
1692 usb_classdev_remove(dev);
1698 static struct notifier_block usbdev_nb = {
1699 .notifier_call = usb_classdev_notify,
1703 static struct cdev usb_device_cdev;
1705 int __init usb_devio_init(void)
1709 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
1712 err("unable to register minors for usb_device");
1715 cdev_init(&usb_device_cdev, &usbdev_file_operations);
1716 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
1718 err("unable to get usb_device major %d", USB_DEVICE_MAJOR);
1721 #ifdef CONFIG_USB_DEVICE_CLASS
1722 usb_classdev_class = class_create(THIS_MODULE, "usb_device");
1723 if (IS_ERR(usb_classdev_class)) {
1724 err("unable to register usb_device class");
1725 retval = PTR_ERR(usb_classdev_class);
1726 cdev_del(&usb_device_cdev);
1727 usb_classdev_class = NULL;
1731 usb_register_notify(&usbdev_nb);
1737 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
1741 void usb_devio_cleanup(void)
1743 #ifdef CONFIG_USB_DEVICE_CLASS
1744 usb_unregister_notify(&usbdev_nb);
1745 class_destroy(usb_classdev_class);
1747 cdev_del(&usb_device_cdev);
1748 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);