ecm.c 18 KB

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