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>
27 #include <linux/usb/ch9.h>
28 #include <linux/usb/gadget.h>
31 * struct usb_udc - describes one usb device controller
32 * @driver - the gadget driver pointer. For use by the class code
33 * @dev - the child device to the actual controller
34 * @gadget - the gadget. For use by the class code
35 * @list - for use by the udc class driver
37 * This represents the internal data structure which is used by the UDC-class
38 * to hold information about udc driver and gadget together.
41 struct usb_gadget_driver *driver;
42 struct usb_gadget *gadget;
44 struct list_head list;
47 static struct class *udc_class;
48 static LIST_HEAD(udc_list);
49 static DEFINE_MUTEX(udc_lock);
51 /* ------------------------------------------------------------------------- */
55 int usb_gadget_map_request(struct usb_gadget *gadget,
56 struct usb_request *req, int is_in)
64 mapped = dma_map_sg(&gadget->dev, req->sg, req->num_sgs,
65 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
67 dev_err(&gadget->dev, "failed to map SGs\n");
71 req->num_mapped_sgs = mapped;
73 req->dma = dma_map_single(&gadget->dev, req->buf, req->length,
74 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
76 if (dma_mapping_error(&gadget->dev, req->dma)) {
77 dev_err(&gadget->dev, "failed to map buffer\n");
84 EXPORT_SYMBOL_GPL(usb_gadget_map_request);
86 void usb_gadget_unmap_request(struct usb_gadget *gadget,
87 struct usb_request *req, int is_in)
92 if (req->num_mapped_sgs) {
93 dma_unmap_sg(&gadget->dev, req->sg, req->num_mapped_sgs,
94 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
96 req->num_mapped_sgs = 0;
98 dma_unmap_single(&gadget->dev, req->dma, req->length,
99 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
102 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
104 #endif /* CONFIG_HAS_DMA */
106 /* ------------------------------------------------------------------------- */
108 void usb_gadget_set_state(struct usb_gadget *gadget,
109 enum usb_device_state state)
111 gadget->state = state;
112 sysfs_notify(&gadget->dev.kobj, NULL, "status");
114 EXPORT_SYMBOL_GPL(usb_gadget_set_state);
116 /* ------------------------------------------------------------------------- */
119 * usb_gadget_udc_start - tells usb device controller to start up
120 * @gadget: The gadget we want to get started
121 * @driver: The driver we want to bind to @gadget
123 * This call is issued by the UDC Class driver when it's about
124 * to register a gadget driver to the device controller, before
125 * calling gadget driver's bind() method.
127 * It allows the controller to be powered off until strictly
128 * necessary to have it powered on.
130 * Returns zero on success, else negative errno.
132 static inline int usb_gadget_udc_start(struct usb_gadget *gadget,
133 struct usb_gadget_driver *driver)
135 return gadget->ops->udc_start(gadget, driver);
139 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
140 * @gadget: The device we want to stop activity
141 * @driver: The driver to unbind from @gadget
143 * This call is issued by the UDC Class driver after calling
144 * gadget driver's unbind() method.
146 * The details are implementation specific, but it can go as
147 * far as powering off UDC completely and disable its data
150 static inline void usb_gadget_udc_stop(struct usb_gadget *gadget,
151 struct usb_gadget_driver *driver)
153 gadget->ops->udc_stop(gadget, driver);
157 * usb_udc_release - release the usb_udc struct
158 * @dev: the dev member within usb_udc
160 * This is called by driver's core in order to free memory once the last
161 * reference is released.
163 static void usb_udc_release(struct device *dev)
167 udc = container_of(dev, struct usb_udc, dev);
168 dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
172 static const struct attribute_group *usb_udc_attr_groups[];
174 static void usb_udc_nop_release(struct device *dev)
176 dev_vdbg(dev, "%s\n", __func__);
180 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
181 * @parent: the parent device to this udc. Usually the controller driver's
183 * @gadget: the gadget to be added to the list.
184 * @release: a gadget release function.
186 * Returns zero on success, negative errno otherwise.
188 int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
189 void (*release)(struct device *dev))
194 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
198 dev_set_name(&gadget->dev, "gadget");
199 gadget->dev.parent = parent;
201 #ifdef CONFIG_HAS_DMA
202 dma_set_coherent_mask(&gadget->dev, parent->coherent_dma_mask);
203 gadget->dev.dma_parms = parent->dma_parms;
204 gadget->dev.dma_mask = parent->dma_mask;
208 gadget->dev.release = release;
210 gadget->dev.release = usb_udc_nop_release;
212 ret = device_register(&gadget->dev);
216 device_initialize(&udc->dev);
217 udc->dev.release = usb_udc_release;
218 udc->dev.class = udc_class;
219 udc->dev.groups = usb_udc_attr_groups;
220 udc->dev.parent = parent;
221 ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
225 udc->gadget = gadget;
227 mutex_lock(&udc_lock);
228 list_add_tail(&udc->list, &udc_list);
230 ret = device_add(&udc->dev);
234 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
236 mutex_unlock(&udc_lock);
241 list_del(&udc->list);
242 mutex_unlock(&udc_lock);
245 put_device(&udc->dev);
248 put_device(&gadget->dev);
254 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
257 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
258 * @parent: the parent device to this udc. Usually the controller
260 * @gadget: the gadget to be added to the list
262 * Returns zero on success, negative errno otherwise.
264 int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
266 return usb_add_gadget_udc_release(parent, gadget, NULL);
268 EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
270 static void usb_gadget_remove_driver(struct usb_udc *udc)
272 dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
275 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
277 usb_gadget_disconnect(udc->gadget);
278 udc->driver->disconnect(udc->gadget);
279 udc->driver->unbind(udc->gadget);
280 usb_gadget_udc_stop(udc->gadget, NULL);
283 udc->dev.driver = NULL;
284 udc->gadget->dev.driver = NULL;
288 * usb_del_gadget_udc - deletes @udc from udc_list
289 * @gadget: the gadget to be removed.
291 * This, will call usb_gadget_unregister_driver() if
292 * the @udc is still busy.
294 void usb_del_gadget_udc(struct usb_gadget *gadget)
296 struct usb_udc *udc = NULL;
298 mutex_lock(&udc_lock);
299 list_for_each_entry(udc, &udc_list, list)
300 if (udc->gadget == gadget)
303 dev_err(gadget->dev.parent, "gadget not registered.\n");
304 mutex_unlock(&udc_lock);
309 dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
311 list_del(&udc->list);
312 mutex_unlock(&udc_lock);
315 usb_gadget_remove_driver(udc);
317 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
318 device_unregister(&udc->dev);
319 device_unregister(&gadget->dev);
321 EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
323 /* ------------------------------------------------------------------------- */
325 static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
329 dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
332 udc->driver = driver;
333 udc->dev.driver = &driver->driver;
334 udc->gadget->dev.driver = &driver->driver;
336 ret = driver->bind(udc->gadget, driver);
339 ret = usb_gadget_udc_start(udc->gadget, driver);
341 driver->unbind(udc->gadget);
344 usb_gadget_connect(udc->gadget);
346 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
349 dev_err(&udc->dev, "failed to start %s: %d\n",
350 udc->driver->function, ret);
352 udc->dev.driver = NULL;
353 udc->gadget->dev.driver = NULL;
357 int udc_attach_driver(const char *name, struct usb_gadget_driver *driver)
359 struct usb_udc *udc = NULL;
362 mutex_lock(&udc_lock);
363 list_for_each_entry(udc, &udc_list, list) {
364 ret = strcmp(name, dev_name(&udc->dev));
376 ret = udc_bind_to_driver(udc, driver);
378 mutex_unlock(&udc_lock);
381 EXPORT_SYMBOL_GPL(udc_attach_driver);
383 int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
385 struct usb_udc *udc = NULL;
388 if (!driver || !driver->bind || !driver->setup)
391 mutex_lock(&udc_lock);
392 list_for_each_entry(udc, &udc_list, list) {
393 /* For now we take the first one */
398 pr_debug("couldn't find an available UDC\n");
399 mutex_unlock(&udc_lock);
402 ret = udc_bind_to_driver(udc, driver);
403 mutex_unlock(&udc_lock);
406 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
408 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
410 struct usb_udc *udc = NULL;
413 if (!driver || !driver->unbind)
416 mutex_lock(&udc_lock);
417 list_for_each_entry(udc, &udc_list, list)
418 if (udc->driver == driver) {
419 usb_gadget_remove_driver(udc);
424 mutex_unlock(&udc_lock);
427 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
429 /* ------------------------------------------------------------------------- */
431 static ssize_t usb_udc_srp_store(struct device *dev,
432 struct device_attribute *attr, const char *buf, size_t n)
434 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
436 if (sysfs_streq(buf, "1"))
437 usb_gadget_wakeup(udc->gadget);
441 static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
443 static ssize_t usb_udc_softconn_store(struct device *dev,
444 struct device_attribute *attr, const char *buf, size_t n)
446 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
448 if (sysfs_streq(buf, "connect")) {
449 usb_gadget_udc_start(udc->gadget, udc->driver);
450 usb_gadget_connect(udc->gadget);
451 } else if (sysfs_streq(buf, "disconnect")) {
452 usb_gadget_disconnect(udc->gadget);
453 usb_gadget_udc_stop(udc->gadget, udc->driver);
455 dev_err(dev, "unsupported command '%s'\n", buf);
461 static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
463 static ssize_t usb_gadget_state_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
466 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
467 struct usb_gadget *gadget = udc->gadget;
469 return sprintf(buf, "%s\n", usb_state_string(gadget->state));
471 static DEVICE_ATTR(state, S_IRUGO, usb_gadget_state_show, NULL);
473 #define USB_UDC_SPEED_ATTR(name, param) \
474 ssize_t usb_udc_##param##_show(struct device *dev, \
475 struct device_attribute *attr, char *buf) \
477 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
478 return snprintf(buf, PAGE_SIZE, "%s\n", \
479 usb_speed_string(udc->gadget->param)); \
481 static DEVICE_ATTR(name, S_IRUGO, usb_udc_##param##_show, NULL)
483 static USB_UDC_SPEED_ATTR(current_speed, speed);
484 static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
486 #define USB_UDC_ATTR(name) \
487 ssize_t usb_udc_##name##_show(struct device *dev, \
488 struct device_attribute *attr, char *buf) \
490 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
491 struct usb_gadget *gadget = udc->gadget; \
493 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
495 static DEVICE_ATTR(name, S_IRUGO, usb_udc_##name##_show, NULL)
497 static USB_UDC_ATTR(is_otg);
498 static USB_UDC_ATTR(is_a_peripheral);
499 static USB_UDC_ATTR(b_hnp_enable);
500 static USB_UDC_ATTR(a_hnp_support);
501 static USB_UDC_ATTR(a_alt_hnp_support);
503 static struct attribute *usb_udc_attrs[] = {
505 &dev_attr_soft_connect.attr,
506 &dev_attr_state.attr,
507 &dev_attr_current_speed.attr,
508 &dev_attr_maximum_speed.attr,
510 &dev_attr_is_otg.attr,
511 &dev_attr_is_a_peripheral.attr,
512 &dev_attr_b_hnp_enable.attr,
513 &dev_attr_a_hnp_support.attr,
514 &dev_attr_a_alt_hnp_support.attr,
518 static const struct attribute_group usb_udc_attr_group = {
519 .attrs = usb_udc_attrs,
522 static const struct attribute_group *usb_udc_attr_groups[] = {
527 static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
529 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
532 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
534 dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
539 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
540 udc->driver->function);
542 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
550 static int __init usb_udc_init(void)
552 udc_class = class_create(THIS_MODULE, "udc");
553 if (IS_ERR(udc_class)) {
554 pr_err("failed to create udc class --> %ld\n",
556 return PTR_ERR(udc_class);
559 udc_class->dev_uevent = usb_udc_uevent;
562 subsys_initcall(usb_udc_init);
564 static void __exit usb_udc_exit(void)
566 class_destroy(udc_class);
568 module_exit(usb_udc_exit);
570 MODULE_DESCRIPTION("UDC Framework");
571 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
572 MODULE_LICENSE("GPL v2");