drivers: usb_dc_mcux_ehci: rework ep_write/ep_read
Rework ep_write/ep_read. Fix style. Signed-off-by: Johann Fischer <j.fischer@phytec.de>
This commit is contained in:
parent
0958667125
commit
c2d45640c2
1 changed files with 64 additions and 35 deletions
|
@ -7,6 +7,7 @@
|
||||||
#include <soc.h>
|
#include <soc.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <drivers/usb/usb_dc.h>
|
#include <drivers/usb/usb_dc.h>
|
||||||
|
#include <usb/usb_device.h>
|
||||||
#include <soc.h>
|
#include <soc.h>
|
||||||
#include <device.h>
|
#include <device.h>
|
||||||
#include "usb_dc_mcux.h"
|
#include "usb_dc_mcux.h"
|
||||||
|
@ -374,19 +375,20 @@ int usb_dc_ep_flush(const u8_t ep)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* send data to the host */
|
|
||||||
int usb_dc_ep_write(const u8_t ep, const u8_t *const data,
|
int usb_dc_ep_write(const u8_t ep, const u8_t *const data,
|
||||||
const u32_t data_len, u32_t *const ret_bytes)
|
const u32_t data_len, u32_t *const ret_bytes)
|
||||||
{
|
{
|
||||||
u8_t ep_abs_idx = EP_ABS_IDX(ep);
|
u8_t ep_abs_idx = EP_ABS_IDX(ep);
|
||||||
u8_t *buffer = (u8_t *)dev_data.eps[ep_abs_idx].block.data;
|
u8_t *buffer = (u8_t *)dev_data.eps[ep_abs_idx].block.data;
|
||||||
u32_t len_to_send;
|
u32_t len_to_send;
|
||||||
|
usb_status_t status;
|
||||||
|
|
||||||
if (data_len > dev_data.eps[ep_abs_idx].ep_mps) {
|
if (data_len > dev_data.eps[ep_abs_idx].ep_mps) {
|
||||||
len_to_send = dev_data.eps[ep_abs_idx].ep_mps;
|
len_to_send = dev_data.eps[ep_abs_idx].ep_mps;
|
||||||
} else {
|
} else {
|
||||||
len_to_send = data_len;
|
len_to_send = data_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (u32_t n = 0; n < len_to_send; n++) {
|
for (u32_t n = 0; n < len_to_send; n++) {
|
||||||
buffer[n] = data[n];
|
buffer[n] = data[n];
|
||||||
}
|
}
|
||||||
|
@ -394,16 +396,46 @@ int usb_dc_ep_write(const u8_t ep, const u8_t *const data,
|
||||||
#if defined(CONFIG_HAS_MCUX_CACHE) && !defined(EP_BUF_NONCACHED)
|
#if defined(CONFIG_HAS_MCUX_CACHE) && !defined(EP_BUF_NONCACHED)
|
||||||
DCACHE_CleanByRange((uint32_t)buffer, len_to_send);
|
DCACHE_CleanByRange((uint32_t)buffer, len_to_send);
|
||||||
#endif
|
#endif
|
||||||
dev_data.interface->deviceSend(dev_data.controllerHandle,
|
status = dev_data.interface->deviceSend(dev_data.controllerHandle,
|
||||||
ep,
|
ep, buffer, len_to_send);
|
||||||
buffer,
|
if (kStatus_USB_Success != status) {
|
||||||
len_to_send);
|
LOG_ERR("Failed to fill ep 0x%02x buffer", ep);
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
|
||||||
if (ret_bytes) {
|
if (ret_bytes) {
|
||||||
*ret_bytes = len_to_send;
|
*ret_bytes = len_to_send;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void update_control_stage(usb_device_callback_message_struct_t *cb_msg,
|
||||||
|
u32_t data_len, u32_t max_data_len)
|
||||||
|
{
|
||||||
|
struct usb_setup_packet *usbd_setup;
|
||||||
|
|
||||||
|
usbd_setup = (struct usb_setup_packet *)cb_msg->buffer;
|
||||||
|
|
||||||
|
if (cb_msg->isSetup) {
|
||||||
|
if (usbd_setup->wLength == 0) {
|
||||||
|
dev_data.setupDataStage = SETUP_DATA_STAGE_DONE;
|
||||||
|
} else if (REQTYPE_GET_DIR(usbd_setup->bmRequestType)
|
||||||
|
== REQTYPE_DIR_TO_HOST) {
|
||||||
|
dev_data.setupDataStage = SETUP_DATA_STAGE_IN;
|
||||||
|
} else {
|
||||||
|
dev_data.setupDataStage = SETUP_DATA_STAGE_OUT;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (dev_data.setupDataStage != SETUP_DATA_STAGE_DONE) {
|
||||||
|
if ((data_len >= max_data_len) ||
|
||||||
|
(data_len < dev_data.eps[0].ep_mps)) {
|
||||||
|
dev_data.setupDataStage = SETUP_DATA_STAGE_DONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int usb_dc_ep_read_wait(u8_t ep, u8_t *data, u32_t max_data_len,
|
int usb_dc_ep_read_wait(u8_t ep, u8_t *data, u32_t max_data_len,
|
||||||
u32_t *read_bytes)
|
u32_t *read_bytes)
|
||||||
{
|
{
|
||||||
|
@ -416,16 +448,20 @@ int usb_dc_ep_read_wait(u8_t ep, u8_t *data, u32_t max_data_len,
|
||||||
LOG_ERR("Endpoint is occupied by the controller");
|
LOG_ERR("Endpoint is occupied by the controller");
|
||||||
return -EBUSY;
|
return -EBUSY;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((ep_idx >= NUM_OF_EP_MAX) || (EP_ADDR2DIR(ep) != USB_EP_DIR_OUT)) {
|
if ((ep_idx >= NUM_OF_EP_MAX) || (EP_ADDR2DIR(ep) != USB_EP_DIR_OUT)) {
|
||||||
LOG_ERR("Wrong endpoint index/address/direction");
|
LOG_ERR("Wrong endpoint index/address/direction");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allow to read 0 bytes */
|
/* Allow to read 0 bytes */
|
||||||
if (!data && max_data_len) {
|
if (!data && max_data_len) {
|
||||||
LOG_ERR("Wrong arguments");
|
LOG_ERR("Wrong arguments");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
/* it is control setup, we should use message.buffer,
|
|
||||||
|
/*
|
||||||
|
* It is control setup, we should use message.buffer,
|
||||||
* this buffer is from internal setup array.
|
* this buffer is from internal setup array.
|
||||||
*/
|
*/
|
||||||
bufp = dev_data.eps[ep_abs_idx].transfer_message.buffer;
|
bufp = dev_data.eps[ep_abs_idx].transfer_message.buffer;
|
||||||
|
@ -436,6 +472,7 @@ int usb_dc_ep_read_wait(u8_t ep, u8_t *data, u32_t max_data_len,
|
||||||
}
|
}
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data && !max_data_len) {
|
if (!data && !max_data_len) {
|
||||||
/* When both buffer and max data to read are zero return the
|
/* When both buffer and max data to read are zero return the
|
||||||
* available data in buffer.
|
* available data in buffer.
|
||||||
|
@ -445,46 +482,34 @@ int usb_dc_ep_read_wait(u8_t ep, u8_t *data, u32_t max_data_len,
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data_len > max_data_len) {
|
if (data_len > max_data_len) {
|
||||||
LOG_WRN("Not enough room to copy all the data!");
|
LOG_WRN("Not enough room to copy all the data!");
|
||||||
data_len = max_data_len;
|
data_len = max_data_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data != NULL) {
|
if (data != NULL) {
|
||||||
for (u32_t i = 0; i < data_len; i++) {
|
for (u32_t i = 0; i < data_len; i++) {
|
||||||
data[i] = bufp[i];
|
data[i] = bufp[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (read_bytes) {
|
if (read_bytes) {
|
||||||
*read_bytes = data_len;
|
*read_bytes = data_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (EP_ADDR2IDX(ep) == USB_ENDPOINT_CONTROL) {
|
if (EP_ADDR2IDX(ep) == USB_ENDPOINT_CONTROL) {
|
||||||
u8_t isSetup = dev_data.eps[0].transfer_message.isSetup;
|
update_control_stage(&dev_data.eps[0].transfer_message,
|
||||||
u8_t *buffer = dev_data.eps[0].transfer_message.buffer;
|
data_len, max_data_len);
|
||||||
|
|
||||||
if (isSetup) {
|
|
||||||
if (((usb_setup_struct_t *)buffer)->wLength == 0) {
|
|
||||||
dev_data.setupDataStage = SETUP_DATA_STAGE_DONE;
|
|
||||||
} else if (((usb_setup_struct_t *)buffer)->bmRequestType & USB_REQUEST_TYPE_DIR_MASK) {
|
|
||||||
dev_data.setupDataStage = SETUP_DATA_STAGE_IN;
|
|
||||||
} else {
|
|
||||||
dev_data.setupDataStage = SETUP_DATA_STAGE_OUT;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (dev_data.setupDataStage != SETUP_DATA_STAGE_DONE) {
|
|
||||||
if ((data_len >= max_data_len) || (data_len < dev_data.eps[0].ep_mps)) {
|
|
||||||
dev_data.setupDataStage = SETUP_DATA_STAGE_DONE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int usb_dc_ep_read_continue(u8_t ep)
|
int usb_dc_ep_read_continue(u8_t ep)
|
||||||
{
|
{
|
||||||
/* select the index of the next endpoint buffer */
|
|
||||||
u8_t ep_abs_idx = EP_ABS_IDX(ep);
|
u8_t ep_abs_idx = EP_ABS_IDX(ep);
|
||||||
|
usb_status_t status;
|
||||||
|
|
||||||
if (dev_data.eps[ep_abs_idx].ep_occupied) {
|
if (dev_data.eps[ep_abs_idx].ep_occupied) {
|
||||||
LOG_WRN("endpoint 0x%x already occupied", ep);
|
LOG_WRN("endpoint 0x%x already occupied", ep);
|
||||||
|
@ -495,14 +520,20 @@ int usb_dc_ep_read_continue(u8_t ep)
|
||||||
if (dev_data.setupDataStage == SETUP_DATA_STAGE_DONE) {
|
if (dev_data.setupDataStage == SETUP_DATA_STAGE_DONE) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dev_data.setupDataStage == SETUP_DATA_STAGE_IN) {
|
if (dev_data.setupDataStage == SETUP_DATA_STAGE_IN) {
|
||||||
dev_data.setupDataStage = SETUP_DATA_STAGE_DONE;
|
dev_data.setupDataStage = SETUP_DATA_STAGE_DONE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dev_data.interface->deviceRecv(dev_data.controllerHandle,
|
|
||||||
ep,
|
status = dev_data.interface->deviceRecv(dev_data.controllerHandle, ep,
|
||||||
(u8_t *)dev_data.eps[ep_abs_idx].block.data,
|
(u8_t *)dev_data.eps[ep_abs_idx].block.data,
|
||||||
dev_data.eps[ep_abs_idx].ep_mps);
|
dev_data.eps[ep_abs_idx].ep_mps);
|
||||||
|
if (kStatus_USB_Success != status) {
|
||||||
|
LOG_ERR("Failed to enable reception on ep 0x%02x", ep);
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
|
||||||
dev_data.eps[ep_abs_idx].ep_occupied = true;
|
dev_data.eps[ep_abs_idx].ep_occupied = true;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -516,18 +547,16 @@ int usb_dc_ep_read(const u8_t ep, u8_t *const data,
|
||||||
if (retval) {
|
if (retval) {
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data && !max_data_len) {
|
if (!data && !max_data_len) {
|
||||||
/* When both buffer and max data to read are zero the above
|
/*
|
||||||
|
* When both buffer and max data to read are zero the above
|
||||||
* call would fetch the data len and we simply return.
|
* call would fetch the data len and we simply return.
|
||||||
*/
|
*/
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (usb_dc_ep_read_continue(ep) != 0) {
|
return usb_dc_ep_read_continue(ep);
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int usb_dc_ep_set_callback(const u8_t ep, const usb_dc_ep_callback cb)
|
int usb_dc_ep_set_callback(const u8_t ep, const usb_dc_ep_callback cb)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue