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>
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.
45 struct usb_gadget_driver
*driver
;
46 struct usb_gadget
*gadget
;
48 struct list_head list
;
52 static struct class *udc_class
;
53 static LIST_HEAD(udc_list
);
54 static DEFINE_MUTEX(udc_lock
);
56 /* ------------------------------------------------------------------------- */
60 int usb_gadget_map_request(struct usb_gadget
*gadget
,
61 struct usb_request
*req
, int is_in
)
69 mapped
= dma_map_sg(&gadget
->dev
, req
->sg
, req
->num_sgs
,
70 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
72 dev_err(&gadget
->dev
, "failed to map SGs\n");
76 req
->num_mapped_sgs
= mapped
;
78 req
->dma
= dma_map_single(&gadget
->dev
, req
->buf
, req
->length
,
79 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
81 if (dma_mapping_error(&gadget
->dev
, req
->dma
)) {
82 dev_err(&gadget
->dev
, "failed to map buffer\n");
89 EXPORT_SYMBOL_GPL(usb_gadget_map_request
);
91 void usb_gadget_unmap_request(struct usb_gadget
*gadget
,
92 struct usb_request
*req
, int is_in
)
97 if (req
->num_mapped_sgs
) {
98 dma_unmap_sg(&gadget
->dev
, req
->sg
, req
->num_mapped_sgs
,
99 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
101 req
->num_mapped_sgs
= 0;
103 dma_unmap_single(&gadget
->dev
, req
->dma
, req
->length
,
104 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
107 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request
);
109 #endif /* CONFIG_HAS_DMA */
111 /* ------------------------------------------------------------------------- */
114 * usb_gadget_giveback_request - give the request back to the gadget layer
115 * Context: in_interrupt()
117 * This is called by device controller drivers in order to return the
118 * completed request back to the gadget layer.
120 void usb_gadget_giveback_request(struct usb_ep
*ep
,
121 struct usb_request
*req
)
123 if (likely(req
->status
== 0))
124 usb_led_activity(USB_LED_EVENT_GADGET
);
126 req
->complete(ep
, req
);
128 EXPORT_SYMBOL_GPL(usb_gadget_giveback_request
);
130 /* ------------------------------------------------------------------------- */
132 static void usb_gadget_state_work(struct work_struct
*work
)
134 struct usb_gadget
*gadget
= work_to_gadget(work
);
135 struct usb_udc
*udc
= gadget
->udc
;
138 sysfs_notify(&udc
->dev
.kobj
, NULL
, "state");
141 void usb_gadget_set_state(struct usb_gadget
*gadget
,
142 enum usb_device_state state
)
144 gadget
->state
= state
;
145 schedule_work(&gadget
->work
);
147 EXPORT_SYMBOL_GPL(usb_gadget_set_state
);
149 /* ------------------------------------------------------------------------- */
151 static void usb_udc_connect_control(struct usb_udc
*udc
)
154 usb_gadget_connect(udc
->gadget
);
156 usb_gadget_disconnect(udc
->gadget
);
160 * usb_udc_vbus_handler - updates the udc core vbus status, and try to
161 * connect or disconnect gadget
162 * @gadget: The gadget which vbus change occurs
163 * @status: The vbus status
165 * The udc driver calls it when it wants to connect or disconnect gadget
166 * according to vbus status.
168 void usb_udc_vbus_handler(struct usb_gadget
*gadget
, bool status
)
170 struct usb_udc
*udc
= gadget
->udc
;
174 usb_udc_connect_control(udc
);
177 EXPORT_SYMBOL_GPL(usb_udc_vbus_handler
);
180 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
181 * @gadget: The gadget which bus reset occurs
182 * @driver: The gadget driver we want to notify
184 * If the udc driver has bus reset handler, it needs to call this when the bus
185 * reset occurs, it notifies the gadget driver that the bus reset occurs as
186 * well as updates gadget state.
188 void usb_gadget_udc_reset(struct usb_gadget
*gadget
,
189 struct usb_gadget_driver
*driver
)
191 driver
->reset(gadget
);
192 usb_gadget_set_state(gadget
, USB_STATE_DEFAULT
);
194 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset
);
197 * usb_gadget_udc_start - tells usb device controller to start up
198 * @udc: The UDC to be started
200 * This call is issued by the UDC Class driver when it's about
201 * to register a gadget driver to the device controller, before
202 * calling gadget driver's bind() method.
204 * It allows the controller to be powered off until strictly
205 * necessary to have it powered on.
207 * Returns zero on success, else negative errno.
209 static inline int usb_gadget_udc_start(struct usb_udc
*udc
)
211 return udc
->gadget
->ops
->udc_start(udc
->gadget
, udc
->driver
);
215 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
216 * @gadget: The device we want to stop activity
217 * @driver: The driver to unbind from @gadget
219 * This call is issued by the UDC Class driver after calling
220 * gadget driver's unbind() method.
222 * The details are implementation specific, but it can go as
223 * far as powering off UDC completely and disable its data
226 static inline void usb_gadget_udc_stop(struct usb_udc
*udc
)
228 udc
->gadget
->ops
->udc_stop(udc
->gadget
);
232 * usb_udc_release - release the usb_udc struct
233 * @dev: the dev member within usb_udc
235 * This is called by driver's core in order to free memory once the last
236 * reference is released.
238 static void usb_udc_release(struct device
*dev
)
242 udc
= container_of(dev
, struct usb_udc
, dev
);
243 dev_dbg(dev
, "releasing '%s'\n", dev_name(dev
));
247 static const struct attribute_group
*usb_udc_attr_groups
[];
249 static void usb_udc_nop_release(struct device
*dev
)
251 dev_vdbg(dev
, "%s\n", __func__
);
255 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
256 * @parent: the parent device to this udc. Usually the controller driver's
258 * @gadget: the gadget to be added to the list.
259 * @release: a gadget release function.
261 * Returns zero on success, negative errno otherwise.
263 int usb_add_gadget_udc_release(struct device
*parent
, struct usb_gadget
*gadget
,
264 void (*release
)(struct device
*dev
))
269 udc
= kzalloc(sizeof(*udc
), GFP_KERNEL
);
273 dev_set_name(&gadget
->dev
, "gadget");
274 INIT_WORK(&gadget
->work
, usb_gadget_state_work
);
275 gadget
->dev
.parent
= parent
;
277 #ifdef CONFIG_HAS_DMA
278 dma_set_coherent_mask(&gadget
->dev
, parent
->coherent_dma_mask
);
279 gadget
->dev
.dma_parms
= parent
->dma_parms
;
280 gadget
->dev
.dma_mask
= parent
->dma_mask
;
284 gadget
->dev
.release
= release
;
286 gadget
->dev
.release
= usb_udc_nop_release
;
288 ret
= device_register(&gadget
->dev
);
292 device_initialize(&udc
->dev
);
293 udc
->dev
.release
= usb_udc_release
;
294 udc
->dev
.class = udc_class
;
295 udc
->dev
.groups
= usb_udc_attr_groups
;
296 udc
->dev
.parent
= parent
;
297 ret
= dev_set_name(&udc
->dev
, "%s", kobject_name(&parent
->kobj
));
301 udc
->gadget
= gadget
;
304 mutex_lock(&udc_lock
);
305 list_add_tail(&udc
->list
, &udc_list
);
307 ret
= device_add(&udc
->dev
);
311 usb_gadget_set_state(gadget
, USB_STATE_NOTATTACHED
);
314 mutex_unlock(&udc_lock
);
319 list_del(&udc
->list
);
320 mutex_unlock(&udc_lock
);
323 put_device(&udc
->dev
);
326 put_device(&gadget
->dev
);
332 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release
);
335 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
336 * @parent: the parent device to this udc. Usually the controller
338 * @gadget: the gadget to be added to the list
340 * Returns zero on success, negative errno otherwise.
342 int usb_add_gadget_udc(struct device
*parent
, struct usb_gadget
*gadget
)
344 return usb_add_gadget_udc_release(parent
, gadget
, NULL
);
346 EXPORT_SYMBOL_GPL(usb_add_gadget_udc
);
348 static void usb_gadget_remove_driver(struct usb_udc
*udc
)
350 dev_dbg(&udc
->dev
, "unregistering UDC driver [%s]\n",
351 udc
->driver
->function
);
353 kobject_uevent(&udc
->dev
.kobj
, KOBJ_CHANGE
);
355 usb_gadget_disconnect(udc
->gadget
);
356 udc
->driver
->disconnect(udc
->gadget
);
357 udc
->driver
->unbind(udc
->gadget
);
358 usb_gadget_udc_stop(udc
);
361 udc
->dev
.driver
= NULL
;
362 udc
->gadget
->dev
.driver
= NULL
;
366 * usb_del_gadget_udc - deletes @udc from udc_list
367 * @gadget: the gadget to be removed.
369 * This, will call usb_gadget_unregister_driver() if
370 * the @udc is still busy.
372 void usb_del_gadget_udc(struct usb_gadget
*gadget
)
374 struct usb_udc
*udc
= gadget
->udc
;
379 dev_vdbg(gadget
->dev
.parent
, "unregistering gadget\n");
381 mutex_lock(&udc_lock
);
382 list_del(&udc
->list
);
383 mutex_unlock(&udc_lock
);
386 usb_gadget_remove_driver(udc
);
388 kobject_uevent(&udc
->dev
.kobj
, KOBJ_REMOVE
);
389 flush_work(&gadget
->work
);
390 device_unregister(&udc
->dev
);
391 device_unregister(&gadget
->dev
);
393 EXPORT_SYMBOL_GPL(usb_del_gadget_udc
);
395 /* ------------------------------------------------------------------------- */
397 static int udc_bind_to_driver(struct usb_udc
*udc
, struct usb_gadget_driver
*driver
)
401 dev_dbg(&udc
->dev
, "registering UDC driver [%s]\n",
404 udc
->driver
= driver
;
405 udc
->dev
.driver
= &driver
->driver
;
406 udc
->gadget
->dev
.driver
= &driver
->driver
;
408 ret
= driver
->bind(udc
->gadget
, driver
);
411 ret
= usb_gadget_udc_start(udc
);
413 driver
->unbind(udc
->gadget
);
416 usb_udc_connect_control(udc
);
418 kobject_uevent(&udc
->dev
.kobj
, KOBJ_CHANGE
);
422 dev_err(&udc
->dev
, "failed to start %s: %d\n",
423 udc
->driver
->function
, ret
);
425 udc
->dev
.driver
= NULL
;
426 udc
->gadget
->dev
.driver
= NULL
;
430 int usb_udc_attach_driver(const char *name
, struct usb_gadget_driver
*driver
)
432 struct usb_udc
*udc
= NULL
;
435 mutex_lock(&udc_lock
);
436 list_for_each_entry(udc
, &udc_list
, list
) {
437 ret
= strcmp(name
, dev_name(&udc
->dev
));
449 ret
= udc_bind_to_driver(udc
, driver
);
451 mutex_unlock(&udc_lock
);
454 EXPORT_SYMBOL_GPL(usb_udc_attach_driver
);
456 int usb_gadget_probe_driver(struct usb_gadget_driver
*driver
)
458 struct usb_udc
*udc
= NULL
;
461 if (!driver
|| !driver
->bind
|| !driver
->setup
)
464 mutex_lock(&udc_lock
);
465 list_for_each_entry(udc
, &udc_list
, list
) {
466 /* For now we take the first one */
471 pr_debug("couldn't find an available UDC\n");
472 mutex_unlock(&udc_lock
);
475 ret
= udc_bind_to_driver(udc
, driver
);
476 mutex_unlock(&udc_lock
);
479 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver
);
481 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
483 struct usb_udc
*udc
= NULL
;
486 if (!driver
|| !driver
->unbind
)
489 mutex_lock(&udc_lock
);
490 list_for_each_entry(udc
, &udc_list
, list
)
491 if (udc
->driver
== driver
) {
492 usb_gadget_remove_driver(udc
);
493 usb_gadget_set_state(udc
->gadget
,
494 USB_STATE_NOTATTACHED
);
499 mutex_unlock(&udc_lock
);
502 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver
);
504 /* ------------------------------------------------------------------------- */
506 static ssize_t
usb_udc_srp_store(struct device
*dev
,
507 struct device_attribute
*attr
, const char *buf
, size_t n
)
509 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
511 if (sysfs_streq(buf
, "1"))
512 usb_gadget_wakeup(udc
->gadget
);
516 static DEVICE_ATTR(srp
, S_IWUSR
, NULL
, usb_udc_srp_store
);
518 static ssize_t
usb_udc_softconn_store(struct device
*dev
,
519 struct device_attribute
*attr
, const char *buf
, size_t n
)
521 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
524 dev_err(dev
, "soft-connect without a gadget driver\n");
528 if (sysfs_streq(buf
, "connect")) {
529 usb_gadget_udc_start(udc
);
530 usb_gadget_connect(udc
->gadget
);
531 } else if (sysfs_streq(buf
, "disconnect")) {
532 usb_gadget_disconnect(udc
->gadget
);
533 udc
->driver
->disconnect(udc
->gadget
);
534 usb_gadget_udc_stop(udc
);
536 dev_err(dev
, "unsupported command '%s'\n", buf
);
542 static DEVICE_ATTR(soft_connect
, S_IWUSR
, NULL
, usb_udc_softconn_store
);
544 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
547 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
548 struct usb_gadget
*gadget
= udc
->gadget
;
550 return sprintf(buf
, "%s\n", usb_state_string(gadget
->state
));
552 static DEVICE_ATTR_RO(state
);
554 #define USB_UDC_SPEED_ATTR(name, param) \
555 ssize_t name##_show(struct device *dev, \
556 struct device_attribute *attr, char *buf) \
558 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
559 return snprintf(buf, PAGE_SIZE, "%s\n", \
560 usb_speed_string(udc->gadget->param)); \
562 static DEVICE_ATTR_RO(name)
564 static USB_UDC_SPEED_ATTR(current_speed
, speed
);
565 static USB_UDC_SPEED_ATTR(maximum_speed
, max_speed
);
567 #define USB_UDC_ATTR(name) \
568 ssize_t name##_show(struct device *dev, \
569 struct device_attribute *attr, char *buf) \
571 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
572 struct usb_gadget *gadget = udc->gadget; \
574 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
576 static DEVICE_ATTR_RO(name)
578 static USB_UDC_ATTR(is_otg
);
579 static USB_UDC_ATTR(is_a_peripheral
);
580 static USB_UDC_ATTR(b_hnp_enable
);
581 static USB_UDC_ATTR(a_hnp_support
);
582 static USB_UDC_ATTR(a_alt_hnp_support
);
583 static USB_UDC_ATTR(is_selfpowered
);
585 static struct attribute
*usb_udc_attrs
[] = {
587 &dev_attr_soft_connect
.attr
,
588 &dev_attr_state
.attr
,
589 &dev_attr_current_speed
.attr
,
590 &dev_attr_maximum_speed
.attr
,
592 &dev_attr_is_otg
.attr
,
593 &dev_attr_is_a_peripheral
.attr
,
594 &dev_attr_b_hnp_enable
.attr
,
595 &dev_attr_a_hnp_support
.attr
,
596 &dev_attr_a_alt_hnp_support
.attr
,
597 &dev_attr_is_selfpowered
.attr
,
601 static const struct attribute_group usb_udc_attr_group
= {
602 .attrs
= usb_udc_attrs
,
605 static const struct attribute_group
*usb_udc_attr_groups
[] = {
610 static int usb_udc_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
612 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
615 ret
= add_uevent_var(env
, "USB_UDC_NAME=%s", udc
->gadget
->name
);
617 dev_err(dev
, "failed to add uevent USB_UDC_NAME\n");
622 ret
= add_uevent_var(env
, "USB_UDC_DRIVER=%s",
623 udc
->driver
->function
);
625 dev_err(dev
, "failed to add uevent USB_UDC_DRIVER\n");
633 static int __init
usb_udc_init(void)
635 udc_class
= class_create(THIS_MODULE
, "udc");
636 if (IS_ERR(udc_class
)) {
637 pr_err("failed to create udc class --> %ld\n",
639 return PTR_ERR(udc_class
);
642 udc_class
->dev_uevent
= usb_udc_uevent
;
645 subsys_initcall(usb_udc_init
);
647 static void __exit
usb_udc_exit(void)
649 class_destroy(udc_class
);
651 module_exit(usb_udc_exit
);
653 MODULE_DESCRIPTION("UDC Framework");
654 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
655 MODULE_LICENSE("GPL v2");