Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * usb port device code
0004  *
0005  * Copyright (C) 2012 Intel Corp
0006  *
0007  * Author: Lan Tianyu <tianyu.lan@intel.com>
0008  */
0009 
0010 #include <linux/slab.h>
0011 #include <linux/pm_qos.h>
0012 #include <linux/component.h>
0013 
0014 #include "hub.h"
0015 
0016 static int usb_port_block_power_off;
0017 
0018 static const struct attribute_group *port_dev_group[];
0019 
0020 static ssize_t disable_show(struct device *dev,
0021                   struct device_attribute *attr, char *buf)
0022 {
0023     struct usb_port *port_dev = to_usb_port(dev);
0024     struct usb_device *hdev = to_usb_device(dev->parent->parent);
0025     struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
0026     struct usb_interface *intf = to_usb_interface(hub->intfdev);
0027     int port1 = port_dev->portnum;
0028     u16 portstatus, unused;
0029     bool disabled;
0030     int rc;
0031 
0032     rc = usb_autopm_get_interface(intf);
0033     if (rc < 0)
0034         return rc;
0035 
0036     usb_lock_device(hdev);
0037     if (hub->disconnected) {
0038         rc = -ENODEV;
0039         goto out_hdev_lock;
0040     }
0041 
0042     usb_hub_port_status(hub, port1, &portstatus, &unused);
0043     disabled = !usb_port_is_power_on(hub, portstatus);
0044 
0045 out_hdev_lock:
0046     usb_unlock_device(hdev);
0047     usb_autopm_put_interface(intf);
0048 
0049     if (rc)
0050         return rc;
0051 
0052     return sysfs_emit(buf, "%s\n", disabled ? "1" : "0");
0053 }
0054 
0055 static ssize_t disable_store(struct device *dev, struct device_attribute *attr,
0056                 const char *buf, size_t count)
0057 {
0058     struct usb_port *port_dev = to_usb_port(dev);
0059     struct usb_device *hdev = to_usb_device(dev->parent->parent);
0060     struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
0061     struct usb_interface *intf = to_usb_interface(hub->intfdev);
0062     int port1 = port_dev->portnum;
0063     bool disabled;
0064     int rc;
0065 
0066     rc = strtobool(buf, &disabled);
0067     if (rc)
0068         return rc;
0069 
0070     rc = usb_autopm_get_interface(intf);
0071     if (rc < 0)
0072         return rc;
0073 
0074     usb_lock_device(hdev);
0075     if (hub->disconnected) {
0076         rc = -ENODEV;
0077         goto out_hdev_lock;
0078     }
0079 
0080     if (disabled && port_dev->child)
0081         usb_disconnect(&port_dev->child);
0082 
0083     rc = usb_hub_set_port_power(hdev, hub, port1, !disabled);
0084 
0085     if (disabled) {
0086         usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
0087         if (!port_dev->is_superspeed)
0088             usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
0089     }
0090 
0091     if (!rc)
0092         rc = count;
0093 
0094 out_hdev_lock:
0095     usb_unlock_device(hdev);
0096     usb_autopm_put_interface(intf);
0097 
0098     return rc;
0099 }
0100 static DEVICE_ATTR_RW(disable);
0101 
0102 static ssize_t location_show(struct device *dev,
0103                  struct device_attribute *attr, char *buf)
0104 {
0105     struct usb_port *port_dev = to_usb_port(dev);
0106 
0107     return sprintf(buf, "0x%08x\n", port_dev->location);
0108 }
0109 static DEVICE_ATTR_RO(location);
0110 
0111 static ssize_t connect_type_show(struct device *dev,
0112                  struct device_attribute *attr, char *buf)
0113 {
0114     struct usb_port *port_dev = to_usb_port(dev);
0115     char *result;
0116 
0117     switch (port_dev->connect_type) {
0118     case USB_PORT_CONNECT_TYPE_HOT_PLUG:
0119         result = "hotplug";
0120         break;
0121     case USB_PORT_CONNECT_TYPE_HARD_WIRED:
0122         result = "hardwired";
0123         break;
0124     case USB_PORT_NOT_USED:
0125         result = "not used";
0126         break;
0127     default:
0128         result = "unknown";
0129         break;
0130     }
0131 
0132     return sprintf(buf, "%s\n", result);
0133 }
0134 static DEVICE_ATTR_RO(connect_type);
0135 
0136 static ssize_t over_current_count_show(struct device *dev,
0137                        struct device_attribute *attr, char *buf)
0138 {
0139     struct usb_port *port_dev = to_usb_port(dev);
0140 
0141     return sprintf(buf, "%u\n", port_dev->over_current_count);
0142 }
0143 static DEVICE_ATTR_RO(over_current_count);
0144 
0145 static ssize_t quirks_show(struct device *dev,
0146                struct device_attribute *attr, char *buf)
0147 {
0148     struct usb_port *port_dev = to_usb_port(dev);
0149 
0150     return sprintf(buf, "%08x\n", port_dev->quirks);
0151 }
0152 
0153 static ssize_t quirks_store(struct device *dev, struct device_attribute *attr,
0154                 const char *buf, size_t count)
0155 {
0156     struct usb_port *port_dev = to_usb_port(dev);
0157     u32 value;
0158 
0159     if (kstrtou32(buf, 16, &value))
0160         return -EINVAL;
0161 
0162     port_dev->quirks = value;
0163     return count;
0164 }
0165 static DEVICE_ATTR_RW(quirks);
0166 
0167 static ssize_t usb3_lpm_permit_show(struct device *dev,
0168                   struct device_attribute *attr, char *buf)
0169 {
0170     struct usb_port *port_dev = to_usb_port(dev);
0171     const char *p;
0172 
0173     if (port_dev->usb3_lpm_u1_permit) {
0174         if (port_dev->usb3_lpm_u2_permit)
0175             p = "u1_u2";
0176         else
0177             p = "u1";
0178     } else {
0179         if (port_dev->usb3_lpm_u2_permit)
0180             p = "u2";
0181         else
0182             p = "0";
0183     }
0184 
0185     return sprintf(buf, "%s\n", p);
0186 }
0187 
0188 static ssize_t usb3_lpm_permit_store(struct device *dev,
0189                    struct device_attribute *attr,
0190                    const char *buf, size_t count)
0191 {
0192     struct usb_port *port_dev = to_usb_port(dev);
0193     struct usb_device *udev = port_dev->child;
0194     struct usb_hcd *hcd;
0195 
0196     if (!strncmp(buf, "u1_u2", 5)) {
0197         port_dev->usb3_lpm_u1_permit = 1;
0198         port_dev->usb3_lpm_u2_permit = 1;
0199 
0200     } else if (!strncmp(buf, "u1", 2)) {
0201         port_dev->usb3_lpm_u1_permit = 1;
0202         port_dev->usb3_lpm_u2_permit = 0;
0203 
0204     } else if (!strncmp(buf, "u2", 2)) {
0205         port_dev->usb3_lpm_u1_permit = 0;
0206         port_dev->usb3_lpm_u2_permit = 1;
0207 
0208     } else if (!strncmp(buf, "0", 1)) {
0209         port_dev->usb3_lpm_u1_permit = 0;
0210         port_dev->usb3_lpm_u2_permit = 0;
0211     } else
0212         return -EINVAL;
0213 
0214     /* If device is connected to the port, disable or enable lpm
0215      * to make new u1 u2 setting take effect immediately.
0216      */
0217     if (udev) {
0218         hcd = bus_to_hcd(udev->bus);
0219         if (!hcd)
0220             return -EINVAL;
0221         usb_lock_device(udev);
0222         mutex_lock(hcd->bandwidth_mutex);
0223         if (!usb_disable_lpm(udev))
0224             usb_enable_lpm(udev);
0225         mutex_unlock(hcd->bandwidth_mutex);
0226         usb_unlock_device(udev);
0227     }
0228 
0229     return count;
0230 }
0231 static DEVICE_ATTR_RW(usb3_lpm_permit);
0232 
0233 static struct attribute *port_dev_attrs[] = {
0234     &dev_attr_connect_type.attr,
0235     &dev_attr_location.attr,
0236     &dev_attr_quirks.attr,
0237     &dev_attr_over_current_count.attr,
0238     &dev_attr_disable.attr,
0239     NULL,
0240 };
0241 
0242 static const struct attribute_group port_dev_attr_grp = {
0243     .attrs = port_dev_attrs,
0244 };
0245 
0246 static const struct attribute_group *port_dev_group[] = {
0247     &port_dev_attr_grp,
0248     NULL,
0249 };
0250 
0251 static struct attribute *port_dev_usb3_attrs[] = {
0252     &dev_attr_usb3_lpm_permit.attr,
0253     NULL,
0254 };
0255 
0256 static const struct attribute_group port_dev_usb3_attr_grp = {
0257     .attrs = port_dev_usb3_attrs,
0258 };
0259 
0260 static const struct attribute_group *port_dev_usb3_group[] = {
0261     &port_dev_attr_grp,
0262     &port_dev_usb3_attr_grp,
0263     NULL,
0264 };
0265 
0266 static void usb_port_device_release(struct device *dev)
0267 {
0268     struct usb_port *port_dev = to_usb_port(dev);
0269 
0270     kfree(port_dev->req);
0271     kfree(port_dev);
0272 }
0273 
0274 #ifdef CONFIG_PM
0275 static int usb_port_runtime_resume(struct device *dev)
0276 {
0277     struct usb_port *port_dev = to_usb_port(dev);
0278     struct usb_device *hdev = to_usb_device(dev->parent->parent);
0279     struct usb_interface *intf = to_usb_interface(dev->parent);
0280     struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
0281     struct usb_device *udev = port_dev->child;
0282     struct usb_port *peer = port_dev->peer;
0283     int port1 = port_dev->portnum;
0284     int retval;
0285 
0286     if (!hub)
0287         return -EINVAL;
0288     if (hub->in_reset) {
0289         set_bit(port1, hub->power_bits);
0290         return 0;
0291     }
0292 
0293     /*
0294      * Power on our usb3 peer before this usb2 port to prevent a usb3
0295      * device from degrading to its usb2 connection
0296      */
0297     if (!port_dev->is_superspeed && peer)
0298         pm_runtime_get_sync(&peer->dev);
0299 
0300     retval = usb_autopm_get_interface(intf);
0301     if (retval < 0)
0302         return retval;
0303 
0304     retval = usb_hub_set_port_power(hdev, hub, port1, true);
0305     msleep(hub_power_on_good_delay(hub));
0306     if (udev && !retval) {
0307         /*
0308          * Our preference is to simply wait for the port to reconnect,
0309          * as that is the lowest latency method to restart the port.
0310          * However, there are cases where toggling port power results in
0311          * the host port and the device port getting out of sync causing
0312          * a link training live lock.  Upon timeout, flag the port as
0313          * needing warm reset recovery (to be performed later by
0314          * usb_port_resume() as requested via usb_wakeup_notification())
0315          */
0316         if (hub_port_debounce_be_connected(hub, port1) < 0) {
0317             dev_dbg(&port_dev->dev, "reconnect timeout\n");
0318             if (hub_is_superspeed(hdev))
0319                 set_bit(port1, hub->warm_reset_bits);
0320         }
0321 
0322         /* Force the child awake to revalidate after the power loss. */
0323         if (!test_and_set_bit(port1, hub->child_usage_bits)) {
0324             pm_runtime_get_noresume(&port_dev->dev);
0325             pm_request_resume(&udev->dev);
0326         }
0327     }
0328 
0329     usb_autopm_put_interface(intf);
0330 
0331     return retval;
0332 }
0333 
0334 static int usb_port_runtime_suspend(struct device *dev)
0335 {
0336     struct usb_port *port_dev = to_usb_port(dev);
0337     struct usb_device *hdev = to_usb_device(dev->parent->parent);
0338     struct usb_interface *intf = to_usb_interface(dev->parent);
0339     struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
0340     struct usb_port *peer = port_dev->peer;
0341     int port1 = port_dev->portnum;
0342     int retval;
0343 
0344     if (!hub)
0345         return -EINVAL;
0346     if (hub->in_reset)
0347         return -EBUSY;
0348 
0349     if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
0350             == PM_QOS_FLAGS_ALL)
0351         return -EAGAIN;
0352 
0353     if (usb_port_block_power_off)
0354         return -EBUSY;
0355 
0356     retval = usb_autopm_get_interface(intf);
0357     if (retval < 0)
0358         return retval;
0359 
0360     retval = usb_hub_set_port_power(hdev, hub, port1, false);
0361     usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
0362     if (!port_dev->is_superspeed)
0363         usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
0364     usb_autopm_put_interface(intf);
0365 
0366     /*
0367      * Our peer usb3 port may now be able to suspend, so
0368      * asynchronously queue a suspend request to observe that this
0369      * usb2 port is now off.
0370      */
0371     if (!port_dev->is_superspeed && peer)
0372         pm_runtime_put(&peer->dev);
0373 
0374     return retval;
0375 }
0376 #endif
0377 
0378 static void usb_port_shutdown(struct device *dev)
0379 {
0380     struct usb_port *port_dev = to_usb_port(dev);
0381 
0382     if (port_dev->child)
0383         usb_disable_usb2_hardware_lpm(port_dev->child);
0384 }
0385 
0386 static const struct dev_pm_ops usb_port_pm_ops = {
0387 #ifdef CONFIG_PM
0388     .runtime_suspend =  usb_port_runtime_suspend,
0389     .runtime_resume =   usb_port_runtime_resume,
0390 #endif
0391 };
0392 
0393 struct device_type usb_port_device_type = {
0394     .name =     "usb_port",
0395     .release =  usb_port_device_release,
0396     .pm =       &usb_port_pm_ops,
0397 };
0398 
0399 static struct device_driver usb_port_driver = {
0400     .name = "usb",
0401     .owner = THIS_MODULE,
0402     .shutdown = usb_port_shutdown,
0403 };
0404 
0405 static int link_peers(struct usb_port *left, struct usb_port *right)
0406 {
0407     struct usb_port *ss_port, *hs_port;
0408     int rc;
0409 
0410     if (left->peer == right && right->peer == left)
0411         return 0;
0412 
0413     if (left->peer || right->peer) {
0414         struct usb_port *lpeer = left->peer;
0415         struct usb_port *rpeer = right->peer;
0416         char *method;
0417 
0418         if (left->location && left->location == right->location)
0419             method = "location";
0420         else
0421             method = "default";
0422 
0423         pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
0424             dev_name(&left->dev), dev_name(&right->dev), method,
0425             dev_name(&left->dev),
0426             lpeer ? dev_name(&lpeer->dev) : "none",
0427             dev_name(&right->dev),
0428             rpeer ? dev_name(&rpeer->dev) : "none");
0429         return -EBUSY;
0430     }
0431 
0432     rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
0433     if (rc)
0434         return rc;
0435     rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
0436     if (rc) {
0437         sysfs_remove_link(&left->dev.kobj, "peer");
0438         return rc;
0439     }
0440 
0441     /*
0442      * We need to wake the HiSpeed port to make sure we don't race
0443      * setting ->peer with usb_port_runtime_suspend().  Otherwise we
0444      * may miss a suspend event for the SuperSpeed port.
0445      */
0446     if (left->is_superspeed) {
0447         ss_port = left;
0448         WARN_ON(right->is_superspeed);
0449         hs_port = right;
0450     } else {
0451         ss_port = right;
0452         WARN_ON(!right->is_superspeed);
0453         hs_port = left;
0454     }
0455     pm_runtime_get_sync(&hs_port->dev);
0456 
0457     left->peer = right;
0458     right->peer = left;
0459 
0460     /*
0461      * The SuperSpeed reference is dropped when the HiSpeed port in
0462      * this relationship suspends, i.e. when it is safe to allow a
0463      * SuperSpeed connection to drop since there is no risk of a
0464      * device degrading to its powered-off HiSpeed connection.
0465      *
0466      * Also, drop the HiSpeed ref taken above.
0467      */
0468     pm_runtime_get_sync(&ss_port->dev);
0469     pm_runtime_put(&hs_port->dev);
0470 
0471     return 0;
0472 }
0473 
0474 static void link_peers_report(struct usb_port *left, struct usb_port *right)
0475 {
0476     int rc;
0477 
0478     rc = link_peers(left, right);
0479     if (rc == 0) {
0480         dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
0481     } else {
0482         dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
0483                 dev_name(&right->dev), rc);
0484         pr_warn_once("usb: port power management may be unreliable\n");
0485         usb_port_block_power_off = 1;
0486     }
0487 }
0488 
0489 static void unlink_peers(struct usb_port *left, struct usb_port *right)
0490 {
0491     struct usb_port *ss_port, *hs_port;
0492 
0493     WARN(right->peer != left || left->peer != right,
0494             "%s and %s are not peers?\n",
0495             dev_name(&left->dev), dev_name(&right->dev));
0496 
0497     /*
0498      * We wake the HiSpeed port to make sure we don't race its
0499      * usb_port_runtime_resume() event which takes a SuperSpeed ref
0500      * when ->peer is !NULL.
0501      */
0502     if (left->is_superspeed) {
0503         ss_port = left;
0504         hs_port = right;
0505     } else {
0506         ss_port = right;
0507         hs_port = left;
0508     }
0509 
0510     pm_runtime_get_sync(&hs_port->dev);
0511 
0512     sysfs_remove_link(&left->dev.kobj, "peer");
0513     right->peer = NULL;
0514     sysfs_remove_link(&right->dev.kobj, "peer");
0515     left->peer = NULL;
0516 
0517     /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
0518     pm_runtime_put(&ss_port->dev);
0519 
0520     /* Drop the ref taken above */
0521     pm_runtime_put(&hs_port->dev);
0522 }
0523 
0524 /*
0525  * For each usb hub device in the system check to see if it is in the
0526  * peer domain of the given port_dev, and if it is check to see if it
0527  * has a port that matches the given port by location
0528  */
0529 static int match_location(struct usb_device *peer_hdev, void *p)
0530 {
0531     int port1;
0532     struct usb_hcd *hcd, *peer_hcd;
0533     struct usb_port *port_dev = p, *peer;
0534     struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
0535     struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
0536 
0537     if (!peer_hub)
0538         return 0;
0539 
0540     hcd = bus_to_hcd(hdev->bus);
0541     peer_hcd = bus_to_hcd(peer_hdev->bus);
0542     /* peer_hcd is provisional until we verify it against the known peer */
0543     if (peer_hcd != hcd->shared_hcd)
0544         return 0;
0545 
0546     for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
0547         peer = peer_hub->ports[port1 - 1];
0548         if (peer && peer->location == port_dev->location) {
0549             link_peers_report(port_dev, peer);
0550             return 1; /* done */
0551         }
0552     }
0553 
0554     return 0;
0555 }
0556 
0557 /*
0558  * Find the peer port either via explicit platform firmware "location"
0559  * data, the peer hcd for root hubs, or the upstream peer relationship
0560  * for all other hubs.
0561  */
0562 static void find_and_link_peer(struct usb_hub *hub, int port1)
0563 {
0564     struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
0565     struct usb_device *hdev = hub->hdev;
0566     struct usb_device *peer_hdev;
0567     struct usb_hub *peer_hub;
0568 
0569     /*
0570      * If location data is available then we can only peer this port
0571      * by a location match, not the default peer (lest we create a
0572      * situation where we need to go back and undo a default peering
0573      * when the port is later peered by location data)
0574      */
0575     if (port_dev->location) {
0576         /* we link the peer in match_location() if found */
0577         usb_for_each_dev(port_dev, match_location);
0578         return;
0579     } else if (!hdev->parent) {
0580         struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
0581         struct usb_hcd *peer_hcd = hcd->shared_hcd;
0582 
0583         if (!peer_hcd)
0584             return;
0585 
0586         peer_hdev = peer_hcd->self.root_hub;
0587     } else {
0588         struct usb_port *upstream;
0589         struct usb_device *parent = hdev->parent;
0590         struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
0591 
0592         if (!parent_hub)
0593             return;
0594 
0595         upstream = parent_hub->ports[hdev->portnum - 1];
0596         if (!upstream || !upstream->peer)
0597             return;
0598 
0599         peer_hdev = upstream->peer->child;
0600     }
0601 
0602     peer_hub = usb_hub_to_struct_hub(peer_hdev);
0603     if (!peer_hub || port1 > peer_hdev->maxchild)
0604         return;
0605 
0606     /*
0607      * we found a valid default peer, last check is to make sure it
0608      * does not have location data
0609      */
0610     peer = peer_hub->ports[port1 - 1];
0611     if (peer && peer->location == 0)
0612         link_peers_report(port_dev, peer);
0613 }
0614 
0615 static int connector_bind(struct device *dev, struct device *connector, void *data)
0616 {
0617     int ret;
0618 
0619     ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector");
0620     if (ret)
0621         return ret;
0622 
0623     ret = sysfs_create_link(&connector->kobj, &dev->kobj, dev_name(dev));
0624     if (ret)
0625         sysfs_remove_link(&dev->kobj, "connector");
0626 
0627     return ret;
0628 }
0629 
0630 static void connector_unbind(struct device *dev, struct device *connector, void *data)
0631 {
0632     sysfs_remove_link(&connector->kobj, dev_name(dev));
0633     sysfs_remove_link(&dev->kobj, "connector");
0634 }
0635 
0636 static const struct component_ops connector_ops = {
0637     .bind = connector_bind,
0638     .unbind = connector_unbind,
0639 };
0640 
0641 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
0642 {
0643     struct usb_port *port_dev;
0644     struct usb_device *hdev = hub->hdev;
0645     int retval;
0646 
0647     port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
0648     if (!port_dev)
0649         return -ENOMEM;
0650 
0651     port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
0652     if (!port_dev->req) {
0653         kfree(port_dev);
0654         return -ENOMEM;
0655     }
0656 
0657     hub->ports[port1 - 1] = port_dev;
0658     port_dev->portnum = port1;
0659     set_bit(port1, hub->power_bits);
0660     port_dev->dev.parent = hub->intfdev;
0661     if (hub_is_superspeed(hdev)) {
0662         port_dev->usb3_lpm_u1_permit = 1;
0663         port_dev->usb3_lpm_u2_permit = 1;
0664         port_dev->dev.groups = port_dev_usb3_group;
0665     } else
0666         port_dev->dev.groups = port_dev_group;
0667     port_dev->dev.type = &usb_port_device_type;
0668     port_dev->dev.driver = &usb_port_driver;
0669     if (hub_is_superspeed(hub->hdev))
0670         port_dev->is_superspeed = 1;
0671     dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
0672             port1);
0673     mutex_init(&port_dev->status_lock);
0674     retval = device_register(&port_dev->dev);
0675     if (retval) {
0676         put_device(&port_dev->dev);
0677         return retval;
0678     }
0679 
0680     /* Set default policy of port-poweroff disabled. */
0681     retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
0682             DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
0683     if (retval < 0) {
0684         device_unregister(&port_dev->dev);
0685         return retval;
0686     }
0687 
0688     retval = component_add(&port_dev->dev, &connector_ops);
0689     if (retval) {
0690         dev_warn(&port_dev->dev, "failed to add component\n");
0691         device_unregister(&port_dev->dev);
0692         return retval;
0693     }
0694 
0695     find_and_link_peer(hub, port1);
0696 
0697     /*
0698      * Enable runtime pm and hold a refernce that hub_configure()
0699      * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
0700      * and the hub has been fully registered (hdev->maxchild set).
0701      */
0702     pm_runtime_set_active(&port_dev->dev);
0703     pm_runtime_get_noresume(&port_dev->dev);
0704     pm_runtime_enable(&port_dev->dev);
0705     device_enable_async_suspend(&port_dev->dev);
0706 
0707     /*
0708      * Keep hidden the ability to enable port-poweroff if the hub
0709      * does not support power switching.
0710      */
0711     if (!hub_is_port_power_switchable(hub))
0712         return 0;
0713 
0714     /* Attempt to let userspace take over the policy. */
0715     retval = dev_pm_qos_expose_flags(&port_dev->dev,
0716             PM_QOS_FLAG_NO_POWER_OFF);
0717     if (retval < 0) {
0718         dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
0719         return 0;
0720     }
0721 
0722     /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
0723     retval = dev_pm_qos_remove_request(port_dev->req);
0724     if (retval >= 0) {
0725         kfree(port_dev->req);
0726         port_dev->req = NULL;
0727     }
0728     return 0;
0729 }
0730 
0731 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
0732 {
0733     struct usb_port *port_dev = hub->ports[port1 - 1];
0734     struct usb_port *peer;
0735 
0736     peer = port_dev->peer;
0737     if (peer)
0738         unlink_peers(port_dev, peer);
0739     component_del(&port_dev->dev, &connector_ops);
0740     device_unregister(&port_dev->dev);
0741 }