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/component.h>
11 #include <linux/usb/role.h>
12 #include <linux/property.h>
13 #include <linux/device.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/slab.h>
18 static const struct class role_class = {
22 struct usb_role_switch {
24 struct mutex lock; /* device lock*/
25 struct module *module; /* the module this device depends on */
30 struct device *usb2_port;
31 struct device *usb3_port;
33 usb_role_switch_set_t set;
34 usb_role_switch_get_t get;
35 bool allow_userspace_control;
38 #define to_role_switch(d) container_of(d, struct usb_role_switch, dev)
40 static int connector_bind(struct device *dev, struct device *connector, void *data)
44 ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector");
48 ret = sysfs_create_link(&connector->kobj, &dev->kobj, "usb-role-switch");
50 sysfs_remove_link(&dev->kobj, "connector");
55 static void connector_unbind(struct device *dev, struct device *connector, void *data)
57 sysfs_remove_link(&connector->kobj, "usb-role-switch");
58 sysfs_remove_link(&dev->kobj, "connector");
61 static const struct component_ops connector_ops = {
62 .bind = connector_bind,
63 .unbind = connector_unbind,
67 * usb_role_switch_set_role - Set USB role for a switch
68 * @sw: USB role switch
69 * @role: USB role to be switched to
71 * Set USB role @role for @sw.
73 int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
77 if (IS_ERR_OR_NULL(sw))
83 mutex_lock(&sw->lock);
85 ret = sw->set(sw, role);
88 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
91 mutex_unlock(&sw->lock);
95 EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
98 * usb_role_switch_get_role - Get the USB role for a switch
99 * @sw: USB role switch
101 * Depending on the role-switch-driver this function returns either a cached
102 * value of the last set role, or reads back the actual value from the hardware.
104 enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
108 if (IS_ERR_OR_NULL(sw) || !sw->registered)
109 return USB_ROLE_NONE;
111 mutex_lock(&sw->lock);
118 mutex_unlock(&sw->lock);
122 EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
124 static void *usb_role_switch_match(const struct fwnode_handle *fwnode, const char *id,
129 if (id && !fwnode_property_present(fwnode, id))
132 dev = class_find_device_by_fwnode(&role_class, fwnode);
134 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
137 static struct usb_role_switch *
138 usb_role_switch_is_parent(struct fwnode_handle *fwnode)
140 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
143 if (!fwnode_property_present(parent, "usb-role-switch")) {
144 fwnode_handle_put(parent);
148 dev = class_find_device_by_fwnode(&role_class, parent);
149 fwnode_handle_put(parent);
150 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
154 * usb_role_switch_get - Find USB role switch linked with the caller
155 * @dev: The caller device
157 * Finds and returns role switch linked with @dev. The reference count for the
158 * found switch is incremented.
160 struct usb_role_switch *usb_role_switch_get(struct device *dev)
162 struct usb_role_switch *sw;
164 sw = usb_role_switch_is_parent(dev_fwnode(dev));
166 sw = device_connection_find_match(dev, "usb-role-switch", NULL,
167 usb_role_switch_match);
169 if (!IS_ERR_OR_NULL(sw))
170 WARN_ON(!try_module_get(sw->module));
174 EXPORT_SYMBOL_GPL(usb_role_switch_get);
177 * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
178 * @fwnode: The caller device node
180 * This is similar to the usb_role_switch_get() function above, but it searches
181 * the switch using fwnode instead of device entry.
183 struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
185 struct usb_role_switch *sw;
187 sw = usb_role_switch_is_parent(fwnode);
189 sw = fwnode_connection_find_match(fwnode, "usb-role-switch",
190 NULL, usb_role_switch_match);
191 if (!IS_ERR_OR_NULL(sw))
192 WARN_ON(!try_module_get(sw->module));
196 EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
199 * usb_role_switch_put - Release handle to a switch
200 * @sw: USB Role Switch
202 * Decrement reference count for @sw.
204 void usb_role_switch_put(struct usb_role_switch *sw)
206 if (!IS_ERR_OR_NULL(sw)) {
207 module_put(sw->module);
208 put_device(&sw->dev);
211 EXPORT_SYMBOL_GPL(usb_role_switch_put);
214 * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
215 * @fwnode: fwnode of the USB Role Switch
217 * Finds and returns role switch with @fwnode. The reference count for the
218 * found switch is incremented.
220 struct usb_role_switch *
221 usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
224 struct usb_role_switch *sw = NULL;
229 dev = class_find_device_by_fwnode(&role_class, fwnode);
231 sw = to_role_switch(dev);
232 WARN_ON(!try_module_get(sw->module));
237 EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode);
240 usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
242 struct device *dev = kobj_to_dev(kobj);
243 struct usb_role_switch *sw = to_role_switch(dev);
245 if (sw->allow_userspace_control)
251 static const char * const usb_roles[] = {
252 [USB_ROLE_NONE] = "none",
253 [USB_ROLE_HOST] = "host",
254 [USB_ROLE_DEVICE] = "device",
257 const char *usb_role_string(enum usb_role role)
259 if (role < 0 || role >= ARRAY_SIZE(usb_roles))
262 return usb_roles[role];
264 EXPORT_SYMBOL_GPL(usb_role_string);
267 role_show(struct device *dev, struct device_attribute *attr, char *buf)
269 struct usb_role_switch *sw = to_role_switch(dev);
270 enum usb_role role = usb_role_switch_get_role(sw);
272 return sprintf(buf, "%s\n", usb_roles[role]);
275 static ssize_t role_store(struct device *dev, struct device_attribute *attr,
276 const char *buf, size_t size)
278 struct usb_role_switch *sw = to_role_switch(dev);
281 ret = sysfs_match_string(usb_roles, buf);
285 /* Extra check if the user wants to disable the switch */
286 ret = kstrtobool(buf, &res);
291 ret = usb_role_switch_set_role(sw, ret);
297 static DEVICE_ATTR_RW(role);
299 static struct attribute *usb_role_switch_attrs[] = {
304 static const struct attribute_group usb_role_switch_group = {
305 .is_visible = usb_role_switch_is_visible,
306 .attrs = usb_role_switch_attrs,
309 static const struct attribute_group *usb_role_switch_groups[] = {
310 &usb_role_switch_group,
314 static int usb_role_switch_uevent(const struct device *dev, struct kobj_uevent_env *env)
318 ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
320 dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
325 static void usb_role_switch_release(struct device *dev)
327 struct usb_role_switch *sw = to_role_switch(dev);
332 static const struct device_type usb_role_dev_type = {
333 .name = "usb_role_switch",
334 .groups = usb_role_switch_groups,
335 .uevent = usb_role_switch_uevent,
336 .release = usb_role_switch_release,
340 * usb_role_switch_register - Register USB Role Switch
341 * @parent: Parent device for the switch
342 * @desc: Description of the switch
344 * USB Role Switch is a device capable or choosing the role for USB connector.
345 * On platforms where the USB controller is dual-role capable, the controller
346 * driver will need to register the switch. On platforms where the USB host and
347 * USB device controllers behind the connector are separate, there will be a
348 * mux, and the driver for that mux will need to register the switch.
350 * Returns handle to a new role switch or ERR_PTR. The content of @desc is
353 struct usb_role_switch *
354 usb_role_switch_register(struct device *parent,
355 const struct usb_role_switch_desc *desc)
357 struct usb_role_switch *sw;
360 if (!desc || !desc->set)
361 return ERR_PTR(-EINVAL);
363 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
365 return ERR_PTR(-ENOMEM);
367 mutex_init(&sw->lock);
369 sw->allow_userspace_control = desc->allow_userspace_control;
370 sw->usb2_port = desc->usb2_port;
371 sw->usb3_port = desc->usb3_port;
376 sw->module = parent->driver->owner;
377 sw->dev.parent = parent;
378 sw->dev.fwnode = desc->fwnode;
379 sw->dev.class = &role_class;
380 sw->dev.type = &usb_role_dev_type;
381 dev_set_drvdata(&sw->dev, desc->driver_data);
382 dev_set_name(&sw->dev, "%s-role-switch",
383 desc->name ? desc->name : dev_name(parent));
385 ret = device_register(&sw->dev);
387 put_device(&sw->dev);
391 if (dev_fwnode(&sw->dev)) {
392 ret = component_add(&sw->dev, &connector_ops);
394 dev_warn(&sw->dev, "failed to add component\n");
397 sw->registered = true;
399 /* TODO: Symlinks for the host port and the device controller. */
403 EXPORT_SYMBOL_GPL(usb_role_switch_register);
406 * usb_role_switch_unregister - Unregsiter USB Role Switch
407 * @sw: USB Role Switch
409 * Unregister switch that was registered with usb_role_switch_register().
411 void usb_role_switch_unregister(struct usb_role_switch *sw)
413 if (IS_ERR_OR_NULL(sw))
415 sw->registered = false;
416 if (dev_fwnode(&sw->dev))
417 component_del(&sw->dev, &connector_ops);
418 device_unregister(&sw->dev);
420 EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
423 * usb_role_switch_set_drvdata - Assign private data pointer to a switch
424 * @sw: USB Role Switch
425 * @data: Private data pointer
427 void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
429 dev_set_drvdata(&sw->dev, data);
431 EXPORT_SYMBOL_GPL(usb_role_switch_set_drvdata);
434 * usb_role_switch_get_drvdata - Get the private data pointer of a switch
435 * @sw: USB Role Switch
437 void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
439 return dev_get_drvdata(&sw->dev);
441 EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);
443 static int __init usb_roles_init(void)
445 return class_register(&role_class);
447 subsys_initcall(usb_roles_init);
449 static void __exit usb_roles_exit(void)
451 class_unregister(&role_class);
453 module_exit(usb_roles_exit);
455 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
456 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
457 MODULE_LICENSE("GPL v2");
458 MODULE_DESCRIPTION("USB Role Class");