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