ecm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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[64];
  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_CDC_BUFSIZE,
  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_CDC_BUFSIZE,
  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. static struct usb_qualifier_descriptor dev_qualifier =
  160. {
  161. sizeof(dev_qualifier),
  162. USB_DESC_TYPE_DEVICEQUALIFIER,
  163. 0x0200,
  164. USB_CLASS_CDC,
  165. 0x00,
  166. 64,
  167. 0x01,
  168. 0,
  169. };
  170. static rt_err_t _cdc_send_notifi(ufunction_t func,ucdc_notification_code_t notifi,rt_uint16_t wValue,rt_uint16_t wLength)
  171. {
  172. static struct ucdc_management_element_notifications _notifi;
  173. cdc_eps_t eps;
  174. RT_ASSERT(func!=RT_NULL)
  175. eps = &((rt_ecm_eth_t)func->user_data)->eps;
  176. _notifi.bmRequestType = 0xA1;
  177. _notifi.bNotificatinCode = notifi;
  178. _notifi.wValue = wValue;
  179. _notifi.wLength = wLength;
  180. eps->ep_cmd->request.buffer = (void *)&_notifi;
  181. eps->ep_cmd->request.size = 8;
  182. eps->ep_cmd->request.req_type = UIO_REQUEST_WRITE;
  183. rt_usbd_io_request(func->device, eps->ep_cmd, &eps->ep_cmd->request);
  184. return RT_EOK;
  185. }
  186. static rt_err_t _ecm_set_eth_packet_filter(ufunction_t func, ureq_t setup)
  187. {
  188. rt_ecm_eth_t _ecm_eth = (rt_ecm_eth_t)func->user_data;
  189. dcd_ep0_send_status(func->device->dcd);
  190. /* send link up. */
  191. eth_device_linkchange(&_ecm_eth->parent, RT_TRUE);
  192. _cdc_send_notifi(func,UCDC_NOTIFI_NETWORK_CONNECTION,1,0);
  193. return RT_EOK;
  194. }
  195. /**
  196. * This function will handle rndis interface request.
  197. *
  198. * @param device the usb device object.
  199. * @param setup the setup request.
  200. *
  201. * @return RT_EOK on successful.
  202. */
  203. static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
  204. {
  205. RT_ASSERT(func != RT_NULL);
  206. RT_ASSERT(setup != RT_NULL);
  207. switch(setup->bRequest)
  208. {
  209. case CDC_SET_ETH_PACKET_FILTER:
  210. _ecm_set_eth_packet_filter(func, setup);
  211. break;
  212. default:
  213. rt_kprintf("setup->bRequest:0x%02X",setup->bRequest);
  214. break;
  215. }
  216. return RT_EOK;
  217. }
  218. /**
  219. * This function will handle rndis bulk in endpoint request.
  220. *
  221. * @param device the usb device object.
  222. * @param size request size.
  223. *
  224. * @return RT_EOK.
  225. */
  226. static rt_err_t _ep_in_handler(ufunction_t func, rt_size_t size)
  227. {
  228. rt_ecm_eth_t ecm_device = (rt_ecm_eth_t)func->user_data;
  229. rt_sem_release(&ecm_device->tx_buffer_free);
  230. return RT_EOK;
  231. }
  232. /**
  233. * This function will handle RNDIS bulk out endpoint request.
  234. *
  235. * @param device the usb device object.
  236. * @param size request size.
  237. *
  238. * @return RT_EOK.
  239. */
  240. static rt_err_t _ep_out_handler(ufunction_t func, rt_size_t size)
  241. {
  242. rt_ecm_eth_t ecm_device = (rt_ecm_eth_t)func->user_data;
  243. rt_memcpy((void *)(ecm_device->rx_buffer + ecm_device->rx_offset),ecm_device->rx_pool,size);
  244. ecm_device->rx_offset += size;
  245. if(size < EP_MAXPACKET(ecm_device->eps.ep_out))
  246. {
  247. ecm_device->rx_size = ecm_device->rx_offset;
  248. ecm_device->rx_offset = 0;
  249. eth_device_ready(&ecm_device->parent);
  250. }else
  251. {
  252. ecm_device->eps.ep_out->request.buffer = ecm_device->eps.ep_out->buffer;
  253. ecm_device->eps.ep_out->request.size = EP_MAXPACKET(ecm_device->eps.ep_out);
  254. ecm_device->eps.ep_out->request.req_type = UIO_REQUEST_READ_BEST;
  255. rt_usbd_io_request(ecm_device->func->device, ecm_device->eps.ep_out, &ecm_device->eps.ep_out->request);
  256. }
  257. return RT_EOK;
  258. }
  259. static rt_err_t rt_ecm_eth_init(rt_device_t dev)
  260. {
  261. return RT_EOK;
  262. }
  263. static rt_err_t rt_ecm_eth_open(rt_device_t dev, rt_uint16_t oflag)
  264. {
  265. return RT_EOK;
  266. }
  267. static rt_err_t rt_ecm_eth_close(rt_device_t dev)
  268. {
  269. return RT_EOK;
  270. }
  271. static rt_size_t rt_ecm_eth_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  272. {
  273. rt_set_errno(-RT_ENOSYS);
  274. return 0;
  275. }
  276. static rt_size_t rt_ecm_eth_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  277. {
  278. rt_set_errno(-RT_ENOSYS);
  279. return 0;
  280. }
  281. static rt_err_t rt_ecm_eth_control(rt_device_t dev, int cmd, void *args)
  282. {
  283. rt_ecm_eth_t ecm_eth_dev = (rt_ecm_eth_t)dev;
  284. switch(cmd)
  285. {
  286. case NIOCTL_GADDR:
  287. /* get mac address */
  288. if(args) rt_memcpy(args, ecm_eth_dev->dev_addr, MAX_ADDR_LEN);
  289. else return -RT_ERROR;
  290. break;
  291. default :
  292. break;
  293. }
  294. return RT_EOK;
  295. }
  296. struct pbuf *rt_ecm_eth_rx(rt_device_t dev)
  297. {
  298. struct pbuf* p = RT_NULL;
  299. rt_uint32_t offset = 0;
  300. rt_ecm_eth_t ecm_eth_dev = (rt_ecm_eth_t)dev;
  301. if(ecm_eth_dev->rx_size != 0)
  302. {
  303. /* allocate buffer */
  304. p = pbuf_alloc(PBUF_RAW, ecm_eth_dev->rx_size, PBUF_RAM);
  305. if (p != RT_NULL)
  306. {
  307. struct pbuf* q;
  308. for (q = p; q != RT_NULL; q= q->next)
  309. {
  310. /* Copy the received frame into buffer from memory pointed by the current ETHERNET DMA Rx descriptor */
  311. rt_memcpy(q->payload,
  312. (rt_uint8_t *)((ecm_eth_dev->rx_buffer) + offset),
  313. q->len);
  314. offset += q->len;
  315. }
  316. }
  317. }
  318. {
  319. if(ecm_eth_dev->func->device->state == USB_STATE_CONFIGURED)
  320. {
  321. ecm_eth_dev->rx_size = 0;
  322. ecm_eth_dev->rx_offset = 0;
  323. ecm_eth_dev->eps.ep_out->request.buffer = ecm_eth_dev->eps.ep_out->buffer;
  324. ecm_eth_dev->eps.ep_out->request.size = EP_MAXPACKET(ecm_eth_dev->eps.ep_out);
  325. ecm_eth_dev->eps.ep_out->request.req_type = UIO_REQUEST_READ_BEST;
  326. rt_usbd_io_request(ecm_eth_dev->func->device, ecm_eth_dev->eps.ep_out, &ecm_eth_dev->eps.ep_out->request);
  327. }
  328. }
  329. return p;
  330. }
  331. rt_err_t rt_ecm_eth_tx(rt_device_t dev, struct pbuf* p)
  332. {
  333. struct pbuf* q;
  334. char * pbuffer;
  335. rt_err_t result = RT_EOK;
  336. rt_ecm_eth_t ecm_eth_dev = (rt_ecm_eth_t)dev;
  337. if(!ecm_eth_dev->parent.link_status)
  338. {
  339. ECM_PRINTF("linkdown, drop pkg\r\n");
  340. return RT_EOK;
  341. }
  342. // RT_ASSERT(p->tot_len < USB_ETH_MTU);
  343. if(p->tot_len > USB_ETH_MTU)
  344. {
  345. ECM_PRINTF("RNDIS MTU is:%d, but the send packet size is %d\r\n",
  346. USB_ETH_MTU, p->tot_len);
  347. p->tot_len = USB_ETH_MTU;
  348. }
  349. result = rt_sem_take(&ecm_eth_dev->tx_buffer_free, RT_WAITING_FOREVER);
  350. if(result != RT_EOK)
  351. {
  352. return result;
  353. }
  354. pbuffer = (char *)&ecm_eth_dev->tx_buffer;
  355. for (q = p; q != NULL; q = q->next)
  356. {
  357. rt_memcpy(pbuffer, q->payload, q->len);
  358. pbuffer += q->len;
  359. }
  360. {
  361. if(ecm_eth_dev->func->device->state == USB_STATE_CONFIGURED)
  362. {
  363. ecm_eth_dev->eps.ep_in->request.buffer = (void *)&ecm_eth_dev->tx_buffer;
  364. ecm_eth_dev->eps.ep_in->request.size = p->tot_len;
  365. ecm_eth_dev->eps.ep_in->request.req_type = UIO_REQUEST_WRITE;
  366. rt_usbd_io_request(ecm_eth_dev->func->device, ecm_eth_dev->eps.ep_in, &ecm_eth_dev->eps.ep_in->request);
  367. }
  368. }
  369. return result;
  370. }
  371. /**
  372. * This function will handle RNDIS interrupt in endpoint request.
  373. *
  374. * @param device the usb device object.
  375. * @param size request size.
  376. *
  377. * @return RT_EOK.
  378. */
  379. static rt_err_t _ep_cmd_handler(ufunction_t func, rt_size_t size)
  380. {
  381. return RT_EOK;
  382. }
  383. /**
  384. * This function will run cdc class, it will be called on handle set configuration request.
  385. *
  386. * @param device the usb device object.
  387. *
  388. * @return RT_EOK on successful.
  389. */
  390. static rt_err_t _function_enable(ufunction_t func)
  391. {
  392. cdc_eps_t eps;
  393. rt_ecm_eth_t ecm_device = (rt_ecm_eth_t)func->user_data;
  394. eps = (cdc_eps_t)&ecm_device->eps;
  395. eps->ep_out->buffer = ecm_device->rx_pool;
  396. ecm_device->rx_size = 0;
  397. ecm_device->rx_offset = 0;
  398. eps->ep_out->request.buffer = (void *)eps->ep_out->buffer;
  399. eps->ep_out->request.size = EP_MAXPACKET(eps->ep_out);
  400. eps->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
  401. rt_usbd_io_request(func->device, eps->ep_out, &eps->ep_out->request);
  402. return RT_EOK;
  403. }
  404. /**
  405. * This function will stop cdc class, it will be called on handle set configuration request.
  406. *
  407. * @param device the usb device object.
  408. *
  409. * @return RT_EOK on successful.
  410. */
  411. static rt_err_t _function_disable(ufunction_t func)
  412. {
  413. eth_device_linkchange(&((rt_ecm_eth_t)func->user_data)->parent, RT_FALSE);
  414. return RT_EOK;
  415. }
  416. static struct ufunction_ops ops =
  417. {
  418. _function_enable,
  419. _function_disable,
  420. RT_NULL,
  421. };
  422. /**
  423. * This function will configure cdc descriptor.
  424. *
  425. * @param comm the communication interface number.
  426. * @param data the data interface number.
  427. *
  428. * @return RT_EOK on successful.
  429. */
  430. 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)
  431. {
  432. comm->call_mgmt_desc.data_interface = dintf_nr;
  433. comm->union_desc.master_interface = cintf_nr;
  434. comm->union_desc.slave_interface0 = dintf_nr;
  435. #ifdef RT_USB_DEVICE_COMPOSITE
  436. comm->iad_desc.bFirstInterface = cintf_nr;
  437. #endif
  438. return RT_EOK;
  439. }
  440. /**
  441. * This function will create a cdc ecm class instance.
  442. *
  443. * @param device the usb device object.
  444. *
  445. * @return RT_EOK on successful.
  446. */
  447. ufunction_t rt_usbd_function_ecm_create(udevice_t device)
  448. {
  449. ufunction_t cdc;
  450. rt_ecm_eth_t _ecm_eth;
  451. cdc_eps_t eps;
  452. uintf_t intf_comm, intf_data;
  453. ualtsetting_t comm_setting, data_setting;
  454. ucdc_data_desc_t data_desc;
  455. ucdc_eth_desc_t comm_desc;
  456. /* parameter check */
  457. RT_ASSERT(device != RT_NULL);
  458. /* set usb device string description */
  459. rt_usbd_device_set_string(device, _ustring);
  460. /* create a cdc class */
  461. cdc = rt_usbd_function_new(device, &_dev_desc, &ops);
  462. rt_usbd_device_set_qualifier(device, &dev_qualifier);
  463. _ecm_eth= rt_malloc(sizeof(struct rt_ecm_eth));
  464. rt_memset(_ecm_eth, 0, sizeof(struct rt_ecm_eth));
  465. cdc->user_data = _ecm_eth;
  466. _ecm_eth->func = cdc;
  467. /* create a cdc class endpoints collection */
  468. eps = &_ecm_eth->eps;
  469. /* create a cdc communication interface and a cdc data interface */
  470. intf_comm = rt_usbd_interface_new(device, _interface_handler);
  471. intf_data = rt_usbd_interface_new(device, _interface_handler);
  472. /* create a communication alternate setting and a data alternate setting */
  473. comm_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_eth_descriptor));
  474. data_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_data_descriptor));
  475. /* config desc in alternate setting */
  476. rt_usbd_altsetting_config_descriptor(comm_setting, &_comm_desc,
  477. (rt_off_t)&((ucdc_eth_desc_t)0)->intf_desc);
  478. rt_usbd_altsetting_config_descriptor(data_setting, &_data_desc, 0);
  479. /* configure the cdc interface descriptor */
  480. _cdc_descriptor_config(comm_setting->desc, intf_comm->intf_num, data_setting->desc, intf_data->intf_num);
  481. /* create a command endpoint */
  482. comm_desc = (ucdc_eth_desc_t)comm_setting->desc;
  483. eps->ep_cmd = rt_usbd_endpoint_new(&comm_desc->ep_desc, _ep_cmd_handler);
  484. /* add the command endpoint to the cdc communication interface */
  485. rt_usbd_altsetting_add_endpoint(comm_setting, eps->ep_cmd);
  486. /* add the communication alternate setting to the communication interface,
  487. then set default setting of the interface */
  488. rt_usbd_interface_add_altsetting(intf_comm, comm_setting);
  489. rt_usbd_set_altsetting(intf_comm, 0);
  490. /* add the communication interface to the cdc class */
  491. rt_usbd_function_add_interface(cdc, intf_comm);
  492. /* create a bulk in and a bulk out endpoint */
  493. data_desc = (ucdc_data_desc_t)data_setting->desc;
  494. eps->ep_out = rt_usbd_endpoint_new(&data_desc->ep_out_desc, _ep_out_handler);
  495. eps->ep_in = rt_usbd_endpoint_new(&data_desc->ep_in_desc, _ep_in_handler);
  496. /* add the bulk out and bulk in endpoints to the data alternate setting */
  497. rt_usbd_altsetting_add_endpoint(data_setting, eps->ep_in);
  498. rt_usbd_altsetting_add_endpoint(data_setting, eps->ep_out);
  499. /* add the data alternate setting to the data interface
  500. then set default setting of the interface */
  501. rt_usbd_interface_add_altsetting(intf_data, data_setting);
  502. rt_usbd_set_altsetting(intf_data, 0);
  503. /* add the cdc data interface to cdc class */
  504. rt_usbd_function_add_interface(cdc, intf_data);
  505. rt_sem_init(&_ecm_eth->tx_buffer_free, "ue_tx", 1, RT_IPC_FLAG_FIFO);
  506. /* OUI 00-00-00, only for test. */
  507. _ecm_eth->dev_addr[0] = 0x34;
  508. _ecm_eth->dev_addr[1] = 0x97;
  509. _ecm_eth->dev_addr[2] = 0xF6;
  510. /* generate random MAC. */
  511. _ecm_eth->dev_addr[3] = 0x94;//*(const rt_uint8_t *)(0x1fff7a10);
  512. _ecm_eth->dev_addr[4] = 0xEC;//*(const rt_uint8_t *)(0x1fff7a14);
  513. _ecm_eth->dev_addr[5] = 0xAC;//(const rt_uint8_t *)(0x1fff7a18);
  514. /* OUI 00-00-00, only for test. */
  515. _ecm_eth->host_addr[0] = 0x34;
  516. _ecm_eth->host_addr[1] = 0x97;
  517. _ecm_eth->host_addr[2] = 0xF6;
  518. /* generate random MAC. */
  519. _ecm_eth->host_addr[3] = 0x94;//*(const rt_uint8_t *)(0x1fff7a10);
  520. _ecm_eth->host_addr[4] = 0xEC;//*(const rt_uint8_t *)(0x1fff7a14);
  521. _ecm_eth->host_addr[5] = 0xAB;//*(const rt_uint8_t *)(0x1fff7a18);
  522. _ecm_eth->parent.parent.init = rt_ecm_eth_init;
  523. _ecm_eth->parent.parent.open = rt_ecm_eth_open;
  524. _ecm_eth->parent.parent.close = rt_ecm_eth_close;
  525. _ecm_eth->parent.parent.read = rt_ecm_eth_read;
  526. _ecm_eth->parent.parent.write = rt_ecm_eth_write;
  527. _ecm_eth->parent.parent.control = rt_ecm_eth_control;
  528. _ecm_eth->parent.parent.user_data = device;
  529. _ecm_eth->parent.eth_rx = rt_ecm_eth_rx;
  530. _ecm_eth->parent.eth_tx = rt_ecm_eth_tx;
  531. /* register eth device */
  532. eth_device_init(&_ecm_eth->parent, "u0");
  533. /* send link up. */
  534. eth_device_linkchange(&_ecm_eth->parent, RT_FALSE);
  535. return cdc;
  536. }