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 */
58 #define USB_DEVICE_MAX USB_MAXBUS * 128
60 /* Mutual exclusion for removal, open, and release */
61 DEFINE_MUTEX(usbfs_mutex);
64 struct list_head list; /* state list */
65 struct usb_device *dev;
67 spinlock_t lock; /* protects the async urb lists */
68 struct list_head async_pending;
69 struct list_head async_completed;
70 wait_queue_head_t wait; /* wake up if a request completed */
71 unsigned int discsignr;
73 uid_t disc_uid, disc_euid;
74 void __user *disccontext;
75 unsigned long ifclaimed;
80 struct list_head asynclist;
86 void __user *userbuffer;
93 static int usbfs_snoop;
94 module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
95 MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
97 #define snoop(dev, format, arg...) \
100 dev_info(dev , format , ## arg); \
103 #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
106 #define MAX_USBFS_BUFFER_SIZE 16384
108 static int connected(struct dev_state *ps)
110 return (!list_empty(&ps->list) &&
111 ps->dev->state != USB_STATE_NOTATTACHED);
114 static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
122 file->f_pos = offset;
126 file->f_pos += offset;
138 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
141 struct dev_state *ps = file->private_data;
142 struct usb_device *dev = ps->dev;
149 usb_lock_device(dev);
150 if (!connected(ps)) {
153 } else if (pos < 0) {
158 if (pos < sizeof(struct usb_device_descriptor)) {
159 /* 18 bytes - fits on the stack */
160 struct usb_device_descriptor temp_desc;
162 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
163 le16_to_cpus(&temp_desc.bcdUSB);
164 le16_to_cpus(&temp_desc.idVendor);
165 le16_to_cpus(&temp_desc.idProduct);
166 le16_to_cpus(&temp_desc.bcdDevice);
168 len = sizeof(struct usb_device_descriptor) - pos;
171 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
182 pos = sizeof(struct usb_device_descriptor);
183 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
184 struct usb_config_descriptor *config =
185 (struct usb_config_descriptor *)dev->rawdescriptors[i];
186 unsigned int length = le16_to_cpu(config->wTotalLength);
188 if (*ppos < pos + length) {
190 /* The descriptor may claim to be longer than it
191 * really is. Here is the actual allocated length. */
193 le16_to_cpu(dev->config[i].desc.wTotalLength);
195 len = length - (*ppos - pos);
199 /* Simply don't write (skip over) unallocated parts */
200 if (alloclen > (*ppos - pos)) {
201 alloclen -= (*ppos - pos);
202 if (copy_to_user(buf,
203 dev->rawdescriptors[i] + (*ppos - pos),
204 min(len, alloclen))) {
220 usb_unlock_device(dev);
225 * async list handling
228 static struct async *alloc_async(unsigned int numisoframes)
232 as = kzalloc(sizeof(struct async), GFP_KERNEL);
235 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
243 static void free_async(struct async *as)
246 kfree(as->urb->transfer_buffer);
247 kfree(as->urb->setup_packet);
248 usb_free_urb(as->urb);
252 static void async_newpending(struct async *as)
254 struct dev_state *ps = as->ps;
257 spin_lock_irqsave(&ps->lock, flags);
258 list_add_tail(&as->asynclist, &ps->async_pending);
259 spin_unlock_irqrestore(&ps->lock, flags);
262 static void async_removepending(struct async *as)
264 struct dev_state *ps = as->ps;
267 spin_lock_irqsave(&ps->lock, flags);
268 list_del_init(&as->asynclist);
269 spin_unlock_irqrestore(&ps->lock, flags);
272 static struct async *async_getcompleted(struct dev_state *ps)
275 struct async *as = NULL;
277 spin_lock_irqsave(&ps->lock, flags);
278 if (!list_empty(&ps->async_completed)) {
279 as = list_entry(ps->async_completed.next, struct async,
281 list_del_init(&as->asynclist);
283 spin_unlock_irqrestore(&ps->lock, flags);
287 static struct async *async_getpending(struct dev_state *ps,
288 void __user *userurb)
293 spin_lock_irqsave(&ps->lock, flags);
294 list_for_each_entry(as, &ps->async_pending, asynclist)
295 if (as->userurb == userurb) {
296 list_del_init(&as->asynclist);
297 spin_unlock_irqrestore(&ps->lock, flags);
300 spin_unlock_irqrestore(&ps->lock, flags);
304 static void snoop_urb(struct urb *urb, void __user *userurb)
307 unsigned char *data = urb->transfer_buffer;
312 dev_info(&urb->dev->dev, "direction=%s\n",
313 usb_urb_dir_in(urb) ? "IN" : "OUT");
314 dev_info(&urb->dev->dev, "userurb=%p\n", userurb);
315 dev_info(&urb->dev->dev, "transfer_buffer_length=%u\n",
316 urb->transfer_buffer_length);
317 dev_info(&urb->dev->dev, "actual_length=%u\n", urb->actual_length);
318 dev_info(&urb->dev->dev, "data: ");
319 for (j = 0; j < urb->transfer_buffer_length; ++j)
320 printk("%02x ", data[j]);
324 static void async_completed(struct urb *urb)
326 struct async *as = urb->context;
327 struct dev_state *ps = as->ps;
328 struct siginfo sinfo;
329 struct pid *pid = NULL;
335 spin_lock(&ps->lock);
336 list_move_tail(&as->asynclist, &ps->async_completed);
337 as->status = urb->status;
340 sinfo.si_signo = as->signr;
341 sinfo.si_errno = as->status;
342 sinfo.si_code = SI_ASYNCIO;
343 sinfo.si_addr = as->userurb;
349 snoop(&urb->dev->dev, "urb complete\n");
350 snoop_urb(urb, as->userurb);
351 spin_unlock(&ps->lock);
354 kill_pid_info_as_uid(sinfo.si_signo, &sinfo, pid, uid,
360 static void destroy_async(struct dev_state *ps, struct list_head *list)
365 spin_lock_irqsave(&ps->lock, flags);
366 while (!list_empty(list)) {
367 as = list_entry(list->next, struct async, asynclist);
368 list_del_init(&as->asynclist);
370 /* drop the spinlock so the completion handler can run */
371 spin_unlock_irqrestore(&ps->lock, flags);
372 usb_kill_urb(as->urb);
373 spin_lock_irqsave(&ps->lock, flags);
375 spin_unlock_irqrestore(&ps->lock, flags);
378 static void destroy_async_on_interface(struct dev_state *ps,
381 struct list_head *p, *q, hitlist;
384 INIT_LIST_HEAD(&hitlist);
385 spin_lock_irqsave(&ps->lock, flags);
386 list_for_each_safe(p, q, &ps->async_pending)
387 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
388 list_move_tail(p, &hitlist);
389 spin_unlock_irqrestore(&ps->lock, flags);
390 destroy_async(ps, &hitlist);
393 static void destroy_all_async(struct dev_state *ps)
395 destroy_async(ps, &ps->async_pending);
399 * interface claims are made only at the request of user level code,
400 * which can also release them (explicitly or by closing files).
401 * they're also undone when devices disconnect.
404 static int driver_probe(struct usb_interface *intf,
405 const struct usb_device_id *id)
410 static void driver_disconnect(struct usb_interface *intf)
412 struct dev_state *ps = usb_get_intfdata(intf);
413 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
418 /* NOTE: this relies on usbcore having canceled and completed
419 * all pending I/O requests; 2.6 does that.
422 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
423 clear_bit(ifnum, &ps->ifclaimed);
425 dev_warn(&intf->dev, "interface number %u out of range\n",
428 usb_set_intfdata(intf, NULL);
430 /* force async requests to complete */
431 destroy_async_on_interface(ps, ifnum);
434 /* The following routines are merely placeholders. There is no way
435 * to inform a user task about suspend or resumes.
437 static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
442 static int driver_resume(struct usb_interface *intf)
447 struct usb_driver usbfs_driver = {
449 .probe = driver_probe,
450 .disconnect = driver_disconnect,
451 .suspend = driver_suspend,
452 .resume = driver_resume,
455 static int claimintf(struct dev_state *ps, unsigned int ifnum)
457 struct usb_device *dev = ps->dev;
458 struct usb_interface *intf;
461 if (ifnum >= 8*sizeof(ps->ifclaimed))
463 /* already claimed */
464 if (test_bit(ifnum, &ps->ifclaimed))
467 intf = usb_ifnum_to_if(dev, ifnum);
471 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
473 set_bit(ifnum, &ps->ifclaimed);
477 static int releaseintf(struct dev_state *ps, unsigned int ifnum)
479 struct usb_device *dev;
480 struct usb_interface *intf;
484 if (ifnum >= 8*sizeof(ps->ifclaimed))
487 intf = usb_ifnum_to_if(dev, ifnum);
490 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
491 usb_driver_release_interface(&usbfs_driver, intf);
497 static int checkintf(struct dev_state *ps, unsigned int ifnum)
499 if (ps->dev->state != USB_STATE_CONFIGURED)
500 return -EHOSTUNREACH;
501 if (ifnum >= 8*sizeof(ps->ifclaimed))
503 if (test_bit(ifnum, &ps->ifclaimed))
505 /* if not yet claimed, claim it for the driver */
506 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
507 "interface %u before use\n", task_pid_nr(current),
508 current->comm, ifnum);
509 return claimintf(ps, ifnum);
512 static int findintfep(struct usb_device *dev, unsigned int ep)
514 unsigned int i, j, e;
515 struct usb_interface *intf;
516 struct usb_host_interface *alts;
517 struct usb_endpoint_descriptor *endpt;
519 if (ep & ~(USB_DIR_IN|0xf))
523 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
524 intf = dev->actconfig->interface[i];
525 for (j = 0; j < intf->num_altsetting; j++) {
526 alts = &intf->altsetting[j];
527 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
528 endpt = &alts->endpoint[e].desc;
529 if (endpt->bEndpointAddress == ep)
530 return alts->desc.bInterfaceNumber;
537 static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
542 if (ps->dev->state != USB_STATE_UNAUTHENTICATED
543 && ps->dev->state != USB_STATE_ADDRESS
544 && ps->dev->state != USB_STATE_CONFIGURED)
545 return -EHOSTUNREACH;
546 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
550 switch (requesttype & USB_RECIP_MASK) {
551 case USB_RECIP_ENDPOINT:
552 ret = findintfep(ps->dev, index);
554 ret = checkintf(ps, ret);
557 case USB_RECIP_INTERFACE:
558 ret = checkintf(ps, index);
564 static int match_devt(struct device *dev, void *data)
566 return dev->devt == (dev_t) (unsigned long) data;
569 static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
573 dev = bus_find_device(&usb_bus_type, NULL,
574 (void *) (unsigned long) devt, match_devt);
577 return container_of(dev, struct usb_device, dev);
583 static int usbdev_open(struct inode *inode, struct file *file)
585 struct usb_device *dev = NULL;
586 struct dev_state *ps;
587 const struct cred *cred = current_cred();
591 /* Protect against simultaneous removal or release */
592 mutex_lock(&usbfs_mutex);
595 ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
601 /* usbdev device-node */
602 if (imajor(inode) == USB_DEVICE_MAJOR)
603 dev = usbdev_lookup_by_devt(inode->i_rdev);
604 #ifdef CONFIG_USB_DEVICEFS
607 dev = inode->i_private;
608 if (dev && dev->usbfs_dentry &&
609 dev->usbfs_dentry->d_inode == inode)
615 if (!dev || dev->state == USB_STATE_NOTATTACHED)
617 ret = usb_autoresume_device(dev);
624 spin_lock_init(&ps->lock);
625 INIT_LIST_HEAD(&ps->list);
626 INIT_LIST_HEAD(&ps->async_pending);
627 INIT_LIST_HEAD(&ps->async_completed);
628 init_waitqueue_head(&ps->wait);
630 ps->disc_pid = get_pid(task_pid(current));
631 ps->disc_uid = cred->uid;
632 ps->disc_euid = cred->euid;
633 ps->disccontext = NULL;
635 security_task_getsecid(current, &ps->secid);
637 list_add_tail(&ps->list, &dev->filelist);
638 file->private_data = ps;
639 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
646 mutex_unlock(&usbfs_mutex);
651 static int usbdev_release(struct inode *inode, struct file *file)
653 struct dev_state *ps = file->private_data;
654 struct usb_device *dev = ps->dev;
658 usb_lock_device(dev);
659 usb_hub_release_all_ports(dev, ps);
661 /* Protect against simultaneous open */
662 mutex_lock(&usbfs_mutex);
663 list_del_init(&ps->list);
664 mutex_unlock(&usbfs_mutex);
666 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
668 if (test_bit(ifnum, &ps->ifclaimed))
669 releaseintf(ps, ifnum);
671 destroy_all_async(ps);
672 usb_autosuspend_device(dev);
673 usb_unlock_device(dev);
675 put_pid(ps->disc_pid);
677 as = async_getcompleted(ps);
680 as = async_getcompleted(ps);
686 static int proc_control(struct dev_state *ps, void __user *arg)
688 struct usb_device *dev = ps->dev;
689 struct usbdevfs_ctrltransfer ctrl;
695 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
697 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex);
700 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
701 if (wLength > PAGE_SIZE)
703 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
707 if (ctrl.bRequestType & 0x80) {
708 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
710 free_page((unsigned long)tbuf);
713 snoop(&dev->dev, "control read: bRequest=%02x "
714 "bRrequestType=%02x wValue=%04x "
715 "wIndex=%04x wLength=%04x\n",
716 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
717 ctrl.wIndex, ctrl.wLength);
719 usb_unlock_device(dev);
720 i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest,
721 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
722 tbuf, ctrl.wLength, tmo);
723 usb_lock_device(dev);
724 if ((i > 0) && ctrl.wLength) {
726 dev_info(&dev->dev, "control read: data ");
727 for (j = 0; j < i; ++j)
728 printk("%02x ", (u8)(tbuf)[j]);
731 if (copy_to_user(ctrl.data, tbuf, i)) {
732 free_page((unsigned long)tbuf);
738 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
739 free_page((unsigned long)tbuf);
743 snoop(&dev->dev, "control write: bRequest=%02x "
744 "bRrequestType=%02x wValue=%04x "
745 "wIndex=%04x wLength=%04x\n",
746 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
747 ctrl.wIndex, ctrl.wLength);
749 dev_info(&dev->dev, "control write: data: ");
750 for (j = 0; j < ctrl.wLength; ++j)
751 printk("%02x ", (unsigned char)(tbuf)[j]);
754 usb_unlock_device(dev);
755 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
756 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
757 tbuf, ctrl.wLength, tmo);
758 usb_lock_device(dev);
760 free_page((unsigned long)tbuf);
761 if (i < 0 && i != -EPIPE) {
762 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
763 "failed cmd %s rqt %u rq %u len %u ret %d\n",
764 current->comm, ctrl.bRequestType, ctrl.bRequest,
770 static int proc_bulk(struct dev_state *ps, void __user *arg)
772 struct usb_device *dev = ps->dev;
773 struct usbdevfs_bulktransfer bulk;
774 unsigned int tmo, len1, pipe;
779 if (copy_from_user(&bulk, arg, sizeof(bulk)))
781 ret = findintfep(ps->dev, bulk.ep);
784 ret = checkintf(ps, ret);
787 if (bulk.ep & USB_DIR_IN)
788 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
790 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
791 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
794 if (len1 > MAX_USBFS_BUFFER_SIZE)
796 if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
799 if (bulk.ep & 0x80) {
800 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
804 snoop(&dev->dev, "bulk read: len=0x%02x timeout=%04d\n",
805 bulk.len, bulk.timeout);
806 usb_unlock_device(dev);
807 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
808 usb_lock_device(dev);
811 dev_info(&dev->dev, "bulk read: data ");
812 for (j = 0; j < len2; ++j)
813 printk("%02x ", (u8)(tbuf)[j]);
816 if (copy_to_user(bulk.data, tbuf, len2)) {
823 if (copy_from_user(tbuf, bulk.data, len1)) {
828 snoop(&dev->dev, "bulk write: len=0x%02x timeout=%04d\n",
829 bulk.len, bulk.timeout);
831 dev_info(&dev->dev, "bulk write: data: ");
832 for (j = 0; j < len1; ++j)
833 printk("%02x ", (unsigned char)(tbuf)[j]);
836 usb_unlock_device(dev);
837 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
838 usb_lock_device(dev);
846 static int proc_resetep(struct dev_state *ps, void __user *arg)
851 if (get_user(ep, (unsigned int __user *)arg))
853 ret = findintfep(ps->dev, ep);
856 ret = checkintf(ps, ret);
859 usb_reset_endpoint(ps->dev, ep);
863 static int proc_clearhalt(struct dev_state *ps, void __user *arg)
869 if (get_user(ep, (unsigned int __user *)arg))
871 ret = findintfep(ps->dev, ep);
874 ret = checkintf(ps, ret);
878 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
880 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
882 return usb_clear_halt(ps->dev, pipe);
885 static int proc_getdriver(struct dev_state *ps, void __user *arg)
887 struct usbdevfs_getdriver gd;
888 struct usb_interface *intf;
891 if (copy_from_user(&gd, arg, sizeof(gd)))
893 intf = usb_ifnum_to_if(ps->dev, gd.interface);
894 if (!intf || !intf->dev.driver)
897 strncpy(gd.driver, intf->dev.driver->name,
899 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
904 static int proc_connectinfo(struct dev_state *ps, void __user *arg)
906 struct usbdevfs_connectinfo ci;
908 ci.devnum = ps->dev->devnum;
909 ci.slow = ps->dev->speed == USB_SPEED_LOW;
910 if (copy_to_user(arg, &ci, sizeof(ci)))
915 static int proc_resetdevice(struct dev_state *ps)
917 return usb_reset_device(ps->dev);
920 static int proc_setintf(struct dev_state *ps, void __user *arg)
922 struct usbdevfs_setinterface setintf;
925 if (copy_from_user(&setintf, arg, sizeof(setintf)))
927 if ((ret = checkintf(ps, setintf.interface)))
929 return usb_set_interface(ps->dev, setintf.interface,
933 static int proc_setconfig(struct dev_state *ps, void __user *arg)
937 struct usb_host_config *actconfig;
939 if (get_user(u, (int __user *)arg))
942 actconfig = ps->dev->actconfig;
944 /* Don't touch the device if any interfaces are claimed.
945 * It could interfere with other drivers' operations, and if
946 * an interface is claimed by usbfs it could easily deadlock.
951 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
952 if (usb_interface_claimed(actconfig->interface[i])) {
953 dev_warn(&ps->dev->dev,
954 "usbfs: interface %d claimed by %s "
955 "while '%s' sets config #%d\n",
956 actconfig->interface[i]
958 ->desc.bInterfaceNumber,
959 actconfig->interface[i]
968 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
969 * so avoid usb_set_configuration()'s kick to sysfs
972 if (actconfig && actconfig->desc.bConfigurationValue == u)
973 status = usb_reset_configuration(ps->dev);
975 status = usb_set_configuration(ps->dev, u);
981 static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
982 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
985 struct usbdevfs_iso_packet_desc *isopkt = NULL;
986 struct usb_host_endpoint *ep;
988 struct usb_ctrlrequest *dr = NULL;
989 const struct cred *cred = current_cred();
990 unsigned int u, totlen, isofrmlen;
994 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
995 USBDEVFS_URB_SHORT_NOT_OK |
996 USBDEVFS_URB_NO_FSBR |
997 USBDEVFS_URB_ZERO_PACKET |
998 USBDEVFS_URB_NO_INTERRUPT))
1000 if (uurb->buffer_length > 0 && !uurb->buffer)
1002 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
1003 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
1004 ifnum = findintfep(ps->dev, uurb->endpoint);
1007 ret = checkintf(ps, ifnum);
1011 if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
1013 ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
1016 ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
1020 switch(uurb->type) {
1021 case USBDEVFS_URB_TYPE_CONTROL:
1022 if (!usb_endpoint_xfer_control(&ep->desc))
1024 /* min 8 byte setup packet,
1025 * max 8 byte setup plus an arbitrary data stage */
1026 if (uurb->buffer_length < 8 ||
1027 uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE))
1029 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1032 if (copy_from_user(dr, uurb->buffer, 8)) {
1036 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
1040 ret = check_ctrlrecip(ps, dr->bRequestType,
1041 le16_to_cpup(&dr->wIndex));
1046 uurb->number_of_packets = 0;
1047 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1049 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1051 uurb->endpoint |= USB_DIR_IN;
1054 uurb->endpoint &= ~USB_DIR_IN;
1056 snoop(&ps->dev->dev, "control urb: bRequest=%02x "
1057 "bRrequestType=%02x wValue=%04x "
1058 "wIndex=%04x wLength=%04x\n",
1059 dr->bRequest, dr->bRequestType,
1060 __le16_to_cpup(&dr->wValue),
1061 __le16_to_cpup(&dr->wIndex),
1062 __le16_to_cpup(&dr->wLength));
1065 case USBDEVFS_URB_TYPE_BULK:
1066 switch (usb_endpoint_type(&ep->desc)) {
1067 case USB_ENDPOINT_XFER_CONTROL:
1068 case USB_ENDPOINT_XFER_ISOC:
1070 /* allow single-shot interrupt transfers, at bogus rates */
1072 uurb->number_of_packets = 0;
1073 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1075 snoop(&ps->dev->dev, "bulk urb\n");
1078 case USBDEVFS_URB_TYPE_ISO:
1079 /* arbitrary limit */
1080 if (uurb->number_of_packets < 1 ||
1081 uurb->number_of_packets > 128)
1083 if (!usb_endpoint_xfer_isoc(&ep->desc))
1085 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1086 uurb->number_of_packets;
1087 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
1089 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
1093 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1095 * sufficient for USB 2.0 high-bandwidth iso */
1096 if (isopkt[u].length > 8192) {
1100 totlen += isopkt[u].length;
1102 if (totlen > 32768) {
1106 uurb->buffer_length = totlen;
1107 snoop(&ps->dev->dev, "iso urb\n");
1110 case USBDEVFS_URB_TYPE_INTERRUPT:
1111 uurb->number_of_packets = 0;
1112 if (!usb_endpoint_xfer_int(&ep->desc))
1114 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1116 snoop(&ps->dev->dev, "interrupt urb\n");
1122 if (uurb->buffer_length > 0 &&
1123 !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1124 uurb->buffer, uurb->buffer_length)) {
1129 as = alloc_async(uurb->number_of_packets);
1135 if (uurb->buffer_length > 0) {
1136 as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
1138 if (!as->urb->transfer_buffer) {
1145 as->urb->dev = ps->dev;
1146 as->urb->pipe = (uurb->type << 30) |
1147 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1148 (uurb->endpoint & USB_DIR_IN);
1150 /* This tedious sequence is necessary because the URB_* flags
1151 * are internal to the kernel and subject to change, whereas
1152 * the USBDEVFS_URB_* flags are a user API and must not be changed.
1154 u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
1155 if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
1157 if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
1158 u |= URB_SHORT_NOT_OK;
1159 if (uurb->flags & USBDEVFS_URB_NO_FSBR)
1161 if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1162 u |= URB_ZERO_PACKET;
1163 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
1164 u |= URB_NO_INTERRUPT;
1165 as->urb->transfer_flags = u;
1167 as->urb->transfer_buffer_length = uurb->buffer_length;
1168 as->urb->setup_packet = (unsigned char *)dr;
1169 as->urb->start_frame = uurb->start_frame;
1170 as->urb->number_of_packets = uurb->number_of_packets;
1171 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1172 ps->dev->speed == USB_SPEED_HIGH)
1173 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
1175 as->urb->interval = ep->desc.bInterval;
1176 as->urb->context = as;
1177 as->urb->complete = async_completed;
1178 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1179 as->urb->iso_frame_desc[u].offset = totlen;
1180 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1181 totlen += isopkt[u].length;
1186 if (is_in && uurb->buffer_length > 0)
1187 as->userbuffer = uurb->buffer;
1189 as->userbuffer = NULL;
1190 as->signr = uurb->signr;
1192 as->pid = get_pid(task_pid(current));
1193 as->uid = cred->uid;
1194 as->euid = cred->euid;
1195 security_task_getsecid(current, &as->secid);
1196 if (!is_in && uurb->buffer_length > 0) {
1197 if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
1198 uurb->buffer_length)) {
1203 snoop_urb(as->urb, as->userurb);
1204 async_newpending(as);
1205 if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
1206 dev_printk(KERN_DEBUG, &ps->dev->dev,
1207 "usbfs: usb_submit_urb returned %d\n", ret);
1208 async_removepending(as);
1215 static int proc_submiturb(struct dev_state *ps, void __user *arg)
1217 struct usbdevfs_urb uurb;
1219 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1222 return proc_do_submiturb(ps, &uurb,
1223 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1227 static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
1231 as = async_getpending(ps, arg);
1234 usb_kill_urb(as->urb);
1238 static int processcompl(struct async *as, void __user * __user *arg)
1240 struct urb *urb = as->urb;
1241 struct usbdevfs_urb __user *userurb = as->userurb;
1242 void __user *addr = as->userurb;
1246 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1247 urb->transfer_buffer_length))
1249 if (put_user(as->status, &userurb->status))
1251 if (put_user(urb->actual_length, &userurb->actual_length))
1253 if (put_user(urb->error_count, &userurb->error_count))
1256 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1257 for (i = 0; i < urb->number_of_packets; i++) {
1258 if (put_user(urb->iso_frame_desc[i].actual_length,
1259 &userurb->iso_frame_desc[i].actual_length))
1261 if (put_user(urb->iso_frame_desc[i].status,
1262 &userurb->iso_frame_desc[i].status))
1269 if (put_user(addr, (void __user * __user *)arg))
1278 static struct async *reap_as(struct dev_state *ps)
1280 DECLARE_WAITQUEUE(wait, current);
1281 struct async *as = NULL;
1282 struct usb_device *dev = ps->dev;
1284 add_wait_queue(&ps->wait, &wait);
1286 __set_current_state(TASK_INTERRUPTIBLE);
1287 as = async_getcompleted(ps);
1290 if (signal_pending(current))
1292 usb_unlock_device(dev);
1294 usb_lock_device(dev);
1296 remove_wait_queue(&ps->wait, &wait);
1297 set_current_state(TASK_RUNNING);
1301 static int proc_reapurb(struct dev_state *ps, void __user *arg)
1303 struct async *as = reap_as(ps);
1305 return processcompl(as, (void __user * __user *)arg);
1306 if (signal_pending(current))
1311 static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
1315 if (!(as = async_getcompleted(ps)))
1317 return processcompl(as, (void __user * __user *)arg);
1320 #ifdef CONFIG_COMPAT
1322 static int get_urb32(struct usbdevfs_urb *kurb,
1323 struct usbdevfs_urb32 __user *uurb)
1326 if (!access_ok(VERIFY_READ, uurb, sizeof(*uurb)) ||
1327 __get_user(kurb->type, &uurb->type) ||
1328 __get_user(kurb->endpoint, &uurb->endpoint) ||
1329 __get_user(kurb->status, &uurb->status) ||
1330 __get_user(kurb->flags, &uurb->flags) ||
1331 __get_user(kurb->buffer_length, &uurb->buffer_length) ||
1332 __get_user(kurb->actual_length, &uurb->actual_length) ||
1333 __get_user(kurb->start_frame, &uurb->start_frame) ||
1334 __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
1335 __get_user(kurb->error_count, &uurb->error_count) ||
1336 __get_user(kurb->signr, &uurb->signr))
1339 if (__get_user(uptr, &uurb->buffer))
1341 kurb->buffer = compat_ptr(uptr);
1342 if (__get_user(uptr, &uurb->usercontext))
1344 kurb->usercontext = compat_ptr(uptr);
1349 static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
1351 struct usbdevfs_urb uurb;
1353 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
1356 return proc_do_submiturb(ps, &uurb,
1357 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
1361 static int processcompl_compat(struct async *as, void __user * __user *arg)
1363 struct urb *urb = as->urb;
1364 struct usbdevfs_urb32 __user *userurb = as->userurb;
1365 void __user *addr = as->userurb;
1369 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1370 urb->transfer_buffer_length))
1372 if (put_user(as->status, &userurb->status))
1374 if (put_user(urb->actual_length, &userurb->actual_length))
1376 if (put_user(urb->error_count, &userurb->error_count))
1379 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1380 for (i = 0; i < urb->number_of_packets; i++) {
1381 if (put_user(urb->iso_frame_desc[i].actual_length,
1382 &userurb->iso_frame_desc[i].actual_length))
1384 if (put_user(urb->iso_frame_desc[i].status,
1385 &userurb->iso_frame_desc[i].status))
1391 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
1396 static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
1398 struct async *as = reap_as(ps);
1400 return processcompl_compat(as, (void __user * __user *)arg);
1401 if (signal_pending(current))
1406 static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
1410 if (!(as = async_getcompleted(ps)))
1412 return processcompl_compat(as, (void __user * __user *)arg);
1417 static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
1419 struct usbdevfs_disconnectsignal ds;
1421 if (copy_from_user(&ds, arg, sizeof(ds)))
1423 ps->discsignr = ds.signr;
1424 ps->disccontext = ds.context;
1428 static int proc_claiminterface(struct dev_state *ps, void __user *arg)
1432 if (get_user(ifnum, (unsigned int __user *)arg))
1434 return claimintf(ps, ifnum);
1437 static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
1442 if (get_user(ifnum, (unsigned int __user *)arg))
1444 if ((ret = releaseintf(ps, ifnum)) < 0)
1446 destroy_async_on_interface (ps, ifnum);
1450 static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
1455 struct usb_interface *intf = NULL;
1456 struct usb_driver *driver = NULL;
1459 if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
1460 if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
1462 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
1463 if (copy_from_user(buf, ctl->data, size)) {
1468 memset(buf, 0, size);
1472 if (!connected(ps)) {
1477 if (ps->dev->state != USB_STATE_CONFIGURED)
1478 retval = -EHOSTUNREACH;
1479 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
1481 else switch (ctl->ioctl_code) {
1483 /* disconnect kernel driver from interface */
1484 case USBDEVFS_DISCONNECT:
1485 if (intf->dev.driver) {
1486 driver = to_usb_driver(intf->dev.driver);
1487 dev_dbg(&intf->dev, "disconnect by usbfs\n");
1488 usb_driver_release_interface(driver, intf);
1493 /* let kernel drivers try to (re)bind to the interface */
1494 case USBDEVFS_CONNECT:
1495 if (!intf->dev.driver)
1496 retval = device_attach(&intf->dev);
1501 /* talk directly to the interface's driver */
1503 if (intf->dev.driver)
1504 driver = to_usb_driver(intf->dev.driver);
1505 if (driver == NULL || driver->ioctl == NULL) {
1508 retval = driver->ioctl(intf, ctl->ioctl_code, buf);
1509 if (retval == -ENOIOCTLCMD)
1514 /* cleanup and return */
1516 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
1518 && copy_to_user(ctl->data, buf, size) != 0)
1525 static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
1527 struct usbdevfs_ioctl ctrl;
1529 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1531 return proc_ioctl(ps, &ctrl);
1534 #ifdef CONFIG_COMPAT
1535 static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
1537 struct usbdevfs_ioctl32 __user *uioc;
1538 struct usbdevfs_ioctl ctrl;
1541 uioc = compat_ptr((long)arg);
1542 if (!access_ok(VERIFY_READ, uioc, sizeof(*uioc)) ||
1543 __get_user(ctrl.ifno, &uioc->ifno) ||
1544 __get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
1545 __get_user(udata, &uioc->data))
1547 ctrl.data = compat_ptr(udata);
1549 return proc_ioctl(ps, &ctrl);
1553 static int proc_claim_port(struct dev_state *ps, void __user *arg)
1558 if (get_user(portnum, (unsigned __user *) arg))
1560 rc = usb_hub_claim_port(ps->dev, portnum, ps);
1562 snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n",
1563 portnum, task_pid_nr(current), current->comm);
1567 static int proc_release_port(struct dev_state *ps, void __user *arg)
1571 if (get_user(portnum, (unsigned __user *) arg))
1573 return usb_hub_release_port(ps->dev, portnum, ps);
1577 * NOTE: All requests here that have interface numbers as parameters
1578 * are assuming that somehow the configuration has been prevented from
1579 * changing. But there's no mechanism to ensure that...
1581 static int usbdev_ioctl(struct inode *inode, struct file *file,
1582 unsigned int cmd, unsigned long arg)
1584 struct dev_state *ps = file->private_data;
1585 struct usb_device *dev = ps->dev;
1586 void __user *p = (void __user *)arg;
1589 if (!(file->f_mode & FMODE_WRITE))
1591 usb_lock_device(dev);
1592 if (!connected(ps)) {
1593 usb_unlock_device(dev);
1598 case USBDEVFS_CONTROL:
1599 snoop(&dev->dev, "%s: CONTROL\n", __func__);
1600 ret = proc_control(ps, p);
1602 inode->i_mtime = CURRENT_TIME;
1606 snoop(&dev->dev, "%s: BULK\n", __func__);
1607 ret = proc_bulk(ps, p);
1609 inode->i_mtime = CURRENT_TIME;
1612 case USBDEVFS_RESETEP:
1613 snoop(&dev->dev, "%s: RESETEP\n", __func__);
1614 ret = proc_resetep(ps, p);
1616 inode->i_mtime = CURRENT_TIME;
1619 case USBDEVFS_RESET:
1620 snoop(&dev->dev, "%s: RESET\n", __func__);
1621 ret = proc_resetdevice(ps);
1624 case USBDEVFS_CLEAR_HALT:
1625 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
1626 ret = proc_clearhalt(ps, p);
1628 inode->i_mtime = CURRENT_TIME;
1631 case USBDEVFS_GETDRIVER:
1632 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
1633 ret = proc_getdriver(ps, p);
1636 case USBDEVFS_CONNECTINFO:
1637 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
1638 ret = proc_connectinfo(ps, p);
1641 case USBDEVFS_SETINTERFACE:
1642 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
1643 ret = proc_setintf(ps, p);
1646 case USBDEVFS_SETCONFIGURATION:
1647 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
1648 ret = proc_setconfig(ps, p);
1651 case USBDEVFS_SUBMITURB:
1652 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
1653 ret = proc_submiturb(ps, p);
1655 inode->i_mtime = CURRENT_TIME;
1658 #ifdef CONFIG_COMPAT
1660 case USBDEVFS_SUBMITURB32:
1661 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
1662 ret = proc_submiturb_compat(ps, p);
1664 inode->i_mtime = CURRENT_TIME;
1667 case USBDEVFS_REAPURB32:
1668 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
1669 ret = proc_reapurb_compat(ps, p);
1672 case USBDEVFS_REAPURBNDELAY32:
1673 snoop(&dev->dev, "%s: REAPURBDELAY32\n", __func__);
1674 ret = proc_reapurbnonblock_compat(ps, p);
1677 case USBDEVFS_IOCTL32:
1678 snoop(&dev->dev, "%s: IOCTL\n", __func__);
1679 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
1683 case USBDEVFS_DISCARDURB:
1684 snoop(&dev->dev, "%s: DISCARDURB\n", __func__);
1685 ret = proc_unlinkurb(ps, p);
1688 case USBDEVFS_REAPURB:
1689 snoop(&dev->dev, "%s: REAPURB\n", __func__);
1690 ret = proc_reapurb(ps, p);
1693 case USBDEVFS_REAPURBNDELAY:
1694 snoop(&dev->dev, "%s: REAPURBDELAY\n", __func__);
1695 ret = proc_reapurbnonblock(ps, p);
1698 case USBDEVFS_DISCSIGNAL:
1699 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
1700 ret = proc_disconnectsignal(ps, p);
1703 case USBDEVFS_CLAIMINTERFACE:
1704 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
1705 ret = proc_claiminterface(ps, p);
1708 case USBDEVFS_RELEASEINTERFACE:
1709 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
1710 ret = proc_releaseinterface(ps, p);
1713 case USBDEVFS_IOCTL:
1714 snoop(&dev->dev, "%s: IOCTL\n", __func__);
1715 ret = proc_ioctl_default(ps, p);
1718 case USBDEVFS_CLAIM_PORT:
1719 snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__);
1720 ret = proc_claim_port(ps, p);
1723 case USBDEVFS_RELEASE_PORT:
1724 snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__);
1725 ret = proc_release_port(ps, p);
1728 usb_unlock_device(dev);
1730 inode->i_atime = CURRENT_TIME;
1734 /* No kernel lock - fine */
1735 static unsigned int usbdev_poll(struct file *file,
1736 struct poll_table_struct *wait)
1738 struct dev_state *ps = file->private_data;
1739 unsigned int mask = 0;
1741 poll_wait(file, &ps->wait, wait);
1742 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
1743 mask |= POLLOUT | POLLWRNORM;
1745 mask |= POLLERR | POLLHUP;
1749 const struct file_operations usbdev_file_operations = {
1750 .owner = THIS_MODULE,
1751 .llseek = usbdev_lseek,
1752 .read = usbdev_read,
1753 .poll = usbdev_poll,
1754 .ioctl = usbdev_ioctl,
1755 .open = usbdev_open,
1756 .release = usbdev_release,
1759 static void usbdev_remove(struct usb_device *udev)
1761 struct dev_state *ps;
1762 struct siginfo sinfo;
1764 while (!list_empty(&udev->filelist)) {
1765 ps = list_entry(udev->filelist.next, struct dev_state, list);
1766 destroy_all_async(ps);
1767 wake_up_all(&ps->wait);
1768 list_del_init(&ps->list);
1769 if (ps->discsignr) {
1770 sinfo.si_signo = ps->discsignr;
1771 sinfo.si_errno = EPIPE;
1772 sinfo.si_code = SI_ASYNCIO;
1773 sinfo.si_addr = ps->disccontext;
1774 kill_pid_info_as_uid(ps->discsignr, &sinfo,
1775 ps->disc_pid, ps->disc_uid,
1776 ps->disc_euid, ps->secid);
1781 #ifdef CONFIG_USB_DEVICE_CLASS
1782 static struct class *usb_classdev_class;
1784 static int usb_classdev_add(struct usb_device *dev)
1786 struct device *cldev;
1788 cldev = device_create(usb_classdev_class, &dev->dev, dev->dev.devt,
1789 NULL, "usbdev%d.%d", dev->bus->busnum,
1792 return PTR_ERR(cldev);
1793 dev->usb_classdev = cldev;
1797 static void usb_classdev_remove(struct usb_device *dev)
1799 if (dev->usb_classdev)
1800 device_unregister(dev->usb_classdev);
1804 #define usb_classdev_add(dev) 0
1805 #define usb_classdev_remove(dev) do {} while (0)
1809 static int usbdev_notify(struct notifier_block *self,
1810 unsigned long action, void *dev)
1813 case USB_DEVICE_ADD:
1814 if (usb_classdev_add(dev))
1817 case USB_DEVICE_REMOVE:
1818 usb_classdev_remove(dev);
1825 static struct notifier_block usbdev_nb = {
1826 .notifier_call = usbdev_notify,
1829 static struct cdev usb_device_cdev;
1831 int __init usb_devio_init(void)
1835 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
1838 printk(KERN_ERR "Unable to register minors for usb_device\n");
1841 cdev_init(&usb_device_cdev, &usbdev_file_operations);
1842 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
1844 printk(KERN_ERR "Unable to get usb_device major %d\n",
1848 #ifdef CONFIG_USB_DEVICE_CLASS
1849 usb_classdev_class = class_create(THIS_MODULE, "usb_device");
1850 if (IS_ERR(usb_classdev_class)) {
1851 printk(KERN_ERR "Unable to register usb_device class\n");
1852 retval = PTR_ERR(usb_classdev_class);
1853 cdev_del(&usb_device_cdev);
1854 usb_classdev_class = NULL;
1857 /* devices of this class shadow the major:minor of their parent
1858 * device, so clear ->dev_kobj to prevent adding duplicate entries
1861 usb_classdev_class->dev_kobj = NULL;
1863 usb_register_notify(&usbdev_nb);
1868 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
1872 void usb_devio_cleanup(void)
1874 usb_unregister_notify(&usbdev_nb);
1875 #ifdef CONFIG_USB_DEVICE_CLASS
1876 class_destroy(usb_classdev_class);
1878 cdev_del(&usb_device_cdev);
1879 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);