cdc_ecm_template.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. #ifndef CONFIG_USBDEV_CDC_ECM_USING_LWIP
  9. #error "Please enable CONFIG_USBDEV_CDC_ECM_USING_LWIP for this demo"
  10. #endif
  11. /*!< endpoint address */
  12. #define CDC_IN_EP 0x81
  13. #define CDC_OUT_EP 0x02
  14. #define CDC_INT_EP 0x83
  15. #define USBD_VID 0xFFFF
  16. #define USBD_PID 0xFFFF
  17. #define USBD_MAX_POWER 100
  18. #define USBD_LANGID_STRING 1033
  19. /*!< config descriptor size */
  20. #define USB_CONFIG_SIZE (9 + CDC_ECM_DESCRIPTOR_LEN)
  21. #ifdef CONFIG_USB_HS
  22. #define CDC_MAX_MPS 512
  23. #else
  24. #define CDC_MAX_MPS 64
  25. #endif
  26. /* str idx = 4 is for mac address: aa:bb:cc:dd:ee:ff*/
  27. #define CDC_ECM_MAC_STRING_INDEX 4
  28. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  29. static const uint8_t device_descriptor[] = {
  30. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01)
  31. };
  32. static const uint8_t config_descriptor[] = {
  33. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  34. CDC_ECM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, CDC_ECM_MAC_STRING_INDEX)
  35. };
  36. static const uint8_t device_quality_descriptor[] = {
  37. ///////////////////////////////////////
  38. /// device qualifier descriptor
  39. ///////////////////////////////////////
  40. 0x0a,
  41. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  42. 0x00,
  43. 0x02,
  44. 0x00,
  45. 0x00,
  46. 0x00,
  47. 0x40,
  48. 0x00,
  49. 0x00,
  50. };
  51. static const char *string_descriptors[] = {
  52. (const char[]){ 0x09, 0x04 }, /* Langid */
  53. "CherryUSB", /* Manufacturer */
  54. "CherryUSB CDC ECM DEMO", /* Product */
  55. "2022123456", /* Serial Number */
  56. "aabbccddeeff", /* ecm mac address */
  57. };
  58. static const uint8_t *device_descriptor_callback(uint8_t speed)
  59. {
  60. return device_descriptor;
  61. }
  62. static const uint8_t *config_descriptor_callback(uint8_t speed)
  63. {
  64. return config_descriptor;
  65. }
  66. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  67. {
  68. return device_quality_descriptor;
  69. }
  70. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  71. {
  72. if (index > 3) {
  73. return NULL;
  74. }
  75. return string_descriptors[index];
  76. }
  77. const struct usb_descriptor cdc_ecm_descriptor = {
  78. .device_descriptor_callback = device_descriptor_callback,
  79. .config_descriptor_callback = config_descriptor_callback,
  80. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  81. .string_descriptor_callback = string_descriptor_callback
  82. };
  83. #else
  84. /*!< global descriptor */
  85. static const uint8_t cdc_ecm_descriptor[] = {
  86. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01),
  87. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  88. CDC_ECM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, CDC_ECM_MAC_STRING_INDEX),
  89. ///////////////////////////////////////
  90. /// string0 descriptor
  91. ///////////////////////////////////////
  92. USB_LANGID_INIT(USBD_LANGID_STRING),
  93. ///////////////////////////////////////
  94. /// string1 descriptor
  95. ///////////////////////////////////////
  96. 0x14, /* bLength */
  97. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  98. 'C', 0x00, /* wcChar0 */
  99. 'h', 0x00, /* wcChar1 */
  100. 'e', 0x00, /* wcChar2 */
  101. 'r', 0x00, /* wcChar3 */
  102. 'r', 0x00, /* wcChar4 */
  103. 'y', 0x00, /* wcChar5 */
  104. 'U', 0x00, /* wcChar6 */
  105. 'S', 0x00, /* wcChar7 */
  106. 'B', 0x00, /* wcChar8 */
  107. ///////////////////////////////////////
  108. /// string2 descriptor
  109. ///////////////////////////////////////
  110. 0x2E, /* 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. ' ', 0x00, /* wcChar9 */
  122. 'C', 0x00, /* wcChar10 */
  123. 'D', 0x00, /* wcChar11 */
  124. 'C', 0x00, /* wcChar12 */
  125. ' ', 0x00, /* wcChar13 */
  126. 'E', 0x00, /* wcChar14 */
  127. 'C', 0x00, /* wcChar15 */
  128. 'M', 0x00, /* wcChar16 */
  129. ' ', 0x00, /* wcChar17 */
  130. 'D', 0x00, /* wcChar18 */
  131. 'E', 0x00, /* wcChar19 */
  132. 'M', 0x00, /* wcChar20 */
  133. 'O', 0x00, /* wcChar21 */
  134. ///////////////////////////////////////
  135. /// string3 descriptor
  136. ///////////////////////////////////////
  137. 0x16, /* bLength */
  138. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  139. '2', 0x00, /* wcChar0 */
  140. '0', 0x00, /* wcChar1 */
  141. '2', 0x00, /* wcChar2 */
  142. '2', 0x00, /* wcChar3 */
  143. '1', 0x00, /* wcChar4 */
  144. '2', 0x00, /* wcChar5 */
  145. '3', 0x00, /* wcChar6 */
  146. '4', 0x00, /* wcChar7 */
  147. '5', 0x00, /* wcChar8 */
  148. '6', 0x00, /* wcChar9 */
  149. ///////////////////////////////////////
  150. /// string4 descriptor
  151. ///////////////////////////////////////
  152. 0x1A, /* bLength */
  153. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  154. 'a', 0x00, /* wcChar0 */
  155. 'a', 0x00, /* wcChar1 */
  156. 'b', 0x00, /* wcChar2 */
  157. 'b', 0x00, /* wcChar3 */
  158. 'c', 0x00, /* wcChar4 */
  159. 'c', 0x00, /* wcChar5 */
  160. 'd', 0x00, /* wcChar6 */
  161. 'd', 0x00, /* wcChar7 */
  162. 'e', 0x00, /* wcChar8 */
  163. 'e', 0x00, /* wcChar9 */
  164. 'f', 0x00, /* wcChar10 */
  165. 'f', 0x00, /* wcChar11 */
  166. #ifdef CONFIG_USB_HS
  167. ///////////////////////////////////////
  168. /// device qualifier descriptor
  169. ///////////////////////////////////////
  170. 0x0a,
  171. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  172. 0x00,
  173. 0x02,
  174. 0x00,
  175. 0x00,
  176. 0x00,
  177. 0x40,
  178. 0x00,
  179. 0x00,
  180. #endif
  181. 0x00
  182. };
  183. #endif
  184. const uint8_t mac[6] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
  185. volatile bool cdc_ecm_tx_done = false;
  186. void usbd_cdc_ecm_data_send_done(uint32_t len)
  187. {
  188. cdc_ecm_tx_done = true; // suggest you to use semaphore in os
  189. }
  190. #ifdef RT_USING_LWIP
  191. #ifndef RT_LWIP_DHCP
  192. #error cdc_ecm must enable RT_LWIP_DHCP
  193. #endif
  194. #ifndef LWIP_USING_DHCPD
  195. #error cdc_ecm must enable LWIP_USING_DHCPD
  196. #endif
  197. #include <rtthread.h>
  198. #include <rtdevice.h>
  199. #include <netif/ethernetif.h>
  200. #include <dhcp_server.h>
  201. struct eth_device cdc_ecm_dev;
  202. static rt_err_t rt_usbd_cdc_ecm_control(rt_device_t dev, int cmd, void *args)
  203. {
  204. switch (cmd) {
  205. case NIOCTL_GADDR:
  206. /* get mac address */
  207. if (args) {
  208. uint8_t *mac_dev = (uint8_t *)args;
  209. rt_memcpy(mac_dev, mac, 6);
  210. mac_dev[5] = ~mac_dev[5]; /* device mac can't same as host. */
  211. } else
  212. return -RT_ERROR;
  213. break;
  214. default:
  215. break;
  216. }
  217. return RT_EOK;
  218. }
  219. struct pbuf *rt_usbd_cdc_ecm_eth_rx(rt_device_t dev)
  220. {
  221. return usbd_cdc_ecm_eth_rx();
  222. }
  223. rt_err_t rt_usbd_cdc_ecm_eth_tx(rt_device_t dev, struct pbuf *p)
  224. {
  225. int ret;
  226. cdc_ecm_tx_done = false;
  227. ret = usbd_cdc_ecm_eth_tx(p);
  228. if (ret == 0) {
  229. while (!cdc_ecm_tx_done) {
  230. }
  231. return RT_EOK;
  232. } else
  233. return -RT_ERROR;
  234. }
  235. void cdc_ecm_lwip_init(void)
  236. {
  237. cdc_ecm_dev.parent.control = rt_usbd_cdc_ecm_control;
  238. cdc_ecm_dev.eth_rx = rt_usbd_cdc_ecm_eth_rx;
  239. cdc_ecm_dev.eth_tx = rt_usbd_cdc_ecm_eth_tx;
  240. eth_device_init(&cdc_ecm_dev, "u0");
  241. eth_device_linkchange(&cdc_ecm_dev, RT_TRUE);
  242. dhcpd_start("u0");
  243. }
  244. void usbd_cdc_ecm_data_recv_done(uint32_t len)
  245. {
  246. eth_device_ready(&cdc_ecm_dev);
  247. }
  248. #else
  249. #include "netif/etharp.h"
  250. #include "lwip/init.h"
  251. #include "lwip/netif.h"
  252. #include "lwip/pbuf.h"
  253. #include "dhserver.h"
  254. #include "dnserver.h"
  255. /*Static IP ADDRESS: IP_ADDR0.IP_ADDR1.IP_ADDR2.IP_ADDR3 */
  256. #define IP_ADDR0 (uint8_t)192
  257. #define IP_ADDR1 (uint8_t)168
  258. #define IP_ADDR2 (uint8_t)7
  259. #define IP_ADDR3 (uint8_t)1
  260. /*NETMASK*/
  261. #define NETMASK_ADDR0 (uint8_t)255
  262. #define NETMASK_ADDR1 (uint8_t)255
  263. #define NETMASK_ADDR2 (uint8_t)255
  264. #define NETMASK_ADDR3 (uint8_t)0
  265. /*Gateway Address*/
  266. #define GW_ADDR0 (uint8_t)0
  267. #define GW_ADDR1 (uint8_t)0
  268. #define GW_ADDR2 (uint8_t)0
  269. #define GW_ADDR3 (uint8_t)0
  270. const ip_addr_t ipaddr = IPADDR4_INIT_BYTES(IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  271. const ip_addr_t netmask = IPADDR4_INIT_BYTES(NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  272. const ip_addr_t gateway = IPADDR4_INIT_BYTES(GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  273. #define NUM_DHCP_ENTRY 3
  274. static dhcp_entry_t entries[NUM_DHCP_ENTRY] = {
  275. /* mac ip address subnet mask lease time */
  276. { { 0 }, { 192, 168, 7, 2 }, { 255, 255, 255, 0 }, 24 * 60 * 60 },
  277. { { 0 }, { 192, 168, 7, 3 }, { 255, 255, 255, 0 }, 24 * 60 * 60 },
  278. { { 0 }, { 192, 168, 7, 4 }, { 255, 255, 255, 0 }, 24 * 60 * 60 }
  279. };
  280. static dhcp_config_t dhcp_config = {
  281. { 192, 168, 7, 1 }, /* server address */
  282. 67, /* port */
  283. { 192, 168, 7, 1 }, /* dns server */
  284. "cherry", /* dns suffix */
  285. NUM_DHCP_ENTRY, /* num entry */
  286. entries /* entries */
  287. };
  288. static bool dns_query_proc(const char *name, ip_addr_t *addr)
  289. {
  290. if (strcmp(name, "cdc_ecm.cherry") == 0 || strcmp(name, "www.cdc_ecm.cherry") == 0) {
  291. addr->addr = ipaddr.addr;
  292. return true;
  293. }
  294. return false;
  295. }
  296. static struct netif cdc_ecm_netif; //network interface
  297. /* Network interface name */
  298. #define IFNAME0 'E'
  299. #define IFNAME1 'X'
  300. err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
  301. {
  302. int ret;
  303. cdc_ecm_tx_done = false;
  304. ret = usbd_cdc_ecm_eth_tx(p);
  305. if (ret == 0) {
  306. while (!cdc_ecm_tx_done) {
  307. }
  308. return ERR_OK;
  309. } else
  310. return ERR_BUF;
  311. }
  312. err_t cdc_ecm_if_init(struct netif *netif)
  313. {
  314. LWIP_ASSERT("netif != NULL", (netif != NULL));
  315. netif->mtu = 1500;
  316. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
  317. netif->state = NULL;
  318. netif->name[0] = IFNAME0;
  319. netif->name[1] = IFNAME1;
  320. netif->output = etharp_output;
  321. netif->linkoutput = linkoutput_fn;
  322. return ERR_OK;
  323. }
  324. err_t cdc_ecm_if_input(struct netif *netif)
  325. {
  326. err_t err;
  327. struct pbuf *p;
  328. p = usbd_cdc_ecm_eth_rx();
  329. if (p != NULL) {
  330. err = netif->input(p, netif);
  331. if (err != ERR_OK) {
  332. pbuf_free(p);
  333. }
  334. } else {
  335. return ERR_BUF;
  336. }
  337. return err;
  338. }
  339. void cdc_ecm_lwip_init(void)
  340. {
  341. struct netif *netif = &cdc_ecm_netif;
  342. lwip_init();
  343. netif->hwaddr_len = 6;
  344. memcpy(netif->hwaddr, mac, 6);
  345. netif->hwaddr[5] = ~netif->hwaddr[5]; /* device mac can't same as host. */
  346. netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, cdc_ecm_if_init, netif_input);
  347. netif_set_default(netif);
  348. while (!netif_is_up(netif)) {
  349. }
  350. while (dhserv_init(&dhcp_config)) {
  351. }
  352. while (dnserv_init(IP_ADDR_ANY, 53, dns_query_proc)) {
  353. }
  354. }
  355. void usbd_cdc_ecm_data_recv_done(uint32_t len)
  356. {
  357. }
  358. void cdc_ecm_input_poll(void)
  359. {
  360. cdc_ecm_if_input(&cdc_ecm_netif);
  361. }
  362. #endif
  363. static void usbd_event_handler(uint8_t busid, uint8_t event)
  364. {
  365. switch (event) {
  366. case USBD_EVENT_RESET:
  367. break;
  368. case USBD_EVENT_CONNECTED:
  369. break;
  370. case USBD_EVENT_DISCONNECTED:
  371. break;
  372. case USBD_EVENT_RESUME:
  373. break;
  374. case USBD_EVENT_SUSPEND:
  375. break;
  376. case USBD_EVENT_CONFIGURED:
  377. break;
  378. case USBD_EVENT_SET_REMOTE_WAKEUP:
  379. break;
  380. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  381. break;
  382. default:
  383. break;
  384. }
  385. }
  386. struct usbd_interface intf0;
  387. struct usbd_interface intf1;
  388. /* ecm only supports in linux, and you should input the following command
  389. *
  390. * sudo ifconfig enxaabbccddeeff up
  391. * sudo dhcpclient enxaabbccddeeff
  392. */
  393. void cdc_ecm_init(uint8_t busid, uintptr_t reg_base)
  394. {
  395. cdc_ecm_lwip_init();
  396. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  397. usbd_desc_register(busid, &cdc_ecm_descriptor);
  398. #else
  399. usbd_desc_register(busid, cdc_ecm_descriptor);
  400. #endif
  401. usbd_add_interface(busid, usbd_cdc_ecm_init_intf(&intf0, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP));
  402. usbd_add_interface(busid, usbd_cdc_ecm_init_intf(&intf1, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP));
  403. usbd_initialize(busid, reg_base, usbd_event_handler);
  404. }