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