1
2
3
4
5
6
7
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
17static struct class *role_class;
18
19struct usb_role_switch {
20 struct device dev;
21 struct mutex lock;
22 enum usb_role role;
23
24
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
37
38
39
40
41
42int 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->dev.parent, role);
52 if (!ret)
53 sw->role = role;
54
55 mutex_unlock(&sw->lock);
56
57 return ret;
58}
59EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
60
61
62
63
64
65
66
67
68enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
69{
70 enum usb_role role;
71
72 if (IS_ERR_OR_NULL(sw))
73 return USB_ROLE_NONE;
74
75 mutex_lock(&sw->lock);
76
77 if (sw->get)
78 role = sw->get(sw->dev.parent);
79 else
80 role = sw->role;
81
82 mutex_unlock(&sw->lock);
83
84 return role;
85}
86EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
87
88static int switch_fwnode_match(struct device *dev, const void *fwnode)
89{
90 return dev_fwnode(dev) == fwnode;
91}
92
93static int switch_name_match(struct device *dev, const void *name)
94{
95 return !strcmp((const char *)name, dev_name(dev));
96}
97
98static void *usb_role_switch_match(struct device_connection *con, int ep,
99 void *data)
100{
101 struct device *dev;
102
103 if (con->fwnode) {
104 if (con->id && !fwnode_property_present(con->fwnode, con->id))
105 return NULL;
106
107 dev = class_find_device(role_class, NULL, con->fwnode,
108 switch_fwnode_match);
109 } else {
110 dev = class_find_device(role_class, NULL, con->endpoint[ep],
111 switch_name_match);
112 }
113
114 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
115}
116
117
118
119
120
121
122
123
124struct usb_role_switch *usb_role_switch_get(struct device *dev)
125{
126 struct usb_role_switch *sw;
127
128 sw = device_connection_find_match(dev, "usb-role-switch", NULL,
129 usb_role_switch_match);
130
131 if (!IS_ERR_OR_NULL(sw))
132 WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
133
134 return sw;
135}
136EXPORT_SYMBOL_GPL(usb_role_switch_get);
137
138
139
140
141
142
143
144void usb_role_switch_put(struct usb_role_switch *sw)
145{
146 if (!IS_ERR_OR_NULL(sw)) {
147 put_device(&sw->dev);
148 module_put(sw->dev.parent->driver->owner);
149 }
150}
151EXPORT_SYMBOL_GPL(usb_role_switch_put);
152
153static umode_t
154usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
155{
156 struct device *dev = container_of(kobj, typeof(*dev), kobj);
157 struct usb_role_switch *sw = to_role_switch(dev);
158
159 if (sw->allow_userspace_control)
160 return attr->mode;
161
162 return 0;
163}
164
165static const char * const usb_roles[] = {
166 [USB_ROLE_NONE] = "none",
167 [USB_ROLE_HOST] = "host",
168 [USB_ROLE_DEVICE] = "device",
169};
170
171static ssize_t
172role_show(struct device *dev, struct device_attribute *attr, char *buf)
173{
174 struct usb_role_switch *sw = to_role_switch(dev);
175 enum usb_role role = usb_role_switch_get_role(sw);
176
177 return sprintf(buf, "%s\n", usb_roles[role]);
178}
179
180static ssize_t role_store(struct device *dev, struct device_attribute *attr,
181 const char *buf, size_t size)
182{
183 struct usb_role_switch *sw = to_role_switch(dev);
184 int ret;
185
186 ret = sysfs_match_string(usb_roles, buf);
187 if (ret < 0) {
188 bool res;
189
190
191 ret = kstrtobool(buf, &res);
192 if (ret || res)
193 return -EINVAL;
194 }
195
196 ret = usb_role_switch_set_role(sw, ret);
197 if (ret)
198 return ret;
199
200 return size;
201}
202static DEVICE_ATTR_RW(role);
203
204static struct attribute *usb_role_switch_attrs[] = {
205 &dev_attr_role.attr,
206 NULL,
207};
208
209static const struct attribute_group usb_role_switch_group = {
210 .is_visible = usb_role_switch_is_visible,
211 .attrs = usb_role_switch_attrs,
212};
213
214static const struct attribute_group *usb_role_switch_groups[] = {
215 &usb_role_switch_group,
216 NULL,
217};
218
219static int
220usb_role_switch_uevent(struct device *dev, struct kobj_uevent_env *env)
221{
222 int ret;
223
224 ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
225 if (ret)
226 dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
227
228 return ret;
229}
230
231static void usb_role_switch_release(struct device *dev)
232{
233 struct usb_role_switch *sw = to_role_switch(dev);
234
235 kfree(sw);
236}
237
238static const struct device_type usb_role_dev_type = {
239 .name = "usb_role_switch",
240 .groups = usb_role_switch_groups,
241 .uevent = usb_role_switch_uevent,
242 .release = usb_role_switch_release,
243};
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259struct usb_role_switch *
260usb_role_switch_register(struct device *parent,
261 const struct usb_role_switch_desc *desc)
262{
263 struct usb_role_switch *sw;
264 int ret;
265
266 if (!desc || !desc->set)
267 return ERR_PTR(-EINVAL);
268
269 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
270 if (!sw)
271 return ERR_PTR(-ENOMEM);
272
273 mutex_init(&sw->lock);
274
275 sw->allow_userspace_control = desc->allow_userspace_control;
276 sw->usb2_port = desc->usb2_port;
277 sw->usb3_port = desc->usb3_port;
278 sw->udc = desc->udc;
279 sw->set = desc->set;
280 sw->get = desc->get;
281
282 sw->dev.parent = parent;
283 sw->dev.fwnode = desc->fwnode;
284 sw->dev.class = role_class;
285 sw->dev.type = &usb_role_dev_type;
286 dev_set_name(&sw->dev, "%s-role-switch", dev_name(parent));
287
288 ret = device_register(&sw->dev);
289 if (ret) {
290 put_device(&sw->dev);
291 return ERR_PTR(ret);
292 }
293
294
295
296 return sw;
297}
298EXPORT_SYMBOL_GPL(usb_role_switch_register);
299
300
301
302
303
304
305
306void usb_role_switch_unregister(struct usb_role_switch *sw)
307{
308 if (!IS_ERR_OR_NULL(sw))
309 device_unregister(&sw->dev);
310}
311EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
312
313static int __init usb_roles_init(void)
314{
315 role_class = class_create(THIS_MODULE, "usb_role");
316 return PTR_ERR_OR_ZERO(role_class);
317}
318subsys_initcall(usb_roles_init);
319
320static void __exit usb_roles_exit(void)
321{
322 class_destroy(role_class);
323}
324module_exit(usb_roles_exit);
325
326MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
327MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
328MODULE_LICENSE("GPL v2");
329MODULE_DESCRIPTION("USB Role Class");
330