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 struct class *role_class
;
19 struct usb_role_switch
{
21 struct mutex lock
; /* device lock*/
25 struct device
*usb2_port
;
26 struct device
*usb3_port
;
28 usb_role_switch_set_t set
;
29 usb_role_switch_get_t get
;
30 bool allow_userspace_control
;
33 #define to_role_switch(d) container_of(d, struct usb_role_switch, dev)
36 * usb_role_switch_set_role - Set USB role for a switch
37 * @sw: USB role switch
38 * @role: USB role to be switched to
40 * Set USB role @role for @sw.
42 int usb_role_switch_set_role(struct usb_role_switch
*sw
, enum usb_role role
)
46 if (IS_ERR_OR_NULL(sw
))
49 mutex_lock(&sw
->lock
);
51 ret
= sw
->set(sw
, role
);
54 kobject_uevent(&sw
->dev
.kobj
, KOBJ_CHANGE
);
57 mutex_unlock(&sw
->lock
);
61 EXPORT_SYMBOL_GPL(usb_role_switch_set_role
);
64 * usb_role_switch_get_role - Get the USB role for a switch
65 * @sw: USB role switch
67 * Depending on the role-switch-driver this function returns either a cached
68 * value of the last set role, or reads back the actual value from the hardware.
70 enum usb_role
usb_role_switch_get_role(struct usb_role_switch
*sw
)
74 if (IS_ERR_OR_NULL(sw
))
77 mutex_lock(&sw
->lock
);
84 mutex_unlock(&sw
->lock
);
88 EXPORT_SYMBOL_GPL(usb_role_switch_get_role
);
90 static void *usb_role_switch_match(struct fwnode_handle
*fwnode
, const char *id
,
95 if (id
&& !fwnode_property_present(fwnode
, id
))
98 dev
= class_find_device_by_fwnode(role_class
, fwnode
);
100 return dev
? to_role_switch(dev
) : ERR_PTR(-EPROBE_DEFER
);
103 static struct usb_role_switch
*
104 usb_role_switch_is_parent(struct fwnode_handle
*fwnode
)
106 struct fwnode_handle
*parent
= fwnode_get_parent(fwnode
);
109 if (!parent
|| !fwnode_property_present(parent
, "usb-role-switch"))
112 dev
= class_find_device_by_fwnode(role_class
, parent
);
113 return dev
? to_role_switch(dev
) : ERR_PTR(-EPROBE_DEFER
);
117 * usb_role_switch_get - Find USB role switch linked with the caller
118 * @dev: The caller device
120 * Finds and returns role switch linked with @dev. The reference count for the
121 * found switch is incremented.
123 struct usb_role_switch
*usb_role_switch_get(struct device
*dev
)
125 struct usb_role_switch
*sw
;
127 sw
= usb_role_switch_is_parent(dev_fwnode(dev
));
129 sw
= device_connection_find_match(dev
, "usb-role-switch", NULL
,
130 usb_role_switch_match
);
132 if (!IS_ERR_OR_NULL(sw
))
133 WARN_ON(!try_module_get(sw
->dev
.parent
->driver
->owner
));
137 EXPORT_SYMBOL_GPL(usb_role_switch_get
);
140 * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
141 * @fwnode: The caller device node
143 * This is similar to the usb_role_switch_get() function above, but it searches
144 * the switch using fwnode instead of device entry.
146 struct usb_role_switch
*fwnode_usb_role_switch_get(struct fwnode_handle
*fwnode
)
148 struct usb_role_switch
*sw
;
150 sw
= usb_role_switch_is_parent(fwnode
);
152 sw
= fwnode_connection_find_match(fwnode
, "usb-role-switch",
153 NULL
, usb_role_switch_match
);
154 if (!IS_ERR_OR_NULL(sw
))
155 WARN_ON(!try_module_get(sw
->dev
.parent
->driver
->owner
));
159 EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get
);
162 * usb_role_switch_put - Release handle to a switch
163 * @sw: USB Role Switch
165 * Decrement reference count for @sw.
167 void usb_role_switch_put(struct usb_role_switch
*sw
)
169 if (!IS_ERR_OR_NULL(sw
)) {
170 module_put(sw
->dev
.parent
->driver
->owner
);
171 put_device(&sw
->dev
);
174 EXPORT_SYMBOL_GPL(usb_role_switch_put
);
177 * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
178 * @fwnode: fwnode of the USB Role Switch
180 * Finds and returns role switch with @fwnode. The reference count for the
181 * found switch is incremented.
183 struct usb_role_switch
*
184 usb_role_switch_find_by_fwnode(const struct fwnode_handle
*fwnode
)
191 dev
= class_find_device_by_fwnode(role_class
, fwnode
);
193 return dev
? to_role_switch(dev
) : NULL
;
195 EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode
);
198 usb_role_switch_is_visible(struct kobject
*kobj
, struct attribute
*attr
, int n
)
200 struct device
*dev
= kobj_to_dev(kobj
);
201 struct usb_role_switch
*sw
= to_role_switch(dev
);
203 if (sw
->allow_userspace_control
)
209 static const char * const usb_roles
[] = {
210 [USB_ROLE_NONE
] = "none",
211 [USB_ROLE_HOST
] = "host",
212 [USB_ROLE_DEVICE
] = "device",
216 role_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
218 struct usb_role_switch
*sw
= to_role_switch(dev
);
219 enum usb_role role
= usb_role_switch_get_role(sw
);
221 return sprintf(buf
, "%s\n", usb_roles
[role
]);
224 static ssize_t
role_store(struct device
*dev
, struct device_attribute
*attr
,
225 const char *buf
, size_t size
)
227 struct usb_role_switch
*sw
= to_role_switch(dev
);
230 ret
= sysfs_match_string(usb_roles
, buf
);
234 /* Extra check if the user wants to disable the switch */
235 ret
= kstrtobool(buf
, &res
);
240 ret
= usb_role_switch_set_role(sw
, ret
);
246 static DEVICE_ATTR_RW(role
);
248 static struct attribute
*usb_role_switch_attrs
[] = {
253 static const struct attribute_group usb_role_switch_group
= {
254 .is_visible
= usb_role_switch_is_visible
,
255 .attrs
= usb_role_switch_attrs
,
258 static const struct attribute_group
*usb_role_switch_groups
[] = {
259 &usb_role_switch_group
,
264 usb_role_switch_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
268 ret
= add_uevent_var(env
, "USB_ROLE_SWITCH=%s", dev_name(dev
));
270 dev_err(dev
, "failed to add uevent USB_ROLE_SWITCH\n");
275 static void usb_role_switch_release(struct device
*dev
)
277 struct usb_role_switch
*sw
= to_role_switch(dev
);
282 static const struct device_type usb_role_dev_type
= {
283 .name
= "usb_role_switch",
284 .groups
= usb_role_switch_groups
,
285 .uevent
= usb_role_switch_uevent
,
286 .release
= usb_role_switch_release
,
290 * usb_role_switch_register - Register USB Role Switch
291 * @parent: Parent device for the switch
292 * @desc: Description of the switch
294 * USB Role Switch is a device capable or choosing the role for USB connector.
295 * On platforms where the USB controller is dual-role capable, the controller
296 * driver will need to register the switch. On platforms where the USB host and
297 * USB device controllers behind the connector are separate, there will be a
298 * mux, and the driver for that mux will need to register the switch.
300 * Returns handle to a new role switch or ERR_PTR. The content of @desc is
303 struct usb_role_switch
*
304 usb_role_switch_register(struct device
*parent
,
305 const struct usb_role_switch_desc
*desc
)
307 struct usb_role_switch
*sw
;
310 if (!desc
|| !desc
->set
)
311 return ERR_PTR(-EINVAL
);
313 sw
= kzalloc(sizeof(*sw
), GFP_KERNEL
);
315 return ERR_PTR(-ENOMEM
);
317 mutex_init(&sw
->lock
);
319 sw
->allow_userspace_control
= desc
->allow_userspace_control
;
320 sw
->usb2_port
= desc
->usb2_port
;
321 sw
->usb3_port
= desc
->usb3_port
;
326 sw
->dev
.parent
= parent
;
327 sw
->dev
.fwnode
= desc
->fwnode
;
328 sw
->dev
.class = role_class
;
329 sw
->dev
.type
= &usb_role_dev_type
;
330 dev_set_drvdata(&sw
->dev
, desc
->driver_data
);
331 dev_set_name(&sw
->dev
, "%s-role-switch",
332 desc
->name
? desc
->name
: dev_name(parent
));
334 ret
= device_register(&sw
->dev
);
336 put_device(&sw
->dev
);
340 /* TODO: Symlinks for the host port and the device controller. */
344 EXPORT_SYMBOL_GPL(usb_role_switch_register
);
347 * usb_role_switch_unregister - Unregsiter USB Role Switch
348 * @sw: USB Role Switch
350 * Unregister switch that was registered with usb_role_switch_register().
352 void usb_role_switch_unregister(struct usb_role_switch
*sw
)
354 if (!IS_ERR_OR_NULL(sw
))
355 device_unregister(&sw
->dev
);
357 EXPORT_SYMBOL_GPL(usb_role_switch_unregister
);
360 * usb_role_switch_set_drvdata - Assign private data pointer to a switch
361 * @sw: USB Role Switch
362 * @data: Private data pointer
364 void usb_role_switch_set_drvdata(struct usb_role_switch
*sw
, void *data
)
366 dev_set_drvdata(&sw
->dev
, data
);
368 EXPORT_SYMBOL_GPL(usb_role_switch_set_drvdata
);
371 * usb_role_switch_get_drvdata - Get the private data pointer of a switch
372 * @sw: USB Role Switch
374 void *usb_role_switch_get_drvdata(struct usb_role_switch
*sw
)
376 return dev_get_drvdata(&sw
->dev
);
378 EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata
);
380 static int __init
usb_roles_init(void)
382 role_class
= class_create(THIS_MODULE
, "usb_role");
383 return PTR_ERR_OR_ZERO(role_class
);
385 subsys_initcall(usb_roles_init
);
387 static void __exit
usb_roles_exit(void)
389 class_destroy(role_class
);
391 module_exit(usb_roles_exit
);
393 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
394 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
395 MODULE_LICENSE("GPL v2");
396 MODULE_DESCRIPTION("USB Role Class");