webusb_hid_template.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  54. static const uint8_t device_descriptor[] = {
  55. USB_DEVICE_DESCRIPTOR_INIT(USB_2_1, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01)
  56. };
  57. static const uint8_t config_descriptor[] = {
  58. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  59. HID_KEYBOARD_DESCRIPTOR_INIT(0x00, 0x01, HID_KEYBOARD_REPORT_DESC_SIZE, HID_INT_EP, HID_INT_EP_SIZE, HID_INT_EP_INTERVAL),
  60. USB_INTERFACE_DESCRIPTOR_INIT(WEBUSB_INTF_NUM, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00)
  61. };
  62. static const uint8_t device_quality_descriptor[] = {
  63. ///////////////////////////////////////
  64. /// device qualifier descriptor
  65. ///////////////////////////////////////
  66. 0x0a,
  67. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  68. 0x00,
  69. 0x02,
  70. 0x00,
  71. 0x00,
  72. 0x00,
  73. 0x40,
  74. 0x00,
  75. 0x00,
  76. };
  77. static const char *string_descriptors[] = {
  78. (const char[]){ 0x09, 0x04 }, /* Langid */
  79. "CherryUSB", /* Manufacturer */
  80. "CherryUSB WEBUSB HID DEMO", /* Product */
  81. "2022123456", /* Serial Number */
  82. };
  83. static const uint8_t *device_descriptor_callback(uint8_t speed)
  84. {
  85. return device_descriptor;
  86. }
  87. static const uint8_t *config_descriptor_callback(uint8_t speed)
  88. {
  89. return config_descriptor;
  90. }
  91. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  92. {
  93. return device_quality_descriptor;
  94. }
  95. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  96. {
  97. if (index > 3) {
  98. return NULL;
  99. }
  100. return string_descriptors[index];
  101. }
  102. const struct usb_descriptor webusb_hid_descriptor = {
  103. .device_descriptor_callback = device_descriptor_callback,
  104. .config_descriptor_callback = config_descriptor_callback,
  105. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  106. .string_descriptor_callback = string_descriptor_callback,
  107. .msosv2_descriptor = &msosv2_desc,
  108. .webusb_url_descriptor = &webusb_url_desc,
  109. .bos_descriptor = &bos_desc
  110. };
  111. #else
  112. static const uint8_t webusb_hid_descriptor[] = {
  113. USB_DEVICE_DESCRIPTOR_INIT(USB_2_1, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01),
  114. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  115. HID_KEYBOARD_DESCRIPTOR_INIT(0x00, 0x01, HID_KEYBOARD_REPORT_DESC_SIZE, HID_INT_EP, HID_INT_EP_SIZE, HID_INT_EP_INTERVAL),
  116. USB_INTERFACE_DESCRIPTOR_INIT(WEBUSB_INTF_NUM, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00),
  117. ///////////////////////////////////////
  118. /// string0 descriptor
  119. ///////////////////////////////////////
  120. USB_LANGID_INIT(USBD_LANGID_STRING),
  121. ///////////////////////////////////////
  122. /// string1 descriptor
  123. ///////////////////////////////////////
  124. 0x14, /* bLength */
  125. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  126. 'C', 0x00, /* wcChar0 */
  127. 'h', 0x00, /* wcChar1 */
  128. 'e', 0x00, /* wcChar2 */
  129. 'r', 0x00, /* wcChar3 */
  130. 'r', 0x00, /* wcChar4 */
  131. 'y', 0x00, /* wcChar5 */
  132. 'U', 0x00, /* wcChar6 */
  133. 'S', 0x00, /* wcChar7 */
  134. 'B', 0x00, /* wcChar8 */
  135. ///////////////////////////////////////
  136. /// string2 descriptor
  137. ///////////////////////////////////////
  138. 0x2C, /* bLength */
  139. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  140. 'C', 0x00, /* wcChar0 */
  141. 'h', 0x00, /* wcChar1 */
  142. 'e', 0x00, /* wcChar2 */
  143. 'r', 0x00, /* wcChar3 */
  144. 'r', 0x00, /* wcChar4 */
  145. 'y', 0x00, /* wcChar5 */
  146. 'U', 0x00, /* wcChar6 */
  147. 'S', 0x00, /* wcChar7 */
  148. 'B', 0x00, /* wcChar8 */
  149. ' ', 0x00, /* wcChar9 */
  150. 'W', 0x00, /* wcChar10 */
  151. 'E', 0x00, /* wcChar11 */
  152. 'B', 0x00, /* wcChar12 */
  153. 'U', 0x00, /* wcChar13 */
  154. 'S', 0x00, /* wcChar14 */
  155. 'B', 0x00, /* wcChar15 */
  156. ' ', 0x00, /* wcChar16 */
  157. 'D', 0x00, /* wcChar17 */
  158. 'E', 0x00, /* wcChar18 */
  159. 'M', 0x00, /* wcChar19 */
  160. 'O', 0x00, /* wcChar20 */
  161. ///////////////////////////////////////
  162. /// string3 descriptor
  163. ///////////////////////////////////////
  164. 0x16, /* bLength */
  165. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  166. '2', 0x00, /* wcChar0 */
  167. '0', 0x00, /* wcChar1 */
  168. '2', 0x00, /* wcChar2 */
  169. '2', 0x00, /* wcChar3 */
  170. '1', 0x00, /* wcChar4 */
  171. '2', 0x00, /* wcChar5 */
  172. '3', 0x00, /* wcChar6 */
  173. '4', 0x00, /* wcChar7 */
  174. '5', 0x00, /* wcChar8 */
  175. '6', 0x00, /* wcChar9 */
  176. #ifdef CONFIG_USB_HS
  177. ///////////////////////////////////////
  178. /// device qualifier descriptor
  179. ///////////////////////////////////////
  180. 0x0a,
  181. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  182. 0x00,
  183. 0x02,
  184. 0x00,
  185. 0x00,
  186. 0x00,
  187. 0x40,
  188. 0x00,
  189. 0x00,
  190. #endif
  191. 0x00
  192. };
  193. #endif
  194. /* USB HID device Configuration Descriptor */
  195. static uint8_t hid_desc[9] __ALIGN_END = {
  196. /* 18 */
  197. 0x09, /* bLength: HID Descriptor size */
  198. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  199. 0x11, /* bcdHID: HID Class Spec release number */
  200. 0x01,
  201. 0x00, /* bCountryCode: Hardware target country */
  202. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  203. 0x22, /* bDescriptorType */
  204. HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  205. 0x00,
  206. };
  207. static const uint8_t hid_keyboard_report_desc[HID_KEYBOARD_REPORT_DESC_SIZE] = {
  208. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  209. 0x09, 0x06, // USAGE (Keyboard)
  210. 0xa1, 0x01, // COLLECTION (Application)
  211. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  212. 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
  213. 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
  214. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  215. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  216. 0x75, 0x01, // REPORT_SIZE (1)
  217. 0x95, 0x08, // REPORT_COUNT (8)
  218. 0x81, 0x02, // INPUT (Data,Var,Abs)
  219. 0x95, 0x01, // REPORT_COUNT (1)
  220. 0x75, 0x08, // REPORT_SIZE (8)
  221. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  222. 0x95, 0x05, // REPORT_COUNT (5)
  223. 0x75, 0x01, // REPORT_SIZE (1)
  224. 0x05, 0x08, // USAGE_PAGE (LEDs)
  225. 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
  226. 0x29, 0x05, // USAGE_MAXIMUM (Kana)
  227. 0x91, 0x02, // OUTPUT (Data,Var,Abs)
  228. 0x95, 0x01, // REPORT_COUNT (1)
  229. 0x75, 0x03, // REPORT_SIZE (3)
  230. 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
  231. 0x95, 0x06, // REPORT_COUNT (6)
  232. 0x75, 0x08, // REPORT_SIZE (8)
  233. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  234. 0x25, 0xFF, // LOGICAL_MAXIMUM (255)
  235. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  236. 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
  237. 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
  238. 0x81, 0x00, // INPUT (Data,Ary,Abs)
  239. 0xc0 // END_COLLECTION
  240. };
  241. #define HID_STATE_IDLE 0
  242. #define HID_STATE_BUSY 1
  243. /*!< hid state ! Data can be sent only when state is idle */
  244. static volatile uint8_t hid_state = HID_STATE_IDLE;
  245. static void usbd_event_handler(uint8_t busid, uint8_t event)
  246. {
  247. switch (event) {
  248. case USBD_EVENT_RESET:
  249. break;
  250. case USBD_EVENT_CONNECTED:
  251. break;
  252. case USBD_EVENT_DISCONNECTED:
  253. break;
  254. case USBD_EVENT_RESUME:
  255. break;
  256. case USBD_EVENT_SUSPEND:
  257. break;
  258. case USBD_EVENT_CONFIGURED:
  259. hid_state = HID_STATE_IDLE;
  260. break;
  261. case USBD_EVENT_SET_REMOTE_WAKEUP:
  262. break;
  263. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  264. break;
  265. default:
  266. break;
  267. }
  268. }
  269. void usbd_hid_int_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  270. {
  271. hid_state = HID_STATE_IDLE;
  272. }
  273. static struct usbd_endpoint hid_in_ep = {
  274. .ep_cb = usbd_hid_int_callback,
  275. .ep_addr = HID_INT_EP
  276. };
  277. static struct usbd_interface intf0;
  278. void webusb_hid_keyboard_init(uint8_t busid, uintptr_t reg_base)
  279. {
  280. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  281. usbd_desc_register(busid, &webusb_hid_descriptor);
  282. #else
  283. usbd_desc_register(busid, webusb_hid_descriptor);
  284. #endif
  285. #ifndef CONFIG_USBDEV_ADVANCE_DESC
  286. usbd_bos_desc_register(busid, &bos_desc);
  287. usbd_msosv2_desc_register(busid, &msosv2_desc);
  288. usbd_webusb_desc_register(busid, &webusb_url_desc);
  289. #endif
  290. usbd_add_interface(busid, usbd_hid_init_intf(busid, &intf0, hid_keyboard_report_desc, HID_KEYBOARD_REPORT_DESC_SIZE));
  291. usbd_add_endpoint(busid, &hid_in_ep);
  292. usbd_initialize(busid, reg_base, usbd_event_handler);
  293. }
  294. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[64];
  295. void hid_keyboard_test(uint8_t busid)
  296. {
  297. const uint8_t sendbuffer[8] = { 0x00, 0x00, HID_KBD_USAGE_A, 0x00, 0x00, 0x00, 0x00, 0x00 };
  298. if (usb_device_is_configured(busid) == false) {
  299. return;
  300. }
  301. memcpy(write_buffer, sendbuffer, 8);
  302. hid_state = HID_STATE_BUSY;
  303. usbd_ep_start_write(busid, HID_INT_EP, write_buffer, 8);
  304. while (hid_state == HID_STATE_BUSY) {
  305. }
  306. }