winusb2.0_template.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. static const uint8_t device_descriptor[] = {
  54. USB_DEVICE_DESCRIPTOR_INIT(USB_2_1, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0001, 0x01)
  55. };
  56. static const uint8_t config_descriptor[] = {
  57. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, INTF_NUM, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  58. USB_INTERFACE_DESCRIPTOR_INIT(0x00, 0x00, 0x02, 0xff, 0xff, 0x00, 0x04),
  59. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_IN_EP, 0x02, WINUSB_EP_MPS, 0x00),
  60. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_OUT_EP, 0x02, WINUSB_EP_MPS, 0x00),
  61. #if WINUSB_NUM == 2
  62. USB_INTERFACE_DESCRIPTOR_INIT(0x01, 0x00, 0x02, 0xff, 0xff, 0x00, 0x05),
  63. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_IN_EP2, 0x02, WINUSB_EP_MPS, 0x00),
  64. USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_OUT_EP2, 0x02, WINUSB_EP_MPS, 0x00),
  65. #endif
  66. };
  67. static const uint8_t device_quality_descriptor[] = {
  68. ///////////////////////////////////////
  69. /// device qualifier descriptor
  70. ///////////////////////////////////////
  71. 0x0a,
  72. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  73. 0x00,
  74. 0x02,
  75. 0x00,
  76. 0x00,
  77. 0x00,
  78. 0x40,
  79. 0x00,
  80. 0x00,
  81. };
  82. static const char *string_descriptors[] = {
  83. (const char[]){ 0x09, 0x04 }, /* Langid */
  84. "CherryUSB", /* Manufacturer */
  85. "CherryUSB WINUSB DEMO", /* Product */
  86. "2022123456", /* Serial Number */
  87. "CherryUSB WINUSB DEMO 1", /* STRING4 */
  88. "CherryUSB WINUSB DEMO 2", /* STRING5 */
  89. };
  90. static const uint8_t *device_descriptor_callback(uint8_t speed)
  91. {
  92. return device_descriptor;
  93. }
  94. static const uint8_t *config_descriptor_callback(uint8_t speed)
  95. {
  96. return config_descriptor;
  97. }
  98. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  99. {
  100. return device_quality_descriptor;
  101. }
  102. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  103. {
  104. if (index > 5) {
  105. return NULL;
  106. }
  107. return string_descriptors[index];
  108. }
  109. const struct usb_descriptor winusbv2_descriptor = {
  110. .device_descriptor_callback = device_descriptor_callback,
  111. .config_descriptor_callback = config_descriptor_callback,
  112. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  113. .string_descriptor_callback = string_descriptor_callback,
  114. .msosv2_descriptor = &msosv2_desc,
  115. .bos_descriptor = &bos_desc,
  116. };
  117. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[2048];
  118. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[2048];
  119. volatile bool ep_tx_busy_flag = false;
  120. static void usbd_event_handler(uint8_t busid, uint8_t event)
  121. {
  122. switch (event) {
  123. case USBD_EVENT_RESET:
  124. break;
  125. case USBD_EVENT_CONNECTED:
  126. break;
  127. case USBD_EVENT_DISCONNECTED:
  128. break;
  129. case USBD_EVENT_RESUME:
  130. break;
  131. case USBD_EVENT_SUSPEND:
  132. break;
  133. case USBD_EVENT_CONFIGURED:
  134. ep_tx_busy_flag = false;
  135. /* setup first out ep read transfer */
  136. usbd_ep_start_read(busid, WINUSB_OUT_EP, read_buffer, 2048);
  137. #if WINUSB_NUM == 2
  138. usbd_ep_start_read(busid, WINUSB_OUT_EP2, read_buffer, 2048);
  139. #endif
  140. break;
  141. case USBD_EVENT_SET_REMOTE_WAKEUP:
  142. break;
  143. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. void usbd_winusb_out(uint8_t busid, uint8_t ep, uint32_t nbytes)
  150. {
  151. USB_LOG_RAW("actual out len:%d\r\n", (unsigned int)nbytes);
  152. // for (int i = 0; i < 100; i++) {
  153. // printf("%02x ", read_buffer[i]);
  154. // }
  155. // printf("\r\n");
  156. usbd_ep_start_write(busid, WINUSB_IN_EP, read_buffer, nbytes);
  157. /* setup next out ep read transfer */
  158. usbd_ep_start_read(busid, WINUSB_OUT_EP, read_buffer, 2048);
  159. }
  160. void usbd_winusb_in(uint8_t busid, uint8_t ep, uint32_t nbytes)
  161. {
  162. USB_LOG_RAW("actual in len:%d\r\n", (unsigned int)nbytes);
  163. if ((nbytes % WINUSB_EP_MPS) == 0 && nbytes) {
  164. /* send zlp */
  165. usbd_ep_start_write(busid, WINUSB_IN_EP, NULL, 0);
  166. } else {
  167. ep_tx_busy_flag = false;
  168. }
  169. }
  170. struct usbd_endpoint winusb_out_ep1 = {
  171. .ep_addr = WINUSB_OUT_EP,
  172. .ep_cb = usbd_winusb_out
  173. };
  174. struct usbd_endpoint winusb_in_ep1 = {
  175. .ep_addr = WINUSB_IN_EP,
  176. .ep_cb = usbd_winusb_in
  177. };
  178. struct usbd_interface intf0;
  179. #if WINUSB_NUM == 2
  180. void usbd_winusb_out2(uint8_t busid, uint8_t ep, uint32_t nbytes)
  181. {
  182. USB_LOG_RAW("actual out len:%d\r\n", (unsigned int)nbytes);
  183. // for (int i = 0; i < 100; i++) {
  184. // printf("%02x ", read_buffer[i]);
  185. // }
  186. // printf("\r\n");
  187. usbd_ep_start_write(busid, WINUSB_IN_EP2, read_buffer, nbytes);
  188. /* setup next out ep read transfer */
  189. usbd_ep_start_read(busid, WINUSB_OUT_EP2, read_buffer, 2048);
  190. }
  191. void usbd_winusb_in2(uint8_t busid, uint8_t ep, uint32_t nbytes)
  192. {
  193. USB_LOG_RAW("actual in len:%d\r\n", (unsigned int)nbytes);
  194. if ((nbytes % usbd_get_ep_mps(busid, ep)) == 0 && nbytes) {
  195. /* send zlp */
  196. usbd_ep_start_write(busid, WINUSB_IN_EP2, NULL, 0);
  197. } else {
  198. ep_tx_busy_flag = false;
  199. }
  200. }
  201. struct usbd_endpoint winusb_out_ep2 = {
  202. .ep_addr = WINUSB_OUT_EP2,
  203. .ep_cb = usbd_winusb_out2
  204. };
  205. struct usbd_endpoint winusb_in_ep2 = {
  206. .ep_addr = WINUSB_IN_EP2,
  207. .ep_cb = usbd_winusb_in2
  208. };
  209. struct usbd_interface intf1;
  210. #endif
  211. void winusbv2_init(uint8_t busid, uintptr_t reg_base)
  212. {
  213. usbd_desc_register(busid, &winusbv2_descriptor);
  214. usbd_add_interface(busid, &intf0);
  215. usbd_add_endpoint(busid, &winusb_out_ep1);
  216. usbd_add_endpoint(busid, &winusb_in_ep1);
  217. #if WINUSB_NUM == 2
  218. usbd_add_interface(busid, &intf1);
  219. usbd_add_endpoint(busid, &winusb_out_ep2);
  220. usbd_add_endpoint(busid, &winusb_in_ep2);
  221. #endif
  222. usbd_initialize(busid, reg_base, usbd_event_handler);
  223. }