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