PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / usb / gadget / udc-core.c
blob27768a7d986a7f84e66d510be82cab2c513c7367
1 /**
2 * udc.c - Core UDC Framework
4 * Copyright (C) 2010 Texas Instruments
5 * Author: Felipe Balbi <balbi@ti.com>
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.
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.
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/>.
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 #include <linux/dma-mapping.h>
26 #include <linux/workqueue.h>
28 #include <linux/usb/ch9.h>
29 #include <linux/usb/gadget.h>
31 /**
32 * struct usb_udc - describes one usb device controller
33 * @driver - the gadget driver pointer. For use by the class code
34 * @dev - the child device to the actual controller
35 * @gadget - the gadget. For use by the class code
36 * @list - for use by the udc class driver
38 * This represents the internal data structure which is used by the UDC-class
39 * to hold information about udc driver and gadget together.
41 struct usb_udc {
42 struct usb_gadget_driver *driver;
43 struct usb_gadget *gadget;
44 struct device dev;
45 struct list_head list;
48 static struct class *udc_class;
49 static LIST_HEAD(udc_list);
50 static DEFINE_MUTEX(udc_lock);
52 /* ------------------------------------------------------------------------- */
54 #ifdef CONFIG_HAS_DMA
56 int usb_gadget_map_request(struct usb_gadget *gadget,
57 struct usb_request *req, int is_in)
59 if (req->length == 0)
60 return 0;
62 if (req->num_sgs) {
63 int mapped;
65 mapped = dma_map_sg(&gadget->dev, req->sg, req->num_sgs,
66 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
67 if (mapped == 0) {
68 dev_err(&gadget->dev, "failed to map SGs\n");
69 return -EFAULT;
72 req->num_mapped_sgs = mapped;
73 } else {
74 req->dma = dma_map_single(&gadget->dev, req->buf, req->length,
75 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
77 if (dma_mapping_error(&gadget->dev, req->dma)) {
78 dev_err(&gadget->dev, "failed to map buffer\n");
79 return -EFAULT;
83 return 0;
85 EXPORT_SYMBOL_GPL(usb_gadget_map_request);
87 void usb_gadget_unmap_request(struct usb_gadget *gadget,
88 struct usb_request *req, int is_in)
90 if (req->length == 0)
91 return;
93 if (req->num_mapped_sgs) {
94 dma_unmap_sg(&gadget->dev, req->sg, req->num_mapped_sgs,
95 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
97 req->num_mapped_sgs = 0;
98 } else {
99 dma_unmap_single(&gadget->dev, req->dma, req->length,
100 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
103 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
105 #endif /* CONFIG_HAS_DMA */
107 /* ------------------------------------------------------------------------- */
109 static void usb_gadget_state_work(struct work_struct *work)
111 struct usb_gadget *gadget = work_to_gadget(work);
113 sysfs_notify(&gadget->dev.kobj, NULL, "state");
116 void usb_gadget_set_state(struct usb_gadget *gadget,
117 enum usb_device_state state)
119 gadget->state = state;
120 schedule_work(&gadget->work);
122 EXPORT_SYMBOL_GPL(usb_gadget_set_state);
124 /* ------------------------------------------------------------------------- */
127 * usb_gadget_udc_start - tells usb device controller to start up
128 * @gadget: The gadget we want to get started
129 * @driver: The driver we want to bind to @gadget
131 * This call is issued by the UDC Class driver when it's about
132 * to register a gadget driver to the device controller, before
133 * calling gadget driver's bind() method.
135 * It allows the controller to be powered off until strictly
136 * necessary to have it powered on.
138 * Returns zero on success, else negative errno.
140 static inline int usb_gadget_udc_start(struct usb_gadget *gadget,
141 struct usb_gadget_driver *driver)
143 return gadget->ops->udc_start(gadget, driver);
147 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
148 * @gadget: The device we want to stop activity
149 * @driver: The driver to unbind from @gadget
151 * This call is issued by the UDC Class driver after calling
152 * gadget driver's unbind() method.
154 * The details are implementation specific, but it can go as
155 * far as powering off UDC completely and disable its data
156 * line pullups.
158 static inline void usb_gadget_udc_stop(struct usb_gadget *gadget,
159 struct usb_gadget_driver *driver)
161 gadget->ops->udc_stop(gadget, driver);
165 * usb_udc_release - release the usb_udc struct
166 * @dev: the dev member within usb_udc
168 * This is called by driver's core in order to free memory once the last
169 * reference is released.
171 static void usb_udc_release(struct device *dev)
173 struct usb_udc *udc;
175 udc = container_of(dev, struct usb_udc, dev);
176 dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
177 kfree(udc);
180 static const struct attribute_group *usb_udc_attr_groups[];
182 static void usb_udc_nop_release(struct device *dev)
184 dev_vdbg(dev, "%s\n", __func__);
188 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
189 * @parent: the parent device to this udc. Usually the controller driver's
190 * device.
191 * @gadget: the gadget to be added to the list.
192 * @release: a gadget release function.
194 * Returns zero on success, negative errno otherwise.
196 int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
197 void (*release)(struct device *dev))
199 struct usb_udc *udc;
200 int ret = -ENOMEM;
202 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
203 if (!udc)
204 goto err1;
206 dev_set_name(&gadget->dev, "gadget");
207 INIT_WORK(&gadget->work, usb_gadget_state_work);
208 gadget->dev.parent = parent;
210 #ifdef CONFIG_HAS_DMA
211 dma_set_coherent_mask(&gadget->dev, parent->coherent_dma_mask);
212 gadget->dev.dma_parms = parent->dma_parms;
213 gadget->dev.dma_mask = parent->dma_mask;
214 #endif
216 if (release)
217 gadget->dev.release = release;
218 else
219 gadget->dev.release = usb_udc_nop_release;
221 ret = device_register(&gadget->dev);
222 if (ret)
223 goto err2;
225 device_initialize(&udc->dev);
226 udc->dev.release = usb_udc_release;
227 udc->dev.class = udc_class;
228 udc->dev.groups = usb_udc_attr_groups;
229 udc->dev.parent = parent;
230 ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
231 if (ret)
232 goto err3;
234 udc->gadget = gadget;
236 mutex_lock(&udc_lock);
237 list_add_tail(&udc->list, &udc_list);
239 ret = device_add(&udc->dev);
240 if (ret)
241 goto err4;
243 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
245 mutex_unlock(&udc_lock);
247 return 0;
249 err4:
250 list_del(&udc->list);
251 mutex_unlock(&udc_lock);
253 err3:
254 put_device(&udc->dev);
256 err2:
257 put_device(&gadget->dev);
258 kfree(udc);
260 err1:
261 return ret;
263 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
266 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
267 * @parent: the parent device to this udc. Usually the controller
268 * driver's device.
269 * @gadget: the gadget to be added to the list
271 * Returns zero on success, negative errno otherwise.
273 int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
275 return usb_add_gadget_udc_release(parent, gadget, NULL);
277 EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
279 static void usb_gadget_remove_driver(struct usb_udc *udc)
281 dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
282 udc->gadget->name);
284 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
286 usb_gadget_disconnect(udc->gadget);
287 udc->driver->disconnect(udc->gadget);
288 udc->driver->unbind(udc->gadget);
289 usb_gadget_udc_stop(udc->gadget, NULL);
291 udc->driver = NULL;
292 udc->dev.driver = NULL;
293 udc->gadget->dev.driver = NULL;
297 * usb_del_gadget_udc - deletes @udc from udc_list
298 * @gadget: the gadget to be removed.
300 * This, will call usb_gadget_unregister_driver() if
301 * the @udc is still busy.
303 void usb_del_gadget_udc(struct usb_gadget *gadget)
305 struct usb_udc *udc = NULL;
307 mutex_lock(&udc_lock);
308 list_for_each_entry(udc, &udc_list, list)
309 if (udc->gadget == gadget)
310 goto found;
312 dev_err(gadget->dev.parent, "gadget not registered.\n");
313 mutex_unlock(&udc_lock);
315 return;
317 found:
318 dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
320 list_del(&udc->list);
321 mutex_unlock(&udc_lock);
323 if (udc->driver)
324 usb_gadget_remove_driver(udc);
326 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
327 flush_work(&gadget->work);
328 device_unregister(&udc->dev);
329 device_unregister(&gadget->dev);
331 EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
333 /* ------------------------------------------------------------------------- */
335 static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
337 int ret;
339 dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
340 driver->function);
342 udc->driver = driver;
343 udc->dev.driver = &driver->driver;
344 udc->gadget->dev.driver = &driver->driver;
346 ret = driver->bind(udc->gadget, driver);
347 if (ret)
348 goto err1;
349 ret = usb_gadget_udc_start(udc->gadget, driver);
350 if (ret) {
351 driver->unbind(udc->gadget);
352 goto err1;
354 usb_gadget_connect(udc->gadget);
356 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
357 return 0;
358 err1:
359 if (ret != -EISNAM)
360 dev_err(&udc->dev, "failed to start %s: %d\n",
361 udc->driver->function, ret);
362 udc->driver = NULL;
363 udc->dev.driver = NULL;
364 udc->gadget->dev.driver = NULL;
365 return ret;
368 int udc_attach_driver(const char *name, struct usb_gadget_driver *driver)
370 struct usb_udc *udc = NULL;
371 int ret = -ENODEV;
373 mutex_lock(&udc_lock);
374 list_for_each_entry(udc, &udc_list, list) {
375 ret = strcmp(name, dev_name(&udc->dev));
376 if (!ret)
377 break;
379 if (ret) {
380 ret = -ENODEV;
381 goto out;
383 if (udc->driver) {
384 ret = -EBUSY;
385 goto out;
387 ret = udc_bind_to_driver(udc, driver);
388 out:
389 mutex_unlock(&udc_lock);
390 return ret;
392 EXPORT_SYMBOL_GPL(udc_attach_driver);
394 int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
396 struct usb_udc *udc = NULL;
397 int ret;
399 if (!driver || !driver->bind || !driver->setup)
400 return -EINVAL;
402 mutex_lock(&udc_lock);
403 list_for_each_entry(udc, &udc_list, list) {
404 /* For now we take the first one */
405 if (!udc->driver)
406 goto found;
409 pr_debug("couldn't find an available UDC\n");
410 mutex_unlock(&udc_lock);
411 return -ENODEV;
412 found:
413 ret = udc_bind_to_driver(udc, driver);
414 mutex_unlock(&udc_lock);
415 return ret;
417 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
419 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
421 struct usb_udc *udc = NULL;
422 int ret = -ENODEV;
424 if (!driver || !driver->unbind)
425 return -EINVAL;
427 mutex_lock(&udc_lock);
428 list_for_each_entry(udc, &udc_list, list)
429 if (udc->driver == driver) {
430 usb_gadget_remove_driver(udc);
431 ret = 0;
432 break;
435 mutex_unlock(&udc_lock);
436 return ret;
438 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
440 /* ------------------------------------------------------------------------- */
442 static ssize_t usb_udc_srp_store(struct device *dev,
443 struct device_attribute *attr, const char *buf, size_t n)
445 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
447 if (sysfs_streq(buf, "1"))
448 usb_gadget_wakeup(udc->gadget);
450 return n;
452 static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
454 static ssize_t usb_udc_softconn_store(struct device *dev,
455 struct device_attribute *attr, const char *buf, size_t n)
457 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
459 if (sysfs_streq(buf, "connect")) {
460 usb_gadget_udc_start(udc->gadget, udc->driver);
461 usb_gadget_connect(udc->gadget);
462 } else if (sysfs_streq(buf, "disconnect")) {
463 usb_gadget_disconnect(udc->gadget);
464 usb_gadget_udc_stop(udc->gadget, udc->driver);
465 } else {
466 dev_err(dev, "unsupported command '%s'\n", buf);
467 return -EINVAL;
470 return n;
472 static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
474 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
475 char *buf)
477 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
478 struct usb_gadget *gadget = udc->gadget;
480 return sprintf(buf, "%s\n", usb_state_string(gadget->state));
482 static DEVICE_ATTR_RO(state);
484 #define USB_UDC_SPEED_ATTR(name, param) \
485 ssize_t name##_show(struct device *dev, \
486 struct device_attribute *attr, char *buf) \
488 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
489 return snprintf(buf, PAGE_SIZE, "%s\n", \
490 usb_speed_string(udc->gadget->param)); \
492 static DEVICE_ATTR_RO(name)
494 static USB_UDC_SPEED_ATTR(current_speed, speed);
495 static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
497 #define USB_UDC_ATTR(name) \
498 ssize_t name##_show(struct device *dev, \
499 struct device_attribute *attr, char *buf) \
501 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
502 struct usb_gadget *gadget = udc->gadget; \
504 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
506 static DEVICE_ATTR_RO(name)
508 static USB_UDC_ATTR(is_otg);
509 static USB_UDC_ATTR(is_a_peripheral);
510 static USB_UDC_ATTR(b_hnp_enable);
511 static USB_UDC_ATTR(a_hnp_support);
512 static USB_UDC_ATTR(a_alt_hnp_support);
514 static struct attribute *usb_udc_attrs[] = {
515 &dev_attr_srp.attr,
516 &dev_attr_soft_connect.attr,
517 &dev_attr_state.attr,
518 &dev_attr_current_speed.attr,
519 &dev_attr_maximum_speed.attr,
521 &dev_attr_is_otg.attr,
522 &dev_attr_is_a_peripheral.attr,
523 &dev_attr_b_hnp_enable.attr,
524 &dev_attr_a_hnp_support.attr,
525 &dev_attr_a_alt_hnp_support.attr,
526 NULL,
529 static const struct attribute_group usb_udc_attr_group = {
530 .attrs = usb_udc_attrs,
533 static const struct attribute_group *usb_udc_attr_groups[] = {
534 &usb_udc_attr_group,
535 NULL,
538 static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
540 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
541 int ret;
543 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
544 if (ret) {
545 dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
546 return ret;
549 if (udc->driver) {
550 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
551 udc->driver->function);
552 if (ret) {
553 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
554 return ret;
558 return 0;
561 static int __init usb_udc_init(void)
563 udc_class = class_create(THIS_MODULE, "udc");
564 if (IS_ERR(udc_class)) {
565 pr_err("failed to create udc class --> %ld\n",
566 PTR_ERR(udc_class));
567 return PTR_ERR(udc_class);
570 udc_class->dev_uevent = usb_udc_uevent;
571 return 0;
573 subsys_initcall(usb_udc_init);
575 static void __exit usb_udc_exit(void)
577 class_destroy(udc_class);
579 module_exit(usb_udc_exit);
581 MODULE_DESCRIPTION("UDC Framework");
582 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
583 MODULE_LICENSE("GPL v2");