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
->dev
.parent
, role
);
55 mutex_unlock(&sw
->lock
);
59 EXPORT_SYMBOL_GPL(usb_role_switch_set_role
);
62 * usb_role_switch_get_role - Get the USB role for a switch
63 * @sw: USB role switch
65 * Depending on the role-switch-driver this function returns either a cached
66 * value of the last set role, or reads back the actual value from the hardware.
68 enum usb_role
usb_role_switch_get_role(struct usb_role_switch
*sw
)
72 if (IS_ERR_OR_NULL(sw
))
75 mutex_lock(&sw
->lock
);
78 role
= sw
->get(sw
->dev
.parent
);
82 mutex_unlock(&sw
->lock
);
86 EXPORT_SYMBOL_GPL(usb_role_switch_get_role
);
88 static int switch_fwnode_match(struct device
*dev
, const void *fwnode
)
90 return dev_fwnode(dev
) == fwnode
;
93 static int switch_name_match(struct device
*dev
, const void *name
)
95 return !strcmp((const char *)name
, dev_name(dev
));
98 static void *usb_role_switch_match(struct device_connection
*con
, int ep
,
104 if (!fwnode_property_present(con
->fwnode
, con
->id
))
107 dev
= class_find_device(role_class
, NULL
, con
->fwnode
,
108 switch_fwnode_match
);
110 dev
= class_find_device(role_class
, NULL
, con
->endpoint
[ep
],
114 return dev
? to_role_switch(dev
) : ERR_PTR(-EPROBE_DEFER
);
118 * usb_role_switch_get - Find USB role switch linked with the caller
119 * @dev: The caller device
121 * Finds and returns role switch linked with @dev. The reference count for the
122 * found switch is incremented.
124 struct usb_role_switch
*usb_role_switch_get(struct device
*dev
)
126 struct usb_role_switch
*sw
;
128 sw
= device_connection_find_match(dev
, "usb-role-switch", NULL
,
129 usb_role_switch_match
);
131 if (!IS_ERR_OR_NULL(sw
))
132 WARN_ON(!try_module_get(sw
->dev
.parent
->driver
->owner
));
136 EXPORT_SYMBOL_GPL(usb_role_switch_get
);
139 * usb_role_switch_put - Release handle to a switch
140 * @sw: USB Role Switch
142 * Decrement reference count for @sw.
144 void usb_role_switch_put(struct usb_role_switch
*sw
)
146 if (!IS_ERR_OR_NULL(sw
)) {
147 put_device(&sw
->dev
);
148 module_put(sw
->dev
.parent
->driver
->owner
);
151 EXPORT_SYMBOL_GPL(usb_role_switch_put
);
154 usb_role_switch_is_visible(struct kobject
*kobj
, struct attribute
*attr
, int n
)
156 struct device
*dev
= container_of(kobj
, typeof(*dev
), kobj
);
157 struct usb_role_switch
*sw
= to_role_switch(dev
);
159 if (sw
->allow_userspace_control
)
165 static const char * const usb_roles
[] = {
166 [USB_ROLE_NONE
] = "none",
167 [USB_ROLE_HOST
] = "host",
168 [USB_ROLE_DEVICE
] = "device",
172 role_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
174 struct usb_role_switch
*sw
= to_role_switch(dev
);
175 enum usb_role role
= usb_role_switch_get_role(sw
);
177 return sprintf(buf
, "%s\n", usb_roles
[role
]);
180 static ssize_t
role_store(struct device
*dev
, struct device_attribute
*attr
,
181 const char *buf
, size_t size
)
183 struct usb_role_switch
*sw
= to_role_switch(dev
);
186 ret
= sysfs_match_string(usb_roles
, buf
);
190 /* Extra check if the user wants to disable the switch */
191 ret
= kstrtobool(buf
, &res
);
196 ret
= usb_role_switch_set_role(sw
, ret
);
202 static DEVICE_ATTR_RW(role
);
204 static struct attribute
*usb_role_switch_attrs
[] = {
209 static const struct attribute_group usb_role_switch_group
= {
210 .is_visible
= usb_role_switch_is_visible
,
211 .attrs
= usb_role_switch_attrs
,
214 static const struct attribute_group
*usb_role_switch_groups
[] = {
215 &usb_role_switch_group
,
220 usb_role_switch_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
224 ret
= add_uevent_var(env
, "USB_ROLE_SWITCH=%s", dev_name(dev
));
226 dev_err(dev
, "failed to add uevent USB_ROLE_SWITCH\n");
231 static void usb_role_switch_release(struct device
*dev
)
233 struct usb_role_switch
*sw
= to_role_switch(dev
);
238 static const struct device_type usb_role_dev_type
= {
239 .name
= "usb_role_switch",
240 .groups
= usb_role_switch_groups
,
241 .uevent
= usb_role_switch_uevent
,
242 .release
= usb_role_switch_release
,
246 * usb_role_switch_register - Register USB Role Switch
247 * @parent: Parent device for the switch
248 * @desc: Description of the switch
250 * USB Role Switch is a device capable or choosing the role for USB connector.
251 * On platforms where the USB controller is dual-role capable, the controller
252 * driver will need to register the switch. On platforms where the USB host and
253 * USB device controllers behind the connector are separate, there will be a
254 * mux, and the driver for that mux will need to register the switch.
256 * Returns handle to a new role switch or ERR_PTR. The content of @desc is
259 struct usb_role_switch
*
260 usb_role_switch_register(struct device
*parent
,
261 const struct usb_role_switch_desc
*desc
)
263 struct usb_role_switch
*sw
;
266 if (!desc
|| !desc
->set
)
267 return ERR_PTR(-EINVAL
);
269 sw
= kzalloc(sizeof(*sw
), GFP_KERNEL
);
271 return ERR_PTR(-ENOMEM
);
273 mutex_init(&sw
->lock
);
275 sw
->allow_userspace_control
= desc
->allow_userspace_control
;
276 sw
->usb2_port
= desc
->usb2_port
;
277 sw
->usb3_port
= desc
->usb3_port
;
282 sw
->dev
.parent
= parent
;
283 sw
->dev
.fwnode
= desc
->fwnode
;
284 sw
->dev
.class = role_class
;
285 sw
->dev
.type
= &usb_role_dev_type
;
286 dev_set_name(&sw
->dev
, "%s-role-switch", dev_name(parent
));
288 ret
= device_register(&sw
->dev
);
290 put_device(&sw
->dev
);
294 /* TODO: Symlinks for the host port and the device controller. */
298 EXPORT_SYMBOL_GPL(usb_role_switch_register
);
301 * usb_role_switch_unregister - Unregsiter USB Role Switch
302 * @sw: USB Role Switch
304 * Unregister switch that was registered with usb_role_switch_register().
306 void usb_role_switch_unregister(struct usb_role_switch
*sw
)
308 if (!IS_ERR_OR_NULL(sw
))
309 device_unregister(&sw
->dev
);
311 EXPORT_SYMBOL_GPL(usb_role_switch_unregister
);
313 static int __init
usb_roles_init(void)
315 role_class
= class_create(THIS_MODULE
, "usb_role");
316 return PTR_ERR_OR_ZERO(role_class
);
318 subsys_initcall(usb_roles_init
);
320 static void __exit
usb_roles_exit(void)
322 class_destroy(role_class
);
324 module_exit(usb_roles_exit
);
326 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
327 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
328 MODULE_LICENSE("GPL v2");
329 MODULE_DESCRIPTION("USB Role Class");