SyterKit 0.4.0.x
SyterKit is a bare-metal framework
Loading...
Searching...
No Matches
Classes | Macros | Typedefs | Enumerations | Functions | Variables
usb.h File Reference
#include "reg/reg-usb.h"
Include dependency graph for usb.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  usb_endpoint_descriptor
 
struct  usb_interface_descriptor
 
struct  usb_configuration_descriptor
 
struct  usb_device_descriptor
 
struct  usb_qualifier_descriptor
 
struct  usb_string_descriptor
 
struct  usb_generic_descriptor
 
struct  usb_device_request
 
struct  sunxi_udc
 
struct  sunxi_ubuf
 
struct  sunxi_usb_setup_req_s
 

Macros

#define USB_DT_DEVICE   0x01
 
#define USB_DT_CONFIG   0x02
 
#define USB_DT_STRING   0x03
 
#define USB_DT_INTERFACE   0x04
 
#define USB_DT_ENDPOINT   0x05
 
#define USB_DT_DEVICE_QUALIFIER   0x06
 
#define USB_DT_HID   (USB_TYPE_CLASS | 0x01)
 
#define USB_DT_REPORT   (USB_TYPE_CLASS | 0x02)
 
#define USB_DT_PHYSICAL   (USB_TYPE_CLASS | 0x03)
 
#define USB_DT_HUB   (USB_TYPE_CLASS | 0x09)
 
#define USB_DT_DEVICE_SIZE   18
 
#define USB_DT_CONFIG_SIZE   9
 
#define USB_DT_INTERFACE_SIZE   9
 
#define USB_DT_ENDPOINT_SIZE   7
 
#define USB_DT_ENDPOINT_AUDIO_SIZE   9 /* Audio extension */
 
#define USB_DT_HUB_NONVAR_SIZE   7
 
#define USB_DT_HID_SIZE   9
 
#define SUNXI_USB_DEVICE_DETECT   (1)
 
#define SUNXI_USB_DEVICE_MASS   (2)
 
#define sunxi_usb_module_init(name, state_init, state_exit, state_reset, standard_req_op, nonstandard_req_op, state_loop, dma_rx_isr, dma_tx_isr)    sunxi_usb_setup_req_t setup_req_##name = {state_init, state_exit, state_reset, standard_req_op, nonstandard_req_op, state_loop, dma_rx_isr, dma_tx_isr};
 
#define sunxi_usb_module_reg(name)   sunxi_udev_active = &setup_req_##name
 
#define sunxi_usb_module_ext(name)   extern sunxi_usb_setup_req_t setup_req_##name
 
#define usb_get_bit8(bp, reg)   (read8(reg) & (1 << (bp)))
 
#define usb_get_bit16(bp, reg)   (read16(reg) & (1 << (bp)))
 
#define usb_get_bit32(bp, reg)   (read32(reg) & (1 << (bp)))
 
#define usb_set_bit8(bp, reg)   (write8((reg), (read8(reg) | (1 << (bp)))))
 
#define usb_set_bit16(bp, reg)   (write16((reg), (read16(reg) | (1 << (bp)))))
 
#define usb_set_bit32(bp, reg)   (write32((reg), (read32(reg) | (1 << (bp)))))
 
#define usb_clear_bit8(bp, reg)   (write8((reg), (read8(reg) & (~(1 << (bp))))))
 
#define usb_clear_bit16(bp, reg)   (write16((reg), (read16(reg) & (~(1 << (bp))))))
 
#define usb_clear_bit32(bp, reg)   (write32((reg), (read32(reg) & (~(1 << (bp))))))
 
#define SUNXI_USB_REQ_SUCCESSED   (0)
 
#define SUNXI_USB_REQ_DEVICE_NOT_SUPPORTED   (-1)
 
#define SUNXI_USB_REQ_UNKNOWN_COMMAND   (-2)
 
#define SUNXI_USB_REQ_UNMATCHED_COMMAND   (-3)
 
#define SUNXI_USB_REQ_DATA_HUNGRY   (-4)
 
#define SUNXI_USB_REQ_OP_ERR   (-5)
 

Typedefs

typedef struct sunxi_udc sunxi_udc_t
 
typedef struct sunxi_ubuf sunxi_ubuf_t
 
typedef struct sunxi_usb_setup_req_s sunxi_usb_setup_req_t
 

Enumerations

enum  usb_device_speed { USB_SPEED_LOW , USB_SPEED_FULL , USB_SPEED_HIGH , USB_SPEED_RESERVED }
 

