Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / usb / core / sysfs.c
blobd85699bee671f791bd95c2f95abc2881914419b4
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * drivers/usb/core/sysfs.c
5 * (C) Copyright 2002 David Brownell
6 * (C) Copyright 2002,2004 Greg Kroah-Hartman
7 * (C) Copyright 2002,2004 IBM Corp.
9 * All of the sysfs file attributes for usb devices and interfaces.
11 * Released under the GPLv2 only.
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/usb.h>
18 #include <linux/usb/hcd.h>
19 #include <linux/usb/quirks.h>
20 #include <linux/of.h>
21 #include "usb.h"
23 /* Active configuration fields */
24 #define usb_actconfig_show(field, format_string) \
25 static ssize_t field##_show(struct device *dev, \
26 struct device_attribute *attr, char *buf) \
27 { \
28 struct usb_device *udev; \
29 struct usb_host_config *actconfig; \
30 ssize_t rc; \
32 udev = to_usb_device(dev); \
33 rc = usb_lock_device_interruptible(udev); \
34 if (rc < 0) \
35 return -EINTR; \
36 actconfig = udev->actconfig; \
37 if (actconfig) \
38 rc = sprintf(buf, format_string, \
39 actconfig->desc.field); \
40 usb_unlock_device(udev); \
41 return rc; \
42 } \
44 #define usb_actconfig_attr(field, format_string) \
45 usb_actconfig_show(field, format_string) \
46 static DEVICE_ATTR_RO(field)
48 usb_actconfig_attr(bNumInterfaces, "%2d\n");
49 usb_actconfig_attr(bmAttributes, "%2x\n");
51 static ssize_t bMaxPower_show(struct device *dev,
52 struct device_attribute *attr, char *buf)
54 struct usb_device *udev;
55 struct usb_host_config *actconfig;
56 ssize_t rc;
58 udev = to_usb_device(dev);
59 rc = usb_lock_device_interruptible(udev);
60 if (rc < 0)
61 return -EINTR;
62 actconfig = udev->actconfig;
63 if (actconfig)
64 rc = sprintf(buf, "%dmA\n", usb_get_max_power(udev, actconfig));
65 usb_unlock_device(udev);
66 return rc;
68 static DEVICE_ATTR_RO(bMaxPower);
70 static ssize_t configuration_show(struct device *dev,
71 struct device_attribute *attr, char *buf)
73 struct usb_device *udev;
74 struct usb_host_config *actconfig;
75 ssize_t rc;
77 udev = to_usb_device(dev);
78 rc = usb_lock_device_interruptible(udev);
79 if (rc < 0)
80 return -EINTR;
81 actconfig = udev->actconfig;
82 if (actconfig && actconfig->string)
83 rc = sprintf(buf, "%s\n", actconfig->string);
84 usb_unlock_device(udev);
85 return rc;
87 static DEVICE_ATTR_RO(configuration);
89 /* configuration value is always present, and r/w */
90 usb_actconfig_show(bConfigurationValue, "%u\n");
92 static ssize_t bConfigurationValue_store(struct device *dev,
93 struct device_attribute *attr,
94 const char *buf, size_t count)
96 struct usb_device *udev = to_usb_device(dev);
97 int config, value, rc;
99 if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
100 return -EINVAL;
101 rc = usb_lock_device_interruptible(udev);
102 if (rc < 0)
103 return -EINTR;
104 value = usb_set_configuration(udev, config);
105 usb_unlock_device(udev);
106 return (value < 0) ? value : count;
108 static DEVICE_ATTR_IGNORE_LOCKDEP(bConfigurationValue, S_IRUGO | S_IWUSR,
109 bConfigurationValue_show, bConfigurationValue_store);
111 #ifdef CONFIG_OF
112 static ssize_t devspec_show(struct device *dev, struct device_attribute *attr,
113 char *buf)
115 struct device_node *of_node = dev->of_node;
117 return sprintf(buf, "%pOF\n", of_node);
119 static DEVICE_ATTR_RO(devspec);
120 #endif
122 /* String fields */
123 #define usb_string_attr(name) \
124 static ssize_t name##_show(struct device *dev, \
125 struct device_attribute *attr, char *buf) \
127 struct usb_device *udev; \
128 int retval; \
130 udev = to_usb_device(dev); \
131 retval = usb_lock_device_interruptible(udev); \
132 if (retval < 0) \
133 return -EINTR; \
134 retval = sprintf(buf, "%s\n", udev->name); \
135 usb_unlock_device(udev); \
136 return retval; \
138 static DEVICE_ATTR_RO(name)
140 usb_string_attr(product);
141 usb_string_attr(manufacturer);
142 usb_string_attr(serial);
144 static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
145 char *buf)
147 struct usb_device *udev;
148 char *speed;
150 udev = to_usb_device(dev);
152 switch (udev->speed) {
153 case USB_SPEED_LOW:
154 speed = "1.5";
155 break;
156 case USB_SPEED_UNKNOWN:
157 case USB_SPEED_FULL:
158 speed = "12";
159 break;
160 case USB_SPEED_HIGH:
161 speed = "480";
162 break;
163 case USB_SPEED_WIRELESS:
164 speed = "480";
165 break;
166 case USB_SPEED_SUPER:
167 speed = "5000";
168 break;
169 case USB_SPEED_SUPER_PLUS:
170 speed = "10000";
171 break;
172 default:
173 speed = "unknown";
175 return sprintf(buf, "%s\n", speed);
177 static DEVICE_ATTR_RO(speed);
179 static ssize_t rx_lanes_show(struct device *dev, struct device_attribute *attr,
180 char *buf)
182 struct usb_device *udev;
184 udev = to_usb_device(dev);
185 return sprintf(buf, "%d\n", udev->rx_lanes);
187 static DEVICE_ATTR_RO(rx_lanes);
189 static ssize_t tx_lanes_show(struct device *dev, struct device_attribute *attr,
190 char *buf)
192 struct usb_device *udev;
194 udev = to_usb_device(dev);
195 return sprintf(buf, "%d\n", udev->tx_lanes);
197 static DEVICE_ATTR_RO(tx_lanes);
199 static ssize_t busnum_show(struct device *dev, struct device_attribute *attr,
200 char *buf)
202 struct usb_device *udev;
204 udev = to_usb_device(dev);
205 return sprintf(buf, "%d\n", udev->bus->busnum);
207 static DEVICE_ATTR_RO(busnum);
209 static ssize_t devnum_show(struct device *dev, struct device_attribute *attr,
210 char *buf)
212 struct usb_device *udev;
214 udev = to_usb_device(dev);
215 return sprintf(buf, "%d\n", udev->devnum);
217 static DEVICE_ATTR_RO(devnum);
219 static ssize_t devpath_show(struct device *dev, struct device_attribute *attr,
220 char *buf)
222 struct usb_device *udev;
224 udev = to_usb_device(dev);
225 return sprintf(buf, "%s\n", udev->devpath);
227 static DEVICE_ATTR_RO(devpath);
229 static ssize_t version_show(struct device *dev, struct device_attribute *attr,
230 char *buf)
232 struct usb_device *udev;
233 u16 bcdUSB;
235 udev = to_usb_device(dev);
236 bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
237 return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
239 static DEVICE_ATTR_RO(version);
241 static ssize_t maxchild_show(struct device *dev, struct device_attribute *attr,
242 char *buf)
244 struct usb_device *udev;
246 udev = to_usb_device(dev);
247 return sprintf(buf, "%d\n", udev->maxchild);
249 static DEVICE_ATTR_RO(maxchild);
251 static ssize_t quirks_show(struct device *dev, struct device_attribute *attr,
252 char *buf)
254 struct usb_device *udev;
256 udev = to_usb_device(dev);
257 return sprintf(buf, "0x%x\n", udev->quirks);
259 static DEVICE_ATTR_RO(quirks);
261 static ssize_t avoid_reset_quirk_show(struct device *dev,
262 struct device_attribute *attr, char *buf)
264 struct usb_device *udev;
266 udev = to_usb_device(dev);
267 return sprintf(buf, "%d\n", !!(udev->quirks & USB_QUIRK_RESET));
270 static ssize_t avoid_reset_quirk_store(struct device *dev,
271 struct device_attribute *attr,
272 const char *buf, size_t count)
274 struct usb_device *udev = to_usb_device(dev);
275 int val, rc;
277 if (sscanf(buf, "%d", &val) != 1 || val < 0 || val > 1)
278 return -EINVAL;
279 rc = usb_lock_device_interruptible(udev);
280 if (rc < 0)
281 return -EINTR;
282 if (val)
283 udev->quirks |= USB_QUIRK_RESET;
284 else
285 udev->quirks &= ~USB_QUIRK_RESET;
286 usb_unlock_device(udev);
287 return count;
289 static DEVICE_ATTR_RW(avoid_reset_quirk);
291 static ssize_t urbnum_show(struct device *dev, struct device_attribute *attr,
292 char *buf)
294 struct usb_device *udev;
296 udev = to_usb_device(dev);
297 return sprintf(buf, "%d\n", atomic_read(&udev->urbnum));
299 static DEVICE_ATTR_RO(urbnum);
301 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
302 char *buf)
304 struct usb_device *udev;
305 char *state;
307 udev = to_usb_device(dev);
309 switch (udev->removable) {
310 case USB_DEVICE_REMOVABLE:
311 state = "removable";
312 break;
313 case USB_DEVICE_FIXED:
314 state = "fixed";
315 break;
316 default:
317 state = "unknown";
320 return sprintf(buf, "%s\n", state);
322 static DEVICE_ATTR_RO(removable);
324 static ssize_t ltm_capable_show(struct device *dev,
325 struct device_attribute *attr, char *buf)
327 if (usb_device_supports_ltm(to_usb_device(dev)))
328 return sprintf(buf, "%s\n", "yes");
329 return sprintf(buf, "%s\n", "no");
331 static DEVICE_ATTR_RO(ltm_capable);
333 #ifdef CONFIG_PM
335 static ssize_t persist_show(struct device *dev, struct device_attribute *attr,
336 char *buf)
338 struct usb_device *udev = to_usb_device(dev);
340 return sprintf(buf, "%d\n", udev->persist_enabled);
343 static ssize_t persist_store(struct device *dev, struct device_attribute *attr,
344 const char *buf, size_t count)
346 struct usb_device *udev = to_usb_device(dev);
347 int value, rc;
349 /* Hubs are always enabled for USB_PERSIST */
350 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
351 return -EPERM;
353 if (sscanf(buf, "%d", &value) != 1)
354 return -EINVAL;
356 rc = usb_lock_device_interruptible(udev);
357 if (rc < 0)
358 return -EINTR;
359 udev->persist_enabled = !!value;
360 usb_unlock_device(udev);
361 return count;
363 static DEVICE_ATTR_RW(persist);
365 static int add_persist_attributes(struct device *dev)
367 int rc = 0;
369 if (is_usb_device(dev)) {
370 struct usb_device *udev = to_usb_device(dev);
372 /* Hubs are automatically enabled for USB_PERSIST,
373 * no point in creating the attribute file.
375 if (udev->descriptor.bDeviceClass != USB_CLASS_HUB)
376 rc = sysfs_add_file_to_group(&dev->kobj,
377 &dev_attr_persist.attr,
378 power_group_name);
380 return rc;
383 static void remove_persist_attributes(struct device *dev)
385 sysfs_remove_file_from_group(&dev->kobj,
386 &dev_attr_persist.attr,
387 power_group_name);
390 static ssize_t connected_duration_show(struct device *dev,
391 struct device_attribute *attr, char *buf)
393 struct usb_device *udev = to_usb_device(dev);
395 return sprintf(buf, "%u\n",
396 jiffies_to_msecs(jiffies - udev->connect_time));
398 static DEVICE_ATTR_RO(connected_duration);
401 * If the device is resumed, the last time the device was suspended has
402 * been pre-subtracted from active_duration. We add the current time to
403 * get the duration that the device was actually active.
405 * If the device is suspended, the active_duration is up-to-date.
407 static ssize_t active_duration_show(struct device *dev,
408 struct device_attribute *attr, char *buf)
410 struct usb_device *udev = to_usb_device(dev);
411 int duration;
413 if (udev->state != USB_STATE_SUSPENDED)
414 duration = jiffies_to_msecs(jiffies + udev->active_duration);
415 else
416 duration = jiffies_to_msecs(udev->active_duration);
417 return sprintf(buf, "%u\n", duration);
419 static DEVICE_ATTR_RO(active_duration);
421 static ssize_t autosuspend_show(struct device *dev,
422 struct device_attribute *attr, char *buf)
424 return sprintf(buf, "%d\n", dev->power.autosuspend_delay / 1000);
427 static ssize_t autosuspend_store(struct device *dev,
428 struct device_attribute *attr, const char *buf,
429 size_t count)
431 int value;
433 if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/1000 ||
434 value <= -INT_MAX/1000)
435 return -EINVAL;
437 pm_runtime_set_autosuspend_delay(dev, value * 1000);
438 return count;
440 static DEVICE_ATTR_RW(autosuspend);
442 static const char on_string[] = "on";
443 static const char auto_string[] = "auto";
445 static void warn_level(void)
447 static int level_warned;
449 if (!level_warned) {
450 level_warned = 1;
451 printk(KERN_WARNING "WARNING! power/level is deprecated; "
452 "use power/control instead\n");
456 static ssize_t level_show(struct device *dev, struct device_attribute *attr,
457 char *buf)
459 struct usb_device *udev = to_usb_device(dev);
460 const char *p = auto_string;
462 warn_level();
463 if (udev->state != USB_STATE_SUSPENDED && !udev->dev.power.runtime_auto)
464 p = on_string;
465 return sprintf(buf, "%s\n", p);
468 static ssize_t level_store(struct device *dev, struct device_attribute *attr,
469 const char *buf, size_t count)
471 struct usb_device *udev = to_usb_device(dev);
472 int len = count;
473 char *cp;
474 int rc = count;
475 int rv;
477 warn_level();
478 cp = memchr(buf, '\n', count);
479 if (cp)
480 len = cp - buf;
482 rv = usb_lock_device_interruptible(udev);
483 if (rv < 0)
484 return -EINTR;
486 if (len == sizeof on_string - 1 &&
487 strncmp(buf, on_string, len) == 0)
488 usb_disable_autosuspend(udev);
490 else if (len == sizeof auto_string - 1 &&
491 strncmp(buf, auto_string, len) == 0)
492 usb_enable_autosuspend(udev);
494 else
495 rc = -EINVAL;
497 usb_unlock_device(udev);
498 return rc;
500 static DEVICE_ATTR_RW(level);
502 static ssize_t usb2_hardware_lpm_show(struct device *dev,
503 struct device_attribute *attr, char *buf)
505 struct usb_device *udev = to_usb_device(dev);
506 const char *p;
508 if (udev->usb2_hw_lpm_allowed == 1)
509 p = "enabled";
510 else
511 p = "disabled";
513 return sprintf(buf, "%s\n", p);
516 static ssize_t usb2_hardware_lpm_store(struct device *dev,
517 struct device_attribute *attr,
518 const char *buf, size_t count)
520 struct usb_device *udev = to_usb_device(dev);
521 bool value;
522 int ret;
524 ret = usb_lock_device_interruptible(udev);
525 if (ret < 0)
526 return -EINTR;
528 ret = strtobool(buf, &value);
530 if (!ret) {
531 udev->usb2_hw_lpm_allowed = value;
532 if (value)
533 ret = usb_enable_usb2_hardware_lpm(udev);
534 else
535 ret = usb_disable_usb2_hardware_lpm(udev);
538 usb_unlock_device(udev);
540 if (!ret)
541 return count;
543 return ret;
545 static DEVICE_ATTR_RW(usb2_hardware_lpm);
547 static ssize_t usb2_lpm_l1_timeout_show(struct device *dev,
548 struct device_attribute *attr,
549 char *buf)
551 struct usb_device *udev = to_usb_device(dev);
552 return sprintf(buf, "%d\n", udev->l1_params.timeout);
555 static ssize_t usb2_lpm_l1_timeout_store(struct device *dev,
556 struct device_attribute *attr,
557 const char *buf, size_t count)
559 struct usb_device *udev = to_usb_device(dev);
560 u16 timeout;
562 if (kstrtou16(buf, 0, &timeout))
563 return -EINVAL;
565 udev->l1_params.timeout = timeout;
567 return count;
569 static DEVICE_ATTR_RW(usb2_lpm_l1_timeout);
571 static ssize_t usb2_lpm_besl_show(struct device *dev,
572 struct device_attribute *attr, char *buf)
574 struct usb_device *udev = to_usb_device(dev);
575 return sprintf(buf, "%d\n", udev->l1_params.besl);
578 static ssize_t usb2_lpm_besl_store(struct device *dev,
579 struct device_attribute *attr,
580 const char *buf, size_t count)
582 struct usb_device *udev = to_usb_device(dev);
583 u8 besl;
585 if (kstrtou8(buf, 0, &besl) || besl > 15)
586 return -EINVAL;
588 udev->l1_params.besl = besl;
590 return count;
592 static DEVICE_ATTR_RW(usb2_lpm_besl);
594 static ssize_t usb3_hardware_lpm_u1_show(struct device *dev,
595 struct device_attribute *attr, char *buf)
597 struct usb_device *udev = to_usb_device(dev);
598 const char *p;
599 int rc;
601 rc = usb_lock_device_interruptible(udev);
602 if (rc < 0)
603 return -EINTR;
605 if (udev->usb3_lpm_u1_enabled)
606 p = "enabled";
607 else
608 p = "disabled";
610 usb_unlock_device(udev);
612 return sprintf(buf, "%s\n", p);
614 static DEVICE_ATTR_RO(usb3_hardware_lpm_u1);
616 static ssize_t usb3_hardware_lpm_u2_show(struct device *dev,
617 struct device_attribute *attr, char *buf)
619 struct usb_device *udev = to_usb_device(dev);
620 const char *p;
621 int rc;
623 rc = usb_lock_device_interruptible(udev);
624 if (rc < 0)
625 return -EINTR;
627 if (udev->usb3_lpm_u2_enabled)
628 p = "enabled";
629 else
630 p = "disabled";
632 usb_unlock_device(udev);
634 return sprintf(buf, "%s\n", p);
636 static DEVICE_ATTR_RO(usb3_hardware_lpm_u2);
638 static struct attribute *usb2_hardware_lpm_attr[] = {
639 &dev_attr_usb2_hardware_lpm.attr,
640 &dev_attr_usb2_lpm_l1_timeout.attr,
641 &dev_attr_usb2_lpm_besl.attr,
642 NULL,
644 static const struct attribute_group usb2_hardware_lpm_attr_group = {
645 .name = power_group_name,
646 .attrs = usb2_hardware_lpm_attr,
649 static struct attribute *usb3_hardware_lpm_attr[] = {
650 &dev_attr_usb3_hardware_lpm_u1.attr,
651 &dev_attr_usb3_hardware_lpm_u2.attr,
652 NULL,
654 static const struct attribute_group usb3_hardware_lpm_attr_group = {
655 .name = power_group_name,
656 .attrs = usb3_hardware_lpm_attr,
659 static struct attribute *power_attrs[] = {
660 &dev_attr_autosuspend.attr,
661 &dev_attr_level.attr,
662 &dev_attr_connected_duration.attr,
663 &dev_attr_active_duration.attr,
664 NULL,
666 static const struct attribute_group power_attr_group = {
667 .name = power_group_name,
668 .attrs = power_attrs,
671 static int add_power_attributes(struct device *dev)
673 int rc = 0;
675 if (is_usb_device(dev)) {
676 struct usb_device *udev = to_usb_device(dev);
677 rc = sysfs_merge_group(&dev->kobj, &power_attr_group);
678 if (udev->usb2_hw_lpm_capable == 1)
679 rc = sysfs_merge_group(&dev->kobj,
680 &usb2_hardware_lpm_attr_group);
681 if ((udev->speed == USB_SPEED_SUPER ||
682 udev->speed == USB_SPEED_SUPER_PLUS) &&
683 udev->lpm_capable == 1)
684 rc = sysfs_merge_group(&dev->kobj,
685 &usb3_hardware_lpm_attr_group);
688 return rc;
691 static void remove_power_attributes(struct device *dev)
693 sysfs_unmerge_group(&dev->kobj, &usb2_hardware_lpm_attr_group);
694 sysfs_unmerge_group(&dev->kobj, &power_attr_group);
697 #else
699 #define add_persist_attributes(dev) 0
700 #define remove_persist_attributes(dev) do {} while (0)
702 #define add_power_attributes(dev) 0
703 #define remove_power_attributes(dev) do {} while (0)
705 #endif /* CONFIG_PM */
708 /* Descriptor fields */
709 #define usb_descriptor_attr_le16(field, format_string) \
710 static ssize_t \
711 field##_show(struct device *dev, struct device_attribute *attr, \
712 char *buf) \
714 struct usb_device *udev; \
716 udev = to_usb_device(dev); \
717 return sprintf(buf, format_string, \
718 le16_to_cpu(udev->descriptor.field)); \
720 static DEVICE_ATTR_RO(field)
722 usb_descriptor_attr_le16(idVendor, "%04x\n");
723 usb_descriptor_attr_le16(idProduct, "%04x\n");
724 usb_descriptor_attr_le16(bcdDevice, "%04x\n");
726 #define usb_descriptor_attr(field, format_string) \
727 static ssize_t \
728 field##_show(struct device *dev, struct device_attribute *attr, \
729 char *buf) \
731 struct usb_device *udev; \
733 udev = to_usb_device(dev); \
734 return sprintf(buf, format_string, udev->descriptor.field); \
736 static DEVICE_ATTR_RO(field)
738 usb_descriptor_attr(bDeviceClass, "%02x\n");
739 usb_descriptor_attr(bDeviceSubClass, "%02x\n");
740 usb_descriptor_attr(bDeviceProtocol, "%02x\n");
741 usb_descriptor_attr(bNumConfigurations, "%d\n");
742 usb_descriptor_attr(bMaxPacketSize0, "%d\n");
745 /* show if the device is authorized (1) or not (0) */
746 static ssize_t authorized_show(struct device *dev,
747 struct device_attribute *attr, char *buf)
749 struct usb_device *usb_dev = to_usb_device(dev);
750 return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
754 * Authorize a device to be used in the system
756 * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
758 static ssize_t authorized_store(struct device *dev,
759 struct device_attribute *attr, const char *buf,
760 size_t size)
762 ssize_t result;
763 struct usb_device *usb_dev = to_usb_device(dev);
764 unsigned val;
765 result = sscanf(buf, "%u\n", &val);
766 if (result != 1)
767 result = -EINVAL;
768 else if (val == 0)
769 result = usb_deauthorize_device(usb_dev);
770 else
771 result = usb_authorize_device(usb_dev);
772 return result < 0 ? result : size;
774 static DEVICE_ATTR_IGNORE_LOCKDEP(authorized, S_IRUGO | S_IWUSR,
775 authorized_show, authorized_store);
777 /* "Safely remove a device" */
778 static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
779 const char *buf, size_t count)
781 struct usb_device *udev = to_usb_device(dev);
782 int rc = 0;
784 usb_lock_device(udev);
785 if (udev->state != USB_STATE_NOTATTACHED) {
787 /* To avoid races, first unconfigure and then remove */
788 usb_set_configuration(udev, -1);
789 rc = usb_remove_device(udev);
791 if (rc == 0)
792 rc = count;
793 usb_unlock_device(udev);
794 return rc;
796 static DEVICE_ATTR_IGNORE_LOCKDEP(remove, S_IWUSR, NULL, remove_store);
799 static struct attribute *dev_attrs[] = {
800 /* current configuration's attributes */
801 &dev_attr_configuration.attr,
802 &dev_attr_bNumInterfaces.attr,
803 &dev_attr_bConfigurationValue.attr,
804 &dev_attr_bmAttributes.attr,
805 &dev_attr_bMaxPower.attr,
806 /* device attributes */
807 &dev_attr_urbnum.attr,
808 &dev_attr_idVendor.attr,
809 &dev_attr_idProduct.attr,
810 &dev_attr_bcdDevice.attr,
811 &dev_attr_bDeviceClass.attr,
812 &dev_attr_bDeviceSubClass.attr,
813 &dev_attr_bDeviceProtocol.attr,
814 &dev_attr_bNumConfigurations.attr,
815 &dev_attr_bMaxPacketSize0.attr,
816 &dev_attr_speed.attr,
817 &dev_attr_rx_lanes.attr,
818 &dev_attr_tx_lanes.attr,
819 &dev_attr_busnum.attr,
820 &dev_attr_devnum.attr,
821 &dev_attr_devpath.attr,
822 &dev_attr_version.attr,
823 &dev_attr_maxchild.attr,
824 &dev_attr_quirks.attr,
825 &dev_attr_avoid_reset_quirk.attr,
826 &dev_attr_authorized.attr,
827 &dev_attr_remove.attr,
828 &dev_attr_removable.attr,
829 &dev_attr_ltm_capable.attr,
830 #ifdef CONFIG_OF
831 &dev_attr_devspec.attr,
832 #endif
833 NULL,
835 static const struct attribute_group dev_attr_grp = {
836 .attrs = dev_attrs,
839 /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
840 * accordingly.
842 static struct attribute *dev_string_attrs[] = {
843 &dev_attr_manufacturer.attr,
844 &dev_attr_product.attr,
845 &dev_attr_serial.attr,
846 NULL
849 static umode_t dev_string_attrs_are_visible(struct kobject *kobj,
850 struct attribute *a, int n)
852 struct device *dev = kobj_to_dev(kobj);
853 struct usb_device *udev = to_usb_device(dev);
855 if (a == &dev_attr_manufacturer.attr) {
856 if (udev->manufacturer == NULL)
857 return 0;
858 } else if (a == &dev_attr_product.attr) {
859 if (udev->product == NULL)
860 return 0;
861 } else if (a == &dev_attr_serial.attr) {
862 if (udev->serial == NULL)
863 return 0;
865 return a->mode;
868 static const struct attribute_group dev_string_attr_grp = {
869 .attrs = dev_string_attrs,
870 .is_visible = dev_string_attrs_are_visible,
873 const struct attribute_group *usb_device_groups[] = {
874 &dev_attr_grp,
875 &dev_string_attr_grp,
876 NULL
879 /* Binary descriptors */
881 static ssize_t
882 read_descriptors(struct file *filp, struct kobject *kobj,
883 struct bin_attribute *attr,
884 char *buf, loff_t off, size_t count)
886 struct device *dev = kobj_to_dev(kobj);
887 struct usb_device *udev = to_usb_device(dev);
888 size_t nleft = count;
889 size_t srclen, n;
890 int cfgno;
891 void *src;
892 int retval;
894 retval = usb_lock_device_interruptible(udev);
895 if (retval < 0)
896 return -EINTR;
897 /* The binary attribute begins with the device descriptor.
898 * Following that are the raw descriptor entries for all the
899 * configurations (config plus subsidiary descriptors).
901 for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
902 nleft > 0; ++cfgno) {
903 if (cfgno < 0) {
904 src = &udev->descriptor;
905 srclen = sizeof(struct usb_device_descriptor);
906 } else {
907 src = udev->rawdescriptors[cfgno];
908 srclen = __le16_to_cpu(udev->config[cfgno].desc.
909 wTotalLength);
911 if (off < srclen) {
912 n = min(nleft, srclen - (size_t) off);
913 memcpy(buf, src + off, n);
914 nleft -= n;
915 buf += n;
916 off = 0;
917 } else {
918 off -= srclen;
921 usb_unlock_device(udev);
922 return count - nleft;
925 static struct bin_attribute dev_bin_attr_descriptors = {
926 .attr = {.name = "descriptors", .mode = 0444},
927 .read = read_descriptors,
928 .size = 18 + 65535, /* dev descr + max-size raw descriptor */
932 * Show & store the current value of authorized_default
934 static ssize_t authorized_default_show(struct device *dev,
935 struct device_attribute *attr, char *buf)
937 struct usb_device *rh_usb_dev = to_usb_device(dev);
938 struct usb_bus *usb_bus = rh_usb_dev->bus;
939 struct usb_hcd *hcd;
941 hcd = bus_to_hcd(usb_bus);
942 return snprintf(buf, PAGE_SIZE, "%u\n", hcd->dev_policy);
945 static ssize_t authorized_default_store(struct device *dev,
946 struct device_attribute *attr,
947 const char *buf, size_t size)
949 ssize_t result;
950 unsigned int val;
951 struct usb_device *rh_usb_dev = to_usb_device(dev);
952 struct usb_bus *usb_bus = rh_usb_dev->bus;
953 struct usb_hcd *hcd;
955 hcd = bus_to_hcd(usb_bus);
956 result = sscanf(buf, "%u\n", &val);
957 if (result == 1) {
958 hcd->dev_policy = val <= USB_DEVICE_AUTHORIZE_INTERNAL ?
959 val : USB_DEVICE_AUTHORIZE_ALL;
960 result = size;
961 } else {
962 result = -EINVAL;
964 return result;
966 static DEVICE_ATTR_RW(authorized_default);
969 * interface_authorized_default_show - show default authorization status
970 * for USB interfaces
972 * note: interface_authorized_default is the default value
973 * for initializing the authorized attribute of interfaces
975 static ssize_t interface_authorized_default_show(struct device *dev,
976 struct device_attribute *attr, char *buf)
978 struct usb_device *usb_dev = to_usb_device(dev);
979 struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus);
981 return sprintf(buf, "%u\n", !!HCD_INTF_AUTHORIZED(hcd));
985 * interface_authorized_default_store - store default authorization status
986 * for USB interfaces
988 * note: interface_authorized_default is the default value
989 * for initializing the authorized attribute of interfaces
991 static ssize_t interface_authorized_default_store(struct device *dev,
992 struct device_attribute *attr, const char *buf, size_t count)
994 struct usb_device *usb_dev = to_usb_device(dev);
995 struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus);
996 int rc = count;
997 bool val;
999 if (strtobool(buf, &val) != 0)
1000 return -EINVAL;
1002 if (val)
1003 set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags);
1004 else
1005 clear_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags);
1007 return rc;
1009 static DEVICE_ATTR_RW(interface_authorized_default);
1011 /* Group all the USB bus attributes */
1012 static struct attribute *usb_bus_attrs[] = {
1013 &dev_attr_authorized_default.attr,
1014 &dev_attr_interface_authorized_default.attr,
1015 NULL,
1018 static const struct attribute_group usb_bus_attr_group = {
1019 .name = NULL, /* we want them in the same directory */
1020 .attrs = usb_bus_attrs,
1024 static int add_default_authorized_attributes(struct device *dev)
1026 int rc = 0;
1028 if (is_usb_device(dev))
1029 rc = sysfs_create_group(&dev->kobj, &usb_bus_attr_group);
1031 return rc;
1034 static void remove_default_authorized_attributes(struct device *dev)
1036 if (is_usb_device(dev)) {
1037 sysfs_remove_group(&dev->kobj, &usb_bus_attr_group);
1041 int usb_create_sysfs_dev_files(struct usb_device *udev)
1043 struct device *dev = &udev->dev;
1044 int retval;
1046 retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
1047 if (retval)
1048 goto error;
1050 retval = add_persist_attributes(dev);
1051 if (retval)
1052 goto error;
1054 retval = add_power_attributes(dev);
1055 if (retval)
1056 goto error;
1058 if (is_root_hub(udev)) {
1059 retval = add_default_authorized_attributes(dev);
1060 if (retval)
1061 goto error;
1063 return retval;
1065 error:
1066 usb_remove_sysfs_dev_files(udev);
1067 return retval;
1070 void usb_remove_sysfs_dev_files(struct usb_device *udev)
1072 struct device *dev = &udev->dev;
1074 if (is_root_hub(udev))
1075 remove_default_authorized_attributes(dev);
1077 remove_power_attributes(dev);
1078 remove_persist_attributes(dev);
1079 device_remove_bin_file(dev, &dev_bin_attr_descriptors);
1082 /* Interface Association Descriptor fields */
1083 #define usb_intf_assoc_attr(field, format_string) \
1084 static ssize_t \
1085 iad_##field##_show(struct device *dev, struct device_attribute *attr, \
1086 char *buf) \
1088 struct usb_interface *intf = to_usb_interface(dev); \
1090 return sprintf(buf, format_string, \
1091 intf->intf_assoc->field); \
1093 static DEVICE_ATTR_RO(iad_##field)
1095 usb_intf_assoc_attr(bFirstInterface, "%02x\n");
1096 usb_intf_assoc_attr(bInterfaceCount, "%02d\n");
1097 usb_intf_assoc_attr(bFunctionClass, "%02x\n");
1098 usb_intf_assoc_attr(bFunctionSubClass, "%02x\n");
1099 usb_intf_assoc_attr(bFunctionProtocol, "%02x\n");
1101 /* Interface fields */
1102 #define usb_intf_attr(field, format_string) \
1103 static ssize_t \
1104 field##_show(struct device *dev, struct device_attribute *attr, \
1105 char *buf) \
1107 struct usb_interface *intf = to_usb_interface(dev); \
1109 return sprintf(buf, format_string, \
1110 intf->cur_altsetting->desc.field); \
1112 static DEVICE_ATTR_RO(field)
1114 usb_intf_attr(bInterfaceNumber, "%02x\n");
1115 usb_intf_attr(bAlternateSetting, "%2d\n");
1116 usb_intf_attr(bNumEndpoints, "%02x\n");
1117 usb_intf_attr(bInterfaceClass, "%02x\n");
1118 usb_intf_attr(bInterfaceSubClass, "%02x\n");
1119 usb_intf_attr(bInterfaceProtocol, "%02x\n");
1121 static ssize_t interface_show(struct device *dev, struct device_attribute *attr,
1122 char *buf)
1124 struct usb_interface *intf;
1125 char *string;
1127 intf = to_usb_interface(dev);
1128 string = READ_ONCE(intf->cur_altsetting->string);
1129 if (!string)
1130 return 0;
1131 return sprintf(buf, "%s\n", string);
1133 static DEVICE_ATTR_RO(interface);
1135 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
1136 char *buf)
1138 struct usb_interface *intf;
1139 struct usb_device *udev;
1140 struct usb_host_interface *alt;
1142 intf = to_usb_interface(dev);
1143 udev = interface_to_usbdev(intf);
1144 alt = READ_ONCE(intf->cur_altsetting);
1146 return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
1147 "ic%02Xisc%02Xip%02Xin%02X\n",
1148 le16_to_cpu(udev->descriptor.idVendor),
1149 le16_to_cpu(udev->descriptor.idProduct),
1150 le16_to_cpu(udev->descriptor.bcdDevice),
1151 udev->descriptor.bDeviceClass,
1152 udev->descriptor.bDeviceSubClass,
1153 udev->descriptor.bDeviceProtocol,
1154 alt->desc.bInterfaceClass,
1155 alt->desc.bInterfaceSubClass,
1156 alt->desc.bInterfaceProtocol,
1157 alt->desc.bInterfaceNumber);
1159 static DEVICE_ATTR_RO(modalias);
1161 static ssize_t supports_autosuspend_show(struct device *dev,
1162 struct device_attribute *attr,
1163 char *buf)
1165 int s;
1167 s = device_lock_interruptible(dev);
1168 if (s < 0)
1169 return -EINTR;
1170 /* Devices will be autosuspended even when an interface isn't claimed */
1171 s = (!dev->driver || to_usb_driver(dev->driver)->supports_autosuspend);
1172 device_unlock(dev);
1174 return sprintf(buf, "%u\n", s);
1176 static DEVICE_ATTR_RO(supports_autosuspend);
1179 * interface_authorized_show - show authorization status of an USB interface
1180 * 1 is authorized, 0 is deauthorized
1182 static ssize_t interface_authorized_show(struct device *dev,
1183 struct device_attribute *attr, char *buf)
1185 struct usb_interface *intf = to_usb_interface(dev);
1187 return sprintf(buf, "%u\n", intf->authorized);
1191 * interface_authorized_store - authorize or deauthorize an USB interface
1193 static ssize_t interface_authorized_store(struct device *dev,
1194 struct device_attribute *attr, const char *buf, size_t count)
1196 struct usb_interface *intf = to_usb_interface(dev);
1197 bool val;
1199 if (strtobool(buf, &val) != 0)
1200 return -EINVAL;
1202 if (val)
1203 usb_authorize_interface(intf);
1204 else
1205 usb_deauthorize_interface(intf);
1207 return count;
1209 static struct device_attribute dev_attr_interface_authorized =
1210 __ATTR(authorized, S_IRUGO | S_IWUSR,
1211 interface_authorized_show, interface_authorized_store);
1213 static struct attribute *intf_attrs[] = {
1214 &dev_attr_bInterfaceNumber.attr,
1215 &dev_attr_bAlternateSetting.attr,
1216 &dev_attr_bNumEndpoints.attr,
1217 &dev_attr_bInterfaceClass.attr,
1218 &dev_attr_bInterfaceSubClass.attr,
1219 &dev_attr_bInterfaceProtocol.attr,
1220 &dev_attr_modalias.attr,
1221 &dev_attr_supports_autosuspend.attr,
1222 &dev_attr_interface_authorized.attr,
1223 NULL,
1225 static const struct attribute_group intf_attr_grp = {
1226 .attrs = intf_attrs,
1229 static struct attribute *intf_assoc_attrs[] = {
1230 &dev_attr_iad_bFirstInterface.attr,
1231 &dev_attr_iad_bInterfaceCount.attr,
1232 &dev_attr_iad_bFunctionClass.attr,
1233 &dev_attr_iad_bFunctionSubClass.attr,
1234 &dev_attr_iad_bFunctionProtocol.attr,
1235 NULL,
1238 static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
1239 struct attribute *a, int n)
1241 struct device *dev = kobj_to_dev(kobj);
1242 struct usb_interface *intf = to_usb_interface(dev);
1244 if (intf->intf_assoc == NULL)
1245 return 0;
1246 return a->mode;
1249 static const struct attribute_group intf_assoc_attr_grp = {
1250 .attrs = intf_assoc_attrs,
1251 .is_visible = intf_assoc_attrs_are_visible,
1254 const struct attribute_group *usb_interface_groups[] = {
1255 &intf_attr_grp,
1256 &intf_assoc_attr_grp,
1257 NULL
1260 void usb_create_sysfs_intf_files(struct usb_interface *intf)
1262 struct usb_device *udev = interface_to_usbdev(intf);
1263 struct usb_host_interface *alt = intf->cur_altsetting;
1265 if (intf->sysfs_files_created || intf->unregistering)
1266 return;
1268 if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
1269 alt->string = usb_cache_string(udev, alt->desc.iInterface);
1270 if (alt->string && device_create_file(&intf->dev, &dev_attr_interface)) {
1271 /* This is not a serious error */
1272 dev_dbg(&intf->dev, "interface string descriptor file not created\n");
1274 intf->sysfs_files_created = 1;
1277 void usb_remove_sysfs_intf_files(struct usb_interface *intf)
1279 if (!intf->sysfs_files_created)
1280 return;
1282 device_remove_file(&intf->dev, &dev_attr_interface);
1283 intf->sysfs_files_created = 0;