dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / usb / roles / class.c
blob27d92af296351f138f9e7724bbb65c4c73e8522e
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * USB Role Switch Support
5 * Copyright (C) 2018 Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 * Hans de Goede <hdegoede@redhat.com>
8 */
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>
17 static struct class *role_class;
19 struct usb_role_switch {
20 struct device dev;
21 struct mutex lock; /* device lock*/
22 enum usb_role role;
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;
33 #define to_role_switch(d) container_of(d, struct usb_role_switch, dev)
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
40 * Set USB role @role for @sw.
42 int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
44 int ret;
46 if (IS_ERR_OR_NULL(sw))
47 return 0;
49 mutex_lock(&sw->lock);
51 ret = sw->set(sw, role);
52 if (!ret) {
53 sw->role = role;
54 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
57 mutex_unlock(&sw->lock);
59 return ret;
61 EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
63 /**
64 * usb_role_switch_get_role - Get the USB role for a switch
65 * @sw: USB role switch
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.
70 enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
72 enum usb_role role;
74 if (IS_ERR_OR_NULL(sw))
75 return USB_ROLE_NONE;
77 mutex_lock(&sw->lock);
79 if (sw->get)
80 role = sw->get(sw);
81 else
82 role = sw->role;
84 mutex_unlock(&sw->lock);
86 return role;
88 EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
90 static void *usb_role_switch_match(struct device_connection *con, int ep,
91 void *data)
93 struct device *dev;
95 if (con->fwnode) {
96 if (con->id && !fwnode_property_present(con->fwnode, con->id))
97 return NULL;
99 dev = class_find_device_by_fwnode(role_class, con->fwnode);
100 } else {
101 dev = class_find_device_by_name(role_class, con->endpoint[ep]);
104 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
107 static struct usb_role_switch *
108 usb_role_switch_is_parent(struct fwnode_handle *fwnode)
110 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
111 struct device *dev;
113 if (!parent || !fwnode_property_present(parent, "usb-role-switch"))
114 return NULL;
116 dev = class_find_device_by_fwnode(role_class, parent);
117 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
121 * usb_role_switch_get - Find USB role switch linked with the caller
122 * @dev: The caller device
124 * Finds and returns role switch linked with @dev. The reference count for the
125 * found switch is incremented.
127 struct usb_role_switch *usb_role_switch_get(struct device *dev)
129 struct usb_role_switch *sw;
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);
136 if (!IS_ERR_OR_NULL(sw))
137 WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
139 return sw;
141 EXPORT_SYMBOL_GPL(usb_role_switch_get);
144 * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
145 * @fwnode: The caller device node
147 * This is similar to the usb_role_switch_get() function above, but it searches
148 * the switch using fwnode instead of device entry.
150 struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
152 struct usb_role_switch *sw;
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->dev.parent->driver->owner));
161 return sw;
163 EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
166 * usb_role_switch_put - Release handle to a switch
167 * @sw: USB Role Switch
169 * Decrement reference count for @sw.
171 void usb_role_switch_put(struct usb_role_switch *sw)
173 if (!IS_ERR_OR_NULL(sw)) {
174 module_put(sw->dev.parent->driver->owner);
175 put_device(&sw->dev);
178 EXPORT_SYMBOL_GPL(usb_role_switch_put);
181 * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
182 * @fwnode: fwnode of the USB Role Switch
184 * Finds and returns role switch with @fwnode. The reference count for the
185 * found switch is incremented.
187 struct usb_role_switch *
188 usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
190 struct device *dev;
192 if (!fwnode)
193 return NULL;
195 dev = class_find_device_by_fwnode(role_class, fwnode);
197 return dev ? to_role_switch(dev) : NULL;
199 EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode);
201 static umode_t
202 usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
204 struct device *dev = kobj_to_dev(kobj);
205 struct usb_role_switch *sw = to_role_switch(dev);
207 if (sw->allow_userspace_control)
208 return attr->mode;
210 return 0;
213 static const char * const usb_roles[] = {
214 [USB_ROLE_NONE] = "none",
215 [USB_ROLE_HOST] = "host",
216 [USB_ROLE_DEVICE] = "device",
219 static ssize_t
220 role_show(struct device *dev, struct device_attribute *attr, char *buf)
222 struct usb_role_switch *sw = to_role_switch(dev);
223 enum usb_role role = usb_role_switch_get_role(sw);
225 return sprintf(buf, "%s\n", usb_roles[role]);
228 static ssize_t role_store(struct device *dev, struct device_attribute *attr,
229 const char *buf, size_t size)
231 struct usb_role_switch *sw = to_role_switch(dev);
232 int ret;
234 ret = sysfs_match_string(usb_roles, buf);
235 if (ret < 0) {
236 bool res;
238 /* Extra check if the user wants to disable the switch */
239 ret = kstrtobool(buf, &res);
240 if (ret || res)
241 return -EINVAL;
244 ret = usb_role_switch_set_role(sw, ret);
245 if (ret)
246 return ret;
248 return size;
250 static DEVICE_ATTR_RW(role);
252 static struct attribute *usb_role_switch_attrs[] = {
253 &dev_attr_role.attr,
254 NULL,
257 static const struct attribute_group usb_role_switch_group = {
258 .is_visible = usb_role_switch_is_visible,
259 .attrs = usb_role_switch_attrs,
262 static const struct attribute_group *usb_role_switch_groups[] = {
263 &usb_role_switch_group,
264 NULL,
267 static int
268 usb_role_switch_uevent(struct device *dev, struct kobj_uevent_env *env)
270 int ret;
272 ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
273 if (ret)
274 dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
276 return ret;
279 static void usb_role_switch_release(struct device *dev)
281 struct usb_role_switch *sw = to_role_switch(dev);
283 kfree(sw);
286 static const struct device_type usb_role_dev_type = {
287 .name = "usb_role_switch",
288 .groups = usb_role_switch_groups,
289 .uevent = usb_role_switch_uevent,
290 .release = usb_role_switch_release,
294 * usb_role_switch_register - Register USB Role Switch
295 * @parent: Parent device for the switch
296 * @desc: Description of the switch
298 * USB Role Switch is a device capable or choosing the role for USB connector.
299 * On platforms where the USB controller is dual-role capable, the controller
300 * driver will need to register the switch. On platforms where the USB host and
301 * USB device controllers behind the connector are separate, there will be a
302 * mux, and the driver for that mux will need to register the switch.
304 * Returns handle to a new role switch or ERR_PTR. The content of @desc is
305 * copied.
307 struct usb_role_switch *
308 usb_role_switch_register(struct device *parent,
309 const struct usb_role_switch_desc *desc)
311 struct usb_role_switch *sw;
312 int ret;
314 if (!desc || !desc->set)
315 return ERR_PTR(-EINVAL);
317 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
318 if (!sw)
319 return ERR_PTR(-ENOMEM);
321 mutex_init(&sw->lock);
323 sw->allow_userspace_control = desc->allow_userspace_control;
324 sw->usb2_port = desc->usb2_port;
325 sw->usb3_port = desc->usb3_port;
326 sw->udc = desc->udc;
327 sw->set = desc->set;
328 sw->get = desc->get;
330 sw->dev.parent = parent;
331 sw->dev.fwnode = desc->fwnode;
332 sw->dev.class = role_class;
333 sw->dev.type = &usb_role_dev_type;
334 dev_set_drvdata(&sw->dev, desc->driver_data);
335 dev_set_name(&sw->dev, "%s-role-switch",
336 desc->name ? desc->name : dev_name(parent));
338 ret = device_register(&sw->dev);
339 if (ret) {
340 put_device(&sw->dev);
341 return ERR_PTR(ret);
344 /* TODO: Symlinks for the host port and the device controller. */
346 return sw;
348 EXPORT_SYMBOL_GPL(usb_role_switch_register);
351 * usb_role_switch_unregister - Unregsiter USB Role Switch
352 * @sw: USB Role Switch
354 * Unregister switch that was registered with usb_role_switch_register().
356 void usb_role_switch_unregister(struct usb_role_switch *sw)
358 if (!IS_ERR_OR_NULL(sw))
359 device_unregister(&sw->dev);
361 EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
364 * usb_role_switch_set_drvdata - Assign private data pointer to a switch
365 * @sw: USB Role Switch
366 * @data: Private data pointer
368 void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
370 dev_set_drvdata(&sw->dev, data);
372 EXPORT_SYMBOL_GPL(usb_role_switch_set_drvdata);
375 * usb_role_switch_get_drvdata - Get the private data pointer of a switch
376 * @sw: USB Role Switch
378 void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
380 return dev_get_drvdata(&sw->dev);
382 EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);
384 static int __init usb_roles_init(void)
386 role_class = class_create(THIS_MODULE, "usb_role");
387 return PTR_ERR_OR_ZERO(role_class);
389 subsys_initcall(usb_roles_init);
391 static void __exit usb_roles_exit(void)
393 class_destroy(role_class);
395 module_exit(usb_roles_exit);
397 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
398 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
399 MODULE_LICENSE("GPL v2");
400 MODULE_DESCRIPTION("USB Role Class");