Functions

struct usb_endpoint_descriptor __attribute__ ((packed))
 
 sunxi_usb_module_ext (SUNXI_USB_DEVICE_DETECT)
 
 sunxi_usb_module_ext (SUNXI_USB_DEVICE_MASS)
 
void sunxi_usb_attach_module (uint32_t device_type)
 Attach a module to the USB controller.
 
int sunxi_usb_init ()
 Initialize the USB controller.
 
void sunxi_usb_dump (uint32_t usbc_base, uint32_t ep_index)
 Dump the USB controller registers for a specific endpoint.
 
void sunxi_usb_ep_reset ()
 Reset all endpoints of the USB controller.
 
void sunxi_usb_irq ()
 Handle the USB interrupt.
 
void sunxi_usb_attach ()
 Attach the USB device to the USB controller.
 
int sunxi_usb_extern_loop ()
 Run the USB device state machine loop once.
 
void sunxi_usb_bulk_ep_reset ()
 Reset the bulk endpoints of the USB controller.
 
int sunxi_usb_start_recv_by_dma (void *mem_base, uint32_t length)
 Start receiving data by DMA.
 
void sunxi_usb_send_setup (uint32_t length, void *buffer)
 Send a setup packet.
 
int sunxi_usb_set_address (uint32_t address)
 Set USB device address.
 
int sunxi_usb_send_data (void *buffer, uint32_t buffer_size)
 Send data over USB.
 
int sunxi_usb_get_ep_max (void)
 Get the maximum number of endpoints.
 
int sunxi_usb_get_ep_in_type (void)
 Get the IN endpoint type.
 
int sunxi_usb_get_ep_out_type (void)
 Get the OUT endpoint type.
 

Variables

uint8_t bLength
 
uint8_t bDescriptorType
 
uint8_t bEndpointAddress
 
uint8_t bmAttributes
 
uint16_t wMaxPacketSize
 
uint8_t bInterval
 
uint8_t bInterfaceNumber
 
uint8_t bAlternateSetting
 
uint8_t bNumEndpoints
 
uint8_t bInterfaceClass
 
uint8_t bInterfaceSubClass
 
uint8_t bInterfaceProtocol
 
uint8_t iInterface
 
uint16_t wTotalLength
 
uint8_t bNumInterfaces
 
uint8_t bConfigurationValue
 
uint8_t iConfiguration
 
uint8_t bMaxPower
 
uint16_t bcdUSB
 
uint8_t bDeviceClass
 
uint8_t bDeviceSubClass
 
uint8_t bDeviceProtocol
 
uint8_t bMaxPacketSize0
 
uint16_t idVendor
 
uint16_t idProduct
 
uint16_t bcdDevice
 
uint8_t iManufacturer
 
uint8_t iProduct
 
uint8_t iSerialNumber
 
uint8_t bNumConfigurations
 
uint8_t breserved
 
uint16_t wData [0]
 
uint8_t bDescriptorSubtype
 
uint8_t request_type
 
uint8_t request
 
uint16_t value
 
uint16_t index
 
uint16_t length
 

Macro Definition Documentation

◆ SUNXI_USB_DEVICE_DETECT

#define SUNXI_USB_DEVICE_DETECT   (1)

◆ SUNXI_USB_DEVICE_MASS

#define SUNXI_USB_DEVICE_MASS   (2)

◆ sunxi_usb_module_ext

#define sunxi_usb_module_ext (   name)    extern sunxi_usb_setup_req_t setup_req_##name

◆ sunxi_usb_module_init

#define sunxi_usb_module_init (   name,
  state_init,
  state_exit,
  state_reset,
  standard_req_op,
  nonstandard_req_op,
  state_loop,
  dma_rx_isr,
  dma_tx_isr 
)     sunxi_usb_setup_req_t setup_req_##name = {state_init, state_exit, state_reset, standard_req_op, nonstandard_req_op, state_loop, dma_rx_isr, dma_tx_isr};

◆ sunxi_usb_module_reg

#define sunxi_usb_module_reg (   name)    sunxi_udev_active = &setup_req_##name

◆ SUNXI_USB_REQ_DATA_HUNGRY

#define SUNXI_USB_REQ_DATA_HUNGRY   (-4)

◆ SUNXI_USB_REQ_DEVICE_NOT_SUPPORTED

#define SUNXI_USB_REQ_DEVICE_NOT_SUPPORTED   (-1)

◆ SUNXI_USB_REQ_OP_ERR

