drivers: usb: unify endpoint helper macros
Unify endpoint index and direction helper macros across all usb device drivers. Signed-off-by: Johann Fischer <j.fischer@phytec.de>
This commit is contained in:
parent
3b126e30e2
commit
b95558dd9f
9 changed files with 186 additions and 204 deletions
|
@ -27,13 +27,6 @@
|
|||
#include <logging/log.h>
|
||||
LOG_MODULE_REGISTER(usb_dc_dw);
|
||||
|
||||
/* convert from endpoint address to hardware endpoint index */
|
||||
#define USB_DW_EP_ADDR2IDX(ep) ((ep) & ~USB_EP_DIR_MASK)
|
||||
/* get direction from endpoint address */
|
||||
#define USB_DW_EP_ADDR2DIR(ep) ((ep) & USB_EP_DIR_MASK)
|
||||
/* convert from hardware endpoint index and direction to endpoint address */
|
||||
#define USB_DW_EP_IDX2ADDR(idx, dir) ((idx) | ((dir) & USB_EP_DIR_MASK))
|
||||
|
||||
/* Number of SETUP back-to-back packets */
|
||||
#define USB_DW_SUP_CNT (1)
|
||||
|
||||
|
@ -97,14 +90,12 @@ static void usb_dw_reg_dump(void)
|
|||
|
||||
static uint8_t usb_dw_ep_is_valid(uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USB_DW_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
/* Check if ep enabled */
|
||||
if ((USB_DW_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) &&
|
||||
ep_idx < USB_DW_OUT_EP_NUM) {
|
||||
if ((USB_EP_DIR_IS_OUT(ep)) && ep_idx < USB_DW_OUT_EP_NUM) {
|
||||
return 1;
|
||||
} else if ((USB_DW_EP_ADDR2DIR(ep) == USB_EP_DIR_IN) &&
|
||||
ep_idx < USB_DW_IN_EP_NUM) {
|
||||
} else if ((USB_EP_DIR_IS_IN(ep)) && ep_idx < USB_DW_IN_EP_NUM) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -113,13 +104,13 @@ static uint8_t usb_dw_ep_is_valid(uint8_t ep)
|
|||
|
||||
static uint8_t usb_dw_ep_is_enabled(uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USB_DW_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
/* Check if ep enabled */
|
||||
if ((USB_DW_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) &&
|
||||
if ((USB_EP_DIR_IS_OUT(ep)) &&
|
||||
usb_dw_ctrl.out_ep_ctrl[ep_idx].ep_ena) {
|
||||
return 1;
|
||||
} else if ((USB_DW_EP_ADDR2DIR(ep) == USB_EP_DIR_IN) &&
|
||||
} else if ((USB_EP_DIR_IS_IN(ep)) &&
|
||||
usb_dw_ctrl.in_ep_ctrl[ep_idx].ep_ena) {
|
||||
return 1;
|
||||
}
|
||||
|
@ -189,7 +180,7 @@ static int usb_dw_tx_fifo_avail(int ep)
|
|||
/* Choose a FIFO number for an IN endpoint */
|
||||
static int usb_dw_set_fifo(uint8_t ep)
|
||||
{
|
||||
int ep_idx = USB_DW_EP_ADDR2IDX(ep);
|
||||
int ep_idx = USB_EP_GET_IDX(ep);
|
||||
volatile uint32_t *reg = &USB_DW->in_ep_reg[ep_idx].diepctl;
|
||||
uint32_t val;
|
||||
int fifo = 0;
|
||||
|
@ -238,11 +229,11 @@ static int usb_dw_ep_set(uint8_t ep,
|
|||
uint32_t ep_mps, enum usb_dc_ep_transfer_type ep_type)
|
||||
{
|
||||
volatile uint32_t *p_depctl;
|
||||
uint8_t ep_idx = USB_DW_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
LOG_DBG("%s ep %x, mps %d, type %d", __func__, ep, ep_mps, ep_type);
|
||||
|
||||
if (USB_DW_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
p_depctl = &USB_DW->out_ep_reg[ep_idx].doepctl;
|
||||
usb_dw_ctrl.out_ep_ctrl[ep_idx].mps = ep_mps;
|
||||
} else {
|
||||
|
@ -309,7 +300,7 @@ static int usb_dw_ep_set(uint8_t ep,
|
|||
*p_depctl |= USB_DW_DEPCTL_SETDOPID;
|
||||
}
|
||||
|
||||
if (USB_DW_EP_ADDR2DIR(ep) == USB_EP_DIR_IN) {
|
||||
if (USB_EP_DIR_IS_IN(ep)) {
|
||||
int ret = usb_dw_set_fifo(ep);
|
||||
|
||||
if (ret) {
|
||||
|
@ -322,7 +313,7 @@ static int usb_dw_ep_set(uint8_t ep,
|
|||
|
||||
static void usb_dw_prep_rx(const uint8_t ep, uint8_t setup)
|
||||
{
|
||||
enum usb_dw_out_ep_idx ep_idx = USB_DW_EP_ADDR2IDX(ep);
|
||||
enum usb_dw_out_ep_idx ep_idx = USB_EP_GET_IDX(ep);
|
||||
uint32_t ep_mps = usb_dw_ctrl.out_ep_ctrl[ep_idx].mps;
|
||||
|
||||
/* Set max RX size to EP mps so we get an interrupt
|
||||
|
@ -346,7 +337,7 @@ static void usb_dw_prep_rx(const uint8_t ep, uint8_t setup)
|
|||
static int usb_dw_tx(uint8_t ep, const uint8_t *const data,
|
||||
uint32_t data_len)
|
||||
{
|
||||
enum usb_dw_in_ep_idx ep_idx = USB_DW_EP_ADDR2IDX(ep);
|
||||
enum usb_dw_in_ep_idx ep_idx = USB_EP_GET_IDX(ep);
|
||||
uint32_t max_xfer_size, max_pkt_cnt, pkt_cnt, avail_space;
|
||||
uint32_t ep_mps = usb_dw_ctrl.in_ep_ctrl[ep_idx].mps;
|
||||
unsigned int key;
|
||||
|
@ -571,14 +562,14 @@ static inline void usb_dw_int_rx_flvl_handler(void)
|
|||
case USB_DW_GRXSTSR_PKT_STS_SETUP:
|
||||
/* Call the registered callback if any */
|
||||
if (ep_cb) {
|
||||
ep_cb(USB_DW_EP_IDX2ADDR(ep_idx, USB_EP_DIR_OUT),
|
||||
ep_cb(USB_EP_GET_ADDR(ep_idx, USB_EP_DIR_OUT),
|
||||
USB_DC_EP_SETUP);
|
||||
}
|
||||
|
||||
break;
|
||||
case USB_DW_GRXSTSR_PKT_STS_OUT_DATA:
|
||||
if (ep_cb) {
|
||||
ep_cb(USB_DW_EP_IDX2ADDR(ep_idx, USB_EP_DIR_OUT),
|
||||
ep_cb(USB_EP_GET_ADDR(ep_idx, USB_EP_DIR_OUT),
|
||||
USB_DC_EP_DATA_OUT);
|
||||
}
|
||||
|
||||
|
@ -614,7 +605,7 @@ static inline void usb_dw_int_iep_handler(void)
|
|||
(ep_int_status & USB_DW_DIEPINT_XFER_COMPL)) {
|
||||
|
||||
/* Call the registered callback */
|
||||
ep_cb(USB_DW_EP_IDX2ADDR(ep_idx, USB_EP_DIR_IN),
|
||||
ep_cb(USB_EP_GET_ADDR(ep_idx, USB_EP_DIR_IN),
|
||||
USB_DC_EP_DATA_IN);
|
||||
}
|
||||
}
|
||||
|
@ -778,7 +769,7 @@ int usb_dc_set_address(const uint8_t addr)
|
|||
|
||||
int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data * const cfg)
|
||||
{
|
||||
uint8_t ep_idx = USB_DW_EP_ADDR2IDX(cfg->ep_addr);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(cfg->ep_addr);
|
||||
|
||||
LOG_DBG("ep %x, mps %d, type %d", cfg->ep_addr, cfg->ep_mps,
|
||||
cfg->ep_type);
|
||||
|
@ -793,13 +784,13 @@ int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data * const cfg)
|
|||
return -1;
|
||||
}
|
||||
|
||||
if ((USB_DW_EP_ADDR2DIR(cfg->ep_addr) == USB_EP_DIR_OUT) &&
|
||||
if ((USB_EP_DIR_IS_OUT(cfg->ep_addr)) &&
|
||||
(ep_idx >= DW_USB_OUT_EP_NUM)) {
|
||||
LOG_WRN("OUT endpoint address out of range");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((USB_DW_EP_ADDR2DIR(cfg->ep_addr) == USB_EP_DIR_IN) &&
|
||||
if ((USB_EP_DIR_IS_IN(cfg->ep_addr)) &&
|
||||
(ep_idx >= DW_USB_IN_EP_NUM)) {
|
||||
LOG_WRN("IN endpoint address out of range");
|
||||
return -1;
|
||||
|
@ -830,14 +821,14 @@ int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data * const ep_cfg)
|
|||
|
||||
int usb_dc_ep_set_stall(const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USB_DW_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (!usb_dw_ctrl.attached || !usb_dw_ep_is_valid(ep)) {
|
||||
LOG_ERR("Not attached / Invalid endpoint: EP 0x%x", ep);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (USB_DW_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
USB_DW->out_ep_reg[ep_idx].doepctl |= USB_DW_DEPCTL_STALL;
|
||||
} else {
|
||||
USB_DW->in_ep_reg[ep_idx].diepctl |= USB_DW_DEPCTL_STALL;
|
||||
|
@ -848,7 +839,7 @@ int usb_dc_ep_set_stall(const uint8_t ep)
|
|||
|
||||
int usb_dc_ep_clear_stall(const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USB_DW_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (!usb_dw_ctrl.attached || !usb_dw_ep_is_valid(ep)) {
|
||||
LOG_ERR("Not attached / Invalid endpoint: EP 0x%x", ep);
|
||||
|
@ -860,7 +851,7 @@ int usb_dc_ep_clear_stall(const uint8_t ep)
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (USB_DW_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
USB_DW->out_ep_reg[ep_idx].doepctl &= ~USB_DW_DEPCTL_STALL;
|
||||
} else {
|
||||
USB_DW->in_ep_reg[ep_idx].diepctl &= ~USB_DW_DEPCTL_STALL;
|
||||
|
@ -871,7 +862,7 @@ int usb_dc_ep_clear_stall(const uint8_t ep)
|
|||
|
||||
int usb_dc_ep_halt(const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USB_DW_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
volatile uint32_t *p_depctl;
|
||||
|
||||
if (!usb_dw_ctrl.attached || !usb_dw_ep_is_valid(ep)) {
|
||||
|
@ -883,7 +874,7 @@ int usb_dc_ep_halt(const uint8_t ep)
|
|||
/* Cannot disable EP0, just set stall */
|
||||
usb_dc_ep_set_stall(ep);
|
||||
} else {
|
||||
if (USB_DW_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
p_depctl = &USB_DW->out_ep_reg[ep_idx].doepctl;
|
||||
} else {
|
||||
p_depctl = &USB_DW->in_ep_reg[ep_idx].diepctl;
|
||||
|
@ -902,7 +893,7 @@ int usb_dc_ep_halt(const uint8_t ep)
|
|||
|
||||
int usb_dc_ep_is_stalled(const uint8_t ep, uint8_t *const stalled)
|
||||
{
|
||||
uint8_t ep_idx = USB_DW_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (!usb_dw_ctrl.attached || !usb_dw_ep_is_valid(ep)) {
|
||||
LOG_ERR("Not attached / Invalid endpoint: EP 0x%x", ep);
|
||||
|
@ -914,7 +905,7 @@ int usb_dc_ep_is_stalled(const uint8_t ep, uint8_t *const stalled)
|
|||
}
|
||||
|
||||
*stalled = 0U;
|
||||
if (USB_DW_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
if (USB_DW->out_ep_reg[ep_idx].doepctl & USB_DW_DEPCTL_STALL) {
|
||||
*stalled = 1U;
|
||||
}
|
||||
|
@ -929,7 +920,7 @@ int usb_dc_ep_is_stalled(const uint8_t ep, uint8_t *const stalled)
|
|||
|
||||
int usb_dc_ep_enable(const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USB_DW_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (!usb_dw_ctrl.attached || !usb_dw_ep_is_valid(ep)) {
|
||||
LOG_ERR("Not attached / Invalid endpoint: EP 0x%x", ep);
|
||||
|
@ -937,14 +928,14 @@ int usb_dc_ep_enable(const uint8_t ep)
|
|||
}
|
||||
|
||||
/* enable EP interrupts */
|
||||
if (USB_DW_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
USB_DW->daintmsk |= USB_DW_DAINT_OUT_EP_INT(ep_idx);
|
||||
} else {
|
||||
USB_DW->daintmsk |= USB_DW_DAINT_IN_EP_INT(ep_idx);
|
||||
}
|
||||
|
||||
/* Activate Ep */
|
||||
if (USB_DW_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
USB_DW->out_ep_reg[ep_idx].doepctl |= USB_DW_DEPCTL_USB_ACT_EP;
|
||||
usb_dw_ctrl.out_ep_ctrl[ep_idx].ep_ena = 1U;
|
||||
} else {
|
||||
|
@ -952,7 +943,7 @@ int usb_dc_ep_enable(const uint8_t ep)
|
|||
usb_dw_ctrl.in_ep_ctrl[ep_idx].ep_ena = 1U;
|
||||
}
|
||||
|
||||
if (USB_DW_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT &&
|
||||
if (USB_EP_DIR_IS_OUT(ep) &&
|
||||
usb_dw_ctrl.out_ep_ctrl[ep_idx].cb != usb_transfer_ep_callback) {
|
||||
/* Start reading now, except for transfer managed eps */
|
||||
usb_dw_prep_rx(ep, 0);
|
||||
|
@ -963,7 +954,7 @@ int usb_dc_ep_enable(const uint8_t ep)
|
|||
|
||||
int usb_dc_ep_disable(const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USB_DW_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (!usb_dw_ctrl.attached || !usb_dw_ep_is_valid(ep)) {
|
||||
LOG_ERR("Not attached / Invalid endpoint: EP 0x%x", ep);
|
||||
|
@ -971,7 +962,7 @@ int usb_dc_ep_disable(const uint8_t ep)
|
|||
}
|
||||
|
||||
/* Disable EP interrupts */
|
||||
if (USB_DW_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
USB_DW->daintmsk &= ~USB_DW_DAINT_OUT_EP_INT(ep_idx);
|
||||
USB_DW->doepmsk &= ~USB_DW_DOEPINT_SET_UP;
|
||||
} else {
|
||||
|
@ -981,7 +972,7 @@ int usb_dc_ep_disable(const uint8_t ep)
|
|||
}
|
||||
|
||||
/* De-activate, disable and set NAK for Ep */
|
||||
if (USB_DW_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
USB_DW->out_ep_reg[ep_idx].doepctl &=
|
||||
~(USB_DW_DEPCTL_USB_ACT_EP |
|
||||
USB_DW_DEPCTL_EP_ENA |
|
||||
|
@ -1000,7 +991,7 @@ int usb_dc_ep_disable(const uint8_t ep)
|
|||
|
||||
int usb_dc_ep_flush(const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USB_DW_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
uint32_t cnt;
|
||||
|
||||
if (!usb_dw_ctrl.attached || !usb_dw_ep_is_valid(ep)) {
|
||||
|
@ -1008,7 +999,7 @@ int usb_dc_ep_flush(const uint8_t ep)
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (USB_DW_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
/* RX FIFO is global and cannot be flushed per EP */
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -1041,7 +1032,7 @@ int usb_dc_ep_write(const uint8_t ep, const uint8_t *const data,
|
|||
}
|
||||
|
||||
/* Check if IN ep */
|
||||
if (USB_DW_EP_ADDR2DIR(ep) != USB_EP_DIR_IN) {
|
||||
if (USB_EP_GET_DIR(ep) != USB_EP_DIR_IN) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
|
@ -1065,7 +1056,7 @@ int usb_dc_ep_write(const uint8_t ep, const uint8_t *const data,
|
|||
int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
|
||||
uint32_t *read_bytes)
|
||||
{
|
||||
uint8_t ep_idx = USB_DW_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
uint32_t i, j, data_len, bytes_to_copy;
|
||||
|
||||
if (!usb_dw_ctrl.attached || !usb_dw_ep_is_valid(ep)) {
|
||||
|
@ -1074,7 +1065,7 @@ int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
|
|||
}
|
||||
|
||||
/* Check if OUT ep */
|
||||
if (USB_DW_EP_ADDR2DIR(ep) != USB_EP_DIR_OUT) {
|
||||
if (USB_EP_GET_DIR(ep) != USB_EP_DIR_OUT) {
|
||||
LOG_ERR("Wrong endpoint direction");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -1140,7 +1131,7 @@ int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
|
|||
|
||||
int usb_dc_ep_read_continue(uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USB_DW_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (!usb_dw_ctrl.attached || !usb_dw_ep_is_valid(ep)) {
|
||||
LOG_ERR("Not attached / Invalid endpoint: EP 0x%x", ep);
|
||||
|
@ -1148,7 +1139,7 @@ int usb_dc_ep_read_continue(uint8_t ep)
|
|||
}
|
||||
|
||||
/* Check if OUT ep */
|
||||
if (USB_DW_EP_ADDR2DIR(ep) != USB_EP_DIR_OUT) {
|
||||
if (USB_EP_GET_DIR(ep) != USB_EP_DIR_OUT) {
|
||||
LOG_ERR("Wrong endpoint direction");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -1183,14 +1174,14 @@ int usb_dc_ep_read(const uint8_t ep, uint8_t *const data,
|
|||
|
||||
int usb_dc_ep_set_callback(const uint8_t ep, const usb_dc_ep_callback cb)
|
||||
{
|
||||
uint8_t ep_idx = USB_DW_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (!usb_dw_ctrl.attached || !usb_dw_ep_is_valid(ep)) {
|
||||
LOG_ERR("Not attached / Invalid endpoint: EP 0x%x", ep);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (USB_DW_EP_ADDR2DIR(ep) == USB_EP_DIR_IN) {
|
||||
if (USB_EP_DIR_IS_IN(ep)) {
|
||||
usb_dw_ctrl.in_ep_ctrl[ep_idx].cb = cb;
|
||||
} else {
|
||||
usb_dw_ctrl.out_ep_ctrl[ep_idx].cb = cb;
|
||||
|
@ -1206,14 +1197,14 @@ void usb_dc_set_status_callback(const usb_dc_status_callback cb)
|
|||
|
||||
int usb_dc_ep_mps(const uint8_t ep)
|
||||
{
|
||||
enum usb_dw_out_ep_idx ep_idx = USB_DW_EP_ADDR2IDX(ep);
|
||||
enum usb_dw_out_ep_idx ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (!usb_dw_ctrl.attached || !usb_dw_ep_is_valid(ep)) {
|
||||
LOG_ERR("Not attached / Invalid endpoint: EP 0x%x", ep);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (USB_DW_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
return usb_dw_ctrl.out_ep_ctrl[ep_idx].mps;
|
||||
} else {
|
||||
return usb_dw_ctrl.in_ep_ctrl[ep_idx].mps;
|
||||
|
|
|
@ -39,9 +39,6 @@ LOG_MODULE_REGISTER(usb_dc_kinetis);
|
|||
#define KINETIS_EP_NUMOF_MASK 0xf
|
||||
#define KINETIS_ADDR2IDX(addr) ((addr) & (KINETIS_EP_NUMOF_MASK))
|
||||
|
||||
#define EP_ADDR2IDX(ep) ((ep) & ~USB_EP_DIR_MASK)
|
||||
#define EP_ADDR2DIR(ep) ((ep) & USB_EP_DIR_MASK)
|
||||
|
||||
/*
|
||||
* Buffer Descriptor (BD) entry provides endpoint buffer control
|
||||
* information for USBFS controller. Every endpoint direction requires
|
||||
|
@ -276,7 +273,7 @@ int usb_dc_set_address(const uint8_t addr)
|
|||
|
||||
int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data * const cfg)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(cfg->ep_addr);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(cfg->ep_addr);
|
||||
|
||||
if (ep_idx > (NUM_OF_EP_MAX - 1)) {
|
||||
LOG_ERR("endpoint index/address out of range");
|
||||
|
@ -310,12 +307,12 @@ int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data * const cfg)
|
|||
}
|
||||
|
||||
if (ep_idx & BIT(0)) {
|
||||
if (EP_ADDR2DIR(cfg->ep_addr) != USB_EP_DIR_IN) {
|
||||
if (USB_EP_GET_DIR(cfg->ep_addr) != USB_EP_DIR_IN) {
|
||||
LOG_INF("pre-selected as IN endpoint");
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
if (EP_ADDR2DIR(cfg->ep_addr) != USB_EP_DIR_OUT) {
|
||||
if (USB_EP_GET_DIR(cfg->ep_addr) != USB_EP_DIR_OUT) {
|
||||
LOG_INF("pre-selected as OUT endpoint");
|
||||
return -1;
|
||||
}
|
||||
|
@ -326,7 +323,7 @@ int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data * const cfg)
|
|||
|
||||
int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data * const cfg)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(cfg->ep_addr);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(cfg->ep_addr);
|
||||
struct usb_ep_ctrl_data *ep_ctrl;
|
||||
struct k_mem_block *block;
|
||||
uint8_t idx_even;
|
||||
|
@ -349,7 +346,7 @@ int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data * const cfg)
|
|||
LOG_DBG("ep %x, mps %d, type %d", cfg->ep_addr, cfg->ep_mps,
|
||||
cfg->ep_type);
|
||||
|
||||
if (EP_ADDR2DIR(cfg->ep_addr) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(cfg->ep_addr)) {
|
||||
block = &(ep_ctrl->mblock_out);
|
||||
} else {
|
||||
block = &(ep_ctrl->mblock_in);
|
||||
|
@ -397,14 +394,14 @@ int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data * const cfg)
|
|||
case USB_DC_EP_BULK:
|
||||
case USB_DC_EP_INTERRUPT:
|
||||
USB0->ENDPOINT[ep_idx].ENDPT |= USB_ENDPT_EPHSHK_MASK;
|
||||
if (EP_ADDR2DIR(cfg->ep_addr) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(cfg->ep_addr)) {
|
||||
USB0->ENDPOINT[ep_idx].ENDPT |= USB_ENDPT_EPRXEN_MASK;
|
||||
} else {
|
||||
USB0->ENDPOINT[ep_idx].ENDPT |= USB_ENDPT_EPTXEN_MASK;
|
||||
}
|
||||
break;
|
||||
case USB_DC_EP_ISOCHRONOUS:
|
||||
if (EP_ADDR2DIR(cfg->ep_addr) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(cfg->ep_addr)) {
|
||||
USB0->ENDPOINT[ep_idx].ENDPT |= USB_ENDPT_EPRXEN_MASK;
|
||||
} else {
|
||||
USB0->ENDPOINT[ep_idx].ENDPT |= USB_ENDPT_EPTXEN_MASK;
|
||||
|
@ -419,7 +416,7 @@ int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data * const cfg)
|
|||
|
||||
int usb_dc_ep_set_stall(const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
uint8_t bd_idx;
|
||||
|
||||
if (ep_idx > (NUM_OF_EP_MAX - 1)) {
|
||||
|
@ -429,7 +426,7 @@ int usb_dc_ep_set_stall(const uint8_t ep)
|
|||
|
||||
LOG_DBG("ep %x, idx %d", ep, ep_idx);
|
||||
|
||||
if (EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
dev_data.ep_ctrl[ep_idx].status.out_stalled = 1U;
|
||||
bd_idx = get_bdt_idx(ep,
|
||||
~dev_data.ep_ctrl[ep_idx].status.out_odd);
|
||||
|
@ -446,7 +443,7 @@ int usb_dc_ep_set_stall(const uint8_t ep)
|
|||
|
||||
int usb_dc_ep_clear_stall(const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
uint8_t bd_idx;
|
||||
|
||||
if (ep_idx > (NUM_OF_EP_MAX - 1)) {
|
||||
|
@ -457,7 +454,7 @@ int usb_dc_ep_clear_stall(const uint8_t ep)
|
|||
LOG_DBG("ep %x, idx %d", ep, ep_idx);
|
||||
USB0->ENDPOINT[ep_idx].ENDPT &= ~USB_ENDPT_EPSTALL_MASK;
|
||||
|
||||
if (EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
dev_data.ep_ctrl[ep_idx].status.out_stalled = 0U;
|
||||
dev_data.ep_ctrl[ep_idx].status.out_data1 = false;
|
||||
bd_idx = get_bdt_idx(ep,
|
||||
|
@ -482,7 +479,7 @@ int usb_dc_ep_clear_stall(const uint8_t ep)
|
|||
|
||||
int usb_dc_ep_is_stalled(const uint8_t ep, uint8_t *const stalled)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (ep_idx > (NUM_OF_EP_MAX - 1)) {
|
||||
LOG_ERR("Wrong endpoint index/address");
|
||||
|
@ -495,7 +492,7 @@ int usb_dc_ep_is_stalled(const uint8_t ep, uint8_t *const stalled)
|
|||
}
|
||||
|
||||
*stalled = 0U;
|
||||
if (EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
*stalled = dev_data.ep_ctrl[ep_idx].status.out_stalled;
|
||||
} else {
|
||||
*stalled = dev_data.ep_ctrl[ep_idx].status.in_stalled;
|
||||
|
@ -518,7 +515,7 @@ int usb_dc_ep_halt(const uint8_t ep)
|
|||
|
||||
int usb_dc_ep_enable(const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
uint8_t idx_even;
|
||||
uint8_t idx_odd;
|
||||
|
||||
|
@ -536,7 +533,7 @@ int usb_dc_ep_enable(const uint8_t ep)
|
|||
return -EALREADY;
|
||||
}
|
||||
|
||||
if (EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
bdt[idx_even].set.bd_ctrl = BD_DTS_MASK | BD_OWN_MASK;
|
||||
bdt[idx_odd].set.bd_ctrl = 0U;
|
||||
dev_data.ep_ctrl[ep_idx].status.out_odd = 0U;
|
||||
|
@ -559,7 +556,7 @@ int usb_dc_ep_enable(const uint8_t ep)
|
|||
|
||||
int usb_dc_ep_disable(const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
uint8_t idx_even;
|
||||
uint8_t idx_odd;
|
||||
|
||||
|
@ -575,7 +572,7 @@ int usb_dc_ep_disable(const uint8_t ep)
|
|||
|
||||
bdt[idx_even].bd_fields = 0U;
|
||||
bdt[idx_odd].bd_fields = 0U;
|
||||
if (EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
dev_data.ep_ctrl[ep_idx].status.out_enabled = false;
|
||||
} else {
|
||||
dev_data.ep_ctrl[ep_idx].status.in_enabled = false;
|
||||
|
@ -586,7 +583,7 @@ int usb_dc_ep_disable(const uint8_t ep)
|
|||
|
||||
int usb_dc_ep_flush(const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (ep_idx > (NUM_OF_EP_MAX - 1)) {
|
||||
LOG_ERR("Wrong endpoint index/address");
|
||||
|
@ -601,7 +598,7 @@ int usb_dc_ep_flush(const uint8_t ep)
|
|||
int usb_dc_ep_write(const uint8_t ep, const uint8_t *const data,
|
||||
const uint32_t data_len, uint32_t * const ret_bytes)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
uint32_t len_to_send = data_len;
|
||||
uint8_t odd;
|
||||
uint8_t bd_idx;
|
||||
|
@ -616,7 +613,7 @@ int usb_dc_ep_write(const uint8_t ep, const uint8_t *const data,
|
|||
bd_idx = get_bdt_idx(ep, odd);
|
||||
bufp = (uint8_t *)bdt[bd_idx].buf_addr;
|
||||
|
||||
if (EP_ADDR2DIR(ep) != USB_EP_DIR_IN) {
|
||||
if (USB_EP_GET_DIR(ep) != USB_EP_DIR_IN) {
|
||||
LOG_ERR("Wrong endpoint direction");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -667,7 +664,7 @@ int usb_dc_ep_write(const uint8_t ep, const uint8_t *const data,
|
|||
int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
|
||||
uint32_t *read_bytes)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
uint32_t data_len;
|
||||
uint8_t bd_idx;
|
||||
uint8_t *bufp;
|
||||
|
@ -681,7 +678,7 @@ int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
|
|||
bd_idx = get_bdt_idx(ep, dev_data.ep_ctrl[ep_idx].status.out_odd);
|
||||
bufp = (uint8_t *)bdt[bd_idx].buf_addr;
|
||||
|
||||
if (EP_ADDR2DIR(ep) != USB_EP_DIR_OUT) {
|
||||
if (USB_EP_GET_DIR(ep) != USB_EP_DIR_OUT) {
|
||||
LOG_ERR("Wrong endpoint direction");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -739,7 +736,7 @@ int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
|
|||
|
||||
int usb_dc_ep_read_continue(uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
uint8_t bd_idx;
|
||||
|
||||
if (ep_idx > (NUM_OF_EP_MAX - 1)) {
|
||||
|
@ -749,7 +746,7 @@ int usb_dc_ep_read_continue(uint8_t ep)
|
|||
|
||||
bd_idx = get_bdt_idx(ep, dev_data.ep_ctrl[ep_idx].status.out_odd);
|
||||
|
||||
if (EP_ADDR2DIR(ep) != USB_EP_DIR_OUT) {
|
||||
if (USB_EP_GET_DIR(ep) != USB_EP_DIR_OUT) {
|
||||
LOG_ERR("Wrong endpoint direction");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -812,7 +809,7 @@ int usb_dc_ep_read(const uint8_t ep, uint8_t *const data,
|
|||
|
||||
int usb_dc_ep_set_callback(const uint8_t ep, const usb_dc_ep_callback cb)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (ep_idx > (NUM_OF_EP_MAX - 1)) {
|
||||
LOG_ERR("Wrong endpoint index/address");
|
||||
|
@ -842,7 +839,7 @@ void usb_dc_set_status_callback(const usb_dc_status_callback cb)
|
|||
|
||||
int usb_dc_ep_mps(const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (ep_idx > (NUM_OF_EP_MAX - 1)) {
|
||||
LOG_ERR("Wrong endpoint index/address");
|
||||
|
@ -997,7 +994,7 @@ static void usb_kinetis_thread_main(void *arg1, void *unused1, void *unused2)
|
|||
|
||||
while (true) {
|
||||
k_msgq_get(&usb_dc_msgq, &msg, K_FOREVER);
|
||||
ep_idx = EP_ADDR2IDX(msg.ep);
|
||||
ep_idx = USB_EP_GET_IDX(msg.ep);
|
||||
|
||||
if (msg.type == USB_DC_CB_TYPE_EP) {
|
||||
switch (msg.cb) {
|
||||
|
|
|
@ -35,10 +35,8 @@ extern void USB_DeviceEhciIsrFunction(void *deviceHandle);
|
|||
#define SETUP_DATA_STAGE_OUT (2)
|
||||
|
||||
/* Then endpoint number/index calculation */
|
||||
#define EP_ADDR2IDX(ep) ((ep) & ~USB_EP_DIR_MASK)
|
||||
#define EP_ADDR2DIR(ep) ((ep) & USB_EP_DIR_MASK)
|
||||
#define EP_ABS_IDX(ep) (((ep) & ~USB_EP_DIR_MASK) * 2 + \
|
||||
(((ep) & USB_EP_DIR_MASK) >> 7))
|
||||
#define EP_ABS_IDX(ep) (USB_EP_GET_IDX(ep) * 2 + \
|
||||
(USB_EP_GET_DIR(ep) >> 7))
|
||||
#define NUM_OF_EP_MAX DT_INST_PROP(0, num_bidir_endpoints)
|
||||
|
||||
/* The minimum value is 1 */
|
||||
|
@ -145,7 +143,7 @@ int usb_dc_set_address(const uint8_t addr)
|
|||
|
||||
int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data *const cfg)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(cfg->ep_addr);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(cfg->ep_addr);
|
||||
|
||||
if ((cfg->ep_type == USB_DC_EP_CONTROL) && ep_idx) {
|
||||
LOG_ERR("invalid endpoint configuration");
|
||||
|
@ -158,12 +156,12 @@ int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data *const cfg)
|
|||
}
|
||||
|
||||
if (ep_idx & BIT(0)) {
|
||||
if (EP_ADDR2DIR(cfg->ep_addr) != USB_EP_DIR_IN) {
|
||||
if (USB_EP_GET_DIR(cfg->ep_addr) != USB_EP_DIR_IN) {
|
||||
LOG_INF("pre-selected as IN endpoint");
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
if (EP_ADDR2DIR(cfg->ep_addr) != USB_EP_DIR_OUT) {
|
||||
if (USB_EP_GET_DIR(cfg->ep_addr) != USB_EP_DIR_OUT) {
|
||||
LOG_INF("pre-selected as OUT endpoint");
|
||||
return -1;
|
||||
}
|
||||
|
@ -220,8 +218,8 @@ int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data *const cfg)
|
|||
* If it is control endpoint, controller will prime setup
|
||||
* here set the occupied flag.
|
||||
*/
|
||||
if ((EP_ADDR2IDX(cfg->ep_addr) == USB_CONTROL_ENDPOINT) &&
|
||||
(EP_ADDR2DIR(cfg->ep_addr) == USB_EP_DIR_OUT)) {
|
||||
if ((USB_EP_GET_IDX(cfg->ep_addr) == USB_CONTROL_ENDPOINT) &&
|
||||
(USB_EP_DIR_IS_OUT(cfg->ep_addr))) {
|
||||
dev_data.eps[ep_abs_idx].ep_occupied = true;
|
||||
}
|
||||
dev_data.eps[ep_abs_idx].ep_enabled = true;
|
||||
|
@ -268,8 +266,8 @@ int usb_dc_ep_clear_stall(const uint8_t ep)
|
|||
return -EIO;
|
||||
}
|
||||
|
||||
if ((EP_ADDR2IDX(ep) != USB_CONTROL_ENDPOINT) &&
|
||||
(EP_ADDR2DIR(ep) == USB_EP_DIR_OUT)) {
|
||||
if ((USB_EP_GET_IDX(ep) != USB_CONTROL_ENDPOINT) &&
|
||||
(USB_EP_DIR_IS_OUT(ep))) {
|
||||
status = dev_data.interface->deviceRecv(
|
||||
dev_data.controllerHandle, ep,
|
||||
(uint8_t *)dev_data.eps[ep_abs_idx].block.data,
|
||||
|
@ -343,8 +341,8 @@ int usb_dc_ep_enable(const uint8_t ep)
|
|||
return -EALREADY;
|
||||
}
|
||||
|
||||
if ((EP_ADDR2IDX(ep) != USB_CONTROL_ENDPOINT) &&
|
||||
(EP_ADDR2DIR(ep) == USB_EP_DIR_OUT)) {
|
||||
if ((USB_EP_GET_IDX(ep) != USB_CONTROL_ENDPOINT) &&
|
||||
(USB_EP_DIR_IS_OUT(ep))) {
|
||||
status = dev_data.interface->deviceRecv(
|
||||
dev_data.controllerHandle, ep,
|
||||
(uint8_t *)dev_data.eps[ep_abs_idx].block.data,
|
||||
|
@ -390,7 +388,7 @@ int usb_dc_ep_disable(const uint8_t ep)
|
|||
|
||||
int usb_dc_ep_flush(const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (ep_idx >= NUM_OF_EP_MAX) {
|
||||
LOG_ERR("Wrong endpoint index/address");
|
||||
|
@ -471,7 +469,7 @@ static void update_control_stage(usb_device_callback_message_struct_t *cb_msg,
|
|||
int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
|
||||
uint32_t *read_bytes)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
uint8_t ep_abs_idx = EP_ABS_IDX(ep);
|
||||
uint32_t data_len;
|
||||
uint8_t *bufp = NULL;
|
||||
|
@ -481,7 +479,7 @@ int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
|
|||
return -EBUSY;
|
||||
}
|
||||
|
||||
if ((ep_idx >= NUM_OF_EP_MAX) || (EP_ADDR2DIR(ep) != USB_EP_DIR_OUT)) {
|
||||
if ((ep_idx >= NUM_OF_EP_MAX) || (USB_EP_GET_DIR(ep) != USB_EP_DIR_OUT)) {
|
||||
LOG_ERR("Wrong endpoint index/address/direction");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -530,7 +528,7 @@ int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
|
|||
*read_bytes = data_len;
|
||||
}
|
||||
|
||||
if (EP_ADDR2IDX(ep) == USB_ENDPOINT_CONTROL) {
|
||||
if (USB_EP_GET_IDX(ep) == USB_ENDPOINT_CONTROL) {
|
||||
update_control_stage(&dev_data.eps[0].transfer_message,
|
||||
data_len, max_data_len);
|
||||
}
|
||||
|
@ -553,7 +551,7 @@ int usb_dc_ep_read_continue(uint8_t ep)
|
|||
return -EBUSY;
|
||||
}
|
||||
|
||||
if (EP_ADDR2IDX(ep) == USB_ENDPOINT_CONTROL) {
|
||||
if (USB_EP_GET_IDX(ep) == USB_ENDPOINT_CONTROL) {
|
||||
if (dev_data.setupDataStage == SETUP_DATA_STAGE_DONE) {
|
||||
return 0;
|
||||
}
|
||||
|
@ -699,7 +697,7 @@ static void handle_transfer_msg(usb_device_callback_message_struct_t *cb_msg)
|
|||
ep_status_code = USB_DC_EP_SETUP;
|
||||
} else {
|
||||
/* IN TOKEN */
|
||||
if ((ep & USB_EP_DIR_MASK) == USB_EP_DIR_IN) {
|
||||
if (USB_EP_DIR_IS_IN(ep)) {
|
||||
if ((dev_data.address != 0) && (ep_abs_idx == 1)) {
|
||||
/*
|
||||
* Set Address in the status stage in
|
||||
|
|
|
@ -22,13 +22,6 @@
|
|||
#include <logging/log.h>
|
||||
LOG_MODULE_REGISTER(native_posix);
|
||||
|
||||
/* convert from endpoint address to hardware endpoint index */
|
||||
#define USBIP_EP_ADDR2IDX(ep) ((ep) & ~USB_EP_DIR_MASK)
|
||||
/* get direction from endpoint address */
|
||||
#define USBIP_EP_ADDR2DIR(ep) ((ep) & USB_EP_DIR_MASK)
|
||||
/* convert from hardware endpoint index and direction to endpoint address */
|
||||
#define USBIP_EP_IDX2ADDR(idx, dir) ((idx) | ((dir) & USB_EP_DIR_MASK))
|
||||
|
||||
#define USBIP_IN_EP_NUM 8
|
||||
#define USBIP_OUT_EP_NUM 8
|
||||
|
||||
|
@ -73,13 +66,13 @@ static struct usbip_ctrl_prv {
|
|||
|
||||
static uint8_t usbip_ep_is_valid(uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USBIP_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
/* Check if ep is valid */
|
||||
if ((USBIP_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) &&
|
||||
if ((USB_EP_DIR_IS_OUT(ep)) &&
|
||||
ep_idx < USBIP_OUT_EP_NUM) {
|
||||
return 1;
|
||||
} else if ((USBIP_EP_ADDR2DIR(ep) == USB_EP_DIR_IN) &&
|
||||
} else if ((USB_EP_DIR_IS_IN(ep)) &&
|
||||
ep_idx < USBIP_IN_EP_NUM) {
|
||||
return 1;
|
||||
}
|
||||
|
@ -89,15 +82,15 @@ static uint8_t usbip_ep_is_valid(uint8_t ep)
|
|||
|
||||
static uint8_t usbip_ep_is_enabled(uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USBIP_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
LOG_DBG("ep %x", ep);
|
||||
|
||||
/* Check if ep enabled */
|
||||
if ((USBIP_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) &&
|
||||
if ((USB_EP_DIR_IS_OUT(ep)) &&
|
||||
usbip_ctrl.out_ep_ctrl[ep_idx].ep_ena) {
|
||||
return 1;
|
||||
} else if ((USBIP_EP_ADDR2DIR(ep) == USB_EP_DIR_IN) &&
|
||||
} else if ((USB_EP_DIR_IS_IN(ep)) &&
|
||||
usbip_ctrl.in_ep_ctrl[ep_idx].ep_ena) {
|
||||
return 1;
|
||||
}
|
||||
|
@ -156,7 +149,7 @@ int usb_dc_set_address(const uint8_t addr)
|
|||
|
||||
int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data * const cfg)
|
||||
{
|
||||
uint8_t ep_idx = USBIP_EP_ADDR2IDX(cfg->ep_addr);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(cfg->ep_addr);
|
||||
|
||||
LOG_DBG("ep %x, mps %d, type %d", cfg->ep_addr, cfg->ep_mps,
|
||||
cfg->ep_type);
|
||||
|
@ -171,13 +164,13 @@ int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data * const cfg)
|
|||
return -1;
|
||||
}
|
||||
|
||||
if ((USBIP_EP_ADDR2DIR(cfg->ep_addr) == USB_EP_DIR_OUT) &&
|
||||
if ((USB_EP_DIR_IS_OUT(cfg->ep_addr)) &&
|
||||
(ep_idx >= USBIP_OUT_EP_NUM)) {
|
||||
LOG_WRN("OUT endpoint address out of range");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((USBIP_EP_ADDR2DIR(cfg->ep_addr) == USB_EP_DIR_IN) &&
|
||||
if ((USB_EP_DIR_IS_IN(cfg->ep_addr)) &&
|
||||
(ep_idx >= USBIP_IN_EP_NUM)) {
|
||||
LOG_WRN("IN endpoint address out of range");
|
||||
return -1;
|
||||
|
@ -190,7 +183,7 @@ int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data * const cfg)
|
|||
{
|
||||
uint16_t ep_mps = cfg->ep_mps;
|
||||
uint8_t ep = cfg->ep_addr;
|
||||
uint8_t ep_idx = USBIP_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (usb_dc_ep_check_cap(cfg)) {
|
||||
return -EINVAL;
|
||||
|
@ -201,7 +194,7 @@ int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data * const cfg)
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (USBIP_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
usbip_ctrl.out_ep_ctrl[ep_idx].mps = ep_mps;
|
||||
} else {
|
||||
usbip_ctrl.in_ep_ctrl[ep_idx].mps = ep_mps;
|
||||
|
@ -227,7 +220,7 @@ int usb_dc_ep_set_stall(const uint8_t ep)
|
|||
|
||||
int usb_dc_ep_clear_stall(const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USBIP_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
LOG_DBG("ep %x", ep);
|
||||
|
||||
|
@ -246,7 +239,7 @@ int usb_dc_ep_clear_stall(const uint8_t ep)
|
|||
|
||||
int usb_dc_ep_halt(const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USBIP_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
LOG_DBG("ep %x", ep);
|
||||
|
||||
|
@ -281,7 +274,7 @@ int usb_dc_ep_is_stalled(const uint8_t ep, uint8_t *const stalled)
|
|||
|
||||
int usb_dc_ep_enable(const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USBIP_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
LOG_DBG("ep %x", ep);
|
||||
|
||||
|
@ -291,7 +284,7 @@ int usb_dc_ep_enable(const uint8_t ep)
|
|||
}
|
||||
|
||||
/* Enable Ep */
|
||||
if (USBIP_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
usbip_ctrl.out_ep_ctrl[ep_idx].ep_ena = 1U;
|
||||
} else {
|
||||
usbip_ctrl.in_ep_ctrl[ep_idx].ep_ena = 1U;
|
||||
|
@ -321,7 +314,7 @@ int usb_dc_ep_flush(const uint8_t ep)
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (USBIP_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
/* RX FIFO is global and cannot be flushed per EP */
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -340,7 +333,7 @@ int usb_dc_ep_write(const uint8_t ep, const uint8_t *const data,
|
|||
}
|
||||
|
||||
/* Check if IN ep */
|
||||
if (USBIP_EP_ADDR2DIR(ep) != USB_EP_DIR_IN) {
|
||||
if (USB_EP_GET_DIR(ep) != USB_EP_DIR_IN) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
|
@ -350,7 +343,7 @@ int usb_dc_ep_write(const uint8_t ep, const uint8_t *const data,
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (USBIP_EP_ADDR2IDX(ep) == 0) {
|
||||
if (USB_EP_GET_IDX(ep) == 0) {
|
||||
if (!usbip_send_common(ep, data_len)) {
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -359,7 +352,7 @@ int usb_dc_ep_write(const uint8_t ep, const uint8_t *const data,
|
|||
return -EIO;
|
||||
}
|
||||
} else {
|
||||
uint8_t ep_idx = USBIP_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
struct usb_ep_ctrl_prv *ctrl = &usbip_ctrl.in_ep_ctrl[ep_idx];
|
||||
|
||||
memcpy(ctrl->buf, data, data_len);
|
||||
|
@ -384,7 +377,7 @@ int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
|
|||
}
|
||||
|
||||
/* Check if OUT ep */
|
||||
if (USBIP_EP_ADDR2DIR(ep) != USB_EP_DIR_OUT) {
|
||||
if (USB_EP_GET_DIR(ep) != USB_EP_DIR_OUT) {
|
||||
LOG_ERR("Wrong endpoint direction");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -406,7 +399,7 @@ int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
|
|||
* the available data in buffer
|
||||
*/
|
||||
if (read_bytes) {
|
||||
uint8_t ep_idx = USBIP_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
*read_bytes = usbip_ctrl.out_ep_ctrl[ep_idx].data_len;
|
||||
}
|
||||
|
@ -427,7 +420,7 @@ int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
|
|||
|
||||
int usb_dc_ep_read_continue(uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USBIP_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (!usbip_ctrl.attached || !usbip_ep_is_valid(ep)) {
|
||||
LOG_ERR("Not attached / Invalid endpoint: EP 0x%x", ep);
|
||||
|
@ -435,7 +428,7 @@ int usb_dc_ep_read_continue(uint8_t ep)
|
|||
}
|
||||
|
||||
/* Check if OUT ep */
|
||||
if (USBIP_EP_ADDR2DIR(ep) != USB_EP_DIR_OUT) {
|
||||
if (USB_EP_GET_DIR(ep) != USB_EP_DIR_OUT) {
|
||||
LOG_ERR("Wrong endpoint direction");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -473,7 +466,7 @@ int usb_dc_ep_read(const uint8_t ep, uint8_t *const data,
|
|||
|
||||
int usb_dc_ep_set_callback(const uint8_t ep, const usb_dc_ep_callback cb)
|
||||
{
|
||||
uint8_t ep_idx = USBIP_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
LOG_DBG("ep %x callback %p", ep, cb);
|
||||
|
||||
|
@ -482,7 +475,7 @@ int usb_dc_ep_set_callback(const uint8_t ep, const usb_dc_ep_callback cb)
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (USBIP_EP_ADDR2DIR(ep) == USB_EP_DIR_IN) {
|
||||
if (USB_EP_DIR_IS_IN(ep)) {
|
||||
usbip_ctrl.in_ep_ctrl[ep_idx].cb = cb;
|
||||
} else {
|
||||
usbip_ctrl.out_ep_ctrl[ep_idx].cb = cb;
|
||||
|
@ -498,7 +491,7 @@ void usb_dc_set_status_callback(const usb_dc_status_callback cb)
|
|||
|
||||
int usb_dc_ep_mps(const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USBIP_EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
LOG_DBG("ep %x", ep);
|
||||
|
||||
|
@ -507,7 +500,7 @@ int usb_dc_ep_mps(const uint8_t ep)
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (USBIP_EP_ADDR2DIR(ep) == USB_EP_DIR_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
return usbip_ctrl.out_ep_ctrl[ep_idx].mps;
|
||||
} else {
|
||||
return usbip_ctrl.in_ep_ctrl[ep_idx].mps;
|
||||
|
@ -516,7 +509,7 @@ int usb_dc_ep_mps(const uint8_t ep)
|
|||
|
||||
int handle_usb_control(struct usbip_header *hdr)
|
||||
{
|
||||
uint8_t ep_idx = USBIP_EP_ADDR2IDX(ntohl(hdr->common.ep));
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ntohl(hdr->common.ep));
|
||||
usb_dc_ep_callback ep_cb = usbip_ctrl.out_ep_ctrl[ep_idx].cb;
|
||||
|
||||
LOG_DBG("ep %x idx %u", ntohl(hdr->common.ep), ep_idx);
|
||||
|
|
|
@ -312,7 +312,7 @@ static inline uint8_t nrfx_addr_to_ep(nrfx_usbd_ep_t ep)
|
|||
|
||||
static inline bool ep_is_valid(const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_num = ep & ~USB_EP_DIR_MASK;
|
||||
uint8_t ep_num = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (NRF_USBD_EPIN_CHECK(ep)) {
|
||||
if (unlikely(ep_num == NRF_USBD_EPISO_FIRST)) {
|
||||
|
|
|
@ -45,10 +45,6 @@ LOG_MODULE_REGISTER(usb_dc_sam);
|
|||
#define USBHS_DEVEPTIDR_CTRL_STALLRQC USBHS_DEVEPTIDR_STALLRQC
|
||||
#endif
|
||||
|
||||
/* Helper macros to make it easier to work with endpoint numbers */
|
||||
#define EP_ADDR2IDX(ep) ((ep) & ~USB_EP_DIR_MASK)
|
||||
#define EP_ADDR2DIR(ep) ((ep) & USB_EP_DIR_MASK)
|
||||
|
||||
#define NUM_OF_EP_MAX DT_INST_PROP(0, num_bidir_endpoints)
|
||||
#if DT_INST_NODE_HAS_PROP(0, maximum_speed)
|
||||
#define USB_MAXIMUM_SPEED DT_INST_PROP(0, maximum_speed)
|
||||
|
@ -419,7 +415,7 @@ void usb_dc_set_status_callback(const usb_dc_status_callback cb)
|
|||
/* Check endpoint capabilities */
|
||||
int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data * const cfg)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(cfg->ep_addr);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(cfg->ep_addr);
|
||||
|
||||
if (ep_idx >= NUM_OF_EP_MAX) {
|
||||
LOG_ERR("endpoint index/address out of range");
|
||||
|
@ -432,12 +428,12 @@ int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data * const cfg)
|
|||
return -1;
|
||||
}
|
||||
} else if (ep_idx & BIT(0)) {
|
||||
if (EP_ADDR2DIR(cfg->ep_addr) != USB_EP_DIR_IN) {
|
||||
if (USB_EP_GET_DIR(cfg->ep_addr) != USB_EP_DIR_IN) {
|
||||
LOG_INF("pre-selected as IN endpoint");
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
if (EP_ADDR2DIR(cfg->ep_addr) != USB_EP_DIR_OUT) {
|
||||
if (USB_EP_GET_DIR(cfg->ep_addr) != USB_EP_DIR_OUT) {
|
||||
LOG_INF("pre-selected as OUT endpoint");
|
||||
return -1;
|
||||
}
|
||||
|
@ -455,7 +451,7 @@ int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data * const cfg)
|
|||
/* Configure endpoint */
|
||||
int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data *const cfg)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(cfg->ep_addr);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(cfg->ep_addr);
|
||||
bool ep_configured[NUM_OF_EP_MAX];
|
||||
bool ep_enabled[NUM_OF_EP_MAX];
|
||||
uint32_t regval = 0U;
|
||||
|
@ -500,7 +496,7 @@ int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data *const cfg)
|
|||
}
|
||||
|
||||
/* Map the endpoint direction */
|
||||
if (EP_ADDR2DIR(cfg->ep_addr) == USB_EP_DIR_OUT ||
|
||||
if (USB_EP_DIR_IS_OUT(cfg->ep_addr) ||
|
||||
cfg->ep_type == USB_DC_EP_CONTROL) {
|
||||
regval |= USBHS_DEVEPTCFG_EPDIR_OUT;
|
||||
} else {
|
||||
|
@ -567,7 +563,7 @@ int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data *const cfg)
|
|||
/* Set stall condition for the selected endpoint */
|
||||
int usb_dc_ep_set_stall(uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (ep_idx >= NUM_OF_EP_MAX) {
|
||||
LOG_ERR("wrong endpoint index/address");
|
||||
|
@ -583,7 +579,7 @@ int usb_dc_ep_set_stall(uint8_t ep)
|
|||
/* Clear stall condition for the selected endpoint */
|
||||
int usb_dc_ep_clear_stall(uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (ep_idx >= NUM_OF_EP_MAX) {
|
||||
LOG_ERR("wrong endpoint index/address");
|
||||
|
@ -599,7 +595,7 @@ int usb_dc_ep_clear_stall(uint8_t ep)
|
|||
/* Check if the selected endpoint is stalled */
|
||||
int usb_dc_ep_is_stalled(uint8_t ep, uint8_t *stalled)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (ep_idx >= NUM_OF_EP_MAX) {
|
||||
LOG_ERR("wrong endpoint index/address");
|
||||
|
@ -626,7 +622,7 @@ int usb_dc_ep_halt(uint8_t ep)
|
|||
/* Enable the selected endpoint */
|
||||
int usb_dc_ep_enable(uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (ep_idx >= NUM_OF_EP_MAX) {
|
||||
LOG_ERR("wrong endpoint index/address");
|
||||
|
@ -655,7 +651,7 @@ int usb_dc_ep_enable(uint8_t ep)
|
|||
/* Disable the selected endpoint */
|
||||
int usb_dc_ep_disable(uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (ep_idx >= NUM_OF_EP_MAX) {
|
||||
LOG_ERR("wrong endpoint index/address");
|
||||
|
@ -676,7 +672,7 @@ int usb_dc_ep_disable(uint8_t ep)
|
|||
/* Flush the selected endpoint */
|
||||
int usb_dc_ep_flush(uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (ep_idx >= NUM_OF_EP_MAX) {
|
||||
LOG_ERR("wrong endpoint index/address");
|
||||
|
@ -714,7 +710,7 @@ int usb_dc_ep_flush(uint8_t ep)
|
|||
/* Write data to the specified endpoint */
|
||||
int usb_dc_ep_write(uint8_t ep, const uint8_t *data, uint32_t data_len, uint32_t *ret_bytes)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
uint32_t packet_len;
|
||||
|
||||
if (ep_idx >= NUM_OF_EP_MAX) {
|
||||
|
@ -727,7 +723,7 @@ int usb_dc_ep_write(uint8_t ep, const uint8_t *data, uint32_t data_len, uint32_t
|
|||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (EP_ADDR2DIR(ep) != USB_EP_DIR_IN) {
|
||||
if (USB_EP_GET_DIR(ep) != USB_EP_DIR_IN) {
|
||||
LOG_ERR("wrong endpoint direction");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -772,7 +768,7 @@ int usb_dc_ep_write(uint8_t ep, const uint8_t *data, uint32_t data_len, uint32_t
|
|||
/* Read data from the specified endpoint */
|
||||
int usb_dc_ep_read(uint8_t ep, uint8_t *data, uint32_t max_data_len, uint32_t *read_bytes)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
int rc;
|
||||
|
||||
rc = usb_dc_ep_read_wait(ep, data, max_data_len, read_bytes);
|
||||
|
@ -800,14 +796,14 @@ int usb_dc_ep_read(uint8_t ep, uint8_t *data, uint32_t max_data_len, uint32_t *r
|
|||
/* Set callback function for the specified endpoint */
|
||||
int usb_dc_ep_set_callback(uint8_t ep, const usb_dc_ep_callback cb)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (ep_idx >= NUM_OF_EP_MAX) {
|
||||
LOG_ERR("wrong endpoint index/address");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (EP_ADDR2DIR(ep) == USB_EP_DIR_IN) {
|
||||
if (USB_EP_DIR_IS_IN(ep)) {
|
||||
dev_data.ep_data[ep_idx].cb_in = cb;
|
||||
} else {
|
||||
dev_data.ep_data[ep_idx].cb_out = cb;
|
||||
|
@ -821,7 +817,7 @@ int usb_dc_ep_set_callback(uint8_t ep, const usb_dc_ep_callback cb)
|
|||
int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
|
||||
uint32_t *read_bytes)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
uint32_t data_len = (USBHS->USBHS_DEVEPTISR[ep_idx] &
|
||||
USBHS_DEVEPTISR_BYCT_Msk) >> USBHS_DEVEPTISR_BYCT_Pos;
|
||||
|
||||
|
@ -835,7 +831,7 @@ int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
|
|||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (EP_ADDR2DIR(ep) != USB_EP_DIR_OUT) {
|
||||
if (USB_EP_GET_DIR(ep) != USB_EP_DIR_OUT) {
|
||||
LOG_ERR("wrong endpoint direction");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -879,7 +875,7 @@ int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
|
|||
/* Continue reading data from the endpoint */
|
||||
int usb_dc_ep_read_continue(uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (ep_idx >= NUM_OF_EP_MAX) {
|
||||
LOG_ERR("wrong endpoint index/address");
|
||||
|
@ -891,7 +887,7 @@ int usb_dc_ep_read_continue(uint8_t ep)
|
|||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (EP_ADDR2DIR(ep) != USB_EP_DIR_OUT) {
|
||||
if (USB_EP_GET_DIR(ep) != USB_EP_DIR_OUT) {
|
||||
LOG_ERR("wrong endpoint direction");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -919,7 +915,7 @@ int usb_dc_ep_read_continue(uint8_t ep)
|
|||
/* Endpoint max packet size (mps) */
|
||||
int usb_dc_ep_mps(uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = EP_ADDR2IDX(ep);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (ep_idx >= NUM_OF_EP_MAX) {
|
||||
LOG_ERR("wrong endpoint index/address");
|
||||
|
|
|
@ -324,7 +324,7 @@ void usb_dc_set_status_callback(const usb_dc_status_callback cb)
|
|||
|
||||
int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data * const cfg)
|
||||
{
|
||||
uint8_t ep_idx = cfg->ep_addr & ~USB_EP_DIR_MASK;
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(cfg->ep_addr);
|
||||
|
||||
if ((cfg->ep_type == USB_DC_EP_CONTROL) && ep_idx) {
|
||||
LOG_ERR("invalid endpoint configuration");
|
||||
|
@ -343,7 +343,7 @@ int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data *const cfg)
|
|||
{
|
||||
struct usb_sam0_data *data = usb_sam0_get_data();
|
||||
UsbDevice *regs = ®S->DEVICE;
|
||||
uint8_t ep = cfg->ep_addr & ~USB_EP_DIR_MASK;
|
||||
uint8_t ep = USB_EP_GET_IDX(cfg->ep_addr);
|
||||
UsbDeviceEndpoint *endpoint = ®s->DeviceEndpoint[ep];
|
||||
UsbDeviceDescriptor *desc = &data->descriptors[ep];
|
||||
UsbDeviceDescBank *bank;
|
||||
|
@ -382,7 +382,7 @@ int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data *const cfg)
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (cfg->ep_addr & USB_EP_DIR_MASK) {
|
||||
if (USB_EP_DIR_IS_IN(cfg->ep_addr)) {
|
||||
bank = &desc->DeviceDescBank[1];
|
||||
} else {
|
||||
bank = &desc->DeviceDescBank[0];
|
||||
|
@ -402,7 +402,7 @@ int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data *const cfg)
|
|||
bank->ADDR.reg = (uintptr_t)buf;
|
||||
}
|
||||
|
||||
if (cfg->ep_addr & USB_EP_DIR_MASK) {
|
||||
if (USB_EP_DIR_IS_IN(cfg->ep_addr)) {
|
||||
endpoint->EPCFG.bit.EPTYPE1 = type;
|
||||
endpoint->EPSTATUSCLR.bit.BK1RDY = 1;
|
||||
} else {
|
||||
|
@ -416,8 +416,8 @@ int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data *const cfg)
|
|||
int usb_dc_ep_set_stall(const uint8_t ep)
|
||||
{
|
||||
UsbDevice *regs = ®S->DEVICE;
|
||||
uint8_t for_in = ep & USB_EP_DIR_MASK;
|
||||
uint8_t ep_num = ep & ~USB_EP_DIR_MASK;
|
||||
uint8_t for_in = USB_EP_GET_DIR(ep);
|
||||
uint8_t ep_num = USB_EP_GET_IDX(ep);
|
||||
UsbDeviceEndpoint *endpoint = ®s->DeviceEndpoint[ep_num];
|
||||
|
||||
if (ep_num >= USB_NUM_ENDPOINTS) {
|
||||
|
@ -437,8 +437,8 @@ int usb_dc_ep_set_stall(const uint8_t ep)
|
|||
int usb_dc_ep_clear_stall(const uint8_t ep)
|
||||
{
|
||||
UsbDevice *regs = ®S->DEVICE;
|
||||
uint8_t for_in = ep & USB_EP_DIR_MASK;
|
||||
uint8_t ep_num = ep & ~USB_EP_DIR_MASK;
|
||||
uint8_t for_in = USB_EP_GET_DIR(ep);
|
||||
uint8_t ep_num = USB_EP_GET_IDX(ep);
|
||||
UsbDeviceEndpoint *endpoint = ®s->DeviceEndpoint[ep_num];
|
||||
|
||||
if (ep_num >= USB_NUM_ENDPOINTS) {
|
||||
|
@ -458,8 +458,8 @@ int usb_dc_ep_clear_stall(const uint8_t ep)
|
|||
int usb_dc_ep_is_stalled(const uint8_t ep, uint8_t *stalled)
|
||||
{
|
||||
UsbDevice *regs = ®S->DEVICE;
|
||||
uint8_t for_in = ep & USB_EP_DIR_MASK;
|
||||
uint8_t ep_num = ep & ~USB_EP_DIR_MASK;
|
||||
uint8_t for_in = USB_EP_GET_DIR(ep);
|
||||
uint8_t ep_num = USB_EP_GET_IDX(ep);
|
||||
UsbDeviceEndpoint *endpoint = ®s->DeviceEndpoint[ep_num];
|
||||
|
||||
if (ep_num >= USB_NUM_ENDPOINTS) {
|
||||
|
@ -490,7 +490,7 @@ int usb_dc_ep_halt(uint8_t ep)
|
|||
/* Flush the selected endpoint */
|
||||
int usb_dc_ep_flush(uint8_t ep)
|
||||
{
|
||||
uint8_t ep_num = ep & ~USB_EP_DIR_MASK;
|
||||
uint8_t ep_num = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (ep_num >= USB_NUM_ENDPOINTS) {
|
||||
LOG_ERR("endpoint index/address out of range");
|
||||
|
@ -507,8 +507,8 @@ int usb_dc_ep_flush(uint8_t ep)
|
|||
int usb_dc_ep_enable(const uint8_t ep)
|
||||
{
|
||||
UsbDevice *regs = ®S->DEVICE;
|
||||
uint8_t for_in = ep & USB_EP_DIR_MASK;
|
||||
uint8_t ep_num = ep & ~USB_EP_DIR_MASK;
|
||||
uint8_t for_in = USB_EP_GET_DIR(ep);
|
||||
uint8_t ep_num = USB_EP_GET_IDX(ep);
|
||||
UsbDeviceEndpoint *endpoint = ®s->DeviceEndpoint[ep_num];
|
||||
|
||||
if (ep_num >= USB_NUM_ENDPOINTS) {
|
||||
|
@ -533,7 +533,7 @@ int usb_dc_ep_enable(const uint8_t ep)
|
|||
int usb_dc_ep_disable(uint8_t ep)
|
||||
{
|
||||
UsbDevice *regs = ®S->DEVICE;
|
||||
uint8_t ep_num = ep & ~USB_EP_DIR_MASK;
|
||||
uint8_t ep_num = USB_EP_GET_IDX(ep);
|
||||
UsbDeviceEndpoint *endpoint = ®s->DeviceEndpoint[ep_num];
|
||||
|
||||
if (ep_num >= USB_NUM_ENDPOINTS) {
|
||||
|
@ -553,7 +553,7 @@ int usb_dc_ep_write(uint8_t ep, const uint8_t *buf, uint32_t len, uint32_t *ret_
|
|||
{
|
||||
struct usb_sam0_data *data = usb_sam0_get_data();
|
||||
UsbDevice *regs = ®S->DEVICE;
|
||||
uint8_t ep_num = ep & ~USB_EP_DIR_MASK;
|
||||
uint8_t ep_num = USB_EP_GET_IDX(ep);
|
||||
UsbDeviceEndpoint *endpoint = ®s->DeviceEndpoint[ep_num];
|
||||
UsbDeviceDescriptor *desc = &data->descriptors[ep_num];
|
||||
uint32_t addr = desc->DeviceDescBank[1].ADDR.reg;
|
||||
|
@ -596,7 +596,7 @@ int usb_dc_ep_read_ex(uint8_t ep, uint8_t *buf, uint32_t max_data_len,
|
|||
{
|
||||
struct usb_sam0_data *data = usb_sam0_get_data();
|
||||
UsbDevice *regs = ®S->DEVICE;
|
||||
uint8_t ep_num = ep & ~USB_EP_DIR_MASK;
|
||||
uint8_t ep_num = USB_EP_GET_IDX(ep);
|
||||
UsbDeviceEndpoint *endpoint = ®s->DeviceEndpoint[ep_num];
|
||||
UsbDeviceDescriptor *desc = &data->descriptors[ep_num];
|
||||
uint32_t addr = desc->DeviceDescBank[0].ADDR.reg;
|
||||
|
@ -664,7 +664,7 @@ int usb_dc_ep_read_continue(uint8_t ep)
|
|||
{
|
||||
struct usb_sam0_data *data = usb_sam0_get_data();
|
||||
UsbDevice *regs = ®S->DEVICE;
|
||||
uint8_t ep_num = ep & ~USB_EP_DIR_MASK;
|
||||
uint8_t ep_num = USB_EP_GET_IDX(ep);
|
||||
UsbDeviceEndpoint *endpoint = ®s->DeviceEndpoint[ep_num];
|
||||
|
||||
if (ep_num >= USB_NUM_ENDPOINTS) {
|
||||
|
@ -681,8 +681,8 @@ int usb_dc_ep_read_continue(uint8_t ep)
|
|||
int usb_dc_ep_set_callback(const uint8_t ep, const usb_dc_ep_callback cb)
|
||||
{
|
||||
struct usb_sam0_data *data = usb_sam0_get_data();
|
||||
uint8_t for_in = ep & USB_EP_DIR_MASK;
|
||||
uint8_t ep_num = ep & ~USB_EP_DIR_MASK;
|
||||
uint8_t for_in = USB_EP_GET_DIR(ep);
|
||||
uint8_t ep_num = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (ep_num >= USB_NUM_ENDPOINTS) {
|
||||
LOG_ERR("endpoint index/address out of range");
|
||||
|
@ -698,8 +698,8 @@ int usb_dc_ep_mps(const uint8_t ep)
|
|||
{
|
||||
struct usb_sam0_data *data = usb_sam0_get_data();
|
||||
UsbDevice *regs = ®S->DEVICE;
|
||||
uint8_t for_in = ep & USB_EP_DIR_MASK;
|
||||
uint8_t ep_num = ep & ~USB_EP_DIR_MASK;
|
||||
uint8_t for_in = USB_EP_GET_DIR(ep);
|
||||
uint8_t ep_num = USB_EP_GET_IDX(ep);
|
||||
UsbDeviceDescriptor *desc = &data->descriptors[ep_num];
|
||||
UsbDeviceEndpoint *endpoint = ®s->DeviceEndpoint[ep_num];
|
||||
|
||||
|
|
|
@ -160,10 +160,6 @@ LOG_MODULE_REGISTER(usb_dc_stm32);
|
|||
#define EP0_IN (EP0_IDX | USB_EP_DIR_IN)
|
||||
#define EP0_OUT (EP0_IDX | USB_EP_DIR_OUT)
|
||||
|
||||
#define EP_IDX(ep) ((ep) & ~USB_EP_DIR_MASK)
|
||||
#define EP_IS_IN(ep) (((ep) & USB_EP_DIR_MASK) == USB_EP_DIR_IN)
|
||||
#define EP_IS_OUT(ep) (((ep) & USB_EP_DIR_MASK) == USB_EP_DIR_OUT)
|
||||
|
||||
/* Endpoint state */
|
||||
struct usb_dc_stm32_ep_state {
|
||||
uint16_t ep_mps; /** Endpoint max packet size */
|
||||
|
@ -197,17 +193,17 @@ static struct usb_dc_stm32_ep_state *usb_dc_stm32_get_ep_state(uint8_t ep)
|
|||
{
|
||||
struct usb_dc_stm32_ep_state *ep_state_base;
|
||||
|
||||
if (EP_IDX(ep) >= USB_NUM_BIDIR_ENDPOINTS) {
|
||||
if (USB_EP_GET_IDX(ep) >= USB_NUM_BIDIR_ENDPOINTS) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (EP_IS_OUT(ep)) {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
ep_state_base = usb_dc_stm32_state.out_ep_state;
|
||||
} else {
|
||||
ep_state_base = usb_dc_stm32_state.in_ep_state;
|
||||
}
|
||||
|
||||
return ep_state_base + EP_IDX(ep);
|
||||
return ep_state_base + USB_EP_GET_IDX(ep);
|
||||
}
|
||||
|
||||
static void usb_dc_stm32_isr(void *arg)
|
||||
|
@ -537,7 +533,7 @@ int usb_dc_ep_start_read(uint8_t ep, uint8_t *data, uint32_t max_data_len)
|
|||
LOG_DBG("ep 0x%02x, len %u", ep, max_data_len);
|
||||
|
||||
/* we flush EP0_IN by doing a 0 length receive on it */
|
||||
if (!EP_IS_OUT(ep) && (ep != EP0_IN || max_data_len)) {
|
||||
if (!USB_EP_DIR_IS_OUT(ep) && (ep != EP0_IN || max_data_len)) {
|
||||
LOG_ERR("invalid ep 0x%02x", ep);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -547,7 +543,7 @@ int usb_dc_ep_start_read(uint8_t ep, uint8_t *data, uint32_t max_data_len)
|
|||
}
|
||||
|
||||
status = HAL_PCD_EP_Receive(&usb_dc_stm32_state.pcd, ep,
|
||||
usb_dc_stm32_state.ep_buf[EP_IDX(ep)],
|
||||
usb_dc_stm32_state.ep_buf[USB_EP_GET_IDX(ep)],
|
||||
max_data_len);
|
||||
if (status != HAL_OK) {
|
||||
LOG_ERR("HAL_PCD_EP_Receive failed(0x%02x), %d", ep,
|
||||
|
@ -560,7 +556,7 @@ int usb_dc_ep_start_read(uint8_t ep, uint8_t *data, uint32_t max_data_len)
|
|||
|
||||
int usb_dc_ep_get_read_count(uint8_t ep, uint32_t *read_bytes)
|
||||
{
|
||||
if (!EP_IS_OUT(ep) || !read_bytes) {
|
||||
if (!USB_EP_DIR_IS_OUT(ep) || !read_bytes) {
|
||||
LOG_ERR("invalid ep 0x%02x", ep);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -572,7 +568,7 @@ int usb_dc_ep_get_read_count(uint8_t ep, uint32_t *read_bytes)
|
|||
|
||||
int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data * const cfg)
|
||||
{
|
||||
uint8_t ep_idx = EP_IDX(cfg->ep_addr);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(cfg->ep_addr);
|
||||
|
||||
LOG_DBG("ep %x, mps %d, type %d", cfg->ep_addr, cfg->ep_mps,
|
||||
cfg->ep_type);
|
||||
|
@ -721,9 +717,9 @@ int usb_dc_ep_enable(const uint8_t ep)
|
|||
return -EIO;
|
||||
}
|
||||
|
||||
if (EP_IS_OUT(ep) && ep != EP0_OUT) {
|
||||
if (USB_EP_DIR_IS_OUT(ep) && ep != EP0_OUT) {
|
||||
return usb_dc_ep_start_read(ep,
|
||||
usb_dc_stm32_state.ep_buf[EP_IDX(ep)],
|
||||
usb_dc_stm32_state.ep_buf[USB_EP_GET_IDX(ep)],
|
||||
EP_MPS);
|
||||
}
|
||||
|
||||
|
@ -761,7 +757,7 @@ int usb_dc_ep_write(const uint8_t ep, const uint8_t *const data,
|
|||
|
||||
LOG_DBG("ep 0x%02x, len %u", ep, data_len);
|
||||
|
||||
if (!ep_state || !EP_IS_IN(ep)) {
|
||||
if (!ep_state || !USB_EP_DIR_IS_IN(ep)) {
|
||||
LOG_ERR("invalid ep 0x%02x", ep);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -823,7 +819,7 @@ int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
|
|||
LOG_DBG("ep 0x%02x, %u bytes, %u+%u, %p", ep, max_data_len,
|
||||
ep_state->read_offset, read_count, data);
|
||||
|
||||
if (!EP_IS_OUT(ep)) { /* check if OUT ep */
|
||||
if (!USB_EP_DIR_IS_OUT(ep)) { /* check if OUT ep */
|
||||
LOG_ERR("Wrong endpoint direction: 0x%02x", ep);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -834,7 +830,7 @@ int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
|
|||
*/
|
||||
if (data) {
|
||||
read_count = MIN(read_count, max_data_len);
|
||||
memcpy(data, usb_dc_stm32_state.ep_buf[EP_IDX(ep)] +
|
||||
memcpy(data, usb_dc_stm32_state.ep_buf[USB_EP_GET_IDX(ep)] +
|
||||
ep_state->read_offset, read_count);
|
||||
ep_state->read_count -= read_count;
|
||||
ep_state->read_offset += read_count;
|
||||
|
@ -853,7 +849,7 @@ int usb_dc_ep_read_continue(uint8_t ep)
|
|||
{
|
||||
struct usb_dc_stm32_ep_state *ep_state = usb_dc_stm32_get_ep_state(ep);
|
||||
|
||||
if (!ep_state || !EP_IS_OUT(ep)) { /* Check if OUT ep */
|
||||
if (!ep_state || !USB_EP_DIR_IS_OUT(ep)) { /* Check if OUT ep */
|
||||
LOG_ERR("Not valid endpoint: %02x", ep);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -862,7 +858,7 @@ int usb_dc_ep_read_continue(uint8_t ep)
|
|||
* DataOutStageCallback will called on transaction complete.
|
||||
*/
|
||||
if (!ep_state->read_count) {
|
||||
usb_dc_ep_start_read(ep, usb_dc_stm32_state.ep_buf[EP_IDX(ep)],
|
||||
usb_dc_ep_start_read(ep, usb_dc_stm32_state.ep_buf[USB_EP_GET_IDX(ep)],
|
||||
EP_MPS);
|
||||
}
|
||||
|
||||
|
@ -1026,7 +1022,7 @@ void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
|
|||
|
||||
void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
|
||||
{
|
||||
uint8_t ep_idx = EP_IDX(epnum);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(epnum);
|
||||
uint8_t ep = ep_idx | USB_EP_DIR_OUT;
|
||||
struct usb_dc_stm32_ep_state *ep_state = usb_dc_stm32_get_ep_state(ep);
|
||||
|
||||
|
@ -1046,7 +1042,7 @@ void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
|
|||
|
||||
void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
|
||||
{
|
||||
uint8_t ep_idx = EP_IDX(epnum);
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(epnum);
|
||||
uint8_t ep = ep_idx | USB_EP_DIR_IN;
|
||||
struct usb_dc_stm32_ep_state *ep_state = usb_dc_stm32_get_ep_state(ep);
|
||||
|
||||
|
|
|
@ -26,6 +26,17 @@
|
|||
#define USB_EP_DIR_IN 0x80
|
||||
#define USB_EP_DIR_OUT 0x00
|
||||
|
||||
/** Get endpoint index (number) from endpoint address */
|
||||
#define USB_EP_GET_IDX(ep) ((ep) & ~USB_EP_DIR_MASK)
|
||||
/** Get direction from endpoint address */
|
||||
#define USB_EP_GET_DIR(ep) ((ep) & USB_EP_DIR_MASK)
|
||||
/** Get endpoint address from endpoint index and direction */
|
||||
#define USB_EP_GET_ADDR(idx, dir) ((idx) | ((dir) & USB_EP_DIR_MASK))
|
||||
/** True if the endpoint is an IN endpoint */
|
||||
#define USB_EP_DIR_IS_IN(ep) (USB_EP_GET_DIR(ep) == USB_EP_DIR_IN)
|
||||
/** True if the endpoint is an OUT endpoint */
|
||||
#define USB_EP_DIR_IS_OUT(ep) (USB_EP_GET_DIR(ep) == USB_EP_DIR_OUT)
|
||||
|
||||
/**
|
||||
* USB endpoint Transfer Type mask.
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue