Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * USB Role Switch Support
  4 *
  5 * Copyright (C) 2018 Intel Corporation
  6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
  7 *         Hans de Goede <hdegoede@redhat.com>
  8 */
  9
 10#include <linux/component.h>
 11#include <linux/usb/role.h>
 12#include <linux/property.h>
 13#include <linux/device.h>
 14#include <linux/lockdep.h>
 15#include <linux/module.h>
 16#include <linux/mutex.h>
 17#include <linux/slab.h>
 18
 19static const struct class role_class = {
 20	.name = "usb_role",
 21};
 22
 23struct usb_role_switch {
 24	struct device dev;
 25	struct lock_class_key key;
 26	struct mutex lock; /* device lock*/
 27	struct module *module; /* the module this device depends on */
 28	enum usb_role role;
 29	bool registered;
 30
 31	/* From descriptor */
 32	struct device *usb2_port;
 33	struct device *usb3_port;
 34	struct device *udc;
 35	usb_role_switch_set_t set;
 36	usb_role_switch_get_t get;
 37	bool allow_userspace_control;
 38};
 39
 40#define to_role_switch(d)	container_of(d, struct usb_role_switch, dev)
 41
 42static int connector_bind(struct device *dev, struct device *connector, void *data)
 43{
 44	int ret;
 45
 46	ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector");
 47	if (ret)
 48		return ret;
 49
 50	ret = sysfs_create_link(&connector->kobj, &dev->kobj, "usb-role-switch");
 51	if (ret)
 52		sysfs_remove_link(&dev->kobj, "connector");
 53
 54	return ret;
 55}
 56
 57static void connector_unbind(struct device *dev, struct device *connector, void *data)
 58{
 59	sysfs_remove_link(&connector->kobj, "usb-role-switch");
 60	sysfs_remove_link(&dev->kobj, "connector");
 61}
 62
 63static const struct component_ops connector_ops = {
 64	.bind = connector_bind,
 65	.unbind = connector_unbind,
 66};
 67
 68/**
 69 * usb_role_switch_set_role - Set USB role for a switch
 70 * @sw: USB role switch
 71 * @role: USB role to be switched to
 72 *
 73 * Set USB role @role for @sw.
 74 */
 75int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
 76{
 77	int ret;
 78
 79	if (IS_ERR_OR_NULL(sw))
 80		return 0;
 81
 82	if (!sw->registered)
 83		return -EOPNOTSUPP;
 84
 85	mutex_lock(&sw->lock);
 86
 87	ret = sw->set(sw, role);
 88	if (!ret) {
 89		sw->role = role;
 90		kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
 91	}
 92
 93	mutex_unlock(&sw->lock);
 94
 95	return ret;
 96}
 97EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
 98
 99/**
100 * usb_role_switch_get_role - Get the USB role for a switch
101 * @sw: USB role switch
102 *
103 * Depending on the role-switch-driver this function returns either a cached
104 * value of the last set role, or reads back the actual value from the hardware.
105 */
106enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
107{
108	enum usb_role role;
109
110	if (IS_ERR_OR_NULL(sw) || !sw->registered)
111		return USB_ROLE_NONE;
112
113	mutex_lock(&sw->lock);
114
115	if (sw->get)
116		role = sw->get(sw);
117	else
118		role = sw->role;
119
120	mutex_unlock(&sw->lock);
121
122	return role;
123}
124EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
125
126static void *usb_role_switch_match(const struct fwnode_handle *fwnode, const char *id,
127				   void *data)
128{
129	struct device *dev;
130
131	if (id && !fwnode_property_present(fwnode, id))
132		return NULL;
133
134	dev = class_find_device_by_fwnode(&role_class, fwnode);
135
136	return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
137}
138
139static struct usb_role_switch *
140usb_role_switch_is_parent(struct fwnode_handle *fwnode)
141{
142	struct fwnode_handle *parent = fwnode_get_parent(fwnode);
143	struct device *dev;
144
145	if (!fwnode_property_present(parent, "usb-role-switch")) {
146		fwnode_handle_put(parent);
147		return NULL;
148	}
149
150	dev = class_find_device_by_fwnode(&role_class, parent);
151	fwnode_handle_put(parent);
152	return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
153}
154
155/**
156 * usb_role_switch_get - Find USB role switch linked with the caller
157 * @dev: The caller device
158 *
159 * Finds and returns role switch linked with @dev. The reference count for the
160 * found switch is incremented.
161 */
162struct usb_role_switch *usb_role_switch_get(struct device *dev)
163{
164	struct usb_role_switch *sw;
165
166	sw = usb_role_switch_is_parent(dev_fwnode(dev));
167	if (!sw)
168		sw = device_connection_find_match(dev, "usb-role-switch", NULL,
169						  usb_role_switch_match);
170
171	if (!IS_ERR_OR_NULL(sw))
172		WARN_ON(!try_module_get(sw->module));
173
174	return sw;
175}
176EXPORT_SYMBOL_GPL(usb_role_switch_get);
177
178/**
179 * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
180 * @fwnode: The caller device node
181 *
182 * This is similar to the usb_role_switch_get() function above, but it searches
183 * the switch using fwnode instead of device entry.
184 */
185struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
186{
187	struct usb_role_switch *sw;
188
189	sw = usb_role_switch_is_parent(fwnode);
190	if (!sw)
191		sw = fwnode_connection_find_match(fwnode, "usb-role-switch",
192						  NULL, usb_role_switch_match);
193	if (!IS_ERR_OR_NULL(sw))
194		WARN_ON(!try_module_get(sw->module));
195
196	return sw;
197}
198EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
199
200/**
201 * usb_role_switch_put - Release handle to a switch
202 * @sw: USB Role Switch
203 *
204 * Decrement reference count for @sw.
205 */
206void usb_role_switch_put(struct usb_role_switch *sw)
207{
208	if (!IS_ERR_OR_NULL(sw)) {
209		module_put(sw->module);
210		put_device(&sw->dev);
211	}
212}
213EXPORT_SYMBOL_GPL(usb_role_switch_put);
214
215/**
216 * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
217 * @fwnode: fwnode of the USB Role Switch
218 *
219 * Finds and returns role switch with @fwnode. The reference count for the
220 * found switch is incremented.
221 */
222struct usb_role_switch *
223usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
224{
225	struct device *dev;
226	struct usb_role_switch *sw = NULL;
227
228	if (!fwnode)
229		return NULL;
230
231	dev = class_find_device_by_fwnode(&role_class, fwnode);
232	if (dev) {
233		sw = to_role_switch(dev);
234		WARN_ON(!try_module_get(sw->module));
235	}
236
237	return sw;
238}
239EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode);
240
241static umode_t
242usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
243{
244	struct device *dev = kobj_to_dev(kobj);
245	struct usb_role_switch *sw = to_role_switch(dev);
246
247	if (sw->allow_userspace_control)
248		return attr->mode;
249
250	return 0;
251}
252
253static const char * const usb_roles[] = {
254	[USB_ROLE_NONE]		= "none",
255	[USB_ROLE_HOST]		= "host",
256	[USB_ROLE_DEVICE]	= "device",
257};
258
259const char *usb_role_string(enum usb_role role)
260{
261	if (role < 0 || role >= ARRAY_SIZE(usb_roles))
262		return "unknown";
263
264	return usb_roles[role];
265}
266EXPORT_SYMBOL_GPL(usb_role_string);
267
268static ssize_t
269role_show(struct device *dev, struct device_attribute *attr, char *buf)
270{
271	struct usb_role_switch *sw = to_role_switch(dev);
272	enum usb_role role = usb_role_switch_get_role(sw);
273
274	return sprintf(buf, "%s\n", usb_roles[role]);
275}
276
277static ssize_t role_store(struct device *dev, struct device_attribute *attr,
278			  const char *buf, size_t size)
279{
280	struct usb_role_switch *sw = to_role_switch(dev);
281	int ret;
282
283	ret = sysfs_match_string(usb_roles, buf);
284	if (ret < 0) {
285		bool res;
286
287		/* Extra check if the user wants to disable the switch */
288		ret = kstrtobool(buf, &res);
289		if (ret || res)
290			return -EINVAL;
291	}
292
293	ret = usb_role_switch_set_role(sw, ret);
294	if (ret)
295		return ret;
296
297	return size;
298}
299static DEVICE_ATTR_RW(role);
300
301static struct attribute *usb_role_switch_attrs[] = {
302	&dev_attr_role.attr,
303	NULL,
304};
305
306static const struct attribute_group usb_role_switch_group = {
307	.is_visible = usb_role_switch_is_visible,
308	.attrs = usb_role_switch_attrs,
309};
310
311static const struct attribute_group *usb_role_switch_groups[] = {
312	&usb_role_switch_group,
313	NULL,
314};
315
316static int usb_role_switch_uevent(const struct device *dev, struct kobj_uevent_env *env)
 
