Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: core: add phy notify connect and disconnect

In Realtek SoC, the parameter of usb phy is designed to can dynamic
tuning base on port status. Therefore, add a notify callback of generic
phy driver when usb device connect and disconnect change.

The Realtek phy driver is designed to dynamically adjust disconnection
level and calibrate phy parameters. When the device connected bit changes
and when the disconnected bit changes, do connection change notification:

Check if portstatus is USB_PORT_STAT_CONNECTION and portchange is
USB_PORT_STAT_C_CONNECTION.
1. The device is connected, the driver lowers the disconnection level and
calibrates the phy parameters.
2. The device disconnects, the driver increases the disconnect level and
calibrates the phy parameters.

Generic phy driver in usb core framework does not support device connect
and disconnect notifications. Therefore, we add an api to notify phy
the connection changes.

Additionally, the generic phy only specifies primary_hcd in the original
design. Added specific "usb2-phy" on primary_hcd and "usb3-phy" on
shared_hcd.

Signed-off-by: Stanley Chang <stanley_chang@realtek.com>
Link: https://lore.kernel.org/r/20231213031203.4911-4-stanley_chang@realtek.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Stanley Chang and committed by
Greg Kroah-Hartman
53a2d95d b48415af

+162 -4
+10 -4
drivers/usb/core/hcd.c
··· 2792 2792 struct usb_device *rhdev; 2793 2793 struct usb_hcd *shared_hcd; 2794 2794 2795 - if (!hcd->skip_phy_initialization && usb_hcd_is_primary_hcd(hcd)) { 2796 - hcd->phy_roothub = usb_phy_roothub_alloc(hcd->self.sysdev); 2797 - if (IS_ERR(hcd->phy_roothub)) 2798 - return PTR_ERR(hcd->phy_roothub); 2795 + if (!hcd->skip_phy_initialization) { 2796 + if (usb_hcd_is_primary_hcd(hcd)) { 2797 + hcd->phy_roothub = usb_phy_roothub_alloc(hcd->self.sysdev); 2798 + if (IS_ERR(hcd->phy_roothub)) 2799 + return PTR_ERR(hcd->phy_roothub); 2800 + } else { 2801 + hcd->phy_roothub = usb_phy_roothub_alloc_usb3_phy(hcd->self.sysdev); 2802 + if (IS_ERR(hcd->phy_roothub)) 2803 + return PTR_ERR(hcd->phy_roothub); 2804 + } 2799 2805 2800 2806 retval = usb_phy_roothub_init(hcd->phy_roothub); 2801 2807 if (retval)
+29
drivers/usb/core/hub.c
··· 37 37 #include <asm/byteorder.h> 38 38 39 39 #include "hub.h" 40 + #include "phy.h" 40 41 #include "otg_productlist.h" 41 42 42 43 #define USB_VENDOR_GENESYS_LOGIC 0x05e3 ··· 635 634 ret = 0; 636 635 } 637 636 mutex_unlock(&hub->status_mutex); 637 + 638 + /* 639 + * There is no need to lock status_mutex here, because status_mutex 640 + * protects hub->status, and the phy driver only checks the port 641 + * status without changing the status. 642 + */ 643 + if (!ret) { 644 + struct usb_device *hdev = hub->hdev; 645 + 646 + /* 647 + * Only roothub will be notified of connection changes, 648 + * since the USB PHY only cares about changes at the next 649 + * level. 650 + */ 651 + if (is_root_hub(hdev)) { 652 + struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 653 + bool connect; 654 + bool connect_change; 655 + 656 + connect_change = *change & USB_PORT_STAT_C_CONNECTION; 657 + connect = *status & USB_PORT_STAT_CONNECTION; 658 + if (connect_change && connect) 659 + usb_phy_roothub_notify_connect(hcd->phy_roothub, port1 - 1); 660 + else if (connect_change) 661 + usb_phy_roothub_notify_disconnect(hcd->phy_roothub, port1 - 1); 662 + } 663 + } 664 + 638 665 return ret; 639 666 } 640 667
+120
drivers/usb/core/phy.c
··· 19 19 struct list_head list; 20 20 }; 21 21 22 + /* Allocate the roothub_entry by specific name of phy */ 23 + static int usb_phy_roothub_add_phy_by_name(struct device *dev, const char *name, 24 + struct list_head *list) 25 + { 26 + struct usb_phy_roothub *roothub_entry; 27 + struct phy *phy; 28 + 29 + phy = devm_of_phy_get(dev, dev->of_node, name); 30 + if (IS_ERR(phy)) 31 + return PTR_ERR(phy); 32 + 33 + roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL); 34 + if (!roothub_entry) 35 + return -ENOMEM; 36 + 37 + INIT_LIST_HEAD(&roothub_entry->list); 38 + 39 + roothub_entry->phy = phy; 40 + 41 + list_add_tail(&roothub_entry->list, list); 42 + 43 + return 0; 44 + } 45 + 22 46 static int usb_phy_roothub_add_phy(struct device *dev, int index, 23 47 struct list_head *list) 24 48 { ··· 89 65 90 66 INIT_LIST_HEAD(&phy_roothub->list); 91 67 68 + if (!usb_phy_roothub_add_phy_by_name(dev, "usb2-phy", &phy_roothub->list)) 69 + return phy_roothub; 70 + 92 71 for (i = 0; i < num_phys; i++) { 93 72 err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list); 94 73 if (err) ··· 101 74 return phy_roothub; 102 75 } 103 76 EXPORT_SYMBOL_GPL(usb_phy_roothub_alloc); 77 + 78 + /** 79 + * usb_phy_roothub_alloc_usb3_phy - alloc the roothub 80 + * @dev: the device of the host controller 81 + * 82 + * Allocate the usb phy roothub if the host use a generic usb3-phy. 83 + * 84 + * Return: On success, a pointer to the usb_phy_roothub. Otherwise, 85 + * %NULL if no use usb3 phy or %-ENOMEM if out of memory. 86 + */ 87 + struct usb_phy_roothub *usb_phy_roothub_alloc_usb3_phy(struct device *dev) 88 + { 89 + struct usb_phy_roothub *phy_roothub; 90 + int num_phys; 91 + 92 + if (!IS_ENABLED(CONFIG_GENERIC_PHY)) 93 + return NULL; 94 + 95 + num_phys = of_count_phandle_with_args(dev->of_node, "phys", 96 + "#phy-cells"); 97 + if (num_phys <= 0) 98 + return NULL; 99 + 100 + phy_roothub = devm_kzalloc(dev, sizeof(*phy_roothub), GFP_KERNEL); 101 + if (!phy_roothub) 102 + return ERR_PTR(-ENOMEM); 103 + 104 + INIT_LIST_HEAD(&phy_roothub->list); 105 + 106 + if (!usb_phy_roothub_add_phy_by_name(dev, "usb3-phy", &phy_roothub->list)) 107 + return phy_roothub; 108 + 109 + return NULL; 110 + } 111 + EXPORT_SYMBOL_GPL(usb_phy_roothub_alloc_usb3_phy); 104 112 105 113 int usb_phy_roothub_init(struct usb_phy_roothub *phy_roothub) 106 114 { ··· 233 171 return 0; 234 172 } 235 173 EXPORT_SYMBOL_GPL(usb_phy_roothub_calibrate); 174 + 175 + /** 176 + * usb_phy_roothub_notify_connect() - connect notification 177 + * @phy_roothub: the phy of roothub, if the host use a generic phy. 178 + * @port: the port index for connect 179 + * 180 + * If the phy needs to get connection status, the callback can be used. 181 + * Returns: %0 if successful, a negative error code otherwise 182 + */ 183 + int usb_phy_roothub_notify_connect(struct usb_phy_roothub *phy_roothub, int port) 184 + { 185 + struct usb_phy_roothub *roothub_entry; 186 + struct list_head *head; 187 + int err; 188 + 189 + if (!phy_roothub) 190 + return 0; 191 + 192 + head = &phy_roothub->list; 193 + 194 + list_for_each_entry(roothub_entry, head, list) { 195 + err = phy_notify_connect(roothub_entry->phy, port); 196 + if (err) 197 + return err; 198 + } 199 + 200 + return 0; 201 + } 202 + EXPORT_SYMBOL_GPL(usb_phy_roothub_notify_connect); 203 + 204 + /** 205 + * usb_phy_roothub_notify_disconnect() - disconnect notification 206 + * @phy_roothub: the phy of roothub, if the host use a generic phy. 207 + * @port: the port index for disconnect 208 + * 209 + * If the phy needs to get connection status, the callback can be used. 210 + * Returns: %0 if successful, a negative error code otherwise 211 + */ 212 + int usb_phy_roothub_notify_disconnect(struct usb_phy_roothub *phy_roothub, int port) 213 + { 214 + struct usb_phy_roothub *roothub_entry; 215 + struct list_head *head; 216 + int err; 217 + 218 + if (!phy_roothub) 219 + return 0; 220 + 221 + head = &phy_roothub->list; 222 + 223 + list_for_each_entry(roothub_entry, head, list) { 224 + err = phy_notify_disconnect(roothub_entry->phy, port); 225 + if (err) 226 + return err; 227 + } 228 + 229 + return 0; 230 + } 231 + EXPORT_SYMBOL_GPL(usb_phy_roothub_notify_disconnect); 236 232 237 233 int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub) 238 234 {
+3
drivers/usb/core/phy.h
··· 12 12 struct usb_phy_roothub; 13 13 14 14 struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev); 15 + struct usb_phy_roothub *usb_phy_roothub_alloc_usb3_phy(struct device *dev); 15 16 16 17 int usb_phy_roothub_init(struct usb_phy_roothub *phy_roothub); 17 18 int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub); ··· 20 19 int usb_phy_roothub_set_mode(struct usb_phy_roothub *phy_roothub, 21 20 enum phy_mode mode); 22 21 int usb_phy_roothub_calibrate(struct usb_phy_roothub *phy_roothub); 22 + int usb_phy_roothub_notify_connect(struct usb_phy_roothub *phy_roothub, int port); 23 + int usb_phy_roothub_notify_disconnect(struct usb_phy_roothub *phy_roothub, int port); 23 24 int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub); 24 25 void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub); 25 26