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