15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2aa923ef1SMichal Sojka /* 3aa923ef1SMichal Sojka * Provides code common for host and device side USB. 4aa923ef1SMichal Sojka * 5aa923ef1SMichal Sojka * If either host side (ie. CONFIG_USB=y) or device side USB stack 6aa923ef1SMichal Sojka * (ie. CONFIG_USB_GADGET=y) is compiled in the kernel, this module is 7aa923ef1SMichal Sojka * compiled-in as well. Otherwise, if either of the two stacks is 8aa923ef1SMichal Sojka * compiled as module, this file is compiled as module as well. 9aa923ef1SMichal Sojka */ 10aa923ef1SMichal Sojka 11aa923ef1SMichal Sojka #include <linux/kernel.h> 12aa923ef1SMichal Sojka #include <linux/module.h> 13aa923ef1SMichal Sojka #include <linux/of.h> 14aa923ef1SMichal Sojka #include <linux/usb/ch9.h> 15aa923ef1SMichal Sojka #include <linux/usb/of.h> 16aa923ef1SMichal Sojka #include <linux/usb/otg.h> 1798bfb394SBin Liu #include <linux/of_platform.h> 18*812086d3SGreg Kroah-Hartman #include <linux/debugfs.h> 19*812086d3SGreg Kroah-Hartman #include "common.h" 20aa923ef1SMichal Sojka 214d537f37SChunfeng Yun static const char *const ep_type_names[] = { 224d537f37SChunfeng Yun [USB_ENDPOINT_XFER_CONTROL] = "ctrl", 234d537f37SChunfeng Yun [USB_ENDPOINT_XFER_ISOC] = "isoc", 244d537f37SChunfeng Yun [USB_ENDPOINT_XFER_BULK] = "bulk", 254d537f37SChunfeng Yun [USB_ENDPOINT_XFER_INT] = "intr", 264d537f37SChunfeng Yun }; 274d537f37SChunfeng Yun 284d537f37SChunfeng Yun const char *usb_ep_type_string(int ep_type) 294d537f37SChunfeng Yun { 304d537f37SChunfeng Yun if (ep_type < 0 || ep_type >= ARRAY_SIZE(ep_type_names)) 314d537f37SChunfeng Yun return "unknown"; 324d537f37SChunfeng Yun 334d537f37SChunfeng Yun return ep_type_names[ep_type]; 344d537f37SChunfeng Yun } 354d537f37SChunfeng Yun EXPORT_SYMBOL_GPL(usb_ep_type_string); 364d537f37SChunfeng Yun 37aa923ef1SMichal Sojka const char *usb_otg_state_string(enum usb_otg_state state) 38aa923ef1SMichal Sojka { 39aa923ef1SMichal Sojka static const char *const names[] = { 40aa923ef1SMichal Sojka [OTG_STATE_A_IDLE] = "a_idle", 41aa923ef1SMichal Sojka [OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise", 42aa923ef1SMichal Sojka [OTG_STATE_A_WAIT_BCON] = "a_wait_bcon", 43aa923ef1SMichal Sojka [OTG_STATE_A_HOST] = "a_host", 44aa923ef1SMichal Sojka [OTG_STATE_A_SUSPEND] = "a_suspend", 45aa923ef1SMichal Sojka [OTG_STATE_A_PERIPHERAL] = "a_peripheral", 46aa923ef1SMichal Sojka [OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall", 47aa923ef1SMichal Sojka [OTG_STATE_A_VBUS_ERR] = "a_vbus_err", 48aa923ef1SMichal Sojka [OTG_STATE_B_IDLE] = "b_idle", 49aa923ef1SMichal Sojka [OTG_STATE_B_SRP_INIT] = "b_srp_init", 50aa923ef1SMichal Sojka [OTG_STATE_B_PERIPHERAL] = "b_peripheral", 51aa923ef1SMichal Sojka [OTG_STATE_B_WAIT_ACON] = "b_wait_acon", 52aa923ef1SMichal Sojka [OTG_STATE_B_HOST] = "b_host", 53aa923ef1SMichal Sojka }; 54aa923ef1SMichal Sojka 55aa923ef1SMichal Sojka if (state < 0 || state >= ARRAY_SIZE(names)) 56aa923ef1SMichal Sojka return "UNDEFINED"; 57aa923ef1SMichal Sojka 58aa923ef1SMichal Sojka return names[state]; 59aa923ef1SMichal Sojka } 60aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_otg_state_string); 61aa923ef1SMichal Sojka 62aa923ef1SMichal Sojka static const char *const speed_names[] = { 63aa923ef1SMichal Sojka [USB_SPEED_UNKNOWN] = "UNKNOWN", 64aa923ef1SMichal Sojka [USB_SPEED_LOW] = "low-speed", 65aa923ef1SMichal Sojka [USB_SPEED_FULL] = "full-speed", 66aa923ef1SMichal Sojka [USB_SPEED_HIGH] = "high-speed", 67aa923ef1SMichal Sojka [USB_SPEED_WIRELESS] = "wireless", 68aa923ef1SMichal Sojka [USB_SPEED_SUPER] = "super-speed", 698a1b2725SMathias Nyman [USB_SPEED_SUPER_PLUS] = "super-speed-plus", 70aa923ef1SMichal Sojka }; 71aa923ef1SMichal Sojka 72aa923ef1SMichal Sojka const char *usb_speed_string(enum usb_device_speed speed) 73aa923ef1SMichal Sojka { 74aa923ef1SMichal Sojka if (speed < 0 || speed >= ARRAY_SIZE(speed_names)) 75aa923ef1SMichal Sojka speed = USB_SPEED_UNKNOWN; 76aa923ef1SMichal Sojka return speed_names[speed]; 77aa923ef1SMichal Sojka } 78aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_speed_string); 79aa923ef1SMichal Sojka 8063863b98SHeikki Krogerus enum usb_device_speed usb_get_maximum_speed(struct device *dev) 8163863b98SHeikki Krogerus { 8263863b98SHeikki Krogerus const char *maximum_speed; 83efc2cd79SHeikki Krogerus int ret; 8463863b98SHeikki Krogerus 85efc2cd79SHeikki Krogerus ret = device_property_read_string(dev, "maximum-speed", &maximum_speed); 86efc2cd79SHeikki Krogerus if (ret < 0) 8763863b98SHeikki Krogerus return USB_SPEED_UNKNOWN; 8863863b98SHeikki Krogerus 89efc2cd79SHeikki Krogerus ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed); 9063863b98SHeikki Krogerus 91efc2cd79SHeikki Krogerus return (ret < 0) ? USB_SPEED_UNKNOWN : ret; 9263863b98SHeikki Krogerus } 9363863b98SHeikki Krogerus EXPORT_SYMBOL_GPL(usb_get_maximum_speed); 9463863b98SHeikki Krogerus 95aa923ef1SMichal Sojka const char *usb_state_string(enum usb_device_state state) 96aa923ef1SMichal Sojka { 97aa923ef1SMichal Sojka static const char *const names[] = { 98aa923ef1SMichal Sojka [USB_STATE_NOTATTACHED] = "not attached", 99aa923ef1SMichal Sojka [USB_STATE_ATTACHED] = "attached", 100aa923ef1SMichal Sojka [USB_STATE_POWERED] = "powered", 101aa923ef1SMichal Sojka [USB_STATE_RECONNECTING] = "reconnecting", 102aa923ef1SMichal Sojka [USB_STATE_UNAUTHENTICATED] = "unauthenticated", 103aa923ef1SMichal Sojka [USB_STATE_DEFAULT] = "default", 104aa923ef1SMichal Sojka [USB_STATE_ADDRESS] = "addressed", 105aa923ef1SMichal Sojka [USB_STATE_CONFIGURED] = "configured", 106aa923ef1SMichal Sojka [USB_STATE_SUSPENDED] = "suspended", 107aa923ef1SMichal Sojka }; 108aa923ef1SMichal Sojka 109aa923ef1SMichal Sojka if (state < 0 || state >= ARRAY_SIZE(names)) 110aa923ef1SMichal Sojka return "UNKNOWN"; 111aa923ef1SMichal Sojka 112aa923ef1SMichal Sojka return names[state]; 113aa923ef1SMichal Sojka } 114aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_state_string); 115aa923ef1SMichal Sojka 116aa923ef1SMichal Sojka static const char *const usb_dr_modes[] = { 117aa923ef1SMichal Sojka [USB_DR_MODE_UNKNOWN] = "", 118aa923ef1SMichal Sojka [USB_DR_MODE_HOST] = "host", 119aa923ef1SMichal Sojka [USB_DR_MODE_PERIPHERAL] = "peripheral", 120aa923ef1SMichal Sojka [USB_DR_MODE_OTG] = "otg", 121aa923ef1SMichal Sojka }; 122aa923ef1SMichal Sojka 12398bfb394SBin Liu static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str) 12498bfb394SBin Liu { 125efc2cd79SHeikki Krogerus int ret; 12698bfb394SBin Liu 127efc2cd79SHeikki Krogerus ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str); 128efc2cd79SHeikki Krogerus return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret; 12998bfb394SBin Liu } 13098bfb394SBin Liu 13106e7114fSHeikki Krogerus enum usb_dr_mode usb_get_dr_mode(struct device *dev) 132aa923ef1SMichal Sojka { 133aa923ef1SMichal Sojka const char *dr_mode; 13498bfb394SBin Liu int err; 135aa923ef1SMichal Sojka 13606e7114fSHeikki Krogerus err = device_property_read_string(dev, "dr_mode", &dr_mode); 137aa923ef1SMichal Sojka if (err < 0) 138aa923ef1SMichal Sojka return USB_DR_MODE_UNKNOWN; 139aa923ef1SMichal Sojka 14098bfb394SBin Liu return usb_get_dr_mode_from_string(dr_mode); 141aa923ef1SMichal Sojka } 14206e7114fSHeikki Krogerus EXPORT_SYMBOL_GPL(usb_get_dr_mode); 143aa923ef1SMichal Sojka 14406e7114fSHeikki Krogerus #ifdef CONFIG_OF 145aa923ef1SMichal Sojka /** 14698bfb394SBin Liu * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device 14798bfb394SBin Liu * which is associated with the given phy device_node 14898bfb394SBin Liu * @np: Pointer to the given phy device_node 149ce15ed4cSHans de Goede * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for 150ce15ed4cSHans de Goede * phys which do not have phy-cells 15198bfb394SBin Liu * 15298bfb394SBin Liu * In dts a usb controller associates with phy devices. The function gets 15398bfb394SBin Liu * the string from property 'dr_mode' of the controller associated with the 15498bfb394SBin Liu * given phy device node, and returns the correspondig enum usb_dr_mode. 15598bfb394SBin Liu */ 156ce15ed4cSHans de Goede enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0) 15798bfb394SBin Liu { 15898bfb394SBin Liu struct device_node *controller = NULL; 159ce15ed4cSHans de Goede struct of_phandle_args args; 16098bfb394SBin Liu const char *dr_mode; 16198bfb394SBin Liu int index; 16298bfb394SBin Liu int err; 16398bfb394SBin Liu 16498bfb394SBin Liu do { 16598bfb394SBin Liu controller = of_find_node_with_property(controller, "phys"); 166238e0268SFabrizio Castro if (!of_device_is_available(controller)) 167238e0268SFabrizio Castro continue; 16898bfb394SBin Liu index = 0; 16998bfb394SBin Liu do { 170ce15ed4cSHans de Goede if (arg0 == -1) { 171ce15ed4cSHans de Goede args.np = of_parse_phandle(controller, "phys", 172ce15ed4cSHans de Goede index); 173ce15ed4cSHans de Goede args.args_count = 0; 174ce15ed4cSHans de Goede } else { 175ce15ed4cSHans de Goede err = of_parse_phandle_with_args(controller, 176ce15ed4cSHans de Goede "phys", "#phy-cells", 177ce15ed4cSHans de Goede index, &args); 178ce15ed4cSHans de Goede if (err) 179ce15ed4cSHans de Goede break; 180ce15ed4cSHans de Goede } 181ce15ed4cSHans de Goede 182ce15ed4cSHans de Goede of_node_put(args.np); 183ce15ed4cSHans de Goede if (args.np == np && (args.args_count == 0 || 184ce15ed4cSHans de Goede args.args[0] == arg0)) 18598bfb394SBin Liu goto finish; 18698bfb394SBin Liu index++; 187ce15ed4cSHans de Goede } while (args.np); 18898bfb394SBin Liu } while (controller); 18998bfb394SBin Liu 19098bfb394SBin Liu finish: 19198bfb394SBin Liu err = of_property_read_string(controller, "dr_mode", &dr_mode); 19298bfb394SBin Liu of_node_put(controller); 19398bfb394SBin Liu 19498bfb394SBin Liu if (err < 0) 19598bfb394SBin Liu return USB_DR_MODE_UNKNOWN; 19698bfb394SBin Liu 19798bfb394SBin Liu return usb_get_dr_mode_from_string(dr_mode); 19898bfb394SBin Liu } 19998bfb394SBin Liu EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy); 20098bfb394SBin Liu 20198bfb394SBin Liu /** 202aa923ef1SMichal Sojka * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported 203aa923ef1SMichal Sojka * for given targeted hosts (non-PC hosts) 204aa923ef1SMichal Sojka * @np: Pointer to the given device_node 205aa923ef1SMichal Sojka * 206aa923ef1SMichal Sojka * The function gets if the targeted hosts support TPL or not 207aa923ef1SMichal Sojka */ 208aa923ef1SMichal Sojka bool of_usb_host_tpl_support(struct device_node *np) 209aa923ef1SMichal Sojka { 210334007d5SSergei Shtylyov return of_property_read_bool(np, "tpl-support"); 211aa923ef1SMichal Sojka } 212aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(of_usb_host_tpl_support); 213929412d9SLi Jun 214929412d9SLi Jun /** 215929412d9SLi Jun * of_usb_update_otg_caps - to update usb otg capabilities according to 216929412d9SLi Jun * the passed properties in DT. 217929412d9SLi Jun * @np: Pointer to the given device_node 218929412d9SLi Jun * @otg_caps: Pointer to the target usb_otg_caps to be set 219929412d9SLi Jun * 220929412d9SLi Jun * The function updates the otg capabilities 221929412d9SLi Jun */ 222929412d9SLi Jun int of_usb_update_otg_caps(struct device_node *np, 223929412d9SLi Jun struct usb_otg_caps *otg_caps) 224929412d9SLi Jun { 225929412d9SLi Jun u32 otg_rev; 226929412d9SLi Jun 227929412d9SLi Jun if (!otg_caps) 228929412d9SLi Jun return -EINVAL; 229929412d9SLi Jun 230929412d9SLi Jun if (!of_property_read_u32(np, "otg-rev", &otg_rev)) { 231929412d9SLi Jun switch (otg_rev) { 232929412d9SLi Jun case 0x0100: 233929412d9SLi Jun case 0x0120: 234929412d9SLi Jun case 0x0130: 235929412d9SLi Jun case 0x0200: 236929412d9SLi Jun /* Choose the lesser one if it's already been set */ 237929412d9SLi Jun if (otg_caps->otg_rev) 238929412d9SLi Jun otg_caps->otg_rev = min_t(u16, otg_rev, 239929412d9SLi Jun otg_caps->otg_rev); 240929412d9SLi Jun else 241929412d9SLi Jun otg_caps->otg_rev = otg_rev; 242929412d9SLi Jun break; 243929412d9SLi Jun default: 244d9241ff2SRob Herring pr_err("%pOF: unsupported otg-rev: 0x%x\n", 245d9241ff2SRob Herring np, otg_rev); 246929412d9SLi Jun return -EINVAL; 247929412d9SLi Jun } 248929412d9SLi Jun } else { 249929412d9SLi Jun /* 250929412d9SLi Jun * otg-rev is mandatory for otg properties, if not passed 251929412d9SLi Jun * we set it to be 0 and assume it's a legacy otg device. 252929412d9SLi Jun * Non-dt platform can set it afterwards. 253929412d9SLi Jun */ 254929412d9SLi Jun otg_caps->otg_rev = 0; 255929412d9SLi Jun } 256929412d9SLi Jun 257334007d5SSergei Shtylyov if (of_property_read_bool(np, "hnp-disable")) 258929412d9SLi Jun otg_caps->hnp_support = false; 259334007d5SSergei Shtylyov if (of_property_read_bool(np, "srp-disable")) 260929412d9SLi Jun otg_caps->srp_support = false; 261334007d5SSergei Shtylyov if (of_property_read_bool(np, "adp-disable") || 262929412d9SLi Jun (otg_caps->otg_rev < 0x0200)) 263929412d9SLi Jun otg_caps->adp_support = false; 264929412d9SLi Jun 265929412d9SLi Jun return 0; 266929412d9SLi Jun } 267929412d9SLi Jun EXPORT_SYMBOL_GPL(of_usb_update_otg_caps); 268929412d9SLi Jun 269fa827966SYoshihiro Shimoda /** 270fa827966SYoshihiro Shimoda * usb_of_get_companion_dev - Find the companion device 271fa827966SYoshihiro Shimoda * @dev: the device pointer to find a companion 272fa827966SYoshihiro Shimoda * 273fa827966SYoshihiro Shimoda * Find the companion device from platform bus. 274fa827966SYoshihiro Shimoda * 275fa827966SYoshihiro Shimoda * Takes a reference to the returned struct device which needs to be dropped 276fa827966SYoshihiro Shimoda * after use. 277fa827966SYoshihiro Shimoda * 278fa827966SYoshihiro Shimoda * Return: On success, a pointer to the companion device, %NULL on failure. 279fa827966SYoshihiro Shimoda */ 280fa827966SYoshihiro Shimoda struct device *usb_of_get_companion_dev(struct device *dev) 281fa827966SYoshihiro Shimoda { 282fa827966SYoshihiro Shimoda struct device_node *node; 283fa827966SYoshihiro Shimoda struct platform_device *pdev = NULL; 284fa827966SYoshihiro Shimoda 285fa827966SYoshihiro Shimoda node = of_parse_phandle(dev->of_node, "companion", 0); 286fa827966SYoshihiro Shimoda if (node) 287fa827966SYoshihiro Shimoda pdev = of_find_device_by_node(node); 288fa827966SYoshihiro Shimoda 289fa827966SYoshihiro Shimoda of_node_put(node); 290fa827966SYoshihiro Shimoda 291fa827966SYoshihiro Shimoda return pdev ? &pdev->dev : NULL; 292fa827966SYoshihiro Shimoda } 293fa827966SYoshihiro Shimoda EXPORT_SYMBOL_GPL(usb_of_get_companion_dev); 294aa923ef1SMichal Sojka #endif 295aa923ef1SMichal Sojka 296*812086d3SGreg Kroah-Hartman struct dentry *usb_debug_root; 297*812086d3SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_debug_root); 298*812086d3SGreg Kroah-Hartman 299*812086d3SGreg Kroah-Hartman static int __init usb_common_init(void) 300*812086d3SGreg Kroah-Hartman { 301*812086d3SGreg Kroah-Hartman usb_debug_root = debugfs_create_dir("usb", NULL); 302*812086d3SGreg Kroah-Hartman ledtrig_usb_init(); 303*812086d3SGreg Kroah-Hartman return 0; 304*812086d3SGreg Kroah-Hartman } 305*812086d3SGreg Kroah-Hartman 306*812086d3SGreg Kroah-Hartman static void __exit usb_common_exit(void) 307*812086d3SGreg Kroah-Hartman { 308*812086d3SGreg Kroah-Hartman ledtrig_usb_exit(); 309*812086d3SGreg Kroah-Hartman debugfs_remove_recursive(usb_debug_root); 310*812086d3SGreg Kroah-Hartman } 311*812086d3SGreg Kroah-Hartman 312*812086d3SGreg Kroah-Hartman subsys_initcall(usb_common_init); 313*812086d3SGreg Kroah-Hartman module_exit(usb_common_exit); 314*812086d3SGreg Kroah-Hartman 315aa923ef1SMichal Sojka MODULE_LICENSE("GPL"); 316