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>
18 /* Active configuration fields */
19 #define usb_actconfig_show(field, multiplier, format_string) \
20 static ssize_t show_##field(struct device *dev, \
21 struct device_attribute *attr, char *buf) \
23 struct usb_device *udev; \
24 struct usb_host_config *actconfig; \
26 udev = to_usb_device(dev); \
27 actconfig = udev->actconfig; \
29 return sprintf(buf, format_string, \
30 actconfig->desc.field * multiplier); \
35 #define usb_actconfig_attr(field, multiplier, format_string) \
36 usb_actconfig_show(field, multiplier, format_string) \
37 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
39 usb_actconfig_attr(bNumInterfaces
, 1, "%2d\n")
40 usb_actconfig_attr(bmAttributes
, 1, "%2x\n")
41 usb_actconfig_attr(bMaxPower
, 2, "%3dmA\n")
43 static ssize_t
show_configuration_string(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
;
51 if ((!actconfig
) || (!actconfig
->string
))
53 return sprintf(buf
, "%s\n", actconfig
->string
);
55 static DEVICE_ATTR(configuration
, S_IRUGO
, show_configuration_string
, NULL
);
57 /* configuration value is always present, and r/w */
58 usb_actconfig_show(bConfigurationValue
, 1, "%u\n");
61 set_bConfigurationValue(struct device
*dev
, struct device_attribute
*attr
,
62 const char *buf
, size_t count
)
64 struct usb_device
*udev
= to_usb_device(dev
);
67 if (sscanf(buf
, "%d", &config
) != 1 || config
< -1 || config
> 255)
69 usb_lock_device(udev
);
70 value
= usb_set_configuration(udev
, config
);
71 usb_unlock_device(udev
);
72 return (value
< 0) ? value
: count
;
75 static DEVICE_ATTR(bConfigurationValue
, S_IRUGO
| S_IWUSR
,
76 show_bConfigurationValue
, set_bConfigurationValue
);
79 #define usb_string_attr(name) \
80 static ssize_t show_##name(struct device *dev, \
81 struct device_attribute *attr, char *buf) \
83 struct usb_device *udev; \
85 udev = to_usb_device(dev); \
86 return sprintf(buf, "%s\n", udev->name); \
88 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
90 usb_string_attr(product
);
91 usb_string_attr(manufacturer
);
92 usb_string_attr(serial
);
95 show_speed(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
97 struct usb_device
*udev
;
100 udev
= to_usb_device(dev
);
102 switch (udev
->speed
) {
106 case USB_SPEED_UNKNOWN
:
116 return sprintf(buf
, "%s\n", speed
);
118 static DEVICE_ATTR(speed
, S_IRUGO
, show_speed
, NULL
);
121 show_busnum(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
123 struct usb_device
*udev
;
125 udev
= to_usb_device(dev
);
126 return sprintf(buf
, "%d\n", udev
->bus
->busnum
);
128 static DEVICE_ATTR(busnum
, S_IRUGO
, show_busnum
, NULL
);
131 show_devnum(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
133 struct usb_device
*udev
;
135 udev
= to_usb_device(dev
);
136 return sprintf(buf
, "%d\n", udev
->devnum
);
138 static DEVICE_ATTR(devnum
, S_IRUGO
, show_devnum
, NULL
);
141 show_version(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
143 struct usb_device
*udev
;
146 udev
= to_usb_device(dev
);
147 bcdUSB
= le16_to_cpu(udev
->descriptor
.bcdUSB
);
148 return sprintf(buf
, "%2x.%02x\n", bcdUSB
>> 8, bcdUSB
& 0xff);
150 static DEVICE_ATTR(version
, S_IRUGO
, show_version
, NULL
);
153 show_maxchild(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
155 struct usb_device
*udev
;
157 udev
= to_usb_device(dev
);
158 return sprintf(buf
, "%d\n", udev
->maxchild
);
160 static DEVICE_ATTR(maxchild
, S_IRUGO
, show_maxchild
, NULL
);
163 show_quirks(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
165 struct usb_device
*udev
;
167 udev
= to_usb_device(dev
);
168 return sprintf(buf
, "0x%x\n", udev
->quirks
);
170 static DEVICE_ATTR(quirks
, S_IRUGO
, show_quirks
, NULL
);
173 show_urbnum(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
175 struct usb_device
*udev
;
177 udev
= to_usb_device(dev
);
178 return sprintf(buf
, "%d\n", atomic_read(&udev
->urbnum
));
180 static DEVICE_ATTR(urbnum
, S_IRUGO
, show_urbnum
, NULL
);
183 #if defined(CONFIG_USB_PERSIST) || defined(CONFIG_USB_SUSPEND)
184 static const char power_group
[] = "power";
187 #ifdef CONFIG_USB_PERSIST
190 show_persist(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
192 struct usb_device
*udev
= to_usb_device(dev
);
194 return sprintf(buf
, "%d\n", udev
->persist_enabled
);
198 set_persist(struct device
*dev
, struct device_attribute
*attr
,
199 const char *buf
, size_t count
)
201 struct usb_device
*udev
= to_usb_device(dev
);
204 /* Hubs are always enabled for USB_PERSIST */
205 if (udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
)
208 if (sscanf(buf
, "%d", &value
) != 1)
211 udev
->persist_enabled
= !!value
;
216 static DEVICE_ATTR(persist
, S_IRUGO
| S_IWUSR
, show_persist
, set_persist
);
218 static int add_persist_attributes(struct device
*dev
)
222 if (is_usb_device(dev
)) {
223 struct usb_device
*udev
= to_usb_device(dev
);
225 /* Hubs are automatically enabled for USB_PERSIST */
226 if (udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
)
227 udev
->persist_enabled
= 1;
228 rc
= sysfs_add_file_to_group(&dev
->kobj
,
229 &dev_attr_persist
.attr
,
235 static void remove_persist_attributes(struct device
*dev
)
237 sysfs_remove_file_from_group(&dev
->kobj
,
238 &dev_attr_persist
.attr
,
244 #define add_persist_attributes(dev) 0
245 #define remove_persist_attributes(dev) do {} while (0)
247 #endif /* CONFIG_USB_PERSIST */
249 #ifdef CONFIG_USB_SUSPEND
252 show_autosuspend(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
254 struct usb_device
*udev
= to_usb_device(dev
);
256 return sprintf(buf
, "%d\n", udev
->autosuspend_delay
/ HZ
);
260 set_autosuspend(struct device
*dev
, struct device_attribute
*attr
,
261 const char *buf
, size_t count
)
263 struct usb_device
*udev
= to_usb_device(dev
);
266 if (sscanf(buf
, "%d", &value
) != 1 || value
>= INT_MAX
/HZ
||
267 value
<= - INT_MAX
/HZ
)
271 udev
->autosuspend_delay
= value
;
273 usb_try_autosuspend_device(udev
);
275 if (usb_autoresume_device(udev
) == 0)
276 usb_autosuspend_device(udev
);
281 static DEVICE_ATTR(autosuspend
, S_IRUGO
| S_IWUSR
,
282 show_autosuspend
, set_autosuspend
);
284 static const char on_string
[] = "on";
285 static const char auto_string
[] = "auto";
286 static const char suspend_string
[] = "suspend";
289 show_level(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
291 struct usb_device
*udev
= to_usb_device(dev
);
292 const char *p
= auto_string
;
294 if (udev
->state
== USB_STATE_SUSPENDED
) {
295 if (udev
->autoresume_disabled
)
298 if (udev
->autosuspend_disabled
)
301 return sprintf(buf
, "%s\n", p
);
305 set_level(struct device
*dev
, struct device_attribute
*attr
,
306 const char *buf
, size_t count
)
308 struct usb_device
*udev
= to_usb_device(dev
);
312 int old_autosuspend_disabled
, old_autoresume_disabled
;
314 cp
= memchr(buf
, '\n', count
);
318 usb_lock_device(udev
);
319 old_autosuspend_disabled
= udev
->autosuspend_disabled
;
320 old_autoresume_disabled
= udev
->autoresume_disabled
;
322 /* Setting the flags without calling usb_pm_lock is a subject to
323 * races, but who cares...
325 if (len
== sizeof on_string
- 1 &&
326 strncmp(buf
, on_string
, len
) == 0) {
327 udev
->autosuspend_disabled
= 1;
328 udev
->autoresume_disabled
= 0;
329 rc
= usb_external_resume_device(udev
);
331 } else if (len
== sizeof auto_string
- 1 &&
332 strncmp(buf
, auto_string
, len
) == 0) {
333 udev
->autosuspend_disabled
= 0;
334 udev
->autoresume_disabled
= 0;
335 rc
= usb_external_resume_device(udev
);
337 } else if (len
== sizeof suspend_string
- 1 &&
338 strncmp(buf
, suspend_string
, len
) == 0) {
339 udev
->autosuspend_disabled
= 0;
340 udev
->autoresume_disabled
= 1;
341 rc
= usb_external_suspend_device(udev
, PMSG_SUSPEND
);
347 udev
->autosuspend_disabled
= old_autosuspend_disabled
;
348 udev
->autoresume_disabled
= old_autoresume_disabled
;
350 usb_unlock_device(udev
);
351 return (rc
< 0 ? rc
: count
);
354 static DEVICE_ATTR(level
, S_IRUGO
| S_IWUSR
, show_level
, set_level
);
356 static int add_power_attributes(struct device
*dev
)
360 if (is_usb_device(dev
)) {
361 rc
= sysfs_add_file_to_group(&dev
->kobj
,
362 &dev_attr_autosuspend
.attr
,
365 rc
= sysfs_add_file_to_group(&dev
->kobj
,
366 &dev_attr_level
.attr
,
372 static void remove_power_attributes(struct device
*dev
)
374 sysfs_remove_file_from_group(&dev
->kobj
,
375 &dev_attr_level
.attr
,
377 sysfs_remove_file_from_group(&dev
->kobj
,
378 &dev_attr_autosuspend
.attr
,
384 #define add_power_attributes(dev) 0
385 #define remove_power_attributes(dev) do {} while (0)
387 #endif /* CONFIG_USB_SUSPEND */
390 /* Descriptor fields */
391 #define usb_descriptor_attr_le16(field, format_string) \
393 show_##field(struct device *dev, struct device_attribute *attr, \
396 struct usb_device *udev; \
398 udev = to_usb_device(dev); \
399 return sprintf(buf, format_string, \
400 le16_to_cpu(udev->descriptor.field)); \
402 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
404 usb_descriptor_attr_le16(idVendor
, "%04x\n")
405 usb_descriptor_attr_le16(idProduct
, "%04x\n")
406 usb_descriptor_attr_le16(bcdDevice
, "%04x\n")
408 #define usb_descriptor_attr(field, format_string) \
410 show_##field(struct device *dev, struct device_attribute *attr, \
413 struct usb_device *udev; \
415 udev = to_usb_device(dev); \
416 return sprintf(buf, format_string, udev->descriptor.field); \
418 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
420 usb_descriptor_attr(bDeviceClass
, "%02x\n")
421 usb_descriptor_attr(bDeviceSubClass
, "%02x\n")
422 usb_descriptor_attr(bDeviceProtocol
, "%02x\n")
423 usb_descriptor_attr(bNumConfigurations
, "%d\n")
424 usb_descriptor_attr(bMaxPacketSize0
, "%d\n")
428 /* show if the device is authorized (1) or not (0) */
429 static ssize_t
usb_dev_authorized_show(struct device
*dev
,
430 struct device_attribute
*attr
,
433 struct usb_device
*usb_dev
= to_usb_device(dev
);
434 return snprintf(buf
, PAGE_SIZE
, "%u\n", usb_dev
->authorized
);
439 * Authorize a device to be used in the system
441 * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
443 static ssize_t
usb_dev_authorized_store(struct device
*dev
,
444 struct device_attribute
*attr
,
445 const char *buf
, size_t size
)
448 struct usb_device
*usb_dev
= to_usb_device(dev
);
450 result
= sscanf(buf
, "%u\n", &val
);
454 result
= usb_deauthorize_device(usb_dev
);
456 result
= usb_authorize_device(usb_dev
);
457 return result
< 0? result
: size
;
460 static DEVICE_ATTR(authorized
, 0644,
461 usb_dev_authorized_show
, usb_dev_authorized_store
);
464 static struct attribute
*dev_attrs
[] = {
465 /* current configuration's attributes */
466 &dev_attr_configuration
.attr
,
467 &dev_attr_bNumInterfaces
.attr
,
468 &dev_attr_bConfigurationValue
.attr
,
469 &dev_attr_bmAttributes
.attr
,
470 &dev_attr_bMaxPower
.attr
,
471 &dev_attr_urbnum
.attr
,
472 /* device attributes */
473 &dev_attr_idVendor
.attr
,
474 &dev_attr_idProduct
.attr
,
475 &dev_attr_bcdDevice
.attr
,
476 &dev_attr_bDeviceClass
.attr
,
477 &dev_attr_bDeviceSubClass
.attr
,
478 &dev_attr_bDeviceProtocol
.attr
,
479 &dev_attr_bNumConfigurations
.attr
,
480 &dev_attr_bMaxPacketSize0
.attr
,
481 &dev_attr_speed
.attr
,
482 &dev_attr_busnum
.attr
,
483 &dev_attr_devnum
.attr
,
484 &dev_attr_version
.attr
,
485 &dev_attr_maxchild
.attr
,
486 &dev_attr_quirks
.attr
,
487 &dev_attr_authorized
.attr
,
490 static struct attribute_group dev_attr_grp
= {
494 /* Binary descriptors */
497 read_descriptors(struct kobject
*kobj
, struct bin_attribute
*attr
,
498 char *buf
, loff_t off
, size_t count
)
500 struct usb_device
*udev
= to_usb_device(
501 container_of(kobj
, struct device
, kobj
));
502 size_t nleft
= count
;
505 usb_lock_device(udev
);
507 /* The binary attribute begins with the device descriptor */
508 srclen
= sizeof(struct usb_device_descriptor
);
510 n
= min_t(size_t, nleft
, srclen
- off
);
511 memcpy(buf
, off
+ (char *) &udev
->descriptor
, n
);
519 /* Then follows the raw descriptor entry for the current
520 * configuration (config plus subsidiary descriptors).
522 if (udev
->actconfig
) {
523 int cfgno
= udev
->actconfig
- udev
->config
;
525 srclen
= __le16_to_cpu(udev
->actconfig
->desc
.wTotalLength
);
527 n
= min_t(size_t, nleft
, srclen
- off
);
528 memcpy(buf
, off
+ udev
->rawdescriptors
[cfgno
], n
);
532 usb_unlock_device(udev
);
533 return count
- nleft
;
536 static struct bin_attribute dev_bin_attr_descriptors
= {
537 .attr
= {.name
= "descriptors", .mode
= 0444},
538 .read
= read_descriptors
,
539 .size
= 18 + 65535, /* dev descr + max-size raw descriptor */
542 int usb_create_sysfs_dev_files(struct usb_device
*udev
)
544 struct device
*dev
= &udev
->dev
;
547 retval
= sysfs_create_group(&dev
->kobj
, &dev_attr_grp
);
551 retval
= device_create_bin_file(dev
, &dev_bin_attr_descriptors
);
555 retval
= add_persist_attributes(dev
);
559 retval
= add_power_attributes(dev
);
563 if (udev
->manufacturer
) {
564 retval
= device_create_file(dev
, &dev_attr_manufacturer
);
569 retval
= device_create_file(dev
, &dev_attr_product
);
574 retval
= device_create_file(dev
, &dev_attr_serial
);
578 retval
= usb_create_ep_files(dev
, &udev
->ep0
, udev
);
583 usb_remove_sysfs_dev_files(udev
);
587 void usb_remove_sysfs_dev_files(struct usb_device
*udev
)
589 struct device
*dev
= &udev
->dev
;
591 usb_remove_ep_files(&udev
->ep0
);
592 device_remove_file(dev
, &dev_attr_manufacturer
);
593 device_remove_file(dev
, &dev_attr_product
);
594 device_remove_file(dev
, &dev_attr_serial
);
595 remove_power_attributes(dev
);
596 remove_persist_attributes(dev
);
597 device_remove_bin_file(dev
, &dev_bin_attr_descriptors
);
598 sysfs_remove_group(&dev
->kobj
, &dev_attr_grp
);
601 /* Interface Accociation Descriptor fields */
602 #define usb_intf_assoc_attr(field, format_string) \
604 show_iad_##field (struct device *dev, struct device_attribute *attr, \
607 struct usb_interface *intf = to_usb_interface (dev); \
609 return sprintf (buf, format_string, \
610 intf->intf_assoc->field); \
612 static DEVICE_ATTR(iad_##field, S_IRUGO, show_iad_##field, NULL);
614 usb_intf_assoc_attr (bFirstInterface
, "%02x\n")
615 usb_intf_assoc_attr (bInterfaceCount
, "%02d\n")
616 usb_intf_assoc_attr (bFunctionClass
, "%02x\n")
617 usb_intf_assoc_attr (bFunctionSubClass
, "%02x\n")
618 usb_intf_assoc_attr (bFunctionProtocol
, "%02x\n")
620 /* Interface fields */
621 #define usb_intf_attr(field, format_string) \
623 show_##field(struct device *dev, struct device_attribute *attr, \
626 struct usb_interface *intf = to_usb_interface(dev); \
628 return sprintf(buf, format_string, \
629 intf->cur_altsetting->desc.field); \
631 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
633 usb_intf_attr(bInterfaceNumber
, "%02x\n")
634 usb_intf_attr(bAlternateSetting
, "%2d\n")
635 usb_intf_attr(bNumEndpoints
, "%02x\n")
636 usb_intf_attr(bInterfaceClass
, "%02x\n")
637 usb_intf_attr(bInterfaceSubClass
, "%02x\n")
638 usb_intf_attr(bInterfaceProtocol
, "%02x\n")
640 static ssize_t
show_interface_string(struct device
*dev
,
641 struct device_attribute
*attr
, char *buf
)
643 struct usb_interface
*intf
;
644 struct usb_device
*udev
;
647 intf
= to_usb_interface(dev
);
648 udev
= interface_to_usbdev(intf
);
649 len
= snprintf(buf
, 256, "%s", intf
->cur_altsetting
->string
);
656 static DEVICE_ATTR(interface
, S_IRUGO
, show_interface_string
, NULL
);
658 static ssize_t
show_modalias(struct device
*dev
,
659 struct device_attribute
*attr
, char *buf
)
661 struct usb_interface
*intf
;
662 struct usb_device
*udev
;
663 struct usb_host_interface
*alt
;
665 intf
= to_usb_interface(dev
);
666 udev
= interface_to_usbdev(intf
);
667 alt
= intf
->cur_altsetting
;
669 return sprintf(buf
, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
670 "ic%02Xisc%02Xip%02X\n",
671 le16_to_cpu(udev
->descriptor
.idVendor
),
672 le16_to_cpu(udev
->descriptor
.idProduct
),
673 le16_to_cpu(udev
->descriptor
.bcdDevice
),
674 udev
->descriptor
.bDeviceClass
,
675 udev
->descriptor
.bDeviceSubClass
,
676 udev
->descriptor
.bDeviceProtocol
,
677 alt
->desc
.bInterfaceClass
,
678 alt
->desc
.bInterfaceSubClass
,
679 alt
->desc
.bInterfaceProtocol
);
681 static DEVICE_ATTR(modalias
, S_IRUGO
, show_modalias
, NULL
);
683 static struct attribute
*intf_assoc_attrs
[] = {
684 &dev_attr_iad_bFirstInterface
.attr
,
685 &dev_attr_iad_bInterfaceCount
.attr
,
686 &dev_attr_iad_bFunctionClass
.attr
,
687 &dev_attr_iad_bFunctionSubClass
.attr
,
688 &dev_attr_iad_bFunctionProtocol
.attr
,
691 static struct attribute_group intf_assoc_attr_grp
= {
692 .attrs
= intf_assoc_attrs
,
695 static struct attribute
*intf_attrs
[] = {
696 &dev_attr_bInterfaceNumber
.attr
,
697 &dev_attr_bAlternateSetting
.attr
,
698 &dev_attr_bNumEndpoints
.attr
,
699 &dev_attr_bInterfaceClass
.attr
,
700 &dev_attr_bInterfaceSubClass
.attr
,
701 &dev_attr_bInterfaceProtocol
.attr
,
702 &dev_attr_modalias
.attr
,
705 static struct attribute_group intf_attr_grp
= {
709 static inline void usb_create_intf_ep_files(struct usb_interface
*intf
,
710 struct usb_device
*udev
)
712 struct usb_host_interface
*iface_desc
;
715 iface_desc
= intf
->cur_altsetting
;
716 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
)
717 usb_create_ep_files(&intf
->dev
, &iface_desc
->endpoint
[i
],
721 static inline void usb_remove_intf_ep_files(struct usb_interface
*intf
)
723 struct usb_host_interface
*iface_desc
;
726 iface_desc
= intf
->cur_altsetting
;
727 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
)
728 usb_remove_ep_files(&iface_desc
->endpoint
[i
]);
731 int usb_create_sysfs_intf_files(struct usb_interface
*intf
)
733 struct device
*dev
= &intf
->dev
;
734 struct usb_device
*udev
= interface_to_usbdev(intf
);
735 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
738 if (intf
->sysfs_files_created
)
740 retval
= sysfs_create_group(&dev
->kobj
, &intf_attr_grp
);
744 if (alt
->string
== NULL
)
745 alt
->string
= usb_cache_string(udev
, alt
->desc
.iInterface
);
747 retval
= device_create_file(dev
, &dev_attr_interface
);
748 if (intf
->intf_assoc
)
749 retval
= sysfs_create_group(&dev
->kobj
, &intf_assoc_attr_grp
);
750 usb_create_intf_ep_files(intf
, udev
);
751 intf
->sysfs_files_created
= 1;
755 void usb_remove_sysfs_intf_files(struct usb_interface
*intf
)
757 struct device
*dev
= &intf
->dev
;
759 if (!intf
->sysfs_files_created
)
761 usb_remove_intf_ep_files(intf
);
762 device_remove_file(dev
, &dev_attr_interface
);
763 sysfs_remove_group(&dev
->kobj
, &intf_attr_grp
);
764 sysfs_remove_group(&intf
->dev
.kobj
, &intf_assoc_attr_grp
);
765 intf
->sysfs_files_created
= 0;