File Index
Files and directories
Sign in
FILE

port.c

File preview with syntax highlighting.

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