cdc_rndis_template.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usbd_rndis.h"
  8. #ifndef CONFIG_USBDEV_RNDIS_USING_LWIP
  9. #error "Please enable CONFIG_USBDEV_RNDIS_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 0xEFFF
  16. #define USBD_PID 0xEFFF
  17. #define USBD_MAX_POWER 100
  18. #define USBD_LANGID_STRING 1033
  19. /*!< config descriptor size */
  20. #define USB_CONFIG_SIZE (9 + CDC_RNDIS_DESCRIPTOR_LEN)
  21. #ifdef CONFIG_USB_HS
  22. #define CDC_MAX_MPS 512
  23. #else
  24. #define CDC_MAX_MPS 64
  25. #endif
  26. /*!< global descriptor */
  27. static const uint8_t cdc_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_RNDIS_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, 0x02),
  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. 0x2A, /* 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. 'R', 0x00, /* wcChar10 */
  65. 'N', 0x00, /* wcChar11 */
  66. 'D', 0x00, /* wcChar12 */
  67. 'I', 0x00, /* wcChar13 */
  68. 'S', 0x00, /* wcChar14 */
  69. ' ', 0x00, /* wcChar15 */
  70. 'D', 0x00, /* wcChar16 */
  71. 'E', 0x00, /* wcChar17 */
  72. 'M', 0x00, /* wcChar18 */
  73. 'O', 0x00, /* wcChar19 */
  74. ///////////////////////////////////////
  75. /// string3 descriptor
  76. ///////////////////////////////////////
  77. 0x16, /* bLength */
  78. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  79. '2', 0x00, /* wcChar0 */
  80. '0', 0x00, /* wcChar1 */
  81. '2', 0x00, /* wcChar2 */
  82. '2', 0x00, /* wcChar3 */
  83. '1', 0x00, /* wcChar4 */
  84. '2', 0x00, /* wcChar5 */
  85. '3', 0x00, /* wcChar6 */
  86. '4', 0x00, /* wcChar7 */
  87. '5', 0x00, /* wcChar8 */
  88. '6', 0x00, /* wcChar9 */
  89. #ifdef CONFIG_USB_HS
  90. ///////////////////////////////////////
  91. /// device qualifier descriptor
  92. ///////////////////////////////////////
  93. 0x0a,
  94. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  95. 0x00,
  96. 0x02,
  97. 0x02,
  98. 0x02,
  99. 0x01,
  100. 0x40,
  101. 0x00,
  102. 0x00,
  103. #endif
  104. 0x00
  105. };
  106. const uint8_t mac[6] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
  107. /*Static IP ADDRESS: IP_ADDR0.IP_ADDR1.IP_ADDR2.IP_ADDR3 */
  108. #define IP_ADDR0 (uint8_t)192
  109. #define IP_ADDR1 (uint8_t)168
  110. #define IP_ADDR2 (uint8_t)123
  111. #define IP_ADDR3 (uint8_t)100
  112. /*NETMASK*/
  113. #define NETMASK_ADDR0 (uint8_t)255
  114. #define NETMASK_ADDR1 (uint8_t)255
  115. #define NETMASK_ADDR2 (uint8_t)255
  116. #define NETMASK_ADDR3 (uint8_t)0
  117. /*Gateway Address*/
  118. #define GW_ADDR0 (uint8_t)192
  119. #define GW_ADDR1 (uint8_t)168
  120. #define GW_ADDR2 (uint8_t)123
  121. #define GW_ADDR3 (uint8_t)1
  122. #ifdef RT_USING_LWIP
  123. #include <rtthread.h>
  124. #include <rtdevice.h>
  125. #include <netif/ethernetif.h>
  126. const ip_addr_t ipaddr = IPADDR4_INIT_BYTES(IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  127. const ip_addr_t netmask = IPADDR4_INIT_BYTES(NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  128. const ip_addr_t gateway = IPADDR4_INIT_BYTES(GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  129. struct eth_device rndis_dev;
  130. static rt_err_t rt_usbd_rndis_control(rt_device_t dev, int cmd, void *args)
  131. {
  132. switch (cmd) {
  133. case NIOCTL_GADDR:
  134. /* get mac address */
  135. if (args)
  136. {
  137. uint8_t *mac_dev = (uint8_t *)args;
  138. rt_memcpy(mac_dev, mac, 6);
  139. mac_dev[5] = ~mac_dev[5]; /* device mac can't same as host. */
  140. }
  141. else
  142. return -RT_ERROR;
  143. break;
  144. default:
  145. break;
  146. }
  147. return RT_EOK;
  148. }
  149. struct pbuf *rt_usbd_rndis_eth_rx(rt_device_t dev)
  150. {
  151. return usbd_rndis_eth_rx();
  152. }
  153. rt_err_t rt_usbd_rndis_eth_tx(rt_device_t dev, struct pbuf *p)
  154. {
  155. return usbd_rndis_eth_tx(p);
  156. }
  157. void usbd_rndis_data_recv_done(uint32_t len)
  158. {
  159. eth_device_ready(&rndis_dev);
  160. }
  161. void rt_usbd_rndis_init(void)
  162. {
  163. rndis_dev.parent.control = rt_usbd_rndis_control;
  164. rndis_dev.eth_rx = rt_usbd_rndis_eth_rx;
  165. rndis_dev.eth_tx = rt_usbd_rndis_eth_tx;
  166. eth_device_init(&rndis_dev, "u0");
  167. eth_device_linkchange(&rndis_dev, RT_FALSE);
  168. }
  169. #else
  170. #include "netif/etharp.h"
  171. #include "lwip/init.h"
  172. #include "lwip/netif.h"
  173. #include "lwip/pbuf.h"
  174. const ip_addr_t ipaddr = IPADDR4_INIT_BYTES(IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  175. const ip_addr_t netmask = IPADDR4_INIT_BYTES(NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  176. const ip_addr_t gateway = IPADDR4_INIT_BYTES(GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  177. static struct netif rndis_netif; //network interface
  178. /* Network interface name */
  179. #define IFNAME0 'E'
  180. #define IFNAME1 'X'
  181. err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
  182. {
  183. static int ret;
  184. ret = usbd_rndis_eth_tx(p);
  185. if (ret == 0)
  186. return ERR_OK;
  187. else
  188. return ERR_BUF;
  189. }
  190. err_t rndisif_init(struct netif *netif)
  191. {
  192. LWIP_ASSERT("netif != NULL", (netif != NULL));
  193. netif->mtu = 1500;
  194. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
  195. netif->state = NULL;
  196. netif->name[0] = IFNAME0;
  197. netif->name[1] = IFNAME1;
  198. netif->output = etharp_output;
  199. netif->linkoutput = linkoutput_fn;
  200. return ERR_OK;
  201. }
  202. err_t rndisif_input(struct netif *netif)
  203. {
  204. static err_t err;
  205. static struct pbuf *p;
  206. p = usbd_rndis_eth_rx();
  207. if (p != NULL) {
  208. err = netif->input(p, netif);
  209. if (err != ERR_OK) {
  210. pbuf_free(p);
  211. }
  212. } else {
  213. return ERR_BUF;
  214. }
  215. return err;
  216. }
  217. void rndis_lwip_init(void)
  218. {
  219. struct netif *netif = &rndis_netif;
  220. lwip_init();
  221. netif->hwaddr_len = 6;
  222. memcpy(netif->hwaddr, mac, 6);
  223. netif->hwaddr[5] = ~netif->hwaddr[5]; /* device mac can't same as host. */
  224. netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, rndisif_init, netif_input);
  225. netif_set_default(netif);
  226. while (!netif_is_up(netif)) {
  227. }
  228. while (dhserv_init(&dhcp_config)) {}
  229. while (dnserv_init(&ipaddr, PORT_DNS, dns_query_proc)) {}
  230. }
  231. void usbd_rndis_data_recv_done(uint32_t len)
  232. {
  233. }
  234. void rndis_input_poll(void)
  235. {
  236. rndisif_input(&rndis_netif);
  237. }
  238. #endif /* RT_USING_LWIP */
  239. static void usbd_event_handler(uint8_t busid, uint8_t event)
  240. {
  241. switch (event) {
  242. case USBD_EVENT_RESET:
  243. break;
  244. case USBD_EVENT_CONNECTED:
  245. break;
  246. case USBD_EVENT_DISCONNECTED:
  247. break;
  248. case USBD_EVENT_RESUME:
  249. break;
  250. case USBD_EVENT_SUSPEND:
  251. break;
  252. case USBD_EVENT_CONFIGURED:
  253. #ifdef RT_USING_LWIP
  254. eth_device_linkchange(&rndis_dev, RT_TRUE);
  255. #endif
  256. break;
  257. case USBD_EVENT_SET_REMOTE_WAKEUP:
  258. break;
  259. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  260. break;
  261. default:
  262. break;
  263. }
  264. }
  265. struct usbd_interface intf0;
  266. struct usbd_interface intf1;
  267. void cdc_rndis_init(uint8_t busid, uintptr_t reg_base)
  268. {
  269. #ifdef RT_USING_LWIP
  270. rt_usbd_rndis_init();
  271. #else
  272. rndis_lwip_init();
  273. #endif
  274. usbd_desc_register(busid, cdc_descriptor);
  275. usbd_add_interface(busid, usbd_rndis_init_intf(&intf0, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP, mac));
  276. usbd_add_interface(busid, usbd_rndis_init_intf(&intf1, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP, mac));
  277. usbd_initialize(busid, reg_base, usbd_event_handler);
  278. }