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/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 struct usb_role_switch
*sw
;
114 sw
= device_connection_find_match(dev
, "usb-role-switch", NULL
,
115 usb_role_switch_match
);
117 if (!IS_ERR_OR_NULL(sw
))
118 WARN_ON(!try_module_get(sw
->dev
.parent
->driver
->owner
));
122 EXPORT_SYMBOL_GPL(usb_role_switch_get
);
125 * usb_role_switch_put - Release handle to a switch
126 * @sw: USB Role Switch
128 * Decrement reference count for @sw.
130 void usb_role_switch_put(struct usb_role_switch
*sw
)
132 if (!IS_ERR_OR_NULL(sw
)) {
133 module_put(sw
->dev
.parent
->driver
->owner
);
134 put_device(&sw
->dev
);
137 EXPORT_SYMBOL_GPL(usb_role_switch_put
);
140 usb_role_switch_is_visible(struct kobject
*kobj
, struct attribute
*attr
, int n
)
142 struct device
*dev
= container_of(kobj
, typeof(*dev
), kobj
);
143 struct usb_role_switch
*sw
= to_role_switch(dev
);
145 if (sw
->allow_userspace_control
)
151 static const char * const usb_roles
[] = {
152 [USB_ROLE_NONE
] = "none",
153 [USB_ROLE_HOST
] = "host",
154 [USB_ROLE_DEVICE
] = "device",
158 role_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
160 struct usb_role_switch
*sw
= to_role_switch(dev
);
161 enum usb_role role
= usb_role_switch_get_role(sw
);
163 return sprintf(buf
, "%s\n", usb_roles
[role
]);
166 static ssize_t
role_store(struct device
*dev
, struct device_attribute
*attr
,
167 const char *buf
, size_t size
)
169 struct usb_role_switch
*sw
= to_role_switch(dev
);
172 ret
= sysfs_match_string(usb_roles
, buf
);
176 /* Extra check if the user wants to disable the switch */
177 ret
= kstrtobool(buf
, &res
);
182 ret
= usb_role_switch_set_role(sw
, ret
);
188 static DEVICE_ATTR_RW(role
);
190 static struct attribute
*usb_role_switch_attrs
[] = {
195 static const struct attribute_group usb_role_switch_group
= {
196 .is_visible
= usb_role_switch_is_visible
,
197 .attrs
= usb_role_switch_attrs
,
200 static const struct attribute_group
*usb_role_switch_groups
[] = {
201 &usb_role_switch_group
,
206 usb_role_switch_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
210 ret
= add_uevent_var(env
, "USB_ROLE_SWITCH=%s", dev_name(dev
));
212 dev_err(dev
, "failed to add uevent USB_ROLE_SWITCH\n");
217 static void usb_role_switch_release(struct device
*dev
)
219 struct usb_role_switch
*sw
= to_role_switch(dev
);
224 static const struct device_type usb_role_dev_type
= {
225 .name
= "usb_role_switch",
226 .groups
= usb_role_switch_groups
,
227 .uevent
= usb_role_switch_uevent
,
228 .release
= usb_role_switch_release
,
232 * usb_role_switch_register - Register USB Role Switch
233 * @parent: Parent device for the switch
234 * @desc: Description of the switch
236 * USB Role Switch is a device capable or choosing the role for USB connector.
237 * On platforms where the USB controller is dual-role capable, the controller
238 * driver will need to register the switch. On platforms where the USB host and
239 * USB device controllers behind the connector are separate, there will be a
240 * mux, and the driver for that mux will need to register the switch.
242 * Returns handle to a new role switch or ERR_PTR. The content of @desc is
245 struct usb_role_switch
*
246 usb_role_switch_register(struct device
*parent
,
247 const struct usb_role_switch_desc
*desc
)
249 struct usb_role_switch
*sw
;
252 if (!desc
|| !desc
->set
)
253 return ERR_PTR(-EINVAL
);
255 sw
= kzalloc(sizeof(*sw
), GFP_KERNEL
);
257 return ERR_PTR(-ENOMEM
);
259 mutex_init(&sw
->lock
);
261 sw
->allow_userspace_control
= desc
->allow_userspace_control
;
262 sw
->usb2_port
= desc
->usb2_port
;
263 sw
->usb3_port
= desc
->usb3_port
;
268 sw
->dev
.parent
= parent
;
269 sw
->dev
.class = role_class
;
270 sw
->dev
.type
= &usb_role_dev_type
;
271 dev_set_name(&sw
->dev
, "%s-role-switch", dev_name(parent
));
273 ret
= device_register(&sw
->dev
);
275 put_device(&sw
->dev
);
279 /* TODO: Symlinks for the host port and the device controller. */
283 EXPORT_SYMBOL_GPL(usb_role_switch_register
);
286 * usb_role_switch_unregister - Unregsiter USB Role Switch
287 * @sw: USB Role Switch
289 * Unregister switch that was registered with usb_role_switch_register().
291 void usb_role_switch_unregister(struct usb_role_switch
*sw
)
293 if (!IS_ERR_OR_NULL(sw
))
294 device_unregister(&sw
->dev
);
296 EXPORT_SYMBOL_GPL(usb_role_switch_unregister
);
298 static int __init
usb_roles_init(void)
300 role_class
= class_create(THIS_MODULE
, "usb_role");
301 return PTR_ERR_OR_ZERO(role_class
);
303 subsys_initcall(usb_roles_init
);
305 static void __exit
usb_roles_exit(void)
307 class_destroy(role_class
);
309 module_exit(usb_roles_exit
);
311 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
312 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
313 MODULE_LICENSE("GPL v2");
314 MODULE_DESCRIPTION("USB Role Class");