2019-05-05 13:18:40 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2019, NXP
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <soc.h>
|
|
|
|
#include <string.h>
|
2020-01-25 05:35:03 -06:00
|
|
|
#include <drivers/usb/usb_dc.h>
|
2019-05-05 13:18:40 +08:00
|
|
|
#include <soc.h>
|
|
|
|
#include <device.h>
|
|
|
|
#include "usb_dc_mcux.h"
|
|
|
|
#include "usb_device_ehci.h"
|
|
|
|
|
2019-06-11 17:54:01 +02:00
|
|
|
#ifdef CONFIG_HAS_MCUX_CACHE
|
|
|
|
#include <fsl_cache.h>
|
|
|
|
#endif
|
|
|
|
|
2019-05-05 13:18:40 +08:00
|
|
|
#define LOG_LEVEL CONFIG_USB_DRIVER_LOG_LEVEL
|
|
|
|
#include <logging/log.h>
|
|
|
|
LOG_MODULE_REGISTER(usb_dc_mcux_ehci);
|
|
|
|
|
|
|
|
#define CONTROLLER_ID kUSB_ControllerEhci0
|
|
|
|
|
|
|
|
static void usb_isr_handler(void);
|
|
|
|
extern void USB_DeviceEhciIsrFunction(void *deviceHandle);
|
|
|
|
|
|
|
|
/* the setup transfer state */
|
2019-12-02 17:00:27 +01:00
|
|
|
#define SETUP_DATA_STAGE_DONE (0)
|
|
|
|
#define SETUP_DATA_STAGE_IN (1)
|
|
|
|
#define SETUP_DATA_STAGE_OUT (2)
|
|
|
|
|
2019-05-05 13:18:40 +08:00
|
|
|
/* Then endpoint number/index calculation */
|
2019-12-02 17:00:27 +01:00
|
|
|
#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 NUM_OF_EP_MAX DT_USBD_MCUX_EHCI_NUM_BIDIR_EP
|
|
|
|
|
2019-05-05 13:18:40 +08:00
|
|
|
/* The minimum value is 1 */
|
2019-12-02 17:00:27 +01:00
|
|
|
#define EP_BUF_NUMOF_BLOCKS ((NUM_OF_EP_MAX + 3) / 4)
|
|
|
|
|
2019-05-05 13:18:40 +08:00
|
|
|
/* The max MPS is 1023 for FS, 1024 for HS. */
|
|
|
|
#if defined(CONFIG_NOCACHE_MEMORY)
|
2019-06-11 17:54:01 +02:00
|
|
|
#define EP_BUF_NONCACHED
|
2019-05-05 13:18:40 +08:00
|
|
|
__nocache K_MEM_POOL_DEFINE(ep_buf_pool, 16, 1024, EP_BUF_NUMOF_BLOCKS, 4);
|
|
|
|
#else
|
|
|
|
K_MEM_POOL_DEFINE(ep_buf_pool, 16, 1024, EP_BUF_NUMOF_BLOCKS, 4);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static usb_ep_ctrl_data_t s_ep_ctrl[NUM_OF_EP_MAX];
|
2019-12-02 17:00:27 +01:00
|
|
|
static usb_device_struct_t dev_data;
|
2019-05-05 13:18:40 +08:00
|
|
|
|
|
|
|
#if ((defined(USB_DEVICE_CONFIG_EHCI)) && (USB_DEVICE_CONFIG_EHCI > 0U))
|
|
|
|
/* EHCI device driver interface */
|
2019-12-02 17:08:56 +01:00
|
|
|
static const usb_device_controller_interface_struct_t ehci_iface = {
|
2019-05-05 13:18:40 +08:00
|
|
|
USB_DeviceEhciInit, USB_DeviceEhciDeinit, USB_DeviceEhciSend,
|
|
|
|
USB_DeviceEhciRecv, USB_DeviceEhciCancel, USB_DeviceEhciControl
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int usb_dc_reset(void)
|
|
|
|
{
|
2019-12-02 17:00:27 +01:00
|
|
|
if (dev_data.controllerHandle != NULL) {
|
|
|
|
dev_data.interface->deviceControl(dev_data.controllerHandle, kUSB_DeviceControlStop, NULL);
|
|
|
|
dev_data.interface->deviceDeinit(dev_data.controllerHandle);
|
|
|
|
dev_data.controllerHandle = NULL;
|
2019-05-05 13:18:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_dc_attach(void)
|
|
|
|
{
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.eps = &s_ep_ctrl[0];
|
|
|
|
if (dev_data.attached) {
|
2019-05-05 13:18:40 +08:00
|
|
|
LOG_WRN("already attached");
|
|
|
|
return 0;
|
|
|
|
}
|
2019-12-02 17:08:56 +01:00
|
|
|
dev_data.interface = &ehci_iface;
|
2019-12-02 17:00:27 +01:00
|
|
|
if (kStatus_USB_Success != dev_data.interface->deviceInit(CONTROLLER_ID, &dev_data, &dev_data.controllerHandle)) {
|
2019-05-05 13:18:40 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Connect and enable USB interrupt */
|
|
|
|
IRQ_CONNECT(DT_USBD_MCUX_EHCI_IRQ, DT_USBD_MCUX_EHCI_IRQ_PRI,
|
|
|
|
usb_isr_handler, 0, 0);
|
|
|
|
irq_enable(DT_USBD_MCUX_EHCI_IRQ);
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.attached = 1;
|
2019-05-05 13:18:40 +08:00
|
|
|
LOG_DBG("attached");
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.interface->deviceControl(dev_data.controllerHandle, kUSB_DeviceControlRun, NULL);
|
2019-05-05 13:18:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_dc_detach(void)
|
|
|
|
{
|
|
|
|
LOG_DBG("detached.");
|
2019-12-02 17:00:27 +01:00
|
|
|
if (dev_data.controllerHandle != NULL) {
|
|
|
|
dev_data.interface->deviceControl(dev_data.controllerHandle, kUSB_DeviceControlStop, NULL);
|
|
|
|
dev_data.interface->deviceDeinit(dev_data.controllerHandle);
|
|
|
|
dev_data.controllerHandle = NULL;
|
2019-05-05 13:18:40 +08:00
|
|
|
}
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.attached = 0;
|
2019-05-05 13:18:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_dc_set_address(const u8_t addr)
|
|
|
|
{
|
|
|
|
LOG_DBG("");
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.address = addr;
|
2019-05-05 13:18:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data *const cfg)
|
|
|
|
{
|
|
|
|
u8_t ep_idx = EP_ADDR2IDX(cfg->ep_addr);
|
|
|
|
|
|
|
|
if ((cfg->ep_type == USB_DC_EP_CONTROL) && ep_idx) {
|
|
|
|
LOG_ERR("invalid endpoint configuration");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ep_idx >= NUM_OF_EP_MAX) {
|
|
|
|
LOG_ERR("endpoint index/address out of range");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ep_idx & BIT(0)) {
|
|
|
|
if (EP_ADDR2DIR(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) {
|
|
|
|
LOG_INF("pre-selected as OUT endpoint");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data *const cfg)
|
|
|
|
{
|
|
|
|
u8_t ep_abs_idx = EP_ABS_IDX(cfg->ep_addr);
|
2019-12-02 17:00:27 +01:00
|
|
|
usb_device_endpoint_init_struct_t ep_init;
|
2019-05-05 13:18:40 +08:00
|
|
|
struct k_mem_block *block;
|
2019-12-02 17:00:27 +01:00
|
|
|
struct usb_ep_ctrl_data *eps = &dev_data.eps[ep_abs_idx];
|
2019-05-05 13:18:40 +08:00
|
|
|
|
2019-12-02 17:00:27 +01:00
|
|
|
ep_init.zlt = 0U;
|
|
|
|
ep_init.endpointAddress = cfg->ep_addr;
|
|
|
|
ep_init.maxPacketSize = cfg->ep_mps;
|
|
|
|
ep_init.transferType = cfg->ep_type;
|
|
|
|
dev_data.eps[ep_abs_idx].ep_type = cfg->ep_type;
|
2019-05-05 13:18:40 +08:00
|
|
|
|
|
|
|
if (ep_abs_idx >= NUM_OF_EP_MAX) {
|
|
|
|
LOG_ERR("Wrong endpoint index/address");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
block = &(eps->block);
|
|
|
|
if (block->data) {
|
|
|
|
k_mem_pool_free(block);
|
|
|
|
block->data = NULL;
|
|
|
|
}
|
|
|
|
|
2019-10-02 19:26:14 -05:00
|
|
|
if (k_mem_pool_alloc(&ep_buf_pool, block, cfg->ep_mps, K_MSEC(10)) == 0) {
|
2019-05-05 13:18:40 +08:00
|
|
|
memset(block->data, 0, cfg->ep_mps);
|
|
|
|
} else {
|
|
|
|
LOG_ERR("Memory allocation time-out");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.eps[ep_abs_idx].ep_mps = cfg->ep_mps;
|
|
|
|
if (dev_data.eps[ep_abs_idx].ep_enabled) {
|
|
|
|
dev_data.interface->deviceControl(dev_data.controllerHandle, kUSB_DeviceControlEndpointDeinit, (void *)(&cfg->ep_addr));
|
2019-05-05 13:18:40 +08:00
|
|
|
}
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.interface->deviceControl(dev_data.controllerHandle, kUSB_DeviceControlEndpointInit, &ep_init);
|
2019-05-05 13:18:40 +08:00
|
|
|
|
|
|
|
/* if it is controlendpoint, 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)) {
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.eps[ep_abs_idx].ep_occupied = true;
|
2019-05-05 13:18:40 +08:00
|
|
|
}
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.eps[ep_abs_idx].ep_enabled = true;
|
2019-05-05 13:18:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_dc_ep_set_stall(const u8_t ep)
|
|
|
|
{
|
|
|
|
u8_t endpoint = ep;
|
|
|
|
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.interface->deviceControl(dev_data.controllerHandle, kUSB_DeviceControlEndpointStall, &endpoint);
|
2019-05-05 13:18:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_dc_ep_clear_stall(const u8_t ep)
|
|
|
|
{
|
|
|
|
u8_t endpoint = ep;
|
|
|
|
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.interface->deviceControl(dev_data.controllerHandle, kUSB_DeviceControlEndpointUnstall, &endpoint);
|
2019-05-05 13:18:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_dc_ep_is_stalled(const u8_t ep, u8_t *const stalled)
|
|
|
|
{
|
|
|
|
if (!stalled) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
*stalled = 0;
|
|
|
|
/* Get the endpoint status */
|
|
|
|
usb_device_endpoint_status_struct_t endpointStatus;
|
|
|
|
|
|
|
|
endpointStatus.endpointAddress = ep;
|
|
|
|
endpointStatus.endpointStatus = kUSB_DeviceEndpointStateIdle;
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.interface->deviceControl(dev_data.controllerHandle, kUSB_DeviceControlGetEndpointStatus, &endpointStatus);
|
2019-05-05 13:18:40 +08:00
|
|
|
*stalled = endpointStatus.endpointStatus;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_dc_ep_halt(const u8_t ep)
|
|
|
|
{
|
|
|
|
return usb_dc_ep_set_stall(ep);
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_dc_ep_enable(const u8_t ep)
|
|
|
|
{
|
|
|
|
u8_t ep_abs_idx = EP_ABS_IDX(ep);
|
|
|
|
|
|
|
|
/* endpoint0 OUT is primed by controller driver when configure this
|
|
|
|
* endpoint.
|
|
|
|
*/
|
|
|
|
if (!ep_abs_idx) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-12-02 17:00:27 +01:00
|
|
|
if (dev_data.eps[ep_abs_idx].ep_occupied) {
|
2019-05-05 13:18:40 +08:00
|
|
|
LOG_WRN("endpoint 0x%x already enabled", ep);
|
2020-02-05 10:45:50 +01:00
|
|
|
return -EALREADY;
|
2019-05-05 13:18:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((EP_ADDR2IDX(ep) != USB_CONTROL_ENDPOINT) && (EP_ADDR2DIR(ep) == USB_EP_DIR_OUT)) {
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.interface->deviceRecv(dev_data.controllerHandle,
|
2019-05-05 13:18:40 +08:00
|
|
|
ep,
|
2019-12-02 17:00:27 +01:00
|
|
|
(u8_t *)dev_data.eps[ep_abs_idx].block.data,
|
|
|
|
(uint32_t)dev_data.eps[ep_abs_idx].ep_mps);
|
|
|
|
dev_data.eps[ep_abs_idx].ep_occupied = true;
|
2019-05-05 13:18:40 +08:00
|
|
|
} else {
|
|
|
|
/* control enpoint just be enabled before enumeration,
|
|
|
|
* when running here, setup has been primed.
|
|
|
|
*/
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.eps[ep_abs_idx].ep_occupied = true;
|
2019-05-05 13:18:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_dc_ep_disable(const u8_t ep)
|
|
|
|
{
|
|
|
|
u8_t ep_abs_idx = EP_ABS_IDX(ep);
|
|
|
|
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.interface->deviceCancel(dev_data.controllerHandle, ep);
|
|
|
|
dev_data.eps[ep_abs_idx].ep_enabled = false;
|
2019-05-05 13:18:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_dc_ep_flush(const u8_t ep)
|
|
|
|
{
|
|
|
|
u8_t ep_idx = EP_ADDR2IDX(ep);
|
|
|
|
|
|
|
|
if (ep_idx >= NUM_OF_EP_MAX) {
|
|
|
|
LOG_ERR("Wrong endpoint index/address");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_DBG("ep %x, idx %d", ep_idx, ep);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send data to the host */
|
|
|
|
int usb_dc_ep_write(const u8_t ep, const u8_t *const data,
|
|
|
|
const u32_t data_len, u32_t *const ret_bytes)
|
|
|
|
{
|
|
|
|
u8_t ep_abs_idx = EP_ABS_IDX(ep);
|
2019-12-02 17:00:27 +01:00
|
|
|
u8_t *buffer = (u8_t *)dev_data.eps[ep_abs_idx].block.data;
|
2019-05-05 13:18:40 +08:00
|
|
|
u32_t len_to_send;
|
|
|
|
|
2019-12-02 17:00:27 +01:00
|
|
|
if (data_len > dev_data.eps[ep_abs_idx].ep_mps) {
|
|
|
|
len_to_send = dev_data.eps[ep_abs_idx].ep_mps;
|
2019-05-05 13:18:40 +08:00
|
|
|
} else {
|
|
|
|
len_to_send = data_len;
|
|
|
|
}
|
|
|
|
for (u32_t n = 0; n < len_to_send; n++) {
|
|
|
|
buffer[n] = data[n];
|
|
|
|
}
|
2019-06-11 17:54:01 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_HAS_MCUX_CACHE) && !defined(EP_BUF_NONCACHED)
|
|
|
|
DCACHE_CleanByRange((uint32_t)buffer, len_to_send);
|
|
|
|
#endif
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.interface->deviceSend(dev_data.controllerHandle,
|
2019-05-05 13:18:40 +08:00
|
|
|
ep,
|
|
|
|
buffer,
|
|
|
|
len_to_send);
|
|
|
|
if (ret_bytes) {
|
|
|
|
*ret_bytes = len_to_send;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_dc_ep_read_wait(u8_t ep, u8_t *data, u32_t max_data_len,
|
|
|
|
u32_t *read_bytes)
|
|
|
|
{
|
|
|
|
u8_t ep_idx = EP_ADDR2IDX(ep);
|
|
|
|
u8_t ep_abs_idx = EP_ABS_IDX(ep);
|
|
|
|
u32_t data_len;
|
|
|
|
u8_t *bufp = NULL;
|
|
|
|
|
2019-12-02 17:00:27 +01:00
|
|
|
while (dev_data.eps[ep_abs_idx].ep_occupied) {
|
2019-05-05 13:18:40 +08:00
|
|
|
LOG_ERR("Endpoint is occupied by the controller");
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
if ((ep_idx >= NUM_OF_EP_MAX) || (EP_ADDR2DIR(ep) != USB_EP_DIR_OUT)) {
|
|
|
|
LOG_ERR("Wrong endpoint index/address/direction");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
/* Allow to read 0 bytes */
|
|
|
|
if (!data && max_data_len) {
|
|
|
|
LOG_ERR("Wrong arguments");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
/* it is control setup, we should use message.buffer,
|
|
|
|
* this buffer is from internal setup array.
|
|
|
|
*/
|
2019-12-02 17:00:27 +01:00
|
|
|
bufp = dev_data.eps[ep_abs_idx].transfer_message.buffer;
|
|
|
|
data_len = dev_data.eps[ep_abs_idx].transfer_message.length;
|
2019-05-05 13:18:40 +08:00
|
|
|
if (data_len == USB_UNINITIALIZED_VAL_32) {
|
|
|
|
if (read_bytes) {
|
|
|
|
*read_bytes = 0;
|
|
|
|
}
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (!data && !max_data_len) {
|
|
|
|
/* When both buffer and max data to read are zero return the
|
|
|
|
* available data in buffer.
|
|
|
|
*/
|
|
|
|
if (read_bytes) {
|
|
|
|
*read_bytes = data_len;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (data_len > max_data_len) {
|
|
|
|
LOG_WRN("Not enough room to copy all the data!");
|
|
|
|
data_len = max_data_len;
|
|
|
|
}
|
|
|
|
if (data != NULL) {
|
|
|
|
for (u32_t i = 0; i < data_len; i++) {
|
|
|
|
data[i] = bufp[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (read_bytes) {
|
|
|
|
*read_bytes = data_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EP_ADDR2IDX(ep) == USB_ENDPOINT_CONTROL) {
|
2019-12-02 17:00:27 +01:00
|
|
|
u8_t isSetup = dev_data.eps[0].transfer_message.isSetup;
|
|
|
|
u8_t *buffer = dev_data.eps[0].transfer_message.buffer;
|
2019-05-05 13:18:40 +08:00
|
|
|
|
|
|
|
if (isSetup) {
|
|
|
|
if (((usb_setup_struct_t *)buffer)->wLength == 0) {
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.setupDataStage = SETUP_DATA_STAGE_DONE;
|
2019-05-05 13:18:40 +08:00
|
|
|
} else if (((usb_setup_struct_t *)buffer)->bmRequestType & USB_REQUEST_TYPE_DIR_MASK) {
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.setupDataStage = SETUP_DATA_STAGE_IN;
|
2019-05-05 13:18:40 +08:00
|
|
|
} else {
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.setupDataStage = SETUP_DATA_STAGE_OUT;
|
2019-05-05 13:18:40 +08:00
|
|
|
}
|
|
|
|
} else {
|
2019-12-02 17:00:27 +01:00
|
|
|
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;
|
2019-05-05 13:18:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-12-02 17:00:27 +01:00
|
|
|
if (dev_data.eps[ep_abs_idx].ep_occupied) {
|
2019-06-11 10:27:04 +02:00
|
|
|
LOG_WRN("endpoint 0x%x already occupied", ep);
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2019-05-05 13:18:40 +08:00
|
|
|
if (EP_ADDR2IDX(ep) == USB_ENDPOINT_CONTROL) {
|
2019-12-02 17:00:27 +01:00
|
|
|
if (dev_data.setupDataStage == SETUP_DATA_STAGE_DONE) {
|
2019-05-05 13:18:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2019-12-02 17:00:27 +01:00
|
|
|
if (dev_data.setupDataStage == SETUP_DATA_STAGE_IN) {
|
|
|
|
dev_data.setupDataStage = SETUP_DATA_STAGE_DONE;
|
2019-05-05 13:18:40 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.interface->deviceRecv(dev_data.controllerHandle,
|
2019-05-05 13:18:40 +08:00
|
|
|
ep,
|
2019-12-02 17:00:27 +01:00
|
|
|
(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_occupied = true;
|
2019-05-05 13:18:40 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_dc_ep_read(const u8_t ep, u8_t *const data,
|
|
|
|
const u32_t max_data_len, u32_t *const read_bytes)
|
|
|
|
{
|
|
|
|
int retval = usb_dc_ep_read_wait(ep, data, max_data_len, read_bytes);
|
|
|
|
|
|
|
|
if (retval) {
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
if (!data && !max_data_len) {
|
|
|
|
/* When both buffer and max data to read are zero the above
|
|
|
|
* call would fetch the data len and we simply return.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usb_dc_ep_read_continue(ep) != 0) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_dc_ep_set_callback(const u8_t ep, const usb_dc_ep_callback cb)
|
|
|
|
{
|
|
|
|
u8_t ep_abs_idx = EP_ABS_IDX(ep);
|
|
|
|
|
2019-12-02 17:00:27 +01:00
|
|
|
if (!dev_data.attached) {
|
2019-05-05 13:18:40 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.eps[ep_abs_idx].callback = cb;
|
2019-05-05 13:18:40 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_dc_set_status_callback(const usb_dc_status_callback cb)
|
|
|
|
{
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.status_callback = cb;
|
2019-05-05 13:18:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int usb_dc_ep_mps(const u8_t ep)
|
|
|
|
{
|
|
|
|
u8_t ep_abs_idx = EP_ABS_IDX(ep);
|
|
|
|
|
2019-12-02 17:00:27 +01:00
|
|
|
return dev_data.eps[ep_abs_idx].ep_mps;
|
2019-05-05 13:18:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void control_endpoint_enable(void)
|
|
|
|
{
|
2019-12-02 17:00:27 +01:00
|
|
|
usb_device_endpoint_init_struct_t ep_init;
|
2019-05-05 13:18:40 +08:00
|
|
|
u8_t ep_abs_idx = 0;
|
|
|
|
|
2019-12-02 17:00:27 +01:00
|
|
|
ep_init.zlt = 0U;
|
|
|
|
ep_init.transferType = USB_ENDPOINT_CONTROL;
|
|
|
|
ep_init.maxPacketSize = EP0_MAX_PACKET_SIZE;
|
2019-05-05 13:18:40 +08:00
|
|
|
|
2019-12-02 17:00:27 +01:00
|
|
|
ep_init.endpointAddress = EP0_OUT;
|
|
|
|
ep_abs_idx = EP_ABS_IDX(ep_init.endpointAddress);
|
|
|
|
dev_data.eps[ep_abs_idx].ep_mps = EP0_MAX_PACKET_SIZE;
|
2019-05-05 13:18:40 +08:00
|
|
|
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.interface->deviceControl(dev_data.controllerHandle, kUSB_DeviceControlEndpointInit, &ep_init);
|
|
|
|
dev_data.eps[ep_abs_idx].ep_occupied = false;
|
|
|
|
dev_data.eps[ep_abs_idx].ep_enabled = true;
|
2019-05-05 13:18:40 +08:00
|
|
|
|
2019-12-02 17:00:27 +01:00
|
|
|
ep_init.endpointAddress = EP0_IN;
|
|
|
|
ep_abs_idx = EP_ABS_IDX(ep_init.endpointAddress);
|
|
|
|
dev_data.eps[ep_abs_idx].ep_mps = EP0_MAX_PACKET_SIZE;
|
|
|
|
dev_data.interface->deviceControl(dev_data.controllerHandle, kUSB_DeviceControlEndpointInit, &ep_init);
|
|
|
|
dev_data.eps[ep_abs_idx].ep_occupied = false;
|
|
|
|
dev_data.eps[ep_abs_idx].ep_enabled = true;
|
2019-05-05 13:18:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Notify the up layer the KHCI status changed. */
|
|
|
|
void USB_DeviceNotificationTrigger(void *handle, void *msg)
|
|
|
|
{
|
|
|
|
usb_device_callback_message_struct_t *message = (usb_device_callback_message_struct_t *)msg;
|
|
|
|
|
|
|
|
switch (message->code) {
|
|
|
|
case kUSB_DeviceNotifyBusReset:
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.address = 0;
|
|
|
|
dev_data.interface->deviceControl(dev_data.controllerHandle, kUSB_DeviceControlSetDefaultStatus, NULL);
|
2019-05-05 13:18:40 +08:00
|
|
|
for (int i = 0; i < NUM_OF_EP_MAX; i++) {
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.eps[i].ep_occupied = false;
|
|
|
|
dev_data.eps[i].ep_enabled = false;
|
2019-05-05 13:18:40 +08:00
|
|
|
}
|
|
|
|
control_endpoint_enable();
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.status_callback(USB_DC_RESET, NULL);
|
2019-05-05 13:18:40 +08:00
|
|
|
break;
|
|
|
|
case kUSB_DeviceNotifyError:
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.status_callback(USB_DC_ERROR, NULL);
|
2019-05-05 13:18:40 +08:00
|
|
|
break;
|
|
|
|
case kUSB_DeviceNotifySuspend:
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.status_callback(USB_DC_SUSPEND, NULL);
|
2019-05-05 13:18:40 +08:00
|
|
|
break;
|
|
|
|
case kUSB_DeviceNotifyResume:
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.status_callback(USB_DC_RESUME, NULL);
|
2019-05-05 13:18:40 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
u8_t ep_packet_type = 0;
|
|
|
|
u8_t ep_abs_idx = EP_ABS_IDX(message->code);
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.eps[ep_abs_idx].transfer_message.length = message->length;
|
|
|
|
dev_data.eps[ep_abs_idx].transfer_message.isSetup = message->isSetup;
|
|
|
|
dev_data.eps[ep_abs_idx].transfer_message.code = message->code;
|
|
|
|
dev_data.eps[ep_abs_idx].transfer_message.buffer = message->buffer;
|
|
|
|
dev_data.eps[ep_abs_idx].ep_occupied = false;
|
2019-05-05 13:18:40 +08:00
|
|
|
if (message->isSetup) {
|
|
|
|
ep_packet_type = USB_DC_EP_SETUP;
|
|
|
|
} else {
|
|
|
|
/* IN TOKEN */
|
|
|
|
if ((message->code & USB_REQUEST_TYPE_DIR_MASK) == USB_REQUEST_TYPE_DIR_IN) {
|
|
|
|
/* control endpoint 0 and status stage for setAddr transfer */
|
2019-12-02 17:00:27 +01:00
|
|
|
if ((dev_data.address != 0) && (ep_abs_idx == 1)) {
|
2019-05-05 13:18:40 +08:00
|
|
|
/* SET ADDRESS in the status stage in the IN transfer*/
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.interface->deviceControl(dev_data.controllerHandle, kUSB_DeviceControlSetDeviceAddress, &dev_data.address);
|
|
|
|
dev_data.address = 0;
|
2019-05-05 13:18:40 +08:00
|
|
|
}
|
|
|
|
ep_packet_type = USB_DC_EP_DATA_IN;
|
|
|
|
}
|
|
|
|
/* OUT TOKEN */
|
|
|
|
else {
|
|
|
|
ep_packet_type = USB_DC_EP_DATA_OUT;
|
|
|
|
}
|
|
|
|
}
|
2019-12-02 17:00:27 +01:00
|
|
|
if (dev_data.eps[ep_abs_idx].callback) {
|
2019-06-11 17:54:01 +02:00
|
|
|
#if defined(CONFIG_HAS_MCUX_CACHE) && !defined(EP_BUF_NONCACHED)
|
|
|
|
if (message->length) {
|
|
|
|
DCACHE_InvalidateByRange((uint32_t)message->buffer,
|
|
|
|
message->length);
|
|
|
|
}
|
|
|
|
#endif
|
2019-12-02 17:00:27 +01:00
|
|
|
dev_data.eps[ep_abs_idx].callback(message->code,
|
2019-05-05 13:18:40 +08:00
|
|
|
ep_packet_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_isr_handler(void)
|
|
|
|
{
|
2019-12-02 17:00:27 +01:00
|
|
|
USB_DeviceEhciIsrFunction(&dev_data);
|
2019-05-05 13:18:40 +08:00
|
|
|
}
|