1 // SPDX-License-Identifier: GPL-2.0
3 * USB Role Switch Support
5 * Copyright (C) 2018 Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 * Hans de Goede <hdegoede@redhat.com>
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>
17 static struct class *role_class;
19 struct usb_role_switch {
21 struct mutex lock; /* device lock*/
25 struct device *usb2_port;
26 struct device *usb3_port;
28 usb_role_switch_set_t set;
29 usb_role_switch_get_t get;
30 bool allow_userspace_control;
33 #define to_role_switch(d) container_of(d, struct usb_role_switch, dev)
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
40 * Set USB role @role for @sw.
42 int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
46 if (IS_ERR_OR_NULL(sw))
49 mutex_lock(&sw->lock);
51 ret = sw->set(sw, role);
54 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
57 mutex_unlock(&sw->lock);
61 EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
64 * usb_role_switch_get_role - Get the USB role for a switch
65 * @sw: USB role switch
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.
70 enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
74 if (IS_ERR_OR_NULL(sw))
77 mutex_lock(&sw->lock);
84 mutex_unlock(&sw->lock);
88 EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
90 static void *usb_role_switch_match(const struct fwnode_handle *fwnode, const char *id,
95 if (id && !fwnode_property_present(fwnode, id))
98 dev = class_find_device_by_fwnode(role_class, fwnode);
100 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
103 static struct usb_role_switch *
104 usb_role_switch_is_parent(struct fwnode_handle *fwnode)
106 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
109 if (!fwnode_property_present(parent, "usb-role-switch")) {
110 fwnode_handle_put(parent);
114 dev = class_find_device_by_fwnode(role_class, parent);
115 fwnode_handle_put(parent);
116 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
120 * usb_role_switch_get - Find USB role switch linked with the caller
121 * @dev: The caller device
123 * Finds and returns role switch linked with @dev. The reference count for the
124 * found switch is incremented.
126 struct usb_role_switch *usb_role_switch_get(struct device *dev)
128 struct usb_role_switch *sw;
130 sw = usb_role_switch_is_parent(dev_fwnode(dev));
132 sw = device_connection_find_match(dev, "usb-role-switch", NULL,
133 usb_role_switch_match);
135 if (!IS_ERR_OR_NULL(sw))
136 WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
140 EXPORT_SYMBOL_GPL(usb_role_switch_get);
143 * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
144 * @fwnode: The caller device node
146 * This is similar to the usb_role_switch_get() function above, but it searches
147 * the switch using fwnode instead of device entry.
149 struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
151 struct usb_role_switch *sw;
153 sw = usb_role_switch_is_parent(fwnode);
155 sw = fwnode_connection_find_match(fwnode, "usb-role-switch",
156 NULL, usb_role_switch_match);
157 if (!IS_ERR_OR_NULL(sw))
158 WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
162 EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
165 * usb_role_switch_put - Release handle to a switch
166 * @sw: USB Role Switch
168 * Decrement reference count for @sw.
170 void usb_role_switch_put(struct usb_role_switch *sw)
172 if (!IS_ERR_OR_NULL(sw)) {
173 module_put(sw->dev.parent->driver->owner);
174 put_device(&sw->dev);
177 EXPORT_SYMBOL_GPL(usb_role_switch_put);
180 * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
181 * @fwnode: fwnode of the USB Role Switch
183 * Finds and returns role switch with @fwnode. The reference count for the
184 * found switch is incremented.
186 struct usb_role_switch *
187 usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
194 dev = class_find_device_by_fwnode(role_class, fwnode);
196 WARN_ON(!try_module_get(dev->parent->driver->owner));
198 return dev ? to_role_switch(dev) : NULL;
200 EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode);
203 usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
205 struct device *dev = kobj_to_dev(kobj);
206 struct usb_role_switch *sw = to_role_switch(dev);
208 if (sw->allow_userspace_control)
214 static const char * const usb_roles[] = {
215 [USB_ROLE_NONE] = "none",
216 [USB_ROLE_HOST] = "host",
217 [USB_ROLE_DEVICE] = "device",
220 const char *usb_role_string(enum usb_role role)
222 if (role < 0 || role >= ARRAY_SIZE(usb_roles))
225 return usb_roles[role];
227 EXPORT_SYMBOL_GPL(usb_role_string);
230 role_show(struct device *dev, struct device_attribute *attr, char *buf)
232 struct usb_role_switch *sw = to_role_switch(dev);
233 enum usb_role role = usb_role_switch_get_role(sw);
235 return sprintf(buf, "%s\n", usb_roles[role]);
238 static ssize_t role_store(struct device *dev, struct device_attribute *attr,
239 const char *buf, size_t size)
241 struct usb_role_switch *sw = to_role_switch(dev);
244 ret = sysfs_match_string(usb_roles, buf);
248 /* Extra check if the user wants to disable the switch */
249 ret = kstrtobool(buf, &res);
254 ret = usb_role_switch_set_role(sw, ret);
260 static DEVICE_ATTR_RW(role);
262 static struct attribute *usb_role_switch_attrs[] = {
267 static const struct attribute_group usb_role_switch_group = {
268 .is_visible = usb_role_switch_is_visible,
269 .attrs = usb_role_switch_attrs,
272 static const struct attribute_group *usb_role_switch_groups[] = {
273 &usb_role_switch_group,
277 static int usb_role_switch_uevent(const struct device *dev, struct kobj_uevent_env *env)
281 ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
283 dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
288 static void usb_role_switch_release(struct device *dev)
290 struct usb_role_switch *sw = to_role_switch(dev);
295 static const struct device_type usb_role_dev_type = {
296 .name = "usb_role_switch",
297 .groups = usb_role_switch_groups,
298 .uevent = usb_role_switch_uevent,
299 .release = usb_role_switch_release,
303 * usb_role_switch_register - Register USB Role Switch
304 * @parent: Parent device for the switch
305 * @desc: Description of the switch
307 * USB Role Switch is a device capable or choosing the role for USB connector.
308 * On platforms where the USB controller is dual-role capable, the controller
309 * driver will need to register the switch. On platforms where the USB host and
310 * USB device controllers behind the connector are separate, there will be a
311 * mux, and the driver for that mux will need to register the switch.
313 * Returns handle to a new role switch or ERR_PTR. The content of @desc is
316 struct usb_role_switch *
317 usb_role_switch_register(struct device *parent,
318 const struct usb_role_switch_desc *desc)
320 struct usb_role_switch *sw;
323 if (!desc || !desc->set)
324 return ERR_PTR(-EINVAL);
326 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
328 return ERR_PTR(-ENOMEM);
330 mutex_init(&sw->lock);
332 sw->allow_userspace_control = desc->allow_userspace_control;
333 sw->usb2_port = desc->usb2_port;
334 sw->usb3_port = desc->usb3_port;
339 sw->dev.parent = parent;
340 sw->dev.fwnode = desc->fwnode;
341 sw->dev.class = role_class;
342 sw->dev.type = &usb_role_dev_type;
343 dev_set_drvdata(&sw->dev, desc->driver_data);
344 dev_set_name(&sw->dev, "%s-role-switch",
345 desc->name ? desc->name : dev_name(parent));
347 ret = device_register(&sw->dev);
349 put_device(&sw->dev);
353 /* TODO: Symlinks for the host port and the device controller. */
357 EXPORT_SYMBOL_GPL(usb_role_switch_register);
360 * usb_role_switch_unregister - Unregsiter USB Role Switch
361 * @sw: USB Role Switch
363 * Unregister switch that was registered with usb_role_switch_register().
365 void usb_role_switch_unregister(struct usb_role_switch *sw)
367 if (!IS_ERR_OR_NULL(sw))
368 device_unregister(&sw->dev);
370 EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
373 * usb_role_switch_set_drvdata - Assign private data pointer to a switch
374 * @sw: USB Role Switch
375 * @data: Private data pointer
377 void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
379 dev_set_drvdata(&sw->dev, data);
381 EXPORT_SYMBOL_GPL(usb_role_switch_set_drvdata);
384 * usb_role_switch_get_drvdata - Get the private data pointer of a switch
385 * @sw: USB Role Switch
387 void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
389 return dev_get_drvdata(&sw->dev);
391 EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);
393 static int __init usb_roles_init(void)
395 role_class = class_create("usb_role");
396 return PTR_ERR_OR_ZERO(role_class);
398 subsys_initcall(usb_roles_init);
400 static void __exit usb_roles_exit(void)
402 class_destroy(role_class);
404 module_exit(usb_roles_exit);
406 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
407 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
408 MODULE_LICENSE("GPL v2");
409 MODULE_DESCRIPTION("USB Role Class");