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 
9 #ifndef CONFIG_USBDEV_RNDIS_USING_LWIP
10 #error "Please enable CONFIG_USBDEV_RNDIS_USING_LWIP for this demo"
11 #endif
12 
13 /*!< endpoint address */
14 #define CDC_IN_EP  0x81
15 #define CDC_OUT_EP 0x02
16 #define CDC_INT_EP 0x83
17 
18 #define USBD_VID           0xEFFF
19 #define USBD_PID           0xEFFF
20 #define USBD_MAX_POWER     100
21 #define USBD_LANGID_STRING 1033
22 
23 /*!< config descriptor size */
24 #define USB_CONFIG_SIZE (9 + CDC_RNDIS_DESCRIPTOR_LEN)
25 
26 #ifdef CONFIG_USB_HS
27 #define CDC_MAX_MPS 512
28 #else
29 #define CDC_MAX_MPS 64
30 #endif
31 
32 #ifdef CONFIG_USBDEV_ADVANCE_DESC
33 static const uint8_t device_descriptor[] = {
34     USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01)
35 };
36 
37 static const uint8_t config_descriptor[] = {
38     USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
39     CDC_RNDIS_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, 0x02)
40 };
41 
42 static const uint8_t device_quality_descriptor[] = {
43     ///////////////////////////////////////
44     /// device qualifier descriptor
45     ///////////////////////////////////////
46     0x0a,
47     USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
48     0x00,
49     0x02,
50     0x00,
51     0x00,
52     0x00,
53     0x40,
54     0x00,
55     0x00,
56 };
57 
58 static const char *string_descriptors[] = {
59     (const char[]){ 0x09, 0x04 }, /* Langid */
60     "CherryUSB",                  /* Manufacturer */
61     "CherryUSB RNDIS DEMO",       /* Product */
62     "2022123456",                 /* Serial Number */
63 };
64 
device_descriptor_callback(uint8_t speed)65 static const uint8_t *device_descriptor_callback(uint8_t speed)
66 {
67     return device_descriptor;
68 }
69 
config_descriptor_callback(uint8_t speed)70 static const uint8_t *config_descriptor_callback(uint8_t speed)
71 {
72     return config_descriptor;
73 }
74 
device_quality_descriptor_callback(uint8_t speed)75 static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
76 {
77     return device_quality_descriptor;
78 }
79 
string_descriptor_callback(uint8_t speed,uint8_t index)80 static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
81 {
82     if (index > 3) {
83         return NULL;
84     }
85     return string_descriptors[index];
86 }
87 
88 const struct usb_descriptor cdc_rndis_descriptor = {
89     .device_descriptor_callback = device_descriptor_callback,
90     .config_descriptor_callback = config_descriptor_callback,
91     .device_quality_descriptor_callback = device_quality_descriptor_callback,
92     .string_descriptor_callback = string_descriptor_callback
93 };
94 #else
95 /*!< global descriptor */
96 static const uint8_t cdc_rndis_descriptor[] = {
97     USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01),
98     USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
99     CDC_RNDIS_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, 0x02),
100     ///////////////////////////////////////
101     /// string0 descriptor
102     ///////////////////////////////////////
103     USB_LANGID_INIT(USBD_LANGID_STRING),
104     ///////////////////////////////////////
105     /// string1 descriptor
106     ///////////////////////////////////////
107     0x14,                       /* 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     ///////////////////////////////////////
119     /// string2 descriptor
120     ///////////////////////////////////////
121     0x2A,                       /* bLength */
122     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
123     'C', 0x00,                  /* wcChar0 */
124     'h', 0x00,                  /* wcChar1 */
125     'e', 0x00,                  /* wcChar2 */
126     'r', 0x00,                  /* wcChar3 */
127     'r', 0x00,                  /* wcChar4 */
128     'y', 0x00,                  /* wcChar5 */
129     'U', 0x00,                  /* wcChar6 */
130     'S', 0x00,                  /* wcChar7 */
131     'B', 0x00,                  /* wcChar8 */
132     ' ', 0x00,                  /* wcChar9 */
133     'R', 0x00,                  /* wcChar10 */
134     'N', 0x00,                  /* wcChar11 */
135     'D', 0x00,                  /* wcChar12 */
136     'I', 0x00,                  /* wcChar13 */
137     'S', 0x00,                  /* wcChar14 */
138     ' ', 0x00,                  /* wcChar15 */
139     'D', 0x00,                  /* wcChar16 */
140     'E', 0x00,                  /* wcChar17 */
141     'M', 0x00,                  /* wcChar18 */
142     'O', 0x00,                  /* wcChar19 */
143     ///////////////////////////////////////
144     /// string3 descriptor
145     ///////////////////////////////////////
146     0x16,                       /* bLength */
147     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
148     '2', 0x00,                  /* wcChar0 */
149     '0', 0x00,                  /* wcChar1 */
150     '2', 0x00,                  /* wcChar2 */
151     '2', 0x00,                  /* wcChar3 */
152     '1', 0x00,                  /* wcChar4 */
153     '2', 0x00,                  /* wcChar5 */
154     '3', 0x00,                  /* wcChar6 */
155     '4', 0x00,                  /* wcChar7 */
156     '5', 0x00,                  /* wcChar8 */
157     '6', 0x00,                  /* wcChar9 */
158 #ifdef CONFIG_USB_HS
159     ///////////////////////////////////////
160     /// device qualifier descriptor
161     ///////////////////////////////////////
162     0x0a,
163     USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
164     0x00,
165     0x02,
166     0x00,
167     0x00,
168     0x00,
169     0x40,
170     0x00,
171     0x00,
172 #endif
173     0x00
174 };
175 #endif
176 
177 const uint8_t mac[6] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
178 
179 volatile bool rndis_tx_done = false;
180 
usbd_rndis_data_send_done(uint32_t len)181 void usbd_rndis_data_send_done(uint32_t len)
182 {
183     rndis_tx_done = true; // suggest you to use semaphore in os
184 }
185 
186 #ifdef RT_USING_LWIP
187 
188 #ifndef RT_LWIP_DHCP
189 #error rndis must enable RT_LWIP_DHCP
190 #endif
191 
192 #ifndef LWIP_USING_DHCPD
193 #error rndis must enable LWIP_USING_DHCPD
194 #endif
195 
196 #include <rtthread.h>
197 #include <rtdevice.h>
198 #include <netif/ethernetif.h>
199 #include <dhcp_server.h>
200 
201 struct eth_device rndis_dev;
202 
rt_usbd_rndis_control(rt_device_t dev,int cmd,void * args)203 static rt_err_t rt_usbd_rndis_control(rt_device_t dev, int cmd, void *args)
204 {
205     switch (cmd) {
206         case NIOCTL_GADDR:
207 
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 
216             break;
217 
218         default:
219             break;
220     }
221 
222     return RT_EOK;
223 }
224 
rt_usbd_rndis_eth_rx(rt_device_t dev)225 struct pbuf *rt_usbd_rndis_eth_rx(rt_device_t dev)
226 {
227     return usbd_rndis_eth_rx();
228 }
229 
rt_usbd_rndis_eth_tx(rt_device_t dev,struct pbuf * p)230 rt_err_t rt_usbd_rndis_eth_tx(rt_device_t dev, struct pbuf *p)
231 {
232     int ret;
233 
234     rndis_tx_done = false;
235     ret = usbd_rndis_eth_tx(p);
236     if (ret == 0) {
237         while (!rndis_tx_done) {
238         }
239         return RT_EOK;
240     } else
241         return -RT_ERROR;
242 }
243 
rndis_lwip_init(void)244 void rndis_lwip_init(void)
245 {
246     rndis_dev.parent.control = rt_usbd_rndis_control;
247     rndis_dev.eth_rx = rt_usbd_rndis_eth_rx;
248     rndis_dev.eth_tx = rt_usbd_rndis_eth_tx;
249 
250     eth_device_init(&rndis_dev, "u0");
251 
252     eth_device_linkchange(&rndis_dev, RT_TRUE);
253     dhcpd_start("u0");
254 }
255 
usbd_rndis_data_recv_done(uint32_t len)256 void usbd_rndis_data_recv_done(uint32_t len)
257 {
258     eth_device_ready(&rndis_dev);
259 }
260 
261 #else
262 #include "netif/etharp.h"
263 #include "lwip/init.h"
264 #include "lwip/netif.h"
265 #include "lwip/pbuf.h"
266 
267 #include "dhserver.h"
268 #include "dnserver.h"
269 
270 /*Static IP ADDRESS: IP_ADDR0.IP_ADDR1.IP_ADDR2.IP_ADDR3 */
271 #define IP_ADDR0      (uint8_t)192
272 #define IP_ADDR1      (uint8_t)168
273 #define IP_ADDR2      (uint8_t)7
274 #define IP_ADDR3      (uint8_t)1
275 
276 /*NETMASK*/
277 #define NETMASK_ADDR0 (uint8_t)255
278 #define NETMASK_ADDR1 (uint8_t)255
279 #define NETMASK_ADDR2 (uint8_t)255
280 #define NETMASK_ADDR3 (uint8_t)0
281 
282 /*Gateway Address*/
283 #define GW_ADDR0      (uint8_t)0
284 #define GW_ADDR1      (uint8_t)0
285 #define GW_ADDR2      (uint8_t)0
286 #define GW_ADDR3      (uint8_t)0
287 
288 const ip_addr_t ipaddr = IPADDR4_INIT_BYTES(IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
289 const ip_addr_t netmask = IPADDR4_INIT_BYTES(NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
290 const ip_addr_t gateway = IPADDR4_INIT_BYTES(GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
291 
292 #define NUM_DHCP_ENTRY 3
293 
294 static dhcp_entry_t entries[NUM_DHCP_ENTRY] = {
295     /* mac    ip address        subnet mask        lease time */
296     { { 0 }, { 192, 168, 7, 2 }, { 255, 255, 255, 0 }, 24 * 60 * 60 },
297     { { 0 }, { 192, 168, 7, 3 }, { 255, 255, 255, 0 }, 24 * 60 * 60 },
298     { { 0 }, { 192, 168, 7, 4 }, { 255, 255, 255, 0 }, 24 * 60 * 60 }
299 };
300 
301 static dhcp_config_t dhcp_config = {
302     { 192, 168, 7, 1 }, /* server address */
303     67,                 /* port */
304     { 192, 168, 7, 1 }, /* dns server */
305     "cherry",           /* dns suffix */
306     NUM_DHCP_ENTRY,     /* num entry */
307     entries             /* entries */
308 };
309 
dns_query_proc(const char * name,ip_addr_t * addr)310 static bool dns_query_proc(const char *name, ip_addr_t *addr)
311 {
312     if (strcmp(name, "rndis.cherry") == 0 || strcmp(name, "www.rndis.cherry") == 0) {
313         addr->addr = ipaddr.addr;
314         return true;
315     }
316     return false;
317 }
318 
319 static struct netif rndis_netif; //network interface
320 
321 /* Network interface name */
322 #define IFNAME0        'E'
323 #define IFNAME1        'X'
324 
linkoutput_fn(struct netif * netif,struct pbuf * p)325 err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
326 {
327     int ret;
328 
329     rndis_tx_done = false;
330     ret = usbd_rndis_eth_tx(p);
331     if (ret == 0) {
332         while (!rndis_tx_done) {
333         }
334         return ERR_OK;
335     } else
336         return ERR_BUF;
337 }
338 
rndisif_init(struct netif * netif)339 err_t rndisif_init(struct netif *netif)
340 {
341     LWIP_ASSERT("netif != NULL", (netif != NULL));
342 
343     netif->mtu = 1500;
344     netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
345     netif->state = NULL;
346     netif->name[0] = IFNAME0;
347     netif->name[1] = IFNAME1;
348     netif->output = etharp_output;
349     netif->linkoutput = linkoutput_fn;
350     return ERR_OK;
351 }
352 
rndisif_input(struct netif * netif)353 err_t rndisif_input(struct netif *netif)
354 {
355     err_t err;
356     struct pbuf *p;
357 
358     p = usbd_rndis_eth_rx();
359     if (p != NULL) {
360         err = netif->input(p, netif);
361         if (err != ERR_OK) {
362             pbuf_free(p);
363         }
364     } else {
365         return ERR_BUF;
366     }
367     return err;
368 }
369 
rndis_lwip_init(void)370 void rndis_lwip_init(void)
371 {
372     struct netif *netif = &rndis_netif;
373 
374     lwip_init();
375 
376     netif->hwaddr_len = 6;
377     memcpy(netif->hwaddr, mac, 6);
378     netif->hwaddr[5] = ~netif->hwaddr[5]; /* device mac can't same as host. */
379 
380     netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, rndisif_init, netif_input);
381     netif_set_default(netif);
382     while (!netif_is_up(netif)) {
383     }
384 
385     while (dhserv_init(&dhcp_config)) {
386     }
387 
388     while (dnserv_init(IP_ADDR_ANY, 53, dns_query_proc)) {
389     }
390 }
391 
usbd_rndis_data_recv_done(uint32_t len)392 void usbd_rndis_data_recv_done(uint32_t len)
393 {
394 }
395 
rndis_input_poll(void)396 void rndis_input_poll(void)
397 {
398     rndisif_input(&rndis_netif);
399 }
400 #endif /* RT_USING_LWIP */
401 
usbd_event_handler(uint8_t busid,uint8_t event)402 static void usbd_event_handler(uint8_t busid, uint8_t event)
403 {
404     switch (event) {
405         case USBD_EVENT_RESET:
406             break;
407         case USBD_EVENT_CONNECTED:
408             break;
409         case USBD_EVENT_DISCONNECTED:
410             break;
411         case USBD_EVENT_RESUME:
412             break;
413         case USBD_EVENT_SUSPEND:
414             break;
415         case USBD_EVENT_CONFIGURED:
416             break;
417         case USBD_EVENT_SET_REMOTE_WAKEUP:
418             break;
419         case USBD_EVENT_CLR_REMOTE_WAKEUP:
420             break;
421 
422         default:
423             break;
424     }
425 }
426 
427 struct usbd_interface intf0;
428 struct usbd_interface intf1;
429 
cdc_rndis_init(uint8_t busid,uintptr_t reg_base)430 void cdc_rndis_init(uint8_t busid, uintptr_t reg_base)
431 {
432     rndis_lwip_init();
433 
434 #ifdef CONFIG_USBDEV_ADVANCE_DESC
435     usbd_desc_register(busid, &cdc_rndis_descriptor);
436 #else
437     usbd_desc_register(busid, cdc_rndis_descriptor);
438 #endif
439     usbd_add_interface(busid, usbd_rndis_init_intf(&intf0, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP, (uint8_t *)mac));
440     usbd_add_interface(busid, usbd_rndis_init_intf(&intf1, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP, (uint8_t *)mac));
441     usbd_initialize(busid, reg_base, usbd_event_handler);
442 }
443