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->dev.parent, role);
55 mutex_unlock(&sw->lock);
59 EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
62 * usb_role_switch_get_role - Get the USB role for a switch
63 * @sw: USB role switch
65 * Depending on the role-switch-driver this function returns either a cached
66 * value of the last set role, or reads back the actual value from the hardware.
68 enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
72 if (IS_ERR_OR_NULL(sw))
75 mutex_lock(&sw->lock);
78 role = sw->get(sw->dev.parent);
82 mutex_unlock(&sw->lock);
86 EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
88 static void *usb_role_switch_match(struct device_connection *con, int ep,
94 if (con->id && !fwnode_property_present(con->fwnode, con->id))
97 dev = class_find_device_by_fwnode(role_class, con->fwnode);
99 dev = class_find_device_by_name(role_class, con->endpoint[ep]);
102 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
105 static struct usb_role_switch *
106 usb_role_switch_is_parent(struct fwnode_handle *fwnode)
108 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
111 if (!parent || !fwnode_property_present(parent, "usb-role-switch"))
114 dev = class_find_device_by_fwnode(role_class, parent);
115 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
119 * usb_role_switch_get - Find USB role switch linked with the caller
120 * @dev: The caller device
122 * Finds and returns role switch linked with @dev. The reference count for the
123 * found switch is incremented.
125 struct usb_role_switch *usb_role_switch_get(struct device *dev)
127 struct usb_role_switch *sw;
129 sw = usb_role_switch_is_parent(dev_fwnode(dev));
131 sw = device_connection_find_match(dev, "usb-role-switch", NULL,
132 usb_role_switch_match);
134 if (!IS_ERR_OR_NULL(sw))
135 WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
139 EXPORT_SYMBOL_GPL(usb_role_switch_get);
142 * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
143 * @fwnode: The caller device node
145 * This is similar to the usb_role_switch_get() function above, but it searches
146 * the switch using fwnode instead of device entry.
148 struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
150 struct usb_role_switch *sw;
152 sw = usb_role_switch_is_parent(fwnode);
154 sw = fwnode_connection_find_match(fwnode, "usb-role-switch",
155 NULL, usb_role_switch_match);
156 if (!IS_ERR_OR_NULL(sw))
157 WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
161 EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
164 * usb_role_switch_put - Release handle to a switch
165 * @sw: USB Role Switch
167 * Decrement reference count for @sw.
169 void usb_role_switch_put(struct usb_role_switch *sw)
171 if (!IS_ERR_OR_NULL(sw)) {
172 put_device(&sw->dev);
173 module_put(sw->dev.parent->driver->owner);
176 EXPORT_SYMBOL_GPL(usb_role_switch_put);
179 usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
181 struct device *dev = container_of(kobj, typeof(*dev), kobj);
182 struct usb_role_switch *sw = to_role_switch(dev);
184 if (sw->allow_userspace_control)
190 static const char * const usb_roles[] = {
191 [USB_ROLE_NONE] = "none",
192 [USB_ROLE_HOST] = "host",
193 [USB_ROLE_DEVICE] = "device",
197 role_show(struct device *dev, struct device_attribute *attr, char *buf)
199 struct usb_role_switch *sw = to_role_switch(dev);
200 enum usb_role role = usb_role_switch_get_role(sw);
202 return sprintf(buf, "%s\n", usb_roles[role]);
205 static ssize_t role_store(struct device *dev, struct device_attribute *attr,
206 const char *buf, size_t size)
208 struct usb_role_switch *sw = to_role_switch(dev);
211 ret = sysfs_match_string(usb_roles, buf);
215 /* Extra check if the user wants to disable the switch */
216 ret = kstrtobool(buf, &res);
221 ret = usb_role_switch_set_role(sw, ret);
227 static DEVICE_ATTR_RW(role);
229 static struct attribute *usb_role_switch_attrs[] = {
234 static const struct attribute_group usb_role_switch_group = {
235 .is_visible = usb_role_switch_is_visible,
236 .attrs = usb_role_switch_attrs,
239 static const struct attribute_group *usb_role_switch_groups[] = {
240 &usb_role_switch_group,
245 usb_role_switch_uevent(struct device *dev, struct kobj_uevent_env *env)
249 ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
251 dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
256 static void usb_role_switch_release(struct device *dev)
258 struct usb_role_switch *sw = to_role_switch(dev);
263 static const struct device_type usb_role_dev_type = {
264 .name = "usb_role_switch",
265 .groups = usb_role_switch_groups,
266 .uevent = usb_role_switch_uevent,
267 .release = usb_role_switch_release,
271 * usb_role_switch_register - Register USB Role Switch
272 * @parent: Parent device for the switch
273 * @desc: Description of the switch
275 * USB Role Switch is a device capable or choosing the role for USB connector.
276 * On platforms where the USB controller is dual-role capable, the controller
277 * driver will need to register the switch. On platforms where the USB host and
278 * USB device controllers behind the connector are separate, there will be a
279 * mux, and the driver for that mux will need to register the switch.
281 * Returns handle to a new role switch or ERR_PTR. The content of @desc is
284 struct usb_role_switch *
285 usb_role_switch_register(struct device *parent,
286 const struct usb_role_switch_desc *desc)
288 struct usb_role_switch *sw;
291 if (!desc || !desc->set)
292 return ERR_PTR(-EINVAL);
294 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
296 return ERR_PTR(-ENOMEM);
298 mutex_init(&sw->lock);
300 sw->allow_userspace_control = desc->allow_userspace_control;
301 sw->usb2_port = desc->usb2_port;
302 sw->usb3_port = desc->usb3_port;
307 sw->dev.parent = parent;
308 sw->dev.fwnode = desc->fwnode;
309 sw->dev.class = role_class;
310 sw->dev.type = &usb_role_dev_type;
311 dev_set_name(&sw->dev, "%s-role-switch", dev_name(parent));
313 ret = device_register(&sw->dev);
315 put_device(&sw->dev);
319 /* TODO: Symlinks for the host port and the device controller. */
323 EXPORT_SYMBOL_GPL(usb_role_switch_register);
326 * usb_role_switch_unregister - Unregsiter USB Role Switch
327 * @sw: USB Role Switch
329 * Unregister switch that was registered with usb_role_switch_register().
331 void usb_role_switch_unregister(struct usb_role_switch *sw)
333 if (!IS_ERR_OR_NULL(sw))
334 device_unregister(&sw->dev);
336 EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
338 static int __init usb_roles_init(void)
340 role_class = class_create(THIS_MODULE, "usb_role");
341 return PTR_ERR_OR_ZERO(role_class);
343 subsys_initcall(usb_roles_init);
345 static void __exit usb_roles_exit(void)
347 class_destroy(role_class);
349 module_exit(usb_roles_exit);
351 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
352 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
353 MODULE_LICENSE("GPL v2");
354 MODULE_DESCRIPTION("USB Role Class");