]> Git Repo - esp-hosted.git/blobdiff - esp_hosted_ng/host/main.c
Allow to specify reset pins via DT
[esp-hosted.git] / esp_hosted_ng / host / main.c
index 9bb99c6040673b04ded6b6efdd133e6b529a06dd..977f6cb6ef4a4d29458cb8a4fb2e68eadc1fa154 100644 (file)
@@ -1,22 +1,11 @@
+// SPDX-License-Identifier: GPL-2.0-only
 /*
  * Espressif Systems Wireless LAN device driver
  *
- * Copyright (C) 2015-2021 Espressif Systems (Shanghai) PTE LTD
+ * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  *
- * This software file (the "File") is distributed by Espressif Systems (Shanghai)
- * PTE LTD under the terms of the GNU General Public License Version 2, June 1991
- * (the "License").  You may use, redistribute and/or modify this File in
- * accordance with the terms and conditions of the License, a copy of which
- * is available by writing to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
- * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
- *
- * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
- * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
- * this warranty disclaimer.
  */
-
+#include "utils.h"
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include "esp_cfg80211.h"
 #include "esp_stats.h"
 
+#define RELEASE_VERSION "1.0.3"
 #define HOST_GPIO_PIN_INVALID -1
+#define CONFIG_ALLOW_MULTICAST_WAKEUP 1
 static int resetpin = HOST_GPIO_PIN_INVALID;
+static int readypin = HOST_GPIO_PIN_INVALID;
+static int handshakepin = HOST_GPIO_PIN_INVALID;
+static u32 clockspeed = 0;
 extern u8 ap_bssid[MAC_ADDR_LEN];
 extern volatile u8 host_sleep;
