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_allowed
== 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 udev
->usb2_hw_lpm_allowed
= value
;
474 ret
= usb_set_usb2_hardware_lpm(udev
, value
);
477 usb_unlock_device(udev
);
484 static DEVICE_ATTR_RW(usb2_hardware_lpm
);
486 static ssize_t
usb2_lpm_l1_timeout_show(struct device
*dev
,
487 struct device_attribute
*attr
,
490 struct usb_device
*udev
= to_usb_device(dev
);
491 return sprintf(buf
, "%d\n", udev
->l1_params
.timeout
);
494 static ssize_t
usb2_lpm_l1_timeout_store(struct device
*dev
,
495 struct device_attribute
*attr
,
496 const char *buf
, size_t count
)
498 struct usb_device
*udev
= to_usb_device(dev
);
501 if (kstrtou16(buf
, 0, &timeout
))
504 udev
->l1_params
.timeout
= timeout
;
508 static DEVICE_ATTR_RW(usb2_lpm_l1_timeout
);
510 static ssize_t
usb2_lpm_besl_show(struct device
*dev
,
511 struct device_attribute
*attr
, char *buf
)
513 struct usb_device
*udev
= to_usb_device(dev
);
514 return sprintf(buf
, "%d\n", udev
->l1_params
.besl
);
517 static ssize_t
usb2_lpm_besl_store(struct device
*dev
,
518 struct device_attribute
*attr
,
519 const char *buf
, size_t count
)
521 struct usb_device
*udev
= to_usb_device(dev
);
524 if (kstrtou8(buf
, 0, &besl
) || besl
> 15)
527 udev
->l1_params
.besl
= besl
;
531 static DEVICE_ATTR_RW(usb2_lpm_besl
);
533 static struct attribute
*usb2_hardware_lpm_attr
[] = {
534 &dev_attr_usb2_hardware_lpm
.attr
,
535 &dev_attr_usb2_lpm_l1_timeout
.attr
,
536 &dev_attr_usb2_lpm_besl
.attr
,
539 static struct attribute_group usb2_hardware_lpm_attr_group
= {
540 .name
= power_group_name
,
541 .attrs
= usb2_hardware_lpm_attr
,
544 static struct attribute
*power_attrs
[] = {
545 &dev_attr_autosuspend
.attr
,
546 &dev_attr_level
.attr
,
547 &dev_attr_connected_duration
.attr
,
548 &dev_attr_active_duration
.attr
,
551 static struct attribute_group power_attr_group
= {
552 .name
= power_group_name
,
553 .attrs
= power_attrs
,
556 static int add_power_attributes(struct device
*dev
)
560 if (is_usb_device(dev
)) {
561 struct usb_device
*udev
= to_usb_device(dev
);
562 rc
= sysfs_merge_group(&dev
->kobj
, &power_attr_group
);
563 if (udev
->usb2_hw_lpm_capable
== 1)
564 rc
= sysfs_merge_group(&dev
->kobj
,
565 &usb2_hardware_lpm_attr_group
);
571 static void remove_power_attributes(struct device
*dev
)
573 sysfs_unmerge_group(&dev
->kobj
, &usb2_hardware_lpm_attr_group
);
574 sysfs_unmerge_group(&dev
->kobj
, &power_attr_group
);
579 #define add_power_attributes(dev) 0
580 #define remove_power_attributes(dev) do {} while (0)
582 #endif /* CONFIG_PM_RUNTIME */
585 /* Descriptor fields */
586 #define usb_descriptor_attr_le16(field, format_string) \
588 field##_show(struct device *dev, struct device_attribute *attr, \
591 struct usb_device *udev; \
593 udev = to_usb_device(dev); \
594 return sprintf(buf, format_string, \
595 le16_to_cpu(udev->descriptor.field)); \
597 static DEVICE_ATTR_RO(field)
599 usb_descriptor_attr_le16(idVendor
, "%04x\n");
600 usb_descriptor_attr_le16(idProduct
, "%04x\n");
601 usb_descriptor_attr_le16(bcdDevice
, "%04x\n");
603 #define usb_descriptor_attr(field, format_string) \
605 field##_show(struct device *dev, struct device_attribute *attr, \
608 struct usb_device *udev; \
610 udev = to_usb_device(dev); \
611 return sprintf(buf, format_string, udev->descriptor.field); \
613 static DEVICE_ATTR_RO(field)
615 usb_descriptor_attr(bDeviceClass
, "%02x\n");
616 usb_descriptor_attr(bDeviceSubClass
, "%02x\n");
617 usb_descriptor_attr(bDeviceProtocol
, "%02x\n");
618 usb_descriptor_attr(bNumConfigurations
, "%d\n");
619 usb_descriptor_attr(bMaxPacketSize0
, "%d\n");
622 /* show if the device is authorized (1) or not (0) */
623 static ssize_t
authorized_show(struct device
*dev
,
624 struct device_attribute
*attr
, char *buf
)
626 struct usb_device
*usb_dev
= to_usb_device(dev
);
627 return snprintf(buf
, PAGE_SIZE
, "%u\n", usb_dev
->authorized
);
631 * Authorize a device to be used in the system
633 * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
635 static ssize_t
authorized_store(struct device
*dev
,
636 struct device_attribute
*attr
, const char *buf
,
640 struct usb_device
*usb_dev
= to_usb_device(dev
);
642 result
= sscanf(buf
, "%u\n", &val
);
646 result
= usb_deauthorize_device(usb_dev
);
648 result
= usb_authorize_device(usb_dev
);
649 return result
< 0? result
: size
;
651 static DEVICE_ATTR_IGNORE_LOCKDEP(authorized
, S_IRUGO
| S_IWUSR
,
652 authorized_show
, authorized_store
);
654 /* "Safely remove a device" */
655 static ssize_t
remove_store(struct device
*dev
, struct device_attribute
*attr
,
656 const char *buf
, size_t count
)
658 struct usb_device
*udev
= to_usb_device(dev
);
661 usb_lock_device(udev
);
662 if (udev
->state
!= USB_STATE_NOTATTACHED
) {
664 /* To avoid races, first unconfigure and then remove */
665 usb_set_configuration(udev
, -1);
666 rc
= usb_remove_device(udev
);
670 usb_unlock_device(udev
);
673 static DEVICE_ATTR_IGNORE_LOCKDEP(remove
, S_IWUSR
, NULL
, remove_store
);
676 static struct attribute
*dev_attrs
[] = {
677 /* current configuration's attributes */
678 &dev_attr_configuration
.attr
,
679 &dev_attr_bNumInterfaces
.attr
,
680 &dev_attr_bConfigurationValue
.attr
,
681 &dev_attr_bmAttributes
.attr
,
682 &dev_attr_bMaxPower
.attr
,
683 /* device attributes */
684 &dev_attr_urbnum
.attr
,
685 &dev_attr_idVendor
.attr
,
686 &dev_attr_idProduct
.attr
,
687 &dev_attr_bcdDevice
.attr
,
688 &dev_attr_bDeviceClass
.attr
,
689 &dev_attr_bDeviceSubClass
.attr
,
690 &dev_attr_bDeviceProtocol
.attr
,
691 &dev_attr_bNumConfigurations
.attr
,
692 &dev_attr_bMaxPacketSize0
.attr
,
693 &dev_attr_speed
.attr
,
694 &dev_attr_busnum
.attr
,
695 &dev_attr_devnum
.attr
,
696 &dev_attr_devpath
.attr
,
697 &dev_attr_version
.attr
,
698 &dev_attr_maxchild
.attr
,
699 &dev_attr_quirks
.attr
,
700 &dev_attr_avoid_reset_quirk
.attr
,
701 &dev_attr_authorized
.attr
,
702 &dev_attr_remove
.attr
,
703 &dev_attr_removable
.attr
,
704 &dev_attr_ltm_capable
.attr
,
707 static struct attribute_group dev_attr_grp
= {
711 /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
714 static struct attribute
*dev_string_attrs
[] = {
715 &dev_attr_manufacturer
.attr
,
716 &dev_attr_product
.attr
,
717 &dev_attr_serial
.attr
,
721 static umode_t
dev_string_attrs_are_visible(struct kobject
*kobj
,
722 struct attribute
*a
, int n
)
724 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
725 struct usb_device
*udev
= to_usb_device(dev
);
727 if (a
== &dev_attr_manufacturer
.attr
) {
728 if (udev
->manufacturer
== NULL
)
730 } else if (a
== &dev_attr_product
.attr
) {
731 if (udev
->product
== NULL
)
733 } else if (a
== &dev_attr_serial
.attr
) {
734 if (udev
->serial
== NULL
)
740 static struct attribute_group dev_string_attr_grp
= {
741 .attrs
= dev_string_attrs
,
742 .is_visible
= dev_string_attrs_are_visible
,
745 const struct attribute_group
*usb_device_groups
[] = {
747 &dev_string_attr_grp
,
751 /* Binary descriptors */
754 read_descriptors(struct file
*filp
, struct kobject
*kobj
,
755 struct bin_attribute
*attr
,
756 char *buf
, loff_t off
, size_t count
)
758 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
759 struct usb_device
*udev
= to_usb_device(dev
);
760 size_t nleft
= count
;
765 /* The binary attribute begins with the device descriptor.
766 * Following that are the raw descriptor entries for all the
767 * configurations (config plus subsidiary descriptors).
769 for (cfgno
= -1; cfgno
< udev
->descriptor
.bNumConfigurations
&&
770 nleft
> 0; ++cfgno
) {
772 src
= &udev
->descriptor
;
773 srclen
= sizeof(struct usb_device_descriptor
);
775 src
= udev
->rawdescriptors
[cfgno
];
776 srclen
= __le16_to_cpu(udev
->config
[cfgno
].desc
.
780 n
= min(nleft
, srclen
- (size_t) off
);
781 memcpy(buf
, src
+ off
, n
);
789 return count
- nleft
;
792 static struct bin_attribute dev_bin_attr_descriptors
= {
793 .attr
= {.name
= "descriptors", .mode
= 0444},
794 .read
= read_descriptors
,
795 .size
= 18 + 65535, /* dev descr + max-size raw descriptor */
798 int usb_create_sysfs_dev_files(struct usb_device
*udev
)
800 struct device
*dev
= &udev
->dev
;
803 retval
= device_create_bin_file(dev
, &dev_bin_attr_descriptors
);
807 retval
= add_persist_attributes(dev
);
811 retval
= add_power_attributes(dev
);
816 usb_remove_sysfs_dev_files(udev
);
820 void usb_remove_sysfs_dev_files(struct usb_device
*udev
)
822 struct device
*dev
= &udev
->dev
;
824 remove_power_attributes(dev
);
825 remove_persist_attributes(dev
);
826 device_remove_bin_file(dev
, &dev_bin_attr_descriptors
);
829 /* Interface Accociation Descriptor fields */
830 #define usb_intf_assoc_attr(field, format_string) \
832 iad_##field##_show(struct device *dev, struct device_attribute *attr, \
835 struct usb_interface *intf = to_usb_interface(dev); \
837 return sprintf(buf, format_string, \
838 intf->intf_assoc->field); \
840 static DEVICE_ATTR_RO(iad_##field)
842 usb_intf_assoc_attr(bFirstInterface
, "%02x\n");
843 usb_intf_assoc_attr(bInterfaceCount
, "%02d\n");
844 usb_intf_assoc_attr(bFunctionClass
, "%02x\n");
845 usb_intf_assoc_attr(bFunctionSubClass
, "%02x\n");
846 usb_intf_assoc_attr(bFunctionProtocol
, "%02x\n");
848 /* Interface fields */
849 #define usb_intf_attr(field, format_string) \
851 field##_show(struct device *dev, struct device_attribute *attr, \
854 struct usb_interface *intf = to_usb_interface(dev); \
856 return sprintf(buf, format_string, \
857 intf->cur_altsetting->desc.field); \
859 static DEVICE_ATTR_RO(field)
861 usb_intf_attr(bInterfaceNumber
, "%02x\n");
862 usb_intf_attr(bAlternateSetting
, "%2d\n");
863 usb_intf_attr(bNumEndpoints
, "%02x\n");
864 usb_intf_attr(bInterfaceClass
, "%02x\n");
865 usb_intf_attr(bInterfaceSubClass
, "%02x\n");
866 usb_intf_attr(bInterfaceProtocol
, "%02x\n");
868 static ssize_t
interface_show(struct device
*dev
, struct device_attribute
*attr
,
871 struct usb_interface
*intf
;
874 intf
= to_usb_interface(dev
);
875 string
= intf
->cur_altsetting
->string
;
876 barrier(); /* The altsetting might change! */
880 return sprintf(buf
, "%s\n", string
);
882 static DEVICE_ATTR_RO(interface
);
884 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
,
887 struct usb_interface
*intf
;
888 struct usb_device
*udev
;
889 struct usb_host_interface
*alt
;
891 intf
= to_usb_interface(dev
);
892 udev
= interface_to_usbdev(intf
);
893 alt
= intf
->cur_altsetting
;
895 return sprintf(buf
, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
896 "ic%02Xisc%02Xip%02Xin%02X\n",
897 le16_to_cpu(udev
->descriptor
.idVendor
),
898 le16_to_cpu(udev
->descriptor
.idProduct
),
899 le16_to_cpu(udev
->descriptor
.bcdDevice
),
900 udev
->descriptor
.bDeviceClass
,
901 udev
->descriptor
.bDeviceSubClass
,
902 udev
->descriptor
.bDeviceProtocol
,
903 alt
->desc
.bInterfaceClass
,
904 alt
->desc
.bInterfaceSubClass
,
905 alt
->desc
.bInterfaceProtocol
,
906 alt
->desc
.bInterfaceNumber
);
908 static DEVICE_ATTR_RO(modalias
);
910 static ssize_t
supports_autosuspend_show(struct device
*dev
,
911 struct device_attribute
*attr
,
914 struct usb_interface
*intf
;
915 struct usb_device
*udev
;
918 intf
= to_usb_interface(dev
);
919 udev
= interface_to_usbdev(intf
);
921 usb_lock_device(udev
);
922 /* Devices will be autosuspended even when an interface isn't claimed */
923 if (!intf
->dev
.driver
||
924 to_usb_driver(intf
->dev
.driver
)->supports_autosuspend
)
925 ret
= sprintf(buf
, "%u\n", 1);
927 ret
= sprintf(buf
, "%u\n", 0);
928 usb_unlock_device(udev
);
932 static DEVICE_ATTR_RO(supports_autosuspend
);
934 static struct attribute
*intf_attrs
[] = {
935 &dev_attr_bInterfaceNumber
.attr
,
936 &dev_attr_bAlternateSetting
.attr
,
937 &dev_attr_bNumEndpoints
.attr
,
938 &dev_attr_bInterfaceClass
.attr
,
939 &dev_attr_bInterfaceSubClass
.attr
,
940 &dev_attr_bInterfaceProtocol
.attr
,
941 &dev_attr_modalias
.attr
,
942 &dev_attr_supports_autosuspend
.attr
,
945 static struct attribute_group intf_attr_grp
= {
949 static struct attribute
*intf_assoc_attrs
[] = {
950 &dev_attr_iad_bFirstInterface
.attr
,
951 &dev_attr_iad_bInterfaceCount
.attr
,
952 &dev_attr_iad_bFunctionClass
.attr
,
953 &dev_attr_iad_bFunctionSubClass
.attr
,
954 &dev_attr_iad_bFunctionProtocol
.attr
,
958 static umode_t
intf_assoc_attrs_are_visible(struct kobject
*kobj
,
959 struct attribute
*a
, int n
)
961 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
962 struct usb_interface
*intf
= to_usb_interface(dev
);
964 if (intf
->intf_assoc
== NULL
)
969 static struct attribute_group intf_assoc_attr_grp
= {
970 .attrs
= intf_assoc_attrs
,
971 .is_visible
= intf_assoc_attrs_are_visible
,
974 const struct attribute_group
*usb_interface_groups
[] = {
976 &intf_assoc_attr_grp
,
980 void usb_create_sysfs_intf_files(struct usb_interface
*intf
)
982 struct usb_device
*udev
= interface_to_usbdev(intf
);
983 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
985 if (intf
->sysfs_files_created
|| intf
->unregistering
)
988 if (!alt
->string
&& !(udev
->quirks
& USB_QUIRK_CONFIG_INTF_STRINGS
))
989 alt
->string
= usb_cache_string(udev
, alt
->desc
.iInterface
);
990 if (alt
->string
&& device_create_file(&intf
->dev
, &dev_attr_interface
))
991 ; /* We don't actually care if the function fails. */
992 intf
->sysfs_files_created
= 1;
995 void usb_remove_sysfs_intf_files(struct usb_interface
*intf
)
997 if (!intf
->sysfs_files_created
)
1000 device_remove_file(&intf
->dev
, &dev_attr_interface
);
1001 intf
->sysfs_files_created
= 0;