usbd_adb_template.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  99. static const uint8_t device_descriptor[] = {
  100. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0100, 0x01)
  101. };
  102. static const uint8_t config_descriptor[] = {
  103. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  104. ADB_DESCRIPTOR_INIT(ADB_INTF_NUM, WINUSB_IN_EP, WINUSB_OUT_EP, WINUSB_MAX_MPS)
  105. };
  106. static const uint8_t device_quality_descriptor[] = {
  107. ///////////////////////////////////////
  108. /// device qualifier descriptor
  109. ///////////////////////////////////////
  110. 0x0a,
  111. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  112. 0x00,
  113. 0x02,
  114. 0x00,
  115. 0x00,
  116. 0x00,
  117. 0x40,
  118. 0x00,
  119. 0x00,
  120. };
  121. static const char *string_descriptors[] = {
  122. (const char[]){ 0x09, 0x04 }, /* Langid */
  123. "CherryUSB", /* Manufacturer */
  124. "CherryADB", /* Product */
  125. "CherryADB2024", /* Serial Number */
  126. };
  127. static const uint8_t *device_descriptor_callback(uint8_t speed)
  128. {
  129. return device_descriptor;
  130. }
  131. static const uint8_t *config_descriptor_callback(uint8_t speed)
  132. {
  133. return config_descriptor;
  134. }
  135. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  136. {
  137. return device_quality_descriptor;
  138. }
  139. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  140. {
  141. if (index > 3) {
  142. return NULL;
  143. }
  144. return string_descriptors[index];
  145. }
  146. const struct usb_descriptor msc_bootuf2_descriptor = {
  147. .device_descriptor_callback = device_descriptor_callback,
  148. .config_descriptor_callback = config_descriptor_callback,
  149. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  150. .string_descriptor_callback = string_descriptor_callback,
  151. .msosv1_descriptor = &msosv1_desc
  152. };
  153. #else
  154. /*!< global descriptor */
  155. static const uint8_t adb_descriptor[] = {
  156. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0100, 0x01),
  157. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  158. ADB_DESCRIPTOR_INIT(ADB_INTF_NUM, WINUSB_IN_EP, WINUSB_OUT_EP, WINUSB_MAX_MPS),
  159. ///////////////////////////////////////
  160. /// string0 descriptor
  161. ///////////////////////////////////////
  162. USB_LANGID_INIT(USBD_LANGID_STRING),
  163. ///////////////////////////////////////
  164. /// string1 descriptor
  165. ///////////////////////////////////////
  166. 0x14, /* bLength */
  167. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  168. 'C', 0x00, /* wcChar0 */
  169. 'h', 0x00, /* wcChar1 */
  170. 'e', 0x00, /* wcChar2 */
  171. 'r', 0x00, /* wcChar3 */
  172. 'r', 0x00, /* wcChar4 */
  173. 'y', 0x00, /* wcChar5 */
  174. 'U', 0x00, /* wcChar6 */
  175. 'S', 0x00, /* wcChar7 */
  176. 'B', 0x00, /* wcChar8 */
  177. ///////////////////////////////////////
  178. /// string2 descriptor
  179. ///////////////////////////////////////
  180. 0x14, /* bLength */
  181. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  182. 'C', 0x00, /* wcChar0 */
  183. 'h', 0x00, /* wcChar1 */
  184. 'e', 0x00, /* wcChar2 */
  185. 'r', 0x00, /* wcChar3 */
  186. 'r', 0x00, /* wcChar4 */
  187. 'y', 0x00, /* wcChar5 */
  188. 'A', 0x00, /* wcChar6 */
  189. 'D', 0x00, /* wcChar7 */
  190. 'B', 0x00, /* wcChar8 */
  191. ///////////////////////////////////////
  192. /// string3 descriptor
  193. ///////////////////////////////////////
  194. 0x1C, /* bLength */
  195. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  196. 'C', 0x00, /* wcChar0 */
  197. 'h', 0x00, /* wcChar1 */
  198. 'e', 0x00, /* wcChar2 */
  199. 'r', 0x00, /* wcChar3 */
  200. 'r', 0x00, /* wcChar4 */
  201. 'y', 0x00, /* wcChar5 */
  202. 'A', 0x00, /* wcChar6 */
  203. 'D', 0x00, /* wcChar7 */
  204. 'B', 0x00, /* wcChar8 */
  205. '2', 0x00, /* wcChar9 */
  206. '0', 0x00, /* wcChar10 */
  207. '2', 0x00, /* wcChar11 */
  208. '4', 0x00, /* wcChar12 */
  209. #ifdef CONFIG_USB_HS
  210. ///////////////////////////////////////
  211. /// device qualifier descriptor
  212. ///////////////////////////////////////
  213. 0x0a,
  214. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  215. 0x00,
  216. 0x02,
  217. 0x00,
  218. 0x00,
  219. 0x00,
  220. 0x40,
  221. 0x00,
  222. 0x00,
  223. #endif
  224. 0x00
  225. };
  226. #endif
  227. static void usbd_event_handler(uint8_t busid, uint8_t event)
  228. {
  229. switch (event) {
  230. case USBD_EVENT_RESET:
  231. break;
  232. case USBD_EVENT_CONNECTED:
  233. break;
  234. case USBD_EVENT_DISCONNECTED:
  235. break;
  236. case USBD_EVENT_RESUME:
  237. break;
  238. case USBD_EVENT_SUSPEND:
  239. break;
  240. case USBD_EVENT_CONFIGURED:
  241. break;
  242. case USBD_EVENT_SET_REMOTE_WAKEUP:
  243. break;
  244. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  245. break;
  246. default:
  247. break;
  248. }
  249. }
  250. static struct usbd_interface intf0;
  251. extern int shell_init(bool need_login);
  252. void cherryadb_init(uint8_t busid, uint32_t reg_base)
  253. {
  254. /* default password is : 12345678 */
  255. /* shell_init() must be called in-task */
  256. if (0 != shell_init(false)) {
  257. /* shell failed to be initialized */
  258. printf("Failed to initialize shell\r\n");
  259. for (;;) {
  260. ;
  261. }
  262. }
  263. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  264. usbd_desc_register(busid, &adb_descriptor);
  265. #else
  266. usbd_desc_register(busid, adb_descriptor);
  267. #endif
  268. #ifndef CONFIG_USBDEV_ADVANCE_DESC
  269. usbd_msosv1_desc_register(busid, &msosv1_desc);
  270. #endif
  271. usbd_add_interface(busid, usbd_adb_init_intf(busid, &intf0, WINUSB_IN_EP, WINUSB_OUT_EP));
  272. usbd_initialize(busid, reg_base, usbd_event_handler);
  273. }