usbd_adb_template.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. /*!< global descriptor */
  99. static const uint8_t adb_descriptor[] = {
  100. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0100, 0x01),
  101. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  102. ADB_DESCRIPTOR_INIT(ADB_INTF_NUM, WINUSB_IN_EP, WINUSB_OUT_EP, WINUSB_MAX_MPS),
  103. ///////////////////////////////////////
  104. /// string0 descriptor
  105. ///////////////////////////////////////
  106. USB_LANGID_INIT(USBD_LANGID_STRING),
  107. ///////////////////////////////////////
  108. /// string1 descriptor
  109. ///////////////////////////////////////
  110. 0x14, /* bLength */
  111. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  112. 'C', 0x00, /* wcChar0 */
  113. 'h', 0x00, /* wcChar1 */
  114. 'e', 0x00, /* wcChar2 */
  115. 'r', 0x00, /* wcChar3 */
  116. 'r', 0x00, /* wcChar4 */
  117. 'y', 0x00, /* wcChar5 */
  118. 'U', 0x00, /* wcChar6 */
  119. 'S', 0x00, /* wcChar7 */
  120. 'B', 0x00, /* wcChar8 */
  121. ///////////////////////////////////////
  122. /// string2 descriptor
  123. ///////////////////////////////////////
  124. 0x14, /* bLength */
  125. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  126. 'C', 0x00, /* wcChar0 */
  127. 'h', 0x00, /* wcChar1 */
  128. 'e', 0x00, /* wcChar2 */
  129. 'r', 0x00, /* wcChar3 */
  130. 'r', 0x00, /* wcChar4 */
  131. 'y', 0x00, /* wcChar5 */
  132. 'A', 0x00, /* wcChar6 */
  133. 'D', 0x00, /* wcChar7 */
  134. 'B', 0x00, /* wcChar8 */
  135. ///////////////////////////////////////
  136. /// string3 descriptor
  137. ///////////////////////////////////////
  138. 0x1C, /* bLength */
  139. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  140. 'C', 0x00, /* wcChar0 */
  141. 'h', 0x00, /* wcChar1 */
  142. 'e', 0x00, /* wcChar2 */
  143. 'r', 0x00, /* wcChar3 */
  144. 'r', 0x00, /* wcChar4 */
  145. 'y', 0x00, /* wcChar5 */
  146. 'A', 0x00, /* wcChar6 */
  147. 'D', 0x00, /* wcChar7 */
  148. 'B', 0x00, /* wcChar8 */
  149. '2', 0x00, /* wcChar9 */
  150. '0', 0x00, /* wcChar10 */
  151. '2', 0x00, /* wcChar11 */
  152. '4', 0x00, /* wcChar12 */
  153. #ifdef CONFIG_USB_HS
  154. ///////////////////////////////////////
  155. /// device qualifier descriptor
  156. ///////////////////////////////////////
  157. 0x0a,
  158. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  159. 0x00,
  160. 0x02,
  161. 0x02,
  162. 0x02,
  163. 0x01,
  164. 0x40,
  165. 0x00,
  166. 0x00,
  167. #endif
  168. 0x00
  169. };
  170. static void usbd_event_handler(uint8_t busid, uint8_t event)
  171. {
  172. switch (event) {
  173. case USBD_EVENT_RESET:
  174. break;
  175. case USBD_EVENT_CONNECTED:
  176. break;
  177. case USBD_EVENT_DISCONNECTED:
  178. break;
  179. case USBD_EVENT_RESUME:
  180. break;
  181. case USBD_EVENT_SUSPEND:
  182. break;
  183. case USBD_EVENT_CONFIGURED:
  184. break;
  185. case USBD_EVENT_SET_REMOTE_WAKEUP:
  186. break;
  187. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  188. break;
  189. default:
  190. break;
  191. }
  192. }
  193. static struct usbd_interface intf0;
  194. extern int shell_init(bool need_login);
  195. void cherryadb_init(uint8_t busid, uint32_t reg_base)
  196. {
  197. /* default password is : 12345678 */
  198. /* shell_init() must be called in-task */
  199. if (0 != shell_init(false)) {
  200. /* shell failed to be initialized */
  201. printf("Failed to initialize shell\r\n");
  202. for (;;) {
  203. ;
  204. }
  205. }
  206. usbd_desc_register(busid, adb_descriptor);
  207. usbd_add_interface(busid, usbd_adb_init_intf(busid, &intf0, WINUSB_IN_EP, WINUSB_OUT_EP));
  208. usbd_initialize(busid, reg_base, usbd_event_handler);
  209. }