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