1aa923ef1SMichal Sojka /* 2aa923ef1SMichal Sojka * Provides code common for host and device side USB. 3aa923ef1SMichal Sojka * 4aa923ef1SMichal Sojka * This program is free software; you can redistribute it and/or 5aa923ef1SMichal Sojka * modify it under the terms of the GNU General Public License as 6aa923ef1SMichal Sojka * published by the Free Software Foundation, version 2. 7aa923ef1SMichal Sojka * 8aa923ef1SMichal Sojka * If either host side (ie. CONFIG_USB=y) or device side USB stack 9aa923ef1SMichal Sojka * (ie. CONFIG_USB_GADGET=y) is compiled in the kernel, this module is 10aa923ef1SMichal Sojka * compiled-in as well. Otherwise, if either of the two stacks is 11aa923ef1SMichal Sojka * compiled as module, this file is compiled as module as well. 12aa923ef1SMichal Sojka */ 13aa923ef1SMichal Sojka 14aa923ef1SMichal Sojka #include <linux/kernel.h> 15aa923ef1SMichal Sojka #include <linux/module.h> 16aa923ef1SMichal Sojka #include <linux/of.h> 17aa923ef1SMichal Sojka #include <linux/usb/ch9.h> 18aa923ef1SMichal Sojka #include <linux/usb/of.h> 19aa923ef1SMichal Sojka #include <linux/usb/otg.h> 2098bfb394SBin Liu #include <linux/of_platform.h> 21aa923ef1SMichal Sojka 22aa923ef1SMichal Sojka const char *usb_otg_state_string(enum usb_otg_state state) 23aa923ef1SMichal Sojka { 24aa923ef1SMichal Sojka static const char *const names[] = { 25aa923ef1SMichal Sojka [OTG_STATE_A_IDLE] = "a_idle", 26aa923ef1SMichal Sojka [OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise", 27aa923ef1SMichal Sojka [OTG_STATE_A_WAIT_BCON] = "a_wait_bcon", 28aa923ef1SMichal Sojka [OTG_STATE_A_HOST] = "a_host", 29aa923ef1SMichal Sojka [OTG_STATE_A_SUSPEND] = "a_suspend", 30aa923ef1SMichal Sojka [OTG_STATE_A_PERIPHERAL] = "a_peripheral", 31aa923ef1SMichal Sojka [OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall", 32aa923ef1SMichal Sojka [OTG_STATE_A_VBUS_ERR] = "a_vbus_err", 33aa923ef1SMichal Sojka [OTG_STATE_B_IDLE] = "b_idle", 34aa923ef1SMichal Sojka [OTG_STATE_B_SRP_INIT] = "b_srp_init", 35aa923ef1SMichal Sojka [OTG_STATE_B_PERIPHERAL] = "b_peripheral", 36aa923ef1SMichal Sojka [OTG_STATE_B_WAIT_ACON] = "b_wait_acon", 37aa923ef1SMichal Sojka [OTG_STATE_B_HOST] = "b_host", 38aa923ef1SMichal Sojka }; 39aa923ef1SMichal Sojka 40aa923ef1SMichal Sojka if (state < 0 || state >= ARRAY_SIZE(names)) 41aa923ef1SMichal Sojka return "UNDEFINED"; 42aa923ef1SMichal Sojka 43aa923ef1SMichal Sojka return names[state]; 44aa923ef1SMichal Sojka } 45aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_otg_state_string); 46aa923ef1SMichal Sojka 47aa923ef1SMichal Sojka static const char *const speed_names[] = { 48aa923ef1SMichal Sojka [USB_SPEED_UNKNOWN] = "UNKNOWN", 49aa923ef1SMichal Sojka [USB_SPEED_LOW] = "low-speed", 50aa923ef1SMichal Sojka [USB_SPEED_FULL] = "full-speed", 51aa923ef1SMichal Sojka [USB_SPEED_HIGH] = "high-speed", 52aa923ef1SMichal Sojka [USB_SPEED_WIRELESS] = "wireless", 53aa923ef1SMichal Sojka [USB_SPEED_SUPER] = "super-speed", 548a1b2725SMathias Nyman [USB_SPEED_SUPER_PLUS] = "super-speed-plus", 55aa923ef1SMichal Sojka }; 56aa923ef1SMichal Sojka 57aa923ef1SMichal Sojka const char *usb_speed_string(enum usb_device_speed speed) 58aa923ef1SMichal Sojka { 59aa923ef1SMichal Sojka if (speed < 0 || speed >= ARRAY_SIZE(speed_names)) 60aa923ef1SMichal Sojka speed = USB_SPEED_UNKNOWN; 61aa923ef1SMichal Sojka return speed_names[speed]; 62aa923ef1SMichal Sojka } 63aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_speed_string); 64aa923ef1SMichal Sojka 6563863b98SHeikki Krogerus enum usb_device_speed usb_get_maximum_speed(struct device *dev) 6663863b98SHeikki Krogerus { 6763863b98SHeikki Krogerus const char *maximum_speed; 68efc2cd79SHeikki Krogerus int ret; 6963863b98SHeikki Krogerus 70efc2cd79SHeikki Krogerus ret = device_property_read_string(dev, "maximum-speed", &maximum_speed); 71efc2cd79SHeikki Krogerus if (ret < 0) 7263863b98SHeikki Krogerus return USB_SPEED_UNKNOWN; 7363863b98SHeikki Krogerus 74efc2cd79SHeikki Krogerus ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed); 7563863b98SHeikki Krogerus 76efc2cd79SHeikki Krogerus return (ret < 0) ? USB_SPEED_UNKNOWN : ret; 7763863b98SHeikki Krogerus } 7863863b98SHeikki Krogerus EXPORT_SYMBOL_GPL(usb_get_maximum_speed); 7963863b98SHeikki Krogerus 80aa923ef1SMichal Sojka const char *usb_state_string(enum usb_device_state state) 81aa923ef1SMichal Sojka { 82aa923ef1SMichal Sojka static const char *const names[] = { 83aa923ef1SMichal Sojka [USB_STATE_NOTATTACHED] = "not attached", 84aa923ef1SMichal Sojka [USB_STATE_ATTACHED] = "attached", 85aa923ef1SMichal Sojka [USB_STATE_POWERED] = "powered", 86aa923ef1SMichal Sojka [USB_STATE_RECONNECTING] = "reconnecting", 87aa923ef1SMichal Sojka [USB_STATE_UNAUTHENTICATED] = "unauthenticated", 88aa923ef1SMichal Sojka [USB_STATE_DEFAULT] = "default", 89aa923ef1SMichal Sojka [USB_STATE_ADDRESS] = "addressed", 90aa923ef1SMichal Sojka [USB_STATE_CONFIGURED] = "configured", 91aa923ef1SMichal Sojka [USB_STATE_SUSPENDED] = "suspended", 92aa923ef1SMichal Sojka }; 93aa923ef1SMichal Sojka 94aa923ef1SMichal Sojka if (state < 0 || state >= ARRAY_SIZE(names)) 95aa923ef1SMichal Sojka return "UNKNOWN"; 96aa923ef1SMichal Sojka 97aa923ef1SMichal Sojka return names[state]; 98aa923ef1SMichal Sojka } 99aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_state_string); 100aa923ef1SMichal Sojka 101aa923ef1SMichal Sojka static const char *const usb_dr_modes[] = { 102aa923ef1SMichal Sojka [USB_DR_MODE_UNKNOWN] = "", 103aa923ef1SMichal Sojka [USB_DR_MODE_HOST] = "host", 104aa923ef1SMichal Sojka [USB_DR_MODE_PERIPHERAL] = "peripheral", 105aa923ef1SMichal Sojka [USB_DR_MODE_OTG] = "otg", 106aa923ef1SMichal Sojka }; 107aa923ef1SMichal Sojka 10898bfb394SBin Liu static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str) 10998bfb394SBin Liu { 110efc2cd79SHeikki Krogerus int ret; 11198bfb394SBin Liu 112efc2cd79SHeikki Krogerus ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str); 113efc2cd79SHeikki Krogerus return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret; 11498bfb394SBin Liu } 11598bfb394SBin Liu 11606e7114fSHeikki Krogerus enum usb_dr_mode usb_get_dr_mode(struct device *dev) 117aa923ef1SMichal Sojka { 118aa923ef1SMichal Sojka const char *dr_mode; 11998bfb394SBin Liu int err; 120aa923ef1SMichal Sojka 12106e7114fSHeikki Krogerus err = device_property_read_string(dev, "dr_mode", &dr_mode); 122aa923ef1SMichal Sojka if (err < 0) 123aa923ef1SMichal Sojka return USB_DR_MODE_UNKNOWN; 124aa923ef1SMichal Sojka 12598bfb394SBin Liu return usb_get_dr_mode_from_string(dr_mode); 126aa923ef1SMichal Sojka } 12706e7114fSHeikki Krogerus EXPORT_SYMBOL_GPL(usb_get_dr_mode); 128aa923ef1SMichal Sojka 12906e7114fSHeikki Krogerus #ifdef CONFIG_OF 130aa923ef1SMichal Sojka /** 13198bfb394SBin Liu * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device 13298bfb394SBin Liu * which is associated with the given phy device_node 13398bfb394SBin Liu * @np: Pointer to the given phy device_node 134*ce15ed4cSHans de Goede * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for 135*ce15ed4cSHans de Goede * phys which do not have phy-cells 13698bfb394SBin Liu * 13798bfb394SBin Liu * In dts a usb controller associates with phy devices. The function gets 13898bfb394SBin Liu * the string from property 'dr_mode' of the controller associated with the 13998bfb394SBin Liu * given phy device node, and returns the correspondig enum usb_dr_mode. 14098bfb394SBin Liu */ 141*ce15ed4cSHans de Goede enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0) 14298bfb394SBin Liu { 14398bfb394SBin Liu struct device_node *controller = NULL; 144*ce15ed4cSHans de Goede struct of_phandle_args args; 14598bfb394SBin Liu const char *dr_mode; 14698bfb394SBin Liu int index; 14798bfb394SBin Liu int err; 14898bfb394SBin Liu 14998bfb394SBin Liu do { 15098bfb394SBin Liu controller = of_find_node_with_property(controller, "phys"); 15198bfb394SBin Liu index = 0; 15298bfb394SBin Liu do { 153*ce15ed4cSHans de Goede if (arg0 == -1) { 154*ce15ed4cSHans de Goede args.np = of_parse_phandle(controller, "phys", 155*ce15ed4cSHans de Goede index); 156*ce15ed4cSHans de Goede args.args_count = 0; 157*ce15ed4cSHans de Goede } else { 158*ce15ed4cSHans de Goede err = of_parse_phandle_with_args(controller, 159*ce15ed4cSHans de Goede "phys", "#phy-cells", 160*ce15ed4cSHans de Goede index, &args); 161*ce15ed4cSHans de Goede if (err) 162*ce15ed4cSHans de Goede break; 163*ce15ed4cSHans de Goede } 164*ce15ed4cSHans de Goede 165*ce15ed4cSHans de Goede of_node_put(args.np); 166*ce15ed4cSHans de Goede if (args.np == np && (args.args_count == 0 || 167*ce15ed4cSHans de Goede args.args[0] == arg0)) 16898bfb394SBin Liu goto finish; 16998bfb394SBin Liu index++; 170*ce15ed4cSHans de Goede } while (args.np); 17198bfb394SBin Liu } while (controller); 17298bfb394SBin Liu 17398bfb394SBin Liu finish: 17498bfb394SBin Liu err = of_property_read_string(controller, "dr_mode", &dr_mode); 17598bfb394SBin Liu of_node_put(controller); 17698bfb394SBin Liu 17798bfb394SBin Liu if (err < 0) 17898bfb394SBin Liu return USB_DR_MODE_UNKNOWN; 17998bfb394SBin Liu 18098bfb394SBin Liu return usb_get_dr_mode_from_string(dr_mode); 18198bfb394SBin Liu } 18298bfb394SBin Liu EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy); 18398bfb394SBin Liu 18498bfb394SBin Liu /** 185aa923ef1SMichal Sojka * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported 186aa923ef1SMichal Sojka * for given targeted hosts (non-PC hosts) 187aa923ef1SMichal Sojka * @np: Pointer to the given device_node 188aa923ef1SMichal Sojka * 189aa923ef1SMichal Sojka * The function gets if the targeted hosts support TPL or not 190aa923ef1SMichal Sojka */ 191aa923ef1SMichal Sojka bool of_usb_host_tpl_support(struct device_node *np) 192aa923ef1SMichal Sojka { 193aa923ef1SMichal Sojka if (of_find_property(np, "tpl-support", NULL)) 194aa923ef1SMichal Sojka return true; 195aa923ef1SMichal Sojka 196aa923ef1SMichal Sojka return false; 197aa923ef1SMichal Sojka } 198aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(of_usb_host_tpl_support); 199929412d9SLi Jun 200929412d9SLi Jun /** 201929412d9SLi Jun * of_usb_update_otg_caps - to update usb otg capabilities according to 202929412d9SLi Jun * the passed properties in DT. 203929412d9SLi Jun * @np: Pointer to the given device_node 204929412d9SLi Jun * @otg_caps: Pointer to the target usb_otg_caps to be set 205929412d9SLi Jun * 206929412d9SLi Jun * The function updates the otg capabilities 207929412d9SLi Jun */ 208929412d9SLi Jun int of_usb_update_otg_caps(struct device_node *np, 209929412d9SLi Jun struct usb_otg_caps *otg_caps) 210929412d9SLi Jun { 211929412d9SLi Jun u32 otg_rev; 212929412d9SLi Jun 213929412d9SLi Jun if (!otg_caps) 214929412d9SLi Jun return -EINVAL; 215929412d9SLi Jun 216929412d9SLi Jun if (!of_property_read_u32(np, "otg-rev", &otg_rev)) { 217929412d9SLi Jun switch (otg_rev) { 218929412d9SLi Jun case 0x0100: 219929412d9SLi Jun case 0x0120: 220929412d9SLi Jun case 0x0130: 221929412d9SLi Jun case 0x0200: 222929412d9SLi Jun /* Choose the lesser one if it's already been set */ 223929412d9SLi Jun if (otg_caps->otg_rev) 224929412d9SLi Jun otg_caps->otg_rev = min_t(u16, otg_rev, 225929412d9SLi Jun otg_caps->otg_rev); 226929412d9SLi Jun else 227929412d9SLi Jun otg_caps->otg_rev = otg_rev; 228929412d9SLi Jun break; 229929412d9SLi Jun default: 230929412d9SLi Jun pr_err("%s: unsupported otg-rev: 0x%x\n", 231929412d9SLi Jun np->full_name, otg_rev); 232929412d9SLi Jun return -EINVAL; 233929412d9SLi Jun } 234929412d9SLi Jun } else { 235929412d9SLi Jun /* 236929412d9SLi Jun * otg-rev is mandatory for otg properties, if not passed 237929412d9SLi Jun * we set it to be 0 and assume it's a legacy otg device. 238929412d9SLi Jun * Non-dt platform can set it afterwards. 239929412d9SLi Jun */ 240929412d9SLi Jun otg_caps->otg_rev = 0; 241929412d9SLi Jun } 242929412d9SLi Jun 243929412d9SLi Jun if (of_find_property(np, "hnp-disable", NULL)) 244929412d9SLi Jun otg_caps->hnp_support = false; 245929412d9SLi Jun if (of_find_property(np, "srp-disable", NULL)) 246929412d9SLi Jun otg_caps->srp_support = false; 247929412d9SLi Jun if (of_find_property(np, "adp-disable", NULL) || 248929412d9SLi Jun (otg_caps->otg_rev < 0x0200)) 249929412d9SLi Jun otg_caps->adp_support = false; 250929412d9SLi Jun 251929412d9SLi Jun return 0; 252929412d9SLi Jun } 253929412d9SLi Jun EXPORT_SYMBOL_GPL(of_usb_update_otg_caps); 254929412d9SLi Jun 255aa923ef1SMichal Sojka #endif 256aa923ef1SMichal Sojka 257aa923ef1SMichal Sojka MODULE_LICENSE("GPL"); 258