#define SUNXI_USB_REQ_OP_ERR   (-5)

◆ SUNXI_USB_REQ_SUCCESSED

#define SUNXI_USB_REQ_SUCCESSED   (0)

◆ SUNXI_USB_REQ_UNKNOWN_COMMAND

#define SUNXI_USB_REQ_UNKNOWN_COMMAND   (-2)

◆ SUNXI_USB_REQ_UNMATCHED_COMMAND

#define SUNXI_USB_REQ_UNMATCHED_COMMAND   (-3)

◆ usb_clear_bit16

#define usb_clear_bit16 (   bp,
  reg 
)    (write16((reg), (read16(reg) & (~(1 << (bp))))))

◆ usb_clear_bit32

#define usb_clear_bit32 (   bp,
  reg 
)    (write32((reg), (read32(reg) & (~(1 << (bp))))))

◆ usb_clear_bit8

#define usb_clear_bit8 (   bp,
  reg 
)    (write8((reg), (read8(reg) & (~(1 << (bp))))))

◆ USB_DT_CONFIG

#define USB_DT_CONFIG   0x02

◆ USB_DT_CONFIG_SIZE

#define USB_DT_CONFIG_SIZE   9

◆ USB_DT_DEVICE

#define USB_DT_DEVICE   0x01

◆ USB_DT_DEVICE_QUALIFIER

#define USB_DT_DEVICE_QUALIFIER   0x06

◆ USB_DT_DEVICE_SIZE

#define USB_DT_DEVICE_SIZE   18

◆ USB_DT_ENDPOINT

#define USB_DT_ENDPOINT   0x05

◆ USB_DT_ENDPOINT_AUDIO_SIZE

#define USB_DT_ENDPOINT_AUDIO_SIZE   9 /* Audio extension */

◆ USB_DT_ENDPOINT_SIZE

#define USB_DT_ENDPOINT_SIZE   7

◆ USB_DT_HID

#define USB_DT_HID   (USB_TYPE_CLASS | 0x01)

◆ USB_DT_HID_SIZE

#define USB_DT_HID_SIZE   9

◆ USB_DT_HUB

#define USB_DT_HUB   (USB_TYPE_CLASS | 0x09)

◆ USB_DT_HUB_NONVAR_SIZE

#define USB_DT_HUB_NONVAR_SIZE   7

◆ USB_DT_INTERFACE

#define USB_DT_INTERFACE   0x04

◆ USB_DT_INTERFACE_SIZE

#define USB_DT_INTERFACE_SIZE   9

◆ USB_DT_PHYSICAL

#define USB_DT_PHYSICAL   (USB_TYPE_CLASS | 0x03)

◆ USB_DT_REPORT

#define USB_DT_REPORT   (USB_TYPE_CLASS | 0x02)

◆ USB_DT_STRING

#define USB_DT_STRING   0x03

◆ usb_get_bit16

#define usb_get_bit16 (   bp,
  reg 
)    (read16(reg) & (1 << (bp)))

◆ usb_get_bit32

#define usb_get_bit32 (   bp,
  reg 
)    (read32(reg) & (1 << (bp)))

◆ usb_get_bit8

#define usb_get_bit8 (   bp,
  reg 
)    (read8(reg) & (1 << (bp)))

◆ usb_set_bit16

#define usb_set_bit16 (   bp,
  reg 
)    (write16((reg), (read16(reg) | (1 << (bp)))))

◆ usb_set_bit32

#define usb_set_bit32 (   bp,
  reg 
)    (write32((reg), (read32(reg) | (1 << (bp)))))

◆ usb_set_bit8

#define usb_set_bit8 (   bp,
  reg 
)    (write8((reg), (read8(reg) | (1 << (bp)))))

Typedef Documentation

◆ sunxi_ubuf_t

typedef struct sunxi_ubuf sunxi_ubuf_t

◆ sunxi_udc_t

typedef struct sunxi_udc sunxi_udc_t

◆ sunxi_usb_setup_req_t

Enumeration Type Documentation

◆ usb_device_speed

Enumerator
USB_SPEED_LOW 
USB_SPEED_FULL 
USB_SPEED_HIGH 
USB_SPEED_RESERVED 

Function Documentation

◆ __attribute__()

struct umass_bbb_csw_t __attribute__ ( (packed)  )

◆ sunxi_usb_attach()

void sunxi_usb_attach ( )

Attach the USB device to the USB controller.

