drivers/usb/usbb_udc.c File Reference

USBB driver: Device part. More...

#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"

Include dependency graph for usbb_udc.c:

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.


Detailed Description

USBB driver: Device part.

This file implements a USB Device Controller (UDC) driver utilizing the USBB controller hardware.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file usbb_udc.c.


Function Documentation

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:

  • The ID pin indicates Device operation. When the driver operates in device-only mode, this condition is assumed to always be true.
  • An acceptable Vbus level from the host is detected.

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.

Here is the call graph for this function:

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.

Here is the call graph for this function:

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.

Parameters:
udc The USB Device Controller instance.
Precondition:
ep0 is ready to transmit data (no other buffers are queued.)
Postcondition:
ep0 has entered the status OUT phase.

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().

Here is the call graph for this function:

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.

Parameters:
udc The USB Device Controller instance.
Precondition:
ep0 is ready to transmit data (no other buffers are queued.)
Postcondition:
ep0 has entered the status IN phase.

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.

Here is the call graph for this function:

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).

Parameters:
udc The USB Device Controller instance
req Request containing IN data for transmission.
Precondition:
No other requests are queued on ep0.
Postcondition:
ep0 has entered the data IN phase.

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().

Here is the call graph for this function:

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).

Parameters:
udc The USB Device Controller instance
req Request to use for receiving the OUT data.
Precondition:
No other requests are queued on ep0.
Postcondition:
ep0 has entered the data OUT phase.

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.

Here is the call graph for this function:

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.

Parameters:
udc The USB Device Controller instance.
data The data to be transmitted on ep0.
len The number of bytes to be transmitted.
Returns:
The number of bytes actually transmitted. This may be less than the requested number of bytes. When sending 8 bytes or less, it is safe to assume that everything will be sent.
Note:
This function should only be used for small quantities of data when it is impractical to submit a buffer asynchronously.
Precondition:
ep0 is ready to transmit data (no other buffers are queued.)
Postcondition:
ep0 is ready to transmit data.

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().

Here is the call graph for this function:

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.

Parameters:
udc USB Device Controller instance.
ep The ID of the endpoint to be un-halted.
Return values:
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.

Here is the call graph for this function:

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.

Parameters:
udc USB Device Controller instance.
ep The ID of the endpoint to be un-wedged.
Precondition:
ep < CONFIG_USBB_NR_EP

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().

Here is the call graph for this function:

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.

Parameters:
udc USB Device Controller instance.
desc USB Endpoint Descriptor for the new endpoint.
nr_banks The number of FIFO banks to allocate.
Returns:
A cookie identifying the new endpoint, or a negative error code.

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.

Here is the call graph for this function:

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.

Parameters:
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.

Here is the call graph for this function:

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.

Parameters:
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().

Here is the call graph for this function:

int udc_ep_is_halted ( struct udc udc,
usb_ep_id_t  ep 
)

Check if a given endpoint is halted.

Parameters:
udc USB Device Controller instance.
ep The ID of the endpoint to check.
Return values:
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().

Parameters:
udc USB Device Controller instance.
ep The ID of the endpoint to check.
Return values:
true if ep is wedged
false if ep is not wedged.
Precondition:
ep < CONFIG_USBB_NR_EP

Definition at line 781 of file usbb_udc.c.

References assert, and test_bit().

Here is the call graph for this function:

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.

Parameters:
udc USB Device Controller instance.
ep The ID of the endpoint to be halted.
Return values:
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.

Parameters:
udc USB Device Controller instance.
ep The ID of the endpoint to be wedged.
Precondition:
ep < CONFIG_USBB_NR_EP

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().

Here is the call graph for this function:

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.

Parameters:
udc The USB Device Controller instance
ep_id The endpoint ID on which to queue the buffer.
req Request containing IN data for transmission.
Precondition:
Interrupts not masked

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().

Here is the call graph for this function:

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.

Parameters:
udc The USB Device Controller instance
ep_id The endpoint ID on which to queue the buffer.
req Request containing OUT data for reception.
Precondition:
Interrupts not masked

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().

Here is the call graph for this function:

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.

Parameters:
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.

Parameters:
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).
Returns:
STATUS_OK if the endpoint was configured successfully or a negative status_code otherwise.

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().

Here is the call graph for this function:

static void usbb_udc_dma_buf_done ( struct buffer buf  )  [static]

Precondition:
In interrupt handler and/or interrupts disabled.

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]

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.

Returns:
A USBB device controller instance.

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().

Here is the call graph for this function:

void usbb_udc_interrupt ( struct usbb_udc *  udcb  ) 

The USBB device-mode interrupt handler.

For internal use only.

Parameters:
udcb The USBB Device Controller instance.

Definition at line 1661 of file usbb_udc.c.

References softirq_raise().

Referenced by usbb_interrupt().

Here is the call graph for this function:

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.

Parameters:
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]

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.

Note:
This function will unmask interrupts while processing the queue, but will return with interrupts masked.
Precondition:
ep->active == NULL

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().

Here is the call graph for this function:

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.

Note:
This function will unmask interrupts while processing the queue, but will return with interrupts masked.
Precondition:
ep->active == NULL

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().

Here is the call graph for this function:

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.

Parameters:
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.

Here is the call graph for this function:

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.

Parameters:
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.

Here is the call graph for this function:


Generated on Tue Sep 15 10:20:25 2009 for libavr32 by  doxygen 1.5.8