ecm.c 19 KB

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