Commit | Line | Data |
---|---|---|
2ccea03a FB |
1 | /** |
2 | * udc.c - Core UDC Framework | |
3 | * | |
4 | * Copyright (C) 2010 Texas Instruments | |
5 | * Author: Felipe Balbi <balbi@ti.com> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 of | |
9 | * the License as published by the Free Software Foundation. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <linux/kernel.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/device.h> | |
23 | #include <linux/list.h> | |
24 | #include <linux/err.h> | |
25 | ||
26 | #include <linux/usb/ch9.h> | |
27 | #include <linux/usb/gadget.h> | |
28 | ||
29 | /** | |
30 | * struct usb_udc - describes one usb device controller | |
31 | * @driver - the gadget driver pointer. For use by the class code | |
32 | * @dev - the child device to the actual controller | |
33 | * @gadget - the gadget. For use by the class code | |
34 | * @list - for use by the udc class driver | |
35 | * | |
36 | * This represents the internal data structure which is used by the UDC-class | |
37 | * to hold information about udc driver and gadget together. | |
38 | */ | |
39 | struct usb_udc { | |
40 | struct usb_gadget_driver *driver; | |
41 | struct usb_gadget *gadget; | |
42 | struct device dev; | |
43 | struct list_head list; | |
44 | }; | |
45 | ||
46 | static struct class *udc_class; | |
47 | static struct device_type udc_device_type; | |
48 | static LIST_HEAD(udc_list); | |
49 | static DEFINE_MUTEX(udc_lock); | |
50 | ||
51 | /* ------------------------------------------------------------------------- */ | |
52 | ||
53 | /** | |
54 | * usb_gadget_start - tells usb device controller to start up | |
55 | * @gadget: The gadget we want to get started | |
56 | * @driver: The driver we want to bind to @gadget | |
57 | * @bind: The bind function for @driver | |
58 | * | |
59 | * This call is issued by the UDC Class driver when it's about | |
60 | * to register a gadget driver to the device controller, before | |
61 | * calling gadget driver's bind() method. | |
62 | * | |
63 | * It allows the controller to be powered off until strictly | |
64 | * necessary to have it powered on. | |
65 | * | |
66 | * Returns zero on success, else negative errno. | |
67 | */ | |
68 | static inline int usb_gadget_start(struct usb_gadget *gadget, | |
69 | struct usb_gadget_driver *driver, | |
70 | int (*bind)(struct usb_gadget *)) | |
71 | { | |
72 | return gadget->ops->start(driver, bind); | |
73 | } | |
74 | ||
352c2dc8 SAS |
75 | /** |
76 | * usb_gadget_udc_start - tells usb device controller to start up | |
77 | * @gadget: The gadget we want to get started | |
78 | * @driver: The driver we want to bind to @gadget | |
79 | * | |
80 | * This call is issued by the UDC Class driver when it's about | |
81 | * to register a gadget driver to the device controller, before | |
82 | * calling gadget driver's bind() method. | |
83 | * | |
84 | * It allows the controller to be powered off until strictly | |
85 | * necessary to have it powered on. | |
86 | * | |
87 | * Returns zero on success, else negative errno. | |
88 | */ | |
89 | static inline int usb_gadget_udc_start(struct usb_gadget *gadget, | |
90 | struct usb_gadget_driver *driver) | |
91 | { | |
92 | return gadget->ops->udc_start(gadget, driver); | |
93 | } | |
94 | ||
2ccea03a FB |
95 | /** |
96 | * usb_gadget_stop - tells usb device controller we don't need it anymore | |
97 | * @gadget: The device we want to stop activity | |
98 | * @driver: The driver to unbind from @gadget | |
99 | * | |
100 | * This call is issued by the UDC Class driver after calling | |
101 | * gadget driver's unbind() method. | |
102 | * | |
103 | * The details are implementation specific, but it can go as | |
104 | * far as powering off UDC completely and disable its data | |
105 | * line pullups. | |
106 | */ | |
107 | static inline void usb_gadget_stop(struct usb_gadget *gadget, | |
108 | struct usb_gadget_driver *driver) | |
109 | { | |
110 | gadget->ops->stop(driver); | |
111 | } | |
112 | ||
352c2dc8 SAS |
113 | /** |
114 | * usb_gadget_udc_stop - tells usb device controller we don't need it anymore | |
115 | * @gadget: The device we want to stop activity | |
116 | * @driver: The driver to unbind from @gadget | |
117 | * | |
118 | * This call is issued by the UDC Class driver after calling | |
119 | * gadget driver's unbind() method. | |
120 | * | |
121 | * The details are implementation specific, but it can go as | |
122 | * far as powering off UDC completely and disable its data | |
123 | * line pullups. | |
124 | */ | |
125 | static inline void usb_gadget_udc_stop(struct usb_gadget *gadget, | |
126 | struct usb_gadget_driver *driver) | |
127 | { | |
128 | gadget->ops->udc_stop(gadget, driver); | |
129 | } | |
130 | ||
2ccea03a FB |
131 | /** |
132 | * usb_udc_release - release the usb_udc struct | |
133 | * @dev: the dev member within usb_udc | |
134 | * | |
135 | * This is called by driver's core in order to free memory once the last | |
136 | * reference is released. | |
137 | */ | |
138 | static void usb_udc_release(struct device *dev) | |
139 | { | |
140 | struct usb_udc *udc; | |
141 | ||
142 | udc = container_of(dev, struct usb_udc, dev); | |
143 | dev_dbg(dev, "releasing '%s'\n", dev_name(dev)); | |
144 | kfree(udc); | |
145 | } | |
146 | ||
147 | /** | |
148 | * usb_add_gadget_udc - adds a new gadget to the udc class driver list | |
149 | * @parent: the parent device to this udc. Usually the controller | |
150 | * driver's device. | |
151 | * @gadget: the gadget to be added to the list | |
152 | * | |
153 | * Returns zero on success, negative errno otherwise. | |
154 | */ | |
155 | int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget) | |
156 | { | |
157 | struct usb_udc *udc; | |
158 | int ret = -ENOMEM; | |
159 | ||
160 | udc = kzalloc(sizeof(*udc), GFP_KERNEL); | |
161 | if (!udc) | |
162 | goto err1; | |
163 | ||
164 | device_initialize(&udc->dev); | |
165 | udc->dev.release = usb_udc_release; | |
166 | udc->dev.class = udc_class; | |
167 | udc->dev.parent = parent; | |
168 | ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj)); | |
169 | if (ret) | |
170 | goto err2; | |
171 | ||
172 | udc->gadget = gadget; | |
173 | ||
174 | mutex_lock(&udc_lock); | |
175 | list_add_tail(&udc->list, &udc_list); | |
176 | ||
177 | ret = device_add(&udc->dev); | |
178 | if (ret) | |
179 | goto err3; | |
180 | ||
181 | mutex_unlock(&udc_lock); | |
182 | ||
183 | return 0; | |
184 | err3: | |
185 | list_del(&udc->list); | |
186 | mutex_unlock(&udc_lock); | |
187 | ||
188 | err2: | |
189 | put_device(&udc->dev); | |
190 | ||
191 | err1: | |
192 | return ret; | |
193 | } | |
194 | EXPORT_SYMBOL_GPL(usb_add_gadget_udc); | |
195 | ||
352c2dc8 SAS |
196 | static int udc_is_newstyle(struct usb_udc *udc) |
197 | { | |
198 | if (udc->gadget->ops->udc_start && udc->gadget->ops->udc_stop) | |
199 | return 1; | |
200 | return 0; | |
201 | } | |
202 | ||
203 | ||
2ccea03a FB |
204 | static void usb_gadget_remove_driver(struct usb_udc *udc) |
205 | { | |
206 | dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n", | |
207 | udc->gadget->name); | |
208 | ||
209 | kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); | |
210 | ||
352c2dc8 SAS |
211 | if (udc_is_newstyle(udc)) { |
212 | usb_gadget_disconnect(udc->gadget); | |
213 | udc->driver->unbind(udc->gadget); | |
214 | usb_gadget_udc_stop(udc->gadget, udc->driver); | |
215 | ||
216 | } else { | |
217 | usb_gadget_stop(udc->gadget, udc->driver); | |
218 | } | |
2ccea03a FB |
219 | |
220 | udc->driver = NULL; | |
221 | udc->dev.driver = NULL; | |
222 | } | |
223 | ||
224 | /** | |
225 | * usb_del_gadget_udc - deletes @udc from udc_list | |
226 | * @gadget: the gadget to be removed. | |
227 | * | |
228 | * This, will call usb_gadget_unregister_driver() if | |
229 | * the @udc is still busy. | |
230 | */ | |
231 | void usb_del_gadget_udc(struct usb_gadget *gadget) | |
232 | { | |
233 | struct usb_udc *udc = NULL; | |
234 | ||
235 | mutex_lock(&udc_lock); | |
236 | list_for_each_entry(udc, &udc_list, list) | |
237 | if (udc->gadget == gadget) | |
238 | goto found; | |
239 | ||
240 | dev_err(gadget->dev.parent, "gadget not registered.\n"); | |
241 | mutex_unlock(&udc_lock); | |
242 | ||
243 | return; | |
244 | ||
245 | found: | |
246 | dev_vdbg(gadget->dev.parent, "unregistering gadget\n"); | |
247 | ||
248 | list_del(&udc->list); | |
249 | mutex_unlock(&udc_lock); | |
250 | ||
251 | if (udc->driver) | |
252 | usb_gadget_remove_driver(udc); | |
253 | ||
254 | kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE); | |
255 | device_unregister(&udc->dev); | |
256 | } | |
257 | EXPORT_SYMBOL_GPL(usb_del_gadget_udc); | |
258 | ||
259 | /* ------------------------------------------------------------------------- */ | |
260 | ||
261 | int usb_gadget_probe_driver(struct usb_gadget_driver *driver, | |
262 | int (*bind)(struct usb_gadget *)) | |
263 | { | |
264 | struct usb_udc *udc = NULL; | |
265 | int ret; | |
266 | ||
267 | if (!driver || !bind || !driver->setup) | |
268 | return -EINVAL; | |
269 | ||
270 | mutex_lock(&udc_lock); | |
271 | list_for_each_entry(udc, &udc_list, list) { | |
272 | /* For now we take the first one */ | |
273 | if (!udc->driver) | |
274 | goto found; | |
275 | } | |
276 | ||
277 | pr_debug("couldn't find an available UDC\n"); | |
278 | mutex_unlock(&udc_lock); | |
279 | return -ENODEV; | |
280 | ||
281 | found: | |
282 | dev_dbg(&udc->dev, "registering UDC driver [%s]\n", | |
283 | driver->function); | |
284 | ||
285 | udc->driver = driver; | |
286 | udc->dev.driver = &driver->driver; | |
287 | ||
352c2dc8 SAS |
288 | if (udc_is_newstyle(udc)) { |
289 | ret = bind(udc->gadget); | |
290 | if (ret) | |
291 | goto err1; | |
292 | ret = usb_gadget_udc_start(udc->gadget, driver); | |
293 | if (ret) { | |
294 | driver->unbind(udc->gadget); | |
295 | goto err1; | |
296 | } | |
297 | usb_gadget_connect(udc->gadget); | |
298 | } else { | |
299 | ||
300 | ret = usb_gadget_start(udc->gadget, driver, bind); | |
301 | if (ret) | |
302 | goto err1; | |
303 | ||
304 | } | |
2ccea03a FB |
305 | |
306 | kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); | |
307 | mutex_unlock(&udc_lock); | |
308 | return 0; | |
309 | ||
310 | err1: | |
311 | dev_err(&udc->dev, "failed to start %s: %d\n", | |
312 | udc->driver->function, ret); | |
313 | udc->driver = NULL; | |
314 | udc->dev.driver = NULL; | |
315 | mutex_unlock(&udc_lock); | |
316 | return ret; | |
317 | } | |
318 | EXPORT_SYMBOL_GPL(usb_gadget_probe_driver); | |
319 | ||
320 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) | |
321 | { | |
322 | struct usb_udc *udc = NULL; | |
323 | int ret = -ENODEV; | |
324 | ||
325 | if (!driver || !driver->unbind) | |
326 | return -EINVAL; | |
327 | ||
328 | mutex_lock(&udc_lock); | |
329 | list_for_each_entry(udc, &udc_list, list) | |
330 | if (udc->driver == driver) { | |
331 | usb_gadget_remove_driver(udc); | |
332 | ret = 0; | |
333 | break; | |
334 | } | |
335 | ||
336 | mutex_unlock(&udc_lock); | |
337 | return ret; | |
338 | } | |
339 | EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver); | |
340 | ||
341 | /* ------------------------------------------------------------------------- */ | |
342 | ||
343 | static ssize_t usb_udc_srp_store(struct device *dev, | |
344 | struct device_attribute *attr, const char *buf, size_t n) | |
345 | { | |
346 | struct usb_udc *udc = dev_get_drvdata(dev); | |
347 | ||
348 | if (sysfs_streq(buf, "1")) | |
349 | usb_gadget_wakeup(udc->gadget); | |
350 | ||
351 | return n; | |
352 | } | |
353 | static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store); | |
354 | ||
355 | static ssize_t usb_udc_softconn_store(struct device *dev, | |
356 | struct device_attribute *attr, const char *buf, size_t n) | |
357 | { | |
358 | struct usb_udc *udc = dev_get_drvdata(dev); | |
359 | ||
360 | if (sysfs_streq(buf, "connect")) { | |
361 | usb_gadget_connect(udc->gadget); | |
362 | } else if (sysfs_streq(buf, "disconnect")) { | |
363 | usb_gadget_disconnect(udc->gadget); | |
364 | } else { | |
365 | dev_err(dev, "unsupported command '%s'\n", buf); | |
366 | return -EINVAL; | |
367 | } | |
368 | ||
369 | return n; | |
370 | } | |
371 | static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store); | |
372 | ||
373 | static ssize_t usb_udc_speed_show(struct device *dev, | |
374 | struct device_attribute *attr, char *buf) | |
375 | { | |
376 | struct usb_udc *udc = dev_get_drvdata(dev); | |
377 | struct usb_gadget *gadget = udc->gadget; | |
378 | ||
379 | switch (gadget->speed) { | |
380 | case USB_SPEED_LOW: | |
381 | return snprintf(buf, PAGE_SIZE, "low-speed\n"); | |
382 | case USB_SPEED_FULL: | |
383 | return snprintf(buf, PAGE_SIZE, "full-speed\n"); | |
384 | case USB_SPEED_HIGH: | |
385 | return snprintf(buf, PAGE_SIZE, "high-speed\n"); | |
386 | case USB_SPEED_WIRELESS: | |
387 | return snprintf(buf, PAGE_SIZE, "wireless\n"); | |
388 | case USB_SPEED_SUPER: | |
389 | return snprintf(buf, PAGE_SIZE, "super-speed\n"); | |
390 | case USB_SPEED_UNKNOWN: /* FALLTHROUGH */ | |
391 | default: | |
392 | return snprintf(buf, PAGE_SIZE, "UNKNOWN\n"); | |
393 | } | |
394 | } | |
395 | static DEVICE_ATTR(speed, S_IRUSR, usb_udc_speed_show, NULL); | |
396 | ||
397 | #define USB_UDC_ATTR(name) \ | |
398 | ssize_t usb_udc_##name##_show(struct device *dev, \ | |
399 | struct device_attribute *attr, char *buf) \ | |
400 | { \ | |
401 | struct usb_udc *udc = dev_get_drvdata(dev); \ | |
402 | struct usb_gadget *gadget = udc->gadget; \ | |
403 | \ | |
404 | return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \ | |
405 | } \ | |
406 | static DEVICE_ATTR(name, S_IRUSR, usb_udc_##name##_show, NULL) | |
407 | ||
408 | static USB_UDC_ATTR(is_dualspeed); | |
409 | static USB_UDC_ATTR(is_otg); | |
410 | static USB_UDC_ATTR(is_a_peripheral); | |
411 | static USB_UDC_ATTR(b_hnp_enable); | |
412 | static USB_UDC_ATTR(a_hnp_support); | |
413 | static USB_UDC_ATTR(a_alt_hnp_support); | |
414 | ||
415 | static struct attribute *usb_udc_attrs[] = { | |
416 | &dev_attr_srp.attr, | |
417 | &dev_attr_soft_connect.attr, | |
418 | &dev_attr_speed.attr, | |
419 | ||
420 | &dev_attr_is_dualspeed.attr, | |
421 | &dev_attr_is_otg.attr, | |
422 | &dev_attr_is_a_peripheral.attr, | |
423 | &dev_attr_b_hnp_enable.attr, | |
424 | &dev_attr_a_hnp_support.attr, | |
425 | &dev_attr_a_alt_hnp_support.attr, | |
426 | NULL, | |
427 | }; | |
428 | ||
429 | static const struct attribute_group usb_udc_attr_group = { | |
430 | .attrs = usb_udc_attrs, | |
431 | }; | |
432 | ||
433 | static const struct attribute_group *usb_udc_attr_groups[] = { | |
434 | &usb_udc_attr_group, | |
435 | NULL, | |
436 | }; | |
437 | ||
438 | static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env) | |
439 | { | |
440 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); | |
441 | int ret; | |
442 | ||
443 | ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name); | |
444 | if (ret) { | |
445 | dev_err(dev, "failed to add uevent USB_UDC_NAME\n"); | |
446 | return ret; | |
447 | } | |
448 | ||
449 | if (udc->driver) { | |
450 | ret = add_uevent_var(env, "USB_UDC_DRIVER=%s", | |
451 | udc->driver->function); | |
452 | if (ret) { | |
453 | dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n"); | |
454 | return ret; | |
455 | } | |
456 | } | |
457 | ||
458 | return 0; | |
459 | } | |
460 | ||
461 | static int __init usb_udc_init(void) | |
462 | { | |
463 | udc_class = class_create(THIS_MODULE, "udc"); | |
464 | if (IS_ERR(udc_class)) { | |
465 | pr_err("failed to create udc class --> %ld\n", | |
466 | PTR_ERR(udc_class)); | |
467 | return PTR_ERR(udc_class); | |
468 | } | |
469 | ||
470 | udc_class->dev_uevent = usb_udc_uevent; | |
471 | udc_device_type.groups = usb_udc_attr_groups; | |
472 | ||
473 | return 0; | |
474 | } | |
475 | subsys_initcall(usb_udc_init); | |
476 | ||
477 | static void __exit usb_udc_exit(void) | |
478 | { | |
479 | class_destroy(udc_class); | |
480 | } | |
481 | module_exit(usb_udc_exit); | |
482 | ||
483 | MODULE_DESCRIPTION("UDC Framework"); | |
484 | MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); | |
485 | MODULE_LICENSE("GPL v2"); |