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*/
22 struct module *module; /* the module this device depends on */
27 struct device *usb2_port;
28 struct device *usb3_port;
30 usb_role_switch_set_t set;
31 usb_role_switch_get_t get;
32 bool allow_userspace_control;
35 #define to_role_switch(d) container_of(d, struct usb_role_switch, dev)
38 * usb_role_switch_set_role - Set USB role for a switch
39 * @sw: USB role switch
40 * @role: USB role to be switched to
42 * Set USB role @role for @sw.
44 int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
48 if (IS_ERR_OR_NULL(sw))
54 mutex_lock(&sw->lock);
56 ret = sw->set(sw, role);
59 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
62 mutex_unlock(&sw->lock);
66 EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
69 * usb_role_switch_get_role - Get the USB role for a switch
70 * @sw: USB role switch
72 * Depending on the role-switch-driver this function returns either a cached
73 * value of the last set role, or reads back the actual value from the hardware.
75 enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
79 if (IS_ERR_OR_NULL(sw) || !sw->registered)
82 mutex_lock(&sw->lock);
89 mutex_unlock(&sw->lock);
93 EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
95 static void *usb_role_switch_match(struct fwnode_handle *fwnode, const char *id,
100 if (id && !fwnode_property_present(fwnode, id))
103 dev = class_find_device_by_fwnode(role_class, fwnode);
105 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
108 static struct usb_role_switch *
109 usb_role_switch_is_parent(struct fwnode_handle *fwnode)
111 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
114 if (!fwnode_property_present(parent, "usb-role-switch")) {
115 fwnode_handle_put(parent);
119 dev = class_find_device_by_fwnode(role_class, parent);
120 fwnode_handle_put(parent);
121 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
125 * usb_role_switch_get - Find USB role switch linked with the caller
126 * @dev: The caller device
128 * Finds and returns role switch linked with @dev. The reference count for the
129 * found switch is incremented.
131 struct usb_role_switch *usb_role_switch_get(struct device *dev)
133 struct usb_role_switch *sw;
135 sw = usb_role_switch_is_parent(dev_fwnode(dev));
137 sw = device_connection_find_match(dev, "usb-role-switch", NULL,
138 usb_role_switch_match);
140 if (!IS_ERR_OR_NULL(sw))
141 WARN_ON(!try_module_get(sw->module));
145 EXPORT_SYMBOL_GPL(usb_role_switch_get);
148 * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
149 * @fwnode: The caller device node
151 * This is similar to the usb_role_switch_get() function above, but it searches
152 * the switch using fwnode instead of device entry.
154 struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
156 struct usb_role_switch *sw;
158 sw = usb_role_switch_is_parent(fwnode);
160 sw = fwnode_connection_find_match(fwnode, "usb-role-switch",
161 NULL, usb_role_switch_match);
162 if (!IS_ERR_OR_NULL(sw))
163 WARN_ON(!try_module_get(sw->module));
167 EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
170 * usb_role_switch_put - Release handle to a switch
171 * @sw: USB Role Switch
173 * Decrement reference count for @sw.
175 void usb_role_switch_put(struct usb_role_switch *sw)
177 if (!IS_ERR_OR_NULL(sw)) {
178 module_put(sw->module);
179 put_device(&sw->dev);
182 EXPORT_SYMBOL_GPL(usb_role_switch_put);
185 * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
186 * @fwnode: fwnode of the USB Role Switch
188 * Finds and returns role switch with @fwnode. The reference count for the
189 * found switch is incremented.
191 struct usb_role_switch *
192 usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
195 struct usb_role_switch *sw = NULL;
200 dev = class_find_device_by_fwnode(role_class, fwnode);
202 sw = to_role_switch(dev);
203 WARN_ON(!try_module_get(sw->module));
208 EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode);
211 usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
213 struct device *dev = kobj_to_dev(kobj);
214 struct usb_role_switch *sw = to_role_switch(dev);
216 if (sw->allow_userspace_control)
222 static const char * const usb_roles[] = {
223 [USB_ROLE_NONE] = "none",
224 [USB_ROLE_HOST] = "host",
225 [USB_ROLE_DEVICE] = "device",
229 role_show(struct device *dev, struct device_attribute *attr, char *buf)
231 struct usb_role_switch *sw = to_role_switch(dev);
232 enum usb_role role = usb_role_switch_get_role(sw);
234 return sprintf(buf, "%s\n", usb_roles[role]);
237 static ssize_t role_store(struct device *dev, struct device_attribute *attr,
238 const char *buf, size_t size)
240 struct usb_role_switch *sw = to_role_switch(dev);
243 ret = sysfs_match_string(usb_roles, buf);
247 /* Extra check if the user wants to disable the switch */
248 ret = kstrtobool(buf, &res);
253 ret = usb_role_switch_set_role(sw, ret);
259 static DEVICE_ATTR_RW(role);
261 static struct attribute *usb_role_switch_attrs[] = {
266 static const struct attribute_group usb_role_switch_group = {
267 .is_visible = usb_role_switch_is_visible,
268 .attrs = usb_role_switch_attrs,
271 static const struct attribute_group *usb_role_switch_groups[] = {
272 &usb_role_switch_group,
277 usb_role_switch_uevent(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->module = parent->driver->owner;
340 sw->dev.parent = parent;
341 sw->dev.fwnode = desc->fwnode;
342 sw->dev.class = role_class;
343 sw->dev.type = &usb_role_dev_type;
344 dev_set_drvdata(&sw->dev, desc->driver_data);
345 dev_set_name(&sw->dev, "%s-role-switch",
346 desc->name ? desc->name : dev_name(parent));
348 ret = device_register(&sw->dev);
350 put_device(&sw->dev);
354 sw->registered = true;
356 /* TODO: Symlinks for the host port and the device controller. */
360 EXPORT_SYMBOL_GPL(usb_role_switch_register);
363 * usb_role_switch_unregister - Unregsiter USB Role Switch
364 * @sw: USB Role Switch
366 * Unregister switch that was registered with usb_role_switch_register().
368 void usb_role_switch_unregister(struct usb_role_switch *sw)
370 if (!IS_ERR_OR_NULL(sw)) {
371 sw->registered = false;
372 device_unregister(&sw->dev);
375 EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
378 * usb_role_switch_set_drvdata - Assign private data pointer to a switch
379 * @sw: USB Role Switch
380 * @data: Private data pointer
382 void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
384 dev_set_drvdata(&sw->dev, data);
386 EXPORT_SYMBOL_GPL(usb_role_switch_set_drvdata);
389 * usb_role_switch_get_drvdata - Get the private data pointer of a switch
390 * @sw: USB Role Switch
392 void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
394 return dev_get_drvdata(&sw->dev);
396 EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);
398 static int __init usb_roles_init(void)
400 role_class = class_create(THIS_MODULE, "usb_role");
401 return PTR_ERR_OR_ZERO(role_class);
403 subsys_initcall(usb_roles_init);
405 static void __exit usb_roles_exit(void)
407 class_destroy(role_class);
409 module_exit(usb_roles_exit);
411 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
412 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
413 MODULE_LICENSE("GPL v2");
414 MODULE_DESCRIPTION("USB Role Class");