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/hcd.h>
19 #include <linux/usb/quirks.h>
23 /* Active configuration fields */
24 #define usb_actconfig_show(field, format_string) \
25 static ssize_t field##_show(struct device *dev, \
26 struct device_attribute *attr, char *buf) \
28 struct usb_device *udev; \
29 struct usb_host_config *actconfig; \
32 udev = to_usb_device(dev); \
33 rc = usb_lock_device_interruptible(udev); \
36 actconfig = udev->actconfig; \
38 rc = sprintf(buf, format_string, \
39 actconfig->desc.field); \
40 usb_unlock_device(udev); \
44 #define usb_actconfig_attr(field, format_string) \
45 usb_actconfig_show(field, format_string) \
46 static DEVICE_ATTR_RO(field)
48 usb_actconfig_attr(bNumInterfaces
, "%2d\n");
49 usb_actconfig_attr(bmAttributes
, "%2x\n");
51 static ssize_t
bMaxPower_show(struct device
*dev
,
52 struct device_attribute
*attr
, char *buf
)
54 struct usb_device
*udev
;
55 struct usb_host_config
*actconfig
;
58 udev
= to_usb_device(dev
);
59 rc
= usb_lock_device_interruptible(udev
);
62 actconfig
= udev
->actconfig
;
64 rc
= sprintf(buf
, "%dmA\n", usb_get_max_power(udev
, actconfig
));
65 usb_unlock_device(udev
);
68 static DEVICE_ATTR_RO(bMaxPower
);
70 static ssize_t
configuration_show(struct device
*dev
,
71 struct device_attribute
*attr
, char *buf
)
73 struct usb_device
*udev
;
74 struct usb_host_config
*actconfig
;
77 udev
= to_usb_device(dev
);
78 rc
= usb_lock_device_interruptible(udev
);
81 actconfig
= udev
->actconfig
;
82 if (actconfig
&& actconfig
->string
)
83 rc
= sprintf(buf
, "%s\n", actconfig
->string
);
84 usb_unlock_device(udev
);
87 static DEVICE_ATTR_RO(configuration
);
89 /* configuration value is always present, and r/w */
90 usb_actconfig_show(bConfigurationValue
, "%u\n");
92 static ssize_t
bConfigurationValue_store(struct device
*dev
,
93 struct device_attribute
*attr
,
94 const char *buf
, size_t count
)
96 struct usb_device
*udev
= to_usb_device(dev
);
97 int config
, value
, rc
;
99 if (sscanf(buf
, "%d", &config
) != 1 || config
< -1 || config
> 255)
101 rc
= usb_lock_device_interruptible(udev
);
104 value
= usb_set_configuration(udev
, config
);
105 usb_unlock_device(udev
);
106 return (value
< 0) ? value
: count
;
108 static DEVICE_ATTR_IGNORE_LOCKDEP(bConfigurationValue
, S_IRUGO
| S_IWUSR
,
109 bConfigurationValue_show
, bConfigurationValue_store
);
112 static ssize_t
devspec_show(struct device
*dev
, struct device_attribute
*attr
,
115 struct device_node
*of_node
= dev
->of_node
;
117 return sprintf(buf
, "%pOF\n", of_node
);
119 static DEVICE_ATTR_RO(devspec
);
123 #define usb_string_attr(name) \
124 static ssize_t name##_show(struct device *dev, \
125 struct device_attribute *attr, char *buf) \
127 struct usb_device *udev; \
130 udev = to_usb_device(dev); \
131 retval = usb_lock_device_interruptible(udev); \
134 retval = sprintf(buf, "%s\n", udev->name); \
135 usb_unlock_device(udev); \
138 static DEVICE_ATTR_RO(name)
140 usb_string_attr(product
);
141 usb_string_attr(manufacturer
);
142 usb_string_attr(serial
);
144 static ssize_t
speed_show(struct device
*dev
, struct device_attribute
*attr
,
147 struct usb_device
*udev
;
150 udev
= to_usb_device(dev
);
152 switch (udev
->speed
) {
156 case USB_SPEED_UNKNOWN
:
163 case USB_SPEED_WIRELESS
:
166 case USB_SPEED_SUPER
:
169 case USB_SPEED_SUPER_PLUS
:
175 return sprintf(buf
, "%s\n", speed
);
177 static DEVICE_ATTR_RO(speed
);
179 static ssize_t
rx_lanes_show(struct device
*dev
, struct device_attribute
*attr
,
182 struct usb_device
*udev
;
184 udev
= to_usb_device(dev
);
185 return sprintf(buf
, "%d\n", udev
->rx_lanes
);
187 static DEVICE_ATTR_RO(rx_lanes
);
189 static ssize_t
tx_lanes_show(struct device
*dev
, struct device_attribute
*attr
,
192 struct usb_device
*udev
;
194 udev
= to_usb_device(dev
);
195 return sprintf(buf
, "%d\n", udev
->tx_lanes
);
197 static DEVICE_ATTR_RO(tx_lanes
);
199 static ssize_t
busnum_show(struct device
*dev
, struct device_attribute
*attr
,
202 struct usb_device
*udev
;
204 udev
= to_usb_device(dev
);
205 return sprintf(buf
, "%d\n", udev
->bus
->busnum
);
207 static DEVICE_ATTR_RO(busnum
);
209 static ssize_t
devnum_show(struct device
*dev
, struct device_attribute
*attr
,
212 struct usb_device
*udev
;
214 udev
= to_usb_device(dev
);
215 return sprintf(buf
, "%d\n", udev
->devnum
);
217 static DEVICE_ATTR_RO(devnum
);
219 static ssize_t
devpath_show(struct device
*dev
, struct device_attribute
*attr
,
222 struct usb_device
*udev
;
224 udev
= to_usb_device(dev
);
225 return sprintf(buf
, "%s\n", udev
->devpath
);
227 static DEVICE_ATTR_RO(devpath
);
229 static ssize_t
version_show(struct device
*dev
, struct device_attribute
*attr
,
232 struct usb_device
*udev
;
235 udev
= to_usb_device(dev
);
236 bcdUSB
= le16_to_cpu(udev
->descriptor
.bcdUSB
);
237 return sprintf(buf
, "%2x.%02x\n", bcdUSB
>> 8, bcdUSB
& 0xff);
239 static DEVICE_ATTR_RO(version
);
241 static ssize_t
maxchild_show(struct device
*dev
, struct device_attribute
*attr
,
244 struct usb_device
*udev
;
246 udev
= to_usb_device(dev
);
247 return sprintf(buf
, "%d\n", udev
->maxchild
);
249 static DEVICE_ATTR_RO(maxchild
);
251 static ssize_t
quirks_show(struct device
*dev
, struct device_attribute
*attr
,
254 struct usb_device
*udev
;
256 udev
= to_usb_device(dev
);
257 return sprintf(buf
, "0x%x\n", udev
->quirks
);
259 static DEVICE_ATTR_RO(quirks
);
261 static ssize_t
avoid_reset_quirk_show(struct device
*dev
,
262 struct device_attribute
*attr
, char *buf
)
264 struct usb_device
*udev
;
266 udev
= to_usb_device(dev
);
267 return sprintf(buf
, "%d\n", !!(udev
->quirks
& USB_QUIRK_RESET
));
270 static ssize_t
avoid_reset_quirk_store(struct device
*dev
,
271 struct device_attribute
*attr
,
272 const char *buf
, size_t count
)
274 struct usb_device
*udev
= to_usb_device(dev
);
277 if (sscanf(buf
, "%d", &val
) != 1 || val
< 0 || val
> 1)
279 rc
= usb_lock_device_interruptible(udev
);
283 udev
->quirks
|= USB_QUIRK_RESET
;
285 udev
->quirks
&= ~USB_QUIRK_RESET
;
286 usb_unlock_device(udev
);
289 static DEVICE_ATTR_RW(avoid_reset_quirk
);
291 static ssize_t
urbnum_show(struct device
*dev
, struct device_attribute
*attr
,
294 struct usb_device
*udev
;
296 udev
= to_usb_device(dev
);
297 return sprintf(buf
, "%d\n", atomic_read(&udev
->urbnum
));
299 static DEVICE_ATTR_RO(urbnum
);
301 static ssize_t
removable_show(struct device
*dev
, struct device_attribute
*attr
,
304 struct usb_device
*udev
;
307 udev
= to_usb_device(dev
);
309 switch (udev
->removable
) {
310 case USB_DEVICE_REMOVABLE
:
313 case USB_DEVICE_FIXED
:
320 return sprintf(buf
, "%s\n", state
);
322 static DEVICE_ATTR_RO(removable
);
324 static ssize_t
ltm_capable_show(struct device
*dev
,
325 struct device_attribute
*attr
, char *buf
)
327 if (usb_device_supports_ltm(to_usb_device(dev
)))
328 return sprintf(buf
, "%s\n", "yes");
329 return sprintf(buf
, "%s\n", "no");
331 static DEVICE_ATTR_RO(ltm_capable
);
335 static ssize_t
persist_show(struct device
*dev
, struct device_attribute
*attr
,
338 struct usb_device
*udev
= to_usb_device(dev
);
340 return sprintf(buf
, "%d\n", udev
->persist_enabled
);
343 static ssize_t
persist_store(struct device
*dev
, struct device_attribute
*attr
,
344 const char *buf
, size_t count
)
346 struct usb_device
*udev
= to_usb_device(dev
);
349 /* Hubs are always enabled for USB_PERSIST */
350 if (udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
)
353 if (sscanf(buf
, "%d", &value
) != 1)
356 rc
= usb_lock_device_interruptible(udev
);
359 udev
->persist_enabled
= !!value
;
360 usb_unlock_device(udev
);
363 static DEVICE_ATTR_RW(persist
);
365 static int add_persist_attributes(struct device
*dev
)
369 if (is_usb_device(dev
)) {
370 struct usb_device
*udev
= to_usb_device(dev
);
372 /* Hubs are automatically enabled for USB_PERSIST,
373 * no point in creating the attribute file.
375 if (udev
->descriptor
.bDeviceClass
!= USB_CLASS_HUB
)
376 rc
= sysfs_add_file_to_group(&dev
->kobj
,
377 &dev_attr_persist
.attr
,
383 static void remove_persist_attributes(struct device
*dev
)
385 sysfs_remove_file_from_group(&dev
->kobj
,
386 &dev_attr_persist
.attr
,
390 static ssize_t
connected_duration_show(struct device
*dev
,
391 struct device_attribute
*attr
, char *buf
)
393 struct usb_device
*udev
= to_usb_device(dev
);
395 return sprintf(buf
, "%u\n",
396 jiffies_to_msecs(jiffies
- udev
->connect_time
));
398 static DEVICE_ATTR_RO(connected_duration
);
401 * If the device is resumed, the last time the device was suspended has
402 * been pre-subtracted from active_duration. We add the current time to
403 * get the duration that the device was actually active.
405 * If the device is suspended, the active_duration is up-to-date.
407 static ssize_t
active_duration_show(struct device
*dev
,
408 struct device_attribute
*attr
, char *buf
)
410 struct usb_device
*udev
= to_usb_device(dev
);
413 if (udev
->state
!= USB_STATE_SUSPENDED
)
414 duration
= jiffies_to_msecs(jiffies
+ udev
->active_duration
);
416 duration
= jiffies_to_msecs(udev
->active_duration
);
417 return sprintf(buf
, "%u\n", duration
);
419 static DEVICE_ATTR_RO(active_duration
);
421 static ssize_t
autosuspend_show(struct device
*dev
,
422 struct device_attribute
*attr
, char *buf
)
424 return sprintf(buf
, "%d\n", dev
->power
.autosuspend_delay
/ 1000);
427 static ssize_t
autosuspend_store(struct device
*dev
,
428 struct device_attribute
*attr
, const char *buf
,
433 if (sscanf(buf
, "%d", &value
) != 1 || value
>= INT_MAX
/1000 ||
434 value
<= -INT_MAX
/1000)
437 pm_runtime_set_autosuspend_delay(dev
, value
* 1000);
440 static DEVICE_ATTR_RW(autosuspend
);
442 static const char on_string
[] = "on";
443 static const char auto_string
[] = "auto";
445 static void warn_level(void)
447 static int level_warned
;
451 printk(KERN_WARNING
"WARNING! power/level is deprecated; "
452 "use power/control instead\n");
456 static ssize_t
level_show(struct device
*dev
, struct device_attribute
*attr
,
459 struct usb_device
*udev
= to_usb_device(dev
);
460 const char *p
= auto_string
;
463 if (udev
->state
!= USB_STATE_SUSPENDED
&& !udev
->dev
.power
.runtime_auto
)
465 return sprintf(buf
, "%s\n", p
);
468 static ssize_t
level_store(struct device
*dev
, struct device_attribute
*attr
,
469 const char *buf
, size_t count
)
471 struct usb_device
*udev
= to_usb_device(dev
);
478 cp
= memchr(buf
, '\n', count
);
482 rv
= usb_lock_device_interruptible(udev
);
486 if (len
== sizeof on_string
- 1 &&
487 strncmp(buf
, on_string
, len
) == 0)
488 usb_disable_autosuspend(udev
);
490 else if (len
== sizeof auto_string
- 1 &&
491 strncmp(buf
, auto_string
, len
) == 0)
492 usb_enable_autosuspend(udev
);
497 usb_unlock_device(udev
);
500 static DEVICE_ATTR_RW(level
);
502 static ssize_t
usb2_hardware_lpm_show(struct device
*dev
,
503 struct device_attribute
*attr
, char *buf
)
505 struct usb_device
*udev
= to_usb_device(dev
);
508 if (udev
->usb2_hw_lpm_allowed
== 1)
513 return sprintf(buf
, "%s\n", p
);
516 static ssize_t
usb2_hardware_lpm_store(struct device
*dev
,
517 struct device_attribute
*attr
,
518 const char *buf
, size_t count
)
520 struct usb_device
*udev
= to_usb_device(dev
);
524 ret
= usb_lock_device_interruptible(udev
);
528 ret
= strtobool(buf
, &value
);
531 udev
->usb2_hw_lpm_allowed
= value
;
533 ret
= usb_enable_usb2_hardware_lpm(udev
);
535 ret
= usb_disable_usb2_hardware_lpm(udev
);
538 usb_unlock_device(udev
);
545 static DEVICE_ATTR_RW(usb2_hardware_lpm
);
547 static ssize_t
usb2_lpm_l1_timeout_show(struct device
*dev
,
548 struct device_attribute
*attr
,
551 struct usb_device
*udev
= to_usb_device(dev
);
552 return sprintf(buf
, "%d\n", udev
->l1_params
.timeout
);
555 static ssize_t
usb2_lpm_l1_timeout_store(struct device
*dev
,
556 struct device_attribute
*attr
,
557 const char *buf
, size_t count
)
559 struct usb_device
*udev
= to_usb_device(dev
);
562 if (kstrtou16(buf
, 0, &timeout
))
565 udev
->l1_params
.timeout
= timeout
;
569 static DEVICE_ATTR_RW(usb2_lpm_l1_timeout
);
571 static ssize_t
usb2_lpm_besl_show(struct device
*dev
,
572 struct device_attribute
*attr
, char *buf
)
574 struct usb_device
*udev
= to_usb_device(dev
);
575 return sprintf(buf
, "%d\n", udev
->l1_params
.besl
);
578 static ssize_t
usb2_lpm_besl_store(struct device
*dev
,
579 struct device_attribute
*attr
,
580 const char *buf
, size_t count
)
582 struct usb_device
*udev
= to_usb_device(dev
);
585 if (kstrtou8(buf
, 0, &besl
) || besl
> 15)
588 udev
->l1_params
.besl
= besl
;
592 static DEVICE_ATTR_RW(usb2_lpm_besl
);
594 static ssize_t
usb3_hardware_lpm_u1_show(struct device
*dev
,
595 struct device_attribute
*attr
, char *buf
)
597 struct usb_device
*udev
= to_usb_device(dev
);
601 rc
= usb_lock_device_interruptible(udev
);
605 if (udev
->usb3_lpm_u1_enabled
)
610 usb_unlock_device(udev
);
612 return sprintf(buf
, "%s\n", p
);
614 static DEVICE_ATTR_RO(usb3_hardware_lpm_u1
);
616 static ssize_t
usb3_hardware_lpm_u2_show(struct device
*dev
,
617 struct device_attribute
*attr
, char *buf
)
619 struct usb_device
*udev
= to_usb_device(dev
);
623 rc
= usb_lock_device_interruptible(udev
);
627 if (udev
->usb3_lpm_u2_enabled
)
632 usb_unlock_device(udev
);
634 return sprintf(buf
, "%s\n", p
);
636 static DEVICE_ATTR_RO(usb3_hardware_lpm_u2
);
638 static struct attribute
*usb2_hardware_lpm_attr
[] = {
639 &dev_attr_usb2_hardware_lpm
.attr
,
640 &dev_attr_usb2_lpm_l1_timeout
.attr
,
641 &dev_attr_usb2_lpm_besl
.attr
,
644 static const struct attribute_group usb2_hardware_lpm_attr_group
= {
645 .name
= power_group_name
,
646 .attrs
= usb2_hardware_lpm_attr
,
649 static struct attribute
*usb3_hardware_lpm_attr
[] = {
650 &dev_attr_usb3_hardware_lpm_u1
.attr
,
651 &dev_attr_usb3_hardware_lpm_u2
.attr
,
654 static const struct attribute_group usb3_hardware_lpm_attr_group
= {
655 .name
= power_group_name
,
656 .attrs
= usb3_hardware_lpm_attr
,
659 static struct attribute
*power_attrs
[] = {
660 &dev_attr_autosuspend
.attr
,
661 &dev_attr_level
.attr
,
662 &dev_attr_connected_duration
.attr
,
663 &dev_attr_active_duration
.attr
,
666 static const struct attribute_group power_attr_group
= {
667 .name
= power_group_name
,
668 .attrs
= power_attrs
,
671 static int add_power_attributes(struct device
*dev
)
675 if (is_usb_device(dev
)) {
676 struct usb_device
*udev
= to_usb_device(dev
);
677 rc
= sysfs_merge_group(&dev
->kobj
, &power_attr_group
);
678 if (udev
->usb2_hw_lpm_capable
== 1)
679 rc
= sysfs_merge_group(&dev
->kobj
,
680 &usb2_hardware_lpm_attr_group
);
681 if ((udev
->speed
== USB_SPEED_SUPER
||
682 udev
->speed
== USB_SPEED_SUPER_PLUS
) &&
683 udev
->lpm_capable
== 1)
684 rc
= sysfs_merge_group(&dev
->kobj
,
685 &usb3_hardware_lpm_attr_group
);
691 static void remove_power_attributes(struct device
*dev
)
693 sysfs_unmerge_group(&dev
->kobj
, &usb2_hardware_lpm_attr_group
);
694 sysfs_unmerge_group(&dev
->kobj
, &power_attr_group
);
699 #define add_persist_attributes(dev) 0
700 #define remove_persist_attributes(dev) do {} while (0)
702 #define add_power_attributes(dev) 0
703 #define remove_power_attributes(dev) do {} while (0)
705 #endif /* CONFIG_PM */
708 /* Descriptor fields */
709 #define usb_descriptor_attr_le16(field, format_string) \
711 field##_show(struct device *dev, struct device_attribute *attr, \
714 struct usb_device *udev; \
716 udev = to_usb_device(dev); \
717 return sprintf(buf, format_string, \
718 le16_to_cpu(udev->descriptor.field)); \
720 static DEVICE_ATTR_RO(field)
722 usb_descriptor_attr_le16(idVendor
, "%04x\n");
723 usb_descriptor_attr_le16(idProduct
, "%04x\n");
724 usb_descriptor_attr_le16(bcdDevice
, "%04x\n");
726 #define usb_descriptor_attr(field, format_string) \
728 field##_show(struct device *dev, struct device_attribute *attr, \
731 struct usb_device *udev; \
733 udev = to_usb_device(dev); \
734 return sprintf(buf, format_string, udev->descriptor.field); \
736 static DEVICE_ATTR_RO(field)
738 usb_descriptor_attr(bDeviceClass
, "%02x\n");
739 usb_descriptor_attr(bDeviceSubClass
, "%02x\n");
740 usb_descriptor_attr(bDeviceProtocol
, "%02x\n");
741 usb_descriptor_attr(bNumConfigurations
, "%d\n");
742 usb_descriptor_attr(bMaxPacketSize0
, "%d\n");
745 /* show if the device is authorized (1) or not (0) */
746 static ssize_t
authorized_show(struct device
*dev
,
747 struct device_attribute
*attr
, char *buf
)
749 struct usb_device
*usb_dev
= to_usb_device(dev
);
750 return snprintf(buf
, PAGE_SIZE
, "%u\n", usb_dev
->authorized
);
754 * Authorize a device to be used in the system
756 * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
758 static ssize_t
authorized_store(struct device
*dev
,
759 struct device_attribute
*attr
, const char *buf
,
763 struct usb_device
*usb_dev
= to_usb_device(dev
);
765 result
= sscanf(buf
, "%u\n", &val
);
769 result
= usb_deauthorize_device(usb_dev
);
771 result
= usb_authorize_device(usb_dev
);
772 return result
< 0 ? result
: size
;
774 static DEVICE_ATTR_IGNORE_LOCKDEP(authorized
, S_IRUGO
| S_IWUSR
,
775 authorized_show
, authorized_store
);
777 /* "Safely remove a device" */
778 static ssize_t
remove_store(struct device
*dev
, struct device_attribute
*attr
,
779 const char *buf
, size_t count
)
781 struct usb_device
*udev
= to_usb_device(dev
);
784 usb_lock_device(udev
);
785 if (udev
->state
!= USB_STATE_NOTATTACHED
) {
787 /* To avoid races, first unconfigure and then remove */
788 usb_set_configuration(udev
, -1);
789 rc
= usb_remove_device(udev
);
793 usb_unlock_device(udev
);
796 static DEVICE_ATTR_IGNORE_LOCKDEP(remove
, S_IWUSR
, NULL
, remove_store
);
799 static struct attribute
*dev_attrs
[] = {
800 /* current configuration's attributes */
801 &dev_attr_configuration
.attr
,
802 &dev_attr_bNumInterfaces
.attr
,
803 &dev_attr_bConfigurationValue
.attr
,
804 &dev_attr_bmAttributes
.attr
,
805 &dev_attr_bMaxPower
.attr
,
806 /* device attributes */
807 &dev_attr_urbnum
.attr
,
808 &dev_attr_idVendor
.attr
,
809 &dev_attr_idProduct
.attr
,
810 &dev_attr_bcdDevice
.attr
,
811 &dev_attr_bDeviceClass
.attr
,
812 &dev_attr_bDeviceSubClass
.attr
,
813 &dev_attr_bDeviceProtocol
.attr
,
814 &dev_attr_bNumConfigurations
.attr
,
815 &dev_attr_bMaxPacketSize0
.attr
,
816 &dev_attr_speed
.attr
,
817 &dev_attr_rx_lanes
.attr
,
818 &dev_attr_tx_lanes
.attr
,
819 &dev_attr_busnum
.attr
,
820 &dev_attr_devnum
.attr
,
821 &dev_attr_devpath
.attr
,
822 &dev_attr_version
.attr
,
823 &dev_attr_maxchild
.attr
,
824 &dev_attr_quirks
.attr
,
825 &dev_attr_avoid_reset_quirk
.attr
,
826 &dev_attr_authorized
.attr
,
827 &dev_attr_remove
.attr
,
828 &dev_attr_removable
.attr
,
829 &dev_attr_ltm_capable
.attr
,
831 &dev_attr_devspec
.attr
,
835 static const struct attribute_group dev_attr_grp
= {
839 /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
842 static struct attribute
*dev_string_attrs
[] = {
843 &dev_attr_manufacturer
.attr
,
844 &dev_attr_product
.attr
,
845 &dev_attr_serial
.attr
,
849 static umode_t
dev_string_attrs_are_visible(struct kobject
*kobj
,
850 struct attribute
*a
, int n
)
852 struct device
*dev
= kobj_to_dev(kobj
);
853 struct usb_device
*udev
= to_usb_device(dev
);
855 if (a
== &dev_attr_manufacturer
.attr
) {
856 if (udev
->manufacturer
== NULL
)
858 } else if (a
== &dev_attr_product
.attr
) {
859 if (udev
->product
== NULL
)
861 } else if (a
== &dev_attr_serial
.attr
) {
862 if (udev
->serial
== NULL
)
868 static const struct attribute_group dev_string_attr_grp
= {
869 .attrs
= dev_string_attrs
,
870 .is_visible
= dev_string_attrs_are_visible
,
873 const struct attribute_group
*usb_device_groups
[] = {
875 &dev_string_attr_grp
,
879 /* Binary descriptors */
882 read_descriptors(struct file
*filp
, struct kobject
*kobj
,
883 struct bin_attribute
*attr
,
884 char *buf
, loff_t off
, size_t count
)
886 struct device
*dev
= kobj_to_dev(kobj
);
887 struct usb_device
*udev
= to_usb_device(dev
);
888 size_t nleft
= count
;
894 retval
= usb_lock_device_interruptible(udev
);
897 /* The binary attribute begins with the device descriptor.
898 * Following that are the raw descriptor entries for all the
899 * configurations (config plus subsidiary descriptors).
901 for (cfgno
= -1; cfgno
< udev
->descriptor
.bNumConfigurations
&&
902 nleft
> 0; ++cfgno
) {
904 src
= &udev
->descriptor
;
905 srclen
= sizeof(struct usb_device_descriptor
);
907 src
= udev
->rawdescriptors
[cfgno
];
908 srclen
= __le16_to_cpu(udev
->config
[cfgno
].desc
.
912 n
= min(nleft
, srclen
- (size_t) off
);
913 memcpy(buf
, src
+ off
, n
);
921 usb_unlock_device(udev
);
922 return count
- nleft
;
925 static struct bin_attribute dev_bin_attr_descriptors
= {
926 .attr
= {.name
= "descriptors", .mode
= 0444},
927 .read
= read_descriptors
,
928 .size
= 18 + 65535, /* dev descr + max-size raw descriptor */
932 * Show & store the current value of authorized_default
934 static ssize_t
authorized_default_show(struct device
*dev
,
935 struct device_attribute
*attr
, char *buf
)
937 struct usb_device
*rh_usb_dev
= to_usb_device(dev
);
938 struct usb_bus
*usb_bus
= rh_usb_dev
->bus
;
941 hcd
= bus_to_hcd(usb_bus
);
942 return snprintf(buf
, PAGE_SIZE
, "%u\n", hcd
->dev_policy
);
945 static ssize_t
authorized_default_store(struct device
*dev
,
946 struct device_attribute
*attr
,
947 const char *buf
, size_t size
)
951 struct usb_device
*rh_usb_dev
= to_usb_device(dev
);
952 struct usb_bus
*usb_bus
= rh_usb_dev
->bus
;
955 hcd
= bus_to_hcd(usb_bus
);
956 result
= sscanf(buf
, "%u\n", &val
);
958 hcd
->dev_policy
= val
<= USB_DEVICE_AUTHORIZE_INTERNAL
?
959 val
: USB_DEVICE_AUTHORIZE_ALL
;
966 static DEVICE_ATTR_RW(authorized_default
);
969 * interface_authorized_default_show - show default authorization status
972 * note: interface_authorized_default is the default value
973 * for initializing the authorized attribute of interfaces
975 static ssize_t
interface_authorized_default_show(struct device
*dev
,
976 struct device_attribute
*attr
, char *buf
)
978 struct usb_device
*usb_dev
= to_usb_device(dev
);
979 struct usb_hcd
*hcd
= bus_to_hcd(usb_dev
->bus
);
981 return sprintf(buf
, "%u\n", !!HCD_INTF_AUTHORIZED(hcd
));
985 * interface_authorized_default_store - store default authorization status
988 * note: interface_authorized_default is the default value
989 * for initializing the authorized attribute of interfaces
991 static ssize_t
interface_authorized_default_store(struct device
*dev
,
992 struct device_attribute
*attr
, const char *buf
, size_t count
)
994 struct usb_device
*usb_dev
= to_usb_device(dev
);
995 struct usb_hcd
*hcd
= bus_to_hcd(usb_dev
->bus
);
999 if (strtobool(buf
, &val
) != 0)
1003 set_bit(HCD_FLAG_INTF_AUTHORIZED
, &hcd
->flags
);
1005 clear_bit(HCD_FLAG_INTF_AUTHORIZED
, &hcd
->flags
);
1009 static DEVICE_ATTR_RW(interface_authorized_default
);
1011 /* Group all the USB bus attributes */
1012 static struct attribute
*usb_bus_attrs
[] = {
1013 &dev_attr_authorized_default
.attr
,
1014 &dev_attr_interface_authorized_default
.attr
,
1018 static const struct attribute_group usb_bus_attr_group
= {
1019 .name
= NULL
, /* we want them in the same directory */
1020 .attrs
= usb_bus_attrs
,
1024 static int add_default_authorized_attributes(struct device
*dev
)
1028 if (is_usb_device(dev
))
1029 rc
= sysfs_create_group(&dev
->kobj
, &usb_bus_attr_group
);
1034 static void remove_default_authorized_attributes(struct device
*dev
)
1036 if (is_usb_device(dev
)) {
1037 sysfs_remove_group(&dev
->kobj
, &usb_bus_attr_group
);
1041 int usb_create_sysfs_dev_files(struct usb_device
*udev
)
1043 struct device
*dev
= &udev
->dev
;
1046 retval
= device_create_bin_file(dev
, &dev_bin_attr_descriptors
);
1050 retval
= add_persist_attributes(dev
);
1054 retval
= add_power_attributes(dev
);
1058 if (is_root_hub(udev
)) {
1059 retval
= add_default_authorized_attributes(dev
);
1066 usb_remove_sysfs_dev_files(udev
);
1070 void usb_remove_sysfs_dev_files(struct usb_device
*udev
)
1072 struct device
*dev
= &udev
->dev
;
1074 if (is_root_hub(udev
))
1075 remove_default_authorized_attributes(dev
);
1077 remove_power_attributes(dev
);
1078 remove_persist_attributes(dev
);
1079 device_remove_bin_file(dev
, &dev_bin_attr_descriptors
);
1082 /* Interface Association Descriptor fields */
1083 #define usb_intf_assoc_attr(field, format_string) \
1085 iad_##field##_show(struct device *dev, struct device_attribute *attr, \
1088 struct usb_interface *intf = to_usb_interface(dev); \
1090 return sprintf(buf, format_string, \
1091 intf->intf_assoc->field); \
1093 static DEVICE_ATTR_RO(iad_##field)
1095 usb_intf_assoc_attr(bFirstInterface
, "%02x\n");
1096 usb_intf_assoc_attr(bInterfaceCount
, "%02d\n");
1097 usb_intf_assoc_attr(bFunctionClass
, "%02x\n");
1098 usb_intf_assoc_attr(bFunctionSubClass
, "%02x\n");
1099 usb_intf_assoc_attr(bFunctionProtocol
, "%02x\n");
1101 /* Interface fields */
1102 #define usb_intf_attr(field, format_string) \
1104 field##_show(struct device *dev, struct device_attribute *attr, \
1107 struct usb_interface *intf = to_usb_interface(dev); \
1109 return sprintf(buf, format_string, \
1110 intf->cur_altsetting->desc.field); \
1112 static DEVICE_ATTR_RO(field)
1114 usb_intf_attr(bInterfaceNumber
, "%02x\n");
1115 usb_intf_attr(bAlternateSetting
, "%2d\n");
1116 usb_intf_attr(bNumEndpoints
, "%02x\n");
1117 usb_intf_attr(bInterfaceClass
, "%02x\n");
1118 usb_intf_attr(bInterfaceSubClass
, "%02x\n");
1119 usb_intf_attr(bInterfaceProtocol
, "%02x\n");
1121 static ssize_t
interface_show(struct device
*dev
, struct device_attribute
*attr
,
1124 struct usb_interface
*intf
;
1127 intf
= to_usb_interface(dev
);
1128 string
= READ_ONCE(intf
->cur_altsetting
->string
);
1131 return sprintf(buf
, "%s\n", string
);
1133 static DEVICE_ATTR_RO(interface
);
1135 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
,
1138 struct usb_interface
*intf
;
1139 struct usb_device
*udev
;
1140 struct usb_host_interface
*alt
;
1142 intf
= to_usb_interface(dev
);
1143 udev
= interface_to_usbdev(intf
);
1144 alt
= READ_ONCE(intf
->cur_altsetting
);
1146 return sprintf(buf
, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
1147 "ic%02Xisc%02Xip%02Xin%02X\n",
1148 le16_to_cpu(udev
->descriptor
.idVendor
),
1149 le16_to_cpu(udev
->descriptor
.idProduct
),
1150 le16_to_cpu(udev
->descriptor
.bcdDevice
),
1151 udev
->descriptor
.bDeviceClass
,
1152 udev
->descriptor
.bDeviceSubClass
,
1153 udev
->descriptor
.bDeviceProtocol
,
1154 alt
->desc
.bInterfaceClass
,
1155 alt
->desc
.bInterfaceSubClass
,
1156 alt
->desc
.bInterfaceProtocol
,
1157 alt
->desc
.bInterfaceNumber
);
1159 static DEVICE_ATTR_RO(modalias
);
1161 static ssize_t
supports_autosuspend_show(struct device
*dev
,
1162 struct device_attribute
*attr
,
1167 s
= device_lock_interruptible(dev
);
1170 /* Devices will be autosuspended even when an interface isn't claimed */
1171 s
= (!dev
->driver
|| to_usb_driver(dev
->driver
)->supports_autosuspend
);
1174 return sprintf(buf
, "%u\n", s
);
1176 static DEVICE_ATTR_RO(supports_autosuspend
);
1179 * interface_authorized_show - show authorization status of an USB interface
1180 * 1 is authorized, 0 is deauthorized
1182 static ssize_t
interface_authorized_show(struct device
*dev
,
1183 struct device_attribute
*attr
, char *buf
)
1185 struct usb_interface
*intf
= to_usb_interface(dev
);
1187 return sprintf(buf
, "%u\n", intf
->authorized
);
1191 * interface_authorized_store - authorize or deauthorize an USB interface
1193 static ssize_t
interface_authorized_store(struct device
*dev
,
1194 struct device_attribute
*attr
, const char *buf
, size_t count
)
1196 struct usb_interface
*intf
= to_usb_interface(dev
);
1199 if (strtobool(buf
, &val
) != 0)
1203 usb_authorize_interface(intf
);
1205 usb_deauthorize_interface(intf
);
1209 static struct device_attribute dev_attr_interface_authorized
=
1210 __ATTR(authorized
, S_IRUGO
| S_IWUSR
,
1211 interface_authorized_show
, interface_authorized_store
);
1213 static struct attribute
*intf_attrs
[] = {
1214 &dev_attr_bInterfaceNumber
.attr
,
1215 &dev_attr_bAlternateSetting
.attr
,
1216 &dev_attr_bNumEndpoints
.attr
,
1217 &dev_attr_bInterfaceClass
.attr
,
1218 &dev_attr_bInterfaceSubClass
.attr
,
1219 &dev_attr_bInterfaceProtocol
.attr
,
1220 &dev_attr_modalias
.attr
,
1221 &dev_attr_supports_autosuspend
.attr
,
1222 &dev_attr_interface_authorized
.attr
,
1225 static const struct attribute_group intf_attr_grp
= {
1226 .attrs
= intf_attrs
,
1229 static struct attribute
*intf_assoc_attrs
[] = {
1230 &dev_attr_iad_bFirstInterface
.attr
,
1231 &dev_attr_iad_bInterfaceCount
.attr
,
1232 &dev_attr_iad_bFunctionClass
.attr
,
1233 &dev_attr_iad_bFunctionSubClass
.attr
,
1234 &dev_attr_iad_bFunctionProtocol
.attr
,
1238 static umode_t
intf_assoc_attrs_are_visible(struct kobject
*kobj
,
1239 struct attribute
*a
, int n
)
1241 struct device
*dev
= kobj_to_dev(kobj
);
1242 struct usb_interface
*intf
= to_usb_interface(dev
);
1244 if (intf
->intf_assoc
== NULL
)
1249 static const struct attribute_group intf_assoc_attr_grp
= {
1250 .attrs
= intf_assoc_attrs
,
1251 .is_visible
= intf_assoc_attrs_are_visible
,
1254 const struct attribute_group
*usb_interface_groups
[] = {
1256 &intf_assoc_attr_grp
,
1260 void usb_create_sysfs_intf_files(struct usb_interface
*intf
)
1262 struct usb_device
*udev
= interface_to_usbdev(intf
);
1263 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
1265 if (intf
->sysfs_files_created
|| intf
->unregistering
)
1268 if (!alt
->string
&& !(udev
->quirks
& USB_QUIRK_CONFIG_INTF_STRINGS
))
1269 alt
->string
= usb_cache_string(udev
, alt
->desc
.iInterface
);
1270 if (alt
->string
&& device_create_file(&intf
->dev
, &dev_attr_interface
)) {
1271 /* This is not a serious error */
1272 dev_dbg(&intf
->dev
, "interface string descriptor file not created\n");
1274 intf
->sysfs_files_created
= 1;
1277 void usb_remove_sysfs_intf_files(struct usb_interface
*intf
)
1279 if (!intf
->sysfs_files_created
)
1282 device_remove_file(&intf
->dev
, &dev_attr_interface
);
1283 intf
->sysfs_files_created
= 0;