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, format_string) \
21 static ssize_t field##_show(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); \
36 #define usb_actconfig_attr(field, format_string) \
37 usb_actconfig_show(field, format_string) \
38 static DEVICE_ATTR_RO(field)
40 usb_actconfig_attr(bNumInterfaces
, "%2d\n");
41 usb_actconfig_attr(bmAttributes
, "%2x\n");
43 static ssize_t
bMaxPower_show(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
;
53 return sprintf(buf
, "%dmA\n", usb_get_max_power(udev
, actconfig
));
55 static DEVICE_ATTR_RO(bMaxPower
);
57 static ssize_t
configuration_show(struct device
*dev
,
58 struct device_attribute
*attr
, char *buf
)
60 struct usb_device
*udev
;
61 struct usb_host_config
*actconfig
;
63 udev
= to_usb_device(dev
);
64 actconfig
= udev
->actconfig
;
65 if ((!actconfig
) || (!actconfig
->string
))
67 return sprintf(buf
, "%s\n", actconfig
->string
);
69 static DEVICE_ATTR_RO(configuration
);
71 /* configuration value is always present, and r/w */
72 usb_actconfig_show(bConfigurationValue
, "%u\n");
74 static ssize_t
bConfigurationValue_store(struct device
*dev
,
75 struct device_attribute
*attr
,
76 const char *buf
, size_t count
)
78 struct usb_device
*udev
= to_usb_device(dev
);
81 if (sscanf(buf
, "%d", &config
) != 1 || config
< -1 || config
> 255)
83 usb_lock_device(udev
);
84 value
= usb_set_configuration(udev
, config
);
85 usb_unlock_device(udev
);
86 return (value
< 0) ? value
: count
;
88 static DEVICE_ATTR_IGNORE_LOCKDEP(bConfigurationValue
, S_IRUGO
| S_IWUSR
,
89 bConfigurationValue_show
, bConfigurationValue_store
);
92 #define usb_string_attr(name) \
93 static ssize_t name##_show(struct device *dev, \
94 struct device_attribute *attr, char *buf) \
96 struct usb_device *udev; \
99 udev = to_usb_device(dev); \
100 usb_lock_device(udev); \
101 retval = sprintf(buf, "%s\n", udev->name); \
102 usb_unlock_device(udev); \
105 static DEVICE_ATTR_RO(name)
107 usb_string_attr(product
);
108 usb_string_attr(manufacturer
);
109 usb_string_attr(serial
);
111 static ssize_t
speed_show(struct device
*dev
, struct device_attribute
*attr
,
114 struct usb_device
*udev
;
117 udev
= to_usb_device(dev
);
119 switch (udev
->speed
) {
123 case USB_SPEED_UNKNOWN
:
130 case USB_SPEED_WIRELESS
:
133 case USB_SPEED_SUPER
:
139 return sprintf(buf
, "%s\n", speed
);
141 static DEVICE_ATTR_RO(speed
);
143 static ssize_t
busnum_show(struct device
*dev
, struct device_attribute
*attr
,
146 struct usb_device
*udev
;
148 udev
= to_usb_device(dev
);
149 return sprintf(buf
, "%d\n", udev
->bus
->busnum
);
151 static DEVICE_ATTR_RO(busnum
);
153 static ssize_t
devnum_show(struct device
*dev
, struct device_attribute
*attr
,
156 struct usb_device
*udev
;
158 udev
= to_usb_device(dev
);
159 return sprintf(buf
, "%d\n", udev
->devnum
);
161 static DEVICE_ATTR_RO(devnum
);
163 static ssize_t
devpath_show(struct device
*dev
, struct device_attribute
*attr
,
166 struct usb_device
*udev
;
168 udev
= to_usb_device(dev
);
169 return sprintf(buf
, "%s\n", udev
->devpath
);
171 static DEVICE_ATTR_RO(devpath
);
173 static ssize_t
version_show(struct device
*dev
, struct device_attribute
*attr
,
176 struct usb_device
*udev
;
179 udev
= to_usb_device(dev
);
180 bcdUSB
= le16_to_cpu(udev
->descriptor
.bcdUSB
);
181 return sprintf(buf
, "%2x.%02x\n", bcdUSB
>> 8, bcdUSB
& 0xff);
183 static DEVICE_ATTR_RO(version
);
185 static ssize_t
maxchild_show(struct device
*dev
, struct device_attribute
*attr
,
188 struct usb_device
*udev
;
190 udev
= to_usb_device(dev
);
191 return sprintf(buf
, "%d\n", udev
->maxchild
);
193 static DEVICE_ATTR_RO(maxchild
);
195 static ssize_t
quirks_show(struct device
*dev
, struct device_attribute
*attr
,
198 struct usb_device
*udev
;
200 udev
= to_usb_device(dev
);
201 return sprintf(buf
, "0x%x\n", udev
->quirks
);
203 static DEVICE_ATTR_RO(quirks
);
205 static ssize_t
avoid_reset_quirk_show(struct device
*dev
,
206 struct device_attribute
*attr
, char *buf
)
208 struct usb_device
*udev
;
210 udev
= to_usb_device(dev
);
211 return sprintf(buf
, "%d\n", !!(udev
->quirks
& USB_QUIRK_RESET
));
214 static ssize_t
avoid_reset_quirk_store(struct device
*dev
,
215 struct device_attribute
*attr
,
216 const char *buf
, size_t count
)
218 struct usb_device
*udev
= to_usb_device(dev
);
221 if (sscanf(buf
, "%d", &val
) != 1 || val
< 0 || val
> 1)
223 usb_lock_device(udev
);
225 udev
->quirks
|= USB_QUIRK_RESET
;
227 udev
->quirks
&= ~USB_QUIRK_RESET
;
228 usb_unlock_device(udev
);
231 static DEVICE_ATTR_RW(avoid_reset_quirk
);
233 static ssize_t
urbnum_show(struct device
*dev
, struct device_attribute
*attr
,
236 struct usb_device
*udev
;
238 udev
= to_usb_device(dev
);
239 return sprintf(buf
, "%d\n", atomic_read(&udev
->urbnum
));
241 static DEVICE_ATTR_RO(urbnum
);
243 static ssize_t
removable_show(struct device
*dev
, struct device_attribute
*attr
,
246 struct usb_device
*udev
;
249 udev
= to_usb_device(dev
);
251 switch (udev
->removable
) {
252 case USB_DEVICE_REMOVABLE
:
255 case USB_DEVICE_FIXED
:
262 return sprintf(buf
, "%s\n", state
);
264 static DEVICE_ATTR_RO(removable
);
266 static ssize_t
ltm_capable_show(struct device
*dev
,
267 struct device_attribute
*attr
, char *buf
)
269 if (usb_device_supports_ltm(to_usb_device(dev
)))
270 return sprintf(buf
, "%s\n", "yes");
271 return sprintf(buf
, "%s\n", "no");
273 static DEVICE_ATTR_RO(ltm_capable
);
277 static ssize_t
persist_show(struct device
*dev
, struct device_attribute
*attr
,
280 struct usb_device
*udev
= to_usb_device(dev
);
282 return sprintf(buf
, "%d\n", udev
->persist_enabled
);
285 static ssize_t
persist_store(struct device
*dev
, struct device_attribute
*attr
,
286 const char *buf
, size_t count
)
288 struct usb_device
*udev
= to_usb_device(dev
);
291 /* Hubs are always enabled for USB_PERSIST */
292 if (udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
)
295 if (sscanf(buf
, "%d", &value
) != 1)
298 usb_lock_device(udev
);
299 udev
->persist_enabled
= !!value
;
300 usb_unlock_device(udev
);
303 static DEVICE_ATTR_RW(persist
);
305 static int add_persist_attributes(struct device
*dev
)
309 if (is_usb_device(dev
)) {
310 struct usb_device
*udev
= to_usb_device(dev
);
312 /* Hubs are automatically enabled for USB_PERSIST,
313 * no point in creating the attribute file.
315 if (udev
->descriptor
.bDeviceClass
!= USB_CLASS_HUB
)
316 rc
= sysfs_add_file_to_group(&dev
->kobj
,
317 &dev_attr_persist
.attr
,
323 static void remove_persist_attributes(struct device
*dev
)
325 sysfs_remove_file_from_group(&dev
->kobj
,
326 &dev_attr_persist
.attr
,
331 #define add_persist_attributes(dev) 0
332 #define remove_persist_attributes(dev) do {} while (0)
334 #endif /* CONFIG_PM */
336 #ifdef CONFIG_PM_RUNTIME
338 static ssize_t
connected_duration_show(struct device
*dev
,
339 struct device_attribute
*attr
, char *buf
)
341 struct usb_device
*udev
= to_usb_device(dev
);
343 return sprintf(buf
, "%u\n",
344 jiffies_to_msecs(jiffies
- udev
->connect_time
));
346 static DEVICE_ATTR_RO(connected_duration
);
349 * If the device is resumed, the last time the device was suspended has
350 * been pre-subtracted from active_duration. We add the current time to
351 * get the duration that the device was actually active.
353 * If the device is suspended, the active_duration is up-to-date.
355 static ssize_t
active_duration_show(struct device
*dev
,
356 struct device_attribute
*attr
, char *buf
)
358 struct usb_device
*udev
= to_usb_device(dev
);
361 if (udev
->state
!= USB_STATE_SUSPENDED
)
362 duration
= jiffies_to_msecs(jiffies
+ udev
->active_duration
);
364 duration
= jiffies_to_msecs(udev
->active_duration
);
365 return sprintf(buf
, "%u\n", duration
);
367 static DEVICE_ATTR_RO(active_duration
);
369 static ssize_t
autosuspend_show(struct device
*dev
,
370 struct device_attribute
*attr
, char *buf
)
372 return sprintf(buf
, "%d\n", dev
->power
.autosuspend_delay
/ 1000);
375 static ssize_t
autosuspend_store(struct device
*dev
,
376 struct device_attribute
*attr
, const char *buf
,
381 if (sscanf(buf
, "%d", &value
) != 1 || value
>= INT_MAX
/1000 ||
382 value
<= -INT_MAX
/1000)
385 pm_runtime_set_autosuspend_delay(dev
, value
* 1000);
388 static DEVICE_ATTR_RW(autosuspend
);
390 static const char on_string
[] = "on";
391 static const char auto_string
[] = "auto";
393 static void warn_level(void) {
394 static int level_warned
;
398 printk(KERN_WARNING
"WARNING! power/level is deprecated; "
399 "use power/control instead\n");
403 static ssize_t
level_show(struct device
*dev
, struct device_attribute
*attr
,
406 struct usb_device
*udev
= to_usb_device(dev
);
407 const char *p
= auto_string
;
410 if (udev
->state
!= USB_STATE_SUSPENDED
&& !udev
->dev
.power
.runtime_auto
)
412 return sprintf(buf
, "%s\n", p
);
415 static ssize_t
level_store(struct device
*dev
, struct device_attribute
*attr
,
416 const char *buf
, size_t count
)
418 struct usb_device
*udev
= to_usb_device(dev
);
424 cp
= memchr(buf
, '\n', count
);
428 usb_lock_device(udev
);
430 if (len
== sizeof on_string
- 1 &&
431 strncmp(buf
, on_string
, len
) == 0)
432 usb_disable_autosuspend(udev
);
434 else if (len
== sizeof auto_string
- 1 &&
435 strncmp(buf
, auto_string
, len
) == 0)
436 usb_enable_autosuspend(udev
);
441 usb_unlock_device(udev
);
444 static DEVICE_ATTR_RW(level
);
446 static ssize_t
usb2_hardware_lpm_show(struct device
*dev
,
447 struct device_attribute
*attr
, char *buf
)
449 struct usb_device
*udev
= to_usb_device(dev
);
452 if (udev
->usb2_hw_lpm_enabled
== 1)
457 return sprintf(buf
, "%s\n", p
);
460 static ssize_t
usb2_hardware_lpm_store(struct device
*dev
,
461 struct device_attribute
*attr
,
462 const char *buf
, size_t count
)
464 struct usb_device
*udev
= to_usb_device(dev
);
468 usb_lock_device(udev
);
470 ret
= strtobool(buf
, &value
);
473 ret
= usb_set_usb2_hardware_lpm(udev
, value
);
475 usb_unlock_device(udev
);
482 static DEVICE_ATTR_RW(usb2_hardware_lpm
);
484 static ssize_t
usb2_lpm_l1_timeout_show(struct device
*dev
,
485 struct device_attribute
*attr
,
488 struct usb_device
*udev
= to_usb_device(dev
);
489 return sprintf(buf
, "%d\n", udev
->l1_params
.timeout
);
492 static ssize_t
usb2_lpm_l1_timeout_store(struct device
*dev
,
493 struct device_attribute
*attr
,
494 const char *buf
, size_t count
)
496 struct usb_device
*udev
= to_usb_device(dev
);
499 if (kstrtou16(buf
, 0, &timeout
))
502 udev
->l1_params
.timeout
= timeout
;
506 static DEVICE_ATTR_RW(usb2_lpm_l1_timeout
);
508 static ssize_t
usb2_lpm_besl_show(struct device
*dev
,
509 struct device_attribute
*attr
, char *buf
)
511 struct usb_device
*udev
= to_usb_device(dev
);
512 return sprintf(buf
, "%d\n", udev
->l1_params
.besl
);
515 static ssize_t
usb2_lpm_besl_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
);
522 if (kstrtou8(buf
, 0, &besl
) || besl
> 15)
525 udev
->l1_params
.besl
= besl
;
529 static DEVICE_ATTR_RW(usb2_lpm_besl
);
531 static struct attribute
*usb2_hardware_lpm_attr
[] = {
532 &dev_attr_usb2_hardware_lpm
.attr
,
533 &dev_attr_usb2_lpm_l1_timeout
.attr
,
534 &dev_attr_usb2_lpm_besl
.attr
,
537 static struct attribute_group usb2_hardware_lpm_attr_group
= {
538 .name
= power_group_name
,
539 .attrs
= usb2_hardware_lpm_attr
,
542 static struct attribute
*power_attrs
[] = {
543 &dev_attr_autosuspend
.attr
,
544 &dev_attr_level
.attr
,
545 &dev_attr_connected_duration
.attr
,
546 &dev_attr_active_duration
.attr
,
549 static struct attribute_group power_attr_group
= {
550 .name
= power_group_name
,
551 .attrs
= power_attrs
,
554 static int add_power_attributes(struct device
*dev
)
558 if (is_usb_device(dev
)) {
559 struct usb_device
*udev
= to_usb_device(dev
);
560 rc
= sysfs_merge_group(&dev
->kobj
, &power_attr_group
);
561 if (udev
->usb2_hw_lpm_capable
== 1)
562 rc
= sysfs_merge_group(&dev
->kobj
,
563 &usb2_hardware_lpm_attr_group
);
569 static void remove_power_attributes(struct device
*dev
)
571 sysfs_unmerge_group(&dev
->kobj
, &usb2_hardware_lpm_attr_group
);
572 sysfs_unmerge_group(&dev
->kobj
, &power_attr_group
);
577 #define add_power_attributes(dev) 0
578 #define remove_power_attributes(dev) do {} while (0)
580 #endif /* CONFIG_PM_RUNTIME */
583 /* Descriptor fields */
584 #define usb_descriptor_attr_le16(field, format_string) \
586 field##_show(struct device *dev, struct device_attribute *attr, \
589 struct usb_device *udev; \
591 udev = to_usb_device(dev); \
592 return sprintf(buf, format_string, \
593 le16_to_cpu(udev->descriptor.field)); \
595 static DEVICE_ATTR_RO(field)
597 usb_descriptor_attr_le16(idVendor
, "%04x\n");
598 usb_descriptor_attr_le16(idProduct
, "%04x\n");
599 usb_descriptor_attr_le16(bcdDevice
, "%04x\n");
601 #define usb_descriptor_attr(field, format_string) \
603 field##_show(struct device *dev, struct device_attribute *attr, \
606 struct usb_device *udev; \
608 udev = to_usb_device(dev); \
609 return sprintf(buf, format_string, udev->descriptor.field); \
611 static DEVICE_ATTR_RO(field)
613 usb_descriptor_attr(bDeviceClass
, "%02x\n");
614 usb_descriptor_attr(bDeviceSubClass
, "%02x\n");
615 usb_descriptor_attr(bDeviceProtocol
, "%02x\n");
616 usb_descriptor_attr(bNumConfigurations
, "%d\n");
617 usb_descriptor_attr(bMaxPacketSize0
, "%d\n");
620 /* show if the device is authorized (1) or not (0) */
621 static ssize_t
authorized_show(struct device
*dev
,
622 struct device_attribute
*attr
, char *buf
)
624 struct usb_device
*usb_dev
= to_usb_device(dev
);
625 return snprintf(buf
, PAGE_SIZE
, "%u\n", usb_dev
->authorized
);
629 * Authorize a device to be used in the system
631 * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
633 static ssize_t
authorized_store(struct device
*dev
,
634 struct device_attribute
*attr
, const char *buf
,
638 struct usb_device
*usb_dev
= to_usb_device(dev
);
640 result
= sscanf(buf
, "%u\n", &val
);
644 result
= usb_deauthorize_device(usb_dev
);
646 result
= usb_authorize_device(usb_dev
);
647 return result
< 0? result
: size
;
649 static DEVICE_ATTR_IGNORE_LOCKDEP(authorized
, S_IRUGO
| S_IWUSR
,
650 authorized_show
, authorized_store
);
652 /* "Safely remove a device" */
653 static ssize_t
remove_store(struct device
*dev
, struct device_attribute
*attr
,
654 const char *buf
, size_t count
)
656 struct usb_device
*udev
= to_usb_device(dev
);
659 usb_lock_device(udev
);
660 if (udev
->state
!= USB_STATE_NOTATTACHED
) {
662 /* To avoid races, first unconfigure and then remove */
663 usb_set_configuration(udev
, -1);
664 rc
= usb_remove_device(udev
);
668 usb_unlock_device(udev
);
671 static DEVICE_ATTR_IGNORE_LOCKDEP(remove
, S_IWUSR
, NULL
, remove_store
);
674 static struct attribute
*dev_attrs
[] = {
675 /* current configuration's attributes */
676 &dev_attr_configuration
.attr
,
677 &dev_attr_bNumInterfaces
.attr
,
678 &dev_attr_bConfigurationValue
.attr
,
679 &dev_attr_bmAttributes
.attr
,
680 &dev_attr_bMaxPower
.attr
,
681 /* device attributes */
682 &dev_attr_urbnum
.attr
,
683 &dev_attr_idVendor
.attr
,
684 &dev_attr_idProduct
.attr
,
685 &dev_attr_bcdDevice
.attr
,
686 &dev_attr_bDeviceClass
.attr
,
687 &dev_attr_bDeviceSubClass
.attr
,
688 &dev_attr_bDeviceProtocol
.attr
,
689 &dev_attr_bNumConfigurations
.attr
,
690 &dev_attr_bMaxPacketSize0
.attr
,
691 &dev_attr_speed
.attr
,
692 &dev_attr_busnum
.attr
,
693 &dev_attr_devnum
.attr
,
694 &dev_attr_devpath
.attr
,
695 &dev_attr_version
.attr
,
696 &dev_attr_maxchild
.attr
,
697 &dev_attr_quirks
.attr
,
698 &dev_attr_avoid_reset_quirk
.attr
,
699 &dev_attr_authorized
.attr
,
700 &dev_attr_remove
.attr
,
701 &dev_attr_removable
.attr
,
702 &dev_attr_ltm_capable
.attr
,
705 static struct attribute_group dev_attr_grp
= {
709 /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
712 static struct attribute
*dev_string_attrs
[] = {
713 &dev_attr_manufacturer
.attr
,
714 &dev_attr_product
.attr
,
715 &dev_attr_serial
.attr
,
719 static umode_t
dev_string_attrs_are_visible(struct kobject
*kobj
,
720 struct attribute
*a
, int n
)
722 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
723 struct usb_device
*udev
= to_usb_device(dev
);
725 if (a
== &dev_attr_manufacturer
.attr
) {
726 if (udev
->manufacturer
== NULL
)
728 } else if (a
== &dev_attr_product
.attr
) {
729 if (udev
->product
== NULL
)
731 } else if (a
== &dev_attr_serial
.attr
) {
732 if (udev
->serial
== NULL
)
738 static struct attribute_group dev_string_attr_grp
= {
739 .attrs
= dev_string_attrs
,
740 .is_visible
= dev_string_attrs_are_visible
,
743 const struct attribute_group
*usb_device_groups
[] = {
745 &dev_string_attr_grp
,
749 /* Binary descriptors */
752 read_descriptors(struct file
*filp
, struct kobject
*kobj
,
753 struct bin_attribute
*attr
,
754 char *buf
, loff_t off
, size_t count
)
756 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
757 struct usb_device
*udev
= to_usb_device(dev
);
758 size_t nleft
= count
;
763 /* The binary attribute begins with the device descriptor.
764 * Following that are the raw descriptor entries for all the
765 * configurations (config plus subsidiary descriptors).
767 for (cfgno
= -1; cfgno
< udev
->descriptor
.bNumConfigurations
&&
768 nleft
> 0; ++cfgno
) {
770 src
= &udev
->descriptor
;
771 srclen
= sizeof(struct usb_device_descriptor
);
773 src
= udev
->rawdescriptors
[cfgno
];
774 srclen
= __le16_to_cpu(udev
->config
[cfgno
].desc
.
778 n
= min(nleft
, srclen
- (size_t) off
);
779 memcpy(buf
, src
+ off
, n
);
787 return count
- nleft
;
790 static struct bin_attribute dev_bin_attr_descriptors
= {
791 .attr
= {.name
= "descriptors", .mode
= 0444},
792 .read
= read_descriptors
,
793 .size
= 18 + 65535, /* dev descr + max-size raw descriptor */
796 int usb_create_sysfs_dev_files(struct usb_device
*udev
)
798 struct device
*dev
= &udev
->dev
;
801 retval
= device_create_bin_file(dev
, &dev_bin_attr_descriptors
);
805 retval
= add_persist_attributes(dev
);
809 retval
= add_power_attributes(dev
);
814 usb_remove_sysfs_dev_files(udev
);
818 void usb_remove_sysfs_dev_files(struct usb_device
*udev
)
820 struct device
*dev
= &udev
->dev
;
822 remove_power_attributes(dev
);
823 remove_persist_attributes(dev
);
824 device_remove_bin_file(dev
, &dev_bin_attr_descriptors
);
827 /* Interface Accociation Descriptor fields */
828 #define usb_intf_assoc_attr(field, format_string) \
830 iad_##field##_show(struct device *dev, struct device_attribute *attr, \
833 struct usb_interface *intf = to_usb_interface(dev); \
835 return sprintf(buf, format_string, \
836 intf->intf_assoc->field); \
838 static DEVICE_ATTR_RO(iad_##field)
840 usb_intf_assoc_attr(bFirstInterface
, "%02x\n");
841 usb_intf_assoc_attr(bInterfaceCount
, "%02d\n");
842 usb_intf_assoc_attr(bFunctionClass
, "%02x\n");
843 usb_intf_assoc_attr(bFunctionSubClass
, "%02x\n");
844 usb_intf_assoc_attr(bFunctionProtocol
, "%02x\n");
846 /* Interface fields */
847 #define usb_intf_attr(field, format_string) \
849 field##_show(struct device *dev, struct device_attribute *attr, \
852 struct usb_interface *intf = to_usb_interface(dev); \
854 return sprintf(buf, format_string, \
855 intf->cur_altsetting->desc.field); \
857 static DEVICE_ATTR_RO(field)
859 usb_intf_attr(bInterfaceNumber
, "%02x\n");
860 usb_intf_attr(bAlternateSetting
, "%2d\n");
861 usb_intf_attr(bNumEndpoints
, "%02x\n");
862 usb_intf_attr(bInterfaceClass
, "%02x\n");
863 usb_intf_attr(bInterfaceSubClass
, "%02x\n");
864 usb_intf_attr(bInterfaceProtocol
, "%02x\n");
866 static ssize_t
interface_show(struct device
*dev
, struct device_attribute
*attr
,
869 struct usb_interface
*intf
;
872 intf
= to_usb_interface(dev
);
873 string
= intf
->cur_altsetting
->string
;
874 barrier(); /* The altsetting might change! */
878 return sprintf(buf
, "%s\n", string
);
880 static DEVICE_ATTR_RO(interface
);
882 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
,
885 struct usb_interface
*intf
;
886 struct usb_device
*udev
;
887 struct usb_host_interface
*alt
;
889 intf
= to_usb_interface(dev
);
890 udev
= interface_to_usbdev(intf
);
891 alt
= intf
->cur_altsetting
;
893 return sprintf(buf
, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
894 "ic%02Xisc%02Xip%02Xin%02X\n",
895 le16_to_cpu(udev
->descriptor
.idVendor
),
896 le16_to_cpu(udev
->descriptor
.idProduct
),
897 le16_to_cpu(udev
->descriptor
.bcdDevice
),
898 udev
->descriptor
.bDeviceClass
,
899 udev
->descriptor
.bDeviceSubClass
,
900 udev
->descriptor
.bDeviceProtocol
,
901 alt
->desc
.bInterfaceClass
,
902 alt
->desc
.bInterfaceSubClass
,
903 alt
->desc
.bInterfaceProtocol
,
904 alt
->desc
.bInterfaceNumber
);
906 static DEVICE_ATTR_RO(modalias
);
908 static ssize_t
supports_autosuspend_show(struct device
*dev
,
909 struct device_attribute
*attr
,
912 struct usb_interface
*intf
;
913 struct usb_device
*udev
;
916 intf
= to_usb_interface(dev
);
917 udev
= interface_to_usbdev(intf
);
919 usb_lock_device(udev
);
920 /* Devices will be autosuspended even when an interface isn't claimed */
921 if (!intf
->dev
.driver
||
922 to_usb_driver(intf
->dev
.driver
)->supports_autosuspend
)
923 ret
= sprintf(buf
, "%u\n", 1);
925 ret
= sprintf(buf
, "%u\n", 0);
926 usb_unlock_device(udev
);
930 static DEVICE_ATTR_RO(supports_autosuspend
);
932 static struct attribute
*intf_attrs
[] = {
933 &dev_attr_bInterfaceNumber
.attr
,
934 &dev_attr_bAlternateSetting
.attr
,
935 &dev_attr_bNumEndpoints
.attr
,
936 &dev_attr_bInterfaceClass
.attr
,
937 &dev_attr_bInterfaceSubClass
.attr
,
938 &dev_attr_bInterfaceProtocol
.attr
,
939 &dev_attr_modalias
.attr
,
940 &dev_attr_supports_autosuspend
.attr
,
943 static struct attribute_group intf_attr_grp
= {
947 static struct attribute
*intf_assoc_attrs
[] = {
948 &dev_attr_iad_bFirstInterface
.attr
,
949 &dev_attr_iad_bInterfaceCount
.attr
,
950 &dev_attr_iad_bFunctionClass
.attr
,
951 &dev_attr_iad_bFunctionSubClass
.attr
,
952 &dev_attr_iad_bFunctionProtocol
.attr
,
956 static umode_t
intf_assoc_attrs_are_visible(struct kobject
*kobj
,
957 struct attribute
*a
, int n
)
959 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
960 struct usb_interface
*intf
= to_usb_interface(dev
);
962 if (intf
->intf_assoc
== NULL
)
967 static struct attribute_group intf_assoc_attr_grp
= {
968 .attrs
= intf_assoc_attrs
,
969 .is_visible
= intf_assoc_attrs_are_visible
,
972 const struct attribute_group
*usb_interface_groups
[] = {
974 &intf_assoc_attr_grp
,
978 void usb_create_sysfs_intf_files(struct usb_interface
*intf
)
980 struct usb_device
*udev
= interface_to_usbdev(intf
);
981 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
983 if (intf
->sysfs_files_created
|| intf
->unregistering
)
986 if (!alt
->string
&& !(udev
->quirks
& USB_QUIRK_CONFIG_INTF_STRINGS
))
987 alt
->string
= usb_cache_string(udev
, alt
->desc
.iInterface
);
988 if (alt
->string
&& device_create_file(&intf
->dev
, &dev_attr_interface
))
989 ; /* We don't actually care if the function fails. */
990 intf
->sysfs_files_created
= 1;
993 void usb_remove_sysfs_intf_files(struct usb_interface
*intf
)
995 if (!intf
->sysfs_files_created
)
998 device_remove_file(&intf
->dev
, &dev_attr_interface
);
999 intf
->sysfs_files_created
= 0;