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/device.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/slab.h>
16 static struct class *role_class;
18 struct usb_role_switch {
20 struct mutex lock; /* device lock*/
24 struct device *usb2_port;
25 struct device *usb3_port;
27 usb_role_switch_set_t set;
28 usb_role_switch_get_t get;
29 bool allow_userspace_control;
32 #define to_role_switch(d) container_of(d, struct usb_role_switch, dev)
35 * usb_role_switch_set_role - Set USB role for a switch
36 * @sw: USB role switch
37 * @role: USB role to be switched to
39 * Set USB role @role for @sw.
41 int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
45 if (IS_ERR_OR_NULL(sw))
48 mutex_lock(&sw->lock);
50 ret = sw->set(sw->dev.parent, role);
54 mutex_unlock(&sw->lock);
58 EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
61 * usb_role_switch_get_role - Get the USB role for a switch
62 * @sw: USB role switch
64 * Depending on the role-switch-driver this function returns either a cached
65 * value of the last set role, or reads back the actual value from the hardware.
67 enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
71 if (IS_ERR_OR_NULL(sw))
74 mutex_lock(&sw->lock);
77 role = sw->get(sw->dev.parent);
81 mutex_unlock(&sw->lock);
85 EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
87 static int __switch_match(struct device *dev, const void *name)
89 return !strcmp((const char *)name, dev_name(dev));
92 static void *usb_role_switch_match(struct device_connection *con, int ep,
97 dev = class_find_device(role_class, NULL, con->endpoint[ep],
100 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
104 * usb_role_switch_get - Find USB role switch linked with the caller
105 * @dev: The caller device
107 * Finds and returns role switch linked with @dev. The reference count for the
108 * found switch is incremented.
110 struct usb_role_switch *usb_role_switch_get(struct device *dev)
112 return device_connection_find_match(dev, "usb-role-switch", NULL,
113 usb_role_switch_match);
115 EXPORT_SYMBOL_GPL(usb_role_switch_get);
118 * usb_role_switch_put - Release handle to a switch
119 * @sw: USB Role Switch
121 * Decrement reference count for @sw.
123 void usb_role_switch_put(struct usb_role_switch *sw)
125 if (!IS_ERR_OR_NULL(sw))
126 put_device(&sw->dev);
128 EXPORT_SYMBOL_GPL(usb_role_switch_put);
131 usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
133 struct device *dev = container_of(kobj, typeof(*dev), kobj);
134 struct usb_role_switch *sw = to_role_switch(dev);
136 if (sw->allow_userspace_control)
142 static const char * const usb_roles[] = {
143 [USB_ROLE_NONE] = "none",
144 [USB_ROLE_HOST] = "host",
145 [USB_ROLE_DEVICE] = "device",
149 role_show(struct device *dev, struct device_attribute *attr, char *buf)
151 struct usb_role_switch *sw = to_role_switch(dev);
152 enum usb_role role = usb_role_switch_get_role(sw);
154 return sprintf(buf, "%s\n", usb_roles[role]);
157 static ssize_t role_store(struct device *dev, struct device_attribute *attr,
158 const char *buf, size_t size)
160 struct usb_role_switch *sw = to_role_switch(dev);
163 ret = sysfs_match_string(usb_roles, buf);
167 /* Extra check if the user wants to disable the switch */
168 ret = kstrtobool(buf, &res);
173 ret = usb_role_switch_set_role(sw, ret);
179 static DEVICE_ATTR_RW(role);
181 static struct attribute *usb_role_switch_attrs[] = {
186 static const struct attribute_group usb_role_switch_group = {
187 .is_visible = usb_role_switch_is_visible,
188 .attrs = usb_role_switch_attrs,
191 static const struct attribute_group *usb_role_switch_groups[] = {
192 &usb_role_switch_group,
197 usb_role_switch_uevent(struct device *dev, struct kobj_uevent_env *env)
201 ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
203 dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
208 static void usb_role_switch_release(struct device *dev)
210 struct usb_role_switch *sw = to_role_switch(dev);
215 static const struct device_type usb_role_dev_type = {
216 .name = "usb_role_switch",
217 .groups = usb_role_switch_groups,
218 .uevent = usb_role_switch_uevent,
219 .release = usb_role_switch_release,
223 * usb_role_switch_register - Register USB Role Switch
224 * @parent: Parent device for the switch
225 * @desc: Description of the switch
227 * USB Role Switch is a device capable or choosing the role for USB connector.
228 * On platforms where the USB controller is dual-role capable, the controller
229 * driver will need to register the switch. On platforms where the USB host and
230 * USB device controllers behind the connector are separate, there will be a
231 * mux, and the driver for that mux will need to register the switch.
233 * Returns handle to a new role switch or ERR_PTR. The content of @desc is
236 struct usb_role_switch *
237 usb_role_switch_register(struct device *parent,
238 const struct usb_role_switch_desc *desc)
240 struct usb_role_switch *sw;
243 if (!desc || !desc->set)
244 return ERR_PTR(-EINVAL);
246 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
248 return ERR_PTR(-ENOMEM);
250 mutex_init(&sw->lock);
252 sw->allow_userspace_control = desc->allow_userspace_control;
253 sw->usb2_port = desc->usb2_port;
254 sw->usb3_port = desc->usb3_port;
259 sw->dev.parent = parent;
260 sw->dev.class = role_class;
261 sw->dev.type = &usb_role_dev_type;
262 dev_set_name(&sw->dev, "%s-role-switch", dev_name(parent));
264 ret = device_register(&sw->dev);
266 put_device(&sw->dev);
270 /* TODO: Symlinks for the host port and the device controller. */
274 EXPORT_SYMBOL_GPL(usb_role_switch_register);
277 * usb_role_switch_unregister - Unregsiter USB Role Switch
278 * @sw: USB Role Switch
280 * Unregister switch that was registered with usb_role_switch_register().
282 void usb_role_switch_unregister(struct usb_role_switch *sw)
284 if (!IS_ERR_OR_NULL(sw))
285 device_unregister(&sw->dev);
287 EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
289 static int __init usb_roles_init(void)
291 role_class = class_create(THIS_MODULE, "usb_role");
292 return PTR_ERR_OR_ZERO(role_class);
294 subsys_initcall(usb_roles_init);
296 static void __exit usb_roles_exit(void)
298 class_destroy(role_class);
300 module_exit(usb_roles_exit);
304 MODULE_LICENSE("GPL v2");
305 MODULE_DESCRIPTION("USB Role Class");