ecm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. #ifdef RT_USING_DEVICE_OPS
  299. const static struct rt_device_ops ecm_device_ops =
  300. {
  301. rt_ecm_eth_init,
  302. rt_ecm_eth_open,
  303. rt_ecm_eth_close,
  304. rt_ecm_eth_read,
  305. rt_ecm_eth_write,
  306. rt_ecm_eth_control
  307. };
  308. #endif
  309. struct pbuf *rt_ecm_eth_rx(rt_device_t dev)
  310. {
  311. struct pbuf* p = RT_NULL;
  312. rt_uint32_t offset = 0;
  313. rt_ecm_eth_t ecm_eth_dev = (rt_ecm_eth_t)dev;
  314. if(ecm_eth_dev->rx_size != 0)
  315. {
  316. /* allocate buffer */
  317. p = pbuf_alloc(PBUF_RAW, ecm_eth_dev->rx_size, PBUF_RAM);
  318. if (p != RT_NULL)
  319. {
  320. struct pbuf* q;
  321. for (q = p; q != RT_NULL; q= q->next)
  322. {
  323. /* Copy the received frame into buffer from memory pointed by the current ETHERNET DMA Rx descriptor */
  324. rt_memcpy(q->payload,
  325. (rt_uint8_t *)((ecm_eth_dev->rx_buffer) + offset),
  326. q->len);
  327. offset += q->len;
  328. }
  329. }
  330. }
  331. {
  332. if(ecm_eth_dev->func->device->state == USB_STATE_CONFIGURED)
  333. {
  334. ecm_eth_dev->rx_size = 0;
  335. ecm_eth_dev->rx_offset = 0;
  336. ecm_eth_dev->eps.ep_out->request.buffer = ecm_eth_dev->eps.ep_out->buffer;
  337. ecm_eth_dev->eps.ep_out->request.size = EP_MAXPACKET(ecm_eth_dev->eps.ep_out);
  338. ecm_eth_dev->eps.ep_out->request.req_type = UIO_REQUEST_READ_BEST;
  339. rt_usbd_io_request(ecm_eth_dev->func->device, ecm_eth_dev->eps.ep_out, &ecm_eth_dev->eps.ep_out->request);
  340. }
  341. }
  342. return p;
  343. }
  344. rt_err_t rt_ecm_eth_tx(rt_device_t dev, struct pbuf* p)
  345. {
  346. struct pbuf* q;
  347. char * pbuffer;
  348. rt_err_t result = RT_EOK;
  349. rt_ecm_eth_t ecm_eth_dev = (rt_ecm_eth_t)dev;
  350. if(!ecm_eth_dev->parent.link_status)
  351. {
  352. ECM_PRINTF("linkdown, drop pkg\r\n");
  353. return RT_EOK;
  354. }
  355. // RT_ASSERT(p->tot_len < USB_ETH_MTU);
  356. if(p->tot_len > USB_ETH_MTU)
  357. {
  358. ECM_PRINTF("RNDIS MTU is:%d, but the send packet size is %d\r\n",
  359. USB_ETH_MTU, p->tot_len);
  360. p->tot_len = USB_ETH_MTU;
  361. }
  362. result = rt_sem_take(&ecm_eth_dev->tx_buffer_free, RT_WAITING_FOREVER);
  363. if(result != RT_EOK)
  364. {
  365. return result;
  366. }
  367. pbuffer = (char *)&ecm_eth_dev->tx_buffer;
  368. for (q = p; q != NULL; q = q->next)
  369. {
  370. rt_memcpy(pbuffer, q->payload, q->len);
  371. pbuffer += q->len;
  372. }
  373. {
  374. if(ecm_eth_dev->func->device->state == USB_STATE_CONFIGURED)
  375. {
  376. ecm_eth_dev->eps.ep_in->request.buffer = (void *)&ecm_eth_dev->tx_buffer;
  377. ecm_eth_dev->eps.ep_in->request.size = p->tot_len;
  378. ecm_eth_dev->eps.ep_in->request.req_type = UIO_REQUEST_WRITE;
  379. rt_usbd_io_request(ecm_eth_dev->func->device, ecm_eth_dev->eps.ep_in, &ecm_eth_dev->eps.ep_in->request);
  380. }
  381. }
  382. return result;
  383. }
  384. /**
  385. * This function will handle RNDIS interrupt in endpoint request.
  386. *
  387. * @param device the usb device object.
  388. * @param size request size.
  389. *
  390. * @return RT_EOK.
  391. */
  392. static rt_err_t _ep_cmd_handler(ufunction_t func, rt_size_t size)
  393. {
  394. return RT_EOK;
  395. }
  396. /**
  397. * This function will run cdc class, it will be called on handle set configuration request.
  398. *
  399. * @param device the usb device object.
  400. *
  401. * @return RT_EOK on successful.
  402. */
  403. static rt_err_t _function_enable(ufunction_t func)
  404. {
  405. cdc_eps_t eps;
  406. rt_ecm_eth_t ecm_device = (rt_ecm_eth_t)func->user_data;
  407. eps = (cdc_eps_t)&ecm_device->eps;
  408. eps->ep_out->buffer = ecm_device->rx_pool;
  409. ecm_device->rx_size = 0;
  410. ecm_device->rx_offset = 0;
  411. eps->ep_out->request.buffer = (void *)eps->ep_out->buffer;
  412. eps->ep_out->request.size = EP_MAXPACKET(eps->ep_out);
  413. eps->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
  414. rt_usbd_io_request(func->device, eps->ep_out, &eps->ep_out->request);
  415. return RT_EOK;
  416. }
  417. /**
  418. * This function will stop cdc class, it will be called on handle set configuration request.
  419. *
  420. * @param device the usb device object.
  421. *
  422. * @return RT_EOK on successful.
  423. */
  424. static rt_err_t _function_disable(ufunction_t func)
  425. {
  426. eth_device_linkchange(&((rt_ecm_eth_t)func->user_data)->parent, RT_FALSE);
  427. return RT_EOK;
  428. }
  429. static struct ufunction_ops ops =
  430. {
  431. _function_enable,
  432. _function_disable,
  433. RT_NULL,
  434. };
  435. /**
  436. * This function will configure cdc descriptor.
  437. *
  438. * @param comm the communication interface number.
  439. * @param data the data interface number.
  440. *
  441. * @return RT_EOK on successful.
  442. */
  443. 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)
  444. {
  445. comm->call_mgmt_desc.data_interface = dintf_nr;
  446. comm->union_desc.master_interface = cintf_nr;
  447. comm->union_desc.slave_interface0 = dintf_nr;
  448. #ifdef RT_USB_DEVICE_COMPOSITE
  449. comm->iad_desc.bFirstInterface = cintf_nr;
  450. #endif
  451. data->ep_out_desc.wMaxPacketSize = device_is_hs ? 512 : 64;
  452. data->ep_in_desc.wMaxPacketSize = device_is_hs ? 512 : 64;
  453. return RT_EOK;
  454. }
  455. /**
  456. * This function will create a cdc ecm class instance.
  457. *
  458. * @param device the usb device object.
  459. *
  460. * @return RT_EOK on successful.
  461. */
  462. ufunction_t rt_usbd_function_ecm_create(udevice_t device)
  463. {
  464. ufunction_t cdc;
  465. rt_ecm_eth_t _ecm_eth;
  466. cdc_eps_t eps;
  467. uintf_t intf_comm, intf_data;
  468. ualtsetting_t comm_setting, data_setting;
  469. ucdc_data_desc_t data_desc;
  470. ucdc_eth_desc_t comm_desc;
  471. /* parameter check */
  472. RT_ASSERT(device != RT_NULL);
  473. /* set usb device string description */
  474. rt_usbd_device_set_string(device, _ustring);
  475. /* create a cdc class */
  476. cdc = rt_usbd_function_new(device, &_dev_desc, &ops);
  477. rt_usbd_device_set_qualifier(device, &dev_qualifier);
  478. _ecm_eth= rt_malloc(sizeof(struct rt_ecm_eth));
  479. rt_memset(_ecm_eth, 0, sizeof(struct rt_ecm_eth));
  480. cdc->user_data = _ecm_eth;
  481. _ecm_eth->func = cdc;
  482. /* create a cdc class endpoints collection */
  483. eps = &_ecm_eth->eps;
  484. /* create a cdc communication interface and a cdc data interface */
  485. intf_comm = rt_usbd_interface_new(device, _interface_handler);
  486. intf_data = rt_usbd_interface_new(device, _interface_handler);
  487. /* create a communication alternate setting and a data alternate setting */
  488. comm_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_eth_descriptor));
  489. data_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_data_descriptor));
  490. /* config desc in alternate setting */
  491. rt_usbd_altsetting_config_descriptor(comm_setting, &_comm_desc,
  492. (rt_off_t)&((ucdc_eth_desc_t)0)->intf_desc);
  493. rt_usbd_altsetting_config_descriptor(data_setting, &_data_desc, 0);
  494. /* configure the cdc interface descriptor */
  495. _cdc_descriptor_config(comm_setting->desc, intf_comm->intf_num, data_setting->desc, intf_data->intf_num, device->dcd->device_is_hs);
  496. /* create a command endpoint */
  497. comm_desc = (ucdc_eth_desc_t)comm_setting->desc;
  498. eps->ep_cmd = rt_usbd_endpoint_new(&comm_desc->ep_desc, _ep_cmd_handler);
  499. /* add the command endpoint to the cdc communication interface */
  500. rt_usbd_altsetting_add_endpoint(comm_setting, eps->ep_cmd);
  501. /* add the communication alternate setting to the communication interface,
  502. then set default setting of the interface */
  503. rt_usbd_interface_add_altsetting(intf_comm, comm_setting);
  504. rt_usbd_set_altsetting(intf_comm, 0);
  505. /* add the communication interface to the cdc class */
  506. rt_usbd_function_add_interface(cdc, intf_comm);
  507. /* create a bulk in and a bulk out endpoint */
  508. data_desc = (ucdc_data_desc_t)data_setting->desc;
  509. eps->ep_out = rt_usbd_endpoint_new(&data_desc->ep_out_desc, _ep_out_handler);
  510. eps->ep_in = rt_usbd_endpoint_new(&data_desc->ep_in_desc, _ep_in_handler);
  511. /* add the bulk out and bulk in endpoints to the data alternate setting */
  512. rt_usbd_altsetting_add_endpoint(data_setting, eps->ep_in);
  513. rt_usbd_altsetting_add_endpoint(data_setting, eps->ep_out);
  514. /* add the data alternate setting to the data interface
  515. then set default setting of the interface */
  516. rt_usbd_interface_add_altsetting(intf_data, data_setting);
  517. rt_usbd_set_altsetting(intf_data, 0);
  518. /* add the cdc data interface to cdc class */
  519. rt_usbd_function_add_interface(cdc, intf_data);
  520. rt_sem_init(&_ecm_eth->tx_buffer_free, "ue_tx", 1, RT_IPC_FLAG_FIFO);
  521. /* OUI 00-00-00, only for test. */
  522. _ecm_eth->dev_addr[0] = 0x34;
  523. _ecm_eth->dev_addr[1] = 0x97;
  524. _ecm_eth->dev_addr[2] = 0xF6;
  525. /* generate random MAC. */
  526. _ecm_eth->dev_addr[3] = 0x94;//*(const rt_uint8_t *)(0x1fff7a10);
  527. _ecm_eth->dev_addr[4] = 0xEC;//*(const rt_uint8_t *)(0x1fff7a14);
  528. _ecm_eth->dev_addr[5] = 0xAC;//(const rt_uint8_t *)(0x1fff7a18);
  529. /* OUI 00-00-00, only for test. */
  530. _ecm_eth->host_addr[0] = 0x34;
  531. _ecm_eth->host_addr[1] = 0x97;
  532. _ecm_eth->host_addr[2] = 0xF6;
  533. /* generate random MAC. */
  534. _ecm_eth->host_addr[3] = 0x94;//*(const rt_uint8_t *)(0x1fff7a10);
  535. _ecm_eth->host_addr[4] = 0xEC;//*(const rt_uint8_t *)(0x1fff7a14);
  536. _ecm_eth->host_addr[5] = 0xAB;//*(const rt_uint8_t *)(0x1fff7a18);
  537. _ecm_eth->parent.parent.init = rt_ecm_eth_init;
  538. _ecm_eth->parent.parent.open = rt_ecm_eth_open;
  539. _ecm_eth->parent.parent.close = rt_ecm_eth_close;
  540. _ecm_eth->parent.parent.read = rt_ecm_eth_read;
  541. _ecm_eth->parent.parent.write = rt_ecm_eth_write;
  542. _ecm_eth->parent.parent.control = rt_ecm_eth_control;
  543. _ecm_eth->parent.parent.user_data = device;
  544. _ecm_eth->parent.eth_rx = rt_ecm_eth_rx;
  545. _ecm_eth->parent.eth_tx = rt_ecm_eth_tx;
  546. /* register eth device */
  547. eth_device_init(&_ecm_eth->parent, "u0");
  548. /* send link up. */
  549. eth_device_linkchange(&_ecm_eth->parent, RT_FALSE);
  550. return cdc;
  551. }
  552. struct udclass ecm_class =
  553. {
  554. .rt_usbd_function_create = rt_usbd_function_ecm_create
  555. };
  556. int rt_usbd_ecm_class_register(void)
  557. {
  558. rt_usbd_class_register(&ecm_class);
  559. return 0;
  560. }
  561. INIT_PREV_EXPORT(rt_usbd_ecm_class_register);