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>
16 #include <linux/usb/quirks.h>
19 /* Active configuration fields */
20 #define usb_actconfig_show(field, multiplier, format_string) \
21 static ssize_t show_##field(struct device *dev, \
22 struct device_attribute *attr, char *buf) \
24 struct usb_device *udev; \
25 struct usb_host_config *actconfig; \
27 udev = to_usb_device(dev); \
28 actconfig = udev->actconfig; \
30 return sprintf(buf, format_string, \
31 actconfig->desc.field * multiplier); \
36 #define usb_actconfig_attr(field, multiplier, format_string) \
37 usb_actconfig_show(field, multiplier, format_string) \
38 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
40 usb_actconfig_attr(bNumInterfaces, 1, "%2d\n")
41 usb_actconfig_attr(bmAttributes, 1, "%2x\n")
42 usb_actconfig_attr(bMaxPower, 2, "%3dmA\n")
44 static ssize_t show_configuration_string(struct device *dev,
45 struct device_attribute *attr, char *buf)
47 struct usb_device *udev;
48 struct usb_host_config *actconfig;
50 udev = to_usb_device(dev);
51 actconfig = udev->actconfig;
52 if ((!actconfig) || (!actconfig->string))
54 return sprintf(buf, "%s\n", actconfig->string);
56 static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL);
58 /* configuration value is always present, and r/w */
59 usb_actconfig_show(bConfigurationValue, 1, "%u\n");
62 set_bConfigurationValue(struct device *dev, struct device_attribute *attr,
63 const char *buf, size_t count)
65 struct usb_device *udev = to_usb_device(dev);
68 if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
70 usb_lock_device(udev);
71 value = usb_set_configuration(udev, config);
72 usb_unlock_device(udev);
73 return (value < 0) ? value : count;
76 static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR,
77 show_bConfigurationValue, set_bConfigurationValue);
80 #define usb_string_attr(name) \
81 static ssize_t show_##name(struct device *dev, \
82 struct device_attribute *attr, char *buf) \
84 struct usb_device *udev; \
86 udev = to_usb_device(dev); \
87 return sprintf(buf, "%s\n", udev->name); \
89 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
91 usb_string_attr(product);
92 usb_string_attr(manufacturer);
93 usb_string_attr(serial);
96 show_speed(struct device *dev, struct device_attribute *attr, char *buf)
98 struct usb_device *udev;
101 udev = to_usb_device(dev);
103 switch (udev->speed) {
107 case USB_SPEED_UNKNOWN:
117 return sprintf(buf, "%s\n", speed);
119 static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
122 show_busnum(struct device *dev, struct device_attribute *attr, char *buf)
124 struct usb_device *udev;
126 udev = to_usb_device(dev);
127 return sprintf(buf, "%d\n", udev->bus->busnum);
129 static DEVICE_ATTR(busnum, S_IRUGO, show_busnum, NULL);
132 show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
134 struct usb_device *udev;
136 udev = to_usb_device(dev);
137 return sprintf(buf, "%d\n", udev->devnum);
139 static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
142 show_version(struct device *dev, struct device_attribute *attr, char *buf)
144 struct usb_device *udev;
147 udev = to_usb_device(dev);
148 bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
149 return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
151 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
154 show_maxchild(struct device *dev, struct device_attribute *attr, char *buf)
156 struct usb_device *udev;
158 udev = to_usb_device(dev);
159 return sprintf(buf, "%d\n", udev->maxchild);
161 static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
164 show_quirks(struct device *dev, struct device_attribute *attr, char *buf)
166 struct usb_device *udev;
168 udev = to_usb_device(dev);
169 return sprintf(buf, "0x%x\n", udev->quirks);
171 static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL);
174 show_urbnum(struct device *dev, struct device_attribute *attr, char *buf)
176 struct usb_device *udev;
178 udev = to_usb_device(dev);
179 return sprintf(buf, "%d\n", atomic_read(&udev->urbnum));
181 static DEVICE_ATTR(urbnum, S_IRUGO, show_urbnum, NULL);
186 static const char power_group[] = "power";
189 show_persist(struct device *dev, struct device_attribute *attr, char *buf)
191 struct usb_device *udev = to_usb_device(dev);
193 return sprintf(buf, "%d\n", udev->persist_enabled);
197 set_persist(struct device *dev, struct device_attribute *attr,
198 const char *buf, size_t count)
200 struct usb_device *udev = to_usb_device(dev);
203 /* Hubs are always enabled for USB_PERSIST */
204 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
207 if (sscanf(buf, "%d", &value) != 1)
210 udev->persist_enabled = !!value;
215 static DEVICE_ATTR(persist, S_IRUGO | S_IWUSR, show_persist, set_persist);
217 static int add_persist_attributes(struct device *dev)
221 if (is_usb_device(dev)) {
222 struct usb_device *udev = to_usb_device(dev);
224 /* Hubs are automatically enabled for USB_PERSIST,
225 * no point in creating the attribute file.
227 if (udev->descriptor.bDeviceClass != USB_CLASS_HUB)
228 rc = sysfs_add_file_to_group(&dev->kobj,
229 &dev_attr_persist.attr,
235 static void remove_persist_attributes(struct device *dev)
237 sysfs_remove_file_from_group(&dev->kobj,
238 &dev_attr_persist.attr,
243 #define add_persist_attributes(dev) 0
244 #define remove_persist_attributes(dev) do {} while (0)
246 #endif /* CONFIG_PM */
248 #ifdef CONFIG_USB_SUSPEND
251 show_connected_duration(struct device *dev, struct device_attribute *attr,
254 struct usb_device *udev = to_usb_device(dev);
256 return sprintf(buf, "%u\n",
257 jiffies_to_msecs(jiffies - udev->connect_time));
260 static DEVICE_ATTR(connected_duration, S_IRUGO, show_connected_duration, NULL);
263 * If the device is resumed, the last time the device was suspended has
264 * been pre-subtracted from active_duration. We add the current time to
265 * get the duration that the device was actually active.
267 * If the device is suspended, the active_duration is up-to-date.
270 show_active_duration(struct device *dev, struct device_attribute *attr,
273 struct usb_device *udev = to_usb_device(dev);
276 if (udev->state != USB_STATE_SUSPENDED)
277 duration = jiffies_to_msecs(jiffies + udev->active_duration);
279 duration = jiffies_to_msecs(udev->active_duration);
280 return sprintf(buf, "%u\n", duration);
283 static DEVICE_ATTR(active_duration, S_IRUGO, show_active_duration, NULL);
286 show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf)
288 struct usb_device *udev = to_usb_device(dev);
290 return sprintf(buf, "%d\n", udev->autosuspend_delay / HZ);
294 set_autosuspend(struct device *dev, struct device_attribute *attr,
295 const char *buf, size_t count)
297 struct usb_device *udev = to_usb_device(dev);
300 if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ ||
301 value <= - INT_MAX/HZ)
305 udev->autosuspend_delay = value;
307 usb_try_autosuspend_device(udev);
309 if (usb_autoresume_device(udev) == 0)
310 usb_autosuspend_device(udev);
315 static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
316 show_autosuspend, set_autosuspend);
318 static const char on_string[] = "on";
319 static const char auto_string[] = "auto";
320 static const char suspend_string[] = "suspend";
323 show_level(struct device *dev, struct device_attribute *attr, char *buf)
325 struct usb_device *udev = to_usb_device(dev);
326 const char *p = auto_string;
328 if (udev->state == USB_STATE_SUSPENDED) {
329 if (udev->autoresume_disabled)
332 if (udev->autosuspend_disabled)
335 return sprintf(buf, "%s\n", p);
339 set_level(struct device *dev, struct device_attribute *attr,
340 const char *buf, size_t count)
342 struct usb_device *udev = to_usb_device(dev);
346 int old_autosuspend_disabled, old_autoresume_disabled;
348 cp = memchr(buf, '\n', count);
352 usb_lock_device(udev);
353 old_autosuspend_disabled = udev->autosuspend_disabled;
354 old_autoresume_disabled = udev->autoresume_disabled;
356 /* Setting the flags without calling usb_pm_lock is a subject to
357 * races, but who cares...
359 if (len == sizeof on_string - 1 &&
360 strncmp(buf, on_string, len) == 0) {
361 udev->autosuspend_disabled = 1;
362 udev->autoresume_disabled = 0;
363 rc = usb_external_resume_device(udev, PMSG_USER_RESUME);
365 } else if (len == sizeof auto_string - 1 &&
366 strncmp(buf, auto_string, len) == 0) {
367 udev->autosuspend_disabled = 0;
368 udev->autoresume_disabled = 0;
369 rc = usb_external_resume_device(udev, PMSG_USER_RESUME);
371 } else if (len == sizeof suspend_string - 1 &&
372 strncmp(buf, suspend_string, len) == 0) {
373 udev->autosuspend_disabled = 0;
374 udev->autoresume_disabled = 1;
375 rc = usb_external_suspend_device(udev, PMSG_USER_SUSPEND);
381 udev->autosuspend_disabled = old_autosuspend_disabled;
382 udev->autoresume_disabled = old_autoresume_disabled;
384 usb_unlock_device(udev);
385 return (rc < 0 ? rc : count);
388 static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level);
390 static int add_power_attributes(struct device *dev)
394 if (is_usb_device(dev)) {
395 rc = sysfs_add_file_to_group(&dev->kobj,
396 &dev_attr_autosuspend.attr,
399 rc = sysfs_add_file_to_group(&dev->kobj,
400 &dev_attr_level.attr,
403 rc = sysfs_add_file_to_group(&dev->kobj,
404 &dev_attr_connected_duration.attr,
407 rc = sysfs_add_file_to_group(&dev->kobj,
408 &dev_attr_active_duration.attr,
414 static void remove_power_attributes(struct device *dev)
416 sysfs_remove_file_from_group(&dev->kobj,
417 &dev_attr_active_duration.attr,
419 sysfs_remove_file_from_group(&dev->kobj,
420 &dev_attr_connected_duration.attr,
422 sysfs_remove_file_from_group(&dev->kobj,
423 &dev_attr_level.attr,
425 sysfs_remove_file_from_group(&dev->kobj,
426 &dev_attr_autosuspend.attr,
432 #define add_power_attributes(dev) 0
433 #define remove_power_attributes(dev) do {} while (0)
435 #endif /* CONFIG_USB_SUSPEND */
438 /* Descriptor fields */
439 #define usb_descriptor_attr_le16(field, format_string) \
441 show_##field(struct device *dev, struct device_attribute *attr, \
444 struct usb_device *udev; \
446 udev = to_usb_device(dev); \
447 return sprintf(buf, format_string, \
448 le16_to_cpu(udev->descriptor.field)); \
450 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
452 usb_descriptor_attr_le16(idVendor, "%04x\n")
453 usb_descriptor_attr_le16(idProduct, "%04x\n")
454 usb_descriptor_attr_le16(bcdDevice, "%04x\n")
456 #define usb_descriptor_attr(field, format_string) \
458 show_##field(struct device *dev, struct device_attribute *attr, \
461 struct usb_device *udev; \
463 udev = to_usb_device(dev); \
464 return sprintf(buf, format_string, udev->descriptor.field); \
466 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
468 usb_descriptor_attr(bDeviceClass, "%02x\n")
469 usb_descriptor_attr(bDeviceSubClass, "%02x\n")
470 usb_descriptor_attr(bDeviceProtocol, "%02x\n")
471 usb_descriptor_attr(bNumConfigurations, "%d\n")
472 usb_descriptor_attr(bMaxPacketSize0, "%d\n")
476 /* show if the device is authorized (1) or not (0) */
477 static ssize_t usb_dev_authorized_show(struct device *dev,
478 struct device_attribute *attr,
481 struct usb_device *usb_dev = to_usb_device(dev);
482 return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
487 * Authorize a device to be used in the system
489 * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
491 static ssize_t usb_dev_authorized_store(struct device *dev,
492 struct device_attribute *attr,
493 const char *buf, size_t size)
496 struct usb_device *usb_dev = to_usb_device(dev);
498 result = sscanf(buf, "%u\n", &val);
502 result = usb_deauthorize_device(usb_dev);
504 result = usb_authorize_device(usb_dev);
505 return result < 0? result : size;
508 static DEVICE_ATTR(authorized, 0644,
509 usb_dev_authorized_show, usb_dev_authorized_store);
512 static struct attribute *dev_attrs[] = {
513 /* current configuration's attributes */
514 &dev_attr_configuration.attr,
515 &dev_attr_bNumInterfaces.attr,
516 &dev_attr_bConfigurationValue.attr,
517 &dev_attr_bmAttributes.attr,
518 &dev_attr_bMaxPower.attr,
519 &dev_attr_urbnum.attr,
520 /* device attributes */
521 &dev_attr_idVendor.attr,
522 &dev_attr_idProduct.attr,
523 &dev_attr_bcdDevice.attr,
524 &dev_attr_bDeviceClass.attr,
525 &dev_attr_bDeviceSubClass.attr,
526 &dev_attr_bDeviceProtocol.attr,
527 &dev_attr_bNumConfigurations.attr,
528 &dev_attr_bMaxPacketSize0.attr,
529 &dev_attr_speed.attr,
530 &dev_attr_busnum.attr,
531 &dev_attr_devnum.attr,
532 &dev_attr_version.attr,
533 &dev_attr_maxchild.attr,
534 &dev_attr_quirks.attr,
535 &dev_attr_authorized.attr,
538 static struct attribute_group dev_attr_grp = {
542 /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
545 static struct attribute *dev_string_attrs[] = {
546 &dev_attr_manufacturer.attr,
547 &dev_attr_product.attr,
548 &dev_attr_serial.attr,
552 static mode_t dev_string_attrs_are_visible(struct kobject *kobj,
553 struct attribute *a, int n)
555 struct usb_device *udev = to_usb_device(
556 container_of(kobj, struct device, kobj));
558 if (a == &dev_attr_manufacturer.attr) {
559 if (udev->manufacturer == NULL)
561 } else if (a == &dev_attr_product.attr) {
562 if (udev->product == NULL)
564 } else if (a == &dev_attr_serial.attr) {
565 if (udev->serial == NULL)
571 static struct attribute_group dev_string_attr_grp = {
572 .attrs = dev_string_attrs,
573 .is_visible = dev_string_attrs_are_visible,
576 struct attribute_group *usb_device_groups[] = {
578 &dev_string_attr_grp,
582 /* Binary descriptors */
585 read_descriptors(struct kobject *kobj, struct bin_attribute *attr,
586 char *buf, loff_t off, size_t count)
588 struct usb_device *udev = to_usb_device(
589 container_of(kobj, struct device, kobj));
590 size_t nleft = count;
595 /* The binary attribute begins with the device descriptor.
596 * Following that are the raw descriptor entries for all the
597 * configurations (config plus subsidiary descriptors).
599 for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
600 nleft > 0; ++cfgno) {
602 src = &udev->descriptor;
603 srclen = sizeof(struct usb_device_descriptor);
605 src = udev->rawdescriptors[cfgno];
606 srclen = __le16_to_cpu(udev->config[cfgno].desc.
610 n = min(nleft, srclen - (size_t) off);
611 memcpy(buf, src + off, n);
619 return count - nleft;
622 static struct bin_attribute dev_bin_attr_descriptors = {
623 .attr = {.name = "descriptors", .mode = 0444},
624 .read = read_descriptors,
625 .size = 18 + 65535, /* dev descr + max-size raw descriptor */
628 int usb_create_sysfs_dev_files(struct usb_device *udev)
630 struct device *dev = &udev->dev;
633 retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
637 retval = add_persist_attributes(dev);
641 retval = add_power_attributes(dev);
646 usb_remove_sysfs_dev_files(udev);
650 void usb_remove_sysfs_dev_files(struct usb_device *udev)
652 struct device *dev = &udev->dev;
654 remove_power_attributes(dev);
655 remove_persist_attributes(dev);
656 device_remove_bin_file(dev, &dev_bin_attr_descriptors);
659 /* Interface Accociation Descriptor fields */
660 #define usb_intf_assoc_attr(field, format_string) \
662 show_iad_##field(struct device *dev, struct device_attribute *attr, \
665 struct usb_interface *intf = to_usb_interface(dev); \
667 return sprintf(buf, format_string, \
668 intf->intf_assoc->field); \
670 static DEVICE_ATTR(iad_##field, S_IRUGO, show_iad_##field, NULL);
672 usb_intf_assoc_attr(bFirstInterface, "%02x\n")
673 usb_intf_assoc_attr(bInterfaceCount, "%02d\n")
674 usb_intf_assoc_attr(bFunctionClass, "%02x\n")
675 usb_intf_assoc_attr(bFunctionSubClass, "%02x\n")
676 usb_intf_assoc_attr(bFunctionProtocol, "%02x\n")
678 /* Interface fields */
679 #define usb_intf_attr(field, format_string) \
681 show_##field(struct device *dev, struct device_attribute *attr, \
684 struct usb_interface *intf = to_usb_interface(dev); \
686 return sprintf(buf, format_string, \
687 intf->cur_altsetting->desc.field); \
689 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
691 usb_intf_attr(bInterfaceNumber, "%02x\n")
692 usb_intf_attr(bAlternateSetting, "%2d\n")
693 usb_intf_attr(bNumEndpoints, "%02x\n")
694 usb_intf_attr(bInterfaceClass, "%02x\n")
695 usb_intf_attr(bInterfaceSubClass, "%02x\n")
696 usb_intf_attr(bInterfaceProtocol, "%02x\n")
698 static ssize_t show_interface_string(struct device *dev,
699 struct device_attribute *attr, char *buf)
701 struct usb_interface *intf;
704 intf = to_usb_interface(dev);
705 string = intf->cur_altsetting->string;
706 barrier(); /* The altsetting might change! */
710 return sprintf(buf, "%s\n", string);
712 static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
714 static ssize_t show_modalias(struct device *dev,
715 struct device_attribute *attr, char *buf)
717 struct usb_interface *intf;
718 struct usb_device *udev;
719 struct usb_host_interface *alt;
721 intf = to_usb_interface(dev);
722 udev = interface_to_usbdev(intf);
723 alt = intf->cur_altsetting;
725 return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
726 "ic%02Xisc%02Xip%02X\n",
727 le16_to_cpu(udev->descriptor.idVendor),
728 le16_to_cpu(udev->descriptor.idProduct),
729 le16_to_cpu(udev->descriptor.bcdDevice),
730 udev->descriptor.bDeviceClass,
731 udev->descriptor.bDeviceSubClass,
732 udev->descriptor.bDeviceProtocol,
733 alt->desc.bInterfaceClass,
734 alt->desc.bInterfaceSubClass,
735 alt->desc.bInterfaceProtocol);
737 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
739 static ssize_t show_supports_autosuspend(struct device *dev,
740 struct device_attribute *attr, char *buf)
742 struct usb_interface *intf;
743 struct usb_device *udev;
746 intf = to_usb_interface(dev);
747 udev = interface_to_usbdev(intf);
749 usb_lock_device(udev);
750 /* Devices will be autosuspended even when an interface isn't claimed */
751 if (!intf->dev.driver ||
752 to_usb_driver(intf->dev.driver)->supports_autosuspend)
753 ret = sprintf(buf, "%u\n", 1);
755 ret = sprintf(buf, "%u\n", 0);
756 usb_unlock_device(udev);
760 static DEVICE_ATTR(supports_autosuspend, S_IRUGO, show_supports_autosuspend, NULL);
762 static struct attribute *intf_attrs[] = {
763 &dev_attr_bInterfaceNumber.attr,
764 &dev_attr_bAlternateSetting.attr,
765 &dev_attr_bNumEndpoints.attr,
766 &dev_attr_bInterfaceClass.attr,
767 &dev_attr_bInterfaceSubClass.attr,
768 &dev_attr_bInterfaceProtocol.attr,
769 &dev_attr_modalias.attr,
770 &dev_attr_supports_autosuspend.attr,
773 static struct attribute_group intf_attr_grp = {
777 static struct attribute *intf_assoc_attrs[] = {
778 &dev_attr_iad_bFirstInterface.attr,
779 &dev_attr_iad_bInterfaceCount.attr,
780 &dev_attr_iad_bFunctionClass.attr,
781 &dev_attr_iad_bFunctionSubClass.attr,
782 &dev_attr_iad_bFunctionProtocol.attr,
786 static mode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
787 struct attribute *a, int n)
789 struct usb_interface *intf = to_usb_interface(
790 container_of(kobj, struct device, kobj));
792 if (intf->intf_assoc == NULL)
797 static struct attribute_group intf_assoc_attr_grp = {
798 .attrs = intf_assoc_attrs,
799 .is_visible = intf_assoc_attrs_are_visible,
802 struct attribute_group *usb_interface_groups[] = {
804 &intf_assoc_attr_grp,
808 int usb_create_sysfs_intf_files(struct usb_interface *intf)
810 struct usb_device *udev = interface_to_usbdev(intf);
811 struct usb_host_interface *alt = intf->cur_altsetting;
814 if (intf->sysfs_files_created || intf->unregistering)
817 if (alt->string == NULL &&
818 !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
819 alt->string = usb_cache_string(udev, alt->desc.iInterface);
821 retval = device_create_file(&intf->dev, &dev_attr_interface);
822 intf->sysfs_files_created = 1;
826 void usb_remove_sysfs_intf_files(struct usb_interface *intf)
828 if (!intf->sysfs_files_created)
831 device_remove_file(&intf->dev, &dev_attr_interface);
832 intf->sysfs_files_created = 0;