webusb_hid_template.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 USBD_VID 0xffff
  9. #define USBD_PID 0xffff
  10. #define USBD_MAX_POWER 100
  11. #define USBD_LANGID_STRING 1033
  12. #define HID_INT_EP 0x81
  13. #define HID_INT_EP_SIZE 8
  14. #define HID_INT_EP_INTERVAL 10
  15. #define USB_HID_CONFIG_DESC_SIZ (34 + 9)
  16. #define HID_KEYBOARD_REPORT_DESC_SIZE 63
  17. #define USBD_WEBUSB_VENDOR_CODE (0x22)
  18. #define USBD_WINUSB_VENDOR_CODE (0x21)
  19. #define USBD_WINUSB_DESC_SET_LEN (0xB2)
  20. #define URL_DESCRIPTOR_LENGTH (3 + 36)
  21. #define USBD_WEBUSB_INTF_NUM 0x01
  22. #define WEBUSB_URL_STRINGS \
  23. 'g', 'i', 't', 'h', 'u', 'b', '.', 'c', 'o', 'm', '/', \
  24. 'c', 'h', 'e', 'r', 'r', 'y', '-', 'e', 'm', 'b', 'e', 'd', 'd', 'e', 'd', '/', 'C', 'h', 'e', 'r', 'r', 'y', 'U', 'S', 'B',
  25. const uint8_t USBD_WinUSBDescriptorSetDescriptor[USBD_WINUSB_DESC_SET_LEN] = {
  26. // Microsoft OS 2.0 描述符集标头
  27. 0x0A, 0x00, // Descriptor size (10 bytes)
  28. 0x00, 0x00, // MS OS 2.0 descriptor set header
  29. 0x00, 0x00, 0x03, 0x06, // Windows version (8.1) (0x06030000)
  30. USBD_WINUSB_DESC_SET_LEN, 0x00, // Size, MS OS 2.0 descriptor set
  31. // Microsoft OS 2.0 配置子集标头
  32. 0x08, 0x00, // wLength
  33. 0x01, 0x00, // wDescriptorType
  34. 0x00, // 适用于配置 1
  35. 0x00, // bReserved
  36. 0XA8, 0X00, // Size, MS OS 2.0 configuration subset
  37. // Microsoft OS 2.0 功能子集头
  38. 0x08, 0x00, // Descriptor size (8 bytes)
  39. 0x02, 0x00, // MS OS 2.0 function subset header
  40. USBD_WEBUSB_INTF_NUM, // bFirstInterface
  41. 0x00, // 必须设置为 0
  42. 0xA0, 0x00,
  43. // Microsoft OS 2.0 兼容 ID 描述符
  44. // 兼容 ID 描述符告诉 Windows 此设备与 WinUSB 驱动程序兼容
  45. 0x14, 0x00, // wLength 20
  46. 0x03, 0x00, // MS_OS_20_FEATURE_COMPATIBLE_ID
  47. 'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49. // Microsoft OS 2.0 注册表属性描述符
  50. // 注册表属性分配设备接口 GUID
  51. 0x84, 0x00, //wLength: 132
  52. 0x04, 0x00, // wDescriptorType: MS_OS_20_FEATURE_REG_PROPERTY: 0x04 (Table 9)
  53. 0x07, 0x00, //wPropertyDataType: REG_MULTI_SZ (Table 15)
  54. 0x2a, 0x00, //wPropertyNameLength:
  55. //bPropertyName: “DeviceInterfaceGUID”
  56. 'D', 0x00, 'e', 0x00, 'v', 0x00, 'i', 0x00, 'c', 0x00, 'e', 0x00, 'I', 0x00, 'n', 0x00, 't', 0x00, 'e', 0x00,
  57. 'r', 0x00, 'f', 0x00, 'a', 0x00, 'c', 0x00, 'e', 0x00, 'G', 0x00, 'U', 0x00, 'I', 0x00, 'D', 0x00, 's', 0x00,
  58. 0x00, 0x00,
  59. 0x50, 0x00, // wPropertyDataLength
  60. //bPropertyData: “{975F44D9-0D08-43FD-8B3E-127CA8AFFF9D}”.
  61. '{', 0x00, '9', 0x00, 'd', 0x00, '7', 0x00, 'd', 0x00, 'e', 0x00, 'b', 0x00, 'b', 0x00, 'c', 0x00, '-', 0x00,
  62. 'c', 0x00, '8', 0x00, '5', 0x00, 'd', 0x00, '-', 0x00, '1', 0x00, '1', 0x00, 'd', 0x00, '1', 0x00, '-', 0x00,
  63. '9', 0x00, 'e', 0x00, 'b', 0x00, '4', 0x00, '-', 0x00, '0', 0x00, '0', 0x00, '6', 0x00, '0', 0x00, '0', 0x00,
  64. '8', 0x00, 'c', 0x00, '3', 0x00, 'a', 0x00, '1', 0x00, '9', 0x00, 'a', 0x00, '}', 0x00, 0x00, 0x00, 0x00, 0x00
  65. };
  66. const uint8_t USBD_WebUSBURLDescriptor[URL_DESCRIPTOR_LENGTH] = {
  67. URL_DESCRIPTOR_LENGTH,
  68. WEBUSB_URL_TYPE,
  69. WEBUSB_URL_SCHEME_HTTPS,
  70. WEBUSB_URL_STRINGS
  71. };
  72. #define USBD_BOS_WTOTALLENGTH 0x39
  73. #define LANDING_PAGE 0x01
  74. uint8_t USBD_BinaryObjectStoreDescriptor[USBD_BOS_WTOTALLENGTH] = {
  75. // BOS描述符
  76. 0x05, // bLength 固长为5
  77. 0x0F, // bDescriptorType 固定为15
  78. USBD_BOS_WTOTALLENGTH, 0x00, // wTotalLength BOS描述符的总大小
  79. 0x02, // bNumDeviceCaps BOS描述符中独立设备功能特性描述符的数量
  80. // WebUSB 平台功能描述符
  81. 0x18, // Descriptor size (24 bytes)
  82. 0x10, // Descriptor type (Device Capability) 设备功能描述符
  83. 0x05, // Capability type (Platform) 平台描述符
  84. 0x00, // Reserved
  85. // WebUSB Platform Capability ID (3408b638-09a9-47a0-8bfd-a0768815b665)
  86. // 平台功能 UUID 将此标识为WebUSB 平台功能描述符,它提供有关设备的基本信息
  87. 0x38, 0xB6, 0x08, 0x34,
  88. 0xA9, 0x09,
  89. 0xA0, 0x47,
  90. 0x8B, 0xFD,
  91. 0xA0, 0x76, 0x88, 0x15, 0xB6, 0x65,
  92. 0x00, 0x01, // WebUSB version 1.0
  93. USBD_WEBUSB_VENDOR_CODE, // Vendor-assigned WebUSB request code
  94. LANDING_PAGE, // Landing page
  95. // Microsoft 平台功能描述符
  96. // 标头
  97. 0x1C, // Descriptor size (28 bytes)
  98. 0x10, // Descriptor type (Device Capability)
  99. 0x05, // Capability type (Platform)
  100. 0x00, // Reserved
  101. 0xDF, 0x60, 0xDD, 0xD8, /* PlatformCapabilityUUID */
  102. 0x89, 0x45, 0xC7, 0x4C,
  103. 0x9C, 0xD2, 0x65, 0x9D,
  104. 0x9E, 0x64, 0x8A, 0x9F,
  105. // 描述符集信息结构
  106. 0x00, 0x00, 0x03, 0x06, /* >= Win 8.1 * dwWindowsVersion 最低兼容 Windows 版本 */
  107. USBD_WINUSB_DESC_SET_LEN, 0X00, /* wDescriptorSetTotalLength */
  108. USBD_WINUSB_VENDOR_CODE, /* bVendorCode */
  109. 0X00 /* bAltEnumCode */
  110. };
  111. struct usb_webusb_descriptor webusb_url_desc = {
  112. .vendor_code = USBD_WEBUSB_VENDOR_CODE,
  113. .string = USBD_WebUSBURLDescriptor,
  114. .string_len = USBD_WINUSB_DESC_SET_LEN
  115. };
  116. struct usb_msosv2_descriptor msosv2_desc = {
  117. .vendor_code = USBD_WINUSB_VENDOR_CODE,
  118. .compat_id = USBD_WinUSBDescriptorSetDescriptor,
  119. .compat_id_len = USBD_WINUSB_DESC_SET_LEN,
  120. };
  121. struct usb_bos_descriptor bos_desc = {
  122. .string = USBD_BinaryObjectStoreDescriptor,
  123. .string_len = USBD_BOS_WTOTALLENGTH
  124. };
  125. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  126. static const uint8_t device_descriptor[] = {
  127. USB_DEVICE_DESCRIPTOR_INIT(USB_2_1, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01)
  128. };
  129. static const uint8_t config_descriptor[] = {
  130. USB_CONFIG_DESCRIPTOR_INIT(USB_HID_CONFIG_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  131. /************** Descriptor of Joystick Mouse interface ****************/
  132. /* 09 */
  133. 0x09, /* bLength: Interface Descriptor size */
  134. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
  135. 0x00, /* bInterfaceNumber: Number of Interface */
  136. 0x00, /* bAlternateSetting: Alternate setting */
  137. 0x01, /* bNumEndpoints */
  138. 0x03, /* bInterfaceClass: HID */
  139. 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
  140. 0x01, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
  141. 0, /* iInterface: Index of string descriptor */
  142. /******************** Descriptor of Joystick Mouse HID ********************/
  143. /* 18 */
  144. 0x09, /* bLength: HID Descriptor size */
  145. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  146. 0x11, /* bcdHID: HID Class Spec release number */
  147. 0x01,
  148. 0x00, /* bCountryCode: Hardware target country */
  149. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  150. 0x22, /* bDescriptorType */
  151. HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  152. 0x00,
  153. /******************** Descriptor of Mouse endpoint ********************/
  154. /* 27 */
  155. 0x07, /* bLength: Endpoint Descriptor size */
  156. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
  157. HID_INT_EP, /* bEndpointAddress: Endpoint Address (IN) */
  158. 0x03, /* bmAttributes: Interrupt endpoint */
  159. HID_INT_EP_SIZE, /* wMaxPacketSize: 4 Byte max */
  160. 0x00,
  161. HID_INT_EP_INTERVAL, /* bInterval: Polling Interval */
  162. /* 34 */
  163. USB_INTERFACE_DESCRIPTOR_INIT(USBD_WEBUSB_INTF_NUM, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00)
  164. };
  165. static const uint8_t device_quality_descriptor[] = {
  166. ///////////////////////////////////////
  167. /// device qualifier descriptor
  168. ///////////////////////////////////////
  169. 0x0a,
  170. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  171. 0x00,
  172. 0x02,
  173. 0x00,
  174. 0x00,
  175. 0x00,
  176. 0x40,
  177. 0x00,
  178. 0x00,
  179. };
  180. static const char *string_descriptors[] = {
  181. (const char[]){ 0x09, 0x04 }, /* Langid */
  182. "CherryUSB", /* Manufacturer */
  183. "CherryUSB WEBUSB HID DEMO", /* Product */
  184. "2022123456", /* Serial Number */
  185. };
  186. static const uint8_t *device_descriptor_callback(uint8_t speed)
  187. {
  188. return device_descriptor;
  189. }
  190. static const uint8_t *config_descriptor_callback(uint8_t speed)
  191. {
  192. return config_descriptor;
  193. }
  194. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  195. {
  196. return device_quality_descriptor;
  197. }
  198. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  199. {
  200. if (index > 3) {
  201. return NULL;
  202. }
  203. return string_descriptors[index];
  204. }
  205. const struct usb_descriptor webusb_hid_descriptor = {
  206. .device_descriptor_callback = device_descriptor_callback,
  207. .config_descriptor_callback = config_descriptor_callback,
  208. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  209. .string_descriptor_callback = string_descriptor_callback,
  210. .msosv2_descriptor = &msosv2_desc,
  211. .webusb_url_descriptor = &webusb_url_desc,
  212. .bos_descriptor = &bos_desc
  213. };
  214. #else
  215. static const uint8_t webusb_hid_descriptor[] = {
  216. USB_DEVICE_DESCRIPTOR_INIT(USB_2_1, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01),
  217. USB_CONFIG_DESCRIPTOR_INIT(USB_HID_CONFIG_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  218. /************** Descriptor of Joystick Mouse interface ****************/
  219. /* 09 */
  220. 0x09, /* bLength: Interface Descriptor size */
  221. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
  222. 0x00, /* bInterfaceNumber: Number of Interface */
  223. 0x00, /* bAlternateSetting: Alternate setting */
  224. 0x01, /* bNumEndpoints */
  225. 0x03, /* bInterfaceClass: HID */
  226. 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
  227. 0x01, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
  228. 0, /* iInterface: Index of string descriptor */
  229. /******************** Descriptor of Joystick Mouse HID ********************/
  230. /* 18 */
  231. 0x09, /* bLength: HID Descriptor size */
  232. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  233. 0x11, /* bcdHID: HID Class Spec release number */
  234. 0x01,
  235. 0x00, /* bCountryCode: Hardware target country */
  236. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  237. 0x22, /* bDescriptorType */
  238. HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  239. 0x00,
  240. /******************** Descriptor of Mouse endpoint ********************/
  241. /* 27 */
  242. 0x07, /* bLength: Endpoint Descriptor size */
  243. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
  244. HID_INT_EP, /* bEndpointAddress: Endpoint Address (IN) */
  245. 0x03, /* bmAttributes: Interrupt endpoint */
  246. HID_INT_EP_SIZE, /* wMaxPacketSize: 4 Byte max */
  247. 0x00,
  248. HID_INT_EP_INTERVAL, /* bInterval: Polling Interval */
  249. /* 34 */
  250. USB_INTERFACE_DESCRIPTOR_INIT(USBD_WEBUSB_INTF_NUM, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00),
  251. ///////////////////////////////////////
  252. /// string0 descriptor
  253. ///////////////////////////////////////
  254. USB_LANGID_INIT(USBD_LANGID_STRING),
  255. ///////////////////////////////////////
  256. /// string1 descriptor
  257. ///////////////////////////////////////
  258. 0x14, /* bLength */
  259. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  260. 'C', 0x00, /* wcChar0 */
  261. 'h', 0x00, /* wcChar1 */
  262. 'e', 0x00, /* wcChar2 */
  263. 'r', 0x00, /* wcChar3 */
  264. 'r', 0x00, /* wcChar4 */
  265. 'y', 0x00, /* wcChar5 */
  266. 'U', 0x00, /* wcChar6 */
  267. 'S', 0x00, /* wcChar7 */
  268. 'B', 0x00, /* wcChar8 */
  269. ///////////////////////////////////////
  270. /// string2 descriptor
  271. ///////////////////////////////////////
  272. 0x2C, /* bLength */
  273. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  274. 'C', 0x00, /* wcChar0 */
  275. 'h', 0x00, /* wcChar1 */
  276. 'e', 0x00, /* wcChar2 */
  277. 'r', 0x00, /* wcChar3 */
  278. 'r', 0x00, /* wcChar4 */
  279. 'y', 0x00, /* wcChar5 */
  280. 'U', 0x00, /* wcChar6 */
  281. 'S', 0x00, /* wcChar7 */
  282. 'B', 0x00, /* wcChar8 */
  283. ' ', 0x00, /* wcChar9 */
  284. 'W', 0x00, /* wcChar10 */
  285. 'E', 0x00, /* wcChar11 */
  286. 'B', 0x00, /* wcChar12 */
  287. 'U', 0x00, /* wcChar13 */
  288. 'S', 0x00, /* wcChar14 */
  289. 'B', 0x00, /* wcChar15 */
  290. ' ', 0x00, /* wcChar16 */
  291. 'D', 0x00, /* wcChar17 */
  292. 'E', 0x00, /* wcChar18 */
  293. 'M', 0x00, /* wcChar19 */
  294. 'O', 0x00, /* wcChar20 */
  295. ///////////////////////////////////////
  296. /// string3 descriptor
  297. ///////////////////////////////////////
  298. 0x16, /* bLength */
  299. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  300. '2', 0x00, /* wcChar0 */
  301. '0', 0x00, /* wcChar1 */
  302. '2', 0x00, /* wcChar2 */
  303. '2', 0x00, /* wcChar3 */
  304. '1', 0x00, /* wcChar4 */
  305. '2', 0x00, /* wcChar5 */
  306. '3', 0x00, /* wcChar6 */
  307. '4', 0x00, /* wcChar7 */
  308. '5', 0x00, /* wcChar8 */
  309. '6', 0x00, /* wcChar9 */
  310. #ifdef CONFIG_USB_HS
  311. ///////////////////////////////////////
  312. /// device qualifier descriptor
  313. ///////////////////////////////////////
  314. 0x0a,
  315. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  316. 0x00,
  317. 0x02,
  318. 0x00,
  319. 0x00,
  320. 0x00,
  321. 0x40,
  322. 0x00,
  323. 0x00,
  324. #endif
  325. 0x00
  326. };
  327. #endif
  328. /* USB HID device Configuration Descriptor */
  329. static uint8_t hid_desc[9] __ALIGN_END = {
  330. /* 18 */
  331. 0x09, /* bLength: HID Descriptor size */
  332. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  333. 0x11, /* bcdHID: HID Class Spec release number */
  334. 0x01,
  335. 0x00, /* bCountryCode: Hardware target country */
  336. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  337. 0x22, /* bDescriptorType */
  338. HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  339. 0x00,
  340. };
  341. static const uint8_t hid_keyboard_report_desc[HID_KEYBOARD_REPORT_DESC_SIZE] = {
  342. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  343. 0x09, 0x06, // USAGE (Keyboard)
  344. 0xa1, 0x01, // COLLECTION (Application)
  345. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  346. 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
  347. 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
  348. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  349. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  350. 0x75, 0x01, // REPORT_SIZE (1)
  351. 0x95, 0x08, // REPORT_COUNT (8)
  352. 0x81, 0x02, // INPUT (Data,Var,Abs)
  353. 0x95, 0x01, // REPORT_COUNT (1)
  354. 0x75, 0x08, // REPORT_SIZE (8)
  355. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  356. 0x95, 0x05, // REPORT_COUNT (5)
  357. 0x75, 0x01, // REPORT_SIZE (1)
  358. 0x05, 0x08, // USAGE_PAGE (LEDs)
  359. 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
  360. 0x29, 0x05, // USAGE_MAXIMUM (Kana)
  361. 0x91, 0x02, // OUTPUT (Data,Var,Abs)
  362. 0x95, 0x01, // REPORT_COUNT (1)
  363. 0x75, 0x03, // REPORT_SIZE (3)
  364. 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
  365. 0x95, 0x06, // REPORT_COUNT (6)
  366. 0x75, 0x08, // REPORT_SIZE (8)
  367. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  368. 0x25, 0xFF, // LOGICAL_MAXIMUM (255)
  369. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  370. 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
  371. 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
  372. 0x81, 0x00, // INPUT (Data,Ary,Abs)
  373. 0xc0 // END_COLLECTION
  374. };
  375. #define HID_STATE_IDLE 0
  376. #define HID_STATE_BUSY 1
  377. /*!< hid state ! Data can be sent only when state is idle */
  378. static volatile uint8_t hid_state = HID_STATE_IDLE;
  379. static void usbd_event_handler(uint8_t busid, uint8_t event)
  380. {
  381. switch (event) {
  382. case USBD_EVENT_RESET:
  383. break;
  384. case USBD_EVENT_CONNECTED:
  385. break;
  386. case USBD_EVENT_DISCONNECTED:
  387. break;
  388. case USBD_EVENT_RESUME:
  389. break;
  390. case USBD_EVENT_SUSPEND:
  391. break;
  392. case USBD_EVENT_CONFIGURED:
  393. hid_state = HID_STATE_IDLE;
  394. break;
  395. case USBD_EVENT_SET_REMOTE_WAKEUP:
  396. break;
  397. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  398. break;
  399. default:
  400. break;
  401. }
  402. }
  403. void usbd_hid_int_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  404. {
  405. hid_state = HID_STATE_IDLE;
  406. }
  407. static struct usbd_endpoint hid_in_ep = {
  408. .ep_cb = usbd_hid_int_callback,
  409. .ep_addr = HID_INT_EP
  410. };
  411. static struct usbd_interface intf0;
  412. void webusb_hid_keyboard_init(uint8_t busid, uintptr_t reg_base)
  413. {
  414. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  415. usbd_desc_register(busid, &webusb_hid_descriptor);
  416. #else
  417. usbd_desc_register(busid, webusb_hid_descriptor);
  418. #endif
  419. #ifndef CONFIG_USBDEV_ADVANCE_DESC
  420. usbd_bos_desc_register(busid, &bos_desc);
  421. usbd_msosv2_desc_register(busid, &msosv2_desc);
  422. usbd_webusb_desc_register(busid, &webusb_url_desc);
  423. #endif
  424. usbd_add_interface(busid, usbd_hid_init_intf(busid, &intf0, hid_keyboard_report_desc, HID_KEYBOARD_REPORT_DESC_SIZE));
  425. usbd_add_endpoint(busid, &hid_in_ep);
  426. usbd_initialize(busid, reg_base, usbd_event_handler);
  427. }
  428. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[64];
  429. void hid_keyboard_test(uint8_t busid)
  430. {
  431. const uint8_t sendbuffer[8] = { 0x00, 0x00, HID_KBD_USAGE_A, 0x00, 0x00, 0x00, 0x00, 0x00 };
  432. if(usb_device_is_configured(busid) == false) {
  433. return;
  434. }
  435. memcpy(write_buffer, sendbuffer, 8);
  436. hid_state = HID_STATE_BUSY;
  437. usbd_ep_start_write(busid, HID_INT_EP, write_buffer, 8);
  438. while (hid_state == HID_STATE_BUSY) {
  439. }
  440. }