| 1 | |
| 2 | |
| 3 | * usb port device code
|
| 4 | *
|
| 5 | * Copyright (C) 2012 Intel Corp
|
| 6 | *
|
| 7 | * Author: Lan Tianyu <[email protected]>
|
| 8 |
|
| 9 |
|
| 10 | #include <linux/kstrtox.h>
|
| 11 | #include <linux/slab.h>
|
| 12 | #include <linux/string_choices.h>
|
| 13 | #include <linux/sysfs.h>
|
| 14 | #include <linux/pm_qos.h>
|
| 15 | #include <linux/component.h>
|
| 16 | #include <linux/usb/of.h>
|
| 17 |
|
| 18 | #include "hub.h"
|
| 19 |
|
| 20 | static int usb_port_block_power_off;
|
| 21 |
|
| 22 | static const struct attribute_group *port_dev_group[];
|
| 23 |
|
| 24 | static bool usb_port_allow_power_off(struct usb_device *hdev,
|
| 25 | struct usb_hub *hub,
|
| 26 | struct usb_port *port_dev)
|
| 27 | {
|
| 28 | if (hub_is_port_power_switchable(hub))
|
| 29 | return true;
|
| 30 |
|
| 31 | if (!IS_ENABLED(CONFIG_ACPI))
|
| 32 | return false;
|
| 33 |
|
| 34 | return port_dev->connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED &&
|
| 35 | usb_acpi_power_manageable(hdev, port_dev->portnum - 1);
|
| 36 | }
|
| 37 |
|
| 38 | static ssize_t early_stop_show(struct device *dev,
|
| 39 | struct device_attribute *attr, char *buf)
|
| 40 | {
|
| 41 | struct usb_port *port_dev = to_usb_port(dev);
|
| 42 |
|
| 43 | return sysfs_emit(buf, "%s\n", str_yes_no(port_dev->early_stop));
|
| 44 | }
|
| 45 |
|
| 46 | static ssize_t early_stop_store(struct device *dev, struct device_attribute *attr,
|
| 47 | const char *buf, size_t count)
|
| 48 | {
|
| 49 | struct usb_port *port_dev = to_usb_port(dev);
|
| 50 | bool value;
|
| 51 |
|
| 52 | if (kstrtobool(buf, &value))
|
| 53 | return -EINVAL;
|
| 54 |
|
| 55 | if (value)
|
| 56 | port_dev->early_stop = 1;
|
| 57 | else
|
| 58 | port_dev->early_stop = 0;
|
| 59 |
|
| 60 | return count;
|
| 61 | }
|
| 62 | static DEVICE_ATTR_RW(early_stop);
|
| 63 |
|
| 64 | static ssize_t disable_show(struct device *dev,
|
| 65 | struct device_attribute *attr, char *buf)
|
| 66 | {
|
| 67 | struct usb_port *port_dev = to_usb_port(dev);
|
| 68 | struct usb_device *hdev = to_usb_device(dev->parent->parent);
|
| 69 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
|
| 70 | struct usb_interface *intf = to_usb_interface(dev->parent);
|
| 71 | int port1 = port_dev->portnum;
|
| 72 | u16 portstatus, unused;
|
| 73 | bool disabled;
|
| 74 | int rc;
|
| 75 | struct kernfs_node *kn;
|
| 76 |
|
| 77 | if (!hub)
|
| 78 | return -ENODEV;
|
| 79 | hub_get(hub);
|
| 80 | rc = usb_autopm_get_interface(intf);
|
| 81 | if (rc < 0)
|
| 82 | goto out_hub_get;
|
| 83 |
|
| 84 | |
| 85 | * Prevent deadlock if another process is concurrently
|
| 86 | * trying to unregister hdev.
|
| 87 |
|
| 88 | kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
|
| 89 | if (!kn) {
|
| 90 | rc = -ENODEV;
|
| 91 | goto out_autopm;
|
| 92 | }
|
| 93 | usb_lock_device(hdev);
|
| 94 | if (hub->disconnected) {
|
| 95 | rc = -ENODEV;
|
| 96 | goto out_hdev_lock;
|
| 97 | }
|
| 98 |
|
| 99 | usb_hub_port_status(hub, port1, &portstatus, &unused);
|
| 100 | disabled = !usb_port_is_power_on(hub, portstatus);
|
| 101 |
|
| 102 | out_hdev_lock:
|
| 103 | usb_unlock_device(hdev);
|
| 104 | sysfs_unbreak_active_protection(kn);
|
| 105 | out_autopm:
|
| 106 | usb_autopm_put_interface(intf);
|
| 107 | out_hub_get:
|
| 108 | hub_put(hub);
|
| 109 |
|
| 110 | if (rc)
|
| 111 | return rc;
|
| 112 |
|
| 113 | return sysfs_emit(buf, "%s\n", disabled ? "1" : "0");
|
| 114 | }
|
| 115 |
|
| 116 | static ssize_t disable_store(struct device *dev, struct device_attribute *attr,
|
| 117 | const char *buf, size_t count)
|
| 118 | {
|
| 119 | struct usb_port *port_dev = to_usb_port(dev);
|
| 120 | struct usb_device *hdev = to_usb_device(dev->parent->parent);
|
| 121 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
|
| 122 | struct usb_interface *intf = to_usb_interface(dev->parent);
|
| 123 | int port1 = port_dev->portnum;
|
| 124 | bool disabled;
|
| 125 | int rc;
|
| 126 | struct kernfs_node *kn;
|
| 127 |
|
| 128 | if (!hub)
|
| 129 | return -ENODEV;
|
| 130 | rc = kstrtobool(buf, &disabled);
|
| 131 | if (rc)
|
| 132 | return rc;
|
| 133 |
|
| 134 | hub_get(hub);
|
| 135 | rc = usb_autopm_get_interface(intf);
|
| 136 | if (rc < 0)
|
| 137 | goto out_hub_get;
|
| 138 |
|
| 139 | |
| 140 | * Prevent deadlock if another process is concurrently
|
| 141 | * trying to unregister hdev.
|
| 142 |
|
| 143 | kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
|
| 144 | if (!kn) {
|
| 145 | rc = -ENODEV;
|
| 146 | goto out_autopm;
|
| 147 | }
|
| 148 | usb_lock_device(hdev);
|
| 149 | if (hub->disconnected) {
|
| 150 | rc = -ENODEV;
|
| 151 | goto out_hdev_lock;
|
| 152 | }
|
| 153 |
|
| 154 | if (disabled && port_dev->child)
|
| 155 | usb_disconnect(&port_dev->child);
|
| 156 |
|
| 157 | rc = usb_hub_set_port_power(hdev, hub, port1, !disabled);
|
| 158 | msleep(2 * hub_power_on_good_delay(hub));
|
| 159 |
|
| 160 | if (disabled) {
|
| 161 | usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
|
| 162 | if (!port_dev->is_superspeed)
|
| 163 | usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
|
| 164 | }
|
| 165 |
|
| 166 | if (!rc)
|
| 167 | rc = count;
|
| 168 |
|
| 169 | out_hdev_lock:
|
| 170 | usb_unlock_device(hdev);
|
| 171 | sysfs_unbreak_active_protection(kn);
|
| 172 | out_autopm:
|
| 173 | usb_autopm_put_interface(intf);
|
| 174 | out_hub_get:
|
| 175 | hub_put(hub);
|
| 176 |
|
| 177 | return rc;
|
| 178 | }
|
| 179 | static DEVICE_ATTR_RW(disable);
|
| 180 |
|
| 181 | static ssize_t location_show(struct device *dev,
|
| 182 | struct device_attribute *attr, char *buf)
|
| 183 | {
|
| 184 | struct usb_port *port_dev = to_usb_port(dev);
|
| 185 |
|
| 186 | return sysfs_emit(buf, "0x%08x\n", port_dev->location);
|
| 187 | }
|
| 188 | static DEVICE_ATTR_RO(location);
|
| 189 |
|
| 190 | static ssize_t connect_type_show(struct device *dev,
|
| 191 | struct device_attribute *attr, char *buf)
|
| 192 | {
|
| 193 | struct usb_port *port_dev = to_usb_port(dev);
|
| 194 | char *result;
|
| 195 |
|
| 196 | switch (port_dev->connect_type) {
|
| 197 | case USB_PORT_CONNECT_TYPE_HOT_PLUG:
|
| 198 | result = "hotplug";
|
| 199 | break;
|
| 200 | case USB_PORT_CONNECT_TYPE_HARD_WIRED:
|
| 201 | result = "hardwired";
|
| 202 | break;
|
| 203 | case USB_PORT_NOT_USED:
|
| 204 | result = "not used";
|
| 205 | break;
|
| 206 | default:
|
| 207 | result = "unknown";
|
| 208 | break;
|
| 209 | }
|
| 210 |
|
| 211 | return sysfs_emit(buf, "%s\n", result);
|
| 212 | }
|
| 213 | static DEVICE_ATTR_RO(connect_type);
|
| 214 |
|
| 215 | static ssize_t state_show(struct device *dev,
|
| 216 | struct device_attribute *attr, char *buf)
|
| 217 | {
|
| 218 | struct usb_port *port_dev = to_usb_port(dev);
|
| 219 | enum usb_device_state state = READ_ONCE(port_dev->state);
|
| 220 |
|
| 221 | return sysfs_emit(buf, "%s\n", usb_state_string(state));
|
| 222 | }
|
| 223 | static DEVICE_ATTR_RO(state);
|
| 224 |
|
| 225 | static ssize_t over_current_count_show(struct device *dev,
|
| 226 | struct device_attribute *attr, char *buf)
|
| 227 | {
|
| 228 | struct usb_port *port_dev = to_usb_port(dev);
|
| 229 |
|
| 230 | return sysfs_emit(buf, "%u\n", port_dev->over_current_count);
|
| 231 | }
|
| 232 | static DEVICE_ATTR_RO(over_current_count);
|
| 233 |
|
| 234 | static ssize_t quirks_show(struct device *dev,
|
| 235 | struct device_attribute *attr, char *buf)
|
| 236 | {
|
| 237 | struct usb_port *port_dev = to_usb_port(dev);
|
| 238 |
|
| 239 | return sysfs_emit(buf, "%08x\n", port_dev->quirks);
|
| 240 | }
|
| 241 |
|
| 242 | static ssize_t quirks_store(struct device *dev, struct device_attribute *attr,
|
| 243 | const char *buf, size_t count)
|
| 244 | {
|
| 245 | struct usb_port *port_dev = to_usb_port(dev);
|
| 246 | u32 value;
|
| 247 |
|
| 248 | if (kstrtou32(buf, 16, &value))
|
| 249 | return -EINVAL;
|
| 250 |
|
| 251 | port_dev->quirks = value;
|
| 252 | return count;
|
| 253 | }
|
| 254 | static DEVICE_ATTR_RW(quirks);
|
| 255 |
|
| 256 | static ssize_t usb3_lpm_permit_show(struct device *dev,
|
| 257 | struct device_attribute *attr, char *buf)
|
| 258 | {
|
| 259 | struct usb_port *port_dev = to_usb_port(dev);
|
| 260 | const char *p;
|
| 261 |
|
| 262 | if (port_dev->usb3_lpm_u1_permit) {
|
| 263 | if (port_dev->usb3_lpm_u2_permit)
|
| 264 | p = "u1_u2";
|
| 265 | else
|
| 266 | p = "u1";
|
| 267 | } else {
|
| 268 | if (port_dev->usb3_lpm_u2_permit)
|
| 269 | p = "u2";
|
| 270 | else
|
| 271 | p = "0";
|
| 272 | }
|
| 273 |
|
| 274 | return sysfs_emit(buf, "%s\n", p);
|
| 275 | }
|
| 276 |
|
| 277 | static ssize_t usb3_lpm_permit_store(struct device *dev,
|
| 278 | struct device_attribute *attr,
|
| 279 | const char *buf, size_t count)
|
| 280 | {
|
| 281 | struct usb_port *port_dev = to_usb_port(dev);
|
| 282 | struct usb_device *udev = port_dev->child;
|
| 283 | struct usb_hcd *hcd;
|
| 284 |
|
| 285 | if (!strncmp(buf, "u1_u2", 5)) {
|
| 286 | port_dev->usb3_lpm_u1_permit = 1;
|
| 287 | port_dev->usb3_lpm_u2_permit = 1;
|
| 288 |
|
| 289 | } else if (!strncmp(buf, "u1", 2)) {
|
| 290 | port_dev->usb3_lpm_u1_permit = 1;
|
| 291 | port_dev->usb3_lpm_u2_permit = 0;
|
| 292 |
|
| 293 | } else if (!strncmp(buf, "u2", 2)) {
|
| 294 | port_dev->usb3_lpm_u1_permit = 0;
|
| 295 | port_dev->usb3_lpm_u2_permit = 1;
|
| 296 |
|
| 297 | } else if (!strncmp(buf, "0", 1)) {
|
| 298 | port_dev->usb3_lpm_u1_permit = 0;
|
| 299 | port_dev->usb3_lpm_u2_permit = 0;
|
| 300 | } else
|
| 301 | return -EINVAL;
|
| 302 |
|
| 303 | |
| 304 | * to make new u1 u2 setting take effect immediately.
|
| 305 |
|
| 306 | if (udev) {
|
| 307 | hcd = bus_to_hcd(udev->bus);
|
| 308 | if (!hcd)
|
| 309 | return -EINVAL;
|
| 310 | usb_lock_device(udev);
|
| 311 | mutex_lock(hcd->bandwidth_mutex);
|
| 312 | if (!usb_disable_lpm(udev))
|
| 313 | usb_enable_lpm(udev);
|
| 314 | mutex_unlock(hcd->bandwidth_mutex);
|
| 315 | usb_unlock_device(udev);
|
| 316 | }
|
| 317 |
|
| 318 | return count;
|
| 319 | }
|
| 320 | static DEVICE_ATTR_RW(usb3_lpm_permit);
|
| 321 |
|
| 322 | static struct attribute *port_dev_attrs[] = {
|
| 323 | &dev_attr_connect_type.attr,
|
| 324 | &dev_attr_state.attr,
|
| 325 | &dev_attr_location.attr,
|
| 326 | &dev_attr_quirks.attr,
|
| 327 | &dev_attr_over_current_count.attr,
|
| 328 | &dev_attr_disable.attr,
|
| 329 | &dev_attr_early_stop.attr,
|
| 330 | NULL,
|
| 331 | };
|
| 332 |
|
| 333 | static const struct attribute_group port_dev_attr_grp = {
|
| 334 | .attrs = port_dev_attrs,
|
| 335 | };
|
| 336 |
|
| 337 | static const struct attribute_group *port_dev_group[] = {
|
| 338 | &port_dev_attr_grp,
|
| 339 | NULL,
|
| 340 | };
|
| 341 |
|
| 342 | static struct attribute *port_dev_usb3_attrs[] = {
|
| 343 | &dev_attr_usb3_lpm_permit.attr,
|
| 344 | NULL,
|
| 345 | };
|
| 346 |
|
| 347 | static const struct attribute_group port_dev_usb3_attr_grp = {
|
| 348 | .attrs = port_dev_usb3_attrs,
|
| 349 | };
|
| 350 |
|
| 351 | static const struct attribute_group *port_dev_usb3_group[] = {
|
| 352 | &port_dev_attr_grp,
|
| 353 | &port_dev_usb3_attr_grp,
|
| 354 | NULL,
|
| 355 | };
|
| 356 |
|
| 357 | static void usb_port_device_release(struct device *dev)
|
| 358 | {
|
| 359 | struct usb_port *port_dev = to_usb_port(dev);
|
| 360 |
|
| 361 | kfree(port_dev->req);
|
| 362 | kfree(port_dev);
|
| 363 | }
|
| 364 |
|
| 365 | #ifdef CONFIG_PM
|
| 366 | static int usb_port_runtime_resume(struct device *dev)
|
| 367 | {
|
| 368 | struct usb_port *port_dev = to_usb_port(dev);
|
| 369 | struct usb_device *hdev = to_usb_device(dev->parent->parent);
|
| 370 | struct usb_interface *intf = to_usb_interface(dev->parent);
|
| 371 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
|
| 372 | struct usb_device *udev = port_dev->child;
|
| 373 | struct usb_port *peer = port_dev->peer;
|
| 374 | int port1 = port_dev->portnum;
|
| 375 | int retval;
|
| 376 |
|
| 377 | if (!hub)
|
| 378 | return -EINVAL;
|
| 379 | if (hub->in_reset) {
|
| 380 | set_bit(port1, hub->power_bits);
|
| 381 | return 0;
|
| 382 | }
|
| 383 |
|
| 384 | |
| 385 | * Power on our usb3 peer before this usb2 port to prevent a usb3
|
| 386 | * device from degrading to its usb2 connection
|
| 387 |
|
| 388 | if (!port_dev->is_superspeed && peer)
|
| 389 | pm_runtime_get_sync(&peer->dev);
|
| 390 |
|
| 391 | retval = usb_autopm_get_interface(intf);
|
| 392 | if (retval < 0)
|
| 393 | return retval;
|
| 394 |
|
| 395 | retval = usb_hub_set_port_power(hdev, hub, port1, true);
|
| 396 | msleep(hub_power_on_good_delay(hub));
|
| 397 | if (udev && !retval) {
|
| 398 | |
| 399 | * Our preference is to simply wait for the port to reconnect,
|
| 400 | * as that is the lowest latency method to restart the port.
|
| 401 | * However, there are cases where toggling port power results in
|
| 402 | * the host port and the device port getting out of sync causing
|
| 403 | * a link training live lock. Upon timeout, flag the port as
|
| 404 | * needing warm reset recovery (to be performed later by
|
| 405 | * usb_port_resume() as requested via usb_wakeup_notification())
|
| 406 |
|
| 407 | if (hub_port_debounce_be_connected(hub, port1) < 0) {
|
| 408 | dev_dbg(&port_dev->dev, "reconnect timeout\n");
|
| 409 | if (hub_is_superspeed(hdev))
|
| 410 | set_bit(port1, hub->warm_reset_bits);
|
| 411 | }
|
| 412 |
|
| 413 |
|
| 414 | if (!test_and_set_bit(port1, hub->child_usage_bits)) {
|
| 415 | pm_runtime_get_noresume(&port_dev->dev);
|
| 416 | pm_request_resume(&udev->dev);
|
| 417 | }
|
| 418 | }
|
| 419 |
|
| 420 | usb_autopm_put_interface(intf);
|
| 421 |
|
| 422 | return retval;
|
| 423 | }
|
| 424 |
|
| 425 | static int usb_port_runtime_suspend(struct device *dev)
|
| 426 | {
|
| 427 | struct usb_port *port_dev = to_usb_port(dev);
|
| 428 | struct usb_device *hdev = to_usb_device(dev->parent->parent);
|
| 429 | struct usb_interface *intf = to_usb_interface(dev->parent);
|
| 430 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
|
| 431 | struct usb_port *peer = port_dev->peer;
|
| 432 | int port1 = port_dev->portnum;
|
| 433 | int retval;
|
| 434 |
|
| 435 | if (!hub)
|
| 436 | return -EINVAL;
|
| 437 | if (hub->in_reset)
|
| 438 | return -EBUSY;
|
| 439 |
|
| 440 | if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
|
| 441 | == PM_QOS_FLAGS_ALL)
|
| 442 | return -EAGAIN;
|
| 443 |
|
| 444 | if (usb_port_block_power_off)
|
| 445 | return -EBUSY;
|
| 446 |
|
| 447 | retval = usb_autopm_get_interface(intf);
|
| 448 | if (retval < 0)
|
| 449 | return retval;
|
| 450 |
|
| 451 | retval = usb_hub_set_port_power(hdev, hub, port1, false);
|
| 452 | usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
|
| 453 | if (!port_dev->is_superspeed)
|
| 454 | usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
|
| 455 | usb_autopm_put_interface(intf);
|
| 456 |
|
| 457 | |
| 458 | * Our peer usb3 port may now be able to suspend, so
|
| 459 | * asynchronously queue a suspend request to observe that this
|
| 460 | * usb2 port is now off.
|
| 461 |
|
| 462 | if (!port_dev->is_superspeed && peer)
|
| 463 | pm_runtime_put(&peer->dev);
|
| 464 |
|
| 465 | return retval;
|
| 466 | }
|
| 467 | #endif
|
| 468 |
|
| 469 | static void usb_port_shutdown(struct device *dev)
|
| 470 | {
|
| 471 | struct usb_port *port_dev = to_usb_port(dev);
|
| 472 | struct usb_device *udev = port_dev->child;
|
| 473 |
|
| 474 | if (udev && !udev->port_is_suspended) {
|
| 475 | usb_disable_usb2_hardware_lpm(udev);
|
| 476 | usb_unlocked_disable_lpm(udev);
|
| 477 | }
|
| 478 | }
|
| 479 |
|
| 480 | static const struct dev_pm_ops usb_port_pm_ops = {
|
| 481 | #ifdef CONFIG_PM
|
| 482 | .runtime_suspend = usb_port_runtime_suspend,
|
| 483 | .runtime_resume = usb_port_runtime_resume,
|
| 484 | #endif
|
| 485 | };
|
| 486 |
|
| 487 | const struct device_type usb_port_device_type = {
|
| 488 | .name = "usb_port",
|
| 489 | .release = usb_port_device_release,
|
| 490 | .pm = &usb_port_pm_ops,
|
| 491 | };
|
| 492 |
|
| 493 | static struct device_driver usb_port_driver = {
|
| 494 | .name = "usb",
|
| 495 | .owner = THIS_MODULE,
|
| 496 | .shutdown = usb_port_shutdown,
|
| 497 | };
|
| 498 |
|
| 499 | static int link_peers(struct usb_port *left, struct usb_port *right)
|
| 500 | {
|
| 501 | struct usb_port *ss_port, *hs_port;
|
| 502 | int rc;
|
| 503 |
|
| 504 | if (left->peer == right && right->peer == left)
|
| 505 | return 0;
|
| 506 |
|
| 507 | if (left->peer || right->peer) {
|
| 508 | struct usb_port *lpeer = left->peer;
|
| 509 | struct usb_port *rpeer = right->peer;
|
| 510 | char *method;
|
| 511 |
|
| 512 | if (left->location && left->location == right->location)
|
| 513 | method = "location";
|
| 514 | else
|
| 515 | method = "default";
|
| 516 |
|
| 517 | pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
|
| 518 | dev_name(&left->dev), dev_name(&right->dev), method,
|
| 519 | dev_name(&left->dev),
|
| 520 | lpeer ? dev_name(&lpeer->dev) : "none",
|
| 521 | dev_name(&right->dev),
|
| 522 | rpeer ? dev_name(&rpeer->dev) : "none");
|
| 523 | return -EBUSY;
|
| 524 | }
|
| 525 |
|
| 526 | rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
|
| 527 | if (rc)
|
| 528 | return rc;
|
| 529 | rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
|
| 530 | if (rc) {
|
| 531 | sysfs_remove_link(&left->dev.kobj, "peer");
|
| 532 | return rc;
|
| 533 | }
|
| 534 |
|
| 535 | |
| 536 | * We need to wake the HiSpeed port to make sure we don't race
|
| 537 | * setting ->peer with usb_port_runtime_suspend(). Otherwise we
|
| 538 | * may miss a suspend event for the SuperSpeed port.
|
| 539 |
|
| 540 | if (left->is_superspeed) {
|
| 541 | ss_port = left;
|
| 542 | WARN_ON(right->is_superspeed);
|
| 543 | hs_port = right;
|
| 544 | } else {
|
| 545 | ss_port = right;
|
| 546 | WARN_ON(!right->is_superspeed);
|
| 547 | hs_port = left;
|
| 548 | }
|
| 549 | pm_runtime_get_sync(&hs_port->dev);
|
| 550 |
|
| 551 | left->peer = right;
|
| 552 | right->peer = left;
|
| 553 |
|
| 554 | |
| 555 | * The SuperSpeed reference is dropped when the HiSpeed port in
|
| 556 | * this relationship suspends, i.e. when it is safe to allow a
|
| 557 | * SuperSpeed connection to drop since there is no risk of a
|
| 558 | * device degrading to its powered-off HiSpeed connection.
|
| 559 | *
|
| 560 | * Also, drop the HiSpeed ref taken above.
|
| 561 |
|
| 562 | pm_runtime_get_sync(&ss_port->dev);
|
| 563 | pm_runtime_put(&hs_port->dev);
|
| 564 |
|
| 565 | return 0;
|
| 566 | }
|
| 567 |
|
| 568 | static void link_peers_report(struct usb_port *left, struct usb_port *right)
|
| 569 | {
|
| 570 | int rc;
|
| 571 |
|
| 572 | rc = link_peers(left, right);
|
| 573 | if (rc == 0) {
|
| 574 | dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
|
| 575 | } else {
|
| 576 | dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
|
| 577 | dev_name(&right->dev), rc);
|
| 578 | pr_warn_once("usb: port power management may be unreliable\n");
|
| 579 | usb_port_block_power_off = 1;
|
| 580 | }
|
| 581 | }
|
| 582 |
|
| 583 | static void unlink_peers(struct usb_port *left, struct usb_port *right)
|
| 584 | {
|
| 585 | struct usb_port *ss_port, *hs_port;
|
| 586 |
|
| 587 | WARN(right->peer != left || left->peer != right,
|
| 588 | "%s and %s are not peers?\n",
|
| 589 | dev_name(&left->dev), dev_name(&right->dev));
|
| 590 |
|
| 591 | |
| 592 | * We wake the HiSpeed port to make sure we don't race its
|
| 593 | * usb_port_runtime_resume() event which takes a SuperSpeed ref
|
| 594 | * when ->peer is !NULL.
|
| 595 |
|
| 596 | if (left->is_superspeed) {
|
| 597 | ss_port = left;
|
| 598 | hs_port = right;
|
| 599 | } else {
|
| 600 | ss_port = right;
|
| 601 | hs_port = left;
|
| 602 | }
|
| 603 |
|
| 604 | pm_runtime_get_sync(&hs_port->dev);
|
| 605 |
|
| 606 | sysfs_remove_link(&left->dev.kobj, "peer");
|
| 607 | right->peer = NULL;
|
| 608 | sysfs_remove_link(&right->dev.kobj, "peer");
|
| 609 | left->peer = NULL;
|
| 610 |
|
| 611 |
|
| 612 | pm_runtime_put(&ss_port->dev);
|
| 613 |
|
| 614 |
|
| 615 | pm_runtime_put(&hs_port->dev);
|
| 616 | }
|
| 617 |
|
| 618 | |
| 619 | * For each usb hub device in the system check to see if it is in the
|
| 620 | * peer domain of the given port_dev, and if it is check to see if it
|
| 621 | * has a port that matches the given port by location
|
| 622 |
|
| 623 | static int match_location(struct usb_device *peer_hdev, void *p)
|
| 624 | {
|
| 625 | int port1;
|
| 626 | struct usb_hcd *hcd, *peer_hcd;
|
| 627 | struct usb_port *port_dev = p, *peer;
|
| 628 | struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
|
| 629 | struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
|
| 630 |
|
| 631 | if (!peer_hub || port_dev->connect_type == USB_PORT_NOT_USED)
|
| 632 | return 0;
|
| 633 |
|
| 634 | hcd = bus_to_hcd(hdev->bus);
|
| 635 | peer_hcd = bus_to_hcd(peer_hdev->bus);
|
| 636 |
|
| 637 | if (peer_hcd != hcd->shared_hcd)
|
| 638 | return 0;
|
| 639 |
|
| 640 | for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
|
| 641 | peer = peer_hub->ports[port1 - 1];
|
| 642 | if (peer && peer->connect_type != USB_PORT_NOT_USED &&
|
| 643 | peer->location == port_dev->location) {
|
| 644 | link_peers_report(port_dev, peer);
|
| 645 | return 1;
|
| 646 | }
|
| 647 | }
|
| 648 |
|
| 649 | return 0;
|
| 650 | }
|
| 651 |
|
| 652 | |
| 653 | * Find the peer port either via explicit platform firmware "location"
|
| 654 | * data, the peer hcd for root hubs, or the upstream peer relationship
|
| 655 | * for all other hubs.
|
| 656 |
|
| 657 | static void find_and_link_peer(struct usb_hub *hub, int port1)
|
| 658 | {
|
| 659 | struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
|
| 660 | struct usb_device *hdev = hub->hdev;
|
| 661 | struct usb_device *peer_hdev;
|
| 662 | struct usb_hub *peer_hub;
|
| 663 |
|
| 664 | |
| 665 | * If location data is available then we can only peer this port
|
| 666 | * by a location match, not the default peer (lest we create a
|
| 667 | * situation where we need to go back and undo a default peering
|
| 668 | * when the port is later peered by location data)
|
| 669 |
|
| 670 | if (port_dev->location) {
|
| 671 |
|
| 672 | usb_for_each_dev(port_dev, match_location);
|
| 673 | return;
|
| 674 | } else if (!hdev->parent) {
|
| 675 | struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
|
| 676 | struct usb_hcd *peer_hcd = hcd->shared_hcd;
|
| 677 |
|
| 678 | if (!peer_hcd)
|
| 679 | return;
|
| 680 |
|
| 681 | peer_hdev = peer_hcd->self.root_hub;
|
| 682 | } else {
|
| 683 | struct usb_port *upstream;
|
| 684 | struct usb_device *parent = hdev->parent;
|
| 685 | struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
|
| 686 |
|
| 687 | if (!parent_hub)
|
| 688 | return;
|
| 689 |
|
| 690 | upstream = parent_hub->ports[hdev->portnum - 1];
|
| 691 | if (!upstream || !upstream->peer)
|
| 692 | return;
|
| 693 |
|
| 694 | peer_hdev = upstream->peer->child;
|
| 695 | }
|
| 696 |
|
| 697 | peer_hub = usb_hub_to_struct_hub(peer_hdev);
|
| 698 | if (!peer_hub || port1 > peer_hdev->maxchild)
|
| 699 | return;
|
| 700 |
|
| 701 | |
| 702 | * we found a valid default peer, last check is to make sure it
|
| 703 | * does not have location data
|
| 704 |
|
| 705 | peer = peer_hub->ports[port1 - 1];
|
| 706 | if (peer && peer->location == 0)
|
| 707 | link_peers_report(port_dev, peer);
|
| 708 | }
|
| 709 |
|
| 710 | static int connector_bind(struct device *dev, struct device *connector, void *data)
|
| 711 | {
|
| 712 | struct usb_port *port_dev = to_usb_port(dev);
|
| 713 | int ret;
|
| 714 |
|
| 715 | ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector");
|
| 716 | if (ret)
|
| 717 | return ret;
|
| 718 |
|
| 719 | ret = sysfs_create_link(&connector->kobj, &dev->kobj, dev_name(dev));
|
| 720 | if (ret) {
|
| 721 | sysfs_remove_link(&dev->kobj, "connector");
|
| 722 | return ret;
|
| 723 | }
|
| 724 |
|
| 725 | port_dev->connector = data;
|
| 726 |
|
| 727 | |
| 728 | * If there is already USB device connected to the port, letting the
|
| 729 | * Type-C connector know about it immediately.
|
| 730 |
|
| 731 | if (port_dev->child)
|
| 732 | typec_attach(port_dev->connector, &port_dev->child->dev);
|
| 733 |
|
| 734 | return 0;
|
| 735 | }
|
| 736 |
|
| 737 | static void connector_unbind(struct device *dev, struct device *connector, void *data)
|
| 738 | {
|
| 739 | struct usb_port *port_dev = to_usb_port(dev);
|
| 740 |
|
| 741 | sysfs_remove_link(&connector->kobj, dev_name(dev));
|
| 742 | sysfs_remove_link(&dev->kobj, "connector");
|
| 743 | if (port_dev->child)
|
| 744 | typec_deattach(port_dev->connector, &port_dev->child->dev);
|
| 745 | port_dev->connector = NULL;
|
| 746 | }
|
| 747 |
|
| 748 | static const struct component_ops connector_ops = {
|
| 749 | .bind = connector_bind,
|
| 750 | .unbind = connector_unbind,
|
| 751 | };
|
| 752 |
|
| 753 | int usb_hub_create_port_device(struct usb_hub *hub, int port1)
|
| 754 | {
|
| 755 | struct usb_port *port_dev;
|
| 756 | struct usb_device *hdev = hub->hdev;
|
| 757 | int retval;
|
| 758 |
|
| 759 | port_dev = kzalloc_obj(*port_dev);
|
| 760 | if (!port_dev)
|
| 761 | return -ENOMEM;
|
| 762 |
|
| 763 | port_dev->req = kzalloc_obj(*(port_dev->req));
|
| 764 | if (!port_dev->req) {
|
| 765 | kfree(port_dev);
|
| 766 | return -ENOMEM;
|
| 767 | }
|
| 768 |
|
| 769 | port_dev->connect_type = usb_of_get_connect_type(hdev, port1);
|
| 770 | hub->ports[port1 - 1] = port_dev;
|
| 771 | port_dev->portnum = port1;
|
| 772 | set_bit(port1, hub->power_bits);
|
| 773 | port_dev->dev.parent = hub->intfdev;
|
| 774 | if (hub_is_superspeed(hdev)) {
|
| 775 | port_dev->is_superspeed = 1;
|
| 776 | port_dev->usb3_lpm_u1_permit = 1;
|
| 777 | port_dev->usb3_lpm_u2_permit = 1;
|
| 778 | port_dev->dev.groups = port_dev_usb3_group;
|
| 779 | } else
|
| 780 | port_dev->dev.groups = port_dev_group;
|
| 781 | port_dev->dev.type = &usb_port_device_type;
|
| 782 | port_dev->dev.driver = &usb_port_driver;
|
| 783 | dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
|
| 784 | port1);
|
| 785 | mutex_init(&port_dev->status_lock);
|
| 786 | retval = device_register(&port_dev->dev);
|
| 787 | if (retval) {
|
| 788 | put_device(&port_dev->dev);
|
| 789 | return retval;
|
| 790 | }
|
| 791 |
|
| 792 | port_dev->state_kn = sysfs_get_dirent(port_dev->dev.kobj.sd, "state");
|
| 793 | if (!port_dev->state_kn) {
|
| 794 | dev_err(&port_dev->dev, "failed to sysfs_get_dirent 'state'\n");
|
| 795 | retval = -ENODEV;
|
| 796 | goto err_unregister;
|
| 797 | }
|
| 798 |
|
| 799 |
|
| 800 | retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
|
| 801 | DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
|
| 802 | if (retval < 0) {
|
| 803 | goto err_put_kn;
|
| 804 | }
|
| 805 |
|
| 806 | retval = component_add(&port_dev->dev, &connector_ops);
|
| 807 | if (retval) {
|
| 808 | dev_warn(&port_dev->dev, "failed to add component\n");
|
| 809 | goto err_put_kn;
|
| 810 | }
|
| 811 |
|
| 812 | find_and_link_peer(hub, port1);
|
| 813 |
|
| 814 | |
| 815 | * Enable runtime pm and hold a refernce that hub_configure()
|
| 816 | * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
|
| 817 | * and the hub has been fully registered (hdev->maxchild set).
|
| 818 |
|
| 819 | pm_runtime_set_active(&port_dev->dev);
|
| 820 | pm_runtime_get_noresume(&port_dev->dev);
|
| 821 | pm_runtime_enable(&port_dev->dev);
|
| 822 | device_enable_async_suspend(&port_dev->dev);
|
| 823 |
|
| 824 | |
| 825 | * Keep hidden the ability to enable port-poweroff if neither the
|
| 826 | * USB hub nor platform firmware can manage downstream port power.
|
| 827 |
|
| 828 | if (!usb_port_allow_power_off(hdev, hub, port_dev))
|
| 829 | return 0;
|
| 830 |
|
| 831 |
|
| 832 | retval = dev_pm_qos_expose_flags(&port_dev->dev,
|
| 833 | PM_QOS_FLAG_NO_POWER_OFF);
|
| 834 | if (retval < 0) {
|
| 835 | dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
|
| 836 | return 0;
|
| 837 | }
|
| 838 |
|
| 839 |
|
| 840 | retval = dev_pm_qos_remove_request(port_dev->req);
|
| 841 | if (retval >= 0) {
|
| 842 | kfree(port_dev->req);
|
| 843 | port_dev->req = NULL;
|
| 844 | }
|
| 845 | return 0;
|
| 846 |
|
| 847 | err_put_kn:
|
| 848 | sysfs_put(port_dev->state_kn);
|
| 849 | err_unregister:
|
| 850 | device_unregister(&port_dev->dev);
|
| 851 |
|
| 852 | return retval;
|
| 853 | }
|
| 854 |
|
| 855 | void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
|
| 856 | {
|
| 857 | struct usb_port *port_dev = hub->ports[port1 - 1];
|
| 858 | struct usb_port *peer;
|
| 859 |
|
| 860 | peer = port_dev->peer;
|
| 861 | if (peer)
|
| 862 | unlink_peers(port_dev, peer);
|
| 863 | component_del(&port_dev->dev, &connector_ops);
|
| 864 | sysfs_put(port_dev->state_kn);
|
| 865 | device_unregister(&port_dev->dev);
|
| 866 | }
|