1 // SPDX-License-Identifier: GPL-2.0
3 * USB Role Switch Support
5 * Copyright (C) 2018 Intel Corporation
10 #include <linux/component.h>
11 #include <linux/usb/role.h>
12 #include <linux/property.h>
13 #include <linux/device.h>
14 #include <linux/lockdep.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
19 static const struct class role_class = {
23 struct usb_role_switch {
25 struct lock_class_key key;
26 struct mutex lock; /* device lock*/
27 struct module *module; /* the module this device depends on */
32 struct device *usb2_port;
33 struct device *usb3_port;
35 usb_role_switch_set_t set;
36 usb_role_switch_get_t get;
37 bool allow_userspace_control;
40 #define to_role_switch(d) container_of(d, struct usb_role_switch, dev)
42 static int connector_bind(struct device *dev, struct device *connector, void *data)
46 ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector");
50 ret = sysfs_create_link(&connector->kobj, &dev->kobj, "usb-role-switch");
52 sysfs_remove_link(&dev->kobj, "connector");
57 static void connector_unbind(struct device *dev, struct device *connector, void *data)
59 sysfs_remove_link(&connector->kobj, "usb-role-switch");
60 sysfs_remove_link(&dev->kobj, "connector");
63 static const struct component_ops connector_ops = {
64 .bind = connector_bind,
65 .unbind = connector_unbind,
69 * usb_role_switch_set_role - Set USB role for a switch
70 * @sw: USB role switch
71 * @role: USB role to be switched to
73 * Set USB role @role for @sw.
75 int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
79 if (IS_ERR_OR_NULL(sw))
85 mutex_lock(&sw->lock);
87 ret = sw->set(sw, role);
90 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
93 mutex_unlock(&sw->lock);
97 EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
100 * usb_role_switch_get_role - Get the USB role for a switch
101 * @sw: USB role switch
103 * Depending on the role-switch-driver this function returns either a cached
104 * value of the last set role, or reads back the actual value from the hardware.
106 enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
110 if (IS_ERR_OR_NULL(sw) || !sw->registered)
111 return USB_ROLE_NONE;
113 mutex_lock(&sw->lock);
120 mutex_unlock(&sw->lock);
124 EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
126 static void *usb_role_switch_match(const struct fwnode_handle *fwnode, const char *id,
131 if (id && !fwnode_property_present(fwnode, id))
134 dev = class_find_device_by_fwnode(&role_class, fwnode);
136 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
139 static struct usb_role_switch *
140 usb_role_switch_is_parent(struct fwnode_handle *fwnode)
142 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
145 if (!fwnode_property_present(parent, "usb-role-switch")) {
146 fwnode_handle_put(parent);
150 dev = class_find_device_by_fwnode(&role_class, parent);
151 fwnode_handle_put(parent);
152 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
156 * usb_role_switch_get - Find USB role switch linked with the caller
157 * @dev: The caller device
159 * Finds and returns role switch linked with @dev. The reference count for the
160 * found switch is incremented.
162 struct usb_role_switch *usb_role_switch_get(struct device *dev)
164 struct usb_role_switch *sw;
166 sw = usb_role_switch_is_parent(dev_fwnode(dev));
168 sw = device_connection_find_match(dev, "usb-role-switch", NULL,
169 usb_role_switch_match);
171 if (!IS_ERR_OR_NULL(sw))
172 WARN_ON(!try_module_get(sw->module));
176 EXPORT_SYMBOL_GPL(usb_role_switch_get);
179 * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
180 * @fwnode: The caller device node
182 * This is similar to the usb_role_switch_get() function above, but it searches
183 * the switch using fwnode instead of device entry.
185 struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
187 struct usb_role_switch *sw;
189 sw = usb_role_switch_is_parent(fwnode);
191 sw = fwnode_connection_find_match(fwnode, "usb-role-switch",
192 NULL, usb_role_switch_match);
193 if (!IS_ERR_OR_NULL(sw))
194 WARN_ON(!try_module_get(sw->module));
198 EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
201 * usb_role_switch_put - Release handle to a switch
202 * @sw: USB Role Switch
204 * Decrement reference count for @sw.
206 void usb_role_switch_put(struct usb_role_switch *sw)
208 if (!IS_ERR_OR_NULL(sw)) {
209 module_put(sw->module);
210 put_device(&sw->dev);
213 EXPORT_SYMBOL_GPL(usb_role_switch_put);
216 * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
217 * @fwnode: fwnode of the USB Role Switch
219 * Finds and returns role switch with @fwnode. The reference count for the
220 * found switch is incremented.
222 struct usb_role_switch *
223 usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
226 struct usb_role_switch *sw = NULL;
231 dev = class_find_device_by_fwnode(&role_class, fwnode);
233 sw = to_role_switch(dev);
234 WARN_ON(!try_module_get(sw->module));
239 EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode);
242 usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
244 struct device *dev = kobj_to_dev(kobj);
245 struct usb_role_switch *sw = to_role_switch(dev);
247 if (sw->allow_userspace_control)
253 static const char * const usb_roles[] = {
254 [USB_ROLE_NONE] = "none",
255 [USB_ROLE_HOST] = "host",
256 [USB_ROLE_DEVICE] = "device",
259 const char *usb_role_string(enum usb_role role)
261 if (role < 0 || role >= ARRAY_SIZE(usb_roles))
264 return usb_roles[role];
266 EXPORT_SYMBOL_GPL(usb_role_string);
269 role_show(struct device *dev, struct device_attribute *attr, char *buf)
271 struct usb_role_switch *sw = to_role_switch(dev);
272 enum usb_role role = usb_role_switch_get_role(sw);
274 return sprintf(buf, "%s\n", usb_roles[role]);
277 static ssize_t role_store(struct device *dev, struct device_attribute *attr,
278 const char *buf, size_t size)
280 struct usb_role_switch *sw = to_role_switch(dev);
283 ret = sysfs_match_string(usb_roles, buf);
287 /* Extra check if the user wants to disable the switch */
288 ret = kstrtobool(buf, &res);
293 ret = usb_role_switch_set_role(sw, ret);
299 static DEVICE_ATTR_RW(role);
301 static struct attribute *usb_role_switch_attrs[] = {
306 static const struct attribute_group usb_role_switch_group = {
307 .is_visible = usb_role_switch_is_visible,
308 .attrs = usb_role_switch_attrs,
311 static const struct attribute_group *usb_role_switch_groups[] = {
312 &usb_role_switch_group,
316 static int usb_role_switch_uevent(const struct device *dev, struct kobj_uevent_env *env)
320 ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
322 dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
327 static void usb_role_switch_release(struct device *dev)
329 struct usb_role_switch *sw = to_role_switch(dev);
331 mutex_destroy(&sw->lock);
332 lockdep_unregister_key(&sw->key);
336 static const struct device_type usb_role_dev_type = {
337 .name = "usb_role_switch",
338 .groups = usb_role_switch_groups,
339 .uevent = usb_role_switch_uevent,
340 .release = usb_role_switch_release,
344 * usb_role_switch_register - Register USB Role Switch
345 * @parent: Parent device for the switch
346 * @desc: Description of the switch
348 * USB Role Switch is a device capable or choosing the role for USB connector.
349 * On platforms where the USB controller is dual-role capable, the controller
350 * driver will need to register the switch. On platforms where the USB host and
351 * USB device controllers behind the connector are separate, there will be a
352 * mux, and the driver for that mux will need to register the switch.
354 * Returns handle to a new role switch or ERR_PTR. The content of @desc is
357 struct usb_role_switch *
358 usb_role_switch_register(struct device *parent,
359 const struct usb_role_switch_desc *desc)
361 struct usb_role_switch *sw;
364 if (!desc || !desc->set)
365 return ERR_PTR(-EINVAL);
367 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
369 return ERR_PTR(-ENOMEM);
371 lockdep_register_key(&sw->key);
372 mutex_init_with_key(&sw->lock, &sw->key);
374 sw->allow_userspace_control = desc->allow_userspace_control;
375 sw->usb2_port = desc->usb2_port;
376 sw->usb3_port = desc->usb3_port;
381 sw->module = parent->driver->owner;
382 sw->dev.parent = parent;
383 sw->dev.fwnode = desc->fwnode;
384 sw->dev.class = &role_class;
385 sw->dev.type = &usb_role_dev_type;
386 dev_set_drvdata(&sw->dev, desc->driver_data);
387 dev_set_name(&sw->dev, "%s-role-switch",
388 desc->name ? desc->name : dev_name(parent));
390 ret = device_register(&sw->dev);
392 put_device(&sw->dev);
396 if (dev_fwnode(&sw->dev)) {
397 ret = component_add(&sw->dev, &connector_ops);
399 dev_warn(&sw->dev, "failed to add component\n");
402 sw->registered = true;
404 /* TODO: Symlinks for the host port and the device controller. */
408 EXPORT_SYMBOL_GPL(usb_role_switch_register);
411 * usb_role_switch_unregister - Unregsiter USB Role Switch
412 * @sw: USB Role Switch
414 * Unregister switch that was registered with usb_role_switch_register().
416 void usb_role_switch_unregister(struct usb_role_switch *sw)
418 if (IS_ERR_OR_NULL(sw))
420 sw->registered = false;
421 if (dev_fwnode(&sw->dev))
422 component_del(&sw->dev, &connector_ops);
423 device_unregister(&sw->dev);
425 EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
428 * usb_role_switch_set_drvdata - Assign private data pointer to a switch
429 * @sw: USB Role Switch
430 * @data: Private data pointer
432 void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
434 dev_set_drvdata(&sw->dev, data);
436 EXPORT_SYMBOL_GPL(usb_role_switch_set_drvdata);
439 * usb_role_switch_get_drvdata - Get the private data pointer of a switch
440 * @sw: USB Role Switch
442 void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
444 return dev_get_drvdata(&sw->dev);
446 EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);
448 static int __init usb_roles_init(void)
450 return class_register(&role_class);
452 subsys_initcall(usb_roles_init);
454 static void __exit usb_roles_exit(void)
456 class_unregister(&role_class);
458 module_exit(usb_roles_exit);
462 MODULE_LICENSE("GPL v2");
463 MODULE_DESCRIPTION("USB Role Class");