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(struct usb_gadget
*gadget
,
65 struct usb_request
*req
, int is_in
)
67 struct device
*dev
= gadget
->dev
.parent
;
75 mapped
= dma_map_sg(dev
, req
->sg
, req
->num_sgs
,
76 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
78 dev_err(dev
, "failed to map SGs\n");
82 req
->num_mapped_sgs
= mapped
;
84 req
->dma
= dma_map_single(dev
, req
->buf
, req
->length
,
85 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
87 if (dma_mapping_error(dev
, req
->dma
)) {
88 dev_err(dev
, "failed to map buffer\n");
95 EXPORT_SYMBOL_GPL(usb_gadget_map_request
);
97 void usb_gadget_unmap_request(struct usb_gadget
*gadget
,
98 struct usb_request
*req
, int is_in
)
100 if (req
->length
== 0)
103 if (req
->num_mapped_sgs
) {
104 dma_unmap_sg(gadget
->dev
.parent
, req
->sg
, req
->num_mapped_sgs
,
105 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
107 req
->num_mapped_sgs
= 0;
109 dma_unmap_single(gadget
->dev
.parent
, req
->dma
, req
->length
,
110 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
113 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request
);
115 #endif /* CONFIG_HAS_DMA */
117 /* ------------------------------------------------------------------------- */
120 * usb_gadget_giveback_request - give the request back to the gadget layer
121 * Context: in_interrupt()
123 * This is called by device controller drivers in order to return the
124 * completed request back to the gadget layer.
126 void usb_gadget_giveback_request(struct usb_ep
*ep
,
127 struct usb_request
*req
)
129 if (likely(req
->status
== 0))
130 usb_led_activity(USB_LED_EVENT_GADGET
);
132 req
->complete(ep
, req
);
134 EXPORT_SYMBOL_GPL(usb_gadget_giveback_request
);
136 /* ------------------------------------------------------------------------- */
139 * gadget_find_ep_by_name - returns ep whose name is the same as sting passed
140 * in second parameter or NULL if searched endpoint not found
141 * @g: controller to check for quirk
142 * @name: name of searched endpoint
144 struct usb_ep
*gadget_find_ep_by_name(struct usb_gadget
*g
, const char *name
)
148 gadget_for_each_ep(ep
, g
) {
149 if (!strcmp(ep
->name
, name
))
155 EXPORT_SYMBOL_GPL(gadget_find_ep_by_name
);
157 /* ------------------------------------------------------------------------- */
159 int usb_gadget_ep_match_desc(struct usb_gadget
*gadget
,
160 struct usb_ep
*ep
, struct usb_endpoint_descriptor
*desc
,
161 struct usb_ss_ep_comp_descriptor
*ep_comp
)
165 int num_req_streams
= 0;
167 /* endpoint already claimed? */
171 type
= usb_endpoint_type(desc
);
172 max
= 0x7ff & usb_endpoint_maxp(desc
);
174 if (usb_endpoint_dir_in(desc
) && !ep
->caps
.dir_in
)
176 if (usb_endpoint_dir_out(desc
) && !ep
->caps
.dir_out
)
179 if (max
> ep
->maxpacket_limit
)
182 /* "high bandwidth" works only at high speed */
183 if (!gadget_is_dualspeed(gadget
) && usb_endpoint_maxp(desc
) & (3<<11))
187 case USB_ENDPOINT_XFER_CONTROL
:
188 /* only support ep0 for portable CONTROL traffic */
190 case USB_ENDPOINT_XFER_ISOC
:
191 if (!ep
->caps
.type_iso
)
193 /* ISO: limit 1023 bytes full speed, 1024 high/super speed */
194 if (!gadget_is_dualspeed(gadget
) && max
> 1023)
197 case USB_ENDPOINT_XFER_BULK
:
198 if (!ep
->caps
.type_bulk
)
200 if (ep_comp
&& gadget_is_superspeed(gadget
)) {
201 /* Get the number of required streams from the
202 * EP companion descriptor and see if the EP
205 num_req_streams
= ep_comp
->bmAttributes
& 0x1f;
206 if (num_req_streams
> ep
->max_streams
)
210 case USB_ENDPOINT_XFER_INT
:
211 /* Bulk endpoints handle interrupt transfers,
212 * except the toggle-quirky iso-synch kind
214 if (!ep
->caps
.type_int
&& !ep
->caps
.type_bulk
)
216 /* INT: limit 64 bytes full speed, 1024 high/super speed */
217 if (!gadget_is_dualspeed(gadget
) && max
> 64)
224 EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc
);
226 /* ------------------------------------------------------------------------- */
228 static void usb_gadget_state_work(struct work_struct
*work
)
230 struct usb_gadget
*gadget
= work_to_gadget(work
);
231 struct usb_udc
*udc
= gadget
->udc
;
234 sysfs_notify(&udc
->dev
.kobj
, NULL
, "state");
237 void usb_gadget_set_state(struct usb_gadget
*gadget
,
238 enum usb_device_state state
)
240 gadget
->state
= state
;
241 schedule_work(&gadget
->work
);
243 EXPORT_SYMBOL_GPL(usb_gadget_set_state
);
245 /* ------------------------------------------------------------------------- */
247 static void usb_udc_connect_control(struct usb_udc
*udc
)
250 usb_gadget_connect(udc
->gadget
);
252 usb_gadget_disconnect(udc
->gadget
);
256 * usb_udc_vbus_handler - updates the udc core vbus status, and try to
257 * connect or disconnect gadget
258 * @gadget: The gadget which vbus change occurs
259 * @status: The vbus status
261 * The udc driver calls it when it wants to connect or disconnect gadget
262 * according to vbus status.
264 void usb_udc_vbus_handler(struct usb_gadget
*gadget
, bool status
)
266 struct usb_udc
*udc
= gadget
->udc
;
270 usb_udc_connect_control(udc
);
273 EXPORT_SYMBOL_GPL(usb_udc_vbus_handler
);
276 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
277 * @gadget: The gadget which bus reset occurs
278 * @driver: The gadget driver we want to notify
280 * If the udc driver has bus reset handler, it needs to call this when the bus
281 * reset occurs, it notifies the gadget driver that the bus reset occurs as
282 * well as updates gadget state.
284 void usb_gadget_udc_reset(struct usb_gadget
*gadget
,
285 struct usb_gadget_driver
*driver
)
287 driver
->reset(gadget
);
288 usb_gadget_set_state(gadget
, USB_STATE_DEFAULT
);
290 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset
);
293 * usb_gadget_udc_start - tells usb device controller to start up
294 * @udc: The UDC to be started
296 * This call is issued by the UDC Class driver when it's about
297 * to register a gadget driver to the device controller, before
298 * calling gadget driver's bind() method.
300 * It allows the controller to be powered off until strictly
301 * necessary to have it powered on.
303 * Returns zero on success, else negative errno.
305 static inline int usb_gadget_udc_start(struct usb_udc
*udc
)
307 return udc
->gadget
->ops
->udc_start(udc
->gadget
, udc
->driver
);
311 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
312 * @gadget: The device we want to stop activity
313 * @driver: The driver to unbind from @gadget
315 * This call is issued by the UDC Class driver after calling
316 * gadget driver's unbind() method.
318 * The details are implementation specific, but it can go as
319 * far as powering off UDC completely and disable its data
322 static inline void usb_gadget_udc_stop(struct usb_udc
*udc
)
324 udc
->gadget
->ops
->udc_stop(udc
->gadget
);
328 * usb_udc_release - release the usb_udc struct
329 * @dev: the dev member within usb_udc
331 * This is called by driver's core in order to free memory once the last
332 * reference is released.
334 static void usb_udc_release(struct device
*dev
)
338 udc
= container_of(dev
, struct usb_udc
, dev
);
339 dev_dbg(dev
, "releasing '%s'\n", dev_name(dev
));
343 static const struct attribute_group
*usb_udc_attr_groups
[];
345 static void usb_udc_nop_release(struct device
*dev
)
347 dev_vdbg(dev
, "%s\n", __func__
);
351 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
352 * @parent: the parent device to this udc. Usually the controller driver's
354 * @gadget: the gadget to be added to the list.
355 * @release: a gadget release function.
357 * Returns zero on success, negative errno otherwise.
359 int usb_add_gadget_udc_release(struct device
*parent
, struct usb_gadget
*gadget
,
360 void (*release
)(struct device
*dev
))
363 struct usb_gadget_driver
*driver
;
366 udc
= kzalloc(sizeof(*udc
), GFP_KERNEL
);
370 dev_set_name(&gadget
->dev
, "gadget");
371 INIT_WORK(&gadget
->work
, usb_gadget_state_work
);
372 gadget
->dev
.parent
= parent
;
375 gadget
->dev
.release
= release
;
377 gadget
->dev
.release
= usb_udc_nop_release
;
379 ret
= device_register(&gadget
->dev
);
383 device_initialize(&udc
->dev
);
384 udc
->dev
.release
= usb_udc_release
;
385 udc
->dev
.class = udc_class
;
386 udc
->dev
.groups
= usb_udc_attr_groups
;
387 udc
->dev
.parent
= parent
;
388 ret
= dev_set_name(&udc
->dev
, "%s", kobject_name(&parent
->kobj
));
392 udc
->gadget
= gadget
;
395 mutex_lock(&udc_lock
);
396 list_add_tail(&udc
->list
, &udc_list
);
398 ret
= device_add(&udc
->dev
);
402 usb_gadget_set_state(gadget
, USB_STATE_NOTATTACHED
);
405 /* pick up one of pending gadget drivers */
406 list_for_each_entry(driver
, &gadget_driver_pending_list
, pending
) {
407 if (!driver
->udc_name
|| strcmp(driver
->udc_name
,
408 dev_name(&udc
->dev
)) == 0) {
409 ret
= udc_bind_to_driver(udc
, driver
);
410 if (ret
!= -EPROBE_DEFER
)
411 list_del(&driver
->pending
);
418 mutex_unlock(&udc_lock
);
423 list_del(&udc
->list
);
424 mutex_unlock(&udc_lock
);
427 put_device(&udc
->dev
);
428 device_del(&gadget
->dev
);
431 put_device(&gadget
->dev
);
437 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release
);
440 * usb_get_gadget_udc_name - get the name of the first UDC controller
441 * This functions returns the name of the first UDC controller in the system.
442 * Please note that this interface is usefull only for legacy drivers which
443 * assume that there is only one UDC controller in the system and they need to
444 * get its name before initialization. There is no guarantee that the UDC
445 * of the returned name will be still available, when gadget driver registers
448 * Returns pointer to string with UDC controller name on success, NULL
449 * otherwise. Caller should kfree() returned string.
451 char *usb_get_gadget_udc_name(void)
456 /* For now we take the first available UDC */
457 mutex_lock(&udc_lock
);
458 list_for_each_entry(udc
, &udc_list
, list
) {
460 name
= kstrdup(udc
->gadget
->name
, GFP_KERNEL
);
464 mutex_unlock(&udc_lock
);
467 EXPORT_SYMBOL_GPL(usb_get_gadget_udc_name
);
470 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
471 * @parent: the parent device to this udc. Usually the controller
473 * @gadget: the gadget to be added to the list
475 * Returns zero on success, negative errno otherwise.
477 int usb_add_gadget_udc(struct device
*parent
, struct usb_gadget
*gadget
)
479 return usb_add_gadget_udc_release(parent
, gadget
, NULL
);
481 EXPORT_SYMBOL_GPL(usb_add_gadget_udc
);
483 static void usb_gadget_remove_driver(struct usb_udc
*udc
)
485 dev_dbg(&udc
->dev
, "unregistering UDC driver [%s]\n",
486 udc
->driver
->function
);
488 kobject_uevent(&udc
->dev
.kobj
, KOBJ_CHANGE
);
490 usb_gadget_disconnect(udc
->gadget
);
491 udc
->driver
->disconnect(udc
->gadget
);
492 udc
->driver
->unbind(udc
->gadget
);
493 usb_gadget_udc_stop(udc
);
496 udc
->dev
.driver
= NULL
;
497 udc
->gadget
->dev
.driver
= NULL
;
501 * usb_del_gadget_udc - deletes @udc from udc_list
502 * @gadget: the gadget to be removed.
504 * This, will call usb_gadget_unregister_driver() if
505 * the @udc is still busy.
507 void usb_del_gadget_udc(struct usb_gadget
*gadget
)
509 struct usb_udc
*udc
= gadget
->udc
;
514 dev_vdbg(gadget
->dev
.parent
, "unregistering gadget\n");
516 mutex_lock(&udc_lock
);
517 list_del(&udc
->list
);
520 struct usb_gadget_driver
*driver
= udc
->driver
;
522 usb_gadget_remove_driver(udc
);
523 list_add(&driver
->pending
, &gadget_driver_pending_list
);
525 mutex_unlock(&udc_lock
);
527 kobject_uevent(&udc
->dev
.kobj
, KOBJ_REMOVE
);
528 flush_work(&gadget
->work
);
529 device_unregister(&udc
->dev
);
530 device_unregister(&gadget
->dev
);
532 EXPORT_SYMBOL_GPL(usb_del_gadget_udc
);
534 /* ------------------------------------------------------------------------- */
536 static int udc_bind_to_driver(struct usb_udc
*udc
, struct usb_gadget_driver
*driver
)
540 dev_dbg(&udc
->dev
, "registering UDC driver [%s]\n",
543 udc
->driver
= driver
;
544 udc
->dev
.driver
= &driver
->driver
;
545 udc
->gadget
->dev
.driver
= &driver
->driver
;
547 ret
= driver
->bind(udc
->gadget
, driver
);
550 ret
= usb_gadget_udc_start(udc
);
552 driver
->unbind(udc
->gadget
);
555 usb_udc_connect_control(udc
);
557 kobject_uevent(&udc
->dev
.kobj
, KOBJ_CHANGE
);
561 dev_err(&udc
->dev
, "failed to start %s: %d\n",
562 udc
->driver
->function
, ret
);
564 udc
->dev
.driver
= NULL
;
565 udc
->gadget
->dev
.driver
= NULL
;
569 int usb_gadget_probe_driver(struct usb_gadget_driver
*driver
)
571 struct usb_udc
*udc
= NULL
;
574 if (!driver
|| !driver
->bind
|| !driver
->setup
)
577 mutex_lock(&udc_lock
);
578 if (driver
->udc_name
) {
579 list_for_each_entry(udc
, &udc_list
, list
) {
580 ret
= strcmp(driver
->udc_name
, dev_name(&udc
->dev
));
584 if (!ret
&& !udc
->driver
)
587 list_for_each_entry(udc
, &udc_list
, list
) {
588 /* For now we take the first one */
594 list_add_tail(&driver
->pending
, &gadget_driver_pending_list
);
595 pr_info("udc-core: couldn't find an available UDC - added [%s] to list of pending drivers\n",
597 mutex_unlock(&udc_lock
);
600 ret
= udc_bind_to_driver(udc
, driver
);
601 mutex_unlock(&udc_lock
);
604 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver
);
606 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
608 struct usb_udc
*udc
= NULL
;
611 if (!driver
|| !driver
->unbind
)
614 mutex_lock(&udc_lock
);
615 list_for_each_entry(udc
, &udc_list
, list
)
616 if (udc
->driver
== driver
) {
617 usb_gadget_remove_driver(udc
);
618 usb_gadget_set_state(udc
->gadget
,
619 USB_STATE_NOTATTACHED
);
625 list_del(&driver
->pending
);
628 mutex_unlock(&udc_lock
);
631 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver
);
633 /* ------------------------------------------------------------------------- */
635 static ssize_t
usb_udc_srp_store(struct device
*dev
,
636 struct device_attribute
*attr
, const char *buf
, size_t n
)
638 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
640 if (sysfs_streq(buf
, "1"))
641 usb_gadget_wakeup(udc
->gadget
);
645 static DEVICE_ATTR(srp
, S_IWUSR
, NULL
, usb_udc_srp_store
);
647 static ssize_t
usb_udc_softconn_store(struct device
*dev
,
648 struct device_attribute
*attr
, const char *buf
, size_t n
)
650 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
653 dev_err(dev
, "soft-connect without a gadget driver\n");
657 if (sysfs_streq(buf
, "connect")) {
658 usb_gadget_udc_start(udc
);
659 usb_gadget_connect(udc
->gadget
);
660 } else if (sysfs_streq(buf
, "disconnect")) {
661 usb_gadget_disconnect(udc
->gadget
);
662 udc
->driver
->disconnect(udc
->gadget
);
663 usb_gadget_udc_stop(udc
);
665 dev_err(dev
, "unsupported command '%s'\n", buf
);
671 static DEVICE_ATTR(soft_connect
, S_IWUSR
, NULL
, usb_udc_softconn_store
);
673 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
676 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
677 struct usb_gadget
*gadget
= udc
->gadget
;
679 return sprintf(buf
, "%s\n", usb_state_string(gadget
->state
));
681 static DEVICE_ATTR_RO(state
);
683 #define USB_UDC_SPEED_ATTR(name, param) \
684 ssize_t name##_show(struct device *dev, \
685 struct device_attribute *attr, char *buf) \
687 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
688 return snprintf(buf, PAGE_SIZE, "%s\n", \
689 usb_speed_string(udc->gadget->param)); \
691 static DEVICE_ATTR_RO(name)
693 static USB_UDC_SPEED_ATTR(current_speed
, speed
);
694 static USB_UDC_SPEED_ATTR(maximum_speed
, max_speed
);
696 #define USB_UDC_ATTR(name) \
697 ssize_t name##_show(struct device *dev, \
698 struct device_attribute *attr, char *buf) \
700 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
701 struct usb_gadget *gadget = udc->gadget; \
703 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
705 static DEVICE_ATTR_RO(name)
707 static USB_UDC_ATTR(is_otg
);
708 static USB_UDC_ATTR(is_a_peripheral
);
709 static USB_UDC_ATTR(b_hnp_enable
);
710 static USB_UDC_ATTR(a_hnp_support
);
711 static USB_UDC_ATTR(a_alt_hnp_support
);
712 static USB_UDC_ATTR(is_selfpowered
);
714 static struct attribute
*usb_udc_attrs
[] = {
716 &dev_attr_soft_connect
.attr
,
717 &dev_attr_state
.attr
,
718 &dev_attr_current_speed
.attr
,
719 &dev_attr_maximum_speed
.attr
,
721 &dev_attr_is_otg
.attr
,
722 &dev_attr_is_a_peripheral
.attr
,
723 &dev_attr_b_hnp_enable
.attr
,
724 &dev_attr_a_hnp_support
.attr
,
725 &dev_attr_a_alt_hnp_support
.attr
,
726 &dev_attr_is_selfpowered
.attr
,
730 static const struct attribute_group usb_udc_attr_group
= {
731 .attrs
= usb_udc_attrs
,
734 static const struct attribute_group
*usb_udc_attr_groups
[] = {
739 static int usb_udc_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
741 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
744 ret
= add_uevent_var(env
, "USB_UDC_NAME=%s", udc
->gadget
->name
);
746 dev_err(dev
, "failed to add uevent USB_UDC_NAME\n");
751 ret
= add_uevent_var(env
, "USB_UDC_DRIVER=%s",
752 udc
->driver
->function
);
754 dev_err(dev
, "failed to add uevent USB_UDC_DRIVER\n");
762 static int __init
usb_udc_init(void)
764 udc_class
= class_create(THIS_MODULE
, "udc");
765 if (IS_ERR(udc_class
)) {
766 pr_err("failed to create udc class --> %ld\n",
768 return PTR_ERR(udc_class
);
771 udc_class
->dev_uevent
= usb_udc_uevent
;
774 subsys_initcall(usb_udc_init
);
776 static void __exit
usb_udc_exit(void)
778 class_destroy(udc_class
);
780 module_exit(usb_udc_exit
);
782 MODULE_DESCRIPTION("UDC Framework");
783 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
784 MODULE_LICENSE("GPL v2");