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 const struct class role_class = {
21 struct usb_role_switch {
23 struct mutex lock; /* device lock*/
24 struct module *module; /* the module this device depends on */
29 struct device *usb2_port;
30 struct device *usb3_port;
32 usb_role_switch_set_t set;
33 usb_role_switch_get_t get;
34 bool allow_userspace_control;
37 #define to_role_switch(d) container_of(d, struct usb_role_switch, dev)
40 * usb_role_switch_set_role - Set USB role for a switch
41 * @sw: USB role switch
42 * @role: USB role to be switched to
44 * Set USB role @role for @sw.
46 int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
50 if (IS_ERR_OR_NULL(sw))
56 mutex_lock(&sw->lock);
58 ret = sw->set(sw, role);
61 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
64 mutex_unlock(&sw->lock);
68 EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
71 * usb_role_switch_get_role - Get the USB role for a switch
72 * @sw: USB role switch
74 * Depending on the role-switch-driver this function returns either a cached
75 * value of the last set role, or reads back the actual value from the hardware.
77 enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
81 if (IS_ERR_OR_NULL(sw) || !sw->registered)
84 mutex_lock(&sw->lock);
91 mutex_unlock(&sw->lock);
95 EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
97 static void *usb_role_switch_match(const struct fwnode_handle *fwnode, const char *id,
102 if (id && !fwnode_property_present(fwnode, id))
105 dev = class_find_device_by_fwnode(&role_class, fwnode);
107 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
110 static struct usb_role_switch *
111 usb_role_switch_is_parent(struct fwnode_handle *fwnode)
113 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
116 if (!fwnode_property_present(parent, "usb-role-switch")) {
117 fwnode_handle_put(parent);
121 dev = class_find_device_by_fwnode(&role_class, parent);
122 fwnode_handle_put(parent);
123 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
127 * usb_role_switch_get - Find USB role switch linked with the caller
128 * @dev: The caller device
130 * Finds and returns role switch linked with @dev. The reference count for the
131 * found switch is incremented.
133 struct usb_role_switch *usb_role_switch_get(struct device *dev)
135 struct usb_role_switch *sw;
137 sw = usb_role_switch_is_parent(dev_fwnode(dev));
139 sw = device_connection_find_match(dev, "usb-role-switch", NULL,
140 usb_role_switch_match);
142 if (!IS_ERR_OR_NULL(sw))
143 WARN_ON(!try_module_get(sw->module));
147 EXPORT_SYMBOL_GPL(usb_role_switch_get);
150 * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
151 * @fwnode: The caller device node
153 * This is similar to the usb_role_switch_get() function above, but it searches
154 * the switch using fwnode instead of device entry.
156 struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
158 struct usb_role_switch *sw;
160 sw = usb_role_switch_is_parent(fwnode);
162 sw = fwnode_connection_find_match(fwnode, "usb-role-switch",
163 NULL, usb_role_switch_match);
164 if (!IS_ERR_OR_NULL(sw))
165 WARN_ON(!try_module_get(sw->module));
169 EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
172 * usb_role_switch_put - Release handle to a switch
173 * @sw: USB Role Switch
175 * Decrement reference count for @sw.
177 void usb_role_switch_put(struct usb_role_switch *sw)
179 if (!IS_ERR_OR_NULL(sw)) {
180 module_put(sw->module);
181 put_device(&sw->dev);
184 EXPORT_SYMBOL_GPL(usb_role_switch_put);
187 * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
188 * @fwnode: fwnode of the USB Role Switch
190 * Finds and returns role switch with @fwnode. The reference count for the
191 * found switch is incremented.
193 struct usb_role_switch *
194 usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
197 struct usb_role_switch *sw = NULL;
202 dev = class_find_device_by_fwnode(&role_class, fwnode);
204 sw = to_role_switch(dev);
205 WARN_ON(!try_module_get(sw->module));
210 EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode);
213 usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
215 struct device *dev = kobj_to_dev(kobj);
216 struct usb_role_switch *sw = to_role_switch(dev);
218 if (sw->allow_userspace_control)
224 static const char * const usb_roles[] = {
225 [USB_ROLE_NONE] = "none",
226 [USB_ROLE_HOST] = "host",
227 [USB_ROLE_DEVICE] = "device",
230 const char *usb_role_string(enum usb_role role)
232 if (role < 0 || role >= ARRAY_SIZE(usb_roles))
235 return usb_roles[role];
237 EXPORT_SYMBOL_GPL(usb_role_string);
240 role_show(struct device *dev, struct device_attribute *attr, char *buf)
242 struct usb_role_switch *sw = to_role_switch(dev);
243 enum usb_role role = usb_role_switch_get_role(sw);
245 return sprintf(buf, "%s\n", usb_roles[role]);
248 static ssize_t role_store(struct device *dev, struct device_attribute *attr,
249 const char *buf, size_t size)
251 struct usb_role_switch *sw = to_role_switch(dev);
254 ret = sysfs_match_string(usb_roles, buf);
258 /* Extra check if the user wants to disable the switch */
259 ret = kstrtobool(buf, &res);
264 ret = usb_role_switch_set_role(sw, ret);
270 static DEVICE_ATTR_RW(role);
272 static struct attribute *usb_role_switch_attrs[] = {
277 static const struct attribute_group usb_role_switch_group = {
278 .is_visible = usb_role_switch_is_visible,
279 .attrs = usb_role_switch_attrs,
282 static const struct attribute_group *usb_role_switch_groups[] = {
283 &usb_role_switch_group,
287 static int usb_role_switch_uevent(const struct device *dev, struct kobj_uevent_env *env)
291 ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
293 dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
298 static void usb_role_switch_release(struct device *dev)
300 struct usb_role_switch *sw = to_role_switch(dev);
305 static const struct device_type usb_role_dev_type = {
306 .name = "usb_role_switch",
307 .groups = usb_role_switch_groups,
308 .uevent = usb_role_switch_uevent,
309 .release = usb_role_switch_release,
313 * usb_role_switch_register - Register USB Role Switch
314 * @parent: Parent device for the switch
315 * @desc: Description of the switch
317 * USB Role Switch is a device capable or choosing the role for USB connector.
318 * On platforms where the USB controller is dual-role capable, the controller
319 * driver will need to register the switch. On platforms where the USB host and
320 * USB device controllers behind the connector are separate, there will be a
321 * mux, and the driver for that mux will need to register the switch.
323 * Returns handle to a new role switch or ERR_PTR. The content of @desc is
326 struct usb_role_switch *
327 usb_role_switch_register(struct device *parent,
328 const struct usb_role_switch_desc *desc)
330 struct usb_role_switch *sw;
333 if (!desc || !desc->set)
334 return ERR_PTR(-EINVAL);
336 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
338 return ERR_PTR(-ENOMEM);
340 mutex_init(&sw->lock);
342 sw->allow_userspace_control = desc->allow_userspace_control;
343 sw->usb2_port = desc->usb2_port;
344 sw->usb3_port = desc->usb3_port;
349 sw->module = parent->driver->owner;
350 sw->dev.parent = parent;
351 sw->dev.fwnode = desc->fwnode;
352 sw->dev.class = &role_class;
353 sw->dev.type = &usb_role_dev_type;
354 dev_set_drvdata(&sw->dev, desc->driver_data);
355 dev_set_name(&sw->dev, "%s-role-switch",
356 desc->name ? desc->name : dev_name(parent));
358 ret = device_register(&sw->dev);
360 put_device(&sw->dev);
364 sw->registered = true;
366 /* TODO: Symlinks for the host port and the device controller. */
370 EXPORT_SYMBOL_GPL(usb_role_switch_register);
373 * usb_role_switch_unregister - Unregsiter USB Role Switch
374 * @sw: USB Role Switch
376 * Unregister switch that was registered with usb_role_switch_register().
378 void usb_role_switch_unregister(struct usb_role_switch *sw)
380 if (!IS_ERR_OR_NULL(sw)) {
381 sw->registered = false;
382 device_unregister(&sw->dev);
385 EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
388 * usb_role_switch_set_drvdata - Assign private data pointer to a switch
389 * @sw: USB Role Switch
390 * @data: Private data pointer
392 void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
394 dev_set_drvdata(&sw->dev, data);
396 EXPORT_SYMBOL_GPL(usb_role_switch_set_drvdata);
399 * usb_role_switch_get_drvdata - Get the private data pointer of a switch
400 * @sw: USB Role Switch
402 void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
404 return dev_get_drvdata(&sw->dev);
406 EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);
408 static int __init usb_roles_init(void)
410 return class_register(&role_class);
412 subsys_initcall(usb_roles_init);
414 static void __exit usb_roles_exit(void)
416 class_unregister(&role_class);
418 module_exit(usb_roles_exit);
420 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
421 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
422 MODULE_LICENSE("GPL v2");
423 MODULE_DESCRIPTION("USB Role Class");