Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Provides code common for host and device side USB.
  4 *
 
 
 
 
  5 * If either host side (ie. CONFIG_USB=y) or device side USB stack
  6 * (ie. CONFIG_USB_GADGET=y) is compiled in the kernel, this module is
  7 * compiled-in as well.  Otherwise, if either of the two stacks is
  8 * compiled as module, this file is compiled as module as well.
  9 */
 10
 11#include <linux/kernel.h>
 12#include <linux/module.h>
 13#include <linux/of.h>
 14#include <linux/usb/ch9.h>
 15#include <linux/usb/of.h>
 16#include <linux/usb/otg.h>
 17#include <linux/of_platform.h>
 18#include <linux/debugfs.h>
 19#include "common.h"
 20
 21static const char *const ep_type_names[] = {
 22	[USB_ENDPOINT_XFER_CONTROL] = "ctrl",
 23	[USB_ENDPOINT_XFER_ISOC] = "isoc",
 24	[USB_ENDPOINT_XFER_BULK] = "bulk",
 25	[USB_ENDPOINT_XFER_INT] = "intr",
 26};
 27
 28const char *usb_ep_type_string(int ep_type)
 29{
 30	if (ep_type < 0 || ep_type >= ARRAY_SIZE(ep_type_names))
 31		return "unknown";
 32
 33	return ep_type_names[ep_type];
 34}
 35EXPORT_SYMBOL_GPL(usb_ep_type_string);
 36
 37const char *usb_otg_state_string(enum usb_otg_state state)
 38{
 39	static const char *const names[] = {
 40		[OTG_STATE_A_IDLE] = "a_idle",
 41		[OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise",
 42		[OTG_STATE_A_WAIT_BCON] = "a_wait_bcon",
 43		[OTG_STATE_A_HOST] = "a_host",
 44		[OTG_STATE_A_SUSPEND] = "a_suspend",
 45		[OTG_STATE_A_PERIPHERAL] = "a_peripheral",
 46		[OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall",
 47		[OTG_STATE_A_VBUS_ERR] = "a_vbus_err",
 48		[OTG_STATE_B_IDLE] = "b_idle",
 49		[OTG_STATE_B_SRP_INIT] = "b_srp_init",
 50		[OTG_STATE_B_PERIPHERAL] = "b_peripheral",
 51		[OTG_STATE_B_WAIT_ACON] = "b_wait_acon",
 52		[OTG_STATE_B_HOST] = "b_host",
 53	};
 54
 55	if (state < 0 || state >= ARRAY_SIZE(names))
 56		return "UNDEFINED";
 57
 58	return names[state];
 59}
 60EXPORT_SYMBOL_GPL(usb_otg_state_string);
 61
 62static const char *const speed_names[] = {
 63	[USB_SPEED_UNKNOWN] = "UNKNOWN",
 64	[USB_SPEED_LOW] = "low-speed",
 65	[USB_SPEED_FULL] = "full-speed",
 66	[USB_SPEED_HIGH] = "high-speed",
 67	[USB_SPEED_WIRELESS] = "wireless",
 68	[USB_SPEED_SUPER] = "super-speed",
 69	[USB_SPEED_SUPER_PLUS] = "super-speed-plus",
 70};
 71
 72const char *usb_speed_string(enum usb_device_speed speed)
 73{
 74	if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
 75		speed = USB_SPEED_UNKNOWN;
 76	return speed_names[speed];
 77}
 78EXPORT_SYMBOL_GPL(usb_speed_string);
 79
 80enum usb_device_speed usb_get_maximum_speed(struct device *dev)
 81{
 82	const char *maximum_speed;
 83	int ret;
 84
 85	ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
 86	if (ret < 0)
 87		return USB_SPEED_UNKNOWN;
 88
 89	ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
 90
 91	return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
 92}
 93EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
 94
 95const char *usb_state_string(enum usb_device_state state)
 96{
 97	static const char *const names[] = {
 98		[USB_STATE_NOTATTACHED] = "not attached",
 99		[USB_STATE_ATTACHED] = "attached",
100		[USB_STATE_POWERED] = "powered",
101		[USB_STATE_RECONNECTING] = "reconnecting",
102		[USB_STATE_UNAUTHENTICATED] = "unauthenticated",
103		[USB_STATE_DEFAULT] = "default",
104		[USB_STATE_ADDRESS] = "addressed",
105		[USB_STATE_CONFIGURED] = "configured",
106		[USB_STATE_SUSPENDED] = "suspended",
107	};
108
109	if (state < 0 || state >= ARRAY_SIZE(names))
110		return "UNKNOWN";
111
112	return names[state];
113}
114EXPORT_SYMBOL_GPL(usb_state_string);
115
116static const char *const usb_dr_modes[] = {
117	[USB_DR_MODE_UNKNOWN]		= "",
118	[USB_DR_MODE_HOST]		= "host",
119	[USB_DR_MODE_PERIPHERAL]	= "peripheral",
120	[USB_DR_MODE_OTG]		= "otg",
121};
122
123static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
124{
125	int ret;
126
127	ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
128	return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
129}
130
131enum usb_dr_mode usb_get_dr_mode(struct device *dev)
132{
133	const char *dr_mode;
134	int err;
135
136	err = device_property_read_string(dev, "dr_mode", &dr_mode);
137	if (err < 0)
138		return USB_DR_MODE_UNKNOWN;
139
140	return usb_get_dr_mode_from_string(dr_mode);
141}
142EXPORT_SYMBOL_GPL(usb_get_dr_mode);
143
144#ifdef CONFIG_OF
145/**
146 * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
147 * which is associated with the given phy device_node
148 * @np:	Pointer to the given phy device_node
149 * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for
150 *        phys which do not have phy-cells
151 *
152 * In dts a usb controller associates with phy devices.  The function gets
153 * the string from property 'dr_mode' of the controller associated with the
154 * given phy device node, and returns the correspondig enum usb_dr_mode.
155 */
156enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0)
157{
158	struct device_node *controller = NULL;
159	struct of_phandle_args args;
160	const char *dr_mode;
161	int index;
162	int err;
163
164	do {
165		controller = of_find_node_with_property(controller, "phys");
166		if (!of_device_is_available(controller))
167			continue;
168		index = 0;
169		do {
170			if (arg0 == -1) {
171				args.np = of_parse_phandle(controller, "phys",
172							index);
173				args.args_count = 0;
174			} else {
175				err = of_parse_phandle_with_args(controller,
176							"phys", "#phy-cells",
177							index, &args);
178				if (err)
179					break;
180			}
181
182			of_node_put(args.np);
183			if (args.np == np && (args.args_count == 0 ||
184					      args.args[0] == arg0))
185				goto finish;
186			index++;
187		} while (args.np);
188	} while (controller);
189
190finish:
191	err = of_property_read_string(controller, "dr_mode", &dr_mode);
192	of_node_put(controller);
193
194	if (err < 0)
195		return USB_DR_MODE_UNKNOWN;
196
197	return usb_get_dr_mode_from_string(dr_mode);
198}
199EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
200
201/**
202 * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
203 * for given targeted hosts (non-PC hosts)
204 * @np: Pointer to the given device_node
205 *
206 * The function gets if the targeted hosts support TPL or not
207 */
208bool of_usb_host_tpl_support(struct device_node *np)
209{
210	return of_property_read_bool(np, "tpl-support");
 
 
 
211}
212EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
213
214/**
215 * of_usb_update_otg_caps - to update usb otg capabilities according to
216 * the passed properties in DT.
217 * @np: Pointer to the given device_node
218 * @otg_caps: Pointer to the target usb_otg_caps to be set
219 *
220 * The function updates the otg capabilities
221 */
222int of_usb_update_otg_caps(struct device_node *np,
223			struct usb_otg_caps *otg_caps)
224{
225	u32 otg_rev;
226
227	if (!otg_caps)
228		return -EINVAL;
229
230	if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
231		switch (otg_rev) {
232		case 0x0100:
233		case 0x0120:
234		case 0x0130:
235		case 0x0200:
236			/* Choose the lesser one if it's already been set */
237			if (otg_caps->otg_rev)
238				otg_caps->otg_rev = min_t(u16, otg_rev,
239							otg_caps->otg_rev);
240			else
241				otg_caps->otg_rev = otg_rev;
242			break;
243		default:
244			pr_err("%pOF: unsupported otg-rev: 0x%x\n",
245						np, otg_rev);
246			return -EINVAL;
247		}
248	} else {
249		/*
250		 * otg-rev is mandatory for otg properties, if not passed
251		 * we set it to be 0 and assume it's a legacy otg device.
252		 * Non-dt platform can set it afterwards.
253		 */
254		otg_caps->otg_rev = 0;
255	}
256
257	if (of_property_read_bool(np, "hnp-disable"))
258		otg_caps->hnp_support = false;
259	if (of_property_read_bool(np, "srp-disable"))
260		otg_caps->srp_support = false;
261	if (of_property_read_bool(np, "adp-disable") ||
262				(otg_caps->otg_rev < 0x0200))
263		otg_caps->adp_support = false;
264
265	return 0;
266}
267EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
268
269/**
270 * usb_of_get_companion_dev - Find the companion device
271 * @dev: the device pointer to find a companion
272 *
273 * Find the companion device from platform bus.
274 *
275 * Takes a reference to the returned struct device which needs to be dropped
276 * after use.
277 *
278 * Return: On success, a pointer to the companion device, %NULL on failure.
279 */
280struct device *usb_of_get_companion_dev(struct device *dev)
281{
282	struct device_node *node;
283	struct platform_device *pdev = NULL;
284
285	node = of_parse_phandle(dev->of_node, "companion", 0);
286	if (node)
287		pdev = of_find_device_by_node(node);
288
289	of_node_put(node);
290
291	return pdev ? &pdev->dev : NULL;
292}
293EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
294#endif
295
296struct dentry *usb_debug_root;
297EXPORT_SYMBOL_GPL(usb_debug_root);
298
299static int __init usb_common_init(void)
300{
301	usb_debug_root = debugfs_create_dir("usb", NULL);
302	ledtrig_usb_init();
303	return 0;
304}
305
306static void __exit usb_common_exit(void)
307{
308	ledtrig_usb_exit();
309	debugfs_remove_recursive(usb_debug_root);
310}
311
312subsys_initcall(usb_common_init);
313module_exit(usb_common_exit);
314
315MODULE_LICENSE("GPL");
v4.6
 
  1/*
  2 * Provides code common for host and device side USB.
  3 *
  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.
  7 *
  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.
 12 */
 13
 14#include <linux/kernel.h>
 15#include <linux/module.h>
 16#include <linux/of.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>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 21
 22const char *usb_otg_state_string(enum usb_otg_state state)
 23{
 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",
 38	};
 39
 40	if (state < 0 || state >= ARRAY_SIZE(names))
 41		return "UNDEFINED";
 42
 43	return names[state];
 44}
 45EXPORT_SYMBOL_GPL(usb_otg_state_string);
 46
 47static 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",
 55};
 56
 57const char *usb_speed_string(enum usb_device_speed speed)
 58{
 59	if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
 60		speed = USB_SPEED_UNKNOWN;
 61	return speed_names[speed];
 62}
 63EXPORT_SYMBOL_GPL(usb_speed_string);
 64
 65enum usb_device_speed usb_get_maximum_speed(struct device *dev)
 66{
 67	const char *maximum_speed;
 68	int ret;
 69
 70	ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
 71	if (ret < 0)
 72		return USB_SPEED_UNKNOWN;
 73
 74	ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
 75
 76	return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
 77}
 78EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
 79
 80const char *usb_state_string(enum usb_device_state state)
 81{
 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",
 92	};
 93
 94	if (state < 0 || state >= ARRAY_SIZE(names))
 95		return "UNKNOWN";
 96
 97	return names[state];
 98}
 99EXPORT_SYMBOL_GPL(usb_state_string);
