1 #include "usbd_core.h"
2 #include "usbd_rndis.h"
3
4 /*!< endpoint address */
5 #define CDC_IN_EP 0x81
6 #define CDC_OUT_EP 0x02
7 #define CDC_INT_EP 0x83
8
9 #define USBD_VID 0xEFFF
10 #define USBD_PID 0xEFFF
11 #define USBD_MAX_POWER 100
12 #define USBD_LANGID_STRING 1033
13
14 /*!< config descriptor size */
15 #define USB_CONFIG_SIZE (9 + CDC_RNDIS_DESCRIPTOR_LEN)
16
17 #ifdef CONFIG_USB_HS
18 #define CDC_MAX_MPS 512
19 #else
20 #define CDC_MAX_MPS 64
21 #endif
22
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 0x01,
99 0x00,
100 #endif
101 0x00
102 };
103
104 const uint8_t mac[6] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
105 /*Static IP ADDRESS: IP_ADDR0.IP_ADDR1.IP_ADDR2.IP_ADDR3 */
106 #define IP_ADDR0 (uint8_t)192
107 #define IP_ADDR1 (uint8_t)168
108 #define IP_ADDR2 (uint8_t)123
109 #define IP_ADDR3 (uint8_t)100
110
111 /*NETMASK*/
112 #define NETMASK_ADDR0 (uint8_t)255
113 #define NETMASK_ADDR1 (uint8_t)255
114 #define NETMASK_ADDR2 (uint8_t)255
115 #define NETMASK_ADDR3 (uint8_t)0
116
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
123 #ifdef RT_USING_LWIP
124 #include <rtthread.h>
125 #include <rtdevice.h>
126 #include <netif/ethernetif.h>
127
128 const ip_addr_t ipaddr = IPADDR4_INIT_BYTES(IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
129 const ip_addr_t netmask = IPADDR4_INIT_BYTES(NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
130 const ip_addr_t gateway = IPADDR4_INIT_BYTES(GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
131
132 struct eth_device rndis_dev;
133
rt_usbd_rndis_control(rt_device_t dev,int cmd,void * args)134 static rt_err_t rt_usbd_rndis_control(rt_device_t dev, int cmd, void *args)
135 {
136 switch (cmd) {
137 case NIOCTL_GADDR:
138
139 /* get mac address */
140 if (args)
141 rt_memcpy(args, mac, 6);
142 else
143 return -RT_ERROR;
144
145 break;
146
147 default:
148 break;
149 }
150
151 return RT_EOK;
152 }
153
rt_usbd_rndis_eth_rx(rt_device_t dev)154 struct pbuf *rt_usbd_rndis_eth_rx(rt_device_t dev)
155 {
156 return usbd_rndis_eth_rx();
157 }
158
rt_usbd_rndis_eth_tx(rt_device_t dev,struct pbuf * p)159 rt_err_t rt_usbd_rndis_eth_tx(rt_device_t dev, struct pbuf *p)
160 {
161 return usbd_rndis_eth_tx(p);
162 }
163
usbd_rndis_data_recv_done(void)164 void usbd_rndis_data_recv_done(void)
165 {
166 eth_device_ready(&rndis_dev);
167 }
168
rt_usbd_rndis_init(void)169 void rt_usbd_rndis_init(void)
170 {
171 rndis_dev.parent.control = rt_usbd_rndis_control;
172 rndis_dev.eth_rx = rt_usbd_rndis_eth_rx;
173 rndis_dev.eth_tx = rt_usbd_rndis_eth_tx;
174
175 eth_device_init(&rndis_dev, "u0");
176
177 eth_device_linkchange(&rndis_dev, RT_FALSE);
178 }
179 #else
180 #include "netif/etharp.h"
181 #include "lwip/init.h"
182 #include "lwip/netif.h"
183 #include "lwip/pbuf.h"
184
185 const ip_addr_t ipaddr = IPADDR4_INIT_BYTES(IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
186 const ip_addr_t netmask = IPADDR4_INIT_BYTES(NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
187 const ip_addr_t gateway = IPADDR4_INIT_BYTES(GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
188
189 static struct netif rndis_netif; //network interface
190
191 /* Network interface name */
192 #define IFNAME0 'E'
193 #define IFNAME1 'X'
194
linkoutput_fn(struct netif * netif,struct pbuf * p)195 err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
196 {
197 static int ret;
198
199 ret = usbd_rndis_eth_tx(p);
200 if (ret == 0)
201 return ERR_OK;
202 else
203 return ERR_BUF;
204 }
205
rndisif_init(struct netif * netif)206 err_t rndisif_init(struct netif *netif)
207 {
208 LWIP_ASSERT("netif != NULL", (netif != NULL));
209
210 netif->mtu = 1500;
211 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
212 netif->state = NULL;
213 netif->name[0] = IFNAME0;
214 netif->name[1] = IFNAME1;
215 netif->output = etharp_output;
216 netif->linkoutput = linkoutput_fn;
217 return ERR_OK;
218 }
219
rndisif_input(struct netif * netif)220 err_t rndisif_input(struct netif *netif)
221 {
222 static err_t err;
223 static struct pbuf *p;
224
225 p = usbd_rndis_eth_rx();
226 if (p != NULL) {
227 err = netif->input(p, netif);
228 if (err != ERR_OK) {
229 pbuf_free(p);
230 }
231 } else {
232 return ERR_BUF;
233 }
234 return err;
235 }
236
rndis_lwip_init(void)237 void rndis_lwip_init(void)
238 {
239 struct netif *netif = &rndis_netif;
240
241 lwip_init();
242
243 netif->hwaddr_len = 6;
244 memcpy(netif->hwaddr, mac, 6);
245
246 netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, rndisif_init, netif_input);
247 netif_set_default(netif);
248 while (!netif_is_up(netif)) {
249 }
250
251 // while (dhserv_init(&dhcp_config)) {}
252
253 // while (dnserv_init(&ipaddr, PORT_DNS, dns_query_proc)) {}
254 }
255
usbd_rndis_data_recv_done(void)256 void usbd_rndis_data_recv_done(void)
257 {
258 }
259
rndis_input_poll(void)260 void rndis_input_poll(void)
261 {
262 rndisif_input(&rndis_netif);
263 }
264 #endif /* RT_USING_LWIP */
265
usbd_event_handler(uint8_t event)266 void usbd_event_handler(uint8_t event)
267 {
268 switch (event) {
269 case USBD_EVENT_RESET:
270 break;
271 case USBD_EVENT_CONNECTED:
272 break;
273 case USBD_EVENT_DISCONNECTED:
274 break;
275 case USBD_EVENT_RESUME:
276 break;
277 case USBD_EVENT_SUSPEND:
278 break;
279 case USBD_EVENT_CONFIGURED:
280 #ifdef RT_USING_LWIP
281 eth_device_linkchange(&rndis_dev, RT_TRUE);
282 #endif
283 break;
284 case USBD_EVENT_SET_REMOTE_WAKEUP:
285 break;
286 case USBD_EVENT_CLR_REMOTE_WAKEUP:
287 break;
288
289 default:
290 break;
291 }
292 }
293
294 struct usbd_interface intf0;
295 struct usbd_interface intf1;
296
cdc_rndis_init(void)297 void cdc_rndis_init(void)
298 {
299 #ifdef RT_USING_LWIP
300 rt_usbd_rndis_init();
301 #else
302 rndis_lwip_init();
303 #endif
304 usbd_desc_register(cdc_descriptor);
305 usbd_add_interface(usbd_rndis_init_intf(&intf0, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP, mac));
306 usbd_add_interface(usbd_rndis_init_intf(&intf1, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP, mac));
307 usbd_initialize();
308 }
309