Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * USB Role Switch Support
4 *
5 * Copyright (C) 2018 Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 * Hans de Goede <hdegoede@redhat.com>
8 */
9
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>
16
17static struct class *role_class;
18
19struct usb_role_switch {
20 struct device dev;
21 struct mutex lock; /* device lock*/
22 enum usb_role role;
23
24 /* From descriptor */
25 struct device *usb2_port;
26 struct device *usb3_port;
27 struct device *udc;
28 usb_role_switch_set_t set;
29 usb_role_switch_get_t get;
30 bool allow_userspace_control;
31};
32
33#define to_role_switch(d) container_of(d, struct usb_role_switch, dev)
34
35/**
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
39 *
40 * Set USB role @role for @sw.
41 */
42int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
43{
44 int ret;
45
46 if (IS_ERR_OR_NULL(sw))
47 return 0;
48
49 mutex_lock(&sw->lock);
50
51 ret = sw->set(sw, role);
52 if (!ret) {
53 sw->role = role;
54 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
55 }
56
57 mutex_unlock(&sw->lock);
58
59 return ret;
60}
61EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
62
63/**
64 * usb_role_switch_get_role - Get the USB role for a switch
65 * @sw: USB role switch
66 *
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.
69 */
70enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
71{
72 enum usb_role role;
73
74 if (IS_ERR_OR_NULL(sw))
75 return USB_ROLE_NONE;
76
77 mutex_lock(&sw->lock);
78
79 if (sw->get)
80 role = sw->get(sw);
81 else
82 role = sw->role;
83
84 mutex_unlock(&sw->lock);
85
86 return role;
87}
88EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
89
90static void *usb_role_switch_match(const struct fwnode_handle *fwnode, const char *id,
91 void *data)
92{
93 struct device *dev;
94
95 if (id && !fwnode_property_present(fwnode, id))
96 return NULL;
97
98 dev = class_find_device_by_fwnode(role_class, fwnode);
99
100 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
101}
102
103static struct usb_role_switch *
104usb_role_switch_is_parent(struct fwnode_handle *fwnode)
105{
106 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
107 struct device *dev;
108
109 if (!fwnode_property_present(parent, "usb-role-switch")) {
110 fwnode_handle_put(parent);
111 return NULL;
112 }
113
114 dev = class_find_device_by_fwnode(role_class, parent);
115 fwnode_handle_put(parent);
116 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
117}
118
119/**
120 * usb_role_switch_get - Find USB role switch linked with the caller
121 * @dev: The caller device
122 *
123 * Finds and returns role switch linked with @dev. The reference count for the
124 * found switch is incremented.
125 */
126struct usb_role_switch *usb_role_switch_get(struct device *dev)
127{
128 struct usb_role_switch *sw;
129
130 sw = usb_role_switch_is_parent(dev_fwnode(dev));
131 if (!sw)
132 sw = device_connection_find_match(dev, "usb-role-switch", NULL,
133 usb_role_switch_match);
134
135 if (!IS_ERR_OR_NULL(sw))
136 WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
137
138 return sw;
139}
140EXPORT_SYMBOL_GPL(usb_role_switch_get);
141
142/**
143 * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
144 * @fwnode: The caller device node
145 *
146 * This is similar to the usb_role_switch_get() function above, but it searches
147 * the switch using fwnode instead of device entry.
148 */
149struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
150{
151 struct usb_role_switch *sw;
152
153 sw = usb_role_switch_is_parent(fwnode);
154 if (!sw)
155 sw = fwnode_connection_find_match(fwnode, "usb-role-switch",
156 NULL, usb_role_switch_match);
157 if (!IS_ERR_OR_NULL(sw))
158 WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
159
160 return sw;
161}
162EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
163
164/**
165 * usb_role_switch_put - Release handle to a switch
166 * @sw: USB Role Switch
167 *
168 * Decrement reference count for @sw.
169 */
170void usb_role_switch_put(struct usb_role_switch *sw)
171{
172 if (!IS_ERR_OR_NULL(sw)) {
173 module_put(sw->dev.parent->driver->owner);
174 put_device(&sw->dev);
175 }
176}
177EXPORT_SYMBOL_GPL(usb_role_switch_put);
178
179/**
180 * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
181 * @fwnode: fwnode of the USB Role Switch
182 *
183 * Finds and returns role switch with @fwnode. The reference count for the
184 * found switch is incremented.
185 */
186struct usb_role_switch *
187usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
188{
189 struct device *dev;
190
191 if (!fwnode)
192 return NULL;
193
194 dev = class_find_device_by_fwnode(role_class, fwnode);
195 if (dev)
196 WARN_ON(!try_module_get(dev->parent->driver->owner));
197
198 return dev ? to_role_switch(dev) : NULL;
199}
200EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode);
201
202static umode_t
203usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
204{
205 struct device *dev = kobj_to_dev(kobj);
206 struct usb_role_switch *sw = to_role_switch(dev);
207
208 if (sw->allow_userspace_control)
209 return attr->mode;
210
211 return 0;
212}
213
214static const char * const usb_roles[] = {
215 [USB_ROLE_NONE] = "none",
216 [USB_ROLE_HOST] = "host",
217 [USB_ROLE_DEVICE] = "device",
218};
219
220const char *usb_role_string(enum usb_role role)
221{
222 if (role < 0 || role >= ARRAY_SIZE(usb_roles))
223 return "unknown";
224
225 return usb_roles[role];
226}
227EXPORT_SYMBOL_GPL(usb_role_string);
228
229static ssize_t
230role_show(struct device *dev, struct device_attribute *attr, char *buf)
231{
232 struct usb_role_switch *sw = to_role_switch(dev);
233 enum usb_role role = usb_role_switch_get_role(sw);
234
235 return sprintf(buf, "%s\n", usb_roles[role]);
236}
237
238static ssize_t role_store(struct device *dev, struct device_attribute *attr,
239 const char *buf, size_t size)
240{
241 struct usb_role_switch *sw = to_role_switch(dev);
242 int ret;
243
244 ret = sysfs_match_string(usb_roles, buf);
245 if (ret < 0) {
246 bool res;
247
248 /* Extra check if the user wants to disable the switch */
249 ret = kstrtobool(buf, &res);
250 if (ret || res)
251 return -EINVAL;
252 }
253
254 ret = usb_role_switch_set_role(sw, ret);
255 if (ret)
256 return ret;
257
258 return size;
259}
260static DEVICE_ATTR_RW(role);
261
262static struct attribute *usb_role_switch_attrs[] = {
263 &dev_attr_role.attr,
264 NULL,
265};
266
267static const struct attribute_group usb_role_switch_group = {
268 .is_visible = usb_role_switch_is_visible,
269 .attrs = usb_role_switch_attrs,
270};
271
272static const struct attribute_group *usb_role_switch_groups[] = {
273 &usb_role_switch_group,
274 NULL,
275};
276
277static int
278usb_role_switch_uevent(struct device *dev, struct kobj_uevent_env *env)
279{
280 int ret;
281
282 ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
283 if (ret)
284 dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
285
286 return ret;
287}
288
289static void usb_role_switch_release(struct device *dev)
290{
291 struct usb_role_switch *sw = to_role_switch(dev);
292
293 kfree(sw);
294}
295
296static const struct device_type usb_role_dev_type = {
297 .name = "usb_role_switch",
298 .groups = usb_role_switch_groups,
299 .uevent = usb_role_switch_uevent,
300 .release = usb_role_switch_release,
301};
302
303/**
304 * usb_role_switch_register - Register USB Role Switch
305 * @parent: Parent device for the switch
306 * @desc: Description of the switch
307 *
308 * USB Role Switch is a device capable or choosing the role for USB connector.
309 * On platforms where the USB controller is dual-role capable, the controller
310 * driver will need to register the switch. On platforms where the USB host and
311 * USB device controllers behind the connector are separate, there will be a
312 * mux, and the driver for that mux will need to register the switch.
313 *
314 * Returns handle to a new role switch or ERR_PTR. The content of @desc is
315 * copied.
316 */
317struct usb_role_switch *
318usb_role_switch_register(struct device *parent,
319 const struct usb_role_switch_desc *desc)
320{
321 struct usb_role_switch *sw;
322 int ret;
323
324 if (!desc || !desc->set)
325 return ERR_PTR(-EINVAL);
326
327 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
328 if (!sw)
329 return ERR_PTR(-ENOMEM);
330
331 mutex_init(&sw->lock);
332
333 sw->allow_userspace_control = desc->allow_userspace_control;
334 sw->usb2_port = desc->usb2_port;
335 sw->usb3_port = desc->usb3_port;
336 sw->udc = desc->udc;
337 sw->set = desc->set;
338 sw->get = desc->get;
339
340 sw->dev.parent = parent;
341 sw->dev.fwnode = desc->fwnode;
342 sw->dev.class = role_class;
343 sw->dev.type = &usb_role_dev_type;
344 dev_set_drvdata(&sw->dev, desc->driver_data);
345 dev_set_name(&sw->dev, "%s-role-switch",
346 desc->name ? desc->name : dev_name(parent));
347
348 ret = device_register(&sw->dev);
349 if (ret) {
350 put_device(&sw->dev);
351 return ERR_PTR(ret);
352 }
353
354 /* TODO: Symlinks for the host port and the device controller. */
355
356 return sw;
357}
358EXPORT_SYMBOL_GPL(usb_role_switch_register);
359
360/**
361 * usb_role_switch_unregister - Unregsiter USB Role Switch
362 * @sw: USB Role Switch
363 *
364 * Unregister switch that was registered with usb_role_switch_register().
365 */
366void usb_role_switch_unregister(struct usb_role_switch *sw)
367{
368 if (!IS_ERR_OR_NULL(sw))
369 device_unregister(&sw->dev);
370}
371EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
372
373/**
374 * usb_role_switch_set_drvdata - Assign private data pointer to a switch
375 * @sw: USB Role Switch
376 * @data: Private data pointer
377 */
378void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
379{
380 dev_set_drvdata(&sw->dev, data);
381}
382EXPORT_SYMBOL_GPL(usb_role_switch_set_drvdata);
383
384/**
385 * usb_role_switch_get_drvdata - Get the private data pointer of a switch
386 * @sw: USB Role Switch
387 */
388void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
389{
390 return dev_get_drvdata(&sw->dev);
391}
392EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);
393
394static int __init usb_roles_init(void)
395{
396 role_class = class_create(THIS_MODULE, "usb_role");
397 return PTR_ERR_OR_ZERO(role_class);
398}
399subsys_initcall(usb_roles_init);
400
401static void __exit usb_roles_exit(void)
402{
403 class_destroy(role_class);
404}
405module_exit(usb_roles_exit);
406
407MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
408MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
409MODULE_LICENSE("GPL v2");
410MODULE_DESCRIPTION("USB Role Class");
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * USB Role Switch Support
4 *
5 * Copyright (C) 2018 Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 * Hans de Goede <hdegoede@redhat.com>
8 */
9
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>
16
17static struct class *role_class;
18
19struct usb_role_switch {
20 struct device dev;
21 struct mutex lock; /* device lock*/
22 enum usb_role role;
23
24 /* From descriptor */
25 struct device *usb2_port;
26 struct device *usb3_port;
27 struct device *udc;
28 usb_role_switch_set_t set;
29 usb_role_switch_get_t get;
30 bool allow_userspace_control;
31};
32
33#define to_role_switch(d) container_of(d, struct usb_role_switch, dev)
34
35/**
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
39 *
40 * Set USB role @role for @sw.
41 */
42int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
43{
44 int ret;
45
46 if (IS_ERR_OR_NULL(sw))
47 return 0;
48
49 mutex_lock(&sw->lock);
50
51 ret = sw->set(sw->dev.parent, role);
52 if (!ret)
53 sw->role = role;
54
55 mutex_unlock(&sw->lock);
56
57 return ret;
58}
59EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
60
61/**
62 * usb_role_switch_get_role - Get the USB role for a switch
63 * @sw: USB role switch
64 *
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.
67 */
68enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
69{
70 enum usb_role role;
71
72 if (IS_ERR_OR_NULL(sw))
73 return USB_ROLE_NONE;
74
75 mutex_lock(&sw->lock);
76
77 if (sw->get)
78 role = sw->get(sw->dev.parent);
79 else
80 role = sw->role;
81
82 mutex_unlock(&sw->lock);
83
84 return role;
85}
86EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
87
88static void *usb_role_switch_match(struct device_connection *con, int ep,
89 void *data)
90{
91 struct device *dev;
92
93 if (con->fwnode) {
94 if (con->id && !fwnode_property_present(con->fwnode, con->id))
95 return NULL;
96
97 dev = class_find_device_by_fwnode(role_class, con->fwnode);
98 } else {
99 dev = class_find_device_by_name(role_class, con->endpoint[ep]);
100 }
101
102 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
103}
104
105static struct usb_role_switch *
106usb_role_switch_is_parent(struct fwnode_handle *fwnode)
107{
108 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
109 struct device *dev;
110
111 if (!parent || !fwnode_property_present(parent, "usb-role-switch"))
112 return NULL;
113
114 dev = class_find_device_by_fwnode(role_class, parent);
115 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
116}
117
118/**
119 * usb_role_switch_get - Find USB role switch linked with the caller
120 * @dev: The caller device
121 *
122 * Finds and returns role switch linked with @dev. The reference count for the
123 * found switch is incremented.
124 */
125struct usb_role_switch *usb_role_switch_get(struct device *dev)
126{
127 struct usb_role_switch *sw;
128
129 sw = usb_role_switch_is_parent(dev_fwnode(dev));
130 if (!sw)
131 sw = device_connection_find_match(dev, "usb-role-switch", NULL,
132 usb_role_switch_match);
133
134 if (!IS_ERR_OR_NULL(sw))
135 WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
136
137 return sw;
138}
139EXPORT_SYMBOL_GPL(usb_role_switch_get);
140
141/**
142 * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
143 * @fwnode: The caller device node
144 *
145 * This is similar to the usb_role_switch_get() function above, but it searches
146 * the switch using fwnode instead of device entry.
147 */
148struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
149{
150 struct usb_role_switch *sw;
151
152 sw = usb_role_switch_is_parent(fwnode);
153 if (!sw)
154 sw = fwnode_connection_find_match(fwnode, "usb-role-switch",
155 NULL, usb_role_switch_match);
156 if (!IS_ERR_OR_NULL(sw))
157 WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
158
159 return sw;
160}
161EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
162
163/**
164 * usb_role_switch_put - Release handle to a switch
165 * @sw: USB Role Switch
166 *
167 * Decrement reference count for @sw.
168 */
169void usb_role_switch_put(struct usb_role_switch *sw)
170{
171 if (!IS_ERR_OR_NULL(sw)) {
172 put_device(&sw->dev);
173 module_put(sw->dev.parent->driver->owner);
174 }
175}
176EXPORT_SYMBOL_GPL(usb_role_switch_put);
177
178static umode_t
179usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
180{
181 struct device *dev = container_of(kobj, typeof(*dev), kobj);
182 struct usb_role_switch *sw = to_role_switch(dev);
183
184 if (sw->allow_userspace_control)
185 return attr->mode;
186
187 return 0;
188}
189
190static const char * const usb_roles[] = {
191 [USB_ROLE_NONE] = "none",
192 [USB_ROLE_HOST] = "host",
193 [USB_ROLE_DEVICE] = "device",
194};
195
196static ssize_t
197role_show(struct device *dev, struct device_attribute *attr, char *buf)
198{
199 struct usb_role_switch *sw = to_role_switch(dev);
200 enum usb_role role = usb_role_switch_get_role(sw);
201
202 return sprintf(buf, "%s\n", usb_roles[role]);
203}
204
205static ssize_t role_store(struct device *dev, struct device_attribute *attr,
206 const char *buf, size_t size)
207{
208 struct usb_role_switch *sw = to_role_switch(dev);
209 int ret;
210
211 ret = sysfs_match_string(usb_roles, buf);
212 if (ret < 0) {
213 bool res;
214
215 /* Extra check if the user wants to disable the switch */
216 ret = kstrtobool(buf, &res);
217 if (ret || res)
218 return -EINVAL;
219 }
220
221 ret = usb_role_switch_set_role(sw, ret);
222 if (ret)
223 return ret;
224
225 return size;
226}
227static DEVICE_ATTR_RW(role);
228
229static struct attribute *usb_role_switch_attrs[] = {
230 &dev_attr_role.attr,
231 NULL,
232};
233
234static const struct attribute_group usb_role_switch_group = {
235 .is_visible = usb_role_switch_is_visible,
236 .attrs = usb_role_switch_attrs,
237};
238
239static const struct attribute_group *usb_role_switch_groups[] = {
240 &usb_role_switch_group,
241 NULL,
242};
243
244static int
245usb_role_switch_uevent(struct device *dev, struct kobj_uevent_env *env)
246{
247 int ret;
248
249 ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
250 if (ret)
251 dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
252
253 return ret;
254}
255
256static void usb_role_switch_release(struct device *dev)
257{
258 struct usb_role_switch *sw = to_role_switch(dev);
259
260 kfree(sw);
261}
262
263static const struct device_type usb_role_dev_type = {
264 .name = "usb_role_switch",
265 .groups = usb_role_switch_groups,
266 .uevent = usb_role_switch_uevent,
267 .release = usb_role_switch_release,
268};
269
270/**
271 * usb_role_switch_register - Register USB Role Switch
272 * @parent: Parent device for the switch
273 * @desc: Description of the switch
274 *
275 * USB Role Switch is a device capable or choosing the role for USB connector.
276 * On platforms where the USB controller is dual-role capable, the controller
277 * driver will need to register the switch. On platforms where the USB host and
278 * USB device controllers behind the connector are separate, there will be a
279 * mux, and the driver for that mux will need to register the switch.
280 *
281 * Returns handle to a new role switch or ERR_PTR. The content of @desc is
282 * copied.
283 */
284struct usb_role_switch *
285usb_role_switch_register(struct device *parent,
286 const struct usb_role_switch_desc *desc)
287{
288 struct usb_role_switch *sw;
289 int ret;
290
291 if (!desc || !desc->set)
292 return ERR_PTR(-EINVAL);
293
294 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
295 if (!sw)
296 return ERR_PTR(-ENOMEM);
297
298 mutex_init(&sw->lock);
299
300 sw->allow_userspace_control = desc->allow_userspace_control;
301 sw->usb2_port = desc->usb2_port;
302 sw->usb3_port = desc->usb3_port;
303 sw->udc = desc->udc;
304 sw->set = desc->set;
305 sw->get = desc->get;
306
307 sw->dev.parent = parent;
308 sw->dev.fwnode = desc->fwnode;
309 sw->dev.class = role_class;
310 sw->dev.type = &usb_role_dev_type;
311 dev_set_name(&sw->dev, "%s-role-switch", dev_name(parent));
312
313 ret = device_register(&sw->dev);
314 if (ret) {
315 put_device(&sw->dev);
316 return ERR_PTR(ret);
317 }
318
319 /* TODO: Symlinks for the host port and the device controller. */
320
321 return sw;
322}
323EXPORT_SYMBOL_GPL(usb_role_switch_register);
324
325/**
326 * usb_role_switch_unregister - Unregsiter USB Role Switch
327 * @sw: USB Role Switch
328 *
329 * Unregister switch that was registered with usb_role_switch_register().
330 */
331void usb_role_switch_unregister(struct usb_role_switch *sw)
332{
333 if (!IS_ERR_OR_NULL(sw))
334 device_unregister(&sw->dev);
335}
336EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
337
338static int __init usb_roles_init(void)
339{
340 role_class = class_create(THIS_MODULE, "usb_role");
341 return PTR_ERR_OR_ZERO(role_class);
342}
343subsys_initcall(usb_roles_init);
344
345static void __exit usb_roles_exit(void)
346{
347 class_destroy(role_class);
348}
349module_exit(usb_roles_exit);
350
351MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
352MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
353MODULE_LICENSE("GPL v2");
354MODULE_DESCRIPTION("USB Role Class");