refactor usb device intf and ep register
This commit is contained in:
@@ -6,32 +6,6 @@
|
||||
#include "usbd_core.h"
|
||||
#include "usbd_hid.h"
|
||||
|
||||
struct usbd_hid {
|
||||
const uint8_t *hid_descriptor;
|
||||
const uint8_t *hid_report_descriptor;
|
||||
uint32_t hid_report_descriptor_len;
|
||||
uint8_t intf_num;
|
||||
uint8_t report;
|
||||
uint8_t idle_state;
|
||||
uint8_t protocol;
|
||||
|
||||
usb_slist_t list;
|
||||
};
|
||||
|
||||
static usb_slist_t usbd_hid_head = USB_SLIST_OBJECT_INIT(usbd_hid_head);
|
||||
|
||||
static void usbd_hid_reset(void)
|
||||
{
|
||||
usb_slist_t *i;
|
||||
usb_slist_for_each(i, &usbd_hid_head)
|
||||
{
|
||||
struct usbd_hid *hid_intf = usb_slist_entry(i, struct usbd_hid, list);
|
||||
hid_intf->report = 0;
|
||||
hid_intf->idle_state = 0;
|
||||
hid_intf->protocol = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int hid_custom_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
|
||||
{
|
||||
USB_LOG_DBG("HID Custom request: "
|
||||
@@ -43,33 +17,34 @@ static int hid_custom_request_handler(struct usb_setup_packet *setup, uint8_t **
|
||||
uint8_t value = (uint8_t)(setup->wValue >> 8);
|
||||
uint8_t intf_num = (uint8_t)setup->wIndex;
|
||||
|
||||
struct usbd_hid *current_hid_class = NULL;
|
||||
usb_slist_t *i;
|
||||
usb_slist_for_each(i, &usbd_hid_head)
|
||||
{
|
||||
struct usbd_hid *hid_class = usb_slist_entry(i, struct usbd_hid, list);
|
||||
struct usbd_interface *match_intf = NULL;
|
||||
|
||||
if (hid_class->intf_num == intf_num) {
|
||||
current_hid_class = hid_class;
|
||||
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) {
|
||||
match_intf = intf;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (current_hid_class == NULL) {
|
||||
if (match_intf == NULL) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
switch (value) {
|
||||
case HID_DESCRIPTOR_TYPE_HID:
|
||||
USB_LOG_INFO("get HID Descriptor\r\n");
|
||||
*data = (uint8_t *)current_hid_class->hid_descriptor;
|
||||
*len = current_hid_class->hid_descriptor[0];
|
||||
// *data = (uint8_t *)match_intf->hid_descriptor;
|
||||
// *len = match_intf->hid_descriptor[0];
|
||||
break;
|
||||
|
||||
case HID_DESCRIPTOR_TYPE_HID_REPORT:
|
||||
USB_LOG_INFO("get Report Descriptor\r\n");
|
||||
*data = (uint8_t *)current_hid_class->hid_report_descriptor;
|
||||
*len = current_hid_class->hid_report_descriptor_len;
|
||||
*data = (uint8_t *)match_intf->hid_report_descriptor;
|
||||
*len = match_intf->hid_report_descriptor_len;
|
||||
break;
|
||||
|
||||
case HID_DESCRIPTOR_TYPE_HID_PHYSICAL:
|
||||
@@ -93,50 +68,33 @@ static int hid_class_request_handler(struct usb_setup_packet *setup, uint8_t **d
|
||||
"bRequest 0x%02x\r\n",
|
||||
setup->bRequest);
|
||||
|
||||
struct usbd_hid *current_hid_class = NULL;
|
||||
usb_slist_t *i;
|
||||
uint8_t intf = LO_BYTE(setup->wIndex);
|
||||
|
||||
usb_slist_for_each(i, &usbd_hid_head)
|
||||
{
|
||||
struct usbd_hid *hid_class = usb_slist_entry(i, struct usbd_hid, list);
|
||||
if (hid_class->intf_num == intf) {
|
||||
current_hid_class = hid_class;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (current_hid_class == NULL) {
|
||||
return -2;
|
||||
}
|
||||
uint8_t intf_num = LO_BYTE(setup->wIndex);
|
||||
|
||||
switch (setup->bRequest) {
|
||||
case HID_REQUEST_GET_REPORT:
|
||||
current_hid_class->report = usbh_hid_get_report(intf, LO_BYTE(setup->wValue), HI_BYTE(setup->wValue)); /*report id ,report type*/
|
||||
*data = (uint8_t *)¤t_hid_class->report;
|
||||
/* report id ,report type */
|
||||
(*data)[0] = usbh_hid_get_report(intf_num, LO_BYTE(setup->wValue), HI_BYTE(setup->wValue));
|
||||
*len = 1;
|
||||
break;
|
||||
case HID_REQUEST_GET_IDLE:
|
||||
current_hid_class->idle_state = usbh_hid_get_idle(intf, LO_BYTE(setup->wValue));
|
||||
*data = (uint8_t *)¤t_hid_class->idle_state;
|
||||
(*data)[0] = usbh_hid_get_idle(intf_num, LO_BYTE(setup->wValue));
|
||||
*len = 1;
|
||||
break;
|
||||
case HID_REQUEST_GET_PROTOCOL:
|
||||
current_hid_class->protocol = usbh_hid_get_protocol(intf);
|
||||
*data = (uint8_t *)¤t_hid_class->protocol;
|
||||
(*data)[0] = usbh_hid_get_protocol(intf_num);
|
||||
*len = 1;
|
||||
break;
|
||||
case HID_REQUEST_SET_REPORT:
|
||||
usbh_hid_set_report(intf, LO_BYTE(setup->wValue), HI_BYTE(setup->wValue), *data, *len); /*report id ,report type,report,report len*/
|
||||
current_hid_class->report = **data;
|
||||
/* report id ,report type, report, report len */
|
||||
usbh_hid_set_report(intf_num, LO_BYTE(setup->wValue), HI_BYTE(setup->wValue), *data, *len);
|
||||
break;
|
||||
case HID_REQUEST_SET_IDLE:
|
||||
usbh_hid_set_idle(intf, LO_BYTE(setup->wValue), HI_BYTE(setup->wIndex)); /*report id ,duration*/
|
||||
current_hid_class->idle_state = HI_BYTE(setup->wIndex);
|
||||
/* report id, duration */
|
||||
usbh_hid_set_idle(intf_num, LO_BYTE(setup->wValue), HI_BYTE(setup->wIndex));
|
||||
break;
|
||||
case HID_REQUEST_SET_PROTOCOL:
|
||||
usbh_hid_set_protocol(intf, LO_BYTE(setup->wValue)); /*protocol*/
|
||||
current_hid_class->protocol = LO_BYTE(setup->wValue);
|
||||
/* protocol */
|
||||
usbh_hid_set_protocol(intf_num, LO_BYTE(setup->wValue));
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -147,60 +105,22 @@ static int hid_class_request_handler(struct usb_setup_packet *setup, uint8_t **d
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void hid_notify_handler(uint8_t event, void *arg)
|
||||
struct usbd_interface *usbd_hid_alloc_intf(const uint8_t *desc, uint32_t desc_len)
|
||||
{
|
||||
switch (event) {
|
||||
case USBD_EVENT_RESET:
|
||||
usbd_hid_reset();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int usbd_hid_alloc(uint8_t intf)
|
||||
{
|
||||
struct usbd_hid *hid_class = usb_malloc(sizeof(struct usbd_hid));
|
||||
|
||||
if (hid_class == NULL) {
|
||||
USB_LOG_ERR("no memory to alloc hid_class\r\n");
|
||||
return -1;
|
||||
struct usbd_interface *intf = usb_malloc(sizeof(struct usbd_interface));
|
||||
if (intf == NULL) {
|
||||
USB_LOG_ERR("no mem to alloc intf\r\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(hid_class, 0, sizeof(struct usbd_hid));
|
||||
hid_class->intf_num = intf;
|
||||
usb_slist_add_tail(&usbd_hid_head, &hid_class->list);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void usbd_hid_add_interface(usbd_class_t *devclass, usbd_interface_t *intf)
|
||||
{
|
||||
intf->class_handler = hid_class_request_handler;
|
||||
intf->custom_handler = hid_custom_request_handler;
|
||||
intf->vendor_handler = NULL;
|
||||
intf->notify_handler = hid_notify_handler;
|
||||
usbd_class_add_interface(devclass, intf);
|
||||
usbd_hid_alloc(intf->intf_num);
|
||||
}
|
||||
intf->notify_handler = NULL;
|
||||
|
||||
void usbd_hid_descriptor_register(uint8_t intf_num, const uint8_t *desc)
|
||||
{
|
||||
// usbd_hid_cfg.hid_descriptor = desc;
|
||||
}
|
||||
|
||||
void usbd_hid_report_descriptor_register(uint8_t intf_num, const uint8_t *desc, uint32_t desc_len)
|
||||
{
|
||||
usb_slist_t *i;
|
||||
usb_slist_for_each(i, &usbd_hid_head)
|
||||
{
|
||||
struct usbd_hid *hid_class = usb_slist_entry(i, struct usbd_hid, list);
|
||||
|
||||
if (hid_class->intf_num == intf_num) {
|
||||
hid_class->hid_report_descriptor = desc;
|
||||
hid_class->hid_report_descriptor_len = desc_len;
|
||||
return;
|
||||
}
|
||||
}
|
||||
intf->hid_report_descriptor = desc;
|
||||
intf->hid_report_descriptor_len = desc_len;
|
||||
return intf;
|
||||
}
|
||||
|
||||
__WEAK uint8_t usbh_hid_get_report(uint8_t intf, uint8_t report_id, uint8_t report_type)
|
||||
|
||||
@@ -12,8 +12,8 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Register api */
|
||||
void usbd_hid_add_interface(usbd_class_t *devclass, usbd_interface_t *intf);
|
||||
/* Alloc hid interface driver */
|
||||
struct usbd_interface *usbd_hid_alloc_intf(const uint8_t *desc, uint32_t desc_len);
|
||||
|
||||
/* Register desc api */
|
||||
void usbd_hid_descriptor_register(uint8_t intf_num, const uint8_t *desc);
|
||||
|
||||
Reference in New Issue
Block a user