Linux 4.2.1
[linux/fpc-iii.git] / drivers / usb / gadget / udc / udc-core.c
blob89ed5e71a1991e0cd249c48b18bd04dd67bacf91
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>
30 #include <linux/usb.h>
32 /**
33 * struct usb_udc - describes one usb device controller
34 * @driver - the gadget driver pointer. For use by the class code
35 * @dev - the child device to the actual controller
36 * @gadget - the gadget. For use by the class code
37 * @list - for use by the udc class driver
38 * @vbus - for udcs who care about vbus status, this value is real vbus status;
39 * for udcs who do not care about vbus status, this value is always true
41 * This represents the internal data structure which is used by the UDC-class
42 * to hold information about udc driver and gadget together.
44 struct usb_udc {
45 struct usb_gadget_driver *driver;
46 struct usb_gadget *gadget;
47 struct device dev;
48 struct list_head list;
49 bool vbus;
52 static struct class *udc_class;
53 static LIST_HEAD(udc_list);
54 static DEFINE_MUTEX(udc_lock);
56 /* ------------------------------------------------------------------------- */
58 #ifdef CONFIG_HAS_DMA
60 int usb_gadget_map_request(struct usb_gadget *gadget,
61 struct usb_request *req, int is_in)
63 struct device *dev = gadget->dev.parent;
65 if (req->length == 0)
66 return 0;
68 if (req->num_sgs) {
69 int mapped;
71 mapped = dma_map_sg(dev, req->sg, req->num_sgs,
72 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
73 if (mapped == 0) {
74 dev_err(&gadget->dev, "failed to map SGs\n");
75 return -EFAULT;
78 req->num_mapped_sgs = mapped;
79 } else {
80 req->dma = dma_map_single(dev, req->buf, req->length,
81 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
83 if (dma_mapping_error(dev, req->dma)) {
84 dev_err(dev, "failed to map buffer\n");
85 return -EFAULT;
89 return 0;
91 EXPORT_SYMBOL_GPL(usb_gadget_map_request);
93 void usb_gadget_unmap_request(struct usb_gadget *gadget,
94 struct usb_request *req, int is_in)
96 if (req->length == 0)
97 return;
99 if (req->num_mapped_sgs) {
100 dma_unmap_sg(gadget->dev.parent, req->sg, req->num_mapped_sgs,
101 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
103 req->num_mapped_sgs = 0;
104 } else {
105 dma_unmap_single(gadget->dev.parent, req->dma, req->length,
106 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
109 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
111 #endif /* CONFIG_HAS_DMA */
113 /* ------------------------------------------------------------------------- */
116 * usb_gadget_giveback_request - give the request back to the gadget layer
117 * Context: in_interrupt()
119 * This is called by device controller drivers in order to return the
120 * completed request back to the gadget layer.
122 void usb_gadget_giveback_request(struct usb_ep *ep,
123 struct usb_request *req)
125 if (likely(req->status == 0))
126 usb_led_activity(USB_LED_EVENT_GADGET);
128 req->complete(ep, req);
130 EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
132 /* ------------------------------------------------------------------------- */
134 static void usb_gadget_state_work(struct work_struct *work)
136 struct usb_gadget *gadget = work_to_gadget(work);
137 struct usb_udc *udc = gadget->udc;
139 if (udc)
140 sysfs_notify(&udc->dev.kobj, NULL, "state");
143 void usb_gadget_set_state(struct usb_gadget *gadget,
144 enum usb_device_state state)
146 gadget->state = state;
147 schedule_work(&gadget->work);
149 EXPORT_SYMBOL_GPL(usb_gadget_set_state);
151 /* ------------------------------------------------------------------------- */
153 static void usb_udc_connect_control(struct usb_udc *udc)
155 if (udc->vbus)
156 usb_gadget_connect(udc->gadget);
157 else
158 usb_gadget_disconnect(udc->gadget);
162 * usb_udc_vbus_handler - updates the udc core vbus status, and try to
163 * connect or disconnect gadget
164 * @gadget: The gadget which vbus change occurs
165 * @status: The vbus status
167 * The udc driver calls it when it wants to connect or disconnect gadget
168 * according to vbus status.
170 void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
172 struct usb_udc *udc = gadget->udc;
174 if (udc) {
175 udc->vbus = status;
176 usb_udc_connect_control(udc);
179 EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
182 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
183 * @gadget: The gadget which bus reset occurs
184 * @driver: The gadget driver we want to notify
186 * If the udc driver has bus reset handler, it needs to call this when the bus
187 * reset occurs, it notifies the gadget driver that the bus reset occurs as
188 * well as updates gadget state.
190 void usb_gadget_udc_reset(struct usb_gadget *gadget,
191 struct usb_gadget_driver *driver)
193 driver->reset(gadget);
194 usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
196 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
199 * usb_gadget_udc_start - tells usb device controller to start up
200 * @udc: The UDC to be started
202 * This call is issued by the UDC Class driver when it's about
203 * to register a gadget driver to the device controller, before
204 * calling gadget driver's bind() method.
206 * It allows the controller to be powered off until strictly
207 * necessary to have it powered on.
209 * Returns zero on success, else negative errno.
211 static inline int usb_gadget_udc_start(struct usb_udc *udc)
213 return udc->gadget->ops->udc_start(udc->gadget, udc->driver);
217 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
218 * @gadget: The device we want to stop activity
219 * @driver: The driver to unbind from @gadget
221 * This call is issued by the UDC Class driver after calling
222 * gadget driver's unbind() method.
224 * The details are implementation specific, but it can go as
225 * far as powering off UDC completely and disable its data
226 * line pullups.
228 static inline void usb_gadget_udc_stop(struct usb_udc *udc)
230 udc->gadget->ops->udc_stop(udc->gadget);
234 * usb_udc_release - release the usb_udc struct
235 * @dev: the dev member within usb_udc
237 * This is called by driver's core in order to free memory once the last
238 * reference is released.
240 static void usb_udc_release(struct device *dev)
242 struct usb_udc *udc;
244 udc = container_of(dev, struct usb_udc, dev);
245 dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
246 kfree(udc);
249 static const struct attribute_group *usb_udc_attr_groups[];
251 static void usb_udc_nop_release(struct device *dev)
253 dev_vdbg(dev, "%s\n", __func__);
257 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
258 * @parent: the parent device to this udc. Usually the controller driver's
259 * device.
260 * @gadget: the gadget to be added to the list.
261 * @release: a gadget release function.
263 * Returns zero on success, negative errno otherwise.
265 int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
266 void (*release)(struct device *dev))
268 struct usb_udc *udc;
269 int ret = -ENOMEM;
271 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
272 if (!udc)
273 goto err1;
275 dev_set_name(&gadget->dev, "gadget");
276 INIT_WORK(&gadget->work, usb_gadget_state_work);
277 gadget->dev.parent = parent;
279 #ifdef CONFIG_HAS_DMA
280 dma_set_coherent_mask(&gadget->dev, parent->coherent_dma_mask);
281 gadget->dev.dma_parms = parent->dma_parms;
282 gadget->dev.dma_mask = parent->dma_mask;
283 #endif
285 if (release)
286 gadget->dev.release = release;
287 else
288 gadget->dev.release = usb_udc_nop_release;
290 ret = device_register(&gadget->dev);
291 if (ret)
292 goto err2;
294 device_initialize(&udc->dev);
295 udc->dev.release = usb_udc_release;
296 udc->dev.class = udc_class;
297 udc->dev.groups = usb_udc_attr_groups;
298 udc->dev.parent = parent;
299 ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
300 if (ret)
301 goto err3;
303 udc->gadget = gadget;
304 gadget->udc = udc;
306 mutex_lock(&udc_lock);
307 list_add_tail(&udc->list, &udc_list);
309 ret = device_add(&udc->dev);
310 if (ret)
311 goto err4;
313 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
314 udc->vbus = true;
316 mutex_unlock(&udc_lock);
318 return 0;
320 err4:
321 list_del(&udc->list);
322 mutex_unlock(&udc_lock);
324 err3:
325 put_device(&udc->dev);
326 device_del(&gadget->dev);
328 err2:
329 put_device(&gadget->dev);
330 kfree(udc);
332 err1:
333 return ret;
335 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
338 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
339 * @parent: the parent device to this udc. Usually the controller
340 * driver's device.
341 * @gadget: the gadget to be added to the list
343 * Returns zero on success, negative errno otherwise.
345 int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
347 return usb_add_gadget_udc_release(parent, gadget, NULL);
349 EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
351 static void usb_gadget_remove_driver(struct usb_udc *udc)
353 dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
354 udc->driver->function);
356 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
358 usb_gadget_disconnect(udc->gadget);
359 udc->driver->disconnect(udc->gadget);
360 udc->driver->unbind(udc->gadget);
361 usb_gadget_udc_stop(udc);
363 udc->driver = NULL;
364 udc->dev.driver = NULL;
365 udc->gadget->dev.driver = NULL;
369 * usb_del_gadget_udc - deletes @udc from udc_list
370 * @gadget: the gadget to be removed.
372 * This, will call usb_gadget_unregister_driver() if
373 * the @udc is still busy.
375 void usb_del_gadget_udc(struct usb_gadget *gadget)
377 struct usb_udc *udc = gadget->udc;
379 if (!udc)
380 return;
382 dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
384 mutex_lock(&udc_lock);
385 list_del(&udc->list);
386 mutex_unlock(&udc_lock);
388 if (udc->driver)
389 usb_gadget_remove_driver(udc);
391 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
392 flush_work(&gadget->work);
393 device_unregister(&udc->dev);
394 device_unregister(&gadget->dev);
396 EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
398 /* ------------------------------------------------------------------------- */
400 static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
402 int ret;
404 dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
405 driver->function);
407 udc->driver = driver;
408 udc->dev.driver = &driver->driver;
409 udc->gadget->dev.driver = &driver->driver;
411 ret = driver->bind(udc->gadget, driver);
412 if (ret)
413 goto err1;
414 ret = usb_gadget_udc_start(udc);
415 if (ret) {
416 driver->unbind(udc->gadget);
417 goto err1;
419 usb_udc_connect_control(udc);
421 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
422 return 0;
423 err1:
424 if (ret != -EISNAM)
425 dev_err(&udc->dev, "failed to start %s: %d\n",
426 udc->driver->function, ret);
427 udc->driver = NULL;
428 udc->dev.driver = NULL;
429 udc->gadget->dev.driver = NULL;
430 return ret;
433 int usb_udc_attach_driver(const char *name, struct usb_gadget_driver *driver)
435 struct usb_udc *udc = NULL;
436 int ret = -ENODEV;
438 mutex_lock(&udc_lock);
439 list_for_each_entry(udc, &udc_list, list) {
440 ret = strcmp(name, dev_name(&udc->dev));
441 if (!ret)
442 break;
444 if (ret) {
445 ret = -ENODEV;
446 goto out;
448 if (udc->driver) {
449 ret = -EBUSY;
450 goto out;
452 ret = udc_bind_to_driver(udc, driver);
453 out:
454 mutex_unlock(&udc_lock);
455 return ret;
457 EXPORT_SYMBOL_GPL(usb_udc_attach_driver);
459 int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
461 struct usb_udc *udc = NULL;
462 int ret;
464 if (!driver || !driver->bind || !driver->setup)
465 return -EINVAL;
467 mutex_lock(&udc_lock);
468 list_for_each_entry(udc, &udc_list, list) {
469 /* For now we take the first one */
470 if (!udc->driver)
471 goto found;
474 pr_debug("couldn't find an available UDC\n");
475 mutex_unlock(&udc_lock);
476 return -ENODEV;
477 found:
478 ret = udc_bind_to_driver(udc, driver);
479 mutex_unlock(&udc_lock);
480 return ret;
482 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
484 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
486 struct usb_udc *udc = NULL;
487 int ret = -ENODEV;
489 if (!driver || !driver->unbind)
490 return -EINVAL;
492 mutex_lock(&udc_lock);
493 list_for_each_entry(udc, &udc_list, list)
494 if (udc->driver == driver) {
495 usb_gadget_remove_driver(udc);
496 usb_gadget_set_state(udc->gadget,
497 USB_STATE_NOTATTACHED);
498 ret = 0;
499 break;
502 mutex_unlock(&udc_lock);
503 return ret;
505 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
507 /* ------------------------------------------------------------------------- */
509 static ssize_t usb_udc_srp_store(struct device *dev,
510 struct device_attribute *attr, const char *buf, size_t n)
512 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
514 if (sysfs_streq(buf, "1"))
515 usb_gadget_wakeup(udc->gadget);
517 return n;
519 static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
521 static ssize_t usb_udc_softconn_store(struct device *dev,
522 struct device_attribute *attr, const char *buf, size_t n)
524 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
526 if (!udc->driver) {
527 dev_err(dev, "soft-connect without a gadget driver\n");
528 return -EOPNOTSUPP;
531 if (sysfs_streq(buf, "connect")) {
532 usb_gadget_udc_start(udc);
533 usb_gadget_connect(udc->gadget);
534 } else if (sysfs_streq(buf, "disconnect")) {
535 usb_gadget_disconnect(udc->gadget);
536 udc->driver->disconnect(udc->gadget);
537 usb_gadget_udc_stop(udc);
538 } else {
539 dev_err(dev, "unsupported command '%s'\n", buf);
540 return -EINVAL;
543 return n;
545 static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
547 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
548 char *buf)
550 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
551 struct usb_gadget *gadget = udc->gadget;
553 return sprintf(buf, "%s\n", usb_state_string(gadget->state));
555 static DEVICE_ATTR_RO(state);
557 #define USB_UDC_SPEED_ATTR(name, param) \
558 ssize_t name##_show(struct device *dev, \
559 struct device_attribute *attr, char *buf) \
561 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
562 return snprintf(buf, PAGE_SIZE, "%s\n", \
563 usb_speed_string(udc->gadget->param)); \
565 static DEVICE_ATTR_RO(name)
567 static USB_UDC_SPEED_ATTR(current_speed, speed);
568 static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
570 #define USB_UDC_ATTR(name) \
571 ssize_t name##_show(struct device *dev, \
572 struct device_attribute *attr, char *buf) \
574 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
575 struct usb_gadget *gadget = udc->gadget; \
577 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
579 static DEVICE_ATTR_RO(name)
581 static USB_UDC_ATTR(is_otg);
582 static USB_UDC_ATTR(is_a_peripheral);
583 static USB_UDC_ATTR(b_hnp_enable);
584 static USB_UDC_ATTR(a_hnp_support);
585 static USB_UDC_ATTR(a_alt_hnp_support);
586 static USB_UDC_ATTR(is_selfpowered);
588 static struct attribute *usb_udc_attrs[] = {
589 &dev_attr_srp.attr,
590 &dev_attr_soft_connect.attr,
591 &dev_attr_state.attr,
592 &dev_attr_current_speed.attr,
593 &dev_attr_maximum_speed.attr,
595 &dev_attr_is_otg.attr,
596 &dev_attr_is_a_peripheral.attr,
597 &dev_attr_b_hnp_enable.attr,
598 &dev_attr_a_hnp_support.attr,
599 &dev_attr_a_alt_hnp_support.attr,
600 &dev_attr_is_selfpowered.attr,
601 NULL,
604 static const struct attribute_group usb_udc_attr_group = {
605 .attrs = usb_udc_attrs,
608 static const struct attribute_group *usb_udc_attr_groups[] = {
609 &usb_udc_attr_group,
610 NULL,
613 static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
615 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
616 int ret;
618 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
619 if (ret) {
620 dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
621 return ret;
624 if (udc->driver) {
625 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
626 udc->driver->function);
627 if (ret) {
628 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
629 return ret;
633 return 0;
636 static int __init usb_udc_init(void)
638 udc_class = class_create(THIS_MODULE, "udc");
639 if (IS_ERR(udc_class)) {
640 pr_err("failed to create udc class --> %ld\n",
641 PTR_ERR(udc_class));
642 return PTR_ERR(udc_class);
645 udc_class->dev_uevent = usb_udc_uevent;
646 return 0;
648 subsys_initcall(usb_udc_init);
650 static void __exit usb_udc_exit(void)
652 class_destroy(udc_class);
654 module_exit(usb_udc_exit);
656 MODULE_DESCRIPTION("UDC Framework");
657 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
658 MODULE_LICENSE("GPL v2");