[Components][Drivers][USB][HID]Update to common hid

This commit is contained in:
uestczyh222 2017-11-16 14:51:04 +08:00 committed by Bernard Xiong
parent 40d0ff9960
commit e365ff5473
4 changed files with 575 additions and 173 deletions

View File

@ -97,7 +97,9 @@ config RT_USING_WDT
bool "Using Watch Dog device drivers"
default n
config RT_USING_USB_HOST
menu "Using USB"
config RT_USING_USB_HOST
bool "Using USB host"
default n
@ -106,16 +108,32 @@ config RT_USING_USB_HOST
bool "Enable connected with Android by ADK USB"
default n
endif
config RT_USING_USB_DEVICE
config RT_USING_USB_DEVICE
bool "Using USB device"
default n
if RT_USING_USB_DEVICE
config RT_USB_DEVICE_COMPOSITE
bool "Enable composite device"
default n
choice
prompt "Device type"
default _RT_USB_DEVICE_CDC
depends on !RT_USB_DEVICE_COMPOSITE
config _RT_USB_DEVICE_CDC
bool "Enable to use device as CDC device"
select RT_USB_DEVICE_CDC
config _RT_USB_DEVICE_MSTORAGE
bool "Enable to use device as Mass Storage device"
select RT_USB_DEVICE_MSTORAGE
config _RT_USB_DEVICE_HID
bool "Enable to use device as HID device"
select RT_USB_DEVICE_HID
endchoice
if RT_USB_DEVICE_COMPOSITE
config RT_USB_DEVICE_CDC
bool "Enable to use device as CDC device"
default n
default y
config RT_USB_DEVICE_MSTORAGE
bool "Enable to use device as Mass Storage device"
default n
@ -123,4 +141,38 @@ config RT_USING_USB_DEVICE
bool "Enable to use device as HID device"
default n
endif
if RT_USB_DEVICE_HID
config RT_USB_DEVICE_HID_KEYBOARD
bool "Use to HID device as Keyboard"
default n
if RT_USB_DEVICE_HID_KEYBOARD
config RT_USB_DEVICE_HID_KEYBOARD_NUMBER
int "Number of Keyboard(max 3)"
default 1
range 1 3
endif
config RT_USB_DEVICE_HID_MOUSE
bool "Use to HID device as Mouse"
default n
config RT_USB_DEVICE_HID_GENERAL
bool "Use to HID device as General HID device"
default y
if RT_USB_DEVICE_HID_GENERAL
config RT_USB_DEVICE_HID_GENERAL_OUT_REPORT_LENGTH
int "General HID device out report length"
default 63
range 0 63
config RT_USB_DEVICE_HID_GENERAL_IN_REPORT_LENGTH
int "General HID device in report length"
default 63
range 0 63
endif
config RT_USB_DEVICE_HID_MEDIA
bool "Use to HID device as media keyboard"
default y
endif
endif
endmenu
endmenu

View File

