webusb_hid_template.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usbd_hid.h"
  8. #define WEBUSB_VENDOR_CODE (0x22)
  9. #define WINUSB_VENDOR_CODE (0x21)
  10. #define URL_DESCRIPTOR_LENGTH (3 + 36)
  11. #define WEBUSB_INTF_NUM 0x01
  12. #define WEBUSB_URL_STRINGS \
  13. 'g', 'i', 't', 'h', 'u', 'b', '.', 'c', 'o', 'm', '/', \
  14. 'c', 'h', 'e', 'r', 'r', 'y', '-', 'e', 'm', 'b', 'e', 'd', 'd', 'e', 'd', '/', 'C', 'h', 'e', 'r', 'r', 'y', 'U', 'S', 'B',
  15. const uint8_t USBD_WebUSBURLDescriptor[URL_DESCRIPTOR_LENGTH] = {
  16. URL_DESCRIPTOR_LENGTH,
  17. WEBUSB_URL_TYPE,
  18. WEBUSB_URL_SCHEME_HTTPS,
  19. WEBUSB_URL_STRINGS
  20. };
  21. const uint8_t WINUSB_WCIDDescriptor[] = {
  22. USB_MSOSV2_COMP_ID_SET_HEADER_DESCRIPTOR_INIT(10 + USB_MSOSV2_COMP_ID_FUNCTION_WINUSB_MULTI_DESCRIPTOR_LEN),
  23. USB_MSOSV2_COMP_ID_FUNCTION_WINUSB_MULTI_DESCRIPTOR_INIT(WEBUSB_INTF_NUM),
  24. };
  25. __ALIGN_BEGIN const uint8_t USBD_BinaryObjectStoreDescriptor[] = {
  26. USB_BOS_HEADER_DESCRIPTOR_INIT(5 + USB_BOS_CAP_PLATFORM_WEBUSB_DESCRIPTOR_LEN + USB_BOS_CAP_PLATFORM_WINUSB_DESCRIPTOR_LEN, 2),
  27. USB_BOS_CAP_PLATFORM_WEBUSB_DESCRIPTOR_INIT(WEBUSB_VENDOR_CODE, 0x01),
  28. USB_BOS_CAP_PLATFORM_WINUSB_DESCRIPTOR_INIT(WINUSB_VENDOR_CODE, sizeof(WINUSB_WCIDDescriptor)),
  29. };
  30. struct usb_webusb_descriptor webusb_url_desc = {
  31. .vendor_code = WEBUSB_VENDOR_CODE,
  32. .string = USBD_WebUSBURLDescriptor,
  33. .string_len = URL_DESCRIPTOR_LENGTH
  34. };
  35. struct usb_msosv2_descriptor msosv2_desc = {
  36. .vendor_code = WINUSB_VENDOR_CODE,
  37. .compat_id = WINUSB_WCIDDescriptor,
  38. .compat_id_len = sizeof(WINUSB_WCIDDescriptor),
  39. };
  40. struct usb_bos_descriptor bos_desc = {
  41. .string = USBD_BinaryObjectStoreDescriptor,
  42. .string_len = sizeof(USBD_BinaryObjectStoreDescriptor)
  43. };
  44. #define USBD_VID 0xffff
  45. #define USBD_PID 0xffff
  46. #define USBD_MAX_POWER 100
  47. #define USBD_LANGID_STRING 1033
  48. #define HID_INT_EP 0x81
  49. #define HID_INT_EP_SIZE 8
  50. #define HID_INT_EP_INTERVAL 10
  51. #define USB_CONFIG_SIZE (34 + 9)
  52. #define HID_KEYBOARD_REPORT_DESC_SIZE 63
  53. static const uint8_t device_descriptor[] = {
  54. USB_DEVICE_DESCRIPTOR_INIT(USB_2_1, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01)
  55. };
  56. static const uint8_t config_descriptor[] = {
  57. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  58. HID_KEYBOARD_DESCRIPTOR_INIT(0x00, 0x01, HID_KEYBOARD_REPORT_DESC_SIZE, HID_INT_EP, HID_INT_EP_SIZE, HID_INT_EP_INTERVAL),
  59. USB_INTERFACE_DESCRIPTOR_INIT(WEBUSB_INTF_NUM, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00)
  60. };
  61. static const uint8_t device_quality_descriptor[] = {
  62. ///////////////////////////////////////
  63. /// device qualifier descriptor
  64. ///////////////////////////////////////
  65. 0x0a,
  66. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  67. 0x00,
  68. 0x02,
  69. 0x00,
  70. 0x00,
  71. 0x00,
  72. 0x40,
  73. 0x00,
  74. 0x00,
  75. };
  76. static const char *string_descriptors[] = {
  77. (const char[]){ 0x09, 0x04 }, /* Langid */
  78. "CherryUSB", /* Manufacturer */
  79. "CherryUSB WEBUSB HID DEMO", /* Product */
  80. "2022123456", /* Serial Number */
  81. };
  82. static const uint8_t *device_descriptor_callback(uint8_t speed)
  83. {
  84. return device_descriptor;
  85. }
  86. static const uint8_t *config_descriptor_callback(uint8_t speed)
  87. {
  88. return config_descriptor;
  89. }
  90. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  91. {
  92. return device_quality_descriptor;
  93. }
  94. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  95. {
  96. if (index > 3) {
  97. return NULL;
  98. }
  99. return string_descriptors[index];
  100. }
  101. const struct usb_descriptor webusb_hid_descriptor = {
  102. .device_descriptor_callback = device_descriptor_callback,
  103. .config_descriptor_callback = config_descriptor_callback,
  104. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  105. .string_descriptor_callback = string_descriptor_callback,
  106. .msosv2_descriptor = &msosv2_desc,
  107. .webusb_url_descriptor = &webusb_url_desc,
  108. .bos_descriptor = &bos_desc
  109. };
  110. /* USB HID device Configuration Descriptor */
  111. static uint8_t hid_desc[9] __ALIGN_END = {
  112. /* 18 */
  113. 0x09, /* bLength: HID Descriptor size */
  114. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  115. 0x11, /* bcdHID: HID Class Spec release number */
  116. 0x01,
  117. 0x00, /* bCountryCode: Hardware target country */
  118. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  119. 0x22, /* bDescriptorType */
  120. HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  121. 0x00,
  122. };
  123. static const uint8_t hid_keyboard_report_desc[HID_KEYBOARD_REPORT_DESC_SIZE] = {
  124. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  125. 0x09, 0x06, // USAGE (Keyboard)
  126. 0xa1, 0x01, // COLLECTION (Application)
  127. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  128. 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
  129. 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
  130. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  131. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  132. 0x75, 0x01, // REPORT_SIZE (1)
  133. 0x95, 0x08, // REPORT_COUNT (8)
  134. 0x81, 0x02, // INPUT (Data,Var,Abs)
  135. 0x95, 0x01, // REPORT_COUNT (1)
  136. 0x75, 0x08, // REPORT_SIZE (8)
  137. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  138. 0x95, 0x05, // REPORT_COUNT (5)
  139. 0x75, 0x01, // REPORT_SIZE (1)
  140. 0x05, 0x08, // USAGE_PAGE (LEDs)
  141. 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
  142. 0x29, 0x05, // USAGE_MAXIMUM (Kana)
  143. 0x91, 0x02, // OUTPUT (Data,Var,Abs)
  144. 0x95, 0x01, // REPORT_COUNT (1)
  145. 0x75, 0x03, // REPORT_SIZE (3)
  146. 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
  147. 0x95, 0x06, // REPORT_COUNT (6)
  148. 0x75, 0x08, // REPORT_SIZE (8)
  149. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  150. 0x25, 0xFF, // LOGICAL_MAXIMUM (255)
  151. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  152. 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
  153. 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
  154. 0x81, 0x00, // INPUT (Data,Ary,Abs)
  155. 0xc0 // END_COLLECTION
  156. };
  157. #define HID_STATE_IDLE 0
  158. #define HID_STATE_BUSY 1
  159. /*!< hid state ! Data can be sent only when state is idle */
  160. static volatile uint8_t hid_state = HID_STATE_IDLE;
  161. static void usbd_event_handler(uint8_t busid, uint8_t event)
  162. {
  163. switch (event) {
  164. case USBD_EVENT_RESET:
  165. break;
  166. case USBD_EVENT_CONNECTED:
  167. break;
  168. case USBD_EVENT_DISCONNECTED:
  169. break;
  170. case USBD_EVENT_RESUME:
  171. break;
  172. case USBD_EVENT_SUSPEND:
  173. break;
  174. case USBD_EVENT_CONFIGURED:
  175. hid_state = HID_STATE_IDLE;
  176. break;
  177. case USBD_EVENT_SET_REMOTE_WAKEUP:
  178. break;
  179. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  180. break;
  181. default:
  182. break;
  183. }
  184. }
  185. void usbd_hid_int_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  186. {
  187. hid_state = HID_STATE_IDLE;
  188. }
  189. static struct usbd_endpoint hid_in_ep = {
  190. .ep_cb = usbd_hid_int_callback,
  191. .ep_addr = HID_INT_EP
  192. };
  193. static struct usbd_interface intf0;
  194. void webusb_hid_keyboard_init(uint8_t busid, uintptr_t reg_base)
  195. {
  196. usbd_desc_register(busid, &webusb_hid_descriptor);
  197. usbd_add_interface(busid, usbd_hid_init_intf(busid, &intf0, hid_keyboard_report_desc, HID_KEYBOARD_REPORT_DESC_SIZE));
  198. usbd_add_endpoint(busid, &hid_in_ep);
  199. usbd_initialize(busid, reg_base, usbd_event_handler);
  200. }
  201. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[64];
  202. void hid_keyboard_test(uint8_t busid)
  203. {
  204. const uint8_t sendbuffer[8] = { 0x00, 0x00, HID_KBD_USAGE_A, 0x00, 0x00, 0x00, 0x00, 0x00 };
  205. if (usb_device_is_configured(busid) == false) {
  206. return;
  207. }
  208. memcpy(write_buffer, sendbuffer, 8);
  209. hid_state = HID_STATE_BUSY;
  210. usbd_ep_start_write(busid, HID_INT_EP, write_buffer, 8);
  211. while (hid_state == HID_STATE_BUSY) {
  212. }
  213. }