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