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 #if defined(CONFIG_USB_PERSIST) || defined(CONFIG_USB_SUSPEND)
174 static const char power_group[] = "power";
177 #ifdef CONFIG_USB_PERSIST
180 show_persist(struct device *dev, struct device_attribute *attr, char *buf)
182 struct usb_device *udev = to_usb_device(dev);
184 return sprintf(buf, "%d\n", udev->persist_enabled);
188 set_persist(struct device *dev, struct device_attribute *attr,
189 const char *buf, size_t count)
191 struct usb_device *udev = to_usb_device(dev);
194 /* Hubs are always enabled for USB_PERSIST */
195 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
198 if (sscanf(buf, "%d", &value) != 1)
201 udev->persist_enabled = !!value;
206 static DEVICE_ATTR(persist, S_IRUGO | S_IWUSR, show_persist, set_persist);
208 static int add_persist_attributes(struct device *dev)
212 if (is_usb_device(dev)) {
213 struct usb_device *udev = to_usb_device(dev);
215 /* Hubs are automatically enabled for USB_PERSIST */
216 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
217 udev->persist_enabled = 1;
218 rc = sysfs_add_file_to_group(&dev->kobj,
219 &dev_attr_persist.attr,
225 static void remove_persist_attributes(struct device *dev)
227 sysfs_remove_file_from_group(&dev->kobj,
228 &dev_attr_persist.attr,
234 #define add_persist_attributes(dev) 0
235 #define remove_persist_attributes(dev) do {} while (0)
237 #endif /* CONFIG_USB_PERSIST */
239 #ifdef CONFIG_USB_SUSPEND
242 show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf)
244 struct usb_device *udev = to_usb_device(dev);
246 return sprintf(buf, "%d\n", udev->autosuspend_delay / HZ);
250 set_autosuspend(struct device *dev, struct device_attribute *attr,
251 const char *buf, size_t count)
253 struct usb_device *udev = to_usb_device(dev);
256 if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ ||
257 value <= - INT_MAX/HZ)
261 udev->autosuspend_delay = value;
263 usb_try_autosuspend_device(udev);
265 if (usb_autoresume_device(udev) == 0)
266 usb_autosuspend_device(udev);
271 static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
272 show_autosuspend, set_autosuspend);
274 static const char on_string[] = "on";
275 static const char auto_string[] = "auto";
276 static const char suspend_string[] = "suspend";
279 show_level(struct device *dev, struct device_attribute *attr, char *buf)
281 struct usb_device *udev = to_usb_device(dev);
282 const char *p = auto_string;
284 if (udev->state == USB_STATE_SUSPENDED) {
285 if (udev->autoresume_disabled)
288 if (udev->autosuspend_disabled)
291 return sprintf(buf, "%s\n", p);
295 set_level(struct device *dev, struct device_attribute *attr,
296 const char *buf, size_t count)
298 struct usb_device *udev = to_usb_device(dev);
302 int old_autosuspend_disabled, old_autoresume_disabled;
304 cp = memchr(buf, '\n', count);
308 usb_lock_device(udev);
309 old_autosuspend_disabled = udev->autosuspend_disabled;
310 old_autoresume_disabled = udev->autoresume_disabled;
312 /* Setting the flags without calling usb_pm_lock is a subject to
313 * races, but who cares...
315 if (len == sizeof on_string - 1 &&
316 strncmp(buf, on_string, len) == 0) {
317 udev->autosuspend_disabled = 1;
318 udev->autoresume_disabled = 0;
319 rc = usb_external_resume_device(udev);
321 } else if (len == sizeof auto_string - 1 &&
322 strncmp(buf, auto_string, len) == 0) {
323 udev->autosuspend_disabled = 0;
324 udev->autoresume_disabled = 0;
325 rc = usb_external_resume_device(udev);
327 } else if (len == sizeof suspend_string - 1 &&
328 strncmp(buf, suspend_string, len) == 0) {
329 udev->autosuspend_disabled = 0;
330 udev->autoresume_disabled = 1;
331 rc = usb_external_suspend_device(udev, PMSG_SUSPEND);
337 udev->autosuspend_disabled = old_autosuspend_disabled;
338 udev->autoresume_disabled = old_autoresume_disabled;
340 usb_unlock_device(udev);
341 return (rc < 0 ? rc : count);
344 static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level);
346 static int add_power_attributes(struct device *dev)
350 if (is_usb_device(dev)) {
351 rc = sysfs_add_file_to_group(&dev->kobj,
352 &dev_attr_autosuspend.attr,
355 rc = sysfs_add_file_to_group(&dev->kobj,
356 &dev_attr_level.attr,
362 static void remove_power_attributes(struct device *dev)
364 sysfs_remove_file_from_group(&dev->kobj,
365 &dev_attr_level.attr,
367 sysfs_remove_file_from_group(&dev->kobj,
368 &dev_attr_autosuspend.attr,
374 #define add_power_attributes(dev) 0
375 #define remove_power_attributes(dev) do {} while (0)
377 #endif /* CONFIG_USB_SUSPEND */
380 /* Descriptor fields */
381 #define usb_descriptor_attr_le16(field, format_string) \
383 show_##field(struct device *dev, struct device_attribute *attr, \
386 struct usb_device *udev; \
388 udev = to_usb_device(dev); \
389 return sprintf(buf, format_string, \
390 le16_to_cpu(udev->descriptor.field)); \
392 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
394 usb_descriptor_attr_le16(idVendor, "%04x\n")
395 usb_descriptor_attr_le16(idProduct, "%04x\n")
396 usb_descriptor_attr_le16(bcdDevice, "%04x\n")
398 #define usb_descriptor_attr(field, format_string) \
400 show_##field(struct device *dev, struct device_attribute *attr, \
403 struct usb_device *udev; \
405 udev = to_usb_device(dev); \
406 return sprintf(buf, format_string, udev->descriptor.field); \
408 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
410 usb_descriptor_attr(bDeviceClass, "%02x\n")
411 usb_descriptor_attr(bDeviceSubClass, "%02x\n")
412 usb_descriptor_attr(bDeviceProtocol, "%02x\n")
413 usb_descriptor_attr(bNumConfigurations, "%d\n")
414 usb_descriptor_attr(bMaxPacketSize0, "%d\n")
416 static struct attribute *dev_attrs[] = {
417 /* current configuration's attributes */
418 &dev_attr_configuration.attr,
419 &dev_attr_bNumInterfaces.attr,
420 &dev_attr_bConfigurationValue.attr,
421 &dev_attr_bmAttributes.attr,
422 &dev_attr_bMaxPower.attr,
423 /* device attributes */
424 &dev_attr_idVendor.attr,
425 &dev_attr_idProduct.attr,
426 &dev_attr_bcdDevice.attr,
427 &dev_attr_bDeviceClass.attr,
428 &dev_attr_bDeviceSubClass.attr,
429 &dev_attr_bDeviceProtocol.attr,
430 &dev_attr_bNumConfigurations.attr,
431 &dev_attr_bMaxPacketSize0.attr,
432 &dev_attr_speed.attr,
433 &dev_attr_busnum.attr,
434 &dev_attr_devnum.attr,
435 &dev_attr_version.attr,
436 &dev_attr_maxchild.attr,
437 &dev_attr_quirks.attr,
440 static struct attribute_group dev_attr_grp = {
444 /* Binary descriptors */
447 read_descriptors(struct kobject *kobj, struct bin_attribute *attr,
448 char *buf, loff_t off, size_t count)
450 struct usb_device *udev = to_usb_device(
451 container_of(kobj, struct device, kobj));
452 size_t nleft = count;
455 usb_lock_device(udev);
457 /* The binary attribute begins with the device descriptor */
458 srclen = sizeof(struct usb_device_descriptor);
460 n = min_t(size_t, nleft, srclen - off);
461 memcpy(buf, off + (char *) &udev->descriptor, n);
469 /* Then follows the raw descriptor entry for the current
470 * configuration (config plus subsidiary descriptors).
472 if (udev->actconfig) {
473 int cfgno = udev->actconfig - udev->config;
475 srclen = __le16_to_cpu(udev->actconfig->desc.wTotalLength);
477 n = min_t(size_t, nleft, srclen - off);
478 memcpy(buf, off + udev->rawdescriptors[cfgno], n);
482 usb_unlock_device(udev);
483 return count - nleft;
486 static struct bin_attribute dev_bin_attr_descriptors = {
487 .attr = {.name = "descriptors", .mode = 0444},
488 .read = read_descriptors,
489 .size = 18 + 65535, /* dev descr + max-size raw descriptor */
492 int usb_create_sysfs_dev_files(struct usb_device *udev)
494 struct device *dev = &udev->dev;
497 retval = sysfs_create_group(&dev->kobj, &dev_attr_grp);
501 retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
505 retval = add_persist_attributes(dev);
509 retval = add_power_attributes(dev);
513 if (udev->manufacturer) {
514 retval = device_create_file(dev, &dev_attr_manufacturer);
519 retval = device_create_file(dev, &dev_attr_product);
524 retval = device_create_file(dev, &dev_attr_serial);
528 retval = usb_create_ep_files(dev, &udev->ep0, udev);
533 usb_remove_sysfs_dev_files(udev);
537 void usb_remove_sysfs_dev_files(struct usb_device *udev)
539 struct device *dev = &udev->dev;
541 usb_remove_ep_files(&udev->ep0);
542 device_remove_file(dev, &dev_attr_manufacturer);
543 device_remove_file(dev, &dev_attr_product);
544 device_remove_file(dev, &dev_attr_serial);
545 remove_power_attributes(dev);
546 remove_persist_attributes(dev);
547 device_remove_bin_file(dev, &dev_bin_attr_descriptors);
548 sysfs_remove_group(&dev->kobj, &dev_attr_grp);
551 /* Interface Accociation Descriptor fields */
552 #define usb_intf_assoc_attr(field, format_string) \
554 show_iad_##field (struct device *dev, struct device_attribute *attr, \
557 struct usb_interface *intf = to_usb_interface (dev); \
559 return sprintf (buf, format_string, \
560 intf->intf_assoc->field); \
562 static DEVICE_ATTR(iad_##field, S_IRUGO, show_iad_##field, NULL);
564 usb_intf_assoc_attr (bFirstInterface, "%02x\n")
565 usb_intf_assoc_attr (bInterfaceCount, "%02d\n")
566 usb_intf_assoc_attr (bFunctionClass, "%02x\n")
567 usb_intf_assoc_attr (bFunctionSubClass, "%02x\n")
568 usb_intf_assoc_attr (bFunctionProtocol, "%02x\n")
570 /* Interface fields */
571 #define usb_intf_attr(field, format_string) \
573 show_##field(struct device *dev, struct device_attribute *attr, \
576 struct usb_interface *intf = to_usb_interface(dev); \
578 return sprintf(buf, format_string, \
579 intf->cur_altsetting->desc.field); \
581 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
583 usb_intf_attr(bInterfaceNumber, "%02x\n")
584 usb_intf_attr(bAlternateSetting, "%2d\n")
585 usb_intf_attr(bNumEndpoints, "%02x\n")
586 usb_intf_attr(bInterfaceClass, "%02x\n")
587 usb_intf_attr(bInterfaceSubClass, "%02x\n")
588 usb_intf_attr(bInterfaceProtocol, "%02x\n")
590 static ssize_t show_interface_string(struct device *dev,
591 struct device_attribute *attr, char *buf)
593 struct usb_interface *intf;
594 struct usb_device *udev;
597 intf = to_usb_interface(dev);
598 udev = interface_to_usbdev(intf);
599 len = snprintf(buf, 256, "%s", intf->cur_altsetting->string);
606 static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
608 static ssize_t show_modalias(struct device *dev,
609 struct device_attribute *attr, char *buf)
611 struct usb_interface *intf;
612 struct usb_device *udev;
613 struct usb_host_interface *alt;
615 intf = to_usb_interface(dev);
616 udev = interface_to_usbdev(intf);
617 alt = intf->cur_altsetting;
619 return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
620 "ic%02Xisc%02Xip%02X\n",
621 le16_to_cpu(udev->descriptor.idVendor),
622 le16_to_cpu(udev->descriptor.idProduct),
623 le16_to_cpu(udev->descriptor.bcdDevice),
624 udev->descriptor.bDeviceClass,
625 udev->descriptor.bDeviceSubClass,
626 udev->descriptor.bDeviceProtocol,
627 alt->desc.bInterfaceClass,
628 alt->desc.bInterfaceSubClass,
629 alt->desc.bInterfaceProtocol);
631 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
633 static struct attribute *intf_assoc_attrs[] = {
634 &dev_attr_iad_bFirstInterface.attr,
635 &dev_attr_iad_bInterfaceCount.attr,
636 &dev_attr_iad_bFunctionClass.attr,
637 &dev_attr_iad_bFunctionSubClass.attr,
638 &dev_attr_iad_bFunctionProtocol.attr,
641 static struct attribute_group intf_assoc_attr_grp = {
642 .attrs = intf_assoc_attrs,
645 static struct attribute *intf_attrs[] = {
646 &dev_attr_bInterfaceNumber.attr,
647 &dev_attr_bAlternateSetting.attr,
648 &dev_attr_bNumEndpoints.attr,
649 &dev_attr_bInterfaceClass.attr,
650 &dev_attr_bInterfaceSubClass.attr,
651 &dev_attr_bInterfaceProtocol.attr,
652 &dev_attr_modalias.attr,
655 static struct attribute_group intf_attr_grp = {
659 static inline void usb_create_intf_ep_files(struct usb_interface *intf,
660 struct usb_device *udev)
662 struct usb_host_interface *iface_desc;
665 iface_desc = intf->cur_altsetting;
666 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
667 usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i],
671 static inline void usb_remove_intf_ep_files(struct usb_interface *intf)
673 struct usb_host_interface *iface_desc;
676 iface_desc = intf->cur_altsetting;
677 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
678 usb_remove_ep_files(&iface_desc->endpoint[i]);
681 int usb_create_sysfs_intf_files(struct usb_interface *intf)
683 struct device *dev = &intf->dev;
684 struct usb_device *udev = interface_to_usbdev(intf);
685 struct usb_host_interface *alt = intf->cur_altsetting;
688 retval = sysfs_create_group(&dev->kobj, &intf_attr_grp);
692 if (alt->string == NULL)
693 alt->string = usb_cache_string(udev, alt->desc.iInterface);
695 retval = device_create_file(dev, &dev_attr_interface);
696 if (intf->intf_assoc)
697 retval = sysfs_create_group(&dev->kobj, &intf_assoc_attr_grp);
698 usb_create_intf_ep_files(intf, udev);
702 void usb_remove_sysfs_intf_files(struct usb_interface *intf)
704 struct device *dev = &intf->dev;
706 usb_remove_intf_ep_files(intf);
707 device_remove_file(dev, &dev_attr_interface);
708 sysfs_remove_group(&dev->kobj, &intf_attr_grp);
709 sysfs_remove_group(&intf->dev.kobj, &intf_assoc_attr_grp);