317{
318	int ret;
319
320	ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
321	if (ret)
322		dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
323
324	return ret;
325}
326
327static void usb_role_switch_release(struct device *dev)
328{
329	struct usb_role_switch *sw = to_role_switch(dev);
330
331	mutex_destroy(&sw->lock);
332	lockdep_unregister_key(&sw->key);
333	kfree(sw);
334}
335
336static const struct device_type usb_role_dev_type = {
337	.name = "usb_role_switch",
338	.groups = usb_role_switch_groups,
339	.uevent = usb_role_switch_uevent,
340	.release = usb_role_switch_release,
341};
342
343/**
344 * usb_role_switch_register - Register USB Role Switch
345 * @parent: Parent device for the switch
346 * @desc: Description of the switch
347 *
348 * USB Role Switch is a device capable or choosing the role for USB connector.
349 * On platforms where the USB controller is dual-role capable, the controller
350 * driver will need to register the switch. On platforms where the USB host and
351 * USB device controllers behind the connector are separate, there will be a
352 * mux, and the driver for that mux will need to register the switch.
353 *
354 * Returns handle to a new role switch or ERR_PTR. The content of @desc is
355 * copied.
356 */
357struct usb_role_switch *
358usb_role_switch_register(struct device *parent,
359			 const struct usb_role_switch_desc *desc)
360{
361	struct usb_role_switch *sw;
362	int ret;
363
364	if (!desc || !desc->set)
365		return ERR_PTR(-EINVAL);
366
367	sw = kzalloc(sizeof(*sw), GFP_KERNEL);
368	if (!sw)
369		return ERR_PTR(-ENOMEM);
370
371	lockdep_register_key(&sw->key);
372	mutex_init_with_key(&sw->lock, &sw->key);
373
374	sw->allow_userspace_control = desc->allow_userspace_control;
375	sw->usb2_port = desc->usb2_port;
376	sw->usb3_port = desc->usb3_port;
377	sw->udc = desc->udc;
378	sw->set = desc->set;
379	sw->get = desc->get;
380
381	sw->module = parent->driver->owner;
382	sw->dev.parent = parent;
383	sw->dev.fwnode = desc->fwnode;
384	sw->dev.class = &role_class;
385	sw->dev.type = &usb_role_dev_type;
386	dev_set_drvdata(&sw->dev, desc->driver_data);
387	dev_set_name(&sw->dev, "%s-role-switch",
388		     desc->name ? desc->name : dev_name(parent));
389
390	sw->registered = true;
391
392	ret = device_register(&sw->dev);
393	if (ret) {
394		sw->registered = false;
395		put_device(&sw->dev);
396		return ERR_PTR(ret);
397	}
398
399	if (dev_fwnode(&sw->dev)) {
400		ret = component_add(&sw->dev, &connector_ops);
401		if (ret)
402			dev_warn(&sw->dev, "failed to add component\n");
403	}
404
405	/* TODO: Symlinks for the host port and the device controller. */
406
407	return sw;
408}
409EXPORT_SYMBOL_GPL(usb_role_switch_register);
410
411/**
412 * usb_role_switch_unregister - Unregsiter USB Role Switch
413 * @sw: USB Role Switch
414 *
415 * Unregister switch that was registered with usb_role_switch_register().
416 */
417void usb_role_switch_unregister(struct usb_role_switch *sw)
418{
419	if (IS_ERR_OR_NULL(sw))
420		return;
421	sw->registered = false;
422	if (dev_fwnode(&sw->dev))
423		component_del(&sw->dev, &connector_ops);
424	device_unregister(&sw->dev);
425}
426EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
427
428/**
429 * usb_role_switch_set_drvdata - Assign private data pointer to a switch
430 * @sw: USB Role Switch
431 * @data: Private data pointer
432 */
433void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
434{
435	dev_set_drvdata(&sw->dev, data);
436}
437EXPORT_SYMBOL_GPL(usb_role_switch_set_drvdata);
438
439/**
440 * usb_role_switch_get_drvdata - Get the private data pointer of a switch
441 * @sw: USB Role Switch
442 */
443void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
444{
445	return dev_get_drvdata(&sw->dev);
446}
447EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);
448
449static int __init usb_roles_init(void)
450{
451	return class_register(&role_class);
 
452}
453subsys_initcall(usb_roles_init);
454
455static void __exit usb_roles_exit(void)
456{
457	class_unregister(&role_class);
458}
459module_exit(usb_roles_exit);
460
461MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
462MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
463MODULE_LICENSE("GPL v2");
464MODULE_DESCRIPTION("USB Role Class");
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * USB Role Switch Support
  4 *
  5 * Copyright (C) 2018 Intel Corporation
  6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
  7 *         Hans de Goede <hdegoede@redhat.com>
  8 */
  9
 
 10#include <linux/usb/role.h>
 11#include <linux/property.h>
 12#include <linux/device.h>
 
 13#include <linux/module.h>
 14#include <linux/mutex.h>
 15#include <linux/slab.h>
 16
 17static struct class *role_class;
 
 
 18
 19struct usb_role_switch {
 20	struct device dev;
 
 21	struct mutex lock; /* device lock*/
 
 22	enum usb_role role;
 
 23
 24	/* From descriptor */
 25	struct device *usb2_port;
 26	struct device *usb3_port;
 27	struct device *udc;
 28	usb_role_switch_set_t set;
 29	usb_role_switch_get_t get;
 30	bool allow_userspace_control;
 31};
 32
 33#define to_role_switch(d)	container_of(d, struct usb_role_switch, dev)
 34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 35/**
 36 * usb_role_switch_set_role - Set USB role for a switch
 37 * @sw: USB role switch
 38 * @role: USB role to be switched to
 39 *
 40 * Set USB role @role for @sw.
 41 */
 42int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
 43{
 44	int ret;
 45
 46	if (IS_ERR_OR_NULL(sw))
 47		return 0;
 48
 
 
 
 49	mutex_lock(&sw->lock);
 50
 51	ret = sw->set(sw, role);
 52	if (!ret) {
 53		sw->role = role;
 54		kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
 55	}
 56
 57	mutex_unlock(&sw->lock);
 58
 59	return ret;
 60}
 61EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
 62
 63/**
 64 * usb_role_switch_get_role - Get the USB role for a switch
 65 * @sw: USB role switch
 66 *
 67 * Depending on the role-switch-driver this function returns either a cached
 68 * value of the last set role, or reads back the actual value from the hardware.
 69 */
 70enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
 71{
 72	enum usb_role role;
 73
 74	if (IS_ERR_OR_NULL(sw))
 75		return USB_ROLE_NONE;
 76
 77	mutex_lock(&sw->lock);
 78
 79	if (sw->get)
 80		role = sw->get(sw);
 81	else
 82		role = sw->role;
 83
 84	mutex_unlock(&sw->lock);
 85
 86	return role;
 87}
 88EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
 89
 90static void *usb_role_switch_match(struct fwnode_handle *fwnode, const char *id,
 91				   void *data)
 92{
 93	struct device *dev;
 94
 95	if (id && !fwnode_property_present(fwnode, id))
 96		return NULL;
 97
 98	dev = class_find_device_by_fwnode(role_class, fwnode);
 99
100	return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
101}
102
103static struct usb_role_switch *
104usb_role_switch_is_parent(struct fwnode_handle *fwnode)
105{
106	struct fwnode_handle *parent = fwnode_get_parent(fwnode);
107	struct device *dev;
108
109	if (!parent || !fwnode_property_present(parent, "usb-role-switch"))
 
110		return NULL;
 
111
112	dev = class_find_device_by_fwnode(role_class, parent);
 
113	return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
114}
115
116/**
117 * usb_role_switch_get - Find USB role switch linked with the caller
118 * @dev: The caller device
119 *
120 * Finds and returns role switch linked with @dev. The reference count for the
121 * found switch is incremented.
122 */
123struct usb_role_switch *usb_role_switch_get(struct device *dev)
124{
125	struct usb_role_switch *sw;
126
127	sw = usb_role_switch_is_parent(dev_fwnode(dev));
128	if (!sw)
129		sw = device_connection_find_match(dev, "usb-role-switch", NULL,
130						  usb_role_switch_match);
131
132	if (!IS_ERR_OR_NULL(sw))
133		WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
134
135	return sw;
136}
137EXPORT_SYMBOL_GPL(usb_role_switch_get);
138
139/**
140 * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
141 * @fwnode: The caller device node
142 *
143 * This is similar to the usb_role_switch_get() function above, but it searches
144 * the switch using fwnode instead of device entry.
145 */
146struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
147{
148	struct usb_role_switch *sw;
149
150	sw = usb_role_switch_is_parent(fwnode);
151	if (!sw)
152		sw = fwnode_connection_find_match(fwnode, "usb-role-switch",
153						  NULL, usb_role_switch_match);
154	if (!IS_ERR_OR_NULL(sw))
155		WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
156
157	return sw;
158}
159EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
160
161/**
162 * usb_role_switch_put - Release handle to a switch
163 * @sw: USB Role Switch
164 *
165 * Decrement reference count for @sw.
166 */
167void usb_role_switch_put(struct usb_role_switch *sw)
168{
169	if (!IS_ERR_OR_NULL(sw)) {
170		module_put(sw->dev.parent->driver->owner);
171		put_device(&sw->dev);
172	}
173}
174EXPORT_SYMBOL_GPL(usb_role_switch_put);
175
176/**
177 * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
178 * @fwnode: fwnode of the USB Role Switch
179 *
180 * Finds and returns role switch with @fwnode. The reference count for the
181 * found switch is incremented.
182 */
183struct usb_role_switch *
184usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
185{
186	struct device *dev;
 
187
188	if (!fwnode)
189		return NULL;
190
191	dev = class_find_device_by_fwnode(role_class, fwnode);
192	if (dev)
193		WARN_ON(!try_module_get(dev->parent->driver->owner));
 
 
194
195	return dev ? to_role_switch(dev) : NULL;
196}
197EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode);
198
199static umode_t
200usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
201{
202	struct device *dev = kobj_to_dev(kobj);
203	struct usb_role_switch *sw = to_role_switch(dev);
204
205	if (sw->allow_userspace_control)
206		return attr->mode;
207
208	return 0;
209}
210
211static const char * const usb_roles[] = {
212	[USB_ROLE_NONE]		= "none",
213	[USB_ROLE_HOST]		= "host",
214	[USB_ROLE_DEVICE]	= "device",
215};
216
217const char *usb_role_string(enum usb_role role)
218{
219	if (role < 0 || role >= ARRAY_SIZE(usb_roles))
220		return "unknown";
221
222	return usb_roles[role];
223}
224EXPORT_SYMBOL_GPL(usb_role_string);
225
226static ssize_t
227role_show(struct device *dev, struct device_attribute *attr, char *buf)
228{
229	struct usb_role_switch *sw = to_role_switch(dev);
230	enum usb_role role = usb_role_switch_get_role(sw);
231
232	return sprintf(buf, "%s\n", usb_roles[role]);
233}
234
235static ssize_t role_store(struct device *dev, struct device_attribute *attr,
236			  const char *buf, size_t size)
237{
238	struct usb_role_switch *sw = to_role_switch(dev);
239	int ret;
240
241	ret = sysfs_match_string(usb_roles, buf);
242	if (ret < 0) {
243		bool res;
244
245		/* Extra check if the user wants to disable the switch */
246		ret = kstrtobool(buf, &res);
247		if (ret || res)
248			return -EINVAL;
249	}
250
251	ret = usb_role_switch_set_role(sw, ret);
252	if (ret)
253		return ret;
254
255	return size;
256}
257static DEVICE_ATTR_RW(role);
258
259static struct attribute *usb_role_switch_attrs[] = {
260	&dev_attr_role.attr,
261	NULL,
262};
263
264static const struct attribute_group usb_role_switch_group = {
265	.is_visible = usb_role_switch_is_visible,
266	.attrs = usb_role_switch_attrs,
267};
268
269static const struct attribute_group *usb_role_switch_groups[] = {
270	&usb_role_switch_group,
271	NULL,
272};
273
274static int
275usb_role_switch_uevent(struct device *dev, struct kobj_uevent_env *env)
276{
277	int ret;
278
279	ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
280	if (ret)
281		dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
282
283	return ret;
284}
285
286static void usb_role_switch_release(struct device *dev)
287{
288	struct usb_role_switch *sw = to_role_switch(dev);
289
 
 
290	kfree(sw);
291}
292
293static const struct device_type usb_role_dev_type = {
294	.name = "usb_role_switch",
295	.groups = usb_role_switch_groups,
296	.uevent = usb_role_switch_uevent,
297	.release = usb_role_switch_release,
298};
299
300/**
301 * usb_role_switch_register - Register USB Role Switch
302 * @parent: Parent device for the switch
303 * @desc: Description of the switch
304 *
305 * USB Role Switch is a device capable or choosing the role for USB connector.
306 * On platforms where the USB controller is dual-role capable, the controller
307 * driver will need to register the switch. On platforms where the USB host and
308 * USB device controllers behind the connector are separate, there will be a
309 * mux, and the driver for that mux will need to register the switch.
310 *
311 * Returns handle to a new role switch or ERR_PTR. The content of @desc is
312 * copied.
313 */
314struct usb_role_switch *
315usb_role_switch_register(struct device *parent,
316			 const struct usb_role_switch_desc *desc)
317{
318	struct usb_role_switch *sw;
319	int ret;
320
321	if (!desc || !desc->set)
322		return ERR_PTR(-EINVAL);
323
324	sw = kzalloc(sizeof(*sw), GFP_KERNEL);
325	if (!sw)
326		return ERR_PTR(-ENOMEM);
327
328	mutex_init(&sw->lock);
 
329
330	sw->allow_userspace_control = desc->allow_userspace_control;
331	sw->usb2_port = desc->usb2_port;
332	sw->usb3_port = desc->usb3_port;
333	sw->udc = desc->udc;
334	sw->set = desc->set;
335	sw->get = desc->get;
336
 
337	sw->dev.parent = parent;
338	sw->dev.fwnode = desc->fwnode;
339	sw->dev.class = role_class;
340	sw->dev.type = &usb_role_dev_type;
341	dev_set_drvdata(&sw->dev, desc->driver_data);
342	dev_set_name(&sw->dev, "%s-role-switch",
343		     desc->name ? desc->name : dev_name(parent));
344
 
 
345	ret = device_register(&sw->dev);
346	if (ret) {
 
347		put_device(&sw->dev);
348		return ERR_PTR(ret);
349	}
350
 
 
 
 
 
 
351	/* TODO: Symlinks for the host port and the device controller. */
352
353	return sw;
354}
355EXPORT_SYMBOL_GPL(usb_role_switch_register);
356
357/**
358 * usb_role_switch_unregister - Unregsiter USB Role Switch
359 * @sw: USB Role Switch
360 *
361 * Unregister switch that was registered with usb_role_switch_register().
362 */
363void usb_role_switch_unregister(struct usb_role_switch *sw)
364{
365	if (!IS_ERR_OR_NULL(sw))
366		device_unregister(&sw->dev);
 
 
 
 
367}
368EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
369
370/**
371 * usb_role_switch_set_drvdata - Assign private data pointer to a switch
372 * @sw: USB Role Switch
373 * @data: Private data pointer
374 */
375void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
376{
377	dev_set_drvdata(&sw->dev, data);
378}
379EXPORT_SYMBOL_GPL(usb_role_switch_set_drvdata);
380
381/**
382 * usb_role_switch_get_drvdata - Get the private data pointer of a switch
383 * @sw: USB Role Switch
384 */
385void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
386{
387	return dev_get_drvdata(&sw->dev);
388}
389EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);
390
391static int __init usb_roles_init(void)
392{
393	role_class = class_create(THIS_MODULE, "usb_role");
394	return PTR_ERR_OR_ZERO(role_class);
395}
396subsys_initcall(usb_roles_init);
397
398static void __exit usb_roles_exit(void)
399{
400	class_destroy(role_class);
401}
402module_exit(usb_roles_exit);
403
404MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
405MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
406MODULE_LICENSE("GPL v2");
407MODULE_DESCRIPTION("USB Role Class");