1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2012 Intel Corp
7 * Author: Lan Tianyu <tianyu.lan@intel.com>
10 #include <linux/kstrtox.h>
11 #include <linux/slab.h>
12 #include <linux/pm_qos.h>
13 #include <linux/component.h>
17 static int usb_port_block_power_off
;
19 static const struct attribute_group
*port_dev_group
[];
21 static ssize_t
early_stop_show(struct device
*dev
,
22 struct device_attribute
*attr
, char *buf
)
24 struct usb_port
*port_dev
= to_usb_port(dev
);
26 return sysfs_emit(buf
, "%s\n", port_dev
->early_stop
? "yes" : "no");
29 static ssize_t
early_stop_store(struct device
*dev
, struct device_attribute
*attr
,
30 const char *buf
, size_t count
)
32 struct usb_port
*port_dev
= to_usb_port(dev
);
35 if (kstrtobool(buf
, &value
))
39 port_dev
->early_stop
= 1;
41 port_dev
->early_stop
= 0;
45 static DEVICE_ATTR_RW(early_stop
);
47 static ssize_t
disable_show(struct device
*dev
,
48 struct device_attribute
*attr
, char *buf
)
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
;
59 rc
= usb_autopm_get_interface(intf
);
63 usb_lock_device(hdev
);
64 if (hub
->disconnected
) {
69 usb_hub_port_status(hub
, port1
, &portstatus
, &unused
);
70 disabled
= !usb_port_is_power_on(hub
, portstatus
);
73 usb_unlock_device(hdev
);
74 usb_autopm_put_interface(intf
);
79 return sysfs_emit(buf
, "%s\n", disabled
? "1" : "0");
82 static ssize_t
disable_store(struct device
*dev
, struct device_attribute
*attr
,
83 const char *buf
, size_t count
)
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
;
93 rc
= kstrtobool(buf
, &disabled
);
97 rc
= usb_autopm_get_interface(intf
);
101 usb_lock_device(hdev
);
102 if (hub
->disconnected
) {
107 if (disabled
&& port_dev
->child
)
108 usb_disconnect(&port_dev
->child
);
110 rc
= usb_hub_set_port_power(hdev
, hub
, port1
, !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
);
122 usb_unlock_device(hdev
);
123 usb_autopm_put_interface(intf
);
127 static DEVICE_ATTR_RW(disable
);
129 static ssize_t
location_show(struct device
*dev
,
130 struct device_attribute
*attr
, char *buf
)
132 struct usb_port
*port_dev
= to_usb_port(dev
);
134 return sprintf(buf
, "0x%08x\n", port_dev
->location
);
136 static DEVICE_ATTR_RO(location
);
138 static ssize_t
connect_type_show(struct device
*dev
,
139 struct device_attribute
*attr
, char *buf
)
141 struct usb_port
*port_dev
= to_usb_port(dev
);
144 switch (port_dev
->connect_type
) {
145 case USB_PORT_CONNECT_TYPE_HOT_PLUG
:
148 case USB_PORT_CONNECT_TYPE_HARD_WIRED
:
149 result
= "hardwired";
151 case USB_PORT_NOT_USED
:
159 return sprintf(buf
, "%s\n", result
);
161 static DEVICE_ATTR_RO(connect_type
);
163 static ssize_t
state_show(struct device
*dev
,
164 struct device_attribute
*attr
, char *buf
)
166 struct usb_port
*port_dev
= to_usb_port(dev
);
167 enum usb_device_state state
= READ_ONCE(port_dev
->state
);
169 return sysfs_emit(buf
, "%s\n", usb_state_string(state
));
171 static DEVICE_ATTR_RO(state
);
173 static ssize_t
over_current_count_show(struct device
*dev
,
174 struct device_attribute
*attr
, char *buf
)
176 struct usb_port
*port_dev
= to_usb_port(dev
);
178 return sprintf(buf
, "%u\n", port_dev
->over_current_count
);
180 static DEVICE_ATTR_RO(over_current_count
);
182 static ssize_t
quirks_show(struct device
*dev
,
183 struct device_attribute
*attr
, char *buf
)
185 struct usb_port
*port_dev
= to_usb_port(dev
);
187 return sprintf(buf
, "%08x\n", port_dev
->quirks
);
190 static ssize_t
quirks_store(struct device
*dev
, struct device_attribute
*attr
,
191 const char *buf
, size_t count
)
193 struct usb_port
*port_dev
= to_usb_port(dev
);
196 if (kstrtou32(buf
, 16, &value
))
199 port_dev
->quirks
= value
;
202 static DEVICE_ATTR_RW(quirks
);
204 static ssize_t
usb3_lpm_permit_show(struct device
*dev
,
205 struct device_attribute
*attr
, char *buf
)
207 struct usb_port
*port_dev
= to_usb_port(dev
);
210 if (port_dev
->usb3_lpm_u1_permit
) {
211 if (port_dev
->usb3_lpm_u2_permit
)
216 if (port_dev
->usb3_lpm_u2_permit
)
222 return sprintf(buf
, "%s\n", p
);
225 static ssize_t
usb3_lpm_permit_store(struct device
*dev
,
226 struct device_attribute
*attr
,
227 const char *buf
, size_t count
)
229 struct usb_port
*port_dev
= to_usb_port(dev
);
230 struct usb_device
*udev
= port_dev
->child
;
233 if (!strncmp(buf
, "u1_u2", 5)) {
234 port_dev
->usb3_lpm_u1_permit
= 1;
235 port_dev
->usb3_lpm_u2_permit
= 1;
237 } else if (!strncmp(buf
, "u1", 2)) {
238 port_dev
->usb3_lpm_u1_permit
= 1;
239 port_dev
->usb3_lpm_u2_permit
= 0;
241 } else if (!strncmp(buf
, "u2", 2)) {
242 port_dev
->usb3_lpm_u1_permit
= 0;
243 port_dev
->usb3_lpm_u2_permit
= 1;
245 } else if (!strncmp(buf
, "0", 1)) {
246 port_dev
->usb3_lpm_u1_permit
= 0;
247 port_dev
->usb3_lpm_u2_permit
= 0;
251 /* If device is connected to the port, disable or enable lpm
252 * to make new u1 u2 setting take effect immediately.
255 hcd
= bus_to_hcd(udev
->bus
);
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
);
268 static DEVICE_ATTR_RW(usb3_lpm_permit
);
270 static 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
,
281 static const struct attribute_group port_dev_attr_grp
= {
282 .attrs
= port_dev_attrs
,
285 static const struct attribute_group
*port_dev_group
[] = {
290 static struct attribute
*port_dev_usb3_attrs
[] = {
291 &dev_attr_usb3_lpm_permit
.attr
,
295 static const struct attribute_group port_dev_usb3_attr_grp
= {
296 .attrs
= port_dev_usb3_attrs
,
299 static const struct attribute_group
*port_dev_usb3_group
[] = {
301 &port_dev_usb3_attr_grp
,
305 static void usb_port_device_release(struct device
*dev
)
307 struct usb_port
*port_dev
= to_usb_port(dev
);
309 kfree(port_dev
->req
);
314 static int usb_port_runtime_resume(struct device
*dev
)
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
;
328 set_bit(port1
, hub
->power_bits
);
333 * Power on our usb3 peer before this usb2 port to prevent a usb3
334 * device from degrading to its usb2 connection
336 if (!port_dev
->is_superspeed
&& peer
)
337 pm_runtime_get_sync(&peer
->dev
);
339 retval
= usb_autopm_get_interface(intf
);
343 retval
= usb_hub_set_port_power(hdev
, hub
, port1
, true);
344 msleep(hub_power_on_good_delay(hub
));
345 if (udev
&& !retval
) {
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())
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
);
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
);
368 usb_autopm_put_interface(intf
);
373 static int usb_port_runtime_suspend(struct device
*dev
)
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
;
388 if (dev_pm_qos_flags(&port_dev
->dev
, PM_QOS_FLAG_NO_POWER_OFF
)
392 if (usb_port_block_power_off
)
395 retval
= usb_autopm_get_interface(intf
);
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
);
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.
410 if (!port_dev
->is_superspeed
&& peer
)
411 pm_runtime_put(&peer
->dev
);
417 static void usb_port_shutdown(struct device
*dev
)
419 struct usb_port
*port_dev
= to_usb_port(dev
);
422 usb_disable_usb2_hardware_lpm(port_dev
->child
);
425 static const struct dev_pm_ops usb_port_pm_ops
= {
427 .runtime_suspend
= usb_port_runtime_suspend
,
428 .runtime_resume
= usb_port_runtime_resume
,
432 struct device_type usb_port_device_type
= {
434 .release
= usb_port_device_release
,
435 .pm
= &usb_port_pm_ops
,
438 static struct device_driver usb_port_driver
= {
440 .owner
= THIS_MODULE
,
441 .shutdown
= usb_port_shutdown
,
444 static int link_peers(struct usb_port
*left
, struct usb_port
*right
)
446 struct usb_port
*ss_port
, *hs_port
;
449 if (left
->peer
== right
&& right
->peer
== left
)
452 if (left
->peer
|| right
->peer
) {
453 struct usb_port
*lpeer
= left
->peer
;
454 struct usb_port
*rpeer
= right
->peer
;
457 if (left
->location
&& left
->location
== right
->location
)
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");
471 rc
= sysfs_create_link(&left
->dev
.kobj
, &right
->dev
.kobj
, "peer");
474 rc
= sysfs_create_link(&right
->dev
.kobj
, &left
->dev
.kobj
, "peer");
476 sysfs_remove_link(&left
->dev
.kobj
, "peer");
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.
485 if (left
->is_superspeed
) {
487 WARN_ON(right
->is_superspeed
);
491 WARN_ON(!right
->is_superspeed
);
494 pm_runtime_get_sync(&hs_port
->dev
);
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.
505 * Also, drop the HiSpeed ref taken above.
507 pm_runtime_get_sync(&ss_port
->dev
);
508 pm_runtime_put(&hs_port
->dev
);
513 static void link_peers_report(struct usb_port
*left
, struct usb_port
*right
)
517 rc
= link_peers(left
, right
);
519 dev_dbg(&left
->dev
, "peered to %s\n", dev_name(&right
->dev
));
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;
528 static void unlink_peers(struct usb_port
*left
, struct usb_port
*right
)
530 struct usb_port
*ss_port
, *hs_port
;
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
));
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.
541 if (left
->is_superspeed
) {
549 pm_runtime_get_sync(&hs_port
->dev
);
551 sysfs_remove_link(&left
->dev
.kobj
, "peer");
553 sysfs_remove_link(&right
->dev
.kobj
, "peer");
556 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
557 pm_runtime_put(&ss_port
->dev
);
559 /* Drop the ref taken above */
560 pm_runtime_put(&hs_port
->dev
);
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
568 static int match_location(struct usb_device
*peer_hdev
, void *p
)
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
);
576 if (!peer_hub
|| port_dev
->connect_type
== USB_PORT_NOT_USED
)
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
)
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
);
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.
602 static void find_and_link_peer(struct usb_hub
*hub
, int port1
)
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
;
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)
615 if (port_dev
->location
) {
616 /* we link the peer in match_location() if found */
617 usb_for_each_dev(port_dev
, match_location
);
619 } else if (!hdev
->parent
) {
620 struct usb_hcd
*hcd
= bus_to_hcd(hdev
->bus
);
621 struct usb_hcd
*peer_hcd
= hcd
->shared_hcd
;
626 peer_hdev
= peer_hcd
->self
.root_hub
;
628 struct usb_port
*upstream
;
629 struct usb_device
*parent
= hdev
->parent
;
630 struct usb_hub
*parent_hub
= usb_hub_to_struct_hub(parent
);
635 upstream
= parent_hub
->ports
[hdev
->portnum
- 1];
636 if (!upstream
|| !upstream
->peer
)
639 peer_hdev
= upstream
->peer
->child
;
642 peer_hub
= usb_hub_to_struct_hub(peer_hdev
);
643 if (!peer_hub
|| port1
> peer_hdev
->maxchild
)
647 * we found a valid default peer, last check is to make sure it
648 * does not have location data
650 peer
= peer_hub
->ports
[port1
- 1];
651 if (peer
&& peer
->location
== 0)
652 link_peers_report(port_dev
, peer
);
655 static int connector_bind(struct device
*dev
, struct device
*connector
, void *data
)
657 struct usb_port
*port_dev
= to_usb_port(dev
);
660 ret
= sysfs_create_link(&dev
->kobj
, &connector
->kobj
, "connector");
664 ret
= sysfs_create_link(&connector
->kobj
, &dev
->kobj
, dev_name(dev
));
666 sysfs_remove_link(&dev
->kobj
, "connector");
670 port_dev
->connector
= data
;
673 * If there is already USB device connected to the port, letting the
674 * Type-C connector know about it immediately.
677 typec_attach(port_dev
->connector
, &port_dev
->child
->dev
);
682 static void connector_unbind(struct device
*dev
, struct device
*connector
, void *data
)
684 struct usb_port
*port_dev
= to_usb_port(dev
);
686 sysfs_remove_link(&connector
->kobj
, dev_name(dev
));
687 sysfs_remove_link(&dev
->kobj
, "connector");
688 port_dev
->connector
= NULL
;
691 static const struct component_ops connector_ops
= {
692 .bind
= connector_bind
,
693 .unbind
= connector_unbind
,
696 int usb_hub_create_port_device(struct usb_hub
*hub
, int port1
)
698 struct usb_port
*port_dev
;
699 struct usb_device
*hdev
= hub
->hdev
;
702 port_dev
= kzalloc(sizeof(*port_dev
), GFP_KERNEL
);
706 port_dev
->req
= kzalloc(sizeof(*(port_dev
->req
)), GFP_KERNEL
);
707 if (!port_dev
->req
) {
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
;
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
),
727 mutex_init(&port_dev
->status_lock
);
728 retval
= device_register(&port_dev
->dev
);
730 put_device(&port_dev
->dev
);
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");
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
);
748 retval
= component_add(&port_dev
->dev
, &connector_ops
);
750 dev_warn(&port_dev
->dev
, "failed to add component\n");
754 find_and_link_peer(hub
, port1
);
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).
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
);
767 * Keep hidden the ability to enable port-poweroff if the hub
768 * does not support power switching.
770 if (!hub_is_port_power_switchable(hub
))
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
);
777 dev_warn(&port_dev
->dev
, "failed to expose pm_qos_no_poweroff\n");
781 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
782 retval
= dev_pm_qos_remove_request(port_dev
->req
);
784 kfree(port_dev
->req
);
785 port_dev
->req
= NULL
;
790 sysfs_put(port_dev
->state_kn
);
792 device_unregister(&port_dev
->dev
);
797 void usb_hub_remove_port_device(struct usb_hub
*hub
, int port1
)
799 struct usb_port
*port_dev
= hub
->ports
[port1
- 1];
800 struct usb_port
*peer
;
802 peer
= port_dev
->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
);