cdc_ecm_template.c 13 KB

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