xref: /linux/drivers/usb/roles/class.c (revision 8d23e94a443388e81c42ea7e476a5d79c1c795c9)
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
278 usb_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 
289 static 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 
296 static 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  */
317 struct usb_role_switch *
318 usb_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 }
358 EXPORT_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  */
366 void usb_role_switch_unregister(struct usb_role_switch *sw)
367 {
368 	if (!IS_ERR_OR_NULL(sw))
369 		device_unregister(&sw->dev);
370 }
371 EXPORT_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  */
378 void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
379 {
380 	dev_set_drvdata(&sw->dev, data);
381 }
382 EXPORT_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  */
388 void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
389 {
390 	return dev_get_drvdata(&sw->dev);
391 }
392 EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);
393 
394 static 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 }
399 subsys_initcall(usb_roles_init);
400 
401 static void __exit usb_roles_exit(void)
402 {
403 	class_destroy(role_class);
404 }
405 module_exit(usb_roles_exit);
406 
407 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
408 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
409 MODULE_LICENSE("GPL v2");
410 MODULE_DESCRIPTION("USB Role Class");
411