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 19 #include <linux/slab.h> 20 #include <linux/pm_qos.h> 21 22 #include "hub.h" 23 24 static int usb_port_block_power_off; 25 26 static const struct attribute_group *port_dev_group[]; 27 28 static ssize_t connect_type_show(struct device *dev, 29 struct device_attribute *attr, char *buf) 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 } 51 static DEVICE_ATTR_RO(connect_type); 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 67 static void usb_port_device_release(struct device *dev) 68 { 69 struct usb_port *port_dev = to_usb_port(dev); 70 71 kfree(port_dev->req); 72 kfree(port_dev); 73 } 74 75 #ifdef CONFIG_PM 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); 81 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 82 struct usb_device *udev = port_dev->child; 83 struct usb_port *peer = port_dev->peer; 84 int port1 = port_dev->portnum; 85 int retval; 86 87 if (!hub) 88 return -EINVAL; 89 if (hub->in_reset) { 90 set_bit(port1, hub->power_bits); 91 return 0; 92 } 93 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 101 usb_autopm_get_interface(intf); 102 retval = usb_hub_set_port_power(hdev, hub, port1, true); 103 msleep(hub_power_on_good_delay(hub)); 104 if (udev && !retval) { 105 /* 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()) 113 */ 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 } 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 } 125 } 126 127 usb_autopm_put_interface(intf); 128 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); 137 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 138 struct usb_port *peer = port_dev->peer; 139 int port1 = port_dev->portnum; 140 int retval; 141 142 if (!hub) 143 return -EINVAL; 144 if (hub->in_reset) 145 return -EBUSY; 146 147 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF) 148 == PM_QOS_FLAGS_ALL) 149 return -EAGAIN; 150 151 if (usb_port_block_power_off) 152 return -EBUSY; 153 154 usb_autopm_get_interface(intf); 155 retval = usb_hub_set_port_power(hdev, hub, port1, false); 156 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION); 157 if (!port_dev->is_superspeed) 158 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE); 159 usb_autopm_put_interface(intf); 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 169 return retval; 170 } 171 #endif 172 173 static const struct dev_pm_ops usb_port_pm_ops = { 174 #ifdef CONFIG_PM 175 .runtime_suspend = usb_port_runtime_suspend, 176 .runtime_resume = usb_port_runtime_resume, 177 #endif 178 }; 179 180 struct device_type usb_port_device_type = { 181 .name = "usb_port", 182 .release = usb_port_device_release, 183 .pm = &usb_port_pm_ops, 184 }; 185 186 static struct device_driver usb_port_driver = { 187 .name = "usb", 188 .owner = THIS_MODULE, 189 }; 190 191 static int link_peers(struct usb_port *left, struct usb_port *right) 192 { 193 struct usb_port *ss_port, *hs_port; 194 int rc; 195 196 if (left->peer == right && right->peer == left) 197 return 0; 198 199 if (left->peer || right->peer) { 200 struct usb_port *lpeer = left->peer; 201 struct usb_port *rpeer = right->peer; 202 char *method; 203 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"); 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; 225 } 226 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 243 left->peer = right; 244 right->peer = left; 245 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 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"); 271 usb_port_block_power_off = 1; 272 } 273 } 274 275 static void unlink_peers(struct usb_port *left, struct usb_port *right) 276 { 277 struct usb_port *ss_port, *hs_port; 278 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 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 298 sysfs_remove_link(&left->dev.kobj, "peer"); 299 right->peer = NULL; 300 sysfs_remove_link(&right->dev.kobj, "peer"); 301 left->peer = NULL; 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); 308 } 309 310 /* 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) { 335 link_peers_report(port_dev, peer); 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. 347 */ 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; 352 struct usb_device *peer_hdev; 353 struct usb_hub *peer_hub; 354 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) { 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; 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) 379 return; 380 381 upstream = parent_hub->ports[hdev->portnum - 1]; 382 if (!upstream || !upstream->peer) 383 return; 384 385 peer_hdev = upstream->peer->child; 386 } 387 388 peer_hub = usb_hub_to_struct_hub(peer_hdev); 389 if (!peer_hub || port1 > peer_hdev->maxchild) 390 return; 391 392 /* 393 * we found a valid default peer, last check is to make sure it 394 * does not have location data 395 */ 396 peer = peer_hub->ports[port1 - 1]; 397 if (peer && peer->location == 0) 398 link_peers_report(port_dev, peer); 399 } 400 401 int usb_hub_create_port_device(struct usb_hub *hub, int port1) 402 { 403 struct usb_port *port_dev; 404 int retval; 405 406 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL); 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; 414 } 415 416 hub->ports[port1 - 1] = port_dev; 417 port_dev->portnum = port1; 418 set_bit(port1, hub->power_bits); 419 port_dev->dev.parent = hub->intfdev; 420 port_dev->dev.groups = port_dev_group; 421 port_dev->dev.type = &usb_port_device_type; 422 port_dev->dev.driver = &usb_port_driver; 423 if (hub_is_superspeed(hub->hdev)) 424 port_dev->is_superspeed = 1; 425 dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev), 426 port1); 427 mutex_init(&port_dev->status_lock); 428 retval = device_register(&port_dev->dev); 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 } 441 442 find_and_link_peer(hub, port1); 443 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 */ 449 pm_runtime_set_active(&port_dev->dev); 450 pm_runtime_get_noresume(&port_dev->dev); 451 pm_runtime_enable(&port_dev->dev); 452 device_enable_async_suspend(&port_dev->dev); 453 454 /* 455 * Keep hidden the ability to enable port-poweroff if the hub 456 * does not support power switching. 457 */ 458 if (!hub_is_port_power_switchable(hub)) 459 return 0; 460 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 } 475 return 0; 476 } 477 478 void usb_hub_remove_port_device(struct usb_hub *hub, int port1) 479 { 480 struct usb_port *port_dev = hub->ports[port1 - 1]; 481 struct usb_port *peer; 482 483 peer = port_dev->peer; 484 if (peer) 485 unlink_peers(port_dev, peer); 486 device_unregister(&port_dev->dev); 487 } 488