1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2012 Intel Corp
7 * Author: Lan Tianyu <tianyu.lan@intel.com>
10 #include <linux/slab.h>
11 #include <linux/pm_qos.h>
15 static int usb_port_block_power_off
;
17 static const struct attribute_group
*port_dev_group
[];
19 static ssize_t
connect_type_show(struct device
*dev
,
20 struct device_attribute
*attr
, char *buf
)
22 struct usb_port
*port_dev
= to_usb_port(dev
);
25 switch (port_dev
->connect_type
) {
26 case USB_PORT_CONNECT_TYPE_HOT_PLUG
:
29 case USB_PORT_CONNECT_TYPE_HARD_WIRED
:
32 case USB_PORT_NOT_USED
:
40 return sprintf(buf
, "%s\n", result
);
42 static DEVICE_ATTR_RO(connect_type
);
44 static ssize_t
over_current_count_show(struct device
*dev
,
45 struct device_attribute
*attr
, char *buf
)
47 struct usb_port
*port_dev
= to_usb_port(dev
);
49 return sprintf(buf
, "%u\n", port_dev
->over_current_count
);
51 static DEVICE_ATTR_RO(over_current_count
);
53 static ssize_t
quirks_show(struct device
*dev
,
54 struct device_attribute
*attr
, char *buf
)
56 struct usb_port
*port_dev
= to_usb_port(dev
);
58 return sprintf(buf
, "%08x\n", port_dev
->quirks
);
61 static ssize_t
quirks_store(struct device
*dev
, struct device_attribute
*attr
,
62 const char *buf
, size_t count
)
64 struct usb_port
*port_dev
= to_usb_port(dev
);
67 if (kstrtou32(buf
, 16, &value
))
70 port_dev
->quirks
= value
;
73 static DEVICE_ATTR_RW(quirks
);
75 static ssize_t
usb3_lpm_permit_show(struct device
*dev
,
76 struct device_attribute
*attr
, char *buf
)
78 struct usb_port
*port_dev
= to_usb_port(dev
);
81 if (port_dev
->usb3_lpm_u1_permit
) {
82 if (port_dev
->usb3_lpm_u2_permit
)
87 if (port_dev
->usb3_lpm_u2_permit
)
93 return sprintf(buf
, "%s\n", p
);
96 static ssize_t
usb3_lpm_permit_store(struct device
*dev
,
97 struct device_attribute
*attr
,
98 const char *buf
, size_t count
)
100 struct usb_port
*port_dev
= to_usb_port(dev
);
101 struct usb_device
*udev
= port_dev
->child
;
104 if (!strncmp(buf
, "u1_u2", 5)) {
105 port_dev
->usb3_lpm_u1_permit
= 1;
106 port_dev
->usb3_lpm_u2_permit
= 1;
108 } else if (!strncmp(buf
, "u1", 2)) {
109 port_dev
->usb3_lpm_u1_permit
= 1;
110 port_dev
->usb3_lpm_u2_permit
= 0;
112 } else if (!strncmp(buf
, "u2", 2)) {
113 port_dev
->usb3_lpm_u1_permit
= 0;
114 port_dev
->usb3_lpm_u2_permit
= 1;
116 } else if (!strncmp(buf
, "0", 1)) {
117 port_dev
->usb3_lpm_u1_permit
= 0;
118 port_dev
->usb3_lpm_u2_permit
= 0;
122 /* If device is connected to the port, disable or enable lpm
123 * to make new u1 u2 setting take effect immediately.
126 hcd
= bus_to_hcd(udev
->bus
);
129 usb_lock_device(udev
);
130 mutex_lock(hcd
->bandwidth_mutex
);
131 if (!usb_disable_lpm(udev
))
132 usb_enable_lpm(udev
);
133 mutex_unlock(hcd
->bandwidth_mutex
);
134 usb_unlock_device(udev
);
139 static DEVICE_ATTR_RW(usb3_lpm_permit
);
141 static struct attribute
*port_dev_attrs
[] = {
142 &dev_attr_connect_type
.attr
,
143 &dev_attr_quirks
.attr
,
144 &dev_attr_over_current_count
.attr
,
148 static struct attribute_group port_dev_attr_grp
= {
149 .attrs
= port_dev_attrs
,
152 static const struct attribute_group
*port_dev_group
[] = {
157 static struct attribute
*port_dev_usb3_attrs
[] = {
158 &dev_attr_usb3_lpm_permit
.attr
,
162 static struct attribute_group port_dev_usb3_attr_grp
= {
163 .attrs
= port_dev_usb3_attrs
,
166 static const struct attribute_group
*port_dev_usb3_group
[] = {
168 &port_dev_usb3_attr_grp
,
172 static void usb_port_device_release(struct device
*dev
)
174 struct usb_port
*port_dev
= to_usb_port(dev
);
176 kfree(port_dev
->req
);
181 static int usb_port_runtime_resume(struct device
*dev
)
183 struct usb_port
*port_dev
= to_usb_port(dev
);
184 struct usb_device
*hdev
= to_usb_device(dev
->parent
->parent
);
185 struct usb_interface
*intf
= to_usb_interface(dev
->parent
);
186 struct usb_hub
*hub
= usb_hub_to_struct_hub(hdev
);
187 struct usb_device
*udev
= port_dev
->child
;
188 struct usb_port
*peer
= port_dev
->peer
;
189 int port1
= port_dev
->portnum
;
195 set_bit(port1
, hub
->power_bits
);
200 * Power on our usb3 peer before this usb2 port to prevent a usb3
201 * device from degrading to its usb2 connection
203 if (!port_dev
->is_superspeed
&& peer
)
204 pm_runtime_get_sync(&peer
->dev
);
206 retval
= usb_autopm_get_interface(intf
);
210 retval
= usb_hub_set_port_power(hdev
, hub
, port1
, true);
211 msleep(hub_power_on_good_delay(hub
));
212 if (udev
&& !retval
) {
214 * Our preference is to simply wait for the port to reconnect,
215 * as that is the lowest latency method to restart the port.
216 * However, there are cases where toggling port power results in
217 * the host port and the device port getting out of sync causing
218 * a link training live lock. Upon timeout, flag the port as
219 * needing warm reset recovery (to be performed later by
220 * usb_port_resume() as requested via usb_wakeup_notification())
222 if (hub_port_debounce_be_connected(hub
, port1
) < 0) {
223 dev_dbg(&port_dev
->dev
, "reconnect timeout\n");
224 if (hub_is_superspeed(hdev
))
225 set_bit(port1
, hub
->warm_reset_bits
);
228 /* Force the child awake to revalidate after the power loss. */
229 if (!test_and_set_bit(port1
, hub
->child_usage_bits
)) {
230 pm_runtime_get_noresume(&port_dev
->dev
);
231 pm_request_resume(&udev
->dev
);
235 usb_autopm_put_interface(intf
);
240 static int usb_port_runtime_suspend(struct device
*dev
)
242 struct usb_port
*port_dev
= to_usb_port(dev
);
243 struct usb_device
*hdev
= to_usb_device(dev
->parent
->parent
);
244 struct usb_interface
*intf
= to_usb_interface(dev
->parent
);
245 struct usb_hub
*hub
= usb_hub_to_struct_hub(hdev
);
246 struct usb_port
*peer
= port_dev
->peer
;
247 int port1
= port_dev
->portnum
;
255 if (dev_pm_qos_flags(&port_dev
->dev
, PM_QOS_FLAG_NO_POWER_OFF
)
259 if (usb_port_block_power_off
)
262 retval
= usb_autopm_get_interface(intf
);
266 retval
= usb_hub_set_port_power(hdev
, hub
, port1
, false);
267 usb_clear_port_feature(hdev
, port1
, USB_PORT_FEAT_C_CONNECTION
);
268 if (!port_dev
->is_superspeed
)
269 usb_clear_port_feature(hdev
, port1
, USB_PORT_FEAT_C_ENABLE
);
270 usb_autopm_put_interface(intf
);
273 * Our peer usb3 port may now be able to suspend, so
274 * asynchronously queue a suspend request to observe that this
275 * usb2 port is now off.
277 if (!port_dev
->is_superspeed
&& peer
)
278 pm_runtime_put(&peer
->dev
);
284 static const struct dev_pm_ops usb_port_pm_ops
= {
286 .runtime_suspend
= usb_port_runtime_suspend
,
287 .runtime_resume
= usb_port_runtime_resume
,
291 struct device_type usb_port_device_type
= {
293 .release
= usb_port_device_release
,
294 .pm
= &usb_port_pm_ops
,
297 static struct device_driver usb_port_driver
= {
299 .owner
= THIS_MODULE
,
302 static int link_peers(struct usb_port
*left
, struct usb_port
*right
)
304 struct usb_port
*ss_port
, *hs_port
;
307 if (left
->peer
== right
&& right
->peer
== left
)
310 if (left
->peer
|| right
->peer
) {
311 struct usb_port
*lpeer
= left
->peer
;
312 struct usb_port
*rpeer
= right
->peer
;
315 if (left
->location
&& left
->location
== right
->location
)
320 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
321 dev_name(&left
->dev
), dev_name(&right
->dev
), method
,
322 dev_name(&left
->dev
),
323 lpeer
? dev_name(&lpeer
->dev
) : "none",
324 dev_name(&right
->dev
),
325 rpeer
? dev_name(&rpeer
->dev
) : "none");
329 rc
= sysfs_create_link(&left
->dev
.kobj
, &right
->dev
.kobj
, "peer");
332 rc
= sysfs_create_link(&right
->dev
.kobj
, &left
->dev
.kobj
, "peer");
334 sysfs_remove_link(&left
->dev
.kobj
, "peer");
339 * We need to wake the HiSpeed port to make sure we don't race
340 * setting ->peer with usb_port_runtime_suspend(). Otherwise we
341 * may miss a suspend event for the SuperSpeed port.
343 if (left
->is_superspeed
) {
345 WARN_ON(right
->is_superspeed
);
349 WARN_ON(!right
->is_superspeed
);
352 pm_runtime_get_sync(&hs_port
->dev
);
358 * The SuperSpeed reference is dropped when the HiSpeed port in
359 * this relationship suspends, i.e. when it is safe to allow a
360 * SuperSpeed connection to drop since there is no risk of a
361 * device degrading to its powered-off HiSpeed connection.
363 * Also, drop the HiSpeed ref taken above.
365 pm_runtime_get_sync(&ss_port
->dev
);
366 pm_runtime_put(&hs_port
->dev
);
371 static void link_peers_report(struct usb_port
*left
, struct usb_port
*right
)
375 rc
= link_peers(left
, right
);
377 dev_dbg(&left
->dev
, "peered to %s\n", dev_name(&right
->dev
));
379 dev_dbg(&left
->dev
, "failed to peer to %s (%d)\n",
380 dev_name(&right
->dev
), rc
);
381 pr_warn_once("usb: port power management may be unreliable\n");
382 usb_port_block_power_off
= 1;
386 static void unlink_peers(struct usb_port
*left
, struct usb_port
*right
)
388 struct usb_port
*ss_port
, *hs_port
;
390 WARN(right
->peer
!= left
|| left
->peer
!= right
,
391 "%s and %s are not peers?\n",
392 dev_name(&left
->dev
), dev_name(&right
->dev
));
395 * We wake the HiSpeed port to make sure we don't race its
396 * usb_port_runtime_resume() event which takes a SuperSpeed ref
397 * when ->peer is !NULL.
399 if (left
->is_superspeed
) {
407 pm_runtime_get_sync(&hs_port
->dev
);
409 sysfs_remove_link(&left
->dev
.kobj
, "peer");
411 sysfs_remove_link(&right
->dev
.kobj
, "peer");
414 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
415 pm_runtime_put(&ss_port
->dev
);
417 /* Drop the ref taken above */
418 pm_runtime_put(&hs_port
->dev
);
422 * For each usb hub device in the system check to see if it is in the
423 * peer domain of the given port_dev, and if it is check to see if it
424 * has a port that matches the given port by location
426 static int match_location(struct usb_device
*peer_hdev
, void *p
)
429 struct usb_hcd
*hcd
, *peer_hcd
;
430 struct usb_port
*port_dev
= p
, *peer
;
431 struct usb_hub
*peer_hub
= usb_hub_to_struct_hub(peer_hdev
);
432 struct usb_device
*hdev
= to_usb_device(port_dev
->dev
.parent
->parent
);
437 hcd
= bus_to_hcd(hdev
->bus
);
438 peer_hcd
= bus_to_hcd(peer_hdev
->bus
);
439 /* peer_hcd is provisional until we verify it against the known peer */
440 if (peer_hcd
!= hcd
->shared_hcd
)
443 for (port1
= 1; port1
<= peer_hdev
->maxchild
; port1
++) {
444 peer
= peer_hub
->ports
[port1
- 1];
445 if (peer
&& peer
->location
== port_dev
->location
) {
446 link_peers_report(port_dev
, peer
);
455 * Find the peer port either via explicit platform firmware "location"
456 * data, the peer hcd for root hubs, or the upstream peer relationship
457 * for all other hubs.
459 static void find_and_link_peer(struct usb_hub
*hub
, int port1
)
461 struct usb_port
*port_dev
= hub
->ports
[port1
- 1], *peer
;
462 struct usb_device
*hdev
= hub
->hdev
;
463 struct usb_device
*peer_hdev
;
464 struct usb_hub
*peer_hub
;
467 * If location data is available then we can only peer this port
468 * by a location match, not the default peer (lest we create a
469 * situation where we need to go back and undo a default peering
470 * when the port is later peered by location data)
472 if (port_dev
->location
) {
473 /* we link the peer in match_location() if found */
474 usb_for_each_dev(port_dev
, match_location
);
476 } else if (!hdev
->parent
) {
477 struct usb_hcd
*hcd
= bus_to_hcd(hdev
->bus
);
478 struct usb_hcd
*peer_hcd
= hcd
->shared_hcd
;
483 peer_hdev
= peer_hcd
->self
.root_hub
;
485 struct usb_port
*upstream
;
486 struct usb_device
*parent
= hdev
->parent
;
487 struct usb_hub
*parent_hub
= usb_hub_to_struct_hub(parent
);
492 upstream
= parent_hub
->ports
[hdev
->portnum
- 1];
493 if (!upstream
|| !upstream
->peer
)
496 peer_hdev
= upstream
->peer
->child
;
499 peer_hub
= usb_hub_to_struct_hub(peer_hdev
);
500 if (!peer_hub
|| port1
> peer_hdev
->maxchild
)
504 * we found a valid default peer, last check is to make sure it
505 * does not have location data
507 peer
= peer_hub
->ports
[port1
- 1];
508 if (peer
&& peer
->location
== 0)
509 link_peers_report(port_dev
, peer
);
512 int usb_hub_create_port_device(struct usb_hub
*hub
, int port1
)
514 struct usb_port
*port_dev
;
515 struct usb_device
*hdev
= hub
->hdev
;
518 port_dev
= kzalloc(sizeof(*port_dev
), GFP_KERNEL
);
522 port_dev
->req
= kzalloc(sizeof(*(port_dev
->req
)), GFP_KERNEL
);
523 if (!port_dev
->req
) {
528 hub
->ports
[port1
- 1] = port_dev
;
529 port_dev
->portnum
= port1
;
530 set_bit(port1
, hub
->power_bits
);
531 port_dev
->dev
.parent
= hub
->intfdev
;
532 if (hub_is_superspeed(hdev
)) {
533 port_dev
->usb3_lpm_u1_permit
= 1;
534 port_dev
->usb3_lpm_u2_permit
= 1;
535 port_dev
->dev
.groups
= port_dev_usb3_group
;
537 port_dev
->dev
.groups
= port_dev_group
;
538 port_dev
->dev
.type
= &usb_port_device_type
;
539 port_dev
->dev
.driver
= &usb_port_driver
;
540 if (hub_is_superspeed(hub
->hdev
))
541 port_dev
->is_superspeed
= 1;
542 dev_set_name(&port_dev
->dev
, "%s-port%d", dev_name(&hub
->hdev
->dev
),
544 mutex_init(&port_dev
->status_lock
);
545 retval
= device_register(&port_dev
->dev
);
547 put_device(&port_dev
->dev
);
551 /* Set default policy of port-poweroff disabled. */
552 retval
= dev_pm_qos_add_request(&port_dev
->dev
, port_dev
->req
,
553 DEV_PM_QOS_FLAGS
, PM_QOS_FLAG_NO_POWER_OFF
);
555 device_unregister(&port_dev
->dev
);
559 find_and_link_peer(hub
, port1
);
562 * Enable runtime pm and hold a refernce that hub_configure()
563 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
564 * and the hub has been fully registered (hdev->maxchild set).
566 pm_runtime_set_active(&port_dev
->dev
);
567 pm_runtime_get_noresume(&port_dev
->dev
);
568 pm_runtime_enable(&port_dev
->dev
);
569 device_enable_async_suspend(&port_dev
->dev
);
572 * Keep hidden the ability to enable port-poweroff if the hub
573 * does not support power switching.
575 if (!hub_is_port_power_switchable(hub
))
578 /* Attempt to let userspace take over the policy. */
579 retval
= dev_pm_qos_expose_flags(&port_dev
->dev
,
580 PM_QOS_FLAG_NO_POWER_OFF
);
582 dev_warn(&port_dev
->dev
, "failed to expose pm_qos_no_poweroff\n");
586 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
587 retval
= dev_pm_qos_remove_request(port_dev
->req
);
589 kfree(port_dev
->req
);
590 port_dev
->req
= NULL
;
595 void usb_hub_remove_port_device(struct usb_hub
*hub
, int port1
)
597 struct usb_port
*port_dev
= hub
->ports
[port1
- 1];
598 struct usb_port
*peer
;
600 peer
= port_dev
->peer
;
602 unlink_peers(port_dev
, peer
);
603 device_unregister(&port_dev
->dev
);