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