usbh_cdc_ecm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbh_core.h"
  7. #include "usbh_cdc_ecm.h"
  8. #undef USB_DBG_TAG
  9. #define USB_DBG_TAG "usbh_cdc_ecm"
  10. #include "usb_log.h"
  11. #define DEV_FORMAT "/dev/cdc_ether"
  12. /* general descriptor field offsets */
  13. #define DESC_bLength 0 /** Length offset */
  14. #define DESC_bDescriptorType 1 /** Descriptor type offset */
  15. #define DESC_bDescriptorSubType 2 /** Descriptor subtype offset */
  16. /* interface descriptor field offsets */
  17. #define INTF_DESC_bInterfaceNumber 2 /** Interface number offset */
  18. #define INTF_DESC_bAlternateSetting 3 /** Alternate setting offset */
  19. #define CONFIG_USBHOST_CDC_ECM_PKT_FILTER 0x000C
  20. #define CONFIG_USBHOST_CDC_ECM_ETH_MAX_SIZE 1514U
  21. static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_cdc_ecm_rx_buffer[CONFIG_USBHOST_CDC_ECM_ETH_MAX_SIZE];
  22. static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_cdc_ecm_tx_buffer[CONFIG_USBHOST_CDC_ECM_ETH_MAX_SIZE];
  23. static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_cdc_ecm_inttx_buffer[16];
  24. static struct usbh_cdc_ecm g_cdc_ecm_class;
  25. static int usbh_cdc_ecm_set_eth_packet_filter(struct usbh_cdc_ecm *cdc_ecm_class, uint16_t filter_value)
  26. {
  27. struct usb_setup_packet *setup;
  28. if (!cdc_ecm_class || !cdc_ecm_class->hport) {
  29. return -USB_ERR_INVAL;
  30. }
  31. setup = cdc_ecm_class->hport->setup;
  32. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  33. setup->bRequest = CDC_REQUEST_SET_ETHERNET_PACKET_FILTER;
  34. setup->wValue = filter_value;
  35. setup->wIndex = cdc_ecm_class->ctrl_intf;
  36. setup->wLength = 0;
  37. return usbh_control_transfer(cdc_ecm_class->hport, setup, NULL);
  38. }
  39. int usbh_cdc_ecm_get_connect_status(struct usbh_cdc_ecm *cdc_ecm_class)
  40. {
  41. int ret;
  42. usbh_int_urb_fill(&cdc_ecm_class->intin_urb, cdc_ecm_class->hport, cdc_ecm_class->intin, g_cdc_ecm_inttx_buffer, 16, USB_OSAL_WAITING_FOREVER, NULL, NULL);
  43. ret = usbh_submit_urb(&cdc_ecm_class->intin_urb);
  44. if (ret < 0) {
  45. return ret;
  46. }
  47. if (g_cdc_ecm_inttx_buffer[1] == CDC_ECM_NOTIFY_CODE_NETWORK_CONNECTION) {
  48. if (g_cdc_ecm_inttx_buffer[2] == CDC_ECM_NET_CONNECTED) {
  49. cdc_ecm_class->connect_status = true;
  50. } else {
  51. cdc_ecm_class->connect_status = false;
  52. }
  53. } else if (g_cdc_ecm_inttx_buffer[1] == CDC_ECM_NOTIFY_CODE_CONNECTION_SPEED_CHANGE) {
  54. memcpy(cdc_ecm_class->speed, &g_cdc_ecm_inttx_buffer[8], 8);
  55. }
  56. return 0;
  57. }
  58. static int usbh_cdc_ecm_connect(struct usbh_hubport *hport, uint8_t intf)
  59. {
  60. struct usb_endpoint_descriptor *ep_desc;
  61. int ret;
  62. uint8_t altsetting = 0;
  63. char mac_buffer[12];
  64. uint8_t *p;
  65. uint8_t cur_iface = 0xff;
  66. uint8_t mac_str_idx = 0xff;
  67. struct usbh_cdc_ecm *cdc_ecm_class = &g_cdc_ecm_class;
  68. memset(cdc_ecm_class, 0, sizeof(struct usbh_cdc_ecm));
  69. cdc_ecm_class->hport = hport;
  70. cdc_ecm_class->ctrl_intf = intf;
  71. cdc_ecm_class->data_intf = intf + 1;
  72. hport->config.intf[intf].priv = cdc_ecm_class;
  73. hport->config.intf[intf + 1].priv = NULL;
  74. p = hport->raw_config_desc;
  75. while (p[DESC_bLength]) {
  76. switch (p[DESC_bDescriptorType]) {
  77. case USB_DESCRIPTOR_TYPE_INTERFACE:
  78. cur_iface = p[INTF_DESC_bInterfaceNumber];
  79. //cur_alt_setting = p[INTF_DESC_bAlternateSetting];
  80. break;
  81. case CDC_CS_INTERFACE:
  82. if ((cur_iface == cdc_ecm_class->ctrl_intf) && p[DESC_bDescriptorSubType] == CDC_FUNC_DESC_ETHERNET_NETWORKING) {
  83. struct cdc_eth_descriptor *desc = (struct cdc_eth_descriptor *)p;
  84. mac_str_idx = desc->iMACAddress;
  85. cdc_ecm_class->max_segment_size = desc->wMaxSegmentSize;
  86. goto get_mac;
  87. }
  88. break;
  89. default:
  90. break;
  91. }
  92. /* skip to next descriptor */
  93. p += p[DESC_bLength];
  94. }
  95. get_mac:
  96. if (mac_str_idx == 0xff) {
  97. USB_LOG_ERR("Do not find cdc ecm mac string\r\n");
  98. return -1;
  99. }
  100. memset(mac_buffer, 0, 12);
  101. ret = usbh_get_string_desc(cdc_ecm_class->hport, mac_str_idx, (uint8_t *)mac_buffer);
  102. if (ret < 0) {
  103. return ret;
  104. }
  105. for (int i = 0, j = 0; i < 12; i += 2, j++) {
  106. char byte_str[3];
  107. byte_str[0] = mac_buffer[i];
  108. byte_str[1] = mac_buffer[i + 1];
  109. byte_str[2] = '\0';
  110. uint32_t byte = strtoul(byte_str, NULL, 16);
  111. cdc_ecm_class->mac[j] = (unsigned char)byte;
  112. }
  113. USB_LOG_INFO("CDC ECM MAC address %02x:%02x:%02x:%02x:%02x:%02x\r\n",
  114. cdc_ecm_class->mac[0],
  115. cdc_ecm_class->mac[1],
  116. cdc_ecm_class->mac[2],
  117. cdc_ecm_class->mac[3],
  118. cdc_ecm_class->mac[4],
  119. cdc_ecm_class->mac[5]);
  120. if (cdc_ecm_class->max_segment_size > CONFIG_USBHOST_CDC_ECM_ETH_MAX_SIZE) {
  121. USB_LOG_ERR("CDC ECM Max Segment Size is overflow, default is %u, but now %u\r\n", CONFIG_USBHOST_CDC_ECM_ETH_MAX_SIZE, cdc_ecm_class->max_segment_size);
  122. } else {
  123. USB_LOG_INFO("CDC ECM Max Segment Size:%u\r\n", cdc_ecm_class->max_segment_size);
  124. }
  125. /* enable int ep */
  126. ep_desc = &hport->config.intf[intf].altsetting[0].ep[0].ep_desc;
  127. USBH_EP_INIT(cdc_ecm_class->intin, ep_desc);
  128. if (hport->config.intf[intf + 1].altsetting_num > 1) {
  129. altsetting = hport->config.intf[intf + 1].altsetting_num - 1;
  130. for (uint8_t i = 0; i < hport->config.intf[intf + 1].altsetting[altsetting].intf_desc.bNumEndpoints; i++) {
  131. ep_desc = &hport->config.intf[intf + 1].altsetting[altsetting].ep[i].ep_desc;
  132. if (ep_desc->bEndpointAddress & 0x80) {
  133. USBH_EP_INIT(cdc_ecm_class->bulkin, ep_desc);
  134. } else {
  135. USBH_EP_INIT(cdc_ecm_class->bulkout, ep_desc);
  136. }
  137. }
  138. USB_LOG_INFO("Select cdc ecm altsetting: %d\r\n", altsetting);
  139. usbh_set_interface(cdc_ecm_class->hport, cdc_ecm_class->data_intf, altsetting);
  140. } else {
  141. for (uint8_t i = 0; i < hport->config.intf[intf + 1].altsetting[0].intf_desc.bNumEndpoints; i++) {
  142. ep_desc = &hport->config.intf[intf + 1].altsetting[0].ep[i].ep_desc;
  143. if (ep_desc->bEndpointAddress & 0x80) {
  144. USBH_EP_INIT(cdc_ecm_class->bulkin, ep_desc);
  145. } else {
  146. USBH_EP_INIT(cdc_ecm_class->bulkout, ep_desc);
  147. }
  148. }
  149. }
  150. /* bit0 Promiscuous
  151. * bit1 ALL Multicast
  152. * bit2 Directed
  153. * bit3 Broadcast
  154. * bit4 Multicast
  155. */
  156. ret = usbh_cdc_ecm_set_eth_packet_filter(cdc_ecm_class, CONFIG_USBHOST_CDC_ECM_PKT_FILTER);
  157. if (ret < 0) {
  158. return ret;
  159. }
  160. USB_LOG_INFO("Set CDC ECM packet filter:%04x\r\n", CONFIG_USBHOST_CDC_ECM_PKT_FILTER);
  161. strncpy(hport->config.intf[intf].devname, DEV_FORMAT, CONFIG_USBHOST_DEV_NAMELEN);
  162. USB_LOG_INFO("Register CDC ECM Class:%s\r\n", hport->config.intf[intf].devname);
  163. usbh_cdc_ecm_run(cdc_ecm_class);
  164. return ret;
  165. }
  166. static int usbh_cdc_ecm_disconnect(struct usbh_hubport *hport, uint8_t intf)
  167. {
  168. int ret = 0;
  169. struct usbh_cdc_ecm *cdc_ecm_class = (struct usbh_cdc_ecm *)hport->config.intf[intf].priv;
  170. if (cdc_ecm_class) {
  171. if (cdc_ecm_class->bulkin) {
  172. usbh_kill_urb(&cdc_ecm_class->bulkin_urb);
  173. }
  174. if (cdc_ecm_class->bulkout) {
  175. usbh_kill_urb(&cdc_ecm_class->bulkout_urb);
  176. }
  177. if (cdc_ecm_class->intin) {
  178. usbh_kill_urb(&cdc_ecm_class->intin_urb);
  179. }
  180. if (hport->config.intf[intf].devname[0] != '\0') {
  181. USB_LOG_INFO("Unregister CDC ECM Class:%s\r\n", hport->config.intf[intf].devname);
  182. usbh_cdc_ecm_stop(cdc_ecm_class);
  183. }
  184. memset(cdc_ecm_class, 0, sizeof(struct usbh_cdc_ecm));
  185. }
  186. return ret;
  187. }
  188. void usbh_cdc_ecm_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
  189. {
  190. uint32_t g_cdc_ecm_rx_length;
  191. int ret;
  192. (void)CONFIG_USB_OSAL_THREAD_GET_ARGV;
  193. USB_LOG_INFO("Create cdc ecm rx thread\r\n");
  194. // clang-format off
  195. find_class:
  196. // clang-format on
  197. g_cdc_ecm_class.connect_status = false;
  198. if (usbh_find_class_instance("/dev/cdc_ether") == NULL) {
  199. goto delete;
  200. }
  201. while (g_cdc_ecm_class.connect_status == false) {
  202. ret = usbh_cdc_ecm_get_connect_status(&g_cdc_ecm_class);
  203. if (ret < 0) {
  204. usb_osal_msleep(100);
  205. goto find_class;
  206. }
  207. usb_osal_msleep(128);
  208. }
  209. g_cdc_ecm_rx_length = 0;
  210. while (1) {
  211. usbh_bulk_urb_fill(&g_cdc_ecm_class.bulkin_urb, g_cdc_ecm_class.hport, g_cdc_ecm_class.bulkin, g_cdc_ecm_rx_buffer, CONFIG_USBHOST_CDC_ECM_ETH_MAX_SIZE, USB_OSAL_WAITING_FOREVER, NULL, NULL);
  212. ret = usbh_submit_urb(&g_cdc_ecm_class.bulkin_urb);
  213. if (ret < 0) {
  214. goto find_class;
  215. }
  216. g_cdc_ecm_rx_length = g_cdc_ecm_class.bulkin_urb.actual_length;
  217. /* A transfer is complete because last packet is a short packet.
  218. * Short packet is not zero, match g_cdc_ecm_rx_length % USB_GET_MAXPACKETSIZE(g_cdc_ecm_class.bulkin->wMaxPacketSize).
  219. * Short packet is zero, check if g_cdc_ecm_class.bulkin_urb.actual_length < transfer_size, for example transfer is complete with size is 512 < 1514.
  220. * This case is always true
  221. */
  222. if (g_cdc_ecm_rx_length % USB_GET_MAXPACKETSIZE(g_cdc_ecm_class.bulkin->wMaxPacketSize) ||
  223. (g_cdc_ecm_class.bulkin_urb.actual_length < CONFIG_USBHOST_CDC_ECM_ETH_MAX_SIZE)) {
  224. USB_LOG_DBG("rxlen:%d\r\n", g_cdc_ecm_rx_length);
  225. usbh_cdc_ecm_eth_input(g_cdc_ecm_rx_buffer, g_cdc_ecm_rx_length);
  226. g_cdc_ecm_rx_length = 0;
  227. } else {
  228. /* There's no way to run here. */
  229. }
  230. }
  231. // clang-format off
  232. delete:
  233. USB_LOG_INFO("Delete cdc ecm rx thread\r\n");
  234. usb_osal_thread_delete(NULL);
  235. // clang-format on
  236. }
  237. uint8_t *usbh_cdc_ecm_get_eth_txbuf(void)
  238. {
  239. return g_cdc_ecm_tx_buffer;
  240. }
  241. int usbh_cdc_ecm_eth_output(uint32_t buflen)
  242. {
  243. if (g_cdc_ecm_class.connect_status == false) {
  244. return -USB_ERR_NOTCONN;
  245. }
  246. USB_LOG_DBG("txlen:%d\r\n", buflen);
  247. usbh_bulk_urb_fill(&g_cdc_ecm_class.bulkout_urb, g_cdc_ecm_class.hport, g_cdc_ecm_class.bulkout, g_cdc_ecm_tx_buffer, buflen, USB_OSAL_WAITING_FOREVER, NULL, NULL);
  248. return usbh_submit_urb(&g_cdc_ecm_class.bulkout_urb);
  249. }
  250. __WEAK void usbh_cdc_ecm_run(struct usbh_cdc_ecm *cdc_ecm_class)
  251. {
  252. (void)cdc_ecm_class;
  253. }
  254. __WEAK void usbh_cdc_ecm_stop(struct usbh_cdc_ecm *cdc_ecm_class)
  255. {
  256. (void)cdc_ecm_class;
  257. }
  258. const struct usbh_class_driver cdc_ecm_class_driver = {
  259. .driver_name = "cdc_ecm",
  260. .connect = usbh_cdc_ecm_connect,
  261. .disconnect = usbh_cdc_ecm_disconnect
  262. };
  263. CLASS_INFO_DEFINE const struct usbh_class_info cdc_ecm_class_info = {
  264. .match_flags = USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL,
  265. .bInterfaceClass = USB_DEVICE_CLASS_CDC,
  266. .bInterfaceSubClass = CDC_ETHERNET_NETWORKING_CONTROL_MODEL,
  267. .bInterfaceProtocol = CDC_COMMON_PROTOCOL_NONE,
  268. .id_table = NULL,
  269. .class_driver = &cdc_ecm_class_driver
  270. };