#include <assert.h>
#include <byteorder.h>
#include <debug.h>
#include <dma.h>
#include <io.h>
#include <softirq.h>
#include <status-codes.h>
#include <string.h>
#include <util.h>
#include <arch/interrupt.h>
#include <chip/memory-map.h>
#include <chip/usbb.h>
#include <usb/request.h>
#include <usb/function.h>
#include <usb/udc.h>
#include <app/config_usb.h>
#include <app/softirq.h>
#include "usbb_internal.h"
#include "usbb_regs.h"

Go to the source code of this file.
Functions | |
| static void | usbb_udc_dma_buf_done (struct buffer *buf) |
| void | udc_ep0_submit_out_req (struct udc *udc, struct usb_request *req) |
| Submit an OUT request on the default control endpoint. | |
| void | udc_ep0_submit_in_req (struct udc *udc, struct usb_request *req) |
| Submit an IN request on the default control endpoint. | |
| int | udc_ep0_write_sync (struct udc *udc, const void *data, size_t len) |
| Transmit IN data on the default control data synchronously. | |
| void | udc_ep0_send_status (struct udc *udc) |
| Send a status packet on the default control endpoint. | |
| void | udc_ep0_expect_status (struct udc *udc) |
| Signal that a status packet is expected on the default control endpoint. | |
| void | udc_set_address (struct udc *udc, unsigned int addr) |
| Signal that the UDC is to change its USB address after the status IN phase is complete. | |
| static void | usbb_udc_submit_out_queue (struct usbb_udc *udcb, usb_ep_id_t ep_id, struct usbb_udc_ep *ep) |
| Submit queued buffers on a non-control OUT endpoint. | |
| static void | usbb_udc_submit_in_queue (struct usbb_udc *udcb, usb_ep_id_t ep_id, struct usbb_udc_ep *ep) |
| Submit queued buffers on a non-control IN endpoint. | |
| void | udc_ep_submit_out_req (struct udc *udc, usb_ep_id_t ep_id, struct usb_request *req) |
| Submit an OUT request on a non-control endpoint. | |
| void | udc_ep_submit_in_req (struct udc *udc, usb_ep_id_t ep_id, struct usb_request *req) |
| Submit an IN request on a non-control endpoint. | |
| int | udc_ep_is_halted (struct udc *udc, usb_ep_id_t ep) |
| Check if a given endpoint is halted. | |
| int | udc_ep_set_halt (struct udc *udc, usb_ep_id_t ep) |
| Set the halted state of an endpoint. | |
| int | udc_ep_clear_halt (struct udc *udc, usb_ep_id_t ep) |
| Clear the halted state of an endpoint. | |
| bool | udc_ep_is_wedged (struct udc *udc, usb_ep_id_t ep) |
| Check if a given endpoint is wedged. | |
| void | udc_ep_set_wedge (struct udc *udc, usb_ep_id_t ep) |
| Set the wedged state of an endpoint. | |
| void | udc_ep_clear_wedge (struct udc *udc, usb_ep_id_t ep) |
| Clear the wedged state of an endpoint. | |
| static int | usbb_udc_configure_ep (unsigned int id, unsigned int size, enum usb_ep_xfer_type type, bool is_in, unsigned int nr_banks, bool autosw) |
| void | udc_ep_flush (struct udc *udc, usb_ep_id_t ep_id) |
| Terminate all pending requests on an endpoint. | |
| usb_ep_id_t | udc_ep_create (struct udc *udc, const struct usb_endpoint_descriptor *desc, unsigned int nr_banks) |
| Create a new endpoint. | |
| void | udc_ep_destroy (struct udc *udc, usb_ep_id_t ep_id) |
| Destroy a previously created endpoint. | |
| static void | usbb_udc_dma_interrupt (struct usbb_udc *udcb, usb_ep_id_t ep_id) |
| Handle a DMA interrupt for an endpoint. | |
| static void | usbb_udc_softirq (void *data) |
| The USBB device-mode soft interrupt handler. | |
| void | usbb_udc_interrupt (struct usbb_udc *udcb) |
| The USBB device-mode interrupt handler. | |
| void | usbb_udc_vbus_on (struct usbb_udc *udcb) |
| Signal that a high Vbus level has been detected. | |
| void | usbb_udc_vbus_off (struct usbb_udc *udcb) |
| Signal that a low Vbus level has been detected. | |
| void | udc_attach (struct udc *udc) |
| Attach udc to the bus when possible. | |
| void | udc_detach (struct udc *udc) |
| Detach udc from the bus. | |
| struct usbb_udc * | usbb_udc_init (void) |
| Initialize the device part of the USBB controller. | |
| void | usbb_udc_shutdown (struct usbb_udc *udcb) |
| Shut down the device part of the USBB controller. | |
This file implements a USB Device Controller (UDC) driver utilizing the USBB controller hardware.
Definition in file usbb_udc.c.
| void udc_attach | ( | struct udc * | udc | ) |
Attach udc to the bus when possible.
Call this function to signal that the application is ready for the UDC to attach to the bus. This will cause the UDC to attach whenever the following conditions are present:
Definition at line 1757 of file usbb_udc.c.
References cpu_irq_restore(), cpu_irq_save(), udc::flags, set_bit(), test_bit(), and USB_DEV_AUTOATTACH.

