cdc_rndis_template.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. volatile bool rndis_tx_done = false;
  164. void usbd_rndis_data_send_done(uint32_t len)
  165. {
  166. rndis_tx_done = true; // suggest you to use semaphore in os
  167. }
  168. #ifdef RT_USING_LWIP
  169. #ifndef RT_LWIP_DHCP
  170. #error rndis must enable RT_LWIP_DHCP
  171. #endif
  172. #ifndef LWIP_USING_DHCPD
  173. #error rndis must enable LWIP_USING_DHCPD
  174. #endif
  175. #include <rtthread.h>
  176. #include <rtdevice.h>
  177. #include <netif/ethernetif.h>
  178. #include <dhcp_server.h>
  179. struct eth_device rndis_dev;
  180. static rt_err_t rt_usbd_rndis_control(rt_device_t dev, int cmd, void *args)
  181. {
  182. switch (cmd) {
  183. case NIOCTL_GADDR:
  184. /* get mac address */
  185. if (args) {
  186. uint8_t *mac_dev = (uint8_t *)args;
  187. rt_memcpy(mac_dev, mac, 6);
  188. mac_dev[5] = ~mac_dev[5]; /* device mac can't same as host. */
  189. } else
  190. return -RT_ERROR;
  191. break;
  192. default:
  193. break;
  194. }
  195. return RT_EOK;
  196. }
  197. struct pbuf *rt_usbd_rndis_eth_rx(rt_device_t dev)
  198. {
  199. return usbd_rndis_eth_rx();
  200. }
  201. rt_err_t rt_usbd_rndis_eth_tx(rt_device_t dev, struct pbuf *p)
  202. {
  203. int ret;
  204. rndis_tx_done = false;
  205. ret = usbd_rndis_eth_tx(p);
  206. if (ret == 0) {
  207. while (!rndis_tx_done) {
  208. }
  209. return RT_EOK;
  210. } else
  211. return -RT_ERROR;
  212. }
  213. void rndis_lwip_init(void)
  214. {
  215. rndis_dev.parent.control = rt_usbd_rndis_control;
  216. rndis_dev.eth_rx = rt_usbd_rndis_eth_rx;
  217. rndis_dev.eth_tx = rt_usbd_rndis_eth_tx;
  218. eth_device_init(&rndis_dev, "u0");
  219. eth_device_linkchange(&rndis_dev, RT_TRUE);
  220. dhcpd_start("u0");
  221. }
  222. void usbd_rndis_data_recv_done(uint32_t len)
  223. {
  224. eth_device_ready(&rndis_dev);
  225. }
  226. #else
  227. #include "netif/etharp.h"
  228. #include "lwip/init.h"
  229. #include "lwip/netif.h"
  230. #include "lwip/pbuf.h"
  231. #include "dhserver.h"
  232. #include "dnserver.h"
  233. /*Static IP ADDRESS: IP_ADDR0.IP_ADDR1.IP_ADDR2.IP_ADDR3 */
  234. #define IP_ADDR0 (uint8_t)192
  235. #define IP_ADDR1 (uint8_t)168
  236. #define IP_ADDR2 (uint8_t)7
  237. #define IP_ADDR3 (uint8_t)1
  238. /*NETMASK*/
  239. #define NETMASK_ADDR0 (uint8_t)255
  240. #define NETMASK_ADDR1 (uint8_t)255
  241. #define NETMASK_ADDR2 (uint8_t)255
  242. #define NETMASK_ADDR3 (uint8_t)0
  243. /*Gateway Address*/
  244. #define GW_ADDR0 (uint8_t)0
  245. #define GW_ADDR1 (uint8_t)0
  246. #define GW_ADDR2 (uint8_t)0
  247. #define GW_ADDR3 (uint8_t)0
  248. const ip_addr_t ipaddr = IPADDR4_INIT_BYTES(IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  249. const ip_addr_t netmask = IPADDR4_INIT_BYTES(NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  250. const ip_addr_t gateway = IPADDR4_INIT_BYTES(GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  251. #define NUM_DHCP_ENTRY 3
  252. static dhcp_entry_t entries[NUM_DHCP_ENTRY] = {
  253. /* mac ip address subnet mask lease time */
  254. { { 0 }, { 192, 168, 7, 2 }, { 255, 255, 255, 0 }, 24 * 60 * 60 },
  255. { { 0 }, { 192, 168, 7, 3 }, { 255, 255, 255, 0 }, 24 * 60 * 60 },
  256. { { 0 }, { 192, 168, 7, 4 }, { 255, 255, 255, 0 }, 24 * 60 * 60 }
  257. };
  258. static dhcp_config_t dhcp_config = {
  259. { 192, 168, 7, 1 }, /* server address */
  260. 67, /* port */
  261. { 192, 168, 7, 1 }, /* dns server */
  262. "cherry", /* dns suffix */
  263. NUM_DHCP_ENTRY, /* num entry */
  264. entries /* entries */
  265. };
  266. static bool dns_query_proc(const char *name, ip_addr_t *addr)
  267. {
  268. if (strcmp(name, "rndis.cherry") == 0 || strcmp(name, "www.rndis.cherry") == 0) {
  269. addr->addr = ipaddr.addr;
  270. return true;
  271. }
  272. return false;
  273. }
  274. static struct netif rndis_netif; //network interface
  275. /* Network interface name */
  276. #define IFNAME0 'E'
  277. #define IFNAME1 'X'
  278. err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
  279. {
  280. int ret;
  281. rndis_tx_done = false;
  282. ret = usbd_rndis_eth_tx(p);
  283. if (ret == 0) {
  284. while (!rndis_tx_done) {
  285. }
  286. return ERR_OK;
  287. } else
  288. return ERR_BUF;
  289. }
  290. err_t rndisif_init(struct netif *netif)
  291. {
  292. LWIP_ASSERT("netif != NULL", (netif != NULL));
  293. netif->mtu = 1500;
  294. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
  295. netif->state = NULL;
  296. netif->name[0] = IFNAME0;
  297. netif->name[1] = IFNAME1;
  298. netif->output = etharp_output;
  299. netif->linkoutput = linkoutput_fn;
  300. return ERR_OK;
  301. }
  302. err_t rndisif_input(struct netif *netif)
  303. {
  304. err_t err;
  305. struct pbuf *p;
  306. p = usbd_rndis_eth_rx();
  307. if (p != NULL) {
  308. err = netif->input(p, netif);
  309. if (err != ERR_OK) {
  310. pbuf_free(p);
  311. }
  312. } else {
  313. return ERR_BUF;
  314. }
  315. return err;
  316. }
  317. void rndis_lwip_init(void)
  318. {
  319. struct netif *netif = &rndis_netif;
  320. lwip_init();
  321. netif->hwaddr_len = 6;
  322. memcpy(netif->hwaddr, mac, 6);
  323. netif->hwaddr[5] = ~netif->hwaddr[5]; /* device mac can't same as host. */
  324. netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, rndisif_init, netif_input);
  325. netif_set_default(netif);
  326. while (!netif_is_up(netif)) {
  327. }
  328. while (dhserv_init(&dhcp_config)) {
  329. }
  330. while (dnserv_init(IP_ADDR_ANY, 53, dns_query_proc)) {
  331. }
  332. }
  333. void usbd_rndis_data_recv_done(uint32_t len)
  334. {
  335. }
  336. void rndis_input_poll(void)
  337. {
  338. rndisif_input(&rndis_netif);
  339. }
  340. #endif /* RT_USING_LWIP */
  341. static void usbd_event_handler(uint8_t busid, uint8_t event)
  342. {
  343. switch (event) {
  344. case USBD_EVENT_RESET:
  345. break;
  346. case USBD_EVENT_CONNECTED:
  347. break;
  348. case USBD_EVENT_DISCONNECTED:
  349. break;
  350. case USBD_EVENT_RESUME:
  351. break;
  352. case USBD_EVENT_SUSPEND:
  353. break;
  354. case USBD_EVENT_CONFIGURED:
  355. break;
  356. case USBD_EVENT_SET_REMOTE_WAKEUP:
  357. break;
  358. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  359. break;
  360. default:
  361. break;
  362. }
  363. }
  364. struct usbd_interface intf0;
  365. struct usbd_interface intf1;
  366. void cdc_rndis_init(uint8_t busid, uintptr_t reg_base)
  367. {
  368. rndis_lwip_init();
  369. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  370. usbd_desc_register(busid, &cdc_rndis_descriptor);
  371. #else
  372. usbd_desc_register(busid, cdc_rndis_descriptor);
  373. #endif
  374. usbd_add_interface(busid, usbd_rndis_init_intf(&intf0, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP, (uint8_t *)mac));
  375. usbd_add_interface(busid, usbd_rndis_init_intf(&intf1, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP, (uint8_t *)mac));
  376. usbd_initialize(busid, reg_base, usbd_event_handler);
  377. }