+u32 raw_tp_mode = 0;
+int log_level = ESP_INFO;
 
 module_param(resetpin, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
 MODULE_PARM_DESC(resetpin, "Host's GPIO pin number which is connected to ESP32's EN to reset ESP32 device");
 
+module_param(clockspeed, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+MODULE_PARM_DESC(clockspeed, "Hosts clock speed in MHz");
+
+module_param(raw_tp_mode, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+MODULE_PARM_DESC(raw_tp_mode, "Mode choosed to test raw throughput");
+
+module_param(readypin, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+MODULE_PARM_DESC(readypin, "Data ready pin default is pin 13, gpio27");
+
+module_param(handshakepin, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+MODULE_PARM_DESC(handshakepin, "Handshake pin default is pin 15, gpio22");
+
 static void deinit_adapter(void);
 
 
@@ -48,18 +56,18 @@ struct multicast_list mcast_list = {0};
 struct esp_adapter adapter;
 /*struct esp_device esp_dev;*/
 
-struct esp_adapter * esp_get_adapter(void)
+struct esp_adapter *esp_get_adapter(void)
 {
        return &adapter;
 }
 
 void esp_process_new_packet_intr(struct esp_adapter *adapter)
 {
-       if(adapter)
+       if (adapter)
                queue_work(adapter->if_rx_workqueue, &adapter->if_rx_work);
 }
 
-static int process_tx_packet (struct sk_buff *skb)
+static int process_tx_packet(struct sk_buff *skb)
 {
        struct esp_wifi_device *priv = NULL;
        struct esp_skb_cb *cb = NULL;
@@ -69,7 +77,7 @@ static int process_tx_packet (struct sk_buff *skb)
        u8 pad_len = 0, realloc_skb = 0;
        u16 len = 0;
        u16 total_len = 0;
-       static u8 c = 0;
+       static u8 c;
        u8 *pos = NULL;
 
        c++;
@@ -79,12 +87,12 @@ static int process_tx_packet (struct sk_buff *skb)
 
        if (!priv) {
                dev_kfree_skb(skb);
-               printk(KERN_INFO "%s: no priv\n", __func__);
+               esp_info("No priv\n");
                return NETDEV_TX_OK;
        }
 
        if (netif_queue_stopped((const struct net_device *) priv->ndev)) {
-               printk(KERN_INFO "%s: Netif queue stopped\n", __func__);
+               esp_info("Netif queue stopped\n");
                return NETDEV_TX_BUSY;
        }
 
@@ -112,14 +120,14 @@ static int process_tx_packet (struct sk_buff *skb)
                if (skb_linearize(skb)) {
                        priv->stats.tx_errors++;
                        dev_kfree_skb(skb);
-                       printk(KERN_ERR "%s: Failed to linearize SKB", __func__);
+                       esp_err("Failed to linearize SKB");
                        return NETDEV_TX_OK;
                }
 
                new_skb = esp_alloc_skb(skb->len + pad_len);
 
                if (!new_skb) {
-                       printk(KERN_ERR "%s: Failed to allocate SKB", __func__);
+                       esp_err("Failed to allocate SKB");
                        priv->stats.tx_errors++;
                        dev_kfree_skb(skb);
                        return NETDEV_TX_OK;
@@ -157,7 +165,7 @@ static int process_tx_packet (struct sk_buff *skb)
                ret = esp_send_packet(priv->adapter, skb);
 
                if (ret) {
-/*                     printk(KERN_ERR "%s: Failed to send SKB", __func__);*/
+                       esp_verbose("Failed to send SKB");
                        priv->stats.tx_errors++;
                } else {
                        priv->stats.tx_packets++;
@@ -171,13 +179,13 @@ static int process_tx_packet (struct sk_buff *skb)
        return 0;
 }
 
-void esp_port_open(struct esp_wifi_device * priv)
+void esp_port_open(struct esp_wifi_device *priv)
 {
        priv->port_open = 1;
        priv->stop_data = 0;
 }
 
-void esp_port_close(struct esp_wifi_device * priv)
+void esp_port_close(struct esp_wifi_device *priv)
 {
        if (!priv)
                return;
@@ -188,53 +196,49 @@ void esp_port_close(struct esp_wifi_device * priv)
 
 void print_capabilities(u32 cap)
 {
-       printk(KERN_INFO "Capabilities: 0x%x. Features supported are:\n", cap);
+       esp_info("Capabilities: 0x%x. Features supported are:\n", cap);
        if (cap & ESP_WLAN_SDIO_SUPPORT)
-               printk(KERN_INFO "\t * WLAN on SDIO\n");
+               esp_info("\t * WLAN on SDIO\n");
        else if (cap & ESP_WLAN_SPI_SUPPORT)
-               printk(KERN_INFO "\t * WLAN on SPI\n");
+               esp_info("\t * WLAN on SPI\n");
 
        if ((cap & ESP_BT_UART_SUPPORT) ||
                    (cap & ESP_BT_SDIO_SUPPORT) ||
                    (cap & ESP_BT_SPI_SUPPORT)) {
-               printk(KERN_INFO "\t * BT/BLE\n");
+               esp_info("\t * BT/BLE\n");
                if (cap & ESP_BT_UART_SUPPORT)
-                       printk(KERN_INFO "\t   - HCI over UART\n");
+                       esp_info("\t   - HCI over UART\n");
                if (cap & ESP_BT_SDIO_SUPPORT)
-                       printk(KERN_INFO "\t   - HCI over SDIO\n");
+                       esp_info("\t   - HCI over SDIO\n");
                if (cap & ESP_BT_SPI_SUPPORT)
-                       printk(KERN_INFO "\t   - HCI over SPI\n");
+                       esp_info("\t   - HCI over SPI\n");
 
                if ((cap & ESP_BLE_ONLY_SUPPORT) && (cap & ESP_BR_EDR_ONLY_SUPPORT))
-                       printk(KERN_INFO "\t   - BT/BLE dual mode\n");
+                       esp_info("\t   - BT/BLE dual mode\n");
                else if (cap & ESP_BLE_ONLY_SUPPORT)
-                       printk(KERN_INFO "\t   - BLE only\n");
+                       esp_info("\t   - BLE only\n");
                else if (cap & ESP_BR_EDR_ONLY_SUPPORT)
-                       printk(KERN_INFO "\t   - BR EDR only\n");
+                       esp_info("\t   - BR EDR only\n");
        }
 }
 
-void process_capabilities(struct esp_adapter *adapter)
+void init_bt(struct esp_adapter *adapter)
 {
-       printk(KERN_INFO "ESP peripheral capabilities: 0x%x\n", adapter->capabilities);
-
-       /* Reset BT */
-       esp_deinit_bt(adapter);
 
        if ((adapter->capabilities & ESP_BT_SPI_SUPPORT) ||
                (adapter->capabilities & ESP_BT_SDIO_SUPPORT)) {
                msleep(200);
-               printk(KERN_INFO "ESP Bluetooth init\n");
+               esp_info("ESP Bluetooth init\n");
                esp_init_bt(adapter);
        }
 }
 
 static int check_esp_version(struct fw_version *ver)
 {
-       printk(KERN_INFO "esp32: ESP Firmware version: %u.%u.%u\n",
+       esp_info("ESP Firmware version: %u.%u.%u\n",
                        ver->major1, ver->major2, ver->minor);
        if (!ver->major1) {
-               printk(KERN_ERR "Incompatible ESP firmware release detected, Please use correct ESP-Hosted branch/compatible release\n");
+               esp_err("Incompatible ESP firmware release detected, Please use correct ESP-Hosted branch/compatible release\n");
                return -1;
        }
        return 0;
@@ -244,37 +248,109 @@ static void print_reset_reason(uint32_t reason)
 {
        switch (reason)
        {
-               case 1 : printk(KERN_INFO  "POWERON_RESET\n");break;          /**<1, Vbat power on reset*/
-               case 3 : printk(KERN_INFO  "SW_RESET\n");break;               /**<3, Software reset digital core*/
-               case 4 : printk(KERN_INFO  "OWDT_RESET\n");break;             /**<4, Legacy watch dog reset digital core*/
-               case 5 : printk(KERN_INFO  "DEEPSLEEP_RESET\n");break;        /**<5, Deep Sleep reset digital core*/
-               case 6 : printk(KERN_INFO  "SDIO_RESET\n");break;             /**<6, Reset by SLC module, reset digital core*/
-               case 7 : printk(KERN_INFO  "TG0WDT_SYS_RESET\n");break;       /**<7, Timer Group0 Watch dog reset digital core*/
-               case 8 : printk(KERN_INFO  "TG1WDT_SYS_RESET\n");break;       /**<8, Timer Group1 Watch dog reset digital core*/
-               case 9 : printk(KERN_INFO  "RTCWDT_SYS_RESET\n");break;       /**<9, RTC Watch dog Reset digital core*/
-               case 10 : printk(KERN_INFO  "INTRUSION_RESET\n");break;       /**<10, Instrusion tested to reset CPU*/
-               case 11 : printk(KERN_INFO  "TGWDT_CPU_RESET\n");break;       /**<11, Time Group reset CPU*/
-               case 12 : printk(KERN_INFO  "SW_CPU_RESET\n");break;          /**<12, Software reset CPU*/
-               case 13 : printk(KERN_INFO  "RTCWDT_CPU_RESET\n");break;      /**<13, RTC Watch dog Reset CPU*/
-               case 14 : printk(KERN_INFO  "EXT_CPU_RESET\n");break;         /**<14, for APP CPU, reseted by PRO CPU*/
-               case 15 : printk(KERN_INFO  "RTCWDT_BROWN_OUT_RESET\n");break;/**<15, Reset when the vdd voltage is not stable*/
-               case 16 : printk(KERN_INFO  "RTCWDT_RTC_RESET\n");break;      /**<16, RTC Watch dog reset digital core and rtc module*/
-               default : printk(KERN_INFO  "Unknown[%u]\n",reason);break;
-       }
-}
-
-int process_fw_data(struct fw_data *fw_p)
-{
-       if (!fw_p) {
-               printk(KERN_ERR "Incomplete/incorrect bootup event received\n");
+               case 1: esp_info("POWERON_RESET\n"); break;          /**<1, Vbat power on reset*/
+               case 3: esp_info("SW_RESET\n"); break;               /**<3, Software reset digital core*/
+               case 4: esp_info("OWDT_RESET\n"); break;             /**<4, Legacy watch dog reset digital core*/
+               case 5: esp_info("DEEPSLEEP_RESET\n"); break;        /**<5, Deep Sleep reset digital core*/
+               case 6: esp_info("SDIO_RESET\n"); break;             /**<6, Reset by SLC module, reset digital core*/
+               case 7: esp_info("TG0WDT_SYS_RESET\n"); break;       /**<7, Timer Group0 Watch dog reset digital core*/
+               case 8: esp_info("TG1WDT_SYS_RESET\n"); break;       /**<8, Timer Group1 Watch dog reset digital core*/
+               case 9: esp_info("RTCWDT_SYS_RESET\n"); break;       /**<9, RTC Watch dog Reset digital core*/
+               case 10: esp_info("INTRUSION_RESET\n"); break;       /**<10, Instrusion tested to reset CPU*/
+               case 11: esp_info("TGWDT_CPU_RESET\n"); break;       /**<11, Time Group reset CPU*/
+               case 12: esp_info("SW_CPU_RESET\n"); break;          /**<12, Software reset CPU*/
+               case 13: esp_info("RTCWDT_CPU_RESET\n"); break;      /**<13, RTC Watch dog Reset CPU*/
+               case 14: esp_info("EXT_CPU_RESET\n"); break;         /**<14, for APP CPU, reseted by PRO CPU*/
+               case 15: esp_info("RTCWDT_BROWN_OUT_RESET\n"); break;/**<15, Reset when the vdd voltage is not stable*/
+               case 16: esp_info("RTCWDT_RTC_RESET\n"); break;      /**<16, RTC Watch dog reset digital core and rtc module*/
+               default: esp_info("Unknown[%u]\n", reason); break;
+       }
+}
+
+static int process_fw_data(struct fw_data *fw_p, int tag_len)
+{
+       if (tag_len != sizeof(struct fw_data)) {
+               esp_err("Length not matching to firmware data size\n");
                return -1;
        }
 
-       printk(KERN_INFO "esp32: %s ESP chipset's last reset cause: ", __func__);
+       esp_info("ESP chipset's last reset cause:\n");
        print_reset_reason(le32_to_cpu(fw_p->last_reset_reason));
+
        return check_esp_version(&fw_p->version);
 }
 
+int process_event_esp_bootup(struct esp_adapter *adapter, u8 *evt_buf, u8 len)
+{
+       int len_left = len, tag_len, ret = 0;
+       u8 *pos;
+
+       if (!adapter || !evt_buf)
+               return -1;
+
+       if (len_left >= 64) {
+               esp_info("ESP init event len looks unexpected: %u (>=64)\n", len_left);
+               esp_info("You probably facing timing mismatch at transport layer\n");
+       }
+
+       clear_bit(ESP_INIT_DONE, &adapter->state_flags);
+       /* Deinit module if already initialized */
+       test_raw_tp_cleanup();
+       esp_deinit_module(adapter);
+
+       pos = evt_buf;
+
+       while (len_left > 0) {
+               tag_len = *(pos + 1);
+
+               esp_info("Bootup Event tag: %d\n", *pos);
+
+               switch (*pos) {
+               case ESP_BOOTUP_CAPABILITY:
+                       adapter->capabilities = *(pos + 2);
+                       break;
+               case ESP_BOOTUP_FIRMWARE_CHIP_ID:
+                       ret = esp_validate_chipset(adapter, *(pos + 2));
+                       break;
+               case ESP_BOOTUP_FW_DATA:
+                       ret = process_fw_data((struct fw_data *)(pos + 2), tag_len);
+                       break;
+               case ESP_BOOTUP_SPI_CLK_MHZ:
+                       ret = esp_adjust_spi_clock(adapter, *(pos + 2));
+                       break;
+               default:
+                       esp_warn("Unsupported tag=%x in bootup event\n", *pos);
+               }
+
+               if (ret < 0) {
+                       esp_err("failed to process tag=%x in bootup event\n", *pos);
+                       return -1;
+               }
+               pos += (tag_len + 2);
+               len_left -= (tag_len + 2);
+       }
+
+       if (esp_add_card(adapter)) {
+               esp_err("network iterface init failed\n");
+               return -1;
+       }
+       init_bt(adapter);
+
+       if (raw_tp_mode !=0) {
+#if TEST_RAW_TP
+               process_test_capabilities(raw_tp_mode);
+               esp_init_raw_tp(adapter);
+#else
+               esp_err("RAW TP mode selected but not enabled\n");
+               return -1;
+#endif
+       }
+       set_bit(ESP_INIT_DONE, &adapter->state_flags);
+       print_capabilities(adapter->capabilities);
+
+       return 0;
+}
+
 static int esp_open(struct net_device *ndev)
 {
        return 0;
@@ -282,12 +358,10 @@ static int esp_open(struct net_device *ndev)
 
 static int esp_stop(struct net_device *ndev)
 {
-       struct esp_wifi_device *priv = netdev_priv(ndev);
-       ESP_MARK_SCAN_DONE(priv, true);
        return 0;
 }
 
-static struct net_device_statsesp_get_stats(struct net_device *ndev)
+static struct net_device_stats *esp_get_stats(struct net_device *ndev)
 {
        struct esp_wifi_device *priv = netdev_priv(ndev);
 
@@ -300,54 +374,27 @@ static struct net_device_stats* esp_get_stats(struct net_device *ndev)
 static int esp_set_mac_address(struct net_device *ndev, void *data)
 {
        struct esp_wifi_device *priv = netdev_priv(ndev);
-       //struct sockaddr *mac_addr = data;
+       struct sockaddr *sa = (struct sockaddr *)data;
+       int ret;
 
        if (!priv || !priv->adapter)
                return -EINVAL;
 
-       printk(KERN_INFO "%s:%u %pM\n", __func__, __LINE__, priv->mac_address);
-       /* TODO Handle in correct way */
-       ether_addr_copy(ndev->dev_addr, priv->mac_address/*mac_addr->sa_data*/);
+       esp_info("%u "MACSTR"\n", __LINE__, MAC2STR(sa->sa_data));
 
-       return 0;
-}
+       ret = cmd_set_mac(priv, sa->sa_data);
 
-static NDO_TX_TIMEOUT_PROTOTYPE()
-{
+       if (ret == 0)
+               eth_hw_addr_set(ndev, priv->mac_address/*mac_addr->sa_data*/);
+
+       return ret;
 }
 
 static void esp_set_rx_mode(struct net_device *ndev)
 {
-       struct esp_wifi_device *priv = netdev_priv(ndev);
-       struct netdev_hw_addr *mac_addr;
-       u32 count = 0;
-#if 0
-       struct in_device *in_dev = in_dev_get(ndev);
-       struct ip_mc_list *ip_list = in_dev->mc_list;
-#endif
-       netdev_for_each_mc_addr(mac_addr, ndev) {
-               if (count < MAX_MULTICAST_ADDR_COUNT) {
-                       /*printk(KERN_INFO "%d: %pM\n", count+1, mac_addr->addr);*/
-                       memcpy(&mcast_list.mcast_addr[count++], mac_addr->addr, ETH_ALEN);
-               }
-       }
-
-       mcast_list.priv = priv;
-       mcast_list.addr_count = count;
-
-       if (priv->port_open) {
-               /*printk (KERN_INFO "Set Multicast list\n");*/
-               if (adapter.mac_filter_wq)
-                       queue_work(adapter.mac_filter_wq, &adapter.mac_flter_work);
-       }
-#if 0
-       cmd_set_mcast_mac_list(priv, &mcast_list);
-       while(ip_list) {
-               printk(KERN_DEBUG " IP MC Address: 0x%x\n", ip_list->multiaddr);
-               ip_list = ip_list->next;
-       }
-#endif
+        struct esp_adapter *adapter = esp_get_adapter();
 
+       schedule_work(&adapter->mac_flter_work);
 }
 
 static int esp_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
@@ -366,13 +413,13 @@ static int esp_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
 
        if (!priv->port_open) {
                priv->stats.tx_dropped++;
-               /*printk(KERN_ERR "esp32: %s: port not yet open\n", __func__);*/
+               esp_verbose("Port not yet open\n");
                dev_kfree_skb(skb);
                return NETDEV_TX_OK;
        }
 
        if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
-               printk(KERN_ERR "esp32: %s: Bad len %d\n", __func__, skb->len);
+               esp_err("Bad len %d\n", skb->len);
                priv->stats.tx_dropped++;
                dev_kfree_skb(skb);
                return NETDEV_TX_OK;
@@ -390,7 +437,6 @@ static const struct net_device_ops esp_netdev_ops = {
        .ndo_start_xmit = esp_hard_start_xmit,
        .ndo_set_mac_address = esp_set_mac_address,
        .ndo_validate_addr = eth_validate_addr,
-       .ndo_tx_timeout = esp_tx_timeout,
        .ndo_get_stats = esp_get_stats,
        .ndo_set_rx_mode = esp_set_rx_mode,
 };
@@ -403,25 +449,17 @@ void esp_init_priv(struct net_device *ndev)
                        INTERFACE_HEADER_PADDING, 4);
 }
 
-static int add_network_iface(void)
+static int esp_add_network_ifaces(struct esp_adapter *adapter)
 {
-       int ret = 0;
-       struct esp_adapter * adapter = esp_get_adapter();
-       struct wireless_dev * wdev = NULL;
+       struct wireless_dev *wdev = NULL;
 
        if (!adapter) {
-               printk(KERN_INFO "%s: adapter not yet init\n", __func__);
+               esp_info("adapter not yet init\n");
                return -EINVAL;
        }
 
-       ret = esp_cfg80211_register(adapter);
-       if (ret) {
-               printk(KERN_ERR "Failed to register with cfg80211 (err code 0x%x)\n", ret);
-               return ret;
-       }
-
        rtnl_lock();
-       wdev = esp_cfg80211_add_iface(adapter->wiphy, "espsta%d", 1, NL80211_IFTYPE_STATION, NULL);
+       wdev = esp_cfg80211_add_iface(adapter->wiphy, "wlan%d", 1, NL80211_IFTYPE_STATION, NULL);
        rtnl_unlock();
 
        /* Return success if network added successfully */
@@ -431,82 +469,119 @@ static int add_network_iface(void)
        return -1;
 }
 
+int esp_init_raw_tp(struct esp_adapter *adapter)
+{
+       RET_ON_FAIL(cmd_init_raw_tp_task_timer(adapter->priv[ESP_STA_NW_IF]));
+       return 0;
+}
+
 int esp_add_card(struct esp_adapter *adapter)
 {
        RET_ON_FAIL(esp_commands_setup(adapter));
-
-       RET_ON_FAIL(add_network_iface());
+       RET_ON_FAIL(esp_add_wiphy(adapter));
+       RET_ON_FAIL(esp_add_network_ifaces(adapter));
+       clear_bit(ESP_CLEANUP_IN_PROGRESS, &adapter->state_flags);
 
        return 0;
 }
 
-void esp_remove_network_interfaces(struct esp_adapter *adapter)
+static int esp_remove_network_ifaces(struct esp_adapter *adapter)
 {
        uint8_t iface_idx = 0;
        struct net_device *ndev = NULL;
        struct esp_wifi_device *priv = NULL;
 
-       for (iface_idx=0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) {
+       for (iface_idx = 0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) {
 
                priv = adapter->priv[iface_idx];
-
                if (!priv)
                        continue;
-
                if (!test_bit(ESP_NETWORK_UP, &priv->priv_flags))
                        continue;
 
-               /* stop and unregister network */
                ndev = priv->ndev;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
+               if (ndev)
+                       ndev->needs_free_netdev = true;
+               rtnl_lock();
+               wiphy_lock(adapter->wiphy);
+               cfg80211_unregister_wdev(&priv->wdev);
+               wiphy_unlock(adapter->wiphy);
+               rtnl_unlock();
+#else
+               if (ndev && ndev->reg_state == NETREG_REGISTERED) {
+                       unregister_netdev(ndev);
+                       free_netdev(ndev);
+                       ndev = NULL;
+               }
+#endif
+               adapter->priv[iface_idx] = NULL;
+       }
 
-               if (ndev) {
+       return 0;
+}
 
-                       if (netif_carrier_ok(ndev))
-                               netif_carrier_off(ndev);
+static int stop_network_iface(struct esp_wifi_device *priv)
+{
+       struct net_device *ndev;
 
-                       netif_device_detach(ndev);
+       if (!priv)
+               return 0;
 
-                       if (ndev->reg_state == NETREG_REGISTERED) {
-                               unregister_inetaddr_notifier(&(adapter->priv[0]->nb));
-                               unregister_netdev(ndev);
-                               free_netdev(ndev);
-                               ndev = NULL;
-                       }
-               }
-               clear_bit(ESP_NETWORK_UP, &priv->priv_flags);
-       }
+       if (!test_bit(ESP_NETWORK_UP, &priv->priv_flags))
+               return 0;
 
-       if (adapter->wiphy) {
+       esp_mark_scan_done_and_disconnect(priv, false);
+       esp_port_close(priv);
 
-               wiphy_unregister(adapter->wiphy);
-               wiphy_free(adapter->wiphy);
-               adapter->wiphy = NULL;
+       /* stop and unregister network */
+       ndev = priv->ndev;
+
+       if (ndev) {
+               netif_carrier_off(ndev);
+               netif_device_detach(ndev);
+
+               unregister_inetaddr_notifier(&(priv->nb));
        }
+
+       return 0;
 }
 
-int esp_remove_card(struct esp_adapter *adapter)
+int esp_stop_network_ifaces(struct esp_adapter *adapter)
 {
        uint8_t iface_idx = 0;
 
-       if (!adapter) {
-               return 0;
+       for (iface_idx = 0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) {
+               stop_network_iface(adapter->priv[iface_idx]);
        }
 
-       esp_deinit_bt(adapter);
+       rtnl_lock();
+       if (adapter->wiphy)
+               cfg80211_shutdown_all_interfaces(adapter->wiphy);
 
-       esp_commands_teardown(adapter);
+       rtnl_unlock();
 
-       esp_remove_network_interfaces(adapter);
+       return 0;
+}
 
-       for (iface_idx=0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) {
-               esp_port_close(adapter->priv[iface_idx]);
-               adapter->priv[iface_idx] = NULL;
+int esp_remove_card(struct esp_adapter *adapter)
+{
+       if (!adapter) {
+               return 0;
        }
 
+       esp_stop_network_ifaces(adapter);
+       esp_cfg_cleanup(adapter);
+       /* BT may have been initialized after fw bootup event, deinit it */
+       esp_deinit_bt(adapter);
+       esp_commands_teardown(adapter);
+       esp_remove_network_ifaces(adapter);
+       esp_remove_wiphy(adapter);
+
        return 0;
 }
 
-struct esp_wifi_device * get_priv_from_payload_header(
+struct esp_wifi_device *get_priv_from_payload_header(
                struct esp_payload_header *header)
 {
        struct esp_wifi_device *priv = NULL;
@@ -533,16 +608,16 @@ static void process_esp_bootup_event(struct esp_adapter *adapter,
                struct esp_internal_bootup_event *evt)
 {
        if (!adapter || !evt) {
-               printk(KERN_ERR "%s: Invalid arguments\n", __func__);
+               esp_err("Invalid arguments\n");
                return;
        }
 
        if (evt->header.status) {
-               printk(KERN_ERR "%s: Incorrect ESP bootup event\n", __func__);
+               esp_err("Incorrect ESP bootup event\n");
                return;
        }
 
-       printk (KERN_INFO "\nReceived ESP bootup event\n");
+       esp_info("Received ESP bootup event\n");
        process_event_esp_bootup(adapter, evt->data, evt->len);
 }
 
@@ -552,7 +627,7 @@ static int process_internal_event(struct esp_adapter *adapter,
        struct event_header *header = NULL;
 
        if (!skb || !adapter) {
-               printk (KERN_ERR "esp32: Incorrect event data!\n");
+               esp_err("Incorrect event data!\n");
                return -1;
        }
 
@@ -566,8 +641,8 @@ static int process_internal_event(struct esp_adapter *adapter,
                break;
 
        default:
-               printk(KERN_INFO "%s:%u unhandled internal event[%u]\n",
-                               __func__, __LINE__, header->event_code);
+               esp_info("%u unhandled internal event[%u]\n",
+                               __LINE__, header->event_code);
                break;
        }
 
@@ -582,8 +657,6 @@ static void process_rx_packet(struct esp_adapter *adapter, struct sk_buff *skb)
        u16 rx_checksum = 0, checksum = 0;
        struct hci_dev *hdev = adapter->hcidev;
        u8 *type = NULL;
-       struct sk_buff * eap_skb = NULL;
-       struct ethhdr * eth = NULL;
 
        if (!skb)
                return;
@@ -595,7 +668,7 @@ static void process_rx_packet(struct esp_adapter *adapter, struct sk_buff *skb)
        offset = le16_to_cpu(payload_header->offset);
 
        if (payload_header->reserved2 == 0xFF) {
-               print_hex_dump(KERN_INFO, "Wake up packet: ", DUMP_PREFIX_ADDRESS, 16, 1, skb->data, len+offset, 1);
+               esp_hex_dump("Wake up packet: ", skb->data, len+offset);
        }
 
        if (adapter->capabilities & ESP_CHECKSUM_ENABLED) {
@@ -619,35 +692,17 @@ static void process_rx_packet(struct esp_adapter *adapter, struct sk_buff *skb)
                priv = get_priv_from_payload_header(payload_header);
 
                if (!priv) {
-                       printk(KERN_ERR "%s: empty priv\n", __func__);
+                       esp_err("Empty priv\n");
                        dev_kfree_skb_any(skb);
                        return;
                }
 
                if (payload_header->packet_type == PACKET_TYPE_EAPOL) {
-                       printk(KERN_INFO "%s: Rx PACKET_TYPE_EAPOL!!!!\n", __func__);
+                       esp_dbg("Rx PACKET_TYPE_EAPOL!!!!\n");
                        esp_port_open(priv);
-
-                       eap_skb = alloc_skb(skb->len + ETH_HLEN, GFP_KERNEL);
-                       if(!eap_skb) {
-                               printk(KERN_INFO "%s:%u memory alloc failed\n",__func__, __LINE__);
-                               return;
-                       }
-                       eap_skb->dev = priv->ndev;
-
-                       if (!IS_ALIGNED((unsigned long) eap_skb->data, SKB_DATA_ADDR_ALIGNMENT)) {
-                               printk(KERN_INFO "%s:%u eap skb unaligned\n",__func__, __LINE__);
-                       }
-
-                       eth = (struct ethhdr *) skb_put(eap_skb, ETH_HLEN);
-                       ether_addr_copy(eth->h_dest, /*skb->data*/priv->ndev->dev_addr);
-                       ether_addr_copy(eth->h_source, /*skb->data+6*/ ap_bssid);
-                       eth->h_proto = cpu_to_be16(ETH_P_PAE);
-
-                       skb_put_data(eap_skb, skb->data, skb->len);
-                       eap_skb->protocol = eth_type_trans(eap_skb, eap_skb->dev);
-
-                       netif_rx(eap_skb);
+                       skb->dev = priv->ndev;
+                       skb->protocol = eth_type_trans(skb, priv->ndev);
+                       netif_rx(skb);
 
                } else if (payload_header->packet_type == PACKET_TYPE_DATA) {
 
@@ -657,12 +712,7 @@ static void process_rx_packet(struct esp_adapter *adapter, struct sk_buff *skb)
 
                        priv->stats.rx_bytes += skb->len;
                        /* Forward skb to kernel */
-#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 18, 0))
-                       netif_rx(skb);
-#else
-                       netif_rx_ni(skb);
-#endif
-
+                       NETIF_RX_NI(skb);
                        priv->stats.rx_packets++;
                } else if (payload_header->packet_type == PACKET_TYPE_COMMAND_RESPONSE) {
                        process_cmd_resp(priv->adapter, skb);
@@ -699,15 +749,50 @@ static void process_rx_packet(struct esp_adapter *adapter, struct sk_buff *skb)
                        dev_kfree_skb_any(skb);
 
        } else if (payload_header->if_type == ESP_TEST_IF) {
-               #if TEST_RAW_TP
+#if TEST_RAW_TP
+               if (raw_tp_mode != 0) {
                        update_test_raw_tp_rx_stats(len);
-               #endif
+               }
+#endif
                dev_kfree_skb_any(skb);
        } else {
                dev_kfree_skb_any(skb);
        }
 }
 
+char *esp_get_hardware_name(int hardware_id)
+{
+       if(hardware_id == ESP_FIRMWARE_CHIP_ESP32)
+               return "ESP32";
+       else if(hardware_id == ESP_FIRMWARE_CHIP_ESP32S2)
+               return "ESP32S2";
+       else if(hardware_id == ESP_FIRMWARE_CHIP_ESP32C3)
+               return "ESP32C3";
+       else if(hardware_id == ESP_FIRMWARE_CHIP_ESP32S3)
+               return "ESP32S3";
+       else if(hardware_id == ESP_FIRMWARE_CHIP_ESP32C2)
+               return "ESP32C2";
+       else if(hardware_id == ESP_FIRMWARE_CHIP_ESP32C6)
+               return "ESP32C6";
+       else
+               return "N/A";
+}
+
+bool esp_is_valid_hardware_id(int hardware_id)
+{
+       switch(hardware_id) {
+       case ESP_FIRMWARE_CHIP_ESP32:
+       case ESP_FIRMWARE_CHIP_ESP32S2:
+       case ESP_FIRMWARE_CHIP_ESP32C3:
+       case ESP_FIRMWARE_CHIP_ESP32S3:
+       case ESP_FIRMWARE_CHIP_ESP32C2:
+       case ESP_FIRMWARE_CHIP_ESP32C6:
+               return true;
+       default:
+               return false;
+       }
+}
+
 int esp_is_tx_queue_paused(struct esp_wifi_device *priv)
 {
        if (!priv || !priv->ndev)
@@ -739,7 +824,7 @@ void esp_tx_resume(struct esp_wifi_device *priv)
        }
 }
 
-struct sk_buff * esp_alloc_skb(u32 len)
+struct sk_buff *esp_alloc_skb(u32 len)
 {
        struct sk_buff *skb = NULL;
 
@@ -779,7 +864,7 @@ static int esp_get_packets(struct esp_adapter *adapter)
 int esp_send_packet(struct esp_adapter *adapter, struct sk_buff *skb)
 {
        if (!adapter || !adapter->if_ops || !adapter->if_ops->write) {
-               printk(KERN_ERR "esp32: %s:%u adapter: %p\n", __func__, __LINE__, adapter);
+               esp_err("%u adapter: %p\n", __LINE__, adapter);
                return -EINVAL;
        }
 
@@ -794,7 +879,40 @@ static void esp_if_rx_work(struct work_struct *work)
 
 static void update_mac_filter(struct work_struct *work)
 {
+       struct esp_adapter *adapter = esp_get_adapter();
+       struct esp_wifi_device *priv = adapter->priv[0];
+       struct net_device *ndev;
+       struct netdev_hw_addr *mac_addr;
+       u32 count = 0;
+
+       if (!priv)
+               return;
+
+       ndev = priv->ndev;
+       if (!ndev)
+               return;
+
+       if (!priv->port_open) {
+               esp_verbose("Port is not open yet, skipping mac filter update\n");
+               return;
+       }
+
+#if CONFIG_ALLOW_MULTICAST_WAKEUP
+       netdev_for_each_mc_addr(mac_addr, ndev) {
+               if (count < MAX_MULTICAST_ADDR_COUNT) {
+                       esp_verbose("%d: "MACSTR"\n", count+1, MAC2STR(mac_addr->addr));
+                       memcpy(&mcast_list.mcast_addr[count++], mac_addr->addr, ETH_ALEN);
+               }
+       }
+
+       mcast_list.priv = priv;
+       mcast_list.addr_count = count;
+
+       esp_verbose("Setting Multicast list\n");
        cmd_set_mcast_mac_list(mcast_list.priv, &mcast_list);
+#else
+  esp_info("Not setting FW multicast addresses\n");
+#endif
 }
 
 static void esp_events_work(struct work_struct *work)
@@ -809,7 +927,7 @@ static void esp_events_work(struct work_struct *work)
        dev_kfree_skb_any(skb);
 }
 
-static struct esp_adapter * init_adapter(void)
+static struct esp_adapter *init_adapter(void)
 {
        memset(&adapter, 0, sizeof(adapter));
 
@@ -834,12 +952,6 @@ static struct esp_adapter * init_adapter(void)
 
        INIT_WORK(&adapter.events_work, esp_events_work);
 
-       adapter.mac_filter_wq = alloc_workqueue("MAC_FILTER", 0, 0);
-       if (!adapter.mac_filter_wq) {
-               deinit_adapter();
-               return NULL;
-       }
-
        INIT_WORK(&adapter.mac_flter_work, update_mac_filter);
 
        return &adapter;
@@ -854,9 +966,6 @@ static void deinit_adapter(void)
 
        if (adapter.if_rx_workqueue)
                destroy_workqueue(adapter.if_rx_workqueue);
-
-       if (adapter.mac_filter_wq)
-               destroy_workqueue(adapter.mac_filter_wq);
 }
 
 static void esp_reset(void)
@@ -864,7 +973,7 @@ static void esp_reset(void)
        if (resetpin != HOST_GPIO_PIN_INVALID) {
                /* Check valid GPIO or not */
                if (!gpio_is_valid(resetpin)) {
-                       printk(KERN_WARNING "%s, ESP32: host resetpin (%d) configured is invalid GPIO\n", __func__, resetpin);
+                       esp_warn("host resetpin (%d) configured is invalid GPIO\n", resetpin);
                        resetpin = HOST_GPIO_PIN_INVALID;
                } else {
                        gpio_request(resetpin, "sysfs");
@@ -879,16 +988,36 @@ static void esp_reset(void)
                        /* HOST's resetpin set to INPUT */
                        gpio_direction_input(resetpin);
 
-                       printk(KERN_DEBUG "%s, ESP32: Triggering ESP reset.\n", __func__);
+                       esp_dbg("Triggering ESP reset.\n");
                }
        }
 }
 
+void dt_get_reset_pin(void)
+{
+       struct device_node *resetpin_node = NULL;
+       int temp_resetpin = HOST_GPIO_PIN_INVALID;
+
+
+       resetpin_node = of_find_compatible_node(NULL, NULL, "espressif,esp_sdio");
+       if (!resetpin_node)
+               resetpin_node = of_find_compatible_node(NULL, NULL, "espressif,esp_spi");
+       if (resetpin_node){
+               of_property_read_u32(resetpin_node, "resetpin", &temp_resetpin);
+               if (temp_resetpin == HOST_GPIO_PIN_INVALID)
+                       esp_warn("Unable to find resetpin in device tree.\n");
+               else
+                       resetpin = temp_resetpin;
+       }
+}
 
 static int __init esp_init(void)
 {
        int ret = 0;
        struct esp_adapter *adapter = NULL;
+       struct esp_if_params if_params;
+
+       dt_get_reset_pin();
 
        /* Reset ESP, Clean start ESP */
        esp_reset();
@@ -899,13 +1028,17 @@ static int __init esp_init(void)
        if (!adapter)
                return -EFAULT;
 
+       if_params.speed = clockspeed;
+       if_params.handshake_pin = handshakepin;
+       if_params.data_ready_pin = readypin;
        /* Init transport layer */
-       ret = esp_init_interface_layer(adapter);
+       ret = esp_init_interface_layer(adapter, &if_params);
 
        if (ret != 0) {
                deinit_adapter();
        }
 
+       ret = debugfs_init();
        return ret;
 }
 
@@ -913,9 +1046,11 @@ static void __exit esp_exit(void)
 {
        uint8_t iface_idx = 0;
 #if TEST_RAW_TP
-       test_raw_tp_cleanup();
+       if (raw_tp_mode != 0) {
+               test_raw_tp_cleanup();
+       }
 #endif
-       for (iface_idx=0; iface_idx<ESP_MAX_INTERFACE; iface_idx++) {
+       for (iface_idx = 0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) {
                cmd_deinit_interface(adapter.priv[iface_idx]);
        }
        clear_bit(ESP_DRIVER_ACTIVE, &adapter.state_flags);
@@ -926,12 +1061,14 @@ static void __exit esp_exit(void)
        if (resetpin != HOST_GPIO_PIN_INVALID) {
                gpio_free(resetpin);
        }
+       debugfs_exit();
 }
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Amey Inamdar <[email protected]>");
 MODULE_AUTHOR("Mangesh Malusare <[email protected]>");
 MODULE_AUTHOR("Yogesh Mantri <[email protected]>");
+MODULE_AUTHOR("Kapil Gupta <[email protected]>");
 MODULE_DESCRIPTION("Wifi driver for ESP-Hosted solution");
-MODULE_VERSION("0.1");
+MODULE_VERSION(RELEASE_VERSION);
 module_init(esp_init);
 module_exit(esp_exit);
This page took 0.061953 seconds and 4 git commands to generate.