This function attaches the USB device to the USB controller and enters a loop to handle USB events. It continuously checks for pending USB interrupts, handles the USB interrupt by calling sunxi_usb_irq(), and then processes the USB device state by calling sunxi_udev_active->state_loop(&sunxi_ubuf) multiple times.

Parameters
none
Returns
none

◆ sunxi_usb_attach_module()

void sunxi_usb_attach_module ( uint32_t  device_type)

Attach a module to the USB controller.

This function attaches a module to the USB controller. It registers the specified USB module and switches to the specified device type. Currently, two types of devices are supported: USB device detect and USB mass storage. If an unknown device type is specified, an error message is printed.

Parameters
device_typeThe type of the device to attach
  • SUNXI_USB_DEVICE_DETECT: USB device detect module
  • SUNXI_USB_DEVICE_MASS: USB mass storage module
Returns
none

◆ sunxi_usb_bulk_ep_reset()

void sunxi_usb_bulk_ep_reset ( )

Reset the bulk endpoints of the USB controller.

This function resets the bulk endpoints of the Allwinner A64 USB controller. It configures the bulk-in and bulk-out endpoints for data transfer. The steps involved are as follows:

  1. Get the index of the currently active endpoint.
  2. Configure the bulk-in endpoint for transmitting data to the host.
    • Set the transfer type to bulk (USBC_TS_TYPE_BULK).
    • Set the endpoint type to transmit (USBC_EP_TYPE_TX).
    • Set the maximum packet size for the endpoint based on sunxi_udc_source.bulk_ep_max.
    • Configure the FIFO buffer with a size of sunxi_udc_source.fifo_size and the base address sunxi_udc_source.bulk_out_addr.
    • Enable interrupts for the endpoint.
  3. Configure the bulk-out endpoint for receiving data from the host.
    • Set the transfer type to bulk (USBC_TS_TYPE_BULK).
    • Set the endpoint type to receive (USBC_EP_TYPE_RX).
    • Set the maximum packet size for the endpoint based on sunxi_udc_source.bulk_ep_max.
    • Configure the FIFO buffer with a size of sunxi_udc_source.fifo_size and the base address sunxi_udc_source.bulk_in_addr.
    • Enable interrupts for the endpoint.
  4. Restore the previously active endpoint.
Parameters
none
Returns
none

◆ sunxi_usb_dump()

void sunxi_usb_dump ( uint32_t  usbc_base,
uint32_t  ep_index 
)

Dump the USB controller registers for a specific endpoint.

This function dumps the USB controller registers for a specific endpoint of the Allwinner A64 USB controller.

Parameters
usbc_baseThe base address of the USB controller
ep_indexThe index of the endpoint to dump registers for
Returns
none

◆ sunxi_usb_ep_reset()

void sunxi_usb_ep_reset ( )

Reset all endpoints of the USB controller.

This function resets all endpoints of the Allwinner A64 USB controller by calling the sunxi_usb_bulk_ep_reset() function to reset the bulk endpoints.

Parameters
none
Returns
none

◆ sunxi_usb_extern_loop()

int sunxi_usb_extern_loop ( )

Run the USB device state machine loop once.

This function runs the USB device state machine loop once by calling sunxi_udev_active->state_loop(&sunxi_ubuf) and returning the result.

Parameters
none
Returns
The result of sunxi_udev_active->state_loop(&sunxi_ubuf)

◆ sunxi_usb_get_ep_in_type()

int sunxi_usb_get_ep_in_type ( void  )

Get the IN endpoint type.

This function returns the type of the IN endpoint.

Returns
The IN endpoint type

◆ sunxi_usb_get_ep_max()

int sunxi_usb_get_ep_max ( void  )

Get the maximum number of endpoints.

This function returns the maximum number of endpoints supported by the USB controller.

Returns
The maximum number of endpoints

◆ sunxi_usb_get_ep_out_type()

int sunxi_usb_get_ep_out_type ( void  )

Get the OUT endpoint type.

This function returns the type of the OUT endpoint.

Returns
The OUT endpoint type

◆ sunxi_usb_init()

int sunxi_usb_init ( )

Initialize the USB controller.

This function initializes the USB controller. It first checks if it can successfully initialize the USB device, then initializes the UDC controller source and requests DMA channels for data sending and receiving. Next, it configures the USB device by setting the transfer mode and speed, configuring DMA, etc. Finally, it enables interrupts and opens the USB device.

Returns
0 on success, -1 on failure
Parameters
none

◆ sunxi_usb_irq()

void sunxi_usb_irq ( )

Handle the USB interrupt.

