winusb2.0_template.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #define WINUSB_VENDOR_CODE 0x17
  8. #define WINUSB_NUM 1
  9. // note that if device is composite device, you should use USB_MSOSV2_COMP_ID_FUNCTION_WINUSB_MULTI_DESCRIPTOR_INIT
  10. const uint8_t WINUSB_WCIDDescriptor[] = {
  11. #if WINUSB_NUM == 1
  12. USB_MSOSV2_COMP_ID_SET_HEADER_DESCRIPTOR_INIT(10 + USB_MSOSV2_COMP_ID_FUNCTION_WINUSB_SINGLE_DESCRIPTOR_LEN),
  13. USB_MSOSV2_COMP_ID_FUNCTION_WINUSB_SINGLE_DESCRIPTOR_INIT(),
  14. #else
  15. USB_MSOSV2_COMP_ID_SET_HEADER_DESCRIPTOR_INIT(10 + WINUSB_NUM * USB_MSOSV2_COMP_ID_FUNCTION_WINUSB_MULTI_DESCRIPTOR_LEN),
  16. USB_MSOSV2_COMP_ID_FUNCTION_WINUSB_MULTI_DESCRIPTOR_INIT(0x00),
  17. USB_MSOSV2_COMP_ID_FUNCTION_WINUSB_MULTI_DESCRIPTOR_INIT(0x01),
  18. #endif
  19. };
  20. const uint8_t USBD_BinaryObjectStoreDescriptor[] = {
  21. USB_BOS_HEADER_DESCRIPTOR_INIT(5 + USB_BOS_CAP_PLATFORM_WINUSB_DESCRIPTOR_LEN, 1),
  22. USB_BOS_CAP_PLATFORM_WINUSB_DESCRIPTOR_INIT(WINUSB_VENDOR_CODE, sizeof(WINUSB_WCIDDescriptor)),
  23. };
  24. const struct usb_msosv2_descriptor msosv2_desc = {
  25. .vendor_code = WINUSB_VENDOR_CODE,
  26. .compat_id = WINUSB_WCIDDescriptor,
  27. .compat_id_len = sizeof(WINUSB_WCIDDescriptor),
  28. };
  29. const struct usb_bos_descriptor bos_desc = {
  30. .string = USBD_BinaryObjectStoreDescriptor,
  31. .string_len = sizeof(USBD_BinaryObjectStoreDescriptor),
  32. };
  33. #define WINUSB_IN_EP 0x81
  34. #define WINUSB_OUT_EP 0x02
  35. #define USBD_VID 0xFFFE
  36. #define USBD_PID 0xffff
  37. #define USBD_MAX_POWER 100
  38. #define USBD_LANGID_STRING 1033
  39. #if WINUSB_NUM == 1
  40. #define USB_CONFIG_SIZE (9 + 9 + 7 + 7)
  41. #define INTF_NUM 1
  42. #else
  43. #define WINUSB_IN_EP2 0x83
  44. #define WINUSB_OUT_EP2 0x04
  45. #define USB_CONFIG_SIZE (9 + 9 + 7 + 7 + 9 + 7 + 7)
  46. #define INTF_NUM 2
  47. #endif
  48. #ifdef CONFIG_USB_HS
  49. #define WINUSB_EP_MPS 512
  50. #else
  51. #define WINUSB_EP_MPS 64
  52. #endif
  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, 0x0001, 0x01)
  56. };
  57. static const uint8_t config_descriptor[] = {
  58. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, INTF_NUM, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  59. USB_INTERFACE_DESCRIPTOR_INIT(0x00, 0x00, 0x02, 0xff, 0xff, 0x00, 0x04),
  60. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_IN_EP, 0x02, WINUSB_EP_MPS, 0x00),
  61. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_OUT_EP, 0x02, WINUSB_EP_MPS, 0x00),
  62. #if WINUSB_NUM == 2
  63. USB_INTERFACE_DESCRIPTOR_INIT(0x01, 0x00, 0x02, 0xff, 0xff, 0x00, 0x05),
  64. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_IN_EP2, 0x02, WINUSB_EP_MPS, 0x00),
  65. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_OUT_EP2, 0x02, WINUSB_EP_MPS, 0x00),
  66. #endif
  67. };
  68. static const uint8_t device_quality_descriptor[] = {
  69. ///////////////////////////////////////
  70. /// device qualifier descriptor
  71. ///////////////////////////////////////
  72. 0x0a,
  73. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  74. 0x00,
  75. 0x02,
  76. 0x00,
  77. 0x00,
  78. 0x00,
  79. 0x40,
  80. 0x00,
  81. 0x00,
  82. };
  83. static const char *string_descriptors[] = {
  84. (const char[]){ 0x09, 0x04 }, /* Langid */
  85. "CherryUSB", /* Manufacturer */
  86. "CherryUSB WINUSB DEMO", /* Product */
  87. "2022123456", /* Serial Number */
  88. "CherryUSB WINUSB DEMO 1", /* STRING4 */
  89. "CherryUSB WINUSB DEMO 2", /* STRING5 */
  90. };
  91. static const uint8_t *device_descriptor_callback(uint8_t speed)
  92. {
  93. return device_descriptor;
  94. }
  95. static const uint8_t *config_descriptor_callback(uint8_t speed)
  96. {
  97. return config_descriptor;
  98. }
  99. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  100. {
  101. return device_quality_descriptor;
  102. }
  103. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  104. {
  105. if (index > 5) {
  106. return NULL;
  107. }
  108. return string_descriptors[index];
  109. }
  110. const struct usb_descriptor winusbv2_descriptor = {
  111. .device_descriptor_callback = device_descriptor_callback,
  112. .config_descriptor_callback = config_descriptor_callback,
  113. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  114. .string_descriptor_callback = string_descriptor_callback,
  115. .msosv2_descriptor = &msosv2_desc,
  116. .bos_descriptor = &bos_desc,
  117. };
  118. #else
  119. const uint8_t winusbv2_descriptor[] = {
  120. USB_DEVICE_DESCRIPTOR_INIT(USB_2_1, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0001, 0x01),
  121. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, INTF_NUM, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  122. USB_INTERFACE_DESCRIPTOR_INIT(0x00, 0x00, 0x02, 0xff, 0xff, 0x00, 0x04),
  123. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_IN_EP, 0x02, WINUSB_EP_MPS, 0x00),
  124. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_OUT_EP, 0x02, WINUSB_EP_MPS, 0x00),
  125. #if WINUSB_NUM == 2
  126. USB_INTERFACE_DESCRIPTOR_INIT(0x01, 0x00, 0x02, 0xff, 0xff, 0x00, 0x05),
  127. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_IN_EP2, 0x02, WINUSB_EP_MPS, 0x00),
  128. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_OUT_EP2, 0x02, WINUSB_EP_MPS, 0x00),
  129. #endif
  130. ///////////////////////////////////////
  131. /// string0 descriptor
  132. ///////////////////////////////////////
  133. USB_LANGID_INIT(USBD_LANGID_STRING),
  134. ///////////////////////////////////////
  135. /// string1 descriptor
  136. ///////////////////////////////////////
  137. 0x14, /* bLength */
  138. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  139. 'C', 0x00, /* wcChar0 */
  140. 'h', 0x00, /* wcChar1 */
  141. 'e', 0x00, /* wcChar2 */
  142. 'r', 0x00, /* wcChar3 */
  143. 'r', 0x00, /* wcChar4 */
  144. 'y', 0x00, /* wcChar5 */
  145. 'U', 0x00, /* wcChar6 */
  146. 'S', 0x00, /* wcChar7 */
  147. 'B', 0x00, /* wcChar8 */
  148. ///////////////////////////////////////
  149. /// string2 descriptor
  150. ///////////////////////////////////////
  151. 0x2C, /* bLength */
  152. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  153. 'C', 0x00, /* wcChar0 */
  154. 'h', 0x00, /* wcChar1 */
  155. 'e', 0x00, /* wcChar2 */
  156. 'r', 0x00, /* wcChar3 */
  157. 'r', 0x00, /* wcChar4 */
  158. 'y', 0x00, /* wcChar5 */
  159. 'U', 0x00, /* wcChar6 */
  160. 'S', 0x00, /* wcChar7 */
  161. 'B', 0x00, /* wcChar8 */
  162. ' ', 0x00, /* wcChar9 */
  163. 'W', 0x00, /* wcChar10 */
  164. 'I', 0x00, /* wcChar11 */
  165. 'N', 0x00, /* wcChar12 */
  166. 'U', 0x00, /* wcChar13 */
  167. 'S', 0x00, /* wcChar14 */
  168. 'B', 0x00, /* wcChar15 */
  169. ' ', 0x00, /* wcChar16 */
  170. 'D', 0x00, /* wcChar17 */
  171. 'E', 0x00, /* wcChar18 */
  172. 'M', 0x00, /* wcChar19 */
  173. 'O', 0x00, /* wcChar20 */
  174. ///////////////////////////////////////
  175. /// string3 descriptor
  176. ///////////////////////////////////////
  177. 0x16, /* bLength */
  178. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  179. '2', 0x00, /* wcChar0 */
  180. '0', 0x00, /* wcChar1 */
  181. '2', 0x00, /* wcChar2 */
  182. '1', 0x00, /* wcChar3 */
  183. '1', 0x00, /* wcChar4 */
  184. '2', 0x00, /* wcChar5 */
  185. '3', 0x00, /* wcChar6 */
  186. '4', 0x00, /* wcChar7 */
  187. '5', 0x00, /* wcChar8 */
  188. '6', 0x00, /* wcChar9 */
  189. ///////////////////////////////////////
  190. /// string4 descriptor
  191. ///////////////////////////////////////
  192. 0x30, /* bLength */
  193. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  194. 'C', 0x00, /* wcChar0 */
  195. 'h', 0x00, /* wcChar1 */
  196. 'e', 0x00, /* wcChar2 */
  197. 'r', 0x00, /* wcChar3 */
  198. 'r', 0x00, /* wcChar4 */
  199. 'y', 0x00, /* wcChar5 */
  200. 'U', 0x00, /* wcChar6 */
  201. 'S', 0x00, /* wcChar7 */
  202. 'B', 0x00, /* wcChar8 */
  203. ' ', 0x00, /* wcChar9 */
  204. 'W', 0x00, /* wcChar10 */
  205. 'I', 0x00, /* wcChar11 */
  206. 'N', 0x00, /* wcChar12 */
  207. 'U', 0x00, /* wcChar13 */
  208. 'S', 0x00, /* wcChar14 */
  209. 'B', 0x00, /* wcChar15 */
  210. ' ', 0x00, /* wcChar16 */
  211. 'D', 0x00, /* wcChar17 */
  212. 'E', 0x00, /* wcChar18 */
  213. 'M', 0x00, /* wcChar19 */
  214. 'O', 0x00, /* wcChar20 */
  215. ' ', 0x00, /* wcChar16 */
  216. '1', 0x00, /* wcChar21 */
  217. ///////////////////////////////////////
  218. /// string5 descriptor
  219. ///////////////////////////////////////
  220. 0x30, /* bLength */
  221. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  222. 'C', 0x00, /* wcChar0 */
  223. 'h', 0x00, /* wcChar1 */
  224. 'e', 0x00, /* wcChar2 */
  225. 'r', 0x00, /* wcChar3 */
  226. 'r', 0x00, /* wcChar4 */
  227. 'y', 0x00, /* wcChar5 */
  228. 'U', 0x00, /* wcChar6 */
  229. 'S', 0x00, /* wcChar7 */
  230. 'B', 0x00, /* wcChar8 */
  231. ' ', 0x00, /* wcChar9 */
  232. 'W', 0x00, /* wcChar10 */
  233. 'I', 0x00, /* wcChar11 */
  234. 'N', 0x00, /* wcChar12 */
  235. 'U', 0x00, /* wcChar13 */
  236. 'S', 0x00, /* wcChar14 */
  237. 'B', 0x00, /* wcChar15 */
  238. ' ', 0x00, /* wcChar16 */
  239. 'D', 0x00, /* wcChar17 */
  240. 'E', 0x00, /* wcChar18 */
  241. 'M', 0x00, /* wcChar19 */
  242. 'O', 0x00, /* wcChar20 */
  243. ' ', 0x00, /* wcChar16 */
  244. '2', 0x00, /* wcChar21 */
  245. #ifdef CONFIG_USB_HS
  246. ///////////////////////////////////////
  247. /// device qualifier descriptor
  248. ///////////////////////////////////////
  249. 0x0a,
  250. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  251. 0x00,
  252. 0x02,
  253. 0x00,
  254. 0x00,
  255. 0x00,
  256. 0x40,
  257. 0x00,
  258. 0x00,
  259. #endif
  260. 0x00
  261. };
  262. #endif
  263. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[2048];
  264. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[2048];
  265. volatile bool ep_tx_busy_flag = false;
  266. static void usbd_event_handler(uint8_t busid, uint8_t event)
  267. {
  268. switch (event) {
  269. case USBD_EVENT_RESET:
  270. break;
  271. case USBD_EVENT_CONNECTED:
  272. break;
  273. case USBD_EVENT_DISCONNECTED:
  274. break;
  275. case USBD_EVENT_RESUME:
  276. break;
  277. case USBD_EVENT_SUSPEND:
  278. break;
  279. case USBD_EVENT_CONFIGURED:
  280. ep_tx_busy_flag = false;
  281. /* setup first out ep read transfer */
  282. usbd_ep_start_read(busid, WINUSB_OUT_EP, read_buffer, 2048);
  283. #if WINUSB_NUM == 2
  284. usbd_ep_start_read(busid, WINUSB_OUT_EP2, read_buffer, 2048);
  285. #endif
  286. break;
  287. case USBD_EVENT_SET_REMOTE_WAKEUP:
  288. break;
  289. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  290. break;
  291. default:
  292. break;
  293. }
  294. }
  295. void usbd_winusb_out(uint8_t busid, uint8_t ep, uint32_t nbytes)
  296. {
  297. USB_LOG_RAW("actual out len:%d\r\n", (unsigned int)nbytes);
  298. // for (int i = 0; i < 100; i++) {
  299. // printf("%02x ", read_buffer[i]);
  300. // }
  301. // printf("\r\n");
  302. usbd_ep_start_write(busid, WINUSB_IN_EP, read_buffer, nbytes);
  303. /* setup next out ep read transfer */
  304. usbd_ep_start_read(busid, WINUSB_OUT_EP, read_buffer, 2048);
  305. }
  306. void usbd_winusb_in(uint8_t busid, uint8_t ep, uint32_t nbytes)
  307. {
  308. USB_LOG_RAW("actual in len:%d\r\n", (unsigned int)nbytes);
  309. if ((nbytes % WINUSB_EP_MPS) == 0 && nbytes) {
  310. /* send zlp */
  311. usbd_ep_start_write(busid, WINUSB_IN_EP, NULL, 0);
  312. } else {
  313. ep_tx_busy_flag = false;
  314. }
  315. }
  316. struct usbd_endpoint winusb_out_ep1 = {
  317. .ep_addr = WINUSB_OUT_EP,
  318. .ep_cb = usbd_winusb_out
  319. };
  320. struct usbd_endpoint winusb_in_ep1 = {
  321. .ep_addr = WINUSB_IN_EP,
  322. .ep_cb = usbd_winusb_in
  323. };
  324. struct usbd_interface intf0;
  325. #if WINUSB_NUM == 2
  326. void usbd_winusb_out2(uint8_t busid, uint8_t ep, uint32_t nbytes)
  327. {
  328. USB_LOG_RAW("actual out len:%d\r\n", (unsigned int)nbytes);
  329. // for (int i = 0; i < 100; i++) {
  330. // printf("%02x ", read_buffer[i]);
  331. // }
  332. // printf("\r\n");
  333. usbd_ep_start_write(busid, WINUSB_IN_EP2, read_buffer, nbytes);
  334. /* setup next out ep read transfer */
  335. usbd_ep_start_read(busid, WINUSB_OUT_EP2, read_buffer, 2048);
  336. }
  337. void usbd_winusb_in2(uint8_t busid, uint8_t ep, uint32_t nbytes)
  338. {
  339. USB_LOG_RAW("actual in len:%d\r\n", (unsigned int)nbytes);
  340. if ((nbytes % usbd_get_ep_mps(busid, ep)) == 0 && nbytes) {
  341. /* send zlp */
  342. usbd_ep_start_write(busid, WINUSB_IN_EP2, NULL, 0);
  343. } else {
  344. ep_tx_busy_flag = false;
  345. }
  346. }
  347. struct usbd_endpoint winusb_out_ep2 = {
  348. .ep_addr = WINUSB_OUT_EP2,
  349. .ep_cb = usbd_winusb_out2
  350. };
  351. struct usbd_endpoint winusb_in_ep2 = {
  352. .ep_addr = WINUSB_IN_EP2,
  353. .ep_cb = usbd_winusb_in2
  354. };
  355. struct usbd_interface intf1;
  356. #endif
  357. void winusbv2_init(uint8_t busid, uintptr_t reg_base)
  358. {
  359. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  360. usbd_desc_register(busid, &winusbv2_descriptor);
  361. #else
  362. usbd_desc_register(busid, winusbv2_descriptor);
  363. #endif
  364. #ifndef CONFIG_USBDEV_ADVANCE_DESC
  365. usbd_bos_desc_register(busid, &bos_desc);
  366. usbd_msosv2_desc_register(busid, &msosv2_desc);
  367. #endif
  368. usbd_add_interface(busid, &intf0);
  369. usbd_add_endpoint(busid, &winusb_out_ep1);
  370. usbd_add_endpoint(busid, &winusb_in_ep1);
  371. #if WINUSB_NUM == 2
  372. usbd_add_interface(busid, &intf1);
  373. usbd_add_endpoint(busid, &winusb_out_ep2);
  374. usbd_add_endpoint(busid, &winusb_in_ep2);
  375. #endif
  376. usbd_initialize(busid, reg_base, usbd_event_handler);
  377. }