1 // SPDX-License-Identifier: GPL-2.0
3 * USB Role Switch Support
5 * Copyright (C) 2018 Intel Corporation
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*/
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))
51 mutex_lock(&sw->lock);
53 ret = sw->set(sw, role);
56 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
59 mutex_unlock(&sw->lock);
63 EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
66 * usb_role_switch_get_role - Get the USB role for a switch
67 * @sw: USB role switch
69 * Depending on the role-switch-driver this function returns either a cached
70 * value of the last set role, or reads back the actual value from the hardware.
72 enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
76 if (IS_ERR_OR_NULL(sw))
79 mutex_lock(&sw->lock);
86 mutex_unlock(&sw->lock);
90 EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
92 static void *usb_role_switch_match(const struct fwnode_handle *fwnode, const char *id,
97 if (id && !fwnode_property_present(fwnode, id))
100 dev = class_find_device_by_fwnode(&role_class, fwnode);
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 (!fwnode_property_present(parent, "usb-role-switch")) {
112 fwnode_handle_put(parent);
116 dev = class_find_device_by_fwnode(&role_class, parent);
117 fwnode_handle_put(parent);
118 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
122 * usb_role_switch_get - Find USB role switch linked with the caller
123 * @dev: The caller device
125 * Finds and returns role switch linked with @dev. The reference count for the
126 * found switch is incremented.
128 struct usb_role_switch *usb_role_switch_get(struct device *dev)
130 struct usb_role_switch *sw;
132 sw = usb_role_switch_is_parent(dev_fwnode(dev));
134 sw = device_connection_find_match(dev, "usb-role-switch", NULL,
135 usb_role_switch_match);
137 if (!IS_ERR_OR_NULL(sw))
138 WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
142 EXPORT_SYMBOL_GPL(usb_role_switch_get);
145 * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
146 * @fwnode: The caller device node
148 * This is similar to the usb_role_switch_get() function above, but it searches
149 * the switch using fwnode instead of device entry.
151 struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
153 struct usb_role_switch *sw;
155 sw = usb_role_switch_is_parent(fwnode);
157 sw = fwnode_connection_find_match(fwnode, "usb-role-switch",
158 NULL, usb_role_switch_match);
159 if (!IS_ERR_OR_NULL(sw))
160 WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
164 EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
167 * usb_role_switch_put - Release handle to a switch
168 * @sw: USB Role Switch
170 * Decrement reference count for @sw.
172 void usb_role_switch_put(struct usb_role_switch *sw)
174 if (!IS_ERR_OR_NULL(sw)) {
175 module_put(sw->dev.parent->driver->owner);
176 put_device(&sw->dev);
179 EXPORT_SYMBOL_GPL(usb_role_switch_put);
182 * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
183 * @fwnode: fwnode of the USB Role Switch
185 * Finds and returns role switch with @fwnode. The reference count for the
186 * found switch is incremented.
188 struct usb_role_switch *
189 usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
196 dev = class_find_device_by_fwnode(&role_class, fwnode);
198 WARN_ON(!try_module_get(dev->parent->driver->owner));
200 return dev ? to_role_switch(dev) : NULL;
202 EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode);
205 usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
207 struct device *dev = kobj_to_dev(kobj);
208 struct usb_role_switch *sw = to_role_switch(dev);
210 if (sw->allow_userspace_control)
216 static const char * const usb_roles[] = {
217 [USB_ROLE_NONE] = "none",
218 [USB_ROLE_HOST] = "host",
219 [USB_ROLE_DEVICE] = "device",
222 const char *usb_role_string(enum usb_role role)
224 if (role < 0 || role >= ARRAY_SIZE(usb_roles))
227 return usb_roles[role];
229 EXPORT_SYMBOL_GPL(usb_role_string);
232 role_show(struct device *dev, struct device_attribute *attr, char *buf)
234 struct usb_role_switch *sw = to_role_switch(dev);
235 enum usb_role role = usb_role_switch_get_role(sw);
237 return sprintf(buf, "%s\n", usb_roles[role]);
240 static ssize_t role_store(struct device *dev, struct device_attribute *attr,
241 const char *buf, size_t size)
243 struct usb_role_switch *sw = to_role_switch(dev);
246 ret = sysfs_match_string(usb_roles, buf);
250 /* Extra check if the user wants to disable the switch */
251 ret = kstrtobool(buf, &res);
256 ret = usb_role_switch_set_role(sw, ret);
262 static DEVICE_ATTR_RW(role);
264 static struct attribute *usb_role_switch_attrs[] = {
269 static const struct attribute_group usb_role_switch_group = {
270 .is_visible = usb_role_switch_is_visible,
271 .attrs = usb_role_switch_attrs,
274 static const struct attribute_group *usb_role_switch_groups[] = {
275 &usb_role_switch_group,
279 static int usb_role_switch_uevent(const struct device *dev, struct kobj_uevent_env *env)
283 ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
285 dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
290 static void usb_role_switch_release(struct device *dev)
292 struct usb_role_switch *sw = to_role_switch(dev);
297 static const struct device_type usb_role_dev_type = {
298 .name = "usb_role_switch",
299 .groups = usb_role_switch_groups,
300 .uevent = usb_role_switch_uevent,
301 .release = usb_role_switch_release,
305 * usb_role_switch_register - Register USB Role Switch
306 * @parent: Parent device for the switch
307 * @desc: Description of the switch
309 * USB Role Switch is a device capable or choosing the role for USB connector.
310 * On platforms where the USB controller is dual-role capable, the controller
311 * driver will need to register the switch. On platforms where the USB host and
312 * USB device controllers behind the connector are separate, there will be a
313 * mux, and the driver for that mux will need to register the switch.
315 * Returns handle to a new role switch or ERR_PTR. The content of @desc is
318 struct usb_role_switch *
319 usb_role_switch_register(struct device *parent,
320 const struct usb_role_switch_desc *desc)
322 struct usb_role_switch *sw;
325 if (!desc || !desc->set)
326 return ERR_PTR(-EINVAL);
328 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
330 return ERR_PTR(-ENOMEM);
332 mutex_init(&sw->lock);
334 sw->allow_userspace_control = desc->allow_userspace_control;
335 sw->usb2_port = desc->usb2_port;
336 sw->usb3_port = desc->usb3_port;
341 sw->dev.parent = parent;
342 sw->dev.fwnode = desc->fwnode;
343 sw->dev.class = &role_class;
344 sw->dev.type = &usb_role_dev_type;
345 dev_set_drvdata(&sw->dev, desc->driver_data);
346 dev_set_name(&sw->dev, "%s-role-switch",
347 desc->name ? desc->name : dev_name(parent));
349 ret = device_register(&sw->dev);
351 put_device(&sw->dev);
355 /* TODO: Symlinks for the host port and the device controller. */
359 EXPORT_SYMBOL_GPL(usb_role_switch_register);
362 * usb_role_switch_unregister - Unregsiter USB Role Switch
363 * @sw: USB Role Switch
365 * Unregister switch that was registered with usb_role_switch_register().
367 void usb_role_switch_unregister(struct usb_role_switch *sw)
369 if (!IS_ERR_OR_NULL(sw))
370 device_unregister(&sw->dev);
372 EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
375 * usb_role_switch_set_drvdata - Assign private data pointer to a switch
376 * @sw: USB Role Switch
377 * @data: Private data pointer
379 void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
381 dev_set_drvdata(&sw->dev, data);
383 EXPORT_SYMBOL_GPL(usb_role_switch_set_drvdata);
386 * usb_role_switch_get_drvdata - Get the private data pointer of a switch
387 * @sw: USB Role Switch
389 void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
391 return dev_get_drvdata(&sw->dev);
393 EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);
395 static int __init usb_roles_init(void)
397 return class_register(&role_class);
399 subsys_initcall(usb_roles_init);
401 static void __exit usb_roles_exit(void)
403 class_unregister(&role_class);
405 module_exit(usb_roles_exit);
409 MODULE_LICENSE("GPL v2");
410 MODULE_DESCRIPTION("USB Role Class");