winusb1.0_template.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. const uint8_t WCID_StringDescriptor_MSOS[18] = {
  10. USB_MSOSV1_STRING_DESCRIPTOR_INIT(WINUSB_VENDOR_CODE)
  11. };
  12. const uint8_t WINUSB_WCIDDescriptor[] = {
  13. USB_MSOSV1_COMP_ID_HEADER_DESCRIPTOR_INIT(WINUSB_NUM),
  14. USB_MSOSV1_COMP_ID_FUNCTION_WINUSB_DESCRIPTOR_INIT(0),
  15. #if WINUSB_NUM == 2
  16. USB_MSOSV1_COMP_ID_FUNCTION_WINUSB_DESCRIPTOR_INIT(1),
  17. #endif
  18. };
  19. const uint8_t WINUSB_IF0_WCIDProperties[142] = {
  20. ///////////////////////////////////////
  21. /// WCID property descriptor
  22. ///////////////////////////////////////
  23. 0x8e, 0x00, 0x00, 0x00, /* dwLength */
  24. 0x00, 0x01, /* bcdVersion */
  25. 0x05, 0x00, /* wIndex */
  26. 0x01, 0x00, /* wCount */
  27. ///////////////////////////////////////
  28. /// registry propter descriptor
  29. ///////////////////////////////////////
  30. 0x84, 0x00, 0x00, 0x00, /* dwSize */
  31. 0x01, 0x00, 0x00, 0x00, /* dwPropertyDataType */
  32. 0x28, 0x00, /* wPropertyNameLength */
  33. /* DeviceInterfaceGUID */
  34. 'D', 0x00, 'e', 0x00, 'v', 0x00, 'i', 0x00, /* wcName_20 */
  35. 'c', 0x00, 'e', 0x00, 'I', 0x00, 'n', 0x00, /* wcName_20 */
  36. 't', 0x00, 'e', 0x00, 'r', 0x00, 'f', 0x00, /* wcName_20 */
  37. 'a', 0x00, 'c', 0x00, 'e', 0x00, 'G', 0x00, /* wcName_20 */
  38. 'U', 0x00, 'I', 0x00, 'D', 0x00, 0x00, 0x00, /* wcName_20 */
  39. 0x4e, 0x00, 0x00, 0x00, /* dwPropertyDataLength */
  40. /* {1D4B2365-4749-48EA-B38A-7C6FDDDD7E26} */
  41. '{', 0x00, '1', 0x00, 'D', 0x00, '4', 0x00, /* wcData_39 */
  42. 'B', 0x00, '2', 0x00, '3', 0x00, '6', 0x00, /* wcData_39 */
  43. '5', 0x00, '-', 0x00, '4', 0x00, '7', 0x00, /* wcData_39 */
  44. '4', 0x00, '9', 0x00, '-', 0x00, '4', 0x00, /* wcData_39 */
  45. '8', 0x00, 'E', 0x00, 'A', 0x00, '-', 0x00, /* wcData_39 */
  46. 'B', 0x00, '3', 0x00, '8', 0x00, 'A', 0x00, /* wcData_39 */
  47. '-', 0x00, '7', 0x00, 'C', 0x00, '6', 0x00, /* wcData_39 */
  48. 'F', 0x00, 'D', 0x00, 'D', 0x00, 'D', 0x00, /* wcData_39 */
  49. 'D', 0x00, '7', 0x00, 'E', 0x00, '2', 0x00, /* wcData_39 */
  50. '6', 0x00, '}', 0x00, 0x00, 0x00, /* wcData_39 */
  51. };
  52. #if WINUSB_NUM == 2
  53. #define WINUSB_IF1_WCID_PROPERTIES_SIZE (142)
  54. const uint8_t WINUSB_IF1_WCIDProperties[142] = {
  55. ///////////////////////////////////////
  56. /// WCID property descriptor
  57. ///////////////////////////////////////
  58. 0x8e, 0x00, 0x00, 0x00, /* dwLength */
  59. 0x00, 0x01, /* bcdVersion */
  60. 0x05, 0x00, /* wIndex */
  61. 0x01, 0x00, /* wCount */
  62. ///////////////////////////////////////
  63. /// registry propter descriptor
  64. ///////////////////////////////////////
  65. 0x84, 0x00, 0x00, 0x00, /* dwSize */
  66. 0x01, 0x00, 0x00, 0x00, /* dwPropertyDataType */
  67. 0x28, 0x00, /* wPropertyNameLength */
  68. /* DeviceInterfaceGUID */
  69. 'D', 0x00, 'e', 0x00, 'v', 0x00, 'i', 0x00, /* wcName_20 */
  70. 'c', 0x00, 'e', 0x00, 'I', 0x00, 'n', 0x00, /* wcName_20 */
  71. 't', 0x00, 'e', 0x00, 'r', 0x00, 'f', 0x00, /* wcName_20 */
  72. 'a', 0x00, 'c', 0x00, 'e', 0x00, 'G', 0x00, /* wcName_20 */
  73. 'U', 0x00, 'I', 0x00, 'D', 0x00, 0x00, 0x00, /* wcName_20 */
  74. 0x4e, 0x00, 0x00, 0x00, /* dwPropertyDataLength */
  75. /* {1D4B2365-4749-48EA-B38A-7C6FDDDD7E26} */
  76. '{', 0x00, '1', 0x00, 'D', 0x00, '4', 0x00, /* wcData_39 */
  77. 'B', 0x00, '2', 0x00, '3', 0x00, '6', 0x00, /* wcData_39 */
  78. '5', 0x00, '-', 0x00, '4', 0x00, '7', 0x00, /* wcData_39 */
  79. '4', 0x00, '9', 0x00, '-', 0x00, '4', 0x00, /* wcData_39 */
  80. '8', 0x00, 'E', 0x00, 'A', 0x00, '-', 0x00, /* wcData_39 */
  81. 'B', 0x00, '3', 0x00, '8', 0x00, 'A', 0x00, /* wcData_39 */
  82. '-', 0x00, '7', 0x00, 'C', 0x00, '6', 0x00, /* wcData_39 */
  83. 'F', 0x00, 'D', 0x00, 'D', 0x00, 'D', 0x00, /* wcData_39 */
  84. 'D', 0x00, '7', 0x00, 'E', 0x00, '2', 0x00, /* wcData_39 */
  85. '6', 0x00, '}', 0x00, 0x00, 0x00, /* wcData_39 */
  86. };
  87. #endif
  88. const uint8_t *WINUSB_IFx_WCIDProperties[] = {
  89. WINUSB_IF0_WCIDProperties,
  90. #if WINUSB_NUM == 2
  91. WINUSB_IF1_WCIDProperties,
  92. #endif
  93. };
  94. struct usb_msosv1_descriptor msosv1_desc = {
  95. .string = WCID_StringDescriptor_MSOS,
  96. .vendor_code = WINUSB_VENDOR_CODE,
  97. .compat_id = WINUSB_WCIDDescriptor,
  98. .comp_id_property = WINUSB_IFx_WCIDProperties,
  99. };
  100. #define WINUSB_IN_EP 0x81
  101. #define WINUSB_OUT_EP 0x02
  102. #define USBD_VID 0xFFFE
  103. #define USBD_PID 0xffff
  104. #define USBD_MAX_POWER 100
  105. #define USBD_LANGID_STRING 1033
  106. #if WINUSB_NUM == 1
  107. #define USB_CONFIG_SIZE (9 + 9 + 7 + 7)
  108. #define INTF_NUM 1
  109. #else
  110. #define WINUSB_IN_EP2 0x83
  111. #define WINUSB_OUT_EP2 0x04
  112. #define USB_CONFIG_SIZE (9 + 9 + 7 + 7 + 9 + 7 + 7)
  113. #define INTF_NUM 2
  114. #endif
  115. #ifdef CONFIG_USB_HS
  116. #define WINUSB_EP_MPS 512
  117. #else
  118. #define WINUSB_EP_MPS 64
  119. #endif
  120. static const uint8_t device_descriptor[] = {
  121. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0001, 0x01)
  122. };
  123. static const uint8_t config_descriptor[] = {
  124. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, INTF_NUM, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  125. USB_INTERFACE_DESCRIPTOR_INIT(0x00, 0x00, 0x02, 0xff, 0xff, 0x00, 0x04),
  126. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_IN_EP, 0x02, WINUSB_EP_MPS, 0x00),
  127. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_OUT_EP, 0x02, WINUSB_EP_MPS, 0x00),
  128. #if WINUSB_NUM == 2
  129. USB_INTERFACE_DESCRIPTOR_INIT(0x01, 0x00, 0x02, 0xff, 0xff, 0x00, 0x05),
  130. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_IN_EP2, 0x02, WINUSB_EP_MPS, 0x00),
  131. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_OUT_EP2, 0x02, WINUSB_EP_MPS, 0x00),
  132. #endif
  133. };
  134. static const uint8_t device_quality_descriptor[] = {
  135. ///////////////////////////////////////
  136. /// device qualifier descriptor
  137. ///////////////////////////////////////
  138. 0x0a,
  139. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  140. 0x00,
  141. 0x02,
  142. 0x00,
  143. 0x00,
  144. 0x00,
  145. 0x40,
  146. 0x00,
  147. 0x00,
  148. };
  149. static const char *string_descriptors[] = {
  150. (const char[]){ 0x09, 0x04 }, /* Langid */
  151. "CherryUSB", /* Manufacturer */
  152. "CherryUSB WINUSB DEMO", /* Product */
  153. "2022123456", /* Serial Number */
  154. "CherryUSB WINUSB DEMO 1", /* STRING4 */
  155. "CherryUSB WINUSB DEMO 2", /* STRING5 */
  156. };
  157. static const uint8_t *device_descriptor_callback(uint8_t speed)
  158. {
  159. return device_descriptor;
  160. }
  161. static const uint8_t *config_descriptor_callback(uint8_t speed)
  162. {
  163. return config_descriptor;
  164. }
  165. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  166. {
  167. return device_quality_descriptor;
  168. }
  169. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  170. {
  171. if (index > 5) {
  172. return NULL;
  173. }
  174. return string_descriptors[index];
  175. }
  176. const struct usb_descriptor winusbv1_descriptor = {
  177. .device_descriptor_callback = device_descriptor_callback,
  178. .config_descriptor_callback = config_descriptor_callback,
  179. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  180. .string_descriptor_callback = string_descriptor_callback,
  181. .msosv1_descriptor = &msosv1_desc
  182. };
  183. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[2048];
  184. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[2048];
  185. volatile bool ep_tx_busy_flag = false;
  186. static void usbd_event_handler(uint8_t busid, uint8_t event)
  187. {
  188. switch (event) {
  189. case USBD_EVENT_RESET:
  190. break;
  191. case USBD_EVENT_CONNECTED:
  192. break;
  193. case USBD_EVENT_DISCONNECTED:
  194. break;
  195. case USBD_EVENT_RESUME:
  196. break;
  197. case USBD_EVENT_SUSPEND:
  198. break;
  199. case USBD_EVENT_CONFIGURED:
  200. ep_tx_busy_flag = false;
  201. /* setup first out ep read transfer */
  202. usbd_ep_start_read(busid, WINUSB_OUT_EP, read_buffer, 2048);
  203. #if WINUSB_NUM == 2
  204. usbd_ep_start_read(busid, WINUSB_OUT_EP2, read_buffer, 2048);
  205. #endif
  206. break;
  207. case USBD_EVENT_SET_REMOTE_WAKEUP:
  208. break;
  209. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  210. break;
  211. default:
  212. break;
  213. }
  214. }
  215. void usbd_winusb_out(uint8_t busid, uint8_t ep, uint32_t nbytes)
  216. {
  217. USB_LOG_RAW("actual out len:%d\r\n", (unsigned int)nbytes);
  218. // for (int i = 0; i < 100; i++) {
  219. // printf("%02x ", read_buffer[i]);
  220. // }
  221. // printf("\r\n");
  222. usbd_ep_start_write(busid, WINUSB_IN_EP, read_buffer, nbytes);
  223. /* setup next out ep read transfer */
  224. usbd_ep_start_read(busid, WINUSB_OUT_EP, read_buffer, 2048);
  225. }
  226. void usbd_winusb_in(uint8_t busid, uint8_t ep, uint32_t nbytes)
  227. {
  228. USB_LOG_RAW("actual in len:%d\r\n", (unsigned int)nbytes);
  229. if ((nbytes % WINUSB_EP_MPS) == 0 && nbytes) {
  230. /* send zlp */
  231. usbd_ep_start_write(busid, WINUSB_IN_EP, NULL, 0);
  232. } else {
  233. ep_tx_busy_flag = false;
  234. }
  235. }
  236. struct usbd_endpoint winusb_out_ep1 = {
  237. .ep_addr = WINUSB_OUT_EP,
  238. .ep_cb = usbd_winusb_out
  239. };
  240. struct usbd_endpoint winusb_in_ep1 = {
  241. .ep_addr = WINUSB_IN_EP,
  242. .ep_cb = usbd_winusb_in
  243. };
  244. struct usbd_interface intf0;
  245. #if WINUSB_NUM == 2
  246. void usbd_winusb_out2(uint8_t busid, uint8_t ep, uint32_t nbytes)
  247. {
  248. USB_LOG_RAW("actual out len:%d\r\n", (unsigned int)nbytes);
  249. // for (int i = 0; i < 100; i++) {
  250. // printf("%02x ", read_buffer[i]);
  251. // }
  252. // printf("\r\n");
  253. usbd_ep_start_write(busid, WINUSB_IN_EP2, read_buffer, nbytes);
  254. /* setup next out ep read transfer */
  255. usbd_ep_start_read(busid, WINUSB_OUT_EP2, read_buffer, 2048);
  256. }
  257. void usbd_winusb_in2(uint8_t busid, uint8_t ep, uint32_t nbytes)
  258. {
  259. USB_LOG_RAW("actual in len:%d\r\n", (unsigned int)nbytes);
  260. if ((nbytes % usbd_get_ep_mps(busid, ep)) == 0 && nbytes) {
  261. /* send zlp */
  262. usbd_ep_start_write(busid, WINUSB_IN_EP2, NULL, 0);
  263. } else {
  264. ep_tx_busy_flag = false;
  265. }
  266. }
  267. struct usbd_endpoint winusb_out_ep2 = {
  268. .ep_addr = WINUSB_OUT_EP2,
  269. .ep_cb = usbd_winusb_out2
  270. };
  271. struct usbd_endpoint winusb_in_ep2 = {
  272. .ep_addr = WINUSB_IN_EP2,
  273. .ep_cb = usbd_winusb_in2
  274. };
  275. struct usbd_interface intf1;
  276. #endif
  277. void winusbv1_init(uint8_t busid, uintptr_t reg_base)
  278. {
  279. usbd_desc_register(busid, &winusbv1_descriptor);
  280. usbd_add_interface(busid, &intf0);
  281. usbd_add_endpoint(busid, &winusb_out_ep1);
  282. usbd_add_endpoint(busid, &winusb_in_ep1);
  283. #if WINUSB_NUM == 2
  284. usbd_add_interface(busid, &intf1);
  285. usbd_add_endpoint(busid, &winusb_out_ep2);
  286. usbd_add_endpoint(busid, &winusb_in_ep2);
  287. #endif
  288. usbd_initialize(busid, reg_base, usbd_event_handler);
  289. }