1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author            Notes
8  * 2017-11-19     ZYH               first version
9  * 2019-06-10     ZYH               fix hotplug
10  */
11 
12 #include <rtdevice.h>
13 #ifdef RT_USB_DEVICE_ECM
14 #include "cdc.h"
15 
16 #define DBG_LEVEL           DBG_WARNING
17 #define DBG_SECTION_NAME    "ECM"
18 #include <rtdbg.h>
19 
20 /* RT-Thread LWIP ethernet interface */
21 #include <netif/ethernetif.h>
22 
23 #ifndef USB_ETH_MTU
24 #define USB_ETH_MTU     1514
25 #endif
26 #define MAX_ADDR_LEN    6
27 #define ECM_INTF_STR_INDEX 10
28 
29 struct rt_ecm_eth
30 {
31     /* inherit from ethernet device */
32     struct eth_device       parent;
33     struct ufunction *      func;
34     struct cdc_eps          eps;
35     /* interface address info */
36     rt_uint8_t              host_addr[MAX_ADDR_LEN];
37     rt_uint8_t              dev_addr[MAX_ADDR_LEN];
38 
39     rt_align(4)
40     rt_uint8_t              rx_pool[512];
41     rt_align(4)
42     rt_size_t               rx_size;
43     rt_align(4)
44     rt_size_t               rx_offset;
45     rt_align(4)
46     char                    rx_buffer[USB_ETH_MTU];
47     char                    tx_buffer[USB_ETH_MTU];
48 
49     struct rt_semaphore     tx_buffer_free;
50 
51 };
52 typedef struct rt_ecm_eth * rt_ecm_eth_t;
53 
54 rt_align(4)
55 static struct udevice_descriptor _dev_desc =
56 {
57     USB_DESC_LENGTH_DEVICE,     /* bLength */
58     USB_DESC_TYPE_DEVICE,       /* type */
59     USB_BCD_VERSION,            /* bcdUSB */
60     USB_CLASS_CDC,              /* bDeviceClass */
61     USB_CDC_SUBCLASS_ETH,       /* bDeviceSubClass */
62     USB_CDC_PROTOCOL_NONE,      /* bDeviceProtocol */
63     0x40,                       /* bMaxPacketSize0 */
64     _VENDOR_ID,                 /* idVendor */
65     _PRODUCT_ID,                /* idProduct */
66     USB_BCD_DEVICE,             /* bcdDevice */
67     USB_STRING_MANU_INDEX,      /* iManufacturer */
68     USB_STRING_PRODUCT_INDEX,   /* iProduct */
69     USB_STRING_SERIAL_INDEX,    /* iSerialNumber */
70     USB_DYNAMIC                 /* bNumConfigurations */
71 };
72 
73 /* communcation interface descriptor */
74 rt_align(4)
75 const static struct ucdc_eth_descriptor _comm_desc =
76 {
77 #ifdef RT_USB_DEVICE_COMPOSITE
78     /* Interface Association Descriptor */
79     {
80         USB_DESC_LENGTH_IAD,
81         USB_DESC_TYPE_IAD,
82         USB_DYNAMIC,
83         0x02,
84         USB_CDC_CLASS_COMM,
85         USB_CDC_SUBCLASS_ETH,
86         USB_CDC_PROTOCOL_NONE,
87         0x00,
88     },
89 #endif
90     /* Interface Descriptor */
91     {
92         USB_DESC_LENGTH_INTERFACE,
93         USB_DESC_TYPE_INTERFACE,
94         USB_DYNAMIC,
95         0x00,
96         0x01,
97         USB_CDC_CLASS_COMM,
98         USB_CDC_SUBCLASS_ETH,
99         USB_CDC_PROTOCOL_NONE,
100 #ifdef RT_USB_DEVICE_COMPOSITE
101         ECM_INTF_STR_INDEX,
102 #else
103         0x00,
104 #endif
105     },
106     /* Header Functional Descriptor */
107     {
108         sizeof(struct ucdc_header_descriptor),
109         USB_CDC_CS_INTERFACE,
110         USB_CDC_SCS_HEADER,
111         0x0110,
112     },
113     /* Union Functional Descriptor */
114     {
115         sizeof(struct ucdc_union_descriptor),
116         USB_CDC_CS_INTERFACE,
117         USB_CDC_SCS_UNION,
118         USB_DYNAMIC,
119         USB_DYNAMIC,
120     },
121     /* Abstract Control Management Functional Descriptor */
122     {
123         sizeof(struct ucdc_enet_descriptor),
124         USB_CDC_CS_INTERFACE,
125         USB_CDC_SCS_ETH,
126         USB_STRING_SERIAL_INDEX,
127         {0,0,0,0},
128         USB_ETH_MTU,
129         0x00,
130         0x00,
131     },
132     /* Endpoint Descriptor */
133     {
134         USB_DESC_LENGTH_ENDPOINT,
135         USB_DESC_TYPE_ENDPOINT,
136         USB_DIR_IN | USB_DYNAMIC,
137         USB_EP_ATTR_INT,
138         0x08,
139         0xFF,
140     },
141 };
142 
143 /* data interface descriptor */
144 rt_align(4)
145 const static struct ucdc_data_descriptor _data_desc =
146 {
147     /* interface descriptor */
148     {
149         USB_DESC_LENGTH_INTERFACE,
150         USB_DESC_TYPE_INTERFACE,
151         USB_DYNAMIC,
152         0x00,
153         0x02,
154         USB_CDC_CLASS_DATA,
155         USB_CDC_SUBCLASS_ETH,
156         0x00,
157         0x00,
158     },
159     /* endpoint, bulk out */
160     {
161         USB_DESC_LENGTH_ENDPOINT,
162         USB_DESC_TYPE_ENDPOINT,
163         USB_DIR_OUT | USB_DYNAMIC,
164         USB_EP_ATTR_BULK,
165         USB_DYNAMIC,
166         0x00,
167     },
168     /* endpoint, bulk in */
169     {
170         USB_DESC_LENGTH_ENDPOINT,
171         USB_DESC_TYPE_ENDPOINT,
172         USB_DYNAMIC | USB_DIR_IN,
173         USB_EP_ATTR_BULK,
174         USB_DYNAMIC,
175         0x00,
176     },
177 };
178 
179 rt_align(4)
180 const static char* _ustring[] =
181 {
182     "Language",                 /* LANGID */
183     "RT-Thread Team.",          /* MANU */
184     "RT-Thread ECM device",     /* PRODUCT */
185     "3497F694ECAB",             /* SERIAL (MAC)*/
186     "Configuration",            /* CONFIG */
187     "Interface",                /* INTERFACE */
188 };
189 
190 rt_align(4)
191 //FS and HS needed
192 static struct usb_qualifier_descriptor dev_qualifier =
193 {
194     sizeof(dev_qualifier),          //bLength
195     USB_DESC_TYPE_DEVICEQUALIFIER,  //bDescriptorType
196     0x0200,                         //bcdUSB
197     USB_CLASS_CDC,                  //bDeviceClass
198     USB_CDC_SUBCLASS_ETH,           //bDeviceSubClass
199     USB_CDC_PROTOCOL_NONE,          //bDeviceProtocol
200     64,                             //bMaxPacketSize0
201     0x01,                           //bNumConfigurations
202     0,
203 };
204 
_cdc_send_notifi(ufunction_t func,ucdc_notification_code_t notifi,rt_uint16_t wValue,rt_uint16_t wLength)205 static rt_err_t _cdc_send_notifi(ufunction_t func,ucdc_notification_code_t notifi,rt_uint16_t wValue,rt_uint16_t wLength)
206 {
207     static struct ucdc_management_element_notifications _notifi;
208     cdc_eps_t eps;
209     RT_ASSERT(func!=RT_NULL)
210     eps = &((rt_ecm_eth_t)func->user_data)->eps;
211     _notifi.bmRequestType = 0xA1;
212     _notifi.bNotificatinCode = notifi;
213     _notifi.wValue = wValue;
214     _notifi.wLength = wLength;
215 
216     eps->ep_cmd->request.buffer = (void *)&_notifi;
217     eps->ep_cmd->request.size = 8;
218     eps->ep_cmd->request.req_type = UIO_REQUEST_WRITE;
219     rt_usbd_io_request(func->device, eps->ep_cmd, &eps->ep_cmd->request);
220     return RT_EOK;
221 }
222 
223 
_ecm_set_eth_packet_filter(ufunction_t func,ureq_t setup)224 static rt_err_t _ecm_set_eth_packet_filter(ufunction_t func, ureq_t setup)
225 {
226     rt_ecm_eth_t _ecm_eth = (rt_ecm_eth_t)func->user_data;
227     dcd_ep0_send_status(func->device->dcd);
228 
229     /* send link up. */
230     eth_device_linkchange(&_ecm_eth->parent, RT_TRUE);
231     _cdc_send_notifi(func, UCDC_NOTIFI_NETWORK_CONNECTION, 1, 0);
232 
233 #ifdef LWIP_USING_DHCPD
234     extern void dhcpd_start(const char *netif_name);
235     dhcpd_start("u0");
236 #endif
237 
238     return RT_EOK;
239 }
240 /**
241  * This function will handle rndis interface request.
242  *
243  * @param device the usb device object.
244  * @param setup the setup request.
245  *
246  * @return RT_EOK on successful.
247  */
_interface_handler(ufunction_t func,ureq_t setup)248 static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
249 {
250     RT_ASSERT(func != RT_NULL);
251     RT_ASSERT(setup != RT_NULL);
252 
253     switch(setup->bRequest)
254     {
255     case CDC_SET_ETH_PACKET_FILTER:
256         LOG_D("CDC_SET_ETH_PACKET_FILTER");
257         _ecm_set_eth_packet_filter(func, setup);
258         break;
259     default:
260         LOG_E("Unknow setup->bRequest: 0x%02X", setup->bRequest);
261         break;
262     }
263     return RT_EOK;
264 }
265 
266 /**
267  * This function will handle rndis bulk in endpoint request.
268  *
269  * @param device the usb device object.
270  * @param size request size.
271  *
272  * @return RT_EOK.
273  */
274 
_ep_in_handler(ufunction_t func,rt_size_t size)275 static rt_err_t _ep_in_handler(ufunction_t func, rt_size_t size)
276 {
277     rt_ecm_eth_t ecm_device = (rt_ecm_eth_t)func->user_data;
278     rt_sem_release(&ecm_device->tx_buffer_free);
279     return RT_EOK;
280 }
281 
282 /**
283  * This function will handle RNDIS bulk out endpoint request.
284  *
285  * @param device the usb device object.
286  * @param size request size.
287  *
288  * @return RT_EOK.
289  */
_ep_out_handler(ufunction_t func,rt_size_t size)290 static rt_err_t _ep_out_handler(ufunction_t func, rt_size_t size)
291 {
292     rt_ecm_eth_t ecm_device = (rt_ecm_eth_t)func->user_data;
293     rt_memcpy((void *)(ecm_device->rx_buffer + ecm_device->rx_offset),ecm_device->rx_pool,size);
294     ecm_device->rx_offset += size;
295     if(size < EP_MAXPACKET(ecm_device->eps.ep_out))
296     {
297         ecm_device->rx_size = ecm_device->rx_offset;
298         ecm_device->rx_offset = 0;
299         eth_device_ready(&ecm_device->parent);
300 
301     }else
302     {
303         ecm_device->eps.ep_out->request.buffer = ecm_device->eps.ep_out->buffer;
304         ecm_device->eps.ep_out->request.size = EP_MAXPACKET(ecm_device->eps.ep_out);
305         ecm_device->eps.ep_out->request.req_type = UIO_REQUEST_READ_BEST;
306         rt_usbd_io_request(ecm_device->func->device, ecm_device->eps.ep_out, &ecm_device->eps.ep_out->request);
307     }
308 
309 
310     return RT_EOK;
311 }
rt_ecm_eth_init(rt_device_t dev)312 static rt_err_t rt_ecm_eth_init(rt_device_t dev)
313 {
314     return RT_EOK;
315 }
316 
rt_ecm_eth_open(rt_device_t dev,rt_uint16_t oflag)317 static rt_err_t rt_ecm_eth_open(rt_device_t dev, rt_uint16_t oflag)
318 {
319     return RT_EOK;
320 }
321 
rt_ecm_eth_close(rt_device_t dev)322 static rt_err_t rt_ecm_eth_close(rt_device_t dev)
323 {
324     return RT_EOK;
325 }
326 
rt_ecm_eth_read(rt_device_t dev,rt_off_t pos,void * buffer,rt_size_t size)327 static rt_ssize_t rt_ecm_eth_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
328 {
329     rt_set_errno(-RT_ENOSYS);
330     return 0;
331 }
332 
rt_ecm_eth_write(rt_device_t dev,rt_off_t pos,const void * buffer,rt_size_t size)333 static rt_ssize_t rt_ecm_eth_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
334 {
335     rt_set_errno(-RT_ENOSYS);
336     return 0;
337 }
rt_ecm_eth_control(rt_device_t dev,int cmd,void * args)338 static rt_err_t rt_ecm_eth_control(rt_device_t dev, int cmd, void *args)
339 {
340     rt_ecm_eth_t ecm_eth_dev = (rt_ecm_eth_t)dev;
341     switch(cmd)
342     {
343     case NIOCTL_GADDR:
344         /* get mac address */
345         if(args) rt_memcpy(args, ecm_eth_dev->dev_addr, MAX_ADDR_LEN);
346         else return -RT_ERROR;
347         break;
348 
349     default :
350         break;
351     }
352 
353     return RT_EOK;
354 }
355 
356 #ifdef RT_USING_DEVICE_OPS
357 const static struct rt_device_ops ecm_device_ops =
358 {
359     rt_ecm_eth_init,
360     rt_ecm_eth_open,
361     rt_ecm_eth_close,
362     rt_ecm_eth_read,
363     rt_ecm_eth_write,
364     rt_ecm_eth_control
365 };
366 #endif
367 
rt_ecm_eth_rx(rt_device_t dev)368 struct pbuf *rt_ecm_eth_rx(rt_device_t dev)
369 {
370     struct pbuf* p = RT_NULL;
371     rt_uint32_t offset = 0;
372     rt_ecm_eth_t ecm_eth_dev = (rt_ecm_eth_t)dev;
373     if(ecm_eth_dev->rx_size != 0)
374     {
375         /* allocate buffer */
376         p = pbuf_alloc(PBUF_RAW, ecm_eth_dev->rx_size, PBUF_RAM);
377         if (p != RT_NULL)
378         {
379             struct pbuf* q;
380 
381             for (q = p; q != RT_NULL; q= q->next)
382             {
383                 /* Copy the received frame into buffer from memory pointed by the current ETHERNET DMA Rx descriptor */
384                 rt_memcpy(q->payload,
385                         (rt_uint8_t *)((ecm_eth_dev->rx_buffer) + offset),
386                         q->len);
387                 offset += q->len;
388             }
389         }
390     }
391 
392     {
393         if(ecm_eth_dev->func->device->state == USB_STATE_CONFIGURED)
394         {
395             ecm_eth_dev->rx_size = 0;
396             ecm_eth_dev->rx_offset = 0;
397             ecm_eth_dev->eps.ep_out->request.buffer = ecm_eth_dev->eps.ep_out->buffer;
398             ecm_eth_dev->eps.ep_out->request.size = EP_MAXPACKET(ecm_eth_dev->eps.ep_out);
399             ecm_eth_dev->eps.ep_out->request.req_type = UIO_REQUEST_READ_BEST;
400             rt_usbd_io_request(ecm_eth_dev->func->device, ecm_eth_dev->eps.ep_out, &ecm_eth_dev->eps.ep_out->request);
401         }
402     }
403 
404     return p;
405 }
406 
rt_ecm_eth_tx(rt_device_t dev,struct pbuf * p)407 rt_err_t rt_ecm_eth_tx(rt_device_t dev, struct pbuf* p)
408 {
409     struct pbuf* q;
410     char * pbuffer;
411     rt_err_t result = RT_EOK;
412     rt_ecm_eth_t ecm_eth_dev = (rt_ecm_eth_t)dev;
413 
414     if(!ecm_eth_dev->parent.link_status)
415     {
416         LOG_D("linkdown, drop pkg");
417         return RT_EOK;
418     }
419 
420     if(p->tot_len > USB_ETH_MTU)
421     {
422         LOG_W("ECM MTU is:%d, but the send packet size is %d",
423                      USB_ETH_MTU, p->tot_len);
424         p->tot_len = USB_ETH_MTU;
425     }
426 
427     result = rt_sem_take(&ecm_eth_dev->tx_buffer_free, rt_tick_from_millisecond(1000));
428     if(result != RT_EOK)
429     {
430         LOG_W("wait for buffer free timeout");
431         /* if cost 1s to wait send done it said that connection is close . drop it */
432         rt_sem_release(&ecm_eth_dev->tx_buffer_free);
433         return result;
434     }
435 
436     pbuffer = (char *)&ecm_eth_dev->tx_buffer;
437     for (q = p; q != NULL; q = q->next)
438     {
439         rt_memcpy(pbuffer, q->payload, q->len);
440         pbuffer += q->len;
441     }
442 
443     {
444         if(ecm_eth_dev->func->device->state == USB_STATE_CONFIGURED)
445         {
446             ecm_eth_dev->eps.ep_in->request.buffer = (void *)&ecm_eth_dev->tx_buffer;
447             ecm_eth_dev->eps.ep_in->request.size = p->tot_len;
448             ecm_eth_dev->eps.ep_in->request.req_type = UIO_REQUEST_WRITE;
449             rt_usbd_io_request(ecm_eth_dev->func->device, ecm_eth_dev->eps.ep_in, &ecm_eth_dev->eps.ep_in->request);
450         }
451     }
452 
453     return result;
454 }
455 /**
456  * This function will handle RNDIS interrupt in endpoint request.
457  *
458  * @param device the usb device object.
459  * @param size request size.
460  *
461  * @return RT_EOK.
462  */
_ep_cmd_handler(ufunction_t func,rt_size_t size)463 static rt_err_t _ep_cmd_handler(ufunction_t func, rt_size_t size)
464 {
465     return RT_EOK;
466 }
467 
468 /**
469  * This function will run cdc class, it will be called on handle set configuration request.
470  *
471  * @param device the usb device object.
472  *
473  * @return RT_EOK on successful.
474  */
_function_enable(ufunction_t func)475 static rt_err_t _function_enable(ufunction_t func)
476 {
477     cdc_eps_t eps;
478     rt_ecm_eth_t ecm_device = (rt_ecm_eth_t)func->user_data;
479 
480     LOG_D("plugged in");
481 
482     eps = (cdc_eps_t)&ecm_device->eps;
483     eps->ep_out->buffer = ecm_device->rx_pool;
484 
485     /* reset eth rx tx */
486     ecm_device->rx_size = 0;
487     ecm_device->rx_offset = 0;
488 
489     eps->ep_out->request.buffer = (void *)eps->ep_out->buffer;
490     eps->ep_out->request.size = EP_MAXPACKET(eps->ep_out);
491     eps->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
492     rt_usbd_io_request(func->device, eps->ep_out, &eps->ep_out->request);
493     return RT_EOK;
494 }
495 
496 /**
497  * This function will stop cdc class, it will be called on handle set configuration request.
498  *
499  * @param device the usb device object.
500  *
501  * @return RT_EOK on successful.
502  */
_function_disable(ufunction_t func)503 static rt_err_t _function_disable(ufunction_t func)
504 {
505     LOG_D("plugged out");
506 
507     eth_device_linkchange(&((rt_ecm_eth_t)func->user_data)->parent, RT_FALSE);
508 
509     /* reset eth rx tx */
510     ((rt_ecm_eth_t)func->user_data)->rx_size = 0;
511     ((rt_ecm_eth_t)func->user_data)->rx_offset = 0;
512 
513     return RT_EOK;
514 }
515 
516 
517 static struct ufunction_ops ops =
518 {
519     _function_enable,
520     _function_disable,
521     RT_NULL,
522 };
523 
524 /**
525  * This function will configure cdc descriptor.
526  *
527  * @param comm the communication interface number.
528  * @param data the data interface number.
529  *
530  * @return RT_EOK on successful.
531  */
_cdc_descriptor_config(ucdc_comm_desc_t comm,rt_uint8_t cintf_nr,ucdc_data_desc_t data,rt_uint8_t dintf_nr,rt_uint8_t device_is_hs)532 static rt_err_t _cdc_descriptor_config(ucdc_comm_desc_t comm, rt_uint8_t cintf_nr, ucdc_data_desc_t data, rt_uint8_t dintf_nr, rt_uint8_t device_is_hs)
533 {
534     comm->call_mgmt_desc.data_interface = dintf_nr;
535     comm->union_desc.master_interface = cintf_nr;
536     comm->union_desc.slave_interface0 = dintf_nr;
537 #ifdef RT_USB_DEVICE_COMPOSITE
538     comm->iad_desc.bFirstInterface = cintf_nr;
539 #endif
540     data->ep_out_desc.wMaxPacketSize = device_is_hs ? 512 : 64;
541     data->ep_in_desc.wMaxPacketSize = device_is_hs ? 512 : 64;
542     return RT_EOK;
543 }
544 
545 
546 
547 /**
548  * This function will create a cdc ecm class instance.
549  *
550  * @param device the usb device object.
551  *
552  * @return RT_EOK on successful.
553  */
rt_usbd_function_ecm_create(udevice_t device)554 ufunction_t rt_usbd_function_ecm_create(udevice_t device)
555 {
556     ufunction_t cdc;
557     rt_ecm_eth_t _ecm_eth;
558     cdc_eps_t eps;
559     uintf_t intf_comm, intf_data;
560     ualtsetting_t comm_setting, data_setting;
561     ucdc_data_desc_t data_desc;
562     ucdc_eth_desc_t comm_desc;
563 
564     /* parameter check */
565     RT_ASSERT(device != RT_NULL);
566 
567     /* set usb device string description */
568 #ifdef RT_USB_DEVICE_COMPOSITE
569     rt_usbd_device_set_interface_string(device, ECM_INTF_STR_INDEX, _ustring[2]);
570 #else
571     rt_usbd_device_set_string(device, _ustring);
572 #endif
573     /* create a cdc class */
574     cdc = rt_usbd_function_new(device, &_dev_desc, &ops);
575     rt_usbd_device_set_qualifier(device, &dev_qualifier);
576     _ecm_eth= rt_malloc(sizeof(struct rt_ecm_eth));
577     RT_ASSERT(_ecm_eth != RT_NULL);
578     rt_memset(_ecm_eth, 0, sizeof(struct rt_ecm_eth));
579     cdc->user_data = _ecm_eth;
580 
581     _ecm_eth->func = cdc;
582     /* create a cdc class endpoints collection */
583     eps = &_ecm_eth->eps;
584     /* create a cdc communication interface and a cdc data interface */
585     intf_comm = rt_usbd_interface_new(device, _interface_handler);
586     intf_data = rt_usbd_interface_new(device, _interface_handler);
587 
588     /* create a communication alternate setting and a data alternate setting */
589     comm_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_eth_descriptor));
590     data_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_data_descriptor));
591 
592     /* config desc in alternate setting */
593     rt_usbd_altsetting_config_descriptor(comm_setting, &_comm_desc,
594                                          (rt_off_t)&((ucdc_eth_desc_t)0)->intf_desc);
595     rt_usbd_altsetting_config_descriptor(data_setting, &_data_desc, 0);
596     /* configure the cdc interface descriptor */
597     _cdc_descriptor_config(comm_setting->desc, intf_comm->intf_num, data_setting->desc, intf_data->intf_num, device->dcd->device_is_hs);
598 
599     /* create a command endpoint */
600     comm_desc = (ucdc_eth_desc_t)comm_setting->desc;
601     eps->ep_cmd = rt_usbd_endpoint_new(&comm_desc->ep_desc, _ep_cmd_handler);
602     /* add the command endpoint to the cdc communication interface */
603     rt_usbd_altsetting_add_endpoint(comm_setting, eps->ep_cmd);
604 
605     /* add the communication alternate setting to the communication interface,
606        then set default setting of the interface */
607     rt_usbd_interface_add_altsetting(intf_comm, comm_setting);
608     rt_usbd_set_altsetting(intf_comm, 0);
609     /* add the communication interface to the cdc class */
610     rt_usbd_function_add_interface(cdc, intf_comm);
611 
612     /* create a bulk in and a bulk out endpoint */
613     data_desc = (ucdc_data_desc_t)data_setting->desc;
614     eps->ep_out = rt_usbd_endpoint_new(&data_desc->ep_out_desc, _ep_out_handler);
615     eps->ep_in = rt_usbd_endpoint_new(&data_desc->ep_in_desc, _ep_in_handler);
616 
617     /* add the bulk out and bulk in endpoints to the data alternate setting */
618     rt_usbd_altsetting_add_endpoint(data_setting, eps->ep_in);
619     rt_usbd_altsetting_add_endpoint(data_setting, eps->ep_out);
620 
621     /* add the data alternate setting to the data interface
622             then set default setting of the interface */
623     rt_usbd_interface_add_altsetting(intf_data, data_setting);
624     rt_usbd_set_altsetting(intf_data, 0);
625 
626     /* add the cdc data interface to cdc class */
627     rt_usbd_function_add_interface(cdc, intf_data);
628 
629     rt_sem_init(&_ecm_eth->tx_buffer_free, "ue_tx", 1, RT_IPC_FLAG_FIFO);
630     /* OUI 00-00-00, only for test. */
631     _ecm_eth->dev_addr[0] = 0x34;
632     _ecm_eth->dev_addr[1] = 0x97;
633     _ecm_eth->dev_addr[2] = 0xF6;
634     /* generate random MAC. */
635     _ecm_eth->dev_addr[3] = 0x94;//*(const rt_uint8_t *)(0x1fff7a10);
636     _ecm_eth->dev_addr[4] = 0xEC;//*(const rt_uint8_t *)(0x1fff7a14);
637     _ecm_eth->dev_addr[5] = 0xAC;//(const rt_uint8_t *)(0x1fff7a18);
638     /* OUI 00-00-00, only for test. */
639     _ecm_eth->host_addr[0] = 0x34;
640     _ecm_eth->host_addr[1] = 0x97;
641     _ecm_eth->host_addr[2] = 0xF6;
642     /* generate random MAC. */
643     _ecm_eth->host_addr[3] = 0x94;//*(const rt_uint8_t *)(0x1fff7a10);
644     _ecm_eth->host_addr[4] = 0xEC;//*(const rt_uint8_t *)(0x1fff7a14);
645     _ecm_eth->host_addr[5] = 0xAB;//*(const rt_uint8_t *)(0x1fff7a18);
646 
647 #ifdef RT_USING_DEVICE_OPS
648     _ecm_eth->parent.parent.ops = &ecm_device_ops;
649 #else
650     _ecm_eth->parent.parent.init       = rt_ecm_eth_init;
651     _ecm_eth->parent.parent.open       = rt_ecm_eth_open;
652     _ecm_eth->parent.parent.close      = rt_ecm_eth_close;
653     _ecm_eth->parent.parent.read       = rt_ecm_eth_read;
654     _ecm_eth->parent.parent.write      = rt_ecm_eth_write;
655     _ecm_eth->parent.parent.control    = rt_ecm_eth_control;
656 #endif
657     _ecm_eth->parent.parent.user_data  = device;
658 
659     _ecm_eth->parent.eth_rx     = rt_ecm_eth_rx;
660     _ecm_eth->parent.eth_tx     = rt_ecm_eth_tx;
661     /* register eth device */
662     eth_device_init(&_ecm_eth->parent, "u0");
663 
664     /* send link up. */
665     eth_device_linkchange(&_ecm_eth->parent, RT_FALSE);
666 
667     return cdc;
668 }
669 
670 struct udclass ecm_class =
671 {
672     .rt_usbd_function_create = rt_usbd_function_ecm_create
673 };
674 
rt_usbd_ecm_class_register(void)675 int rt_usbd_ecm_class_register(void)
676 {
677     rt_usbd_class_register(&ecm_class);
678     return 0;
679 }
680 INIT_PREV_EXPORT(rt_usbd_ecm_class_register);
681 
682 #endif /* RT_USB_DEVICE_ECM */
683