1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/usb/core/sysfs.c
5 * (C) Copyright 2002 David Brownell
6 * (C) Copyright 2002,2004 Greg Kroah-Hartman
7 * (C) Copyright 2002,2004 IBM Corp.
9 * All of the sysfs file attributes for usb devices and interfaces.
11 * Released under the GPLv2 only.
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/usb.h>
18 #include <linux/usb/quirks.h>
22 /* Active configuration fields */
23 #define usb_actconfig_show(field, format_string) \
24 static ssize_t field##_show(struct device *dev, \
25 struct device_attribute *attr, char *buf) \
27 struct usb_device *udev; \
28 struct usb_host_config *actconfig; \
31 udev = to_usb_device(dev); \
32 rc = usb_lock_device_interruptible(udev); \
35 actconfig = udev->actconfig; \
37 rc = sprintf(buf, format_string, \
38 actconfig->desc.field); \
39 usb_unlock_device(udev); \
43 #define usb_actconfig_attr(field, format_string) \
44 usb_actconfig_show(field, format_string) \
45 static DEVICE_ATTR_RO(field)
47 usb_actconfig_attr(bNumInterfaces, "%2d\n");
48 usb_actconfig_attr(bmAttributes, "%2x\n");
50 static ssize_t bMaxPower_show(struct device *dev,
51 struct device_attribute *attr, char *buf)
53 struct usb_device *udev;
54 struct usb_host_config *actconfig;
57 udev = to_usb_device(dev);
58 rc = usb_lock_device_interruptible(udev);
61 actconfig = udev->actconfig;
63 rc = sprintf(buf, "%dmA\n", usb_get_max_power(udev, actconfig));
64 usb_unlock_device(udev);
67 static DEVICE_ATTR_RO(bMaxPower);
69 static ssize_t configuration_show(struct device *dev,
70 struct device_attribute *attr, char *buf)
72 struct usb_device *udev;
73 struct usb_host_config *actconfig;
76 udev = to_usb_device(dev);
77 rc = usb_lock_device_interruptible(udev);
80 actconfig = udev->actconfig;
81 if (actconfig && actconfig->string)
82 rc = sprintf(buf, "%s\n", actconfig->string);
83 usb_unlock_device(udev);
86 static DEVICE_ATTR_RO(configuration);
88 /* configuration value is always present, and r/w */
89 usb_actconfig_show(bConfigurationValue, "%u\n");
91 static ssize_t bConfigurationValue_store(struct device *dev,
92 struct device_attribute *attr,
93 const char *buf, size_t count)
95 struct usb_device *udev = to_usb_device(dev);
96 int config, value, rc;
98 if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
100 rc = usb_lock_device_interruptible(udev);
103 value = usb_set_configuration(udev, config);
104 usb_unlock_device(udev);
105 return (value < 0) ? value : count;
107 static DEVICE_ATTR_IGNORE_LOCKDEP(bConfigurationValue, S_IRUGO | S_IWUSR,
108 bConfigurationValue_show, bConfigurationValue_store);
111 static ssize_t devspec_show(struct device *dev, struct device_attribute *attr,
114 struct device_node *of_node = dev->of_node;
116 return sprintf(buf, "%pOF\n", of_node);
118 static DEVICE_ATTR_RO(devspec);
122 #define usb_string_attr(name) \
123 static ssize_t name##_show(struct device *dev, \
124 struct device_attribute *attr, char *buf) \
126 struct usb_device *udev; \
129 udev = to_usb_device(dev); \
130 retval = usb_lock_device_interruptible(udev); \
133 retval = sprintf(buf, "%s\n", udev->name); \
134 usb_unlock_device(udev); \
137 static DEVICE_ATTR_RO(name)
139 usb_string_attr(product);
140 usb_string_attr(manufacturer);
141 usb_string_attr(serial);
143 static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
146 struct usb_device *udev;
149 udev = to_usb_device(dev);
151 switch (udev->speed) {
155 case USB_SPEED_UNKNOWN:
162 case USB_SPEED_WIRELESS:
165 case USB_SPEED_SUPER:
168 case USB_SPEED_SUPER_PLUS:
174 return sprintf(buf, "%s\n", speed);
176 static DEVICE_ATTR_RO(speed);
178 static ssize_t rx_lanes_show(struct device *dev, struct device_attribute *attr,
181 struct usb_device *udev;
183 udev = to_usb_device(dev);
184 return sprintf(buf, "%d\n", udev->rx_lanes);
186 static DEVICE_ATTR_RO(rx_lanes);
188 static ssize_t tx_lanes_show(struct device *dev, struct device_attribute *attr,
191 struct usb_device *udev;
193 udev = to_usb_device(dev);
194 return sprintf(buf, "%d\n", udev->tx_lanes);
196 static DEVICE_ATTR_RO(tx_lanes);
198 static ssize_t busnum_show(struct device *dev, struct device_attribute *attr,
201 struct usb_device *udev;
203 udev = to_usb_device(dev);
204 return sprintf(buf, "%d\n", udev->bus->busnum);
206 static DEVICE_ATTR_RO(busnum);
208 static ssize_t devnum_show(struct device *dev, struct device_attribute *attr,
211 struct usb_device *udev;
213 udev = to_usb_device(dev);
214 return sprintf(buf, "%d\n", udev->devnum);
216 static DEVICE_ATTR_RO(devnum);
218 static ssize_t devpath_show(struct device *dev, struct device_attribute *attr,
221 struct usb_device *udev;
223 udev = to_usb_device(dev);
224 return sprintf(buf, "%s\n", udev->devpath);
226 static DEVICE_ATTR_RO(devpath);
228 static ssize_t version_show(struct device *dev, struct device_attribute *attr,
231 struct usb_device *udev;
234 udev = to_usb_device(dev);
235 bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
236 return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
238 static DEVICE_ATTR_RO(version);
240 static ssize_t maxchild_show(struct device *dev, struct device_attribute *attr,
243 struct usb_device *udev;
245 udev = to_usb_device(dev);
246 return sprintf(buf, "%d\n", udev->maxchild);
248 static DEVICE_ATTR_RO(maxchild);
250 static ssize_t quirks_show(struct device *dev, struct device_attribute *attr,
253 struct usb_device *udev;
255 udev = to_usb_device(dev);
256 return sprintf(buf, "0x%x\n", udev->quirks);
258 static DEVICE_ATTR_RO(quirks);
260 static ssize_t avoid_reset_quirk_show(struct device *dev,
261 struct device_attribute *attr, char *buf)
263 struct usb_device *udev;
265 udev = to_usb_device(dev);
266 return sprintf(buf, "%d\n", !!(udev->quirks & USB_QUIRK_RESET));
269 static ssize_t avoid_reset_quirk_store(struct device *dev,
270 struct device_attribute *attr,
271 const char *buf, size_t count)
273 struct usb_device *udev = to_usb_device(dev);
276 if (sscanf(buf, "%d", &val) != 1 || val < 0 || val > 1)
278 rc = usb_lock_device_interruptible(udev);
282 udev->quirks |= USB_QUIRK_RESET;
284 udev->quirks &= ~USB_QUIRK_RESET;
285 usb_unlock_device(udev);
288 static DEVICE_ATTR_RW(avoid_reset_quirk);
290 static ssize_t urbnum_show(struct device *dev, struct device_attribute *attr,
293 struct usb_device *udev;
295 udev = to_usb_device(dev);
296 return sprintf(buf, "%d\n", atomic_read(&udev->urbnum));
298 static DEVICE_ATTR_RO(urbnum);
300 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
303 struct usb_device *udev;
306 udev = to_usb_device(dev);
308 switch (udev->removable) {
309 case USB_DEVICE_REMOVABLE:
312 case USB_DEVICE_FIXED:
319 return sprintf(buf, "%s\n", state);
321 static DEVICE_ATTR_RO(removable);
323 static ssize_t ltm_capable_show(struct device *dev,
324 struct device_attribute *attr, char *buf)
326 if (usb_device_supports_ltm(to_usb_device(dev)))
327 return sprintf(buf, "%s\n", "yes");
328 return sprintf(buf, "%s\n", "no");
330 static DEVICE_ATTR_RO(ltm_capable);
334 static ssize_t persist_show(struct device *dev, struct device_attribute *attr,
337 struct usb_device *udev = to_usb_device(dev);
339 return sprintf(buf, "%d\n", udev->persist_enabled);
342 static ssize_t persist_store(struct device *dev, struct device_attribute *attr,
343 const char *buf, size_t count)
345 struct usb_device *udev = to_usb_device(dev);
348 /* Hubs are always enabled for USB_PERSIST */
349 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
352 if (sscanf(buf, "%d", &value) != 1)
355 rc = usb_lock_device_interruptible(udev);
358 udev->persist_enabled = !!value;
359 usb_unlock_device(udev);
362 static DEVICE_ATTR_RW(persist);
364 static int add_persist_attributes(struct device *dev)
368 if (is_usb_device(dev)) {
369 struct usb_device *udev = to_usb_device(dev);
371 /* Hubs are automatically enabled for USB_PERSIST,
372 * no point in creating the attribute file.
374 if (udev->descriptor.bDeviceClass != USB_CLASS_HUB)
375 rc = sysfs_add_file_to_group(&dev->kobj,
376 &dev_attr_persist.attr,
382 static void remove_persist_attributes(struct device *dev)
384 sysfs_remove_file_from_group(&dev->kobj,
385 &dev_attr_persist.attr,
389 static ssize_t connected_duration_show(struct device *dev,
390 struct device_attribute *attr, char *buf)
392 struct usb_device *udev = to_usb_device(dev);
394 return sprintf(buf, "%u\n",
395 jiffies_to_msecs(jiffies - udev->connect_time));
397 static DEVICE_ATTR_RO(connected_duration);
400 * If the device is resumed, the last time the device was suspended has
401 * been pre-subtracted from active_duration. We add the current time to
402 * get the duration that the device was actually active.
404 * If the device is suspended, the active_duration is up-to-date.
406 static ssize_t active_duration_show(struct device *dev,
407 struct device_attribute *attr, char *buf)
409 struct usb_device *udev = to_usb_device(dev);
412 if (udev->state != USB_STATE_SUSPENDED)
413 duration = jiffies_to_msecs(jiffies + udev->active_duration);
415 duration = jiffies_to_msecs(udev->active_duration);
416 return sprintf(buf, "%u\n", duration);
418 static DEVICE_ATTR_RO(active_duration);
420 static ssize_t autosuspend_show(struct device *dev,
421 struct device_attribute *attr, char *buf)
423 return sprintf(buf, "%d\n", dev->power.autosuspend_delay / 1000);
426 static ssize_t autosuspend_store(struct device *dev,
427 struct device_attribute *attr, const char *buf,
432 if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/1000 ||
433 value <= -INT_MAX/1000)
436 pm_runtime_set_autosuspend_delay(dev, value * 1000);
439 static DEVICE_ATTR_RW(autosuspend);
441 static const char on_string[] = "on";
442 static const char auto_string[] = "auto";
444 static void warn_level(void)
446 static int level_warned;
450 printk(KERN_WARNING "WARNING! power/level is deprecated; "
451 "use power/control instead\n");
455 static ssize_t level_show(struct device *dev, struct device_attribute *attr,
458 struct usb_device *udev = to_usb_device(dev);
459 const char *p = auto_string;
462 if (udev->state != USB_STATE_SUSPENDED && !udev->dev.power.runtime_auto)
464 return sprintf(buf, "%s\n", p);
467 static ssize_t level_store(struct device *dev, struct device_attribute *attr,
468 const char *buf, size_t count)
470 struct usb_device *udev = to_usb_device(dev);
477 cp = memchr(buf, '\n', count);
481 rv = usb_lock_device_interruptible(udev);
485 if (len == sizeof on_string - 1 &&
486 strncmp(buf, on_string, len) == 0)
487 usb_disable_autosuspend(udev);
489 else if (len == sizeof auto_string - 1 &&
490 strncmp(buf, auto_string, len) == 0)
491 usb_enable_autosuspend(udev);
496 usb_unlock_device(udev);
499 static DEVICE_ATTR_RW(level);
501 static ssize_t usb2_hardware_lpm_show(struct device *dev,
502 struct device_attribute *attr, char *buf)
504 struct usb_device *udev = to_usb_device(dev);
507 if (udev->usb2_hw_lpm_allowed == 1)
512 return sprintf(buf, "%s\n", p);
515 static ssize_t usb2_hardware_lpm_store(struct device *dev,
516 struct device_attribute *attr,
517 const char *buf, size_t count)
519 struct usb_device *udev = to_usb_device(dev);
523 ret = usb_lock_device_interruptible(udev);
527 ret = strtobool(buf, &value);
530 udev->usb2_hw_lpm_allowed = value;
532 ret = usb_enable_usb2_hardware_lpm(udev);
534 ret = usb_disable_usb2_hardware_lpm(udev);
537 usb_unlock_device(udev);
544 static DEVICE_ATTR_RW(usb2_hardware_lpm);
546 static ssize_t usb2_lpm_l1_timeout_show(struct device *dev,
547 struct device_attribute *attr,
550 struct usb_device *udev = to_usb_device(dev);
551 return sprintf(buf, "%d\n", udev->l1_params.timeout);
554 static ssize_t usb2_lpm_l1_timeout_store(struct device *dev,
555 struct device_attribute *attr,
556 const char *buf, size_t count)
558 struct usb_device *udev = to_usb_device(dev);
561 if (kstrtou16(buf, 0, &timeout))
564 udev->l1_params.timeout = timeout;
568 static DEVICE_ATTR_RW(usb2_lpm_l1_timeout);
570 static ssize_t usb2_lpm_besl_show(struct device *dev,
571 struct device_attribute *attr, char *buf)
573 struct usb_device *udev = to_usb_device(dev);
574 return sprintf(buf, "%d\n", udev->l1_params.besl);
577 static ssize_t usb2_lpm_besl_store(struct device *dev,
578 struct device_attribute *attr,
579 const char *buf, size_t count)
581 struct usb_device *udev = to_usb_device(dev);
584 if (kstrtou8(buf, 0, &besl) || besl > 15)
587 udev->l1_params.besl = besl;
591 static DEVICE_ATTR_RW(usb2_lpm_besl);
593 static ssize_t usb3_hardware_lpm_u1_show(struct device *dev,
594 struct device_attribute *attr, char *buf)
596 struct usb_device *udev = to_usb_device(dev);
600 rc = usb_lock_device_interruptible(udev);
604 if (udev->usb3_lpm_u1_enabled)
609 usb_unlock_device(udev);
611 return sprintf(buf, "%s\n", p);
613 static DEVICE_ATTR_RO(usb3_hardware_lpm_u1);
615 static ssize_t usb3_hardware_lpm_u2_show(struct device *dev,
616 struct device_attribute *attr, char *buf)
618 struct usb_device *udev = to_usb_device(dev);
622 rc = usb_lock_device_interruptible(udev);
626 if (udev->usb3_lpm_u2_enabled)
631 usb_unlock_device(udev);
633 return sprintf(buf, "%s\n", p);
635 static DEVICE_ATTR_RO(usb3_hardware_lpm_u2);
637 static struct attribute *usb2_hardware_lpm_attr[] = {
638 &dev_attr_usb2_hardware_lpm.attr,
639 &dev_attr_usb2_lpm_l1_timeout.attr,
640 &dev_attr_usb2_lpm_besl.attr,
643 static struct attribute_group usb2_hardware_lpm_attr_group = {
644 .name = power_group_name,
645 .attrs = usb2_hardware_lpm_attr,
648 static struct attribute *usb3_hardware_lpm_attr[] = {
649 &dev_attr_usb3_hardware_lpm_u1.attr,
650 &dev_attr_usb3_hardware_lpm_u2.attr,
653 static struct attribute_group usb3_hardware_lpm_attr_group = {
654 .name = power_group_name,
655 .attrs = usb3_hardware_lpm_attr,
658 static struct attribute *power_attrs[] = {
659 &dev_attr_autosuspend.attr,
660 &dev_attr_level.attr,
661 &dev_attr_connected_duration.attr,
662 &dev_attr_active_duration.attr,
665 static struct attribute_group power_attr_group = {
666 .name = power_group_name,
667 .attrs = power_attrs,
670 static int add_power_attributes(struct device *dev)
674 if (is_usb_device(dev)) {
675 struct usb_device *udev = to_usb_device(dev);
676 rc = sysfs_merge_group(&dev->kobj, &power_attr_group);
677 if (udev->usb2_hw_lpm_capable == 1)
678 rc = sysfs_merge_group(&dev->kobj,
679 &usb2_hardware_lpm_attr_group);
680 if ((udev->speed == USB_SPEED_SUPER ||
681 udev->speed == USB_SPEED_SUPER_PLUS) &&
682 udev->lpm_capable == 1)
683 rc = sysfs_merge_group(&dev->kobj,
684 &usb3_hardware_lpm_attr_group);
690 static void remove_power_attributes(struct device *dev)
692 sysfs_unmerge_group(&dev->kobj, &usb2_hardware_lpm_attr_group);
693 sysfs_unmerge_group(&dev->kobj, &power_attr_group);
698 #define add_persist_attributes(dev) 0
699 #define remove_persist_attributes(dev) do {} while (0)
701 #define add_power_attributes(dev) 0
702 #define remove_power_attributes(dev) do {} while (0)
704 #endif /* CONFIG_PM */
707 /* Descriptor fields */
708 #define usb_descriptor_attr_le16(field, format_string) \
710 field##_show(struct device *dev, struct device_attribute *attr, \
713 struct usb_device *udev; \
715 udev = to_usb_device(dev); \
716 return sprintf(buf, format_string, \
717 le16_to_cpu(udev->descriptor.field)); \
719 static DEVICE_ATTR_RO(field)
721 usb_descriptor_attr_le16(idVendor, "%04x\n");
722 usb_descriptor_attr_le16(idProduct, "%04x\n");
723 usb_descriptor_attr_le16(bcdDevice, "%04x\n");
725 #define usb_descriptor_attr(field, format_string) \
727 field##_show(struct device *dev, struct device_attribute *attr, \
730 struct usb_device *udev; \
732 udev = to_usb_device(dev); \
733 return sprintf(buf, format_string, udev->descriptor.field); \
735 static DEVICE_ATTR_RO(field)
737 usb_descriptor_attr(bDeviceClass, "%02x\n");
738 usb_descriptor_attr(bDeviceSubClass, "%02x\n");
739 usb_descriptor_attr(bDeviceProtocol, "%02x\n");
740 usb_descriptor_attr(bNumConfigurations, "%d\n");
741 usb_descriptor_attr(bMaxPacketSize0, "%d\n");
744 /* show if the device is authorized (1) or not (0) */
745 static ssize_t authorized_show(struct device *dev,
746 struct device_attribute *attr, char *buf)
748 struct usb_device *usb_dev = to_usb_device(dev);
749 return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
753 * Authorize a device to be used in the system
755 * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
757 static ssize_t authorized_store(struct device *dev,
758 struct device_attribute *attr, const char *buf,
762 struct usb_device *usb_dev = to_usb_device(dev);
764 result = sscanf(buf, "%u\n", &val);
768 result = usb_deauthorize_device(usb_dev);
770 result = usb_authorize_device(usb_dev);
771 return result < 0 ? result : size;
773 static DEVICE_ATTR_IGNORE_LOCKDEP(authorized, S_IRUGO | S_IWUSR,
774 authorized_show, authorized_store);
776 /* "Safely remove a device" */
777 static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
778 const char *buf, size_t count)
780 struct usb_device *udev = to_usb_device(dev);
783 usb_lock_device(udev);
784 if (udev->state != USB_STATE_NOTATTACHED) {
786 /* To avoid races, first unconfigure and then remove */
787 usb_set_configuration(udev, -1);
788 rc = usb_remove_device(udev);
792 usb_unlock_device(udev);
795 static DEVICE_ATTR_IGNORE_LOCKDEP(remove, S_IWUSR, NULL, remove_store);
798 static struct attribute *dev_attrs[] = {
799 /* current configuration's attributes */
800 &dev_attr_configuration.attr,
801 &dev_attr_bNumInterfaces.attr,
802 &dev_attr_bConfigurationValue.attr,
803 &dev_attr_bmAttributes.attr,
804 &dev_attr_bMaxPower.attr,
805 /* device attributes */
806 &dev_attr_urbnum.attr,
807 &dev_attr_idVendor.attr,
808 &dev_attr_idProduct.attr,
809 &dev_attr_bcdDevice.attr,
810 &dev_attr_bDeviceClass.attr,
811 &dev_attr_bDeviceSubClass.attr,
812 &dev_attr_bDeviceProtocol.attr,
813 &dev_attr_bNumConfigurations.attr,
814 &dev_attr_bMaxPacketSize0.attr,
815 &dev_attr_speed.attr,
816 &dev_attr_rx_lanes.attr,
817 &dev_attr_tx_lanes.attr,
818 &dev_attr_busnum.attr,
819 &dev_attr_devnum.attr,
820 &dev_attr_devpath.attr,
821 &dev_attr_version.attr,
822 &dev_attr_maxchild.attr,
823 &dev_attr_quirks.attr,
824 &dev_attr_avoid_reset_quirk.attr,
825 &dev_attr_authorized.attr,
826 &dev_attr_remove.attr,
827 &dev_attr_removable.attr,
828 &dev_attr_ltm_capable.attr,
830 &dev_attr_devspec.attr,
834 static struct attribute_group dev_attr_grp = {
838 /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
841 static struct attribute *dev_string_attrs[] = {
842 &dev_attr_manufacturer.attr,
843 &dev_attr_product.attr,
844 &dev_attr_serial.attr,
848 static umode_t dev_string_attrs_are_visible(struct kobject *kobj,
849 struct attribute *a, int n)
851 struct device *dev = container_of(kobj, struct device, kobj);
852 struct usb_device *udev = to_usb_device(dev);
854 if (a == &dev_attr_manufacturer.attr) {
855 if (udev->manufacturer == NULL)
857 } else if (a == &dev_attr_product.attr) {
858 if (udev->product == NULL)
860 } else if (a == &dev_attr_serial.attr) {
861 if (udev->serial == NULL)
867 static struct attribute_group dev_string_attr_grp = {
868 .attrs = dev_string_attrs,
869 .is_visible = dev_string_attrs_are_visible,
872 const struct attribute_group *usb_device_groups[] = {
874 &dev_string_attr_grp,
878 /* Binary descriptors */
881 read_descriptors(struct file *filp, struct kobject *kobj,
882 struct bin_attribute *attr,
883 char *buf, loff_t off, size_t count)
885 struct device *dev = container_of(kobj, struct device, kobj);
886 struct usb_device *udev = to_usb_device(dev);
887 size_t nleft = count;
892 /* The binary attribute begins with the device descriptor.
893 * Following that are the raw descriptor entries for all the
894 * configurations (config plus subsidiary descriptors).
896 for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
897 nleft > 0; ++cfgno) {
899 src = &udev->descriptor;
900 srclen = sizeof(struct usb_device_descriptor);
902 src = udev->rawdescriptors[cfgno];
903 srclen = __le16_to_cpu(udev->config[cfgno].desc.
907 n = min(nleft, srclen - (size_t) off);
908 memcpy(buf, src + off, n);
916 return count - nleft;
919 static struct bin_attribute dev_bin_attr_descriptors = {
920 .attr = {.name = "descriptors", .mode = 0444},
921 .read = read_descriptors,
922 .size = 18 + 65535, /* dev descr + max-size raw descriptor */
925 int usb_create_sysfs_dev_files(struct usb_device *udev)
927 struct device *dev = &udev->dev;
930 retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
934 retval = add_persist_attributes(dev);
938 retval = add_power_attributes(dev);
943 usb_remove_sysfs_dev_files(udev);
947 void usb_remove_sysfs_dev_files(struct usb_device *udev)
949 struct device *dev = &udev->dev;
951 remove_power_attributes(dev);
952 remove_persist_attributes(dev);
953 device_remove_bin_file(dev, &dev_bin_attr_descriptors);
956 /* Interface Association Descriptor fields */
957 #define usb_intf_assoc_attr(field, format_string) \
959 iad_##field##_show(struct device *dev, struct device_attribute *attr, \
962 struct usb_interface *intf = to_usb_interface(dev); \
964 return sprintf(buf, format_string, \
965 intf->intf_assoc->field); \
967 static DEVICE_ATTR_RO(iad_##field)
969 usb_intf_assoc_attr(bFirstInterface, "%02x\n");
970 usb_intf_assoc_attr(bInterfaceCount, "%02d\n");
971 usb_intf_assoc_attr(bFunctionClass, "%02x\n");
972 usb_intf_assoc_attr(bFunctionSubClass, "%02x\n");
973 usb_intf_assoc_attr(bFunctionProtocol, "%02x\n");
975 /* Interface fields */
976 #define usb_intf_attr(field, format_string) \
978 field##_show(struct device *dev, struct device_attribute *attr, \
981 struct usb_interface *intf = to_usb_interface(dev); \
983 return sprintf(buf, format_string, \
984 intf->cur_altsetting->desc.field); \
986 static DEVICE_ATTR_RO(field)
988 usb_intf_attr(bInterfaceNumber, "%02x\n");
989 usb_intf_attr(bAlternateSetting, "%2d\n");
990 usb_intf_attr(bNumEndpoints, "%02x\n");
991 usb_intf_attr(bInterfaceClass, "%02x\n");
992 usb_intf_attr(bInterfaceSubClass, "%02x\n");
993 usb_intf_attr(bInterfaceProtocol, "%02x\n");
995 static ssize_t interface_show(struct device *dev, struct device_attribute *attr,
998 struct usb_interface *intf;
1001 intf = to_usb_interface(dev);
1002 string = READ_ONCE(intf->cur_altsetting->string);
1005 return sprintf(buf, "%s\n", string);
1007 static DEVICE_ATTR_RO(interface);
1009 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
1012 struct usb_interface *intf;
1013 struct usb_device *udev;
1014 struct usb_host_interface *alt;
1016 intf = to_usb_interface(dev);
1017 udev = interface_to_usbdev(intf);
1018 alt = READ_ONCE(intf->cur_altsetting);
1020 return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
1021 "ic%02Xisc%02Xip%02Xin%02X\n",
1022 le16_to_cpu(udev->descriptor.idVendor),
1023 le16_to_cpu(udev->descriptor.idProduct),
1024 le16_to_cpu(udev->descriptor.bcdDevice),
1025 udev->descriptor.bDeviceClass,
1026 udev->descriptor.bDeviceSubClass,
1027 udev->descriptor.bDeviceProtocol,
1028 alt->desc.bInterfaceClass,
1029 alt->desc.bInterfaceSubClass,
1030 alt->desc.bInterfaceProtocol,
1031 alt->desc.bInterfaceNumber);
1033 static DEVICE_ATTR_RO(modalias);
1035 static ssize_t supports_autosuspend_show(struct device *dev,
1036 struct device_attribute *attr,
1041 s = device_lock_interruptible(dev);
1044 /* Devices will be autosuspended even when an interface isn't claimed */
1045 s = (!dev->driver || to_usb_driver(dev->driver)->supports_autosuspend);
1048 return sprintf(buf, "%u\n", s);
1050 static DEVICE_ATTR_RO(supports_autosuspend);
1053 * interface_authorized_show - show authorization status of an USB interface
1054 * 1 is authorized, 0 is deauthorized
1056 static ssize_t interface_authorized_show(struct device *dev,
1057 struct device_attribute *attr, char *buf)
1059 struct usb_interface *intf = to_usb_interface(dev);
1061 return sprintf(buf, "%u\n", intf->authorized);
1065 * interface_authorized_store - authorize or deauthorize an USB interface
1067 static ssize_t interface_authorized_store(struct device *dev,
1068 struct device_attribute *attr, const char *buf, size_t count)
1070 struct usb_interface *intf = to_usb_interface(dev);
1073 if (strtobool(buf, &val) != 0)
1077 usb_authorize_interface(intf);
1079 usb_deauthorize_interface(intf);
1083 static struct device_attribute dev_attr_interface_authorized =
1084 __ATTR(authorized, S_IRUGO | S_IWUSR,
1085 interface_authorized_show, interface_authorized_store);
1087 static struct attribute *intf_attrs[] = {
1088 &dev_attr_bInterfaceNumber.attr,
1089 &dev_attr_bAlternateSetting.attr,
1090 &dev_attr_bNumEndpoints.attr,
1091 &dev_attr_bInterfaceClass.attr,
1092 &dev_attr_bInterfaceSubClass.attr,
1093 &dev_attr_bInterfaceProtocol.attr,
1094 &dev_attr_modalias.attr,
1095 &dev_attr_supports_autosuspend.attr,
1096 &dev_attr_interface_authorized.attr,
1099 static struct attribute_group intf_attr_grp = {
1100 .attrs = intf_attrs,
1103 static struct attribute *intf_assoc_attrs[] = {
1104 &dev_attr_iad_bFirstInterface.attr,
1105 &dev_attr_iad_bInterfaceCount.attr,
1106 &dev_attr_iad_bFunctionClass.attr,
1107 &dev_attr_iad_bFunctionSubClass.attr,
1108 &dev_attr_iad_bFunctionProtocol.attr,
1112 static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
1113 struct attribute *a, int n)
1115 struct device *dev = container_of(kobj, struct device, kobj);
1116 struct usb_interface *intf = to_usb_interface(dev);
1118 if (intf->intf_assoc == NULL)
1123 static struct attribute_group intf_assoc_attr_grp = {
1124 .attrs = intf_assoc_attrs,
1125 .is_visible = intf_assoc_attrs_are_visible,
1128 const struct attribute_group *usb_interface_groups[] = {
1130 &intf_assoc_attr_grp,
1134 void usb_create_sysfs_intf_files(struct usb_interface *intf)
1136 struct usb_device *udev = interface_to_usbdev(intf);
1137 struct usb_host_interface *alt = intf->cur_altsetting;
1139 if (intf->sysfs_files_created || intf->unregistering)
1142 if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
1143 alt->string = usb_cache_string(udev, alt->desc.iInterface);
1144 if (alt->string && device_create_file(&intf->dev, &dev_attr_interface))
1145 ; /* We don't actually care if the function fails. */
1146 intf->sysfs_files_created = 1;
1149 void usb_remove_sysfs_intf_files(struct usb_interface *intf)
1151 if (!intf->sysfs_files_created)
1154 device_remove_file(&intf->dev, &dev_attr_interface);
1155 intf->sysfs_files_created = 0;