Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * drivers/usb/core/sysfs.c
0004  *
0005  * (C) Copyright 2002 David Brownell
0006  * (C) Copyright 2002,2004 Greg Kroah-Hartman
0007  * (C) Copyright 2002,2004 IBM Corp.
0008  *
0009  * All of the sysfs file attributes for usb devices and interfaces.
0010  *
0011  * Released under the GPLv2 only.
0012  */
0013 
0014 
0015 #include <linux/kernel.h>
0016 #include <linux/string.h>
0017 #include <linux/usb.h>
0018 #include <linux/usb/hcd.h>
0019 #include <linux/usb/quirks.h>
0020 #include <linux/of.h>
0021 #include "usb.h"
0022 
0023 /* Active configuration fields */
0024 #define usb_actconfig_show(field, format_string)            \
0025 static ssize_t field##_show(struct device *dev,             \
0026                 struct device_attribute *attr, char *buf)   \
0027 {                                   \
0028     struct usb_device *udev;                    \
0029     struct usb_host_config *actconfig;              \
0030     ssize_t rc;                         \
0031                                     \
0032     udev = to_usb_device(dev);                  \
0033     rc = usb_lock_device_interruptible(udev);           \
0034     if (rc < 0)                         \
0035         return -EINTR;                      \
0036     actconfig = udev->actconfig;                    \
0037     if (actconfig)                          \
0038         rc = sysfs_emit(buf, format_string,         \
0039                 actconfig->desc.field);         \
0040     usb_unlock_device(udev);                    \
0041     return rc;                          \
0042 }                                   \
0043 
0044 #define usb_actconfig_attr(field, format_string)        \
0045     usb_actconfig_show(field, format_string)        \
0046     static DEVICE_ATTR_RO(field)
0047 
0048 usb_actconfig_attr(bNumInterfaces, "%2d\n");
0049 usb_actconfig_attr(bmAttributes, "%2x\n");
0050 
0051 static ssize_t bMaxPower_show(struct device *dev,
0052         struct device_attribute *attr, char *buf)
0053 {
0054     struct usb_device *udev;
0055     struct usb_host_config *actconfig;
0056     ssize_t rc;
0057 
0058     udev = to_usb_device(dev);
0059     rc = usb_lock_device_interruptible(udev);
0060     if (rc < 0)
0061         return -EINTR;
0062     actconfig = udev->actconfig;
0063     if (actconfig)
0064         rc = sysfs_emit(buf, "%dmA\n", usb_get_max_power(udev, actconfig));
0065     usb_unlock_device(udev);
0066     return rc;
0067 }
0068 static DEVICE_ATTR_RO(bMaxPower);
0069 
0070 static ssize_t configuration_show(struct device *dev,
0071         struct device_attribute *attr, char *buf)
0072 {
0073     struct usb_device *udev;
0074     struct usb_host_config *actconfig;
0075     ssize_t rc;
0076 
0077     udev = to_usb_device(dev);
0078     rc = usb_lock_device_interruptible(udev);
0079     if (rc < 0)
0080         return -EINTR;
0081     actconfig = udev->actconfig;
0082     if (actconfig && actconfig->string)
0083         rc = sysfs_emit(buf, "%s\n", actconfig->string);
0084     usb_unlock_device(udev);
0085     return rc;
0086 }
0087 static DEVICE_ATTR_RO(configuration);
0088 
0089 /* configuration value is always present, and r/w */
0090 usb_actconfig_show(bConfigurationValue, "%u\n");
0091 
0092 static ssize_t bConfigurationValue_store(struct device *dev,
0093                      struct device_attribute *attr,
0094                      const char *buf, size_t count)
0095 {
0096     struct usb_device   *udev = to_usb_device(dev);
0097     int         config, value, rc;
0098 
0099     if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
0100         return -EINVAL;
0101     rc = usb_lock_device_interruptible(udev);
0102     if (rc < 0)
0103         return -EINTR;
0104     value = usb_set_configuration(udev, config);
0105     usb_unlock_device(udev);
0106     return (value < 0) ? value : count;
0107 }
0108 static DEVICE_ATTR_IGNORE_LOCKDEP(bConfigurationValue, S_IRUGO | S_IWUSR,
0109         bConfigurationValue_show, bConfigurationValue_store);
0110 
0111 #ifdef CONFIG_OF
0112 static ssize_t devspec_show(struct device *dev, struct device_attribute *attr,
0113                 char *buf)
0114 {
0115     struct device_node *of_node = dev->of_node;
0116 
0117     return sysfs_emit(buf, "%pOF\n", of_node);
0118 }
0119 static DEVICE_ATTR_RO(devspec);
0120 #endif
0121 
0122 /* String fields */
0123 #define usb_string_attr(name)                       \
0124 static ssize_t  name##_show(struct device *dev,             \
0125         struct device_attribute *attr, char *buf)       \
0126 {                                   \
0127     struct usb_device *udev;                    \
0128     int retval;                         \
0129                                     \
0130     udev = to_usb_device(dev);                  \
0131     retval = usb_lock_device_interruptible(udev);           \
0132     if (retval < 0)                         \
0133         return -EINTR;                      \
0134     retval = sysfs_emit(buf, "%s\n", udev->name);           \
0135     usb_unlock_device(udev);                    \
0136     return retval;                          \
0137 }                                   \
0138 static DEVICE_ATTR_RO(name)
0139 
0140 usb_string_attr(product);
0141 usb_string_attr(manufacturer);
0142 usb_string_attr(serial);
0143 
0144 static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
0145               char *buf)
0146 {
0147     struct usb_device *udev;
0148     char *speed;
0149 
0150     udev = to_usb_device(dev);
0151 
0152     switch (udev->speed) {
0153     case USB_SPEED_LOW:
0154         speed = "1.5";
0155         break;
0156     case USB_SPEED_UNKNOWN:
0157     case USB_SPEED_FULL:
0158         speed = "12";
0159         break;
0160     case USB_SPEED_HIGH:
0161         speed = "480";
0162         break;
0163     case USB_SPEED_WIRELESS:
0164         speed = "480";
0165         break;
0166     case USB_SPEED_SUPER:
0167         speed = "5000";
0168         break;
0169     case USB_SPEED_SUPER_PLUS:
0170         if (udev->ssp_rate == USB_SSP_GEN_2x2)
0171             speed = "20000";
0172         else
0173             speed = "10000";
0174         break;
0175     default:
0176         speed = "unknown";
0177     }
0178     return sysfs_emit(buf, "%s\n", speed);
0179 }
0180 static DEVICE_ATTR_RO(speed);
0181 
0182 static ssize_t rx_lanes_show(struct device *dev, struct device_attribute *attr,
0183               char *buf)
0184 {
0185     struct usb_device *udev;
0186 
0187     udev = to_usb_device(dev);
0188     return sysfs_emit(buf, "%d\n", udev->rx_lanes);
0189 }
0190 static DEVICE_ATTR_RO(rx_lanes);
0191 
0192 static ssize_t tx_lanes_show(struct device *dev, struct device_attribute *attr,
0193               char *buf)
0194 {
0195     struct usb_device *udev;
0196 
0197     udev = to_usb_device(dev);
0198     return sysfs_emit(buf, "%d\n", udev->tx_lanes);
0199 }
0200 static DEVICE_ATTR_RO(tx_lanes);
0201 
0202 static ssize_t busnum_show(struct device *dev, struct device_attribute *attr,
0203                char *buf)
0204 {
0205     struct usb_device *udev;
0206 
0207     udev = to_usb_device(dev);
0208     return sysfs_emit(buf, "%d\n", udev->bus->busnum);
0209 }
0210 static DEVICE_ATTR_RO(busnum);
0211 
0212 static ssize_t devnum_show(struct device *dev, struct device_attribute *attr,
0213                char *buf)
0214 {
0215     struct usb_device *udev;
0216 
0217     udev = to_usb_device(dev);
0218     return sysfs_emit(buf, "%d\n", udev->devnum);
0219 }
0220 static DEVICE_ATTR_RO(devnum);
0221 
0222 static ssize_t devpath_show(struct device *dev, struct device_attribute *attr,
0223                 char *buf)
0224 {
0225     struct usb_device *udev;
0226 
0227     udev = to_usb_device(dev);
0228     return sysfs_emit(buf, "%s\n", udev->devpath);
0229 }
0230 static DEVICE_ATTR_RO(devpath);
0231 
0232 static ssize_t version_show(struct device *dev, struct device_attribute *attr,
0233                 char *buf)
0234 {
0235     struct usb_device *udev;
0236     u16 bcdUSB;
0237 
0238     udev = to_usb_device(dev);
0239     bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
0240     return sysfs_emit(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
0241 }
0242 static DEVICE_ATTR_RO(version);
0243 
0244 static ssize_t maxchild_show(struct device *dev, struct device_attribute *attr,
0245                  char *buf)
0246 {
0247     struct usb_device *udev;
0248 
0249     udev = to_usb_device(dev);
0250     return sysfs_emit(buf, "%d\n", udev->maxchild);
0251 }
0252 static DEVICE_ATTR_RO(maxchild);
0253 
0254 static ssize_t quirks_show(struct device *dev, struct device_attribute *attr,
0255                char *buf)
0256 {
0257     struct usb_device *udev;
0258 
0259     udev = to_usb_device(dev);
0260     return sysfs_emit(buf, "0x%x\n", udev->quirks);
0261 }
0262 static DEVICE_ATTR_RO(quirks);
0263 
0264 static ssize_t avoid_reset_quirk_show(struct device *dev,
0265                       struct device_attribute *attr, char *buf)
0266 {
0267     struct usb_device *udev;
0268 
0269     udev = to_usb_device(dev);
0270     return sysfs_emit(buf, "%d\n", !!(udev->quirks & USB_QUIRK_RESET));
0271 }
0272 
0273 static ssize_t avoid_reset_quirk_store(struct device *dev,
0274                       struct device_attribute *attr,
0275                       const char *buf, size_t count)
0276 {
0277     struct usb_device   *udev = to_usb_device(dev);
0278     int         val, rc;
0279 
0280     if (sscanf(buf, "%d", &val) != 1 || val < 0 || val > 1)
0281         return -EINVAL;
0282     rc = usb_lock_device_interruptible(udev);
0283     if (rc < 0)
0284         return -EINTR;
0285     if (val)
0286         udev->quirks |= USB_QUIRK_RESET;
0287     else
0288         udev->quirks &= ~USB_QUIRK_RESET;
0289     usb_unlock_device(udev);
0290     return count;
0291 }
0292 static DEVICE_ATTR_RW(avoid_reset_quirk);
0293 
0294 static ssize_t urbnum_show(struct device *dev, struct device_attribute *attr,
0295                char *buf)
0296 {
0297     struct usb_device *udev;
0298 
0299     udev = to_usb_device(dev);
0300     return sysfs_emit(buf, "%d\n", atomic_read(&udev->urbnum));
0301 }
0302 static DEVICE_ATTR_RO(urbnum);
0303 
0304 static ssize_t ltm_capable_show(struct device *dev,
0305                 struct device_attribute *attr, char *buf)
0306 {
0307     if (usb_device_supports_ltm(to_usb_device(dev)))
0308         return sysfs_emit(buf, "%s\n", "yes");
0309     return sysfs_emit(buf, "%s\n", "no");
0310 }
0311 static DEVICE_ATTR_RO(ltm_capable);
0312 
0313 #ifdef  CONFIG_PM
0314 
0315 static ssize_t persist_show(struct device *dev, struct device_attribute *attr,
0316                 char *buf)
0317 {
0318     struct usb_device *udev = to_usb_device(dev);
0319 
0320     return sysfs_emit(buf, "%d\n", udev->persist_enabled);
0321 }
0322 
0323 static ssize_t persist_store(struct device *dev, struct device_attribute *attr,
0324                  const char *buf, size_t count)
0325 {
0326     struct usb_device *udev = to_usb_device(dev);
0327     int value, rc;
0328 
0329     /* Hubs are always enabled for USB_PERSIST */
0330     if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
0331         return -EPERM;
0332 
0333     if (sscanf(buf, "%d", &value) != 1)
0334         return -EINVAL;
0335 
0336     rc = usb_lock_device_interruptible(udev);
0337     if (rc < 0)
0338         return -EINTR;
0339     udev->persist_enabled = !!value;
0340     usb_unlock_device(udev);
0341     return count;
0342 }
0343 static DEVICE_ATTR_RW(persist);
0344 
0345 static int add_persist_attributes(struct device *dev)
0346 {
0347     int rc = 0;
0348 
0349     if (is_usb_device(dev)) {
0350         struct usb_device *udev = to_usb_device(dev);
0351 
0352         /* Hubs are automatically enabled for USB_PERSIST,
0353          * no point in creating the attribute file.
0354          */
0355         if (udev->descriptor.bDeviceClass != USB_CLASS_HUB)
0356             rc = sysfs_add_file_to_group(&dev->kobj,
0357                     &dev_attr_persist.attr,
0358                     power_group_name);
0359     }
0360     return rc;
0361 }
0362 
0363 static void remove_persist_attributes(struct device *dev)
0364 {
0365     sysfs_remove_file_from_group(&dev->kobj,
0366             &dev_attr_persist.attr,
0367             power_group_name);
0368 }
0369 
0370 static ssize_t connected_duration_show(struct device *dev,
0371                        struct device_attribute *attr, char *buf)
0372 {
0373     struct usb_device *udev = to_usb_device(dev);
0374 
0375     return sysfs_emit(buf, "%u\n",
0376             jiffies_to_msecs(jiffies - udev->connect_time));
0377 }
0378 static DEVICE_ATTR_RO(connected_duration);
0379 
0380 /*
0381  * If the device is resumed, the last time the device was suspended has
0382  * been pre-subtracted from active_duration.  We add the current time to
0383  * get the duration that the device was actually active.
0384  *
0385  * If the device is suspended, the active_duration is up-to-date.
0386  */
0387 static ssize_t active_duration_show(struct device *dev,
0388                     struct device_attribute *attr, char *buf)
0389 {
0390     struct usb_device *udev = to_usb_device(dev);
0391     int duration;
0392 
0393     if (udev->state != USB_STATE_SUSPENDED)
0394         duration = jiffies_to_msecs(jiffies + udev->active_duration);
0395     else
0396         duration = jiffies_to_msecs(udev->active_duration);
0397     return sysfs_emit(buf, "%u\n", duration);
0398 }
0399 static DEVICE_ATTR_RO(active_duration);
0400 
0401 static ssize_t autosuspend_show(struct device *dev,
0402                 struct device_attribute *attr, char *buf)
0403 {
0404     return sysfs_emit(buf, "%d\n", dev->power.autosuspend_delay / 1000);
0405 }
0406 
0407 static ssize_t autosuspend_store(struct device *dev,
0408                  struct device_attribute *attr, const char *buf,
0409                  size_t count)
0410 {
0411     int value;
0412 
0413     if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/1000 ||
0414             value <= -INT_MAX/1000)
0415         return -EINVAL;
0416 
0417     pm_runtime_set_autosuspend_delay(dev, value * 1000);
0418     return count;
0419 }
0420 static DEVICE_ATTR_RW(autosuspend);
0421 
0422 static const char on_string[] = "on";
0423 static const char auto_string[] = "auto";
0424 
0425 static void warn_level(void)
0426 {
0427     static int level_warned;
0428 
0429     if (!level_warned) {
0430         level_warned = 1;
0431         printk(KERN_WARNING "WARNING! power/level is deprecated; "
0432                 "use power/control instead\n");
0433     }
0434 }
0435 
0436 static ssize_t level_show(struct device *dev, struct device_attribute *attr,
0437               char *buf)
0438 {
0439     struct usb_device *udev = to_usb_device(dev);
0440     const char *p = auto_string;
0441 
0442     warn_level();
0443     if (udev->state != USB_STATE_SUSPENDED && !udev->dev.power.runtime_auto)
0444         p = on_string;
0445     return sysfs_emit(buf, "%s\n", p);
0446 }
0447 
0448 static ssize_t level_store(struct device *dev, struct device_attribute *attr,
0449                const char *buf, size_t count)
0450 {
0451     struct usb_device *udev = to_usb_device(dev);
0452     int len = count;
0453     char *cp;
0454     int rc = count;
0455     int rv;
0456 
0457     warn_level();
0458     cp = memchr(buf, '\n', count);
0459     if (cp)
0460         len = cp - buf;
0461 
0462     rv = usb_lock_device_interruptible(udev);
0463     if (rv < 0)
0464         return -EINTR;
0465 
0466     if (len == sizeof on_string - 1 &&
0467             strncmp(buf, on_string, len) == 0)
0468         usb_disable_autosuspend(udev);
0469 
0470     else if (len == sizeof auto_string - 1 &&
0471             strncmp(buf, auto_string, len) == 0)
0472         usb_enable_autosuspend(udev);
0473 
0474     else
0475         rc = -EINVAL;
0476 
0477     usb_unlock_device(udev);
0478     return rc;
0479 }
0480 static DEVICE_ATTR_RW(level);
0481 
0482 static ssize_t usb2_hardware_lpm_show(struct device *dev,
0483                       struct device_attribute *attr, char *buf)
0484 {
0485     struct usb_device *udev = to_usb_device(dev);
0486     const char *p;
0487 
0488     if (udev->usb2_hw_lpm_allowed == 1)
0489         p = "enabled";
0490     else
0491         p = "disabled";
0492 
0493     return sysfs_emit(buf, "%s\n", p);
0494 }
0495 
0496 static ssize_t usb2_hardware_lpm_store(struct device *dev,
0497                        struct device_attribute *attr,
0498                        const char *buf, size_t count)
0499 {
0500     struct usb_device *udev = to_usb_device(dev);
0501     bool value;
0502     int ret;
0503 
0504     ret = usb_lock_device_interruptible(udev);
0505     if (ret < 0)
0506         return -EINTR;
0507 
0508     ret = strtobool(buf, &value);
0509 
0510     if (!ret) {
0511         udev->usb2_hw_lpm_allowed = value;
0512         if (value)
0513             ret = usb_enable_usb2_hardware_lpm(udev);
0514         else
0515             ret = usb_disable_usb2_hardware_lpm(udev);
0516     }
0517 
0518     usb_unlock_device(udev);
0519 
0520     if (!ret)
0521         return count;
0522 
0523     return ret;
0524 }
0525 static DEVICE_ATTR_RW(usb2_hardware_lpm);
0526 
0527 static ssize_t usb2_lpm_l1_timeout_show(struct device *dev,
0528                     struct device_attribute *attr,
0529                     char *buf)
0530 {
0531     struct usb_device *udev = to_usb_device(dev);
0532     return sysfs_emit(buf, "%d\n", udev->l1_params.timeout);
0533 }
0534 
0535 static ssize_t usb2_lpm_l1_timeout_store(struct device *dev,
0536                      struct device_attribute *attr,
0537                      const char *buf, size_t count)
0538 {
0539     struct usb_device *udev = to_usb_device(dev);
0540     u16 timeout;
0541 
0542     if (kstrtou16(buf, 0, &timeout))
0543         return -EINVAL;
0544 
0545     udev->l1_params.timeout = timeout;
0546 
0547     return count;
0548 }
0549 static DEVICE_ATTR_RW(usb2_lpm_l1_timeout);
0550 
0551 static ssize_t usb2_lpm_besl_show(struct device *dev,
0552                   struct device_attribute *attr, char *buf)
0553 {
0554     struct usb_device *udev = to_usb_device(dev);
0555     return sysfs_emit(buf, "%d\n", udev->l1_params.besl);
0556 }
0557 
0558 static ssize_t usb2_lpm_besl_store(struct device *dev,
0559                    struct device_attribute *attr,
0560                    const char *buf, size_t count)
0561 {
0562     struct usb_device *udev = to_usb_device(dev);
0563     u8 besl;
0564 
0565     if (kstrtou8(buf, 0, &besl) || besl > 15)
0566         return -EINVAL;
0567 
0568     udev->l1_params.besl = besl;
0569 
0570     return count;
0571 }
0572 static DEVICE_ATTR_RW(usb2_lpm_besl);
0573 
0574 static ssize_t usb3_hardware_lpm_u1_show(struct device *dev,
0575                       struct device_attribute *attr, char *buf)
0576 {
0577     struct usb_device *udev = to_usb_device(dev);
0578     const char *p;
0579     int rc;
0580 
0581     rc = usb_lock_device_interruptible(udev);
0582     if (rc < 0)
0583         return -EINTR;
0584 
0585     if (udev->usb3_lpm_u1_enabled)
0586         p = "enabled";
0587     else
0588         p = "disabled";
0589 
0590     usb_unlock_device(udev);
0591 
0592     return sysfs_emit(buf, "%s\n", p);
0593 }
0594 static DEVICE_ATTR_RO(usb3_hardware_lpm_u1);
0595 
0596 static ssize_t usb3_hardware_lpm_u2_show(struct device *dev,
0597                       struct device_attribute *attr, char *buf)
0598 {
0599     struct usb_device *udev = to_usb_device(dev);
0600     const char *p;
0601     int rc;
0602 
0603     rc = usb_lock_device_interruptible(udev);
0604     if (rc < 0)
0605         return -EINTR;
0606 
0607     if (udev->usb3_lpm_u2_enabled)
0608         p = "enabled";
0609     else
0610         p = "disabled";
0611 
0612     usb_unlock_device(udev);
0613 
0614     return sysfs_emit(buf, "%s\n", p);
0615 }
0616 static DEVICE_ATTR_RO(usb3_hardware_lpm_u2);
0617 
0618 static struct attribute *usb2_hardware_lpm_attr[] = {
0619     &dev_attr_usb2_hardware_lpm.attr,
0620     &dev_attr_usb2_lpm_l1_timeout.attr,
0621     &dev_attr_usb2_lpm_besl.attr,
0622     NULL,
0623 };
0624 static const struct attribute_group usb2_hardware_lpm_attr_group = {
0625     .name   = power_group_name,
0626     .attrs  = usb2_hardware_lpm_attr,
0627 };
0628 
0629 static struct attribute *usb3_hardware_lpm_attr[] = {
0630     &dev_attr_usb3_hardware_lpm_u1.attr,
0631     &dev_attr_usb3_hardware_lpm_u2.attr,
0632     NULL,
0633 };
0634 static const struct attribute_group usb3_hardware_lpm_attr_group = {
0635     .name   = power_group_name,
0636     .attrs  = usb3_hardware_lpm_attr,
0637 };
0638 
0639 static struct attribute *power_attrs[] = {
0640     &dev_attr_autosuspend.attr,
0641     &dev_attr_level.attr,
0642     &dev_attr_connected_duration.attr,
0643     &dev_attr_active_duration.attr,
0644     NULL,
0645 };
0646 static const struct attribute_group power_attr_group = {
0647     .name   = power_group_name,
0648     .attrs  = power_attrs,
0649 };
0650 
0651 static int add_power_attributes(struct device *dev)
0652 {
0653     int rc = 0;
0654 
0655     if (is_usb_device(dev)) {
0656         struct usb_device *udev = to_usb_device(dev);
0657         rc = sysfs_merge_group(&dev->kobj, &power_attr_group);
0658         if (udev->usb2_hw_lpm_capable == 1)
0659             rc = sysfs_merge_group(&dev->kobj,
0660                     &usb2_hardware_lpm_attr_group);
0661         if ((udev->speed == USB_SPEED_SUPER ||
0662              udev->speed == USB_SPEED_SUPER_PLUS) &&
0663                 udev->lpm_capable == 1)
0664             rc = sysfs_merge_group(&dev->kobj,
0665                     &usb3_hardware_lpm_attr_group);
0666     }
0667 
0668     return rc;
0669 }
0670 
0671 static void remove_power_attributes(struct device *dev)
0672 {
0673     sysfs_unmerge_group(&dev->kobj, &usb2_hardware_lpm_attr_group);
0674     sysfs_unmerge_group(&dev->kobj, &power_attr_group);
0675 }
0676 
0677 #else
0678 
0679 #define add_persist_attributes(dev) 0
0680 #define remove_persist_attributes(dev)  do {} while (0)
0681 
0682 #define add_power_attributes(dev)   0
0683 #define remove_power_attributes(dev)    do {} while (0)
0684 
0685 #endif  /* CONFIG_PM */
0686 
0687 
0688 /* Descriptor fields */
0689 #define usb_descriptor_attr_le16(field, format_string)          \
0690 static ssize_t                              \
0691 field##_show(struct device *dev, struct device_attribute *attr, \
0692         char *buf)                      \
0693 {                                   \
0694     struct usb_device *udev;                    \
0695                                     \
0696     udev = to_usb_device(dev);                  \
0697     return sysfs_emit(buf, format_string,               \
0698             le16_to_cpu(udev->descriptor.field));       \
0699 }                                   \
0700 static DEVICE_ATTR_RO(field)
0701 
0702 usb_descriptor_attr_le16(idVendor, "%04x\n");
0703 usb_descriptor_attr_le16(idProduct, "%04x\n");
0704 usb_descriptor_attr_le16(bcdDevice, "%04x\n");
0705 
0706 #define usb_descriptor_attr(field, format_string)           \
0707 static ssize_t                              \
0708 field##_show(struct device *dev, struct device_attribute *attr, \
0709         char *buf)                      \
0710 {                                   \
0711     struct usb_device *udev;                    \
0712                                     \
0713     udev = to_usb_device(dev);                  \
0714     return sysfs_emit(buf, format_string, udev->descriptor.field);  \
0715 }                                   \
0716 static DEVICE_ATTR_RO(field)
0717 
0718 usb_descriptor_attr(bDeviceClass, "%02x\n");
0719 usb_descriptor_attr(bDeviceSubClass, "%02x\n");
0720 usb_descriptor_attr(bDeviceProtocol, "%02x\n");
0721 usb_descriptor_attr(bNumConfigurations, "%d\n");
0722 usb_descriptor_attr(bMaxPacketSize0, "%d\n");
0723 
0724 
0725 /* show if the device is authorized (1) or not (0) */
0726 static ssize_t authorized_show(struct device *dev,
0727                    struct device_attribute *attr, char *buf)
0728 {
0729     struct usb_device *usb_dev = to_usb_device(dev);
0730     return sysfs_emit(buf, "%u\n", usb_dev->authorized);
0731 }
0732 
0733 /*
0734  * Authorize a device to be used in the system
0735  *
0736  * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
0737  */
0738 static ssize_t authorized_store(struct device *dev,
0739                 struct device_attribute *attr, const char *buf,
0740                 size_t size)
0741 {
0742     ssize_t result;
0743     struct usb_device *usb_dev = to_usb_device(dev);
0744     unsigned val;
0745     result = sscanf(buf, "%u\n", &val);
0746     if (result != 1)
0747         result = -EINVAL;
0748     else if (val == 0)
0749         result = usb_deauthorize_device(usb_dev);
0750     else
0751         result = usb_authorize_device(usb_dev);
0752     return result < 0 ? result : size;
0753 }
0754 static DEVICE_ATTR_IGNORE_LOCKDEP(authorized, S_IRUGO | S_IWUSR,
0755                   authorized_show, authorized_store);
0756 
0757 /* "Safely remove a device" */
0758 static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
0759                 const char *buf, size_t count)
0760 {
0761     struct usb_device *udev = to_usb_device(dev);
0762     int rc = 0;
0763 
0764     usb_lock_device(udev);
0765     if (udev->state != USB_STATE_NOTATTACHED) {
0766 
0767         /* To avoid races, first unconfigure and then remove */
0768         usb_set_configuration(udev, -1);
0769         rc = usb_remove_device(udev);
0770     }
0771     if (rc == 0)
0772         rc = count;
0773     usb_unlock_device(udev);
0774     return rc;
0775 }
0776 static DEVICE_ATTR_IGNORE_LOCKDEP(remove, S_IWUSR, NULL, remove_store);
0777 
0778 
0779 static struct attribute *dev_attrs[] = {
0780     /* current configuration's attributes */
0781     &dev_attr_configuration.attr,
0782     &dev_attr_bNumInterfaces.attr,
0783     &dev_attr_bConfigurationValue.attr,
0784     &dev_attr_bmAttributes.attr,
0785     &dev_attr_bMaxPower.attr,
0786     /* device attributes */
0787     &dev_attr_urbnum.attr,
0788     &dev_attr_idVendor.attr,
0789     &dev_attr_idProduct.attr,
0790     &dev_attr_bcdDevice.attr,
0791     &dev_attr_bDeviceClass.attr,
0792     &dev_attr_bDeviceSubClass.attr,
0793     &dev_attr_bDeviceProtocol.attr,
0794     &dev_attr_bNumConfigurations.attr,
0795     &dev_attr_bMaxPacketSize0.attr,
0796     &dev_attr_speed.attr,
0797     &dev_attr_rx_lanes.attr,
0798     &dev_attr_tx_lanes.attr,
0799     &dev_attr_busnum.attr,
0800     &dev_attr_devnum.attr,
0801     &dev_attr_devpath.attr,
0802     &dev_attr_version.attr,
0803     &dev_attr_maxchild.attr,
0804     &dev_attr_quirks.attr,
0805     &dev_attr_avoid_reset_quirk.attr,
0806     &dev_attr_authorized.attr,
0807     &dev_attr_remove.attr,
0808     &dev_attr_ltm_capable.attr,
0809 #ifdef CONFIG_OF
0810     &dev_attr_devspec.attr,
0811 #endif
0812     NULL,
0813 };
0814 static const struct attribute_group dev_attr_grp = {
0815     .attrs = dev_attrs,
0816 };
0817 
0818 /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
0819  * accordingly.
0820  */
0821 static struct attribute *dev_string_attrs[] = {
0822     &dev_attr_manufacturer.attr,
0823     &dev_attr_product.attr,
0824     &dev_attr_serial.attr,
0825     NULL
0826 };
0827 
0828 static umode_t dev_string_attrs_are_visible(struct kobject *kobj,
0829         struct attribute *a, int n)
0830 {
0831     struct device *dev = kobj_to_dev(kobj);
0832     struct usb_device *udev = to_usb_device(dev);
0833 
0834     if (a == &dev_attr_manufacturer.attr) {
0835         if (udev->manufacturer == NULL)
0836             return 0;
0837     } else if (a == &dev_attr_product.attr) {
0838         if (udev->product == NULL)
0839             return 0;
0840     } else if (a == &dev_attr_serial.attr) {
0841         if (udev->serial == NULL)
0842             return 0;
0843     }
0844     return a->mode;
0845 }
0846 
0847 static const struct attribute_group dev_string_attr_grp = {
0848     .attrs =    dev_string_attrs,
0849     .is_visible =   dev_string_attrs_are_visible,
0850 };
0851 
0852 const struct attribute_group *usb_device_groups[] = {
0853     &dev_attr_grp,
0854     &dev_string_attr_grp,
0855     NULL
0856 };
0857 
0858 /* Binary descriptors */
0859 
0860 static ssize_t
0861 read_descriptors(struct file *filp, struct kobject *kobj,
0862         struct bin_attribute *attr,
0863         char *buf, loff_t off, size_t count)
0864 {
0865     struct device *dev = kobj_to_dev(kobj);
0866     struct usb_device *udev = to_usb_device(dev);
0867     size_t nleft = count;
0868     size_t srclen, n;
0869     int cfgno;
0870     void *src;
0871     int retval;
0872 
0873     retval = usb_lock_device_interruptible(udev);
0874     if (retval < 0)
0875         return -EINTR;
0876     /* The binary attribute begins with the device descriptor.
0877      * Following that are the raw descriptor entries for all the
0878      * configurations (config plus subsidiary descriptors).
0879      */
0880     for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
0881             nleft > 0; ++cfgno) {
0882         if (cfgno < 0) {
0883             src = &udev->descriptor;
0884             srclen = sizeof(struct usb_device_descriptor);
0885         } else {
0886             src = udev->rawdescriptors[cfgno];
0887             srclen = __le16_to_cpu(udev->config[cfgno].desc.
0888                     wTotalLength);
0889         }
0890         if (off < srclen) {
0891             n = min(nleft, srclen - (size_t) off);
0892             memcpy(buf, src + off, n);
0893             nleft -= n;
0894             buf += n;
0895             off = 0;
0896         } else {
0897             off -= srclen;
0898         }
0899     }
0900     usb_unlock_device(udev);
0901     return count - nleft;
0902 }
0903 
0904 static struct bin_attribute dev_bin_attr_descriptors = {
0905     .attr = {.name = "descriptors", .mode = 0444},
0906     .read = read_descriptors,
0907     .size = 18 + 65535, /* dev descr + max-size raw descriptor */
0908 };
0909 
0910 /*
0911  * Show & store the current value of authorized_default
0912  */
0913 static ssize_t authorized_default_show(struct device *dev,
0914                        struct device_attribute *attr, char *buf)
0915 {
0916     struct usb_device *rh_usb_dev = to_usb_device(dev);
0917     struct usb_bus *usb_bus = rh_usb_dev->bus;
0918     struct usb_hcd *hcd;
0919 
0920     hcd = bus_to_hcd(usb_bus);
0921     return sysfs_emit(buf, "%u\n", hcd->dev_policy);
0922 }
0923 
0924 static ssize_t authorized_default_store(struct device *dev,
0925                     struct device_attribute *attr,
0926                     const char *buf, size_t size)
0927 {
0928     ssize_t result;
0929     unsigned int val;
0930     struct usb_device *rh_usb_dev = to_usb_device(dev);
0931     struct usb_bus *usb_bus = rh_usb_dev->bus;
0932     struct usb_hcd *hcd;
0933 
0934     hcd = bus_to_hcd(usb_bus);
0935     result = sscanf(buf, "%u\n", &val);
0936     if (result == 1) {
0937         hcd->dev_policy = val <= USB_DEVICE_AUTHORIZE_INTERNAL ?
0938             val : USB_DEVICE_AUTHORIZE_ALL;
0939         result = size;
0940     } else {
0941         result = -EINVAL;
0942     }
0943     return result;
0944 }
0945 static DEVICE_ATTR_RW(authorized_default);
0946 
0947 /*
0948  * interface_authorized_default_show - show default authorization status
0949  * for USB interfaces
0950  *
0951  * note: interface_authorized_default is the default value
0952  *       for initializing the authorized attribute of interfaces
0953  */
0954 static ssize_t interface_authorized_default_show(struct device *dev,
0955         struct device_attribute *attr, char *buf)
0956 {
0957     struct usb_device *usb_dev = to_usb_device(dev);
0958     struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus);
0959 
0960     return sysfs_emit(buf, "%u\n", !!HCD_INTF_AUTHORIZED(hcd));
0961 }
0962 
0963 /*
0964  * interface_authorized_default_store - store default authorization status
0965  * for USB interfaces
0966  *
0967  * note: interface_authorized_default is the default value
0968  *       for initializing the authorized attribute of interfaces
0969  */
0970 static ssize_t interface_authorized_default_store(struct device *dev,
0971         struct device_attribute *attr, const char *buf, size_t count)
0972 {
0973     struct usb_device *usb_dev = to_usb_device(dev);
0974     struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus);
0975     int rc = count;
0976     bool val;
0977 
0978     if (strtobool(buf, &val) != 0)
0979         return -EINVAL;
0980 
0981     if (val)
0982         set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags);
0983     else
0984         clear_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags);
0985 
0986     return rc;
0987 }
0988 static DEVICE_ATTR_RW(interface_authorized_default);
0989 
0990 /* Group all the USB bus attributes */
0991 static struct attribute *usb_bus_attrs[] = {
0992         &dev_attr_authorized_default.attr,
0993         &dev_attr_interface_authorized_default.attr,
0994         NULL,
0995 };
0996 
0997 static const struct attribute_group usb_bus_attr_group = {
0998     .name = NULL,   /* we want them in the same directory */
0999     .attrs = usb_bus_attrs,
1000 };
1001 
1002 
1003 static int add_default_authorized_attributes(struct device *dev)
1004 {
1005     int rc = 0;
1006 
1007     if (is_usb_device(dev))
1008         rc = sysfs_create_group(&dev->kobj, &usb_bus_attr_group);
1009 
1010     return rc;
1011 }
1012 
1013 static void remove_default_authorized_attributes(struct device *dev)
1014 {
1015     if (is_usb_device(dev)) {
1016         sysfs_remove_group(&dev->kobj, &usb_bus_attr_group);
1017     }
1018 }
1019 
1020 int usb_create_sysfs_dev_files(struct usb_device *udev)
1021 {
1022     struct device *dev = &udev->dev;
1023     int retval;
1024 
1025     retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
1026     if (retval)
1027         goto error;
1028 
1029     retval = add_persist_attributes(dev);
1030     if (retval)
1031         goto error;
1032 
1033     retval = add_power_attributes(dev);
1034     if (retval)
1035         goto error;
1036 
1037     if (is_root_hub(udev)) {
1038         retval = add_default_authorized_attributes(dev);
1039         if (retval)
1040             goto error;
1041     }
1042     return retval;
1043 
1044 error:
1045     usb_remove_sysfs_dev_files(udev);
1046     return retval;
1047 }
1048 
1049 void usb_remove_sysfs_dev_files(struct usb_device *udev)
1050 {
1051     struct device *dev = &udev->dev;
1052 
1053     if (is_root_hub(udev))
1054         remove_default_authorized_attributes(dev);
1055 
1056     remove_power_attributes(dev);
1057     remove_persist_attributes(dev);
1058     device_remove_bin_file(dev, &dev_bin_attr_descriptors);
1059 }
1060 
1061 /* Interface Association Descriptor fields */
1062 #define usb_intf_assoc_attr(field, format_string)           \
1063 static ssize_t                              \
1064 iad_##field##_show(struct device *dev, struct device_attribute *attr,   \
1065         char *buf)                      \
1066 {                                   \
1067     struct usb_interface *intf = to_usb_interface(dev);     \
1068                                     \
1069     return sysfs_emit(buf, format_string,               \
1070             intf->intf_assoc->field);           \
1071 }                                   \
1072 static DEVICE_ATTR_RO(iad_##field)
1073 
1074 usb_intf_assoc_attr(bFirstInterface, "%02x\n");
1075 usb_intf_assoc_attr(bInterfaceCount, "%02d\n");
1076 usb_intf_assoc_attr(bFunctionClass, "%02x\n");
1077 usb_intf_assoc_attr(bFunctionSubClass, "%02x\n");
1078 usb_intf_assoc_attr(bFunctionProtocol, "%02x\n");
1079 
1080 /* Interface fields */
1081 #define usb_intf_attr(field, format_string)             \
1082 static ssize_t                              \
1083 field##_show(struct device *dev, struct device_attribute *attr,     \
1084         char *buf)                      \
1085 {                                   \
1086     struct usb_interface *intf = to_usb_interface(dev);     \
1087                                     \
1088     return sysfs_emit(buf, format_string,               \
1089             intf->cur_altsetting->desc.field);      \
1090 }                                   \
1091 static DEVICE_ATTR_RO(field)
1092 
1093 usb_intf_attr(bInterfaceNumber, "%02x\n");
1094 usb_intf_attr(bAlternateSetting, "%2d\n");
1095 usb_intf_attr(bNumEndpoints, "%02x\n");
1096 usb_intf_attr(bInterfaceClass, "%02x\n");
1097 usb_intf_attr(bInterfaceSubClass, "%02x\n");
1098 usb_intf_attr(bInterfaceProtocol, "%02x\n");
1099 
1100 static ssize_t interface_show(struct device *dev, struct device_attribute *attr,
1101                   char *buf)
1102 {
1103     struct usb_interface *intf;
1104     char *string;
1105 
1106     intf = to_usb_interface(dev);
1107     string = READ_ONCE(intf->cur_altsetting->string);
1108     if (!string)
1109         return 0;
1110     return sysfs_emit(buf, "%s\n", string);
1111 }
1112 static DEVICE_ATTR_RO(interface);
1113 
1114 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
1115                  char *buf)
1116 {
1117     struct usb_interface *intf;
1118     struct usb_device *udev;
1119     struct usb_host_interface *alt;
1120 
1121     intf = to_usb_interface(dev);
1122     udev = interface_to_usbdev(intf);
1123     alt = READ_ONCE(intf->cur_altsetting);
1124 
1125     return sysfs_emit(buf,
1126             "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
1127             "ic%02Xisc%02Xip%02Xin%02X\n",
1128             le16_to_cpu(udev->descriptor.idVendor),
1129             le16_to_cpu(udev->descriptor.idProduct),
1130             le16_to_cpu(udev->descriptor.bcdDevice),
1131             udev->descriptor.bDeviceClass,
1132             udev->descriptor.bDeviceSubClass,
1133             udev->descriptor.bDeviceProtocol,
1134             alt->desc.bInterfaceClass,
1135             alt->desc.bInterfaceSubClass,
1136             alt->desc.bInterfaceProtocol,
1137             alt->desc.bInterfaceNumber);
1138 }
1139 static DEVICE_ATTR_RO(modalias);
1140 
1141 static ssize_t supports_autosuspend_show(struct device *dev,
1142                      struct device_attribute *attr,
1143                      char *buf)
1144 {
1145     int s;
1146 
1147     s = device_lock_interruptible(dev);
1148     if (s < 0)
1149         return -EINTR;
1150     /* Devices will be autosuspended even when an interface isn't claimed */
1151     s = (!dev->driver || to_usb_driver(dev->driver)->supports_autosuspend);
1152     device_unlock(dev);
1153 
1154     return sysfs_emit(buf, "%u\n", s);
1155 }
1156 static DEVICE_ATTR_RO(supports_autosuspend);
1157 
1158 /*
1159  * interface_authorized_show - show authorization status of an USB interface
1160  * 1 is authorized, 0 is deauthorized
1161  */
1162 static ssize_t interface_authorized_show(struct device *dev,
1163         struct device_attribute *attr, char *buf)
1164 {
1165     struct usb_interface *intf = to_usb_interface(dev);
1166 
1167     return sysfs_emit(buf, "%u\n", intf->authorized);
1168 }
1169 
1170 /*
1171  * interface_authorized_store - authorize or deauthorize an USB interface
1172  */
1173 static ssize_t interface_authorized_store(struct device *dev,
1174         struct device_attribute *attr, const char *buf, size_t count)
1175 {
1176     struct usb_interface *intf = to_usb_interface(dev);
1177     bool val;
1178 
1179     if (strtobool(buf, &val) != 0)
1180         return -EINVAL;
1181 
1182     if (val)
1183         usb_authorize_interface(intf);
1184     else
1185         usb_deauthorize_interface(intf);
1186 
1187     return count;
1188 }
1189 static struct device_attribute dev_attr_interface_authorized =
1190         __ATTR(authorized, S_IRUGO | S_IWUSR,
1191         interface_authorized_show, interface_authorized_store);
1192 
1193 static struct attribute *intf_attrs[] = {
1194     &dev_attr_bInterfaceNumber.attr,
1195     &dev_attr_bAlternateSetting.attr,
1196     &dev_attr_bNumEndpoints.attr,
1197     &dev_attr_bInterfaceClass.attr,
1198     &dev_attr_bInterfaceSubClass.attr,
1199     &dev_attr_bInterfaceProtocol.attr,
1200     &dev_attr_modalias.attr,
1201     &dev_attr_supports_autosuspend.attr,
1202     &dev_attr_interface_authorized.attr,
1203     NULL,
1204 };
1205 static const struct attribute_group intf_attr_grp = {
1206     .attrs = intf_attrs,
1207 };
1208 
1209 static struct attribute *intf_assoc_attrs[] = {
1210     &dev_attr_iad_bFirstInterface.attr,
1211     &dev_attr_iad_bInterfaceCount.attr,
1212     &dev_attr_iad_bFunctionClass.attr,
1213     &dev_attr_iad_bFunctionSubClass.attr,
1214     &dev_attr_iad_bFunctionProtocol.attr,
1215     NULL,
1216 };
1217 
1218 static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
1219         struct attribute *a, int n)
1220 {
1221     struct device *dev = kobj_to_dev(kobj);
1222     struct usb_interface *intf = to_usb_interface(dev);
1223 
1224     if (intf->intf_assoc == NULL)
1225         return 0;
1226     return a->mode;
1227 }
1228 
1229 static const struct attribute_group intf_assoc_attr_grp = {
1230     .attrs =    intf_assoc_attrs,
1231     .is_visible =   intf_assoc_attrs_are_visible,
1232 };
1233 
1234 const struct attribute_group *usb_interface_groups[] = {
1235     &intf_attr_grp,
1236     &intf_assoc_attr_grp,
1237     NULL
1238 };
1239 
1240 void usb_create_sysfs_intf_files(struct usb_interface *intf)
1241 {
1242     struct usb_device *udev = interface_to_usbdev(intf);
1243     struct usb_host_interface *alt = intf->cur_altsetting;
1244 
1245     if (intf->sysfs_files_created || intf->unregistering)
1246         return;
1247 
1248     if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
1249         alt->string = usb_cache_string(udev, alt->desc.iInterface);
1250     if (alt->string && device_create_file(&intf->dev, &dev_attr_interface)) {
1251         /* This is not a serious error */
1252         dev_dbg(&intf->dev, "interface string descriptor file not created\n");
1253     }
1254     intf->sysfs_files_created = 1;
1255 }
1256 
1257 void usb_remove_sysfs_intf_files(struct usb_interface *intf)
1258 {
1259     if (!intf->sysfs_files_created)
1260         return;
1261 
1262     device_remove_file(&intf->dev, &dev_attr_interface);
1263     intf->sysfs_files_created = 0;
1264 }