webusb_hid_template.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. static const uint8_t webusb_hid_descriptor[] = {
  126. USB_DEVICE_DESCRIPTOR_INIT(USB_2_1, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01),
  127. USB_CONFIG_DESCRIPTOR_INIT(USB_HID_CONFIG_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  128. /************** Descriptor of Joystick Mouse interface ****************/
  129. /* 09 */
  130. 0x09, /* bLength: Interface Descriptor size */
  131. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
  132. 0x00, /* bInterfaceNumber: Number of Interface */
  133. 0x00, /* bAlternateSetting: Alternate setting */
  134. 0x01, /* bNumEndpoints */
  135. 0x03, /* bInterfaceClass: HID */
  136. 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
  137. 0x01, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
  138. 0, /* iInterface: Index of string descriptor */
  139. /******************** Descriptor of Joystick Mouse HID ********************/
  140. /* 18 */
  141. 0x09, /* bLength: HID Descriptor size */
  142. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  143. 0x11, /* bcdHID: HID Class Spec release number */
  144. 0x01,
  145. 0x00, /* bCountryCode: Hardware target country */
  146. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  147. 0x22, /* bDescriptorType */
  148. HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  149. 0x00,
  150. /******************** Descriptor of Mouse endpoint ********************/
  151. /* 27 */
  152. 0x07, /* bLength: Endpoint Descriptor size */
  153. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
  154. HID_INT_EP, /* bEndpointAddress: Endpoint Address (IN) */
  155. 0x03, /* bmAttributes: Interrupt endpoint */
  156. HID_INT_EP_SIZE, /* wMaxPacketSize: 4 Byte max */
  157. 0x00,
  158. HID_INT_EP_INTERVAL, /* bInterval: Polling Interval */
  159. /* 34 */
  160. USB_INTERFACE_DESCRIPTOR_INIT(USBD_WEBUSB_INTF_NUM, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00),
  161. ///////////////////////////////////////
  162. /// string0 descriptor
  163. ///////////////////////////////////////
  164. USB_LANGID_INIT(USBD_LANGID_STRING),
  165. ///////////////////////////////////////
  166. /// string1 descriptor
  167. ///////////////////////////////////////
  168. 0x14, /* bLength */
  169. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  170. 'C', 0x00, /* wcChar0 */
  171. 'h', 0x00, /* wcChar1 */
  172. 'e', 0x00, /* wcChar2 */
  173. 'r', 0x00, /* wcChar3 */
  174. 'r', 0x00, /* wcChar4 */
  175. 'y', 0x00, /* wcChar5 */
  176. 'U', 0x00, /* wcChar6 */
  177. 'S', 0x00, /* wcChar7 */
  178. 'B', 0x00, /* wcChar8 */
  179. ///////////////////////////////////////
  180. /// string2 descriptor
  181. ///////////////////////////////////////
  182. 0x26, /* bLength */
  183. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  184. 'C', 0x00, /* wcChar0 */
  185. 'h', 0x00, /* wcChar1 */
  186. 'e', 0x00, /* wcChar2 */
  187. 'r', 0x00, /* wcChar3 */
  188. 'r', 0x00, /* wcChar4 */
  189. 'y', 0x00, /* wcChar5 */
  190. 'U', 0x00, /* wcChar6 */
  191. 'S', 0x00, /* wcChar7 */
  192. 'B', 0x00, /* wcChar8 */
  193. ' ', 0x00, /* wcChar9 */
  194. 'H', 0x00, /* wcChar10 */
  195. 'I', 0x00, /* wcChar11 */
  196. 'D', 0x00, /* wcChar12 */
  197. ' ', 0x00, /* wcChar13 */
  198. 'D', 0x00, /* wcChar14 */
  199. 'E', 0x00, /* wcChar15 */
  200. 'M', 0x00, /* wcChar16 */
  201. 'O', 0x00, /* wcChar17 */
  202. ///////////////////////////////////////
  203. /// string3 descriptor
  204. ///////////////////////////////////////
  205. 0x16, /* bLength */
  206. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  207. '2', 0x00, /* wcChar0 */
  208. '0', 0x00, /* wcChar1 */
  209. '2', 0x00, /* wcChar2 */
  210. '2', 0x00, /* wcChar3 */
  211. '1', 0x00, /* wcChar4 */
  212. '2', 0x00, /* wcChar5 */
  213. '3', 0x00, /* wcChar6 */
  214. '4', 0x00, /* wcChar7 */
  215. '5', 0x00, /* wcChar8 */
  216. '6', 0x00, /* wcChar9 */
  217. #ifdef CONFIG_USB_HS
  218. ///////////////////////////////////////
  219. /// device qualifier descriptor
  220. ///////////////////////////////////////
  221. 0x0a,
  222. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  223. 0x00,
  224. 0x02,
  225. 0x00,
  226. 0x00,
  227. 0x00,
  228. 0x40,
  229. 0x00,
  230. 0x00,
  231. #endif
  232. 0x00
  233. };
  234. /* USB HID device Configuration Descriptor */
  235. static uint8_t hid_desc[9] __ALIGN_END = {
  236. /* 18 */
  237. 0x09, /* bLength: HID Descriptor size */
  238. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  239. 0x11, /* bcdHID: HID Class Spec release number */
  240. 0x01,
  241. 0x00, /* bCountryCode: Hardware target country */
  242. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  243. 0x22, /* bDescriptorType */
  244. HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  245. 0x00,
  246. };
  247. static const uint8_t hid_keyboard_report_desc[HID_KEYBOARD_REPORT_DESC_SIZE] = {
  248. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  249. 0x09, 0x06, // USAGE (Keyboard)
  250. 0xa1, 0x01, // COLLECTION (Application)
  251. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  252. 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
  253. 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
  254. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  255. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  256. 0x75, 0x01, // REPORT_SIZE (1)
  257. 0x95, 0x08, // REPORT_COUNT (8)
  258. 0x81, 0x02, // INPUT (Data,Var,Abs)
  259. 0x95, 0x01, // REPORT_COUNT (1)
  260. 0x75, 0x08, // REPORT_SIZE (8)
  261. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  262. 0x95, 0x05, // REPORT_COUNT (5)
  263. 0x75, 0x01, // REPORT_SIZE (1)
  264. 0x05, 0x08, // USAGE_PAGE (LEDs)
  265. 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
  266. 0x29, 0x05, // USAGE_MAXIMUM (Kana)
  267. 0x91, 0x02, // OUTPUT (Data,Var,Abs)
  268. 0x95, 0x01, // REPORT_COUNT (1)
  269. 0x75, 0x03, // REPORT_SIZE (3)
  270. 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
  271. 0x95, 0x06, // REPORT_COUNT (6)
  272. 0x75, 0x08, // REPORT_SIZE (8)
  273. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  274. 0x25, 0xFF, // LOGICAL_MAXIMUM (255)
  275. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  276. 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
  277. 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
  278. 0x81, 0x00, // INPUT (Data,Ary,Abs)
  279. 0xc0 // END_COLLECTION
  280. };
  281. #define HID_STATE_IDLE 0
  282. #define HID_STATE_BUSY 1
  283. /*!< hid state ! Data can be sent only when state is idle */
  284. static volatile uint8_t hid_state = HID_STATE_IDLE;
  285. static void usbd_event_handler(uint8_t busid, uint8_t event)
  286. {
  287. switch (event) {
  288. case USBD_EVENT_RESET:
  289. break;
  290. case USBD_EVENT_CONNECTED:
  291. break;
  292. case USBD_EVENT_DISCONNECTED:
  293. break;
  294. case USBD_EVENT_RESUME:
  295. break;
  296. case USBD_EVENT_SUSPEND:
  297. break;
  298. case USBD_EVENT_CONFIGURED:
  299. hid_state = HID_STATE_IDLE;
  300. break;
  301. case USBD_EVENT_SET_REMOTE_WAKEUP:
  302. break;
  303. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  304. break;
  305. default:
  306. break;
  307. }
  308. }
  309. void usbd_hid_int_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  310. {
  311. hid_state = HID_STATE_IDLE;
  312. }
  313. static struct usbd_endpoint hid_in_ep = {
  314. .ep_cb = usbd_hid_int_callback,
  315. .ep_addr = HID_INT_EP
  316. };
  317. static struct usbd_interface intf0;
  318. void webusb_hid_keyboard_init(uint8_t busid, uintptr_t reg_base)
  319. {
  320. usbd_desc_register(busid, webusb_hid_descriptor);
  321. usbd_bos_desc_register(busid, &bos_desc);
  322. usbd_msosv2_desc_register(busid, &msosv2_desc);
  323. usbd_webusb_desc_register(busid, &webusb_url_desc);
  324. usbd_add_interface(busid, usbd_hid_init_intf(busid, &intf0, hid_keyboard_report_desc, HID_KEYBOARD_REPORT_DESC_SIZE));
  325. usbd_add_endpoint(busid, &hid_in_ep);
  326. usbd_initialize(busid, reg_base, usbd_event_handler);
  327. }
  328. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[64];
  329. void hid_keyboard_test(uint8_t busid)
  330. {
  331. const uint8_t sendbuffer[8] = { 0x00, 0x00, HID_KBD_USAGE_A, 0x00, 0x00, 0x00, 0x00, 0x00 };
  332. if(usb_device_is_configured(busid) == false) {
  333. return;
  334. }
  335. memcpy(write_buffer, sendbuffer, 8);
  336. hid_state = HID_STATE_BUSY;
  337. usbd_ep_start_write(busid, HID_INT_EP, write_buffer, 8);
  338. while (hid_state == HID_STATE_BUSY) {
  339. }
  340. }