#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);
ret = esp_send_packet(priv->adapter, skb);
if (ret) {
-/* esp_err("Failed to send SKB");*/
+ esp_verbose("Failed to send SKB");
priv->stats.tx_errors++;
} else {
priv->stats.tx_packets++;
}
}
-void process_capabilities(struct esp_adapter *adapter)
+void init_bt(struct esp_adapter *adapter)
{
- esp_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)) {
static int check_esp_version(struct fw_version *ver)
{
- esp_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) {
esp_err("Incompatible ESP firmware release detected, Please use correct ESP-Hosted branch/compatible release\n");
}
}
-int process_fw_data(struct fw_data *fw_p)
+static int process_fw_data(struct fw_data *fw_p, int tag_len)
{
- if (!fw_p) {
- esp_err("Incomplete/incorrect bootup event received\n");
+ if (tag_len != sizeof(struct fw_data)) {
+ esp_err("Length not matching to firmware data size\n");
return -1;
}
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;
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 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) {
- /*esp_info("%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;
-
- if (priv->port_open) {
- /*esp_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) {
- esp_dbg(" 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)
if (!priv->port_open) {
priv->stats.tx_dropped++;
- /*esp_err("Port not yet open\n");*/
+ esp_verbose("Port not yet open\n");
dev_kfree_skb(skb);
return NETDEV_TX_OK;
}
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;
if (!adapter) {
return -EINVAL;
}
- ret = esp_cfg80211_register(adapter);
- if (ret) {
- esp_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 */
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;
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;
}
return;
}
- esp_info("\nReceived ESP bootup event\n");
+ esp_info("Received ESP bootup event\n");
process_event_esp_bootup(adapter, evt->data, evt->len);
}
struct event_header *header = NULL;
if (!skb || !adapter) {
- esp_err("esp32: Incorrect event data!\n");
+ esp_err("Incorrect event data!\n");
return -1;
}
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;
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) {
}
if (payload_header->packet_type == PACKET_TYPE_EAPOL) {
- esp_info("Rx PACKET_TYPE_EAPOL!!!!\n");
+ esp_dbg("Rx PACKET_TYPE_EAPOL!!!!\n");
esp_port_open(priv);
-
- eap_skb = alloc_skb(skb->len + ETH_HLEN, GFP_KERNEL);
- if (!eap_skb) {
- esp_info("%u memory alloc failed\n", __LINE__);
- return;
- }
- eap_skb->dev = priv->ndev;
-
- if (!IS_ALIGNED((unsigned long) eap_skb->data, SKB_DATA_ADDR_ALIGNMENT)) {
- esp_info("%u eap skb unaligned\n", __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) {
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)
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)
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;
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)
}
}
+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();
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, clockspeed);
+ ret = esp_init_interface_layer(adapter, &if_params);
if (ret != 0) {
deinit_adapter();
}
+ ret = debugfs_init();
return ret;
}
{
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++) {
cmd_deinit_interface(adapter.priv[iface_idx]);
if (resetpin != HOST_GPIO_PIN_INVALID) {
gpio_free(resetpin);
}
+ debugfs_exit();
}
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Wifi driver for ESP-Hosted solution");
-MODULE_VERSION("0.1");
+MODULE_VERSION(RELEASE_VERSION);
module_init(esp_init);
module_exit(esp_exit);