ecm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. * 2019-06-10 ZYH fix hotplug
  10. */
  11. #include <rtdevice.h>
  12. #ifdef RT_USB_DEVICE_ECM
  13. #include "cdc.h"
  14. #define DBG_LEVEL DBG_WARNING
  15. #define DBG_SECTION_NAME "ECM"
  16. #include <rtdbg.h>
  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. #ifdef LWIP_USING_DHCPD
  210. extern void dhcpd_start(const char *netif_name);
  211. dhcpd_start("u0");
  212. #endif
  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. LOG_D("CDC_SET_ETH_PACKET_FILTER");
  231. _ecm_set_eth_packet_filter(func, setup);
  232. break;
  233. default:
  234. LOG_E("Unknow setup->bRequest: 0x%02X", setup->bRequest);
  235. break;
  236. }
  237. return RT_EOK;
  238. }
  239. /**
  240. * This function will handle rndis bulk in endpoint request.
  241. *
  242. * @param device the usb device object.
  243. * @param size request size.
  244. *
  245. * @return RT_EOK.
  246. */
  247. static rt_err_t _ep_in_handler(ufunction_t func, rt_size_t size)
  248. {
  249. rt_ecm_eth_t ecm_device = (rt_ecm_eth_t)func->user_data;
  250. rt_sem_release(&ecm_device->tx_buffer_free);
  251. return RT_EOK;
  252. }
  253. /**
  254. * This function will handle RNDIS bulk out endpoint request.
  255. *
  256. * @param device the usb device object.
  257. * @param size request size.
  258. *
  259. * @return RT_EOK.
  260. */
  261. static rt_err_t _ep_out_handler(ufunction_t func, rt_size_t size)
  262. {
  263. rt_ecm_eth_t ecm_device = (rt_ecm_eth_t)func->user_data;
  264. rt_memcpy((void *)(ecm_device->rx_buffer + ecm_device->rx_offset),ecm_device->rx_pool,size);
  265. ecm_device->rx_offset += size;
  266. if(size < EP_MAXPACKET(ecm_device->eps.ep_out))
  267. {
  268. ecm_device->rx_size = ecm_device->rx_offset;
  269. ecm_device->rx_offset = 0;
  270. eth_device_ready(&ecm_device->parent);
  271. }else
  272. {
  273. ecm_device->eps.ep_out->request.buffer = ecm_device->eps.ep_out->buffer;
  274. ecm_device->eps.ep_out->request.size = EP_MAXPACKET(ecm_device->eps.ep_out);
  275. ecm_device->eps.ep_out->request.req_type = UIO_REQUEST_READ_BEST;
  276. rt_usbd_io_request(ecm_device->func->device, ecm_device->eps.ep_out, &ecm_device->eps.ep_out->request);
  277. }
  278. return RT_EOK;
  279. }
  280. static rt_err_t rt_ecm_eth_init(rt_device_t dev)
  281. {
  282. return RT_EOK;
  283. }
  284. static rt_err_t rt_ecm_eth_open(rt_device_t dev, rt_uint16_t oflag)
  285. {
  286. return RT_EOK;
  287. }
  288. static rt_err_t rt_ecm_eth_close(rt_device_t dev)
  289. {
  290. return RT_EOK;
  291. }
  292. static rt_size_t rt_ecm_eth_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  293. {
  294. rt_set_errno(-RT_ENOSYS);
  295. return 0;
  296. }
  297. static rt_size_t rt_ecm_eth_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  298. {
  299. rt_set_errno(-RT_ENOSYS);
  300. return 0;
  301. }
  302. static rt_err_t rt_ecm_eth_control(rt_device_t dev, int cmd, void *args)
  303. {
  304. rt_ecm_eth_t ecm_eth_dev = (rt_ecm_eth_t)dev;
  305. switch(cmd)
  306. {
  307. case NIOCTL_GADDR:
  308. /* get mac address */
  309. if(args) rt_memcpy(args, ecm_eth_dev->dev_addr, MAX_ADDR_LEN);
  310. else return -RT_ERROR;
  311. break;
  312. default :
  313. break;
  314. }
  315. return RT_EOK;
  316. }
  317. #ifdef RT_USING_DEVICE_OPS
  318. const static struct rt_device_ops ecm_device_ops =
  319. {
  320. rt_ecm_eth_init,
  321. rt_ecm_eth_open,
  322. rt_ecm_eth_close,
  323. rt_ecm_eth_read,
  324. rt_ecm_eth_write,
  325. rt_ecm_eth_control
  326. };
  327. #endif
  328. struct pbuf *rt_ecm_eth_rx(rt_device_t dev)
  329. {
  330. struct pbuf* p = RT_NULL;
  331. rt_uint32_t offset = 0;
  332. rt_ecm_eth_t ecm_eth_dev = (rt_ecm_eth_t)dev;
  333. if(ecm_eth_dev->rx_size != 0)
  334. {
  335. /* allocate buffer */
  336. p = pbuf_alloc(PBUF_RAW, ecm_eth_dev->rx_size, PBUF_RAM);
  337. if (p != RT_NULL)
  338. {
  339. struct pbuf* q;
  340. for (q = p; q != RT_NULL; q= q->next)
  341. {
  342. /* Copy the received frame into buffer from memory pointed by the current ETHERNET DMA Rx descriptor */
  343. rt_memcpy(q->payload,
  344. (rt_uint8_t *)((ecm_eth_dev->rx_buffer) + offset),
  345. q->len);
  346. offset += q->len;
  347. }
  348. }
  349. }
  350. {
  351. if(ecm_eth_dev->func->device->state == USB_STATE_CONFIGURED)
  352. {
  353. ecm_eth_dev->rx_size = 0;
  354. ecm_eth_dev->rx_offset = 0;
  355. ecm_eth_dev->eps.ep_out->request.buffer = ecm_eth_dev->eps.ep_out->buffer;
  356. ecm_eth_dev->eps.ep_out->request.size = EP_MAXPACKET(ecm_eth_dev->eps.ep_out);
  357. ecm_eth_dev->eps.ep_out->request.req_type = UIO_REQUEST_READ_BEST;
  358. rt_usbd_io_request(ecm_eth_dev->func->device, ecm_eth_dev->eps.ep_out, &ecm_eth_dev->eps.ep_out->request);
  359. }
  360. }
  361. return p;
  362. }
  363. rt_err_t rt_ecm_eth_tx(rt_device_t dev, struct pbuf* p)
  364. {
  365. struct pbuf* q;
  366. char * pbuffer;
  367. rt_err_t result = RT_EOK;
  368. rt_ecm_eth_t ecm_eth_dev = (rt_ecm_eth_t)dev;
  369. if(!ecm_eth_dev->parent.link_status)
  370. {
  371. LOG_D("linkdown, drop pkg");
  372. return RT_EOK;
  373. }
  374. if(p->tot_len > USB_ETH_MTU)
  375. {
  376. LOG_W("ECM MTU is:%d, but the send packet size is %d",
  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_tick_from_millisecond(1000));
  381. if(result != RT_EOK)
  382. {
  383. LOG_W("wait for buffer free timeout");
  384. /* if cost 1s to wait send done it said that connection is close . drop it */
  385. rt_sem_release(&ecm_eth_dev->tx_buffer_free);
  386. return result;
  387. }
  388. pbuffer = (char *)&ecm_eth_dev->tx_buffer;
  389. for (q = p; q != NULL; q = q->next)
  390. {
  391. rt_memcpy(pbuffer, q->payload, q->len);
  392. pbuffer += q->len;
  393. }
  394. {
  395. if(ecm_eth_dev->func->device->state == USB_STATE_CONFIGURED)
  396. {
  397. ecm_eth_dev->eps.ep_in->request.buffer = (void *)&ecm_eth_dev->tx_buffer;
  398. ecm_eth_dev->eps.ep_in->request.size = p->tot_len;
  399. ecm_eth_dev->eps.ep_in->request.req_type = UIO_REQUEST_WRITE;
  400. rt_usbd_io_request(ecm_eth_dev->func->device, ecm_eth_dev->eps.ep_in, &ecm_eth_dev->eps.ep_in->request);
  401. }
  402. }
  403. return result;
  404. }
  405. /**
  406. * This function will handle RNDIS interrupt in endpoint request.
  407. *
  408. * @param device the usb device object.
  409. * @param size request size.
  410. *
  411. * @return RT_EOK.
  412. */
  413. static rt_err_t _ep_cmd_handler(ufunction_t func, rt_size_t size)
  414. {
  415. return RT_EOK;
  416. }
  417. /**
  418. * This function will run 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_enable(ufunction_t func)
  425. {
  426. cdc_eps_t eps;
  427. rt_ecm_eth_t ecm_device = (rt_ecm_eth_t)func->user_data;
  428. LOG_D("plugged in");
  429. eps = (cdc_eps_t)&ecm_device->eps;
  430. eps->ep_out->buffer = ecm_device->rx_pool;
  431. /* reset eth rx tx */
  432. ecm_device->rx_size = 0;
  433. ecm_device->rx_offset = 0;
  434. eps->ep_out->request.buffer = (void *)eps->ep_out->buffer;
  435. eps->ep_out->request.size = EP_MAXPACKET(eps->ep_out);
  436. eps->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
  437. rt_usbd_io_request(func->device, eps->ep_out, &eps->ep_out->request);
  438. return RT_EOK;
  439. }
  440. /**
  441. * This function will stop cdc class, it will be called on handle set configuration request.
  442. *
  443. * @param device the usb device object.
  444. *
  445. * @return RT_EOK on successful.
  446. */
  447. static rt_err_t _function_disable(ufunction_t func)
  448. {
  449. LOG_D("plugged out");
  450. eth_device_linkchange(&((rt_ecm_eth_t)func->user_data)->parent, RT_FALSE);
  451. /* reset eth rx tx */
  452. ((rt_ecm_eth_t)func->user_data)->rx_size = 0;
  453. ((rt_ecm_eth_t)func->user_data)->rx_offset = 0;
  454. return RT_EOK;
  455. }
  456. static struct ufunction_ops ops =
  457. {
  458. _function_enable,
  459. _function_disable,
  460. RT_NULL,
  461. };
  462. /**
  463. * This function will configure cdc descriptor.
  464. *
  465. * @param comm the communication interface number.
  466. * @param data the data interface number.
  467. *
  468. * @return RT_EOK on successful.
  469. */
  470. 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)
  471. {
  472. comm->call_mgmt_desc.data_interface = dintf_nr;
  473. comm->union_desc.master_interface = cintf_nr;
  474. comm->union_desc.slave_interface0 = dintf_nr;
  475. #ifdef RT_USB_DEVICE_COMPOSITE
  476. comm->iad_desc.bFirstInterface = cintf_nr;
  477. #endif
  478. data->ep_out_desc.wMaxPacketSize = device_is_hs ? 512 : 64;
  479. data->ep_in_desc.wMaxPacketSize = device_is_hs ? 512 : 64;
  480. return RT_EOK;
  481. }
  482. /**
  483. * This function will create a cdc ecm class instance.
  484. *
  485. * @param device the usb device object.
  486. *
  487. * @return RT_EOK on successful.
  488. */
  489. ufunction_t rt_usbd_function_ecm_create(udevice_t device)
  490. {
  491. ufunction_t cdc;
  492. rt_ecm_eth_t _ecm_eth;
  493. cdc_eps_t eps;
  494. uintf_t intf_comm, intf_data;
  495. ualtsetting_t comm_setting, data_setting;
  496. ucdc_data_desc_t data_desc;
  497. ucdc_eth_desc_t comm_desc;
  498. /* parameter check */
  499. RT_ASSERT(device != RT_NULL);
  500. /* set usb device string description */
  501. rt_usbd_device_set_string(device, _ustring);
  502. /* create a cdc class */
  503. cdc = rt_usbd_function_new(device, &_dev_desc, &ops);
  504. rt_usbd_device_set_qualifier(device, &dev_qualifier);
  505. _ecm_eth= rt_malloc(sizeof(struct rt_ecm_eth));
  506. rt_memset(_ecm_eth, 0, sizeof(struct rt_ecm_eth));
  507. cdc->user_data = _ecm_eth;
  508. _ecm_eth->func = cdc;
  509. /* create a cdc class endpoints collection */
  510. eps = &_ecm_eth->eps;
  511. /* create a cdc communication interface and a cdc data interface */
  512. intf_comm = rt_usbd_interface_new(device, _interface_handler);
  513. intf_data = rt_usbd_interface_new(device, _interface_handler);
  514. /* create a communication alternate setting and a data alternate setting */
  515. comm_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_eth_descriptor));
  516. data_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_data_descriptor));
  517. /* config desc in alternate setting */
  518. rt_usbd_altsetting_config_descriptor(comm_setting, &_comm_desc,
  519. (rt_off_t)&((ucdc_eth_desc_t)0)->intf_desc);
  520. rt_usbd_altsetting_config_descriptor(data_setting, &_data_desc, 0);
  521. /* configure the cdc interface descriptor */
  522. _cdc_descriptor_config(comm_setting->desc, intf_comm->intf_num, data_setting->desc, intf_data->intf_num, device->dcd->device_is_hs);
  523. /* create a command endpoint */
  524. comm_desc = (ucdc_eth_desc_t)comm_setting->desc;
  525. eps->ep_cmd = rt_usbd_endpoint_new(&comm_desc->ep_desc, _ep_cmd_handler);
  526. /* add the command endpoint to the cdc communication interface */
  527. rt_usbd_altsetting_add_endpoint(comm_setting, eps->ep_cmd);
  528. /* add the communication alternate setting to the communication interface,
  529. then set default setting of the interface */
  530. rt_usbd_interface_add_altsetting(intf_comm, comm_setting);
  531. rt_usbd_set_altsetting(intf_comm, 0);
  532. /* add the communication interface to the cdc class */
  533. rt_usbd_function_add_interface(cdc, intf_comm);
  534. /* create a bulk in and a bulk out endpoint */
  535. data_desc = (ucdc_data_desc_t)data_setting->desc;
  536. eps->ep_out = rt_usbd_endpoint_new(&data_desc->ep_out_desc, _ep_out_handler);
  537. eps->ep_in = rt_usbd_endpoint_new(&data_desc->ep_in_desc, _ep_in_handler);
  538. /* add the bulk out and bulk in endpoints to the data alternate setting */
  539. rt_usbd_altsetting_add_endpoint(data_setting, eps->ep_in);
  540. rt_usbd_altsetting_add_endpoint(data_setting, eps->ep_out);
  541. /* add the data alternate setting to the data interface
  542. then set default setting of the interface */
  543. rt_usbd_interface_add_altsetting(intf_data, data_setting);
  544. rt_usbd_set_altsetting(intf_data, 0);
  545. /* add the cdc data interface to cdc class */
  546. rt_usbd_function_add_interface(cdc, intf_data);
  547. rt_sem_init(&_ecm_eth->tx_buffer_free, "ue_tx", 1, RT_IPC_FLAG_FIFO);
  548. /* OUI 00-00-00, only for test. */
  549. _ecm_eth->dev_addr[0] = 0x34;
  550. _ecm_eth->dev_addr[1] = 0x97;
  551. _ecm_eth->dev_addr[2] = 0xF6;
  552. /* generate random MAC. */
  553. _ecm_eth->dev_addr[3] = 0x94;//*(const rt_uint8_t *)(0x1fff7a10);
  554. _ecm_eth->dev_addr[4] = 0xEC;//*(const rt_uint8_t *)(0x1fff7a14);
  555. _ecm_eth->dev_addr[5] = 0xAC;//(const rt_uint8_t *)(0x1fff7a18);
  556. /* OUI 00-00-00, only for test. */
  557. _ecm_eth->host_addr[0] = 0x34;
  558. _ecm_eth->host_addr[1] = 0x97;
  559. _ecm_eth->host_addr[2] = 0xF6;
  560. /* generate random MAC. */
  561. _ecm_eth->host_addr[3] = 0x94;//*(const rt_uint8_t *)(0x1fff7a10);
  562. _ecm_eth->host_addr[4] = 0xEC;//*(const rt_uint8_t *)(0x1fff7a14);
  563. _ecm_eth->host_addr[5] = 0xAB;//*(const rt_uint8_t *)(0x1fff7a18);
  564. _ecm_eth->parent.parent.init = rt_ecm_eth_init;
  565. _ecm_eth->parent.parent.open = rt_ecm_eth_open;
  566. _ecm_eth->parent.parent.close = rt_ecm_eth_close;
  567. _ecm_eth->parent.parent.read = rt_ecm_eth_read;
  568. _ecm_eth->parent.parent.write = rt_ecm_eth_write;
  569. _ecm_eth->parent.parent.control = rt_ecm_eth_control;
  570. _ecm_eth->parent.parent.user_data = device;
  571. _ecm_eth->parent.eth_rx = rt_ecm_eth_rx;
  572. _ecm_eth->parent.eth_tx = rt_ecm_eth_tx;
  573. /* register eth device */
  574. eth_device_init(&_ecm_eth->parent, "u0");
  575. /* send link up. */
  576. eth_device_linkchange(&_ecm_eth->parent, RT_FALSE);
  577. return cdc;
  578. }
  579. struct udclass ecm_class =
  580. {
  581. .rt_usbd_function_create = rt_usbd_function_ecm_create
  582. };
  583. int rt_usbd_ecm_class_register(void)
  584. {
  585. rt_usbd_class_register(&ecm_class);
  586. return 0;
  587. }
  588. INIT_PREV_EXPORT(rt_usbd_ecm_class_register);
  589. #endif /* RT_USB_DEVICE_ECM */