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