| void udc_detach | ( | struct udc * | udc | ) |
Detach udc from the bus.
Call this function to forcibly detach the UDC from the bus. The UDC will detach immediately and won't reattach until udc_attach() is called, subject to the conditions listed for that function.
Definition at line 1777 of file usbb_udc.c.
References clear_bit(), cpu_irq_restore(), cpu_irq_save(), udc::flags, test_bit(), and USB_DEV_AUTOATTACH.

| void udc_ep0_expect_status | ( | struct udc * | udc | ) |
Signal that a status packet is expected on the default control endpoint.
This function marks the end of the data IN phase, and signals that a status OUT packet is expected.
| udc | The USB Device Controller instance. |
Definition at line 282 of file usbb_udc.c.
References assert, EP0_STATE_STATUS_OUT, test_bit(), and USBB_EP_TXINI.
Referenced by usb_func_get_interface().

| void udc_ep0_send_status | ( | struct udc * | udc | ) |
Send a status packet on the default control endpoint.
This function will send a zero-length status packet on ep0. It does not wait for completion, as a status packet marks the end of a control transaction so no further action by the function driver is necessary.
| udc | The USB Device Controller instance. |
Definition at line 253 of file usbb_udc.c.
References assert, dbg_printf, EP0_STATE_STATUS_IN, test_bit(), USBB_EP_RXOUTI, and USBB_EP_TXINI.

| void udc_ep0_submit_in_req | ( | struct udc * | udc, | |
| struct usb_request * | req | |||
| ) |
Submit an IN request on the default control endpoint.
This function queues a USB request for transmitting IN data on the default control endpoint (ep0).
| udc | The USB Device Controller instance | |
| req | Request containing IN data for transmission. |
Definition at line 187 of file usbb_udc.c.
References assert, barrier, usb_request::buf_list, EP0_STATE_DATA_IN, usb_request::node, set_bit(), slist_borrow_to_tail(), slist_insert_tail(), slist_is_empty(), test_bit(), and USBB_EP_TXINI.
Referenced by usb_func_get_descriptor().

| void udc_ep0_submit_out_req | ( | struct udc * | udc, | |
| struct usb_request * | req | |||
| ) |
Submit an OUT request on the default control endpoint.
This function queues a USB request for receiving OUT data on the default control endpoint (ep0).
| udc | The USB Device Controller instance | |
| req | Request to use for receiving the OUT data. |
Definition at line 157 of file usbb_udc.c.
References assert, barrier, usb_request::buf_list, EP0_STATE_DATA_OUT, usb_request::node, set_bit(), slist_borrow_to_tail(), slist_insert_tail(), slist_is_empty(), test_bit(), and USBB_EP_RXOUTI.

