2 * Provides code common for host and device side USB.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, version 2.
8 * If either host side (ie. CONFIG_USB=y) or device side USB stack
9 * (ie. CONFIG_USB_GADGET=y) is compiled in the kernel, this module is
10 * compiled-in as well. Otherwise, if either of the two stacks is
11 * compiled as module, this file is compiled as module as well.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
17 #include <linux/usb/ch9.h>
18 #include <linux/usb/of.h>
19 #include <linux/usb/otg.h>
20 #include <linux/of_platform.h>
22 const char *usb_otg_state_string(enum usb_otg_state state
)
24 static const char *const names
[] = {
25 [OTG_STATE_A_IDLE
] = "a_idle",
26 [OTG_STATE_A_WAIT_VRISE
] = "a_wait_vrise",
27 [OTG_STATE_A_WAIT_BCON
] = "a_wait_bcon",
28 [OTG_STATE_A_HOST
] = "a_host",
29 [OTG_STATE_A_SUSPEND
] = "a_suspend",
30 [OTG_STATE_A_PERIPHERAL
] = "a_peripheral",
31 [OTG_STATE_A_WAIT_VFALL
] = "a_wait_vfall",
32 [OTG_STATE_A_VBUS_ERR
] = "a_vbus_err",
33 [OTG_STATE_B_IDLE
] = "b_idle",
34 [OTG_STATE_B_SRP_INIT
] = "b_srp_init",
35 [OTG_STATE_B_PERIPHERAL
] = "b_peripheral",
36 [OTG_STATE_B_WAIT_ACON
] = "b_wait_acon",
37 [OTG_STATE_B_HOST
] = "b_host",
40 if (state
< 0 || state
>= ARRAY_SIZE(names
))
45 EXPORT_SYMBOL_GPL(usb_otg_state_string
);
47 static const char *const speed_names
[] = {
48 [USB_SPEED_UNKNOWN
] = "UNKNOWN",
49 [USB_SPEED_LOW
] = "low-speed",
50 [USB_SPEED_FULL
] = "full-speed",
51 [USB_SPEED_HIGH
] = "high-speed",
52 [USB_SPEED_WIRELESS
] = "wireless",
53 [USB_SPEED_SUPER
] = "super-speed",
54 [USB_SPEED_SUPER_PLUS
] = "super-speed-plus",
57 const char *usb_speed_string(enum usb_device_speed speed
)
59 if (speed
< 0 || speed
>= ARRAY_SIZE(speed_names
))
60 speed
= USB_SPEED_UNKNOWN
;
61 return speed_names
[speed
];
63 EXPORT_SYMBOL_GPL(usb_speed_string
);
65 enum usb_device_speed
usb_get_maximum_speed(struct device
*dev
)
67 const char *maximum_speed
;
70 ret
= device_property_read_string(dev
, "maximum-speed", &maximum_speed
);
72 return USB_SPEED_UNKNOWN
;
74 ret
= match_string(speed_names
, ARRAY_SIZE(speed_names
), maximum_speed
);
76 return (ret
< 0) ? USB_SPEED_UNKNOWN
: ret
;
78 EXPORT_SYMBOL_GPL(usb_get_maximum_speed
);
80 const char *usb_state_string(enum usb_device_state state
)
82 static const char *const names
[] = {
83 [USB_STATE_NOTATTACHED
] = "not attached",
84 [USB_STATE_ATTACHED
] = "attached",
85 [USB_STATE_POWERED
] = "powered",
86 [USB_STATE_RECONNECTING
] = "reconnecting",
87 [USB_STATE_UNAUTHENTICATED
] = "unauthenticated",
88 [USB_STATE_DEFAULT
] = "default",
89 [USB_STATE_ADDRESS
] = "addressed",
90 [USB_STATE_CONFIGURED
] = "configured",
91 [USB_STATE_SUSPENDED
] = "suspended",
94 if (state
< 0 || state
>= ARRAY_SIZE(names
))
99 EXPORT_SYMBOL_GPL(usb_state_string
);
101 static const char *const usb_dr_modes
[] = {
102 [USB_DR_MODE_UNKNOWN
] = "",
103 [USB_DR_MODE_HOST
] = "host",
104 [USB_DR_MODE_PERIPHERAL
] = "peripheral",
105 [USB_DR_MODE_OTG
] = "otg",
108 static enum usb_dr_mode
usb_get_dr_mode_from_string(const char *str
)
112 ret
= match_string(usb_dr_modes
, ARRAY_SIZE(usb_dr_modes
), str
);
113 return (ret
< 0) ? USB_DR_MODE_UNKNOWN
: ret
;
116 enum usb_dr_mode
usb_get_dr_mode(struct device
*dev
)
121 err
= device_property_read_string(dev
, "dr_mode", &dr_mode
);
123 return USB_DR_MODE_UNKNOWN
;
125 return usb_get_dr_mode_from_string(dr_mode
);
127 EXPORT_SYMBOL_GPL(usb_get_dr_mode
);
131 * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
132 * which is associated with the given phy device_node
133 * @np: Pointer to the given phy device_node
135 * In dts a usb controller associates with phy devices. The function gets
136 * the string from property 'dr_mode' of the controller associated with the
137 * given phy device node, and returns the correspondig enum usb_dr_mode.
139 enum usb_dr_mode
of_usb_get_dr_mode_by_phy(struct device_node
*phy_np
)
141 struct device_node
*controller
= NULL
;
142 struct device_node
*phy
;
148 controller
= of_find_node_with_property(controller
, "phys");
151 phy
= of_parse_phandle(controller
, "phys", index
);
157 } while (controller
);
160 err
= of_property_read_string(controller
, "dr_mode", &dr_mode
);
161 of_node_put(controller
);
164 return USB_DR_MODE_UNKNOWN
;
166 return usb_get_dr_mode_from_string(dr_mode
);
168 EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy
);
171 * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
172 * for given targeted hosts (non-PC hosts)
173 * @np: Pointer to the given device_node
175 * The function gets if the targeted hosts support TPL or not
177 bool of_usb_host_tpl_support(struct device_node
*np
)
179 if (of_find_property(np
, "tpl-support", NULL
))
184 EXPORT_SYMBOL_GPL(of_usb_host_tpl_support
);
187 * of_usb_update_otg_caps - to update usb otg capabilities according to
188 * the passed properties in DT.
189 * @np: Pointer to the given device_node
190 * @otg_caps: Pointer to the target usb_otg_caps to be set
192 * The function updates the otg capabilities
194 int of_usb_update_otg_caps(struct device_node
*np
,
195 struct usb_otg_caps
*otg_caps
)
202 if (!of_property_read_u32(np
, "otg-rev", &otg_rev
)) {
208 /* Choose the lesser one if it's already been set */
209 if (otg_caps
->otg_rev
)
210 otg_caps
->otg_rev
= min_t(u16
, otg_rev
,
213 otg_caps
->otg_rev
= otg_rev
;
216 pr_err("%s: unsupported otg-rev: 0x%x\n",
217 np
->full_name
, otg_rev
);
222 * otg-rev is mandatory for otg properties, if not passed
223 * we set it to be 0 and assume it's a legacy otg device.
224 * Non-dt platform can set it afterwards.
226 otg_caps
->otg_rev
= 0;
229 if (of_find_property(np
, "hnp-disable", NULL
))
230 otg_caps
->hnp_support
= false;
231 if (of_find_property(np
, "srp-disable", NULL
))
232 otg_caps
->srp_support
= false;
233 if (of_find_property(np
, "adp-disable", NULL
) ||
234 (otg_caps
->otg_rev
< 0x0200))
235 otg_caps
->adp_support
= false;
239 EXPORT_SYMBOL_GPL(of_usb_update_otg_caps
);
243 MODULE_LICENSE("GPL");