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
)
63 struct device
*dev
= gadget
->dev
.parent
;
71 mapped
= dma_map_sg(dev
, req
->sg
, req
->num_sgs
,
72 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
74 dev_err(dev
, "failed to map SGs\n");
78 req
->num_mapped_sgs
= mapped
;
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");
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
)
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;
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 /* ------------------------------------------------------------------------- */
135 * gadget_find_ep_by_name - returns ep whose name is the same as sting passed
136 * in second parameter or NULL if searched endpoint not found
137 * @g: controller to check for quirk
138 * @name: name of searched endpoint
140 struct usb_ep
*gadget_find_ep_by_name(struct usb_gadget
*g
, const char *name
)
144 gadget_for_each_ep(ep
, g
) {
145 if (!strcmp(ep
->name
, name
))
151 EXPORT_SYMBOL_GPL(gadget_find_ep_by_name
);
153 /* ------------------------------------------------------------------------- */
155 int usb_gadget_ep_match_desc(struct usb_gadget
*gadget
,
156 struct usb_ep
*ep
, struct usb_endpoint_descriptor
*desc
,
157 struct usb_ss_ep_comp_descriptor
*ep_comp
)
161 int num_req_streams
= 0;
163 /* endpoint already claimed? */
167 type
= usb_endpoint_type(desc
);
168 max
= 0x7ff & usb_endpoint_maxp(desc
);
170 if (usb_endpoint_dir_in(desc
) && !ep
->caps
.dir_in
)
172 if (usb_endpoint_dir_out(desc
) && !ep
->caps
.dir_out
)
175 if (max
> ep
->maxpacket_limit
)
178 /* "high bandwidth" works only at high speed */
179 if (!gadget_is_dualspeed(gadget
) && usb_endpoint_maxp(desc
) & (3<<11))
183 case USB_ENDPOINT_XFER_CONTROL
:
184 /* only support ep0 for portable CONTROL traffic */
186 case USB_ENDPOINT_XFER_ISOC
:
187 if (!ep
->caps
.type_iso
)
189 /* ISO: limit 1023 bytes full speed, 1024 high/super speed */
190 if (!gadget_is_dualspeed(gadget
) && max
> 1023)
193 case USB_ENDPOINT_XFER_BULK
:
194 if (!ep
->caps
.type_bulk
)
196 if (ep_comp
&& gadget_is_superspeed(gadget
)) {
197 /* Get the number of required streams from the
198 * EP companion descriptor and see if the EP
201 num_req_streams
= ep_comp
->bmAttributes
& 0x1f;
202 if (num_req_streams
> ep
->max_streams
)
206 case USB_ENDPOINT_XFER_INT
:
207 /* Bulk endpoints handle interrupt transfers,
208 * except the toggle-quirky iso-synch kind
210 if (!ep
->caps
.type_int
&& !ep
->caps
.type_bulk
)
212 /* INT: limit 64 bytes full speed, 1024 high/super speed */
213 if (!gadget_is_dualspeed(gadget
) && max
> 64)
220 EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc
);
222 /* ------------------------------------------------------------------------- */
224 static void usb_gadget_state_work(struct work_struct
*work
)
226 struct usb_gadget
*gadget
= work_to_gadget(work
);
227 struct usb_udc
*udc
= gadget
->udc
;
230 sysfs_notify(&udc
->dev
.kobj
, NULL
, "state");
233 void usb_gadget_set_state(struct usb_gadget
*gadget
,
234 enum usb_device_state state
)
236 gadget
->state
= state
;
237 schedule_work(&gadget
->work
);
239 EXPORT_SYMBOL_GPL(usb_gadget_set_state
);
241 /* ------------------------------------------------------------------------- */
243 static void usb_udc_connect_control(struct usb_udc
*udc
)
246 usb_gadget_connect(udc
->gadget
);
248 usb_gadget_disconnect(udc
->gadget
);
252 * usb_udc_vbus_handler - updates the udc core vbus status, and try to
253 * connect or disconnect gadget
254 * @gadget: The gadget which vbus change occurs
255 * @status: The vbus status
257 * The udc driver calls it when it wants to connect or disconnect gadget
258 * according to vbus status.
260 void usb_udc_vbus_handler(struct usb_gadget
*gadget
, bool status
)
262 struct usb_udc
*udc
= gadget
->udc
;
266 usb_udc_connect_control(udc
);
269 EXPORT_SYMBOL_GPL(usb_udc_vbus_handler
);
272 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
273 * @gadget: The gadget which bus reset occurs
274 * @driver: The gadget driver we want to notify
276 * If the udc driver has bus reset handler, it needs to call this when the bus
277 * reset occurs, it notifies the gadget driver that the bus reset occurs as
278 * well as updates gadget state.
280 void usb_gadget_udc_reset(struct usb_gadget
*gadget
,
281 struct usb_gadget_driver
*driver
)
283 driver
->reset(gadget
);
284 usb_gadget_set_state(gadget
, USB_STATE_DEFAULT
);
286 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset
);
289 * usb_gadget_udc_start - tells usb device controller to start up
290 * @udc: The UDC to be started
292 * This call is issued by the UDC Class driver when it's about
293 * to register a gadget driver to the device controller, before
294 * calling gadget driver's bind() method.
296 * It allows the controller to be powered off until strictly
297 * necessary to have it powered on.
299 * Returns zero on success, else negative errno.
301 static inline int usb_gadget_udc_start(struct usb_udc
*udc
)
303 return udc
->gadget
->ops
->udc_start(udc
->gadget
, udc
->driver
);
307 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
308 * @gadget: The device we want to stop activity
309 * @driver: The driver to unbind from @gadget
311 * This call is issued by the UDC Class driver after calling
312 * gadget driver's unbind() method.
314 * The details are implementation specific, but it can go as
315 * far as powering off UDC completely and disable its data
318 static inline void usb_gadget_udc_stop(struct usb_udc
*udc
)
320 udc
->gadget
->ops
->udc_stop(udc
->gadget
);
324 * usb_udc_release - release the usb_udc struct
325 * @dev: the dev member within usb_udc
327 * This is called by driver's core in order to free memory once the last
328 * reference is released.
330 static void usb_udc_release(struct device
*dev
)
334 udc
= container_of(dev
, struct usb_udc
, dev
);
335 dev_dbg(dev
, "releasing '%s'\n", dev_name(dev
));
339 static const struct attribute_group
*usb_udc_attr_groups
[];
341 static void usb_udc_nop_release(struct device
*dev
)
343 dev_vdbg(dev
, "%s\n", __func__
);
347 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
348 * @parent: the parent device to this udc. Usually the controller driver's
350 * @gadget: the gadget to be added to the list.
351 * @release: a gadget release function.
353 * Returns zero on success, negative errno otherwise.
355 int usb_add_gadget_udc_release(struct device
*parent
, struct usb_gadget
*gadget
,
356 void (*release
)(struct device
*dev
))
361 udc
= kzalloc(sizeof(*udc
), GFP_KERNEL
);
365 dev_set_name(&gadget
->dev
, "gadget");
366 INIT_WORK(&gadget
->work
, usb_gadget_state_work
);
367 gadget
->dev
.parent
= parent
;
369 #ifdef CONFIG_HAS_DMA
370 dma_set_coherent_mask(&gadget
->dev
, parent
->coherent_dma_mask
);
371 gadget
->dev
.dma_parms
= parent
->dma_parms
;
372 gadget
->dev
.dma_mask
= parent
->dma_mask
;
376 gadget
->dev
.release
= release
;
378 gadget
->dev
.release
= usb_udc_nop_release
;
380 ret
= device_register(&gadget
->dev
);
384 device_initialize(&udc
->dev
);
385 udc
->dev
.release
= usb_udc_release
;
386 udc
->dev
.class = udc_class
;
387 udc
->dev
.groups
= usb_udc_attr_groups
;
388 udc
->dev
.parent
= parent
;
389 ret
= dev_set_name(&udc
->dev
, "%s", kobject_name(&parent
->kobj
));
393 udc
->gadget
= gadget
;
396 mutex_lock(&udc_lock
);
397 list_add_tail(&udc
->list
, &udc_list
);
399 ret
= device_add(&udc
->dev
);
403 usb_gadget_set_state(gadget
, USB_STATE_NOTATTACHED
);
406 mutex_unlock(&udc_lock
);
411 list_del(&udc
->list
);
412 mutex_unlock(&udc_lock
);
415 put_device(&udc
->dev
);
416 device_del(&gadget
->dev
);
419 put_device(&gadget
->dev
);
425 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release
);
428 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
429 * @parent: the parent device to this udc. Usually the controller
431 * @gadget: the gadget to be added to the list
433 * Returns zero on success, negative errno otherwise.
435 int usb_add_gadget_udc(struct device
*parent
, struct usb_gadget
*gadget
)
437 return usb_add_gadget_udc_release(parent
, gadget
, NULL
);
439 EXPORT_SYMBOL_GPL(usb_add_gadget_udc
);
441 static void usb_gadget_remove_driver(struct usb_udc
*udc
)
443 dev_dbg(&udc
->dev
, "unregistering UDC driver [%s]\n",
444 udc
->driver
->function
);
446 kobject_uevent(&udc
->dev
.kobj
, KOBJ_CHANGE
);
448 usb_gadget_disconnect(udc
->gadget
);
449 udc
->driver
->disconnect(udc
->gadget
);
450 udc
->driver
->unbind(udc
->gadget
);
451 usb_gadget_udc_stop(udc
);
454 udc
->dev
.driver
= NULL
;
455 udc
->gadget
->dev
.driver
= NULL
;
459 * usb_del_gadget_udc - deletes @udc from udc_list
460 * @gadget: the gadget to be removed.
462 * This, will call usb_gadget_unregister_driver() if
463 * the @udc is still busy.
465 void usb_del_gadget_udc(struct usb_gadget
*gadget
)
467 struct usb_udc
*udc
= gadget
->udc
;
472 dev_vdbg(gadget
->dev
.parent
, "unregistering gadget\n");
474 mutex_lock(&udc_lock
);
475 list_del(&udc
->list
);
476 mutex_unlock(&udc_lock
);
479 usb_gadget_remove_driver(udc
);
481 kobject_uevent(&udc
->dev
.kobj
, KOBJ_REMOVE
);
482 flush_work(&gadget
->work
);
483 device_unregister(&udc
->dev
);
484 device_unregister(&gadget
->dev
);
486 EXPORT_SYMBOL_GPL(usb_del_gadget_udc
);
488 /* ------------------------------------------------------------------------- */
490 static int udc_bind_to_driver(struct usb_udc
*udc
, struct usb_gadget_driver
*driver
)
494 dev_dbg(&udc
->dev
, "registering UDC driver [%s]\n",
497 udc
->driver
= driver
;
498 udc
->dev
.driver
= &driver
->driver
;
499 udc
->gadget
->dev
.driver
= &driver
->driver
;
501 ret
= driver
->bind(udc
->gadget
, driver
);
504 ret
= usb_gadget_udc_start(udc
);
506 driver
->unbind(udc
->gadget
);
509 usb_udc_connect_control(udc
);
511 kobject_uevent(&udc
->dev
.kobj
, KOBJ_CHANGE
);
515 dev_err(&udc
->dev
, "failed to start %s: %d\n",
516 udc
->driver
->function
, ret
);
518 udc
->dev
.driver
= NULL
;
519 udc
->gadget
->dev
.driver
= NULL
;
523 int usb_udc_attach_driver(const char *name
, struct usb_gadget_driver
*driver
)
525 struct usb_udc
*udc
= NULL
;
528 mutex_lock(&udc_lock
);
529 list_for_each_entry(udc
, &udc_list
, list
) {
530 ret
= strcmp(name
, dev_name(&udc
->dev
));
542 ret
= udc_bind_to_driver(udc
, driver
);
544 mutex_unlock(&udc_lock
);
547 EXPORT_SYMBOL_GPL(usb_udc_attach_driver
);
549 int usb_gadget_probe_driver(struct usb_gadget_driver
*driver
)
551 struct usb_udc
*udc
= NULL
;
554 if (!driver
|| !driver
->bind
|| !driver
->setup
)
557 mutex_lock(&udc_lock
);
558 list_for_each_entry(udc
, &udc_list
, list
) {
559 /* For now we take the first one */
564 pr_debug("couldn't find an available UDC\n");
565 mutex_unlock(&udc_lock
);
568 ret
= udc_bind_to_driver(udc
, driver
);
569 mutex_unlock(&udc_lock
);
572 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver
);
574 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
576 struct usb_udc
*udc
= NULL
;
579 if (!driver
|| !driver
->unbind
)
582 mutex_lock(&udc_lock
);
583 list_for_each_entry(udc
, &udc_list
, list
)
584 if (udc
->driver
== driver
) {
585 usb_gadget_remove_driver(udc
);
586 usb_gadget_set_state(udc
->gadget
,
587 USB_STATE_NOTATTACHED
);
592 mutex_unlock(&udc_lock
);
595 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver
);
597 /* ------------------------------------------------------------------------- */
599 static ssize_t
usb_udc_srp_store(struct device
*dev
,
600 struct device_attribute
*attr
, const char *buf
, size_t n
)
602 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
604 if (sysfs_streq(buf
, "1"))
605 usb_gadget_wakeup(udc
->gadget
);
609 static DEVICE_ATTR(srp
, S_IWUSR
, NULL
, usb_udc_srp_store
);
611 static ssize_t
usb_udc_softconn_store(struct device
*dev
,
612 struct device_attribute
*attr
, const char *buf
, size_t n
)
614 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
617 dev_err(dev
, "soft-connect without a gadget driver\n");
621 if (sysfs_streq(buf
, "connect")) {
622 usb_gadget_udc_start(udc
);
623 usb_gadget_connect(udc
->gadget
);
624 } else if (sysfs_streq(buf
, "disconnect")) {
625 usb_gadget_disconnect(udc
->gadget
);
626 udc
->driver
->disconnect(udc
->gadget
);
627 usb_gadget_udc_stop(udc
);
629 dev_err(dev
, "unsupported command '%s'\n", buf
);
635 static DEVICE_ATTR(soft_connect
, S_IWUSR
, NULL
, usb_udc_softconn_store
);
637 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
640 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
641 struct usb_gadget
*gadget
= udc
->gadget
;
643 return sprintf(buf
, "%s\n", usb_state_string(gadget
->state
));
645 static DEVICE_ATTR_RO(state
);
647 #define USB_UDC_SPEED_ATTR(name, param) \
648 ssize_t name##_show(struct device *dev, \
649 struct device_attribute *attr, char *buf) \
651 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
652 return snprintf(buf, PAGE_SIZE, "%s\n", \
653 usb_speed_string(udc->gadget->param)); \
655 static DEVICE_ATTR_RO(name)
657 static USB_UDC_SPEED_ATTR(current_speed
, speed
);
658 static USB_UDC_SPEED_ATTR(maximum_speed
, max_speed
);
660 #define USB_UDC_ATTR(name) \
661 ssize_t name##_show(struct device *dev, \
662 struct device_attribute *attr, char *buf) \
664 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
665 struct usb_gadget *gadget = udc->gadget; \
667 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
669 static DEVICE_ATTR_RO(name)
671 static USB_UDC_ATTR(is_otg
);
672 static USB_UDC_ATTR(is_a_peripheral
);
673 static USB_UDC_ATTR(b_hnp_enable
);
674 static USB_UDC_ATTR(a_hnp_support
);
675 static USB_UDC_ATTR(a_alt_hnp_support
);
676 static USB_UDC_ATTR(is_selfpowered
);
678 static struct attribute
*usb_udc_attrs
[] = {
680 &dev_attr_soft_connect
.attr
,
681 &dev_attr_state
.attr
,
682 &dev_attr_current_speed
.attr
,
683 &dev_attr_maximum_speed
.attr
,
685 &dev_attr_is_otg
.attr
,
686 &dev_attr_is_a_peripheral
.attr
,
687 &dev_attr_b_hnp_enable
.attr
,
688 &dev_attr_a_hnp_support
.attr
,
689 &dev_attr_a_alt_hnp_support
.attr
,
690 &dev_attr_is_selfpowered
.attr
,
694 static const struct attribute_group usb_udc_attr_group
= {
695 .attrs
= usb_udc_attrs
,
698 static const struct attribute_group
*usb_udc_attr_groups
[] = {
703 static int usb_udc_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
705 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
708 ret
= add_uevent_var(env
, "USB_UDC_NAME=%s", udc
->gadget
->name
);
710 dev_err(dev
, "failed to add uevent USB_UDC_NAME\n");
715 ret
= add_uevent_var(env
, "USB_UDC_DRIVER=%s",
716 udc
->driver
->function
);
718 dev_err(dev
, "failed to add uevent USB_UDC_DRIVER\n");
726 static int __init
usb_udc_init(void)
728 udc_class
= class_create(THIS_MODULE
, "udc");
729 if (IS_ERR(udc_class
)) {
730 pr_err("failed to create udc class --> %ld\n",
732 return PTR_ERR(udc_class
);
735 udc_class
->dev_uevent
= usb_udc_uevent
;
738 subsys_initcall(usb_udc_init
);
740 static void __exit
usb_udc_exit(void)
742 class_destroy(udc_class
);
744 module_exit(usb_udc_exit
);
746 MODULE_DESCRIPTION("UDC Framework");
747 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
748 MODULE_LICENSE("GPL v2");