| int udc_ep0_write_sync | ( | struct udc * | udc, | |
| const void * | data, | |||
| size_t | len | |||
| ) |
Transmit IN data on the default control data synchronously.
This function will submit IN data on the default control endpoint (ep0) and busy-wait until it has been sent.
| udc | The USB Device Controller instance. | |
| data | The data to be transmitted on ep0. | |
| len | The number of bytes to be transmitted. |
Definition at line 225 of file usbb_udc.c.
References assert, barrier, memcpy(), min, test_bit(), and USBB_EP_TXINI.
Referenced by usb_func_get_interface().

| int udc_ep_clear_halt | ( | struct udc * | udc, | |
| usb_ep_id_t | ep | |||
| ) |
Clear the halted state of an endpoint.
After this function is called, any transaction on ep will be handled normally, i.e. a STALL hanshake will not be sent, and the data toggle sequence will start at DATA0.
| udc | USB Device Controller instance. | |
| ep | The ID of the endpoint to be un-halted. |
| 0 | if the endpoint was successfully halted. | |
| -1 | if the endpoint address is invalid. |
Definition at line 744 of file usbb_udc.c.
References dbg_printf, test_bit(), USBB_UECON_RSTDT, and USBB_UECON_STALLRQ.

| void udc_ep_clear_wedge | ( | struct udc * | udc, | |
| usb_ep_id_t | ep | |||
| ) |
Clear the wedged state of an endpoint.
After this function is called, the endpoint halt condition may be cleared by calling udc_ep_clear_halt(). In particular, the host is allowed to clear the halt condition using the ClearFeature(HALT) control request.
This function may be called even if the endpoint isn't wedged, but if it is wedged, it must be halted too.
| udc | USB Device Controller instance. | |
| ep | The ID of the endpoint to be un-wedged. |
Definition at line 838 of file usbb_udc.c.
References assert, clear_bit(), cpu_irq_restore(), cpu_irq_save(), dbg_printf, test_bit(), and udc_ep_is_halted().

| usb_ep_id_t udc_ep_create | ( | struct udc * | udc, | |
| const struct usb_endpoint_descriptor * | desc, | |||
| unsigned int | nr_banks | |||
| ) |
Create a new endpoint.
Create a new endpoint matching a given endpoint descriptor. The transfer type, endpoint address, and FIFO bank size parameters are taken from the descriptor.
| udc | USB Device Controller instance. | |
| desc | USB Endpoint Descriptor for the new endpoint. | |
| nr_banks | The number of FIFO banks to allocate. |
Definition at line 1009 of file usbb_udc.c.
References assert, usb_endpoint_descriptor::bEndpointAddress, usb_endpoint_descriptor::bmAttributes, cpu_irq_restore(), cpu_irq_save(), dbg_printf, set_bit(), slist_init(), STATUS_INVALID_PARAM, usbb_udc_configure_ep(), USBB_UDINT_EP, and usb_endpoint_descriptor::wMaxPacketSize.

| void udc_ep_destroy | ( | struct udc * | udc, | |
| usb_ep_id_t | ep_id | |||
| ) |
Destroy a previously created endpoint.
This function will disable the specified endpoint, terminating all queued buffers.
| udc | USB Device Controller instance. | |
| ep_id | USB Endpoint ID previously returned by udc_ep_create(). |
Definition at line 1072 of file usbb_udc.c.
References assert, cpu_irq_restore(), cpu_irq_save(), dbg_printf, and USBB_UERST_EPEN.

| void udc_ep_flush | ( | struct udc * | udc, | |
| usb_ep_id_t | ep_id | |||
| ) |
Terminate all pending requests on an endpoint.
This function will flush an endpoint, terminating all queued requests with an error status. After this function returns, the endpoint request queue will be empty.
| udc | USB Device Controller instance. | |
| ep_id | USB Endpoint ID previously returned by udc_ep_create(). |
Definition at line 978 of file usbb_udc.c.
References cpu_irq_restore(), cpu_irq_save(), dbg_printf, and set_bit().

