remove custom handler, split class handler into interface and endpoint

handler
This commit is contained in:
sakumisu
2022-09-08 21:08:39 +08:00
parent de5ca1313a
commit cb2510f121
12 changed files with 78 additions and 91 deletions

View File

@@ -77,7 +77,7 @@ const uint8_t default_sampling_freq_table[] = {
#endif
#if CONFIG_USBDEV_AUDIO_VERSION < 0x0200
static int audio_custom_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
static int audio_class_endpoint_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
uint8_t control_selector;
uint32_t sampling_freq = 0;
@@ -121,7 +121,7 @@ static int audio_custom_request_handler(struct usb_setup_packet *setup, uint8_t
}
#endif
static int audio_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
static int audio_class_interface_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("AUDIO Class request: "
"bRequest 0x%02x\r\n",
@@ -381,11 +381,11 @@ struct usbd_interface *usbd_audio_alloc_intf(void)
return NULL;
}
intf->class_handler = audio_class_request_handler;
intf->class_interface_handler = audio_class_interface_request_handler;
#if CONFIG_USBDEV_AUDIO_VERSION < 0x0200
intf->custom_handler = audio_custom_request_handler;
intf->class_endpoint_handler = audio_class_endpoint_request_handler;
#else
intf->custom_handler = NULL;
intf->class_endpoint_handler = NULL;
#endif
intf->vendor_handler = NULL;
intf->notify_handler = audio_notify_handler;

View File

@@ -9,7 +9,7 @@
const char *stop_name[] = { "1", "1.5", "2" };
const char *parity_name[] = { "N", "O", "E", "M", "S" };
static int cdc_acm_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
static int cdc_acm_class_interface_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("CDC Class request: "
"bRequest 0x%02x\r\n",
@@ -98,8 +98,8 @@ struct usbd_interface *usbd_cdc_acm_alloc_intf(void)
return NULL;
}
intf->class_handler = cdc_acm_class_request_handler;
intf->custom_handler = NULL;
intf->class_interface_handler = cdc_acm_class_interface_request_handler;
intf->class_endpoint_handler = NULL;
intf->vendor_handler = NULL;
intf->notify_handler = cdc_notify_handler;

View File

@@ -10,7 +10,7 @@ struct dfu_cfg_priv {
struct dfu_info info;
} usbd_dfu_cfg;
static int dfu_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
static int dfu_class_interface_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_WRN("DFU Class request: "
"bRequest 0x%02x\r\n",
@@ -58,8 +58,8 @@ struct usbd_interface *usbd_dfu_alloc_intf(void)
return NULL;
}
intf->class_handler = dfu_class_request_handler;
intf->custom_handler = NULL;
intf->class_interface_handler = dfu_class_interface_request_handler;
intf->class_endpoint_handler = NULL;
intf->vendor_handler = NULL;
intf->notify_handler = dfu_notify_handler;

View File

@@ -62,7 +62,7 @@ static int hid_custom_request_handler(struct usb_setup_packet *setup, uint8_t **
return -1;
}
static int hid_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
static int hid_class_interface_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("HID Class request: "
"bRequest 0x%02x\r\n",
@@ -113,8 +113,8 @@ struct usbd_interface *usbd_hid_alloc_intf(const uint8_t *desc, uint32_t desc_le
return NULL;
}
intf->class_handler = hid_class_request_handler;
intf->custom_handler = hid_custom_request_handler;
intf->class_interface_handler = hid_class_interface_request_handler;
intf->class_endpoint_handler = NULL;
intf->vendor_handler = NULL;
intf->notify_handler = NULL;

View File

@@ -64,7 +64,7 @@ static void usbd_msc_reset(void)
usbd_msc_cfg.stage = MSC_READ_CBW;
}
static int msc_storage_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
static int msc_storage_class_interface_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("MSC Class request: "
"bRequest 0x%02x\r\n",
@@ -919,8 +919,8 @@ struct usbd_interface *usbd_msc_alloc_intf(const uint8_t out_ep, const uint8_t i
return NULL;
}
intf->class_handler = msc_storage_class_request_handler;
intf->custom_handler = NULL;
intf->class_interface_handler = msc_storage_class_interface_request_handler;
intf->class_endpoint_handler = NULL;
intf->vendor_handler = NULL;
intf->notify_handler = msc_storage_notify_handler;

View File

@@ -23,7 +23,7 @@ struct mtp_cfg_priv {
/* Describe EndPoints configuration */
static struct usbd_interface mtp_ep_data[2];
static int mtp_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
static int mtp_class_interface_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("MTP Class request: "
"bRequest 0x%02x\r\n",

View File

@@ -11,7 +11,7 @@ struct printer_cfg_priv {
uint8_t port_status;
} usbd_printer_cfg;
static int printer_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
static int printer_class_interface_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("Printer Class request: "
"bRequest 0x%02x\r\n",
@@ -54,8 +54,8 @@ struct usbd_interface *usbd_printer_alloc_intf(void)
return NULL;
}
intf->class_handler = printer_class_request_handler;
intf->custom_handler = NULL;
intf->class_interface_handler = printer_class_interface_request_handler;
intf->class_endpoint_handler = NULL;
intf->vendor_handler = NULL;
intf->notify_handler = printer_notify_handler;

View File

@@ -1,7 +1,7 @@
#include "usbd_core.h"
#include "usbd_xxx.h"
static int xxx_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
static int xxx_class_interface_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_WRN("XXX Class request: "
"bRequest 0x%02x\r\n",
@@ -30,8 +30,8 @@ static void xxx_notify_handler(uint8_t event, void *arg)
void usbd_xxx_add_interface(usbd_class_t *devclass, usbd_interface_t *intf)
{
intf->class_handler = xxx_class_request_handler;
intf->custom_handler = NULL;
intf->class_interface_handler = xxx_class_interface_request_handler;
intf->class_endpoint_handler = NULL;
intf->vendor_handler = NULL;
intf->notify_handler = xxx_notify_handler;

View File

@@ -659,7 +659,7 @@ static int usbd_video_stream_request_handler(struct usb_setup_packet *setup, uin
return 0;
}
static int video_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
static int video_class_interface_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("Video Class request: "
"bRequest 0x%02x\r\n",
@@ -750,8 +750,8 @@ struct usbd_interface *usbd_video_alloc_intf(uint32_t dwFrameInterval, uint32_t
return NULL;
}
intf->class_handler = video_class_request_handler;
intf->custom_handler = NULL;
intf->class_interface_handler = video_class_interface_request_handler;
intf->class_endpoint_handler = NULL;
intf->vendor_handler = NULL;
intf->notify_handler = video_notify_handler;

View File

@@ -391,8 +391,8 @@ void usbd_rndis_add_interface(usbd_class_t *devclass, usbd_interface_t *intf)
usbd_class_register(devclass);
}
intf->class_handler = rndis_class_request_handler;
intf->custom_handler = NULL;
intf->class_interface_handler = rndis_class_request_handler;
intf->class_endpoint_handler = NULL;
intf->vendor_handler = NULL;
intf->notify_handler = rndis_notify_handler;
usbd_class_add_interface(devclass, intf);

View File

@@ -431,6 +431,8 @@ static bool usbd_std_device_req_handler(struct usb_setup_packet *setup, uint8_t
static bool usbd_std_interface_req_handler(struct usb_setup_packet *setup,
uint8_t **data, uint32_t *len)
{
uint8_t type = HI_BYTE(setup->wValue);
uint8_t intf_num = LO_BYTE(setup->wIndex);
bool ret = true;
/* Only when device is configured, then interface requests can be valid. */
@@ -445,6 +447,24 @@ static bool usbd_std_interface_req_handler(struct usb_setup_packet *setup,
*len = 2;
break;
case USB_REQUEST_GET_DESCRIPTOR:
if (type == 0x22) { /* HID_DESCRIPTOR_TYPE_HID_REPORT */
USB_LOG_INFO("read hid report descriptor\r\n");
usb_slist_t *i;
usb_slist_for_each(i, &usbd_intf_head)
{
struct usbd_interface *intf = usb_slist_entry(i, struct usbd_interface, list);
if (intf->intf_num == intf_num) {
*data = (uint8_t *)intf->hid_report_descriptor;
*len = intf->hid_report_descriptor_len;
return true;
}
}
}
ret = false;
break;
case USB_REQUEST_CLEAR_FEATURE:
case USB_REQUEST_SET_FEATURE:
ret = false;
@@ -587,8 +607,8 @@ static int usbd_class_request_handler(struct usb_setup_packet *setup, uint8_t **
{
struct usbd_interface *intf = usb_slist_entry(i, struct usbd_interface, list);
if (intf->class_handler && (intf->intf_num == (setup->wIndex & 0xFF))) {
return intf->class_handler(setup, data, len);
if (intf->class_interface_handler && (intf->intf_num == (setup->wIndex & 0xFF))) {
return intf->class_interface_handler(setup, data, len);
}
}
} else if ((setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) == USB_REQUEST_RECIPIENT_ENDPOINT) {
@@ -596,8 +616,8 @@ static int usbd_class_request_handler(struct usb_setup_packet *setup, uint8_t **
{
struct usbd_interface *intf = usb_slist_entry(i, struct usbd_interface, list);
if (intf->custom_handler && (intf->intf_num == ((setup->wIndex >> 8) & 0xFF))) {
return intf->custom_handler(setup, data, len);
if (intf->class_endpoint_handler && (intf->intf_num == ((setup->wIndex >> 8) & 0xFF))) {
return intf->class_endpoint_handler(setup, data, len);
}
}
}
@@ -666,34 +686,6 @@ static int usbd_vendor_request_handler(struct usb_setup_packet *setup, uint8_t *
return -1;
}
/**
* @brief handler for special requests (for hid report or audio ep request, later will be removed)
*
* @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
*/
static int usbd_custom_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
if ((setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) != USB_REQUEST_RECIPIENT_INTERFACE) {
return -1;
}
usb_slist_t *i;
usb_slist_for_each(i, &usbd_intf_head)
{
struct usbd_interface *intf = usb_slist_entry(i, struct usbd_interface, list);
if (intf->custom_handler && (intf->intf_num == (setup->wIndex & 0xFF))) {
return intf->custom_handler(setup, data, len);
}
}
return -1;
}
/**
* @brief handle setup request( standard/class/vendor/other)
*
@@ -705,32 +697,31 @@ static int usbd_custom_request_handler(struct usb_setup_packet *setup, uint8_t *
*/
static bool usbd_setup_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
uint8_t type = setup->bmRequestType & USB_REQUEST_TYPE_MASK;
switch (setup->bmRequestType & USB_REQUEST_TYPE_MASK) {
case USB_REQUEST_STANDARD:
if (usbd_standard_request_handler(setup, data, len) < 0) {
USB_LOG_ERR("standard request error\r\n");
usbd_print_setup(setup);
return false;
}
break;
case USB_REQUEST_CLASS:
if (usbd_class_request_handler(setup, data, len) < 0) {
USB_LOG_ERR("class request error\r\n");
usbd_print_setup(setup);
return false;
}
break;
case USB_REQUEST_VENDOR:
if (usbd_vendor_request_handler(setup, data, len) < 0) {
USB_LOG_ERR("vendor request error\r\n");
usbd_print_setup(setup);
return false;
}
break;
if (!usbd_custom_request_handler(setup, data, len)) {
return true;
}
if (type == USB_REQUEST_STANDARD) {
if (usbd_standard_request_handler(setup, data, len) < 0) {
USB_LOG_ERR("standard request error\r\n");
usbd_print_setup(setup);
default:
return false;
}
} else if (type == USB_REQUEST_CLASS) {
if (usbd_class_request_handler(setup, data, len) < 0) {
USB_LOG_ERR("class request error\r\n");
usbd_print_setup(setup);
return false;
}
} else if (type == USB_REQUEST_VENDOR) {
if (usbd_vendor_request_handler(setup, data, len) < 0) {
USB_LOG_ERR("vendor request error\r\n");
usbd_print_setup(setup);
return false;
}
} else {
return false;
}
return true;

View File

@@ -53,13 +53,9 @@ struct usbd_endpoint {
struct usbd_interface {
usb_slist_t list;
/** Handler for USB Class specific commands*/
usbd_request_handler class_handler;
/** Handler for USB Vendor specific commands */
usbd_request_handler class_interface_handler;
usbd_request_handler class_endpoint_handler;
usbd_request_handler vendor_handler;
/** Handler for USB custom specific commands */
usbd_request_handler custom_handler;
/** Handler for USB event notify commands */
usbd_notify_handler notify_handler;
const uint8_t *hid_report_descriptor;
uint32_t hid_report_descriptor_len;