xref: /linux/drivers/usb/roles/class.c (revision c894ec016c9d0418dd832202225a8c64f450d71e)
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 	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  */
42 int 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 }
61 EXPORT_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  */
70 enum 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 }
88 EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
89 
90 static 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 
103 static struct usb_role_switch *
104 usb_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  */
126 struct 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 }
140 EXPORT_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  */
149 struct 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 }
162 EXPORT_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  */
170 void 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 }
177 EXPORT_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  */
186 struct usb_role_switch *
187 usb_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 }
200 EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode);
201 
202 static umode_t
203 usb_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 
214 static const char * const usb_roles[] = {
215 	[USB_ROLE_NONE]		= "none",
216 	[USB_ROLE_HOST]		= "host",
217 	[USB_ROLE_DEVICE]	= "device",
218 };
219 
220 const 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 }
227 EXPORT_SYMBOL_GPL(usb_role_string);
228 
229 static ssize_t
230 role_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 
238 static 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 }
260 static DEVICE_ATTR_RW(role);
261 
262 static struct attribute *usb_role_switch_attrs[] = {
263 	&dev_attr_role.attr,
264 	NULL,
265 };
266 
267 static const struct attribute_group usb_role_switch_group = {
268 	.is_visible = usb_role_switch_is_visible,
269 	.attrs = usb_role_switch_attrs,
270 };
271 
272 static const struct attribute_group *usb_role_switch_groups[] = {
273 	&usb_role_switch_group,
274 	NULL,
275 };
276 
277 static int usb_role_switch_uevent(const struct device *dev, struct kobj_uevent_env *env)
278 {
279 	int ret;
280 
281 	ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
282 	if (ret)
283 		dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
284 
285 	return ret;
286 }
287 
288 static void usb_role_switch_release(struct device *dev)
289 {
290 	struct usb_role_switch *sw = to_role_switch(dev);
291 
292 	kfree(sw);
293 }
294 
295 static const struct device_type usb_role_dev_type = {
296 	.name = "usb_role_switch",
297 	.groups = usb_role_switch_groups,
298 	.uevent = usb_role_switch_uevent,
299 	.release = usb_role_switch_release,
300 };
301 
302 /**
303  * usb_role_switch_register - Register USB Role Switch
304  * @parent: Parent device for the switch
305  * @desc: Description of the switch
306  *
307  * USB Role Switch is a device capable or choosing the role for USB connector.
308  * On platforms where the USB controller is dual-role capable, the controller
309  * driver will need to register the switch. On platforms where the USB host and
310  * USB device controllers behind the connector are separate, there will be a
311  * mux, and the driver for that mux will need to register the switch.
312  *
313  * Returns handle to a new role switch or ERR_PTR. The content of @desc is
314  * copied.
315  */
316 struct usb_role_switch *
317 usb_role_switch_register(struct device *parent,
318 			 const struct usb_role_switch_desc *desc)
319 {
320 	struct usb_role_switch *sw;
321 	int ret;
322 
323 	if (!desc || !desc->set)
324 		return ERR_PTR(-EINVAL);
325 
326 	sw = kzalloc(sizeof(*sw), GFP_KERNEL);
327 	if (!sw)
328 		return ERR_PTR(-ENOMEM);
329 
330 	mutex_init(&sw->lock);
331 
332 	sw->allow_userspace_control = desc->allow_userspace_control;
333 	sw->usb2_port = desc->usb2_port;
334 	sw->usb3_port = desc->usb3_port;
335 	sw->udc = desc->udc;
336 	sw->set = desc->set;
337 	sw->get = desc->get;
338 
339 	sw->dev.parent = parent;
340 	sw->dev.fwnode = desc->fwnode;
341 	sw->dev.class = role_class;
342 	sw->dev.type = &usb_role_dev_type;
343 	dev_set_drvdata(&sw->dev, desc->driver_data);
344 	dev_set_name(&sw->dev, "%s-role-switch",
345 		     desc->name ? desc->name : dev_name(parent));
346 
347 	ret = device_register(&sw->dev);
348 	if (ret) {
349 		put_device(&sw->dev);
350 		return ERR_PTR(ret);
351 	}
352 
353 	/* TODO: Symlinks for the host port and the device controller. */
354 
355 	return sw;
356 }
357 EXPORT_SYMBOL_GPL(usb_role_switch_register);
358 
359 /**
360  * usb_role_switch_unregister - Unregsiter USB Role Switch
361  * @sw: USB Role Switch
362  *
363  * Unregister switch that was registered with usb_role_switch_register().
364  */
365 void usb_role_switch_unregister(struct usb_role_switch *sw)
366 {
367 	if (!IS_ERR_OR_NULL(sw))
368 		device_unregister(&sw->dev);
369 }
370 EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
371 
372 /**
373  * usb_role_switch_set_drvdata - Assign private data pointer to a switch
374  * @sw: USB Role Switch
375  * @data: Private data pointer
376  */
377 void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
378 {
379 	dev_set_drvdata(&sw->dev, data);
380 }
381 EXPORT_SYMBOL_GPL(usb_role_switch_set_drvdata);
382 
383 /**
384  * usb_role_switch_get_drvdata - Get the private data pointer of a switch
385  * @sw: USB Role Switch
386  */
387 void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
388 {
389 	return dev_get_drvdata(&sw->dev);
390 }
391 EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);
392 
393 static int __init usb_roles_init(void)
394 {
395 	role_class = class_create("usb_role");
396 	return PTR_ERR_OR_ZERO(role_class);
397 }
398 subsys_initcall(usb_roles_init);
399 
400 static void __exit usb_roles_exit(void)
401 {
402 	class_destroy(role_class);
403 }
404 module_exit(usb_roles_exit);
405 
406 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
407 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
408 MODULE_LICENSE("GPL v2");
409 MODULE_DESCRIPTION("USB Role Class");
410