2 * drivers/usb/core/sysfs.c
4 * (C) Copyright 2002 David Brownell
5 * (C) Copyright 2002,2004 Greg Kroah-Hartman
6 * (C) Copyright 2002,2004 IBM Corp.
8 * All of the sysfs file attributes for usb devices and interfaces.
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/usb.h>
18 /* Active configuration fields */
19 #define usb_actconfig_show(field, multiplier, format_string) \
20 static ssize_t show_##field(struct device *dev, \
21 struct device_attribute *attr, char *buf) \
23 struct usb_device *udev; \
24 struct usb_host_config *actconfig; \
26 udev = to_usb_device(dev); \
27 actconfig = udev->actconfig; \
29 return sprintf(buf, format_string, \
30 actconfig->desc.field * multiplier); \
35 #define usb_actconfig_attr(field, multiplier, format_string) \
36 usb_actconfig_show(field, multiplier, format_string) \
37 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
39 usb_actconfig_attr(bNumInterfaces, 1, "%2d\n")
40 usb_actconfig_attr(bmAttributes, 1, "%2x\n")
41 usb_actconfig_attr(bMaxPower, 2, "%3dmA\n")
43 static ssize_t show_configuration_string(struct device *dev,
44 struct device_attribute *attr, char *buf)
46 struct usb_device *udev;
47 struct usb_host_config *actconfig;
49 udev = to_usb_device(dev);
50 actconfig = udev->actconfig;
51 if ((!actconfig) || (!actconfig->string))
53 return sprintf(buf, "%s\n", actconfig->string);
55 static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL);
57 /* configuration value is always present, and r/w */
58 usb_actconfig_show(bConfigurationValue, 1, "%u\n");
61 set_bConfigurationValue(struct device *dev, struct device_attribute *attr,
62 const char *buf, size_t count)
64 struct usb_device *udev = to_usb_device(dev);
67 if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
69 usb_lock_device(udev);
70 value = usb_set_configuration(udev, config);
71 usb_unlock_device(udev);
72 return (value < 0) ? value : count;
75 static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR,
76 show_bConfigurationValue, set_bConfigurationValue);
79 #define usb_string_attr(name) \
80 static ssize_t show_##name(struct device *dev, \
81 struct device_attribute *attr, char *buf) \
83 struct usb_device *udev; \
85 udev = to_usb_device(dev); \
86 return sprintf(buf, "%s\n", udev->name); \
88 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
90 usb_string_attr(product);
91 usb_string_attr(manufacturer);
92 usb_string_attr(serial);
95 show_speed(struct device *dev, struct device_attribute *attr, char *buf)
97 struct usb_device *udev;
100 udev = to_usb_device(dev);
102 switch (udev->speed) {
106 case USB_SPEED_UNKNOWN:
116 return sprintf(buf, "%s\n", speed);
118 static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
121 show_busnum(struct device *dev, struct device_attribute *attr, char *buf)
123 struct usb_device *udev;
125 udev = to_usb_device(dev);
126 return sprintf(buf, "%d\n", udev->bus->busnum);
128 static DEVICE_ATTR(busnum, S_IRUGO, show_busnum, NULL);
131 show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
133 struct usb_device *udev;
135 udev = to_usb_device(dev);
136 return sprintf(buf, "%d\n", udev->devnum);
138 static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
141 show_version(struct device *dev, struct device_attribute *attr, char *buf)
143 struct usb_device *udev;
146 udev = to_usb_device(dev);
147 bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
148 return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
150 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
153 show_maxchild(struct device *dev, struct device_attribute *attr, char *buf)
155 struct usb_device *udev;
157 udev = to_usb_device(dev);
158 return sprintf(buf, "%d\n", udev->maxchild);
160 static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
163 show_quirks(struct device *dev, struct device_attribute *attr, char *buf)
165 struct usb_device *udev;
167 udev = to_usb_device(dev);
168 return sprintf(buf, "0x%x\n", udev->quirks);
170 static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL);
173 show_urbnum(struct device *dev, struct device_attribute *attr, char *buf)
175 struct usb_device *udev;
177 udev = to_usb_device(dev);
178 return sprintf(buf, "%d\n", atomic_read(&udev->urbnum));
180 static DEVICE_ATTR(urbnum, S_IRUGO, show_urbnum, NULL);
185 static const char power_group[] = "power";
188 show_persist(struct device *dev, struct device_attribute *attr, char *buf)
190 struct usb_device *udev = to_usb_device(dev);
192 return sprintf(buf, "%d\n", udev->persist_enabled);
196 set_persist(struct device *dev, struct device_attribute *attr,
197 const char *buf, size_t count)
199 struct usb_device *udev = to_usb_device(dev);
202 /* Hubs are always enabled for USB_PERSIST */
203 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
206 if (sscanf(buf, "%d", &value) != 1)
209 udev->persist_enabled = !!value;
214 static DEVICE_ATTR(persist, S_IRUGO | S_IWUSR, show_persist, set_persist);
216 static int add_persist_attributes(struct device *dev)
220 if (is_usb_device(dev)) {
221 struct usb_device *udev = to_usb_device(dev);
223 /* Hubs are automatically enabled for USB_PERSIST,
224 * no point in creating the attribute file.
226 if (udev->descriptor.bDeviceClass != USB_CLASS_HUB)
227 rc = sysfs_add_file_to_group(&dev->kobj,
228 &dev_attr_persist.attr,
234 static void remove_persist_attributes(struct device *dev)
236 sysfs_remove_file_from_group(&dev->kobj,
237 &dev_attr_persist.attr,
242 #define add_persist_attributes(dev) 0
243 #define remove_persist_attributes(dev) do {} while (0)
245 #endif /* CONFIG_PM */
247 #ifdef CONFIG_USB_SUSPEND
250 show_connected_duration(struct device *dev, struct device_attribute *attr,
253 struct usb_device *udev = to_usb_device(dev);
255 return sprintf(buf, "%u\n",
256 jiffies_to_msecs(jiffies - udev->connect_time));
259 static DEVICE_ATTR(connected_duration, S_IRUGO, show_connected_duration, NULL);
262 * If the device is resumed, the last time the device was suspended has
263 * been pre-subtracted from active_duration. We add the current time to
264 * get the duration that the device was actually active.
266 * If the device is suspended, the active_duration is up-to-date.
269 show_active_duration(struct device *dev, struct device_attribute *attr,
272 struct usb_device *udev = to_usb_device(dev);
275 if (udev->state != USB_STATE_SUSPENDED)
276 duration = jiffies_to_msecs(jiffies + udev->active_duration);
278 duration = jiffies_to_msecs(udev->active_duration);
279 return sprintf(buf, "%u\n", duration);
282 static DEVICE_ATTR(active_duration, S_IRUGO, show_active_duration, NULL);
285 show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf)
287 struct usb_device *udev = to_usb_device(dev);
289 return sprintf(buf, "%d\n", udev->autosuspend_delay / HZ);
293 set_autosuspend(struct device *dev, struct device_attribute *attr,
294 const char *buf, size_t count)
296 struct usb_device *udev = to_usb_device(dev);
299 if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ ||
300 value <= - INT_MAX/HZ)
304 udev->autosuspend_delay = value;
306 usb_try_autosuspend_device(udev);
308 if (usb_autoresume_device(udev) == 0)
309 usb_autosuspend_device(udev);
314 static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
315 show_autosuspend, set_autosuspend);
317 static const char on_string[] = "on";
318 static const char auto_string[] = "auto";
319 static const char suspend_string[] = "suspend";
322 show_level(struct device *dev, struct device_attribute *attr, char *buf)
324 struct usb_device *udev = to_usb_device(dev);
325 const char *p = auto_string;
327 if (udev->state == USB_STATE_SUSPENDED) {
328 if (udev->autoresume_disabled)
331 if (udev->autosuspend_disabled)
334 return sprintf(buf, "%s\n", p);
338 set_level(struct device *dev, struct device_attribute *attr,
339 const char *buf, size_t count)
341 struct usb_device *udev = to_usb_device(dev);
345 int old_autosuspend_disabled, old_autoresume_disabled;
347 cp = memchr(buf, '\n', count);
351 usb_lock_device(udev);
352 old_autosuspend_disabled = udev->autosuspend_disabled;
353 old_autoresume_disabled = udev->autoresume_disabled;
355 /* Setting the flags without calling usb_pm_lock is a subject to
356 * races, but who cares...
358 if (len == sizeof on_string - 1 &&
359 strncmp(buf, on_string, len) == 0) {
360 udev->autosuspend_disabled = 1;
361 udev->autoresume_disabled = 0;
362 rc = usb_external_resume_device(udev);
364 } else if (len == sizeof auto_string - 1 &&
365 strncmp(buf, auto_string, len) == 0) {
366 udev->autosuspend_disabled = 0;
367 udev->autoresume_disabled = 0;
368 rc = usb_external_resume_device(udev);
370 } else if (len == sizeof suspend_string - 1 &&
371 strncmp(buf, suspend_string, len) == 0) {
372 udev->autosuspend_disabled = 0;
373 udev->autoresume_disabled = 1;
374 rc = usb_external_suspend_device(udev, PMSG_SUSPEND);
380 udev->autosuspend_disabled = old_autosuspend_disabled;
381 udev->autoresume_disabled = old_autoresume_disabled;
383 usb_unlock_device(udev);
384 return (rc < 0 ? rc : count);
387 static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level);
389 static int add_power_attributes(struct device *dev)
393 if (is_usb_device(dev)) {
394 rc = sysfs_add_file_to_group(&dev->kobj,
395 &dev_attr_autosuspend.attr,
398 rc = sysfs_add_file_to_group(&dev->kobj,
399 &dev_attr_level.attr,
402 rc = sysfs_add_file_to_group(&dev->kobj,
403 &dev_attr_connected_duration.attr,
406 rc = sysfs_add_file_to_group(&dev->kobj,
407 &dev_attr_active_duration.attr,
413 static void remove_power_attributes(struct device *dev)
415 sysfs_remove_file_from_group(&dev->kobj,
416 &dev_attr_active_duration.attr,
418 sysfs_remove_file_from_group(&dev->kobj,
419 &dev_attr_connected_duration.attr,
421 sysfs_remove_file_from_group(&dev->kobj,
422 &dev_attr_level.attr,
424 sysfs_remove_file_from_group(&dev->kobj,
425 &dev_attr_autosuspend.attr,
431 #define add_power_attributes(dev) 0
432 #define remove_power_attributes(dev) do {} while (0)
434 #endif /* CONFIG_USB_SUSPEND */
437 /* Descriptor fields */
438 #define usb_descriptor_attr_le16(field, format_string) \
440 show_##field(struct device *dev, struct device_attribute *attr, \
443 struct usb_device *udev; \
445 udev = to_usb_device(dev); \
446 return sprintf(buf, format_string, \
447 le16_to_cpu(udev->descriptor.field)); \
449 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
451 usb_descriptor_attr_le16(idVendor, "%04x\n")
452 usb_descriptor_attr_le16(idProduct, "%04x\n")
453 usb_descriptor_attr_le16(bcdDevice, "%04x\n")
455 #define usb_descriptor_attr(field, format_string) \
457 show_##field(struct device *dev, struct device_attribute *attr, \
460 struct usb_device *udev; \
462 udev = to_usb_device(dev); \
463 return sprintf(buf, format_string, udev->descriptor.field); \
465 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
467 usb_descriptor_attr(bDeviceClass, "%02x\n")
468 usb_descriptor_attr(bDeviceSubClass, "%02x\n")
469 usb_descriptor_attr(bDeviceProtocol, "%02x\n")
470 usb_descriptor_attr(bNumConfigurations, "%d\n")
471 usb_descriptor_attr(bMaxPacketSize0, "%d\n")
475 /* show if the device is authorized (1) or not (0) */
476 static ssize_t usb_dev_authorized_show(struct device *dev,
477 struct device_attribute *attr,
480 struct usb_device *usb_dev = to_usb_device(dev);
481 return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
486 * Authorize a device to be used in the system
488 * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
490 static ssize_t usb_dev_authorized_store(struct device *dev,
491 struct device_attribute *attr,
492 const char *buf, size_t size)
495 struct usb_device *usb_dev = to_usb_device(dev);
497 result = sscanf(buf, "%u\n", &val);
501 result = usb_deauthorize_device(usb_dev);
503 result = usb_authorize_device(usb_dev);
504 return result < 0? result : size;
507 static DEVICE_ATTR(authorized, 0644,
508 usb_dev_authorized_show, usb_dev_authorized_store);
511 static struct attribute *dev_attrs[] = {
512 /* current configuration's attributes */
513 &dev_attr_configuration.attr,
514 &dev_attr_bNumInterfaces.attr,
515 &dev_attr_bConfigurationValue.attr,
516 &dev_attr_bmAttributes.attr,
517 &dev_attr_bMaxPower.attr,
518 &dev_attr_urbnum.attr,
519 /* device attributes */
520 &dev_attr_idVendor.attr,
521 &dev_attr_idProduct.attr,
522 &dev_attr_bcdDevice.attr,
523 &dev_attr_bDeviceClass.attr,
524 &dev_attr_bDeviceSubClass.attr,
525 &dev_attr_bDeviceProtocol.attr,
526 &dev_attr_bNumConfigurations.attr,
527 &dev_attr_bMaxPacketSize0.attr,
528 &dev_attr_speed.attr,
529 &dev_attr_busnum.attr,
530 &dev_attr_devnum.attr,
531 &dev_attr_version.attr,
532 &dev_attr_maxchild.attr,
533 &dev_attr_quirks.attr,
534 &dev_attr_authorized.attr,
537 static struct attribute_group dev_attr_grp = {
541 /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
544 static struct attribute *dev_string_attrs[] = {
545 &dev_attr_manufacturer.attr,
546 &dev_attr_product.attr,
547 &dev_attr_serial.attr,
551 static mode_t dev_string_attrs_are_visible(struct kobject *kobj,
552 struct attribute *a, int n)
554 struct usb_device *udev = to_usb_device(
555 container_of(kobj, struct device, kobj));
557 if (a == &dev_attr_manufacturer.attr) {
558 if (udev->manufacturer == NULL)
560 } else if (a == &dev_attr_product.attr) {
561 if (udev->product == NULL)
563 } else if (a == &dev_attr_serial.attr) {
564 if (udev->serial == NULL)
570 static struct attribute_group dev_string_attr_grp = {
571 .attrs = dev_string_attrs,
572 .is_visible = dev_string_attrs_are_visible,
575 struct attribute_group *usb_device_groups[] = {
577 &dev_string_attr_grp,
581 /* Binary descriptors */
584 read_descriptors(struct kobject *kobj, struct bin_attribute *attr,
585 char *buf, loff_t off, size_t count)
587 struct usb_device *udev = to_usb_device(
588 container_of(kobj, struct device, kobj));
589 size_t nleft = count;
594 /* The binary attribute begins with the device descriptor.
595 * Following that are the raw descriptor entries for all the
596 * configurations (config plus subsidiary descriptors).
598 for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
599 nleft > 0; ++cfgno) {
601 src = &udev->descriptor;
602 srclen = sizeof(struct usb_device_descriptor);
604 src = udev->rawdescriptors[cfgno];
605 srclen = __le16_to_cpu(udev->config[cfgno].desc.
609 n = min(nleft, srclen - (size_t) off);
610 memcpy(buf, src + off, n);
618 return count - nleft;
621 static struct bin_attribute dev_bin_attr_descriptors = {
622 .attr = {.name = "descriptors", .mode = 0444},
623 .read = read_descriptors,
624 .size = 18 + 65535, /* dev descr + max-size raw descriptor */
627 int usb_create_sysfs_dev_files(struct usb_device *udev)
629 struct device *dev = &udev->dev;
632 /* Unforunately these attributes cannot be created before
633 * the uevent is broadcast.
635 retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
639 retval = add_persist_attributes(dev);
643 retval = add_power_attributes(dev);
647 retval = usb_create_ep_files(dev, &udev->ep0, udev);
652 usb_remove_sysfs_dev_files(udev);
656 void usb_remove_sysfs_dev_files(struct usb_device *udev)
658 struct device *dev = &udev->dev;
660 usb_remove_ep_files(&udev->ep0);
661 remove_power_attributes(dev);
662 remove_persist_attributes(dev);
663 device_remove_bin_file(dev, &dev_bin_attr_descriptors);
666 /* Interface Accociation Descriptor fields */
667 #define usb_intf_assoc_attr(field, format_string) \
669 show_iad_##field(struct device *dev, struct device_attribute *attr, \
672 struct usb_interface *intf = to_usb_interface(dev); \
674 return sprintf(buf, format_string, \
675 intf->intf_assoc->field); \
677 static DEVICE_ATTR(iad_##field, S_IRUGO, show_iad_##field, NULL);
679 usb_intf_assoc_attr(bFirstInterface, "%02x\n")
680 usb_intf_assoc_attr(bInterfaceCount, "%02d\n")
681 usb_intf_assoc_attr(bFunctionClass, "%02x\n")
682 usb_intf_assoc_attr(bFunctionSubClass, "%02x\n")
683 usb_intf_assoc_attr(bFunctionProtocol, "%02x\n")
685 /* Interface fields */
686 #define usb_intf_attr(field, format_string) \
688 show_##field(struct device *dev, struct device_attribute *attr, \
691 struct usb_interface *intf = to_usb_interface(dev); \
693 return sprintf(buf, format_string, \
694 intf->cur_altsetting->desc.field); \
696 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
698 usb_intf_attr(bInterfaceNumber, "%02x\n")
699 usb_intf_attr(bAlternateSetting, "%2d\n")
700 usb_intf_attr(bNumEndpoints, "%02x\n")
701 usb_intf_attr(bInterfaceClass, "%02x\n")
702 usb_intf_attr(bInterfaceSubClass, "%02x\n")
703 usb_intf_attr(bInterfaceProtocol, "%02x\n")
705 static ssize_t show_interface_string(struct device *dev,
706 struct device_attribute *attr, char *buf)
708 struct usb_interface *intf;
711 intf = to_usb_interface(dev);
712 string = intf->cur_altsetting->string;
713 barrier(); /* The altsetting might change! */
717 return sprintf(buf, "%s\n", string);
719 static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
721 static ssize_t show_modalias(struct device *dev,
722 struct device_attribute *attr, char *buf)
724 struct usb_interface *intf;
725 struct usb_device *udev;
726 struct usb_host_interface *alt;
728 intf = to_usb_interface(dev);
729 udev = interface_to_usbdev(intf);
730 alt = intf->cur_altsetting;
732 return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
733 "ic%02Xisc%02Xip%02X\n",
734 le16_to_cpu(udev->descriptor.idVendor),
735 le16_to_cpu(udev->descriptor.idProduct),
736 le16_to_cpu(udev->descriptor.bcdDevice),
737 udev->descriptor.bDeviceClass,
738 udev->descriptor.bDeviceSubClass,
739 udev->descriptor.bDeviceProtocol,
740 alt->desc.bInterfaceClass,
741 alt->desc.bInterfaceSubClass,
742 alt->desc.bInterfaceProtocol);
744 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
746 static struct attribute *intf_attrs[] = {
747 &dev_attr_bInterfaceNumber.attr,
748 &dev_attr_bAlternateSetting.attr,
749 &dev_attr_bNumEndpoints.attr,
750 &dev_attr_bInterfaceClass.attr,
751 &dev_attr_bInterfaceSubClass.attr,
752 &dev_attr_bInterfaceProtocol.attr,
753 &dev_attr_modalias.attr,
756 static struct attribute_group intf_attr_grp = {
760 static struct attribute *intf_assoc_attrs[] = {
761 &dev_attr_iad_bFirstInterface.attr,
762 &dev_attr_iad_bInterfaceCount.attr,
763 &dev_attr_iad_bFunctionClass.attr,
764 &dev_attr_iad_bFunctionSubClass.attr,
765 &dev_attr_iad_bFunctionProtocol.attr,
769 static mode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
770 struct attribute *a, int n)
772 struct usb_interface *intf = to_usb_interface(
773 container_of(kobj, struct device, kobj));
775 if (intf->intf_assoc == NULL)
780 static struct attribute_group intf_assoc_attr_grp = {
781 .attrs = intf_assoc_attrs,
782 .is_visible = intf_assoc_attrs_are_visible,
785 struct attribute_group *usb_interface_groups[] = {
787 &intf_assoc_attr_grp,
791 static inline void usb_create_intf_ep_files(struct usb_interface *intf,
792 struct usb_device *udev)
794 struct usb_host_interface *iface_desc;
797 iface_desc = intf->cur_altsetting;
798 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
799 usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i],
803 static inline void usb_remove_intf_ep_files(struct usb_interface *intf)
805 struct usb_host_interface *iface_desc;
808 iface_desc = intf->cur_altsetting;
809 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
810 usb_remove_ep_files(&iface_desc->endpoint[i]);
813 int usb_create_sysfs_intf_files(struct usb_interface *intf)
815 struct usb_device *udev = interface_to_usbdev(intf);
816 struct usb_host_interface *alt = intf->cur_altsetting;
819 if (intf->sysfs_files_created)
822 /* The interface string may be present in some altsettings
823 * and missing in others. Hence its attribute cannot be created
824 * before the uevent is broadcast.
826 if (alt->string == NULL)
827 alt->string = usb_cache_string(udev, alt->desc.iInterface);
829 retval = device_create_file(&intf->dev, &dev_attr_interface);
830 usb_create_intf_ep_files(intf, udev);
831 intf->sysfs_files_created = 1;
835 void usb_remove_sysfs_intf_files(struct usb_interface *intf)
837 struct device *dev = &intf->dev;
839 if (!intf->sysfs_files_created)
841 usb_remove_intf_ep_files(intf);
842 device_remove_file(dev, &dev_attr_interface);
843 intf->sysfs_files_created = 0;