| int udc_ep_is_halted | ( | struct udc * | udc, | |
| usb_ep_id_t | ep | |||
| ) |
Check if a given endpoint is halted.
| udc | USB Device Controller instance. | |
| ep | The ID of the endpoint to check. |
| 1 | if ep is halted, i.e. the controller will respond with a STALL handshake to any transaction other than SETUP. | |
| 0 | if ep is not halted. | |
| -1 | if the endpoint address is invalid. |
Definition at line 652 of file usbb_udc.c.
References USBB_UECON_STALLRQ.
Referenced by udc_ep_clear_wedge().
| bool udc_ep_is_wedged | ( | struct udc * | udc, | |
| usb_ep_id_t | ep | |||
| ) |
Check if a given endpoint is wedged.
A wedged endpoint is a halted endpoint where udc_ep_clear_halt() requests are ignored. To un-halt an wedged endpoint, first call udc_ep_clear_wedge(), then call udc_ep_clear_halt().
| udc | USB Device Controller instance. | |
| ep | The ID of the endpoint to check. |
| true | if ep is wedged | |
| false | if ep is not wedged. |
Definition at line 781 of file usbb_udc.c.
References assert, and test_bit().

| int udc_ep_set_halt | ( | struct udc * | udc, | |
| usb_ep_id_t | ep | |||
| ) |
Set the halted state of an endpoint.
After this function is called, any transaction on ep will result in a STALL hanshake being sent. Any pending transactions will be performed first, however.
| udc | USB Device Controller instance. | |
| ep | The ID of the endpoint to be halted. |
| 0 | if the endpoint was successfully halted. | |
| -1 | if the endpoint address is invalid. |
Definition at line 673 of file usbb_udc.c.
References dbg_printf, USBB_UECFG_EPDIR_IN, USBB_UECON_STALLRQ, and USBB_UESTA_GET_NBUSYBK.
Referenced by udc_ep_set_wedge().
| void udc_ep_set_wedge | ( | struct udc * | udc, | |
| usb_ep_id_t | ep | |||
| ) |
Set the wedged state of an endpoint.
After this function is called, any transaction on ep will result in a STALL hanshake being sent, and all requests to clear the halt condition will be ignored. Any pending transactions will be performed first, however.
| udc | USB Device Controller instance. | |
| ep | The ID of the endpoint to be wedged. |
Definition at line 802 of file usbb_udc.c.
References assert, cpu_irq_restore(), cpu_irq_save(), dbg_printf, set_bit(), and udc_ep_set_halt().

| void udc_ep_submit_in_req | ( | struct udc * | udc, | |
| usb_ep_id_t | ep_id, | |||
| struct usb_request * | req | |||
| ) |
Submit an IN request on a non-control endpoint.
This function queues a USB request for transmitting IN data on a non-control endpoint.
| udc | The USB Device Controller instance | |
| ep_id | The endpoint ID on which to queue the buffer. | |
| req | Request containing IN data for transmission. |
ep > 0 && ep < CONFIG_USBB_NR_EP
Definition at line 615 of file usbb_udc.c.
References assert, usb_request::buf_list, cpu_irq_disable, cpu_irq_enable, cpu_irq_is_enabled, usb_request::node, slist_borrow_to_tail(), slist_insert_tail(), STATUS_IO_ERROR, test_bit(), and usbb_udc_submit_in_queue().

| void udc_ep_submit_out_req | ( | struct udc * | udc, | |
| usb_ep_id_t | ep_id, | |||
| struct usb_request * | req | |||
| ) |
Submit an OUT request on a non-control endpoint.
This function queues a USB request for receiving OUT data on a non-control endpoint.
| udc | The USB Device Controller instance | |
| ep_id | The endpoint ID on which to queue the buffer. | |
| req | Request containing OUT data for reception. |
ep > 0 && ep < CONFIG_USBB_NR_EP
Definition at line 576 of file usbb_udc.c.
References assert, usb_request::buf_list, cpu_irq_disable, cpu_irq_enable, cpu_irq_is_enabled, usb_request::node, slist_borrow_to_tail(), slist_insert_tail(), STATUS_IO_ERROR, test_bit(), and usbb_udc_submit_out_queue().
Referenced by msc_submit_write_data_req().

