xref: /openbmc/linux/drivers/usb/core/port.c (revision 9470114d)
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 }
454 
455 static const struct dev_pm_ops usb_port_pm_ops = {
456 #ifdef CONFIG_PM
457 	.runtime_suspend =	usb_port_runtime_suspend,
458 	.runtime_resume =	usb_port_runtime_resume,
459 #endif
460 };
461 
462 struct device_type usb_port_device_type = {
463 	.name =		"usb_port",
464 	.release =	usb_port_device_release,
465 	.pm =		&usb_port_pm_ops,
466 };
467 
468 static struct device_driver usb_port_driver = {
469 	.name = "usb",
470 	.owner = THIS_MODULE,
471 	.shutdown = usb_port_shutdown,
472 };
473 
474 static int link_peers(struct usb_port *left, struct usb_port *right)
475 {
476 	struct usb_port *ss_port, *hs_port;
477 	int rc;
478 
479 	if (left->peer == right && right->peer == left)
480 		return 0;
481 
482 	if (left->peer || right->peer) {
483 		struct usb_port *lpeer = left->peer;
484 		struct usb_port *rpeer = right->peer;
485 		char *method;
486 
487 		if (left->location && left->location == right->location)
488 			method = "location";
489 		else
490 			method = "default";
491 
492 		pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
493 			dev_name(&left->dev), dev_name(&right->dev), method,
494 			dev_name(&left->dev),
495 			lpeer ? dev_name(&lpeer->dev) : "none",
496 			dev_name(&right->dev),
497 			rpeer ? dev_name(&rpeer->dev) : "none");
498 		return -EBUSY;
499 	}
500 
501 	rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
502 	if (rc)
503 		return rc;
504 	rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
505 	if (rc) {
506 		sysfs_remove_link(&left->dev.kobj, "peer");
507 		return rc;
508 	}
509 
510 	/*
511 	 * We need to wake the HiSpeed port to make sure we don't race
512 	 * setting ->peer with usb_port_runtime_suspend().  Otherwise we
513 	 * may miss a suspend event for the SuperSpeed port.
514 	 */
515 	if (left->is_superspeed) {
516 		ss_port = left;
517 		WARN_ON(right->is_superspeed);
518 		hs_port = right;
519 	} else {
520 		ss_port = right;
521 		WARN_ON(!right->is_superspeed);
522 		hs_port = left;
523 	}
524 	pm_runtime_get_sync(&hs_port->dev);
525 
526 	left->peer = right;
527 	right->peer = left;
528 
529 	/*
530 	 * The SuperSpeed reference is dropped when the HiSpeed port in
531 	 * this relationship suspends, i.e. when it is safe to allow a
532 	 * SuperSpeed connection to drop since there is no risk of a
533 	 * device degrading to its powered-off HiSpeed connection.
534 	 *
535 	 * Also, drop the HiSpeed ref taken above.
536 	 */
537 	pm_runtime_get_sync(&ss_port->dev);
538 	pm_runtime_put(&hs_port->dev);
539 
540 	return 0;
541 }
542 
543 static void link_peers_report(struct usb_port *left, struct usb_port *right)
544 {
545 	int rc;
546 
547 	rc = link_peers(left, right);
548 	if (rc == 0) {
549 		dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
550 	} else {
551 		dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
552 				dev_name(&right->dev), rc);
553 		pr_warn_once("usb: port power management may be unreliable\n");
554 		usb_port_block_power_off = 1;
555 	}
556 }
557 
558 static void unlink_peers(struct usb_port *left, struct usb_port *right)
559 {
560 	struct usb_port *ss_port, *hs_port;
561 
562 	WARN(right->peer != left || left->peer != right,
563 			"%s and %s are not peers?\n",
564 			dev_name(&left->dev), dev_name(&right->dev));
565 
566 	/*
567 	 * We wake the HiSpeed port to make sure we don't race its
568 	 * usb_port_runtime_resume() event which takes a SuperSpeed ref
569 	 * when ->peer is !NULL.
570 	 */
571 	if (left->is_superspeed) {
572 		ss_port = left;
573 		hs_port = right;
574 	} else {
575 		ss_port = right;
576 		hs_port = left;
577 	}
578 
579 	pm_runtime_get_sync(&hs_port->dev);
580 
581 	sysfs_remove_link(&left->dev.kobj, "peer");
582 	right->peer = NULL;
583 	sysfs_remove_link(&right->dev.kobj, "peer");
584 	left->peer = NULL;
585 
586 	/* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
587 	pm_runtime_put(&ss_port->dev);
588 
589 	/* Drop the ref taken above */
590 	pm_runtime_put(&hs_port->dev);
591 }
592 
593 /*
594  * For each usb hub device in the system check to see if it is in the
595  * peer domain of the given port_dev, and if it is check to see if it
596  * has a port that matches the given port by location
597  */
598 static int match_location(struct usb_device *peer_hdev, void *p)
599 {
600 	int port1;
601 	struct usb_hcd *hcd, *peer_hcd;
602 	struct usb_port *port_dev = p, *peer;
603 	struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
604 	struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
605 
606 	if (!peer_hub || port_dev->connect_type == USB_PORT_NOT_USED)
607 		return 0;
608 
609 	hcd = bus_to_hcd(hdev->bus);
610 	peer_hcd = bus_to_hcd(peer_hdev->bus);
611 	/* peer_hcd is provisional until we verify it against the known peer */
612 	if (peer_hcd != hcd->shared_hcd)
613 		return 0;
614 
615 	for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
616 		peer = peer_hub->ports[port1 - 1];
617 		if (peer && peer->connect_type != USB_PORT_NOT_USED &&
618 		    peer->location == port_dev->location) {
619 			link_peers_report(port_dev, peer);
620 			return 1; /* done */
621 		}
622 	}
623 
624 	return 0;
625 }
626 
627 /*
628  * Find the peer port either via explicit platform firmware "location"
629  * data, the peer hcd for root hubs, or the upstream peer relationship
630  * for all other hubs.
631  */
632 static void find_and_link_peer(struct usb_hub *hub, int port1)
633 {
634 	struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
635 	struct usb_device *hdev = hub->hdev;
636 	struct usb_device *peer_hdev;
637 	struct usb_hub *peer_hub;
638 
639 	/*
640 	 * If location data is available then we can only peer this port
641 	 * by a location match, not the default peer (lest we create a
642 	 * situation where we need to go back and undo a default peering
643 	 * when the port is later peered by location data)
644 	 */
645 	if (port_dev->location) {
646 		/* we link the peer in match_location() if found */
647 		usb_for_each_dev(port_dev, match_location);
648 		return;
649 	} else if (!hdev->parent) {
650 		struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
651 		struct usb_hcd *peer_hcd = hcd->shared_hcd;
652 
653 		if (!peer_hcd)
654 			return;
655 
656 		peer_hdev = peer_hcd->self.root_hub;
657 	} else {
658 		struct usb_port *upstream;
659 		struct usb_device *parent = hdev->parent;
660 		struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
661 
662 		if (!parent_hub)
663 			return;
664 
665 		upstream = parent_hub->ports[hdev->portnum - 1];
666 		if (!upstream || !upstream->peer)
667 			return;
668 
669 		peer_hdev = upstream->peer->child;
670 	}
671 
672 	peer_hub = usb_hub_to_struct_hub(peer_hdev);
673 	if (!peer_hub || port1 > peer_hdev->maxchild)
674 		return;
675 
676 	/*
677 	 * we found a valid default peer, last check is to make sure it
678 	 * does not have location data
679 	 */
680 	peer = peer_hub->ports[port1 - 1];
681 	if (peer && peer->location == 0)
682 		link_peers_report(port_dev, peer);
683 }
684 
685 static int connector_bind(struct device *dev, struct device *connector, void *data)
686 {
687 	int ret;
688 
689 	ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector");
690 	if (ret)
691 		return ret;
692 
693 	ret = sysfs_create_link(&connector->kobj, &dev->kobj, dev_name(dev));
694 	if (ret)
695 		sysfs_remove_link(&dev->kobj, "connector");
696 
697 	return ret;
698 }
699 
700 static void connector_unbind(struct device *dev, struct device *connector, void *data)
701 {
702 	sysfs_remove_link(&connector->kobj, dev_name(dev));
703 	sysfs_remove_link(&dev->kobj, "connector");
704 }
705 
706 static const struct component_ops connector_ops = {
707 	.bind = connector_bind,
708 	.unbind = connector_unbind,
709 };
710 
711 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
712 {
713 	struct usb_port *port_dev;
714 	struct usb_device *hdev = hub->hdev;
715 	int retval;
716 
717 	port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
718 	if (!port_dev)
719 		return -ENOMEM;
720 
721 	port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
722 	if (!port_dev->req) {
723 		kfree(port_dev);
724 		return -ENOMEM;
725 	}
726 
727 	hub->ports[port1 - 1] = port_dev;
728 	port_dev->portnum = port1;
729 	set_bit(port1, hub->power_bits);
730 	port_dev->dev.parent = hub->intfdev;
731 	if (hub_is_superspeed(hdev)) {
732 		port_dev->usb3_lpm_u1_permit = 1;
733 		port_dev->usb3_lpm_u2_permit = 1;
734 		port_dev->dev.groups = port_dev_usb3_group;
735 	} else
736 		port_dev->dev.groups = port_dev_group;
737 	port_dev->dev.type = &usb_port_device_type;
738 	port_dev->dev.driver = &usb_port_driver;
739 	if (hub_is_superspeed(hub->hdev))
740 		port_dev->is_superspeed = 1;
741 	dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
742 			port1);
743 	mutex_init(&port_dev->status_lock);
744 	retval = device_register(&port_dev->dev);
745 	if (retval) {
746 		put_device(&port_dev->dev);
747 		return retval;
748 	}
749 
750 	port_dev->state_kn = sysfs_get_dirent(port_dev->dev.kobj.sd, "state");
751 	if (!port_dev->state_kn) {
752 		dev_err(&port_dev->dev, "failed to sysfs_get_dirent 'state'\n");
753 		retval = -ENODEV;
754 		goto err_unregister;
755 	}
756 
757 	/* Set default policy of port-poweroff disabled. */
758 	retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
759 			DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
760 	if (retval < 0) {
761 		goto err_put_kn;
762 	}
763 
764 	retval = component_add(&port_dev->dev, &connector_ops);
765 	if (retval) {
766 		dev_warn(&port_dev->dev, "failed to add component\n");
767 		goto err_put_kn;
768 	}
769 
770 	find_and_link_peer(hub, port1);
771 
772 	/*
773 	 * Enable runtime pm and hold a refernce that hub_configure()
774 	 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
775 	 * and the hub has been fully registered (hdev->maxchild set).
776 	 */
777 	pm_runtime_set_active(&port_dev->dev);
778 	pm_runtime_get_noresume(&port_dev->dev);
779 	pm_runtime_enable(&port_dev->dev);
780 	device_enable_async_suspend(&port_dev->dev);
781 
782 	/*
783 	 * Keep hidden the ability to enable port-poweroff if the hub
784 	 * does not support power switching.
785 	 */
786 	if (!hub_is_port_power_switchable(hub))
787 		return 0;
788 
789 	/* Attempt to let userspace take over the policy. */
790 	retval = dev_pm_qos_expose_flags(&port_dev->dev,
791 			PM_QOS_FLAG_NO_POWER_OFF);
792 	if (retval < 0) {
793 		dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
794 		return 0;
795 	}
796 
797 	/* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
798 	retval = dev_pm_qos_remove_request(port_dev->req);
799 	if (retval >= 0) {
800 		kfree(port_dev->req);
801 		port_dev->req = NULL;
802 	}
803 	return 0;
804 
805 err_put_kn:
806 	sysfs_put(port_dev->state_kn);
807 err_unregister:
808 	device_unregister(&port_dev->dev);
809 
810 	return retval;
811 }
812 
813 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
814 {
815 	struct usb_port *port_dev = hub->ports[port1 - 1];
816 	struct usb_port *peer;
817 
818 	peer = port_dev->peer;
819 	if (peer)
820 		unlink_peers(port_dev, peer);
821 	component_del(&port_dev->dev, &connector_ops);
822 	sysfs_put(port_dev->state_kn);
823 	device_unregister(&port_dev->dev);
824 }
825