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