cdc_ecm_template.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usbd_cdc_ecm.h"
  8. /*!< endpoint address */
  9. #define CDC_IN_EP 0x81
  10. #define CDC_OUT_EP 0x02
  11. #define CDC_INT_EP 0x83
  12. #define USBD_VID 0xFFFF
  13. #define USBD_PID 0xFFFF
  14. #define USBD_MAX_POWER 100
  15. #define USBD_LANGID_STRING 1033
  16. /*!< config descriptor size */
  17. #define USB_CONFIG_SIZE (9 + CDC_ECM_DESCRIPTOR_LEN)
  18. #ifdef CONFIG_USB_HS
  19. #define CDC_MAX_MPS 512
  20. #else
  21. #define CDC_MAX_MPS 64
  22. #endif
  23. #define CDC_ECM_ETH_STATISTICS_BITMAP 0x00000000
  24. /* str idx = 4 is for mac address: aa:bb:cc:dd:ee:ff*/
  25. #define CDC_ECM_MAC_STRING_INDEX 4
  26. /*!< global descriptor */
  27. static const uint8_t cdc_ecm_descriptor[] = {
  28. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01),
  29. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  30. CDC_ECM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, CDC_ECM_ETH_STATISTICS_BITMAP, CONFIG_CDC_ECM_ETH_MAX_SEGSZE, 0, 0, CDC_ECM_MAC_STRING_INDEX),
  31. ///////////////////////////////////////
  32. /// string0 descriptor
  33. ///////////////////////////////////////
  34. USB_LANGID_INIT(USBD_LANGID_STRING),
  35. ///////////////////////////////////////
  36. /// string1 descriptor
  37. ///////////////////////////////////////
  38. 0x14, /* bLength */
  39. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  40. 'C', 0x00, /* wcChar0 */
  41. 'h', 0x00, /* wcChar1 */
  42. 'e', 0x00, /* wcChar2 */
  43. 'r', 0x00, /* wcChar3 */
  44. 'r', 0x00, /* wcChar4 */
  45. 'y', 0x00, /* wcChar5 */
  46. 'U', 0x00, /* wcChar6 */
  47. 'S', 0x00, /* wcChar7 */
  48. 'B', 0x00, /* wcChar8 */
  49. ///////////////////////////////////////
  50. /// string2 descriptor
  51. ///////////////////////////////////////
  52. 0x2E, /* bLength */
  53. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  54. 'C', 0x00, /* wcChar0 */
  55. 'h', 0x00, /* wcChar1 */
  56. 'e', 0x00, /* wcChar2 */
  57. 'r', 0x00, /* wcChar3 */
  58. 'r', 0x00, /* wcChar4 */
  59. 'y', 0x00, /* wcChar5 */
  60. 'U', 0x00, /* wcChar6 */
  61. 'S', 0x00, /* wcChar7 */
  62. 'B', 0x00, /* wcChar8 */
  63. ' ', 0x00, /* wcChar9 */
  64. 'C', 0x00, /* wcChar10 */
  65. 'D', 0x00, /* wcChar11 */
  66. 'C', 0x00, /* wcChar12 */
  67. ' ', 0x00, /* wcChar13 */
  68. 'E', 0x00, /* wcChar14 */
  69. 'C', 0x00, /* wcChar15 */
  70. 'M', 0x00, /* wcChar16 */
  71. ' ', 0x00, /* wcChar17 */
  72. 'D', 0x00, /* wcChar18 */
  73. 'E', 0x00, /* wcChar19 */
  74. 'M', 0x00, /* wcChar20 */
  75. 'O', 0x00, /* wcChar21 */
  76. ///////////////////////////////////////
  77. /// string3 descriptor
  78. ///////////////////////////////////////
  79. 0x16, /* bLength */
  80. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  81. '2', 0x00, /* wcChar0 */
  82. '0', 0x00, /* wcChar1 */
  83. '2', 0x00, /* wcChar2 */
  84. '2', 0x00, /* wcChar3 */
  85. '1', 0x00, /* wcChar4 */
  86. '2', 0x00, /* wcChar5 */
  87. '3', 0x00, /* wcChar6 */
  88. '4', 0x00, /* wcChar7 */
  89. '5', 0x00, /* wcChar8 */
  90. '6', 0x00, /* wcChar9 */
  91. ///////////////////////////////////////
  92. /// string4 descriptor
  93. ///////////////////////////////////////
  94. 0x1A, /* bLength */
  95. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  96. 'a', 0x00, /* wcChar0 */
  97. 'a', 0x00, /* wcChar1 */
  98. 'b', 0x00, /* wcChar2 */
  99. 'b', 0x00, /* wcChar3 */
  100. 'c', 0x00, /* wcChar4 */
  101. 'c', 0x00, /* wcChar5 */
  102. 'd', 0x00, /* wcChar6 */
  103. 'd', 0x00, /* wcChar7 */
  104. 'e', 0x00, /* wcChar8 */
  105. 'e', 0x00, /* wcChar9 */
  106. 'f', 0x00, /* wcChar10 */
  107. 'f', 0x00, /* wcChar11 */
  108. #ifdef CONFIG_USB_HS
  109. ///////////////////////////////////////
  110. /// device qualifier descriptor
  111. ///////////////////////////////////////
  112. 0x0a,
  113. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  114. 0x00,
  115. 0x02,
  116. 0x02,
  117. 0x02,
  118. 0x01,
  119. 0x40,
  120. 0x01,
  121. 0x00,
  122. #endif
  123. 0x00
  124. };
  125. const uint8_t mac[6] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
  126. /*Static IP ADDRESS: IP_ADDR0.IP_ADDR1.IP_ADDR2.IP_ADDR3 */
  127. #define IP_ADDR0 (uint8_t)192
  128. #define IP_ADDR1 (uint8_t)168
  129. #define IP_ADDR2 (uint8_t)123
  130. #define IP_ADDR3 (uint8_t)100
  131. /*NETMASK*/
  132. #define NETMASK_ADDR0 (uint8_t)255
  133. #define NETMASK_ADDR1 (uint8_t)255
  134. #define NETMASK_ADDR2 (uint8_t)255
  135. #define NETMASK_ADDR3 (uint8_t)0
  136. /*Gateway Address*/
  137. #define GW_ADDR0 (uint8_t)192
  138. #define GW_ADDR1 (uint8_t)168
  139. #define GW_ADDR2 (uint8_t)123
  140. #define GW_ADDR3 (uint8_t)1
  141. #include "netif/etharp.h"
  142. #include "lwip/init.h"
  143. #include "lwip/netif.h"
  144. #include "lwip/pbuf.h"
  145. const ip_addr_t ipaddr = IPADDR4_INIT_BYTES(IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  146. const ip_addr_t netmask = IPADDR4_INIT_BYTES(NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  147. const ip_addr_t gateway = IPADDR4_INIT_BYTES(GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  148. static struct netif cdc_ecm_netif; //network interface
  149. /* Network interface name */
  150. #define IFNAME0 'E'
  151. #define IFNAME1 'X'
  152. static err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
  153. {
  154. static int ret;
  155. ret = usbd_cdc_ecm_eth_tx(p);
  156. if (ret == 0)
  157. return ERR_OK;
  158. else
  159. return ERR_BUF;
  160. }
  161. err_t cdc_ecm_if_init(struct netif *netif)
  162. {
  163. LWIP_ASSERT("netif != NULL", (netif != NULL));
  164. netif->mtu = 1500;
  165. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
  166. netif->state = NULL;
  167. netif->name[0] = IFNAME0;
  168. netif->name[1] = IFNAME1;
  169. netif->output = etharp_output;
  170. netif->linkoutput = linkoutput_fn;
  171. return ERR_OK;
  172. }
  173. err_t cdc_ecm_if_input(struct netif *netif)
  174. {
  175. static err_t err;
  176. static struct pbuf *p;
  177. p = usbd_cdc_ecm_eth_rx();
  178. if (p != NULL) {
  179. err = netif->input(p, netif);
  180. if (err != ERR_OK) {
  181. pbuf_free(p);
  182. }
  183. } else {
  184. return ERR_BUF;
  185. }
  186. return err;
  187. }
  188. void cdc_ecm_lwip_init(void)
  189. {
  190. struct netif *netif = &cdc_ecm_netif;
  191. lwip_init();
  192. netif->hwaddr_len = 6;
  193. memcpy(netif->hwaddr, mac, 6);
  194. netif->hwaddr[5] = ~netif->hwaddr[5]; /* device mac can't same as host. */
  195. netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, cdc_ecm_if_init, netif_input);
  196. netif_set_default(netif);
  197. while (!netif_is_up(netif)) {
  198. }
  199. // while (dhserv_init(&dhcp_config)) {}
  200. // while (dnserv_init(&ipaddr, PORT_DNS, dns_query_proc)) {}
  201. }
  202. void usbd_cdc_ecm_data_recv_done(uint8_t *buf, uint32_t len)
  203. {
  204. }
  205. void cdc_ecm_input_poll(void)
  206. {
  207. cdc_ecm_if_input(&cdc_ecm_netif);
  208. }
  209. static void usbd_event_handler(uint8_t busid, uint8_t event)
  210. {
  211. switch (event) {
  212. case USBD_EVENT_RESET:
  213. break;
  214. case USBD_EVENT_CONNECTED:
  215. break;
  216. case USBD_EVENT_DISCONNECTED:
  217. break;
  218. case USBD_EVENT_RESUME:
  219. break;
  220. case USBD_EVENT_SUSPEND:
  221. break;
  222. case USBD_EVENT_CONFIGURED:
  223. break;
  224. case USBD_EVENT_SET_REMOTE_WAKEUP:
  225. break;
  226. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  227. break;
  228. default:
  229. break;
  230. }
  231. }
  232. struct usbd_interface intf0;
  233. struct usbd_interface intf1;
  234. /* ecm only supports in linux, and you should input the following command
  235. *
  236. * sudo ifconfig enxaabbccddeeff up
  237. * sudo dhcpclient enxaabbccddeeff
  238. */
  239. void cdc_ecm_init(uint8_t busid, uintptr_t reg_base)
  240. {
  241. cdc_ecm_lwip_init();
  242. usbd_desc_register(busid, cdc_ecm_descriptor);
  243. usbd_add_interface(busid, usbd_cdc_ecm_init_intf(&intf0, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP));
  244. usbd_add_interface(busid, usbd_cdc_ecm_init_intf(&intf1, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP));
  245. usbd_initialize(busid, reg_base, usbd_event_handler);
  246. }