GNU Linux-libre 5.10.215-gnu1
[releases.git] / drivers / usb / roles / class.c
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         bool registered;
25
26         /* From descriptor */
27         struct device *usb2_port;
28         struct device *usb3_port;
29         struct device *udc;
30         usb_role_switch_set_t set;
31         usb_role_switch_get_t get;
32         bool allow_userspace_control;
33 };
34
35 #define to_role_switch(d)       container_of(d, struct usb_role_switch, dev)
36
37 /**
38  * usb_role_switch_set_role - Set USB role for a switch
39  * @sw: USB role switch
40  * @role: USB role to be switched to
41  *
42  * Set USB role @role for @sw.
43  */
44 int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
45 {
46         int ret;
47
48         if (IS_ERR_OR_NULL(sw))
49                 return 0;
50
51         if (!sw->registered)
52                 return -EOPNOTSUPP;
53
54         mutex_lock(&sw->lock);
55
56         ret = sw->set(sw, role);
57         if (!ret) {
58                 sw->role = role;
59                 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
60         }
61
62         mutex_unlock(&sw->lock);
63
64         return ret;
65 }
66 EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
67
68 /**
69  * usb_role_switch_get_role - Get the USB role for a switch
70  * @sw: USB role switch
71  *
72  * Depending on the role-switch-driver this function returns either a cached
73  * value of the last set role, or reads back the actual value from the hardware.
74  */
75 enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
76 {
77         enum usb_role role;
78
79         if (IS_ERR_OR_NULL(sw) || !sw->registered)
80                 return USB_ROLE_NONE;
81
82         mutex_lock(&sw->lock);
83
84         if (sw->get)
85                 role = sw->get(sw);
86         else
87                 role = sw->role;
88
89         mutex_unlock(&sw->lock);
90
91         return role;
92 }
93 EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
94
95 static void *usb_role_switch_match(struct fwnode_handle *fwnode, const char *id,
96                                    void *data)
97 {
98         struct device *dev;
99
100         if (id && !fwnode_property_present(fwnode, id))
101                 return NULL;
102
103         dev = class_find_device_by_fwnode(role_class, fwnode);
104
105         return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
106 }
107
108 static struct usb_role_switch *
109 usb_role_switch_is_parent(struct fwnode_handle *fwnode)
110 {
111         struct fwnode_handle *parent = fwnode_get_parent(fwnode);
112         struct device *dev;
113
114         if (!fwnode_property_present(parent, "usb-role-switch")) {
115                 fwnode_handle_put(parent);
116                 return NULL;
117         }
118
119         dev = class_find_device_by_fwnode(role_class, parent);
120         fwnode_handle_put(parent);
121         return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
122 }
123
124 /**
125  * usb_role_switch_get - Find USB role switch linked with the caller
126  * @dev: The caller device
127  *
128  * Finds and returns role switch linked with @dev. The reference count for the
129  * found switch is incremented.
130  */
131 struct usb_role_switch *usb_role_switch_get(struct device *dev)
132 {
133         struct usb_role_switch *sw;
134
135         sw = usb_role_switch_is_parent(dev_fwnode(dev));
136         if (!sw)
137                 sw = device_connection_find_match(dev, "usb-role-switch", NULL,
138                                                   usb_role_switch_match);
139
140         if (!IS_ERR_OR_NULL(sw))
141                 WARN_ON(!try_module_get(sw->module));
142
143         return sw;
144 }
145 EXPORT_SYMBOL_GPL(usb_role_switch_get);
146
147 /**
148  * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
149  * @fwnode: The caller device node
150  *
151  * This is similar to the usb_role_switch_get() function above, but it searches
152  * the switch using fwnode instead of device entry.
153  */
154 struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
155 {
156         struct usb_role_switch *sw;
157
158         sw = usb_role_switch_is_parent(fwnode);
159         if (!sw)
160                 sw = fwnode_connection_find_match(fwnode, "usb-role-switch",
161                                                   NULL, usb_role_switch_match);
162         if (!IS_ERR_OR_NULL(sw))
163                 WARN_ON(!try_module_get(sw->module));
164
165         return sw;
166 }
167 EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
168
169 /**
170  * usb_role_switch_put - Release handle to a switch
171  * @sw: USB Role Switch
172  *
173  * Decrement reference count for @sw.
174  */
175 void usb_role_switch_put(struct usb_role_switch *sw)
176 {
177         if (!IS_ERR_OR_NULL(sw)) {
178                 module_put(sw->module);
179                 put_device(&sw->dev);
180         }
181 }
182 EXPORT_SYMBOL_GPL(usb_role_switch_put);
183
184 /**
185  * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
186  * @fwnode: fwnode of the USB Role Switch
187  *
188  * Finds and returns role switch with @fwnode. The reference count for the
189  * found switch is incremented.
190  */
191 struct usb_role_switch *
192 usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
193 {
194         struct device *dev;
195         struct usb_role_switch *sw = NULL;
196
197         if (!fwnode)
198                 return NULL;
199
200         dev = class_find_device_by_fwnode(role_class, fwnode);
201         if (dev) {
202                 sw = to_role_switch(dev);
203                 WARN_ON(!try_module_get(sw->module));
204         }
205
206         return sw;
207 }
208 EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode);
209
210 static umode_t
211 usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
212 {
213         struct device *dev = kobj_to_dev(kobj);
214         struct usb_role_switch *sw = to_role_switch(dev);
215
216         if (sw->allow_userspace_control)
217                 return attr->mode;
218
219         return 0;
220 }
221
222 static const char * const usb_roles[] = {
223         [USB_ROLE_NONE]         = "none",
224         [USB_ROLE_HOST]         = "host",
225         [USB_ROLE_DEVICE]       = "device",
226 };
227
228 static ssize_t
229 role_show(struct device *dev, struct device_attribute *attr, char *buf)
230 {
231         struct usb_role_switch *sw = to_role_switch(dev);
232         enum usb_role role = usb_role_switch_get_role(sw);
233
234         return sprintf(buf, "%s\n", usb_roles[role]);
235 }
236
237 static ssize_t role_store(struct device *dev, struct device_attribute *attr,
238                           const char *buf, size_t size)
239 {
240         struct usb_role_switch *sw = to_role_switch(dev);
241         int ret;
242
243         ret = sysfs_match_string(usb_roles, buf);
244         if (ret < 0) {
245                 bool res;
246
247                 /* Extra check if the user wants to disable the switch */
248                 ret = kstrtobool(buf, &res);
249                 if (ret || res)
250                         return -EINVAL;
251         }
252
253         ret = usb_role_switch_set_role(sw, ret);
254         if (ret)
255                 return ret;
256
257         return size;
258 }
259 static DEVICE_ATTR_RW(role);
260
261 static struct attribute *usb_role_switch_attrs[] = {
262         &dev_attr_role.attr,
263         NULL,
264 };
265
266 static const struct attribute_group usb_role_switch_group = {
267         .is_visible = usb_role_switch_is_visible,
268         .attrs = usb_role_switch_attrs,
269 };
270
271 static const struct attribute_group *usb_role_switch_groups[] = {
272         &usb_role_switch_group,
273         NULL,
274 };
275
276 static int
277 usb_role_switch_uevent(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->module = parent->driver->owner;
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         sw->registered = true;
355
356         /* TODO: Symlinks for the host port and the device controller. */
357
358         return sw;
359 }
360 EXPORT_SYMBOL_GPL(usb_role_switch_register);
361
362 /**
363  * usb_role_switch_unregister - Unregsiter USB Role Switch
364  * @sw: USB Role Switch
365  *
366  * Unregister switch that was registered with usb_role_switch_register().
367  */
368 void usb_role_switch_unregister(struct usb_role_switch *sw)
369 {
370         if (!IS_ERR_OR_NULL(sw)) {
371                 sw->registered = false;
372                 device_unregister(&sw->dev);
373         }
374 }
375 EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
376
377 /**
378  * usb_role_switch_set_drvdata - Assign private data pointer to a switch
379  * @sw: USB Role Switch
380  * @data: Private data pointer
381  */
382 void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
383 {
384         dev_set_drvdata(&sw->dev, data);
385 }
386 EXPORT_SYMBOL_GPL(usb_role_switch_set_drvdata);
387
388 /**
389  * usb_role_switch_get_drvdata - Get the private data pointer of a switch
390  * @sw: USB Role Switch
391  */
392 void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
393 {
394         return dev_get_drvdata(&sw->dev);
395 }
396 EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);
397
398 static int __init usb_roles_init(void)
399 {
400         role_class = class_create(THIS_MODULE, "usb_role");
401         return PTR_ERR_OR_ZERO(role_class);
402 }
403 subsys_initcall(usb_roles_init);
404
405 static void __exit usb_roles_exit(void)
406 {
407         class_destroy(role_class);
408 }
409 module_exit(usb_roles_exit);
410
411 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
412 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
413 MODULE_LICENSE("GPL v2");
414 MODULE_DESCRIPTION("USB Role Class");