Files
CherryUSB/class/hid/usbh_hid.c

221 lines
6.5 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 "usbh_core.h"
#include "usbh_hid.h"
#define DEV_FORMAT "/dev/input%d"
2022-02-08 11:44:46 +08:00
static uint32_t g_devinuse = 0;
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_hid_buf[128];
2022-02-08 11:44:46 +08:00
/****************************************************************************
* Name: usbh_hid_devno_alloc
*
* Description:
* Allocate a unique /dev/input[n] minor number in the range 0-31.
2022-02-08 11:44:46 +08:00
*
****************************************************************************/
static int usbh_hid_devno_alloc(struct usbh_hid *hid_class)
2022-02-08 11:44:46 +08:00
{
size_t flags;
2022-02-08 11:44:46 +08:00
int devno;
flags = usb_osal_enter_critical_section();
for (devno = 0; devno < 32; devno++) {
uint32_t bitno = 1 << devno;
if ((g_devinuse & bitno) == 0) {
g_devinuse |= bitno;
hid_class->minor = devno;
2022-02-08 11:44:46 +08:00
usb_osal_leave_critical_section(flags);
return 0;
}
}
usb_osal_leave_critical_section(flags);
return -EMFILE;
}
/****************************************************************************
* Name: usbh_hid_devno_free
*
* Description:
* Free a /dev/input[n] minor number so that it can be used.
2022-02-08 11:44:46 +08:00
*
****************************************************************************/
static void usbh_hid_devno_free(struct usbh_hid *hid_class)
2022-02-08 11:44:46 +08:00
{
int devno = hid_class->minor;
2022-02-08 11:44:46 +08:00
if (devno >= 0 && devno < 32) {
2022-04-01 23:09:03 +08:00
size_t flags = usb_osal_enter_critical_section();
2022-02-08 11:44:46 +08:00
g_devinuse &= ~(1 << devno);
usb_osal_leave_critical_section(flags);
}
}
static int usbh_hid_get_report_descriptor(struct usbh_hid *hid_class, uint8_t *buffer)
2022-02-08 11:44:46 +08:00
{
struct usb_setup_packet *setup = &hid_class->hport->setup;
int ret;
2022-02-08 11:44:46 +08:00
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
setup->wValue = HID_DESCRIPTOR_TYPE_HID_REPORT << 8;
setup->wIndex = hid_class->intf;
2022-02-08 11:44:46 +08:00
setup->wLength = 128;
ret = usbh_control_transfer(hid_class->hport->ep0, setup, g_hid_buf);
if (ret < 0) {
return ret;
}
memcpy(buffer, g_hid_buf, ret - 8);
return ret;
2022-02-08 11:44:46 +08:00
}
int usbh_hid_set_idle(struct usbh_hid *hid_class, uint8_t report_id, uint8_t duration)
2022-02-08 11:44:46 +08:00
{
struct usb_setup_packet *setup = &hid_class->hport->setup;
2022-02-08 11:44:46 +08:00
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = HID_REQUEST_SET_IDLE;
setup->wValue = report_id;
setup->wIndex = (duration << 8) | hid_class->intf;
2022-02-08 11:44:46 +08:00
setup->wLength = 0;
2022-06-16 22:23:43 +08:00
return usbh_control_transfer(hid_class->hport->ep0, setup, NULL);
2022-02-08 11:44:46 +08:00
}
int usbh_hid_get_idle(struct usbh_hid *hid_class, uint8_t *buffer)
2022-02-08 11:44:46 +08:00
{
struct usb_setup_packet *setup = &hid_class->hport->setup;
int ret;
2022-02-08 11:44:46 +08:00
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = HID_REQUEST_GET_IDLE;
setup->wValue = 0;
setup->wIndex = hid_class->intf;
2022-02-08 11:44:46 +08:00
setup->wLength = 1;
ret = usbh_control_transfer(hid_class->hport->ep0, setup, g_hid_buf);
if (ret < 0) {
return ret;
}
memcpy(buffer, g_hid_buf, 1);
return ret;
2022-02-08 11:44:46 +08:00
}
int usbh_hid_connect(struct usbh_hubport *hport, uint8_t intf)
{
struct usbh_endpoint_cfg ep_cfg = { 0 };
struct usb_endpoint_descriptor *ep_desc;
int ret;
struct usbh_hid *hid_class = usb_malloc(sizeof(struct usbh_hid));
if (hid_class == NULL) {
USB_LOG_ERR("Fail to alloc hid_class\r\n");
return -ENOMEM;
}
2022-02-08 11:44:46 +08:00
memset(hid_class, 0, sizeof(struct usbh_hid));
usbh_hid_devno_alloc(hid_class);
hid_class->hport = hport;
hid_class->intf = intf;
2022-02-08 11:44:46 +08:00
hport->config.intf[intf].priv = hid_class;
ret = usbh_hid_set_idle(hid_class, 0, 0);
2022-02-08 11:44:46 +08:00
if (ret < 0) {
return ret;
}
ret = usbh_hid_get_report_descriptor(hid_class, hid_class->report_desc);
2022-02-08 11:44:46 +08:00
if (ret < 0) {
return ret;
}
for (uint8_t i = 0; i < hport->config.intf[intf].intf_desc.bNumEndpoints; i++) {
ep_desc = &hport->config.intf[intf].ep[i].ep_desc;
ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize & USB_MAXPACKETSIZE_MASK;
2022-02-08 11:44:46 +08:00
ep_cfg.ep_interval = ep_desc->bInterval;
ep_cfg.hport = hport;
if (ep_desc->bEndpointAddress & 0x80) {
usbh_pipe_alloc(&hid_class->intin, &ep_cfg);
2022-02-08 11:44:46 +08:00
} else {
usbh_pipe_alloc(&hid_class->intout, &ep_cfg);
2022-02-08 11:44:46 +08:00
}
USB_LOG_INFO("Ep=%02x Attr=%02u Mps=%d Interval=%02u\r\n",
ep_desc->bEndpointAddress,
ep_desc->bmAttributes,
ep_desc->wMaxPacketSize,
ep_desc->bInterval);
2022-02-08 11:44:46 +08:00
}
snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, hid_class->minor);
USB_LOG_INFO("Register HID Class:%s\r\n", hport->config.intf[intf].devname);
2022-02-08 11:44:46 +08:00
return 0;
}
int usbh_hid_disconnect(struct usbh_hubport *hport, uint8_t intf)
{
int ret = 0;
struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
if (hid_class) {
usbh_hid_devno_free(hid_class);
if (hid_class->intin) {
usbh_pipe_free(hid_class->intin);
2022-02-08 11:44:46 +08:00
}
if (hid_class->intout) {
usbh_pipe_free(hid_class->intout);
2022-02-08 11:44:46 +08:00
}
memset(hid_class, 0, sizeof(struct usbh_hid));
2022-02-08 11:44:46 +08:00
usb_free(hid_class);
if (hport->config.intf[intf].devname[0] != '\0')
USB_LOG_INFO("Unregister HID Class:%s\r\n", hport->config.intf[intf].devname);
2022-02-08 11:44:46 +08:00
}
return ret;
}
const struct usbh_class_driver hid_class_driver = {
.driver_name = "hid",
.connect = usbh_hid_connect,
.disconnect = usbh_hid_disconnect
};
CLASS_INFO_DEFINE const struct usbh_class_info hid_keyboard_class_info = {
.match_flags = USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL,
.class = USB_DEVICE_CLASS_HID,
.subclass = HID_SUBCLASS_BOOTIF,
.protocol = HID_PROTOCOL_KEYBOARD,
.vid = 0x00,
.pid = 0x00,
.class_driver = &hid_class_driver
};
CLASS_INFO_DEFINE const struct usbh_class_info hid_mouse_class_info = {
.match_flags = USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL,
.class = USB_DEVICE_CLASS_HID,
.subclass = HID_SUBCLASS_BOOTIF,
.protocol = HID_PROTOCOL_MOUSE,
.vid = 0x00,
.pid = 0x00,
.class_driver = &hid_class_driver
2022-03-27 14:38:47 +08:00
};