From 0c5d2ad72968facc35d080db40af41a1164ee135 Mon Sep 17 00:00:00 2001 From: sakumisu <1203593632@qq.com> Date: Sat, 8 Feb 2025 22:59:09 +0800 Subject: [PATCH] feat(class/hid): add HID_X_DESCRIPTOR_INIT macro Signed-off-by: sakumisu <1203593632@qq.com> --- class/hid/usb_hid.h | 158 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 122 insertions(+), 36 deletions(-) diff --git a/class/hid/usb_hid.h b/class/hid/usb_hid.h index b244d119..62525a74 100644 --- a/class/hid/usb_hid.h +++ b/class/hid/usb_hid.h @@ -191,11 +191,11 @@ #define HID_MOUSE_INPUT_REPORT_BUTTON3 (1 << 2) #define HID_MOUSE_INPUT_REPORT_BUTTON_MASK (7) -#define HID_MOUSE_INPUT_BUTTON_LEFT (1 << 0) -#define HID_MOUSE_INPUT_BUTTON_RIGHT (1 << 1) -#define HID_MOUSE_INPUT_BUTTON_MIDDLE (1 << 2) -#define HID_MOUSE_INPUT_BUTTON_BACKWARD (1 << 3) -#define HID_MOUSE_INPUT_BUTTON_FORWARD (1 << 4) +#define HID_MOUSE_INPUT_BUTTON_LEFT (1 << 0) +#define HID_MOUSE_INPUT_BUTTON_RIGHT (1 << 1) +#define HID_MOUSE_INPUT_BUTTON_MIDDLE (1 << 2) +#define HID_MOUSE_INPUT_BUTTON_BACKWARD (1 << 3) +#define HID_MOUSE_INPUT_BUTTON_FORWARD (1 << 4) /* Joystick input report (4 bytes) (HID D.1) */ #define HID_JS_INPUT_REPORT_HATSWITCH_SHIFT (0) @@ -241,14 +241,14 @@ #define HID_DESKTOP_USAGE_UNDEFINED 0x00 /* Undefined */ #define HID_DESKTOP_USAGE_POINTER 0x01 /* Pointer */ #define HID_DESKTOP_USAGE_MOUSE 0x02 /* Mouse */ - /* 0x03 Reserved */ + /* 0x03 Reserved */ #define HID_DESKTOP_USAGE_JOYSTICK 0x04 /* Joystick */ #define HID_DESKTOP_USAGE_GAMEPAD 0x05 /* Game Pad */ #define HID_DESKTOP_USAGE_KEYBOARD 0x06 /* Keyboard */ #define HID_DESKTOP_USAGE_KEYPAD 0x07 /* Keypad */ #define HID_DESKTOP_USAGE_MULTIAXIS 0x08 /* Multi-axis Controller */ #define HID_DESKTOP_USAGE_TABLET 0x09 /* Tablet PC System Controls */ - /* 0x0a-2f Reserved */ + /* 0x0a-2f Reserved */ #define HID_DESKTOP_USAGE_X 0x30 /* X */ #define HID_DESKTOP_USAGE_Y 0x31 /* Y */ #define HID_DESKTOP_USAGE_Z 0x32 /* Z */ @@ -264,7 +264,7 @@ #define HID_DESKTOP_USAGE_MOTION 0x3c /* Motion Wakeup */ #define HID_DESKTOP_USAGE_START 0x3d /* Start */ #define HID_DESKTOP_USAGE_SELECT 0x3e /* Select */ - /* 0x3f Reserved */ + /* 0x3f Reserved */ #define HID_DESKTOP_USAGE_VX 0x40 /* Vx */ #define HID_DESKTOP_USAGE_VY 0x41 /* Vy */ #define HID_DESKTOP_USAGE_VZ 0x42 /* Vz */ @@ -274,7 +274,7 @@ #define HID_DESKTOP_USAGE_VNO 0x46 /* Vno */ #define HID_DESKTOP_USAGE_FEATURE 0x47 /* Feature Notification */ #define HID_DESKTOP_USAGE_RESOLUTION 0x48 /* Resolution Multiplier */ - /* 0x49-7f Reserved */ + /* 0x49-7f Reserved */ #define HID_DESKTOP_USAGE_CONTROL 0x80 /* System Control */ #define HID_DESKTOP_USAGE_POWERDOWN 0x81 /* System Power Down */ #define HID_DESKTOP_USAGE_SLEEP 0x82 /* System Sleep */ @@ -295,7 +295,7 @@ #define HID_DESKTOP_USAGE_DPAD_DOWN 0x91 /* D-pad Down */ #define HID_DESKTOP_USAGE_DPAD_RIGHT 0x92 /* D-pad Right */ #define HID_DESKTOP_USAGE_DPAD_LEFT 0x93 /* D-pad Left */ - /* 0x94-9f Reserved */ + /* 0x94-9f Reserved */ #define HID_DESKTOP_USAGE_DOCK 0xa0 /* System Dock */ #define HID_DESKTOP_USAGE_UNDOCK 0xa1 /* System Undock */ #define HID_DESKTOP_USAGE_SETUP 0xa2 /* System Setup */ @@ -305,7 +305,7 @@ #define HID_DESKTOP_USAGE_APP_DEBUG_BREAK 0xa6 /* Application Debugger Break */ #define HID_DESKTOP_USAGE_MUTE 0xa7 /* System Speaker Mute */ #define HID_DESKTOP_USAGE_HIBERNATE 0xa8 /* System Hibernate */ - /* 0xa9-af Reserved */ + /* 0xa9-af Reserved */ #define HID_DESKTOP_USAGE_DISPLAY_INVERT 0xb0 /* System Display Invert */ #define HID_DESKTOP_USAGE_DISPALY_INTERNAL 0xb1 /* System Display Internal */ #define HID_DESKTOP_USAGE_DISPLAY_EXTERNAL 0xb2 /* System Display External */ @@ -314,7 +314,7 @@ #define HID_DESKTOP_USAGE_DISPLAY_TOGGLE 0xb5 /* System Display Toggle Int/Ext */ #define HID_DESKTOP_USAGE_DISPLAY_SWAP 0xb6 /* System Display Swap */ #define HID_DESKTOP_USAGE_ 0xb7 /* System Display LCD Autoscale */ - /* 0xb8-ffff Reserved */ + /* 0xb8-ffff Reserved */ /* Keyboard usage IDs (HuT 10) */ #define HID_KBD_USAGE_NONE 0x00 /* Reserved (no event indicated) */ @@ -529,16 +529,16 @@ /* HID Report Definitions */ struct usb_hid_class_subdescriptor { - uint8_t bDescriptorType;/* Class descriptor type (See 7.1) */ - uint16_t wDescriptorLength;/* Size of the report descriptor */ + uint8_t bDescriptorType; /* Class descriptor type (See 7.1) */ + uint16_t wDescriptorLength; /* Size of the report descriptor */ } __PACKED; struct usb_hid_descriptor { - uint8_t bLength; /* Size of the HID descriptor */ - uint8_t bDescriptorType;/* HID descriptor type */ - uint16_t bcdHID;/* HID class specification release */ - uint8_t bCountryCode;/* Country code */ - uint8_t bNumDescriptors;/* Number of descriptors (>=1) */ + uint8_t bLength; /* Size of the HID descriptor */ + uint8_t bDescriptorType; /* HID descriptor type */ + uint16_t bcdHID; /* HID class specification release */ + uint8_t bCountryCode; /* Country code */ + uint8_t bNumDescriptors; /* Number of descriptors (>=1) */ /* * Specification says at least one Class Descriptor needs to @@ -550,11 +550,10 @@ struct usb_hid_descriptor { /* Standard Reports *********************************************************/ /* Keyboard input report (8 bytes) (HID B.1) */ -struct usb_hid_kbd_report -{ - uint8_t modifier; /* Modifier keys. See HID_MODIFER_* definitions */ - uint8_t reserved; - uint8_t key[6]; /* Keycode 1-6 */ +struct usb_hid_kbd_report { + uint8_t modifier; /* Modifier keys. See HID_MODIFER_* definitions */ + uint8_t reserved; + uint8_t key[6]; /* Keycode 1-6 */ }; /* Keyboard output report (1 byte) (HID B.1), @@ -562,22 +561,109 @@ struct usb_hid_kbd_report */ /* Mouse input report (HID B.2) */ -struct usb_hid_mouse_report -{ - uint8_t buttons; /* See HID_MOUSE_INPUT_BUTTON_* definitions */ - int8_t xdisp; /* X displacement */ - int8_t ydisp; /* y displacement */ +struct usb_hid_mouse_report { + uint8_t buttons; /* See HID_MOUSE_INPUT_BUTTON_* definitions */ + int8_t xdisp; /* X displacement */ + int8_t ydisp; /* y displacement */ /* Device specific additional bytes may follow */ - uint8_t wdisp; /* Wheel displacement */ + uint8_t wdisp; /* Wheel displacement */ }; /* Joystick input report (1 bytes) (HID D.1) */ -struct usb_hid_js_report -{ - int8_t xpos; /* X position */ - int8_t ypos; /* X position */ - uint8_t buttons; /* See USBHID_JSIN_* definitions */ - uint8_t throttle; /* Throttle */ +struct usb_hid_js_report { + int8_t xpos; /* X position */ + int8_t ypos; /* X position */ + uint8_t buttons; /* See USBHID_JSIN_* definitions */ + uint8_t throttle; /* Throttle */ }; +// clang-format off +#define HID_MOUSE_DESCRIPTOR_LEN (9 + 9 + 7) + +#define HID_MOUSE_DESCRIPTOR_INIT(bInterfaceNumber, bInterfaceSubClass, wItemLength, int_ep, wMaxPacketSize, bInterval) \ + 0x09, /* bLength: Interface Descriptor size */ \ + USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */ \ + bInterfaceNumber, /* bInterfaceNumber: Number of Interface */ \ + 0x00, /* bAlternateSetting: Alternate setting */ \ + 0x01, /* bNumEndpoints */ \ + 0x03, /* bInterfaceClass: HID */ \ + bInterfaceSubClass, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ \ + 0x02, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ \ + 0x00, /* iInterface: Index of string descriptor */ \ + 0x09, /* bLength: HID Descriptor size */ \ + HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */ \ + 0x11, /* bcdHID: HID Class Spec release number */ \ + 0x01, \ + 0x00, /* bCountryCode: Hardware target country */ \ + 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */ \ + 0x22, /* bDescriptorType */ \ + WBVAL(wItemLength), /* wItemLength: Total length of Report descriptor */ \ + 0x07, /* bLength: Endpoint Descriptor size */ \ + USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */ \ + int_ep, /* bEndpointAddress: Endpoint Address (IN) */ \ + 0x03, /* bmAttributes: Interrupt endpoint */ \ + WBVAL(wMaxPacketSize), /* wMaxPacketSize: x Byte max */ \ + bInterval /* bInterval: Polling Interval */ + +#define HID_KEYBOARD_DESCRIPTOR_LEN (9 + 9 + 7) + +#define HID_KEYBOARD_DESCRIPTOR_INIT(bInterfaceNumber, bInterfaceSubClass, wItemLength, int_ep, wMaxPacketSize, bInterval) \ + 0x09, /* bLength: Interface Descriptor size */ \ + USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */ \ + bInterfaceNumber, /* bInterfaceNumber: Number of Interface */ \ + 0x00, /* bAlternateSetting: Alternate setting */ \ + 0x01, /* bNumEndpoints */ \ + 0x03, /* bInterfaceClass: HID */ \ + bInterfaceSubClass, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ \ + 0x01, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ \ + 0x00, /* iInterface: Index of string descriptor */ \ + 0x09, /* bLength: HID Descriptor size */ \ + HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */ \ + 0x11, /* bcdHID: HID Class Spec release number */ \ + 0x01, \ + 0x00, /* bCountryCode: Hardware target country */ \ + 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */ \ + 0x22, /* bDescriptorType */ \ + WBVAL(wItemLength), /* wItemLength: Total length of Report descriptor */ \ + 0x07, /* bLength: Endpoint Descriptor size */ \ + USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */ \ + int_ep, /* bEndpointAddress: Endpoint Address (IN) */ \ + 0x03, /* bmAttributes: Interrupt endpoint */ \ + WBVAL(wMaxPacketSize), /* wMaxPacketSize: x Byte max */ \ + bInterval /* bInterval: Polling Interval */ + +#define HID_CUSTOM_INOUT_DESCRIPTOR_LEN (9 + 9 + 7 + 7) + +#define HID_CUSTOM_INOUT_DESCRIPTOR_INIT(bInterfaceNumber, bInterfaceSubClass, wItemLength, in_ep, out_ep,wMaxPacketSize, bInterval) \ + 0x09, /* bLength: Interface Descriptor size */ \ + USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */ \ + bInterfaceNumber, /* bInterfaceNumber: Number of Interface */ \ + 0x00, /* bAlternateSetting: Alternate setting */ \ + 0x02, /* bNumEndpoints */ \ + 0x03, /* bInterfaceClass: HID */ \ + bInterfaceSubClass, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ \ + 0x00, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ \ + 0x00, /* iInterface: Index of string descriptor */ \ + 0x09, /* bLength: HID Descriptor size */ \ + HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */ \ + 0x11, /* bcdHID: HID Class Spec release number */ \ + 0x01, \ + 0x00, /* bCountryCode: Hardware target country */ \ + 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */ \ + 0x22, /* bDescriptorType */ \ + WBVAL(wItemLength), /* wItemLength: Total length of Report descriptor */ \ + 0x07, /* bLength: Endpoint Descriptor size */ \ + USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */ \ + in_ep, /* bEndpointAddress: Endpoint Address (IN) */ \ + 0x03, /* bmAttributes: Interrupt endpoint */ \ + WBVAL(wMaxPacketSize), /* wMaxPacketSize: x Byte max */ \ + bInterval, /* bInterval: Polling Interval */ \ + 0x07, /* bLength: Endpoint Descriptor size */ \ + USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */ \ + out_ep, /* bEndpointAddress: Endpoint Address (IN) */ \ + 0x03, /* bmAttributes: Interrupt endpoint */ \ + WBVAL(wMaxPacketSize), /* wMaxPacketSize: x Byte max */ \ + bInterval /* bInterval: Polling Interval */ +// clang-format on + #endif /* USB_HID_H */