winusb1.0_template.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usbd_cdc_acm.h"
  8. #define WCID_VENDOR_CODE 0x17
  9. #define DOUBLE_WINUSB 0
  10. __ALIGN_BEGIN const uint8_t WCID_StringDescriptor_MSOS[18] __ALIGN_END = {
  11. ///////////////////////////////////////
  12. /// MS OS string descriptor
  13. ///////////////////////////////////////
  14. 0x12, /* bLength */
  15. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  16. /* MSFT100 */
  17. 'M', 0x00, 'S', 0x00, 'F', 0x00, 'T', 0x00, /* wcChar_7 */
  18. '1', 0x00, '0', 0x00, '0', 0x00, /* wcChar_7 */
  19. WCID_VENDOR_CODE, /* bVendorCode */
  20. 0x00, /* bReserved */
  21. };
  22. #if DOUBLE_WINUSB == 0
  23. __ALIGN_BEGIN const uint8_t WINUSB_WCIDDescriptor[40] __ALIGN_END = {
  24. ///////////////////////////////////////
  25. /// WCID descriptor
  26. ///////////////////////////////////////
  27. 0x28, 0x00, 0x00, 0x00, /* dwLength */
  28. 0x00, 0x01, /* bcdVersion */
  29. 0x04, 0x00, /* wIndex */
  30. 0x01, /* bCount */
  31. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* bReserved_7 */
  32. ///////////////////////////////////////
  33. /// WCID function descriptor
  34. ///////////////////////////////////////
  35. 0x00, /* bFirstInterfaceNumber */
  36. 0x01, /* bReserved */
  37. /* WINUSB */
  38. 'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00, /* cCID_8 */
  39. /* */
  40. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* cSubCID_8 */
  41. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* bReserved_6 */
  42. };
  43. #else
  44. __ALIGN_BEGIN const uint8_t WINUSB_WCIDDescriptor[64] __ALIGN_END = {
  45. ///////////////////////////////////////
  46. /// WCID descriptor
  47. ///////////////////////////////////////
  48. 0x40, 0x00, 0x00, 0x00, /* dwLength */
  49. 0x00, 0x01, /* bcdVersion */
  50. 0x04, 0x00, /* wIndex */
  51. 0x02, /* bCount */
  52. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* bReserved_7 */
  53. ///////////////////////////////////////
  54. /// WCID function descriptor
  55. ///////////////////////////////////////
  56. 0x00, /* bFirstInterfaceNumber */
  57. 0x01, /* bReserved */
  58. /* WINUSB */
  59. 'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00, /* cCID_8 */
  60. /* */
  61. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* cSubCID_8 */
  62. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* bReserved_6 */
  63. ///////////////////////////////////////
  64. /// WCID function descriptor
  65. ///////////////////////////////////////
  66. 0x01, /* bFirstInterfaceNumber */
  67. 0x01, /* bReserved */
  68. /* WINUSB */
  69. 'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00, /* cCID_8 */
  70. /* */
  71. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* cSubCID_8 */
  72. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* bReserved_6 */
  73. };
  74. #endif
  75. __ALIGN_BEGIN const uint8_t WINUSB_IF0_WCIDProperties [142] __ALIGN_END = {
  76. ///////////////////////////////////////
  77. /// WCID property descriptor
  78. ///////////////////////////////////////
  79. 0x8e, 0x00, 0x00, 0x00, /* dwLength */
  80. 0x00, 0x01, /* bcdVersion */
  81. 0x05, 0x00, /* wIndex */
  82. 0x01, 0x00, /* wCount */
  83. ///////////////////////////////////////
  84. /// registry propter descriptor
  85. ///////////////////////////////////////
  86. 0x84, 0x00, 0x00, 0x00, /* dwSize */
  87. 0x01, 0x00, 0x00, 0x00, /* dwPropertyDataType */
  88. 0x28, 0x00, /* wPropertyNameLength */
  89. /* DeviceInterfaceGUID */
  90. 'D', 0x00, 'e', 0x00, 'v', 0x00, 'i', 0x00, /* wcName_20 */
  91. 'c', 0x00, 'e', 0x00, 'I', 0x00, 'n', 0x00, /* wcName_20 */
  92. 't', 0x00, 'e', 0x00, 'r', 0x00, 'f', 0x00, /* wcName_20 */
  93. 'a', 0x00, 'c', 0x00, 'e', 0x00, 'G', 0x00, /* wcName_20 */
  94. 'U', 0x00, 'I', 0x00, 'D', 0x00, 0x00, 0x00, /* wcName_20 */
  95. 0x4e, 0x00, 0x00, 0x00, /* dwPropertyDataLength */
  96. /* {1D4B2365-4749-48EA-B38A-7C6FDDDD7E26} */
  97. '{', 0x00, '1', 0x00, 'D', 0x00, '4', 0x00, /* wcData_39 */
  98. 'B', 0x00, '2', 0x00, '3', 0x00, '6', 0x00, /* wcData_39 */
  99. '5', 0x00, '-', 0x00, '4', 0x00, '7', 0x00, /* wcData_39 */
  100. '4', 0x00, '9', 0x00, '-', 0x00, '4', 0x00, /* wcData_39 */
  101. '8', 0x00, 'E', 0x00, 'A', 0x00, '-', 0x00, /* wcData_39 */
  102. 'B', 0x00, '3', 0x00, '8', 0x00, 'A', 0x00, /* wcData_39 */
  103. '-', 0x00, '7', 0x00, 'C', 0x00, '6', 0x00, /* wcData_39 */
  104. 'F', 0x00, 'D', 0x00, 'D', 0x00, 'D', 0x00, /* wcData_39 */
  105. 'D', 0x00, '7', 0x00, 'E', 0x00, '2', 0x00, /* wcData_39 */
  106. '6', 0x00, '}', 0x00, 0x00, 0x00, /* wcData_39 */
  107. };
  108. #define WINUSB_IF1_WCID_PROPERTIES_SIZE (142)
  109. __ALIGN_BEGIN const uint8_t WINUSB_IF1_WCIDProperties [142] __ALIGN_END = {
  110. ///////////////////////////////////////
  111. /// WCID property descriptor
  112. ///////////////////////////////////////
  113. 0x8e, 0x00, 0x00, 0x00, /* dwLength */
  114. 0x00, 0x01, /* bcdVersion */
  115. 0x05, 0x00, /* wIndex */
  116. 0x01, 0x00, /* wCount */
  117. ///////////////////////////////////////
  118. /// registry propter descriptor
  119. ///////////////////////////////////////
  120. 0x84, 0x00, 0x00, 0x00, /* dwSize */
  121. 0x01, 0x00, 0x00, 0x00, /* dwPropertyDataType */
  122. 0x28, 0x00, /* wPropertyNameLength */
  123. /* DeviceInterfaceGUID */
  124. 'D', 0x00, 'e', 0x00, 'v', 0x00, 'i', 0x00, /* wcName_20 */
  125. 'c', 0x00, 'e', 0x00, 'I', 0x00, 'n', 0x00, /* wcName_20 */
  126. 't', 0x00, 'e', 0x00, 'r', 0x00, 'f', 0x00, /* wcName_20 */
  127. 'a', 0x00, 'c', 0x00, 'e', 0x00, 'G', 0x00, /* wcName_20 */
  128. 'U', 0x00, 'I', 0x00, 'D', 0x00, 0x00, 0x00, /* wcName_20 */
  129. 0x4e, 0x00, 0x00, 0x00, /* dwPropertyDataLength */
  130. /* {1D4B2365-4749-48EA-B38A-7C6FDDDD7E26} */
  131. '{', 0x00, '1', 0x00, 'D', 0x00, '4', 0x00, /* wcData_39 */
  132. 'B', 0x00, '2', 0x00, '3', 0x00, '6', 0x00, /* wcData_39 */
  133. '5', 0x00, '-', 0x00, '4', 0x00, '7', 0x00, /* wcData_39 */
  134. '4', 0x00, '9', 0x00, '-', 0x00, '4', 0x00, /* wcData_39 */
  135. '8', 0x00, 'E', 0x00, 'A', 0x00, '-', 0x00, /* wcData_39 */
  136. 'B', 0x00, '3', 0x00, '8', 0x00, 'A', 0x00, /* wcData_39 */
  137. '-', 0x00, '7', 0x00, 'C', 0x00, '6', 0x00, /* wcData_39 */
  138. 'F', 0x00, 'D', 0x00, 'D', 0x00, 'D', 0x00, /* wcData_39 */
  139. 'D', 0x00, '7', 0x00, 'E', 0x00, '2', 0x00, /* wcData_39 */
  140. '6', 0x00, '}', 0x00, 0x00, 0x00, /* wcData_39 */
  141. };
  142. const uint8_t *WINUSB_IFx_WCIDProperties[] = {
  143. WINUSB_IF0_WCIDProperties,
  144. #if DOUBLE_WINUSB == 1
  145. WINUSB_IF1_WCIDProperties,
  146. #endif
  147. };
  148. struct usb_msosv1_descriptor msosv1_desc = {
  149. .string = WCID_StringDescriptor_MSOS,
  150. .vendor_code = WCID_VENDOR_CODE,
  151. .compat_id = WINUSB_WCIDDescriptor,
  152. .comp_id_property = WINUSB_IFx_WCIDProperties,
  153. };
  154. #define WINUSB_IN_EP 0x81
  155. #define WINUSB_OUT_EP 0x02
  156. #define USBD_VID 0xefff
  157. #define USBD_PID 0xffff
  158. #define USBD_MAX_POWER 100
  159. #define USBD_LANGID_STRING 1033
  160. #if DOUBLE_WINUSB == 0
  161. #define USB_CONFIG_SIZE (9 + 9 + 7 + 7)
  162. #define INTF_NUM 1
  163. #else
  164. #define WINUSB_IN_EP2 0x83
  165. #define WINUSB_OUT_EP2 0x04
  166. #define USB_CONFIG_SIZE (9 + 9 + 7 + 7 + 9 + 7 + 7)
  167. #define INTF_NUM 2
  168. #endif
  169. #ifdef CONFIG_USB_HS
  170. #define WINUSB_EP_MPS 512
  171. #else
  172. #define WINUSB_EP_MPS 64
  173. #endif
  174. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  175. static const uint8_t device_descriptor[] = {
  176. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0001, 0x01)
  177. };
  178. static const uint8_t config_descriptor[] = {
  179. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, INTF_NUM, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  180. USB_INTERFACE_DESCRIPTOR_INIT(0x00, 0x00, 0x02, 0xff, 0xff, 0x00, 0x04),
  181. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_IN_EP, 0x02, WINUSB_EP_MPS, 0x00),
  182. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_OUT_EP, 0x02, WINUSB_EP_MPS, 0x00),
  183. #if DOUBLE_WINUSB == 1
  184. USB_INTERFACE_DESCRIPTOR_INIT(0x01, 0x00, 0x02, 0xff, 0xff, 0x00, 0x05),
  185. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_IN_EP2, 0x02, WINUSB_EP_MPS, 0x00),
  186. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_OUT_EP2, 0x02, WINUSB_EP_MPS, 0x00),
  187. #endif
  188. };
  189. static const uint8_t device_quality_descriptor[] = {
  190. ///////////////////////////////////////
  191. /// device qualifier descriptor
  192. ///////////////////////////////////////
  193. 0x0a,
  194. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  195. 0x00,
  196. 0x02,
  197. 0x00,
  198. 0x00,
  199. 0x00,
  200. 0x40,
  201. 0x00,
  202. 0x00,
  203. };
  204. static const char *string_descriptors[] = {
  205. (const char[]){ 0x09, 0x04 }, /* Langid */
  206. "CherryUSB", /* Manufacturer */
  207. "CherryUSB WINUSB DEMO", /* Product */
  208. "2022123456", /* Serial Number */
  209. "CherryUSB WINUSB DEMO 1", /* STRING4 */
  210. "CherryUSB WINUSB DEMO 2", /* STRING5 */
  211. };
  212. static const uint8_t *device_descriptor_callback(uint8_t speed)
  213. {
  214. return device_descriptor;
  215. }
  216. static const uint8_t *config_descriptor_callback(uint8_t speed)
  217. {
  218. return config_descriptor;
  219. }
  220. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  221. {
  222. return device_quality_descriptor;
  223. }
  224. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  225. {
  226. if (index > 5) {
  227. return NULL;
  228. }
  229. return string_descriptors[index];
  230. }
  231. const struct usb_descriptor winusb_descriptor = {
  232. .device_descriptor_callback = device_descriptor_callback,
  233. .config_descriptor_callback = config_descriptor_callback,
  234. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  235. .string_descriptor_callback = string_descriptor_callback,
  236. .msosv1_descriptor = &msosv1_desc
  237. };
  238. #else
  239. const uint8_t winusb_descriptor[] = {
  240. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0001, 0x01),
  241. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, INTF_NUM, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  242. USB_INTERFACE_DESCRIPTOR_INIT(0x00, 0x00, 0x02, 0xff, 0xff, 0x00, 0x04),
  243. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_IN_EP, 0x02, WINUSB_EP_MPS, 0x00),
  244. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_OUT_EP, 0x02, WINUSB_EP_MPS, 0x00),
  245. #if DOUBLE_WINUSB == 1
  246. USB_INTERFACE_DESCRIPTOR_INIT(0x01, 0x00, 0x02, 0xff, 0xff, 0x00, 0x05),
  247. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_IN_EP2, 0x02, WINUSB_EP_MPS, 0x00),
  248. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_OUT_EP2, 0x02, WINUSB_EP_MPS, 0x00),
  249. #endif
  250. ///////////////////////////////////////
  251. /// string0 descriptor
  252. ///////////////////////////////////////
  253. USB_LANGID_INIT(USBD_LANGID_STRING),
  254. ///////////////////////////////////////
  255. /// string1 descriptor
  256. ///////////////////////////////////////
  257. 0x14, /* bLength */
  258. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  259. 'C', 0x00, /* wcChar0 */
  260. 'h', 0x00, /* wcChar1 */
  261. 'e', 0x00, /* wcChar2 */
  262. 'r', 0x00, /* wcChar3 */
  263. 'r', 0x00, /* wcChar4 */
  264. 'y', 0x00, /* wcChar5 */
  265. 'U', 0x00, /* wcChar6 */
  266. 'S', 0x00, /* wcChar7 */
  267. 'B', 0x00, /* wcChar8 */
  268. ///////////////////////////////////////
  269. /// string2 descriptor
  270. ///////////////////////////////////////
  271. 0x2C, /* bLength */
  272. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  273. 'C', 0x00, /* wcChar0 */
  274. 'h', 0x00, /* wcChar1 */
  275. 'e', 0x00, /* wcChar2 */
  276. 'r', 0x00, /* wcChar3 */
  277. 'r', 0x00, /* wcChar4 */
  278. 'y', 0x00, /* wcChar5 */
  279. 'U', 0x00, /* wcChar6 */
  280. 'S', 0x00, /* wcChar7 */
  281. 'B', 0x00, /* wcChar8 */
  282. ' ', 0x00, /* wcChar9 */
  283. 'W', 0x00, /* wcChar10 */
  284. 'I', 0x00, /* wcChar11 */
  285. 'N', 0x00, /* wcChar12 */
  286. 'U', 0x00, /* wcChar13 */
  287. 'S', 0x00, /* wcChar14 */
  288. 'B', 0x00, /* wcChar15 */
  289. ' ', 0x00, /* wcChar16 */
  290. 'D', 0x00, /* wcChar17 */
  291. 'E', 0x00, /* wcChar18 */
  292. 'M', 0x00, /* wcChar19 */
  293. 'O', 0x00, /* wcChar20 */
  294. ///////////////////////////////////////
  295. /// string3 descriptor
  296. ///////////////////////////////////////
  297. 0x16, /* bLength */
  298. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  299. '2', 0x00, /* wcChar0 */
  300. '0', 0x00, /* wcChar1 */
  301. '2', 0x00, /* wcChar2 */
  302. '1', 0x00, /* wcChar3 */
  303. '1', 0x00, /* wcChar4 */
  304. '2', 0x00, /* wcChar5 */
  305. '3', 0x00, /* wcChar6 */
  306. '4', 0x00, /* wcChar7 */
  307. '5', 0x00, /* wcChar8 */
  308. '6', 0x00, /* wcChar9 */
  309. ///////////////////////////////////////
  310. /// string4 descriptor
  311. ///////////////////////////////////////
  312. 0x30, /* bLength */
  313. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  314. 'C', 0x00, /* wcChar0 */
  315. 'h', 0x00, /* wcChar1 */
  316. 'e', 0x00, /* wcChar2 */
  317. 'r', 0x00, /* wcChar3 */
  318. 'r', 0x00, /* wcChar4 */
  319. 'y', 0x00, /* wcChar5 */
  320. 'U', 0x00, /* wcChar6 */
  321. 'S', 0x00, /* wcChar7 */
  322. 'B', 0x00, /* wcChar8 */
  323. ' ', 0x00, /* wcChar9 */
  324. 'W', 0x00, /* wcChar10 */
  325. 'I', 0x00, /* wcChar11 */
  326. 'N', 0x00, /* wcChar12 */
  327. 'U', 0x00, /* wcChar13 */
  328. 'S', 0x00, /* wcChar14 */
  329. 'B', 0x00, /* wcChar15 */
  330. ' ', 0x00, /* wcChar16 */
  331. 'D', 0x00, /* wcChar17 */
  332. 'E', 0x00, /* wcChar18 */
  333. 'M', 0x00, /* wcChar19 */
  334. 'O', 0x00, /* wcChar20 */
  335. ' ', 0x00, /* wcChar16 */
  336. '1', 0x00, /* wcChar21 */
  337. ///////////////////////////////////////
  338. /// string5 descriptor
  339. ///////////////////////////////////////
  340. 0x30, /* bLength */
  341. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  342. 'C', 0x00, /* wcChar0 */
  343. 'h', 0x00, /* wcChar1 */
  344. 'e', 0x00, /* wcChar2 */
  345. 'r', 0x00, /* wcChar3 */
  346. 'r', 0x00, /* wcChar4 */
  347. 'y', 0x00, /* wcChar5 */
  348. 'U', 0x00, /* wcChar6 */
  349. 'S', 0x00, /* wcChar7 */
  350. 'B', 0x00, /* wcChar8 */
  351. ' ', 0x00, /* wcChar9 */
  352. 'W', 0x00, /* wcChar10 */
  353. 'I', 0x00, /* wcChar11 */
  354. 'N', 0x00, /* wcChar12 */
  355. 'U', 0x00, /* wcChar13 */
  356. 'S', 0x00, /* wcChar14 */
  357. 'B', 0x00, /* wcChar15 */
  358. ' ', 0x00, /* wcChar16 */
  359. 'D', 0x00, /* wcChar17 */
  360. 'E', 0x00, /* wcChar18 */
  361. 'M', 0x00, /* wcChar19 */
  362. 'O', 0x00, /* wcChar20 */
  363. ' ', 0x00, /* wcChar16 */
  364. '2', 0x00, /* wcChar21 */
  365. #ifdef CONFIG_USB_HS
  366. ///////////////////////////////////////
  367. /// device qualifier descriptor
  368. ///////////////////////////////////////
  369. 0x0a,
  370. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  371. 0x00,
  372. 0x02,
  373. 0x00,
  374. 0x00,
  375. 0x00,
  376. 0x40,
  377. 0x00,
  378. 0x00,
  379. #endif
  380. 0x00
  381. };
  382. #endif
  383. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[2048];
  384. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[2048];
  385. volatile bool ep_tx_busy_flag = false;
  386. static void usbd_event_handler(uint8_t busid, uint8_t event)
  387. {
  388. switch (event) {
  389. case USBD_EVENT_RESET:
  390. break;
  391. case USBD_EVENT_CONNECTED:
  392. break;
  393. case USBD_EVENT_DISCONNECTED:
  394. break;
  395. case USBD_EVENT_RESUME:
  396. break;
  397. case USBD_EVENT_SUSPEND:
  398. break;
  399. case USBD_EVENT_CONFIGURED:
  400. ep_tx_busy_flag = false;
  401. /* setup first out ep read transfer */
  402. usbd_ep_start_read(busid, WINUSB_OUT_EP, read_buffer, 2048);
  403. #if DOUBLE_WINUSB == 1
  404. usbd_ep_start_read(busid, WINUSB_OUT_EP2, read_buffer, 2048);
  405. #endif
  406. break;
  407. case USBD_EVENT_SET_REMOTE_WAKEUP:
  408. break;
  409. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  410. break;
  411. default:
  412. break;
  413. }
  414. }
  415. void usbd_winusb_out(uint8_t busid, uint8_t ep, uint32_t nbytes)
  416. {
  417. USB_LOG_RAW("actual out len:%d\r\n", nbytes);
  418. // for (int i = 0; i < 100; i++) {
  419. // printf("%02x ", read_buffer[i]);
  420. // }
  421. // printf("\r\n");
  422. usbd_ep_start_write(busid, WINUSB_IN_EP, read_buffer, nbytes);
  423. /* setup next out ep read transfer */
  424. usbd_ep_start_read(busid, WINUSB_OUT_EP, read_buffer, 2048);
  425. }
  426. void usbd_winusb_in(uint8_t busid, uint8_t ep, uint32_t nbytes)
  427. {
  428. USB_LOG_RAW("actual in len:%d\r\n", nbytes);
  429. if ((nbytes % WINUSB_EP_MPS) == 0 && nbytes) {
  430. /* send zlp */
  431. usbd_ep_start_write(busid, WINUSB_IN_EP, NULL, 0);
  432. } else {
  433. ep_tx_busy_flag = false;
  434. }
  435. }
  436. struct usbd_endpoint winusb_out_ep1 = {
  437. .ep_addr = WINUSB_OUT_EP,
  438. .ep_cb = usbd_winusb_out
  439. };
  440. struct usbd_endpoint winusb_in_ep1 = {
  441. .ep_addr = WINUSB_IN_EP,
  442. .ep_cb = usbd_winusb_in
  443. };
  444. struct usbd_interface intf0;
  445. #if DOUBLE_WINUSB == 1
  446. void usbd_winusb_out2(uint8_t busid, uint8_t ep, uint32_t nbytes)
  447. {
  448. USB_LOG_RAW("actual out len:%d\r\n", nbytes);
  449. // for (int i = 0; i < 100; i++) {
  450. // printf("%02x ", read_buffer[i]);
  451. // }
  452. // printf("\r\n");
  453. usbd_ep_start_write(busid, WINUSB_IN_EP2, read_buffer, nbytes);
  454. /* setup next out ep read transfer */
  455. usbd_ep_start_read(busid, WINUSB_OUT_EP2, read_buffer, 2048);
  456. }
  457. void usbd_winusb_in2(uint8_t busid, uint8_t ep, uint32_t nbytes)
  458. {
  459. USB_LOG_RAW("actual in len:%d\r\n", nbytes);
  460. if ((nbytes % usbd_get_ep_mps(busid, ep)) == 0 && nbytes) {
  461. /* send zlp */
  462. usbd_ep_start_write(busid, WINUSB_IN_EP2, NULL, 0);
  463. } else {
  464. ep_tx_busy_flag = false;
  465. }
  466. }
  467. struct usbd_endpoint winusb_out_ep2 = {
  468. .ep_addr = WINUSB_OUT_EP2,
  469. .ep_cb = usbd_winusb_out2
  470. };
  471. struct usbd_endpoint winusb_in_ep2 = {
  472. .ep_addr = WINUSB_IN_EP2,
  473. .ep_cb = usbd_winusb_in2
  474. };
  475. struct usbd_interface intf1;
  476. #endif
  477. void winusb_init(uint8_t busid, uintptr_t reg_base)
  478. {
  479. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  480. usbd_desc_register(busid, &winusb_descriptor);
  481. #else
  482. usbd_desc_register(busid, winusb_descriptor);
  483. #endif
  484. #ifndef CONFIG_USBDEV_ADVANCE_DESC
  485. usbd_msosv1_desc_register(busid, &msosv1_desc);
  486. #endif
  487. usbd_add_interface(busid, &intf0);
  488. usbd_add_endpoint(busid, &winusb_out_ep1);
  489. usbd_add_endpoint(busid, &winusb_in_ep1);
  490. #if DOUBLE_WINUSB == 1
  491. usbd_add_interface(busid, &intf1);
  492. usbd_add_endpoint(busid, &winusb_out_ep2);
  493. usbd_add_endpoint(busid, &winusb_in_ep2);
  494. #endif
  495. usbd_initialize(busid, reg_base, usbd_event_handler);
  496. }