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 LIST_HEAD(gadget_driver_pending_list
);
55 static DEFINE_MUTEX(udc_lock
);
57 static int udc_bind_to_driver(struct usb_udc
*udc
,
58 struct usb_gadget_driver
*driver
);
60 /* ------------------------------------------------------------------------- */
64 int usb_gadget_map_request_by_dev(struct device
*dev
,
65 struct usb_request
*req
, int is_in
)
73 mapped
= dma_map_sg(dev
, req
->sg
, req
->num_sgs
,
74 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
76 dev_err(dev
, "failed to map SGs\n");
80 req
->num_mapped_sgs
= mapped
;
82 req
->dma
= dma_map_single(dev
, req
->buf
, req
->length
,
83 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
85 if (dma_mapping_error(dev
, req
->dma
)) {
86 dev_err(dev
, "failed to map buffer\n");
93 EXPORT_SYMBOL_GPL(usb_gadget_map_request_by_dev
);
95 int usb_gadget_map_request(struct usb_gadget
*gadget
,
96 struct usb_request
*req
, int is_in
)
98 return usb_gadget_map_request_by_dev(gadget
->dev
.parent
, req
, is_in
);
100 EXPORT_SYMBOL_GPL(usb_gadget_map_request
);
102 void usb_gadget_unmap_request_by_dev(struct device
*dev
,
103 struct usb_request
*req
, int is_in
)
105 if (req
->length
== 0)
108 if (req
->num_mapped_sgs
) {
109 dma_unmap_sg(dev
, req
->sg
, req
->num_mapped_sgs
,
110 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
112 req
->num_mapped_sgs
= 0;
114 dma_unmap_single(dev
, req
->dma
, req
->length
,
115 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
118 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request_by_dev
);
120 void usb_gadget_unmap_request(struct usb_gadget
*gadget
,
121 struct usb_request
*req
, int is_in
)
123 usb_gadget_unmap_request_by_dev(gadget
->dev
.parent
, req
, is_in
);
125 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request
);
127 #endif /* CONFIG_HAS_DMA */
129 /* ------------------------------------------------------------------------- */
132 * usb_gadget_giveback_request - give the request back to the gadget layer
133 * Context: in_interrupt()
135 * This is called by device controller drivers in order to return the
136 * completed request back to the gadget layer.
138 void usb_gadget_giveback_request(struct usb_ep
*ep
,
139 struct usb_request
*req
)
141 if (likely(req
->status
== 0))
142 usb_led_activity(USB_LED_EVENT_GADGET
);
144 req
->complete(ep
, req
);
146 EXPORT_SYMBOL_GPL(usb_gadget_giveback_request
);
148 /* ------------------------------------------------------------------------- */
151 * gadget_find_ep_by_name - returns ep whose name is the same as sting passed
152 * in second parameter or NULL if searched endpoint not found
153 * @g: controller to check for quirk
154 * @name: name of searched endpoint
156 struct usb_ep
*gadget_find_ep_by_name(struct usb_gadget
*g
, const char *name
)
160 gadget_for_each_ep(ep
, g
) {
161 if (!strcmp(ep
->name
, name
))
167 EXPORT_SYMBOL_GPL(gadget_find_ep_by_name
);
169 /* ------------------------------------------------------------------------- */
171 int usb_gadget_ep_match_desc(struct usb_gadget
*gadget
,
172 struct usb_ep
*ep
, struct usb_endpoint_descriptor
*desc
,
173 struct usb_ss_ep_comp_descriptor
*ep_comp
)
177 int num_req_streams
= 0;
179 /* endpoint already claimed? */
183 type
= usb_endpoint_type(desc
);
184 max
= 0x7ff & usb_endpoint_maxp(desc
);
186 if (usb_endpoint_dir_in(desc
) && !ep
->caps
.dir_in
)
188 if (usb_endpoint_dir_out(desc
) && !ep
->caps
.dir_out
)
191 if (max
> ep
->maxpacket_limit
)
194 /* "high bandwidth" works only at high speed */
195 if (!gadget_is_dualspeed(gadget
) && usb_endpoint_maxp(desc
) & (3<<11))
199 case USB_ENDPOINT_XFER_CONTROL
:
200 /* only support ep0 for portable CONTROL traffic */
202 case USB_ENDPOINT_XFER_ISOC
:
203 if (!ep
->caps
.type_iso
)
205 /* ISO: limit 1023 bytes full speed, 1024 high/super speed */
206 if (!gadget_is_dualspeed(gadget
) && max
> 1023)
209 case USB_ENDPOINT_XFER_BULK
:
210 if (!ep
->caps
.type_bulk
)
212 if (ep_comp
&& gadget_is_superspeed(gadget
)) {
213 /* Get the number of required streams from the
214 * EP companion descriptor and see if the EP
217 num_req_streams
= ep_comp
->bmAttributes
& 0x1f;
218 if (num_req_streams
> ep
->max_streams
)
222 case USB_ENDPOINT_XFER_INT
:
223 /* Bulk endpoints handle interrupt transfers,
224 * except the toggle-quirky iso-synch kind
226 if (!ep
->caps
.type_int
&& !ep
->caps
.type_bulk
)
228 /* INT: limit 64 bytes full speed, 1024 high/super speed */
229 if (!gadget_is_dualspeed(gadget
) && max
> 64)
236 EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc
);
238 /* ------------------------------------------------------------------------- */
240 static void usb_gadget_state_work(struct work_struct
*work
)
242 struct usb_gadget
*gadget
= work_to_gadget(work
);
243 struct usb_udc
*udc
= gadget
->udc
;
246 sysfs_notify(&udc
->dev
.kobj
, NULL
, "state");
249 void usb_gadget_set_state(struct usb_gadget
*gadget
,
250 enum usb_device_state state
)
252 gadget
->state
= state
;
253 schedule_work(&gadget
->work
);
255 EXPORT_SYMBOL_GPL(usb_gadget_set_state
);
257 /* ------------------------------------------------------------------------- */
259 static void usb_udc_connect_control(struct usb_udc
*udc
)
262 usb_gadget_connect(udc
->gadget
);
264 usb_gadget_disconnect(udc
->gadget
);
268 * usb_udc_vbus_handler - updates the udc core vbus status, and try to
269 * connect or disconnect gadget
270 * @gadget: The gadget which vbus change occurs
271 * @status: The vbus status
273 * The udc driver calls it when it wants to connect or disconnect gadget
274 * according to vbus status.
276 void usb_udc_vbus_handler(struct usb_gadget
*gadget
, bool status
)
278 struct usb_udc
*udc
= gadget
->udc
;
282 usb_udc_connect_control(udc
);
285 EXPORT_SYMBOL_GPL(usb_udc_vbus_handler
);
288 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
289 * @gadget: The gadget which bus reset occurs
290 * @driver: The gadget driver we want to notify
292 * If the udc driver has bus reset handler, it needs to call this when the bus
293 * reset occurs, it notifies the gadget driver that the bus reset occurs as
294 * well as updates gadget state.
296 void usb_gadget_udc_reset(struct usb_gadget
*gadget
,
297 struct usb_gadget_driver
*driver
)
299 driver
->reset(gadget
);
300 usb_gadget_set_state(gadget
, USB_STATE_DEFAULT
);
302 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset
);
305 * usb_gadget_udc_start - tells usb device controller to start up
306 * @udc: The UDC to be started
308 * This call is issued by the UDC Class driver when it's about
309 * to register a gadget driver to the device controller, before
310 * calling gadget driver's bind() method.
312 * It allows the controller to be powered off until strictly
313 * necessary to have it powered on.
315 * Returns zero on success, else negative errno.
317 static inline int usb_gadget_udc_start(struct usb_udc
*udc
)
319 return udc
->gadget
->ops
->udc_start(udc
->gadget
, udc
->driver
);
323 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
324 * @gadget: The device we want to stop activity
325 * @driver: The driver to unbind from @gadget
327 * This call is issued by the UDC Class driver after calling
328 * gadget driver's unbind() method.
330 * The details are implementation specific, but it can go as
331 * far as powering off UDC completely and disable its data
334 static inline void usb_gadget_udc_stop(struct usb_udc
*udc
)
336 udc
->gadget
->ops
->udc_stop(udc
->gadget
);
340 * usb_udc_release - release the usb_udc struct
341 * @dev: the dev member within usb_udc
343 * This is called by driver's core in order to free memory once the last
344 * reference is released.
346 static void usb_udc_release(struct device
*dev
)
350 udc
= container_of(dev
, struct usb_udc
, dev
);
351 dev_dbg(dev
, "releasing '%s'\n", dev_name(dev
));
355 static const struct attribute_group
*usb_udc_attr_groups
[];
357 static void usb_udc_nop_release(struct device
*dev
)
359 dev_vdbg(dev
, "%s\n", __func__
);
363 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
364 * @parent: the parent device to this udc. Usually the controller driver's
366 * @gadget: the gadget to be added to the list.
367 * @release: a gadget release function.
369 * Returns zero on success, negative errno otherwise.
371 int usb_add_gadget_udc_release(struct device
*parent
, struct usb_gadget
*gadget
,
372 void (*release
)(struct device
*dev
))
375 struct usb_gadget_driver
*driver
;
378 udc
= kzalloc(sizeof(*udc
), GFP_KERNEL
);
382 dev_set_name(&gadget
->dev
, "gadget");
383 INIT_WORK(&gadget
->work
, usb_gadget_state_work
);
384 gadget
->dev
.parent
= parent
;
387 gadget
->dev
.release
= release
;
389 gadget
->dev
.release
= usb_udc_nop_release
;
391 ret
= device_register(&gadget
->dev
);
395 device_initialize(&udc
->dev
);
396 udc
->dev
.release
= usb_udc_release
;
397 udc
->dev
.class = udc_class
;
398 udc
->dev
.groups
= usb_udc_attr_groups
;
399 udc
->dev
.parent
= parent
;
400 ret
= dev_set_name(&udc
->dev
, "%s", kobject_name(&parent
->kobj
));
404 udc
->gadget
= gadget
;
407 mutex_lock(&udc_lock
);
408 list_add_tail(&udc
->list
, &udc_list
);
410 ret
= device_add(&udc
->dev
);
414 usb_gadget_set_state(gadget
, USB_STATE_NOTATTACHED
);
417 /* pick up one of pending gadget drivers */
418 list_for_each_entry(driver
, &gadget_driver_pending_list
, pending
) {
419 if (!driver
->udc_name
|| strcmp(driver
->udc_name
,
420 dev_name(&udc
->dev
)) == 0) {
421 ret
= udc_bind_to_driver(udc
, driver
);
422 if (ret
!= -EPROBE_DEFER
)
423 list_del(&driver
->pending
);
430 mutex_unlock(&udc_lock
);
435 list_del(&udc
->list
);
436 mutex_unlock(&udc_lock
);
439 put_device(&udc
->dev
);
440 device_del(&gadget
->dev
);
443 put_device(&gadget
->dev
);
449 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release
);
452 * usb_get_gadget_udc_name - get the name of the first UDC controller
453 * This functions returns the name of the first UDC controller in the system.
454 * Please note that this interface is usefull only for legacy drivers which
455 * assume that there is only one UDC controller in the system and they need to
456 * get its name before initialization. There is no guarantee that the UDC
457 * of the returned name will be still available, when gadget driver registers
460 * Returns pointer to string with UDC controller name on success, NULL
461 * otherwise. Caller should kfree() returned string.
463 char *usb_get_gadget_udc_name(void)
468 /* For now we take the first available UDC */
469 mutex_lock(&udc_lock
);
470 list_for_each_entry(udc
, &udc_list
, list
) {
472 name
= kstrdup(udc
->gadget
->name
, GFP_KERNEL
);
476 mutex_unlock(&udc_lock
);
479 EXPORT_SYMBOL_GPL(usb_get_gadget_udc_name
);
482 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
483 * @parent: the parent device to this udc. Usually the controller
485 * @gadget: the gadget to be added to the list
487 * Returns zero on success, negative errno otherwise.
489 int usb_add_gadget_udc(struct device
*parent
, struct usb_gadget
*gadget
)
491 return usb_add_gadget_udc_release(parent
, gadget
, NULL
);
493 EXPORT_SYMBOL_GPL(usb_add_gadget_udc
);
495 static void usb_gadget_remove_driver(struct usb_udc
*udc
)
497 dev_dbg(&udc
->dev
, "unregistering UDC driver [%s]\n",
498 udc
->driver
->function
);
500 kobject_uevent(&udc
->dev
.kobj
, KOBJ_CHANGE
);
502 usb_gadget_disconnect(udc
->gadget
);
503 udc
->driver
->disconnect(udc
->gadget
);
504 udc
->driver
->unbind(udc
->gadget
);
505 usb_gadget_udc_stop(udc
);
508 udc
->dev
.driver
= NULL
;
509 udc
->gadget
->dev
.driver
= NULL
;
513 * usb_del_gadget_udc - deletes @udc from udc_list
514 * @gadget: the gadget to be removed.
516 * This, will call usb_gadget_unregister_driver() if
517 * the @udc is still busy.
519 void usb_del_gadget_udc(struct usb_gadget
*gadget
)
521 struct usb_udc
*udc
= gadget
->udc
;
526 dev_vdbg(gadget
->dev
.parent
, "unregistering gadget\n");
528 mutex_lock(&udc_lock
);
529 list_del(&udc
->list
);
532 struct usb_gadget_driver
*driver
= udc
->driver
;
534 usb_gadget_remove_driver(udc
);
535 list_add(&driver
->pending
, &gadget_driver_pending_list
);
537 mutex_unlock(&udc_lock
);
539 kobject_uevent(&udc
->dev
.kobj
, KOBJ_REMOVE
);
540 flush_work(&gadget
->work
);
541 device_unregister(&udc
->dev
);
542 device_unregister(&gadget
->dev
);
544 EXPORT_SYMBOL_GPL(usb_del_gadget_udc
);
546 /* ------------------------------------------------------------------------- */
548 static int udc_bind_to_driver(struct usb_udc
*udc
, struct usb_gadget_driver
*driver
)
552 dev_dbg(&udc
->dev
, "registering UDC driver [%s]\n",
555 udc
->driver
= driver
;
556 udc
->dev
.driver
= &driver
->driver
;
557 udc
->gadget
->dev
.driver
= &driver
->driver
;
559 ret
= driver
->bind(udc
->gadget
, driver
);
562 ret
= usb_gadget_udc_start(udc
);
564 driver
->unbind(udc
->gadget
);
567 usb_udc_connect_control(udc
);
569 kobject_uevent(&udc
->dev
.kobj
, KOBJ_CHANGE
);
573 dev_err(&udc
->dev
, "failed to start %s: %d\n",
574 udc
->driver
->function
, ret
);
576 udc
->dev
.driver
= NULL
;
577 udc
->gadget
->dev
.driver
= NULL
;
581 int usb_gadget_probe_driver(struct usb_gadget_driver
*driver
)
583 struct usb_udc
*udc
= NULL
;
586 if (!driver
|| !driver
->bind
|| !driver
->setup
)
589 mutex_lock(&udc_lock
);
590 if (driver
->udc_name
) {
591 list_for_each_entry(udc
, &udc_list
, list
) {
592 ret
= strcmp(driver
->udc_name
, dev_name(&udc
->dev
));
596 if (!ret
&& !udc
->driver
)
599 list_for_each_entry(udc
, &udc_list
, list
) {
600 /* For now we take the first one */
606 if (!driver
->match_existing_only
) {
607 list_add_tail(&driver
->pending
, &gadget_driver_pending_list
);
608 pr_info("udc-core: couldn't find an available UDC - added [%s] to list of pending drivers\n",
613 mutex_unlock(&udc_lock
);
616 ret
= udc_bind_to_driver(udc
, driver
);
617 mutex_unlock(&udc_lock
);
620 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver
);
622 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
624 struct usb_udc
*udc
= NULL
;
627 if (!driver
|| !driver
->unbind
)
630 mutex_lock(&udc_lock
);
631 list_for_each_entry(udc
, &udc_list
, list
)
632 if (udc
->driver
== driver
) {
633 usb_gadget_remove_driver(udc
);
634 usb_gadget_set_state(udc
->gadget
,
635 USB_STATE_NOTATTACHED
);
641 list_del(&driver
->pending
);
644 mutex_unlock(&udc_lock
);
647 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver
);
649 /* ------------------------------------------------------------------------- */
651 static ssize_t
usb_udc_srp_store(struct device
*dev
,
652 struct device_attribute
*attr
, const char *buf
, size_t n
)
654 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
656 if (sysfs_streq(buf
, "1"))
657 usb_gadget_wakeup(udc
->gadget
);
661 static DEVICE_ATTR(srp
, S_IWUSR
, NULL
, usb_udc_srp_store
);
663 static ssize_t
usb_udc_softconn_store(struct device
*dev
,
664 struct device_attribute
*attr
, const char *buf
, size_t n
)
666 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
669 dev_err(dev
, "soft-connect without a gadget driver\n");
673 if (sysfs_streq(buf
, "connect")) {
674 usb_gadget_udc_start(udc
);
675 usb_gadget_connect(udc
->gadget
);
676 } else if (sysfs_streq(buf
, "disconnect")) {
677 usb_gadget_disconnect(udc
->gadget
);
678 udc
->driver
->disconnect(udc
->gadget
);
679 usb_gadget_udc_stop(udc
);
681 dev_err(dev
, "unsupported command '%s'\n", buf
);
687 static DEVICE_ATTR(soft_connect
, S_IWUSR
, NULL
, usb_udc_softconn_store
);
689 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
692 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
693 struct usb_gadget
*gadget
= udc
->gadget
;
695 return sprintf(buf
, "%s\n", usb_state_string(gadget
->state
));
697 static DEVICE_ATTR_RO(state
);
699 #define USB_UDC_SPEED_ATTR(name, param) \
700 ssize_t name##_show(struct device *dev, \
701 struct device_attribute *attr, char *buf) \
703 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
704 return snprintf(buf, PAGE_SIZE, "%s\n", \
705 usb_speed_string(udc->gadget->param)); \
707 static DEVICE_ATTR_RO(name)
709 static USB_UDC_SPEED_ATTR(current_speed
, speed
);
710 static USB_UDC_SPEED_ATTR(maximum_speed
, max_speed
);
712 #define USB_UDC_ATTR(name) \
713 ssize_t name##_show(struct device *dev, \
714 struct device_attribute *attr, char *buf) \
716 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
717 struct usb_gadget *gadget = udc->gadget; \
719 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
721 static DEVICE_ATTR_RO(name)
723 static USB_UDC_ATTR(is_otg
);
724 static USB_UDC_ATTR(is_a_peripheral
);
725 static USB_UDC_ATTR(b_hnp_enable
);
726 static USB_UDC_ATTR(a_hnp_support
);
727 static USB_UDC_ATTR(a_alt_hnp_support
);
728 static USB_UDC_ATTR(is_selfpowered
);
730 static struct attribute
*usb_udc_attrs
[] = {
732 &dev_attr_soft_connect
.attr
,
733 &dev_attr_state
.attr
,
734 &dev_attr_current_speed
.attr
,
735 &dev_attr_maximum_speed
.attr
,
737 &dev_attr_is_otg
.attr
,
738 &dev_attr_is_a_peripheral
.attr
,
739 &dev_attr_b_hnp_enable
.attr
,
740 &dev_attr_a_hnp_support
.attr
,
741 &dev_attr_a_alt_hnp_support
.attr
,
742 &dev_attr_is_selfpowered
.attr
,
746 static const struct attribute_group usb_udc_attr_group
= {
747 .attrs
= usb_udc_attrs
,
750 static const struct attribute_group
*usb_udc_attr_groups
[] = {
755 static int usb_udc_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
757 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
760 ret
= add_uevent_var(env
, "USB_UDC_NAME=%s", udc
->gadget
->name
);
762 dev_err(dev
, "failed to add uevent USB_UDC_NAME\n");
767 ret
= add_uevent_var(env
, "USB_UDC_DRIVER=%s",
768 udc
->driver
->function
);
770 dev_err(dev
, "failed to add uevent USB_UDC_DRIVER\n");
778 static int __init
usb_udc_init(void)
780 udc_class
= class_create(THIS_MODULE
, "udc");
781 if (IS_ERR(udc_class
)) {
782 pr_err("failed to create udc class --> %ld\n",
784 return PTR_ERR(udc_class
);
787 udc_class
->dev_uevent
= usb_udc_uevent
;
790 subsys_initcall(usb_udc_init
);
792 static void __exit
usb_udc_exit(void)
794 class_destroy(udc_class
);
796 module_exit(usb_udc_exit
);
798 MODULE_DESCRIPTION("UDC Framework");
799 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
800 MODULE_LICENSE("GPL v2");