cdc_rndis_template.c 8.8 KB

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