@ -218,6 +218,27 @@ extern "C" {
#define USB_EP_DESC_NUM(addr) (addr & USB_EP_DESC_NUM_MASK)
#define USB_EP_DIR(addr) ((addr & USB_DIR_MASK)>>7)
#ifdef RT_USB_DEVICE_HID
#ifdef RT_USB_DEVICE_HID_KEYBOARD
#define HID_REPORT_ID_KEYBOARD1 1
#if RT_USB_DEVICE_HID_KEYBOARD_NUMBER>1
#define HID_REPORT_ID_KEYBOARD2 2
#if RT_USB_DEVICE_HID_KEYBOARD_NUMBER>2
#define HID_REPORT_ID_KEYBOARD3 3
#endif
#endif
#endif
#ifdef RT_USB_DEVICE_HID_MEDIA
#define HID_REPORT_ID_MEDIA 4
#endif
#ifdef RT_USB_DEVICE_HID_GENERAL
#define HID_REPORT_ID_GENERAL 5
#endif
#ifdef RT_USB_DEVICE_HID_MOUSE
#define HID_REPORT_ID_MOUSE 6
#endif
#endif
#define uswap_32(x) \
((((x) & 0xff000000) >> 24) | \
(((x) & 0x00ff0000) >> 8) | \
@ -372,6 +393,8 @@ struct usb_qualifier_descriptor
#ifndef HID_SUB_DESCRIPTOR_MAX
#define HID_SUB_DESCRIPTOR_MAX 1
#endif
#ifdef RT_USB_DEVICE_HID
struct uhid_descriptor
{
rt_uint8_t bLength;
@ -387,6 +410,15 @@ struct uhid_descriptor
};
typedef struct uhid_descriptor* uhid_desc_t;
struct hid_report
{
rt_uint8_t report_id;
rt_uint8_t report[63];
rt_uint8_t size;
};
typedef struct hid_report* hid_report_t;
extern void HID_Report_Received(hid_report_t report);
#endif
struct urequest
{
rt_uint8_t request_type;

View File

@ -4,7 +4,8 @@
*
* Change Logs:
* Date Author Notes
* 2017Äê3ÔÂ13ÈÕ Urey the first version
* 2017-03-13 Urey the first version
* 2017-11-16 ZYH Update to common hid
*/
#include <rthw.h>
@ -21,132 +22,191 @@
struct hid_s
{
struct rt_device parent;
struct ufunction *func;
uep_t ep_in;
uep_t ep_out;
int status;
rt_uint16_t protocol;
rt_uint8_t report_buf[MAX_REPORT_SIZE];
struct rt_messagequeue hid_mq;
};
/* CustomHID_ConfigDescriptor */
const rt_uint8_t _report_desc[] __attribute__ ((packed))=
const rt_uint8_t _report_desc[]=
{
0x06, 0xFF, 0x00, /* USAGE_PAGE (Vendor Page: 0xFF00) */
0x09, 0x01, /* USAGE (Demo Kit) */
0xa1, 0x01, /* COLLECTION (Application) */
/* 6 */
#ifdef RT_USB_DEVICE_HID_KEYBOARD
USAGE_PAGE(1), 0x01,
USAGE(1), 0x06,
COLLECTION(1), 0x01,
REPORT_ID(1), HID_REPORT_ID_KEYBOARD1,
/* Led 1 */
0x85, 0x01, /* REPORT_ID (1) */
0x09, 0x01, /* USAGE (LED 1) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x01, /* REPORT_COUNT (1) */
0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
USAGE_PAGE(1), 0x07,
USAGE_MINIMUM(1), 0xE0,
USAGE_MAXIMUM(1), 0xE7,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0x01,
REPORT_SIZE(1), 0x01,
REPORT_COUNT(1), 0x08,
INPUT(1), 0x02,
REPORT_COUNT(1), 0x01,
REPORT_SIZE(1), 0x08,
INPUT(1), 0x01,
0x85, 0x01, /* REPORT_ID (1) */
0x09, 0x01, /* USAGE (LED 1) */
0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
/* 26 */
/* Led 2 */
0x85, 0x02, /* REPORT_ID 2 */
0x09, 0x02, /* USAGE (LED 2) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x01, /* REPORT_COUNT (1) */
0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
REPORT_COUNT(1), 0x05,
REPORT_SIZE(1), 0x01,
USAGE_PAGE(1), 0x08,
USAGE_MINIMUM(1), 0x01,
USAGE_MAXIMUM(1), 0x05,
OUTPUT(1), 0x02,
REPORT_COUNT(1), 0x01,
REPORT_SIZE(1), 0x03,
OUTPUT(1), 0x01,
0x85, 0x02, /* REPORT_ID (2) */
0x09, 0x02, /* USAGE (LED 2) */
0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
/* 46 */
/* Led 3 */
0x85, 0x03, /* REPORT_ID (3) */
0x09, 0x03, /* USAGE (LED 3) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x01, /* REPORT_COUNT (1) */
0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
REPORT_COUNT(1), 0x06,
REPORT_SIZE(1), 0x08,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0x65,
USAGE_PAGE(1), 0x07,
USAGE_MINIMUM(1), 0x00,
USAGE_MAXIMUM(1), 0x65,
INPUT(1), 0x00,
END_COLLECTION(0),
#if RT_USB_DEVICE_HID_KEYBOARD_NUMBER>1
/****keyboard2*****/
USAGE_PAGE(1), 0x01,
USAGE(1), 0x06,
COLLECTION(1), 0x01,
REPORT_ID(1), HID_REPORT_ID_KEYBOARD2,
0x85, 0x03, /* REPORT_ID (3) */
0x09, 0x03, /* USAGE (LED 3) */
0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
/* 66 */
USAGE_PAGE(1), 0x07,
USAGE_MINIMUM(1), 0xE0,
USAGE_MAXIMUM(1), 0xE7,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0x01,
REPORT_SIZE(1), 0x01,
REPORT_COUNT(1), 0x08,
INPUT(1), 0x02,
REPORT_COUNT(1), 0x01,
REPORT_SIZE(1), 0x08,
INPUT(1), 0x01,
/* Led 4 */
0x85, 0x04, /* REPORT_ID 4) */
0x09, 0x04, /* USAGE (LED 4) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x01, /* REPORT_COUNT (1) */
0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
REPORT_COUNT(1), 0x06,
REPORT_SIZE(1), 0x08,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0x65,
USAGE_PAGE(1), 0x07,
USAGE_MINIMUM(1), 0x00,
USAGE_MAXIMUM(1), 0x65,
INPUT(1), 0x00,
END_COLLECTION(0),
#if RT_USB_DEVICE_HID_KEYBOARD_NUMBER>2
USAGE_PAGE(1), 0x01,
USAGE(1), 0x06,
COLLECTION(1), 0x01,
REPORT_ID(1), HID_REPORT_ID_KEYBOARD3,
0x85, 0x04, /* REPORT_ID (4) */
0x09, 0x04, /* USAGE (LED 4) */
0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
/* 86 */
USAGE_PAGE(1), 0x07,
USAGE_MINIMUM(1), 0xE0,
USAGE_MAXIMUM(1), 0xE7,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0x01,
REPORT_SIZE(1), 0x01,
REPORT_COUNT(1), 0x08,
INPUT(1), 0x02,
REPORT_COUNT(1), 0x01,
REPORT_SIZE(1), 0x08,
INPUT(1), 0x01,
/* key Push Button */
0x85, 0x05, /* REPORT_ID (5) */
0x09, 0x05, /* USAGE (Push Button) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */
REPORT_COUNT(1), 0x06,
REPORT_SIZE(1), 0x08,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0x65,
USAGE_PAGE(1), 0x07,
USAGE_MINIMUM(1), 0x00,
USAGE_MAXIMUM(1), 0x65,
INPUT(1), 0x00,
END_COLLECTION(0),
#endif
#endif
#endif
// Media Control
#ifdef RT_USB_DEVICE_HID_MEDIA
USAGE_PAGE(1), 0x0C,
USAGE(1), 0x01,
COLLECTION(1), 0x01,
REPORT_ID(1), HID_REPORT_ID_MEDIA,
USAGE_PAGE(1), 0x0C,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0x01,
REPORT_SIZE(1), 0x01,
REPORT_COUNT(1), 0x07,
USAGE(1), 0xB5, // Next Track
USAGE(1), 0xB6, // Previous Track
USAGE(1), 0xB7, // Stop
USAGE(1), 0xCD, // Play / Pause
USAGE(1), 0xE2, // Mute
USAGE(1), 0xE9, // Volume Up
USAGE(1), 0xEA, // Volume Down
INPUT(1), 0x02, // Input (Data, Variable, Absolute)
REPORT_COUNT(1), 0x01,
INPUT(1), 0x01,
END_COLLECTION(0),
#endif
0x09, 0x05, /* USAGE (Push Button) */
0x75, 0x01, /* REPORT_SIZE (1) */
0xb1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
#ifdef RT_USB_DEVICE_HID_GENERAL
USAGE_PAGE(1), 0x8c,
USAGE(1), 0x01,
COLLECTION(1), 0x01,
REPORT_ID(1), HID_REPORT_ID_GENERAL,
0x75, 0x07, /* REPORT_SIZE (7) */
0x81, 0x83, /* INPUT (Cnst,Var,Abs,Vol) */
0x85, 0x05, /* REPORT_ID (2) */
REPORT_COUNT(1), RT_USB_DEVICE_HID_GENERAL_IN_REPORT_LENGTH,
USAGE(1), 0x03,
REPORT_SIZE(1), 0x08,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0xFF,
INPUT(1), 0x02,
0x75, 0x07, /* REPORT_SIZE (7) */
0xb1, 0x83, /* FEATURE (Cnst,Var,Abs,Vol) */
/* 114 */
/* Tamper Push Button */
0x85, 0x06, /* REPORT_ID (6) */
0x09, 0x06, /* USAGE (Tamper Push Button) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */
0x09, 0x06, /* USAGE (Tamper Push Button) */
0x75, 0x01, /* REPORT_SIZE (1) */
0xb1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
0x75, 0x07, /* REPORT_SIZE (7) */
0x81, 0x83, /* INPUT (Cnst,Var,Abs,Vol) */
0x85, 0x06, /* REPORT_ID (6) */
0x75, 0x07, /* REPORT_SIZE (7) */
0xb1, 0x83, /* FEATURE (Cnst,Var,Abs,Vol) */
/* 142 */
/* ADC IN */
0x85, 0x07, /* REPORT_ID (7) */
0x09, 0x07, /* USAGE (ADC IN) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */
0x85, 0x07, /* REPORT_ID (7) */
0x09, 0x07, /* USAGE (ADC in) */
0xb1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
/* 161 */
0xc0 /* END_COLLECTION */
REPORT_COUNT(1), RT_USB_DEVICE_HID_GENERAL_OUT_REPORT_LENGTH,
USAGE(1), 0x04,
REPORT_SIZE(1), 0x08,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0xFF,
OUTPUT(1), 0x02,
END_COLLECTION(0),
#endif
#ifdef RT_USB_DEVICE_HID_MOUSE
USAGE_PAGE(1), 0x01, // Generic Desktop
USAGE(1), 0x02, // Mouse
COLLECTION(1), 0x01, // Application
USAGE(1), 0x01, // Pointer
COLLECTION(1), 0x00, // Physical
REPORT_ID(1), HID_REPORT_ID_MOUSE,
REPORT_COUNT(1), 0x03,
REPORT_SIZE(1), 0x01,
USAGE_PAGE(1), 0x09, // Buttons
USAGE_MINIMUM(1), 0x1,
USAGE_MAXIMUM(1), 0x3,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0x01,
INPUT(1), 0x02,
REPORT_COUNT(1), 0x01,
REPORT_SIZE(1), 0x05,
INPUT(1), 0x01,
REPORT_COUNT(1), 0x03,
REPORT_SIZE(1), 0x08,
USAGE_PAGE(1), 0x01,
USAGE(1), 0x30, // X
USAGE(1), 0x31, // Y
USAGE(1), 0x38, // scroll
LOGICAL_MINIMUM(1), 0x81,
LOGICAL_MAXIMUM(1), 0x7f,
INPUT(1), 0x06,
END_COLLECTION(0),
END_COLLECTION(0),
#endif
}; /* CustomHID_ReportDescriptor */
static struct udevice_descriptor _dev_desc =
@ -189,8 +249,18 @@ const static struct uhid_comm_descriptor _hid_comm_desc =
USB_DYNAMIC,
0x02,
0x03, /* bInterfaceClass: HID */
#if defined(RT_USB_DEVICE_HID_KEYBOARD)||defined(RT_USB_DEVICE_HID_MOUSE)
USB_HID_SUBCLASS_BOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
#else
USB_HID_SUBCLASS_NOBOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
#endif
#if !defined(RT_USB_DEVICE_HID_KEYBOARD)||!defined(RT_USB_DEVICE_HID_MOUSE)||!defined(RT_USB_DEVICE_HID_MEDIA)
USB_HID_PROTOCOL_NONE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
#elif !defined(RT_USB_DEVICE_HID_MOUSE)
USB_HID_PROTOCOL_KEYBOARD, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
#else
USB_HID_PROTOCOL_MOUSE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
#endif
0x00,
#endif
@ -201,8 +271,18 @@ const static struct uhid_comm_descriptor _hid_comm_desc =
0x00, /* bAlternateSetting: Alternate setting */
0x02, /* bNumEndpoints */
0x03, /* bInterfaceClass: HID */
#if defined(RT_USB_DEVICE_HID_KEYBOARD)||defined(RT_USB_DEVICE_HID_MOUSE)
USB_HID_SUBCLASS_BOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
#else
USB_HID_SUBCLASS_NOBOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
#endif
#if !defined(RT_USB_DEVICE_HID_KEYBOARD)||!defined(RT_USB_DEVICE_HID_MOUSE)||!defined(RT_USB_DEVICE_HID_MEDIA)
USB_HID_PROTOCOL_NONE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
#elif !defined(RT_USB_DEVICE_HID_MOUSE)
USB_HID_PROTOCOL_KEYBOARD, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
#else
USB_HID_PROTOCOL_MOUSE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
#endif
0, /* iInterface: Index of string descriptor */
/* HID Descriptor */
@ -219,16 +299,16 @@ const static struct uhid_comm_descriptor _hid_comm_desc =
USB_DESC_TYPE_ENDPOINT,
USB_DYNAMIC | USB_DIR_IN,
USB_EP_ATTR_INT,
0x08,
0x20,
0x40,
0x01,
/* Endpoint Descriptor OUT */
USB_DESC_LENGTH_ENDPOINT,
USB_DESC_TYPE_ENDPOINT,
USB_DYNAMIC | USB_DIR_OUT,
USB_EP_ATTR_INT,
0x08,
0x20,
0x40,
0x01,
};
@ -242,39 +322,40 @@ const static char* _ustring[] =
"Interface",
};
#define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
static void dump_hex(const rt_uint8_t *ptr, rt_size_t buflen)
static void dump_data(uint8_t *data, rt_size_t size)
{
unsigned char *buf = (unsigned char*)ptr;
int i, j;
for (i=0; i<buflen; i+=16)
rt_size_t i;
for (i = 0; i < size; i++)
{
rt_kprintf("%02x ", *data++);
if ((i + 1) % 8 == 0)
{
rt_kprintf("%06x: ", i);
for (j=0; j<16; j++)
if (i+j < buflen)
rt_kprintf("%02x ", buf[i+j]);
else
rt_kprintf(" ");
rt_kprintf(" ");
for (j=0; j<16; j++)
if (i+j < buflen)
rt_kprintf("%c", __is_print(buf[i+j]) ? buf[i+j] : '.');
rt_kprintf("\n");
}else if ((i + 1) % 4 == 0){
rt_kprintf(" ");
}
}
}
static void dump_report(struct hid_report * report)
{
rt_kprintf("\nHID Recived:");
rt_kprintf("\nReport ID %02x \n", report->report_id);
dump_data(report->report,report->size);
}
static rt_err_t _ep_out_handler(ufunction_t func, rt_size_t size)
{
struct hid_s *data;
struct hid_report report;
RT_ASSERT(func != RT_NULL);
RT_ASSERT(func->device != RT_NULL);
rt_kprintf("%s size = %d\n",__func__,size);
data = (struct hid_s *) func->user_data;
if(size != 0)
{
dump_hex(data->ep_out->buffer,size);
rt_memcpy((void *)&report,(void*)data->ep_out->buffer,size);
report.size = size-1;
rt_mq_send(&data->hid_mq,(void *)&report,sizeof(report));
}
data->ep_out->request.buffer = data->ep_out->buffer;
@ -301,8 +382,6 @@ static rt_err_t _hid_set_report_callback(udevice_t device, rt_size_t size)
if(size != 0)
{
rt_kprintf("HID set report callback:\n");
dump_hex(device->dcd->ep0.request.buffer - size,size);
}
dcd_ep0_send_status(device->dcd);
@ -334,20 +413,15 @@ static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
case USB_REQ_GET_DESCRIPTOR:
if((setup->wValue >> 8) == USB_DESC_TYPE_REPORT)
{
rt_kprintf("HID get report descriptor\n");
rt_kprintf("sizeof _report_desc = %d bytes\n",sizeof(_report_desc));
rt_usbd_ep0_write(func->device, (void *)(&_report_desc[0]), sizeof(_report_desc));
}
else if((setup->wValue >> 8) == USB_DESC_TYPE_HID)
{
rt_kprintf("HID get hid descriptor\n");
rt_usbd_ep0_write(func->device, (void *)(&_hid_comm_desc.hid_desc), sizeof(struct uhid_descriptor));
}
break;
case USB_HID_REQ_GET_REPORT:
rt_kprintf("HID get report \n");
rt_kprintf("wLength = %d\n",setup->wLength);
if(setup->wLength == 0)
{
rt_usbd_ep0_set_stall(func->device);
@ -358,17 +432,13 @@ static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
rt_usbd_ep0_write(func->device, data->report_buf,setup->wLength);
break;
case USB_HID_REQ_GET_IDLE:
rt_kprintf("HID get idle \n");
dcd_ep0_send_status(func->device->dcd);
break;
case USB_HID_REQ_GET_PROTOCOL:
rt_kprintf("HID get protocol \n");
rt_usbd_ep0_write(func->device, &data->protocol,2);
break;
case USB_HID_REQ_SET_REPORT:
rt_kprintf("HID set report \n");
rt_kprintf("set report size = %d\n",setup->wLength);
if((setup->wLength == 0) || (setup->wLength > MAX_REPORT_SIZE))
rt_usbd_ep0_set_stall(func->device);
@ -379,14 +449,11 @@ static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
{
int duration = (setup->wValue >> 8);
int report_id = (setup->wValue & 0xFF);
rt_kprintf("HID set idle \n");
rt_kprintf("duration = %d,report_id = %d\n",duration,report_id);
dcd_ep0_send_status(func->device->dcd);
}
break;
case USB_HID_REQ_SET_PROTOCOL:
rt_kprintf("HID set protocol \n");
data->protocol = setup->wValue;
dcd_ep0_send_status(func->device->dcd);
@ -478,10 +545,59 @@ static rt_err_t _hid_descriptor_config(uhid_comm_desc_t hid, rt_uint8_t cintf_nr
return RT_EOK;
}
static rt_size_t _hid_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
struct hid_s *hiddev = (struct hid_s *)dev;
struct hid_report report;
if (hiddev->func->device->state == USB_STATE_CONFIGURED)
{
report.report_id = pos;
rt_memcpy((void *)report.report,(void *)buffer,size);
report.size = size;
hiddev->ep_in->request.buffer = (void *)&report;
hiddev->ep_in->request.size = (size+1) > 64 ? 64 : size+1;
hiddev->ep_in->request.req_type = UIO_REQUEST_WRITE;
rt_usbd_io_request(hiddev->func->device, hiddev->ep_in, &hiddev->ep_in->request);
return size;
}
return 0;
}
RT_WEAK void HID_Report_Received(hid_report_t report)
{
dump_report(report);
}
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t hid_thread_stack[RT_USBD_THREAD_STACK_SZ];
static struct rt_thread hid_thread;
static void hid_thread_entry(void* parameter)
{
struct hid_report report;
struct hid_s *hiddev;
hiddev = (struct hid_s *)parameter;
while(1)
{
if(rt_mq_recv(&hiddev->hid_mq, &report, sizeof(report),RT_WAITING_FOREVER) != RT_EOK )
continue;
HID_Report_Received(&report);
}
}
static rt_uint8_t hid_mq_pool[(sizeof(struct hid_report)+sizeof(void*))*32];
static void rt_usb_hid_init(struct ufunction *func)
{
struct hid_s *hiddev;
hiddev = (struct hid_s *)func->user_data;
rt_memset(&hiddev->parent, 0, sizeof(hiddev->parent));
hiddev->parent.write = _hid_write;
rt_device_register(&hiddev->parent, "hidd", RT_DEVICE_FLAG_RDWR);
rt_mq_init(&hiddev->hid_mq, "hiddmq", hid_mq_pool, sizeof(struct hid_report),
sizeof(hid_mq_pool), RT_IPC_FLAG_FIFO);
rt_thread_init(&hid_thread, "hidd", hid_thread_entry, hiddev,
hid_thread_stack, RT_USBD_THREAD_STACK_SZ, RT_USBD_THREAD_PRIO, 20);
rt_thread_startup(&hid_thread);
}
@ -516,9 +632,6 @@ ufunction_t rt_usbd_function_hid_create(udevice_t device)
rt_memset(data, 0, sizeof(struct hid_s));
func->user_data = (void*)data;
/* initilize hid */
rt_usb_hid_init(func);
/* create an interface object */
hid_intf = rt_usbd_interface_new(device, _interface_handler);
@ -546,6 +659,9 @@ ufunction_t rt_usbd_function_hid_create(udevice_t device)
/* add the interface to the mass storage function */
rt_usbd_function_add_interface(func, hid_intf);
/* initilize hid */
rt_usb_hid_init(func);
return func;
}

View File

@ -4,7 +4,8 @@
*
* Change Logs:
* Date Author Notes
* 2017313 Urey the first version
* 2017-03-13 Urey the first version
* 2017-11-16 ZYH Update to common hid
*/
#ifndef _USBDEVICE_CLASS_HID_H_
#define _USBDEVICE_CLASS_HID_H_
@ -32,9 +33,210 @@ extern "C" {
#define USB_HID_REQ_SET_IDLE 0x0a
#define USB_HID_REQ_SET_PROTOCOL 0x0b
#define MAX_REPORT_SIZE 8
#define MAX_REPORT_SIZE 64
#define HID_RX_BUFSIZE 64
/* HID Report Types */
#define HID_REPORT_INPUT 0x01
#define HID_REPORT_OUTPUT 0x02
#define HID_REPORT_FEATURE 0x03
/* Usage Pages */
#define USAGEPAGE_UNDEFINED 0x00
#define USAGEPAGE_GENERIC 0x01
#define USAGEPAGE_SIMULATION 0x02
#define USAGEPAGE_VR 0x03
#define USAGEPAGE_SPORT 0x04
#define USAGEPAGE_GAME 0x05
#define USAGEPAGE_DEV_CONTROLS 0x06
#define USAGEPAGE_KEYBOARD 0x07
#define USAGEPAGE_LED 0x08
#define USAGEPAGE_BUTTON 0x09
#define USAGEPAGE_ORDINAL 0x0A
#define USAGEPAGE_TELEPHONY 0x0B
#define USAGEPAGE_CONSUMER 0x0C
#define USAGEPAGE_DIGITIZER 0x0D
#define USAGEPAGE_PIDPAGE 0x0F
#define USAGEPAGE_UNICODE 0x10
#define USAGEPAGE_ALPHANUMERIC 0x14
#define USAGEPAGE_BARCODESCANNER 0x8C
/* Generic Desktop Page (0x01) */
#define USAGE_GENERIC_POINTER 0x01
#define USAGE_GENERIC_MOUSE 0x02
#define USAGE_GENERIC_JOYSTICK 0x04
#define USAGE_GENERIC_GAMEPAD 0x05
#define USAGE_GENERIC_KEYBOARD 0x06
#define USAGE_GENERIC_KEYPAD 0x07
#define USAGE_GENERIC_X 0x30
#define USAGE_GENERIC_Y 0x31
#define USAGE_GENERIC_Z 0x32
#define USAGE_GENERIC_RX 0x33
#define USAGE_GENERIC_RY 0x34
#define USAGE_GENERIC_RZ 0x35
#define USAGE_GENERIC_SLIDER 0x36
#define USAGE_GENERIC_DIAL 0x37
#define USAGE_GENERIC_WHEEL 0x38
#define USAGE_GENERIC_HATSWITCH 0x39
#define USAGE_GENERIC_COUNTED_BUFFER 0x3A
#define USAGE_GENERIC_BYTE_COUNT 0x3B
#define USAGE_GENERIC_MOTION_WAKEUP 0x3C
#define USAGE_GENERIC_VX 0x40
#define USAGE_GENERIC_VY 0x41
#define USAGE_GENERIC_VZ 0x42
#define USAGE_GENERIC_VBRX 0x43
#define USAGE_GENERIC_VBRY 0x44
#define USAGE_GENERIC_VBRZ 0x45
#define USAGE_GENERIC_VNO 0x46
#define USAGE_GENERIC_SYSTEM_CTL 0x80
#define USAGE_GENERIC_SYSCTL_POWER 0x81
#define USAGE_GENERIC_SYSCTL_SLEEP 0x82
#define USAGE_GENERIC_SYSCTL_WAKE 0x83
#define USAGE_GENERIC_SYSCTL_CONTEXT_MENU 0x84
#define USAGE_GENERIC_SYSCTL_MAIN_MENU 0x85
#define USAGE_GENERIC_SYSCTL_APP_MENU 0x86
#define USAGE_GENERIC_SYSCTL_HELP_MENU 0x87
#define USAGE_GENERIC_SYSCTL_MENU_EXIT 0x88
#define USAGE_GENERIC_SYSCTL_MENU_SELECT 0x89
#define USAGE_GENERIC_SYSCTL_MENU_RIGHT 0x8A
#define USAGE_GENERIC_SYSCTL_MENU_LEFT 0x8B
#define USAGE_GENERIC_SYSCTL_MENU_UP 0x8C
#define USAGE_GENERIC_SYSCTL_MENU_DOWN 0x8D
/* Simulation Controls Page(0x02) */
#define USAGE_SIMCTRL_THROTTLE 0xBB
/* HID Report Items */
/* Main Items */
#define HID_Input(x) 0x81,x
#define HID_Output(x) 0x91,x
#define HID_Feature(x) 0xB1,x
#define HID_Collection(x) 0xA1,x
#define HID_EndCollection() 0xC0
/* Local Items */
#define HID_Usage(x) 0x09,x
#define HID_UsageMin(x) 0x19,x
#define HID_UsageMax(x) 0x29,x
/* Global Items */
#define HID_UsagePage(x) 0x05,x
#define HID_UsagePageVendor(x) 0x06,x,0xFF
#define HID_LogicalMin(x) 0x15,x
#define HID_LogicalMinS(x) 0x16,(x&0xFF),((x>>8)&0xFF)
#define HID_LogicalMinL(x) 0x17,(x&0xFF),((x>>8)&0xFF),((x>>16)&0xFF),((x>>24)&0xFF)
#define HID_LogicalMax(x) 0x25,x
#define HID_LogicalMaxS(x) 0x26,(x&0xFF),((x>>8)&0xFF)
#define HID_LogicalMaxL(x) 0x27,(x&0xFF),((x>>8)&0xFF),((x>>16)&0xFF),((x>>24)&0xFF)
#define HID_PhysicalMin(x) 0x35,x
#define HID_PhysicalMinS(x) 0x36,(x&0xFF),((x>>8)&0xFF)
#define HID_PhysicalMinL(x) 0x37,(x&0xFF),((x>>8)&0xFF),((x>>16)&0xFF),((x>>24)&0xFF)
#define HID_PhysicalMax(x) 0x45,x
#define HID_PhysicalMaxS(x) 0x46,(x&0xFF),((x>>8)&0xFF)
#define HID_PhysicalMaxL(x) 0x47,(x&0xFF),((x>>8)&0xFF),((x>>16)&0xFF),((x>>24)&0xFF)
#define HID_UnitExponent(x) 0x55,x
#define HID_Unit(x) 0x65,x
#define HID_UnitS(x) 0x66,(x&0xFF),((x>>8)&0xFF)
#define HID_UnitL(x) 0x67,(x&0xFF),((x>>8)&0xFF),((x>>16)&0xFF),((x>>24)&0xFF)
#define HID_ReportSize(x) 0x75,x
#define HID_ReportSizeS(x) 0x76,(x&0xFF),((x>>8)&0xFF))
#define HID_ReportSizeL(x) 0x77,(x&0xFF),((x>>8)&0xFF),((x>>16)&0xFF),((x>>24)&0xFF)
#define HID_ReportID(x) 0x85,x
#define HID_ReportCount(x) 0x95,x
#define HID_ReportCountS(x) 0x96,(x&0xFF),((x>>8)&0xFF)
#define HID_ReportCountL(x) 0x97,(x&0xFF),((x>>8)&0xFF),((x>>16)&0xFF),((x>>24)&0xFF)
#define HID_Push() 0xA4
#define HID_Pop() 0xB4
/* Input, Output, Feature Data */
#define HID_DATA (0<<0)
#define HID_CONST (1<<0)
#define HID_ARRAY (0<<1)
#define HID_VAR (1<<1)
#define HID_ABS (0<<2)
#define HID_REL (1<<2)
#define HID_NOWRAP (0<<3)
#define HID_WRAP (1<<3)
#define HID_LINEAR (0<<4)
#define HID_NONLINEAR (1<<4)
#define HID_PREFERREDSTATE (0<<5)
#define HID_NOPREFERRED (1<<5)
#define HID_NONULLPOSITION (0<<6)
#define HID_NULLSTATE (1<<6)
#define HID_NONVOLATILE (0<<7)
#define HID_VOLATILE (1<<7)
/* Collection Data */
#define HID_PHYSICAL 0x00
#define HID_APPLICATION 0x01
#define HID_LOGICAL 0x02
#define HID_REPORT 0x03
#define HID_NAMEDARRAY 0x04
#define HID_USAGESWITCH 0x05
#define HID_USAGEMODIFIER 0x06
//HID_MBED_DEFINE
#define HID_VERSION_1_11 (0x0111)
/* HID Class */
#define HID_CLASS (3)
#define HID_SUBCLASS_NONE (0)
#define HID_SUBCLASS_BOOT (1)
#define HID_PROTOCOL_NONE (0)
#define HID_PROTOCOL_KEYBOARD (1)
#define HID_PROTOCOL_MOUSE (2)
/* Descriptors */
#define HID_DESCRIPTOR (33)
#define HID_DESCRIPTOR_LENGTH (0x09)
#define REPORT_DESCRIPTOR (34)
/* Class requests */
#define GET_REPORT (0x1)
#define GET_IDLE (0x2)
#define SET_REPORT (0x9)
#define SET_IDLE (0xa)
/* HID Class Report Descriptor */
/* Short items: size is 0, 1, 2 or 3 specifying 0, 1, 2 or 4 (four) bytes */
/* of data as per HID Class standard */
/* Main items */
#define INPUT(size) (0x80 | size)
#define OUTPUT(size) (0x90 | size)
#define FEATURE(size) (0xb0 | size)
#define COLLECTION(size) (0xa0 | size)
#define END_COLLECTION(size) (0xc0 | size)
/* Global items */
#define USAGE_PAGE(size) (0x04 | size)
#define LOGICAL_MINIMUM(size) (0x14 | size)
#define LOGICAL_MAXIMUM(size) (0x24 | size)
#define PHYSICAL_MINIMUM(size) (0x34 | size)
#define PHYSICAL_MAXIMUM(size) (0x44 | size)
#define UNIT_EXPONENT(size) (0x54 | size)
#define UNIT(size) (0x64 | size)
#define REPORT_SIZE(size) (0x74 | size)
#define REPORT_ID(size) (0x84 | size)
#define REPORT_COUNT(size) (0x94 | size)
#define PUSH(size) (0xa4 | size)
#define POP(size) (0xb4 | size)
/* Local items */
#define USAGE(size) (0x08 | size)
#define USAGE_MINIMUM(size) (0x18 | size)
#define USAGE_MAXIMUM(size) (0x28 | size)
#define DESIGNATOR_INDEX(size) (0x38 | size)
#define DESIGNATOR_MINIMUM(size) (0x48 | size)
#define DESIGNATOR_MAXIMUM(size) (0x58 | size)
#define STRING_INDEX(size) (0x78 | size)
#define STRING_MINIMUM(size) (0x88 | size)
#define STRING_MAXIMUM(size) (0x98 | size)
#define DELIMITER(size) (0xa8 | size)
#define LSB(n) ((n)&0xff)
#define MSB(n) (((n)&0xff00)>>8)
struct uhid_comm_descriptor
{
#ifdef RT_USB_DEVICE_COMPOSITE