usbd_adb_template.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usbd_adb.h"
  8. /*!< endpoint address */
  9. #define WINUSB_IN_EP 0x81
  10. #define WINUSB_OUT_EP 0x02
  11. #define USBD_VID 0xFFFF
  12. #define USBD_PID 0xFFFF
  13. #define USBD_MAX_POWER 100
  14. #define USBD_LANGID_STRING 1033
  15. /*!< config descriptor size */
  16. #define USB_CONFIG_SIZE (9 + 9 + 7 + 7)
  17. #ifdef CONFIG_USB_HS
  18. #define WINUSB_MAX_MPS 512
  19. #else
  20. #define WINUSB_MAX_MPS 64
  21. #endif
  22. #define WCID_VENDOR_CODE 0x17
  23. #define ADB_INTF_NUM 0
  24. __ALIGN_BEGIN const uint8_t WCID_StringDescriptor_MSOS[18] __ALIGN_END = {
  25. ///////////////////////////////////////
  26. /// MS OS string descriptor
  27. ///////////////////////////////////////
  28. 0x12, /* bLength */
  29. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  30. /* MSFT100 */
  31. 'M', 0x00, 'S', 0x00, 'F', 0x00, 'T', 0x00, /* wcChar_7 */
  32. '1', 0x00, '0', 0x00, '0', 0x00, /* wcChar_7 */
  33. WCID_VENDOR_CODE, /* bVendorCode */
  34. 0x00, /* bReserved */
  35. };
  36. __ALIGN_BEGIN const uint8_t WINUSB_WCIDDescriptor[40] __ALIGN_END = {
  37. ///////////////////////////////////////
  38. /// WCID descriptor
  39. ///////////////////////////////////////
  40. 0x28, 0x00, 0x00, 0x00, /* dwLength */
  41. 0x00, 0x01, /* bcdVersion */
  42. 0x04, 0x00, /* wIndex */
  43. 0x01, /* bCount */
  44. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* bReserved_7 */
  45. ///////////////////////////////////////
  46. /// WCID function descriptor
  47. ///////////////////////////////////////
  48. ADB_INTF_NUM, /* bFirstInterfaceNumber */
  49. 0x01, /* bReserved */
  50. /* Compatible ID */
  51. 'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00, /* cCID_8: WINUSB */
  52. /* */
  53. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* cSubCID_8 */
  54. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* bReserved_6 */
  55. };
  56. __ALIGN_BEGIN const uint8_t WINUSB_IF0_WCIDProperties[142] __ALIGN_END = {
  57. ///////////////////////////////////////
  58. /// WCID property descriptor
  59. ///////////////////////////////////////
  60. 0x8e, 0x00, 0x00, 0x00, /* dwLength */
  61. 0x00, 0x01, /* bcdVersion */
  62. 0x05, 0x00, /* wIndex */
  63. 0x01, 0x00, /* wCount */
  64. ///////////////////////////////////////
  65. /// registry propter descriptor
  66. ///////////////////////////////////////
  67. 0x84, 0x00, 0x00, 0x00, /* dwSize */
  68. 0x01, 0x00, 0x00, 0x00, /* dwPropertyDataType */
  69. 0x28, 0x00, /* wPropertyNameLength */
  70. /* DeviceInterfaceGUID */
  71. 'D', 0x00, 'e', 0x00, 'v', 0x00, 'i', 0x00, /* wcName_20 */
  72. 'c', 0x00, 'e', 0x00, 'I', 0x00, 'n', 0x00, /* wcName_20 */
  73. 't', 0x00, 'e', 0x00, 'r', 0x00, 'f', 0x00, /* wcName_20 */
  74. 'a', 0x00, 'c', 0x00, 'e', 0x00, 'G', 0x00, /* wcName_20 */
  75. 'U', 0x00, 'I', 0x00, 'D', 0x00, 0x00, 0x00, /* wcName_20 */
  76. 0x4e, 0x00, 0x00, 0x00, /* dwPropertyDataLength */
  77. /* {1D4B2365-4749-48EA-B38A-7C6FDDDD7E26} */
  78. '{', 0x00, '1', 0x00, 'D', 0x00, '4', 0x00, /* wcData_39 */
  79. 'B', 0x00, '2', 0x00, '3', 0x00, '6', 0x00, /* wcData_39 */
  80. '5', 0x00, '-', 0x00, '4', 0x00, '7', 0x00, /* wcData_39 */
  81. '4', 0x00, '9', 0x00, '-', 0x00, '4', 0x00, /* wcData_39 */
  82. '8', 0x00, 'E', 0x00, 'A', 0x00, '-', 0x00, /* wcData_39 */
  83. 'B', 0x00, '3', 0x00, '8', 0x00, 'A', 0x00, /* wcData_39 */
  84. '-', 0x00, '7', 0x00, 'C', 0x00, '6', 0x00, /* wcData_39 */
  85. 'F', 0x00, 'D', 0x00, 'D', 0x00, 'D', 0x00, /* wcData_39 */
  86. 'D', 0x00, '7', 0x00, 'E', 0x00, '2', 0x00, /* wcData_39 */
  87. '6', 0x00, '}', 0x00, 0x00, 0x00, /* wcData_39 */
  88. };
  89. const uint8_t *WINUSB_IFx_WCIDProperties[] = {
  90. WINUSB_IF0_WCIDProperties,
  91. };
  92. struct usb_msosv1_descriptor msosv1_desc = {
  93. .string = WCID_StringDescriptor_MSOS,
  94. .vendor_code = WCID_VENDOR_CODE,
  95. .compat_id = WINUSB_WCIDDescriptor,
  96. .comp_id_property = WINUSB_IFx_WCIDProperties,
  97. };
  98. static const uint8_t device_descriptor[] = {
  99. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0100, 0x01)
  100. };
  101. static const uint8_t config_descriptor[] = {
  102. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  103. ADB_DESCRIPTOR_INIT(ADB_INTF_NUM, WINUSB_IN_EP, WINUSB_OUT_EP, WINUSB_MAX_MPS)
  104. };
  105. static const uint8_t device_quality_descriptor[] = {
  106. ///////////////////////////////////////
  107. /// device qualifier descriptor
  108. ///////////////////////////////////////
  109. 0x0a,
  110. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  111. 0x00,
  112. 0x02,
  113. 0x00,
  114. 0x00,
  115. 0x00,
  116. 0x40,
  117. 0x00,
  118. 0x00,
  119. };
  120. static const char *string_descriptors[] = {
  121. (const char[]){ 0x09, 0x04 }, /* Langid */
  122. "CherryUSB", /* Manufacturer */
  123. "CherryADB", /* Product */
  124. "CherryADB2024", /* Serial Number */
  125. };
  126. static const uint8_t *device_descriptor_callback(uint8_t speed)
  127. {
  128. return device_descriptor;
  129. }
  130. static const uint8_t *config_descriptor_callback(uint8_t speed)
  131. {
  132. return config_descriptor;
  133. }
  134. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  135. {
  136. return device_quality_descriptor;
  137. }
  138. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  139. {
  140. if (index > 3) {
  141. return NULL;
  142. }
  143. return string_descriptors[index];
  144. }
  145. const struct usb_descriptor adb_descriptor = {
  146. .device_descriptor_callback = device_descriptor_callback,
  147. .config_descriptor_callback = config_descriptor_callback,
  148. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  149. .string_descriptor_callback = string_descriptor_callback,
  150. .msosv1_descriptor = &msosv1_desc
  151. };
  152. static void usbd_event_handler(uint8_t busid, uint8_t event)
  153. {
  154. switch (event) {
  155. case USBD_EVENT_RESET:
  156. break;
  157. case USBD_EVENT_CONNECTED:
  158. break;
  159. case USBD_EVENT_DISCONNECTED:
  160. break;
  161. case USBD_EVENT_RESUME:
  162. break;
  163. case USBD_EVENT_SUSPEND:
  164. break;
  165. case USBD_EVENT_CONFIGURED:
  166. break;
  167. case USBD_EVENT_SET_REMOTE_WAKEUP:
  168. break;
  169. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  170. break;
  171. default:
  172. break;
  173. }
  174. }
  175. static struct usbd_interface intf0;
  176. #ifdef RT_USING_MSH
  177. extern void usbd_adb_shell_init(uint8_t in_ep, uint8_t out_ep);
  178. #else
  179. extern int shell_init(bool need_login);
  180. #endif
  181. void cherryadb_init(uint8_t busid, uint32_t reg_base)
  182. {
  183. #ifdef RT_USING_MSH
  184. usbd_adb_shell_init(WINUSB_IN_EP, WINUSB_OUT_EP);
  185. #else
  186. /* default password is : 12345678 */
  187. /* shell_init() must be called in-task */
  188. if (0 != shell_init(false)) {
  189. /* shell failed to be initialized */
  190. USB_LOG_RAW("Failed to initialize shell\r\n");
  191. for (;;) {
  192. ;
  193. }
  194. }
  195. #endif
  196. usbd_desc_register(busid, &adb_descriptor);
  197. usbd_add_interface(busid, usbd_adb_init_intf(busid, &intf0, WINUSB_IN_EP, WINUSB_OUT_EP));
  198. usbd_initialize(busid, reg_base, usbd_event_handler);
  199. }