| void udc_set_address | ( | struct udc * | udc, | |
| unsigned int | addr | |||
| ) |
Signal that the UDC is to change its USB address after the status IN phase is complete.
| udc | The USB Device Controller instance. | |
| addr | The new USB address to be used starting from the next control transaction. |
Definition at line 304 of file usbb_udc.c.
| static int usbb_udc_configure_ep | ( | unsigned int | id, | |
| unsigned int | size, | |||
| enum usb_ep_xfer_type | type, | |||
| bool | is_in, | |||
| unsigned int | nr_banks, | |||
| bool | autosw | |||
| ) | [static] |
Configure an endpoint at the hardware level.
| id | Endpoint number. | |
| size | Maximum packet size. | |
| type | Endpoint transfer type. | |
| is_in | true if the endpoint is an IN endpoint. Must be false for control endpoints. | |
| nr_banks | Number of FIFO banks (1, 2 or 3). |
Definition at line 867 of file usbb_udc.c.
References assert, cpu_irq_restore(), cpu_irq_save(), dbg_printf, ilog2(), STATUS_INVALID_PARAM, STATUS_OK, USBB_UECFG_ALLOC, USBB_UECFG_EPAUTOSW, USBB_UECFG_EPBK, USBB_UECFG_EPDIR_IN, USBB_UECFG_EPSIZE, USBB_UECFG_EPTYPE, USBB_UERST_EPEN, USBB_UERST_EPRST, and USBB_UESTA_CFGOK.
Referenced by udc_ep_create(), and usbb_udc_softirq().

| static void usbb_udc_dma_buf_done | ( | struct buffer * | buf | ) | [static] |
Definition at line 129 of file usbb_udc.c.
References buffer::dma_desc.
Referenced by usbb_udc_dma_interrupt().
| static void usbb_udc_dma_interrupt | ( | struct usbb_udc * | udcb, | |
| usb_ep_id_t | ep_id | |||
| ) | [static] |
Handle a DMA interrupt for an endpoint.
For internal use only.
Definition at line 1473 of file usbb_udc.c.
References assert, usb_request::buf_list, usb_request::bytes_xfered, clear_bit(), dbg_printf, dbg_verbose, buffer::dma_desc, usbb_sw_dma_desc::hw, slist::last, buffer::len, usb_request::node, buffer::node, slist_insert_head(), slist_is_empty(), slist_peek_head, slist_pop_head, unlikely, USBB_DMA_CH_ACTIVE, USBB_DMA_EOT, usbb_udc_dma_buf_done(), usbb_udc_submit_in_queue(), usbb_udc_submit_out_queue(), and USBB_UDINT_DMA.
Referenced by usbb_udc_softirq().

| struct usbb_udc* usbb_udc_init | ( | void | ) | [read] |
Initialize the device part of the USBB controller.
For internal use only.
This function does any device-side initialization necessary when the USBB controller as a whole is being initialized. It does not enable any device-side functionality.
Definition at line 1802 of file usbb_udc.c.
References slist_init(), softirq_set_handler(), usbb_udc_softirq(), USBB_UDCON_LS, and USBB_UDCON_SPDCONF_FULL.
Referenced by usbb_init().

| void usbb_udc_interrupt | ( | struct usbb_udc * | udcb | ) |
The USBB device-mode interrupt handler.
For internal use only.
| udcb | The USBB Device Controller instance. |
Definition at line 1661 of file usbb_udc.c.
References softirq_raise().
Referenced by usbb_interrupt().

