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>
14 #include <linux/usb/of.h>
18 static int usb_port_block_power_off
;
20 static const struct attribute_group
*port_dev_group
[];
22 static ssize_t
early_stop_show(struct device
*dev
,
23 struct device_attribute
*attr
, char *buf
)
25 struct usb_port
*port_dev
= to_usb_port(dev
);
27 return sysfs_emit(buf
, "%s\n", port_dev
->early_stop
? "yes" : "no");
30 static ssize_t
early_stop_store(struct device
*dev
, struct device_attribute
*attr
,
31 const char *buf
, size_t count
)
33 struct usb_port
*port_dev
= to_usb_port(dev
);
36 if (kstrtobool(buf
, &value
))
40 port_dev
->early_stop
= 1;
42 port_dev
->early_stop
= 0;
46 static DEVICE_ATTR_RW(early_stop
);
48 static ssize_t
disable_show(struct device
*dev
,
49 struct device_attribute
*attr
, char *buf
)
51 struct usb_port
*port_dev
= to_usb_port(dev
);
52 struct usb_device
*hdev
= to_usb_device(dev
->parent
->parent
);
53 struct usb_hub
*hub
= usb_hub_to_struct_hub(hdev
);
54 struct usb_interface
*intf
= to_usb_interface(hub
->intfdev
);
55 int port1
= port_dev
->portnum
;
56 u16 portstatus
, unused
;
60 rc
= usb_autopm_get_interface(intf
);
64 usb_lock_device(hdev
);
65 if (hub
->disconnected
) {
70 usb_hub_port_status(hub
, port1
, &portstatus
, &unused
);
71 disabled
= !usb_port_is_power_on(hub
, portstatus
);
74 usb_unlock_device(hdev
);
75 usb_autopm_put_interface(intf
);
80 return sysfs_emit(buf
, "%s\n", disabled
? "1" : "0");
83 static ssize_t
disable_store(struct device
*dev
, struct device_attribute
*attr
,
84 const char *buf
, size_t count
)
86 struct usb_port
*port_dev
= to_usb_port(dev
);
87 struct usb_device
*hdev
= to_usb_device(dev
->parent
->parent
);
88 struct usb_hub
*hub
= usb_hub_to_struct_hub(hdev
);
89 struct usb_interface
*intf
= to_usb_interface(hub
->intfdev
);
90 int port1
= port_dev
->portnum
;
94 rc
= kstrtobool(buf
, &disabled
);
98 rc
= usb_autopm_get_interface(intf
);
102 usb_lock_device(hdev
);
103 if (hub
->disconnected
) {
108 if (disabled
&& port_dev
->child
)
109 usb_disconnect(&port_dev
->child
);
111 rc
= usb_hub_set_port_power(hdev
, hub
, port1
, !disabled
);
114 usb_clear_port_feature(hdev
, port1
, USB_PORT_FEAT_C_CONNECTION
);
115 if (!port_dev
->is_superspeed
)
116 usb_clear_port_feature(hdev
, port1
, USB_PORT_FEAT_C_ENABLE
);
123 usb_unlock_device(hdev
);
124 usb_autopm_put_interface(intf
);
128 static DEVICE_ATTR_RW(disable
);
130 static ssize_t
location_show(struct device
*dev
,
131 struct device_attribute
*attr
, char *buf
)
133 struct usb_port
*port_dev
= to_usb_port(dev
);
135 return sprintf(buf
, "0x%08x\n", port_dev
->location
);
137 static DEVICE_ATTR_RO(location
);
139 static ssize_t
connect_type_show(struct device
*dev
,
140 struct device_attribute
*attr
, char *buf
)
142 struct usb_port
*port_dev
= to_usb_port(dev
);
145 switch (port_dev
->connect_type
) {
146 case USB_PORT_CONNECT_TYPE_HOT_PLUG
:
149 case USB_PORT_CONNECT_TYPE_HARD_WIRED
:
150 result
= "hardwired";
152 case USB_PORT_NOT_USED
:
160 return sprintf(buf
, "%s\n", result
);
162 static DEVICE_ATTR_RO(connect_type
);
164 static ssize_t
state_show(struct device
*dev
,
165 struct device_attribute
*attr
, char *buf
)
167 struct usb_port
*port_dev
= to_usb_port(dev
);
168 enum usb_device_state state
= READ_ONCE(port_dev
->state
);
170 return sysfs_emit(buf
, "%s\n", usb_state_string(state
));
172 static DEVICE_ATTR_RO(state
);
174 static ssize_t
over_current_count_show(struct device
*dev
,
175 struct device_attribute
*attr
, char *buf
)
177 struct usb_port
*port_dev
= to_usb_port(dev
);
179 return sprintf(buf
, "%u\n", port_dev
->over_current_count
);
181 static DEVICE_ATTR_RO(over_current_count
);
183 static ssize_t
quirks_show(struct device
*dev
,
184 struct device_attribute
*attr
, char *buf
)
186 struct usb_port
*port_dev
= to_usb_port(dev
);
188 return sprintf(buf
, "%08x\n", port_dev
->quirks
);
191 static ssize_t
quirks_store(struct device
*dev
, struct device_attribute
*attr
,
192 const char *buf
, size_t count
)
194 struct usb_port
*port_dev
= to_usb_port(dev
);
197 if (kstrtou32(buf
, 16, &value
))
200 port_dev
->quirks
= value
;
203 static DEVICE_ATTR_RW(quirks
);
205 static ssize_t
usb3_lpm_permit_show(struct device
*dev
,
206 struct device_attribute
*attr
, char *buf
)
208 struct usb_port
*port_dev
= to_usb_port(dev
);
211 if (port_dev
->usb3_lpm_u1_permit
) {
212 if (port_dev
->usb3_lpm_u2_permit
)
217 if (port_dev
->usb3_lpm_u2_permit
)
223 return sprintf(buf
, "%s\n", p
);
226 static ssize_t
usb3_lpm_permit_store(struct device
*dev
,
227 struct device_attribute
*attr
,
228 const char *buf
, size_t count
)
230 struct usb_port
*port_dev
= to_usb_port(dev
);
231 struct usb_device
*udev
= port_dev
->child
;
234 if (!strncmp(buf
, "u1_u2", 5)) {
235 port_dev
->usb3_lpm_u1_permit
= 1;
236 port_dev
->usb3_lpm_u2_permit
= 1;
238 } else if (!strncmp(buf
, "u1", 2)) {
239 port_dev
->usb3_lpm_u1_permit
= 1;
240 port_dev
->usb3_lpm_u2_permit
= 0;
242 } else if (!strncmp(buf
, "u2", 2)) {
243 port_dev
->usb3_lpm_u1_permit
= 0;
244 port_dev
->usb3_lpm_u2_permit
= 1;
246 } else if (!strncmp(buf
, "0", 1)) {
247 port_dev
->usb3_lpm_u1_permit
= 0;
248 port_dev
->usb3_lpm_u2_permit
= 0;
252 /* If device is connected to the port, disable or enable lpm
253 * to make new u1 u2 setting take effect immediately.
256 hcd
= bus_to_hcd(udev
->bus
);
259 usb_lock_device(udev
);
260 mutex_lock(hcd
->bandwidth_mutex
);
261 if (!usb_disable_lpm(udev
))
262 usb_enable_lpm(udev
);
263 mutex_unlock(hcd
->bandwidth_mutex
);
264 usb_unlock_device(udev
);
269 static DEVICE_ATTR_RW(usb3_lpm_permit
);
271 static struct attribute
*port_dev_attrs
[] = {
272 &dev_attr_connect_type
.attr
,
273 &dev_attr_state
.attr
,
274 &dev_attr_location
.attr
,
275 &dev_attr_quirks
.attr
,
276 &dev_attr_over_current_count
.attr
,
277 &dev_attr_disable
.attr
,
278 &dev_attr_early_stop
.attr
,
282 static const struct attribute_group port_dev_attr_grp
= {
283 .attrs
= port_dev_attrs
,
286 static const struct attribute_group
*port_dev_group
[] = {
291 static struct attribute
*port_dev_usb3_attrs
[] = {
292 &dev_attr_usb3_lpm_permit
.attr
,
296 static const struct attribute_group port_dev_usb3_attr_grp
= {
297 .attrs
= port_dev_usb3_attrs
,
300 static const struct attribute_group
*port_dev_usb3_group
[] = {
302 &port_dev_usb3_attr_grp
,
306 static void usb_port_device_release(struct device
*dev
)
308 struct usb_port
*port_dev
= to_usb_port(dev
);
310 kfree(port_dev
->req
);
315 static int usb_port_runtime_resume(struct device
*dev
)
317 struct usb_port
*port_dev
= to_usb_port(dev
);
318 struct usb_device
*hdev
= to_usb_device(dev
->parent
->parent
);
319 struct usb_interface
*intf
= to_usb_interface(dev
->parent
);
320 struct usb_hub
*hub
= usb_hub_to_struct_hub(hdev
);
321 struct usb_device
*udev
= port_dev
->child
;
322 struct usb_port
*peer
= port_dev
->peer
;
323 int port1
= port_dev
->portnum
;
329 set_bit(port1
, hub
->power_bits
);
334 * Power on our usb3 peer before this usb2 port to prevent a usb3
335 * device from degrading to its usb2 connection
337 if (!port_dev
->is_superspeed
&& peer
)
338 pm_runtime_get_sync(&peer
->dev
);
340 retval
= usb_autopm_get_interface(intf
);
344 retval
= usb_hub_set_port_power(hdev
, hub
, port1
, true);
345 msleep(hub_power_on_good_delay(hub
));
346 if (udev
&& !retval
) {
348 * Our preference is to simply wait for the port to reconnect,
349 * as that is the lowest latency method to restart the port.
350 * However, there are cases where toggling port power results in
351 * the host port and the device port getting out of sync causing
352 * a link training live lock. Upon timeout, flag the port as
353 * needing warm reset recovery (to be performed later by
354 * usb_port_resume() as requested via usb_wakeup_notification())
356 if (hub_port_debounce_be_connected(hub
, port1
) < 0) {
357 dev_dbg(&port_dev
->dev
, "reconnect timeout\n");
358 if (hub_is_superspeed(hdev
))
359 set_bit(port1
, hub
->warm_reset_bits
);
362 /* Force the child awake to revalidate after the power loss. */
363 if (!test_and_set_bit(port1
, hub
->child_usage_bits
)) {
364 pm_runtime_get_noresume(&port_dev
->dev
);
365 pm_request_resume(&udev
->dev
);
369 usb_autopm_put_interface(intf
);
374 static int usb_port_runtime_suspend(struct device
*dev
)
376 struct usb_port
*port_dev
= to_usb_port(dev
);
377 struct usb_device
*hdev
= to_usb_device(dev
->parent
->parent
);
378 struct usb_interface
*intf
= to_usb_interface(dev
->parent
);
379 struct usb_hub
*hub
= usb_hub_to_struct_hub(hdev
);
380 struct usb_port
*peer
= port_dev
->peer
;
381 int port1
= port_dev
->portnum
;
389 if (dev_pm_qos_flags(&port_dev
->dev
, PM_QOS_FLAG_NO_POWER_OFF
)
393 if (usb_port_block_power_off
)
396 retval
= usb_autopm_get_interface(intf
);
400 retval
= usb_hub_set_port_power(hdev
, hub
, port1
, false);
401 usb_clear_port_feature(hdev
, port1
, USB_PORT_FEAT_C_CONNECTION
);
402 if (!port_dev
->is_superspeed
)
403 usb_clear_port_feature(hdev
, port1
, USB_PORT_FEAT_C_ENABLE
);
404 usb_autopm_put_interface(intf
);
407 * Our peer usb3 port may now be able to suspend, so
408 * asynchronously queue a suspend request to observe that this
409 * usb2 port is now off.
411 if (!port_dev
->is_superspeed
&& peer
)
412 pm_runtime_put(&peer
->dev
);
418 static void usb_port_shutdown(struct device
*dev
)
420 struct usb_port
*port_dev
= to_usb_port(dev
);
423 usb_disable_usb2_hardware_lpm(port_dev
->child
);
426 static const struct dev_pm_ops usb_port_pm_ops
= {
428 .runtime_suspend
= usb_port_runtime_suspend
,
429 .runtime_resume
= usb_port_runtime_resume
,
433 const struct device_type usb_port_device_type
= {
435 .release
= usb_port_device_release
,
436 .pm
= &usb_port_pm_ops
,
439 static struct device_driver usb_port_driver
= {
441 .owner
= THIS_MODULE
,
442 .shutdown
= usb_port_shutdown
,
445 static int link_peers(struct usb_port
*left
, struct usb_port
*right
)
447 struct usb_port
*ss_port
, *hs_port
;
450 if (left
->peer
== right
&& right
->peer
== left
)
453 if (left
->peer
|| right
->peer
) {
454 struct usb_port
*lpeer
= left
->peer
;
455 struct usb_port
*rpeer
= right
->peer
;
458 if (left
->location
&& left
->location
== right
->location
)
463 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
464 dev_name(&left
->dev
), dev_name(&right
->dev
), method
,
465 dev_name(&left
->dev
),
466 lpeer
? dev_name(&lpeer
->dev
) : "none",
467 dev_name(&right
->dev
),
468 rpeer
? dev_name(&rpeer
->dev
) : "none");
472 rc
= sysfs_create_link(&left
->dev
.kobj
, &right
->dev
.kobj
, "peer");
475 rc
= sysfs_create_link(&right
->dev
.kobj
, &left
->dev
.kobj
, "peer");
477 sysfs_remove_link(&left
->dev
.kobj
, "peer");
482 * We need to wake the HiSpeed port to make sure we don't race
483 * setting ->peer with usb_port_runtime_suspend(). Otherwise we
484 * may miss a suspend event for the SuperSpeed port.
486 if (left
->is_superspeed
) {
488 WARN_ON(right
->is_superspeed
);
492 WARN_ON(!right
->is_superspeed
);
495 pm_runtime_get_sync(&hs_port
->dev
);
501 * The SuperSpeed reference is dropped when the HiSpeed port in
502 * this relationship suspends, i.e. when it is safe to allow a
503 * SuperSpeed connection to drop since there is no risk of a
504 * device degrading to its powered-off HiSpeed connection.
506 * Also, drop the HiSpeed ref taken above.
508 pm_runtime_get_sync(&ss_port
->dev
);
509 pm_runtime_put(&hs_port
->dev
);
514 static void link_peers_report(struct usb_port
*left
, struct usb_port
*right
)
518 rc
= link_peers(left
, right
);
520 dev_dbg(&left
->dev
, "peered to %s\n", dev_name(&right
->dev
));
522 dev_dbg(&left
->dev
, "failed to peer to %s (%d)\n",
523 dev_name(&right
->dev
), rc
);
524 pr_warn_once("usb: port power management may be unreliable\n");
525 usb_port_block_power_off
= 1;
529 static void unlink_peers(struct usb_port
*left
, struct usb_port
*right
)
531 struct usb_port
*ss_port
, *hs_port
;
533 WARN(right
->peer
!= left
|| left
->peer
!= right
,
534 "%s and %s are not peers?\n",
535 dev_name(&left
->dev
), dev_name(&right
->dev
));
538 * We wake the HiSpeed port to make sure we don't race its
539 * usb_port_runtime_resume() event which takes a SuperSpeed ref
540 * when ->peer is !NULL.
542 if (left
->is_superspeed
) {
550 pm_runtime_get_sync(&hs_port
->dev
);
552 sysfs_remove_link(&left
->dev
.kobj
, "peer");
554 sysfs_remove_link(&right
->dev
.kobj
, "peer");
557 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
558 pm_runtime_put(&ss_port
->dev
);
560 /* Drop the ref taken above */
561 pm_runtime_put(&hs_port
->dev
);
565 * For each usb hub device in the system check to see if it is in the
566 * peer domain of the given port_dev, and if it is check to see if it
567 * has a port that matches the given port by location
569 static int match_location(struct usb_device
*peer_hdev
, void *p
)
572 struct usb_hcd
*hcd
, *peer_hcd
;
573 struct usb_port
*port_dev
= p
, *peer
;
574 struct usb_hub
*peer_hub
= usb_hub_to_struct_hub(peer_hdev
);
575 struct usb_device
*hdev
= to_usb_device(port_dev
->dev
.parent
->parent
);
577 if (!peer_hub
|| port_dev
->connect_type
== USB_PORT_NOT_USED
)
580 hcd
= bus_to_hcd(hdev
->bus
);
581 peer_hcd
= bus_to_hcd(peer_hdev
->bus
);
582 /* peer_hcd is provisional until we verify it against the known peer */
583 if (peer_hcd
!= hcd
->shared_hcd
)
586 for (port1
= 1; port1
<= peer_hdev
->maxchild
; port1
++) {
587 peer
= peer_hub
->ports
[port1
- 1];
588 if (peer
&& peer
->connect_type
!= USB_PORT_NOT_USED
&&
589 peer
->location
== port_dev
->location
) {
590 link_peers_report(port_dev
, peer
);
599 * Find the peer port either via explicit platform firmware "location"
600 * data, the peer hcd for root hubs, or the upstream peer relationship
601 * for all other hubs.
603 static void find_and_link_peer(struct usb_hub
*hub
, int port1
)
605 struct usb_port
*port_dev
= hub
->ports
[port1
- 1], *peer
;
606 struct usb_device
*hdev
= hub
->hdev
;
607 struct usb_device
*peer_hdev
;
608 struct usb_hub
*peer_hub
;
611 * If location data is available then we can only peer this port
612 * by a location match, not the default peer (lest we create a
613 * situation where we need to go back and undo a default peering
614 * when the port is later peered by location data)
616 if (port_dev
->location
) {
617 /* we link the peer in match_location() if found */
618 usb_for_each_dev(port_dev
, match_location
);
620 } else if (!hdev
->parent
) {
621 struct usb_hcd
*hcd
= bus_to_hcd(hdev
->bus
);
622 struct usb_hcd
*peer_hcd
= hcd
->shared_hcd
;
627 peer_hdev
= peer_hcd
->self
.root_hub
;
629 struct usb_port
*upstream
;
630 struct usb_device
*parent
= hdev
->parent
;
631 struct usb_hub
*parent_hub
= usb_hub_to_struct_hub(parent
);
636 upstream
= parent_hub
->ports
[hdev
->portnum
- 1];
637 if (!upstream
|| !upstream
->peer
)
640 peer_hdev
= upstream
->peer
->child
;
643 peer_hub
= usb_hub_to_struct_hub(peer_hdev
);
644 if (!peer_hub
|| port1
> peer_hdev
->maxchild
)
648 * we found a valid default peer, last check is to make sure it
649 * does not have location data
651 peer
= peer_hub
->ports
[port1
- 1];
652 if (peer
&& peer
->location
== 0)
653 link_peers_report(port_dev
, peer
);
656 static int connector_bind(struct device
*dev
, struct device
*connector
, void *data
)
658 struct usb_port
*port_dev
= to_usb_port(dev
);
661 ret
= sysfs_create_link(&dev
->kobj
, &connector
->kobj
, "connector");
665 ret
= sysfs_create_link(&connector
->kobj
, &dev
->kobj
, dev_name(dev
));
667 sysfs_remove_link(&dev
->kobj
, "connector");
671 port_dev
->connector
= data
;
674 * If there is already USB device connected to the port, letting the
675 * Type-C connector know about it immediately.
678 typec_attach(port_dev
->connector
, &port_dev
->child
->dev
);
683 static void connector_unbind(struct device
*dev
, struct device
*connector
, void *data
)
685 struct usb_port
*port_dev
= to_usb_port(dev
);
687 sysfs_remove_link(&connector
->kobj
, dev_name(dev
));
688 sysfs_remove_link(&dev
->kobj
, "connector");
689 port_dev
->connector
= NULL
;
692 static const struct component_ops connector_ops
= {
693 .bind
= connector_bind
,
694 .unbind
= connector_unbind
,
697 int usb_hub_create_port_device(struct usb_hub
*hub
, int port1
)
699 struct usb_port
*port_dev
;
700 struct usb_device
*hdev
= hub
->hdev
;
703 port_dev
= kzalloc(sizeof(*port_dev
), GFP_KERNEL
);
707 port_dev
->req
= kzalloc(sizeof(*(port_dev
->req
)), GFP_KERNEL
);
708 if (!port_dev
->req
) {
713 port_dev
->connect_type
= usb_of_get_connect_type(hdev
, port1
);
714 hub
->ports
[port1
- 1] = port_dev
;
715 port_dev
->portnum
= port1
;
716 set_bit(port1
, hub
->power_bits
);
717 port_dev
->dev
.parent
= hub
->intfdev
;
718 if (hub_is_superspeed(hdev
)) {
719 port_dev
->is_superspeed
= 1;
720 port_dev
->usb3_lpm_u1_permit
= 1;
721 port_dev
->usb3_lpm_u2_permit
= 1;
722 port_dev
->dev
.groups
= port_dev_usb3_group
;
724 port_dev
->dev
.groups
= port_dev_group
;
725 port_dev
->dev
.type
= &usb_port_device_type
;
726 port_dev
->dev
.driver
= &usb_port_driver
;
727 dev_set_name(&port_dev
->dev
, "%s-port%d", dev_name(&hub
->hdev
->dev
),
729 mutex_init(&port_dev
->status_lock
);
730 retval
= device_register(&port_dev
->dev
);
732 put_device(&port_dev
->dev
);
736 port_dev
->state_kn
= sysfs_get_dirent(port_dev
->dev
.kobj
.sd
, "state");
737 if (!port_dev
->state_kn
) {
738 dev_err(&port_dev
->dev
, "failed to sysfs_get_dirent 'state'\n");
743 /* Set default policy of port-poweroff disabled. */
744 retval
= dev_pm_qos_add_request(&port_dev
->dev
, port_dev
->req
,
745 DEV_PM_QOS_FLAGS
, PM_QOS_FLAG_NO_POWER_OFF
);
750 retval
= component_add(&port_dev
->dev
, &connector_ops
);
752 dev_warn(&port_dev
->dev
, "failed to add component\n");
756 find_and_link_peer(hub
, port1
);
759 * Enable runtime pm and hold a refernce that hub_configure()
760 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
761 * and the hub has been fully registered (hdev->maxchild set).
763 pm_runtime_set_active(&port_dev
->dev
);
764 pm_runtime_get_noresume(&port_dev
->dev
);
765 pm_runtime_enable(&port_dev
->dev
);
766 device_enable_async_suspend(&port_dev
->dev
);
769 * Keep hidden the ability to enable port-poweroff if the hub
770 * does not support power switching.
772 if (!hub_is_port_power_switchable(hub
))
775 /* Attempt to let userspace take over the policy. */
776 retval
= dev_pm_qos_expose_flags(&port_dev
->dev
,
777 PM_QOS_FLAG_NO_POWER_OFF
);
779 dev_warn(&port_dev
->dev
, "failed to expose pm_qos_no_poweroff\n");
783 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
784 retval
= dev_pm_qos_remove_request(port_dev
->req
);
786 kfree(port_dev
->req
);
787 port_dev
->req
= NULL
;
792 sysfs_put(port_dev
->state_kn
);
794 device_unregister(&port_dev
->dev
);
799 void usb_hub_remove_port_device(struct usb_hub
*hub
, int port1
)
801 struct usb_port
*port_dev
= hub
->ports
[port1
- 1];
802 struct usb_port
*peer
;
804 peer
= port_dev
->peer
;
806 unlink_peers(port_dev
, peer
);
807 component_del(&port_dev
->dev
, &connector_ops
);
808 sysfs_put(port_dev
->state_kn
);
809 device_unregister(&port_dev
->dev
);