Lan Tianyu | 6e30d7c | 2013-01-11 20:10:38 +0800 | [diff] [blame] | 1 | /* |
| 2 | * usb port device code |
| 3 | * |
| 4 | * Copyright (C) 2012 Intel Corp |
| 5 | * |
| 6 | * Author: Lan Tianyu <tianyu.lan@intel.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 14 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | * for more details. |
| 16 | * |
| 17 | */ |
| 18 | |
Lan Tianyu | 9f7344f | 2013-01-19 22:30:19 +0800 | [diff] [blame] | 19 | #include <linux/slab.h> |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 20 | #include <linux/pm_qos.h> |
Lan Tianyu | 9f7344f | 2013-01-19 22:30:19 +0800 | [diff] [blame] | 21 | |
Lan Tianyu | 6e30d7c | 2013-01-11 20:10:38 +0800 | [diff] [blame] | 22 | #include "hub.h" |
| 23 | |
Dan Williams | 6c79fe4 | 2014-06-17 16:16:27 -0700 | [diff] [blame] | 24 | static int usb_port_block_power_off; |
| 25 | |
Lan Tianyu | cef7468 | 2013-01-20 01:53:32 +0800 | [diff] [blame] | 26 | static const struct attribute_group *port_dev_group[]; |
| 27 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 28 | static ssize_t connect_type_show(struct device *dev, |
| 29 | struct device_attribute *attr, char *buf) |
Lan Tianyu | cef7468 | 2013-01-20 01:53:32 +0800 | [diff] [blame] | 30 | { |
| 31 | struct usb_port *port_dev = to_usb_port(dev); |
| 32 | char *result; |
| 33 | |
| 34 | switch (port_dev->connect_type) { |
| 35 | case USB_PORT_CONNECT_TYPE_HOT_PLUG: |
| 36 | result = "hotplug"; |
| 37 | break; |
| 38 | case USB_PORT_CONNECT_TYPE_HARD_WIRED: |
| 39 | result = "hardwired"; |
| 40 | break; |
| 41 | case USB_PORT_NOT_USED: |
| 42 | result = "not used"; |
| 43 | break; |
| 44 | default: |
| 45 | result = "unknown"; |
| 46 | break; |
| 47 | } |
| 48 | |
| 49 | return sprintf(buf, "%s\n", result); |
| 50 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 51 | static DEVICE_ATTR_RO(connect_type); |
Lan Tianyu | cef7468 | 2013-01-20 01:53:32 +0800 | [diff] [blame] | 52 | |
| 53 | static struct attribute *port_dev_attrs[] = { |
| 54 | &dev_attr_connect_type.attr, |
| 55 | NULL, |
| 56 | }; |
| 57 | |
| 58 | static struct attribute_group port_dev_attr_grp = { |
| 59 | .attrs = port_dev_attrs, |
| 60 | }; |
| 61 | |
| 62 | static const struct attribute_group *port_dev_group[] = { |
| 63 | &port_dev_attr_grp, |
| 64 | NULL, |
| 65 | }; |
| 66 | |
Lan Tianyu | 6e30d7c | 2013-01-11 20:10:38 +0800 | [diff] [blame] | 67 | static void usb_port_device_release(struct device *dev) |
| 68 | { |
| 69 | struct usb_port *port_dev = to_usb_port(dev); |
| 70 | |
Dan Williams | e3d1050 | 2014-06-17 16:16:32 -0700 | [diff] [blame] | 71 | kfree(port_dev->req); |
Lan Tianyu | 6e30d7c | 2013-01-11 20:10:38 +0800 | [diff] [blame] | 72 | kfree(port_dev); |
| 73 | } |
| 74 | |
Rafael J. Wysocki | ceb6c9c | 2014-11-29 23:47:05 +0100 | [diff] [blame] | 75 | #ifdef CONFIG_PM |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 76 | static int usb_port_runtime_resume(struct device *dev) |
| 77 | { |
| 78 | struct usb_port *port_dev = to_usb_port(dev); |
| 79 | struct usb_device *hdev = to_usb_device(dev->parent->parent); |
| 80 | struct usb_interface *intf = to_usb_interface(dev->parent); |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 81 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
Dan Williams | 7027df3 | 2014-05-20 18:09:36 -0700 | [diff] [blame] | 82 | struct usb_device *udev = port_dev->child; |
Dan Williams | 7ad3c47 | 2014-05-20 18:08:57 -0700 | [diff] [blame] | 83 | struct usb_port *peer = port_dev->peer; |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 84 | int port1 = port_dev->portnum; |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 85 | int retval; |
| 86 | |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 87 | if (!hub) |
| 88 | return -EINVAL; |
Alan Stern | 600856c | 2014-05-20 18:08:07 -0700 | [diff] [blame] | 89 | if (hub->in_reset) { |
Dan Williams | d5c3834 | 2014-05-20 18:08:52 -0700 | [diff] [blame] | 90 | set_bit(port1, hub->power_bits); |
Alan Stern | 600856c | 2014-05-20 18:08:07 -0700 | [diff] [blame] | 91 | return 0; |
| 92 | } |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 93 | |
Dan Williams | 7ad3c47 | 2014-05-20 18:08:57 -0700 | [diff] [blame] | 94 | /* |
| 95 | * Power on our usb3 peer before this usb2 port to prevent a usb3 |
| 96 | * device from degrading to its usb2 connection |
| 97 | */ |
| 98 | if (!port_dev->is_superspeed && peer) |
| 99 | pm_runtime_get_sync(&peer->dev); |
| 100 | |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 101 | usb_autopm_get_interface(intf); |
Mathias Nyman | 4134126 | 2013-06-18 17:28:48 +0300 | [diff] [blame] | 102 | retval = usb_hub_set_port_power(hdev, hub, port1, true); |
Dan Williams | 7ad3c47 | 2014-05-20 18:08:57 -0700 | [diff] [blame] | 103 | msleep(hub_power_on_good_delay(hub)); |
Dan Williams | 7027df3 | 2014-05-20 18:09:36 -0700 | [diff] [blame] | 104 | if (udev && !retval) { |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 105 | /* |
Dan Williams | 3cd12f9 | 2014-05-29 12:58:46 -0700 | [diff] [blame] | 106 | * Our preference is to simply wait for the port to reconnect, |
| 107 | * as that is the lowest latency method to restart the port. |
| 108 | * However, there are cases where toggling port power results in |
| 109 | * the host port and the device port getting out of sync causing |
| 110 | * a link training live lock. Upon timeout, flag the port as |
| 111 | * needing warm reset recovery (to be performed later by |
| 112 | * usb_port_resume() as requested via usb_wakeup_notification()) |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 113 | */ |
Dan Williams | 3cd12f9 | 2014-05-29 12:58:46 -0700 | [diff] [blame] | 114 | if (hub_port_debounce_be_connected(hub, port1) < 0) { |
| 115 | dev_dbg(&port_dev->dev, "reconnect timeout\n"); |
| 116 | if (hub_is_superspeed(hdev)) |
| 117 | set_bit(port1, hub->warm_reset_bits); |
| 118 | } |
Dan Williams | 7027df3 | 2014-05-20 18:09:36 -0700 | [diff] [blame] | 119 | |
| 120 | /* Force the child awake to revalidate after the power loss. */ |
| 121 | if (!test_and_set_bit(port1, hub->child_usage_bits)) { |
| 122 | pm_runtime_get_noresume(&port_dev->dev); |
| 123 | pm_request_resume(&udev->dev); |
| 124 | } |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 125 | } |
| 126 | |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 127 | usb_autopm_put_interface(intf); |
Dan Williams | 7ad3c47 | 2014-05-20 18:08:57 -0700 | [diff] [blame] | 128 | |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 129 | return retval; |
| 130 | } |
| 131 | |
| 132 | static int usb_port_runtime_suspend(struct device *dev) |
| 133 | { |
| 134 | struct usb_port *port_dev = to_usb_port(dev); |
| 135 | struct usb_device *hdev = to_usb_device(dev->parent->parent); |
| 136 | struct usb_interface *intf = to_usb_interface(dev->parent); |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 137 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
Dan Williams | 7ad3c47 | 2014-05-20 18:08:57 -0700 | [diff] [blame] | 138 | struct usb_port *peer = port_dev->peer; |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 139 | int port1 = port_dev->portnum; |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 140 | int retval; |
| 141 | |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 142 | if (!hub) |
| 143 | return -EINVAL; |
Alan Stern | 600856c | 2014-05-20 18:08:07 -0700 | [diff] [blame] | 144 | if (hub->in_reset) |
| 145 | return -EBUSY; |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 146 | |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 147 | if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF) |
| 148 | == PM_QOS_FLAGS_ALL) |
| 149 | return -EAGAIN; |
| 150 | |
Dan Williams | 6c79fe4 | 2014-06-17 16:16:27 -0700 | [diff] [blame] | 151 | if (usb_port_block_power_off) |
| 152 | return -EBUSY; |
| 153 | |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 154 | usb_autopm_get_interface(intf); |
Mathias Nyman | 4134126 | 2013-06-18 17:28:48 +0300 | [diff] [blame] | 155 | retval = usb_hub_set_port_power(hdev, hub, port1, false); |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 156 | usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION); |
Dan Williams | 6908058 | 2014-05-20 18:09:10 -0700 | [diff] [blame] | 157 | if (!port_dev->is_superspeed) |
| 158 | usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE); |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 159 | usb_autopm_put_interface(intf); |
Dan Williams | 7ad3c47 | 2014-05-20 18:08:57 -0700 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | * Our peer usb3 port may now be able to suspend, so |
| 163 | * asynchronously queue a suspend request to observe that this |
| 164 | * usb2 port is now off. |
| 165 | */ |
| 166 | if (!port_dev->is_superspeed && peer) |
| 167 | pm_runtime_put(&peer->dev); |
| 168 | |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 169 | return retval; |
| 170 | } |
| 171 | #endif |
| 172 | |
| 173 | static const struct dev_pm_ops usb_port_pm_ops = { |
Rafael J. Wysocki | ceb6c9c | 2014-11-29 23:47:05 +0100 | [diff] [blame] | 174 | #ifdef CONFIG_PM |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 175 | .runtime_suspend = usb_port_runtime_suspend, |
| 176 | .runtime_resume = usb_port_runtime_resume, |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 177 | #endif |
| 178 | }; |
| 179 | |
Lan Tianyu | 6e30d7c | 2013-01-11 20:10:38 +0800 | [diff] [blame] | 180 | struct device_type usb_port_device_type = { |
| 181 | .name = "usb_port", |
| 182 | .release = usb_port_device_release, |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 183 | .pm = &usb_port_pm_ops, |
Lan Tianyu | 6e30d7c | 2013-01-11 20:10:38 +0800 | [diff] [blame] | 184 | }; |
| 185 | |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 186 | static struct device_driver usb_port_driver = { |
| 187 | .name = "usb", |
| 188 | .owner = THIS_MODULE, |
| 189 | }; |
| 190 | |
Dan Williams | b7e38ea | 2014-05-20 18:08:45 -0700 | [diff] [blame] | 191 | static int link_peers(struct usb_port *left, struct usb_port *right) |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 192 | { |
Dan Williams | 7ad3c47 | 2014-05-20 18:08:57 -0700 | [diff] [blame] | 193 | struct usb_port *ss_port, *hs_port; |
Dan Williams | b7e38ea | 2014-05-20 18:08:45 -0700 | [diff] [blame] | 194 | int rc; |
| 195 | |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 196 | if (left->peer == right && right->peer == left) |
Dan Williams | b7e38ea | 2014-05-20 18:08:45 -0700 | [diff] [blame] | 197 | return 0; |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 198 | |
| 199 | if (left->peer || right->peer) { |
| 200 | struct usb_port *lpeer = left->peer; |
| 201 | struct usb_port *rpeer = right->peer; |
Dan Williams | 6c79fe4 | 2014-06-17 16:16:27 -0700 | [diff] [blame] | 202 | char *method; |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 203 | |
Dan Williams | 6c79fe4 | 2014-06-17 16:16:27 -0700 | [diff] [blame] | 204 | if (left->location && left->location == right->location) |
| 205 | method = "location"; |
| 206 | else |
| 207 | method = "default"; |
| 208 | |
| 209 | pr_warn("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n", |
| 210 | dev_name(&left->dev), dev_name(&right->dev), method, |
| 211 | dev_name(&left->dev), |
| 212 | lpeer ? dev_name(&lpeer->dev) : "none", |
| 213 | dev_name(&right->dev), |
| 214 | rpeer ? dev_name(&rpeer->dev) : "none"); |
Dan Williams | b7e38ea | 2014-05-20 18:08:45 -0700 | [diff] [blame] | 215 | return -EBUSY; |
| 216 | } |
| 217 | |
| 218 | rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer"); |
| 219 | if (rc) |
| 220 | return rc; |
| 221 | rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer"); |
| 222 | if (rc) { |
| 223 | sysfs_remove_link(&left->dev.kobj, "peer"); |
| 224 | return rc; |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Dan Williams | 7ad3c47 | 2014-05-20 18:08:57 -0700 | [diff] [blame] | 227 | /* |
| 228 | * We need to wake the HiSpeed port to make sure we don't race |
| 229 | * setting ->peer with usb_port_runtime_suspend(). Otherwise we |
| 230 | * may miss a suspend event for the SuperSpeed port. |
| 231 | */ |
| 232 | if (left->is_superspeed) { |
| 233 | ss_port = left; |
| 234 | WARN_ON(right->is_superspeed); |
| 235 | hs_port = right; |
| 236 | } else { |
| 237 | ss_port = right; |
| 238 | WARN_ON(!right->is_superspeed); |
| 239 | hs_port = left; |
| 240 | } |
| 241 | pm_runtime_get_sync(&hs_port->dev); |
| 242 | |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 243 | left->peer = right; |
| 244 | right->peer = left; |
Dan Williams | b7e38ea | 2014-05-20 18:08:45 -0700 | [diff] [blame] | 245 | |
Dan Williams | 7ad3c47 | 2014-05-20 18:08:57 -0700 | [diff] [blame] | 246 | /* |
| 247 | * The SuperSpeed reference is dropped when the HiSpeed port in |
| 248 | * this relationship suspends, i.e. when it is safe to allow a |
| 249 | * SuperSpeed connection to drop since there is no risk of a |
| 250 | * device degrading to its powered-off HiSpeed connection. |
| 251 | * |
| 252 | * Also, drop the HiSpeed ref taken above. |
| 253 | */ |
| 254 | pm_runtime_get_sync(&ss_port->dev); |
| 255 | pm_runtime_put(&hs_port->dev); |
| 256 | |
Dan Williams | b7e38ea | 2014-05-20 18:08:45 -0700 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static void link_peers_report(struct usb_port *left, struct usb_port *right) |
| 261 | { |
| 262 | int rc; |
| 263 | |
| 264 | rc = link_peers(left, right); |
| 265 | if (rc == 0) { |
| 266 | dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev)); |
| 267 | } else { |
| 268 | dev_warn(&left->dev, "failed to peer to %s (%d)\n", |
| 269 | dev_name(&right->dev), rc); |
| 270 | pr_warn_once("usb: port power management may be unreliable\n"); |
Dan Williams | 6c79fe4 | 2014-06-17 16:16:27 -0700 | [diff] [blame] | 271 | usb_port_block_power_off = 1; |
Dan Williams | b7e38ea | 2014-05-20 18:08:45 -0700 | [diff] [blame] | 272 | } |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static void unlink_peers(struct usb_port *left, struct usb_port *right) |
| 276 | { |
Dan Williams | 7ad3c47 | 2014-05-20 18:08:57 -0700 | [diff] [blame] | 277 | struct usb_port *ss_port, *hs_port; |
| 278 | |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 279 | WARN(right->peer != left || left->peer != right, |
| 280 | "%s and %s are not peers?\n", |
| 281 | dev_name(&left->dev), dev_name(&right->dev)); |
| 282 | |
Dan Williams | 7ad3c47 | 2014-05-20 18:08:57 -0700 | [diff] [blame] | 283 | /* |
| 284 | * We wake the HiSpeed port to make sure we don't race its |
| 285 | * usb_port_runtime_resume() event which takes a SuperSpeed ref |
| 286 | * when ->peer is !NULL. |
| 287 | */ |
| 288 | if (left->is_superspeed) { |
| 289 | ss_port = left; |
| 290 | hs_port = right; |
| 291 | } else { |
| 292 | ss_port = right; |
| 293 | hs_port = left; |
| 294 | } |
| 295 | |
| 296 | pm_runtime_get_sync(&hs_port->dev); |
| 297 | |
Dan Williams | b7e38ea | 2014-05-20 18:08:45 -0700 | [diff] [blame] | 298 | sysfs_remove_link(&left->dev.kobj, "peer"); |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 299 | right->peer = NULL; |
Dan Williams | b7e38ea | 2014-05-20 18:08:45 -0700 | [diff] [blame] | 300 | sysfs_remove_link(&right->dev.kobj, "peer"); |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 301 | left->peer = NULL; |
Dan Williams | 7ad3c47 | 2014-05-20 18:08:57 -0700 | [diff] [blame] | 302 | |
| 303 | /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */ |
| 304 | pm_runtime_put(&ss_port->dev); |
| 305 | |
| 306 | /* Drop the ref taken above */ |
| 307 | pm_runtime_put(&hs_port->dev); |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Dan Williams | 8b1ba80 | 2014-05-20 18:08:33 -0700 | [diff] [blame] | 310 | /* |
Dan Williams | 3bfd659 | 2014-05-20 18:08:40 -0700 | [diff] [blame] | 311 | * For each usb hub device in the system check to see if it is in the |
| 312 | * peer domain of the given port_dev, and if it is check to see if it |
| 313 | * has a port that matches the given port by location |
| 314 | */ |
| 315 | static int match_location(struct usb_device *peer_hdev, void *p) |
| 316 | { |
| 317 | int port1; |
| 318 | struct usb_hcd *hcd, *peer_hcd; |
| 319 | struct usb_port *port_dev = p, *peer; |
| 320 | struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev); |
| 321 | struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent); |
| 322 | |
| 323 | if (!peer_hub) |
| 324 | return 0; |
| 325 | |
| 326 | hcd = bus_to_hcd(hdev->bus); |
| 327 | peer_hcd = bus_to_hcd(peer_hdev->bus); |
| 328 | /* peer_hcd is provisional until we verify it against the known peer */ |
| 329 | if (peer_hcd != hcd->shared_hcd) |
| 330 | return 0; |
| 331 | |
| 332 | for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) { |
| 333 | peer = peer_hub->ports[port1 - 1]; |
| 334 | if (peer && peer->location == port_dev->location) { |
Dan Williams | b7e38ea | 2014-05-20 18:08:45 -0700 | [diff] [blame] | 335 | link_peers_report(port_dev, peer); |
Dan Williams | 3bfd659 | 2014-05-20 18:08:40 -0700 | [diff] [blame] | 336 | return 1; /* done */ |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Find the peer port either via explicit platform firmware "location" |
| 345 | * data, the peer hcd for root hubs, or the upstream peer relationship |
| 346 | * for all other hubs. |
Dan Williams | 8b1ba80 | 2014-05-20 18:08:33 -0700 | [diff] [blame] | 347 | */ |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 348 | static void find_and_link_peer(struct usb_hub *hub, int port1) |
| 349 | { |
| 350 | struct usb_port *port_dev = hub->ports[port1 - 1], *peer; |
| 351 | struct usb_device *hdev = hub->hdev; |
Dan Williams | 8b1ba80 | 2014-05-20 18:08:33 -0700 | [diff] [blame] | 352 | struct usb_device *peer_hdev; |
| 353 | struct usb_hub *peer_hub; |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 354 | |
Dan Williams | 3bfd659 | 2014-05-20 18:08:40 -0700 | [diff] [blame] | 355 | /* |
| 356 | * If location data is available then we can only peer this port |
| 357 | * by a location match, not the default peer (lest we create a |
| 358 | * situation where we need to go back and undo a default peering |
| 359 | * when the port is later peered by location data) |
| 360 | */ |
| 361 | if (port_dev->location) { |
| 362 | /* we link the peer in match_location() if found */ |
| 363 | usb_for_each_dev(port_dev, match_location); |
| 364 | return; |
| 365 | } else if (!hdev->parent) { |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 366 | struct usb_hcd *hcd = bus_to_hcd(hdev->bus); |
| 367 | struct usb_hcd *peer_hcd = hcd->shared_hcd; |
| 368 | |
| 369 | if (!peer_hcd) |
| 370 | return; |
| 371 | |
| 372 | peer_hdev = peer_hcd->self.root_hub; |
Dan Williams | 8b1ba80 | 2014-05-20 18:08:33 -0700 | [diff] [blame] | 373 | } else { |
| 374 | struct usb_port *upstream; |
| 375 | struct usb_device *parent = hdev->parent; |
| 376 | struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent); |
| 377 | |
| 378 | if (!parent_hub) |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 379 | return; |
| 380 | |
Dan Williams | 8b1ba80 | 2014-05-20 18:08:33 -0700 | [diff] [blame] | 381 | upstream = parent_hub->ports[hdev->portnum - 1]; |
| 382 | if (!upstream || !upstream->peer) |
| 383 | return; |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 384 | |
Dan Williams | 8b1ba80 | 2014-05-20 18:08:33 -0700 | [diff] [blame] | 385 | peer_hdev = upstream->peer->child; |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 386 | } |
Dan Williams | 8b1ba80 | 2014-05-20 18:08:33 -0700 | [diff] [blame] | 387 | |
| 388 | peer_hub = usb_hub_to_struct_hub(peer_hdev); |
| 389 | if (!peer_hub || port1 > peer_hdev->maxchild) |
| 390 | return; |
| 391 | |
Dan Williams | 3bfd659 | 2014-05-20 18:08:40 -0700 | [diff] [blame] | 392 | /* |
| 393 | * we found a valid default peer, last check is to make sure it |
| 394 | * does not have location data |
| 395 | */ |
Dan Williams | 8b1ba80 | 2014-05-20 18:08:33 -0700 | [diff] [blame] | 396 | peer = peer_hub->ports[port1 - 1]; |
Dan Williams | 3bfd659 | 2014-05-20 18:08:40 -0700 | [diff] [blame] | 397 | if (peer && peer->location == 0) |
Dan Williams | b7e38ea | 2014-05-20 18:08:45 -0700 | [diff] [blame] | 398 | link_peers_report(port_dev, peer); |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Lan Tianyu | 6e30d7c | 2013-01-11 20:10:38 +0800 | [diff] [blame] | 401 | int usb_hub_create_port_device(struct usb_hub *hub, int port1) |
| 402 | { |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 403 | struct usb_port *port_dev; |
Lan Tianyu | 6e30d7c | 2013-01-11 20:10:38 +0800 | [diff] [blame] | 404 | int retval; |
| 405 | |
| 406 | port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL); |
Dan Williams | e3d1050 | 2014-06-17 16:16:32 -0700 | [diff] [blame] | 407 | if (!port_dev) |
| 408 | return -ENOMEM; |
| 409 | |
| 410 | port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL); |
| 411 | if (!port_dev->req) { |
| 412 | kfree(port_dev); |
| 413 | return -ENOMEM; |
Lan Tianyu | 6e30d7c | 2013-01-11 20:10:38 +0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | hub->ports[port1 - 1] = port_dev; |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 417 | port_dev->portnum = port1; |
Dan Williams | d5c3834 | 2014-05-20 18:08:52 -0700 | [diff] [blame] | 418 | set_bit(port1, hub->power_bits); |
Lan Tianyu | 6e30d7c | 2013-01-11 20:10:38 +0800 | [diff] [blame] | 419 | port_dev->dev.parent = hub->intfdev; |
Lan Tianyu | cef7468 | 2013-01-20 01:53:32 +0800 | [diff] [blame] | 420 | port_dev->dev.groups = port_dev_group; |
Lan Tianyu | 6e30d7c | 2013-01-11 20:10:38 +0800 | [diff] [blame] | 421 | port_dev->dev.type = &usb_port_device_type; |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 422 | port_dev->dev.driver = &usb_port_driver; |
Dan Williams | 7ad3c47 | 2014-05-20 18:08:57 -0700 | [diff] [blame] | 423 | if (hub_is_superspeed(hub->hdev)) |
| 424 | port_dev->is_superspeed = 1; |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 425 | dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev), |
| 426 | port1); |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 427 | mutex_init(&port_dev->status_lock); |
Lan Tianyu | 6e30d7c | 2013-01-11 20:10:38 +0800 | [diff] [blame] | 428 | retval = device_register(&port_dev->dev); |
Dan Williams | e3d1050 | 2014-06-17 16:16:32 -0700 | [diff] [blame] | 429 | if (retval) { |
| 430 | put_device(&port_dev->dev); |
| 431 | return retval; |
| 432 | } |
| 433 | |
| 434 | /* Set default policy of port-poweroff disabled. */ |
| 435 | retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req, |
| 436 | DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF); |
| 437 | if (retval < 0) { |
| 438 | device_unregister(&port_dev->dev); |
| 439 | return retval; |
| 440 | } |
Lan Tianyu | 6e30d7c | 2013-01-11 20:10:38 +0800 | [diff] [blame] | 441 | |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 442 | find_and_link_peer(hub, port1); |
| 443 | |
Dan Williams | e3d1050 | 2014-06-17 16:16:32 -0700 | [diff] [blame] | 444 | /* |
| 445 | * Enable runtime pm and hold a refernce that hub_configure() |
| 446 | * will drop once the PM_QOS_NO_POWER_OFF flag state has been set |
| 447 | * and the hub has been fully registered (hdev->maxchild set). |
| 448 | */ |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 449 | pm_runtime_set_active(&port_dev->dev); |
Dan Williams | e3d1050 | 2014-06-17 16:16:32 -0700 | [diff] [blame] | 450 | pm_runtime_get_noresume(&port_dev->dev); |
| 451 | pm_runtime_enable(&port_dev->dev); |
| 452 | device_enable_async_suspend(&port_dev->dev); |
Lan Tianyu | f6cced1 | 2013-01-23 04:26:31 +0800 | [diff] [blame] | 453 | |
Dan Williams | 9262c19 | 2014-05-20 18:08:12 -0700 | [diff] [blame] | 454 | /* |
Dan Williams | e3d1050 | 2014-06-17 16:16:32 -0700 | [diff] [blame] | 455 | * Keep hidden the ability to enable port-poweroff if the hub |
| 456 | * does not support power switching. |
Lan Tianyu | f6cced1 | 2013-01-23 04:26:31 +0800 | [diff] [blame] | 457 | */ |
Dan Williams | e3d1050 | 2014-06-17 16:16:32 -0700 | [diff] [blame] | 458 | if (!hub_is_port_power_switchable(hub)) |
| 459 | return 0; |
Lan Tianyu | f6cced1 | 2013-01-23 04:26:31 +0800 | [diff] [blame] | 460 | |
Dan Williams | e3d1050 | 2014-06-17 16:16:32 -0700 | [diff] [blame] | 461 | /* Attempt to let userspace take over the policy. */ |
| 462 | retval = dev_pm_qos_expose_flags(&port_dev->dev, |
| 463 | PM_QOS_FLAG_NO_POWER_OFF); |
| 464 | if (retval < 0) { |
| 465 | dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n"); |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */ |
| 470 | retval = dev_pm_qos_remove_request(port_dev->req); |
| 471 | if (retval >= 0) { |
| 472 | kfree(port_dev->req); |
| 473 | port_dev->req = NULL; |
| 474 | } |
Lan Tianyu | 6e30d7c | 2013-01-11 20:10:38 +0800 | [diff] [blame] | 475 | return 0; |
Lan Tianyu | 6e30d7c | 2013-01-11 20:10:38 +0800 | [diff] [blame] | 476 | } |
| 477 | |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 478 | void usb_hub_remove_port_device(struct usb_hub *hub, int port1) |
Lan Tianyu | 6e30d7c | 2013-01-11 20:10:38 +0800 | [diff] [blame] | 479 | { |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 480 | struct usb_port *port_dev = hub->ports[port1 - 1]; |
| 481 | struct usb_port *peer; |
Lan Tianyu | 6e30d7c | 2013-01-11 20:10:38 +0800 | [diff] [blame] | 482 | |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 483 | peer = port_dev->peer; |
| 484 | if (peer) |
| 485 | unlink_peers(port_dev, peer); |
| 486 | device_unregister(&port_dev->dev); |
| 487 | } |