From cb2510f121018840f309877aa17e9fdeb81000be Mon Sep 17 00:00:00 2001 From: sakumisu <1203593632@qq.com> Date: Thu, 8 Sep 2022 21:08:39 +0800 Subject: [PATCH] remove custom handler, split class handler into interface and endpoint handler --- class/audio/usbd_audio.c | 10 ++-- class/cdc/usbd_cdc.c | 6 +- class/dfu/usbd_dfu.c | 6 +- class/hid/usbd_hid.c | 6 +- class/msc/usbd_msc.c | 6 +- class/mtp/usbd_mtp.c | 2 +- class/printer/usbd_printer.c | 6 +- class/template/usbd_xxx.c | 6 +- class/video/usbd_video.c | 6 +- class/wireless/usbd_rndis.c | 4 +- core/usbd_core.c | 103 ++++++++++++++++------------------- core/usbd_core.h | 8 +-- 12 files changed, 78 insertions(+), 91 deletions(-) diff --git a/class/audio/usbd_audio.c b/class/audio/usbd_audio.c index 6931b290..6755b83d 100644 --- a/class/audio/usbd_audio.c +++ b/class/audio/usbd_audio.c @@ -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; diff --git a/class/cdc/usbd_cdc.c b/class/cdc/usbd_cdc.c index 403c2850..8c9ee5a8 100644 --- a/class/cdc/usbd_cdc.c +++ b/class/cdc/usbd_cdc.c @@ -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; diff --git a/class/dfu/usbd_dfu.c b/class/dfu/usbd_dfu.c index 3df0aeda..6ee98c90 100644 --- a/class/dfu/usbd_dfu.c +++ b/class/dfu/usbd_dfu.c @@ -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; diff --git a/class/hid/usbd_hid.c b/class/hid/usbd_hid.c index b969318f..12a69cf1 100644 --- a/class/hid/usbd_hid.c +++ b/class/hid/usbd_hid.c @@ -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; diff --git a/class/msc/usbd_msc.c b/class/msc/usbd_msc.c index 8552f4e8..b025551a 100644 --- a/class/msc/usbd_msc.c +++ b/class/msc/usbd_msc.c @@ -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; diff --git a/class/mtp/usbd_mtp.c b/class/mtp/usbd_mtp.c index d3062169..38661e91 100644 --- a/class/mtp/usbd_mtp.c +++ b/class/mtp/usbd_mtp.c @@ -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", diff --git a/class/printer/usbd_printer.c b/class/printer/usbd_printer.c index 24e16c98..bdc5984c 100644 --- a/class/printer/usbd_printer.c +++ b/class/printer/usbd_printer.c @@ -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; diff --git a/class/template/usbd_xxx.c b/class/template/usbd_xxx.c index 66632517..f35911c5 100644 --- a/class/template/usbd_xxx.c +++ b/class/template/usbd_xxx.c @@ -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; diff --git a/class/video/usbd_video.c b/class/video/usbd_video.c index ac847400..b04816c6 100644 --- a/class/video/usbd_video.c +++ b/class/video/usbd_video.c @@ -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; diff --git a/class/wireless/usbd_rndis.c b/class/wireless/usbd_rndis.c index 29ece977..f9197279 100644 --- a/class/wireless/usbd_rndis.c +++ b/class/wireless/usbd_rndis.c @@ -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); diff --git a/core/usbd_core.c b/core/usbd_core.c index b81d09a4..1601cb0f 100644 --- a/core/usbd_core.c +++ b/core/usbd_core.c @@ -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; diff --git a/core/usbd_core.h b/core/usbd_core.h index 10c3bb1f..b56cc067 100644 --- a/core/usbd_core.h +++ b/core/usbd_core.h @@ -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;