]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/usb/core/port.c
blk-mq: punt failed direct issue to dispatch list
[mirror_ubuntu-bionic-kernel.git] / drivers / usb / core / port.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * usb port device code
4 *
5 * Copyright (C) 2012 Intel Corp
6 *
7 * Author: Lan Tianyu <tianyu.lan@intel.com>
8 */
9
10 #include <linux/slab.h>
11 #include <linux/pm_qos.h>
12
13 #include "hub.h"
14
15 static int usb_port_block_power_off;
16
17 static const struct attribute_group *port_dev_group[];
18
19 static ssize_t connect_type_show(struct device *dev,
20 struct device_attribute *attr, char *buf)
21 {
22 struct usb_port *port_dev = to_usb_port(dev);
23 char *result;
24
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;
38 }
39
40 return sprintf(buf, "%s\n", result);
41 }
42 static DEVICE_ATTR_RO(connect_type);
43
44 static ssize_t usb3_lpm_permit_show(struct device *dev,
45 struct device_attribute *attr, char *buf)
46 {
47 struct usb_port *port_dev = to_usb_port(dev);
48 const char *p;
49
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";
60 }
61
62 return sprintf(buf, "%s\n", p);
63 }
64
65 static ssize_t usb3_lpm_permit_store(struct device *dev,
66 struct device_attribute *attr,
67 const char *buf, size_t count)
68 {
69 struct usb_port *port_dev = to_usb_port(dev);
70 struct usb_device *udev = port_dev->child;
71 struct usb_hcd *hcd;
72
73 if (!strncmp(buf, "u1_u2", 5)) {
74 port_dev->usb3_lpm_u1_permit = 1;
75 port_dev->usb3_lpm_u2_permit = 1;
76
77 } else if (!strncmp(buf, "u1", 2)) {
78 port_dev->usb3_lpm_u1_permit = 1;
79 port_dev->usb3_lpm_u2_permit = 0;
80
81 } else if (!strncmp(buf, "u2", 2)) {
82 port_dev->usb3_lpm_u1_permit = 0;
83 port_dev->usb3_lpm_u2_permit = 1;
84
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;
90
91 /* If device is connected to the port, disable or enable lpm
92 * to make new u1 u2 setting take effect immediately.
93 */
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);
104 }
105
106 return count;
107 }
108 static DEVICE_ATTR_RW(usb3_lpm_permit);
109
110 static struct attribute *port_dev_attrs[] = {
111 &dev_attr_connect_type.attr,
112 NULL,
113 };
114
115 static struct attribute_group port_dev_attr_grp = {
116 .attrs = port_dev_attrs,
117 };
118
119 static const struct attribute_group *port_dev_group[] = {
120 &port_dev_attr_grp,
121 NULL,
122 };
123
124 static struct attribute *port_dev_usb3_attrs[] = {
125 &dev_attr_usb3_lpm_permit.attr,
126 NULL,
127 };
128
129 static struct attribute_group port_dev_usb3_attr_grp = {
130 .attrs = port_dev_usb3_attrs,
131 };
132
133 static const struct attribute_group *port_dev_usb3_group[] = {
134 &port_dev_attr_grp,
135 &port_dev_usb3_attr_grp,
136 NULL,
137 };
138
139 static void usb_port_device_release(struct device *dev)
140 {
141 struct usb_port *port_dev = to_usb_port(dev);
142
143 kfree(port_dev->req);
144 kfree(port_dev);
145 }
146
147 #ifdef CONFIG_PM
148 static int usb_port_runtime_resume(struct device *dev)
149 {
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;
158
159 if (!hub)
160 return -EINVAL;
161 if (hub->in_reset) {
162 set_bit(port1, hub->power_bits);
163 return 0;
164 }
165
166 /*
167 * Power on our usb3 peer before this usb2 port to prevent a usb3
168 * device from degrading to its usb2 connection
169 */
170 if (!port_dev->is_superspeed && peer)
171 pm_runtime_get_sync(&peer->dev);
172
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) {
177 /*
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())
185 */
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);
190 }
191
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);
196 }
197 }
198
199 usb_autopm_put_interface(intf);
200
201 return retval;
202 }
203
204 static int usb_port_runtime_suspend(struct device *dev)
205 {
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;
213
214 if (!hub)
215 return -EINVAL;
216 if (hub->in_reset)
217 return -EBUSY;
218
219 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
220 == PM_QOS_FLAGS_ALL)
221 return -EAGAIN;
222
223 if (usb_port_block_power_off)
224 return -EBUSY;
225
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);
232
233 /*
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.
237 */
238 if (!port_dev->is_superspeed && peer)
239 pm_runtime_put(&peer->dev);
240
241 return retval;
242 }
243 #endif
244
245 static void usb_port_shutdown(struct device *dev)
246 {
247 struct usb_port *port_dev = to_usb_port(dev);
248
249 if (port_dev->child)
250 usb_disable_usb2_hardware_lpm(port_dev->child);
251 }
252
253 static const struct dev_pm_ops usb_port_pm_ops = {
254 #ifdef CONFIG_PM
255 .runtime_suspend = usb_port_runtime_suspend,
256 .runtime_resume = usb_port_runtime_resume,
257 #endif
258 };
259
260 struct device_type usb_port_device_type = {
261 .name = "usb_port",
262 .release = usb_port_device_release,
263 .pm = &usb_port_pm_ops,
264 };
265
266 static struct device_driver usb_port_driver = {
267 .name = "usb",
268 .owner = THIS_MODULE,
269 .shutdown = usb_port_shutdown,
270 };
271
272 static int link_peers(struct usb_port *left, struct usb_port *right)
273 {
274 struct usb_port *ss_port, *hs_port;
275 int rc;
276
277 if (left->peer == right && right->peer == left)
278 return 0;
279
280 if (left->peer || right->peer) {
281 struct usb_port *lpeer = left->peer;
282 struct usb_port *rpeer = right->peer;
283 char *method;
284
285 if (left->location && left->location == right->location)
286 method = "location";
287 else
288 method = "default";
289
290 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
291 dev_name(&left->dev), dev_name(&right->dev), method,
292 dev_name(&left->dev),
293 lpeer ? dev_name(&lpeer->dev) : "none",
294 dev_name(&right->dev),
295 rpeer ? dev_name(&rpeer->dev) : "none");
296 return -EBUSY;
297 }
298
299 rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
300 if (rc)
301 return rc;
302 rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
303 if (rc) {
304 sysfs_remove_link(&left->dev.kobj, "peer");
305 return rc;
306 }
307
308 /*
309 * We need to wake the HiSpeed port to make sure we don't race
310 * setting ->peer with usb_port_runtime_suspend(). Otherwise we
311 * may miss a suspend event for the SuperSpeed port.
312 */
313 if (left->is_superspeed) {
314 ss_port = left;
315 WARN_ON(right->is_superspeed);
316 hs_port = right;
317 } else {
318 ss_port = right;
319 WARN_ON(!right->is_superspeed);
320 hs_port = left;
321 }
322 pm_runtime_get_sync(&hs_port->dev);
323
324 left->peer = right;
325 right->peer = left;
326
327 /*
328 * The SuperSpeed reference is dropped when the HiSpeed port in
329 * this relationship suspends, i.e. when it is safe to allow a
330 * SuperSpeed connection to drop since there is no risk of a
331 * device degrading to its powered-off HiSpeed connection.
332 *
333 * Also, drop the HiSpeed ref taken above.
334 */
335 pm_runtime_get_sync(&ss_port->dev);
336 pm_runtime_put(&hs_port->dev);
337
338 return 0;
339 }
340
341 static void link_peers_report(struct usb_port *left, struct usb_port *right)
342 {
343 int rc;
344
345 rc = link_peers(left, right);
346 if (rc == 0) {
347 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
348 } else {
349 dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
350 dev_name(&right->dev), rc);
351 pr_warn_once("usb: port power management may be unreliable\n");
352 usb_port_block_power_off = 1;
353 }
354 }
355
356 static void unlink_peers(struct usb_port *left, struct usb_port *right)
357 {
358 struct usb_port *ss_port, *hs_port;
359
360 WARN(right->peer != left || left->peer != right,
361 "%s and %s are not peers?\n",
362 dev_name(&left->dev), dev_name(&right->dev));
363
364 /*
365 * We wake the HiSpeed port to make sure we don't race its
366 * usb_port_runtime_resume() event which takes a SuperSpeed ref
367 * when ->peer is !NULL.
368 */
369 if (left->is_superspeed) {
370 ss_port = left;
371 hs_port = right;
372 } else {
373 ss_port = right;
374 hs_port = left;
375 }
376
377 pm_runtime_get_sync(&hs_port->dev);
378
379 sysfs_remove_link(&left->dev.kobj, "peer");
380 right->peer = NULL;
381 sysfs_remove_link(&right->dev.kobj, "peer");
382 left->peer = NULL;
383
384 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
385 pm_runtime_put(&ss_port->dev);
386
387 /* Drop the ref taken above */
388 pm_runtime_put(&hs_port->dev);
389 }
390
391 /*
392 * For each usb hub device in the system check to see if it is in the
393 * peer domain of the given port_dev, and if it is check to see if it
394 * has a port that matches the given port by location
395 */
396 static int match_location(struct usb_device *peer_hdev, void *p)
397 {
398 int port1;
399 struct usb_hcd *hcd, *peer_hcd;
400 struct usb_port *port_dev = p, *peer;
401 struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
402 struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
403
404 if (!peer_hub)
405 return 0;
406
407 hcd = bus_to_hcd(hdev->bus);
408 peer_hcd = bus_to_hcd(peer_hdev->bus);
409 /* peer_hcd is provisional until we verify it against the known peer */
410 if (peer_hcd != hcd->shared_hcd)
411 return 0;
412
413 for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
414 peer = peer_hub->ports[port1 - 1];
415 if (peer && peer->location == port_dev->location) {
416 link_peers_report(port_dev, peer);
417 return 1; /* done */
418 }
419 }
420
421 return 0;
422 }
423
424 /*
425 * Find the peer port either via explicit platform firmware "location"
426 * data, the peer hcd for root hubs, or the upstream peer relationship
427 * for all other hubs.
428 */
429 static void find_and_link_peer(struct usb_hub *hub, int port1)
430 {
431 struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
432 struct usb_device *hdev = hub->hdev;
433 struct usb_device *peer_hdev;
434 struct usb_hub *peer_hub;
435
436 /*
437 * If location data is available then we can only peer this port
438 * by a location match, not the default peer (lest we create a
439 * situation where we need to go back and undo a default peering
440 * when the port is later peered by location data)
441 */
442 if (port_dev->location) {
443 /* we link the peer in match_location() if found */
444 usb_for_each_dev(port_dev, match_location);
445 return;
446 } else if (!hdev->parent) {
447 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
448 struct usb_hcd *peer_hcd = hcd->shared_hcd;
449
450 if (!peer_hcd)
451 return;
452
453 peer_hdev = peer_hcd->self.root_hub;
454 } else {
455 struct usb_port *upstream;
456 struct usb_device *parent = hdev->parent;
457 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
458
459 if (!parent_hub)
460 return;
461
462 upstream = parent_hub->ports[hdev->portnum - 1];
463 if (!upstream || !upstream->peer)
464 return;
465
466 peer_hdev = upstream->peer->child;
467 }
468
469 peer_hub = usb_hub_to_struct_hub(peer_hdev);
470 if (!peer_hub || port1 > peer_hdev->maxchild)
471 return;
472
473 /*
474 * we found a valid default peer, last check is to make sure it
475 * does not have location data
476 */
477 peer = peer_hub->ports[port1 - 1];
478 if (peer && peer->location == 0)
479 link_peers_report(port_dev, peer);
480 }
481
482 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
483 {
484 struct usb_port *port_dev;
485 struct usb_device *hdev = hub->hdev;
486 int retval;
487
488 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
489 if (!port_dev)
490 return -ENOMEM;
491
492 port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
493 if (!port_dev->req) {
494 kfree(port_dev);
495 return -ENOMEM;
496 }
497
498 hub->ports[port1 - 1] = port_dev;
499 port_dev->portnum = port1;
500 set_bit(port1, hub->power_bits);
501 port_dev->dev.parent = hub->intfdev;
502 if (hub_is_superspeed(hdev)) {
503 port_dev->usb3_lpm_u1_permit = 1;
504 port_dev->usb3_lpm_u2_permit = 1;
505 port_dev->dev.groups = port_dev_usb3_group;
506 } else
507 port_dev->dev.groups = port_dev_group;
508 port_dev->dev.type = &usb_port_device_type;
509 port_dev->dev.driver = &usb_port_driver;
510 if (hub_is_superspeed(hub->hdev))
511 port_dev->is_superspeed = 1;
512 dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
513 port1);
514 mutex_init(&port_dev->status_lock);
515 retval = device_register(&port_dev->dev);
516 if (retval) {
517 put_device(&port_dev->dev);
518 return retval;
519 }
520
521 /* Set default policy of port-poweroff disabled. */
522 retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
523 DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
524 if (retval < 0) {
525 device_unregister(&port_dev->dev);
526 return retval;
527 }
528
529 find_and_link_peer(hub, port1);
530
531 /*
532 * Enable runtime pm and hold a refernce that hub_configure()
533 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
534 * and the hub has been fully registered (hdev->maxchild set).
535 */
536 pm_runtime_set_active(&port_dev->dev);
537 pm_runtime_get_noresume(&port_dev->dev);
538 pm_runtime_enable(&port_dev->dev);
539 device_enable_async_suspend(&port_dev->dev);
540
541 /*
542 * Keep hidden the ability to enable port-poweroff if the hub
543 * does not support power switching.
544 */
545 if (!hub_is_port_power_switchable(hub))
546 return 0;
547
548 /* Attempt to let userspace take over the policy. */
549 retval = dev_pm_qos_expose_flags(&port_dev->dev,
550 PM_QOS_FLAG_NO_POWER_OFF);
551 if (retval < 0) {
552 dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
553 return 0;
554 }
555
556 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
557 retval = dev_pm_qos_remove_request(port_dev->req);
558 if (retval >= 0) {
559 kfree(port_dev->req);
560 port_dev->req = NULL;
561 }
562 return 0;
563 }
564
565 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
566 {
567 struct usb_port *port_dev = hub->ports[port1 - 1];
568 struct usb_port *peer;
569
570 peer = port_dev->peer;
571 if (peer)
572 unlink_peers(port_dev, peer);
573 device_unregister(&port_dev->dev);
574 }