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