13 #include <linux/kernel.h>
14 #include <linux/string.h>
20 #define usb_actconfig_show(field, multiplier, format_string) \
21 static ssize_t show_##field(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 * multiplier); \
36 #define usb_actconfig_attr(field, multiplier, format_string) \
37 usb_actconfig_show(field, multiplier, format_string) \
38 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
47 struct usb_device *
udev;
48 struct usb_host_config *actconfig;
50 udev = to_usb_device(dev);
51 actconfig = udev->actconfig;
52 if ((!actconfig) || (!actconfig->string))
54 return sprintf(buf,
"%s\n", actconfig->string);
65 struct usb_device *
udev = to_usb_device(dev);
70 usb_lock_device(udev);
72 usb_unlock_device(udev);
73 return (value < 0) ? value :
count;
77 show_bConfigurationValue, set_bConfigurationValue);
80 #define usb_string_attr(name) \
81 static ssize_t show_##name(struct device *dev, \
82 struct device_attribute *attr, char *buf) \
84 struct usb_device *udev; \
87 udev = to_usb_device(dev); \
88 usb_lock_device(udev); \
89 retval = sprintf(buf, "%s\n", udev->name); \
90 usb_unlock_device(udev); \
93 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
102 struct usb_device *
udev;
105 udev = to_usb_device(dev);
107 switch (udev->speed) {
127 return sprintf(buf,
"%s\n", speed);
134 struct usb_device *
udev;
136 udev = to_usb_device(dev);
137 return sprintf(buf,
"%d\n", udev->bus->busnum);
144 struct usb_device *
udev;
146 udev = to_usb_device(dev);
147 return sprintf(buf,
"%d\n", udev->devnum);
154 struct usb_device *
udev;
156 udev = to_usb_device(dev);
157 return sprintf(buf,
"%s\n", udev->devpath);
164 struct usb_device *
udev;
167 udev = to_usb_device(dev);
169 return sprintf(buf,
"%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
176 struct usb_device *
udev;
178 udev = to_usb_device(dev);
179 return sprintf(buf,
"%d\n", udev->maxchild);
186 struct usb_device *
udev;
188 udev = to_usb_device(dev);
189 return sprintf(buf,
"0x%x\n", udev->quirks);
196 struct usb_device *
udev;
198 udev = to_usb_device(dev);
204 const char *buf,
size_t count)
206 struct usb_device *udev = to_usb_device(dev);
209 if (
sscanf(buf,
"%d", &val) != 1 || val < 0 || val > 1)
211 usb_lock_device(udev);
216 usb_unlock_device(udev);
221 show_avoid_reset_quirk, set_avoid_reset_quirk);
226 struct usb_device *
udev;
228 udev = to_usb_device(dev);
236 struct usb_device *
udev;
239 udev = to_usb_device(dev);
241 switch (udev->removable) {
242 case USB_DEVICE_REMOVABLE:
245 case USB_DEVICE_FIXED:
252 return sprintf(buf,
"%s\n", state);
259 if (usb_device_supports_ltm(to_usb_device(dev)))
260 return sprintf(buf,
"%s\n",
"yes");
261 return sprintf(buf,
"%s\n",
"no");
270 struct usb_device *udev = to_usb_device(dev);
272 return sprintf(buf,
"%d\n", udev->persist_enabled);
277 const char *buf,
size_t count)
279 struct usb_device *udev = to_usb_device(dev);
286 if (
sscanf(buf,
"%d", &value) != 1)
289 usb_lock_device(udev);
290 udev->persist_enabled = !!
value;
291 usb_unlock_device(udev);
301 if (is_usb_device(dev)) {
302 struct usb_device *udev = to_usb_device(dev);
309 &dev_attr_persist.attr,
318 &dev_attr_persist.attr,
323 #define add_persist_attributes(dev) 0
324 #define remove_persist_attributes(dev) do {} while (0)
328 #ifdef CONFIG_USB_SUSPEND
334 struct usb_device *udev = to_usb_device(dev);
353 struct usb_device *udev = to_usb_device(dev);
360 return sprintf(buf,
"%u\n", duration);
368 return sprintf(buf,
"%d\n", dev->
power.autosuspend_delay / 1000);
373 const char *buf,
size_t count)
377 if (
sscanf(buf,
"%d", &value) != 1 || value >=
INT_MAX/1000 ||
386 show_autosuspend, set_autosuspend);
388 static const char on_string[] =
"on";
389 static const char auto_string[] =
"auto";
391 static void warn_level(
void) {
392 static int level_warned;
397 "use power/control instead\n");
404 struct usb_device *udev = to_usb_device(dev);
405 const char *
p = auto_string;
410 return sprintf(buf,
"%s\n", p);
415 const char *buf,
size_t count)
417 struct usb_device *udev = to_usb_device(dev);
423 cp =
memchr(buf,
'\n', count);
427 usb_lock_device(udev);
429 if (len ==
sizeof on_string - 1 &&
430 strncmp(buf, on_string, len) == 0)
431 usb_disable_autosuspend(udev);
433 else if (len ==
sizeof auto_string - 1 &&
434 strncmp(buf, auto_string, len) == 0)
435 usb_enable_autosuspend(udev);
440 usb_unlock_device(udev);
450 struct usb_device *udev = to_usb_device(dev);
453 if (udev->usb2_hw_lpm_enabled == 1)
458 return sprintf(buf,
"%s\n", p);
463 const char *buf,
size_t count)
465 struct usb_device *udev = to_usb_device(dev);
469 usb_lock_device(udev);
474 ret = usb_set_usb2_hardware_lpm(udev, value);
476 usb_unlock_device(udev);
485 set_usb2_hardware_lpm);
487 static struct attribute *usb2_hardware_lpm_attr[] = {
488 &dev_attr_usb2_hardware_lpm.attr,
493 .attrs = usb2_hardware_lpm_attr,
496 static struct attribute *power_attrs[] = {
497 &dev_attr_autosuspend.attr,
498 &dev_attr_level.attr,
499 &dev_attr_connected_duration.attr,
500 &dev_attr_active_duration.attr,
505 .attrs = power_attrs,
512 if (is_usb_device(dev)) {
513 struct usb_device *udev = to_usb_device(dev);
515 if (udev->usb2_hw_lpm_capable == 1)
517 &usb2_hardware_lpm_attr_group);
531 #define add_power_attributes(dev) 0
532 #define remove_power_attributes(dev) do {} while (0)
538 #define usb_descriptor_attr_le16(field, format_string) \
540 show_##field(struct device *dev, struct device_attribute *attr, \
543 struct usb_device *udev; \
545 udev = to_usb_device(dev); \
546 return sprintf(buf, format_string, \
547 le16_to_cpu(udev->descriptor.field)); \
549 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
555 #define usb_descriptor_attr(field, format_string) \
557 show_##field(struct device *dev, struct device_attribute *attr, \
560 struct usb_device *udev; \
562 udev = to_usb_device(dev); \
563 return sprintf(buf, format_string, udev->descriptor.field); \
565 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
580 struct usb_device *usb_dev = to_usb_device(dev);
592 const char *buf,
size_t size)
595 struct usb_device *usb_dev = to_usb_device(dev);
597 result =
sscanf(buf,
"%u\n", &val);
604 return result < 0? result :
size;
608 usb_dev_authorized_show, usb_dev_authorized_store);
613 const char *buf,
size_t count)
615 struct usb_device *udev = to_usb_device(dev);
618 usb_lock_device(udev);
627 usb_unlock_device(udev);
635 &dev_attr_configuration.attr,
636 &dev_attr_bNumInterfaces.attr,
637 &dev_attr_bConfigurationValue.attr,
638 &dev_attr_bmAttributes.attr,
639 &dev_attr_bMaxPower.attr,
641 &dev_attr_urbnum.attr,
642 &dev_attr_idVendor.attr,
643 &dev_attr_idProduct.attr,
644 &dev_attr_bcdDevice.attr,
645 &dev_attr_bDeviceClass.attr,
646 &dev_attr_bDeviceSubClass.attr,
647 &dev_attr_bDeviceProtocol.attr,
648 &dev_attr_bNumConfigurations.attr,
649 &dev_attr_bMaxPacketSize0.attr,
650 &dev_attr_speed.attr,
651 &dev_attr_busnum.attr,
652 &dev_attr_devnum.attr,
653 &dev_attr_devpath.attr,
654 &dev_attr_version.attr,
655 &dev_attr_maxchild.attr,
656 &dev_attr_quirks.attr,
657 &dev_attr_avoid_reset_quirk.attr,
658 &dev_attr_authorized.attr,
659 &dev_attr_remove.attr,
660 &dev_attr_removable.attr,
661 &dev_attr_ltm_capable.attr,
671 static struct attribute *dev_string_attrs[] = {
672 &dev_attr_manufacturer.attr,
673 &dev_attr_product.attr,
674 &dev_attr_serial.attr,
682 struct usb_device *udev = to_usb_device(dev);
684 if (a == &dev_attr_manufacturer.attr) {
685 if (udev->manufacturer ==
NULL)
687 }
else if (a == &dev_attr_product.attr) {
688 if (udev->product ==
NULL)
690 }
else if (a == &dev_attr_serial.attr) {
691 if (udev->serial ==
NULL)
698 .attrs = dev_string_attrs,
699 .is_visible = dev_string_attrs_are_visible,
704 &dev_string_attr_grp,
711 read_descriptors(
struct file *filp,
struct kobject *kobj,
713 char *buf, loff_t off,
size_t count)
716 struct usb_device *udev = to_usb_device(dev);
717 size_t nleft =
count;
726 for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
727 nleft > 0; ++cfgno) {
729 src = &udev->descriptor;
732 src = udev->rawdescriptors[cfgno];
737 n =
min(nleft, srclen - (
size_t) off);
738 memcpy(buf, src + off, n);
746 return count - nleft;
750 .attr = {.name =
"descriptors", .mode = 0444},
751 .read = read_descriptors,
757 struct device *dev = &udev->dev;
779 struct device *dev = &udev->dev;
787 #define usb_intf_assoc_attr(field, format_string) \
789 show_iad_##field(struct device *dev, struct device_attribute *attr, \
792 struct usb_interface *intf = to_usb_interface(dev); \
794 return sprintf(buf, format_string, \
795 intf->intf_assoc->field); \
797 static DEVICE_ATTR(iad_##field, S_IRUGO, show_iad_##field, NULL);
806 #define usb_intf_attr(field, format_string) \
808 show_##field(struct device *dev, struct device_attribute *attr, \
811 struct usb_interface *intf = to_usb_interface(dev); \
813 return sprintf(buf, format_string, \
814 intf->cur_altsetting->desc.field); \
816 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
831 intf = to_usb_interface(dev);
832 string = intf->cur_altsetting->string;
837 return sprintf(buf,
"%s\n",
string);
845 struct usb_device *
udev;
846 struct usb_host_interface *alt;
848 intf = to_usb_interface(dev);
849 udev = interface_to_usbdev(intf);
850 alt = intf->cur_altsetting;
852 return sprintf(buf,
"usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
853 "ic%02Xisc%02Xip%02Xin%02X\n",
857 udev->descriptor.bDeviceClass,
858 udev->descriptor.bDeviceSubClass,
859 udev->descriptor.bDeviceProtocol,
860 alt->desc.bInterfaceClass,
861 alt->desc.bInterfaceSubClass,
862 alt->desc.bInterfaceProtocol,
863 alt->desc.bInterfaceNumber);
871 struct usb_device *
udev;
874 intf = to_usb_interface(dev);
875 udev = interface_to_usbdev(intf);
877 usb_lock_device(udev);
879 if (!intf->dev.driver ||
880 to_usb_driver(intf->dev.driver)->supports_autosuspend)
884 usb_unlock_device(udev);
890 static struct attribute *intf_attrs[] = {
891 &dev_attr_bInterfaceNumber.attr,
892 &dev_attr_bAlternateSetting.attr,
893 &dev_attr_bNumEndpoints.attr,
894 &dev_attr_bInterfaceClass.attr,
895 &dev_attr_bInterfaceSubClass.attr,
896 &dev_attr_bInterfaceProtocol.attr,
897 &dev_attr_modalias.attr,
898 &dev_attr_supports_autosuspend.attr,
905 static struct attribute *intf_assoc_attrs[] = {
906 &dev_attr_iad_bFirstInterface.attr,
907 &dev_attr_iad_bInterfaceCount.attr,
908 &dev_attr_iad_bFunctionClass.attr,
909 &dev_attr_iad_bFunctionSubClass.attr,
910 &dev_attr_iad_bFunctionProtocol.attr,
920 if (intf->intf_assoc ==
NULL)
926 .attrs = intf_assoc_attrs,
927 .is_visible = intf_assoc_attrs_are_visible,
932 &intf_assoc_attr_grp,
938 struct usb_device *udev = interface_to_usbdev(intf);
939 struct usb_host_interface *alt = intf->cur_altsetting;
941 if (intf->sysfs_files_created || intf->unregistering)
948 intf->sysfs_files_created = 1;
953 if (!intf->sysfs_files_created)
957 intf->sysfs_files_created = 0;