]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: GPL-2.0-only | |
2 | /* | |
3 | * Espressif Systems Wireless LAN device driver | |
4 | * | |
5 | * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD | |
6 | * | |
7 | */ | |
8 | #include "utils.h" | |
9 | #include <linux/init.h> | |
10 | #include <linux/module.h> | |
11 | #include <linux/kernel.h> | |
12 | #include <linux/gpio.h> | |
13 | #include <linux/igmp.h> | |
14 | ||
15 | #include "esp.h" | |
16 | #include "esp_if.h" | |
17 | #include "esp_bt_api.h" | |
18 | #include "esp_api.h" | |
19 | #include "esp_cmd.h" | |
20 | #include "esp_kernel_port.h" | |
21 | ||
22 | #include "esp_cfg80211.h" | |
23 | #include "esp_stats.h" | |
24 | ||
25 | #define RELEASE_VERSION "1.0.3" | |
26 | #define HOST_GPIO_PIN_INVALID -1 | |
27 | #define CONFIG_ALLOW_MULTICAST_WAKEUP 1 | |
28 | static int resetpin = HOST_GPIO_PIN_INVALID; | |
29 | static int readypin = HOST_GPIO_PIN_INVALID; | |
30 | static int handshakepin = HOST_GPIO_PIN_INVALID; | |
31 | static u32 clockspeed = 0; | |
32 | extern u8 ap_bssid[MAC_ADDR_LEN]; | |
33 | extern volatile u8 host_sleep; | |
34 | u32 raw_tp_mode = 0; | |
35 | int log_level = ESP_INFO; | |
36 | ||
37 | module_param(resetpin, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); | |
38 | MODULE_PARM_DESC(resetpin, "Host's GPIO pin number which is connected to ESP32's EN to reset ESP32 device"); | |
39 | ||
40 | module_param(clockspeed, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); | |
41 | MODULE_PARM_DESC(clockspeed, "Hosts clock speed in MHz"); | |
42 | ||
43 | module_param(raw_tp_mode, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); | |
44 | MODULE_PARM_DESC(raw_tp_mode, "Mode choosed to test raw throughput"); | |
45 | ||
46 | module_param(readypin, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); | |
47 | MODULE_PARM_DESC(readypin, "Data ready pin default is pin 13, gpio27"); | |
48 | ||
49 | module_param(handshakepin, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); | |
50 | MODULE_PARM_DESC(handshakepin, "Handshake pin default is pin 15, gpio22"); | |
51 | ||
52 | static void deinit_adapter(void); | |
53 | ||
54 | ||
55 | struct multicast_list mcast_list = {0}; | |
56 | struct esp_adapter adapter; | |
57 | /*struct esp_device esp_dev;*/ | |
58 | ||
59 | struct esp_adapter *esp_get_adapter(void) | |
60 | { | |
61 | return &adapter; | |
62 | } | |
63 | ||
64 | void esp_process_new_packet_intr(struct esp_adapter *adapter) | |
65 | { | |
66 | if (adapter) | |
67 | queue_work(adapter->if_rx_workqueue, &adapter->if_rx_work); | |
68 | } | |
69 | ||
70 | static int process_tx_packet(struct sk_buff *skb) | |
71 | { | |
72 | struct esp_wifi_device *priv = NULL; | |
73 | struct esp_skb_cb *cb = NULL; | |
74 | struct esp_payload_header *payload_header = NULL; | |
75 | struct sk_buff *new_skb = NULL; | |
76 | int ret = 0; | |
77 | u8 pad_len = 0, realloc_skb = 0; | |
78 | u16 len = 0; | |
79 | u16 total_len = 0; | |
80 | static u8 c; | |
81 | u8 *pos = NULL; | |
82 | ||
83 | c++; | |
84 | /* Get the priv */ | |
85 | cb = (struct esp_skb_cb *) skb->cb; | |
86 | priv = cb->priv; | |
87 | ||
88 | if (!priv) { | |
89 | dev_kfree_skb(skb); | |
90 | esp_info("No priv\n"); | |
91 | return NETDEV_TX_OK; | |
92 | } | |
93 | ||
94 | if (netif_queue_stopped((const struct net_device *) priv->ndev)) { | |
95 | esp_info("Netif queue stopped\n"); | |
96 | return NETDEV_TX_BUSY; | |
97 | } | |
98 | ||
99 | if (host_sleep) { | |
100 | return NETDEV_TX_BUSY; | |
101 | } | |
102 | ||
103 | len = skb->len; | |
104 | ||
105 | /* Create space for payload header */ | |
106 | pad_len = sizeof(struct esp_payload_header); | |
107 | ||
108 | total_len = len + pad_len; | |
109 | ||
110 | /* Align buffer length */ | |
111 | pad_len += SKB_DATA_ADDR_ALIGNMENT - (total_len % SKB_DATA_ADDR_ALIGNMENT); | |
112 | ||
113 | if (skb_headroom(skb) < pad_len) { | |
114 | /* Headroom is not sufficient */ | |
115 | realloc_skb = 1; | |
116 | } | |
117 | ||
118 | if (realloc_skb || !IS_ALIGNED((unsigned long) skb->data, SKB_DATA_ADDR_ALIGNMENT)) { | |
119 | /* Realloc SKB */ | |
120 | if (skb_linearize(skb)) { | |
121 | priv->stats.tx_errors++; | |
122 | dev_kfree_skb(skb); | |
123 | esp_err("Failed to linearize SKB"); | |
124 | return NETDEV_TX_OK; | |
125 | } | |
126 | ||
127 | new_skb = esp_alloc_skb(skb->len + pad_len); | |
128 | ||
129 | if (!new_skb) { | |
130 | esp_err("Failed to allocate SKB"); | |
131 | priv->stats.tx_errors++; | |
132 | dev_kfree_skb(skb); | |
133 | return NETDEV_TX_OK; | |
134 | } | |
135 | ||
136 | pos = new_skb->data; | |
137 | pos += pad_len; | |
138 | ||
139 | /* Populate new SKB */ | |
140 | skb_copy_from_linear_data(skb, pos, skb->len); | |
141 | skb_put(new_skb, skb->len + pad_len); | |
142 | ||
143 | /* Replace old SKB */ | |
144 | dev_kfree_skb_any(skb); | |
145 | skb = new_skb; | |
146 | } else { | |
147 | /* Realloc is not needed, Make space for interface header */ | |
148 | skb_push(skb, pad_len); | |
149 | } | |
150 | ||
151 | /* Set payload header */ | |
152 | payload_header = (struct esp_payload_header *) skb->data; | |
153 | memset(payload_header, 0, pad_len); | |
154 | ||
155 | payload_header->if_type = priv->if_type; | |
156 | payload_header->if_num = priv->if_num; | |
157 | payload_header->len = cpu_to_le16(len); | |
158 | payload_header->offset = cpu_to_le16(pad_len); | |
159 | payload_header->packet_type = PACKET_TYPE_DATA; | |
160 | ||
161 | if (adapter.capabilities & ESP_CHECKSUM_ENABLED) | |
162 | payload_header->checksum = cpu_to_le16(compute_checksum(skb->data, (len + pad_len))); | |
163 | ||
164 | if (!priv->stop_data) { | |
165 | ret = esp_send_packet(priv->adapter, skb); | |
166 | ||
167 | if (ret) { | |
168 | esp_verbose("Failed to send SKB"); | |
169 | priv->stats.tx_errors++; | |
170 | } else { | |
171 | priv->stats.tx_packets++; | |
172 | priv->stats.tx_bytes += skb->len; | |
173 | } | |
174 | } else { | |
175 | dev_kfree_skb_any(skb); | |
176 | priv->stats.tx_dropped++; | |
177 | } | |
178 | ||
179 | return 0; | |
180 | } | |
181 | ||
182 | void esp_port_open(struct esp_wifi_device *priv) | |
183 | { | |
184 | priv->port_open = 1; | |
185 | priv->stop_data = 0; | |
186 | } | |
187 | ||
188 | void esp_port_close(struct esp_wifi_device *priv) | |
189 | { | |
190 | if (!priv) | |
191 | return; | |
192 | ||
193 | priv->port_open = 0; | |
194 | priv->stop_data = 1; | |
195 | } | |
196 | ||
197 | void print_capabilities(u32 cap) | |
198 | { | |
199 | esp_info("Capabilities: 0x%x. Features supported are:\n", cap); | |
200 | if (cap & ESP_WLAN_SDIO_SUPPORT) | |
201 | esp_info("\t * WLAN on SDIO\n"); | |
202 | else if (cap & ESP_WLAN_SPI_SUPPORT) | |
203 | esp_info("\t * WLAN on SPI\n"); | |
204 | ||
205 | if ((cap & ESP_BT_UART_SUPPORT) || | |
206 | (cap & ESP_BT_SDIO_SUPPORT) || | |
207 | (cap & ESP_BT_SPI_SUPPORT)) { | |
208 | esp_info("\t * BT/BLE\n"); | |
209 | if (cap & ESP_BT_UART_SUPPORT) | |
210 | esp_info("\t - HCI over UART\n"); | |
211 | if (cap & ESP_BT_SDIO_SUPPORT) | |
212 | esp_info("\t - HCI over SDIO\n"); | |
213 | if (cap & ESP_BT_SPI_SUPPORT) | |
214 | esp_info("\t - HCI over SPI\n"); | |
215 | ||
216 | if ((cap & ESP_BLE_ONLY_SUPPORT) && (cap & ESP_BR_EDR_ONLY_SUPPORT)) | |
217 | esp_info("\t - BT/BLE dual mode\n"); | |
218 | else if (cap & ESP_BLE_ONLY_SUPPORT) | |
219 | esp_info("\t - BLE only\n"); | |
220 | else if (cap & ESP_BR_EDR_ONLY_SUPPORT) | |
221 | esp_info("\t - BR EDR only\n"); | |
222 | } | |
223 | } | |
224 | ||
225 | void init_bt(struct esp_adapter *adapter) | |
226 | { | |
227 | ||
228 | if ((adapter->capabilities & ESP_BT_SPI_SUPPORT) || | |
229 | (adapter->capabilities & ESP_BT_SDIO_SUPPORT)) { | |
230 | msleep(200); | |
231 | esp_info("ESP Bluetooth init\n"); | |
232 | esp_init_bt(adapter); | |
233 | } | |
234 | } | |
235 | ||
236 | static int check_esp_version(struct fw_version *ver) | |
237 | { | |
238 | esp_info("ESP Firmware version: %u.%u.%u\n", | |
239 | ver->major1, ver->major2, ver->minor); | |
240 | if (!ver->major1) { | |
241 | esp_err("Incompatible ESP firmware release detected, Please use correct ESP-Hosted branch/compatible release\n"); | |
242 | return -1; | |
243 | } | |
244 | return 0; | |
245 | } | |
246 | ||
247 | static void print_reset_reason(uint32_t reason) | |
248 | { | |
249 | switch (reason) | |
250 | { | |
251 | case 1: esp_info("POWERON_RESET\n"); break; /**<1, Vbat power on reset*/ | |
252 | case 3: esp_info("SW_RESET\n"); break; /**<3, Software reset digital core*/ | |
253 | case 4: esp_info("OWDT_RESET\n"); break; /**<4, Legacy watch dog reset digital core*/ | |
254 | case 5: esp_info("DEEPSLEEP_RESET\n"); break; /**<5, Deep Sleep reset digital core*/ | |
255 | case 6: esp_info("SDIO_RESET\n"); break; /**<6, Reset by SLC module, reset digital core*/ | |
256 | case 7: esp_info("TG0WDT_SYS_RESET\n"); break; /**<7, Timer Group0 Watch dog reset digital core*/ | |
257 | case 8: esp_info("TG1WDT_SYS_RESET\n"); break; /**<8, Timer Group1 Watch dog reset digital core*/ | |
258 | case 9: esp_info("RTCWDT_SYS_RESET\n"); break; /**<9, RTC Watch dog Reset digital core*/ | |
259 | case 10: esp_info("INTRUSION_RESET\n"); break; /**<10, Instrusion tested to reset CPU*/ | |
260 | case 11: esp_info("TGWDT_CPU_RESET\n"); break; /**<11, Time Group reset CPU*/ | |
261 | case 12: esp_info("SW_CPU_RESET\n"); break; /**<12, Software reset CPU*/ | |
262 | case 13: esp_info("RTCWDT_CPU_RESET\n"); break; /**<13, RTC Watch dog Reset CPU*/ | |
263 | case 14: esp_info("EXT_CPU_RESET\n"); break; /**<14, for APP CPU, reseted by PRO CPU*/ | |
264 | case 15: esp_info("RTCWDT_BROWN_OUT_RESET\n"); break;/**<15, Reset when the vdd voltage is not stable*/ | |
265 | case 16: esp_info("RTCWDT_RTC_RESET\n"); break; /**<16, RTC Watch dog reset digital core and rtc module*/ | |
266 | default: esp_info("Unknown[%u]\n", reason); break; | |
267 | } | |
268 | } | |
269 | ||
270 | static int process_fw_data(struct fw_data *fw_p, int tag_len) | |
271 | { | |
272 | if (tag_len != sizeof(struct fw_data)) { | |
273 | esp_err("Length not matching to firmware data size\n"); | |
274 | return -1; | |
275 | } | |
276 | ||
277 | esp_info("ESP chipset's last reset cause:\n"); | |
278 | print_reset_reason(le32_to_cpu(fw_p->last_reset_reason)); | |
279 | ||
280 | return check_esp_version(&fw_p->version); | |
281 | } | |
282 | ||
283 | int process_event_esp_bootup(struct esp_adapter *adapter, u8 *evt_buf, u8 len) | |
284 | { | |
285 | int len_left = len, tag_len, ret = 0; | |
286 | u8 *pos; | |
287 | ||
288 | if (!adapter || !evt_buf) | |
289 | return -1; | |
290 | ||
291 | if (len_left >= 64) { | |
292 | esp_info("ESP init event len looks unexpected: %u (>=64)\n", len_left); | |
293 | esp_info("You probably facing timing mismatch at transport layer\n"); | |
294 | } | |
295 | ||
296 | clear_bit(ESP_INIT_DONE, &adapter->state_flags); | |
297 | /* Deinit module if already initialized */ | |
298 | test_raw_tp_cleanup(); | |
299 | esp_deinit_module(adapter); | |
300 | ||
301 | pos = evt_buf; | |
302 | ||
303 | while (len_left > 0) { | |
304 | tag_len = *(pos + 1); | |
305 | ||
306 | esp_info("Bootup Event tag: %d\n", *pos); | |
307 | ||
308 | switch (*pos) { | |
309 | case ESP_BOOTUP_CAPABILITY: | |
310 | adapter->capabilities = *(pos + 2); | |
311 | break; | |
312 | case ESP_BOOTUP_FIRMWARE_CHIP_ID: | |
313 | ret = esp_validate_chipset(adapter, *(pos + 2)); | |
314 | break; | |
315 | case ESP_BOOTUP_FW_DATA: | |
316 | ret = process_fw_data((struct fw_data *)(pos + 2), tag_len); | |
317 | break; | |
318 | case ESP_BOOTUP_SPI_CLK_MHZ: | |
319 | ret = esp_adjust_spi_clock(adapter, *(pos + 2)); | |
320 | break; | |
321 | default: | |
322 | esp_warn("Unsupported tag=%x in bootup event\n", *pos); | |
323 | } | |
324 | ||
325 | if (ret < 0) { | |
326 | esp_err("failed to process tag=%x in bootup event\n", *pos); | |
327 | return -1; | |
328 | } | |
329 | pos += (tag_len + 2); | |
330 | len_left -= (tag_len + 2); | |
331 | } | |
332 | ||
333 | if (esp_add_card(adapter)) { | |
334 | esp_err("network iterface init failed\n"); | |
335 | return -1; | |
336 | } | |
337 | init_bt(adapter); | |
338 | ||
339 | if (raw_tp_mode !=0) { | |
340 | #if TEST_RAW_TP | |
341 | process_test_capabilities(raw_tp_mode); | |
342 | esp_init_raw_tp(adapter); | |
343 | #else | |
344 | esp_err("RAW TP mode selected but not enabled\n"); | |
345 | return -1; | |
346 | #endif | |
347 | } | |
348 | set_bit(ESP_INIT_DONE, &adapter->state_flags); | |
349 | print_capabilities(adapter->capabilities); | |
350 | ||
351 | return 0; | |
352 | } | |
353 | ||
354 | static int esp_open(struct net_device *ndev) | |
355 | { | |
356 | return 0; | |
357 | } | |
358 | ||
359 | static int esp_stop(struct net_device *ndev) | |
360 | { | |
361 | return 0; | |
362 | } | |
363 | ||
364 | static struct net_device_stats *esp_get_stats(struct net_device *ndev) | |
365 | { | |
366 | struct esp_wifi_device *priv = netdev_priv(ndev); | |
367 | ||
368 | if (!priv) | |
369 | return NULL; | |
370 | ||
371 | return &priv->stats; | |
372 | } | |
373 | ||
374 | static int esp_set_mac_address(struct net_device *ndev, void *data) | |
375 | { | |
376 | struct esp_wifi_device *priv = netdev_priv(ndev); | |
377 | struct sockaddr *sa = (struct sockaddr *)data; | |
378 | int ret; | |
379 | ||
380 | if (!priv || !priv->adapter) | |
381 | return -EINVAL; | |
382 | ||
383 | esp_info("%u "MACSTR"\n", __LINE__, MAC2STR(sa->sa_data)); | |
384 | ||
385 | ret = cmd_set_mac(priv, sa->sa_data); | |
386 | ||
387 | if (ret == 0) | |
388 | eth_hw_addr_set(ndev, priv->mac_address/*mac_addr->sa_data*/); | |
389 | ||
390 | return ret; | |
391 | } | |
392 | ||
393 | static void esp_set_rx_mode(struct net_device *ndev) | |
394 | { | |
395 | struct esp_adapter *adapter = esp_get_adapter(); | |
396 | ||
397 | schedule_work(&adapter->mac_flter_work); | |
398 | } | |
399 | ||
400 | static int esp_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev) | |
401 | { | |
402 | struct esp_wifi_device *priv = NULL; | |
403 | struct esp_skb_cb *cb = NULL; | |
404 | ||
405 | if (!skb || !ndev) | |
406 | return NETDEV_TX_OK; | |
407 | ||
408 | priv = netdev_priv(ndev); | |
409 | if (!priv) { | |
410 | dev_kfree_skb(skb); | |
411 | return NETDEV_TX_OK; | |
412 | } | |
413 | ||
414 | if (!priv->port_open) { | |
415 | priv->stats.tx_dropped++; | |
416 | esp_verbose("Port not yet open\n"); | |
417 | dev_kfree_skb(skb); | |
418 | return NETDEV_TX_OK; | |
419 | } | |
420 | ||
421 | if (!skb->len || (skb->len > ETH_FRAME_LEN)) { | |
422 | esp_err("Bad len %d\n", skb->len); | |
423 | priv->stats.tx_dropped++; | |
424 | dev_kfree_skb(skb); | |
425 | return NETDEV_TX_OK; | |
426 | } | |
427 | ||
428 | cb = (struct esp_skb_cb *) skb->cb; | |
429 | cb->priv = priv; | |
430 | ||
431 | return process_tx_packet(skb); | |
432 | } | |
433 | ||
434 | static const struct net_device_ops esp_netdev_ops = { | |
435 | .ndo_open = esp_open, | |
436 | .ndo_stop = esp_stop, | |
437 | .ndo_start_xmit = esp_hard_start_xmit, | |
438 | .ndo_set_mac_address = esp_set_mac_address, | |
439 | .ndo_validate_addr = eth_validate_addr, | |
440 | .ndo_get_stats = esp_get_stats, | |
441 | .ndo_set_rx_mode = esp_set_rx_mode, | |
442 | }; | |
443 | ||
444 | ||
445 | void esp_init_priv(struct net_device *ndev) | |
446 | { | |
447 | ndev->netdev_ops = &esp_netdev_ops; | |
448 | ndev->needed_headroom = roundup(sizeof(struct esp_payload_header) + | |
449 | INTERFACE_HEADER_PADDING, 4); | |
450 | } | |
451 | ||
452 | static int esp_add_network_ifaces(struct esp_adapter *adapter) | |
453 | { | |
454 | struct wireless_dev *wdev = NULL; | |
455 | ||
456 | if (!adapter) { | |
457 | esp_info("adapter not yet init\n"); | |
458 | return -EINVAL; | |
459 | } | |
460 | ||
461 | rtnl_lock(); | |
462 | wdev = esp_cfg80211_add_iface(adapter->wiphy, "wlan%d", 1, NL80211_IFTYPE_STATION, NULL); | |
463 | rtnl_unlock(); | |
464 | ||
465 | /* Return success if network added successfully */ | |
466 | if (wdev) | |
467 | return 0; | |
468 | ||
469 | return -1; | |
470 | } | |
471 | ||
472 | int esp_init_raw_tp(struct esp_adapter *adapter) | |
473 | { | |
474 | RET_ON_FAIL(cmd_init_raw_tp_task_timer(adapter->priv[ESP_STA_NW_IF])); | |
475 | return 0; | |
476 | } | |
477 | ||
478 | int esp_add_card(struct esp_adapter *adapter) | |
479 | { | |
480 | RET_ON_FAIL(esp_commands_setup(adapter)); | |
481 | RET_ON_FAIL(esp_add_wiphy(adapter)); | |
482 | RET_ON_FAIL(esp_add_network_ifaces(adapter)); | |
483 | clear_bit(ESP_CLEANUP_IN_PROGRESS, &adapter->state_flags); | |
484 | ||
485 | return 0; | |
486 | } | |
487 | ||
488 | static int esp_remove_network_ifaces(struct esp_adapter *adapter) | |
489 | { | |
490 | uint8_t iface_idx = 0; | |
491 | struct net_device *ndev = NULL; | |
492 | struct esp_wifi_device *priv = NULL; | |
493 | ||
494 | for (iface_idx = 0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) { | |
495 | ||
496 | priv = adapter->priv[iface_idx]; | |
497 | if (!priv) | |
498 | continue; | |
499 | if (!test_bit(ESP_NETWORK_UP, &priv->priv_flags)) | |
500 | continue; | |
501 | ||
502 | ndev = priv->ndev; | |
503 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0) | |
504 | if (ndev) | |
505 | ndev->needs_free_netdev = true; | |
506 | rtnl_lock(); | |
507 | wiphy_lock(adapter->wiphy); | |
508 | cfg80211_unregister_wdev(&priv->wdev); | |
509 | wiphy_unlock(adapter->wiphy); | |
510 | rtnl_unlock(); | |
511 | #else | |
512 | if (ndev && ndev->reg_state == NETREG_REGISTERED) { | |
513 | unregister_netdev(ndev); | |
514 | free_netdev(ndev); | |
515 | ndev = NULL; | |
516 | } | |
517 | #endif | |
518 | adapter->priv[iface_idx] = NULL; | |
519 | } | |
520 | ||
521 | return 0; | |
522 | } | |
523 | ||
524 | static int stop_network_iface(struct esp_wifi_device *priv) | |
525 | { | |
526 | struct net_device *ndev; | |
527 | ||
528 | if (!priv) | |
529 | return 0; | |
530 | ||
531 | if (!test_bit(ESP_NETWORK_UP, &priv->priv_flags)) | |
532 | return 0; | |
533 | ||
534 | esp_mark_scan_done_and_disconnect(priv, false); | |
535 | esp_port_close(priv); | |
536 | ||
537 | /* stop and unregister network */ | |
538 | ndev = priv->ndev; | |
539 | ||
540 | if (ndev) { | |
541 | netif_carrier_off(ndev); | |
542 | netif_device_detach(ndev); | |
543 | ||
544 | unregister_inetaddr_notifier(&(priv->nb)); | |
545 | } | |
546 | ||
547 | return 0; | |
548 | } | |
549 | ||
550 | int esp_stop_network_ifaces(struct esp_adapter *adapter) | |
551 | { | |
552 | uint8_t iface_idx = 0; | |
553 | ||
554 | for (iface_idx = 0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) { | |
555 | stop_network_iface(adapter->priv[iface_idx]); | |
556 | } | |
557 | ||
558 | rtnl_lock(); | |
559 | if (adapter->wiphy) | |
560 | cfg80211_shutdown_all_interfaces(adapter->wiphy); | |
561 | ||
562 | rtnl_unlock(); | |
563 | ||
564 | return 0; | |
565 | } | |
566 | ||
567 | int esp_remove_card(struct esp_adapter *adapter) | |
568 | { | |
569 | if (!adapter) { | |
570 | return 0; | |
571 | } | |
572 | ||
573 | esp_stop_network_ifaces(adapter); | |
574 | esp_cfg_cleanup(adapter); | |
575 | /* BT may have been initialized after fw bootup event, deinit it */ | |
576 | esp_deinit_bt(adapter); | |
577 | esp_commands_teardown(adapter); | |
578 | esp_remove_network_ifaces(adapter); | |
579 | esp_remove_wiphy(adapter); | |
580 | ||
581 | return 0; | |
582 | } | |
583 | ||
584 | struct esp_wifi_device *get_priv_from_payload_header( | |
585 | struct esp_payload_header *header) | |
586 | { | |
587 | struct esp_wifi_device *priv = NULL; | |
588 | u8 i = 0; | |
589 | ||
590 | if (!header) | |
591 | return NULL; | |
592 | ||
593 | for (i = 0; i < ESP_MAX_INTERFACE; i++) { | |
594 | priv = adapter.priv[i]; | |
595 | ||
596 | if (!priv) | |
597 | continue; | |
598 | ||
599 | if (priv->if_type == header->if_type && | |
600 | priv->if_num == header->if_num) { | |
601 | return priv; | |
602 | } | |
603 | } | |
604 | return NULL; | |
605 | } | |
606 | ||
607 | static void process_esp_bootup_event(struct esp_adapter *adapter, | |
608 | struct esp_internal_bootup_event *evt) | |
609 | { | |
610 | if (!adapter || !evt) { | |
611 | esp_err("Invalid arguments\n"); | |
612 | return; | |
613 | } | |
614 | ||
615 | if (evt->header.status) { | |
616 | esp_err("Incorrect ESP bootup event\n"); | |
617 | return; | |
618 | } | |
619 | ||
620 | esp_info("Received ESP bootup event\n"); | |
621 | process_event_esp_bootup(adapter, evt->data, evt->len); | |
622 | } | |
623 | ||
624 | static int process_internal_event(struct esp_adapter *adapter, | |
625 | struct sk_buff *skb) | |
626 | { | |
627 | struct event_header *header = NULL; | |
628 | ||
629 | if (!skb || !adapter) { | |
630 | esp_err("Incorrect event data!\n"); | |
631 | return -1; | |
632 | } | |
633 | ||
634 | header = (struct event_header *) (skb->data); | |
635 | ||
636 | switch (header->event_code) { | |
637 | ||
638 | case ESP_INTERNAL_BOOTUP_EVENT: | |
639 | process_esp_bootup_event(adapter, | |
640 | (struct esp_internal_bootup_event *)(skb->data)); | |
641 | break; | |
642 | ||
643 | default: | |
644 | esp_info("%u unhandled internal event[%u]\n", | |
645 | __LINE__, header->event_code); | |
646 | break; | |
647 | } | |
648 | ||
649 | return 0; | |
650 | } | |
651 | ||
652 | static void process_rx_packet(struct esp_adapter *adapter, struct sk_buff *skb) | |
653 | { | |
654 | struct esp_wifi_device *priv = NULL; | |
655 | struct esp_payload_header *payload_header = NULL; | |
656 | u16 len = 0, offset = 0; | |
657 | u16 rx_checksum = 0, checksum = 0; | |
658 | struct hci_dev *hdev = adapter->hcidev; | |
659 | u8 *type = NULL; | |
660 | ||
661 | if (!skb) | |
662 | return; | |
663 | ||
664 | /* get the paload header */ | |
665 | payload_header = (struct esp_payload_header *) skb->data; | |
666 | ||
667 | len = le16_to_cpu(payload_header->len); | |
668 | offset = le16_to_cpu(payload_header->offset); | |
669 | ||
670 | if (payload_header->reserved2 == 0xFF) { | |
671 | esp_hex_dump("Wake up packet: ", skb->data, len+offset); | |
672 | } | |
673 | ||
674 | if (adapter->capabilities & ESP_CHECKSUM_ENABLED) { | |
675 | rx_checksum = le16_to_cpu(payload_header->checksum); | |
676 | payload_header->checksum = 0; | |
677 | ||
678 | checksum = compute_checksum(skb->data, (len + offset)); | |
679 | ||
680 | if (checksum != rx_checksum) { | |
681 | dev_kfree_skb_any(skb); | |
682 | return; | |
683 | } | |
684 | } | |
685 | ||
686 | /* chop off the header from skb */ | |
687 | skb_pull(skb, offset); | |
688 | ||
689 | if (payload_header->if_type == ESP_STA_IF || payload_header->if_type == ESP_AP_IF) { | |
690 | ||
691 | /* retrieve priv based on payload header contents */ | |
692 | priv = get_priv_from_payload_header(payload_header); | |
693 | ||
694 | if (!priv) { | |
695 | esp_err("Empty priv\n"); | |
696 | dev_kfree_skb_any(skb); | |
697 | return; | |
698 | } | |
699 | ||
700 | if (payload_header->packet_type == PACKET_TYPE_EAPOL) { | |
701 | esp_dbg("Rx PACKET_TYPE_EAPOL!!!!\n"); | |
702 | esp_port_open(priv); | |
703 | skb->dev = priv->ndev; | |
704 | skb->protocol = eth_type_trans(skb, priv->ndev); | |
705 | netif_rx(skb); | |
706 | ||
707 | } else if (payload_header->packet_type == PACKET_TYPE_DATA) { | |
708 | ||
709 | skb->dev = priv->ndev; | |
710 | skb->protocol = eth_type_trans(skb, priv->ndev); | |
711 | skb->ip_summed = CHECKSUM_NONE; | |
712 | ||
713 | priv->stats.rx_bytes += skb->len; | |
714 | /* Forward skb to kernel */ | |
715 | NETIF_RX_NI(skb); | |
716 | priv->stats.rx_packets++; | |
717 | } else if (payload_header->packet_type == PACKET_TYPE_COMMAND_RESPONSE) { | |
718 | process_cmd_resp(priv->adapter, skb); | |
719 | } else if (payload_header->packet_type == PACKET_TYPE_EVENT) { | |
720 | process_cmd_event(priv, skb); | |
721 | dev_kfree_skb_any(skb); | |
722 | } | |
723 | ||
724 | } else if (payload_header->if_type == ESP_HCI_IF) { | |
725 | if (hdev) { | |
726 | ||
727 | type = skb->data; | |
728 | hci_skb_pkt_type(skb) = *type; | |
729 | skb_pull(skb, 1); | |
730 | ||
731 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)) | |
732 | if (hci_recv_frame(hdev, skb)) { | |
733 | #else | |
734 | if (hci_recv_frame(skb)) { | |
735 | #endif | |
736 | hdev->stat.err_rx++; | |
737 | } else { | |
738 | esp_hci_update_rx_counter(hdev, *type, skb->len); | |
739 | } | |
740 | } | |
741 | } else if (payload_header->if_type == ESP_INTERNAL_IF) { | |
742 | ||
743 | /* Queue event skb for processing in events workqueue */ | |
744 | skb_queue_tail(&adapter->events_skb_q, skb); | |
745 | ||
746 | if (adapter->events_wq) | |
747 | queue_work(adapter->events_wq, &adapter->events_work); | |
748 | else | |
749 | dev_kfree_skb_any(skb); | |
750 | ||
751 | } else if (payload_header->if_type == ESP_TEST_IF) { | |
752 | #if TEST_RAW_TP | |
753 | if (raw_tp_mode != 0) { | |
754 | update_test_raw_tp_rx_stats(len); | |
755 | } | |
756 | #endif | |
757 | dev_kfree_skb_any(skb); | |
758 | } else { | |
759 | dev_kfree_skb_any(skb); | |
760 | } | |
761 | } | |
762 | ||
763 | char *esp_get_hardware_name(int hardware_id) | |
764 | { | |
765 | if(hardware_id == ESP_FIRMWARE_CHIP_ESP32) | |
766 | return "ESP32"; | |
767 | else if(hardware_id == ESP_FIRMWARE_CHIP_ESP32S2) | |
768 | return "ESP32S2"; | |
769 | else if(hardware_id == ESP_FIRMWARE_CHIP_ESP32C3) | |
770 | return "ESP32C3"; | |
771 | else if(hardware_id == ESP_FIRMWARE_CHIP_ESP32S3) | |
772 | return "ESP32S3"; | |
773 | else if(hardware_id == ESP_FIRMWARE_CHIP_ESP32C2) | |
774 | return "ESP32C2"; | |
775 | else if(hardware_id == ESP_FIRMWARE_CHIP_ESP32C6) | |
776 | return "ESP32C6"; | |
777 | else | |
778 | return "N/A"; | |
779 | } | |
780 | ||
781 | bool esp_is_valid_hardware_id(int hardware_id) | |
782 | { | |
783 | switch(hardware_id) { | |
784 | case ESP_FIRMWARE_CHIP_ESP32: | |
785 | case ESP_FIRMWARE_CHIP_ESP32S2: | |
786 | case ESP_FIRMWARE_CHIP_ESP32C3: | |
787 | case ESP_FIRMWARE_CHIP_ESP32S3: | |
788 | case ESP_FIRMWARE_CHIP_ESP32C2: | |
789 | case ESP_FIRMWARE_CHIP_ESP32C6: | |
790 | return true; | |
791 | default: | |
792 | return false; | |
793 | } | |
794 | } | |
795 | ||
796 | int esp_is_tx_queue_paused(struct esp_wifi_device *priv) | |
797 | { | |
798 | if (!priv || !priv->ndev) | |
799 | return 0; | |
800 | ||
801 | if ((priv->ndev && | |
802 | !netif_queue_stopped((const struct net_device *)priv->ndev))) | |
803 | return 1; | |
804 | return 0; | |
805 | } | |
806 | ||
807 | void esp_tx_pause(struct esp_wifi_device *priv) | |
808 | { | |
809 | if (!priv || !priv->ndev) | |
810 | return; | |
811 | ||
812 | if (!netif_queue_stopped((const struct net_device *)priv->ndev)) { | |
813 | netif_stop_queue(priv->ndev); | |
814 | } | |
815 | } | |
816 | ||
817 | void esp_tx_resume(struct esp_wifi_device *priv) | |
818 | { | |
819 | if (!priv || !priv->ndev) | |
820 | return; | |
821 | ||
822 | if (netif_queue_stopped((const struct net_device *)priv->ndev)) { | |
823 | netif_wake_queue(priv->ndev); | |
824 | } | |
825 | } | |
826 | ||
827 | struct sk_buff *esp_alloc_skb(u32 len) | |
828 | { | |
829 | struct sk_buff *skb = NULL; | |
830 | ||
831 | u8 offset; | |
832 | ||
833 | skb = netdev_alloc_skb(NULL, len + INTERFACE_HEADER_PADDING); | |
834 | ||
835 | if (skb) { | |
836 | /* Align SKB data pointer */ | |
837 | offset = ((unsigned long)skb->data) & (SKB_DATA_ADDR_ALIGNMENT - 1); | |
838 | ||
839 | if (offset) | |
840 | skb_reserve(skb, INTERFACE_HEADER_PADDING - offset); | |
841 | } | |
842 | ||
843 | return skb; | |
844 | } | |
845 | ||
846 | ||
847 | static int esp_get_packets(struct esp_adapter *adapter) | |
848 | { | |
849 | struct sk_buff *skb = NULL; | |
850 | ||
851 | if (!adapter || !adapter->if_ops || !adapter->if_ops->read) | |
852 | return -EINVAL; | |
853 | ||
854 | skb = adapter->if_ops->read(adapter); | |
855 | ||
856 | if (!skb) | |
857 | return -EFAULT; | |
858 | ||
859 | process_rx_packet(adapter, skb); | |
860 | ||
861 | return 0; | |
862 | } | |
863 | ||
864 | int esp_send_packet(struct esp_adapter *adapter, struct sk_buff *skb) | |
865 | { | |
866 | if (!adapter || !adapter->if_ops || !adapter->if_ops->write) { | |
867 | esp_err("%u adapter: %p\n", __LINE__, adapter); | |
868 | return -EINVAL; | |
869 | } | |
870 | ||
871 | return adapter->if_ops->write(adapter, skb); | |
872 | } | |
873 | ||
874 | static void esp_if_rx_work(struct work_struct *work) | |
875 | { | |
876 | /* read inbound packet and forward it to network/serial interface */ | |
877 | esp_get_packets(&adapter); | |
878 | } | |
879 | ||
880 | static void update_mac_filter(struct work_struct *work) | |
881 | { | |
882 | struct esp_adapter *adapter = esp_get_adapter(); | |
883 | struct esp_wifi_device *priv = adapter->priv[0]; | |
884 | struct net_device *ndev; | |
885 | struct netdev_hw_addr *mac_addr; | |
886 | u32 count = 0; | |
887 | ||
888 | if (!priv) | |
889 | return; | |
890 | ||
891 | ndev = priv->ndev; | |
892 | if (!ndev) | |
893 | return; | |
894 | ||
895 | if (!priv->port_open) { | |
896 | esp_verbose("Port is not open yet, skipping mac filter update\n"); | |
897 | return; | |
898 | } | |
899 | ||
900 | #if CONFIG_ALLOW_MULTICAST_WAKEUP | |
901 | netdev_for_each_mc_addr(mac_addr, ndev) { | |
902 | if (count < MAX_MULTICAST_ADDR_COUNT) { | |
903 | esp_verbose("%d: "MACSTR"\n", count+1, MAC2STR(mac_addr->addr)); | |
904 | memcpy(&mcast_list.mcast_addr[count++], mac_addr->addr, ETH_ALEN); | |
905 | } | |
906 | } | |
907 | ||
908 | mcast_list.priv = priv; | |
909 | mcast_list.addr_count = count; | |
910 | ||
911 | esp_verbose("Setting Multicast list\n"); | |
912 | cmd_set_mcast_mac_list(mcast_list.priv, &mcast_list); | |
913 | #else | |
914 | esp_info("Not setting FW multicast addresses\n"); | |
915 | #endif | |
916 | } | |
917 | ||
918 | static void esp_events_work(struct work_struct *work) | |
919 | { | |
920 | struct sk_buff *skb = NULL; | |
921 | ||
922 | skb = skb_dequeue(&adapter.events_skb_q); | |
923 | if (!skb) | |
924 | return; | |
925 | ||
926 | process_internal_event(&adapter, skb); | |
927 | dev_kfree_skb_any(skb); | |
928 | } | |
929 | ||
930 | static struct esp_adapter *init_adapter(void) | |
931 | { | |
932 | memset(&adapter, 0, sizeof(adapter)); | |
933 | ||
934 | /* Prepare interface RX work */ | |
935 | adapter.if_rx_workqueue = alloc_workqueue("ESP_IF_RX_WORK_QUEUE", 0, 0); | |
936 | ||
937 | if (!adapter.if_rx_workqueue) { | |
938 | deinit_adapter(); | |
939 | return NULL; | |
940 | } | |
941 | ||
942 | INIT_WORK(&adapter.if_rx_work, esp_if_rx_work); | |
943 | ||
944 | skb_queue_head_init(&adapter.events_skb_q); | |
945 | ||
946 | adapter.events_wq = alloc_workqueue("ESP_EVENTS_WORKQUEUE", WQ_HIGHPRI, 0); | |
947 | ||
948 | if (!adapter.events_wq) { | |
949 | deinit_adapter(); | |
950 | return NULL; | |
951 | } | |
952 | ||
953 | INIT_WORK(&adapter.events_work, esp_events_work); | |
954 | ||
955 | INIT_WORK(&adapter.mac_flter_work, update_mac_filter); | |
956 | ||
957 | return &adapter; | |
958 | } | |
959 | ||
960 | static void deinit_adapter(void) | |
961 | { | |
962 | skb_queue_purge(&adapter.events_skb_q); | |
963 | ||
964 | if (adapter.events_wq) | |
965 | destroy_workqueue(adapter.events_wq); | |
966 | ||
967 | if (adapter.if_rx_workqueue) | |
968 | destroy_workqueue(adapter.if_rx_workqueue); | |
969 | } | |
970 | ||
971 | static void esp_reset(void) | |
972 | { | |
973 | if (resetpin != HOST_GPIO_PIN_INVALID) { | |
974 | /* Check valid GPIO or not */ | |
975 | if (!gpio_is_valid(resetpin)) { | |
976 | esp_warn("host resetpin (%d) configured is invalid GPIO\n", resetpin); | |
977 | resetpin = HOST_GPIO_PIN_INVALID; | |
978 | } else { | |
979 | gpio_request(resetpin, "sysfs"); | |
980 | ||
981 | /* HOST's resetpin set to OUTPUT, HIGH */ | |
982 | gpio_direction_output(resetpin, true); | |
983 | ||
984 | /* HOST's resetpin set to LOW */ | |
985 | gpio_set_value(resetpin, 0); | |
986 | udelay(200); | |
987 | ||
988 | /* HOST's resetpin set to INPUT */ | |
989 | gpio_direction_input(resetpin); | |
990 | ||
991 | esp_dbg("Triggering ESP reset.\n"); | |
992 | } | |
993 | } | |
994 | } | |
995 | ||
996 | void dt_get_reset_pin(void) | |
997 | { | |
998 | struct device_node *resetpin_node = NULL; | |
999 | int temp_resetpin = HOST_GPIO_PIN_INVALID; | |
1000 | ||
1001 | ||
1002 | resetpin_node = of_find_compatible_node(NULL, NULL, "espressif,esp_sdio"); | |
1003 | if (!resetpin_node) | |
1004 | resetpin_node = of_find_compatible_node(NULL, NULL, "espressif,esp_spi"); | |
1005 | if (resetpin_node){ | |
1006 | of_property_read_u32(resetpin_node, "resetpin", &temp_resetpin); | |
1007 | if (temp_resetpin == HOST_GPIO_PIN_INVALID) | |
1008 | esp_warn("Unable to find resetpin in device tree.\n"); | |
1009 | else | |
1010 | resetpin = temp_resetpin; | |
1011 | } | |
1012 | } | |
1013 | ||
1014 | static int __init esp_init(void) | |
1015 | { | |
1016 | int ret = 0; | |
1017 | struct esp_adapter *adapter = NULL; | |
1018 | struct esp_if_params if_params; | |
1019 | ||
1020 | dt_get_reset_pin(); | |
1021 | ||
1022 | /* Reset ESP, Clean start ESP */ | |
1023 | esp_reset(); | |
1024 | msleep(200); | |
1025 | ||
1026 | adapter = init_adapter(); | |
1027 | ||
1028 | if (!adapter) | |
1029 | return -EFAULT; | |
1030 | ||
1031 | if_params.speed = clockspeed; | |
1032 | if_params.handshake_pin = handshakepin; | |
1033 | if_params.data_ready_pin = readypin; | |
1034 | /* Init transport layer */ | |
1035 | ret = esp_init_interface_layer(adapter, &if_params); | |
1036 | ||
1037 | if (ret != 0) { | |
1038 | deinit_adapter(); | |
1039 | } | |
1040 | ||
1041 | ret = debugfs_init(); | |
1042 | return ret; | |
1043 | } | |
1044 | ||
1045 | static void __exit esp_exit(void) | |
1046 | { | |
1047 | uint8_t iface_idx = 0; | |
1048 | #if TEST_RAW_TP | |
1049 | if (raw_tp_mode != 0) { | |
1050 | test_raw_tp_cleanup(); | |
1051 | } | |
1052 | #endif | |
1053 | for (iface_idx = 0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) { | |
1054 | cmd_deinit_interface(adapter.priv[iface_idx]); | |
1055 | } | |
1056 | clear_bit(ESP_DRIVER_ACTIVE, &adapter.state_flags); | |
1057 | ||
1058 | esp_deinit_interface_layer(); | |
1059 | deinit_adapter(); | |
1060 | ||
1061 | if (resetpin != HOST_GPIO_PIN_INVALID) { | |
1062 | gpio_free(resetpin); | |
1063 | } | |
1064 | debugfs_exit(); | |
1065 | } | |
1066 | MODULE_LICENSE("GPL"); | |
1067 | MODULE_AUTHOR("Amey Inamdar <[email protected]>"); | |
1068 | MODULE_AUTHOR("Mangesh Malusare <[email protected]>"); | |
1069 | MODULE_AUTHOR("Yogesh Mantri <[email protected]>"); | |
1070 | MODULE_AUTHOR("Kapil Gupta <[email protected]>"); | |
1071 | MODULE_DESCRIPTION("Wifi driver for ESP-Hosted solution"); | |
1072 | MODULE_VERSION(RELEASE_VERSION); | |
1073 | module_init(esp_init); | |
1074 | module_exit(esp_exit); |