1
0

ecm.c 19 KB

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