Files
CherryUSB/class/hid/usbd_hid.c

151 lines
4.3 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"
#include "usbd_hid.h"
2022-02-12 18:49:06 +08:00
static int hid_custom_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
2022-02-08 11:44:46 +08:00
{
USB_LOG_DBG("HID Custom request: "
2022-02-12 18:49:06 +08:00
"bRequest 0x%02x\r\n",
setup->bRequest);
2022-02-08 11:44:46 +08:00
if (((setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_IN) &&
setup->bRequest == USB_REQUEST_GET_DESCRIPTOR) {
uint8_t value = (uint8_t)(setup->wValue >> 8);
uint8_t intf_num = (uint8_t)setup->wIndex;
usb_slist_t *i;
struct usbd_interface *match_intf = NULL;
usb_slist_for_each(i, &usbd_intf_head)
2022-02-08 11:44:46 +08:00
{
struct usbd_interface *intf = usb_slist_entry(i, struct usbd_interface, list);
2022-02-08 11:44:46 +08:00
if (intf->intf_num == intf_num) {
match_intf = intf;
2022-02-08 11:44:46 +08:00
break;
}
}
if (match_intf == NULL) {
2022-02-08 11:44:46 +08:00
return -2;
}
switch (value) {
case HID_DESCRIPTOR_TYPE_HID:
USB_LOG_INFO("get HID Descriptor\r\n");
// *data = (uint8_t *)match_intf->hid_descriptor;
// *len = match_intf->hid_descriptor[0];
2022-02-08 11:44:46 +08:00
break;
case HID_DESCRIPTOR_TYPE_HID_REPORT:
USB_LOG_INFO("get Report Descriptor\r\n");
*data = (uint8_t *)match_intf->hid_report_descriptor;
*len = match_intf->hid_report_descriptor_len;
2022-02-08 11:44:46 +08:00
break;
case HID_DESCRIPTOR_TYPE_HID_PHYSICAL:
USB_LOG_INFO("get PHYSICAL Descriptor\r\n");
break;
default:
return -2;
}
return 0;
}
return -1;
}
static int hid_class_interface_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
2022-02-08 11:44:46 +08:00
{
USB_LOG_DBG("HID Class request: "
2022-02-12 18:49:06 +08:00
"bRequest 0x%02x\r\n",
setup->bRequest);
2022-02-08 11:44:46 +08:00
uint8_t intf_num = LO_BYTE(setup->wIndex);
2022-02-08 11:44:46 +08:00
switch (setup->bRequest) {
case HID_REQUEST_GET_REPORT:
/* report id ,report type */
(*data)[0] = usbh_hid_get_report(intf_num, LO_BYTE(setup->wValue), HI_BYTE(setup->wValue));
2022-02-08 11:44:46 +08:00
*len = 1;
break;
case HID_REQUEST_GET_IDLE:
(*data)[0] = usbh_hid_get_idle(intf_num, LO_BYTE(setup->wValue));
2022-02-08 11:44:46 +08:00
*len = 1;
break;
case HID_REQUEST_GET_PROTOCOL:
(*data)[0] = usbh_hid_get_protocol(intf_num);
2022-02-08 11:44:46 +08:00
*len = 1;
break;
case HID_REQUEST_SET_REPORT:
/* report id ,report type, report, report len */
usbh_hid_set_report(intf_num, LO_BYTE(setup->wValue), HI_BYTE(setup->wValue), *data, *len);
2022-02-08 11:44:46 +08:00
break;
case HID_REQUEST_SET_IDLE:
/* report id, duration */
usbh_hid_set_idle(intf_num, LO_BYTE(setup->wValue), HI_BYTE(setup->wIndex));
2022-02-08 11:44:46 +08:00
break;
case HID_REQUEST_SET_PROTOCOL:
/* protocol */
usbh_hid_set_protocol(intf_num, LO_BYTE(setup->wValue));
2022-02-08 11:44:46 +08:00
break;
default:
USB_LOG_WRN("Unhandled HID Class bRequest 0x%02x\r\n", setup->bRequest);
return -1;
}
return 0;
}
struct usbd_interface *usbd_hid_alloc_intf(const uint8_t *desc, uint32_t desc_len)
2022-02-08 11:44:46 +08:00
{
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;
2022-02-08 11:44:46 +08:00
}
2022-06-28 20:38:06 +08:00
intf->class_interface_handler = hid_class_interface_request_handler;
intf->class_endpoint_handler = NULL;
2022-02-12 18:49:06 +08:00
intf->vendor_handler = NULL;
intf->notify_handler = NULL;
2022-02-08 11:44:46 +08:00
intf->hid_report_descriptor = desc;
intf->hid_report_descriptor_len = desc_len;
return intf;
2022-02-08 11:44:46 +08:00
}
2022-02-12 18:49:06 +08:00
2022-06-28 20:38:06 +08:00
__WEAK uint8_t usbh_hid_get_report(uint8_t intf, uint8_t report_id, uint8_t report_type)
2022-02-08 11:44:46 +08:00
{
2022-06-28 20:38:06 +08:00
return 0;
2022-02-08 11:44:46 +08:00
}
2022-06-28 20:38:06 +08:00
__WEAK uint8_t usbh_hid_get_idle(uint8_t intf, uint8_t report_id)
2022-02-08 11:44:46 +08:00
{
2022-06-28 20:38:06 +08:00
return 0;
2022-02-12 18:49:06 +08:00
}
2022-02-08 11:44:46 +08:00
2022-06-28 20:38:06 +08:00
__WEAK uint8_t usbh_hid_get_protocol(uint8_t intf)
2022-02-12 18:49:06 +08:00
{
2022-06-28 20:38:06 +08:00
return 0;
2022-02-08 11:44:46 +08:00
}
2022-06-28 20:38:06 +08:00
__WEAK void usbh_hid_set_report(uint8_t intf, uint8_t report_id, uint8_t report_type, uint8_t *report, uint8_t report_len)
{
}
__WEAK void usbh_hid_set_idle(uint8_t intf, uint8_t report_id, uint8_t duration)
{
}
__WEAK void usbh_hid_set_protocol(uint8_t intf, uint8_t protocol)
{
}