| void usbb_udc_shutdown | ( | struct usbb_udc * | udcb | ) |
Shut down the device part of the USBB controller.
For internal use only.
This function does any device-side cleanups necessary when the USBB controller as a whole is being shut down.
| udcb | The USBB device controller instance. |
Definition at line 1834 of file usbb_udc.c.
Referenced by usbb_init().
| static void usbb_udc_softirq | ( | void * | data | ) | [static] |
The USBB device-mode soft interrupt handler.
For internal use only.
Definition at line 1587 of file usbb_udc.c.
References udc::address, clear_bit(), dbg_printf, EP0_STATE_SETUP, udc::flags, udc::speed, USB_DEV_IS_SUSPENDED, usb_func_reset(), usbb_udc_configure_ep(), usbb_udc_dma_interrupt(), USBB_UDINT_DMA, USBB_UDINT_EORST, USBB_UDINT_EP, USBB_USBSTA_SPEED_FULL, USBB_USBSTA_SPEED_HIGH, USBB_USBSTA_SPEED_LOW, and USBB_USBSTA_SPEED_MASK.
Referenced by usbb_udc_init().

| static void usbb_udc_submit_in_queue | ( | struct usbb_udc * | udcb, | |
| usb_ep_id_t | ep_id, | |||
| struct usbb_udc_ep * | ep | |||
| ) | [static] |
Submit queued buffers on a non-control IN endpoint.
For internal use only.
Interrupts masked
Definition at line 436 of file usbb_udc.c.
References buffer::addr, assert, barrier, usb_request::buf_list, clear_bit(), cpu_irq_disable, cpu_irq_enable, dbg_printf, buffer::dma_desc, usb_request::flags, usbb_sw_dma_desc::hw, slist::last, buffer::len, buffer::node, usb_request::node, buffer_addr_t::phys, usbb_sw_dma_desc::phys, usb_request::req_done, set_bit(), slist_for_each_safe, slist_is_empty(), slist_node_is_last(), slist_peek_head, slist_peek_head_node(), slist_peek_next, test_bit(), USB_REQ_SHORT_PKT, USBB_DMA_BYTE_LEN, USBB_DMA_CH_EN, USBB_DMA_DMAEND_EN, USBB_DMA_EOBUFF, USBB_DMA_LD_NXT_CH_DESC_EN, and USBB_UDINT_DMA.
Referenced by udc_ep_submit_in_req(), and usbb_udc_dma_interrupt().

| static void usbb_udc_submit_out_queue | ( | struct usbb_udc * | udcb, | |
| usb_ep_id_t | ep_id, | |||
| struct usbb_udc_ep * | ep | |||
| ) | [static] |
Submit queued buffers on a non-control OUT endpoint.
For internal use only.
Interrupts masked
Definition at line 319 of file usbb_udc.c.
References buffer::addr, assert, barrier, usb_request::buf_list, clear_bit(), cpu_irq_disable, cpu_irq_enable, dbg_printf, buffer::dma_desc, usbb_sw_dma_desc::hw, slist::last, buffer::len, buffer::node, usb_request::node, buffer_addr_t::phys, usbb_sw_dma_desc::phys, usb_request::req_done, set_bit(), slist_for_each_safe, slist_is_empty(), slist_node_is_last(), slist_peek_head, slist_peek_head_node(), slist_peek_next, test_bit(), USBB_DMA_BUFF_CLOSE_IN_EN, USBB_DMA_BYTE_LEN, USBB_DMA_CH_EN, USBB_DMA_EOBUFF, USBB_DMA_EOT, USBB_DMA_LD_NXT_CH_DESC_EN, and USBB_UDINT_DMA.
Referenced by udc_ep_submit_out_req(), and usbb_udc_dma_interrupt().

| void usbb_udc_vbus_off | ( | struct usbb_udc * | udcb | ) |
Signal that a low Vbus level has been detected.
For internal use only.
This function is called by the USBB bus interface driver when Vbus power is no longer provided by the host.
| udcb | The USBB Device Controller instance |
Definition at line 1734 of file usbb_udc.c.
References clear_bit(), dbg_printf, udc::flags, test_bit(), and USB_DEV_HAS_POWER.

| void usbb_udc_vbus_on | ( | struct usbb_udc * | udcb | ) |
Signal that a high Vbus level has been detected.
For internal use only.
This function is called by the USBB bus interface driver when Vbus power is provided by the host.
| udcb | The USBB Device Controller instance |
Definition at line 1714 of file usbb_udc.c.
References dbg_printf, udc::flags, set_bit(), test_bit(), and USB_DEV_HAS_POWER.

1.5.8