Linux Audio

Check our new training course

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