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