Linux Audio

Check our new training course

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