This function is responsible for handling various USB interrupts, including RESET, RESUME, SUSPEND, DISCONNECT, SOF, endpoint 0 (EP0), and data transfers for both transmit (TX) and receive (RX) endpoints. It also handles DMA interrupts for both TX and RX endpoints.

Parameters
None
Returns
None

◆ sunxi_usb_module_ext() [1/2]

sunxi_usb_module_ext ( SUNXI_USB_DEVICE_DETECT  )

◆ sunxi_usb_module_ext() [2/2]

sunxi_usb_module_ext ( SUNXI_USB_DEVICE_MASS  )

◆ sunxi_usb_send_data()

int sunxi_usb_send_data ( void *  buffer,
uint32_t  buffer_size 
)

Send data over USB.

This function sends data over USB. It sets the rx_ready_for_data flag to 0 in the sunxi_ubuf structure and calls sunxi_usb_write_fifo() to write the data to the USB FIFO.

Parameters
bufferThe buffer containing the data to send
buffer_sizeThe size of the data buffer
Returns
0 on success, -1 on failure

◆ sunxi_usb_send_setup()

void sunxi_usb_send_setup ( uint32_t  length,
void *  buffer 
)

Send a setup packet.

This function sends a setup packet over USB. If the length is zero, it sends a zero-length packet. Otherwise, it selects the appropriate FIFO, writes the packet to the FIFO, and calls sunxi_usb_write_complete() to handle the completion of the write operation.

Parameters
lengthThe length of the setup packet
bufferThe buffer containing the setup packet

◆ sunxi_usb_set_address()

int sunxi_usb_set_address ( uint32_t  address)

Set USB device address.

This function sets the USB device address to the specified address. It updates the address value in the sunxi_udc_source structure and calls usb_device_read_data_status() to handle the status phase of the control transfer.

Parameters
addressThe USB device address to set
Returns
SUNXI_USB_REQ_SUCCESSED on success

◆ sunxi_usb_start_recv_by_dma()

int sunxi_usb_start_recv_by_dma ( void *  mem_base,
uint32_t  length 
)

Start receiving data by DMA.

This function starts receiving data by DMA. It configures the USB controller to use DMA for receiving, flushes the cache, enables DMA transfer, and restores the active endpoint after the transfer is started.

Parameters
mem_baseThe base address of the memory buffer
lengthThe length of the data to be received
Returns
0 on success, -1 on failure

Variable Documentation

◆ bAlternateSetting

uint8_t bAlternateSetting

◆ bcdDevice

uint16_t bcdDevice

◆ bcdUSB

uint16_t bcdUSB

◆ bConfigurationValue

uint8_t bConfigurationValue

◆ bDescriptorSubtype

uint8_t bDescriptorSubtype

◆ bDescriptorType

uint8_t bDescriptorType

◆ bDeviceClass

uint8_t bDeviceClass

◆ bDeviceProtocol

uint8_t bDeviceProtocol

◆ bDeviceSubClass

uint8_t bDeviceSubClass

◆ bEndpointAddress

uint8_t bEndpointAddress

◆ bInterfaceClass

uint8_t bInterfaceClass

◆ bInterfaceNumber

uint8_t bInterfaceNumber

◆ bInterfaceProtocol

uint8_t bInterfaceProtocol

◆ bInterfaceSubClass

uint8_t bInterfaceSubClass

◆ bInterval

uint8_t bInterval

◆ bLength

uint8_t bLength

◆ bmAttributes

uint8_t bmAttributes

◆ bMaxPacketSize0

uint8_t bMaxPacketSize0

◆ bMaxPower

uint8_t bMaxPower

◆ bNumConfigurations

uint8_t bNumConfigurations

◆ bNumEndpoints

uint8_t bNumEndpoints

◆ bNumInterfaces

uint8_t bNumInterfaces

◆ breserved

uint8_t breserved

◆ iConfiguration

uint8_t iConfiguration

◆ idProduct

uint16_t idProduct

◆ idVendor

uint16_t idVendor

◆ iInterface

uint8_t iInterface

◆ iManufacturer

uint8_t iManufacturer

◆ index

uint16_t index

◆ iProduct

uint8_t iProduct

◆ iSerialNumber

uint8_t iSerialNumber

◆ length

uint16_t length

◆ request

uint8_t request

◆ request_type

uint8_t request_type

◆ value

uint16_t value

◆ wData

uint16_t wData[0]

◆ wMaxPacketSize

uint16_t wMaxPacketSize

◆ wTotalLength

uint16_t wTotalLength