100
101static 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",
106};
107
108static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
109{
110	int ret;
111
112	ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
113	return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
114}
115
116enum usb_dr_mode usb_get_dr_mode(struct device *dev)
117{
118	const char *dr_mode;
119	int err;
120
121	err = device_property_read_string(dev, "dr_mode", &dr_mode);
122	if (err < 0)
123		return USB_DR_MODE_UNKNOWN;
124
125	return usb_get_dr_mode_from_string(dr_mode);
126}
127EXPORT_SYMBOL_GPL(usb_get_dr_mode);
128
129#ifdef CONFIG_OF
130/**
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
 
 
134 *
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.
138 */
139enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *phy_np)
140{
141	struct device_node *controller = NULL;
142	struct device_node *phy;
143	const char *dr_mode;
144	int index;
145	int err;
146
147	do {
148		controller = of_find_node_with_property(controller, "phys");
 
 
149		index = 0;
150		do {
151			phy = of_parse_phandle(controller, "phys", index);
152			of_node_put(phy);
153			if (phy == phy_np)
 
 
 
 
 
 
 
 
 
 
 
 
154				goto finish;
155			index++;
156		} while (phy);
157	} while (controller);
158
159finish:
160	err = of_property_read_string(controller, "dr_mode", &dr_mode);
161	of_node_put(controller);
162
163	if (err < 0)
164		return USB_DR_MODE_UNKNOWN;
165
166	return usb_get_dr_mode_from_string(dr_mode);
167}
168EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
169
170/**
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
174 *
175 * The function gets if the targeted hosts support TPL or not
176 */
177bool of_usb_host_tpl_support(struct device_node *np)
178{
179	if (of_find_property(np, "tpl-support", NULL))
180		return true;
181
182	return false;
183}
184EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
185
186/**
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
191 *
192 * The function updates the otg capabilities
193 */
194int of_usb_update_otg_caps(struct device_node *np,
195			struct usb_otg_caps *otg_caps)
196{
197	u32 otg_rev;
198
199	if (!otg_caps)
200		return -EINVAL;
201
202	if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
203		switch (otg_rev) {
204		case 0x0100:
205		case 0x0120:
206		case 0x0130:
207		case 0x0200:
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,
211							otg_caps->otg_rev);
212			else
213				otg_caps->otg_rev = otg_rev;
214			break;
215		default:
216			pr_err("%s: unsupported otg-rev: 0x%x\n",
217						np->full_name, otg_rev);
218			return -EINVAL;
219		}
220	} else {
221		/*
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.
225		 */
226		otg_caps->otg_rev = 0;
227	}
228
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;
236
237	return 0;
238}
239EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241#endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
243MODULE_LICENSE("GPL");