mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2026-05-05 09:57:21 +02:00
0179c6da07
Commit53a2d95df8("usb: core: add phy notify connect and disconnect") causes double use of the 'usb3-phy' in certain cases. Since that commit, if a generic PHY named 'usb3-phy' is specified in the device tree, that is getting added to the 'phy_roothub' list of the secondary HCD by the usb_phy_roothub_alloc_usb3_phy() function. However, that PHY is getting added also to the primary HCD's 'phy_roothub' list by usb_phy_roothub_alloc() if there is no generic PHY specified with 'usb2-phy' name. This causes that the usb_add_hcd() function executes each phy operations twice on the 'usb3-phy'. Once when the primary HCD is added, then once again when the secondary HCD is added. The issue affects the Marvell Armada 3700 platform at least, where a custom name is used for the USB2 PHY: $ git grep 'phy-names.*usb3' arch/arm64/boot/dts/marvell/armada-37xx.dtsi | tr '\t' ' ' arch/arm64/boot/dts/marvell/armada-37xx.dtsi: phy-names = "usb3-phy", "usb2-utmi-otg-phy"; Extend the usb_phy_roothub_alloc_usb3_phy() function to skip adding the 'usb3-phy' to the 'phy_roothub' list of the secondary HCD when 'usb2-phy' is not specified in the device tree to avoid the double use. Fixes:53a2d95df8("usb: core: add phy notify connect and disconnect") Cc: stable <stable@kernel.org> Signed-off-by: Gabor Juhos <j4g8y7@gmail.com> Link: https://patch.msgid.link/20260330-usb-avoid-usb3-phy-double-use-v1-1-d2113aecb535@gmail.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
372 lines
8.3 KiB
C
372 lines
8.3 KiB
C
// SPDX-License-Identifier: GPL-2.0+
|
|
/*
|
|
* A wrapper for multiple PHYs which passes all phy_* function calls to
|
|
* multiple (actual) PHY devices. This is comes handy when initializing
|
|
* all PHYs on a HCD and to keep them all in the same state.
|
|
*
|
|
* Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
|
|
*/
|
|
|
|
#include <linux/device.h>
|
|
#include <linux/list.h>
|
|
#include <linux/phy/phy.h>
|
|
#include <linux/of.h>
|
|
|
|
#include "phy.h"
|
|
|
|
struct usb_phy_roothub {
|
|
struct phy *phy;
|
|
struct list_head list;
|
|
};
|
|
|
|
/* Allocate the roothub_entry by specific name of phy */
|
|
static int usb_phy_roothub_add_phy_by_name(struct device *dev, const char *name,
|
|
struct list_head *list)
|
|
{
|
|
struct usb_phy_roothub *roothub_entry;
|
|
struct phy *phy;
|
|
|
|
phy = devm_of_phy_get(dev, dev->of_node, name);
|
|
if (IS_ERR(phy))
|
|
return PTR_ERR(phy);
|
|
|
|
roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
|
|
if (!roothub_entry)
|
|
return -ENOMEM;
|
|
|
|
INIT_LIST_HEAD(&roothub_entry->list);
|
|
|
|
roothub_entry->phy = phy;
|
|
|
|
list_add_tail(&roothub_entry->list, list);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int usb_phy_roothub_add_phy(struct device *dev, int index,
|
|
struct list_head *list)
|
|
{
|
|
struct usb_phy_roothub *roothub_entry;
|
|
struct phy *phy;
|
|
|
|
phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
|
|
if (IS_ERR(phy)) {
|
|
if (PTR_ERR(phy) == -ENODEV)
|
|
return 0;
|
|
else
|
|
return PTR_ERR(phy);
|
|
}
|
|
|
|
roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
|
|
if (!roothub_entry)
|
|
return -ENOMEM;
|
|
|
|
INIT_LIST_HEAD(&roothub_entry->list);
|
|
|
|
roothub_entry->phy = phy;
|
|
|
|
list_add_tail(&roothub_entry->list, list);
|
|
|
|
return 0;
|
|
}
|
|
|
|
struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
|
|
{
|
|
struct usb_phy_roothub *phy_roothub;
|
|
int i, num_phys, err;
|
|
|
|
if (!IS_ENABLED(CONFIG_GENERIC_PHY))
|
|
return NULL;
|
|
|
|
num_phys = of_count_phandle_with_args(dev->of_node, "phys",
|
|
"#phy-cells");
|
|
if (num_phys <= 0)
|
|
return NULL;
|
|
|
|
phy_roothub = devm_kzalloc(dev, sizeof(*phy_roothub), GFP_KERNEL);
|
|
if (!phy_roothub)
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
INIT_LIST_HEAD(&phy_roothub->list);
|
|
|
|
if (!usb_phy_roothub_add_phy_by_name(dev, "usb2-phy", &phy_roothub->list))
|
|
return phy_roothub;
|
|
|
|
for (i = 0; i < num_phys; i++) {
|
|
err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
|
|
if (err)
|
|
return ERR_PTR(err);
|
|
}
|
|
|
|
return phy_roothub;
|
|
}
|
|
EXPORT_SYMBOL_GPL(usb_phy_roothub_alloc);
|
|
|
|
/**
|
|
* usb_phy_roothub_alloc_usb3_phy - alloc the roothub
|
|
* @dev: the device of the host controller
|
|
*
|
|
* Allocate the usb phy roothub if the host use a generic usb3-phy.
|
|
*
|
|
* Return: On success, a pointer to the usb_phy_roothub. Otherwise,
|
|
* %NULL if no use usb3 phy or %-ENOMEM if out of memory.
|
|
*/
|
|
struct usb_phy_roothub *usb_phy_roothub_alloc_usb3_phy(struct device *dev)
|
|
{
|
|
struct usb_phy_roothub *phy_roothub;
|
|
int num_phys, usb2_phy_index;
|
|
|
|
if (!IS_ENABLED(CONFIG_GENERIC_PHY))
|
|
return NULL;
|
|
|
|
num_phys = of_count_phandle_with_args(dev->of_node, "phys",
|
|
"#phy-cells");
|
|
if (num_phys <= 0)
|
|
return NULL;
|
|
|
|
/*
|
|
* If 'usb2-phy' is not present, usb_phy_roothub_alloc() added
|
|
* all PHYs to the primary HCD's phy_roothub already, so skip
|
|
* adding 'usb3-phy' here to avoid double use of that.
|
|
*/
|
|
usb2_phy_index = of_property_match_string(dev->of_node, "phy-names",
|
|
"usb2-phy");
|
|
if (usb2_phy_index < 0)
|
|
return NULL;
|
|
|
|
phy_roothub = devm_kzalloc(dev, sizeof(*phy_roothub), GFP_KERNEL);
|
|
if (!phy_roothub)
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
INIT_LIST_HEAD(&phy_roothub->list);
|
|
|
|
if (!usb_phy_roothub_add_phy_by_name(dev, "usb3-phy", &phy_roothub->list))
|
|
return phy_roothub;
|
|
|
|
return NULL;
|
|
}
|
|
EXPORT_SYMBOL_GPL(usb_phy_roothub_alloc_usb3_phy);
|
|
|
|
int usb_phy_roothub_init(struct usb_phy_roothub *phy_roothub)
|
|
{
|
|
struct usb_phy_roothub *roothub_entry;
|
|
struct list_head *head;
|
|
int err;
|
|
|
|
if (!phy_roothub)
|
|
return 0;
|
|
|
|
head = &phy_roothub->list;
|
|
|
|
list_for_each_entry(roothub_entry, head, list) {
|
|
err = phy_init(roothub_entry->phy);
|
|
if (err)
|
|
goto err_exit_phys;
|
|
}
|
|
|
|
return 0;
|
|
|
|
err_exit_phys:
|
|
list_for_each_entry_continue_reverse(roothub_entry, head, list)
|
|
phy_exit(roothub_entry->phy);
|
|
|
|
return err;
|
|
}
|
|
EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
|
|
|
|
int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
|
|
{
|
|
struct usb_phy_roothub *roothub_entry;
|
|
struct list_head *head;
|
|
int err, ret = 0;
|
|
|
|
if (!phy_roothub)
|
|
return 0;
|
|
|
|
head = &phy_roothub->list;
|
|
|
|
list_for_each_entry(roothub_entry, head, list) {
|
|
err = phy_exit(roothub_entry->phy);
|
|
if (err)
|
|
ret = err;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
|
|
|
|
int usb_phy_roothub_set_mode(struct usb_phy_roothub *phy_roothub,
|
|
enum phy_mode mode)
|
|
{
|
|
struct usb_phy_roothub *roothub_entry;
|
|
struct list_head *head;
|
|
int err;
|
|
|
|
if (!phy_roothub)
|
|
return 0;
|
|
|
|
head = &phy_roothub->list;
|
|
|
|
list_for_each_entry(roothub_entry, head, list) {
|
|
err = phy_set_mode(roothub_entry->phy, mode);
|
|
if (err)
|
|
return err;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(usb_phy_roothub_set_mode);
|
|
|
|
int usb_phy_roothub_calibrate(struct usb_phy_roothub *phy_roothub)
|
|
{
|
|
struct usb_phy_roothub *roothub_entry;
|
|
struct list_head *head;
|
|
int err;
|
|
|
|
if (!phy_roothub)
|
|
return 0;
|
|
|
|
head = &phy_roothub->list;
|
|
|
|
list_for_each_entry(roothub_entry, head, list) {
|
|
err = phy_calibrate(roothub_entry->phy);
|
|
if (err)
|
|
return err;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(usb_phy_roothub_calibrate);
|
|
|
|
/**
|
|
* usb_phy_roothub_notify_connect() - connect notification
|
|
* @phy_roothub: the phy of roothub, if the host use a generic phy.
|
|
* @port: the port index for connect
|
|
*
|
|
* If the phy needs to get connection status, the callback can be used.
|
|
* Returns: %0 if successful, a negative error code otherwise
|
|
*/
|
|
int usb_phy_roothub_notify_connect(struct usb_phy_roothub *phy_roothub, int port)
|
|
{
|
|
struct usb_phy_roothub *roothub_entry;
|
|
struct list_head *head;
|
|
int err;
|
|
|
|
if (!phy_roothub)
|
|
return 0;
|
|
|
|
head = &phy_roothub->list;
|
|
|
|
list_for_each_entry(roothub_entry, head, list) {
|
|
err = phy_notify_connect(roothub_entry->phy, port);
|
|
if (err)
|
|
return err;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(usb_phy_roothub_notify_connect);
|
|
|
|
/**
|
|
* usb_phy_roothub_notify_disconnect() - disconnect notification
|
|
* @phy_roothub: the phy of roothub, if the host use a generic phy.
|
|
* @port: the port index for disconnect
|
|
*
|
|
* If the phy needs to get connection status, the callback can be used.
|
|
* Returns: %0 if successful, a negative error code otherwise
|
|
*/
|
|
int usb_phy_roothub_notify_disconnect(struct usb_phy_roothub *phy_roothub, int port)
|
|
{
|
|
struct usb_phy_roothub *roothub_entry;
|
|
struct list_head *head;
|
|
int err;
|
|
|
|
if (!phy_roothub)
|
|
return 0;
|
|
|
|
head = &phy_roothub->list;
|
|
|
|
list_for_each_entry(roothub_entry, head, list) {
|
|
err = phy_notify_disconnect(roothub_entry->phy, port);
|
|
if (err)
|
|
return err;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(usb_phy_roothub_notify_disconnect);
|
|
|
|
int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
|
|
{
|
|
struct usb_phy_roothub *roothub_entry;
|
|
struct list_head *head;
|
|
int err;
|
|
|
|
if (!phy_roothub)
|
|
return 0;
|
|
|
|
head = &phy_roothub->list;
|
|
|
|
list_for_each_entry(roothub_entry, head, list) {
|
|
err = phy_power_on(roothub_entry->phy);
|
|
if (err)
|
|
goto err_out;
|
|
}
|
|
|
|
return 0;
|
|
|
|
err_out:
|
|
list_for_each_entry_continue_reverse(roothub_entry, head, list)
|
|
phy_power_off(roothub_entry->phy);
|
|
|
|
return err;
|
|
}
|
|
EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
|
|
|
|
void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
|
|
{
|
|
struct usb_phy_roothub *roothub_entry;
|
|
|
|
if (!phy_roothub)
|
|
return;
|
|
|
|
list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
|
|
phy_power_off(roothub_entry->phy);
|
|
}
|
|
EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
|
|
|
|
int usb_phy_roothub_suspend(struct device *controller_dev,
|
|
struct usb_phy_roothub *phy_roothub)
|
|
{
|
|
usb_phy_roothub_power_off(phy_roothub);
|
|
|
|
/* keep the PHYs initialized so the device can wake up the system */
|
|
if (device_may_wakeup(controller_dev))
|
|
return 0;
|
|
|
|
return usb_phy_roothub_exit(phy_roothub);
|
|
}
|
|
EXPORT_SYMBOL_GPL(usb_phy_roothub_suspend);
|
|
|
|
int usb_phy_roothub_resume(struct device *controller_dev,
|
|
struct usb_phy_roothub *phy_roothub)
|
|
{
|
|
int err;
|
|
|
|
/* if the device can't wake up the system _exit was called */
|
|
if (!device_may_wakeup(controller_dev)) {
|
|
err = usb_phy_roothub_init(phy_roothub);
|
|
if (err)
|
|
return err;
|
|
}
|
|
|
|
err = usb_phy_roothub_power_on(phy_roothub);
|
|
|
|
/* undo _init if _power_on failed */
|
|
if (err && !device_may_wakeup(controller_dev))
|
|
usb_phy_roothub_exit(phy_roothub);
|
|
|
|
return err;
|
|
}
|
|
EXPORT_SYMBOL_GPL(usb_phy_roothub_resume);
|