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> 18812086d3SGreg Kroah-Hartman #include <linux/debugfs.h> 19812086d3SGreg 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 72*52c2d157SThinh Nguyen static const char *const ssp_rate[] = { 73*52c2d157SThinh Nguyen [USB_SSP_GEN_UNKNOWN] = "UNKNOWN", 74*52c2d157SThinh Nguyen [USB_SSP_GEN_2x1] = "super-speed-plus-gen2x1", 75*52c2d157SThinh Nguyen [USB_SSP_GEN_1x2] = "super-speed-plus-gen1x2", 76*52c2d157SThinh Nguyen [USB_SSP_GEN_2x2] = "super-speed-plus-gen2x2", 77*52c2d157SThinh Nguyen }; 78*52c2d157SThinh Nguyen 79aa923ef1SMichal Sojka const char *usb_speed_string(enum usb_device_speed speed) 80aa923ef1SMichal Sojka { 81aa923ef1SMichal Sojka if (speed < 0 || speed >= ARRAY_SIZE(speed_names)) 82aa923ef1SMichal Sojka speed = USB_SPEED_UNKNOWN; 83aa923ef1SMichal Sojka return speed_names[speed]; 84aa923ef1SMichal Sojka } 85aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_speed_string); 86aa923ef1SMichal Sojka 8763863b98SHeikki Krogerus enum usb_device_speed usb_get_maximum_speed(struct device *dev) 8863863b98SHeikki Krogerus { 8963863b98SHeikki Krogerus const char *maximum_speed; 90efc2cd79SHeikki Krogerus int ret; 9163863b98SHeikki Krogerus 92efc2cd79SHeikki Krogerus ret = device_property_read_string(dev, "maximum-speed", &maximum_speed); 93efc2cd79SHeikki Krogerus if (ret < 0) 9463863b98SHeikki Krogerus return USB_SPEED_UNKNOWN; 9563863b98SHeikki Krogerus 96*52c2d157SThinh Nguyen ret = match_string(ssp_rate, ARRAY_SIZE(ssp_rate), maximum_speed); 97*52c2d157SThinh Nguyen if (ret > 0) 98*52c2d157SThinh Nguyen return USB_SPEED_SUPER_PLUS; 9963863b98SHeikki Krogerus 100*52c2d157SThinh Nguyen ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed); 101efc2cd79SHeikki Krogerus return (ret < 0) ? USB_SPEED_UNKNOWN : ret; 10263863b98SHeikki Krogerus } 10363863b98SHeikki Krogerus EXPORT_SYMBOL_GPL(usb_get_maximum_speed); 10463863b98SHeikki Krogerus 105*52c2d157SThinh Nguyen enum usb_ssp_rate usb_get_maximum_ssp_rate(struct device *dev) 106*52c2d157SThinh Nguyen { 107*52c2d157SThinh Nguyen const char *maximum_speed; 108*52c2d157SThinh Nguyen int ret; 109*52c2d157SThinh Nguyen 110*52c2d157SThinh Nguyen ret = device_property_read_string(dev, "maximum-speed", &maximum_speed); 111*52c2d157SThinh Nguyen if (ret < 0) 112*52c2d157SThinh Nguyen return USB_SSP_GEN_UNKNOWN; 113*52c2d157SThinh Nguyen 114*52c2d157SThinh Nguyen ret = match_string(ssp_rate, ARRAY_SIZE(ssp_rate), maximum_speed); 115*52c2d157SThinh Nguyen return (ret < 0) ? USB_SSP_GEN_UNKNOWN : ret; 116*52c2d157SThinh Nguyen } 117*52c2d157SThinh Nguyen EXPORT_SYMBOL_GPL(usb_get_maximum_ssp_rate); 118*52c2d157SThinh Nguyen 119aa923ef1SMichal Sojka const char *usb_state_string(enum usb_device_state state) 120aa923ef1SMichal Sojka { 121aa923ef1SMichal Sojka static const char *const names[] = { 122aa923ef1SMichal Sojka [USB_STATE_NOTATTACHED] = "not attached", 123aa923ef1SMichal Sojka [USB_STATE_ATTACHED] = "attached", 124aa923ef1SMichal Sojka [USB_STATE_POWERED] = "powered", 125aa923ef1SMichal Sojka [USB_STATE_RECONNECTING] = "reconnecting", 126aa923ef1SMichal Sojka [USB_STATE_UNAUTHENTICATED] = "unauthenticated", 127aa923ef1SMichal Sojka [USB_STATE_DEFAULT] = "default", 128aa923ef1SMichal Sojka [USB_STATE_ADDRESS] = "addressed", 129aa923ef1SMichal Sojka [USB_STATE_CONFIGURED] = "configured", 130aa923ef1SMichal Sojka [USB_STATE_SUSPENDED] = "suspended", 131aa923ef1SMichal Sojka }; 132aa923ef1SMichal Sojka 133aa923ef1SMichal Sojka if (state < 0 || state >= ARRAY_SIZE(names)) 134aa923ef1SMichal Sojka return "UNKNOWN"; 135aa923ef1SMichal Sojka 136aa923ef1SMichal Sojka return names[state]; 137aa923ef1SMichal Sojka } 138aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_state_string); 139aa923ef1SMichal Sojka 140aa923ef1SMichal Sojka static const char *const usb_dr_modes[] = { 141aa923ef1SMichal Sojka [USB_DR_MODE_UNKNOWN] = "", 142aa923ef1SMichal Sojka [USB_DR_MODE_HOST] = "host", 143aa923ef1SMichal Sojka [USB_DR_MODE_PERIPHERAL] = "peripheral", 144aa923ef1SMichal Sojka [USB_DR_MODE_OTG] = "otg", 145aa923ef1SMichal Sojka }; 146aa923ef1SMichal Sojka 14798bfb394SBin Liu static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str) 14898bfb394SBin Liu { 149efc2cd79SHeikki Krogerus int ret; 15098bfb394SBin Liu 151efc2cd79SHeikki Krogerus ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str); 152efc2cd79SHeikki Krogerus return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret; 15398bfb394SBin Liu } 15498bfb394SBin Liu 15506e7114fSHeikki Krogerus enum usb_dr_mode usb_get_dr_mode(struct device *dev) 156aa923ef1SMichal Sojka { 157aa923ef1SMichal Sojka const char *dr_mode; 15898bfb394SBin Liu int err; 159aa923ef1SMichal Sojka 16006e7114fSHeikki Krogerus err = device_property_read_string(dev, "dr_mode", &dr_mode); 161aa923ef1SMichal Sojka if (err < 0) 162aa923ef1SMichal Sojka return USB_DR_MODE_UNKNOWN; 163aa923ef1SMichal Sojka 16498bfb394SBin Liu return usb_get_dr_mode_from_string(dr_mode); 165aa923ef1SMichal Sojka } 16606e7114fSHeikki Krogerus EXPORT_SYMBOL_GPL(usb_get_dr_mode); 167aa923ef1SMichal Sojka 16806e7114fSHeikki Krogerus #ifdef CONFIG_OF 169aa923ef1SMichal Sojka /** 17098bfb394SBin Liu * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device 17198bfb394SBin Liu * which is associated with the given phy device_node 17298bfb394SBin Liu * @np: Pointer to the given phy device_node 173ce15ed4cSHans de Goede * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for 174ce15ed4cSHans de Goede * phys which do not have phy-cells 17598bfb394SBin Liu * 17698bfb394SBin Liu * In dts a usb controller associates with phy devices. The function gets 17798bfb394SBin Liu * the string from property 'dr_mode' of the controller associated with the 17898bfb394SBin Liu * given phy device node, and returns the correspondig enum usb_dr_mode. 17998bfb394SBin Liu */ 180ce15ed4cSHans de Goede enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0) 18198bfb394SBin Liu { 18298bfb394SBin Liu struct device_node *controller = NULL; 183ce15ed4cSHans de Goede struct of_phandle_args args; 18498bfb394SBin Liu const char *dr_mode; 18598bfb394SBin Liu int index; 18698bfb394SBin Liu int err; 18798bfb394SBin Liu 18898bfb394SBin Liu do { 18998bfb394SBin Liu controller = of_find_node_with_property(controller, "phys"); 190238e0268SFabrizio Castro if (!of_device_is_available(controller)) 191238e0268SFabrizio Castro continue; 19298bfb394SBin Liu index = 0; 19398bfb394SBin Liu do { 194ce15ed4cSHans de Goede if (arg0 == -1) { 195ce15ed4cSHans de Goede args.np = of_parse_phandle(controller, "phys", 196ce15ed4cSHans de Goede index); 197ce15ed4cSHans de Goede args.args_count = 0; 198ce15ed4cSHans de Goede } else { 199ce15ed4cSHans de Goede err = of_parse_phandle_with_args(controller, 200ce15ed4cSHans de Goede "phys", "#phy-cells", 201ce15ed4cSHans de Goede index, &args); 202ce15ed4cSHans de Goede if (err) 203ce15ed4cSHans de Goede break; 204ce15ed4cSHans de Goede } 205ce15ed4cSHans de Goede 206ce15ed4cSHans de Goede of_node_put(args.np); 207ce15ed4cSHans de Goede if (args.np == np && (args.args_count == 0 || 208ce15ed4cSHans de Goede args.args[0] == arg0)) 20998bfb394SBin Liu goto finish; 21098bfb394SBin Liu index++; 211ce15ed4cSHans de Goede } while (args.np); 21298bfb394SBin Liu } while (controller); 21398bfb394SBin Liu 21498bfb394SBin Liu finish: 21598bfb394SBin Liu err = of_property_read_string(controller, "dr_mode", &dr_mode); 21698bfb394SBin Liu of_node_put(controller); 21798bfb394SBin Liu 21898bfb394SBin Liu if (err < 0) 21998bfb394SBin Liu return USB_DR_MODE_UNKNOWN; 22098bfb394SBin Liu 22198bfb394SBin Liu return usb_get_dr_mode_from_string(dr_mode); 22298bfb394SBin Liu } 22398bfb394SBin Liu EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy); 22498bfb394SBin Liu 22598bfb394SBin Liu /** 226aa923ef1SMichal Sojka * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported 227aa923ef1SMichal Sojka * for given targeted hosts (non-PC hosts) 228aa923ef1SMichal Sojka * @np: Pointer to the given device_node 229aa923ef1SMichal Sojka * 230aa923ef1SMichal Sojka * The function gets if the targeted hosts support TPL or not 231aa923ef1SMichal Sojka */ 232aa923ef1SMichal Sojka bool of_usb_host_tpl_support(struct device_node *np) 233aa923ef1SMichal Sojka { 234334007d5SSergei Shtylyov return of_property_read_bool(np, "tpl-support"); 235aa923ef1SMichal Sojka } 236aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(of_usb_host_tpl_support); 237929412d9SLi Jun 238929412d9SLi Jun /** 239929412d9SLi Jun * of_usb_update_otg_caps - to update usb otg capabilities according to 240929412d9SLi Jun * the passed properties in DT. 241929412d9SLi Jun * @np: Pointer to the given device_node 242929412d9SLi Jun * @otg_caps: Pointer to the target usb_otg_caps to be set 243929412d9SLi Jun * 244929412d9SLi Jun * The function updates the otg capabilities 245929412d9SLi Jun */ 246929412d9SLi Jun int of_usb_update_otg_caps(struct device_node *np, 247929412d9SLi Jun struct usb_otg_caps *otg_caps) 248929412d9SLi Jun { 249929412d9SLi Jun u32 otg_rev; 250929412d9SLi Jun 251929412d9SLi Jun if (!otg_caps) 252929412d9SLi Jun return -EINVAL; 253929412d9SLi Jun 254929412d9SLi Jun if (!of_property_read_u32(np, "otg-rev", &otg_rev)) { 255929412d9SLi Jun switch (otg_rev) { 256929412d9SLi Jun case 0x0100: 257929412d9SLi Jun case 0x0120: 258929412d9SLi Jun case 0x0130: 259929412d9SLi Jun case 0x0200: 260929412d9SLi Jun /* Choose the lesser one if it's already been set */ 261929412d9SLi Jun if (otg_caps->otg_rev) 262929412d9SLi Jun otg_caps->otg_rev = min_t(u16, otg_rev, 263929412d9SLi Jun otg_caps->otg_rev); 264929412d9SLi Jun else 265929412d9SLi Jun otg_caps->otg_rev = otg_rev; 266929412d9SLi Jun break; 267929412d9SLi Jun default: 268d9241ff2SRob Herring pr_err("%pOF: unsupported otg-rev: 0x%x\n", 269d9241ff2SRob Herring np, otg_rev); 270929412d9SLi Jun return -EINVAL; 271929412d9SLi Jun } 272929412d9SLi Jun } else { 273929412d9SLi Jun /* 274929412d9SLi Jun * otg-rev is mandatory for otg properties, if not passed 275929412d9SLi Jun * we set it to be 0 and assume it's a legacy otg device. 276929412d9SLi Jun * Non-dt platform can set it afterwards. 277929412d9SLi Jun */ 278929412d9SLi Jun otg_caps->otg_rev = 0; 279929412d9SLi Jun } 280929412d9SLi Jun 281334007d5SSergei Shtylyov if (of_property_read_bool(np, "hnp-disable")) 282929412d9SLi Jun otg_caps->hnp_support = false; 283334007d5SSergei Shtylyov if (of_property_read_bool(np, "srp-disable")) 284929412d9SLi Jun otg_caps->srp_support = false; 285334007d5SSergei Shtylyov if (of_property_read_bool(np, "adp-disable") || 286929412d9SLi Jun (otg_caps->otg_rev < 0x0200)) 287929412d9SLi Jun otg_caps->adp_support = false; 288929412d9SLi Jun 289929412d9SLi Jun return 0; 290929412d9SLi Jun } 291929412d9SLi Jun EXPORT_SYMBOL_GPL(of_usb_update_otg_caps); 292929412d9SLi Jun 293fa827966SYoshihiro Shimoda /** 294fa827966SYoshihiro Shimoda * usb_of_get_companion_dev - Find the companion device 295fa827966SYoshihiro Shimoda * @dev: the device pointer to find a companion 296fa827966SYoshihiro Shimoda * 297fa827966SYoshihiro Shimoda * Find the companion device from platform bus. 298fa827966SYoshihiro Shimoda * 299fa827966SYoshihiro Shimoda * Takes a reference to the returned struct device which needs to be dropped 300fa827966SYoshihiro Shimoda * after use. 301fa827966SYoshihiro Shimoda * 302fa827966SYoshihiro Shimoda * Return: On success, a pointer to the companion device, %NULL on failure. 303fa827966SYoshihiro Shimoda */ 304fa827966SYoshihiro Shimoda struct device *usb_of_get_companion_dev(struct device *dev) 305fa827966SYoshihiro Shimoda { 306fa827966SYoshihiro Shimoda struct device_node *node; 307fa827966SYoshihiro Shimoda struct platform_device *pdev = NULL; 308fa827966SYoshihiro Shimoda 309fa827966SYoshihiro Shimoda node = of_parse_phandle(dev->of_node, "companion", 0); 310fa827966SYoshihiro Shimoda if (node) 311fa827966SYoshihiro Shimoda pdev = of_find_device_by_node(node); 312fa827966SYoshihiro Shimoda 313fa827966SYoshihiro Shimoda of_node_put(node); 314fa827966SYoshihiro Shimoda 315fa827966SYoshihiro Shimoda return pdev ? &pdev->dev : NULL; 316fa827966SYoshihiro Shimoda } 317fa827966SYoshihiro Shimoda EXPORT_SYMBOL_GPL(usb_of_get_companion_dev); 318aa923ef1SMichal Sojka #endif 319aa923ef1SMichal Sojka 320812086d3SGreg Kroah-Hartman struct dentry *usb_debug_root; 321812086d3SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_debug_root); 322812086d3SGreg Kroah-Hartman 323812086d3SGreg Kroah-Hartman static int __init usb_common_init(void) 324812086d3SGreg Kroah-Hartman { 325812086d3SGreg Kroah-Hartman usb_debug_root = debugfs_create_dir("usb", NULL); 326812086d3SGreg Kroah-Hartman ledtrig_usb_init(); 327812086d3SGreg Kroah-Hartman return 0; 328812086d3SGreg Kroah-Hartman } 329812086d3SGreg Kroah-Hartman 330812086d3SGreg Kroah-Hartman static void __exit usb_common_exit(void) 331812086d3SGreg Kroah-Hartman { 332812086d3SGreg Kroah-Hartman ledtrig_usb_exit(); 333812086d3SGreg Kroah-Hartman debugfs_remove_recursive(usb_debug_root); 334812086d3SGreg Kroah-Hartman } 335812086d3SGreg Kroah-Hartman 336812086d3SGreg Kroah-Hartman subsys_initcall(usb_common_init); 337812086d3SGreg Kroah-Hartman module_exit(usb_common_exit); 338812086d3SGreg Kroah-Hartman 339aa923ef1SMichal Sojka MODULE_LICENSE("GPL"); 340