Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / usb / core / port.c
blob1a01e9ad38049c7928a618a9941d8950cf21556c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * usb port device code
5 * Copyright (C) 2012 Intel Corp
7 * Author: Lan Tianyu <tianyu.lan@intel.com>
8 */
10 #include <linux/slab.h>
11 #include <linux/pm_qos.h>
13 #include "hub.h"
15 static int usb_port_block_power_off;
17 static const struct attribute_group *port_dev_group[];
19 static ssize_t connect_type_show(struct device *dev,
20 struct device_attribute *attr, char *buf)
22 struct usb_port *port_dev = to_usb_port(dev);
23 char *result;
25 switch (port_dev->connect_type) {
26 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
27 result = "hotplug";
28 break;
29 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
30 result = "hardwired";
31 break;
32 case USB_PORT_NOT_USED:
33 result = "not used";
34 break;
35 default:
36 result = "unknown";
37 break;
40 return sprintf(buf, "%s\n", result);
42 static DEVICE_ATTR_RO(connect_type);
44 static ssize_t usb3_lpm_permit_show(struct device *dev,
45 struct device_attribute *attr, char *buf)
47 struct usb_port *port_dev = to_usb_port(dev);
48 const char *p;
50 if (port_dev->usb3_lpm_u1_permit) {
51 if (port_dev->usb3_lpm_u2_permit)
52 p = "u1_u2";
53 else
54 p = "u1";
55 } else {
56 if (port_dev->usb3_lpm_u2_permit)
57 p = "u2";
58 else
59 p = "0";
62 return sprintf(buf, "%s\n", p);
65 static ssize_t usb3_lpm_permit_store(struct device *dev,
66 struct device_attribute *attr,
67 const char *buf, size_t count)
69 struct usb_port *port_dev = to_usb_port(dev);
70 struct usb_device *udev = port_dev->child;
71 struct usb_hcd *hcd;
73 if (!strncmp(buf, "u1_u2", 5)) {
74 port_dev->usb3_lpm_u1_permit = 1;
75 port_dev->usb3_lpm_u2_permit = 1;
77 } else if (!strncmp(buf, "u1", 2)) {
78 port_dev->usb3_lpm_u1_permit = 1;
79 port_dev->usb3_lpm_u2_permit = 0;
81 } else if (!strncmp(buf, "u2", 2)) {
82 port_dev->usb3_lpm_u1_permit = 0;
83 port_dev->usb3_lpm_u2_permit = 1;
85 } else if (!strncmp(buf, "0", 1)) {
86 port_dev->usb3_lpm_u1_permit = 0;
87 port_dev->usb3_lpm_u2_permit = 0;
88 } else
89 return -EINVAL;
91 /* If device is connected to the port, disable or enable lpm
92 * to make new u1 u2 setting take effect immediately.
94 if (udev) {
95 hcd = bus_to_hcd(udev->bus);
96 if (!hcd)
97 return -EINVAL;
98 usb_lock_device(udev);
99 mutex_lock(hcd->bandwidth_mutex);
100 if (!usb_disable_lpm(udev))
101 usb_enable_lpm(udev);
102 mutex_unlock(hcd->bandwidth_mutex);
103 usb_unlock_device(udev);
106 return count;
108 static DEVICE_ATTR_RW(usb3_lpm_permit);
110 static struct attribute *port_dev_attrs[] = {
111 &dev_attr_connect_type.attr,
112 NULL,
115 static struct attribute_group port_dev_attr_grp = {
116 .attrs = port_dev_attrs,
119 static const struct attribute_group *port_dev_group[] = {
120 &port_dev_attr_grp,
121 NULL,
124 static struct attribute *port_dev_usb3_attrs[] = {
125 &dev_attr_usb3_lpm_permit.attr,
126 NULL,
129 static struct attribute_group port_dev_usb3_attr_grp = {
130 .attrs = port_dev_usb3_attrs,
133 static const struct attribute_group *port_dev_usb3_group[] = {
134 &port_dev_attr_grp,
135 &port_dev_usb3_attr_grp,
136 NULL,
139 static void usb_port_device_release(struct device *dev)
141 struct usb_port *port_dev = to_usb_port(dev);
143 kfree(port_dev->req);
144 kfree(port_dev);
147 #ifdef CONFIG_PM
148 static int usb_port_runtime_resume(struct device *dev)
150 struct usb_port *port_dev = to_usb_port(dev);
151 struct usb_device *hdev = to_usb_device(dev->parent->parent);
152 struct usb_interface *intf = to_usb_interface(dev->parent);
153 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
154 struct usb_device *udev = port_dev->child;
155 struct usb_port *peer = port_dev->peer;
156 int port1 = port_dev->portnum;
157 int retval;
159 if (!hub)
160 return -EINVAL;
161 if (hub->in_reset) {
162 set_bit(port1, hub->power_bits);
163 return 0;
167 * Power on our usb3 peer before this usb2 port to prevent a usb3
168 * device from degrading to its usb2 connection
170 if (!port_dev->is_superspeed && peer)
171 pm_runtime_get_sync(&peer->dev);
173 usb_autopm_get_interface(intf);
174 retval = usb_hub_set_port_power(hdev, hub, port1, true);
175 msleep(hub_power_on_good_delay(hub));
176 if (udev && !retval) {
178 * Our preference is to simply wait for the port to reconnect,
179 * as that is the lowest latency method to restart the port.
180 * However, there are cases where toggling port power results in
181 * the host port and the device port getting out of sync causing
182 * a link training live lock. Upon timeout, flag the port as
183 * needing warm reset recovery (to be performed later by
184 * usb_port_resume() as requested via usb_wakeup_notification())
186 if (hub_port_debounce_be_connected(hub, port1) < 0) {
187 dev_dbg(&port_dev->dev, "reconnect timeout\n");
188 if (hub_is_superspeed(hdev))
189 set_bit(port1, hub->warm_reset_bits);
192 /* Force the child awake to revalidate after the power loss. */
193 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
194 pm_runtime_get_noresume(&port_dev->dev);
195 pm_request_resume(&udev->dev);
199 usb_autopm_put_interface(intf);
201 return retval;
204 static int usb_port_runtime_suspend(struct device *dev)
206 struct usb_port *port_dev = to_usb_port(dev);
207 struct usb_device *hdev = to_usb_device(dev->parent->parent);
208 struct usb_interface *intf = to_usb_interface(dev->parent);
209 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
210 struct usb_port *peer = port_dev->peer;
211 int port1 = port_dev->portnum;
212 int retval;
214 if (!hub)
215 return -EINVAL;
216 if (hub->in_reset)
217 return -EBUSY;
219 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
220 == PM_QOS_FLAGS_ALL)
221 return -EAGAIN;
223 if (usb_port_block_power_off)
224 return -EBUSY;
226 usb_autopm_get_interface(intf);
227 retval = usb_hub_set_port_power(hdev, hub, port1, false);
228 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
229 if (!port_dev->is_superspeed)
230 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
231 usb_autopm_put_interface(intf);
234 * Our peer usb3 port may now be able to suspend, so
235 * asynchronously queue a suspend request to observe that this
236 * usb2 port is now off.
238 if (!port_dev->is_superspeed && peer)
239 pm_runtime_put(&peer->dev);
241 return retval;
243 #endif
245 static const struct dev_pm_ops usb_port_pm_ops = {
246 #ifdef CONFIG_PM
247 .runtime_suspend = usb_port_runtime_suspend,
248 .runtime_resume = usb_port_runtime_resume,
249 #endif
252 struct device_type usb_port_device_type = {
253 .name = "usb_port",
254 .release = usb_port_device_release,
255 .pm = &usb_port_pm_ops,
258 static struct device_driver usb_port_driver = {
259 .name = "usb",
260 .owner = THIS_MODULE,
263 static int link_peers(struct usb_port *left, struct usb_port *right)
265 struct usb_port *ss_port, *hs_port;
266 int rc;
268 if (left->peer == right && right->peer == left)
269 return 0;
271 if (left->peer || right->peer) {
272 struct usb_port *lpeer = left->peer;
273 struct usb_port *rpeer = right->peer;
274 char *method;
276 if (left->location && left->location == right->location)
277 method = "location";
278 else
279 method = "default";
281 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
282 dev_name(&left->dev), dev_name(&right->dev), method,
283 dev_name(&left->dev),
284 lpeer ? dev_name(&lpeer->dev) : "none",
285 dev_name(&right->dev),
286 rpeer ? dev_name(&rpeer->dev) : "none");
287 return -EBUSY;
290 rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
291 if (rc)
292 return rc;
293 rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
294 if (rc) {
295 sysfs_remove_link(&left->dev.kobj, "peer");
296 return rc;
300 * We need to wake the HiSpeed port to make sure we don't race
301 * setting ->peer with usb_port_runtime_suspend(). Otherwise we
302 * may miss a suspend event for the SuperSpeed port.
304 if (left->is_superspeed) {
305 ss_port = left;
306 WARN_ON(right->is_superspeed);
307 hs_port = right;
308 } else {
309 ss_port = right;
310 WARN_ON(!right->is_superspeed);
311 hs_port = left;
313 pm_runtime_get_sync(&hs_port->dev);
315 left->peer = right;
316 right->peer = left;
319 * The SuperSpeed reference is dropped when the HiSpeed port in
320 * this relationship suspends, i.e. when it is safe to allow a
321 * SuperSpeed connection to drop since there is no risk of a
322 * device degrading to its powered-off HiSpeed connection.
324 * Also, drop the HiSpeed ref taken above.
326 pm_runtime_get_sync(&ss_port->dev);
327 pm_runtime_put(&hs_port->dev);
329 return 0;
332 static void link_peers_report(struct usb_port *left, struct usb_port *right)
334 int rc;
336 rc = link_peers(left, right);
337 if (rc == 0) {
338 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
339 } else {
340 dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
341 dev_name(&right->dev), rc);
342 pr_warn_once("usb: port power management may be unreliable\n");
343 usb_port_block_power_off = 1;
347 static void unlink_peers(struct usb_port *left, struct usb_port *right)
349 struct usb_port *ss_port, *hs_port;
351 WARN(right->peer != left || left->peer != right,
352 "%s and %s are not peers?\n",
353 dev_name(&left->dev), dev_name(&right->dev));
356 * We wake the HiSpeed port to make sure we don't race its
357 * usb_port_runtime_resume() event which takes a SuperSpeed ref
358 * when ->peer is !NULL.
360 if (left->is_superspeed) {
361 ss_port = left;
362 hs_port = right;
363 } else {
364 ss_port = right;
365 hs_port = left;
368 pm_runtime_get_sync(&hs_port->dev);
370 sysfs_remove_link(&left->dev.kobj, "peer");
371 right->peer = NULL;
372 sysfs_remove_link(&right->dev.kobj, "peer");
373 left->peer = NULL;
375 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
376 pm_runtime_put(&ss_port->dev);
378 /* Drop the ref taken above */
379 pm_runtime_put(&hs_port->dev);
383 * For each usb hub device in the system check to see if it is in the
384 * peer domain of the given port_dev, and if it is check to see if it
385 * has a port that matches the given port by location
387 static int match_location(struct usb_device *peer_hdev, void *p)
389 int port1;
390 struct usb_hcd *hcd, *peer_hcd;
391 struct usb_port *port_dev = p, *peer;
392 struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
393 struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
395 if (!peer_hub)
396 return 0;
398 hcd = bus_to_hcd(hdev->bus);
399 peer_hcd = bus_to_hcd(peer_hdev->bus);
400 /* peer_hcd is provisional until we verify it against the known peer */
401 if (peer_hcd != hcd->shared_hcd)
402 return 0;
404 for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
405 peer = peer_hub->ports[port1 - 1];
406 if (peer && peer->location == port_dev->location) {
407 link_peers_report(port_dev, peer);
408 return 1; /* done */
412 return 0;
416 * Find the peer port either via explicit platform firmware "location"
417 * data, the peer hcd for root hubs, or the upstream peer relationship
418 * for all other hubs.
420 static void find_and_link_peer(struct usb_hub *hub, int port1)
422 struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
423 struct usb_device *hdev = hub->hdev;
424 struct usb_device *peer_hdev;
425 struct usb_hub *peer_hub;
428 * If location data is available then we can only peer this port
429 * by a location match, not the default peer (lest we create a
430 * situation where we need to go back and undo a default peering
431 * when the port is later peered by location data)
433 if (port_dev->location) {
434 /* we link the peer in match_location() if found */
435 usb_for_each_dev(port_dev, match_location);
436 return;
437 } else if (!hdev->parent) {
438 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
439 struct usb_hcd *peer_hcd = hcd->shared_hcd;
441 if (!peer_hcd)
442 return;
444 peer_hdev = peer_hcd->self.root_hub;
445 } else {
446 struct usb_port *upstream;
447 struct usb_device *parent = hdev->parent;
448 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
450 if (!parent_hub)
451 return;
453 upstream = parent_hub->ports[hdev->portnum - 1];
454 if (!upstream || !upstream->peer)
455 return;
457 peer_hdev = upstream->peer->child;
460 peer_hub = usb_hub_to_struct_hub(peer_hdev);
461 if (!peer_hub || port1 > peer_hdev->maxchild)
462 return;
465 * we found a valid default peer, last check is to make sure it
466 * does not have location data
468 peer = peer_hub->ports[port1 - 1];
469 if (peer && peer->location == 0)
470 link_peers_report(port_dev, peer);
473 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
475 struct usb_port *port_dev;
476 struct usb_device *hdev = hub->hdev;
477 int retval;
479 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
480 if (!port_dev)
481 return -ENOMEM;
483 port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
484 if (!port_dev->req) {
485 kfree(port_dev);
486 return -ENOMEM;
489 hub->ports[port1 - 1] = port_dev;
490 port_dev->portnum = port1;
491 set_bit(port1, hub->power_bits);
492 port_dev->dev.parent = hub->intfdev;
493 if (hub_is_superspeed(hdev)) {
494 port_dev->usb3_lpm_u1_permit = 1;
495 port_dev->usb3_lpm_u2_permit = 1;
496 port_dev->dev.groups = port_dev_usb3_group;
497 } else
498 port_dev->dev.groups = port_dev_group;
499 port_dev->dev.type = &usb_port_device_type;
500 port_dev->dev.driver = &usb_port_driver;
501 if (hub_is_superspeed(hub->hdev))
502 port_dev->is_superspeed = 1;
503 dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
504 port1);
505 mutex_init(&port_dev->status_lock);
506 retval = device_register(&port_dev->dev);
507 if (retval) {
508 put_device(&port_dev->dev);
509 return retval;
512 /* Set default policy of port-poweroff disabled. */
513 retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
514 DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
515 if (retval < 0) {
516 device_unregister(&port_dev->dev);
517 return retval;
520 find_and_link_peer(hub, port1);
523 * Enable runtime pm and hold a refernce that hub_configure()
524 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
525 * and the hub has been fully registered (hdev->maxchild set).
527 pm_runtime_set_active(&port_dev->dev);
528 pm_runtime_get_noresume(&port_dev->dev);
529 pm_runtime_enable(&port_dev->dev);
530 device_enable_async_suspend(&port_dev->dev);
533 * Keep hidden the ability to enable port-poweroff if the hub
534 * does not support power switching.
536 if (!hub_is_port_power_switchable(hub))
537 return 0;
539 /* Attempt to let userspace take over the policy. */
540 retval = dev_pm_qos_expose_flags(&port_dev->dev,
541 PM_QOS_FLAG_NO_POWER_OFF);
542 if (retval < 0) {
543 dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
544 return 0;
547 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
548 retval = dev_pm_qos_remove_request(port_dev->req);
549 if (retval >= 0) {
550 kfree(port_dev->req);
551 port_dev->req = NULL;
553 return 0;
556 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
558 struct usb_port *port_dev = hub->ports[port1 - 1];
559 struct usb_port *peer;
561 peer = port_dev->peer;
562 if (peer)
563 unlink_peers(port_dev, peer);
564 device_unregister(&port_dev->dev);