Files
CherryUSB/core/usbd_core.c

1601 lines
55 KiB
C
Raw Normal View History

/*
* Copyright (c) 2022, sakumisu
2022-02-08 11:44:46 +08:00
*
* SPDX-License-Identifier: Apache-2.0
2022-02-08 11:44:46 +08:00
*/
#include "usbd_core.h"
#ifdef CONFIG_USBDEV_EP0_THREAD
#include "usb_osal.h"
#define USB_EP0_STATE_SETUP 0
#define USB_EP0_STATE_IN 1
#define USB_EP0_STATE_OUT 2
#endif
#undef USB_DBG_TAG
#define USB_DBG_TAG "usbd_core"
#include "usb_log.h"
2022-02-08 11:44:46 +08:00
/* general descriptor field offsets */
#define DESC_bLength 0 /** Length offset */
#define DESC_bDescriptorType 1 /** Descriptor type offset */
/* config descriptor field offsets */
#define CONF_DESC_wTotalLength 2 /** Total length offset */
#define CONF_DESC_bConfigurationValue 5 /** Configuration value offset */
#define CONF_DESC_bmAttributes 7 /** configuration characteristics */
/* interface descriptor field offsets */
#define INTF_DESC_bInterfaceNumber 2 /** Interface number offset */
#define INTF_DESC_bAlternateSetting 3 /** Alternate setting offset */
2022-12-11 21:19:48 +08:00
struct usbd_tx_rx_msg {
uint8_t ep;
2024-04-29 14:11:24 +08:00
uint8_t ep_mult;
2024-04-29 11:32:54 +08:00
uint16_t ep_mps;
2022-12-11 21:19:48 +08:00
uint32_t nbytes;
usbd_endpoint_callback cb;
};
USB_NOCACHE_RAM_SECTION struct usbd_core_priv {
2022-02-08 11:44:46 +08:00
/** Setup packet */
USB_MEM_ALIGNX struct usb_setup_packet setup;
2022-02-08 11:44:46 +08:00
/** Pointer to data buffer */
USB_MEM_ALIGNX uint8_t *ep0_data_buf;
2022-02-08 11:44:46 +08:00
/** Remaining bytes in buffer */
uint32_t ep0_data_buf_residue;
/** Total length of control transfer */
uint32_t ep0_data_buf_len;
/** Zero length packet flag of control transfer */
bool zlp_flag;
/** Pointer to registered descriptors */
2023-01-07 17:07:56 +08:00
#ifdef CONFIG_USBDEV_ADVANCE_DESC
const struct usb_descriptor *descriptors;
2022-11-18 22:43:32 +08:00
#else
2022-02-08 11:44:46 +08:00
const uint8_t *descriptors;
struct usb_msosv1_descriptor *msosv1_desc;
struct usb_msosv2_descriptor *msosv2_desc;
struct usb_bos_descriptor *bos_desc;
2024-07-25 21:39:36 +08:00
struct usb_webusb_descriptor *webusb_url_desc;
#endif
2022-02-08 11:44:46 +08:00
/* Buffer used for storing standard, class and vendor request data */
USB_MEM_ALIGNX uint8_t req_data[USB_ALIGN_UP(CONFIG_USBDEV_REQUEST_BUFFER_LEN, CONFIG_USB_ALIGN_SIZE)];
2022-02-08 11:44:46 +08:00
/** Currently selected configuration */
uint8_t configuration;
2024-07-26 22:01:12 +08:00
uint8_t device_address;
uint8_t ep0_next_state;
bool self_powered;
bool remote_wakeup_support;
bool remote_wakeup_enabled;
2024-07-26 22:01:12 +08:00
bool is_suspend;
#ifdef CONFIG_USBDEV_ADVANCE_DESC
2022-11-18 22:43:32 +08:00
uint8_t speed;
#endif
2022-08-18 21:48:27 +08:00
#ifdef CONFIG_USBDEV_TEST_MODE
bool test_req;
#endif
#ifdef CONFIG_USBDEV_EP0_THREAD
usb_osal_mq_t usbd_ep0_mq;
usb_osal_thread_t usbd_ep0_thread;
2022-08-18 21:48:27 +08:00
#endif
struct usbd_interface *intf[16];
uint8_t intf_altsetting[16];
2022-09-24 23:23:34 +08:00
uint8_t intf_offset;
2022-02-08 11:44:46 +08:00
struct usbd_tx_rx_msg tx_msg[16];
struct usbd_tx_rx_msg rx_msg[16];
2022-02-08 11:44:46 +08:00
void (*event_handler)(uint8_t busid, uint8_t event);
2024-02-06 19:51:50 +08:00
} g_usbd_core[CONFIG_USBDEV_MAX_BUS];
struct usbd_bus g_usbdev_bus[CONFIG_USBDEV_MAX_BUS];
2024-02-06 19:51:50 +08:00
static void usbd_class_event_notify_handler(uint8_t busid, uint8_t event, void *arg);
2022-08-17 20:12:37 +08:00
2022-02-08 11:44:46 +08:00
static void usbd_print_setup(struct usb_setup_packet *setup)
{
USB_LOG_ERR("Setup: "
"bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\r\n",
setup->bmRequestType,
setup->bRequest,
setup->wValue,
setup->wIndex,
setup->wLength);
2022-02-08 11:44:46 +08:00
}
#if (CONFIG_USB_DBG_LEVEL >= USB_DBG_LOG)
static const char *usb_ep0_state_string[] = {
"setup",
"indata",
"outdata",
"instatus",
"outstatus"
};
#endif
2024-02-06 19:51:50 +08:00
static bool is_device_configured(uint8_t busid)
2022-02-08 11:44:46 +08:00
{
2024-02-06 19:51:50 +08:00
return (g_usbd_core[busid].configuration != 0);
2022-02-08 11:44:46 +08:00
}
/**
* @brief configure and enable endpoint
*
* This function sets endpoint configuration according to one specified in USB
* endpoint descriptor and then enables it for data transfers.
*
2024-10-30 21:04:06 +08:00
* @param [in] busid busid
* @param [in] ep Endpoint descriptor byte array
2022-02-08 11:44:46 +08:00
*
* @return true if successfully configured and enabled
*/
2024-02-06 19:51:50 +08:00
static bool usbd_set_endpoint(uint8_t busid, const struct usb_endpoint_descriptor *ep)
2022-02-08 11:44:46 +08:00
{
USB_LOG_DBG("Open ep:0x%02x type:%u mps:%u\r\n",
ep->bEndpointAddress,
USB_GET_ENDPOINT_TYPE(ep->bmAttributes),
USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize));
2022-02-08 11:44:46 +08:00
2024-04-29 11:32:54 +08:00
if (ep->bEndpointAddress & 0x80) {
2024-04-29 14:11:24 +08:00
g_usbd_core[busid].tx_msg[ep->bEndpointAddress & 0x7f].ep_mps = USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize);
g_usbd_core[busid].tx_msg[ep->bEndpointAddress & 0x7f].ep_mult = USB_GET_MULT(ep->wMaxPacketSize);
2024-04-29 11:32:54 +08:00
} else {
2024-04-29 14:11:24 +08:00
g_usbd_core[busid].rx_msg[ep->bEndpointAddress & 0x7f].ep_mps = USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize);
g_usbd_core[busid].rx_msg[ep->bEndpointAddress & 0x7f].ep_mult = USB_GET_MULT(ep->wMaxPacketSize);
2024-04-29 11:32:54 +08:00
}
2024-02-06 19:51:50 +08:00
return usbd_ep_open(busid, ep) == 0 ? true : false;
2022-02-08 11:44:46 +08:00
}
/**
* @brief Disable endpoint for transferring data
*
* This function cancels transfers that are associated with endpoint and
* disabled endpoint itself.
*
2024-10-30 21:04:06 +08:00
* @param [in] busid busid
* @param [in] ep Endpoint descriptor byte array
2022-02-08 11:44:46 +08:00
*
* @return true if successfully deconfigured and disabled
*/
2024-02-06 19:51:50 +08:00
static bool usbd_reset_endpoint(uint8_t busid, const struct usb_endpoint_descriptor *ep)
2022-02-08 11:44:46 +08:00
{
USB_LOG_DBG("Close ep:0x%02x type:%u\r\n",
ep->bEndpointAddress,
USB_GET_ENDPOINT_TYPE(ep->bmAttributes));
2022-02-08 11:44:46 +08:00
2024-02-06 19:51:50 +08:00
return usbd_ep_close(busid, ep->bEndpointAddress) == 0 ? true : false;
2022-02-08 11:44:46 +08:00
}
/**
* @brief get specified USB descriptor
*
* This function parses the list of installed USB descriptors and attempts
* to find the specified USB descriptor.
*
2024-10-30 21:04:06 +08:00
* @param [in] busid busid
2022-02-08 11:44:46 +08:00
* @param [in] type_index Type and index of the descriptor
* @param [out] data Descriptor data
* @param [out] len Descriptor length
*
* @return true if the descriptor was found, false otherwise
*/
2023-01-07 17:07:56 +08:00
#ifdef CONFIG_USBDEV_ADVANCE_DESC
2024-02-06 19:51:50 +08:00
static bool usbd_get_descriptor(uint8_t busid, uint16_t type_index, uint8_t **data, uint32_t *len)
2022-11-18 22:43:32 +08:00
{
uint8_t type = 0U;
uint8_t index = 0U;
bool found = true;
uint32_t desc_len = 0;
const char *string = NULL;
const uint8_t *desc = NULL;
2022-11-18 22:43:32 +08:00
type = HI_BYTE(type_index);
index = LO_BYTE(type_index);
switch (type) {
case USB_DESCRIPTOR_TYPE_DEVICE:
g_usbd_core[busid].speed = usbd_get_port_speed(busid); /* before we get device descriptor, we have known steady port speed */
if (g_usbd_core[busid].descriptors->device_descriptor_callback == NULL) {
found = false;
break;
}
2024-02-06 19:51:50 +08:00
desc = g_usbd_core[busid].descriptors->device_descriptor_callback(g_usbd_core[busid].speed);
if (desc == NULL) {
found = false;
break;
}
desc_len = desc[0];
2022-11-18 22:43:32 +08:00
break;
case USB_DESCRIPTOR_TYPE_CONFIGURATION:
if (g_usbd_core[busid].descriptors->config_descriptor_callback == NULL) {
found = false;
break;
}
2024-02-06 19:51:50 +08:00
desc = g_usbd_core[busid].descriptors->config_descriptor_callback(g_usbd_core[busid].speed);
if (desc == NULL) {
found = false;
break;
2022-11-18 22:43:32 +08:00
}
desc_len = ((desc[CONF_DESC_wTotalLength]) | (desc[CONF_DESC_wTotalLength + 1] << 8));
g_usbd_core[busid].self_powered = (desc[7] & USB_CONFIG_POWERED_MASK) ? true : false;
g_usbd_core[busid].remote_wakeup_support = (desc[7] & USB_CONFIG_REMOTE_WAKEUP) ? true : false;
2022-11-18 22:43:32 +08:00
break;
case USB_DESCRIPTOR_TYPE_STRING:
if (index == USB_OSDESC_STRING_DESC_INDEX) {
2024-02-06 19:51:50 +08:00
if (!g_usbd_core[busid].descriptors->msosv1_descriptor) {
found = false;
break;
2022-11-18 22:43:32 +08:00
}
2024-02-06 19:51:50 +08:00
desc = (uint8_t *)g_usbd_core[busid].descriptors->msosv1_descriptor->string;
desc_len = g_usbd_core[busid].descriptors->msosv1_descriptor->string[0];
} else {
if (g_usbd_core[busid].descriptors->string_descriptor_callback == NULL) {
found = false;
break;
}
2024-05-17 21:56:56 +08:00
string = g_usbd_core[busid].descriptors->string_descriptor_callback(g_usbd_core[busid].speed, index);
if (string == NULL) {
found = false;
break;
}
2024-05-17 21:56:56 +08:00
if (index == USB_STRING_LANGID_INDEX) {
(*data)[0] = 4;
(*data)[1] = USB_DESCRIPTOR_TYPE_STRING;
2024-05-17 21:56:56 +08:00
(*data)[2] = string[0];
(*data)[3] = string[1];
*len = 4;
return true;
}
uint16_t str_size = strlen(string);
uint16_t total_size = 2 * str_size + 2;
if (total_size > CONFIG_USBDEV_REQUEST_BUFFER_LEN) {
USB_LOG_ERR("string size overflow\r\n");
return false;
}
(*data)[0] = total_size;
(*data)[1] = USB_DESCRIPTOR_TYPE_STRING;
for (uint16_t i = 0; i < str_size; i++) {
(*data)[2 * i + 2] = string[i];
(*data)[2 * i + 3] = 0x00;
}
*len = total_size;
return true;
2022-11-18 22:43:32 +08:00
}
break;
case USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER:
#ifndef CONFIG_USB_HS
return false;
#else
if (g_usbd_core[busid].descriptors->device_quality_descriptor_callback == NULL) {
found = false;
break;
}
2024-02-06 19:51:50 +08:00
desc = g_usbd_core[busid].descriptors->device_quality_descriptor_callback(g_usbd_core[busid].speed);
if (desc == NULL) {
2022-11-18 22:43:32 +08:00
found = false;
break;
2022-11-18 22:43:32 +08:00
}
desc_len = desc[0];
2022-11-18 22:43:32 +08:00
break;
#endif
2022-11-18 22:43:32 +08:00
case USB_DESCRIPTOR_TYPE_OTHER_SPEED:
if (g_usbd_core[busid].descriptors->other_speed_descriptor_callback == NULL) {
found = false;
break;
}
2024-02-06 19:51:50 +08:00
desc = g_usbd_core[busid].descriptors->other_speed_descriptor_callback(g_usbd_core[busid].speed);
if (desc == NULL) {
found = false;
break;
2022-11-18 22:43:32 +08:00
}
desc_len = ((desc[CONF_DESC_wTotalLength]) | (desc[CONF_DESC_wTotalLength + 1] << 8));
2022-11-18 22:43:32 +08:00
break;
case USB_DESCRIPTOR_TYPE_BINARY_OBJECT_STORE:
2024-02-06 19:51:50 +08:00
if (!g_usbd_core[busid].descriptors->bos_descriptor) {
found = false;
break;
}
2024-02-06 19:51:50 +08:00
desc = (uint8_t *)g_usbd_core[busid].descriptors->bos_descriptor->string;
desc_len = g_usbd_core[busid].descriptors->bos_descriptor->string_len;
2022-11-18 22:43:32 +08:00
break;
default:
found = false;
break;
}
if (found == false) {
/* nothing found */
USB_LOG_ERR("descriptor <type:%x,index:%x> not found!\r\n", type, index);
} else {
*data = (uint8_t *)desc;
//memcpy(*data, desc, desc_len);
*len = desc_len;
2022-11-18 22:43:32 +08:00
}
return found;
}
#else
2024-02-06 19:51:50 +08:00
static bool usbd_get_descriptor(uint8_t busid, uint16_t type_index, uint8_t **data, uint32_t *len)
2022-02-08 11:44:46 +08:00
{
uint8_t type = 0U;
uint8_t index = 0U;
uint8_t *p = NULL;
uint32_t cur_index = 0U;
bool found = false;
2022-08-17 20:12:37 +08:00
type = HI_BYTE(type_index);
index = LO_BYTE(type_index);
2022-02-08 11:44:46 +08:00
if ((type == USB_DESCRIPTOR_TYPE_STRING) && (index == USB_OSDESC_STRING_DESC_INDEX)) {
2024-02-06 19:51:50 +08:00
if (!g_usbd_core[busid].msosv1_desc) {
2022-02-08 11:44:46 +08:00
return false;
}
*data = (uint8_t *)g_usbd_core[busid].msosv1_desc->string;
//memcpy(*data, (uint8_t *)g_usbd_core[busid].msosv1_desc->string, g_usbd_core[busid].msosv1_desc->string[0]);
2024-02-06 19:51:50 +08:00
*len = g_usbd_core[busid].msosv1_desc->string[0];
2022-02-08 11:44:46 +08:00
return true;
} else if (type == USB_DESCRIPTOR_TYPE_BINARY_OBJECT_STORE) {
2024-02-06 19:51:50 +08:00
if (!g_usbd_core[busid].bos_desc) {
2022-02-08 11:44:46 +08:00
return false;
}
2024-05-20 19:13:11 +08:00
*data = (uint8_t *)g_usbd_core[busid].bos_desc->string;
//memcpy(*data, (uint8_t *)g_usbd_core[busid].bos_desc->string, g_usbd_core[busid].bos_desc->string_len);
2024-02-06 19:51:50 +08:00
*len = g_usbd_core[busid].bos_desc->string_len;
2022-02-08 11:44:46 +08:00
return true;
}
/*
* Invalid types of descriptors,
* see USB Spec. Revision 2.0, 9.4.3 Get Descriptor
*/
else if ((type == USB_DESCRIPTOR_TYPE_INTERFACE) || (type == USB_DESCRIPTOR_TYPE_ENDPOINT) ||
#ifndef CONFIG_USB_HS
(type > USB_DESCRIPTOR_TYPE_ENDPOINT)) {
#else
(type > USB_DESCRIPTOR_TYPE_OTHER_SPEED)) {
#endif
return false;
}
2024-02-06 19:51:50 +08:00
p = (uint8_t *)g_usbd_core[busid].descriptors;
2022-02-08 11:44:46 +08:00
cur_index = 0U;
while (p[DESC_bLength] != 0U) {
if (p[DESC_bDescriptorType] == type) {
if (cur_index == index) {
found = true;
break;
}
cur_index++;
}
/* skip to next descriptor */
p += p[DESC_bLength];
}
if (found) {
2022-06-25 22:46:47 +08:00
if ((type == USB_DESCRIPTOR_TYPE_CONFIGURATION) || ((type == USB_DESCRIPTOR_TYPE_OTHER_SPEED))) {
2022-08-17 20:12:37 +08:00
/* configuration or other speed descriptor is an
* exception, length is at offset 2 and 3
2022-02-08 11:44:46 +08:00
*/
*len = (p[CONF_DESC_wTotalLength]) |
(p[CONF_DESC_wTotalLength + 1] << 8);
g_usbd_core[busid].self_powered = (p[7] & USB_CONFIG_POWERED_MASK) ? true : false;
g_usbd_core[busid].remote_wakeup_support = (p[7] & USB_CONFIG_REMOTE_WAKEUP) ? true : false;
2022-02-08 11:44:46 +08:00
} else {
/* normally length is at offset 0 */
*len = p[DESC_bLength];
}
*data = p;
//memcpy(*data, p, *len);
2022-02-08 11:44:46 +08:00
} else {
/* nothing found */
USB_LOG_ERR("descriptor <type:0x%02x,index:0x%02x> not found!\r\n", type, index);
2022-02-08 11:44:46 +08:00
}
return found;
}
2022-11-18 22:43:32 +08:00
#endif
2022-02-08 11:44:46 +08:00
/**
* @brief set USB configuration
*
* This function configures the device according to the specified configuration
* index and alternate setting by parsing the installed USB descriptor list.
* A configuration index of 0 unconfigures the device.
*
2024-10-30 21:04:06 +08:00
* @param [in] busid busid
2022-02-08 11:44:46 +08:00
* @param [in] config_index Configuration index
* @param [in] alt_setting Alternate setting number
*
* @return true if successfully configured false if error or unconfigured
*/
2024-02-06 19:51:50 +08:00
static bool usbd_set_configuration(uint8_t busid, uint8_t config_index, uint8_t alt_setting)
2022-02-08 11:44:46 +08:00
{
uint8_t cur_alt_setting = 0xFF;
uint8_t cur_config = 0xFF;
bool found = false;
const uint8_t *p;
uint32_t desc_len = 0;
uint32_t current_desc_len = 0;
2023-01-07 17:07:56 +08:00
#ifdef CONFIG_USBDEV_ADVANCE_DESC
2024-02-06 19:51:50 +08:00
p = g_usbd_core[busid].descriptors->config_descriptor_callback(g_usbd_core[busid].speed);
2022-11-18 22:43:32 +08:00
#else
2024-02-06 19:51:50 +08:00
p = (uint8_t *)g_usbd_core[busid].descriptors;
2022-11-18 22:43:32 +08:00
#endif
2022-02-08 11:44:46 +08:00
/* configure endpoints for this configuration/altsetting */
while (p[DESC_bLength] != 0U) {
switch (p[DESC_bDescriptorType]) {
case USB_DESCRIPTOR_TYPE_CONFIGURATION:
/* remember current configuration index */
cur_config = p[CONF_DESC_bConfigurationValue];
if (cur_config == config_index) {
found = true;
current_desc_len = 0;
desc_len = (p[CONF_DESC_wTotalLength]) |
(p[CONF_DESC_wTotalLength + 1] << 8);
2022-02-08 11:44:46 +08:00
}
break;
case USB_DESCRIPTOR_TYPE_INTERFACE:
/* remember current alternate setting */
cur_alt_setting =
p[INTF_DESC_bAlternateSetting];
break;
case USB_DESCRIPTOR_TYPE_ENDPOINT:
if ((cur_config != config_index) ||
(cur_alt_setting != alt_setting)) {
break;
}
2024-02-06 19:51:50 +08:00
found = usbd_set_endpoint(busid, (struct usb_endpoint_descriptor *)p);
2022-02-08 11:44:46 +08:00
break;
default:
break;
}
/* skip to next descriptor */
current_desc_len += p[DESC_bLength];
p += p[DESC_bLength];
if (current_desc_len >= desc_len && desc_len) {
break;
}
2022-02-08 11:44:46 +08:00
}
return found;
}
/**
* @brief set USB interface
*
2024-10-30 21:04:06 +08:00
* @param [in] busid busid
2022-02-08 11:44:46 +08:00
* @param [in] iface Interface index
* @param [in] alt_setting Alternate setting number
*
* @return true if successfully configured false if error or unconfigured
*/
2024-02-06 19:51:50 +08:00
static bool usbd_set_interface(uint8_t busid, uint8_t iface, uint8_t alt_setting)
2022-02-08 11:44:46 +08:00
{
const uint8_t *if_desc = NULL;
struct usb_endpoint_descriptor *ep_desc;
uint8_t cur_alt_setting = 0xFF;
uint8_t cur_iface = 0xFF;
bool ret = false;
const uint8_t *p;
uint32_t desc_len = 0;
uint32_t current_desc_len = 0;
2023-01-07 17:07:56 +08:00
#ifdef CONFIG_USBDEV_ADVANCE_DESC
2024-02-06 19:51:50 +08:00
p = g_usbd_core[busid].descriptors->config_descriptor_callback(g_usbd_core[busid].speed);
2022-11-18 22:43:32 +08:00
#else
2024-02-06 19:51:50 +08:00
p = (uint8_t *)g_usbd_core[busid].descriptors;
2022-11-18 22:43:32 +08:00
#endif
2022-02-08 11:44:46 +08:00
USB_LOG_DBG("iface %u alt_setting %u\r\n", iface, alt_setting);
while (p[DESC_bLength] != 0U) {
switch (p[DESC_bDescriptorType]) {
case USB_DESCRIPTOR_TYPE_CONFIGURATION:
current_desc_len = 0;
desc_len = (p[CONF_DESC_wTotalLength]) |
(p[CONF_DESC_wTotalLength + 1] << 8);
break;
2022-02-08 11:44:46 +08:00
case USB_DESCRIPTOR_TYPE_INTERFACE:
/* remember current alternate setting */
cur_alt_setting = p[INTF_DESC_bAlternateSetting];
cur_iface = p[INTF_DESC_bInterfaceNumber];
if (cur_iface == iface &&
cur_alt_setting == alt_setting) {
if_desc = (void *)p;
}
break;
case USB_DESCRIPTOR_TYPE_ENDPOINT:
if (cur_iface == iface) {
ep_desc = (struct usb_endpoint_descriptor *)p;
if (alt_setting == 0) {
2024-02-06 19:51:50 +08:00
ret = usbd_reset_endpoint(busid, ep_desc);
} else if (cur_alt_setting == alt_setting) {
2024-02-06 19:51:50 +08:00
ret = usbd_set_endpoint(busid, ep_desc);
} else {
2022-02-08 11:44:46 +08:00
}
}
break;
default:
break;
}
/* skip to next descriptor */
current_desc_len += p[DESC_bLength];
p += p[DESC_bLength];
if (current_desc_len >= desc_len && desc_len) {
break;
}
2022-02-08 11:44:46 +08:00
}
2024-02-06 19:51:50 +08:00
usbd_class_event_notify_handler(busid, USBD_EVENT_SET_INTERFACE, (void *)if_desc);
2022-02-08 11:44:46 +08:00
return ret;
}
/**
* @brief handle a standard device request
*
2024-10-30 21:04:06 +08:00
* @param [in] busid busid
2022-02-08 11:44:46 +08:00
* @param [in] setup The setup packet
* @param [in,out] data Data buffer
* @param [in,out] len Pointer to data length
*
* @return true if the request was handled successfully
*/
2024-02-06 19:51:50 +08:00
static bool usbd_std_device_req_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
2022-02-08 11:44:46 +08:00
{
uint16_t value = setup->wValue;
bool ret = true;
switch (setup->bRequest) {
case USB_REQUEST_GET_STATUS:
/* bit 0: self-powered */
/* bit 1: remote wakeup */
2022-08-17 20:12:37 +08:00
(*data)[0] = 0x00;
if (g_usbd_core[busid].self_powered) {
(*data)[0] |= USB_GETSTATUS_SELF_POWERED;
}
if (g_usbd_core[busid].remote_wakeup_enabled) {
(*data)[0] |= USB_GETSTATUS_REMOTE_WAKEUP;
}
2022-08-17 20:12:37 +08:00
(*data)[1] = 0x00;
2022-02-08 11:44:46 +08:00
*len = 2;
break;
case USB_REQUEST_CLEAR_FEATURE:
case USB_REQUEST_SET_FEATURE:
if (value == USB_FEATURE_REMOTE_WAKEUP) {
2023-05-17 20:59:38 +08:00
if (setup->bRequest == USB_REQUEST_SET_FEATURE) {
g_usbd_core[busid].remote_wakeup_enabled = true;
g_usbd_core[busid].event_handler(busid, USBD_EVENT_SET_REMOTE_WAKEUP);
2023-05-17 20:59:38 +08:00
} else {
g_usbd_core[busid].remote_wakeup_enabled = false;
g_usbd_core[busid].event_handler(busid, USBD_EVENT_CLR_REMOTE_WAKEUP);
2023-05-17 20:59:38 +08:00
}
2022-08-18 21:48:27 +08:00
} else if (value == USB_FEATURE_TEST_MODE) {
#ifdef CONFIG_USBDEV_TEST_MODE
g_usbd_core[busid].test_req = true;
2022-08-18 21:48:27 +08:00
#endif
2022-02-08 11:44:46 +08:00
}
2022-08-17 20:12:37 +08:00
*len = 0;
2022-02-08 11:44:46 +08:00
break;
case USB_REQUEST_SET_ADDRESS:
2024-07-26 22:01:12 +08:00
g_usbd_core[busid].device_address = value;
2024-02-06 19:51:50 +08:00
usbd_set_address(busid, value);
2022-08-17 20:12:37 +08:00
*len = 0;
2022-02-08 11:44:46 +08:00
break;
case USB_REQUEST_GET_DESCRIPTOR:
2024-02-06 19:51:50 +08:00
ret = usbd_get_descriptor(busid, value, data, len);
2022-02-08 11:44:46 +08:00
break;
case USB_REQUEST_SET_DESCRIPTOR:
ret = false;
break;
case USB_REQUEST_GET_CONFIGURATION:
(*data)[0] = g_usbd_core[busid].configuration;
2022-02-08 11:44:46 +08:00
*len = 1;
break;
case USB_REQUEST_SET_CONFIGURATION:
value &= 0xFF;
if (value == 0) {
g_usbd_core[busid].configuration = 0;
} else if (!usbd_set_configuration(busid, value, 0)) {
2022-02-08 11:44:46 +08:00
ret = false;
} else {
2024-02-06 19:51:50 +08:00
g_usbd_core[busid].configuration = value;
2024-07-26 22:01:12 +08:00
g_usbd_core[busid].is_suspend = false;
2024-02-06 19:51:50 +08:00
usbd_class_event_notify_handler(busid, USBD_EVENT_CONFIGURED, NULL);
g_usbd_core[busid].event_handler(busid, USBD_EVENT_CONFIGURED);
2022-02-08 11:44:46 +08:00
}
2022-08-17 20:12:37 +08:00
*len = 0;
2022-02-08 11:44:46 +08:00
break;
case USB_REQUEST_GET_INTERFACE:
case USB_REQUEST_SET_INTERFACE:
2022-08-17 20:12:37 +08:00
ret = false;
2022-02-08 11:44:46 +08:00
break;
default:
ret = false;
break;
}
return ret;
}
/**
* @brief handle a standard interface request
*
2024-10-30 21:04:06 +08:00
* @param [in] busid busid
2022-02-08 11:44:46 +08:00
* @param [in] setup The setup packet
* @param [in,out] data Data buffer
* @param [in,out] len Pointer to data length
*
* @return true if the request was handled successfully
*/
2024-02-06 19:51:50 +08:00
static bool usbd_std_interface_req_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
2022-02-08 11:44:46 +08:00
{
uint8_t type = HI_BYTE(setup->wValue);
uint8_t intf_num = LO_BYTE(setup->wIndex);
2022-08-17 20:12:37 +08:00
bool ret = true;
const uint8_t *p;
uint32_t desc_len = 0;
uint32_t current_desc_len = 0;
uint8_t cur_iface = 0xFF;
#ifdef CONFIG_USBDEV_ADVANCE_DESC
p = g_usbd_core[busid].descriptors->config_descriptor_callback(g_usbd_core[busid].speed);
#else
p = (uint8_t *)g_usbd_core[busid].descriptors;
#endif
2022-08-17 20:12:37 +08:00
/* Only when device is configured, then interface requests can be valid. */
2024-02-06 19:51:50 +08:00
if (!is_device_configured(busid)) {
2022-02-08 11:44:46 +08:00
return false;
}
switch (setup->bRequest) {
case USB_REQUEST_GET_STATUS:
2022-08-17 20:12:37 +08:00
(*data)[0] = 0x00;
(*data)[1] = 0x00;
2022-02-08 11:44:46 +08:00
*len = 2;
break;
case USB_REQUEST_GET_DESCRIPTOR:
if (type == 0x21) { /* HID_DESCRIPTOR_TYPE_HID */
while (p[DESC_bLength] != 0U) {
switch (p[DESC_bDescriptorType]) {
case USB_DESCRIPTOR_TYPE_CONFIGURATION:
current_desc_len = 0;
desc_len = (p[CONF_DESC_wTotalLength]) |
(p[CONF_DESC_wTotalLength + 1] << 8);
break;
case USB_DESCRIPTOR_TYPE_INTERFACE:
cur_iface = p[INTF_DESC_bInterfaceNumber];
break;
case 0x21:
if (cur_iface == intf_num) {
*data = (uint8_t *)p;
//memcpy(*data, p, p[DESC_bLength]);
*len = p[DESC_bLength];
return true;
}
break;
default:
break;
}
/* skip to next descriptor */
current_desc_len += p[DESC_bLength];
p += p[DESC_bLength];
if (current_desc_len >= desc_len && desc_len) {
break;
}
}
} else if (type == 0x22) { /* HID_DESCRIPTOR_TYPE_HID_REPORT */
2024-02-06 19:51:50 +08:00
for (uint8_t i = 0; i < g_usbd_core[busid].intf_offset; i++) {
struct usbd_interface *intf = g_usbd_core[busid].intf[i];
if (intf && (intf->intf_num == intf_num)) {
*data = (uint8_t *)intf->hid_report_descriptor;
//memcpy(*data, intf->hid_report_descriptor, intf->hid_report_descriptor_len);
*len = intf->hid_report_descriptor_len;
return true;
}
}
}
ret = false;
break;
2022-02-08 11:44:46 +08:00
case USB_REQUEST_CLEAR_FEATURE:
case USB_REQUEST_SET_FEATURE:
2022-08-17 20:12:37 +08:00
ret = false;
break;
2022-02-08 11:44:46 +08:00
case USB_REQUEST_GET_INTERFACE:
(*data)[0] = g_usbd_core[busid].intf_altsetting[intf_num];
2022-02-08 11:44:46 +08:00
*len = 1;
break;
case USB_REQUEST_SET_INTERFACE:
g_usbd_core[busid].intf_altsetting[intf_num] = LO_BYTE(setup->wValue);
2024-02-06 19:51:50 +08:00
usbd_set_interface(busid, setup->wIndex, setup->wValue);
2022-08-17 20:12:37 +08:00
*len = 0;
2022-02-08 11:44:46 +08:00
break;
default:
2022-08-17 20:12:37 +08:00
ret = false;
break;
2022-02-08 11:44:46 +08:00
}
2022-08-17 20:12:37 +08:00
return ret;
2022-02-08 11:44:46 +08:00
}
/**
* @brief handle a standard endpoint request
*
2024-10-30 21:04:06 +08:00
* @param [in] busid busid
2022-02-08 11:44:46 +08:00
* @param [in] setup The setup packet
* @param [in,out] data Data buffer
* @param [in,out] len Pointer to data length
*
* @return true if the request was handled successfully
*/
2024-02-06 19:51:50 +08:00
static bool usbd_std_endpoint_req_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
2022-02-08 11:44:46 +08:00
{
uint8_t ep = (uint8_t)setup->wIndex;
2022-08-17 20:12:37 +08:00
bool ret = true;
uint8_t stalled;
2022-08-17 20:12:37 +08:00
/* Only when device is configured, then endpoint requests can be valid. */
2024-02-06 19:51:50 +08:00
if (!is_device_configured(busid)) {
2022-08-17 20:12:37 +08:00
return false;
}
2022-02-08 11:44:46 +08:00
switch (setup->bRequest) {
case USB_REQUEST_GET_STATUS:
usbd_ep_is_stalled(busid, ep, &stalled);
if (stalled) {
(*data)[0] = 0x01;
} else {
(*data)[0] = 0x00;
}
2022-08-17 20:12:37 +08:00
(*data)[1] = 0x00;
*len = 2;
break;
2022-02-08 11:44:46 +08:00
case USB_REQUEST_CLEAR_FEATURE:
if (setup->wValue == USB_FEATURE_ENDPOINT_HALT) {
2022-08-17 20:12:37 +08:00
USB_LOG_ERR("ep:%02x clear halt\r\n", ep);
2022-02-08 11:44:46 +08:00
2024-02-06 19:51:50 +08:00
usbd_ep_clear_stall(busid, ep);
2022-08-17 20:12:37 +08:00
break;
} else {
ret = false;
}
*len = 0;
break;
2022-02-08 11:44:46 +08:00
case USB_REQUEST_SET_FEATURE:
if (setup->wValue == USB_FEATURE_ENDPOINT_HALT) {
2022-08-17 20:12:37 +08:00
USB_LOG_ERR("ep:%02x set halt\r\n", ep);
2022-02-08 11:44:46 +08:00
2024-02-06 19:51:50 +08:00
usbd_ep_set_stall(busid, ep);
2022-08-17 20:12:37 +08:00
} else {
ret = false;
}
*len = 0;
break;
2022-02-08 11:44:46 +08:00
case USB_REQUEST_SYNCH_FRAME:
2022-08-17 20:12:37 +08:00
ret = false;
break;
2022-02-08 11:44:46 +08:00
default:
2022-08-17 20:12:37 +08:00
ret = false;
break;
2022-02-08 11:44:46 +08:00
}
2022-08-17 20:12:37 +08:00
return ret;
2022-02-08 11:44:46 +08:00
}
/**
2022-08-17 20:12:37 +08:00
* @brief handle standard requests (list in chapter 9)
2022-02-08 11:44:46 +08:00
*
2024-10-30 21:04:06 +08:00
* @param [in] busid busid
2022-02-08 11:44:46 +08:00
* @param [in] setup The setup packet
* @param [in,out] data Data buffer
* @param [in,out] len Pointer to data length
*
* @return true if the request was handled successfully
*/
2024-02-06 19:51:50 +08:00
static int usbd_standard_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
2022-02-08 11:44:46 +08:00
{
int rc = 0;
switch (setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) {
case USB_REQUEST_RECIPIENT_DEVICE:
2024-02-06 19:51:50 +08:00
if (usbd_std_device_req_handler(busid, setup, data, len) == false) {
2022-02-08 11:44:46 +08:00
rc = -1;
}
break;
case USB_REQUEST_RECIPIENT_INTERFACE:
2024-02-06 19:51:50 +08:00
if (usbd_std_interface_req_handler(busid, setup, data, len) == false) {
2022-02-08 11:44:46 +08:00
rc = -1;
}
break;
case USB_REQUEST_RECIPIENT_ENDPOINT:
2024-02-06 19:51:50 +08:00
if (usbd_std_endpoint_req_handler(busid, setup, data, len) == false) {
2022-02-08 11:44:46 +08:00
rc = -1;
}
break;
default:
rc = -1;
2022-08-17 20:12:37 +08:00
break;
2022-02-08 11:44:46 +08:00
}
return rc;
}
/**
* @brief handler for class requests
*
2024-10-30 21:04:06 +08:00
* @param [in] busid busid
2022-02-08 11:44:46 +08:00
* @param [in] setup The setup packet
* @param [in,out] data Data buffer
* @param [in,out] len Pointer to data length
*
* @return true if the request was handled successfully
*/
2024-02-06 19:51:50 +08:00
static int usbd_class_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
2022-02-08 11:44:46 +08:00
{
2022-04-08 15:07:32 +08:00
if ((setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) == USB_REQUEST_RECIPIENT_INTERFACE) {
2024-02-06 19:51:50 +08:00
for (uint8_t i = 0; i < g_usbd_core[busid].intf_offset; i++) {
struct usbd_interface *intf = g_usbd_core[busid].intf[i];
2022-04-08 15:07:32 +08:00
if (intf && intf->class_interface_handler && (intf->intf_num == (setup->wIndex & 0xFF))) {
2024-02-06 19:51:50 +08:00
return intf->class_interface_handler(busid, setup, data, len);
2022-04-08 15:07:32 +08:00
}
}
} else if ((setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) == USB_REQUEST_RECIPIENT_ENDPOINT) {
2024-02-06 19:51:50 +08:00
for (uint8_t i = 0; i < g_usbd_core[busid].intf_offset; i++) {
struct usbd_interface *intf = g_usbd_core[busid].intf[i];
2022-04-08 15:07:32 +08:00
if (intf && intf->class_endpoint_handler) {
2024-02-06 19:51:50 +08:00
return intf->class_endpoint_handler(busid, setup, data, len);
2022-02-08 11:44:46 +08:00
}
}
}
return -1;
}
/**
* @brief handler for vendor requests
*
2024-10-30 21:04:06 +08:00
* @param [in] busid busid
2022-02-08 11:44:46 +08:00
* @param [in] setup The setup packet
* @param [in,out] data Data buffer
* @param [in,out] len Pointer to data length
*
* @return true if the request was handled successfully
*/
2024-02-06 19:51:50 +08:00
static int usbd_vendor_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
2022-02-08 11:44:46 +08:00
{
2023-06-28 22:11:14 +08:00
uint32_t desclen;
2023-01-07 17:07:56 +08:00
#ifdef CONFIG_USBDEV_ADVANCE_DESC
2024-02-06 19:51:50 +08:00
if (g_usbd_core[busid].descriptors->msosv1_descriptor) {
if (setup->bRequest == g_usbd_core[busid].descriptors->msosv1_descriptor->vendor_code) {
2022-11-18 22:43:32 +08:00
switch (setup->wIndex) {
case 0x04:
2024-02-06 19:51:50 +08:00
desclen = g_usbd_core[busid].descriptors->msosv1_descriptor->compat_id[0] +
(g_usbd_core[busid].descriptors->msosv1_descriptor->compat_id[1] << 8) +
(g_usbd_core[busid].descriptors->msosv1_descriptor->compat_id[2] << 16) +
(g_usbd_core[busid].descriptors->msosv1_descriptor->compat_id[3] << 24);
2023-06-28 22:11:14 +08:00
*data = (uint8_t *)g_usbd_core[busid].descriptors->msosv1_descriptor->compat_id;
//memcpy(*data, g_usbd_core[busid].descriptors->msosv1_descriptor->compat_id, desclen);
2023-06-28 22:11:14 +08:00
*len = desclen;
2022-11-18 22:43:32 +08:00
return 0;
case 0x05:
2024-02-06 19:51:50 +08:00
desclen = g_usbd_core[busid].descriptors->msosv1_descriptor->comp_id_property[setup->wValue][0] +
(g_usbd_core[busid].descriptors->msosv1_descriptor->comp_id_property[setup->wValue][1] << 8) +
(g_usbd_core[busid].descriptors->msosv1_descriptor->comp_id_property[setup->wValue][2] << 16) +
(g_usbd_core[busid].descriptors->msosv1_descriptor->comp_id_property[setup->wValue][3] << 24);
2023-06-28 22:11:14 +08:00
*data = (uint8_t *)g_usbd_core[busid].descriptors->msosv1_descriptor->comp_id_property[setup->wValue];
//memcpy(*data, g_usbd_core[busid].descriptors->msosv1_descriptor->comp_id_property[setup->wValue], desclen);
2023-06-28 22:11:14 +08:00
*len = desclen;
2022-11-18 22:43:32 +08:00
return 0;
default:
return -1;
}
}
2024-02-06 19:51:50 +08:00
} else if (g_usbd_core[busid].descriptors->msosv2_descriptor) {
if (setup->bRequest == g_usbd_core[busid].descriptors->msosv2_descriptor->vendor_code) {
2022-11-18 22:43:32 +08:00
switch (setup->wIndex) {
case WINUSB_REQUEST_GET_DESCRIPTOR_SET:
2024-02-06 19:51:50 +08:00
desclen = g_usbd_core[busid].descriptors->msosv2_descriptor->compat_id_len;
*data = (uint8_t *)g_usbd_core[busid].descriptors->msosv2_descriptor->compat_id;
//memcpy(*data, g_usbd_core[busid].descriptors->msosv2_descriptor->compat_id, desclen);
2024-02-06 19:51:50 +08:00
*len = g_usbd_core[busid].descriptors->msosv2_descriptor->compat_id_len;
2022-11-18 22:43:32 +08:00
return 0;
default:
return -1;
}
}
2024-07-25 21:39:36 +08:00
}
if (g_usbd_core[busid].descriptors->webusb_url_descriptor) {
2024-02-06 19:51:50 +08:00
if (setup->bRequest == g_usbd_core[busid].descriptors->webusb_url_descriptor->vendor_code) {
2023-01-29 16:15:28 +08:00
switch (setup->wIndex) {
2024-07-25 21:39:36 +08:00
case WEBUSB_REQUEST_GET_URL:
2024-02-06 19:51:50 +08:00
desclen = g_usbd_core[busid].descriptors->webusb_url_descriptor->string_len;
*data = (uint8_t *)g_usbd_core[busid].descriptors->webusb_url_descriptor->string;
//memcpy(*data, g_usbd_core[busid].descriptors->webusb_url_descriptor->string, desclen);
*len = desclen;
2023-01-29 16:15:28 +08:00
return 0;
default:
return -1;
}
}
2022-11-18 22:43:32 +08:00
}
#else
2024-02-06 19:51:50 +08:00
if (g_usbd_core[busid].msosv1_desc) {
if (setup->bRequest == g_usbd_core[busid].msosv1_desc->vendor_code) {
2022-02-08 11:44:46 +08:00
switch (setup->wIndex) {
case 0x04:
*data = (uint8_t *)g_usbd_core[busid].msosv1_desc->compat_id;
2024-02-06 19:51:50 +08:00
desclen = g_usbd_core[busid].msosv1_desc->compat_id[0] +
(g_usbd_core[busid].msosv1_desc->compat_id[1] << 8) +
(g_usbd_core[busid].msosv1_desc->compat_id[2] << 16) +
(g_usbd_core[busid].msosv1_desc->compat_id[3] << 24);
//memcpy(*data, g_usbd_core[busid].msosv1_desc->compat_id, desclen);
2023-06-28 22:11:14 +08:00
*len = desclen;
2022-02-08 11:44:46 +08:00
return 0;
case 0x05:
*data = (uint8_t *)g_usbd_core[busid].msosv1_desc->comp_id_property[setup->wValue];
2024-02-06 19:51:50 +08:00
desclen = g_usbd_core[busid].msosv1_desc->comp_id_property[setup->wValue][0] +
(g_usbd_core[busid].msosv1_desc->comp_id_property[setup->wValue][1] << 8) +
(g_usbd_core[busid].msosv1_desc->comp_id_property[setup->wValue][2] << 16) +
(g_usbd_core[busid].msosv1_desc->comp_id_property[setup->wValue][3] << 24);
//memcpy(*data, g_usbd_core[busid].msosv1_desc->comp_id_property[setup->wValue], desclen);
2023-06-28 22:11:14 +08:00
*len = desclen;
2022-02-08 11:44:46 +08:00
return 0;
default:
return -1;
}
}
2024-02-06 19:51:50 +08:00
} else if (g_usbd_core[busid].msosv2_desc) {
if (setup->bRequest == g_usbd_core[busid].msosv2_desc->vendor_code) {
2022-02-08 11:44:46 +08:00
switch (setup->wIndex) {
case WINUSB_REQUEST_GET_DESCRIPTOR_SET:
*data = (uint8_t *)g_usbd_core[busid].msosv2_desc->compat_id;
//memcpy(*data, g_usbd_core[busid].msosv2_desc->compat_id, g_usbd_core[busid].msosv2_desc->compat_id_len);
2024-02-06 19:51:50 +08:00
*len = g_usbd_core[busid].msosv2_desc->compat_id_len;
2022-02-08 11:44:46 +08:00
return 0;
default:
return -1;
}
}
}
2024-07-25 21:39:36 +08:00
if (g_usbd_core[busid].webusb_url_desc) {
if (setup->bRequest == g_usbd_core[busid].webusb_url_desc->vendor_code) {
switch (setup->wIndex) {
case WEBUSB_REQUEST_GET_URL:
desclen = g_usbd_core[busid].webusb_url_desc->string_len;
*data = (uint8_t *)g_usbd_core[busid].webusb_url_desc->string;
//memcpy(*data, g_usbd_core[busid].webusb_url_desc->string, desclen);
*len = desclen;
return 0;
default:
return -1;
}
}
}
2022-11-18 22:43:32 +08:00
#endif
2024-02-06 19:51:50 +08:00
for (uint8_t i = 0; i < g_usbd_core[busid].intf_offset; i++) {
struct usbd_interface *intf = g_usbd_core[busid].intf[i];
2022-02-08 11:44:46 +08:00
2024-02-06 19:51:50 +08:00
if (intf && intf->vendor_handler && (intf->vendor_handler(busid, setup, data, len) == 0)) {
2022-08-19 23:32:10 +08:00
return 0;
2022-02-08 11:44:46 +08:00
}
}
return -1;
}
/**
2022-08-17 20:12:37 +08:00
* @brief handle setup request( standard/class/vendor/other)
2022-02-08 11:44:46 +08:00
*
2024-10-30 21:04:06 +08:00
* @param [in] busid busid
2022-02-08 11:44:46 +08:00
* @param [in] setup The setup packet
* @param [in,out] data Data buffer
* @param [in,out] len Pointer to data length
*
* @return true if the request was handles successfully
*/
2024-02-06 19:51:50 +08:00
static bool usbd_setup_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
2022-02-08 11:44:46 +08:00
{
switch (setup->bmRequestType & USB_REQUEST_TYPE_MASK) {
case USB_REQUEST_STANDARD:
2024-02-06 19:51:50 +08:00
if (usbd_standard_request_handler(busid, setup, data, len) < 0) {
/* Ignore error log for getting Device Qualifier Descriptor request */
if ((setup->bRequest == 0x06) && (setup->wValue == 0x0600)) {
//USB_LOG_DBG("Ignore DQD in fs\r\n");
return false;
}
USB_LOG_ERR("standard request error\r\n");
usbd_print_setup(setup);
return false;
}
break;
case USB_REQUEST_CLASS:
2024-02-06 19:51:50 +08:00
if (usbd_class_request_handler(busid, setup, data, len) < 0) {
USB_LOG_ERR("class request error\r\n");
usbd_print_setup(setup);
return false;
}
break;
case USB_REQUEST_VENDOR:
2024-02-06 19:51:50 +08:00
if (usbd_vendor_request_handler(busid, setup, data, len) < 0) {
USB_LOG_ERR("vendor request error\r\n");
usbd_print_setup(setup);
return false;
}
break;
2022-02-08 11:44:46 +08:00
default:
2022-02-08 11:44:46 +08:00
return false;
}
return true;
}
2024-02-06 19:51:50 +08:00
static void usbd_class_event_notify_handler(uint8_t busid, uint8_t event, void *arg)
{
2024-02-06 19:51:50 +08:00
for (uint8_t i = 0; i < g_usbd_core[busid].intf_offset; i++) {
struct usbd_interface *intf = g_usbd_core[busid].intf[i];
2022-02-08 11:44:46 +08:00
2023-06-06 22:22:46 +08:00
if (arg) {
struct usb_interface_descriptor *desc = (struct usb_interface_descriptor *)arg;
if (intf && intf->notify_handler && (desc->bInterfaceNumber == (intf->intf_num))) {
2024-02-06 19:51:50 +08:00
intf->notify_handler(busid, event, arg);
2023-06-06 22:22:46 +08:00
}
} else {
if (intf && intf->notify_handler) {
2024-02-06 19:51:50 +08:00
intf->notify_handler(busid, event, arg);
2023-06-06 22:22:46 +08:00
}
2022-02-08 11:44:46 +08:00
}
}
}
void usbd_event_sof_handler(uint8_t busid)
{
g_usbd_core[busid].event_handler(busid, USBD_EVENT_SOF);
}
2024-02-06 19:51:50 +08:00
void usbd_event_connect_handler(uint8_t busid)
{
g_usbd_core[busid].event_handler(busid, USBD_EVENT_CONNECTED);
}
2024-02-06 19:51:50 +08:00
void usbd_event_disconnect_handler(uint8_t busid)
{
g_usbd_core[busid].configuration = 0;
g_usbd_core[busid].event_handler(busid, USBD_EVENT_DISCONNECTED);
}
2024-02-06 19:51:50 +08:00
void usbd_event_resume_handler(uint8_t busid)
{
2024-07-26 22:01:12 +08:00
g_usbd_core[busid].is_suspend = false;
g_usbd_core[busid].event_handler(busid, USBD_EVENT_RESUME);
}
2024-02-06 19:51:50 +08:00
void usbd_event_suspend_handler(uint8_t busid)
{
2024-07-26 22:01:12 +08:00
if (g_usbd_core[busid].device_address > 0) {
g_usbd_core[busid].is_suspend = true;
g_usbd_core[busid].event_handler(busid, USBD_EVENT_SUSPEND);
}
}
2024-02-06 19:51:50 +08:00
void usbd_event_reset_handler(uint8_t busid)
{
struct usb_endpoint_descriptor ep0;
2024-02-06 19:51:50 +08:00
usbd_set_address(busid, 0);
2024-07-26 22:01:12 +08:00
g_usbd_core[busid].device_address = 0;
2024-02-06 19:51:50 +08:00
g_usbd_core[busid].configuration = 0;
g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_SETUP;
#ifdef CONFIG_USBDEV_ADVANCE_DESC
2024-04-25 21:11:55 +08:00
g_usbd_core[busid].speed = USB_SPEED_UNKNOWN;
USB_ASSERT_MSG(g_usbd_core[busid].descriptors->device_descriptor_callback != NULL,
"device_descriptor_callback is NULL\r\n");
struct usb_device_descriptor *device_desc = (struct usb_device_descriptor *)g_usbd_core[busid].descriptors->device_descriptor_callback(g_usbd_core[busid].speed);
ep0.wMaxPacketSize = device_desc->bMaxPacketSize0;
#else
ep0.wMaxPacketSize = USB_CTRL_EP_MPS;
#endif
ep0.bLength = 7;
ep0.bDescriptorType = USB_DESCRIPTOR_TYPE_ENDPOINT;
ep0.bmAttributes = USB_ENDPOINT_TYPE_CONTROL;
ep0.bEndpointAddress = USB_CONTROL_IN_EP0;
ep0.bInterval = 0;
2024-02-06 19:51:50 +08:00
usbd_ep_open(busid, &ep0);
ep0.bEndpointAddress = USB_CONTROL_OUT_EP0;
2024-02-06 19:51:50 +08:00
usbd_ep_open(busid, &ep0);
2024-02-06 19:51:50 +08:00
usbd_class_event_notify_handler(busid, USBD_EVENT_RESET, NULL);
g_usbd_core[busid].event_handler(busid, USBD_EVENT_RESET);
}
static void __usbd_event_ep0_setup_complete_handler(uint8_t busid, struct usb_setup_packet *setup)
2022-02-08 11:44:46 +08:00
{
uint8_t *buf;
2022-02-08 11:44:46 +08:00
USB_LOG_DBG("[%s] 0x%02x 0x%02x 0x%04x 0x%04x 0x%04x\r\n",
usb_ep0_state_string[usbd_get_ep0_next_state(busid)],
setup->bmRequestType,
setup->bRequest,
setup->wValue,
setup->wIndex,
setup->wLength);
2022-04-08 15:07:32 +08:00
if (setup->wLength > CONFIG_USBDEV_REQUEST_BUFFER_LEN) {
2022-02-08 11:44:46 +08:00
if ((setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_OUT) {
USB_LOG_ERR("Request buffer too small\r\n");
2024-02-06 19:51:50 +08:00
usbd_ep_set_stall(busid, USB_CONTROL_IN_EP0);
2022-02-08 11:44:46 +08:00
return;
}
}
2024-02-06 19:51:50 +08:00
g_usbd_core[busid].ep0_data_buf = g_usbd_core[busid].req_data;
g_usbd_core[busid].ep0_data_buf_residue = setup->wLength;
g_usbd_core[busid].ep0_data_buf_len = setup->wLength;
g_usbd_core[busid].zlp_flag = false;
2024-05-10 11:36:17 +08:00
buf = g_usbd_core[busid].ep0_data_buf;
2022-02-08 11:44:46 +08:00
2022-08-17 20:12:37 +08:00
/* handle class request when all the data is received */
if (setup->wLength && ((setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_OUT)) {
USB_LOG_DBG("Start reading %d bytes from ep0\r\n", setup->wLength);
g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_OUT_DATA;
2024-02-06 19:51:50 +08:00
usbd_ep_start_read(busid, USB_CONTROL_OUT_EP0, g_usbd_core[busid].ep0_data_buf, setup->wLength);
2022-02-08 11:44:46 +08:00
return;
}
/* Ask installed handler to process request */
if (!usbd_setup_request_handler(busid, setup, &buf, &g_usbd_core[busid].ep0_data_buf_len)) {
g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_SETUP;
2024-02-06 19:51:50 +08:00
usbd_ep_set_stall(busid, USB_CONTROL_IN_EP0);
2022-02-08 11:44:46 +08:00
return;
}
2022-02-08 11:44:46 +08:00
/* Send smallest of requested and offered length */
2024-02-06 19:51:50 +08:00
g_usbd_core[busid].ep0_data_buf_residue = MIN(g_usbd_core[busid].ep0_data_buf_len, setup->wLength);
if (g_usbd_core[busid].ep0_data_buf_residue > CONFIG_USBDEV_REQUEST_BUFFER_LEN) {
2023-01-06 20:39:45 +08:00
USB_LOG_ERR("Request buffer too small\r\n");
g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_SETUP;
usbd_ep_set_stall(busid, USB_CONTROL_IN_EP0);
2023-01-06 20:39:45 +08:00
return;
}
2023-01-06 20:39:45 +08:00
/* use *data = xxx; g_usbd_core[busid].ep0_data_buf records real data address, we should copy data into ep0 buffer.
* Why we should copy once? because some chips are not access to flash with dma if real data address is in flash address(such as ch32).
*/
if (buf != g_usbd_core[busid].ep0_data_buf) {
#ifdef CONFIG_USBDEV_EP0_INDATA_NO_COPY
g_usbd_core[busid].ep0_data_buf = buf;
#else
2024-11-24 23:04:27 +08:00
usb_memcpy(g_usbd_core[busid].ep0_data_buf, buf, g_usbd_core[busid].ep0_data_buf_residue);
#endif
} else {
/* use memcpy(*data, xxx, len); has copied into ep0 buffer, we do nothing */
}
if (g_usbd_core[busid].ep0_data_buf_residue > 0) {
g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_IN_DATA;
} else {
g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_IN_STATUS;
}
/* Send data or status to host */
2024-02-06 19:51:50 +08:00
usbd_ep_start_write(busid, USB_CONTROL_IN_EP0, g_usbd_core[busid].ep0_data_buf, g_usbd_core[busid].ep0_data_buf_residue);
/*
2022-11-18 22:43:32 +08:00
* Set ZLP flag when host asks for a bigger length and the data size is
* multiplier of USB_CTRL_EP_MPS, to indicate the transfer done after zlp
* sent.
*/
2024-02-06 19:51:50 +08:00
if ((setup->wLength > g_usbd_core[busid].ep0_data_buf_len) && (!(g_usbd_core[busid].ep0_data_buf_len % USB_CTRL_EP_MPS))) {
g_usbd_core[busid].zlp_flag = true;
}
2022-02-08 11:44:46 +08:00
}
void usbd_event_ep0_setup_complete_handler(uint8_t busid, uint8_t *psetup)
{
struct usb_setup_packet *setup = &g_usbd_core[busid].setup;
memcpy(setup, psetup, 8);
#ifdef CONFIG_USBDEV_EP0_THREAD
usb_osal_mq_send(g_usbd_core[busid].usbd_ep0_mq, USB_EP0_STATE_SETUP);
#else
__usbd_event_ep0_setup_complete_handler(busid, setup);
#endif
}
static void usbd_event_ep0_in_complete_handler(uint8_t busid, uint8_t ep, uint32_t nbytes)
2022-02-08 11:44:46 +08:00
{
2024-02-06 19:51:50 +08:00
struct usb_setup_packet *setup = &g_usbd_core[busid].setup;
2022-02-08 11:44:46 +08:00
(void)ep;
2024-02-06 19:51:50 +08:00
g_usbd_core[busid].ep0_data_buf += nbytes;
g_usbd_core[busid].ep0_data_buf_residue -= nbytes;
2022-02-08 11:44:46 +08:00
USB_LOG_DBG("[%s] in %d bytes, %d remained\r\n",
usb_ep0_state_string[usbd_get_ep0_next_state(busid)],
(unsigned int)nbytes,
(unsigned int)g_usbd_core[busid].ep0_data_buf_residue);
2022-02-08 11:44:46 +08:00
2024-02-06 19:51:50 +08:00
if (g_usbd_core[busid].ep0_data_buf_residue != 0) {
2022-12-11 21:19:48 +08:00
/* Start sending the remain data */
2024-02-06 19:51:50 +08:00
usbd_ep_start_write(busid, USB_CONTROL_IN_EP0, g_usbd_core[busid].ep0_data_buf, g_usbd_core[busid].ep0_data_buf_residue);
2022-12-11 21:19:48 +08:00
} else {
2024-02-06 19:51:50 +08:00
if (g_usbd_core[busid].zlp_flag == true) {
g_usbd_core[busid].zlp_flag = false;
2022-12-11 21:19:48 +08:00
/* Send zlp to host */
USB_LOG_DBG("EP0 Send zlp\r\n");
2024-02-06 19:51:50 +08:00
usbd_ep_start_write(busid, USB_CONTROL_IN_EP0, NULL, 0);
} else {
2022-12-11 21:19:48 +08:00
/* Satisfying three conditions will jump here.
* 1. send status completely
* 2. send zlp completely
* 3. send last data completely.
*/
2022-12-11 21:19:48 +08:00
if (setup->wLength && ((setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_IN)) {
/* if all data has sent completely, start reading out status */
g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_OUT_STATUS;
2024-02-06 19:51:50 +08:00
usbd_ep_start_read(busid, USB_CONTROL_OUT_EP0, NULL, 0);
return;
}
if (g_usbd_core[busid].ep0_next_state == USBD_EP0_STATE_IN_STATUS) {
g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_SETUP;
}
#ifdef CONFIG_USBDEV_TEST_MODE
if (g_usbd_core[busid].test_req) {
usbd_execute_test_mode(busid, HI_BYTE(setup->wIndex));
g_usbd_core[busid].test_req = false;
}
#endif
2022-02-08 11:44:46 +08:00
}
}
}
static void usbd_event_ep0_out_complete_handler(uint8_t busid, uint8_t ep, uint32_t nbytes)
2022-02-08 11:44:46 +08:00
{
2024-02-06 19:51:50 +08:00
struct usb_setup_packet *setup = &g_usbd_core[busid].setup;
2022-02-08 11:44:46 +08:00
(void)ep;
(void)setup;
USB_LOG_DBG("[%s] out %d bytes, %d remained\r\n",
usb_ep0_state_string[usbd_get_ep0_next_state(busid)],
(unsigned int)nbytes,
(unsigned int)g_usbd_core[busid].ep0_data_buf_residue);
2022-12-11 21:19:48 +08:00
if (nbytes > 0) {
2024-02-06 19:51:50 +08:00
g_usbd_core[busid].ep0_data_buf += nbytes;
g_usbd_core[busid].ep0_data_buf_residue -= nbytes;
2022-02-08 11:44:46 +08:00
2024-02-06 19:51:50 +08:00
if (g_usbd_core[busid].ep0_data_buf_residue == 0) {
#ifdef CONFIG_USBDEV_EP0_THREAD
usb_osal_mq_send(g_usbd_core[busid].usbd_ep0_mq, USB_EP0_STATE_OUT);
#else
2022-12-11 21:19:48 +08:00
/* Received all, send data to handler */
2024-02-06 19:51:50 +08:00
g_usbd_core[busid].ep0_data_buf = g_usbd_core[busid].req_data;
if (!usbd_setup_request_handler(busid, setup, &g_usbd_core[busid].ep0_data_buf, &g_usbd_core[busid].ep0_data_buf_len)) {
g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_SETUP;
2024-02-06 19:51:50 +08:00
usbd_ep_set_stall(busid, USB_CONTROL_IN_EP0);
2022-12-11 21:19:48 +08:00
return;
}
2022-12-11 21:19:48 +08:00
g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_IN_STATUS;
2022-12-11 21:19:48 +08:00
/*Send status to host*/
2024-02-06 19:51:50 +08:00
usbd_ep_start_write(busid, USB_CONTROL_IN_EP0, NULL, 0);
#endif
} else {
2022-12-11 21:19:48 +08:00
/* Start reading the remain data */
2024-02-06 19:51:50 +08:00
usbd_ep_start_read(busid, USB_CONTROL_OUT_EP0, g_usbd_core[busid].ep0_data_buf, g_usbd_core[busid].ep0_data_buf_residue);
}
} else {
2022-12-11 21:19:48 +08:00
/* Read out status completely, do nothing */
g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_SETUP;
2022-12-11 21:19:48 +08:00
}
}
2024-02-06 19:51:50 +08:00
void usbd_event_ep_in_complete_handler(uint8_t busid, uint8_t ep, uint32_t nbytes)
2022-12-11 21:19:48 +08:00
{
2024-02-06 19:51:50 +08:00
if (g_usbd_core[busid].tx_msg[ep & 0x7f].cb) {
g_usbd_core[busid].tx_msg[ep & 0x7f].cb(busid, ep, nbytes);
2022-12-11 21:19:48 +08:00
}
}
2024-02-06 19:51:50 +08:00
void usbd_event_ep_out_complete_handler(uint8_t busid, uint8_t ep, uint32_t nbytes)
2022-12-11 21:19:48 +08:00
{
2024-02-06 19:51:50 +08:00
if (g_usbd_core[busid].rx_msg[ep & 0x7f].cb) {
g_usbd_core[busid].rx_msg[ep & 0x7f].cb(busid, ep, nbytes);
2022-12-11 21:19:48 +08:00
}
}
2023-01-07 17:07:56 +08:00
#ifdef CONFIG_USBDEV_ADVANCE_DESC
2024-02-06 19:51:50 +08:00
void usbd_desc_register(uint8_t busid, const struct usb_descriptor *desc)
2022-11-18 22:43:32 +08:00
{
2024-02-06 19:51:50 +08:00
memset(&g_usbd_core[busid], 0, sizeof(struct usbd_core_priv));
2023-03-22 15:00:34 +08:00
2024-02-06 19:51:50 +08:00
g_usbd_core[busid].descriptors = desc;
g_usbd_core[busid].intf_offset = 0;
2023-03-22 15:00:34 +08:00
2024-02-06 19:51:50 +08:00
g_usbd_core[busid].tx_msg[0].ep = 0x80;
g_usbd_core[busid].tx_msg[0].cb = usbd_event_ep0_in_complete_handler;
g_usbd_core[busid].rx_msg[0].ep = 0x00;
g_usbd_core[busid].rx_msg[0].cb = usbd_event_ep0_out_complete_handler;
2022-11-18 22:43:32 +08:00
}
#else
2024-02-06 19:51:50 +08:00
void usbd_desc_register(uint8_t busid, const uint8_t *desc)
2022-02-08 11:44:46 +08:00
{
2024-02-06 19:51:50 +08:00
memset(&g_usbd_core[busid], 0, sizeof(struct usbd_core_priv));
2023-03-22 15:00:34 +08:00
2024-02-06 19:51:50 +08:00
g_usbd_core[busid].descriptors = desc;
g_usbd_core[busid].intf_offset = 0;
2023-03-22 15:00:34 +08:00
2024-02-06 19:51:50 +08:00
g_usbd_core[busid].tx_msg[0].ep = 0x80;
g_usbd_core[busid].tx_msg[0].cb = usbd_event_ep0_in_complete_handler;
g_usbd_core[busid].rx_msg[0].ep = 0x00;
g_usbd_core[busid].rx_msg[0].cb = usbd_event_ep0_out_complete_handler;
2022-02-08 11:44:46 +08:00
}
2022-02-08 11:44:46 +08:00
/* Register MS OS Descriptors version 1 */
2024-02-06 19:51:50 +08:00
void usbd_msosv1_desc_register(uint8_t busid, struct usb_msosv1_descriptor *desc)
2022-02-08 11:44:46 +08:00
{
2024-02-06 19:51:50 +08:00
g_usbd_core[busid].msosv1_desc = desc;
2022-02-08 11:44:46 +08:00
}
/* Register MS OS Descriptors version 2 */
2024-02-06 19:51:50 +08:00
void usbd_msosv2_desc_register(uint8_t busid, struct usb_msosv2_descriptor *desc)
2022-02-08 11:44:46 +08:00
{
2024-02-06 19:51:50 +08:00
g_usbd_core[busid].msosv2_desc = desc;
2022-02-08 11:44:46 +08:00
}
2024-02-06 19:51:50 +08:00
void usbd_bos_desc_register(uint8_t busid, struct usb_bos_descriptor *desc)
2022-02-08 11:44:46 +08:00
{
2024-02-06 19:51:50 +08:00
g_usbd_core[busid].bos_desc = desc;
2022-02-08 11:44:46 +08:00
}
2024-07-25 21:39:36 +08:00
void usbd_webusb_desc_register(uint8_t busid, struct usb_webusb_descriptor *desc)
{
g_usbd_core[busid].webusb_url_desc = desc;
}
2022-11-18 22:43:32 +08:00
#endif
2022-02-08 11:44:46 +08:00
2024-02-06 19:51:50 +08:00
void usbd_add_interface(uint8_t busid, struct usbd_interface *intf)
2022-02-08 11:44:46 +08:00
{
2024-02-06 19:51:50 +08:00
intf->intf_num = g_usbd_core[busid].intf_offset;
g_usbd_core[busid].intf[g_usbd_core[busid].intf_offset] = intf;
g_usbd_core[busid].intf_offset++;
2022-02-08 11:44:46 +08:00
}
2024-02-06 19:51:50 +08:00
void usbd_add_endpoint(uint8_t busid, struct usbd_endpoint *ep)
2022-02-08 11:44:46 +08:00
{
2022-07-19 19:08:46 +08:00
if (ep->ep_addr & 0x80) {
2024-02-06 19:51:50 +08:00
g_usbd_core[busid].tx_msg[ep->ep_addr & 0x7f].ep = ep->ep_addr;
g_usbd_core[busid].tx_msg[ep->ep_addr & 0x7f].cb = ep->ep_cb;
2022-07-19 19:08:46 +08:00
} else {
2024-02-06 19:51:50 +08:00
g_usbd_core[busid].rx_msg[ep->ep_addr & 0x7f].ep = ep->ep_addr;
g_usbd_core[busid].rx_msg[ep->ep_addr & 0x7f].cb = ep->ep_cb;
2022-07-19 19:08:46 +08:00
}
2022-02-08 11:44:46 +08:00
}
2024-04-29 11:32:54 +08:00
uint16_t usbd_get_ep_mps(uint8_t busid, uint8_t ep)
{
if (ep & 0x80) {
return g_usbd_core[busid].tx_msg[ep & 0x7f].ep_mps;
} else {
return g_usbd_core[busid].rx_msg[ep & 0x7f].ep_mps;
}
}
2024-04-29 14:11:24 +08:00
uint8_t usbd_get_ep_mult(uint8_t busid, uint8_t ep)
{
if (ep & 0x80) {
return g_usbd_core[busid].tx_msg[ep & 0x7f].ep_mult;
} else {
return g_usbd_core[busid].rx_msg[ep & 0x7f].ep_mult;
}
}
2024-02-06 19:51:50 +08:00
bool usb_device_is_configured(uint8_t busid)
2022-02-08 11:44:46 +08:00
{
2024-02-06 19:51:50 +08:00
return g_usbd_core[busid].configuration;
2022-02-08 11:44:46 +08:00
}
bool usb_device_is_suspend(uint8_t busid)
{
return g_usbd_core[busid].is_suspend;
}
2024-07-26 22:01:12 +08:00
int usbd_send_remote_wakeup(uint8_t busid)
{
if (g_usbd_core[busid].remote_wakeup_support && g_usbd_core[busid].remote_wakeup_enabled && g_usbd_core[busid].is_suspend) {
return usbd_set_remote_wakeup(busid);
} else {
if (!g_usbd_core[busid].remote_wakeup_support) {
USB_LOG_ERR("device does not support remote wakeup\r\n");
}
if (!g_usbd_core[busid].remote_wakeup_enabled) {
USB_LOG_ERR("device remote wakeup is not enabled\r\n");
}
if (!g_usbd_core[busid].is_suspend) {
USB_LOG_ERR("device is not in suspend state\r\n");
}
return -1;
}
}
uint8_t usbd_get_ep0_next_state(uint8_t busid)
{
return g_usbd_core[busid].ep0_next_state;
}
#ifdef CONFIG_USBDEV_EP0_THREAD
static void usbdev_ep0_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
{
uintptr_t event;
int ret;
uint8_t busid = (uint8_t)CONFIG_USB_OSAL_THREAD_GET_ARGV;
struct usb_setup_packet *setup = &g_usbd_core[busid].setup;
while (1) {
ret = usb_osal_mq_recv(g_usbd_core[busid].usbd_ep0_mq, (uintptr_t *)&event, USB_OSAL_WAITING_FOREVER);
if (ret < 0) {
continue;
}
USB_LOG_DBG("event:%d\r\n", (unsigned int)event);
switch (event) {
case USB_EP0_STATE_SETUP:
__usbd_event_ep0_setup_complete_handler(busid, setup);
break;
case USB_EP0_STATE_IN:
// do nothing
break;
case USB_EP0_STATE_OUT:
/* Received all, send data to handler */
g_usbd_core[busid].ep0_data_buf = g_usbd_core[busid].req_data;
if (!usbd_setup_request_handler(busid, setup, &g_usbd_core[busid].ep0_data_buf, &g_usbd_core[busid].ep0_data_buf_len)) {
g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_SETUP;
usbd_ep_set_stall(busid, USB_CONTROL_IN_EP0);
continue;
}
g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_IN_STATUS;
/*Send status to host*/
usbd_ep_start_write(busid, USB_CONTROL_IN_EP0, NULL, 0);
break;
default:
break;
}
}
}
#endif
int usbd_initialize(uint8_t busid, uintptr_t reg_base, void (*event_handler)(uint8_t busid, uint8_t event))
{
2024-01-16 22:50:29 +08:00
int ret;
struct usbd_bus *bus;
if (busid >= CONFIG_USBDEV_MAX_BUS) {
USB_LOG_ERR("bus overflow\r\n");
while (1) {
}
}
bus = &g_usbdev_bus[busid];
bus->reg_base = reg_base;
2024-01-16 22:50:29 +08:00
#ifdef CONFIG_USBDEV_EP0_THREAD
g_usbd_core[busid].usbd_ep0_mq = usb_osal_mq_create(1);
if (g_usbd_core[busid].usbd_ep0_mq == NULL) {
USB_LOG_ERR("No memory to alloc for g_usbd_core[busid].usbd_ep0_mq\r\n");
while (1) {
}
}
g_usbd_core[busid].usbd_ep0_thread = usb_osal_thread_create("usbd_ep0", CONFIG_USBDEV_EP0_STACKSIZE, CONFIG_USBDEV_EP0_PRIO, usbdev_ep0_thread, (void *)(uint32_t)busid);
if (g_usbd_core[busid].usbd_ep0_thread == NULL) {
USB_LOG_ERR("No memory to alloc for g_usbd_core[busid].usbd_ep0_thread\r\n");
while (1) {
}
}
#endif
2024-02-06 19:51:50 +08:00
g_usbd_core[busid].event_handler = event_handler;
ret = usb_dc_init(busid);
usbd_class_event_notify_handler(busid, USBD_EVENT_INIT, NULL);
g_usbd_core[busid].event_handler(busid, USBD_EVENT_INIT);
2024-01-16 22:50:29 +08:00
return ret;
2022-08-19 23:32:10 +08:00
}
2022-09-24 23:23:34 +08:00
2024-02-06 19:51:50 +08:00
int usbd_deinitialize(uint8_t busid)
2022-09-24 23:23:34 +08:00
{
2024-09-18 18:36:17 +08:00
if (busid >= CONFIG_USBDEV_MAX_BUS) {
USB_LOG_ERR("bus overflow\r\n");
while (1) {
}
}
g_usbd_core[busid].event_handler(busid, USBD_EVENT_DEINIT);
usbd_class_event_notify_handler(busid, USBD_EVENT_DEINIT, NULL);
usb_dc_deinit(busid);
g_usbd_core[busid].intf_offset = 0;
#ifdef CONFIG_USBDEV_EP0_THREAD
if (g_usbd_core[busid].usbd_ep0_mq) {
usb_osal_mq_delete(g_usbd_core[busid].usbd_ep0_mq);
}
if (g_usbd_core[busid].usbd_ep0_thread) {
usb_osal_thread_delete(g_usbd_core[busid].usbd_ep0_thread);
}
#endif
2022-09-24 23:23:34 +08:00
return 0;
}