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
early_stop_show(struct device * dev,struct device_attribute * attr,char * buf)19 static ssize_t early_stop_show(struct device *dev,
20 struct device_attribute *attr, char *buf)
21 {
22 struct usb_port *port_dev = to_usb_port(dev);
23
24 return sysfs_emit(buf, "%s\n", port_dev->early_stop ? "yes" : "no");
25 }
26
early_stop_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)27 static ssize_t early_stop_store(struct device *dev, struct device_attribute *attr,
28 const char *buf, size_t count)
29 {
30 struct usb_port *port_dev = to_usb_port(dev);
31 bool value;
32
33 if (kstrtobool(buf, &value))
34 return -EINVAL;
35
36 if (value)
37 port_dev->early_stop = 1;
38 else
39 port_dev->early_stop = 0;
40
41 return count;
42 }
43 static DEVICE_ATTR_RW(early_stop);
44
location_show(struct device * dev,struct device_attribute * attr,char * buf)45 static ssize_t location_show(struct device *dev,
46 struct device_attribute *attr, char *buf)
47 {
48 struct usb_port *port_dev = to_usb_port(dev);
49
50 return sprintf(buf, "0x%08x\n", port_dev->location);
51 }
52 static DEVICE_ATTR_RO(location);
53
connect_type_show(struct device * dev,struct device_attribute * attr,char * buf)54 static ssize_t connect_type_show(struct device *dev,
55 struct device_attribute *attr, char *buf)
56 {
57 struct usb_port *port_dev = to_usb_port(dev);
58 char *result;
59
60 switch (port_dev->connect_type) {
61 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
62 result = "hotplug";
63 break;
64 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
65 result = "hardwired";
66 break;
67 case USB_PORT_NOT_USED:
68 result = "not used";
69 break;
70 default:
71 result = "unknown";
72 break;
73 }
74
75 return sprintf(buf, "%s\n", result);
76 }
77 static DEVICE_ATTR_RO(connect_type);
78
state_show(struct device * dev,struct device_attribute * attr,char * buf)79 static ssize_t state_show(struct device *dev,
80 struct device_attribute *attr, char *buf)
81 {
82 struct usb_port *port_dev = to_usb_port(dev);
83 enum usb_device_state state = READ_ONCE(port_dev->state);
84
85 return sysfs_emit(buf, "%s\n", usb_state_string(state));
86 }
87 static DEVICE_ATTR_RO(state);
88
over_current_count_show(struct device * dev,struct device_attribute * attr,char * buf)89 static ssize_t over_current_count_show(struct device *dev,
90 struct device_attribute *attr, char *buf)
91 {
92 struct usb_port *port_dev = to_usb_port(dev);
93
94 return sprintf(buf, "%u\n", port_dev->over_current_count);
95 }
96 static DEVICE_ATTR_RO(over_current_count);
97
quirks_show(struct device * dev,struct device_attribute * attr,char * buf)98 static ssize_t quirks_show(struct device *dev,
99 struct device_attribute *attr, char *buf)
100 {
101 struct usb_port *port_dev = to_usb_port(dev);
102
103 return sprintf(buf, "%08x\n", port_dev->quirks);
104 }
105
quirks_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)106 static ssize_t quirks_store(struct device *dev, struct device_attribute *attr,
107 const char *buf, size_t count)
108 {
109 struct usb_port *port_dev = to_usb_port(dev);
110 u32 value;
111
112 if (kstrtou32(buf, 16, &value))
113 return -EINVAL;
114
115 port_dev->quirks = value;
116 return count;
117 }
118 static DEVICE_ATTR_RW(quirks);
119
usb3_lpm_permit_show(struct device * dev,struct device_attribute * attr,char * buf)120 static ssize_t usb3_lpm_permit_show(struct device *dev,
121 struct device_attribute *attr, char *buf)
122 {
123 struct usb_port *port_dev = to_usb_port(dev);
124 const char *p;
125
126 if (port_dev->usb3_lpm_u1_permit) {
127 if (port_dev->usb3_lpm_u2_permit)
128 p = "u1_u2";
129 else
130 p = "u1";
131 } else {
132 if (port_dev->usb3_lpm_u2_permit)
133 p = "u2";
134 else
135 p = "0";
136 }
137
138 return sprintf(buf, "%s\n", p);
139 }
140
usb3_lpm_permit_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)141 static ssize_t usb3_lpm_permit_store(struct device *dev,
142 struct device_attribute *attr,
143 const char *buf, size_t count)
144 {
145 struct usb_port *port_dev = to_usb_port(dev);
146 struct usb_device *udev = port_dev->child;
147 struct usb_hcd *hcd;
148
149 if (!strncmp(buf, "u1_u2", 5)) {
150 port_dev->usb3_lpm_u1_permit = 1;
151 port_dev->usb3_lpm_u2_permit = 1;
152
153 } else if (!strncmp(buf, "u1", 2)) {
154 port_dev->usb3_lpm_u1_permit = 1;
155 port_dev->usb3_lpm_u2_permit = 0;
156
157 } else if (!strncmp(buf, "u2", 2)) {
158 port_dev->usb3_lpm_u1_permit = 0;
159 port_dev->usb3_lpm_u2_permit = 1;
160
161 } else if (!strncmp(buf, "0", 1)) {
162 port_dev->usb3_lpm_u1_permit = 0;
163 port_dev->usb3_lpm_u2_permit = 0;
164 } else
165 return -EINVAL;
166
167 /* If device is connected to the port, disable or enable lpm
168 * to make new u1 u2 setting take effect immediately.
169 */
170 if (udev) {
171 hcd = bus_to_hcd(udev->bus);
172 if (!hcd)
173 return -EINVAL;
174 usb_lock_device(udev);
175 mutex_lock(hcd->bandwidth_mutex);
176 if (!usb_disable_lpm(udev))
177 usb_enable_lpm(udev);
178 mutex_unlock(hcd->bandwidth_mutex);
179 usb_unlock_device(udev);
180 }
181
182 return count;
183 }
184 static DEVICE_ATTR_RW(usb3_lpm_permit);
185
186 static struct attribute *port_dev_attrs[] = {
187 &dev_attr_connect_type.attr,
188 &dev_attr_state.attr,
189 &dev_attr_location.attr,
190 &dev_attr_quirks.attr,
191 &dev_attr_over_current_count.attr,
192 &dev_attr_early_stop.attr,
193 NULL,
194 };
195
196 static const struct attribute_group port_dev_attr_grp = {
197 .attrs = port_dev_attrs,
198 };
199
200 static const struct attribute_group *port_dev_group[] = {
201 &port_dev_attr_grp,
202 NULL,
203 };
204
205 static struct attribute *port_dev_usb3_attrs[] = {
206 &dev_attr_usb3_lpm_permit.attr,
207 NULL,
208 };
209
210 static const struct attribute_group port_dev_usb3_attr_grp = {
211 .attrs = port_dev_usb3_attrs,
212 };
213
214 static const struct attribute_group *port_dev_usb3_group[] = {
215 &port_dev_attr_grp,
216 &port_dev_usb3_attr_grp,
217 NULL,
218 };
219
usb_port_device_release(struct device * dev)220 static void usb_port_device_release(struct device *dev)
221 {
222 struct usb_port *port_dev = to_usb_port(dev);
223
224 kfree(port_dev->req);
225 kfree(port_dev);
226 }
227
228 #ifdef CONFIG_PM
usb_port_runtime_resume(struct device * dev)229 static int usb_port_runtime_resume(struct device *dev)
230 {
231 struct usb_port *port_dev = to_usb_port(dev);
232 struct usb_device *hdev = to_usb_device(dev->parent->parent);
233 struct usb_interface *intf = to_usb_interface(dev->parent);
234 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
235 struct usb_device *udev = port_dev->child;
236 struct usb_port *peer = port_dev->peer;
237 int port1 = port_dev->portnum;
238 int retval;
239
240 if (!hub)
241 return -EINVAL;
242 if (hub->in_reset) {
243 set_bit(port1, hub->power_bits);
244 return 0;
245 }
246
247 /*
248 * Power on our usb3 peer before this usb2 port to prevent a usb3
249 * device from degrading to its usb2 connection
250 */
251 if (!port_dev->is_superspeed && peer)
252 pm_runtime_get_sync(&peer->dev);
253
254 retval = usb_autopm_get_interface(intf);
255 if (retval < 0)
256 return retval;
257
258 retval = usb_hub_set_port_power(hdev, hub, port1, true);
259 msleep(hub_power_on_good_delay(hub));
260 if (udev && !retval) {
261 /*
262 * Our preference is to simply wait for the port to reconnect,
263 * as that is the lowest latency method to restart the port.
264 * However, there are cases where toggling port power results in
265 * the host port and the device port getting out of sync causing
266 * a link training live lock. Upon timeout, flag the port as
267 * needing warm reset recovery (to be performed later by
268 * usb_port_resume() as requested via usb_wakeup_notification())
269 */
270 if (hub_port_debounce_be_connected(hub, port1) < 0) {
271 dev_dbg(&port_dev->dev, "reconnect timeout\n");
272 if (hub_is_superspeed(hdev))
273 set_bit(port1, hub->warm_reset_bits);
274 }
275
276 /* Force the child awake to revalidate after the power loss. */
277 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
278 pm_runtime_get_noresume(&port_dev->dev);
279 pm_request_resume(&udev->dev);
280 }
281 }
282
283 usb_autopm_put_interface(intf);
284
285 return retval;
286 }
287
usb_port_runtime_suspend(struct device * dev)288 static int usb_port_runtime_suspend(struct device *dev)
289 {
290 struct usb_port *port_dev = to_usb_port(dev);
291 struct usb_device *hdev = to_usb_device(dev->parent->parent);
292 struct usb_interface *intf = to_usb_interface(dev->parent);
293 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
294 struct usb_port *peer = port_dev->peer;
295 int port1 = port_dev->portnum;
296 int retval;
297
298 if (!hub)
299 return -EINVAL;
300 if (hub->in_reset)
301 return -EBUSY;
302
303 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
304 == PM_QOS_FLAGS_ALL)
305 return -EAGAIN;
306
307 if (usb_port_block_power_off)
308 return -EBUSY;
309
310 retval = usb_autopm_get_interface(intf);
311 if (retval < 0)
312 return retval;
313
314 retval = usb_hub_set_port_power(hdev, hub, port1, false);
315 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
316 if (!port_dev->is_superspeed)
317 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
318 usb_autopm_put_interface(intf);
319
320 /*
321 * Our peer usb3 port may now be able to suspend, so
322 * asynchronously queue a suspend request to observe that this
323 * usb2 port is now off.
324 */
325 if (!port_dev->is_superspeed && peer)
326 pm_runtime_put(&peer->dev);
327
328 return retval;
329 }
330 #endif
331
usb_port_shutdown(struct device * dev)332 static void usb_port_shutdown(struct device *dev)
333 {
334 struct usb_port *port_dev = to_usb_port(dev);
335
336 if (port_dev->child)
337 usb_disable_usb2_hardware_lpm(port_dev->child);
338 }
339
340 static const struct dev_pm_ops usb_port_pm_ops = {
341 #ifdef CONFIG_PM
342 .runtime_suspend = usb_port_runtime_suspend,
343 .runtime_resume = usb_port_runtime_resume,
344 #endif
345 };
346
347 struct device_type usb_port_device_type = {
348 .name = "usb_port",
349 .release = usb_port_device_release,
350 .pm = &usb_port_pm_ops,
351 };
352
353 static struct device_driver usb_port_driver = {
354 .name = "usb",
355 .owner = THIS_MODULE,
356 .shutdown = usb_port_shutdown,
357 };
358
link_peers(struct usb_port * left,struct usb_port * right)359 static int link_peers(struct usb_port *left, struct usb_port *right)
360 {
361 struct usb_port *ss_port, *hs_port;
362 int rc;
363
364 if (left->peer == right && right->peer == left)
365 return 0;
366
367 if (left->peer || right->peer) {
368 struct usb_port *lpeer = left->peer;
369 struct usb_port *rpeer = right->peer;
370 char *method;
371
372 if (left->location && left->location == right->location)
373 method = "location";
374 else
375 method = "default";
376
377 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
378 dev_name(&left->dev), dev_name(&right->dev), method,
379 dev_name(&left->dev),
380 lpeer ? dev_name(&lpeer->dev) : "none",
381 dev_name(&right->dev),
382 rpeer ? dev_name(&rpeer->dev) : "none");
383 return -EBUSY;
384 }
385
386 rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
387 if (rc)
388 return rc;
389 rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
390 if (rc) {
391 sysfs_remove_link(&left->dev.kobj, "peer");
392 return rc;
393 }
394
395 /*
396 * We need to wake the HiSpeed port to make sure we don't race
397 * setting ->peer with usb_port_runtime_suspend(). Otherwise we
398 * may miss a suspend event for the SuperSpeed port.
399 */
400 if (left->is_superspeed) {
401 ss_port = left;
402 WARN_ON(right->is_superspeed);
403 hs_port = right;
404 } else {
405 ss_port = right;
406 WARN_ON(!right->is_superspeed);
407 hs_port = left;
408 }
409 pm_runtime_get_sync(&hs_port->dev);
410
411 left->peer = right;
412 right->peer = left;
413
414 /*
415 * The SuperSpeed reference is dropped when the HiSpeed port in
416 * this relationship suspends, i.e. when it is safe to allow a
417 * SuperSpeed connection to drop since there is no risk of a
418 * device degrading to its powered-off HiSpeed connection.
419 *
420 * Also, drop the HiSpeed ref taken above.
421 */
422 pm_runtime_get_sync(&ss_port->dev);
423 pm_runtime_put(&hs_port->dev);
424
425 return 0;
426 }
427
link_peers_report(struct usb_port * left,struct usb_port * right)428 static void link_peers_report(struct usb_port *left, struct usb_port *right)
429 {
430 int rc;
431
432 rc = link_peers(left, right);
433 if (rc == 0) {
434 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
435 } else {
436 dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
437 dev_name(&right->dev), rc);
438 pr_warn_once("usb: port power management may be unreliable\n");
439 usb_port_block_power_off = 1;
440 }
441 }
442
unlink_peers(struct usb_port * left,struct usb_port * right)443 static void unlink_peers(struct usb_port *left, struct usb_port *right)
444 {
445 struct usb_port *ss_port, *hs_port;
446
447 WARN(right->peer != left || left->peer != right,
448 "%s and %s are not peers?\n",
449 dev_name(&left->dev), dev_name(&right->dev));
450
451 /*
452 * We wake the HiSpeed port to make sure we don't race its
453 * usb_port_runtime_resume() event which takes a SuperSpeed ref
454 * when ->peer is !NULL.
455 */
456 if (left->is_superspeed) {
457 ss_port = left;
458 hs_port = right;
459 } else {
460 ss_port = right;
461 hs_port = left;
462 }
463
464 pm_runtime_get_sync(&hs_port->dev);
465
466 sysfs_remove_link(&left->dev.kobj, "peer");
467 right->peer = NULL;
468 sysfs_remove_link(&right->dev.kobj, "peer");
469 left->peer = NULL;
470
471 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
472 pm_runtime_put(&ss_port->dev);
473
474 /* Drop the ref taken above */
475 pm_runtime_put(&hs_port->dev);
476 }
477
478 /*
479 * For each usb hub device in the system check to see if it is in the
480 * peer domain of the given port_dev, and if it is check to see if it
481 * has a port that matches the given port by location
482 */
match_location(struct usb_device * peer_hdev,void * p)483 static int match_location(struct usb_device *peer_hdev, void *p)
484 {
485 int port1;
486 struct usb_hcd *hcd, *peer_hcd;
487 struct usb_port *port_dev = p, *peer;
488 struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
489 struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
490
491 if (!peer_hub)
492 return 0;
493
494 hcd = bus_to_hcd(hdev->bus);
495 peer_hcd = bus_to_hcd(peer_hdev->bus);
496 /* peer_hcd is provisional until we verify it against the known peer */
497 if (peer_hcd != hcd->shared_hcd)
498 return 0;
499
500 for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
501 peer = peer_hub->ports[port1 - 1];
502 if (peer && peer->location == port_dev->location) {
503 link_peers_report(port_dev, peer);
504 return 1; /* done */
505 }
506 }
507
508 return 0;
509 }
510
511 /*
512 * Find the peer port either via explicit platform firmware "location"
513 * data, the peer hcd for root hubs, or the upstream peer relationship
514 * for all other hubs.
515 */
find_and_link_peer(struct usb_hub * hub,int port1)516 static void find_and_link_peer(struct usb_hub *hub, int port1)
517 {
518 struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
519 struct usb_device *hdev = hub->hdev;
520 struct usb_device *peer_hdev;
521 struct usb_hub *peer_hub;
522
523 /*
524 * If location data is available then we can only peer this port
525 * by a location match, not the default peer (lest we create a
526 * situation where we need to go back and undo a default peering
527 * when the port is later peered by location data)
528 */
529 if (port_dev->location) {
530 /* we link the peer in match_location() if found */
531 usb_for_each_dev(port_dev, match_location);
532 return;
533 } else if (!hdev->parent) {
534 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
535 struct usb_hcd *peer_hcd = hcd->shared_hcd;
536
537 if (!peer_hcd)
538 return;
539
540 peer_hdev = peer_hcd->self.root_hub;
541 } else {
542 struct usb_port *upstream;
543 struct usb_device *parent = hdev->parent;
544 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
545
546 if (!parent_hub)
547 return;
548
549 upstream = parent_hub->ports[hdev->portnum - 1];
550 if (!upstream || !upstream->peer)
551 return;
552
553 peer_hdev = upstream->peer->child;
554 }
555
556 peer_hub = usb_hub_to_struct_hub(peer_hdev);
557 if (!peer_hub || port1 > peer_hdev->maxchild)
558 return;
559
560 /*
561 * we found a valid default peer, last check is to make sure it
562 * does not have location data
563 */
564 peer = peer_hub->ports[port1 - 1];
565 if (peer && peer->location == 0)
566 link_peers_report(port_dev, peer);
567 }
568
usb_hub_create_port_device(struct usb_hub * hub,int port1)569 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
570 {
571 struct usb_port *port_dev;
572 struct usb_device *hdev = hub->hdev;
573 int retval;
574
575 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
576 if (!port_dev)
577 return -ENOMEM;
578
579 port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
580 if (!port_dev->req) {
581 kfree(port_dev);
582 return -ENOMEM;
583 }
584
585 hub->ports[port1 - 1] = port_dev;
586 port_dev->portnum = port1;
587 set_bit(port1, hub->power_bits);
588 port_dev->dev.parent = hub->intfdev;
589 if (hub_is_superspeed(hdev)) {
590 port_dev->usb3_lpm_u1_permit = 1;
591 port_dev->usb3_lpm_u2_permit = 1;
592 port_dev->dev.groups = port_dev_usb3_group;
593 } else
594 port_dev->dev.groups = port_dev_group;
595 port_dev->dev.type = &usb_port_device_type;
596 port_dev->dev.driver = &usb_port_driver;
597 if (hub_is_superspeed(hub->hdev))
598 port_dev->is_superspeed = 1;
599 dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
600 port1);
601 mutex_init(&port_dev->status_lock);
602 retval = device_register(&port_dev->dev);
603 if (retval) {
604 put_device(&port_dev->dev);
605 return retval;
606 }
607
608 port_dev->state_kn = sysfs_get_dirent(port_dev->dev.kobj.sd, "state");
609 if (!port_dev->state_kn) {
610 dev_err(&port_dev->dev, "failed to sysfs_get_dirent 'state'\n");
611 retval = -ENODEV;
612 goto err_unregister;
613 }
614
615 /* Set default policy of port-poweroff disabled. */
616 retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
617 DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
618 if (retval < 0) {
619 goto err_put_kn;
620 }
621
622 find_and_link_peer(hub, port1);
623
624 /*
625 * Enable runtime pm and hold a refernce that hub_configure()
626 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
627 * and the hub has been fully registered (hdev->maxchild set).
628 */
629 pm_runtime_set_active(&port_dev->dev);
630 pm_runtime_get_noresume(&port_dev->dev);
631 pm_runtime_enable(&port_dev->dev);
632 device_enable_async_suspend(&port_dev->dev);
633
634 /*
635 * Keep hidden the ability to enable port-poweroff if the hub
636 * does not support power switching.
637 */
638 if (!hub_is_port_power_switchable(hub))
639 return 0;
640
641 /* Attempt to let userspace take over the policy. */
642 retval = dev_pm_qos_expose_flags(&port_dev->dev,
643 PM_QOS_FLAG_NO_POWER_OFF);
644 if (retval < 0) {
645 dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
646 return 0;
647 }
648
649 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
650 retval = dev_pm_qos_remove_request(port_dev->req);
651 if (retval >= 0) {
652 kfree(port_dev->req);
653 port_dev->req = NULL;
654 }
655 return 0;
656
657 err_put_kn:
658 sysfs_put(port_dev->state_kn);
659 err_unregister:
660 device_unregister(&port_dev->dev);
661
662 return retval;
663 }
664
usb_hub_remove_port_device(struct usb_hub * hub,int port1)665 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
666 {
667 struct usb_port *port_dev = hub->ports[port1 - 1];
668 struct usb_port *peer;
669
670 peer = port_dev->peer;
671 if (peer)
672 unlink_peers(port_dev, peer);
673 sysfs_put(port_dev->state_kn);
674 device_unregister(&port_dev->dev);
675 }
676