cdc_rndis_template.c 11 KB

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