From 75c26685e1ba492c37ebf07daa0f552c95265a49 Mon Sep 17 00:00:00 2001 From: ivanovborislav <81085106+ivanovborislav@users.noreply.github.com> Date: Sun, 21 Nov 2021 14:15:16 +0200 Subject: [PATCH] Add files via upload --- os_dep/linux/custom_gpio_linux.c | 340 + os_dep/linux/ioctl_cfg80211.c | 10896 ++++++++++++++++ os_dep/linux/ioctl_cfg80211.h | 454 + os_dep/linux/ioctl_linux.c | 13058 ++++++++++++++++++++ os_dep/linux/ioctl_mp.c | 3530 ++++++ os_dep/linux/mlme_linux.c | 444 + os_dep/linux/nlrtw.c | 583 + os_dep/linux/nlrtw.h | 48 + os_dep/linux/os_intfs.c | 5768 +++++++++ os_dep/linux/recv_linux.c | 738 ++ os_dep/linux/rhashtable.c | 844 ++ os_dep/linux/rhashtable.h | 827 ++ os_dep/linux/rtw_android.c | 1346 ++ os_dep/linux/rtw_cfgvendor.c | 2172 ++++ os_dep/linux/rtw_cfgvendor.h | 638 + os_dep/linux/rtw_proc.c | 6290 ++++++++++ os_dep/linux/rtw_proc.h | 72 + os_dep/linux/rtw_rhashtable.c | 77 + os_dep/linux/rtw_rhashtable.h | 67 + os_dep/linux/usb_intf.c | 1505 +++ os_dep/linux/usb_ops_linux.c | 1157 ++ os_dep/linux/wifi_regd.c | 414 + os_dep/linux/wifi_regd.h | 27 + os_dep/linux/xmit_linux.c | 554 + os_dep/osdep_service.c | 3473 ++++++ platform/custom_country_chplan.h | 22 + platform/platform_ARM_SUN50IW1P1_sdio.c | 86 + platform/platform_ARM_SUNnI_sdio.c | 130 + platform/platform_ARM_SUNxI_sdio.c | 90 + platform/platform_ARM_SUNxI_usb.c | 136 + platform/platform_ARM_WMT_sdio.c | 46 + platform/platform_RTK_DMP_usb.c | 30 + platform/platform_aml_s905_sdio.c | 54 + platform/platform_aml_s905_sdio.h | 28 + platform/platform_arm_act_sdio.c | 53 + platform/platform_hisilicon_hi3798_sdio.c | 110 + platform/platform_hisilicon_hi3798_sdio.h | 28 + platform/platform_ops.c | 32 + platform/platform_ops.h | 26 + platform/platform_sprd_sdio.c | 84 + platform/platform_zte_zx296716_sdio.c | 53 + platform/platform_zte_zx296716_sdio.h | 25 + 42 files changed, 56355 insertions(+) create mode 100644 os_dep/linux/custom_gpio_linux.c create mode 100644 os_dep/linux/ioctl_cfg80211.c create mode 100644 os_dep/linux/ioctl_cfg80211.h create mode 100644 os_dep/linux/ioctl_linux.c create mode 100644 os_dep/linux/ioctl_mp.c create mode 100644 os_dep/linux/mlme_linux.c create mode 100644 os_dep/linux/nlrtw.c create mode 100644 os_dep/linux/nlrtw.h create mode 100644 os_dep/linux/os_intfs.c create mode 100644 os_dep/linux/recv_linux.c create mode 100644 os_dep/linux/rhashtable.c create mode 100644 os_dep/linux/rhashtable.h create mode 100644 os_dep/linux/rtw_android.c create mode 100644 os_dep/linux/rtw_cfgvendor.c create mode 100644 os_dep/linux/rtw_cfgvendor.h create mode 100644 os_dep/linux/rtw_proc.c create mode 100644 os_dep/linux/rtw_proc.h create mode 100644 os_dep/linux/rtw_rhashtable.c create mode 100644 os_dep/linux/rtw_rhashtable.h create mode 100644 os_dep/linux/usb_intf.c create mode 100644 os_dep/linux/usb_ops_linux.c create mode 100644 os_dep/linux/wifi_regd.c create mode 100644 os_dep/linux/wifi_regd.h create mode 100644 os_dep/linux/xmit_linux.c create mode 100644 os_dep/osdep_service.c create mode 100644 platform/custom_country_chplan.h create mode 100644 platform/platform_ARM_SUN50IW1P1_sdio.c create mode 100644 platform/platform_ARM_SUNnI_sdio.c create mode 100644 platform/platform_ARM_SUNxI_sdio.c create mode 100644 platform/platform_ARM_SUNxI_usb.c create mode 100644 platform/platform_ARM_WMT_sdio.c create mode 100644 platform/platform_RTK_DMP_usb.c create mode 100644 platform/platform_aml_s905_sdio.c create mode 100644 platform/platform_aml_s905_sdio.h create mode 100644 platform/platform_arm_act_sdio.c create mode 100644 platform/platform_hisilicon_hi3798_sdio.c create mode 100644 platform/platform_hisilicon_hi3798_sdio.h create mode 100644 platform/platform_ops.c create mode 100644 platform/platform_ops.h create mode 100644 platform/platform_sprd_sdio.c create mode 100644 platform/platform_zte_zx296716_sdio.c create mode 100644 platform/platform_zte_zx296716_sdio.h diff --git a/os_dep/linux/custom_gpio_linux.c b/os_dep/linux/custom_gpio_linux.c new file mode 100644 index 0000000..23401b7 --- /dev/null +++ b/os_dep/linux/custom_gpio_linux.c @@ -0,0 +1,340 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#include "drv_types.h" + +#ifdef CONFIG_PLATFORM_SPRD + +/* gspi func & GPIO define */ +#include /* 0915 */ +#include + +#if !(defined ANDROID_2X) + +#ifdef CONFIG_RTL8188E +#include +#include +#endif /* CONFIG_RTL8188E */ + +#ifndef GPIO_WIFI_POWER +#define GPIO_WIFI_POWER -1 +#endif /* !GPIO_WIFI_POWER */ + +#ifndef GPIO_WIFI_RESET +#define GPIO_WIFI_RESET -1 +#endif /* !GPIO_WIFI_RESET */ + +#ifndef GPIO_WIFI_PWDN +#define GPIO_WIFI_PWDN -1 +#endif /* !GPIO_WIFI_RESET */ +#ifdef CONFIG_GSPI_HCI +extern unsigned int oob_irq; +#endif /* CONFIG_GSPI_HCI */ + +#ifdef CONFIG_SDIO_HCI +extern int rtw_mp_mode; +#else /* !CONFIG_SDIO_HCI */ +#endif /* !CONFIG_SDIO_HCI */ + +int rtw_wifi_gpio_init(void) +{ +#ifdef CONFIG_GSPI_HCI + if (GPIO_WIFI_IRQ > 0) { + gpio_request(GPIO_WIFI_IRQ, "oob_irq"); + gpio_direction_input(GPIO_WIFI_IRQ); + + oob_irq = gpio_to_irq(GPIO_WIFI_IRQ); + + RTW_INFO("%s oob_irq:%d\n", __func__, oob_irq); + } +#endif + if (GPIO_WIFI_RESET > 0) + gpio_request(GPIO_WIFI_RESET , "wifi_rst"); + if (GPIO_WIFI_POWER > 0) + gpio_request(GPIO_WIFI_POWER, "wifi_power"); + +#ifdef CONFIG_SDIO_HCI +#if (defined(CONFIG_RTL8723B)) && (MP_DRIVER == 1) + if (rtw_mp_mode == 1) { + RTW_INFO("%s GPIO_BT_RESET pin special for mp_test\n", __func__); + if (GPIO_BT_RESET > 0) + gpio_request(GPIO_BT_RESET , "bt_rst"); + } +#endif +#endif + return 0; +} + +int rtw_wifi_gpio_deinit(void) +{ +#ifdef CONFIG_GSPI_HCI + if (GPIO_WIFI_IRQ > 0) + gpio_free(GPIO_WIFI_IRQ); +#endif + if (GPIO_WIFI_RESET > 0) + gpio_free(GPIO_WIFI_RESET); + if (GPIO_WIFI_POWER > 0) + gpio_free(GPIO_WIFI_POWER); + +#ifdef CONFIG_SDIO_HCI +#if (defined(CONFIG_RTL8723B)) && (MP_DRIVER == 1) + if (rtw_mp_mode == 1) { + RTW_INFO("%s GPIO_BT_RESET pin special for mp_test\n", __func__); + if (GPIO_BT_RESET > 0) + gpio_free(GPIO_BT_RESET); + } +#endif +#endif + return 0; +} + +/* Customer function to control hw specific wlan gpios */ +void rtw_wifi_gpio_wlan_ctrl(int onoff) +{ + switch (onoff) { + case WLAN_PWDN_OFF: + RTW_INFO("%s: call customer specific GPIO(%d) to set wifi power down pin to 0\n", + __FUNCTION__, GPIO_WIFI_RESET); + +#ifndef CONFIG_DONT_BUS_SCAN + if (GPIO_WIFI_RESET > 0) + gpio_direction_output(GPIO_WIFI_RESET , 0); +#endif + break; + + case WLAN_PWDN_ON: + RTW_INFO("%s: callc customer specific GPIO(%d) to set wifi power down pin to 1\n", + __FUNCTION__, GPIO_WIFI_RESET); + + if (GPIO_WIFI_RESET > 0) + gpio_direction_output(GPIO_WIFI_RESET , 1); + break; + + case WLAN_POWER_OFF: + break; + + case WLAN_POWER_ON: + break; +#ifdef CONFIG_SDIO_HCI +#if (defined(CONFIG_RTL8723B)) && (MP_DRIVER == 1) + case WLAN_BT_PWDN_OFF: + if (rtw_mp_mode == 1) { + RTW_INFO("%s: call customer specific GPIO to set wifi power down pin to 0\n", + __FUNCTION__); + if (GPIO_BT_RESET > 0) + gpio_direction_output(GPIO_BT_RESET , 0); + } + break; + + case WLAN_BT_PWDN_ON: + if (rtw_mp_mode == 1) { + RTW_INFO("%s: callc customer specific GPIO to set wifi power down pin to 1 %x\n", + __FUNCTION__, GPIO_BT_RESET); + + if (GPIO_BT_RESET > 0) + gpio_direction_output(GPIO_BT_RESET , 1); + } + break; +#endif +#endif + } +} + +#else /* ANDROID_2X */ + +#include + +#ifdef CONFIG_RTL8188E +extern int sprd_3rdparty_gpio_wifi_power; +#endif +extern int sprd_3rdparty_gpio_wifi_pwd; +#if defined(CONFIG_RTL8723B) +extern int sprd_3rdparty_gpio_bt_reset; +#endif + +int rtw_wifi_gpio_init(void) +{ +#if defined(CONFIG_RTL8723B) + if (sprd_3rdparty_gpio_bt_reset > 0) + gpio_direction_output(sprd_3rdparty_gpio_bt_reset, 1); +#endif + + return 0; +} + +int rtw_wifi_gpio_deinit(void) +{ + return 0; +} + +/* Customer function to control hw specific wlan gpios */ +void rtw_wifi_gpio_wlan_ctrl(int onoff) +{ + switch (onoff) { + case WLAN_PWDN_OFF: + RTW_INFO("%s: call customer specific GPIO to set wifi power down pin to 0\n", + __FUNCTION__); + if (sprd_3rdparty_gpio_wifi_pwd > 0) + gpio_set_value(sprd_3rdparty_gpio_wifi_pwd, 0); + + if (sprd_3rdparty_gpio_wifi_pwd == 60) { + RTW_INFO("%s: turn off VSIM2 2.8V\n", __func__); + LDO_TurnOffLDO(LDO_LDO_SIM2); + } + break; + + case WLAN_PWDN_ON: + RTW_INFO("%s: callc customer specific GPIO to set wifi power down pin to 1\n", + __FUNCTION__); + if (sprd_3rdparty_gpio_wifi_pwd == 60) { + RTW_INFO("%s: turn on VSIM2 2.8V\n", __func__); + LDO_SetVoltLevel(LDO_LDO_SIM2, LDO_VOLT_LEVEL0); + LDO_TurnOnLDO(LDO_LDO_SIM2); + } + if (sprd_3rdparty_gpio_wifi_pwd > 0) + gpio_set_value(sprd_3rdparty_gpio_wifi_pwd, 1); + break; + + case WLAN_POWER_OFF: +#ifdef CONFIG_RTL8188E +#ifdef CONFIG_WIF1_LDO + RTW_INFO("%s: turn off VDD-WIFI0 1.2V\n", __FUNCTION__); + LDO_TurnOffLDO(LDO_LDO_WIF1); +#endif /* CONFIG_WIF1_LDO */ + + RTW_INFO("%s: turn off VDD-WIFI0 3.3V\n", __FUNCTION__); + LDO_TurnOffLDO(LDO_LDO_WIF0); + + RTW_INFO("%s: call customer specific GPIO(%d) to turn off wifi power\n", + __FUNCTION__, sprd_3rdparty_gpio_wifi_power); + if (sprd_3rdparty_gpio_wifi_power != 65535) + gpio_set_value(sprd_3rdparty_gpio_wifi_power, 0); +#endif + break; + + case WLAN_POWER_ON: +#ifdef CONFIG_RTL8188E + RTW_INFO("%s: call customer specific GPIO(%d) to turn on wifi power\n", + __FUNCTION__, sprd_3rdparty_gpio_wifi_power); + if (sprd_3rdparty_gpio_wifi_power != 65535) + gpio_set_value(sprd_3rdparty_gpio_wifi_power, 1); + + RTW_INFO("%s: turn on VDD-WIFI0 3.3V\n", __FUNCTION__); + LDO_TurnOnLDO(LDO_LDO_WIF0); + LDO_SetVoltLevel(LDO_LDO_WIF0, LDO_VOLT_LEVEL1); + +#ifdef CONFIG_WIF1_LDO + RTW_INFO("%s: turn on VDD-WIFI1 1.2V\n", __func__); + LDO_TurnOnLDO(LDO_LDO_WIF1); + LDO_SetVoltLevel(LDO_LDO_WIF1, LDO_VOLT_LEVEL3); +#endif /* CONFIG_WIF1_LDO */ +#endif + break; + + case WLAN_BT_PWDN_OFF: + RTW_INFO("%s: call customer specific GPIO to set bt power down pin to 0\n", + __FUNCTION__); +#if defined(CONFIG_RTL8723B) + if (sprd_3rdparty_gpio_bt_reset > 0) + gpio_set_value(sprd_3rdparty_gpio_bt_reset, 0); +#endif + break; + + case WLAN_BT_PWDN_ON: + RTW_INFO("%s: callc customer specific GPIO to set bt power down pin to 1\n", + __FUNCTION__); +#if defined(CONFIG_RTL8723B) + if (sprd_3rdparty_gpio_bt_reset > 0) + gpio_set_value(sprd_3rdparty_gpio_bt_reset, 1); +#endif + break; + } +} +#endif /* ANDROID_2X */ + +#elif defined(CONFIG_PLATFORM_ARM_RK3066) +#include + +#define GPIO_WIFI_IRQ RK30_PIN2_PC2 +extern unsigned int oob_irq; +int rtw_wifi_gpio_init(void) +{ +#ifdef CONFIG_GSPI_HCI + if (GPIO_WIFI_IRQ > 0) { + rk30_mux_api_set(GPIO2C2_LCDC1DATA18_SMCBLSN1_HSADCDATA5_NAME, GPIO2C_GPIO2C2);/* jacky_test */ + gpio_request(GPIO_WIFI_IRQ, "oob_irq"); + gpio_direction_input(GPIO_WIFI_IRQ); + + oob_irq = gpio_to_irq(GPIO_WIFI_IRQ); + + RTW_INFO("%s oob_irq:%d\n", __func__, oob_irq); + } +#endif + return 0; +} + + +int rtw_wifi_gpio_deinit(void) +{ +#ifdef CONFIG_GSPI_HCI + if (GPIO_WIFI_IRQ > 0) + gpio_free(GPIO_WIFI_IRQ); +#endif + return 0; +} + +void rtw_wifi_gpio_wlan_ctrl(int onoff) +{ +} + +#ifdef CONFIG_GPIO_API +/* this is a demo for extending GPIO pin[7] as interrupt mode */ +struct net_device *rtl_net; +extern int rtw_register_gpio_interrupt(struct net_device *netdev, int gpio_num, void(*callback)(u8 level)); +extern int rtw_disable_gpio_interrupt(struct net_device *netdev, int gpio_num); +void gpio_int(u8 is_high) +{ + RTW_INFO("%s level=%d\n", __func__, is_high); +} +int register_net_gpio_init(void) +{ + rtl_net = dev_get_by_name(&init_net, "wlan0"); + if (!rtl_net) { + RTW_PRINT("rtl_net init fail!\n"); + return -1; + } + return rtw_register_gpio_interrupt(rtl_net, 7, gpio_int); +} +int unregister_net_gpio_init(void) +{ + rtl_net = dev_get_by_name(&init_net, "wlan0"); + if (!rtl_net) { + RTW_PRINT("rtl_net init fail!\n"); + return -1; + } + return rtw_disable_gpio_interrupt(rtl_net, 7); +} +#endif + +#else + +int rtw_wifi_gpio_init(void) +{ + return 0; +} + +void rtw_wifi_gpio_wlan_ctrl(int onoff) +{ +} +#endif /* CONFIG_PLATFORM_SPRD */ diff --git a/os_dep/linux/ioctl_cfg80211.c b/os_dep/linux/ioctl_cfg80211.c new file mode 100644 index 0000000..cb6add5 --- /dev/null +++ b/os_dep/linux/ioctl_cfg80211.c @@ -0,0 +1,10896 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2019 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#define _IOCTL_CFG80211_C_ + +#include +#include + +#ifdef CONFIG_IOCTL_CFG80211 + +#ifndef DBG_RTW_CFG80211_STA_PARAM +#define DBG_RTW_CFG80211_STA_PARAM 0 +#endif + +#ifndef DBG_RTW_CFG80211_MESH_CONF +#define DBG_RTW_CFG80211_MESH_CONF 0 +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 0, 0)) +#define STATION_INFO_INACTIVE_TIME BIT(NL80211_STA_INFO_INACTIVE_TIME) +#define STATION_INFO_LLID BIT(NL80211_STA_INFO_LLID) +#define STATION_INFO_PLID BIT(NL80211_STA_INFO_PLID) +#define STATION_INFO_PLINK_STATE BIT(NL80211_STA_INFO_PLINK_STATE) +#define STATION_INFO_SIGNAL BIT(NL80211_STA_INFO_SIGNAL) +#define STATION_INFO_TX_BITRATE BIT(NL80211_STA_INFO_TX_BITRATE) +#define STATION_INFO_RX_PACKETS BIT(NL80211_STA_INFO_RX_PACKETS) +#define STATION_INFO_TX_PACKETS BIT(NL80211_STA_INFO_TX_PACKETS) +#define STATION_INFO_TX_FAILED BIT(NL80211_STA_INFO_TX_FAILED) +#define STATION_INFO_LOCAL_PM BIT(NL80211_STA_INFO_LOCAL_PM) +#define STATION_INFO_PEER_PM BIT(NL80211_STA_INFO_PEER_PM) +#define STATION_INFO_NONPEER_PM BIT(NL80211_STA_INFO_NONPEER_PM) +#define STATION_INFO_ASSOC_REQ_IES 0 +#endif /* Linux kernel >= 4.0.0 */ + +#define RTW_MAX_MGMT_TX_CNT (8) +#define RTW_MAX_MGMT_TX_MS_GAS (500) + +#define RTW_SCAN_IE_LEN_MAX 2304 +#define RTW_MAX_REMAIN_ON_CHANNEL_DURATION 5000 /* ms */ +#define RTW_MAX_NUM_PMKIDS 4 + +#define RTW_CH_MAX_2G_CHANNEL 14 /* Max channel in 2G band */ + +#ifdef CONFIG_WAPI_SUPPORT + +#ifndef WLAN_CIPHER_SUITE_SMS4 +#define WLAN_CIPHER_SUITE_SMS4 0x00147201 +#endif + +#ifndef WLAN_AKM_SUITE_WAPI_PSK +#define WLAN_AKM_SUITE_WAPI_PSK 0x000FAC04 +#endif + +#ifndef WLAN_AKM_SUITE_WAPI_CERT +#define WLAN_AKM_SUITE_WAPI_CERT 0x000FAC12 +#endif + +#ifndef NL80211_WAPI_VERSION_1 +#define NL80211_WAPI_VERSION_1 (1 << 2) +#endif + +#endif /* CONFIG_WAPI_SUPPORT */ + +#if (LINUX_VERSION_CODE <= KERNEL_VERSION(4, 11, 12)) +#ifdef CONFIG_RTW_80211R +#define WLAN_AKM_SUITE_FT_8021X 0x000FAC03 +#define WLAN_AKM_SUITE_FT_PSK 0x000FAC04 +#endif +#endif + +#define WIFI_CIPHER_SUITE_GCMP 0x000FAC08 +#define WIFI_CIPHER_SUITE_GCMP_256 0x000FAC09 +#define WIFI_CIPHER_SUITE_CCMP_256 0x000FAC0A +#define WIFI_CIPHER_SUITE_BIP_GMAC_128 0x000FAC0B +#define WIFI_CIPHER_SUITE_BIP_GMAC_256 0x000FAC0C +#define WIFI_CIPHER_SUITE_BIP_CMAC_256 0x000FAC0D + +/* + * If customer need, defining this flag will make driver + * always return -EBUSY at the condition of scan deny. + */ +/* #define CONFIG_NOTIFY_SCAN_ABORT_WITH_BUSY */ + +static const u32 rtw_cipher_suites[] = { + WLAN_CIPHER_SUITE_WEP40, + WLAN_CIPHER_SUITE_WEP104, + WLAN_CIPHER_SUITE_TKIP, + WLAN_CIPHER_SUITE_CCMP, +#ifdef CONFIG_WAPI_SUPPORT + WLAN_CIPHER_SUITE_SMS4, +#endif /* CONFIG_WAPI_SUPPORT */ +#ifdef CONFIG_IEEE80211W + WLAN_CIPHER_SUITE_AES_CMAC, + WIFI_CIPHER_SUITE_GCMP, + WIFI_CIPHER_SUITE_GCMP_256, + WIFI_CIPHER_SUITE_CCMP_256, + WIFI_CIPHER_SUITE_BIP_GMAC_128, + WIFI_CIPHER_SUITE_BIP_GMAC_256, + WIFI_CIPHER_SUITE_BIP_CMAC_256, +#endif /* CONFIG_IEEE80211W */ +}; + +#define RATETAB_ENT(_rate, _rateid, _flags) \ + { \ + .bitrate = (_rate), \ + .hw_value = (_rateid), \ + .flags = (_flags), \ + } + +#define CHAN2G(_channel, _freq, _flags) { \ + .band = NL80211_BAND_2GHZ, \ + .center_freq = (_freq), \ + .hw_value = (_channel), \ + .flags = (_flags), \ + .max_antenna_gain = 0, \ + .max_power = 0, \ + } + +#define CHAN5G(_channel, _flags) { \ + .band = NL80211_BAND_5GHZ, \ + .center_freq = 5000 + (5 * (_channel)), \ + .hw_value = (_channel), \ + .flags = (_flags), \ + .max_antenna_gain = 0, \ + .max_power = 0, \ + } + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)) +/* if wowlan is not supported, kernel generate a disconnect at each suspend + * cf: /net/wireless/sysfs.c, so register a stub wowlan. + * Moreover wowlan has to be enabled via a the nl80211_set_wowlan callback. + * (from user space, e.g. iw phy0 wowlan enable) + */ +static const struct wiphy_wowlan_support wowlan_stub = { + .flags = WIPHY_WOWLAN_ANY, + .n_patterns = 0, + .pattern_max_len = 0, + .pattern_min_len = 0, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)) + .max_pkt_offset = 0, +#endif +}; +#endif + +static struct ieee80211_rate rtw_rates[] = { + RATETAB_ENT(10, 0x1, 0), + RATETAB_ENT(20, 0x2, 0), + RATETAB_ENT(55, 0x4, 0), + RATETAB_ENT(110, 0x8, 0), + RATETAB_ENT(60, 0x10, 0), + RATETAB_ENT(90, 0x20, 0), + RATETAB_ENT(120, 0x40, 0), + RATETAB_ENT(180, 0x80, 0), + RATETAB_ENT(240, 0x100, 0), + RATETAB_ENT(360, 0x200, 0), + RATETAB_ENT(480, 0x400, 0), + RATETAB_ENT(540, 0x800, 0), +}; + +#define rtw_a_rates (rtw_rates + 4) +#define RTW_A_RATES_NUM 8 +#define rtw_g_rates (rtw_rates + 0) +#define RTW_G_RATES_NUM 12 + +/* from center_ch_2g */ +static struct ieee80211_channel rtw_2ghz_channels[MAX_CHANNEL_NUM_2G] = { + CHAN2G(1, 2412, 0), + CHAN2G(2, 2417, 0), + CHAN2G(3, 2422, 0), + CHAN2G(4, 2427, 0), + CHAN2G(5, 2432, 0), + CHAN2G(6, 2437, 0), + CHAN2G(7, 2442, 0), + CHAN2G(8, 2447, 0), + CHAN2G(9, 2452, 0), + CHAN2G(10, 2457, 0), + CHAN2G(11, 2462, 0), + CHAN2G(12, 2467, 0), + CHAN2G(13, 2472, 0), + CHAN2G(14, 2484, 0), +}; + +/* from center_ch_5g_20m */ +static struct ieee80211_channel rtw_5ghz_a_channels[MAX_CHANNEL_NUM_5G] = { + CHAN5G(36, 0), CHAN5G(40, 0), CHAN5G(44, 0), CHAN5G(48, 0), + + CHAN5G(52, 0), CHAN5G(56, 0), CHAN5G(60, 0), CHAN5G(64, 0), + + CHAN5G(100, 0), CHAN5G(104, 0), CHAN5G(108, 0), CHAN5G(112, 0), + CHAN5G(116, 0), CHAN5G(120, 0), CHAN5G(124, 0), CHAN5G(128, 0), + CHAN5G(132, 0), CHAN5G(136, 0), CHAN5G(140, 0), CHAN5G(144, 0), + + CHAN5G(149, 0), CHAN5G(153, 0), CHAN5G(157, 0), CHAN5G(161, 0), + CHAN5G(165, 0), CHAN5G(169, 0), CHAN5G(173, 0), CHAN5G(177, 0), +}; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) +static u8 rtw_chbw_to_cfg80211_chan_def(struct wiphy *wiphy, struct cfg80211_chan_def *chdef, u8 ch, u8 bw, u8 offset, u8 ht) +{ + int freq, cfreq; + struct ieee80211_channel *chan; + u8 ret = _FAIL; + + _rtw_memset(chdef, 0, sizeof(*chdef)); + + freq = rtw_ch2freq(ch); + if (!freq) + goto exit; + + cfreq = rtw_get_center_ch(ch, bw, offset); + if (!cfreq) + goto exit; + cfreq = rtw_ch2freq(cfreq); + if (!cfreq) + goto exit; + + chan = ieee80211_get_channel(wiphy, freq); + if (!chan) + goto exit; + + if (bw == CHANNEL_WIDTH_20) + chdef->width = ht ? NL80211_CHAN_WIDTH_20 : NL80211_CHAN_WIDTH_20_NOHT; + else if (bw == CHANNEL_WIDTH_40) + chdef->width = NL80211_CHAN_WIDTH_40; + else if (bw == CHANNEL_WIDTH_80) + chdef->width = NL80211_CHAN_WIDTH_80; + else if (bw == CHANNEL_WIDTH_160) + chdef->width = NL80211_CHAN_WIDTH_160; +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) + else if (bw == CHANNEL_WIDTH_5) + chdef->width = NL80211_CHAN_WIDTH_5; + else if (bw == CHANNEL_WIDTH_10) + chdef->width = NL80211_CHAN_WIDTH_10; +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) */ + else { + rtw_warn_on(1); + goto exit; + } + + chdef->chan = chan; + chdef->center_freq1 = cfreq; + + ret = _SUCCESS; + +exit: + return ret; +} + +#ifdef CONFIG_RTW_MESH +static const char *nl80211_chan_width_str(enum nl80211_chan_width cwidth) +{ + switch (cwidth) { + case NL80211_CHAN_WIDTH_20_NOHT: + return "20_NOHT"; + case NL80211_CHAN_WIDTH_20: + return "20"; + case NL80211_CHAN_WIDTH_40: + return "40"; + case NL80211_CHAN_WIDTH_80: + return "80"; + case NL80211_CHAN_WIDTH_80P80: + return "80+80"; + case NL80211_CHAN_WIDTH_160: + return "160"; +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) + case NL80211_CHAN_WIDTH_5: + return "5"; + case NL80211_CHAN_WIDTH_10: + return "10"; +#endif + default: + return "INVALID"; + }; +} + +static void rtw_get_chbw_from_cfg80211_chan_def(struct cfg80211_chan_def *chdef, u8 *ht, u8 *ch, u8 *bw, u8 *offset) +{ + int pri_freq; + struct ieee80211_channel *chan = chdef->chan; + + pri_freq = rtw_ch2freq(chan->hw_value); + if (!pri_freq) { + RTW_INFO("invalid channel:%d\n", chan->hw_value); + rtw_warn_on(1); + *ch = 0; + return; + } + + switch (chdef->width) { + case NL80211_CHAN_WIDTH_20_NOHT: + *ht = 0; + *bw = CHANNEL_WIDTH_20; + *offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + *ch = chan->hw_value; + break; + case NL80211_CHAN_WIDTH_20: + *ht = 1; + *bw = CHANNEL_WIDTH_20; + *offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + *ch = chan->hw_value; + break; + case NL80211_CHAN_WIDTH_40: + *ht = 1; + *bw = CHANNEL_WIDTH_40; + *offset = pri_freq > chdef->center_freq1 ? HAL_PRIME_CHNL_OFFSET_UPPER : HAL_PRIME_CHNL_OFFSET_LOWER; + if (rtw_get_offset_by_chbw(chan->hw_value, *bw, offset)) + *ch = chan->hw_value; + break; + case NL80211_CHAN_WIDTH_80: + *ht = 1; + *bw = CHANNEL_WIDTH_80; + if (rtw_get_offset_by_chbw(chan->hw_value, *bw, offset)) + *ch = chan->hw_value; + break; + case NL80211_CHAN_WIDTH_160: + *ht = 1; + *bw = CHANNEL_WIDTH_160; + if (rtw_get_offset_by_chbw(chan->hw_value, *bw, offset)) + *ch = chan->hw_value; + break; + case NL80211_CHAN_WIDTH_80P80: + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) + case NL80211_CHAN_WIDTH_5: + case NL80211_CHAN_WIDTH_10: + #endif + default: + *ht = 0; + *bw = CHANNEL_WIDTH_20; + *offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + RTW_INFO("unsupported cwidth:%s\n", nl80211_chan_width_str(chdef->width)); + rtw_warn_on(1); + }; +} +#endif /* CONFIG_RTW_MESH */ +#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)) +static const char *nl80211_channel_type_str(enum nl80211_channel_type ctype) +{ + switch (ctype) { + case NL80211_CHAN_NO_HT: + return "NO_HT"; + case NL80211_CHAN_HT20: + return "HT20"; + case NL80211_CHAN_HT40MINUS: + return "HT40-"; + case NL80211_CHAN_HT40PLUS: + return "HT40+"; + default: + return "INVALID"; + }; +} + +static enum nl80211_channel_type rtw_chbw_to_nl80211_channel_type(u8 ch, u8 bw, u8 offset, u8 ht) +{ + rtw_warn_on(!ht && (bw >= CHANNEL_WIDTH_40 || offset != HAL_PRIME_CHNL_OFFSET_DONT_CARE)); + + if (!ht) + return NL80211_CHAN_NO_HT; + if (bw >= CHANNEL_WIDTH_40) { + if (offset == HAL_PRIME_CHNL_OFFSET_UPPER) + return NL80211_CHAN_HT40MINUS; + else if (offset == HAL_PRIME_CHNL_OFFSET_LOWER) + return NL80211_CHAN_HT40PLUS; + else + rtw_warn_on(1); + } + return NL80211_CHAN_HT20; +} + +static void rtw_get_chbw_from_nl80211_channel_type(struct ieee80211_channel *chan, enum nl80211_channel_type ctype, u8 *ht, u8 *ch, u8 *bw, u8 *offset) +{ + int pri_freq; + + pri_freq = rtw_ch2freq(chan->hw_value); + if (!pri_freq) { + RTW_INFO("invalid channel:%d\n", chan->hw_value); + rtw_warn_on(1); + *ch = 0; + return; + } + *ch = chan->hw_value; + + switch (ctype) { + case NL80211_CHAN_NO_HT: + *ht = 0; + *bw = CHANNEL_WIDTH_20; + *offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + break; + case NL80211_CHAN_HT20: + *ht = 1; + *bw = CHANNEL_WIDTH_20; + *offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + break; + case NL80211_CHAN_HT40MINUS: + *ht = 1; + *bw = CHANNEL_WIDTH_40; + *offset = HAL_PRIME_CHNL_OFFSET_UPPER; + break; + case NL80211_CHAN_HT40PLUS: + *ht = 1; + *bw = CHANNEL_WIDTH_40; + *offset = HAL_PRIME_CHNL_OFFSET_LOWER; + break; + default: + *ht = 0; + *bw = CHANNEL_WIDTH_20; + *offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + RTW_INFO("unsupported ctype:%s\n", nl80211_channel_type_str(ctype)); + rtw_warn_on(1); + }; +} +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)) */ + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)) +bool rtw_cfg80211_allow_ch_switch_notify(_adapter *adapter) +{ +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 19, 0)) + if ((!MLME_IS_AP(adapter)) +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)) + && (!MLME_IS_ADHOC(adapter)) + && (!MLME_IS_ADHOC_MASTER(adapter)) + && (!MLME_IS_MESH(adapter)) +#elif defined(CONFIG_RTW_MESH) + && (!MLME_IS_MESH(adapter)) +#endif + ) + return 0; +#endif + return 1; +} + +u8 rtw_cfg80211_ch_switch_notify(_adapter *adapter, u8 ch, u8 bw, u8 offset, + u8 ht, bool started) +{ + struct wiphy *wiphy = adapter_to_wiphy(adapter); + u8 ret = _SUCCESS; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + struct cfg80211_chan_def chdef; + + ret = rtw_chbw_to_cfg80211_chan_def(wiphy, &chdef, ch, bw, offset, ht); + if (ret != _SUCCESS) + goto exit; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)) + if (started) { +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 11, 0)) + + /* --- cfg80211_ch_switch_started_notfiy() --- + * A new parameter, bool quiet, is added from Linux kernel v5.11, + * to see if block-tx was requested by the AP. since currently, + * the API is used for station before connected in rtw_chk_start_clnt_join() + * the quiet is set to false here first. May need to refine it if + * called by others with block-tx. + */ + + cfg80211_ch_switch_started_notify(adapter->pnetdev, &chdef, 0, false); +#else + cfg80211_ch_switch_started_notify(adapter->pnetdev, &chdef, 0); +#endif + goto exit; + } +#endif + + if (!rtw_cfg80211_allow_ch_switch_notify(adapter)) + goto exit; + + cfg80211_ch_switch_notify(adapter->pnetdev, &chdef); + +#else + int freq = rtw_ch2freq(ch); + enum nl80211_channel_type ctype; + + if (!rtw_cfg80211_allow_ch_switch_notify(adapter)) + goto exit; + + if (!freq) { + ret = _FAIL; + goto exit; + } + + ctype = rtw_chbw_to_nl80211_channel_type(ch, bw, offset, ht); + cfg80211_ch_switch_notify(adapter->pnetdev, freq, ctype); +#endif + +exit: + return ret; +} +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)) */ + +void rtw_2g_channels_init(struct ieee80211_channel *channels) +{ + _rtw_memcpy((void *)channels, (void *)rtw_2ghz_channels, sizeof(rtw_2ghz_channels)); +} + +void rtw_5g_channels_init(struct ieee80211_channel *channels) +{ + _rtw_memcpy((void *)channels, (void *)rtw_5ghz_a_channels, sizeof(rtw_5ghz_a_channels)); +} + +void rtw_2g_rates_init(struct ieee80211_rate *rates) +{ + _rtw_memcpy(rates, rtw_g_rates, + sizeof(struct ieee80211_rate) * RTW_G_RATES_NUM + ); +} + +void rtw_5g_rates_init(struct ieee80211_rate *rates) +{ + _rtw_memcpy(rates, rtw_a_rates, + sizeof(struct ieee80211_rate) * RTW_A_RATES_NUM + ); +} + +struct ieee80211_supported_band *rtw_spt_band_alloc(BAND_TYPE band) +{ + struct ieee80211_supported_band *spt_band = NULL; + int n_channels, n_bitrates; + + if (band == BAND_ON_2_4G) { + n_channels = MAX_CHANNEL_NUM_2G; + n_bitrates = RTW_G_RATES_NUM; + } else if (band == BAND_ON_5G) { + n_channels = MAX_CHANNEL_NUM_5G; + n_bitrates = RTW_A_RATES_NUM; + } else + goto exit; + + spt_band = (struct ieee80211_supported_band *)rtw_zmalloc( + sizeof(struct ieee80211_supported_band) + + sizeof(struct ieee80211_channel) * n_channels + + sizeof(struct ieee80211_rate) * n_bitrates + ); + if (!spt_band) + goto exit; + + spt_band->channels = (struct ieee80211_channel *)(((u8 *)spt_band) + sizeof(struct ieee80211_supported_band)); + spt_band->bitrates = (struct ieee80211_rate *)(((u8 *)spt_band->channels) + sizeof(struct ieee80211_channel) * n_channels); + spt_band->band = rtw_band_to_nl80211_band(band); + spt_band->n_channels = n_channels; + spt_band->n_bitrates = n_bitrates; + +exit: + return spt_band; +} + +void rtw_spt_band_free(struct ieee80211_supported_band *spt_band) +{ + u32 size = 0; + + if (!spt_band) + return; + + if (spt_band->band == NL80211_BAND_2GHZ) { + size = sizeof(struct ieee80211_supported_band) + + sizeof(struct ieee80211_channel) * MAX_CHANNEL_NUM_2G + + sizeof(struct ieee80211_rate) * RTW_G_RATES_NUM; + } else if (spt_band->band == NL80211_BAND_5GHZ) { + size = sizeof(struct ieee80211_supported_band) + + sizeof(struct ieee80211_channel) * MAX_CHANNEL_NUM_5G + + sizeof(struct ieee80211_rate) * RTW_A_RATES_NUM; + } else { + + } + rtw_mfree((u8 *)spt_band, size); +} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) +static const struct ieee80211_txrx_stypes + rtw_cfg80211_default_mgmt_stypes[NUM_NL80211_IFTYPES] = { + [NL80211_IFTYPE_ADHOC] = { + .tx = 0xffff, + .rx = BIT(IEEE80211_STYPE_ACTION >> 4) + }, + [NL80211_IFTYPE_STATION] = { + .tx = 0xffff, + .rx = BIT(IEEE80211_STYPE_ACTION >> 4) | + BIT(IEEE80211_STYPE_AUTH >> 4) | + BIT(IEEE80211_STYPE_PROBE_REQ >> 4) + }, + [NL80211_IFTYPE_AP] = { + .tx = 0xffff, + .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) | + BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) | + BIT(IEEE80211_STYPE_PROBE_REQ >> 4) | + BIT(IEEE80211_STYPE_DISASSOC >> 4) | + BIT(IEEE80211_STYPE_AUTH >> 4) | + BIT(IEEE80211_STYPE_DEAUTH >> 4) | + BIT(IEEE80211_STYPE_ACTION >> 4) + }, + [NL80211_IFTYPE_AP_VLAN] = { + /* copy AP */ + .tx = 0xffff, + .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) | + BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) | + BIT(IEEE80211_STYPE_PROBE_REQ >> 4) | + BIT(IEEE80211_STYPE_DISASSOC >> 4) | + BIT(IEEE80211_STYPE_AUTH >> 4) | + BIT(IEEE80211_STYPE_DEAUTH >> 4) | + BIT(IEEE80211_STYPE_ACTION >> 4) + }, + [NL80211_IFTYPE_P2P_CLIENT] = { + .tx = 0xffff, + .rx = BIT(IEEE80211_STYPE_ACTION >> 4) | + BIT(IEEE80211_STYPE_PROBE_REQ >> 4) + }, + [NL80211_IFTYPE_P2P_GO] = { + .tx = 0xffff, + .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) | + BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) | + BIT(IEEE80211_STYPE_PROBE_REQ >> 4) | + BIT(IEEE80211_STYPE_DISASSOC >> 4) | + BIT(IEEE80211_STYPE_AUTH >> 4) | + BIT(IEEE80211_STYPE_DEAUTH >> 4) | + BIT(IEEE80211_STYPE_ACTION >> 4) + }, +#if defined(RTW_DEDICATED_P2P_DEVICE) + [NL80211_IFTYPE_P2P_DEVICE] = { + .tx = 0xffff, + .rx = BIT(IEEE80211_STYPE_ACTION >> 4) | + BIT(IEEE80211_STYPE_PROBE_REQ >> 4) + }, +#endif +#if defined(CONFIG_RTW_MESH) + [NL80211_IFTYPE_MESH_POINT] = { + .tx = 0xffff, + .rx = BIT(IEEE80211_STYPE_ACTION >> 4) + | BIT(IEEE80211_STYPE_AUTH >> 4) + }, +#endif + +}; +#endif + +NDIS_802_11_NETWORK_INFRASTRUCTURE nl80211_iftype_to_rtw_network_type(enum nl80211_iftype type) +{ + switch (type) { + case NL80211_IFTYPE_ADHOC: + return Ndis802_11IBSS; + + #if defined(CONFIG_P2P) && ((LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE)) + case NL80211_IFTYPE_P2P_CLIENT: + #endif + case NL80211_IFTYPE_STATION: + return Ndis802_11Infrastructure; + +#ifdef CONFIG_AP_MODE + #if defined(CONFIG_P2P) && ((LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE)) + case NL80211_IFTYPE_P2P_GO: + #endif + case NL80211_IFTYPE_AP: + return Ndis802_11APMode; +#endif + +#ifdef CONFIG_RTW_MESH + case NL80211_IFTYPE_MESH_POINT: + return Ndis802_11_mesh; +#endif + +#ifdef CONFIG_WIFI_MONITOR + case NL80211_IFTYPE_MONITOR: + return Ndis802_11Monitor; +#endif /* CONFIG_WIFI_MONITOR */ + + default: + return Ndis802_11InfrastructureMax; + } +} + +u32 nl80211_iftype_to_rtw_mlme_state(enum nl80211_iftype type) +{ + switch (type) { + case NL80211_IFTYPE_ADHOC: + return WIFI_ADHOC_STATE; + + #if defined(CONFIG_P2P) && ((LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE)) + case NL80211_IFTYPE_P2P_CLIENT: + #endif + case NL80211_IFTYPE_STATION: + return WIFI_STATION_STATE; + +#ifdef CONFIG_AP_MODE + #if defined(CONFIG_P2P) && ((LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE)) + case NL80211_IFTYPE_P2P_GO: + #endif + case NL80211_IFTYPE_AP: + return WIFI_AP_STATE; +#endif + +#ifdef CONFIG_RTW_MESH + case NL80211_IFTYPE_MESH_POINT: + return WIFI_MESH_STATE; +#endif + + case NL80211_IFTYPE_MONITOR: + return WIFI_MONITOR_STATE; + + default: + return WIFI_NULL_STATE; + } +} + +static int rtw_cfg80211_sync_iftype(_adapter *adapter) +{ + struct wireless_dev *rtw_wdev = adapter->rtw_wdev; + + if (!(nl80211_iftype_to_rtw_mlme_state(rtw_wdev->iftype) & MLME_STATE(adapter))) { + /* iftype and mlme state is not syc */ + NDIS_802_11_NETWORK_INFRASTRUCTURE network_type; + + network_type = nl80211_iftype_to_rtw_network_type(rtw_wdev->iftype); + if (network_type != Ndis802_11InfrastructureMax) { + if (rtw_pwr_wakeup(adapter) == _FAIL) { + RTW_WARN(FUNC_ADPT_FMT" call rtw_pwr_wakeup fail\n", FUNC_ADPT_ARG(adapter)); + return _FAIL; + } + + rtw_set_802_11_infrastructure_mode(adapter, network_type, 0); + rtw_setopmode_cmd(adapter, network_type, RTW_CMDF_WAIT_ACK); + } else { + rtw_warn_on(1); + RTW_WARN(FUNC_ADPT_FMT" iftype:%u is not support\n", FUNC_ADPT_ARG(adapter), rtw_wdev->iftype); + return _FAIL; + } + } + + return _SUCCESS; +} + +static u64 rtw_get_systime_us(void) +{ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)) + return ktime_to_us(ktime_get_boottime()); +#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39)) + struct timespec ts; + get_monotonic_boottime(&ts); + return ((u64)ts.tv_sec * 1000000) + ts.tv_nsec / 1000; +#else + struct timeval tv; + do_gettimeofday(&tv); + return ((u64)tv.tv_sec * 1000000) + tv.tv_usec; +#endif +} + +/* Try to remove non target BSS's SR to reduce PBC overlap rate */ +static int rtw_cfg80211_clear_wps_sr_of_non_target_bss(_adapter *padapter, struct wlan_network *pnetwork, struct cfg80211_ssid *req_ssid) +{ + int ret = 0; + u8 *psr = NULL, sr = 0; + NDIS_802_11_SSID *pssid = &pnetwork->network.Ssid; + u32 wpsielen = 0; + u8 *wpsie = NULL; + + if (pssid->SsidLength == req_ssid->ssid_len + && _rtw_memcmp(pssid->Ssid, req_ssid->ssid, req_ssid->ssid_len) == _TRUE) + goto exit; + + wpsie = rtw_get_wps_ie(pnetwork->network.IEs + _FIXED_IE_LENGTH_ + , pnetwork->network.IELength - _FIXED_IE_LENGTH_, NULL, &wpsielen); + if (wpsie && wpsielen > 0) + psr = rtw_get_wps_attr_content(wpsie, wpsielen, WPS_ATTR_SELECTED_REGISTRAR, &sr, NULL); + + if (psr && sr) { + if (0) + RTW_INFO("clear sr of non target bss:%s("MAC_FMT")\n" + , pssid->Ssid, MAC_ARG(pnetwork->network.MacAddress)); + *psr = 0; /* clear sr */ + ret = 1; + } + +exit: + return ret; +} + +#define MAX_BSSINFO_LEN 1000 +struct cfg80211_bss *rtw_cfg80211_inform_bss(_adapter *padapter, struct wlan_network *pnetwork) +{ + struct ieee80211_channel *notify_channel; + struct cfg80211_bss *bss = NULL; + /* struct ieee80211_supported_band *band; */ + u16 channel; + u32 freq; + u64 notify_timestamp; + u16 notify_capability; + u16 notify_interval; + u8 *notify_ie; + size_t notify_ielen; + s32 notify_signal; + /* u8 buf[MAX_BSSINFO_LEN]; */ + + u8 *pbuf; + size_t buf_size = MAX_BSSINFO_LEN; + size_t len, bssinf_len = 0; + struct rtw_ieee80211_hdr *pwlanhdr; + unsigned short *fctrl; + u8 bc_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; + + struct wireless_dev *wdev = padapter->rtw_wdev; + struct wiphy *wiphy = wdev->wiphy; + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + + pbuf = rtw_zmalloc(buf_size); + if (pbuf == NULL) { + RTW_INFO("%s pbuf allocate failed !!\n", __FUNCTION__); + return bss; + } + + /* RTW_INFO("%s\n", __func__); */ + + bssinf_len = pnetwork->network.IELength + sizeof(struct rtw_ieee80211_hdr_3addr); + if (bssinf_len > buf_size) { + RTW_INFO("%s IE Length too long > %zu byte\n", __FUNCTION__, buf_size); + goto exit; + } + +#ifndef CONFIG_WAPI_SUPPORT + { + u16 wapi_len = 0; + + if (rtw_get_wapi_ie(pnetwork->network.IEs, pnetwork->network.IELength, NULL, &wapi_len) > 0) { + if (wapi_len > 0) { + RTW_INFO("%s, no support wapi!\n", __FUNCTION__); + goto exit; + } + } + } +#endif /* !CONFIG_WAPI_SUPPORT */ + + channel = pnetwork->network.Configuration.DSConfig; + freq = rtw_ch2freq(channel); + notify_channel = ieee80211_get_channel(wiphy, freq); + + if (0) + notify_timestamp = le64_to_cpu(*(u64 *)rtw_get_timestampe_from_ie(pnetwork->network.IEs)); + else + notify_timestamp = rtw_get_systime_us(); + + notify_interval = le16_to_cpu(*(u16 *)rtw_get_beacon_interval_from_ie(pnetwork->network.IEs)); + notify_capability = le16_to_cpu(*(u16 *)rtw_get_capability_from_ie(pnetwork->network.IEs)); + + notify_ie = pnetwork->network.IEs + _FIXED_IE_LENGTH_; + notify_ielen = pnetwork->network.IELength - _FIXED_IE_LENGTH_; + + /* We've set wiphy's signal_type as CFG80211_SIGNAL_TYPE_MBM: signal strength in mBm (100*dBm) */ + if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _TRUE && + is_same_network(&pmlmepriv->cur_network.network, &pnetwork->network, 0)) { + notify_signal = 100 * translate_percentage_to_dbm(padapter->recvpriv.signal_strength); /* dbm */ + } else { + notify_signal = 100 * translate_percentage_to_dbm(pnetwork->network.PhyInfo.SignalStrength); /* dbm */ + } + +#if 0 + RTW_INFO("bssid: "MAC_FMT"\n", MAC_ARG(pnetwork->network.MacAddress)); + RTW_INFO("Channel: %d(%d)\n", channel, freq); + RTW_INFO("Capability: %X\n", notify_capability); + RTW_INFO("Beacon interval: %d\n", notify_interval); + RTW_INFO("Signal: %d\n", notify_signal); + RTW_INFO("notify_timestamp: %llu\n", notify_timestamp); +#endif + + /* pbuf = buf; */ + + pwlanhdr = (struct rtw_ieee80211_hdr *)pbuf; + fctrl = &(pwlanhdr->frame_ctl); + *(fctrl) = 0; + + SetSeqNum(pwlanhdr, 0/*pmlmeext->mgnt_seq*/); + /* pmlmeext->mgnt_seq++; */ + + if (pnetwork->network.Reserved[0] == BSS_TYPE_BCN) { /* WIFI_BEACON */ + _rtw_memcpy(pwlanhdr->addr1, bc_addr, ETH_ALEN); + set_frame_sub_type(pbuf, WIFI_BEACON); + } else { + _rtw_memcpy(pwlanhdr->addr1, adapter_mac_addr(padapter), ETH_ALEN); + set_frame_sub_type(pbuf, WIFI_PROBERSP); + } + + _rtw_memcpy(pwlanhdr->addr2, pnetwork->network.MacAddress, ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr3, pnetwork->network.MacAddress, ETH_ALEN); + + + /* pbuf += sizeof(struct rtw_ieee80211_hdr_3addr); */ + len = sizeof(struct rtw_ieee80211_hdr_3addr); + _rtw_memcpy((pbuf + len), pnetwork->network.IEs, pnetwork->network.IELength); + *((u64 *)(pbuf + len)) = cpu_to_le64(notify_timestamp); + + len += pnetwork->network.IELength; + + #if defined(CONFIG_P2P) && 0 + if(rtw_get_p2p_ie(pnetwork->network.IEs+12, pnetwork->network.IELength-12, NULL, NULL)) + RTW_INFO("%s, got p2p_ie\n", __func__); + #endif + +#if 1 + bss = cfg80211_inform_bss_frame(wiphy, notify_channel, (struct ieee80211_mgmt *)pbuf, + len, notify_signal, GFP_ATOMIC); +#else + + bss = cfg80211_inform_bss(wiphy, notify_channel, (const u8 *)pnetwork->network.MacAddress, + notify_timestamp, notify_capability, notify_interval, notify_ie, + notify_ielen, notify_signal, GFP_ATOMIC/*GFP_KERNEL*/); +#endif + + if (unlikely(!bss)) { + RTW_INFO(FUNC_ADPT_FMT" bss NULL\n", FUNC_ADPT_ARG(padapter)); + goto exit; + } + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 38)) +#ifndef COMPAT_KERNEL_RELEASE + /* patch for cfg80211, update beacon ies to information_elements */ + if (pnetwork->network.Reserved[0] == BSS_TYPE_BCN) { /* WIFI_BEACON */ + + if (bss->len_information_elements != bss->len_beacon_ies) { + bss->information_elements = bss->beacon_ies; + bss->len_information_elements = bss->len_beacon_ies; + } + } +#endif /* COMPAT_KERNEL_RELEASE */ +#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 38) */ + +#if 0 + { + if (bss->information_elements == bss->proberesp_ies) { + if (bss->len_information_elements != bss->len_proberesp_ies) + RTW_INFO("error!, len_information_elements != bss->len_proberesp_ies\n"); + } else if (bss->len_information_elements < bss->len_beacon_ies) { + bss->information_elements = bss->beacon_ies; + bss->len_information_elements = bss->len_beacon_ies; + } + } +#endif +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0) + cfg80211_put_bss(wiphy, bss); +#else + cfg80211_put_bss(bss); +#endif + +exit: + if (pbuf) + rtw_mfree(pbuf, buf_size); + return bss; + +} + +/* + Check the given bss is valid by kernel API cfg80211_get_bss() + @padapter : the given adapter + + return _TRUE if bss is valid, _FALSE for not found. +*/ +int rtw_cfg80211_check_bss(_adapter *padapter) +{ + WLAN_BSSID_EX *pnetwork = &(padapter->mlmeextpriv.mlmext_info.network); + struct cfg80211_bss *bss = NULL; + struct ieee80211_channel *notify_channel = NULL; + u32 freq; + + if (!(pnetwork) || !(padapter->rtw_wdev)) + return _FALSE; + + freq = rtw_ch2freq(pnetwork->Configuration.DSConfig); + notify_channel = ieee80211_get_channel(padapter->rtw_wdev->wiphy, freq); + bss = cfg80211_get_bss(padapter->rtw_wdev->wiphy, notify_channel, + pnetwork->MacAddress, pnetwork->Ssid.Ssid, + pnetwork->Ssid.SsidLength, +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0) + pnetwork->InfrastructureMode == Ndis802_11Infrastructure?IEEE80211_BSS_TYPE_ESS:IEEE80211_BSS_TYPE_IBSS, + IEEE80211_PRIVACY(pnetwork->Privacy)); +#else + pnetwork->InfrastructureMode == Ndis802_11Infrastructure?WLAN_CAPABILITY_ESS:WLAN_CAPABILITY_IBSS, pnetwork->InfrastructureMode == Ndis802_11Infrastructure?WLAN_CAPABILITY_ESS:WLAN_CAPABILITY_IBSS); +#endif + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0) + cfg80211_put_bss(padapter->rtw_wdev->wiphy, bss); +#else + cfg80211_put_bss(bss); +#endif + + return bss != NULL; +} + +void rtw_cfg80211_ibss_indicate_connect(_adapter *padapter) +{ + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct wlan_network *cur_network = &(pmlmepriv->cur_network); + struct wireless_dev *pwdev = padapter->rtw_wdev; +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0)) + struct wiphy *wiphy = pwdev->wiphy; + int freq = 2412; + struct ieee80211_channel *notify_channel; +#endif + + RTW_INFO(FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(padapter)); + + if (pwdev->iftype != NL80211_IFTYPE_ADHOC) + return; + + if (!rtw_cfg80211_check_bss(padapter)) { + WLAN_BSSID_EX *pnetwork = &(padapter->mlmeextpriv.mlmext_info.network); + struct wlan_network *scanned = pmlmepriv->cur_network_scanned; + + if (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == _TRUE) { + + _rtw_memcpy(&cur_network->network, pnetwork, sizeof(WLAN_BSSID_EX)); + if (cur_network) { + if (!rtw_cfg80211_inform_bss(padapter, cur_network)) + RTW_INFO(FUNC_ADPT_FMT" inform fail !!\n", FUNC_ADPT_ARG(padapter)); + else + RTW_INFO(FUNC_ADPT_FMT" inform success !!\n", FUNC_ADPT_ARG(padapter)); + } else { + RTW_INFO("cur_network is not exist!!!\n"); + return ; + } + } else { + if (scanned == NULL) + rtw_warn_on(1); + + if (_rtw_memcmp(&(scanned->network.Ssid), &(pnetwork->Ssid), sizeof(NDIS_802_11_SSID)) == _TRUE + && _rtw_memcmp(scanned->network.MacAddress, pnetwork->MacAddress, sizeof(NDIS_802_11_MAC_ADDRESS)) == _TRUE + ) { + if (!rtw_cfg80211_inform_bss(padapter, scanned)) + RTW_INFO(FUNC_ADPT_FMT" inform fail !!\n", FUNC_ADPT_ARG(padapter)); + else { + /* RTW_INFO(FUNC_ADPT_FMT" inform success !!\n", FUNC_ADPT_ARG(padapter)); */ + } + } else { + RTW_INFO("scanned & pnetwork compare fail\n"); + rtw_warn_on(1); + } + } + + if (!rtw_cfg80211_check_bss(padapter)) + RTW_PRINT(FUNC_ADPT_FMT" BSS not found !!\n", FUNC_ADPT_ARG(padapter)); + } + /* notify cfg80211 that device joined an IBSS */ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0)) + freq = rtw_ch2freq(cur_network->network.Configuration.DSConfig); + if (1) + RTW_INFO("chan: %d, freq: %d\n", cur_network->network.Configuration.DSConfig, freq); + notify_channel = ieee80211_get_channel(wiphy, freq); + cfg80211_ibss_joined(padapter->pnetdev, cur_network->network.MacAddress, notify_channel, GFP_ATOMIC); +#else + cfg80211_ibss_joined(padapter->pnetdev, cur_network->network.MacAddress, GFP_ATOMIC); +#endif +} + +void rtw_cfg80211_indicate_connect(_adapter *padapter) +{ + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct wlan_network *cur_network = &(pmlmepriv->cur_network); + struct wireless_dev *pwdev = padapter->rtw_wdev; + struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(padapter); + _irqL irqL; +#ifdef CONFIG_P2P + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); +#endif +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) + struct cfg80211_roam_info roam_info ={}; +#endif + + RTW_INFO(FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(padapter)); + if (pwdev->iftype != NL80211_IFTYPE_STATION + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + && pwdev->iftype != NL80211_IFTYPE_P2P_CLIENT + #endif + ) + return; + + if (!MLME_IS_STA(padapter)) + return; + +#ifdef CONFIG_P2P + if (pwdinfo->driver_interface == DRIVER_CFG80211) { + #if !RTW_P2P_GROUP_INTERFACE + if (!rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) { + rtw_p2p_set_pre_state(pwdinfo, rtw_p2p_state(pwdinfo)); + rtw_p2p_set_role(pwdinfo, P2P_ROLE_CLIENT); + rtw_p2p_set_state(pwdinfo, P2P_STATE_GONEGO_OK); + RTW_INFO("%s, role=%d, p2p_state=%d, pre_p2p_state=%d\n", __func__, rtw_p2p_role(pwdinfo), rtw_p2p_state(pwdinfo), rtw_p2p_pre_state(pwdinfo)); + } + #endif + } +#endif /* CONFIG_P2P */ + + if (check_fwstate(pmlmepriv, WIFI_MONITOR_STATE) != _TRUE) { + WLAN_BSSID_EX *pnetwork = &(padapter->mlmeextpriv.mlmext_info.network); + struct wlan_network *scanned = pmlmepriv->cur_network_scanned; + + /* RTW_INFO(FUNC_ADPT_FMT" BSS not found\n", FUNC_ADPT_ARG(padapter)); */ + + if (scanned == NULL) { + rtw_warn_on(1); + goto check_bss; + } + + if (_rtw_memcmp(scanned->network.MacAddress, pnetwork->MacAddress, sizeof(NDIS_802_11_MAC_ADDRESS)) == _TRUE + && _rtw_memcmp(&(scanned->network.Ssid), &(pnetwork->Ssid), sizeof(NDIS_802_11_SSID)) == _TRUE + ) { + if (!rtw_cfg80211_inform_bss(padapter, scanned)) + RTW_INFO(FUNC_ADPT_FMT" inform fail !!\n", FUNC_ADPT_ARG(padapter)); + else { + /* RTW_INFO(FUNC_ADPT_FMT" inform success !!\n", FUNC_ADPT_ARG(padapter)); */ + } + } else { + RTW_INFO("scanned: %s("MAC_FMT"), cur: %s("MAC_FMT")\n", + scanned->network.Ssid.Ssid, MAC_ARG(scanned->network.MacAddress), + pnetwork->Ssid.Ssid, MAC_ARG(pnetwork->MacAddress) + ); + rtw_warn_on(1); + } + } + +check_bss: + if (!rtw_cfg80211_check_bss(padapter)) + RTW_PRINT(FUNC_ADPT_FMT" BSS not found !!\n", FUNC_ADPT_ARG(padapter)); + + _enter_critical_bh(&pwdev_priv->connect_req_lock, &irqL); + + if (rtw_to_roam(padapter) > 0) { + #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39) || defined(COMPAT_KERNEL_RELEASE) + struct wiphy *wiphy = pwdev->wiphy; + struct ieee80211_channel *notify_channel; + u32 freq; + u16 channel = cur_network->network.Configuration.DSConfig; + + freq = rtw_ch2freq(channel); + notify_channel = ieee80211_get_channel(wiphy, freq); + #endif + + #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) + roam_info.bssid = cur_network->network.MacAddress; + roam_info.req_ie = pmlmepriv->assoc_req + sizeof(struct rtw_ieee80211_hdr_3addr) + 2; + roam_info.req_ie_len = pmlmepriv->assoc_req_len - sizeof(struct rtw_ieee80211_hdr_3addr) - 2; + roam_info.resp_ie = pmlmepriv->assoc_rsp + sizeof(struct rtw_ieee80211_hdr_3addr) + 6; + roam_info.resp_ie_len = pmlmepriv->assoc_rsp_len - sizeof(struct rtw_ieee80211_hdr_3addr) - 6; + + cfg80211_roamed(padapter->pnetdev, &roam_info, GFP_ATOMIC); + #else + cfg80211_roamed(padapter->pnetdev + #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39) || defined(COMPAT_KERNEL_RELEASE) + , notify_channel + #endif + , cur_network->network.MacAddress + , pmlmepriv->assoc_req + sizeof(struct rtw_ieee80211_hdr_3addr) + 2 + , pmlmepriv->assoc_req_len - sizeof(struct rtw_ieee80211_hdr_3addr) - 2 + , pmlmepriv->assoc_rsp + sizeof(struct rtw_ieee80211_hdr_3addr) + 6 + , pmlmepriv->assoc_rsp_len - sizeof(struct rtw_ieee80211_hdr_3addr) - 6 + , GFP_ATOMIC); + #endif /*LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)*/ + + RTW_INFO(FUNC_ADPT_FMT" call cfg80211_roamed\n", FUNC_ADPT_ARG(padapter)); + +#ifdef CONFIG_RTW_80211R + if (rtw_ft_roam(padapter)) + rtw_ft_set_status(padapter, RTW_FT_ASSOCIATED_STA); +#endif + } else { + #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0) || defined(COMPAT_KERNEL_RELEASE) + RTW_INFO("pwdev->sme_state(b)=%d\n", pwdev->sme_state); + #endif + + if (check_fwstate(pmlmepriv, WIFI_MONITOR_STATE) != _TRUE) + rtw_cfg80211_connect_result(pwdev, cur_network->network.MacAddress + , pmlmepriv->assoc_req + sizeof(struct rtw_ieee80211_hdr_3addr) + 2 + , pmlmepriv->assoc_req_len - sizeof(struct rtw_ieee80211_hdr_3addr) - 2 + , pmlmepriv->assoc_rsp + sizeof(struct rtw_ieee80211_hdr_3addr) + 6 + , pmlmepriv->assoc_rsp_len - sizeof(struct rtw_ieee80211_hdr_3addr) - 6 + , WLAN_STATUS_SUCCESS, GFP_ATOMIC); + #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0) || defined(COMPAT_KERNEL_RELEASE) + RTW_INFO("pwdev->sme_state(a)=%d\n", pwdev->sme_state); + #endif + } + + rtw_wdev_free_connect_req(pwdev_priv); + + _exit_critical_bh(&pwdev_priv->connect_req_lock, &irqL); +} + +void rtw_cfg80211_indicate_disconnect(_adapter *padapter, u16 reason, u8 locally_generated) +{ + struct wireless_dev *pwdev = padapter->rtw_wdev; + struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(padapter); + _irqL irqL; +#ifdef CONFIG_P2P + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); +#endif + + RTW_INFO(FUNC_ADPT_FMT" ,reason = %d\n", FUNC_ADPT_ARG(padapter), reason); + + /*always replace privated definitions with wifi reserved value 0*/ + if (WLAN_REASON_IS_PRIVATE(reason)) + reason = 0; + + if (pwdev->iftype != NL80211_IFTYPE_STATION + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + && pwdev->iftype != NL80211_IFTYPE_P2P_CLIENT + #endif + ) + return; + + if (!MLME_IS_STA(padapter)) + return; + +#ifdef CONFIG_P2P + if (pwdinfo->driver_interface == DRIVER_CFG80211) { + if (!rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) { + rtw_p2p_set_state(pwdinfo, rtw_p2p_pre_state(pwdinfo)); + + #if RTW_P2P_GROUP_INTERFACE + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + if (pwdev->iftype != NL80211_IFTYPE_P2P_CLIENT) + #endif + #endif + rtw_p2p_set_role(pwdinfo, P2P_ROLE_DEVICE); + + RTW_INFO("%s, role=%d, p2p_state=%d, pre_p2p_state=%d\n", __func__, rtw_p2p_role(pwdinfo), rtw_p2p_state(pwdinfo), rtw_p2p_pre_state(pwdinfo)); + } + } +#endif /* CONFIG_P2P */ + + _enter_critical_bh(&pwdev_priv->connect_req_lock, &irqL); + + if (padapter->ndev_unregistering || !rtw_wdev_not_indic_disco(pwdev_priv)) { + #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0) || defined(COMPAT_KERNEL_RELEASE) + RTW_INFO("pwdev->sme_state(b)=%d\n", pwdev->sme_state); + + if (pwdev->sme_state == CFG80211_SME_CONNECTING) { + RTW_INFO(FUNC_ADPT_FMT" call cfg80211_connect_result, reason:%d\n", FUNC_ADPT_ARG(padapter), reason); + rtw_cfg80211_connect_result(pwdev, NULL, NULL, 0, NULL, 0, + reason?reason:WLAN_STATUS_UNSPECIFIED_FAILURE, + GFP_ATOMIC); + } else if (pwdev->sme_state == CFG80211_SME_CONNECTED) { + RTW_INFO(FUNC_ADPT_FMT" call cfg80211_disconnected, reason:%d\n", FUNC_ADPT_ARG(padapter), reason); + rtw_cfg80211_disconnected(pwdev, reason, NULL, 0, locally_generated, GFP_ATOMIC); + } + + RTW_INFO("pwdev->sme_state(a)=%d\n", pwdev->sme_state); + #else + if (pwdev_priv->connect_req) { + RTW_INFO(FUNC_ADPT_FMT" call cfg80211_connect_result, reason:%d\n", FUNC_ADPT_ARG(padapter), reason); + rtw_cfg80211_connect_result(pwdev, NULL, NULL, 0, NULL, 0, + reason?reason:WLAN_STATUS_UNSPECIFIED_FAILURE, + GFP_ATOMIC); + } else { + RTW_INFO(FUNC_ADPT_FMT" call cfg80211_disconnected, reason:%d\n", FUNC_ADPT_ARG(padapter), reason); + rtw_cfg80211_disconnected(pwdev, reason, NULL, 0, locally_generated, GFP_ATOMIC); + } + #endif + } + + rtw_wdev_free_connect_req(pwdev_priv); + + _exit_critical_bh(&pwdev_priv->connect_req_lock, &irqL); +} + + +#ifdef CONFIG_AP_MODE +static int rtw_cfg80211_ap_set_encryption(struct net_device *dev, struct ieee_param *param) +{ + int ret = 0; + u32 wep_key_idx, wep_key_len; + struct sta_info *psta = NULL, *pbcmc_sta = NULL; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct security_priv *psecuritypriv = &(padapter->securitypriv); + struct sta_priv *pstapriv = &padapter->stapriv; + + RTW_INFO("%s\n", __FUNCTION__); + + param->u.crypt.err = 0; + param->u.crypt.alg[IEEE_CRYPT_ALG_NAME_LEN - 1] = '\0'; + + if (is_broadcast_mac_addr(param->sta_addr)) { + if (param->u.crypt.idx >= WEP_KEYS + #ifdef CONFIG_IEEE80211W + && param->u.crypt.idx > BIP_MAX_KEYID + #endif + ) { + ret = -EINVAL; + goto exit; + } + } else { + psta = rtw_get_stainfo(pstapriv, param->sta_addr); + if (!psta) { + ret = -EINVAL; + RTW_INFO(FUNC_ADPT_FMT", sta "MAC_FMT" not found\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(param->sta_addr)); + goto exit; + } + } + + if (strcmp(param->u.crypt.alg, "none") == 0 && (psta == NULL)) { + /* todo:clear default encryption keys */ + + RTW_INFO("clear default encryption keys, keyid=%d\n", param->u.crypt.idx); + + goto exit; + } + + + if (strcmp(param->u.crypt.alg, "WEP") == 0 && (psta == NULL)) { + RTW_INFO("r871x_set_encryption, crypt.alg = WEP\n"); + + wep_key_idx = param->u.crypt.idx; + wep_key_len = param->u.crypt.key_len; + + RTW_INFO("r871x_set_encryption, wep_key_idx=%d, len=%d\n", wep_key_idx, wep_key_len); + + if ((wep_key_idx >= WEP_KEYS) || (wep_key_len <= 0)) { + ret = -EINVAL; + goto exit; + } + + if (wep_key_len > 0) + wep_key_len = wep_key_len <= 5 ? 5 : 13; + + if (psecuritypriv->bWepDefaultKeyIdxSet == 0) { + /* wep default key has not been set, so use this key index as default key. */ + + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Auto; + psecuritypriv->ndisencryptstatus = Ndis802_11Encryption1Enabled; + psecuritypriv->dot11PrivacyAlgrthm = _WEP40_; + psecuritypriv->dot118021XGrpPrivacy = _WEP40_; + + if (wep_key_len == 13) { + psecuritypriv->dot11PrivacyAlgrthm = _WEP104_; + psecuritypriv->dot118021XGrpPrivacy = _WEP104_; + } + + psecuritypriv->dot11PrivacyKeyIndex = wep_key_idx; + } + + _rtw_memcpy(&(psecuritypriv->dot11DefKey[wep_key_idx].skey[0]), param->u.crypt.key, wep_key_len); + + psecuritypriv->dot11DefKeylen[wep_key_idx] = wep_key_len; + + rtw_ap_set_wep_key(padapter, param->u.crypt.key, wep_key_len, wep_key_idx, 1); + + goto exit; + + } + + if (!psta) { /* group key */ + if (param->u.crypt.set_tx == 0) { /* group key, TX only */ + if (strcmp(param->u.crypt.alg, "WEP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set WEP TX GTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + _rtw_memcpy(psecuritypriv->dot118021XGrpKey[param->u.crypt.idx].skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + psecuritypriv->dot118021XGrpPrivacy = _WEP40_; + if (param->u.crypt.key_len == 13) + psecuritypriv->dot118021XGrpPrivacy = _WEP104_; + + } else if (strcmp(param->u.crypt.alg, "TKIP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set TKIP TX GTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + psecuritypriv->dot118021XGrpPrivacy = _TKIP_; + _rtw_memcpy(psecuritypriv->dot118021XGrpKey[param->u.crypt.idx].skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + /* set mic key */ + _rtw_memcpy(psecuritypriv->dot118021XGrptxmickey[param->u.crypt.idx].skey, &(param->u.crypt.key[16]), 8); + _rtw_memcpy(psecuritypriv->dot118021XGrprxmickey[param->u.crypt.idx].skey, &(param->u.crypt.key[24]), 8); + psecuritypriv->busetkipkey = _TRUE; + + } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set CCMP TX GTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + psecuritypriv->dot118021XGrpPrivacy = _AES_; + _rtw_memcpy(psecuritypriv->dot118021XGrpKey[param->u.crypt.idx].skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + + } else if (strcmp(param->u.crypt.alg, "GCMP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set GCMP TX GTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + psecuritypriv->dot118021XGrpPrivacy = _GCMP_; + _rtw_memcpy(psecuritypriv->dot118021XGrpKey[param->u.crypt.idx].skey, + param->u.crypt.key, + (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + + } else if (strcmp(param->u.crypt.alg, "GCMP_256") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set GCMP_256 TX GTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + psecuritypriv->dot118021XGrpPrivacy = _GCMP_256_; + _rtw_memcpy(psecuritypriv->dot118021XGrpKey[param->u.crypt.idx].skey, + param->u.crypt.key, + (param->u.crypt.key_len > 32 ? 32 : param->u.crypt.key_len)); + + } else if (strcmp(param->u.crypt.alg, "CCMP_256") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set CCMP_256 TX GTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + psecuritypriv->dot118021XGrpPrivacy = _CCMP_256_; + _rtw_memcpy(psecuritypriv->dot118021XGrpKey[param->u.crypt.idx].skey, + param->u.crypt.key, + (param->u.crypt.key_len > 32 ? 32: param->u.crypt.key_len)); + + #ifdef CONFIG_IEEE80211W + } else if (strcmp(param->u.crypt.alg, "BIP") == 0) { + psecuritypriv->dot11wCipher = _BIP_CMAC_128_; + RTW_INFO(FUNC_ADPT_FMT" set TX CMAC-128 IGTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + _rtw_memcpy(padapter->securitypriv.dot11wBIPKey[param->u.crypt.idx].skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + padapter->securitypriv.dot11wBIPKeyid = param->u.crypt.idx; + psecuritypriv->dot11wBIPtxpn.val = RTW_GET_LE64(param->u.crypt.seq); + padapter->securitypriv.binstallBIPkey = _TRUE; + goto exit; + } else if (strcmp(param->u.crypt.alg, "BIP_GMAC_128") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set TX GMAC-128 IGTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + psecuritypriv->dot11wCipher = _BIP_GMAC_128_; + _rtw_memcpy(psecuritypriv->dot11wBIPKey[param->u.crypt.idx].skey, + param->u.crypt.key, param->u.crypt.key_len); + psecuritypriv->dot11wBIPKeyid = param->u.crypt.idx; + psecuritypriv->dot11wBIPtxpn.val = RTW_GET_LE64(param->u.crypt.seq); + psecuritypriv->binstallBIPkey = _TRUE; + goto exit; + } else if (strcmp(param->u.crypt.alg, "BIP_GMAC_256") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set TX GMAC-256 IGTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + psecuritypriv->dot11wCipher = _BIP_GMAC_256_; + _rtw_memcpy(psecuritypriv->dot11wBIPKey[param->u.crypt.idx].skey, + param->u.crypt.key, param->u.crypt.key_len); + padapter->securitypriv.dot11wBIPKeyid = param->u.crypt.idx; + psecuritypriv->dot11wBIPtxpn.val = RTW_GET_LE64(param->u.crypt.seq); + padapter->securitypriv.binstallBIPkey = _TRUE; + goto exit; + } else if (strcmp(param->u.crypt.alg, "BIP_CMAC_256") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set TX CMAC-256 IGTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + psecuritypriv->dot11wCipher = _BIP_CMAC_256_; + _rtw_memcpy(psecuritypriv->dot11wBIPKey[param->u.crypt.idx].skey, + param->u.crypt.key, param->u.crypt.key_len); + psecuritypriv->dot11wBIPKeyid = param->u.crypt.idx; + psecuritypriv->dot11wBIPtxpn.val = RTW_GET_LE64(param->u.crypt.seq); + psecuritypriv->binstallBIPkey = _TRUE; + goto exit; + #endif /* CONFIG_IEEE80211W */ + + } else if (strcmp(param->u.crypt.alg, "none") == 0) { + RTW_INFO(FUNC_ADPT_FMT" clear group key, idx:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx); + psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_; + } else { + RTW_WARN(FUNC_ADPT_FMT" set group key, not support\n" + , FUNC_ADPT_ARG(padapter)); + goto exit; + } + + psecuritypriv->dot118021XGrpKeyid = param->u.crypt.idx; + pbcmc_sta = rtw_get_bcmc_stainfo(padapter); + if (pbcmc_sta) { + pbcmc_sta->dot11txpn.val = RTW_GET_LE64(param->u.crypt.seq); + pbcmc_sta->ieee8021x_blocked = _FALSE; + pbcmc_sta->dot118021XPrivacy = psecuritypriv->dot118021XGrpPrivacy; /* rx will use bmc_sta's dot118021XPrivacy */ + } + psecuritypriv->binstallGrpkey = _TRUE; + psecuritypriv->dot11PrivacyAlgrthm = psecuritypriv->dot118021XGrpPrivacy;/* !!! */ + + rtw_ap_set_group_key(padapter, param->u.crypt.key, psecuritypriv->dot118021XGrpPrivacy, param->u.crypt.idx); + } + + goto exit; + + } + + if (psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_8021X && psta) { /* psk/802_1x */ + if (param->u.crypt.set_tx == 1) { + /* pairwise key */ + if (param->u.crypt.key_len == 32) + _rtw_memcpy(psta->dot118021x_UncstKey.skey, + param->u.crypt.key, + (param->u.crypt.key_len > 32 ? 32 : param->u.crypt.key_len)); + else + _rtw_memcpy(psta->dot118021x_UncstKey.skey, + param->u.crypt.key, + (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + + if (strcmp(param->u.crypt.alg, "WEP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set WEP PTK of "MAC_FMT" idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx, param->u.crypt.key_len); + psta->dot118021XPrivacy = _WEP40_; + if (param->u.crypt.key_len == 13) + psta->dot118021XPrivacy = _WEP104_; + + } else if (strcmp(param->u.crypt.alg, "TKIP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set TKIP PTK of "MAC_FMT" idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx, param->u.crypt.key_len); + psta->dot118021XPrivacy = _TKIP_; + /* set mic key */ + _rtw_memcpy(psta->dot11tkiptxmickey.skey, &(param->u.crypt.key[16]), 8); + _rtw_memcpy(psta->dot11tkiprxmickey.skey, &(param->u.crypt.key[24]), 8); + psecuritypriv->busetkipkey = _TRUE; + + } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set CCMP PTK of "MAC_FMT" idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx, param->u.crypt.key_len); + psta->dot118021XPrivacy = _AES_; + + } else if (strcmp(param->u.crypt.alg, "GCMP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set GCMP PTK of "MAC_FMT" idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx, param->u.crypt.key_len); + psta->dot118021XPrivacy = _GCMP_; + + } else if (strcmp(param->u.crypt.alg, "GCMP_256") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set GCMP_256 PTK of "MAC_FMT" idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx, param->u.crypt.key_len); + psta->dot118021XPrivacy = _GCMP_256_; + + } else if (strcmp(param->u.crypt.alg, "CCMP_256") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set CCMP_256 PTK of "MAC_FMT" idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx, param->u.crypt.key_len); + psta->dot118021XPrivacy = _CCMP_256_; + + } else if (strcmp(param->u.crypt.alg, "none") == 0) { + RTW_INFO(FUNC_ADPT_FMT" clear pairwise key of "MAC_FMT" idx:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx); + psta->dot118021XPrivacy = _NO_PRIVACY_; + } else { + RTW_WARN(FUNC_ADPT_FMT" set pairwise key of "MAC_FMT", not support\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr)); + goto exit; + } + + psta->dot11txpn.val = RTW_GET_LE64(param->u.crypt.seq); + psta->dot11rxpn.val = RTW_GET_LE64(param->u.crypt.seq); + psta->ieee8021x_blocked = _FALSE; + + if (psta->dot118021XPrivacy != _NO_PRIVACY_) { + psta->bpairwise_key_installed = _TRUE; + + /* WPA2 key-handshake has completed */ + if (psecuritypriv->ndisauthtype == Ndis802_11AuthModeWPA2PSK) + psta->state &= (~WIFI_UNDER_KEY_HANDSHAKE); + } + + rtw_ap_set_pairwise_key(padapter, psta); + } else { + /* peer's group key, RX only */ + #ifdef CONFIG_RTW_MESH + if (strcmp(param->u.crypt.alg, "CCMP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set CCMP GTK of "MAC_FMT", idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx, param->u.crypt.key_len); + psta->group_privacy = _AES_; + _rtw_memcpy(psta->gtk.skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + psta->gtk_bmp |= BIT(param->u.crypt.idx); + psta->gtk_pn.val = RTW_GET_LE64(param->u.crypt.seq); + + } else if (strcmp(param->u.crypt.alg, "GCMP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set GCMP GTK of "MAC_FMT", idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx, param->u.crypt.key_len); + psta->group_privacy = _GCMP_; + _rtw_memcpy(psta->gtk.skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + psta->gtk_bmp |= BIT(param->u.crypt.idx); + psta->gtk_pn.val = RTW_GET_LE64(param->u.crypt.seq); + + } else if (strcmp(param->u.crypt.alg, "CCMP_256") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set CCMP_256 GTK of "MAC_FMT", idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx, param->u.crypt.key_len); + psta->group_privacy = _CCMP_256_; + _rtw_memcpy(psta->gtk.skey, param->u.crypt.key, (param->u.crypt.key_len > 32 ? 32 : param->u.crypt.key_len)); + psta->gtk_bmp |= BIT(param->u.crypt.idx); + psta->gtk_pn.val = RTW_GET_LE64(param->u.crypt.seq); + + } else if (strcmp(param->u.crypt.alg, "GCMP_256") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set GCMP_256 GTK of "MAC_FMT", idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx, param->u.crypt.key_len); + psta->group_privacy = _GCMP_256_; + _rtw_memcpy(psta->gtk.skey, param->u.crypt.key, (param->u.crypt.key_len > 32 ? 32 : param->u.crypt.key_len)); + psta->gtk_bmp |= BIT(param->u.crypt.idx); + psta->gtk_pn.val = RTW_GET_LE64(param->u.crypt.seq); + + #ifdef CONFIG_IEEE80211W + } else if (strcmp(param->u.crypt.alg, "BIP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set CMAC-128 IGTK of "MAC_FMT", idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx, param->u.crypt.key_len); + psta->dot11wCipher = _BIP_CMAC_128_; + _rtw_memcpy(psta->igtk.skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + psta->igtk_bmp |= BIT(param->u.crypt.idx); + psta->igtk_id = param->u.crypt.idx; + psta->igtk_pn.val = RTW_GET_LE64(param->u.crypt.seq); + goto exit; + + } else if (strcmp(param->u.crypt.alg, "BIP_GMAC_128") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set GMAC-128 IGTK of "MAC_FMT", idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx, param->u.crypt.key_len); + psta->dot11wCipher = _BIP_GMAC_128_; + _rtw_memcpy(psta->igtk.skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + psta->igtk_bmp |= BIT(param->u.crypt.idx); + psta->igtk_id = param->u.crypt.idx; + psta->igtk_pn.val = RTW_GET_LE64(param->u.crypt.seq); + goto exit; + + } else if (strcmp(param->u.crypt.alg, "BIP_CMAC_256") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set CMAC-256 IGTK of "MAC_FMT", idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx, param->u.crypt.key_len); + psta->dot11wCipher = _BIP_CMAC_256_; + _rtw_memcpy(psta->igtk.skey, param->u.crypt.key, (param->u.crypt.key_len > 32 ? 32 : param->u.crypt.key_len)); + psta->igtk_bmp |= BIT(param->u.crypt.idx); + psta->igtk_id = param->u.crypt.idx; + psta->igtk_pn.val = RTW_GET_LE64(param->u.crypt.seq); + goto exit; + + } else if (strcmp(param->u.crypt.alg, "BIP_GMAC_256") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set GMAC-256 IGTK of "MAC_FMT", idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx, param->u.crypt.key_len); + psta->dot11wCipher = _BIP_GMAC_256_; + _rtw_memcpy(psta->igtk.skey, param->u.crypt.key, (param->u.crypt.key_len > 32 ? 32 : param->u.crypt.key_len)); + psta->igtk_bmp |= BIT(param->u.crypt.idx); + psta->igtk_id = param->u.crypt.idx; + psta->igtk_pn.val = RTW_GET_LE64(param->u.crypt.seq); + goto exit; + #endif /* CONFIG_IEEE80211W */ + + } else if (strcmp(param->u.crypt.alg, "none") == 0) { + RTW_INFO(FUNC_ADPT_FMT" clear group key of "MAC_FMT", idx:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx); + psta->group_privacy = _NO_PRIVACY_; + psta->gtk_bmp &= ~BIT(param->u.crypt.idx); + } else + #endif /* CONFIG_RTW_MESH */ + { + RTW_WARN(FUNC_ADPT_FMT" set group key of "MAC_FMT", not support\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr)); + goto exit; + } + + #ifdef CONFIG_RTW_MESH + rtw_ap_set_sta_key(padapter, psta->cmn.mac_addr, psta->group_privacy + , param->u.crypt.key, param->u.crypt.idx, 1); + #endif + } + + } + +exit: + return ret; +} +#endif /* CONFIG_AP_MODE */ + +static int rtw_cfg80211_set_encryption(struct net_device *dev, struct ieee_param *param) +{ + int ret = 0; + u32 wep_key_idx, wep_key_len; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct security_priv *psecuritypriv = &padapter->securitypriv; +#ifdef CONFIG_P2P + struct wifidirect_info *pwdinfo = &padapter->wdinfo; +#endif /* CONFIG_P2P */ + + RTW_INFO("%s\n", __func__); + + param->u.crypt.err = 0; + param->u.crypt.alg[IEEE_CRYPT_ALG_NAME_LEN - 1] = '\0'; + + if (is_broadcast_mac_addr(param->sta_addr)) { + if (param->u.crypt.idx >= WEP_KEYS + #ifdef CONFIG_IEEE80211W + && param->u.crypt.idx > BIP_MAX_KEYID + #endif + ) { + ret = -EINVAL; + goto exit; + } + } else { +#ifdef CONFIG_WAPI_SUPPORT + if (strcmp(param->u.crypt.alg, "SMS4")) +#endif + { + ret = -EINVAL; + goto exit; + } + } + + if (strcmp(param->u.crypt.alg, "WEP") == 0) { + RTW_INFO("wpa_set_encryption, crypt.alg = WEP\n"); + + wep_key_idx = param->u.crypt.idx; + wep_key_len = param->u.crypt.key_len; + + if ((wep_key_idx >= WEP_KEYS) || (wep_key_len <= 0)) { + ret = -EINVAL; + goto exit; + } + + if (psecuritypriv->bWepDefaultKeyIdxSet == 0) { + /* wep default key has not been set, so use this key index as default key. */ + + wep_key_len = wep_key_len <= 5 ? 5 : 13; + + psecuritypriv->ndisencryptstatus = Ndis802_11Encryption1Enabled; + psecuritypriv->dot11PrivacyAlgrthm = _WEP40_; + psecuritypriv->dot118021XGrpPrivacy = _WEP40_; + + if (wep_key_len == 13) { + psecuritypriv->dot11PrivacyAlgrthm = _WEP104_; + psecuritypriv->dot118021XGrpPrivacy = _WEP104_; + } + + psecuritypriv->dot11PrivacyKeyIndex = wep_key_idx; + } + + _rtw_memcpy(&(psecuritypriv->dot11DefKey[wep_key_idx].skey[0]), param->u.crypt.key, wep_key_len); + + psecuritypriv->dot11DefKeylen[wep_key_idx] = wep_key_len; + + rtw_set_key(padapter, psecuritypriv, wep_key_idx, 0, _TRUE); + + goto exit; + } + + if (padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) { /* 802_1x */ + struct sta_info *psta, *pbcmc_sta; + struct sta_priv *pstapriv = &padapter->stapriv; + + /* RTW_INFO("%s, : dot11AuthAlgrthm == dot11AuthAlgrthm_8021X\n", __func__); */ + + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_MP_STATE) == _TRUE) { /* sta mode */ +#ifdef CONFIG_RTW_80211R + if (rtw_ft_roam(padapter)) + psta = rtw_get_stainfo(pstapriv, pmlmepriv->assoc_bssid); + else +#endif + psta = rtw_get_stainfo(pstapriv, get_bssid(pmlmepriv)); + if (psta == NULL) { + /* DEBUG_ERR( ("Set wpa_set_encryption: Obtain Sta_info fail\n")); */ + RTW_INFO("%s, : Obtain Sta_info fail\n", __func__); + } else { + /* Jeff: don't disable ieee8021x_blocked while clearing key */ + if (strcmp(param->u.crypt.alg, "none") != 0) + psta->ieee8021x_blocked = _FALSE; + + if ((padapter->securitypriv.ndisencryptstatus == Ndis802_11Encryption2Enabled) || + (padapter->securitypriv.ndisencryptstatus == Ndis802_11Encryption3Enabled)) + psta->dot118021XPrivacy = padapter->securitypriv.dot11PrivacyAlgrthm; + + if (param->u.crypt.set_tx == 1) { /* pairwise key */ + RTW_INFO(FUNC_ADPT_FMT" set %s PTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.alg, param->u.crypt.idx, param->u.crypt.key_len); + + if (strcmp(param->u.crypt.alg, "GCMP_256") == 0 + || strcmp(param->u.crypt.alg, "CCMP_256") == 0) { + _rtw_memcpy(psta->dot118021x_UncstKey.skey, + param->u.crypt.key, + ((param->u.crypt.key_len > 32) ? + 32 : param->u.crypt.key_len)); + } else + _rtw_memcpy(psta->dot118021x_UncstKey.skey, + param->u.crypt.key, + (param->u.crypt.key_len > 16 ? + 16 : param->u.crypt.key_len)); + + if (strcmp(param->u.crypt.alg, "TKIP") == 0) { /* set mic key */ + _rtw_memcpy(psta->dot11tkiptxmickey.skey, &(param->u.crypt.key[16]), 8); + _rtw_memcpy(psta->dot11tkiprxmickey.skey, &(param->u.crypt.key[24]), 8); + padapter->securitypriv.busetkipkey = _FALSE; + } + psta->dot11txpn.val = RTW_GET_LE64(param->u.crypt.seq); + psta->dot11rxpn.val = RTW_GET_LE64(param->u.crypt.seq); + psta->bpairwise_key_installed = _TRUE; + #ifdef CONFIG_RTW_80211R + psta->ft_pairwise_key_installed = _TRUE; + #endif + rtw_setstakey_cmd(padapter, psta, UNICAST_KEY, _TRUE); + + } else { /* group key */ + if (strcmp(param->u.crypt.alg, "TKIP") == 0 + || strcmp(param->u.crypt.alg, "CCMP") == 0 + || strcmp(param->u.crypt.alg, "GCMP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set %s GTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.alg, param->u.crypt.idx, param->u.crypt.key_len); + _rtw_memcpy(padapter->securitypriv.dot118021XGrpKey[param->u.crypt.idx].skey, + param->u.crypt.key, + (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + _rtw_memcpy(padapter->securitypriv.dot118021XGrptxmickey[param->u.crypt.idx].skey, &(param->u.crypt.key[16]), 8); + _rtw_memcpy(padapter->securitypriv.dot118021XGrprxmickey[param->u.crypt.idx].skey, &(param->u.crypt.key[24]), 8); + padapter->securitypriv.binstallGrpkey = _TRUE; + if (param->u.crypt.idx < 4) + _rtw_memcpy(padapter->securitypriv.iv_seq[param->u.crypt.idx], param->u.crypt.seq, 8); + padapter->securitypriv.dot118021XGrpKeyid = param->u.crypt.idx; + rtw_set_key(padapter, &padapter->securitypriv, param->u.crypt.idx, 1, _TRUE); + } else if (strcmp(param->u.crypt.alg, "GCMP_256") == 0 + || strcmp(param->u.crypt.alg, "CCMP_256") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set %s GTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.alg, param->u.crypt.idx, param->u.crypt.key_len); + _rtw_memcpy( + padapter->securitypriv.dot118021XGrpKey[param->u.crypt.idx].skey, + param->u.crypt.key, + (param->u.crypt.key_len > 32 ? 32 : param->u.crypt.key_len)); + padapter->securitypriv.binstallGrpkey = _TRUE; + padapter->securitypriv.dot118021XGrpKeyid = param->u.crypt.idx; + rtw_set_key(padapter, &padapter->securitypriv, param->u.crypt.idx, 1, _TRUE); + #ifdef CONFIG_IEEE80211W + } else if (strcmp(param->u.crypt.alg, "BIP") == 0) { + psecuritypriv->dot11wCipher = _BIP_CMAC_128_; + RTW_INFO(FUNC_ADPT_FMT" set CMAC-128 IGTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + _rtw_memcpy(padapter->securitypriv.dot11wBIPKey[param->u.crypt.idx].skey, + param->u.crypt.key, + (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + psecuritypriv->dot11wBIPKeyid = param->u.crypt.idx; + psecuritypriv->dot11wBIPrxpn.val = RTW_GET_LE64(param->u.crypt.seq); + psecuritypriv->binstallBIPkey = _TRUE; + } else if (strcmp(param->u.crypt.alg, "BIP_GMAC_128") == 0) { + psecuritypriv->dot11wCipher = _BIP_GMAC_128_; + RTW_INFO(FUNC_ADPT_FMT" set GMAC-128 IGTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + _rtw_memcpy(padapter->securitypriv.dot11wBIPKey[param->u.crypt.idx].skey, + param->u.crypt.key, + (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + psecuritypriv->dot11wBIPKeyid = param->u.crypt.idx; + psecuritypriv->dot11wBIPrxpn.val = RTW_GET_LE64(param->u.crypt.seq); + psecuritypriv->binstallBIPkey = _TRUE; + } else if (strcmp(param->u.crypt.alg, "BIP_GMAC_256") == 0) { + psecuritypriv->dot11wCipher = _BIP_GMAC_256_; + RTW_INFO(FUNC_ADPT_FMT" set GMAC-256 IGTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + _rtw_memcpy(padapter->securitypriv.dot11wBIPKey[param->u.crypt.idx].skey, + param->u.crypt.key, + (param->u.crypt.key_len > 32 ? 32 : param->u.crypt.key_len)); + psecuritypriv->dot11wBIPKeyid = param->u.crypt.idx; + psecuritypriv->dot11wBIPrxpn.val = RTW_GET_LE64(param->u.crypt.seq); + psecuritypriv->binstallBIPkey = _TRUE; + } else if (strcmp(param->u.crypt.alg, "BIP_CMAC_256") == 0) { + psecuritypriv->dot11wCipher = _BIP_CMAC_256_; + RTW_INFO(FUNC_ADPT_FMT" set CMAC-256 IGTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + _rtw_memcpy(psecuritypriv->dot11wBIPKey[param->u.crypt.idx].skey, + param->u.crypt.key, param->u.crypt.key_len); + psecuritypriv->dot11wBIPKeyid = param->u.crypt.idx; + psecuritypriv->dot11wBIPrxpn.val = RTW_GET_LE64(param->u.crypt.seq); + psecuritypriv->binstallBIPkey = _TRUE; + #endif /* CONFIG_IEEE80211W */ + + } + +#ifdef CONFIG_P2P + if (pwdinfo->driver_interface == DRIVER_CFG80211) { + if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_PROVISIONING_ING)) + rtw_p2p_set_state(pwdinfo, P2P_STATE_PROVISIONING_DONE); + } +#endif /* CONFIG_P2P */ + + /* WPA/WPA2 key-handshake has completed */ + clr_fwstate(pmlmepriv, WIFI_UNDER_KEY_HANDSHAKE); + + } + } + + pbcmc_sta = rtw_get_bcmc_stainfo(padapter); + if (pbcmc_sta == NULL) { + /* DEBUG_ERR( ("Set OID_802_11_ADD_KEY: bcmc stainfo is null\n")); */ + } else { + /* Jeff: don't disable ieee8021x_blocked while clearing key */ + if (strcmp(param->u.crypt.alg, "none") != 0) + pbcmc_sta->ieee8021x_blocked = _FALSE; + + if ((padapter->securitypriv.ndisencryptstatus == Ndis802_11Encryption2Enabled) || + (padapter->securitypriv.ndisencryptstatus == Ndis802_11Encryption3Enabled)) + pbcmc_sta->dot118021XPrivacy = padapter->securitypriv.dot11PrivacyAlgrthm; + } + } else if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) { /* adhoc mode */ + } + } + + #ifdef CONFIG_WAPI_SUPPORT + if (strcmp(param->u.crypt.alg, "SMS4") == 0) + rtw_wapi_set_set_encryption(padapter, param); + #endif + +exit: + + RTW_INFO("%s, ret=%d\n", __func__, ret); + + + return ret; +} + +static int cfg80211_rtw_add_key(struct wiphy *wiphy, struct net_device *ndev + , u8 key_index +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + , bool pairwise +#endif + , const u8 *mac_addr, struct key_params *params) +{ + char *alg_name; + u32 param_len; + struct ieee_param *param = NULL; + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + struct wireless_dev *rtw_wdev = padapter->rtw_wdev; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; +#ifdef CONFIG_TDLS + struct sta_info *ptdls_sta; +#endif /* CONFIG_TDLS */ + + if (mac_addr) + RTW_INFO(FUNC_NDEV_FMT" adding key for %pM\n", FUNC_NDEV_ARG(ndev), mac_addr); + RTW_INFO(FUNC_NDEV_FMT" cipher=0x%x\n", FUNC_NDEV_ARG(ndev), params->cipher); + RTW_INFO(FUNC_NDEV_FMT" key_len=%d, key_index=%d\n", FUNC_NDEV_ARG(ndev), params->key_len, key_index); +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + RTW_INFO(FUNC_NDEV_FMT" pairwise=%d\n", FUNC_NDEV_ARG(ndev), pairwise); +#endif + + if (rtw_cfg80211_sync_iftype(padapter) != _SUCCESS) { + ret = -ENOTSUPP; + goto addkey_end; + } + + param_len = sizeof(struct ieee_param) + params->key_len; + param = rtw_malloc(param_len); + if (param == NULL) + return -1; + + _rtw_memset(param, 0, param_len); + + param->cmd = IEEE_CMD_SET_ENCRYPTION; + _rtw_memset(param->sta_addr, 0xff, ETH_ALEN); + + switch (params->cipher) { + case IW_AUTH_CIPHER_NONE: + /* todo: remove key */ + /* remove = 1; */ + alg_name = "none"; + break; + case WLAN_CIPHER_SUITE_WEP40: + case WLAN_CIPHER_SUITE_WEP104: + alg_name = "WEP"; + break; + case WLAN_CIPHER_SUITE_TKIP: + alg_name = "TKIP"; + break; + case WLAN_CIPHER_SUITE_CCMP: + alg_name = "CCMP"; + break; + case WIFI_CIPHER_SUITE_GCMP: + alg_name = "GCMP"; + break; + case WIFI_CIPHER_SUITE_GCMP_256: + alg_name = "GCMP_256"; + break; + case WIFI_CIPHER_SUITE_CCMP_256: + alg_name = "CCMP_256"; + break; +#ifdef CONFIG_IEEE80211W + case WLAN_CIPHER_SUITE_AES_CMAC: + alg_name = "BIP"; + break; + case WIFI_CIPHER_SUITE_BIP_GMAC_128: + alg_name = "BIP_GMAC_128"; + break; + case WIFI_CIPHER_SUITE_BIP_GMAC_256: + alg_name = "BIP_GMAC_256"; + break; + case WIFI_CIPHER_SUITE_BIP_CMAC_256: + alg_name = "BIP_CMAC_256"; + break; +#endif /* CONFIG_IEEE80211W */ +#ifdef CONFIG_WAPI_SUPPORT + case WLAN_CIPHER_SUITE_SMS4: + alg_name = "SMS4"; + if (pairwise == NL80211_KEYTYPE_PAIRWISE) { + if (key_index != 0 && key_index != 1) { + ret = -ENOTSUPP; + goto addkey_end; + } + _rtw_memcpy((void *)param->sta_addr, (void *)mac_addr, ETH_ALEN); + } else + RTW_INFO("mac_addr is null\n"); + RTW_INFO("rtw_wx_set_enc_ext: SMS4 case\n"); + break; +#endif + + default: + ret = -ENOTSUPP; + goto addkey_end; + } + + strncpy((char *)param->u.crypt.alg, alg_name, IEEE_CRYPT_ALG_NAME_LEN); + + + if (!mac_addr || is_broadcast_ether_addr(mac_addr) + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + || !pairwise + #endif + ) { + param->u.crypt.set_tx = 0; /* for wpa/wpa2 group key */ + } else { + param->u.crypt.set_tx = 1; /* for wpa/wpa2 pairwise key */ + } + + param->u.crypt.idx = key_index; + + if (params->seq_len && params->seq) { + _rtw_memcpy(param->u.crypt.seq, (u8 *)params->seq, params->seq_len); + RTW_INFO(FUNC_NDEV_FMT" seq_len:%u, seq:0x%llx\n", FUNC_NDEV_ARG(ndev) + , params->seq_len, RTW_GET_LE64(param->u.crypt.seq)); + } + + if (params->key_len && params->key) { + param->u.crypt.key_len = params->key_len; + _rtw_memcpy(param->u.crypt.key, (u8 *)params->key, params->key_len); + } + + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == _TRUE) { +#ifdef CONFIG_TDLS + if (rtw_tdls_is_driver_setup(padapter) == _FALSE && mac_addr) { + ptdls_sta = rtw_get_stainfo(&padapter->stapriv, (void *)mac_addr); + if (ptdls_sta != NULL && ptdls_sta->tdls_sta_state) { + _rtw_memcpy(ptdls_sta->tpk.tk, params->key, params->key_len); + rtw_tdls_set_key(padapter, ptdls_sta); + goto addkey_end; + } + } +#endif /* CONFIG_TDLS */ + ret = rtw_cfg80211_set_encryption(ndev, param); + } else if (MLME_IS_AP(padapter) || MLME_IS_MESH(padapter)) { +#ifdef CONFIG_AP_MODE + if (mac_addr) + _rtw_memcpy(param->sta_addr, (void *)mac_addr, ETH_ALEN); + + ret = rtw_cfg80211_ap_set_encryption(ndev, param); +#endif + } else if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == _TRUE + || check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == _TRUE + ) { + /* RTW_INFO("@@@@@@@@@@ fw_state=0x%x, iftype=%d\n", pmlmepriv->fw_state, rtw_wdev->iftype); */ + ret = rtw_cfg80211_set_encryption(ndev, param); + } else + RTW_INFO("error! fw_state=0x%x, iftype=%d\n", pmlmepriv->fw_state, rtw_wdev->iftype); + + +addkey_end: + if (param) + rtw_mfree(param, param_len); + + return ret; + +} + +static int cfg80211_rtw_get_key(struct wiphy *wiphy, struct net_device *ndev + , u8 keyid +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + , bool pairwise +#endif + , const u8 *mac_addr, void *cookie + , void (*callback)(void *cookie, struct key_params *)) +{ +#define GET_KEY_PARAM_FMT_S " keyid=%d" +#define GET_KEY_PARAM_ARG_S , keyid +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + #define GET_KEY_PARAM_FMT_2_6_37 ", pairwise=%d" + #define GET_KEY_PARAM_ARG_2_6_37 , pairwise +#else + #define GET_KEY_PARAM_FMT_2_6_37 "" + #define GET_KEY_PARAM_ARG_2_6_37 +#endif +#define GET_KEY_PARAM_FMT_E ", addr=%pM" +#define GET_KEY_PARAM_ARG_E , mac_addr + + _adapter *adapter = (_adapter *)rtw_netdev_priv(ndev); + struct security_priv *sec = &adapter->securitypriv; + struct sta_priv *stapriv = &adapter->stapriv; + struct sta_info *sta = NULL; + u32 cipher = _NO_PRIVACY_; + union Keytype *key = NULL; + u8 key_len = 0; + u64 *pn = NULL; + u8 pn_len = 0; + u8 pn_val[8] = {0}; + + struct key_params params; + int ret = -ENOENT; + + if (keyid >= WEP_KEYS + #ifdef CONFIG_IEEE80211W + && keyid > BIP_MAX_KEYID + #endif + ) + goto exit; + + if (!mac_addr || is_broadcast_ether_addr(mac_addr) + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + || (MLME_IS_STA(adapter) && !pairwise) + #endif + ) { + /* WEP key, TX GTK/IGTK, RX GTK/IGTK(for STA mode) */ + if (is_wep_enc(sec->dot118021XGrpPrivacy)) { + if (keyid >= WEP_KEYS) + goto exit; + if (!(sec->key_mask & BIT(keyid))) + goto exit; + cipher = sec->dot118021XGrpPrivacy; + key = &sec->dot11DefKey[keyid]; + } else { + if (keyid < WEP_KEYS) { + if (sec->binstallGrpkey != _TRUE) + goto exit; + cipher = sec->dot118021XGrpPrivacy; + key = &sec->dot118021XGrpKey[keyid]; + sta = rtw_get_bcmc_stainfo(adapter); + if (sta) + pn = &sta->dot11txpn.val; + #ifdef CONFIG_IEEE80211W + } else if (keyid <= BIP_MAX_KEYID) { + if (SEC_IS_BIP_KEY_INSTALLED(sec) != _TRUE) + goto exit; + cipher = sec->dot11wCipher; + key = &sec->dot11wBIPKey[keyid]; + pn = &sec->dot11wBIPtxpn.val; + #endif + } + } + } else { + /* Pairwise key, RX GTK/IGTK for specific peer */ + sta = rtw_get_stainfo(stapriv, mac_addr); + if (!sta) + goto exit; + + if (keyid < WEP_KEYS && pairwise) { + if (sta->bpairwise_key_installed != _TRUE) + goto exit; + cipher = sta->dot118021XPrivacy; + key = &sta->dot118021x_UncstKey; + #ifdef CONFIG_RTW_MESH + } else if (keyid < WEP_KEYS && !pairwise) { + if (!(sta->gtk_bmp & BIT(keyid))) + goto exit; + cipher = sta->group_privacy; + key = &sta->gtk; + #ifdef CONFIG_IEEE80211W + } else if (keyid <= BIP_MAX_KEYID && !pairwise) { + if (!(sta->igtk_bmp & BIT(keyid))) + goto exit; + cipher = sta->dot11wCipher; + key = &sta->igtk; + pn = &sta->igtk_pn.val; + #endif + #endif /* CONFIG_RTW_MESH */ + } + } + + if (!key) + goto exit; + + if (cipher == _WEP40_) { + cipher = WLAN_CIPHER_SUITE_WEP40; + key_len = sec->dot11DefKeylen[keyid]; + } else if (cipher == _WEP104_) { + cipher = WLAN_CIPHER_SUITE_WEP104; + key_len = sec->dot11DefKeylen[keyid]; + } else if (cipher == _TKIP_ || cipher == _TKIP_WTMIC_) { + cipher = WLAN_CIPHER_SUITE_TKIP; + key_len = 16; + } else if (cipher == _AES_) { + cipher = WLAN_CIPHER_SUITE_CCMP; + key_len = 16; +#ifdef CONFIG_WAPI_SUPPORT + } else if (cipher == _SMS4_) { + cipher = WLAN_CIPHER_SUITE_SMS4; + key_len = 16; +#endif + } else if (cipher == _GCMP_) { + cipher = WIFI_CIPHER_SUITE_GCMP; + key_len = 16; + } else if (cipher == _CCMP_256_) { + cipher = WIFI_CIPHER_SUITE_CCMP_256; + key_len = 32; + } else if (cipher == _GCMP_256_) { + cipher = WIFI_CIPHER_SUITE_GCMP_256; + key_len = 32; + #ifdef CONFIG_IEEE80211W + } else if (cipher == _BIP_CMAC_128_) { + cipher = WLAN_CIPHER_SUITE_AES_CMAC; + key_len = 16; + } else if (cipher == _BIP_GMAC_128_) { + cipher = WIFI_CIPHER_SUITE_BIP_GMAC_128; + key_len = 16; + } else if (cipher == _BIP_GMAC_256_) { + cipher = WIFI_CIPHER_SUITE_BIP_GMAC_256; + key_len = 32; + } else if (cipher == _BIP_CMAC_256_) { + cipher = WIFI_CIPHER_SUITE_BIP_CMAC_256; + key_len = 32; + #endif + } else { + RTW_WARN(FUNC_NDEV_FMT" unknown cipher:%u\n", FUNC_NDEV_ARG(ndev), cipher); + rtw_warn_on(1); + goto exit; + } + + if (pn) { + *((u64 *)pn_val) = cpu_to_le64(*pn); + pn_len = 6; + } + + ret = 0; + +exit: + RTW_INFO(FUNC_NDEV_FMT + GET_KEY_PARAM_FMT_S + GET_KEY_PARAM_FMT_2_6_37 + GET_KEY_PARAM_FMT_E + " ret %d\n", FUNC_NDEV_ARG(ndev) + GET_KEY_PARAM_ARG_S + GET_KEY_PARAM_ARG_2_6_37 + GET_KEY_PARAM_ARG_E + , ret); + if (pn) + RTW_INFO(FUNC_NDEV_FMT " seq:0x%llx\n", FUNC_NDEV_ARG(ndev), *pn); + + if (ret == 0) { + _rtw_memset(¶ms, 0, sizeof(params)); + + params.cipher = cipher; + params.key = key->skey; + params.key_len = key_len; + if (pn) { + params.seq = pn_val; + params.seq_len = pn_len; + } + + callback(cookie, ¶ms); + } + + return ret; +} + +static int cfg80211_rtw_del_key(struct wiphy *wiphy, struct net_device *ndev, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + u8 key_index, bool pairwise, const u8 *mac_addr) +#else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) */ + u8 key_index, const u8 *mac_addr) +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) */ +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + struct security_priv *psecuritypriv = &padapter->securitypriv; + + RTW_INFO(FUNC_NDEV_FMT" key_index=%d, addr=%pM\n", FUNC_NDEV_ARG(ndev), key_index, mac_addr); + + if (key_index == psecuritypriv->dot11PrivacyKeyIndex) { + /* clear the flag of wep default key set. */ + psecuritypriv->bWepDefaultKeyIdxSet = 0; + } + + return 0; +} + +static int cfg80211_rtw_set_default_key(struct wiphy *wiphy, + struct net_device *ndev, u8 key_index + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)) || defined(COMPAT_KERNEL_RELEASE) + , bool unicast, bool multicast + #endif +) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + struct security_priv *psecuritypriv = &padapter->securitypriv; + +#define SET_DEF_KEY_PARAM_FMT " key_index=%d" +#define SET_DEF_KEY_PARAM_ARG , key_index +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)) || defined(COMPAT_KERNEL_RELEASE) + #define SET_DEF_KEY_PARAM_FMT_2_6_38 ", unicast=%d, multicast=%d" + #define SET_DEF_KEY_PARAM_ARG_2_6_38 , unicast, multicast +#else + #define SET_DEF_KEY_PARAM_FMT_2_6_38 "" + #define SET_DEF_KEY_PARAM_ARG_2_6_38 +#endif + + RTW_INFO(FUNC_NDEV_FMT + SET_DEF_KEY_PARAM_FMT + SET_DEF_KEY_PARAM_FMT_2_6_38 + "\n", FUNC_NDEV_ARG(ndev) + SET_DEF_KEY_PARAM_ARG + SET_DEF_KEY_PARAM_ARG_2_6_38 + ); + + if ((key_index < WEP_KEYS) && ((psecuritypriv->dot11PrivacyAlgrthm == _WEP40_) || (psecuritypriv->dot11PrivacyAlgrthm == _WEP104_))) { /* set wep default key */ + psecuritypriv->ndisencryptstatus = Ndis802_11Encryption1Enabled; + + psecuritypriv->dot11PrivacyKeyIndex = key_index; + + psecuritypriv->dot11PrivacyAlgrthm = _WEP40_; + psecuritypriv->dot118021XGrpPrivacy = _WEP40_; + if (psecuritypriv->dot11DefKeylen[key_index] == 13) { + psecuritypriv->dot11PrivacyAlgrthm = _WEP104_; + psecuritypriv->dot118021XGrpPrivacy = _WEP104_; + } + + psecuritypriv->bWepDefaultKeyIdxSet = 1; /* set the flag to represent that wep default key has been set */ + } + + return 0; + +} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)) +int cfg80211_rtw_set_default_mgmt_key(struct wiphy *wiphy, + struct net_device *ndev, u8 key_index) +{ +#define SET_DEF_KEY_PARAM_FMT " key_index=%d" +#define SET_DEF_KEY_PARAM_ARG , key_index + + RTW_INFO(FUNC_NDEV_FMT + SET_DEF_KEY_PARAM_FMT + "\n", FUNC_NDEV_ARG(ndev) + SET_DEF_KEY_PARAM_ARG + ); + + return 0; +} +#endif + +#if defined(CONFIG_GTK_OL) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 1, 0)) +static int cfg80211_rtw_set_rekey_data(struct wiphy *wiphy, + struct net_device *ndev, + struct cfg80211_gtk_rekey_data *data) +{ + /*int i;*/ + struct sta_info *psta; + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct sta_priv *pstapriv = &padapter->stapriv; + struct security_priv *psecuritypriv = &(padapter->securitypriv); + + psta = rtw_get_stainfo(pstapriv, get_bssid(pmlmepriv)); + if (psta == NULL) { + RTW_INFO("%s, : Obtain Sta_info fail\n", __func__); + return -1; + } + + _rtw_memcpy(psta->kek, data->kek, NL80211_KEK_LEN); + /*printk("\ncfg80211_rtw_set_rekey_data KEK:"); + for(i=0;ikek[i]);*/ + _rtw_memcpy(psta->kck, data->kck, NL80211_KCK_LEN); + /*printk("\ncfg80211_rtw_set_rekey_data KCK:"); + for(i=0;ikck[i]);*/ + _rtw_memcpy(psta->replay_ctr, data->replay_ctr, NL80211_REPLAY_CTR_LEN); + psecuritypriv->binstallKCK_KEK = _TRUE; + /*printk("\nREPLAY_CTR: "); + for(i=0;ireplay_ctr[i]);*/ + + return 0; +} +#endif /*CONFIG_GTK_OL*/ + +#ifdef CONFIG_RTW_MESH +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) +static enum nl80211_mesh_power_mode rtw_mesh_ps_to_nl80211_mesh_power_mode(u8 ps) +{ + if (ps == RTW_MESH_PS_UNKNOWN) + return NL80211_MESH_POWER_UNKNOWN; + if (ps == RTW_MESH_PS_ACTIVE) + return NL80211_MESH_POWER_ACTIVE; + if (ps == RTW_MESH_PS_LSLEEP) + return NL80211_MESH_POWER_LIGHT_SLEEP; + if (ps == RTW_MESH_PS_DSLEEP) + return NL80211_MESH_POWER_DEEP_SLEEP; + + rtw_warn_on(1); + return NL80211_MESH_POWER_UNKNOWN; +} +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)) +enum nl80211_plink_state rtw_plink_state_to_nl80211_plink_state(u8 plink_state) +{ + if (plink_state == RTW_MESH_PLINK_UNKNOWN) + return NUM_NL80211_PLINK_STATES; + if (plink_state == RTW_MESH_PLINK_LISTEN) + return NL80211_PLINK_LISTEN; + if (plink_state == RTW_MESH_PLINK_OPN_SNT) + return NL80211_PLINK_OPN_SNT; + if (plink_state == RTW_MESH_PLINK_OPN_RCVD) + return NL80211_PLINK_OPN_RCVD; + if (plink_state == RTW_MESH_PLINK_CNF_RCVD) + return NL80211_PLINK_CNF_RCVD; + if (plink_state == RTW_MESH_PLINK_ESTAB) + return NL80211_PLINK_ESTAB; + if (plink_state == RTW_MESH_PLINK_HOLDING) + return NL80211_PLINK_HOLDING; + if (plink_state == RTW_MESH_PLINK_BLOCKED) + return NL80211_PLINK_BLOCKED; + + rtw_warn_on(1); + return NUM_NL80211_PLINK_STATES; +} + +u8 nl80211_plink_state_to_rtw_plink_state(enum nl80211_plink_state plink_state) +{ + if (plink_state == NL80211_PLINK_LISTEN) + return RTW_MESH_PLINK_LISTEN; + if (plink_state == NL80211_PLINK_OPN_SNT) + return RTW_MESH_PLINK_OPN_SNT; + if (plink_state == NL80211_PLINK_OPN_RCVD) + return RTW_MESH_PLINK_OPN_RCVD; + if (plink_state == NL80211_PLINK_CNF_RCVD) + return RTW_MESH_PLINK_CNF_RCVD; + if (plink_state == NL80211_PLINK_ESTAB) + return RTW_MESH_PLINK_ESTAB; + if (plink_state == NL80211_PLINK_HOLDING) + return RTW_MESH_PLINK_HOLDING; + if (plink_state == NL80211_PLINK_BLOCKED) + return RTW_MESH_PLINK_BLOCKED; + + rtw_warn_on(1); + return RTW_MESH_PLINK_UNKNOWN; +} +#endif + +static void rtw_cfg80211_fill_mesh_only_sta_info(struct mesh_plink_ent *plink, struct sta_info *sta, struct station_info *sinfo) +{ + sinfo->filled |= STATION_INFO_LLID; + sinfo->llid = plink->llid; + sinfo->filled |= STATION_INFO_PLID; + sinfo->plid = plink->plid; + sinfo->filled |= STATION_INFO_PLINK_STATE; + sinfo->plink_state = rtw_plink_state_to_nl80211_plink_state(plink->plink_state); + if (!sta && plink->scanned) { + sinfo->filled |= STATION_INFO_SIGNAL; + sinfo->signal = translate_percentage_to_dbm(plink->scanned->network.PhyInfo.SignalStrength); + sinfo->filled |= STATION_INFO_INACTIVE_TIME; + if (plink->plink_state == RTW_MESH_PLINK_UNKNOWN) + sinfo->inactive_time = 0 - 1; + else + sinfo->inactive_time = rtw_get_passing_time_ms(plink->scanned->last_scanned); + } +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) + if (sta) { + sinfo->filled |= STATION_INFO_LOCAL_PM; + sinfo->local_pm = rtw_mesh_ps_to_nl80211_mesh_power_mode(sta->local_mps); + sinfo->filled |= STATION_INFO_PEER_PM; + sinfo->peer_pm = rtw_mesh_ps_to_nl80211_mesh_power_mode(sta->peer_mps); + sinfo->filled |= STATION_INFO_NONPEER_PM; + sinfo->nonpeer_pm = rtw_mesh_ps_to_nl80211_mesh_power_mode(sta->nonpeer_mps); + } +#endif +} +#endif /* CONFIG_RTW_MESH */ + +static int cfg80211_rtw_get_station(struct wiphy *wiphy, + struct net_device *ndev, +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 16, 0)) + u8 *mac, +#else + const u8 *mac, +#endif + struct station_info *sinfo) +{ + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct sta_info *psta = NULL; + struct sta_priv *pstapriv = &padapter->stapriv; +#ifdef CONFIG_RTW_MESH + struct mesh_plink_ent *plink = NULL; +#endif + + sinfo->filled = 0; + + if (!mac) { + RTW_INFO(FUNC_NDEV_FMT" mac==%p\n", FUNC_NDEV_ARG(ndev), mac); + ret = -ENOENT; + goto exit; + } + + psta = rtw_get_stainfo(pstapriv, mac); +#ifdef CONFIG_RTW_MESH + if (MLME_IS_MESH(padapter)) { + if (psta) + plink = psta->plink; + if (!plink) + plink = rtw_mesh_plink_get(padapter, mac); + } +#endif /* CONFIG_RTW_MESH */ + + if ((!MLME_IS_MESH(padapter) && !psta) + #ifdef CONFIG_RTW_MESH + || (MLME_IS_MESH(padapter) && !plink) + #endif + ) { + RTW_INFO(FUNC_NDEV_FMT" no sta info for mac="MAC_FMT"\n" + , FUNC_NDEV_ARG(ndev), MAC_ARG(mac)); + ret = -ENOENT; + goto exit; + } + +#ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO(FUNC_NDEV_FMT" mac="MAC_FMT"\n", FUNC_NDEV_ARG(ndev), MAC_ARG(mac)); +#endif + + /* for infra./P2PClient mode */ + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) + && check_fwstate(pmlmepriv, WIFI_ASOC_STATE) + ) { + struct wlan_network *cur_network = &(pmlmepriv->cur_network); + + if (_rtw_memcmp((u8 *)mac, cur_network->network.MacAddress, ETH_ALEN) == _FALSE) { + RTW_INFO("%s, mismatch bssid="MAC_FMT"\n", __func__, MAC_ARG(cur_network->network.MacAddress)); + ret = -ENOENT; + goto exit; + } + + sinfo->filled |= STATION_INFO_SIGNAL; + sinfo->signal = translate_percentage_to_dbm(padapter->recvpriv.signal_strength); + + sinfo->filled |= STATION_INFO_TX_BITRATE; + sinfo->txrate.legacy = rtw_get_cur_max_rate(padapter); + } + + if (psta) { + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == _FALSE + || check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _FALSE + ) { + sinfo->filled |= STATION_INFO_SIGNAL; + sinfo->signal = translate_percentage_to_dbm(psta->cmn.rssi_stat.rssi); + } + sinfo->filled |= STATION_INFO_INACTIVE_TIME; + sinfo->inactive_time = rtw_get_passing_time_ms(psta->sta_stats.last_rx_time); + sinfo->filled |= STATION_INFO_RX_PACKETS; + sinfo->rx_packets = sta_rx_data_pkts(psta); + sinfo->filled |= STATION_INFO_TX_PACKETS; + sinfo->tx_packets = psta->sta_stats.tx_pkts; + sinfo->filled |= STATION_INFO_TX_FAILED; + sinfo->tx_failed = psta->sta_stats.tx_fail_cnt; + } + +#ifdef CONFIG_RTW_MESH + if (MLME_IS_MESH(padapter)) + rtw_cfg80211_fill_mesh_only_sta_info(plink, psta, sinfo); +#endif + +exit: + return ret; +} + +extern int netdev_open(struct net_device *pnetdev); + +#if 0 +enum nl80211_iftype { + NL80211_IFTYPE_UNSPECIFIED, + NL80211_IFTYPE_ADHOC, /* 1 */ + NL80211_IFTYPE_STATION, /* 2 */ + NL80211_IFTYPE_AP, /* 3 */ + NL80211_IFTYPE_AP_VLAN, + NL80211_IFTYPE_WDS, + NL80211_IFTYPE_MONITOR, /* 6 */ + NL80211_IFTYPE_MESH_POINT, + NL80211_IFTYPE_P2P_CLIENT, /* 8 */ + NL80211_IFTYPE_P2P_GO, /* 9 */ + /* keep last */ + NUM_NL80211_IFTYPES, + NL80211_IFTYPE_MAX = NUM_NL80211_IFTYPES - 1 +}; +#endif +static int cfg80211_rtw_change_iface(struct wiphy *wiphy, + struct net_device *ndev, + enum nl80211_iftype type, +#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0)) + u32 *flags, +#endif + struct vif_params *params) +{ + enum nl80211_iftype old_type; + NDIS_802_11_NETWORK_INFRASTRUCTURE networkType; + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + struct wireless_dev *rtw_wdev = padapter->rtw_wdev; + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); +#ifdef CONFIG_P2P + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); +#endif +#ifdef CONFIG_MONITOR_MODE_XMIT + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); +#endif + int ret = 0; + u8 change = _FALSE; + + RTW_INFO(FUNC_NDEV_FMT" type=%d, hw_port:%d\n", FUNC_NDEV_ARG(ndev), type, padapter->hw_port); + + if (adapter_to_dvobj(padapter)->processing_dev_remove == _TRUE) { + ret = -EPERM; + goto exit; + } + + + RTW_INFO(FUNC_NDEV_FMT" call netdev_open\n", FUNC_NDEV_ARG(ndev)); + if (netdev_open(ndev) != 0) { + RTW_INFO(FUNC_NDEV_FMT" call netdev_open fail\n", FUNC_NDEV_ARG(ndev)); + ret = -EPERM; + goto exit; + } + + + if (_FAIL == rtw_pwr_wakeup(padapter)) { + RTW_INFO(FUNC_NDEV_FMT" call rtw_pwr_wakeup fail\n", FUNC_NDEV_ARG(ndev)); + ret = -EPERM; + goto exit; + } + + old_type = rtw_wdev->iftype; + RTW_INFO(FUNC_NDEV_FMT" old_iftype=%d, new_iftype=%d\n", + FUNC_NDEV_ARG(ndev), old_type, type); + + if (old_type != type) { + change = _TRUE; + pmlmeext->action_public_rxseq = 0xffff; + pmlmeext->action_public_dialog_token = 0xff; + } + + /* initial default type */ + ndev->type = ARPHRD_ETHER; + + /* + * Disable Power Save in moniter mode, + * and enable it after leaving moniter mode. + */ + if (type == NL80211_IFTYPE_MONITOR) { + rtw_ps_deny(padapter, PS_DENY_MONITOR_MODE); + LeaveAllPowerSaveMode(padapter); + } else if (old_type == NL80211_IFTYPE_MONITOR) { + /* driver in moniter mode in last time */ + rtw_ps_deny_cancel(padapter, PS_DENY_MONITOR_MODE); + } + + switch (type) { + case NL80211_IFTYPE_ADHOC: + networkType = Ndis802_11IBSS; + break; + + case NL80211_IFTYPE_STATION: + networkType = Ndis802_11Infrastructure; + #ifdef CONFIG_P2P + if (change && pwdinfo->driver_interface == DRIVER_CFG80211) { + #if !RTW_P2P_GROUP_INTERFACE + if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_CLIENT) + || rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO) + ) { + /* it means remove GC/GO and change mode from GC/GO to station(P2P DEVICE) */ + rtw_p2p_set_role(pwdinfo, P2P_ROLE_DEVICE); + } + #endif + } + #endif /* CONFIG_P2P */ + break; + + case NL80211_IFTYPE_AP: + networkType = Ndis802_11APMode; + #ifdef CONFIG_P2P + if (change && pwdinfo->driver_interface == DRIVER_CFG80211) { + #if !RTW_P2P_GROUP_INTERFACE + if (!rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) { + /* it means P2P Group created, we will be GO and change mode from P2P DEVICE to AP(GO) */ + rtw_p2p_set_role(pwdinfo, P2P_ROLE_GO); + } + #endif + } + #endif /* CONFIG_P2P */ + break; + +#if defined(CONFIG_P2P) && ((LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE)) + case NL80211_IFTYPE_P2P_CLIENT: + networkType = Ndis802_11Infrastructure; + if (change && pwdinfo->driver_interface == DRIVER_CFG80211) { + if (!rtw_p2p_enable(padapter, P2P_ROLE_CLIENT)) { + ret = -EOPNOTSUPP; + goto exit; + } + } + break; + + case NL80211_IFTYPE_P2P_GO: + networkType = Ndis802_11APMode; + if (change && pwdinfo->driver_interface == DRIVER_CFG80211) { + if (!rtw_p2p_enable(padapter, P2P_ROLE_GO)) { + ret = -EOPNOTSUPP; + goto exit; + } + } + break; +#endif /* defined(CONFIG_P2P) && ((LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE)) */ + +#ifdef CONFIG_RTW_MESH + case NL80211_IFTYPE_MESH_POINT: + networkType = Ndis802_11_mesh; + break; +#endif + +#ifdef CONFIG_WIFI_MONITOR + case NL80211_IFTYPE_MONITOR: + networkType = Ndis802_11Monitor; + +#ifdef CONFIG_CUSTOMER_ALIBABA_GENERAL + ndev->type = ARPHRD_IEEE80211; /* IEEE 802.11 : 801 */ +#else + ndev->type = ARPHRD_IEEE80211_RADIOTAP; /* IEEE 802.11 + radiotap header : 803 */ +#endif + break; +#endif /* CONFIG_WIFI_MONITOR */ + default: + ret = -EOPNOTSUPP; + goto exit; + } + + rtw_wdev->iftype = type; + + if (rtw_set_802_11_infrastructure_mode(padapter, networkType, 0) == _FALSE) { + rtw_wdev->iftype = old_type; + ret = -EPERM; + goto exit; + } + + rtw_setopmode_cmd(padapter, networkType, RTW_CMDF_WAIT_ACK); +#ifdef CONFIG_MONITOR_MODE_XMIT + if (check_fwstate(pmlmepriv, WIFI_MONITOR_STATE) == _TRUE) + rtw_indicate_connect(padapter); +#endif + + #if defined(CONFIG_RTW_WDS) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)) + if (params->use_4addr != -1) { + RTW_INFO(FUNC_NDEV_FMT" use_4addr=%d\n" + , FUNC_NDEV_ARG(ndev), params->use_4addr); + adapter_set_use_wds(padapter, params->use_4addr); + } + #endif + +exit: + + RTW_INFO(FUNC_NDEV_FMT" ret:%d\n", FUNC_NDEV_ARG(ndev), ret); + return ret; +} + +void rtw_cfg80211_indicate_scan_done(_adapter *adapter, bool aborted) +{ + struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(adapter); + _irqL irqL; + +#if (KERNEL_VERSION(4, 8, 0) <= LINUX_VERSION_CODE) + struct cfg80211_scan_info info; + + memset(&info, 0, sizeof(info)); + info.aborted = aborted; +#endif + + _enter_critical_bh(&pwdev_priv->scan_req_lock, &irqL); + if (pwdev_priv->scan_request != NULL) { + #ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("%s with scan req\n", __FUNCTION__); + #endif + + /* avoid WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req); */ + if (pwdev_priv->scan_request->wiphy != pwdev_priv->rtw_wdev->wiphy) + RTW_INFO("error wiphy compare\n"); + else +#if (KERNEL_VERSION(4, 8, 0) <= LINUX_VERSION_CODE) + cfg80211_scan_done(pwdev_priv->scan_request, &info); +#else + cfg80211_scan_done(pwdev_priv->scan_request, aborted); +#endif + + pwdev_priv->scan_request = NULL; + } else { + #ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("%s without scan req\n", __FUNCTION__); + #endif + } + _exit_critical_bh(&pwdev_priv->scan_req_lock, &irqL); +} + +u32 rtw_cfg80211_wait_scan_req_empty(_adapter *adapter, u32 timeout_ms) +{ + struct rtw_wdev_priv *wdev_priv = adapter_wdev_data(adapter); + u8 empty = _FALSE; + systime start; + u32 pass_ms; + + start = rtw_get_current_time(); + + while (rtw_get_passing_time_ms(start) <= timeout_ms) { + + if (RTW_CANNOT_RUN(adapter)) + break; + + if (!wdev_priv->scan_request) { + empty = _TRUE; + break; + } + + rtw_msleep_os(10); + } + + pass_ms = rtw_get_passing_time_ms(start); + + if (empty == _FALSE && pass_ms > timeout_ms) + RTW_PRINT(FUNC_ADPT_FMT" pass_ms:%u, timeout\n" + , FUNC_ADPT_ARG(adapter), pass_ms); + + return pass_ms; +} + +void rtw_cfg80211_unlink_bss(_adapter *padapter, struct wlan_network *pnetwork) +{ + struct wireless_dev *pwdev = padapter->rtw_wdev; + struct wiphy *wiphy = pwdev->wiphy; + struct cfg80211_bss *bss = NULL; + WLAN_BSSID_EX select_network = pnetwork->network; + + bss = cfg80211_get_bss(wiphy, NULL/*notify_channel*/, + select_network.MacAddress, select_network.Ssid.Ssid, + select_network.Ssid.SsidLength, +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0) + select_network.InfrastructureMode == Ndis802_11Infrastructure?IEEE80211_BSS_TYPE_ESS:IEEE80211_BSS_TYPE_IBSS, + IEEE80211_PRIVACY(select_network.Privacy)); +#else + select_network.InfrastructureMode == Ndis802_11Infrastructure?WLAN_CAPABILITY_ESS:WLAN_CAPABILITY_IBSS, + select_network.InfrastructureMode == Ndis802_11Infrastructure?WLAN_CAPABILITY_ESS:WLAN_CAPABILITY_IBSS); +#endif + + if (bss) { + cfg80211_unlink_bss(wiphy, bss); + RTW_INFO("%s(): cfg80211_unlink %s!!\n", __func__, select_network.Ssid.Ssid); +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0) + cfg80211_put_bss(padapter->rtw_wdev->wiphy, bss); +#else + cfg80211_put_bss(bss); +#endif + } + return; +} + +/* if target wps scan ongoing, target_ssid is filled */ +int rtw_cfg80211_is_target_wps_scan(struct cfg80211_scan_request *scan_req, struct cfg80211_ssid *target_ssid) +{ + int ret = 0; + + if (scan_req->n_ssids != 1 + || scan_req->ssids[0].ssid_len == 0 + || scan_req->n_channels != 1 + ) + goto exit; + + /* under target WPS scan */ + _rtw_memcpy(target_ssid, scan_req->ssids, sizeof(struct cfg80211_ssid)); + ret = 1; + +exit: + return ret; +} + +static void _rtw_cfg80211_surveydone_event_callback(_adapter *padapter, struct cfg80211_scan_request *scan_req) +{ + struct rf_ctl_t *rfctl = adapter_to_rfctl(padapter); + RT_CHANNEL_INFO *chset = rfctl->channel_set; + _irqL irqL; + _list *plist, *phead; + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + _queue *queue = &(pmlmepriv->scanned_queue); + struct wlan_network *pnetwork = NULL; + struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(padapter); + struct cfg80211_ssid target_ssid; + u8 target_wps_scan = 0; + u8 ch; + +#ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("%s\n", __func__); +#endif + + if (scan_req) + target_wps_scan = rtw_cfg80211_is_target_wps_scan(scan_req, &target_ssid); + else { + _enter_critical_bh(&pwdev_priv->scan_req_lock, &irqL); + if (pwdev_priv->scan_request != NULL) + target_wps_scan = rtw_cfg80211_is_target_wps_scan(pwdev_priv->scan_request, &target_ssid); + _exit_critical_bh(&pwdev_priv->scan_req_lock, &irqL); + } + + _enter_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + phead = get_list_head(queue); + plist = get_next(phead); + + while (1) { + if (rtw_end_of_queue_search(phead, plist) == _TRUE) + break; + + pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list); + ch = pnetwork->network.Configuration.DSConfig; + + /* report network only if the current channel set contains the channel to which this network belongs */ + if (rtw_chset_search_ch(chset, ch) >= 0 + && rtw_mlme_band_check(padapter, ch) == _TRUE + && _TRUE == rtw_validate_ssid(&(pnetwork->network.Ssid)) + && (!IS_DFS_SLAVE_WITH_RD(rfctl) + || rtw_rfctl_dfs_domain_unknown(rfctl) + || !rtw_chset_is_ch_non_ocp(chset, ch)) + ) { + if (target_wps_scan) + rtw_cfg80211_clear_wps_sr_of_non_target_bss(padapter, pnetwork, &target_ssid); + rtw_cfg80211_inform_bss(padapter, pnetwork); + } +#if 0 + /* check ralink testbed RSN IE length */ + { + if (_rtw_memcmp(pnetwork->network.Ssid.Ssid, "Ralink_11n_AP", 13)) { + uint ie_len = 0; + u8 *p = NULL; + p = rtw_get_ie(pnetwork->network.IEs + _BEACON_IE_OFFSET_, _RSN_IE_2_, &ie_len, (pnetwork->network.IELength - _BEACON_IE_OFFSET_)); + RTW_INFO("ie_len=%d\n", ie_len); + } + } +#endif + plist = get_next(plist); + + } + + _exit_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); +} + +inline void rtw_cfg80211_surveydone_event_callback(_adapter *padapter) +{ + _rtw_cfg80211_surveydone_event_callback(padapter, NULL); +} + +static int rtw_cfg80211_set_probe_req_wpsp2pie(_adapter *padapter, char *buf, int len) +{ + int ret = 0; + uint wps_ielen = 0; + u8 *wps_ie; + u32 p2p_ielen = 0; + u8 *p2p_ie; + u32 wfd_ielen = 0; + u8 *wfd_ie; + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + +#ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("%s, ielen=%d\n", __func__, len); +#endif + + if (len > 0) { + wps_ie = rtw_get_wps_ie(buf, len, NULL, &wps_ielen); + if (wps_ie) { + #ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("probe_req_wps_ielen=%d\n", wps_ielen); + #endif + + if (pmlmepriv->wps_probe_req_ie) { + u32 free_len = pmlmepriv->wps_probe_req_ie_len; + pmlmepriv->wps_probe_req_ie_len = 0; + rtw_mfree(pmlmepriv->wps_probe_req_ie, free_len); + pmlmepriv->wps_probe_req_ie = NULL; + } + + pmlmepriv->wps_probe_req_ie = rtw_malloc(wps_ielen); + if (pmlmepriv->wps_probe_req_ie == NULL) { + RTW_INFO("%s()-%d: rtw_malloc() ERROR!\n", __FUNCTION__, __LINE__); + return -EINVAL; + + } + _rtw_memcpy(pmlmepriv->wps_probe_req_ie, wps_ie, wps_ielen); + pmlmepriv->wps_probe_req_ie_len = wps_ielen; + } + + /* buf += wps_ielen; */ + /* len -= wps_ielen; */ + + #ifdef CONFIG_P2P + p2p_ie = rtw_get_p2p_ie(buf, len, NULL, &p2p_ielen); + if (p2p_ie) { + struct wifidirect_info *wdinfo = &padapter->wdinfo; + u32 attr_contentlen = 0; + u8 listen_ch_attr[5]; + + #ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("probe_req_p2p_ielen=%d\n", p2p_ielen); + #endif + + if (pmlmepriv->p2p_probe_req_ie) { + u32 free_len = pmlmepriv->p2p_probe_req_ie_len; + pmlmepriv->p2p_probe_req_ie_len = 0; + rtw_mfree(pmlmepriv->p2p_probe_req_ie, free_len); + pmlmepriv->p2p_probe_req_ie = NULL; + } + + pmlmepriv->p2p_probe_req_ie = rtw_malloc(p2p_ielen); + if (pmlmepriv->p2p_probe_req_ie == NULL) { + RTW_INFO("%s()-%d: rtw_malloc() ERROR!\n", __FUNCTION__, __LINE__); + return -EINVAL; + + } + _rtw_memcpy(pmlmepriv->p2p_probe_req_ie, p2p_ie, p2p_ielen); + pmlmepriv->p2p_probe_req_ie_len = p2p_ielen; + + if (rtw_get_p2p_attr_content(p2p_ie, p2p_ielen, P2P_ATTR_LISTEN_CH, (u8 *)listen_ch_attr, (uint *) &attr_contentlen) + && attr_contentlen == 5) { + if (wdinfo->listen_channel != listen_ch_attr[4]) { + RTW_INFO(FUNC_ADPT_FMT" listen channel - country:%c%c%c, class:%u, ch:%u\n", + FUNC_ADPT_ARG(padapter), listen_ch_attr[0], listen_ch_attr[1], listen_ch_attr[2], + listen_ch_attr[3], listen_ch_attr[4]); + wdinfo->listen_channel = listen_ch_attr[4]; + } + } + } + #endif /* CONFIG_P2P */ + + #ifdef CONFIG_WFD + wfd_ie = rtw_get_wfd_ie(buf, len, NULL, &wfd_ielen); + if (wfd_ie) { + #ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("probe_req_wfd_ielen=%d\n", wfd_ielen); + #endif + + if (rtw_mlme_update_wfd_ie_data(pmlmepriv, MLME_PROBE_REQ_IE, wfd_ie, wfd_ielen) != _SUCCESS) + return -EINVAL; + } + #endif /* CONFIG_WFD */ + + #ifdef CONFIG_RTW_MBO + rtw_mbo_update_ie_data(padapter, buf, len); + #endif + } + + return ret; + +} + +#ifdef CONFIG_CONCURRENT_MODE +u8 rtw_cfg80211_scan_via_buddy(_adapter *padapter, struct cfg80211_scan_request *request) +{ + int i; + u8 ret = _FALSE; + _adapter *iface = NULL; + _irqL irqL; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(padapter); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + + for (i = 0; i < dvobj->iface_nums; i++) { + struct mlme_priv *buddy_mlmepriv; + struct rtw_wdev_priv *buddy_wdev_priv; + + iface = dvobj->padapters[i]; + if (iface == NULL) + continue; + + if (iface == padapter) + continue; + + if (rtw_is_adapter_up(iface) == _FALSE) + continue; + + buddy_mlmepriv = &iface->mlmepriv; + if (!check_fwstate(buddy_mlmepriv, WIFI_UNDER_SURVEY)) + continue; + + buddy_wdev_priv = adapter_wdev_data(iface); + _enter_critical_bh(&pwdev_priv->scan_req_lock, &irqL); + _enter_critical_bh(&buddy_wdev_priv->scan_req_lock, &irqL); + if (buddy_wdev_priv->scan_request) { + pmlmepriv->scanning_via_buddy_intf = _TRUE; + _enter_critical_bh(&pmlmepriv->lock, &irqL); + set_fwstate(pmlmepriv, WIFI_UNDER_SURVEY); + _exit_critical_bh(&pmlmepriv->lock, &irqL); + pwdev_priv->scan_request = request; + ret = _TRUE; + } + _exit_critical_bh(&buddy_wdev_priv->scan_req_lock, &irqL); + _exit_critical_bh(&pwdev_priv->scan_req_lock, &irqL); + + if (ret == _TRUE) + goto exit; + } + +exit: + return ret; +} + +void rtw_cfg80211_indicate_scan_done_for_buddy(_adapter *padapter, bool bscan_aborted) +{ + int i; + _adapter *iface = NULL; + _irqL irqL; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mlme_priv *mlmepriv; + struct rtw_wdev_priv *wdev_priv; + bool indicate_buddy_scan; + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if ((iface) && rtw_is_adapter_up(iface)) { + + if (iface == padapter) + continue; + + mlmepriv = &(iface->mlmepriv); + wdev_priv = adapter_wdev_data(iface); + + indicate_buddy_scan = _FALSE; + _enter_critical_bh(&wdev_priv->scan_req_lock, &irqL); + if (mlmepriv->scanning_via_buddy_intf == _TRUE) { + mlmepriv->scanning_via_buddy_intf = _FALSE; + clr_fwstate(mlmepriv, WIFI_UNDER_SURVEY); + if (wdev_priv->scan_request) + indicate_buddy_scan = _TRUE; + } + _exit_critical_bh(&wdev_priv->scan_req_lock, &irqL); + + if (indicate_buddy_scan == _TRUE) { + rtw_cfg80211_surveydone_event_callback(iface); + rtw_indicate_scan_done(iface, bscan_aborted); + } + + } + } +} +#endif /* CONFIG_CONCURRENT_MODE */ + +static int cfg80211_rtw_scan(struct wiphy *wiphy + #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0)) + , struct net_device *ndev + #endif + , struct cfg80211_scan_request *request) +{ + int i; + u8 _status = _FALSE; + int ret = 0; + struct sitesurvey_parm parm; + _irqL irqL; + u8 survey_times = 3; + u8 survey_times_for_one_ch = 6; + struct cfg80211_ssid *ssids = request->ssids; + int social_channel = 0, j = 0; + bool need_indicate_scan_done = _FALSE; + bool ps_denied = _FALSE; + u8 ssc_chk; + _adapter *padapter; + struct wireless_dev *wdev; + struct rtw_wdev_priv *pwdev_priv; + struct mlme_priv *pmlmepriv = NULL; +#ifdef CONFIG_P2P + struct wifidirect_info *pwdinfo; +#endif /* CONFIG_P2P */ + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + wdev = request->wdev; + #if defined(RTW_DEDICATED_P2P_DEVICE) + if (wdev == wiphy_to_pd_wdev(wiphy)) + padapter = wiphy_to_adapter(wiphy); + else + #endif + if (wdev_to_ndev(wdev)) + padapter = (_adapter *)rtw_netdev_priv(wdev_to_ndev(wdev)); + else { + ret = -EINVAL; + goto exit; + } +#else + if (ndev == NULL) { + ret = -EINVAL; + goto exit; + } + padapter = (_adapter *)rtw_netdev_priv(ndev); + wdev = ndev_to_wdev(ndev); +#endif + + pwdev_priv = adapter_wdev_data(padapter); + pmlmepriv = &padapter->mlmepriv; +#ifdef CONFIG_P2P + pwdinfo = &(padapter->wdinfo); +#endif /* CONFIG_P2P */ + + RTW_INFO(FUNC_ADPT_FMT"%s\n", FUNC_ADPT_ARG(padapter) + , wdev == wiphy_to_pd_wdev(wiphy) ? " PD" : ""); + +#ifdef CONFIG_RTW_SCAN_RAND +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)) + if (request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) { + get_random_mask_addr(pwdev_priv->pno_mac_addr, request->mac_addr, + request->mac_addr_mask); + print_hex_dump(KERN_DEBUG, "random mac_addr: ", + DUMP_PREFIX_OFFSET, 16, 1, pwdev_priv->pno_mac_addr, ETH_ALEN, 1); + } + else + memset(pwdev_priv->pno_mac_addr, 0xFF, ETH_ALEN); + +#endif +#endif + + +#if 1 + ssc_chk = rtw_sitesurvey_condition_check(padapter, _TRUE); + + if (ssc_chk == SS_DENY_MP_MODE) + goto bypass_p2p_chk; +#ifdef DBG_LA_MODE + if (ssc_chk == SS_DENY_LA_MODE) + goto bypass_p2p_chk; +#endif +#ifdef CONFIG_P2P + if (pwdinfo->driver_interface == DRIVER_CFG80211) { + if (request->n_ssids && ssids + && _rtw_memcmp(ssids[0].ssid, "DIRECT-", 7) + && rtw_get_p2p_ie((u8 *)request->ie, request->ie_len, NULL, NULL) + ) { + if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) { + if (!rtw_p2p_enable(padapter, P2P_ROLE_DEVICE)) { + ret = -EOPNOTSUPP; + goto exit; + } + } else { + rtw_p2p_set_pre_state(pwdinfo, rtw_p2p_state(pwdinfo)); + #ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("%s, role=%d, p2p_state=%d\n", __func__, rtw_p2p_role(pwdinfo), rtw_p2p_state(pwdinfo)); + #endif + } + rtw_p2p_set_state(pwdinfo, P2P_STATE_LISTEN); + + if (request->n_channels == 3 && + request->channels[0]->hw_value == 1 && + request->channels[1]->hw_value == 6 && + request->channels[2]->hw_value == 11 + ) + social_channel = 1; + } + } +#endif /*CONFIG_P2P*/ + + if (request->ie && request->ie_len > 0) + rtw_cfg80211_set_probe_req_wpsp2pie(padapter, (u8 *)request->ie, request->ie_len); + +bypass_p2p_chk: + + switch (ssc_chk) { + case SS_ALLOW : + break; + + case SS_DENY_MP_MODE: + ret = -EPERM; + goto exit; + #ifdef DBG_LA_MODE + case SS_DENY_LA_MODE: + ret = -EPERM; + goto exit; + #endif + #ifdef CONFIG_RTW_REPEATER_SON + case SS_DENY_RSON_SCANING : + #endif + case SS_DENY_BLOCK_SCAN : + case SS_DENY_SELF_AP_UNDER_WPS : + case SS_DENY_SELF_AP_UNDER_LINKING : + case SS_DENY_SELF_AP_UNDER_SURVEY : + case SS_DENY_SELF_STA_UNDER_SURVEY : + #ifdef CONFIG_CONCURRENT_MODE + case SS_DENY_BUDDY_UNDER_LINK_WPS : + #endif + case SS_DENY_BUSY_TRAFFIC : + case SS_DENY_ADAPTIVITY: + need_indicate_scan_done = _TRUE; + goto check_need_indicate_scan_done; + + case SS_DENY_BY_DRV : + #ifdef CONFIG_NOTIFY_SCAN_ABORT_WITH_BUSY + ret = -EBUSY; + goto exit; + #else + need_indicate_scan_done = _TRUE; + goto check_need_indicate_scan_done; + #endif + break; + + case SS_DENY_SELF_STA_UNDER_LINKING : + ret = -EBUSY; + goto check_need_indicate_scan_done; + + #ifdef CONFIG_CONCURRENT_MODE + case SS_DENY_BUDDY_UNDER_SURVEY : + { + bool scan_via_buddy = rtw_cfg80211_scan_via_buddy(padapter, request); + + if (scan_via_buddy == _FALSE) + need_indicate_scan_done = _TRUE; + + goto check_need_indicate_scan_done; + } + #endif + + default : + RTW_ERR("site survey check code (%d) unknown\n", ssc_chk); + need_indicate_scan_done = _TRUE; + goto check_need_indicate_scan_done; + } + + rtw_ps_deny(padapter, PS_DENY_SCAN); + ps_denied = _TRUE; + if (_FAIL == rtw_pwr_wakeup(padapter)) { + need_indicate_scan_done = _TRUE; + goto check_need_indicate_scan_done; + } + +#else + + +#ifdef CONFIG_MP_INCLUDED + if (rtw_mp_mode_check(padapter)) { + RTW_INFO("MP mode block Scan request\n"); + ret = -EPERM; + goto exit; + } +#endif + +#ifdef CONFIG_P2P + if (pwdinfo->driver_interface == DRIVER_CFG80211) { + if (request->n_ssids && ssids + && _rtw_memcmp(ssids[0].ssid, "DIRECT-", 7) + && rtw_get_p2p_ie((u8 *)request->ie, request->ie_len, NULL, NULL) + ) { + if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) + rtw_p2p_enable(padapter, P2P_ROLE_DEVICE); + else { + rtw_p2p_set_pre_state(pwdinfo, rtw_p2p_state(pwdinfo)); + #ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("%s, role=%d, p2p_state=%d\n", __func__, rtw_p2p_role(pwdinfo), rtw_p2p_state(pwdinfo)); + #endif + } + rtw_p2p_set_state(pwdinfo, P2P_STATE_LISTEN); + + if (request->n_channels == 3 && + request->channels[0]->hw_value == 1 && + request->channels[1]->hw_value == 6 && + request->channels[2]->hw_value == 11 + ) + social_channel = 1; + } + } +#endif /*CONFIG_P2P*/ + + if (request->ie && request->ie_len > 0) + rtw_cfg80211_set_probe_req_wpsp2pie(padapter, (u8 *)request->ie, request->ie_len); + +#ifdef CONFIG_RTW_REPEATER_SON + if (padapter->rtw_rson_scanstage == RSON_SCAN_PROCESS) { + RTW_INFO(FUNC_ADPT_FMT" blocking scan for under rson scanning process\n", FUNC_ADPT_ARG(padapter)); + need_indicate_scan_done = _TRUE; + goto check_need_indicate_scan_done; + } +#endif + + if (adapter_wdev_data(padapter)->block_scan == _TRUE) { + RTW_INFO(FUNC_ADPT_FMT" wdev_priv.block_scan is set\n", FUNC_ADPT_ARG(padapter)); + need_indicate_scan_done = _TRUE; + goto check_need_indicate_scan_done; + } + + rtw_ps_deny(padapter, PS_DENY_SCAN); + ps_denied = _TRUE; + if (_FAIL == rtw_pwr_wakeup(padapter)) { + need_indicate_scan_done = _TRUE; + goto check_need_indicate_scan_done; + } + + if (rtw_is_scan_deny(padapter)) { + RTW_INFO(FUNC_ADPT_FMT ": scan deny\n", FUNC_ADPT_ARG(padapter)); +#ifdef CONFIG_NOTIFY_SCAN_ABORT_WITH_BUSY + ret = -EBUSY; + goto exit; +#else + need_indicate_scan_done = _TRUE; + goto check_need_indicate_scan_done; +#endif + } + + /* check fw state*/ + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == _TRUE) { + +#ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO(FUNC_ADPT_FMT" under WIFI_AP_STATE\n", FUNC_ADPT_ARG(padapter)); +#endif + + if (check_fwstate(pmlmepriv, WIFI_UNDER_WPS | WIFI_UNDER_SURVEY | WIFI_UNDER_LINKING) == _TRUE) { + RTW_INFO("%s, fwstate=0x%x\n", __func__, pmlmepriv->fw_state); + + if (check_fwstate(pmlmepriv, WIFI_UNDER_WPS)) + RTW_INFO("AP mode process WPS\n"); + + need_indicate_scan_done = _TRUE; + goto check_need_indicate_scan_done; + } + } + + if (check_fwstate(pmlmepriv, WIFI_UNDER_SURVEY) == _TRUE) { + RTW_INFO("%s, fwstate=0x%x\n", __func__, pmlmepriv->fw_state); + need_indicate_scan_done = _TRUE; + goto check_need_indicate_scan_done; + } else if (check_fwstate(pmlmepriv, WIFI_UNDER_LINKING) == _TRUE) { + RTW_INFO("%s, fwstate=0x%x\n", __func__, pmlmepriv->fw_state); + ret = -EBUSY; + goto check_need_indicate_scan_done; + } + +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_buddy_check_fwstate(padapter, WIFI_UNDER_LINKING | WIFI_UNDER_WPS)) { + RTW_INFO("%s exit due to buddy_intf's mlme state under linking or wps\n", __func__); + need_indicate_scan_done = _TRUE; + goto check_need_indicate_scan_done; + + } else if (rtw_mi_buddy_check_fwstate(padapter, WIFI_UNDER_SURVEY)) { + bool scan_via_buddy = rtw_cfg80211_scan_via_buddy(padapter, request); + + if (scan_via_buddy == _FALSE) + need_indicate_scan_done = _TRUE; + + goto check_need_indicate_scan_done; + } +#endif /* CONFIG_CONCURRENT_MODE */ + +#ifdef RTW_BUSY_DENY_SCAN + /* + * busy traffic check + * Rules: + * 1. If (scan interval <= BUSY_TRAFFIC_SCAN_DENY_PERIOD) always allow + * scan, otherwise goto rule 2. + * 2. Deny scan if any interface is busy, otherwise allow scan. + */ + if (pmlmepriv->lastscantime + && (rtw_get_passing_time_ms(pmlmepriv->lastscantime) > + registry_par->scan_interval_thr) + && rtw_mi_busy_traffic_check(padapter)) { + RTW_WARN(FUNC_ADPT_FMT ": scan abort!! BusyTraffic\n", + FUNC_ADPT_ARG(padapter)); + need_indicate_scan_done = _TRUE; + goto check_need_indicate_scan_done; + } +#endif /* RTW_BUSY_DENY_SCAN */ +#endif + +#ifdef CONFIG_P2P + if (!rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE) && !rtw_p2p_chk_state(pwdinfo, P2P_STATE_IDLE)) { + rtw_p2p_set_state(pwdinfo, P2P_STATE_FIND_PHASE_SEARCH); + + if (social_channel == 0) + rtw_p2p_findphase_ex_set(pwdinfo, P2P_FINDPHASE_EX_NONE); + else + rtw_p2p_findphase_ex_set(pwdinfo, P2P_FINDPHASE_EX_SOCIAL_LAST); + } +#endif /* CONFIG_P2P */ + + rtw_init_sitesurvey_parm(padapter, &parm); + + /* parsing request ssids, n_ssids */ + for (i = 0; i < request->n_ssids && ssids && i < RTW_SSID_SCAN_AMOUNT; i++) { + #ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("ssid=%s, len=%d\n", ssids[i].ssid, ssids[i].ssid_len); + #endif + _rtw_memcpy(&parm.ssid[i].Ssid, ssids[i].ssid, ssids[i].ssid_len); + parm.ssid[i].SsidLength = ssids[i].ssid_len; + } + parm.ssid_num = i; + + /* no ssid entry, set the scan type as passvie */ + if (request->n_ssids == 0) + parm.scan_mode = SCAN_PASSIVE; + + /* parsing channels, n_channels */ + for (i = 0; i < request->n_channels && i < RTW_CHANNEL_SCAN_AMOUNT; i++) { + #ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO(FUNC_ADPT_FMT CHAN_FMT"\n", FUNC_ADPT_ARG(padapter), CHAN_ARG(request->channels[i])); + #endif + parm.ch[i].hw_value = request->channels[i]->hw_value; + parm.ch[i].flags = request->channels[i]->flags; + } + parm.ch_num = i; + + if (request->n_channels == 1) { + for (i = 1; i < survey_times_for_one_ch; i++) + _rtw_memcpy(&parm.ch[i], &parm.ch[0], sizeof(struct rtw_ieee80211_channel)); + parm.ch_num = survey_times_for_one_ch; + } else if (request->n_channels <= 4) { + for (j = request->n_channels - 1; j >= 0; j--) + for (i = 0; i < survey_times; i++) + _rtw_memcpy(&parm.ch[j * survey_times + i], &parm.ch[j], sizeof(struct rtw_ieee80211_channel)); + parm.ch_num = survey_times * request->n_channels; + } + + _enter_critical_bh(&pwdev_priv->scan_req_lock, &irqL); + _enter_critical_bh(&pmlmepriv->lock, &irqL); + _status = rtw_sitesurvey_cmd(padapter, &parm); + if (_status == _SUCCESS) + pwdev_priv->scan_request = request; + else + ret = -1; + _exit_critical_bh(&pmlmepriv->lock, &irqL); + _exit_critical_bh(&pwdev_priv->scan_req_lock, &irqL); + +check_need_indicate_scan_done: + if (_TRUE == need_indicate_scan_done) { +#if (KERNEL_VERSION(4, 8, 0) <= LINUX_VERSION_CODE) + struct cfg80211_scan_info info; + + memset(&info, 0, sizeof(info)); + info.aborted = 0; +#endif + /* the process time of scan results must be over at least 1ms in the newly Android */ + rtw_msleep_os(1); + + _rtw_cfg80211_surveydone_event_callback(padapter, request); +#if (KERNEL_VERSION(4, 8, 0) <= LINUX_VERSION_CODE) + cfg80211_scan_done(request, &info); +#else + cfg80211_scan_done(request, 0); +#endif + } + + if (ps_denied == _TRUE) + rtw_ps_deny_cancel(padapter, PS_DENY_SCAN); + +exit: +#ifdef RTW_BUSY_DENY_SCAN + if (pmlmepriv) + pmlmepriv->lastscantime = rtw_get_current_time(); +#endif + + return ret; +} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)) && \ + defined(CONFIG_RTW_ABORT_SCAN) +static void cfg80211_rtw_abort_scan(struct wiphy *wiphy, + struct wireless_dev *wdev) +{ + _adapter *padapter = wiphy_to_adapter(wiphy); + + RTW_INFO("=>"FUNC_ADPT_FMT" - Abort Scan\n", FUNC_ADPT_ARG(padapter)); + if (wdev->iftype != NL80211_IFTYPE_STATION) { + RTW_ERR("abort scan ignored, iftype(%d)\n", wdev->iftype); + return; + } + rtw_scan_abort(padapter); +} +#endif + +static int cfg80211_rtw_set_wiphy_params(struct wiphy *wiphy, u32 changed) +{ +#if 0 + struct iwm_priv *iwm = wiphy_to_iwm(wiphy); + + if (changed & WIPHY_PARAM_RTS_THRESHOLD && + (iwm->conf.rts_threshold != wiphy->rts_threshold)) { + int ret; + + iwm->conf.rts_threshold = wiphy->rts_threshold; + + ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX, + CFG_RTS_THRESHOLD, + iwm->conf.rts_threshold); + if (ret < 0) + return ret; + } + + if (changed & WIPHY_PARAM_FRAG_THRESHOLD && + (iwm->conf.frag_threshold != wiphy->frag_threshold)) { + int ret; + + iwm->conf.frag_threshold = wiphy->frag_threshold; + + ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_FA_CFG_FIX, + CFG_FRAG_THRESHOLD, + iwm->conf.frag_threshold); + if (ret < 0) + return ret; + } +#endif + RTW_INFO("%s\n", __func__); + return 0; +} + + + +static int rtw_cfg80211_set_wpa_version(struct security_priv *psecuritypriv, u32 wpa_version) +{ + RTW_INFO("%s, wpa_version=%d\n", __func__, wpa_version); + + if (!wpa_version) { + psecuritypriv->ndisauthtype = Ndis802_11AuthModeOpen; + return 0; + } + + + if (wpa_version & (NL80211_WPA_VERSION_1 | NL80211_WPA_VERSION_2)) + psecuritypriv->ndisauthtype = Ndis802_11AuthModeWPAPSK; + +#if 0 + if (wpa_version & NL80211_WPA_VERSION_2) + psecuritypriv->ndisauthtype = Ndis802_11AuthModeWPA2PSK; +#endif + + #ifdef CONFIG_WAPI_SUPPORT + if (wpa_version & NL80211_WAPI_VERSION_1) + psecuritypriv->ndisauthtype = Ndis802_11AuthModeWAPI; + #endif + + return 0; + +} + +static int rtw_cfg80211_set_auth_type(struct security_priv *psecuritypriv, + enum nl80211_auth_type sme_auth_type) +{ + RTW_INFO("%s, nl80211_auth_type=%d\n", __func__, sme_auth_type); + + if (NL80211_AUTHTYPE_MAX <= (int)MLME_AUTHTYPE_SAE) { + if (MLME_AUTHTYPE_SAE == psecuritypriv->auth_type) { + /* This case pre handle in + * rtw_check_connect_sae_compat() + */ + psecuritypriv->auth_alg = WLAN_AUTH_SAE; + return 0; + } + } else if (sme_auth_type == (int)MLME_AUTHTYPE_SAE) { + psecuritypriv->auth_type = MLME_AUTHTYPE_SAE; + psecuritypriv->auth_alg = WLAN_AUTH_SAE; + return 0; + } + + psecuritypriv->auth_type = sme_auth_type; + + switch (sme_auth_type) { + case NL80211_AUTHTYPE_AUTOMATIC: + + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Auto; + + break; + case NL80211_AUTHTYPE_OPEN_SYSTEM: + + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open; + + if (psecuritypriv->ndisauthtype > Ndis802_11AuthModeWPA) + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X; + +#ifdef CONFIG_WAPI_SUPPORT + if (psecuritypriv->ndisauthtype == Ndis802_11AuthModeWAPI) + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_WAPI; +#endif + + break; + case NL80211_AUTHTYPE_SHARED_KEY: + + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Shared; + + psecuritypriv->ndisencryptstatus = Ndis802_11Encryption1Enabled; + + + break; + default: + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open; + /* return -ENOTSUPP; */ + } + + return 0; + +} + +static int rtw_cfg80211_set_cipher(struct security_priv *psecuritypriv, u32 cipher, bool ucast) +{ + u32 ndisencryptstatus = Ndis802_11EncryptionDisabled; + + u32 *profile_cipher = ucast ? &psecuritypriv->dot11PrivacyAlgrthm : + &psecuritypriv->dot118021XGrpPrivacy; + + RTW_INFO("%s, ucast=%d, cipher=0x%x\n", __func__, ucast, cipher); + + + if (!cipher) { + *profile_cipher = _NO_PRIVACY_; + psecuritypriv->ndisencryptstatus = ndisencryptstatus; + return 0; + } + + switch (cipher) { + case IW_AUTH_CIPHER_NONE: + *profile_cipher = _NO_PRIVACY_; + ndisencryptstatus = Ndis802_11EncryptionDisabled; +#ifdef CONFIG_WAPI_SUPPORT + if (psecuritypriv->dot11PrivacyAlgrthm == _SMS4_) + *profile_cipher = _SMS4_; +#endif + break; + case WLAN_CIPHER_SUITE_WEP40: + *profile_cipher = _WEP40_; + ndisencryptstatus = Ndis802_11Encryption1Enabled; + break; + case WLAN_CIPHER_SUITE_WEP104: + *profile_cipher = _WEP104_; + ndisencryptstatus = Ndis802_11Encryption1Enabled; + break; + case WLAN_CIPHER_SUITE_TKIP: + *profile_cipher = _TKIP_; + ndisencryptstatus = Ndis802_11Encryption2Enabled; + break; + case WLAN_CIPHER_SUITE_CCMP: + *profile_cipher = _AES_; + ndisencryptstatus = Ndis802_11Encryption3Enabled; + break; + case WIFI_CIPHER_SUITE_GCMP: + *profile_cipher = _GCMP_; + ndisencryptstatus = Ndis802_11Encryption3Enabled; + break; + case WIFI_CIPHER_SUITE_GCMP_256: + *profile_cipher = _GCMP_256_; + ndisencryptstatus = Ndis802_11Encryption3Enabled; + break; + case WIFI_CIPHER_SUITE_CCMP_256: + *profile_cipher = _CCMP_256_; + ndisencryptstatus = Ndis802_11Encryption3Enabled; + break; +#ifdef CONFIG_WAPI_SUPPORT + case WLAN_CIPHER_SUITE_SMS4: + *profile_cipher = _SMS4_; + ndisencryptstatus = Ndis802_11_EncrypteionWAPI; + break; +#endif + default: + RTW_INFO("Unsupported cipher: 0x%x\n", cipher); + return -ENOTSUPP; + } + + if (ucast) { + psecuritypriv->ndisencryptstatus = ndisencryptstatus; + + /* if(psecuritypriv->dot11PrivacyAlgrthm >= _AES_) */ + /* psecuritypriv->ndisauthtype = Ndis802_11AuthModeWPA2PSK; */ + } + + return 0; +} + +static int rtw_cfg80211_set_key_mgt(struct security_priv *psecuritypriv, u32 key_mgt) +{ + RTW_INFO("%s, key_mgt=0x%x\n", __func__, key_mgt); + + if (key_mgt == WLAN_AKM_SUITE_8021X) { + /* *auth_type = UMAC_AUTH_TYPE_8021X; */ + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X; + psecuritypriv->rsn_akm_suite_type = 1; + } else if (key_mgt == WLAN_AKM_SUITE_PSK) { + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X; + psecuritypriv->rsn_akm_suite_type = 2; + } +#ifdef CONFIG_WAPI_SUPPORT + else if (key_mgt == WLAN_AKM_SUITE_WAPI_PSK) + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_WAPI; + else if (key_mgt == WLAN_AKM_SUITE_WAPI_CERT) + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_WAPI; +#endif +#ifdef CONFIG_RTW_80211R + else if (key_mgt == WLAN_AKM_SUITE_FT_8021X) { + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X; + psecuritypriv->rsn_akm_suite_type = 3; + } else if (key_mgt == WLAN_AKM_SUITE_FT_PSK) { + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X; + psecuritypriv->rsn_akm_suite_type = 4; + } +#endif + else if (key_mgt == WLAN_AKM_SUITE_SAE) { + psecuritypriv->rsn_akm_suite_type = 8; + } else { + RTW_INFO("Invalid key mgt: 0x%x\n", key_mgt); + /* return -EINVAL; */ + } + + return 0; +} + +static int rtw_cfg80211_set_wpa_ie(_adapter *padapter, u8 *pie, size_t ielen) +{ + u8 *buf = NULL, *pos = NULL; + int group_cipher = 0, pairwise_cipher = 0; + u8 mfp_opt = MFP_NO; + int ret = 0; + int wpa_ielen = 0; + int wpa2_ielen = 0; + u8 *pwpa, *pwpa2; + u8 null_addr[] = {0, 0, 0, 0, 0, 0}; + + if (pie == NULL || !ielen) { + /* Treat this as normal case, but need to clear WIFI_UNDER_WPS */ + _clr_fwstate_(&padapter->mlmepriv, WIFI_UNDER_WPS); + goto exit; + } + + if (ielen > MAX_WPA_IE_LEN + MAX_WPS_IE_LEN + MAX_P2P_IE_LEN) { + ret = -EINVAL; + goto exit; + } + + buf = rtw_zmalloc(ielen); + if (buf == NULL) { + ret = -ENOMEM; + goto exit; + } + + _rtw_memcpy(buf, pie , ielen); + + RTW_INFO("set wpa_ie(length:%zu):\n", ielen); + RTW_INFO_DUMP(NULL, buf, ielen); + + pos = buf; + if (ielen < RSN_HEADER_LEN) { + ret = -1; + goto exit; + } + + pwpa = rtw_get_wpa_ie(buf, &wpa_ielen, ielen); + if (pwpa && wpa_ielen > 0) { + if (rtw_parse_wpa_ie(pwpa, wpa_ielen + 2, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) { + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_8021X; + padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeWPAPSK; + _rtw_memcpy(padapter->securitypriv.supplicant_ie, &pwpa[0], wpa_ielen + 2); + + RTW_INFO("got wpa_ie, wpa_ielen:%u\n", wpa_ielen); + } + } + + pwpa2 = rtw_get_wpa2_ie(buf, &wpa2_ielen, ielen); + if (pwpa2 && wpa2_ielen > 0) { + if (rtw_parse_wpa2_ie(pwpa2, wpa2_ielen + 2, &group_cipher, &pairwise_cipher, NULL, NULL, &mfp_opt, NULL) == _SUCCESS) { + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_8021X; + padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeWPA2PSK; + _rtw_memcpy(padapter->securitypriv.supplicant_ie, &pwpa2[0], wpa2_ielen + 2); + + RTW_INFO("got wpa2_ie, wpa2_ielen:%u\n", wpa2_ielen); + } + } + + if (group_cipher == 0) + group_cipher = WPA_CIPHER_NONE; + if (pairwise_cipher == 0) + pairwise_cipher = WPA_CIPHER_NONE; + + switch (group_cipher) { + case WPA_CIPHER_NONE: + padapter->securitypriv.dot118021XGrpPrivacy = _NO_PRIVACY_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11EncryptionDisabled; + break; + case WPA_CIPHER_WEP40: + padapter->securitypriv.dot118021XGrpPrivacy = _WEP40_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled; + break; + case WPA_CIPHER_TKIP: + padapter->securitypriv.dot118021XGrpPrivacy = _TKIP_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption2Enabled; + break; + case WPA_CIPHER_CCMP: + padapter->securitypriv.dot118021XGrpPrivacy = _AES_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled; + break; + case WPA_CIPHER_GCMP: + padapter->securitypriv.dot118021XGrpPrivacy = _GCMP_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled; + break; + case WPA_CIPHER_GCMP_256: + padapter->securitypriv.dot118021XGrpPrivacy = _GCMP_256_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled; + break; + case WPA_CIPHER_CCMP_256: + padapter->securitypriv.dot118021XGrpPrivacy = _CCMP_256_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled; + break; + case WPA_CIPHER_WEP104: + padapter->securitypriv.dot118021XGrpPrivacy = _WEP104_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled; + break; + } + + switch (pairwise_cipher) { + case WPA_CIPHER_NONE: + padapter->securitypriv.dot11PrivacyAlgrthm = _NO_PRIVACY_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11EncryptionDisabled; + break; + case WPA_CIPHER_WEP40: + padapter->securitypriv.dot11PrivacyAlgrthm = _WEP40_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled; + break; + case WPA_CIPHER_TKIP: + padapter->securitypriv.dot11PrivacyAlgrthm = _TKIP_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption2Enabled; + break; + case WPA_CIPHER_CCMP: + padapter->securitypriv.dot11PrivacyAlgrthm = _AES_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled; + break; + case WPA_CIPHER_GCMP: + padapter->securitypriv.dot11PrivacyAlgrthm = _GCMP_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled; + break; + case WPA_CIPHER_GCMP_256: + padapter->securitypriv.dot11PrivacyAlgrthm = _GCMP_256_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled; + break; + case WPA_CIPHER_CCMP_256: + padapter->securitypriv.dot11PrivacyAlgrthm = _CCMP_256_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled; + break; + case WPA_CIPHER_WEP104: + padapter->securitypriv.dot11PrivacyAlgrthm = _WEP104_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled; + break; + } + + if (mfp_opt == MFP_INVALID) { + RTW_INFO(FUNC_ADPT_FMT" invalid MFP setting\n", FUNC_ADPT_ARG(padapter)); + ret = -EINVAL; + goto exit; + } + padapter->securitypriv.mfp_opt = mfp_opt; + + {/* handle wps_ie */ + uint wps_ielen; + u8 *wps_ie; + + wps_ie = rtw_get_wps_ie(buf, ielen, NULL, &wps_ielen); + if (wps_ie && wps_ielen > 0) { + RTW_INFO("got wps_ie, wps_ielen:%u\n", wps_ielen); + padapter->securitypriv.wps_ie_len = wps_ielen < MAX_WPS_IE_LEN ? wps_ielen : MAX_WPS_IE_LEN; + _rtw_memcpy(padapter->securitypriv.wps_ie, wps_ie, padapter->securitypriv.wps_ie_len); + set_fwstate(&padapter->mlmepriv, WIFI_UNDER_WPS); + } else + _clr_fwstate_(&padapter->mlmepriv, WIFI_UNDER_WPS); + } + + {/* handle owe_ie */ + uint owe_ielen; + u8 *owe_ie; + + owe_ie = rtw_get_owe_ie(buf, ielen, NULL, &owe_ielen); + if (owe_ie && owe_ielen > 0) { + RTW_INFO("got owe_ie, owe_ielen:%u\n", owe_ielen); + padapter->securitypriv.owe_ie_len = owe_ielen < MAX_OWE_IE_LEN ? owe_ielen : MAX_OWE_IE_LEN; + _rtw_memcpy(padapter->securitypriv.owe_ie, owe_ie, padapter->securitypriv.owe_ie_len); + } + } + + #ifdef CONFIG_P2P + {/* check p2p_ie for assoc req; */ + uint p2p_ielen = 0; + u8 *p2p_ie; + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + + p2p_ie = rtw_get_p2p_ie(buf, ielen, NULL, &p2p_ielen); + if (p2p_ie) { + #ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("%s p2p_assoc_req_ielen=%d\n", __FUNCTION__, p2p_ielen); + #endif + + if (pmlmepriv->p2p_assoc_req_ie) { + u32 free_len = pmlmepriv->p2p_assoc_req_ie_len; + pmlmepriv->p2p_assoc_req_ie_len = 0; + rtw_mfree(pmlmepriv->p2p_assoc_req_ie, free_len); + pmlmepriv->p2p_assoc_req_ie = NULL; + } + + pmlmepriv->p2p_assoc_req_ie = rtw_malloc(p2p_ielen); + if (pmlmepriv->p2p_assoc_req_ie == NULL) { + RTW_INFO("%s()-%d: rtw_malloc() ERROR!\n", __FUNCTION__, __LINE__); + goto exit; + } + _rtw_memcpy(pmlmepriv->p2p_assoc_req_ie, p2p_ie, p2p_ielen); + pmlmepriv->p2p_assoc_req_ie_len = p2p_ielen; + } + } + #endif /* CONFIG_P2P */ + + #ifdef CONFIG_WFD + { + uint wfd_ielen = 0; + u8 *wfd_ie; + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + + wfd_ie = rtw_get_wfd_ie(buf, ielen, NULL, &wfd_ielen); + if (wfd_ie) { + #ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("%s wfd_assoc_req_ielen=%d\n", __FUNCTION__, wfd_ielen); + #endif + + if (rtw_mlme_update_wfd_ie_data(pmlmepriv, MLME_ASSOC_REQ_IE, wfd_ie, wfd_ielen) != _SUCCESS) + goto exit; + } + } + #endif /* CONFIG_WFD */ + + #ifdef CONFIG_RTW_MULTI_AP + padapter->multi_ap = rtw_get_multi_ap_ie_ext(buf, ielen) & MULTI_AP_BACKHAUL_STA; + if (padapter->multi_ap) + adapter_set_use_wds(padapter, 1); + #endif + + /* TKIP and AES disallow multicast packets until installing group key */ + if (padapter->securitypriv.dot11PrivacyAlgrthm == _TKIP_ + || padapter->securitypriv.dot11PrivacyAlgrthm == _TKIP_WTMIC_ + || padapter->securitypriv.dot11PrivacyAlgrthm == _AES_ + || padapter->securitypriv.dot11PrivacyAlgrthm == _GCMP_ + || padapter->securitypriv.dot11PrivacyAlgrthm == _GCMP_256_ + || padapter->securitypriv.dot11PrivacyAlgrthm == _CCMP_256_) + /* WPS open need to enable multicast */ + /* || check_fwstate(&padapter->mlmepriv, WIFI_UNDER_WPS) == _TRUE) */ + rtw_hal_set_hwreg(padapter, HW_VAR_OFF_RCR_AM, null_addr); + + +exit: + if (buf) + rtw_mfree(buf, ielen); + if (ret) + _clr_fwstate_(&padapter->mlmepriv, WIFI_UNDER_WPS); + + return ret; +} + +static int cfg80211_rtw_join_ibss(struct wiphy *wiphy, struct net_device *ndev, + struct cfg80211_ibss_params *params) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + NDIS_802_11_SSID ndis_ssid; + struct security_priv *psecuritypriv = &padapter->securitypriv; + struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv; +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + struct cfg80211_chan_def *pch_def; +#endif + struct ieee80211_channel *pch; + int ret = 0; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + pch_def = (struct cfg80211_chan_def *)(¶ms->chandef); + pch = (struct ieee80211_channel *) pch_def->chan; +#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)) + pch = (struct ieee80211_channel *)(params->channel); +#endif + + if (!params->ssid || !params->ssid_len) { + ret = -EINVAL; + goto exit; + } + + if (params->ssid_len > IW_ESSID_MAX_SIZE) { + ret = -E2BIG; + goto exit; + } + + rtw_ps_deny(padapter, PS_DENY_JOIN); + if (_FAIL == rtw_pwr_wakeup(padapter)) { + ret = -EPERM; + goto cancel_ps_deny; + } + +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_buddy_check_fwstate(padapter, WIFI_UNDER_LINKING)) { + RTW_INFO("%s, but buddy_intf is under linking\n", __FUNCTION__); + ret = -EINVAL; + goto cancel_ps_deny; + } + rtw_mi_buddy_scan_abort(padapter, _TRUE); /* OR rtw_mi_scan_abort(padapter, _TRUE);*/ +#endif /*CONFIG_CONCURRENT_MODE*/ + + + _rtw_memset(&ndis_ssid, 0, sizeof(NDIS_802_11_SSID)); + ndis_ssid.SsidLength = params->ssid_len; + _rtw_memcpy(ndis_ssid.Ssid, (u8 *)params->ssid, params->ssid_len); + + /* RTW_INFO("ssid=%s, len=%zu\n", ndis_ssid.Ssid, params->ssid_len); */ + + psecuritypriv->ndisencryptstatus = Ndis802_11EncryptionDisabled; + psecuritypriv->dot11PrivacyAlgrthm = _NO_PRIVACY_; + psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_; + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open; /* open system */ + psecuritypriv->ndisauthtype = Ndis802_11AuthModeOpen; + + ret = rtw_cfg80211_set_auth_type(psecuritypriv, NL80211_AUTHTYPE_OPEN_SYSTEM); + rtw_set_802_11_authentication_mode(padapter, psecuritypriv->ndisauthtype); + + RTW_INFO("%s: center_freq = %d\n", __func__, pch->center_freq); + pmlmeext->cur_channel = rtw_freq2ch(pch->center_freq); + + if (rtw_set_802_11_ssid(padapter, &ndis_ssid) == _FALSE) { + ret = -1; + goto cancel_ps_deny; + } + +cancel_ps_deny: + rtw_ps_deny_cancel(padapter, PS_DENY_JOIN); +exit: + return ret; +} + +static int cfg80211_rtw_leave_ibss(struct wiphy *wiphy, struct net_device *ndev) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + struct wireless_dev *rtw_wdev = padapter->rtw_wdev; + enum nl80211_iftype old_type; + int ret = 0; + + RTW_INFO(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(ndev)); + +#if (RTW_CFG80211_BLOCK_STA_DISCON_EVENT & RTW_CFG80211_BLOCK_DISCON_WHEN_DISCONNECT) + rtw_wdev_set_not_indic_disco(adapter_wdev_data(padapter), 1); +#endif + + old_type = rtw_wdev->iftype; + + rtw_set_to_roam(padapter, 0); + + if (check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE)) { + rtw_scan_abort(padapter); + LeaveAllPowerSaveMode(padapter); + + rtw_wdev->iftype = NL80211_IFTYPE_STATION; + + if (rtw_set_802_11_infrastructure_mode(padapter, Ndis802_11Infrastructure, 0) == _FALSE) { + rtw_wdev->iftype = old_type; + ret = -EPERM; + goto leave_ibss; + } + rtw_setopmode_cmd(padapter, Ndis802_11Infrastructure, RTW_CMDF_WAIT_ACK); + } + +leave_ibss: +#if (RTW_CFG80211_BLOCK_STA_DISCON_EVENT & RTW_CFG80211_BLOCK_DISCON_WHEN_DISCONNECT) + rtw_wdev_set_not_indic_disco(adapter_wdev_data(padapter), 0); +#endif + + return 0; +} + +bool rtw_cfg80211_is_connect_requested(_adapter *adapter) +{ + struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(adapter); + _irqL irqL; + bool requested; + + _enter_critical_bh(&pwdev_priv->connect_req_lock, &irqL); + requested = pwdev_priv->connect_req ? 1 : 0; + _exit_critical_bh(&pwdev_priv->connect_req_lock, &irqL); + + return requested; +} + +static int _rtw_disconnect(struct wiphy *wiphy, struct net_device *ndev) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + + + /* if(check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE)) */ + { + rtw_scan_abort(padapter); + rtw_join_abort_timeout(padapter, 300); + LeaveAllPowerSaveMode(padapter); + rtw_disassoc_cmd(padapter, 500, RTW_CMDF_WAIT_ACK); +#ifdef CONFIG_RTW_REPEATER_SON + rtw_rson_do_disconnect(padapter); +#endif + RTW_INFO("%s...call rtw_indicate_disconnect\n", __func__); + + rtw_free_assoc_resources_cmd(padapter, _TRUE, RTW_CMDF_WAIT_ACK); + + /* indicate locally_generated = 0 when suspend */ + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)) + rtw_indicate_disconnect(padapter, 0, wiphy->dev.power.is_prepared ? _FALSE : _TRUE); + #else + /* + * for kernel < 4.2, DISCONNECT event is hardcoded with + * NL80211_ATTR_DISCONNECTED_BY_AP=1 in NL80211 layer + * no need to judge if under suspend + */ + rtw_indicate_disconnect(padapter, 0, _TRUE); + #endif + + rtw_pwr_wakeup(padapter); + } + return 0; +} + +#if (KERNEL_VERSION(4, 17, 0) > LINUX_VERSION_CODE) \ + && !defined(CONFIG_KERNEL_PATCH_EXTERNAL_AUTH) +static bool rtw_check_connect_sae_compat(struct cfg80211_connect_params *sme) +{ + struct rtw_ieee802_11_elems elems; + struct rsne_info info; + u8 AKM_SUITE_SAE[] = { 0x00, 0x0f, 0xac, 8 }; + int i; + + if (sme->auth_type != (int)MLME_AUTHTYPE_SHARED_KEY) + return false; + + if (rtw_ieee802_11_parse_elems((u8 *)sme->ie, sme->ie_len, &elems, 0) + == ParseFailed) + return false; + + if (!elems.rsn_ie) + return false; + + if (rtw_rsne_info_parse(elems.rsn_ie - 2, elems.rsn_ie_len + 2, &info) == _FAIL) + return false; + + for (i = 0; i < info.akm_cnt; i++) + if (memcmp(info.akm_list + i * RSN_SELECTOR_LEN, + AKM_SUITE_SAE, RSN_SELECTOR_LEN) == 0) + return true; + + return false; +} +#else +#define rtw_check_connect_sae_compat(sme) false +#endif + +static int cfg80211_rtw_connect(struct wiphy *wiphy, struct net_device *ndev, + struct cfg80211_connect_params *sme) +{ + int ret = 0; + NDIS_802_11_AUTHENTICATION_MODE authmode; + NDIS_802_11_SSID ndis_ssid; + /* u8 matched_by_bssid=_FALSE; */ + /* u8 matched_by_ssid=_FALSE; */ + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + struct security_priv *psecuritypriv = &padapter->securitypriv; + struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(padapter); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + _irqL irqL; + +#if (RTW_CFG80211_BLOCK_STA_DISCON_EVENT & RTW_CFG80211_BLOCK_DISCON_WHEN_CONNECT) + rtw_wdev_set_not_indic_disco(pwdev_priv, 1); +#endif + + RTW_INFO("=>"FUNC_NDEV_FMT" - Start to Connection\n", FUNC_NDEV_ARG(ndev)); + RTW_INFO("privacy=%d, key=%p, key_len=%d, key_idx=%d, auth_type=%d\n", + sme->privacy, sme->key, sme->key_len, sme->key_idx, sme->auth_type); + + if (rtw_check_connect_sae_compat(sme)) { + sme->auth_type = (int)MLME_AUTHTYPE_SAE; + psecuritypriv->auth_type = MLME_AUTHTYPE_SAE; + psecuritypriv->auth_alg = WLAN_AUTH_SAE; + RTW_INFO("%s set sme->auth_type for SAE compat\n", __FUNCTION__); + } + + if (pwdev_priv->block == _TRUE) { + ret = -EBUSY; + RTW_INFO("%s wdev_priv.block is set\n", __FUNCTION__); + goto exit; + } + + if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE | WIFI_UNDER_LINKING) == _TRUE) { + + _rtw_disconnect(wiphy, ndev); + RTW_INFO("%s disconnect before connecting! fw_state=0x%x\n", + __FUNCTION__, pmlmepriv->fw_state); + } + +#ifdef CONFIG_PLATFORM_MSTAR_SCAN_BEFORE_CONNECT + printk("MStar Android!\n"); + if (pwdev_priv->bandroid_scan == _FALSE) { +#ifdef CONFIG_P2P + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) +#endif /* CONFIG_P2P */ + { + ret = -EBUSY; + printk("Android hasn't attached yet!\n"); + goto exit; + } + } +#endif + + if (!sme->ssid || !sme->ssid_len) { + ret = -EINVAL; + goto exit; + } + + if (sme->ssid_len > IW_ESSID_MAX_SIZE) { + ret = -E2BIG; + goto exit; + } + + rtw_ps_deny(padapter, PS_DENY_JOIN); + if (_FAIL == rtw_pwr_wakeup(padapter)) { + ret = -EPERM; + goto cancel_ps_deny; + } + + rtw_mi_scan_abort(padapter, _TRUE); + + rtw_join_abort_timeout(padapter, 300); +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_buddy_check_fwstate(padapter, WIFI_UNDER_LINKING)) { + ret = -EINVAL; + goto cancel_ps_deny; + } +#endif + + _rtw_memset(&ndis_ssid, 0, sizeof(NDIS_802_11_SSID)); + ndis_ssid.SsidLength = sme->ssid_len; + _rtw_memcpy(ndis_ssid.Ssid, (u8 *)sme->ssid, sme->ssid_len); + + RTW_INFO("ssid=%s, len=%zu\n", ndis_ssid.Ssid, sme->ssid_len); + + + if (sme->bssid) + RTW_INFO("bssid="MAC_FMT"\n", MAC_ARG(sme->bssid)); + + + psecuritypriv->ndisencryptstatus = Ndis802_11EncryptionDisabled; + psecuritypriv->dot11PrivacyAlgrthm = _NO_PRIVACY_; + psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_; + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open; /* open system */ + psecuritypriv->ndisauthtype = Ndis802_11AuthModeOpen; + psecuritypriv->auth_alg = WLAN_AUTH_OPEN; + psecuritypriv->extauth_status = WLAN_STATUS_UNSPECIFIED_FAILURE; + +#ifdef CONFIG_WAPI_SUPPORT + padapter->wapiInfo.bWapiEnable = false; +#endif + + ret = rtw_cfg80211_set_wpa_version(psecuritypriv, sme->crypto.wpa_versions); + if (ret < 0) + goto cancel_ps_deny; + +#ifdef CONFIG_WAPI_SUPPORT + if (sme->crypto.wpa_versions & NL80211_WAPI_VERSION_1) { + padapter->wapiInfo.bWapiEnable = true; + padapter->wapiInfo.extra_prefix_len = WAPI_EXT_LEN; + padapter->wapiInfo.extra_postfix_len = SMS4_MIC_LEN; + } +#endif + + ret = rtw_cfg80211_set_auth_type(psecuritypriv, sme->auth_type); + +#ifdef CONFIG_WAPI_SUPPORT + if (psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_WAPI) + padapter->mlmeextpriv.mlmext_info.auth_algo = psecuritypriv->dot11AuthAlgrthm; +#endif + + + if (ret < 0) + goto cancel_ps_deny; + + RTW_INFO("%s, ie_len=%zu\n", __func__, sme->ie_len); + + ret = rtw_cfg80211_set_wpa_ie(padapter, (u8 *)sme->ie, sme->ie_len); + if (ret < 0) + goto cancel_ps_deny; + + if (sme->crypto.n_ciphers_pairwise) { + ret = rtw_cfg80211_set_cipher(psecuritypriv, sme->crypto.ciphers_pairwise[0], _TRUE); + if (ret < 0) + goto cancel_ps_deny; + } + + /* For WEP Shared auth */ + if (sme->key_len > 0 && sme->key) { + u32 wep_key_idx, wep_key_len, wep_total_len; + NDIS_802_11_WEP *pwep = NULL; + RTW_INFO("%s(): Shared/Auto WEP\n", __FUNCTION__); + + wep_key_idx = sme->key_idx; + wep_key_len = sme->key_len; + + if (sme->key_idx > WEP_KEYS) { + ret = -EINVAL; + goto cancel_ps_deny; + } + + if (wep_key_len > 0) { + wep_key_len = wep_key_len <= 5 ? 5 : 13; + wep_total_len = wep_key_len + FIELD_OFFSET(NDIS_802_11_WEP, KeyMaterial); + pwep = (NDIS_802_11_WEP *) rtw_malloc(wep_total_len); + if (pwep == NULL) { + RTW_INFO(" wpa_set_encryption: pwep allocate fail !!!\n"); + ret = -ENOMEM; + goto cancel_ps_deny; + } + + _rtw_memset(pwep, 0, wep_total_len); + + pwep->KeyLength = wep_key_len; + pwep->Length = wep_total_len; + + if (wep_key_len == 13) { + padapter->securitypriv.dot11PrivacyAlgrthm = _WEP104_; + padapter->securitypriv.dot118021XGrpPrivacy = _WEP104_; + } + } else { + ret = -EINVAL; + goto cancel_ps_deny; + } + + pwep->KeyIndex = wep_key_idx; + pwep->KeyIndex |= 0x80000000; + + _rtw_memcpy(pwep->KeyMaterial, (void *)sme->key, pwep->KeyLength); + + if (rtw_set_802_11_add_wep(padapter, pwep) == (u8)_FAIL) + ret = -EOPNOTSUPP ; + + if (pwep) + rtw_mfree((u8 *)pwep, wep_total_len); + + if (ret < 0) + goto cancel_ps_deny; + } + + ret = rtw_cfg80211_set_cipher(psecuritypriv, sme->crypto.cipher_group, _FALSE); + if (ret < 0) + return ret; + + if (sme->crypto.n_akm_suites) { + ret = rtw_cfg80211_set_key_mgt(psecuritypriv, sme->crypto.akm_suites[0]); + if (ret < 0) + goto cancel_ps_deny; + } +#ifdef CONFIG_8011R + else { + /*It could be a connection without RSN IEs*/ + psecuritypriv->rsn_akm_suite_type = 0; + } +#endif + +#ifdef CONFIG_WAPI_SUPPORT + if (sme->crypto.akm_suites[0] == WLAN_AKM_SUITE_WAPI_PSK) + padapter->wapiInfo.bWapiPSK = true; + else if (sme->crypto.akm_suites[0] == WLAN_AKM_SUITE_WAPI_CERT) + padapter->wapiInfo.bWapiPSK = false; +#endif + + authmode = psecuritypriv->ndisauthtype; + rtw_set_802_11_authentication_mode(padapter, authmode); + + /* rtw_set_802_11_encryption_mode(padapter, padapter->securitypriv.ndisencryptstatus); */ + +#ifdef CONFIG_RTW_MBO + rtw_mbo_update_ie_data(padapter, (u8 *)sme->ie, sme->ie_len); +#endif + + if (rtw_set_802_11_connect(padapter, (u8 *)sme->bssid, &ndis_ssid, \ + sme->channel ? sme->channel->hw_value : 0) == _FALSE) { + ret = -1; + goto cancel_ps_deny; + } + + + _enter_critical_bh(&pwdev_priv->connect_req_lock, &irqL); + + if (pwdev_priv->connect_req) { + rtw_wdev_free_connect_req(pwdev_priv); + RTW_INFO(FUNC_NDEV_FMT" free existing connect_req\n", FUNC_NDEV_ARG(ndev)); + } + + pwdev_priv->connect_req = (struct cfg80211_connect_params *)rtw_malloc(sizeof(*pwdev_priv->connect_req)); + if (pwdev_priv->connect_req) + _rtw_memcpy(pwdev_priv->connect_req, sme, sizeof(*pwdev_priv->connect_req)); + else + RTW_WARN(FUNC_NDEV_FMT" alloc connect_req fail\n", FUNC_NDEV_ARG(ndev)); + + _exit_critical_bh(&pwdev_priv->connect_req_lock, &irqL); + + RTW_INFO("set ssid:dot11AuthAlgrthm=%d, dot11PrivacyAlgrthm=%d, dot118021XGrpPrivacy=%d\n", psecuritypriv->dot11AuthAlgrthm, psecuritypriv->dot11PrivacyAlgrthm, + psecuritypriv->dot118021XGrpPrivacy); + +cancel_ps_deny: + rtw_ps_deny_cancel(padapter, PS_DENY_JOIN); + +exit: + RTW_INFO("<=%s, ret %d\n", __FUNCTION__, ret); + +#if (RTW_CFG80211_BLOCK_STA_DISCON_EVENT & RTW_CFG80211_BLOCK_DISCON_WHEN_CONNECT) + rtw_wdev_set_not_indic_disco(pwdev_priv, 0); +#endif + + return ret; +} + +static int cfg80211_rtw_disconnect(struct wiphy *wiphy, struct net_device *ndev, + u16 reason_code) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + + RTW_INFO(FUNC_NDEV_FMT" - Start to Disconnect\n", FUNC_NDEV_ARG(ndev)); + +#if (RTW_CFG80211_BLOCK_STA_DISCON_EVENT & RTW_CFG80211_BLOCK_DISCON_WHEN_DISCONNECT) + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) + if (!wiphy->dev.power.is_prepared) + #endif + rtw_wdev_set_not_indic_disco(adapter_wdev_data(padapter), 1); +#endif + + rtw_set_to_roam(padapter, 0); + + /* if(check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE)) */ + { + _rtw_disconnect(wiphy, ndev); + } + +#if (RTW_CFG80211_BLOCK_STA_DISCON_EVENT & RTW_CFG80211_BLOCK_DISCON_WHEN_DISCONNECT) + rtw_wdev_set_not_indic_disco(adapter_wdev_data(padapter), 0); +#endif + + RTW_INFO(FUNC_NDEV_FMT" return 0\n", FUNC_NDEV_ARG(ndev)); + return 0; +} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)) +#ifdef CONFIG_RTW_DEBUG +static const char *nl80211_tx_power_setting_str(int type) +{ + switch (type) { + case NL80211_TX_POWER_AUTOMATIC: + return "AUTO"; + case NL80211_TX_POWER_LIMITED: + return "LIMIT"; + case NL80211_TX_POWER_FIXED: + return "FIX"; + default: + return "UNKNOWN"; + }; +} +#endif /* CONFIG_RTW_DEBUG */ + +static int cfg80211_rtw_set_txpower(struct wiphy *wiphy, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + struct wireless_dev *wdev, +#endif +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)) || defined(COMPAT_KERNEL_RELEASE) + enum nl80211_tx_power_setting type, int mbm) +#else + enum tx_power_setting type, int dbm) +#endif +{ +#if !((LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)) || defined(COMPAT_KERNEL_RELEASE)) + int mbm = dbm * 100; +#endif + struct rtw_wiphy_data *wiphy_data = rtw_wiphy_priv(wiphy); + _adapter *adapter = wiphy_to_adapter(wiphy); + int ret = -EOPNOTSUPP; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + if (wdev) { + RTW_WARN(FUNC_WIPHY_FMT" wdev specific control is not supported\n", FUNC_WIPHY_ARG(wiphy)); + goto exit; + } +#endif + + RTW_INFO(FUNC_WIPHY_FMT" type:%s(%u) mbm:%d\n", FUNC_WIPHY_ARG(wiphy) + , nl80211_tx_power_setting_str(type), type, mbm); + + switch (type) { + case NL80211_TX_POWER_AUTOMATIC: + wiphy_data->txpwr_total_lmt_mbm = UNSPECIFIED_MBM; + wiphy_data->txpwr_total_target_mbm = UNSPECIFIED_MBM; + ret = 0; + break; + case NL80211_TX_POWER_LIMITED: + if (!phy_is_txpwr_user_mbm_valid(adapter, mbm)) { + RTW_WARN(FUNC_WIPHY_FMT" mbm:%d not support\n", FUNC_WIPHY_ARG(wiphy), mbm); + goto exit; + } + wiphy_data->txpwr_total_lmt_mbm = mbm; + wiphy_data->txpwr_total_target_mbm = UNSPECIFIED_MBM; + ret = 0; + break; + case NL80211_TX_POWER_FIXED: + if (!phy_is_txpwr_user_mbm_valid(adapter, mbm)) { + RTW_WARN(FUNC_WIPHY_FMT" mbm:%d not support\n", FUNC_WIPHY_ARG(wiphy), mbm); + goto exit; + } + wiphy_data->txpwr_total_lmt_mbm = UNSPECIFIED_MBM; + wiphy_data->txpwr_total_target_mbm = mbm; + ret = 0; + break; + default: + RTW_WARN(FUNC_WIPHY_FMT" unknown type:%d\n", FUNC_WIPHY_ARG(wiphy), type); + } + + if (ret == 0) + rtw_run_in_thread_cmd_wait(adapter, ((void *)(rtw_hal_update_txpwr_level)), adapter, 2000); + +exit: + return ret; +} + +static int cfg80211_rtw_get_txpower(struct wiphy *wiphy, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + struct wireless_dev *wdev, +#endif + int *dbm) +{ + struct dvobj_priv *dvobj = wiphy_to_dvobj(wiphy); + s16 mbm; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + if (wdev && wdev_to_ndev(wdev)) { + _adapter *adapter = (_adapter *)rtw_netdev_priv(wdev_to_ndev(wdev)); + mbm = rtw_adapter_get_oper_txpwr_max_mbm(adapter, 1); + RTW_INFO(FUNC_ADPT_FMT" total max: %d mbm\n", FUNC_ADPT_ARG(adapter), mbm); + } else +#endif + { + mbm = rtw_get_oper_txpwr_max_mbm(dvobj, 1); + RTW_INFO(FUNC_WIPHY_FMT" total max: %d mbm\n", FUNC_WIPHY_ARG(wiphy), mbm); + } + + *dbm = mbm / MBM_PDBM; + + return 0; +} +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)) */ + +inline bool rtw_cfg80211_pwr_mgmt(_adapter *adapter) +{ + struct rtw_wdev_priv *rtw_wdev_priv = adapter_wdev_data(adapter); + struct wireless_dev *wdev = rtw_wdev_priv->rtw_wdev; + + return wdev->ps; +} + +static int cfg80211_rtw_set_power_mgmt(struct wiphy *wiphy, + struct net_device *ndev, + bool enabled, int timeout) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + + RTW_INFO(FUNC_NDEV_FMT" enabled:%u, timeout:%d\n", FUNC_NDEV_ARG(ndev), + enabled, timeout); + +#ifdef CONFIG_LPS + if (!enabled) + rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_LEAVE_CFG80211_PWRMGMT, 0); +#endif + + return 0; +} + +static void _rtw_set_pmksa(struct net_device *ndev, + u8 *bssid, u8 *pmkid) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + struct security_priv *psecuritypriv = &padapter->securitypriv; + u8 index, blInserted = _FALSE; + + /* overwrite PMKID */ + for (index = 0 ; index < NUM_PMKID_CACHE; index++) { + if (_rtw_memcmp(psecuritypriv->PMKIDList[index].Bssid, bssid, ETH_ALEN) == _TRUE) { + /* BSSID is matched, the same AP => rewrite with new PMKID. */ + RTW_INFO("BSSID("MAC_FMT") exists in the PMKList.\n", MAC_ARG(bssid)); + + _rtw_memcpy(psecuritypriv->PMKIDList[index].PMKID, pmkid, WLAN_PMKID_LEN); + psecuritypriv->PMKIDList[index].bUsed = _TRUE; + blInserted = _TRUE; + break; + } + } + + if (!blInserted) { + /* Find a new entry */ + RTW_INFO("Use the new entry index = %d for this PMKID.\n", + psecuritypriv->PMKIDIndex); + + _rtw_memcpy(psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].Bssid, bssid, ETH_ALEN); + _rtw_memcpy(psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].PMKID, pmkid, WLAN_PMKID_LEN); + + psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].bUsed = _TRUE; + psecuritypriv->PMKIDIndex++ ; + if (psecuritypriv->PMKIDIndex == 16) + psecuritypriv->PMKIDIndex = 0; + } +} + +static int cfg80211_rtw_set_pmksa(struct wiphy *wiphy, + struct net_device *ndev, + struct cfg80211_pmksa *pmksa) +{ + u8 index, blInserted = _FALSE; + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + struct mlme_priv *mlme = &padapter->mlmepriv; + struct security_priv *psecuritypriv = &padapter->securitypriv; + u8 strZeroMacAddress[ETH_ALEN] = { 0x00 }; + bool sae_auth = rtw_sec_chk_auth_type(padapter, MLME_AUTHTYPE_SAE); + + RTW_INFO(FUNC_NDEV_FMT" "MAC_FMT" "KEY_FMT"\n", FUNC_NDEV_ARG(ndev) + , MAC_ARG(pmksa->bssid), KEY_ARG(pmksa->pmkid)); + + if (_rtw_memcmp((u8 *)pmksa->bssid, strZeroMacAddress, ETH_ALEN) == _TRUE) + return -EINVAL; + + _rtw_set_pmksa(ndev, (u8 *)pmksa->bssid, (u8 *)pmksa->pmkid); + + if (sae_auth && + (psecuritypriv->extauth_status == WLAN_STATUS_SUCCESS)) { + RTW_PRINT("SAE: auth success, start assoc\n"); + start_clnt_assoc(padapter); + } + + return 0; +} + +static int cfg80211_rtw_del_pmksa(struct wiphy *wiphy, + struct net_device *ndev, + struct cfg80211_pmksa *pmksa) +{ + u8 index, bMatched = _FALSE; + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + struct security_priv *psecuritypriv = &padapter->securitypriv; + + RTW_INFO(FUNC_NDEV_FMT" "MAC_FMT" "KEY_FMT"\n", FUNC_NDEV_ARG(ndev) + , MAC_ARG(pmksa->bssid), KEY_ARG(pmksa->pmkid)); + + for (index = 0 ; index < NUM_PMKID_CACHE; index++) { + if (_rtw_memcmp(psecuritypriv->PMKIDList[index].Bssid, (u8 *)pmksa->bssid, ETH_ALEN) == _TRUE) { + /* BSSID is matched, the same AP => Remove this PMKID information and reset it. */ + _rtw_memset(psecuritypriv->PMKIDList[index].Bssid, 0x00, ETH_ALEN); + _rtw_memset(psecuritypriv->PMKIDList[index].PMKID, 0x00, WLAN_PMKID_LEN); + psecuritypriv->PMKIDList[index].bUsed = _FALSE; + bMatched = _TRUE; + RTW_INFO(FUNC_NDEV_FMT" clear id:%hhu\n", FUNC_NDEV_ARG(ndev), index); + break; + } + } + + if (_FALSE == bMatched) { + RTW_INFO(FUNC_NDEV_FMT" do not have matched BSSID\n" + , FUNC_NDEV_ARG(ndev)); + return -EINVAL; + } + + return 0; +} + +static int cfg80211_rtw_flush_pmksa(struct wiphy *wiphy, + struct net_device *ndev) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + struct security_priv *psecuritypriv = &padapter->securitypriv; + + RTW_INFO(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(ndev)); + + _rtw_memset(&psecuritypriv->PMKIDList[0], 0x00, sizeof(RT_PMKID_LIST) * NUM_PMKID_CACHE); + psecuritypriv->PMKIDIndex = 0; + + return 0; +} + +static int rtw_cfg80211_monitor_if_open(struct net_device *ndev) +{ + int ret = 0; + + RTW_INFO("%s\n", __func__); + + return ret; +} + +static int rtw_cfg80211_monitor_if_close(struct net_device *ndev) +{ + int ret = 0; + + RTW_INFO("%s\n", __func__); + + return ret; +} + +static int rtw_cfg80211_monitor_if_xmit_entry(struct sk_buff *skb, struct net_device *ndev) +{ + int ret = 0; + int rtap_len; + int qos_len = 0; + int dot11_hdr_len = 24; + int snap_len = 6; + unsigned char *pdata; + u16 frame_ctl; + unsigned char src_mac_addr[ETH_ALEN]; + unsigned char dst_mac_addr[ETH_ALEN]; + struct rtw_ieee80211_hdr *dot11_hdr; + struct ieee80211_radiotap_header *rtap_hdr; + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); +#ifdef CONFIG_DFS_MASTER + struct rf_ctl_t *rfctl = adapter_to_rfctl(padapter); +#endif + + RTW_INFO(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(ndev)); + + rtw_mstat_update(MSTAT_TYPE_SKB, MSTAT_ALLOC_SUCCESS, skb->truesize); + + if (IS_CH_WAITING(rfctl)) { + #ifdef CONFIG_DFS_MASTER + if (rtw_rfctl_overlap_radar_detect_ch(rfctl)) + goto fail; + #endif + } + + if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header))) + goto fail; + + rtap_hdr = (struct ieee80211_radiotap_header *)skb->data; + if (unlikely(rtap_hdr->it_version)) + goto fail; + + rtap_len = ieee80211_get_radiotap_len(skb->data); + if (unlikely(skb->len < rtap_len)) + goto fail; + + if (rtap_len != 14) { + RTW_INFO("radiotap len (should be 14): %d\n", rtap_len); + goto fail; + } + + /* Skip the ratio tap header */ + skb_pull(skb, rtap_len); + + dot11_hdr = (struct rtw_ieee80211_hdr *)skb->data; + frame_ctl = le16_to_cpu(dot11_hdr->frame_ctl); + /* Check if the QoS bit is set */ + if ((frame_ctl & RTW_IEEE80211_FCTL_FTYPE) == RTW_IEEE80211_FTYPE_DATA) { + /* Check if this ia a Wireless Distribution System (WDS) frame + * which has 4 MAC addresses + */ + if (dot11_hdr->frame_ctl & 0x0080) + qos_len = 2; + if ((dot11_hdr->frame_ctl & 0x0300) == 0x0300) + dot11_hdr_len += 6; + + memcpy(dst_mac_addr, dot11_hdr->addr1, sizeof(dst_mac_addr)); + memcpy(src_mac_addr, dot11_hdr->addr2, sizeof(src_mac_addr)); + + /* Skip the 802.11 header, QoS (if any) and SNAP, but leave spaces for + * for two MAC addresses + */ + skb_pull(skb, dot11_hdr_len + qos_len + snap_len - sizeof(src_mac_addr) * 2); + pdata = (unsigned char *)skb->data; + memcpy(pdata, dst_mac_addr, sizeof(dst_mac_addr)); + memcpy(pdata + sizeof(dst_mac_addr), src_mac_addr, sizeof(src_mac_addr)); + + RTW_INFO("should be eapol packet\n"); + + /* Use the real net device to transmit the packet */ + ret = _rtw_xmit_entry(skb, padapter->pnetdev); + + return ret; + + } else if ((frame_ctl & (RTW_IEEE80211_FCTL_FTYPE | RTW_IEEE80211_FCTL_STYPE)) + == (RTW_IEEE80211_FTYPE_MGMT | RTW_IEEE80211_STYPE_ACTION) + ) { + /* only for action frames */ + struct xmit_frame *pmgntframe; + struct pkt_attrib *pattrib; + unsigned char *pframe; + /* u8 category, action, OUI_Subtype, dialogToken=0; */ + /* unsigned char *frame_body; */ + struct rtw_ieee80211_hdr *pwlanhdr; + struct xmit_priv *pxmitpriv = &(padapter->xmitpriv); + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + u8 *buf = skb->data; + u32 len = skb->len; + u8 category, action; + int type = -1; + + if (rtw_action_frame_parse(buf, len, &category, &action) == _FALSE) { + RTW_INFO(FUNC_NDEV_FMT" frame_control:0x%x\n", FUNC_NDEV_ARG(ndev), + le16_to_cpu(((struct rtw_ieee80211_hdr_3addr *)buf)->frame_ctl)); + goto fail; + } + + RTW_INFO("RTW_Tx:da="MAC_FMT" via "FUNC_NDEV_FMT"\n", + MAC_ARG(GetAddr1Ptr(buf)), FUNC_NDEV_ARG(ndev)); + #ifdef CONFIG_P2P + type = rtw_p2p_check_frames(padapter, buf, len, _TRUE); + if (type >= 0) + goto dump; + #endif + if (category == RTW_WLAN_CATEGORY_PUBLIC) + RTW_INFO("RTW_Tx:%s\n", action_public_str(action)); + else + RTW_INFO("RTW_Tx:category(%u), action(%u)\n", category, action); +#ifdef CONFIG_P2P +dump: +#endif + /* starting alloc mgmt frame to dump it */ + pmgntframe = alloc_mgtxmitframe(pxmitpriv); + if (pmgntframe == NULL) + goto fail; + + /* update attribute */ + pattrib = &pmgntframe->attrib; + update_mgntframe_attrib(padapter, pattrib); + pattrib->retry_ctrl = _FALSE; + + _rtw_memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET); + + pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET; + + _rtw_memcpy(pframe, (void *)buf, len); + pattrib->pktlen = len; + +#ifdef CONFIG_P2P + if (type >= 0) + rtw_xframe_chk_wfd_ie(pmgntframe); +#endif /* CONFIG_P2P */ + + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + /* update seq number */ + pmlmeext->mgnt_seq = GetSequence(pwlanhdr); + pattrib->seqnum = pmlmeext->mgnt_seq; + pmlmeext->mgnt_seq++; + + pattrib->last_txcmdsz = pattrib->pktlen; + + dump_mgntframe(padapter, pmgntframe); + + } else + RTW_INFO("frame_ctl=0x%x\n", frame_ctl & (RTW_IEEE80211_FCTL_FTYPE | RTW_IEEE80211_FCTL_STYPE)); + + +fail: + + rtw_skb_free(skb); + + return 0; + +} + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0)) +static void rtw_cfg80211_monitor_if_set_multicast_list(struct net_device *ndev) +{ + RTW_INFO("%s\n", __func__); +} +#endif +static int rtw_cfg80211_monitor_if_set_mac_address(struct net_device *ndev, void *addr) +{ + int ret = 0; + + RTW_INFO("%s\n", __func__); + + return ret; +} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)) +static const struct net_device_ops rtw_cfg80211_monitor_if_ops = { + .ndo_open = rtw_cfg80211_monitor_if_open, + .ndo_stop = rtw_cfg80211_monitor_if_close, + .ndo_start_xmit = rtw_cfg80211_monitor_if_xmit_entry, + #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0)) + .ndo_set_multicast_list = rtw_cfg80211_monitor_if_set_multicast_list, + #endif + .ndo_set_mac_address = rtw_cfg80211_monitor_if_set_mac_address, +}; +#endif + +static int rtw_cfg80211_add_monitor_if(_adapter *padapter, char *name, struct net_device **ndev) +{ + int ret = 0; + struct net_device *mon_ndev = NULL; + struct wireless_dev *mon_wdev = NULL; + struct rtw_netdev_priv_indicator *pnpi; + struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(padapter); + + if (!name) { + RTW_INFO(FUNC_ADPT_FMT" without specific name\n", FUNC_ADPT_ARG(padapter)); + ret = -EINVAL; + goto out; + } + + if (pwdev_priv->pmon_ndev) { + RTW_INFO(FUNC_ADPT_FMT" monitor interface exist: "NDEV_FMT"\n", + FUNC_ADPT_ARG(padapter), NDEV_ARG(pwdev_priv->pmon_ndev)); + ret = -EBUSY; + goto out; + } + + mon_ndev = alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator)); + if (!mon_ndev) { + RTW_INFO(FUNC_ADPT_FMT" allocate ndev fail\n", FUNC_ADPT_ARG(padapter)); + ret = -ENOMEM; + goto out; + } + + mon_ndev->type = ARPHRD_IEEE80211_RADIOTAP; + strncpy(mon_ndev->name, name, IFNAMSIZ); + mon_ndev->name[IFNAMSIZ - 1] = 0; +#if (LINUX_VERSION_CODE > KERNEL_VERSION(4, 11, 8)) + mon_ndev->priv_destructor = rtw_ndev_destructor; +#else + mon_ndev->destructor = rtw_ndev_destructor; +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)) + mon_ndev->netdev_ops = &rtw_cfg80211_monitor_if_ops; +#else + mon_ndev->open = rtw_cfg80211_monitor_if_open; + mon_ndev->stop = rtw_cfg80211_monitor_if_close; + mon_ndev->hard_start_xmit = rtw_cfg80211_monitor_if_xmit_entry; + mon_ndev->set_mac_address = rtw_cfg80211_monitor_if_set_mac_address; +#endif + + pnpi = netdev_priv(mon_ndev); + pnpi->priv = padapter; + pnpi->sizeof_priv = sizeof(_adapter); + + /* wdev */ + mon_wdev = (struct wireless_dev *)rtw_zmalloc(sizeof(struct wireless_dev)); + if (!mon_wdev) { + RTW_INFO(FUNC_ADPT_FMT" allocate mon_wdev fail\n", FUNC_ADPT_ARG(padapter)); + ret = -ENOMEM; + goto out; + } + + mon_wdev->wiphy = padapter->rtw_wdev->wiphy; + mon_wdev->netdev = mon_ndev; + mon_wdev->iftype = NL80211_IFTYPE_MONITOR; + mon_ndev->ieee80211_ptr = mon_wdev; + + ret = register_netdevice(mon_ndev); + if (ret) + goto out; + + *ndev = pwdev_priv->pmon_ndev = mon_ndev; + _rtw_memcpy(pwdev_priv->ifname_mon, name, IFNAMSIZ + 1); + +out: + if (ret && mon_wdev) { + rtw_mfree((u8 *)mon_wdev, sizeof(struct wireless_dev)); + mon_wdev = NULL; + } + + if (ret && mon_ndev) { + free_netdev(mon_ndev); + *ndev = mon_ndev = NULL; + } + + return ret; +} + +#ifdef CONFIG_AP_MODE +void rtw_cfg80211_indicate_sta_assoc(_adapter *padapter, u8 *pmgmt_frame, uint frame_len) +{ +#if !defined(RTW_USE_CFG80211_STA_EVENT) && !defined(COMPAT_KERNEL_RELEASE) + s32 freq; + int channel; + struct wireless_dev *pwdev = padapter->rtw_wdev; + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); +#endif + struct net_device *ndev = padapter->pnetdev; + + RTW_INFO(FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(padapter)); + +#if defined(RTW_USE_CFG80211_STA_EVENT) || defined(COMPAT_KERNEL_RELEASE) + { + struct station_info sinfo; + u8 ie_offset; + if (get_frame_sub_type(pmgmt_frame) == WIFI_ASSOCREQ) + ie_offset = _ASOCREQ_IE_OFFSET_; + else /* WIFI_REASSOCREQ */ + ie_offset = _REASOCREQ_IE_OFFSET_; + + memset(&sinfo, 0, sizeof(sinfo)); +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)) + sinfo.filled = STATION_INFO_ASSOC_REQ_IES; + sinfo.assoc_req_ies = pmgmt_frame + WLAN_HDR_A3_LEN + ie_offset; + sinfo.assoc_req_ies_len = frame_len - WLAN_HDR_A3_LEN - ie_offset; +#endif + cfg80211_new_sta(ndev, get_addr2_ptr(pmgmt_frame), &sinfo, GFP_ATOMIC); + } +#else /* defined(RTW_USE_CFG80211_STA_EVENT) */ + channel = pmlmeext->cur_channel; + freq = rtw_ch2freq(channel); + + #ifdef COMPAT_KERNEL_RELEASE + rtw_cfg80211_rx_mgmt(pwdev, freq, 0, pmgmt_frame, frame_len, GFP_ATOMIC); + #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) && !defined(CONFIG_CFG80211_FORCE_COMPATIBLE_2_6_37_UNDER) + rtw_cfg80211_rx_mgmt(pwdev, freq, 0, pmgmt_frame, frame_len, GFP_ATOMIC); + #else /* COMPAT_KERNEL_RELEASE */ + { + /* to avoid WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION) when calling cfg80211_send_rx_assoc() */ + #ifndef CONFIG_PLATFORM_MSTAR + pwdev->iftype = NL80211_IFTYPE_STATION; + #endif /* CONFIG_PLATFORM_MSTAR */ + RTW_INFO("iftype=%d before call cfg80211_send_rx_assoc()\n", pwdev->iftype); + rtw_cfg80211_send_rx_assoc(padapter, NULL, pmgmt_frame, frame_len); + RTW_INFO("iftype=%d after call cfg80211_send_rx_assoc()\n", pwdev->iftype); + pwdev->iftype = NL80211_IFTYPE_AP; + /* cfg80211_rx_action(padapter->pnetdev, freq, pmgmt_frame, frame_len, GFP_ATOMIC); */ + } + #endif /* COMPAT_KERNEL_RELEASE */ +#endif /* defined(RTW_USE_CFG80211_STA_EVENT) */ + +} + +void rtw_cfg80211_indicate_sta_disassoc(_adapter *padapter, const u8 *da, unsigned short reason) +{ +#if !defined(RTW_USE_CFG80211_STA_EVENT) && !defined(COMPAT_KERNEL_RELEASE) + s32 freq; + int channel; + u8 *pmgmt_frame; + uint frame_len; + struct rtw_ieee80211_hdr *pwlanhdr; + unsigned short *fctrl; + u8 mgmt_buf[128] = {0}; + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + struct wireless_dev *wdev = padapter->rtw_wdev; +#endif + struct net_device *ndev = padapter->pnetdev; + + RTW_INFO(FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(padapter)); + +#if defined(RTW_USE_CFG80211_STA_EVENT) || defined(COMPAT_KERNEL_RELEASE) + cfg80211_del_sta(ndev, da, GFP_ATOMIC); +#else /* defined(RTW_USE_CFG80211_STA_EVENT) */ + channel = pmlmeext->cur_channel; + freq = rtw_ch2freq(channel); + + pmgmt_frame = mgmt_buf; + pwlanhdr = (struct rtw_ieee80211_hdr *)pmgmt_frame; + + fctrl = &(pwlanhdr->frame_ctl); + *(fctrl) = 0; + + _rtw_memcpy(pwlanhdr->addr1, adapter_mac_addr(padapter), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr2, da, ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr3, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); + + SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq); + pmlmeext->mgnt_seq++; + set_frame_sub_type(pmgmt_frame, WIFI_DEAUTH); + + pmgmt_frame += sizeof(struct rtw_ieee80211_hdr_3addr); + frame_len = sizeof(struct rtw_ieee80211_hdr_3addr); + + reason = cpu_to_le16(reason); + pmgmt_frame = rtw_set_fixed_ie(pmgmt_frame, _RSON_CODE_ , (unsigned char *)&reason, &frame_len); + + #ifdef COMPAT_KERNEL_RELEASE + rtw_cfg80211_rx_mgmt(wdev, freq, 0, mgmt_buf, frame_len, GFP_ATOMIC); + #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) && !defined(CONFIG_CFG80211_FORCE_COMPATIBLE_2_6_37_UNDER) + rtw_cfg80211_rx_mgmt(wdev, freq, 0, mgmt_buf, frame_len, GFP_ATOMIC); + #else /* COMPAT_KERNEL_RELEASE */ + cfg80211_send_disassoc(padapter->pnetdev, mgmt_buf, frame_len); + /* cfg80211_rx_action(padapter->pnetdev, freq, mgmt_buf, frame_len, GFP_ATOMIC); */ + #endif /* COMPAT_KERNEL_RELEASE */ +#endif /* defined(RTW_USE_CFG80211_STA_EVENT) */ +} + +static int rtw_add_beacon(_adapter *adapter, const u8 *head, size_t head_len, const u8 *tail, size_t tail_len) +{ + int ret = 0; + u8 *pbuf = NULL; + uint len, wps_ielen = 0; + uint p2p_ielen = 0; + u8 got_p2p_ie = _FALSE; + struct mlme_priv *pmlmepriv = &(adapter->mlmepriv); + /* struct sta_priv *pstapriv = &padapter->stapriv; */ + + + RTW_INFO("%s beacon_head_len=%zu, beacon_tail_len=%zu\n", __FUNCTION__, head_len, tail_len); + + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) != _TRUE) + return -EINVAL; + + if (head_len < 24) + return -EINVAL; + + #ifdef CONFIG_FW_HANDLE_TXBCN + if (!rtw_ap_nums_check(adapter)) { + RTW_ERR(FUNC_ADPT_FMT"failed, con't support over %d BCN\n", FUNC_ADPT_ARG(adapter), CONFIG_LIMITED_AP_NUM); + return -EINVAL; + } + #endif /*CONFIG_FW_HANDLE_TXBCN*/ + + pbuf = rtw_zmalloc(head_len + tail_len); + if (!pbuf) { + ret = -ENOMEM; + goto exit; + } + + + /* _rtw_memcpy(&pstapriv->max_num_sta, param->u.bcn_ie.reserved, 2); */ + + /* if((pstapriv->max_num_sta>NUM_STA) || (pstapriv->max_num_sta<=0)) */ + /* pstapriv->max_num_sta = NUM_STA; */ + + + _rtw_memcpy(pbuf, (void *)head + 24, head_len - 24); /* 24=beacon header len. */ + _rtw_memcpy(pbuf + head_len - 24, (void *)tail, tail_len); + + len = head_len + tail_len - 24; + + /* check wps ie if inclued */ + if (rtw_get_wps_ie(pbuf + _FIXED_IE_LENGTH_, len - _FIXED_IE_LENGTH_, NULL, &wps_ielen)) + RTW_INFO("add bcn, wps_ielen=%d\n", wps_ielen); + +#ifdef CONFIG_P2P + if (adapter->wdinfo.driver_interface == DRIVER_CFG80211) { + /* check p2p if enable */ + if (rtw_get_p2p_ie(pbuf + _FIXED_IE_LENGTH_, len - _FIXED_IE_LENGTH_, NULL, &p2p_ielen)) { + struct wifidirect_info *pwdinfo = &(adapter->wdinfo); + + RTW_INFO("got p2p_ie, len=%d\n", p2p_ielen); + + got_p2p_ie = _TRUE; + + if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) { + + if (rtw_p2p_enable(adapter, P2P_ROLE_GO)) { + RTW_INFO("Enable P2P function for the first time\n"); + adapter->stapriv.expire_to = 3; /* 3x2 = 6 sec in p2p mode */ + } else { + ret = -EOPNOTSUPP; + goto exit; + } + } else { + RTW_INFO("enter GO Mode, p2p_ielen=%d\n", p2p_ielen); + + rtw_p2p_set_role(pwdinfo, P2P_ROLE_GO); + rtw_p2p_set_state(pwdinfo, P2P_STATE_GONEGO_OK); + pwdinfo->intent = 15; + } + } + } +#endif /* CONFIG_P2P */ + + /* pbss_network->IEs will not include p2p_ie, wfd ie */ + rtw_ies_remove_ie(pbuf, &len, _BEACON_IE_OFFSET_, _VENDOR_SPECIFIC_IE_, P2P_OUI, 4); + rtw_ies_remove_ie(pbuf, &len, _BEACON_IE_OFFSET_, _VENDOR_SPECIFIC_IE_, WFD_OUI, 4); + + if (rtw_check_beacon_data(adapter, pbuf, len) == _SUCCESS) { +#ifdef CONFIG_P2P + /* check p2p if enable */ + if (got_p2p_ie == _TRUE) { + struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv; + struct wifidirect_info *pwdinfo = &(adapter->wdinfo); + pwdinfo->operating_channel = pmlmeext->cur_channel; + } +#endif /* CONFIG_P2P */ + ret = 0; + } else + ret = -EINVAL; + +exit: + if (pbuf) + rtw_mfree(pbuf, head_len + tail_len); + + return ret; +} + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)) && !defined(COMPAT_KERNEL_RELEASE) +static int cfg80211_rtw_add_beacon(struct wiphy *wiphy, struct net_device *ndev, + struct beacon_parameters *info) +{ + int ret = 0; + _adapter *adapter = (_adapter *)rtw_netdev_priv(ndev); + + RTW_INFO(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(ndev)); + + if (rtw_cfg80211_sync_iftype(adapter) != _SUCCESS) { + ret = -ENOTSUPP; + goto exit; + } + rtw_mi_scan_abort(adapter, _TRUE); + rtw_mi_buddy_set_scan_deny(adapter, 300); + ret = rtw_add_beacon(adapter, info->head, info->head_len, info->tail, info->tail_len); + +exit: + return ret; +} + +static int cfg80211_rtw_set_beacon(struct wiphy *wiphy, struct net_device *ndev, + struct beacon_parameters *info) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(ndev); + struct mlme_ext_priv *pmlmeext = &(adapter->mlmeextpriv); + + RTW_INFO(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(ndev)); + + pmlmeext->bstart_bss = _TRUE; + + cfg80211_rtw_add_beacon(wiphy, ndev, info); + + return 0; +} + +static int cfg80211_rtw_del_beacon(struct wiphy *wiphy, struct net_device *ndev) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(ndev); + + RTW_INFO(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(ndev)); + + rtw_stop_ap_cmd(adapter, RTW_CMDF_WAIT_ACK); + return 0; +} +#else +static int cfg80211_rtw_start_ap(struct wiphy *wiphy, struct net_device *ndev, + struct cfg80211_ap_settings *settings) +{ + int ret = 0; + _adapter *adapter = (_adapter *)rtw_netdev_priv(ndev); + + RTW_INFO(FUNC_NDEV_FMT" hidden_ssid:%d, auth_type:%d\n", FUNC_NDEV_ARG(ndev), + settings->hidden_ssid, settings->auth_type); + + if (rtw_cfg80211_sync_iftype(adapter) != _SUCCESS) { + ret = -ENOTSUPP; + goto exit; + } + + /* Kernel < v5.x, the auth_type set as NL80211_AUTHTYPE_AUTOMATIC. if + * the AKM SAE in the RSN IE, we have to update the auth_type for SAE in + * rtw_check_beacon_data(). + * + * we only update auth_type when rtw_check_beacon_data() + */ + /* rtw_cfg80211_set_auth_type(&adapter->securitypriv, settings->auth_type); */ + + rtw_mi_scan_abort(adapter, _TRUE); + rtw_mi_buddy_set_scan_deny(adapter, 300); + + adapter->mlmeextpriv.mlmext_info.hidden_ssid_mode = settings->hidden_ssid; + ret = rtw_add_beacon(adapter, settings->beacon.head, settings->beacon.head_len, + settings->beacon.tail, settings->beacon.tail_len); + + if (settings->ssid && settings->ssid_len) { + WLAN_BSSID_EX *pbss_network = &adapter->mlmepriv.cur_network.network; + WLAN_BSSID_EX *pbss_network_ext = &adapter->mlmeextpriv.mlmext_info.network; + + if (0) + RTW_INFO(FUNC_ADPT_FMT" ssid:(%s,%zu), from ie:(%s,%d)\n", FUNC_ADPT_ARG(adapter), + settings->ssid, settings->ssid_len, + pbss_network->Ssid.Ssid, pbss_network->Ssid.SsidLength); + + _rtw_memcpy(pbss_network->Ssid.Ssid, (void *)settings->ssid, settings->ssid_len); + pbss_network->Ssid.SsidLength = settings->ssid_len; + _rtw_memcpy(pbss_network_ext->Ssid.Ssid, (void *)settings->ssid, settings->ssid_len); + pbss_network_ext->Ssid.SsidLength = settings->ssid_len; + + if (0) + RTW_INFO(FUNC_ADPT_FMT" after ssid:(%s,%d), (%s,%d)\n", FUNC_ADPT_ARG(adapter), + pbss_network->Ssid.Ssid, pbss_network->Ssid.SsidLength, + pbss_network_ext->Ssid.Ssid, pbss_network_ext->Ssid.SsidLength); + } + +exit: + return ret; +} + +static int cfg80211_rtw_change_beacon(struct wiphy *wiphy, struct net_device *ndev, + struct cfg80211_beacon_data *info) +{ + int ret = 0; + _adapter *adapter = (_adapter *)rtw_netdev_priv(ndev); + + RTW_INFO(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(ndev)); + + ret = rtw_add_beacon(adapter, info->head, info->head_len, info->tail, info->tail_len); + + return ret; +} + +static int cfg80211_rtw_stop_ap(struct wiphy *wiphy, struct net_device *ndev) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(ndev); + + RTW_INFO(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(ndev)); + + rtw_stop_ap_cmd(adapter, RTW_CMDF_WAIT_ACK); + return 0; +} +#endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)) */ + +#if CONFIG_RTW_MACADDR_ACL && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) +static int cfg80211_rtw_set_mac_acl(struct wiphy *wiphy, struct net_device *ndev, + const struct cfg80211_acl_data *params) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(ndev); + u8 acl_mode = RTW_ACL_MODE_DISABLED; + int ret = -1; + int i; + + if (!params) { + RTW_WARN(FUNC_ADPT_FMT" params NULL\n", FUNC_ADPT_ARG(adapter)); + rtw_macaddr_acl_clear(adapter, RTW_ACL_PERIOD_BSS); + goto exit; + } + + RTW_INFO(FUNC_ADPT_FMT" acl_policy:%d, entry_num:%d\n" + , FUNC_ADPT_ARG(adapter), params->acl_policy, params->n_acl_entries); + + if (params->acl_policy == NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) + acl_mode = RTW_ACL_MODE_ACCEPT_UNLESS_LISTED; + else if (params->acl_policy == NL80211_ACL_POLICY_DENY_UNLESS_LISTED) + acl_mode = RTW_ACL_MODE_DENY_UNLESS_LISTED; + + rtw_macaddr_acl_clear(adapter, RTW_ACL_PERIOD_BSS); + + rtw_set_macaddr_acl(adapter, RTW_ACL_PERIOD_BSS, acl_mode); + + for (i = 0; i < params->n_acl_entries; i++) + rtw_acl_add_sta(adapter, RTW_ACL_PERIOD_BSS, params->mac_addrs[i].addr); + + ret = 0; + +exit: + return ret; +} +#endif /* CONFIG_RTW_MACADDR_ACL && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) */ + +const char *_nl80211_sta_flags_str[] = { + "INVALID", + "AUTHORIZED", + "SHORT_PREAMBLE", + "WME", + "MFP", + "AUTHENTICATED", + "TDLS_PEER", + "ASSOCIATED", +}; + +#define nl80211_sta_flags_str(_f) ((_f <= NL80211_STA_FLAG_MAX) ? _nl80211_sta_flags_str[_f] : _nl80211_sta_flags_str[0]) + +const char *_nl80211_plink_state_str[] = { + "LISTEN", + "OPN_SNT", + "OPN_RCVD", + "CNF_RCVD", + "ESTAB", + "HOLDING", + "BLOCKED", + "UNKNOWN", +}; + +#define nl80211_plink_state_str(_s) ((_s < NUM_NL80211_PLINK_STATES) ? _nl80211_plink_state_str[_s] : _nl80211_plink_state_str[NUM_NL80211_PLINK_STATES]) + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)) +#define NL80211_PLINK_ACTION_NO_ACTION PLINK_ACTION_INVALID +#define NL80211_PLINK_ACTION_OPEN PLINK_ACTION_OPEN +#define NL80211_PLINK_ACTION_BLOCK PLINK_ACTION_BLOCK +#define NUM_NL80211_PLINK_ACTIONS 3 +#endif + +const char *_nl80211_plink_actions_str[] = { + "NO_ACTION", + "OPEN", + "BLOCK", + "UNKNOWN", +}; + +#define nl80211_plink_actions_str(_a) ((_a < NUM_NL80211_PLINK_ACTIONS) ? _nl80211_plink_actions_str[_a] : _nl80211_plink_actions_str[NUM_NL80211_PLINK_ACTIONS]) + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) +const char *_nl80211_mesh_power_mode_str[] = { + "UNKNOWN", + "ACTIVE", + "LIGHT_SLEEP", + "DEEP_SLEEP", +}; + +#define nl80211_mesh_power_mode_str(_p) ((_p <= NL80211_MESH_POWER_MAX) ? _nl80211_mesh_power_mode_str[_p] : _nl80211_mesh_power_mode_str[0]) +#endif + +void dump_station_parameters(void *sel, struct wiphy *wiphy, const struct station_parameters *params) +{ +#if DBG_RTW_CFG80211_STA_PARAM + if (params->supported_rates_len) { + #define SUPP_RATES_BUF_LEN (3 * RTW_G_RATES_NUM + 1) + int i; + char supp_rates_buf[SUPP_RATES_BUF_LEN] = {0}; + u8 cnt = 0; + + rtw_warn_on(params->supported_rates_len > RTW_G_RATES_NUM); + + for (i = 0; i < params->supported_rates_len; i++) { + if (i >= RTW_G_RATES_NUM) + break; + cnt += snprintf(supp_rates_buf + cnt, SUPP_RATES_BUF_LEN - cnt -1 + , "%02X ", params->supported_rates[i]); + if (cnt >= SUPP_RATES_BUF_LEN - 1) + break; + } + + RTW_PRINT_SEL(sel, "supported_rates:%s\n", supp_rates_buf); + } + + if (params->vlan) + RTW_PRINT_SEL(sel, "vlan:"NDEV_FMT"\n", NDEV_ARG(params->vlan)); + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)) + if (params->sta_flags_mask) { + #define STA_FLAGS_BUF_LEN 128 + int i = 0; + char sta_flags_buf[STA_FLAGS_BUF_LEN] = {0}; + u8 cnt = 0; + + for (i = 1; i <= NL80211_STA_FLAG_MAX; i++) { + if (params->sta_flags_mask & BIT(i)) { + cnt += snprintf(sta_flags_buf + cnt, STA_FLAGS_BUF_LEN - cnt -1, "%s=%u " + , nl80211_sta_flags_str(i), (params->sta_flags_set & BIT(i)) ? 1 : 0); + if (cnt >= STA_FLAGS_BUF_LEN - 1) + break; + } + } + + RTW_PRINT_SEL(sel, "sta_flags:%s\n", sta_flags_buf); + } +#else + u32 station_flags; + #error "TBD\n" +#endif + + if (params->listen_interval != -1) + RTW_PRINT_SEL(sel, "listen_interval:%d\n", params->listen_interval); + + if (params->aid) + RTW_PRINT_SEL(sel, "aid:%u\n", params->aid); + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)) + if (params->peer_aid) + RTW_PRINT_SEL(sel, "peer_aid:%u\n", params->peer_aid); +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)) + if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION) + RTW_PRINT_SEL(sel, "plink_action:%s\n", nl80211_plink_actions_str(params->plink_action)); +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)) + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)) + if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE) + #endif + RTW_PRINT_SEL(sel, "plink_state:%s\n" + , nl80211_plink_state_str(params->plink_state)); +#endif + +#if 0 /* TODO */ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)) + const struct ieee80211_ht_cap *ht_capa; +#endif +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + const struct ieee80211_vht_cap *vht_capa; +#endif +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)) + if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD) + RTW_PRINT_SEL(sel, "uapsd_queues:0x%02x\n", params->uapsd_queues); + if (params->max_sp) + RTW_PRINT_SEL(sel, "max_sp:%u\n", params->max_sp); +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) + if (params->local_pm != NL80211_MESH_POWER_UNKNOWN) { + RTW_PRINT_SEL(sel, "local_pm:%s\n" + , nl80211_mesh_power_mode_str(params->local_pm)); + } + + if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY) + RTW_PRINT_SEL(sel, "capability:0x%04x\n", params->capability); + +#if 0 /* TODO */ + const u8 *ext_capab; + u8 ext_capab_len; +#endif +#endif + +#if 0 /* TODO */ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)) + const u8 *supported_channels; + u8 supported_channels_len; + const u8 *supported_oper_classes; + u8 supported_oper_classes_len; +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)) + u8 opmode_notif; + bool opmode_notif_used; +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)) + int support_p2p_ps; +#endif +#endif +#endif /* DBG_RTW_CFG80211_STA_PARAM */ +} + +static int cfg80211_rtw_add_station(struct wiphy *wiphy, struct net_device *ndev, +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 16, 0)) + u8 *mac, +#else + const u8 *mac, +#endif + struct station_parameters *params) +{ + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); +#if defined(CONFIG_TDLS) || defined(CONFIG_RTW_MESH) + struct sta_priv *pstapriv = &padapter->stapriv; +#endif +#ifdef CONFIG_TDLS + struct sta_info *psta; +#endif /* CONFIG_TDLS */ + + RTW_INFO(FUNC_NDEV_FMT" mac:"MAC_FMT"\n", FUNC_NDEV_ARG(ndev), MAC_ARG(mac)); + +#if CONFIG_RTW_MACADDR_ACL + if (rtw_access_ctrl(padapter, mac) == _FALSE) { + RTW_INFO(FUNC_NDEV_FMT" deny by macaddr ACL\n", FUNC_NDEV_ARG(ndev)); + ret = -EINVAL; + goto exit; + } +#endif + + dump_station_parameters(RTW_DBGDUMP, wiphy, params); + +#ifdef CONFIG_RTW_MESH + if (MLME_IS_MESH(padapter)) { + struct rtw_mesh_cfg *mcfg = &padapter->mesh_cfg; + struct rtw_mesh_info *minfo = &padapter->mesh_info; + struct mesh_plink_pool *plink_ctl = &minfo->plink_ctl; + struct mesh_plink_ent *plink = NULL; + struct wlan_network *scanned = NULL; + bool acnode = 0; + u8 add_new_sta = 0, probe_req = 0; + _irqL irqL; + + if (params->plink_state != NL80211_PLINK_LISTEN) { + RTW_WARN(FUNC_NDEV_FMT" %s\n", FUNC_NDEV_ARG(ndev), nl80211_plink_state_str(params->plink_state)); + rtw_warn_on(1); + } + if (!params->aid || params->aid > pstapriv->max_aid) { + RTW_WARN(FUNC_NDEV_FMT" invalid aid:%u\n", FUNC_NDEV_ARG(ndev), params->aid); + rtw_warn_on(1); + ret = -EINVAL; + goto exit; + } + + _enter_critical_bh(&(plink_ctl->lock), &irqL); + + plink = _rtw_mesh_plink_get(padapter, mac); + if (plink) + goto release_plink_ctl; + + #if CONFIG_RTW_MESH_PEER_BLACKLIST + if (rtw_mesh_peer_blacklist_search(padapter, mac)) { + RTW_INFO(FUNC_NDEV_FMT" deny by peer blacklist\n" + , FUNC_NDEV_ARG(ndev)); + ret = -EINVAL; + goto release_plink_ctl; + } + #endif + + scanned = rtw_find_network(&padapter->mlmepriv.scanned_queue, mac); + if (!scanned + || rtw_get_passing_time_ms(scanned->last_scanned) >= mcfg->peer_sel_policy.scanr_exp_ms + ) { + if (!scanned) + RTW_INFO(FUNC_NDEV_FMT" corresponding network not found\n", FUNC_NDEV_ARG(ndev)); + else + RTW_INFO(FUNC_NDEV_FMT" corresponding network too old\n", FUNC_NDEV_ARG(ndev)); + + if (adapter_to_rfctl(padapter)->offch_state == OFFCHS_NONE) + probe_req = 1; + + ret = -EINVAL; + goto release_plink_ctl; + } + + #if CONFIG_RTW_MESH_ACNODE_PREVENT + if (plink_ctl->acnode_rsvd) + acnode = rtw_mesh_scanned_is_acnode_confirmed(padapter, scanned); + #endif + + /* wpa_supplicant's auto peer will initiate peering when candidate peer is reported without max_peer_links consideration */ + if (plink_ctl->num >= mcfg->max_peer_links + acnode ? 1 : 0) { + RTW_INFO(FUNC_NDEV_FMT" exceed max_peer_links:%u%s\n" + , FUNC_NDEV_ARG(ndev), mcfg->max_peer_links, acnode ? " acn" : ""); + ret = -EINVAL; + goto release_plink_ctl; + } + + if (!rtw_bss_is_candidate_mesh_peer(padapter, &scanned->network, 1, 1)) { + RTW_WARN(FUNC_NDEV_FMT" corresponding network is not candidate with same ch\n" + , FUNC_NDEV_ARG(ndev)); + ret = -EINVAL; + goto release_plink_ctl; + } + + #if CONFIG_RTW_MESH_CTO_MGATE_BLACKLIST + if (!rtw_mesh_cto_mgate_network_filter(padapter, scanned)) { + RTW_INFO(FUNC_NDEV_FMT" peer filtered out by cto_mgate check\n" + , FUNC_NDEV_ARG(ndev)); + ret = -EINVAL; + goto release_plink_ctl; + } + #endif + + if (_rtw_mesh_plink_add(padapter, mac) == _SUCCESS) { + /* hook corresponding network in scan queue */ + plink = _rtw_mesh_plink_get(padapter, mac); + plink->aid = params->aid; + plink->scanned = scanned; + + #if CONFIG_RTW_MESH_ACNODE_PREVENT + if (acnode) { + RTW_INFO(FUNC_ADPT_FMT" acnode "MAC_FMT"\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(scanned->network.MacAddress)); + } + #endif + + add_new_sta = 1; + } else { + RTW_WARN(FUNC_NDEV_FMT" rtw_mesh_plink_add not success\n" + , FUNC_NDEV_ARG(ndev)); + ret = -EINVAL; + } +release_plink_ctl: + _exit_critical_bh(&(plink_ctl->lock), &irqL); + + if (probe_req) + issue_probereq(padapter, &padapter->mlmepriv.cur_network.network.mesh_id, mac); + + if (add_new_sta) { + struct station_info sinfo; + + #ifdef CONFIG_DFS_MASTER + if (IS_UNDER_CAC(adapter_to_rfctl(padapter))) + rtw_force_stop_cac(adapter_to_rfctl(padapter), 300); + #endif + + /* indicate new sta */ + _rtw_memset(&sinfo, 0, sizeof(sinfo)); + cfg80211_new_sta(ndev, mac, &sinfo, GFP_ATOMIC); + } + goto exit; + } +#endif /* CONFIG_RTW_MESH */ + +#ifdef CONFIG_TDLS + psta = rtw_get_stainfo(pstapriv, (u8 *)mac); + if (psta == NULL) { + psta = rtw_alloc_stainfo(pstapriv, (u8 *)mac); + if (psta == NULL) { + RTW_INFO("[%s] Alloc station for "MAC_FMT" fail\n", __FUNCTION__, MAC_ARG(mac)); + ret = -EOPNOTSUPP; + goto exit; + } + } +#endif /* CONFIG_TDLS */ + +exit: + return ret; +} + +static int cfg80211_rtw_del_station(struct wiphy *wiphy, struct net_device *ndev, +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 16, 0)) + u8 *mac +#elif (LINUX_VERSION_CODE < KERNEL_VERSION(3, 19, 0)) + const u8 *mac +#else + struct station_del_parameters *params +#endif +) +{ + int ret = 0; + _irqL irqL; + _list *phead, *plist; + u8 updated = _FALSE; + const u8 *target_mac; + struct sta_info *psta = NULL; + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct sta_priv *pstapriv = &padapter->stapriv; + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 19, 0)) + target_mac = mac; +#else + target_mac = params->mac; +#endif + + RTW_INFO("+"FUNC_NDEV_FMT" mac=%pM\n", FUNC_NDEV_ARG(ndev), target_mac); + + if (check_fwstate(pmlmepriv, (WIFI_ASOC_STATE | WIFI_AP_STATE | WIFI_MESH_STATE)) != _TRUE) { + RTW_INFO("%s, fw_state != FW_LINKED|WIFI_AP_STATE|WIFI_MESH_STATE\n", __func__); + return -EINVAL; + } + + + if (!target_mac) { + RTW_INFO("flush all sta, and cam_entry\n"); + + flush_all_cam_entry(padapter); /* clear CAM */ + +#ifdef CONFIG_AP_MODE + ret = rtw_sta_flush(padapter, _TRUE); +#endif + return ret; + } + + + RTW_INFO("free sta macaddr =" MAC_FMT "\n", MAC_ARG(target_mac)); + + if (target_mac[0] == 0xff && target_mac[1] == 0xff && + target_mac[2] == 0xff && target_mac[3] == 0xff && + target_mac[4] == 0xff && target_mac[5] == 0xff) + return -EINVAL; + + + _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL); + + phead = &pstapriv->asoc_list; + plist = get_next(phead); + + /* check asoc_queue */ + while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) { + psta = LIST_CONTAINOR(plist, struct sta_info, asoc_list); + + plist = get_next(plist); + + if (_rtw_memcmp((u8 *)target_mac, psta->cmn.mac_addr, ETH_ALEN)) { + if (psta->dot8021xalg == 1 && psta->bpairwise_key_installed == _FALSE) { + RTW_INFO("%s, sta's dot8021xalg = 1 and key_installed = _FALSE\n", __func__); + + #ifdef CONFIG_AP_MODE + if (MLME_IS_AP(padapter)) { + rtw_list_delete(&psta->asoc_list); + pstapriv->asoc_list_cnt--; + #ifdef CONFIG_RTW_TOKEN_BASED_XMIT + if (psta->tbtx_enable) + pstapriv->tbtx_asoc_list_cnt--; + #endif + STA_SET_MESH_PLINK(psta, NULL); + + ap_free_sta(padapter, psta, _TRUE, WLAN_REASON_IEEE_802_1X_AUTH_FAILED, _TRUE); + psta = NULL; + break; + } + #endif + } else { + RTW_INFO("free psta=%p, aid=%d\n", psta, psta->cmn.aid); + + rtw_list_delete(&psta->asoc_list); + pstapriv->asoc_list_cnt--; + #ifdef CONFIG_RTW_TOKEN_BASED_XMIT + if (psta->tbtx_enable) + pstapriv->tbtx_asoc_list_cnt--; + #endif + STA_SET_MESH_PLINK(psta, NULL); + + /* _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL); */ + if (MLME_IS_AP(padapter)) + updated = ap_free_sta(padapter, psta, _TRUE, WLAN_REASON_PREV_AUTH_NOT_VALID, _TRUE); + else + updated = ap_free_sta(padapter, psta, _TRUE, WLAN_REASON_DEAUTH_LEAVING, _TRUE); + /* _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL); */ + + psta = NULL; + + break; + } + + } + + } + + _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL); + + associated_clients_update(padapter, updated, STA_INFO_UPDATE_ALL); + +#ifdef CONFIG_RTW_MESH + if (MLME_IS_MESH(padapter)) + rtw_mesh_plink_del(padapter, target_mac); +#endif + + RTW_INFO("-"FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(ndev)); + + return ret; + +} + +static int cfg80211_rtw_change_station(struct wiphy *wiphy, struct net_device *ndev, +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 16, 0)) + u8 *mac, +#else + const u8 *mac, +#endif + struct station_parameters *params) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(ndev); + int ret = 0; + + RTW_INFO(FUNC_ADPT_FMT" mac:"MAC_FMT"\n", FUNC_ADPT_ARG(adapter), MAC_ARG(mac)); + + dump_station_parameters(RTW_DBGDUMP, wiphy, params); + +#ifdef CONFIG_RTW_MESH + if (MLME_IS_MESH(adapter)) { + enum cfg80211_station_type sta_type = CFG80211_STA_MESH_PEER_USER; + u8 plink_state = nl80211_plink_state_to_rtw_plink_state(params->plink_state); + + ret = cfg80211_check_station_change(wiphy, params, sta_type); + if (ret) { + RTW_INFO("cfg80211_check_station_change return %d\n", ret); + goto exit; + } + + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)) + if (!(params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)) + goto exit; + #endif + + if (rtw_mesh_set_plink_state_cmd(adapter, mac, plink_state) != _SUCCESS) + ret = -ENOENT; + } + +exit: +#endif /* CONFIG_RTW_MESH */ + + if (ret) + RTW_INFO(FUNC_ADPT_FMT" mac:"MAC_FMT" ret:%d\n", FUNC_ADPT_ARG(adapter), MAC_ARG(mac), ret); + return ret; +} + +struct sta_info *rtw_sta_info_get_by_idx(struct sta_priv *pstapriv, const int idx, u8 *asoc_list_num) +{ + _list *phead, *plist; + struct sta_info *psta = NULL; + int i = 0; + + phead = &pstapriv->asoc_list; + plist = get_next(phead); + + /* check asoc_queue */ + while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) { + if (idx == i) + psta = LIST_CONTAINOR(plist, struct sta_info, asoc_list); + plist = get_next(plist); + i++; + } + + if (asoc_list_num) + *asoc_list_num = i; + + return psta; +} + +static int cfg80211_rtw_dump_station(struct wiphy *wiphy, struct net_device *ndev, + int idx, u8 *mac, struct station_info *sinfo) +{ +#define DBG_DUMP_STATION 0 + + int ret = 0; + _irqL irqL; + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + struct sta_priv *pstapriv = &padapter->stapriv; + struct sta_info *psta = NULL; +#ifdef CONFIG_RTW_MESH + struct mesh_plink_ent *plink = NULL; +#endif + u8 asoc_list_num; + + if (DBG_DUMP_STATION) + RTW_INFO(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(ndev)); + + _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL); + psta = rtw_sta_info_get_by_idx(pstapriv, idx, &asoc_list_num); + _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL); + +#ifdef CONFIG_RTW_MESH + if (MLME_IS_MESH(padapter)) { + if (psta) + plink = psta->plink; + if (!plink) + plink = rtw_mesh_plink_get_no_estab_by_idx(padapter, idx - asoc_list_num); + } +#endif /* CONFIG_RTW_MESH */ + + if ((!MLME_IS_MESH(padapter) && !psta) + #ifdef CONFIG_RTW_MESH + || (MLME_IS_MESH(padapter) && !plink) + #endif + ) { + if (DBG_DUMP_STATION) + RTW_INFO(FUNC_NDEV_FMT" end with idx:%d\n", FUNC_NDEV_ARG(ndev), idx); + ret = -ENOENT; + goto exit; + } + + if (psta) + _rtw_memcpy(mac, psta->cmn.mac_addr, ETH_ALEN); + #ifdef CONFIG_RTW_MESH + else + _rtw_memcpy(mac, plink->addr, ETH_ALEN); + #endif + + sinfo->filled = 0; + + if (psta) { + sinfo->filled |= STATION_INFO_SIGNAL; + sinfo->signal = translate_percentage_to_dbm(psta->cmn.rssi_stat.rssi); + sinfo->filled |= STATION_INFO_INACTIVE_TIME; + sinfo->inactive_time = rtw_get_passing_time_ms(psta->sta_stats.last_rx_time); + } + +#ifdef CONFIG_RTW_MESH + if (MLME_IS_MESH(padapter)) + rtw_cfg80211_fill_mesh_only_sta_info(plink, psta, sinfo); +#endif + +exit: + return ret; +} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)) +static int cfg80211_rtw_change_bss(struct wiphy *wiphy, struct net_device *ndev, + struct bss_parameters *params) +{ + _adapter *adapter = rtw_netdev_priv(ndev); + + RTW_INFO(FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(adapter)); + +if (0) { + if (params->use_cts_prot != -1) + RTW_INFO("use_cts_prot=%d\n", params->use_cts_prot); + if (params->use_short_preamble != -1) + RTW_INFO("use_short_preamble=%d\n", params->use_short_preamble); + if (params->use_short_slot_time != -1) + RTW_INFO("use_short_slot_time=%d\n", params->use_short_slot_time); + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)) + if (params->basic_rates && params->basic_rates_len) { + RTW_INFO("basic_rates_len=%d\n", params->basic_rates_len); + RTW_INFO_DUMP("basic_rates=", params->basic_rates, params->basic_rates_len); + } +#endif +} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + if (params->ap_isolate != -1) { + RTW_INFO("ap_isolate=%d\n", params->ap_isolate); + adapter->mlmepriv.ap_isolate = params->ap_isolate ? 1 : 0; + } +#endif + +if (0) { +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)) + if (params->ht_opmode != -1) + RTW_INFO("ht_opmode=0x%04x\n", params->ht_opmode); +#endif +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + if (params->p2p_ctwindow != -1) + RTW_INFO("p2p_ctwindow=%d\n", params->p2p_ctwindow); + if (params->p2p_opp_ps != -1) + RTW_INFO("p2p_opp_ps=%d\n", params->p2p_opp_ps); +#endif +} + return 0; +} +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)) */ + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)) +static int cfg80211_rtw_set_txq_params(struct wiphy *wiphy +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)) + , struct net_device *ndev +#endif + , struct ieee80211_txq_params *params) +{ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)) + _adapter *padapter = rtw_netdev_priv(ndev); +#else + _adapter *padapter = wiphy_to_adapter(wiphy); +#endif + struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv; + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + u8 ac, AIFS, ECWMin, ECWMax, aSifsTime; + u16 TXOP; + u8 shift_count = 0; + u32 acParm; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)) + ac = params->ac; +#else + ac = params->queue; +#endif + + switch (ac) { + case NL80211_TXQ_Q_VO: + ac = XMIT_VO_QUEUE; + break; + + case NL80211_TXQ_Q_VI: + ac = XMIT_VI_QUEUE; + break; + + case NL80211_TXQ_Q_BE: + ac = XMIT_BE_QUEUE; + break; + + case NL80211_TXQ_Q_BK: + ac = XMIT_BK_QUEUE; + break; + + default: + break; + } + +#if 0 + RTW_INFO("ac=%d\n", ac); + RTW_INFO("txop=%u\n", params->txop); + RTW_INFO("cwmin=%u\n", params->cwmin); + RTW_INFO("cwmax=%u\n", params->cwmax); + RTW_INFO("aifs=%u\n", params->aifs); +#endif + + if (is_supported_5g(pmlmeext->cur_wireless_mode) || + (pmlmeext->cur_wireless_mode & WIRELESS_11_24N)) + aSifsTime = 16; + else + aSifsTime = 10; + + AIFS = params->aifs * pmlmeinfo->slotTime + aSifsTime; + + while ((params->cwmin + 1) >> shift_count != 1) { + shift_count++; + if (shift_count == 15) + break; + } + + ECWMin = shift_count; + + shift_count = 0; + while ((params->cwmax + 1) >> shift_count != 1) { + shift_count++; + if (shift_count == 15) + break; + } + + ECWMax = shift_count; + + TXOP = le16_to_cpu(params->txop); + + acParm = AIFS | (ECWMin << 8) | (ECWMax << 12) | (TXOP << 16); + + set_txq_params_cmd(padapter, acParm, ac); + + return 0; +} +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)) */ + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0)) +static int cfg80211_rtw_set_channel(struct wiphy *wiphy + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + , struct net_device *ndev + #endif + , struct ieee80211_channel *chan, enum nl80211_channel_type channel_type) +{ +#ifdef CONFIG_WIFI_MONITOR + _adapter *padapter = wiphy_to_adapter(wiphy); +#else /* CONFIG_WIFI_MONITOR */ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); +#else + _adapter *padapter = wiphy_to_adapter(wiphy); +#endif +#endif /* CONFIG_WIFI_MONITOR */ + int chan_target = (u8) ieee80211_frequency_to_channel(chan->center_freq); + int chan_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + int chan_width = CHANNEL_WIDTH_20; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + RTW_INFO(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(ndev)); +#endif + + switch (channel_type) { + case NL80211_CHAN_NO_HT: + case NL80211_CHAN_HT20: + chan_width = CHANNEL_WIDTH_20; + chan_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + break; + case NL80211_CHAN_HT40MINUS: + chan_width = CHANNEL_WIDTH_40; + chan_offset = HAL_PRIME_CHNL_OFFSET_UPPER; + break; + case NL80211_CHAN_HT40PLUS: + chan_width = CHANNEL_WIDTH_40; + chan_offset = HAL_PRIME_CHNL_OFFSET_LOWER; + break; + default: + chan_width = CHANNEL_WIDTH_20; + chan_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + break; + } + + RTW_INFO(FUNC_ADPT_FMT" ch:%d bw:%d, offset:%d\n" + , FUNC_ADPT_ARG(padapter), chan_target, chan_width, chan_offset); + + rtw_set_chbw_cmd(padapter, chan_target, chan_width, chan_offset, RTW_CMDF_WAIT_ACK); + + return 0; +} +#endif /*#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0))*/ + +/* +static int cfg80211_rtw_auth(struct wiphy *wiphy, struct net_device *ndev, + struct cfg80211_auth_request *req) +{ + RTW_INFO(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(ndev)); + + return 0; +} + +static int cfg80211_rtw_assoc(struct wiphy *wiphy, struct net_device *ndev, + struct cfg80211_assoc_request *req) +{ + RTW_INFO(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(ndev)); + + return 0; +} +*/ + +static int rtw_cfg80211_set_beacon_wpsp2pie(struct net_device *ndev, char *buf, int len) +{ + int ret = 0; + uint wps_ielen = 0; + u8 *wps_ie; + u32 p2p_ielen = 0; + u8 wps_oui[8] = {0x0, 0x50, 0xf2, 0x04}; + u8 *p2p_ie; + u32 wfd_ielen = 0; + u8 *wfd_ie; + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + + RTW_INFO(FUNC_NDEV_FMT" ielen=%d\n", FUNC_NDEV_ARG(ndev), len); + + if (len > 0) { + wps_ie = rtw_get_wps_ie(buf, len, NULL, &wps_ielen); + if (wps_ie) { + #ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("bcn_wps_ielen=%d\n", wps_ielen); + #endif + + if (pmlmepriv->wps_beacon_ie) { + u32 free_len = pmlmepriv->wps_beacon_ie_len; + pmlmepriv->wps_beacon_ie_len = 0; + rtw_mfree(pmlmepriv->wps_beacon_ie, free_len); + pmlmepriv->wps_beacon_ie = NULL; + } + + pmlmepriv->wps_beacon_ie = rtw_malloc(wps_ielen); + if (pmlmepriv->wps_beacon_ie == NULL) { + RTW_INFO("%s()-%d: rtw_malloc() ERROR!\n", __FUNCTION__, __LINE__); + return -EINVAL; + + } + + _rtw_memcpy(pmlmepriv->wps_beacon_ie, wps_ie, wps_ielen); + pmlmepriv->wps_beacon_ie_len = wps_ielen; + + update_beacon(padapter, _VENDOR_SPECIFIC_IE_, wps_oui, _TRUE, RTW_CMDF_WAIT_ACK); + + } + + /* buf += wps_ielen; */ + /* len -= wps_ielen; */ + + #ifdef CONFIG_P2P + p2p_ie = rtw_get_p2p_ie(buf, len, NULL, &p2p_ielen); + if (p2p_ie) { + #ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("bcn_p2p_ielen=%d\n", p2p_ielen); + #endif + + if (pmlmepriv->p2p_beacon_ie) { + u32 free_len = pmlmepriv->p2p_beacon_ie_len; + pmlmepriv->p2p_beacon_ie_len = 0; + rtw_mfree(pmlmepriv->p2p_beacon_ie, free_len); + pmlmepriv->p2p_beacon_ie = NULL; + } + + pmlmepriv->p2p_beacon_ie = rtw_malloc(p2p_ielen); + if (pmlmepriv->p2p_beacon_ie == NULL) { + RTW_INFO("%s()-%d: rtw_malloc() ERROR!\n", __FUNCTION__, __LINE__); + return -EINVAL; + + } + + _rtw_memcpy(pmlmepriv->p2p_beacon_ie, p2p_ie, p2p_ielen); + pmlmepriv->p2p_beacon_ie_len = p2p_ielen; + + } + #endif /* CONFIG_P2P */ + + + #ifdef CONFIG_WFD + wfd_ie = rtw_get_wfd_ie(buf, len, NULL, &wfd_ielen); + if (wfd_ie) { + #ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("bcn_wfd_ielen=%d\n", wfd_ielen); + #endif + + if (rtw_mlme_update_wfd_ie_data(pmlmepriv, MLME_BEACON_IE, wfd_ie, wfd_ielen) != _SUCCESS) + return -EINVAL; + } + #endif /* CONFIG_WFD */ + + pmlmeext->bstart_bss = _TRUE; + + } + + return ret; + +} + +static int rtw_cfg80211_set_probe_resp_wpsp2pie(struct net_device *net, char *buf, int len) +{ + int ret = 0; + uint wps_ielen = 0; + u8 *wps_ie; + u32 p2p_ielen = 0; + u8 *p2p_ie; + u32 wfd_ielen = 0; + u8 *wfd_ie; + _adapter *padapter = (_adapter *)rtw_netdev_priv(net); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + +#ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("%s, ielen=%d\n", __func__, len); +#endif + + if (len > 0) { + wps_ie = rtw_get_wps_ie(buf, len, NULL, &wps_ielen); + if (wps_ie) { + uint attr_contentlen = 0; + u16 uconfig_method, *puconfig_method = NULL; + + #ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("probe_resp_wps_ielen=%d\n", wps_ielen); + #endif + + if (check_fwstate(pmlmepriv, WIFI_UNDER_WPS)) { + u8 sr = 0; + rtw_get_wps_attr_content(wps_ie, wps_ielen, WPS_ATTR_SELECTED_REGISTRAR, (u8 *)(&sr), NULL); + + if (sr != 0) + RTW_INFO("%s, got sr\n", __func__); + else { + RTW_INFO("GO mode process WPS under site-survey, sr no set\n"); + return ret; + } + } + + if (pmlmepriv->wps_probe_resp_ie) { + u32 free_len = pmlmepriv->wps_probe_resp_ie_len; + pmlmepriv->wps_probe_resp_ie_len = 0; + rtw_mfree(pmlmepriv->wps_probe_resp_ie, free_len); + pmlmepriv->wps_probe_resp_ie = NULL; + } + + pmlmepriv->wps_probe_resp_ie = rtw_malloc(wps_ielen); + if (pmlmepriv->wps_probe_resp_ie == NULL) { + RTW_INFO("%s()-%d: rtw_malloc() ERROR!\n", __FUNCTION__, __LINE__); + return -EINVAL; + + } + + /* add PUSH_BUTTON config_method by driver self in wpsie of probe_resp at GO Mode */ + puconfig_method = (u16 *)rtw_get_wps_attr_content(wps_ie, wps_ielen, WPS_ATTR_CONF_METHOD , NULL, &attr_contentlen); + if (puconfig_method != NULL) { + /* struct registry_priv *pregistrypriv = &padapter->registrypriv; */ + struct wireless_dev *wdev = padapter->rtw_wdev; + + #ifdef CONFIG_DEBUG_CFG80211 + /* printk("config_method in wpsie of probe_resp = 0x%x\n", be16_to_cpu(*puconfig_method)); */ + #endif + + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + /* for WIFI-DIRECT LOGO 4.2.2, AUTO GO can't set PUSH_BUTTON flags */ + if (wdev->iftype == NL80211_IFTYPE_P2P_GO) { + uconfig_method = WPS_CM_PUSH_BUTTON; + uconfig_method = cpu_to_be16(uconfig_method); + + *puconfig_method &= ~uconfig_method; + } + #endif + } + + _rtw_memcpy(pmlmepriv->wps_probe_resp_ie, wps_ie, wps_ielen); + pmlmepriv->wps_probe_resp_ie_len = wps_ielen; + + } + + /* buf += wps_ielen; */ + /* len -= wps_ielen; */ + + #ifdef CONFIG_P2P + p2p_ie = rtw_get_p2p_ie(buf, len, NULL, &p2p_ielen); + if (p2p_ie) { + u8 is_GO = _FALSE; + u32 attr_contentlen = 0; + u16 cap_attr = 0; + + #ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("probe_resp_p2p_ielen=%d\n", p2p_ielen); + #endif + + /* Check P2P Capability ATTR */ + if (rtw_get_p2p_attr_content(p2p_ie, p2p_ielen, P2P_ATTR_CAPABILITY, (u8 *)&cap_attr, (uint *) &attr_contentlen)) { + u8 grp_cap = 0; + /* RTW_INFO( "[%s] Got P2P Capability Attr!!\n", __FUNCTION__ ); */ + cap_attr = le16_to_cpu(cap_attr); + grp_cap = (u8)((cap_attr >> 8) & 0xff); + + is_GO = (grp_cap & BIT(0)) ? _TRUE : _FALSE; + + if (is_GO) + RTW_INFO("Got P2P Capability Attr, grp_cap=0x%x, is_GO\n", grp_cap); + } + + + if (is_GO == _FALSE) { + if (pmlmepriv->p2p_probe_resp_ie) { + u32 free_len = pmlmepriv->p2p_probe_resp_ie_len; + pmlmepriv->p2p_probe_resp_ie_len = 0; + rtw_mfree(pmlmepriv->p2p_probe_resp_ie, free_len); + pmlmepriv->p2p_probe_resp_ie = NULL; + } + + pmlmepriv->p2p_probe_resp_ie = rtw_malloc(p2p_ielen); + if (pmlmepriv->p2p_probe_resp_ie == NULL) { + RTW_INFO("%s()-%d: rtw_malloc() ERROR!\n", __FUNCTION__, __LINE__); + return -EINVAL; + + } + _rtw_memcpy(pmlmepriv->p2p_probe_resp_ie, p2p_ie, p2p_ielen); + pmlmepriv->p2p_probe_resp_ie_len = p2p_ielen; + } else { + if (pmlmepriv->p2p_go_probe_resp_ie) { + u32 free_len = pmlmepriv->p2p_go_probe_resp_ie_len; + pmlmepriv->p2p_go_probe_resp_ie_len = 0; + rtw_mfree(pmlmepriv->p2p_go_probe_resp_ie, free_len); + pmlmepriv->p2p_go_probe_resp_ie = NULL; + } + + pmlmepriv->p2p_go_probe_resp_ie = rtw_malloc(p2p_ielen); + if (pmlmepriv->p2p_go_probe_resp_ie == NULL) { + RTW_INFO("%s()-%d: rtw_malloc() ERROR!\n", __FUNCTION__, __LINE__); + return -EINVAL; + + } + _rtw_memcpy(pmlmepriv->p2p_go_probe_resp_ie, p2p_ie, p2p_ielen); + pmlmepriv->p2p_go_probe_resp_ie_len = p2p_ielen; + } + + } + #endif /* CONFIG_P2P */ + + + #ifdef CONFIG_WFD + wfd_ie = rtw_get_wfd_ie(buf, len, NULL, &wfd_ielen); + #ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("probe_resp_wfd_ielen=%d\n", wfd_ielen); + #endif + + if (rtw_mlme_update_wfd_ie_data(pmlmepriv, MLME_PROBE_RESP_IE, wfd_ie, wfd_ielen) != _SUCCESS) + return -EINVAL; + #endif /* CONFIG_WFD */ + + } + + return ret; + +} + +static int rtw_cfg80211_set_assoc_resp_wpsp2pie(struct net_device *net, char *buf, int len) +{ + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(net); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + u8 *ie; + u32 ie_len; + + RTW_INFO("%s, ielen=%d\n", __func__, len); + + if (len <= 0) + goto exit; + + ie = rtw_get_wps_ie(buf, len, NULL, &ie_len); + if (ie && ie_len) { + if (pmlmepriv->wps_assoc_resp_ie) { + u32 free_len = pmlmepriv->wps_assoc_resp_ie_len; + + pmlmepriv->wps_assoc_resp_ie_len = 0; + rtw_mfree(pmlmepriv->wps_assoc_resp_ie, free_len); + pmlmepriv->wps_assoc_resp_ie = NULL; + } + + pmlmepriv->wps_assoc_resp_ie = rtw_malloc(ie_len); + if (pmlmepriv->wps_assoc_resp_ie == NULL) { + RTW_INFO("%s()-%d: rtw_malloc() ERROR!\n", __FUNCTION__, __LINE__); + return -EINVAL; + } + _rtw_memcpy(pmlmepriv->wps_assoc_resp_ie, ie, ie_len); + pmlmepriv->wps_assoc_resp_ie_len = ie_len; + } +#ifdef CONFIG_P2P + ie = rtw_get_p2p_ie(buf, len, NULL, &ie_len); + if (ie && ie_len) { + if (pmlmepriv->p2p_assoc_resp_ie) { + u32 free_len = pmlmepriv->p2p_assoc_resp_ie_len; + + pmlmepriv->p2p_assoc_resp_ie_len = 0; + rtw_mfree(pmlmepriv->p2p_assoc_resp_ie, free_len); + pmlmepriv->p2p_assoc_resp_ie = NULL; + } + + pmlmepriv->p2p_assoc_resp_ie = rtw_malloc(ie_len); + if (pmlmepriv->p2p_assoc_resp_ie == NULL) { + RTW_INFO("%s()-%d: rtw_malloc() ERROR!\n", __FUNCTION__, __LINE__); + return -EINVAL; + } + _rtw_memcpy(pmlmepriv->p2p_assoc_resp_ie, ie, ie_len); + pmlmepriv->p2p_assoc_resp_ie_len = ie_len; + } +#endif +#ifdef CONFIG_WFD + ie = rtw_get_wfd_ie(buf, len, NULL, &ie_len); + if (rtw_mlme_update_wfd_ie_data(pmlmepriv, MLME_ASSOC_RESP_IE, ie, ie_len) != _SUCCESS) + return -EINVAL; +#endif + +exit: + return ret; +} + +int rtw_cfg80211_set_mgnt_wpsp2pie(struct net_device *net, char *buf, int len, + int type) +{ + int ret = 0; + uint wps_ielen = 0; + u32 p2p_ielen = 0; + +#ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("%s, ielen=%d\n", __func__, len); +#endif + + if ((rtw_get_wps_ie(buf, len, NULL, &wps_ielen) && (wps_ielen > 0)) + #ifdef CONFIG_P2P + || (rtw_get_p2p_ie(buf, len, NULL, &p2p_ielen) && (p2p_ielen > 0)) + #endif + ) { + if (net != NULL) { + switch (type) { + case 0x1: /* BEACON */ + ret = rtw_cfg80211_set_beacon_wpsp2pie(net, buf, len); + break; + case 0x2: /* PROBE_RESP */ + ret = rtw_cfg80211_set_probe_resp_wpsp2pie(net, buf, len); + #ifdef CONFIG_P2P + if (ret == 0) + adapter_wdev_data((_adapter *)rtw_netdev_priv(net))->probe_resp_ie_update_time = rtw_get_current_time(); + #endif + break; + case 0x4: /* ASSOC_RESP */ + ret = rtw_cfg80211_set_assoc_resp_wpsp2pie(net, buf, len); + break; + } + } + } + + return ret; + +} +#endif /* CONFIG_AP_MODE */ + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) +static struct wireless_dev * +#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)) || defined(COMPAT_KERNEL_RELEASE) +static struct net_device * +#else +static int +#endif + cfg80211_rtw_add_virtual_intf( + struct wiphy *wiphy, + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)) + const char *name, + #else + char *name, + #endif + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)) + unsigned char name_assign_type, + #endif + enum nl80211_iftype type, + #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0)) + u32 *flags, + #endif + struct vif_params *params) +{ + int ret = 0; + struct wireless_dev *wdev = NULL; + struct net_device *ndev = NULL; + _adapter *padapter; + struct dvobj_priv *dvobj = wiphy_to_dvobj(wiphy); + + rtw_set_rtnl_lock_holder(dvobj, current); + + RTW_INFO(FUNC_WIPHY_FMT" name:%s, type:%d\n", FUNC_WIPHY_ARG(wiphy), name, type); + + switch (type) { + case NL80211_IFTYPE_MONITOR: + padapter = wiphy_to_adapter(wiphy); /* TODO: get ap iface ? */ + ret = rtw_cfg80211_add_monitor_if(padapter, (char *)name, &ndev); + if (ret == 0) + wdev = ndev->ieee80211_ptr; + break; + +#if defined(CONFIG_P2P) && ((LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE)) + case NL80211_IFTYPE_P2P_CLIENT: + case NL80211_IFTYPE_P2P_GO: +#endif + case NL80211_IFTYPE_STATION: + case NL80211_IFTYPE_AP: +#ifdef CONFIG_RTW_MESH + case NL80211_IFTYPE_MESH_POINT: +#endif + padapter = dvobj_get_unregisterd_adapter(dvobj); + if (!padapter) { + RTW_WARN("adapter pool empty!\n"); + ret = -ENODEV; + break; + } + + #if defined(CONFIG_CONCURRENT_MODE) && defined(CONFIG_P2P) + #if defined(CONFIG_P2P) && ((KERNEL_VERSION(2, 6, 37) <= LINUX_VERSION_CODE) || defined(COMPAT_KERNEL_RELEASE)) + if ((type == NL80211_IFTYPE_P2P_CLIENT || type == NL80211_IFTYPE_P2P_GO) && (padapter->iface_id != padapter->registrypriv.sel_p2p_iface)) { + RTW_ERR("%s, iface_id:%d is not P2P interface!\n", __func__, padapter->iface_id); + ret = -EOPNOTSUPP; + break; + } + #endif + #endif + + if (rtw_os_ndev_init(padapter, name) != _SUCCESS) { + RTW_WARN("ndev init fail!\n"); + ret = -ENODEV; + break; + } + #if defined(CONFIG_P2P) && ((LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE)) + if (type == NL80211_IFTYPE_P2P_CLIENT || type == NL80211_IFTYPE_P2P_GO) + rtw_p2p_enable(padapter, P2P_ROLE_DEVICE); + #endif + ndev = padapter->pnetdev; + wdev = ndev->ieee80211_ptr; + break; + +#if defined(CONFIG_P2P) && defined(RTW_DEDICATED_P2P_DEVICE) + case NL80211_IFTYPE_P2P_DEVICE: + ret = rtw_pd_iface_alloc(wiphy, name, &wdev); + break; +#endif + + case NL80211_IFTYPE_ADHOC: + case NL80211_IFTYPE_AP_VLAN: + case NL80211_IFTYPE_WDS: + default: + ret = -ENODEV; + RTW_INFO("Unsupported interface type\n"); + break; + } + + if (ndev) + RTW_INFO(FUNC_WIPHY_FMT" ndev:%p, ret:%d\n", FUNC_WIPHY_ARG(wiphy), ndev, ret); + else + RTW_INFO(FUNC_WIPHY_FMT" wdev:%p, ret:%d\n", FUNC_WIPHY_ARG(wiphy), wdev, ret); + + rtw_set_rtnl_lock_holder(dvobj, NULL); + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + return wdev ? wdev : ERR_PTR(ret); +#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)) || defined(COMPAT_KERNEL_RELEASE) + return ndev ? ndev : ERR_PTR(ret); +#else + return ret; +#endif +} + +static int cfg80211_rtw_del_virtual_intf(struct wiphy *wiphy, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + struct wireless_dev *wdev +#else + struct net_device *ndev +#endif +) +{ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + struct net_device *ndev = wdev_to_ndev(wdev); +#endif + int ret = 0; + struct dvobj_priv *dvobj = wiphy_to_dvobj(wiphy); + _adapter *adapter; + struct rtw_wdev_priv *pwdev_priv; + + rtw_set_rtnl_lock_holder(dvobj, current); + + if (ndev) { + adapter = (_adapter *)rtw_netdev_priv(ndev); + pwdev_priv = adapter_wdev_data(adapter); + + if (ndev == pwdev_priv->pmon_ndev) { + unregister_netdevice(ndev); + pwdev_priv->pmon_ndev = NULL; + pwdev_priv->ifname_mon[0] = '\0'; + RTW_INFO(FUNC_NDEV_FMT" remove monitor ndev\n", FUNC_NDEV_ARG(ndev)); + } else { + RTW_INFO(FUNC_NDEV_FMT" unregister ndev\n", FUNC_NDEV_ARG(ndev)); + rtw_os_ndev_unregister(adapter); + } + } else +#if defined(CONFIG_P2P) && defined(RTW_DEDICATED_P2P_DEVICE) + if (wdev->iftype == NL80211_IFTYPE_P2P_DEVICE) { + if (wdev == wiphy_to_pd_wdev(wiphy)) + rtw_pd_iface_free(wiphy); + else { + RTW_ERR(FUNC_WIPHY_FMT" unknown P2P Device wdev:%p\n", FUNC_WIPHY_ARG(wiphy), wdev); + rtw_warn_on(1); + } + } else +#endif + { + ret = -EINVAL; + goto exit; + } + +exit: + rtw_set_rtnl_lock_holder(dvobj, NULL); + return ret; +} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) +static int cfg80211_rtw_get_channel(struct wiphy *wiphy, + struct wireless_dev *wdev, + struct cfg80211_chan_def *chandef) +{ + _adapter *padapter = wiphy_to_adapter(wiphy); + struct mlme_ext_priv *mlmeext = &(padapter->mlmeextpriv); + u8 ht_option = 0; + u8 report = 0; + int retval = 1; + + if (MLME_IS_ASOC(padapter)) { +#ifdef CONFIG_80211N_HT + ht_option = padapter->mlmepriv.htpriv.ht_option; +#endif /* CONFIG_80211N_HT */ + report = 1; + } else if (MLME_IS_MONITOR(padapter)) { + /* monitor mode always set to HT + we don't support sniffer No HT */ + ht_option = 1; + report = 1; + } + + if (report) { + rtw_chbw_to_cfg80211_chan_def(wiphy, chandef, + mlmeext->cur_channel, mlmeext->cur_bwmode, + mlmeext->cur_ch_offset, ht_option); + retval = 0; + } + + return retval; +} + +static void rtw_get_chbwoff_from_cfg80211_chan_def( + struct cfg80211_chan_def *chandef, + u8 *ht, u8 *ch, u8 *bw, u8 *offset) +{ + struct ieee80211_channel *chan = chandef->chan; + + *ch = chan->hw_value; + *ht = 1; + + switch (chandef->width) { + case NL80211_CHAN_WIDTH_20_NOHT: + *ht = 0; + /* fall through */ + case NL80211_CHAN_WIDTH_20: + *bw = CHANNEL_WIDTH_20; + *offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + break; + case NL80211_CHAN_WIDTH_40: + *bw = CHANNEL_WIDTH_40; + *offset = (chandef->center_freq1 > chan->center_freq) ? + HAL_PRIME_CHNL_OFFSET_LOWER : HAL_PRIME_CHNL_OFFSET_UPPER; + break; + case NL80211_CHAN_WIDTH_80: + *bw = CHANNEL_WIDTH_80; + *offset = (chandef->center_freq1 > chan->center_freq) ? + HAL_PRIME_CHNL_OFFSET_LOWER : HAL_PRIME_CHNL_OFFSET_UPPER; + break; + case NL80211_CHAN_WIDTH_160: + *bw = CHANNEL_WIDTH_160; + *offset = (chandef->center_freq1 > chan->center_freq) ? + HAL_PRIME_CHNL_OFFSET_LOWER : HAL_PRIME_CHNL_OFFSET_UPPER; + break; + case NL80211_CHAN_WIDTH_80P80: + *bw = CHANNEL_WIDTH_80_80; + *offset = (chandef->center_freq1 > chan->center_freq) ? + HAL_PRIME_CHNL_OFFSET_LOWER : HAL_PRIME_CHNL_OFFSET_UPPER; + break; +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) + case NL80211_CHAN_WIDTH_5: + *bw = CHANNEL_WIDTH_5; + *offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + break; + case NL80211_CHAN_WIDTH_10: + *bw = CHANNEL_WIDTH_10; + *offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + break; +#endif + default: + *ht = 0; + *bw = CHANNEL_WIDTH_20; + *offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + RTW_INFO("unsupported cwidth:%u\n", chandef->width); + rtw_warn_on(1); + }; +} +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) */ + +static int cfg80211_rtw_set_monitor_channel(struct wiphy *wiphy +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + , struct cfg80211_chan_def *chandef +#else + , struct ieee80211_channel *chan + , enum nl80211_channel_type channel_type +#endif + ) +{ + _adapter *padapter = wiphy_to_adapter(wiphy); + u8 target_channal, target_offset, target_width, ht_option; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) +#ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("center_freq %u Mhz ch %u width %u freq1 %u freq2 %u\n" + , chandef->chan->center_freq + , chandef->chan->hw_value + , chandef->width + , chandef->center_freq1 + , chandef->center_freq2); +#endif /* CONFIG_DEBUG_CFG80211 */ + + rtw_get_chbwoff_from_cfg80211_chan_def(chandef, + &ht_option, &target_channal, &target_width, &target_offset); +#else +#ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("center_freq %u Mhz ch %u channel_type %u\n" + , chan->center_freq + , chan->hw_value + , channel_type); +#endif /* CONFIG_DEBUG_CFG80211 */ + + rtw_get_chbw_from_nl80211_channel_type(chan, channel_type, + &ht_option, &target_channal, &target_width, &target_offset); +#endif + RTW_INFO(FUNC_ADPT_FMT" ch:%d bw:%d, offset:%d\n", + FUNC_ADPT_ARG(padapter), target_channal, + target_width, target_offset); + + rtw_set_chbw_cmd(padapter, target_channal, target_width, + target_offset, RTW_CMDF_WAIT_ACK); + + return 0; +} + +void rtw_cfg80211_external_auth_request(_adapter *padapter, union recv_frame *rframe) +{ + struct rtw_external_auth_params params; + struct wireless_dev *wdev = padapter->rtw_wdev; + struct net_device *netdev = wdev_to_ndev(wdev); + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + + u8 frame[256] = { 0 }; + uint frame_len = 24; + s32 freq = 0; + + /* rframe, in this case is null point */ + + freq = rtw_ch2freq(pmlmeext->cur_channel); + +#if (KERNEL_VERSION(4, 17, 0) <= LINUX_VERSION_CODE) \ + || defined(CONFIG_KERNEL_PATCH_EXTERNAL_AUTH) + params.action = EXTERNAL_AUTH_START; + _rtw_memcpy(params.bssid, get_my_bssid(&pmlmeinfo->network), ETH_ALEN); + params.ssid.ssid_len = pmlmeinfo->network.Ssid.SsidLength; + _rtw_memcpy(params.ssid.ssid, pmlmeinfo->network.Ssid.Ssid, + pmlmeinfo->network.Ssid.SsidLength); + params.key_mgmt_suite = 0x8ac0f00; + + cfg80211_external_auth_request(netdev, + (struct cfg80211_external_auth_params *)¶ms, GFP_ATOMIC); +#elif (KERNEL_VERSION(2, 6, 37) <= LINUX_VERSION_CODE) + set_frame_sub_type(frame, WIFI_AUTH); + + _rtw_memcpy(frame + 4, get_my_bssid(&pmlmeinfo->network), ETH_ALEN); + _rtw_memcpy(frame + 10, adapter_mac_addr(padapter), ETH_ALEN); + _rtw_memcpy(frame + 16, get_my_bssid(&pmlmeinfo->network), ETH_ALEN); + RTW_PUT_LE32((frame + 18), 0x8ac0f00); + RTW_PUT_LE32((frame + 24), 0x0003); + + if (pmlmeinfo->network.Ssid.SsidLength) { + *(frame + 26) = pmlmeinfo->network.Ssid.SsidLength; + _rtw_memcpy(frame + 27, pmlmeinfo->network.Ssid.Ssid, + pmlmeinfo->network.Ssid.SsidLength); + frame_len = 27 + pmlmeinfo->network.Ssid.SsidLength; + } + rtw_cfg80211_rx_mgmt(wdev, freq, 0, frame, frame_len, GFP_ATOMIC); +#endif +} + +void rtw_cfg80211_rx_probe_request(_adapter *adapter, union recv_frame *rframe) +{ + struct wireless_dev *wdev = adapter->rtw_wdev; + u8 *frame = get_recvframe_data(rframe); + uint frame_len = rframe->u.hdr.len; + s32 freq; + u8 ch, sch = rtw_get_oper_ch(adapter); + + ch = rframe->u.hdr.attrib.ch ? rframe->u.hdr.attrib.ch : sch; + freq = rtw_ch2freq(ch); + +#ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("RTW_Rx: probe request, ch=%d(%d), ta="MAC_FMT"\n" + , ch, sch, MAC_ARG(get_addr2_ptr(frame))); +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)) || defined(COMPAT_KERNEL_RELEASE) + rtw_cfg80211_rx_mgmt(wdev, freq, 0, frame, frame_len, GFP_ATOMIC); +#else + cfg80211_rx_action(adapter->pnetdev, freq, frame, frame_len, GFP_ATOMIC); +#endif +} + +void rtw_cfg80211_rx_action_p2p(_adapter *adapter, union recv_frame *rframe) +{ + struct wireless_dev *wdev = adapter->rtw_wdev; + u8 *frame = get_recvframe_data(rframe); + uint frame_len = rframe->u.hdr.len; + s32 freq; + u8 ch, sch = rtw_get_oper_ch(adapter); + u8 category, action; + int type; + + ch = rframe->u.hdr.attrib.ch ? rframe->u.hdr.attrib.ch : sch; + freq = rtw_ch2freq(ch); + + RTW_INFO("RTW_Rx:ch=%d(%d), ta="MAC_FMT"\n" + , ch, sch, MAC_ARG(get_addr2_ptr(frame))); +#ifdef CONFIG_P2P + type = rtw_p2p_check_frames(adapter, frame, frame_len, _FALSE); + if (type >= 0) + goto indicate; +#endif + rtw_action_frame_parse(frame, frame_len, &category, &action); + RTW_INFO("RTW_Rx:category(%u), action(%u)\n", category, action); +#ifdef CONFIG_P2P +indicate: +#endif +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + rtw_cfg80211_rx_mgmt(wdev, freq, 0, frame, frame_len, GFP_ATOMIC); +#else + cfg80211_rx_action(adapter->pnetdev, freq, frame, frame_len, GFP_ATOMIC); +#endif +} + +void rtw_cfg80211_rx_p2p_action_public(_adapter *adapter, union recv_frame *rframe) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct wireless_dev *wdev = adapter->rtw_wdev; + struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(adapter); + u8 *frame = get_recvframe_data(rframe); + uint frame_len = rframe->u.hdr.len; + s32 freq; + u8 ch, sch = rtw_get_oper_ch(adapter); + u8 category, action; + int type; + + ch = rframe->u.hdr.attrib.ch ? rframe->u.hdr.attrib.ch : sch; + freq = rtw_ch2freq(ch); + + RTW_INFO("RTW_Rx:ch=%d(%d), ta="MAC_FMT"\n" + , ch, sch, MAC_ARG(get_addr2_ptr(frame))); + #ifdef CONFIG_P2P + type = rtw_p2p_check_frames(adapter, frame, frame_len, _FALSE); + if (type >= 0) { + switch (type) { + case P2P_GO_NEGO_CONF: + if (0) { + RTW_INFO(FUNC_ADPT_FMT" Nego confirm. state=%u, status=%u, iaddr="MAC_FMT"\n" + , FUNC_ADPT_ARG(adapter), pwdev_priv->nego_info.state, pwdev_priv->nego_info.status + , MAC_ARG(pwdev_priv->nego_info.iface_addr)); + } + if (pwdev_priv->nego_info.state == 2 + && pwdev_priv->nego_info.status == 0 + && rtw_check_invalid_mac_address(pwdev_priv->nego_info.iface_addr, _FALSE) == _FALSE + ) { + _adapter *intended_iface = dvobj_get_adapter_by_addr(dvobj, pwdev_priv->nego_info.iface_addr); + + if (intended_iface) { + RTW_INFO(FUNC_ADPT_FMT" Nego confirm. Allow only "ADPT_FMT" to scan for 2000 ms\n" + , FUNC_ADPT_ARG(adapter), ADPT_ARG(intended_iface)); + /* allow only intended_iface to do scan for 2000 ms */ + rtw_mi_set_scan_deny(adapter, 2000); + rtw_clear_scan_deny(intended_iface); + } + } + break; + case P2P_PROVISION_DISC_RESP: + case P2P_INVIT_RESP: + rtw_clear_scan_deny(adapter); + #if !RTW_P2P_GROUP_INTERFACE + rtw_mi_buddy_set_scan_deny(adapter, 2000); + #endif + break; + } + goto indicate; + } + #endif + rtw_action_frame_parse(frame, frame_len, &category, &action); + RTW_INFO("RTW_Rx:category(%u), action(%u)\n", category, action); +#ifdef CONFIG_P2P +indicate: +#endif + #if defined(RTW_DEDICATED_P2P_DEVICE) + if (rtw_cfg80211_redirect_pd_wdev(dvobj_to_wiphy(dvobj), get_ra(frame), &wdev)) + if (0) + RTW_INFO("redirect to pd_wdev:%p\n", wdev); + #endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + rtw_cfg80211_rx_mgmt(wdev, freq, 0, frame, frame_len, GFP_ATOMIC); +#else + cfg80211_rx_action(adapter->pnetdev, freq, frame, frame_len, GFP_ATOMIC); +#endif +} + +void rtw_cfg80211_rx_action(_adapter *adapter, union recv_frame *rframe, const char *msg) +{ + struct wireless_dev *wdev = adapter->rtw_wdev; + u8 *frame = get_recvframe_data(rframe); + uint frame_len = rframe->u.hdr.len; + s32 freq; + u8 ch, sch = rtw_get_oper_ch(adapter); + u8 category, action; + int type = -1; + + ch = rframe->u.hdr.attrib.ch ? rframe->u.hdr.attrib.ch : sch; + freq = rtw_ch2freq(ch); + + RTW_INFO("RTW_Rx:ch=%d(%d), ta="MAC_FMT"\n" + , ch, sch, MAC_ARG(get_addr2_ptr(frame))); + +#ifdef CONFIG_RTW_MESH + if (MLME_IS_MESH(adapter)) { + type = rtw_mesh_check_frames_rx(adapter, frame, frame_len); + if (type >= 0) + goto indicate; + } +#endif + rtw_action_frame_parse(frame, frame_len, &category, &action); + if (category == RTW_WLAN_CATEGORY_PUBLIC) { + if (action == ACT_PUBLIC_GAS_INITIAL_REQ) { + rtw_mi_set_scan_deny(adapter, 200); + rtw_mi_scan_abort(adapter, _FALSE); /*rtw_scan_abort_no_wait*/ + } + } +#ifdef CONFIG_RTW_MESH +indicate: +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + rtw_cfg80211_rx_mgmt(wdev, freq, 0, frame, frame_len, GFP_ATOMIC); +#else + cfg80211_rx_action(adapter->pnetdev, freq, frame, frame_len, GFP_ATOMIC); +#endif + + if (type == -1) { + if (msg) + RTW_INFO("RTW_Rx:%s\n", msg); + else + RTW_INFO("RTW_Rx:category(%u), action(%u)\n", category, action); + } +} + +#ifdef CONFIG_RTW_80211K +void rtw_cfg80211_rx_rrm_action(_adapter *adapter, union recv_frame *rframe) +{ + struct wireless_dev *wdev = adapter->rtw_wdev; + u8 *frame = get_recvframe_data(rframe); + uint frame_len = rframe->u.hdr.len; + s32 freq; + u8 ch, sch = rtw_get_oper_ch(adapter); + + ch = rframe->u.hdr.attrib.ch ? rframe->u.hdr.attrib.ch : sch; + freq = rtw_ch2freq(ch); + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + rtw_cfg80211_rx_mgmt(wdev, freq, 0, frame, frame_len, GFP_ATOMIC); +#else + cfg80211_rx_action(adapter->pnetdev, freq, frame, frame_len, GFP_ATOMIC); +#endif + RTW_INFO("RTW_Rx:ch=%d(%d), ta="MAC_FMT"\n" + , ch, sch, MAC_ARG(get_addr2_ptr(frame))); +} +#endif /* CONFIG_RTW_80211K */ + +void rtw_cfg80211_rx_mframe(_adapter *adapter, union recv_frame *rframe, const char *msg) +{ + struct wireless_dev *wdev = adapter->rtw_wdev; + u8 *frame = get_recvframe_data(rframe); + uint frame_len = rframe->u.hdr.len; + s32 freq; + u8 ch, sch = rtw_get_oper_ch(adapter); + + ch = rframe->u.hdr.attrib.ch ? rframe->u.hdr.attrib.ch : sch; + freq = rtw_ch2freq(ch); + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + rtw_cfg80211_rx_mgmt(wdev, freq, 0, frame, frame_len, GFP_ATOMIC); +#else + cfg80211_rx_action(adapter->pnetdev, freq, frame, frame_len, GFP_ATOMIC); +#endif + + RTW_INFO("RTW_Rx:ch=%d(%d), ta="MAC_FMT"\n", ch, sch, MAC_ARG(get_addr2_ptr(frame))); + if (!rtw_sae_preprocess(adapter, frame, frame_len, _FALSE)) { + if (msg) + RTW_INFO("RTW_Rx:%s\n", msg); + else + RTW_INFO("RTW_Rx:frame_control:0x%02x\n", le16_to_cpu(((struct rtw_ieee80211_hdr_3addr *)rframe)->frame_ctl)); + } +} + +#ifdef CONFIG_P2P +void rtw_cfg80211_issue_p2p_provision_request(_adapter *padapter, const u8 *buf, size_t len) +{ + u16 wps_devicepassword_id = 0x0000; + uint wps_devicepassword_id_len = 0; + u8 wpsie[255] = { 0x00 }, p2p_ie[255] = { 0x00 }; + uint p2p_ielen = 0; + uint wpsielen = 0; + u32 devinfo_contentlen = 0; + u8 devinfo_content[64] = { 0x00 }; + u16 capability = 0; + uint capability_len = 0; + + unsigned char category = RTW_WLAN_CATEGORY_PUBLIC; + u8 action = P2P_PUB_ACTION_ACTION; + u8 dialogToken = 1; + u32 p2poui = cpu_to_be32(P2POUI); + u8 oui_subtype = P2P_PROVISION_DISC_REQ; + u32 p2pielen = 0; +#ifdef CONFIG_WFD + u32 wfdielen = 0; +#endif + + struct xmit_frame *pmgntframe; + struct pkt_attrib *pattrib; + unsigned char *pframe; + struct rtw_ieee80211_hdr *pwlanhdr; + unsigned short *fctrl; + struct xmit_priv *pxmitpriv = &(padapter->xmitpriv); + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + u8 *frame_body = (unsigned char *)(buf + sizeof(struct rtw_ieee80211_hdr_3addr)); + size_t frame_body_len = len - sizeof(struct rtw_ieee80211_hdr_3addr); + + + RTW_INFO("[%s] In\n", __FUNCTION__); + + /* prepare for building provision_request frame */ + _rtw_memcpy(pwdinfo->tx_prov_disc_info.peerIFAddr, GetAddr1Ptr(buf), ETH_ALEN); + _rtw_memcpy(pwdinfo->tx_prov_disc_info.peerDevAddr, GetAddr1Ptr(buf), ETH_ALEN); + + pwdinfo->tx_prov_disc_info.wps_config_method_request = WPS_CM_PUSH_BUTTON; + + rtw_get_wps_ie(frame_body + _PUBLIC_ACTION_IE_OFFSET_, frame_body_len - _PUBLIC_ACTION_IE_OFFSET_, wpsie, &wpsielen); + rtw_get_wps_attr_content(wpsie, wpsielen, WPS_ATTR_DEVICE_PWID, (u8 *) &wps_devicepassword_id, &wps_devicepassword_id_len); + wps_devicepassword_id = be16_to_cpu(wps_devicepassword_id); + + switch (wps_devicepassword_id) { + case WPS_DPID_PIN: + pwdinfo->tx_prov_disc_info.wps_config_method_request = WPS_CM_LABEL; + break; + case WPS_DPID_USER_SPEC: + pwdinfo->tx_prov_disc_info.wps_config_method_request = WPS_CM_DISPLYA; + break; + case WPS_DPID_MACHINE_SPEC: + break; + case WPS_DPID_REKEY: + break; + case WPS_DPID_PBC: + pwdinfo->tx_prov_disc_info.wps_config_method_request = WPS_CM_PUSH_BUTTON; + break; + case WPS_DPID_REGISTRAR_SPEC: + pwdinfo->tx_prov_disc_info.wps_config_method_request = WPS_CM_KEYPAD; + break; + default: + break; + } + + + if (rtw_get_p2p_ie(frame_body + _PUBLIC_ACTION_IE_OFFSET_, frame_body_len - _PUBLIC_ACTION_IE_OFFSET_, p2p_ie, &p2p_ielen)) { + + rtw_get_p2p_attr_content(p2p_ie, p2p_ielen, P2P_ATTR_DEVICE_INFO, devinfo_content, &devinfo_contentlen); + rtw_get_p2p_attr_content(p2p_ie, p2p_ielen, P2P_ATTR_CAPABILITY, (u8 *)&capability, &capability_len); + + } + + + /* start to build provision_request frame */ + _rtw_memset(wpsie, 0, sizeof(wpsie)); + _rtw_memset(p2p_ie, 0, sizeof(p2p_ie)); + p2p_ielen = 0; + + pmgntframe = alloc_mgtxmitframe(pxmitpriv); + if (pmgntframe == NULL) + return; + + + /* update attribute */ + pattrib = &pmgntframe->attrib; + update_mgntframe_attrib(padapter, pattrib); + + _rtw_memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET); + + pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET; + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + fctrl = &(pwlanhdr->frame_ctl); + *(fctrl) = 0; + + _rtw_memcpy(pwlanhdr->addr1, pwdinfo->tx_prov_disc_info.peerDevAddr, ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr2, adapter_mac_addr(padapter), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr3, pwdinfo->tx_prov_disc_info.peerDevAddr, ETH_ALEN); + + SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq); + pmlmeext->mgnt_seq++; + set_frame_sub_type(pframe, WIFI_ACTION); + + pframe += sizeof(struct rtw_ieee80211_hdr_3addr); + pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr); + + pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pattrib->pktlen)); + pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pattrib->pktlen)); + pframe = rtw_set_fixed_ie(pframe, 4, (unsigned char *) &(p2poui), &(pattrib->pktlen)); + pframe = rtw_set_fixed_ie(pframe, 1, &(oui_subtype), &(pattrib->pktlen)); + pframe = rtw_set_fixed_ie(pframe, 1, &(dialogToken), &(pattrib->pktlen)); + + + /* build_prov_disc_request_p2p_ie */ + /* P2P OUI */ + p2pielen = 0; + p2p_ie[p2pielen++] = 0x50; + p2p_ie[p2pielen++] = 0x6F; + p2p_ie[p2pielen++] = 0x9A; + p2p_ie[p2pielen++] = 0x09; /* WFA P2P v1.0 */ + + /* Commented by Albert 20110301 */ + /* According to the P2P Specification, the provision discovery request frame should contain 3 P2P attributes */ + /* 1. P2P Capability */ + /* 2. Device Info */ + /* 3. Group ID ( When joining an operating P2P Group ) */ + + /* P2P Capability ATTR */ + /* Type: */ + p2p_ie[p2pielen++] = P2P_ATTR_CAPABILITY; + + /* Length: */ + /* *(u16*) ( p2pie + p2pielen ) = cpu_to_le16( 0x0002 ); */ + RTW_PUT_LE16(p2p_ie + p2pielen, 0x0002); + p2pielen += 2; + + /* Value: */ + /* Device Capability Bitmap, 1 byte */ + /* Group Capability Bitmap, 1 byte */ + _rtw_memcpy(p2p_ie + p2pielen, &capability, 2); + p2pielen += 2; + + + /* Device Info ATTR */ + /* Type: */ + p2p_ie[p2pielen++] = P2P_ATTR_DEVICE_INFO; + + /* Length: */ + /* 21->P2P Device Address (6bytes) + Config Methods (2bytes) + Primary Device Type (8bytes) */ + /* + NumofSecondDevType (1byte) + WPS Device Name ID field (2bytes) + WPS Device Name Len field (2bytes) */ + /* *(u16*) ( p2pie + p2pielen ) = cpu_to_le16( 21 + pwdinfo->device_name_len ); */ + RTW_PUT_LE16(p2p_ie + p2pielen, devinfo_contentlen); + p2pielen += 2; + + /* Value: */ + _rtw_memcpy(p2p_ie + p2pielen, devinfo_content, devinfo_contentlen); + p2pielen += devinfo_contentlen; + + + pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, p2pielen, (unsigned char *) p2p_ie, &p2p_ielen); + /* p2pielen = build_prov_disc_request_p2p_ie( pwdinfo, pframe, NULL, 0, pwdinfo->tx_prov_disc_info.peerDevAddr); */ + /* pframe += p2pielen; */ + pattrib->pktlen += p2p_ielen; + + wpsielen = 0; + /* WPS OUI */ + *(u32 *)(wpsie) = cpu_to_be32(WPSOUI); + wpsielen += 4; + + /* WPS version */ + /* Type: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_VER1); + wpsielen += 2; + + /* Length: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(0x0001); + wpsielen += 2; + + /* Value: */ + wpsie[wpsielen++] = WPS_VERSION_1; /* Version 1.0 */ + + /* Config Method */ + /* Type: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_CONF_METHOD); + wpsielen += 2; + + /* Length: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(0x0002); + wpsielen += 2; + + /* Value: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(pwdinfo->tx_prov_disc_info.wps_config_method_request); + wpsielen += 2; + + pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, wpsielen, (unsigned char *) wpsie, &pattrib->pktlen); + + +#ifdef CONFIG_WFD + wfdielen = build_provdisc_req_wfd_ie(pwdinfo, pframe); + pframe += wfdielen; + pattrib->pktlen += wfdielen; +#endif + + pattrib->last_txcmdsz = pattrib->pktlen; + + /* dump_mgntframe(padapter, pmgntframe); */ + if (dump_mgntframe_and_wait_ack(padapter, pmgntframe) != _SUCCESS) + RTW_INFO("%s, ack to\n", __func__); + + #if 0 + if(wps_devicepassword_id == WPS_DPID_REGISTRAR_SPEC) { + RTW_INFO("waiting for p2p peer key-in PIN CODE\n"); + rtw_msleep_os(15000); /* 15 sec for key in PIN CODE, workaround for GS2 before issuing Nego Req. */ + } + #endif + +} + +#ifdef CONFIG_RTW_80211R +static s32 cfg80211_rtw_update_ft_ies(struct wiphy *wiphy, + struct net_device *ndev, + struct cfg80211_update_ft_ies_params *ftie) +{ + _adapter *padapter = NULL; + struct mlme_priv *pmlmepriv = NULL; + struct ft_roam_info *pft_roam = NULL; + _irqL irqL; + u8 *p; + u8 *pie = NULL; + u32 ie_len = 0; + + if (ndev == NULL) + return -EINVAL; + + padapter = (_adapter *)rtw_netdev_priv(ndev); + pmlmepriv = &(padapter->mlmepriv); + pft_roam = &(pmlmepriv->ft_roam); + + p = (u8 *)ftie->ie; + if (ftie->ie_len <= sizeof(pft_roam->updated_ft_ies)) { + _enter_critical_bh(&pmlmepriv->lock, &irqL); + _rtw_memcpy(pft_roam->updated_ft_ies, ftie->ie, ftie->ie_len); + pft_roam->updated_ft_ies_len = ftie->ie_len; + _exit_critical_bh(&pmlmepriv->lock, &irqL); + } else { + RTW_ERR("FTIEs parsing fail!\n"); + return -EINVAL; + } + + if (rtw_ft_roam_status(padapter, RTW_FT_AUTHENTICATED_STA)) { + RTW_PRINT("auth success, start reassoc\n"); + rtw_ft_lock_set_status(padapter, RTW_FT_ASSOCIATING_STA, &irqL); + start_clnt_assoc(padapter); + } + + return 0; +} +#endif +#endif /* CONFIG_P2P */ + +inline void rtw_cfg80211_set_is_roch(_adapter *adapter, bool val) +{ + adapter->rochinfo.is_ro_ch = val; + rtw_mi_update_iface_status(&(adapter->mlmepriv), 0); +} + +inline bool rtw_cfg80211_get_is_roch(_adapter *adapter) +{ + return adapter->rochinfo.is_ro_ch; +} + +inline bool rtw_cfg80211_is_ro_ch_once(_adapter *adapter) +{ + return adapter->rochinfo.last_ro_ch_time ? 1 : 0; +} + +inline void rtw_cfg80211_set_last_ro_ch_time(_adapter *adapter) +{ + adapter->rochinfo.last_ro_ch_time = rtw_get_current_time(); + + if (!adapter->rochinfo.last_ro_ch_time) + adapter->rochinfo.last_ro_ch_time++; +} + +inline s32 rtw_cfg80211_get_last_ro_ch_passing_ms(_adapter *adapter) +{ + return rtw_get_passing_time_ms(adapter->rochinfo.last_ro_ch_time); +} + +static s32 cfg80211_rtw_remain_on_channel(struct wiphy *wiphy, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + struct wireless_dev *wdev, +#else + struct net_device *ndev, +#endif + struct ieee80211_channel *channel, +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0)) + enum nl80211_channel_type channel_type, +#endif + unsigned int duration, u64 *cookie) +{ + s32 err = 0; + u8 remain_ch = (u8) ieee80211_frequency_to_channel(channel->center_freq); + _adapter *padapter = NULL; + struct rtw_wdev_priv *pwdev_priv; + struct roch_info *prochinfo; +#ifdef CONFIG_P2P + struct wifidirect_info *pwdinfo; +#ifdef CONFIG_CONCURRENT_MODE + u8 is_p2p_find = _FALSE; +#endif +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + #if defined(RTW_DEDICATED_P2P_DEVICE) + if (wdev == wiphy_to_pd_wdev(wiphy)) + padapter = wiphy_to_adapter(wiphy); + else + #endif + if (wdev_to_ndev(wdev)) + padapter = (_adapter *)rtw_netdev_priv(wdev_to_ndev(wdev)); + else { + err = -EINVAL; + goto exit; + } +#else + struct wireless_dev *wdev; + + if (ndev == NULL) { + err = -EINVAL; + goto exit; + } + padapter = (_adapter *)rtw_netdev_priv(ndev); + wdev = ndev_to_wdev(ndev); +#endif + + pwdev_priv = adapter_wdev_data(padapter); + prochinfo = &padapter->rochinfo; +#ifdef CONFIG_P2P + pwdinfo = &padapter->wdinfo; +#ifdef CONFIG_CONCURRENT_MODE + is_p2p_find = (duration < (pwdinfo->ext_listen_interval)) ? _TRUE : _FALSE; +#endif +#endif + + *cookie = ATOMIC_INC_RETURN(&prochinfo->ro_ch_cookie_gen); + + RTW_INFO(FUNC_ADPT_FMT"%s ch:%u duration:%d, cookie:0x%llx\n" + , FUNC_ADPT_ARG(padapter), wdev == wiphy_to_pd_wdev(wiphy) ? " PD" : "" + , remain_ch, duration, *cookie); + + if (rtw_chset_search_ch(adapter_to_chset(padapter), remain_ch) < 0) { + RTW_WARN(FUNC_ADPT_FMT" invalid ch:%u\n", FUNC_ADPT_ARG(padapter), remain_ch); + err = -EFAULT; + goto exit; + } + +#ifdef CONFIG_MP_INCLUDED + if (rtw_mp_mode_check(padapter)) { + RTW_INFO("MP mode block remain_on_channel request\n"); + err = -EFAULT; + goto exit; + } +#endif + + if (_FAIL == rtw_pwr_wakeup(padapter)) { + err = -EFAULT; + goto exit; + } + + rtw_scan_abort(padapter); +#if defined(CONFIG_CONCURRENT_MODE) && defined(CONFIG_P2P) + /*don't scan_abort during p2p_listen.*/ + if (is_p2p_find) + rtw_mi_buddy_scan_abort(padapter, _TRUE); +#endif /* defined(CONFIG_CONCURRENT_MODE) && defined(CONFIG_P2P) */ + + if (rtw_cfg80211_get_is_roch(padapter) == _TRUE) { + _cancel_timer_ex(&padapter->rochinfo.remain_on_ch_timer); + rtw_cancel_roch_cmd(padapter, 0, NULL, RTW_CMDF_WAIT_ACK); + } + +#ifdef CONFIG_P2P + /* if(!rtw_p2p_chk_role(pwdinfo, P2P_ROLE_CLIENT) && !rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO)) */ + if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE) + #if defined(CONFIG_CONCURRENT_MODE) && defined(CONFIG_P2P) + && ((padapter->iface_id == padapter->registrypriv.sel_p2p_iface)) + #endif + ) { + rtw_p2p_enable(padapter, P2P_ROLE_DEVICE); + padapter->wdinfo.listen_channel = remain_ch; + RTW_INFO(FUNC_ADPT_FMT" init listen_channel %u\n" + , FUNC_ADPT_ARG(padapter), padapter->wdinfo.listen_channel); + } else if (rtw_p2p_chk_state(pwdinfo , P2P_STATE_LISTEN) + && (time_after_eq(rtw_get_current_time(), pwdev_priv->probe_resp_ie_update_time) + && rtw_get_passing_time_ms(pwdev_priv->probe_resp_ie_update_time) < 50) + ) { + if (padapter->wdinfo.listen_channel != remain_ch) { + padapter->wdinfo.listen_channel = remain_ch; + RTW_INFO(FUNC_ADPT_FMT" update listen_channel %u\n" + , FUNC_ADPT_ARG(padapter), padapter->wdinfo.listen_channel); + } + } else { + rtw_p2p_set_pre_state(pwdinfo, rtw_p2p_state(pwdinfo)); +#ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("%s, role=%d, p2p_state=%d\n", __func__, rtw_p2p_role(pwdinfo), rtw_p2p_state(pwdinfo)); +#endif + } + + rtw_p2p_set_state(pwdinfo, P2P_STATE_LISTEN); +#endif /* CONFIG_P2P */ + + #ifdef RTW_ROCH_DURATION_ENLARGE + if (duration < 400) + duration = duration * 3; /* extend from exper */ + #endif + +#if defined(RTW_ROCH_BACK_OP) && defined(CONFIG_CONCURRENT_MODE) && defined(CONFIG_P2P) + if (rtw_mi_check_status(padapter, MI_LINKED)) { + if (is_p2p_find) /* p2p_find , duration<1000 */ + duration = duration + pwdinfo->ext_listen_interval; + } +#endif /* defined (RTW_ROCH_BACK_OP) && defined(CONFIG_CONCURRENT_MODE) && defined(CONFIG_P2P) */ + + rtw_cfg80211_set_is_roch(padapter, _TRUE); + prochinfo->ro_ch_wdev = wdev; + prochinfo->remain_on_ch_cookie = *cookie; + rtw_cfg80211_set_last_ro_ch_time(padapter); + _rtw_memcpy(&prochinfo->remain_on_ch_channel, channel, sizeof(struct ieee80211_channel)); + #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0)) + prochinfo->remain_on_ch_type = channel_type; + #endif + prochinfo->restore_channel = rtw_get_oper_ch(padapter); + + rtw_roch_cmd(padapter, *cookie, wdev, channel, prochinfo->remain_on_ch_type, + duration, RTW_CMDF_WAIT_ACK); + + rtw_cfg80211_ready_on_channel(wdev, *cookie, channel, channel_type, duration, GFP_KERNEL); +exit: + return err; +} + +static s32 cfg80211_rtw_cancel_remain_on_channel(struct wiphy *wiphy, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + struct wireless_dev *wdev, +#else + struct net_device *ndev, +#endif + u64 cookie) +{ + s32 err = 0; + _adapter *padapter; + struct rtw_wdev_priv *pwdev_priv; + struct roch_info *prochinfo; +#ifdef CONFIG_P2P + struct wifidirect_info *pwdinfo; +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + #if defined(RTW_DEDICATED_P2P_DEVICE) + if (wdev == wiphy_to_pd_wdev(wiphy)) + padapter = wiphy_to_adapter(wiphy); + else + #endif + if (wdev_to_ndev(wdev)) + padapter = (_adapter *)rtw_netdev_priv(wdev_to_ndev(wdev)); + else { + err = -EINVAL; + goto exit; + } +#else + struct wireless_dev *wdev; + + if (ndev == NULL) { + err = -EINVAL; + goto exit; + } + padapter = (_adapter *)rtw_netdev_priv(ndev); + wdev = ndev_to_wdev(ndev); +#endif + + pwdev_priv = adapter_wdev_data(padapter); + prochinfo = &padapter->rochinfo; +#ifdef CONFIG_P2P + pwdinfo = &padapter->wdinfo; +#endif + + RTW_INFO(FUNC_ADPT_FMT"%s cookie:0x%llx\n" + , FUNC_ADPT_ARG(padapter), wdev == wiphy_to_pd_wdev(wiphy) ? " PD" : "" + , cookie); + + if (rtw_cfg80211_get_is_roch(padapter) == _TRUE) { + _cancel_timer_ex(&padapter->rochinfo.remain_on_ch_timer); + rtw_cancel_roch_cmd(padapter, cookie, wdev, RTW_CMDF_WAIT_ACK); + } + +exit: + return err; +} + +#ifdef CONFIG_P2P +inline int rtw_cfg80211_iface_has_p2p_group_cap(_adapter *adapter) +{ +#if RTW_P2P_GROUP_INTERFACE + if (is_primary_adapter(adapter)) + return 0; +#endif + return 1; +} + +inline int rtw_cfg80211_is_p2p_scan(_adapter *adapter) +{ +#if RTW_P2P_GROUP_INTERFACE + if (rtw_cfg80211_iface_has_p2p_group_cap(adapter)) +#endif + { + struct wifidirect_info *wdinfo = &adapter->wdinfo; + + return rtw_p2p_chk_state(wdinfo, P2P_STATE_SCAN) + || rtw_p2p_chk_state(wdinfo, P2P_STATE_FIND_PHASE_SEARCH); + } + +#if RTW_P2P_GROUP_INTERFACE + #if defined(RTW_DEDICATED_P2P_DEVICE) + if (wiphy_to_pd_wdev(adapter_to_wiphy(adapter))) /* pd_wdev exist */ + return rtw_cfg80211_is_scan_by_pd_wdev(adapter); + #endif + { + /* + * For 2 RTW_P2P_GROUP_INTERFACE cases: + * 1. RTW_DEDICATED_P2P_DEVICE defined but upper layer don't use pd_wdev or + * 2. RTW_DEDICATED_P2P_DEVICE not defined + */ + struct rtw_wdev_priv *wdev_data = adapter_wdev_data(adapter); + _irqL irqL; + int is_p2p_scan = 0; + + _enter_critical_bh(&wdev_data->scan_req_lock, &irqL); + if (wdev_data->scan_request + && wdev_data->scan_request->n_ssids + && wdev_data->scan_request->ssids + && wdev_data->scan_request->ie + ) { + if (_rtw_memcmp(wdev_data->scan_request->ssids[0].ssid, "DIRECT-", 7) + && rtw_get_p2p_ie((u8 *)wdev_data->scan_request->ie, wdev_data->scan_request->ie_len, NULL, NULL)) + is_p2p_scan = 1; + } + _exit_critical_bh(&wdev_data->scan_req_lock, &irqL); + + return is_p2p_scan; + } +#endif +} + +#if defined(RTW_DEDICATED_P2P_DEVICE) +int rtw_pd_iface_alloc(struct wiphy *wiphy, const char *name, struct wireless_dev **pd_wdev) +{ + struct rtw_wiphy_data *wiphy_data = rtw_wiphy_priv(wiphy); + struct wireless_dev *wdev = NULL; + struct rtw_netdev_priv_indicator *npi; + _adapter *primary_adpt = wiphy_to_adapter(wiphy); + int ret = 0; + + if (wiphy_data->pd_wdev) { + RTW_WARN(FUNC_WIPHY_FMT" pd_wdev already exists\n", FUNC_WIPHY_ARG(wiphy)); + ret = -EBUSY; + goto exit; + } + + wdev = (struct wireless_dev *)rtw_zmalloc(sizeof(struct wireless_dev)); + if (!wdev) { + RTW_WARN(FUNC_WIPHY_FMT" allocate wdev fail\n", FUNC_WIPHY_ARG(wiphy)); + ret = -ENOMEM; + goto exit; + } + + wdev->wiphy = wiphy; + wdev->iftype = NL80211_IFTYPE_P2P_DEVICE; + _rtw_memcpy(wdev->address, adapter_mac_addr(primary_adpt), ETH_ALEN); + + wiphy_data->pd_wdev = wdev; + *pd_wdev = wdev; + + RTW_INFO(FUNC_WIPHY_FMT" pd_wdev:%p, addr="MAC_FMT" added\n" + , FUNC_WIPHY_ARG(wiphy), wdev, MAC_ARG(wdev_address(wdev))); + +exit: + if (ret && wdev) { + rtw_mfree((u8 *)wdev, sizeof(struct wireless_dev)); + wdev = NULL; + } + + return ret; +} + +void rtw_pd_iface_free(struct wiphy *wiphy) +{ + struct dvobj_priv *dvobj = wiphy_to_dvobj(wiphy); + struct rtw_wiphy_data *wiphy_data = rtw_wiphy_priv(wiphy); + u8 rtnl_lock_needed; + + if (!wiphy_data->pd_wdev) + goto exit; + + RTW_INFO(FUNC_WIPHY_FMT" pd_wdev:%p, addr="MAC_FMT"\n" + , FUNC_WIPHY_ARG(wiphy), wiphy_data->pd_wdev + , MAC_ARG(wdev_address(wiphy_data->pd_wdev))); + + rtnl_lock_needed = rtw_rtnl_lock_needed(dvobj); + if (rtnl_lock_needed) + rtnl_lock(); + cfg80211_unregister_wdev(wiphy_data->pd_wdev); + if (rtnl_lock_needed) + rtnl_unlock(); + + rtw_mfree((u8 *)wiphy_data->pd_wdev, sizeof(struct wireless_dev)); + wiphy_data->pd_wdev = NULL; + +exit: + return; +} + +static int cfg80211_rtw_start_p2p_device(struct wiphy *wiphy, struct wireless_dev *wdev) +{ + _adapter *adapter = wiphy_to_adapter(wiphy); + + RTW_INFO(FUNC_WIPHY_FMT" wdev=%p\n", FUNC_WIPHY_ARG(wiphy), wdev); + + rtw_p2p_enable(adapter, P2P_ROLE_DEVICE); + return 0; +} + +static void cfg80211_rtw_stop_p2p_device(struct wiphy *wiphy, struct wireless_dev *wdev) +{ + _adapter *adapter = wiphy_to_adapter(wiphy); + + RTW_INFO(FUNC_WIPHY_FMT" wdev=%p\n", FUNC_WIPHY_ARG(wiphy), wdev); + + if (rtw_cfg80211_is_p2p_scan(adapter)) + rtw_scan_abort(adapter); + + rtw_p2p_enable(adapter, P2P_ROLE_DISABLE); +} + +inline int rtw_cfg80211_redirect_pd_wdev(struct wiphy *wiphy, u8 *ra, struct wireless_dev **wdev) +{ + struct wireless_dev *pd_wdev = wiphy_to_pd_wdev(wiphy); + + if (pd_wdev && pd_wdev != *wdev + && _rtw_memcmp(wdev_address(pd_wdev), ra, ETH_ALEN) == _TRUE + ) { + *wdev = pd_wdev; + return 1; + } + return 0; +} + +inline int rtw_cfg80211_is_scan_by_pd_wdev(_adapter *adapter) +{ + struct wiphy *wiphy = adapter_to_wiphy(adapter); + struct rtw_wdev_priv *wdev_data = adapter_wdev_data(adapter); + struct wireless_dev *wdev = NULL; + _irqL irqL; + + _enter_critical_bh(&wdev_data->scan_req_lock, &irqL); + if (wdev_data->scan_request) + wdev = wdev_data->scan_request->wdev; + _exit_critical_bh(&wdev_data->scan_req_lock, &irqL); + + if (wdev && wdev == wiphy_to_pd_wdev(wiphy)) + return 1; + + return 0; +} +#endif /* RTW_DEDICATED_P2P_DEVICE */ +#endif /* CONFIG_P2P */ + +inline void rtw_cfg80211_set_is_mgmt_tx(_adapter *adapter, u8 val) +{ + struct rtw_wdev_priv *wdev_priv = adapter_wdev_data(adapter); + + wdev_priv->is_mgmt_tx = val; + rtw_mi_update_iface_status(&(adapter->mlmepriv), 0); +} + +inline u8 rtw_cfg80211_get_is_mgmt_tx(_adapter *adapter) +{ + struct rtw_wdev_priv *wdev_priv = adapter_wdev_data(adapter); + + return wdev_priv->is_mgmt_tx; +} + +static int _cfg80211_rtw_mgmt_tx(_adapter *padapter, u8 tx_ch, u8 no_cck, const u8 *buf, size_t len, int wait_ack) +{ + struct xmit_frame *pmgntframe; + struct pkt_attrib *pattrib; + unsigned char *pframe; + int ret = _FAIL; + bool ack = _TRUE; + struct rtw_ieee80211_hdr *pwlanhdr; +#if defined(RTW_ROCH_BACK_OP) && defined(CONFIG_P2P) && defined(CONFIG_CONCURRENT_MODE) + struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(padapter); +#endif + struct xmit_priv *pxmitpriv = &(padapter->xmitpriv); + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + u8 u_ch = rtw_mi_get_union_chan(padapter); + u8 leave_op = 0; + struct roch_info *prochinfo = &padapter->rochinfo; +#if defined(CONFIG_P2P) && defined(CONFIG_CONCURRENT_MODE) + struct wifidirect_info *pwdinfo = &padapter->wdinfo; +#endif + + rtw_cfg80211_set_is_mgmt_tx(padapter, 1); + +#ifdef CONFIG_BT_COEXIST + rtw_btcoex_ScanNotify(padapter, _TRUE); +#endif + +#ifdef CONFIG_P2P + if (rtw_cfg80211_get_is_roch(padapter) == _TRUE) { + #ifdef CONFIG_CONCURRENT_MODE + if (!check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE)) { + RTW_INFO("%s, extend ro ch time\n", __func__); + _set_timer(&padapter->rochinfo.remain_on_ch_timer, pwdinfo->ext_listen_period); + } + #endif /* CONFIG_CONCURRENT_MODE */ + } +#endif /* CONFIG_P2P */ + +#ifdef CONFIG_MCC_MODE + if (MCC_EN(padapter)) { + if (rtw_hal_check_mcc_status(padapter, MCC_STATUS_DOING_MCC)) + /* don't set channel, issue frame directly */ + goto issue_mgmt_frame; + } +#endif /* CONFIG_MCC_MODE */ + + if (rtw_mi_check_status(padapter, MI_LINKED) + && tx_ch != u_ch + ) { + rtw_leave_opch(padapter); + leave_op = 1; + } + + if (tx_ch != rtw_get_oper_ch(padapter)) + set_channel_bwmode(padapter, tx_ch, HAL_PRIME_CHNL_OFFSET_DONT_CARE, CHANNEL_WIDTH_20); +#ifdef CONFIG_MCC_MODE +issue_mgmt_frame: +#endif + /* starting alloc mgmt frame to dump it */ + pmgntframe = alloc_mgtxmitframe(pxmitpriv); + if (pmgntframe == NULL) { + /* ret = -ENOMEM; */ + ret = _FAIL; + goto exit; + } + + /* update attribute */ + pattrib = &pmgntframe->attrib; + update_mgntframe_attrib(padapter, pattrib); + + if (no_cck && IS_CCK_RATE(pattrib->rate)) { + /* force OFDM 6M rate*/ + pattrib->rate = MGN_6M; + pattrib->raid = rtw_get_mgntframe_raid(padapter, WIRELESS_11G); + } + + pattrib->retry_ctrl = _FALSE; + + _rtw_memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET); + + pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET; + + _rtw_memcpy(pframe, (void *)buf, len); + pattrib->pktlen = len; + + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + /* update seq number */ + pmlmeext->mgnt_seq = GetSequence(pwlanhdr); + pattrib->seqnum = pmlmeext->mgnt_seq; + pmlmeext->mgnt_seq++; + +#ifdef CONFIG_P2P + rtw_xframe_chk_wfd_ie(pmgntframe); +#endif /* CONFIG_P2P */ + + pattrib->last_txcmdsz = pattrib->pktlen; + + if (wait_ack) { + if (dump_mgntframe_and_wait_ack(padapter, pmgntframe) != _SUCCESS) { + ack = _FALSE; + ret = _FAIL; + +#ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("%s, ack == _FAIL\n", __func__); +#endif + } else { + +#ifdef CONFIG_XMIT_ACK + if (!MLME_IS_MESH(padapter)) /* TODO: remove this sleep for all mode */ + rtw_msleep_os(50); +#endif +#ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("%s, ack=%d, ok!\n", __func__, ack); +#endif + ret = _SUCCESS; + } + } else { + dump_mgntframe(padapter, pmgntframe); + ret = _SUCCESS; + } + +exit: + if (rtw_cfg80211_get_is_roch(padapter) + && !rtw_roch_stay_in_cur_chan(padapter) + && prochinfo->remain_on_ch_channel.hw_value != u_ch + ) { + /* roch is ongoing, switch back to rch */ + if (prochinfo->remain_on_ch_channel.hw_value != tx_ch) + set_channel_bwmode(padapter, prochinfo->remain_on_ch_channel.hw_value + , HAL_PRIME_CHNL_OFFSET_DONT_CARE, CHANNEL_WIDTH_20); + } else if (leave_op) { + if (rtw_mi_check_status(padapter, MI_LINKED)) { + u8 u_bw = rtw_mi_get_union_bw(padapter); + u8 u_offset = rtw_mi_get_union_offset(padapter); + + set_channel_bwmode(padapter, u_ch, u_offset, u_bw); + } + rtw_back_opch(padapter); + } + + rtw_cfg80211_set_is_mgmt_tx(padapter, 0); + +#ifdef CONFIG_BT_COEXIST + rtw_btcoex_ScanNotify(padapter, _FALSE); +#endif + +#ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("%s, ret=%d\n", __func__, ret); +#endif + + return ret; + +} + +u8 rtw_mgnt_tx_handler(_adapter *adapter, u8 *buf) +{ + u8 rst = H2C_CMD_FAIL; + struct mgnt_tx_parm *mgnt_parm = (struct mgnt_tx_parm *)buf; + + if (_cfg80211_rtw_mgmt_tx(adapter, mgnt_parm->tx_ch, mgnt_parm->no_cck, + mgnt_parm->buf, mgnt_parm->len, mgnt_parm->wait_ack) == _SUCCESS) + rst = H2C_SUCCESS; + + return rst; +} + +static int cfg80211_rtw_mgmt_tx(struct wiphy *wiphy, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + struct wireless_dev *wdev, +#else + struct net_device *ndev, +#endif +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0)) || defined(COMPAT_KERNEL_RELEASE) + struct ieee80211_channel *chan, + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)) || defined(COMPAT_KERNEL_RELEASE) + bool offchan, + #endif + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34)) && (LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0)) + enum nl80211_channel_type channel_type, + #endif + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)) && (LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0)) + bool channel_type_valid, + #endif + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)) || defined(COMPAT_KERNEL_RELEASE) + unsigned int wait, + #endif + const u8 *buf, size_t len, + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)) + bool no_cck, + #endif + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)) + bool dont_wait_for_ack, + #endif +#else + struct cfg80211_mgmt_tx_params *params, +#endif + u64 *cookie) +{ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)) || defined(COMPAT_KERNEL_RELEASE) + struct ieee80211_channel *chan = params->chan; + const u8 *buf = params->buf; + size_t len = params->len; + bool no_cck = params->no_cck; +#endif +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0)) + bool no_cck = 0; +#endif + int ret = 0; + u8 tx_ret; + int wait_ack = 1; + const u8 *dump_buf = buf; + size_t dump_len = len; + u32 dump_limit = RTW_MAX_MGMT_TX_CNT; + u32 dump_cnt = 0; + u32 sleep_ms = 0; + u32 retry_guarantee_ms = 0; + bool ack = _TRUE; + u8 tx_ch; + u8 category, action; + u8 frame_styp; +#ifdef CONFIG_P2P + u8 is_p2p = 0; +#endif + int type = (-1); + systime start = rtw_get_current_time(); + _adapter *padapter; + struct dvobj_priv *dvobj; + struct rtw_wdev_priv *pwdev_priv; + struct rf_ctl_t *rfctl; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + #if defined(RTW_DEDICATED_P2P_DEVICE) + if (wdev == wiphy_to_pd_wdev(wiphy)) + padapter = wiphy_to_adapter(wiphy); + else + #endif + if (wdev_to_ndev(wdev)) + padapter = (_adapter *)rtw_netdev_priv(wdev_to_ndev(wdev)); + else { + ret = -EINVAL; + goto exit; + } +#else + struct wireless_dev *wdev; + + if (ndev == NULL) { + ret = -EINVAL; + goto exit; + } + padapter = (_adapter *)rtw_netdev_priv(ndev); + wdev = ndev_to_wdev(ndev); +#endif + + if (chan == NULL) { + ret = -EINVAL; + goto exit; + } + + rfctl = adapter_to_rfctl(padapter); + tx_ch = (u8)ieee80211_frequency_to_channel(chan->center_freq); + if (IS_CH_WAITING(rfctl)) { + #ifdef CONFIG_DFS_MASTER + if (_rtw_rfctl_overlap_radar_detect_ch(rfctl, tx_ch, CHANNEL_WIDTH_20, HAL_PRIME_CHNL_OFFSET_DONT_CARE)) { + ret = -EINVAL; + goto exit; + } + #endif + } + + dvobj = adapter_to_dvobj(padapter); + pwdev_priv = adapter_wdev_data(padapter); + + /* cookie generation */ + *cookie = pwdev_priv->mgmt_tx_cookie++; + +#ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO(FUNC_ADPT_FMT"%s len=%zu, ch=%d" + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34)) && (LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0)) + ", ch_type=%d" + #endif + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)) && (LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0)) + ", channel_type_valid=%d" + #endif + "\n", FUNC_ADPT_ARG(padapter), wdev == wiphy_to_pd_wdev(wiphy) ? " PD" : "" + , len, tx_ch + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34)) && (LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0)) + , channel_type + #endif + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34)) && (LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0)) + , channel_type_valid + #endif + ); +#endif /* CONFIG_DEBUG_CFG80211 */ + + /* indicate ack before issue frame to avoid racing with rsp frame */ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + rtw_cfg80211_mgmt_tx_status(wdev, *cookie, buf, len, ack, GFP_KERNEL); +#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34) && LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 36)) + cfg80211_action_tx_status(ndev, *cookie, buf, len, ack, GFP_KERNEL); +#endif + + frame_styp = le16_to_cpu(((struct rtw_ieee80211_hdr_3addr *)buf)->frame_ctl) & IEEE80211_FCTL_STYPE; + if (IEEE80211_STYPE_PROBE_RESP == frame_styp) { +#ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO("RTW_Tx: probe_resp tx_ch=%d, no_cck=%u, da="MAC_FMT"\n", tx_ch, no_cck, MAC_ARG(GetAddr1Ptr(buf))); +#endif /* CONFIG_DEBUG_CFG80211 */ + wait_ack = 0; + goto dump; + } + else if (frame_styp == RTW_IEEE80211_STYPE_AUTH) { + int retval = 0; + + RTW_INFO("RTW_Tx:tx_ch=%d, no_cck=%u, da="MAC_FMT"\n", tx_ch, no_cck, MAC_ARG(GetAddr1Ptr(buf))); + + retval = rtw_sae_preprocess(padapter, buf, len, _TRUE); + if (retval == 2) + goto exit; + if (retval == 0) + RTW_INFO("RTW_Tx:AUTH\n"); + dump_limit = 1; + goto dump; + } + + if (rtw_action_frame_parse(buf, len, &category, &action) == _FALSE) { + RTW_INFO(FUNC_ADPT_FMT" frame_control:0x%02x\n", FUNC_ADPT_ARG(padapter), + le16_to_cpu(((struct rtw_ieee80211_hdr_3addr *)buf)->frame_ctl)); + goto exit; + } + + RTW_INFO("RTW_Tx:tx_ch=%d, no_cck=%u, da="MAC_FMT"\n", tx_ch, no_cck, MAC_ARG(GetAddr1Ptr(buf))); +#ifdef CONFIG_P2P + type = rtw_p2p_check_frames(padapter, buf, len, _TRUE); + if (type >= 0) { + is_p2p = 1; + no_cck = 1; /* force no CCK for P2P frames */ + goto dump; + } +#endif +#ifdef CONFIG_RTW_MESH + if (MLME_IS_MESH(padapter)) { + type = rtw_mesh_check_frames_tx(padapter, &dump_buf, &dump_len); + if (type >= 0) { + dump_limit = 1; + goto dump; + } + } +#endif + if (category == RTW_WLAN_CATEGORY_PUBLIC) { + RTW_INFO("RTW_Tx:%s\n", action_public_str(action)); + switch (action) { + case ACT_PUBLIC_GAS_INITIAL_REQ: + case ACT_PUBLIC_GAS_INITIAL_RSP: + sleep_ms = 50; + retry_guarantee_ms = RTW_MAX_MGMT_TX_MS_GAS; + break; + } + } +#ifdef CONFIG_RTW_80211K + else if (category == RTW_WLAN_CATEGORY_RADIO_MEAS) + RTW_INFO("RTW_Tx: RRM Action\n"); +#endif + else + RTW_INFO("RTW_Tx:category(%u), action(%u)\n", category, action); + +dump: + + rtw_ps_deny(padapter, PS_DENY_MGNT_TX); + if (_FAIL == rtw_pwr_wakeup(padapter)) { + ret = -EFAULT; + goto cancel_ps_deny; + } + + while (1) { + dump_cnt++; + + rtw_mi_set_scan_deny(padapter, 1000); + rtw_mi_scan_abort(padapter, _TRUE); + tx_ret = rtw_mgnt_tx_cmd(padapter, tx_ch, no_cck, dump_buf, dump_len, wait_ack, RTW_CMDF_WAIT_ACK); + if (tx_ret == _SUCCESS + || (dump_cnt >= dump_limit && rtw_get_passing_time_ms(start) >= retry_guarantee_ms)) + break; + + if (sleep_ms > 0) + rtw_msleep_os(sleep_ms); + } + + if (tx_ret != _SUCCESS || dump_cnt > 1) { + RTW_INFO(FUNC_ADPT_FMT" %s (%d/%d) in %d ms\n", FUNC_ADPT_ARG(padapter), + tx_ret == _SUCCESS ? "OK" : "FAIL", dump_cnt, dump_limit, rtw_get_passing_time_ms(start)); + } + +#ifdef CONFIG_P2P + if (is_p2p) { + switch (type) { + case P2P_GO_NEGO_CONF: + if (0) { + RTW_INFO(FUNC_ADPT_FMT" Nego confirm. state=%u, status=%u, iaddr="MAC_FMT"\n" + , FUNC_ADPT_ARG(padapter), pwdev_priv->nego_info.state, pwdev_priv->nego_info.status + , MAC_ARG(pwdev_priv->nego_info.iface_addr)); + } + if (pwdev_priv->nego_info.state == 2 + && pwdev_priv->nego_info.status == 0 + && rtw_check_invalid_mac_address(pwdev_priv->nego_info.iface_addr, _FALSE) == _FALSE + ) { + _adapter *intended_iface = dvobj_get_adapter_by_addr(dvobj, pwdev_priv->nego_info.iface_addr); + + if (intended_iface) { + RTW_INFO(FUNC_ADPT_FMT" Nego confirm. Allow only "ADPT_FMT" to scan for 2000 ms\n" + , FUNC_ADPT_ARG(padapter), ADPT_ARG(intended_iface)); + /* allow only intended_iface to do scan for 2000 ms */ + rtw_mi_set_scan_deny(padapter, 2000); + rtw_clear_scan_deny(intended_iface); + } + } + break; + case P2P_INVIT_RESP: + if (pwdev_priv->invit_info.flags & BIT(0) + && pwdev_priv->invit_info.status == 0 + ) { + rtw_clear_scan_deny(padapter); + RTW_INFO(FUNC_ADPT_FMT" agree with invitation of persistent group\n", + FUNC_ADPT_ARG(padapter)); + #if !RTW_P2P_GROUP_INTERFACE + rtw_mi_buddy_set_scan_deny(padapter, 5000); + #endif + rtw_pwr_wakeup_ex(padapter, 5000); + } + break; + } + } +#endif /* CONFIG_P2P */ + +cancel_ps_deny: + rtw_ps_deny_cancel(padapter, PS_DENY_MGNT_TX); + + if (dump_buf != buf) + rtw_mfree((u8 *)dump_buf, dump_len); +exit: + return ret; +} + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 8, 0)) +static void cfg80211_rtw_mgmt_frame_register(struct wiphy *wiphy, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + struct wireless_dev *wdev, +#else + struct net_device *ndev, +#endif + u16 frame_type, bool reg) +{ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + struct net_device *ndev = wdev_to_ndev(wdev); +#endif + _adapter *adapter; + struct rtw_wdev_priv *pwdev_priv; + + if (ndev == NULL) + goto exit; + + adapter = (_adapter *)rtw_netdev_priv(ndev); + pwdev_priv = adapter_wdev_data(adapter); + +#ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO(FUNC_ADPT_FMT" frame_type:%x, reg:%d\n", FUNC_ADPT_ARG(adapter), + frame_type, reg); +#endif + + switch (frame_type) { + case IEEE80211_STYPE_AUTH: /* 0x00B0 */ + if (reg > 0) + SET_CFG80211_REPORT_MGMT(pwdev_priv, IEEE80211_STYPE_AUTH); + else + CLR_CFG80211_REPORT_MGMT(pwdev_priv, IEEE80211_STYPE_AUTH); + break; +#ifdef not_yet + case IEEE80211_STYPE_PROBE_REQ: /* 0x0040 */ + if (reg > 0) + SET_CFG80211_REPORT_MGMT(pwdev_priv, IEEE80211_STYPE_PROBE_REQ); + else + CLR_CFG80211_REPORT_MGMT(pwdev_priv, IEEE80211_STYPE_PROBE_REQ); + break; + case IEEE80211_STYPE_ACTION: /* 0x00D0 */ + if (reg > 0) + SET_CFG80211_REPORT_MGMT(pwdev_priv, IEEE80211_STYPE_ACTION); + else + CLR_CFG80211_REPORT_MGMT(pwdev_priv, IEEE80211_STYPE_ACTION); + break; +#endif + default: + break; + } + +exit: + return; +} +#else +static void cfg80211_rtw_update_mgmt_frame_register( + struct wiphy *wiphy, + struct wireless_dev *wdev, + struct mgmt_frame_regs *upd) +{ + struct net_device *ndev; + _adapter *padapter; + struct rtw_wdev_priv *pwdev_priv; + u32 rtw_stypes_mask = 0; + u32 rtw_mstypes_mask = 0; + + ndev = wdev_to_ndev(wdev); + + if (ndev == NULL) + goto exit; + + padapter = (_adapter *)rtw_netdev_priv(ndev); + pwdev_priv = adapter_wdev_data(padapter); + + rtw_stypes_mask = BIT(IEEE80211_STYPE_AUTH >> 4); + +#ifdef CONFIG_DEBUG_CFG80211 + RTW_INFO(FUNC_ADPT_FMT " global_stypes:0x%08x interface_stypes:0x%08x\n", + FUNC_ADPT_ARG(padapter), upd->global_stypes, upd->interface_stypes); + RTW_INFO(FUNC_ADPT_FMT " global_mcast_stypes:0x%08x interface_mcast_stypes:0x%08x\n", + FUNC_ADPT_ARG(padapter), upd->global_mcast_stypes, upd->interface_mcast_stypes); + RTW_INFO(FUNC_ADPT_FMT " old_regs:0x%08x new_regs:0x%08x\n", + FUNC_ADPT_ARG(padapter), pwdev_priv->mgmt_regs, + (upd->interface_stypes & rtw_stypes_mask)); +#endif + if (pwdev_priv->mgmt_regs != + (upd->interface_stypes & rtw_stypes_mask)) { + pwdev_priv->mgmt_regs = (upd->interface_stypes & rtw_stypes_mask); + } + +exit: + return; +} +#endif + +#if defined(CONFIG_TDLS) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)) +static int cfg80211_rtw_tdls_mgmt(struct wiphy *wiphy, + struct net_device *ndev, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)) + const u8 *peer, +#else + u8 *peer, +#endif + u8 action_code, + u8 dialog_token, + u16 status_code, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0)) + u32 peer_capability, +#endif +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)) + bool initiator, +#endif + const u8 *buf, + size_t len) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv; + struct mlme_ext_info *pmlmeinfo = &pmlmeext->mlmext_info; + int ret = 0; + struct tdls_txmgmt txmgmt; + + if (hal_chk_wl_func(padapter, WL_FUNC_TDLS) == _FALSE) { + RTW_INFO("Discard tdls action:%d, since hal doesn't support tdls\n", action_code); + goto discard; + } + + if (rtw_is_tdls_enabled(padapter) == _FALSE) { + RTW_INFO("TDLS is not enabled\n"); + goto discard; + } + + if (rtw_tdls_is_driver_setup(padapter)) { + RTW_INFO("Discard tdls action:%d, let driver to set up direct link\n", action_code); + goto discard; + } + + _rtw_memset(&txmgmt, 0x00, sizeof(struct tdls_txmgmt)); + _rtw_memcpy(txmgmt.peer, peer, ETH_ALEN); + txmgmt.action_code = action_code; + txmgmt.dialog_token = dialog_token; + txmgmt.status_code = status_code; + txmgmt.len = len; + txmgmt.buf = (u8 *)rtw_malloc(txmgmt.len); + if (txmgmt.buf == NULL) { + ret = -ENOMEM; + goto bad; + } + _rtw_memcpy(txmgmt.buf, (void *)buf, txmgmt.len); + + /* Debug purpose */ +#if 1 + RTW_INFO("%s %d\n", __FUNCTION__, __LINE__); + RTW_INFO("peer:"MAC_FMT", action code:%d, dialog:%d, status code:%d\n", + MAC_ARG(txmgmt.peer), txmgmt.action_code, + txmgmt.dialog_token, txmgmt.status_code); + if (txmgmt.len > 0) { + int i = 0; + for (; i < len; i++) + printk("%02x ", *(txmgmt.buf + i)); + RTW_INFO("len:%d\n", (u32)txmgmt.len); + } +#endif + + switch (txmgmt.action_code) { + case TDLS_SETUP_REQUEST: + issue_tdls_setup_req(padapter, &txmgmt, _TRUE); + break; + case TDLS_SETUP_RESPONSE: + issue_tdls_setup_rsp(padapter, &txmgmt); + break; + case TDLS_SETUP_CONFIRM: + issue_tdls_setup_cfm(padapter, &txmgmt); + break; + case TDLS_TEARDOWN: + issue_tdls_teardown(padapter, &txmgmt, _TRUE); + break; + case TDLS_DISCOVERY_REQUEST: + issue_tdls_dis_req(padapter, &txmgmt); + break; + case TDLS_DISCOVERY_RESPONSE: + issue_tdls_dis_rsp(padapter, &txmgmt, pmlmeinfo->enc_algo ? _TRUE : _FALSE); + break; + } + +bad: + if (txmgmt.buf) + rtw_mfree(txmgmt.buf, txmgmt.len); + +discard: + return ret; +} + +static int cfg80211_rtw_tdls_oper(struct wiphy *wiphy, + struct net_device *ndev, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)) + const u8 *peer, +#else + u8 *peer, +#endif + enum nl80211_tdls_operation oper) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + struct tdls_info *ptdlsinfo = &padapter->tdlsinfo; + struct tdls_txmgmt txmgmt; + struct sta_info *ptdls_sta = NULL; + + RTW_INFO(FUNC_NDEV_FMT", nl80211_tdls_operation:%d\n", FUNC_NDEV_ARG(ndev), oper); + + if (hal_chk_wl_func(padapter, WL_FUNC_TDLS) == _FALSE) { + RTW_INFO("Discard tdls oper:%d, since hal doesn't support tdls\n", oper); + return 0; + } + + if (rtw_is_tdls_enabled(padapter) == _FALSE) { + RTW_INFO("TDLS is not enabled\n"); + return 0; + } + +#ifdef CONFIG_LPS + rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_LEAVE, 0); +#endif /* CONFIG_LPS */ + + _rtw_memset(&txmgmt, 0x00, sizeof(struct tdls_txmgmt)); + if (peer) + _rtw_memcpy(txmgmt.peer, peer, ETH_ALEN); + + if (rtw_tdls_is_driver_setup(padapter)) { + /* these two cases are done by driver itself */ + if (oper == NL80211_TDLS_ENABLE_LINK || oper == NL80211_TDLS_DISABLE_LINK) + return 0; + } + + switch (oper) { + case NL80211_TDLS_DISCOVERY_REQ: + issue_tdls_dis_req(padapter, &txmgmt); + break; + case NL80211_TDLS_SETUP: +#ifdef CONFIG_WFD + if (_AES_ != padapter->securitypriv.dot11PrivacyAlgrthm) { + if (padapter->wdinfo.wfd_tdls_weaksec == _TRUE) + issue_tdls_setup_req(padapter, &txmgmt, _TRUE); + else + RTW_INFO("[%s] Current link is not AES, SKIP sending the tdls setup request!!\n", __FUNCTION__); + } else +#endif /* CONFIG_WFD */ + { + issue_tdls_setup_req(padapter, &txmgmt, _TRUE); + } + break; + case NL80211_TDLS_TEARDOWN: + ptdls_sta = rtw_get_stainfo(&(padapter->stapriv), txmgmt.peer); + if (ptdls_sta != NULL) { + txmgmt.status_code = _RSON_TDLS_TEAR_UN_RSN_; + issue_tdls_teardown(padapter, &txmgmt, _TRUE); + } else + RTW_INFO("TDLS peer not found\n"); + break; + case NL80211_TDLS_ENABLE_LINK: + RTW_INFO(FUNC_NDEV_FMT", NL80211_TDLS_ENABLE_LINK;mac:"MAC_FMT"\n", FUNC_NDEV_ARG(ndev), MAC_ARG(peer)); + ptdls_sta = rtw_get_stainfo(&(padapter->stapriv), (u8 *)peer); + if (ptdls_sta != NULL) { + rtw_tdls_set_link_established(padapter, _TRUE); + ptdls_sta->tdls_sta_state |= TDLS_LINKED_STATE; + ptdls_sta->state |= WIFI_ASOC_STATE; + rtw_tdls_cmd(padapter, txmgmt.peer, TDLS_ESTABLISHED); + } + break; + case NL80211_TDLS_DISABLE_LINK: + RTW_INFO(FUNC_NDEV_FMT", NL80211_TDLS_DISABLE_LINK;mac:"MAC_FMT"\n", FUNC_NDEV_ARG(ndev), MAC_ARG(peer)); + ptdls_sta = rtw_get_stainfo(&(padapter->stapriv), (u8 *)peer); + if (ptdls_sta != NULL) { + rtw_tdls_teardown_pre_hdl(padapter, ptdls_sta); + rtw_tdls_cmd(padapter, (u8 *)peer, TDLS_TEARDOWN_STA_LOCALLY_POST); + } + break; + } + return 0; +} +#endif /* CONFIG_TDLS */ + +#if defined(CONFIG_RTW_MESH) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)) + +#if DBG_RTW_CFG80211_MESH_CONF +#define LEGACY_RATES_STR_LEN (RTW_G_RATES_NUM * 5 + 1) +int get_legacy_rates_str(struct wiphy *wiphy, enum nl80211_band band, u32 mask, char *buf) +{ + int i; + int cnt = 0; + + for (i = 0; i < wiphy->bands[band]->n_bitrates; i++) { + if (mask & BIT(i)) { + cnt += snprintf(buf + cnt, LEGACY_RATES_STR_LEN - cnt -1, "%d.%d " + , wiphy->bands[band]->bitrates[i].bitrate / 10 + , wiphy->bands[band]->bitrates[i].bitrate % 10); + if (cnt >= LEGACY_RATES_STR_LEN - 1) + break; + } + } + + return cnt; +} + +void dump_mesh_setup(void *sel, struct wiphy *wiphy, const struct mesh_setup *setup) +{ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + struct cfg80211_chan_def *chdef = (struct cfg80211_chan_def *)(&setup->chandef); +#endif + struct ieee80211_channel *chan; +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + chan = (struct ieee80211_channel *)chdef->chan; +#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + chan = (struct ieee80211_channel *)setup->channel; +#endif + + RTW_PRINT_SEL(sel, "mesh_id:\"%s\", len:%u\n", setup->mesh_id, setup->mesh_id_len); +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)) + RTW_PRINT_SEL(sel, "sync_method:%u\n", setup->sync_method); +#endif + RTW_PRINT_SEL(sel, "path_sel_proto:%u, path_metric:%u\n", setup->path_sel_proto, setup->path_metric); +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) + RTW_PRINT_SEL(sel, "auth_id:%u\n", setup->auth_id); +#endif +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)) + if (setup->ie && setup->ie_len) { + RTW_PRINT_SEL(sel, "ie:%p, len:%u\n", setup->ie, setup->ie_len); + dump_ies(RTW_DBGDUMP, setup->ie, setup->ie_len); + } +#else + if (setup->vendor_ie && setup->vendor_ie_len) { + RTW_PRINT_SEL(sel, "ie:%p, len:%u\n", setup->vendor_ie, setup->vendor_ie_len); + dump_ies(RTW_DBGDUMP, setup->vendor_ie, setup->vendor_ie_len); + } +#endif +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)) + RTW_PRINT_SEL(sel, "is_authenticated:%d, is_secure:%d\n", setup->is_authenticated, setup->is_secure); +#endif +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)) + RTW_PRINT_SEL(sel, "user_mpm:%d\n", setup->user_mpm); +#endif +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) + RTW_PRINT_SEL(sel, "dtim_period:%u, beacon_interval:%u\n", setup->dtim_period, setup->beacon_interval); +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + RTW_PRINT_SEL(sel, "center_freq:%u, ch:%u, width:%s, cfreq1:%u, cfreq2:%u\n" + , chan->center_freq, chan->hw_value, nl80211_chan_width_str(chdef->width), chdef->center_freq1, chdef->center_freq2); +#else + RTW_PRINT_SEL(sel, "center_freq:%u, ch:%u, channel_type:%s\n" + , chan->center_freq, chan->hw_value, nl80211_channel_type_str(setup->channel_type)); +#endif +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)) + if (setup->mcast_rate[chan->band]) { + RTW_PRINT_SEL(sel, "mcast_rate:%d.%d\n" + , wiphy->bands[chan->band]->bitrates[setup->mcast_rate[chan->band] - 1].bitrate / 10 + , wiphy->bands[chan->band]->bitrates[setup->mcast_rate[chan->band] - 1].bitrate % 10 + ); + } +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) + if (setup->basic_rates) { + char buf[LEGACY_RATES_STR_LEN] = {0}; + + get_legacy_rates_str(wiphy, chan->band, setup->basic_rates, buf); + RTW_PRINT_SEL(sel, "basic_rates:%s\n", buf); + } +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)) + if (setup->beacon_rate.control[chan->band].legacy) { + char buf[LEGACY_RATES_STR_LEN] = {0}; + + get_legacy_rates_str(wiphy, chan->band, setup->beacon_rate.control[chan->band].legacy, buf); + RTW_PRINT_SEL(sel, "beacon_rate.legacy:%s\n", buf); + } + if (*((u32 *)&(setup->beacon_rate.control[chan->band].ht_mcs[0])) + || *((u32 *)&(setup->beacon_rate.control[chan->band].ht_mcs[4])) + || *((u16 *)&(setup->beacon_rate.control[chan->band].ht_mcs[8])) + ) { + RTW_PRINT_SEL(sel, "beacon_rate.ht_mcs:"HT_RX_MCS_BMP_FMT"\n" + , HT_RX_MCS_BMP_ARG(setup->beacon_rate.control[chan->band].ht_mcs)); + } + + if (setup->beacon_rate.control[chan->band].vht_mcs[0] + || setup->beacon_rate.control[chan->band].vht_mcs[1] + || setup->beacon_rate.control[chan->band].vht_mcs[2] + || setup->beacon_rate.control[chan->band].vht_mcs[3] + ) { + int i; + + for (i = 0; i < 4; i++) {/* parsing up to 4SS */ + u16 mcs_mask = setup->beacon_rate.control[chan->band].vht_mcs[i]; + + RTW_PRINT_SEL(sel, "beacon_rate.vht_mcs[%d]:%s\n", i + , mcs_mask == 0x00FF ? "0~7" : mcs_mask == 0x01FF ? "0~8" : mcs_mask == 0x03FF ? "0~9" : "invalid"); + } + } + + if (setup->beacon_rate.control[chan->band].gi) { + RTW_PRINT_SEL(sel, "beacon_rate.gi:%s\n" + , setup->beacon_rate.control[chan->band].gi == NL80211_TXRATE_FORCE_SGI ? "SGI" : + setup->beacon_rate.control[chan->band].gi == NL80211_TXRATE_FORCE_LGI ? "LGI" : "invalid" + ); + } +#endif +} + +void dump_mesh_config(void *sel, const struct mesh_config *conf) +{ + RTW_PRINT_SEL(sel, "dot11MeshRetryTimeout:%u\n", conf->dot11MeshRetryTimeout); + RTW_PRINT_SEL(sel, "dot11MeshConfirmTimeout:%u\n", conf->dot11MeshConfirmTimeout); + RTW_PRINT_SEL(sel, "dot11MeshHoldingTimeout:%u\n", conf->dot11MeshHoldingTimeout); + RTW_PRINT_SEL(sel, "dot11MeshMaxPeerLinks:%u\n", conf->dot11MeshMaxPeerLinks); + RTW_PRINT_SEL(sel, "dot11MeshMaxRetries:%u\n", conf->dot11MeshMaxRetries); + RTW_PRINT_SEL(sel, "dot11MeshTTL:%u\n", conf->dot11MeshTTL); + RTW_PRINT_SEL(sel, "element_ttl:%u\n", conf->element_ttl); + RTW_PRINT_SEL(sel, "auto_open_plinks:%d\n", conf->auto_open_plinks); + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)) + RTW_PRINT_SEL(sel, "dot11MeshNbrOffsetMaxNeighbor:%u\n", conf->dot11MeshNbrOffsetMaxNeighbor); +#endif + + RTW_PRINT_SEL(sel, "dot11MeshHWMPmaxPREQretries:%u\n", conf->dot11MeshHWMPmaxPREQretries); + RTW_PRINT_SEL(sel, "path_refresh_time:%u\n", conf->path_refresh_time); + RTW_PRINT_SEL(sel, "min_discovery_timeout:%u\n", conf->min_discovery_timeout); + RTW_PRINT_SEL(sel, "dot11MeshHWMPactivePathTimeout:%u\n", conf->dot11MeshHWMPactivePathTimeout); + RTW_PRINT_SEL(sel, "dot11MeshHWMPpreqMinInterval:%u\n", conf->dot11MeshHWMPpreqMinInterval); +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)) + RTW_PRINT_SEL(sel, "dot11MeshHWMPperrMinInterval:%u\n", conf->dot11MeshHWMPperrMinInterval); +#endif + RTW_PRINT_SEL(sel, "dot11MeshHWMPnetDiameterTraversalTime:%u\n", conf->dot11MeshHWMPnetDiameterTraversalTime); + RTW_PRINT_SEL(sel, "dot11MeshHWMPRootMode:%u\n", conf->dot11MeshHWMPRootMode); +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)) + RTW_PRINT_SEL(sel, "dot11MeshHWMPRannInterval:%u\n", conf->dot11MeshHWMPRannInterval); + RTW_PRINT_SEL(sel, "dot11MeshGateAnnouncementProtocol:%d\n", conf->dot11MeshGateAnnouncementProtocol); +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)) + RTW_PRINT_SEL(sel, "dot11MeshForwarding:%d\n", conf->dot11MeshForwarding); + RTW_PRINT_SEL(sel, "rssi_threshold:%d\n", conf->rssi_threshold); +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)) + RTW_PRINT_SEL(sel, "ht_opmode:0x%04x\n", conf->ht_opmode); +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + RTW_PRINT_SEL(sel, "dot11MeshHWMPactivePathToRootTimeout:%u\n", conf->dot11MeshHWMPactivePathToRootTimeout); + RTW_PRINT_SEL(sel, "dot11MeshHWMProotInterval:%u\n", conf->dot11MeshHWMProotInterval); + RTW_PRINT_SEL(sel, "dot11MeshHWMPconfirmationInterval:%u\n", conf->dot11MeshHWMPconfirmationInterval); +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) + RTW_PRINT_SEL(sel, "power_mode:%s\n", nl80211_mesh_power_mode_str(conf->power_mode)); + RTW_PRINT_SEL(sel, "dot11MeshAwakeWindowDuration:%u\n", conf->dot11MeshAwakeWindowDuration); +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) + RTW_PRINT_SEL(sel, "plink_timeout:%u\n", conf->plink_timeout); +#endif +} +#endif /* DBG_RTW_CFG80211_MESH_CONF */ + +static void rtw_cfg80211_mesh_info_set_profile(struct rtw_mesh_info *minfo, const struct mesh_setup *setup) +{ + _rtw_memcpy(minfo->mesh_id, setup->mesh_id, setup->mesh_id_len); + minfo->mesh_id_len = setup->mesh_id_len; + minfo->mesh_pp_id = setup->path_sel_proto; + minfo->mesh_pm_id = setup->path_metric; + minfo->mesh_cc_id = 0; +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)) + minfo->mesh_sp_id = setup->sync_method; +#endif +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) + minfo->mesh_auth_id = setup->auth_id; +#else + if (setup->is_authenticated) { + u8 *rsn_ie; + sint rsn_ie_len; + struct rsne_info info; + u8 *akm; + u8 AKM_SUITE_SAE[4] = {0x00, 0x0F, 0xAC, 0x08}; + + rsn_ie = rtw_get_ie(setup->ie, WLAN_EID_RSN, &rsn_ie_len, setup->ie_len); + if (!rsn_ie || !rsn_ie_len) { + rtw_warn_on(1); + return; + } + + if (rtw_rsne_info_parse(rsn_ie, rsn_ie_len + 2, &info) != _SUCCESS) { + rtw_warn_on(1); + return; + } + + if (!info.akm_list || !info.akm_cnt) { + rtw_warn_on(1); + return; + } + + akm = info.akm_list; + while (akm < info.akm_list + info.akm_cnt * 4) { + if (_rtw_memcmp(akm, AKM_SUITE_SAE, 4) == _TRUE) { + minfo->mesh_auth_id = 0x01; + break; + } + } + + if (!minfo->mesh_auth_id) { + rtw_warn_on(1); + return; + } + } +#endif +} + +static inline bool chk_mesh_attr(enum nl80211_meshconf_params parm, u32 mask) +{ + return (mask >> (parm - 1)) & 0x1; +} + +static void rtw_cfg80211_mesh_cfg_set(_adapter *adapter, const struct mesh_config *conf, u32 mask) +{ + struct rtw_mesh_cfg *mcfg = &adapter->mesh_cfg; + +#if 0 /* driver MPM */ + if (chk_mesh_attr(NL80211_MESHCONF_RETRY_TIMEOUT, mask)); + if (chk_mesh_attr(NL80211_MESHCONF_CONFIRM_TIMEOUT, mask)); + if (chk_mesh_attr(NL80211_MESHCONF_HOLDING_TIMEOUT, mask)); + if (chk_mesh_attr(NL80211_MESHCONF_MAX_PEER_LINKS, mask)); + if (chk_mesh_attr(NL80211_MESHCONF_MAX_RETRIES, mask)); +#endif + + if (chk_mesh_attr(NL80211_MESHCONF_TTL, mask)) + mcfg->dot11MeshTTL = conf->dot11MeshTTL; + if (chk_mesh_attr(NL80211_MESHCONF_ELEMENT_TTL, mask)) + mcfg->element_ttl = conf->element_ttl; + +#if 0 /* driver MPM */ + if (chk_mesh_attr(NL80211_MESHCONF_AUTO_OPEN_PLINKS, mask)); +#endif + +#if 0 /* TBD: synchronization */ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)) + if (chk_mesh_attr(NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR, mask)); +#endif +#endif + + if (chk_mesh_attr(NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES, mask)) + mcfg->dot11MeshHWMPmaxPREQretries = conf->dot11MeshHWMPmaxPREQretries; + if (chk_mesh_attr(NL80211_MESHCONF_PATH_REFRESH_TIME, mask)) + mcfg->path_refresh_time = conf->path_refresh_time; + if (chk_mesh_attr(NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT, mask)) + mcfg->min_discovery_timeout = conf->min_discovery_timeout; + if (chk_mesh_attr(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT, mask)) + mcfg->dot11MeshHWMPactivePathTimeout = conf->dot11MeshHWMPactivePathTimeout; + if (chk_mesh_attr(NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL, mask)) + mcfg->dot11MeshHWMPpreqMinInterval = conf->dot11MeshHWMPpreqMinInterval; +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)) + if (chk_mesh_attr(NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL, mask)) + mcfg->dot11MeshHWMPperrMinInterval = conf->dot11MeshHWMPperrMinInterval; +#endif + if (chk_mesh_attr(NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME, mask)) + mcfg->dot11MeshHWMPnetDiameterTraversalTime = conf->dot11MeshHWMPnetDiameterTraversalTime; + + if (chk_mesh_attr(NL80211_MESHCONF_HWMP_ROOTMODE, mask)) + mcfg->dot11MeshHWMPRootMode = conf->dot11MeshHWMPRootMode; +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)) + if (chk_mesh_attr(NL80211_MESHCONF_GATE_ANNOUNCEMENTS, mask)) + mcfg->dot11MeshGateAnnouncementProtocol = conf->dot11MeshGateAnnouncementProtocol; + /* our current gate annc implementation rides on root annc with gate annc bit in PREQ flags */ + if (mcfg->dot11MeshGateAnnouncementProtocol + && mcfg->dot11MeshHWMPRootMode <= RTW_IEEE80211_ROOTMODE_ROOT + ) { + mcfg->dot11MeshHWMPRootMode = RTW_IEEE80211_PROACTIVE_RANN; + RTW_INFO(ADPT_FMT" enable PROACTIVE_RANN becaue gate annc is needed\n", ADPT_ARG(adapter)); + } + if (chk_mesh_attr(NL80211_MESHCONF_HWMP_RANN_INTERVAL, mask)) + mcfg->dot11MeshHWMPRannInterval = conf->dot11MeshHWMPRannInterval; +#endif +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)) + if (chk_mesh_attr(NL80211_MESHCONF_FORWARDING, mask)) + mcfg->dot11MeshForwarding = conf->dot11MeshForwarding; + + if (chk_mesh_attr(NL80211_MESHCONF_RSSI_THRESHOLD, mask)) + mcfg->rssi_threshold = conf->rssi_threshold; +#endif + +#if 0 /* controlled by driver */ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)) + if (chk_mesh_attr(NL80211_MESHCONF_HT_OPMODE, mask)); +#endif +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + if (chk_mesh_attr(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT, mask)) + mcfg->dot11MeshHWMPactivePathToRootTimeout = conf->dot11MeshHWMPactivePathToRootTimeout; + if (chk_mesh_attr(NL80211_MESHCONF_HWMP_ROOT_INTERVAL, mask)) + mcfg->dot11MeshHWMProotInterval = conf->dot11MeshHWMProotInterval; + if (chk_mesh_attr(NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL, mask)) + mcfg->dot11MeshHWMPconfirmationInterval = conf->dot11MeshHWMPconfirmationInterval; +#endif + +#if 0 /* TBD */ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) + if (chk_mesh_attr(NL80211_MESHCONF_POWER_MODE, mask)); + if (chk_mesh_attr(NL80211_MESHCONF_AWAKE_WINDOW, mask)); +#endif +#endif + +#if 0 /* driver MPM */ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) + if (chk_mesh_attr(NL80211_MESHCONF_PLINK_TIMEOUT, mask)); +#endif +#endif +} + +u8 *rtw_cfg80211_construct_mesh_beacon_ies(struct wiphy *wiphy, _adapter *adapter + , const struct mesh_config *conf, const struct mesh_setup *setup + , uint *ies_len) +{ + struct rtw_mesh_info *minfo = &adapter->mesh_info; + struct rtw_mesh_cfg *mcfg = &adapter->mesh_cfg; +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + struct cfg80211_chan_def *chdef = (struct cfg80211_chan_def *)(&setup->chandef); +#endif + struct ieee80211_channel *chan; + u8 ch, bw, offset; +#endif + uint len; + u8 n_bitrates; + u8 ht = 0; + u8 vht = 0; + u8 *rsn_ie = NULL; + sint rsn_ie_len = 0; + u8 *ies = NULL, *c; + u8 supported_rates[RTW_G_RATES_NUM] = {0}; + int i; + + *ies_len = 0; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + chan = (struct ieee80211_channel *)chdef->chan; +#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + chan = (struct ieee80211_channel *)setup->channel; +#endif + + n_bitrates = wiphy->bands[chan->band]->n_bitrates; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + rtw_get_chbw_from_cfg80211_chan_def(chdef, &ht, &ch, &bw, &offset); +#else + rtw_get_chbw_from_nl80211_channel_type(chan, setup->channel_type, &ht, &ch, &bw, &offset); +#endif + if (!ch) + goto exit; + +#if defined(CONFIG_80211AC_VHT) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + vht = ht && ch > 14 && bw >= CHANNEL_WIDTH_80; /* VHT40/VHT20? */ +#endif + + RTW_INFO(FUNC_ADPT_FMT" => ch:%u,%u,%u, ht:%u, vht:%u\n" + , FUNC_ADPT_ARG(adapter), ch, bw, offset, ht, vht); +#endif + + rsn_ie = rtw_get_ie(setup->ie, WLAN_EID_RSN, &rsn_ie_len, setup->ie_len); + if (rsn_ie && !rsn_ie_len) { + rtw_warn_on(1); + rsn_ie = NULL; + } + + len = _BEACON_IE_OFFSET_ + + 2 /* 0-length SSID */ + + (n_bitrates >= 8 ? 8 : n_bitrates) + 2 /* Supported Rates */ + + 3 /* DS parameter set */ + + 6 /* TIM */ + + (n_bitrates > 8 ? n_bitrates - 8 + 2 : 0) /* Extended Supported Rates */ + + (rsn_ie ? rsn_ie_len + 2 : 0) /* RSN */ + #if defined(CONFIG_80211N_HT) + + (ht ? HT_CAP_IE_LEN + 2 + HT_OP_IE_LEN + 2 : 0) /* HT */ + #endif + #if defined(CONFIG_80211AC_VHT) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + + (vht ? VHT_CAP_IE_LEN + 2 + VHT_OP_IE_LEN + 2 : 0) /* VHT */ + #endif + + minfo->mesh_id_len + 2 /* Mesh ID */ + + 9 /* Mesh configuration */ + ; + + ies = rtw_zmalloc(len); + if (!ies) + goto exit; + + /* timestamp */ + c = ies + 8; + + /* beacon interval */ + RTW_PUT_LE16(c , setup->beacon_interval); + c += 2; + + /* capability */ + if (rsn_ie) + *((u16 *)c) |= cpu_to_le16(cap_Privacy); + c += 2; + + /* SSID */ + c = rtw_set_ie(c, WLAN_EID_SSID, 0, NULL, NULL); + + /* Supported Rates */ + for (i = 0; i < n_bitrates; i++) { + supported_rates[i] = wiphy->bands[chan->band]->bitrates[i].bitrate / 5; + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) + if (setup->basic_rates & BIT(i)) + #else + if (rtw_is_basic_rate_mix(supported_rates[i])) + #endif + supported_rates[i] |= IEEE80211_BASIC_RATE_MASK; + } + c = rtw_set_ie(c, WLAN_EID_SUPP_RATES, (n_bitrates >= 8 ? 8 : n_bitrates), supported_rates, NULL); + + /* DS parameter set */ + c = rtw_set_ie(c, WLAN_EID_DS_PARAMS, 1, &ch, NULL); + + /* TIM */ + *c = WLAN_EID_TIM; + *(c + 1) = 4; + c += 6; + //c = rtw_set_ie(c, _TIM_IE_, 4, NULL, NULL); + + /* Extended Supported Rates */ + if (n_bitrates > 8) + c = rtw_set_ie(c, WLAN_EID_EXT_SUPP_RATES, n_bitrates - 8, supported_rates + 8, NULL); + + /* RSN */ + if (rsn_ie) + c = rtw_set_ie(c, WLAN_EID_RSN, rsn_ie_len, rsn_ie + 2, NULL); + +#if defined(CONFIG_80211N_HT) + if (ht) { + struct ieee80211_sta_ht_cap *sta_ht_cap = &wiphy->bands[chan->band]->ht_cap; + u8 ht_cap[HT_CAP_IE_LEN]; + u8 ht_op[HT_OP_IE_LEN]; + + _rtw_memset(ht_cap, 0, HT_CAP_IE_LEN); + _rtw_memset(ht_op, 0, HT_OP_IE_LEN); + + /* WLAN_EID_HT_CAP */ + RTW_PUT_LE16(HT_CAP_ELE_CAP_INFO(ht_cap), sta_ht_cap->cap); + SET_HT_CAP_ELE_MAX_AMPDU_LEN_EXP(ht_cap, sta_ht_cap->ampdu_factor); + SET_HT_CAP_ELE_MIN_MPDU_S_SPACE(ht_cap, sta_ht_cap->ampdu_density); + _rtw_memcpy(HT_CAP_ELE_SUP_MCS_SET(ht_cap), &sta_ht_cap->mcs, 16); + c = rtw_set_ie(c, WLAN_EID_HT_CAP, HT_CAP_IE_LEN, ht_cap, NULL); + + /* WLAN_EID_HT_OPERATION */ + SET_HT_OP_ELE_PRI_CHL(ht_op, ch); + switch (offset) { + case HAL_PRIME_CHNL_OFFSET_LOWER: + SET_HT_OP_ELE_2ND_CHL_OFFSET(ht_op, SCA); + break; + case HAL_PRIME_CHNL_OFFSET_UPPER: + SET_HT_OP_ELE_2ND_CHL_OFFSET(ht_op, SCB); + break; + case HAL_PRIME_CHNL_OFFSET_DONT_CARE: + default: + SET_HT_OP_ELE_2ND_CHL_OFFSET(ht_op, SCN); + break; + } + if (bw >= CHANNEL_WIDTH_40) + SET_HT_OP_ELE_STA_CHL_WIDTH(ht_op, 1); + else + SET_HT_OP_ELE_STA_CHL_WIDTH(ht_op, 0); + c = rtw_set_ie(c, WLAN_EID_HT_OPERATION, HT_OP_IE_LEN, ht_op, NULL); + } +#endif /* defined(CONFIG_80211N_HT) */ + +#if defined(CONFIG_80211AC_VHT) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + if (vht) { + struct ieee80211_sta_vht_cap *sta_vht_cap = &wiphy->bands[chan->band]->vht_cap; + u8 vht_cap[VHT_CAP_IE_LEN]; + u8 vht_op[VHT_OP_IE_LEN]; + u8 cch = rtw_get_center_ch(ch, bw, offset); + + _rtw_memset(vht_op, 0, VHT_OP_IE_LEN); + + /* WLAN_EID_VHT_CAPABILITY */ + _rtw_memcpy(vht_cap, &sta_vht_cap->cap, 4); + _rtw_memcpy(vht_cap + 4, &sta_vht_cap->vht_mcs, 8); + c = rtw_set_ie(c, WLAN_EID_VHT_CAPABILITY, VHT_CAP_IE_LEN, vht_cap, NULL); + + /* WLAN_EID_VHT_OPERATION */ + if (bw < CHANNEL_WIDTH_80) { + SET_VHT_OPERATION_ELE_CHL_WIDTH(vht_op, 0); + SET_VHT_OPERATION_ELE_CHL_CENTER_FREQ1(vht_op, 0); + SET_VHT_OPERATION_ELE_CHL_CENTER_FREQ2(vht_op, 0); + } else if (bw == CHANNEL_WIDTH_80) { + SET_VHT_OPERATION_ELE_CHL_WIDTH(vht_op, 1); + SET_VHT_OPERATION_ELE_CHL_CENTER_FREQ1(vht_op, cch); + SET_VHT_OPERATION_ELE_CHL_CENTER_FREQ2(vht_op, 0); + } else { + RTW_ERR(FUNC_ADPT_FMT" unsupported BW:%u\n", FUNC_ADPT_ARG(adapter), bw); + rtw_warn_on(1); + rtw_mfree(ies, len); + goto exit; + } + + /* Hard code 1 stream, MCS0-7 is a min Basic VHT MCS rates */ + vht_op[3] = 0xfc; + vht_op[4] = 0xff; + c = rtw_set_ie(c, WLAN_EID_VHT_OPERATION, VHT_OP_IE_LEN, vht_op, NULL); + } +#endif /* defined(CONFIG_80211AC_VHT) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) */ + + /* Mesh ID */ + c = rtw_set_ie_mesh_id(c, NULL, minfo->mesh_id, minfo->mesh_id_len); + + /* Mesh configuration */ + c = rtw_set_ie_mesh_config(c, NULL + , minfo->mesh_pp_id + , minfo->mesh_pm_id + , minfo->mesh_cc_id + , minfo->mesh_sp_id + , minfo->mesh_auth_id + , 0, 0, 0 + , 1 + , 0, 0 + , mcfg->dot11MeshForwarding + , 0, 0, 0 + ); + +#if DBG_RTW_CFG80211_MESH_CONF + RTW_INFO(FUNC_ADPT_FMT" ies_len:%u\n", FUNC_ADPT_ARG(adapter), len); + dump_ies(RTW_DBGDUMP, ies + _BEACON_IE_OFFSET_, len - _BEACON_IE_OFFSET_); +#endif + +exit: + if (ies) + *ies_len = len; + return ies; +} + +static int cfg80211_rtw_get_mesh_config(struct wiphy *wiphy, struct net_device *dev + , struct mesh_config *conf) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rtw_mesh_cfg *mesh_cfg = &adapter->mesh_cfg; + int ret = 0; + + RTW_INFO(FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(adapter)); + + /* driver MPM */ + conf->dot11MeshRetryTimeout = 0; + conf->dot11MeshConfirmTimeout = 0; + conf->dot11MeshHoldingTimeout = 0; + conf->dot11MeshMaxPeerLinks = mesh_cfg->max_peer_links; + conf->dot11MeshMaxRetries = 0; + + conf->dot11MeshTTL = mesh_cfg->dot11MeshTTL; + conf->element_ttl = mesh_cfg->element_ttl; + + /* driver MPM */ + conf->auto_open_plinks = 0; + + /* TBD: synchronization */ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)) + conf->dot11MeshNbrOffsetMaxNeighbor = 0; +#endif + + conf->dot11MeshHWMPmaxPREQretries = mesh_cfg->dot11MeshHWMPmaxPREQretries; + conf->path_refresh_time = mesh_cfg->path_refresh_time; + conf->min_discovery_timeout = mesh_cfg->min_discovery_timeout; + conf->dot11MeshHWMPactivePathTimeout = mesh_cfg->dot11MeshHWMPactivePathTimeout; + conf->dot11MeshHWMPpreqMinInterval = mesh_cfg->dot11MeshHWMPpreqMinInterval; +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)) + conf->dot11MeshHWMPperrMinInterval = mesh_cfg->dot11MeshHWMPperrMinInterval; +#endif + conf->dot11MeshHWMPnetDiameterTraversalTime = mesh_cfg->dot11MeshHWMPnetDiameterTraversalTime; + conf->dot11MeshHWMPRootMode = mesh_cfg->dot11MeshHWMPRootMode; +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)) + conf->dot11MeshHWMPRannInterval = mesh_cfg->dot11MeshHWMPRannInterval; +#endif + conf->dot11MeshGateAnnouncementProtocol = mesh_cfg->dot11MeshGateAnnouncementProtocol; +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)) + conf->dot11MeshForwarding = mesh_cfg->dot11MeshForwarding; + conf->rssi_threshold = mesh_cfg->rssi_threshold; +#endif + + /* TBD */ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)) + conf->ht_opmode = 0xffff; +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + conf->dot11MeshHWMPactivePathToRootTimeout = mesh_cfg->dot11MeshHWMPactivePathToRootTimeout; + conf->dot11MeshHWMProotInterval = mesh_cfg->dot11MeshHWMProotInterval; + conf->dot11MeshHWMPconfirmationInterval = mesh_cfg->dot11MeshHWMPconfirmationInterval; +#endif + + /* TBD: power save */ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) + conf->power_mode = NL80211_MESH_POWER_ACTIVE; + conf->dot11MeshAwakeWindowDuration = 0; +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) + conf->plink_timeout = mesh_cfg->plink_timeout; +#endif + + return ret; +} + +static void rtw_mbss_info_change_notify(_adapter *adapter, bool minfo_changed, bool need_work) +{ + if (need_work) + rtw_mesh_work(&adapter->mesh_work); +} + +static int cfg80211_rtw_update_mesh_config(struct wiphy *wiphy, struct net_device *dev + , u32 mask, const struct mesh_config *nconf) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + int ret = 0; + bool minfo_changed = _FALSE, need_work = _FALSE; + + RTW_INFO(FUNC_ADPT_FMT" mask:0x%08x\n", FUNC_ADPT_ARG(adapter), mask); + + rtw_cfg80211_mesh_cfg_set(adapter, nconf, mask); + update_beacon(adapter, WLAN_EID_MESH_CONFIG, NULL, _TRUE, 0); +#if CONFIG_RTW_MESH_CTO_MGATE_CARRIER + if (rtw_mesh_cto_mgate_required(adapter)) + rtw_netif_carrier_off(adapter->pnetdev); + else + rtw_netif_carrier_on(adapter->pnetdev); +#endif + need_work = rtw_ieee80211_mesh_root_setup(adapter); + + rtw_mbss_info_change_notify(adapter, minfo_changed, need_work); + + return ret; +} + +static int cfg80211_rtw_join_mesh(struct wiphy *wiphy, struct net_device *dev, + const struct mesh_config *conf, const struct mesh_setup *setup) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + u8 *ies = NULL; + uint ies_len; + int ret = 0; + + RTW_INFO(FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(adapter)); + +#if DBG_RTW_CFG80211_MESH_CONF + RTW_INFO(FUNC_ADPT_FMT" mesh_setup:\n", FUNC_ADPT_ARG(adapter)); + dump_mesh_setup(RTW_DBGDUMP, wiphy, setup); + RTW_INFO(FUNC_ADPT_FMT" mesh_config:\n", FUNC_ADPT_ARG(adapter)); + dump_mesh_config(RTW_DBGDUMP, conf); +#endif + + if (rtw_cfg80211_sync_iftype(adapter) != _SUCCESS) { + ret = -ENOTSUPP; + goto exit; + } + + /* initialization */ + rtw_mesh_init_mesh_info(adapter); + + /* apply cfg80211 settings*/ + rtw_cfg80211_mesh_info_set_profile(&adapter->mesh_info, setup); + rtw_cfg80211_mesh_cfg_set(adapter, conf, 0xFFFFFFFF); + + /* apply cfg80211 settings (join only) */ + rtw_mesh_cfg_init_max_peer_links(adapter, conf->dot11MeshMaxPeerLinks); + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) + rtw_mesh_cfg_init_plink_timeout(adapter, conf->plink_timeout); + #endif + + rtw_ieee80211_mesh_root_setup(adapter); + + ies = rtw_cfg80211_construct_mesh_beacon_ies(wiphy, adapter, conf, setup, &ies_len); + if (!ies) { + ret = -EINVAL; + goto exit; + } + + /* start mbss */ + if (rtw_check_beacon_data(adapter, ies, ies_len) != _SUCCESS) { + ret = -EINVAL; + goto exit; + } + + rtw_mesh_work(&adapter->mesh_work); + +exit: + if (ies) + rtw_mfree(ies, ies_len); + if (ret) + rtw_mesh_deinit_mesh_info(adapter); + + return ret; +} + +static int cfg80211_rtw_leave_mesh(struct wiphy *wiphy, struct net_device *dev) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + int ret = 0; + + RTW_INFO(FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(adapter)); + + rtw_mesh_deinit_mesh_info(adapter); + + rtw_stop_ap_cmd(adapter, RTW_CMDF_WAIT_ACK); + + return ret; +} + +static int cfg80211_rtw_add_mpath(struct wiphy *wiphy, struct net_device *dev + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)) + , const u8 *dst, const u8 *next_hop + #else + , u8 *dst, u8 *next_hop + #endif +) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct sta_priv *stapriv = &adapter->stapriv; + struct sta_info *sta; + struct rtw_mesh_path *mpath; + int ret = 0; + + rtw_rcu_read_lock(); + + sta = rtw_get_stainfo(stapriv, next_hop); + if (!sta) { + ret = -ENOENT; + goto exit; + } + + mpath = rtw_mesh_path_add(adapter, dst); + if (!mpath) { + ret = -ENOENT; + goto exit; + } + + rtw_mesh_path_fix_nexthop(mpath, sta); + +exit: + rtw_rcu_read_unlock(); + + return ret; +} + +static int cfg80211_rtw_del_mpath(struct wiphy *wiphy, struct net_device *dev + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)) + , const u8 *dst + #else + , u8 *dst + #endif +) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + int ret = 0; + + if (dst) { + if (rtw_mesh_path_del(adapter, dst)) { + ret = -ENOENT; + goto exit; + } + } else { + rtw_mesh_path_flush_by_iface(adapter); + } + +exit: + return ret; +} + +static int cfg80211_rtw_change_mpath(struct wiphy *wiphy, struct net_device *dev + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)) + , const u8 *dst, const u8 *next_hop + #else + , u8 *dst, u8 *next_hop + #endif +) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct sta_priv *stapriv = &adapter->stapriv; + struct sta_info *sta; + struct rtw_mesh_path *mpath; + int ret = 0; + + rtw_rcu_read_lock(); + + sta = rtw_get_stainfo(stapriv, next_hop); + if (!sta) { + ret = -ENOENT; + goto exit; + } + + mpath = rtw_mesh_path_lookup(adapter, dst); + if (!mpath) { + ret = -ENOENT; + goto exit; + } + + rtw_mesh_path_fix_nexthop(mpath, sta); + +exit: + rtw_rcu_read_unlock(); + + return ret; +} + +static void rtw_cfg80211_mpath_set_pinfo(struct rtw_mesh_path *mpath, u8 *next_hop, struct mpath_info *pinfo) +{ + struct sta_info *next_hop_sta = rtw_rcu_dereference(mpath->next_hop); + + if (next_hop_sta) + _rtw_memcpy(next_hop, next_hop_sta->cmn.mac_addr, ETH_ALEN); + else + _rtw_memset(next_hop, 0, ETH_ALEN); + + _rtw_memset(pinfo, 0, sizeof(*pinfo)); + + pinfo->generation = mpath->adapter->mesh_info.mesh_paths_generation; + + pinfo->filled = 0 + | MPATH_INFO_FRAME_QLEN + | MPATH_INFO_SN + | MPATH_INFO_METRIC + | MPATH_INFO_EXPTIME + | MPATH_INFO_DISCOVERY_TIMEOUT + | MPATH_INFO_DISCOVERY_RETRIES + | MPATH_INFO_FLAGS + ; + + pinfo->frame_qlen = mpath->frame_queue_len; + pinfo->sn = mpath->sn; + pinfo->metric = mpath->metric; + if (rtw_time_after(mpath->exp_time, rtw_get_current_time())) + pinfo->exptime = rtw_get_remaining_time_ms(mpath->exp_time); + pinfo->discovery_timeout = rtw_systime_to_ms(mpath->discovery_timeout); + pinfo->discovery_retries = mpath->discovery_retries; + if (mpath->flags & RTW_MESH_PATH_ACTIVE) + pinfo->flags |= NL80211_MPATH_FLAG_ACTIVE; + if (mpath->flags & RTW_MESH_PATH_RESOLVING) + pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING; + if (mpath->flags & RTW_MESH_PATH_SN_VALID) + pinfo->flags |= NL80211_MPATH_FLAG_SN_VALID; + if (mpath->flags & RTW_MESH_PATH_FIXED) + pinfo->flags |= NL80211_MPATH_FLAG_FIXED; + if (mpath->flags & RTW_MESH_PATH_RESOLVED) + pinfo->flags |= NL80211_MPATH_FLAG_RESOLVED; +} + +static int cfg80211_rtw_get_mpath(struct wiphy *wiphy, struct net_device *dev, u8 *dst, u8 *next_hop, struct mpath_info *pinfo) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rtw_mesh_path *mpath; + int ret = 0; + + rtw_rcu_read_lock(); + + mpath = rtw_mesh_path_lookup(adapter, dst); + if (!mpath) { + ret = -ENOENT; + goto exit; + } + + rtw_cfg80211_mpath_set_pinfo(mpath, next_hop, pinfo); + +exit: + rtw_rcu_read_unlock(); + + return ret; +} + +static int cfg80211_rtw_dump_mpath(struct wiphy *wiphy, struct net_device *dev, int idx, u8 *dst, u8 *next_hop, struct mpath_info *pinfo) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rtw_mesh_path *mpath; + int ret = 0; + + rtw_rcu_read_lock(); + + mpath = rtw_mesh_path_lookup_by_idx(adapter, idx); + if (!mpath) { + ret = -ENOENT; + goto exit; + } + + _rtw_memcpy(dst, mpath->dst, ETH_ALEN); + rtw_cfg80211_mpath_set_pinfo(mpath, next_hop, pinfo); + +exit: + rtw_rcu_read_unlock(); + + return ret; +} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)) +static void rtw_cfg80211_mpp_set_pinfo(struct rtw_mesh_path *mpath, u8 *mpp, struct mpath_info *pinfo) +{ + _rtw_memcpy(mpp, mpath->mpp, ETH_ALEN); + + _rtw_memset(pinfo, 0, sizeof(*pinfo)); + pinfo->generation = mpath->adapter->mesh_info.mpp_paths_generation; +} + +static int cfg80211_rtw_get_mpp(struct wiphy *wiphy, struct net_device *dev, u8 *dst, u8 *mpp, struct mpath_info *pinfo) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rtw_mesh_path *mpath; + int ret = 0; + + rtw_rcu_read_lock(); + + mpath = rtw_mpp_path_lookup(adapter, dst); + if (!mpath) { + ret = -ENOENT; + goto exit; + } + + rtw_cfg80211_mpp_set_pinfo(mpath, mpp, pinfo); + +exit: + rtw_rcu_read_unlock(); + + return ret; +} + +static int cfg80211_rtw_dump_mpp(struct wiphy *wiphy, struct net_device *dev, int idx, u8 *dst, u8 *mpp, struct mpath_info *pinfo) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rtw_mesh_path *mpath; + int ret = 0; + + rtw_rcu_read_lock(); + + mpath = rtw_mpp_path_lookup_by_idx(adapter, idx); + if (!mpath) { + ret = -ENOENT; + goto exit; + } + + _rtw_memcpy(dst, mpath->dst, ETH_ALEN); + rtw_cfg80211_mpp_set_pinfo(mpath, mpp, pinfo); + +exit: + rtw_rcu_read_unlock(); + + return ret; +} +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)) */ + +#endif /* defined(CONFIG_RTW_MESH) */ + +#if defined(CONFIG_PNO_SUPPORT) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)) +static int cfg80211_rtw_sched_scan_start(struct wiphy *wiphy, + struct net_device *dev, + struct cfg80211_sched_scan_request *request) +{ + + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct cfg80211_ssid *ssids; + int n_ssids = 0; + int interval = 0; + int i = 0; + u8 ret; + + if (padapter->bup == _FALSE) { + RTW_INFO("%s: net device is down.\n", __func__); + return -EIO; + } + + if (check_fwstate(pmlmepriv, WIFI_UNDER_SURVEY) == _TRUE || + check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _TRUE || + check_fwstate(pmlmepriv, WIFI_UNDER_LINKING) == _TRUE) { + RTW_INFO("%s: device is busy.\n", __func__); + rtw_scan_abort(padapter); + } + + if (request == NULL) { + RTW_INFO("%s: invalid cfg80211_requests parameters.\n", __func__); + return -EINVAL; + } +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0) + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0) + interval = request->scan_plans->interval; +#else + interval = request->interval; +#endif + n_ssids = request->n_match_sets; + ssids = (struct cfg80211_ssid *)rtw_zmalloc(n_ssids * sizeof(struct cfg80211_ssid)); + if(ssids == NULL) { + RTW_ERR("Fail to allocate ssids for PNO\n"); + return -ENOMEM; + } + for (i=0;in_match_sets;i++) { + ssids[i].ssid_len = request->match_sets[i].ssid.ssid_len; + memcpy(ssids[i].ssid, request->match_sets[i].ssid.ssid, + request->match_sets[i].ssid.ssid_len); + } +#else + interval = request->interval; + n_ssids = request->n_ssids; + ssids = request->ssids; +#endif +ret = rtw_android_cfg80211_pno_setup(dev, ssids, + n_ssids, interval); + if (ret < 0) { + RTW_INFO("%s ret: %d\n", __func__, ret); + goto exit; + } + + ret = rtw_android_pno_enable(dev, _TRUE); + if (ret < 0) { + RTW_INFO("%s ret: %d\n", __func__, ret); + goto exit; + } +exit: + return ret; +} + +static int cfg80211_rtw_sched_scan_stop(struct wiphy *wiphy, + struct net_device *dev) +{ + return rtw_android_pno_enable(dev, _FALSE); +} + +int cfg80211_rtw_suspend(struct wiphy *wiphy, struct cfg80211_wowlan *wow) { + RTW_DBG("==> %s\n",__func__); + RTW_DBG("<== %s\n",__func__); + return 0; +} + +int cfg80211_rtw_resume(struct wiphy *wiphy) { + + _adapter *padapter; + struct pwrctrl_priv *pwrpriv; + struct mlme_priv *pmlmepriv; + padapter = wiphy_to_adapter(wiphy); + pwrpriv = adapter_to_pwrctl(padapter); + pmlmepriv = &padapter->mlmepriv; + struct sitesurvey_parm parm; + int i, len; + + + RTW_DBG("==> %s\n",__func__); + if (pwrpriv->wowlan_last_wake_reason == RX_PNO) { + + struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(padapter); + _irqL irqL; + int PNOWakeupScanWaitCnt = 0; + + rtw_cfg80211_disconnected(padapter->rtw_wdev, 0, NULL, 0, 1, GFP_ATOMIC); + + rtw_init_sitesurvey_parm(padapter, &parm); + for (i=0;ipnlo_info->ssid_num && i < RTW_SSID_SCAN_AMOUNT; i++) { + len = pwrpriv->pno_ssid_list->node[i].SSID_len; + _rtw_memcpy(&parm.ssid[i].Ssid, pwrpriv->pno_ssid_list->node[i].SSID, len); + parm.ssid[i].SsidLength = len; + } + parm.ssid_num = pwrpriv->pnlo_info->ssid_num; + + _enter_critical_bh(&pmlmepriv->lock, &irqL); + //This modification fix PNO wakeup reconnect issue with hidden SSID AP. + //rtw_sitesurvey_cmd(padapter, NULL); + rtw_sitesurvey_cmd(padapter, &parm); + _exit_critical_bh(&pmlmepriv->lock, &irqL); + + for (PNOWakeupScanWaitCnt = 0; PNOWakeupScanWaitCnt < 10; PNOWakeupScanWaitCnt++) { + if(check_fwstate(pmlmepriv, WIFI_UNDER_SURVEY) == _FALSE) + break; + rtw_msleep_os(1000); + } + + _enter_critical_bh(&pmlmepriv->lock, &irqL); + cfg80211_sched_scan_results(padapter->rtw_wdev->wiphy); + _exit_critical_bh(&pmlmepriv->lock, &irqL); + + } + RTW_DBG("<== %s\n",__func__); + return 0; + +} +#endif /* CONFIG_PNO_SUPPORT */ + +#ifdef CONFIG_80211N_HT +static void rtw_cfg80211_init_ht_capab_ex(_adapter *padapter + , struct ieee80211_sta_ht_cap *ht_cap, BAND_TYPE band, u8 rf_type) +{ + struct registry_priv *pregistrypriv = &padapter->registrypriv; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct ht_priv *phtpriv = &pmlmepriv->htpriv; + u8 stbc_rx_enable = _FALSE; + + rtw_ht_use_default_setting(padapter); + + /* RX LDPC */ + if (TEST_FLAG(phtpriv->ldpc_cap, LDPC_HT_ENABLE_RX)) + ht_cap->cap |= IEEE80211_HT_CAP_LDPC_CODING; + + /* TX STBC */ + if (TEST_FLAG(phtpriv->stbc_cap, STBC_HT_ENABLE_TX)) + ht_cap->cap |= IEEE80211_HT_CAP_TX_STBC; + + /* RX STBC */ + if (TEST_FLAG(phtpriv->stbc_cap, STBC_HT_ENABLE_RX)) { + /*rtw_rx_stbc 0: disable, bit(0):enable 2.4g, bit(1):enable 5g*/ + if (band == BAND_ON_2_4G) + stbc_rx_enable = (pregistrypriv->rx_stbc & BIT(0)) ? _TRUE : _FALSE; + if (band == BAND_ON_5G) + stbc_rx_enable = (pregistrypriv->rx_stbc & BIT(1)) ? _TRUE : _FALSE; + + if (stbc_rx_enable) { + switch (rf_type) { + case RF_1T1R: + ht_cap->cap |= IEEE80211_HT_CAP_RX_STBC_1R;/*RX STBC One spatial stream*/ + break; + + case RF_2T2R: + case RF_1T2R: + ht_cap->cap |= IEEE80211_HT_CAP_RX_STBC_1R;/* Only one spatial-stream STBC RX is supported */ + break; + case RF_3T3R: + case RF_3T4R: + case RF_4T4R: + ht_cap->cap |= IEEE80211_HT_CAP_RX_STBC_1R;/* Only one spatial-stream STBC RX is supported */ + break; + default: + RTW_INFO("[warning] rf_type %d is not expected\n", rf_type); + break; + } + } + } +} + +static void rtw_cfg80211_init_ht_capab(_adapter *padapter + , struct ieee80211_sta_ht_cap *ht_cap, BAND_TYPE band, u8 rf_type) +{ + struct registry_priv *regsty = &padapter->registrypriv; + u8 rx_nss = 0; + + if (!regsty->ht_enable || !is_supported_ht(regsty->wireless_mode)) + return; + + ht_cap->ht_supported = 1; + + ht_cap->cap = IEEE80211_HT_CAP_MAX_AMSDU; + + if (TEST_FLAG(regsty->short_gi, BIT0)) + ht_cap->cap |= IEEE80211_HT_CAP_SGI_20; + if (hal_is_bw_support(padapter, CHANNEL_WIDTH_40) + && ((band == BAND_ON_2_4G && REGSTY_IS_BW_2G_SUPPORT(regsty, CHANNEL_WIDTH_40)) + || (band == BAND_ON_5G && REGSTY_IS_BW_5G_SUPPORT(regsty, CHANNEL_WIDTH_40))) + ) { + ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40; + if (band == BAND_ON_2_4G) + ht_cap->cap |= IEEE80211_HT_CAP_DSSSCCK40; + if (TEST_FLAG(regsty->short_gi, BIT1)) + ht_cap->cap |= IEEE80211_HT_CAP_SGI_40; + } + + rtw_cfg80211_init_ht_capab_ex(padapter, ht_cap, band, rf_type); + + /* + *Maximum length of AMPDU that the STA can receive. + *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets) + */ + ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K; + + /*Minimum MPDU start spacing , */ + ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16; + + ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED; + + rx_nss = GET_HAL_RX_NSS(padapter); + switch (rx_nss) { + case 1: + ht_cap->mcs.rx_mask[0] = 0xFF; + break; + case 2: + ht_cap->mcs.rx_mask[0] = 0xFF; + ht_cap->mcs.rx_mask[1] = 0xFF; + break; + case 3: + ht_cap->mcs.rx_mask[0] = 0xFF; + ht_cap->mcs.rx_mask[1] = 0xFF; + ht_cap->mcs.rx_mask[2] = 0xFF; + break; + case 4: + ht_cap->mcs.rx_mask[0] = 0xFF; + ht_cap->mcs.rx_mask[1] = 0xFF; + ht_cap->mcs.rx_mask[2] = 0xFF; + ht_cap->mcs.rx_mask[3] = 0xFF; + break; + default: + rtw_warn_on(1); + RTW_INFO("%s, error rf_type=%d, rx_nss=%d\n", __func__, rf_type, rx_nss); + }; + + ht_cap->mcs.rx_highest = cpu_to_le16( + rtw_ht_mcs_rate(hal_is_bw_support(padapter, CHANNEL_WIDTH_40) + , hal_is_bw_support(padapter, CHANNEL_WIDTH_40) ? ht_cap->cap & IEEE80211_HT_CAP_SGI_40 : ht_cap->cap & IEEE80211_HT_CAP_SGI_20 + , ht_cap->mcs.rx_mask) / 10); +} +#endif /* CONFIG_80211N_HT */ + +#if defined(CONFIG_80211AC_VHT) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) +static void rtw_cfg80211_init_vht_capab(_adapter *padapter + , struct ieee80211_sta_vht_cap *sta_vht_cap, BAND_TYPE band, u8 rf_type) +{ + struct registry_priv *regsty = &padapter->registrypriv; + u8 vht_cap_ie[2 + 12] = {0}; + + if (!REGSTY_IS_11AC_ENABLE(regsty) || !is_supported_vht(regsty->wireless_mode)) + return; + + rtw_vht_use_default_setting(padapter); + rtw_build_vht_cap_ie(padapter, vht_cap_ie); + + sta_vht_cap->vht_supported = 1; + + _rtw_memcpy(&sta_vht_cap->cap, vht_cap_ie + 2, 4); + _rtw_memcpy(&sta_vht_cap->vht_mcs, vht_cap_ie + 2 + 4, 8); +} +#endif /* defined(CONFIG_80211AC_VHT) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) */ + +void rtw_cfg80211_init_wdev_data(_adapter *padapter) +{ +#ifdef CONFIG_CONCURRENT_MODE + struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(padapter); + + ATOMIC_SET(&pwdev_priv->switch_ch_to, 1); +#endif +} + +static int rtw_cfg80211_init_wiphy_band(_adapter *padapter, struct wiphy *wiphy) +{ + u8 rf_type; + struct ieee80211_supported_band *band; + int ret = _FAIL; + + rf_type = GET_HAL_RFPATH(padapter); + RTW_INFO("%s:rf_type=%d\n", __func__, rf_type); + + if (IsSupported24G(padapter->registrypriv.wireless_mode)) { + band = wiphy->bands[NL80211_BAND_2GHZ] = rtw_spt_band_alloc(BAND_ON_2_4G); + if (!band) + goto exit; + rtw_2g_channels_init(band->channels); + rtw_2g_rates_init(band->bitrates); + #if defined(CONFIG_80211N_HT) + rtw_cfg80211_init_ht_capab(padapter, &band->ht_cap, BAND_ON_2_4G, rf_type); + #endif + } +#if CONFIG_IEEE80211_BAND_5GHZ + if (is_supported_5g(padapter->registrypriv.wireless_mode)) { + band = wiphy->bands[NL80211_BAND_5GHZ] = rtw_spt_band_alloc(BAND_ON_5G); + if (!band) { + if (wiphy->bands[NL80211_BAND_2GHZ]) { + rtw_spt_band_free(wiphy->bands[NL80211_BAND_2GHZ]); + wiphy->bands[NL80211_BAND_2GHZ] = NULL; + } + goto exit; + } + rtw_5g_channels_init(band->channels); + rtw_5g_rates_init(band->bitrates); + #if defined(CONFIG_80211N_HT) + rtw_cfg80211_init_ht_capab(padapter, &band->ht_cap, BAND_ON_5G, rf_type); + #endif + #if defined(CONFIG_80211AC_VHT) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + rtw_cfg80211_init_vht_capab(padapter, &band->vht_cap, BAND_ON_5G, rf_type); + #endif + } +#endif + + ret = _SUCCESS; + +exit: + return ret; +} + +#if !defined(CONFIG_REGD_SRC_FROM_OS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)) +void rtw_cfg80211_update_wiphy_max_txpower(_adapter *adapter, struct wiphy *wiphy) +{ + struct ieee80211_supported_band *band; + struct ieee80211_channel *channel; + s16 max_txpwr; + int i; + + if (IsSupported24G(adapter->registrypriv.wireless_mode)) { + band = wiphy->bands[NL80211_BAND_2GHZ]; + if (band) { + max_txpwr = phy_get_txpwr_by_rate_total_max_mbm(adapter, BAND_ON_2_4G, 1, 1); + if (max_txpwr != UNSPECIFIED_MBM) { + for (i = 0; i < band->n_channels; i++) { + channel = &band->channels[i]; + channel->max_power = max_txpwr / MBM_PDBM; + } + } + } + } +#if CONFIG_IEEE80211_BAND_5GHZ + if (is_supported_5g(adapter->registrypriv.wireless_mode)) { + band = wiphy->bands[NL80211_BAND_5GHZ]; + if (band) { + max_txpwr = phy_get_txpwr_by_rate_total_max_mbm(adapter, BAND_ON_5G, 1, 1); + if (max_txpwr != UNSPECIFIED_MBM) { + for (i = 0; i < band->n_channels; i++) { + channel = &band->channels[i]; + channel->max_power = max_txpwr / MBM_PDBM; + } + } + } + } +#endif +} +#endif /* defined(CONFIG_REGD_SRC_FROM_OS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)) */ + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)) && defined(RTW_SINGLE_WIPHY) && (CONFIG_IFACE_NUMBER >= 2) +struct ieee80211_iface_limit rtw_limits[] = { + { + .max = CONFIG_IFACE_NUMBER, + .types = BIT(NL80211_IFTYPE_STATION) + #if defined(CONFIG_P2P) && ((LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE)) + | BIT(NL80211_IFTYPE_P2P_CLIENT) + #endif + }, + #ifdef CONFIG_AP_MODE + { + .max = rtw_min(CONFIG_IFACE_NUMBER, CONFIG_LIMITED_AP_NUM), + .types = BIT(NL80211_IFTYPE_AP) + #if defined(CONFIG_P2P) && ((LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE)) + | BIT(NL80211_IFTYPE_P2P_GO) + #endif + }, + #endif + #if defined(RTW_DEDICATED_P2P_DEVICE) + { + .max = 1, + .types = BIT(NL80211_IFTYPE_P2P_DEVICE) + }, + #endif + #if defined(CONFIG_RTW_MESH) + { + .max = 1, + .types = BIT(NL80211_IFTYPE_MESH_POINT) + }, + #endif +}; + +struct ieee80211_iface_combination rtw_combinations[] = { + { + .limits = rtw_limits, + .n_limits = ARRAY_SIZE(rtw_limits), + #if defined(RTW_DEDICATED_P2P_DEVICE) + .max_interfaces = CONFIG_IFACE_NUMBER + 1, + #else + .max_interfaces = CONFIG_IFACE_NUMBER, + #endif + .num_different_channels = 1, + }, +}; +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)) */ + +static int rtw_cfg80211_init_wiphy(_adapter *adapter, struct wiphy *wiphy) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct registry_priv *regsty = dvobj_to_regsty(dvobj); + int ret = _FAIL; + + /* copy mac_addr to wiphy */ + _rtw_memcpy(wiphy->perm_addr, adapter_mac_addr(adapter), ETH_ALEN); + + wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM; + + wiphy->max_scan_ssids = RTW_SSID_SCAN_AMOUNT; + wiphy->max_scan_ie_len = RTW_SCAN_IE_LEN_MAX; + wiphy->max_num_pmkids = RTW_MAX_NUM_PMKIDS; + +#if CONFIG_RTW_MACADDR_ACL && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) + wiphy->max_acl_mac_addrs = NUM_ACL; +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)) || defined(COMPAT_KERNEL_RELEASE) + wiphy->max_remain_on_channel_duration = RTW_MAX_REMAIN_ON_CHANNEL_DURATION; +#endif + + wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) + #ifdef CONFIG_AP_MODE + | BIT(NL80211_IFTYPE_ADHOC) /* todo : AD-HOC task group will refine it */ + | BIT(NL80211_IFTYPE_AP) + #endif + #ifdef CONFIG_WIFI_MONITOR + | BIT(NL80211_IFTYPE_MONITOR) + #endif +#if defined(CONFIG_P2P) && ((LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE)) + | BIT(NL80211_IFTYPE_P2P_CLIENT) + | BIT(NL80211_IFTYPE_P2P_GO) + #if defined(RTW_DEDICATED_P2P_DEVICE) + | BIT(NL80211_IFTYPE_P2P_DEVICE) + #endif +#endif +#ifdef CONFIG_RTW_MESH + | BIT(NL80211_IFTYPE_MESH_POINT) /* 2.6.26 */ +#endif + ; + +#if !defined(RTW_SINGLE_WIPHY) && defined(CONFIG_CONCURRENT_MODE) && defined(CONFIG_P2P) + if (adapter->iface_id != adapter->registrypriv.sel_p2p_iface) { + wiphy->interface_modes &= ~(BIT(NL80211_IFTYPE_P2P_GO) | BIT(NL80211_IFTYPE_P2P_CLIENT)); + RTW_INFO("%s iface_id:%d- don't set p2p capability\n", __func__, adapter->iface_id); + } +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + wiphy->mgmt_stypes = rtw_cfg80211_default_mgmt_stypes; +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)) + #ifdef CONFIG_WIFI_MONITOR + wiphy->software_iftypes |= BIT(NL80211_IFTYPE_MONITOR); + #endif +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)) && defined(RTW_SINGLE_WIPHY) && (CONFIG_IFACE_NUMBER >= 2) + wiphy->iface_combinations = rtw_combinations; + wiphy->n_iface_combinations = ARRAY_SIZE(rtw_combinations); +#endif + + wiphy->cipher_suites = rtw_cipher_suites; + + /* Todo refine */ + if (_rtw_camctl_chk_cap(adapter, SEC_CAP_CHK_EXTRA_SEC)) + wiphy->n_cipher_suites = ARRAY_SIZE(rtw_cipher_suites); + else { +#ifdef CONFIG_IEEE80211W + /* remove hardware not support cipher */ + wiphy->n_cipher_suites = ARRAY_SIZE(rtw_cipher_suites) - 6; +#else + wiphy->n_cipher_suites = ARRAY_SIZE(rtw_cipher_suites); +#endif + } + + if (rtw_cfg80211_init_wiphy_band(adapter, wiphy) != _SUCCESS) { + RTW_ERR("rtw_cfg80211_init_wiphy_band fail\n"); + goto exit; + } + #if !defined(CONFIG_REGD_SRC_FROM_OS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)) + rtw_cfg80211_update_wiphy_max_txpower(adapter, wiphy); + #endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38) && LINUX_VERSION_CODE < KERNEL_VERSION(3, 0, 0)) + wiphy->flags |= WIPHY_FLAG_SUPPORTS_SEPARATE_DEFAULT_KEYS; +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)) + wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL; + wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME; + /* remove WIPHY_FLAG_OFFCHAN_TX, because we not support this feature */ + /* wiphy->flags |= WIPHY_FLAG_OFFCHAN_TX | WIPHY_FLAG_HAVE_AP_SME; */ +#endif + +#if defined(CONFIG_PM) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0) && \ + LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0)) + wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN; +#ifdef CONFIG_PNO_SUPPORT + wiphy->max_sched_scan_ssids = MAX_PNO_LIST_COUNT; +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0) + wiphy->max_match_sets = MAX_PNO_LIST_COUNT; +#endif +#endif +#endif + +#if defined(CONFIG_PM) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)) +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0)) + wiphy->wowlan = wowlan_stub; +#else + wiphy->wowlan = &wowlan_stub; +#endif +#endif + +#if defined(CONFIG_TDLS) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)) + wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS; +#ifndef CONFIG_TDLS_DRIVER_SETUP + wiphy->flags |= WIPHY_FLAG_TDLS_EXTERNAL_SETUP; /* Driver handles key exchange */ + wiphy->flags |= NL80211_ATTR_HT_CAPABILITY; +#endif /* CONFIG_TDLS_DRIVER_SETUP */ +#endif /* CONFIG_TDLS */ + +#ifdef CONFIG_LPS + wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT; +#else + wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT; +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)) + /* wiphy->flags |= WIPHY_FLAG_SUPPORTS_FW_ROAM; */ +#endif + +#ifdef CONFIG_RTW_WDS + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)) + wiphy->flags |= WIPHY_FLAG_4ADDR_AP; + wiphy->flags |= WIPHY_FLAG_4ADDR_STATION; + #endif +#endif + +#ifdef CONFIG_RTW_MESH + wiphy->flags |= 0 + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) + | WIPHY_FLAG_IBSS_RSN + #endif + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)) + | WIPHY_FLAG_MESH_AUTH + #endif + ; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)) + wiphy->features |= 0 + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)) + | NL80211_FEATURE_USERSPACE_MPM + #endif + ; +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)) */ +#endif /* CONFIG_RTW_MESH */ + +#if defined(CONFIG_RTW_80211K) && (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 6, 0)) + wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_RRM); +#endif + +#if (KERNEL_VERSION(3, 8, 0) <= LINUX_VERSION_CODE) + wiphy->features |= NL80211_FEATURE_SAE; +#endif + +#ifdef CONFIG_RTW_SCAN_RAND + #if (KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE) + wiphy->features |= NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR; + #endif +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) +#ifdef CONFIG_WIFI_MONITOR + /* Currently only for Monitor debugging */ + wiphy->flags |= WIPHY_FLAG_SUPPORTS_5_10_MHZ; +#endif +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) */ + + ret = _SUCCESS; + +exit: + return ret; +} + +#ifdef CONFIG_RFKILL_POLL +void rtw_cfg80211_init_rfkill(struct wiphy *wiphy) +{ + wiphy_rfkill_set_hw_state(wiphy, 0); + wiphy_rfkill_start_polling(wiphy); +} + +void rtw_cfg80211_deinit_rfkill(struct wiphy *wiphy) +{ + wiphy_rfkill_stop_polling(wiphy); +} + +static void cfg80211_rtw_rfkill_poll(struct wiphy *wiphy) +{ + _adapter *padapter = NULL; + bool blocked = _FALSE; + u8 valid = 0; + + padapter = wiphy_to_adapter(wiphy); + + if (adapter_to_dvobj(padapter)->processing_dev_remove == _TRUE) { + /*RTW_INFO("cfg80211_rtw_rfkill_poll: device is removed!\n");*/ + return; + } + + blocked = rtw_hal_rfkill_poll(padapter, &valid); + /*RTW_INFO("cfg80211_rtw_rfkill_poll: valid=%d, blocked=%d\n", + valid, blocked);*/ + + if (valid) + wiphy_rfkill_set_hw_state(wiphy, blocked); +} +#endif + +#if defined(CONFIG_RTW_HOSTAPD_ACS) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)) + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) && (LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)) +#define SURVEY_INFO_TIME SURVEY_INFO_CHANNEL_TIME +#define SURVEY_INFO_TIME_BUSY SURVEY_INFO_CHANNEL_TIME_BUSY +#define SURVEY_INFO_TIME_EXT_BUSY SURVEY_INFO_CHANNEL_TIME_EXT_BUSY +#define SURVEY_INFO_TIME_RX SURVEY_INFO_CHANNEL_TIME_RX +#define SURVEY_INFO_TIME_TX SURVEY_INFO_CHANNEL_TIME_TX +#endif + +#ifdef CONFIG_RTW_ACS +static void rtw_cfg80211_set_survey_info_with_clm(PADAPTER padapter, int idx, struct survey_info *pinfo) +{ + s8 noise = -50; /*channel noise in dBm. This and all following fields are optional */ + u64 time = SURVEY_TO; /*amount of time in ms the radio was turn on (on the channel)*/ + u64 time_busy = 0; /*amount of time the primary channel was sensed busy*/ + u8 chan = (u8)idx; + + if ((idx < 0) || (pinfo == NULL)) + return; + + pinfo->filled = SURVEY_INFO_NOISE_DBM + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) + | SURVEY_INFO_TIME | SURVEY_INFO_TIME_BUSY + #endif + ; + + time_busy = rtw_acs_get_clm_ratio_by_ch_idx(padapter, chan); + noise = rtw_acs_get_nhm_noise_pwr_by_ch_idx(padapter, chan); + /* RTW_INFO("%s: ch-idx:%d time=%llu(ms), time_busy=%llu(ms), noise=%d(dbm)\n", __func__, idx, time, time_busy, noise); */ + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) + #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)) + pinfo->channel_time = time; + pinfo->channel_time_busy = time_busy; + #else + pinfo->time = time; + pinfo->time_busy = time_busy; + #endif +#endif + pinfo->noise = noise; +} +#endif /* CONFIG_RTW_ACS */ + +static int rtw_hostapd_acs_dump_survey(struct wiphy *wiphy, struct net_device *netdev, int idx, struct survey_info *info) +{ + PADAPTER padapter = (_adapter *)rtw_netdev_priv(netdev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(padapter); + RT_CHANNEL_INFO *pch_set = rfctl->channel_set; + u8 max_chan_nums = rfctl->max_chan_nums; + u32 freq = 0; + u8 ret = 0; + u16 channel = 0; + + if (!netdev || !info) { + RTW_INFO("%s: invial parameters.\n", __func__); + return -EINVAL; + } + + _rtw_memset(info, 0, sizeof(struct survey_info)); + if (padapter->bup == _FALSE) { + RTW_INFO("%s: net device is down.\n", __func__); + return -EIO; + } + + if (idx >= max_chan_nums) + return -ENOENT; + + channel = pch_set[idx].ChannelNum; + freq = rtw_ch2freq(channel); + info->channel = ieee80211_get_channel(wiphy, freq); + /* RTW_INFO("%s: channel %d, freq %d\n", __func__, channel, freq); */ + + if (!info->channel) + return -EINVAL; + + if (info->channel->flags == IEEE80211_CHAN_DISABLED) + return ret; + +#ifdef CONFIG_RTW_ACS + rtw_cfg80211_set_survey_info_with_clm(padapter, idx, info); +#else + RTW_ERR("%s: unknown acs operation!\n", __func__); +#endif + + return ret; +} +#endif /* defined(CONFIG_RTW_HOSTAPD_ACS) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)) */ + +#if (KERNEL_VERSION(4, 17, 0) <= LINUX_VERSION_CODE) \ + || defined(CONFIG_KERNEL_PATCH_EXTERNAL_AUTH) +int cfg80211_rtw_external_auth(struct wiphy *wiphy, struct net_device *dev, + struct cfg80211_external_auth_params *params) +{ + PADAPTER padapter = (_adapter *)rtw_netdev_priv(dev); + + RTW_INFO(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(dev)); + + rtw_cfg80211_external_auth_status(wiphy, dev, + (struct rtw_external_auth_params *)params); + + return 0; +} +#endif + +void rtw_cfg80211_external_auth_status(struct wiphy *wiphy, struct net_device *dev, + struct rtw_external_auth_params *params) +{ + PADAPTER padapter = (_adapter *)rtw_netdev_priv(dev); + struct security_priv *psecuritypriv = &padapter->securitypriv; + struct sta_priv *pstapriv = &padapter->stapriv; + struct sta_info *psta = NULL; + u8 *buf = NULL; + u32 len = 0; + _irqL irqL; + + RTW_INFO(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(dev)); + + RTW_INFO("SAE: action: %u, status: %u\n", params->action, params->status); + if (params->status == WLAN_STATUS_SUCCESS) { + RTW_INFO("bssid: "MAC_FMT"\n", MAC_ARG(params->bssid)); + RTW_INFO("SSID: [%s]\n", + ((params->ssid.ssid_len == 0) ? "" : (char *)params->ssid.ssid)); + RTW_INFO("suite: 0x%08x\n", params->key_mgmt_suite); + } + + psta = rtw_get_stainfo(pstapriv, params->bssid); + if (psta && (params->status == WLAN_STATUS_SUCCESS)) { +#ifdef CONFIG_AP_MODE + /* AP mode */ + RTW_INFO("station match\n"); + + psta->state &= ~WIFI_FW_AUTH_NULL; + psta->state |= WIFI_FW_AUTH_SUCCESS; + psta->expire_to = padapter->stapriv.assoc_to; + + /* ToDo: Kernel v5.1 pmkid is pointer */ + /* RTW_INFO_DUMP("PMKID:", params->pmkid, PMKID_LEN); */ + _rtw_set_pmksa(dev, params->bssid, params->pmkid); + + _enter_critical_bh(&psta->lock, &irqL); + if ((psta->auth_len != 0) && (psta->pauth_frame != NULL)) { + buf = rtw_zmalloc(psta->auth_len); + if (buf) { + _rtw_memcpy(buf, psta->pauth_frame, psta->auth_len); + len = psta->auth_len; + } + + rtw_mfree(psta->pauth_frame, psta->auth_len); + psta->pauth_frame = NULL; + psta->auth_len = 0; + } + _exit_critical_bh(&psta->lock, &irqL); + + if (buf) { + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + /* send the SAE auth Confirm */ + + rtw_ps_deny(padapter, PS_DENY_MGNT_TX); + if (_SUCCESS == rtw_pwr_wakeup(padapter)) { + rtw_mi_set_scan_deny(padapter, 1000); + rtw_mi_scan_abort(padapter, _TRUE); + + RTW_INFO("SAE: Tx auth Confirm\n"); + rtw_mgnt_tx_cmd(padapter, pmlmeext->cur_channel, 1, buf, len, 0, RTW_CMDF_DIRECTLY); + + } + rtw_ps_deny_cancel(padapter, PS_DENY_MGNT_TX); + + rtw_mfree(buf, len); + buf = NULL; + len = 0; + } +#endif + } else if (psta && (params->status == WLAN_STATUS_UNSPECIFIED_FAILURE)) { +#ifdef CONFIG_AP_MODE + rtw_cfg80211_indicate_sta_disassoc(padapter, psta->cmn.mac_addr, WLAN_STATUS_UNSPECIFIED_FAILURE); +#endif + } else { + /* STA mode */ + psecuritypriv->extauth_status = params->status; + } +} + +static struct cfg80211_ops rtw_cfg80211_ops = { + .change_virtual_intf = cfg80211_rtw_change_iface, + .add_key = cfg80211_rtw_add_key, + .get_key = cfg80211_rtw_get_key, + .del_key = cfg80211_rtw_del_key, + .set_default_key = cfg80211_rtw_set_default_key, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)) + .set_default_mgmt_key = cfg80211_rtw_set_default_mgmt_key, +#endif +#if defined(CONFIG_GTK_OL) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 1, 0)) + .set_rekey_data = cfg80211_rtw_set_rekey_data, +#endif /*CONFIG_GTK_OL*/ + .get_station = cfg80211_rtw_get_station, + .scan = cfg80211_rtw_scan, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)) && \ + defined(CONFIG_RTW_ABORT_SCAN) + .abort_scan = cfg80211_rtw_abort_scan, +#endif + .set_wiphy_params = cfg80211_rtw_set_wiphy_params, + .connect = cfg80211_rtw_connect, + .disconnect = cfg80211_rtw_disconnect, + .join_ibss = cfg80211_rtw_join_ibss, + .leave_ibss = cfg80211_rtw_leave_ibss, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)) + .set_tx_power = cfg80211_rtw_set_txpower, + .get_tx_power = cfg80211_rtw_get_txpower, +#endif + .set_power_mgmt = cfg80211_rtw_set_power_mgmt, + .set_pmksa = cfg80211_rtw_set_pmksa, + .del_pmksa = cfg80211_rtw_del_pmksa, + .flush_pmksa = cfg80211_rtw_flush_pmksa, + + .add_virtual_intf = cfg80211_rtw_add_virtual_intf, + .del_virtual_intf = cfg80211_rtw_del_virtual_intf, + +#ifdef CONFIG_AP_MODE +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)) && !defined(COMPAT_KERNEL_RELEASE) + .add_beacon = cfg80211_rtw_add_beacon, + .set_beacon = cfg80211_rtw_set_beacon, + .del_beacon = cfg80211_rtw_del_beacon, +#else + .start_ap = cfg80211_rtw_start_ap, + .change_beacon = cfg80211_rtw_change_beacon, + .stop_ap = cfg80211_rtw_stop_ap, +#endif + +#if CONFIG_RTW_MACADDR_ACL && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) + .set_mac_acl = cfg80211_rtw_set_mac_acl, +#endif + + .add_station = cfg80211_rtw_add_station, + .del_station = cfg80211_rtw_del_station, + .change_station = cfg80211_rtw_change_station, + .dump_station = cfg80211_rtw_dump_station, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)) + .change_bss = cfg80211_rtw_change_bss, +#endif +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)) + .set_txq_params = cfg80211_rtw_set_txq_params, +#endif +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0)) + .set_channel = cfg80211_rtw_set_channel, +#endif + /* .auth = cfg80211_rtw_auth, */ + /* .assoc = cfg80211_rtw_assoc, */ +#endif /* CONFIG_AP_MODE */ + +#if defined(CONFIG_RTW_MESH) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)) + .get_mesh_config = cfg80211_rtw_get_mesh_config, + .update_mesh_config = cfg80211_rtw_update_mesh_config, + .join_mesh = cfg80211_rtw_join_mesh, + .leave_mesh = cfg80211_rtw_leave_mesh, + .add_mpath = cfg80211_rtw_add_mpath, + .del_mpath = cfg80211_rtw_del_mpath, + .change_mpath = cfg80211_rtw_change_mpath, + .get_mpath = cfg80211_rtw_get_mpath, + .dump_mpath = cfg80211_rtw_dump_mpath, + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)) + .get_mpp = cfg80211_rtw_get_mpp, + .dump_mpp = cfg80211_rtw_dump_mpp, + #endif +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + .set_monitor_channel = cfg80211_rtw_set_monitor_channel, +#endif +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + .get_channel = cfg80211_rtw_get_channel, +#endif + + .remain_on_channel = cfg80211_rtw_remain_on_channel, + .cancel_remain_on_channel = cfg80211_rtw_cancel_remain_on_channel, + +#if defined(CONFIG_P2P) && defined(RTW_DEDICATED_P2P_DEVICE) + .start_p2p_device = cfg80211_rtw_start_p2p_device, + .stop_p2p_device = cfg80211_rtw_stop_p2p_device, +#endif + +#ifdef CONFIG_RTW_80211R + .update_ft_ies = cfg80211_rtw_update_ft_ies, +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) || defined(COMPAT_KERNEL_RELEASE) + .mgmt_tx = cfg80211_rtw_mgmt_tx, +#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 8, 0)) + .mgmt_frame_register = cfg80211_rtw_mgmt_frame_register, +#else + .update_mgmt_frame_registrations = cfg80211_rtw_update_mgmt_frame_register, +#endif +#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34) && LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 35)) + .action = cfg80211_rtw_mgmt_tx, +#endif + +#if defined(CONFIG_TDLS) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)) + .tdls_mgmt = cfg80211_rtw_tdls_mgmt, + .tdls_oper = cfg80211_rtw_tdls_oper, +#endif /* CONFIG_TDLS */ + +#if defined(CONFIG_PNO_SUPPORT) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)) + .sched_scan_start = cfg80211_rtw_sched_scan_start, + .sched_scan_stop = cfg80211_rtw_sched_scan_stop, + .suspend = cfg80211_rtw_suspend, + .resume = cfg80211_rtw_resume, +#endif /* CONFIG_PNO_SUPPORT */ +#ifdef CONFIG_RFKILL_POLL + .rfkill_poll = cfg80211_rtw_rfkill_poll, +#endif +#if defined(CONFIG_RTW_HOSTAPD_ACS) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)) + .dump_survey = rtw_hostapd_acs_dump_survey, +#endif +#if (KERNEL_VERSION(4, 17, 0) <= LINUX_VERSION_CODE) \ + || defined(CONFIG_KERNEL_PATCH_EXTERNAL_AUTH) + .external_auth = cfg80211_rtw_external_auth, +#endif +}; + +struct wiphy *rtw_wiphy_alloc(_adapter *padapter, struct device *dev) +{ + struct wiphy *wiphy; + struct rtw_wiphy_data *wiphy_data; + + /* wiphy */ + wiphy = wiphy_new(&rtw_cfg80211_ops, sizeof(struct rtw_wiphy_data)); + if (!wiphy) { + RTW_ERR("Couldn't allocate wiphy device\n"); + goto exit; + } + set_wiphy_dev(wiphy, dev); + + /* wiphy_data */ + wiphy_data = rtw_wiphy_priv(wiphy); + wiphy_data->dvobj = adapter_to_dvobj(padapter); +#ifndef RTW_SINGLE_WIPHY + wiphy_data->adapter = padapter; +#endif + wiphy_data->txpwr_total_lmt_mbm = UNSPECIFIED_MBM; + wiphy_data->txpwr_total_target_mbm = UNSPECIFIED_MBM; + + if (rtw_cfg80211_init_wiphy(padapter, wiphy) != _SUCCESS) { + rtw_wiphy_free(wiphy); + wiphy = NULL; + goto exit; + } + + RTW_INFO(FUNC_WIPHY_FMT"\n", FUNC_WIPHY_ARG(wiphy)); + +exit: + return wiphy; +} + +void rtw_wiphy_free(struct wiphy *wiphy) +{ + if (!wiphy) + return; + + RTW_INFO(FUNC_WIPHY_FMT"\n", FUNC_WIPHY_ARG(wiphy)); + + if (wiphy->bands[NL80211_BAND_2GHZ]) { + rtw_spt_band_free(wiphy->bands[NL80211_BAND_2GHZ]); + wiphy->bands[NL80211_BAND_2GHZ] = NULL; + } + if (wiphy->bands[NL80211_BAND_5GHZ]) { + rtw_spt_band_free(wiphy->bands[NL80211_BAND_5GHZ]); + wiphy->bands[NL80211_BAND_5GHZ] = NULL; + } + + wiphy_free(wiphy); +} + +int rtw_wiphy_register(struct wiphy *wiphy) +{ + RTW_INFO(FUNC_WIPHY_FMT"\n", FUNC_WIPHY_ARG(wiphy)); + +#if ( (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)) \ + || defined(RTW_VENDOR_EXT_SUPPORT) ) + rtw_cfgvendor_attach(wiphy); +#endif + + rtw_regd_init(wiphy); + + return wiphy_register(wiphy); +} + +void rtw_wiphy_unregister(struct wiphy *wiphy) +{ + RTW_INFO(FUNC_WIPHY_FMT"\n", FUNC_WIPHY_ARG(wiphy)); + +#if ( (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)) \ + || defined(RTW_VENDOR_EXT_SUPPORT) ) + rtw_cfgvendor_detach(wiphy); +#endif + + #if defined(RTW_DEDICATED_P2P_DEVICE) + rtw_pd_iface_free(wiphy); + #endif + + return wiphy_unregister(wiphy); +} + +int rtw_wdev_alloc(_adapter *padapter, struct wiphy *wiphy) +{ + int ret = 0; + struct net_device *pnetdev = padapter->pnetdev; + struct wireless_dev *wdev; + struct rtw_wdev_priv *pwdev_priv; + + RTW_INFO("%s(padapter=%p)\n", __func__, padapter); + + /* wdev */ + wdev = (struct wireless_dev *)rtw_zmalloc(sizeof(struct wireless_dev)); + if (!wdev) { + RTW_INFO("Couldn't allocate wireless device\n"); + ret = -ENOMEM; + goto exit; + } + wdev->wiphy = wiphy; + wdev->netdev = pnetdev; + wdev->iftype = NL80211_IFTYPE_STATION; + padapter->rtw_wdev = wdev; + pnetdev->ieee80211_ptr = wdev; + + /* init pwdev_priv */ + pwdev_priv = adapter_wdev_data(padapter); + pwdev_priv->rtw_wdev = wdev; + pwdev_priv->pmon_ndev = NULL; + pwdev_priv->ifname_mon[0] = '\0'; + pwdev_priv->padapter = padapter; + pwdev_priv->scan_request = NULL; + _rtw_spinlock_init(&pwdev_priv->scan_req_lock); + pwdev_priv->connect_req = NULL; + _rtw_spinlock_init(&pwdev_priv->connect_req_lock); + + pwdev_priv->p2p_enabled = _FALSE; + pwdev_priv->probe_resp_ie_update_time = rtw_get_current_time(); + pwdev_priv->provdisc_req_issued = _FALSE; + rtw_wdev_invit_info_init(&pwdev_priv->invit_info); + rtw_wdev_nego_info_init(&pwdev_priv->nego_info); + + pwdev_priv->bandroid_scan = _FALSE; + + _rtw_mutex_init(&pwdev_priv->roch_mutex); + +#ifdef CONFIG_CONCURRENT_MODE + ATOMIC_SET(&pwdev_priv->switch_ch_to, 1); +#endif + +#ifdef CONFIG_RTW_CFGVENDOR_RSSIMONITOR + pwdev_priv->rssi_monitor_enable = 0; + pwdev_priv->rssi_monitor_max = 0; + pwdev_priv->rssi_monitor_min = 0; +#endif + + +exit: + return ret; +} + +void rtw_wdev_free(struct wireless_dev *wdev) +{ + if (!wdev) + return; + + RTW_INFO("%s(wdev=%p)\n", __func__, wdev); + + if (wdev_to_ndev(wdev)) { + _adapter *adapter = (_adapter *)rtw_netdev_priv(wdev_to_ndev(wdev)); + struct rtw_wdev_priv *wdev_priv = adapter_wdev_data(adapter); + _irqL irqL; + + _rtw_spinlock_free(&wdev_priv->scan_req_lock); + + _enter_critical_bh(&wdev_priv->connect_req_lock, &irqL); + rtw_wdev_free_connect_req(wdev_priv); + _exit_critical_bh(&wdev_priv->connect_req_lock, &irqL); + _rtw_spinlock_free(&wdev_priv->connect_req_lock); + + _rtw_mutex_free(&wdev_priv->roch_mutex); + } + + rtw_mfree((u8 *)wdev, sizeof(struct wireless_dev)); +} + +void rtw_wdev_unregister(struct wireless_dev *wdev) +{ + struct net_device *ndev; + _adapter *adapter; + struct rtw_wdev_priv *pwdev_priv; + + if (!wdev) + return; + + RTW_INFO("%s(wdev=%p)\n", __func__, wdev); + + ndev = wdev_to_ndev(wdev); + if (!ndev) + return; + + adapter = (_adapter *)rtw_netdev_priv(ndev); + pwdev_priv = adapter_wdev_data(adapter); + + rtw_cfg80211_indicate_scan_done(adapter, _TRUE); + + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) || defined(COMPAT_KERNEL_RELEASE) + if (wdev->current_bss) { + RTW_INFO(FUNC_ADPT_FMT" clear current_bss by cfg80211_disconnected\n", FUNC_ADPT_ARG(adapter)); + rtw_cfg80211_indicate_disconnect(adapter, 0, 1); + } + #endif + + if (pwdev_priv->pmon_ndev) { + RTW_INFO("%s, unregister monitor interface\n", __func__); + unregister_netdev(pwdev_priv->pmon_ndev); + } +} + +int rtw_cfg80211_ndev_res_alloc(_adapter *adapter) +{ + int ret = _FAIL; + +#if !defined(RTW_SINGLE_WIPHY) + struct wiphy *wiphy; + struct device *dev = dvobj_to_dev(adapter_to_dvobj(adapter)); + + wiphy = rtw_wiphy_alloc(adapter, dev); + if (wiphy == NULL) + goto exit; + + adapter->wiphy = wiphy; +#endif + + if (rtw_wdev_alloc(adapter, adapter_to_wiphy(adapter)) == 0) + ret = _SUCCESS; + +#if !defined(RTW_SINGLE_WIPHY) + if (ret != _SUCCESS) { + rtw_wiphy_free(wiphy); + adapter->wiphy = NULL; + } + +exit: +#endif + + return ret; +} + +void rtw_cfg80211_ndev_res_free(_adapter *adapter) +{ + rtw_wdev_free(adapter->rtw_wdev); + adapter->rtw_wdev = NULL; +#if !defined(RTW_SINGLE_WIPHY) + rtw_wiphy_free(adapter_to_wiphy(adapter)); + adapter->wiphy = NULL; +#endif +} + + +int rtw_cfg80211_ndev_res_register(_adapter *adapter) +{ +#if !defined(RTW_SINGLE_WIPHY) + int ret = _FAIL; + + if (rtw_wiphy_register(adapter_to_wiphy(adapter)) < 0) { + RTW_INFO("%s rtw_wiphy_register fail for if%d\n", __func__, (adapter->iface_id + 1)); + goto exit; + } + + #ifdef CONFIG_RFKILL_POLL + rtw_cfg80211_init_rfkill(adapter_to_wiphy(adapter)); + #endif + + ret = _SUCCESS; + +exit: + return ret; +#else + return _SUCCESS; +#endif +} + +void rtw_cfg80211_ndev_res_unregister(_adapter *adapter) +{ + rtw_wdev_unregister(adapter->rtw_wdev); +} + +int rtw_cfg80211_dev_res_alloc(struct dvobj_priv *dvobj) +{ + int ret = _FAIL; + +#if defined(RTW_SINGLE_WIPHY) + struct wiphy *wiphy; + struct device *dev = dvobj_to_dev(dvobj); + + wiphy = rtw_wiphy_alloc(dvobj_get_primary_adapter(dvobj), dev); + if (wiphy == NULL) + return ret; + + dvobj->wiphy = wiphy; +#endif + + ret = _SUCCESS; + return ret; +} + +void rtw_cfg80211_dev_res_free(struct dvobj_priv *dvobj) +{ +#if defined(RTW_SINGLE_WIPHY) + rtw_wiphy_free(dvobj_to_wiphy(dvobj)); + dvobj->wiphy = NULL; +#endif +} + +int rtw_cfg80211_dev_res_register(struct dvobj_priv *dvobj) +{ + int ret = _FAIL; + +#if defined(RTW_SINGLE_WIPHY) + if (rtw_wiphy_register(dvobj_to_wiphy(dvobj)) != 0) + return ret; + +#ifdef CONFIG_RFKILL_POLL + rtw_cfg80211_init_rfkill(dvobj_to_wiphy(dvobj)); +#endif +#endif + + ret = _SUCCESS; + + return ret; +} + +void rtw_cfg80211_dev_res_unregister(struct dvobj_priv *dvobj) +{ +#if defined(RTW_SINGLE_WIPHY) +#ifdef CONFIG_RFKILL_POLL + rtw_cfg80211_deinit_rfkill(dvobj_to_wiphy(dvobj)); +#endif + rtw_wiphy_unregister(dvobj_to_wiphy(dvobj)); +#endif +} + +s16 rtw_cfg80211_dev_get_total_txpwr_lmt_mbm(struct dvobj_priv *dvobj) +{ + struct rtw_wiphy_data *wiphy_data; + s16 mbm = UNSPECIFIED_MBM; + + /* TODO: input radio index to choose corresponding wiphy(s) */ + +#if defined(RTW_SINGLE_WIPHY) + wiphy_data = rtw_wiphy_priv(dvobj_to_wiphy(dvobj)); + mbm = wiphy_data->txpwr_total_lmt_mbm; +#else + struct wiphy *wiphy; + int i; + + for (i = 0; i < dvobj->iface_nums; i++) { + if (!dvobj->padapters[i]) + continue; + wiphy = dvobj->padapters[i]->wiphy; + if (!wiphy) + continue; + wiphy_data = rtw_wiphy_priv(wiphy); + if (wiphy_data->txpwr_total_lmt_mbm == UNSPECIFIED_MBM) + continue; + if (mbm > wiphy_data->txpwr_total_lmt_mbm) + mbm = wiphy_data->txpwr_total_lmt_mbm; + } +#endif + + return mbm; +} + +s16 rtw_cfg80211_dev_get_total_txpwr_target_mbm(struct dvobj_priv *dvobj) +{ + struct rtw_wiphy_data *wiphy_data; + s16 mbm = UNSPECIFIED_MBM; + + /* TODO: input radio index to choose corresponding wiphy(s) */ + +#if defined(RTW_SINGLE_WIPHY) + wiphy_data = rtw_wiphy_priv(dvobj_to_wiphy(dvobj)); + mbm = wiphy_data->txpwr_total_target_mbm; +#else + struct wiphy *wiphy; + int i; + + for (i = 0; i < dvobj->iface_nums; i++) { + if (!dvobj->padapters[i]) + continue; + wiphy = dvobj->padapters[i]->wiphy; + if (!wiphy) + continue; + wiphy_data = rtw_wiphy_priv(wiphy); + if (wiphy_data->txpwr_total_target_mbm == UNSPECIFIED_MBM) + continue; + if (mbm > wiphy_data->txpwr_total_target_mbm) + mbm = wiphy_data->txpwr_total_target_mbm; + } +#endif + + return mbm; +} +#endif /* CONFIG_IOCTL_CFG80211 */ diff --git a/os_dep/linux/ioctl_cfg80211.h b/os_dep/linux/ioctl_cfg80211.h new file mode 100644 index 0000000..7c50e32 --- /dev/null +++ b/os_dep/linux/ioctl_cfg80211.h @@ -0,0 +1,454 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#ifndef __IOCTL_CFG80211_H__ +#define __IOCTL_CFG80211_H__ + +#define RTW_CFG80211_BLOCK_DISCON_WHEN_CONNECT BIT0 +#define RTW_CFG80211_BLOCK_DISCON_WHEN_DISCONNECT BIT1 + +#ifndef RTW_CFG80211_BLOCK_STA_DISCON_EVENT +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)) +#define RTW_CFG80211_BLOCK_STA_DISCON_EVENT (RTW_CFG80211_BLOCK_DISCON_WHEN_CONNECT) +#else +#define RTW_CFG80211_BLOCK_STA_DISCON_EVENT (RTW_CFG80211_BLOCK_DISCON_WHEN_CONNECT | RTW_CFG80211_BLOCK_DISCON_WHEN_DISCONNECT) +#endif +#endif + +#if defined(RTW_USE_CFG80211_STA_EVENT) + #undef CONFIG_CFG80211_FORCE_COMPATIBLE_2_6_37_UNDER +#endif + +#ifndef RTW_P2P_GROUP_INTERFACE + #define RTW_P2P_GROUP_INTERFACE 0 +#endif + +/* +* (RTW_P2P_GROUP_INTERFACE, RTW_DEDICATED_P2P_DEVICE) +* (0, 0): wlan0 + p2p0(PD+PG) +* (1, 0): wlan0(with PD) + dynamic PGs +* (1, 1): wlan0 (with dynamic PD wdev) + dynamic PGs +*/ + +#if RTW_P2P_GROUP_INTERFACE + #ifndef CONFIG_RTW_DYNAMIC_NDEV + #define CONFIG_RTW_DYNAMIC_NDEV + #endif + #ifndef RTW_SINGLE_WIPHY + #define RTW_SINGLE_WIPHY + #endif + #ifndef CONFIG_RADIO_WORK + #define CONFIG_RADIO_WORK + #endif + #ifndef RTW_DEDICATED_P2P_DEVICE + #define RTW_DEDICATED_P2P_DEVICE + #endif +#endif + +#ifndef CONFIG_RADIO_WORK +#define RTW_ROCH_DURATION_ENLARGE +#define RTW_ROCH_BACK_OP +#endif + +#if !defined(CONFIG_P2P) && RTW_P2P_GROUP_INTERFACE + #error "RTW_P2P_GROUP_INTERFACE can't be enabled when CONFIG_P2P is disabled\n" +#endif + +#if !RTW_P2P_GROUP_INTERFACE && defined(RTW_DEDICATED_P2P_DEVICE) + #error "RTW_DEDICATED_P2P_DEVICE can't be enabled when RTW_P2P_GROUP_INTERFACE is disabled\n" +#endif + +#if defined(RTW_DEDICATED_P2P_DEVICE) && (LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)) + #error "RTW_DEDICATED_P2P_DEVICE can't be enabled when kernel < 3.7.0\n" +#endif + +#ifdef CONFIG_RTW_MESH + #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)) + #error "CONFIG_RTW_MESH can't be enabled when kernel < 3.10.0\n" + #endif +#endif + +struct rtw_wdev_invit_info { + u8 state; /* 0: req, 1:rep */ + u8 peer_mac[ETH_ALEN]; + u8 group_bssid[ETH_ALEN]; + u8 active; + u8 token; + u8 flags; + u8 status; + u8 req_op_ch; + u8 rsp_op_ch; +}; + +#define rtw_wdev_invit_info_init(invit_info) \ + do { \ + (invit_info)->state = 0xff; \ + _rtw_memset((invit_info)->peer_mac, 0, ETH_ALEN); \ + _rtw_memset((invit_info)->group_bssid, 0, ETH_ALEN); \ + (invit_info)->active = 0xff; \ + (invit_info)->token = 0; \ + (invit_info)->flags = 0x00; \ + (invit_info)->status = 0xff; \ + (invit_info)->req_op_ch = 0; \ + (invit_info)->rsp_op_ch = 0; \ + } while (0) + +struct rtw_wdev_nego_info { + u8 state; /* 0: req, 1:rep, 2:conf */ + u8 iface_addr[ETH_ALEN]; + u8 peer_mac[ETH_ALEN]; + u8 peer_iface_addr[ETH_ALEN]; + u8 active; + u8 token; + u8 status; + u8 req_intent; + u8 req_op_ch; + u8 req_listen_ch; + u8 rsp_intent; + u8 rsp_op_ch; + u8 conf_op_ch; +}; + +#define rtw_wdev_nego_info_init(nego_info) \ + do { \ + (nego_info)->state = 0xff; \ + _rtw_memset((nego_info)->iface_addr, 0, ETH_ALEN); \ + _rtw_memset((nego_info)->peer_mac, 0, ETH_ALEN); \ + _rtw_memset((nego_info)->peer_iface_addr, 0, ETH_ALEN); \ + (nego_info)->active = 0xff; \ + (nego_info)->token = 0; \ + (nego_info)->status = 0xff; \ + (nego_info)->req_intent = 0xff; \ + (nego_info)->req_op_ch = 0; \ + (nego_info)->req_listen_ch = 0; \ + (nego_info)->rsp_intent = 0xff; \ + (nego_info)->rsp_op_ch = 0; \ + (nego_info)->conf_op_ch = 0; \ + } while (0) + +struct rtw_wdev_priv { + struct wireless_dev *rtw_wdev; + + _adapter *padapter; + + #if RTW_CFG80211_BLOCK_STA_DISCON_EVENT + u8 not_indic_disco; + #endif + + struct cfg80211_scan_request *scan_request; + _lock scan_req_lock; + + struct cfg80211_connect_params *connect_req; + _lock connect_req_lock; + + struct net_device *pmon_ndev;/* for monitor interface */ + char ifname_mon[IFNAMSIZ + 1]; /* interface name for monitor interface */ + + u8 p2p_enabled; + systime probe_resp_ie_update_time; + + u8 provdisc_req_issued; + + struct rtw_wdev_invit_info invit_info; + struct rtw_wdev_nego_info nego_info; + + u8 bandroid_scan; + bool block; + bool block_scan; + + /** + * mgmt_regs: bitmap of management frame subtypes registered for the + * given interface + * mcast_mgmt_regs: mcast RX is needed on this interface for these + * subtypes + */ + u32 mgmt_regs; + /* u32 mcast_mgmt_regs; */ + + u8 is_mgmt_tx; + u16 mgmt_tx_cookie; + + _mutex roch_mutex; + +#ifdef CONFIG_CONCURRENT_MODE + ATOMIC_T switch_ch_to; +#endif + +#if defined(CONFIG_RTW_CFGVENDOR_RANDOM_MAC_OUI) || defined(CONFIG_RTW_SCAN_RAND) + u8 pno_mac_addr[ETH_ALEN]; + u16 pno_scan_seq_num; +#endif + +#ifdef CONFIG_RTW_CFGVENDOR_RSSIMONITOR + s8 rssi_monitor_max; + s8 rssi_monitor_min; + u8 rssi_monitor_enable; +#endif + +}; + +enum external_auth_action { + EXTERNAL_AUTH_START, + EXTERNAL_AUTH_ABORT, +}; + +struct rtw_external_auth_params { + enum external_auth_action action; + u8 bssid[ETH_ALEN]__aligned(2); + struct cfg80211_ssid ssid; + unsigned int key_mgmt_suite; + u16 status; + u8 pmkid[PMKID_LEN]; +}; + +bool rtw_cfg80211_is_connect_requested(_adapter *adapter); + +#if RTW_CFG80211_BLOCK_STA_DISCON_EVENT +#define rtw_wdev_not_indic_disco(rtw_wdev_data) ((rtw_wdev_data)->not_indic_disco) +#define rtw_wdev_set_not_indic_disco(rtw_wdev_data, val) do { (rtw_wdev_data)->not_indic_disco = (val); } while (0) +#else +#define rtw_wdev_not_indic_disco(rtw_wdev_data) 0 +#define rtw_wdev_set_not_indic_disco(rtw_wdev_data, val) do {} while (0) +#endif + +#define rtw_wdev_free_connect_req(rtw_wdev_data) \ + do { \ + if ((rtw_wdev_data)->connect_req) { \ + rtw_mfree((u8 *)(rtw_wdev_data)->connect_req, sizeof(*(rtw_wdev_data)->connect_req)); \ + (rtw_wdev_data)->connect_req = NULL; \ + } \ + } while (0) + +#define wdev_to_ndev(w) ((w)->netdev) +#define wdev_to_wiphy(w) ((w)->wiphy) +#define ndev_to_wdev(n) ((n)->ieee80211_ptr) + +struct rtw_wiphy_data { + struct dvobj_priv *dvobj; + +#ifndef RTW_SINGLE_WIPHY + _adapter *adapter; +#endif + +#if defined(RTW_DEDICATED_P2P_DEVICE) + struct wireless_dev *pd_wdev; /* P2P device wdev */ +#endif + + s16 txpwr_total_lmt_mbm; /* EIRP */ + s16 txpwr_total_target_mbm; /* EIRP */ +}; + +#define rtw_wiphy_priv(wiphy) ((struct rtw_wiphy_data *)wiphy_priv(wiphy)) +#define wiphy_to_dvobj(wiphy) (((struct rtw_wiphy_data *)wiphy_priv(wiphy))->dvobj) +#ifdef RTW_SINGLE_WIPHY +#define wiphy_to_adapter(wiphy) (dvobj_get_primary_adapter(wiphy_to_dvobj(wiphy))) +#else +#define wiphy_to_adapter(wiphy) (((struct rtw_wiphy_data *)wiphy_priv(wiphy))->adapter) +#endif + +#if defined(RTW_DEDICATED_P2P_DEVICE) +#define wiphy_to_pd_wdev(wiphy) (rtw_wiphy_priv(wiphy)->pd_wdev) +#else +#define wiphy_to_pd_wdev(wiphy) NULL +#endif + +#define WIPHY_FMT "%s" +#define WIPHY_ARG(wiphy) wiphy_name(wiphy) +#define FUNC_WIPHY_FMT "%s("WIPHY_FMT")" +#define FUNC_WIPHY_ARG(wiphy) __func__, WIPHY_ARG(wiphy) + +#define SET_CFG80211_MGMT_REGS(w, t) (w |= BIT(t >> 4)) +#define CLR_CFG80211_MGMT_REGS(w, t) (w &= (~BIT(t >> 4))) +#define GET_CFG80211_MGMT_REGS(w, t) ((w & BIT(t >> 4)) > 0) + +#define SET_CFG80211_REPORT_MGMT(w, t) (SET_CFG80211_MGMT_REGS(w->mgmt_regs, t)) +#define CLR_CFG80211_REPORT_MGMT(w, t) (CLR_CFG80211_MGMT_REGS(w->mgmt_regs, t)) +#define GET_CFG80211_REPORT_MGMT(w, t) (GET_CFG80211_MGMT_REGS(w->mgmt_regs, t)) + +struct wiphy *rtw_wiphy_alloc(_adapter *padapter, struct device *dev); +void rtw_wiphy_free(struct wiphy *wiphy); +int rtw_wiphy_register(struct wiphy *wiphy); +void rtw_wiphy_unregister(struct wiphy *wiphy); + +int rtw_wdev_alloc(_adapter *padapter, struct wiphy *wiphy); +void rtw_wdev_free(struct wireless_dev *wdev); +void rtw_wdev_unregister(struct wireless_dev *wdev); + +int rtw_cfg80211_ndev_res_alloc(_adapter *adapter); +void rtw_cfg80211_ndev_res_free(_adapter *adapter); +int rtw_cfg80211_ndev_res_register(_adapter *adapter); +void rtw_cfg80211_ndev_res_unregister(_adapter *adapter); + +int rtw_cfg80211_dev_res_alloc(struct dvobj_priv *dvobj); +void rtw_cfg80211_dev_res_free(struct dvobj_priv *dvobj); +int rtw_cfg80211_dev_res_register(struct dvobj_priv *dvobj); +void rtw_cfg80211_dev_res_unregister(struct dvobj_priv *dvobj); +s16 rtw_cfg80211_dev_get_total_txpwr_lmt_mbm(struct dvobj_priv *dvobj); +s16 rtw_cfg80211_dev_get_total_txpwr_target_mbm(struct dvobj_priv *dvobj); + +void rtw_cfg80211_init_wdev_data(_adapter *padapter); + +void rtw_cfg80211_unlink_bss(_adapter *padapter, struct wlan_network *pnetwork); +void rtw_cfg80211_surveydone_event_callback(_adapter *padapter); +struct cfg80211_bss *rtw_cfg80211_inform_bss(_adapter *padapter, struct wlan_network *pnetwork); +int rtw_cfg80211_check_bss(_adapter *padapter); +void rtw_cfg80211_ibss_indicate_connect(_adapter *padapter); +void rtw_cfg80211_indicate_connect(_adapter *padapter); +void rtw_cfg80211_indicate_disconnect(_adapter *padapter, u16 reason, u8 locally_generated); +void rtw_cfg80211_indicate_scan_done(_adapter *adapter, bool aborted); +u32 rtw_cfg80211_wait_scan_req_empty(_adapter *adapter, u32 timeout_ms); + +#ifdef CONFIG_CONCURRENT_MODE +u8 rtw_cfg80211_scan_via_buddy(_adapter *padapter, struct cfg80211_scan_request *request); +void rtw_cfg80211_indicate_scan_done_for_buddy(_adapter *padapter, bool bscan_aborted); +#endif + +#ifdef CONFIG_AP_MODE +void rtw_cfg80211_indicate_sta_assoc(_adapter *padapter, u8 *pmgmt_frame, uint frame_len); +void rtw_cfg80211_indicate_sta_disassoc(_adapter *padapter, const u8 *da, unsigned short reason); +int rtw_cfg80211_set_mgnt_wpsp2pie(struct net_device *net, char *buf, int len, int type); +#endif /* CONFIG_AP_MODE */ + +void rtw_cfg80211_set_is_roch(_adapter *adapter, bool val); +bool rtw_cfg80211_get_is_roch(_adapter *adapter); +bool rtw_cfg80211_is_ro_ch_once(_adapter *adapter); +void rtw_cfg80211_set_last_ro_ch_time(_adapter *adapter); +s32 rtw_cfg80211_get_last_ro_ch_passing_ms(_adapter *adapter); + +#ifdef CONFIG_P2P +int rtw_cfg80211_iface_has_p2p_group_cap(_adapter *adapter); +int rtw_cfg80211_is_p2p_scan(_adapter *adapter); +#if defined(RTW_DEDICATED_P2P_DEVICE) +int rtw_cfg80211_redirect_pd_wdev(struct wiphy *wiphy, u8 *ra, struct wireless_dev **wdev); +int rtw_cfg80211_is_scan_by_pd_wdev(_adapter *adapter); +int rtw_pd_iface_alloc(struct wiphy *wiphy, const char *name, struct wireless_dev **pd_wdev); +void rtw_pd_iface_free(struct wiphy *wiphy); +#endif +#endif /* CONFIG_P2P */ + +void rtw_cfg80211_set_is_mgmt_tx(_adapter *adapter, u8 val); +u8 rtw_cfg80211_get_is_mgmt_tx(_adapter *adapter); +u8 rtw_mgnt_tx_handler(_adapter *adapter, u8 *buf); + +void rtw_cfg80211_issue_p2p_provision_request(_adapter *padapter, const u8 *buf, size_t len); + +void rtw_cfg80211_rx_p2p_action_public(_adapter *padapter, union recv_frame *rframe); +void rtw_cfg80211_rx_action_p2p(_adapter *padapter, union recv_frame *rframe); +void rtw_cfg80211_rx_action(_adapter *adapter, union recv_frame *rframe, const char *msg); +void rtw_cfg80211_rx_mframe(_adapter *adapter, union recv_frame *rframe, const char *msg); +void rtw_cfg80211_rx_probe_request(_adapter *padapter, union recv_frame *rframe); + +void rtw_cfg80211_external_auth_request(_adapter *padapter, union recv_frame *rframe); +void rtw_cfg80211_external_auth_status(struct wiphy *wiphy, struct net_device *dev, + struct rtw_external_auth_params *params); + +bool rtw_cfg80211_pwr_mgmt(_adapter *adapter); +#ifdef CONFIG_RTW_80211K +void rtw_cfg80211_rx_rrm_action(_adapter *adapter, union recv_frame *rframe); +#endif + +#ifdef CONFIG_RFKILL_POLL +void rtw_cfg80211_init_rfkill(struct wiphy *wiphy); +void rtw_cfg80211_deinit_rfkill(struct wiphy *wiphy); +#endif + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)) && !defined(COMPAT_KERNEL_RELEASE) +#define rtw_cfg80211_rx_mgmt(wdev, freq, sig_dbm, buf, len, gfp) cfg80211_rx_mgmt(wdev_to_ndev(wdev), freq, buf, len, gfp) +#elif (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0)) +#define rtw_cfg80211_rx_mgmt(wdev, freq, sig_dbm, buf, len, gfp) cfg80211_rx_mgmt(wdev_to_ndev(wdev), freq, sig_dbm, buf, len, gfp) +#elif (LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0)) +#define rtw_cfg80211_rx_mgmt(wdev, freq, sig_dbm, buf, len, gfp) cfg80211_rx_mgmt(wdev, freq, sig_dbm, buf, len, gfp) +#elif (LINUX_VERSION_CODE < KERNEL_VERSION(3 , 18 , 0)) +#define rtw_cfg80211_rx_mgmt(wdev , freq , sig_dbm , buf , len , gfp) cfg80211_rx_mgmt(wdev , freq , sig_dbm , buf , len , 0 , gfp) +#else +#define rtw_cfg80211_rx_mgmt(wdev , freq , sig_dbm , buf , len , gfp) cfg80211_rx_mgmt(wdev , freq , sig_dbm , buf , len , 0) +#endif + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)) && !defined(COMPAT_KERNEL_RELEASE) +#define rtw_cfg80211_send_rx_assoc(adapter, bss, buf, len) cfg80211_send_rx_assoc((adapter)->pnetdev, buf, len) +#else +#define rtw_cfg80211_send_rx_assoc(adapter, bss, buf, len) cfg80211_send_rx_assoc((adapter)->pnetdev, bss, buf, len) +#endif + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0)) +#define rtw_cfg80211_mgmt_tx_status(wdev, cookie, buf, len, ack, gfp) cfg80211_mgmt_tx_status(wdev_to_ndev(wdev), cookie, buf, len, ack, gfp) +#else +#define rtw_cfg80211_mgmt_tx_status(wdev, cookie, buf, len, ack, gfp) cfg80211_mgmt_tx_status(wdev, cookie, buf, len, ack, gfp) +#endif + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0)) +#define rtw_cfg80211_ready_on_channel(wdev, cookie, chan, channel_type, duration, gfp) cfg80211_ready_on_channel(wdev_to_ndev(wdev), cookie, chan, channel_type, duration, gfp) +#define rtw_cfg80211_remain_on_channel_expired(wdev, cookie, chan, chan_type, gfp) cfg80211_remain_on_channel_expired(wdev_to_ndev(wdev), cookie, chan, chan_type, gfp) +#elif (LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0)) +#define rtw_cfg80211_ready_on_channel(wdev, cookie, chan, channel_type, duration, gfp) cfg80211_ready_on_channel(wdev, cookie, chan, channel_type, duration, gfp) +#define rtw_cfg80211_remain_on_channel_expired(wdev, cookie, chan, chan_type, gfp) cfg80211_remain_on_channel_expired(wdev, cookie, chan, chan_type, gfp) +#else +#define rtw_cfg80211_ready_on_channel(wdev, cookie, chan, channel_type, duration, gfp) cfg80211_ready_on_channel(wdev, cookie, chan, duration, gfp) +#define rtw_cfg80211_remain_on_channel_expired(wdev, cookie, chan, chan_type, gfp) cfg80211_remain_on_channel_expired(wdev, cookie, chan, gfp) +#endif + +#define rtw_cfg80211_connect_result(wdev, bssid, req_ie, req_ie_len, resp_ie, resp_ie_len, status, gfp) cfg80211_connect_result(wdev_to_ndev(wdev), bssid, req_ie, req_ie_len, resp_ie, resp_ie_len, status, gfp) + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 2, 0)) +#define rtw_cfg80211_disconnected(wdev, reason, ie, ie_len, locally_generated, gfp) cfg80211_disconnected(wdev_to_ndev(wdev), reason, ie, ie_len, gfp) +#else +#define rtw_cfg80211_disconnected(wdev, reason, ie, ie_len, locally_generated, gfp) cfg80211_disconnected(wdev_to_ndev(wdev), reason, ie, ie_len, locally_generated, gfp) +#endif + +#ifdef CONFIG_RTW_80211R +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)) +#define rtw_cfg80211_ft_event(adapter, parm) cfg80211_ft_event((adapter)->pnetdev, parm) +#else + #error "Cannot support FT for KERNEL_VERSION < 3.10\n" +#endif +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)) +#define rtw_cfg80211_notify_new_peer_candidate(wdev, addr, ie, ie_len, sig_dbm, gfp) cfg80211_notify_new_peer_candidate(wdev_to_ndev(wdev), addr, ie, ie_len, sig_dbm, gfp) +#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)) +#define rtw_cfg80211_notify_new_peer_candidate(wdev, addr, ie, ie_len, sig_dbm, gfp) cfg80211_notify_new_peer_candidate(wdev_to_ndev(wdev), addr, ie, ie_len, gfp) +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)) +u8 rtw_cfg80211_ch_switch_notify(_adapter *adapter, u8 ch, u8 bw, u8 offset, u8 ht, bool started); +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) && (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31)) +#define IEEE80211_CHAN_NO_HT40PLUS IEEE80211_CHAN_NO_FAT_ABOVE +#define IEEE80211_CHAN_NO_HT40MINUS IEEE80211_CHAN_NO_FAT_BELOW +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)) && (LINUX_VERSION_CODE < KERNEL_VERSION(4, 7, 0)) +#define NL80211_BAND_2GHZ IEEE80211_BAND_2GHZ +#define NL80211_BAND_5GHZ IEEE80211_BAND_5GHZ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) +#define NL80211_BAND_60GHZ IEEE80211_BAND_60GHZ +#endif +#define NUM_NL80211_BANDS IEEE80211_NUM_BANDS +#endif + +#define rtw_band_to_nl80211_band(band) \ + (band == BAND_ON_2_4G) ? NL80211_BAND_2GHZ : \ + (band == BAND_ON_5G) ? NL80211_BAND_5GHZ : NUM_NL80211_BANDS + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)) +#define NL80211_TX_POWER_AUTOMATIC TX_POWER_AUTOMATIC +#define NL80211_TX_POWER_LIMITED TX_POWER_LIMITED +#define NL80211_TX_POWER_FIXED TX_POWER_FIXED +#endif + +#include "wifi_regd.h" +#include "rtw_cfgvendor.h" + +#endif /* __IOCTL_CFG80211_H__ */ diff --git a/os_dep/linux/ioctl_linux.c b/os_dep/linux/ioctl_linux.c new file mode 100644 index 0000000..c60f87f --- /dev/null +++ b/os_dep/linux/ioctl_linux.c @@ -0,0 +1,13058 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2019 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#define _IOCTL_LINUX_C_ + +#include +#include +#include "../../hal/phydm/phydm_precomp.h" +#ifdef RTW_HALMAC +#include "../../hal/hal_halmac.h" +#endif + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)) +#define iwe_stream_add_event(a, b, c, d, e) iwe_stream_add_event(b, c, d, e) +#define iwe_stream_add_point(a, b, c, d, e) iwe_stream_add_point(b, c, d, e) +#endif + +#ifdef CONFIG_80211N_HT +extern int rtw_ht_enable; +#endif + + + +#define RTL_IOCTL_WPA_SUPPLICANT (SIOCIWFIRSTPRIV+30) + +#define SCAN_ITEM_SIZE 768 +#define MAX_CUSTOM_LEN 64 +#define RATE_COUNT 4 +#define MAX_SCAN_BUFFER_LEN 65535 + +#ifdef CONFIG_GLOBAL_UI_PID +extern int ui_pid[3]; +#endif + +/* combo scan */ +#define WEXT_CSCAN_AMOUNT 9 +#define WEXT_CSCAN_BUF_LEN 360 +#define WEXT_CSCAN_HEADER "CSCAN S\x01\x00\x00S\x00" +#define WEXT_CSCAN_HEADER_SIZE 12 +#define WEXT_CSCAN_SSID_SECTION 'S' +#define WEXT_CSCAN_CHANNEL_SECTION 'C' +#define WEXT_CSCAN_NPROBE_SECTION 'N' +#define WEXT_CSCAN_ACTV_DWELL_SECTION 'A' +#define WEXT_CSCAN_PASV_DWELL_SECTION 'P' +#define WEXT_CSCAN_HOME_DWELL_SECTION 'H' +#define WEXT_CSCAN_TYPE_SECTION 'T' + + +extern u8 key_2char2num(u8 hch, u8 lch); +extern u8 str_2char2num(u8 hch, u8 lch); +extern void macstr2num(u8 *dst, u8 *src); +extern u8 convert_ip_addr(u8 hch, u8 mch, u8 lch); + +u32 rtw_rates[] = {1000000, 2000000, 5500000, 11000000, + 6000000, 9000000, 12000000, 18000000, 24000000, 36000000, 48000000, 54000000}; + +#ifdef CONFIG_RTW_ANDROID +static void indicate_wx_custom_event(_adapter *padapter, char *msg) +{ + u8 *buff; + union iwreq_data wrqu; + + if (strlen(msg) > IW_CUSTOM_MAX) { + RTW_INFO("%s strlen(msg):%zu > IW_CUSTOM_MAX:%u\n", __FUNCTION__ , strlen(msg), IW_CUSTOM_MAX); + return; + } + + buff = rtw_zmalloc(IW_CUSTOM_MAX + 1); + if (!buff) + return; + + _rtw_memcpy(buff, msg, strlen(msg)); + + _rtw_memset(&wrqu, 0, sizeof(wrqu)); + wrqu.data.length = strlen(msg); + + RTW_INFO("%s %s\n", __FUNCTION__, buff); +#ifndef CONFIG_IOCTL_CFG80211 + wireless_send_event(padapter->pnetdev, IWEVCUSTOM, &wrqu, buff); +#endif + + rtw_mfree(buff, IW_CUSTOM_MAX + 1); + +} +#endif + +#if 0 +static void request_wps_pbc_event(_adapter *padapter) +{ + u8 *buff, *p; + union iwreq_data wrqu; + + + buff = rtw_malloc(IW_CUSTOM_MAX); + if (!buff) + return; + + _rtw_memset(buff, 0, IW_CUSTOM_MAX); + + p = buff; + + p += sprintf(p, "WPS_PBC_START.request=TRUE"); + + _rtw_memset(&wrqu, 0, sizeof(wrqu)); + + wrqu.data.length = p - buff; + + wrqu.data.length = (wrqu.data.length < IW_CUSTOM_MAX) ? wrqu.data.length : IW_CUSTOM_MAX; + + RTW_INFO("%s\n", __FUNCTION__); + +#ifndef CONFIG_IOCTL_CFG80211 + wireless_send_event(padapter->pnetdev, IWEVCUSTOM, &wrqu, buff); +#endif + + if (buff) + rtw_mfree(buff, IW_CUSTOM_MAX); + +} +#endif + +#ifdef CONFIG_SUPPORT_HW_WPS_PBC +void rtw_request_wps_pbc_event(_adapter *padapter) +{ +#ifdef RTK_DMP_PLATFORM +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 12)) + kobject_uevent(&padapter->pnetdev->dev.kobj, KOBJ_NET_PBC); +#else + kobject_hotplug(&padapter->pnetdev->class_dev.kobj, KOBJ_NET_PBC); +#endif +#else + + if (padapter->pid[0] == 0) { + /* 0 is the default value and it means the application monitors the HW PBC doesn't privde its pid to driver. */ + return; + } + + rtw_signal_process(padapter->pid[0], SIGUSR1); + +#endif + + rtw_led_control(padapter, LED_CTL_START_WPS_BOTTON); +} +#endif/* #ifdef CONFIG_SUPPORT_HW_WPS_PBC */ + +void indicate_wx_scan_complete_event(_adapter *padapter) +{ + union iwreq_data wrqu; + + _rtw_memset(&wrqu, 0, sizeof(union iwreq_data)); + + /* RTW_INFO("+rtw_indicate_wx_scan_complete_event\n"); */ +#ifndef CONFIG_IOCTL_CFG80211 + wireless_send_event(padapter->pnetdev, SIOCGIWSCAN, &wrqu, NULL); +#endif +} + + +void rtw_indicate_wx_assoc_event(_adapter *padapter) +{ + union iwreq_data wrqu; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv; + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + WLAN_BSSID_EX *pnetwork = (WLAN_BSSID_EX *)(&(pmlmeinfo->network)); + + _rtw_memset(&wrqu, 0, sizeof(union iwreq_data)); + + wrqu.ap_addr.sa_family = ARPHRD_ETHER; + + if (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == _TRUE) + _rtw_memcpy(wrqu.ap_addr.sa_data, pnetwork->MacAddress, ETH_ALEN); + else + _rtw_memcpy(wrqu.ap_addr.sa_data, pmlmepriv->cur_network.network.MacAddress, ETH_ALEN); + + RTW_PRINT("assoc success\n"); +#ifndef CONFIG_IOCTL_CFG80211 + wireless_send_event(padapter->pnetdev, SIOCGIWAP, &wrqu, NULL); +#endif +} + +void rtw_indicate_wx_disassoc_event(_adapter *padapter) +{ + union iwreq_data wrqu; + + _rtw_memset(&wrqu, 0, sizeof(union iwreq_data)); + + wrqu.ap_addr.sa_family = ARPHRD_ETHER; + _rtw_memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN); + +#ifndef CONFIG_IOCTL_CFG80211 + RTW_PRINT("indicate disassoc\n"); + wireless_send_event(padapter->pnetdev, SIOCGIWAP, &wrqu, NULL); +#endif +} + +/* +uint rtw_is_cckrates_included(u8 *rate) +{ + u32 i = 0; + + while(rate[i]!=0) + { + if ( (((rate[i]) & 0x7f) == 2) || (((rate[i]) & 0x7f) == 4) || + (((rate[i]) & 0x7f) == 11) || (((rate[i]) & 0x7f) == 22) ) + return _TRUE; + i++; + } + + return _FALSE; +} + +uint rtw_is_cckratesonly_included(u8 *rate) +{ + u32 i = 0; + + while(rate[i]!=0) + { + if ( (((rate[i]) & 0x7f) != 2) && (((rate[i]) & 0x7f) != 4) && + (((rate[i]) & 0x7f) != 11) && (((rate[i]) & 0x7f) != 22) ) + return _FALSE; + i++; + } + + return _TRUE; +} +*/ + +#ifdef CONFIG_IOCTL_WEXT +static int search_p2p_wfd_ie(_adapter *padapter, + struct iw_request_info *info, struct wlan_network *pnetwork, + char *start, char *stop) +{ +#ifdef CONFIG_P2P + struct wifidirect_info *pwdinfo = &padapter->wdinfo; +#ifdef CONFIG_WFD + if (SCAN_RESULT_ALL == pwdinfo->wfd_info->scan_result_type) { + + } else if ((SCAN_RESULT_P2P_ONLY == pwdinfo->wfd_info->scan_result_type) || + (SCAN_RESULT_WFD_TYPE == pwdinfo->wfd_info->scan_result_type)) +#endif /* CONFIG_WFD */ + { + if (!rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) { + u32 blnGotP2PIE = _FALSE; + + /* User is doing the P2P device discovery */ + /* The prefix of SSID should be "DIRECT-" and the IE should contains the P2P IE. */ + /* If not, the driver should ignore this AP and go to the next AP. */ + + /* Verifying the SSID */ + if (_rtw_memcmp(pnetwork->network.Ssid.Ssid, pwdinfo->p2p_wildcard_ssid, P2P_WILDCARD_SSID_LEN)) { + u32 p2pielen = 0; + + /* Verifying the P2P IE */ + if (rtw_bss_ex_get_p2p_ie(&pnetwork->network, NULL, &p2pielen)) + blnGotP2PIE = _TRUE; + } + + if (blnGotP2PIE == _FALSE) + return _FALSE; + + } + } + +#ifdef CONFIG_WFD + if (SCAN_RESULT_WFD_TYPE == pwdinfo->wfd_info->scan_result_type) { + u32 blnGotWFD = _FALSE; + u8 *wfd_ie; + uint wfd_ielen = 0; + + wfd_ie = rtw_bss_ex_get_wfd_ie(&pnetwork->network, NULL, &wfd_ielen); + if (wfd_ie) { + u8 *wfd_devinfo; + uint wfd_devlen; + + wfd_devinfo = rtw_get_wfd_attr_content(wfd_ie, wfd_ielen, WFD_ATTR_DEVICE_INFO, NULL, &wfd_devlen); + if (wfd_devinfo) { + if (pwdinfo->wfd_info->wfd_device_type == WFD_DEVINFO_PSINK) { + /* the first two bits will indicate the WFD device type */ + if ((wfd_devinfo[1] & 0x03) == WFD_DEVINFO_SOURCE) { + /* If this device is Miracast PSink device, the scan reuslt should just provide the Miracast source. */ + blnGotWFD = _TRUE; + } + } else if (pwdinfo->wfd_info->wfd_device_type == WFD_DEVINFO_SOURCE) { + /* the first two bits will indicate the WFD device type */ + if ((wfd_devinfo[1] & 0x03) == WFD_DEVINFO_PSINK) { + /* If this device is Miracast source device, the scan reuslt should just provide the Miracast PSink. */ + /* Todo: How about the SSink?! */ + blnGotWFD = _TRUE; + } + } + } + } + + if (blnGotWFD == _FALSE) + return _FALSE; + } +#endif /* CONFIG_WFD */ + +#endif /* CONFIG_P2P */ + return _TRUE; +} +static inline char *iwe_stream_mac_addr_proess(_adapter *padapter, + struct iw_request_info *info, struct wlan_network *pnetwork, + char *start, char *stop, struct iw_event *iwe) +{ + /* AP MAC address */ + iwe->cmd = SIOCGIWAP; + iwe->u.ap_addr.sa_family = ARPHRD_ETHER; + + _rtw_memcpy(iwe->u.ap_addr.sa_data, pnetwork->network.MacAddress, ETH_ALEN); + start = iwe_stream_add_event(info, start, stop, iwe, IW_EV_ADDR_LEN); + return start; +} +static inline char *iwe_stream_essid_proess(_adapter *padapter, + struct iw_request_info *info, struct wlan_network *pnetwork, + char *start, char *stop, struct iw_event *iwe) +{ + + /* Add the ESSID */ + iwe->cmd = SIOCGIWESSID; + iwe->u.data.flags = 1; + iwe->u.data.length = min((u16)pnetwork->network.Ssid.SsidLength, (u16)32); + start = iwe_stream_add_point(info, start, stop, iwe, pnetwork->network.Ssid.Ssid); + return start; +} + +static inline char *iwe_stream_chan_process(_adapter *padapter, + struct iw_request_info *info, struct wlan_network *pnetwork, + char *start, char *stop, struct iw_event *iwe) +{ + if (pnetwork->network.Configuration.DSConfig < 1 /*|| pnetwork->network.Configuration.DSConfig>14*/) + pnetwork->network.Configuration.DSConfig = 1; + + /* Add frequency/channel */ + iwe->cmd = SIOCGIWFREQ; + iwe->u.freq.m = rtw_ch2freq(pnetwork->network.Configuration.DSConfig) * 100000; + iwe->u.freq.e = 1; + iwe->u.freq.i = pnetwork->network.Configuration.DSConfig; + start = iwe_stream_add_event(info, start, stop, iwe, IW_EV_FREQ_LEN); + return start; +} +static inline char *iwe_stream_mode_process(_adapter *padapter, + struct iw_request_info *info, struct wlan_network *pnetwork, + char *start, char *stop, struct iw_event *iwe, u16 cap) +{ + /* Add mode */ + if (cap & (WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_BSS)) { + iwe->cmd = SIOCGIWMODE; + if (cap & WLAN_CAPABILITY_BSS) + iwe->u.mode = IW_MODE_MASTER; + else + iwe->u.mode = IW_MODE_ADHOC; + + start = iwe_stream_add_event(info, start, stop, iwe, IW_EV_UINT_LEN); + } + return start; +} +static inline char *iwe_stream_encryption_process(_adapter *padapter, + struct iw_request_info *info, struct wlan_network *pnetwork, + char *start, char *stop, struct iw_event *iwe, u16 cap) +{ + + /* Add encryption capability */ + iwe->cmd = SIOCGIWENCODE; + if (cap & WLAN_CAPABILITY_PRIVACY) + iwe->u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY; + else + iwe->u.data.flags = IW_ENCODE_DISABLED; + iwe->u.data.length = 0; + start = iwe_stream_add_point(info, start, stop, iwe, pnetwork->network.Ssid.Ssid); + return start; + +} + +static inline char *iwe_stream_protocol_process(_adapter *padapter, + struct iw_request_info *info, struct wlan_network *pnetwork, + char *start, char *stop, struct iw_event *iwe) +{ + u16 ht_cap = _FALSE, vht_cap = _FALSE; + u32 ht_ielen = 0, vht_ielen = 0; + char *p; + u8 ie_offset = (pnetwork->network.Reserved[0] == BSS_TYPE_PROB_REQ ? 0 : 12); /* Probe Request */ + +#ifdef CONFIG_80211N_HT + /* parsing HT_CAP_IE */ + if(padapter->registrypriv.ht_enable && is_supported_ht(padapter->registrypriv.wireless_mode)) { + p = rtw_get_ie(&pnetwork->network.IEs[ie_offset], _HT_CAPABILITY_IE_, &ht_ielen, pnetwork->network.IELength - ie_offset); + if (p && ht_ielen > 0) + ht_cap = _TRUE; + } +#endif + +#ifdef CONFIG_80211AC_VHT + /* parsing VHT_CAP_IE */ + if(padapter->registrypriv.wireless_mode & WIRELESS_11AC) { + p = rtw_get_ie(&pnetwork->network.IEs[ie_offset], EID_VHTCapability, &vht_ielen, pnetwork->network.IELength - ie_offset); + if (p && vht_ielen > 0) + vht_cap = _TRUE; + } +#endif + /* Add the protocol name */ + iwe->cmd = SIOCGIWNAME; + if ((rtw_is_cckratesonly_included((u8 *)&pnetwork->network.SupportedRates)) == _TRUE) { + if (ht_cap == _TRUE) + snprintf(iwe->u.name, IFNAMSIZ, "IEEE 802.11bn"); + else + snprintf(iwe->u.name, IFNAMSIZ, "IEEE 802.11b"); + } else if ((rtw_is_cckrates_included((u8 *)&pnetwork->network.SupportedRates)) == _TRUE) { + if (ht_cap == _TRUE) + snprintf(iwe->u.name, IFNAMSIZ, "IEEE 802.11bgn"); + else + snprintf(iwe->u.name, IFNAMSIZ, "IEEE 802.11bg"); + } else { + if (pnetwork->network.Configuration.DSConfig > 14) { + #ifdef CONFIG_80211AC_VHT + if (vht_cap == _TRUE) + snprintf(iwe->u.name, IFNAMSIZ, "IEEE 802.11AC"); + else + #endif + { + if (ht_cap == _TRUE) + snprintf(iwe->u.name, IFNAMSIZ, "IEEE 802.11an"); + else + snprintf(iwe->u.name, IFNAMSIZ, "IEEE 802.11a"); + } + } else { + if (ht_cap == _TRUE) + snprintf(iwe->u.name, IFNAMSIZ, "IEEE 802.11gn"); + else + snprintf(iwe->u.name, IFNAMSIZ, "IEEE 802.11g"); + } + } + start = iwe_stream_add_event(info, start, stop, iwe, IW_EV_CHAR_LEN); + return start; +} + +static inline char *iwe_stream_rate_process(_adapter *padapter, + struct iw_request_info *info, struct wlan_network *pnetwork, + char *start, char *stop, struct iw_event *iwe) +{ + u32 ht_ielen = 0, vht_ielen = 0; + char *p; + u16 max_rate = 0, rate, ht_cap = _FALSE, vht_cap = _FALSE; + u32 i = 0; + u8 bw_40MHz = 0, short_GI = 0, bw_160MHz = 0, vht_highest_rate = 0; + u16 mcs_rate = 0, vht_data_rate = 0; + char custom[MAX_CUSTOM_LEN] = {0}; + u8 ie_offset = (pnetwork->network.Reserved[0] == BSS_TYPE_PROB_REQ ? 0 : 12); /* Probe Request */ + + /* parsing HT_CAP_IE */ + if(is_supported_ht(padapter->registrypriv.wireless_mode)) { + p = rtw_get_ie(&pnetwork->network.IEs[ie_offset], _HT_CAPABILITY_IE_, &ht_ielen, pnetwork->network.IELength - ie_offset); + if (p && ht_ielen > 0) { + struct rtw_ieee80211_ht_cap *pht_capie; + ht_cap = _TRUE; + pht_capie = (struct rtw_ieee80211_ht_cap *)(p + 2); + _rtw_memcpy(&mcs_rate , pht_capie->supp_mcs_set, 2); + bw_40MHz = (pht_capie->cap_info & IEEE80211_HT_CAP_SUP_WIDTH) ? 1 : 0; + short_GI = (pht_capie->cap_info & (IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40)) ? 1 : 0; + } + } +#ifdef CONFIG_80211AC_VHT + /* parsing VHT_CAP_IE */ + if(padapter->registrypriv.wireless_mode & WIRELESS_11AC){ + p = rtw_get_ie(&pnetwork->network.IEs[ie_offset], EID_VHTCapability, &vht_ielen, pnetwork->network.IELength - ie_offset); + if (p && vht_ielen > 0) { + u8 mcs_map[2]; + + vht_cap = _TRUE; + bw_160MHz = GET_VHT_CAPABILITY_ELE_CHL_WIDTH(p + 2); + if (bw_160MHz) + short_GI = GET_VHT_CAPABILITY_ELE_SHORT_GI160M(p + 2); + else + short_GI = GET_VHT_CAPABILITY_ELE_SHORT_GI80M(p + 2); + + _rtw_memcpy(mcs_map, GET_VHT_CAPABILITY_ELE_TX_MCS(p + 2), 2); + + vht_highest_rate = rtw_get_vht_highest_rate(mcs_map); + vht_data_rate = rtw_vht_mcs_to_data_rate(CHANNEL_WIDTH_80, short_GI, vht_highest_rate); + } + } +#endif + + /*Add basic and extended rates */ + p = custom; + p += snprintf(p, MAX_CUSTOM_LEN - (p - custom), " Rates (Mb/s): "); + while (pnetwork->network.SupportedRates[i] != 0) { + rate = pnetwork->network.SupportedRates[i] & 0x7F; + if (rate > max_rate) + max_rate = rate; + p += snprintf(p, MAX_CUSTOM_LEN - (p - custom), + "%d%s ", rate >> 1, (rate & 1) ? ".5" : ""); + i++; + } +#ifdef CONFIG_80211AC_VHT + if (vht_cap == _TRUE) + max_rate = vht_data_rate; + else +#endif + if (ht_cap == _TRUE) { + if (mcs_rate & 0x8000) /* MCS15 */ + max_rate = (bw_40MHz) ? ((short_GI) ? 300 : 270) : ((short_GI) ? 144 : 130); + + else if (mcs_rate & 0x0080) /* MCS7 */ + max_rate = (bw_40MHz) ? ((short_GI) ? 150 : 135) : ((short_GI) ? 72 : 65); + else { /* default MCS7 */ + /* RTW_INFO("wx_get_scan, mcs_rate_bitmap=0x%x\n", mcs_rate); */ + max_rate = (bw_40MHz) ? ((short_GI) ? 150 : 135) : ((short_GI) ? 72 : 65); + } + + max_rate = max_rate * 2; /* Mbps/2; */ + } + + iwe->cmd = SIOCGIWRATE; + iwe->u.bitrate.fixed = iwe->u.bitrate.disabled = 0; + iwe->u.bitrate.value = max_rate * 500000; + start = iwe_stream_add_event(info, start, stop, iwe, IW_EV_PARAM_LEN); + return start ; +} + +static inline char *iwe_stream_wpa_wpa2_process(_adapter *padapter, + struct iw_request_info *info, struct wlan_network *pnetwork, + char *start, char *stop, struct iw_event *iwe) +{ + int buf_size = MAX_WPA_IE_LEN * 2; + /* u8 pbuf[buf_size]={0}; */ + u8 *pbuf = rtw_zmalloc(buf_size); + + u8 wpa_ie[255] = {0}, rsn_ie[255] = {0}; + u16 i, wpa_len = 0, rsn_len = 0; + u8 *p; + sint out_len = 0; + + + if (pbuf) { + p = pbuf; + + /* parsing WPA/WPA2 IE */ + if (pnetwork->network.Reserved[0] != BSS_TYPE_PROB_REQ) { /* Probe Request */ + out_len = rtw_get_sec_ie(pnetwork->network.IEs , pnetwork->network.IELength, rsn_ie, &rsn_len, wpa_ie, &wpa_len); + + if (wpa_len > 0) { + + _rtw_memset(pbuf, 0, buf_size); + p += sprintf(p, "wpa_ie="); + for (i = 0; i < wpa_len; i++) + p += sprintf(p, "%02x", wpa_ie[i]); + + if (wpa_len > 100) { + printk("-----------------Len %d----------------\n", wpa_len); + for (i = 0; i < wpa_len; i++) + printk("%02x ", wpa_ie[i]); + printk("\n"); + printk("-----------------Len %d----------------\n", wpa_len); + } + + _rtw_memset(iwe, 0, sizeof(*iwe)); + iwe->cmd = IWEVCUSTOM; + iwe->u.data.length = strlen(pbuf); + start = iwe_stream_add_point(info, start, stop, iwe, pbuf); + + _rtw_memset(iwe, 0, sizeof(*iwe)); + iwe->cmd = IWEVGENIE; + iwe->u.data.length = wpa_len; + start = iwe_stream_add_point(info, start, stop, iwe, wpa_ie); + } + if (rsn_len > 0) { + + _rtw_memset(pbuf, 0, buf_size); + p += sprintf(p, "rsn_ie="); + for (i = 0; i < rsn_len; i++) + p += sprintf(p, "%02x", rsn_ie[i]); + _rtw_memset(iwe, 0, sizeof(*iwe)); + iwe->cmd = IWEVCUSTOM; + iwe->u.data.length = strlen(pbuf); + start = iwe_stream_add_point(info, start, stop, iwe, pbuf); + + _rtw_memset(iwe, 0, sizeof(*iwe)); + iwe->cmd = IWEVGENIE; + iwe->u.data.length = rsn_len; + start = iwe_stream_add_point(info, start, stop, iwe, rsn_ie); + } + } + + rtw_mfree(pbuf, buf_size); + } + return start; +} + +static inline char *iwe_stream_wps_process(_adapter *padapter, + struct iw_request_info *info, struct wlan_network *pnetwork, + char *start, char *stop, struct iw_event *iwe) +{ + /* parsing WPS IE */ + uint cnt = 0, total_ielen; + u8 *wpsie_ptr = NULL; + uint wps_ielen = 0; + u8 ie_offset = (pnetwork->network.Reserved[0] == BSS_TYPE_PROB_REQ ? 0 : 12); + + u8 *ie_ptr = pnetwork->network.IEs + ie_offset; + total_ielen = pnetwork->network.IELength - ie_offset; + + if (pnetwork->network.Reserved[0] == BSS_TYPE_PROB_REQ) { /* Probe Request */ + ie_ptr = pnetwork->network.IEs; + total_ielen = pnetwork->network.IELength; + } else { /* Beacon or Probe Respones */ + ie_ptr = pnetwork->network.IEs + _FIXED_IE_LENGTH_; + total_ielen = pnetwork->network.IELength - _FIXED_IE_LENGTH_; + } + while (cnt < total_ielen) { + if (rtw_is_wps_ie(&ie_ptr[cnt], &wps_ielen) && (wps_ielen > 2)) { + wpsie_ptr = &ie_ptr[cnt]; + iwe->cmd = IWEVGENIE; + iwe->u.data.length = (u16)wps_ielen; + start = iwe_stream_add_point(info, start, stop, iwe, wpsie_ptr); + } + cnt += ie_ptr[cnt + 1] + 2; /* goto next */ + } + return start; +} + +static inline char *iwe_stream_wapi_process(_adapter *padapter, + struct iw_request_info *info, struct wlan_network *pnetwork, + char *start, char *stop, struct iw_event *iwe) +{ +#ifdef CONFIG_WAPI_SUPPORT + char *p; + + if (pnetwork->network.Reserved[0] != BSS_TYPE_PROB_REQ) { /* Probe Request */ + sint out_len_wapi = 0; + /* here use static for stack size */ + static u8 buf_wapi[MAX_WAPI_IE_LEN * 2] = {0}; + static u8 wapi_ie[MAX_WAPI_IE_LEN] = {0}; + u16 wapi_len = 0; + u16 i; + + out_len_wapi = rtw_get_wapi_ie(pnetwork->network.IEs , pnetwork->network.IELength, wapi_ie, &wapi_len); + + RTW_INFO("rtw_wx_get_scan: %s ", pnetwork->network.Ssid.Ssid); + RTW_INFO("rtw_wx_get_scan: ssid = %d ", wapi_len); + + + if (wapi_len > 0) { + p = buf_wapi; + /* _rtw_memset(buf_wapi, 0, MAX_WAPI_IE_LEN*2); */ + p += sprintf(p, "wapi_ie="); + for (i = 0; i < wapi_len; i++) + p += sprintf(p, "%02x", wapi_ie[i]); + + _rtw_memset(iwe, 0, sizeof(*iwe)); + iwe->cmd = IWEVCUSTOM; + iwe->u.data.length = strlen(buf_wapi); + start = iwe_stream_add_point(info, start, stop, iwe, buf_wapi); + + _rtw_memset(iwe, 0, sizeof(*iwe)); + iwe->cmd = IWEVGENIE; + iwe->u.data.length = wapi_len; + start = iwe_stream_add_point(info, start, stop, iwe, wapi_ie); + } + } +#endif/* #ifdef CONFIG_WAPI_SUPPORT */ + return start; +} + +static inline char *iwe_stream_rssi_process(_adapter *padapter, + struct iw_request_info *info, struct wlan_network *pnetwork, + char *start, char *stop, struct iw_event *iwe) +{ + u8 ss, sq; + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); +#ifdef CONFIG_BACKGROUND_NOISE_MONITOR + s16 noise = 0; +#endif + + /* Add quality statistics */ + iwe->cmd = IWEVQUAL; + iwe->u.qual.updated = IW_QUAL_QUAL_UPDATED | IW_QUAL_LEVEL_UPDATED +#ifdef CONFIG_BACKGROUND_NOISE_MONITOR + | IW_QUAL_NOISE_UPDATED +#else + | IW_QUAL_NOISE_INVALID +#endif +#ifdef CONFIG_SIGNAL_DISPLAY_DBM + | IW_QUAL_DBM +#endif + ; + + if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _TRUE && + is_same_network(&pmlmepriv->cur_network.network, &pnetwork->network, 0)) { + ss = padapter->recvpriv.signal_strength; + sq = padapter->recvpriv.signal_qual; + } else { + ss = pnetwork->network.PhyInfo.SignalStrength; + sq = pnetwork->network.PhyInfo.SignalQuality; + } + + +#ifdef CONFIG_SIGNAL_DISPLAY_DBM + iwe->u.qual.level = (u8) translate_percentage_to_dbm(ss); /* dbm */ +#else + iwe->u.qual.level = (u8)ss; /* % */ +#endif + + iwe->u.qual.qual = (u8)sq; /* signal quality */ + +#ifdef CONFIG_PLATFORM_ROCKCHIPS + iwe->u.qual.noise = -100; /* noise level suggest by zhf@rockchips */ +#else +#ifdef CONFIG_BACKGROUND_NOISE_MONITOR + if (IS_NM_ENABLE(padapter)) { + noise = rtw_noise_query_by_chan_num(padapter, pnetwork->network.Configuration.DSConfig); + #ifndef CONFIG_SIGNAL_DISPLAY_DBM + noise = translate_dbm_to_percentage(noise);/*percentage*/ + #endif + iwe->u.qual.noise = noise; + } +#else + iwe->u.qual.noise = 0; /* noise level */ +#endif +#endif /* CONFIG_PLATFORM_ROCKCHIPS */ + + /* RTW_INFO("iqual=%d, ilevel=%d, inoise=%d, iupdated=%d\n", iwe.u.qual.qual, iwe.u.qual.level , iwe.u.qual.noise, iwe.u.qual.updated); */ + + start = iwe_stream_add_event(info, start, stop, iwe, IW_EV_QUAL_LEN); + return start; +} + +static inline char *iwe_stream_net_rsv_process(_adapter *padapter, + struct iw_request_info *info, struct wlan_network *pnetwork, + char *start, char *stop, struct iw_event *iwe) +{ + u8 buf[32] = {0}; + u8 *p, *pos; + p = buf; + pos = pnetwork->network.Reserved; + + p += sprintf(p, "fm=%02X%02X", pos[1], pos[0]); + _rtw_memset(iwe, 0, sizeof(*iwe)); + iwe->cmd = IWEVCUSTOM; + iwe->u.data.length = strlen(buf); + start = iwe_stream_add_point(info, start, stop, iwe, buf); + return start; +} + +static char *translate_scan(_adapter *padapter, + struct iw_request_info *info, struct wlan_network *pnetwork, + char *start, char *stop) +{ + struct iw_event iwe; + u16 cap = 0; + _rtw_memset(&iwe, 0, sizeof(iwe)); + + if (_FALSE == search_p2p_wfd_ie(padapter, info, pnetwork, start, stop)) + return start; + + start = iwe_stream_mac_addr_proess(padapter, info, pnetwork, start, stop, &iwe); + start = iwe_stream_essid_proess(padapter, info, pnetwork, start, stop, &iwe); + start = iwe_stream_protocol_process(padapter, info, pnetwork, start, stop, &iwe); + if (pnetwork->network.Reserved[0] == BSS_TYPE_PROB_REQ) /* Probe Request */ + cap = 0; + else { + _rtw_memcpy((u8 *)&cap, rtw_get_capability_from_ie(pnetwork->network.IEs), 2); + cap = le16_to_cpu(cap); + } + + start = iwe_stream_mode_process(padapter, info, pnetwork, start, stop, &iwe, cap); + start = iwe_stream_chan_process(padapter, info, pnetwork, start, stop, &iwe); + start = iwe_stream_encryption_process(padapter, info, pnetwork, start, stop, &iwe, cap); + start = iwe_stream_rate_process(padapter, info, pnetwork, start, stop, &iwe); + start = iwe_stream_wpa_wpa2_process(padapter, info, pnetwork, start, stop, &iwe); + start = iwe_stream_wps_process(padapter, info, pnetwork, start, stop, &iwe); + start = iwe_stream_wapi_process(padapter, info, pnetwork, start, stop, &iwe); + start = iwe_stream_rssi_process(padapter, info, pnetwork, start, stop, &iwe); + start = iwe_stream_net_rsv_process(padapter, info, pnetwork, start, stop, &iwe); + + return start; +} + +static int wpa_set_auth_algs(struct net_device *dev, u32 value) +{ + _adapter *padapter = (_adapter *) rtw_netdev_priv(dev); + int ret = 0; + + if ((value & AUTH_ALG_SHARED_KEY) && (value & AUTH_ALG_OPEN_SYSTEM)) { + RTW_INFO("wpa_set_auth_algs, AUTH_ALG_SHARED_KEY and AUTH_ALG_OPEN_SYSTEM [value:0x%x]\n", value); + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled; + padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeAutoSwitch; + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Auto; + } else if (value & AUTH_ALG_SHARED_KEY) { + RTW_INFO("wpa_set_auth_algs, AUTH_ALG_SHARED_KEY [value:0x%x]\n", value); + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled; + +#ifdef CONFIG_PLATFORM_MT53XX + padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeAutoSwitch; + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Auto; +#else + padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeShared; + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Shared; +#endif + } else if (value & AUTH_ALG_OPEN_SYSTEM) { + RTW_INFO("wpa_set_auth_algs, AUTH_ALG_OPEN_SYSTEM\n"); + /* padapter->securitypriv.ndisencryptstatus = Ndis802_11EncryptionDisabled; */ + if (padapter->securitypriv.ndisauthtype < Ndis802_11AuthModeWPAPSK) { +#ifdef CONFIG_PLATFORM_MT53XX + padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeAutoSwitch; + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Auto; +#else + padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeOpen; + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Open; +#endif + } + + } else if (value & AUTH_ALG_LEAP) + RTW_INFO("wpa_set_auth_algs, AUTH_ALG_LEAP\n"); + else { + RTW_INFO("wpa_set_auth_algs, error!\n"); + ret = -EINVAL; + } + + return ret; + +} + +static int wpa_set_encryption(struct net_device *dev, struct ieee_param *param, u32 param_len) +{ + int ret = 0; + u32 wep_key_idx, wep_key_len; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct security_priv *psecuritypriv = &padapter->securitypriv; +#ifdef CONFIG_P2P + struct wifidirect_info *pwdinfo = &padapter->wdinfo; +#endif /* CONFIG_P2P */ + + + param->u.crypt.err = 0; + param->u.crypt.alg[IEEE_CRYPT_ALG_NAME_LEN - 1] = '\0'; + + if (param_len < (u32)((u8 *) param->u.crypt.key - (u8 *) param) + param->u.crypt.key_len) { + ret = -EINVAL; + goto exit; + } + + if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff && + param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff && + param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) { + + if (param->u.crypt.idx >= WEP_KEYS +#ifdef CONFIG_IEEE80211W + && param->u.crypt.idx > BIP_MAX_KEYID +#endif /* CONFIG_IEEE80211W */ + ) { + ret = -EINVAL; + goto exit; + } + } else { +#ifdef CONFIG_WAPI_SUPPORT + if (strcmp(param->u.crypt.alg, "SMS4")) +#endif + { + ret = -EINVAL; + goto exit; + } + } + + if (strcmp(param->u.crypt.alg, "WEP") == 0) { + RTW_INFO("wpa_set_encryption, crypt.alg = WEP\n"); + + wep_key_idx = param->u.crypt.idx; + wep_key_len = param->u.crypt.key_len; + + if ((wep_key_idx >= WEP_KEYS) || (wep_key_len <= 0)) { + ret = -EINVAL; + goto exit; + } + + if (psecuritypriv->bWepDefaultKeyIdxSet == 0) { + /* wep default key has not been set, so use this key index as default key.*/ + + wep_key_len = wep_key_len <= 5 ? 5 : 13; + + psecuritypriv->ndisencryptstatus = Ndis802_11Encryption1Enabled; + psecuritypriv->dot11PrivacyAlgrthm = _WEP40_; + psecuritypriv->dot118021XGrpPrivacy = _WEP40_; + + if (wep_key_len == 13) { + psecuritypriv->dot11PrivacyAlgrthm = _WEP104_; + psecuritypriv->dot118021XGrpPrivacy = _WEP104_; + } + + psecuritypriv->dot11PrivacyKeyIndex = wep_key_idx; + } + + _rtw_memcpy(&(psecuritypriv->dot11DefKey[wep_key_idx].skey[0]), param->u.crypt.key, wep_key_len); + + psecuritypriv->dot11DefKeylen[wep_key_idx] = wep_key_len; + + psecuritypriv->key_mask |= BIT(wep_key_idx); + + padapter->mlmeextpriv.mlmext_info.key_index = wep_key_idx; + goto exit; + } + + if (padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) { /* 802_1x */ + struct sta_info *psta, *pbcmc_sta; + struct sta_priv *pstapriv = &padapter->stapriv; + + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_MP_STATE) == _TRUE) { /* sta mode */ + psta = rtw_get_stainfo(pstapriv, get_bssid(pmlmepriv)); + if (psta == NULL) { + /* DEBUG_ERR( ("Set wpa_set_encryption: Obtain Sta_info fail\n")); */ + } else { + /* Jeff: don't disable ieee8021x_blocked while clearing key */ + if (strcmp(param->u.crypt.alg, "none") != 0) + psta->ieee8021x_blocked = _FALSE; + + if ((padapter->securitypriv.ndisencryptstatus == Ndis802_11Encryption2Enabled) || + (padapter->securitypriv.ndisencryptstatus == Ndis802_11Encryption3Enabled)) + psta->dot118021XPrivacy = padapter->securitypriv.dot11PrivacyAlgrthm; + + if (param->u.crypt.set_tx == 1) { /* pairwise key */ + RTW_INFO(FUNC_ADPT_FMT" set %s PTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.alg, param->u.crypt.idx, param->u.crypt.key_len); + _rtw_memcpy(psta->dot118021x_UncstKey.skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + if (strcmp(param->u.crypt.alg, "TKIP") == 0) { /* set mic key */ + _rtw_memcpy(psta->dot11tkiptxmickey.skey, &(param->u.crypt.key[16]), 8); + _rtw_memcpy(psta->dot11tkiprxmickey.skey, &(param->u.crypt.key[24]), 8); + padapter->securitypriv.busetkipkey = _FALSE; + } + psta->dot11txpn.val = RTW_GET_LE64(param->u.crypt.seq); + psta->dot11rxpn.val = RTW_GET_LE64(param->u.crypt.seq); + psta->bpairwise_key_installed = _TRUE; + rtw_setstakey_cmd(padapter, psta, UNICAST_KEY, _TRUE); + + } else { /* group key */ + if (strcmp(param->u.crypt.alg, "TKIP") == 0 || strcmp(param->u.crypt.alg, "CCMP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set %s GTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.alg, param->u.crypt.idx, param->u.crypt.key_len); + _rtw_memcpy(padapter->securitypriv.dot118021XGrpKey[param->u.crypt.idx].skey, param->u.crypt.key, + (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + /* only TKIP group key need to install this */ + if (param->u.crypt.key_len > 16) { + _rtw_memcpy(padapter->securitypriv.dot118021XGrptxmickey[param->u.crypt.idx].skey, &(param->u.crypt.key[16]), 8); + _rtw_memcpy(padapter->securitypriv.dot118021XGrprxmickey[param->u.crypt.idx].skey, &(param->u.crypt.key[24]), 8); + } + padapter->securitypriv.binstallGrpkey = _TRUE; + if (param->u.crypt.idx < 4) + _rtw_memcpy(padapter->securitypriv.iv_seq[param->u.crypt.idx], param->u.crypt.seq, 8); + padapter->securitypriv.dot118021XGrpKeyid = param->u.crypt.idx; + rtw_set_key(padapter, &padapter->securitypriv, param->u.crypt.idx, 1, _TRUE); + + #ifdef CONFIG_IEEE80211W + } else if (strcmp(param->u.crypt.alg, "BIP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set IGTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + _rtw_memcpy(padapter->securitypriv.dot11wBIPKey[param->u.crypt.idx].skey, param->u.crypt.key, + (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + psecuritypriv->dot11wBIPKeyid = param->u.crypt.idx; + psecuritypriv->dot11wBIPrxpn.val = RTW_GET_LE64(param->u.crypt.seq); + psecuritypriv->binstallBIPkey = _TRUE; + #endif /* CONFIG_IEEE80211W */ + + } + +#ifdef CONFIG_P2P + if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_PROVISIONING_ING)) + rtw_p2p_set_state(pwdinfo, P2P_STATE_PROVISIONING_DONE); +#endif /* CONFIG_P2P */ + + /* WPA/WPA2 key-handshake has completed */ + clr_fwstate(pmlmepriv, WIFI_UNDER_KEY_HANDSHAKE); + } + } + + pbcmc_sta = rtw_get_bcmc_stainfo(padapter); + if (pbcmc_sta == NULL) { + /* DEBUG_ERR( ("Set OID_802_11_ADD_KEY: bcmc stainfo is null\n")); */ + } else { + /* Jeff: don't disable ieee8021x_blocked while clearing key */ + if (strcmp(param->u.crypt.alg, "none") != 0) + pbcmc_sta->ieee8021x_blocked = _FALSE; + + if ((padapter->securitypriv.ndisencryptstatus == Ndis802_11Encryption2Enabled) || + (padapter->securitypriv.ndisencryptstatus == Ndis802_11Encryption3Enabled)) + pbcmc_sta->dot118021XPrivacy = padapter->securitypriv.dot11PrivacyAlgrthm; + } + } else if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) { /* adhoc mode */ + } + } + +#ifdef CONFIG_WAPI_SUPPORT + if (strcmp(param->u.crypt.alg, "SMS4") == 0) + rtw_wapi_set_set_encryption(padapter, param); +#endif + +exit: + + + return ret; +} + +static int rtw_set_wpa_ie(_adapter *padapter, char *pie, unsigned short ielen) +{ + u8 *buf = NULL, *pos = NULL; + int group_cipher = 0, pairwise_cipher = 0; + u8 mfp_opt = MFP_NO; + int ret = 0; + u8 null_addr[] = {0, 0, 0, 0, 0, 0}; +#ifdef CONFIG_P2P + struct wifidirect_info *pwdinfo = &padapter->wdinfo; +#endif /* CONFIG_P2P */ + + if ((ielen > MAX_WPA_IE_LEN) || (pie == NULL)) { + _clr_fwstate_(&padapter->mlmepriv, WIFI_UNDER_WPS); + if (pie == NULL) + return ret; + else + return -EINVAL; + } + + if (ielen) { + buf = rtw_zmalloc(ielen); + if (buf == NULL) { + ret = -ENOMEM; + goto exit; + } + + _rtw_memcpy(buf, pie , ielen); + + /* dump */ + { + int i; + RTW_INFO("\n wpa_ie(length:%d):\n", ielen); + for (i = 0; i < ielen; i = i + 8) + RTW_INFO("0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", buf[i], buf[i + 1], buf[i + 2], buf[i + 3], buf[i + 4], buf[i + 5], buf[i + 6], buf[i + 7]); + } + + pos = buf; + if (ielen < RSN_HEADER_LEN) { + ret = -1; + goto exit; + } + + if (rtw_parse_wpa_ie(buf, ielen, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) { + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_8021X; + padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeWPAPSK; + _rtw_memcpy(padapter->securitypriv.supplicant_ie, &buf[0], ielen); + } + + if (rtw_parse_wpa2_ie(buf, ielen, &group_cipher, &pairwise_cipher, NULL, NULL, &mfp_opt, NULL) == _SUCCESS) { + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_8021X; + padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeWPA2PSK; + _rtw_memcpy(padapter->securitypriv.supplicant_ie, &buf[0], ielen); + } + + if (group_cipher == 0) + group_cipher = WPA_CIPHER_NONE; + if (pairwise_cipher == 0) + pairwise_cipher = WPA_CIPHER_NONE; + + switch (group_cipher) { + case WPA_CIPHER_NONE: + padapter->securitypriv.dot118021XGrpPrivacy = _NO_PRIVACY_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11EncryptionDisabled; + break; + case WPA_CIPHER_WEP40: + padapter->securitypriv.dot118021XGrpPrivacy = _WEP40_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled; + break; + case WPA_CIPHER_TKIP: + padapter->securitypriv.dot118021XGrpPrivacy = _TKIP_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption2Enabled; + break; + case WPA_CIPHER_CCMP: + padapter->securitypriv.dot118021XGrpPrivacy = _AES_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled; + break; + case WPA_CIPHER_WEP104: + padapter->securitypriv.dot118021XGrpPrivacy = _WEP104_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled; + break; + } + + switch (pairwise_cipher) { + case WPA_CIPHER_NONE: + padapter->securitypriv.dot11PrivacyAlgrthm = _NO_PRIVACY_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11EncryptionDisabled; + break; + case WPA_CIPHER_WEP40: + padapter->securitypriv.dot11PrivacyAlgrthm = _WEP40_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled; + break; + case WPA_CIPHER_TKIP: + padapter->securitypriv.dot11PrivacyAlgrthm = _TKIP_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption2Enabled; + break; + case WPA_CIPHER_CCMP: + padapter->securitypriv.dot11PrivacyAlgrthm = _AES_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled; + break; + case WPA_CIPHER_WEP104: + padapter->securitypriv.dot11PrivacyAlgrthm = _WEP104_; + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled; + break; + } + + if (mfp_opt == MFP_INVALID) { + RTW_INFO(FUNC_ADPT_FMT" invalid MFP setting\n", FUNC_ADPT_ARG(padapter)); + ret = -EINVAL; + goto exit; + } + padapter->securitypriv.mfp_opt = mfp_opt; + + _clr_fwstate_(&padapter->mlmepriv, WIFI_UNDER_WPS); + {/* set wps_ie */ + u16 cnt = 0; + u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04}; + + while (cnt < ielen) { + eid = buf[cnt]; + + if ((eid == _VENDOR_SPECIFIC_IE_) && (_rtw_memcmp(&buf[cnt + 2], wps_oui, 4) == _TRUE)) { + RTW_INFO("SET WPS_IE\n"); + + padapter->securitypriv.wps_ie_len = ((buf[cnt + 1] + 2) < MAX_WPS_IE_LEN) ? (buf[cnt + 1] + 2) : MAX_WPS_IE_LEN; + + _rtw_memcpy(padapter->securitypriv.wps_ie, &buf[cnt], padapter->securitypriv.wps_ie_len); + + set_fwstate(&padapter->mlmepriv, WIFI_UNDER_WPS); + +#ifdef CONFIG_P2P + if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_GONEGO_OK)) + rtw_p2p_set_state(pwdinfo, P2P_STATE_PROVISIONING_ING); +#endif /* CONFIG_P2P */ + cnt += buf[cnt + 1] + 2; + + break; + } else { + cnt += buf[cnt + 1] + 2; /* goto next */ + } + } + } + + #ifdef CONFIG_RTW_MULTI_AP + padapter->multi_ap = rtw_get_multi_ap_ie_ext(buf, ielen) & MULTI_AP_BACKHAUL_STA; + if (padapter->multi_ap) + adapter_set_use_wds(padapter, 1); + #endif + } + + /* TKIP and AES disallow multicast packets until installing group key */ + if (padapter->securitypriv.dot11PrivacyAlgrthm == _TKIP_ + || padapter->securitypriv.dot11PrivacyAlgrthm == _TKIP_WTMIC_ + || padapter->securitypriv.dot11PrivacyAlgrthm == _AES_) + /* WPS open need to enable multicast + * || check_fwstate(&padapter->mlmepriv, WIFI_UNDER_WPS) == _TRUE) */ + rtw_hal_set_hwreg(padapter, HW_VAR_OFF_RCR_AM, null_addr); + + +exit: + + if (buf) + rtw_mfree(buf, ielen); + + return ret; +} + +static int rtw_wx_get_name(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + u32 ht_ielen = 0; + char *p; + u8 ht_cap = _FALSE, vht_cap = _FALSE; + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + WLAN_BSSID_EX *pcur_bss = &pmlmepriv->cur_network.network; + NDIS_802_11_RATES_EX *prates = NULL; + + + + if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE | WIFI_ADHOC_MASTER_STATE) == _TRUE) { + /* parsing HT_CAP_IE */ + if( is_supported_ht(padapter->registrypriv.wireless_mode)&&(padapter->registrypriv.ht_enable)) { + p = rtw_get_ie(&pcur_bss->IEs[12], _HT_CAPABILITY_IE_, &ht_ielen, pcur_bss->IELength - 12); + if (p && ht_ielen > 0 ) + ht_cap = _TRUE; + } +#ifdef CONFIG_80211AC_VHT + if ((padapter->registrypriv.wireless_mode & WIRELESS_11AC) && + (pmlmepriv->vhtpriv.vht_option == _TRUE)) + vht_cap = _TRUE; +#endif + + prates = &pcur_bss->SupportedRates; + if (rtw_is_cckratesonly_included((u8 *)prates) == _TRUE) { + if (ht_cap == _TRUE) + snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11bn"); + else + snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11b"); + } else if ((rtw_is_cckrates_included((u8 *)prates)) == _TRUE) { + if (ht_cap == _TRUE) + snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11bgn"); + else { + if(padapter->registrypriv.wireless_mode & WIRELESS_11G) + snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11bg"); + else + snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11b"); + } + } else { + if (pcur_bss->Configuration.DSConfig > 14) { +#ifdef CONFIG_80211AC_VHT + if (vht_cap == _TRUE) + snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11AC"); + else +#endif + { + if (ht_cap == _TRUE) + snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11an"); + else + snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11a"); + } + } else { + if (ht_cap == _TRUE) + snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11gn"); + else + snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11g"); + } + } + } else { + /* prates = &padapter->registrypriv.dev_network.SupportedRates; */ + /* snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11g"); */ + snprintf(wrqu->name, IFNAMSIZ, "unassociated"); + } + + + return 0; +} + +static int rtw_wx_set_freq(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + int exp = 1, freq = 0, div = 0; + + rtw_ps_deny(padapter, PS_DENY_IOCTL); + if (rtw_pwr_wakeup(padapter) == _FALSE) + goto exit; + if (wrqu->freq.m <= 1000) { + if (wrqu->freq.flags == IW_FREQ_AUTO) { + if (rtw_chset_search_ch(adapter_to_chset(padapter), wrqu->freq.m) > 0) { + padapter->mlmeextpriv.cur_channel = wrqu->freq.m; + RTW_INFO("%s: channel is auto, set to channel %d\n", __func__, wrqu->freq.m); + } else { + padapter->mlmeextpriv.cur_channel = 1; + RTW_INFO("%s: channel is auto, Channel Plan don't match just set to channel 1\n", __func__); + } + } else { + padapter->mlmeextpriv.cur_channel = wrqu->freq.m; + RTW_INFO("%s: set to channel %d\n", __func__, padapter->mlmeextpriv.cur_channel); + } + } else { + while (wrqu->freq.e) { + exp *= 10; + wrqu->freq.e--; + } + + freq = wrqu->freq.m; + + while (!(freq % 10)) { + freq /= 10; + exp *= 10; + } + + /* freq unit is MHz here */ + div = 1000000 / exp; + + if (div) + freq /= div; + else { + div = exp / 1000000; + freq *= div; + } + + /* If freq is invalid, rtw_freq2ch() will return channel 1 */ + padapter->mlmeextpriv.cur_channel = rtw_freq2ch(freq); + RTW_INFO("%s: set to channel %d\n", __func__, padapter->mlmeextpriv.cur_channel); + } + set_channel_bwmode(padapter, padapter->mlmeextpriv.cur_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, CHANNEL_WIDTH_20); +exit: + rtw_ps_deny_cancel(padapter, PS_DENY_IOCTL); + + return 0; +} + +static int rtw_wx_get_freq(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + WLAN_BSSID_EX *pcur_bss = &pmlmepriv->cur_network.network; + + if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _TRUE && check_fwstate(pmlmepriv, WIFI_MONITOR_STATE) != _TRUE) { + + wrqu->freq.m = rtw_ch2freq(pcur_bss->Configuration.DSConfig) * 100000; + wrqu->freq.e = 1; + wrqu->freq.i = pcur_bss->Configuration.DSConfig; + + } else { + wrqu->freq.m = rtw_ch2freq(padapter->mlmeextpriv.cur_channel) * 100000; + wrqu->freq.e = 1; + wrqu->freq.i = padapter->mlmeextpriv.cur_channel; + } + + return 0; +} + +static int rtw_wx_set_mode(struct net_device *dev, struct iw_request_info *a, + union iwreq_data *wrqu, char *b) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + NDIS_802_11_NETWORK_INFRASTRUCTURE networkType ; + int ret = 0; + + + if (_FAIL == rtw_pwr_wakeup(padapter)) { + ret = -EPERM; + goto exit; + } + + if (!rtw_is_hw_init_completed(padapter)) { + ret = -EPERM; + goto exit; + } + + /* initial default type */ + dev->type = ARPHRD_ETHER; + + if (wrqu->mode != IW_MODE_MONITOR) { + rtw_ps_deny_cancel(padapter, PS_DENY_MONITOR_MODE); + } + + switch (wrqu->mode) { +#ifdef CONFIG_WIFI_MONITOR + case IW_MODE_MONITOR: + networkType = Ndis802_11Monitor; + + rtw_ps_deny(padapter, PS_DENY_MONITOR_MODE); + LeaveAllPowerSaveMode(padapter); + +#if 0 + dev->type = ARPHRD_IEEE80211; /* IEEE 802.11 : 801 */ +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)) + dev->type = ARPHRD_IEEE80211_RADIOTAP; /* IEEE 802.11 + radiotap header : 803 */ + RTW_INFO("set_mode = IW_MODE_MONITOR\n"); +#else + RTW_INFO("kernel version < 2.6.24 not support IW_MODE_MONITOR\n"); +#endif + break; +#endif /* CONFIG_WIFI_MONITOR */ + case IW_MODE_AUTO: + networkType = Ndis802_11AutoUnknown; + RTW_INFO("set_mode = IW_MODE_AUTO\n"); + break; + case IW_MODE_ADHOC: + networkType = Ndis802_11IBSS; + RTW_INFO("set_mode = IW_MODE_ADHOC\n"); + break; + case IW_MODE_MASTER: + networkType = Ndis802_11APMode; + RTW_INFO("set_mode = IW_MODE_MASTER\n"); + break; + case IW_MODE_INFRA: + networkType = Ndis802_11Infrastructure; + RTW_INFO("set_mode = IW_MODE_INFRA\n"); + break; + + default: + ret = -EINVAL;; + goto exit; + } + + if (rtw_set_802_11_infrastructure_mode(padapter, networkType, 0) == _FALSE) { + + ret = -EPERM; + goto exit; + + } + + rtw_setopmode_cmd(padapter, networkType, RTW_CMDF_WAIT_ACK); + + if (check_fwstate(pmlmepriv, WIFI_MONITOR_STATE) == _TRUE) + rtw_indicate_connect(padapter); + +exit: + + + return ret; + +} + +static int rtw_wx_get_mode(struct net_device *dev, struct iw_request_info *a, + union iwreq_data *wrqu, char *b) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + + + + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == _TRUE) + wrqu->mode = IW_MODE_INFRA; + else if ((check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == _TRUE) || + (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == _TRUE)) + + wrqu->mode = IW_MODE_ADHOC; + else if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == _TRUE) + wrqu->mode = IW_MODE_MASTER; + else if (check_fwstate(pmlmepriv, WIFI_MONITOR_STATE) == _TRUE) + wrqu->mode = IW_MODE_MONITOR; + else + wrqu->mode = IW_MODE_AUTO; + + + return 0; + +} + + +static int rtw_wx_set_pmkid(struct net_device *dev, + struct iw_request_info *a, + union iwreq_data *wrqu, char *extra) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + u8 j, blInserted = _FALSE; + int intReturn = _FALSE; + struct security_priv *psecuritypriv = &padapter->securitypriv; + struct iw_pmksa *pPMK = (struct iw_pmksa *) extra; + u8 strZeroMacAddress[ETH_ALEN] = { 0x00 }; + u8 strIssueBssid[ETH_ALEN] = { 0x00 }; + +#if 0 + struct iw_pmksa { + __u32 cmd; + struct sockaddr bssid; + __u8 pmkid[IW_PMKID_LEN]; /* IW_PMKID_LEN=16 */ + } + There are the BSSID information in the bssid.sa_data array. + If cmd is IW_PMKSA_FLUSH, it means the wpa_suppplicant wants to clear all the PMKID information. + If cmd is IW_PMKSA_ADD, it means the wpa_supplicant wants to add a PMKID / BSSID to driver. + If cmd is IW_PMKSA_REMOVE, it means the wpa_supplicant wants to remove a PMKID / BSSID from driver. +#endif + + _rtw_memcpy(strIssueBssid, pPMK->bssid.sa_data, ETH_ALEN); + if (pPMK->cmd == IW_PMKSA_ADD) { + RTW_INFO("[rtw_wx_set_pmkid] IW_PMKSA_ADD!\n"); + if (_rtw_memcmp(strIssueBssid, strZeroMacAddress, ETH_ALEN) == _TRUE) + return intReturn ; + else + intReturn = _TRUE; + blInserted = _FALSE; + + /* overwrite PMKID */ + for (j = 0 ; j < NUM_PMKID_CACHE; j++) { + if (_rtw_memcmp(psecuritypriv->PMKIDList[j].Bssid, strIssueBssid, ETH_ALEN) == _TRUE) { + /* BSSID is matched, the same AP => rewrite with new PMKID. */ + + RTW_INFO("[rtw_wx_set_pmkid] BSSID exists in the PMKList.\n"); + + _rtw_memcpy(psecuritypriv->PMKIDList[j].PMKID, pPMK->pmkid, IW_PMKID_LEN); + psecuritypriv->PMKIDList[j].bUsed = _TRUE; + blInserted = _TRUE; + break; + } + } + + if (!blInserted) { + /* Find a new entry */ + RTW_INFO("[rtw_wx_set_pmkid] Use the new entry index = %d for this PMKID.\n", + psecuritypriv->PMKIDIndex); + + _rtw_memcpy(psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].Bssid, strIssueBssid, ETH_ALEN); + _rtw_memcpy(psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].PMKID, pPMK->pmkid, IW_PMKID_LEN); + + psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].bUsed = _TRUE; + psecuritypriv->PMKIDIndex++ ; + if (psecuritypriv->PMKIDIndex == 16) + psecuritypriv->PMKIDIndex = 0; + } + } else if (pPMK->cmd == IW_PMKSA_REMOVE) { + RTW_INFO("[rtw_wx_set_pmkid] IW_PMKSA_REMOVE!\n"); + intReturn = _TRUE; + for (j = 0 ; j < NUM_PMKID_CACHE; j++) { + if (_rtw_memcmp(psecuritypriv->PMKIDList[j].Bssid, strIssueBssid, ETH_ALEN) == _TRUE) { + /* BSSID is matched, the same AP => Remove this PMKID information and reset it. */ + _rtw_memset(psecuritypriv->PMKIDList[j].Bssid, 0x00, ETH_ALEN); + psecuritypriv->PMKIDList[j].bUsed = _FALSE; + break; + } + } + } else if (pPMK->cmd == IW_PMKSA_FLUSH) { + RTW_INFO("[rtw_wx_set_pmkid] IW_PMKSA_FLUSH!\n"); + _rtw_memset(&psecuritypriv->PMKIDList[0], 0x00, sizeof(RT_PMKID_LIST) * NUM_PMKID_CACHE); + psecuritypriv->PMKIDIndex = 0; + intReturn = _TRUE; + } + return intReturn ; +} + +static int rtw_wx_get_sens(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ +#ifdef CONFIG_PLATFORM_ROCKCHIPS + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + + /* + * 20110311 Commented by Jeff + * For rockchip platform's wpa_driver_wext_get_rssi + */ + if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _TRUE) { + /* wrqu->sens.value=-padapter->recvpriv.signal_strength; */ + wrqu->sens.value = -padapter->recvpriv.rssi; + /* RTW_INFO("%s: %d\n", __FUNCTION__, wrqu->sens.value); */ + wrqu->sens.fixed = 0; /* no auto select */ + } else +#endif + { + wrqu->sens.value = 0; + wrqu->sens.fixed = 0; /* no auto select */ + wrqu->sens.disabled = 1; + } + return 0; +} + +static int rtw_wx_get_range(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + struct iw_range *range = (struct iw_range *)extra; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(padapter); + u16 val; + int i; + + + + wrqu->data.length = sizeof(*range); + _rtw_memset(range, 0, sizeof(*range)); + + /* Let's try to keep this struct in the same order as in + * linux/include/wireless.h + */ + + /* TODO: See what values we can set, and remove the ones we can't + * set, or fill them with some default data. + */ + + /* ~5 Mb/s real (802.11b) */ + range->throughput = 5 * 1000 * 1000; + + /* TODO: Not used in 802.11b? + * range->min_nwid; Minimal NWID we are able to set */ + /* TODO: Not used in 802.11b? + * range->max_nwid; Maximal NWID we are able to set */ + + /* Old Frequency (backward compat - moved lower ) */ + /* range->old_num_channels; + * range->old_num_frequency; + * range->old_freq[6]; Filler to keep "version" at the same offset */ + + /* signal level threshold range */ + + /* Quality of link & SNR stuff */ + /* Quality range (link, level, noise) + * If the quality is absolute, it will be in the range [0 ; max_qual], + * if the quality is dBm, it will be in the range [max_qual ; 0]. + * Don't forget that we use 8 bit arithmetics... + * + * If percentage range is 0~100 + * Signal strength dbm range logical is -100 ~ 0 + * but usually value is -90 ~ -20 + */ + range->max_qual.qual = 100; +#ifdef CONFIG_SIGNAL_DISPLAY_DBM + range->max_qual.level = (u8)-100; + range->max_qual.noise = (u8)-100; + range->max_qual.updated = IW_QUAL_ALL_UPDATED; /* Updated all three */ + range->max_qual.updated |= IW_QUAL_DBM; +#else /* !CONFIG_SIGNAL_DISPLAY_DBM */ + /* percent values between 0 and 100. */ + range->max_qual.level = 100; + range->max_qual.noise = 100; + range->max_qual.updated = IW_QUAL_ALL_UPDATED; /* Updated all three */ +#endif /* !CONFIG_SIGNAL_DISPLAY_DBM */ + + /* This should contain the average/typical values of the quality + * indicator. This should be the threshold between a "good" and + * a "bad" link (example : monitor going from green to orange). + * Currently, user space apps like quality monitors don't have any + * way to calibrate the measurement. With this, they can split + * the range between 0 and max_qual in different quality level + * (using a geometric subdivision centered on the average). + * I expect that people doing the user space apps will feedback + * us on which value we need to put in each driver... */ + range->avg_qual.qual = 92; /* > 8% missed beacons is 'bad' */ +#ifdef CONFIG_SIGNAL_DISPLAY_DBM + /* TODO: Find real 'good' to 'bad' threshold value for RSSI */ + range->avg_qual.level = (u8)-70; + range->avg_qual.noise = 0; + range->avg_qual.updated = IW_QUAL_ALL_UPDATED; /* Updated all three */ + range->avg_qual.updated |= IW_QUAL_DBM; +#else /* !CONFIG_SIGNAL_DISPLAY_DBM */ + /* TODO: Find real 'good' to 'bad' threshol value for RSSI */ + range->avg_qual.level = 30; + range->avg_qual.noise = 100; + range->avg_qual.updated = IW_QUAL_ALL_UPDATED; /* Updated all three */ +#endif /* !CONFIG_SIGNAL_DISPLAY_DBM */ + + range->num_bitrates = RATE_COUNT; + + for (i = 0; i < RATE_COUNT && i < IW_MAX_BITRATES; i++) + range->bitrate[i] = rtw_rates[i]; + + range->min_frag = MIN_FRAG_THRESHOLD; + range->max_frag = MAX_FRAG_THRESHOLD; + + range->pm_capa = 0; + + range->we_version_compiled = WIRELESS_EXT; + range->we_version_source = 16; + + /* range->retry_capa; What retry options are supported + * range->retry_flags; How to decode max/min retry limit + * range->r_time_flags; How to decode max/min retry life + * range->min_retry; Minimal number of retries + * range->max_retry; Maximal number of retries + * range->min_r_time; Minimal retry lifetime + * range->max_r_time; Maximal retry lifetime */ + + for (i = 0, val = 0; i < rfctl->max_chan_nums; i++) { + + /* Include only legal frequencies for some countries */ + if (rfctl->channel_set[i].ChannelNum != 0) { + range->freq[val].i = rfctl->channel_set[i].ChannelNum; + range->freq[val].m = rtw_ch2freq(rfctl->channel_set[i].ChannelNum) * 100000; + range->freq[val].e = 1; + val++; + } + + if (val == IW_MAX_FREQUENCIES) + break; + } + + range->num_channels = val; + range->num_frequency = val; + + /* Commented by Albert 2009/10/13 + * The following code will proivde the security capability to network manager. + * If the driver doesn't provide this capability to network manager, + * the WPA/WPA2 routers can't be choosen in the network manager. */ + + /* + #define IW_SCAN_CAPA_NONE 0x00 + #define IW_SCAN_CAPA_ESSID 0x01 + #define IW_SCAN_CAPA_BSSID 0x02 + #define IW_SCAN_CAPA_CHANNEL 0x04 + #define IW_SCAN_CAPA_MODE 0x08 + #define IW_SCAN_CAPA_RATE 0x10 + #define IW_SCAN_CAPA_TYPE 0x20 + #define IW_SCAN_CAPA_TIME 0x40 + */ + +#if WIRELESS_EXT > 17 + range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 | + IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP; +#endif + +#ifdef IW_SCAN_CAPA_ESSID /* WIRELESS_EXT > 21 */ + range->scan_capa = IW_SCAN_CAPA_ESSID | IW_SCAN_CAPA_TYPE | IW_SCAN_CAPA_BSSID | + IW_SCAN_CAPA_CHANNEL | IW_SCAN_CAPA_MODE | IW_SCAN_CAPA_RATE; +#endif + + + + return 0; + +} + +/* set bssid flow + * s1. rtw_set_802_11_infrastructure_mode() + * s2. rtw_set_802_11_authentication_mode() + * s3. set_802_11_encryption_mode() + * s4. rtw_set_802_11_bssid() */ +static int rtw_wx_set_wap(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *awrq, + char *extra) +{ + _irqL irqL; + uint ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct sockaddr *temp = (struct sockaddr *)awrq; + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + _list *phead; + u8 *dst_bssid, *src_bssid; + _queue *queue = &(pmlmepriv->scanned_queue); + struct wlan_network *pnetwork = NULL; + NDIS_802_11_AUTHENTICATION_MODE authmode; + + /* + #ifdef CONFIG_CONCURRENT_MODE + if(padapter->adapter_type > PRIMARY_IFACE) + { + ret = -EINVAL; + goto exit; + } + #endif + */ + +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_buddy_check_fwstate(padapter, WIFI_UNDER_SURVEY | WIFI_UNDER_LINKING) == _TRUE) { + RTW_INFO("set bssid, but buddy_intf is under scanning or linking\n"); + + ret = -EINVAL; + + goto exit; + } +#endif + + rtw_ps_deny(padapter, PS_DENY_JOIN); + if (_FAIL == rtw_pwr_wakeup(padapter)) { + ret = -1; + goto cancel_ps_deny; + } + + if (!padapter->bup) { + ret = -1; + goto cancel_ps_deny; + } + + + if (temp->sa_family != ARPHRD_ETHER) { + ret = -EINVAL; + goto cancel_ps_deny; + } + + authmode = padapter->securitypriv.ndisauthtype; + _enter_critical_bh(&queue->lock, &irqL); + phead = get_list_head(queue); + pmlmepriv->pscanned = get_next(phead); + + while (1) { + + if ((rtw_end_of_queue_search(phead, pmlmepriv->pscanned)) == _TRUE) { +#if 0 + ret = -EINVAL; + goto cancel_ps_deny; + + if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == _TRUE) { + rtw_set_802_11_bssid(padapter, temp->sa_data); + goto cancel_ps_deny; + } else { + ret = -EINVAL; + goto cancel_ps_deny; + } +#endif + + break; + } + + pnetwork = LIST_CONTAINOR(pmlmepriv->pscanned, struct wlan_network, list); + + pmlmepriv->pscanned = get_next(pmlmepriv->pscanned); + + dst_bssid = pnetwork->network.MacAddress; + + src_bssid = temp->sa_data; + + if ((_rtw_memcmp(dst_bssid, src_bssid, ETH_ALEN)) == _TRUE) { + if (!rtw_set_802_11_infrastructure_mode(padapter, pnetwork->network.InfrastructureMode, 0)) { + ret = -1; + _exit_critical_bh(&queue->lock, &irqL); + goto cancel_ps_deny; + } + + break; + } + + } + _exit_critical_bh(&queue->lock, &irqL); + + rtw_set_802_11_authentication_mode(padapter, authmode); + /* set_802_11_encryption_mode(padapter, padapter->securitypriv.ndisencryptstatus); */ + if (rtw_set_802_11_bssid(padapter, temp->sa_data) == _FALSE) { + ret = -1; + goto cancel_ps_deny; + } + +cancel_ps_deny: + rtw_ps_deny_cancel(padapter, PS_DENY_JOIN); + +#ifdef CONFIG_CONCURRENT_MODE +exit: +#endif + return ret; +} + +static int rtw_wx_get_wap(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + WLAN_BSSID_EX *pcur_bss = &pmlmepriv->cur_network.network; + + wrqu->ap_addr.sa_family = ARPHRD_ETHER; + + _rtw_memset(wrqu->ap_addr.sa_data, 0, ETH_ALEN); + + + + if (((check_fwstate(pmlmepriv, WIFI_ASOC_STATE)) == _TRUE) || + ((check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) == _TRUE) || + ((check_fwstate(pmlmepriv, WIFI_AP_STATE)) == _TRUE)) + + _rtw_memcpy(wrqu->ap_addr.sa_data, pcur_bss->MacAddress, ETH_ALEN); + else + _rtw_memset(wrqu->ap_addr.sa_data, 0, ETH_ALEN); + + + return 0; + +} + +static int rtw_wx_set_mlme(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ +#if 0 + /* SIOCSIWMLME data */ + struct iw_mlme { + __u16 cmd; /* IW_MLME_* */ + __u16 reason_code; + struct sockaddr addr; + }; +#endif + + int ret = 0; + u16 reason; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct iw_mlme *mlme = (struct iw_mlme *) extra; + + + if (mlme == NULL) + return -1; + + RTW_INFO("%s\n", __FUNCTION__); + + reason = cpu_to_le16(mlme->reason_code); + + + RTW_INFO("%s, cmd=%d, reason=%d\n", __FUNCTION__, mlme->cmd, reason); + + + switch (mlme->cmd) { + case IW_MLME_DEAUTH: + if (!rtw_set_802_11_disassociate(padapter)) + ret = -1; + break; + + case IW_MLME_DISASSOC: + if (!rtw_set_802_11_disassociate(padapter)) + ret = -1; + + break; + + default: + return -EOPNOTSUPP; + } +#ifdef CONFIG_RTW_REPEATER_SON + rtw_rson_do_disconnect(padapter); +#endif + return ret; +} + +static int rtw_wx_set_scan(struct net_device *dev, struct iw_request_info *a, + union iwreq_data *wrqu, char *extra) +{ + u8 _status = _FALSE; + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + /*struct mlme_priv *pmlmepriv = &padapter->mlmepriv;*/ + struct sitesurvey_parm parm; + u8 ssc_chk; +#ifdef CONFIG_P2P + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); +#endif /* CONFIG_P2P */ + +#ifdef DBG_IOCTL + RTW_INFO("DBG_IOCTL %s:%d\n", __FUNCTION__, __LINE__); +#endif + +#if 1 + ssc_chk = rtw_sitesurvey_condition_check(padapter, _FALSE); + + #ifdef CONFIG_DOSCAN_IN_BUSYTRAFFIC + if ((ssc_chk != SS_ALLOW) && (ssc_chk != SS_DENY_BUSY_TRAFFIC)) + #else + /* When Busy Traffic, driver do not site survey. So driver return success. */ + /* wpa_supplicant will not issue SIOCSIWSCAN cmd again after scan timeout. */ + /* modify by thomas 2011-02-22. */ + if (ssc_chk != SS_ALLOW) + #endif + { + if (ssc_chk == SS_DENY_MP_MODE) + ret = -EPERM; + #ifdef DBG_LA_MODE + else if (ssc_chk == SS_DENY_LA_MODE) + ret = -EPERM; + #endif + else + indicate_wx_scan_complete_event(padapter); + + goto exit; + } else + RTW_INFO(FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(padapter)); + + rtw_ps_deny(padapter, PS_DENY_SCAN); + if (_FAIL == rtw_pwr_wakeup(padapter)) { + ret = -1; + goto cancel_ps_deny; + } + if (!rtw_is_adapter_up(padapter)) { + ret = -1; + goto cancel_ps_deny; + } +#else + +#ifdef CONFIG_MP_INCLUDED + if (rtw_mp_mode_check(padapter)) { + RTW_INFO("MP mode block Scan request\n"); + ret = -EPERM; + goto exit; + } +#endif + if (rtw_is_scan_deny(padapter)) { + indicate_wx_scan_complete_event(padapter); + goto exit; + } + + rtw_ps_deny(padapter, PS_DENY_SCAN); + if (_FAIL == rtw_pwr_wakeup(padapter)) { + ret = -1; + goto cancel_ps_deny; + } + + if (!rtw_is_adapter_up(padapter)) { + ret = -1; + goto cancel_ps_deny; + } + +#ifndef CONFIG_DOSCAN_IN_BUSYTRAFFIC + /* When Busy Traffic, driver do not site survey. So driver return success. */ + /* wpa_supplicant will not issue SIOCSIWSCAN cmd again after scan timeout. */ + /* modify by thomas 2011-02-22. */ + if (rtw_mi_busy_traffic_check(padapter)) { + indicate_wx_scan_complete_event(padapter); + goto cancel_ps_deny; + } +#endif +#ifdef CONFIG_RTW_REPEATER_SON + if (padapter->rtw_rson_scanstage == RSON_SCAN_PROCESS) { + RTW_INFO(FUNC_ADPT_FMT" blocking scan for under rson scanning process\n", FUNC_ADPT_ARG(padapter)); + indicate_wx_scan_complete_event(padapter); + goto cancel_ps_deny; + } +#endif + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) && check_fwstate(pmlmepriv, WIFI_UNDER_WPS)) { + RTW_INFO("AP mode process WPS\n"); + indicate_wx_scan_complete_event(padapter); + goto cancel_ps_deny; + } + + if (check_fwstate(pmlmepriv, WIFI_UNDER_SURVEY | WIFI_UNDER_LINKING) == _TRUE) { + indicate_wx_scan_complete_event(padapter); + goto cancel_ps_deny; + } + +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_buddy_check_fwstate(padapter, + WIFI_UNDER_SURVEY | WIFI_UNDER_LINKING | WIFI_UNDER_WPS)) { + + indicate_wx_scan_complete_event(padapter); + goto cancel_ps_deny; + } +#endif +#endif + +#ifdef CONFIG_P2P + if (pwdinfo->p2p_state != P2P_STATE_NONE) { + rtw_p2p_set_pre_state(pwdinfo, rtw_p2p_state(pwdinfo)); + rtw_p2p_set_state(pwdinfo, P2P_STATE_FIND_PHASE_SEARCH); + rtw_p2p_findphase_ex_set(pwdinfo, P2P_FINDPHASE_EX_FULL); + rtw_free_network_queue(padapter, _TRUE); + } +#endif /* CONFIG_P2P */ + +#if WIRELESS_EXT >= 17 + if (wrqu->data.length == sizeof(struct iw_scan_req)) { + struct iw_scan_req *req = (struct iw_scan_req *)extra; + + if (wrqu->data.flags & IW_SCAN_THIS_ESSID) { + int len = min((int)req->essid_len, IW_ESSID_MAX_SIZE); + + rtw_init_sitesurvey_parm(padapter, &parm); + _rtw_memcpy(&parm.ssid[0].Ssid, &req->essid, len); + parm.ssid[0].SsidLength = len; + parm.ssid_num = 1; + + RTW_INFO("IW_SCAN_THIS_ESSID, ssid=%s, len=%d\n", req->essid, req->essid_len); + + _status = rtw_set_802_11_bssid_list_scan(padapter, &parm); + + } else if (req->scan_type == IW_SCAN_TYPE_PASSIVE) + RTW_INFO("rtw_wx_set_scan, req->scan_type == IW_SCAN_TYPE_PASSIVE\n"); + + } else +#endif + + if (wrqu->data.length >= WEXT_CSCAN_HEADER_SIZE + && _rtw_memcmp(extra, WEXT_CSCAN_HEADER, WEXT_CSCAN_HEADER_SIZE) == _TRUE + ) { + int len = wrqu->data.length - WEXT_CSCAN_HEADER_SIZE; + char *pos = extra + WEXT_CSCAN_HEADER_SIZE; + char section; + char sec_len; + int ssid_index = 0; + + /* RTW_INFO("%s COMBO_SCAN header is recognized\n", __FUNCTION__); */ + rtw_init_sitesurvey_parm(padapter, &parm); + + while (len >= 1) { + section = *(pos++); + len -= 1; + + switch (section) { + case WEXT_CSCAN_SSID_SECTION: + /* RTW_INFO("WEXT_CSCAN_SSID_SECTION\n"); */ + if (len < 1) { + len = 0; + break; + } + + sec_len = *(pos++); + len -= 1; + + if (sec_len > 0 && sec_len <= len) { + + parm.ssid[ssid_index].SsidLength = sec_len; + _rtw_memcpy(&parm.ssid[ssid_index].Ssid, pos, sec_len); + + /* RTW_INFO("%s COMBO_SCAN with specific parm.ssid:%s, %d\n", __FUNCTION__ */ + /* , parm.ssid[ssid_index].Ssid, parm.ssid[ssid_index].SsidLength); */ + ssid_index++; + } + + pos += sec_len; + len -= sec_len; + break; + + + case WEXT_CSCAN_CHANNEL_SECTION: + /* RTW_INFO("WEXT_CSCAN_CHANNEL_SECTION\n"); */ + pos += 1; + len -= 1; + break; + case WEXT_CSCAN_ACTV_DWELL_SECTION: + /* RTW_INFO("WEXT_CSCAN_ACTV_DWELL_SECTION\n"); */ + pos += 2; + len -= 2; + break; + case WEXT_CSCAN_PASV_DWELL_SECTION: + /* RTW_INFO("WEXT_CSCAN_PASV_DWELL_SECTION\n"); */ + pos += 2; + len -= 2; + break; + case WEXT_CSCAN_HOME_DWELL_SECTION: + /* RTW_INFO("WEXT_CSCAN_HOME_DWELL_SECTION\n"); */ + pos += 2; + len -= 2; + break; + case WEXT_CSCAN_TYPE_SECTION: + /* RTW_INFO("WEXT_CSCAN_TYPE_SECTION\n"); */ + pos += 1; + len -= 1; + break; +#if 0 + case WEXT_CSCAN_NPROBE_SECTION: + RTW_INFO("WEXT_CSCAN_NPROBE_SECTION\n"); + break; +#endif + + default: + /* RTW_INFO("Unknown CSCAN section %c\n", section); */ + len = 0; /* stop parsing */ + } + /* RTW_INFO("len:%d\n", len); */ + + } + parm.ssid_num = ssid_index; + + /* jeff: it has still some scan paramater to parse, we only do this now... */ + _status = rtw_set_802_11_bssid_list_scan(padapter, &parm); + + } else + + _status = rtw_set_802_11_bssid_list_scan(padapter, NULL); + + if (_status == _FALSE) + ret = -1; + +cancel_ps_deny: + rtw_ps_deny_cancel(padapter, PS_DENY_SCAN); + +exit: +#ifdef DBG_IOCTL + RTW_INFO("DBG_IOCTL %s:%d return %d\n", __FUNCTION__, __LINE__, ret); +#endif + + return ret; +} + +static int rtw_wx_get_scan(struct net_device *dev, struct iw_request_info *a, + union iwreq_data *wrqu, char *extra) +{ + _irqL irqL; + _list *plist, *phead; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(padapter); + RT_CHANNEL_INFO *chset = rfctl->channel_set; + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + _queue *queue = &(pmlmepriv->scanned_queue); + struct wlan_network *pnetwork = NULL; + char *ev = extra; + char *stop = ev + wrqu->data.length; + u32 ret = 0; + u32 wait_for_surveydone; + sint wait_status; + u8 ch; + +#ifdef CONFIG_P2P + struct wifidirect_info *pwdinfo = &padapter->wdinfo; +#endif /* CONFIG_P2P */ + + +#ifdef DBG_IOCTL + RTW_INFO("DBG_IOCTL %s:%d\n", __FUNCTION__, __LINE__); +#endif + + if (adapter_to_pwrctl(padapter)->brfoffbyhw && rtw_is_drv_stopped(padapter)) { + ret = -EINVAL; + goto exit; + } + +#ifdef CONFIG_P2P + if (!rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) + wait_for_surveydone = 200; + else { + /* P2P is disabled */ + wait_for_surveydone = 100; + } +#else + { + wait_for_surveydone = 100; + } +#endif /* CONFIG_P2P */ + +#if 1 /* Wireless Extension use EAGAIN to try */ + wait_status = WIFI_UNDER_SURVEY +#ifndef CONFIG_RTW_ANDROID + | WIFI_UNDER_LINKING +#endif + ; + + while (check_fwstate(pmlmepriv, wait_status) == _TRUE) + return -EAGAIN; +#else + wait_status = WIFI_UNDER_SURVEY +#ifndef CONFIG_RTW_ANDROID + | WIFI_UNDER_LINKING +#endif + ; + + while (check_fwstate(pmlmepriv, wait_status) == _TRUE) { + rtw_msleep_os(30); + cnt++; + if (cnt > wait_for_surveydone) + break; + } +#endif + _enter_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + phead = get_list_head(queue); + plist = get_next(phead); + + while (1) { + if (rtw_end_of_queue_search(phead, plist) == _TRUE) + break; + + if ((stop - ev) < SCAN_ITEM_SIZE) { + if(wrqu->data.length == MAX_SCAN_BUFFER_LEN){ /*max buffer len defined by iwlist*/ + ret = 0; + RTW_INFO("%s: Scan results incomplete\n", __FUNCTION__); + break; + } + ret = -E2BIG; + break; + } + + pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list); + ch = pnetwork->network.Configuration.DSConfig; + + /* report network only if the current channel set contains the channel to which this network belongs */ + if (rtw_chset_search_ch(chset, ch) >= 0 + && rtw_mlme_band_check(padapter, ch) == _TRUE + && _TRUE == rtw_validate_ssid(&(pnetwork->network.Ssid)) + && (!IS_DFS_SLAVE_WITH_RD(rfctl) + || rtw_rfctl_dfs_domain_unknown(rfctl) + || !rtw_chset_is_ch_non_ocp(chset, ch)) + ) + ev = translate_scan(padapter, a, pnetwork, ev, stop); + + plist = get_next(plist); + + } + + _exit_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + wrqu->data.length = ev - extra; + wrqu->data.flags = 0; + +exit: + + +#ifdef DBG_IOCTL + RTW_INFO("DBG_IOCTL %s:%d return %d\n", __FUNCTION__, __LINE__, ret); +#endif + + return ret ; + +} + +/* set ssid flow + * s1. rtw_set_802_11_infrastructure_mode() + * s2. set_802_11_authenticaion_mode() + * s3. set_802_11_encryption_mode() + * s4. rtw_set_802_11_ssid() */ +static int rtw_wx_set_essid(struct net_device *dev, + struct iw_request_info *a, + union iwreq_data *wrqu, char *extra) +{ + _irqL irqL; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + _queue *queue = &pmlmepriv->scanned_queue; + _list *phead; + struct wlan_network *pnetwork = NULL; + NDIS_802_11_AUTHENTICATION_MODE authmode; + NDIS_802_11_SSID ndis_ssid; + u8 *dst_ssid, *src_ssid; + + uint ret = 0, len; + + +#ifdef DBG_IOCTL + RTW_INFO("DBG_IOCTL %s:%d\n", __FUNCTION__, __LINE__); +#endif +#ifdef CONFIG_WEXT_DONT_JOIN_BYSSID + RTW_INFO("%s: CONFIG_WEXT_DONT_JOIN_BYSSID be defined!! only allow bssid joining\n", __func__); + return -EPERM; +#endif + +#if WIRELESS_EXT <= 20 + if ((wrqu->essid.length - 1) > IW_ESSID_MAX_SIZE) { +#else + if (wrqu->essid.length > IW_ESSID_MAX_SIZE) { +#endif + ret = -E2BIG; + goto exit; + } + + + + rtw_ps_deny(padapter, PS_DENY_JOIN); + if (_FAIL == rtw_pwr_wakeup(padapter)) { + ret = -1; + goto cancel_ps_deny; + } + + if (!padapter->bup) { + ret = -1; + goto cancel_ps_deny; + } + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) { + ret = -1; + goto cancel_ps_deny; + } + +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_buddy_check_fwstate(padapter, WIFI_UNDER_SURVEY | WIFI_UNDER_LINKING)) { + RTW_INFO("set ssid, but buddy_intf is under scanning or linking\n"); + ret = -EINVAL; + goto cancel_ps_deny; + } +#endif + authmode = padapter->securitypriv.ndisauthtype; + RTW_INFO("=>%s\n", __FUNCTION__); + if (wrqu->essid.flags && wrqu->essid.length) { + /* Commented by Albert 20100519 */ + /* We got the codes in "set_info" function of iwconfig source code. */ + /* ========================================= */ + /* wrq.u.essid.length = strlen(essid) + 1; */ + /* if(we_kernel_version > 20) */ + /* wrq.u.essid.length--; */ + /* ========================================= */ + /* That means, if the WIRELESS_EXT less than or equal to 20, the correct ssid len should subtract 1. */ +#if WIRELESS_EXT <= 20 + len = ((wrqu->essid.length - 1) < IW_ESSID_MAX_SIZE) ? (wrqu->essid.length - 1) : IW_ESSID_MAX_SIZE; +#else + len = (wrqu->essid.length < IW_ESSID_MAX_SIZE) ? wrqu->essid.length : IW_ESSID_MAX_SIZE; +#endif + + if (wrqu->essid.length != 33) + RTW_INFO("ssid=%s, len=%d\n", extra, wrqu->essid.length); + + _rtw_memset(&ndis_ssid, 0, sizeof(NDIS_802_11_SSID)); + ndis_ssid.SsidLength = len; + _rtw_memcpy(ndis_ssid.Ssid, extra, len); + src_ssid = ndis_ssid.Ssid; + + _enter_critical_bh(&queue->lock, &irqL); + phead = get_list_head(queue); + pmlmepriv->pscanned = get_next(phead); + + while (1) { + if (rtw_end_of_queue_search(phead, pmlmepriv->pscanned) == _TRUE) { +#if 0 + if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == _TRUE) { + rtw_set_802_11_ssid(padapter, &ndis_ssid); + + goto cancel_ps_deny; + } else { + ret = -EINVAL; + goto cancel_ps_deny; + } +#endif + + break; + } + + pnetwork = LIST_CONTAINOR(pmlmepriv->pscanned, struct wlan_network, list); + + pmlmepriv->pscanned = get_next(pmlmepriv->pscanned); + + dst_ssid = pnetwork->network.Ssid.Ssid; + + + if ((_rtw_memcmp(dst_ssid, src_ssid, ndis_ssid.SsidLength) == _TRUE) && + (pnetwork->network.Ssid.SsidLength == ndis_ssid.SsidLength)) { + + if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == _TRUE) { + if (pnetwork->network.InfrastructureMode != pmlmepriv->cur_network.network.InfrastructureMode) + continue; + } + + if (rtw_set_802_11_infrastructure_mode(padapter, pnetwork->network.InfrastructureMode, 0) == _FALSE) { + ret = -1; + _exit_critical_bh(&queue->lock, &irqL); + goto cancel_ps_deny; + } + + break; + } + } + _exit_critical_bh(&queue->lock, &irqL); + rtw_set_802_11_authentication_mode(padapter, authmode); + /* set_802_11_encryption_mode(padapter, padapter->securitypriv.ndisencryptstatus); */ + if (rtw_set_802_11_ssid(padapter, &ndis_ssid) == _FALSE) { + ret = -1; + goto cancel_ps_deny; + } + } + +cancel_ps_deny: + rtw_ps_deny_cancel(padapter, PS_DENY_JOIN); + +exit: + RTW_INFO("<=%s, ret %d\n", __FUNCTION__, ret); + +#ifdef DBG_IOCTL + RTW_INFO("DBG_IOCTL %s:%d return %d\n", __FUNCTION__, __LINE__, ret); +#endif + + + return ret; +} + +static int rtw_wx_get_essid(struct net_device *dev, + struct iw_request_info *a, + union iwreq_data *wrqu, char *extra) +{ + u32 len, ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + WLAN_BSSID_EX *pcur_bss = &pmlmepriv->cur_network.network; + + + + if ((check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _TRUE) || + (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == _TRUE)) { + len = pcur_bss->Ssid.SsidLength; + + wrqu->essid.length = len; + + _rtw_memcpy(extra, pcur_bss->Ssid.Ssid, len); + + wrqu->essid.flags = 1; + } else { + ret = -1; + goto exit; + } + +exit: + + + return ret; + +} + +static int rtw_wx_set_rate(struct net_device *dev, + struct iw_request_info *a, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; +#if 0 + int i; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + u8 datarates[NumRates]; + u32 target_rate = wrqu->bitrate.value; + u32 fixed = wrqu->bitrate.fixed; + u32 ratevalue = 0; + u8 mpdatarate[NumRates] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0xff}; + + + + if (target_rate == -1) { + ratevalue = 11; + goto set_rate; + } + target_rate = target_rate / 100000; + + switch (target_rate) { + case 10: + ratevalue = 0; + break; + case 20: + ratevalue = 1; + break; + case 55: + ratevalue = 2; + break; + case 60: + ratevalue = 3; + break; + case 90: + ratevalue = 4; + break; + case 110: + ratevalue = 5; + break; + case 120: + ratevalue = 6; + break; + case 180: + ratevalue = 7; + break; + case 240: + ratevalue = 8; + break; + case 360: + ratevalue = 9; + break; + case 480: + ratevalue = 10; + break; + case 540: + ratevalue = 11; + break; + default: + ratevalue = 11; + break; + } + +set_rate: + + for (i = 0; i < NumRates; i++) { + if (ratevalue == mpdatarate[i]) { + datarates[i] = mpdatarate[i]; + if (fixed == 0) + break; + } else + datarates[i] = 0xff; + + } + + if (rtw_setdatarate_cmd(padapter, datarates) != _SUCCESS) { + ret = -1; + } + +#endif + return ret; +} + +static int rtw_wx_get_rate(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + u16 max_rate = 0; + + max_rate = rtw_get_cur_max_rate((_adapter *)rtw_netdev_priv(dev)); + + if (max_rate == 0) + return -EPERM; + + wrqu->bitrate.fixed = 0; /* no auto select */ + wrqu->bitrate.value = max_rate * 100000; + + return 0; +} + +static int rtw_wx_set_rts(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + + if (wrqu->rts.disabled) + padapter->registrypriv.rts_thresh = 2347; + else { + if (wrqu->rts.value < 0 || + wrqu->rts.value > 2347) + return -EINVAL; + + padapter->registrypriv.rts_thresh = wrqu->rts.value; + } + + RTW_INFO("%s, rts_thresh=%d\n", __func__, padapter->registrypriv.rts_thresh); + + + return 0; + +} + +static int rtw_wx_get_rts(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + + RTW_INFO("%s, rts_thresh=%d\n", __func__, padapter->registrypriv.rts_thresh); + + wrqu->rts.value = padapter->registrypriv.rts_thresh; + wrqu->rts.fixed = 0; /* no auto select */ + /* wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD); */ + + + return 0; +} + +static int rtw_wx_set_frag(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + + if (wrqu->frag.disabled) + padapter->xmitpriv.frag_len = MAX_FRAG_THRESHOLD; + else { + if (wrqu->frag.value < MIN_FRAG_THRESHOLD || + wrqu->frag.value > MAX_FRAG_THRESHOLD) + return -EINVAL; + + padapter->xmitpriv.frag_len = wrqu->frag.value & ~0x1; + } + + RTW_INFO("%s, frag_len=%d\n", __func__, padapter->xmitpriv.frag_len); + + + return 0; + +} + +static int rtw_wx_get_frag(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + + RTW_INFO("%s, frag_len=%d\n", __func__, padapter->xmitpriv.frag_len); + + wrqu->frag.value = padapter->xmitpriv.frag_len; + wrqu->frag.fixed = 0; /* no auto select */ + /* wrqu->frag.disabled = (wrqu->frag.value == DEFAULT_FRAG_THRESHOLD); */ + + + return 0; +} + +static int rtw_wx_get_retry(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + /* _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); */ + + + wrqu->retry.value = 7; + wrqu->retry.fixed = 0; /* no auto select */ + wrqu->retry.disabled = 1; + + return 0; + +} + +#if 0 + #define IW_ENCODE_INDEX 0x00FF /* Token index (if needed) */ + #define IW_ENCODE_FLAGS 0xFF00 /* Flags defined below */ + #define IW_ENCODE_MODE 0xF000 /* Modes defined below */ + #define IW_ENCODE_DISABLED 0x8000 /* Encoding disabled */ + #define IW_ENCODE_ENABLED 0x0000 /* Encoding enabled */ + #define IW_ENCODE_RESTRICTED 0x4000 /* Refuse non-encoded packets */ + #define IW_ENCODE_OPEN 0x2000 /* Accept non-encoded packets */ + #define IW_ENCODE_NOKEY 0x0800 /* Key is write only, so not present */ + #define IW_ENCODE_TEMP 0x0400 /* Temporary key */ + /* + iwconfig wlan0 key on->flags = 0x6001->maybe it means auto + iwconfig wlan0 key off->flags = 0x8800 + iwconfig wlan0 key open->flags = 0x2800 + iwconfig wlan0 key open 1234567890->flags = 0x2000 + iwconfig wlan0 key restricted->flags = 0x4800 + iwconfig wlan0 key open [3] 1234567890->flags = 0x2003 + iwconfig wlan0 key restricted [2] 1234567890->flags = 0x4002 + iwconfig wlan0 key open [3] -> flags = 0x2803 + iwconfig wlan0 key restricted [2] -> flags = 0x4802 + */ +#endif + +static int rtw_wx_set_enc(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *keybuf) +{ + u32 key, ret = 0; + u32 keyindex_provided; + NDIS_802_11_WEP wep; + NDIS_802_11_AUTHENTICATION_MODE authmode; + + struct iw_point *erq = &(wrqu->encoding); + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + RTW_INFO("+rtw_wx_set_enc, flags=0x%x\n", erq->flags); + + _rtw_memset(&wep, 0, sizeof(NDIS_802_11_WEP)); + + key = erq->flags & IW_ENCODE_INDEX; + + + if (erq->flags & IW_ENCODE_DISABLED) { + RTW_INFO("EncryptionDisabled\n"); + padapter->securitypriv.ndisencryptstatus = Ndis802_11EncryptionDisabled; + padapter->securitypriv.dot11PrivacyAlgrthm = _NO_PRIVACY_; + padapter->securitypriv.dot118021XGrpPrivacy = _NO_PRIVACY_; + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Open; /* open system */ + authmode = Ndis802_11AuthModeOpen; + padapter->securitypriv.ndisauthtype = authmode; + + goto exit; + } + + if (key) { + if (key > WEP_KEYS) + return -EINVAL; + key--; + keyindex_provided = 1; + } else { + keyindex_provided = 0; + key = padapter->securitypriv.dot11PrivacyKeyIndex; + RTW_INFO("rtw_wx_set_enc, key=%d\n", key); + } + + /* set authentication mode */ + if (erq->flags & IW_ENCODE_OPEN) { + RTW_INFO("rtw_wx_set_enc():IW_ENCODE_OPEN\n"); + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;/* Ndis802_11EncryptionDisabled; */ + +#ifdef CONFIG_PLATFORM_MT53XX + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Auto; +#else + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Open; +#endif + + padapter->securitypriv.dot11PrivacyAlgrthm = _NO_PRIVACY_; + padapter->securitypriv.dot118021XGrpPrivacy = _NO_PRIVACY_; + authmode = Ndis802_11AuthModeOpen; + padapter->securitypriv.ndisauthtype = authmode; + } else if (erq->flags & IW_ENCODE_RESTRICTED) { + RTW_INFO("rtw_wx_set_enc():IW_ENCODE_RESTRICTED\n"); + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled; + +#ifdef CONFIG_PLATFORM_MT53XX + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Auto; +#else + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Shared; +#endif + + padapter->securitypriv.dot11PrivacyAlgrthm = _WEP40_; + padapter->securitypriv.dot118021XGrpPrivacy = _WEP40_; + authmode = Ndis802_11AuthModeShared; + padapter->securitypriv.ndisauthtype = authmode; + } else { + RTW_INFO("rtw_wx_set_enc():erq->flags=0x%x\n", erq->flags); + + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;/* Ndis802_11EncryptionDisabled; */ + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Open; /* open system */ + padapter->securitypriv.dot11PrivacyAlgrthm = _NO_PRIVACY_; + padapter->securitypriv.dot118021XGrpPrivacy = _NO_PRIVACY_; + authmode = Ndis802_11AuthModeOpen; + padapter->securitypriv.ndisauthtype = authmode; + } + + wep.KeyIndex = key; + if (erq->length > 0) { + wep.KeyLength = erq->length <= 5 ? 5 : 13; + + wep.Length = wep.KeyLength + FIELD_OFFSET(NDIS_802_11_WEP, KeyMaterial); + } else { + wep.KeyLength = 0 ; + + if (keyindex_provided == 1) { /* set key_id only, no given KeyMaterial(erq->length==0). */ + padapter->securitypriv.dot11PrivacyKeyIndex = key; + + RTW_INFO("(keyindex_provided == 1), keyid=%d, key_len=%d\n", key, padapter->securitypriv.dot11DefKeylen[key]); + + switch (padapter->securitypriv.dot11DefKeylen[key]) { + case 5: + padapter->securitypriv.dot11PrivacyAlgrthm = _WEP40_; + break; + case 13: + padapter->securitypriv.dot11PrivacyAlgrthm = _WEP104_; + break; + default: + padapter->securitypriv.dot11PrivacyAlgrthm = _NO_PRIVACY_; + break; + } + + goto exit; + + } + + } + + wep.KeyIndex |= 0x80000000; + + _rtw_memcpy(wep.KeyMaterial, keybuf, wep.KeyLength); + + if (rtw_set_802_11_add_wep(padapter, &wep) == _FALSE) { + if (rf_on == pwrpriv->rf_pwrstate) + ret = -EOPNOTSUPP; + goto exit; + } + +exit: + + + return ret; + +} + +static int rtw_wx_get_enc(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *keybuf) +{ + uint key, ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct iw_point *erq = &(wrqu->encoding); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + + + if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) != _TRUE) { + if (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) != _TRUE) { + erq->length = 0; + erq->flags |= IW_ENCODE_DISABLED; + return 0; + } + } + + + key = erq->flags & IW_ENCODE_INDEX; + + if (key) { + if (key > WEP_KEYS) + return -EINVAL; + key--; + } else + key = padapter->securitypriv.dot11PrivacyKeyIndex; + + erq->flags = key + 1; + + /* if(padapter->securitypriv.ndisauthtype == Ndis802_11AuthModeOpen) */ + /* { */ + /* erq->flags |= IW_ENCODE_OPEN; */ + /* } */ + + switch (padapter->securitypriv.ndisencryptstatus) { + case Ndis802_11EncryptionNotSupported: + case Ndis802_11EncryptionDisabled: + + erq->length = 0; + erq->flags |= IW_ENCODE_DISABLED; + + break; + + case Ndis802_11Encryption1Enabled: + + erq->length = padapter->securitypriv.dot11DefKeylen[key]; + + if (erq->length) { + _rtw_memcpy(keybuf, padapter->securitypriv.dot11DefKey[key].skey, padapter->securitypriv.dot11DefKeylen[key]); + + erq->flags |= IW_ENCODE_ENABLED; + + if (padapter->securitypriv.ndisauthtype == Ndis802_11AuthModeOpen) + erq->flags |= IW_ENCODE_OPEN; + else if (padapter->securitypriv.ndisauthtype == Ndis802_11AuthModeShared) + erq->flags |= IW_ENCODE_RESTRICTED; + } else { + erq->length = 0; + erq->flags |= IW_ENCODE_DISABLED; + } + + break; + + case Ndis802_11Encryption2Enabled: + case Ndis802_11Encryption3Enabled: + + erq->length = 16; + erq->flags |= (IW_ENCODE_ENABLED | IW_ENCODE_OPEN | IW_ENCODE_NOKEY); + + break; + + default: + erq->length = 0; + erq->flags |= IW_ENCODE_DISABLED; + + break; + + } + + + return ret; + +} + +static int rtw_wx_get_power(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + /* _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); */ + + wrqu->power.value = 0; + wrqu->power.fixed = 0; /* no auto select */ + wrqu->power.disabled = 1; + + return 0; + +} + +static int rtw_wx_set_gen_ie(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + ret = rtw_set_wpa_ie(padapter, extra, wrqu->data.length); + + return ret; +} + +static int rtw_wx_set_auth(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct iw_param *param = (struct iw_param *)&(wrqu->param); +#ifdef CONFIG_WAPI_SUPPORT +#ifndef CONFIG_IOCTL_CFG80211 + struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv; + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + struct security_priv *psecuritypriv = &padapter->securitypriv; + u32 value = param->value; +#endif +#endif + int ret = 0; + + switch (param->flags & IW_AUTH_INDEX) { + + case IW_AUTH_WPA_VERSION: +#ifdef CONFIG_WAPI_SUPPORT +#ifndef CONFIG_IOCTL_CFG80211 + padapter->wapiInfo.bWapiEnable = false; + if (value == IW_AUTH_WAPI_VERSION_1) { + padapter->wapiInfo.bWapiEnable = true; + psecuritypriv->dot11PrivacyAlgrthm = _SMS4_; + psecuritypriv->dot118021XGrpPrivacy = _SMS4_; + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_WAPI; + pmlmeinfo->auth_algo = psecuritypriv->dot11AuthAlgrthm; + padapter->wapiInfo.extra_prefix_len = WAPI_EXT_LEN; + padapter->wapiInfo.extra_postfix_len = SMS4_MIC_LEN; + } +#endif +#endif + break; + case IW_AUTH_CIPHER_PAIRWISE: + + break; + case IW_AUTH_CIPHER_GROUP: + + break; + case IW_AUTH_KEY_MGMT: +#ifdef CONFIG_WAPI_SUPPORT +#ifndef CONFIG_IOCTL_CFG80211 + RTW_INFO("rtw_wx_set_auth: IW_AUTH_KEY_MGMT case\n"); + if (value == IW_AUTH_KEY_MGMT_WAPI_PSK) + padapter->wapiInfo.bWapiPSK = true; + else + padapter->wapiInfo.bWapiPSK = false; + RTW_INFO("rtw_wx_set_auth: IW_AUTH_KEY_MGMT bwapipsk %d\n", padapter->wapiInfo.bWapiPSK); +#endif +#endif + /* + * ??? does not use these parameters + */ + break; + + case IW_AUTH_TKIP_COUNTERMEASURES: { + if (param->value) { + /* wpa_supplicant is enabling the tkip countermeasure. */ + padapter->securitypriv.btkip_countermeasure = _TRUE; + } else { + /* wpa_supplicant is disabling the tkip countermeasure. */ + padapter->securitypriv.btkip_countermeasure = _FALSE; + } + break; + } + case IW_AUTH_DROP_UNENCRYPTED: { + /* HACK: + * + * wpa_supplicant calls set_wpa_enabled when the driver + * is loaded and unloaded, regardless of if WPA is being + * used. No other calls are made which can be used to + * determine if encryption will be used or not prior to + * association being expected. If encryption is not being + * used, drop_unencrypted is set to false, else true -- we + * can use this to determine if the CAP_PRIVACY_ON bit should + * be set. + */ + + if (padapter->securitypriv.ndisencryptstatus == Ndis802_11Encryption1Enabled) { + break;/* it means init value, or using wep, ndisencryptstatus = Ndis802_11Encryption1Enabled, */ + /* then it needn't reset it; */ + } + + if (param->value) { + padapter->securitypriv.ndisencryptstatus = Ndis802_11EncryptionDisabled; + padapter->securitypriv.dot11PrivacyAlgrthm = _NO_PRIVACY_; + padapter->securitypriv.dot118021XGrpPrivacy = _NO_PRIVACY_; + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Open; /* open system */ + padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeOpen; + } + + break; + } + + case IW_AUTH_80211_AUTH_ALG: + +#if defined(CONFIG_RTW_ANDROID) || 1 + /* + * It's the starting point of a link layer connection using wpa_supplicant + */ + if (check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE)) { + LeaveAllPowerSaveMode(padapter); + rtw_disassoc_cmd(padapter, 500, RTW_CMDF_WAIT_ACK); + RTW_INFO("%s...call rtw_indicate_disconnect\n ", __FUNCTION__); + rtw_indicate_disconnect(padapter, 0, _FALSE); + rtw_free_assoc_resources_cmd(padapter, _TRUE, RTW_CMDF_WAIT_ACK); + } +#endif + + + ret = wpa_set_auth_algs(dev, (u32)param->value); + + break; + + case IW_AUTH_WPA_ENABLED: + + /* if(param->value) */ + /* padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_8021X; */ /* 802.1x */ + /* else */ + /* padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Open; */ /* open system */ + + /* _disassociate(priv); */ + + break; + + case IW_AUTH_RX_UNENCRYPTED_EAPOL: + /* ieee->ieee802_1x = param->value; */ + break; + + case IW_AUTH_PRIVACY_INVOKED: + /* ieee->privacy_invoked = param->value; */ + break; + +#ifdef CONFIG_WAPI_SUPPORT +#ifndef CONFIG_IOCTL_CFG80211 + case IW_AUTH_WAPI_ENABLED: + break; +#endif +#endif + + default: + return -EOPNOTSUPP; + + } + + return ret; + +} + +static int rtw_wx_set_enc_ext(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + char *alg_name; + u32 param_len; + struct ieee_param *param = NULL; + struct iw_point *pencoding = &wrqu->encoding; + struct iw_encode_ext *pext = (struct iw_encode_ext *)extra; + int ret = 0; + + param_len = sizeof(struct ieee_param) + pext->key_len; + param = (struct ieee_param *)rtw_malloc(param_len); + if (param == NULL) + return -1; + + _rtw_memset(param, 0, param_len); + + param->cmd = IEEE_CMD_SET_ENCRYPTION; + _rtw_memset(param->sta_addr, 0xff, ETH_ALEN); + + + switch (pext->alg) { + case IW_ENCODE_ALG_NONE: + /* todo: remove key */ + /* remove = 1; */ + alg_name = "none"; + break; + case IW_ENCODE_ALG_WEP: + alg_name = "WEP"; + break; + case IW_ENCODE_ALG_TKIP: + alg_name = "TKIP"; + break; + case IW_ENCODE_ALG_CCMP: + alg_name = "CCMP"; + break; +#ifdef CONFIG_IEEE80211W + case IW_ENCODE_ALG_AES_CMAC: + alg_name = "BIP"; + break; +#endif /* CONFIG_IEEE80211W */ +#ifdef CONFIG_WAPI_SUPPORT +#ifndef CONFIG_IOCTL_CFG80211 + case IW_ENCODE_ALG_SM4: + alg_name = "SMS4"; + _rtw_memcpy(param->sta_addr, pext->addr.sa_data, ETH_ALEN); + RTW_INFO("rtw_wx_set_enc_ext: SMS4 case\n"); + break; +#endif +#endif + default: + ret = -1; + goto exit; + } + + strncpy((char *)param->u.crypt.alg, alg_name, IEEE_CRYPT_ALG_NAME_LEN); + + if (pext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) + param->u.crypt.set_tx = 1; + + /* cliW: WEP does not have group key + * just not checking GROUP key setting + */ + if ((pext->alg != IW_ENCODE_ALG_WEP) && + ((pext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) +#ifdef CONFIG_IEEE80211W + || (pext->ext_flags & IW_ENCODE_ALG_AES_CMAC) +#endif /* CONFIG_IEEE80211W */ + )) + param->u.crypt.set_tx = 0; + + param->u.crypt.idx = (pencoding->flags & 0x00FF) - 1 ; + + if (pext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) { +#ifdef CONFIG_WAPI_SUPPORT +#ifndef CONFIG_IOCTL_CFG80211 + if (pext->alg == IW_ENCODE_ALG_SM4) + _rtw_memcpy(param->u.crypt.seq, pext->rx_seq, 16); + else +#endif /* CONFIG_IOCTL_CFG80211 */ +#endif /* CONFIG_WAPI_SUPPORT */ + _rtw_memcpy(param->u.crypt.seq, pext->rx_seq, 8); + } + + if (pext->key_len) { + param->u.crypt.key_len = pext->key_len; + /* _rtw_memcpy(param + 1, pext + 1, pext->key_len); */ + _rtw_memcpy(param->u.crypt.key, pext + 1, pext->key_len); + } + + if (pencoding->flags & IW_ENCODE_DISABLED) { + /* todo: remove key */ + /* remove = 1; */ + } + + ret = wpa_set_encryption(dev, param, param_len); + +exit: + if (param) + rtw_mfree((u8 *)param, param_len); + + return ret; +} + + +static int rtw_wx_get_nick(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + /* _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); */ + /* struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); */ + /* struct security_priv *psecuritypriv = &padapter->securitypriv; */ + + if (extra) { + wrqu->data.length = 14; + wrqu->data.flags = 1; + _rtw_memcpy(extra, "", 14); + } + + /* rtw_signal_process(pid, SIGUSR1); */ /* for test */ + + /* dump debug info here */ +#if 0 + u32 dot11AuthAlgrthm; /* 802.11 auth, could be open, shared, and 8021x */ + u32 dot11PrivacyAlgrthm; /* This specify the privacy for shared auth. algorithm. */ + u32 dot118021XGrpPrivacy; /* This specify the privacy algthm. used for Grp key */ + u32 ndisauthtype; + u32 ndisencryptstatus; +#endif + + /* RTW_INFO("auth_alg=0x%x, enc_alg=0x%x, auth_type=0x%x, enc_type=0x%x\n", */ + /* psecuritypriv->dot11AuthAlgrthm, psecuritypriv->dot11PrivacyAlgrthm, */ + /* psecuritypriv->ndisauthtype, psecuritypriv->ndisencryptstatus); */ + + /* RTW_INFO("enc_alg=0x%x\n", psecuritypriv->dot11PrivacyAlgrthm); */ + /* RTW_INFO("auth_type=0x%x\n", psecuritypriv->ndisauthtype); */ + /* RTW_INFO("enc_type=0x%x\n", psecuritypriv->ndisencryptstatus); */ + +#if 0 + RTW_INFO("dbg(0x210)=0x%x\n", rtw_read32(padapter, 0x210)); + RTW_INFO("dbg(0x608)=0x%x\n", rtw_read32(padapter, 0x608)); + RTW_INFO("dbg(0x280)=0x%x\n", rtw_read32(padapter, 0x280)); + RTW_INFO("dbg(0x284)=0x%x\n", rtw_read32(padapter, 0x284)); + RTW_INFO("dbg(0x288)=0x%x\n", rtw_read32(padapter, 0x288)); + + RTW_INFO("dbg(0x664)=0x%x\n", rtw_read32(padapter, 0x664)); + + + RTW_INFO("\n"); + + RTW_INFO("dbg(0x430)=0x%x\n", rtw_read32(padapter, 0x430)); + RTW_INFO("dbg(0x438)=0x%x\n", rtw_read32(padapter, 0x438)); + + RTW_INFO("dbg(0x440)=0x%x\n", rtw_read32(padapter, 0x440)); + + RTW_INFO("dbg(0x458)=0x%x\n", rtw_read32(padapter, 0x458)); + + RTW_INFO("dbg(0x484)=0x%x\n", rtw_read32(padapter, 0x484)); + RTW_INFO("dbg(0x488)=0x%x\n", rtw_read32(padapter, 0x488)); + + RTW_INFO("dbg(0x444)=0x%x\n", rtw_read32(padapter, 0x444)); + RTW_INFO("dbg(0x448)=0x%x\n", rtw_read32(padapter, 0x448)); + RTW_INFO("dbg(0x44c)=0x%x\n", rtw_read32(padapter, 0x44c)); + RTW_INFO("dbg(0x450)=0x%x\n", rtw_read32(padapter, 0x450)); +#endif + + return 0; + +} +#endif + +static int rtw_wx_read32(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + PADAPTER padapter; + struct iw_point *p; + u16 len; + u32 addr; + u32 data32; + u32 bytes; + u8 *ptmp; + int ret; + + + ret = 0; + padapter = (PADAPTER)rtw_netdev_priv(dev); + p = &wrqu->data; + len = p->length; + if (0 == len) + return -EINVAL; + + ptmp = (u8 *)rtw_malloc(len); + if (NULL == ptmp) + return -ENOMEM; + + if (copy_from_user(ptmp, p->pointer, len)) { + ret = -EFAULT; + goto exit; + } + + bytes = 0; + addr = 0; + sscanf(ptmp, "%d,%x", &bytes, &addr); + + switch (bytes) { + case 1: + data32 = rtw_read8(padapter, addr); + sprintf(extra, "0x%02X", data32); + break; + case 2: + data32 = rtw_read16(padapter, addr); + sprintf(extra, "0x%04X", data32); + break; + case 4: + data32 = rtw_read32(padapter, addr); + sprintf(extra, "0x%08X", data32); + break; + + #if defined(CONFIG_SDIO_HCI) && defined(CONFIG_SDIO_INDIRECT_ACCESS) && defined(DBG_SDIO_INDIRECT_ACCESS) + case 11: + data32 = rtw_sd_iread8(padapter, addr); + sprintf(extra, "0x%02X", data32); + break; + case 12: + data32 = rtw_sd_iread16(padapter, addr); + sprintf(extra, "0x%04X", data32); + break; + case 14: + data32 = rtw_sd_iread32(padapter, addr); + sprintf(extra, "0x%08X", data32); + break; + #endif + default: + RTW_INFO("%s: usage> read [bytes],[address(hex)]\n", __func__); + ret = -EINVAL; + goto exit; + } + RTW_INFO("%s: addr=0x%08X data=%s\n", __func__, addr, extra); + +exit: + rtw_mfree(ptmp, len); + + return 0; +} + +static int rtw_wx_write32(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + PADAPTER padapter = (PADAPTER)rtw_netdev_priv(dev); + + u32 addr; + u32 data32; + u32 bytes; + + + bytes = 0; + addr = 0; + data32 = 0; + sscanf(extra, "%d,%x,%x", &bytes, &addr, &data32); + + switch (bytes) { + case 1: + rtw_write8(padapter, addr, (u8)data32); + RTW_INFO("%s: addr=0x%08X data=0x%02X\n", __func__, addr, (u8)data32); + break; + case 2: + rtw_write16(padapter, addr, (u16)data32); + RTW_INFO("%s: addr=0x%08X data=0x%04X\n", __func__, addr, (u16)data32); + break; + case 4: + rtw_write32(padapter, addr, data32); + RTW_INFO("%s: addr=0x%08X data=0x%08X\n", __func__, addr, data32); + break; + default: + RTW_INFO("%s: usage> write [bytes],[address(hex)],[data(hex)]\n", __func__); + return -EINVAL; + } + + return 0; +} + +static int rtw_wx_read_rf(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + u32 path, addr, data32; + + + path = *(u32 *)extra; + addr = *((u32 *)extra + 1); + data32 = rtw_hal_read_rfreg(padapter, path, addr, 0xFFFFF); + /* RTW_INFO("%s: path=%d addr=0x%02x data=0x%05x\n", __func__, path, addr, data32); */ + /* + * IMPORTANT!! + * Only when wireless private ioctl is at odd order, + * "extra" would be copied to user space. + */ + sprintf(extra, "0x%05x", data32); + + return 0; +} + +static int rtw_wx_write_rf(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + u32 path, addr, data32; + + + path = *(u32 *)extra; + addr = *((u32 *)extra + 1); + data32 = *((u32 *)extra + 2); + /* RTW_INFO("%s: path=%d addr=0x%02x data=0x%05x\n", __func__, path, addr, data32); */ + rtw_hal_write_rfreg(padapter, path, addr, 0xFFFFF, data32); + + return 0; +} + +static int rtw_wx_priv_null(struct net_device *dev, struct iw_request_info *a, + union iwreq_data *wrqu, char *b) +{ + return -1; +} + +#ifdef CONFIG_RTW_80211K +extern void rm_dbg_cmd(_adapter *padapter, char *s); +static int rtw_wx_priv_rrm(struct net_device *dev, struct iw_request_info *a, + union iwreq_data *wrqu, char *b) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + u32 path, addr, data32; + + + rm_dbg_cmd(padapter, b); + wrqu->data.length = strlen(b); + + return 0; +} +#endif + +#ifdef CONFIG_IOCTL_WEXT +static int dummy(struct net_device *dev, struct iw_request_info *a, + union iwreq_data *wrqu, char *b) +{ + /* _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); */ + /* struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); */ + + /* RTW_INFO("cmd_code=%x, fwstate=0x%x\n", a->cmd, get_fwstate(pmlmepriv)); */ + + return -1; + +} +#endif + +static int rtw_wx_set_channel_plan(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + u8 channel_plan_req = (u8)(*((int *)wrqu)); + + if (_SUCCESS != rtw_set_channel_plan(padapter, channel_plan_req)) + return -EPERM; + + return 0; +} + +static int rtw_wx_set_mtk_wps_probe_ie(struct net_device *dev, + struct iw_request_info *a, + union iwreq_data *wrqu, char *b) +{ +#ifdef CONFIG_PLATFORM_MT53XX + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + +#endif + return 0; +} + +static int rtw_wx_get_sensitivity(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *buf) +{ +#ifdef CONFIG_PLATFORM_MT53XX + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + /* Modified by Albert 20110914 */ + /* This is in dbm format for MTK platform. */ + wrqu->qual.level = padapter->recvpriv.rssi; + RTW_INFO(" level = %u\n", wrqu->qual.level); +#endif + return 0; +} + +static int rtw_wx_set_mtk_wps_ie(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ +#ifdef CONFIG_PLATFORM_MT53XX + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + return rtw_set_wpa_ie(padapter, wrqu->data.pointer, wrqu->data.length); +#else + return 0; +#endif +} + +#ifdef MP_IOCTL_HDL +static void rtw_dbg_mode_hdl(_adapter *padapter, u32 id, u8 *pdata, u32 len) +{ + pRW_Reg RegRWStruct; + struct rf_reg_param *prfreg; + u8 path; + u8 offset; + u32 value; + + RTW_INFO("%s\n", __FUNCTION__); + + switch (id) { + case GEN_MP_IOCTL_SUBCODE(MP_START): + RTW_INFO("871x_driver is only for normal mode, can't enter mp mode\n"); + break; + case GEN_MP_IOCTL_SUBCODE(READ_REG): + RegRWStruct = (pRW_Reg)pdata; + switch (RegRWStruct->width) { + case 1: + RegRWStruct->value = rtw_read8(padapter, RegRWStruct->offset); + break; + case 2: + RegRWStruct->value = rtw_read16(padapter, RegRWStruct->offset); + break; + case 4: + RegRWStruct->value = rtw_read32(padapter, RegRWStruct->offset); + break; + default: + break; + } + + break; + case GEN_MP_IOCTL_SUBCODE(WRITE_REG): + RegRWStruct = (pRW_Reg)pdata; + switch (RegRWStruct->width) { + case 1: + rtw_write8(padapter, RegRWStruct->offset, (u8)RegRWStruct->value); + break; + case 2: + rtw_write16(padapter, RegRWStruct->offset, (u16)RegRWStruct->value); + break; + case 4: + rtw_write32(padapter, RegRWStruct->offset, (u32)RegRWStruct->value); + break; + default: + break; + } + + break; + case GEN_MP_IOCTL_SUBCODE(READ_RF_REG): + + prfreg = (struct rf_reg_param *)pdata; + + path = (u8)prfreg->path; + offset = (u8)prfreg->offset; + + value = rtw_hal_read_rfreg(padapter, path, offset, 0xffffffff); + + prfreg->value = value; + + break; + case GEN_MP_IOCTL_SUBCODE(WRITE_RF_REG): + + prfreg = (struct rf_reg_param *)pdata; + + path = (u8)prfreg->path; + offset = (u8)prfreg->offset; + value = prfreg->value; + + rtw_hal_write_rfreg(padapter, path, offset, 0xffffffff, value); + + break; + case GEN_MP_IOCTL_SUBCODE(TRIGGER_GPIO): + RTW_INFO("==> trigger gpio 0\n"); + rtw_hal_set_hwreg(padapter, HW_VAR_TRIGGER_GPIO_0, 0); + break; +#ifdef CONFIG_BT_COEXIST + case GEN_MP_IOCTL_SUBCODE(SET_DM_BT): + RTW_INFO("==> set dm_bt_coexist:%x\n", *(u8 *)pdata); + rtw_hal_set_hwreg(padapter, HW_VAR_BT_SET_COEXIST, pdata); + break; + case GEN_MP_IOCTL_SUBCODE(DEL_BA): + RTW_INFO("==> delete ba:%x\n", *(u8 *)pdata); + rtw_hal_set_hwreg(padapter, HW_VAR_BT_ISSUE_DELBA, pdata); + break; +#endif +#ifdef DBG_CONFIG_ERROR_DETECT + case GEN_MP_IOCTL_SUBCODE(GET_WIFI_STATUS): + *pdata = rtw_hal_sreset_get_wifi_status(padapter); + break; +#endif + + default: + break; + } + +} +static int rtw_mp_ioctl_hdl(struct net_device *dev, struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + u32 BytesRead, BytesWritten, BytesNeeded; + struct oid_par_priv oid_par; + struct mp_ioctl_handler *phandler; + struct mp_ioctl_param *poidparam; + uint status = 0; + u16 len; + u8 *pparmbuf = NULL, bset; + PADAPTER padapter = (PADAPTER)rtw_netdev_priv(dev); + struct iw_point *p = &wrqu->data; + + /* RTW_INFO("+rtw_mp_ioctl_hdl\n"); */ + + /* mutex_lock(&ioctl_mutex); */ + + if ((!p->length) || (!p->pointer)) { + ret = -EINVAL; + goto _rtw_mp_ioctl_hdl_exit; + } + + pparmbuf = NULL; + bset = (u8)(p->flags & 0xFFFF); + len = p->length; + pparmbuf = (u8 *)rtw_malloc(len); + if (pparmbuf == NULL) { + ret = -ENOMEM; + goto _rtw_mp_ioctl_hdl_exit; + } + + if (copy_from_user(pparmbuf, p->pointer, len)) { + ret = -EFAULT; + goto _rtw_mp_ioctl_hdl_exit; + } + + poidparam = (struct mp_ioctl_param *)pparmbuf; + + if (poidparam->subcode >= MAX_MP_IOCTL_SUBCODE) { + ret = -EINVAL; + goto _rtw_mp_ioctl_hdl_exit; + } + + /* RTW_INFO("%s: %d\n", __func__, poidparam->subcode); */ +#ifdef CONFIG_MP_INCLUDED + if (padapter->registrypriv.mp_mode == 1) { + phandler = mp_ioctl_hdl + poidparam->subcode; + + if ((phandler->paramsize != 0) && (poidparam->len < phandler->paramsize)) { + ret = -EINVAL; + goto _rtw_mp_ioctl_hdl_exit; + } + + if (phandler->handler) { + oid_par.adapter_context = padapter; + oid_par.oid = phandler->oid; + oid_par.information_buf = poidparam->data; + oid_par.information_buf_len = poidparam->len; + oid_par.dbg = 0; + + BytesWritten = 0; + BytesNeeded = 0; + + if (bset) { + oid_par.bytes_rw = &BytesRead; + oid_par.bytes_needed = &BytesNeeded; + oid_par.type_of_oid = SET_OID; + } else { + oid_par.bytes_rw = &BytesWritten; + oid_par.bytes_needed = &BytesNeeded; + oid_par.type_of_oid = QUERY_OID; + } + + status = phandler->handler(&oid_par); + + /* todo:check status, BytesNeeded, etc. */ + } else { + RTW_INFO("rtw_mp_ioctl_hdl(): err!, subcode=%d, oid=%d, handler=%p\n", + poidparam->subcode, phandler->oid, phandler->handler); + ret = -EFAULT; + goto _rtw_mp_ioctl_hdl_exit; + } + } else +#endif + { + rtw_dbg_mode_hdl(padapter, poidparam->subcode, poidparam->data, poidparam->len); + } + + if (bset == 0x00) {/* query info */ + if (copy_to_user(p->pointer, pparmbuf, len)) + ret = -EFAULT; + } + + if (status) { + ret = -EFAULT; + goto _rtw_mp_ioctl_hdl_exit; + } + +_rtw_mp_ioctl_hdl_exit: + + if (pparmbuf) + rtw_mfree(pparmbuf, len); + + /* mutex_unlock(&ioctl_mutex); */ + + return ret; +} +#endif /*MP_IOCTL_HDL*/ +static int rtw_get_ap_info(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + u32 cnt = 0, wpa_ielen; + _irqL irqL; + _list *plist, *phead; + unsigned char *pbuf; + u8 bssid[ETH_ALEN]; + char data[32]; + struct wlan_network *pnetwork = NULL; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + _queue *queue = &(pmlmepriv->scanned_queue); + struct iw_point *pdata = &wrqu->data; + + RTW_INFO("+rtw_get_aplist_info\n"); + + if (rtw_is_drv_stopped(padapter) || (pdata == NULL)) { + ret = -EINVAL; + goto exit; + } + + while ((check_fwstate(pmlmepriv, (WIFI_UNDER_SURVEY | WIFI_UNDER_LINKING))) == _TRUE) { + rtw_msleep_os(30); + cnt++; + if (cnt > 100) + break; + } + + + /* pdata->length = 0; */ /* ? */ + pdata->flags = 0; + if (pdata->length >= 32) { + if (copy_from_user(data, pdata->pointer, 32)) { + ret = -EINVAL; + goto exit; + } + } else { + ret = -EINVAL; + goto exit; + } + + _enter_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + phead = get_list_head(queue); + plist = get_next(phead); + + while (1) { + if (rtw_end_of_queue_search(phead, plist) == _TRUE) + break; + + + pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list); + + if (hwaddr_aton_i(data, bssid)) { + RTW_INFO("Invalid BSSID '%s'.\n", (u8 *)data); + _exit_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + return -EINVAL; + } + + + if (_rtw_memcmp(bssid, pnetwork->network.MacAddress, ETH_ALEN) == _TRUE) { /* BSSID match, then check if supporting wpa/wpa2 */ + RTW_INFO("BSSID:" MAC_FMT "\n", MAC_ARG(bssid)); + + pbuf = rtw_get_wpa_ie(&pnetwork->network.IEs[12], &wpa_ielen, pnetwork->network.IELength - 12); + if (pbuf && (wpa_ielen > 0)) { + pdata->flags = 1; + break; + } + + pbuf = rtw_get_wpa2_ie(&pnetwork->network.IEs[12], &wpa_ielen, pnetwork->network.IELength - 12); + if (pbuf && (wpa_ielen > 0)) { + pdata->flags = 2; + break; + } + + } + + plist = get_next(plist); + + } + + _exit_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + if (pdata->length >= 34) { + if (copy_to_user((u8 *)pdata->pointer + 32, (u8 *)&pdata->flags, 1)) { + ret = -EINVAL; + goto exit; + } + } + +exit: + + return ret; + +} + +static int rtw_set_pid(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = rtw_netdev_priv(dev); + int *pdata = (int *)wrqu; + int selector; + + if (rtw_is_drv_stopped(padapter) || (pdata == NULL)) { + ret = -EINVAL; + goto exit; + } + + selector = *pdata; + if (selector < 3 && selector >= 0) { + padapter->pid[selector] = *(pdata + 1); +#ifdef CONFIG_GLOBAL_UI_PID + ui_pid[selector] = *(pdata + 1); +#endif + RTW_INFO("%s set pid[%d]=%d\n", __FUNCTION__, selector , padapter->pid[selector]); + } else + RTW_INFO("%s selector %d error\n", __FUNCTION__, selector); + +exit: + + return ret; + +} + +static int rtw_wps_start(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct iw_point *pdata = &wrqu->data; + u32 u32wps_start = 0; + unsigned int uintRet = 0; + + if (RTW_CANNOT_RUN(padapter) || (NULL == pdata)) { + ret = -EINVAL; + goto exit; + } + + uintRet = copy_from_user((void *) &u32wps_start, pdata->pointer, 4); + if (u32wps_start == 0) + u32wps_start = *extra; + + RTW_INFO("[%s] wps_start = %d\n", __FUNCTION__, u32wps_start); + + if (u32wps_start == 1) /* WPS Start */ + rtw_led_control(padapter, LED_CTL_START_WPS); + else if (u32wps_start == 2) /* WPS Stop because of wps success */ + rtw_led_control(padapter, LED_CTL_STOP_WPS); + else if (u32wps_start == 3) /* WPS Stop because of wps fail */ + rtw_led_control(padapter, LED_CTL_STOP_WPS_FAIL); + +exit: + + return ret; + +} + +#ifdef CONFIG_P2P +static int rtw_wext_p2p_enable(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv; + enum P2P_ROLE init_role = P2P_ROLE_DISABLE; +#ifdef CONFIG_CONCURRENT_MODE + struct roch_info *prochinfo = &padapter->rochinfo; +#endif + + if (*extra == '0') + init_role = P2P_ROLE_DISABLE; + else if (*extra == '1') + init_role = P2P_ROLE_DEVICE; + else if (*extra == '2') + init_role = P2P_ROLE_CLIENT; + else if (*extra == '3') + init_role = P2P_ROLE_GO; + + if (_FAIL == rtw_p2p_enable(padapter, init_role)) { + ret = -EFAULT; + goto exit; + } + + /* set channel/bandwidth */ + if (init_role != P2P_ROLE_DISABLE) { + u8 channel, ch_offset; + u16 bwmode; + + if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_LISTEN)) { + /* Stay at the listen state and wait for discovery. */ + channel = pwdinfo->listen_channel; + pwdinfo->operating_channel = pwdinfo->listen_channel; + ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + bwmode = CHANNEL_WIDTH_20; + } +#ifdef CONFIG_CONCURRENT_MODE + else if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_IDLE)) { + + _set_timer(&prochinfo->ap_roch_ch_switch_timer, pwdinfo->ext_listen_interval); + + channel = rtw_mi_get_union_chan(padapter); + ch_offset = rtw_mi_get_union_offset(padapter); + bwmode = rtw_mi_get_union_bw(padapter); + + pwdinfo->operating_channel = channel; + } +#endif + else { + pwdinfo->operating_channel = pmlmeext->cur_channel; + + channel = pwdinfo->operating_channel; + ch_offset = pmlmeext->cur_ch_offset; + bwmode = pmlmeext->cur_bwmode; + } + + set_channel_bwmode(padapter, channel, ch_offset, bwmode); + } + +exit: + return ret; + +} + +static int rtw_p2p_set_go_nego_ssid(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + RTW_INFO("[%s] ssid = %s, len = %zu\n", __FUNCTION__, extra, strlen(extra)); + _rtw_memcpy(pwdinfo->nego_ssid, extra, strlen(extra)); + pwdinfo->nego_ssidlen = strlen(extra); + + return ret; + +} + + +static int rtw_p2p_set_intent(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + u8 intent = pwdinfo->intent; + + extra[wrqu->data.length] = 0x00; + + intent = rtw_atoi(extra); + + if (intent <= 15) + pwdinfo->intent = intent; + else + ret = -1; + + RTW_INFO("[%s] intent = %d\n", __FUNCTION__, intent); + + return ret; + +} + +static int rtw_p2p_set_listen_ch(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + u8 listen_ch = pwdinfo->listen_channel; /* Listen channel number */ + + extra[wrqu->data.length] = 0x00; + listen_ch = rtw_atoi(extra); + + if ((listen_ch == 1) || (listen_ch == 6) || (listen_ch == 11)) { + pwdinfo->listen_channel = listen_ch; + set_channel_bwmode(padapter, pwdinfo->listen_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, CHANNEL_WIDTH_20); + } else + ret = -1; + + RTW_INFO("[%s] listen_ch = %d\n", __FUNCTION__, pwdinfo->listen_channel); + + return ret; + +} + +static int rtw_p2p_set_op_ch(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + /* Commented by Albert 20110524 + * This function is used to set the operating channel if the driver will become the group owner */ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + u8 op_ch = pwdinfo->operating_channel; /* Operating channel number */ + + extra[wrqu->data.length] = 0x00; + + op_ch = (u8) rtw_atoi(extra); + if (op_ch > 0) + pwdinfo->operating_channel = op_ch; + else + ret = -1; + + RTW_INFO("[%s] op_ch = %d\n", __FUNCTION__, pwdinfo->operating_channel); + + return ret; + +} + + +static int rtw_p2p_profilefound(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + /* Comment by Albert 2010/10/13 */ + /* Input data format: */ + /* Ex: 0 */ + /* Ex: 1XX:XX:XX:XX:XX:XXYYSSID */ + /* 0 => Reflush the profile record list. */ + /* 1 => Add the profile list */ + /* XX:XX:XX:XX:XX:XX => peer's MAC Address ( ex: 00:E0:4C:00:00:01 ) */ + /* YY => SSID Length */ + /* SSID => SSID for persistence group */ + + RTW_INFO("[%s] In value = %s, len = %d\n", __FUNCTION__, extra, wrqu->data.length - 1); + + + /* The upper application should pass the SSID to driver by using this rtw_p2p_profilefound function. */ + if (!rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) { + if (extra[0] == '0') { + /* Remove all the profile information of wifidirect_info structure. */ + _rtw_memset(&pwdinfo->profileinfo[0], 0x00, sizeof(struct profile_info) * P2P_MAX_PERSISTENT_GROUP_NUM); + pwdinfo->profileindex = 0; + } else { + if (pwdinfo->profileindex >= P2P_MAX_PERSISTENT_GROUP_NUM) + ret = -1; + else { + int jj, kk; + + /* Add this profile information into pwdinfo->profileinfo */ + /* Ex: 1XX:XX:XX:XX:XX:XXYYSSID */ + for (jj = 0, kk = 1; jj < ETH_ALEN; jj++, kk += 3) + pwdinfo->profileinfo[pwdinfo->profileindex].peermac[jj] = key_2char2num(extra[kk], extra[kk + 1]); + + /* pwdinfo->profileinfo[pwdinfo->profileindex].ssidlen = ( extra[18] - '0' ) * 10 + ( extra[19] - '0' ); */ + /* _rtw_memcpy( pwdinfo->profileinfo[pwdinfo->profileindex].ssid, &extra[20], pwdinfo->profileinfo[pwdinfo->profileindex].ssidlen ); */ + pwdinfo->profileindex++; + } + } + } + + return ret; + +} + +static int rtw_p2p_setDN(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + + RTW_INFO("[%s] %s %d\n", __FUNCTION__, extra, wrqu->data.length - 1); + _rtw_memset(pwdinfo->device_name, 0x00, WPS_MAX_DEVICE_NAME_LEN); + _rtw_memcpy(pwdinfo->device_name, extra, wrqu->data.length - 1); + pwdinfo->device_name_len = wrqu->data.length - 1; + + return ret; + +} + + +static int rtw_p2p_get_status(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + if (padapter->bShowGetP2PState) { + RTW_INFO("[%s] Role = %d, Status = %d, peer addr = %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n", __FUNCTION__, rtw_p2p_role(pwdinfo), rtw_p2p_state(pwdinfo), + pwdinfo->p2p_peer_interface_addr[0], pwdinfo->p2p_peer_interface_addr[1], pwdinfo->p2p_peer_interface_addr[2], + pwdinfo->p2p_peer_interface_addr[3], pwdinfo->p2p_peer_interface_addr[4], pwdinfo->p2p_peer_interface_addr[5]); + } + + /* Commented by Albert 2010/10/12 */ + /* Because of the output size limitation, I had removed the "Role" information. */ + /* About the "Role" information, we will use the new private IOCTL to get the "Role" information. */ + sprintf(extra, "\n\nStatus=%.2d\n", rtw_p2p_state(pwdinfo)); + wrqu->data.length = strlen(extra); + + return ret; + +} + +/* Commented by Albert 20110520 + * This function will return the config method description + * This config method description will show us which config method the remote P2P device is intented to use + * by sending the provisioning discovery request frame. */ + +static int rtw_p2p_get_req_cm(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + sprintf(extra, "\n\nCM=%s\n", pwdinfo->rx_prov_disc_info.strconfig_method_desc_of_prov_disc_req); + wrqu->data.length = strlen(extra); + return ret; + +} + + +static int rtw_p2p_get_role(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + RTW_INFO("[%s] Role = %d, Status = %d, peer addr = %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n", __FUNCTION__, rtw_p2p_role(pwdinfo), rtw_p2p_state(pwdinfo), + pwdinfo->p2p_peer_interface_addr[0], pwdinfo->p2p_peer_interface_addr[1], pwdinfo->p2p_peer_interface_addr[2], + pwdinfo->p2p_peer_interface_addr[3], pwdinfo->p2p_peer_interface_addr[4], pwdinfo->p2p_peer_interface_addr[5]); + + sprintf(extra, "\n\nRole=%.2d\n", rtw_p2p_role(pwdinfo)); + wrqu->data.length = strlen(extra); + return ret; + +} + + +static int rtw_p2p_get_peer_ifaddr(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + + RTW_INFO("[%s] Role = %d, Status = %d, peer addr = %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n", __FUNCTION__, rtw_p2p_role(pwdinfo), rtw_p2p_state(pwdinfo), + pwdinfo->p2p_peer_interface_addr[0], pwdinfo->p2p_peer_interface_addr[1], pwdinfo->p2p_peer_interface_addr[2], + pwdinfo->p2p_peer_interface_addr[3], pwdinfo->p2p_peer_interface_addr[4], pwdinfo->p2p_peer_interface_addr[5]); + + sprintf(extra, "\nMAC %.2X:%.2X:%.2X:%.2X:%.2X:%.2X", + pwdinfo->p2p_peer_interface_addr[0], pwdinfo->p2p_peer_interface_addr[1], pwdinfo->p2p_peer_interface_addr[2], + pwdinfo->p2p_peer_interface_addr[3], pwdinfo->p2p_peer_interface_addr[4], pwdinfo->p2p_peer_interface_addr[5]); + wrqu->data.length = strlen(extra); + return ret; + +} + +static int rtw_p2p_get_peer_devaddr(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) + +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + RTW_INFO("[%s] Role = %d, Status = %d, peer addr = %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n", __FUNCTION__, rtw_p2p_role(pwdinfo), rtw_p2p_state(pwdinfo), + pwdinfo->rx_prov_disc_info.peerDevAddr[0], pwdinfo->rx_prov_disc_info.peerDevAddr[1], + pwdinfo->rx_prov_disc_info.peerDevAddr[2], pwdinfo->rx_prov_disc_info.peerDevAddr[3], + pwdinfo->rx_prov_disc_info.peerDevAddr[4], pwdinfo->rx_prov_disc_info.peerDevAddr[5]); + sprintf(extra, "\n%.2X%.2X%.2X%.2X%.2X%.2X", + pwdinfo->rx_prov_disc_info.peerDevAddr[0], pwdinfo->rx_prov_disc_info.peerDevAddr[1], + pwdinfo->rx_prov_disc_info.peerDevAddr[2], pwdinfo->rx_prov_disc_info.peerDevAddr[3], + pwdinfo->rx_prov_disc_info.peerDevAddr[4], pwdinfo->rx_prov_disc_info.peerDevAddr[5]); + wrqu->data.length = strlen(extra); + return ret; + +} + +static int rtw_p2p_get_peer_devaddr_by_invitation(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) + +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + RTW_INFO("[%s] Role = %d, Status = %d, peer addr = %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n", __FUNCTION__, rtw_p2p_role(pwdinfo), rtw_p2p_state(pwdinfo), + pwdinfo->p2p_peer_device_addr[0], pwdinfo->p2p_peer_device_addr[1], + pwdinfo->p2p_peer_device_addr[2], pwdinfo->p2p_peer_device_addr[3], + pwdinfo->p2p_peer_device_addr[4], pwdinfo->p2p_peer_device_addr[5]); + sprintf(extra, "\nMAC %.2X:%.2X:%.2X:%.2X:%.2X:%.2X", + pwdinfo->p2p_peer_device_addr[0], pwdinfo->p2p_peer_device_addr[1], + pwdinfo->p2p_peer_device_addr[2], pwdinfo->p2p_peer_device_addr[3], + pwdinfo->p2p_peer_device_addr[4], pwdinfo->p2p_peer_device_addr[5]); + wrqu->data.length = strlen(extra); + return ret; + +} + +static int rtw_p2p_get_groupid(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) + +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + sprintf(extra, "\n%.2X:%.2X:%.2X:%.2X:%.2X:%.2X %s", + pwdinfo->groupid_info.go_device_addr[0], pwdinfo->groupid_info.go_device_addr[1], + pwdinfo->groupid_info.go_device_addr[2], pwdinfo->groupid_info.go_device_addr[3], + pwdinfo->groupid_info.go_device_addr[4], pwdinfo->groupid_info.go_device_addr[5], + pwdinfo->groupid_info.ssid); + wrqu->data.length = strlen(extra); + return ret; + +} + +static int rtw_p2p_get_op_ch(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) + +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + + RTW_INFO("[%s] Op_ch = %02x\n", __FUNCTION__, pwdinfo->operating_channel); + + sprintf(extra, "\n\nOp_ch=%.2d\n", pwdinfo->operating_channel); + wrqu->data.length = strlen(extra); + return ret; + +} + +static int rtw_p2p_get_wps_configmethod(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra, char *subcmd) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + u8 peerMAC[ETH_ALEN] = { 0x00 }; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + _irqL irqL; + _list *plist, *phead; + _queue *queue = &(pmlmepriv->scanned_queue); + struct wlan_network *pnetwork = NULL; + u8 blnMatch = 0; + u16 attr_content = 0; + uint attr_contentlen = 0; + u8 attr_content_str[P2P_PRIVATE_IOCTL_SET_LEN] = { 0x00 }; + + /* Commented by Albert 20110727 */ + /* The input data is the MAC address which the application wants to know its WPS config method. */ + /* After knowing its WPS config method, the application can decide the config method for provisioning discovery. */ + /* Format: iwpriv wlanx p2p_get_wpsCM 00:E0:4C:00:00:05 */ + + RTW_INFO("[%s] data = %s\n", __FUNCTION__, subcmd); + + macstr2num(peerMAC, subcmd); + + _enter_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + phead = get_list_head(queue); + plist = get_next(phead); + + while (1) { + if (rtw_end_of_queue_search(phead, plist) == _TRUE) + break; + + pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list); + if (_rtw_memcmp(pnetwork->network.MacAddress, peerMAC, ETH_ALEN)) { + u8 *wpsie; + uint wpsie_len = 0; + + /* The mac address is matched. */ + + wpsie = rtw_get_wps_ie_from_scan_queue(&pnetwork->network.IEs[0], pnetwork->network.IELength, NULL, &wpsie_len, pnetwork->network.Reserved[0]); + if (wpsie) { + rtw_get_wps_attr_content(wpsie, wpsie_len, WPS_ATTR_CONF_METHOD, (u8 *)&attr_content, &attr_contentlen); + if (attr_contentlen) { + attr_content = be16_to_cpu(attr_content); + sprintf(attr_content_str, "\n\nM=%.4d", attr_content); + blnMatch = 1; + } + } + + break; + } + + plist = get_next(plist); + + } + + _exit_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + if (!blnMatch) + sprintf(attr_content_str, "\n\nM=0000"); + + wrqu->data.length = strlen(attr_content_str); + _rtw_memcpy(extra, attr_content_str, wrqu->data.length); + + return ret; + +} + +#ifdef CONFIG_WFD +static int rtw_p2p_get_peer_wfd_port(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + RTW_INFO("[%s] p2p_state = %d\n", __FUNCTION__, rtw_p2p_state(pwdinfo)); + + sprintf(extra, "\n\nPort=%d\n", pwdinfo->wfd_info->peer_rtsp_ctrlport); + RTW_INFO("[%s] remote port = %d\n", __FUNCTION__, pwdinfo->wfd_info->peer_rtsp_ctrlport); + + wrqu->data.length = strlen(extra); + return ret; + +} + +static int rtw_p2p_get_peer_wfd_preferred_connection(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + sprintf(extra, "\n\nwfd_pc=%d\n", pwdinfo->wfd_info->wfd_pc); + RTW_INFO("[%s] wfd_pc = %d\n", __FUNCTION__, pwdinfo->wfd_info->wfd_pc); + + wrqu->data.length = strlen(extra); + pwdinfo->wfd_info->wfd_pc = _FALSE; /* Reset the WFD preferred connection to P2P */ + return ret; + +} + +static int rtw_p2p_get_peer_wfd_session_available(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + sprintf(extra, "\n\nwfd_sa=%d\n", pwdinfo->wfd_info->peer_session_avail); + RTW_INFO("[%s] wfd_sa = %d\n", __FUNCTION__, pwdinfo->wfd_info->peer_session_avail); + + wrqu->data.length = strlen(extra); + pwdinfo->wfd_info->peer_session_avail = _TRUE; /* Reset the WFD session available */ + return ret; + +} +#endif /* CONFIG_WFD */ + +static int rtw_p2p_get_go_device_address(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra, char *subcmd) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + u8 peerMAC[ETH_ALEN] = { 0x00 }; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + _irqL irqL; + _list *plist, *phead; + _queue *queue = &(pmlmepriv->scanned_queue); + struct wlan_network *pnetwork = NULL; + u8 blnMatch = 0; + u8 *p2pie; + uint p2pielen = 0, attr_contentlen = 0; + u8 attr_content[100] = { 0x00 }; + u8 go_devadd_str[P2P_PRIVATE_IOCTL_SET_LEN] = { 0x00 }; + + /* Commented by Albert 20121209 */ + /* The input data is the GO's interface address which the application wants to know its device address. */ + /* Format: iwpriv wlanx p2p_get2 go_devadd=00:E0:4C:00:00:05 */ + + RTW_INFO("[%s] data = %s\n", __FUNCTION__, subcmd); + + macstr2num(peerMAC, subcmd); + + _enter_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + phead = get_list_head(queue); + plist = get_next(phead); + + while (1) { + if (rtw_end_of_queue_search(phead, plist) == _TRUE) + break; + + pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list); + if (_rtw_memcmp(pnetwork->network.MacAddress, peerMAC, ETH_ALEN)) { + /* Commented by Albert 2011/05/18 */ + /* Match the device address located in the P2P IE */ + /* This is for the case that the P2P device address is not the same as the P2P interface address. */ + + p2pie = rtw_bss_ex_get_p2p_ie(&pnetwork->network, NULL, &p2pielen); + if (p2pie) { + while (p2pie) { + /* The P2P Device ID attribute is included in the Beacon frame. */ + /* The P2P Device Info attribute is included in the probe response frame. */ + + _rtw_memset(attr_content, 0x00, 100); + if (rtw_get_p2p_attr_content(p2pie, p2pielen, P2P_ATTR_DEVICE_ID, attr_content, &attr_contentlen)) { + /* Handle the P2P Device ID attribute of Beacon first */ + blnMatch = 1; + break; + + } else if (rtw_get_p2p_attr_content(p2pie, p2pielen, P2P_ATTR_DEVICE_INFO, attr_content, &attr_contentlen)) { + /* Handle the P2P Device Info attribute of probe response */ + blnMatch = 1; + break; + } + + /* Get the next P2P IE */ + p2pie = rtw_get_p2p_ie(p2pie + p2pielen, BSS_EX_TLV_IES_LEN(&pnetwork->network) - (p2pie + p2pielen - BSS_EX_TLV_IES(&pnetwork->network)), NULL, &p2pielen); + } + } + } + + plist = get_next(plist); + + } + + _exit_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + if (!blnMatch) + sprintf(go_devadd_str, "\n\ndev_add=NULL"); + else { + sprintf(go_devadd_str, "\n\ndev_add=%.2X:%.2X:%.2X:%.2X:%.2X:%.2X", + attr_content[0], attr_content[1], attr_content[2], attr_content[3], attr_content[4], attr_content[5]); + } + + wrqu->data.length = strlen(go_devadd_str); + _rtw_memcpy(extra, go_devadd_str, wrqu->data.length); + + return ret; + +} + +static int rtw_p2p_get_device_type(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra, char *subcmd) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + u8 peerMAC[ETH_ALEN] = { 0x00 }; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + _irqL irqL; + _list *plist, *phead; + _queue *queue = &(pmlmepriv->scanned_queue); + struct wlan_network *pnetwork = NULL; + u8 blnMatch = 0; + u8 dev_type[8] = { 0x00 }; + uint dev_type_len = 0; + u8 dev_type_str[P2P_PRIVATE_IOCTL_SET_LEN] = { 0x00 }; /* +9 is for the str "dev_type=", we have to clear it at wrqu->data.pointer */ + + /* Commented by Albert 20121209 */ + /* The input data is the MAC address which the application wants to know its device type. */ + /* Such user interface could know the device type. */ + /* Format: iwpriv wlanx p2p_get2 dev_type=00:E0:4C:00:00:05 */ + + RTW_INFO("[%s] data = %s\n", __FUNCTION__, subcmd); + + macstr2num(peerMAC, subcmd); + + _enter_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + phead = get_list_head(queue); + plist = get_next(phead); + + while (1) { + if (rtw_end_of_queue_search(phead, plist) == _TRUE) + break; + + pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list); + if (_rtw_memcmp(pnetwork->network.MacAddress, peerMAC, ETH_ALEN)) { + u8 *wpsie; + uint wpsie_len = 0; + + /* The mac address is matched. */ + + wpsie = rtw_get_wps_ie_from_scan_queue(&pnetwork->network.IEs[0], pnetwork->network.IELength, NULL, &wpsie_len, pnetwork->network.Reserved[0]); + if (wpsie) { + rtw_get_wps_attr_content(wpsie, wpsie_len, WPS_ATTR_PRIMARY_DEV_TYPE, dev_type, &dev_type_len); + if (dev_type_len) { + u16 type = 0; + + _rtw_memcpy(&type, dev_type, 2); + type = be16_to_cpu(type); + sprintf(dev_type_str, "\n\nN=%.2d", type); + blnMatch = 1; + } + } + break; + } + + plist = get_next(plist); + + } + + _exit_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + if (!blnMatch) + sprintf(dev_type_str, "\n\nN=00"); + + wrqu->data.length = strlen(dev_type_str); + _rtw_memcpy(extra, dev_type_str, wrqu->data.length); + + return ret; + +} + +static int rtw_p2p_get_device_name(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra, char *subcmd) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + u8 peerMAC[ETH_ALEN] = { 0x00 }; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + _irqL irqL; + _list *plist, *phead; + _queue *queue = &(pmlmepriv->scanned_queue); + struct wlan_network *pnetwork = NULL; + u8 blnMatch = 0; + u8 dev_name[WPS_MAX_DEVICE_NAME_LEN] = { 0x00 }; + uint dev_len = 0; + u8 dev_name_str[P2P_PRIVATE_IOCTL_SET_LEN] = { 0x00 }; + + /* Commented by Albert 20121225 */ + /* The input data is the MAC address which the application wants to know its device name. */ + /* Such user interface could show peer device's device name instead of ssid. */ + /* Format: iwpriv wlanx p2p_get2 devN=00:E0:4C:00:00:05 */ + + RTW_INFO("[%s] data = %s\n", __FUNCTION__, subcmd); + + macstr2num(peerMAC, subcmd); + + _enter_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + phead = get_list_head(queue); + plist = get_next(phead); + + while (1) { + if (rtw_end_of_queue_search(phead, plist) == _TRUE) + break; + + pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list); + if (_rtw_memcmp(pnetwork->network.MacAddress, peerMAC, ETH_ALEN)) { + u8 *wpsie; + uint wpsie_len = 0; + + /* The mac address is matched. */ + + wpsie = rtw_get_wps_ie_from_scan_queue(&pnetwork->network.IEs[0], pnetwork->network.IELength, NULL, &wpsie_len, pnetwork->network.Reserved[0]); + if (wpsie) { + rtw_get_wps_attr_content(wpsie, wpsie_len, WPS_ATTR_DEVICE_NAME, dev_name, &dev_len); + if (dev_len) { + sprintf(dev_name_str, "\n\nN=%s", dev_name); + blnMatch = 1; + } + } + break; + } + + plist = get_next(plist); + + } + + _exit_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + if (!blnMatch) + sprintf(dev_name_str, "\n\nN=0000"); + + wrqu->data.length = strlen(dev_name_str); + _rtw_memcpy(extra, dev_name_str, wrqu->data.length); + + return ret; + +} + +static int rtw_p2p_get_invitation_procedure(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra, char *subcmd) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + u8 peerMAC[ETH_ALEN] = { 0x00 }; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + _irqL irqL; + _list *plist, *phead; + _queue *queue = &(pmlmepriv->scanned_queue); + struct wlan_network *pnetwork = NULL; + u8 blnMatch = 0; + u8 *p2pie; + uint p2pielen = 0, attr_contentlen = 0; + u8 attr_content[2] = { 0x00 }; + u8 inv_proc_str[P2P_PRIVATE_IOCTL_SET_LEN] = { 0x00 }; + + /* Commented by Ouden 20121226 */ + /* The application wants to know P2P initation procedure is support or not. */ + /* Format: iwpriv wlanx p2p_get2 InvProc=00:E0:4C:00:00:05 */ + + RTW_INFO("[%s] data = %s\n", __FUNCTION__, subcmd); + + macstr2num(peerMAC, subcmd); + + _enter_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + phead = get_list_head(queue); + plist = get_next(phead); + + while (1) { + if (rtw_end_of_queue_search(phead, plist) == _TRUE) + break; + + pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list); + if (_rtw_memcmp(pnetwork->network.MacAddress, peerMAC, ETH_ALEN)) { + /* Commented by Albert 20121226 */ + /* Match the device address located in the P2P IE */ + /* This is for the case that the P2P device address is not the same as the P2P interface address. */ + + p2pie = rtw_bss_ex_get_p2p_ie(&pnetwork->network, NULL, &p2pielen); + if (p2pie) { + while (p2pie) { + /* _rtw_memset( attr_content, 0x00, 2); */ + if (rtw_get_p2p_attr_content(p2pie, p2pielen, P2P_ATTR_CAPABILITY, attr_content, &attr_contentlen)) { + /* Handle the P2P capability attribute */ + blnMatch = 1; + break; + + } + + /* Get the next P2P IE */ + p2pie = rtw_get_p2p_ie(p2pie + p2pielen, BSS_EX_TLV_IES_LEN(&pnetwork->network) - (p2pie + p2pielen - BSS_EX_TLV_IES(&pnetwork->network)), NULL, &p2pielen); + } + } + } + + plist = get_next(plist); + + } + + _exit_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + if (!blnMatch) + sprintf(inv_proc_str, "\nIP=-1"); + else { + if ((attr_content[0] & 0x20) == 0x20) + sprintf(inv_proc_str, "\nIP=1"); + else + sprintf(inv_proc_str, "\nIP=0"); + } + + wrqu->data.length = strlen(inv_proc_str); + _rtw_memcpy(extra, inv_proc_str, wrqu->data.length); + + return ret; + +} + +static int rtw_p2p_connect(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + u8 peerMAC[ETH_ALEN] = { 0x00 }; + int jj, kk; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + _irqL irqL; + _list *plist, *phead; + _queue *queue = &(pmlmepriv->scanned_queue); + struct wlan_network *pnetwork = NULL; + uint uintPeerChannel = 0; +#ifdef CONFIG_CONCURRENT_MODE + struct roch_info *prochinfo = &padapter->rochinfo; +#endif + + /* Commented by Albert 20110304 */ + /* The input data contains two informations. */ + /* 1. First information is the MAC address which wants to formate with */ + /* 2. Second information is the WPS PINCode or "pbc" string for push button method */ + /* Format: 00:E0:4C:00:00:05 */ + /* Format: 00:E0:4C:00:00:05 */ + + RTW_INFO("[%s] data = %s\n", __FUNCTION__, extra); + + if (pwdinfo->p2p_state == P2P_STATE_NONE) { + RTW_INFO("[%s] WiFi Direct is disable!\n", __FUNCTION__); + return ret; + } + + if (pwdinfo->ui_got_wps_info == P2P_NO_WPSINFO) + return -1; + + for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 3) + peerMAC[jj] = key_2char2num(extra[kk], extra[kk + 1]); + + _enter_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + phead = get_list_head(queue); + plist = get_next(phead); + + while (1) { + if (rtw_end_of_queue_search(phead, plist) == _TRUE) + break; + + pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list); + if (_rtw_memcmp(pnetwork->network.MacAddress, peerMAC, ETH_ALEN)) { + if (pnetwork->network.Configuration.DSConfig != 0) + uintPeerChannel = pnetwork->network.Configuration.DSConfig; + else if (pwdinfo->nego_req_info.peer_ch != 0) + uintPeerChannel = pnetwork->network.Configuration.DSConfig = pwdinfo->nego_req_info.peer_ch; + else { + /* Unexpected case */ + uintPeerChannel = 0; + RTW_INFO("%s uintPeerChannel = 0\n", __func__); + } + break; + } + + plist = get_next(plist); + + } + + _exit_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + if (uintPeerChannel) { +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_check_status(padapter, MI_LINKED)) + _cancel_timer_ex(&prochinfo->ap_roch_ch_switch_timer); +#endif /* CONFIG_CONCURRENT_MODE */ + + _rtw_memset(&pwdinfo->nego_req_info, 0x00, sizeof(struct tx_nego_req_info)); + _rtw_memset(&pwdinfo->groupid_info, 0x00, sizeof(struct group_id_info)); + + pwdinfo->nego_req_info.peer_channel_num[0] = uintPeerChannel; + _rtw_memcpy(pwdinfo->nego_req_info.peerDevAddr, pnetwork->network.MacAddress, ETH_ALEN); + pwdinfo->nego_req_info.benable = _TRUE; + + _cancel_timer_ex(&pwdinfo->restore_p2p_state_timer); + if (rtw_p2p_state(pwdinfo) != P2P_STATE_GONEGO_OK) { + /* Restore to the listen state if the current p2p state is not nego OK */ + rtw_p2p_set_state(pwdinfo, P2P_STATE_LISTEN); + } + + rtw_p2p_set_pre_state(pwdinfo, rtw_p2p_state(pwdinfo)); + rtw_p2p_set_state(pwdinfo, P2P_STATE_GONEGO_ING); + +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_check_status(padapter, MI_LINKED)) { + u8 union_ch = rtw_mi_get_union_chan(padapter); + u8 union_bw = rtw_mi_get_union_bw(padapter); + u8 union_offset = rtw_mi_get_union_offset(padapter); + + set_channel_bwmode(padapter, union_ch, union_offset, union_bw); + rtw_leave_opch(padapter); + } +#endif /* CONFIG_CONCURRENT_MODE */ + + RTW_INFO("[%s] Start PreTx Procedure!\n", __FUNCTION__); + _set_timer(&pwdinfo->pre_tx_scan_timer, P2P_TX_PRESCAN_TIMEOUT); +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_check_status(padapter, MI_LINKED)) + _set_timer(&pwdinfo->restore_p2p_state_timer, P2P_CONCURRENT_GO_NEGO_TIMEOUT); + else + _set_timer(&pwdinfo->restore_p2p_state_timer, P2P_GO_NEGO_TIMEOUT); +#else + _set_timer(&pwdinfo->restore_p2p_state_timer, P2P_GO_NEGO_TIMEOUT); +#endif /* CONFIG_CONCURRENT_MODE */ + + } else { + RTW_INFO("[%s] Not Found in Scanning Queue~\n", __FUNCTION__); + ret = -1; + } + + return ret; +} + +static int rtw_p2p_invite_req(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + int jj, kk; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + _list *plist, *phead; + _queue *queue = &(pmlmepriv->scanned_queue); + struct wlan_network *pnetwork = NULL; + uint uintPeerChannel = 0; + u8 attr_content[50] = { 0x00 }; + u8 *p2pie; + uint p2pielen = 0, attr_contentlen = 0; + _irqL irqL; + struct tx_invite_req_info *pinvite_req_info = &pwdinfo->invitereq_info; +#ifdef CONFIG_CONCURRENT_MODE + struct roch_info *prochinfo = &padapter->rochinfo; +#endif + + /* Commented by Albert 20120321 */ + /* The input data contains two informations. */ + /* 1. First information is the P2P device address which you want to send to. */ + /* 2. Second information is the group id which combines with GO's mac address, space and GO's ssid. */ + /* Command line sample: iwpriv wlan0 p2p_set invite="00:11:22:33:44:55 00:E0:4C:00:00:05 DIRECT-xy" */ + /* Format: 00:11:22:33:44:55 00:E0:4C:00:00:05 DIRECT-xy */ + + RTW_INFO("[%s] data = %s\n", __FUNCTION__, extra); + + if (wrqu->data.length <= 37) { + RTW_INFO("[%s] Wrong format!\n", __FUNCTION__); + return ret; + } + + if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) { + RTW_INFO("[%s] WiFi Direct is disable!\n", __FUNCTION__); + return ret; + } else { + /* Reset the content of struct tx_invite_req_info */ + pinvite_req_info->benable = _FALSE; + _rtw_memset(pinvite_req_info->go_bssid, 0x00, ETH_ALEN); + _rtw_memset(pinvite_req_info->go_ssid, 0x00, WLAN_SSID_MAXLEN); + pinvite_req_info->ssidlen = 0x00; + pinvite_req_info->operating_ch = pwdinfo->operating_channel; + _rtw_memset(pinvite_req_info->peer_macaddr, 0x00, ETH_ALEN); + pinvite_req_info->token = 3; + } + + for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 3) + pinvite_req_info->peer_macaddr[jj] = key_2char2num(extra[kk], extra[kk + 1]); + + _enter_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + phead = get_list_head(queue); + plist = get_next(phead); + + while (1) { + if (rtw_end_of_queue_search(phead, plist) == _TRUE) + break; + + pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list); + + /* Commented by Albert 2011/05/18 */ + /* Match the device address located in the P2P IE */ + /* This is for the case that the P2P device address is not the same as the P2P interface address. */ + + p2pie = rtw_bss_ex_get_p2p_ie(&pnetwork->network, NULL, &p2pielen); + if (p2pie) { + /* The P2P Device ID attribute is included in the Beacon frame. */ + /* The P2P Device Info attribute is included in the probe response frame. */ + + if (rtw_get_p2p_attr_content(p2pie, p2pielen, P2P_ATTR_DEVICE_ID, attr_content, &attr_contentlen)) { + /* Handle the P2P Device ID attribute of Beacon first */ + if (_rtw_memcmp(attr_content, pinvite_req_info->peer_macaddr, ETH_ALEN)) { + uintPeerChannel = pnetwork->network.Configuration.DSConfig; + break; + } + } else if (rtw_get_p2p_attr_content(p2pie, p2pielen, P2P_ATTR_DEVICE_INFO, attr_content, &attr_contentlen)) { + /* Handle the P2P Device Info attribute of probe response */ + if (_rtw_memcmp(attr_content, pinvite_req_info->peer_macaddr, ETH_ALEN)) { + uintPeerChannel = pnetwork->network.Configuration.DSConfig; + break; + } + } + + } + + plist = get_next(plist); + + } + + _exit_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + +#ifdef CONFIG_WFD + if (hal_chk_wl_func(padapter, WL_FUNC_MIRACAST) && uintPeerChannel) { + struct wifi_display_info *pwfd_info = pwdinfo->wfd_info; + u8 *wfd_ie; + uint wfd_ielen = 0; + + wfd_ie = rtw_bss_ex_get_wfd_ie(&pnetwork->network, NULL, &wfd_ielen); + if (wfd_ie) { + u8 *wfd_devinfo; + uint wfd_devlen; + + RTW_INFO("[%s] Found WFD IE!\n", __FUNCTION__); + wfd_devinfo = rtw_get_wfd_attr_content(wfd_ie, wfd_ielen, WFD_ATTR_DEVICE_INFO, NULL, &wfd_devlen); + if (wfd_devinfo) { + u16 wfd_devinfo_field = 0; + + /* Commented by Albert 20120319 */ + /* The first two bytes are the WFD device information field of WFD device information subelement. */ + /* In big endian format. */ + wfd_devinfo_field = RTW_GET_BE16(wfd_devinfo); + if (wfd_devinfo_field & WFD_DEVINFO_SESSION_AVAIL) + pwfd_info->peer_session_avail = _TRUE; + else + pwfd_info->peer_session_avail = _FALSE; + } + } + + if (_FALSE == pwfd_info->peer_session_avail) { + RTW_INFO("[%s] WFD Session not avaiable!\n", __FUNCTION__); + goto exit; + } + } +#endif /* CONFIG_WFD */ + + if (uintPeerChannel) { +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_check_status(padapter, MI_LINKED)) + _cancel_timer_ex(&prochinfo->ap_roch_ch_switch_timer); +#endif /* CONFIG_CONCURRENT_MODE */ + + /* Store the GO's bssid */ + for (jj = 0, kk = 18; jj < ETH_ALEN; jj++, kk += 3) + pinvite_req_info->go_bssid[jj] = key_2char2num(extra[kk], extra[kk + 1]); + + /* Store the GO's ssid */ + pinvite_req_info->ssidlen = wrqu->data.length - 36; + _rtw_memcpy(pinvite_req_info->go_ssid, &extra[36], (u32) pinvite_req_info->ssidlen); + pinvite_req_info->benable = _TRUE; + pinvite_req_info->peer_ch = uintPeerChannel; + + rtw_p2p_set_pre_state(pwdinfo, rtw_p2p_state(pwdinfo)); + rtw_p2p_set_state(pwdinfo, P2P_STATE_TX_INVITE_REQ); + +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_check_status(padapter, MI_LINKED)) { + u8 union_ch = rtw_mi_get_union_chan(padapter); + u8 union_bw = rtw_mi_get_union_bw(padapter); + u8 union_offset = rtw_mi_get_union_offset(padapter); + + set_channel_bwmode(padapter, union_ch, union_offset, union_bw); + rtw_leave_opch(padapter); + + } else + set_channel_bwmode(padapter, uintPeerChannel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, CHANNEL_WIDTH_20); +#else + set_channel_bwmode(padapter, uintPeerChannel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, CHANNEL_WIDTH_20); +#endif/*CONFIG_CONCURRENT_MODE*/ + + _set_timer(&pwdinfo->pre_tx_scan_timer, P2P_TX_PRESCAN_TIMEOUT); + +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_check_status(padapter, MI_LINKED)) + _set_timer(&pwdinfo->restore_p2p_state_timer, P2P_CONCURRENT_INVITE_TIMEOUT); + else + _set_timer(&pwdinfo->restore_p2p_state_timer, P2P_INVITE_TIMEOUT); +#else + _set_timer(&pwdinfo->restore_p2p_state_timer, P2P_INVITE_TIMEOUT); +#endif /* CONFIG_CONCURRENT_MODE */ + + + } else + RTW_INFO("[%s] NOT Found in the Scanning Queue!\n", __FUNCTION__); +exit: + + return ret; + +} + +static int rtw_p2p_set_persistent(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + /* Commented by Albert 20120328 */ + /* The input data is 0 or 1 */ + /* 0: disable persistent group functionality */ + /* 1: enable persistent group founctionality */ + + RTW_INFO("[%s] data = %s\n", __FUNCTION__, extra); + + if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) { + RTW_INFO("[%s] WiFi Direct is disable!\n", __FUNCTION__); + return ret; + } else { + if (extra[0] == '0') /* Disable the persistent group function. */ + pwdinfo->persistent_supported = _FALSE; + else if (extra[0] == '1') /* Enable the persistent group function. */ + pwdinfo->persistent_supported = _TRUE; + else + pwdinfo->persistent_supported = _FALSE; + } + printk("[%s] persistent_supported = %d\n", __FUNCTION__, pwdinfo->persistent_supported); + + return ret; + +} + +static int uuid_str2bin(const char *str, u8 *bin) +{ + const char *pos; + u8 *opos; + + pos = str; + opos = bin; + + if (hexstr2bin(pos, opos, 4)) + return -1; + pos += 8; + opos += 4; + + if (*pos++ != '-' || hexstr2bin(pos, opos, 2)) + return -1; + pos += 4; + opos += 2; + + if (*pos++ != '-' || hexstr2bin(pos, opos, 2)) + return -1; + pos += 4; + opos += 2; + + if (*pos++ != '-' || hexstr2bin(pos, opos, 2)) + return -1; + pos += 4; + opos += 2; + + if (*pos++ != '-' || hexstr2bin(pos, opos, 6)) + return -1; + + return 0; +} + +static int rtw_p2p_set_wps_uuid(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + RTW_INFO("[%s] data = %s\n", __FUNCTION__, extra); + + if ((36 == strlen(extra)) && (uuid_str2bin(extra, pwdinfo->uuid) == 0)) + pwdinfo->external_uuid = 1; + else { + pwdinfo->external_uuid = 0; + ret = -EINVAL; + } + + return ret; + +} +#ifdef CONFIG_WFD +static int rtw_p2p_set_pc(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + u8 peerMAC[ETH_ALEN] = { 0x00 }; + int jj, kk; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + _list *plist, *phead; + _queue *queue = &(pmlmepriv->scanned_queue); + struct wlan_network *pnetwork = NULL; + u8 attr_content[50] = { 0x00 }; + u8 *p2pie; + uint p2pielen = 0, attr_contentlen = 0; + _irqL irqL; + uint uintPeerChannel = 0; + + struct wifi_display_info *pwfd_info = pwdinfo->wfd_info; + + /* Commented by Albert 20120512 */ + /* 1. Input information is the MAC address which wants to know the Preferred Connection bit (PC bit) */ + /* Format: 00:E0:4C:00:00:05 */ + + RTW_INFO("[%s] data = %s\n", __FUNCTION__, extra); + + if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) { + RTW_INFO("[%s] WiFi Direct is disable!\n", __FUNCTION__); + return ret; + } + + for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 3) + peerMAC[jj] = key_2char2num(extra[kk], extra[kk + 1]); + + _enter_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + phead = get_list_head(queue); + plist = get_next(phead); + + while (1) { + if (rtw_end_of_queue_search(phead, plist) == _TRUE) + break; + + pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list); + + /* Commented by Albert 2011/05/18 */ + /* Match the device address located in the P2P IE */ + /* This is for the case that the P2P device address is not the same as the P2P interface address. */ + + p2pie = rtw_bss_ex_get_p2p_ie(&pnetwork->network, NULL, &p2pielen); + if (p2pie) { + /* The P2P Device ID attribute is included in the Beacon frame. */ + /* The P2P Device Info attribute is included in the probe response frame. */ + printk("[%s] Got P2P IE\n", __FUNCTION__); + if (rtw_get_p2p_attr_content(p2pie, p2pielen, P2P_ATTR_DEVICE_ID, attr_content, &attr_contentlen)) { + /* Handle the P2P Device ID attribute of Beacon first */ + printk("[%s] P2P_ATTR_DEVICE_ID\n", __FUNCTION__); + if (_rtw_memcmp(attr_content, peerMAC, ETH_ALEN)) { + uintPeerChannel = pnetwork->network.Configuration.DSConfig; + break; + } + } else if (rtw_get_p2p_attr_content(p2pie, p2pielen, P2P_ATTR_DEVICE_INFO, attr_content, &attr_contentlen)) { + /* Handle the P2P Device Info attribute of probe response */ + printk("[%s] P2P_ATTR_DEVICE_INFO\n", __FUNCTION__); + if (_rtw_memcmp(attr_content, peerMAC, ETH_ALEN)) { + uintPeerChannel = pnetwork->network.Configuration.DSConfig; + break; + } + } + + } + + plist = get_next(plist); + + } + + _exit_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + printk("[%s] channel = %d\n", __FUNCTION__, uintPeerChannel); + + if (uintPeerChannel) { + u8 *wfd_ie; + uint wfd_ielen = 0; + + wfd_ie = rtw_bss_ex_get_wfd_ie(&pnetwork->network, NULL, &wfd_ielen); + if (wfd_ie) { + u8 *wfd_devinfo; + uint wfd_devlen; + + RTW_INFO("[%s] Found WFD IE!\n", __FUNCTION__); + wfd_devinfo = rtw_get_wfd_attr_content(wfd_ie, wfd_ielen, WFD_ATTR_DEVICE_INFO, NULL, &wfd_devlen); + if (wfd_devinfo) { + u16 wfd_devinfo_field = 0; + + /* Commented by Albert 20120319 */ + /* The first two bytes are the WFD device information field of WFD device information subelement. */ + /* In big endian format. */ + wfd_devinfo_field = RTW_GET_BE16(wfd_devinfo); + if (wfd_devinfo_field & WFD_DEVINFO_PC_TDLS) + pwfd_info->wfd_pc = _TRUE; + else + pwfd_info->wfd_pc = _FALSE; + } + } + } else + RTW_INFO("[%s] NOT Found in the Scanning Queue!\n", __FUNCTION__); + + return ret; + +} + +static int rtw_p2p_set_wfd_device_type(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + struct wifi_display_info *pwfd_info = pwdinfo->wfd_info; + + /* Commented by Albert 20120328 */ + /* The input data is 0 or 1 */ + /* 0: specify to Miracast source device */ + /* 1 or others: specify to Miracast sink device (display device) */ + + RTW_INFO("[%s] data = %s\n", __FUNCTION__, extra); + + if (extra[0] == '0') /* Set to Miracast source device. */ + pwfd_info->wfd_device_type = WFD_DEVINFO_SOURCE; + else /* Set to Miracast sink device. */ + pwfd_info->wfd_device_type = WFD_DEVINFO_PSINK; + + return ret; + +} + +static int rtw_p2p_set_wfd_enable(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + /* Commented by Kurt 20121206 + * This function is used to set wfd enabled */ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + if (*extra == '0') + rtw_wfd_enable(padapter, 0); + else if (*extra == '1') + rtw_wfd_enable(padapter, 1); + + RTW_INFO("[%s] wfd_enable = %d\n", __FUNCTION__, pwdinfo->wfd_info->wfd_enable); + + return ret; + +} + +static int rtw_p2p_set_driver_iface(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + /* Commented by Kurt 20121206 + * This function is used to set driver iface is WEXT or CFG80211 */ + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + if (*extra == '1') { + pwdinfo->driver_interface = DRIVER_WEXT; + RTW_INFO("[%s] driver_interface = WEXT\n", __FUNCTION__); + } else if (*extra == '2') { + pwdinfo->driver_interface = DRIVER_CFG80211; + RTW_INFO("[%s] driver_interface = CFG80211\n", __FUNCTION__); + } + + return ret; + +} + +/* To set the WFD session available to enable or disable */ +static int rtw_p2p_set_sa(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + RTW_INFO("[%s] data = %s\n", __FUNCTION__, extra); + + if (0) { + RTW_INFO("[%s] WiFi Direct is disable!\n", __FUNCTION__); + return ret; + } else { + if (extra[0] == '0') /* Disable the session available. */ + pwdinfo->session_available = _FALSE; + else if (extra[0] == '1') /* Enable the session available. */ + pwdinfo->session_available = _TRUE; + else + pwdinfo->session_available = _FALSE; + } + printk("[%s] session available = %d\n", __FUNCTION__, pwdinfo->session_available); + + return ret; + +} +#endif /* CONFIG_WFD */ + +static int rtw_p2p_prov_disc(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + u8 peerMAC[ETH_ALEN] = { 0x00 }; + int jj, kk; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + _list *plist, *phead; + _queue *queue = &(pmlmepriv->scanned_queue); + struct wlan_network *pnetwork = NULL; + uint uintPeerChannel = 0; + u8 attr_content[100] = { 0x00 }; + u8 *p2pie; + uint p2pielen = 0, attr_contentlen = 0; + _irqL irqL; +#ifdef CONFIG_CONCURRENT_MODE + struct roch_info *prochinfo = &padapter->rochinfo; +#endif + + /* Commented by Albert 20110301 */ + /* The input data contains two informations. */ + /* 1. First information is the MAC address which wants to issue the provisioning discovery request frame. */ + /* 2. Second information is the WPS configuration method which wants to discovery */ + /* Format: 00:E0:4C:00:00:05_display */ + /* Format: 00:E0:4C:00:00:05_keypad */ + /* Format: 00:E0:4C:00:00:05_pbc */ + /* Format: 00:E0:4C:00:00:05_label */ + + RTW_INFO("[%s] data = %s\n", __FUNCTION__, extra); + + if (pwdinfo->p2p_state == P2P_STATE_NONE) { + RTW_INFO("[%s] WiFi Direct is disable!\n", __FUNCTION__); + return ret; + } else { + /* Reset the content of struct tx_provdisc_req_info excluded the wps_config_method_request. */ + _rtw_memset(pwdinfo->tx_prov_disc_info.peerDevAddr, 0x00, ETH_ALEN); + _rtw_memset(pwdinfo->tx_prov_disc_info.peerIFAddr, 0x00, ETH_ALEN); + _rtw_memset(&pwdinfo->tx_prov_disc_info.ssid, 0x00, sizeof(NDIS_802_11_SSID)); + pwdinfo->tx_prov_disc_info.peer_channel_num[0] = 0; + pwdinfo->tx_prov_disc_info.peer_channel_num[1] = 0; + pwdinfo->tx_prov_disc_info.benable = _FALSE; + } + + for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 3) + peerMAC[jj] = key_2char2num(extra[kk], extra[kk + 1]); + + if (_rtw_memcmp(&extra[18], "display", 7)) + pwdinfo->tx_prov_disc_info.wps_config_method_request = WPS_CM_DISPLYA; + else if (_rtw_memcmp(&extra[18], "keypad", 7)) + pwdinfo->tx_prov_disc_info.wps_config_method_request = WPS_CM_KEYPAD; + else if (_rtw_memcmp(&extra[18], "pbc", 3)) + pwdinfo->tx_prov_disc_info.wps_config_method_request = WPS_CM_PUSH_BUTTON; + else if (_rtw_memcmp(&extra[18], "label", 5)) + pwdinfo->tx_prov_disc_info.wps_config_method_request = WPS_CM_LABEL; + else { + RTW_INFO("[%s] Unknown WPS config methodn", __FUNCTION__); + return ret ; + } + + _enter_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + phead = get_list_head(queue); + plist = get_next(phead); + + while (1) { + if (rtw_end_of_queue_search(phead, plist) == _TRUE) + break; + + if (uintPeerChannel != 0) + break; + + pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list); + + /* Commented by Albert 2011/05/18 */ + /* Match the device address located in the P2P IE */ + /* This is for the case that the P2P device address is not the same as the P2P interface address. */ + + p2pie = rtw_bss_ex_get_p2p_ie(&pnetwork->network, NULL, &p2pielen); + if (p2pie) { + while (p2pie) { + /* The P2P Device ID attribute is included in the Beacon frame. */ + /* The P2P Device Info attribute is included in the probe response frame. */ + + if (rtw_get_p2p_attr_content(p2pie, p2pielen, P2P_ATTR_DEVICE_ID, attr_content, &attr_contentlen)) { + /* Handle the P2P Device ID attribute of Beacon first */ + if (_rtw_memcmp(attr_content, peerMAC, ETH_ALEN)) { + uintPeerChannel = pnetwork->network.Configuration.DSConfig; + break; + } + } else if (rtw_get_p2p_attr_content(p2pie, p2pielen, P2P_ATTR_DEVICE_INFO, attr_content, &attr_contentlen)) { + /* Handle the P2P Device Info attribute of probe response */ + if (_rtw_memcmp(attr_content, peerMAC, ETH_ALEN)) { + uintPeerChannel = pnetwork->network.Configuration.DSConfig; + break; + } + } + + /* Get the next P2P IE */ + p2pie = rtw_get_p2p_ie(p2pie + p2pielen, BSS_EX_TLV_IES_LEN(&pnetwork->network) - (p2pie + p2pielen - BSS_EX_TLV_IES(&pnetwork->network)), NULL, &p2pielen); + } + + } + + plist = get_next(plist); + + } + + _exit_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + + if (uintPeerChannel) { +#ifdef CONFIG_WFD + if (hal_chk_wl_func(padapter, WL_FUNC_MIRACAST)) { + struct wifi_display_info *pwfd_info = pwdinfo->wfd_info; + u8 *wfd_ie; + uint wfd_ielen = 0; + + wfd_ie = rtw_bss_ex_get_wfd_ie(&pnetwork->network, NULL, &wfd_ielen); + if (wfd_ie) { + u8 *wfd_devinfo; + uint wfd_devlen; + + RTW_INFO("[%s] Found WFD IE!\n", __FUNCTION__); + wfd_devinfo = rtw_get_wfd_attr_content(wfd_ie, wfd_ielen, WFD_ATTR_DEVICE_INFO, NULL, &wfd_devlen); + if (wfd_devinfo) { + u16 wfd_devinfo_field = 0; + + /* Commented by Albert 20120319 */ + /* The first two bytes are the WFD device information field of WFD device information subelement. */ + /* In big endian format. */ + wfd_devinfo_field = RTW_GET_BE16(wfd_devinfo); + if (wfd_devinfo_field & WFD_DEVINFO_SESSION_AVAIL) + pwfd_info->peer_session_avail = _TRUE; + else + pwfd_info->peer_session_avail = _FALSE; + } + } + + if (_FALSE == pwfd_info->peer_session_avail) { + RTW_INFO("[%s] WFD Session not avaiable!\n", __FUNCTION__); + goto exit; + } + } +#endif /* CONFIG_WFD */ + + RTW_INFO("[%s] peer channel: %d!\n", __FUNCTION__, uintPeerChannel); +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_check_status(padapter, MI_LINKED)) + _cancel_timer_ex(&prochinfo->ap_roch_ch_switch_timer); +#endif /* CONFIG_CONCURRENT_MODE */ + _rtw_memcpy(pwdinfo->tx_prov_disc_info.peerIFAddr, pnetwork->network.MacAddress, ETH_ALEN); + _rtw_memcpy(pwdinfo->tx_prov_disc_info.peerDevAddr, peerMAC, ETH_ALEN); + pwdinfo->tx_prov_disc_info.peer_channel_num[0] = (u16) uintPeerChannel; + pwdinfo->tx_prov_disc_info.benable = _TRUE; + rtw_p2p_set_pre_state(pwdinfo, rtw_p2p_state(pwdinfo)); + rtw_p2p_set_state(pwdinfo, P2P_STATE_TX_PROVISION_DIS_REQ); + + if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_CLIENT)) + _rtw_memcpy(&pwdinfo->tx_prov_disc_info.ssid, &pnetwork->network.Ssid, sizeof(NDIS_802_11_SSID)); + else if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_DEVICE) || rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO)) { + _rtw_memcpy(pwdinfo->tx_prov_disc_info.ssid.Ssid, pwdinfo->p2p_wildcard_ssid, P2P_WILDCARD_SSID_LEN); + pwdinfo->tx_prov_disc_info.ssid.SsidLength = P2P_WILDCARD_SSID_LEN; + } + +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_check_status(padapter, MI_LINKED)) { + u8 union_ch = rtw_mi_get_union_chan(padapter); + u8 union_bw = rtw_mi_get_union_bw(padapter); + u8 union_offset = rtw_mi_get_union_offset(padapter); + + set_channel_bwmode(padapter, union_ch, union_offset, union_bw); + rtw_leave_opch(padapter); + + } else + set_channel_bwmode(padapter, uintPeerChannel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, CHANNEL_WIDTH_20); +#else + set_channel_bwmode(padapter, uintPeerChannel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, CHANNEL_WIDTH_20); +#endif + + _set_timer(&pwdinfo->pre_tx_scan_timer, P2P_TX_PRESCAN_TIMEOUT); + +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_check_status(padapter, MI_LINKED)) + _set_timer(&pwdinfo->restore_p2p_state_timer, P2P_CONCURRENT_PROVISION_TIMEOUT); + else + _set_timer(&pwdinfo->restore_p2p_state_timer, P2P_PROVISION_TIMEOUT); +#else + _set_timer(&pwdinfo->restore_p2p_state_timer, P2P_PROVISION_TIMEOUT); +#endif /* CONFIG_CONCURRENT_MODE */ + + } else { + RTW_INFO("[%s] NOT Found in the Scanning Queue!\n", __FUNCTION__); + } +exit: + + return ret; + +} + +/* Added by Albert 20110328 + * This function is used to inform the driver the user had specified the pin code value or pbc + * to application. */ + +static int rtw_p2p_got_wpsinfo(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + + RTW_INFO("[%s] data = %s\n", __FUNCTION__, extra); + /* Added by Albert 20110328 */ + /* if the input data is P2P_NO_WPSINFO -> reset the wpsinfo */ + /* if the input data is P2P_GOT_WPSINFO_PEER_DISPLAY_PIN -> the utility just input the PIN code got from the peer P2P device. */ + /* if the input data is P2P_GOT_WPSINFO_SELF_DISPLAY_PIN -> the utility just got the PIN code from itself. */ + /* if the input data is P2P_GOT_WPSINFO_PBC -> the utility just determine to use the PBC */ + + if (*extra == '0') + pwdinfo->ui_got_wps_info = P2P_NO_WPSINFO; + else if (*extra == '1') + pwdinfo->ui_got_wps_info = P2P_GOT_WPSINFO_PEER_DISPLAY_PIN; + else if (*extra == '2') + pwdinfo->ui_got_wps_info = P2P_GOT_WPSINFO_SELF_DISPLAY_PIN; + else if (*extra == '3') + pwdinfo->ui_got_wps_info = P2P_GOT_WPSINFO_PBC; + else + pwdinfo->ui_got_wps_info = P2P_NO_WPSINFO; + + return ret; + +} + +#endif /* CONFIG_P2P */ + +static int rtw_p2p_set(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; +#ifdef CONFIG_P2P + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + RTW_INFO("[%s] extra = %s\n", __FUNCTION__, extra); + + if (_rtw_memcmp(extra, "enable=", 7)) + rtw_wext_p2p_enable(dev, info, wrqu, &extra[7]); + else if (_rtw_memcmp(extra, "setDN=", 6)) { + wrqu->data.length -= 6; + rtw_p2p_setDN(dev, info, wrqu, &extra[6]); + } else if (_rtw_memcmp(extra, "profilefound=", 13)) { + wrqu->data.length -= 13; + rtw_p2p_profilefound(dev, info, wrqu, &extra[13]); + } else if (_rtw_memcmp(extra, "prov_disc=", 10)) { + wrqu->data.length -= 10; + rtw_p2p_prov_disc(dev, info, wrqu, &extra[10]); + } else if (_rtw_memcmp(extra, "nego=", 5)) { + wrqu->data.length -= 5; + rtw_p2p_connect(dev, info, wrqu, &extra[5]); + } else if (_rtw_memcmp(extra, "intent=", 7)) { + /* Commented by Albert 2011/03/23 */ + /* The wrqu->data.length will include the null character */ + /* So, we will decrease 7 + 1 */ + wrqu->data.length -= 8; + rtw_p2p_set_intent(dev, info, wrqu, &extra[7]); + } else if (_rtw_memcmp(extra, "ssid=", 5)) { + wrqu->data.length -= 5; + rtw_p2p_set_go_nego_ssid(dev, info, wrqu, &extra[5]); + } else if (_rtw_memcmp(extra, "got_wpsinfo=", 12)) { + wrqu->data.length -= 12; + rtw_p2p_got_wpsinfo(dev, info, wrqu, &extra[12]); + } else if (_rtw_memcmp(extra, "listen_ch=", 10)) { + /* Commented by Albert 2011/05/24 */ + /* The wrqu->data.length will include the null character */ + /* So, we will decrease (10 + 1) */ + wrqu->data.length -= 11; + rtw_p2p_set_listen_ch(dev, info, wrqu, &extra[10]); + } else if (_rtw_memcmp(extra, "op_ch=", 6)) { + /* Commented by Albert 2011/05/24 */ + /* The wrqu->data.length will include the null character */ + /* So, we will decrease (6 + 1) */ + wrqu->data.length -= 7; + rtw_p2p_set_op_ch(dev, info, wrqu, &extra[6]); + } else if (_rtw_memcmp(extra, "invite=", 7)) { + wrqu->data.length -= 8; + rtw_p2p_invite_req(dev, info, wrqu, &extra[7]); + } else if (_rtw_memcmp(extra, "persistent=", 11)) { + wrqu->data.length -= 11; + rtw_p2p_set_persistent(dev, info, wrqu, &extra[11]); + } else if (_rtw_memcmp(extra, "uuid=", 5)) { + wrqu->data.length -= 5; + ret = rtw_p2p_set_wps_uuid(dev, info, wrqu, &extra[5]); + } + +#ifdef CONFIG_WFD + if (hal_chk_wl_func(padapter, WL_FUNC_MIRACAST)) { + if (_rtw_memcmp(extra, "sa=", 3)) { + /* sa: WFD Session Available information */ + wrqu->data.length -= 3; + rtw_p2p_set_sa(dev, info, wrqu, &extra[3]); + } else if (_rtw_memcmp(extra, "pc=", 3)) { + /* pc: WFD Preferred Connection */ + wrqu->data.length -= 3; + rtw_p2p_set_pc(dev, info, wrqu, &extra[3]); + } else if (_rtw_memcmp(extra, "wfd_type=", 9)) { + wrqu->data.length -= 9; + rtw_p2p_set_wfd_device_type(dev, info, wrqu, &extra[9]); + } else if (_rtw_memcmp(extra, "wfd_enable=", 11)) { + wrqu->data.length -= 11; + rtw_p2p_set_wfd_enable(dev, info, wrqu, &extra[11]); + } else if (_rtw_memcmp(extra, "driver_iface=", 13)) { + wrqu->data.length -= 13; + rtw_p2p_set_driver_iface(dev, info, wrqu, &extra[13]); + } + } +#endif /* CONFIG_WFD */ + +#endif /* CONFIG_P2P */ + + return ret; + +} + +static int rtw_p2p_get(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; +#ifdef CONFIG_P2P + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + if (padapter->bShowGetP2PState) + RTW_INFO("[%s] extra = %s\n", __FUNCTION__, (char *) wrqu->data.pointer); + + if (_rtw_memcmp(wrqu->data.pointer, "status", 6)) + rtw_p2p_get_status(dev, info, wrqu, extra); + else if (_rtw_memcmp(wrqu->data.pointer, "role", 4)) + rtw_p2p_get_role(dev, info, wrqu, extra); + else if (_rtw_memcmp(wrqu->data.pointer, "peer_ifa", 8)) + rtw_p2p_get_peer_ifaddr(dev, info, wrqu, extra); + else if (_rtw_memcmp(wrqu->data.pointer, "req_cm", 6)) + rtw_p2p_get_req_cm(dev, info, wrqu, extra); + else if (_rtw_memcmp(wrqu->data.pointer, "peer_deva", 9)) { + /* Get the P2P device address when receiving the provision discovery request frame. */ + rtw_p2p_get_peer_devaddr(dev, info, wrqu, extra); + } else if (_rtw_memcmp(wrqu->data.pointer, "group_id", 8)) + rtw_p2p_get_groupid(dev, info, wrqu, extra); + else if (_rtw_memcmp(wrqu->data.pointer, "inv_peer_deva", 13)) { + /* Get the P2P device address when receiving the P2P Invitation request frame. */ + rtw_p2p_get_peer_devaddr_by_invitation(dev, info, wrqu, extra); + } else if (_rtw_memcmp(wrqu->data.pointer, "op_ch", 5)) + rtw_p2p_get_op_ch(dev, info, wrqu, extra); + +#ifdef CONFIG_WFD + if (hal_chk_wl_func(padapter, WL_FUNC_MIRACAST)) { + if (_rtw_memcmp(wrqu->data.pointer, "peer_port", 9)) + rtw_p2p_get_peer_wfd_port(dev, info, wrqu, extra); + else if (_rtw_memcmp(wrqu->data.pointer, "wfd_sa", 6)) + rtw_p2p_get_peer_wfd_session_available(dev, info, wrqu, extra); + else if (_rtw_memcmp(wrqu->data.pointer, "wfd_pc", 6)) + rtw_p2p_get_peer_wfd_preferred_connection(dev, info, wrqu, extra); + } +#endif /* CONFIG_WFD */ + +#endif /* CONFIG_P2P */ + + return ret; + +} + +static int rtw_p2p_get2(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + +#ifdef CONFIG_P2P + + int length = wrqu->data.length; + char *buffer = (u8 *)rtw_malloc(length); + + if (buffer == NULL) { + ret = -ENOMEM; + goto bad; + } + + if (copy_from_user(buffer, wrqu->data.pointer, wrqu->data.length)) { + ret = -EFAULT; + goto bad; + } + + RTW_INFO("[%s] buffer = %s\n", __FUNCTION__, buffer); + + if (_rtw_memcmp(buffer, "wpsCM=", 6)) + ret = rtw_p2p_get_wps_configmethod(dev, info, wrqu, extra, &buffer[6]); + else if (_rtw_memcmp(buffer, "devN=", 5)) + ret = rtw_p2p_get_device_name(dev, info, wrqu, extra, &buffer[5]); + else if (_rtw_memcmp(buffer, "dev_type=", 9)) + ret = rtw_p2p_get_device_type(dev, info, wrqu, extra, &buffer[9]); + else if (_rtw_memcmp(buffer, "go_devadd=", 10)) + ret = rtw_p2p_get_go_device_address(dev, info, wrqu, extra, &buffer[10]); + else if (_rtw_memcmp(buffer, "InvProc=", 8)) + ret = rtw_p2p_get_invitation_procedure(dev, info, wrqu, extra, &buffer[8]); + else { + snprintf(extra, sizeof("Command not found."), "Command not found."); + wrqu->data.length = strlen(extra); + } + +bad: + if (buffer) + rtw_mfree(buffer, length); + +#endif /* CONFIG_P2P */ + + return ret; + +} + +#ifdef CONFIG_MP_INCLUDED +static int rtw_cta_test_start(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(padapter); + + RTW_INFO("%s %s\n", __func__, extra); + if (!strcmp(extra, "1")) + hal_data->in_cta_test = 1; + else + hal_data->in_cta_test = 0; + + rtw_hal_rcr_set_chk_bssid(padapter, MLME_ACTION_NONE); + + return ret; +} +#endif + +#ifdef CONFIG_IOL +#include +#endif +#ifdef CONFIG_BACKGROUND_NOISE_MONITOR +#include "../../hal/hal_dm_acs.h" +#endif +#ifdef DBG_CMD_QUEUE +u8 dump_cmd_id = 0; +#endif + +static int rtw_dbg_port(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; +#ifdef CONFIG_RTW_DEBUG + _irqL irqL; + u8 major_cmd, minor_cmd; + u16 arg; + u32 extra_arg, *pdata, val32; + struct sta_info *psta; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv; + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + struct security_priv *psecuritypriv = &padapter->securitypriv; + struct wlan_network *cur_network = &(pmlmepriv->cur_network); + struct sta_priv *pstapriv = &padapter->stapriv; + + + pdata = (u32 *)&wrqu->data; + + val32 = *pdata; + arg = (u16)(val32 & 0x0000ffff); + major_cmd = (u8)(val32 >> 24); + minor_cmd = (u8)((val32 >> 16) & 0x00ff); + + extra_arg = *(pdata + 1); + + switch (major_cmd) { + case 0x70: /* read_reg */ + switch (minor_cmd) { + case 1: + RTW_INFO("rtw_read8(0x%x)=0x%02x\n", arg, rtw_read8(padapter, arg)); + break; + case 2: + RTW_INFO("rtw_read16(0x%x)=0x%04x\n", arg, rtw_read16(padapter, arg)); + break; + case 4: + RTW_INFO("rtw_read32(0x%x)=0x%08x\n", arg, rtw_read32(padapter, arg)); + break; + } + break; + case 0x71: /* write_reg */ + switch (minor_cmd) { + case 1: + rtw_write8(padapter, arg, extra_arg); + RTW_INFO("rtw_write8(0x%x)=0x%02x\n", arg, rtw_read8(padapter, arg)); + break; + case 2: + rtw_write16(padapter, arg, extra_arg); + RTW_INFO("rtw_write16(0x%x)=0x%04x\n", arg, rtw_read16(padapter, arg)); + break; + case 4: + rtw_write32(padapter, arg, extra_arg); + RTW_INFO("rtw_write32(0x%x)=0x%08x\n", arg, rtw_read32(padapter, arg)); + break; + } + break; + case 0x72: /* read_bb */ + RTW_INFO("read_bbreg(0x%x)=0x%x\n", arg, rtw_hal_read_bbreg(padapter, arg, 0xffffffff)); + break; + case 0x73: /* write_bb */ + rtw_hal_write_bbreg(padapter, arg, 0xffffffff, extra_arg); + RTW_INFO("write_bbreg(0x%x)=0x%x\n", arg, rtw_hal_read_bbreg(padapter, arg, 0xffffffff)); + break; + case 0x74: /* read_rf */ + RTW_INFO("read RF_reg path(0x%02x),offset(0x%x),value(0x%08x)\n", minor_cmd, arg, rtw_hal_read_rfreg(padapter, minor_cmd, arg, 0xffffffff)); + break; + case 0x75: /* write_rf */ + rtw_hal_write_rfreg(padapter, minor_cmd, arg, 0xffffffff, extra_arg); + RTW_INFO("write RF_reg path(0x%02x),offset(0x%x),value(0x%08x)\n", minor_cmd, arg, rtw_hal_read_rfreg(padapter, minor_cmd, arg, 0xffffffff)); + break; + + case 0x76: + switch (minor_cmd) { + case 0x00: /* normal mode, */ + padapter->recvpriv.is_signal_dbg = 0; + break; + case 0x01: /* dbg mode */ + padapter->recvpriv.is_signal_dbg = 1; + extra_arg = extra_arg > 100 ? 100 : extra_arg; + padapter->recvpriv.signal_strength_dbg = extra_arg; + break; + } + break; + case 0x78: /* IOL test */ + switch (minor_cmd) { + #ifdef CONFIG_IOL + case 0x04: { /* LLT table initialization test */ + u8 page_boundary = 0xf9; + { + struct xmit_frame *xmit_frame; + + xmit_frame = rtw_IOL_accquire_xmit_frame(padapter); + if (xmit_frame == NULL) { + ret = -ENOMEM; + break; + } + + rtw_IOL_append_LLT_cmd(xmit_frame, page_boundary); + + + if (_SUCCESS != rtw_IOL_exec_cmds_sync(padapter, xmit_frame, 500, 0)) + ret = -EPERM; + } + } + break; + case 0x05: { /* blink LED test */ + u16 reg = 0x4c; + u32 blink_num = 50; + u32 blink_delay_ms = 200; + int i; + + { + struct xmit_frame *xmit_frame; + + xmit_frame = rtw_IOL_accquire_xmit_frame(padapter); + if (xmit_frame == NULL) { + ret = -ENOMEM; + break; + } + + for (i = 0; i < blink_num; i++) { + #ifdef CONFIG_IOL_NEW_GENERATION + rtw_IOL_append_WB_cmd(xmit_frame, reg, 0x00, 0xff); + rtw_IOL_append_DELAY_MS_cmd(xmit_frame, blink_delay_ms); + rtw_IOL_append_WB_cmd(xmit_frame, reg, 0x08, 0xff); + rtw_IOL_append_DELAY_MS_cmd(xmit_frame, blink_delay_ms); + #else + rtw_IOL_append_WB_cmd(xmit_frame, reg, 0x00); + rtw_IOL_append_DELAY_MS_cmd(xmit_frame, blink_delay_ms); + rtw_IOL_append_WB_cmd(xmit_frame, reg, 0x08); + rtw_IOL_append_DELAY_MS_cmd(xmit_frame, blink_delay_ms); + #endif + } + if (_SUCCESS != rtw_IOL_exec_cmds_sync(padapter, xmit_frame, (blink_delay_ms * blink_num * 2) + 200, 0)) + ret = -EPERM; + } + } + break; + + case 0x06: { /* continuous wirte byte test */ + u16 reg = arg; + u16 start_value = 0; + u32 write_num = extra_arg; + int i; + u8 final; + + { + struct xmit_frame *xmit_frame; + + xmit_frame = rtw_IOL_accquire_xmit_frame(padapter); + if (xmit_frame == NULL) { + ret = -ENOMEM; + break; + } + + for (i = 0; i < write_num; i++) { + #ifdef CONFIG_IOL_NEW_GENERATION + rtw_IOL_append_WB_cmd(xmit_frame, reg, i + start_value, 0xFF); + #else + rtw_IOL_append_WB_cmd(xmit_frame, reg, i + start_value); + #endif + } + if (_SUCCESS != rtw_IOL_exec_cmds_sync(padapter, xmit_frame, 5000, 0)) + ret = -EPERM; + } + + final = rtw_read8(padapter, reg); + if (start_value + write_num - 1 == final) + RTW_INFO("continuous IOL_CMD_WB_REG to 0x%x %u times Success, start:%u, final:%u\n", reg, write_num, start_value, final); + else + RTW_INFO("continuous IOL_CMD_WB_REG to 0x%x %u times Fail, start:%u, final:%u\n", reg, write_num, start_value, final); + } + break; + + case 0x07: { /* continuous wirte word test */ + u16 reg = arg; + u16 start_value = 200; + u32 write_num = extra_arg; + + int i; + u16 final; + + { + struct xmit_frame *xmit_frame; + + xmit_frame = rtw_IOL_accquire_xmit_frame(padapter); + if (xmit_frame == NULL) { + ret = -ENOMEM; + break; + } + + for (i = 0; i < write_num; i++) { + #ifdef CONFIG_IOL_NEW_GENERATION + rtw_IOL_append_WW_cmd(xmit_frame, reg, i + start_value, 0xFFFF); + #else + rtw_IOL_append_WW_cmd(xmit_frame, reg, i + start_value); + #endif + } + if (_SUCCESS != rtw_IOL_exec_cmds_sync(padapter, xmit_frame, 5000, 0)) + ret = -EPERM; + } + + final = rtw_read16(padapter, reg); + if (start_value + write_num - 1 == final) + RTW_INFO("continuous IOL_CMD_WW_REG to 0x%x %u times Success, start:%u, final:%u\n", reg, write_num, start_value, final); + else + RTW_INFO("continuous IOL_CMD_WW_REG to 0x%x %u times Fail, start:%u, final:%u\n", reg, write_num, start_value, final); + } + break; + + case 0x08: { /* continuous wirte dword test */ + u16 reg = arg; + u32 start_value = 0x110000c7; + u32 write_num = extra_arg; + + int i; + u32 final; + + { + struct xmit_frame *xmit_frame; + + xmit_frame = rtw_IOL_accquire_xmit_frame(padapter); + if (xmit_frame == NULL) { + ret = -ENOMEM; + break; + } + + for (i = 0; i < write_num; i++) { + #ifdef CONFIG_IOL_NEW_GENERATION + rtw_IOL_append_WD_cmd(xmit_frame, reg, i + start_value, 0xFFFFFFFF); + #else + rtw_IOL_append_WD_cmd(xmit_frame, reg, i + start_value); + #endif + } + if (_SUCCESS != rtw_IOL_exec_cmds_sync(padapter, xmit_frame, 5000, 0)) + ret = -EPERM; + + } + + final = rtw_read32(padapter, reg); + if (start_value + write_num - 1 == final) + RTW_INFO("continuous IOL_CMD_WD_REG to 0x%x %u times Success, start:%u, final:%u\n", reg, write_num, start_value, final); + else + RTW_INFO("continuous IOL_CMD_WD_REG to 0x%x %u times Fail, start:%u, final:%u\n", reg, write_num, start_value, final); + } + break; + #endif /* CONFIG_IOL */ + } + break; + case 0x79: { + /* + * dbg 0x79000000 [value], set RESP_TXAGC to + value, value:0~15 + * dbg 0x79010000 [value], set RESP_TXAGC to - value, value:0~15 + */ + u8 value = extra_arg & 0x0f; + u8 sign = minor_cmd; + u16 write_value = 0; + + RTW_INFO("%s set RESP_TXAGC to %s %u\n", __func__, sign ? "minus" : "plus", value); + + if (sign) + value = value | 0x10; + + write_value = value | (value << 5); + rtw_write16(padapter, 0x6d9, write_value); + } + break; + case 0x7a: + receive_disconnect(padapter, pmlmeinfo->network.MacAddress + , WLAN_REASON_EXPIRATION_CHK, _FALSE); + break; + case 0x7F: + switch (minor_cmd) { + case 0x0: + RTW_INFO("fwstate=0x%x\n", get_fwstate(pmlmepriv)); + break; + case 0x01: + RTW_INFO("auth_alg=0x%x, enc_alg=0x%x, auth_type=0x%x, enc_type=0x%x\n", + psecuritypriv->dot11AuthAlgrthm, psecuritypriv->dot11PrivacyAlgrthm, + psecuritypriv->ndisauthtype, psecuritypriv->ndisencryptstatus); + break; + case 0x03: + RTW_INFO("qos_option=%d\n", pmlmepriv->qospriv.qos_option); +#ifdef CONFIG_80211N_HT + RTW_INFO("ht_option=%d\n", pmlmepriv->htpriv.ht_option); +#endif /* CONFIG_80211N_HT */ + break; + case 0x04: + RTW_INFO("cur_ch=%d\n", pmlmeext->cur_channel); + RTW_INFO("cur_bw=%d\n", pmlmeext->cur_bwmode); + RTW_INFO("cur_ch_off=%d\n", pmlmeext->cur_ch_offset); + + RTW_INFO("oper_ch=%d\n", rtw_get_oper_ch(padapter)); + RTW_INFO("oper_bw=%d\n", rtw_get_oper_bw(padapter)); + RTW_INFO("oper_ch_offet=%d\n", rtw_get_oper_choffset(padapter)); + + break; + case 0x05: + psta = rtw_get_stainfo(pstapriv, cur_network->network.MacAddress); + if (psta) { + RTW_INFO("SSID=%s\n", cur_network->network.Ssid.Ssid); + RTW_INFO("sta's macaddr:" MAC_FMT "\n", MAC_ARG(psta->cmn.mac_addr)); + RTW_INFO("cur_channel=%d, cur_bwmode=%d, cur_ch_offset=%d\n", pmlmeext->cur_channel, pmlmeext->cur_bwmode, pmlmeext->cur_ch_offset); + RTW_INFO("rtsen=%d, cts2slef=%d\n", psta->rtsen, psta->cts2self); + RTW_INFO("state=0x%x, aid=%d, macid=%d, raid=%d\n", + psta->state, psta->cmn.aid, psta->cmn.mac_id, psta->cmn.ra_info.rate_id); +#ifdef CONFIG_80211N_HT + RTW_INFO("qos_en=%d, ht_en=%d, init_rate=%d\n", psta->qos_option, psta->htpriv.ht_option, psta->init_rate); + RTW_INFO("bwmode=%d, ch_offset=%d, sgi_20m=%d,sgi_40m=%d\n" + , psta->cmn.bw_mode, psta->htpriv.ch_offset, psta->htpriv.sgi_20m, psta->htpriv.sgi_40m); + RTW_INFO("ampdu_enable = %d\n", psta->htpriv.ampdu_enable); + RTW_INFO("agg_enable_bitmap=%x, candidate_tid_bitmap=%x\n", psta->htpriv.agg_enable_bitmap, psta->htpriv.candidate_tid_bitmap); +#endif /* CONFIG_80211N_HT */ + + sta_rx_reorder_ctl_dump(RTW_DBGDUMP, psta); + } else + RTW_INFO("can't get sta's macaddr, cur_network's macaddr:" MAC_FMT "\n", MAC_ARG(cur_network->network.MacAddress)); + break; + case 0x06: { + u64 tsf = 0; + + tsf = rtw_hal_get_tsftr_by_port(padapter, extra_arg); + RTW_INFO(" PORT-%d TSF :%21lld\n", extra_arg, tsf); + } + break; + case 0x07: + RTW_INFO("bSurpriseRemoved=%s, bDriverStopped=%s\n" + , rtw_is_surprise_removed(padapter) ? "True" : "False" + , rtw_is_drv_stopped(padapter) ? "True" : "False"); + break; + case 0x08: { + struct xmit_priv *pxmitpriv = &padapter->xmitpriv; + struct recv_priv *precvpriv = &padapter->recvpriv; + + RTW_INFO("free_xmitbuf_cnt=%d, free_xmitframe_cnt=%d" + ", free_xmit_extbuf_cnt=%d, free_xframe_ext_cnt=%d" + ", free_recvframe_cnt=%d\n", + pxmitpriv->free_xmitbuf_cnt, pxmitpriv->free_xmitframe_cnt, + pxmitpriv->free_xmit_extbuf_cnt, pxmitpriv->free_xframe_ext_cnt, + precvpriv->free_recvframe_cnt); +#ifdef CONFIG_USB_HCI + RTW_INFO("rx_urb_pending_cn=%d\n", ATOMIC_READ(&(precvpriv->rx_pending_cnt))); +#endif + } + break; + case 0x09: { + int i; + _list *plist, *phead; + +#ifdef CONFIG_AP_MODE + RTW_INFO_DUMP("sta_dz_bitmap:", pstapriv->sta_dz_bitmap, pstapriv->aid_bmp_len); + RTW_INFO_DUMP("tim_bitmap:", pstapriv->tim_bitmap, pstapriv->aid_bmp_len); +#endif + _enter_critical_bh(&pstapriv->sta_hash_lock, &irqL); + + for (i = 0; i < NUM_STA; i++) { + phead = &(pstapriv->sta_hash[i]); + plist = get_next(phead); + + while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) { + psta = LIST_CONTAINOR(plist, struct sta_info, hash_list); + + plist = get_next(plist); + + if (extra_arg == psta->cmn.aid) { + RTW_INFO("sta's macaddr:" MAC_FMT "\n", MAC_ARG(psta->cmn.mac_addr)); + RTW_INFO("rtsen=%d, cts2slef=%d\n", psta->rtsen, psta->cts2self); + RTW_INFO("state=0x%x, aid=%d, macid=%d, raid=%d\n", + psta->state, psta->cmn.aid, psta->cmn.mac_id, psta->cmn.ra_info.rate_id); +#ifdef CONFIG_80211N_HT + RTW_INFO("qos_en=%d, ht_en=%d, init_rate=%d\n", psta->qos_option, psta->htpriv.ht_option, psta->init_rate); + RTW_INFO("bwmode=%d, ch_offset=%d, sgi_20m=%d,sgi_40m=%d\n", + psta->cmn.bw_mode, psta->htpriv.ch_offset, psta->htpriv.sgi_20m, + psta->htpriv.sgi_40m); + RTW_INFO("ampdu_enable = %d\n", psta->htpriv.ampdu_enable); + RTW_INFO("agg_enable_bitmap=%x, candidate_tid_bitmap=%x\n", psta->htpriv.agg_enable_bitmap, psta->htpriv.candidate_tid_bitmap); +#endif /* CONFIG_80211N_HT */ + +#ifdef CONFIG_AP_MODE + RTW_INFO("capability=0x%x\n", psta->capability); + RTW_INFO("flags=0x%x\n", psta->flags); + RTW_INFO("wpa_psk=0x%x\n", psta->wpa_psk); + RTW_INFO("wpa2_group_cipher=0x%x\n", psta->wpa2_group_cipher); + RTW_INFO("wpa2_pairwise_cipher=0x%x\n", psta->wpa2_pairwise_cipher); + RTW_INFO("qos_info=0x%x\n", psta->qos_info); +#endif + RTW_INFO("dot118021XPrivacy=0x%x\n", psta->dot118021XPrivacy); + + sta_rx_reorder_ctl_dump(RTW_DBGDUMP, psta); + } + + } + } + + _exit_critical_bh(&pstapriv->sta_hash_lock, &irqL); + + } + break; + + case 0x0b: { /* Enable=1, Disable=0 driver control vrtl_carrier_sense. */ + /* u8 driver_vcs_en; */ /* Enable=1, Disable=0 driver control vrtl_carrier_sense. */ + /* u8 driver_vcs_type; */ /* force 0:disable VCS, 1:RTS-CTS, 2:CTS-to-self when vcs_en=1. */ + + if (arg == 0) { + RTW_INFO("disable driver ctrl vcs\n"); + padapter->driver_vcs_en = 0; + } else if (arg == 1) { + RTW_INFO("enable driver ctrl vcs = %d\n", extra_arg); + padapter->driver_vcs_en = 1; + + if (extra_arg > 2) + padapter->driver_vcs_type = 1; + else + padapter->driver_vcs_type = extra_arg; + } + } + break; + case 0x0c: { /* dump rx/tx packet */ + if (arg == 0) { + RTW_INFO("dump rx packet (%d)\n", extra_arg); + /* pHalData->bDumpRxPkt =extra_arg; */ + rtw_hal_set_def_var(padapter, HAL_DEF_DBG_DUMP_RXPKT, &(extra_arg)); + } else if (arg == 1) { + RTW_INFO("dump tx packet (%d)\n", extra_arg); + rtw_hal_set_def_var(padapter, HAL_DEF_DBG_DUMP_TXPKT, &(extra_arg)); + } + } + break; + case 0x0e: { + if (arg == 0) { + RTW_INFO("disable driver ctrl rx_ampdu_factor\n"); + padapter->driver_rx_ampdu_factor = 0xFF; + } else if (arg == 1) { + + RTW_INFO("enable driver ctrl rx_ampdu_factor = %d\n", extra_arg); + + if (extra_arg > 0x03) + padapter->driver_rx_ampdu_factor = 0xFF; + else + padapter->driver_rx_ampdu_factor = extra_arg; + } + } + break; + #ifdef DBG_CONFIG_ERROR_DETECT + case 0x0f: { + if (extra_arg == 0) { + RTW_INFO("###### silent reset test.......#####\n"); + rtw_hal_sreset_reset(padapter); + } else { + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + struct sreset_priv *psrtpriv = &pHalData->srestpriv; + psrtpriv->dbg_trigger_point = extra_arg; + } + + } + break; + case 0x15: { + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + RTW_INFO("==>silent resete cnts:%d\n", pwrpriv->ips_enter_cnts); + } + break; + + #endif + + case 0x10: /* driver version display */ + dump_drv_version(RTW_DBGDUMP); + break; + case 0x11: { /* dump linked status */ + int pre_mode; + pre_mode = padapter->bLinkInfoDump; + /* linked_info_dump(padapter,extra_arg); */ + if (extra_arg == 1 || (extra_arg == 0 && pre_mode == 1)) /* not consider pwr_saving 0: */ + padapter->bLinkInfoDump = extra_arg; + + else if ((extra_arg == 2) || (extra_arg == 0 && pre_mode == 2)) { /* consider power_saving */ + /* RTW_INFO("linked_info_dump =%s\n", (padapter->bLinkInfoDump)?"enable":"disable") */ + linked_info_dump(padapter, extra_arg); + } + + + + } + break; +#ifdef CONFIG_80211N_HT + case 0x12: { /* set rx_stbc */ + struct registry_priv *pregpriv = &padapter->registrypriv; + /* 0: disable, bit(0):enable 2.4g, bit(1):enable 5g, 0x3: enable both 2.4g and 5g */ + /* default is set to enable 2.4GHZ for IOT issue with bufflao's AP at 5GHZ */ + if (pregpriv && (extra_arg == 0 || extra_arg == 1 || extra_arg == 2 || extra_arg == 3)) { + pregpriv->rx_stbc = extra_arg; + RTW_INFO("set rx_stbc=%d\n", pregpriv->rx_stbc); + } else + RTW_INFO("get rx_stbc=%d\n", pregpriv->rx_stbc); + + } + break; + case 0x13: { /* set ampdu_enable */ + struct registry_priv *pregpriv = &padapter->registrypriv; + /* 0: disable, 0x1:enable */ + if (pregpriv && extra_arg < 2) { + pregpriv->ampdu_enable = extra_arg; + RTW_INFO("set ampdu_enable=%d\n", pregpriv->ampdu_enable); + } else + RTW_INFO("get ampdu_enable=%d\n", pregpriv->ampdu_enable); + + } + break; +#endif + case 0x14: { /* get wifi_spec */ + struct registry_priv *pregpriv = &padapter->registrypriv; + RTW_INFO("get wifi_spec=%d\n", pregpriv->wifi_spec); + + } + break; + +#ifdef DBG_FIXED_CHAN + case 0x17: { + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + printk("===> Fixed channel to %d\n", extra_arg); + pmlmeext->fixed_chan = extra_arg; + + } + break; +#endif +#ifdef CONFIG_80211N_HT + case 0x19: { + struct registry_priv *pregistrypriv = &padapter->registrypriv; + /* extra_arg : */ + /* BIT0: Enable VHT LDPC Rx, BIT1: Enable VHT LDPC Tx, */ + /* BIT4: Enable HT LDPC Rx, BIT5: Enable HT LDPC Tx */ + if (arg == 0) { + RTW_INFO("driver disable LDPC\n"); + pregistrypriv->ldpc_cap = 0x00; + } else if (arg == 1) { + RTW_INFO("driver set LDPC cap = 0x%x\n", extra_arg); + pregistrypriv->ldpc_cap = (u8)(extra_arg & 0x33); + } + } + break; + case 0x1a: { + struct registry_priv *pregistrypriv = &padapter->registrypriv; + /* extra_arg : */ + /* BIT0: Enable VHT STBC Rx, BIT1: Enable VHT STBC Tx, */ + /* BIT4: Enable HT STBC Rx, BIT5: Enable HT STBC Tx */ + if (arg == 0) { + RTW_INFO("driver disable STBC\n"); + pregistrypriv->stbc_cap = 0x00; + } else if (arg == 1) { + RTW_INFO("driver set STBC cap = 0x%x\n", extra_arg); + pregistrypriv->stbc_cap = (u8)(extra_arg & 0x33); + } + } + break; +#endif /* CONFIG_80211N_HT */ + case 0x1b: { + struct registry_priv *pregistrypriv = &padapter->registrypriv; + + if (arg == 0) { + RTW_INFO("disable driver ctrl max_rx_rate, reset to default_rate_set\n"); + init_mlme_default_rate_set(padapter); +#ifdef CONFIG_80211N_HT + pregistrypriv->ht_enable = (u8)rtw_ht_enable; +#endif /* CONFIG_80211N_HT */ + } else if (arg == 1) { + + int i; + u8 max_rx_rate; + + RTW_INFO("enable driver ctrl max_rx_rate = 0x%x\n", extra_arg); + + max_rx_rate = (u8)extra_arg; + + if (max_rx_rate < 0xc) { /* max_rx_rate < MSC0->B or G -> disable HT */ +#ifdef CONFIG_80211N_HT + pregistrypriv->ht_enable = 0; +#endif /* CONFIG_80211N_HT */ + for (i = 0; i < NumRates; i++) { + if (pmlmeext->datarate[i] > max_rx_rate) + pmlmeext->datarate[i] = 0xff; + } + + } +#ifdef CONFIG_80211N_HT + else if (max_rx_rate < 0x1c) { /* mcs0~mcs15 */ + u32 mcs_bitmap = 0x0; + + for (i = 0; i < ((max_rx_rate + 1) - 0xc); i++) + mcs_bitmap |= BIT(i); + + set_mcs_rate_by_mask(pmlmeext->default_supported_mcs_set, mcs_bitmap); + } +#endif /* CONFIG_80211N_HT */ + } + } + break; + case 0x1c: { /* enable/disable driver control AMPDU Density for peer sta's rx */ + if (arg == 0) { + RTW_INFO("disable driver ctrl ampdu density\n"); + padapter->driver_ampdu_spacing = 0xFF; + } else if (arg == 1) { + + RTW_INFO("enable driver ctrl ampdu density = %d\n", extra_arg); + + if (extra_arg > 0x07) + padapter->driver_ampdu_spacing = 0xFF; + else + padapter->driver_ampdu_spacing = extra_arg; + } + } + break; +#ifdef CONFIG_BACKGROUND_NOISE_MONITOR + case 0x1e: { + RTW_INFO("===========================================\n"); + rtw_noise_measure_curchan(padapter); + RTW_INFO("===========================================\n"); + } + break; +#endif + + +#if defined(CONFIG_SDIO_HCI) && defined(CONFIG_SDIO_INDIRECT_ACCESS) && defined(DBG_SDIO_INDIRECT_ACCESS) + case 0x1f: + { + int i, j = 0, test_cnts = 0; + static u8 test_code = 0x5A; + static u32 data_misatch_cnt = 0, d_acc_err_cnt = 0; + + u32 d_data, i_data; + u32 imr; + + test_cnts = extra_arg; + for (i = 0; i < test_cnts; i++) { + if (RTW_CANNOT_IO(padapter)) + break; + + rtw_write8(padapter, 0x07, test_code); + + d_data = rtw_read32(padapter, 0x04); + imr = rtw_read32(padapter, 0x10250014); + rtw_write32(padapter, 0x10250014, 0); + rtw_msleep_os(50); + + i_data = rtw_sd_iread32(padapter, 0x04); + + rtw_write32(padapter, 0x10250014, imr); + + if (d_data != i_data) { + data_misatch_cnt++; + RTW_ERR("d_data :0x%08x, i_data : 0x%08x\n", d_data, i_data); + } + + if (test_code != (i_data >> 24)) { + d_acc_err_cnt++; + rtw_write8(padapter, 0x07, 0xAA); + RTW_ERR("test_code :0x%02x, i_data : 0x%08x\n", test_code, i_data); + } + if ((j++) == 100) { + rtw_msleep_os(2000); + RTW_INFO(" Indirect access testing..........%d/%d\n", i, test_cnts); + j = 0; + } + + test_code = ~test_code; + rtw_msleep_os(50); + } + RTW_INFO("========Indirect access test=========\n"); + RTW_INFO(" test_cnts = %d\n", test_cnts); + RTW_INFO(" direct & indirect read32 data missatch cnts = %d\n", data_misatch_cnt); + RTW_INFO(" indirect rdata is not equal to wdata cnts = %d\n", d_acc_err_cnt); + RTW_INFO("========Indirect access test=========\n\n"); + data_misatch_cnt = d_acc_err_cnt = 0; + + } + break; +#endif + case 0x20: + { + if (arg == 0xAA) { + u8 page_offset, page_num; + + page_offset = (u8)(extra_arg >> 16); + page_num = (u8)(extra_arg & 0xFF); + rtw_dump_rsvd_page(RTW_DBGDUMP, padapter, page_offset, page_num); + } +#ifdef CONFIG_SUPPORT_FIFO_DUMP + else { + u8 fifo_sel; + u32 addr, size; + + fifo_sel = (u8)(arg & 0x0F); + addr = (extra_arg >> 16) & 0xFFFF; + size = extra_arg & 0xFFFF; + rtw_dump_fifo(RTW_DBGDUMP, padapter, fifo_sel, addr, size); + } +#endif + } + break; + + case 0x23: { + RTW_INFO("turn %s the bNotifyChannelChange Variable\n", (extra_arg == 1) ? "on" : "off"); + padapter->bNotifyChannelChange = extra_arg; + break; + } + case 0x24: { +#ifdef CONFIG_P2P + RTW_INFO("turn %s the bShowGetP2PState Variable\n", (extra_arg == 1) ? "on" : "off"); + padapter->bShowGetP2PState = extra_arg; +#endif /* CONFIG_P2P */ + break; + } +#ifdef CONFIG_GPIO_API + case 0x25: { /* Get GPIO register */ + /* + * dbg 0x7f250000 [gpio_num], Get gpio value, gpio_num:0~7 + */ + + u8 value; + RTW_INFO("Read GPIO Value extra_arg = %d\n", extra_arg); + value = rtw_hal_get_gpio(padapter, extra_arg); + RTW_INFO("Read GPIO Value = %d\n", value); + break; + } + case 0x26: { /* Set GPIO direction */ + + /* dbg 0x7f26000x [y], Set gpio direction, + * x: gpio_num,4~7 y: indicate direction, 0~1 + */ + + int value; + RTW_INFO("Set GPIO Direction! arg = %d ,extra_arg=%d\n", arg , extra_arg); + value = rtw_hal_config_gpio(padapter, arg, extra_arg); + RTW_INFO("Set GPIO Direction %s\n", (value == -1) ? "Fail!!!" : "Success"); + break; + } + case 0x27: { /* Set GPIO output direction value */ + /* + * dbg 0x7f27000x [y], Set gpio output direction value, + * x: gpio_num,4~7 y: indicate direction, 0~1 + */ + + int value; + RTW_INFO("Set GPIO Value! arg = %d ,extra_arg=%d\n", arg , extra_arg); + value = rtw_hal_set_gpio_output_value(padapter, arg, extra_arg); + RTW_INFO("Set GPIO Value %s\n", (value == -1) ? "Fail!!!" : "Success"); + break; + } +#endif +#ifdef DBG_CMD_QUEUE + case 0x28: { + dump_cmd_id = extra_arg; + RTW_INFO("dump_cmd_id:%d\n", dump_cmd_id); + } + break; +#endif /* DBG_CMD_QUEUE */ + case 0xaa: { + if ((extra_arg & 0x7F) > 0x3F) + extra_arg = 0xFF; + RTW_INFO("chang data rate to :0x%02x\n", extra_arg); + padapter->fix_rate = extra_arg; + } + break; + case 0xdd: { /* registers dump , 0 for mac reg,1 for bb reg, 2 for rf reg */ + if (extra_arg == 0) + mac_reg_dump(RTW_DBGDUMP, padapter); + else if (extra_arg == 1) + bb_reg_dump(RTW_DBGDUMP, padapter); + else if (extra_arg == 2) + rf_reg_dump(RTW_DBGDUMP, padapter); + else if (extra_arg == 11) + bb_reg_dump_ex(RTW_DBGDUMP, padapter); + } + break; + + case 0xee: { + RTW_INFO(" === please control /proc to trun on/off PHYDM func ===\n"); + } + break; + + case 0xfd: + rtw_write8(padapter, 0xc50, arg); + RTW_INFO("wr(0xc50)=0x%x\n", rtw_read8(padapter, 0xc50)); + rtw_write8(padapter, 0xc58, arg); + RTW_INFO("wr(0xc58)=0x%x\n", rtw_read8(padapter, 0xc58)); + break; + case 0xfe: + RTW_INFO("rd(0xc50)=0x%x\n", rtw_read8(padapter, 0xc50)); + RTW_INFO("rd(0xc58)=0x%x\n", rtw_read8(padapter, 0xc58)); + break; + case 0xff: { + RTW_INFO("dbg(0x210)=0x%x\n", rtw_read32(padapter, 0x210)); + RTW_INFO("dbg(0x608)=0x%x\n", rtw_read32(padapter, 0x608)); + RTW_INFO("dbg(0x280)=0x%x\n", rtw_read32(padapter, 0x280)); + RTW_INFO("dbg(0x284)=0x%x\n", rtw_read32(padapter, 0x284)); + RTW_INFO("dbg(0x288)=0x%x\n", rtw_read32(padapter, 0x288)); + + RTW_INFO("dbg(0x664)=0x%x\n", rtw_read32(padapter, 0x664)); + + + RTW_INFO("\n"); + + RTW_INFO("dbg(0x430)=0x%x\n", rtw_read32(padapter, 0x430)); + RTW_INFO("dbg(0x438)=0x%x\n", rtw_read32(padapter, 0x438)); + + RTW_INFO("dbg(0x440)=0x%x\n", rtw_read32(padapter, 0x440)); + + RTW_INFO("dbg(0x458)=0x%x\n", rtw_read32(padapter, 0x458)); + + RTW_INFO("dbg(0x484)=0x%x\n", rtw_read32(padapter, 0x484)); + RTW_INFO("dbg(0x488)=0x%x\n", rtw_read32(padapter, 0x488)); + + RTW_INFO("dbg(0x444)=0x%x\n", rtw_read32(padapter, 0x444)); + RTW_INFO("dbg(0x448)=0x%x\n", rtw_read32(padapter, 0x448)); + RTW_INFO("dbg(0x44c)=0x%x\n", rtw_read32(padapter, 0x44c)); + RTW_INFO("dbg(0x450)=0x%x\n", rtw_read32(padapter, 0x450)); + } + break; + } + break; + default: + RTW_INFO("error dbg cmd!\n"); + break; + } + +#endif + return ret; + +} + +#ifdef CONFIG_IOCTL_WEXT +static int wpa_set_param(struct net_device *dev, u8 name, u32 value) +{ + uint ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + switch (name) { + case IEEE_PARAM_WPA_ENABLED: + + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_8021X; /* 802.1x */ + + /* ret = ieee80211_wpa_enable(ieee, value); */ + + switch ((value) & 0xff) { + case 1: /* WPA */ + padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeWPAPSK; /* WPA_PSK */ + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption2Enabled; + break; + case 2: /* WPA2 */ + padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeWPA2PSK; /* WPA2_PSK */ + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled; + break; + } + + + break; + + case IEEE_PARAM_TKIP_COUNTERMEASURES: + /* ieee->tkip_countermeasures=value; */ + break; + + case IEEE_PARAM_DROP_UNENCRYPTED: { + /* HACK: + * + * wpa_supplicant calls set_wpa_enabled when the driver + * is loaded and unloaded, regardless of if WPA is being + * used. No other calls are made which can be used to + * determine if encryption will be used or not prior to + * association being expected. If encryption is not being + * used, drop_unencrypted is set to false, else true -- we + * can use this to determine if the CAP_PRIVACY_ON bit should + * be set. + */ + +#if 0 + struct ieee80211_security sec = { + .flags = SEC_ENABLED, + .enabled = value, + }; + ieee->drop_unencrypted = value; + /* We only change SEC_LEVEL for open mode. Others + * are set by ipw_wpa_set_encryption. + */ + if (!value) { + sec.flags |= SEC_LEVEL; + sec.level = SEC_LEVEL_0; + } else { + sec.flags |= SEC_LEVEL; + sec.level = SEC_LEVEL_1; + } + if (ieee->set_security) + ieee->set_security(ieee->dev, &sec); +#endif + break; + + } + case IEEE_PARAM_PRIVACY_INVOKED: + + /* ieee->privacy_invoked=value; */ + + break; + + case IEEE_PARAM_AUTH_ALGS: + + ret = wpa_set_auth_algs(dev, value); + + break; + + case IEEE_PARAM_IEEE_802_1X: + + /* ieee->ieee802_1x=value; */ + + break; + + case IEEE_PARAM_WPAX_SELECT: + + /* added for WPA2 mixed mode */ + /*RTW_WARN("------------------------>wpax value = %x\n", value);*/ + /* + spin_lock_irqsave(&ieee->wpax_suitlist_lock,flags); + ieee->wpax_type_set = 1; + ieee->wpax_type_notify = value; + spin_unlock_irqrestore(&ieee->wpax_suitlist_lock,flags); + */ + + break; + + default: + + + + ret = -EOPNOTSUPP; + + + break; + + } + + return ret; + +} + +static int wpa_mlme(struct net_device *dev, u32 command, u32 reason) +{ + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + switch (command) { + case IEEE_MLME_STA_DEAUTH: + + if (!rtw_set_802_11_disassociate(padapter)) + ret = -1; + + break; + + case IEEE_MLME_STA_DISASSOC: + + if (!rtw_set_802_11_disassociate(padapter)) + ret = -1; + + break; + + default: + ret = -EOPNOTSUPP; + break; + } +#ifdef CONFIG_RTW_REPEATER_SON + rtw_rson_do_disconnect(padapter); +#endif + return ret; + +} + +static int wpa_supplicant_ioctl(struct net_device *dev, struct iw_point *p) +{ + struct ieee_param *param; + uint ret = 0; + + /* down(&ieee->wx_sem); */ + + if (p->length < sizeof(struct ieee_param) || !p->pointer) { + ret = -EINVAL; + goto out; + } + + param = (struct ieee_param *)rtw_malloc(p->length); + if (param == NULL) { + ret = -ENOMEM; + goto out; + } + + if (copy_from_user(param, p->pointer, p->length)) { + rtw_mfree((u8 *)param, p->length); + ret = -EFAULT; + goto out; + } + + switch (param->cmd) { + + case IEEE_CMD_SET_WPA_PARAM: + ret = wpa_set_param(dev, param->u.wpa_param.name, param->u.wpa_param.value); + break; + + case IEEE_CMD_SET_WPA_IE: + /* ret = wpa_set_wpa_ie(dev, param, p->length); */ + ret = rtw_set_wpa_ie((_adapter *)rtw_netdev_priv(dev), (char *)param->u.wpa_ie.data, (u16)param->u.wpa_ie.len); + break; + + case IEEE_CMD_SET_ENCRYPTION: + ret = wpa_set_encryption(dev, param, p->length); + break; + + case IEEE_CMD_MLME: + ret = wpa_mlme(dev, param->u.mlme.command, param->u.mlme.reason_code); + break; + + default: + RTW_INFO("Unknown WPA supplicant request: %d\n", param->cmd); + ret = -EOPNOTSUPP; + break; + + } + + if (ret == 0 && copy_to_user(p->pointer, param, p->length)) + ret = -EFAULT; + + rtw_mfree((u8 *)param, p->length); + +out: + + /* up(&ieee->wx_sem); */ + + return ret; + +} + +#ifdef CONFIG_AP_MODE +static int rtw_set_encryption(struct net_device *dev, struct ieee_param *param, u32 param_len) +{ + int ret = 0; + u32 wep_key_idx, wep_key_len, wep_total_len; + NDIS_802_11_WEP *pwep = NULL; + struct sta_info *psta = NULL, *pbcmc_sta = NULL; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct security_priv *psecuritypriv = &(padapter->securitypriv); + struct sta_priv *pstapriv = &padapter->stapriv; + + RTW_INFO("%s\n", __FUNCTION__); + + param->u.crypt.err = 0; + param->u.crypt.alg[IEEE_CRYPT_ALG_NAME_LEN - 1] = '\0'; + + /* sizeof(struct ieee_param) = 64 bytes; */ + /* if (param_len != (u32) ((u8 *) param->u.crypt.key - (u8 *) param) + param->u.crypt.key_len) */ + if (param_len != sizeof(struct ieee_param) + param->u.crypt.key_len) { + ret = -EINVAL; + goto exit; + } + + if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff && + param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff && + param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) { + if (param->u.crypt.idx >= WEP_KEYS +#ifdef CONFIG_IEEE80211W + && param->u.crypt.idx > BIP_MAX_KEYID +#endif /* CONFIG_IEEE80211W */ + ) { + ret = -EINVAL; + goto exit; + } + } else { + psta = rtw_get_stainfo(pstapriv, param->sta_addr); + if (!psta) { + /* ret = -EINVAL; */ + RTW_INFO("rtw_set_encryption(), sta has already been removed or never been added\n"); + goto exit; + } + } + + if (strcmp(param->u.crypt.alg, "none") == 0 && (psta == NULL)) { + /* todo:clear default encryption keys */ + + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open; + psecuritypriv->ndisencryptstatus = Ndis802_11EncryptionDisabled; + psecuritypriv->dot11PrivacyAlgrthm = _NO_PRIVACY_; + psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_; + + RTW_INFO("clear default encryption keys, keyid=%d\n", param->u.crypt.idx); + + goto exit; + } + + + if (strcmp(param->u.crypt.alg, "WEP") == 0 && (psta == NULL)) { + RTW_INFO("r871x_set_encryption, crypt.alg = WEP\n"); + + wep_key_idx = param->u.crypt.idx; + wep_key_len = param->u.crypt.key_len; + + RTW_INFO("r871x_set_encryption, wep_key_idx=%d, len=%d\n", wep_key_idx, wep_key_len); + + if ((wep_key_idx >= WEP_KEYS) || (wep_key_len <= 0)) { + ret = -EINVAL; + goto exit; + } + + + if (wep_key_len > 0) { + wep_key_len = wep_key_len <= 5 ? 5 : 13; + wep_total_len = wep_key_len + FIELD_OFFSET(NDIS_802_11_WEP, KeyMaterial); + pwep = (NDIS_802_11_WEP *)rtw_malloc(wep_total_len); + if (pwep == NULL) { + RTW_INFO(" r871x_set_encryption: pwep allocate fail !!!\n"); + goto exit; + } + + _rtw_memset(pwep, 0, wep_total_len); + + pwep->KeyLength = wep_key_len; + pwep->Length = wep_total_len; + + } + + pwep->KeyIndex = wep_key_idx; + + _rtw_memcpy(pwep->KeyMaterial, param->u.crypt.key, pwep->KeyLength); + + if (param->u.crypt.set_tx) { + RTW_INFO("wep, set_tx=1\n"); + + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Auto; + psecuritypriv->ndisencryptstatus = Ndis802_11Encryption1Enabled; + psecuritypriv->dot11PrivacyAlgrthm = _WEP40_; + psecuritypriv->dot118021XGrpPrivacy = _WEP40_; + + if (pwep->KeyLength == 13) { + psecuritypriv->dot11PrivacyAlgrthm = _WEP104_; + psecuritypriv->dot118021XGrpPrivacy = _WEP104_; + } + + + psecuritypriv->dot11PrivacyKeyIndex = wep_key_idx; + + _rtw_memcpy(&(psecuritypriv->dot11DefKey[wep_key_idx].skey[0]), pwep->KeyMaterial, pwep->KeyLength); + + psecuritypriv->dot11DefKeylen[wep_key_idx] = pwep->KeyLength; + + rtw_ap_set_wep_key(padapter, pwep->KeyMaterial, pwep->KeyLength, wep_key_idx, 1); + } else { + RTW_INFO("wep, set_tx=0\n"); + + /* don't update "psecuritypriv->dot11PrivacyAlgrthm" and */ + /* "psecuritypriv->dot11PrivacyKeyIndex=keyid", but can rtw_set_key to cam */ + + _rtw_memcpy(&(psecuritypriv->dot11DefKey[wep_key_idx].skey[0]), pwep->KeyMaterial, pwep->KeyLength); + + psecuritypriv->dot11DefKeylen[wep_key_idx] = pwep->KeyLength; + + rtw_ap_set_wep_key(padapter, pwep->KeyMaterial, pwep->KeyLength, wep_key_idx, 0); + } + + goto exit; + + } + + + if (!psta && check_fwstate(pmlmepriv, WIFI_AP_STATE)) /* */ { /* group key */ + if (param->u.crypt.set_tx == 1) { + if (strcmp(param->u.crypt.alg, "WEP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set WEP TX GTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + _rtw_memcpy(psecuritypriv->dot118021XGrpKey[param->u.crypt.idx].skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + psecuritypriv->dot118021XGrpPrivacy = _WEP40_; + if (param->u.crypt.key_len == 13) + psecuritypriv->dot118021XGrpPrivacy = _WEP104_; + + } else if (strcmp(param->u.crypt.alg, "TKIP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set TKIP TX GTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + psecuritypriv->dot118021XGrpPrivacy = _TKIP_; + _rtw_memcpy(psecuritypriv->dot118021XGrpKey[param->u.crypt.idx].skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + /* set mic key */ + _rtw_memcpy(psecuritypriv->dot118021XGrptxmickey[param->u.crypt.idx].skey, &(param->u.crypt.key[16]), 8); + _rtw_memcpy(psecuritypriv->dot118021XGrprxmickey[param->u.crypt.idx].skey, &(param->u.crypt.key[24]), 8); + psecuritypriv->busetkipkey = _TRUE; + + } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set CCMP TX GTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + psecuritypriv->dot118021XGrpPrivacy = _AES_; + _rtw_memcpy(psecuritypriv->dot118021XGrpKey[param->u.crypt.idx].skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + + #ifdef CONFIG_IEEE80211W + } else if (strcmp(param->u.crypt.alg, "BIP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set TX IGTK idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx, param->u.crypt.key_len); + _rtw_memcpy(padapter->securitypriv.dot11wBIPKey[param->u.crypt.idx].skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + psecuritypriv->dot11wBIPKeyid = param->u.crypt.idx; + psecuritypriv->dot11wBIPtxpn.val = RTW_GET_LE64(param->u.crypt.seq); + psecuritypriv->binstallBIPkey = _TRUE; + goto exit; + #endif /* CONFIG_IEEE80211W */ + + } else if (strcmp(param->u.crypt.alg, "none") == 0) { + RTW_INFO(FUNC_ADPT_FMT" clear group key, idx:%u\n" + , FUNC_ADPT_ARG(padapter), param->u.crypt.idx); + psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_; + } else { + RTW_WARN(FUNC_ADPT_FMT" set group key, not support\n" + , FUNC_ADPT_ARG(padapter)); + goto exit; + } + + psecuritypriv->dot118021XGrpKeyid = param->u.crypt.idx; + pbcmc_sta = rtw_get_bcmc_stainfo(padapter); + if (pbcmc_sta) { + pbcmc_sta->dot11txpn.val = RTW_GET_LE64(param->u.crypt.seq); + pbcmc_sta->ieee8021x_blocked = _FALSE; + pbcmc_sta->dot118021XPrivacy = psecuritypriv->dot118021XGrpPrivacy; /* rx will use bmc_sta's dot118021XPrivacy */ + } + psecuritypriv->binstallGrpkey = _TRUE; + psecuritypriv->dot11PrivacyAlgrthm = psecuritypriv->dot118021XGrpPrivacy;/* !!! */ + + rtw_ap_set_group_key(padapter, param->u.crypt.key, psecuritypriv->dot118021XGrpPrivacy, param->u.crypt.idx); + } + + goto exit; + + } + + if (psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_8021X && psta) { /* psk/802_1x */ + if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) { + if (param->u.crypt.set_tx == 1) { + _rtw_memcpy(psta->dot118021x_UncstKey.skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + + if (strcmp(param->u.crypt.alg, "WEP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set WEP PTK of "MAC_FMT" idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx, param->u.crypt.key_len); + psta->dot118021XPrivacy = _WEP40_; + if (param->u.crypt.key_len == 13) + psta->dot118021XPrivacy = _WEP104_; + + } else if (strcmp(param->u.crypt.alg, "TKIP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set TKIP PTK of "MAC_FMT" idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx, param->u.crypt.key_len); + psta->dot118021XPrivacy = _TKIP_; + /* set mic key */ + _rtw_memcpy(psta->dot11tkiptxmickey.skey, &(param->u.crypt.key[16]), 8); + _rtw_memcpy(psta->dot11tkiprxmickey.skey, &(param->u.crypt.key[24]), 8); + psecuritypriv->busetkipkey = _TRUE; + + } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) { + RTW_INFO(FUNC_ADPT_FMT" set CCMP PTK of "MAC_FMT" idx:%u, len:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx, param->u.crypt.key_len); + psta->dot118021XPrivacy = _AES_; + + } else if (strcmp(param->u.crypt.alg, "none") == 0) { + RTW_INFO(FUNC_ADPT_FMT" clear pairwise key of "MAC_FMT" idx:%u\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr) + , param->u.crypt.idx); + psta->dot118021XPrivacy = _NO_PRIVACY_; + + } else { + RTW_WARN(FUNC_ADPT_FMT" set pairwise key of "MAC_FMT", not support\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr)); + goto exit; + } + + psta->dot11txpn.val = RTW_GET_LE64(param->u.crypt.seq); + psta->dot11rxpn.val = RTW_GET_LE64(param->u.crypt.seq); + psta->ieee8021x_blocked = _FALSE; + + if (psta->dot118021XPrivacy != _NO_PRIVACY_) { + psta->bpairwise_key_installed = _TRUE; + + /* WPA2 key-handshake has completed */ + if (psecuritypriv->ndisauthtype == Ndis802_11AuthModeWPA2PSK) + psta->state &= (~WIFI_UNDER_KEY_HANDSHAKE); + } + + rtw_ap_set_pairwise_key(padapter, psta); + } else { + RTW_WARN(FUNC_ADPT_FMT" set group key of "MAC_FMT", not support\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(psta->cmn.mac_addr)); + goto exit; + } + + } + + } + +exit: + + if (pwep) + rtw_mfree((u8 *)pwep, wep_total_len); + + return ret; + +} + +static int rtw_set_beacon(struct net_device *dev, struct ieee_param *param, int len) +{ + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct sta_priv *pstapriv = &padapter->stapriv; + unsigned char *pbuf = param->u.bcn_ie.buf; + + + RTW_INFO("%s, len=%d\n", __FUNCTION__, len); + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) != _TRUE) + return -EINVAL; + + _rtw_memcpy(&pstapriv->max_num_sta, param->u.bcn_ie.reserved, 2); + + if ((pstapriv->max_num_sta > NUM_STA) || (pstapriv->max_num_sta <= 0)) + pstapriv->max_num_sta = NUM_STA; + + + if (rtw_check_beacon_data(padapter, pbuf, (len - 12 - 2)) == _SUCCESS) /* 12 = param header, 2:no packed */ + ret = 0; + else + ret = -EINVAL; + + + return ret; + +} + +static int rtw_hostapd_sta_flush(struct net_device *dev) +{ + /* _irqL irqL; */ + /* _list *phead, *plist; */ + int ret = 0; + /* struct sta_info *psta = NULL; */ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + /* struct sta_priv *pstapriv = &padapter->stapriv; */ + + RTW_INFO("%s\n", __FUNCTION__); + + flush_all_cam_entry(padapter); /* clear CAM */ +#ifdef CONFIG_AP_MODE + ret = rtw_sta_flush(padapter, _TRUE); +#endif + return ret; + +} + +static int rtw_add_sta(struct net_device *dev, struct ieee_param *param) +{ + int ret = 0; + struct sta_info *psta = NULL; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct sta_priv *pstapriv = &padapter->stapriv; + + RTW_INFO("rtw_add_sta(aid=%d)=" MAC_FMT "\n", param->u.add_sta.aid, MAC_ARG(param->sta_addr)); + + if (check_fwstate(pmlmepriv, (WIFI_ASOC_STATE | WIFI_AP_STATE)) != _TRUE) + return -EINVAL; + + if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff && + param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff && + param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) + return -EINVAL; + +#if 0 + psta = rtw_get_stainfo(pstapriv, param->sta_addr); + if (psta) { + RTW_INFO("rtw_add_sta(), free has been added psta=%p\n", psta); + /* _enter_critical_bh(&(pstapriv->sta_hash_lock), &irqL); */ + rtw_free_stainfo(padapter, psta); + /* _exit_critical_bh(&(pstapriv->sta_hash_lock), &irqL); */ + + psta = NULL; + } +#endif + /* psta = rtw_alloc_stainfo(pstapriv, param->sta_addr); */ + psta = rtw_get_stainfo(pstapriv, param->sta_addr); + if (psta) { + int flags = param->u.add_sta.flags; + + /* RTW_INFO("rtw_add_sta(), init sta's variables, psta=%p\n", psta); */ + + psta->cmn.aid = param->u.add_sta.aid;/* aid=1~2007 */ + + _rtw_memcpy(psta->bssrateset, param->u.add_sta.tx_supp_rates, 16); + + + /* check wmm cap. */ + if (WLAN_STA_WME & flags) + psta->qos_option = 1; + else + psta->qos_option = 0; + + if (pmlmepriv->qospriv.qos_option == 0) + psta->qos_option = 0; + + +#ifdef CONFIG_80211N_HT + /* chec 802.11n ht cap. */ + if (padapter->registrypriv.ht_enable && + is_supported_ht(padapter->registrypriv.wireless_mode) && + (WLAN_STA_HT & flags)) { + psta->htpriv.ht_option = _TRUE; + psta->qos_option = 1; + _rtw_memcpy((void *)&psta->htpriv.ht_cap, (void *)¶m->u.add_sta.ht_cap, sizeof(struct rtw_ieee80211_ht_cap)); + } else + psta->htpriv.ht_option = _FALSE; + + if (pmlmepriv->htpriv.ht_option == _FALSE) + psta->htpriv.ht_option = _FALSE; + +#endif + + + update_sta_info_apmode(padapter, psta); + + + } else + ret = -ENOMEM; + + return ret; + +} + +static int rtw_del_sta(struct net_device *dev, struct ieee_param *param) +{ + _irqL irqL; + int ret = 0; + struct sta_info *psta = NULL; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct sta_priv *pstapriv = &padapter->stapriv; + + RTW_INFO("rtw_del_sta=" MAC_FMT "\n", MAC_ARG(param->sta_addr)); + + if (check_fwstate(pmlmepriv, (WIFI_ASOC_STATE | WIFI_AP_STATE)) != _TRUE) + return -EINVAL; + + if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff && + param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff && + param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) + return -EINVAL; + + psta = rtw_get_stainfo(pstapriv, param->sta_addr); + if (psta) { + u8 updated = _FALSE; + + /* RTW_INFO("free psta=%p, aid=%d\n", psta, psta->cmn.aid); */ + + _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL); + if (rtw_is_list_empty(&psta->asoc_list) == _FALSE) { + rtw_list_delete(&psta->asoc_list); + pstapriv->asoc_list_cnt--; + #ifdef CONFIG_RTW_TOKEN_BASED_XMIT + if (psta->tbtx_enable) + pstapriv->tbtx_asoc_list_cnt--; + #endif + updated = ap_free_sta(padapter, psta, _TRUE, WLAN_REASON_DEAUTH_LEAVING, _TRUE); + + } + _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL); + + associated_clients_update(padapter, updated, STA_INFO_UPDATE_ALL); + + psta = NULL; + + } else { + RTW_INFO("rtw_del_sta(), sta has already been removed or never been added\n"); + + /* ret = -1; */ + } + + + return ret; + +} + +static int rtw_ioctl_get_sta_data(struct net_device *dev, struct ieee_param *param, int len) +{ + int ret = 0; + struct sta_info *psta = NULL; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct sta_priv *pstapriv = &padapter->stapriv; + struct ieee_param_ex *param_ex = (struct ieee_param_ex *)param; + struct sta_data *psta_data = (struct sta_data *)param_ex->data; + + RTW_INFO("rtw_ioctl_get_sta_info, sta_addr: " MAC_FMT "\n", MAC_ARG(param_ex->sta_addr)); + + if (check_fwstate(pmlmepriv, (WIFI_ASOC_STATE | WIFI_AP_STATE)) != _TRUE) + return -EINVAL; + + if (param_ex->sta_addr[0] == 0xff && param_ex->sta_addr[1] == 0xff && + param_ex->sta_addr[2] == 0xff && param_ex->sta_addr[3] == 0xff && + param_ex->sta_addr[4] == 0xff && param_ex->sta_addr[5] == 0xff) + return -EINVAL; + + psta = rtw_get_stainfo(pstapriv, param_ex->sta_addr); + if (psta) { +#if 0 + struct { + u16 aid; + u16 capability; + int flags; + u32 sta_set; + u8 tx_supp_rates[16]; + u32 tx_supp_rates_len; + struct rtw_ieee80211_ht_cap ht_cap; + u64 rx_pkts; + u64 rx_bytes; + u64 rx_drops; + u64 tx_pkts; + u64 tx_bytes; + u64 tx_drops; + } get_sta; +#endif + psta_data->aid = (u16)psta->cmn.aid; + psta_data->capability = psta->capability; + psta_data->flags = psta->flags; + + /* + nonerp_set : BIT(0) + no_short_slot_time_set : BIT(1) + no_short_preamble_set : BIT(2) + no_ht_gf_set : BIT(3) + no_ht_set : BIT(4) + ht_20mhz_set : BIT(5) + */ + + psta_data->sta_set = ((psta->nonerp_set) | + (psta->no_short_slot_time_set << 1) | + (psta->no_short_preamble_set << 2) | + (psta->no_ht_gf_set << 3) | + (psta->no_ht_set << 4) | + (psta->ht_20mhz_set << 5)); + + psta_data->tx_supp_rates_len = psta->bssratelen; + _rtw_memcpy(psta_data->tx_supp_rates, psta->bssrateset, psta->bssratelen); +#ifdef CONFIG_80211N_HT + if(padapter->registrypriv.ht_enable && is_supported_ht(padapter->registrypriv.wireless_mode)) + _rtw_memcpy(&psta_data->ht_cap, &psta->htpriv.ht_cap, sizeof(struct rtw_ieee80211_ht_cap)); +#endif /* CONFIG_80211N_HT */ + psta_data->rx_pkts = psta->sta_stats.rx_data_pkts; + psta_data->rx_bytes = psta->sta_stats.rx_bytes; + psta_data->rx_drops = psta->sta_stats.rx_drops; + + psta_data->tx_pkts = psta->sta_stats.tx_pkts; + psta_data->tx_bytes = psta->sta_stats.tx_bytes; + psta_data->tx_drops = psta->sta_stats.tx_drops; + + + } else + ret = -1; + + return ret; + +} + +static int rtw_get_sta_wpaie(struct net_device *dev, struct ieee_param *param) +{ + int ret = 0; + struct sta_info *psta = NULL; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct sta_priv *pstapriv = &padapter->stapriv; + + RTW_INFO("rtw_get_sta_wpaie, sta_addr: " MAC_FMT "\n", MAC_ARG(param->sta_addr)); + + if (check_fwstate(pmlmepriv, (WIFI_ASOC_STATE | WIFI_AP_STATE)) != _TRUE) + return -EINVAL; + + if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff && + param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff && + param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) + return -EINVAL; + + psta = rtw_get_stainfo(pstapriv, param->sta_addr); + if (psta) { + if ((psta->wpa_ie[0] == WLAN_EID_RSN) || (psta->wpa_ie[0] == WLAN_EID_GENERIC)) { + int wpa_ie_len; + int copy_len; + + wpa_ie_len = psta->wpa_ie[1]; + + copy_len = ((wpa_ie_len + 2) > sizeof(psta->wpa_ie)) ? (sizeof(psta->wpa_ie)) : (wpa_ie_len + 2); + + param->u.wpa_ie.len = copy_len; + + _rtw_memcpy(param->u.wpa_ie.reserved, psta->wpa_ie, copy_len); + } else { + /* ret = -1; */ + RTW_INFO("sta's wpa_ie is NONE\n"); + } + } else + ret = -1; + + return ret; + +} + +static int rtw_set_wps_beacon(struct net_device *dev, struct ieee_param *param, int len) +{ + int ret = 0; + unsigned char wps_oui[4] = {0x0, 0x50, 0xf2, 0x04}; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + int ie_len; + + RTW_INFO("%s, len=%d\n", __FUNCTION__, len); + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) != _TRUE) + return -EINVAL; + + ie_len = len - 12 - 2; /* 12 = param header, 2:no packed */ + + + if (pmlmepriv->wps_beacon_ie) { + rtw_mfree(pmlmepriv->wps_beacon_ie, pmlmepriv->wps_beacon_ie_len); + pmlmepriv->wps_beacon_ie = NULL; + } + + if (ie_len > 0) { + pmlmepriv->wps_beacon_ie = rtw_malloc(ie_len); + pmlmepriv->wps_beacon_ie_len = ie_len; + if (pmlmepriv->wps_beacon_ie == NULL) { + RTW_INFO("%s()-%d: rtw_malloc() ERROR!\n", __FUNCTION__, __LINE__); + return -EINVAL; + } + + _rtw_memcpy(pmlmepriv->wps_beacon_ie, param->u.bcn_ie.buf, ie_len); + + update_beacon(padapter, _VENDOR_SPECIFIC_IE_, wps_oui, _TRUE, 0); + + pmlmeext->bstart_bss = _TRUE; + + } + + + return ret; + +} + +static int rtw_set_wps_probe_resp(struct net_device *dev, struct ieee_param *param, int len) +{ + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + int ie_len; + + RTW_INFO("%s, len=%d\n", __FUNCTION__, len); + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) != _TRUE) + return -EINVAL; + + ie_len = len - 12 - 2; /* 12 = param header, 2:no packed */ + + + if (pmlmepriv->wps_probe_resp_ie) { + rtw_mfree(pmlmepriv->wps_probe_resp_ie, pmlmepriv->wps_probe_resp_ie_len); + pmlmepriv->wps_probe_resp_ie = NULL; + } + + if (ie_len > 0) { + pmlmepriv->wps_probe_resp_ie = rtw_malloc(ie_len); + pmlmepriv->wps_probe_resp_ie_len = ie_len; + if (pmlmepriv->wps_probe_resp_ie == NULL) { + RTW_INFO("%s()-%d: rtw_malloc() ERROR!\n", __FUNCTION__, __LINE__); + return -EINVAL; + } + _rtw_memcpy(pmlmepriv->wps_probe_resp_ie, param->u.bcn_ie.buf, ie_len); + } + + + return ret; + +} + +static int rtw_set_wps_assoc_resp(struct net_device *dev, struct ieee_param *param, int len) +{ + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + int ie_len; + + RTW_INFO("%s, len=%d\n", __FUNCTION__, len); + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) != _TRUE) + return -EINVAL; + + ie_len = len - 12 - 2; /* 12 = param header, 2:no packed */ + + + if (pmlmepriv->wps_assoc_resp_ie) { + rtw_mfree(pmlmepriv->wps_assoc_resp_ie, pmlmepriv->wps_assoc_resp_ie_len); + pmlmepriv->wps_assoc_resp_ie = NULL; + } + + if (ie_len > 0) { + pmlmepriv->wps_assoc_resp_ie = rtw_malloc(ie_len); + pmlmepriv->wps_assoc_resp_ie_len = ie_len; + if (pmlmepriv->wps_assoc_resp_ie == NULL) { + RTW_INFO("%s()-%d: rtw_malloc() ERROR!\n", __FUNCTION__, __LINE__); + return -EINVAL; + } + + _rtw_memcpy(pmlmepriv->wps_assoc_resp_ie, param->u.bcn_ie.buf, ie_len); + } + + + return ret; + +} + +static int rtw_set_hidden_ssid(struct net_device *dev, struct ieee_param *param, int len) +{ + int ret = 0; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *mlmepriv = &(adapter->mlmepriv); + struct mlme_ext_priv *mlmeext = &(adapter->mlmeextpriv); + struct mlme_ext_info *mlmeinfo = &(mlmeext->mlmext_info); + int ie_len; + u8 *ssid_ie; + char ssid[NDIS_802_11_LENGTH_SSID + 1]; + sint ssid_len = 0; + u8 ignore_broadcast_ssid; + + if (check_fwstate(mlmepriv, WIFI_AP_STATE) != _TRUE) + return -EPERM; + + if (param->u.bcn_ie.reserved[0] != 0xea) + return -EINVAL; + + mlmeinfo->hidden_ssid_mode = ignore_broadcast_ssid = param->u.bcn_ie.reserved[1]; + + ie_len = len - 12 - 2; /* 12 = param header, 2:no packed */ + ssid_ie = rtw_get_ie(param->u.bcn_ie.buf, WLAN_EID_SSID, &ssid_len, ie_len); + + if (ssid_ie && ssid_len > 0 && ssid_len <= NDIS_802_11_LENGTH_SSID) { + WLAN_BSSID_EX *pbss_network = &mlmepriv->cur_network.network; + WLAN_BSSID_EX *pbss_network_ext = &mlmeinfo->network; + + _rtw_memcpy(ssid, ssid_ie + 2, ssid_len); + ssid[ssid_len] = 0x0; + + if (0) + RTW_INFO(FUNC_ADPT_FMT" ssid:(%s,%d), from ie:(%s,%d), (%s,%d)\n", FUNC_ADPT_ARG(adapter), + ssid, ssid_len, + pbss_network->Ssid.Ssid, pbss_network->Ssid.SsidLength, + pbss_network_ext->Ssid.Ssid, pbss_network_ext->Ssid.SsidLength); + + _rtw_memcpy(pbss_network->Ssid.Ssid, (void *)ssid, ssid_len); + pbss_network->Ssid.SsidLength = ssid_len; + _rtw_memcpy(pbss_network_ext->Ssid.Ssid, (void *)ssid, ssid_len); + pbss_network_ext->Ssid.SsidLength = ssid_len; + + if (0) + RTW_INFO(FUNC_ADPT_FMT" after ssid:(%s,%d), (%s,%d)\n", FUNC_ADPT_ARG(adapter), + pbss_network->Ssid.Ssid, pbss_network->Ssid.SsidLength, + pbss_network_ext->Ssid.Ssid, pbss_network_ext->Ssid.SsidLength); + } + + RTW_INFO(FUNC_ADPT_FMT" ignore_broadcast_ssid:%d, %s,%d\n", FUNC_ADPT_ARG(adapter), + ignore_broadcast_ssid, ssid, ssid_len); + + return ret; +} + +#if CONFIG_RTW_MACADDR_ACL +static int rtw_ioctl_acl_remove_sta(struct net_device *dev, struct ieee_param *param, int len) +{ + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) != _TRUE) + return -EINVAL; + + if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff && + param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff && + param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) + return -EINVAL; + + ret = rtw_acl_remove_sta(padapter, RTW_ACL_PERIOD_BSS, param->sta_addr); + + return ret; + +} + +static int rtw_ioctl_acl_add_sta(struct net_device *dev, struct ieee_param *param, int len) +{ + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) != _TRUE) + return -EINVAL; + + if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff && + param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff && + param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) + return -EINVAL; + + ret = rtw_acl_add_sta(padapter, RTW_ACL_PERIOD_BSS, param->sta_addr); + + return ret; + +} + +static int rtw_ioctl_set_macaddr_acl(struct net_device *dev, struct ieee_param *param, int len) +{ + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) != _TRUE) + return -EINVAL; + + rtw_set_macaddr_acl(padapter, RTW_ACL_PERIOD_BSS, param->u.mlme.command); + + return ret; +} +#endif /* CONFIG_RTW_MACADDR_ACL */ + +static int rtw_hostapd_ioctl(struct net_device *dev, struct iw_point *p) +{ + struct ieee_param *param; + int ret = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + /* RTW_INFO("%s\n", __FUNCTION__); */ + + /* + * this function is expect to call in master mode, which allows no power saving + * so, we just check hw_init_completed + */ + + if (!rtw_is_hw_init_completed(padapter)) { + ret = -EPERM; + goto out; + } + + + /* if (p->length < sizeof(struct ieee_param) || !p->pointer){ */ + if (!p->pointer) { + ret = -EINVAL; + goto out; + } + + param = (struct ieee_param *)rtw_malloc(p->length); + if (param == NULL) { + ret = -ENOMEM; + goto out; + } + + if (copy_from_user(param, p->pointer, p->length)) { + rtw_mfree((u8 *)param, p->length); + ret = -EFAULT; + goto out; + } + + /* RTW_INFO("%s, cmd=%d\n", __FUNCTION__, param->cmd); */ + + switch (param->cmd) { + case RTL871X_HOSTAPD_FLUSH: + + ret = rtw_hostapd_sta_flush(dev); + + break; + + case RTL871X_HOSTAPD_ADD_STA: + + ret = rtw_add_sta(dev, param); + + break; + + case RTL871X_HOSTAPD_REMOVE_STA: + + ret = rtw_del_sta(dev, param); + + break; + + case RTL871X_HOSTAPD_SET_BEACON: + + ret = rtw_set_beacon(dev, param, p->length); + + break; + + case RTL871X_SET_ENCRYPTION: + + ret = rtw_set_encryption(dev, param, p->length); + + break; + + case RTL871X_HOSTAPD_GET_WPAIE_STA: + + ret = rtw_get_sta_wpaie(dev, param); + + break; + + case RTL871X_HOSTAPD_SET_WPS_BEACON: + + ret = rtw_set_wps_beacon(dev, param, p->length); + + break; + + case RTL871X_HOSTAPD_SET_WPS_PROBE_RESP: + + ret = rtw_set_wps_probe_resp(dev, param, p->length); + + break; + + case RTL871X_HOSTAPD_SET_WPS_ASSOC_RESP: + + ret = rtw_set_wps_assoc_resp(dev, param, p->length); + + break; + + case RTL871X_HOSTAPD_SET_HIDDEN_SSID: + + ret = rtw_set_hidden_ssid(dev, param, p->length); + + break; + + case RTL871X_HOSTAPD_GET_INFO_STA: + + ret = rtw_ioctl_get_sta_data(dev, param, p->length); + + break; + +#if CONFIG_RTW_MACADDR_ACL + case RTL871X_HOSTAPD_SET_MACADDR_ACL: + ret = rtw_ioctl_set_macaddr_acl(dev, param, p->length); + break; + case RTL871X_HOSTAPD_ACL_ADD_STA: + ret = rtw_ioctl_acl_add_sta(dev, param, p->length); + break; + case RTL871X_HOSTAPD_ACL_REMOVE_STA: + ret = rtw_ioctl_acl_remove_sta(dev, param, p->length); + break; +#endif /* CONFIG_RTW_MACADDR_ACL */ + + default: + RTW_INFO("Unknown hostapd request: %d\n", param->cmd); + ret = -EOPNOTSUPP; + break; + + } + + if (ret == 0 && copy_to_user(p->pointer, param, p->length)) + ret = -EFAULT; + + + rtw_mfree((u8 *)param, p->length); + +out: + + return ret; + +} +#endif /* CONFIG_AP_MODE */ + +static int rtw_wx_set_priv(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *awrq, + char *extra) +{ + +#ifdef CONFIG_DEBUG_RTW_WX_SET_PRIV + char *ext_dbg; +#endif + + int ret = 0; + int len = 0; + char *ext; +#ifdef CONFIG_RTW_ANDROID + int i; +#endif + + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct iw_point *dwrq = (struct iw_point *)awrq; + + if (dwrq->length == 0) + return -EFAULT; + + len = dwrq->length; + ext = rtw_vmalloc(len); + if (!ext) + return -ENOMEM; + + if (copy_from_user(ext, dwrq->pointer, len)) { + rtw_vmfree(ext, len); + return -EFAULT; + } + + + +#ifdef CONFIG_DEBUG_RTW_WX_SET_PRIV + ext_dbg = rtw_vmalloc(len); + if (!ext_dbg) { + rtw_vmfree(ext, len); + return -ENOMEM; + } + + _rtw_memcpy(ext_dbg, ext, len); +#endif + + /* added for wps2.0 @20110524 */ + if (dwrq->flags == 0x8766 && len > 8) { + u32 cp_sz; + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + u8 *probereq_wpsie = ext; + int probereq_wpsie_len = len; + u8 wps_oui[4] = {0x0, 0x50, 0xf2, 0x04}; + + if ((_VENDOR_SPECIFIC_IE_ == probereq_wpsie[0]) && + (_rtw_memcmp(&probereq_wpsie[2], wps_oui, 4) == _TRUE)) { + cp_sz = probereq_wpsie_len > MAX_WPS_IE_LEN ? MAX_WPS_IE_LEN : probereq_wpsie_len; + + if (pmlmepriv->wps_probe_req_ie) { + u32 free_len = pmlmepriv->wps_probe_req_ie_len; + pmlmepriv->wps_probe_req_ie_len = 0; + rtw_mfree(pmlmepriv->wps_probe_req_ie, free_len); + pmlmepriv->wps_probe_req_ie = NULL; + } + + pmlmepriv->wps_probe_req_ie = rtw_malloc(cp_sz); + if (pmlmepriv->wps_probe_req_ie == NULL) { + printk("%s()-%d: rtw_malloc() ERROR!\n", __FUNCTION__, __LINE__); + ret = -EINVAL; + goto FREE_EXT; + + } + + _rtw_memcpy(pmlmepriv->wps_probe_req_ie, probereq_wpsie, cp_sz); + pmlmepriv->wps_probe_req_ie_len = cp_sz; + + } + + goto FREE_EXT; + + } + + if (len >= WEXT_CSCAN_HEADER_SIZE + && _rtw_memcmp(ext, WEXT_CSCAN_HEADER, WEXT_CSCAN_HEADER_SIZE) == _TRUE + ) { + ret = rtw_wx_set_scan(dev, info, awrq, ext); + goto FREE_EXT; + } + +#ifdef CONFIG_RTW_ANDROID + /* RTW_INFO("rtw_wx_set_priv: %s req=%s\n", dev->name, ext); */ + + i = rtw_android_cmdstr_to_num(ext); + + switch (i) { + case ANDROID_WIFI_CMD_START: + indicate_wx_custom_event(padapter, "START"); + break; + case ANDROID_WIFI_CMD_STOP: + indicate_wx_custom_event(padapter, "STOP"); + break; + case ANDROID_WIFI_CMD_RSSI: { + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct wlan_network *pcur_network = &pmlmepriv->cur_network; + + if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _TRUE) + sprintf(ext, "%s rssi %d", pcur_network->network.Ssid.Ssid, padapter->recvpriv.rssi); + else + sprintf(ext, "OK"); + } + break; + case ANDROID_WIFI_CMD_LINKSPEED: { + u16 mbps = rtw_get_cur_max_rate(padapter) / 10; + sprintf(ext, "LINKSPEED %d", mbps); + } + break; + case ANDROID_WIFI_CMD_MACADDR: + sprintf(ext, "MACADDR = " MAC_FMT, MAC_ARG(dev->dev_addr)); + break; + case ANDROID_WIFI_CMD_SCAN_ACTIVE: { + /* rtw_set_scan_mode(padapter, SCAN_ACTIVE); */ + sprintf(ext, "OK"); + } + break; + case ANDROID_WIFI_CMD_SCAN_PASSIVE: { + /* rtw_set_scan_mode(padapter, SCAN_PASSIVE); */ + sprintf(ext, "OK"); + } + break; + + case ANDROID_WIFI_CMD_COUNTRY: { + char country_code[10]; + sscanf(ext, "%*s %s", country_code); + rtw_set_country(padapter, country_code); + sprintf(ext, "OK"); + } + break; + default: + #ifdef CONFIG_DEBUG_RTW_WX_SET_PRIV + RTW_INFO("%s: %s unknowned req=%s\n", __FUNCTION__, + dev->name, ext_dbg); + #endif + + sprintf(ext, "OK"); + + } + + if (copy_to_user(dwrq->pointer, ext, min(dwrq->length, (u16)(strlen(ext) + 1)))) + ret = -EFAULT; + +#ifdef CONFIG_DEBUG_RTW_WX_SET_PRIV + RTW_INFO("%s: %s req=%s rep=%s dwrq->length=%d, strlen(ext)+1=%d\n", __FUNCTION__, + dev->name, ext_dbg , ext, dwrq->length, (u16)(strlen(ext) + 1)); +#endif +#endif /* end of CONFIG_ANDROID */ + + +FREE_EXT: + + rtw_vmfree(ext, len); + #ifdef CONFIG_DEBUG_RTW_WX_SET_PRIV + rtw_vmfree(ext_dbg, len); + #endif + + /* RTW_INFO("rtw_wx_set_priv: (SIOCSIWPRIV) %s ret=%d\n", */ + /* dev->name, ret); */ + + return ret; + +} +#endif /*CONFIG_IOCTL_WEXT*/ + +#ifdef CONFIG_WOWLAN +static int rtw_wowlan_ctrl(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wowlan_ioctl_param poidparam; + struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + int ret = 0; + systime start_time = rtw_get_current_time(); + poidparam.subcode = 0; + + RTW_INFO("+rtw_wowlan_ctrl: %s\n", extra); + + if (!check_fwstate(pmlmepriv, WIFI_ASOC_STATE) && + check_fwstate(pmlmepriv, WIFI_STATION_STATE) && + !WOWLAN_IS_STA_MIX_MODE(padapter)) { +#ifdef CONFIG_PNO_SUPPORT + pwrctrlpriv->wowlan_pno_enable = _TRUE; +#else + RTW_INFO("[%s] WARNING: Please Connect With AP First!!\n", __func__); + goto _rtw_wowlan_ctrl_exit_free; +#endif /* CONFIG_PNO_SUPPORT */ + } + + if (check_fwstate(pmlmepriv, WIFI_UNDER_SURVEY)) + rtw_scan_abort(padapter); + + if (_rtw_memcmp(extra, "enable", 6)) + + + rtw_suspend_common(padapter); + + else if (_rtw_memcmp(extra, "disable", 7)) { +#ifdef CONFIG_USB_HCI + RTW_ENABLE_FUNC(padapter, DF_RX_BIT); + RTW_ENABLE_FUNC(padapter, DF_TX_BIT); +#endif + rtw_resume_common(padapter); + +#ifdef CONFIG_PNO_SUPPORT + pwrctrlpriv->wowlan_pno_enable = _FALSE; +#endif /* CONFIG_PNO_SUPPORT */ + + } else { + RTW_INFO("[%s] Invalid Parameter.\n", __func__); + goto _rtw_wowlan_ctrl_exit_free; + } + /* mutex_lock(&ioctl_mutex); */ +_rtw_wowlan_ctrl_exit_free: + RTW_INFO("-rtw_wowlan_ctrl( subcode = %d)\n", poidparam.subcode); + RTW_PRINT("%s in %d ms\n", __func__, + rtw_get_passing_time_ms(start_time)); + return ret; +} + +/* + * IP filter This pattern if for a frame containing a ip packet: + * AA:AA:AA:AA:AA:AA:BB:BB:BB:BB:BB:BB:CC:CC:DD:-:-:-:-:-:-:-:-:EE:-:-:FF:FF:FF:FF:GG:GG:GG:GG:HH:HH:II:II + * + * A: Ethernet destination address + * B: Ethernet source address + * C: Ethernet protocol type + * D: IP header VER+Hlen, use: 0x45 (4 is for ver 4, 5 is for len 20) + * E: IP protocol + * F: IP source address ( 192.168.0.4: C0:A8:00:2C ) + * G: IP destination address ( 192.168.0.4: C0:A8:00:2C ) + * H: Source port (1024: 04:00) + * I: Destination port (1024: 04:00) + */ + +static int rtw_wowlan_set_pattern(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct wowlan_ioctl_param poidparam; + int ret = 0; + u8 input[MAX_IN_PATTERN_SIZE]; + u8 index = 0; + + poidparam.subcode = 0; + + if (!check_fwstate(pmlmepriv, WIFI_ASOC_STATE) && + check_fwstate(pmlmepriv, WIFI_STATION_STATE)) { + ret = -EFAULT; + RTW_INFO("Please Connect With AP First!!\n"); + goto _rtw_wowlan_set_pattern_exit; + } + + if ((wrqu->data.length <= 0) || (wrqu->data.length > MAX_IN_PATTERN_SIZE)) { + ret = -EFAULT; + RTW_INFO("ERROR: parameter length error, len=%d\n", wrqu->data.length); + goto _rtw_wowlan_set_pattern_exit; + } else { + /* set pattern */ + if (copy_from_user(input, + wrqu->data.pointer, wrqu->data.length)) + return -EFAULT; + /* leave PS first */ + rtw_ps_deny(padapter, PS_DENY_IOCTL); + LeaveAllPowerSaveModeDirect(padapter); + if ((strncmp(input, "pattern=", 8) == 0) ||(strncmp(input, "ack_pattern=", 12) == 0)) { + if (pwrpriv->wowlan_pattern_idx >= MAX_WKFM_CAM_NUM) { + RTW_INFO("WARNING: priv-pattern is full(idx: %d)\n", + pwrpriv->wowlan_pattern_idx); + RTW_INFO("WARNING: please clean priv-pattern first\n"); + ret = -EINVAL; + goto _rtw_wowlan_set_pattern_exit; + } else { + index = pwrpriv->wowlan_pattern_idx; + ret = rtw_wowlan_parser_pattern_cmd(input, + pwrpriv->patterns[index].content, + &pwrpriv->patterns[index].len, + pwrpriv->patterns[index].mask); + + if (ret == _TRUE) { + #ifdef CONFIG_WOW_KEEP_ALIVE_PATTERN + if(strncmp(input, "ack_pattern=", 12) == 0) + pwrpriv->wowlan_keep_alive_ack_index = index; + else + pwrpriv->wowlan_wake_pattern_index = index; + RTW_INFO("pwrpriv->wowlan_keep_alive_ack_index =%d\n",pwrpriv->wowlan_keep_alive_ack_index); + RTW_INFO("pwrpriv->wowlan_wake_pattern_index =%d\n",pwrpriv->wowlan_wake_pattern_index); + #endif /*CONFIG_WOW_KEEP_ALIVE_PATTERN*/ + pwrpriv->wowlan_pattern_idx++; + } + } + } else if (strncmp(input, "clean", 5) == 0) { + poidparam.subcode = WOWLAN_PATTERN_CLEAN; + #ifdef CONFIG_WOW_KEEP_ALIVE_PATTERN + pwrpriv->wowlan_wake_pattern_index = 0xFF; + pwrpriv->wowlan_keep_alive_ack_index = 0xFF; + #endif /*CONFIG_WOW_KEEP_ALIVE_PATTERN*/ + + rtw_hal_set_hwreg(padapter, + HW_VAR_WOWLAN, (u8 *)&poidparam); + } else if (strncmp(input, "show", 4) == 0) { + rtw_wow_pattern_cam_dump(padapter); + rtw_wow_pattern_sw_dump(padapter); + } else { + RTW_INFO("ERROR: incorrect parameter!\n"); + ret = -EINVAL; + } + rtw_ps_deny_cancel(padapter, PS_DENY_IOCTL); + } +_rtw_wowlan_set_pattern_exit: + return ret; +} + +#ifdef CONFIG_WOW_KEEP_ALIVE_PATTERN +static int rtw_wowlan_set_keep_alive_pattern(struct net_device *dev, struct iw_request_info *info, union iwreq_data *wrqu, char *extra) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + + int ret = 0,totoal_len=0,i=0,len=0; + char *cp = NULL; + u32 mode = 0xFF; /*para1*/ + u16 period = 15*10; /* para2;units:100ms,default 15s*/ + char tx_pattern[512]; /*para3*/ + u32 retry_intervel = 2*10; /* para4;units:100ms,default 2s*/ + u32 retry_limit_count = 5; /*para5*/ + + ret = sscanf(extra, "%d %hu %s %d %d", &mode , &period, tx_pattern, &retry_intervel, &retry_limit_count); + pwrpriv->wowlan_keep_alive_mode = mode; + + RTW_INFO("[%s] ret =%d \n", __func__ ,ret); + totoal_len = strlen(tx_pattern); + RTW_INFO("[%s] totoal_len=%d \n", __func__ ,totoal_len); + + if (mode && (ret < 3)) + return -EINVAL; + + if (((mode ==2) ||(mode ==3)) && ((retry_intervel*retry_limit_count) > period)) { + RTW_INFO("[%s] retry_intervel*retry_limit_count need smaller than period\n", __func__ ); + return -EINVAL; + } + + switch(mode){ + case wow_keep_alive_pattern_disable: + /*disable this feature*/ + pwrpriv->keep_alive_pattern_loc = 0; + pwrpriv->keep_alive_pattern_len = 0; + pwrpriv->wowlan_keep_alive_period = 0; + pwrpriv->wowlan_keep_alive_ack_index = 0xFF; + pwrpriv->wowlan_wake_pattern_index = 0xFF; + pwrpriv->wowlan_keep_alive_retry_interval = 0; + pwrpriv->wowlan_keep_alive_retry_counter = 0; + _rtw_memset(pwrpriv->keep_alive_pattern,0,WLAN_MAX_KEEP_ALIVE_IE_LEN); + RTW_INFO("[%s] clear pattern \n", __func__ ); + ret = _SUCCESS; + break; + case wow_keep_alive_pattern_tx: + /*only tx udp packet*/ + pwrpriv->wowlan_keep_alive_period = period; + pwrpriv->wowlan_keep_alive_retry_interval = 0; + pwrpriv->wowlan_keep_alive_retry_counter = 0; + RTW_INFO("[%s] wow_keep_alive_pattern_tx \n", __func__ ); + break; + case wow_keep_alive_pattern_trx: + /*trx+no need wakeup*/ + pwrpriv->wowlan_keep_alive_period = period; + pwrpriv->wowlan_keep_alive_retry_interval = retry_intervel; + pwrpriv->wowlan_keep_alive_retry_counter = retry_limit_count; + RTW_INFO("[%s] wow_keep_alive_pattern_trx \n", __func__ ); + break; + case wow_keep_alive_pattern_trx_with_ack: + /*trx+need wakeup*/ + pwrpriv->wowlan_keep_alive_period = period; + pwrpriv->wowlan_keep_alive_retry_interval = retry_intervel; + pwrpriv->wowlan_keep_alive_retry_counter = retry_limit_count; + RTW_INFO("[%s] wow_keep_alive_pattern_trx_with_ack \n", __func__ ); + break; + default: + RTW_INFO("[%s] please setting valid mode \n", __func__ ); + ret = -EINVAL; + break; + + } + + if((mode == 0) || (mode > 4)) + return ret; + + totoal_len = strlen(tx_pattern); + RTW_INFO("[%s] totoal_len=%d \n", __func__ ,totoal_len); + if (totoal_len > WLAN_MAX_KEEP_ALIVE_IE_LEN*2) { + RTW_INFO("[%s] Fail , not support ie length extend %d\n", __func__ , WLAN_MAX_KEEP_ALIVE_IE_LEN); + return -EFAULT; + } + RTW_INFO("[%s] period = %hu ,ie = %s , len = %d\n", __func__ , period , tx_pattern , totoal_len); + + + if (totoal_len > 0) { + RTW_INFO("[%s] pwrpriv->keep_alive_pattern==========> \n", __func__ ); + for (i = 0 ; i keep_alive_pattern[len] = key_2char2num(tx_pattern[i], tx_pattern[i + 1]); + RTW_INFO("[0x%x] ",pwrpriv->keep_alive_pattern[len]); + len++; + } + RTW_INFO(" \n" ); + pwrpriv->keep_alive_pattern_len = len; + } + + return ret; +} +#endif /*CONFIG_WOW_KEEP_ALIVE_PATTERN*/ + +#endif /* CONFIG_WOWLAN */ + +#ifdef CONFIG_AP_WOWLAN +static int rtw_ap_wowlan_ctrl(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct wowlan_ioctl_param poidparam; + struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct sta_info *psta = NULL; + int ret = 0; + systime start_time = rtw_get_current_time(); + poidparam.subcode = 0; + + RTW_INFO("+rtw_ap_wowlan_ctrl: %s\n", extra); + + if (!check_fwstate(pmlmepriv, WIFI_AP_STATE)) { + RTW_INFO("[%s] It is not AP mode!!\n", __func__); + goto _rtw_ap_wowlan_ctrl_exit_free; + } + + if (_rtw_memcmp(extra, "enable", 6)) { + + pwrctrlpriv->wowlan_ap_mode = _TRUE; + + rtw_suspend_common(padapter); + } else if (_rtw_memcmp(extra, "disable", 7)) { +#ifdef CONFIG_USB_HCI + RTW_ENABLE_FUNC(padapter, DF_RX_BIT); + RTW_ENABLE_FUNC(padapter, DF_TX_BIT); +#endif + rtw_resume_common(padapter); + } else { + RTW_INFO("[%s] Invalid Parameter.\n", __func__); + goto _rtw_ap_wowlan_ctrl_exit_free; + } + /* mutex_lock(&ioctl_mutex); */ +_rtw_ap_wowlan_ctrl_exit_free: + RTW_INFO("-rtw_ap_wowlan_ctrl( subcode = %d)\n", poidparam.subcode); + RTW_PRINT("%s in %d ms\n", __func__, + rtw_get_passing_time_ms(start_time)); +_rtw_ap_wowlan_ctrl_exit: + return ret; +} +#endif /* CONFIG_AP_WOWLAN */ + +static int rtw_pm_set(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + unsigned mode = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + RTW_INFO("[%s] extra = %s\n", __FUNCTION__, extra); + + if (_rtw_memcmp(extra, "lps=", 4)) { + sscanf(extra + 4, "%u", &mode); + ret = rtw_pm_set_lps(padapter, mode); + } else if (_rtw_memcmp(extra, "ips=", 4)) { + sscanf(extra + 4, "%u", &mode); + ret = rtw_pm_set_ips(padapter, mode); + } else if (_rtw_memcmp(extra, "lps_level=", 10)) { + if (sscanf(extra + 10, "%u", &mode) > 0) + ret = rtw_pm_set_lps_level(padapter, mode); +#ifdef CONFIG_LPS_1T1R + } else if (_rtw_memcmp(extra, "lps_1t1r=", 9)) { + if (sscanf(extra + 9, "%u", &mode) > 0) + ret = rtw_pm_set_lps_1t1r(padapter, mode); +#endif + } +#ifdef CONFIG_WOWLAN + else if (_rtw_memcmp(extra, "wow_lps=", 8)) { + sscanf(extra + 8, "%u", &mode); + ret = rtw_pm_set_wow_lps(padapter, mode); + } else if (_rtw_memcmp(extra, "wow_lps_level=", 14)) { + if (sscanf(extra + 14, "%u", &mode) > 0) + ret = rtw_pm_set_wow_lps_level(padapter, mode); + #ifdef CONFIG_LPS_1T1R + } else if (_rtw_memcmp(extra, "wow_lps_1t1r=", 13)) { + if (sscanf(extra + 13, "%u", &mode) > 0) + ret = rtw_pm_set_wow_lps_1t1r(padapter, mode); + #endif + } +#endif /* CONFIG_WOWLAN */ + else + ret = -EINVAL; + + return ret; +} +#ifdef CONFIG_APPEND_VENDOR_IE_ENABLE + +int rtw_vendor_ie_get_raw_data(struct net_device *dev, u32 vendor_ie_num, + char *extra, u32 length) +{ + int j; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + u32 vendor_ie_mask = 0; + char *pstring; + + if (vendor_ie_num >= WLAN_MAX_VENDOR_IE_NUM) { + RTW_INFO("[%s] only support %d vendor ie\n", __func__ , + WLAN_MAX_VENDOR_IE_NUM); + return -EFAULT; + } + + if (pmlmepriv->vendor_ielen[vendor_ie_num] == 0) { + RTW_INFO("[%s] Fail, vendor_ie_num: %d is not set\n", __func__, + vendor_ie_num); + return -EFAULT; + } + + if (length < 2 * pmlmepriv->vendor_ielen[vendor_ie_num] + 5) { + RTW_INFO("[%s] Fail, buffer size is too small\n", __func__); + return -EFAULT; + } + + vendor_ie_mask = pmlmepriv->vendor_ie_mask[vendor_ie_num]; + _rtw_memset(extra, 0, length); + + pstring = extra; + pstring += sprintf(pstring, "%d,%x,", vendor_ie_num, vendor_ie_mask); + + for (j = 0; j < pmlmepriv->vendor_ielen[vendor_ie_num]; j++) + pstring += sprintf(pstring, "%02x", pmlmepriv->vendor_ie[vendor_ie_num][j]); + + length = pstring - extra; + return length; +} + +int rtw_vendor_ie_get_data(struct net_device *dev, int vendor_ie_num, char *extra) +{ + int j; + char *pstring; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + u32 vendor_ie_mask = 0; + __u16 length = 0; + + vendor_ie_mask = pmlmepriv->vendor_ie_mask[vendor_ie_num]; + pstring = extra; + pstring += sprintf(pstring , "\nVendor IE num %d , Mask:%x " , vendor_ie_num , vendor_ie_mask); + + if (vendor_ie_mask & WIFI_BEACON_VENDOR_IE_BIT) + pstring += sprintf(pstring , "[Beacon]"); + if (vendor_ie_mask & WIFI_PROBEREQ_VENDOR_IE_BIT) + pstring += sprintf(pstring , "[Probe Req]"); + if (vendor_ie_mask & WIFI_PROBERESP_VENDOR_IE_BIT) + pstring += sprintf(pstring , "[Probe Resp]"); + if (vendor_ie_mask & WIFI_ASSOCREQ_VENDOR_IE_BIT) + pstring += sprintf(pstring , "[Assoc Req]"); + if (vendor_ie_mask & WIFI_ASSOCRESP_VENDOR_IE_BIT) + pstring += sprintf(pstring , "[Assoc Resp]"); +#ifdef CONFIG_P2P + if (vendor_ie_mask & WIFI_P2P_PROBEREQ_VENDOR_IE_BIT) + pstring += sprintf(pstring , "[P2P_Probe Req]"); + if (vendor_ie_mask & WIFI_P2P_PROBERESP_VENDOR_IE_BIT) + pstring += sprintf(pstring , "[P2P_Probe Resp]"); +#endif + + pstring += sprintf(pstring , "\nVendor IE:\n"); + for (j = 0 ; j < pmlmepriv->vendor_ielen[vendor_ie_num] ; j++) + pstring += sprintf(pstring , "%02x" , pmlmepriv->vendor_ie[vendor_ie_num][j]); + + length = pstring - extra; + return length; + +} + +int rtw_vendor_ie_get(struct net_device *dev, struct iw_request_info *info, union iwreq_data *wrqu, char *extra) +{ + int ret = 0, vendor_ie_num = 0, cmdlen; + struct iw_point *p; + u8 *ptmp; + + p = &wrqu->data; + cmdlen = p->length; + if (0 == cmdlen) + return -EINVAL; + + ptmp = (u8 *)rtw_malloc(cmdlen); + if (NULL == ptmp) + return -ENOMEM; + + if (copy_from_user(ptmp, p->pointer, cmdlen)) { + ret = -EFAULT; + goto exit; + } + ret = sscanf(ptmp , "%d", &vendor_ie_num); + if (vendor_ie_num > WLAN_MAX_VENDOR_IE_NUM - 1) { + ret = -EFAULT; + goto exit; + } + + wrqu->data.length = rtw_vendor_ie_get_data(dev, vendor_ie_num, extra); + +exit: + rtw_mfree(ptmp, cmdlen); + + return 0; +} + +int rtw_vendor_ie_set(struct net_device *dev, struct iw_request_info *info, union iwreq_data *wrqu, char *extra) +{ + int ret = 0, i , len = 0 , totoal_ie_len = 0 , total_ie_len_byte = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + u32 vendor_ie_mask = 0; + u32 vendor_ie_num = 0; + u32 vendor_ie_mask_max = BIT(WLAN_MAX_VENDOR_IE_MASK_MAX) - 1; + u32 id, elen; + + ret = sscanf(extra, "%d,%x,%*s", &vendor_ie_num , &vendor_ie_mask); + if (strrchr(extra , ',')) + extra = strrchr(extra , ',') + 1; + else + return -EINVAL; + totoal_ie_len = strlen(extra); + RTW_INFO("[%s] vendor_ie_num = %d , vendor_ie_mask = 0x%x , vendor_ie = %s , len = %d\n", __func__ , vendor_ie_num , vendor_ie_mask , extra , totoal_ie_len); + + if (vendor_ie_num > WLAN_MAX_VENDOR_IE_NUM - 1) { + RTW_INFO("[%s] Fail, only support %d vendor ie\n", __func__ , WLAN_MAX_VENDOR_IE_NUM); + return -EFAULT; + } + + if (totoal_ie_len > WLAN_MAX_VENDOR_IE_LEN) { + RTW_INFO("[%s] Fail , not support ie length extend %d\n", __func__ , WLAN_MAX_VENDOR_IE_LEN); + return -EFAULT; + } + + if (vendor_ie_mask > vendor_ie_mask_max) { + RTW_INFO("[%s] Fail, not support vendor_ie_mask more than 0x%x\n", __func__ , vendor_ie_mask_max); + return -EFAULT; + } + + if (vendor_ie_mask == 0) { + RTW_INFO("[%s] Clear vendor_ie_num %d group\n", __func__ , vendor_ie_num); + goto _clear_path; + } + + if (totoal_ie_len % 2 != 0) { + RTW_INFO("[%s] Fail , IE length = %zu is odd\n" , __func__ , strlen(extra)); + return -EFAULT; + } + + if (totoal_ie_len > 0) { + for (i = 0 ; i < strlen(extra) ; i += 2) { + pmlmepriv->vendor_ie[vendor_ie_num][len] = key_2char2num(extra[i] , extra[i + 1]); + if (len == 0) { + id = pmlmepriv->vendor_ie[vendor_ie_num][len]; + if (id != WLAN_EID_VENDOR_SPECIFIC) { + RTW_INFO("[%s] Fail , VENDOR SPECIFIC IE ID \"%x\" was not correct\n", __func__ , id); + goto _clear_path; + } + } else if (len == 1) { + total_ie_len_byte = (totoal_ie_len / 2) - 2; + elen = pmlmepriv->vendor_ie[vendor_ie_num][len]; + if (elen != total_ie_len_byte) { + RTW_INFO("[%s] Fail , Input IE length = \"%d\"(hex:%x) bytes , not match input total IE context length \"%d\" bytes\n", __func__ , elen , elen , + total_ie_len_byte); + goto _clear_path; + } + } + len++; + } + pmlmepriv->vendor_ielen[vendor_ie_num] = len; + } else + pmlmepriv->vendor_ielen[vendor_ie_num] = 0; + + + + if (vendor_ie_mask & WIFI_BEACON_VENDOR_IE_BIT) + RTW_INFO("[%s] Beacon append vendor ie\n", __func__); + if (vendor_ie_mask & WIFI_PROBEREQ_VENDOR_IE_BIT) + RTW_INFO("[%s] Probe Req append vendor ie\n", __func__); + if (vendor_ie_mask & WIFI_PROBERESP_VENDOR_IE_BIT) + RTW_INFO("[%s] Probe Resp append vendor ie\n", __func__); + if (vendor_ie_mask & WIFI_ASSOCREQ_VENDOR_IE_BIT) + RTW_INFO("[%s] Assoc Req append vendor ie\n", __func__); + if (vendor_ie_mask & WIFI_ASSOCRESP_VENDOR_IE_BIT) + RTW_INFO("[%s] Assoc Resp append vendor ie\n", __func__); +#ifdef CONFIG_P2P + if (vendor_ie_mask & WIFI_P2P_PROBEREQ_VENDOR_IE_BIT) + RTW_INFO("[%s] P2P Probe Req append vendor ie\n", __func__); + if (vendor_ie_mask & WIFI_P2P_PROBERESP_VENDOR_IE_BIT) + RTW_INFO("[%s] P2P Probe Resp append vendor ie\n", __func__); +#endif + + pmlmepriv->vendor_ie_mask[vendor_ie_num] = vendor_ie_mask; + + return ret; + +_clear_path: + _rtw_memset(pmlmepriv->vendor_ie[vendor_ie_num] , 0 , sizeof(u32) * WLAN_MAX_VENDOR_IE_LEN); + pmlmepriv->vendor_ielen[vendor_ie_num] = 0; + pmlmepriv->vendor_ie_mask[vendor_ie_num] = 0; + return -EFAULT; +} +#endif + +static int rtw_mp_efuse_get(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wdata, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + + PEFUSE_HAL pEfuseHal; + struct iw_point *wrqu; + + u8 ips_mode = IPS_NUM; /* init invalid value */ + u8 lps_mode = PS_MODE_NUM; /* init invalid value */ + struct pwrctrl_priv *pwrctrlpriv ; + u8 *data = NULL; + u8 *rawdata = NULL; + char *pch, *ptmp, *token, *tmp[3] = {0x00, 0x00, 0x00}; + u16 i = 0, j = 0, mapLen = 0, addr = 0, cnts = 0; + u16 max_available_len = 0, raw_cursize = 0, raw_maxsize = 0; + u16 mask_len; + u8 mask_buf[64] = ""; + int err; + char *pextra = NULL; +#ifdef CONFIG_IOL + u8 org_fw_iol = padapter->registrypriv.fw_iol;/* 0:Disable, 1:enable, 2:by usb speed */ +#endif + + wrqu = (struct iw_point *)wdata; + pwrctrlpriv = adapter_to_pwrctl(padapter); + pEfuseHal = &pHalData->EfuseHal; + + err = 0; + data = rtw_zmalloc(EFUSE_BT_MAX_MAP_LEN); + if (data == NULL) { + err = -ENOMEM; + goto exit; + } + rawdata = rtw_zmalloc(EFUSE_BT_MAX_MAP_LEN); + if (rawdata == NULL) { + err = -ENOMEM; + goto exit; + } + + if (copy_from_user(extra, wrqu->pointer, wrqu->length)) { + err = -EFAULT; + goto exit; + } + + *(extra + wrqu->length) = '\0'; + +#ifdef CONFIG_LPS + lps_mode = pwrctrlpriv->power_mgnt;/* keep org value */ + rtw_pm_set_lps(padapter, PS_MODE_ACTIVE); +#endif + +#ifdef CONFIG_IPS + ips_mode = pwrctrlpriv->ips_mode;/* keep org value */ + rtw_pm_set_ips(padapter, IPS_NONE); +#endif + + pch = extra; + RTW_INFO("%s: in=%s\n", __FUNCTION__, extra); + + i = 0; + /* mac 16 "00e04c871200" rmap,00,2 */ + while ((token = strsep(&pch, ",")) != NULL) { + if (i > 2) + break; + tmp[i] = token; + i++; + } +#ifdef CONFIG_IOL + padapter->registrypriv.fw_iol = 0;/* 0:Disable, 1:enable, 2:by usb speed */ +#endif + + if (strcmp(tmp[0], "status") == 0) { + sprintf(extra, "Load File efuse=%s,Load File MAC=%s" + , pHalData->efuse_file_status == EFUSE_FILE_FAILED ? "FAIL" : "OK" + , pHalData->macaddr_file_status == MACADDR_FILE_FAILED ? "FAIL" : "OK" + ); + goto exit; + } else if (strcmp(tmp[0], "drvmap") == 0) { + static u8 drvmaporder = 0; + u8 *efuse; + u32 shift, cnt; + u32 blksz = 0x200; /* The size of one time show, default 512 */ + EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (void *)&mapLen, _FALSE); + + efuse = pHalData->efuse_eeprom_data; + + shift = blksz * drvmaporder; + efuse += shift; + cnt = mapLen - shift; + + if (cnt > blksz) { + cnt = blksz; + drvmaporder++; + } else + drvmaporder = 0; + + sprintf(extra, "\n"); + for (i = 0; i < cnt; i += 16) { + pextra = extra + strlen(extra); + pextra += sprintf(pextra, "0x%02x\t", shift + i); + for (j = 0; j < 8; j++) + pextra += sprintf(pextra, "%02X ", efuse[i + j]); + pextra += sprintf(pextra, "\t"); + for (; j < 16; j++) + pextra += sprintf(pextra, "%02X ", efuse[i + j]); + pextra += sprintf(pextra, "\n"); + } + if ((shift + cnt) < mapLen) + pextra += sprintf(pextra, "\t...more (left:%d/%d)\n", mapLen-(shift + cnt), mapLen); + + } else if (strcmp(tmp[0], "realmap") == 0) { + static u8 order = 0; + u8 *efuse; + u32 shift, cnt; + u32 blksz = 0x200; /* The size of one time show, default 512 */ + + EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN , (void *)&mapLen, _FALSE); + efuse = pEfuseHal->fakeEfuseInitMap; + if (rtw_efuse_mask_map_read(padapter, 0, mapLen, efuse) == _FAIL) { + RTW_INFO("%s: read realmap Fail!!\n", __FUNCTION__); + err = -EFAULT; + goto exit; + } + +#if 0 + RTW_INFO("OFFSET\tVALUE(hex)\n"); + for (i = 0; i < mapLen; i += 16) { + RTW_INFO("0x%02x\t", i); + for (j = 0; j < 8; j++) + RTW_INFO("%02X ", efuse[i + j]); + RTW_INFO("\t"); + for (; j < 16; j++) + RTW_INFO("%02X ", efuse[i + j]); + RTW_INFO("\n"); + } + RTW_INFO("\n"); +#endif + + shift = blksz * order; + efuse += shift; + cnt = mapLen - shift; + if (cnt > blksz) { + cnt = blksz; + order++; + } else + order = 0; + + sprintf(extra, "\n"); + for (i = 0; i < cnt; i += 16) { + pextra = extra + strlen(extra); + pextra += sprintf(pextra, "0x%02x\t", shift + i); + for (j = 0; j < 8; j++) + pextra += sprintf(pextra, "%02X ", efuse[i + j]); + pextra += sprintf(pextra, "\t"); + for (; j < 16; j++) + pextra += sprintf(pextra, "%02X ", efuse[i + j]); + pextra += sprintf(pextra, "\n"); + } + if ((shift + cnt) < mapLen) + pextra += sprintf(pextra, "\t...more (left:%d/%d)\n", mapLen-(shift + cnt), mapLen); + } else if (strcmp(tmp[0], "rmap") == 0) { + if ((tmp[1] == NULL) || (tmp[2] == NULL)) { + RTW_INFO("%s: rmap Fail!! Parameters error!\n", __FUNCTION__); + err = -EINVAL; + goto exit; + } + + /* rmap addr cnts */ + addr = simple_strtoul(tmp[1], &ptmp, 16); + RTW_INFO("%s: addr=%x\n", __FUNCTION__, addr); + + cnts = simple_strtoul(tmp[2], &ptmp, 10); + if (cnts == 0) { + RTW_INFO("%s: rmap Fail!! cnts error!\n", __FUNCTION__); + err = -EINVAL; + goto exit; + } + RTW_INFO("%s: cnts=%d\n", __FUNCTION__, cnts); + + EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN , (void *)&max_available_len, _FALSE); + if ((addr + cnts) > max_available_len) { + RTW_INFO("%s: addr(0x%X)+cnts(%d) parameter error!\n", __FUNCTION__, addr, cnts); + err = -EINVAL; + goto exit; + } + + if (rtw_efuse_mask_map_read(padapter, addr, cnts, data) == _FAIL) { + RTW_INFO("%s: rtw_efuse_mask_map_read error!\n", __func__); + err = -EFAULT; + goto exit; + } + + /* RTW_INFO("%s: data={", __FUNCTION__); */ + *extra = 0; + pextra = extra; + for (i = 0; i < cnts; i++) { + /* RTW_INFO("0x%02x ", data[i]); */ + pextra += sprintf(pextra, "0x%02X ", data[i]); + } + /* RTW_INFO("}\n"); */ + } else if (strcmp(tmp[0], "realraw") == 0) { + static u8 raw_order = 0; + u32 shift, cnt; + u32 blksz = 0x200; /* The size of one time show, default 512 */ + + addr = 0; + EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_REAL_CONTENT_LEN , (void *)&mapLen, _FALSE); + RTW_INFO("Real content len = %d\n",mapLen ); + + if (rtw_efuse_access(padapter, _FALSE, addr, mapLen, rawdata) == _FAIL) { + RTW_INFO("%s: rtw_efuse_access Fail!!\n", __func__); + err = -EFAULT; + goto exit; + } + + _rtw_memset(extra, '\0', strlen(extra)); + + shift = blksz * raw_order; + rawdata += shift; + cnt = mapLen - shift; + if (cnt > blksz) { + cnt = blksz; + raw_order++; + } else + raw_order = 0; + + sprintf(extra, "\n"); + for (i = 0; i < cnt; i += 16) { + pextra = extra + strlen(extra); + pextra += sprintf(pextra, "0x%02x\t", shift + i); + for (j = 0; j < 8; j++) + pextra += sprintf(pextra, "%02X ", rawdata[i + j]); + pextra += sprintf(pextra, "\t"); + for (; j < 16; j++) + pextra += sprintf(pextra, "%02X ", rawdata[i + j]); + pextra += sprintf(pextra, "\n"); + } + if ((shift + cnt) < mapLen) + pextra += sprintf(pextra, "\t...more (left:%d/%d)\n", mapLen-(shift + cnt), mapLen); + + } else if (strcmp(tmp[0], "btrealraw") == 0) { + static u8 bt_raw_order = 0; + u32 shift, cnt; + u32 blksz = 0x200; /* The size of one time show, default 512 */ + + addr = 0; + EFUSE_GetEfuseDefinition(padapter, EFUSE_BT, TYPE_EFUSE_REAL_CONTENT_LEN, (void *)&mapLen, _FALSE); + RTW_INFO("Real content len = %d\n", mapLen); +#ifdef RTW_HALMAC + if (rtw_efuse_bt_access(padapter, _FALSE, 0, mapLen, rawdata) == _FAIL) { + RTW_INFO("%s: rtw_efuse_access Fail!!\n", __func__); + err = -EFAULT; + goto exit; + } +#else + rtw_write8(padapter, 0x35, 0x1); + + if (rtw_efuse_access(padapter, _FALSE, addr, mapLen, rawdata) == _FAIL) { + RTW_INFO("%s: rtw_efuse_access Fail!!\n", __func__); + err = -EFAULT; + goto exit; + } +#endif + _rtw_memset(extra, '\0', strlen(extra)); + + shift = blksz * bt_raw_order; + rawdata += shift; + cnt = mapLen - shift; + if (cnt > blksz) { + cnt = blksz; + bt_raw_order++; + } else + bt_raw_order = 0; + + sprintf(extra, "\n"); + for (i = 0; i < cnt; i += 16) { + pextra = extra + strlen(extra); + pextra += sprintf(pextra, "0x%02x\t", shift + i); + for (j = 0; j < 8; j++) + pextra += sprintf(pextra, "%02X ", rawdata[i + j]); + pextra += sprintf(pextra, "\t"); + for (; j < 16; j++) + pextra += sprintf(pextra, "%02X ", rawdata[i + j]); + pextra += sprintf(pextra, "\n"); + } + if ((shift + cnt) < mapLen) + pextra += sprintf(pextra, "\t...more (left:%d/%d)\n", mapLen-(shift + cnt), mapLen); + + } else if (strcmp(tmp[0], "mac") == 0) { + if (hal_efuse_macaddr_offset(padapter) == -1) { + err = -EFAULT; + goto exit; + } + + addr = hal_efuse_macaddr_offset(padapter); + cnts = 6; + + EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (void *)&max_available_len, _FALSE); + if ((addr + cnts) > max_available_len) { + RTW_INFO("%s: addr(0x%02x)+cnts(%d) parameter error!\n", __FUNCTION__, addr, cnts); + err = -EFAULT; + goto exit; + } + + if (rtw_efuse_mask_map_read(padapter, addr, cnts, data) == _FAIL) { + RTW_INFO("%s: rtw_efuse_mask_map_read error!\n", __func__); + err = -EFAULT; + goto exit; + } + + /* RTW_INFO("%s: MAC address={", __FUNCTION__); */ + *extra = 0; + pextra = extra; + for (i = 0; i < cnts; i++) { + /* RTW_INFO("%02X", data[i]); */ + pextra += sprintf(pextra, "%02X", data[i]); + if (i != (cnts - 1)) { + /* RTW_INFO(":"); */ + pextra += sprintf(pextra, ":"); + } + } + /* RTW_INFO("}\n"); */ + } else if (strcmp(tmp[0], "vidpid") == 0) { +#ifdef CONFIG_RTL8188E +#ifdef CONFIG_USB_HCI + addr = EEPROM_VID_88EU; +#endif +#ifdef CONFIG_PCI_HCI + addr = EEPROM_VID_88EE; +#endif +#endif /* CONFIG_RTL8188E */ + +#ifdef CONFIG_RTL8192E +#ifdef CONFIG_USB_HCI + addr = EEPROM_VID_8192EU; +#endif +#ifdef CONFIG_PCI_HCI + addr = EEPROM_VID_8192EE; +#endif +#endif /* CONFIG_RTL8192E */ +#ifdef CONFIG_RTL8723B + addr = EEPROM_VID_8723BU; +#endif /* CONFIG_RTL8192E */ + +#ifdef CONFIG_RTL8188F + addr = EEPROM_VID_8188FU; +#endif /* CONFIG_RTL8188F */ + +#ifdef CONFIG_RTL8188GTV + addr = EEPROM_VID_8188GTVU; +#endif + +#ifdef CONFIG_RTL8703B +#ifdef CONFIG_USB_HCI + addr = EEPROM_VID_8703BU; +#endif +#endif /* CONFIG_RTL8703B */ + +#ifdef CONFIG_RTL8723D +#ifdef CONFIG_USB_HCI + addr = EEPROM_VID_8723DU; +#endif /* CONFIG_USB_HCI */ +#endif /* CONFIG_RTL8723D */ + + cnts = 4; + + EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (void *)&max_available_len, _FALSE); + if ((addr + cnts) > max_available_len) { + RTW_INFO("%s: addr(0x%02x)+cnts(%d) parameter error!\n", __FUNCTION__, addr, cnts); + err = -EFAULT; + goto exit; + } + if (rtw_efuse_mask_map_read(padapter, addr, cnts, data) == _FAIL) { + RTW_INFO("%s: rtw_efuse_access error!!\n", __FUNCTION__); + err = -EFAULT; + goto exit; + } + + /* RTW_INFO("%s: {VID,PID}={", __FUNCTION__); */ + *extra = 0; + pextra = extra; + for (i = 0; i < cnts; i++) { + /* RTW_INFO("0x%02x", data[i]); */ + pextra += sprintf(pextra, "0x%02X", data[i]); + if (i != (cnts - 1)) { + /* RTW_INFO(","); */ + pextra += sprintf(pextra, ","); + } + } + /* RTW_INFO("}\n"); */ + } else if (strcmp(tmp[0], "ableraw") == 0) { +#ifdef RTW_HALMAC + raw_maxsize = efuse_GetavailableSize(padapter); +#else + efuse_GetCurrentSize(padapter, &raw_cursize); + raw_maxsize = efuse_GetMaxSize(padapter); +#endif + sprintf(extra, "[available raw size]= %d bytes\n", raw_maxsize - raw_cursize); + } else if (strcmp(tmp[0], "btableraw") == 0) { + efuse_bt_GetCurrentSize(padapter, &raw_cursize); + raw_maxsize = efuse_bt_GetMaxSize(padapter); + sprintf(extra, "[available raw size]= %d bytes\n", raw_maxsize - raw_cursize); + } else if (strcmp(tmp[0], "btfmap") == 0) { + + BTEfuse_PowerSwitch(padapter, 1, _TRUE); + + mapLen = EFUSE_BT_MAX_MAP_LEN; + if (rtw_BT_efuse_map_read(padapter, 0, mapLen, pEfuseHal->BTEfuseInitMap) == _FAIL) { + RTW_INFO("%s: rtw_BT_efuse_map_read Fail!!\n", __FUNCTION__); + err = -EFAULT; + goto exit; + } + + /* RTW_INFO("OFFSET\tVALUE(hex)\n"); */ + sprintf(extra, "\n"); + for (i = 0; i < 512; i += 16) { /* set 512 because the iwpriv's extra size have limit 0x7FF */ + /* RTW_INFO("0x%03x\t", i); */ + pextra = extra + strlen(extra); + pextra += sprintf(pextra, "0x%03x\t", i); + for (j = 0; j < 8; j++) { + /* RTW_INFO("%02X ", pEfuseHal->BTEfuseInitMap[i+j]); */ + pextra += sprintf(pextra, "%02X ", pEfuseHal->BTEfuseInitMap[i+j]); + } + /* RTW_INFO("\t"); */ + pextra += sprintf(pextra, "\t"); + for (; j < 16; j++) { + /* RTW_INFO("%02X ", pEfuseHal->BTEfuseInitMap[i+j]); */ + pextra += sprintf(pextra, "%02X ", pEfuseHal->BTEfuseInitMap[i+j]); + } + /* RTW_INFO("\n"); */ + pextra += sprintf(pextra, "\n"); + } + /* RTW_INFO("\n"); */ + } else if (strcmp(tmp[0], "btbmap") == 0) { + BTEfuse_PowerSwitch(padapter, 1, _TRUE); + + mapLen = EFUSE_BT_MAX_MAP_LEN; + if (rtw_BT_efuse_map_read(padapter, 0, mapLen, pEfuseHal->BTEfuseInitMap) == _FAIL) { + RTW_INFO("%s: rtw_BT_efuse_map_read Fail!!\n", __FUNCTION__); + err = -EFAULT; + goto exit; + } + + /* RTW_INFO("OFFSET\tVALUE(hex)\n"); */ + sprintf(extra, "\n"); + for (i = 512; i < 1024 ; i += 16) { + /* RTW_INFO("0x%03x\t", i); */ + pextra = extra + strlen(extra); + pextra += sprintf(pextra, "0x%03x\t", i); + for (j = 0; j < 8; j++) { + /* RTW_INFO("%02X ", data[i+j]); */ + pextra += sprintf(pextra, "%02X ", pEfuseHal->BTEfuseInitMap[i+j]); + } + /* RTW_INFO("\t"); */ + pextra += sprintf(pextra, "\t"); + for (; j < 16; j++) { + /* RTW_INFO("%02X ", data[i+j]); */ + pextra += sprintf(pextra, "%02X ", pEfuseHal->BTEfuseInitMap[i+j]); + } + /* RTW_INFO("\n"); */ + pextra += sprintf(pextra, "\n"); + } + /* RTW_INFO("\n"); */ + } else if (strcmp(tmp[0], "btrmap") == 0) { + u8 BTStatus; + + rtw_write8(padapter, 0xa3, 0x05); /* For 8723AB ,8821S ? */ + BTStatus = rtw_read8(padapter, 0xa0); + + RTW_INFO("%s: Check 0xa0 BT Status =0x%x\n", __FUNCTION__, BTStatus); + if (BTStatus != 0x04) { + sprintf(extra, "BT Status not Active ,can't to read BT eFuse\n"); + goto exit; + } + + if ((tmp[1] == NULL) || (tmp[2] == NULL)) { + err = -EINVAL; + goto exit; + } + + BTEfuse_PowerSwitch(padapter, 1, _TRUE); + + /* rmap addr cnts */ + addr = simple_strtoul(tmp[1], &ptmp, 16); + RTW_INFO("%s: addr=0x%X\n", __FUNCTION__, addr); + + cnts = simple_strtoul(tmp[2], &ptmp, 10); + if (cnts == 0) { + RTW_INFO("%s: btrmap Fail!! cnts error!\n", __FUNCTION__); + err = -EINVAL; + goto exit; + } + RTW_INFO("%s: cnts=%d\n", __FUNCTION__, cnts); +#ifndef RTW_HALMAC + EFUSE_GetEfuseDefinition(padapter, EFUSE_BT, TYPE_EFUSE_MAP_LEN, (void *)&max_available_len, _FALSE); + if ((addr + cnts) > max_available_len) { + RTW_INFO("%s: addr(0x%X)+cnts(%d) parameter error!\n", __FUNCTION__, addr, cnts); + err = -EFAULT; + goto exit; + } +#endif + if (rtw_BT_efuse_map_read(padapter, addr, cnts, data) == _FAIL) { + RTW_INFO("%s: rtw_BT_efuse_map_read error!!\n", __FUNCTION__); + err = -EFAULT; + goto exit; + } + + *extra = 0; + pextra = extra; + /* RTW_INFO("%s: bt efuse data={", __FUNCTION__); */ + for (i = 0; i < cnts; i++) { + /* RTW_INFO("0x%02x ", data[i]); */ + pextra += sprintf(pextra, " 0x%02X ", data[i]); + } + /* RTW_INFO("}\n"); */ + RTW_INFO(FUNC_ADPT_FMT ": BT MAC=[%s]\n", FUNC_ADPT_ARG(padapter), extra); + } else if (strcmp(tmp[0], "btffake") == 0) { + /* RTW_INFO("OFFSET\tVALUE(hex)\n"); */ + sprintf(extra, "\n"); + for (i = 0; i < 512; i += 16) { + /* RTW_INFO("0x%03x\t", i); */ + pextra = extra + strlen(extra); + pextra += sprintf(pextra, "0x%03x\t", i); + for (j = 0; j < 8; j++) { + /* RTW_INFO("%02X ", pEfuseHal->fakeBTEfuseModifiedMap[i+j]); */ + pextra += sprintf(pextra, "%02X ", pEfuseHal->fakeBTEfuseModifiedMap[i+j]); + } + /* RTW_INFO("\t"); */ + pextra += sprintf(pextra, "\t"); + for (; j < 16; j++) { + /* RTW_INFO("%02X ", pEfuseHal->fakeBTEfuseModifiedMap[i+j]); */ + pextra += sprintf(pextra, "%02X ", pEfuseHal->fakeBTEfuseModifiedMap[i+j]); + } + /* RTW_INFO("\n"); */ + pextra += sprintf(pextra, "\n"); + } + /* RTW_INFO("\n"); */ + } else if (strcmp(tmp[0], "btbfake") == 0) { + /* RTW_INFO("OFFSET\tVALUE(hex)\n"); */ + sprintf(extra, "\n"); + for (i = 512; i < 1024; i += 16) { + /* RTW_INFO("0x%03x\t", i); */ + pextra = extra + strlen(extra); + pextra += sprintf(pextra, "0x%03x\t", i); + for (j = 0; j < 8; j++) { + /* RTW_INFO("%02X ", pEfuseHal->fakeBTEfuseModifiedMap[i+j]); */ + pextra += sprintf(pextra, "%02X ", pEfuseHal->fakeBTEfuseModifiedMap[i+j]); + } + /* RTW_INFO("\t"); */ + pextra += sprintf(pextra, "\t"); + for (; j < 16; j++) { + /* RTW_INFO("%02X ", pEfuseHal->fakeBTEfuseModifiedMap[i+j]); */ + pextra += sprintf(pextra, "%02X ", pEfuseHal->fakeBTEfuseModifiedMap[i+j]); + } + /* RTW_INFO("\n"); */ + pextra += sprintf(pextra, "\n"); + } + /* RTW_INFO("\n"); */ + } else if (strcmp(tmp[0], "wlrfkmap") == 0) { + static u8 fk_order = 0; + u8 *efuse; + u32 shift, cnt; + u32 blksz = 0x200; /* The size of one time show, default 512 */ + + EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN , (void *)&mapLen, _FALSE); + efuse = pEfuseHal->fakeEfuseModifiedMap; + + shift = blksz * fk_order; + efuse += shift; + cnt = mapLen - shift; + if (cnt > blksz) { + cnt = blksz; + fk_order++; + } else + fk_order = 0; + + sprintf(extra, "\n"); + for (i = 0; i < cnt; i += 16) { + pextra = extra + strlen(extra); + pextra += sprintf(pextra, "0x%02x\t", shift + i); + for (j = 0; j < 8; j++) + pextra += sprintf(pextra, "%02X ", efuse[i + j]); + pextra += sprintf(pextra, "\t"); + for (; j < 16; j++) + pextra += sprintf(pextra, "%02X ", efuse[i + j]); + pextra += sprintf(pextra, "\n"); + } + if ((shift + cnt) < mapLen) + pextra += sprintf(pextra, "\t...more\n"); + + } else if (strcmp(tmp[0], "wlrfkrmap") == 0) { + if ((tmp[1] == NULL) || (tmp[2] == NULL)) { + RTW_INFO("%s: rmap Fail!! Parameters error!\n", __FUNCTION__); + err = -EINVAL; + goto exit; + } + /* rmap addr cnts */ + addr = simple_strtoul(tmp[1], &ptmp, 16); + RTW_INFO("%s: addr=%x\n", __FUNCTION__, addr); + + cnts = simple_strtoul(tmp[2], &ptmp, 10); + if (cnts == 0) { + RTW_INFO("%s: rmap Fail!! cnts error!\n", __FUNCTION__); + err = -EINVAL; + goto exit; + } + RTW_INFO("%s: cnts=%d\n", __FUNCTION__, cnts); + + /* RTW_INFO("%s: data={", __FUNCTION__); */ + *extra = 0; + pextra = extra; + for (i = 0; i < cnts; i++) { + RTW_INFO("wlrfkrmap = 0x%02x\n", pEfuseHal->fakeEfuseModifiedMap[addr + i]); + pextra += sprintf(pextra, "0x%02X ", pEfuseHal->fakeEfuseModifiedMap[addr+i]); + } + } else if (strcmp(tmp[0], "btrfkrmap") == 0) { + if ((tmp[1] == NULL) || (tmp[2] == NULL)) { + RTW_INFO("%s: rmap Fail!! Parameters error!\n", __FUNCTION__); + err = -EINVAL; + goto exit; + } + /* rmap addr cnts */ + addr = simple_strtoul(tmp[1], &ptmp, 16); + RTW_INFO("%s: addr=%x\n", __FUNCTION__, addr); + + cnts = simple_strtoul(tmp[2], &ptmp, 10); + if (cnts == 0) { + RTW_INFO("%s: rmap Fail!! cnts error!\n", __FUNCTION__); + err = -EINVAL; + goto exit; + } + RTW_INFO("%s: cnts=%d\n", __FUNCTION__, cnts); + + /* RTW_INFO("%s: data={", __FUNCTION__); */ + *extra = 0; + pextra = extra; + for (i = 0; i < cnts; i++) { + RTW_INFO("wlrfkrmap = 0x%02x\n", pEfuseHal->fakeBTEfuseModifiedMap[addr + i]); + pextra += sprintf(pextra, "0x%02X ", pEfuseHal->fakeBTEfuseModifiedMap[addr+i]); + } + } else if (strcmp(tmp[0], "mask") == 0) { + *extra = 0; + mask_len = sizeof(u8) * rtw_get_efuse_mask_arraylen(padapter); + rtw_efuse_mask_array(padapter, mask_buf); + + if (padapter->registrypriv.bFileMaskEfuse == _TRUE) + _rtw_memcpy(mask_buf, maskfileBuffer, mask_len); + + sprintf(extra, "\n"); + pextra = extra + strlen(extra); + for (i = 0; i < mask_len; i++) + pextra += sprintf(pextra, "0x%02X\n", mask_buf[i]); + + } else if (strcmp(tmp[0], "btmask") == 0) { + *extra = 0; + mask_len = sizeof(u8) * rtw_get_bt_efuse_mask_arraylen(padapter); + rtw_bt_efuse_mask_array(padapter, mask_buf); + + if (padapter->registrypriv.bBTFileMaskEfuse == _TRUE) + _rtw_memcpy(mask_buf, btmaskfileBuffer, mask_len); + + sprintf(extra, "\n"); + pextra = extra + strlen(extra); + for (i = 0; i < mask_len; i++) + pextra += sprintf(pextra, "0x%02X\n", mask_buf[i]); + + } else + sprintf(extra, "Command not found!"); + +exit: + if (data) + rtw_mfree(data, EFUSE_BT_MAX_MAP_LEN); + if (rawdata) + rtw_mfree(rawdata, EFUSE_BT_MAX_MAP_LEN); + if (!err) + wrqu->length = strlen(extra); + + if (padapter->registrypriv.mp_mode == 0) { +#ifdef CONFIG_IPS + rtw_pm_set_ips(padapter, ips_mode); +#endif /* CONFIG_IPS */ + +#ifdef CONFIG_LPS + rtw_pm_set_lps(padapter, lps_mode); +#endif /* CONFIG_LPS */ + } + +#ifdef CONFIG_IOL + padapter->registrypriv.fw_iol = org_fw_iol;/* 0:Disable, 1:enable, 2:by usb speed */ +#endif + return err; +} + + +#ifdef CONFIG_MP_INCLUDED +static int rtw_mp_efuse_set(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wdata, char *extra) +{ + struct iw_point *wrqu; + PADAPTER padapter; + struct pwrctrl_priv *pwrctrlpriv ; + PHAL_DATA_TYPE pHalData; + PEFUSE_HAL pEfuseHal; + struct hal_ops *pHalFunc; + struct mp_priv *pmp_priv; + + u8 ips_mode = IPS_NUM; /* init invalid value */ + u8 lps_mode = PS_MODE_NUM; /* init invalid value */ + u32 i = 0, j = 0, jj, kk; + u8 *setdata = NULL; + u8 *ShadowMapBT = NULL; + u8 *ShadowMapWiFi = NULL; + u8 *setrawdata = NULL; + char *pch, *ptmp, *token, *tmp[3] = {0x00, 0x00, 0x00}; + u16 addr = 0xFF, cnts = 0, BTStatus = 0 , max_available_len = 0; + u16 wifimaplen; + int err; + boolean bcmpchk = _TRUE; + + + wrqu = (struct iw_point *)wdata; + padapter = rtw_netdev_priv(dev); + pwrctrlpriv = adapter_to_pwrctl(padapter); + pHalData = GET_HAL_DATA(padapter); + pEfuseHal = &pHalData->EfuseHal; + pHalFunc = &padapter->hal_func; + pmp_priv = &padapter->mppriv; + + err = 0; + + if (copy_from_user(extra, wrqu->pointer, wrqu->length)) + return -EFAULT; +#ifdef CONFIG_RTL8822C + rtw_pre_bt_efuse(padapter); +#endif + *(extra + wrqu->length) = '\0'; + + EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN , (void *)&wifimaplen, _FALSE); + + setdata = rtw_zmalloc(1024); + if (setdata == NULL) { + err = -ENOMEM; + goto exit; + } + ShadowMapBT = rtw_malloc(EFUSE_BT_MAX_MAP_LEN); + if (ShadowMapBT == NULL) { + err = -ENOMEM; + goto exit; + } + ShadowMapWiFi = rtw_malloc(wifimaplen); + if (ShadowMapWiFi == NULL) { + err = -ENOMEM; + goto exit; + } + setrawdata = rtw_malloc(EFUSE_MAX_SIZE); + if (setrawdata == NULL) { + err = -ENOMEM; + goto exit; + } + +#ifdef CONFIG_LPS + lps_mode = pwrctrlpriv->power_mgnt;/* keep org value */ + rtw_pm_set_lps(padapter, PS_MODE_ACTIVE); +#endif + +#ifdef CONFIG_IPS + ips_mode = pwrctrlpriv->ips_mode;/* keep org value */ + rtw_pm_set_ips(padapter, IPS_NONE); +#endif + + pch = extra; + RTW_INFO("%s: in=%s\n", __FUNCTION__, extra); + + i = 0; + while ((token = strsep(&pch, ",")) != NULL) { + if (i > 2) + break; + tmp[i] = token; + i++; + } + + /* tmp[0],[1],[2] */ + /* wmap,addr,00e04c871200 */ + if (strcmp(tmp[0], "wmap") == 0) { + if ((tmp[1] == NULL) || (tmp[2] == NULL)) { + err = -EINVAL; + goto exit; + } + +#ifndef RTW_HALMAC + /* unknown bug workaround, need to fix later */ + addr = 0x1ff; + rtw_write8(padapter, EFUSE_CTRL + 1, (addr & 0xff)); + rtw_msleep_os(10); + rtw_write8(padapter, EFUSE_CTRL + 2, ((addr >> 8) & 0x03)); + rtw_msleep_os(10); + rtw_write8(padapter, EFUSE_CTRL + 3, 0x72); + rtw_msleep_os(10); + rtw_read8(padapter, EFUSE_CTRL); +#endif /* RTW_HALMAC */ + + addr = simple_strtoul(tmp[1], &ptmp, 16); + addr &= 0xFFF; + + cnts = strlen(tmp[2]); + if (cnts % 2) { + err = -EINVAL; + goto exit; + } + cnts /= 2; + if (cnts == 0) { + err = -EINVAL; + goto exit; + } + + RTW_INFO("%s: addr=0x%X\n", __FUNCTION__, addr); + RTW_INFO("%s: cnts=%d\n", __FUNCTION__, cnts); + RTW_INFO("%s: map data=%s\n", __FUNCTION__, tmp[2]); + + for (jj = 0, kk = 0; jj < cnts; jj++, kk += 2) + setdata[jj] = key_2char2num(tmp[2][kk], tmp[2][kk + 1]); + + EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (void *)&max_available_len, _FALSE); + + if ((addr + cnts) > max_available_len) { + RTW_INFO("%s: addr(0x%X)+cnts(%d) parameter error!\n", __FUNCTION__, addr, cnts); + err = -EFAULT; + goto exit; + } + + if (rtw_efuse_map_write(padapter, addr, cnts, setdata) == _FAIL) { + RTW_INFO("%s: rtw_efuse_map_write error!!\n", __FUNCTION__); + err = -EFAULT; + goto exit; + } + *extra = 0; + RTW_INFO("%s: after rtw_efuse_map_write to _rtw_memcmp\n", __func__); + if (rtw_efuse_mask_map_read(padapter, addr, cnts, ShadowMapWiFi) == _SUCCESS) { + if (_rtw_memcmp((void *)ShadowMapWiFi , (void *)setdata, cnts)) { + RTW_INFO("%s: WiFi write map afterf compare success\n", __FUNCTION__); + sprintf(extra, "WiFi write map compare OK\n"); + err = 0; + goto exit; + } else { + sprintf(extra, "WiFi write map compare FAIL\n"); + RTW_INFO("%s: WiFi write map compare Fail\n", __FUNCTION__); + err = 0; + goto exit; + } + } + } else if (strcmp(tmp[0], "wraw") == 0) { + if ((tmp[1] == NULL) || (tmp[2] == NULL)) { + err = -EINVAL; + goto exit; + } + + addr = simple_strtoul(tmp[1], &ptmp, 16); + addr &= 0xFFF; + + cnts = strlen(tmp[2]); + if (cnts % 2) { + err = -EINVAL; + goto exit; + } + cnts /= 2; + if (cnts == 0) { + err = -EINVAL; + goto exit; + } + + RTW_INFO("%s: addr=0x%X\n", __FUNCTION__, addr); + RTW_INFO("%s: cnts=%d\n", __FUNCTION__, cnts); + RTW_INFO("%s: raw data=%s\n", __FUNCTION__, tmp[2]); + + for (jj = 0, kk = 0; jj < cnts; jj++, kk += 2) + setrawdata[jj] = key_2char2num(tmp[2][kk], tmp[2][kk + 1]); + + if (rtw_efuse_access(padapter, _TRUE, addr, cnts, setrawdata) == _FAIL) { + RTW_INFO("%s: rtw_efuse_access error!!\n", __FUNCTION__); + err = -EFAULT; + goto exit; + } + } else if (strcmp(tmp[0], "btwraw") == 0) { + if ((tmp[1] == NULL) || (tmp[2] == NULL)) { + err = -EINVAL; + goto exit; + } + + addr = simple_strtoul(tmp[1], &ptmp, 16); + addr &= 0xFFF; + + cnts = strlen(tmp[2]); + if (cnts % 2) { + err = -EINVAL; + goto exit; + } + cnts /= 2; + if (cnts == 0) { + err = -EINVAL; + goto exit; + } + + RTW_INFO("%s: addr=0x%X\n", __FUNCTION__, addr); + RTW_INFO("%s: cnts=%d\n", __FUNCTION__, cnts); + RTW_INFO("%s: raw data=%s\n", __FUNCTION__, tmp[2]); + + for (jj = 0, kk = 0; jj < cnts; jj++, kk += 2) + setrawdata[jj] = key_2char2num(tmp[2][kk], tmp[2][kk + 1]); +#ifdef RTW_HALMAC + if (rtw_efuse_bt_access(padapter, _TRUE, addr, cnts, setrawdata) == _FAIL) { + RTW_INFO("%s: rtw_efuse_access error!!\n", __FUNCTION__); + err = -EFAULT; + goto exit; + } +#else + rtw_write8(padapter, 0x35, 1); /* switch bank 1 (BT)*/ + if (rtw_efuse_access(padapter, _TRUE, addr, cnts, setrawdata) == _FAIL) { + RTW_INFO("%s: rtw_efuse_access error!!\n", __FUNCTION__); + rtw_write8(padapter, 0x35, 0); /* switch bank 0 (WiFi)*/ + err = -EFAULT; + goto exit; + } + rtw_write8(padapter, 0x35, 0); /* switch bank 0 (WiFi)*/ +#endif + } else if (strcmp(tmp[0], "mac") == 0) { + if (tmp[1] == NULL) { + err = -EINVAL; + goto exit; + } + + /* mac,00e04c871200 */ + + if (hal_efuse_macaddr_offset(padapter) == -1) { + err = -EFAULT; + goto exit; + } + + addr = hal_efuse_macaddr_offset(padapter); + cnts = strlen(tmp[1]); + if (cnts % 2) { + err = -EINVAL; + goto exit; + } + cnts /= 2; + if (cnts == 0) { + err = -EINVAL; + goto exit; + } + if (cnts > 6) { + RTW_INFO("%s: error data for mac addr=\"%s\"\n", __FUNCTION__, tmp[1]); + err = -EFAULT; + goto exit; + } + + RTW_INFO("%s: addr=0x%X\n", __FUNCTION__, addr); + RTW_INFO("%s: cnts=%d\n", __FUNCTION__, cnts); + RTW_INFO("%s: MAC address=%s\n", __FUNCTION__, tmp[1]); + + for (jj = 0, kk = 0; jj < cnts; jj++, kk += 2) + setdata[jj] = key_2char2num(tmp[1][kk], tmp[1][kk + 1]); + + EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (void *)&max_available_len, _FALSE); + + if ((addr + cnts) > max_available_len) { + RTW_INFO("%s: addr(0x%X)+cnts(%d) parameter error!\n", __FUNCTION__, addr, cnts); + err = -EFAULT; + goto exit; + } + + if (rtw_efuse_map_write(padapter, addr, cnts, setdata) == _FAIL) { + RTW_INFO("%s: rtw_efuse_map_write error!!\n", __FUNCTION__); + err = -EFAULT; + goto exit; + } + } else if (strcmp(tmp[0], "vidpid") == 0) { + if (tmp[1] == NULL) { + err = -EINVAL; + goto exit; + } + + /* pidvid,da0b7881 */ +#ifdef CONFIG_RTL8188E +#ifdef CONFIG_USB_HCI + addr = EEPROM_VID_88EU; +#endif +#ifdef CONFIG_PCI_HCI + addr = EEPROM_VID_88EE; +#endif +#endif /* CONFIG_RTL8188E */ + +#ifdef CONFIG_RTL8192E +#ifdef CONFIG_USB_HCI + addr = EEPROM_VID_8192EU; +#endif +#ifdef CONFIG_PCI_HCI + addr = EEPROM_VID_8192EE; +#endif +#endif /* CONFIG_RTL8188E */ + +#ifdef CONFIG_RTL8723B + addr = EEPROM_VID_8723BU; +#endif + +#ifdef CONFIG_RTL8188F + addr = EEPROM_VID_8188FU; +#endif + +#ifdef CONFIG_RTL8188GTV + addr = EEPROM_VID_8188GTVU; +#endif + +#ifdef CONFIG_RTL8703B +#ifdef CONFIG_USB_HCI + addr = EEPROM_VID_8703BU; +#endif /* CONFIG_USB_HCI */ +#endif /* CONFIG_RTL8703B */ + +#ifdef CONFIG_RTL8723D +#ifdef CONFIG_USB_HCI + addr = EEPROM_VID_8723DU; +#endif /* CONFIG_USB_HCI */ +#endif /* CONFIG_RTL8723D */ + + cnts = strlen(tmp[1]); + if (cnts % 2) { + err = -EINVAL; + goto exit; + } + cnts /= 2; + if (cnts == 0) { + err = -EINVAL; + goto exit; + } + + RTW_INFO("%s: addr=0x%X\n", __FUNCTION__, addr); + RTW_INFO("%s: cnts=%d\n", __FUNCTION__, cnts); + RTW_INFO("%s: VID/PID=%s\n", __FUNCTION__, tmp[1]); + + for (jj = 0, kk = 0; jj < cnts; jj++, kk += 2) + setdata[jj] = key_2char2num(tmp[1][kk], tmp[1][kk + 1]); + + EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (void *)&max_available_len, _FALSE); + if ((addr + cnts) > max_available_len) { + RTW_INFO("%s: addr(0x%X)+cnts(%d) parameter error!\n", __FUNCTION__, addr, cnts); + err = -EFAULT; + goto exit; + } + + if (rtw_efuse_map_write(padapter, addr, cnts, setdata) == _FAIL) { + RTW_INFO("%s: rtw_efuse_map_write error!!\n", __FUNCTION__); + err = -EFAULT; + goto exit; + } + } else if (strcmp(tmp[0], "wldumpfake") == 0) { + if (wifimaplen > EFUSE_MAX_MAP_LEN) + cnts = EFUSE_MAX_MAP_LEN; + else + cnts = wifimaplen; + if (rtw_efuse_mask_map_read(padapter, 0, cnts, pEfuseHal->fakeEfuseModifiedMap) == _SUCCESS) + RTW_INFO("%s: WiFi hw efuse dump to Fake map success\n", __func__); + else { + RTW_INFO("%s: WiFi hw efuse dump to Fake map Fail\n", __func__); + err = -EFAULT; + } + } else if (strcmp(tmp[0], "btwmap") == 0) { + rtw_write8(padapter, 0xa3, 0x05); /* For 8723AB ,8821S ? */ + BTStatus = rtw_read8(padapter, 0xa0); + RTW_INFO("%s: btwmap before read 0xa0 BT Status =0x%x\n", __FUNCTION__, BTStatus); + if (BTStatus != 0x04) { + sprintf(extra, "BT Status not Active ,can't do Write\n"); + goto exit; + } + + if ((tmp[1] == NULL) || (tmp[2] == NULL)) { + err = -EINVAL; + goto exit; + } + +#ifndef RTW_HALMAC + BTEfuse_PowerSwitch(padapter, 1, _TRUE); + addr = 0x1ff; + rtw_write8(padapter, EFUSE_CTRL + 1, (addr & 0xff)); + rtw_msleep_os(10); + rtw_write8(padapter, EFUSE_CTRL + 2, ((addr >> 8) & 0x03)); + rtw_msleep_os(10); + rtw_write8(padapter, EFUSE_CTRL + 3, 0x72); + rtw_msleep_os(10); + rtw_read8(padapter, EFUSE_CTRL); + BTEfuse_PowerSwitch(padapter, 1, _FALSE); +#endif /* RTW_HALMAC */ + + addr = simple_strtoul(tmp[1], &ptmp, 16); + addr &= 0xFFF; + + cnts = strlen(tmp[2]); + if (cnts % 2) { + err = -EINVAL; + goto exit; + } + cnts /= 2; + if (cnts == 0) { + err = -EINVAL; + goto exit; + } + + RTW_INFO("%s: addr=0x%X\n", __FUNCTION__, addr); + RTW_INFO("%s: cnts=%d\n", __FUNCTION__, cnts); + RTW_INFO("%s: BT data=%s\n", __FUNCTION__, tmp[2]); + + for (jj = 0, kk = 0; jj < cnts; jj++, kk += 2) + setdata[jj] = key_2char2num(tmp[2][kk], tmp[2][kk + 1]); +#ifndef RTW_HALMAC + EFUSE_GetEfuseDefinition(padapter, EFUSE_BT, TYPE_EFUSE_MAP_LEN, (void *)&max_available_len, _FALSE); + if ((addr + cnts) > max_available_len) { + RTW_INFO("%s: addr(0x%X)+cnts(%d) parameter error!\n", __FUNCTION__, addr, cnts); + err = -EFAULT; + goto exit; + } +#endif + if (rtw_BT_efuse_map_write(padapter, addr, cnts, setdata) == _FAIL) { + RTW_INFO("%s: rtw_BT_efuse_map_write error!!\n", __FUNCTION__); + sprintf(extra, "BT write FAIL !!!\n"); + err = -EFAULT; + goto exit; + } + *extra = 0; + RTW_INFO("%s: after rtw_BT_efuse_map_write to _rtw_memcmp\n", __FUNCTION__); + if ((rtw_BT_efuse_map_read(padapter, addr, cnts, ShadowMapBT) == _SUCCESS)) { + if (_rtw_memcmp((void *)ShadowMapBT , (void *)setdata, cnts)) { + RTW_INFO("%s: BT write map compare OK BTStatus=0x%x\n", __FUNCTION__, BTStatus); + sprintf(extra, "BT write map compare OK"); + err = 0; + goto exit; + } else { + sprintf(extra, "BT write map compare FAIL"); + RTW_INFO("%s: BT write map compare FAIL BTStatus=0x%x\n", __FUNCTION__, BTStatus); + err = 0; + goto exit; + } + } + } else if (strcmp(tmp[0], "btwfake") == 0) { + if ((tmp[1] == NULL) || (tmp[2] == NULL)) { + err = -EINVAL; + goto exit; + } + if (pmp_priv->bprocess_mp_mode != _TRUE) { + RTW_INFO("%s: btwfake not to be exec, please first to mp_start\n", __FUNCTION__); + sprintf(extra, "Error, btwfake cant to be exec, please first to mp_start !!!!\n"); + err = 0; + goto exit; + } + addr = simple_strtoul(tmp[1], &ptmp, 16); + addr &= 0xFFF; + + cnts = strlen(tmp[2]); + if (cnts % 2) { + err = -EINVAL; + goto exit; + } + cnts /= 2; + if (cnts == 0) { + err = -EINVAL; + goto exit; + } + + RTW_INFO("%s: addr=0x%X\n", __FUNCTION__, addr); + RTW_INFO("%s: cnts=%d\n", __FUNCTION__, cnts); + RTW_INFO("%s: BT tmp data=%s\n", __FUNCTION__, tmp[2]); + + for (jj = 0, kk = 0; jj < cnts; jj++, kk += 2) + pEfuseHal->fakeBTEfuseModifiedMap[addr + jj] = key_2char2num(tmp[2][kk], tmp[2][kk + 1]); + } else if (strcmp(tmp[0], "btdumpfake") == 0) { + if (rtw_BT_efuse_map_read(padapter, 0, EFUSE_BT_MAX_MAP_LEN, pEfuseHal->fakeBTEfuseModifiedMap) == _SUCCESS) + RTW_INFO("%s: BT read all map success\n", __FUNCTION__); + else { + RTW_INFO("%s: BT read all map Fail!\n", __FUNCTION__); + err = -EFAULT; + } + } else if (strcmp(tmp[0], "btfk2map") == 0) { +#ifdef CONFIG_BT_EFUSE_MASK + if (padapter->registrypriv.bBTFileMaskEfuse != _TRUE && pmp_priv->bloadBTefusemap == _TRUE) { + RTW_INFO("%s: File BT eFuse mask file not to be loaded\n", __FUNCTION__); + sprintf(extra, "Not load BT eFuse mask file yet, Please advance to use [ efuse_bt_mask ], now remove the Adapter.!!!!\n"); + rtw_set_surprise_removed(padapter); + err = 0; + goto exit; + } +#endif + rtw_write8(padapter, 0xa3, 0x05); + BTStatus = rtw_read8(padapter, 0xa0); + RTW_INFO("%s: btwmap before read 0xa0 BT Status =0x%x\n", __FUNCTION__, BTStatus); + if (BTStatus != 0x04) { + sprintf(extra, "BT Status not Active Write FAIL\n"); + goto exit; + } + if (pmp_priv->bprocess_mp_mode != _TRUE) { + RTW_INFO("%s: btfk2map not to be exec, please first to mp_start\n", __FUNCTION__); + sprintf(extra, "Error, btfk2map cant to be exec, please first to mp_start !!!!\n"); + err = 0; + goto exit; + } +#ifndef RTW_HALMAC + BTEfuse_PowerSwitch(padapter, 1, _TRUE); + addr = 0x1ff; + rtw_write8(padapter, EFUSE_CTRL + 1, (addr & 0xff)); + rtw_msleep_os(10); + rtw_write8(padapter, EFUSE_CTRL + 2, ((addr >> 8) & 0x03)); + rtw_msleep_os(10); + rtw_write8(padapter, EFUSE_CTRL + 3, 0x72); + rtw_msleep_os(10); + rtw_read8(padapter, EFUSE_CTRL); + BTEfuse_PowerSwitch(padapter, 1, _FALSE); +#endif /* RTW_HALMAC */ + + if (rtw_BT_efuse_map_write(padapter, 0x00, EFUSE_BT_MAX_MAP_LEN, pEfuseHal->fakeBTEfuseModifiedMap) == _FAIL) { + RTW_INFO("%s: rtw_BT_efuse_map_write error!\n", __FUNCTION__); + sprintf(extra, "BT write FAIL !!!\n"); + err = -EFAULT; + goto exit; + } + + RTW_INFO("pEfuseHal->fakeBTEfuseModifiedMap OFFSET\tVALUE(hex)\n"); + for (i = 0; i < EFUSE_BT_MAX_MAP_LEN; i += 16) { + printk("0x%02x\t", i); + for (j = 0; j < 8; j++) + printk("%02X ", pEfuseHal->fakeBTEfuseModifiedMap[i + j]); + printk("\t"); + + for (; j < 16; j++) + printk("%02X ", pEfuseHal->fakeBTEfuseModifiedMap[i + j]); + printk("\n"); + } + printk("\n"); +#if 1 + err = -EFAULT; + RTW_INFO("%s: rtw_BT_efuse_map_read _rtw_memcmp\n", __FUNCTION__); + if ((rtw_BT_efuse_map_read(padapter, 0x00, EFUSE_BT_MAX_MAP_LEN, pEfuseHal->fakeBTEfuseInitMap) == _SUCCESS)) { + if (_rtw_memcmp((void *)pEfuseHal->fakeBTEfuseModifiedMap, (void *)pEfuseHal->fakeBTEfuseInitMap, EFUSE_BT_MAX_MAP_LEN)) { + sprintf(extra, "BT write map compare OK"); + RTW_INFO("%s: BT write map afterf compare success BTStatus=0x%x\n", __FUNCTION__, BTStatus); + err = 0; + goto exit; + } else { + sprintf(extra, "BT write map compare FAIL"); + if (rtw_BT_efuse_map_write(padapter, 0x00, EFUSE_BT_MAX_MAP_LEN, pEfuseHal->fakeBTEfuseModifiedMap) == _FAIL) + RTW_INFO("%s: rtw_BT_efuse_map_write compare error,retry = %d!\n", __FUNCTION__, i); + + if (rtw_BT_efuse_map_read(padapter, EFUSE_BT, EFUSE_BT_MAX_MAP_LEN, pEfuseHal->fakeBTEfuseInitMap) == _SUCCESS) { + RTW_INFO("pEfuseHal->fakeBTEfuseInitMap OFFSET\tVALUE(hex)\n"); + + for (i = 0; i < EFUSE_BT_MAX_MAP_LEN; i += 16) { + printk("0x%02x\t", i); + for (j = 0; j < 8; j++) + printk("%02X ", pEfuseHal->fakeBTEfuseInitMap[i + j]); + printk("\t"); + for (; j < 16; j++) + printk("%02X ", pEfuseHal->fakeBTEfuseInitMap[i + j]); + printk("\n"); + } + printk("\n"); + } + RTW_INFO("%s: BT write map afterf compare not match to write efuse try write Map again , BTStatus=0x%x\n", __FUNCTION__, BTStatus); + goto exit; + } + } +#endif + + } else if (strcmp(tmp[0], "wlfk2map") == 0) { + *extra = 0; + + if (padapter->registrypriv.bFileMaskEfuse != _TRUE && pmp_priv->bloadefusemap == _TRUE) { + RTW_INFO("%s: File eFuse mask file not to be loaded\n", __FUNCTION__); + sprintf(extra, "Not load eFuse mask file yet, Please use the efuse_mask CMD, now remove the interface !!!!\n"); + rtw_set_surprise_removed(padapter); + err = 0; + goto exit; + } + + if (pmp_priv->bprocess_mp_mode != _TRUE) { + RTW_INFO("%s: wlfk2map not to be exec, please first to mp_start\n", __FUNCTION__); + sprintf(extra, "Error, wlfk2map cant to be exec, please first to mp_start !!!!\n"); + err = 0; + goto exit; + } + if (wifimaplen > EFUSE_MAX_MAP_LEN) + cnts = EFUSE_MAX_MAP_LEN; + else + cnts = wifimaplen; + if (rtw_efuse_map_write(padapter, 0x00, cnts, pEfuseHal->fakeEfuseModifiedMap) == _FAIL) { + RTW_INFO("%s: rtw_efuse_map_write fakeEfuseModifiedMap error!\n", __FUNCTION__); + err = -EFAULT; + goto exit; + } + + if (rtw_efuse_mask_map_read(padapter, 0x00, wifimaplen, ShadowMapWiFi) == _SUCCESS) { + addr = 0x00; + err = _TRUE; + + for (i = 0; i < cnts; i++) { + if (padapter->registrypriv.boffefusemask == 0) { + if (padapter->registrypriv.bFileMaskEfuse == _TRUE) { + if (rtw_file_efuse_IsMasked(padapter, addr + i, maskfileBuffer) == _TRUE) /*use file efuse mask. */ + bcmpchk = _FALSE; + } else { + if (efuse_IsMasked(padapter, addr + i) == _TRUE) + bcmpchk = _FALSE; + } + } + + if (bcmpchk == _TRUE) { + RTW_INFO("compare readMapWiFi[0x%02x] = %x, ModifiedMap = %x\n", addr + i, ShadowMapWiFi[ addr + i], pEfuseHal->fakeEfuseModifiedMap[addr + i]); + if (_rtw_memcmp((void *) &ShadowMapWiFi[addr + i], (void *)&pEfuseHal->fakeEfuseModifiedMap[addr + i], 1) == _FALSE){ + err = _FALSE; + break; + } + } + bcmpchk = _TRUE; + } + } + + if (err) { + RTW_INFO("%s: WiFi write map afterf compare OK\n", __FUNCTION__); + sprintf(extra, "WiFi write map compare OK\n"); + err = 0; + goto exit; + } else { + sprintf(extra, "WiFi write map compare FAIL\n"); + RTW_INFO("%s: WiFi write map compare Fail\n", __FUNCTION__); + err = 0; + goto exit; + } + } else if (strcmp(tmp[0], "wlwfake") == 0) { + if ((tmp[1] == NULL) || (tmp[2] == NULL)) { + err = -EINVAL; + goto exit; + } + if (pmp_priv->bprocess_mp_mode != _TRUE) { + RTW_INFO("%s: wlwfake not to be exec, please first to mp_start\n", __FUNCTION__); + sprintf(extra, "Error, wlwfake cant to be exec, please first to mp_start !!!!\n"); + err = 0; + goto exit; + } + addr = simple_strtoul(tmp[1], &ptmp, 16); + addr &= 0xFFF; + + cnts = strlen(tmp[2]); + if (cnts % 2) { + err = -EINVAL; + goto exit; + } + cnts /= 2; + if (cnts == 0) { + err = -EINVAL; + goto exit; + } + + RTW_INFO("%s: addr=0x%X\n", __FUNCTION__, addr); + RTW_INFO("%s: cnts=%d\n", __FUNCTION__, cnts); + RTW_INFO("%s: map tmp data=%s\n", __FUNCTION__, tmp[2]); + + for (jj = 0, kk = 0; jj < cnts; jj++, kk += 2) + pEfuseHal->fakeEfuseModifiedMap[addr + jj] = key_2char2num(tmp[2][kk], tmp[2][kk + 1]); + _rtw_memset(extra, '\0', strlen(extra)); + sprintf(extra, "wlwfake OK\n"); + + } + else if (strcmp(tmp[0], "wfakemac") == 0) { + if (tmp[1] == NULL) { + err = -EINVAL; + goto exit; + } + if (pmp_priv->bprocess_mp_mode != _TRUE) { + RTW_INFO("%s: wfakemac not to be exec, please first to mp_start\n", __FUNCTION__); + sprintf(extra, "Error, wfakemac cant to be exec, please first to mp_start !!!!\n"); + err = 0; + goto exit; + } + /* wfakemac,00e04c871200 */ + if (hal_efuse_macaddr_offset(padapter) == -1) { + err = -EFAULT; + goto exit; + } + + addr = hal_efuse_macaddr_offset(padapter); + cnts = strlen(tmp[1]); + if (cnts % 2) { + err = -EINVAL; + goto exit; + } + cnts /= 2; + if (cnts == 0) { + err = -EINVAL; + goto exit; + } + if (cnts > 6) { + RTW_INFO("%s: error data for mac addr=\"%s\"\n", __FUNCTION__, tmp[1]); + err = -EFAULT; + goto exit; + } + + RTW_INFO("%s: addr=0x%X\n", __FUNCTION__, addr); + RTW_INFO("%s: cnts=%d\n", __FUNCTION__, cnts); + RTW_INFO("%s: MAC address=%s\n", __FUNCTION__, tmp[1]); + + for (jj = 0, kk = 0; jj < cnts; jj++, kk += 2) + if ((addr + jj) < EFUSE_MAX_MAP_LEN) + pEfuseHal->fakeEfuseModifiedMap[addr + jj] = key_2char2num(tmp[1][kk], tmp[1][kk + 1]); + + _rtw_memset(extra, '\0', strlen(extra)); + sprintf(extra, "write mac addr to fake map OK\n"); + } else if(strcmp(tmp[0], "update") == 0) { + RTW_INFO("To Use new eFuse map ver3\n"); + + if (tmp[1] != 0x00) { + if (strcmp(tmp[1], "fake") == 0) { + pmp_priv->efuse_update_on = _TRUE; + RTW_INFO("Set efuse update without file\n"); + } else if (strcmp(tmp[1], "phy") == 0) { + pmp_priv->efuse_update_file = _FALSE; + pmp_priv->efuse_update_on = _FALSE; + RTW_INFO("Set efuse update with phy\n"); + } else { + pmp_priv->efuse_update_file = _TRUE; + strcpy(pmp_priv->efuse_file_path , tmp[1]); + RTW_INFO("Got file path %s\n", pmp_priv->efuse_file_path); + } + } + /*step read efuse/eeprom data and get mac_addr*/ + if (padapter->hal_func.read_adapter_info(padapter)) { + _rtw_memset(extra, '\0', strlen(extra)); + sprintf(extra, "eFuse Update OK\n"); + RTW_INFO("eFuse Update OK\n"); + } else { + _rtw_memset(extra, '\0', strlen(extra)); + sprintf(extra, "eFuse Update FAIL\n"); + RTW_INFO("eFuse Update FAIL\n"); + } + + RTW_INFO("To Use new eFuse map ver3 done\n"); + } else if (strcmp(tmp[0], "analyze") == 0) { + + rtw_efuse_analyze(padapter, EFUSE_WIFI, 0); + _rtw_memset(extra, '\0', strlen(extra)); + sprintf(extra, "eFuse Analyze OK,please to check kernel log\n"); + } +exit: + if (setdata) + rtw_mfree(setdata, 1024); + if (ShadowMapBT) + rtw_mfree(ShadowMapBT, EFUSE_BT_MAX_MAP_LEN); + if (ShadowMapWiFi) + rtw_mfree(ShadowMapWiFi, wifimaplen); + if (setrawdata) + rtw_mfree(setrawdata, EFUSE_MAX_SIZE); + + wrqu->length = strlen(extra); + + if (padapter->registrypriv.mp_mode == 0) { +#ifdef CONFIG_IPS + rtw_pm_set_ips(padapter, ips_mode); +#endif /* CONFIG_IPS */ + +#ifdef CONFIG_LPS + rtw_pm_set_lps(padapter, lps_mode); +#endif /* CONFIG_LPS */ + } + + return err; +} + +#ifdef CONFIG_RTW_CUSTOMER_STR +static int rtw_mp_customer_str( + struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + _adapter *adapter = rtw_netdev_priv(dev); + u32 len; + u8 *pbuf = NULL, *pch; + char *ptmp; + u8 param[RTW_CUSTOMER_STR_LEN]; + u8 count = 0; + u8 tmp; + u8 i; + u32 pos; + u8 ret; + u8 read = 0; + + if (adapter->registrypriv.mp_mode != 1 + || !adapter->registrypriv.mp_customer_str) + return -EFAULT; + + len = wrqu->data.length + 1; + + pbuf = (u8 *)rtw_zmalloc(len); + if (pbuf == NULL) { + RTW_WARN("%s: no memory!\n", __func__); + return -ENOMEM; + } + + if (copy_from_user(pbuf, wrqu->data.pointer, wrqu->data.length)) { + rtw_mfree(pbuf, len); + RTW_WARN("%s: copy from user fail!\n", __func__); + return -EFAULT; + } + RTW_INFO("%s: string=\"%s\"\n", __func__, pbuf); + + ptmp = (char *)pbuf; + pch = strsep(&ptmp, ","); + if ((pch == NULL) || (strlen(pch) == 0)) { + rtw_mfree(pbuf, len); + RTW_INFO("%s: parameter error(no cmd)!\n", __func__); + return -EFAULT; + } + + _rtw_memset(param, 0xFF, RTW_CUSTOMER_STR_LEN); + + if (strcmp(pch, "read") == 0) { + read = 1; + ret = rtw_hal_customer_str_read(adapter, param); + + } else if (strcmp(pch, "write") == 0) { + do { + pch = strsep(&ptmp, ":"); + if ((pch == NULL) || (strlen(pch) == 0)) + break; + if (strlen(pch) != 2 + || IsHexDigit(*pch) == _FALSE + || IsHexDigit(*(pch + 1)) == _FALSE + || sscanf(pch, "%hhx", &tmp) != 1 + ) { + RTW_WARN("%s: invalid 8-bit hex!\n", __func__); + rtw_mfree(pbuf, len); + return -EFAULT; + } + + param[count++] = tmp; + + } while (count < RTW_CUSTOMER_STR_LEN); + + if (count == 0) { + rtw_mfree(pbuf, len); + RTW_WARN("%s: no input!\n", __func__); + return -EFAULT; + } + ret = rtw_hal_customer_str_write(adapter, param); + } else { + rtw_mfree(pbuf, len); + RTW_INFO("%s: parameter error(unknown cmd)!\n", __func__); + return -EFAULT; + } + + pos = sprintf(extra, "%s: ", read ? "read" : "write"); + if (read == 0 || ret == _SUCCESS) { + for (i = 0; i < RTW_CUSTOMER_STR_LEN; i++) + pos += sprintf(extra + pos, "%02x:", param[i]); + extra[pos] = 0; + pos--; + } + pos += sprintf(extra + pos, " %s", ret == _SUCCESS ? "OK" : "FAIL"); + + wrqu->data.length = strlen(extra) + 1; + + rtw_mfree(pbuf, len); + return 0; +} +#endif /* CONFIG_RTW_CUSTOMER_STR */ + +static int rtw_priv_mp_set(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wdata, char *extra) +{ + + struct iw_point *wrqu = (struct iw_point *)wdata; + u32 subcmd = wrqu->flags; + PADAPTER padapter = rtw_netdev_priv(dev); + int status = 0; + +#ifdef CONFIG_CONCURRENT_MODE + if (!is_primary_adapter(padapter)) { + RTW_INFO("MP mode only primary Adapter support\n"); + return -EIO; + } +#endif + + RTW_INFO("%s mutx in %d\n", __func__, subcmd); + _enter_critical_mutex(&(adapter_to_dvobj(padapter)->ioctrl_mutex), NULL); + switch (subcmd) { + case CTA_TEST: + RTW_INFO("set CTA_TEST\n"); + status = rtw_cta_test_start(dev, info, wdata, extra); + break; + case MP_DISABLE_BT_COEXIST: + RTW_INFO("set case MP_DISABLE_BT_COEXIST\n"); + status = rtw_mp_disable_bt_coexist(dev, info, wdata, extra); + break; + case MP_IQK: + RTW_INFO("set MP_IQK\n"); + status = rtw_mp_iqk(dev, info, wrqu, extra); + break; + case MP_LCK: + RTW_INFO("set MP_LCK\n"); + status = rtw_mp_lck(dev, info, wrqu, extra); + break; + + default: + status = -EIO; + } + _exit_critical_mutex(&(adapter_to_dvobj(padapter)->ioctrl_mutex), NULL); + RTW_INFO("%s mutx done %d\n", __func__, subcmd); + + return status; +} + +static int rtw_priv_mp_get(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wdata, char *extra) +{ + + struct iw_point *wrqu = (struct iw_point *)wdata; + u32 subcmd = wrqu->flags; + PADAPTER padapter = rtw_netdev_priv(dev); + int status = 0; + +#ifdef CONFIG_CONCURRENT_MODE + if (!is_primary_adapter(padapter)) { + RTW_INFO("MP mode only primary Adapter support\n"); + return -EIO; + } +#endif + + RTW_INFO("%s mutx in %d\n", __func__, subcmd); + _enter_critical_mutex(&(adapter_to_dvobj(padapter)->ioctrl_mutex), NULL); + + switch (subcmd) { + case MP_START: + RTW_INFO("set case mp_start\n"); + status = rtw_mp_start(dev, info, wrqu, extra); + break; + case MP_STOP: + RTW_INFO("set case mp_stop\n"); + status = rtw_mp_stop(dev, info, wrqu, extra); + break; + case MP_BANDWIDTH: + RTW_INFO("set case mp_bandwidth\n"); + status = rtw_mp_bandwidth(dev, info, wrqu, extra); + break; + case MP_RESET_STATS: + RTW_INFO("set case MP_RESET_STATS\n"); + status = rtw_mp_reset_stats(dev, info, wrqu, extra); + break; + case MP_SetRFPathSwh: + RTW_INFO("set MP_SetRFPathSwitch\n"); + status = rtw_mp_SetRFPath(dev, info, wrqu, extra); + break; + case WRITE_REG: + status = rtw_mp_write_reg(dev, info, wrqu, extra); + break; + case WRITE_RF: + status = rtw_mp_write_rf(dev, info, wrqu, extra); + break; + case MP_PHYPARA: + RTW_INFO("mp_get MP_PHYPARA\n"); + status = rtw_mp_phypara(dev, info, wrqu, extra); + break; + case MP_CHANNEL: + RTW_INFO("set case mp_channel\n"); + status = rtw_mp_channel(dev , info, wrqu, extra); + break; + case MP_CHL_OFFSET: + RTW_INFO("set case mp_ch_offset\n"); + status = rtw_mp_ch_offset(dev , info, wrqu, extra); + break; + case READ_REG: + RTW_INFO("mp_get READ_REG\n"); + status = rtw_mp_read_reg(dev, info, wrqu, extra); + break; + case READ_RF: + RTW_INFO("mp_get READ_RF\n"); + status = rtw_mp_read_rf(dev, info, wrqu, extra); + break; + case MP_RATE: + RTW_INFO("set case mp_rate\n"); + status = rtw_mp_rate(dev, info, wrqu, extra); + break; + case MP_TXPOWER: + RTW_INFO("set case MP_TXPOWER\n"); + status = rtw_mp_txpower(dev, info, wrqu, extra); + break; + case MP_ANT_TX: + RTW_INFO("set case MP_ANT_TX\n"); + status = rtw_mp_ant_tx(dev, info, wrqu, extra); + break; + case MP_ANT_RX: + RTW_INFO("set case MP_ANT_RX\n"); + status = rtw_mp_ant_rx(dev, info, wrqu, extra); + break; + case MP_QUERY: + status = rtw_mp_trx_query(dev, info, wrqu, extra); + break; + case MP_CTX: + RTW_INFO("set case MP_CTX\n"); + status = rtw_mp_ctx(dev, info, wrqu, extra); + break; + case MP_ARX: + RTW_INFO("set case MP_ARX\n"); + status = rtw_mp_arx(dev, info, wrqu, extra); + break; + case MP_DUMP: + RTW_INFO("set case MP_DUMP\n"); + status = rtw_mp_dump(dev, info, wrqu, extra); + break; + case MP_PSD: + RTW_INFO("set case MP_PSD\n"); + status = rtw_mp_psd(dev, info, wrqu, extra); + break; + case MP_THER: + RTW_INFO("set case MP_THER\n"); + status = rtw_mp_thermal(dev, info, wrqu, extra); + break; + case MP_PwrCtlDM: + RTW_INFO("set MP_PwrCtlDM\n"); + status = rtw_mp_PwrCtlDM(dev, info, wrqu, extra); + break; + case MP_QueryDrvStats: + RTW_INFO("mp_get MP_QueryDrvStats\n"); + status = rtw_mp_QueryDrv(dev, info, wdata, extra); + break; + case MP_PWRTRK: + RTW_INFO("set case MP_PWRTRK\n"); + status = rtw_mp_pwrtrk(dev, info, wrqu, extra); + break; + case MP_SET_TSSIDE: + RTW_INFO("set case MP_TSSI_DE\n"); + status = rtw_mp_set_tsside(dev, info, wrqu, extra); + break; +#ifdef CONFIG_MP_INCLUDED + case EFUSE_SET: + RTW_INFO("set case efuse set\n"); + status = rtw_mp_efuse_set(dev, info, wdata, extra); + break; +#endif + case EFUSE_GET: + RTW_INFO("efuse get EFUSE_GET\n"); + status = rtw_mp_efuse_get(dev, info, wdata, extra); + break; + case MP_GET_TXPOWER_INX: + RTW_INFO("mp_get MP_GET_TXPOWER_INX\n"); + status = rtw_mp_txpower_index(dev, info, wrqu, extra); + break; + case MP_GETVER: + RTW_INFO("mp_get MP_GETVER\n"); + status = rtw_mp_getver(dev, info, wdata, extra); + break; + case MP_MON: + RTW_INFO("mp_get MP_MON\n"); + status = rtw_mp_mon(dev, info, wdata, extra); + break; + case EFUSE_BT_MASK: + RTW_INFO("mp_get EFUSE_BT_MASK\n"); + status = rtw_bt_efuse_mask_file(dev, info, wdata, extra); + break; + case EFUSE_MASK: + RTW_INFO("mp_get EFUSE_MASK\n"); + status = rtw_efuse_mask_file(dev, info, wdata, extra); + break; + case EFUSE_FILE: + RTW_INFO("mp_get EFUSE_FILE\n"); + status = rtw_efuse_file_map(dev, info, wdata, extra); + break; + case EFUSE_FILE_STORE: + RTW_INFO("mp_get EFUSE_FILE_STORE\n"); + status = rtw_efuse_file_map_store(dev, info, wdata, extra); + break; + case MP_TX: + RTW_INFO("mp_get MP_TX\n"); + status = rtw_mp_tx(dev, info, wdata, extra); + break; + case MP_RX: + RTW_INFO("mp_get MP_RX\n"); + status = rtw_mp_rx(dev, info, wdata, extra); + break; + case MP_HW_TX_MODE: + RTW_INFO("mp_get MP_HW_TX_MODE\n"); + status = rtw_mp_hwtx(dev, info, wdata, extra); + break; + case MP_GET_TSSIDE: + RTW_INFO("mp_get TSSI_DE\n"); + status = rtw_mp_get_tsside(dev, info, wrqu, extra); + break; +#ifdef CONFIG_RTW_CUSTOMER_STR + case MP_CUSTOMER_STR: + RTW_INFO("customer str\n"); + status = rtw_mp_customer_str(dev, info, wdata, extra); + break; +#endif + case MP_PWRLMT: + RTW_INFO("mp_get MP_SETPWRLMT\n"); + status = rtw_mp_pwrlmt(dev, info, wdata, extra); + break; + case MP_PWRBYRATE: + RTW_INFO("mp_get MP_SETPWRBYRATE\n"); + status = rtw_mp_pwrbyrate(dev, info, wdata, extra); + break; + case BT_EFUSE_FILE: + RTW_INFO("mp_get BT EFUSE_FILE\n"); + status = rtw_bt_efuse_file_map(dev, info, wdata, extra); + break; + case MP_SWRFPath: + RTW_INFO("mp_get MP_SWRFPath\n"); + status = rtw_mp_switch_rf_path(dev, info, wrqu, extra); + break; + case MP_LINK: + RTW_INFO("mp_get MP_LINK\n"); + status = rtw_mp_link(dev, info, wrqu, extra); + break; + case MP_DPK_TRK: + RTW_INFO("mp_get MP_DPK_TRK\n"); + status = rtw_mp_dpk_track(dev, info, wdata, extra); + break; + case MP_DPK: + RTW_INFO("set MP_DPK\n"); + status = rtw_mp_dpk(dev, info, wdata, extra); + break; + default: + status = -EIO; + } + + _exit_critical_mutex(&(adapter_to_dvobj(padapter)->ioctrl_mutex), NULL); + RTW_INFO("%s mutx done_%d\n", __func__, subcmd); + + return status; +} +#endif /*#if defined(CONFIG_MP_INCLUDED)*/ + + +#ifdef CONFIG_SDIO_INDIRECT_ACCESS +#define DBG_MP_SDIO_INDIRECT_ACCESS 1 +static int rtw_mp_sd_iread(struct net_device *dev + , struct iw_request_info *info + , struct iw_point *wrqu + , char *extra) +{ + char input[16]; + u8 width; + unsigned long addr; + u32 ret = 0; + PADAPTER padapter = rtw_netdev_priv(dev); + + if (wrqu->length > 16) { + RTW_INFO(FUNC_ADPT_FMT" wrqu->length:%d\n", FUNC_ADPT_ARG(padapter), wrqu->length); + ret = -EINVAL; + goto exit; + } + + if (copy_from_user(input, wrqu->pointer, wrqu->length)) { + RTW_INFO(FUNC_ADPT_FMT" copy_from_user fail\n", FUNC_ADPT_ARG(padapter)); + ret = -EFAULT; + goto exit; + } + + _rtw_memset(extra, 0, wrqu->length); + + if (sscanf(input, "%hhu,%lx", &width, &addr) != 2) { + RTW_INFO(FUNC_ADPT_FMT" sscanf fail\n", FUNC_ADPT_ARG(padapter)); + ret = -EINVAL; + goto exit; + } + + if (addr > 0x3FFF) { + RTW_INFO(FUNC_ADPT_FMT" addr:0x%lx\n", FUNC_ADPT_ARG(padapter), addr); + ret = -EINVAL; + goto exit; + } + + if (DBG_MP_SDIO_INDIRECT_ACCESS) + RTW_INFO(FUNC_ADPT_FMT" width:%u, addr:0x%lx\n", FUNC_ADPT_ARG(padapter), width, addr); + + switch (width) { + case 1: + sprintf(extra, "0x%02x", rtw_sd_iread8(padapter, addr)); + wrqu->length = strlen(extra); + break; + case 2: + sprintf(extra, "0x%04x", rtw_sd_iread16(padapter, addr)); + wrqu->length = strlen(extra); + break; + case 4: + sprintf(extra, "0x%08x", rtw_sd_iread32(padapter, addr)); + wrqu->length = strlen(extra); + break; + default: + wrqu->length = 0; + ret = -EINVAL; + break; + } + +exit: + return ret; +} + +static int rtw_mp_sd_iwrite(struct net_device *dev + , struct iw_request_info *info + , struct iw_point *wrqu + , char *extra) +{ + char width; + unsigned long addr, data; + int ret = 0; + PADAPTER padapter = rtw_netdev_priv(dev); + char input[32]; + + if (wrqu->length > 32) { + RTW_INFO(FUNC_ADPT_FMT" wrqu->length:%d\n", FUNC_ADPT_ARG(padapter), wrqu->length); + ret = -EINVAL; + goto exit; + } + + if (copy_from_user(input, wrqu->pointer, wrqu->length)) { + RTW_INFO(FUNC_ADPT_FMT" copy_from_user fail\n", FUNC_ADPT_ARG(padapter)); + ret = -EFAULT; + goto exit; + } + + _rtw_memset(extra, 0, wrqu->length); + + if (sscanf(input, "%hhu,%lx,%lx", &width, &addr, &data) != 3) { + RTW_INFO(FUNC_ADPT_FMT" sscanf fail\n", FUNC_ADPT_ARG(padapter)); + ret = -EINVAL; + goto exit; + } + + if (addr > 0x3FFF) { + RTW_INFO(FUNC_ADPT_FMT" addr:0x%lx\n", FUNC_ADPT_ARG(padapter), addr); + ret = -EINVAL; + goto exit; + } + + if (DBG_MP_SDIO_INDIRECT_ACCESS) + RTW_INFO(FUNC_ADPT_FMT" width:%u, addr:0x%lx, data:0x%lx\n", FUNC_ADPT_ARG(padapter), width, addr, data); + + switch (width) { + case 1: + if (data > 0xFF) { + ret = -EINVAL; + break; + } + rtw_sd_iwrite8(padapter, addr, data); + break; + case 2: + if (data > 0xFFFF) { + ret = -EINVAL; + break; + } + rtw_sd_iwrite16(padapter, addr, data); + break; + case 4: + rtw_sd_iwrite32(padapter, addr, data); + break; + default: + wrqu->length = 0; + ret = -EINVAL; + break; + } + +exit: + return ret; +} +#endif /* CONFIG_SDIO_INDIRECT_ACCESS */ + +static int rtw_priv_set(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wdata, char *extra) +{ + struct iw_point *wrqu = (struct iw_point *)wdata; + u32 subcmd = wrqu->flags; + PADAPTER padapter = rtw_netdev_priv(dev); + + if (padapter == NULL) + return -ENETDOWN; + + if (padapter->bup == _FALSE) { + RTW_INFO(" %s fail =>(padapter->bup == _FALSE )\n", __FUNCTION__); + return -ENETDOWN; + } + + if (RTW_CANNOT_RUN(padapter)) { + RTW_INFO("%s fail =>(bSurpriseRemoved == _TRUE) || ( bDriverStopped == _TRUE)\n", __func__); + return -ENETDOWN; + } + + if (extra == NULL) { + wrqu->length = 0; + return -EIO; + } + + if (subcmd < MP_NULL) { +#ifdef CONFIG_MP_INCLUDED + rtw_priv_mp_set(dev, info, wdata, extra); +#endif + return 0; + } + + switch (subcmd) { +#ifdef CONFIG_WOWLAN + case MP_WOW_ENABLE: + RTW_INFO("set case MP_WOW_ENABLE: %s\n", extra); + + rtw_wowlan_ctrl(dev, info, wdata, extra); + break; + case MP_WOW_SET_PATTERN: + RTW_INFO("set case MP_WOW_SET_PATTERN: %s\n", extra); + rtw_wowlan_set_pattern(dev, info, wdata, extra); + break; + #ifdef CONFIG_WOW_KEEP_ALIVE_PATTERN + case MP_WOW_SET_KEEP_ALIVE_PATTERN: + RTW_INFO("set case MP_WOW_SET_KEEP_ALIVE_PATTERN: %s\n", extra); + rtw_wowlan_set_keep_alive_pattern(dev, info, wdata, extra); + break; + #endif /*CONFIG_WOW_KEEP_ALIVE_PATTERN*/ + +#endif +#ifdef CONFIG_AP_WOWLAN + case MP_AP_WOW_ENABLE: + RTW_INFO("set case MP_AP_WOW_ENABLE: %s\n", extra); + rtw_ap_wowlan_ctrl(dev, info, wdata, extra); + break; +#endif +#ifdef CONFIG_APPEND_VENDOR_IE_ENABLE + case VENDOR_IE_SET: + RTW_INFO("set case VENDOR_IE_SET\n"); + rtw_vendor_ie_set(dev , info , wdata , extra); + break; +#endif + default: + return -EIO; + } + + return 0; +} + + +static int rtw_priv_get(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wdata, char *extra) +{ + struct iw_point *wrqu = (struct iw_point *)wdata; + u32 subcmd = wrqu->flags; + PADAPTER padapter = rtw_netdev_priv(dev); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + struct dm_struct *p_dm = &pHalData->odmpriv; + struct dm_rf_calibration_struct *p_rf_calibrate_info = &(p_dm->rf_calibrate_info); + struct dm_iqk_info *p_iqk_info = &p_dm->IQK_info; + u32 i = 100; + + + if (padapter->bup == _FALSE) { + RTW_INFO(" %s fail =>(padapter->bup == _FALSE )\n", __FUNCTION__); + return -ENETDOWN; + } + + if (RTW_CANNOT_RUN(padapter)) { + RTW_INFO("%s fail =>(padapter->bSurpriseRemoved == _TRUE) || ( padapter->bDriverStopped == _TRUE)\n", __func__); + return -ENETDOWN; + } + + if (extra == NULL) { + wrqu->length = 0; + return -EIO; + } + + if (subcmd < MP_NULL) { +#ifdef CONFIG_MP_INCLUDED + while (i > 1) { + if (p_rf_calibrate_info->is_iqk_in_progress) { + rtw_msleep_os(10); + } else { + p_iqk_info->rfk_forbidden = _TRUE; + break; + } + i--; + } + if (subcmd == MP_CHANNEL || subcmd == MP_BANDWIDTH || subcmd == MP_START || subcmd == MP_DPK) + p_iqk_info->rfk_forbidden = _FALSE; + rtw_priv_mp_get(dev, info, wdata, extra); + rtw_msleep_os(10); /* delay 5ms for sending pkt before exit adb shell operation */ + p_iqk_info->rfk_forbidden = _FALSE; +#endif + } else { + switch (subcmd) { +#if defined(CONFIG_RTL8723B) + case MP_SetBT: + RTW_INFO("set MP_SetBT\n"); + rtw_mp_SetBT(dev, info, wdata, extra); + break; +#endif +#ifdef CONFIG_SDIO_INDIRECT_ACCESS + case MP_SD_IREAD: + rtw_mp_sd_iread(dev, info, wrqu, extra); + break; + case MP_SD_IWRITE: + rtw_mp_sd_iwrite(dev, info, wrqu, extra); + break; +#endif +#ifdef CONFIG_APPEND_VENDOR_IE_ENABLE + case VENDOR_IE_GET: + RTW_INFO("get case VENDOR_IE_GET\n"); + rtw_vendor_ie_get(dev , info , wdata , extra); + break; +#endif + default: + return -EIO; + } + } + + return 0; +} + + +#ifdef CONFIG_TDLS +static int rtw_wx_tdls_wfd_enable(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + +#ifdef CONFIG_WFD + + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + RTW_INFO("[%s] %s %d\n", __FUNCTION__, extra, wrqu->data.length - 1); + + if (extra[0] == '0') + rtw_tdls_wfd_enable(padapter, 0); + else + rtw_tdls_wfd_enable(padapter, 1); + +#endif /* CONFIG_WFD */ + + return ret; +} + +static int rtw_tdls_weaksec(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + +#ifdef CONFIG_TDLS + + u8 i, j; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + RTW_INFO("[%s] %s %d\n", __FUNCTION__, extra, wrqu->data.length - 1); + + if (extra[0] == '0') + padapter->wdinfo.wfd_tdls_weaksec = 0; + else + padapter->wdinfo.wfd_tdls_weaksec = 1; + +#endif /* CONFIG_TDLS */ + + return ret; +} + + +static int rtw_tdls_enable(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + +#ifdef CONFIG_TDLS + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + RTW_INFO("[%s] %s %d\n", __FUNCTION__, extra, wrqu->data.length - 1); + + if (extra[0] == '0') + rtw_disable_tdls_func(padapter, _TRUE); + else if (extra[0] == '1') + rtw_enable_tdls_func(padapter); +#endif /* CONFIG_TDLS */ + + return ret; +} + +static int rtw_tdls_setup(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; +#ifdef CONFIG_TDLS + u8 i, j; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct tdls_txmgmt txmgmt; +#ifdef CONFIG_WFD + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); +#endif /* CONFIG_WFD */ + + RTW_INFO("[%s] %s %d\n", __FUNCTION__, extra, wrqu->data.length - 1); + + if (wrqu->data.length - 1 != 17) { + RTW_INFO("[%s] length:%d != 17\n", __FUNCTION__, (wrqu->data.length - 1)); + return ret; + } + + _rtw_memset(&txmgmt, 0x00, sizeof(struct tdls_txmgmt)); + for (i = 0, j = 0 ; i < ETH_ALEN; i++, j += 3) + txmgmt.peer[i] = key_2char2num(*(extra + j), *(extra + j + 1)); + +#ifdef CONFIG_WFD + if (_AES_ != padapter->securitypriv.dot11PrivacyAlgrthm) { + /* Weak Security situation with AP. */ + if (0 == pwdinfo->wfd_tdls_weaksec) { + /* Can't send the tdls setup request out!! */ + RTW_INFO("[%s] Current link is not AES, " + "SKIP sending the tdls setup request!!\n", __FUNCTION__); + } else + issue_tdls_setup_req(padapter, &txmgmt, _TRUE); + } else +#endif /* CONFIG_WFD */ + { + issue_tdls_setup_req(padapter, &txmgmt, _TRUE); + } +#endif /* CONFIG_TDLS */ + + return ret; +} + +static int rtw_tdls_teardown(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + +#ifdef CONFIG_TDLS + + u8 i, j; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct sta_info *ptdls_sta = NULL; + struct tdls_txmgmt txmgmt; + + RTW_INFO("[%s] %s %d\n", __FUNCTION__, extra, wrqu->data.length - 1); + + if (wrqu->data.length - 1 != 17 && wrqu->data.length - 1 != 19) { + RTW_INFO("[%s] length:%d != 17 or 19\n", + __FUNCTION__, (wrqu->data.length - 1)); + return ret; + } + + _rtw_memset(&txmgmt, 0x00, sizeof(struct tdls_txmgmt)); + for (i = 0, j = 0; i < ETH_ALEN; i++, j += 3) + txmgmt.peer[i] = key_2char2num(*(extra + j), *(extra + j + 1)); + + ptdls_sta = rtw_get_stainfo(&(padapter->stapriv), txmgmt.peer); + + if (ptdls_sta != NULL) { + txmgmt.status_code = _RSON_TDLS_TEAR_UN_RSN_; + if (wrqu->data.length - 1 == 19) + issue_tdls_teardown(padapter, &txmgmt, _FALSE); + else + issue_tdls_teardown(padapter, &txmgmt, _TRUE); + } else + RTW_INFO("TDLS peer not found\n"); +#endif /* CONFIG_TDLS */ + + return ret; +} + +static int rtw_tdls_discovery(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + +#ifdef CONFIG_TDLS + + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct tdls_txmgmt txmgmt; + int i = 0, j = 0; + + RTW_INFO("[%s] %s %d\n", __FUNCTION__, extra, wrqu->data.length - 1); + + _rtw_memset(&txmgmt, 0x00, sizeof(struct tdls_txmgmt)); + for (i = 0, j = 0 ; i < ETH_ALEN; i++, j += 3) + txmgmt.peer[i] = key_2char2num(*(extra + j), *(extra + j + 1)); + + issue_tdls_dis_req(padapter, &txmgmt); + +#endif /* CONFIG_TDLS */ + + return ret; +} + +static int rtw_tdls_ch_switch(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + +#ifdef CONFIG_TDLS +#ifdef CONFIG_TDLS_CH_SW + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct tdls_ch_switch *pchsw_info = &padapter->tdlsinfo.chsw_info; + u8 i, j; + struct sta_info *ptdls_sta = NULL; + u8 take_care_iqk; + + RTW_INFO("[%s] %s %d\n", __FUNCTION__, extra, wrqu->data.length - 1); + + if (rtw_tdls_is_chsw_allowed(padapter) == _FALSE) { + RTW_INFO("TDLS channel switch is not allowed\n"); + return ret; + } + + for (i = 0, j = 0 ; i < ETH_ALEN; i++, j += 3) + pchsw_info->addr[i] = key_2char2num(*(extra + j), *(extra + j + 1)); + + ptdls_sta = rtw_get_stainfo(&padapter->stapriv, pchsw_info->addr); + if (ptdls_sta == NULL) + return ret; + + pchsw_info->ch_sw_state |= TDLS_CH_SW_INITIATOR_STATE; + + if (ptdls_sta != NULL) { + if (pchsw_info->off_ch_num == 0) + pchsw_info->off_ch_num = 11; + } else + RTW_INFO("TDLS peer not found\n"); + + rtw_pm_set_lps(padapter, PS_MODE_ACTIVE); + + rtw_hal_get_hwreg(padapter, HW_VAR_CH_SW_NEED_TO_TAKE_CARE_IQK_INFO, &take_care_iqk); + if (take_care_iqk == _TRUE) { +#ifdef CONFIG_TDLS_CH_SW_V2 + rtw_tdls_cmd(padapter, ptdls_sta->cmn.mac_addr, TDLS_CH_SW_PREPARE); +#else + u8 central_chnl; + u8 bw_mode; + + bw_mode = (pchsw_info->ch_offset) ? CHANNEL_WIDTH_40 : CHANNEL_WIDTH_20; + central_chnl = rtw_get_center_ch(pchsw_info->off_ch_num, bw_mode, pchsw_info->ch_offset); + if (rtw_hal_ch_sw_iqk_info_search(padapter, central_chnl, bw_mode) >= 0) + rtw_tdls_cmd(padapter, ptdls_sta->cmn.mac_addr, TDLS_CH_SW_START); + else + rtw_tdls_cmd(padapter, ptdls_sta->cmn.mac_addr, TDLS_CH_SW_PREPARE); +#endif + } else + rtw_tdls_cmd(padapter, ptdls_sta->cmn.mac_addr, TDLS_CH_SW_START); + + /* issue_tdls_ch_switch_req(padapter, ptdls_sta); */ + /* RTW_INFO("issue tdls ch switch req\n"); */ + +#endif /* CONFIG_TDLS_CH_SW */ +#endif /* CONFIG_TDLS */ + + return ret; +} + +static int rtw_tdls_ch_switch_off(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + +#ifdef CONFIG_TDLS +#ifdef CONFIG_TDLS_CH_SW + + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct tdls_ch_switch *pchsw_info = &padapter->tdlsinfo.chsw_info; + u8 i, j, mac_addr[ETH_ALEN]; + struct sta_info *ptdls_sta = NULL; + struct tdls_txmgmt txmgmt; + + _rtw_memset(&txmgmt, 0x00, sizeof(struct tdls_txmgmt)); + + RTW_INFO("[%s] %s %d\n", __FUNCTION__, extra, wrqu->data.length - 1); + + if (rtw_tdls_is_chsw_allowed(padapter) == _FALSE) { + RTW_INFO("TDLS channel switch is not allowed\n"); + return ret; + } + + if (wrqu->data.length >= 17) { + for (i = 0, j = 0 ; i < ETH_ALEN; i++, j += 3) + mac_addr[i] = key_2char2num(*(extra + j), *(extra + j + 1)); + ptdls_sta = rtw_get_stainfo(&padapter->stapriv, mac_addr); + } + + if (ptdls_sta == NULL) + return ret; + + rtw_tdls_cmd(padapter, ptdls_sta->cmn.mac_addr, TDLS_CH_SW_END_TO_BASE_CHNL); + + pchsw_info->ch_sw_state &= ~(TDLS_CH_SW_INITIATOR_STATE | + TDLS_CH_SWITCH_ON_STATE | + TDLS_PEER_AT_OFF_STATE); + _rtw_memset(pchsw_info->addr, 0x00, ETH_ALEN); + + ptdls_sta->ch_switch_time = 0; + ptdls_sta->ch_switch_timeout = 0; + _cancel_timer_ex(&ptdls_sta->ch_sw_timer); + _cancel_timer_ex(&ptdls_sta->delay_timer); + _cancel_timer_ex(&ptdls_sta->stay_on_base_chnl_timer); + _cancel_timer_ex(&ptdls_sta->ch_sw_monitor_timer); + + rtw_pm_set_lps(padapter, PS_MODE_MAX); +#endif /* CONFIG_TDLS_CH_SW */ +#endif /* CONFIG_TDLS */ + + return ret; +} + +static int rtw_tdls_dump_ch(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + +#ifdef CONFIG_TDLS +#ifdef CONFIG_TDLS_CH_SW + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct tdls_info *ptdlsinfo = &padapter->tdlsinfo; + + RTW_INFO("[%s] dump_stack:%s\n", __FUNCTION__, extra); + + extra[wrqu->data.length] = 0x00; + ptdlsinfo->chsw_info.dump_stack = rtw_atoi(extra); + + return ret; + +#endif +#endif /* CONFIG_TDLS */ + + return ret; +} + +static int rtw_tdls_off_ch_num(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + +#ifdef CONFIG_TDLS +#ifdef CONFIG_TDLS_CH_SW + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct tdls_info *ptdlsinfo = &padapter->tdlsinfo; + + RTW_INFO("[%s] off_ch_num:%s\n", __FUNCTION__, extra); + + extra[wrqu->data.length] = 0x00; + ptdlsinfo->chsw_info.off_ch_num = rtw_atoi(extra); + + return ret; + +#endif +#endif /* CONFIG_TDLS */ + + return ret; +} + +static int rtw_tdls_ch_offset(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + +#ifdef CONFIG_TDLS +#ifdef CONFIG_TDLS_CH_SW + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct tdls_info *ptdlsinfo = &padapter->tdlsinfo; + + RTW_INFO("[%s] ch_offset:%s\n", __FUNCTION__, extra); + + extra[wrqu->data.length] = 0x00; + switch (rtw_atoi(extra)) { + case SCA: + ptdlsinfo->chsw_info.ch_offset = HAL_PRIME_CHNL_OFFSET_LOWER; + break; + + case SCB: + ptdlsinfo->chsw_info.ch_offset = HAL_PRIME_CHNL_OFFSET_UPPER; + break; + + default: + ptdlsinfo->chsw_info.ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + break; + } + + return ret; + +#endif +#endif /* CONFIG_TDLS */ + + return ret; +} + +static int rtw_tdls_pson(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + +#ifdef CONFIG_TDLS + + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + u8 i, j, mac_addr[ETH_ALEN]; + struct sta_info *ptdls_sta = NULL; + + RTW_INFO("[%s] %s %d\n", __FUNCTION__, extra, wrqu->data.length - 1); + + for (i = 0, j = 0; i < ETH_ALEN; i++, j += 3) + mac_addr[i] = key_2char2num(*(extra + j), *(extra + j + 1)); + + ptdls_sta = rtw_get_stainfo(&padapter->stapriv, mac_addr); + + issue_nulldata_to_TDLS_peer_STA(padapter, ptdls_sta->cmn.mac_addr, 1, 3, 500); + +#endif /* CONFIG_TDLS */ + + return ret; +} + +static int rtw_tdls_psoff(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + +#ifdef CONFIG_TDLS + + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + u8 i, j, mac_addr[ETH_ALEN]; + struct sta_info *ptdls_sta = NULL; + + RTW_INFO("[%s] %s %d\n", __FUNCTION__, extra, wrqu->data.length - 1); + + for (i = 0, j = 0; i < ETH_ALEN; i++, j += 3) + mac_addr[i] = key_2char2num(*(extra + j), *(extra + j + 1)); + + ptdls_sta = rtw_get_stainfo(&padapter->stapriv, mac_addr); + + if (ptdls_sta) + issue_nulldata_to_TDLS_peer_STA(padapter, ptdls_sta->cmn.mac_addr, 0, 3, 500); + +#endif /* CONFIG_TDLS */ + + return ret; +} + +static int rtw_tdls_setip(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + +#ifdef CONFIG_TDLS +#ifdef CONFIG_WFD + + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct tdls_info *ptdlsinfo = &padapter->tdlsinfo; + struct wifi_display_info *pwfd_info = ptdlsinfo->wfd_info; + u8 i = 0, j = 0, k = 0, tag = 0; + + RTW_INFO("[%s] %s %d\n", __FUNCTION__, extra, wrqu->data.length - 1); + + while (i < 4) { + for (j = 0; j < 4; j++) { + if (*(extra + j + tag) == '.' || *(extra + j + tag) == '\0') { + if (j == 1) + pwfd_info->ip_address[i] = convert_ip_addr('0', '0', *(extra + (j - 1) + tag)); + if (j == 2) + pwfd_info->ip_address[i] = convert_ip_addr('0', *(extra + (j - 2) + tag), *(extra + (j - 1) + tag)); + if (j == 3) + pwfd_info->ip_address[i] = convert_ip_addr(*(extra + (j - 3) + tag), *(extra + (j - 2) + tag), *(extra + (j - 1) + tag)); + + tag += j + 1; + break; + } + } + i++; + } + + RTW_INFO("[%s] Set IP = %u.%u.%u.%u\n", __FUNCTION__, + ptdlsinfo->wfd_info->ip_address[0], + ptdlsinfo->wfd_info->ip_address[1], + ptdlsinfo->wfd_info->ip_address[2], + ptdlsinfo->wfd_info->ip_address[3]); + +#endif /* CONFIG_WFD */ +#endif /* CONFIG_TDLS */ + + return ret; +} + +static int rtw_tdls_getip(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + +#ifdef CONFIG_TDLS +#ifdef CONFIG_WFD + + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct tdls_info *ptdlsinfo = &padapter->tdlsinfo; + struct wifi_display_info *pwfd_info = ptdlsinfo->wfd_info; + + RTW_INFO("[%s]\n", __FUNCTION__); + + sprintf(extra, "\n\n%u.%u.%u.%u\n", + pwfd_info->peer_ip_address[0], pwfd_info->peer_ip_address[1], + pwfd_info->peer_ip_address[2], pwfd_info->peer_ip_address[3]); + + RTW_INFO("[%s] IP=%u.%u.%u.%u\n", __FUNCTION__, + pwfd_info->peer_ip_address[0], pwfd_info->peer_ip_address[1], + pwfd_info->peer_ip_address[2], pwfd_info->peer_ip_address[3]); + + wrqu->data.length = strlen(extra); + +#endif /* CONFIG_WFD */ +#endif /* CONFIG_TDLS */ + + return ret; +} + +static int rtw_tdls_getport(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + +#ifdef CONFIG_TDLS +#ifdef CONFIG_WFD + + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct tdls_info *ptdlsinfo = &padapter->tdlsinfo; + struct wifi_display_info *pwfd_info = ptdlsinfo->wfd_info; + + RTW_INFO("[%s]\n", __FUNCTION__); + + sprintf(extra, "\n\n%d\n", pwfd_info->peer_rtsp_ctrlport); + RTW_INFO("[%s] remote port = %d\n", + __FUNCTION__, pwfd_info->peer_rtsp_ctrlport); + + wrqu->data.length = strlen(extra); + +#endif /* CONFIG_WFD */ +#endif /* CONFIG_TDLS */ + + return ret; + +} + +/* WFDTDLS, for sigma test */ +static int rtw_tdls_dis_result(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + +#ifdef CONFIG_TDLS +#ifdef CONFIG_WFD + + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct tdls_info *ptdlsinfo = &padapter->tdlsinfo; + + RTW_INFO("[%s]\n", __FUNCTION__); + + if (ptdlsinfo->dev_discovered == _TRUE) { + sprintf(extra, "\n\nDis=1\n"); + ptdlsinfo->dev_discovered = _FALSE; + } + + wrqu->data.length = strlen(extra); + +#endif /* CONFIG_WFD */ +#endif /* CONFIG_TDLS */ + + return ret; + +} + +/* WFDTDLS, for sigma test */ +static int rtw_wfd_tdls_status(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; + +#ifdef CONFIG_TDLS + + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct tdls_info *ptdlsinfo = &padapter->tdlsinfo; + + RTW_INFO("[%s]\n", __FUNCTION__); + + sprintf(extra, "\nlink_established:%d\n" + "sta_cnt:%d\n" + "sta_maximum:%d\n" + "cur_channel:%d\n" + "tdls_enable:%d" +#ifdef CONFIG_TDLS_CH_SW + "ch_sw_state:%08x\n" + "chsw_on:%d\n" + "off_ch_num:%d\n" + "cur_time:%d\n" + "ch_offset:%d\n" + "delay_swtich_back:%d" +#endif + , + ptdlsinfo->link_established, ptdlsinfo->sta_cnt, + ptdlsinfo->sta_maximum, ptdlsinfo->cur_channel, + rtw_is_tdls_enabled(padapter) +#ifdef CONFIG_TDLS_CH_SW + , + ptdlsinfo->chsw_info.ch_sw_state, + ATOMIC_READ(&padapter->tdlsinfo.chsw_info.chsw_on), + ptdlsinfo->chsw_info.off_ch_num, + ptdlsinfo->chsw_info.cur_time, + ptdlsinfo->chsw_info.ch_offset, + ptdlsinfo->chsw_info.delay_switch_back +#endif + ); + + wrqu->data.length = strlen(extra); + +#endif /* CONFIG_TDLS */ + + return ret; + +} + +static int rtw_tdls_getsta(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + + int ret = 0; +#ifdef CONFIG_TDLS + u8 i, j; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + u8 addr[ETH_ALEN] = {0}; + char charmac[17]; + struct sta_info *ptdls_sta = NULL; + + RTW_INFO("[%s] %s %d\n", __FUNCTION__, + (char *)wrqu->data.pointer, wrqu->data.length - 1); + + if (copy_from_user(charmac, wrqu->data.pointer + 9, 17)) { + ret = -EFAULT; + goto exit; + } + + RTW_INFO("[%s] %d, charmac:%s\n", __FUNCTION__, __LINE__, charmac); + for (i = 0, j = 0 ; i < ETH_ALEN; i++, j += 3) + addr[i] = key_2char2num(*(charmac + j), *(charmac + j + 1)); + + RTW_INFO("[%s] %d, charmac:%s, addr:"MAC_FMT"\n", + __FUNCTION__, __LINE__, charmac, MAC_ARG(addr)); + ptdls_sta = rtw_get_stainfo(&padapter->stapriv, addr); + if (ptdls_sta) { + sprintf(extra, "\n\ntdls_sta_state=0x%08x\n", ptdls_sta->tdls_sta_state); + RTW_INFO("\n\ntdls_sta_state=%d\n", ptdls_sta->tdls_sta_state); + } else { + sprintf(extra, "\n\nNot found this sta\n"); + RTW_INFO("\n\nNot found this sta\n"); + } + wrqu->data.length = strlen(extra); + +exit: +#endif /* CONFIG_TDLS */ + return ret; + +} + +static int rtw_tdls_get_best_ch(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ +#ifdef CONFIG_FIND_BEST_CHANNEL + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(padapter); + u32 i, best_channel_24G = 1, best_channel_5G = 36, index_24G = 0, index_5G = 0; + + for (i = 0; i < rfctl->max_chan_nums && rfctl->channel_set[i].ChannelNum != 0; i++) { + if (rfctl->channel_set[i].ChannelNum == 1) + index_24G = i; + if (rfctl->channel_set[i].ChannelNum == 36) + index_5G = i; + } + + for (i = 0; i < rfctl->max_chan_nums && rfctl->channel_set[i].ChannelNum != 0; i++) { + /* 2.4G */ + if (rfctl->channel_set[i].ChannelNum == 6 || rfctl->channel_set[i].ChannelNum == 11) { + if (rfctl->channel_set[i].rx_count < rfctl->channel_set[index_24G].rx_count) { + index_24G = i; + best_channel_24G = rfctl->channel_set[i].ChannelNum; + } + } + + /* 5G */ + if (rfctl->channel_set[i].ChannelNum >= 36 + && rfctl->channel_set[i].ChannelNum < 140) { + /* Find primary channel */ + if (((rfctl->channel_set[i].ChannelNum - 36) % 8 == 0) + && (rfctl->channel_set[i].rx_count < rfctl->channel_set[index_5G].rx_count)) { + index_5G = i; + best_channel_5G = rfctl->channel_set[i].ChannelNum; + } + } + + if (rfctl->channel_set[i].ChannelNum >= 149 + && rfctl->channel_set[i].ChannelNum < 165) { + /* Find primary channel */ + if (((rfctl->channel_set[i].ChannelNum - 149) % 8 == 0) + && (rfctl->channel_set[i].rx_count < rfctl->channel_set[index_5G].rx_count)) { + index_5G = i; + best_channel_5G = rfctl->channel_set[i].ChannelNum; + } + } +#if 1 /* debug */ + RTW_INFO("The rx cnt of channel %3d = %d\n", + rfctl->channel_set[i].ChannelNum, + rfctl->channel_set[i].rx_count); +#endif + } + + sprintf(extra, "\nbest_channel_24G = %d\n", best_channel_24G); + RTW_INFO("best_channel_24G = %d\n", best_channel_24G); + + if (index_5G != 0) { + sprintf(extra, "best_channel_5G = %d\n", best_channel_5G); + RTW_INFO("best_channel_5G = %d\n", best_channel_5G); + } + + wrqu->data.length = strlen(extra); + +#endif + + return 0; + +} +#endif /*#ifdef CONFIG_TDLS*/ +static int rtw_tdls(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + +#ifdef CONFIG_TDLS + + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + RTW_INFO("[%s] extra = %s\n", __FUNCTION__, extra); + + if (hal_chk_wl_func(padapter, WL_FUNC_TDLS) == _FALSE) { + RTW_INFO("Discard tdls oper since hal doesn't support tdls\n"); + return 0; + } + + if (rtw_is_tdls_enabled(padapter) == _FALSE) { + RTW_INFO("TDLS is not enabled\n"); + return 0; + } + + /* WFD Sigma will use the tdls enable command to let the driver know we want to test the tdls now! */ + + if (hal_chk_wl_func(padapter, WL_FUNC_MIRACAST)) { + if (_rtw_memcmp(extra, "wfdenable=", 10)) { + wrqu->data.length -= 10; + rtw_wx_tdls_wfd_enable(dev, info, wrqu, &extra[10]); + return ret; + } + } + + if (_rtw_memcmp(extra, "weaksec=", 8)) { + wrqu->data.length -= 8; + rtw_tdls_weaksec(dev, info, wrqu, &extra[8]); + return ret; + } else if (_rtw_memcmp(extra, "tdlsenable=", 11)) { + wrqu->data.length -= 11; + rtw_tdls_enable(dev, info, wrqu, &extra[11]); + return ret; + } + + if (_rtw_memcmp(extra, "setup=", 6)) { + wrqu->data.length -= 6; + rtw_tdls_setup(dev, info, wrqu, &extra[6]); + } else if (_rtw_memcmp(extra, "tear=", 5)) { + wrqu->data.length -= 5; + rtw_tdls_teardown(dev, info, wrqu, &extra[5]); + } else if (_rtw_memcmp(extra, "dis=", 4)) { + wrqu->data.length -= 4; + rtw_tdls_discovery(dev, info, wrqu, &extra[4]); + } else if (_rtw_memcmp(extra, "swoff=", 6)) { + wrqu->data.length -= 6; + rtw_tdls_ch_switch_off(dev, info, wrqu, &extra[6]); + } else if (_rtw_memcmp(extra, "sw=", 3)) { + wrqu->data.length -= 3; + rtw_tdls_ch_switch(dev, info, wrqu, &extra[3]); + } else if (_rtw_memcmp(extra, "dumpstack=", 10)) { + wrqu->data.length -= 10; + rtw_tdls_dump_ch(dev, info, wrqu, &extra[10]); + } else if (_rtw_memcmp(extra, "offchnum=", 9)) { + wrqu->data.length -= 9; + rtw_tdls_off_ch_num(dev, info, wrqu, &extra[9]); + } else if (_rtw_memcmp(extra, "choffset=", 9)) { + wrqu->data.length -= 9; + rtw_tdls_ch_offset(dev, info, wrqu, &extra[9]); + } else if (_rtw_memcmp(extra, "pson=", 5)) { + wrqu->data.length -= 5; + rtw_tdls_pson(dev, info, wrqu, &extra[5]); + } else if (_rtw_memcmp(extra, "psoff=", 6)) { + wrqu->data.length -= 6; + rtw_tdls_psoff(dev, info, wrqu, &extra[6]); + } + +#ifdef CONFIG_WFD + if (hal_chk_wl_func(padapter, WL_FUNC_MIRACAST)) { + if (_rtw_memcmp(extra, "setip=", 6)) { + wrqu->data.length -= 6; + rtw_tdls_setip(dev, info, wrqu, &extra[6]); + } else if (_rtw_memcmp(extra, "tprobe=", 6)) + issue_tunneled_probe_req((_adapter *)rtw_netdev_priv(dev)); + } +#endif /* CONFIG_WFD */ + +#endif /* CONFIG_TDLS */ + + return ret; +} + + +static int rtw_tdls_get(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + int ret = 0; + +#ifdef CONFIG_TDLS + + RTW_INFO("[%s] extra = %s\n", __FUNCTION__, (char *) wrqu->data.pointer); + + if (_rtw_memcmp(wrqu->data.pointer, "ip", 2)) + rtw_tdls_getip(dev, info, wrqu, extra); + else if (_rtw_memcmp(wrqu->data.pointer, "port", 4)) + rtw_tdls_getport(dev, info, wrqu, extra); + /* WFDTDLS, for sigma test */ + else if (_rtw_memcmp(wrqu->data.pointer, "dis", 3)) + rtw_tdls_dis_result(dev, info, wrqu, extra); + else if (_rtw_memcmp(wrqu->data.pointer, "status", 6)) + rtw_wfd_tdls_status(dev, info, wrqu, extra); + else if (_rtw_memcmp(wrqu->data.pointer, "tdls_sta=", 9)) + rtw_tdls_getsta(dev, info, wrqu, extra); + else if (_rtw_memcmp(wrqu->data.pointer, "best_ch", 7)) + rtw_tdls_get_best_ch(dev, info, wrqu, extra); +#endif /* CONFIG_TDLS */ + + return ret; +} + +#ifdef CONFIG_MAC_LOOPBACK_DRIVER + +#if defined(CONFIG_RTL8188E) +#include +extern void rtl8188e_cal_txdesc_chksum(struct tx_desc *ptxdesc); +#define cal_txdesc_chksum(padapter, desc) rtl8188e_cal_txdesc_chksum(desc) +#ifdef CONFIG_SDIO_HCI || defined(CONFIG_GSPI_HCI) +extern void rtl8188es_fill_default_txdesc(struct xmit_frame *pxmitframe, u8 *pbuf); +#define fill_default_txdesc rtl8188es_fill_default_txdesc +#endif /* CONFIG_SDIO_HCI */ +#endif /* CONFIG_RTL8188E */ +#if defined(CONFIG_RTL8723B) +extern void rtl8723b_cal_txdesc_chksum(struct tx_desc *ptxdesc); +#define cal_txdesc_chksum(padapter, desc) rtl8723b_cal_txdesc_chksum(desc) +extern void rtl8723b_fill_default_txdesc(struct xmit_frame *pxmitframe, u8 *pbuf); +#define fill_default_txdesc rtl8723b_fill_default_txdesc +#endif /* CONFIG_RTL8723B */ + +#if defined(CONFIG_RTL8703B) +/* extern void rtl8703b_cal_txdesc_chksum(struct tx_desc *ptxdesc); */ +#define cal_txdesc_chksum(padapter, desc) rtl8703b_cal_txdesc_chksum(desc) +/* extern void rtl8703b_fill_default_txdesc(struct xmit_frame *pxmitframe, u8 *pbuf); */ +#define fill_default_txdesc rtl8703b_fill_default_txdesc +#endif /* CONFIG_RTL8703B */ + +#if defined(CONFIG_RTL8723D) +/* extern void rtl8723d_cal_txdesc_chksum(struct tx_desc *ptxdesc); */ +#define cal_txdesc_chksum(padapter, desc) rtl8723d_cal_txdesc_chksum(desc) +/* extern void rtl8723d_fill_default_txdesc(struct xmit_frame *pxmitframe, u8 *pbuf); */ +#define fill_default_txdesc rtl8723d_fill_default_txdesc +#endif /* CONFIG_RTL8723D */ + +#if defined(CONFIG_RTL8710B) +#define cal_txdesc_chksum(padapter, desc) rtl8710b_cal_txdesc_chksum(desc) +#define fill_default_txdesc rtl8710b_fill_default_txdesc +#endif /* CONFIG_RTL8710B */ + +#if defined(CONFIG_RTL8192E) +extern void rtl8192e_cal_txdesc_chksum(struct tx_desc *ptxdesc); +#define cal_txdesc_chksum(padapter, desc) rtl8192e_cal_txdesc_chksum(desc) +#ifdef CONFIG_SDIO_HCI || defined(CONFIG_GSPI_HCI) +extern void rtl8192es_fill_default_txdesc(struct xmit_frame *pxmitframe, u8 *pbuf); +#define fill_default_txdesc rtl8192es_fill_default_txdesc +#endif /* CONFIG_SDIO_HCI */ +#endif /* CONFIG_RTL8192E */ + +#if defined(CONFIG_RTL8192F) +/* extern void rtl8192f_cal_txdesc_chksum(struct tx_desc *ptxdesc); */ +#define cal_txdesc_chksum(padapter, desc) rtl8192f_cal_txdesc_chksum(desc) +/* extern void rtl8192f_fill_default_txdesc(struct xmit_frame *pxmitframe, u8 *pbuf); */ +#define fill_default_txdesc rtl8192f_fill_default_txdesc +#endif /* CONFIG_RTL8192F */ + +#ifdef CONFIG_RTL8723F +#include <../../hal/rtl8723f/rtl8723f.h> + +#define REG_LOOPBACK_ENABLE 0x0103 +#define LOOKBACK_ENABLE_VALUE 0x0b +#define cal_txdesc_chksum(padapter, desc) rtl8723f_cal_txdesc_chksum(padapter, desc) +#define dump_txdesc_data(padapter, desc) rtl8723f_dbg_dump_tx_desc(padapter, DATA_FRAMETAG, desc); +#define get_rx_desc(rx_desc, rxbuf) rtl8723f_rxdesc2attribute(rx_desc, rxbuf) +#define hal_init rtl8723f_hal_init +#endif /* CONFIG_RTL8723F */ + +void dbg_dump_pkt(char *s, u8 *buf, u8 len) +{ + u8 i, j = 1; + + RTW_INFO("%s size = %u\n", s, len); + + for (i = 0; (i + 4) < len; i += 4) { + if (j % 4 == 1) + RTW_PRINT("idx:%u:", i); + _RTW_PRINT(" 0x%02x 0x%02x 0x%02x 0x%02x", buf[i], buf[i+1], buf[i+2], buf[i+3]); + if ((j++) % 4 == 0) + _RTW_PRINT("\n"); + } + + for (; i < len ; i++) { + _RTW_PRINT(" 0x%02x", buf[i]); + } + _RTW_PRINT("\n ================================\n"); +} + +static s32 initLoopback(PADAPTER padapter) +{ + PLOOPBACKDATA ploopback; + + if (padapter->ploopback == NULL) { + ploopback = (PLOOPBACKDATA)rtw_zmalloc(sizeof(LOOPBACKDATA)); + if (ploopback == NULL) + return -ENOMEM; + + _rtw_init_sema(&ploopback->sema, 0); + ploopback->bstop = _TRUE; + ploopback->cnt = 0; + ploopback->size = 300; + _rtw_memset(ploopback->msg, 0, sizeof(ploopback->msg)); + + padapter->ploopback = ploopback; + } + + return 0; +} + +static void freeLoopback(PADAPTER padapter) +{ + PLOOPBACKDATA ploopback; + + ploopback = padapter->ploopback; + if (ploopback) { + rtw_mfree((u8 *)ploopback, sizeof(LOOPBACKDATA)); + padapter->ploopback = NULL; + } +} + +static s32 initpseudoadhoc(PADAPTER padapter) +{ + NDIS_802_11_NETWORK_INFRASTRUCTURE networkType; + s32 err; + + networkType = Ndis802_11IBSS; + err = rtw_set_802_11_infrastructure_mode(padapter, networkType, 0); + if (err == _FALSE) + return _FAIL; + + err = rtw_setopmode_cmd(padapter, networkType, RTW_CMDF_WAIT_ACK); + if (err == _FAIL) + return _FAIL; + + return _SUCCESS; +} + +static s32 createpseudoadhoc(PADAPTER padapter) +{ + NDIS_802_11_AUTHENTICATION_MODE authmode; + struct mlme_priv *pmlmepriv; + NDIS_802_11_SSID *passoc_ssid; + WLAN_BSSID_EX *pdev_network; + u8 *pibss; + u8 ssid[] = "pseduo_ad-hoc"; + s32 err; + _irqL irqL; + + pmlmepriv = &padapter->mlmepriv; + + authmode = Ndis802_11AuthModeOpen; + err = rtw_set_802_11_authentication_mode(padapter, authmode); + if (err == _FALSE) + return _FAIL; + + passoc_ssid = &pmlmepriv->assoc_ssid; + _rtw_memset(passoc_ssid, 0, sizeof(NDIS_802_11_SSID)); + passoc_ssid->SsidLength = sizeof(ssid) - 1; + _rtw_memcpy(passoc_ssid->Ssid, ssid, passoc_ssid->SsidLength); + + pdev_network = &padapter->registrypriv.dev_network; + pibss = padapter->registrypriv.dev_network.MacAddress; + _rtw_memcpy(&pdev_network->Ssid, passoc_ssid, sizeof(NDIS_802_11_SSID)); + + rtw_update_registrypriv_dev_network(padapter); + rtw_generate_random_ibss(pibss); + + _enter_critical_bh(&pmlmepriv->lock, &irqL); + /*pmlmepriv->fw_state = WIFI_ADHOC_MASTER_STATE;*/ + init_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE); + + _exit_critical_bh(&pmlmepriv->lock, &irqL); + +#if 0 + err = rtw_create_ibss_cmd(padapter, 0); + if (err == _FAIL) + return _FAIL; +#else + { + struct wlan_network *pcur_network; + struct sta_info *psta; + + /* 3 create a new psta */ + pcur_network = &pmlmepriv->cur_network; + + /* clear psta in the cur_network, if any */ + psta = rtw_get_stainfo(&padapter->stapriv, pcur_network->network.MacAddress); + if (psta) + rtw_free_stainfo(padapter, psta); + + psta = rtw_alloc_stainfo(&padapter->stapriv, pibss); + if (psta == NULL) + return _FAIL; + + /* 3 join psudo AdHoc */ + pcur_network->join_res = 1; + pcur_network->aid = psta->cmn.aid = 1; + _rtw_memcpy(&pcur_network->network, pdev_network, get_WLAN_BSSID_EX_sz(pdev_network)); + + /* set msr to WIFI_FW_ADHOC_STATE */ + padapter->hw_port = HW_PORT0; + Set_MSR(padapter, WIFI_FW_ADHOC_STATE); + + } +#endif + + return _SUCCESS; +} + +static struct xmit_frame *createloopbackpkt(PADAPTER padapter, u32 size) +{ + struct xmit_priv *pxmitpriv; + struct xmit_frame *pframe; + struct xmit_buf *pxmitbuf; + struct pkt_attrib *pattrib; + struct tx_desc *desc; + u8 *pkt_start, *pkt_end, *ptr; + struct rtw_ieee80211_hdr *hdr; + s32 bmcast; + _irqL irqL; + + + if ((TXDESC_SIZE + WLANHDR_OFFSET + size) > MAX_XMITBUF_SZ) + return NULL; + + pxmitpriv = &padapter->xmitpriv; + pframe = NULL; + + /* 2 1. allocate xmit frame */ + pframe = rtw_alloc_xmitframe(pxmitpriv, 0); + if (pframe == NULL) + return NULL; + pframe->padapter = padapter; + + /* 2 2. allocate xmit buffer */ + _enter_critical_bh(&pxmitpriv->lock, &irqL); + pxmitbuf = rtw_alloc_xmitbuf(pxmitpriv); + _exit_critical_bh(&pxmitpriv->lock, &irqL); + if (pxmitbuf == NULL) { + rtw_free_xmitframe(pxmitpriv, pframe); + return NULL; + } + + pframe->pxmitbuf = pxmitbuf; + pframe->buf_addr = pxmitbuf->pbuf; + pxmitbuf->priv_data = pframe; + + /* 2 3. update_attrib() */ + pattrib = &pframe->attrib; + + /* init xmitframe attribute */ + _rtw_memset(pattrib, 0, sizeof(struct pkt_attrib)); + + pattrib->ether_type = 0x8723; + _rtw_memcpy(pattrib->src, adapter_mac_addr(padapter), ETH_ALEN); + _rtw_memcpy(pattrib->ta, pattrib->src, ETH_ALEN); + _rtw_memset(pattrib->dst, 0xFF, ETH_ALEN); + _rtw_memcpy(pattrib->ra, pattrib->dst, ETH_ALEN); + + /* pattrib->dhcp_pkt = 0; + * pattrib->pktlen = 0; */ + pattrib->ack_policy = 0; + /* pattrib->pkt_hdrlen = ETH_HLEN; */ + pattrib->hdrlen = WLAN_HDR_A3_LEN; + pattrib->subtype = WIFI_DATA; + pattrib->priority = 0; + pattrib->qsel = pattrib->priority; + /* do_queue_select(padapter, pattrib); */ + pattrib->nr_frags = 1; + pattrib->encrypt = 0; + pattrib->bswenc = _FALSE; + pattrib->qos_en = _FALSE; + + bmcast = IS_MCAST(pattrib->ra); + if (bmcast) + pattrib->psta = rtw_get_bcmc_stainfo(padapter); + else + pattrib->psta = rtw_get_stainfo(&padapter->stapriv, get_bssid(&padapter->mlmepriv)); + + pattrib->mac_id = pattrib->psta->cmn.mac_id; + pattrib->pktlen = size; + pattrib->last_txcmdsz = pattrib->hdrlen + pattrib->pktlen; + + /* 2 4. fill TX descriptor */ + desc = (struct tx_desc *)pframe->buf_addr; + _rtw_memset(desc, 0, TXDESC_SIZE); + + fill_default_txdesc(pframe, (u8 *)desc); + +#if 0 + /* Hw set sequence number */ + ((PTXDESC)desc)->hwseq_en = 0; /* HWSEQ_EN, 0:disable, 1:enable + * ((PTXDESC)desc)->hwseq_sel = 0; */ /* HWSEQ_SEL */ + + ((PTXDESC)desc)->disdatafb = 1; + + /* convert to little endian */ + desc->txdw0 = cpu_to_le32(desc->txdw0); + desc->txdw1 = cpu_to_le32(desc->txdw1); + desc->txdw2 = cpu_to_le32(desc->txdw2); + desc->txdw3 = cpu_to_le32(desc->txdw3); + desc->txdw4 = cpu_to_le32(desc->txdw4); + desc->txdw5 = cpu_to_le32(desc->txdw5); + desc->txdw6 = cpu_to_le32(desc->txdw6); + desc->txdw7 = cpu_to_le32(desc->txdw7); +#ifdef CONFIG_PCI_HCI + desc->txdw8 = cpu_to_le32(desc->txdw8); + desc->txdw9 = cpu_to_le32(desc->txdw9); + desc->txdw10 = cpu_to_le32(desc->txdw10); + desc->txdw11 = cpu_to_le32(desc->txdw11); + desc->txdw12 = cpu_to_le32(desc->txdw12); + desc->txdw13 = cpu_to_le32(desc->txdw13); + desc->txdw14 = cpu_to_le32(desc->txdw14); + desc->txdw15 = cpu_to_le32(desc->txdw15); +#endif +#endif + + cal_txdesc_chksum(padapter, (u8*)desc); + /* dump_txdesc_data(padapter, (u8*)desc); */ + + /* 2 5. coalesce */ + pkt_start = pframe->buf_addr + TXDESC_SIZE; + pkt_end = pkt_start + pattrib->last_txcmdsz; + + /* 3 5.1. make wlan header, make_wlanhdr() */ + hdr = (struct rtw_ieee80211_hdr *)pkt_start; + set_frame_sub_type(&hdr->frame_ctl, pattrib->subtype); + _rtw_memcpy(hdr->addr1, pattrib->dst, ETH_ALEN); /* DA */ + _rtw_memcpy(hdr->addr2, pattrib->src, ETH_ALEN); /* SA */ + _rtw_memcpy(hdr->addr3, get_bssid(&padapter->mlmepriv), ETH_ALEN); /* RA, BSSID */ + + /* 3 5.2. make payload */ + ptr = pkt_start + pattrib->hdrlen; + get_random_bytes(ptr, pkt_end - ptr); + + pxmitbuf->len = TXDESC_SIZE + pattrib->last_txcmdsz; +#if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) + pxmitbuf->ptail += pxmitbuf->len; +#endif + + dbg_dump_pkt("TX packet", pxmitbuf->pbuf, pxmitbuf->len); + + return pframe; +} + +static void freeloopbackpkt(PADAPTER padapter, struct xmit_frame *pframe) +{ + struct xmit_priv *pxmitpriv; + struct xmit_buf *pxmitbuf; + + pxmitpriv = &padapter->xmitpriv; + pxmitbuf = pframe->pxmitbuf; + + rtw_free_xmitframe(pxmitpriv, pframe); + rtw_free_xmitbuf(pxmitpriv, pxmitbuf); +} + +static void printdata(u8 *pbuf, u32 len) +{ + u32 i, val; + + for (i = 0; (i + 4) <= len; i += 4) { + printk("%08X", *(u32 *)(pbuf + i)); + if ((i + 4) & 0x1F) + printk(" "); + else + printk("\n"); + } + + if (i < len) { +#ifdef CONFIG_BIG_ENDIAN + for (; i < len, i++) + printk("%02X", pbuf + i); +#else /* CONFIG_LITTLE_ENDIAN */ +#if 0 + val = 0; + _rtw_memcpy(&val, pbuf + i, len - i); + printk("%8X", val); +#else + u8 str[9]; + u8 n; + val = 0; + n = len - i; + _rtw_memcpy(&val, pbuf + i, n); + sprintf(str, "%08X", val); + n = (4 - n) * 2; + printk("%8s", str + n); +#endif +#endif /* CONFIG_LITTLE_ENDIAN */ + } + printk("\n"); +} + +static u8 pktcmp(PADAPTER padapter, u8 *txbuf, u32 txsz, u8 *rxbuf, u32 rxsz) +{ + struct rx_pkt_attrib rx_desc; +#if 0 + struct recv_stat *prxstat; + struct recv_stat report; + PRXREPORT prxreport; +#endif + u32 rxpktsize; + u8 drvinfosize; + u8 shiftsize; + u8 ret = _FALSE; + u8 skip_len = 4; /* Don't compare the frame control and duration field */ + get_rx_desc(&rx_desc, rxbuf); + rxpktsize = rx_desc.pkt_len; + drvinfosize = rx_desc.drvinfo_sz; + shiftsize = rx_desc.shift_sz; + +#if 0 + prxstat = (struct recv_stat *)rxbuf; + report.rxdw0 = le32_to_cpu(prxstat->rxdw0); + report.rxdw1 = le32_to_cpu(prxstat->rxdw1); + report.rxdw2 = le32_to_cpu(prxstat->rxdw2); + report.rxdw3 = le32_to_cpu(prxstat->rxdw3); + report.rxdw4 = le32_to_cpu(prxstat->rxdw4); + report.rxdw5 = le32_to_cpu(prxstat->rxdw5); + + prxreport = (PRXREPORT)&report; + drvinfosize = prxreport->drvinfosize << 3; + rxpktsize = prxreport->pktlen; +#endif + + if (rtw_hal_rcr_check(padapter, RCR_APPFCS)) + rxpktsize -= IEEE80211_FCS_LEN; + + if ((txsz - TXDESC_SIZE) != rxpktsize) { + RTW_INFO("%s: ERROR! size not match tx/rx=%d/%d !\n", + __func__, txsz - TXDESC_SIZE, rxpktsize); + ret = _FALSE; + } else { + ret = _rtw_memcmp(txbuf + TXDESC_SIZE + skip_len, \ + rxbuf + RXDESC_SIZE + skip_len + drvinfosize, \ + txsz - TXDESC_SIZE - skip_len); + if (ret == _FALSE) + RTW_INFO("%s: ERROR! pkt content mismatch!\n", __func__); + } + + if (ret == _FALSE) { + RTW_INFO("\n%s: TX PKT total=%d, desc=%d, content=%d\n", + __func__, txsz, TXDESC_SIZE, txsz - TXDESC_SIZE); + dbg_dump_pkt("TX DESC", txbuf, TXDESC_SIZE); + dbg_dump_pkt("TX content", txbuf + TXDESC_SIZE, txsz - TXDESC_SIZE); + + RTW_INFO("\n%s: RX PKT read=%d offset=%d(%d,%d) content=%d\n", + __func__, rxsz, RXDESC_SIZE + drvinfosize, RXDESC_SIZE, drvinfosize, rxpktsize); + if (rxpktsize != 0) { + dbg_dump_pkt("RX DESC", rxbuf, RXDESC_SIZE); + dbg_dump_pkt("RX drvinfo", rxbuf + RXDESC_SIZE, drvinfosize); + dbg_dump_pkt("RX packet content", rxbuf + RXDESC_SIZE + drvinfosize, rxpktsize); + } else { + RTW_INFO("%s: RX data size=%d\n", __func__, rxsz); + } + } + + return ret; +} + +thread_return lbk_thread(thread_context context) +{ + s32 err; + PADAPTER padapter; + PLOOPBACKDATA ploopback; + struct xmit_frame *pxmitframe; + u32 cnt, ok, fail, headerlen; + u32 pktsize; + u32 ff_hwaddr; + + padapter = (PADAPTER)context; + ploopback = padapter->ploopback; + if (ploopback == NULL) + return -1; + cnt = 0; + ok = 0; + fail = 0; + + thread_enter("RTW_LBK_THREAD"); + /* daemonize("%s", "RTW_LBK_THREAD"); */ + allow_signal(SIGTERM); + + do { + if (ploopback->size == 0) { + get_random_bytes(&pktsize, 4); + pktsize = (pktsize % 1535) + 1; /* 1~1535 */ + } else + pktsize = ploopback->size; + + pxmitframe = createloopbackpkt(padapter, pktsize); + if (pxmitframe == NULL) { + sprintf(ploopback->msg, "loopback FAIL! 3. create Packet FAIL!"); + break; + } + + ploopback->txsize = TXDESC_SIZE + pxmitframe->attrib.last_txcmdsz; + _rtw_memcpy(ploopback->txbuf, pxmitframe->buf_addr, ploopback->txsize); + ff_hwaddr = rtw_get_ff_hwaddr(pxmitframe); + cnt++; + RTW_INFO("%s: wirte port cnt=%d size=%d\n", __func__, cnt, ploopback->txsize); +#if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) + pxmitframe->pxmitbuf->pdata = ploopback->txbuf; +#endif + rtw_write_port(padapter, ff_hwaddr, ploopback->txsize, (u8 *)pxmitframe->pxmitbuf); + + /* wait for rx pkt */ + RTW_INFO("%s: wait for rx packet\n", __func__); + _rtw_down_sema(&ploopback->sema); + + err = pktcmp(padapter, ploopback->txbuf, ploopback->txsize, ploopback->rxbuf, ploopback->rxsize); + if (err == _TRUE) + ok++; + else + fail++; + + ploopback->txsize = 0; + _rtw_memset(ploopback->txbuf, 0, 0x8000); + ploopback->rxsize = 0; + _rtw_memset(ploopback->rxbuf, 0, 0x8000); + + freeloopbackpkt(padapter, pxmitframe); + pxmitframe = NULL; + + flush_signals_thread(); + + if ((ploopback->bstop == _TRUE) || + ((ploopback->cnt != 0) && (ploopback->cnt == cnt))) { + u32 ok_rate, fail_rate, all; + all = cnt; + ok_rate = (ok * 100) / all; + fail_rate = (fail * 100) / all; + sprintf(ploopback->msg, \ + "loopback result: ok=%d%%(%d/%d),error=%d%%(%d/%d)", \ + ok_rate, ok, all, fail_rate, fail, all); + break; + } + } while (1); + + ploopback->bstop = _TRUE; + + thread_exit(NULL); + return 0; +} + +static void loopbackTest(PADAPTER padapter, u32 cnt, u32 size, u8 *pmsg) +{ + PLOOPBACKDATA ploopback; + u32 len; + s32 err; + + ploopback = padapter->ploopback; + + if (ploopback) { + if (ploopback->bstop == _FALSE) { + ploopback->bstop = _TRUE; + _rtw_up_sema(&ploopback->sema); + } + len = 0; + do { + len = strlen(ploopback->msg); + if (len) + break; + rtw_msleep_os(1); + } while (1); + RTW_INFO("Free loopback, end the test.\n"); + _rtw_memcpy(pmsg, ploopback->msg, len + 1); + freeLoopback(padapter); + + return; + } + + /* disable dynamic algorithm */ +#ifndef CONFIG_NO_PHYDM + rtw_phydm_ability_backup(padapter); + rtw_phydm_func_disable_all(padapter); +#endif + + /* create pseudo ad-hoc connection */ + err = initpseudoadhoc(padapter); + if (err == _FAIL) { + sprintf(pmsg, "loopback FAIL! 1.1 init ad-hoc FAIL!"); + return; + } + + err = createpseudoadhoc(padapter); + if (err == _FAIL) { + sprintf(pmsg, "loopback FAIL! 1.2 create ad-hoc master FAIL!"); + return; + } + + err = initLoopback(padapter); + if (err) { + sprintf(pmsg, "loopback FAIL! 2. init FAIL! error code=%d", err); + return; + } + + ploopback = padapter->ploopback; + + ploopback->bstop = _FALSE; + ploopback->cnt = cnt; + ploopback->size = size; + ploopback->lbkthread = kthread_run(lbk_thread, padapter, "RTW_LBK_THREAD"); + if (IS_ERR(ploopback->lbkthread)) { + freeLoopback(padapter); + ploopback->lbkthread = NULL; + sprintf(pmsg, "loopback start FAIL! cnt=%d", cnt); + return; + } + + sprintf(pmsg, "loopback start! cnt=%d", cnt); +} +#endif /* CONFIG_MAC_LOOPBACK_DRIVER */ + +static int rtw_test( + struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + u32 len; + u8 *pbuf, *pch; + char *ptmp; + u8 *delim = ","; + PADAPTER padapter = rtw_netdev_priv(dev); + + + RTW_INFO("+%s\n", __func__); + len = wrqu->data.length; + + pbuf = (u8 *)rtw_zmalloc(len + 1); + if (pbuf == NULL) { + RTW_INFO("%s: no memory!\n", __func__); + return -ENOMEM; + } + + if (copy_from_user(pbuf, wrqu->data.pointer, len)) { + rtw_mfree(pbuf, len + 1); + RTW_INFO("%s: copy from user fail!\n", __func__); + return -EFAULT; + } + + pbuf[len] = '\0'; + + RTW_INFO("%s: string=\"%s\"\n", __func__, pbuf); + + ptmp = (char *)pbuf; + pch = strsep(&ptmp, delim); + if ((pch == NULL) || (strlen(pch) == 0)) { + rtw_mfree(pbuf, len); + RTW_INFO("%s: parameter error(level 1)!\n", __func__); + return -EFAULT; + } + +#ifdef CONFIG_MAC_LOOPBACK_DRIVER + if (strcmp(pch, "init") == 0) { + u8 status; + + rtw_clr_drv_stopped(padapter); /* should clear drv_stopped, otherwise driver can't trx */ + + status = hal_init(padapter); + RTW_INFO("HAL_INIT %s\n", status ? "SUCCESS" : "FAIL"); + + rtw_write8(padapter, REG_LOOPBACK_ENABLE, LOOKBACK_ENABLE_VALUE); + RTW_INFO("Write 0x%03x to 0x%02x, enable loopback\n", + REG_LOOPBACK_ENABLE, LOOKBACK_ENABLE_VALUE); + + } else if (strcmp(pch, "loopback") == 0) { + s32 cnt = 0; + u32 size = 64; + + pch = strsep(&ptmp, delim); + if ((pch == NULL) || (strlen(pch) == 0)) { + rtw_mfree(pbuf, len); + RTW_INFO("%s: parameter error(level 2)!\n", __func__); + return -EFAULT; + } + + sscanf(pch, "%d", &cnt); + RTW_INFO("%s: loopback cnt=%d\n", __func__, cnt); + + pch = strsep(&ptmp, delim); + if ((pch == NULL) || (strlen(pch) == 0)) { + rtw_mfree(pbuf, len); + RTW_INFO("%s: parameter error(level 2)!\n", __func__); + return -EFAULT; + } + + sscanf(pch, "%d", &size); + RTW_INFO("%s: loopback size=%d\n", __func__, size); + + loopbackTest(padapter, cnt, size, extra); + wrqu->data.length = strlen(extra) + 1; + + goto free_buf; + } +#endif + + +#ifdef CONFIG_BT_COEXIST + if (strcmp(pch, "bton") == 0) { + rtw_btcoex_SetManualControl(padapter, _FALSE); + goto free_buf; + } else if (strcmp(pch, "btoff") == 0) { + rtw_btcoex_SetManualControl(padapter, _TRUE); + goto free_buf; + } else if (strcmp(pch, "coex_auto") == 0) { + rtw_btcoex_set_policy_control(padapter, BTCOEX_POLICY_CONTROL_AUTO); + goto free_buf; + } else if (strcmp(pch, "coex_force_freerun") == 0) { + rtw_btcoex_set_policy_control(padapter, BTCOEX_POLICY_CONTROL_FORCE_FREERUN); + goto free_buf; + } else if (strcmp(pch, "coex_force_tdma") == 0) { + rtw_btcoex_set_policy_control(padapter, BTCOEX_POLICY_CONTROL_FORCE_TDMA); + goto free_buf; + } +#endif + + if (strcmp(pch, "h2c") == 0) { + u8 param[8]; + u8 count = 0; + u32 tmp; + u8 i; + u32 pos; + u8 ret; + + do { + pch = strsep(&ptmp, delim); + if ((pch == NULL) || (strlen(pch) == 0)) + break; + + sscanf(pch, "%x", &tmp); + param[count++] = (u8)tmp; + } while (count < 8); + + if (count == 0) { + rtw_mfree(pbuf, len); + RTW_INFO("%s: parameter error(level 2)!\n", __func__); + return -EFAULT; + } + + ret = rtw_test_h2c_cmd(padapter, param, count); + + pos = sprintf(extra, "H2C ID=0x%02x content=", param[0]); + for (i = 1; i < count; i++) + pos += sprintf(extra + pos, "%02x,", param[i]); + extra[pos] = 0; + pos--; + pos += sprintf(extra + pos, " %s", ret == _FAIL ? "FAIL" : "OK"); + + wrqu->data.length = strlen(extra) + 1; + + goto free_buf; + } + + if (strcmp(pch, "dump_mac_reg") == 0) { + mac_reg_dump(RTW_DBGDUMP, padapter); + goto free_buf; + } + +free_buf: + rtw_mfree(pbuf, len); + return 0; +} + +static iw_handler rtw_handlers[] = { +#ifdef CONFIG_IOCTL_WEXT + NULL, /* SIOCSIWCOMMIT */ + rtw_wx_get_name, /* SIOCGIWNAME */ + dummy, /* SIOCSIWNWID */ + dummy, /* SIOCGIWNWID */ + rtw_wx_set_freq, /* SIOCSIWFREQ */ + rtw_wx_get_freq, /* SIOCGIWFREQ */ + rtw_wx_set_mode, /* SIOCSIWMODE */ + rtw_wx_get_mode, /* SIOCGIWMODE */ + dummy, /* SIOCSIWSENS */ + rtw_wx_get_sens, /* SIOCGIWSENS */ + NULL, /* SIOCSIWRANGE */ + rtw_wx_get_range, /* SIOCGIWRANGE */ + rtw_wx_set_priv, /* SIOCSIWPRIV */ + NULL, /* SIOCGIWPRIV */ + NULL, /* SIOCSIWSTATS */ + NULL, /* SIOCGIWSTATS */ + dummy, /* SIOCSIWSPY */ + dummy, /* SIOCGIWSPY */ + NULL, /* SIOCGIWTHRSPY */ + NULL, /* SIOCWIWTHRSPY */ + rtw_wx_set_wap, /* SIOCSIWAP */ + rtw_wx_get_wap, /* SIOCGIWAP */ + rtw_wx_set_mlme, /* request MLME operation; uses struct iw_mlme */ + dummy, /* SIOCGIWAPLIST -- depricated */ + rtw_wx_set_scan, /* SIOCSIWSCAN */ + rtw_wx_get_scan, /* SIOCGIWSCAN */ + rtw_wx_set_essid, /* SIOCSIWESSID */ + rtw_wx_get_essid, /* SIOCGIWESSID */ + dummy, /* SIOCSIWNICKN */ + rtw_wx_get_nick, /* SIOCGIWNICKN */ + NULL, /* -- hole -- */ + NULL, /* -- hole -- */ + rtw_wx_set_rate, /* SIOCSIWRATE */ + rtw_wx_get_rate, /* SIOCGIWRATE */ + rtw_wx_set_rts, /* SIOCSIWRTS */ + rtw_wx_get_rts, /* SIOCGIWRTS */ + rtw_wx_set_frag, /* SIOCSIWFRAG */ + rtw_wx_get_frag, /* SIOCGIWFRAG */ + dummy, /* SIOCSIWTXPOW */ + dummy, /* SIOCGIWTXPOW */ + dummy, /* SIOCSIWRETRY */ + rtw_wx_get_retry, /* SIOCGIWRETRY */ + rtw_wx_set_enc, /* SIOCSIWENCODE */ + rtw_wx_get_enc, /* SIOCGIWENCODE */ + dummy, /* SIOCSIWPOWER */ + rtw_wx_get_power, /* SIOCGIWPOWER */ + NULL, /*---hole---*/ + NULL, /*---hole---*/ + rtw_wx_set_gen_ie, /* SIOCSIWGENIE */ + NULL, /* SIOCGWGENIE */ + rtw_wx_set_auth, /* SIOCSIWAUTH */ + NULL, /* SIOCGIWAUTH */ + rtw_wx_set_enc_ext, /* SIOCSIWENCODEEXT */ + NULL, /* SIOCGIWENCODEEXT */ + rtw_wx_set_pmkid, /* SIOCSIWPMKSA */ + NULL, /*---hole---*/ +#endif +}; + + +static const struct iw_priv_args rtw_private_args[] = { + { + SIOCIWFIRSTPRIV + 0x0, + IW_PRIV_TYPE_CHAR | 0x7FF, 0, "write" + }, + { + SIOCIWFIRSTPRIV + 0x1, + IW_PRIV_TYPE_CHAR | 0x7FF, + IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | IFNAMSIZ, "read" + }, + { + SIOCIWFIRSTPRIV + 0x2, 0, 0, "driver_ext" + }, + { + SIOCIWFIRSTPRIV + 0x3, 0, 0, "mp_ioctl" + }, + { + SIOCIWFIRSTPRIV + 0x4, + IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "apinfo" + }, + { + SIOCIWFIRSTPRIV + 0x5, + IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "setpid" + }, + { + SIOCIWFIRSTPRIV + 0x6, + IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "wps_start" + }, + /* for PLATFORM_MT53XX */ + { + SIOCIWFIRSTPRIV + 0x7, + IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "get_sensitivity" + }, + { + SIOCIWFIRSTPRIV + 0x8, + IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "wps_prob_req_ie" + }, + { + SIOCIWFIRSTPRIV + 0x9, + IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "wps_assoc_req_ie" + }, + + /* for RTK_DMP_PLATFORM */ + { + SIOCIWFIRSTPRIV + 0xA, + IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "channel_plan" + }, + + { + SIOCIWFIRSTPRIV + 0xB, + IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "dbg" + }, + { + SIOCIWFIRSTPRIV + 0xC, + IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 3, 0, "rfw" + }, + { + SIOCIWFIRSTPRIV + 0xD, + IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | IFNAMSIZ, "rfr" + }, +#if 0 + { + SIOCIWFIRSTPRIV + 0xE, 0, 0, "wowlan_ctrl" + }, +#endif + { + SIOCIWFIRSTPRIV + 0x10, + IW_PRIV_TYPE_CHAR | 1024, 0, "p2p_set" + }, + { + SIOCIWFIRSTPRIV + 0x11, + IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK , "p2p_get" + }, + { + SIOCIWFIRSTPRIV + 0x12, 0, 0, "NULL" + }, + { + SIOCIWFIRSTPRIV + 0x13, + IW_PRIV_TYPE_CHAR | 64, IW_PRIV_TYPE_CHAR | 64 , "p2p_get2" + }, + { + SIOCIWFIRSTPRIV + 0x14, + IW_PRIV_TYPE_CHAR | 64, 0, "tdls" + }, + { + SIOCIWFIRSTPRIV + 0x15, + IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | 1024 , "tdls_get" + }, + { + SIOCIWFIRSTPRIV + 0x16, + IW_PRIV_TYPE_CHAR | 64, 0, "pm_set" + }, +#ifdef CONFIG_RTW_80211K + { + SIOCIWFIRSTPRIV + 0x17, + IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | 1024 , "rrm" + }, +#else + {SIOCIWFIRSTPRIV + 0x17, IW_PRIV_TYPE_CHAR | 1024 , 0 , "NULL"}, +#endif + +#ifdef CONFIG_PLATFORM_CMAP_INTFS + {SIOCIWFIRSTPRIV + 0x18, IW_PRIV_TYPE_CHAR | 1024 , 0 , "cmap_intfs"}, +#else + {SIOCIWFIRSTPRIV + 0x18, 0, 0, "NULL"}, +#endif + +#ifdef CONFIG_MP_INCLUDED + {SIOCIWFIRSTPRIV + 0x1A, IW_PRIV_TYPE_CHAR | 1024, 0, "NULL"}, + {SIOCIWFIRSTPRIV + 0x1B, IW_PRIV_TYPE_CHAR | 128, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "NULL"}, +#else + {SIOCIWFIRSTPRIV + 0x1A, IW_PRIV_TYPE_CHAR | 1024, 0, "NULL"}, + {SIOCIWFIRSTPRIV + 0x1B, IW_PRIV_TYPE_CHAR | 128, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "efuse_get"}, +#endif + { + SIOCIWFIRSTPRIV + 0x1D, + IW_PRIV_TYPE_CHAR | 40, IW_PRIV_TYPE_CHAR | 0x7FF, "test" + }, + + { SIOCIWFIRSTPRIV + 0x0E, IW_PRIV_TYPE_CHAR | 1024, 0 , ""}, /* set */ + { SIOCIWFIRSTPRIV + 0x0F, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK , ""},/* get + * --- sub-ioctls definitions --- */ + +#ifdef CONFIG_APPEND_VENDOR_IE_ENABLE + { VENDOR_IE_SET, IW_PRIV_TYPE_CHAR | 1024 , 0 , "vendor_ie_set" }, + { VENDOR_IE_GET, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "vendor_ie_get" }, +#endif +#if defined(CONFIG_RTL8723B) + { MP_SetBT, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_setbt" }, + { MP_DISABLE_BT_COEXIST, IW_PRIV_TYPE_CHAR | 1024, 0, "mp_disa_btcoex"}, +#endif +#ifdef CONFIG_WOWLAN + { MP_WOW_ENABLE , IW_PRIV_TYPE_CHAR | 1024, 0, "wow_mode" }, + { MP_WOW_SET_PATTERN , IW_PRIV_TYPE_CHAR | 1024, 0, "wow_set_pattern" }, +#ifdef CONFIG_WOW_KEEP_ALIVE_PATTERN + { MP_WOW_SET_KEEP_ALIVE_PATTERN ,IW_PRIV_TYPE_CHAR | 1024 , 0 , "wow_keep_alive"}, +#endif /* defined (CONFIG_KEEP_ALIVE_PATTERN)*/ +#endif + +#ifdef CONFIG_AP_WOWLAN + { MP_AP_WOW_ENABLE , IW_PRIV_TYPE_CHAR | 1024, 0, "ap_wow_mode" }, /* set */ +#endif +#ifdef CONFIG_SDIO_INDIRECT_ACCESS + { MP_SD_IREAD, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "sd_iread" }, + { MP_SD_IWRITE, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "sd_iwrite" }, +#endif +}; + + +static const struct iw_priv_args rtw_mp_private_args[] = { + /* --- sub-ioctls definitions --- */ +#ifdef CONFIG_MP_INCLUDED + { MP_START , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_start" }, + { MP_PHYPARA, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_phypara" }, + { MP_STOP , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_stop" }, + { MP_CHANNEL , IW_PRIV_TYPE_CHAR | 1024 , IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_channel" }, + { MP_CHL_OFFSET , IW_PRIV_TYPE_CHAR | 1024 , IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_ch_offset" }, + { MP_BANDWIDTH , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_bandwidth"}, + { MP_RATE , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_rate" }, + { MP_RESET_STATS , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_reset_stats"}, + { MP_QUERY , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK , "mp_query"}, + { READ_REG , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "read_reg" }, + { MP_RATE , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_rate" }, + { READ_RF , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "read_rf" }, + { MP_PSD , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_psd"}, + { MP_DUMP, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_dump" }, + { MP_TXPOWER , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_txpower"}, + { MP_ANT_TX , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_ant_tx"}, + { MP_ANT_RX , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_ant_rx"}, + { WRITE_REG , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "write_reg" }, + { WRITE_RF , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "write_rf" }, + { MP_CTX , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_ctx"}, + { MP_ARX , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_arx"}, + { MP_THER , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_ther"}, + { EFUSE_SET, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "efuse_set" }, + { EFUSE_GET, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "efuse_get" }, + { MP_PWRTRK , IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_pwrtrk"}, + { MP_QueryDrvStats, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_drvquery" }, + { MP_IOCTL, IW_PRIV_TYPE_CHAR | 1024, 0, "mp_ioctl"}, + { MP_SetRFPathSwh, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_setrfpath" }, + { MP_PwrCtlDM, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_pwrctldm" }, + { MP_GET_TXPOWER_INX, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_get_txpower" }, + { MP_GETVER, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_priv_ver" }, + { MP_MON, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_mon" }, + { EFUSE_BT_MASK, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "efuse_bt_mask" }, + { EFUSE_MASK, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "efuse_mask" }, + { EFUSE_FILE, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "efuse_file" }, + { EFUSE_FILE_STORE, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "efuse_store" }, + { MP_TX, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_tx" }, + { MP_RX, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_rx" }, + { MP_HW_TX_MODE, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_hxtx" }, + { MP_PWRLMT, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_pwrlmt" }, + { MP_PWRBYRATE, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_pwrbyrate" }, + { CTA_TEST, IW_PRIV_TYPE_CHAR | 1024, 0, "cta_test"}, + { MP_IQK, IW_PRIV_TYPE_CHAR | 1024, 0, "mp_iqk"}, + { MP_LCK, IW_PRIV_TYPE_CHAR | 1024, 0, "mp_lck"}, + { BT_EFUSE_FILE, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "bt_efuse_file" }, + { MP_SWRFPath, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_swrfpath" }, + { MP_LINK, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_link" }, + { MP_DPK, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_dpk"}, + { MP_DPK_TRK, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_dpk_trk" }, + { MP_GET_TSSIDE, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_get_tsside" }, + { MP_SET_TSSIDE, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "mp_set_tsside" }, +#ifdef CONFIG_RTW_CUSTOMER_STR + { MP_CUSTOMER_STR, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "customer_str" }, +#endif + +#endif /* CONFIG_MP_INCLUDED */ +}; + +static iw_handler rtw_private_handler[] = { + rtw_wx_write32, /* 0x00 */ + rtw_wx_read32, /* 0x01 */ + NULL, /* 0x02 */ +#ifdef MP_IOCTL_HDL + rtw_mp_ioctl_hdl, /* 0x03 */ +#else + rtw_wx_priv_null, +#endif + /* for MM DTV platform */ + rtw_get_ap_info, /* 0x04 */ + + rtw_set_pid, /* 0x05 */ + rtw_wps_start, /* 0x06 */ + + /* for PLATFORM_MT53XX */ + rtw_wx_get_sensitivity, /* 0x07 */ + rtw_wx_set_mtk_wps_probe_ie, /* 0x08 */ + rtw_wx_set_mtk_wps_ie, /* 0x09 */ + + /* for RTK_DMP_PLATFORM + * Set Channel depend on the country code */ + rtw_wx_set_channel_plan, /* 0x0A */ + + rtw_dbg_port, /* 0x0B */ + rtw_wx_write_rf, /* 0x0C */ + rtw_wx_read_rf, /* 0x0D */ + + rtw_priv_set, /*0x0E*/ + rtw_priv_get, /*0x0F*/ + + rtw_p2p_set, /* 0x10 */ + rtw_p2p_get, /* 0x11 */ + NULL, /* 0x12 */ + rtw_p2p_get2, /* 0x13 */ + + rtw_tdls, /* 0x14 */ + rtw_tdls_get, /* 0x15 */ + + rtw_pm_set, /* 0x16 */ +#ifdef CONFIG_RTW_80211K + rtw_wx_priv_rrm, /* 0x17 */ +#else + rtw_wx_priv_null, /* 0x17 */ +#endif +#ifdef CONFIG_PLATFORM_CMAP_INTFS + cmap_intfs_ioctl, /* 0x18 */ +#else + NULL, /* 0x18 */ +#endif + rtw_wx_priv_null, /* 0x19 */ +#ifdef CONFIG_MP_INCLUDED + rtw_wx_priv_null, /* 0x1A */ + rtw_wx_priv_null, /* 0x1B */ +#else + rtw_wx_priv_null, /* 0x1A */ + rtw_mp_efuse_get, /* 0x1B */ +#endif + NULL, /* 0x1C is reserved for hostapd */ + rtw_test, /* 0x1D */ +}; + +#ifdef CONFIG_WIRELESS_EXT +#if WIRELESS_EXT >= 17 +static struct iw_statistics *rtw_get_wireless_stats(struct net_device *dev) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct iw_statistics *piwstats = &padapter->iwstats; + int tmp_level = 0; + int tmp_qual = 0; + int tmp_noise = 0; + + if (check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE) != _TRUE) { + piwstats->qual.qual = 0; + piwstats->qual.level = 0; + piwstats->qual.noise = 0; + /* RTW_INFO("No link level:%d, qual:%d, noise:%d\n", tmp_level, tmp_qual, tmp_noise); */ + } else { +#ifdef CONFIG_SIGNAL_DISPLAY_DBM + tmp_level = translate_percentage_to_dbm(padapter->recvpriv.signal_strength); +#else + tmp_level = padapter->recvpriv.signal_strength; +#endif + + tmp_qual = padapter->recvpriv.signal_qual; + #ifdef CONFIG_BACKGROUND_NOISE_MONITOR + if (IS_NM_ENABLE(padapter)) { + tmp_noise = rtw_noise_measure_curchan(padapter); + #ifndef CONFIG_SIGNAL_DISPLAY_DBM + tmp_noise = translate_dbm_to_percentage(tmp_noise);/*percentage*/ + #endif + } + #endif + /* RTW_INFO("level:%d, qual:%d, noise:%d, rssi (%d)\n", tmp_level, tmp_qual, tmp_noise,padapter->recvpriv.rssi); */ + + piwstats->qual.level = tmp_level; + piwstats->qual.qual = tmp_qual; + piwstats->qual.noise = tmp_noise; + } +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 14)) + piwstats->qual.updated = IW_QUAL_ALL_UPDATED ;/* |IW_QUAL_DBM; */ +#else +#ifdef RTK_DMP_PLATFORM + /* IW_QUAL_DBM= 0x8, if driver use this flag, wireless extension will show value of dbm. */ + /* remove this flag for show percentage 0~100 */ + piwstats->qual.updated = 0x07; +#else + piwstats->qual.updated = 0x0f; +#endif +#endif + +#ifdef CONFIG_SIGNAL_DISPLAY_DBM + piwstats->qual.updated = piwstats->qual.updated | IW_QUAL_DBM; +#endif + + return &padapter->iwstats; +} +#endif + +struct iw_handler_def rtw_handlers_def = { + .standard = rtw_handlers, + .num_standard = sizeof(rtw_handlers) / sizeof(iw_handler), +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33)) || defined(CONFIG_WEXT_PRIV) + .private = rtw_private_handler, + .private_args = (struct iw_priv_args *)rtw_private_args, + .num_private = sizeof(rtw_private_handler) / sizeof(iw_handler), + .num_private_args = sizeof(rtw_private_args) / sizeof(struct iw_priv_args), +#endif +#if WIRELESS_EXT >= 17 + .get_wireless_stats = rtw_get_wireless_stats, +#endif +}; +#endif + +/* copy from net/wireless/wext.c start + * ---------------------------------------------------------------- + * + * Calculate size of private arguments + */ +static const char iw_priv_type_size[] = { + 0, /* IW_PRIV_TYPE_NONE */ + 1, /* IW_PRIV_TYPE_BYTE */ + 1, /* IW_PRIV_TYPE_CHAR */ + 0, /* Not defined */ + sizeof(__u32), /* IW_PRIV_TYPE_INT */ + sizeof(struct iw_freq), /* IW_PRIV_TYPE_FLOAT */ + sizeof(struct sockaddr), /* IW_PRIV_TYPE_ADDR */ + 0, /* Not defined */ +}; + +static int get_priv_size(__u16 args) +{ + int num = args & IW_PRIV_SIZE_MASK; + int type = (args & IW_PRIV_TYPE_MASK) >> 12; + + return num * iw_priv_type_size[type]; +} +/* copy from net/wireless/wext.c end */ + + +static int _rtw_ioctl_wext_private(struct net_device *dev, union iwreq_data *wrq_data) +{ + int err = 0; + u8 *input = NULL; + u32 input_len = 0; + const char delim[] = " "; + u8 *output = NULL; + u32 output_len = 0; + u32 count = 0; + u8 *buffer = NULL; + u32 buffer_len = 0; + char *ptr = NULL; + u8 cmdname[17] = {0}; /* IFNAMSIZ+1 */ + u32 cmdlen; + s32 len; + u8 *extra = NULL; + u32 extra_size = 0; + + s32 k; + const iw_handler *priv; /* Private ioctl */ + const struct iw_priv_args *priv_args; /* Private ioctl description */ + const struct iw_priv_args *mp_priv_args; /*MP Private ioctl description */ + const struct iw_priv_args *sel_priv_args; /*Selected Private ioctl description */ + u32 num_priv; /* Number of ioctl */ + u32 num_priv_args; /* Number of descriptions */ + u32 num_mp_priv_args; /*Number of MP descriptions */ + u32 num_sel_priv_args; /*Number of Selected descriptions */ + iw_handler handler; + int temp; + int subcmd = 0; /* sub-ioctl index */ + int offset = 0; /* Space for sub-ioctl index */ + + union iwreq_data wdata; + + _rtw_memcpy(&wdata, wrq_data, sizeof(wdata)); + + input_len = wdata.data.length; + if (!input_len) + return -EINVAL; + input = rtw_zmalloc(input_len); + + if (input == NULL) { + err = -EOPNOTSUPP; + goto exit; + } + + if (copy_from_user(input, wdata.data.pointer, input_len)) { + err = -EFAULT; + goto exit; + } + input[input_len - 1] = '\0'; + ptr = input; + len = input_len; + + sscanf(ptr, "%16s", cmdname); + cmdlen = strlen(cmdname); + RTW_DBG("%s: cmd=%s\n", __func__, cmdname); + + /* skip command string */ + if (cmdlen > 0) + cmdlen += 1; /* skip one space */ + ptr += cmdlen; + len -= cmdlen; + RTW_DBG("%s: parameters=%s\n", __func__, ptr); + + priv = rtw_private_handler; + priv_args = rtw_private_args; + mp_priv_args = rtw_mp_private_args; + num_priv = sizeof(rtw_private_handler) / sizeof(iw_handler); + num_priv_args = sizeof(rtw_private_args) / sizeof(struct iw_priv_args); + num_mp_priv_args = sizeof(rtw_mp_private_args) / sizeof(struct iw_priv_args); + + if (num_priv_args == 0) { + err = -EOPNOTSUPP; + goto exit; + } + + /* Search the correct ioctl */ + k = -1; + sel_priv_args = priv_args; + num_sel_priv_args = num_priv_args; + while + ((++k < num_sel_priv_args) && strcmp(sel_priv_args[k].name, cmdname)) + ; + + /* If not found... */ + if (k == num_sel_priv_args) { + k = -1; + sel_priv_args = mp_priv_args; + num_sel_priv_args = num_mp_priv_args; + while + ((++k < num_sel_priv_args) && strcmp(sel_priv_args[k].name, cmdname)) + ; + + if (k == num_sel_priv_args) { + err = -EOPNOTSUPP; + goto exit; + } + } + + /* Watch out for sub-ioctls ! */ + if (sel_priv_args[k].cmd < SIOCDEVPRIVATE) { + int j = -1; + + /* Find the matching *real* ioctl */ + while ((++j < num_priv_args) && ((priv_args[j].name[0] != '\0') || + (priv_args[j].set_args != sel_priv_args[k].set_args) || + (priv_args[j].get_args != sel_priv_args[k].get_args))) + ; + + /* If not found... */ + if (j == num_priv_args) { + err = -EINVAL; + goto exit; + } + + /* Save sub-ioctl number */ + subcmd = sel_priv_args[k].cmd; + /* Reserve one int (simplify alignment issues) */ + offset = sizeof(__u32); + /* Use real ioctl definition from now on */ + k = j; + } + + buffer = rtw_zmalloc(4096); + if (NULL == buffer) { + err = -ENOMEM; + goto exit; + } + + if (k >= num_priv_args) { + err = -EINVAL; + goto exit; + } + + /* If we have to set some data */ + if ((priv_args[k].set_args & IW_PRIV_TYPE_MASK) && + (priv_args[k].set_args & IW_PRIV_SIZE_MASK)) { + u8 *str; + + switch (priv_args[k].set_args & IW_PRIV_TYPE_MASK) { + case IW_PRIV_TYPE_BYTE: + /* Fetch args */ + count = 0; + do { + str = strsep(&ptr, delim); + if (NULL == str) + break; + sscanf(str, "%i", &temp); + buffer[count++] = (u8)temp; + } while (1); + buffer_len = count; + + /* Number of args to fetch */ + wdata.data.length = count; + if (wdata.data.length > (priv_args[k].set_args & IW_PRIV_SIZE_MASK)) + wdata.data.length = priv_args[k].set_args & IW_PRIV_SIZE_MASK; + + break; + + case IW_PRIV_TYPE_INT: + /* Fetch args */ + count = 0; + do { + str = strsep(&ptr, delim); + if (NULL == str) + break; + sscanf(str, "%i", &temp); + ((s32 *)buffer)[count++] = (s32)temp; + } while (1); + buffer_len = count * sizeof(s32); + + /* Number of args to fetch */ + wdata.data.length = count; + if (wdata.data.length > (priv_args[k].set_args & IW_PRIV_SIZE_MASK)) + wdata.data.length = priv_args[k].set_args & IW_PRIV_SIZE_MASK; + + break; + + case IW_PRIV_TYPE_CHAR: + if (len > 0) { + /* Size of the string to fetch */ + wdata.data.length = len; + if (wdata.data.length > (priv_args[k].set_args & IW_PRIV_SIZE_MASK)) + wdata.data.length = priv_args[k].set_args & IW_PRIV_SIZE_MASK; + + /* Fetch string */ + _rtw_memcpy(buffer, ptr, wdata.data.length); + } else { + wdata.data.length = 1; + buffer[0] = '\0'; + } + buffer_len = wdata.data.length; + break; + + default: + RTW_INFO("%s: Not yet implemented...\n", __func__); + err = -1; + goto exit; + } + + if ((priv_args[k].set_args & IW_PRIV_SIZE_FIXED) && + (wdata.data.length != (priv_args[k].set_args & IW_PRIV_SIZE_MASK))) { + RTW_INFO("%s: The command %s needs exactly %d argument(s)...\n", + __func__, cmdname, priv_args[k].set_args & IW_PRIV_SIZE_MASK); + err = -EINVAL; + goto exit; + } + } /* if args to set */ + else + wdata.data.length = 0L; + + /* Those two tests are important. They define how the driver + * will have to handle the data */ + if ((priv_args[k].set_args & IW_PRIV_SIZE_FIXED) && + ((get_priv_size(priv_args[k].set_args) + offset) <= IFNAMSIZ)) { + /* First case : all SET args fit within wrq */ + if (offset) + wdata.mode = subcmd; + _rtw_memcpy(wdata.name + offset, buffer, IFNAMSIZ - offset); + } else { + if ((priv_args[k].set_args == 0) && + (priv_args[k].get_args & IW_PRIV_SIZE_FIXED) && + (get_priv_size(priv_args[k].get_args) <= IFNAMSIZ)) { + /* Second case : no SET args, GET args fit within wrq */ + if (offset) + wdata.mode = subcmd; + } else { + /* Third case : args won't fit in wrq, or variable number of args */ + if (copy_to_user(wdata.data.pointer, buffer, buffer_len)) { + err = -EFAULT; + goto exit; + } + wdata.data.flags = subcmd; + } + } + + rtw_mfree(input, input_len); + input = NULL; + + extra_size = 0; + if (IW_IS_SET(priv_args[k].cmd)) { + /* Size of set arguments */ + extra_size = get_priv_size(priv_args[k].set_args); + + /* Does it fits in iwr ? */ + if ((priv_args[k].set_args & IW_PRIV_SIZE_FIXED) && + ((extra_size + offset) <= IFNAMSIZ)) + extra_size = 0; + } else { + /* Size of get arguments */ + extra_size = get_priv_size(priv_args[k].get_args); + + /* Does it fits in iwr ? */ + if ((priv_args[k].get_args & IW_PRIV_SIZE_FIXED) && + (extra_size <= IFNAMSIZ)) + extra_size = 0; + } + + if (extra_size == 0) { + extra = (u8 *)&wdata; + rtw_mfree(buffer, 4096); + buffer = NULL; + } else + extra = buffer; + + handler = priv[priv_args[k].cmd - SIOCIWFIRSTPRIV]; + err = handler(dev, NULL, &wdata, extra); + + /* If we have to get some data */ + if ((priv_args[k].get_args & IW_PRIV_TYPE_MASK) && + (priv_args[k].get_args & IW_PRIV_SIZE_MASK)) { + int j; + int n = 0; /* number of args */ + u8 str[20] = {0}; + + /* Check where is the returned data */ + if ((priv_args[k].get_args & IW_PRIV_SIZE_FIXED) && + (get_priv_size(priv_args[k].get_args) <= IFNAMSIZ)) + n = priv_args[k].get_args & IW_PRIV_SIZE_MASK; + else + n = wdata.data.length; + + output = rtw_zmalloc(4096); + if (NULL == output) { + err = -ENOMEM; + goto exit; + } + + switch (priv_args[k].get_args & IW_PRIV_TYPE_MASK) { + case IW_PRIV_TYPE_BYTE: + /* Display args */ + for (j = 0; j < n; j++) { + sprintf(str, "%d ", extra[j]); + len = strlen(str); + output_len = strlen(output); + if ((output_len + len + 1) > 4096) { + err = -E2BIG; + goto exit; + } + _rtw_memcpy(output + output_len, str, len); + } + break; + + case IW_PRIV_TYPE_INT: + /* Display args */ + for (j = 0; j < n; j++) { + sprintf(str, "%d ", ((__s32 *)extra)[j]); + len = strlen(str); + output_len = strlen(output); + if ((output_len + len + 1) > 4096) { + err = -E2BIG; + goto exit; + } + _rtw_memcpy(output + output_len, str, len); + } + break; + + case IW_PRIV_TYPE_CHAR: + /* Display args */ + _rtw_memcpy(output, extra, n); + break; + + default: + RTW_INFO("%s: Not yet implemented...\n", __func__); + err = -1; + goto exit; + } + + output_len = strlen(output) + 1; + wrq_data->data.length = output_len; + if (copy_to_user(wrq_data->data.pointer, output, output_len)) { + err = -EFAULT; + goto exit; + } + } /* if args to set */ + else + wrq_data->data.length = 0; + +exit: + if (input) + rtw_mfree(input, input_len); + if (buffer) + rtw_mfree(buffer, 4096); + if (output) + rtw_mfree(output, 4096); + + return err; +} + +#ifdef CONFIG_COMPAT +static int rtw_ioctl_compat_wext_private(struct net_device *dev, struct ifreq *rq) +{ + struct compat_iw_point iwp_compat; + union iwreq_data wrq_data; + int err = 0; + RTW_DBG("%s:...\n", __func__); + if (copy_from_user(&iwp_compat, rq->ifr_ifru.ifru_data, sizeof(struct compat_iw_point))) + return -EFAULT; + + wrq_data.data.pointer = compat_ptr(iwp_compat.pointer); + wrq_data.data.length = iwp_compat.length; + wrq_data.data.flags = iwp_compat.flags; + + err = _rtw_ioctl_wext_private(dev, &wrq_data); + + iwp_compat.pointer = ptr_to_compat(wrq_data.data.pointer); + iwp_compat.length = wrq_data.data.length; + iwp_compat.flags = wrq_data.data.flags; + if (copy_to_user(rq->ifr_ifru.ifru_data, &iwp_compat, sizeof(struct compat_iw_point))) + return -EFAULT; + + return err; +} +#endif /* CONFIG_COMPAT */ + +static int rtw_ioctl_standard_wext_private(struct net_device *dev, struct ifreq *rq) +{ + struct iw_point *iwp; + union iwreq_data wrq_data; + int err = 0; + iwp = &wrq_data.data; + RTW_DBG("%s:...\n", __func__); + if (copy_from_user(iwp, rq->ifr_ifru.ifru_data, sizeof(struct iw_point))) + return -EFAULT; + + err = _rtw_ioctl_wext_private(dev, &wrq_data); + + if (copy_to_user(rq->ifr_ifru.ifru_data, iwp, sizeof(struct iw_point))) + return -EFAULT; + + return err; +} + +static int rtw_ioctl_wext_private(struct net_device *dev, struct ifreq *rq) +{ +#ifdef CONFIG_COMPAT +#if (KERNEL_VERSION(4, 6, 0) > LINUX_VERSION_CODE) + if (is_compat_task()) +#else + if (in_compat_syscall()) +#endif + return rtw_ioctl_compat_wext_private(dev, rq); + else +#endif /* CONFIG_COMPAT */ + return rtw_ioctl_standard_wext_private(dev, rq); +} + +int rtw_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) +{ + struct iwreq *wrq = (struct iwreq *)rq; + int ret = 0; + + switch (cmd) { +#ifdef CONFIG_IOCTL_WEXT + case RTL_IOCTL_WPA_SUPPLICANT: + ret = wpa_supplicant_ioctl(dev, &wrq->u.data); + break; +#ifdef CONFIG_AP_MODE + case RTL_IOCTL_HOSTAPD: + ret = rtw_hostapd_ioctl(dev, &wrq->u.data); + break; +#ifdef CONFIG_WIRELESS_EXT + case SIOCSIWMODE: + ret = rtw_wx_set_mode(dev, NULL, &wrq->u, NULL); + break; +#endif +#endif /* CONFIG_AP_MODE */ +#endif /* CONFIG_IOCTL_WEXT */ + case SIOCDEVPRIVATE: + ret = rtw_ioctl_wext_private(dev, rq); + break; + case (SIOCDEVPRIVATE+1): + ret = rtw_android_priv_cmd(dev, rq, cmd); + break; + default: + ret = -EOPNOTSUPP; + break; + } + + return ret; +} diff --git a/os_dep/linux/ioctl_mp.c b/os_dep/linux/ioctl_mp.c new file mode 100644 index 0000000..0c49117 --- /dev/null +++ b/os_dep/linux/ioctl_mp.c @@ -0,0 +1,3530 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#if defined(CONFIG_MP_INCLUDED) + +#include +#include +#include "../../hal/phydm/phydm_precomp.h" +#include + + +#if defined(CONFIG_RTL8723B) + #include +#endif + +#define RTW_IWD_MAX_LEN 128 +inline u8 rtw_do_mp_iwdata_len_chk(const char *caller, u32 len) +{ + u8 is_illegal = _FALSE; + if (len >= RTW_IWD_MAX_LEN) { + RTW_ERR("%s : iw data len(%u) > RTW_IWD_MAX_LEN(%u)", + caller, len, RTW_IWD_MAX_LEN); + is_illegal = _TRUE; + } + return is_illegal; +} + +/* + * Input Format: %s,%d,%d + * %s is width, could be + * "b" for 1 byte + * "w" for WORD (2 bytes) + * "dw" for DWORD (4 bytes) + * 1st %d is address(offset) + * 2st %d is data to write + */ +int rtw_mp_write_reg(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + char *pch, *pnext; + char *width_str; + char width; + u32 addr, data; + int ret; + PADAPTER padapter = rtw_netdev_priv(dev); + char input[RTW_IWD_MAX_LEN]; + + if (rtw_do_mp_iwdata_len_chk(__func__, (wrqu->length + 1))) + return -EFAULT; + + _rtw_memset(input, 0, sizeof(input)); + + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + input[wrqu->length] = '\0'; + + _rtw_memset(extra, 0, wrqu->length); + + pch = input; + + pnext = strpbrk(pch, " ,.-"); + if (pnext == NULL) + return -EINVAL; + *pnext = 0; + width_str = pch; + + pch = pnext + 1; + pnext = strpbrk(pch, " ,.-"); + if (pnext == NULL) + return -EINVAL; + *pnext = 0; + /*addr = simple_strtoul(pch, &ptmp, 16); + _rtw_memset(buf, '\0', sizeof(buf)); + _rtw_memcpy(buf, pch, pnext-pch); + ret = kstrtoul(buf, 16, &addr);*/ + ret = sscanf(pch, "%x", &addr); + + pch = pnext + 1; + pnext = strpbrk(pch, " ,.-"); + if ((pch - input) >= wrqu->length) + return -EINVAL; + /*data = simple_strtoul(pch, &ptmp, 16);*/ + ret = sscanf(pch, "%x", &data); + RTW_INFO("data=%x,addr=%x\n", (u32)data, (u32)addr); + ret = 0; + width = width_str[0]; + switch (width) { + case 'b': + /* 1 byte*/ + if (data > 0xFF) { + ret = -EINVAL; + break; + } + rtw_write8(padapter, addr, data); + break; + case 'w': + /* 2 bytes*/ + if (data > 0xFFFF) { + ret = -EINVAL; + break; + } + rtw_write16(padapter, addr, data); + break; + case 'd': + /* 4 bytes*/ + rtw_write32(padapter, addr, data); + break; + default: + ret = -EINVAL; + break; + } + + return ret; +} + + +/* + * Input Format: %s,%d + * %s is width, could be + * "b" for 1 byte + * "w" for WORD (2 bytes) + * "dw" for DWORD (4 bytes) + * %d is address(offset) + * + * Return: + * %d for data readed + */ +int rtw_mp_read_reg(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + char input[RTW_IWD_MAX_LEN]; + char *pch, *pnext; + char *width_str; + char width; + char data[20], tmp[20]; + u32 addr = 0, strtout = 0; + u32 i = 0, j = 0, ret = 0, data32 = 0; + PADAPTER padapter = rtw_netdev_priv(dev); + char *pextra = extra; + + if (rtw_do_mp_iwdata_len_chk(__func__, (wrqu->length + 1))) + return -EFAULT; + + if (wrqu->length > 128) + return -EFAULT; + + _rtw_memset(input, 0, sizeof(input)); + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + input[wrqu->length] = '\0'; + _rtw_memset(extra, 0, wrqu->length); + _rtw_memset(data, '\0', sizeof(data)); + _rtw_memset(tmp, '\0', sizeof(tmp)); + pch = input; + pnext = strpbrk(pch, " ,.-"); + if (pnext == NULL) + return -EINVAL; + *pnext = 0; + width_str = pch; + + pch = pnext + 1; + + ret = sscanf(pch, "%x", &addr); + if (addr > MP_READ_REG_MAX_OFFSET) + return -EINVAL; + + ret = 0; + width = width_str[0]; + + switch (width) { + case 'b': + data32 = rtw_read8(padapter, addr); + RTW_INFO("%x\n", data32); + sprintf(extra, "%d", data32); + wrqu->length = strlen(extra); + break; + case 'w': + /* 2 bytes*/ + sprintf(data, "%04x\n", rtw_read16(padapter, addr)); + + for (i = 0 ; i <= strlen(data) ; i++) { + if (i % 2 == 0) { + tmp[j] = ' '; + j++; + } + if (data[i] != '\0') + tmp[j] = data[i]; + + j++; + } + pch = tmp; + RTW_INFO("pch=%s", pch); + + while (*pch != '\0') { + pnext = strpbrk(pch, " "); + if (!pnext || ((pnext - tmp) > 4)) + break; + + pnext++; + if (*pnext != '\0') { + /*strtout = simple_strtoul(pnext , &ptmp, 16);*/ + ret = sscanf(pnext, "%x", &strtout); + pextra += sprintf(pextra, " %d", strtout); + } else + break; + pch = pnext; + } + wrqu->length = strlen(extra); + break; + case 'd': + /* 4 bytes */ + sprintf(data, "%08x", rtw_read32(padapter, addr)); + /*add read data format blank*/ + for (i = 0 ; i <= strlen(data) ; i++) { + if (i % 2 == 0) { + tmp[j] = ' '; + j++; + } + if (data[i] != '\0') + tmp[j] = data[i]; + + j++; + } + pch = tmp; + RTW_INFO("pch=%s", pch); + + while (*pch != '\0') { + pnext = strpbrk(pch, " "); + if (!pnext) + break; + + pnext++; + if (*pnext != '\0') { + ret = sscanf(pnext, "%x", &strtout); + pextra += sprintf(pextra, " %d", strtout); + } else + break; + pch = pnext; + } + wrqu->length = strlen(extra); + break; + + default: + wrqu->length = 0; + ret = -EINVAL; + break; + } + + return ret; +} + + +/* + * Input Format: %d,%x,%x + * %d is RF path, should be smaller than MAX_RF_PATH_NUMS + * 1st %x is address(offset) + * 2st %x is data to write + */ +int rtw_mp_write_rf(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + u32 path, addr, data; + int ret; + PADAPTER padapter = rtw_netdev_priv(dev); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(padapter); + char input[RTW_IWD_MAX_LEN]; + + if (rtw_do_mp_iwdata_len_chk(__func__, wrqu->length)) + return -EFAULT; + + _rtw_memset(input, 0, wrqu->length); + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + + ret = sscanf(input, "%d,%x,%x", &path, &addr, &data); + if (ret < 3) + return -EINVAL; + + if (path >= hal_spec->rf_reg_path_num) + return -EINVAL; + if (addr > 0xFF) + return -EINVAL; + if (data > 0xFFFFF) + return -EINVAL; + + _rtw_memset(extra, 0, wrqu->length); + + write_rfreg(padapter, path, addr, data); + + sprintf(extra, "write_rf completed\n"); + wrqu->length = strlen(extra); + + return 0; +} + + +/* + * Input Format: %d,%x + * %d is RF path, should be smaller than MAX_RF_PATH_NUMS + * %x is address(offset) + * + * Return: + * %d for data readed + */ +int rtw_mp_read_rf(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + char input[RTW_IWD_MAX_LEN]; + char *pch, *pnext; + char data[20], tmp[20]; + u32 path, addr, strtou; + u32 ret, i = 0 , j = 0; + PADAPTER padapter = rtw_netdev_priv(dev); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(padapter); + char *pextra = extra; + + if (rtw_do_mp_iwdata_len_chk(__func__, wrqu->length)) + return -EFAULT; + + if (wrqu->length > 128) + return -EFAULT; + _rtw_memset(input, 0, wrqu->length); + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + ret = sscanf(input, "%d,%x", &path, &addr); + if (ret < 2) + return -EINVAL; + + if (path >= hal_spec->rf_reg_path_num) + return -EINVAL; + + if (addr > MP_READ_REG_MAX_OFFSET) + return -EINVAL; + + _rtw_memset(extra, 0, wrqu->length); + + sprintf(data, "%08x", read_rfreg(padapter, path, addr)); + /*add read data format blank*/ + for (i = 0 ; i <= strlen(data) ; i++) { + if (i % 2 == 0) { + tmp[j] = ' '; + j++; + } + tmp[j] = data[i]; + j++; + } + pch = tmp; + RTW_INFO("pch=%s", pch); + + while (*pch != '\0') { + pnext = strpbrk(pch, " "); + if (!pnext) + break; + pnext++; + if (*pnext != '\0') { + /*strtou =simple_strtoul(pnext , &ptmp, 16);*/ + ret = sscanf(pnext, "%x", &strtou); + pextra += sprintf(pextra, " %d", strtou); + } else + break; + pch = pnext; + } + wrqu->length = strlen(extra); + + return 0; +} + + +int rtw_mp_start(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + int ret = 0; + PADAPTER padapter = rtw_netdev_priv(dev); + struct mp_priv *pmppriv = &padapter->mppriv; + + rtw_pm_set_ips(padapter, IPS_NONE); + LeaveAllPowerSaveMode(padapter); + + pmppriv->bprocess_mp_mode = _TRUE; + + if (rtw_mi_check_fwstate(padapter, WIFI_UNDER_SURVEY)) { + rtw_mi_buddy_set_scan_deny(padapter, 5000); + rtw_mi_scan_abort(padapter, _TRUE); + } + + rtw_hal_set_hwreg(padapter, HW_VAR_CHECK_TXBUF, 0); + + if (rtw_mp_cmd(padapter, MP_START, RTW_CMDF_WAIT_ACK) != _SUCCESS) + ret = -EPERM; + + _rtw_memset(extra, 0, wrqu->length); + sprintf(extra, "mp_start %s\n", ret == 0 ? "ok" : "fail"); + wrqu->length = strlen(extra); + + return ret; +} + + + +int rtw_mp_stop(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + int ret = 0; + PADAPTER padapter = rtw_netdev_priv(dev); + struct mp_priv *pmppriv = &padapter->mppriv; + + if (pmppriv->mode != MP_ON) + return -EPERM; + + if (rtw_mp_cmd(padapter, MP_STOP, RTW_CMDF_WAIT_ACK) != _SUCCESS) + ret = -EPERM; + + pmppriv->bprocess_mp_mode = _FALSE; + _rtw_memset(extra, 0, wrqu->length); + sprintf(extra, "mp_stop %s\n", ret == 0 ? "ok" : "fail"); + wrqu->length = strlen(extra); + + return ret; +} + + +int rtw_mp_rate(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + u32 rate = MPT_RATE_1M; + u8 err = 0; + u8 input[RTW_IWD_MAX_LEN]; + PADAPTER padapter = rtw_netdev_priv(dev); + PMPT_CONTEXT pMptCtx = &(padapter->mppriv.mpt_ctx); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(padapter); + struct mp_priv *pmppriv = &padapter->mppriv; + + if (rtw_do_mp_iwdata_len_chk(__func__, (wrqu->length + 1))) + return -EFAULT; + + _rtw_memset(input, 0, sizeof(input)); + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + input[wrqu->length] = '\0'; + rate = rtw_mpRateParseFunc(padapter, input); + pmppriv->rateidx = rate; + + if (rate == 0 && strcmp(input, "1M") != 0) { + rate = rtw_atoi(input); + if (rate <= MGN_VHT4SS_MCS9) + pmppriv->rateidx = MRateToHwRate(rate); + /*if (rate <= 0x7f) + rate = wifirate2_ratetbl_inx((u8)rate); + else if (rate < 0xC8) + rate = (rate - 0x79 + MPT_RATE_MCS0); + HT rate 0x80(MCS0) ~ 0x8F(MCS15) ~ 0x9F(MCS31) 128~159 + VHT1SS~2SS rate 0xA0 (VHT1SS_MCS0 44) ~ 0xB3 (VHT2SS_MCS9 #63) 160~179 + VHT rate 0xB4 (VHT3SS_MCS0 64) ~ 0xC7 (VHT2SS_MCS9 #83) 180~199 + else + VHT rate 0x90(VHT1SS_MCS0) ~ 0x99(VHT1SS_MCS9) 144~153 + rate =(rate - MPT_RATE_VHT1SS_MCS0); + */ + } + + _rtw_memset(extra, 0, wrqu->length); + + if (pmppriv->rateidx > DESC_RATEVHTSS4MCS9) { + sprintf(extra, "Set %s Error" , input); + return -EINVAL; + } + + if (hal_spec->tx_nss_num < 2 && MPT_IS_2SS_RATE(HwRateToMPTRate(pmppriv->rateidx))) + err = 1; + if (hal_spec->tx_nss_num < 3 && MPT_IS_3SS_RATE(HwRateToMPTRate(pmppriv->rateidx))) + err = 1; + if (hal_spec->tx_nss_num < 4 && MPT_IS_4SS_RATE(HwRateToMPTRate(pmppriv->rateidx))) + err = 1; + if (!is_supported_vht(padapter->registrypriv.wireless_mode) && MPT_IS_VHT_RATE(HwRateToMPTRate(pmppriv->rateidx))) + err = 1; + if (!is_supported_ht(padapter->registrypriv.wireless_mode) && MPT_IS_HT_RATE(HwRateToMPTRate(pmppriv->rateidx))) + err = 1; + + if (err == 1) { + sprintf(extra, "Set data rate to %s Error" , input); + pmppriv->rateidx = 0; + } else { + sprintf(extra, "Set data rate to %s index %d" , input, pmppriv->rateidx); + RTW_INFO("%s: %s rate index=%d\n", __func__, input, pmppriv->rateidx); + pMptCtx->mpt_rate_index = HwRateToMPTRate(pmppriv->rateidx); + SetDataRate(padapter); + } + wrqu->length = strlen(extra); + return err; +} + +int rtw_mp_channel(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + + PADAPTER padapter = rtw_netdev_priv(dev); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + u8 input[RTW_IWD_MAX_LEN]; + u8 channel = 1; + struct mp_priv *pmppriv = &padapter->mppriv; + + if (rtw_do_mp_iwdata_len_chk(__func__, (wrqu->length + 1))) + return -EFAULT; + + _rtw_memset(input, 0, sizeof(input)); + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + input[wrqu->length] = '\0'; + channel = rtw_atoi(input); + /*RTW_INFO("%s: channel=%d\n", __func__, channel);*/ + _rtw_memset(extra, 0, wrqu->length); + sprintf(extra, "Change channel %d to channel %d", padapter->mppriv.channel , channel); + padapter->mppriv.channel = channel; + rtw_hal_set_hwreg(padapter, HW_VAR_CHECK_TXBUF, 0); + rtw_adjust_chbw(padapter, channel, &pmppriv->bandwidth, &pmppriv->prime_channel_offset); + SetChannel(padapter); + pHalData->current_channel = channel; + + wrqu->length = strlen(extra); + return 0; +} + + +int rtw_mp_ch_offset(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + + PADAPTER padapter = rtw_netdev_priv(dev); + u8 input[RTW_IWD_MAX_LEN]; + u32 ch_offset = 0; + char *pch; + + if (rtw_do_mp_iwdata_len_chk(__func__, (wrqu->length + 1))) + return -EFAULT; + + _rtw_memset(input, 0, sizeof(input)); + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + input[wrqu->length] = '\0'; + ch_offset = rtw_atoi(input); + /*RTW_INFO("%s: channel=%d\n", __func__, channel);*/ + _rtw_memset(extra, 0, wrqu->length); + pch = extra; + pch += sprintf(pch, "Change prime channel offset %d to %d", padapter->mppriv.prime_channel_offset , ch_offset); + padapter->mppriv.prime_channel_offset = ch_offset; + SetChannel(padapter); + + wrqu->length = strlen(extra); + return 0; +} + + +int rtw_mp_bandwidth(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + u8 bandwidth = 0, sg = 0; + PADAPTER padapter = rtw_netdev_priv(dev); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + struct mp_priv *pmppriv = &padapter->mppriv; + u8 input[RTW_IWD_MAX_LEN]; + + if (rtw_do_mp_iwdata_len_chk(__func__, wrqu->length)) + return -EFAULT; + + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + if (sscanf(input, "40M=%hhd,shortGI=%hhd", &bandwidth, &sg) > 0) + RTW_INFO("%s: bw=%hhd sg=%hhd\n", __func__, bandwidth , sg); + + rtw_adjust_chbw(padapter, pmppriv->channel, &bandwidth, &pmppriv->prime_channel_offset); + + padapter->mppriv.bandwidth = (u8)bandwidth; + padapter->mppriv.preamble = sg; + _rtw_memset(extra, 0, wrqu->length); + sprintf(extra, "Change BW %d to BW %d\n", pHalData->current_channel_bw , bandwidth); + rtw_hal_set_hwreg(padapter, HW_VAR_CHECK_TXBUF, 0); + SetBandwidth(padapter); + pHalData->current_channel_bw = bandwidth; + + wrqu->length = strlen(extra); + + return 0; +} + + +int rtw_mp_txpower_index(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + HAL_DATA_TYPE *phal_data = GET_HAL_DATA(padapter); + char input[RTW_IWD_MAX_LEN]; + u32 rfpath = 0 ; + u32 txpower_inx = 0, tarpowerdbm = 0; + char *pextra = extra; + + if (rtw_do_mp_iwdata_len_chk(__func__, (wrqu->length + 1))) + return -EFAULT; + + if (wrqu->length > 128) + return -EFAULT; + + _rtw_memset(input, 0, sizeof(input)); + + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + input[wrqu->length] = '\0'; + _rtw_memset(extra, 0, strlen(extra)); + + if (wrqu->length == 2) { + if (input[0] != '\0' ) { + rfpath = rtw_atoi(input); + txpower_inx = mpt_ProQueryCalTxPower(padapter, rfpath); + } + pextra += sprintf(pextra, " %d\n", txpower_inx); + tarpowerdbm = mpt_get_tx_power_finalabs_val(padapter, rfpath); + if (tarpowerdbm > 0) + pextra += sprintf(pextra, "\t\t dBm:%d", tarpowerdbm); + } else { + if (phal_data->ant_path == 1) + rfpath = 1; + else + rfpath = 0; + + txpower_inx = mpt_ProQueryCalTxPower(padapter, rfpath); + pextra += sprintf(pextra, "patha=%d", txpower_inx); + if (phal_data->rf_type > RF_1T2R) { + txpower_inx = mpt_ProQueryCalTxPower(padapter, 1); + pextra += sprintf(pextra, ",pathb=%d", txpower_inx); + } + if (phal_data->rf_type > RF_2T4R) { + txpower_inx = mpt_ProQueryCalTxPower(padapter, 2); + pextra += sprintf(pextra, ",pathc=%d", txpower_inx); + } + if (phal_data->rf_type > RF_3T4R) { + txpower_inx = mpt_ProQueryCalTxPower(padapter, 3); + pextra += sprintf(pextra, ",pathd=%d", txpower_inx); + } + + tarpowerdbm = mpt_get_tx_power_finalabs_val(padapter, rfpath); + pextra += sprintf(pextra, "\n\t\t\tpatha dBm=%d", tarpowerdbm); + if (phal_data->rf_type > RF_1T2R) { + tarpowerdbm = mpt_get_tx_power_finalabs_val(padapter, 1); + pextra += sprintf(pextra, ",pathb dBm=%d", tarpowerdbm); + } + if (phal_data->rf_type > RF_2T4R) { + tarpowerdbm = mpt_get_tx_power_finalabs_val(padapter, 2); + pextra += sprintf(pextra, ",pathc dBm=%d", tarpowerdbm); + } + if (phal_data->rf_type > RF_3T4R) { + tarpowerdbm = mpt_get_tx_power_finalabs_val(padapter, 3); + pextra += sprintf(pextra, ",pathd dBm=%d", tarpowerdbm); + } + } + wrqu->length = strlen(extra); + + return 0; +} + + +int rtw_mp_txpower(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + u32 idx_a = 0, idx_b = 0, idx_c = 0, idx_d = 0; + int MsetPower = 1; + u8 input[RTW_IWD_MAX_LEN]; + u8 res = 0; + + PADAPTER padapter = rtw_netdev_priv(dev); + PMPT_CONTEXT pMptCtx = &(padapter->mppriv.mpt_ctx); + + if (rtw_do_mp_iwdata_len_chk(__func__, wrqu->length)) + return -EFAULT; + + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + MsetPower = strncmp(input, "off", 3); + if (MsetPower == 0) { + padapter->mppriv.bSetTxPower = 0; + sprintf(extra, "MP Set power off"); + } else { + res = sscanf(input, "patha=%d,pathb=%d,pathc=%d,pathd=%d", &idx_a, &idx_b, &idx_c, &idx_d); + if (res < 1) { + if(isdigit(input[0])){ + idx_a = rtw_atoi(input); + RTW_INFO("direct set RF Path A Power =%d\n", idx_a); + } else + RTW_INFO("Invalid format on %s !, Get patha=%d,pathb=%d,pathc=%d,pathd=%d\n", input , idx_a , idx_b , idx_c , idx_d); + } + if (res > 0 || idx_a !=0) + sprintf(extra, "Set power level path_A:%d path_B:%d path_C:%d path_D:%d", idx_a , idx_b , idx_c , idx_d); + else + sprintf(extra, "Invalid format on string :%s ", input); + + padapter->mppriv.txpoweridx = (u8)idx_a; + + pMptCtx->TxPwrLevel[RF_PATH_A] = (u8)idx_a; + pMptCtx->TxPwrLevel[RF_PATH_B] = (u8)idx_b; + pMptCtx->TxPwrLevel[RF_PATH_C] = (u8)idx_c; + pMptCtx->TxPwrLevel[RF_PATH_D] = (u8)idx_d; + padapter->mppriv.bSetTxPower = 1; + + SetTxPower(padapter); + } + + wrqu->length = strlen(extra); + return 0; +} + + +int rtw_mp_ant_tx(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + u8 i; + u8 input[RTW_IWD_MAX_LEN]; + u16 antenna = 0; + PADAPTER padapter = rtw_netdev_priv(dev); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + + if (rtw_do_mp_iwdata_len_chk(__func__, (wrqu->length + 1))) + return -EFAULT; + + _rtw_memset(input, 0, sizeof(input)); + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + input[wrqu->length] = '\0'; + sprintf(extra, "switch Tx antenna to %s", input); + + for (i = 0; i < strlen(input); i++) { + switch (input[i]) { + case 'a': + antenna |= ANTENNA_A; + break; + case 'b': + antenna |= ANTENNA_B; + break; + case 'c': + antenna |= ANTENNA_C; + break; + case 'd': + antenna |= ANTENNA_D; + break; + } + } + /*antenna |= BIT(extra[i]-'a');*/ + RTW_INFO("%s: antenna=0x%x\n", __func__, antenna); + padapter->mppriv.antenna_tx = antenna; + + /*RTW_INFO("%s:mppriv.antenna_rx=%d\n", __func__, padapter->mppriv.antenna_tx);*/ + pHalData->antenna_tx_path = antenna; + if (IS_HARDWARE_TYPE_8822C(padapter) && padapter->mppriv.antenna_tx == ANTENNA_B) { + if (padapter->mppriv.antenna_rx == ANTENNA_A || padapter->mppriv.antenna_rx == ANTENNA_B) { + padapter->mppriv.antenna_rx = ANTENNA_AB; + pHalData->AntennaRxPath = ANTENNA_AB; + RTW_INFO("%s:8822C Tx-B Rx Ant to AB\n", __func__); + } + } + SetAntenna(padapter); + + wrqu->length = strlen(extra); + return 0; +} + + +int rtw_mp_ant_rx(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + u8 i; + u16 antenna = 0; + u8 input[RTW_IWD_MAX_LEN]; + PADAPTER padapter = rtw_netdev_priv(dev); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + + if (rtw_do_mp_iwdata_len_chk(__func__, (wrqu->length + 1))) + return -EFAULT; + + _rtw_memset(input, 0, sizeof(input)); + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + input[wrqu->length] = '\0'; + /*RTW_INFO("%s: input=%s\n", __func__, input);*/ + _rtw_memset(extra, 0, wrqu->length); + + sprintf(extra, "switch Rx antenna to %s", input); + + for (i = 0; i < strlen(input); i++) { + switch (input[i]) { + case 'a': + antenna |= ANTENNA_A; + break; + case 'b': + antenna |= ANTENNA_B; + break; + case 'c': + antenna |= ANTENNA_C; + break; + case 'd': + antenna |= ANTENNA_D; + break; + } + } + + RTW_INFO("%s: antenna=0x%x\n", __func__, antenna); + + padapter->mppriv.antenna_rx = antenna; + pHalData->AntennaRxPath = antenna; + /*RTW_INFO("%s:mppriv.antenna_rx=%d\n", __func__, padapter->mppriv.antenna_rx);*/ + SetAntenna(padapter); + wrqu->length = strlen(extra); + + return 0; +} + + +int rtw_set_ctx_destAddr(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + int jj, kk = 0; + + struct pkt_attrib *pattrib; + struct mp_priv *pmp_priv; + PADAPTER padapter = rtw_netdev_priv(dev); + + pmp_priv = &padapter->mppriv; + pattrib = &pmp_priv->tx.attrib; + + if (strlen(extra) < 5) + return _FAIL; + + RTW_INFO("%s: in=%s\n", __func__, extra); + for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 3) + pattrib->dst[jj] = key_2char2num(extra[kk], extra[kk + 1]); + + RTW_INFO("pattrib->dst:%x %x %x %x %x %x\n", pattrib->dst[0], pattrib->dst[1], pattrib->dst[2], pattrib->dst[3], pattrib->dst[4], pattrib->dst[5]); + return 0; +} + + + +int rtw_mp_ctx(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + u32 pkTx = 1; + int countPkTx = 1, cotuTx = 1, CarrSprTx = 1, scTx = 1, sgleTx = 1, stop = 1, payload = 1; + u32 bStartTest = 1; + u32 count = 0, pktinterval = 0, pktlen = 0; + u8 status; + struct mp_priv *pmp_priv; + struct pkt_attrib *pattrib; + PADAPTER padapter = rtw_netdev_priv(dev); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + + pmp_priv = &padapter->mppriv; + pattrib = &pmp_priv->tx.attrib; + + if (padapter->registrypriv.mp_mode != 1 ) { + sprintf(extra, "Error: can't tx ,not in MP mode. \n"); + wrqu->length = strlen(extra); + return 0; + } + + if (copy_from_user(extra, wrqu->pointer, wrqu->length)) + return -EFAULT; + + *(extra + wrqu->length) = '\0'; + RTW_INFO("%s: in=%s\n", __func__, extra); +#ifdef CONFIG_CONCURRENT_MODE + if (!is_primary_adapter(padapter)) { + sprintf(extra, "Error: MP mode can't support Virtual Adapter, Please to use main Adapter.\n"); + wrqu->length = strlen(extra); + return 0; + } +#endif + countPkTx = strncmp(extra, "count=", 5); /* strncmp TRUE is 0*/ + cotuTx = strncmp(extra, "background", 20); + CarrSprTx = strncmp(extra, "background,cs", 20); + scTx = strncmp(extra, "background,sc", 20); + sgleTx = strncmp(extra, "background,stone", 20); + pkTx = strncmp(extra, "background,pkt", 20); + stop = strncmp(extra, "stop", 4); + payload = strncmp(extra, "payload=", 8); + + if (sscanf(extra, "count=%d,pkt", &count) > 0) + RTW_INFO("count= %d\n", count); + if (sscanf(extra, "pktinterval=%d", &pktinterval) > 0) + RTW_INFO("pktinterval= %d\n", pktinterval); + if (sscanf(extra, "pktlen=%d", &pktlen) > 0) + RTW_INFO("pktlen= %d\n", pktlen); + + if (payload == 0) { + payload = MP_TX_Payload_default_random; + if (strncmp(extra, "payload=prbs9", 14) == 0) { + payload = MP_TX_Payload_prbs9; + sprintf(extra, "config payload PRBS9\n"); + } else { + if (sscanf(extra, "payload=%x", &payload) > 0){ + RTW_INFO("payload= %x\n", payload); + sprintf(extra, "config payload setting = %x\n" + "1. input payload=[]:\n " + "[0]: 00, [1]: A5, [2]: 5A, [3]: FF, [4]: PRBS-9, [5]: Random\n" + "2. specified a hex payload: payload=0xee\n", payload); + } + } + pmp_priv->tx.payload = payload; + wrqu->length = strlen(extra); + return 0; + } + + if (_rtw_memcmp(extra, "destmac=", 8)) { + wrqu->length -= 8; + rtw_set_ctx_destAddr(dev, info, wrqu, &extra[8]); + sprintf(extra, "Set dest mac OK !\n"); + return 0; + } + /*RTW_INFO("%s: count=%d countPkTx=%d cotuTx=%d CarrSprTx=%d scTx=%d sgleTx=%d pkTx=%d stop=%d\n", __func__, count, countPkTx, cotuTx, CarrSprTx, pkTx, sgleTx, scTx, stop);*/ + _rtw_memset(extra, '\0', strlen(extra)); + + if (pktinterval != 0) { + sprintf(extra, "Pkt Interval = %d", pktinterval); + padapter->mppriv.pktInterval = pktinterval; + wrqu->length = strlen(extra); + return 0; + + } else if (pktlen != 0) { + sprintf(extra, "Pkt len = %d", pktlen); + pattrib->pktlen = pktlen; + wrqu->length = strlen(extra); + return 0; + + } else if (stop == 0) { + struct xmit_priv *pxmitpriv = &(padapter->xmitpriv); + _queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue; + _queue *pfree_xmit_queue = &pxmitpriv->free_xmit_queue; + + u32 i = 0; + bStartTest = 0; /* To set Stop*/ + pmp_priv->tx.stop = 1; + sprintf(extra, "Stop continuous Tx"); + odm_write_dig(&pHalData->odmpriv, 0x20); + do { + if (pxmitpriv->free_xmitframe_cnt == NR_XMITFRAME && pxmitpriv->free_xmitbuf_cnt == NR_XMITBUFF) + break; + else { + i++; + RTW_INFO("%s:wait queue_empty %d!!\n", __func__, i); + rtw_msleep_os(10); + } + } while (i < 1000); + } else { + bStartTest = 1; + odm_write_dig(&pHalData->odmpriv, 0x3f); + if (IS_HARDWARE_TYPE_8822C(padapter) && pmp_priv->antenna_tx == ANTENNA_B) { + if (pmp_priv->antenna_rx == ANTENNA_A || pmp_priv->antenna_rx == ANTENNA_B) { + pmp_priv->antenna_rx = ANTENNA_AB; + pHalData->AntennaRxPath = ANTENNA_AB; + RTW_INFO("%s:8822C Tx-B Rx Ant to AB\n", __func__); + SetAntenna(padapter); + } + } + if (pmp_priv->mode != MP_ON) { + if (pmp_priv->tx.stop != 1) { + RTW_INFO("%s:Error MP_MODE %d != ON\n", __func__, pmp_priv->mode); + return -EFAULT; + } + } + } + + pmp_priv->tx.count = count; + + if (pkTx == 0 || countPkTx == 0) + pmp_priv->mode = MP_PACKET_TX; + if (sgleTx == 0) + pmp_priv->mode = MP_SINGLE_TONE_TX; + if (cotuTx == 0) + pmp_priv->mode = MP_CONTINUOUS_TX; + if (CarrSprTx == 0) + pmp_priv->mode = MP_CARRIER_SUPPRISSION_TX; + if (scTx == 0) + pmp_priv->mode = MP_SINGLE_CARRIER_TX; + + status = rtw_mp_pretx_proc(padapter, bStartTest, extra); + + if (stop == 0) + pmp_priv->mode = MP_ON; + + wrqu->length = strlen(extra); + return status; +} + + + +int rtw_mp_disable_bt_coexist(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ +#ifdef CONFIG_BT_COEXIST + PADAPTER padapter = (PADAPTER)rtw_netdev_priv(dev); + +#endif + u8 input[RTW_IWD_MAX_LEN]; + u32 bt_coexist; + + if (rtw_do_mp_iwdata_len_chk(__func__, (wrqu->data.length + 1))) + return -EFAULT; + + _rtw_memset(input, 0, sizeof(input)); + + if (copy_from_user(input, wrqu->data.pointer, wrqu->data.length)) + return -EFAULT; + + input[wrqu->data.length] = '\0'; + + bt_coexist = rtw_atoi(input); + + if (bt_coexist == 0) { + RTW_INFO("Set OID_RT_SET_DISABLE_BT_COEXIST: disable BT_COEXIST\n"); +#ifdef CONFIG_BT_COEXIST + rtw_btcoex_HaltNotify(padapter); + rtw_btcoex_SetManualControl(padapter, _TRUE); + /* Force to switch Antenna to WiFi*/ + rtw_write16(padapter, 0x870, 0x300); + rtw_write16(padapter, 0x860, 0x110); +#endif + /* CONFIG_BT_COEXIST */ + } else { +#ifdef CONFIG_BT_COEXIST + rtw_btcoex_SetManualControl(padapter, _FALSE); +#endif + } + + return 0; +} + + +int rtw_mp_arx(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + int bStartRx = 0, bStopRx = 0, bQueryPhy = 0, bQueryMac = 0, bSetBssid = 0, bSetRxframe = 0; + int bmac_filter = 0, bmon = 0, bSmpCfg = 0; + u8 input[RTW_IWD_MAX_LEN]; + char *pch, *token, *tmp[2] = {0x00, 0x00}; + u32 i = 0, jj = 0, kk = 0, cnts = 0, ret; + PADAPTER padapter = rtw_netdev_priv(dev); + struct mp_priv *pmppriv = &padapter->mppriv; + struct dbg_rx_counter rx_counter; + + if (rtw_do_mp_iwdata_len_chk(__func__, wrqu->length)) + return -EFAULT; + + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + RTW_INFO("%s: %s\n", __func__, input); +#ifdef CONFIG_CONCURRENT_MODE + if (!is_primary_adapter(padapter)) { + sprintf(extra, "Error: MP mode can't support Virtual Adapter, Please to use main Adapter.\n"); + wrqu->length = strlen(extra); + return 0; + } +#endif + bStartRx = (strncmp(input, "start", 5) == 0) ? 1 : 0; /* strncmp TRUE is 0*/ + bStopRx = (strncmp(input, "stop", 5) == 0) ? 1 : 0; /* strncmp TRUE is 0*/ + bQueryPhy = (strncmp(input, "phy", 3) == 0) ? 1 : 0; /* strncmp TRUE is 0*/ + bQueryMac = (strncmp(input, "mac", 3) == 0) ? 1 : 0; /* strncmp TRUE is 0*/ + bSetBssid = (strncmp(input, "setbssid=", 8) == 0) ? 1 : 0; /* strncmp TRUE is 0*/ + bSetRxframe = (strncmp(input, "frametype", 9) == 0) ? 1 : 0; + /*bfilter_init = (strncmp(input, "filter_init",11)==0)?1:0;*/ + bmac_filter = (strncmp(input, "accept_mac", 10) == 0) ? 1 : 0; + bmon = (strncmp(input, "mon=", 4) == 0) ? 1 : 0; + bSmpCfg = (strncmp(input , "smpcfg=" , 7) == 0) ? 1 : 0; + pmppriv->bloopback = (strncmp(input, "loopbk", 6) == 0) ? 1 : 0; /* strncmp TRUE is 0*/ + + if (bSetBssid == 1) { + pch = input; + while ((token = strsep(&pch, "=")) != NULL) { + if (i > 1) + break; + tmp[i] = token; + i++; + } + if ((tmp[0] != NULL) && (tmp[1] != NULL)) { + cnts = strlen(tmp[1]) / 2; + if (cnts < 1) + return -EFAULT; + RTW_INFO("%s: cnts=%d\n", __func__, cnts); + RTW_INFO("%s: data=%s\n", __func__, tmp[1]); + for (jj = 0, kk = 0; jj < cnts ; jj++, kk += 2) { + pmppriv->network_macaddr[jj] = key_2char2num(tmp[1][kk], tmp[1][kk + 1]); + RTW_INFO("network_macaddr[%d]=%x\n", jj, pmppriv->network_macaddr[jj]); + } + } else + return -EFAULT; + + pmppriv->bSetRxBssid = _TRUE; + } + if (bSetRxframe) { + if (strncmp(input, "frametype beacon", 16) == 0) + pmppriv->brx_filter_beacon = _TRUE; + else + pmppriv->brx_filter_beacon = _FALSE; + } + + if (bmac_filter) { + pmppriv->bmac_filter = bmac_filter; + pch = input; + while ((token = strsep(&pch, "=")) != NULL) { + if (i > 1) + break; + tmp[i] = token; + i++; + } + if ((tmp[0] != NULL) && (tmp[1] != NULL)) { + cnts = strlen(tmp[1]) / 2; + if (cnts < 1) + return -EFAULT; + RTW_INFO("%s: cnts=%d\n", __func__, cnts); + RTW_INFO("%s: data=%s\n", __func__, tmp[1]); + for (jj = 0, kk = 0; jj < cnts ; jj++, kk += 2) { + pmppriv->mac_filter[jj] = key_2char2num(tmp[1][kk], tmp[1][kk + 1]); + RTW_INFO("%s mac_filter[%d]=%x\n", __func__, jj, pmppriv->mac_filter[jj]); + } + } else + return -EFAULT; + + } + + if (bStartRx) { + sprintf(extra, "start"); + SetPacketRx(padapter, bStartRx, _FALSE); + } else if (bStopRx) { + SetPacketRx(padapter, bStartRx, _FALSE); + pmppriv->bmac_filter = _FALSE; + pmppriv->bSetRxBssid = _FALSE; + sprintf(extra, "Received packet OK:%d CRC error:%d ,Filter out:%d", padapter->mppriv.rx_pktcount, padapter->mppriv.rx_crcerrpktcount, padapter->mppriv.rx_pktcount_filter_out); + } else if (bQueryPhy) { + _rtw_memset(&rx_counter, 0, sizeof(struct dbg_rx_counter)); + rtw_dump_phy_rx_counters(padapter, &rx_counter); + + RTW_INFO("%s: OFDM_FA =%d\n", __func__, rx_counter.rx_ofdm_fa); + RTW_INFO("%s: CCK_FA =%d\n", __func__, rx_counter.rx_cck_fa); + sprintf(extra, "Phy Received packet OK:%d CRC error:%d FA Counter: %d", rx_counter.rx_pkt_ok, rx_counter.rx_pkt_crc_error, rx_counter.rx_cck_fa + rx_counter.rx_ofdm_fa); + + + } else if (bQueryMac) { + _rtw_memset(&rx_counter, 0, sizeof(struct dbg_rx_counter)); + rtw_dump_mac_rx_counters(padapter, &rx_counter); + sprintf(extra, "Mac Received packet OK: %d , CRC error: %d , Drop Packets: %d\n", + rx_counter.rx_pkt_ok, rx_counter.rx_pkt_crc_error, rx_counter.rx_pkt_drop); + + } + + if (bmon == 1) { + ret = sscanf(input, "mon=%d", &bmon); + + if (bmon == 1) { + pmppriv->rx_bindicatePkt = _TRUE; + sprintf(extra, "Indicating Receive Packet to network start\n"); + } else { + pmppriv->rx_bindicatePkt = _FALSE; + sprintf(extra, "Indicating Receive Packet to network Stop\n"); + } + } + if (bSmpCfg == 1) { + ret = sscanf(input, "smpcfg=%d", &bSmpCfg); + + if (bSmpCfg == 1) { + pmppriv->bRTWSmbCfg = _TRUE; + sprintf(extra , "Indicate By Simple Config Format\n"); + SetPacketRx(padapter, _TRUE, _TRUE); + } else { + pmppriv->bRTWSmbCfg = _FALSE; + sprintf(extra , "Indicate By Normal Format\n"); + SetPacketRx(padapter, _TRUE, _FALSE); + } + } + + if (pmppriv->bloopback == _TRUE) { + sprintf(extra , "Enter MAC LoopBack mode\n"); +#if defined(CONFIG_RTL8814B) + /* 1. No adhoc, 2. Enable short cut */ + rtw_write32(padapter, 0x100, 0x0B000EFF); +#else + rtw_write32(padapter, 0x100, 0x0B0106FF); +#endif + RTW_INFO("0x100 :0x%x", rtw_read32(padapter, 0x100)); + rtw_write16(padapter, 0x608, 0x30c); + RTW_INFO("0x608 :0x%x", rtw_read32(padapter, 0x608)); + } + + wrqu->length = strlen(extra) + 1; + + return 0; +} + + +int rtw_mp_trx_query(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + u32 txok, txfail, rxok, rxfail, rxfilterout; + PADAPTER padapter = rtw_netdev_priv(dev); + PMPT_CONTEXT pMptCtx = &(padapter->mppriv.mpt_ctx); + RT_PMAC_TX_INFO PMacTxInfo = pMptCtx->PMacTxInfo; + + if (PMacTxInfo.bEnPMacTx == TRUE) + txok = hal_mpt_query_phytxok(padapter); + else + txok = padapter->mppriv.tx.sended; + + txfail = 0; + rxok = padapter->mppriv.rx_pktcount; + rxfail = padapter->mppriv.rx_crcerrpktcount; + rxfilterout = padapter->mppriv.rx_pktcount_filter_out; + + _rtw_memset(extra, '\0', 128); + + sprintf(extra, "Tx OK:%d, Tx Fail:%d, Rx OK:%d, CRC error:%d ,Rx Filter out:%d\n", txok, txfail, rxok, rxfail, rxfilterout); + + wrqu->length = strlen(extra) + 1; + + return 0; +} + + +int rtw_mp_pwrtrk(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + u8 enable; + u32 thermal; + s32 ret; + PADAPTER padapter = rtw_netdev_priv(dev); + u8 input[RTW_IWD_MAX_LEN]; + + if (rtw_do_mp_iwdata_len_chk(__func__, wrqu->length)) + return -EFAULT; + + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + _rtw_memset(extra, 0, wrqu->length); + + enable = 1; + if (wrqu->length > 1) { + /* not empty string*/ + if (strncmp(input, "stop", 4) == 0) { + enable = 0; + sprintf(extra, "mp tx power tracking stop"); + } else if (sscanf(input, "ther=%d", &thermal) == 1) { + ret = SetThermalMeter(padapter, (u8)thermal); + if (ret == _FAIL) + return -EPERM; + sprintf(extra, "mp tx power tracking start,target value=%d ok", thermal); + } else + return -EINVAL; + } + + ret = SetPowerTracking(padapter, enable); + if (ret == _FAIL) + return -EPERM; + + wrqu->length = strlen(extra); + + return 0; +} + + + +int rtw_mp_psd(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + u8 input[RTW_IWD_MAX_LEN]; + + if (rtw_do_mp_iwdata_len_chk(__func__, (wrqu->length + 1))) + return -EFAULT; + + _rtw_memset(input, 0, sizeof(input)); + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + input[wrqu->length] = '\0'; + strcpy(extra, input); + + wrqu->length = mp_query_psd(padapter, extra); + + return 0; +} + + +int rtw_mp_thermal(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + u8 val[4] = {0}; + u8 ret = 0; + u16 ther_path_addr[4] = {0}; + u16 cnt = 1; + PADAPTER padapter = rtw_netdev_priv(dev); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(padapter); + int rfpath = RF_PATH_A; + +#ifdef CONFIG_RTL8188E + ther_path_addr[0] = EEPROM_THERMAL_METER_88E; +#endif +#if defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A) || defined(CONFIG_RTL8814A) + ther_path_addr[0] = EEPROM_THERMAL_METER_8812; +#endif +#ifdef CONFIG_RTL8192E + ther_path_addr[0] = EEPROM_THERMAL_METER_8192E; +#endif +#ifdef CONFIG_RTL8192F + ther_path_addr[0] = EEPROM_THERMAL_METER_8192F; +#endif +#ifdef CONFIG_RTL8723B + ther_path_addr[0] = EEPROM_THERMAL_METER_8723B; +#endif +#ifdef CONFIG_RTL8703B + ther_path_addr[0] = EEPROM_THERMAL_METER_8703B; +#endif +#ifdef CONFIG_RTL8723D + ther_path_addr[0] = EEPROM_THERMAL_METER_8723D; +#endif +#ifdef CONFIG_RTL8188F + ther_path_addr[0] = EEPROM_THERMAL_METER_8188F; +#endif +#ifdef CONFIG_RTL8188GTV + ther_path_addr[0] = EEPROM_THERMAL_METER_8188GTV; +#endif +#ifdef CONFIG_RTL8822B + ther_path_addr[0] = EEPROM_THERMAL_METER_8822B; +#endif +#ifdef CONFIG_RTL8821C + ther_path_addr[0] = EEPROM_THERMAL_METER_8821C; +#endif +#ifdef CONFIG_RTL8710B + ther_path_addr[0] = EEPROM_THERMAL_METER_8710B; +#endif +#ifdef CONFIG_RTL8822C + ther_path_addr[0] = EEPROM_THERMAL_METER_A_8822C; + ther_path_addr[1] = EEPROM_THERMAL_METER_B_8822C; +#endif +#ifdef CONFIG_RTL8814B + ther_path_addr[0] = EEPROM_THERMAL_METER_A_8814B; + ther_path_addr[1] = EEPROM_THERMAL_METER_B_8814B; + ther_path_addr[2] = EEPROM_THERMAL_METER_C_8814B; + ther_path_addr[3] = EEPROM_THERMAL_METER_D_8814B; +#endif +#ifdef CONFIG_RTL8723F + ther_path_addr[0] = EEPROM_THERMAL_METER_8723F; +#endif + + if (copy_from_user(extra, wrqu->pointer, wrqu->length)) + return -EFAULT; + + if ((strncmp(extra, "write", 6) == 0)) { + int i; + u16 raw_cursize = 0, raw_maxsize = 0; +#ifdef RTW_HALMAC + raw_maxsize = efuse_GetavailableSize(padapter); +#else + efuse_GetCurrentSize(padapter, &raw_cursize); + raw_maxsize = efuse_GetMaxSize(padapter); +#endif + RTW_INFO("[eFuse available raw size]= %d bytes\n", raw_maxsize - raw_cursize); + if (2 > raw_maxsize - raw_cursize) { + RTW_INFO("no available efuse!\n"); + return -EFAULT; + } + + for (i = 0; i < hal_spec->rf_reg_path_num; i++) { + GetThermalMeter(padapter, i , &val[i]); + if (ther_path_addr[i] != 0 && val[i] != 0) { + if (rtw_efuse_map_write(padapter, ther_path_addr[i], cnt, &val[i]) == _FAIL) { + RTW_INFO("Error efuse write thermal addr 0x%x ,val = 0x%x\n", ther_path_addr[i], val[i]); + return -EFAULT; + } + } else { + RTW_INFO("Error efuse write thermal Null addr,val \n"); + return -EFAULT; + } + } + _rtw_memset(extra, 0, wrqu->length); + sprintf(extra, " efuse write ok :%d", val[0]); + } else { + ret = sscanf(extra, "%d", &rfpath); + if (ret < 1) { + rfpath = RF_PATH_A; + RTW_INFO("default thermal of path(%d)\n", rfpath); + } + if (rfpath >= hal_spec->rf_reg_path_num) + return -EINVAL; + + RTW_INFO("read thermal of path(%d)\n", rfpath); + GetThermalMeter(padapter, rfpath, &val[0]); + + _rtw_memset(extra, 0, wrqu->length); + sprintf(extra, "%d", val[0]); + } + wrqu->length = strlen(extra); + + return 0; +} + + + +int rtw_mp_reset_stats(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + struct mp_priv *pmp_priv; + PADAPTER padapter = rtw_netdev_priv(dev); + + pmp_priv = &padapter->mppriv; + + pmp_priv->tx.sended = 0; + pmp_priv->tx_pktcount = 0; + pmp_priv->rx_pktcount = 0; + pmp_priv->rx_pktcount_filter_out = 0; + pmp_priv->rx_crcerrpktcount = 0; + + rtw_reset_phy_rx_counters(padapter); + rtw_reset_mac_rx_counters(padapter); + + _rtw_memset(extra, 0, wrqu->length); + sprintf(extra, "mp_reset_stats ok\n"); + wrqu->length = strlen(extra); + + return 0; +} + + +int rtw_mp_dump(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + struct mp_priv *pmp_priv; + u8 input[RTW_IWD_MAX_LEN]; + PADAPTER padapter = rtw_netdev_priv(dev); + + pmp_priv = &padapter->mppriv; + + if (rtw_do_mp_iwdata_len_chk(__func__, wrqu->length)) + return -EFAULT; + + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + if (strncmp(input, "all", 4) == 0) { + mac_reg_dump(RTW_DBGDUMP, padapter); + bb_reg_dump(RTW_DBGDUMP, padapter); + rf_reg_dump(RTW_DBGDUMP, padapter); + } + return 0; +} + + +int rtw_mp_phypara(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + + PADAPTER padapter = rtw_netdev_priv(dev); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + char input[RTW_IWD_MAX_LEN]; + u32 invalxcap = 0, ret = 0, bwrite_xcap = 0, hwxtaladdr = 0; + u16 pgval; + + if (rtw_do_mp_iwdata_len_chk(__func__, wrqu->length)) + return -EFAULT; + + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + RTW_INFO("%s:priv in=%s\n", __func__, input); + bwrite_xcap = (strncmp(input, "write_xcap=", 11) == 0) ? 1 : 0; + + if (bwrite_xcap == 1) { + ret = sscanf(input, "write_xcap=%d", &invalxcap); + invalxcap = invalxcap & 0x7f; /* xtal bit 0 ~6 */ + RTW_INFO("get crystal_cap %d\n", invalxcap); + + if (IS_HARDWARE_TYPE_8822C(padapter) && ret == 1) { + hwxtaladdr = 0x110; + pgval = invalxcap | 0x80; /* reserved default bit7 on */ + pgval = pgval | pgval << 8; /* xtal xi/xo efuse 0x110 0x111 */ + + RTW_INFO("Get crystal_cap 0x%x\n", pgval); + if (rtw_efuse_map_write(padapter, hwxtaladdr, 2, (u8*)&pgval) == _FAIL) { + RTW_INFO("%s: rtw_efuse_map_write xcap error!!\n", __func__); + sprintf(extra, "write xcap pgdata fail"); + ret = -EFAULT; + } else + sprintf(extra, "write xcap pgdata ok"); + + } + } else { + ret = sscanf(input, "xcap=%d", &invalxcap); + + if (ret == 1) { + pHalData->crystal_cap = (u8)invalxcap; + RTW_INFO("%s:crystal_cap=%d\n", __func__, pHalData->crystal_cap); + + if (rtw_phydm_set_crystal_cap(padapter, pHalData->crystal_cap) == _FALSE) { + RTW_ERR("set crystal_cap failed\n"); + rtw_warn_on(1); + } + sprintf(extra, "Set xcap=%d", invalxcap); + } + } + + wrqu->length = strlen(extra) + 1; + return ret; +} + + +int rtw_mp_SetRFPath(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + char input[RTW_IWD_MAX_LEN]; + int bMain = 1, bTurnoff = 1; +#ifdef CONFIG_ANTENNA_DIVERSITY + u8 ret = _TRUE; +#endif + + if (rtw_do_mp_iwdata_len_chk(__func__, wrqu->length)) + return -EFAULT; + + RTW_INFO("%s:iwpriv in=%s\n", __func__, input); + + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + bMain = strncmp(input, "1", 2); /* strncmp TRUE is 0*/ + bTurnoff = strncmp(input, "0", 3); /* strncmp TRUE is 0*/ + + _rtw_memset(extra, 0, wrqu->length); +#ifdef CONFIG_ANTENNA_DIVERSITY + if (bMain == 0) + ret = rtw_mp_set_antdiv(padapter, _TRUE); + else + ret = rtw_mp_set_antdiv(padapter, _FALSE); + if (ret == _FALSE) + RTW_INFO("%s:ANTENNA_DIVERSITY FAIL\n", __func__); +#endif + + if (bMain == 0) { + MP_PHY_SetRFPathSwitch(padapter, _TRUE); + RTW_INFO("%s:PHY_SetRFPathSwitch=TRUE\n", __func__); + sprintf(extra, "mp_setrfpath Main\n"); + + } else if (bTurnoff == 0) { + MP_PHY_SetRFPathSwitch(padapter, _FALSE); + RTW_INFO("%s:PHY_SetRFPathSwitch=FALSE\n", __func__); + sprintf(extra, "mp_setrfpath Aux\n"); + } else { + bMain = MP_PHY_QueryRFPathSwitch(padapter); + RTW_INFO("%s:Query RF Path = %s\n", __func__, (bMain ? "Main":"Aux")); + sprintf(extra, "RF Path %s\n" , (bMain ? "1":"0")); + } + + wrqu->length = strlen(extra); + + return 0; +} + + +int rtw_mp_switch_rf_path(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + struct mp_priv *pmp_priv; + char input[RTW_IWD_MAX_LEN]; + char *pch; + int bwlg = 1, bwla = 1, btg = 1, bbt=1; + u8 ret = 0; + + if (rtw_do_mp_iwdata_len_chk(__func__, wrqu->length)) + return -EFAULT; + + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + pmp_priv = &padapter->mppriv; + + RTW_INFO("%s: in=%s\n", __func__, input); + + _rtw_memset(extra, '\0', wrqu->length); + pch = extra; +#ifdef CONFIG_RTL8821C /* only support for 8821c wlg/wla/btg/bt RF switch path */ + if ((strncmp(input, "WLG", 3) == 0) || (strncmp(input, "1", 1) == 0)) { + pmp_priv->rf_path_cfg = SWITCH_TO_WLG; + pch += sprintf(pch, "switch rf path WLG\n"); + + } else if ((strncmp(input, "WLA", 3) == 0) || (strncmp(input, "2", 1) == 0)) { + pmp_priv->rf_path_cfg = SWITCH_TO_WLA; + pch += sprintf(pch, "switch rf path WLA\n"); + + } else if ((strncmp(input, "BTG", 3) == 0) || (strncmp(input, "0", 1) == 0)) { + pmp_priv->rf_path_cfg = SWITCH_TO_BTG; + pch += sprintf(pch, "switch rf path BTG\n"); + + } else if ((strncmp(input, "BT", 3) == 0) || (strncmp(input, "3", 1) == 0)) { + pmp_priv->rf_path_cfg = SWITCH_TO_BT; + pch += sprintf(pch, "switch rf path BT\n"); + } else { + pmp_priv->rf_path_cfg = SWITCH_TO_WLG; + pch += sprintf(pch, "Error input, default set WLG\n"); + return -EFAULT; + } + + mp_phy_switch_rf_path_set(padapter, &pmp_priv->rf_path_cfg); +#endif + wrqu->length = strlen(extra); + + return ret; + +} +int rtw_mp_QueryDrv(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + char input[RTW_IWD_MAX_LEN]; + int qAutoLoad = 1; + + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter); + + if (rtw_do_mp_iwdata_len_chk(__func__, wrqu->data.length)) + return -EFAULT; + + if (copy_from_user(input, wrqu->data.pointer, wrqu->data.length)) + return -EFAULT; + RTW_INFO("%s:iwpriv in=%s\n", __func__, input); + + qAutoLoad = strncmp(input, "autoload", 8); /* strncmp TRUE is 0*/ + + if (qAutoLoad == 0) { + RTW_INFO("%s:qAutoLoad\n", __func__); + + if (pHalData->bautoload_fail_flag) + sprintf(extra, "fail"); + else + sprintf(extra, "ok"); + } + wrqu->data.length = strlen(extra) + 1; + return 0; +} + + +int rtw_mp_PwrCtlDM(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + u8 input[RTW_IWD_MAX_LEN]; + u8 pwrtrk_state = 0; + u8 pwtk_type[5][25] = {"Thermal tracking off","Thermal tracking on", + "TSSI tracking off","TSSI tracking on","TSSI calibration"}; + + if (rtw_do_mp_iwdata_len_chk(__func__, wrqu->length)) + return -EFAULT; + + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + input[wrqu->length - 1] = '\0'; + RTW_INFO("%s: in=%s\n", __func__, input); + + if (wrqu->length == 2) { + if(input[0] >= '0' && input[0] <= '4') { + pwrtrk_state = rtw_atoi(input); + MPT_PwrCtlDM(padapter, pwrtrk_state); + sprintf(extra, "PwrCtlDM start %s\n" , pwtk_type[pwrtrk_state]); + } else { + sprintf(extra, "Error unknown number ! Please check your input number\n" + " 0 : Thermal tracking off\n 1 : Thermal tracking on\n 2 : TSSI tracking off\n" + " 3 : TSSI tracking on\n 4 : TSSI calibration\n"); + } + wrqu->length = strlen(extra); + + return 0; + } + if (strncmp(input, "start", 5) == 0 || strncmp(input, "thertrk on", 10) == 0) {/* strncmp TRUE is 0*/ + pwrtrk_state = 1; + sprintf(extra, "PwrCtlDM start %s\n" , pwtk_type[pwrtrk_state]); + } else if (strncmp(input, "thertrk off", 11) == 0 || strncmp(input, "stop", 5) == 0) { + pwrtrk_state = 0; + sprintf(extra, "PwrCtlDM stop %s\n" , pwtk_type[pwrtrk_state]); + } else if (strncmp(input, "tssitrk off", 11) == 0){ + pwrtrk_state = 2; + sprintf(extra, "PwrCtlDM stop %s\n" , pwtk_type[pwrtrk_state]); + } else if (strncmp(input, "tssitrk on", 10) == 0){ + pwrtrk_state = 3; + sprintf(extra, "PwrCtlDM start %s\n" , pwtk_type[pwrtrk_state]); + } else if (strncmp(input, "tssik", 5) == 0){ + pwrtrk_state = 4; + sprintf(extra, "PwrCtlDM start %s\n" , pwtk_type[pwrtrk_state]); + } else { + sprintf(extra, "Error input !!!\n" + " thertrk off : Thermal tracking off\n thertrk on : Thermal tracking on\n" + " tssitrk off : TSSI tracking off\n tssitrk on : TSSI tracking on\n tssik : TSSI calibration\n\n" + " 0 : Thermal tracking off\n 1 : Thermal tracking on\n 2 : TSSI tracking off\n" + " 3 : TSSI tracking on\n 4 : TSSI calibration\n"); + wrqu->length = strlen(extra); + return 0; + } + + MPT_PwrCtlDM(padapter, pwrtrk_state); + wrqu->length = strlen(extra); + + return 0; +} + +int rtw_mp_iqk(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + + rtw_mp_trigger_iqk(padapter); + + return 0; +} + +int rtw_mp_lck(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + + rtw_mp_trigger_lck(padapter); + + return 0; +} + +int rtw_mp_dpk(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + struct dm_struct *pDM_Odm = &pHalData->odmpriv; + struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter); + char *pch; + + u8 ips_mode = IPS_NUM; /* init invalid value */ + u8 lps_mode = PS_MODE_NUM; /* init invalid value */ + + if (copy_from_user(extra, wrqu->data.pointer, wrqu->data.length)) + return -EFAULT; + + *(extra + wrqu->data.length) = '\0'; + pch = extra; + + if (strncmp(extra, "off", 3) == 0 && strlen(extra) < 4) { + pDM_Odm->dpk_info.is_dpk_enable = 0; + halrf_dpk_enable_disable(pDM_Odm); + pch += sprintf(pch, "set dpk off\n"); + + } else if (strncmp(extra, "on", 2) == 0 && strlen(extra) < 3) { + pDM_Odm->dpk_info.is_dpk_enable = 1; + halrf_dpk_enable_disable(pDM_Odm); + pch += sprintf(pch, "set dpk on\n"); + } else { +#ifdef CONFIG_LPS + lps_mode = pwrctrlpriv->power_mgnt;/* keep org value */ + rtw_pm_set_lps(padapter, PS_MODE_ACTIVE); +#endif +#ifdef CONFIG_IPS + ips_mode = pwrctrlpriv->ips_mode;/* keep org value */ + rtw_pm_set_ips(padapter, IPS_NONE); +#endif + rtw_mp_trigger_dpk(padapter); + if (padapter->registrypriv.mp_mode == 0) { +#ifdef CONFIG_IPS + rtw_pm_set_ips(padapter, ips_mode); +#endif /* CONFIG_IPS */ + +#ifdef CONFIG_LPS + rtw_pm_set_lps(padapter, lps_mode); +#endif /* CONFIG_LPS */ + } + pch += sprintf(pch, "set dpk trigger\n"); + } + + wrqu->data.length = strlen(extra); + + return 0; +} + +int rtw_mp_get_tsside(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + struct dm_struct *pDM_Odm = &pHalData->odmpriv; + char input[RTW_IWD_MAX_LEN]; + u8 rfpath; + u32 tssi_de; + + u8 legal_param_num = 1; + int param_num; + char pout_str_buf[7]; + u8 signed_flag = 0; + int integer_num; + u32 decimal_num; + s32 pout; + char *pextra; + int i; + + #ifdef CONFIG_RTL8723F + /* + * rtwpriv wlan0 mp_get_tsside rf_path pout + * rf_path : 0 ~ 1 + * pout : -15.000 ~ 25.000 + * ex : rtwpriv wlan0 mp_get_tsside 0 -12.123 + */ + legal_param_num = 2; + #endif + if (wrqu->length > 128) + return -EFAULT; + + _rtw_memset(input, 0, sizeof(input)); + + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + param_num = sscanf(input, "%hhu %7s", &rfpath, pout_str_buf); + + /* Check parameter format*/ + if(param_num != legal_param_num) + goto invalid_param_format; + + if(rfpath <0 || 3 < rfpath) + goto invalid_param_format; + +#ifdef CONFIG_RTL8723F + /* Convert pout from floating-point to integer + * For Floating-Point Precision, pout*1000 + */ + if(pout_str_buf[0] == '-') + signed_flag = 1; + i = sscanf(pout_str_buf, "%d.%3u", &integer_num, &decimal_num); + pout = integer_num * 1000; + if(i == 2) { + /* Convert decimal number + * ex : 0.1 => 100, -0.1 => 100 + */ + decimal_num = (decimal_num < 10) ? decimal_num * 100 : decimal_num; + decimal_num = (decimal_num < 100) ? decimal_num * 10 : decimal_num; + pout += ((pout < 0 || signed_flag == 1) ? -decimal_num : decimal_num); + } + if(pout < -15000 || 25000 < pout) + goto invalid_param_format; +#endif + +#ifdef CONFIG_RTL8723F + /* For Floating-Point Precision, pout */ + tssi_de = halrf_get_online_tssi_de(pDM_Odm, rfpath, pout); +#else + tssi_de = halrf_tssi_get_de(pDM_Odm, rfpath); +#endif + + if (rfpath == 0) + sprintf(extra, "patha=%d hex=%02x", tssi_de, (u8)tssi_de); + else if (rfpath == 1) + sprintf(extra, "pathb=%d hex=%02x", tssi_de, (u8)tssi_de); + else if (rfpath == 2) + sprintf(extra, "pathc=%d hex=%02x", tssi_de, (u8)tssi_de); + else if (rfpath == 3) + sprintf(extra, "pathd=%d hex=%02x", tssi_de, (u8)tssi_de); + + wrqu->length = strlen(extra); + return 0; + +invalid_param_format: + sprintf(extra, "Invalid command format, please indicate RF path 0/1/2/3"); +#ifdef CONFIG_RTL8723F + pextra = extra + strlen(extra); + sprintf(pextra, " and pout value : -15.000 ~ 25.000\n"); +#endif + wrqu->length = strlen(extra); + + return 0; +} + +int rtw_mp_set_tsside(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + u32 tsside_a = 0, tsside_b = 0, tsside_c = 0, tsside_d = 0; + char input[RTW_IWD_MAX_LEN]; + + PADAPTER padapter = rtw_netdev_priv(dev); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + struct dm_struct *pDM_Odm = &pHalData->odmpriv; + + if (rtw_do_mp_iwdata_len_chk(__func__, wrqu->length)) + return -EFAULT; + + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + if (sscanf(input, "patha=%d", &tsside_a) == 1) { + sprintf(extra, "Set TSSI DE path_A: %d", tsside_a); + halrf_tssi_set_de_for_tx_verify(pDM_Odm, tsside_a, RF_PATH_A); + mpt_trigger_tssi_tracking(padapter, RF_PATH_A); + + } else if (sscanf(input, "pathb=%d", &tsside_b) == 1) { + sprintf(extra, "Set TSSI DE path_B: %d", tsside_b); + halrf_tssi_set_de_for_tx_verify(pDM_Odm, tsside_b, RF_PATH_B); + mpt_trigger_tssi_tracking(padapter, RF_PATH_B); + + } else if (sscanf(input, "pathc=%d", &tsside_c) == 1) { + sprintf(extra, "Set TSSI DE path_C: %d", tsside_c); + halrf_tssi_set_de_for_tx_verify(pDM_Odm, tsside_c, RF_PATH_C); + mpt_trigger_tssi_tracking(padapter, RF_PATH_C); + + } else if (sscanf(input, "pathd=%d", &tsside_d) == 1) { + sprintf(extra, "Set TSSI DE path_D: %d", tsside_d); + halrf_tssi_set_de_for_tx_verify(pDM_Odm, tsside_d, RF_PATH_D); + mpt_trigger_tssi_tracking(padapter, RF_PATH_D); + + } else + sprintf(extra, "Invalid command format, please input TSSI DE value within patha/b/c/d=xyz"); + + wrqu->length = strlen(extra); + + return 0; +} + +int rtw_mp_getver(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + struct mp_priv *pmp_priv; + + pmp_priv = &padapter->mppriv; + + if (copy_from_user(extra, wrqu->data.pointer, wrqu->data.length)) + return -EFAULT; + + sprintf(extra, "rtwpriv=%d\n", RTWPRIV_VER_INFO); + wrqu->data.length = strlen(extra); + return 0; +} + + +int rtw_mp_mon(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + struct mp_priv *pmp_priv = &padapter->mppriv; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct hal_ops *pHalFunc = &padapter->hal_func; + NDIS_802_11_NETWORK_INFRASTRUCTURE networkType; + int bstart = 1, bstop = 1; + + networkType = Ndis802_11Infrastructure; + if (copy_from_user(extra, wrqu->data.pointer, wrqu->data.length)) + return -EFAULT; + + *(extra + wrqu->data.length) = '\0'; + rtw_pm_set_ips(padapter, IPS_NONE); + LeaveAllPowerSaveMode(padapter); + +#ifdef CONFIG_MP_INCLUDED + if (init_mp_priv(padapter) == _FAIL) + RTW_INFO("%s: initialize MP private data Fail!\n", __func__); + padapter->mppriv.channel = 6; + + bstart = strncmp(extra, "start", 5); /* strncmp TRUE is 0*/ + bstop = strncmp(extra, "stop", 4); /* strncmp TRUE is 0*/ + if (bstart == 0) { + mp_join(padapter, WIFI_FW_ADHOC_STATE); + SetPacketRx(padapter, _TRUE, _FALSE); + SetChannel(padapter); + pmp_priv->rx_bindicatePkt = _TRUE; + pmp_priv->bRTWSmbCfg = _TRUE; + sprintf(extra, "monitor mode start\n"); + } else if (bstop == 0) { + SetPacketRx(padapter, _FALSE, _FALSE); + pmp_priv->rx_bindicatePkt = _FALSE; + pmp_priv->bRTWSmbCfg = _FALSE; + padapter->registrypriv.mp_mode = 1; + pHalFunc->hal_deinit(padapter); + padapter->registrypriv.mp_mode = 0; + pHalFunc->hal_init(padapter); + /*rtw_disassoc_cmd(padapter, 0, 0);*/ + if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _TRUE) { + rtw_disassoc_cmd(padapter, 500, 0); + rtw_indicate_disconnect(padapter, 0, _FALSE); + /*rtw_free_assoc_resources_cmd(padapter, _TRUE, 0);*/ + } + rtw_pm_set_ips(padapter, IPS_NORMAL); + sprintf(extra, "monitor mode Stop\n"); + } +#endif + wrqu->data.length = strlen(extra); + return 0; +} + +int rtw_mp_pretx_proc(PADAPTER padapter, u8 bStartTest, char *extra) +{ + struct mp_priv *pmp_priv = &padapter->mppriv; + char *pextra = extra; + + switch (pmp_priv->mode) { + + case MP_PACKET_TX: + if (bStartTest == 0) { + pmp_priv->tx.stop = 1; + pmp_priv->mode = MP_ON; + #ifdef CONFIG_RTL8822B + rtw_write8(padapter, 0x838, 0x61); + #endif + sprintf(extra, "Stop continuous Tx"); + } else if (pmp_priv->tx.stop == 1) { + pextra = extra + strlen(extra); + pextra += sprintf(pextra, "\nStart continuous DA=ffffffffffff len=1500 count=%u\n", pmp_priv->tx.count); + pmp_priv->tx.stop = 0; + #ifdef CONFIG_RTL8822B + rtw_write8(padapter, 0x838, 0x6d); + #endif + SetPacketTx(padapter); + } else + return -EFAULT; + return 0; + case MP_SINGLE_TONE_TX: + if (bStartTest != 0) + strcat(extra, "\nStart continuous DA=ffffffffffff len=1500\n infinite=yes."); + SetSingleToneTx(padapter, (u8)bStartTest); + break; + case MP_CONTINUOUS_TX: + if (bStartTest != 0) + strcat(extra, "\nStart continuous DA=ffffffffffff len=1500\n infinite=yes."); + SetContinuousTx(padapter, (u8)bStartTest); + break; + case MP_CARRIER_SUPPRISSION_TX: + if (bStartTest != 0) { + if (HwRateToMPTRate(pmp_priv->rateidx) <= MPT_RATE_11M) + strcat(extra, "\nStart continuous DA=ffffffffffff len=1500\n infinite=yes."); + else + strcat(extra, "\nSpecify carrier suppression but not CCK rate"); + } + SetCarrierSuppressionTx(padapter, (u8)bStartTest); + break; + case MP_SINGLE_CARRIER_TX: + if (bStartTest != 0) + strcat(extra, "\nStart continuous DA=ffffffffffff len=1500\n infinite=yes."); + SetSingleCarrierTx(padapter, (u8)bStartTest); + break; + + default: + sprintf(extra, "Error! Continuous-Tx is not on-going."); + return -EFAULT; + } + + if (bStartTest == 1 && pmp_priv->mode != MP_ON) { + struct mp_priv *pmp_priv = &padapter->mppriv; + + if (pmp_priv->tx.stop == 0) { + pmp_priv->tx.stop = 1; + rtw_msleep_os(5); + } +#ifdef CONFIG_80211N_HT + if(padapter->registrypriv.ht_enable && + is_supported_ht(padapter->registrypriv.wireless_mode)) + pmp_priv->tx.attrib.ht_en = 1; +#endif + pmp_priv->tx.stop = 0; + pmp_priv->tx.count = 1; + SetPacketTx(padapter); + } else + pmp_priv->mode = MP_ON; + +#if defined(CONFIG_RTL8812A) + if (IS_HARDWARE_TYPE_8812AU(padapter)) { + /* <20130425, Kordan> Turn off OFDM Rx to prevent from CCA causing Tx hang.*/ + if (pmp_priv->mode == MP_PACKET_TX) + phy_set_bb_reg(padapter, rCCAonSec_Jaguar, BIT3, 1); + else + phy_set_bb_reg(padapter, rCCAonSec_Jaguar, BIT3, 0); + } +#endif + + return 0; +} + + +int rtw_mp_tx(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + struct mp_priv *pmp_priv = &padapter->mppriv; + PMPT_CONTEXT pMptCtx = &(padapter->mppriv.mpt_ctx); + char *pextra = extra; + u32 bandwidth = 0, sg = 0, channel = 6, txpower = 40, rate = 108, ant = 0, txmode = 1, count = 0; + u8 bStartTest = 1, status = 0; +#ifdef CONFIG_MP_VHT_HW_TX_MODE + u8 Idx = 0, tmpU1B; +#endif + u16 antenna = 0; + + if (copy_from_user(extra, wrqu->data.pointer, wrqu->data.length)) + return -EFAULT; + RTW_INFO("extra = %s\n", extra); +#ifdef CONFIG_CONCURRENT_MODE + if (!is_primary_adapter(padapter)) { + sprintf(extra, "Error: MP mode can't support Virtual Adapter, Please to use main Adapter.\n"); + wrqu->data.length = strlen(extra); + return 0; + } +#endif + + if (strncmp(extra, "stop", 3) == 0) { + bStartTest = 0; /* To set Stop*/ + pmp_priv->tx.stop = 1; + sprintf(extra, "Stop continuous Tx"); + status = rtw_mp_pretx_proc(padapter, bStartTest, extra); + wrqu->data.length = strlen(extra); + return status; + } else if (strncmp(extra, "count", 5) == 0) { + if (sscanf(extra, "count=%d", &count) < 1) + RTW_INFO("Got Count=%d]\n", count); + pmp_priv->tx.count = count; + return 0; + } else if (strncmp(extra, "setting", 7) == 0) { + _rtw_memset(extra, 0, wrqu->data.length); + pextra += sprintf(pextra, "Current Setting :\n Channel:%d", pmp_priv->channel); + pextra += sprintf(pextra, "\n Bandwidth:%d", pmp_priv->bandwidth); + pextra += sprintf(pextra, "\n Rate index:%d", pmp_priv->rateidx); + pextra += sprintf(pextra, "\n TxPower index:%d", pmp_priv->txpoweridx); + pextra += sprintf(pextra, "\n Antenna TxPath:%d", pmp_priv->antenna_tx); + pextra += sprintf(pextra, "\n Antenna RxPath:%d", pmp_priv->antenna_rx); + pextra += sprintf(pextra, "\n MP Mode:%d", pmp_priv->mode); + wrqu->data.length = strlen(extra); + return 0; +#ifdef CONFIG_MP_VHT_HW_TX_MODE + } else if (strncmp(extra, "pmact", 5) == 0) { + if (strncmp(extra, "pmact=", 6) == 0) { + _rtw_memset(&pMptCtx->PMacTxInfo, 0, sizeof(pMptCtx->PMacTxInfo)); + if (strncmp(extra, "pmact=start", 11) == 0) { + pMptCtx->PMacTxInfo.bEnPMacTx = _TRUE; + sprintf(extra, "Set PMac Tx Mode start\n"); + } else { + pMptCtx->PMacTxInfo.bEnPMacTx = _FALSE; + sprintf(extra, "Set PMac Tx Mode Stop\n"); + } + if (pMptCtx->bldpc == TRUE) + pMptCtx->PMacTxInfo.bLDPC = _TRUE; + + if (pMptCtx->bstbc == TRUE) + pMptCtx->PMacTxInfo.bSTBC = _TRUE; + + pMptCtx->PMacTxInfo.bSPreamble = pmp_priv->preamble; + pMptCtx->PMacTxInfo.bSGI = pmp_priv->preamble; + pMptCtx->PMacTxInfo.BandWidth = pmp_priv->bandwidth; + pMptCtx->PMacTxInfo.TX_RATE = HwRateToMPTRate(pmp_priv->rateidx); + + pMptCtx->PMacTxInfo.Mode = pMptCtx->HWTxmode; + + pMptCtx->PMacTxInfo.NDP_sound = FALSE;/*(Adapter.PacketType == NDP_PKT)?TRUE:FALSE;*/ + + if (padapter->mppriv.pktInterval == 0) + pMptCtx->PMacTxInfo.PacketPeriod = 100; + else + pMptCtx->PMacTxInfo.PacketPeriod = padapter->mppriv.pktInterval; + + if (padapter->mppriv.pktLength < 1000) + pMptCtx->PMacTxInfo.PacketLength = 1000; + else + pMptCtx->PMacTxInfo.PacketLength = padapter->mppriv.pktLength; + + pMptCtx->PMacTxInfo.PacketPattern = rtw_random32() % 0xFF; + + if (padapter->mppriv.tx_pktcount != 0) + pMptCtx->PMacTxInfo.PacketCount = padapter->mppriv.tx_pktcount; + + pMptCtx->PMacTxInfo.Ntx = 0; + for (Idx = 16; Idx < 20; Idx++) { + tmpU1B = (padapter->mppriv.antenna_tx >> Idx) & 1; + if (tmpU1B) + pMptCtx->PMacTxInfo.Ntx++; + } + + _rtw_memset(pMptCtx->PMacTxInfo.MacAddress, 0xFF, ETH_ALEN); + + PMAC_Get_Pkt_Param(&pMptCtx->PMacTxInfo, &pMptCtx->PMacPktInfo); + + if (MPT_IS_CCK_RATE(pMptCtx->PMacTxInfo.TX_RATE)) + + CCK_generator(&pMptCtx->PMacTxInfo, &pMptCtx->PMacPktInfo); + else { + PMAC_Nsym_generator(&pMptCtx->PMacTxInfo, &pMptCtx->PMacPktInfo); + /* 24 BIT*/ + L_SIG_generator(pMptCtx->PMacPktInfo.N_sym, &pMptCtx->PMacTxInfo, &pMptCtx->PMacPktInfo); + } + /* 48BIT*/ + if (MPT_IS_HT_RATE(pMptCtx->PMacTxInfo.TX_RATE)) + HT_SIG_generator(&pMptCtx->PMacTxInfo, &pMptCtx->PMacPktInfo); + else if (MPT_IS_VHT_RATE(pMptCtx->PMacTxInfo.TX_RATE)) { + /* 48BIT*/ + VHT_SIG_A_generator(&pMptCtx->PMacTxInfo, &pMptCtx->PMacPktInfo); + + /* 26/27/29 BIT & CRC 8 BIT*/ + VHT_SIG_B_generator(&pMptCtx->PMacTxInfo); + + /* 32 BIT*/ + VHT_Delimiter_generator(&pMptCtx->PMacTxInfo); + } + + mpt_ProSetPMacTx(padapter); + + } else if (strncmp(extra, "pmact,mode=", 11) == 0) { + int txmode = 0; + + if (sscanf(extra, "pmact,mode=%d", &txmode) > 0) { + if (txmode == 1) { + pMptCtx->HWTxmode = CONTINUOUS_TX; + sprintf(extra, "\t Config HW Tx mode = CONTINUOUS_TX\n"); + } else if (txmode == 2) { + pMptCtx->HWTxmode = OFDM_Single_Tone_TX; + sprintf(extra, "\t Config HW Tx mode = OFDM_Single_Tone_TX\n"); + } else { + pMptCtx->HWTxmode = PACKETS_TX; + sprintf(extra, "\t Config HW Tx mode = PACKETS_TX\n"); + } + } else { + pMptCtx->HWTxmode = PACKETS_TX; + sprintf(extra, "\t Config HW Tx mode=\n 0 = PACKETS_TX\n 1 = CONTINUOUS_TX\n 2 = OFDM_Single_Tone_TX"); + } + } else if (strncmp(extra, "pmact,", 6) == 0) { + int PacketPeriod = 0, PacketLength = 0, PacketCout = 0; + int bldpc = 0, bstbc = 0; + + if (sscanf(extra, "pmact,period=%d", &PacketPeriod) > 0) { + padapter->mppriv.pktInterval = PacketPeriod; + RTW_INFO("PacketPeriod=%d\n", padapter->mppriv.pktInterval); + sprintf(extra, "PacketPeriod [1~255]= %d\n", padapter->mppriv.pktInterval); + + } else if (sscanf(extra, "pmact,length=%d", &PacketLength) > 0) { + padapter->mppriv.pktLength = PacketLength; + RTW_INFO("PacketPeriod=%d\n", padapter->mppriv.pktLength); + sprintf(extra, "PacketLength[~65535]=%d\n", padapter->mppriv.pktLength); + + } else if (sscanf(extra, "pmact,count=%d", &PacketCout) > 0) { + padapter->mppriv.tx_pktcount = PacketCout; + RTW_INFO("Packet Cout =%d\n", padapter->mppriv.tx_pktcount); + sprintf(extra, "Packet Cout =%d\n", padapter->mppriv.tx_pktcount); + + } else if (sscanf(extra, "pmact,ldpc=%d", &bldpc) > 0) { + pMptCtx->bldpc = bldpc; + RTW_INFO("Set LDPC =%d\n", pMptCtx->bldpc); + sprintf(extra, "Set LDPC =%d\n", pMptCtx->bldpc); + + } else if (sscanf(extra, "pmact,stbc=%d", &bstbc) > 0) { + pMptCtx->bstbc = bstbc; + RTW_INFO("Set STBC =%d\n", pMptCtx->bstbc); + sprintf(extra, "Set STBC =%d\n", pMptCtx->bstbc); + } else + sprintf(extra, "\n period={1~255}\n length={1000~65535}\n count={0~}\n ldpc={0/1}\n stbc={0/1}"); + + } + + wrqu->data.length = strlen(extra); + return 0; +#endif + } else { + + if (sscanf(extra, "ch=%d,bw=%d,rate=%d,pwr=%d,ant=%d,tx=%d", &channel, &bandwidth, &rate, &txpower, &ant, &txmode) < 6) { + RTW_INFO("Invalid format [ch=%d,bw=%d,rate=%d,pwr=%d,ant=%d,tx=%d]\n", channel, bandwidth, rate, txpower, ant, txmode); + _rtw_memset(extra, 0, wrqu->data.length); + pextra += sprintf(pextra, "\n Please input correct format as bleow:\n"); + pextra += sprintf(pextra, "\t ch=%d,bw=%d,rate=%d,pwr=%d,ant=%d,tx=%d\n", channel, bandwidth, rate, txpower, ant, txmode); + pextra += sprintf(pextra, "\n [ ch : BGN = <1~14> , A or AC = <36~165> ]"); + pextra += sprintf(pextra, "\n [ bw : Bandwidth: 0 = 20M, 1 = 40M, 2 = 80M ]"); + pextra += sprintf(pextra, "\n [ rate : CCK: 1 2 5.5 11M X 2 = < 2 4 11 22 >]"); + pextra += sprintf(pextra, "\n [ OFDM: 6 9 12 18 24 36 48 54M X 2 = < 12 18 24 36 48 72 96 108>"); + pextra += sprintf(pextra, "\n [ HT 1S2SS MCS0 ~ MCS15 : < [MCS0]=128 ~ [MCS7]=135 ~ [MCS15]=143 >"); + pextra += sprintf(pextra, "\n [ HT 3SS MCS16 ~ MCS32 : < [MCS16]=144 ~ [MCS23]=151 ~ [MCS32]=159 >"); + pextra += sprintf(pextra, "\n [ VHT 1SS MCS0 ~ MCS9 : < [MCS0]=160 ~ [MCS9]=169 >"); + pextra += sprintf(pextra, "\n [ txpower : 1~63 power index"); + pextra += sprintf(pextra, "\n [ ant : ,2T ex: AB=3 BC=6 CD=12"); + pextra += sprintf(pextra, "\n [ txmode : < 0 = CONTINUOUS_TX, 1 = PACKET_TX, 2 = SINGLE_TONE_TX, 3 = CARRIER_SUPPRISSION_TX, 4 = SINGLE_CARRIER_TX>\n"); + wrqu->data.length = strlen(extra); + return status; + + } else { + char *pextra = extra; + RTW_INFO("Got format [ch=%d,bw=%d,rate=%d,pwr=%d,ant=%d,tx=%d]\n", channel, bandwidth, rate, txpower, ant, txmode); + _rtw_memset(extra, 0, wrqu->data.length); + sprintf(extra, "Change Current channel %d to channel %d", padapter->mppriv.channel , channel); + padapter->mppriv.channel = channel; + SetChannel(padapter); + pHalData->current_channel = channel; + + if (bandwidth == 1) + bandwidth = CHANNEL_WIDTH_40; + else if (bandwidth == 2) + bandwidth = CHANNEL_WIDTH_80; + pextra = extra + strlen(pextra); + pextra += sprintf(pextra, "\nChange Current Bandwidth %d to Bandwidth %d", padapter->mppriv.bandwidth, bandwidth); + padapter->mppriv.bandwidth = (u8)bandwidth; + padapter->mppriv.preamble = sg; + SetBandwidth(padapter); + pHalData->current_channel_bw = bandwidth; + + pextra += sprintf(pextra, "\nSet power level :%d", txpower); + padapter->mppriv.txpoweridx = (u8)txpower; + pMptCtx->TxPwrLevel[RF_PATH_A] = (u8)txpower; + pMptCtx->TxPwrLevel[RF_PATH_B] = (u8)txpower; + pMptCtx->TxPwrLevel[RF_PATH_C] = (u8)txpower; + pMptCtx->TxPwrLevel[RF_PATH_D] = (u8)txpower; + SetTxPower(padapter); + + RTW_INFO("%s: bw=%d sg=%d\n", __func__, bandwidth, sg); + + if (rate <= 0x7f) + rate = wifirate2_ratetbl_inx((u8)rate); + else if (rate < 0xC8) + rate = (rate - 0x80 + MPT_RATE_MCS0); + /*HT rate 0x80(MCS0) ~ 0x8F(MCS15) ~ 0x9F(MCS31) 128~159 + VHT1SS~2SS rate 0xA0 (VHT1SS_MCS0 44) ~ 0xB3 (VHT2SS_MCS9 #63) 160~179 + VHT rate 0xB4 (VHT3SS_MCS0 64) ~ 0xC7 (VHT2SS_MCS9 #83) 180~199 + else + VHT rate 0x90(VHT1SS_MCS0) ~ 0x99(VHT1SS_MCS9) 144~153 + rate =(rate - MPT_RATE_VHT1SS_MCS0); + */ + RTW_INFO("%s: rate index=%d\n", __func__, rate); + if (rate >= MPT_RATE_LAST) + return -EINVAL; + pextra += sprintf(pextra, "\nSet data rate to %d index %d", padapter->mppriv.rateidx, rate); + + padapter->mppriv.rateidx = rate; + pMptCtx->mpt_rate_index = rate; + SetDataRate(padapter); + + pextra += sprintf(pextra, "\nSet Antenna Path :%d", ant); + switch (ant) { + case 1: + antenna = ANTENNA_A; + break; + case 2: + antenna = ANTENNA_B; + break; + case 4: + antenna = ANTENNA_C; + break; + case 8: + antenna = ANTENNA_D; + break; + case 3: + antenna = ANTENNA_AB; + break; + case 5: + antenna = ANTENNA_AC; + break; + case 9: + antenna = ANTENNA_AD; + break; + case 6: + antenna = ANTENNA_BC; + break; + case 10: + antenna = ANTENNA_BD; + break; + case 12: + antenna = ANTENNA_CD; + break; + case 7: + antenna = ANTENNA_ABC; + break; + case 14: + antenna = ANTENNA_BCD; + break; + case 11: + antenna = ANTENNA_ABD; + break; + case 15: + antenna = ANTENNA_ABCD; + break; + } + RTW_INFO("%s: antenna=0x%x\n", __func__, antenna); + padapter->mppriv.antenna_tx = antenna; + padapter->mppriv.antenna_rx = antenna; + pHalData->antenna_tx_path = antenna; + SetAntenna(padapter); + + if (txmode == 0) + pmp_priv->mode = MP_CONTINUOUS_TX; + else if (txmode == 1) { + pmp_priv->mode = MP_PACKET_TX; + pmp_priv->tx.count = count; + } else if (txmode == 2) + pmp_priv->mode = MP_SINGLE_TONE_TX; + else if (txmode == 3) + pmp_priv->mode = MP_CARRIER_SUPPRISSION_TX; + else if (txmode == 4) + pmp_priv->mode = MP_SINGLE_CARRIER_TX; + + status = rtw_mp_pretx_proc(padapter, bStartTest, extra); + } + + } + + wrqu->data.length = strlen(extra); + return status; +} + + +int rtw_mp_rx(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + struct mp_priv *pmp_priv = &padapter->mppriv; + char *pextra = extra; + u32 bandwidth = 0, sg = 0, channel = 6, ant = 0; + u16 antenna = 0; + u8 bStartRx = 0; + + if (copy_from_user(extra, wrqu->data.pointer, wrqu->data.length)) + return -EFAULT; + +#ifdef CONFIG_CONCURRENT_MODE + if (!is_primary_adapter(padapter)) { + sprintf(extra, "Error: MP mode can't support Virtual Adapter, Please to use main Adapter.\n"); + wrqu->data.length = strlen(extra); + return 0; + } +#endif + + if (strncmp(extra, "stop", 4) == 0) { + _rtw_memset(extra, 0, wrqu->data.length); + SetPacketRx(padapter, bStartRx, _FALSE); + pmp_priv->bmac_filter = _FALSE; + sprintf(extra, "Received packet OK:%d CRC error:%d ,Filter out:%d", padapter->mppriv.rx_pktcount, padapter->mppriv.rx_crcerrpktcount, padapter->mppriv.rx_pktcount_filter_out); + wrqu->data.length = strlen(extra); + return 0; + + } else if (sscanf(extra, "ch=%d,bw=%d,ant=%d", &channel, &bandwidth, &ant) < 3) { + RTW_INFO("Invalid format [ch=%d,bw=%d,ant=%d]\n", channel, bandwidth, ant); + _rtw_memset(extra, 0, wrqu->data.length); + pextra += sprintf(pextra, "\n Please input correct format as bleow:\n"); + pextra += sprintf(pextra, "\t ch=%d,bw=%d,ant=%d\n", channel, bandwidth, ant); + pextra += sprintf(pextra, "\n [ ch : BGN = <1~14> , A or AC = <36~165> ]"); + pextra += sprintf(pextra, "\n [ bw : Bandwidth: 0 = 20M, 1 = 40M, 2 = 80M ]"); + pextra += sprintf(pextra, "\n [ ant : ,2T ex: AB=3 BC=6 CD=12"); + wrqu->data.length = strlen(extra); + return 0; + + } else { + char *pextra = extra; + bStartRx = 1; + RTW_INFO("Got format [ch=%d,bw=%d,ant=%d]\n", channel, bandwidth, ant); + _rtw_memset(extra, 0, wrqu->data.length); + sprintf(extra, "Change Current channel %d to channel %d", padapter->mppriv.channel , channel); + padapter->mppriv.channel = channel; + SetChannel(padapter); + pHalData->current_channel = channel; + + if (bandwidth == 1) + bandwidth = CHANNEL_WIDTH_40; + else if (bandwidth == 2) + bandwidth = CHANNEL_WIDTH_80; + pextra = extra + strlen(extra); + pextra += sprintf(pextra, "\nChange Current Bandwidth %d to Bandwidth %d", padapter->mppriv.bandwidth, bandwidth); + padapter->mppriv.bandwidth = (u8)bandwidth; + padapter->mppriv.preamble = sg; + SetBandwidth(padapter); + pHalData->current_channel_bw = bandwidth; + + pextra += sprintf(pextra, "\nSet Antenna Path :%d", ant); + switch (ant) { + case 1: + antenna = ANTENNA_A; + break; + case 2: + antenna = ANTENNA_B; + break; + case 4: + antenna = ANTENNA_C; + break; + case 8: + antenna = ANTENNA_D; + break; + case 3: + antenna = ANTENNA_AB; + break; + case 5: + antenna = ANTENNA_AC; + break; + case 9: + antenna = ANTENNA_AD; + break; + case 6: + antenna = ANTENNA_BC; + break; + case 10: + antenna = ANTENNA_BD; + break; + case 12: + antenna = ANTENNA_CD; + break; + case 7: + antenna = ANTENNA_ABC; + break; + case 14: + antenna = ANTENNA_BCD; + break; + case 11: + antenna = ANTENNA_ABD; + break; + case 15: + antenna = ANTENNA_ABCD; + break; + } + RTW_INFO("%s: antenna=0x%x\n", __func__, antenna); + padapter->mppriv.antenna_tx = antenna; + padapter->mppriv.antenna_rx = antenna; + pHalData->antenna_tx_path = antenna; + SetAntenna(padapter); + + strcat(extra, "\nstart Rx"); + SetPacketRx(padapter, bStartRx, _FALSE); + } + wrqu->data.length = strlen(extra); + return 0; +} + + +int rtw_mp_hwtx(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + struct mp_priv *pmp_priv = &padapter->mppriv; + PMPT_CONTEXT pMptCtx = &(padapter->mppriv.mpt_ctx); + char *pch; + +#if defined(CONFIG_RTL8814A) || defined(CONFIG_RTL8821B) || defined(CONFIG_RTL8822B) \ + || defined(CONFIG_RTL8821C) || defined(CONFIG_RTL8822C) || defined(CONFIG_RTL8723F) + if (copy_from_user(extra, wrqu->data.pointer, wrqu->data.length)) + return -EFAULT; + *(extra + wrqu->data.length) = '\0'; + + _rtw_memset(&pMptCtx->PMacTxInfo, 0, sizeof(RT_PMAC_TX_INFO)); + _rtw_memcpy((void *)&pMptCtx->PMacTxInfo, (void *)extra, sizeof(RT_PMAC_TX_INFO)); + _rtw_memset(extra, 0, wrqu->data.length); + pch = extra; + + if (pMptCtx->PMacTxInfo.bEnPMacTx == 1 && pmp_priv->mode != MP_ON) { + pch += sprintf(pch, "MP Tx Running, Please Set PMac Tx Mode Stop\n"); + RTW_INFO("Error !!! MP Tx Running, Please Set PMac Tx Mode Stop\n"); + } else { + RTW_INFO("To set MAC Tx mode\n"); + if (mpt_ProSetPMacTx(padapter)) + pch += sprintf(pch, "Set PMac Tx Mode OK\n"); + else + pch += sprintf(pch, "Set PMac Tx Mode Error\n"); + } + wrqu->data.length = strlen(extra); +#endif + return 0; + +} + +int rtw_mp_pwrlmt(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + struct registry_priv *registry_par = &padapter->registrypriv; + char *pch; + + if (copy_from_user(extra, wrqu->data.pointer, wrqu->data.length)) + return -EFAULT; + + *(extra + wrqu->data.length) = '\0'; + pch = extra; + +#if CONFIG_TXPWR_LIMIT + if (strncmp(extra, "off", 3) == 0 && strlen(extra) < 4) { + padapter->registrypriv.RegEnableTxPowerLimit = 0; + pch += sprintf(pch, "Turn off Power Limit\n"); + + } else if (strncmp(extra, "on", 2) == 0 && strlen(extra) < 3) { + padapter->registrypriv.RegEnableTxPowerLimit = 1; + pch += sprintf(pch, "Turn on Power Limit\n"); + + } else +#endif + pch += sprintf(pch, "Get Power Limit Status:%s\n", (registry_par->RegEnableTxPowerLimit == 1) ? "ON" : "OFF"); + + + wrqu->data.length = strlen(extra); + return 0; +} + +int rtw_mp_pwrbyrate(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + + if (copy_from_user(extra, wrqu->data.pointer, wrqu->data.length)) + return -EFAULT; + + *(extra + wrqu->data.length) = '\0'; + if (strncmp(extra, "off", 3) == 0 && strlen(extra) < 4) { + padapter->registrypriv.RegEnableTxPowerByRate = 0; + sprintf(extra, "Turn off Tx Power by Rate\n"); + + } else if (strncmp(extra, "on", 2) == 0 && strlen(extra) < 3) { + padapter->registrypriv.RegEnableTxPowerByRate = 1; + sprintf(extra, "Turn On Tx Power by Rate\n"); + + } else { + sprintf(extra, "Get Power by Rate Status:%s\n", (padapter->registrypriv.RegEnableTxPowerByRate == 1) ? "ON" : "OFF"); + } + + wrqu->data.length = strlen(extra); + return 0; +} + + +int rtw_mp_dpk_track(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + struct dm_struct *pDM_Odm = &pHalData->odmpriv; + char *pch; + + if (copy_from_user(extra, wrqu->data.pointer, wrqu->data.length)) + return -EFAULT; + + *(extra + wrqu->data.length) = '\0'; + pch = extra; + + if (strncmp(extra, "off", 3) == 0 && strlen(extra) < 4) { + halrf_set_dpk_track(pDM_Odm, FALSE); + pch += sprintf(pch, "set dpk track off\n"); + + } else if (strncmp(extra, "on", 2) == 0 && strlen(extra) < 3) { + halrf_set_dpk_track(pDM_Odm, TRUE); + pch += sprintf(pch, "set dpk track on\n"); + } + + wrqu->data.length = strlen(extra); + return 0; +} + + +int rtw_bt_efuse_mask_file(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + char *rtw_efuse_mask_file_path; + u8 *pch; + char *ptmp, tmp; + u8 Status; + PADAPTER padapter = rtw_netdev_priv(dev); + + _rtw_memset(btmaskfileBuffer, 0x00, sizeof(btmaskfileBuffer)); + + if (copy_from_user(extra, wrqu->data.pointer, wrqu->data.length)) + return -EFAULT; + + *(extra + wrqu->data.length) = '\0'; + ptmp = extra; + + if (strncmp(extra, "data,", 5) == 0) { + u8 count = 0; + u8 i = 0; + + pch = strsep(&ptmp, ","); + + if ((pch == NULL) || (strlen(pch) == 0)) { + RTW_INFO("%s: parameter error(no cmd)!\n", __func__); + return -EFAULT; + } + + do { + pch = strsep(&ptmp, ":"); + if ((pch == NULL) || (strlen(pch) == 0)) + break; + if (strlen(pch) != 2 + || IsHexDigit(*pch) == _FALSE + || IsHexDigit(*(pch + 1)) == _FALSE + || sscanf(pch, "%hhx", &tmp) != 1 + ) { + RTW_INFO("%s: invalid 8-bit hex! input format: data,01:23:45:67:89:ab:cd:ef...\n", __func__); + return -EFAULT; + } + btmaskfileBuffer[count++] = tmp; + + } while (count < 64); + + _rtw_memset(extra, '\0' , strlen(extra)); + + for (i = 0; i < count; i++) + ptmp += sprintf(ptmp, "%02x:", btmaskfileBuffer[i]); + + padapter->registrypriv.bBTFileMaskEfuse = _TRUE; + + ptmp += sprintf(ptmp, "\nLoad BT Efuse Mask data %d hex ok\n", count); + wrqu->data.length = strlen(extra); + return 0; + } + rtw_efuse_mask_file_path = extra; + + if (rtw_is_file_readable(rtw_efuse_mask_file_path) == _TRUE) { + RTW_INFO("%s do rtw_is_file_readable = %s! ,sizeof BT maskfileBuffer %zu\n", __func__, rtw_efuse_mask_file_path, sizeof(btmaskfileBuffer)); + Status = rtw_efuse_file_read(padapter, rtw_efuse_mask_file_path, btmaskfileBuffer, sizeof(btmaskfileBuffer)); + _rtw_memset(extra, '\0' , strlen(extra)); + if (Status == _TRUE) { + padapter->registrypriv.bBTFileMaskEfuse = _TRUE; + ptmp += sprintf(ptmp, "BT efuse mask file read OK\n"); + } else { + padapter->registrypriv.bBTFileMaskEfuse = _FALSE; + ptmp += sprintf(ptmp, "read BT efuse mask file FAIL\n"); + RTW_INFO("%s rtw_efuse_file_read BT mask fail!\n", __func__); + } + } else { + padapter->registrypriv.bBTFileMaskEfuse = _FALSE; + ptmp += sprintf(ptmp, "BT efuse mask file readable FAIL\n"); + RTW_INFO("%s rtw_is_file_readable BT Mask file fail!\n", __func__); + } + wrqu->data.length = strlen(extra); + return 0; +} + + +int rtw_efuse_mask_file(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + char *rtw_efuse_mask_file_path; + u8 Status; + PADAPTER padapter = rtw_netdev_priv(dev); + + _rtw_memset(maskfileBuffer, 0x00, sizeof(maskfileBuffer)); + + if (copy_from_user(extra, wrqu->data.pointer, wrqu->data.length)) + return -EFAULT; + + *(extra + wrqu->data.length) = '\0'; + if (strncmp(extra, "off", 3) == 0 && strlen(extra) < 4) { + padapter->registrypriv.boffefusemask = 1; + sprintf(extra, "Turn off Efuse Mask\n"); + wrqu->data.length = strlen(extra); + return 0; + } + if (strncmp(extra, "on", 2) == 0 && strlen(extra) < 3) { + padapter->registrypriv.boffefusemask = 0; + sprintf(extra, "Turn on Efuse Mask\n"); + wrqu->data.length = strlen(extra); + return 0; + } + if (strncmp(extra, "data,", 5) == 0) { + u8 *pch; + char *ptmp, tmp; + u8 count = 0; + u8 i = 0; + + ptmp = extra; + pch = strsep(&ptmp, ","); + + if ((pch == NULL) || (strlen(pch) == 0)) { + RTW_INFO("%s: parameter error(no cmd)!\n", __func__); + return -EFAULT; + } + + do { + pch = strsep(&ptmp, ":"); + if ((pch == NULL) || (strlen(pch) == 0)) + break; + if (strlen(pch) != 2 + || IsHexDigit(*pch) == _FALSE + || IsHexDigit(*(pch + 1)) == _FALSE + || sscanf(pch, "%hhx", &tmp) != 1 + ) { + RTW_INFO("%s: invalid 8-bit hex! input format: data,01:23:45:67:89:ab:cd:ef...\n", __func__); + return -EFAULT; + } + maskfileBuffer[count++] = tmp; + + } while (count < 64); + + _rtw_memset(extra, '\0' , strlen(extra)); + + for (i = 0; i < count; i++) + ptmp += sprintf(ptmp, "%02x:", maskfileBuffer[i]); + + padapter->registrypriv.bFileMaskEfuse = _TRUE; + + sprintf(ptmp, "\nLoad Efuse Mask data %d hex ok\n", count); + wrqu->data.length = strlen(extra); + return 0; + } + rtw_efuse_mask_file_path = extra; + + if (rtw_is_file_readable(rtw_efuse_mask_file_path) == _TRUE) { + RTW_INFO("%s do rtw_efuse_mask_file_read = %s! ,sizeof maskfileBuffer %zu\n", __func__, rtw_efuse_mask_file_path, sizeof(maskfileBuffer)); + Status = rtw_efuse_file_read(padapter, rtw_efuse_mask_file_path, maskfileBuffer, sizeof(maskfileBuffer)); + if (Status == _TRUE) { + padapter->registrypriv.bFileMaskEfuse = _TRUE; + sprintf(extra, "efuse mask file read OK\n"); + } else { + padapter->registrypriv.bFileMaskEfuse = _FALSE; + sprintf(extra, "read efuse mask file FAIL\n"); + RTW_INFO("%s rtw_efuse_file_read mask fail!\n", __func__); + } + } else { + padapter->registrypriv.bFileMaskEfuse = _FALSE; + sprintf(extra, "efuse mask file readable FAIL\n"); + RTW_INFO("%s rtw_is_file_readable fail!\n", __func__); + } + wrqu->data.length = strlen(extra); + return 0; +} + + +int rtw_efuse_file_map(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + char *rtw_efuse_file_map_path; + u8 Status; + PEFUSE_HAL pEfuseHal; + PADAPTER padapter = rtw_netdev_priv(dev); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + struct mp_priv *pmp_priv = &padapter->mppriv; + + pEfuseHal = &pHalData->EfuseHal; + if (copy_from_user(extra, wrqu->data.pointer, wrqu->data.length)) + return -EFAULT; + + rtw_efuse_file_map_path = extra; + + _rtw_memset(pEfuseHal->fakeEfuseModifiedMap, 0xFF, EFUSE_MAX_MAP_LEN); + + if (rtw_is_file_readable(rtw_efuse_file_map_path) == _TRUE) { + RTW_INFO("%s do rtw_efuse_mask_file_read = %s!\n", __func__, rtw_efuse_file_map_path); + Status = rtw_efuse_file_read(padapter, rtw_efuse_file_map_path, pEfuseHal->fakeEfuseModifiedMap, sizeof(pEfuseHal->fakeEfuseModifiedMap)); + if (Status == _TRUE) { + pmp_priv->bloadefusemap = _TRUE; + sprintf(extra, "efuse file file_read OK\n"); + } else { + pmp_priv->bloadefusemap = _FALSE; + sprintf(extra, "efuse file file_read FAIL\n"); + } + } else { + sprintf(extra, "efuse file readable FAIL\n"); + RTW_INFO("%s rtw_is_file_readable fail!\n", __func__); + } + wrqu->data.length = strlen(extra); + return 0; +} + + +int rtw_efuse_file_map_store(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + char *rtw_efuse_file_map_path; + u8 Status; + u16 mapLen; + PEFUSE_HAL pEfuseHal; + PADAPTER padapter = rtw_netdev_priv(dev); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + struct mp_priv *pmp_priv = &padapter->mppriv; + + pEfuseHal = &pHalData->EfuseHal; + if (copy_from_user(extra, wrqu->data.pointer, wrqu->data.length)) + return -EFAULT; + + rtw_efuse_file_map_path = extra; + RTW_INFO("%s rtw_is_file_readable! %s\n", __func__, rtw_efuse_file_map_path); + + EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN , (void *)&mapLen, _FALSE); + + if (mapLen != 0) { + RTW_INFO("%s, efuse store path = %s! mapLen = %d\n", __func__, rtw_efuse_file_map_path, mapLen); + Status = rtw_efuse_file_store(padapter, rtw_efuse_file_map_path, pEfuseHal->fakeEfuseModifiedMap, mapLen); + if (Status) { + sprintf(extra, "efuse file restore OK\n"); + } else { + sprintf(extra, "efuse file restore FAIL\n"); + } + } else { + sprintf(extra, "efuse file readable FAIL\n"); + RTW_INFO("%s rtw_is_file_readable fail! map Len %d\n", __func__, mapLen); + } + + wrqu->data.length = strlen(extra); + return 0; +} + +int rtw_bt_efuse_file_map(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + char *rtw_efuse_file_map_path; + u8 Status; + PEFUSE_HAL pEfuseHal; + PADAPTER padapter = rtw_netdev_priv(dev); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + struct mp_priv *pmp_priv = &padapter->mppriv; + + pEfuseHal = &pHalData->EfuseHal; + if (copy_from_user(extra, wrqu->data.pointer, wrqu->data.length)) + return -EFAULT; + + rtw_efuse_file_map_path = extra; + + _rtw_memset(pEfuseHal->fakeBTEfuseModifiedMap, 0xFF, EFUSE_BT_MAX_MAP_LEN); + + if (rtw_is_file_readable(rtw_efuse_file_map_path) == _TRUE) { + RTW_INFO("%s do rtw_efuse_mask_file_read = %s!\n", __func__, rtw_efuse_file_map_path); + Status = rtw_efuse_file_read(padapter, rtw_efuse_file_map_path, pEfuseHal->fakeBTEfuseModifiedMap, sizeof(pEfuseHal->fakeBTEfuseModifiedMap)); + if (Status == _TRUE) { + pmp_priv->bloadBTefusemap = _TRUE; + sprintf(extra, "BT efuse file file_read OK\n"); + } else { + pmp_priv->bloadBTefusemap = _FALSE; + sprintf(extra, "BT efuse file file_read FAIL\n"); + } + } else { + sprintf(extra, "BT efuse file readable FAIL\n"); + RTW_INFO("%s rtw_is_file_readable fail!\n", __func__); + } + wrqu->data.length = strlen(extra); + return 0; +} + + +static inline void dump_buf(u8 *buf, u32 len) +{ + u32 i; + + RTW_INFO("-----------------Len %d----------------\n", len); + for (i = 0; i < len; i++) + RTW_INFO("%2.2x-", *(buf + i)); + RTW_INFO("\n"); +} + +int rtw_mp_link(struct net_device *dev, + struct iw_request_info *info, + struct iw_point *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + struct mp_priv *pmp_priv; + char input[RTW_IWD_MAX_LEN]; + int bgetrxdata = 0, btxdata = 0, bsetbt = 0; + u8 err = 0; + u32 i = 0, datalen = 0,jj, kk, waittime = 0; + u16 val = 0x00, ret = 0; + char *pextra = NULL; + u8 *setdata = NULL; + char *pch, *ptmp, *token, *tmp[4] = {0x00, 0x00, 0x00}; + + pmp_priv = &padapter->mppriv; + + if (rtw_do_mp_iwdata_len_chk(__func__, wrqu->length)) + return -EFAULT; + + if (copy_from_user(input, wrqu->pointer, wrqu->length)) + return -EFAULT; + + _rtw_memset(extra, 0, wrqu->length); + + RTW_INFO("%s: in=%s\n", __func__, input); + + bgetrxdata = (strncmp(input, "rxdata", 6) == 0) ? 1 : 0; /* strncmp TRUE is 0*/ + btxdata = (strncmp(input, "txdata", 6) == 0) ? 1 : 0; /* strncmp TRUE is 0*/ + bsetbt = (strncmp(input, "setbt", 5) == 0) ? 1 : 0; /* strncmp TRUE is 0*/ + + if (bgetrxdata) { + RTW_INFO("%s: in= 1 \n", __func__); + if (pmp_priv->mplink_brx == _TRUE) { + pch = extra; + while (waittime < 100 && pmp_priv->mplink_brx == _FALSE) { + if (pmp_priv->mplink_brx == _FALSE) + rtw_msleep_os(10); + else + break; + waittime++; + } + if (pmp_priv->mplink_brx == _TRUE) { + pch += sprintf(pch, "\n"); + + for (i = 0; i < pmp_priv->mplink_rx_len; i ++) { + pch += sprintf(pch, "%02x:", pmp_priv->mplink_buf[i]); + } + _rtw_memset(pmp_priv->mplink_buf, '\0' , sizeof(pmp_priv->mplink_buf)); + pmp_priv->mplink_brx = _FALSE; + } + } + } else if (btxdata) { + struct pkt_attrib *pattrib; + + pch = input; + setdata = rtw_zmalloc(1024); + if (setdata == NULL) { + err = -ENOMEM; + goto exit; + } + + i = 0; + while ((token = strsep(&pch, ",")) != NULL) { + if (i > 2) + break; + tmp[i] = token; + i++; + } + + /* tmp[0],[1],[2] */ + /* txdata,00e04c871200........... */ + if (strcmp(tmp[0], "txdata") == 0) { + if ((tmp[1] == NULL)) { + err = -EINVAL; + goto exit; + } + } + + datalen = strlen(tmp[1]); + if (datalen % 2) { + err = -EINVAL; + goto exit; + } + datalen /= 2; + if (datalen == 0) { + err = -EINVAL; + goto exit; + } + + RTW_INFO("%s: data len=%d\n", __FUNCTION__, datalen); + RTW_INFO("%s: tx data=%s\n", __FUNCTION__, tmp[1]); + + for (jj = 0, kk = 0; jj < datalen; jj++, kk += 2) + setdata[jj] = key_2char2num(tmp[1][kk], tmp[1][kk + 1]); + + dump_buf(setdata, datalen); + _rtw_memset(pmp_priv->mplink_buf, '\0' , sizeof(pmp_priv->mplink_buf)); + _rtw_memcpy(pmp_priv->mplink_buf, setdata, datalen); + + pattrib = &pmp_priv->tx.attrib; + pattrib->pktlen = datalen; + pmp_priv->tx.count = 1; + pmp_priv->tx.stop = 0; + pmp_priv->mplink_btx = _TRUE; + SetPacketTx(padapter); + pmp_priv->mode = MP_PACKET_TX; + + } else if (bsetbt) { + +#ifdef CONFIG_BT_COEXIST + pch = input; + i = 0; + + while ((token = strsep(&pch, ",")) != NULL) { + if (i > 3) + break; + tmp[i] = token; + i++; + } + + if (tmp[1] == NULL) { + err = -EINVAL; + goto exit; + } + + if (strcmp(tmp[1], "scbd") == 0) { + u16 org_val = 0x8002, pre_val, read_score_board_val; + u8 state; + + pre_val = (rtw_read16(padapter,(0xaa))) & 0x7fff; + + if (tmp[2] != NULL) { + state = simple_strtoul(tmp[2], &ptmp, 10); + + if (state) + org_val = org_val | BIT6; + else + org_val = org_val & (~BIT6); + + if (org_val != pre_val) { + pre_val = org_val; + rtw_write16(padapter, 0xaa, org_val); + RTW_INFO("%s,setbt scbd write org_val = 0x%x , pre_val = 0x%x\n", __func__, org_val, pre_val); + } else { + RTW_INFO("%s,setbt scbd org_val = 0x%x ,pre_val = 0x%x\n", __func__, org_val, pre_val); + } + } else { + read_score_board_val = (rtw_read16(padapter,(0xaa))) & 0x7fff; + RTW_INFO("%s,read_score_board_val = 0x%x\n", __func__, read_score_board_val); + } + goto exit; + + } else if (strcmp(tmp[1], "testmode") == 0) { + + if (tmp[2] == NULL) { + err = -EINVAL; + goto exit; + } + + val = simple_strtoul(tmp[2], &ptmp, 16); + RTW_INFO("get tmp, type %s, val =0x%x!\n", tmp[1], val); + + if (tmp[2] != NULL) { + _rtw_memset(extra, 0, wrqu->length); + pch = extra; + ret = rtw_btcoex_btset_testmode(padapter, val); + if (!CHECK_STATUS_CODE_FROM_BT_MP_OPER_RET(ret, BT_STATUS_BT_OP_SUCCESS)) { + RTW_INFO("%s: BT_OP fail = 0x%x!\n", __FUNCTION__, val); + pch += sprintf(pch, "BT_OP fail 0x%x!\n", val); + } else + pch += sprintf(pch, "Set BT_OP 0x%x done!\n", val); + } + + } +#endif /* CONFIG_BT_COEXIST */ + } + +exit: + if (setdata) + rtw_mfree(setdata, 1024); + + wrqu->length = strlen(extra); + return err; + +} + +#if defined(CONFIG_RTL8723B) +int rtw_mp_SetBT(struct net_device *dev, + struct iw_request_info *info, + union iwreq_data *wrqu, char *extra) +{ + PADAPTER padapter = rtw_netdev_priv(dev); + struct hal_ops *pHalFunc = &padapter->hal_func; + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + + BT_REQ_CMD BtReq; + PMPT_CONTEXT pMptCtx = &(padapter->mppriv.mpt_ctx); + PBT_RSP_CMD pBtRsp = (PBT_RSP_CMD)&pMptCtx->mptOutBuf[0]; + char input[128]; + char *pch, *ptmp, *token, *tmp[2] = {0x00, 0x00}; + u8 setdata[100]; + u8 resetbt = 0x00; + u8 tempval, BTStatus; + u8 H2cSetbtmac[6]; + u8 u1H2CBtMpOperParm[4] = {0x01}; + int testmode = 1, ready = 1, trxparam = 1, setgen = 1, getgen = 1, testctrl = 1, testbt = 1, readtherm = 1, setbtmac = 1; + u32 i = 0, ii = 0, jj = 0, kk = 0, cnts = 0, status = 0; + PRT_MP_FIRMWARE pBTFirmware = NULL; + + if (copy_from_user(extra, wrqu->data.pointer, wrqu->data.length)) + return -EFAULT; + + *(extra + wrqu->data.length) = '\0'; + pch = extra; + + if (strlen(extra) < 1) + return -EFAULT; + + RTW_INFO("%s:iwpriv in=%s\n", __func__, extra); + ready = strncmp(extra, "ready", 5); + testmode = strncmp(extra, "testmode", 8); /* strncmp TRUE is 0*/ + trxparam = strncmp(extra, "trxparam", 8); + setgen = strncmp(extra, "setgen", 6); + getgen = strncmp(extra, "getgen", 6); + testctrl = strncmp(extra, "testctrl", 8); + testbt = strncmp(extra, "testbt", 6); + readtherm = strncmp(extra, "readtherm", 9); + setbtmac = strncmp(extra, "setbtmac", 8); + + if (strncmp(extra, "dlbt", 4) == 0) { + pHalData->LastHMEBoxNum = 0; + pHalData->bBTFWReady = _FALSE; + rtw_write8(padapter, 0xa3, 0x05); + BTStatus = rtw_read8(padapter, 0xa0); + RTW_INFO("%s: btwmap before read 0xa0 BT Status =0x%x\n", __func__, BTStatus); + if (BTStatus != 0x04) { + pch += sprintf(pch, "BT Status not Active DLFW FAIL\n"); + goto exit; + } + + tempval = rtw_read8(padapter, 0x6B); + tempval |= BIT7; + rtw_write8(padapter, 0x6B, tempval); + + /* Attention!! Between 0x6A[14] and 0x6A[15] setting need 100us delay*/ + /* So don't write 0x6A[14]=1 and 0x6A[15]=0 together!*/ + rtw_usleep_os(100); + /* disable BT power cut*/ + /* 0x6A[14] = 0*/ + tempval = rtw_read8(padapter, 0x6B); + tempval &= ~BIT6; + rtw_write8(padapter, 0x6B, tempval); + rtw_usleep_os(100); + MPT_PwrCtlDM(padapter, 0); + rtw_write32(padapter, 0xcc, (rtw_read32(padapter, 0xcc) | 0x00000004)); + rtw_write32(padapter, 0x6b, (rtw_read32(padapter, 0x6b) & 0xFFFFFFEF)); + rtw_msleep_os(600); + rtw_write32(padapter, 0x6b, (rtw_read32(padapter, 0x6b) | 0x00000010)); + rtw_write32(padapter, 0xcc, (rtw_read32(padapter, 0xcc) & 0xFFFFFFFB)); + rtw_msleep_os(1200); + pBTFirmware = (PRT_MP_FIRMWARE)rtw_zmalloc(sizeof(RT_MP_FIRMWARE)); + if (pBTFirmware == NULL) + goto exit; + pHalData->bBTFWReady = _FALSE; + FirmwareDownloadBT(padapter, pBTFirmware); + if (pBTFirmware) + rtw_mfree((u8 *)pBTFirmware, sizeof(RT_MP_FIRMWARE)); + + RTW_INFO("Wait for FirmwareDownloadBT fw boot!\n"); + rtw_msleep_os(2000); + _rtw_memset(extra, '\0', wrqu->data.length); + BtReq.opCodeVer = 1; + BtReq.OpCode = 0; + BtReq.paraLength = 0; + mptbt_BtControlProcess(padapter, &BtReq); + rtw_msleep_os(100); + + RTW_INFO("FirmwareDownloadBT ready = 0x%x 0x%x", pMptCtx->mptOutBuf[4], pMptCtx->mptOutBuf[5]); + if ((pMptCtx->mptOutBuf[4] == 0x00) && (pMptCtx->mptOutBuf[5] == 0x00)) { + + if (padapter->mppriv.bTxBufCkFail == _TRUE) + pch += sprintf(pch, "check TxBuf Fail.\n"); + else + pch += sprintf(pch, "download FW Fail.\n"); + } else { + pch += sprintf(pch, "download FW OK.\n"); + goto exit; + } + goto exit; + } + if (strncmp(extra, "dlfw", 4) == 0) { + pHalData->LastHMEBoxNum = 0; + pHalData->bBTFWReady = _FALSE; + rtw_write8(padapter, 0xa3, 0x05); + BTStatus = rtw_read8(padapter, 0xa0); + RTW_INFO("%s: btwmap before read 0xa0 BT Status =0x%x\n", __func__, BTStatus); + if (BTStatus != 0x04) { + pch += sprintf(pch, "BT Status not Active DLFW FAIL\n"); + goto exit; + } + + tempval = rtw_read8(padapter, 0x6B); + tempval |= BIT7; + rtw_write8(padapter, 0x6B, tempval); + + /* Attention!! Between 0x6A[14] and 0x6A[15] setting need 100us delay*/ + /* So don't write 0x6A[14]=1 and 0x6A[15]=0 together!*/ + rtw_usleep_os(100); + /* disable BT power cut*/ + /* 0x6A[14] = 0*/ + tempval = rtw_read8(padapter, 0x6B); + tempval &= ~BIT6; + rtw_write8(padapter, 0x6B, tempval); + rtw_usleep_os(100); + + MPT_PwrCtlDM(padapter, 0); + rtw_write32(padapter, 0xcc, (rtw_read32(padapter, 0xcc) | 0x00000004)); + rtw_write32(padapter, 0x6b, (rtw_read32(padapter, 0x6b) & 0xFFFFFFEF)); + rtw_msleep_os(600); + rtw_write32(padapter, 0x6b, (rtw_read32(padapter, 0x6b) | 0x00000010)); + rtw_write32(padapter, 0xcc, (rtw_read32(padapter, 0xcc) & 0xFFFFFFFB)); + rtw_msleep_os(1200); + +#if defined(CONFIG_PLATFORM_SPRD) && (MP_DRIVER == 1) + /* Pull up BT reset pin.*/ + RTW_INFO("%s: pull up BT reset pin when bt start mp test\n", __func__); + rtw_wifi_gpio_wlan_ctrl(WLAN_BT_PWDN_ON); +#endif + RTW_INFO(" FirmwareDownload!\n"); + +#if defined(CONFIG_RTL8723B) + status = rtl8723b_FirmwareDownload(padapter, _FALSE); +#endif + RTW_INFO("Wait for FirmwareDownloadBT fw boot!\n"); + rtw_msleep_os(1000); +#ifdef CONFIG_BT_COEXIST + rtw_btcoex_HaltNotify(padapter); + RTW_INFO("SetBT btcoex HaltNotify !\n"); + /*hal_btcoex1ant_SetAntPath(padapter);*/ + rtw_btcoex_SetManualControl(padapter, _TRUE); +#endif + _rtw_memset(extra, '\0', wrqu->data.length); + BtReq.opCodeVer = 1; + BtReq.OpCode = 0; + BtReq.paraLength = 0; + mptbt_BtControlProcess(padapter, &BtReq); + rtw_msleep_os(200); + + RTW_INFO("FirmwareDownloadBT ready = 0x%x 0x%x", pMptCtx->mptOutBuf[4], pMptCtx->mptOutBuf[5]); + if ((pMptCtx->mptOutBuf[4] == 0x00) && (pMptCtx->mptOutBuf[5] == 0x00)) { + if (padapter->mppriv.bTxBufCkFail == _TRUE) + pch += sprintf(pch, "check TxBuf Fail.\n"); + else + pch += sprintf(pch, "download FW Fail.\n"); + } else { +#ifdef CONFIG_BT_COEXIST + rtw_btcoex_SwitchBtTRxMask(padapter); +#endif + rtw_msleep_os(200); + pch += sprintf(pch, "download FW OK.\n"); + goto exit; + } + goto exit; + } + + if (strncmp(extra, "down", 4) == 0) { + RTW_INFO("SetBT down for to hal_init !\n"); +#ifdef CONFIG_BT_COEXIST + rtw_btcoex_SetManualControl(padapter, _FALSE); + rtw_btcoex_Initialize(padapter); +#endif + pHalFunc->read_adapter_info(padapter); + pHalFunc->hal_deinit(padapter); + pHalFunc->hal_init(padapter); + rtw_pm_set_ips(padapter, IPS_NONE); + LeaveAllPowerSaveMode(padapter); + MPT_PwrCtlDM(padapter, 0); + rtw_write32(padapter, 0xcc, (rtw_read32(padapter, 0xcc) | 0x00000004)); + rtw_write32(padapter, 0x6b, (rtw_read32(padapter, 0x6b) & 0xFFFFFFEF)); + rtw_msleep_os(600); + /*rtw_write32(padapter, 0x6a, (rtw_read32(padapter, 0x6a)& 0xFFFFFFFE));*/ + rtw_write32(padapter, 0x6b, (rtw_read32(padapter, 0x6b) | 0x00000010)); + rtw_write32(padapter, 0xcc, (rtw_read32(padapter, 0xcc) & 0xFFFFFFFB)); + rtw_msleep_os(1200); + goto exit; + } + if (strncmp(extra, "disable", 7) == 0) { + RTW_INFO("SetBT disable !\n"); + rtw_write32(padapter, 0x6a, (rtw_read32(padapter, 0x6a) & 0xFFFFFFFB)); + rtw_msleep_os(500); + goto exit; + } + if (strncmp(extra, "enable", 6) == 0) { + RTW_INFO("SetBT enable !\n"); + rtw_write32(padapter, 0x6a, (rtw_read32(padapter, 0x6a) | 0x00000004)); + rtw_msleep_os(500); + goto exit; + } + if (strncmp(extra, "h2c", 3) == 0) { + RTW_INFO("SetBT h2c !\n"); + pHalData->bBTFWReady = _TRUE; + rtw_hal_fill_h2c_cmd(padapter, 0x63, 1, u1H2CBtMpOperParm); + goto exit; + } + if (strncmp(extra, "2ant", 4) == 0) { + RTW_INFO("Set BT 2ant use!\n"); + phy_set_mac_reg(padapter, 0x67, BIT5, 0x1); + rtw_write32(padapter, 0x948, 0000); + + goto exit; + } + + if (ready != 0 && testmode != 0 && trxparam != 0 && setgen != 0 && getgen != 0 && testctrl != 0 && testbt != 0 && readtherm != 0 && setbtmac != 0) + return -EFAULT; + + if (testbt == 0) { + BtReq.opCodeVer = 1; + BtReq.OpCode = 6; + BtReq.paraLength = cnts / 2; + goto todo; + } + if (ready == 0) { + BtReq.opCodeVer = 1; + BtReq.OpCode = 0; + BtReq.paraLength = 0; + goto todo; + } + + i = 0; + while ((token = strsep(&pch, ",")) != NULL) { + if (i > 1) + break; + tmp[i] = token; + i++; + } + + if ((tmp[0] != NULL) && (tmp[1] != NULL)) { + cnts = strlen(tmp[1]); + if (cnts < 1) + return -EFAULT; + + RTW_INFO("%s: cnts=%d\n", __func__, cnts); + RTW_INFO("%s: data=%s\n", __func__, tmp[1]); + + for (jj = 0, kk = 0; jj < cnts; jj++, kk += 2) { + BtReq.pParamStart[jj] = key_2char2num(tmp[1][kk], tmp[1][kk + 1]); + /* RTW_INFO("BtReq.pParamStart[%d]=0x%02x\n", jj, BtReq.pParamStart[jj]);*/ + } + } else + return -EFAULT; + + if (testmode == 0) { + BtReq.opCodeVer = 1; + BtReq.OpCode = 1; + BtReq.paraLength = 1; + } + if (trxparam == 0) { + BtReq.opCodeVer = 1; + BtReq.OpCode = 2; + BtReq.paraLength = cnts / 2; + } + if (setgen == 0) { + RTW_INFO("%s: BT_SET_GENERAL\n", __func__); + BtReq.opCodeVer = 1; + BtReq.OpCode = 3;/*BT_SET_GENERAL 3*/ + BtReq.paraLength = cnts / 2; + } + if (getgen == 0) { + RTW_INFO("%s: BT_GET_GENERAL\n", __func__); + BtReq.opCodeVer = 1; + BtReq.OpCode = 4;/*BT_GET_GENERAL 4*/ + BtReq.paraLength = cnts / 2; + } + if (readtherm == 0) { + RTW_INFO("%s: BT_GET_GENERAL\n", __func__); + BtReq.opCodeVer = 1; + BtReq.OpCode = 4;/*BT_GET_GENERAL 4*/ + BtReq.paraLength = cnts / 2; + } + + if (testctrl == 0) { + RTW_INFO("%s: BT_TEST_CTRL\n", __func__); + BtReq.opCodeVer = 1; + BtReq.OpCode = 5;/*BT_TEST_CTRL 5*/ + BtReq.paraLength = cnts / 2; + } + + RTW_INFO("%s: Req opCodeVer=%d OpCode=%d paraLength=%d\n", + __func__, BtReq.opCodeVer, BtReq.OpCode, BtReq.paraLength); + + if (BtReq.paraLength < 1) + goto todo; + for (i = 0; i < BtReq.paraLength; i++) { + RTW_INFO("%s: BtReq.pParamStart[%d] = 0x%02x\n", + __func__, i, BtReq.pParamStart[i]); + } + +todo: + _rtw_memset(extra, '\0', wrqu->data.length); + + if (pHalData->bBTFWReady == _FALSE) { + pch += sprintf(pch, "BTFWReady = FALSE.\n"); + goto exit; + } + + mptbt_BtControlProcess(padapter, &BtReq); + + if (readtherm == 0) { + pch += sprintf(pch, "BT thermal="); + for (i = 4; i < pMptCtx->mptOutLen; i++) { + if ((pMptCtx->mptOutBuf[i] == 0x00) && (pMptCtx->mptOutBuf[i + 1] == 0x00)) + goto exit; + + pch += sprintf(pch, " %d ", (pMptCtx->mptOutBuf[i] & 0x1f)); + } + } else { + for (i = 4; i < pMptCtx->mptOutLen; i++) + pch += sprintf(pch, " 0x%x ", pMptCtx->mptOutBuf[i]); + } + +exit: + wrqu->data.length = strlen(extra) + 1; + RTW_INFO("-%s: output len=%d data=%s\n", __func__, wrqu->data.length, extra); + + return status; +} + +#endif /*#ifdef CONFIG_RTL8723B*/ + +#endif diff --git a/os_dep/linux/mlme_linux.c b/os_dep/linux/mlme_linux.c new file mode 100644 index 0000000..6fd24e8 --- /dev/null +++ b/os_dep/linux/mlme_linux.c @@ -0,0 +1,444 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ + + +#define _MLME_OSDEP_C_ + +#include + + +#ifdef RTK_DMP_PLATFORM +void Linkup_workitem_callback(struct work_struct *work) +{ + struct mlme_priv *pmlmepriv = container_of(work, struct mlme_priv, Linkup_workitem); + _adapter *padapter = container_of(pmlmepriv, _adapter, mlmepriv); + + + +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 12)) + kobject_uevent(&padapter->pnetdev->dev.kobj, KOBJ_LINKUP); +#else + kobject_hotplug(&padapter->pnetdev->class_dev.kobj, KOBJ_LINKUP); +#endif + +} + +void Linkdown_workitem_callback(struct work_struct *work) +{ + struct mlme_priv *pmlmepriv = container_of(work, struct mlme_priv, Linkdown_workitem); + _adapter *padapter = container_of(pmlmepriv, _adapter, mlmepriv); + + + +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 12)) + kobject_uevent(&padapter->pnetdev->dev.kobj, KOBJ_LINKDOWN); +#else + kobject_hotplug(&padapter->pnetdev->class_dev.kobj, KOBJ_LINKDOWN); +#endif + +} +#endif + +extern void rtw_indicate_wx_assoc_event(_adapter *padapter); +extern void rtw_indicate_wx_disassoc_event(_adapter *padapter); + +void rtw_os_indicate_connect(_adapter *adapter) +{ + struct mlme_priv *pmlmepriv = &(adapter->mlmepriv); + +#ifdef CONFIG_IOCTL_CFG80211 + if ((check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == _TRUE) || + (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == _TRUE)) + rtw_cfg80211_ibss_indicate_connect(adapter); + else + rtw_cfg80211_indicate_connect(adapter); +#endif /* CONFIG_IOCTL_CFG80211 */ + + rtw_indicate_wx_assoc_event(adapter); + +#ifdef CONFIG_RTW_MESH +#if CONFIG_RTW_MESH_CTO_MGATE_CARRIER + if (!rtw_mesh_cto_mgate_required(adapter)) +#endif +#endif + rtw_netif_carrier_on(adapter->pnetdev); + + if (adapter->pid[2] != 0) + rtw_signal_process(adapter->pid[2], SIGALRM); + +#ifdef RTK_DMP_PLATFORM + _set_workitem(&adapter->mlmepriv.Linkup_workitem); +#endif + + +} + +extern void indicate_wx_scan_complete_event(_adapter *padapter); +void rtw_os_indicate_scan_done(_adapter *padapter, bool aborted) +{ +#ifdef CONFIG_IOCTL_CFG80211 + rtw_cfg80211_indicate_scan_done(padapter, aborted); +#endif + indicate_wx_scan_complete_event(padapter); +} + +static RT_PMKID_LIST backupPMKIDList[NUM_PMKID_CACHE]; +void rtw_reset_securitypriv(_adapter *adapter) +{ + u8 backupPMKIDIndex = 0; + u8 backupTKIPCountermeasure = 0x00; + u32 backupTKIPcountermeasure_time = 0; + /* add for CONFIG_IEEE80211W, none 11w also can use */ + _irqL irqL; + + _enter_critical_bh(&adapter->security_key_mutex, &irqL); + + if (adapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) { /* 802.1x */ + u8 backup_sw_encrypt, backup_sw_decrypt; + + backup_sw_encrypt = adapter->securitypriv.sw_encrypt; + backup_sw_decrypt = adapter->securitypriv.sw_decrypt; + + /* Added by Albert 2009/02/18 */ + /* We have to backup the PMK information for WiFi PMK Caching test item. */ + /* */ + /* Backup the btkip_countermeasure information. */ + /* When the countermeasure is trigger, the driver have to disconnect with AP for 60 seconds. */ + + _rtw_memset(&backupPMKIDList[0], 0x00, sizeof(RT_PMKID_LIST) * NUM_PMKID_CACHE); + + _rtw_memcpy(&backupPMKIDList[0], &adapter->securitypriv.PMKIDList[0], sizeof(RT_PMKID_LIST) * NUM_PMKID_CACHE); + backupPMKIDIndex = adapter->securitypriv.PMKIDIndex; + backupTKIPCountermeasure = adapter->securitypriv.btkip_countermeasure; + backupTKIPcountermeasure_time = adapter->securitypriv.btkip_countermeasure_time; + _rtw_memset((unsigned char *)&adapter->securitypriv, 0, sizeof(struct security_priv)); + + /* Added by Albert 2009/02/18 */ + /* Restore the PMK information to securitypriv structure for the following connection. */ + _rtw_memcpy(&adapter->securitypriv.PMKIDList[0], &backupPMKIDList[0], sizeof(RT_PMKID_LIST) * NUM_PMKID_CACHE); + adapter->securitypriv.PMKIDIndex = backupPMKIDIndex; + adapter->securitypriv.btkip_countermeasure = backupTKIPCountermeasure; + adapter->securitypriv.btkip_countermeasure_time = backupTKIPcountermeasure_time; + + adapter->securitypriv.ndisauthtype = Ndis802_11AuthModeOpen; + adapter->securitypriv.ndisencryptstatus = Ndis802_11WEPDisabled; + + adapter->securitypriv.extauth_status = WLAN_STATUS_UNSPECIFIED_FAILURE; + + adapter->securitypriv.sw_encrypt = backup_sw_encrypt; + adapter->securitypriv.sw_decrypt = backup_sw_decrypt; + + } else { /* reset values in securitypriv */ + /* if(adapter->mlmepriv.fw_state & WIFI_STATION_STATE) */ + /* { */ + struct security_priv *psec_priv = &adapter->securitypriv; + + psec_priv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open; /* open system */ + psec_priv->dot11PrivacyAlgrthm = _NO_PRIVACY_; + psec_priv->dot11PrivacyKeyIndex = 0; + + psec_priv->dot118021XGrpPrivacy = _NO_PRIVACY_; + psec_priv->dot118021XGrpKeyid = 1; + + psec_priv->ndisauthtype = Ndis802_11AuthModeOpen; + psec_priv->ndisencryptstatus = Ndis802_11WEPDisabled; + /* } */ + + psec_priv->extauth_status = WLAN_STATUS_UNSPECIFIED_FAILURE; + } + /* add for CONFIG_IEEE80211W, none 11w also can use */ + _exit_critical_bh(&adapter->security_key_mutex, &irqL); + + RTW_INFO(FUNC_ADPT_FMT" - End to Disconnect\n", FUNC_ADPT_ARG(adapter)); +} + +void rtw_os_indicate_disconnect(_adapter *adapter, u16 reason, u8 locally_generated) +{ + /* RT_PMKID_LIST backupPMKIDList[NUM_PMKID_CACHE]; */ + + + rtw_netif_carrier_off(adapter->pnetdev); /* Do it first for tx broadcast pkt after disconnection issue! */ + +#ifdef CONFIG_IOCTL_CFG80211 + rtw_cfg80211_indicate_disconnect(adapter, reason, locally_generated); +#endif /* CONFIG_IOCTL_CFG80211 */ + + rtw_indicate_wx_disassoc_event(adapter); + +#ifdef RTK_DMP_PLATFORM + _set_workitem(&adapter->mlmepriv.Linkdown_workitem); +#endif + /* modify for CONFIG_IEEE80211W, none 11w also can use the same command */ + rtw_reset_securitypriv_cmd(adapter); + + +} + +void rtw_report_sec_ie(_adapter *adapter, u8 authmode, u8 *sec_ie) +{ + uint len; + u8 *buff, *p, i; + union iwreq_data wrqu; + + + + buff = NULL; + if (authmode == _WPA_IE_ID_) { + + buff = rtw_zmalloc(IW_CUSTOM_MAX); + if (NULL == buff) { + RTW_INFO(FUNC_ADPT_FMT ": alloc memory FAIL!!\n", + FUNC_ADPT_ARG(adapter)); + return; + } + p = buff; + + p += sprintf(p, "ASSOCINFO(ReqIEs="); + + len = sec_ie[1] + 2; + len = (len < IW_CUSTOM_MAX) ? len : IW_CUSTOM_MAX; + + for (i = 0; i < len; i++) + p += sprintf(p, "%02x", sec_ie[i]); + + p += sprintf(p, ")"); + + _rtw_memset(&wrqu, 0, sizeof(wrqu)); + + wrqu.data.length = p - buff; + + wrqu.data.length = (wrqu.data.length < IW_CUSTOM_MAX) ? wrqu.data.length : IW_CUSTOM_MAX; + +#ifndef CONFIG_IOCTL_CFG80211 + wireless_send_event(adapter->pnetdev, IWEVCUSTOM, &wrqu, buff); +#endif + + rtw_mfree(buff, IW_CUSTOM_MAX); + } + + +} + +#ifdef CONFIG_AP_MODE + +void rtw_indicate_sta_assoc_event(_adapter *padapter, struct sta_info *psta) +{ + union iwreq_data wrqu; + struct sta_priv *pstapriv = &padapter->stapriv; + + if (psta == NULL) + return; + + if (psta->cmn.aid > pstapriv->max_aid) + return; + + if (pstapriv->sta_aid[psta->cmn.aid - 1] != psta) + return; + + + wrqu.addr.sa_family = ARPHRD_ETHER; + + _rtw_memcpy(wrqu.addr.sa_data, psta->cmn.mac_addr, ETH_ALEN); + + RTW_INFO("+rtw_indicate_sta_assoc_event\n"); + +#ifndef CONFIG_IOCTL_CFG80211 + wireless_send_event(padapter->pnetdev, IWEVREGISTERED, &wrqu, NULL); +#endif + +} + +void rtw_indicate_sta_disassoc_event(_adapter *padapter, struct sta_info *psta) +{ + union iwreq_data wrqu; + struct sta_priv *pstapriv = &padapter->stapriv; + + if (psta == NULL) + return; + + if (psta->cmn.aid > pstapriv->max_aid) + return; + + if (pstapriv->sta_aid[psta->cmn.aid - 1] != psta) + return; + + + wrqu.addr.sa_family = ARPHRD_ETHER; + + _rtw_memcpy(wrqu.addr.sa_data, psta->cmn.mac_addr, ETH_ALEN); + + RTW_INFO("+rtw_indicate_sta_disassoc_event\n"); + +#ifndef CONFIG_IOCTL_CFG80211 + wireless_send_event(padapter->pnetdev, IWEVEXPIRED, &wrqu, NULL); +#endif + +} + + +#ifdef CONFIG_HOSTAPD_MLME + +static int mgnt_xmit_entry(struct sk_buff *skb, struct net_device *pnetdev) +{ + struct hostapd_priv *phostapdpriv = rtw_netdev_priv(pnetdev); + _adapter *padapter = (_adapter *)phostapdpriv->padapter; + + /* RTW_INFO("%s\n", __FUNCTION__); */ + + return rtw_hal_hostap_mgnt_xmit_entry(padapter, skb); +} + +static int mgnt_netdev_open(struct net_device *pnetdev) +{ + struct hostapd_priv *phostapdpriv = rtw_netdev_priv(pnetdev); + + RTW_INFO("mgnt_netdev_open: MAC Address:" MAC_FMT "\n", MAC_ARG(pnetdev->dev_addr)); + + + init_usb_anchor(&phostapdpriv->anchored); + + rtw_netif_wake_queue(pnetdev); + + rtw_netif_carrier_on(pnetdev); + + /* rtw_write16(phostapdpriv->padapter, 0x0116, 0x0100); */ /* only excluding beacon */ + + return 0; +} +static int mgnt_netdev_close(struct net_device *pnetdev) +{ + struct hostapd_priv *phostapdpriv = rtw_netdev_priv(pnetdev); + + RTW_INFO("%s\n", __FUNCTION__); + + usb_kill_anchored_urbs(&phostapdpriv->anchored); + + rtw_netif_carrier_off(pnetdev); + + rtw_netif_stop_queue(pnetdev); + + /* rtw_write16(phostapdpriv->padapter, 0x0116, 0x3f3f); */ + + return 0; +} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)) +static const struct net_device_ops rtl871x_mgnt_netdev_ops = { + .ndo_open = mgnt_netdev_open, + .ndo_stop = mgnt_netdev_close, + .ndo_start_xmit = mgnt_xmit_entry, + #if 0 + .ndo_set_mac_address = r871x_net_set_mac_address, + .ndo_get_stats = r871x_net_get_stats, + .ndo_do_ioctl = r871x_mp_ioctl, + #endif +}; +#endif + +int hostapd_mode_init(_adapter *padapter) +{ + unsigned char mac[ETH_ALEN]; + struct hostapd_priv *phostapdpriv; + struct net_device *pnetdev; + + pnetdev = rtw_alloc_etherdev(sizeof(struct hostapd_priv)); + if (!pnetdev) + return -ENOMEM; + + /* SET_MODULE_OWNER(pnetdev); */ + ether_setup(pnetdev); + + /* pnetdev->type = ARPHRD_IEEE80211; */ + + phostapdpriv = rtw_netdev_priv(pnetdev); + phostapdpriv->pmgnt_netdev = pnetdev; + phostapdpriv->padapter = padapter; + padapter->phostapdpriv = phostapdpriv; + + /* pnetdev->init = NULL; */ + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)) + + RTW_INFO("register rtl871x_mgnt_netdev_ops to netdev_ops\n"); + + pnetdev->netdev_ops = &rtl871x_mgnt_netdev_ops; + +#else + + pnetdev->open = mgnt_netdev_open; + + pnetdev->stop = mgnt_netdev_close; + + pnetdev->hard_start_xmit = mgnt_xmit_entry; + + /* pnetdev->set_mac_address = r871x_net_set_mac_address; */ + + /* pnetdev->get_stats = r871x_net_get_stats; */ + + /* pnetdev->do_ioctl = r871x_mp_ioctl; */ + +#endif + + pnetdev->watchdog_timeo = HZ; /* 1 second timeout */ + + /* pnetdev->wireless_handlers = NULL; */ + + + + + if (dev_alloc_name(pnetdev, "mgnt.wlan%d") < 0) + RTW_INFO("hostapd_mode_init(): dev_alloc_name, fail!\n"); + + + /* SET_NETDEV_DEV(pnetdev, pintfpriv->udev); */ + + + mac[0] = 0x00; + mac[1] = 0xe0; + mac[2] = 0x4c; + mac[3] = 0x87; + mac[4] = 0x11; + mac[5] = 0x12; + + _rtw_memcpy(pnetdev->dev_addr, mac, ETH_ALEN); + + + rtw_netif_carrier_off(pnetdev); + + + /* Tell the network stack we exist */ + if (register_netdev(pnetdev) != 0) { + RTW_INFO("hostapd_mode_init(): register_netdev fail!\n"); + + if (pnetdev) + rtw_free_netdev(pnetdev); + } + + return 0; + +} + +void hostapd_mode_unload(_adapter *padapter) +{ + struct hostapd_priv *phostapdpriv = padapter->phostapdpriv; + struct net_device *pnetdev = phostapdpriv->pmgnt_netdev; + + unregister_netdev(pnetdev); + rtw_free_netdev(pnetdev); + +} + +#endif +#endif diff --git a/os_dep/linux/nlrtw.c b/os_dep/linux/nlrtw.c new file mode 100644 index 0000000..14d164b --- /dev/null +++ b/os_dep/linux/nlrtw.c @@ -0,0 +1,583 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2020 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#define _RTW_NLRTW_C_ + +#include +#include "nlrtw.h" + +#ifdef CONFIG_RTW_NLRTW + +#include +#include +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)) +#include +#endif + + +enum nlrtw_cmds { + NLRTW_CMD_UNSPEC, + + NLRTW_CMD_CHANNEL_UTILIZATION, + NLRTW_CMD_REG_CHANGE, + NLRTW_CMD_REG_BEACON_HINT, + NLRTW_CMD_RADAR_EVENT, + NLRTW_CMD_RADIO_OPMODE, + + __NLRTW_CMD_AFTER_LAST, + NLRTW_CMD_MAX = __NLRTW_CMD_AFTER_LAST - 1 +}; + +enum nlrtw_attrs { + NLRTW_ATTR_UNSPEC, + + NLRTW_ATTR_WIPHY_NAME, + NLRTW_ATTR_CHANNEL_UTILIZATIONS, + NLRTW_ATTR_CHANNEL_UTILIZATION_THRESHOLD, + NLRTW_ATTR_CHANNEL_CENTER, + NLRTW_ATTR_CHANNEL_WIDTH, + NLRTW_ATTR_RADAR_EVENT, + NLRTW_ATTR_OP_CLASS, + NLRTW_ATTR_OP_CHANNEL, + NLRTW_ATTR_OP_TXPWR_MAX, + NLRTW_ATTR_IF_OPMODES, + + __NLRTW_ATTR_AFTER_LAST, + NUM_NLRTW_ATTR = __NLRTW_ATTR_AFTER_LAST, + NLRTW_ATTR_MAX = __NLRTW_ATTR_AFTER_LAST - 1 +}; + +enum nlrtw_ch_util_attrs { + __NLRTW_ATTR_CHANNEL_UTILIZATION_INVALID, + + NLRTW_ATTR_CHANNEL_UTILIZATION_VALUE, + NLRTW_ATTR_CHANNEL_UTILIZATION_BSSID, + + __NLRTW_ATTR_CHANNEL_UTILIZATION_AFTER_LAST, + NUM_NLRTW_ATTR_CHANNEL_UTILIZATION = __NLRTW_ATTR_CHANNEL_UTILIZATION_AFTER_LAST, + NLRTW_ATTR_CHANNEL_UTILIZATION_MAX = __NLRTW_ATTR_CHANNEL_UTILIZATION_AFTER_LAST - 1 +}; + +enum nlrtw_radar_event { + NLRTW_RADAR_DETECTED, + NLRTW_RADAR_CAC_FINISHED, + NLRTW_RADAR_CAC_ABORTED, + NLRTW_RADAR_NOP_FINISHED, + NLRTW_RADAR_NOP_STARTED, /* NON_OCP started not by local radar detection */ +}; + +enum nlrtw_if_opmode_attrs { + NLRTW_IF_OPMODE_UNSPEC, + + NLRTW_IF_OPMODE_MACADDR, + NLRTW_IF_OPMODE_OP_CLASS, + NLRTW_IF_OPMODE_OP_CHANNEL, + + __NLRTW_IF_OPMODE_ATTR_AFTER_LAST, + NUM_NLRTW_IF_OPMODE_ATTR = __NLRTW_IF_OPMODE_ATTR_AFTER_LAST, + NLRTW_IF_OPMODE_ATTR_MAX = __NLRTW_IF_OPMODE_ATTR_AFTER_LAST - 1 +}; + +static int nlrtw_ch_util_set(struct sk_buff *skb, struct genl_info *info) +{ + unsigned int msg; + + if (!info->attrs[NLRTW_ATTR_CHANNEL_UTILIZATION_THRESHOLD]) + return -EINVAL; + msg = nla_get_u8(info->attrs[NLRTW_ATTR_CHANNEL_UTILIZATION_THRESHOLD]); + + return 0; +} + +static struct nla_policy nlrtw_genl_policy[NUM_NLRTW_ATTR] = { + [NLRTW_ATTR_CHANNEL_UTILIZATION_THRESHOLD] = { .type = NLA_U8 }, +}; + +static struct genl_ops nlrtw_genl_ops[] = { + { + .cmd = NLRTW_CMD_CHANNEL_UTILIZATION, + .flags = 0, +#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 2, 0) + .policy = nlrtw_genl_policy, +#endif + .doit = nlrtw_ch_util_set, + .dumpit = NULL, + }, +}; + +enum nlrtw_multicast_groups { + NLRTW_MCGRP_DEFAULT, +}; +static struct genl_multicast_group nlrtw_genl_mcgrp[] = { + [NLRTW_MCGRP_DEFAULT] = { .name = "nlrtw_default" }, +}; + +/* family definition */ +static struct genl_family nlrtw_genl_family = { + .hdrsize = 0, + .name = "nlrtw_"DRV_NAME, + .version = 1, + .maxattr = NLRTW_ATTR_MAX, +#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 2, 0) + .policy = nlrtw_genl_policy, +#endif + .netnsok = true, +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 12) + .module = THIS_MODULE, +#endif +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0) + .ops = nlrtw_genl_ops, + .n_ops = ARRAY_SIZE(nlrtw_genl_ops), + .mcgrps = nlrtw_genl_mcgrp, + .n_mcgrps = ARRAY_SIZE(nlrtw_genl_mcgrp), +#endif +}; + +static inline int nlrtw_multicast(const struct genl_family *family, + struct sk_buff *skb, u32 portid, + unsigned int group, gfp_t flags) +{ + int ret; +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0) + ret = genlmsg_multicast(&nlrtw_genl_family, skb, portid, group, flags); +#else + ret = genlmsg_multicast(skb, portid, nlrtw_genl_mcgrp[group].id, flags); +#endif + return ret; +} + +int rtw_nlrtw_ch_util_rpt(_adapter *adapter, u8 n_rpts, u8 *val, u8 **mac_addr) +{ + struct sk_buff *skb = NULL; + void *msg_header = NULL; + struct nlattr *nl_ch_util, *nl_ch_utils; + struct wiphy *wiphy; + u8 i; + int ret; + + wiphy = adapter_to_wiphy(adapter); + if (!wiphy) + return -EINVAL; + + /* allocate memory */ + skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); + if (!skb) { + nlmsg_free(skb); + return -ENOMEM; + } + + /* create the message headers */ + msg_header = genlmsg_put(skb, 0, 0, &nlrtw_genl_family, 0, + NLRTW_CMD_CHANNEL_UTILIZATION); + if (!msg_header) { + ret = -ENOMEM; + goto err_out; + } + + /* add attributes */ + ret = nla_put_string(skb, NLRTW_ATTR_WIPHY_NAME, wiphy_name(wiphy)); + + nl_ch_utils = nla_nest_start(skb, NLRTW_ATTR_CHANNEL_UTILIZATIONS); + if (!nl_ch_utils) { + ret = -EMSGSIZE; + goto err_out; + } + + for (i = 0; i < n_rpts; i++) { + nl_ch_util = nla_nest_start(skb, i); + if (!nl_ch_util) { + ret = -EMSGSIZE; + goto err_out; + } + + ret = nla_put(skb, NLRTW_ATTR_CHANNEL_UTILIZATION_BSSID, ETH_ALEN, *(mac_addr + i)); + if (ret != 0) + goto err_out; + + ret = nla_put_u8(skb, NLRTW_ATTR_CHANNEL_UTILIZATION_VALUE, *(val + i)); + if (ret != 0) + goto err_out; + + nla_nest_end(skb, nl_ch_util); + } + + nla_nest_end(skb, nl_ch_utils); + + /* finalize the message */ + genlmsg_end(skb, msg_header); + + ret = nlrtw_multicast(&nlrtw_genl_family, skb, 0, NLRTW_MCGRP_DEFAULT, GFP_KERNEL); + if (ret == -ESRCH) { + RTW_INFO("[%s] return ESRCH(No such process)." + " Maybe no process waits for this msg\n", __func__); + return ret; + } else if (ret != 0) { + RTW_INFO("[%s] ret = %d\n", __func__, ret); + return ret; + } + + return 0; +err_out: + nlmsg_free(skb); + return ret; +} + +int rtw_nlrtw_reg_change_event(_adapter *adapter) +{ + struct sk_buff *skb = NULL; + void *msg_header = NULL; + struct wiphy *wiphy; + u8 i; + int ret; + + wiphy = adapter_to_wiphy(adapter); + if (!wiphy) { + ret = -EINVAL; + goto err_out; + } + + /* allocate memory */ + skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); + if (!skb) { + ret = -ENOMEM; + goto err_out; + } + + /* create the message headers */ + msg_header = genlmsg_put(skb, 0, 0, &nlrtw_genl_family, 0, NLRTW_CMD_REG_CHANGE); + if (!msg_header) { + ret = -ENOMEM; + goto err_out; + } + + /* add attributes */ + ret = nla_put_string(skb, NLRTW_ATTR_WIPHY_NAME, wiphy_name(wiphy)); + if (ret) + goto err_out; + + /* finalize the message */ + genlmsg_end(skb, msg_header); + + ret = nlrtw_multicast(&nlrtw_genl_family, skb, 0, NLRTW_MCGRP_DEFAULT, GFP_KERNEL); + if (ret == -ESRCH) { + RTW_DBG(FUNC_WIPHY_FMT" return -ESRCH(No such process)." + " Maybe no process waits for this msg\n", FUNC_WIPHY_ARG(wiphy)); + return ret; + } else if (ret != 0) { + RTW_WARN(FUNC_WIPHY_FMT" return %d\n", FUNC_WIPHY_ARG(wiphy), ret); + return ret; + } + + return 0; + +err_out: + if (skb) + nlmsg_free(skb); + return ret; +} + +int rtw_nlrtw_reg_beacon_hint_event(_adapter *adapter) +{ + struct sk_buff *skb = NULL; + void *msg_header = NULL; + struct wiphy *wiphy; + u8 i; + int ret; + + wiphy = adapter_to_wiphy(adapter); + if (!wiphy) { + ret = -EINVAL; + goto err_out; + } + + /* allocate memory */ + skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); + if (!skb) { + ret = -ENOMEM; + goto err_out; + } + + /* create the message headers */ + msg_header = genlmsg_put(skb, 0, 0, &nlrtw_genl_family, 0, NLRTW_CMD_REG_BEACON_HINT); + if (!msg_header) { + ret = -ENOMEM; + goto err_out; + } + + /* add attributes */ + ret = nla_put_string(skb, NLRTW_ATTR_WIPHY_NAME, wiphy_name(wiphy)); + if (ret) + goto err_out; + + /* finalize the message */ + genlmsg_end(skb, msg_header); + + ret = nlrtw_multicast(&nlrtw_genl_family, skb, 0, NLRTW_MCGRP_DEFAULT, GFP_KERNEL); + if (ret == -ESRCH) { + RTW_DBG(FUNC_WIPHY_FMT" return -ESRCH(No such process)." + " Maybe no process waits for this msg\n", FUNC_WIPHY_ARG(wiphy)); + return ret; + } else if (ret != 0) { + RTW_WARN(FUNC_WIPHY_FMT" return %d\n", FUNC_WIPHY_ARG(wiphy), ret); + return ret; + } + + return 0; + +err_out: + if (skb) + nlmsg_free(skb); + return ret; +} + +#ifdef CONFIG_DFS_MASTER +static int _rtw_nlrtw_radar_event(_adapter *adapter, enum nlrtw_radar_event evt_type, u8 cch, u8 bw) +{ + struct sk_buff *skb = NULL; + void *msg_header = NULL; + struct wiphy *wiphy; + u8 i; + int ret; + + wiphy = adapter_to_wiphy(adapter); + if (!wiphy) { + ret = -EINVAL; + goto err_out; + } + + /* allocate memory */ + skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); + if (!skb) { + ret = -ENOMEM; + goto err_out; + } + + /* create the message headers */ + msg_header = genlmsg_put(skb, 0, 0, &nlrtw_genl_family, 0, NLRTW_CMD_RADAR_EVENT); + if (!msg_header) { + ret = -ENOMEM; + goto err_out; + } + + /* add attributes */ + ret = nla_put_string(skb, NLRTW_ATTR_WIPHY_NAME, wiphy_name(wiphy)); + if (ret) + goto err_out; + + ret = nla_put_u8(skb, NLRTW_ATTR_RADAR_EVENT, (uint8_t)evt_type); + if (ret != 0) + goto err_out; + + ret = nla_put_u8(skb, NLRTW_ATTR_CHANNEL_CENTER, cch); + if (ret != 0) + goto err_out; + + ret = nla_put_u8(skb, NLRTW_ATTR_CHANNEL_WIDTH, bw); + if (ret != 0) + goto err_out; + + /* finalize the message */ + genlmsg_end(skb, msg_header); + + ret = nlrtw_multicast(&nlrtw_genl_family, skb, 0, NLRTW_MCGRP_DEFAULT, GFP_KERNEL); + if (ret == -ESRCH) { + RTW_DBG(FUNC_WIPHY_FMT" return -ESRCH(No such process)." + " Maybe no process waits for this msg\n", FUNC_WIPHY_ARG(wiphy)); + return ret; + } else if (ret != 0) { + RTW_WARN(FUNC_WIPHY_FMT" return %d\n", FUNC_WIPHY_ARG(wiphy), ret); + return ret; + } + + return 0; + +err_out: + if (skb) + nlmsg_free(skb); + return ret; +} + +int rtw_nlrtw_radar_detect_event(_adapter *adapter, u8 cch, u8 bw) +{ + return _rtw_nlrtw_radar_event(adapter, NLRTW_RADAR_DETECTED, cch, bw); +} + +int rtw_nlrtw_cac_finish_event(_adapter *adapter, u8 cch, u8 bw) +{ + return _rtw_nlrtw_radar_event(adapter, NLRTW_RADAR_CAC_FINISHED, cch, bw); +} + +int rtw_nlrtw_cac_abort_event(_adapter *adapter, u8 cch, u8 bw) +{ + return _rtw_nlrtw_radar_event(adapter, NLRTW_RADAR_CAC_ABORTED, cch, bw); +} + +int rtw_nlrtw_nop_finish_event(_adapter *adapter, u8 cch, u8 bw) +{ + return _rtw_nlrtw_radar_event(adapter, NLRTW_RADAR_NOP_FINISHED, cch, bw); +} + +int rtw_nlrtw_nop_start_event(_adapter *adapter, u8 cch, u8 bw) +{ + return _rtw_nlrtw_radar_event(adapter, NLRTW_RADAR_NOP_STARTED, cch, bw); +} +#endif /* CONFIG_DFS_MASTER */ + +int rtw_nlrtw_radio_opmode_notify(struct rf_ctl_t *rfctl) +{ + struct dvobj_priv *dvobj = rfctl_to_dvobj(rfctl); + _adapter *iface; + struct sk_buff *skb = NULL; + void *msg_header = NULL; + struct nlattr *nl_if_opmodes, *nl_if_opmode; + struct wiphy *wiphy; + u16 op_txpwr_max_u16; + u8 i; + int ret; + + wiphy = dvobj_to_wiphy(dvobj); + if (!wiphy) { + ret = -EINVAL; + goto err_out; + } + + /* allocate memory */ + skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); + if (!skb) { + ret = -ENOMEM; + goto err_out; + } + + /* create the message headers */ + msg_header = genlmsg_put(skb, 0, 0, &nlrtw_genl_family, 0, NLRTW_CMD_RADIO_OPMODE); + if (!msg_header) { + ret = -ENOBUFS; + goto err_out; + } + + /* add attributes */ + ret = nla_put_string(skb, NLRTW_ATTR_WIPHY_NAME, wiphy_name(wiphy)); + if (ret) + goto err_out; + + ret = nla_put_u8(skb, NLRTW_ATTR_OP_CLASS, rfctl->op_class); + if (ret != 0) + goto err_out; + + ret = nla_put_u8(skb, NLRTW_ATTR_OP_CHANNEL, rfctl->op_ch); + if (ret != 0) + goto err_out; + + *((s16 *)&op_txpwr_max_u16) = rfctl->op_txpwr_max; + ret = nla_put_u16(skb, NLRTW_ATTR_OP_TXPWR_MAX, op_txpwr_max_u16); + if (ret != 0) + goto err_out; + + if (0) + RTW_INFO("radio: %u,%u %d\n", rfctl->op_class, rfctl->op_ch, rfctl->op_txpwr_max); + + nl_if_opmodes = nla_nest_start(skb, NLRTW_ATTR_IF_OPMODES); + if (!nl_if_opmodes) { + ret = -ENOBUFS; + goto err_out; + } + + for (i = 0; i < dvobj->iface_nums; i++) { + if (!dvobj->padapters[i]) + continue; + iface = dvobj->padapters[i]; + + if (!rfctl->if_op_class[i] || !rfctl->if_op_ch[i]) + continue; + + if (0) + RTW_INFO(ADPT_FMT": %u,%u\n", ADPT_ARG(iface), rfctl->if_op_class[i], rfctl->if_op_ch[i]); + + nl_if_opmode = nla_nest_start(skb, i + 1); + if (!nl_if_opmode) { + ret = -ENOBUFS; + goto err_out; + } + + ret = nla_put(skb, NLRTW_IF_OPMODE_MACADDR, ETH_ALEN, adapter_mac_addr(iface)); + if (ret != 0) + goto err_out; + + ret = nla_put_u8(skb, NLRTW_IF_OPMODE_OP_CLASS, rfctl->if_op_class[i]); + if (ret != 0) + goto err_out; + + ret = nla_put_u8(skb, NLRTW_IF_OPMODE_OP_CHANNEL, rfctl->if_op_ch[i]); + if (ret != 0) + goto err_out; + + nla_nest_end(skb, nl_if_opmode); + } + + nla_nest_end(skb, nl_if_opmodes); + + /* finalize the message */ + genlmsg_end(skb, msg_header); + + ret = nlrtw_multicast(&nlrtw_genl_family, skb, 0, NLRTW_MCGRP_DEFAULT, GFP_KERNEL); + if (ret == -ESRCH) { + RTW_DBG(FUNC_WIPHY_FMT" return -ESRCH(No such process)." + " Maybe no process waits for this msg\n", FUNC_WIPHY_ARG(wiphy)); + return ret; + } else if (ret != 0) { + RTW_WARN(FUNC_WIPHY_FMT" return %d\n", FUNC_WIPHY_ARG(wiphy), ret); + return ret; + } + + return 0; + +err_out: + if (skb) + nlmsg_free(skb); + return ret; +} + +int rtw_nlrtw_init(void) +{ + int err; + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0) + err = genl_register_family(&nlrtw_genl_family); + if (err) + return err; +#else + err = genl_register_family_with_ops(&nlrtw_genl_family, nlrtw_genl_ops, ARRAY_SIZE(nlrtw_genl_ops)); + if (err) + return err; + + err = genl_register_mc_group(&nlrtw_genl_family, &nlrtw_genl_mcgrp[0]); + if (err) { + genl_unregister_family(&nlrtw_genl_family); + return err; + } +#endif + RTW_INFO("[%s] %s\n", __func__, nlrtw_genl_family.name); + return 0; +} + +int rtw_nlrtw_deinit(void) +{ + int err; + + err = genl_unregister_family(&nlrtw_genl_family); + RTW_INFO("[%s] err = %d\n", __func__, err); + + return err; +} +#endif /* CONFIG_RTW_NLRTW */ diff --git a/os_dep/linux/nlrtw.h b/os_dep/linux/nlrtw.h new file mode 100644 index 0000000..374002a --- /dev/null +++ b/os_dep/linux/nlrtw.h @@ -0,0 +1,48 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2020 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#ifndef __RTW_NLRTW_H_ +#define __RTW_NLRTW_H_ + +#ifdef CONFIG_RTW_NLRTW +int rtw_nlrtw_init(void); +int rtw_nlrtw_deinit(void); +int rtw_nlrtw_ch_util_rpt(_adapter *adapter, u8 n_rpts, u8 *val, u8 **mac_addr); +int rtw_nlrtw_reg_change_event(_adapter *adapter); +int rtw_nlrtw_reg_beacon_hint_event(_adapter *adapter); +int rtw_nlrtw_radio_opmode_notify(struct rf_ctl_t *rfctl); +#else +static inline int rtw_nlrtw_init(void) {return _FAIL;} +static inline int rtw_nlrtw_deinit(void) {return _FAIL;} +static inline int rtw_nlrtw_ch_util_rpt(_adapter *adapter, u8 n_rpts, u8 *val, u8 **mac_addr) {return _FAIL;} +static inline int rtw_nlrtw_reg_change_event(_adapter *adapter) {return _FAIL;} +static inline int rtw_nlrtw_reg_beacon_hint_event(_adapter *adapter) {return _FAIL;} +static inline int rtw_nlrtw_radio_opmode_notify(struct rf_ctl_t *rfctl) {return _FAIL;} +#endif /* CONFIG_RTW_NLRTW */ + +#if defined(CONFIG_RTW_NLRTW) && defined(CONFIG_DFS_MASTER) +int rtw_nlrtw_radar_detect_event(_adapter *adapter, u8 cch, u8 bw); +int rtw_nlrtw_cac_finish_event(_adapter *adapter, u8 cch, u8 bw); +int rtw_nlrtw_cac_abort_event(_adapter *adapter, u8 cch, u8 bw); +int rtw_nlrtw_nop_finish_event(_adapter *adapter, u8 cch, u8 bw); +int rtw_nlrtw_nop_start_event(_adapter *adapter, u8 cch, u8 bw); +#else +static inline int rtw_nlrtw_radar_detect_event(_adapter *adapter, u8 cch, u8 bw) {return _FAIL;} +static inline int rtw_nlrtw_cac_finish_event(_adapter *adapter, u8 cch, u8 bw) {return _FAIL;} +static inline int rtw_nlrtw_cac_abort_event(_adapter *adapter, u8 cch, u8 bw) {return _FAIL;} +static inline int rtw_nlrtw_nop_finish_event(_adapter *adapter, u8 cch, u8 bw) {return _FAIL;} +static inline int rtw_nlrtw_nop_start_event(_adapter *adapter, u8 cch, u8 bw) {return _FAIL;} +#endif /* defined(CONFIG_RTW_NLRTW) && defined(CONFIG_DFS_MASTER) */ + +#endif /* __RTW_NLRTW_H_ */ diff --git a/os_dep/linux/os_intfs.c b/os_dep/linux/os_intfs.c new file mode 100644 index 0000000..bbed1b1 --- /dev/null +++ b/os_dep/linux/os_intfs.c @@ -0,0 +1,5768 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2019 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#define _OS_INTFS_C_ + +#include +#include + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Realtek Wireless Lan Driver"); +MODULE_AUTHOR("Realtek Semiconductor Corp."); +MODULE_VERSION(DRIVERVERSION); + +/* module param defaults */ +int rtw_chip_version = 0x00; +int rtw_rfintfs = HWPI; +int rtw_lbkmode = 0;/* RTL8712_AIR_TRX; */ +#ifdef DBG_LA_MODE +int rtw_la_mode_en=0; +module_param(rtw_la_mode_en, int, 0644); +#endif +int rtw_network_mode = Ndis802_11IBSS;/* Ndis802_11Infrastructure; */ /* infra, ad-hoc, auto */ +/* NDIS_802_11_SSID ssid; */ +int rtw_channel = 1;/* ad-hoc support requirement */ +int rtw_wireless_mode = WIRELESS_MODE_MAX; +module_param(rtw_wireless_mode, int, 0644); +int rtw_vrtl_carrier_sense = AUTO_VCS; +int rtw_vcs_type = RTS_CTS; +int rtw_rts_thresh = 2347; +int rtw_frag_thresh = 2346; +int rtw_preamble = PREAMBLE_LONG;/* long, short, auto */ +int rtw_scan_mode = 1;/* active, passive */ +/* int smart_ps = 1; */ +#ifdef CONFIG_POWER_SAVING + /* IPS configuration */ + int rtw_ips_mode = RTW_IPS_MODE; + + /* LPS configuration */ +/* RTW_LPS_MODE=0:disable, 1:LPS , 2:LPS with clock gating, 3: power gating */ +#if (RTW_LPS_MODE > 0) + int rtw_power_mgnt = PS_MODE_MAX; + + #ifdef CONFIG_USB_HCI + int rtw_lps_level = LPS_NORMAL; /*USB default LPS level*/ + #else /*SDIO,PCIE*/ + int rtw_lps_level = (RTW_LPS_MODE - 1); + #endif/*CONFIG_USB_HCI*/ +#else + int rtw_power_mgnt = PS_MODE_ACTIVE; + int rtw_lps_level = LPS_NORMAL; +#endif + + int rtw_lps_chk_by_tp = 1; + + /* WOW LPS configuration */ +#ifdef CONFIG_WOWLAN +/* RTW_WOW_LPS_MODE=0:disable, 1:LPS , 2:LPS with clock gating, 3: power gating */ +#if (RTW_WOW_LPS_MODE > 0) + int rtw_wow_power_mgnt = PS_MODE_MAX; + int rtw_wow_lps_level = (RTW_WOW_LPS_MODE - 1); +#else + int rtw_wow_power_mgnt = PS_MODE_ACTIVE; + int rtw_wow_lps_level = LPS_NORMAL; +#endif +#endif /* CONFIG_WOWLAN */ + +#else /* !CONFIG_POWER_SAVING */ + int rtw_ips_mode = IPS_NONE; + int rtw_power_mgnt = PS_MODE_ACTIVE; + int rtw_lps_level = LPS_NORMAL; + int rtw_lps_chk_by_tp = 0; +#ifdef CONFIG_WOWLAN + int rtw_wow_power_mgnt = PS_MODE_ACTIVE; + int rtw_wow_lps_level = LPS_NORMAL; +#endif /* CONFIG_WOWLAN */ +#endif /* CONFIG_POWER_SAVING */ + +#ifdef CONFIG_NARROWBAND_SUPPORTING +int rtw_nb_config = CONFIG_NB_VALUE; +module_param(rtw_nb_config, int, 0644); +MODULE_PARM_DESC(rtw_nb_config, "5M/10M/Normal bandwidth configuration"); +#endif + +module_param(rtw_ips_mode, int, 0644); +MODULE_PARM_DESC(rtw_ips_mode, "The default IPS mode"); + +module_param(rtw_lps_level, int, 0644); +MODULE_PARM_DESC(rtw_lps_level, "The default LPS level"); + +#ifdef CONFIG_LPS_1T1R +int rtw_lps_1t1r = RTW_LPS_1T1R; +module_param(rtw_lps_1t1r, int, 0644); +MODULE_PARM_DESC(rtw_lps_1t1r, "The default LPS 1T1R setting"); +#endif + +module_param(rtw_lps_chk_by_tp, int, 0644); + +#ifdef CONFIG_WOWLAN +module_param(rtw_wow_power_mgnt, int, 0644); +MODULE_PARM_DESC(rtw_wow_power_mgnt, "The default WOW LPS mode"); +module_param(rtw_wow_lps_level, int, 0644); +MODULE_PARM_DESC(rtw_wow_lps_level, "The default WOW LPS level"); +#ifdef CONFIG_LPS_1T1R +int rtw_wow_lps_1t1r = RTW_WOW_LPS_1T1R; +module_param(rtw_wow_lps_1t1r, int, 0644); +MODULE_PARM_DESC(rtw_wow_lps_1t1r, "The default WOW LPS 1T1R setting"); +#endif +#endif /* CONFIG_WOWLAN */ + +/* LPS: + * rtw_smart_ps = 0 => TX: pwr bit = 1, RX: PS_Poll + * rtw_smart_ps = 1 => TX: pwr bit = 0, RX: PS_Poll + * rtw_smart_ps = 2 => TX: pwr bit = 0, RX: NullData with pwr bit = 0 +*/ +int rtw_smart_ps = 2; + +int rtw_max_bss_cnt = 0; +module_param(rtw_max_bss_cnt, int, 0644); +#ifdef CONFIG_WMMPS_STA +/* WMMPS: + * rtw_smart_ps = 0 => Only for fw test + * rtw_smart_ps = 1 => Refer to Beacon's TIM Bitmap + * rtw_smart_ps = 2 => Don't refer to Beacon's TIM Bitmap +*/ +int rtw_wmm_smart_ps = 2; +#endif /* CONFIG_WMMPS_STA */ + +int rtw_check_fw_ps = 1; + +#ifdef CONFIG_TX_EARLY_MODE +int rtw_early_mode = 1; +#endif + +#ifdef CONFIG_RTW_SW_LED +int rtw_led_ctrl = 1; /* Default is set to Normal blink */ +#endif + +int rtw_usb_rxagg_mode = 2;/* RX_AGG_DMA=1, RX_AGG_USB=2 */ +module_param(rtw_usb_rxagg_mode, int, 0644); + +int rtw_dynamic_agg_enable = 1; +module_param(rtw_dynamic_agg_enable, int, 0644); + +/* set log level when inserting driver module, default log level is _DRV_INFO_ = 4, +* please refer to "How_to_set_driver_debug_log_level.doc" to set the available level. +*/ +#ifdef CONFIG_RTW_DEBUG +#ifdef RTW_LOG_LEVEL + uint rtw_drv_log_level = (uint)RTW_LOG_LEVEL; /* from Makefile */ +#else + uint rtw_drv_log_level = _DRV_INFO_; +#endif +module_param(rtw_drv_log_level, uint, 0644); +MODULE_PARM_DESC(rtw_drv_log_level, "set log level when insert driver module, default log level is _DRV_INFO_ = 4"); +#endif +int rtw_radio_enable = 1; +int rtw_long_retry_lmt = 7; +int rtw_short_retry_lmt = 7; +int rtw_busy_thresh = 40; +/* int qos_enable = 0; */ /* * */ +int rtw_ack_policy = NORMAL_ACK; + +int rtw_mp_mode = 0; + +#if defined(CONFIG_MP_INCLUDED) && defined(CONFIG_RTW_CUSTOMER_STR) +uint rtw_mp_customer_str = 0; +module_param(rtw_mp_customer_str, uint, 0644); +MODULE_PARM_DESC(rtw_mp_customer_str, "Whether or not to enable customer str support on MP mode"); +#endif + +int rtw_software_encrypt = 0; +int rtw_software_decrypt = 0; + +int rtw_acm_method = 0;/* 0:By SW 1:By HW. */ + +int rtw_wmm_enable = 1;/* default is set to enable the wmm. */ + +#ifdef CONFIG_WMMPS_STA +/* uapsd (unscheduled automatic power-save delivery) = a kind of wmmps */ +/* 0: NO_LIMIT, 1: TWO_MSDU, 2: FOUR_MSDU, 3: SIX_MSDU */ +int rtw_uapsd_max_sp = NO_LIMIT; +/* BIT0: AC_VO UAPSD, BIT1: AC_VI UAPSD, BIT2: AC_BK UAPSD, BIT3: AC_BE UAPSD */ +int rtw_uapsd_ac_enable = 0x0; +#endif /* CONFIG_WMMPS_STA */ + +#if defined(CONFIG_RTL8814A) + int rtw_pwrtrim_enable = 2; /* disable kfree , rename to power trim disable */ +#elif defined(CONFIG_RTL8821C) || defined(CONFIG_RTL8822B) || defined(CONFIG_RTL8822C) + /*PHYDM API, must enable by default*/ + int rtw_pwrtrim_enable = 1; +#else + int rtw_pwrtrim_enable = 0; /* Default Enalbe power trim by efuse config */ +#endif + +#if CONFIG_TX_AC_LIFETIME +uint rtw_tx_aclt_flags = CONFIG_TX_ACLT_FLAGS; +module_param(rtw_tx_aclt_flags, uint, 0644); +MODULE_PARM_DESC(rtw_tx_aclt_flags, "device TX AC queue packet lifetime control flags"); + +static uint rtw_tx_aclt_conf_default[3] = CONFIG_TX_ACLT_CONF_DEFAULT; +static uint rtw_tx_aclt_conf_default_num = 0; +module_param_array(rtw_tx_aclt_conf_default, uint, &rtw_tx_aclt_conf_default_num, 0644); +MODULE_PARM_DESC(rtw_tx_aclt_conf_default, "device TX AC queue lifetime config for default status"); + +#ifdef CONFIG_AP_MODE +#if CONFIG_RTW_AP_DATA_BMC_TO_UC +static uint rtw_tx_aclt_conf_ap_m2u[3] = CONFIG_TX_ACLT_CONF_AP_M2U; +static uint rtw_tx_aclt_conf_ap_m2u_num = 0; +module_param_array(rtw_tx_aclt_conf_ap_m2u, uint, &rtw_tx_aclt_conf_ap_m2u_num, 0644); +MODULE_PARM_DESC(rtw_tx_aclt_conf_ap_m2u, "device TX AC queue lifetime config for AP mode M2U status"); +#endif +#endif /* CONFIG_AP_MODE */ + +#ifdef CONFIG_RTW_MESH +static uint rtw_tx_aclt_conf_mesh[3] = CONFIG_TX_ACLT_CONF_MESH; +static uint rtw_tx_aclt_conf_mesh_num = 0; +module_param_array(rtw_tx_aclt_conf_mesh, uint, &rtw_tx_aclt_conf_mesh_num, 0644); +MODULE_PARM_DESC(rtw_tx_aclt_conf_mesh, "device TX AC queue lifetime config for MESH status"); +#endif +#endif /* CONFIG_TX_AC_LIFETIME */ + +uint rtw_tx_bw_mode = 0x21; +module_param(rtw_tx_bw_mode, uint, 0644); +MODULE_PARM_DESC(rtw_tx_bw_mode, "The max tx bw for 2.4G and 5G. format is the same as rtw_bw_mode"); + +#ifdef CONFIG_FW_HANDLE_TXBCN +uint rtw_tbtt_rpt = 0; /*ROOT AP - BIT0, VAP1 - BIT1, VAP2 - BIT2, VAP3 - VAP3, FW report TBTT INT by C2H*/ +module_param(rtw_tbtt_rpt, uint, 0644); +#endif + +#ifdef CONFIG_80211N_HT +int rtw_ht_enable = 1; +/* 0: 20 MHz, 1: 40 MHz, 2: 80 MHz, 3: 160MHz, 4: 80+80MHz +* 2.4G use bit 0 ~ 3, 5G use bit 4 ~ 7 +* 0x21 means enable 2.4G 40MHz & 5G 80MHz */ +#ifdef CONFIG_RTW_CUSTOMIZE_BWMODE +int rtw_bw_mode = CONFIG_RTW_CUSTOMIZE_BWMODE; +#else +int rtw_bw_mode = 0x21; +#endif +int rtw_ampdu_enable = 1;/* for enable tx_ampdu , */ /* 0: disable, 0x1:enable */ +int rtw_rx_stbc = 1;/* 0: disable, bit(0):enable 2.4g, bit(1):enable 5g, default is set to enable 2.4GHZ for IOT issue with bufflao's AP at 5GHZ */ +#if (defined(CONFIG_RTL8814A) || defined(CONFIG_RTL8814B) || defined(CONFIG_RTL8822B) || defined(CONFIG_RTL8822C)) && defined(CONFIG_PCI_HCI) +int rtw_rx_ampdu_amsdu = 2;/* 0: disabled, 1:enabled, 2:auto . There is an IOT issu with DLINK DIR-629 when the flag turn on */ +#elif ((defined(CONFIG_RTL8822B) || defined(CONFIG_RTL8822C)) && defined(CONFIG_SDIO_HCI)) +int rtw_rx_ampdu_amsdu = 1; +#else +int rtw_rx_ampdu_amsdu;/* 0: disabled, 1:enabled, 2:auto . There is an IOT issu with DLINK DIR-629 when the flag turn on */ +#endif +/* +* 2: Follow the AMSDU filed in ADDBA Resp. (Deault) +* 0: Force the AMSDU filed in ADDBA Resp. to be disabled. +* 1: Force the AMSDU filed in ADDBA Resp. to be enabled. +*/ +int rtw_tx_ampdu_amsdu = 2; + +int rtw_quick_addba_req = 0; + +static uint rtw_rx_ampdu_sz_limit_1ss[4] = CONFIG_RTW_RX_AMPDU_SZ_LIMIT_1SS; +static uint rtw_rx_ampdu_sz_limit_1ss_num = 0; +module_param_array(rtw_rx_ampdu_sz_limit_1ss, uint, &rtw_rx_ampdu_sz_limit_1ss_num, 0644); +MODULE_PARM_DESC(rtw_rx_ampdu_sz_limit_1ss, "RX AMPDU size limit for 1SS link of each BW, 0xFF: no limitation"); + +static uint rtw_rx_ampdu_sz_limit_2ss[4] = CONFIG_RTW_RX_AMPDU_SZ_LIMIT_2SS; +static uint rtw_rx_ampdu_sz_limit_2ss_num = 0; +module_param_array(rtw_rx_ampdu_sz_limit_2ss, uint, &rtw_rx_ampdu_sz_limit_2ss_num, 0644); +MODULE_PARM_DESC(rtw_rx_ampdu_sz_limit_2ss, "RX AMPDU size limit for 2SS link of each BW, 0xFF: no limitation"); + +static uint rtw_rx_ampdu_sz_limit_3ss[4] = CONFIG_RTW_RX_AMPDU_SZ_LIMIT_3SS; +static uint rtw_rx_ampdu_sz_limit_3ss_num = 0; +module_param_array(rtw_rx_ampdu_sz_limit_3ss, uint, &rtw_rx_ampdu_sz_limit_3ss_num, 0644); +MODULE_PARM_DESC(rtw_rx_ampdu_sz_limit_3ss, "RX AMPDU size limit for 3SS link of each BW, 0xFF: no limitation"); + +static uint rtw_rx_ampdu_sz_limit_4ss[4] = CONFIG_RTW_RX_AMPDU_SZ_LIMIT_4SS; +static uint rtw_rx_ampdu_sz_limit_4ss_num = 0; +module_param_array(rtw_rx_ampdu_sz_limit_4ss, uint, &rtw_rx_ampdu_sz_limit_4ss_num, 0644); +MODULE_PARM_DESC(rtw_rx_ampdu_sz_limit_4ss, "RX AMPDU size limit for 4SS link of each BW, 0xFF: no limitation"); + +/* Short GI support Bit Map +* BIT0 - 20MHz, 0: non-support, 1: support +* BIT1 - 40MHz, 0: non-support, 1: support +* BIT2 - 80MHz, 0: non-support, 1: support +* BIT3 - 160MHz, 0: non-support, 1: support */ +int rtw_short_gi = 0xf; +/* BIT0: Enable VHT LDPC Rx, BIT1: Enable VHT LDPC Tx, BIT4: Enable HT LDPC Rx, BIT5: Enable HT LDPC Tx */ +int rtw_ldpc_cap = 0x33; +/* BIT0: Enable VHT STBC Rx, BIT1: Enable VHT STBC Tx, BIT4: Enable HT STBC Rx, BIT5: Enable HT STBC Tx */ +#ifdef CONFIG_RTL8192F +int rtw_stbc_cap = 0x30; +#else +int rtw_stbc_cap = 0x13; +#endif +module_param(rtw_stbc_cap, int, 0644); +/* +* BIT0: Enable VHT SU Beamformer +* BIT1: Enable VHT SU Beamformee +* BIT2: Enable VHT MU Beamformer, depend on VHT SU Beamformer +* BIT3: Enable VHT MU Beamformee, depend on VHT SU Beamformee +* BIT4: Enable HT Beamformer +* BIT5: Enable HT Beamformee +*/ +int rtw_beamform_cap = BIT(1) | BIT(3); +int rtw_bfer_rf_number = 0; /*BeamformerCapRfNum Rf path number, 0 for auto, others for manual*/ +int rtw_bfee_rf_number = 0; /*BeamformeeCapRfNum Rf path number, 0 for auto, others for manual*/ + +#endif /* CONFIG_80211N_HT */ + +#ifdef CONFIG_80211AC_VHT +int rtw_vht_enable = 1; /* 0:disable, 1:enable, 2:force auto enable */ +module_param(rtw_vht_enable, int, 0644); + +int rtw_vht_24g_enable = 1; /* 0:disable, 1:enable */ +module_param(rtw_vht_24g_enable, int, 0644); + +int rtw_ampdu_factor = 7; + +uint rtw_vht_rx_mcs_map = 0xaaaa; +module_param(rtw_vht_rx_mcs_map, uint, 0644); +MODULE_PARM_DESC(rtw_vht_rx_mcs_map, "VHT RX MCS map"); +#endif /* CONFIG_80211AC_VHT */ + + +/* 0: not check in watch dog, 1: check in watch dog */ +int rtw_check_hw_status = 0; + +int rtw_low_power = 0; +int rtw_wifi_spec = 0; + + +int rtw_trx_path_bmp = 0x00; +module_param(rtw_trx_path_bmp, int, 0644); /* [7:4]TX path bmp, [0:3]RX path bmp, 0: not specified */ + +#ifdef CONFIG_SPECIAL_RF_PATH /* configure Nss/xTxR IC to 1ss/1T1R */ +int rtw_tx_path_lmt = 1; +int rtw_rx_path_lmt = 1; +int rtw_tx_nss = 1; +int rtw_rx_nss = 1; +#elif defined(CONFIG_CUSTOMER01_SMART_ANTENNA) +int rtw_tx_path_lmt = 2; +int rtw_rx_path_lmt = 2; +int rtw_tx_nss = 1; +int rtw_rx_nss = 1; +#else +int rtw_tx_path_lmt = 0; +int rtw_rx_path_lmt = 0; +int rtw_tx_nss = 0; +int rtw_rx_nss = 0; +#endif +module_param(rtw_tx_path_lmt, int, 0644); /* limit of TX path number, 0: not specified */ +module_param(rtw_rx_path_lmt, int, 0644); /* limit of RX path number, 0: not specified */ +module_param(rtw_tx_nss, int, 0644); +module_param(rtw_rx_nss, int, 0644); + +#ifdef CONFIG_REGD_SRC_FROM_OS +static uint rtw_regd_src = CONFIG_RTW_REGD_SRC; +module_param(rtw_regd_src, uint, 0644); +MODULE_PARM_DESC(rtw_regd_src, "The default regd source selection, 0:Realtek defined, 1: OS"); +#endif + +char rtw_country_unspecified[] = {0xFF, 0xFF, 0x00}; +char *rtw_country_code = rtw_country_unspecified; +module_param(rtw_country_code, charp, 0644); +MODULE_PARM_DESC(rtw_country_code, "The default country code (in alpha2)"); + +int rtw_channel_plan = CONFIG_RTW_CHPLAN; +module_param(rtw_channel_plan, int, 0644); +MODULE_PARM_DESC(rtw_channel_plan, "The default chplan ID when rtw_alpha2 is not specified or valid"); + +static uint rtw_excl_chs[MAX_CHANNEL_NUM] = CONFIG_RTW_EXCL_CHS; +static int rtw_excl_chs_num = 0; +module_param_array(rtw_excl_chs, uint, &rtw_excl_chs_num, 0644); +MODULE_PARM_DESC(rtw_excl_chs, "exclusive channel array"); + +/*if concurrent softap + p2p(GO) is needed, this param lets p2p response full channel list. +But Softap must be SHUT DOWN once P2P decide to set up connection and become a GO.*/ +#ifdef CONFIG_FULL_CH_IN_P2P_HANDSHAKE + int rtw_full_ch_in_p2p_handshake = 1; /* reply full channel list*/ +#else + int rtw_full_ch_in_p2p_handshake = 0; /* reply only softap channel*/ +#endif + +#ifdef CONFIG_BT_COEXIST +int rtw_btcoex_enable = 2; +module_param(rtw_btcoex_enable, int, 0644); +MODULE_PARM_DESC(rtw_btcoex_enable, "BT co-existence on/off, 0:off, 1:on, 2:by efuse"); + +int rtw_ant_num = 0; +module_param(rtw_ant_num, int, 0644); +MODULE_PARM_DESC(rtw_ant_num, "Antenna number setting, 0:by efuse"); + +int rtw_bt_iso = 2;/* 0:Low, 1:High, 2:From Efuse */ +int rtw_bt_sco = 3;/* 0:Idle, 1:None-SCO, 2:SCO, 3:From Counter, 4.Busy, 5.OtherBusy */ +int rtw_bt_ampdu = 1 ; /* 0:Disable BT control A-MPDU, 1:Enable BT control A-MPDU. */ +#endif /* CONFIG_BT_COEXIST */ + +int rtw_AcceptAddbaReq = _TRUE;/* 0:Reject AP's Add BA req, 1:Accept AP's Add BA req. */ + +int rtw_antdiv_cfg = 2; /* 0:OFF , 1:ON, 2:decide by Efuse config */ +int rtw_antdiv_type = 0 + ; /* 0:decide by efuse 1: for 88EE, 1Tx and 1RxCG are diversity.(2 Ant with SPDT), 2: for 88EE, 1Tx and 2Rx are diversity.( 2 Ant, Tx and RxCG are both on aux port, RxCS is on main port ), 3: for 88EE, 1Tx and 1RxCG are fixed.(1Ant, Tx and RxCG are both on aux port) */ + +int rtw_drv_ant_band_switch = 1; /* 0:OFF , 1:ON, Driver control antenna band switch*/ + +int rtw_single_ant_path; /*0:main ant , 1:aux ant , Fixed single antenna path, default main ant*/ + +/* 0: doesn't switch, 1: switch from usb2.0 to usb 3.0 2: switch from usb3.0 to usb 2.0 */ +int rtw_switch_usb_mode = 0; + +#ifdef CONFIG_USB_AUTOSUSPEND +int rtw_enusbss = 1;/* 0:disable,1:enable */ +#else +int rtw_enusbss = 0;/* 0:disable,1:enable */ +#endif + +int rtw_hwpdn_mode = 2; /* 0:disable,1:enable,2: by EFUSE config */ + +#ifdef CONFIG_HW_PWRP_DETECTION +int rtw_hwpwrp_detect = 1; +#else +int rtw_hwpwrp_detect = 0; /* HW power ping detect 0:disable , 1:enable */ +#endif + +#ifdef CONFIG_USB_HCI +int rtw_hw_wps_pbc = 1; +#else +int rtw_hw_wps_pbc = 0; +#endif + +#ifdef CONFIG_80211D +int rtw_80211d = 0; +#endif + +#ifdef CONFIG_PCI_ASPM +/* CLK_REQ:BIT0 L0s:BIT1 ASPM_L1:BIT2 L1Off:BIT3*/ +int rtw_pci_aspm_enable = 0x5; +#else +int rtw_pci_aspm_enable; +#endif + +/* + * BIT [15:12] mask of ps mode + * BIT [11:8] val of ps mode + * BIT [7:4] mask of perf mode + * BIT [3:0] val of perf mode + * + * L0s:BIT[+0] L1:BIT[+1] + * + * 0x0030: change value only if perf mode + * 0x3300: change value only if ps mode + * 0x3330: change value in both perf and ps mode + */ +#ifdef CONFIG_PCI_DYNAMIC_ASPM +#ifdef CONFIG_PCI_ASPM +int rtw_pci_dynamic_aspm_linkctrl = 0x3330; +#else +int rtw_pci_dynamic_aspm_linkctrl = 0x0030; +#endif +#else +int rtw_pci_dynamic_aspm_linkctrl = 0x0000; +#endif +module_param(rtw_pci_dynamic_aspm_linkctrl, int, 0644); + +#ifdef CONFIG_QOS_OPTIMIZATION +int rtw_qos_opt_enable = 1; /* 0: disable,1:enable */ +#else +int rtw_qos_opt_enable = 0; /* 0: disable,1:enable */ +#endif +module_param(rtw_qos_opt_enable, int, 0644); + +#ifdef CONFIG_RTW_ACS +int rtw_acs_auto_scan = 0; /*0:disable, 1:enable*/ +module_param(rtw_acs_auto_scan, int, 0644); + +int rtw_acs = 1; +module_param(rtw_acs, int, 0644); +#endif + +#ifdef CONFIG_BACKGROUND_NOISE_MONITOR +int rtw_nm = 1;/*noise monitor*/ +module_param(rtw_nm, int, 0644); +#endif + +char *ifname = "wlan%d"; +module_param(ifname, charp, 0644); +MODULE_PARM_DESC(ifname, "The default name to allocate for first interface"); + +#ifdef CONFIG_PLATFORM_ANDROID + char *if2name = "p2p%d"; +#else /* CONFIG_PLATFORM_ANDROID */ + char *if2name = "wlan%d"; +#endif /* CONFIG_PLATFORM_ANDROID */ +module_param(if2name, charp, 0644); +MODULE_PARM_DESC(if2name, "The default name to allocate for second interface"); + +char *rtw_initmac = 0; /* temp mac address if users want to use instead of the mac address in Efuse */ + +#ifdef CONFIG_CONCURRENT_MODE + + #if (CONFIG_IFACE_NUMBER > 2) + int rtw_virtual_iface_num = CONFIG_IFACE_NUMBER - 1; + module_param(rtw_virtual_iface_num, int, 0644); + #else + int rtw_virtual_iface_num = 1; + #endif + +#ifdef CONFIG_P2P + + #ifdef CONFIG_SEL_P2P_IFACE + int rtw_sel_p2p_iface = CONFIG_SEL_P2P_IFACE; + #else + int rtw_sel_p2p_iface = IFACE_ID1; + #endif + + module_param(rtw_sel_p2p_iface, int, 0644); + +#endif + +#endif + +#ifdef CONFIG_AP_MODE +u8 rtw_bmc_tx_rate = MGN_UNKNOWN; + +#if CONFIG_RTW_AP_DATA_BMC_TO_UC +int rtw_ap_src_b2u_flags = CONFIG_RTW_AP_SRC_B2U_FLAGS; +module_param(rtw_ap_src_b2u_flags, int, 0644); + +int rtw_ap_fwd_b2u_flags = CONFIG_RTW_AP_FWD_B2U_FLAGS; +module_param(rtw_ap_fwd_b2u_flags, int, 0644); +#endif /* CONFIG_RTW_AP_DATA_BMC_TO_UC */ +#endif /* CONFIG_AP_MODE */ + +#ifdef CONFIG_RTW_MESH +#if CONFIG_RTW_MESH_DATA_BMC_TO_UC +int rtw_msrc_b2u_flags = CONFIG_RTW_MSRC_B2U_FLAGS; +module_param(rtw_msrc_b2u_flags, int, 0644); + +int rtw_mfwd_b2u_flags = CONFIG_RTW_MFWD_B2U_FLAGS; +module_param(rtw_mfwd_b2u_flags, int, 0644); +#endif /* CONFIG_RTW_MESH_DATA_BMC_TO_UC */ +#endif /* CONFIG_RTW_MESH */ + +#ifdef RTW_WOW_STA_MIX +int rtw_wowlan_sta_mix_mode = 1; +#else +int rtw_wowlan_sta_mix_mode = 0; +#endif +module_param(rtw_wowlan_sta_mix_mode, int, 0644); +module_param(rtw_pwrtrim_enable, int, 0644); +module_param(rtw_initmac, charp, 0644); +module_param(rtw_chip_version, int, 0644); +module_param(rtw_rfintfs, int, 0644); +module_param(rtw_lbkmode, int, 0644); +module_param(rtw_network_mode, int, 0644); +module_param(rtw_channel, int, 0644); +module_param(rtw_mp_mode, int, 0644); +module_param(rtw_wmm_enable, int, 0644); +#ifdef CONFIG_WMMPS_STA +module_param(rtw_uapsd_max_sp, int, 0644); +module_param(rtw_uapsd_ac_enable, int, 0644); +module_param(rtw_wmm_smart_ps, int, 0644); +#endif /* CONFIG_WMMPS_STA */ +module_param(rtw_vrtl_carrier_sense, int, 0644); +module_param(rtw_vcs_type, int, 0644); +module_param(rtw_busy_thresh, int, 0644); + +#ifdef CONFIG_80211N_HT +module_param(rtw_ht_enable, int, 0644); +module_param(rtw_bw_mode, int, 0644); +module_param(rtw_ampdu_enable, int, 0644); +module_param(rtw_rx_stbc, int, 0644); +module_param(rtw_rx_ampdu_amsdu, int, 0644); +module_param(rtw_tx_ampdu_amsdu, int, 0644); +module_param(rtw_quick_addba_req, int, 0644); +#endif /* CONFIG_80211N_HT */ + +#ifdef CONFIG_BEAMFORMING +module_param(rtw_beamform_cap, int, 0644); +#endif + +module_param(rtw_power_mgnt, int, 0644); +module_param(rtw_smart_ps, int, 0644); +module_param(rtw_low_power, int, 0644); +module_param(rtw_wifi_spec, int, 0644); + +module_param(rtw_full_ch_in_p2p_handshake, int, 0644); +module_param(rtw_antdiv_cfg, int, 0644); +module_param(rtw_antdiv_type, int, 0644); + +module_param(rtw_drv_ant_band_switch, int, 0644); +module_param(rtw_single_ant_path, int, 0644); + +module_param(rtw_switch_usb_mode, int, 0644); + +module_param(rtw_enusbss, int, 0644); +module_param(rtw_hwpdn_mode, int, 0644); +module_param(rtw_hwpwrp_detect, int, 0644); + +module_param(rtw_hw_wps_pbc, int, 0644); +module_param(rtw_check_hw_status, int, 0644); + +#ifdef CONFIG_PCI_HCI +module_param(rtw_pci_aspm_enable, int, 0644); +#endif + +#ifdef CONFIG_TX_EARLY_MODE +module_param(rtw_early_mode, int, 0644); +#endif + +#ifdef CONFIG_RTW_SW_LED +module_param(rtw_led_ctrl, int, 0644); +MODULE_PARM_DESC(rtw_led_ctrl,"LED control, 0:Always off, 1:Normal blink, 2:Always on"); +#endif + +#ifdef CONFIG_ADAPTOR_INFO_CACHING_FILE +char *rtw_adaptor_info_caching_file_path = "/data/misc/wifi/rtw_cache"; +module_param(rtw_adaptor_info_caching_file_path, charp, 0644); +MODULE_PARM_DESC(rtw_adaptor_info_caching_file_path, "The path of adapter info cache file"); +#endif /* CONFIG_ADAPTOR_INFO_CACHING_FILE */ + +#ifdef CONFIG_LAYER2_ROAMING +uint rtw_max_roaming_times = 2; +module_param(rtw_max_roaming_times, uint, 0644); +MODULE_PARM_DESC(rtw_max_roaming_times, "The max roaming times to try"); +#endif /* CONFIG_LAYER2_ROAMING */ + +#ifdef CONFIG_IOL +int rtw_fw_iol = 1; +module_param(rtw_fw_iol, int, 0644); +MODULE_PARM_DESC(rtw_fw_iol, "FW IOL. 0:Disable, 1:enable, 2:by usb speed"); +#endif /* CONFIG_IOL */ + +#ifdef CONFIG_FILE_FWIMG +char *rtw_fw_file_path = "/system/etc/firmware/rtlwifi/FW_NIC.BIN"; +module_param(rtw_fw_file_path, charp, 0644); +MODULE_PARM_DESC(rtw_fw_file_path, "The path of fw image"); + +char *rtw_fw_wow_file_path = "/system/etc/firmware/rtlwifi/FW_WoWLAN.BIN"; +module_param(rtw_fw_wow_file_path, charp, 0644); +MODULE_PARM_DESC(rtw_fw_wow_file_path, "The path of fw for Wake on Wireless image"); + +#ifdef CONFIG_MP_INCLUDED +char *rtw_fw_mp_bt_file_path = ""; +module_param(rtw_fw_mp_bt_file_path, charp, 0644); +MODULE_PARM_DESC(rtw_fw_mp_bt_file_path, "The path of fw for MP-BT image"); +#endif /* CONFIG_MP_INCLUDED */ +#endif /* CONFIG_FILE_FWIMG */ + +#ifdef CONFIG_80211D +module_param(rtw_80211d, int, 0644); +MODULE_PARM_DESC(rtw_80211d, "Enable 802.11d mechanism"); +#endif + +#ifdef CONFIG_ADVANCE_OTA +/* BIT(0): OTA continuous rotated test within low RSSI,1R CCA in path B + BIT(1) & BIT(2): OTA continuous rotated test with low high RSSI */ +/* Experimental environment: shielding room with half of absorber and 2~3 rotation per minute */ +int rtw_advnace_ota; +module_param(rtw_advnace_ota, int, 0644); +#endif + +uint rtw_notch_filter = RTW_NOTCH_FILTER; +module_param(rtw_notch_filter, uint, 0644); +MODULE_PARM_DESC(rtw_notch_filter, "0:Disable, 1:Enable, 2:Enable only for P2P"); + +uint rtw_hiq_filter = CONFIG_RTW_HIQ_FILTER; +module_param(rtw_hiq_filter, uint, 0644); +MODULE_PARM_DESC(rtw_hiq_filter, "0:allow all, 1:allow special, 2:deny all"); + +uint rtw_adaptivity_en = CONFIG_RTW_ADAPTIVITY_EN; +module_param(rtw_adaptivity_en, uint, 0644); +MODULE_PARM_DESC(rtw_adaptivity_en, "0:disable, 1:enable"); + +uint rtw_adaptivity_mode = CONFIG_RTW_ADAPTIVITY_MODE; +module_param(rtw_adaptivity_mode, uint, 0644); +MODULE_PARM_DESC(rtw_adaptivity_mode, "0:normal, 1:carrier sense"); + +int rtw_adaptivity_th_l2h_ini = CONFIG_RTW_ADAPTIVITY_TH_L2H_INI; +module_param(rtw_adaptivity_th_l2h_ini, int, 0644); +MODULE_PARM_DESC(rtw_adaptivity_th_l2h_ini, "th_l2h_ini for Adaptivity"); + +int rtw_adaptivity_th_edcca_hl_diff = CONFIG_RTW_ADAPTIVITY_TH_EDCCA_HL_DIFF; +module_param(rtw_adaptivity_th_edcca_hl_diff, int, 0644); +MODULE_PARM_DESC(rtw_adaptivity_th_edcca_hl_diff, "th_edcca_hl_diff for Adaptivity"); + +#ifdef CONFIG_DFS_MASTER +uint rtw_dfs_region_domain = CONFIG_RTW_DFS_REGION_DOMAIN; +module_param(rtw_dfs_region_domain, uint, 0644); +MODULE_PARM_DESC(rtw_dfs_region_domain, "0:NONE, 1:FCC, 2:MKK, 3:ETSI"); +#endif + +uint rtw_amsdu_mode = RTW_AMSDU_MODE; +module_param(rtw_amsdu_mode, uint, 0644); +MODULE_PARM_DESC(rtw_amsdu_mode, "0:non-spp, 1:spp, 2:all drop"); + +uint rtw_amplifier_type_2g = CONFIG_RTW_AMPLIFIER_TYPE_2G; +module_param(rtw_amplifier_type_2g, uint, 0644); +MODULE_PARM_DESC(rtw_amplifier_type_2g, "BIT3:2G ext-PA, BIT4:2G ext-LNA"); + +uint rtw_amplifier_type_5g = CONFIG_RTW_AMPLIFIER_TYPE_5G; +module_param(rtw_amplifier_type_5g, uint, 0644); +MODULE_PARM_DESC(rtw_amplifier_type_5g, "BIT6:5G ext-PA, BIT7:5G ext-LNA"); + +uint rtw_RFE_type = CONFIG_RTW_RFE_TYPE; +module_param(rtw_RFE_type, uint, 0644); +MODULE_PARM_DESC(rtw_RFE_type, "default init value:64"); + +uint rtw_powertracking_type = 64; +module_param(rtw_powertracking_type, uint, 0644); +MODULE_PARM_DESC(rtw_powertracking_type, "default init value:64"); + +uint rtw_GLNA_type = CONFIG_RTW_GLNA_TYPE; +module_param(rtw_GLNA_type, uint, 0644); +MODULE_PARM_DESC(rtw_GLNA_type, "default init value:0"); + +uint rtw_TxBBSwing_2G = 0xFF; +module_param(rtw_TxBBSwing_2G, uint, 0644); +MODULE_PARM_DESC(rtw_TxBBSwing_2G, "default init value:0xFF"); + +uint rtw_TxBBSwing_5G = 0xFF; +module_param(rtw_TxBBSwing_5G, uint, 0644); +MODULE_PARM_DESC(rtw_TxBBSwing_5G, "default init value:0xFF"); + +uint rtw_OffEfuseMask = 0; +module_param(rtw_OffEfuseMask, uint, 0644); +MODULE_PARM_DESC(rtw_OffEfuseMask, "default open Efuse Mask value:0"); + +uint rtw_FileMaskEfuse = 0; +module_param(rtw_FileMaskEfuse, uint, 0644); +MODULE_PARM_DESC(rtw_FileMaskEfuse, "default drv Mask Efuse value:0"); + +uint rtw_rxgain_offset_2g = 0; +module_param(rtw_rxgain_offset_2g, uint, 0644); +MODULE_PARM_DESC(rtw_rxgain_offset_2g, "default RF Gain 2G Offset value:0"); + +uint rtw_rxgain_offset_5gl = 0; +module_param(rtw_rxgain_offset_5gl, uint, 0644); +MODULE_PARM_DESC(rtw_rxgain_offset_5gl, "default RF Gain 5GL Offset value:0"); + +uint rtw_rxgain_offset_5gm = 0; +module_param(rtw_rxgain_offset_5gm, uint, 0644); +MODULE_PARM_DESC(rtw_rxgain_offset_5gm, "default RF Gain 5GM Offset value:0"); + +uint rtw_rxgain_offset_5gh = 0; +module_param(rtw_rxgain_offset_5gh, uint, 0644); +MODULE_PARM_DESC(rtw_rxgain_offset_5gm, "default RF Gain 5GL Offset value:0"); + +uint rtw_pll_ref_clk_sel = CONFIG_RTW_PLL_REF_CLK_SEL; +module_param(rtw_pll_ref_clk_sel, uint, 0644); +MODULE_PARM_DESC(rtw_pll_ref_clk_sel, "force pll_ref_clk_sel, 0xF:use autoload value"); + +int rtw_tx_pwr_by_rate = CONFIG_TXPWR_BY_RATE_EN; +module_param(rtw_tx_pwr_by_rate, int, 0644); +MODULE_PARM_DESC(rtw_tx_pwr_by_rate, "0:Disable, 1:Enable, 2: Depend on efuse"); + +#if CONFIG_TXPWR_LIMIT +int rtw_tx_pwr_lmt_enable = CONFIG_TXPWR_LIMIT_EN; +module_param(rtw_tx_pwr_lmt_enable, int, 0644); +MODULE_PARM_DESC(rtw_tx_pwr_lmt_enable, "0:Disable, 1:Enable, 2: Depend on efuse"); +#endif + +static int rtw_target_tx_pwr_2g_a[RATE_SECTION_NUM] = CONFIG_RTW_TARGET_TX_PWR_2G_A; +static int rtw_target_tx_pwr_2g_a_num = 0; +module_param_array(rtw_target_tx_pwr_2g_a, int, &rtw_target_tx_pwr_2g_a_num, 0644); +MODULE_PARM_DESC(rtw_target_tx_pwr_2g_a, "2.4G target tx power (unit:dBm) of RF path A for each rate section, should match the real calibrate power, -1: undefined"); + +static int rtw_target_tx_pwr_2g_b[RATE_SECTION_NUM] = CONFIG_RTW_TARGET_TX_PWR_2G_B; +static int rtw_target_tx_pwr_2g_b_num = 0; +module_param_array(rtw_target_tx_pwr_2g_b, int, &rtw_target_tx_pwr_2g_b_num, 0644); +MODULE_PARM_DESC(rtw_target_tx_pwr_2g_b, "2.4G target tx power (unit:dBm) of RF path B for each rate section, should match the real calibrate power, -1: undefined"); + +static int rtw_target_tx_pwr_2g_c[RATE_SECTION_NUM] = CONFIG_RTW_TARGET_TX_PWR_2G_C; +static int rtw_target_tx_pwr_2g_c_num = 0; +module_param_array(rtw_target_tx_pwr_2g_c, int, &rtw_target_tx_pwr_2g_c_num, 0644); +MODULE_PARM_DESC(rtw_target_tx_pwr_2g_c, "2.4G target tx power (unit:dBm) of RF path C for each rate section, should match the real calibrate power, -1: undefined"); + +static int rtw_target_tx_pwr_2g_d[RATE_SECTION_NUM] = CONFIG_RTW_TARGET_TX_PWR_2G_D; +static int rtw_target_tx_pwr_2g_d_num = 0; +module_param_array(rtw_target_tx_pwr_2g_d, int, &rtw_target_tx_pwr_2g_d_num, 0644); +MODULE_PARM_DESC(rtw_target_tx_pwr_2g_d, "2.4G target tx power (unit:dBm) of RF path D for each rate section, should match the real calibrate power, -1: undefined"); + +#if CONFIG_IEEE80211_BAND_5GHZ +static int rtw_target_tx_pwr_5g_a[RATE_SECTION_NUM - 1] = CONFIG_RTW_TARGET_TX_PWR_5G_A; +static int rtw_target_tx_pwr_5g_a_num = 0; +module_param_array(rtw_target_tx_pwr_5g_a, int, &rtw_target_tx_pwr_5g_a_num, 0644); +MODULE_PARM_DESC(rtw_target_tx_pwr_5g_a, "5G target tx power (unit:dBm) of RF path A for each rate section, should match the real calibrate power, -1: undefined"); + +static int rtw_target_tx_pwr_5g_b[RATE_SECTION_NUM - 1] = CONFIG_RTW_TARGET_TX_PWR_5G_B; +static int rtw_target_tx_pwr_5g_b_num = 0; +module_param_array(rtw_target_tx_pwr_5g_b, int, &rtw_target_tx_pwr_5g_b_num, 0644); +MODULE_PARM_DESC(rtw_target_tx_pwr_5g_b, "5G target tx power (unit:dBm) of RF path B for each rate section, should match the real calibrate power, -1: undefined"); + +static int rtw_target_tx_pwr_5g_c[RATE_SECTION_NUM - 1] = CONFIG_RTW_TARGET_TX_PWR_5G_C; +static int rtw_target_tx_pwr_5g_c_num = 0; +module_param_array(rtw_target_tx_pwr_5g_c, int, &rtw_target_tx_pwr_5g_c_num, 0644); +MODULE_PARM_DESC(rtw_target_tx_pwr_5g_c, "5G target tx power (unit:dBm) of RF path C for each rate section, should match the real calibrate power, -1: undefined"); + +static int rtw_target_tx_pwr_5g_d[RATE_SECTION_NUM - 1] = CONFIG_RTW_TARGET_TX_PWR_5G_D; +static int rtw_target_tx_pwr_5g_d_num = 0; +module_param_array(rtw_target_tx_pwr_5g_d, int, &rtw_target_tx_pwr_5g_d_num, 0644); +MODULE_PARM_DESC(rtw_target_tx_pwr_5g_d, "5G target tx power (unit:dBm) of RF path D for each rate section, should match the real calibrate power, -1: undefined"); +#endif /* CONFIG_IEEE80211_BAND_5GHZ */ + +int rtw_antenna_gain = CONFIG_RTW_ANTENNA_GAIN; +module_param(rtw_antenna_gain, int, 0644); +MODULE_PARM_DESC(rtw_antenna_gain, "Antenna gain in mBi. 0x7FFF: unspecifed"); + +#ifdef CONFIG_RTW_TX_NPATH_EN +/*0:disable ,1: 2path*/ +int rtw_tx_npath_enable = 1; +module_param(rtw_tx_npath_enable, int, 0644); +MODULE_PARM_DESC(rtw_tx_npath_enable, "0:Disable, 1:TX-2PATH"); +#endif + +#ifdef CONFIG_RTW_PATH_DIV +/*0:disable ,1: path diversity*/ +int rtw_path_div_enable = 1; +module_param(rtw_path_div_enable, int, 0644); +MODULE_PARM_DESC(rtw_path_div_enable, "0:Disable, 1:Enable path diversity"); +#endif + + +int rtw_tsf_update_pause_factor = CONFIG_TSF_UPDATE_PAUSE_FACTOR; +module_param(rtw_tsf_update_pause_factor, int, 0644); +MODULE_PARM_DESC(rtw_tsf_update_pause_factor, "num of bcn intervals to stay TSF update pause status"); + +int rtw_tsf_update_restore_factor = CONFIG_TSF_UPDATE_RESTORE_FACTOR; +module_param(rtw_tsf_update_restore_factor, int, 0644); +MODULE_PARM_DESC(rtw_tsf_update_restore_factor, "num of bcn intervals to stay TSF update restore status"); + +#ifdef CONFIG_LOAD_PHY_PARA_FROM_FILE +char *rtw_phy_file_path = REALTEK_CONFIG_PATH; +module_param(rtw_phy_file_path, charp, 0644); +MODULE_PARM_DESC(rtw_phy_file_path, "The path of phy parameter"); +/* PHY FILE Bit Map +* BIT0 - MAC, 0: non-support, 1: support +* BIT1 - BB, 0: non-support, 1: support +* BIT2 - BB_PG, 0: non-support, 1: support +* BIT3 - BB_MP, 0: non-support, 1: support +* BIT4 - RF, 0: non-support, 1: support +* BIT5 - RF_TXPWR_TRACK, 0: non-support, 1: support +* BIT6 - RF_TXPWR_LMT, 0: non-support, 1: support */ +int rtw_load_phy_file = (BIT2 | BIT6); +module_param(rtw_load_phy_file, int, 0644); +MODULE_PARM_DESC(rtw_load_phy_file, "PHY File Bit Map"); +int rtw_decrypt_phy_file = 0; +module_param(rtw_decrypt_phy_file, int, 0644); +MODULE_PARM_DESC(rtw_decrypt_phy_file, "Enable Decrypt PHY File"); +#endif + +uint rtw_recvbuf_nr = NR_RECVBUFF; +module_param(rtw_recvbuf_nr, int, 0644); +MODULE_PARM_DESC(rtw_recvbuf_nr, "Preallocated number of struct recv_buf"); + +#ifdef CONFIG_SUPPORT_TRX_SHARED +#ifdef DFT_TRX_SHARE_MODE +int rtw_trx_share_mode = DFT_TRX_SHARE_MODE; +#else +int rtw_trx_share_mode = 0; +#endif +module_param(rtw_trx_share_mode, int, 0644); +MODULE_PARM_DESC(rtw_trx_share_mode, "TRx FIFO Shared"); +#endif + +#ifdef CONFIG_DYNAMIC_SOML +uint rtw_dynamic_soml_en = 1; +module_param(rtw_dynamic_soml_en, int, 0644); +MODULE_PARM_DESC(rtw_dynamic_soml_en, "0: disable, 1: enable with default param, 2: enable with specified param."); + +uint rtw_dynamic_soml_train_num = 0; +module_param(rtw_dynamic_soml_train_num, int, 0644); +MODULE_PARM_DESC(rtw_dynamic_soml_train_num, "SOML training number"); + +uint rtw_dynamic_soml_interval = 0; +module_param(rtw_dynamic_soml_interval, int, 0644); +MODULE_PARM_DESC(rtw_dynamic_soml_interval, "SOML training interval"); + +uint rtw_dynamic_soml_period = 0; +module_param(rtw_dynamic_soml_period, int, 0644); +MODULE_PARM_DESC(rtw_dynamic_soml_period, "SOML training period"); + +uint rtw_dynamic_soml_delay = 0; +module_param(rtw_dynamic_soml_delay, int, 0644); +MODULE_PARM_DESC(rtw_dynamic_soml_delay, "SOML training delay"); +#endif + +uint rtw_phydm_ability = 0xffffffff; +module_param(rtw_phydm_ability, uint, 0644); + +uint rtw_halrf_ability = 0xffffffff; +module_param(rtw_halrf_ability, uint, 0644); + +#ifdef CONFIG_RTW_MESH +uint rtw_peer_alive_based_preq = 1; +module_param(rtw_peer_alive_based_preq, uint, 0644); +MODULE_PARM_DESC(rtw_peer_alive_based_preq, + "On demand PREQ will reference peer alive status. 0: Off, 1: On"); +#endif + +int _netdev_open(struct net_device *pnetdev); +int netdev_open(struct net_device *pnetdev); +static int netdev_close(struct net_device *pnetdev); +#ifdef CONFIG_PLATFORM_INTEL_BYT +extern int rtw_sdio_set_power(int on); +#endif /* CONFIG_PLATFORM_INTEL_BYT */ + +#ifdef CONFIG_MCC_MODE +/* enable MCC mode or not */ +int rtw_en_mcc = 1; +/* can referece following value before insmod driver */ +int rtw_mcc_ap_bw20_target_tx_tp = MCC_AP_BW20_TARGET_TX_TP; +int rtw_mcc_ap_bw40_target_tx_tp = MCC_AP_BW40_TARGET_TX_TP; +int rtw_mcc_ap_bw80_target_tx_tp = MCC_AP_BW80_TARGET_TX_TP; +int rtw_mcc_sta_bw20_target_tx_tp = MCC_STA_BW20_TARGET_TX_TP; +int rtw_mcc_sta_bw40_target_tx_tp = MCC_STA_BW40_TARGET_TX_TP; +int rtw_mcc_sta_bw80_target_tx_tp = MCC_STA_BW80_TARGET_TX_TP; +int rtw_mcc_single_tx_cri = MCC_SINGLE_TX_CRITERIA; +int rtw_mcc_policy_table_idx = 0; +int rtw_mcc_duration = 0; +int rtw_mcc_enable_runtime_duration = 1; +#ifdef CONFIG_MCC_PHYDM_OFFLOAD +int rtw_mcc_phydm_offload = 1; +#else +int rtw_mcc_phydm_offload = 0; +#endif +module_param(rtw_en_mcc, int, 0644); +module_param(rtw_mcc_single_tx_cri, int, 0644); +module_param(rtw_mcc_ap_bw20_target_tx_tp, int, 0644); +module_param(rtw_mcc_ap_bw40_target_tx_tp, int, 0644); +module_param(rtw_mcc_ap_bw80_target_tx_tp, int, 0644); +module_param(rtw_mcc_sta_bw20_target_tx_tp, int, 0644); +module_param(rtw_mcc_sta_bw40_target_tx_tp, int, 0644); +module_param(rtw_mcc_sta_bw80_target_tx_tp, int, 0644); +module_param(rtw_mcc_policy_table_idx, int, 0644); +module_param(rtw_mcc_duration, int, 0644); +module_param(rtw_mcc_phydm_offload, int, 0644); +#endif /*CONFIG_MCC_MODE */ + +#ifdef CONFIG_RTW_NAPI +/*following setting should define NAPI in Makefile +enable napi only = 1, disable napi = 0*/ +int rtw_en_napi = 1; +module_param(rtw_en_napi, int, 0644); +#ifdef CONFIG_RTW_NAPI_DYNAMIC +int rtw_napi_threshold = 100; /* unit: Mbps */ +module_param(rtw_napi_threshold, int, 0644); +#endif /* CONFIG_RTW_NAPI_DYNAMIC */ +#ifdef CONFIG_RTW_GRO +/*following setting should define GRO in Makefile +enable gro = 1, disable gro = 0*/ +int rtw_en_gro = 1; +module_param(rtw_en_gro, int, 0644); +#endif /* CONFIG_RTW_GRO */ +#endif /* CONFIG_RTW_NAPI */ + +#ifdef RTW_IQK_FW_OFFLOAD +int rtw_iqk_fw_offload = 1; +#else +int rtw_iqk_fw_offload; +#endif /* RTW_IQK_FW_OFFLOAD */ +module_param(rtw_iqk_fw_offload, int, 0644); + +#ifdef RTW_CHANNEL_SWITCH_OFFLOAD +int rtw_ch_switch_offload = 0; +#else +int rtw_ch_switch_offload; +#endif /* RTW_CHANNEL_SWITCH_OFFLOAD */ +module_param(rtw_ch_switch_offload, int, 0644); + +#ifdef CONFIG_TDLS +int rtw_en_tdls = 1; +module_param(rtw_en_tdls, int, 0644); +#endif + +#ifdef CONFIG_FW_OFFLOAD_PARAM_INIT +int rtw_fw_param_init = 1; +module_param(rtw_fw_param_init, int, 0644); +#endif + +#ifdef CONFIG_TDMADIG +int rtw_tdmadig_en = 1; +/* +1:MODE_PERFORMANCE +2:MODE_COVERAGE +*/ +int rtw_tdmadig_mode = 1; +int rtw_dynamic_tdmadig = 0; +module_param(rtw_tdmadig_en, int, 0644); +module_param(rtw_tdmadig_mode, int, 0644); +module_param(rtw_dynamic_tdmadig, int, 0644); +#endif/*CONFIG_TDMADIG*/ + +/*dynamic RRSR default enable*/ +int rtw_en_dyn_rrsr = 1; +int rtw_rrsr_value = 0xFFFFFFFF; +module_param(rtw_en_dyn_rrsr, int, 0644); +module_param(rtw_rrsr_value, int, 0644); + +#ifdef CONFIG_WOWLAN +/* + * 0: disable, 1: enable + */ +uint rtw_wow_enable = 1; +module_param(rtw_wow_enable, uint, 0644); +/* + * bit[0]: magic packet wake up + * bit[1]: unucast packet(HW/FW unuicast) + * bit[2]: deauth wake up + */ +uint rtw_wakeup_event = RTW_WAKEUP_EVENT; +module_param(rtw_wakeup_event, uint, 0644); +/* + * 0: common WOWLAN + * bit[0]: disable BB RF + * bit[1]: For wireless remote controller with or without connection + */ +uint rtw_suspend_type = RTW_SUSPEND_TYPE; +module_param(rtw_suspend_type, uint, 0644); +#endif + +#ifdef RTW_BUSY_DENY_SCAN +uint rtw_scan_interval_thr = BUSY_TRAFFIC_SCAN_DENY_PERIOD; +module_param(rtw_scan_interval_thr, uint, 0644); +MODULE_PARM_DESC(rtw_scan_interval_thr, "Threshold used to judge if scan " \ + "request comes from scan UI, unit is ms."); +#endif /* RTW_BUSY_DENY_SCAN */ + +#ifdef CONFIG_RTL8822C_XCAP_NEW_POLICY +uint rtw_8822c_xcap_overwrite = 1; +module_param(rtw_8822c_xcap_overwrite, uint, 0644); +#endif + +#ifdef CONFIG_RTW_MULTI_AP +static int rtw_unassoc_sta_mode_of_stype[UNASOC_STA_SRC_NUM] = CONFIG_RTW_UNASOC_STA_MODE_OF_STYPE; +static int rtw_unassoc_sta_mode_of_stype_num = 0; +module_param_array(rtw_unassoc_sta_mode_of_stype, int, &rtw_unassoc_sta_mode_of_stype_num, 0644); + +uint rtw_max_unassoc_sta_cnt = 0; +module_param(rtw_max_unassoc_sta_cnt, uint, 0644); +#endif + +#if CONFIG_TX_AC_LIFETIME +static void rtw_regsty_load_tx_ac_lifetime(struct registry_priv *regsty) +{ + int i, j; + struct tx_aclt_conf_t *conf; + uint *parm; + + regsty->tx_aclt_flags = (u8)rtw_tx_aclt_flags; + + for (i = 0; i < TX_ACLT_CONF_NUM; i++) { + conf = ®sty->tx_aclt_confs[i]; + if (i == TX_ACLT_CONF_DEFAULT) + parm = rtw_tx_aclt_conf_default; + #ifdef CONFIG_AP_MODE + #if CONFIG_RTW_AP_DATA_BMC_TO_UC + else if (i == TX_ACLT_CONF_AP_M2U) + parm = rtw_tx_aclt_conf_ap_m2u; + #endif + #endif /* CONFIG_AP_MODE */ + #ifdef CONFIG_RTW_MESH + else if (i == TX_ACLT_CONF_MESH) + parm = rtw_tx_aclt_conf_mesh; + #endif + else + parm = NULL; + + if (parm) { + conf->en = parm[0] & 0xF; + conf->vo_vi = parm[1]; + conf->be_bk = parm[2]; + } + } +} +#endif + +void rtw_regsty_load_target_tx_power(struct registry_priv *regsty) +{ + int path, rs; + int *target_tx_pwr; + + for (path = RF_PATH_A; path < RF_PATH_MAX; path++) { + if (path == RF_PATH_A) + target_tx_pwr = rtw_target_tx_pwr_2g_a; + else if (path == RF_PATH_B) + target_tx_pwr = rtw_target_tx_pwr_2g_b; + else if (path == RF_PATH_C) + target_tx_pwr = rtw_target_tx_pwr_2g_c; + else if (path == RF_PATH_D) + target_tx_pwr = rtw_target_tx_pwr_2g_d; + + for (rs = CCK; rs < RATE_SECTION_NUM; rs++) + regsty->target_tx_pwr_2g[path][rs] = target_tx_pwr[rs]; + } + +#if CONFIG_IEEE80211_BAND_5GHZ + for (path = RF_PATH_A; path < RF_PATH_MAX; path++) { + if (path == RF_PATH_A) + target_tx_pwr = rtw_target_tx_pwr_5g_a; + else if (path == RF_PATH_B) + target_tx_pwr = rtw_target_tx_pwr_5g_b; + else if (path == RF_PATH_C) + target_tx_pwr = rtw_target_tx_pwr_5g_c; + else if (path == RF_PATH_D) + target_tx_pwr = rtw_target_tx_pwr_5g_d; + + for (rs = OFDM; rs < RATE_SECTION_NUM; rs++) + regsty->target_tx_pwr_5g[path][rs - 1] = target_tx_pwr[rs - 1]; + } +#endif /* CONFIG_IEEE80211_BAND_5GHZ */ +} + +inline void rtw_regsty_load_excl_chs(struct registry_priv *regsty) +{ + int i; + int ch_num = 0; + + for (i = 0; i < MAX_CHANNEL_NUM; i++) + if (((u8)rtw_excl_chs[i]) != 0) + regsty->excl_chs[ch_num++] = (u8)rtw_excl_chs[i]; + + if (ch_num < MAX_CHANNEL_NUM) + regsty->excl_chs[ch_num] = 0; +} + +#ifdef CONFIG_80211N_HT +inline void rtw_regsty_init_rx_ampdu_sz_limit(struct registry_priv *regsty) +{ + int i, j; + uint *sz_limit; + + for (i = 0; i < 4; i++) { + if (i == 0) + sz_limit = rtw_rx_ampdu_sz_limit_1ss; + else if (i == 1) + sz_limit = rtw_rx_ampdu_sz_limit_2ss; + else if (i == 2) + sz_limit = rtw_rx_ampdu_sz_limit_3ss; + else if (i == 3) + sz_limit = rtw_rx_ampdu_sz_limit_4ss; + + for (j = 0; j < 4; j++) + regsty->rx_ampdu_sz_limit_by_nss_bw[i][j] = sz_limit[j]; + } +} +#endif /* CONFIG_80211N_HT */ + +#ifdef CONFIG_RTW_MULTI_AP +inline void rtw_regsty_init_unassoc_sta_param(struct registry_priv *regsty) +{ + int i; + + for (i = 0; i < UNASOC_STA_SRC_NUM; i++) + regsty->unassoc_sta_mode_of_stype[i] = rtw_unassoc_sta_mode_of_stype[i]; + + regsty->max_unassoc_sta_cnt = (u16) rtw_max_unassoc_sta_cnt; +} +#endif + +uint loadparam(_adapter *padapter) +{ + uint status = _SUCCESS; + struct registry_priv *registry_par = &padapter->registrypriv; + + +#ifdef CONFIG_RTW_DEBUG + if (rtw_drv_log_level >= _DRV_MAX_) + rtw_drv_log_level = _DRV_DEBUG_; +#endif + + registry_par->chip_version = (u8)rtw_chip_version; + registry_par->rfintfs = (u8)rtw_rfintfs; + registry_par->lbkmode = (u8)rtw_lbkmode; + /* registry_par->hci = (u8)hci; */ + registry_par->network_mode = (u8)rtw_network_mode; + + _rtw_memcpy(registry_par->ssid.Ssid, "ANY", 3); + registry_par->ssid.SsidLength = 3; + + registry_par->channel = (u8)rtw_channel; +#ifdef CONFIG_NARROWBAND_SUPPORTING + if (rtw_nb_config != RTW_NB_CONFIG_NONE) + rtw_wireless_mode &= ~WIRELESS_11B; +#endif + registry_par->wireless_mode = (u8)rtw_wireless_mode; + + if (IsSupported24G(registry_par->wireless_mode) && (!is_supported_5g(registry_par->wireless_mode)) + && (registry_par->channel > 14)) + registry_par->channel = 1; + else if (is_supported_5g(registry_par->wireless_mode) && (!IsSupported24G(registry_par->wireless_mode)) + && (registry_par->channel <= 14)) + registry_par->channel = 36; + + registry_par->vrtl_carrier_sense = (u8)rtw_vrtl_carrier_sense ; + registry_par->vcs_type = (u8)rtw_vcs_type; + registry_par->rts_thresh = (u16)rtw_rts_thresh; + registry_par->frag_thresh = (u16)rtw_frag_thresh; + registry_par->preamble = (u8)rtw_preamble; + registry_par->scan_mode = (u8)rtw_scan_mode; + registry_par->smart_ps = (u8)rtw_smart_ps; + registry_par->check_fw_ps = (u8)rtw_check_fw_ps; + #ifdef CONFIG_TDMADIG + registry_par->tdmadig_en = (u8)rtw_tdmadig_en; + registry_par->tdmadig_mode = (u8)rtw_tdmadig_mode; + registry_par->tdmadig_dynamic = (u8) rtw_dynamic_tdmadig; + registry_par->power_mgnt = PS_MODE_ACTIVE; + registry_par->ips_mode = IPS_NONE; + #else + registry_par->power_mgnt = (u8)rtw_power_mgnt; + registry_par->ips_mode = (u8)rtw_ips_mode; + #endif/*CONFIG_TDMADIG*/ + registry_par->lps_level = (u8)rtw_lps_level; + registry_par->en_dyn_rrsr = (u8)rtw_en_dyn_rrsr; + registry_par->set_rrsr_value = (u32)rtw_rrsr_value; +#ifdef CONFIG_LPS_1T1R + registry_par->lps_1t1r = (u8)(rtw_lps_1t1r ? 1 : 0); +#endif + registry_par->lps_chk_by_tp = (u8)rtw_lps_chk_by_tp; +#ifdef CONFIG_WOWLAN + registry_par->wow_power_mgnt = (u8)rtw_wow_power_mgnt; + registry_par->wow_lps_level = (u8)rtw_wow_lps_level; + #ifdef CONFIG_LPS_1T1R + registry_par->wow_lps_1t1r = (u8)(rtw_wow_lps_1t1r ? 1 : 0); + #endif +#endif /* CONFIG_WOWLAN */ + registry_par->radio_enable = (u8)rtw_radio_enable; + registry_par->long_retry_lmt = (u8)rtw_long_retry_lmt; + registry_par->short_retry_lmt = (u8)rtw_short_retry_lmt; + registry_par->busy_thresh = (u16)rtw_busy_thresh; + registry_par->max_bss_cnt = (u16)rtw_max_bss_cnt; + /* registry_par->qos_enable = (u8)rtw_qos_enable; */ + registry_par->ack_policy = (u8)rtw_ack_policy; + registry_par->mp_mode = (u8)rtw_mp_mode; +#if defined(CONFIG_MP_INCLUDED) && defined(CONFIG_RTW_CUSTOMER_STR) + registry_par->mp_customer_str = (u8)rtw_mp_customer_str; +#endif + registry_par->software_encrypt = (u8)rtw_software_encrypt; + registry_par->software_decrypt = (u8)rtw_software_decrypt; + + registry_par->acm_method = (u8)rtw_acm_method; + registry_par->usb_rxagg_mode = (u8)rtw_usb_rxagg_mode; + registry_par->dynamic_agg_enable = (u8)rtw_dynamic_agg_enable; + + /* WMM */ + registry_par->wmm_enable = (u8)rtw_wmm_enable; + +#ifdef CONFIG_WMMPS_STA + /* UAPSD */ + registry_par->uapsd_max_sp_len= (u8)rtw_uapsd_max_sp; + registry_par->uapsd_ac_enable = (u8)rtw_uapsd_ac_enable; + registry_par->wmm_smart_ps = (u8)rtw_wmm_smart_ps; +#endif /* CONFIG_WMMPS_STA */ + + registry_par->RegPwrTrimEnable = (u8)rtw_pwrtrim_enable; + +#if CONFIG_TX_AC_LIFETIME + rtw_regsty_load_tx_ac_lifetime(registry_par); +#endif + + registry_par->tx_bw_mode = (u8)rtw_tx_bw_mode; + +#ifdef CONFIG_80211N_HT + registry_par->ht_enable = (u8)rtw_ht_enable; + if (registry_par->ht_enable && is_supported_ht(registry_par->wireless_mode)) { +#ifdef CONFIG_NARROWBAND_SUPPORTING + if (rtw_nb_config != RTW_NB_CONFIG_NONE) + rtw_bw_mode = 0; +#endif + registry_par->bw_mode = (u8)rtw_bw_mode; + registry_par->ampdu_enable = (u8)rtw_ampdu_enable; + registry_par->rx_stbc = (u8)rtw_rx_stbc; + registry_par->rx_ampdu_amsdu = (u8)rtw_rx_ampdu_amsdu; + registry_par->tx_ampdu_amsdu = (u8)rtw_tx_ampdu_amsdu; + registry_par->tx_quick_addba_req = (u8)rtw_quick_addba_req; + registry_par->short_gi = (u8)rtw_short_gi; + registry_par->ldpc_cap = (u8)rtw_ldpc_cap; +#if defined(CONFIG_CUSTOMER01_SMART_ANTENNA) + rtw_stbc_cap = 0x0; +#endif +#ifdef CONFIG_RTW_TX_NPATH_EN + registry_par->tx_npath = (u8)rtw_tx_npath_enable; +#endif +#ifdef CONFIG_RTW_PATH_DIV + registry_par->path_div = (u8)rtw_path_div_enable; +#endif + registry_par->stbc_cap = (u8)rtw_stbc_cap; + registry_par->beamform_cap = (u8)rtw_beamform_cap; + registry_par->beamformer_rf_num = (u8)rtw_bfer_rf_number; + registry_par->beamformee_rf_num = (u8)rtw_bfee_rf_number; + rtw_regsty_init_rx_ampdu_sz_limit(registry_par); + } +#endif +#ifdef DBG_LA_MODE + registry_par->la_mode_en = (u8)rtw_la_mode_en; +#endif +#ifdef CONFIG_NARROWBAND_SUPPORTING + registry_par->rtw_nb_config = (u8)rtw_nb_config; +#endif + +#ifdef CONFIG_80211AC_VHT + registry_par->vht_enable = (u8)rtw_vht_enable; + registry_par->vht_24g_enable = (u8)rtw_vht_24g_enable; + registry_par->ampdu_factor = (u8)rtw_ampdu_factor; + registry_par->vht_rx_mcs_map[0] = (u8)(rtw_vht_rx_mcs_map & 0xFF); + registry_par->vht_rx_mcs_map[1] = (u8)((rtw_vht_rx_mcs_map & 0xFF00) >> 8); +#endif + +#ifdef CONFIG_TX_EARLY_MODE + registry_par->early_mode = (u8)rtw_early_mode; +#endif +#ifdef CONFIG_RTW_SW_LED + registry_par->led_ctrl = (u8)rtw_led_ctrl; +#endif + registry_par->trx_path_bmp = (u8)rtw_trx_path_bmp; + registry_par->tx_path_lmt = (u8)rtw_tx_path_lmt; + registry_par->rx_path_lmt = (u8)rtw_rx_path_lmt; + registry_par->tx_nss = (u8)rtw_tx_nss; + registry_par->rx_nss = (u8)rtw_rx_nss; + registry_par->low_power = (u8)rtw_low_power; + + registry_par->check_hw_status = (u8)rtw_check_hw_status; + + registry_par->wifi_spec = (u8)rtw_wifi_spec; + +#ifdef CONFIG_REGD_SRC_FROM_OS + if (regd_src_is_valid(rtw_regd_src)) + registry_par->regd_src = (u8)rtw_regd_src; + else { + RTW_WARN("%s invalid rtw_regd_src(%u), use REGD_SRC_RTK_PRIV instead\n", __func__, rtw_regd_src); + registry_par->regd_src = REGD_SRC_RTK_PRIV; + } +#endif + + if (strlen(rtw_country_code) != 2 + || is_alpha(rtw_country_code[0]) == _FALSE + || is_alpha(rtw_country_code[1]) == _FALSE + ) { + if (rtw_country_code != rtw_country_unspecified) + RTW_ERR("%s discard rtw_country_code not in alpha2\n", __func__); + _rtw_memset(registry_par->alpha2, 0xFF, 2); + } else + _rtw_memcpy(registry_par->alpha2, rtw_country_code, 2); + + registry_par->channel_plan = (u8)rtw_channel_plan; + rtw_regsty_load_excl_chs(registry_par); + + registry_par->full_ch_in_p2p_handshake = (u8)rtw_full_ch_in_p2p_handshake; +#ifdef CONFIG_BT_COEXIST + registry_par->btcoex = (u8)rtw_btcoex_enable; + registry_par->bt_iso = (u8)rtw_bt_iso; + registry_par->bt_sco = (u8)rtw_bt_sco; + registry_par->bt_ampdu = (u8)rtw_bt_ampdu; + registry_par->ant_num = (u8)rtw_ant_num; + registry_par->single_ant_path = (u8) rtw_single_ant_path; +#endif + + registry_par->bAcceptAddbaReq = (u8)rtw_AcceptAddbaReq; + + registry_par->antdiv_cfg = (u8)rtw_antdiv_cfg; + registry_par->antdiv_type = (u8)rtw_antdiv_type; + + registry_par->drv_ant_band_switch = (u8) rtw_drv_ant_band_switch; + + registry_par->switch_usb_mode = (u8)rtw_switch_usb_mode; +#ifdef SUPPORT_HW_RFOFF_DETECTED + registry_par->hwpdn_mode = (u8)rtw_hwpdn_mode;/* 0:disable,1:enable,2:by EFUSE config */ + registry_par->hwpwrp_detect = (u8)rtw_hwpwrp_detect;/* 0:disable,1:enable */ +#endif + + registry_par->hw_wps_pbc = (u8)rtw_hw_wps_pbc; + +#ifdef CONFIG_ADAPTOR_INFO_CACHING_FILE + snprintf(registry_par->adaptor_info_caching_file_path, PATH_LENGTH_MAX, "%s", rtw_adaptor_info_caching_file_path); + registry_par->adaptor_info_caching_file_path[PATH_LENGTH_MAX - 1] = 0; +#endif + +#ifdef CONFIG_LAYER2_ROAMING + registry_par->max_roaming_times = (u8)rtw_max_roaming_times; +#endif + +#ifdef CONFIG_IOL + registry_par->fw_iol = rtw_fw_iol; +#endif + +#ifdef CONFIG_80211D + registry_par->enable80211d = (u8)rtw_80211d; +#endif + + snprintf(registry_par->ifname, 16, "%s", ifname); + snprintf(registry_par->if2name, 16, "%s", if2name); + + registry_par->notch_filter = (u8)rtw_notch_filter; + +#ifdef CONFIG_CONCURRENT_MODE + registry_par->virtual_iface_num = (u8)rtw_virtual_iface_num; +#ifdef CONFIG_P2P + registry_par->sel_p2p_iface = (u8)rtw_sel_p2p_iface; + RTW_INFO("%s, Select P2P interface: iface_id:%d\n", __func__, registry_par->sel_p2p_iface); +#endif +#endif + registry_par->pll_ref_clk_sel = (u8)rtw_pll_ref_clk_sel; + +#if CONFIG_TXPWR_LIMIT + registry_par->RegEnableTxPowerLimit = (u8)rtw_tx_pwr_lmt_enable; +#endif + registry_par->RegEnableTxPowerByRate = (u8)rtw_tx_pwr_by_rate; + + rtw_regsty_load_target_tx_power(registry_par); + + registry_par->antenna_gain = (s16)rtw_antenna_gain; + + registry_par->tsf_update_pause_factor = (u8)rtw_tsf_update_pause_factor; + registry_par->tsf_update_restore_factor = (u8)rtw_tsf_update_restore_factor; + + registry_par->TxBBSwing_2G = (s8)rtw_TxBBSwing_2G; + registry_par->TxBBSwing_5G = (s8)rtw_TxBBSwing_5G; + registry_par->bEn_RFE = 1; + registry_par->RFE_Type = (u8)rtw_RFE_type; + registry_par->PowerTracking_Type = (u8)rtw_powertracking_type; + registry_par->AmplifierType_2G = (u8)rtw_amplifier_type_2g; + registry_par->AmplifierType_5G = (u8)rtw_amplifier_type_5g; + registry_par->GLNA_Type = (u8)rtw_GLNA_type; +#ifdef CONFIG_LOAD_PHY_PARA_FROM_FILE + registry_par->load_phy_file = (u8)rtw_load_phy_file; + registry_par->RegDecryptCustomFile = (u8)rtw_decrypt_phy_file; +#endif + registry_par->qos_opt_enable = (u8)rtw_qos_opt_enable; + + registry_par->hiq_filter = (u8)rtw_hiq_filter; + + registry_par->adaptivity_en = (u8)rtw_adaptivity_en; + registry_par->adaptivity_mode = (u8)rtw_adaptivity_mode; + registry_par->adaptivity_th_l2h_ini = (s8)rtw_adaptivity_th_l2h_ini; + registry_par->adaptivity_th_edcca_hl_diff = (s8)rtw_adaptivity_th_edcca_hl_diff; + +#ifdef CONFIG_DYNAMIC_SOML + registry_par->dyn_soml_en = (u8)rtw_dynamic_soml_en; + registry_par->dyn_soml_train_num = (u8)rtw_dynamic_soml_train_num; + registry_par->dyn_soml_interval = (u8)rtw_dynamic_soml_interval; + registry_par->dyn_soml_period = (u8)rtw_dynamic_soml_period; + registry_par->dyn_soml_delay = (u8)rtw_dynamic_soml_delay; +#endif + + registry_par->boffefusemask = (u8)rtw_OffEfuseMask; + registry_par->bFileMaskEfuse = (u8)rtw_FileMaskEfuse; + registry_par->bBTFileMaskEfuse = (u8)rtw_FileMaskEfuse; + +#ifdef CONFIG_RTW_ACS + registry_par->acs_mode = (u8)rtw_acs; + registry_par->acs_auto_scan = (u8)rtw_acs_auto_scan; +#endif +#ifdef CONFIG_BACKGROUND_NOISE_MONITOR + registry_par->nm_mode = (u8)rtw_nm; +#endif + registry_par->reg_rxgain_offset_2g = (u32) rtw_rxgain_offset_2g; + registry_par->reg_rxgain_offset_5gl = (u32) rtw_rxgain_offset_5gl; + registry_par->reg_rxgain_offset_5gm = (u32) rtw_rxgain_offset_5gm; + registry_par->reg_rxgain_offset_5gh = (u32) rtw_rxgain_offset_5gh; + +#ifdef CONFIG_DFS_MASTER + registry_par->dfs_region_domain = (u8)rtw_dfs_region_domain; + #ifdef CONFIG_REGD_SRC_FROM_OS + if (rtw_regd_src == REGD_SRC_OS && registry_par->dfs_region_domain != RTW_DFS_REGD_NONE) { + RTW_WARN("%s force disable radar detection capability when regd_src is OS\n", __func__); + registry_par->dfs_region_domain = RTW_DFS_REGD_NONE; + } + #endif +#endif + + registry_par->amsdu_mode = (u8)rtw_amsdu_mode; + +#ifdef CONFIG_MCC_MODE + registry_par->en_mcc = (u8)rtw_en_mcc; + registry_par->rtw_mcc_ap_bw20_target_tx_tp = (u32)rtw_mcc_ap_bw20_target_tx_tp; + registry_par->rtw_mcc_ap_bw40_target_tx_tp = (u32)rtw_mcc_ap_bw40_target_tx_tp; + registry_par->rtw_mcc_ap_bw80_target_tx_tp = (u32)rtw_mcc_ap_bw80_target_tx_tp; + registry_par->rtw_mcc_sta_bw20_target_tx_tp = (u32)rtw_mcc_sta_bw20_target_tx_tp; + registry_par->rtw_mcc_sta_bw40_target_tx_tp = (u32)rtw_mcc_sta_bw40_target_tx_tp; + registry_par->rtw_mcc_sta_bw80_target_tx_tp = (u32)rtw_mcc_sta_bw80_target_tx_tp; + registry_par->rtw_mcc_single_tx_cri = (u32)rtw_mcc_single_tx_cri; + registry_par->rtw_mcc_policy_table_idx = rtw_mcc_policy_table_idx; + registry_par->rtw_mcc_duration = (u8)rtw_mcc_duration; + registry_par->rtw_mcc_enable_runtime_duration = rtw_mcc_enable_runtime_duration; + registry_par->rtw_mcc_phydm_offload = rtw_mcc_phydm_offload; +#endif /*CONFIG_MCC_MODE */ + +#ifdef CONFIG_WOWLAN + registry_par->wowlan_enable = rtw_wow_enable; + registry_par->wakeup_event = rtw_wakeup_event; + registry_par->suspend_type = rtw_suspend_type; +#endif + +#if defined(CONFIG_SDIO_HCI) && defined(CONFIG_PREALLOC_RX_SKB_BUFFER) + if (rtw_recvbuf_nr != NR_RECVBUFF) { + RTW_WARN("CONFIG_PREALLOC_RX_SKB_BUFFER && CONFIG_SDIO_HCI, force recvbuf_nr to NR_RECVBUFF(%d)\n", NR_RECVBUFF); + rtw_recvbuf_nr = NR_RECVBUFF; + } +#endif + registry_par->recvbuf_nr = rtw_recvbuf_nr; + +#ifdef CONFIG_SUPPORT_TRX_SHARED + registry_par->trx_share_mode = rtw_trx_share_mode; +#endif + registry_par->wowlan_sta_mix_mode = rtw_wowlan_sta_mix_mode; + +#ifdef CONFIG_PCI_HCI + registry_par->pci_aspm_config = rtw_pci_aspm_enable; + registry_par->pci_dynamic_aspm_linkctrl = rtw_pci_dynamic_aspm_linkctrl; +#endif + +#ifdef CONFIG_RTW_NAPI + registry_par->en_napi = (u8)rtw_en_napi; +#ifdef CONFIG_RTW_NAPI_DYNAMIC + registry_par->napi_threshold = (u32)rtw_napi_threshold; +#endif /* CONFIG_RTW_NAPI_DYNAMIC */ +#ifdef CONFIG_RTW_GRO + registry_par->en_gro = (u8)rtw_en_gro; + if (!registry_par->en_napi && registry_par->en_gro) { + registry_par->en_gro = 0; + RTW_WARN("Disable GRO because NAPI is not enabled\n"); + } +#endif /* CONFIG_RTW_GRO */ +#endif /* CONFIG_RTW_NAPI */ + + registry_par->iqk_fw_offload = (u8)rtw_iqk_fw_offload; + registry_par->ch_switch_offload = (u8)rtw_ch_switch_offload; + +#ifdef CONFIG_TDLS + registry_par->en_tdls = rtw_en_tdls; +#endif + +#ifdef CONFIG_ADVANCE_OTA + registry_par->adv_ota = rtw_advnace_ota; +#endif +#ifdef CONFIG_FW_OFFLOAD_PARAM_INIT + registry_par->fw_param_init = rtw_fw_param_init; +#endif +#ifdef CONFIG_AP_MODE + registry_par->bmc_tx_rate = rtw_bmc_tx_rate; + #if CONFIG_RTW_AP_DATA_BMC_TO_UC + registry_par->ap_src_b2u_flags = rtw_ap_src_b2u_flags; + registry_par->ap_fwd_b2u_flags = rtw_ap_fwd_b2u_flags; + #endif +#endif /* CONFIG_AP_MODE */ + +#ifdef CONFIG_RTW_MESH + #if CONFIG_RTW_MESH_DATA_BMC_TO_UC + registry_par->msrc_b2u_flags = rtw_msrc_b2u_flags; + registry_par->mfwd_b2u_flags = rtw_mfwd_b2u_flags; + #endif +#endif /* CONFIG_RTW_MESH */ + +#ifdef CONFIG_FW_HANDLE_TXBCN + registry_par->fw_tbtt_rpt = rtw_tbtt_rpt; +#endif + registry_par->phydm_ability = rtw_phydm_ability; + registry_par->halrf_ability = rtw_halrf_ability; +#ifdef CONFIG_RTW_MESH + registry_par->peer_alive_based_preq = rtw_peer_alive_based_preq; +#endif + +#ifdef RTW_BUSY_DENY_SCAN + registry_par->scan_interval_thr = rtw_scan_interval_thr; +#endif + +#ifdef CONFIG_RTL8822C_XCAP_NEW_POLICY + registry_par->rtw_8822c_xcap_overwrite = (u8)rtw_8822c_xcap_overwrite; +#endif + +#ifdef CONFIG_RTW_MULTI_AP + rtw_regsty_init_unassoc_sta_param(registry_par); +#endif + + return status; +} + +/** + * rtw_net_set_mac_address + * This callback function is used for the Media Access Control address + * of each net_device needs to be changed. + * + * Arguments: + * @pnetdev: net_device pointer. + * @addr: new MAC address. + * + * Return: + * ret = 0: Permit to change net_device's MAC address. + * ret = -1 (Default): Operation not permitted. + * + * Auther: Arvin Liu + * Date: 2015/05/29 + */ +static int rtw_net_set_mac_address(struct net_device *pnetdev, void *addr) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(pnetdev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct sockaddr *sa = (struct sockaddr *)addr; + int ret = -1; + + /* only the net_device is in down state to permit modifying mac addr */ + if ((pnetdev->flags & IFF_UP) == _TRUE) { + RTW_INFO(FUNC_ADPT_FMT": The net_device's is not in down state\n" + , FUNC_ADPT_ARG(padapter)); + + return ret; + } + + /* if the net_device is linked, it's not permit to modify mac addr */ + if (check_fwstate(pmlmepriv, WIFI_UNDER_LINKING) || + check_fwstate(pmlmepriv, WIFI_ASOC_STATE) || + check_fwstate(pmlmepriv, WIFI_UNDER_SURVEY)) { + RTW_INFO(FUNC_ADPT_FMT": The net_device's is not idle currently\n" + , FUNC_ADPT_ARG(padapter)); + + return ret; + } + + /* check whether the input mac address is valid to permit modifying mac addr */ + if (rtw_check_invalid_mac_address(sa->sa_data, _FALSE) == _TRUE) { + RTW_INFO(FUNC_ADPT_FMT": Invalid Mac Addr for "MAC_FMT"\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(sa->sa_data)); + + return ret; + } + + _rtw_memcpy(adapter_mac_addr(padapter), sa->sa_data, ETH_ALEN); /* set mac addr to adapter */ + _rtw_memcpy(pnetdev->dev_addr, sa->sa_data, ETH_ALEN); /* set mac addr to net_device */ + +#if 0 + if (rtw_is_hw_init_completed(padapter)) { + rtw_ps_deny(padapter, PS_DENY_IOCTL); + LeaveAllPowerSaveModeDirect(padapter); /* leave PS mode for guaranteeing to access hw register successfully */ + +#ifdef CONFIG_MI_WITH_MBSSID_CAM + rtw_hal_change_macaddr_mbid(padapter, sa->sa_data); +#else + rtw_hal_set_hwreg(padapter, HW_VAR_MAC_ADDR, sa->sa_data); /* set mac addr to mac register */ +#endif + + rtw_ps_deny_cancel(padapter, PS_DENY_IOCTL); + } +#else + rtw_ps_deny(padapter, PS_DENY_IOCTL); + LeaveAllPowerSaveModeDirect(padapter); /* leave PS mode for guaranteeing to access hw register successfully */ +#ifdef CONFIG_MI_WITH_MBSSID_CAM + rtw_hal_change_macaddr_mbid(padapter, sa->sa_data); +#else + rtw_hal_set_hwreg(padapter, HW_VAR_MAC_ADDR, sa->sa_data); /* set mac addr to mac register */ +#endif + rtw_ps_deny_cancel(padapter, PS_DENY_IOCTL); +#endif + + RTW_INFO(FUNC_ADPT_FMT": Set Mac Addr to "MAC_FMT" Successfully\n" + , FUNC_ADPT_ARG(padapter), MAC_ARG(sa->sa_data)); + + ret = 0; + + return ret; +} + +static struct net_device_stats *rtw_net_get_stats(struct net_device *pnetdev) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(pnetdev); + struct xmit_priv *pxmitpriv = &(padapter->xmitpriv); + struct recv_priv *precvpriv = &(padapter->recvpriv); + + padapter->stats.tx_packets = pxmitpriv->tx_pkts;/* pxmitpriv->tx_pkts++; */ + padapter->stats.rx_packets = precvpriv->rx_pkts;/* precvpriv->rx_pkts++; */ + padapter->stats.tx_dropped = pxmitpriv->tx_drop; + padapter->stats.rx_dropped = precvpriv->rx_drop; + padapter->stats.tx_bytes = pxmitpriv->tx_bytes; + padapter->stats.rx_bytes = precvpriv->rx_bytes; + + return &padapter->stats; +} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) +/* + * AC to queue mapping + * + * AC_VO -> queue 0 + * AC_VI -> queue 1 + * AC_BE -> queue 2 + * AC_BK -> queue 3 + */ +static const u16 rtw_1d_to_queue[8] = { 2, 3, 3, 2, 1, 1, 0, 0 }; + +/* Given a data frame determine the 802.1p/1d tag to use. */ +unsigned int rtw_classify8021d(struct sk_buff *skb) +{ + unsigned int dscp; + + /* skb->priority values from 256->263 are magic values to + * directly indicate a specific 802.1d priority. This is used + * to allow 802.1d priority to be passed directly in from VLAN + * tags, etc. + */ + if (skb->priority >= 256 && skb->priority <= 263) + return skb->priority - 256; + + switch (skb->protocol) { + case htons(ETH_P_IP): + dscp = ip_hdr(skb)->tos & 0xfc; + break; + default: + return 0; + } + + return dscp >> 5; +} + + +static u16 rtw_select_queue(struct net_device *dev, struct sk_buff *skb +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0) + #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) + , struct net_device *sb_dev + #else + , void *accel_priv + #endif + #if ((LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)) && (LINUX_VERSION_CODE < KERNEL_VERSION(5, 2, 0))) + , select_queue_fallback_t fallback + #endif +#endif +) +{ + _adapter *padapter = rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + + skb->priority = rtw_classify8021d(skb); + + if (pmlmepriv->acm_mask != 0) + skb->priority = qos_acm(pmlmepriv->acm_mask, skb->priority); + + return rtw_1d_to_queue[skb->priority]; +} +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) */ + +u16 rtw_os_recv_select_queue(u8 *msdu, enum rtw_rx_llc_hdl llc_hdl) +{ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + u32 priority = 0; + + if (llc_hdl == RTW_RX_LLC_REMOVE) { + u16 eth_type = RTW_GET_BE16(msdu + SNAP_SIZE); + + if (eth_type == ETH_P_IP) { + struct iphdr *iphdr = (struct iphdr *)(msdu + SNAP_SIZE + 2); + unsigned int dscp = iphdr->tos & 0xfc; + + priority = dscp >> 5; + } + } + + return rtw_1d_to_queue[priority]; +#else + return 0; +#endif +} + +static u8 is_rtw_ndev(struct net_device *ndev) +{ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)) + return ndev->netdev_ops + && ndev->netdev_ops->ndo_do_ioctl + && ndev->netdev_ops->ndo_do_ioctl == rtw_ioctl; +#else + return ndev->do_ioctl + && ndev->do_ioctl == rtw_ioctl; +#endif +} + +static int rtw_ndev_notifier_call(struct notifier_block *nb, unsigned long state, void *ptr) +{ + struct net_device *ndev; + + if (ptr == NULL) + return NOTIFY_DONE; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) + ndev = netdev_notifier_info_to_dev(ptr); +#else + ndev = ptr; +#endif + + if (ndev == NULL) + return NOTIFY_DONE; + + if (!is_rtw_ndev(ndev)) + return NOTIFY_DONE; + + RTW_INFO(FUNC_NDEV_FMT" state:%lu\n", FUNC_NDEV_ARG(ndev), state); + + switch (state) { + case NETDEV_CHANGENAME: + rtw_adapter_proc_replace(ndev); + break; + #ifdef CONFIG_NEW_NETDEV_HDL + case NETDEV_PRE_UP : + { + _adapter *adapter = rtw_netdev_priv(ndev); + + rtw_pwr_wakeup(adapter); + } + break; + #endif + } + + return NOTIFY_DONE; +} + +static struct notifier_block rtw_ndev_notifier = { + .notifier_call = rtw_ndev_notifier_call, +}; + +int rtw_ndev_notifier_register(void) +{ + return register_netdevice_notifier(&rtw_ndev_notifier); +} + +void rtw_ndev_notifier_unregister(void) +{ + unregister_netdevice_notifier(&rtw_ndev_notifier); +} + +int rtw_ndev_init(struct net_device *dev) +{ + _adapter *adapter = rtw_netdev_priv(dev); + + RTW_PRINT(FUNC_ADPT_FMT" if%d mac_addr="MAC_FMT"\n" + , FUNC_ADPT_ARG(adapter), (adapter->iface_id + 1), MAC_ARG(dev->dev_addr)); + strncpy(adapter->old_ifname, dev->name, IFNAMSIZ); + adapter->old_ifname[IFNAMSIZ - 1] = '\0'; + rtw_adapter_proc_init(dev); + + return 0; +} + +void rtw_ndev_uninit(struct net_device *dev) +{ + _adapter *adapter = rtw_netdev_priv(dev); + + RTW_PRINT(FUNC_ADPT_FMT" if%d\n" + , FUNC_ADPT_ARG(adapter), (adapter->iface_id + 1)); + rtw_adapter_proc_deinit(dev); +} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)) +static const struct net_device_ops rtw_netdev_ops = { + .ndo_init = rtw_ndev_init, + .ndo_uninit = rtw_ndev_uninit, + .ndo_open = netdev_open, + .ndo_stop = netdev_close, + .ndo_start_xmit = rtw_xmit_entry, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + .ndo_select_queue = rtw_select_queue, +#endif + .ndo_set_mac_address = rtw_net_set_mac_address, + .ndo_get_stats = rtw_net_get_stats, + .ndo_do_ioctl = rtw_ioctl, +}; +#endif + +int rtw_init_netdev_name(struct net_device *pnetdev, const char *ifname) +{ +#ifdef CONFIG_EASY_REPLACEMENT + _adapter *padapter = rtw_netdev_priv(pnetdev); + struct net_device *TargetNetdev = NULL; + _adapter *TargetAdapter = NULL; + + if (padapter->bDongle == 1) { + TargetNetdev = rtw_get_same_net_ndev_by_name(pnetdev, "wlan0"); + if (TargetNetdev) { + RTW_INFO("Force onboard module driver disappear !!!\n"); + TargetAdapter = rtw_netdev_priv(TargetNetdev); + TargetAdapter->DriverState = DRIVER_DISAPPEAR; + + padapter->pid[0] = TargetAdapter->pid[0]; + padapter->pid[1] = TargetAdapter->pid[1]; + padapter->pid[2] = TargetAdapter->pid[2]; + + dev_put(TargetNetdev); + unregister_netdev(TargetNetdev); + + padapter->DriverState = DRIVER_REPLACE_DONGLE; + } + } +#endif /* CONFIG_EASY_REPLACEMENT */ + + if (dev_alloc_name(pnetdev, ifname) < 0) + RTW_ERR("dev_alloc_name, fail!\n"); + + rtw_netif_carrier_off(pnetdev); + /* rtw_netif_stop_queue(pnetdev); */ + + return 0; +} + +void rtw_hook_if_ops(struct net_device *ndev) +{ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)) + ndev->netdev_ops = &rtw_netdev_ops; +#else + ndev->init = rtw_ndev_init; + ndev->uninit = rtw_ndev_uninit; + ndev->open = netdev_open; + ndev->stop = netdev_close; + ndev->hard_start_xmit = rtw_xmit_entry; + ndev->set_mac_address = rtw_net_set_mac_address; + ndev->get_stats = rtw_net_get_stats; + ndev->do_ioctl = rtw_ioctl; +#endif +} + +#ifdef CONFIG_CONCURRENT_MODE +static void rtw_hook_vir_if_ops(struct net_device *ndev); +#endif +struct net_device *rtw_init_netdev(_adapter *old_padapter) +{ + _adapter *padapter; + struct net_device *pnetdev; + + if (old_padapter != NULL) { + rtw_os_ndev_free(old_padapter); + pnetdev = rtw_alloc_etherdev_with_old_priv(sizeof(_adapter), (void *)old_padapter); + } else + pnetdev = rtw_alloc_etherdev(sizeof(_adapter)); + + if (!pnetdev) + return NULL; + + padapter = rtw_netdev_priv(pnetdev); + padapter->pnetdev = pnetdev; + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24) + SET_MODULE_OWNER(pnetdev); +#endif + + rtw_hook_if_ops(pnetdev); +#ifdef CONFIG_CONCURRENT_MODE + if (!is_primary_adapter(padapter)) + rtw_hook_vir_if_ops(pnetdev); +#endif /* CONFIG_CONCURRENT_MODE */ + + +#ifdef CONFIG_TCP_CSUM_OFFLOAD_TX + pnetdev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM); +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39) + pnetdev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM); +#endif +#endif + +#ifdef CONFIG_RTW_NETIF_SG + pnetdev->features |= NETIF_F_SG; +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39) + pnetdev->hw_features |= NETIF_F_SG; +#endif +#endif + + if ((pnetdev->features & NETIF_F_SG) && (pnetdev->features & NETIF_F_IP_CSUM)) { + pnetdev->features |= (NETIF_F_TSO | NETIF_F_GSO); +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39) + pnetdev->hw_features |= (NETIF_F_TSO | NETIF_F_GSO); +#endif + } + /* pnetdev->tx_timeout = NULL; */ + pnetdev->watchdog_timeo = HZ * 3; /* 3 second timeout */ + +#ifdef CONFIG_WIRELESS_EXT + pnetdev->wireless_handlers = (struct iw_handler_def *)&rtw_handlers_def; +#endif + +#ifdef WIRELESS_SPY + /* priv->wireless_data.spy_data = &priv->spy_data; */ + /* pnetdev->wireless_data = &priv->wireless_data; */ +#endif + + return pnetdev; +} + +int rtw_os_ndev_alloc(_adapter *adapter) +{ + int ret = _FAIL; + struct net_device *ndev = NULL; + + ndev = rtw_init_netdev(adapter); + if (ndev == NULL) { + rtw_warn_on(1); + goto exit; + } +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0) + SET_NETDEV_DEV(ndev, dvobj_to_dev(adapter_to_dvobj(adapter))); +#endif + +#ifdef CONFIG_PCI_HCI + if (adapter_to_dvobj(adapter)->bdma64) + ndev->features |= NETIF_F_HIGHDMA; + ndev->irq = adapter_to_dvobj(adapter)->irq; +#endif + +#if defined(CONFIG_IOCTL_CFG80211) + if (rtw_cfg80211_ndev_res_alloc(adapter) != _SUCCESS) { + rtw_warn_on(1); + } else +#endif + ret = _SUCCESS; + + if (ret != _SUCCESS && ndev) + rtw_free_netdev(ndev); +exit: + return ret; +} + +void rtw_os_ndev_free(_adapter *adapter) +{ +#if defined(CONFIG_IOCTL_CFG80211) + rtw_cfg80211_ndev_res_free(adapter); +#endif + + if (adapter->pnetdev) { + rtw_free_netdev(adapter->pnetdev); + adapter->pnetdev = NULL; + } +} + +/* For ethtool +++ */ +#ifdef CONFIG_IOCTL_CFG80211 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 8)) +static void rtw_ethtool_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) +{ + struct wireless_dev *wdev = NULL; + _adapter *padapter = NULL; + HAL_DATA_TYPE *hal_data = NULL; + + wdev = dev->ieee80211_ptr; + if (wdev) { + strlcpy(info->driver, wiphy_dev(wdev->wiphy)->driver->name, + sizeof(info->driver)); + } else { + strlcpy(info->driver, "N/A", sizeof(info->driver)); + } + + strlcpy(info->version, DRIVERVERSION, sizeof(info->version)); + + padapter = (_adapter *)rtw_netdev_priv(dev); + if (padapter) { + hal_data = GET_HAL_DATA(padapter); + } + + if (hal_data) { + scnprintf(info->fw_version, sizeof(info->fw_version), "%d.%d", + hal_data->firmware_version, hal_data->firmware_sub_version); + } else { + strlcpy(info->fw_version, "N/A", sizeof(info->fw_version)); + } + + strlcpy(info->bus_info, dev_name(wiphy_dev(wdev->wiphy)), + sizeof(info->bus_info)); +} + +static const char rtw_ethtool_gstrings_sta_stats[][ETH_GSTRING_LEN] = { + "rx_packets", "rx_bytes", "rx_dropped", + "tx_packets", "tx_bytes", "tx_dropped", +}; + +#define RTW_ETHTOOL_STATS_LEN ARRAY_SIZE(rtw_ethtool_gstrings_sta_stats) + +static int rtw_ethtool_get_sset_count(struct net_device *dev, int sset) +{ + int rv = 0; + + if (sset == ETH_SS_STATS) + rv += RTW_ETHTOOL_STATS_LEN; + + if (rv == 0) + return -EOPNOTSUPP; + + return rv; +} + +static void rtw_ethtool_get_strings(struct net_device *dev, u32 sset, u8 *data) +{ + int sz_sta_stats = 0; + + if (sset == ETH_SS_STATS) { + sz_sta_stats = sizeof(rtw_ethtool_gstrings_sta_stats); + memcpy(data, rtw_ethtool_gstrings_sta_stats, sz_sta_stats); + } +} + +static void rtw_ethtool_get_stats(struct net_device *dev, + struct ethtool_stats *stats, + u64 *data) +{ + int i = 0; + _adapter *padapter = NULL; + struct xmit_priv *pxmitpriv = NULL; + struct recv_priv *precvpriv = NULL; + + memset(data, 0, sizeof(u64) * RTW_ETHTOOL_STATS_LEN); + + padapter = (_adapter *)rtw_netdev_priv(dev); + if (padapter) { + pxmitpriv = &(padapter->xmitpriv); + precvpriv = &(padapter->recvpriv); + + data[i++] = precvpriv->rx_pkts; + data[i++] = precvpriv->rx_bytes; + data[i++] = precvpriv->rx_drop; + + data[i++] = pxmitpriv->tx_pkts; + data[i++] = pxmitpriv->tx_bytes; + data[i++] = pxmitpriv->tx_drop; + } else { + data[i++] = 0; + data[i++] = 0; + data[i++] = 0; + + data[i++] = 0; + data[i++] = 0; + data[i++] = 0; + } +} + +static const struct ethtool_ops rtw_ethtool_ops = { + .get_drvinfo = rtw_ethtool_get_drvinfo, + .get_link = ethtool_op_get_link, + .get_strings = rtw_ethtool_get_strings, + .get_ethtool_stats = rtw_ethtool_get_stats, + .get_sset_count = rtw_ethtool_get_sset_count, +}; +#endif // LINUX_VERSION_CODE >= 3.7.8 +#endif /* CONFIG_IOCTL_CFG80211 */ +/* For ethtool --- */ + +int rtw_os_ndev_register(_adapter *adapter, const char *name) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + int ret = _SUCCESS; + struct net_device *ndev = adapter->pnetdev; + u8 rtnl_lock_needed = rtw_rtnl_lock_needed(dvobj); + +#ifdef CONFIG_RTW_NAPI + netif_napi_add(ndev, &adapter->napi, rtw_recv_napi_poll, RTL_NAPI_WEIGHT); +#endif /* CONFIG_RTW_NAPI */ + +#if defined(CONFIG_IOCTL_CFG80211) + if (rtw_cfg80211_ndev_res_register(adapter) != _SUCCESS) { + rtw_warn_on(1); + ret = _FAIL; + goto exit; + } + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 8)) + netdev_set_default_ethtool_ops(ndev, &rtw_ethtool_ops); +#endif /* LINUX_VERSION_CODE >= 3.7.8 */ +#endif +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)) && defined(CONFIG_PCI_HCI) + ndev->gro_flush_timeout = 100000; +#endif + /* alloc netdev name */ + rtw_init_netdev_name(ndev, name); + + _rtw_memcpy(ndev->dev_addr, adapter_mac_addr(adapter), ETH_ALEN); + + /* Tell the network stack we exist */ + + if (rtnl_lock_needed) + ret = (register_netdev(ndev) == 0) ? _SUCCESS : _FAIL; + else + ret = (register_netdevice(ndev) == 0) ? _SUCCESS : _FAIL; + + if (ret == _SUCCESS) + adapter->registered = 1; + else + RTW_INFO(FUNC_NDEV_FMT" if%d Failed!\n", FUNC_NDEV_ARG(ndev), (adapter->iface_id + 1)); + +#if defined(CONFIG_IOCTL_CFG80211) + if (ret != _SUCCESS) { + rtw_cfg80211_ndev_res_unregister(adapter); + #if !defined(RTW_SINGLE_WIPHY) + rtw_wiphy_unregister(adapter_to_wiphy(adapter)); + #endif + } +#endif + +#if defined(CONFIG_IOCTL_CFG80211) +exit: +#endif +#ifdef CONFIG_RTW_NAPI + if (ret != _SUCCESS) + netif_napi_del(&adapter->napi); +#endif /* CONFIG_RTW_NAPI */ + + return ret; +} + +void rtw_os_ndev_unregister(_adapter *adapter) +{ + struct net_device *netdev = NULL; + + if (adapter == NULL || adapter->registered == 0) + return; + + adapter->ndev_unregistering = 1; + + netdev = adapter->pnetdev; + +#if defined(CONFIG_IOCTL_CFG80211) + rtw_cfg80211_ndev_res_unregister(adapter); +#endif + + if ((adapter->DriverState != DRIVER_DISAPPEAR) && netdev) { + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + u8 rtnl_lock_needed = rtw_rtnl_lock_needed(dvobj); + + if (rtnl_lock_needed) + unregister_netdev(netdev); + else + unregister_netdevice(netdev); + } + +#if defined(CONFIG_IOCTL_CFG80211) && !defined(RTW_SINGLE_WIPHY) +#ifdef CONFIG_RFKILL_POLL + rtw_cfg80211_deinit_rfkill(adapter_to_wiphy(adapter)); +#endif + rtw_wiphy_unregister(adapter_to_wiphy(adapter)); +#endif + +#ifdef CONFIG_RTW_NAPI + if (adapter->napi_state == NAPI_ENABLE) { + napi_disable(&adapter->napi); + adapter->napi_state = NAPI_DISABLE; + } + netif_napi_del(&adapter->napi); +#endif /* CONFIG_RTW_NAPI */ + + adapter->registered = 0; + adapter->ndev_unregistering = 0; +} + +/** + * rtw_os_ndev_init - Allocate and register OS layer net device and relating structures for @adapter + * @adapter: the adapter on which this function applies + * @name: the requesting net device name + * + * Returns: + * _SUCCESS or _FAIL + */ +int rtw_os_ndev_init(_adapter *adapter, const char *name) +{ + int ret = _FAIL; + + if (rtw_os_ndev_alloc(adapter) != _SUCCESS) + goto exit; + + if (rtw_os_ndev_register(adapter, name) != _SUCCESS) + goto os_ndev_free; + + ret = _SUCCESS; + +os_ndev_free: + if (ret != _SUCCESS) + rtw_os_ndev_free(adapter); +exit: + return ret; +} + +/** + * rtw_os_ndev_deinit - Unregister and free OS layer net device and relating structures for @adapter + * @adapter: the adapter on which this function applies + */ +void rtw_os_ndev_deinit(_adapter *adapter) +{ + rtw_os_ndev_unregister(adapter); + rtw_os_ndev_free(adapter); +} + +int rtw_os_ndevs_alloc(struct dvobj_priv *dvobj) +{ + int i, status = _SUCCESS; + _adapter *adapter; + +#if defined(CONFIG_IOCTL_CFG80211) + if (rtw_cfg80211_dev_res_alloc(dvobj) != _SUCCESS) { + rtw_warn_on(1); + return _FAIL; + } +#endif + + for (i = 0; i < dvobj->iface_nums; i++) { + + if (i >= CONFIG_IFACE_NUMBER) { + RTW_ERR("%s %d >= CONFIG_IFACE_NUMBER(%d)\n", __func__, i, CONFIG_IFACE_NUMBER); + rtw_warn_on(1); + continue; + } + + adapter = dvobj->padapters[i]; + if (adapter && !adapter->pnetdev) { + + #ifdef CONFIG_RTW_DYNAMIC_NDEV + if (!is_primary_adapter(adapter)) + continue; + #endif + + status = rtw_os_ndev_alloc(adapter); + if (status != _SUCCESS) { + rtw_warn_on(1); + break; + } + } + } + + if (status != _SUCCESS) { + for (; i >= 0; i--) { + adapter = dvobj->padapters[i]; + if (adapter && adapter->pnetdev) + rtw_os_ndev_free(adapter); + } + } + +#if defined(CONFIG_IOCTL_CFG80211) + if (status != _SUCCESS) + rtw_cfg80211_dev_res_free(dvobj); +#endif + + return status; +} + +void rtw_os_ndevs_free(struct dvobj_priv *dvobj) +{ + int i; + _adapter *adapter = NULL; + + for (i = 0; i < dvobj->iface_nums; i++) { + + if (i >= CONFIG_IFACE_NUMBER) { + RTW_ERR("%s %d >= CONFIG_IFACE_NUMBER(%d)\n", __func__, i, CONFIG_IFACE_NUMBER); + rtw_warn_on(1); + continue; + } + + adapter = dvobj->padapters[i]; + + if (adapter == NULL) + continue; + + rtw_os_ndev_free(adapter); + } + +#if defined(CONFIG_IOCTL_CFG80211) + rtw_cfg80211_dev_res_free(dvobj); +#endif +} + +u32 rtw_start_drv_threads(_adapter *padapter) +{ + u32 _status = _SUCCESS; + + RTW_INFO(FUNC_ADPT_FMT" enter\n", FUNC_ADPT_ARG(padapter)); + +#ifdef CONFIG_XMIT_THREAD_MODE +#if defined(CONFIG_SDIO_HCI) + if (is_primary_adapter(padapter)) +#endif + { + if (padapter->xmitThread == NULL) { + RTW_INFO(FUNC_ADPT_FMT " start RTW_XMIT_THREAD\n", FUNC_ADPT_ARG(padapter)); + padapter->xmitThread = kthread_run(rtw_xmit_thread, padapter, "RTW_XMIT_THREAD"); + if (IS_ERR(padapter->xmitThread)) { + padapter->xmitThread = NULL; + _status = _FAIL; + } + } + } +#endif /* #ifdef CONFIG_XMIT_THREAD_MODE */ + +#ifdef CONFIG_RECV_THREAD_MODE + if (is_primary_adapter(padapter)) { + if (padapter->recvThread == NULL) { + RTW_INFO(FUNC_ADPT_FMT " start RTW_RECV_THREAD\n", FUNC_ADPT_ARG(padapter)); + padapter->recvThread = kthread_run(rtw_recv_thread, padapter, "RTW_RECV_THREAD"); + if (IS_ERR(padapter->recvThread)) { + padapter->recvThread = NULL; + _status = _FAIL; + } + } + } +#endif + + if (is_primary_adapter(padapter)) { + if (padapter->cmdThread == NULL) { + RTW_INFO(FUNC_ADPT_FMT " start RTW_CMD_THREAD\n", FUNC_ADPT_ARG(padapter)); + padapter->cmdThread = kthread_run(rtw_cmd_thread, padapter, "RTW_CMD_THREAD"); + if (IS_ERR(padapter->cmdThread)) { + padapter->cmdThread = NULL; + _status = _FAIL; + } + else + _rtw_down_sema(&padapter->cmdpriv.start_cmdthread_sema); /* wait for cmd_thread to run */ + } + } + + +#ifdef CONFIG_EVENT_THREAD_MODE + if (padapter->evtThread == NULL) { + RTW_INFO(FUNC_ADPT_FMT " start RTW_EVENT_THREAD\n", FUNC_ADPT_ARG(padapter)); + padapter->evtThread = kthread_run(event_thread, padapter, "RTW_EVENT_THREAD"); + if (IS_ERR(padapter->evtThread)) { + padapter->evtThread = NULL; + _status = _FAIL; + } + } +#endif + + rtw_hal_start_thread(padapter); + return _status; + +} + +void rtw_stop_drv_threads(_adapter *padapter) +{ + RTW_INFO(FUNC_ADPT_FMT" enter\n", FUNC_ADPT_ARG(padapter)); + if (is_primary_adapter(padapter)) + rtw_stop_cmd_thread(padapter); + +#ifdef CONFIG_EVENT_THREAD_MODE + if (padapter->evtThread) { + _rtw_up_sema(&padapter->evtpriv.evt_notify); + rtw_thread_stop(padapter->evtThread); + padapter->evtThread = NULL; + } +#endif + +#ifdef CONFIG_XMIT_THREAD_MODE + /* Below is to termindate tx_thread... */ +#if defined(CONFIG_SDIO_HCI) + /* Only wake-up primary adapter */ + if (is_primary_adapter(padapter)) +#endif /*SDIO_HCI */ + { + if (padapter->xmitThread) { + _rtw_up_sema(&padapter->xmitpriv.xmit_sema); + rtw_thread_stop(padapter->xmitThread); + padapter->xmitThread = NULL; + } + } +#endif + +#ifdef CONFIG_RECV_THREAD_MODE + if (is_primary_adapter(padapter) && padapter->recvThread) { + /* Below is to termindate rx_thread... */ + _rtw_up_sema(&padapter->recvpriv.recv_sema); + rtw_thread_stop(padapter->recvThread); + padapter->recvThread = NULL; + } +#endif + + rtw_hal_stop_thread(padapter); +} + +u8 rtw_init_default_value(_adapter *padapter) +{ + u8 ret = _SUCCESS; + struct registry_priv *pregistrypriv = &padapter->registrypriv; + struct xmit_priv *pxmitpriv = &padapter->xmitpriv; + struct security_priv *psecuritypriv = &padapter->securitypriv; + + /* xmit_priv */ + pxmitpriv->vcs_setting = pregistrypriv->vrtl_carrier_sense; + pxmitpriv->vcs = pregistrypriv->vcs_type; + pxmitpriv->vcs_type = pregistrypriv->vcs_type; + /* pxmitpriv->rts_thresh = pregistrypriv->rts_thresh; */ + pxmitpriv->frag_len = pregistrypriv->frag_thresh; + + /* security_priv */ + /* rtw_get_encrypt_decrypt_from_registrypriv(padapter); */ + psecuritypriv->binstallGrpkey = _FAIL; +#ifdef CONFIG_GTK_OL + psecuritypriv->binstallKCK_KEK = _FAIL; +#endif /* CONFIG_GTK_OL */ + psecuritypriv->sw_encrypt = pregistrypriv->software_encrypt; + psecuritypriv->sw_decrypt = pregistrypriv->software_decrypt; + + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open; /* open system */ + psecuritypriv->dot11PrivacyAlgrthm = _NO_PRIVACY_; + + psecuritypriv->dot11PrivacyKeyIndex = 0; + + psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_; + psecuritypriv->dot118021XGrpKeyid = 1; + + psecuritypriv->ndisauthtype = Ndis802_11AuthModeOpen; + psecuritypriv->ndisencryptstatus = Ndis802_11WEPDisabled; + psecuritypriv->dot118021x_bmc_cam_id = INVALID_SEC_MAC_CAM_ID; + + + /* pwrctrl_priv */ + + + /* registry_priv */ + rtw_init_registrypriv_dev_network(padapter); + rtw_update_registrypriv_dev_network(padapter); + + + /* hal_priv */ + rtw_hal_def_value_init(padapter); + +#ifdef CONFIG_MCC_MODE + /* MCC parameter */ + rtw_hal_mcc_parameter_init(padapter); +#endif /* CONFIG_MCC_MODE */ + + /* misc. */ + RTW_ENABLE_FUNC(padapter, DF_RX_BIT); + RTW_ENABLE_FUNC(padapter, DF_TX_BIT); + padapter->bLinkInfoDump = 0; + padapter->bNotifyChannelChange = _FALSE; +#ifdef CONFIG_P2P + padapter->bShowGetP2PState = 1; +#endif + + /* for debug purpose */ + padapter->fix_rate = 0xFF; + padapter->data_fb = 0; + padapter->fix_bw = 0xFF; + padapter->power_offset = 0; + padapter->rsvd_page_offset = 0; + padapter->rsvd_page_num = 0; +#ifdef CONFIG_AP_MODE + padapter->bmc_tx_rate = pregistrypriv->bmc_tx_rate; + #if CONFIG_RTW_AP_DATA_BMC_TO_UC + padapter->b2u_flags_ap_src = pregistrypriv->ap_src_b2u_flags; + padapter->b2u_flags_ap_fwd = pregistrypriv->ap_fwd_b2u_flags; + #endif +#endif + padapter->driver_tx_bw_mode = pregistrypriv->tx_bw_mode; + + padapter->driver_ampdu_spacing = 0xFF; + padapter->driver_rx_ampdu_factor = 0xFF; + padapter->driver_rx_ampdu_spacing = 0xFF; + padapter->fix_rx_ampdu_accept = RX_AMPDU_ACCEPT_INVALID; + padapter->fix_rx_ampdu_size = RX_AMPDU_SIZE_INVALID; +#ifdef CONFIG_TX_AMSDU + padapter->tx_amsdu = 2; + padapter->tx_amsdu_rate = 400; +#endif + padapter->driver_tx_max_agg_num = 0xFF; +#ifdef DBG_RX_COUNTER_DUMP + padapter->dump_rx_cnt_mode = 0; + padapter->drv_rx_cnt_ok = 0; + padapter->drv_rx_cnt_crcerror = 0; + padapter->drv_rx_cnt_drop = 0; +#endif +#ifdef CONFIG_RTW_NAPI + padapter->napi_state = NAPI_DISABLE; +#endif + +#ifdef CONFIG_RTW_ACS + if (pregistrypriv->acs_mode) + rtw_acs_start(padapter); + else + rtw_acs_stop(padapter); +#endif +#ifdef CONFIG_BACKGROUND_NOISE_MONITOR + if (pregistrypriv->nm_mode) + rtw_nm_enable(padapter); + else + rtw_nm_disable(padapter); +#endif + +#ifdef CONFIG_RTW_TOKEN_BASED_XMIT + ATOMIC_SET(&padapter->tbtx_tx_pause, _FALSE); + ATOMIC_SET(&padapter->tbtx_remove_tx_pause, _FALSE); + padapter->tbtx_capability = _TRUE; +#endif + + return ret; +} +#ifdef CONFIG_CLIENT_PORT_CFG +extern void rtw_clt_port_init(struct clt_port_t *cltp); +extern void rtw_clt_port_deinit(struct clt_port_t *cltp); +#endif + +struct dvobj_priv *devobj_init(void) +{ + struct dvobj_priv *pdvobj = NULL; + + pdvobj = (struct dvobj_priv *)rtw_zmalloc(sizeof(*pdvobj)); + if (pdvobj == NULL) + return NULL; + + _rtw_mutex_init(&pdvobj->hw_init_mutex); + _rtw_mutex_init(&pdvobj->h2c_fwcmd_mutex); + _rtw_mutex_init(&pdvobj->setch_mutex); + _rtw_mutex_init(&pdvobj->setbw_mutex); + _rtw_mutex_init(&pdvobj->rf_read_reg_mutex); + _rtw_mutex_init(&pdvobj->ioctrl_mutex); +#ifdef CONFIG_SDIO_INDIRECT_ACCESS + _rtw_mutex_init(&pdvobj->sd_indirect_access_mutex); +#endif +#ifdef CONFIG_SYSON_INDIRECT_ACCESS + _rtw_mutex_init(&pdvobj->syson_indirect_access_mutex); +#endif +#ifdef CONFIG_RTW_CUSTOMER_STR + _rtw_mutex_init(&pdvobj->customer_str_mutex); + _rtw_memset(pdvobj->customer_str, 0xFF, RTW_CUSTOMER_STR_LEN); +#endif +#ifdef CONFIG_PROTSEL_PORT + _rtw_mutex_init(&pdvobj->protsel_port.mutex); +#endif +#ifdef CONFIG_PROTSEL_ATIMDTIM + _rtw_mutex_init(&pdvobj->protsel_atimdtim.mutex); +#endif +#ifdef CONFIG_PROTSEL_MACSLEEP + _rtw_mutex_init(&pdvobj->protsel_macsleep.mutex); +#endif + + pdvobj->processing_dev_remove = _FALSE; + + ATOMIC_SET(&pdvobj->disable_func, 0); + + rtw_macid_ctl_init(&pdvobj->macid_ctl); +#ifdef CONFIG_CLIENT_PORT_CFG + rtw_clt_port_init(&pdvobj->clt_port); +#endif + _rtw_spinlock_init(&pdvobj->cam_ctl.lock); + _rtw_mutex_init(&pdvobj->cam_ctl.sec_cam_access_mutex); +#if defined(CONFIG_PLATFORM_RTK129X) && defined(CONFIG_PCI_HCI) + _rtw_spinlock_init(&pdvobj->io_reg_lock); +#endif +#ifdef CONFIG_MBSSID_CAM + rtw_mbid_cam_init(pdvobj); +#endif + +#ifdef CONFIG_AP_MODE + #ifdef CONFIG_SUPPORT_MULTI_BCN + pdvobj->nr_ap_if = 0; + pdvobj->inter_bcn_space = DEFAULT_BCN_INTERVAL; /* default value is equal to the default beacon_interval (100ms) */ + _rtw_init_queue(&pdvobj->ap_if_q); + pdvobj->vap_map = 0; + #endif /*CONFIG_SUPPORT_MULTI_BCN*/ + #ifdef CONFIG_SWTIMER_BASED_TXBCN + rtw_init_timer(&(pdvobj->txbcn_timer), NULL, tx_beacon_timer_handlder, pdvobj); + #endif +#endif + + rtw_init_timer(&(pdvobj->dynamic_chk_timer), NULL, rtw_dynamic_check_timer_handlder, pdvobj); + rtw_init_timer(&(pdvobj->periodic_tsf_update_end_timer), NULL, rtw_hal_periodic_tsf_update_end_timer_hdl, pdvobj); + +#ifdef CONFIG_MCC_MODE + _rtw_mutex_init(&(pdvobj->mcc_objpriv.mcc_mutex)); + _rtw_mutex_init(&(pdvobj->mcc_objpriv.mcc_tsf_req_mutex)); + _rtw_mutex_init(&(pdvobj->mcc_objpriv.mcc_dbg_reg_mutex)); + _rtw_spinlock_init(&pdvobj->mcc_objpriv.mcc_lock); +#endif /* CONFIG_MCC_MODE */ + +#ifdef CONFIG_RTW_NAPI_DYNAMIC + pdvobj->en_napi_dynamic = 0; +#endif /* CONFIG_RTW_NAPI_DYNAMIC */ + + +#ifdef CONFIG_RTW_TPT_MODE + pdvobj->tpt_mode = 0; + pdvobj->edca_be_ul = 0x5ea42b; + pdvobj->edca_be_dl = 0x00a42b; +#endif + pdvobj->scan_deny = _FALSE; + + return pdvobj; + +} + +void devobj_deinit(struct dvobj_priv *pdvobj) +{ + if (!pdvobj) + return; + + /* TODO: use rtw_os_ndevs_deinit instead at the first stage of driver's dev deinit function */ +#if defined(CONFIG_IOCTL_CFG80211) + rtw_cfg80211_dev_res_free(pdvobj); +#endif + +#ifdef CONFIG_MCC_MODE + _rtw_mutex_free(&(pdvobj->mcc_objpriv.mcc_mutex)); + _rtw_mutex_free(&(pdvobj->mcc_objpriv.mcc_tsf_req_mutex)); + _rtw_mutex_free(&(pdvobj->mcc_objpriv.mcc_dbg_reg_mutex)); + _rtw_spinlock_free(&pdvobj->mcc_objpriv.mcc_lock); +#endif /* CONFIG_MCC_MODE */ + + _rtw_mutex_free(&pdvobj->hw_init_mutex); + _rtw_mutex_free(&pdvobj->h2c_fwcmd_mutex); + +#ifdef CONFIG_RTW_CUSTOMER_STR + _rtw_mutex_free(&pdvobj->customer_str_mutex); +#endif +#ifdef CONFIG_PROTSEL_PORT + _rtw_mutex_free(&pdvobj->protsel_port.mutex); +#endif +#ifdef CONFIG_PROTSEL_ATIMDTIM + _rtw_mutex_free(&pdvobj->protsel_atimdtim.mutex); +#endif +#ifdef CONFIG_PROTSEL_MACSLEEP + _rtw_mutex_free(&pdvobj->protsel_macsleep.mutex); +#endif + + _rtw_mutex_free(&pdvobj->setch_mutex); + _rtw_mutex_free(&pdvobj->setbw_mutex); + _rtw_mutex_free(&pdvobj->rf_read_reg_mutex); + _rtw_mutex_free(&pdvobj->ioctrl_mutex); +#ifdef CONFIG_SDIO_INDIRECT_ACCESS + _rtw_mutex_free(&pdvobj->sd_indirect_access_mutex); +#endif +#ifdef CONFIG_SYSON_INDIRECT_ACCESS + _rtw_mutex_free(&pdvobj->syson_indirect_access_mutex); +#endif + + rtw_macid_ctl_deinit(&pdvobj->macid_ctl); +#ifdef CONFIG_CLIENT_PORT_CFG + rtw_clt_port_deinit(&pdvobj->clt_port); +#endif + + _rtw_spinlock_free(&pdvobj->cam_ctl.lock); + _rtw_mutex_free(&pdvobj->cam_ctl.sec_cam_access_mutex); + +#if defined(CONFIG_PLATFORM_RTK129X) && defined(CONFIG_PCI_HCI) + _rtw_spinlock_free(&pdvobj->io_reg_lock); +#endif +#ifdef CONFIG_MBSSID_CAM + rtw_mbid_cam_deinit(pdvobj); +#endif +#ifdef CONFIG_SUPPORT_MULTI_BCN + _rtw_spinlock_free(&(pdvobj->ap_if_q.lock)); +#endif + rtw_mfree((u8 *)pdvobj, sizeof(*pdvobj)); +} + +inline u8 rtw_rtnl_lock_needed(struct dvobj_priv *dvobj) +{ + if (dvobj->rtnl_lock_holder && dvobj->rtnl_lock_holder == current) + return 0; + return 1; +} + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)) +static inline int rtnl_is_locked(void) +{ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)) + if (unlikely(rtnl_trylock())) { + rtnl_unlock(); +#else + if (unlikely(down_trylock(&rtnl_sem) == 0)) { + up(&rtnl_sem); +#endif + return 0; + } + return 1; +} +#endif + +inline void rtw_set_rtnl_lock_holder(struct dvobj_priv *dvobj, _thread_hdl_ thd_hdl) +{ + rtw_warn_on(!rtnl_is_locked()); + + if (!thd_hdl || rtnl_is_locked()) + dvobj->rtnl_lock_holder = thd_hdl; + + if (dvobj->rtnl_lock_holder && 0) + RTW_INFO("rtnl_lock_holder: %s:%d\n", current->comm, current->pid); +} + +u8 rtw_reset_drv_sw(_adapter *padapter) +{ + u8 ret8 = _SUCCESS; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter); + + /* hal_priv */ + rtw_hal_def_value_init(padapter); + + RTW_ENABLE_FUNC(padapter, DF_RX_BIT); + RTW_ENABLE_FUNC(padapter, DF_TX_BIT); + + padapter->bLinkInfoDump = 0; + + padapter->xmitpriv.tx_pkts = 0; + padapter->recvpriv.rx_pkts = 0; + + pmlmepriv->LinkDetectInfo.bBusyTraffic = _FALSE; + + /* pmlmepriv->LinkDetectInfo.TrafficBusyState = _FALSE; */ + pmlmepriv->LinkDetectInfo.TrafficTransitionCount = 0; + pmlmepriv->LinkDetectInfo.LowPowerTransitionCount = 0; + + _clr_fwstate_(pmlmepriv, WIFI_UNDER_SURVEY | WIFI_UNDER_LINKING); + +#ifdef DBG_CONFIG_ERROR_DETECT + if (is_primary_adapter(padapter)) + rtw_hal_sreset_reset_value(padapter); +#endif + pwrctrlpriv->pwr_state_check_cnts = 0; + + /* mlmeextpriv */ + mlmeext_set_scan_state(&padapter->mlmeextpriv, SCAN_DISABLE); + +#ifdef CONFIG_NEW_SIGNAL_STAT_PROCESS + rtw_set_signal_stat_timer(&padapter->recvpriv); +#endif + + return ret8; +} + + +u8 rtw_init_drv_sw(_adapter *padapter) +{ + u8 ret8 = _SUCCESS; + +#ifdef CONFIG_RTW_CFGVENDOR_RANDOM_MAC_OUI + struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(padapter); +#endif + + #if defined(CONFIG_AP_MODE) && defined(CONFIG_SUPPORT_MULTI_BCN) + _rtw_init_listhead(&padapter->list); + #ifdef CONFIG_FW_HANDLE_TXBCN + padapter->vap_id = CONFIG_LIMITED_AP_NUM; + if (is_primary_adapter(padapter)) + adapter_to_dvobj(padapter)->vap_tbtt_rpt_map = adapter_to_regsty(padapter)->fw_tbtt_rpt; + #endif + #endif + + #ifdef CONFIG_CLIENT_PORT_CFG + padapter->client_id = MAX_CLIENT_PORT_NUM; + padapter->client_port = CLT_PORT_INVALID; + #endif + + if (is_primary_adapter(padapter)) { + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(padapter); + + dvobj->macid_ctl.num = rtw_min(hal_spec->macid_num, MACID_NUM_SW_LIMIT); + dvobj->macid_ctl.macid_cap = hal_spec->macid_cap; + dvobj->macid_ctl.macid_txrpt = hal_spec->macid_txrpt; + dvobj->macid_ctl.macid_txrpt_pgsz = hal_spec->macid_txrpt_pgsz; + dvobj->cam_ctl.sec_cap = hal_spec->sec_cap; + dvobj->cam_ctl.num = rtw_min(hal_spec->sec_cam_ent_num, SEC_CAM_ENT_NUM_SW_LIMIT); + + dvobj->wow_ctl.wow_cap = hal_spec->wow_cap; + + #ifdef CONFIG_SDIO_TX_ENABLE_AVAL_INT + dvobj->tx_aval_int_thr_mode = 2; /*setting by max tx length*/ + dvobj->tx_aval_int_thr_value = 0; + #endif /*CONFIG_SDIO_TX_ENABLE_AVAL_INT*/ + + #if CONFIG_TX_AC_LIFETIME + { + struct registry_priv *regsty = adapter_to_regsty(padapter); + int i; + + dvobj->tx_aclt_flags = regsty->tx_aclt_flags; + for (i = 0; i < TX_ACLT_CONF_NUM; i++) { + dvobj->tx_aclt_confs[i].en = regsty->tx_aclt_confs[i].en; + dvobj->tx_aclt_confs[i].vo_vi + = regsty->tx_aclt_confs[i].vo_vi / (hal_spec->tx_aclt_unit_factor * 32); + if (dvobj->tx_aclt_confs[i].vo_vi > 0xFFFF) + dvobj->tx_aclt_confs[i].vo_vi = 0xFFFF; + dvobj->tx_aclt_confs[i].be_bk + = regsty->tx_aclt_confs[i].be_bk / (hal_spec->tx_aclt_unit_factor * 32); + if (dvobj->tx_aclt_confs[i].be_bk > 0xFFFF) + dvobj->tx_aclt_confs[i].be_bk = 0xFFFF; + } + + dvobj->tx_aclt_force_val.en = 0xFF; + } + #endif + #if defined (CONFIG_CONCURRENT_MODE) && defined (CONFIG_TSF_SYNC) + dvobj->sync_tsfr_counter = 0x0; + #endif + } + + ret8 = rtw_init_default_value(padapter); + + if ((rtw_init_cmd_priv(&padapter->cmdpriv)) == _FAIL) { + ret8 = _FAIL; + goto exit; + } + + padapter->cmdpriv.padapter = padapter; + + if ((rtw_init_evt_priv(&padapter->evtpriv)) == _FAIL) { + ret8 = _FAIL; + goto exit; + } + + if (is_primary_adapter(padapter)) { + if (rtw_hal_rfpath_init(padapter) == _FAIL) { + ret8 = _FAIL; + goto exit; + } + if (rtw_hal_trxnss_init(padapter) == _FAIL) { + ret8 = _FAIL; + goto exit; + } + if (rtw_hal_runtime_trx_path_decision(padapter) == _FAIL) { + ret8 = _FAIL; + goto exit; + } + if (rtw_rfctl_init(padapter) == _FAIL) { + ret8 = _FAIL; + goto exit; + } + } + + if (rtw_init_mlme_priv(padapter) == _FAIL) { + ret8 = _FAIL; + goto exit; + } + +#if (defined(CONFIG_P2P) && defined(CONFIG_CONCURRENT_MODE)) || defined(CONFIG_IOCTL_CFG80211) + rtw_init_roch_info(padapter); +#endif + +#ifdef CONFIG_P2P + rtw_init_wifidirect_timers(padapter); + init_wifidirect_info(padapter, P2P_ROLE_DISABLE); + reset_global_wifidirect_info(padapter); +#ifdef CONFIG_WFD + if (rtw_init_wifi_display_info(padapter) == _FAIL) + RTW_ERR("Can't init init_wifi_display_info\n"); +#endif +#endif /* CONFIG_P2P */ + + if (init_mlme_ext_priv(padapter) == _FAIL) { + ret8 = _FAIL; + goto exit; + } + +#ifdef CONFIG_TDLS + if (rtw_init_tdls_info(padapter) == _FAIL) { + RTW_INFO("Can't rtw_init_tdls_info\n"); + ret8 = _FAIL; + goto exit; + } +#endif /* CONFIG_TDLS */ + +#ifdef CONFIG_RTW_MESH + rtw_mesh_cfg_init(padapter); +#endif + + if (_rtw_init_xmit_priv(&padapter->xmitpriv, padapter) == _FAIL) { + RTW_INFO("Can't _rtw_init_xmit_priv\n"); + ret8 = _FAIL; + goto exit; + } + + if (_rtw_init_recv_priv(&padapter->recvpriv, padapter) == _FAIL) { + RTW_INFO("Can't _rtw_init_recv_priv\n"); + ret8 = _FAIL; + goto exit; + } + /* add for CONFIG_IEEE80211W, none 11w also can use */ + _rtw_spinlock_init(&padapter->security_key_mutex); + + /* We don't need to memset padapter->XXX to zero, because adapter is allocated by rtw_zvmalloc(). */ + /* _rtw_memset((unsigned char *)&padapter->securitypriv, 0, sizeof (struct security_priv)); */ + + if (_rtw_init_sta_priv(&padapter->stapriv) == _FAIL) { + RTW_INFO("Can't _rtw_init_sta_priv\n"); + ret8 = _FAIL; + goto exit; + } + + padapter->setband = WIFI_FREQUENCY_BAND_AUTO; + padapter->fix_rate = 0xFF; + padapter->power_offset = 0; + padapter->rsvd_page_offset = 0; + padapter->rsvd_page_num = 0; + + padapter->data_fb = 0; + padapter->fix_rx_ampdu_accept = RX_AMPDU_ACCEPT_INVALID; + padapter->fix_rx_ampdu_size = RX_AMPDU_SIZE_INVALID; +#ifdef DBG_RX_COUNTER_DUMP + padapter->dump_rx_cnt_mode = 0; + padapter->drv_rx_cnt_ok = 0; + padapter->drv_rx_cnt_crcerror = 0; + padapter->drv_rx_cnt_drop = 0; +#endif + rtw_init_bcmc_stainfo(padapter); + + rtw_init_pwrctrl_priv(padapter); + + /* _rtw_memset((u8 *)&padapter->qospriv, 0, sizeof (struct qos_priv)); */ /* move to mlme_priv */ + +#ifdef CONFIG_MP_INCLUDED + if (init_mp_priv(padapter) == _FAIL) + RTW_INFO("%s: initialize MP private data Fail!\n", __func__); +#endif + + rtw_hal_dm_init(padapter); + + if (is_primary_adapter(padapter)) + rtw_rfctl_chplan_init(padapter); + +#ifdef CONFIG_RTW_SW_LED + rtw_hal_sw_led_init(padapter); +#endif +#ifdef DBG_CONFIG_ERROR_DETECT + rtw_hal_sreset_init(padapter); +#endif + +#ifdef CONFIG_WAPI_SUPPORT + padapter->WapiSupport = true; /* set true temp, will revise according to Efuse or Registry value later. */ + rtw_wapi_init(padapter); +#endif + +#ifdef CONFIG_BR_EXT + _rtw_spinlock_init(&padapter->br_ext_lock); +#endif /* CONFIG_BR_EXT */ + +#ifdef CONFIG_BEAMFORMING +#ifdef RTW_BEAMFORMING_VERSION_2 + rtw_bf_init(padapter); +#endif /* RTW_BEAMFORMING_VERSION_2 */ +#endif /* CONFIG_BEAMFORMING */ + +#ifdef CONFIG_RTW_REPEATER_SON + init_rtw_rson_data(adapter_to_dvobj(padapter)); +#endif + +#ifdef CONFIG_RTW_80211K + rtw_init_rm(padapter); +#endif + +#ifdef CONFIG_RTW_CFGVENDOR_RANDOM_MAC_OUI + memset(pwdev_priv->pno_mac_addr, 0xFF, ETH_ALEN); +#endif + +exit: + + + + return ret8; + +} + +#ifdef CONFIG_WOWLAN +void rtw_cancel_dynamic_chk_timer(_adapter *padapter) +{ + _cancel_timer_ex(&adapter_to_dvobj(padapter)->dynamic_chk_timer); +} +#endif + +void rtw_cancel_all_timer(_adapter *padapter) +{ + + _cancel_timer_ex(&padapter->mlmepriv.assoc_timer); + + _cancel_timer_ex(&padapter->mlmepriv.scan_to_timer); + +#ifdef CONFIG_DFS_MASTER + _cancel_timer_ex(&adapter_to_rfctl(padapter)->radar_detect_timer); +#endif + + _cancel_timer_ex(&adapter_to_dvobj(padapter)->dynamic_chk_timer); + _cancel_timer_ex(&adapter_to_dvobj(padapter)->periodic_tsf_update_end_timer); +#ifdef CONFIG_RTW_SW_LED + /* cancel sw led timer */ + rtw_hal_sw_led_deinit(padapter); +#endif + _cancel_timer_ex(&(adapter_to_pwrctl(padapter)->pwr_state_check_timer)); + +#ifdef CONFIG_TX_AMSDU + _cancel_timer_ex(&padapter->xmitpriv.amsdu_bk_timer); + _cancel_timer_ex(&padapter->xmitpriv.amsdu_be_timer); + _cancel_timer_ex(&padapter->xmitpriv.amsdu_vo_timer); + _cancel_timer_ex(&padapter->xmitpriv.amsdu_vi_timer); +#endif + +#ifdef CONFIG_IOCTL_CFG80211 + _cancel_timer_ex(&padapter->rochinfo.remain_on_ch_timer); +#endif /* CONFIG_IOCTL_CFG80211 */ + +#ifdef CONFIG_SET_SCAN_DENY_TIMER + _cancel_timer_ex(&padapter->mlmepriv.set_scan_deny_timer); + rtw_clear_scan_deny(padapter); +#endif + +#ifdef CONFIG_NEW_SIGNAL_STAT_PROCESS + _cancel_timer_ex(&padapter->recvpriv.signal_stat_timer); +#endif + +#ifdef CONFIG_LPS_RPWM_TIMER + _cancel_timer_ex(&(adapter_to_pwrctl(padapter)->pwr_rpwm_timer)); +#endif /* CONFIG_LPS_RPWM_TIMER */ + +#ifdef CONFIG_RTW_TOKEN_BASED_XMIT + _cancel_timer_ex(&padapter->mlmeextpriv.tbtx_xmit_timer); + _cancel_timer_ex(&padapter->mlmeextpriv.tbtx_token_dispatch_timer); +#endif + + /* cancel dm timer */ + rtw_hal_dm_deinit(padapter); + +#ifdef CONFIG_PLATFORM_FS_MX61 + msleep(50); +#endif +} + +u8 rtw_free_drv_sw(_adapter *padapter) +{ + +#ifdef CONFIG_WAPI_SUPPORT + rtw_wapi_free(padapter); +#endif + + /* we can call rtw_p2p_enable here, but: */ + /* 1. rtw_p2p_enable may have IO operation */ + /* 2. rtw_p2p_enable is bundled with wext interface */ + #ifdef CONFIG_P2P + { + struct wifidirect_info *pwdinfo = &padapter->wdinfo; + #ifdef CONFIG_CONCURRENT_MODE + struct roch_info *prochinfo = &padapter->rochinfo; + #endif + if (!rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) { + _cancel_timer_ex(&pwdinfo->find_phase_timer); + _cancel_timer_ex(&pwdinfo->restore_p2p_state_timer); + _cancel_timer_ex(&pwdinfo->pre_tx_scan_timer); + #ifdef CONFIG_CONCURRENT_MODE + _cancel_timer_ex(&prochinfo->ap_roch_ch_switch_timer); + #endif /* CONFIG_CONCURRENT_MODE */ + rtw_p2p_set_state(pwdinfo, P2P_STATE_NONE); + } + } + #endif + /* add for CONFIG_IEEE80211W, none 11w also can use */ + _rtw_spinlock_free(&padapter->security_key_mutex); + +#ifdef CONFIG_BR_EXT + _rtw_spinlock_free(&padapter->br_ext_lock); +#endif /* CONFIG_BR_EXT */ + + free_mlme_ext_priv(&padapter->mlmeextpriv); + +#ifdef CONFIG_TDLS + /* rtw_free_tdls_info(&padapter->tdlsinfo); */ +#endif /* CONFIG_TDLS */ + +#ifdef CONFIG_RTW_80211K + rtw_free_rm_priv(padapter); +#endif + + rtw_free_cmd_priv(&padapter->cmdpriv); + + rtw_free_evt_priv(&padapter->evtpriv); + + rtw_free_mlme_priv(&padapter->mlmepriv); + + if (is_primary_adapter(padapter)) + rtw_rfctl_deinit(padapter); + + /* free_io_queue(padapter); */ + + _rtw_free_xmit_priv(&padapter->xmitpriv); + + _rtw_free_sta_priv(&padapter->stapriv); /* will free bcmc_stainfo here */ + + _rtw_free_recv_priv(&padapter->recvpriv); + + rtw_free_pwrctrl_priv(padapter); + +#ifdef CONFIG_PLATFORM_CMAP_INTFS + if (padapter->cmap_bss_status_evt) { + cmap_intfs_mfree(padapter->cmap_bss_status_evt, padapter->cmap_bss_status_evt_len); + padapter->cmap_bss_status_evt = NULL; + } +#endif + + /* rtw_mfree((void *)padapter, sizeof (padapter)); */ + + rtw_hal_free_data(padapter); + + return _SUCCESS; + +} +void rtw_intf_start(_adapter *adapter) +{ + if (adapter->intf_start) + adapter->intf_start(adapter); + GET_HAL_DATA(adapter)->intf_start = 1; +} +void rtw_intf_stop(_adapter *adapter) +{ + if (adapter->intf_stop) + adapter->intf_stop(adapter); + GET_HAL_DATA(adapter)->intf_start = 0; +} + +#ifdef CONFIG_CONCURRENT_MODE +#ifndef CONFIG_NEW_NETDEV_HDL +int _netdev_vir_if_open(struct net_device *pnetdev) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(pnetdev); + _adapter *primary_padapter = GET_PRIMARY_ADAPTER(padapter); + + RTW_INFO(FUNC_NDEV_FMT" , bup=%d\n", FUNC_NDEV_ARG(pnetdev), padapter->bup); + + if (!primary_padapter) + goto _netdev_virtual_iface_open_error; + +#ifdef CONFIG_PLATFORM_INTEL_BYT + if (padapter->bup == _FALSE) { + u8 mac[ETH_ALEN]; + + /* get mac address from primary_padapter */ + if (primary_padapter->bup == _FALSE) + rtw_macaddr_cfg(adapter_mac_addr(primary_padapter), get_hal_mac_addr(primary_padapter)); + + _rtw_memcpy(mac, adapter_mac_addr(primary_padapter), ETH_ALEN); + + /* + * If the BIT1 is 0, the address is universally administered. + * If it is 1, the address is locally administered + */ + mac[0] |= BIT(1); + + _rtw_memcpy(adapter_mac_addr(padapter), mac, ETH_ALEN); + +#ifdef CONFIG_MI_WITH_MBSSID_CAM + rtw_mbid_camid_alloc(padapter, adapter_mac_addr(padapter)); +#endif + rtw_init_wifidirect_addrs(padapter, adapter_mac_addr(padapter), adapter_mac_addr(padapter)); + _rtw_memcpy(pnetdev->dev_addr, adapter_mac_addr(padapter), ETH_ALEN); + } +#endif /*CONFIG_PLATFORM_INTEL_BYT*/ + + if (primary_padapter->bup == _FALSE || !rtw_is_hw_init_completed(primary_padapter)) + _netdev_open(primary_padapter->pnetdev); + + if (padapter->bup == _FALSE && primary_padapter->bup == _TRUE && + rtw_is_hw_init_completed(primary_padapter)) { +#if 0 /*#ifdef CONFIG_MI_WITH_MBSSID_CAM*/ + rtw_hal_set_hwreg(padapter, HW_VAR_MAC_ADDR, adapter_mac_addr(padapter)); /* set mac addr to mac register */ +#endif + + } + + if (padapter->bup == _FALSE) { + if (rtw_start_drv_threads(padapter) == _FAIL) + goto _netdev_virtual_iface_open_error; + } + +#ifdef CONFIG_RTW_NAPI + if (padapter->napi_state == NAPI_DISABLE) { + napi_enable(&padapter->napi); + padapter->napi_state = NAPI_ENABLE; + } +#endif + +#ifdef CONFIG_IOCTL_CFG80211 + rtw_cfg80211_init_wdev_data(padapter); +#endif + + padapter->bup = _TRUE; + + padapter->net_closed = _FALSE; + + rtw_netif_wake_queue(pnetdev); + + RTW_INFO(FUNC_NDEV_FMT" (bup=%d) exit\n", FUNC_NDEV_ARG(pnetdev), padapter->bup); + + return 0; + +_netdev_virtual_iface_open_error: + + padapter->bup = _FALSE; + +#ifdef CONFIG_RTW_NAPI + if(padapter->napi_state == NAPI_ENABLE) { + napi_disable(&padapter->napi); + padapter->napi_state = NAPI_DISABLE; + } +#endif + + rtw_netif_carrier_off(pnetdev); + rtw_netif_stop_queue(pnetdev); + + return -1; + +} + +int netdev_vir_if_open(struct net_device *pnetdev) +{ + int ret; + _adapter *padapter = (_adapter *)rtw_netdev_priv(pnetdev); + + _enter_critical_mutex(&(adapter_to_dvobj(padapter)->hw_init_mutex), NULL); + ret = _netdev_vir_if_open(pnetdev); + _exit_critical_mutex(&(adapter_to_dvobj(padapter)->hw_init_mutex), NULL); + +#ifdef CONFIG_AUTO_AP_MODE + /* if(padapter->iface_id == 2) */ + /* rtw_start_auto_ap(padapter); */ +#endif + + return ret; +} + +static int netdev_vir_if_close(struct net_device *pnetdev) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(pnetdev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + + RTW_INFO(FUNC_NDEV_FMT" , bup=%d\n", FUNC_NDEV_ARG(pnetdev), padapter->bup); + padapter->net_closed = _TRUE; + pmlmepriv->LinkDetectInfo.bBusyTraffic = _FALSE; + + if (pnetdev) + rtw_netif_stop_queue(pnetdev); + +#ifdef CONFIG_P2P + if (!rtw_p2p_chk_role(&padapter->wdinfo, P2P_ROLE_DISABLE)) + rtw_p2p_enable(padapter, P2P_ROLE_DISABLE); +#endif + +#ifdef CONFIG_IOCTL_CFG80211 + rtw_scan_abort(padapter); + rtw_cfg80211_wait_scan_req_empty(padapter, 200); + adapter_wdev_data(padapter)->bandroid_scan = _FALSE; +#endif + + return 0; +} +#endif /*#ifndef CONFIG_NEW_NETDEV_HDL*/ + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)) +static const struct net_device_ops rtw_netdev_vir_if_ops = { + .ndo_init = rtw_ndev_init, + .ndo_uninit = rtw_ndev_uninit, + #ifdef CONFIG_NEW_NETDEV_HDL + .ndo_open = netdev_open, + .ndo_stop = netdev_close, + #else + .ndo_open = netdev_vir_if_open, + .ndo_stop = netdev_vir_if_close, + #endif + .ndo_start_xmit = rtw_xmit_entry, + .ndo_set_mac_address = rtw_net_set_mac_address, + .ndo_get_stats = rtw_net_get_stats, + .ndo_do_ioctl = rtw_ioctl, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + .ndo_select_queue = rtw_select_queue, +#endif +}; +#endif + +static void rtw_hook_vir_if_ops(struct net_device *ndev) +{ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)) + ndev->netdev_ops = &rtw_netdev_vir_if_ops; +#else + ndev->init = rtw_ndev_init; + ndev->uninit = rtw_ndev_uninit; + #ifdef CONFIG_NEW_NETDEV_HDL + ndev->open = netdev_open; + ndev->stop = netdev_close; + #else + ndev->open = netdev_vir_if_open; + ndev->stop = netdev_vir_if_close; + #endif + + ndev->set_mac_address = rtw_net_set_mac_address; +#endif +} +_adapter *rtw_drv_add_vir_if(_adapter *primary_padapter, + void (*set_intf_ops)(_adapter *primary_padapter, struct _io_ops *pops)) +{ + int res = _FAIL; + _adapter *padapter = NULL; + struct dvobj_priv *pdvobjpriv; + u8 mac[ETH_ALEN]; +#ifdef CONFIG_MI_UNIQUE_MACADDR_BIT + u32 mi_unique_macaddr_bit = 0; + u8 i; +#endif + + /****** init adapter ******/ + padapter = (_adapter *)rtw_zvmalloc(sizeof(*padapter)); + if (padapter == NULL) + goto exit; + + if (loadparam(padapter) != _SUCCESS) + goto free_adapter; + + _rtw_memcpy(padapter, primary_padapter, sizeof(_adapter)); + + /* */ + padapter->bup = _FALSE; + padapter->net_closed = _TRUE; + padapter->dir_dev = NULL; + padapter->dir_odm = NULL; + + /*set adapter_type/iface type*/ + padapter->isprimary = _FALSE; + padapter->adapter_type = VIRTUAL_ADAPTER; + +#ifdef CONFIG_MI_WITH_MBSSID_CAM + padapter->hw_port = HW_PORT0; +#elif defined(CONFIG_PORT_BASED_TXBCN) + padapter->hw_port = adapter_to_dvobj(padapter)->iface_nums; +#else + padapter->hw_port = HW_PORT1; +#endif + + + /****** hook vir if into dvobj ******/ + pdvobjpriv = adapter_to_dvobj(padapter); + padapter->iface_id = pdvobjpriv->iface_nums; + pdvobjpriv->padapters[pdvobjpriv->iface_nums++] = padapter; + + padapter->intf_start = primary_padapter->intf_start; + padapter->intf_stop = primary_padapter->intf_stop; + + /* step init_io_priv */ + if ((rtw_init_io_priv(padapter, set_intf_ops)) == _FAIL) { + goto free_adapter; + } + + /*init drv data*/ + if (rtw_init_drv_sw(padapter) != _SUCCESS) + goto free_drv_sw; + + + /*get mac address from primary_padapter*/ + _rtw_memcpy(mac, adapter_mac_addr(primary_padapter), ETH_ALEN); + +#ifdef CONFIG_MI_UNIQUE_MACADDR_BIT + mi_unique_macaddr_bit = BIT(CONFIG_MI_UNIQUE_MACADDR_BIT) >> 24; + /* Find out CONFIG_MI_UNIQUE_MACADDR_BIT in which nic specific byte */ + for(i=3;i<6;i++) { + if((mi_unique_macaddr_bit >> 8) == 0) + break; + + mi_unique_macaddr_bit >>= 8; + } + + if((mac[i] & (u8)mi_unique_macaddr_bit)== 0) { + RTW_INFO("%s() "MAC_FMT" : BIT%u is zero\n", __func__, MAC_ARG(mac), CONFIG_MI_UNIQUE_MACADDR_BIT); + /* IFACE_ID1/IFACE_ID3 : set locally administered bit */ + if(padapter->iface_id & BIT(0)) + mac[0] |= BIT(1); + /* IFACE_ID2/IFACE_ID3 : set bit(CONFIG_MI_UNIQUE_MACADDR_BIT) */ + if(padapter->iface_id >> 1) + mac[i] |= (u8)mi_unique_macaddr_bit; + } else +#endif + { + /* + * If the BIT1 is 0, the address is universally administered. + * If it is 1, the address is locally administered + */ + mac[0] |= BIT(1); + if (padapter->iface_id > IFACE_ID1) + mac[0] ^= ((padapter->iface_id)<<2); + } + + _rtw_memcpy(adapter_mac_addr(padapter), mac, ETH_ALEN); + /* update mac-address to mbsid-cam cache*/ +#ifdef CONFIG_MI_WITH_MBSSID_CAM + rtw_mbid_camid_alloc(padapter, adapter_mac_addr(padapter)); +#endif + RTW_INFO("%s if%d mac_addr : "MAC_FMT"\n", __func__, padapter->iface_id + 1, MAC_ARG(adapter_mac_addr(padapter))); +#ifdef CONFIG_P2P + rtw_init_wifidirect_addrs(padapter, adapter_mac_addr(padapter), adapter_mac_addr(padapter)); +#endif + + rtw_led_set_ctl_en_mask_virtual(padapter); + rtw_led_set_iface_en(padapter, 1); + + res = _SUCCESS; + +free_drv_sw: + if (res != _SUCCESS && padapter) + rtw_free_drv_sw(padapter); +free_adapter: + if (res != _SUCCESS && padapter) { + rtw_vmfree((u8 *)padapter, sizeof(*padapter)); + padapter = NULL; + } +exit: + return padapter; +} + +void rtw_drv_stop_vir_if(_adapter *padapter) +{ + struct net_device *pnetdev = NULL; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + + if (padapter == NULL) + return; + RTW_INFO(FUNC_ADPT_FMT" enter\n", FUNC_ADPT_ARG(padapter)); + + pnetdev = padapter->pnetdev; + + if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE)) + rtw_disassoc_cmd(padapter, 0, RTW_CMDF_DIRECTLY); + +#ifdef CONFIG_AP_MODE + if (MLME_IS_AP(padapter) || MLME_IS_MESH(padapter)) { + free_mlme_ap_info(padapter); + #ifdef CONFIG_HOSTAPD_MLME + hostapd_mode_unload(padapter); + #endif + } +#endif + + if (padapter->bup == _TRUE) { + #ifdef CONFIG_XMIT_ACK + if (padapter->xmitpriv.ack_tx) + rtw_ack_tx_done(&padapter->xmitpriv, RTW_SCTX_DONE_DRV_STOP); + #endif + + rtw_intf_stop(padapter); + #ifndef CONFIG_NEW_NETDEV_HDL + rtw_stop_drv_threads(padapter); + #endif + padapter->bup = _FALSE; + } + #ifdef CONFIG_NEW_NETDEV_HDL + rtw_stop_drv_threads(padapter); + #endif + /* cancel timer after thread stop */ + rtw_cancel_all_timer(padapter); +} + +void rtw_drv_free_vir_if(_adapter *padapter) +{ + if (padapter == NULL) + return; + + RTW_INFO(FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(padapter)); + rtw_free_drv_sw(padapter); + + /* TODO: use rtw_os_ndevs_deinit instead at the first stage of driver's dev deinit function */ + rtw_os_ndev_free(padapter); + + rtw_vmfree((u8 *)padapter, sizeof(_adapter)); +} + + +void rtw_drv_stop_vir_ifaces(struct dvobj_priv *dvobj) +{ + int i; + + for (i = VIF_START_ID; i < dvobj->iface_nums; i++) + rtw_drv_stop_vir_if(dvobj->padapters[i]); +} + +void rtw_drv_free_vir_ifaces(struct dvobj_priv *dvobj) +{ + int i; + + for (i = VIF_START_ID; i < dvobj->iface_nums; i++) + rtw_drv_free_vir_if(dvobj->padapters[i]); +} + + +#endif /*end of CONFIG_CONCURRENT_MODE*/ + +/* IPv4, IPv6 IP addr notifier */ +static int rtw_inetaddr_notifier_call(struct notifier_block *nb, + unsigned long action, void *data) +{ + struct in_ifaddr *ifa = (struct in_ifaddr *)data; + struct net_device *ndev; + struct mlme_ext_priv *pmlmeext = NULL; + struct mlme_ext_info *pmlmeinfo = NULL; + _adapter *adapter = NULL; + + if (!ifa || !ifa->ifa_dev || !ifa->ifa_dev->dev) + return NOTIFY_DONE; + + ndev = ifa->ifa_dev->dev; + + if (!is_rtw_ndev(ndev)) + return NOTIFY_DONE; + + adapter = (_adapter *)rtw_netdev_priv(ifa->ifa_dev->dev); + + if (adapter == NULL) + return NOTIFY_DONE; + + pmlmeext = &adapter->mlmeextpriv; + pmlmeinfo = &pmlmeext->mlmext_info; + + switch (action) { + case NETDEV_UP: + _rtw_memcpy(pmlmeinfo->ip_addr, &ifa->ifa_address, + RTW_IP_ADDR_LEN); + RTW_DBG("%s[%s]: up IP: %pI4\n", __func__, + ifa->ifa_label, pmlmeinfo->ip_addr); + break; + case NETDEV_DOWN: + _rtw_memset(pmlmeinfo->ip_addr, 0, RTW_IP_ADDR_LEN); + RTW_DBG("%s[%s]: down IP: %pI4\n", __func__, + ifa->ifa_label, pmlmeinfo->ip_addr); + break; + default: + RTW_DBG("%s: default action\n", __func__); + break; + } + return NOTIFY_DONE; +} + +#ifdef CONFIG_IPV6 +static int rtw_inet6addr_notifier_call(struct notifier_block *nb, + unsigned long action, void *data) +{ + struct inet6_ifaddr *inet6_ifa = data; + struct net_device *ndev; + struct pwrctrl_priv *pwrctl = NULL; + struct mlme_ext_priv *pmlmeext = NULL; + struct mlme_ext_info *pmlmeinfo = NULL; + _adapter *adapter = NULL; + + if (!inet6_ifa || !inet6_ifa->idev || !inet6_ifa->idev->dev) + return NOTIFY_DONE; + + ndev = inet6_ifa->idev->dev; + + if (!is_rtw_ndev(ndev)) + return NOTIFY_DONE; + + adapter = (_adapter *)rtw_netdev_priv(inet6_ifa->idev->dev); + + if (adapter == NULL) + return NOTIFY_DONE; + + pmlmeext = &adapter->mlmeextpriv; + pmlmeinfo = &pmlmeext->mlmext_info; + pwrctl = adapter_to_pwrctl(adapter); + + pmlmeext = &adapter->mlmeextpriv; + pmlmeinfo = &pmlmeext->mlmext_info; + + switch (action) { + case NETDEV_UP: +#ifdef CONFIG_WOWLAN + pwrctl->wowlan_ns_offload_en = _TRUE; +#endif + _rtw_memcpy(pmlmeinfo->ip6_addr, &inet6_ifa->addr, + RTW_IPv6_ADDR_LEN); + RTW_DBG("%s: up IPv6 addrs: %pI6\n", __func__, + pmlmeinfo->ip6_addr); + break; + case NETDEV_DOWN: +#ifdef CONFIG_WOWLAN + pwrctl->wowlan_ns_offload_en = _FALSE; +#endif + _rtw_memset(pmlmeinfo->ip6_addr, 0, RTW_IPv6_ADDR_LEN); + RTW_DBG("%s: down IPv6 addrs: %pI6\n", __func__, + pmlmeinfo->ip6_addr); + break; + default: + RTW_DBG("%s: default action\n", __func__); + break; + } + return NOTIFY_DONE; +} +#endif + +static struct notifier_block rtw_inetaddr_notifier = { + .notifier_call = rtw_inetaddr_notifier_call +}; + +#ifdef CONFIG_IPV6 +static struct notifier_block rtw_inet6addr_notifier = { + .notifier_call = rtw_inet6addr_notifier_call +}; +#endif + +void rtw_inetaddr_notifier_register(void) +{ + RTW_INFO("%s\n", __func__); + register_inetaddr_notifier(&rtw_inetaddr_notifier); +#ifdef CONFIG_IPV6 + register_inet6addr_notifier(&rtw_inet6addr_notifier); +#endif +} + +void rtw_inetaddr_notifier_unregister(void) +{ + RTW_INFO("%s\n", __func__); + unregister_inetaddr_notifier(&rtw_inetaddr_notifier); +#ifdef CONFIG_IPV6 + unregister_inet6addr_notifier(&rtw_inet6addr_notifier); +#endif +} + +int rtw_os_ndevs_register(struct dvobj_priv *dvobj) +{ + int i, status = _SUCCESS; + struct registry_priv *regsty = dvobj_to_regsty(dvobj); + _adapter *adapter; + +#if defined(CONFIG_IOCTL_CFG80211) + if (rtw_cfg80211_dev_res_register(dvobj) != _SUCCESS) { + rtw_warn_on(1); + return _FAIL; + } +#endif + + for (i = 0; i < dvobj->iface_nums; i++) { + + if (i >= CONFIG_IFACE_NUMBER) { + RTW_ERR("%s %d >= CONFIG_IFACE_NUMBER(%d)\n", __func__, i, CONFIG_IFACE_NUMBER); + rtw_warn_on(1); + continue; + } + + adapter = dvobj->padapters[i]; + if (adapter) { + char *name; + + #ifdef CONFIG_RTW_DYNAMIC_NDEV + if (!is_primary_adapter(adapter)) + continue; + #endif + + if (adapter->iface_id == IFACE_ID0) + name = regsty->ifname; + else if (adapter->iface_id == IFACE_ID1) + name = regsty->if2name; + else + name = "wlan%d"; + + status = rtw_os_ndev_register(adapter, name); + + if (status != _SUCCESS) { + rtw_warn_on(1); + break; + } + } + } + + if (status != _SUCCESS) { + for (; i >= 0; i--) { + adapter = dvobj->padapters[i]; + if (adapter) + rtw_os_ndev_unregister(adapter); + } + } + +#if defined(CONFIG_IOCTL_CFG80211) + if (status != _SUCCESS) + rtw_cfg80211_dev_res_unregister(dvobj); +#endif + return status; +} + +void rtw_os_ndevs_unregister(struct dvobj_priv *dvobj) +{ + int i; + _adapter *adapter = NULL; + + for (i = 0; i < dvobj->iface_nums; i++) { + adapter = dvobj->padapters[i]; + + if (adapter == NULL) + continue; + + rtw_os_ndev_unregister(adapter); + } + +#if defined(CONFIG_IOCTL_CFG80211) + rtw_cfg80211_dev_res_unregister(dvobj); +#endif +} + +/** + * rtw_os_ndevs_init - Allocate and register OS layer net devices and relating structures for @dvobj + * @dvobj: the dvobj on which this function applies + * + * Returns: + * _SUCCESS or _FAIL + */ +int rtw_os_ndevs_init(struct dvobj_priv *dvobj) +{ + int ret = _FAIL; + + if (rtw_os_ndevs_alloc(dvobj) != _SUCCESS) + goto exit; + + if (rtw_os_ndevs_register(dvobj) != _SUCCESS) + goto os_ndevs_free; + + ret = _SUCCESS; + +os_ndevs_free: + if (ret != _SUCCESS) + rtw_os_ndevs_free(dvobj); +exit: + return ret; +} + +/** + * rtw_os_ndevs_deinit - Unregister and free OS layer net devices and relating structures for @dvobj + * @dvobj: the dvobj on which this function applies + */ +void rtw_os_ndevs_deinit(struct dvobj_priv *dvobj) +{ + rtw_os_ndevs_unregister(dvobj); + rtw_os_ndevs_free(dvobj); +} + +#ifdef CONFIG_BR_EXT +void netdev_br_init(struct net_device *netdev) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(netdev); + +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35)) + rcu_read_lock(); +#endif + + /* if(check_fwstate(pmlmepriv, WIFI_STATION_STATE|WIFI_ADHOC_STATE) == _TRUE) */ + { + /* struct net_bridge *br = netdev->br_port->br; */ /* ->dev->dev_addr; */ + #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 35)) + if (netdev->br_port) + #else + if (rcu_dereference(adapter->pnetdev->rx_handler_data)) + #endif + { + struct net_device *br_netdev; + + br_netdev = rtw_get_bridge_ndev_by_name(CONFIG_BR_EXT_BRNAME); + if (br_netdev) { + memcpy(adapter->br_mac, br_netdev->dev_addr, ETH_ALEN); + dev_put(br_netdev); + RTW_INFO(FUNC_NDEV_FMT" bind bridge dev "NDEV_FMT"("MAC_FMT")\n" + , FUNC_NDEV_ARG(netdev), NDEV_ARG(br_netdev), MAC_ARG(br_netdev->dev_addr)); + } else { + RTW_INFO(FUNC_NDEV_FMT" can't get bridge dev by name \"%s\"\n" + , FUNC_NDEV_ARG(netdev), CONFIG_BR_EXT_BRNAME); + } + } + + adapter->ethBrExtInfo.addPPPoETag = 1; + } + +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35)) + rcu_read_unlock(); +#endif +} +#endif /* CONFIG_BR_EXT */ + +#ifdef CONFIG_NEW_NETDEV_HDL +int _netdev_open(struct net_device *pnetdev) +{ + uint status; + _adapter *padapter = (_adapter *)rtw_netdev_priv(pnetdev); + struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter); + + RTW_INFO(FUNC_NDEV_FMT" start\n", FUNC_NDEV_ARG(pnetdev)); + + if (!rtw_is_hw_init_completed(padapter)) { // ips + rtw_clr_surprise_removed(padapter); + rtw_clr_drv_stopped(padapter); + RTW_ENABLE_FUNC(padapter, DF_RX_BIT); + RTW_ENABLE_FUNC(padapter, DF_TX_BIT); + status = rtw_hal_init(padapter); + if (status == _FAIL) + goto netdev_open_error; + rtw_led_control(padapter, LED_CTL_NO_LINK); + #ifndef RTW_HALMAC + status = rtw_mi_start_drv_threads(padapter); + if (status == _FAIL) { + RTW_ERR(FUNC_NDEV_FMT "Initialize driver thread failed!\n", FUNC_NDEV_ARG(pnetdev)); + goto netdev_open_error; + } + + rtw_intf_start(GET_PRIMARY_ADAPTER(padapter)); + #endif /* !RTW_HALMAC */ + + { + #ifdef CONFIG_BT_COEXIST_SOCKET_TRX + _adapter *prim_adpt = GET_PRIMARY_ADAPTER(padapter); + + if (prim_adpt && (_TRUE == prim_adpt->EEPROMBluetoothCoexist)) { + rtw_btcoex_init_socket(prim_adpt); + prim_adpt->coex_info.BtMgnt.ExtConfig.HCIExtensionVer = 0x04; + rtw_btcoex_SetHciVersion(prim_adpt, 0x04); + } + #endif /* CONFIG_BT_COEXIST_SOCKET_TRX */ + + _set_timer(&adapter_to_dvobj(padapter)->dynamic_chk_timer, 2000); + + #ifndef CONFIG_IPS_CHECK_IN_WD + rtw_set_pwr_state_check_timer(pwrctrlpriv); + #endif /*CONFIG_IPS_CHECK_IN_WD*/ + } + + } + + /*if (padapter->bup == _FALSE) */ + { + rtw_hal_iface_init(padapter); + + #ifdef CONFIG_RTW_NAPI + if(padapter->napi_state == NAPI_DISABLE) { + napi_enable(&padapter->napi); + padapter->napi_state = NAPI_ENABLE; + } + #endif + + #ifdef CONFIG_IOCTL_CFG80211 + rtw_cfg80211_init_wdev_data(padapter); + #endif + /* rtw_netif_carrier_on(pnetdev); */ /* call this func when rtw_joinbss_event_callback return success */ + rtw_netif_wake_queue(pnetdev); + + #ifdef CONFIG_BR_EXT + if (is_primary_adapter(padapter)) + netdev_br_init(pnetdev); + #endif /* CONFIG_BR_EXT */ + + + padapter->bup = _TRUE; + padapter->net_closed = _FALSE; + padapter->netif_up = _TRUE; + pwrctrlpriv->bips_processing = _FALSE; + } + + RTW_INFO(FUNC_NDEV_FMT" Success (bup=%d)\n", FUNC_NDEV_ARG(pnetdev), padapter->bup); + return 0; + +netdev_open_error: + padapter->bup = _FALSE; + + #ifdef CONFIG_RTW_NAPI + if(padapter->napi_state == NAPI_ENABLE) { + napi_disable(&padapter->napi); + padapter->napi_state = NAPI_DISABLE; + } + #endif + + rtw_netif_carrier_off(pnetdev); + rtw_netif_stop_queue(pnetdev); + + RTW_ERR(FUNC_NDEV_FMT" Failed!! (bup=%d)\n", FUNC_NDEV_ARG(pnetdev), padapter->bup); + + return -1; + +} + +#else +int _netdev_open(struct net_device *pnetdev) +{ + uint status; + _adapter *padapter = (_adapter *)rtw_netdev_priv(pnetdev); + struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter); +#ifdef CONFIG_BT_COEXIST_SOCKET_TRX + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); +#endif /* CONFIG_BT_COEXIST_SOCKET_TRX */ + + + RTW_INFO(FUNC_NDEV_FMT" , bup=%d\n", FUNC_NDEV_ARG(pnetdev), padapter->bup); + + padapter->netif_up = _TRUE; + +#ifdef CONFIG_PLATFORM_INTEL_BYT + rtw_sdio_set_power(1); +#endif /* CONFIG_PLATFORM_INTEL_BYT */ + + if (padapter->bup == _FALSE) { +#ifdef CONFIG_PLATFORM_INTEL_BYT + rtw_macaddr_cfg(adapter_mac_addr(padapter), get_hal_mac_addr(padapter)); +#ifdef CONFIG_MI_WITH_MBSSID_CAM + rtw_mbid_camid_alloc(padapter, adapter_mac_addr(padapter)); +#endif + rtw_init_wifidirect_addrs(padapter, adapter_mac_addr(padapter), adapter_mac_addr(padapter)); + _rtw_memcpy(pnetdev->dev_addr, adapter_mac_addr(padapter), ETH_ALEN); +#endif /* CONFIG_PLATFORM_INTEL_BYT */ + + rtw_clr_surprise_removed(padapter); + rtw_clr_drv_stopped(padapter); + + status = rtw_hal_init(padapter); + if (status == _FAIL) { + goto netdev_open_error; + } +#if 0/*#ifdef CONFIG_MI_WITH_MBSSID_CAM*/ + rtw_hal_set_hwreg(padapter, HW_VAR_MAC_ADDR, adapter_mac_addr(padapter)); /* set mac addr to mac register */ +#endif + + RTW_INFO("MAC Address = "MAC_FMT"\n", MAC_ARG(pnetdev->dev_addr)); + +#ifndef RTW_HALMAC + status = rtw_start_drv_threads(padapter); + if (status == _FAIL) { + RTW_INFO("Initialize driver software resource Failed!\n"); + goto netdev_open_error; + } +#endif /* !RTW_HALMAC */ + +#ifdef CONFIG_RTW_NAPI + if(padapter->napi_state == NAPI_DISABLE) { + napi_enable(&padapter->napi); + padapter->napi_state = NAPI_ENABLE; + } +#endif + +#ifndef RTW_HALMAC + rtw_intf_start(padapter); +#endif /* !RTW_HALMAC */ + +#ifdef CONFIG_IOCTL_CFG80211 + rtw_cfg80211_init_wdev_data(padapter); +#endif + + rtw_led_control(padapter, LED_CTL_NO_LINK); + + padapter->bup = _TRUE; + pwrctrlpriv->bips_processing = _FALSE; + +#ifdef CONFIG_PLATFORM_INTEL_BYT +#ifdef CONFIG_BT_COEXIST + rtw_btcoex_IpsNotify(padapter, IPS_NONE); +#endif /* CONFIG_BT_COEXIST */ +#endif /* CONFIG_PLATFORM_INTEL_BYT */ + } + padapter->net_closed = _FALSE; + + _set_timer(&adapter_to_dvobj(padapter)->dynamic_chk_timer, 2000); + +#ifndef CONFIG_IPS_CHECK_IN_WD + rtw_set_pwr_state_check_timer(pwrctrlpriv); +#endif + + /* rtw_netif_carrier_on(pnetdev); */ /* call this func when rtw_joinbss_event_callback return success */ + rtw_netif_wake_queue(pnetdev); + +#ifdef CONFIG_BR_EXT + netdev_br_init(pnetdev); +#endif /* CONFIG_BR_EXT */ + +#ifdef CONFIG_BT_COEXIST_SOCKET_TRX + if (is_primary_adapter(padapter) && (_TRUE == pHalData->EEPROMBluetoothCoexist)) { + rtw_btcoex_init_socket(padapter); + padapter->coex_info.BtMgnt.ExtConfig.HCIExtensionVer = 0x04; + rtw_btcoex_SetHciVersion(padapter, 0x04); + } else + RTW_INFO("CONFIG_BT_COEXIST: VIRTUAL_ADAPTER\n"); +#endif /* CONFIG_BT_COEXIST_SOCKET_TRX */ + +#ifdef CONFIG_CONCURRENT_MODE + { + _adapter *sec_adapter = adapter_to_dvobj(padapter)->padapters[IFACE_ID1]; + + #ifndef CONFIG_RTW_DYNAMIC_NDEV + if (sec_adapter && (sec_adapter->bup == _FALSE)) + _netdev_vir_if_open(sec_adapter->pnetdev); + #endif + } +#endif + +#ifdef CONFIG_RTW_CFGVENDOR_LLSTATS + pwrctrlpriv->radio_on_start_time = rtw_get_current_time(); + pwrctrlpriv->pwr_saving_start_time = rtw_get_current_time(); + pwrctrlpriv->pwr_saving_time = 0; + pwrctrlpriv->on_time = 0; + pwrctrlpriv->tx_time = 0; + pwrctrlpriv->rx_time = 0; +#endif /* CONFIG_RTW_CFGVEDNOR_LLSTATS */ + + RTW_INFO("-871x_drv - drv_open, bup=%d\n", padapter->bup); + + return 0; + +netdev_open_error: + + padapter->bup = _FALSE; + +#ifdef CONFIG_RTW_NAPI + if(padapter->napi_state == NAPI_ENABLE) { + napi_disable(&padapter->napi); + padapter->napi_state = NAPI_DISABLE; + } +#endif + + rtw_netif_carrier_off(pnetdev); + rtw_netif_stop_queue(pnetdev); + + RTW_INFO("-871x_drv - drv_open fail, bup=%d\n", padapter->bup); + + return -1; + +} +#endif +int netdev_open(struct net_device *pnetdev) +{ + int ret = _FALSE; + _adapter *padapter = (_adapter *)rtw_netdev_priv(pnetdev); + struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter); + + if (pwrctrlpriv->bInSuspend == _TRUE) { + RTW_INFO(" [WARN] "ADPT_FMT" %s failed, bInSuspend=%d\n", ADPT_ARG(padapter), __func__, pwrctrlpriv->bInSuspend); + return 0; + } + + _enter_critical_mutex(&(adapter_to_dvobj(padapter)->hw_init_mutex), NULL); +#ifdef CONFIG_NEW_NETDEV_HDL + ret = _netdev_open(pnetdev); +#else + if (is_primary_adapter(padapter)) + ret = _netdev_open(pnetdev); +#ifdef CONFIG_CONCURRENT_MODE + else + ret = _netdev_vir_if_open(pnetdev); +#endif +#endif + _exit_critical_mutex(&(adapter_to_dvobj(padapter)->hw_init_mutex), NULL); + + +#ifdef CONFIG_AUTO_AP_MODE + if (padapter->iface_id == IFACE_ID2) + rtw_start_auto_ap(padapter); +#endif + + return ret; +} + +#ifdef CONFIG_IPS +int ips_netdrv_open(_adapter *padapter) +{ + int status = _SUCCESS; + /* struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); */ + + padapter->net_closed = _FALSE; + + RTW_INFO("===> %s.........\n", __FUNCTION__); + + + rtw_clr_drv_stopped(padapter); + /* padapter->bup = _TRUE; */ +#ifdef CONFIG_NEW_NETDEV_HDL + if (!rtw_is_hw_init_completed(padapter)) { + status = rtw_hal_init(padapter); + if (status == _FAIL) { + goto netdev_open_error; + } + rtw_mi_hal_iface_init(padapter); + } +#else + status = rtw_hal_init(padapter); + if (status == _FAIL) { + goto netdev_open_error; + } +#endif +#if 0 + rtw_mi_set_mac_addr(padapter); +#endif +#ifndef RTW_HALMAC + rtw_intf_start(padapter); +#endif /* !RTW_HALMAC */ + +#ifndef CONFIG_IPS_CHECK_IN_WD + rtw_set_pwr_state_check_timer(adapter_to_pwrctl(padapter)); +#endif + _set_timer(&adapter_to_dvobj(padapter)->dynamic_chk_timer, 2000); + + return _SUCCESS; + +netdev_open_error: + /* padapter->bup = _FALSE; */ + RTW_INFO("-ips_netdrv_open - drv_open failure, bup=%d\n", padapter->bup); + + return _FAIL; +} + +int rtw_ips_pwr_up(_adapter *padapter) +{ + int result; +#if defined(CONFIG_SWLPS_IN_IPS) || defined(CONFIG_FWLPS_IN_IPS) +#ifdef DBG_CONFIG_ERROR_DETECT + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter); + struct sreset_priv *psrtpriv = &pHalData->srestpriv; +#endif/* #ifdef DBG_CONFIG_ERROR_DETECT */ +#endif /* defined(CONFIG_SWLPS_IN_IPS) || defined(CONFIG_FWLPS_IN_IPS) */ + systime start_time = rtw_get_current_time(); + RTW_INFO("===> rtw_ips_pwr_up..............\n"); + +#if defined(CONFIG_SWLPS_IN_IPS) || defined(CONFIG_FWLPS_IN_IPS) +#ifdef DBG_CONFIG_ERROR_DETECT + if (psrtpriv->silent_reset_inprogress == _TRUE) +#endif/* #ifdef DBG_CONFIG_ERROR_DETECT */ +#endif /* defined(CONFIG_SWLPS_IN_IPS) || defined(CONFIG_FWLPS_IN_IPS) */ + rtw_reset_drv_sw(padapter); + + result = ips_netdrv_open(padapter); + + rtw_led_control(padapter, LED_CTL_NO_LINK); + + RTW_INFO("<=== rtw_ips_pwr_up.............. in %dms\n", rtw_get_passing_time_ms(start_time)); + return result; + +} + +void rtw_ips_pwr_down(_adapter *padapter) +{ + systime start_time = rtw_get_current_time(); + RTW_INFO("===> rtw_ips_pwr_down...................\n"); + + padapter->net_closed = _TRUE; + + rtw_led_control(padapter, LED_CTL_POWER_OFF); + + rtw_ips_dev_unload(padapter); + RTW_INFO("<=== rtw_ips_pwr_down..................... in %dms\n", rtw_get_passing_time_ms(start_time)); +} +#endif +void rtw_ips_dev_unload(_adapter *padapter) +{ +#if defined(CONFIG_SWLPS_IN_IPS) || defined(CONFIG_FWLPS_IN_IPS) +#ifdef DBG_CONFIG_ERROR_DETECT + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter); + struct sreset_priv *psrtpriv = &pHalData->srestpriv; +#endif/* #ifdef DBG_CONFIG_ERROR_DETECT */ +#endif /* defined(CONFIG_SWLPS_IN_IPS) || defined(CONFIG_FWLPS_IN_IPS) */ + RTW_INFO("====> %s...\n", __FUNCTION__); + + +#if defined(CONFIG_SWLPS_IN_IPS) || defined(CONFIG_FWLPS_IN_IPS) +#ifdef DBG_CONFIG_ERROR_DETECT + if (psrtpriv->silent_reset_inprogress == _TRUE) +#endif /* #ifdef DBG_CONFIG_ERROR_DETECT */ +#endif /* defined(CONFIG_SWLPS_IN_IPS) || defined(CONFIG_FWLPS_IN_IPS) */ + { + rtw_hal_set_hwreg(padapter, HW_VAR_FIFO_CLEARN_UP, 0); + rtw_intf_stop(padapter); + } + + if (!rtw_is_surprise_removed(padapter)) + rtw_hal_deinit(padapter); + +} +#ifdef CONFIG_NEW_NETDEV_HDL +int _pm_netdev_open(_adapter *padapter) +{ + uint status; + struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter); + struct net_device *pnetdev = padapter->pnetdev; + + RTW_INFO(FUNC_NDEV_FMT" start\n", FUNC_NDEV_ARG(pnetdev)); + + if (!rtw_is_hw_init_completed(padapter)) { // ips + rtw_clr_surprise_removed(padapter); + rtw_clr_drv_stopped(padapter); + status = rtw_hal_init(padapter); + if (status == _FAIL) + goto netdev_open_error; + rtw_led_control(padapter, LED_CTL_NO_LINK); + #ifndef RTW_HALMAC + status = rtw_mi_start_drv_threads(padapter); + if (status == _FAIL) { + RTW_ERR(FUNC_NDEV_FMT "Initialize driver thread failed!\n", FUNC_NDEV_ARG(pnetdev)); + goto netdev_open_error; + } + + rtw_intf_start(GET_PRIMARY_ADAPTER(padapter)); + #endif /* !RTW_HALMAC */ + + { + _set_timer(&adapter_to_dvobj(padapter)->dynamic_chk_timer, 2000); + + #ifndef CONFIG_IPS_CHECK_IN_WD + rtw_set_pwr_state_check_timer(pwrctrlpriv); + #endif /*CONFIG_IPS_CHECK_IN_WD*/ + } + + } + + /*if (padapter->bup == _FALSE) */ + { + rtw_hal_iface_init(padapter); + + padapter->bup = _TRUE; + padapter->net_closed = _FALSE; + padapter->netif_up = _TRUE; + pwrctrlpriv->bips_processing = _FALSE; + } + + RTW_INFO(FUNC_NDEV_FMT" Success (bup=%d)\n", FUNC_NDEV_ARG(pnetdev), padapter->bup); + return 0; + +netdev_open_error: + padapter->bup = _FALSE; + + rtw_netif_carrier_off(pnetdev); + rtw_netif_stop_queue(pnetdev); + + RTW_ERR(FUNC_NDEV_FMT" Failed!! (bup=%d)\n", FUNC_NDEV_ARG(pnetdev), padapter->bup); + + return -1; + +} +int _mi_pm_netdev_open(struct net_device *pnetdev) +{ + int i; + int status = 0; + _adapter *padapter = (_adapter *)rtw_netdev_priv(pnetdev); + _adapter *iface; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (iface->netif_up) { + status = _pm_netdev_open(iface); + if (status == -1) { + RTW_ERR("%s failled\n", __func__); + break; + } + } + } + + return status; +} +#endif /*CONFIG_NEW_NETDEV_HDL*/ +int pm_netdev_open(struct net_device *pnetdev, u8 bnormal) +{ + int status = 0; + + _adapter *padapter = (_adapter *)rtw_netdev_priv(pnetdev); + + if (_TRUE == bnormal) { + _enter_critical_mutex(&(adapter_to_dvobj(padapter)->hw_init_mutex), NULL); + #ifdef CONFIG_NEW_NETDEV_HDL + status = _mi_pm_netdev_open(pnetdev); + #else + status = _netdev_open(pnetdev); + #endif + _exit_critical_mutex(&(adapter_to_dvobj(padapter)->hw_init_mutex), NULL); + } +#ifdef CONFIG_IPS + else + status = (_SUCCESS == ips_netdrv_open(padapter)) ? (0) : (-1); +#endif + + return status; +} +#ifdef CONFIG_CLIENT_PORT_CFG +extern void rtw_hw_client_port_release(_adapter *adapter); +#endif +static int netdev_close(struct net_device *pnetdev) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(pnetdev); + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(padapter); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; +#ifdef CONFIG_BT_COEXIST_SOCKET_TRX + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); +#endif /* CONFIG_BT_COEXIST_SOCKET_TRX */ + + RTW_INFO(FUNC_NDEV_FMT" , bup=%d\n", FUNC_NDEV_ARG(pnetdev), padapter->bup); +#ifndef CONFIG_PLATFORM_INTEL_BYT + padapter->net_closed = _TRUE; + padapter->netif_up = _FALSE; + pmlmepriv->LinkDetectInfo.bBusyTraffic = _FALSE; + +#ifdef CONFIG_CLIENT_PORT_CFG + if (MLME_IS_STA(padapter)) + rtw_hw_client_port_release(padapter); +#endif + /* if (!rtw_is_hw_init_completed(padapter)) { + RTW_INFO("(1)871x_drv - drv_close, bup=%d, hw_init_completed=%s\n", padapter->bup, rtw_is_hw_init_completed(padapter)?"_TRUE":"_FALSE"); + + rtw_set_drv_stopped(padapter); + + rtw_dev_unload(padapter); + } + else*/ + if (pwrctl->rf_pwrstate == rf_on) { + RTW_INFO("(2)871x_drv - drv_close, bup=%d, hw_init_completed=%s\n", padapter->bup, rtw_is_hw_init_completed(padapter) ? "_TRUE" : "_FALSE"); + + /* s1. */ + if (pnetdev) + rtw_netif_stop_queue(pnetdev); + +#ifndef CONFIG_RTW_ANDROID + /* s2. */ + LeaveAllPowerSaveMode(padapter); + rtw_disassoc_cmd(padapter, 500, RTW_CMDF_WAIT_ACK); + /* s2-2. indicate disconnect to os */ + rtw_indicate_disconnect(padapter, 0, _FALSE); + /* s2-3. */ + rtw_free_assoc_resources_cmd(padapter, _TRUE, RTW_CMDF_WAIT_ACK); + /* s2-4. */ + rtw_free_network_queue(padapter, _TRUE); +#endif + } + +#ifdef CONFIG_BR_EXT + /* if (OPMODE & (WIFI_STATION_STATE | WIFI_ADHOC_STATE)) */ + { + /* void nat25_db_cleanup(_adapter *priv); */ + nat25_db_cleanup(padapter); + } +#endif /* CONFIG_BR_EXT */ + +#ifdef CONFIG_P2P + if (!rtw_p2p_chk_role(&padapter->wdinfo, P2P_ROLE_DISABLE)) + rtw_p2p_enable(padapter, P2P_ROLE_DISABLE); +#endif /* CONFIG_P2P */ + + rtw_scan_abort(padapter); /* stop scanning process before wifi is going to down */ +#ifdef CONFIG_IOCTL_CFG80211 + rtw_cfg80211_wait_scan_req_empty(padapter, 200); + adapter_wdev_data(padapter)->bandroid_scan = _FALSE; + /* padapter->rtw_wdev->iftype = NL80211_IFTYPE_MONITOR; */ /* set this at the end */ +#endif /* CONFIG_IOCTL_CFG80211 */ + +#ifdef CONFIG_WAPI_SUPPORT + rtw_wapi_disable_tx(padapter); +#endif +#ifdef CONFIG_BT_COEXIST_SOCKET_TRX + if (is_primary_adapter(padapter) && (_TRUE == pHalData->EEPROMBluetoothCoexist)) + rtw_btcoex_close_socket(padapter); + else + RTW_INFO("CONFIG_BT_COEXIST: VIRTUAL_ADAPTER\n"); +#endif /* CONFIG_BT_COEXIST_SOCKET_TRX */ +#else /* !CONFIG_PLATFORM_INTEL_BYT */ + + if (pwrctl->bInSuspend == _TRUE) { + RTW_INFO("+871x_drv - drv_close, bInSuspend=%d\n", pwrctl->bInSuspend); + return 0; + } + + rtw_scan_abort(padapter); /* stop scanning process before wifi is going to down */ +#ifdef CONFIG_IOCTL_CFG80211 + rtw_cfg80211_wait_scan_req_empty(padapter, 200); +#endif + + RTW_INFO("netdev_close, bips_processing=%d\n", pwrctl->bips_processing); + while (pwrctl->bips_processing == _TRUE) /* waiting for ips_processing done before call rtw_dev_unload() */ + rtw_msleep_os(1); + + rtw_dev_unload(padapter); + rtw_sdio_set_power(0); + +#endif /* !CONFIG_PLATFORM_INTEL_BYT */ + + RTW_INFO("-871x_drv - drv_close, bup=%d\n", padapter->bup); + + return 0; + +} + +int pm_netdev_close(struct net_device *pnetdev, u8 bnormal) +{ + int status = 0; + + status = netdev_close(pnetdev); + + return status; +} + +void rtw_ndev_destructor(struct net_device *ndev) +{ + RTW_INFO(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(ndev)); + +#ifdef CONFIG_IOCTL_CFG80211 + if (ndev->ieee80211_ptr) + rtw_mfree((u8 *)ndev->ieee80211_ptr, sizeof(struct wireless_dev)); +#endif + free_netdev(ndev); +} + +#ifdef CONFIG_ARP_KEEP_ALIVE +struct route_info { + struct in_addr dst_addr; + struct in_addr src_addr; + struct in_addr gateway; + unsigned int dev_index; +}; + +static void parse_routes(struct nlmsghdr *nl_hdr, struct route_info *rt_info) +{ + struct rtmsg *rt_msg; + struct rtattr *rt_attr; + int rt_len; + + rt_msg = (struct rtmsg *) NLMSG_DATA(nl_hdr); + if ((rt_msg->rtm_family != AF_INET) || (rt_msg->rtm_table != RT_TABLE_MAIN)) + return; + + rt_attr = (struct rtattr *) RTM_RTA(rt_msg); + rt_len = RTM_PAYLOAD(nl_hdr); + + for (; RTA_OK(rt_attr, rt_len); rt_attr = RTA_NEXT(rt_attr, rt_len)) { + switch (rt_attr->rta_type) { + case RTA_OIF: + rt_info->dev_index = *(int *) RTA_DATA(rt_attr); + break; + case RTA_GATEWAY: + rt_info->gateway.s_addr = *(u_int *) RTA_DATA(rt_attr); + break; + case RTA_PREFSRC: + rt_info->src_addr.s_addr = *(u_int *) RTA_DATA(rt_attr); + break; + case RTA_DST: + rt_info->dst_addr.s_addr = *(u_int *) RTA_DATA(rt_attr); + break; + } + } +} + +static int route_dump(u32 *gw_addr , int *gw_index) +{ + int err = 0; + struct socket *sock; + struct { + struct nlmsghdr nlh; + struct rtgenmsg g; + } req; + struct msghdr msg; + struct iovec iov; + struct sockaddr_nl nladdr; + mm_segment_t oldfs; + char *pg; + int size = 0; + + err = sock_create(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE, &sock); + if (err) { + printk(": Could not create a datagram socket, error = %d\n", -ENXIO); + return err; + } + + memset(&nladdr, 0, sizeof(nladdr)); + nladdr.nl_family = AF_NETLINK; + + req.nlh.nlmsg_len = sizeof(req); + req.nlh.nlmsg_type = RTM_GETROUTE; + req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST; + req.nlh.nlmsg_pid = 0; + req.g.rtgen_family = AF_INET; + + iov.iov_base = &req; + iov.iov_len = sizeof(req); + + msg.msg_name = &nladdr; + msg.msg_namelen = sizeof(nladdr); +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)) + /* referece:sock_xmit in kernel code + * WRITE for sock_sendmsg, READ for sock_recvmsg + * third parameter for msg_iovlen + * last parameter for iov_len + */ + iov_iter_init(&msg.msg_iter, WRITE, &iov, 1, sizeof(req)); +#else + msg.msg_iov = &iov; + msg.msg_iovlen = 1; +#endif + msg.msg_control = NULL; + msg.msg_controllen = 0; + msg.msg_flags = MSG_DONTWAIT; + + oldfs = get_fs(); + set_fs(KERNEL_DS); +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)) + err = sock_sendmsg(sock, &msg); +#else + err = sock_sendmsg(sock, &msg, sizeof(req)); +#endif + set_fs(oldfs); + + if (err < 0) + goto out_sock; + + pg = (char *) __get_free_page(GFP_KERNEL); + if (pg == NULL) { + err = -ENOMEM; + goto out_sock; + } + +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) +restart: +#endif + + for (;;) { + struct nlmsghdr *h; + + iov.iov_base = pg; + iov.iov_len = PAGE_SIZE; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)) + iov_iter_init(&msg.msg_iter, READ, &iov, 1, PAGE_SIZE); +#endif + + oldfs = get_fs(); + set_fs(KERNEL_DS); +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)) + err = sock_recvmsg(sock, &msg, MSG_DONTWAIT); +#else + err = sock_recvmsg(sock, &msg, PAGE_SIZE, MSG_DONTWAIT); +#endif + set_fs(oldfs); + + if (err < 0) + goto out_sock_pg; + + if (msg.msg_flags & MSG_TRUNC) { + err = -ENOBUFS; + goto out_sock_pg; + } + + h = (struct nlmsghdr *) pg; + + while (NLMSG_OK(h, err)) { + struct route_info rt_info; + if (h->nlmsg_type == NLMSG_DONE) { + err = 0; + goto done; + } + + if (h->nlmsg_type == NLMSG_ERROR) { + struct nlmsgerr *errm = (struct nlmsgerr *) NLMSG_DATA(h); + err = errm->error; + printk("NLMSG error: %d\n", errm->error); + goto done; + } + + if (h->nlmsg_type == RTM_GETROUTE) + printk("RTM_GETROUTE: NLMSG: %d\n", h->nlmsg_type); + if (h->nlmsg_type != RTM_NEWROUTE) { + printk("NLMSG: %d\n", h->nlmsg_type); + err = -EINVAL; + goto done; + } + + memset(&rt_info, 0, sizeof(struct route_info)); + parse_routes(h, &rt_info); + if (!rt_info.dst_addr.s_addr && rt_info.gateway.s_addr && rt_info.dev_index) { + *gw_addr = rt_info.gateway.s_addr; + *gw_index = rt_info.dev_index; + + } + h = NLMSG_NEXT(h, err); + } + + if (err) { + printk("!!!Remnant of size %d %d %d\n", err, h->nlmsg_len, h->nlmsg_type); + err = -EINVAL; + break; + } + } + +done: +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) + if (!err && req.g.rtgen_family == AF_INET) { + req.g.rtgen_family = AF_INET6; + + iov.iov_base = &req; + iov.iov_len = sizeof(req); + + msg.msg_name = &nladdr; + msg.msg_namelen = sizeof(nladdr); +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)) + iov_iter_init(&msg.msg_iter, WRITE, &iov, 1, sizeof(req)); +#else + msg.msg_iov = &iov; + msg.msg_iovlen = 1; +#endif + msg.msg_control = NULL; + msg.msg_controllen = 0; + msg.msg_flags = MSG_DONTWAIT; + + oldfs = get_fs(); + set_fs(KERNEL_DS); +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)) + err = sock_sendmsg(sock, &msg); +#else + err = sock_sendmsg(sock, &msg, sizeof(req)); +#endif + set_fs(oldfs); + + if (err > 0) + goto restart; + } +#endif + +out_sock_pg: + free_page((unsigned long) pg); + +out_sock: + sock_release(sock); + return err; +} + +static int arp_query(unsigned char *haddr, u32 paddr, + struct net_device *dev) +{ + struct neighbour *neighbor_entry; + int ret = 0; + + neighbor_entry = neigh_lookup(&arp_tbl, &paddr, dev); + + if (neighbor_entry != NULL) { + neighbor_entry->used = jiffies; + if (neighbor_entry->nud_state & NUD_VALID) { + _rtw_memcpy(haddr, neighbor_entry->ha, dev->addr_len); + ret = 1; + } + neigh_release(neighbor_entry); + } + return ret; +} + +static int get_defaultgw(u32 *ip_addr , char mac[]) +{ + int gw_index = 0; /* oif device index */ + struct net_device *gw_dev = NULL; /* oif device */ + + route_dump(ip_addr, &gw_index); + + if (!(*ip_addr) || !gw_index) { + /* RTW_INFO("No default GW\n"); */ + return -1; + } + + gw_dev = dev_get_by_index(&init_net, gw_index); + + if (gw_dev == NULL) { + /* RTW_INFO("get Oif Device Fail\n"); */ + return -1; + } + + if (!arp_query(mac, *ip_addr, gw_dev)) { + /* RTW_INFO( "arp query failed\n"); */ + dev_put(gw_dev); + return -1; + + } + dev_put(gw_dev); + + return 0; +} + +int rtw_gw_addr_query(_adapter *padapter) +{ + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(padapter); + u32 gw_addr = 0; /* default gw address */ + unsigned char gw_mac[32] = {0}; /* default gw mac */ + int i; + int res; + + res = get_defaultgw(&gw_addr, gw_mac); + if (!res) { + pmlmepriv->gw_ip[0] = gw_addr & 0xff; + pmlmepriv->gw_ip[1] = (gw_addr & 0xff00) >> 8; + pmlmepriv->gw_ip[2] = (gw_addr & 0xff0000) >> 16; + pmlmepriv->gw_ip[3] = (gw_addr & 0xff000000) >> 24; + _rtw_memcpy(pmlmepriv->gw_mac_addr, gw_mac, ETH_ALEN); + RTW_INFO("%s Gateway Mac:\t" MAC_FMT "\n", __FUNCTION__, MAC_ARG(pmlmepriv->gw_mac_addr)); + RTW_INFO("%s Gateway IP:\t" IP_FMT "\n", __FUNCTION__, IP_ARG(pmlmepriv->gw_ip)); + } else + RTW_INFO("Get Gateway IP/MAC fail!\n"); + + return res; +} +#endif + +void rtw_dev_unload(PADAPTER padapter) +{ + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(padapter); + struct dvobj_priv *pobjpriv = padapter->dvobj; + struct debug_priv *pdbgpriv = &pobjpriv->drv_dbg; + struct cmd_priv *pcmdpriv = &padapter->cmdpriv; + + if (padapter->bup == _TRUE) { + RTW_INFO("==> "FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(padapter)); + +#ifdef CONFIG_WOWLAN +#ifdef CONFIG_GPIO_WAKEUP + /*default wake up pin change to BT*/ + RTW_INFO("%s:default wake up pin change to BT\n", __FUNCTION__); + rtw_hal_switch_gpio_wl_ctrl(padapter, pwrctl->wowlan_gpio_index, _FALSE); +#endif /* CONFIG_GPIO_WAKEUP */ +#endif /* CONFIG_WOWLAN */ + + rtw_set_drv_stopped(padapter); +#ifdef CONFIG_XMIT_ACK + if (padapter->xmitpriv.ack_tx) + rtw_ack_tx_done(&padapter->xmitpriv, RTW_SCTX_DONE_DRV_STOP); +#endif + + rtw_intf_stop(padapter); + + rtw_stop_drv_threads(padapter); + + if (ATOMIC_READ(&(pcmdpriv->cmdthd_running)) == _TRUE) { + RTW_ERR("cmd_thread not stop !!\n"); + rtw_warn_on(1); + } + + /* check the status of IPS */ + if (rtw_hal_check_ips_status(padapter) == _TRUE || pwrctl->rf_pwrstate == rf_off) { /* check HW status and SW state */ + RTW_PRINT("%s: driver in IPS-FWLPS\n", __func__); + pdbgpriv->dbg_dev_unload_inIPS_cnt++; + } else + RTW_PRINT("%s: driver not in IPS\n", __func__); + + if (!rtw_is_surprise_removed(padapter)) { +#ifdef CONFIG_BT_COEXIST + rtw_btcoex_IpsNotify(padapter, pwrctl->ips_mode_req); +#endif +#ifdef CONFIG_WOWLAN + if (pwrctl->bSupportRemoteWakeup == _TRUE && + pwrctl->wowlan_mode == _TRUE) + RTW_PRINT("%s bSupportRemoteWakeup==_TRUE do not run rtw_hal_deinit()\n", __FUNCTION__); + else +#endif + { + /* amy modify 20120221 for power seq is different between driver open and ips */ + rtw_hal_deinit(padapter); + } + rtw_set_surprise_removed(padapter); + } + + padapter->bup = _FALSE; + + RTW_INFO("<== "FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(padapter)); + } else { + RTW_INFO("%s: bup==_FALSE\n", __FUNCTION__); + } + rtw_cancel_all_timer(padapter); +} + +int rtw_suspend_free_assoc_resource(_adapter *padapter) +{ + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; +#ifdef CONFIG_P2P + struct wifidirect_info *pwdinfo = &padapter->wdinfo; +#endif /* CONFIG_P2P */ + + RTW_INFO("==> "FUNC_ADPT_FMT" entry....\n", FUNC_ADPT_ARG(padapter)); + + if (rtw_chk_roam_flags(padapter, RTW_ROAM_ON_RESUME)) { + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) + && check_fwstate(pmlmepriv, WIFI_ASOC_STATE) + #ifdef CONFIG_P2P + && (rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE) + #if defined(CONFIG_IOCTL_CFG80211) && RTW_P2P_GROUP_INTERFACE + || rtw_p2p_chk_role(pwdinfo, P2P_ROLE_DEVICE) + #endif + ) + #endif /* CONFIG_P2P */ + ) { + RTW_INFO("%s %s(" MAC_FMT "), length:%d assoc_ssid.length:%d\n", __FUNCTION__, + pmlmepriv->cur_network.network.Ssid.Ssid, + MAC_ARG(pmlmepriv->cur_network.network.MacAddress), + pmlmepriv->cur_network.network.Ssid.SsidLength, + pmlmepriv->assoc_ssid.SsidLength); + rtw_set_to_roam(padapter, 1); + } + } + + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) && check_fwstate(pmlmepriv, WIFI_ASOC_STATE)) { + rtw_disassoc_cmd(padapter, 0, RTW_CMDF_DIRECTLY); + /* s2-2. indicate disconnect to os */ + rtw_indicate_disconnect(padapter, 0, _FALSE); + } +#ifdef CONFIG_AP_MODE + else if (MLME_IS_AP(padapter) || MLME_IS_MESH(padapter)) + rtw_sta_flush(padapter, _TRUE); +#endif + + /* s2-3. */ + rtw_free_assoc_resources(padapter, _TRUE); + + /* s2-4. */ + rtw_free_network_queue(padapter, _TRUE); + + if (check_fwstate(pmlmepriv, WIFI_UNDER_SURVEY)) { + RTW_PRINT("%s: fw_under_survey\n", __func__); + rtw_indicate_scan_done(padapter, 1); + clr_fwstate(pmlmepriv, WIFI_UNDER_SURVEY); + } + + if (check_fwstate(pmlmepriv, WIFI_UNDER_LINKING) == _TRUE) { + RTW_PRINT("%s: fw_under_linking\n", __FUNCTION__); + rtw_indicate_disconnect(padapter, 0, _FALSE); + } + + RTW_INFO("<== "FUNC_ADPT_FMT" exit....\n", FUNC_ADPT_ARG(padapter)); + return _SUCCESS; +} + +#ifdef CONFIG_WOWLAN +int rtw_suspend_wow(_adapter *padapter) +{ + u8 ch, bw, offset; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + struct wowlan_ioctl_param poidparam; + int ret = _SUCCESS; + u8 en = _TRUE, i; + struct registry_priv *registry_par = &padapter->registrypriv; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + _adapter *iface = NULL; + struct hal_spec_t *hal_spec = GET_HAL_SPEC(padapter); + + RTW_INFO("==> "FUNC_ADPT_FMT" entry....\n", FUNC_ADPT_ARG(padapter)); + + + RTW_INFO("wowlan_mode: %d\n", pwrpriv->wowlan_mode); + RTW_INFO("wowlan_pno_enable: %d\n", pwrpriv->wowlan_pno_enable); +#ifdef CONFIG_P2P_WOWLAN + RTW_INFO("wowlan_p2p_enable: %d\n", pwrpriv->wowlan_p2p_enable); +#endif + + if (pwrpriv->wowlan_mode == _TRUE) { + rtw_mi_netif_stop_queue(padapter); + #ifdef CONFIG_CONCURRENT_MODE + rtw_mi_buddy_netif_carrier_off(padapter); + #endif + + /* 0. Power off LED */ + rtw_led_control(padapter, LED_CTL_POWER_OFF); + +#if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) + /* 2.only for SDIO disable interrupt */ + rtw_intf_stop(padapter); + + /* 2.1 clean interrupt */ + rtw_hal_clear_interrupt(padapter); +#endif /* CONFIG_SDIO_HCI */ + + /* enable ac lifetime during scan to avoid txfifo not empty. */ + dvobj->lifetime_en = rtw_read8(padapter, 0x426); + dvobj->pkt_lifetime = rtw_read32(padapter, 0x4c0); + rtw_write8(padapter, 0x426, rtw_read8(padapter, 0x426) | 0x0f); + if(hal_spec->tx_aclt_unit_factor == 1) { + rtw_write16(padapter, 0x4c0, 0x1000); // unit: 32us. 131ms + rtw_write16(padapter, 0x4c0 + 2 , 0x1000); // unit: 32us. 131ms + } else { + rtw_write16(padapter, 0x4c0, 0x0200); // unit: 256us. 131ms + rtw_write16(padapter, 0x4c0 + 2 , 0x0200); // unit: 256us. 131ms + } + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if ((iface) && rtw_is_adapter_up(iface)) { + rtw_write_port_cancel(iface); + RTW_INFO(ADPT_FMT " write port cancel\n", ADPT_ARG(iface)); + } + } + RTW_INFO("lifetime_en=%x, pkt_lifetime=%x\n", rtw_read8(padapter, 0x426), rtw_read32(padapter, 0x4c0)); + rtw_msleep_os(200); + + /* 1. stop thread */ + rtw_set_drv_stopped(padapter); /*for stop thread*/ + rtw_mi_stop_drv_threads(padapter); + + rtw_clr_drv_stopped(padapter); /*for 32k command*/ + + /* #ifdef CONFIG_LPS */ + /* rtw_set_ps_mode(padapter, PS_MODE_ACTIVE, 0, 0, "WOWLAN"); */ + /* #endif */ + + #ifdef CONFIG_SDIO_HCI + /* 2.2 free irq */ + #if !(CONFIG_RTW_SDIO_KEEP_IRQ) + sdio_free_irq(adapter_to_dvobj(padapter)); + #endif + #endif/*CONFIG_SDIO_HCI*/ + +#ifdef CONFIG_RUNTIME_PORT_SWITCH + if (rtw_port_switch_chk(padapter)) { + RTW_INFO(" ### PORT SWITCH ###\n"); + rtw_hal_set_hwreg(padapter, HW_VAR_PORT_SWITCH, NULL); + } +#endif + if(registry_par->suspend_type == FW_IPS_WRC) + rtw_hal_set_hwreg(padapter, HW_VAR_VENDOR_WOW_MODE, &en); +#ifdef CONFIG_LPS + rtw_wow_lps_level_decide(padapter, _TRUE); +#endif + poidparam.subcode = WOWLAN_ENABLE; + rtw_hal_set_hwreg(padapter, HW_VAR_WOWLAN, (u8 *)&poidparam); + if (rtw_chk_roam_flags(padapter, RTW_ROAM_ON_RESUME)) { + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) + && check_fwstate(pmlmepriv, WIFI_ASOC_STATE)) { + RTW_INFO("%s %s(" MAC_FMT "), length:%d assoc_ssid.length:%d\n", __FUNCTION__, + pmlmepriv->cur_network.network.Ssid.Ssid, + MAC_ARG(pmlmepriv->cur_network.network.MacAddress), + pmlmepriv->cur_network.network.Ssid.SsidLength, + pmlmepriv->assoc_ssid.SsidLength); + + rtw_set_to_roam(padapter, 0); + } + } + + RTW_PRINT("%s: wowmode suspending\n", __func__); + + if (check_fwstate(pmlmepriv, WIFI_UNDER_SURVEY) == _TRUE) { + RTW_PRINT("%s: fw_under_survey\n", __func__); + rtw_indicate_scan_done(padapter, 1); + clr_fwstate(pmlmepriv, WIFI_UNDER_SURVEY); + } + + if (rtw_mi_check_status(padapter, MI_LINKED)) { + ch = rtw_mi_get_union_chan(padapter); + bw = rtw_mi_get_union_bw(padapter); + offset = rtw_mi_get_union_offset(padapter); + RTW_INFO(FUNC_ADPT_FMT" back to linked/linking union - ch:%u, bw:%u, offset:%u\n", + FUNC_ADPT_ARG(padapter), ch, bw, offset); + set_channel_bwmode(padapter, ch, offset, bw); + } + +#ifdef CONFIG_CONCURRENT_MODE + rtw_mi_buddy_suspend_free_assoc_resource(padapter); +#endif + +#ifdef CONFIG_BT_COEXIST + rtw_btcoex_SuspendNotify(padapter, BTCOEX_SUSPEND_STATE_SUSPEND_KEEP_ANT); +#endif + + if (pwrpriv->wowlan_pno_enable) { + RTW_PRINT("%s: pno: %d\n", __func__, + pwrpriv->wowlan_pno_enable); +#ifdef CONFIG_FWLPS_IN_IPS + rtw_set_fw_in_ips_mode(padapter, _TRUE); +#endif + } +#ifdef CONFIG_LPS + else { + if(pwrpriv->wowlan_power_mgmt != PS_MODE_ACTIVE) { + rtw_set_ps_mode(padapter, pwrpriv->wowlan_power_mgmt, 0, 0, "WOWLAN"); + } + } +#endif /* #ifdef CONFIG_LPS */ + + } else + RTW_PRINT("%s: ### ERROR ### wowlan_mode=%d\n", __FUNCTION__, pwrpriv->wowlan_mode); + RTW_INFO("<== "FUNC_ADPT_FMT" exit....\n", FUNC_ADPT_ARG(padapter)); + return ret; +} +#endif /* #ifdef CONFIG_WOWLAN */ + +#ifdef CONFIG_AP_WOWLAN +int rtw_suspend_ap_wow(_adapter *padapter) +{ + u8 ch, bw, offset; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + struct wowlan_ioctl_param poidparam; + int ret = _SUCCESS; + + RTW_INFO("==> "FUNC_ADPT_FMT" entry....\n", FUNC_ADPT_ARG(padapter)); + + pwrpriv->wowlan_ap_mode = _TRUE; + + RTW_INFO("wowlan_ap_mode: %d\n", pwrpriv->wowlan_ap_mode); + + rtw_mi_netif_stop_queue(padapter); + + /* 0. Power off LED */ + rtw_led_control(padapter, LED_CTL_POWER_OFF); +#ifdef CONFIG_SDIO_HCI + /* 2.only for SDIO disable interrupt*/ + rtw_intf_stop(padapter); + + /* 2.1 clean interrupt */ + rtw_hal_clear_interrupt(padapter); +#endif /* CONFIG_SDIO_HCI */ + + /* 1. stop thread */ + rtw_set_drv_stopped(padapter); /*for stop thread*/ + rtw_mi_stop_drv_threads(padapter); + rtw_clr_drv_stopped(padapter); /*for 32k command*/ + + #ifdef CONFIG_SDIO_HCI + /* 2.2 free irq */ + #if !(CONFIG_RTW_SDIO_KEEP_IRQ) + sdio_free_irq(adapter_to_dvobj(padapter)); + #endif + #endif/*CONFIG_SDIO_HCI*/ + +#ifdef CONFIG_RUNTIME_PORT_SWITCH + if (rtw_port_switch_chk(padapter)) { + RTW_INFO(" ### PORT SWITCH ###\n"); + rtw_hal_set_hwreg(padapter, HW_VAR_PORT_SWITCH, NULL); + } +#endif + + rtw_wow_lps_level_decide(padapter, _TRUE); + poidparam.subcode = WOWLAN_AP_ENABLE; + rtw_hal_set_hwreg(padapter, HW_VAR_WOWLAN, (u8 *)&poidparam); + + RTW_PRINT("%s: wowmode suspending\n", __func__); + + if (rtw_mi_check_status(padapter, MI_LINKED)) { + ch = rtw_mi_get_union_chan(padapter); + bw = rtw_mi_get_union_bw(padapter); + offset = rtw_mi_get_union_offset(padapter); + RTW_INFO("back to linked/linking union - ch:%u, bw:%u, offset:%u\n", ch, bw, offset); + set_channel_bwmode(padapter, ch, offset, bw); + } + + /*FOR ONE AP - TODO :Multi-AP*/ + { + int i; + _adapter *iface; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if ((iface) && rtw_is_adapter_up(iface)) { + if (check_fwstate(&iface->mlmepriv, WIFI_AP_STATE | WIFI_MESH_STATE) == _FALSE) + rtw_suspend_free_assoc_resource(iface); + } + } + + } + +#ifdef CONFIG_BT_COEXIST + rtw_btcoex_SuspendNotify(padapter, BTCOEX_SUSPEND_STATE_SUSPEND_KEEP_ANT); +#endif + +#ifdef CONFIG_LPS + if(pwrpriv->wowlan_power_mgmt != PS_MODE_ACTIVE) { + rtw_set_ps_mode(padapter, pwrpriv->wowlan_power_mgmt, 0, 0, "AP-WOWLAN"); + } +#endif + + RTW_INFO("<== "FUNC_ADPT_FMT" exit....\n", FUNC_ADPT_ARG(padapter)); + return ret; +} +#endif /* CONFIG_AP_WOWLAN */ + + +int rtw_suspend_normal(_adapter *padapter) +{ + int ret = _SUCCESS; + + RTW_INFO("==> "FUNC_ADPT_FMT" entry....\n", FUNC_ADPT_ARG(padapter)); + +#ifdef CONFIG_BT_COEXIST + rtw_btcoex_SuspendNotify(padapter, BTCOEX_SUSPEND_STATE_SUSPEND); +#endif + rtw_mi_netif_caroff_qstop(padapter); + + rtw_mi_suspend_free_assoc_resource(padapter); + + rtw_led_control(padapter, LED_CTL_POWER_OFF); + + if ((rtw_hal_check_ips_status(padapter) == _TRUE) + || (adapter_to_pwrctl(padapter)->rf_pwrstate == rf_off)) + RTW_PRINT("%s: ### ERROR #### driver in IPS ####ERROR###!!!\n", __FUNCTION__); + + +#ifdef CONFIG_CONCURRENT_MODE + rtw_set_drv_stopped(padapter); /*for stop thread*/ + rtw_stop_cmd_thread(padapter); + rtw_drv_stop_vir_ifaces(adapter_to_dvobj(padapter)); +#endif + rtw_dev_unload(padapter); + + #ifdef CONFIG_SDIO_HCI + sdio_deinit(adapter_to_dvobj(padapter)); + + #if !(CONFIG_RTW_SDIO_KEEP_IRQ) + sdio_free_irq(adapter_to_dvobj(padapter)); + #endif + #endif /*CONFIG_SDIO_HCI*/ + + RTW_INFO("<== "FUNC_ADPT_FMT" exit....\n", FUNC_ADPT_ARG(padapter)); + return ret; +} + +int rtw_suspend_common(_adapter *padapter) +{ + struct dvobj_priv *dvobj = padapter->dvobj; + struct debug_priv *pdbgpriv = &dvobj->drv_dbg; + struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj); +#ifdef CONFIG_WOWLAN + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct registry_priv *registry_par = &padapter->registrypriv; +#endif + + int ret = 0; + systime start_time = rtw_get_current_time(); + + RTW_PRINT(" suspend start\n"); + RTW_INFO("==> %s (%s:%d)\n", __FUNCTION__, current->comm, current->pid); + + pdbgpriv->dbg_suspend_cnt++; + + pwrpriv->bInSuspend = _TRUE; + + while (pwrpriv->bips_processing == _TRUE) + rtw_msleep_os(1); + +#ifdef CONFIG_IOL_READ_EFUSE_MAP + if (!padapter->bup) { + u8 bMacPwrCtrlOn = _FALSE; + rtw_hal_get_hwreg(padapter, HW_VAR_APFM_ON_MAC, &bMacPwrCtrlOn); + if (bMacPwrCtrlOn) + rtw_hal_power_off(padapter); + } +#endif + + if ((!padapter->bup) || RTW_CANNOT_RUN(padapter)) { + RTW_INFO("%s bup=%d bDriverStopped=%s bSurpriseRemoved = %s\n", __func__ + , padapter->bup + , rtw_is_drv_stopped(padapter) ? "True" : "False" + , rtw_is_surprise_removed(padapter) ? "True" : "False"); + pdbgpriv->dbg_suspend_error_cnt++; + goto exit; + } + rtw_mi_scan_abort(padapter, _TRUE); + rtw_ps_deny(padapter, PS_DENY_SUSPEND); + + rtw_mi_cancel_all_timer(padapter); + LeaveAllPowerSaveModeDirect(padapter); + + rtw_ps_deny_cancel(padapter, PS_DENY_SUSPEND); + + if (rtw_mi_check_status(padapter, MI_AP_MODE) == _FALSE) { +#ifdef CONFIG_WOWLAN + if (WOWLAN_IS_STA_MIX_MODE(padapter)) + pwrpriv->wowlan_mode = _TRUE; + else if ( registry_par->wowlan_enable && check_fwstate(pmlmepriv, WIFI_ASOC_STATE)) + pwrpriv->wowlan_mode = _TRUE; + else if (pwrpriv->wowlan_pno_enable == _TRUE) + pwrpriv->wowlan_mode |= pwrpriv->wowlan_pno_enable; + +#ifdef CONFIG_P2P_WOWLAN + if (!rtw_p2p_chk_state(&padapter->wdinfo, P2P_STATE_NONE) || P2P_ROLE_DISABLE != padapter->wdinfo.role) + pwrpriv->wowlan_p2p_mode = _TRUE; + if (_TRUE == pwrpriv->wowlan_p2p_mode) + pwrpriv->wowlan_mode |= pwrpriv->wowlan_p2p_mode; +#endif /* CONFIG_P2P_WOWLAN */ + + if (pwrpriv->wowlan_mode == _TRUE) + rtw_suspend_wow(padapter); + else +#endif /* CONFIG_WOWLAN */ + rtw_suspend_normal(padapter); + } else if (rtw_mi_check_status(padapter, MI_AP_MODE)) { +#ifdef CONFIG_AP_WOWLAN + rtw_suspend_ap_wow(padapter); +#else + rtw_suspend_normal(padapter); +#endif /*CONFIG_AP_WOWLAN*/ + } + + + RTW_PRINT("rtw suspend success in %d ms\n", + rtw_get_passing_time_ms(start_time)); + +exit: + RTW_INFO("<=== %s return %d.............. in %dms\n", __FUNCTION__ + , ret, rtw_get_passing_time_ms(start_time)); + + return ret; +} + +#ifdef CONFIG_WOWLAN +int rtw_resume_process_wow(_adapter *padapter) +{ + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv; + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + struct dvobj_priv *psdpriv = padapter->dvobj; + struct debug_priv *pdbgpriv = &psdpriv->drv_dbg; + struct wowlan_ioctl_param poidparam; + struct sta_info *psta = NULL; + struct registry_priv *registry_par = &padapter->registrypriv; + int ret = _SUCCESS; + u8 en = _FALSE; + + RTW_INFO("==> "FUNC_ADPT_FMT" entry....\n", FUNC_ADPT_ARG(padapter)); + + if (padapter) { + pwrpriv = adapter_to_pwrctl(padapter); + } else { + pdbgpriv->dbg_resume_error_cnt++; + ret = -1; + goto exit; + } + + if (RTW_CANNOT_RUN(padapter)) { + RTW_INFO("%s pdapter %p bDriverStopped %s bSurpriseRemoved %s\n" + , __func__, padapter + , rtw_is_drv_stopped(padapter) ? "True" : "False" + , rtw_is_surprise_removed(padapter) ? "True" : "False"); + goto exit; + } + + pwrpriv->wowlan_in_resume = _TRUE; +#ifdef CONFIG_PNO_SUPPORT +#ifdef CONFIG_FWLPS_IN_IPS + if (pwrpriv->wowlan_pno_enable) + rtw_set_fw_in_ips_mode(padapter, _FALSE); +#endif /* CONFIG_FWLPS_IN_IPS */ +#endif/* CONFIG_PNO_SUPPORT */ + + if (pwrpriv->wowlan_mode == _TRUE) { +#ifdef CONFIG_LPS + if(pwrpriv->wowlan_power_mgmt != PS_MODE_ACTIVE) { + rtw_set_ps_mode(padapter, PS_MODE_ACTIVE, 0, 0, "WOWLAN"); + rtw_wow_lps_level_decide(padapter, _FALSE); + } +#endif /* CONFIG_LPS */ + + rtw_write8(padapter, 0x426, psdpriv->lifetime_en); + rtw_write32(padapter, 0x4c0, psdpriv->pkt_lifetime); + + pwrpriv->bFwCurrentInPSMode = _FALSE; + +#if defined(CONFIG_SDIO_HCI) || defined(CONFIG_PCI_HCI) + rtw_mi_intf_stop(padapter); + rtw_hal_clear_interrupt(padapter); +#endif + + #ifdef CONFIG_SDIO_HCI + #if !(CONFIG_RTW_SDIO_KEEP_IRQ) + if (sdio_alloc_irq(adapter_to_dvobj(padapter)) != _SUCCESS) { + ret = -1; + goto exit; + } + #endif + #endif/*CONFIG_SDIO_HCI*/ + + /* Disable WOW, set H2C command */ + poidparam.subcode = WOWLAN_DISABLE; + rtw_hal_set_hwreg(padapter, HW_VAR_WOWLAN, (u8 *)&poidparam); + +#ifdef CONFIG_CONCURRENT_MODE + rtw_mi_buddy_reset_drv_sw(padapter); +#endif + + psta = rtw_get_stainfo(&padapter->stapriv, get_bssid(&padapter->mlmepriv)); + if (psta) + set_sta_rate(padapter, psta); + + + rtw_clr_drv_stopped(padapter); + RTW_INFO("%s: wowmode resuming, DriverStopped:%s\n", __func__, rtw_is_drv_stopped(padapter) ? "True" : "False"); + + if(registry_par->suspend_type == FW_IPS_WRC) + rtw_hal_set_hwreg(padapter, HW_VAR_VENDOR_WOW_MODE, &en); + + rtw_mi_start_drv_threads(padapter); + + rtw_mi_intf_start(padapter); + + if(registry_par->suspend_type == FW_IPS_DISABLE_BBRF && !check_fwstate(pmlmepriv, WIFI_ASOC_STATE)) { + if (!rtw_is_surprise_removed(padapter)) { + rtw_hal_deinit(padapter); + rtw_hal_init(padapter); + } + RTW_INFO("FW_IPS_DISABLE_BBRF hal deinit, hal init \n"); + } + +#ifdef CONFIG_CONCURRENT_MODE + rtw_mi_buddy_netif_carrier_on(padapter); +#endif + + /* start netif queue */ + rtw_mi_netif_wake_queue(padapter); + + } else + + RTW_PRINT("%s: ### ERROR ### wowlan_mode=%d\n", __FUNCTION__, pwrpriv->wowlan_mode); + + if (padapter->pid[1] != 0) { + RTW_INFO("pid[1]:%d\n", padapter->pid[1]); + rtw_signal_process(padapter->pid[1], SIGUSR2); + } + + if (rtw_chk_roam_flags(padapter, RTW_ROAM_ON_RESUME)) { + if (pwrpriv->wowlan_wake_reason == FW_DECISION_DISCONNECT || + pwrpriv->wowlan_wake_reason == RX_DISASSOC|| + pwrpriv->wowlan_wake_reason == RX_DEAUTH) { + + RTW_INFO("%s: disconnect reason: %02x\n", __func__, + pwrpriv->wowlan_wake_reason); + rtw_indicate_disconnect(padapter, 0, _FALSE); + + rtw_sta_media_status_rpt(padapter, + rtw_get_stainfo(&padapter->stapriv, + get_bssid(&padapter->mlmepriv)), 0); + + rtw_free_assoc_resources(padapter, _TRUE); + pmlmeinfo->state = WIFI_FW_NULL_STATE; + + } else { + RTW_INFO("%s: do roaming\n", __func__); + rtw_roaming(padapter, NULL); + } + } + + if (pwrpriv->wowlan_mode == _TRUE) { + pwrpriv->bips_processing = _FALSE; + _set_timer(&adapter_to_dvobj(padapter)->dynamic_chk_timer, 2000); +#ifndef CONFIG_IPS_CHECK_IN_WD + rtw_set_pwr_state_check_timer(pwrpriv); +#endif + } else + RTW_PRINT("do not reset timer\n"); + + pwrpriv->wowlan_mode = _FALSE; + + /* Power On LED */ +#ifdef CONFIG_RTW_SW_LED + + if (pwrpriv->wowlan_wake_reason == RX_DISASSOC|| + pwrpriv->wowlan_wake_reason == RX_DEAUTH|| + pwrpriv->wowlan_wake_reason == FW_DECISION_DISCONNECT) + rtw_led_control(padapter, LED_CTL_NO_LINK); + else + rtw_led_control(padapter, LED_CTL_LINK); +#endif + /* clean driver side wake up reason. */ + pwrpriv->wowlan_last_wake_reason = pwrpriv->wowlan_wake_reason; + pwrpriv->wowlan_wake_reason = 0; + +#ifdef CONFIG_BT_COEXIST + rtw_btcoex_SuspendNotify(padapter, BTCOEX_SUSPEND_STATE_RESUME); +#endif /* CONFIG_BT_COEXIST */ + +exit: + RTW_INFO("<== "FUNC_ADPT_FMT" exit....\n", FUNC_ADPT_ARG(padapter)); + return ret; +} +#endif /* #ifdef CONFIG_WOWLAN */ + +#ifdef CONFIG_AP_WOWLAN +int rtw_resume_process_ap_wow(_adapter *padapter) +{ + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + struct dvobj_priv *psdpriv = padapter->dvobj; + struct debug_priv *pdbgpriv = &psdpriv->drv_dbg; + struct wowlan_ioctl_param poidparam; + struct sta_info *psta = NULL; + int ret = _SUCCESS; + u8 ch, bw, offset; + + RTW_INFO("==> "FUNC_ADPT_FMT" entry....\n", FUNC_ADPT_ARG(padapter)); + + if (padapter) { + pwrpriv = adapter_to_pwrctl(padapter); + } else { + pdbgpriv->dbg_resume_error_cnt++; + ret = -1; + goto exit; + } + + +#ifdef CONFIG_LPS + if(pwrpriv->wowlan_power_mgmt != PS_MODE_ACTIVE) { + rtw_set_ps_mode(padapter, PS_MODE_ACTIVE, 0, 0, "AP-WOWLAN"); + rtw_wow_lps_level_decide(padapter, _FALSE); + } +#endif /* CONFIG_LPS */ + + pwrpriv->bFwCurrentInPSMode = _FALSE; + + rtw_hal_disable_interrupt(padapter); + + rtw_hal_clear_interrupt(padapter); + + #ifdef CONFIG_SDIO_HCI + #if !(CONFIG_RTW_SDIO_KEEP_IRQ) + if (sdio_alloc_irq(adapter_to_dvobj(padapter)) != _SUCCESS) { + ret = -1; + goto exit; + } + #endif + #endif/*CONFIG_SDIO_HCI*/ + /* Disable WOW, set H2C command */ + poidparam.subcode = WOWLAN_AP_DISABLE; + rtw_hal_set_hwreg(padapter, HW_VAR_WOWLAN, (u8 *)&poidparam); + pwrpriv->wowlan_ap_mode = _FALSE; + + rtw_clr_drv_stopped(padapter); + RTW_INFO("%s: wowmode resuming, DriverStopped:%s\n", __func__, rtw_is_drv_stopped(padapter) ? "True" : "False"); + + rtw_mi_start_drv_threads(padapter); + + if (rtw_mi_check_status(padapter, MI_LINKED)) { + ch = rtw_mi_get_union_chan(padapter); + bw = rtw_mi_get_union_bw(padapter); + offset = rtw_mi_get_union_offset(padapter); + RTW_INFO(FUNC_ADPT_FMT" back to linked/linking union - ch:%u, bw:%u, offset:%u\n", FUNC_ADPT_ARG(padapter), ch, bw, offset); + set_channel_bwmode(padapter, ch, offset, bw); + } + + /*FOR ONE AP - TODO :Multi-AP*/ + { + int i; + _adapter *iface; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if ((iface) && rtw_is_adapter_up(iface)) { + if (check_fwstate(&iface->mlmepriv, WIFI_AP_STATE | WIFI_MESH_STATE | WIFI_ASOC_STATE)) + rtw_reset_drv_sw(iface); + } + } + + } + rtw_mi_intf_start(padapter); + + /* start netif queue */ + rtw_mi_netif_wake_queue(padapter); + + if (padapter->pid[1] != 0) { + RTW_INFO("pid[1]:%d\n", padapter->pid[1]); + rtw_signal_process(padapter->pid[1], SIGUSR2); + } + +#ifdef CONFIG_RESUME_IN_WORKQUEUE + /* rtw_unlock_suspend(); */ +#endif /* CONFIG_RESUME_IN_WORKQUEUE */ + + pwrpriv->bips_processing = _FALSE; + _set_timer(&adapter_to_dvobj(padapter)->dynamic_chk_timer, 2000); +#ifndef CONFIG_IPS_CHECK_IN_WD + rtw_set_pwr_state_check_timer(pwrpriv); +#endif + /* clean driver side wake up reason. */ + pwrpriv->wowlan_wake_reason = 0; + +#ifdef CONFIG_BT_COEXIST + rtw_btcoex_SuspendNotify(padapter, BTCOEX_SUSPEND_STATE_RESUME); +#endif /* CONFIG_BT_COEXIST */ + + /* Power On LED */ +#ifdef CONFIG_RTW_SW_LED + + rtw_led_control(padapter, LED_CTL_LINK); +#endif +exit: + RTW_INFO("<== "FUNC_ADPT_FMT" exit....\n", FUNC_ADPT_ARG(padapter)); + return ret; +} +#endif /* #ifdef CONFIG_APWOWLAN */ + +void rtw_mi_resume_process_normal(_adapter *padapter) +{ + int i; + _adapter *iface; + struct mlme_priv *pmlmepriv; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if ((iface) && rtw_is_adapter_up(iface)) { + pmlmepriv = &iface->mlmepriv; + + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) { + RTW_INFO(FUNC_ADPT_FMT" fwstate:0x%08x - WIFI_STATION_STATE\n", FUNC_ADPT_ARG(iface), get_fwstate(pmlmepriv)); + + if (rtw_chk_roam_flags(iface, RTW_ROAM_ON_RESUME)) + rtw_roaming(iface, NULL); + + } +#ifdef CONFIG_AP_MODE + else if (MLME_IS_AP(iface) || MLME_IS_MESH(iface)) { + RTW_INFO(FUNC_ADPT_FMT" %s\n", FUNC_ADPT_ARG(iface), MLME_IS_AP(iface) ? "AP" : "MESH"); + rtw_ap_restore_network(iface); + } +#endif + else if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) + RTW_INFO(FUNC_ADPT_FMT" fwstate:0x%08x - WIFI_ADHOC_STATE\n", FUNC_ADPT_ARG(iface), get_fwstate(pmlmepriv)); + else + RTW_INFO(FUNC_ADPT_FMT" fwstate:0x%08x - ???\n", FUNC_ADPT_ARG(iface), get_fwstate(pmlmepriv)); + } + } +} + +int rtw_resume_process_normal(_adapter *padapter) +{ + struct net_device *pnetdev; + struct pwrctrl_priv *pwrpriv; + struct dvobj_priv *psdpriv; + struct debug_priv *pdbgpriv; + + int ret = _SUCCESS; + + if (!padapter) { + ret = -1; + goto exit; + } + + pnetdev = padapter->pnetdev; + pwrpriv = adapter_to_pwrctl(padapter); + psdpriv = padapter->dvobj; + pdbgpriv = &psdpriv->drv_dbg; + + RTW_INFO("==> "FUNC_ADPT_FMT" entry....\n", FUNC_ADPT_ARG(padapter)); + + #ifdef CONFIG_SDIO_HCI + /* interface init */ + if (sdio_init(adapter_to_dvobj(padapter)) != _SUCCESS) { + ret = -1; + goto exit; + } + #endif/*CONFIG_SDIO_HCI*/ + + rtw_clr_surprise_removed(padapter); + rtw_hal_disable_interrupt(padapter); + + #ifdef CONFIG_SDIO_HCI + #if !(CONFIG_RTW_SDIO_KEEP_IRQ) + if (sdio_alloc_irq(adapter_to_dvobj(padapter)) != _SUCCESS) { + ret = -1; + goto exit; + } + #endif + #endif/*CONFIG_SDIO_HCI*/ + + rtw_mi_reset_drv_sw(padapter); + + pwrpriv->bkeepfwalive = _FALSE; + + RTW_INFO("bkeepfwalive(%x)\n", pwrpriv->bkeepfwalive); + if (pm_netdev_open(pnetdev, _TRUE) != 0) { + ret = -1; + pdbgpriv->dbg_resume_error_cnt++; + goto exit; + } + + rtw_mi_netif_caron_qstart(padapter); + + if (padapter->pid[1] != 0) { + RTW_INFO("pid[1]:%d\n", padapter->pid[1]); + rtw_signal_process(padapter->pid[1], SIGUSR2); + } + +#ifdef CONFIG_BT_COEXIST + rtw_btcoex_SuspendNotify(padapter, BTCOEX_SUSPEND_STATE_RESUME); +#endif /* CONFIG_BT_COEXIST */ + + rtw_mi_resume_process_normal(padapter); + +#ifdef CONFIG_RESUME_IN_WORKQUEUE + /* rtw_unlock_suspend(); */ +#endif /* CONFIG_RESUME_IN_WORKQUEUE */ + RTW_INFO("<== "FUNC_ADPT_FMT" exit....\n", FUNC_ADPT_ARG(padapter)); + +exit: + return ret; +} + +int rtw_resume_common(_adapter *padapter) +{ + int ret = 0; + systime start_time = rtw_get_current_time(); + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + + if (pwrpriv == NULL) + return 0; + + if (pwrpriv->bInSuspend == _FALSE) + return 0; + + RTW_PRINT("resume start\n"); + RTW_INFO("==> %s (%s:%d)\n", __FUNCTION__, current->comm, current->pid); + + if (rtw_mi_check_status(padapter, MI_AP_MODE) == _FALSE) { +#ifdef CONFIG_WOWLAN + if (pwrpriv->wowlan_mode == _TRUE) + rtw_resume_process_wow(padapter); + else +#endif + rtw_resume_process_normal(padapter); + + } else if (rtw_mi_check_status(padapter, MI_AP_MODE)) { +#ifdef CONFIG_AP_WOWLAN + rtw_resume_process_ap_wow(padapter); +#else + rtw_resume_process_normal(padapter); +#endif /* CONFIG_AP_WOWLAN */ + } + + pwrpriv->bInSuspend = _FALSE; + pwrpriv->wowlan_in_resume = _FALSE; + + RTW_PRINT("%s:%d in %d ms\n", __FUNCTION__ , ret, + rtw_get_passing_time_ms(start_time)); + + + return ret; +} + +#ifdef CONFIG_GPIO_API +u8 rtw_get_gpio(struct net_device *netdev, u8 gpio_num) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(netdev); + return rtw_hal_get_gpio(adapter, gpio_num); +} +EXPORT_SYMBOL(rtw_get_gpio); + +int rtw_set_gpio_output_value(struct net_device *netdev, u8 gpio_num, bool isHigh) +{ + u8 direction = 0; + u8 res = -1; + _adapter *adapter = (_adapter *)rtw_netdev_priv(netdev); + return rtw_hal_set_gpio_output_value(adapter, gpio_num, isHigh); +} +EXPORT_SYMBOL(rtw_set_gpio_output_value); + +int rtw_config_gpio(struct net_device *netdev, u8 gpio_num, bool isOutput) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(netdev); + return rtw_hal_config_gpio(adapter, gpio_num, isOutput); +} +EXPORT_SYMBOL(rtw_config_gpio); +int rtw_register_gpio_interrupt(struct net_device *netdev, int gpio_num, void(*callback)(u8 level)) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(netdev); + return rtw_hal_register_gpio_interrupt(adapter, gpio_num, callback); +} +EXPORT_SYMBOL(rtw_register_gpio_interrupt); + +int rtw_disable_gpio_interrupt(struct net_device *netdev, int gpio_num) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(netdev); + return rtw_hal_disable_gpio_interrupt(adapter, gpio_num); +} +EXPORT_SYMBOL(rtw_disable_gpio_interrupt); + +#endif /* #ifdef CONFIG_GPIO_API */ + +#ifdef CONFIG_APPEND_VENDOR_IE_ENABLE + +int rtw_vendor_ie_get_api(struct net_device *dev, int ie_num, char *extra, + u16 extra_len) +{ + int ret = 0; + + ret = rtw_vendor_ie_get_raw_data(dev, ie_num, extra, extra_len); + return ret; +} +EXPORT_SYMBOL(rtw_vendor_ie_get_api); + +int rtw_vendor_ie_set_api(struct net_device *dev, char *extra) +{ + return rtw_vendor_ie_set(dev, NULL, NULL, extra); +} +EXPORT_SYMBOL(rtw_vendor_ie_set_api); + +#endif diff --git a/os_dep/linux/recv_linux.c b/os_dep/linux/recv_linux.c new file mode 100644 index 0000000..14fc295 --- /dev/null +++ b/os_dep/linux/recv_linux.c @@ -0,0 +1,738 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#define _RECV_OSDEP_C_ + +#include + +int rtw_os_recvframe_duplicate_skb(_adapter *padapter, union recv_frame *pcloneframe, _pkt *pskb) +{ + int res = _SUCCESS; + _pkt *pkt_copy = NULL; + + if (pskb == NULL) { + RTW_INFO("%s [WARN] skb == NULL, drop frag frame\n", __func__); + return _FAIL; + } +#if 1 + pkt_copy = rtw_skb_copy(pskb); + + if (pkt_copy == NULL) { + RTW_INFO("%s [WARN] rtw_skb_copy fail , drop frag frame\n", __func__); + return _FAIL; + } +#else + pkt_copy = rtw_skb_clone(pskb); + + if (pkt_copy == NULL) { + RTW_INFO("%s [WARN] rtw_skb_clone fail , drop frag frame\n", __func__); + return _FAIL; + } +#endif + pkt_copy->dev = padapter->pnetdev; + + pcloneframe->u.hdr.pkt = pkt_copy; + pcloneframe->u.hdr.rx_head = pkt_copy->head; + pcloneframe->u.hdr.rx_data = pkt_copy->data; + pcloneframe->u.hdr.rx_end = skb_end_pointer(pkt_copy); + pcloneframe->u.hdr.rx_tail = skb_tail_pointer(pkt_copy); + pcloneframe->u.hdr.len = pkt_copy->len; + + return res; +} + +int rtw_os_alloc_recvframe(_adapter *padapter, union recv_frame *precvframe, u8 *pdata, _pkt *pskb) +{ + int res = _SUCCESS; + u8 shift_sz = 0; + u32 skb_len, alloc_sz; + _pkt *pkt_copy = NULL; + struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib; + + + if (pdata == NULL) { + precvframe->u.hdr.pkt = NULL; + res = _FAIL; + return res; + } + + + /* Modified by Albert 20101213 */ + /* For 8 bytes IP header alignment. */ + shift_sz = pattrib->qos ? 6 : 0; /* Qos data, wireless lan header length is 26 */ + + skb_len = pattrib->pkt_len; + + /* for first fragment packet, driver need allocate 1536+drvinfo_sz+RXDESC_SIZE to defrag packet. */ + /* modify alloc_sz for recvive crc error packet by thomas 2011-06-02 */ + if ((pattrib->mfrag == 1) && (pattrib->frag_num == 0)) { + /* alloc_sz = 1664; */ /* 1664 is 128 alignment. */ + alloc_sz = (skb_len <= 1650) ? 1664 : (skb_len + 14); + } else { + alloc_sz = skb_len; + /* 6 is for IP header 8 bytes alignment in QoS packet case. */ + /* 8 is for skb->data 4 bytes alignment. */ + alloc_sz += 14; + } + + pkt_copy = rtw_skb_alloc(alloc_sz); + + if (pkt_copy) { + pkt_copy->dev = padapter->pnetdev; + pkt_copy->len = skb_len; + precvframe->u.hdr.pkt = pkt_copy; + precvframe->u.hdr.rx_head = pkt_copy->head; + precvframe->u.hdr.rx_end = pkt_copy->data + alloc_sz; + skb_reserve(pkt_copy, 8 - ((SIZE_PTR)(pkt_copy->data) & 7)); /* force pkt_copy->data at 8-byte alignment address */ + skb_reserve(pkt_copy, shift_sz);/* force ip_hdr at 8-byte alignment address according to shift_sz. */ + _rtw_memcpy(pkt_copy->data, pdata, skb_len); + precvframe->u.hdr.rx_data = precvframe->u.hdr.rx_tail = pkt_copy->data; + } else { +#if 0 + { + rtw_free_recvframe(precvframe_if2, &precvpriv->free_recv_queue); + rtw_enqueue_recvbuf_to_head(precvbuf, &precvpriv->recv_buf_pending_queue); + + /* The case of can't allocate skb is serious and may never be recovered, + once bDriverStopped is enable, this task should be stopped.*/ + if (!rtw_is_drv_stopped(secondary_padapter)) +#ifdef PLATFORM_LINUX + tasklet_schedule(&precvpriv->recv_tasklet); +#endif + return ret; + } + +#endif + +#ifdef CONFIG_USE_USB_BUFFER_ALLOC_RX + RTW_INFO("%s:can not allocate memory for skb copy\n", __func__); + + precvframe->u.hdr.pkt = NULL; + + /* rtw_free_recvframe(precvframe, pfree_recv_queue); */ + /*exit_rtw_os_recv_resource_alloc;*/ + + res = _FAIL; +#else + if ((pattrib->mfrag == 1) && (pattrib->frag_num == 0)) { + RTW_INFO("%s: alloc_skb fail , drop frag frame\n", __FUNCTION__); + /* rtw_free_recvframe(precvframe, pfree_recv_queue); */ + res = _FAIL; + goto exit_rtw_os_recv_resource_alloc; + } + + if (pskb == NULL) { + res = _FAIL; + goto exit_rtw_os_recv_resource_alloc; + } + + precvframe->u.hdr.pkt = rtw_skb_clone(pskb); + if (precvframe->u.hdr.pkt) { + precvframe->u.hdr.pkt->dev = padapter->pnetdev; + precvframe->u.hdr.rx_head = precvframe->u.hdr.rx_data = precvframe->u.hdr.rx_tail = pdata; + precvframe->u.hdr.rx_end = pdata + alloc_sz; + } else { + RTW_INFO("%s: rtw_skb_clone fail\n", __FUNCTION__); + /* rtw_free_recvframe(precvframe, pfree_recv_queue); */ + /*exit_rtw_os_recv_resource_alloc;*/ + res = _FAIL; + } +#endif + } + +exit_rtw_os_recv_resource_alloc: + + return res; + +} + +void rtw_os_free_recvframe(union recv_frame *precvframe) +{ + if (precvframe->u.hdr.pkt) { + rtw_os_pkt_free(precvframe->u.hdr.pkt); + precvframe->u.hdr.pkt = NULL; + } +} + +/* init os related resource in struct recv_priv */ +int rtw_os_recv_resource_init(struct recv_priv *precvpriv, _adapter *padapter) +{ + int res = _SUCCESS; + + +#ifdef CONFIG_RTW_NAPI + skb_queue_head_init(&precvpriv->rx_napi_skb_queue); +#endif /* CONFIG_RTW_NAPI */ + + return res; +} + +/* alloc os related resource in union recv_frame */ +int rtw_os_recv_resource_alloc(_adapter *padapter, union recv_frame *precvframe) +{ + int res = _SUCCESS; + + precvframe->u.hdr.pkt = NULL; + + return res; +} + +/* free os related resource in union recv_frame */ +void rtw_os_recv_resource_free(struct recv_priv *precvpriv) +{ + sint i; + union recv_frame *precvframe; + precvframe = (union recv_frame *) precvpriv->precv_frame_buf; + + +#ifdef CONFIG_RTW_NAPI + if (skb_queue_len(&precvpriv->rx_napi_skb_queue)) + RTW_WARN("rx_napi_skb_queue not empty\n"); + rtw_skb_queue_purge(&precvpriv->rx_napi_skb_queue); +#endif /* CONFIG_RTW_NAPI */ + + for (i = 0; i < NR_RECVFRAME; i++) { + rtw_os_free_recvframe(precvframe); + precvframe++; + } +} + +#if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) +#if !defined(CONFIG_RTL8822B) && !defined(CONFIG_RTL8822C) +#ifdef CONFIG_SDIO_RX_COPY +static int sdio_init_recvbuf_with_skb(struct recv_priv *recvpriv, struct recv_buf *rbuf, u32 size) +{ +#ifdef CONFIG_PREALLOC_RX_SKB_BUFFER + if (RBUF_IS_PREALLOC(rbuf)) { + rbuf->pskb = rtw_alloc_skb_premem(size); + if (!rbuf->pskb) { + RTW_WARN("%s: Fail to get pre-alloc skb! size=%d\n", __func__, size); + return _FAIL; + } + skb_set_tail_pointer(rbuf->pskb, 0); /* TODO: do this in RTKM */ + } else +#else + { + SIZE_PTR tmpaddr = 0; + SIZE_PTR alignment = 0; + + rbuf->pskb = rtw_skb_alloc(size + RECVBUFF_ALIGN_SZ); + if (!rbuf->pskb) + return _FAIL; + + tmpaddr = (SIZE_PTR)rbuf->pskb->data; + alignment = tmpaddr & (RECVBUFF_ALIGN_SZ - 1); + skb_reserve(rbuf->pskb, (RECVBUFF_ALIGN_SZ - alignment)); + } +#endif + + rbuf->pskb->dev = recvpriv->adapter->pnetdev; + + /* init recvbuf */ + rbuf->phead = rbuf->pskb->head; + rbuf->pdata = rbuf->pskb->data; + rbuf->ptail = skb_tail_pointer(rbuf->pskb); + rbuf->pend = skb_end_pointer(rbuf->pskb); + rbuf->len = 0; + + return _SUCCESS; +} +#endif /* CONFIG_SDIO_RX_COPY */ +#endif /* !defined(CONFIG_RTL8822B) && !defined(CONFIG_RTL8822C) */ +#endif /* defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) */ + +/* alloc os related resource in struct recv_buf */ +int rtw_os_recvbuf_resource_alloc(_adapter *padapter, struct recv_buf *precvbuf, u32 size) +{ + int res = _SUCCESS; + +#ifdef CONFIG_USB_HCI +#ifdef CONFIG_USE_USB_BUFFER_ALLOC_RX + struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter); + struct usb_device *pusbd = pdvobjpriv->pusbdev; +#endif + + precvbuf->irp_pending = _FALSE; + precvbuf->purb = usb_alloc_urb(0, GFP_KERNEL); + if (precvbuf->purb == NULL) + res = _FAIL; + + precvbuf->pskb = NULL; + + precvbuf->pallocated_buf = precvbuf->pbuf = NULL; + + precvbuf->pdata = precvbuf->phead = precvbuf->ptail = precvbuf->pend = NULL; + + precvbuf->transfer_len = 0; + + precvbuf->len = 0; + +#ifdef CONFIG_USE_USB_BUFFER_ALLOC_RX + precvbuf->pallocated_buf = rtw_usb_buffer_alloc(pusbd, (size_t)size, &precvbuf->dma_transfer_addr); + precvbuf->pbuf = precvbuf->pallocated_buf; + if (precvbuf->pallocated_buf == NULL) + return _FAIL; +#endif /* CONFIG_USE_USB_BUFFER_ALLOC_RX */ + +#elif defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) + #if !defined(CONFIG_RTL8822B) && !defined(CONFIG_RTL8822C) + #ifdef CONFIG_SDIO_RX_COPY + res = sdio_init_recvbuf_with_skb(&padapter->recvpriv, precvbuf, size); + #endif + #endif + +#endif /* CONFIG_XXX_HCI */ + + return res; +} + +/* free os related resource in struct recv_buf */ +int rtw_os_recvbuf_resource_free(_adapter *padapter, struct recv_buf *precvbuf) +{ + int ret = _SUCCESS; + +#ifdef CONFIG_USB_HCI + +#ifdef CONFIG_USE_USB_BUFFER_ALLOC_RX + + struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter); + struct usb_device *pusbd = pdvobjpriv->pusbdev; + + rtw_usb_buffer_free(pusbd, (size_t)precvbuf->alloc_sz, precvbuf->pallocated_buf, precvbuf->dma_transfer_addr); + precvbuf->pallocated_buf = NULL; + precvbuf->dma_transfer_addr = 0; + +#endif /* CONFIG_USE_USB_BUFFER_ALLOC_RX */ + + if (precvbuf->purb) { + /* usb_kill_urb(precvbuf->purb); */ + usb_free_urb(precvbuf->purb); + } + +#endif /* CONFIG_USB_HCI */ + + + if (precvbuf->pskb) { +#ifdef CONFIG_PREALLOC_RX_SKB_BUFFER + if (rtw_free_skb_premem(precvbuf->pskb) != 0) +#endif + rtw_skb_free(precvbuf->pskb); + } + return ret; + +} + +_pkt *rtw_os_alloc_msdu_pkt(union recv_frame *prframe, const u8 *da, const u8 *sa + , u8 *msdu ,u16 msdu_len, enum rtw_rx_llc_hdl llc_hdl) +{ + u8 *data_ptr; + _pkt *sub_skb; + struct rx_pkt_attrib *pattrib; + + pattrib = &prframe->u.hdr.attrib; + +#ifdef CONFIG_SKB_COPY + sub_skb = rtw_skb_alloc(msdu_len + 14); + if (sub_skb) { + skb_reserve(sub_skb, 14); + data_ptr = (u8 *)skb_put(sub_skb, msdu_len); + _rtw_memcpy(data_ptr, msdu, msdu_len); + } else +#endif /* CONFIG_SKB_COPY */ + { + sub_skb = rtw_skb_clone(prframe->u.hdr.pkt); + if (sub_skb) { + sub_skb->data = msdu; + sub_skb->len = msdu_len; + skb_set_tail_pointer(sub_skb, msdu_len); + } else { + RTW_INFO("%s(): rtw_skb_clone() Fail!!!\n", __FUNCTION__); + return NULL; + } + } + + if (llc_hdl) { + /* remove RFC1042 or Bridge-Tunnel encapsulation and replace EtherType */ + skb_pull(sub_skb, SNAP_SIZE); + _rtw_memcpy(skb_push(sub_skb, ETH_ALEN), sa, ETH_ALEN); + _rtw_memcpy(skb_push(sub_skb, ETH_ALEN), da, ETH_ALEN); + } else { + /* Leave Ethernet header part of hdr and full payload */ + u16 len; + + len = htons(sub_skb->len); + _rtw_memcpy(skb_push(sub_skb, 2), &len, 2); + _rtw_memcpy(skb_push(sub_skb, ETH_ALEN), sa, ETH_ALEN); + _rtw_memcpy(skb_push(sub_skb, ETH_ALEN), da, ETH_ALEN); + } + + return sub_skb; +} + +#ifdef CONFIG_RTW_NAPI +static int napi_recv(_adapter *padapter, int budget) +{ + _pkt *pskb; + struct recv_priv *precvpriv = &padapter->recvpriv; + int work_done = 0; + struct registry_priv *pregistrypriv = &padapter->registrypriv; + u8 rx_ok; + + + while ((work_done < budget) && + (!skb_queue_empty(&precvpriv->rx_napi_skb_queue))) { + pskb = skb_dequeue(&precvpriv->rx_napi_skb_queue); + if (!pskb) + break; + + rx_ok = _FALSE; + +#ifdef CONFIG_RTW_GRO + /* + cloned SKB use dataref to avoid kernel release it. + But dataref changed in napi_gro_receive. + So, we should prevent cloned SKB go into napi_gro_receive. + */ + if (pregistrypriv->en_gro && !skb_cloned(pskb)) { +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)) + if (rtw_napi_gro_receive(&padapter->napi, pskb) != GRO_MERGED_FREE) +#else + if (rtw_napi_gro_receive(&padapter->napi, pskb) != GRO_DROP) +#endif + rx_ok = _TRUE; + goto next; + } +#endif /* CONFIG_RTW_GRO */ + + if (rtw_netif_receive_skb(padapter->pnetdev, pskb) == NET_RX_SUCCESS) + rx_ok = _TRUE; + +next: + if (rx_ok == _TRUE) { + work_done++; + DBG_COUNTER(padapter->rx_logs.os_netif_ok); + } else { + DBG_COUNTER(padapter->rx_logs.os_netif_err); + } + } + + return work_done; +} + +int rtw_recv_napi_poll(struct napi_struct *napi, int budget) +{ + _adapter *padapter = container_of(napi, _adapter, napi); + int work_done = 0; + struct recv_priv *precvpriv = &padapter->recvpriv; + + + work_done = napi_recv(padapter, budget); + if (work_done < budget) { +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)) && defined(CONFIG_PCI_HCI) + napi_complete_done(napi, work_done); +#else + napi_complete(napi); +#endif + if (!skb_queue_empty(&precvpriv->rx_napi_skb_queue)) + napi_schedule(napi); + } + + return work_done; +} + +#ifdef CONFIG_RTW_NAPI_DYNAMIC +void dynamic_napi_th_chk (_adapter *adapter) +{ + + if (adapter->registrypriv.en_napi) { + struct dvobj_priv *dvobj; + struct registry_priv *registry; + + dvobj = adapter_to_dvobj(adapter); + registry = &adapter->registrypriv; + if (dvobj->traffic_stat.cur_rx_tp > registry->napi_threshold) + dvobj->en_napi_dynamic = 1; + else + dvobj->en_napi_dynamic = 0; + } + +} +#endif /* CONFIG_RTW_NAPI_DYNAMIC */ +#endif /* CONFIG_RTW_NAPI */ + +void rtw_os_recv_indicate_pkt(_adapter *padapter, _pkt *pkt, union recv_frame *rframe) +{ + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct recv_priv *precvpriv = &(padapter->recvpriv); + struct registry_priv *pregistrypriv = &padapter->registrypriv; +#ifdef CONFIG_BR_EXT + void *br_port = NULL; +#endif + int ret; + + /* Indicat the packets to upper layer */ + if (pkt) { + struct ethhdr *ehdr = (struct ethhdr *)pkt->data; + + DBG_COUNTER(padapter->rx_logs.os_indicate); + +#ifdef CONFIG_BR_EXT + if (!adapter_use_wds(padapter) && check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_ADHOC_STATE) == _TRUE) { + /* Insert NAT2.5 RX here! */ + #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 35)) + br_port = padapter->pnetdev->br_port; + #else + rcu_read_lock(); + br_port = rcu_dereference(padapter->pnetdev->rx_handler_data); + rcu_read_unlock(); + #endif + + if (br_port) { + int nat25_handle_frame(_adapter *priv, struct sk_buff *skb); + + if (nat25_handle_frame(padapter, pkt) == -1) { + /* priv->ext_stats.rx_data_drops++; */ + /* DEBUG_ERR("RX DROP: nat25_handle_frame fail!\n"); */ + /* return FAIL; */ + + #if 1 + /* bypass this frame to upper layer!! */ + #else + rtw_skb_free(sub_skb); + continue; + #endif + } + } + } +#endif /* CONFIG_BR_EXT */ + + /* After eth_type_trans process , pkt->data pointer will move from ethrnet header to ip header */ + pkt->protocol = eth_type_trans(pkt, padapter->pnetdev); + pkt->dev = padapter->pnetdev; + pkt->ip_summed = CHECKSUM_NONE; /* CONFIG_TCP_CSUM_OFFLOAD_RX */ +#ifdef CONFIG_TCP_CSUM_OFFLOAD_RX + if ((rframe->u.hdr.attrib.csum_valid == 1) + && (rframe->u.hdr.attrib.csum_err == 0)) + pkt->ip_summed = CHECKSUM_UNNECESSARY; +#endif /* CONFIG_TCP_CSUM_OFFLOAD_RX */ + +#ifdef CONFIG_RTW_NAPI +#ifdef CONFIG_RTW_NAPI_DYNAMIC + if (!skb_queue_empty(&precvpriv->rx_napi_skb_queue) + && !adapter_to_dvobj(padapter)->en_napi_dynamic + ) + napi_recv(padapter, RTL_NAPI_WEIGHT); +#endif + + if (pregistrypriv->en_napi + #ifdef CONFIG_RTW_NAPI_DYNAMIC + && adapter_to_dvobj(padapter)->en_napi_dynamic + #endif + ) { + skb_queue_tail(&precvpriv->rx_napi_skb_queue, pkt); + #ifndef CONFIG_RTW_NAPI_V2 + napi_schedule(&padapter->napi); + #endif + return; + } +#endif /* CONFIG_RTW_NAPI */ + + ret = rtw_netif_rx(padapter->pnetdev, pkt); + if (ret == NET_RX_SUCCESS) + DBG_COUNTER(padapter->rx_logs.os_netif_ok); + else + DBG_COUNTER(padapter->rx_logs.os_netif_err); + } +} + +void rtw_handle_tkip_mic_err(_adapter *padapter, struct sta_info *sta, u8 bgroup) +{ +#ifdef CONFIG_IOCTL_CFG80211 + enum nl80211_key_type key_type = 0; +#endif + union iwreq_data wrqu; + struct iw_michaelmicfailure ev; + struct security_priv *psecuritypriv = &padapter->securitypriv; + systime cur_time = 0; + + if (psecuritypriv->last_mic_err_time == 0) + psecuritypriv->last_mic_err_time = rtw_get_current_time(); + else { + cur_time = rtw_get_current_time(); + + if (cur_time - psecuritypriv->last_mic_err_time < 60 * HZ) { + psecuritypriv->btkip_countermeasure = _TRUE; + psecuritypriv->last_mic_err_time = 0; + psecuritypriv->btkip_countermeasure_time = cur_time; + } else + psecuritypriv->last_mic_err_time = rtw_get_current_time(); + } + +#ifdef CONFIG_IOCTL_CFG80211 + if (bgroup) + key_type |= NL80211_KEYTYPE_GROUP; + else + key_type |= NL80211_KEYTYPE_PAIRWISE; + + cfg80211_michael_mic_failure(padapter->pnetdev, sta->cmn.mac_addr, key_type, -1, NULL, GFP_ATOMIC); +#endif + + _rtw_memset(&ev, 0x00, sizeof(ev)); + if (bgroup) + ev.flags |= IW_MICFAILURE_GROUP; + else + ev.flags |= IW_MICFAILURE_PAIRWISE; + + ev.src_addr.sa_family = ARPHRD_ETHER; + _rtw_memcpy(ev.src_addr.sa_data, sta->cmn.mac_addr, ETH_ALEN); + + _rtw_memset(&wrqu, 0x00, sizeof(wrqu)); + wrqu.data.length = sizeof(ev); + +#ifndef CONFIG_IOCTL_CFG80211 + wireless_send_event(padapter->pnetdev, IWEVMICHAELMICFAILURE, &wrqu, (char *) &ev); +#endif +} + +#ifdef CONFIG_HOSTAPD_MLME +void rtw_hostapd_mlme_rx(_adapter *padapter, union recv_frame *precv_frame) +{ + _pkt *skb; + struct hostapd_priv *phostapdpriv = padapter->phostapdpriv; + struct net_device *pmgnt_netdev = phostapdpriv->pmgnt_netdev; + + + skb = precv_frame->u.hdr.pkt; + + if (skb == NULL) + return; + + skb->data = precv_frame->u.hdr.rx_data; + skb->tail = precv_frame->u.hdr.rx_tail; + skb->len = precv_frame->u.hdr.len; + + /* pskb_copy = rtw_skb_copy(skb); + * if(skb == NULL) goto _exit; */ + + skb->dev = pmgnt_netdev; + skb->ip_summed = CHECKSUM_NONE; + skb->pkt_type = PACKET_OTHERHOST; + /* skb->protocol = __constant_htons(0x0019); ETH_P_80211_RAW */ + skb->protocol = __constant_htons(0x0003); /*ETH_P_80211_RAW*/ + + /* RTW_INFO("(1)data=0x%x, head=0x%x, tail=0x%x, mac_header=0x%x, len=%d\n", skb->data, skb->head, skb->tail, skb->mac_header, skb->len); */ + + /* skb->mac.raw = skb->data; */ + skb_reset_mac_header(skb); + + /* skb_pull(skb, 24); */ + _rtw_memset(skb->cb, 0, sizeof(skb->cb)); + + rtw_netif_rx(pmgnt_netdev, skb); + + precv_frame->u.hdr.pkt = NULL; /* set pointer to NULL before rtw_free_recvframe() if call rtw_netif_rx() */ +} +#endif /* CONFIG_HOSTAPD_MLME */ + +#ifdef CONFIG_WIFI_MONITOR +/* + precv_frame: impossible to be NULL + precv_frame: free by caller + */ +int rtw_recv_monitor(_adapter *padapter, union recv_frame *precv_frame) +{ + int ret = _FAIL; + _pkt *skb; + + skb = precv_frame->u.hdr.pkt; + if (skb == NULL) { + RTW_INFO("%s :skb==NULL something wrong!!!!\n", __func__); + goto _recv_drop; + } + + skb->data = precv_frame->u.hdr.rx_data; + skb_set_tail_pointer(skb, precv_frame->u.hdr.len); + skb->len = precv_frame->u.hdr.len; + skb->ip_summed = CHECKSUM_NONE; + skb->pkt_type = PACKET_OTHERHOST; + skb->protocol = htons(0x0019); /* ETH_P_80211_RAW */ + + /* send to kernel */ + rtw_netif_rx(padapter->pnetdev, skb); + + /* pointers to NULL before rtw_free_recvframe() */ + precv_frame->u.hdr.pkt = NULL; + + ret = _SUCCESS; + +_recv_drop: + return ret; +} +#endif /* CONFIG_WIFI_MONITOR */ + +inline void rtw_rframe_set_os_pkt(union recv_frame *rframe) +{ + _pkt *skb = rframe->u.hdr.pkt; + + skb->data = rframe->u.hdr.rx_data; + skb_set_tail_pointer(skb, rframe->u.hdr.len); + skb->len = rframe->u.hdr.len; +} + +int rtw_recv_indicatepkt(_adapter *padapter, union recv_frame *precv_frame) +{ + struct recv_priv *precvpriv; + _queue *pfree_recv_queue; + + precvpriv = &(padapter->recvpriv); + pfree_recv_queue = &(precvpriv->free_recv_queue); + + if (precv_frame->u.hdr.pkt == NULL) + goto _recv_indicatepkt_drop; + + rtw_os_recv_indicate_pkt(padapter, precv_frame->u.hdr.pkt, precv_frame); + + precv_frame->u.hdr.pkt = NULL; + rtw_free_recvframe(precv_frame, pfree_recv_queue); + return _SUCCESS; + +_recv_indicatepkt_drop: + rtw_free_recvframe(precv_frame, pfree_recv_queue); + DBG_COUNTER(padapter->rx_logs.os_indicate_err); + return _FAIL; +} + +void rtw_os_read_port(_adapter *padapter, struct recv_buf *precvbuf) +{ +#ifdef CONFIG_USB_HCI + struct recv_priv *precvpriv = &padapter->recvpriv; + + precvbuf->ref_cnt--; + + /* free skb in recv_buf */ + rtw_skb_free(precvbuf->pskb); + + precvbuf->pskb = NULL; + + if (precvbuf->irp_pending == _FALSE) + rtw_read_port(padapter, precvpriv->ff_hwaddr, 0, (unsigned char *)precvbuf); + + +#endif +#if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) + precvbuf->pskb = NULL; +#endif + +} + diff --git a/os_dep/linux/rhashtable.c b/os_dep/linux/rhashtable.c new file mode 100644 index 0000000..af9c9ae --- /dev/null +++ b/os_dep/linux/rhashtable.c @@ -0,0 +1,844 @@ +/* + * Resizable, Scalable, Concurrent Hash Table + * + * Copyright (c) 2015 Herbert Xu + * Copyright (c) 2014-2015 Thomas Graf + * Copyright (c) 2008-2014 Patrick McHardy + * + * Code partially derived from nft_hash + * Rewritten with rehash code from br_multicast plus single list + * pointer as suggested by Josh Triplett + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define HASH_DEFAULT_SIZE 64UL +#define HASH_MIN_SIZE 4U +#define BUCKET_LOCKS_PER_CPU 128UL + +static u32 head_hashfn(struct rhashtable *ht, + const struct bucket_table *tbl, + const struct rhash_head *he) +{ + return rht_head_hashfn(ht, tbl, he, ht->p); +} + +#ifdef CONFIG_PROVE_LOCKING +#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT)) + +int lockdep_rht_mutex_is_held(struct rhashtable *ht) +{ + return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1; +} + +int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash) +{ + spinlock_t *lock = rht_bucket_lock(tbl, hash); + + return (debug_locks) ? lockdep_is_held(lock) : 1; +} +#else +#define ASSERT_RHT_MUTEX(HT) +#endif + + +static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl, + gfp_t gfp) +{ + unsigned int i, size; +#if defined(CONFIG_PROVE_LOCKING) + unsigned int nr_pcpus = 2; +#else + unsigned int nr_pcpus = num_possible_cpus(); +#endif + + nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL); + size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul); + + /* Never allocate more than 0.5 locks per bucket */ + size = min_t(unsigned int, size, tbl->size >> 1); + + if (sizeof(spinlock_t) != 0) { +#ifdef CONFIG_NUMA + if (size * sizeof(spinlock_t) > PAGE_SIZE && + gfp == GFP_KERNEL) + tbl->locks = vmalloc(size * sizeof(spinlock_t)); + else +#endif + tbl->locks = kmalloc_array(size, sizeof(spinlock_t), + gfp); + if (!tbl->locks) + return -ENOMEM; + for (i = 0; i < size; i++) + spin_lock_init(&tbl->locks[i]); + } + tbl->locks_mask = size - 1; + + return 0; +} + +static void bucket_table_free(const struct bucket_table *tbl) +{ + if (tbl) + kvfree(tbl->locks); + + kvfree(tbl); +} + +static void bucket_table_free_rcu(struct rcu_head *head) +{ + bucket_table_free(container_of(head, struct bucket_table, rcu)); +} + +static struct bucket_table *bucket_table_alloc(struct rhashtable *ht, + size_t nbuckets, + gfp_t gfp) +{ + struct bucket_table *tbl = NULL; + size_t size; + int i; + + size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]); + if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER) || + gfp != GFP_KERNEL) + tbl = kzalloc(size, gfp | __GFP_NOWARN | __GFP_NORETRY); + if (tbl == NULL && gfp == GFP_KERNEL) + tbl = vzalloc(size); + if (tbl == NULL) + return NULL; + + tbl->size = nbuckets; + + if (alloc_bucket_locks(ht, tbl, gfp) < 0) { + bucket_table_free(tbl); + return NULL; + } + + INIT_LIST_HEAD(&tbl->walkers); + + get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd)); + + for (i = 0; i < nbuckets; i++) + INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i); + + return tbl; +} + +static struct bucket_table *rhashtable_last_table(struct rhashtable *ht, + struct bucket_table *tbl) +{ + struct bucket_table *new_tbl; + + do { + new_tbl = tbl; + tbl = rht_dereference_rcu(tbl->future_tbl, ht); + } while (tbl); + + return new_tbl; +} + +static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash) +{ + struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); + struct bucket_table *new_tbl = rhashtable_last_table(ht, + rht_dereference_rcu(old_tbl->future_tbl, ht)); + struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash]; + int err = -ENOENT; + struct rhash_head *head, *next, *entry; + spinlock_t *new_bucket_lock; + unsigned int new_hash; + + rht_for_each(entry, old_tbl, old_hash) { + err = 0; + next = rht_dereference_bucket(entry->next, old_tbl, old_hash); + + if (rht_is_a_nulls(next)) + break; + + pprev = &entry->next; + } + + if (err) + goto out; + + new_hash = head_hashfn(ht, new_tbl, entry); + + new_bucket_lock = rht_bucket_lock(new_tbl, new_hash); + + spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING); + head = rht_dereference_bucket(new_tbl->buckets[new_hash], + new_tbl, new_hash); + + RCU_INIT_POINTER(entry->next, head); + + rcu_assign_pointer(new_tbl->buckets[new_hash], entry); + spin_unlock(new_bucket_lock); + + rcu_assign_pointer(*pprev, next); + +out: + return err; +} + +static void rhashtable_rehash_chain(struct rhashtable *ht, + unsigned int old_hash) +{ + struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); + spinlock_t *old_bucket_lock; + + old_bucket_lock = rht_bucket_lock(old_tbl, old_hash); + + spin_lock_bh(old_bucket_lock); + while (!rhashtable_rehash_one(ht, old_hash)) + ; + old_tbl->rehash++; + spin_unlock_bh(old_bucket_lock); +} + +static int rhashtable_rehash_attach(struct rhashtable *ht, + struct bucket_table *old_tbl, + struct bucket_table *new_tbl) +{ + /* Protect future_tbl using the first bucket lock. */ + spin_lock_bh(old_tbl->locks); + + /* Did somebody beat us to it? */ + if (rcu_access_pointer(old_tbl->future_tbl)) { + spin_unlock_bh(old_tbl->locks); + return -EEXIST; + } + + /* Make insertions go into the new, empty table right away. Deletions + * and lookups will be attempted in both tables until we synchronize. + */ + rcu_assign_pointer(old_tbl->future_tbl, new_tbl); + + /* Ensure the new table is visible to readers. */ + smp_wmb(); + + spin_unlock_bh(old_tbl->locks); + + return 0; +} + +static int rhashtable_rehash_table(struct rhashtable *ht) +{ + struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); + struct bucket_table *new_tbl; + struct rhashtable_walker *walker; + unsigned int old_hash; + + new_tbl = rht_dereference(old_tbl->future_tbl, ht); + if (!new_tbl) + return 0; + + for (old_hash = 0; old_hash < old_tbl->size; old_hash++) + rhashtable_rehash_chain(ht, old_hash); + + /* Publish the new table pointer. */ + rcu_assign_pointer(ht->tbl, new_tbl); + + spin_lock(&ht->lock); + list_for_each_entry(walker, &old_tbl->walkers, list) + walker->tbl = NULL; + spin_unlock(&ht->lock); + + /* Wait for readers. All new readers will see the new + * table, and thus no references to the old table will + * remain. + */ + call_rcu(&old_tbl->rcu, bucket_table_free_rcu); + + return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0; +} + +/** + * rhashtable_expand - Expand hash table while allowing concurrent lookups + * @ht: the hash table to expand + * + * A secondary bucket array is allocated and the hash entries are migrated. + * + * This function may only be called in a context where it is safe to call + * synchronize_rcu(), e.g. not within a rcu_read_lock() section. + * + * The caller must ensure that no concurrent resizing occurs by holding + * ht->mutex. + * + * It is valid to have concurrent insertions and deletions protected by per + * bucket locks or concurrent RCU protected lookups and traversals. + */ +static int rhashtable_expand(struct rhashtable *ht) +{ + struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht); + int err; + + ASSERT_RHT_MUTEX(ht); + + old_tbl = rhashtable_last_table(ht, old_tbl); + + new_tbl = bucket_table_alloc(ht, old_tbl->size * 2, GFP_KERNEL); + if (new_tbl == NULL) + return -ENOMEM; + + err = rhashtable_rehash_attach(ht, old_tbl, new_tbl); + if (err) + bucket_table_free(new_tbl); + + return err; +} + +/** + * rhashtable_shrink - Shrink hash table while allowing concurrent lookups + * @ht: the hash table to shrink + * + * This function shrinks the hash table to fit, i.e., the smallest + * size would not cause it to expand right away automatically. + * + * The caller must ensure that no concurrent resizing occurs by holding + * ht->mutex. + * + * The caller must ensure that no concurrent table mutations take place. + * It is however valid to have concurrent lookups if they are RCU protected. + * + * It is valid to have concurrent insertions and deletions protected by per + * bucket locks or concurrent RCU protected lookups and traversals. + */ +static int rhashtable_shrink(struct rhashtable *ht) +{ + struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht); + unsigned int size; + int err; + + ASSERT_RHT_MUTEX(ht); + + size = roundup_pow_of_two(atomic_read(&ht->nelems) * 3 / 2); + if (size < ht->p.min_size) + size = ht->p.min_size; + + if (old_tbl->size <= size) + return 0; + + if (rht_dereference(old_tbl->future_tbl, ht)) + return -EEXIST; + + new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL); + if (new_tbl == NULL) + return -ENOMEM; + + err = rhashtable_rehash_attach(ht, old_tbl, new_tbl); + if (err) + bucket_table_free(new_tbl); + + return err; +} + +static void rht_deferred_worker(struct work_struct *work) +{ + struct rhashtable *ht; + struct bucket_table *tbl; + int err = 0; + + ht = container_of(work, struct rhashtable, run_work); + mutex_lock(&ht->mutex); + + tbl = rht_dereference(ht->tbl, ht); + tbl = rhashtable_last_table(ht, tbl); + + if (rht_grow_above_75(ht, tbl)) + rhashtable_expand(ht); + else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl)) + rhashtable_shrink(ht); + + err = rhashtable_rehash_table(ht); + + mutex_unlock(&ht->mutex); + + if (err) + schedule_work(&ht->run_work); +} + +static bool rhashtable_check_elasticity(struct rhashtable *ht, + struct bucket_table *tbl, + unsigned int hash) +{ + unsigned int elasticity = ht->elasticity; + struct rhash_head *head; + + rht_for_each(head, tbl, hash) + if (!--elasticity) + return true; + + return false; +} + +int rhashtable_insert_rehash(struct rhashtable *ht, + struct bucket_table *tbl) +{ + struct bucket_table *old_tbl; + struct bucket_table *new_tbl; + unsigned int size; + int err; + + old_tbl = rht_dereference_rcu(ht->tbl, ht); + + size = tbl->size; + + err = -EBUSY; + + if (rht_grow_above_75(ht, tbl)) + size *= 2; + /* Do not schedule more than one rehash */ + else if (old_tbl != tbl) + goto fail; + + err = -ENOMEM; + + new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC); + if (new_tbl == NULL) + goto fail; + + err = rhashtable_rehash_attach(ht, tbl, new_tbl); + if (err) { + bucket_table_free(new_tbl); + if (err == -EEXIST) + err = 0; + } else + schedule_work(&ht->run_work); + + return err; + +fail: + /* Do not fail the insert if someone else did a rehash. */ + if (likely(rcu_dereference_raw(tbl->future_tbl))) + return 0; + + /* Schedule async rehash to retry allocation in process context. */ + if (err == -ENOMEM) + schedule_work(&ht->run_work); + + return err; +} + +struct bucket_table *rhashtable_insert_slow(struct rhashtable *ht, + const void *key, + struct rhash_head *obj, + struct bucket_table *tbl) +{ + struct rhash_head *head; + unsigned int hash; + int err; + + tbl = rhashtable_last_table(ht, tbl); + hash = head_hashfn(ht, tbl, obj); + spin_lock_nested(rht_bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING); + + err = -EEXIST; + if (key && rhashtable_lookup_fast(ht, key, ht->p)) + goto exit; + + err = -E2BIG; + if (unlikely(rht_grow_above_max(ht, tbl))) + goto exit; + + err = -EAGAIN; + if (rhashtable_check_elasticity(ht, tbl, hash) || + rht_grow_above_100(ht, tbl)) + goto exit; + + err = 0; + + head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash); + + RCU_INIT_POINTER(obj->next, head); + + rcu_assign_pointer(tbl->buckets[hash], obj); + + atomic_inc(&ht->nelems); + +exit: + spin_unlock(rht_bucket_lock(tbl, hash)); + + if (err == 0) + return NULL; + else if (err == -EAGAIN) + return tbl; + else + return ERR_PTR(err); +} + +/** + * rhashtable_walk_init - Initialise an iterator + * @ht: Table to walk over + * @iter: Hash table Iterator + * + * This function prepares a hash table walk. + * + * Note that if you restart a walk after rhashtable_walk_stop you + * may see the same object twice. Also, you may miss objects if + * there are removals in between rhashtable_walk_stop and the next + * call to rhashtable_walk_start. + * + * For a completely stable walk you should construct your own data + * structure outside the hash table. + * + * This function may sleep so you must not call it from interrupt + * context or with spin locks held. + * + * You must call rhashtable_walk_exit if this function returns + * successfully. + */ +int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter) +{ + iter->ht = ht; + iter->p = NULL; + iter->slot = 0; + iter->skip = 0; + + iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL); + if (!iter->walker) + return -ENOMEM; + + spin_lock(&ht->lock); + iter->walker->tbl = + rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock)); + list_add(&iter->walker->list, &iter->walker->tbl->walkers); + spin_unlock(&ht->lock); + + return 0; +} + +/** + * rhashtable_walk_exit - Free an iterator + * @iter: Hash table Iterator + * + * This function frees resources allocated by rhashtable_walk_init. + */ +void rhashtable_walk_exit(struct rhashtable_iter *iter) +{ + spin_lock(&iter->ht->lock); + if (iter->walker->tbl) + list_del(&iter->walker->list); + spin_unlock(&iter->ht->lock); + kfree(iter->walker); +} + +/** + * rhashtable_walk_start - Start a hash table walk + * @iter: Hash table iterator + * + * Start a hash table walk. Note that we take the RCU lock in all + * cases including when we return an error. So you must always call + * rhashtable_walk_stop to clean up. + * + * Returns zero if successful. + * + * Returns -EAGAIN if resize event occured. Note that the iterator + * will rewind back to the beginning and you may use it immediately + * by calling rhashtable_walk_next. + */ +int rhashtable_walk_start(struct rhashtable_iter *iter) + __acquires(RCU) +{ + struct rhashtable *ht = iter->ht; + + rcu_read_lock(); + + spin_lock(&ht->lock); + if (iter->walker->tbl) + list_del(&iter->walker->list); + spin_unlock(&ht->lock); + + if (!iter->walker->tbl) { + iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht); + return -EAGAIN; + } + + return 0; +} + +/** + * rhashtable_walk_next - Return the next object and advance the iterator + * @iter: Hash table iterator + * + * Note that you must call rhashtable_walk_stop when you are finished + * with the walk. + * + * Returns the next object or NULL when the end of the table is reached. + * + * Returns -EAGAIN if resize event occured. Note that the iterator + * will rewind back to the beginning and you may continue to use it. + */ +void *rhashtable_walk_next(struct rhashtable_iter *iter) +{ + struct bucket_table *tbl = iter->walker->tbl; + struct rhashtable *ht = iter->ht; + struct rhash_head *p = iter->p; + + if (p) { + p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot); + goto next; + } + + for (; iter->slot < tbl->size; iter->slot++) { + int skip = iter->skip; + + rht_for_each_rcu(p, tbl, iter->slot) { + if (!skip) + break; + skip--; + } + +next: + if (!rht_is_a_nulls(p)) { + iter->skip++; + iter->p = p; + return rht_obj(ht, p); + } + + iter->skip = 0; + } + + iter->p = NULL; + + /* Ensure we see any new tables. */ + smp_rmb(); + + iter->walker->tbl = rht_dereference_rcu(tbl->future_tbl, ht); + if (iter->walker->tbl) { + iter->slot = 0; + iter->skip = 0; + return ERR_PTR(-EAGAIN); + } + + return NULL; +} + +/** + * rhashtable_walk_stop - Finish a hash table walk + * @iter: Hash table iterator + * + * Finish a hash table walk. + */ +void rhashtable_walk_stop(struct rhashtable_iter *iter) + __releases(RCU) +{ + struct rhashtable *ht; + struct bucket_table *tbl = iter->walker->tbl; + + if (!tbl) + goto out; + + ht = iter->ht; + + spin_lock(&ht->lock); + if (tbl->rehash < tbl->size) + list_add(&iter->walker->list, &tbl->walkers); + else + iter->walker->tbl = NULL; + spin_unlock(&ht->lock); + + iter->p = NULL; + +out: + rcu_read_unlock(); +} + +static size_t rounded_hashtable_size(const struct rhashtable_params *params) +{ + return max(roundup_pow_of_two(params->nelem_hint * 4 / 3), + (unsigned long)params->min_size); +} + +static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed) +{ + return jhash2(key, length, seed); +} + +/** + * rhashtable_init - initialize a new hash table + * @ht: hash table to be initialized + * @params: configuration parameters + * + * Initializes a new hash table based on the provided configuration + * parameters. A table can be configured either with a variable or + * fixed length key: + * + * Configuration Example 1: Fixed length keys + * struct test_obj { + * int key; + * void * my_member; + * struct rhash_head node; + * }; + * + * struct rhashtable_params params = { + * .head_offset = offsetof(struct test_obj, node), + * .key_offset = offsetof(struct test_obj, key), + * .key_len = sizeof(int), + * .hashfn = jhash, + * .nulls_base = (1U << RHT_BASE_SHIFT), + * }; + * + * Configuration Example 2: Variable length keys + * struct test_obj { + * [...] + * struct rhash_head node; + * }; + * + * u32 my_hash_fn(const void *data, u32 len, u32 seed) + * { + * struct test_obj *obj = data; + * + * return [... hash ...]; + * } + * + * struct rhashtable_params params = { + * .head_offset = offsetof(struct test_obj, node), + * .hashfn = jhash, + * .obj_hashfn = my_hash_fn, + * }; + */ +int rhashtable_init(struct rhashtable *ht, + const struct rhashtable_params *params) +{ + struct bucket_table *tbl; + size_t size; + + size = HASH_DEFAULT_SIZE; + + if ((!params->key_len && !params->obj_hashfn) || + (params->obj_hashfn && !params->obj_cmpfn)) + return -EINVAL; + + if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT)) + return -EINVAL; + + memset(ht, 0, sizeof(*ht)); + mutex_init(&ht->mutex); + spin_lock_init(&ht->lock); + memcpy(&ht->p, params, sizeof(*params)); + + if (params->min_size) + ht->p.min_size = roundup_pow_of_two(params->min_size); + + if (params->max_size) + ht->p.max_size = rounddown_pow_of_two(params->max_size); + + if (params->insecure_max_entries) + ht->p.insecure_max_entries = + rounddown_pow_of_two(params->insecure_max_entries); + else + ht->p.insecure_max_entries = ht->p.max_size * 2; + + ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE); + + if (params->nelem_hint) + size = rounded_hashtable_size(&ht->p); + + /* The maximum (not average) chain length grows with the + * size of the hash table, at a rate of (log N)/(log log N). + * The value of 16 is selected so that even if the hash + * table grew to 2^32 you would not expect the maximum + * chain length to exceed it unless we are under attack + * (or extremely unlucky). + * + * As this limit is only to detect attacks, we don't need + * to set it to a lower value as you'd need the chain + * length to vastly exceed 16 to have any real effect + * on the system. + */ + if (!params->insecure_elasticity) + ht->elasticity = 16; + + if (params->locks_mul) + ht->p.locks_mul = roundup_pow_of_two(params->locks_mul); + else + ht->p.locks_mul = BUCKET_LOCKS_PER_CPU; + + ht->key_len = ht->p.key_len; + if (!params->hashfn) { + ht->p.hashfn = jhash; + + if (!(ht->key_len & (sizeof(u32) - 1))) { + ht->key_len /= sizeof(u32); + ht->p.hashfn = rhashtable_jhash2; + } + } + + tbl = bucket_table_alloc(ht, size, GFP_KERNEL); + if (tbl == NULL) + return -ENOMEM; + + atomic_set(&ht->nelems, 0); + + RCU_INIT_POINTER(ht->tbl, tbl); + + INIT_WORK(&ht->run_work, rht_deferred_worker); + + return 0; +} + +/** + * rhashtable_free_and_destroy - free elements and destroy hash table + * @ht: the hash table to destroy + * @free_fn: callback to release resources of element + * @arg: pointer passed to free_fn + * + * Stops an eventual async resize. If defined, invokes free_fn for each + * element to releasal resources. Please note that RCU protected + * readers may still be accessing the elements. Releasing of resources + * must occur in a compatible manner. Then frees the bucket array. + * + * This function will eventually sleep to wait for an async resize + * to complete. The caller is responsible that no further write operations + * occurs in parallel. + */ +void rhashtable_free_and_destroy(struct rhashtable *ht, + void (*free_fn)(void *ptr, void *arg), + void *arg) +{ + const struct bucket_table *tbl; + unsigned int i; + + cancel_work_sync(&ht->run_work); + + mutex_lock(&ht->mutex); + tbl = rht_dereference(ht->tbl, ht); + if (free_fn) { + for (i = 0; i < tbl->size; i++) { + struct rhash_head *pos, *next; + + for (pos = rht_dereference(tbl->buckets[i], ht), + next = !rht_is_a_nulls(pos) ? + rht_dereference(pos->next, ht) : NULL; + !rht_is_a_nulls(pos); + pos = next, + next = !rht_is_a_nulls(pos) ? + rht_dereference(pos->next, ht) : NULL) + free_fn(rht_obj(ht, pos), arg); + } + } + + bucket_table_free(tbl); + mutex_unlock(&ht->mutex); +} + +void rhashtable_destroy(struct rhashtable *ht) +{ + return rhashtable_free_and_destroy(ht, NULL, NULL); +} + diff --git a/os_dep/linux/rhashtable.h b/os_dep/linux/rhashtable.h new file mode 100644 index 0000000..a6db325 --- /dev/null +++ b/os_dep/linux/rhashtable.h @@ -0,0 +1,827 @@ +/* + * Resizable, Scalable, Concurrent Hash Table + * + * Copyright (c) 2015 Herbert Xu + * Copyright (c) 2014-2015 Thomas Graf + * Copyright (c) 2008-2014 Patrick McHardy + * + * Code partially derived from nft_hash + * Rewritten with rehash code from br_multicast plus single list + * pointer as suggested by Josh Triplett + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#ifndef _LINUX_RHASHTABLE_H +#define _LINUX_RHASHTABLE_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * The end of the chain is marked with a special nulls marks which has + * the following format: + * + * +-------+-----------------------------------------------------+-+ + * | Base | Hash |1| + * +-------+-----------------------------------------------------+-+ + * + * Base (4 bits) : Reserved to distinguish between multiple tables. + * Specified via &struct rhashtable_params.nulls_base. + * Hash (27 bits): Full hash (unmasked) of first element added to bucket + * 1 (1 bit) : Nulls marker (always set) + * + * The remaining bits of the next pointer remain unused for now. + */ +#define RHT_BASE_BITS 4 +#define RHT_HASH_BITS 27 +#define RHT_BASE_SHIFT RHT_HASH_BITS + +/* Base bits plus 1 bit for nulls marker */ +#define RHT_HASH_RESERVED_SPACE (RHT_BASE_BITS + 1) + +struct rhash_head { + struct rhash_head __rcu *next; +}; + +/** + * struct bucket_table - Table of hash buckets + * @size: Number of hash buckets + * @rehash: Current bucket being rehashed + * @hash_rnd: Random seed to fold into hash + * @locks_mask: Mask to apply before accessing locks[] + * @locks: Array of spinlocks protecting individual buckets + * @walkers: List of active walkers + * @rcu: RCU structure for freeing the table + * @future_tbl: Table under construction during rehashing + * @buckets: size * hash buckets + */ +struct bucket_table { + unsigned int size; + unsigned int rehash; + u32 hash_rnd; + unsigned int locks_mask; + spinlock_t *locks; + struct list_head walkers; + struct rcu_head rcu; + + struct bucket_table __rcu *future_tbl; + + struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp; +}; + +/** + * struct rhashtable_compare_arg - Key for the function rhashtable_compare + * @ht: Hash table + * @key: Key to compare against + */ +struct rhashtable_compare_arg { + struct rhashtable *ht; + const void *key; +}; + +typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed); +typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 len, u32 seed); +typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg, + const void *obj); + +struct rhashtable; + +/** + * struct rhashtable_params - Hash table construction parameters + * @nelem_hint: Hint on number of elements, should be 75% of desired size + * @key_len: Length of key + * @key_offset: Offset of key in struct to be hashed + * @head_offset: Offset of rhash_head in struct to be hashed + * @insecure_max_entries: Maximum number of entries (may be exceeded) + * @max_size: Maximum size while expanding + * @min_size: Minimum size while shrinking + * @nulls_base: Base value to generate nulls marker + * @insecure_elasticity: Set to true to disable chain length checks + * @automatic_shrinking: Enable automatic shrinking of tables + * @locks_mul: Number of bucket locks to allocate per cpu (default: 128) + * @hashfn: Hash function (default: jhash2 if !(key_len % 4), or jhash) + * @obj_hashfn: Function to hash object + * @obj_cmpfn: Function to compare key with object + */ +struct rhashtable_params { + size_t nelem_hint; + size_t key_len; + size_t key_offset; + size_t head_offset; + unsigned int insecure_max_entries; + unsigned int max_size; + unsigned int min_size; + u32 nulls_base; + bool insecure_elasticity; + bool automatic_shrinking; + size_t locks_mul; + rht_hashfn_t hashfn; + rht_obj_hashfn_t obj_hashfn; + rht_obj_cmpfn_t obj_cmpfn; +}; + +/** + * struct rhashtable - Hash table handle + * @tbl: Bucket table + * @nelems: Number of elements in table + * @key_len: Key length for hashfn + * @elasticity: Maximum chain length before rehash + * @p: Configuration parameters + * @run_work: Deferred worker to expand/shrink asynchronously + * @mutex: Mutex to protect current/future table swapping + * @lock: Spin lock to protect walker list + */ +struct rhashtable { + struct bucket_table __rcu *tbl; + atomic_t nelems; + unsigned int key_len; + unsigned int elasticity; + struct rhashtable_params p; + struct work_struct run_work; + struct mutex mutex; + spinlock_t lock; +}; + +/** + * struct rhashtable_walker - Hash table walker + * @list: List entry on list of walkers + * @tbl: The table that we were walking over + */ +struct rhashtable_walker { + struct list_head list; + struct bucket_table *tbl; +}; + +/** + * struct rhashtable_iter - Hash table iterator, fits into netlink cb + * @ht: Table to iterate through + * @p: Current pointer + * @walker: Associated rhashtable walker + * @slot: Current slot + * @skip: Number of entries to skip in slot + */ +struct rhashtable_iter { + struct rhashtable *ht; + struct rhash_head *p; + struct rhashtable_walker *walker; + unsigned int slot; + unsigned int skip; +}; + +static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash) +{ + return NULLS_MARKER(ht->p.nulls_base + hash); +} + +#define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \ + ((ptr) = (typeof(ptr)) rht_marker(ht, hash)) + +static inline bool rht_is_a_nulls(const struct rhash_head *ptr) +{ + return ((unsigned long) ptr & 1); +} + +static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr) +{ + return ((unsigned long) ptr) >> 1; +} + +static inline void *rht_obj(const struct rhashtable *ht, + const struct rhash_head *he) +{ + return (char *)he - ht->p.head_offset; +} + +static inline unsigned int rht_bucket_index(const struct bucket_table *tbl, + unsigned int hash) +{ + return (hash >> RHT_HASH_RESERVED_SPACE) & (tbl->size - 1); +} + +static inline unsigned int rht_key_hashfn( + struct rhashtable *ht, const struct bucket_table *tbl, + const void *key, const struct rhashtable_params params) +{ + unsigned int hash; + + /* params must be equal to ht->p if it isn't constant. */ + if (!__builtin_constant_p(params.key_len)) + hash = ht->p.hashfn(key, ht->key_len, tbl->hash_rnd); + else if (params.key_len) { + unsigned int key_len = params.key_len; + + if (params.hashfn) + hash = params.hashfn(key, key_len, tbl->hash_rnd); + else if (key_len & (sizeof(u32) - 1)) + hash = jhash(key, key_len, tbl->hash_rnd); + else + hash = jhash2(key, key_len / sizeof(u32), + tbl->hash_rnd); + } else { + unsigned int key_len = ht->p.key_len; + + if (params.hashfn) + hash = params.hashfn(key, key_len, tbl->hash_rnd); + else + hash = jhash(key, key_len, tbl->hash_rnd); + } + + return rht_bucket_index(tbl, hash); +} + +static inline unsigned int rht_head_hashfn( + struct rhashtable *ht, const struct bucket_table *tbl, + const struct rhash_head *he, const struct rhashtable_params params) +{ + const char *ptr = rht_obj(ht, he); + + return likely(params.obj_hashfn) ? + rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?: + ht->p.key_len, + tbl->hash_rnd)) : + rht_key_hashfn(ht, tbl, ptr + params.key_offset, params); +} + +/** + * rht_grow_above_75 - returns true if nelems > 0.75 * table-size + * @ht: hash table + * @tbl: current table + */ +static inline bool rht_grow_above_75(const struct rhashtable *ht, + const struct bucket_table *tbl) +{ + /* Expand table when exceeding 75% load */ + return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) && + (!ht->p.max_size || tbl->size < ht->p.max_size); +} + +/** + * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size + * @ht: hash table + * @tbl: current table + */ +static inline bool rht_shrink_below_30(const struct rhashtable *ht, + const struct bucket_table *tbl) +{ + /* Shrink table beneath 30% load */ + return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) && + tbl->size > ht->p.min_size; +} + +/** + * rht_grow_above_100 - returns true if nelems > table-size + * @ht: hash table + * @tbl: current table + */ +static inline bool rht_grow_above_100(const struct rhashtable *ht, + const struct bucket_table *tbl) +{ + return atomic_read(&ht->nelems) > tbl->size && + (!ht->p.max_size || tbl->size < ht->p.max_size); +} + +/** + * rht_grow_above_max - returns true if table is above maximum + * @ht: hash table + * @tbl: current table + */ +static inline bool rht_grow_above_max(const struct rhashtable *ht, + const struct bucket_table *tbl) +{ + return ht->p.insecure_max_entries && + atomic_read(&ht->nelems) >= ht->p.insecure_max_entries; +} + +/* The bucket lock is selected based on the hash and protects mutations + * on a group of hash buckets. + * + * A maximum of tbl->size/2 bucket locks is allocated. This ensures that + * a single lock always covers both buckets which may both contains + * entries which link to the same bucket of the old table during resizing. + * This allows to simplify the locking as locking the bucket in both + * tables during resize always guarantee protection. + * + * IMPORTANT: When holding the bucket lock of both the old and new table + * during expansions and shrinking, the old bucket lock must always be + * acquired first. + */ +static inline spinlock_t *rht_bucket_lock(const struct bucket_table *tbl, + unsigned int hash) +{ + return &tbl->locks[hash & tbl->locks_mask]; +} + +#ifdef CONFIG_PROVE_LOCKING +int lockdep_rht_mutex_is_held(struct rhashtable *ht); +int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash); +#else +static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht) +{ + return 1; +} + +static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, + u32 hash) +{ + return 1; +} +#endif /* CONFIG_PROVE_LOCKING */ + +int rhashtable_init(struct rhashtable *ht, + const struct rhashtable_params *params); + +struct bucket_table *rhashtable_insert_slow(struct rhashtable *ht, + const void *key, + struct rhash_head *obj, + struct bucket_table *old_tbl); +int rhashtable_insert_rehash(struct rhashtable *ht, struct bucket_table *tbl); + +int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter); +void rhashtable_walk_exit(struct rhashtable_iter *iter); +int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU); +void *rhashtable_walk_next(struct rhashtable_iter *iter); +void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU); + +void rhashtable_free_and_destroy(struct rhashtable *ht, + void (*free_fn)(void *ptr, void *arg), + void *arg); +void rhashtable_destroy(struct rhashtable *ht); + +#define rht_dereference(p, ht) \ + rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht)) + +#define rht_dereference_rcu(p, ht) \ + rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht)) + +#define rht_dereference_bucket(p, tbl, hash) \ + rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash)) + +#define rht_dereference_bucket_rcu(p, tbl, hash) \ + rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash)) + +#define rht_entry(tpos, pos, member) \ + ({ tpos = container_of(pos, typeof(*tpos), member); 1; }) + +/** + * rht_for_each_continue - continue iterating over hash chain + * @pos: the &struct rhash_head to use as a loop cursor. + * @head: the previous &struct rhash_head to continue from + * @tbl: the &struct bucket_table + * @hash: the hash value / bucket index + */ +#define rht_for_each_continue(pos, head, tbl, hash) \ + for (pos = rht_dereference_bucket(head, tbl, hash); \ + !rht_is_a_nulls(pos); \ + pos = rht_dereference_bucket((pos)->next, tbl, hash)) + +/** + * rht_for_each - iterate over hash chain + * @pos: the &struct rhash_head to use as a loop cursor. + * @tbl: the &struct bucket_table + * @hash: the hash value / bucket index + */ +#define rht_for_each(pos, tbl, hash) \ + rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash) + +/** + * rht_for_each_entry_continue - continue iterating over hash chain + * @tpos: the type * to use as a loop cursor. + * @pos: the &struct rhash_head to use as a loop cursor. + * @head: the previous &struct rhash_head to continue from + * @tbl: the &struct bucket_table + * @hash: the hash value / bucket index + * @member: name of the &struct rhash_head within the hashable struct. + */ +#define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \ + for (pos = rht_dereference_bucket(head, tbl, hash); \ + (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ + pos = rht_dereference_bucket((pos)->next, tbl, hash)) + +/** + * rht_for_each_entry - iterate over hash chain of given type + * @tpos: the type * to use as a loop cursor. + * @pos: the &struct rhash_head to use as a loop cursor. + * @tbl: the &struct bucket_table + * @hash: the hash value / bucket index + * @member: name of the &struct rhash_head within the hashable struct. + */ +#define rht_for_each_entry(tpos, pos, tbl, hash, member) \ + rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \ + tbl, hash, member) + +/** + * rht_for_each_entry_safe - safely iterate over hash chain of given type + * @tpos: the type * to use as a loop cursor. + * @pos: the &struct rhash_head to use as a loop cursor. + * @next: the &struct rhash_head to use as next in loop cursor. + * @tbl: the &struct bucket_table + * @hash: the hash value / bucket index + * @member: name of the &struct rhash_head within the hashable struct. + * + * This hash chain list-traversal primitive allows for the looped code to + * remove the loop cursor from the list. + */ +#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \ + for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \ + next = !rht_is_a_nulls(pos) ? \ + rht_dereference_bucket(pos->next, tbl, hash) : NULL; \ + (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ + pos = next, \ + next = !rht_is_a_nulls(pos) ? \ + rht_dereference_bucket(pos->next, tbl, hash) : NULL) + +/** + * rht_for_each_rcu_continue - continue iterating over rcu hash chain + * @pos: the &struct rhash_head to use as a loop cursor. + * @head: the previous &struct rhash_head to continue from + * @tbl: the &struct bucket_table + * @hash: the hash value / bucket index + * + * This hash chain list-traversal primitive may safely run concurrently with + * the _rcu mutation primitives such as rhashtable_insert() as long as the + * traversal is guarded by rcu_read_lock(). + */ +#define rht_for_each_rcu_continue(pos, head, tbl, hash) \ + for (({barrier(); }), \ + pos = rht_dereference_bucket_rcu(head, tbl, hash); \ + !rht_is_a_nulls(pos); \ + pos = rcu_dereference_raw(pos->next)) + +/** + * rht_for_each_rcu - iterate over rcu hash chain + * @pos: the &struct rhash_head to use as a loop cursor. + * @tbl: the &struct bucket_table + * @hash: the hash value / bucket index + * + * This hash chain list-traversal primitive may safely run concurrently with + * the _rcu mutation primitives such as rhashtable_insert() as long as the + * traversal is guarded by rcu_read_lock(). + */ +#define rht_for_each_rcu(pos, tbl, hash) \ + rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash) + +/** + * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain + * @tpos: the type * to use as a loop cursor. + * @pos: the &struct rhash_head to use as a loop cursor. + * @head: the previous &struct rhash_head to continue from + * @tbl: the &struct bucket_table + * @hash: the hash value / bucket index + * @member: name of the &struct rhash_head within the hashable struct. + * + * This hash chain list-traversal primitive may safely run concurrently with + * the _rcu mutation primitives such as rhashtable_insert() as long as the + * traversal is guarded by rcu_read_lock(). + */ +#define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \ + for (({barrier(); }), \ + pos = rht_dereference_bucket_rcu(head, tbl, hash); \ + (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ + pos = rht_dereference_bucket_rcu(pos->next, tbl, hash)) + +/** + * rht_for_each_entry_rcu - iterate over rcu hash chain of given type + * @tpos: the type * to use as a loop cursor. + * @pos: the &struct rhash_head to use as a loop cursor. + * @tbl: the &struct bucket_table + * @hash: the hash value / bucket index + * @member: name of the &struct rhash_head within the hashable struct. + * + * This hash chain list-traversal primitive may safely run concurrently with + * the _rcu mutation primitives such as rhashtable_insert() as long as the + * traversal is guarded by rcu_read_lock(). + */ +#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \ + rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\ + tbl, hash, member) + +static inline int rhashtable_compare(struct rhashtable_compare_arg *arg, + const void *obj) +{ + struct rhashtable *ht = arg->ht; + const char *ptr = obj; + + return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len); +} + +/** + * rhashtable_lookup_fast - search hash table, inlined version + * @ht: hash table + * @key: the pointer to the key + * @params: hash table parameters + * + * Computes the hash value for the key and traverses the bucket chain looking + * for a entry with an identical key. The first matching entry is returned. + * + * Returns the first entry on which the compare function returned true. + */ +static inline void *rhashtable_lookup_fast( + struct rhashtable *ht, const void *key, + const struct rhashtable_params params) +{ + struct rhashtable_compare_arg arg = { + .ht = ht, + .key = key, + }; + const struct bucket_table *tbl; + struct rhash_head *he; + unsigned int hash; + + rcu_read_lock(); + + tbl = rht_dereference_rcu(ht->tbl, ht); +restart: + hash = rht_key_hashfn(ht, tbl, key, params); + rht_for_each_rcu(he, tbl, hash) { + if (params.obj_cmpfn ? + params.obj_cmpfn(&arg, rht_obj(ht, he)) : + rhashtable_compare(&arg, rht_obj(ht, he))) + continue; + rcu_read_unlock(); + return rht_obj(ht, he); + } + + /* Ensure we see any new tables. */ + smp_rmb(); + + tbl = rht_dereference_rcu(tbl->future_tbl, ht); + if (unlikely(tbl)) + goto restart; + rcu_read_unlock(); + + return NULL; +} + +/* Internal function, please use rhashtable_insert_fast() instead */ +static inline int __rhashtable_insert_fast( + struct rhashtable *ht, const void *key, struct rhash_head *obj, + const struct rhashtable_params params) +{ + struct rhashtable_compare_arg arg = { + .ht = ht, + .key = key, + }; + struct bucket_table *tbl, *new_tbl; + struct rhash_head *head; + spinlock_t *lock; + unsigned int elasticity; + unsigned int hash; + int err; + +restart: + rcu_read_lock(); + + tbl = rht_dereference_rcu(ht->tbl, ht); + + /* All insertions must grab the oldest table containing + * the hashed bucket that is yet to be rehashed. + */ + for (;;) { + hash = rht_head_hashfn(ht, tbl, obj, params); + lock = rht_bucket_lock(tbl, hash); + spin_lock_bh(lock); + + if (tbl->rehash <= hash) + break; + + spin_unlock_bh(lock); + tbl = rht_dereference_rcu(tbl->future_tbl, ht); + } + + new_tbl = rht_dereference_rcu(tbl->future_tbl, ht); + if (unlikely(new_tbl)) { + tbl = rhashtable_insert_slow(ht, key, obj, new_tbl); + if (!IS_ERR_OR_NULL(tbl)) + goto slow_path; + + err = PTR_ERR(tbl); + goto out; + } + + err = -E2BIG; + if (unlikely(rht_grow_above_max(ht, tbl))) + goto out; + + if (unlikely(rht_grow_above_100(ht, tbl))) { +slow_path: + spin_unlock_bh(lock); + err = rhashtable_insert_rehash(ht, tbl); + rcu_read_unlock(); + if (err) + return err; + + goto restart; + } + + err = -EEXIST; + elasticity = ht->elasticity; + rht_for_each(head, tbl, hash) { + if (key && + unlikely(!(params.obj_cmpfn ? + params.obj_cmpfn(&arg, rht_obj(ht, head)) : + rhashtable_compare(&arg, rht_obj(ht, head))))) + goto out; + if (!--elasticity) + goto slow_path; + } + + err = 0; + + head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash); + + RCU_INIT_POINTER(obj->next, head); + + rcu_assign_pointer(tbl->buckets[hash], obj); + + atomic_inc(&ht->nelems); + if (rht_grow_above_75(ht, tbl)) + schedule_work(&ht->run_work); + +out: + spin_unlock_bh(lock); + rcu_read_unlock(); + + return err; +} + +/** + * rhashtable_insert_fast - insert object into hash table + * @ht: hash table + * @obj: pointer to hash head inside object + * @params: hash table parameters + * + * Will take a per bucket spinlock to protect against mutual mutations + * on the same bucket. Multiple insertions may occur in parallel unless + * they map to the same bucket lock. + * + * It is safe to call this function from atomic context. + * + * Will trigger an automatic deferred table resizing if the size grows + * beyond the watermark indicated by grow_decision() which can be passed + * to rhashtable_init(). + */ +static inline int rhashtable_insert_fast( + struct rhashtable *ht, struct rhash_head *obj, + const struct rhashtable_params params) +{ + return __rhashtable_insert_fast(ht, NULL, obj, params); +} + +/** + * rhashtable_lookup_insert_fast - lookup and insert object into hash table + * @ht: hash table + * @obj: pointer to hash head inside object + * @params: hash table parameters + * + * Locks down the bucket chain in both the old and new table if a resize + * is in progress to ensure that writers can't remove from the old table + * and can't insert to the new table during the atomic operation of search + * and insertion. Searches for duplicates in both the old and new table if + * a resize is in progress. + * + * This lookup function may only be used for fixed key hash table (key_len + * parameter set). It will BUG() if used inappropriately. + * + * It is safe to call this function from atomic context. + * + * Will trigger an automatic deferred table resizing if the size grows + * beyond the watermark indicated by grow_decision() which can be passed + * to rhashtable_init(). + */ +static inline int rhashtable_lookup_insert_fast( + struct rhashtable *ht, struct rhash_head *obj, + const struct rhashtable_params params) +{ + const char *key = rht_obj(ht, obj); + + BUG_ON(ht->p.obj_hashfn); + + return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, + params); +} + +/** + * rhashtable_lookup_insert_key - search and insert object to hash table + * with explicit key + * @ht: hash table + * @key: key + * @obj: pointer to hash head inside object + * @params: hash table parameters + * + * Locks down the bucket chain in both the old and new table if a resize + * is in progress to ensure that writers can't remove from the old table + * and can't insert to the new table during the atomic operation of search + * and insertion. Searches for duplicates in both the old and new table if + * a resize is in progress. + * + * Lookups may occur in parallel with hashtable mutations and resizing. + * + * Will trigger an automatic deferred table resizing if the size grows + * beyond the watermark indicated by grow_decision() which can be passed + * to rhashtable_init(). + * + * Returns zero on success. + */ +static inline int rhashtable_lookup_insert_key( + struct rhashtable *ht, const void *key, struct rhash_head *obj, + const struct rhashtable_params params) +{ + BUG_ON(!ht->p.obj_hashfn || !key); + + return __rhashtable_insert_fast(ht, key, obj, params); +} + +/* Internal function, please use rhashtable_remove_fast() instead */ +static inline int __rhashtable_remove_fast( + struct rhashtable *ht, struct bucket_table *tbl, + struct rhash_head *obj, const struct rhashtable_params params) +{ + struct rhash_head __rcu **pprev; + struct rhash_head *he; + spinlock_t * lock; + unsigned int hash; + int err = -ENOENT; + + hash = rht_head_hashfn(ht, tbl, obj, params); + lock = rht_bucket_lock(tbl, hash); + + spin_lock_bh(lock); + + pprev = &tbl->buckets[hash]; + rht_for_each(he, tbl, hash) { + if (he != obj) { + pprev = &he->next; + continue; + } + + rcu_assign_pointer(*pprev, obj->next); + err = 0; + break; + } + + spin_unlock_bh(lock); + + return err; +} + +/** + * rhashtable_remove_fast - remove object from hash table + * @ht: hash table + * @obj: pointer to hash head inside object + * @params: hash table parameters + * + * Since the hash chain is single linked, the removal operation needs to + * walk the bucket chain upon removal. The removal operation is thus + * considerable slow if the hash table is not correctly sized. + * + * Will automatically shrink the table via rhashtable_expand() if the + * shrink_decision function specified at rhashtable_init() returns true. + * + * Returns zero on success, -ENOENT if the entry could not be found. + */ +static inline int rhashtable_remove_fast( + struct rhashtable *ht, struct rhash_head *obj, + const struct rhashtable_params params) +{ + struct bucket_table *tbl; + int err; + + rcu_read_lock(); + + tbl = rht_dereference_rcu(ht->tbl, ht); + + /* Because we have already taken (and released) the bucket + * lock in old_tbl, if we find that future_tbl is not yet + * visible then that guarantees the entry to still be in + * the old tbl if it exists. + */ + while ((err = __rhashtable_remove_fast(ht, tbl, obj, params)) && + (tbl = rht_dereference_rcu(tbl->future_tbl, ht))) + ; + + if (err) + goto out; + + atomic_dec(&ht->nelems); + if (unlikely(ht->p.automatic_shrinking && + rht_shrink_below_30(ht, tbl))) + schedule_work(&ht->run_work); + +out: + rcu_read_unlock(); + + return err; +} + +#endif /* _LINUX_RHASHTABLE_H */ + diff --git a/os_dep/linux/rtw_android.c b/os_dep/linux/rtw_android.c new file mode 100644 index 0000000..10f8f23 --- /dev/null +++ b/os_dep/linux/rtw_android.c @@ -0,0 +1,1346 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ + +#ifdef CONFIG_GPIO_WAKEUP +#include +#endif + +#include + +#if defined(RTW_ENABLE_WIFI_CONTROL_FUNC) +#include +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + #include +#else + #include +#endif +#endif /* defined(RTW_ENABLE_WIFI_CONTROL_FUNC) */ + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 0, 0)) +#define strnicmp strncasecmp +#endif /* Linux kernel >= 4.0.0 */ + +#ifdef CONFIG_GPIO_WAKEUP +#include +#include +#endif + +#include "rtw_version.h" + +extern void macstr2num(u8 *dst, u8 *src); + +const char *android_wifi_cmd_str[ANDROID_WIFI_CMD_MAX] = { + "START", + "STOP", + "SCAN-ACTIVE", + "SCAN-PASSIVE", + "RSSI", + "LINKSPEED", + "RXFILTER-START", + "RXFILTER-STOP", + "RXFILTER-ADD", + "RXFILTER-REMOVE", + "BTCOEXSCAN-START", + "BTCOEXSCAN-STOP", + "BTCOEXMODE", + "SETSUSPENDMODE", + "SETSUSPENDOPT", + "P2P_DEV_ADDR", + "SETFWPATH", + "SETBAND", + "GETBAND", + "COUNTRY", + "P2P_SET_NOA", + "P2P_GET_NOA", + "P2P_SET_PS", + "SET_AP_WPS_P2P_IE", + + "MIRACAST", + +#ifdef CONFIG_PNO_SUPPORT + "PNOSSIDCLR", + "PNOSETUP", + "PNOFORCE", + "PNODEBUG", +#endif + + "MACADDR", + + "BLOCK_SCAN", + "BLOCK", + "WFD-ENABLE", + "WFD-DISABLE", + "WFD-SET-TCPPORT", + "WFD-SET-MAXTPUT", + "WFD-SET-DEVTYPE", + "SET_DTIM", + "HOSTAPD_SET_MACADDR_ACL", + "HOSTAPD_ACL_ADD_STA", + "HOSTAPD_ACL_REMOVE_STA", +#if defined(CONFIG_GTK_OL) && (LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0)) + "GTK_REKEY_OFFLOAD", +#endif /* CONFIG_GTK_OL */ +/* Private command for P2P disable*/ + "P2P_DISABLE", + "SET_AEK", + "EXT_AUTH_STATUS", + "DRIVER_VERSION" +}; + +#ifdef CONFIG_PNO_SUPPORT +#define PNO_TLV_PREFIX 'S' +#define PNO_TLV_VERSION '1' +#define PNO_TLV_SUBVERSION '2' +#define PNO_TLV_RESERVED '0' +#define PNO_TLV_TYPE_SSID_IE 'S' +#define PNO_TLV_TYPE_TIME 'T' +#define PNO_TLV_FREQ_REPEAT 'R' +#define PNO_TLV_FREQ_EXPO_MAX 'M' + +typedef struct cmd_tlv { + char prefix; + char version; + char subver; + char reserved; +} cmd_tlv_t; + +#ifdef CONFIG_PNO_SET_DEBUG +char pno_in_example[] = { + 'P', 'N', 'O', 'S', 'E', 'T', 'U', 'P', ' ', + 'S', '1', '2', '0', + 'S', /* 1 */ + 0x05, + 'd', 'l', 'i', 'n', 'k', + 'S', /* 2 */ + 0x06, + 'B', 'U', 'F', 'B', 'U', 'F', + 'S', /* 3 */ + 0x20, + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '!', '@', '#', '$', '%', '^', + 'S', /* 4 */ + 0x0a, + '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', + 'T', + '0', '5', + 'R', + '2', + 'M', + '2', + 0x00 +}; +#endif /* CONFIG_PNO_SET_DEBUG */ +#endif /* PNO_SUPPORT */ + +typedef struct android_wifi_priv_cmd { + char *buf; + int used_len; + int total_len; +} android_wifi_priv_cmd; + +#ifdef CONFIG_COMPAT +typedef struct compat_android_wifi_priv_cmd { + compat_uptr_t buf; + int used_len; + int total_len; +} compat_android_wifi_priv_cmd; +#endif /* CONFIG_COMPAT */ + +/** + * Local (static) functions and variables + */ + +/* Initialize g_wifi_on to 1 so dhd_bus_start will be called for the first + * time (only) in dhd_open, subsequential wifi on will be handled by + * wl_android_wifi_on + */ +static int g_wifi_on = _TRUE; + +unsigned int oob_irq = 0; +unsigned int oob_gpio = 0; + +#ifdef CONFIG_PNO_SUPPORT +/* + * rtw_android_pno_setup + * Description: + * This is used for private command. + * + * Parameter: + * net: net_device + * command: parameters from private command + * total_len: the length of the command. + * + * */ +static int rtw_android_pno_setup(struct net_device *net, char *command, int total_len) +{ + pno_ssid_t pno_ssids_local[MAX_PNO_LIST_COUNT]; + int res = -1; + int nssid = 0; + cmd_tlv_t *cmd_tlv_temp; + char *str_ptr; + int tlv_size_left; + int pno_time = 0; + int pno_repeat = 0; + int pno_freq_expo_max = 0; + int cmdlen = strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_PNOSETUP_SET]) + 1; + +#ifdef CONFIG_PNO_SET_DEBUG + int i; + char *p; + p = pno_in_example; + + total_len = sizeof(pno_in_example); + str_ptr = p + cmdlen; +#else + str_ptr = command + cmdlen; +#endif + + if (total_len < (cmdlen + sizeof(cmd_tlv_t))) { + RTW_INFO("%s argument=%d less min size\n", __func__, total_len); + goto exit_proc; + } + + tlv_size_left = total_len - cmdlen; + + cmd_tlv_temp = (cmd_tlv_t *)str_ptr; + memset(pno_ssids_local, 0, sizeof(pno_ssids_local)); + + if ((cmd_tlv_temp->prefix == PNO_TLV_PREFIX) && + (cmd_tlv_temp->version == PNO_TLV_VERSION) && + (cmd_tlv_temp->subver == PNO_TLV_SUBVERSION)) { + + str_ptr += sizeof(cmd_tlv_t); + tlv_size_left -= sizeof(cmd_tlv_t); + + nssid = rtw_parse_ssid_list_tlv(&str_ptr, pno_ssids_local, + MAX_PNO_LIST_COUNT, &tlv_size_left); + if (nssid <= 0) { + RTW_INFO("SSID is not presented or corrupted ret=%d\n", nssid); + goto exit_proc; + } else { + if ((str_ptr[0] != PNO_TLV_TYPE_TIME) || (tlv_size_left <= 1)) { + RTW_INFO("%s scan duration corrupted field size %d\n", + __func__, tlv_size_left); + goto exit_proc; + } + str_ptr++; + pno_time = simple_strtoul(str_ptr, &str_ptr, 16); + RTW_INFO("%s: pno_time=%d\n", __func__, pno_time); + + if (str_ptr[0] != 0) { + if ((str_ptr[0] != PNO_TLV_FREQ_REPEAT)) { + RTW_INFO("%s pno repeat : corrupted field\n", + __func__); + goto exit_proc; + } + str_ptr++; + pno_repeat = simple_strtoul(str_ptr, &str_ptr, 16); + RTW_INFO("%s :got pno_repeat=%d\n", __FUNCTION__, pno_repeat); + if (str_ptr[0] != PNO_TLV_FREQ_EXPO_MAX) { + RTW_INFO("%s FREQ_EXPO_MAX corrupted field size\n", + __func__); + goto exit_proc; + } + str_ptr++; + pno_freq_expo_max = simple_strtoul(str_ptr, &str_ptr, 16); + RTW_INFO("%s: pno_freq_expo_max=%d\n", + __func__, pno_freq_expo_max); + } + } + } else { + RTW_INFO("%s get wrong TLV command\n", __FUNCTION__); + goto exit_proc; + } + + res = rtw_dev_pno_set(net, pno_ssids_local, nssid, pno_time, pno_repeat, pno_freq_expo_max); + +#ifdef CONFIG_PNO_SET_DEBUG + rtw_dev_pno_debug(net); +#endif + +exit_proc: + return res; +} + +/* + * rtw_android_cfg80211_pno_setup + * Description: + * This is used for cfg80211 sched_scan. + * + * Parameter: + * net: net_device + * request: cfg80211_request + * */ + +int rtw_android_cfg80211_pno_setup(struct net_device *net, + struct cfg80211_ssid *ssids, int n_ssids, int interval) +{ + int res = -1; + int nssid = 0; + int pno_time = 0; + int pno_repeat = 0; + int pno_freq_expo_max = 0; + int index = 0; + pno_ssid_t pno_ssids_local[MAX_PNO_LIST_COUNT]; + + if (n_ssids > MAX_PNO_LIST_COUNT || n_ssids < 0) { + RTW_INFO("%s: nssids(%d) is invalid.\n", __func__, n_ssids); + return -EINVAL; + } + + memset(pno_ssids_local, 0, sizeof(pno_ssids_local)); + + nssid = n_ssids; + + for (index = 0 ; index < nssid ; index++) { + pno_ssids_local[index].SSID_len = ssids[index].ssid_len; + memcpy(pno_ssids_local[index].SSID, ssids[index].ssid, + ssids[index].ssid_len); + } +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0) + if(ssids) + rtw_mfree((u8 *)ssids, (n_ssids * sizeof(struct cfg80211_ssid))); +#endif + pno_time = (interval / 1000); + + RTW_INFO("%s: nssids: %d, pno_time=%d\n", __func__, nssid, pno_time); + + res = rtw_dev_pno_set(net, pno_ssids_local, nssid, pno_time, + pno_repeat, pno_freq_expo_max); + +#ifdef CONFIG_PNO_SET_DEBUG + rtw_dev_pno_debug(net); +#endif +exit_proc: + return res; +} + +int rtw_android_pno_enable(struct net_device *net, int pno_enable) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(net); + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(padapter); + + if (pwrctl) { + pwrctl->wowlan_pno_enable = pno_enable; + RTW_INFO("%s: wowlan_pno_enable: %d\n", __func__, pwrctl->wowlan_pno_enable); + if (pwrctl->wowlan_pno_enable == 0) { + if (pwrctl->pnlo_info != NULL) { + rtw_mfree((u8 *)pwrctl->pnlo_info, sizeof(pno_nlo_info_t)); + pwrctl->pnlo_info = NULL; + } + if (pwrctl->pno_ssid_list != NULL) { + rtw_mfree((u8 *)pwrctl->pno_ssid_list, sizeof(pno_ssid_list_t)); + pwrctl->pno_ssid_list = NULL; + } + if (pwrctl->pscan_info != NULL) { + rtw_mfree((u8 *)pwrctl->pscan_info, sizeof(pno_scan_info_t)); + pwrctl->pscan_info = NULL; + } + } + return 0; + } else + return -1; +} +#endif /* CONFIG_PNO_SUPPORT */ + +int rtw_android_cmdstr_to_num(char *cmdstr) +{ + int cmd_num; + for (cmd_num = 0 ; cmd_num < ANDROID_WIFI_CMD_MAX; cmd_num++) + if (0 == strnicmp(cmdstr , android_wifi_cmd_str[cmd_num], strlen(android_wifi_cmd_str[cmd_num]))) + break; + + return cmd_num; +} + +int rtw_android_get_rssi(struct net_device *net, char *command, int total_len) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(net); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct wlan_network *pcur_network = &pmlmepriv->cur_network; + int bytes_written = 0; + + if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _TRUE) { + bytes_written += snprintf(&command[bytes_written], total_len, "%s rssi %d", + pcur_network->network.Ssid.Ssid, padapter->recvpriv.rssi); + } + + return bytes_written; +} + +int rtw_android_get_link_speed(struct net_device *net, char *command, int total_len) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(net); + int bytes_written = 0; + u16 link_speed = 0; + + link_speed = rtw_get_cur_max_rate(padapter) / 10; + bytes_written = snprintf(command, total_len, "LinkSpeed %d", link_speed); + + return bytes_written; +} + +int rtw_android_get_macaddr(struct net_device *net, char *command, int total_len) +{ + int bytes_written = 0; + + bytes_written = snprintf(command, total_len, "Macaddr = "MAC_FMT, MAC_ARG(net->dev_addr)); + return bytes_written; +} + +int rtw_android_set_country(struct net_device *net, char *command, int total_len) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(net); + char *country_code = command + strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_COUNTRY]) + 1; + int ret = _FAIL; + + ret = rtw_set_country(adapter, country_code); + + return (ret == _SUCCESS) ? 0 : -1; +} + +int rtw_android_get_p2p_dev_addr(struct net_device *net, char *command, int total_len) +{ + int bytes_written = 0; + + /* We use the same address as our HW MAC address */ + _rtw_memcpy(command, net->dev_addr, ETH_ALEN); + + bytes_written = ETH_ALEN; + return bytes_written; +} + +int rtw_android_set_block_scan(struct net_device *net, char *command, int total_len) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(net); + char *block_value = command + strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_BLOCK_SCAN]) + 1; + +#ifdef CONFIG_IOCTL_CFG80211 + adapter_wdev_data(adapter)->block_scan = (*block_value == '0') ? _FALSE : _TRUE; +#endif + + return 0; +} + +int rtw_android_set_block(struct net_device *net, char *command, int total_len) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(net); + char *block_value = command + strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_BLOCK]) + 1; + +#ifdef CONFIG_IOCTL_CFG80211 + adapter_wdev_data(adapter)->block = (*block_value == '0') ? _FALSE : _TRUE; +#endif + + return 0; +} + +int rtw_android_setband(struct net_device *net, char *command, int total_len) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(net); + char *arg = command + strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_SETBAND]) + 1; + u32 band = WIFI_FREQUENCY_BAND_AUTO; + int ret = _FAIL; + + if (sscanf(arg, "%u", &band) >= 1) + ret = rtw_set_band(adapter, band); + + return (ret == _SUCCESS) ? 0 : -1; +} + +int rtw_android_getband(struct net_device *net, char *command, int total_len) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(net); + int bytes_written = 0; + + bytes_written = snprintf(command, total_len, "%u", adapter->setband); + + return bytes_written; +} + +#ifdef CONFIG_WFD +int rtw_android_set_miracast_mode(struct net_device *net, char *command, int total_len) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(net); + struct wifi_display_info *wfd_info = &adapter->wfd_info; + char *arg = command + strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_MIRACAST]) + 1; + u8 mode; + int num; + int ret = _FAIL; + + num = sscanf(arg, "%hhu", &mode); + + if (num < 1) + goto exit; + + switch (mode) { + case 1: /* soruce */ + mode = MIRACAST_SOURCE; + break; + case 2: /* sink */ + mode = MIRACAST_SINK; + break; + case 0: /* disabled */ + default: + mode = MIRACAST_DISABLED; + break; + } + wfd_info->stack_wfd_mode = mode; + RTW_INFO("stack miracast mode: %s\n", get_miracast_mode_str(wfd_info->stack_wfd_mode)); + + ret = _SUCCESS; + +exit: + return (ret == _SUCCESS) ? 0 : -1; +} +#endif /* CONFIG_WFD */ + +int get_int_from_command(char *pcmd) +{ + int i = 0; + + for (i = 0; i < strlen(pcmd); i++) { + if (pcmd[i] == '=') { + /* Skip the '=' and space characters. */ + i += 2; + break; + } + } + return rtw_atoi(pcmd + i) ; +} + +#if defined(CONFIG_GTK_OL) && (LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0)) +int rtw_gtk_offload(struct net_device *net, u8 *cmd_ptr) +{ + int i; + /* u8 *cmd_ptr = priv_cmd.buf; */ + struct sta_info *psta; + _adapter *padapter = (_adapter *)rtw_netdev_priv(net); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct sta_priv *pstapriv = &padapter->stapriv; + struct security_priv *psecuritypriv = &(padapter->securitypriv); + psta = rtw_get_stainfo(pstapriv, get_bssid(pmlmepriv)); + + + if (psta == NULL) + RTW_INFO("%s, : Obtain Sta_info fail\n", __func__); + else { + /* string command length of "GTK_REKEY_OFFLOAD" */ + cmd_ptr += 18; + + _rtw_memcpy(psta->kek, cmd_ptr, RTW_KEK_LEN); + cmd_ptr += RTW_KEK_LEN; + /* + printk("supplicant KEK: "); + for(i=0;ikek[i]); + printk("\n supplicant KCK: "); + */ + _rtw_memcpy(psta->kck, cmd_ptr, RTW_KCK_LEN); + cmd_ptr += RTW_KCK_LEN; + /* + for(i=0;ikck[i]); + */ + _rtw_memcpy(psta->replay_ctr, cmd_ptr, RTW_REPLAY_CTR_LEN); + psecuritypriv->binstallKCK_KEK = _TRUE; + + /* printk("\nREPLAY_CTR: "); */ + /* for(i=0;ireplay_ctr[i]); */ + } + + return _SUCCESS; +} +#endif /* CONFIG_GTK_OL */ + +#ifdef CONFIG_RTW_MESH_AEK +static int rtw_android_set_aek(struct net_device *ndev, char *command, int total_len) +{ +#define SET_AEK_DATA_LEN (ETH_ALEN + 32) + + _adapter *adapter = (_adapter *)rtw_netdev_priv(ndev); + u8 *addr; + u8 *aek; + int err = 0; + + if (total_len - strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_SET_AEK]) - 1 != SET_AEK_DATA_LEN) { + err = -EINVAL; + goto exit; + } + + addr = command + strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_SET_AEK]) + 1; + aek = addr + ETH_ALEN; + + RTW_PRINT(FUNC_NDEV_FMT" addr="MAC_FMT"\n" + , FUNC_NDEV_ARG(ndev), MAC_ARG(addr)); + if (0) + RTW_PRINT(FUNC_NDEV_FMT" aek="KEY_FMT KEY_FMT"\n" + , FUNC_NDEV_ARG(ndev), KEY_ARG(aek), KEY_ARG(aek + 16)); + + if (rtw_mesh_plink_set_aek(adapter, addr, aek) != _SUCCESS) + err = -ENOENT; + +exit: + return err; +} +#endif /* CONFIG_RTW_MESH_AEK */ + +int rtw_android_priv_cmd(struct net_device *net, struct ifreq *ifr, int cmd) +{ + #define PRIVATE_COMMAND_MAX_LEN 8192 + int ret = 0; + char *command = NULL; + int cmd_num; + int bytes_written = 0; +#ifdef CONFIG_PNO_SUPPORT + uint cmdlen = 0; + uint pno_enable = 0; +#endif + android_wifi_priv_cmd priv_cmd; + _adapter *padapter = (_adapter *) rtw_netdev_priv(net); +#ifdef CONFIG_WFD + struct wifi_display_info *pwfd_info; +#endif + + rtw_lock_suspend(); + + if (!ifr->ifr_data) { + ret = -EINVAL; + goto exit; + } + if (padapter->registrypriv.mp_mode == 1) { + ret = -EINVAL; + goto exit; + } +#ifdef CONFIG_COMPAT +#if (KERNEL_VERSION(4, 6, 0) > LINUX_VERSION_CODE) + if (is_compat_task()) { +#else + if (in_compat_syscall()) { +#endif + /* User space is 32-bit, use compat ioctl */ + compat_android_wifi_priv_cmd compat_priv_cmd; + + if (copy_from_user(&compat_priv_cmd, ifr->ifr_data, sizeof(compat_android_wifi_priv_cmd))) { + ret = -EFAULT; + goto exit; + } + priv_cmd.buf = compat_ptr(compat_priv_cmd.buf); + priv_cmd.used_len = compat_priv_cmd.used_len; + priv_cmd.total_len = compat_priv_cmd.total_len; + } else +#endif /* CONFIG_COMPAT */ + if (copy_from_user(&priv_cmd, ifr->ifr_data, sizeof(android_wifi_priv_cmd))) { + ret = -EFAULT; + goto exit; + } + if (padapter->registrypriv.mp_mode == 1) { + ret = -EFAULT; + goto exit; + } + /*RTW_INFO("%s priv_cmd.buf=%p priv_cmd.total_len=%d priv_cmd.used_len=%d\n",__func__,priv_cmd.buf,priv_cmd.total_len,priv_cmd.used_len);*/ + if (priv_cmd.total_len > PRIVATE_COMMAND_MAX_LEN || priv_cmd.total_len < 0) { + RTW_WARN("%s: invalid private command (%d)\n", __FUNCTION__, + priv_cmd.total_len); + ret = -EFAULT; + goto exit; + } + + command = rtw_zmalloc(priv_cmd.total_len+1); + if (!command) { + RTW_INFO("%s: failed to allocate memory\n", __FUNCTION__); + ret = -ENOMEM; + goto exit; + } + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)) + if (!access_ok(priv_cmd.buf, priv_cmd.total_len)) { + #else + if (!access_ok(VERIFY_READ, priv_cmd.buf, priv_cmd.total_len)) { + #endif + RTW_INFO("%s: failed to access memory\n", __FUNCTION__); + ret = -EFAULT; + goto exit; + } + if (copy_from_user(command, (void *)priv_cmd.buf, priv_cmd.total_len)) { + ret = -EFAULT; + goto exit; + } + command[priv_cmd.total_len] = '\0'; + RTW_INFO("%s: Android private cmd \"%s\" on %s\n" + , __FUNCTION__, command, ifr->ifr_name); + + cmd_num = rtw_android_cmdstr_to_num(command); + + switch (cmd_num) { + case ANDROID_WIFI_CMD_START: + /* bytes_written = wl_android_wifi_on(net); */ + goto response; + case ANDROID_WIFI_CMD_SETFWPATH: + goto response; + } + + if (!g_wifi_on) { + RTW_INFO("%s: Ignore private cmd \"%s\" - iface %s is down\n" + , __FUNCTION__, command, ifr->ifr_name); + ret = 0; + goto exit; + } + + if (!hal_chk_wl_func(padapter, WL_FUNC_MIRACAST)) { + switch (cmd_num) { + case ANDROID_WIFI_CMD_WFD_ENABLE: + case ANDROID_WIFI_CMD_WFD_DISABLE: + case ANDROID_WIFI_CMD_WFD_SET_TCPPORT: + case ANDROID_WIFI_CMD_WFD_SET_MAX_TPUT: + case ANDROID_WIFI_CMD_WFD_SET_DEVTYPE: + goto response; + } + } + + switch (cmd_num) { + + case ANDROID_WIFI_CMD_STOP: + /* bytes_written = wl_android_wifi_off(net); */ + break; + + case ANDROID_WIFI_CMD_SCAN_ACTIVE: + /* rtw_set_scan_mode((_adapter *)rtw_netdev_priv(net), SCAN_ACTIVE); */ +#ifdef CONFIG_PLATFORM_MSTAR +#ifdef CONFIG_IOCTL_CFG80211 + adapter_wdev_data((_adapter *)rtw_netdev_priv(net))->bandroid_scan = _TRUE; +#endif /* CONFIG_IOCTL_CFG80211 */ +#endif /* CONFIG_PLATFORM_MSTAR */ + break; + case ANDROID_WIFI_CMD_SCAN_PASSIVE: + /* rtw_set_scan_mode((_adapter *)rtw_netdev_priv(net), SCAN_PASSIVE); */ + break; + + case ANDROID_WIFI_CMD_RSSI: + bytes_written = rtw_android_get_rssi(net, command, priv_cmd.total_len); + break; + case ANDROID_WIFI_CMD_LINKSPEED: + bytes_written = rtw_android_get_link_speed(net, command, priv_cmd.total_len); + break; + + case ANDROID_WIFI_CMD_MACADDR: + bytes_written = rtw_android_get_macaddr(net, command, priv_cmd.total_len); + break; + + case ANDROID_WIFI_CMD_BLOCK_SCAN: + bytes_written = rtw_android_set_block_scan(net, command, priv_cmd.total_len); + break; + + case ANDROID_WIFI_CMD_BLOCK: + bytes_written = rtw_android_set_block(net, command, priv_cmd.total_len); + break; + + case ANDROID_WIFI_CMD_RXFILTER_START: + /* bytes_written = net_os_set_packet_filter(net, 1); */ + break; + case ANDROID_WIFI_CMD_RXFILTER_STOP: + /* bytes_written = net_os_set_packet_filter(net, 0); */ + break; + case ANDROID_WIFI_CMD_RXFILTER_ADD: + /* int filter_num = *(command + strlen(CMD_RXFILTER_ADD) + 1) - '0'; */ + /* bytes_written = net_os_rxfilter_add_remove(net, TRUE, filter_num); */ + break; + case ANDROID_WIFI_CMD_RXFILTER_REMOVE: + /* int filter_num = *(command + strlen(CMD_RXFILTER_REMOVE) + 1) - '0'; */ + /* bytes_written = net_os_rxfilter_add_remove(net, FALSE, filter_num); */ + break; + + case ANDROID_WIFI_CMD_BTCOEXSCAN_START: + /* TBD: BTCOEXSCAN-START */ + break; + case ANDROID_WIFI_CMD_BTCOEXSCAN_STOP: + /* TBD: BTCOEXSCAN-STOP */ + break; + case ANDROID_WIFI_CMD_BTCOEXMODE: +#if 0 + uint mode = *(command + strlen(CMD_BTCOEXMODE) + 1) - '0'; + if (mode == 1) + net_os_set_packet_filter(net, 0); /* DHCP starts */ + else + net_os_set_packet_filter(net, 1); /* DHCP ends */ +#ifdef WL_CFG80211 + bytes_written = wl_cfg80211_set_btcoex_dhcp(net, command); +#endif +#endif + break; + + case ANDROID_WIFI_CMD_SETSUSPENDMODE: + break; + + case ANDROID_WIFI_CMD_SETSUSPENDOPT: + /* bytes_written = wl_android_set_suspendopt(net, command, priv_cmd.total_len); */ + break; + + case ANDROID_WIFI_CMD_SETBAND: + bytes_written = rtw_android_setband(net, command, priv_cmd.total_len); + break; + + case ANDROID_WIFI_CMD_GETBAND: + bytes_written = rtw_android_getband(net, command, priv_cmd.total_len); + break; + + case ANDROID_WIFI_CMD_COUNTRY: + bytes_written = rtw_android_set_country(net, command, priv_cmd.total_len); + break; + +#ifdef CONFIG_PNO_SUPPORT + case ANDROID_WIFI_CMD_PNOSSIDCLR_SET: + /* bytes_written = dhd_dev_pno_reset(net); */ + break; + case ANDROID_WIFI_CMD_PNOSETUP_SET: + bytes_written = rtw_android_pno_setup(net, command, priv_cmd.total_len); + break; + case ANDROID_WIFI_CMD_PNOENABLE_SET: + cmdlen = strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_PNOENABLE_SET]); + pno_enable = *(command + cmdlen + 1) - '0'; + bytes_written = rtw_android_pno_enable(net, pno_enable); + break; +#endif + + case ANDROID_WIFI_CMD_P2P_DEV_ADDR: + bytes_written = rtw_android_get_p2p_dev_addr(net, command, priv_cmd.total_len); + break; + case ANDROID_WIFI_CMD_P2P_SET_NOA: + /* int skip = strlen(CMD_P2P_SET_NOA) + 1; */ + /* bytes_written = wl_cfg80211_set_p2p_noa(net, command + skip, priv_cmd.total_len - skip); */ + break; + case ANDROID_WIFI_CMD_P2P_GET_NOA: + /* bytes_written = wl_cfg80211_get_p2p_noa(net, command, priv_cmd.total_len); */ + break; + case ANDROID_WIFI_CMD_P2P_SET_PS: + /* int skip = strlen(CMD_P2P_SET_PS) + 1; */ + /* bytes_written = wl_cfg80211_set_p2p_ps(net, command + skip, priv_cmd.total_len - skip); */ + break; + +#ifdef CONFIG_IOCTL_CFG80211 + #ifdef CONFIG_AP_MODE + case ANDROID_WIFI_CMD_SET_AP_WPS_P2P_IE: { + int skip = strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_SET_AP_WPS_P2P_IE]) + 3; + bytes_written = rtw_cfg80211_set_mgnt_wpsp2pie(net, command + skip, priv_cmd.total_len - skip, *(command + skip - 2) - '0'); + break; + } + #endif +#endif /* CONFIG_IOCTL_CFG80211 */ + +#ifdef CONFIG_WFD + + case ANDROID_WIFI_CMD_MIRACAST: + bytes_written = rtw_android_set_miracast_mode(net, command, priv_cmd.total_len); + break; + + case ANDROID_WIFI_CMD_WFD_ENABLE: { + /* Commented by Albert 2012/07/24 */ + /* We can enable the WFD function by using the following command: */ + /* wpa_cli driver wfd-enable */ + + if (padapter->wdinfo.driver_interface == DRIVER_CFG80211) + rtw_wfd_enable(padapter, 1); + break; + } + + case ANDROID_WIFI_CMD_WFD_DISABLE: { + /* Commented by Albert 2012/07/24 */ + /* We can disable the WFD function by using the following command: */ + /* wpa_cli driver wfd-disable */ + + if (padapter->wdinfo.driver_interface == DRIVER_CFG80211) + rtw_wfd_enable(padapter, 0); + break; + } + case ANDROID_WIFI_CMD_WFD_SET_TCPPORT: { + /* Commented by Albert 2012/07/24 */ + /* We can set the tcp port number by using the following command: */ + /* wpa_cli driver wfd-set-tcpport = 554 */ + + if (padapter->wdinfo.driver_interface == DRIVER_CFG80211) + rtw_wfd_set_ctrl_port(padapter, (u16)get_int_from_command(command)); + break; + } + case ANDROID_WIFI_CMD_WFD_SET_MAX_TPUT: { + break; + } + case ANDROID_WIFI_CMD_WFD_SET_DEVTYPE: { + /* Commented by Albert 2012/08/28 */ + /* Specify the WFD device type ( WFD source/primary sink ) */ + + pwfd_info = &padapter->wfd_info; + if (padapter->wdinfo.driver_interface == DRIVER_CFG80211) { + pwfd_info->wfd_device_type = (u8) get_int_from_command(command); + pwfd_info->wfd_device_type &= WFD_DEVINFO_DUAL; + } + break; + } +#endif + case ANDROID_WIFI_CMD_CHANGE_DTIM: { +#ifdef CONFIG_LPS + u8 dtim; + u8 *ptr = (u8 *) command; + + ptr += 9;/* string command length of "SET_DTIM"; */ + + dtim = rtw_atoi(ptr); + + RTW_INFO("DTIM=%d\n", dtim); + + rtw_lps_change_dtim_cmd(padapter, dtim); +#endif + } + break; + +#if CONFIG_RTW_MACADDR_ACL + case ANDROID_WIFI_CMD_HOSTAPD_SET_MACADDR_ACL: { + rtw_set_macaddr_acl(padapter, RTW_ACL_PERIOD_BSS, get_int_from_command(command)); + break; + } + case ANDROID_WIFI_CMD_HOSTAPD_ACL_ADD_STA: { + u8 addr[ETH_ALEN] = {0x00}; + macstr2num(addr, command + strlen("HOSTAPD_ACL_ADD_STA") + 3); /* 3 is space bar + "=" + space bar these 3 chars */ + rtw_acl_add_sta(padapter, RTW_ACL_PERIOD_BSS, addr); + break; + } + case ANDROID_WIFI_CMD_HOSTAPD_ACL_REMOVE_STA: { + u8 addr[ETH_ALEN] = {0x00}; + macstr2num(addr, command + strlen("HOSTAPD_ACL_REMOVE_STA") + 3); /* 3 is space bar + "=" + space bar these 3 chars */ + rtw_acl_remove_sta(padapter, RTW_ACL_PERIOD_BSS, addr); + break; + } +#endif /* CONFIG_RTW_MACADDR_ACL */ +#if defined(CONFIG_GTK_OL) && (LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0)) + case ANDROID_WIFI_CMD_GTK_REKEY_OFFLOAD: + rtw_gtk_offload(net, (u8 *)command); + break; +#endif /* CONFIG_GTK_OL */ + case ANDROID_WIFI_CMD_P2P_DISABLE: { +#ifdef CONFIG_P2P + rtw_p2p_enable(padapter, P2P_ROLE_DISABLE); +#endif /* CONFIG_P2P */ + break; + } + +#ifdef CONFIG_RTW_MESH_AEK + case ANDROID_WIFI_CMD_SET_AEK: + bytes_written = rtw_android_set_aek(net, command, priv_cmd.total_len); + break; +#endif + + case ANDROID_WIFI_CMD_EXT_AUTH_STATUS: { + rtw_set_external_auth_status(padapter, + command + strlen("EXT_AUTH_STATUS "), + priv_cmd.total_len - strlen("EXT_AUTH_STATUS ")); + break; + } + case ANDROID_WIFI_CMD_DRIVERVERSION: { + bytes_written = strlen(DRIVERVERSION); + snprintf(command, bytes_written + 1, DRIVERVERSION); + break; + } + default: + RTW_INFO("Unknown PRIVATE command %s - ignored\n", command); + snprintf(command, 3, "OK"); + bytes_written = strlen("OK"); + } + +response: + if (bytes_written >= 0) { + if ((bytes_written == 0) && (priv_cmd.total_len > 0)) + command[0] = '\0'; + if (bytes_written >= priv_cmd.total_len) { + RTW_INFO("%s: bytes_written = %d\n", __FUNCTION__, bytes_written); + bytes_written = priv_cmd.total_len; + } else + bytes_written++; + priv_cmd.used_len = bytes_written; + if (copy_to_user((void *)priv_cmd.buf, command, bytes_written)) { + RTW_INFO("%s: failed to copy data to user buffer\n", __FUNCTION__); + ret = -EFAULT; + } + } else + ret = bytes_written; + +exit: + rtw_unlock_suspend(); + if (command) + rtw_mfree(command, priv_cmd.total_len + 1); + + return ret; +} + + +/** + * Functions for Android WiFi card detection + */ +#if defined(RTW_ENABLE_WIFI_CONTROL_FUNC) + +static int g_wifidev_registered = 0; +static struct semaphore wifi_control_sem; +static struct wifi_platform_data *wifi_control_data = NULL; +static struct resource *wifi_irqres = NULL; + +static int wifi_add_dev(void); +static void wifi_del_dev(void); + +int rtw_android_wifictrl_func_add(void) +{ + int ret = 0; + sema_init(&wifi_control_sem, 0); + + ret = wifi_add_dev(); + if (ret) { + RTW_INFO("%s: platform_driver_register failed\n", __FUNCTION__); + return ret; + } + g_wifidev_registered = 1; + + /* Waiting callback after platform_driver_register is done or exit with error */ + if (down_timeout(&wifi_control_sem, msecs_to_jiffies(1000)) != 0) { + ret = -EINVAL; + RTW_INFO("%s: platform_driver_register timeout\n", __FUNCTION__); + } + + return ret; +} + +void rtw_android_wifictrl_func_del(void) +{ + if (g_wifidev_registered) { + wifi_del_dev(); + g_wifidev_registered = 0; + } +} + +void *wl_android_prealloc(int section, unsigned long size) +{ + void *alloc_ptr = NULL; + if (wifi_control_data && wifi_control_data->mem_prealloc) { + alloc_ptr = wifi_control_data->mem_prealloc(section, size); + if (alloc_ptr) { + RTW_INFO("success alloc section %d\n", section); + if (size != 0L) + memset(alloc_ptr, 0, size); + return alloc_ptr; + } + } + + RTW_INFO("can't alloc section %d\n", section); + return NULL; +} + +int wifi_get_irq_number(unsigned long *irq_flags_ptr) +{ + if (wifi_irqres) { + *irq_flags_ptr = wifi_irqres->flags & IRQF_TRIGGER_MASK; + return (int)wifi_irqres->start; + } +#ifdef CUSTOM_OOB_GPIO_NUM + return CUSTOM_OOB_GPIO_NUM; +#else + return -1; +#endif +} + +int wifi_set_power(int on, unsigned long msec) +{ + RTW_INFO("%s = %d\n", __FUNCTION__, on); + if (wifi_control_data && wifi_control_data->set_power) + wifi_control_data->set_power(on); + if (msec) + msleep(msec); + return 0; +} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) +int wifi_get_mac_addr(unsigned char *buf) +{ + RTW_INFO("%s\n", __FUNCTION__); + if (!buf) + return -EINVAL; + if (wifi_control_data && wifi_control_data->get_mac_addr) + return wifi_control_data->get_mac_addr(buf); + return -EOPNOTSUPP; +} +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) */ + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39)) || defined(COMPAT_KERNEL_RELEASE) +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)) +void *wifi_get_country_code(char *ccode, u32 flags) +#else /* Linux kernel < 3.18 */ +void *wifi_get_country_code(char *ccode) +#endif /* Linux kernel < 3.18 */ +{ + RTW_INFO("%s\n", __FUNCTION__); + if (!ccode) + return NULL; + if (wifi_control_data && wifi_control_data->get_country_code) +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)) + return wifi_control_data->get_country_code(ccode, flags); +#else /* Linux kernel < 3.18 */ + return wifi_control_data->get_country_code(ccode); +#endif /* Linux kernel < 3.18 */ + return NULL; +} +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39)) */ + +static int wifi_set_carddetect(int on) +{ + RTW_INFO("%s = %d\n", __FUNCTION__, on); + if (wifi_control_data && wifi_control_data->set_carddetect) + wifi_control_data->set_carddetect(on); + return 0; +} + +static int wifi_probe(struct platform_device *pdev) +{ + struct wifi_platform_data *wifi_ctrl = + (struct wifi_platform_data *)(pdev->dev.platform_data); + int wifi_wake_gpio = 0; + + RTW_INFO("## %s\n", __FUNCTION__); + wifi_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "bcmdhd_wlan_irq"); + + if (wifi_irqres == NULL) + wifi_irqres = platform_get_resource_byname(pdev, + IORESOURCE_IRQ, "bcm4329_wlan_irq"); + else + wifi_wake_gpio = wifi_irqres->start; + +#ifdef CONFIG_GPIO_WAKEUP + RTW_INFO("%s: gpio:%d wifi_wake_gpio:%d\n", __func__, + (int)wifi_irqres->start, wifi_wake_gpio); + + if (wifi_wake_gpio > 0) { +#ifdef CONFIG_PLATFORM_INTEL_BYT + wifi_configure_gpio(); +#else /* CONFIG_PLATFORM_INTEL_BYT */ + gpio_request(wifi_wake_gpio, "oob_irq"); + gpio_direction_input(wifi_wake_gpio); + oob_irq = gpio_to_irq(wifi_wake_gpio); +#endif /* CONFIG_PLATFORM_INTEL_BYT */ + RTW_INFO("%s oob_irq:%d\n", __func__, oob_irq); + } else if (wifi_irqres) { + oob_irq = wifi_irqres->start; + RTW_INFO("%s oob_irq:%d\n", __func__, oob_irq); + } +#endif + wifi_control_data = wifi_ctrl; + + wifi_set_power(1, 0); /* Power On */ + wifi_set_carddetect(1); /* CardDetect (0->1) */ + + up(&wifi_control_sem); + return 0; +} + +#ifdef RTW_SUPPORT_PLATFORM_SHUTDOWN +extern PADAPTER g_test_adapter; + +static void shutdown_card(void) +{ + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(g_test_adapter); + u32 addr; + u8 tmp8, cnt = 0; + + if (NULL == g_test_adapter) { + RTW_INFO("%s: padapter==NULL\n", __FUNCTION__); + return; + } + +#ifdef CONFIG_FWLPS_IN_IPS + LeaveAllPowerSaveMode(g_test_adapter); +#endif /* CONFIG_FWLPS_IN_IPS */ + +#ifdef CONFIG_WOWLAN +#ifdef CONFIG_GPIO_WAKEUP + /*default wake up pin change to BT*/ + RTW_INFO("%s:default wake up pin change to BT\n", __FUNCTION__); + rtw_hal_switch_gpio_wl_ctrl(g_test_adapter, pwrpriv->wowlan_gpio_index, _FALSE); +#endif /* CONFIG_GPIO_WAKEUP */ +#endif /* CONFIG_WOWLAN */ + + /* Leave SDIO HCI Suspend */ + addr = 0x10250086; + rtw_write8(g_test_adapter, addr, 0); + do { + tmp8 = rtw_read8(g_test_adapter, addr); + cnt++; + RTW_INFO(FUNC_ADPT_FMT ": polling SDIO_HSUS_CTRL(0x%x)=0x%x, cnt=%d\n", + FUNC_ADPT_ARG(g_test_adapter), addr, tmp8, cnt); + + if (tmp8 & BIT(1)) + break; + + if (cnt >= 100) { + RTW_INFO(FUNC_ADPT_FMT ": polling 0x%x[1]==1 FAIL!!\n", + FUNC_ADPT_ARG(g_test_adapter), addr); + break; + } + + rtw_mdelay_os(10); + } while (1); + + /* unlock register I/O */ + rtw_write8(g_test_adapter, 0x1C, 0); + + /* enable power down function */ + /* 0x04[4] = 1 */ + /* 0x05[7] = 1 */ + addr = 0x04; + tmp8 = rtw_read8(g_test_adapter, addr); + tmp8 |= BIT(4); + rtw_write8(g_test_adapter, addr, tmp8); + RTW_INFO(FUNC_ADPT_FMT ": read after write 0x%x=0x%x\n", + FUNC_ADPT_ARG(g_test_adapter), addr, rtw_read8(g_test_adapter, addr)); + + addr = 0x05; + tmp8 = rtw_read8(g_test_adapter, addr); + tmp8 |= BIT(7); + rtw_write8(g_test_adapter, addr, tmp8); + RTW_INFO(FUNC_ADPT_FMT ": read after write 0x%x=0x%x\n", + FUNC_ADPT_ARG(g_test_adapter), addr, rtw_read8(g_test_adapter, addr)); + + /* lock register page0 0x0~0xB read/write */ + rtw_write8(g_test_adapter, 0x1C, 0x0E); + + rtw_set_surprise_removed(g_test_adapter); + RTW_INFO(FUNC_ADPT_FMT ": bSurpriseRemoved=%s\n", + FUNC_ADPT_ARG(g_test_adapter), rtw_is_surprise_removed(g_test_adapter) ? "True" : "False"); +} +#endif /* RTW_SUPPORT_PLATFORM_SHUTDOWN */ + +static int wifi_remove(struct platform_device *pdev) +{ + struct wifi_platform_data *wifi_ctrl = + (struct wifi_platform_data *)(pdev->dev.platform_data); + + RTW_INFO("## %s\n", __FUNCTION__); + wifi_control_data = wifi_ctrl; + + wifi_set_power(0, 0); /* Power Off */ + wifi_set_carddetect(0); /* CardDetect (1->0) */ + + up(&wifi_control_sem); + return 0; +} + +#ifdef RTW_SUPPORT_PLATFORM_SHUTDOWN +static void wifi_shutdown(struct platform_device *pdev) +{ + struct wifi_platform_data *wifi_ctrl = + (struct wifi_platform_data *)(pdev->dev.platform_data); + + + RTW_INFO("## %s\n", __FUNCTION__); + + wifi_control_data = wifi_ctrl; + + shutdown_card(); + wifi_set_power(0, 0); /* Power Off */ + wifi_set_carddetect(0); /* CardDetect (1->0) */ +} +#endif /* RTW_SUPPORT_PLATFORM_SHUTDOWN */ + +static int wifi_suspend(struct platform_device *pdev, pm_message_t state) +{ + RTW_INFO("##> %s\n", __FUNCTION__); +#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 39)) && defined(OOB_INTR_ONLY) + bcmsdh_oob_intr_set(0); +#endif + return 0; +} + +static int wifi_resume(struct platform_device *pdev) +{ + RTW_INFO("##> %s\n", __FUNCTION__); +#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 39)) && defined(OOB_INTR_ONLY) + if (dhd_os_check_if_up(bcmsdh_get_drvdata())) + bcmsdh_oob_intr_set(1); +#endif + return 0; +} + +/* temporarily use these two */ +static struct platform_driver wifi_device = { + .probe = wifi_probe, + .remove = wifi_remove, + .suspend = wifi_suspend, + .resume = wifi_resume, +#ifdef RTW_SUPPORT_PLATFORM_SHUTDOWN + .shutdown = wifi_shutdown, +#endif /* RTW_SUPPORT_PLATFORM_SHUTDOWN */ + .driver = { + .name = "bcmdhd_wlan", + } +}; + +static struct platform_driver wifi_device_legacy = { + .probe = wifi_probe, + .remove = wifi_remove, + .suspend = wifi_suspend, + .resume = wifi_resume, + .driver = { + .name = "bcm4329_wlan", + } +}; + +static int wifi_add_dev(void) +{ + RTW_INFO("## Calling platform_driver_register\n"); + platform_driver_register(&wifi_device); + platform_driver_register(&wifi_device_legacy); + return 0; +} + +static void wifi_del_dev(void) +{ + RTW_INFO("## Unregister platform_driver_register\n"); + platform_driver_unregister(&wifi_device); + platform_driver_unregister(&wifi_device_legacy); +} +#endif /* defined(RTW_ENABLE_WIFI_CONTROL_FUNC) */ + +#ifdef CONFIG_GPIO_WAKEUP +#ifdef CONFIG_PLATFORM_INTEL_BYT +int wifi_configure_gpio(void) +{ + if (gpio_request(oob_gpio, "oob_irq")) { + RTW_INFO("## %s Cannot request GPIO\n", __FUNCTION__); + return -1; + } + gpio_export(oob_gpio, 0); + if (gpio_direction_input(oob_gpio)) { + RTW_INFO("## %s Cannot set GPIO direction input\n", __FUNCTION__); + return -1; + } + oob_irq = gpio_to_irq(oob_gpio); + if (oob_irq < 0) { + RTW_INFO("## %s Cannot convert GPIO to IRQ\n", __FUNCTION__); + return -1; + } + + RTW_INFO("## %s OOB_IRQ=%d\n", __FUNCTION__, oob_irq); + + return 0; +} +#endif /* CONFIG_PLATFORM_INTEL_BYT */ +void wifi_free_gpio(unsigned int gpio) +{ +#ifdef CONFIG_PLATFORM_INTEL_BYT + if (gpio) + gpio_free(gpio); +#endif /* CONFIG_PLATFORM_INTEL_BYT */ +} +#endif /* CONFIG_GPIO_WAKEUP */ diff --git a/os_dep/linux/rtw_cfgvendor.c b/os_dep/linux/rtw_cfgvendor.c new file mode 100644 index 0000000..53153ba --- /dev/null +++ b/os_dep/linux/rtw_cfgvendor.c @@ -0,0 +1,2172 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ + +#include + +#ifdef CONFIG_IOCTL_CFG80211 + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)) || defined(RTW_VENDOR_EXT_SUPPORT) + +/* +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +*/ + +#include + +#ifndef MIN +#define MIN(x,y) (((x) < (y)) ? (x) : (y)) +#endif + +#ifdef DBG_MEM_ALLOC +extern bool match_mstat_sniff_rules(const enum mstat_f flags, const size_t size); +struct sk_buff *dbg_rtw_cfg80211_vendor_event_alloc(struct wiphy *wiphy, struct wireless_dev *wdev, int len, int event_id, gfp_t gfp + , const enum mstat_f flags, const char *func, const int line) +{ + struct sk_buff *skb; + unsigned int truesize = 0; + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0)) + skb = cfg80211_vendor_event_alloc(wiphy, len, event_id, gfp); +#else + skb = cfg80211_vendor_event_alloc(wiphy, wdev, len, event_id, gfp); +#endif + + if (skb) + truesize = skb->truesize; + + if (!skb || truesize < len || match_mstat_sniff_rules(flags, truesize)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%d), skb:%p, truesize=%u\n", func, line, __FUNCTION__, len, skb, truesize); + + rtw_mstat_update( + flags + , skb ? MSTAT_ALLOC_SUCCESS : MSTAT_ALLOC_FAIL + , truesize + ); + + return skb; +} + +void dbg_rtw_cfg80211_vendor_event(struct sk_buff *skb, gfp_t gfp + , const enum mstat_f flags, const char *func, const int line) +{ + unsigned int truesize = skb->truesize; + + if (match_mstat_sniff_rules(flags, truesize)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s, truesize=%u\n", func, line, __FUNCTION__, truesize); + + cfg80211_vendor_event(skb, gfp); + + rtw_mstat_update( + flags + , MSTAT_FREE + , truesize + ); +} + +struct sk_buff *dbg_rtw_cfg80211_vendor_cmd_alloc_reply_skb(struct wiphy *wiphy, int len + , const enum mstat_f flags, const char *func, const int line) +{ + struct sk_buff *skb; + unsigned int truesize = 0; + + skb = cfg80211_vendor_cmd_alloc_reply_skb(wiphy, len); + + if (skb) + truesize = skb->truesize; + + if (!skb || truesize < len || match_mstat_sniff_rules(flags, truesize)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%d), skb:%p, truesize=%u\n", func, line, __FUNCTION__, len, skb, truesize); + + rtw_mstat_update( + flags + , skb ? MSTAT_ALLOC_SUCCESS : MSTAT_ALLOC_FAIL + , truesize + ); + + return skb; +} + +int dbg_rtw_cfg80211_vendor_cmd_reply(struct sk_buff *skb + , const enum mstat_f flags, const char *func, const int line) +{ + unsigned int truesize = skb->truesize; + int ret; + + if (match_mstat_sniff_rules(flags, truesize)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s, truesize=%u\n", func, line, __FUNCTION__, truesize); + + ret = cfg80211_vendor_cmd_reply(skb); + + rtw_mstat_update( + flags + , MSTAT_FREE + , truesize + ); + + return ret; +} + +#define rtw_cfg80211_vendor_event_alloc(wiphy, wdev, len, event_id, gfp) \ + dbg_rtw_cfg80211_vendor_event_alloc(wiphy, wdev, len, event_id, gfp, MSTAT_FUNC_CFG_VENDOR | MSTAT_TYPE_SKB, __FUNCTION__, __LINE__) + +#define rtw_cfg80211_vendor_event(skb, gfp) \ + dbg_rtw_cfg80211_vendor_event(skb, gfp, MSTAT_FUNC_CFG_VENDOR | MSTAT_TYPE_SKB, __FUNCTION__, __LINE__) + +#define rtw_cfg80211_vendor_cmd_alloc_reply_skb(wiphy, len) \ + dbg_rtw_cfg80211_vendor_cmd_alloc_reply_skb(wiphy, len, MSTAT_FUNC_CFG_VENDOR | MSTAT_TYPE_SKB, __FUNCTION__, __LINE__) + +#define rtw_cfg80211_vendor_cmd_reply(skb) \ + dbg_rtw_cfg80211_vendor_cmd_reply(skb, MSTAT_FUNC_CFG_VENDOR | MSTAT_TYPE_SKB, __FUNCTION__, __LINE__) +#else + +struct sk_buff *rtw_cfg80211_vendor_event_alloc( + struct wiphy *wiphy, struct wireless_dev *wdev, int len, int event_id, gfp_t gfp) +{ + struct sk_buff *skb; + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0)) + skb = cfg80211_vendor_event_alloc(wiphy, len, event_id, gfp); +#else + skb = cfg80211_vendor_event_alloc(wiphy, wdev, len, event_id, gfp); +#endif + return skb; +} + +#define rtw_cfg80211_vendor_event(skb, gfp) \ + cfg80211_vendor_event(skb, gfp) + +#define rtw_cfg80211_vendor_cmd_alloc_reply_skb(wiphy, len) \ + cfg80211_vendor_cmd_alloc_reply_skb(wiphy, len) + +#define rtw_cfg80211_vendor_cmd_reply(skb) \ + cfg80211_vendor_cmd_reply(skb) +#endif /* DBG_MEM_ALLOC */ + +/* + * This API is to be used for asynchronous vendor events. This + * shouldn't be used in response to a vendor command from its + * do_it handler context (instead rtw_cfgvendor_send_cmd_reply should + * be used). + */ +int rtw_cfgvendor_send_async_event(struct wiphy *wiphy, + struct net_device *dev, int event_id, const void *data, int len) +{ + gfp_t kflags; + struct sk_buff *skb; + + kflags = in_atomic() ? GFP_ATOMIC : GFP_KERNEL; + + /* Alloc the SKB for vendor_event */ + skb = rtw_cfg80211_vendor_event_alloc(wiphy, ndev_to_wdev(dev), len, event_id, kflags); + if (!skb) { + RTW_ERR(FUNC_NDEV_FMT" skb alloc failed", FUNC_NDEV_ARG(dev)); + return -ENOMEM; + } + + /* Push the data to the skb */ + nla_put_nohdr(skb, len, data); + + rtw_cfg80211_vendor_event(skb, kflags); + + return 0; +} + +static int rtw_cfgvendor_send_cmd_reply(struct wiphy *wiphy, + struct net_device *dev, const void *data, int len) +{ + struct sk_buff *skb; + + /* Alloc the SKB for vendor_event */ + skb = rtw_cfg80211_vendor_cmd_alloc_reply_skb(wiphy, len); + if (unlikely(!skb)) { + RTW_ERR(FUNC_NDEV_FMT" skb alloc failed", FUNC_NDEV_ARG(dev)); + return -ENOMEM; + } + + /* Push the data to the skb */ + nla_put_nohdr(skb, len, data); + + return rtw_cfg80211_vendor_cmd_reply(skb); +} + +/* Feature enums */ +#define WIFI_FEATURE_INFRA 0x0001 // Basic infrastructure mode +#define WIFI_FEATURE_INFRA_5G 0x0002 // Support for 5 GHz Band +#define WIFI_FEATURE_HOTSPOT 0x0004 // Support for GAS/ANQP +#define WIFI_FEATURE_P2P 0x0008 // Wifi-Direct +#define WIFI_FEATURE_SOFT_AP 0x0010 // Soft AP +#define WIFI_FEATURE_GSCAN 0x0020 // Google-Scan APIs +#define WIFI_FEATURE_NAN 0x0040 // Neighbor Awareness Networking +#define WIFI_FEATURE_D2D_RTT 0x0080 // Device-to-device RTT +#define WIFI_FEATURE_D2AP_RTT 0x0100 // Device-to-AP RTT +#define WIFI_FEATURE_BATCH_SCAN 0x0200 // Batched Scan (legacy) +#define WIFI_FEATURE_PNO 0x0400 // Preferred network offload +#define WIFI_FEATURE_ADDITIONAL_STA 0x0800 // Support for two STAs +#define WIFI_FEATURE_TDLS 0x1000 // Tunnel directed link setup +#define WIFI_FEATURE_TDLS_OFFCHANNEL 0x2000 // Support for TDLS off channel +#define WIFI_FEATURE_EPR 0x4000 // Enhanced power reporting +#define WIFI_FEATURE_AP_STA 0x8000 // Support for AP STA Concurrency +#define WIFI_FEATURE_LINK_LAYER_STATS 0x10000 // Link layer stats collection +#define WIFI_FEATURE_LOGGER 0x20000 // WiFi Logger +#define WIFI_FEATURE_HAL_EPNO 0x40000 // WiFi PNO enhanced +#define WIFI_FEATURE_RSSI_MONITOR 0x80000 // RSSI Monitor +#define WIFI_FEATURE_MKEEP_ALIVE 0x100000 // WiFi mkeep_alive +#define WIFI_FEATURE_CONFIG_NDO 0x200000 // ND offload configure +#define WIFI_FEATURE_TX_TRANSMIT_POWER 0x400000 // Capture Tx transmit power levels +#define WIFI_FEATURE_CONTROL_ROAMING 0x800000 // Enable/Disable firmware roaming +#define WIFI_FEATURE_IE_WHITELIST 0x1000000 // Support Probe IE white listing +#define WIFI_FEATURE_SCAN_RAND 0x2000000 // Support MAC & Probe Sequence Number randomization +#define WIFI_FEATURE_SET_TX_POWER_LIMIT 0x4000000 // Support Tx Power Limit setting +#define WIFI_FEATURE_USE_BODY_HEAD_SAR 0x8000000 // Support Using Body/Head Proximity for SAR +#define WIFI_FEATURE_SET_LATENCY_MODE 0x40000000 // Support Latency mode setting +#define WIFI_FEATURE_P2P_RAND_MAC 0x80000000 // Support Support P2P MAC randomization +// Add more features here + +#define MAX_FEATURE_SET_CONCURRRENT_GROUPS 3 + +#include +int rtw_dev_get_feature_set(struct net_device *dev) +{ + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + HAL_DATA_TYPE *HalData = GET_HAL_DATA(adapter); + int feature_set = 0; + + feature_set |= WIFI_FEATURE_INFRA; + +#if CONFIG_IEEE80211_BAND_5GHZ + if (is_supported_5g(adapter_to_regsty(adapter)->wireless_mode)) + feature_set |= WIFI_FEATURE_INFRA_5G; +#endif + + feature_set |= WIFI_FEATURE_P2P; + feature_set |= WIFI_FEATURE_SOFT_AP; + + feature_set |= WIFI_FEATURE_ADDITIONAL_STA; +#ifdef CONFIG_RTW_CFGVENDOR_LLSTATS + feature_set |= WIFI_FEATURE_LINK_LAYER_STATS; +#endif /* CONFIG_RTW_CFGVENDOR_LLSTATS */ + +#ifdef CONFIG_RTW_CFGVENDOR_RSSIMONITOR + feature_set |= WIFI_FEATURE_RSSI_MONITOR; +#endif + +#ifdef CONFIG_RTW_CFGVENDOR_WIFI_LOGGER + feature_set |= WIFI_FEATURE_LOGGER; +#endif + +#ifdef CONFIG_RTW_WIFI_HAL + feature_set |= WIFI_FEATURE_CONFIG_NDO; +#if defined(CONFIG_RTW_CFGVENDOR_RANDOM_MAC_OUI) || defined(CONFIG_RTW_SCAN_RAND) + feature_set |= WIFI_FEATURE_SCAN_RAND; +#endif +#endif + + return feature_set; +} + +int *rtw_dev_get_feature_set_matrix(struct net_device *dev, int *num) +{ + int feature_set_full, mem_needed; + int *ret; + + *num = 0; + mem_needed = sizeof(int) * MAX_FEATURE_SET_CONCURRRENT_GROUPS; + ret = (int *)rtw_malloc(mem_needed); + + if (!ret) { + RTW_ERR(FUNC_NDEV_FMT" failed to allocate %d bytes\n" + , FUNC_NDEV_ARG(dev), mem_needed); + return ret; + } + + feature_set_full = rtw_dev_get_feature_set(dev); + + ret[0] = (feature_set_full & WIFI_FEATURE_INFRA) | + (feature_set_full & WIFI_FEATURE_INFRA_5G) | + (feature_set_full & WIFI_FEATURE_NAN) | + (feature_set_full & WIFI_FEATURE_D2D_RTT) | + (feature_set_full & WIFI_FEATURE_D2AP_RTT) | + (feature_set_full & WIFI_FEATURE_PNO) | + (feature_set_full & WIFI_FEATURE_BATCH_SCAN) | + (feature_set_full & WIFI_FEATURE_GSCAN) | + (feature_set_full & WIFI_FEATURE_HOTSPOT) | + (feature_set_full & WIFI_FEATURE_ADDITIONAL_STA) | + (feature_set_full & WIFI_FEATURE_EPR); + + ret[1] = (feature_set_full & WIFI_FEATURE_INFRA) | + (feature_set_full & WIFI_FEATURE_INFRA_5G) | + /* Not yet verified NAN with P2P */ + /* (feature_set_full & WIFI_FEATURE_NAN) | */ + (feature_set_full & WIFI_FEATURE_P2P) | + (feature_set_full & WIFI_FEATURE_D2AP_RTT) | + (feature_set_full & WIFI_FEATURE_D2D_RTT) | + (feature_set_full & WIFI_FEATURE_EPR); + + ret[2] = (feature_set_full & WIFI_FEATURE_INFRA) | + (feature_set_full & WIFI_FEATURE_INFRA_5G) | + (feature_set_full & WIFI_FEATURE_NAN) | + (feature_set_full & WIFI_FEATURE_D2D_RTT) | + (feature_set_full & WIFI_FEATURE_D2AP_RTT) | + (feature_set_full & WIFI_FEATURE_TDLS) | + (feature_set_full & WIFI_FEATURE_TDLS_OFFCHANNEL) | + (feature_set_full & WIFI_FEATURE_EPR); + *num = MAX_FEATURE_SET_CONCURRRENT_GROUPS; + + return ret; +} + +static int rtw_cfgvendor_get_feature_set(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int err = 0; + int reply; + + reply = rtw_dev_get_feature_set(wdev_to_ndev(wdev)); + + err = rtw_cfgvendor_send_cmd_reply(wiphy, wdev_to_ndev(wdev), &reply, sizeof(int)); + + if (unlikely(err)) + RTW_ERR(FUNC_NDEV_FMT" Vendor Command reply failed ret:%d\n" + , FUNC_NDEV_ARG(wdev_to_ndev(wdev)), err); + + return err; +} + +static int rtw_cfgvendor_get_feature_set_matrix(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int err = 0; + struct sk_buff *skb; + int *reply; + int num, mem_needed, i; + + reply = rtw_dev_get_feature_set_matrix(wdev_to_ndev(wdev), &num); + + if (!reply) { + RTW_ERR(FUNC_NDEV_FMT" Could not get feature list matrix\n" + , FUNC_NDEV_ARG(wdev_to_ndev(wdev))); + err = -EINVAL; + return err; + } + + mem_needed = VENDOR_REPLY_OVERHEAD + (ATTRIBUTE_U32_LEN * num) + + ATTRIBUTE_U32_LEN; + + /* Alloc the SKB for vendor_event */ + skb = rtw_cfg80211_vendor_cmd_alloc_reply_skb(wiphy, mem_needed); + if (unlikely(!skb)) { + RTW_ERR(FUNC_NDEV_FMT" skb alloc failed", FUNC_NDEV_ARG(wdev_to_ndev(wdev))); + err = -ENOMEM; + goto exit; + } + + nla_put_u32(skb, ANDR_WIFI_ATTRIBUTE_NUM_FEATURE_SET, num); + for (i = 0; i < num; i++) + nla_put_u32(skb, ANDR_WIFI_ATTRIBUTE_FEATURE_SET, reply[i]); + + err = rtw_cfg80211_vendor_cmd_reply(skb); + + if (unlikely(err)) + RTW_ERR(FUNC_NDEV_FMT" Vendor Command reply failed ret:%d\n" + , FUNC_NDEV_ARG(wdev_to_ndev(wdev)), err); +exit: + rtw_mfree((u8 *)reply, sizeof(int) * num); + return err; +} + +#if defined(GSCAN_SUPPORT) && 0 +int rtw_cfgvendor_send_hotlist_event(struct wiphy *wiphy, + struct net_device *dev, void *data, int len, rtw_vendor_event_t event) +{ + u16 kflags; + const void *ptr; + struct sk_buff *skb; + int malloc_len, total, iter_cnt_to_send, cnt; + gscan_results_cache_t *cache = (gscan_results_cache_t *)data; + + total = len / sizeof(wifi_gscan_result_t); + while (total > 0) { + malloc_len = (total * sizeof(wifi_gscan_result_t)) + VENDOR_DATA_OVERHEAD; + if (malloc_len > NLMSG_DEFAULT_SIZE) + malloc_len = NLMSG_DEFAULT_SIZE; + iter_cnt_to_send = + (malloc_len - VENDOR_DATA_OVERHEAD) / sizeof(wifi_gscan_result_t); + total = total - iter_cnt_to_send; + + kflags = in_atomic() ? GFP_ATOMIC : GFP_KERNEL; + + /* Alloc the SKB for vendor_event */ + skb = rtw_cfg80211_vendor_event_alloc(wiphy, ndev_to_wdev(dev), malloc_len, event, kflags); + if (!skb) { + WL_ERR(("skb alloc failed")); + return -ENOMEM; + } + + while (cache && iter_cnt_to_send) { + ptr = (const void *) &cache->results[cache->tot_consumed]; + + if (iter_cnt_to_send < (cache->tot_count - cache->tot_consumed)) + cnt = iter_cnt_to_send; + else + cnt = (cache->tot_count - cache->tot_consumed); + + iter_cnt_to_send -= cnt; + cache->tot_consumed += cnt; + /* Push the data to the skb */ + nla_append(skb, cnt * sizeof(wifi_gscan_result_t), ptr); + if (cache->tot_consumed == cache->tot_count) + cache = cache->next; + + } + + rtw_cfg80211_vendor_event(skb, kflags); + } + + return 0; +} + + +static int rtw_cfgvendor_gscan_get_capabilities(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int err = 0; + struct bcm_cfg80211 *cfg = wiphy_priv(wiphy); + dhd_pno_gscan_capabilities_t *reply = NULL; + uint32 reply_len = 0; + + + reply = dhd_dev_pno_get_gscan(bcmcfg_to_prmry_ndev(cfg), + DHD_PNO_GET_CAPABILITIES, NULL, &reply_len); + if (!reply) { + WL_ERR(("Could not get capabilities\n")); + err = -EINVAL; + return err; + } + + err = rtw_cfgvendor_send_cmd_reply(wiphy, bcmcfg_to_prmry_ndev(cfg), + reply, reply_len); + + if (unlikely(err)) + WL_ERR(("Vendor Command reply failed ret:%d\n", err)); + + kfree(reply); + return err; +} + +static int rtw_cfgvendor_gscan_get_channel_list(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int err = 0, type, band; + struct bcm_cfg80211 *cfg = wiphy_priv(wiphy); + uint16 *reply = NULL; + uint32 reply_len = 0, num_channels, mem_needed; + struct sk_buff *skb; + + type = nla_type(data); + + if (type == GSCAN_ATTRIBUTE_BAND) + band = nla_get_u32(data); + else + return -1; + + reply = dhd_dev_pno_get_gscan(bcmcfg_to_prmry_ndev(cfg), + DHD_PNO_GET_CHANNEL_LIST, &band, &reply_len); + + if (!reply) { + WL_ERR(("Could not get channel list\n")); + err = -EINVAL; + return err; + } + num_channels = reply_len / sizeof(uint32); + mem_needed = reply_len + VENDOR_REPLY_OVERHEAD + (ATTRIBUTE_U32_LEN * 2); + + /* Alloc the SKB for vendor_event */ + skb = rtw_cfg80211_vendor_cmd_alloc_reply_skb(wiphy, mem_needed); + if (unlikely(!skb)) { + WL_ERR(("skb alloc failed")); + err = -ENOMEM; + goto exit; + } + + nla_put_u32(skb, GSCAN_ATTRIBUTE_NUM_CHANNELS, num_channels); + nla_put(skb, GSCAN_ATTRIBUTE_CHANNEL_LIST, reply_len, reply); + + err = rtw_cfg80211_vendor_cmd_reply(skb); + + if (unlikely(err)) + WL_ERR(("Vendor Command reply failed ret:%d\n", err)); +exit: + kfree(reply); + return err; +} + +static int rtw_cfgvendor_gscan_get_batch_results(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int err = 0; + struct bcm_cfg80211 *cfg = wiphy_priv(wiphy); + gscan_results_cache_t *results, *iter; + uint32 reply_len, complete = 0, num_results_iter; + int32 mem_needed; + wifi_gscan_result_t *ptr; + uint16 num_scan_ids, num_results; + struct sk_buff *skb; + struct nlattr *scan_hdr; + + dhd_dev_wait_batch_results_complete(bcmcfg_to_prmry_ndev(cfg)); + dhd_dev_pno_lock_access_batch_results(bcmcfg_to_prmry_ndev(cfg)); + results = dhd_dev_pno_get_gscan(bcmcfg_to_prmry_ndev(cfg), + DHD_PNO_GET_BATCH_RESULTS, NULL, &reply_len); + + if (!results) { + WL_ERR(("No results to send %d\n", err)); + err = rtw_cfgvendor_send_cmd_reply(wiphy, bcmcfg_to_prmry_ndev(cfg), + results, 0); + + if (unlikely(err)) + WL_ERR(("Vendor Command reply failed ret:%d\n", err)); + dhd_dev_pno_unlock_access_batch_results(bcmcfg_to_prmry_ndev(cfg)); + return err; + } + num_scan_ids = reply_len & 0xFFFF; + num_results = (reply_len & 0xFFFF0000) >> 16; + mem_needed = (num_results * sizeof(wifi_gscan_result_t)) + + (num_scan_ids * GSCAN_BATCH_RESULT_HDR_LEN) + + VENDOR_REPLY_OVERHEAD + SCAN_RESULTS_COMPLETE_FLAG_LEN; + + if (mem_needed > (int32)NLMSG_DEFAULT_SIZE) { + mem_needed = (int32)NLMSG_DEFAULT_SIZE; + complete = 0; + } else + complete = 1; + + WL_TRACE(("complete %d mem_needed %d max_mem %d\n", complete, mem_needed, + (int)NLMSG_DEFAULT_SIZE)); + /* Alloc the SKB for vendor_event */ + skb = rtw_cfg80211_vendor_cmd_alloc_reply_skb(wiphy, mem_needed); + if (unlikely(!skb)) { + WL_ERR(("skb alloc failed")); + dhd_dev_pno_unlock_access_batch_results(bcmcfg_to_prmry_ndev(cfg)); + return -ENOMEM; + } + iter = results; + + nla_put_u32(skb, GSCAN_ATTRIBUTE_SCAN_RESULTS_COMPLETE, complete); + + mem_needed = mem_needed - (SCAN_RESULTS_COMPLETE_FLAG_LEN + VENDOR_REPLY_OVERHEAD); + + while (iter && ((mem_needed - GSCAN_BATCH_RESULT_HDR_LEN) > 0)) { + scan_hdr = nla_nest_start(skb, GSCAN_ATTRIBUTE_SCAN_RESULTS); + nla_put_u32(skb, GSCAN_ATTRIBUTE_SCAN_ID, iter->scan_id); + nla_put_u8(skb, GSCAN_ATTRIBUTE_SCAN_FLAGS, iter->flag); + num_results_iter = + (mem_needed - GSCAN_BATCH_RESULT_HDR_LEN) / sizeof(wifi_gscan_result_t); + + if ((iter->tot_count - iter->tot_consumed) < num_results_iter) + num_results_iter = iter->tot_count - iter->tot_consumed; + + nla_put_u32(skb, GSCAN_ATTRIBUTE_NUM_OF_RESULTS, num_results_iter); + if (num_results_iter) { + ptr = &iter->results[iter->tot_consumed]; + iter->tot_consumed += num_results_iter; + nla_put(skb, GSCAN_ATTRIBUTE_SCAN_RESULTS, + num_results_iter * sizeof(wifi_gscan_result_t), ptr); + } + nla_nest_end(skb, scan_hdr); + mem_needed -= GSCAN_BATCH_RESULT_HDR_LEN + + (num_results_iter * sizeof(wifi_gscan_result_t)); + iter = iter->next; + } + + dhd_dev_gscan_batch_cache_cleanup(bcmcfg_to_prmry_ndev(cfg)); + dhd_dev_pno_unlock_access_batch_results(bcmcfg_to_prmry_ndev(cfg)); + + return rtw_cfg80211_vendor_cmd_reply(skb); +} + +static int rtw_cfgvendor_initiate_gscan(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int err = 0; + struct bcm_cfg80211 *cfg = wiphy_priv(wiphy); + int type, tmp = len; + int run = 0xFF; + int flush = 0; + const struct nlattr *iter; + + nla_for_each_attr(iter, data, len, tmp) { + type = nla_type(iter); + if (type == GSCAN_ATTRIBUTE_ENABLE_FEATURE) + run = nla_get_u32(iter); + else if (type == GSCAN_ATTRIBUTE_FLUSH_FEATURE) + flush = nla_get_u32(iter); + } + + if (run != 0xFF) { + err = dhd_dev_pno_run_gscan(bcmcfg_to_prmry_ndev(cfg), run, flush); + + if (unlikely(err)) + WL_ERR(("Could not run gscan:%d\n", err)); + return err; + } else + return -1; + + +} + +static int rtw_cfgvendor_enable_full_scan_result(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int err = 0; + struct bcm_cfg80211 *cfg = wiphy_priv(wiphy); + int type; + bool real_time = FALSE; + + type = nla_type(data); + + if (type == GSCAN_ATTRIBUTE_ENABLE_FULL_SCAN_RESULTS) { + real_time = nla_get_u32(data); + + err = dhd_dev_pno_enable_full_scan_result(bcmcfg_to_prmry_ndev(cfg), real_time); + + if (unlikely(err)) + WL_ERR(("Could not run gscan:%d\n", err)); + + } else + err = -1; + + return err; +} + +static int rtw_cfgvendor_set_scan_cfg(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int err = 0; + struct bcm_cfg80211 *cfg = wiphy_priv(wiphy); + gscan_scan_params_t *scan_param; + int j = 0; + int type, tmp, tmp1, tmp2, k = 0; + const struct nlattr *iter, *iter1, *iter2; + struct dhd_pno_gscan_channel_bucket *ch_bucket; + + scan_param = kzalloc(sizeof(gscan_scan_params_t), GFP_KERNEL); + if (!scan_param) { + WL_ERR(("Could not set GSCAN scan cfg, mem alloc failure\n")); + err = -EINVAL; + return err; + + } + + scan_param->scan_fr = PNO_SCAN_MIN_FW_SEC; + nla_for_each_attr(iter, data, len, tmp) { + type = nla_type(iter); + + if (j >= GSCAN_MAX_CH_BUCKETS) + break; + + switch (type) { + case GSCAN_ATTRIBUTE_BASE_PERIOD: + scan_param->scan_fr = nla_get_u32(iter) / 1000; + break; + case GSCAN_ATTRIBUTE_NUM_BUCKETS: + scan_param->nchannel_buckets = nla_get_u32(iter); + break; + case GSCAN_ATTRIBUTE_CH_BUCKET_1: + case GSCAN_ATTRIBUTE_CH_BUCKET_2: + case GSCAN_ATTRIBUTE_CH_BUCKET_3: + case GSCAN_ATTRIBUTE_CH_BUCKET_4: + case GSCAN_ATTRIBUTE_CH_BUCKET_5: + case GSCAN_ATTRIBUTE_CH_BUCKET_6: + case GSCAN_ATTRIBUTE_CH_BUCKET_7: + nla_for_each_nested(iter1, iter, tmp1) { + type = nla_type(iter1); + ch_bucket = + scan_param->channel_bucket; + + switch (type) { + case GSCAN_ATTRIBUTE_BUCKET_ID: + break; + case GSCAN_ATTRIBUTE_BUCKET_PERIOD: + ch_bucket[j].bucket_freq_multiple = + nla_get_u32(iter1) / 1000; + break; + case GSCAN_ATTRIBUTE_BUCKET_NUM_CHANNELS: + ch_bucket[j].num_channels = + nla_get_u32(iter1); + break; + case GSCAN_ATTRIBUTE_BUCKET_CHANNELS: + nla_for_each_nested(iter2, iter1, tmp2) { + if (k >= PFN_SWC_RSSI_WINDOW_MAX) + break; + ch_bucket[j].chan_list[k] = + nla_get_u32(iter2); + k++; + } + k = 0; + break; + case GSCAN_ATTRIBUTE_BUCKETS_BAND: + ch_bucket[j].band = (uint16) + nla_get_u32(iter1); + break; + case GSCAN_ATTRIBUTE_REPORT_EVENTS: + ch_bucket[j].report_flag = (uint8) + nla_get_u32(iter1); + break; + } + } + j++; + break; + } + } + + if (dhd_dev_pno_set_cfg_gscan(bcmcfg_to_prmry_ndev(cfg), + DHD_PNO_SCAN_CFG_ID, scan_param, 0) < 0) { + WL_ERR(("Could not set GSCAN scan cfg\n")); + err = -EINVAL; + } + + kfree(scan_param); + return err; + +} + +static int rtw_cfgvendor_hotlist_cfg(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int err = 0; + struct bcm_cfg80211 *cfg = wiphy_priv(wiphy); + gscan_hotlist_scan_params_t *hotlist_params; + int tmp, tmp1, tmp2, type, j = 0, dummy; + const struct nlattr *outer, *inner, *iter; + uint8 flush = 0; + struct bssid_t *pbssid; + + hotlist_params = (gscan_hotlist_scan_params_t *)kzalloc(len, GFP_KERNEL); + if (!hotlist_params) { + WL_ERR(("Cannot Malloc mem to parse config commands size - %d bytes\n", len)); + return -1; + } + + hotlist_params->lost_ap_window = GSCAN_LOST_AP_WINDOW_DEFAULT; + + nla_for_each_attr(iter, data, len, tmp2) { + type = nla_type(iter); + switch (type) { + case GSCAN_ATTRIBUTE_HOTLIST_BSSIDS: + pbssid = hotlist_params->bssid; + nla_for_each_nested(outer, iter, tmp) { + nla_for_each_nested(inner, outer, tmp1) { + type = nla_type(inner); + + switch (type) { + case GSCAN_ATTRIBUTE_BSSID: + memcpy(&(pbssid[j].macaddr), + nla_data(inner), ETHER_ADDR_LEN); + break; + case GSCAN_ATTRIBUTE_RSSI_LOW: + pbssid[j].rssi_reporting_threshold = + (int8) nla_get_u8(inner); + break; + case GSCAN_ATTRIBUTE_RSSI_HIGH: + dummy = (int8) nla_get_u8(inner); + break; + } + } + j++; + } + hotlist_params->nbssid = j; + break; + case GSCAN_ATTRIBUTE_HOTLIST_FLUSH: + flush = nla_get_u8(iter); + break; + case GSCAN_ATTRIBUTE_LOST_AP_SAMPLE_SIZE: + hotlist_params->lost_ap_window = nla_get_u32(iter); + break; + } + + } + + if (dhd_dev_pno_set_cfg_gscan(bcmcfg_to_prmry_ndev(cfg), + DHD_PNO_GEOFENCE_SCAN_CFG_ID, hotlist_params, flush) < 0) { + WL_ERR(("Could not set GSCAN HOTLIST cfg\n")); + err = -EINVAL; + goto exit; + } +exit: + kfree(hotlist_params); + return err; +} +static int rtw_cfgvendor_set_batch_scan_cfg(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int err = 0, tmp, type; + struct bcm_cfg80211 *cfg = wiphy_priv(wiphy); + gscan_batch_params_t batch_param; + const struct nlattr *iter; + + batch_param.mscan = batch_param.bestn = 0; + batch_param.buffer_threshold = GSCAN_BATCH_NO_THR_SET; + + nla_for_each_attr(iter, data, len, tmp) { + type = nla_type(iter); + + switch (type) { + case GSCAN_ATTRIBUTE_NUM_AP_PER_SCAN: + batch_param.bestn = nla_get_u32(iter); + break; + case GSCAN_ATTRIBUTE_NUM_SCANS_TO_CACHE: + batch_param.mscan = nla_get_u32(iter); + break; + case GSCAN_ATTRIBUTE_REPORT_THRESHOLD: + batch_param.buffer_threshold = nla_get_u32(iter); + break; + } + } + + if (dhd_dev_pno_set_cfg_gscan(bcmcfg_to_prmry_ndev(cfg), + DHD_PNO_BATCH_SCAN_CFG_ID, &batch_param, 0) < 0) { + WL_ERR(("Could not set batch cfg\n")); + err = -EINVAL; + return err; + } + + return err; +} + +static int rtw_cfgvendor_significant_change_cfg(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int err = 0; + struct bcm_cfg80211 *cfg = wiphy_priv(wiphy); + gscan_swc_params_t *significant_params; + int tmp, tmp1, tmp2, type, j = 0; + const struct nlattr *outer, *inner, *iter; + uint8 flush = 0; + wl_pfn_significant_bssid_t *pbssid; + + significant_params = (gscan_swc_params_t *) kzalloc(len, GFP_KERNEL); + if (!significant_params) { + WL_ERR(("Cannot Malloc mem to parse config commands size - %d bytes\n", len)); + return -1; + } + + + nla_for_each_attr(iter, data, len, tmp2) { + type = nla_type(iter); + + switch (type) { + case GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_FLUSH: + flush = nla_get_u8(iter); + break; + case GSCAN_ATTRIBUTE_RSSI_SAMPLE_SIZE: + significant_params->rssi_window = nla_get_u16(iter); + break; + case GSCAN_ATTRIBUTE_LOST_AP_SAMPLE_SIZE: + significant_params->lost_ap_window = nla_get_u16(iter); + break; + case GSCAN_ATTRIBUTE_MIN_BREACHING: + significant_params->swc_threshold = nla_get_u16(iter); + break; + case GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_BSSIDS: + pbssid = significant_params->bssid_elem_list; + nla_for_each_nested(outer, iter, tmp) { + nla_for_each_nested(inner, outer, tmp1) { + switch (nla_type(inner)) { + case GSCAN_ATTRIBUTE_BSSID: + memcpy(&(pbssid[j].macaddr), + nla_data(inner), + ETHER_ADDR_LEN); + break; + case GSCAN_ATTRIBUTE_RSSI_HIGH: + pbssid[j].rssi_high_threshold = + (int8) nla_get_u8(inner); + break; + case GSCAN_ATTRIBUTE_RSSI_LOW: + pbssid[j].rssi_low_threshold = + (int8) nla_get_u8(inner); + break; + } + } + j++; + } + break; + } + } + significant_params->nbssid = j; + + if (dhd_dev_pno_set_cfg_gscan(bcmcfg_to_prmry_ndev(cfg), + DHD_PNO_SIGNIFICANT_SCAN_CFG_ID, significant_params, flush) < 0) { + WL_ERR(("Could not set GSCAN significant cfg\n")); + err = -EINVAL; + goto exit; + } +exit: + kfree(significant_params); + return err; +} +#endif /* GSCAN_SUPPORT */ + +#if defined(RTT_SUPPORT) && 0 +void rtw_cfgvendor_rtt_evt(void *ctx, void *rtt_data) +{ + struct wireless_dev *wdev = (struct wireless_dev *)ctx; + struct wiphy *wiphy; + struct sk_buff *skb; + uint32 tot_len = NLMSG_DEFAULT_SIZE, entry_len = 0; + gfp_t kflags; + rtt_report_t *rtt_report = NULL; + rtt_result_t *rtt_result = NULL; + struct list_head *rtt_list; + wiphy = wdev->wiphy; + + WL_DBG(("In\n")); + /* Push the data to the skb */ + if (!rtt_data) { + WL_ERR(("rtt_data is NULL\n")); + goto exit; + } + rtt_list = (struct list_head *)rtt_data; + kflags = in_atomic() ? GFP_ATOMIC : GFP_KERNEL; + /* Alloc the SKB for vendor_event */ + skb = rtw_cfg80211_vendor_event_alloc(wiphy, wdev, tot_len, GOOGLE_RTT_COMPLETE_EVENT, kflags); + if (!skb) { + WL_ERR(("skb alloc failed")); + goto exit; + } + /* fill in the rtt results on each entry */ + list_for_each_entry(rtt_result, rtt_list, list) { + entry_len = 0; + if (rtt_result->TOF_type == TOF_TYPE_ONE_WAY) { + entry_len = sizeof(rtt_report_t); + rtt_report = kzalloc(entry_len, kflags); + if (!rtt_report) { + WL_ERR(("rtt_report alloc failed")); + goto exit; + } + rtt_report->addr = rtt_result->peer_mac; + rtt_report->num_measurement = 1; /* ONE SHOT */ + rtt_report->status = rtt_result->err_code; + rtt_report->type = (rtt_result->TOF_type == TOF_TYPE_ONE_WAY) ? RTT_ONE_WAY : RTT_TWO_WAY; + rtt_report->peer = rtt_result->target_info->peer; + rtt_report->channel = rtt_result->target_info->channel; + rtt_report->rssi = rtt_result->avg_rssi; + /* tx_rate */ + rtt_report->tx_rate = rtt_result->tx_rate; + /* RTT */ + rtt_report->rtt = rtt_result->meanrtt; + rtt_report->rtt_sd = rtt_result->sdrtt; + /* convert to centi meter */ + if (rtt_result->distance != 0xffffffff) + rtt_report->distance = (rtt_result->distance >> 2) * 25; + else /* invalid distance */ + rtt_report->distance = -1; + + rtt_report->ts = rtt_result->ts; + nla_append(skb, entry_len, rtt_report); + kfree(rtt_report); + } + } + rtw_cfg80211_vendor_event(skb, kflags); +exit: + return; +} + +static int rtw_cfgvendor_rtt_set_config(struct wiphy *wiphy, struct wireless_dev *wdev, + const void *data, int len) +{ + int err = 0, rem, rem1, rem2, type; + rtt_config_params_t rtt_param; + rtt_target_info_t *rtt_target = NULL; + const struct nlattr *iter, *iter1, *iter2; + int8 eabuf[ETHER_ADDR_STR_LEN]; + int8 chanbuf[CHANSPEC_STR_LEN]; + struct bcm_cfg80211 *cfg = wiphy_priv(wiphy); + + WL_DBG(("In\n")); + err = dhd_dev_rtt_register_noti_callback(wdev->netdev, wdev, wl_cfgvendor_rtt_evt); + if (err < 0) { + WL_ERR(("failed to register rtt_noti_callback\n")); + goto exit; + } + memset(&rtt_param, 0, sizeof(rtt_param)); + nla_for_each_attr(iter, data, len, rem) { + type = nla_type(iter); + switch (type) { + case RTT_ATTRIBUTE_TARGET_CNT: + rtt_param.rtt_target_cnt = nla_get_u8(iter); + if (rtt_param.rtt_target_cnt > RTT_MAX_TARGET_CNT) { + WL_ERR(("exceed max target count : %d\n", + rtt_param.rtt_target_cnt)); + err = BCME_RANGE; + } + break; + case RTT_ATTRIBUTE_TARGET_INFO: + rtt_target = rtt_param.target_info; + nla_for_each_nested(iter1, iter, rem1) { + nla_for_each_nested(iter2, iter1, rem2) { + type = nla_type(iter2); + switch (type) { + case RTT_ATTRIBUTE_TARGET_MAC: + memcpy(&rtt_target->addr, nla_data(iter2), ETHER_ADDR_LEN); + break; + case RTT_ATTRIBUTE_TARGET_TYPE: + rtt_target->type = nla_get_u8(iter2); + break; + case RTT_ATTRIBUTE_TARGET_PEER: + rtt_target->peer = nla_get_u8(iter2); + break; + case RTT_ATTRIBUTE_TARGET_CHAN: + memcpy(&rtt_target->channel, nla_data(iter2), + sizeof(rtt_target->channel)); + break; + case RTT_ATTRIBUTE_TARGET_MODE: + rtt_target->continuous = nla_get_u8(iter2); + break; + case RTT_ATTRIBUTE_TARGET_INTERVAL: + rtt_target->interval = nla_get_u32(iter2); + break; + case RTT_ATTRIBUTE_TARGET_NUM_MEASUREMENT: + rtt_target->measure_cnt = nla_get_u32(iter2); + break; + case RTT_ATTRIBUTE_TARGET_NUM_PKT: + rtt_target->ftm_cnt = nla_get_u32(iter2); + break; + case RTT_ATTRIBUTE_TARGET_NUM_RETRY: + rtt_target->retry_cnt = nla_get_u32(iter2); + } + } + /* convert to chanspec value */ + rtt_target->chanspec = dhd_rtt_convert_to_chspec(rtt_target->channel); + if (rtt_target->chanspec == 0) { + WL_ERR(("Channel is not valid\n")); + goto exit; + } + WL_INFORM(("Target addr %s, Channel : %s for RTT\n", + bcm_ether_ntoa((const struct ether_addr *)&rtt_target->addr, eabuf), + wf_chspec_ntoa(rtt_target->chanspec, chanbuf))); + rtt_target++; + } + break; + } + } + WL_DBG(("leave :target_cnt : %d\n", rtt_param.rtt_target_cnt)); + if (dhd_dev_rtt_set_cfg(bcmcfg_to_prmry_ndev(cfg), &rtt_param) < 0) { + WL_ERR(("Could not set RTT configuration\n")); + err = -EINVAL; + } +exit: + return err; +} + +static int rtw_cfgvendor_rtt_cancel_config(struct wiphy *wiphy, struct wireless_dev *wdev, + const void *data, int len) +{ + int err = 0, rem, type, target_cnt = 0; + const struct nlattr *iter; + struct ether_addr *mac_list = NULL, *mac_addr = NULL; + struct bcm_cfg80211 *cfg = wiphy_priv(wiphy); + + nla_for_each_attr(iter, data, len, rem) { + type = nla_type(iter); + switch (type) { + case RTT_ATTRIBUTE_TARGET_CNT: + target_cnt = nla_get_u8(iter); + mac_list = (struct ether_addr *)kzalloc(target_cnt * ETHER_ADDR_LEN , GFP_KERNEL); + if (mac_list == NULL) { + WL_ERR(("failed to allocate mem for mac list\n")); + goto exit; + } + mac_addr = &mac_list[0]; + break; + case RTT_ATTRIBUTE_TARGET_MAC: + if (mac_addr) + memcpy(mac_addr++, nla_data(iter), ETHER_ADDR_LEN); + else { + WL_ERR(("mac_list is NULL\n")); + goto exit; + } + break; + } + if (dhd_dev_rtt_cancel_cfg(bcmcfg_to_prmry_ndev(cfg), mac_list, target_cnt) < 0) { + WL_ERR(("Could not cancel RTT configuration\n")); + err = -EINVAL; + goto exit; + } + } +exit: + if (mac_list) + kfree(mac_list); + return err; +} +static int rtw_cfgvendor_rtt_get_capability(struct wiphy *wiphy, struct wireless_dev *wdev, + const void *data, int len) +{ + int err = 0; + struct bcm_cfg80211 *cfg = wiphy_priv(wiphy); + rtt_capabilities_t capability; + + err = dhd_dev_rtt_capability(bcmcfg_to_prmry_ndev(cfg), &capability); + if (unlikely(err)) { + WL_ERR(("Vendor Command reply failed ret:%d\n", err)); + goto exit; + } + err = rtw_cfgvendor_send_cmd_reply(wiphy, bcmcfg_to_prmry_ndev(cfg), + &capability, sizeof(capability)); + + if (unlikely(err)) + WL_ERR(("Vendor Command reply failed ret:%d\n", err)); +exit: + return err; +} + +#endif /* RTT_SUPPORT */ + +#ifdef CONFIG_RTW_CFGVENDOR_LLSTATS +enum { + LSTATS_SUBCMD_GET_INFO = ANDROID_NL80211_SUBCMD_LSTATS_RANGE_START, + LSTATS_SUBCMD_SET_INFO, + LSTATS_SUBCMD_CLEAR_INFO, +}; +static void LinkLayerStats(_adapter *padapter) +{ + struct xmit_priv *pxmitpriv = &(padapter->xmitpriv); + struct recv_priv *precvpriv = &(padapter->recvpriv); + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter); + u32 ps_time, trx_total_time; + u64 tx_bytes, rx_bytes, trx_total_bytes = 0; + u64 tmp = 0; + + RTW_DBG("%s adapter type : %u\n", __func__, padapter->adapter_type); + + tx_bytes = 0; + rx_bytes = 0; + ps_time = 0; + trx_total_time = 0; + + if ( padapter->netif_up == _TRUE ) { + + pwrpriv->on_time = rtw_get_passing_time_ms(pwrpriv->radio_on_start_time); + + if (rtw_mi_check_fwstate(padapter, WIFI_ASOC_STATE)) { + if ( pwrpriv->bpower_saving == _TRUE ) { + pwrpriv->pwr_saving_time += rtw_get_passing_time_ms(pwrpriv->pwr_saving_start_time); + pwrpriv->pwr_saving_start_time = rtw_get_current_time(); + } + } else { +#ifdef CONFIG_IPS + if ( pwrpriv->bpower_saving == _TRUE ) { + pwrpriv->pwr_saving_time += rtw_get_passing_time_ms(pwrpriv->pwr_saving_start_time); + pwrpriv->pwr_saving_start_time = rtw_get_current_time(); + } +#else + pwrpriv->pwr_saving_time = pwrpriv->on_time; +#endif + } + + ps_time = pwrpriv->pwr_saving_time; + + /* Deviation caused by caculation start time */ + if ( ps_time > pwrpriv->on_time ) + ps_time = pwrpriv->on_time; + + tx_bytes = pdvobjpriv->traffic_stat.last_tx_bytes; + rx_bytes = pdvobjpriv->traffic_stat.last_rx_bytes; + trx_total_bytes = tx_bytes + rx_bytes; + + trx_total_time = pwrpriv->on_time - ps_time; + + if ( trx_total_bytes == 0) { + pwrpriv->tx_time = 0; + pwrpriv->rx_time = 0; + } else { + + /* tx_time = (trx_total_time * tx_total_bytes) / trx_total_bytes; */ + /* rx_time = (trx_total_time * rx_total_bytes) / trx_total_bytes; */ + + tmp = (tx_bytes * trx_total_time); + tmp = rtw_division64(tmp, trx_total_bytes); + pwrpriv->tx_time = tmp; + + tmp = (rx_bytes * trx_total_time); + tmp = rtw_division64(tmp, trx_total_bytes); + pwrpriv->rx_time = tmp; + + } + + } + else { + pwrpriv->on_time = 0; + pwrpriv->tx_time = 0; + pwrpriv->rx_time = 0; + } + +#ifdef CONFIG_RTW_WIFI_HAL_DEBUG + RTW_INFO("- tx_bytes : %llu rx_bytes : %llu total bytes : %llu\n", tx_bytes, rx_bytes, trx_total_bytes); + RTW_INFO("- netif_up = %s, on_time : %u ms\n", padapter->netif_up ? "1":"0", pwrpriv->on_time); + RTW_INFO("- pwr_saving_time : %u (%u) ms\n", pwrpriv->pwr_saving_time, ps_time); + RTW_INFO("- trx_total_time : %u ms\n", trx_total_time); + RTW_INFO("- tx_time : %u ms\n", pwrpriv->tx_time); + RTW_INFO("- rx_time : %u ms\n", pwrpriv->rx_time); +#endif /* CONFIG_RTW_WIFI_HAL_DEBUG */ + +} + +#define DUMMY_TIME_STATICS 99 +static int rtw_cfgvendor_lstats_get_info(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int err = 0; + _adapter *padapter = GET_PRIMARY_ADAPTER(wiphy_to_adapter(wiphy)); + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + wifi_radio_stat_internal *radio; + wifi_iface_stat *iface; + char *output; + + output = rtw_malloc(sizeof(wifi_radio_stat_internal) + sizeof(wifi_iface_stat)); + if (output == NULL) { + RTW_DBG("Allocate lstats info buffer fail!\n"); + } + + radio = (wifi_radio_stat_internal *)output; + + radio->num_channels = 0; + radio->radio = 1; + + /* to get on_time, tx_time, rx_time */ + LinkLayerStats(padapter); + + radio->on_time = pwrpriv->on_time; + radio->tx_time = pwrpriv->tx_time; + radio->rx_time = pwrpriv->rx_time; + radio->on_time_scan = 0; + radio->on_time_nbd = 0; + radio->on_time_gscan = 0; + radio->on_time_pno_scan = 0; + radio->on_time_hs20 = 0; + #ifdef CONFIG_RTW_WIFI_HAL_DEBUG + RTW_INFO("==== %s ====\n", __func__); + RTW_INFO("radio->radio : %d\n", (radio->radio)); + RTW_INFO("pwrpriv->on_time : %u ms\n", (pwrpriv->on_time)); + RTW_INFO("pwrpriv->tx_time : %u ms\n", (pwrpriv->tx_time)); + RTW_INFO("pwrpriv->rx_time : %u ms\n", (pwrpriv->rx_time)); + RTW_INFO("radio->on_time : %u ms\n", (radio->on_time)); + RTW_INFO("radio->tx_time : %u ms\n", (radio->tx_time)); + RTW_INFO("radio->rx_time : %u ms\n", (radio->rx_time)); + #endif /* CONFIG_RTW_WIFI_HAL_DEBUG */ + + RTW_DBG(FUNC_NDEV_FMT" %s\n", FUNC_NDEV_ARG(wdev_to_ndev(wdev)), (char*)data); + err = rtw_cfgvendor_send_cmd_reply(wiphy, wdev_to_ndev(wdev), + output, sizeof(wifi_iface_stat) + sizeof(wifi_radio_stat_internal)); + if (unlikely(err)) + RTW_ERR(FUNC_NDEV_FMT"Vendor Command reply failed ret:%d \n" + , FUNC_NDEV_ARG(wdev_to_ndev(wdev)), err); + rtw_mfree(output, sizeof(wifi_iface_stat) + sizeof(wifi_radio_stat_internal)); + return err; +} +static int rtw_cfgvendor_lstats_set_info(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int err = 0; + RTW_INFO("%s\n", __func__); + return err; +} +static int rtw_cfgvendor_lstats_clear_info(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int err = 0; + RTW_INFO("%s\n", __func__); + return err; +} +#endif /* CONFIG_RTW_CFGVENDOR_LLSTATS */ +#ifdef CONFIG_RTW_CFGVENDOR_RSSIMONITOR +static int rtw_cfgvendor_set_rssi_monitor(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + _adapter *padapter = GET_PRIMARY_ADAPTER(wiphy_to_adapter(wiphy)); + struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(padapter); + + struct recv_priv *precvpriv = &padapter->recvpriv; + int err = 0, rem, type; + const struct nlattr *iter; + + RTW_DBG(FUNC_NDEV_FMT" %s\n", FUNC_NDEV_ARG(wdev_to_ndev(wdev)), (char*)data); + + nla_for_each_attr(iter, data, len, rem) { + type = nla_type(iter); + + switch (type) { + case RSSI_MONITOR_ATTRIBUTE_MAX_RSSI: + pwdev_priv->rssi_monitor_max = (s8)nla_get_u32(iter);; + break; + case RSSI_MONITOR_ATTRIBUTE_MIN_RSSI: + pwdev_priv->rssi_monitor_min = (s8)nla_get_u32(iter); + break; + case RSSI_MONITOR_ATTRIBUTE_START: + pwdev_priv->rssi_monitor_enable = (u8)nla_get_u32(iter); + break; + } + } + + return err; +} + +void rtw_cfgvendor_rssi_monitor_evt(_adapter *padapter) { + struct wireless_dev *wdev = padapter->rtw_wdev; + struct wiphy *wiphy= wdev->wiphy; + struct recv_priv *precvpriv = &padapter->recvpriv; + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct wlan_network *pcur_network = &pmlmepriv->cur_network; + struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(padapter); + struct sk_buff *skb; + u32 tot_len = NLMSG_DEFAULT_SIZE; + gfp_t kflags; + rssi_monitor_evt data ; + s8 rssi = precvpriv->rssi; + + if (pwdev_priv->rssi_monitor_enable == 0 || check_fwstate(pmlmepriv, WIFI_ASOC_STATE) != _TRUE) + return; + + if (rssi < pwdev_priv->rssi_monitor_max || rssi > pwdev_priv->rssi_monitor_min) + return; + + kflags = in_atomic() ? GFP_ATOMIC : GFP_KERNEL; + + /* Alloc the SKB for vendor_event */ + skb = rtw_cfg80211_vendor_event_alloc(wiphy, wdev, tot_len, GOOGLE_RSSI_MONITOR_EVENT, kflags); + if (!skb) { + goto exit; + } + + _rtw_memset(&data, 0, sizeof(data)); + + data.version = RSSI_MONITOR_EVT_VERSION; + data.cur_rssi = rssi; + _rtw_memcpy(data.BSSID, pcur_network->network.MacAddress, sizeof(mac_addr)); + + nla_append(skb, sizeof(data), &data); + + rtw_cfg80211_vendor_event(skb, kflags); +exit: + return; +} +#endif /* CONFIG_RTW_CFGVENDOR_RSSIMONITR */ + +#ifdef CONFIG_RTW_CFGVENDOR_WIFI_LOGGER +static int rtw_cfgvendor_logger_start_logging(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int ret = 0, rem, type; + char ring_name[32] = {0}; + int log_level = 0, flags = 0, time_intval = 0, threshold = 0; + const struct nlattr *iter; + + nla_for_each_attr(iter, data, len, rem) { + type = nla_type(iter); + switch (type) { + case LOGGER_ATTRIBUTE_RING_NAME: + strncpy(ring_name, nla_data(iter), + MIN(sizeof(ring_name) -1, nla_len(iter))); + break; + case LOGGER_ATTRIBUTE_LOG_LEVEL: + log_level = nla_get_u32(iter); + break; + case LOGGER_ATTRIBUTE_RING_FLAGS: + flags = nla_get_u32(iter); + break; + case LOGGER_ATTRIBUTE_LOG_TIME_INTVAL: + time_intval = nla_get_u32(iter); + break; + case LOGGER_ATTRIBUTE_LOG_MIN_DATA_SIZE: + threshold = nla_get_u32(iter); + break; + default: + RTW_ERR("Unknown type: %d\n", type); + ret = WIFI_ERROR_INVALID_ARGS; + goto exit; + } + } + +exit: + return ret; +} +static int rtw_cfgvendor_logger_get_feature(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int err = 0; + u32 supported_features = 0; + + err = rtw_cfgvendor_send_cmd_reply(wiphy, wdev_to_ndev(wdev), &supported_features, sizeof(supported_features)); + + if (unlikely(err)) + RTW_ERR(FUNC_NDEV_FMT" Vendor Command reply failed ret:%d\n" + , FUNC_NDEV_ARG(wdev_to_ndev(wdev)), err); + + return err; +} +static int rtw_cfgvendor_logger_get_version(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + _adapter *padapter = GET_PRIMARY_ADAPTER(wiphy_to_adapter(wiphy)); + HAL_DATA_TYPE *hal = GET_HAL_DATA(padapter); + int ret = 0, rem, type; + int buf_len = 1024; + char *buf_ptr; + const struct nlattr *iter; + gfp_t kflags; + + kflags = in_atomic() ? GFP_ATOMIC : GFP_KERNEL; + buf_ptr = kzalloc(buf_len, kflags); + if (!buf_ptr) { + RTW_ERR("failed to allocate the buffer for version n"); + ret = -ENOMEM; + goto exit; + } + nla_for_each_attr(iter, data, len, rem) { + type = nla_type(iter); + switch (type) { + case LOGGER_ATTRIBUTE_GET_DRIVER: + memcpy(buf_ptr, DRIVERVERSION, strlen(DRIVERVERSION)+1); + break; + case LOGGER_ATTRIBUTE_GET_FW: + sprintf(buf_ptr, "v%d.%d", hal->firmware_version, hal->firmware_sub_version); + break; + default: + RTW_ERR("Unknown type: %d\n", type); + ret = -EINVAL; + goto exit; + } + } + if (ret < 0) { + RTW_ERR("failed to get the version %d\n", ret); + goto exit; + } + + + ret = rtw_cfgvendor_send_cmd_reply(wiphy, wdev_to_ndev(wdev), buf_ptr, strlen(buf_ptr)); +exit: + kfree(buf_ptr); + return ret; +} + +static int rtw_cfgvendor_logger_get_ring_status(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int ret = 0; + int ring_id; + char ring_buf_name[] = "RTW_RING_BUFFER"; + + struct sk_buff *skb; + wifi_ring_buffer_status ring_status; + + + _rtw_memcpy(ring_status.name, ring_buf_name, strlen(ring_buf_name)+1); + ring_status.ring_id = 1; + /* Alloc the SKB for vendor_event */ + skb = cfg80211_vendor_cmd_alloc_reply_skb(wiphy, + sizeof(wifi_ring_buffer_status)); + if (!skb) { + RTW_ERR("skb allocation is failed\n"); + ret = FAIL; + goto exit; + } + + nla_put_u32(skb, LOGGER_ATTRIBUTE_RING_NUM, 1); + nla_put(skb, LOGGER_ATTRIBUTE_RING_STATUS, sizeof(wifi_ring_buffer_status), + &ring_status); + ret = cfg80211_vendor_cmd_reply(skb); + + if (ret) { + RTW_ERR("Vendor Command reply failed ret:%d \n", ret); + } +exit: + return ret; +} + +static int rtw_cfgvendor_logger_get_ring_data(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int ret = 0, rem, type; + char ring_name[32] = {0}; + const struct nlattr *iter; + + nla_for_each_attr(iter, data, len, rem) { + type = nla_type(iter); + switch (type) { + case LOGGER_ATTRIBUTE_RING_NAME: + strncpy(ring_name, nla_data(iter), + MIN(sizeof(ring_name) -1, nla_len(iter))); + RTW_INFO(" %s LOGGER_ATTRIBUTE_RING_NAME : %s\n", __func__, ring_name); + break; + default: + RTW_ERR("Unknown type: %d\n", type); + return ret; + } + } + + + return ret; +} + +static int rtw_cfgvendor_logger_get_firmware_memory_dump(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int ret = WIFI_ERROR_NOT_SUPPORTED; + + return ret; +} + +static int rtw_cfgvendor_logger_start_pkt_fate_monitoring(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int ret = WIFI_SUCCESS; + + return ret; +} + +static int rtw_cfgvendor_logger_get_tx_pkt_fates(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int ret = WIFI_SUCCESS; + + return ret; +} + +static int rtw_cfgvendor_logger_get_rx_pkt_fates(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int ret = WIFI_SUCCESS; + + return ret; +} + +#endif /* CONFIG_RTW_CFGVENDOR_WIFI_LOGGER */ +#ifdef CONFIG_RTW_WIFI_HAL +#ifdef CONFIG_RTW_CFGVENDOR_RANDOM_MAC_OUI + +#ifndef ETHER_ISMULTI +#define ETHER_ISMULTI(ea) (((const u8 *)(ea))[0] & 1) +#endif + + +static u8 null_addr[ETH_ALEN] = {0}; +static void rtw_hal_random_gen_mac_addr(u8 *mac_addr) +{ + do { + get_random_bytes(&mac_addr[3], ETH_ALEN-3); + if (memcmp(mac_addr, null_addr, ETH_ALEN) != 0) + break; + } while(1); +} + +void rtw_hal_pno_random_gen_mac_addr(PADAPTER adapter) +{ + u8 mac_addr[ETH_ALEN]; + struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(adapter); + + memcpy(mac_addr, pwdev_priv->pno_mac_addr, ETH_ALEN); + if (mac_addr[0] == 0xFF) return; + rtw_hal_random_gen_mac_addr(mac_addr); + memcpy(pwdev_priv->pno_mac_addr, mac_addr, ETH_ALEN); +#ifdef CONFIG_RTW_DEBUG + print_hex_dump(KERN_DEBUG, "pno_mac_addr: ", + DUMP_PREFIX_OFFSET, 16, 1, pwdev_priv->pno_mac_addr, + ETH_ALEN, 1); +#endif +} + + +static int rtw_cfgvendor_set_rand_mac_oui(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int err = 0; + PADAPTER adapter; + void *devaddr; + struct net_device *netdev; + int type, mac_len; + u8 pno_random_mac_oui[3]; + u8 mac_addr[ETH_ALEN] = {0}; + struct pwrctrl_priv *pwrctl; + struct rtw_wdev_priv *pwdev_priv; + + type = nla_type(data); + mac_len = nla_len(data); + if (mac_len != 3) { + RTW_ERR("%s oui len error %d != 3\n", __func__, mac_len); + return -1; + } + + if (type == ANDR_WIFI_ATTRIBUTE_RANDOM_MAC_OUI) { + memcpy(pno_random_mac_oui, nla_data(data), 3); + print_hex_dump(KERN_DEBUG, "pno_random_mac_oui: ", + DUMP_PREFIX_OFFSET, 16, 1, pno_random_mac_oui, + 3, 1); + + if (ETHER_ISMULTI(pno_random_mac_oui)) { + pr_err("%s: oui is multicast address\n", __func__); + return -1; + } + + adapter = wiphy_to_adapter(wiphy); + if (adapter == NULL) { + pr_err("%s: wiphy_to_adapter == NULL\n", __func__); + return -1; + } + + pwdev_priv = adapter_wdev_data(adapter); + + memcpy(mac_addr, pno_random_mac_oui, 3); + rtw_hal_random_gen_mac_addr(mac_addr); + memcpy(pwdev_priv->pno_mac_addr, mac_addr, ETH_ALEN); +#ifdef CONFIG_RTW_DEBUG + print_hex_dump(KERN_DEBUG, "pno_mac_addr: ", + DUMP_PREFIX_OFFSET, 16, 1, pwdev_priv->pno_mac_addr, + ETH_ALEN, 1); +#endif + } else { + RTW_ERR("%s oui type error %x != 0x2\n", __func__, type); + err = -1; + } + + + return err; +} +#endif + +#if defined(CONFIG_RTW_CFGVENDOR_RANDOM_MAC_OUI) || defined(CONFIG_RTW_SCAN_RAND) +void rtw_hal_set_hw_mac_addr(PADAPTER adapter, u8 *mac_addr) +{ + rtw_ps_deny(adapter, PS_DENY_IOCTL); + LeaveAllPowerSaveModeDirect(adapter); + +#ifdef CONFIG_MI_WITH_MBSSID_CAM + rtw_hal_change_macaddr_mbid(adapter, mac_addr); +#else + rtw_hal_set_hwreg(adapter, HW_VAR_MAC_ADDR, mac_addr); +#endif +#ifdef CONFIG_RTW_DEBUG + rtw_hal_dump_macaddr(RTW_DBGDUMP, adapter); +#endif + rtw_ps_deny_cancel(adapter, PS_DENY_IOCTL); +} +#endif + +#ifdef CONFIG_RTW_CFGVENDOR_WIFI_OFFLOAD +static int rtw_cfgvendor_start_mkeep_alive(struct wiphy *wiphy, struct wireless_dev *wdev, + const void *data, int len) +{ + int ret = WIFI_SUCCESS; + + RTW_INFO("%s : TODO\n", __func__); + + return ret; +} + +static int rtw_cfgvendor_stop_mkeep_alive(struct wiphy *wiphy, struct wireless_dev *wdev, + const void *data, int len) +{ + int ret = WIFI_SUCCESS; + + RTW_INFO("%s : TODO\n", __func__); + + return ret; +} +#endif + +static int rtw_cfgvendor_set_nodfs_flag(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int err = 0; + int type; + u32 nodfs = 0; + _adapter *padapter = GET_PRIMARY_ADAPTER(wiphy_to_adapter(wiphy)); + + RTW_DBG(FUNC_NDEV_FMT" %s\n", FUNC_NDEV_ARG(wdev_to_ndev(wdev)), (char*)data); + + type = nla_type(data); + if (type == ANDR_WIFI_ATTRIBUTE_NODFS_SET) { + nodfs = nla_get_u32(data); + adapter_to_dvobj(padapter)->nodfs = nodfs; + } else { + err = -EINVAL; + } + + RTW_INFO("%s nodfs=%d, err=%d\n", __func__, nodfs, err); + + return err; +} + +static int rtw_cfgvendor_set_country(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ +#define CNTRY_BUF_SZ 4 /* Country string is 3 bytes + NUL */ + int err = 0, rem, type; + char country_code[CNTRY_BUF_SZ] = {0}; + const struct nlattr *iter; + _adapter *padapter = GET_PRIMARY_ADAPTER(wiphy_to_adapter(wiphy)); + + RTW_DBG(FUNC_NDEV_FMT" %s\n", FUNC_NDEV_ARG(wdev_to_ndev(wdev)), (char*)data); + + nla_for_each_attr(iter, data, len, rem) { + type = nla_type(iter); + switch (type) { + case ANDR_WIFI_ATTRIBUTE_COUNTRY: + _rtw_memcpy(country_code, nla_data(iter), + MIN(nla_len(iter), CNTRY_BUF_SZ)); + break; + default: + RTW_ERR("Unknown type: %d\n", type); + return -EINVAL; + } + } + + RTW_INFO("%s country_code:\"%c%c\" \n", __func__, country_code[0], country_code[1]); + + rtw_set_country(padapter, country_code); + + return err; +} + +static int rtw_cfgvendor_set_nd_offload(struct wiphy *wiphy, + struct wireless_dev *wdev, const void *data, int len) +{ + int err = 0; + int type; + u8 nd_en = 0; + _adapter *padapter = GET_PRIMARY_ADAPTER(wiphy_to_adapter(wiphy)); + + RTW_DBG(FUNC_NDEV_FMT" %s\n", FUNC_NDEV_ARG(wdev_to_ndev(wdev)), (char*)data); + + type = nla_type(data); + if (type == ANDR_WIFI_ATTRIBUTE_ND_OFFLOAD_VALUE) { + nd_en = nla_get_u8(data); + /* ND has been enabled when wow is enabled */ + } else { + err = -EINVAL; + } + + RTW_INFO("%s nd_en=%d, err=%d\n", __func__, nd_en, err); + + return err; +} +#endif /* CONFIG_RTW_WIFI_HAL */ + +static const struct wiphy_vendor_command rtw_vendor_cmds[] = { +#if defined(GSCAN_SUPPORT) && 0 + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = GSCAN_SUBCMD_GET_CAPABILITIES + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, + .doit = rtw_cfgvendor_gscan_get_capabilities + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = GSCAN_SUBCMD_SET_CONFIG + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, + .doit = rtw_cfgvendor_set_scan_cfg + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = GSCAN_SUBCMD_SET_SCAN_CONFIG + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, + .doit = rtw_cfgvendor_set_batch_scan_cfg + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = GSCAN_SUBCMD_ENABLE_GSCAN + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, + .doit = rtw_cfgvendor_initiate_gscan + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = GSCAN_SUBCMD_ENABLE_FULL_SCAN_RESULTS + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, + .doit = rtw_cfgvendor_enable_full_scan_result + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = GSCAN_SUBCMD_SET_HOTLIST + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, + .doit = rtw_cfgvendor_hotlist_cfg + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = GSCAN_SUBCMD_SET_SIGNIFICANT_CHANGE_CONFIG + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, + .doit = rtw_cfgvendor_significant_change_cfg + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = GSCAN_SUBCMD_GET_SCAN_RESULTS + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, + .doit = rtw_cfgvendor_gscan_get_batch_results + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = GSCAN_SUBCMD_GET_CHANNEL_LIST + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, + .doit = rtw_cfgvendor_gscan_get_channel_list + }, +#endif /* GSCAN_SUPPORT */ +#if defined(RTT_SUPPORT) && 0 + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = RTT_SUBCMD_SET_CONFIG + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, + .doit = rtw_cfgvendor_rtt_set_config + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = RTT_SUBCMD_CANCEL_CONFIG + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, + .doit = rtw_cfgvendor_rtt_cancel_config + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = RTT_SUBCMD_GETCAPABILITY + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, + .doit = rtw_cfgvendor_rtt_get_capability + }, +#endif /* RTT_SUPPORT */ +#ifdef CONFIG_RTW_CFGVENDOR_LLSTATS + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = LSTATS_SUBCMD_GET_INFO + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_lstats_get_info + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = LSTATS_SUBCMD_SET_INFO + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_lstats_set_info + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = LSTATS_SUBCMD_CLEAR_INFO + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_lstats_clear_info + }, +#endif /* CONFIG_RTW_CFGVENDOR_LLSTATS */ +#ifdef CONFIG_RTW_CFGVENDOR_RSSIMONITOR + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = WIFI_SUBCMD_SET_RSSI_MONITOR + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_set_rssi_monitor + }, +#endif /* CONFIG_RTW_CFGVENDOR_RSSIMONITOR */ +#ifdef CONFIG_RTW_CFGVENDOR_WIFI_LOGGER + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = LOGGER_START_LOGGING + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_logger_start_logging + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = LOGGER_GET_FEATURE + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_logger_get_feature + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = LOGGER_GET_VER + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_logger_get_version + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = LOGGER_GET_RING_STATUS + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_logger_get_ring_status + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = LOGGER_GET_RING_DATA + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_logger_get_ring_data + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = LOGGER_TRIGGER_MEM_DUMP + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_logger_get_firmware_memory_dump + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = LOGGER_START_PKT_FATE_MONITORING + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_logger_start_pkt_fate_monitoring + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = LOGGER_GET_TX_PKT_FATES + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_logger_get_tx_pkt_fates + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = LOGGER_GET_RX_PKT_FATES + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_logger_get_rx_pkt_fates + }, +#endif /* CONFIG_RTW_CFGVENDOR_WIFI_LOGGER */ +#ifdef CONFIG_RTW_WIFI_HAL +#ifdef CONFIG_RTW_CFGVENDOR_RANDOM_MAC_OUI + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = WIFI_SUBCMD_SET_PNO_RANDOM_MAC_OUI + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_set_rand_mac_oui + }, +#endif +#ifdef CONFIG_RTW_CFGVENDOR_WIFI_OFFLOAD + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = WIFI_OFFLOAD_SUBCMD_START_MKEEP_ALIVE + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_start_mkeep_alive + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = WIFI_OFFLOAD_SUBCMD_STOP_MKEEP_ALIVE + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_stop_mkeep_alive + }, +#endif + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = WIFI_SUBCMD_NODFS_SET + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_set_nodfs_flag + + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = WIFI_SUBCMD_SET_COUNTRY_CODE + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_set_country + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = WIFI_SUBCMD_CONFIG_ND_OFFLOAD + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_set_nd_offload + }, +#endif /* CONFIG_RTW_WIFI_HAL */ + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = WIFI_SUBCMD_GET_FEATURE_SET + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_get_feature_set + }, + { + { + .vendor_id = OUI_GOOGLE, + .subcmd = WIFI_SUBCMD_GET_FEATURE_SET_MATRIX + }, + .flags = WIPHY_VENDOR_CMD_NEED_WDEV | WIPHY_VENDOR_CMD_NEED_NETDEV, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)) + .policy = VENDOR_CMD_RAW_DATA, +#endif + .doit = rtw_cfgvendor_get_feature_set_matrix + } +}; + +static const struct nl80211_vendor_cmd_info rtw_vendor_events[] = { +#if defined(GSCAN_SUPPORT) && 0 + { OUI_GOOGLE, GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS }, + { OUI_GOOGLE, GSCAN_EVENT_HOTLIST_RESULTS_FOUND }, + { OUI_GOOGLE, GSCAN_EVENT_SCAN_RESULTS_AVAILABLE }, + { OUI_GOOGLE, GSCAN_EVENT_FULL_SCAN_RESULTS }, +#endif /* GSCAN_SUPPORT */ +#if defined(RTT_SUPPORT) && 0 + { OUI_GOOGLE, RTT_EVENT_COMPLETE }, +#endif /* RTT_SUPPORT */ + +#ifdef CONFIG_RTW_CFGVENDOR_RSSIMONITOR + { OUI_GOOGLE, GOOGLE_RSSI_MONITOR_EVENT }, +#endif /* RTW_CFGVENDOR_RSSIMONITR */ + +#if defined(GSCAN_SUPPORT) && 0 + { OUI_GOOGLE, GSCAN_EVENT_COMPLETE_SCAN }, + { OUI_GOOGLE, GSCAN_EVENT_HOTLIST_RESULTS_LOST } +#endif /* GSCAN_SUPPORT */ +}; + +int rtw_cfgvendor_attach(struct wiphy *wiphy) +{ + + RTW_INFO("Register RTW cfg80211 vendor cmd(0x%x) interface\n", NL80211_CMD_VENDOR); + + wiphy->vendor_commands = rtw_vendor_cmds; + wiphy->n_vendor_commands = ARRAY_SIZE(rtw_vendor_cmds); + wiphy->vendor_events = rtw_vendor_events; + wiphy->n_vendor_events = ARRAY_SIZE(rtw_vendor_events); + + return 0; +} + +int rtw_cfgvendor_detach(struct wiphy *wiphy) +{ + RTW_INFO("Vendor: Unregister RTW cfg80211 vendor interface\n"); + + wiphy->vendor_commands = NULL; + wiphy->vendor_events = NULL; + wiphy->n_vendor_commands = 0; + wiphy->n_vendor_events = 0; + + return 0; +} +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)) || defined(RTW_VENDOR_EXT_SUPPORT) */ + +#endif /* CONFIG_IOCTL_CFG80211 */ diff --git a/os_dep/linux/rtw_cfgvendor.h b/os_dep/linux/rtw_cfgvendor.h new file mode 100644 index 0000000..3fefa27 --- /dev/null +++ b/os_dep/linux/rtw_cfgvendor.h @@ -0,0 +1,638 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ + +#ifndef _RTW_CFGVENDOR_H_ +#define _RTW_CFGVENDOR_H_ + +#define OUI_GOOGLE 0x001A11 +#define ATTRIBUTE_U32_LEN (NLA_HDRLEN + 4) +#define VENDOR_ID_OVERHEAD ATTRIBUTE_U32_LEN +#define VENDOR_SUBCMD_OVERHEAD ATTRIBUTE_U32_LEN +#define VENDOR_DATA_OVERHEAD (NLA_HDRLEN) + +#define SCAN_RESULTS_COMPLETE_FLAG_LEN ATTRIBUTE_U32_LEN +#define SCAN_INDEX_HDR_LEN (NLA_HDRLEN) +#define SCAN_ID_HDR_LEN ATTRIBUTE_U32_LEN +#define SCAN_FLAGS_HDR_LEN ATTRIBUTE_U32_LEN +#define GSCAN_NUM_RESULTS_HDR_LEN ATTRIBUTE_U32_LEN +#define GSCAN_RESULTS_HDR_LEN (NLA_HDRLEN) +#define GSCAN_BATCH_RESULT_HDR_LEN (SCAN_INDEX_HDR_LEN + SCAN_ID_HDR_LEN + \ + SCAN_FLAGS_HDR_LEN + \ + GSCAN_NUM_RESULTS_HDR_LEN + \ + GSCAN_RESULTS_HDR_LEN) + +#define VENDOR_REPLY_OVERHEAD (VENDOR_ID_OVERHEAD + \ + VENDOR_SUBCMD_OVERHEAD + \ + VENDOR_DATA_OVERHEAD) +typedef enum { + /* don't use 0 as a valid subcommand */ + VENDOR_NL80211_SUBCMD_UNSPECIFIED, + + /* define all vendor startup commands between 0x0 and 0x0FFF */ + VENDOR_NL80211_SUBCMD_RANGE_START = 0x0001, + VENDOR_NL80211_SUBCMD_RANGE_END = 0x0FFF, + + /* define all GScan related commands between 0x1000 and 0x10FF */ + ANDROID_NL80211_SUBCMD_GSCAN_RANGE_START = 0x1000, + ANDROID_NL80211_SUBCMD_GSCAN_RANGE_END = 0x10FF, + + /* define all NearbyDiscovery related commands between 0x1100 and 0x11FF */ + ANDROID_NL80211_SUBCMD_NBD_RANGE_START = 0x1100, + ANDROID_NL80211_SUBCMD_NBD_RANGE_END = 0x11FF, + + /* define all RTT related commands between 0x1100 and 0x11FF */ + ANDROID_NL80211_SUBCMD_RTT_RANGE_START = 0x1100, + ANDROID_NL80211_SUBCMD_RTT_RANGE_END = 0x11FF, + + ANDROID_NL80211_SUBCMD_LSTATS_RANGE_START = 0x1200, + ANDROID_NL80211_SUBCMD_LSTATS_RANGE_END = 0x12FF, + + /* define all Logger related commands between 0x1400 and 0x14FF */ + ANDROID_NL80211_SUBCMD_DEBUG_RANGE_START = 0x1400, + ANDROID_NL80211_SUBCMD_DEBUG_RANGE_END = 0x14FF, + + /* define all wifi offload related commands between 0x1600 and 0x16FF */ + ANDROID_NL80211_SUBCMD_WIFI_OFFLOAD_RANGE_START = 0x1600, + ANDROID_NL80211_SUBCMD_WIFI_OFFLOAD_RANGE_END = 0x16FF, + + /* define all NAN related commands between 0x1700 and 0x17FF */ + ANDROID_NL80211_SUBCMD_NAN_RANGE_START = 0x1700, + ANDROID_NL80211_SUBCMD_NAN_RANGE_END = 0x17FF, + + /* define all Android Packet Filter related commands between 0x1800 and 0x18FF */ + ANDROID_NL80211_SUBCMD_PKT_FILTER_RANGE_START = 0x1800, + ANDROID_NL80211_SUBCMD_PKT_FILTER_RANGE_END = 0x18FF, + + /* This is reserved for future usage */ + +} ANDROID_VENDOR_SUB_COMMAND; + +enum rtw_vendor_subcmd { + GSCAN_SUBCMD_GET_CAPABILITIES = ANDROID_NL80211_SUBCMD_GSCAN_RANGE_START, + + GSCAN_SUBCMD_SET_CONFIG, /* 0x1001 */ + + GSCAN_SUBCMD_SET_SCAN_CONFIG, /* 0x1002 */ + GSCAN_SUBCMD_ENABLE_GSCAN, /* 0x1003 */ + GSCAN_SUBCMD_GET_SCAN_RESULTS, /* 0x1004 */ + GSCAN_SUBCMD_SCAN_RESULTS, /* 0x1005 */ + + GSCAN_SUBCMD_SET_HOTLIST, /* 0x1006 */ + + GSCAN_SUBCMD_SET_SIGNIFICANT_CHANGE_CONFIG, /* 0x1007 */ + GSCAN_SUBCMD_ENABLE_FULL_SCAN_RESULTS, /* 0x1008 */ + GSCAN_SUBCMD_GET_CHANNEL_LIST, /* 0x1009 */ + + WIFI_SUBCMD_GET_FEATURE_SET, /* 0x100A */ + WIFI_SUBCMD_GET_FEATURE_SET_MATRIX, /* 0x100B */ + WIFI_SUBCMD_SET_PNO_RANDOM_MAC_OUI, /* 0x100C */ + WIFI_SUBCMD_NODFS_SET, /* 0x100D */ + WIFI_SUBCMD_SET_COUNTRY_CODE, /* 0x100E */ + /* Add more sub commands here */ + GSCAN_SUBCMD_SET_EPNO_SSID, /* 0x100F */ + + WIFI_SUBCMD_SET_SSID_WHITE_LIST, /* 0x1010 */ + WIFI_SUBCMD_SET_ROAM_PARAMS, /* 0x1011 */ + WIFI_SUBCMD_ENABLE_LAZY_ROAM, /* 0x1012 */ + WIFI_SUBCMD_SET_BSSID_PREF, /* 0x1013 */ + WIFI_SUBCMD_SET_BSSID_BLACKLIST, /* 0x1014 */ + + GSCAN_SUBCMD_ANQPO_CONFIG, /* 0x1015 */ + WIFI_SUBCMD_SET_RSSI_MONITOR, /* 0x1016 */ + WIFI_SUBCMD_CONFIG_ND_OFFLOAD, /* 0x1017 */ + /* Add more sub commands here */ + + GSCAN_SUBCMD_MAX, + + RTT_SUBCMD_SET_CONFIG = ANDROID_NL80211_SUBCMD_RTT_RANGE_START, + RTT_SUBCMD_CANCEL_CONFIG, + RTT_SUBCMD_GETCAPABILITY, + + APF_SUBCMD_GET_CAPABILITIES = ANDROID_NL80211_SUBCMD_PKT_FILTER_RANGE_START, + APF_SUBCMD_SET_FILTER, + + LOGGER_START_LOGGING = ANDROID_NL80211_SUBCMD_DEBUG_RANGE_START, + LOGGER_TRIGGER_MEM_DUMP, + LOGGER_GET_MEM_DUMP, + LOGGER_GET_VER, + LOGGER_GET_RING_STATUS, + LOGGER_GET_RING_DATA, + LOGGER_GET_FEATURE, + LOGGER_RESET_LOGGING, + LOGGER_TRIGGER_DRIVER_MEM_DUMP, + LOGGER_GET_DRIVER_MEM_DUMP, + LOGGER_START_PKT_FATE_MONITORING, + LOGGER_GET_TX_PKT_FATES, + LOGGER_GET_RX_PKT_FATES, + + WIFI_OFFLOAD_SUBCMD_START_MKEEP_ALIVE = ANDROID_NL80211_SUBCMD_WIFI_OFFLOAD_RANGE_START, + WIFI_OFFLOAD_SUBCMD_STOP_MKEEP_ALIVE, + + VENDOR_SUBCMD_MAX +}; + +enum gscan_attributes { + GSCAN_ATTRIBUTE_NUM_BUCKETS = 10, + GSCAN_ATTRIBUTE_BASE_PERIOD, + GSCAN_ATTRIBUTE_BUCKETS_BAND, + GSCAN_ATTRIBUTE_BUCKET_ID, + GSCAN_ATTRIBUTE_BUCKET_PERIOD, + GSCAN_ATTRIBUTE_BUCKET_NUM_CHANNELS, + GSCAN_ATTRIBUTE_BUCKET_CHANNELS, + GSCAN_ATTRIBUTE_NUM_AP_PER_SCAN, + GSCAN_ATTRIBUTE_REPORT_THRESHOLD, + GSCAN_ATTRIBUTE_NUM_SCANS_TO_CACHE, + GSCAN_ATTRIBUTE_BAND = GSCAN_ATTRIBUTE_BUCKETS_BAND, + + GSCAN_ATTRIBUTE_ENABLE_FEATURE = 20, + GSCAN_ATTRIBUTE_SCAN_RESULTS_COMPLETE, + GSCAN_ATTRIBUTE_FLUSH_FEATURE, + GSCAN_ATTRIBUTE_ENABLE_FULL_SCAN_RESULTS, + GSCAN_ATTRIBUTE_REPORT_EVENTS, + /* remaining reserved for additional attributes */ + GSCAN_ATTRIBUTE_NUM_OF_RESULTS = 30, + GSCAN_ATTRIBUTE_FLUSH_RESULTS, + GSCAN_ATTRIBUTE_SCAN_RESULTS, /* flat array of wifi_scan_result */ + GSCAN_ATTRIBUTE_SCAN_ID, /* indicates scan number */ + GSCAN_ATTRIBUTE_SCAN_FLAGS, /* indicates if scan was aborted */ + GSCAN_ATTRIBUTE_AP_FLAGS, /* flags on significant change event */ + GSCAN_ATTRIBUTE_NUM_CHANNELS, + GSCAN_ATTRIBUTE_CHANNEL_LIST, + + /* remaining reserved for additional attributes */ + + GSCAN_ATTRIBUTE_SSID = 40, + GSCAN_ATTRIBUTE_BSSID, + GSCAN_ATTRIBUTE_CHANNEL, + GSCAN_ATTRIBUTE_RSSI, + GSCAN_ATTRIBUTE_TIMESTAMP, + GSCAN_ATTRIBUTE_RTT, + GSCAN_ATTRIBUTE_RTTSD, + + /* remaining reserved for additional attributes */ + + GSCAN_ATTRIBUTE_HOTLIST_BSSIDS = 50, + GSCAN_ATTRIBUTE_RSSI_LOW, + GSCAN_ATTRIBUTE_RSSI_HIGH, + GSCAN_ATTRIBUTE_HOSTLIST_BSSID_ELEM, + GSCAN_ATTRIBUTE_HOTLIST_FLUSH, + + /* remaining reserved for additional attributes */ + GSCAN_ATTRIBUTE_RSSI_SAMPLE_SIZE = 60, + GSCAN_ATTRIBUTE_LOST_AP_SAMPLE_SIZE, + GSCAN_ATTRIBUTE_MIN_BREACHING, + GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_BSSIDS, + GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_FLUSH, + GSCAN_ATTRIBUTE_MAX +}; + +enum gscan_bucket_attributes { + GSCAN_ATTRIBUTE_CH_BUCKET_1, + GSCAN_ATTRIBUTE_CH_BUCKET_2, + GSCAN_ATTRIBUTE_CH_BUCKET_3, + GSCAN_ATTRIBUTE_CH_BUCKET_4, + GSCAN_ATTRIBUTE_CH_BUCKET_5, + GSCAN_ATTRIBUTE_CH_BUCKET_6, + GSCAN_ATTRIBUTE_CH_BUCKET_7 +}; + +enum gscan_ch_attributes { + GSCAN_ATTRIBUTE_CH_ID_1, + GSCAN_ATTRIBUTE_CH_ID_2, + GSCAN_ATTRIBUTE_CH_ID_3, + GSCAN_ATTRIBUTE_CH_ID_4, + GSCAN_ATTRIBUTE_CH_ID_5, + GSCAN_ATTRIBUTE_CH_ID_6, + GSCAN_ATTRIBUTE_CH_ID_7 +}; + +enum wifi_rssi_monitor_attr { + RSSI_MONITOR_ATTRIBUTE_MAX_RSSI, + RSSI_MONITOR_ATTRIBUTE_MIN_RSSI, + RSSI_MONITOR_ATTRIBUTE_START, +}; + + +enum rtt_attributes { + RTT_ATTRIBUTE_TARGET_CNT, + RTT_ATTRIBUTE_TARGET_INFO, + RTT_ATTRIBUTE_TARGET_MAC, + RTT_ATTRIBUTE_TARGET_TYPE, + RTT_ATTRIBUTE_TARGET_PEER, + RTT_ATTRIBUTE_TARGET_CHAN, + RTT_ATTRIBUTE_TARGET_MODE, + RTT_ATTRIBUTE_TARGET_INTERVAL, + RTT_ATTRIBUTE_TARGET_NUM_MEASUREMENT, + RTT_ATTRIBUTE_TARGET_NUM_PKT, + RTT_ATTRIBUTE_TARGET_NUM_RETRY +}; + +enum logger_attributes { + LOGGER_ATTRIBUTE_GET_DRIVER, + LOGGER_ATTRIBUTE_GET_FW, + LOGGER_ATTRIBUTE_RING_ID, + LOGGER_ATTRIBUTE_RING_NAME, + LOGGER_ATTRIBUTE_RING_FLAGS, + LOGGER_ATTRIBUTE_LOG_LEVEL, + LOGGER_ATTRIBUTE_LOG_TIME_INTVAL, + LOGGER_ATTRIBUTE_LOG_MIN_DATA_SIZE, + LOGGER_ATTRIBUTE_FW_DUMP_LEN, + LOGGER_ATTRIBUTE_FW_DUMP_DATA, + LOGGERG_ATTRIBUTE_RING_DATA, + LOGGER_ATTRIBUTE_RING_STATUS, + LOGGER_ATTRIBUTE_RING_NUM +}; +typedef enum rtw_vendor_event { + RTK_RESERVED1, + RTK_RESERVED2, + GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS , + GSCAN_EVENT_HOTLIST_RESULTS_FOUND, + GSCAN_EVENT_SCAN_RESULTS_AVAILABLE, + GSCAN_EVENT_FULL_SCAN_RESULTS, + RTT_EVENT_COMPLETE, + GSCAN_EVENT_COMPLETE_SCAN, + GSCAN_EVENT_HOTLIST_RESULTS_LOST, + GSCAN_EVENT_EPNO_EVENT, + GOOGLE_DEBUG_RING_EVENT, + GOOGLE_DEBUG_MEM_DUMP_EVENT, + GSCAN_EVENT_ANQPO_HOTSPOT_MATCH, + GOOGLE_RSSI_MONITOR_EVENT +} rtw_vendor_event_t; + +enum andr_wifi_feature_set_attr { + ANDR_WIFI_ATTRIBUTE_NUM_FEATURE_SET, + ANDR_WIFI_ATTRIBUTE_FEATURE_SET, + ANDR_WIFI_ATTRIBUTE_RANDOM_MAC_OUI, + ANDR_WIFI_ATTRIBUTE_NODFS_SET, + ANDR_WIFI_ATTRIBUTE_COUNTRY, + ANDR_WIFI_ATTRIBUTE_ND_OFFLOAD_VALUE + // Add more attribute here +}; + +typedef enum rtw_vendor_gscan_attribute { + ATTR_START_GSCAN, + ATTR_STOP_GSCAN, + ATTR_SET_SCAN_BATCH_CFG_ID, /* set batch scan params */ + ATTR_SET_SCAN_GEOFENCE_CFG_ID, /* set list of bssids to track */ + ATTR_SET_SCAN_SIGNIFICANT_CFG_ID, /* set list of bssids, rssi threshold etc.. */ + ATTR_SET_SCAN_CFG_ID, /* set common scan config params here */ + ATTR_GET_GSCAN_CAPABILITIES_ID, + /* Add more sub commands here */ + ATTR_GSCAN_MAX +} rtw_vendor_gscan_attribute_t; + +typedef enum gscan_batch_attribute { + ATTR_GSCAN_BATCH_BESTN, + ATTR_GSCAN_BATCH_MSCAN, + ATTR_GSCAN_BATCH_BUFFER_THRESHOLD +} gscan_batch_attribute_t; + +typedef enum gscan_geofence_attribute { + ATTR_GSCAN_NUM_HOTLIST_BSSID, + ATTR_GSCAN_HOTLIST_BSSID +} gscan_geofence_attribute_t; + +typedef enum gscan_complete_event { + WIFI_SCAN_BUFFER_FULL, + WIFI_SCAN_COMPLETE +} gscan_complete_event_t; +/* wifi_hal.h */ +/* WiFi Common definitions */ +typedef unsigned char byte; +typedef int wifi_request_id; +typedef int wifi_channel; // indicates channel frequency in MHz +typedef int wifi_rssi; +typedef byte mac_addr[6]; +typedef byte oui[3]; +typedef int64_t wifi_timestamp; // In microseconds (us) +typedef int64_t wifi_timespan; // In picoseconds (ps) + +struct wifi_info; +struct wifi_interface_info; +typedef struct wifi_info *wifi_handle; +typedef struct wifi_interface_info *wifi_interface_handle; + +/* channel operating width */ +typedef enum { + WIFI_CHAN_WIDTH_20 = 0, + WIFI_CHAN_WIDTH_40 = 1, + WIFI_CHAN_WIDTH_80 = 2, + WIFI_CHAN_WIDTH_160 = 3, + WIFI_CHAN_WIDTH_80P80 = 4, + WIFI_CHAN_WIDTH_5 = 5, + WIFI_CHAN_WIDTH_10 = 6, + WIFI_CHAN_WIDTH_INVALID = -1 +} wifi_channel_width; + +typedef int wifi_radio; + +typedef struct { + wifi_channel_width width; + int center_frequency0; + int center_frequency1; + int primary_frequency; +} wifi_channel_spec; + +typedef enum { + WIFI_SUCCESS = 0, + WIFI_ERROR_NONE = 0, + WIFI_ERROR_UNKNOWN = -1, + WIFI_ERROR_UNINITIALIZED = -2, + WIFI_ERROR_NOT_SUPPORTED = -3, + WIFI_ERROR_NOT_AVAILABLE = -4, // Not available right now, but try later + WIFI_ERROR_INVALID_ARGS = -5, + WIFI_ERROR_INVALID_REQUEST_ID = -6, + WIFI_ERROR_TIMED_OUT = -7, + WIFI_ERROR_TOO_MANY_REQUESTS = -8, // Too many instances of this request + WIFI_ERROR_OUT_OF_MEMORY = -9, + WIFI_ERROR_BUSY = -10, +} wifi_error; + +typedef int wifi_ring_buffer_id; +/* ring buffer params */ +/** + * written_bytes and read_bytes implement a producer consumer API + * hence written_bytes >= read_bytes + * a modulo arithmetic of the buffer size has to be applied to those counters: + * actual offset into ring buffer = written_bytes % ring_buffer_byte_size + * + */ +typedef struct { + u8 name[32]; + u32 flags; + wifi_ring_buffer_id ring_id; // unique integer representing the ring + u32 ring_buffer_byte_size; // total memory size allocated for the buffer + u32 verbose_level; // verbose level for ring buffer + u32 written_bytes; // number of bytes that was written to the buffer by driver, + // monotonously increasing integer + u32 read_bytes; // number of bytes that was read from the buffer by user land, + // monotonously increasing integer + u32 written_records; // number of records that was written to the buffer by driver, + // monotonously increasing integer +} wifi_ring_buffer_status; + +#ifdef CONFIG_RTW_CFGVENDOR_LLSTATS +#define STATS_MAJOR_VERSION 1 +#define STATS_MINOR_VERSION 0 +#define STATS_MICRO_VERSION 0 + +typedef enum { + WIFI_DISCONNECTED = 0, + WIFI_AUTHENTICATING = 1, + WIFI_ASSOCIATING = 2, + WIFI_ASSOCIATED = 3, + WIFI_EAPOL_STARTED = 4, // if done by firmware/driver + WIFI_EAPOL_COMPLETED = 5, // if done by firmware/driver +} wifi_connection_state; + +typedef enum { + WIFI_ROAMING_IDLE = 0, + WIFI_ROAMING_ACTIVE = 1, +} wifi_roam_state; + +typedef enum { + WIFI_INTERFACE_STA = 0, + WIFI_INTERFACE_SOFTAP = 1, + WIFI_INTERFACE_IBSS = 2, + WIFI_INTERFACE_P2P_CLIENT = 3, + WIFI_INTERFACE_P2P_GO = 4, + WIFI_INTERFACE_NAN = 5, + WIFI_INTERFACE_MESH = 6, + WIFI_INTERFACE_UNKNOWN = -1 + } wifi_interface_mode; + +#define WIFI_CAPABILITY_QOS 0x00000001 // set for QOS association +#define WIFI_CAPABILITY_PROTECTED 0x00000002 // set for protected association (802.11 beacon frame control protected bit set) +#define WIFI_CAPABILITY_INTERWORKING 0x00000004 // set if 802.11 Extended Capabilities element interworking bit is set +#define WIFI_CAPABILITY_HS20 0x00000008 // set for HS20 association +#define WIFI_CAPABILITY_SSID_UTF8 0x00000010 // set is 802.11 Extended Capabilities element UTF-8 SSID bit is set +#define WIFI_CAPABILITY_COUNTRY 0x00000020 // set is 802.11 Country Element is present + +typedef struct { + wifi_interface_mode mode; // interface mode + u8 mac_addr[6]; // interface mac address (self) + wifi_connection_state state; // connection state (valid for STA, CLI only) + wifi_roam_state roaming; // roaming state + u32 capabilities; // WIFI_CAPABILITY_XXX (self) + u8 ssid[33]; // null terminated SSID + u8 bssid[6]; // bssid + u8 ap_country_str[3]; // country string advertised by AP + u8 country_str[3]; // country string for this association +} wifi_interface_link_layer_info; + +/* channel information */ +typedef struct { + wifi_channel_width width; // channel width (20, 40, 80, 80+80, 160) + wifi_channel center_freq; // primary 20 MHz channel + wifi_channel center_freq0; // center frequency (MHz) first segment + wifi_channel center_freq1; // center frequency (MHz) second segment +} wifi_channel_info; + +/* wifi rate */ +typedef struct { + u32 preamble :3; // 0: OFDM, 1:CCK, 2:HT 3:VHT 4..7 reserved + u32 nss :2; // 0:1x1, 1:2x2, 3:3x3, 4:4x4 + u32 bw :3; // 0:20MHz, 1:40Mhz, 2:80Mhz, 3:160Mhz + u32 rateMcsIdx :8; // OFDM/CCK rate code would be as per ieee std in the units of 0.5mbps + // HT/VHT it would be mcs index + u32 reserved :16; // reserved + u32 bitrate; // units of 100 Kbps +} wifi_rate; + +/* channel statistics */ +typedef struct { + wifi_channel_info channel; // channel + u32 on_time; // msecs the radio is awake (32 bits number accruing over time) + u32 cca_busy_time; // msecs the CCA register is busy (32 bits number accruing over time) +} wifi_channel_stat; + +// Max number of tx power levels. The actual number vary per device and is specified by |num_tx_levels| +#define RADIO_STAT_MAX_TX_LEVELS 256 + +/* Internal radio statistics structure in the driver */ +typedef struct { + wifi_radio radio; // wifi radio (if multiple radio supported) + u32 on_time; // msecs the radio is awake (32 bits number accruing over time) + u32 tx_time; // msecs the radio is transmitting (32 bits number accruing over time) + u32 rx_time; // msecs the radio is in active receive (32 bits number accruing over time) + u32 on_time_scan; // msecs the radio is awake due to all scan (32 bits number accruing over time) + u32 on_time_nbd; // msecs the radio is awake due to NAN (32 bits number accruing over time) + u32 on_time_gscan; // msecs the radio is awake due to G?scan (32 bits number accruing over time) + u32 on_time_roam_scan; // msecs the radio is awake due to roam?scan (32 bits number accruing over time) + u32 on_time_pno_scan; // msecs the radio is awake due to PNO scan (32 bits number accruing over time) + u32 on_time_hs20; // msecs the radio is awake due to HS2.0 scans and GAS exchange (32 bits number accruing over time) + u32 num_channels; // number of channels + wifi_channel_stat channels[]; // channel statistics +} wifi_radio_stat_internal; + +/** + * Packet statistics reporting by firmware is performed on MPDU basi (i.e. counters increase by 1 for each MPDU) + * As well, "data packet" in associated comments, shall be interpreted as 802.11 data packet, + * that is, 802.11 frame control subtype == 2 and excluding management and control frames. + * + * As an example, in the case of transmission of an MSDU fragmented in 16 MPDUs which are transmitted + * OTA in a 16 units long a-mpdu, for which a block ack is received with 5 bits set: + * tx_mpdu : shall increase by 5 + * retries : shall increase by 16 + * tx_ampdu : shall increase by 1 + * data packet counters shall not increase regardless of the number of BAR potentially sent by device for this a-mpdu + * data packet counters shall not increase regardless of the number of BA received by device for this a-mpdu + * + * For each subsequent retransmission of the 11 remaining non ACK'ed mpdus + * (regardless of the fact that they are transmitted in a-mpdu or not) + * retries : shall increase by 1 + * + * If no subsequent BA or ACK are received from AP, until packet lifetime expires for those 11 packet that were not ACK'ed + * mpdu_lost : shall increase by 11 + */ + +/* per rate statistics */ +typedef struct { + wifi_rate rate; // rate information + u32 tx_mpdu; // number of successfully transmitted data pkts (ACK rcvd) + u32 rx_mpdu; // number of received data pkts + u32 mpdu_lost; // number of data packet losses (no ACK) + u32 retries; // total number of data pkt retries + u32 retries_short; // number of short data pkt retries + u32 retries_long; // number of long data pkt retries +} wifi_rate_stat; + +/* access categories */ +typedef enum { + WIFI_AC_VO = 0, + WIFI_AC_VI = 1, + WIFI_AC_BE = 2, + WIFI_AC_BK = 3, + WIFI_AC_MAX = 4, +} wifi_traffic_ac; + +/* wifi peer type */ +typedef enum +{ + WIFI_PEER_STA, + WIFI_PEER_AP, + WIFI_PEER_P2P_GO, + WIFI_PEER_P2P_CLIENT, + WIFI_PEER_NAN, + WIFI_PEER_TDLS, + WIFI_PEER_INVALID, +} wifi_peer_type; + +/* per peer statistics */ +typedef struct { + wifi_peer_type type; // peer type (AP, TDLS, GO etc.) + u8 peer_mac_address[6]; // mac address + u32 capabilities; // peer WIFI_CAPABILITY_XXX + u32 num_rate; // number of rates + wifi_rate_stat rate_stats[]; // per rate statistics, number of entries = num_rate +} wifi_peer_info; + +/* Per access category statistics */ +typedef struct { + wifi_traffic_ac ac; // access category (VI, VO, BE, BK) + u32 tx_mpdu; // number of successfully transmitted unicast data pkts (ACK rcvd) + u32 rx_mpdu; // number of received unicast data packets + u32 tx_mcast; // number of succesfully transmitted multicast data packets + // STA case: implies ACK received from AP for the unicast packet in which mcast pkt was sent + u32 rx_mcast; // number of received multicast data packets + u32 rx_ampdu; // number of received unicast a-mpdus; support of this counter is optional + u32 tx_ampdu; // number of transmitted unicast a-mpdus; support of this counter is optional + u32 mpdu_lost; // number of data pkt losses (no ACK) + u32 retries; // total number of data pkt retries + u32 retries_short; // number of short data pkt retries + u32 retries_long; // number of long data pkt retries + u32 contention_time_min; // data pkt min contention time (usecs) + u32 contention_time_max; // data pkt max contention time (usecs) + u32 contention_time_avg; // data pkt avg contention time (usecs) + u32 contention_num_samples; // num of data pkts used for contention statistics +} wifi_wmm_ac_stat; + +/* interface statistics */ +typedef struct { + wifi_interface_handle iface; // wifi interface + wifi_interface_link_layer_info info; // current state of the interface + u32 beacon_rx; // access point beacon received count from connected AP + u64 average_tsf_offset; // average beacon offset encountered (beacon_TSF - TBTT) + // The average_tsf_offset field is used so as to calculate the + // typical beacon contention time on the channel as well may be + // used to debug beacon synchronization and related power consumption issue + u32 leaky_ap_detected; // indicate that this AP typically leaks packets beyond the driver guard time. + u32 leaky_ap_avg_num_frames_leaked; // average number of frame leaked by AP after frame with PM bit set was ACK'ed by AP + u32 leaky_ap_guard_time; // guard time currently in force (when implementing IEEE power management based on + // frame control PM bit), How long driver waits before shutting down the radio and + // after receiving an ACK for a data frame with PM bit set) + u32 mgmt_rx; // access point mgmt frames received count from connected AP (including Beacon) + u32 mgmt_action_rx; // action frames received count + u32 mgmt_action_tx; // action frames transmit count + wifi_rssi rssi_mgmt; // access Point Beacon and Management frames RSSI (averaged) + wifi_rssi rssi_data; // access Point Data Frames RSSI (averaged) from connected AP + wifi_rssi rssi_ack; // access Point ACK RSSI (averaged) from connected AP + wifi_wmm_ac_stat ac[WIFI_AC_MAX]; // per ac data packet statistics + u32 num_peers; // number of peers + wifi_peer_info peer_info[]; // per peer statistics +} wifi_iface_stat; + +/* configuration params */ +typedef struct { + u32 mpdu_size_threshold; // threshold to classify the pkts as short or long + // packet size < mpdu_size_threshold => short + u32 aggressive_statistics_gathering; // set for field debug mode. Driver should collect all statistics regardless of performance impact. +} wifi_link_layer_params; + +#define RSSI_MONITOR_EVT_VERSION 1 +typedef struct { + u8 version; + s8 cur_rssi; + mac_addr BSSID; +} rssi_monitor_evt; + + +/* wifi statistics bitmap */ +#define WIFI_STATS_RADIO 0x00000001 // all radio statistics +#define WIFI_STATS_RADIO_CCA 0x00000002 // cca_busy_time (within radio statistics) +#define WIFI_STATS_RADIO_CHANNELS 0x00000004 // all channel statistics (within radio statistics) +#define WIFI_STATS_RADIO_SCAN 0x00000008 // all scan statistics (within radio statistics) +#define WIFI_STATS_IFACE 0x00000010 // all interface statistics +#define WIFI_STATS_IFACE_TXRATE 0x00000020 // all tx rate statistics (within interface statistics) +#define WIFI_STATS_IFACE_AC 0x00000040 // all ac statistics (within interface statistics) +#define WIFI_STATS_IFACE_CONTENTION 0x00000080 // all contention (min, max, avg) statistics (within ac statisctics) + +#endif /* CONFIG_RTW_CFGVENDOR_LLSTATS */ + + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)) || defined(RTW_VENDOR_EXT_SUPPORT) +extern int rtw_cfgvendor_attach(struct wiphy *wiphy); +extern int rtw_cfgvendor_detach(struct wiphy *wiphy); +extern int rtw_cfgvendor_send_async_event(struct wiphy *wiphy, + struct net_device *dev, int event_id, const void *data, int len); +#if defined(GSCAN_SUPPORT) && 0 +extern int rtw_cfgvendor_send_hotlist_event(struct wiphy *wiphy, + struct net_device *dev, void *data, int len, rtw_vendor_event_t event); +#endif +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)) || defined(RTW_VENDOR_EXT_SUPPORT) */ + +#ifdef CONFIG_RTW_CFGVENDOR_RSSIMONITOR +void rtw_cfgvendor_rssi_monitor_evt(_adapter *padapter); +#endif + +#ifdef CONFIG_RTW_CFGVENDOR_RANDOM_MAC_OUI +void rtw_hal_pno_random_gen_mac_addr(PADAPTER adapter); +#endif +#if defined(CONFIG_RTW_CFGVENDOR_RANDOM_MAC_OUI) || defined(CONFIG_RTW_SCAN_RAND) +void rtw_hal_set_hw_mac_addr(PADAPTER adapter, u8 *mac_addr); +#endif + + +#endif /* _RTW_CFGVENDOR_H_ */ diff --git a/os_dep/linux/rtw_proc.c b/os_dep/linux/rtw_proc.c new file mode 100644 index 0000000..e055f05 --- /dev/null +++ b/os_dep/linux/rtw_proc.c @@ -0,0 +1,6290 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2019 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ + +#include /* tolower() */ +#include +#include +#include "rtw_proc.h" +#include + +#ifdef CONFIG_PROC_DEBUG + +static struct proc_dir_entry *rtw_proc = NULL; + +inline struct proc_dir_entry *get_rtw_drv_proc(void) +{ + return rtw_proc; +} + +#define RTW_PROC_NAME DRV_NAME + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0)) +#define file_inode(file) ((file)->f_dentry->d_inode) +#endif + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)) +#define PDE_DATA(inode) PDE((inode))->data +#define proc_get_parent_data(inode) PDE((inode))->parent->data +#endif + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)) +#define get_proc_net proc_net +#else +#define get_proc_net init_net.proc_net +#endif + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)) +int single_open_size(struct file *file, int (*show)(struct seq_file *, void *), + void *data, size_t size) +{ + char *buf = kmalloc(size, GFP_KERNEL); + int ret; + if (!buf) + return -ENOMEM; + ret = single_open(file, show, data); + if (ret) { + kfree(buf); + return ret; + } + ((struct seq_file *)file->private_data)->buf = buf; + ((struct seq_file *)file->private_data)->size = size; + return 0; +} +#endif + +inline struct proc_dir_entry *rtw_proc_create_dir(const char *name, struct proc_dir_entry *parent, void *data) +{ + struct proc_dir_entry *entry; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)) + entry = proc_mkdir_data(name, S_IRUGO | S_IXUGO, parent, data); +#else + /* entry = proc_mkdir_mode(name, S_IRUGO|S_IXUGO, parent); */ + entry = proc_mkdir(name, parent); + if (entry) + entry->data = data; +#endif + + return entry; +} + +inline struct proc_dir_entry *rtw_proc_create_entry(const char *name, struct proc_dir_entry *parent, + const struct rtw_proc_ops *fops, void * data) +{ + struct proc_dir_entry *entry; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)) + entry = proc_create_data(name, S_IFREG | S_IRUGO | S_IWUGO, parent, fops, data); +#else + entry = create_proc_entry(name, S_IFREG | S_IRUGO | S_IWUGO, parent); + if (entry) { + entry->data = data; + entry->proc_fops = fops; + } +#endif + + return entry; +} + +static int proc_get_dummy(struct seq_file *m, void *v) +{ + return 0; +} + +static int proc_get_drv_version(struct seq_file *m, void *v) +{ + dump_drv_version(m); + return 0; +} + +static int proc_get_log_level(struct seq_file *m, void *v) +{ + dump_log_level(m); + return 0; +} + +static int proc_get_drv_cfg(struct seq_file *m, void *v) +{ + dump_drv_cfg(m); + return 0; +} + +static ssize_t proc_set_log_level(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + char tmp[32]; + int log_level; + + if (count < 1) + return -EINVAL; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + +#ifdef CONFIG_RTW_DEBUG + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%d ", &log_level); + + if (num == 1 && + log_level >= _DRV_NONE_ && log_level <= _DRV_MAX_) { + rtw_drv_log_level = log_level; + printk("rtw_drv_log_level:%d\n", rtw_drv_log_level); + } + } else + return -EFAULT; +#else + printk("CONFIG_RTW_DEBUG is disabled\n"); +#endif + + return count; +} + +#ifdef DBG_MEM_ALLOC +static int proc_get_mstat(struct seq_file *m, void *v) +{ + rtw_mstat_dump(m); + return 0; +} +#endif /* DBG_MEM_ALLOC */ + +static int proc_get_country_chplan_map(struct seq_file *m, void *v) +{ + dump_country_chplan_map(m); + return 0; +} + +static int proc_get_chplan_id_list(struct seq_file *m, void *v) +{ + dump_chplan_id_list(m); + return 0; +} + +#ifdef CONFIG_RTW_DEBUG +static int proc_get_chplan_test(struct seq_file *m, void *v) +{ + dump_chplan_test(m); + return 0; +} +#endif + +static int proc_get_chplan_ver(struct seq_file *m, void *v) +{ + dump_chplan_ver(m); + return 0; +} + +static int proc_get_global_op_class(struct seq_file *m, void *v) +{ + dump_global_op_class(m); + return 0; +} + +#ifdef CONFIG_RTW_DEBUG +static int proc_get_hw_rate_map_test(struct seq_file *m, void *v) +{ + dump_hw_rate_map_test(m); + return 0; +} +#endif + +#ifdef RTW_HALMAC +extern void rtw_halmac_get_version(char *str, u32 len); + +static int proc_get_halmac_info(struct seq_file *m, void *v) +{ + char ver[30] = {0}; + + + rtw_halmac_get_version(ver, 30); + RTW_PRINT_SEL(m, "version: %s\n", ver); + + return 0; +} +#endif + +/* +* rtw_drv_proc: +* init/deinit when register/unregister driver +*/ +const struct rtw_proc_hdl drv_proc_hdls[] = { + RTW_PROC_HDL_SSEQ("ver_info", proc_get_drv_version, NULL), + RTW_PROC_HDL_SSEQ("log_level", proc_get_log_level, proc_set_log_level), + RTW_PROC_HDL_SSEQ("drv_cfg", proc_get_drv_cfg, NULL), +#ifdef DBG_MEM_ALLOC + RTW_PROC_HDL_SSEQ("mstat", proc_get_mstat, NULL), +#endif /* DBG_MEM_ALLOC */ + RTW_PROC_HDL_SSEQ("country_chplan_map", proc_get_country_chplan_map, NULL), + RTW_PROC_HDL_SSEQ("chplan_id_list", proc_get_chplan_id_list, NULL), +#ifdef CONFIG_RTW_DEBUG + RTW_PROC_HDL_SSEQ("chplan_test", proc_get_chplan_test, NULL), +#endif + RTW_PROC_HDL_SSEQ("chplan_ver", proc_get_chplan_ver, NULL), + RTW_PROC_HDL_SSEQ("global_op_class", proc_get_global_op_class, NULL), +#ifdef CONFIG_RTW_DEBUG + RTW_PROC_HDL_SSEQ("hw_rate_map_test", proc_get_hw_rate_map_test, NULL), +#endif +#ifdef RTW_HALMAC + RTW_PROC_HDL_SSEQ("halmac_info", proc_get_halmac_info, NULL), +#endif /* RTW_HALMAC */ +}; + +const int drv_proc_hdls_num = sizeof(drv_proc_hdls) / sizeof(struct rtw_proc_hdl); + +static int rtw_drv_proc_open(struct inode *inode, struct file *file) +{ + /* struct net_device *dev = proc_get_parent_data(inode); */ + ssize_t index = (ssize_t)PDE_DATA(inode); + const struct rtw_proc_hdl *hdl = drv_proc_hdls + index; + void *private = NULL; + + if (hdl->type == RTW_PROC_HDL_TYPE_SEQ) { + int res = seq_open(file, hdl->u.seq_op); + + if (res == 0) + ((struct seq_file *)file->private_data)->private = private; + + return res; + } else if (hdl->type == RTW_PROC_HDL_TYPE_SSEQ) { + int (*show)(struct seq_file *, void *) = hdl->u.show ? hdl->u.show : proc_get_dummy; + + return single_open(file, show, private); + } else if (hdl->type == RTW_PROC_HDL_TYPE_SZSEQ) { + int (*show)(struct seq_file *, void *) = hdl->u.sz.show ? hdl->u.sz.show : proc_get_dummy; + + return single_open_size(file, show, private, hdl->u.sz.size); + } else { + return -EROFS; + } +} + +static ssize_t rtw_drv_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) +{ + ssize_t index = (ssize_t)PDE_DATA(file_inode(file)); + const struct rtw_proc_hdl *hdl = drv_proc_hdls + index; + ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *, void *) = hdl->write; + + if (write) + return write(file, buffer, count, pos, NULL); + + return -EROFS; +} + +static const struct rtw_proc_ops rtw_drv_proc_seq_fops = { +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)) + .proc_open = rtw_drv_proc_open, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_release = seq_release, + .proc_write = rtw_drv_proc_write, +#else + .owner = THIS_MODULE, + .open = rtw_drv_proc_open, + .read = seq_read, + .llseek = seq_lseek, + .release = seq_release, + .write = rtw_drv_proc_write, +#endif +}; + +static const struct rtw_proc_ops rtw_drv_proc_sseq_fops = { +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)) + .proc_open = rtw_drv_proc_open, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_release = single_release, + .proc_write = rtw_drv_proc_write, +#else + .owner = THIS_MODULE, + .open = rtw_drv_proc_open, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, + .write = rtw_drv_proc_write, +#endif +}; + +int rtw_drv_proc_init(void) +{ + int ret = _FAIL; + ssize_t i; + struct proc_dir_entry *entry = NULL; + + if (rtw_proc != NULL) { + rtw_warn_on(1); + goto exit; + } + + rtw_proc = rtw_proc_create_dir(RTW_PROC_NAME, get_proc_net, NULL); + + if (rtw_proc == NULL) { + rtw_warn_on(1); + goto exit; + } + + for (i = 0; i < drv_proc_hdls_num; i++) { + if (drv_proc_hdls[i].type == RTW_PROC_HDL_TYPE_SEQ) + entry = rtw_proc_create_entry(drv_proc_hdls[i].name, rtw_proc, &rtw_drv_proc_seq_fops, (void *)i); + else if (drv_proc_hdls[i].type == RTW_PROC_HDL_TYPE_SSEQ || + drv_proc_hdls[i].type == RTW_PROC_HDL_TYPE_SZSEQ) + entry = rtw_proc_create_entry(drv_proc_hdls[i].name, rtw_proc, &rtw_drv_proc_sseq_fops, (void *)i); + else + entry = NULL; + + if (!entry) { + rtw_warn_on(1); + goto exit; + } + } + + ret = _SUCCESS; + +exit: + return ret; +} + +void rtw_drv_proc_deinit(void) +{ + int i; + + if (rtw_proc == NULL) + return; + + for (i = 0; i < drv_proc_hdls_num; i++) + remove_proc_entry(drv_proc_hdls[i].name, rtw_proc); + + remove_proc_entry(RTW_PROC_NAME, get_proc_net); + rtw_proc = NULL; +} + +#ifndef RTW_SEQ_FILE_TEST +#define RTW_SEQ_FILE_TEST 0 +#endif + +#if RTW_SEQ_FILE_TEST +#define RTW_SEQ_FILE_TEST_SHOW_LIMIT 300 +static void *proc_start_seq_file_test(struct seq_file *m, loff_t *pos) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + RTW_PRINT(FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(adapter)); + if (*pos >= RTW_SEQ_FILE_TEST_SHOW_LIMIT) { + RTW_PRINT(FUNC_ADPT_FMT" pos:%llu, out of range return\n", FUNC_ADPT_ARG(adapter), *pos); + return NULL; + } + + RTW_PRINT(FUNC_ADPT_FMT" return pos:%lld\n", FUNC_ADPT_ARG(adapter), *pos); + return pos; +} +void proc_stop_seq_file_test(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + RTW_PRINT(FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(adapter)); +} + +void *proc_next_seq_file_test(struct seq_file *m, void *v, loff_t *pos) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + (*pos)++; + if (*pos >= RTW_SEQ_FILE_TEST_SHOW_LIMIT) { + RTW_PRINT(FUNC_ADPT_FMT" pos:%lld, out of range return\n", FUNC_ADPT_ARG(adapter), *pos); + return NULL; + } + + RTW_PRINT(FUNC_ADPT_FMT" return pos:%lld\n", FUNC_ADPT_ARG(adapter), *pos); + return pos; +} + +static int proc_get_seq_file_test(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + u32 pos = *((loff_t *)(v)); + RTW_PRINT(FUNC_ADPT_FMT" pos:%d\n", FUNC_ADPT_ARG(adapter), pos); + RTW_PRINT_SEL(m, FUNC_ADPT_FMT" pos:%d\n", FUNC_ADPT_ARG(adapter), pos); + return 0; +} + +struct seq_operations seq_file_test = { + .start = proc_start_seq_file_test, + .stop = proc_stop_seq_file_test, + .next = proc_next_seq_file_test, + .show = proc_get_seq_file_test, +}; +#endif /* RTW_SEQ_FILE_TEST */ + +#ifdef CONFIG_SDIO_HCI +static int proc_get_sd_f0_reg_dump(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + sd_f0_reg_dump(m, adapter); + + return 0; +} + +static int proc_get_sdio_local_reg_dump(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + sdio_local_reg_dump(m, adapter); + + return 0; +} +static int proc_get_sdio_card_info(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_sdio_card_info(m, adapter_to_dvobj(adapter)); + + return 0; +} + +#ifdef CONFIG_SDIO_RECVBUF_AGGREGATION +int proc_get_sdio_recvbuf_aggregation(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = GET_PRIMARY_ADAPTER((_adapter *)rtw_netdev_priv(dev)); + struct recv_priv *recvpriv = &adapter->recvpriv; + + RTW_PRINT_SEL(m, "%d\n", recvpriv->recvbuf_agg); + + return 0; +} + +ssize_t proc_set_sdio_recvbuf_aggregation(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = GET_PRIMARY_ADAPTER((_adapter *)rtw_netdev_priv(dev)); + struct recv_priv *recvpriv = &adapter->recvpriv; + + char tmp[32]; + u8 enable; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hhu", &enable); + + if (num >= 1) + recvpriv->recvbuf_agg = enable ? 1 : 0; + } + + return count; +} +#endif /* CONFIG_SDIO_RECVBUF_AGGREGATION */ + +#ifdef CONFIG_SDIO_RECVBUF_PWAIT +int proc_get_sdio_recvbuf_pwait(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = GET_PRIMARY_ADAPTER((_adapter *)rtw_netdev_priv(dev)); + struct recv_priv *recvpriv = &adapter->recvpriv; + + dump_recvbuf_pwait_conf(m, recvpriv); + + return 0; +} + +ssize_t proc_set_sdio_recvbuf_pwait(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ +#ifdef CONFIG_SDIO_RECVBUF_PWAIT_RUNTIME_ADJUST + struct net_device *dev = data; + _adapter *adapter = GET_PRIMARY_ADAPTER((_adapter *)rtw_netdev_priv(dev)); + struct recv_priv *recvpriv = &adapter->recvpriv; + + char tmp[64]; + char type[64]; + s32 time; + s32 cnt_lmt; + + if (count < 3) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + int num = sscanf(tmp, "%s %d %d", type, &time, &cnt_lmt); + int i; + + if (num < 3) + return -EINVAL; + + for (i = 0; i < RTW_PWAIT_TYPE_NUM; i++) + if (strncmp(_rtw_pwait_type_str[i], type, strlen(_rtw_pwait_type_str[i])) == 0) + break; + + if (i < RTW_PWAIT_TYPE_NUM && recvbuf_pwait_config_req(recvpriv, i, time, cnt_lmt) != _SUCCESS) + return -EINVAL; + } + return count; +#else + return -EFAULT; +#endif /* CONFIG_SDIO_RECVBUF_PWAIT_RUNTIME_ADJUST */ +} +#endif /* CONFIG_SDIO_RECVBUF_PWAIT */ + +#ifdef DBG_SDIO +static int proc_get_sdio_dbg(struct seq_file *m, void *v) +{ + struct net_device *dev; + struct _ADAPTER *a; + struct dvobj_priv *d; + struct sdio_data *sdio; + + + dev = m->private; + a = (struct _ADAPTER *)rtw_netdev_priv(dev); + d = adapter_to_dvobj(a); + sdio = &d->intf_data; + + dump_sdio_card_info(m, d); + + RTW_PRINT_SEL(m, "CMD52 error cnt: %d\n", sdio->cmd52_err_cnt); + RTW_PRINT_SEL(m, "CMD53 error cnt: %d\n", sdio->cmd53_err_cnt); + +#if (DBG_SDIO >= 3) + RTW_PRINT_SEL(m, "dbg: %s\n", sdio->dbg_enable?"enable":"disable"); + RTW_PRINT_SEL(m, "err_stop: %s\n", sdio->err_stop?"enable":"disable"); + RTW_PRINT_SEL(m, "err_test: %s\n", sdio->err_test?"enable":"disable"); + RTW_PRINT_SEL(m, "err_test_triggered: %s\n", + sdio->err_test_triggered?"yes":"no"); +#endif /* DBG_SDIO >= 3 */ + +#if (DBG_SDIO >= 2) + RTW_PRINT_SEL(m, "I/O error dump mark: %d\n", sdio->reg_dump_mark); + if (sdio->reg_dump_mark) { + if (sdio->dbg_msg) + RTW_PRINT_SEL(m, "debug messages: %s\n", sdio->dbg_msg); + if (sdio->reg_mac) + RTW_BUF_DUMP_SEL(_DRV_ALWAYS_, m, "MAC register:", + _TRUE, sdio->reg_mac, 0x800); + if (sdio->reg_mac_ext) + RTW_BUF_DUMP_SEL(_DRV_ALWAYS_, m, "MAC EXT register:", + _TRUE, sdio->reg_mac_ext, 0x800); + if (sdio->reg_local) + RTW_BUF_DUMP_SEL(_DRV_ALWAYS_, m, "SDIO Local register:", + _TRUE, sdio->reg_local, 0x100); + if (sdio->reg_cia) + RTW_BUF_DUMP_SEL(_DRV_ALWAYS_, m, "SDIO CIA register:", + _TRUE, sdio->reg_cia, 0x200); + } +#endif /* DBG_SDIO >= 2 */ + + return 0; +} + +#if (DBG_SDIO >= 2) +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 0, 0)) +#define strnicmp strncasecmp +#endif /* Linux kernel >= 4.0.0 */ +void rtw_sdio_dbg_reg_free(struct dvobj_priv *d); +#endif /* DBG_SDIO >= 2 */ + +ssize_t proc_set_sdio_dbg(struct file *file, const char __user *buffer, + size_t count, loff_t *pos, void *data) +{ +#if (DBG_SDIO >= 2) + struct net_device *dev = data; + struct dvobj_priv *d; + struct _ADAPTER *a; + struct sdio_data *sdio; + char tmp[32], cmd[32] = {0}; + int num; + + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + a = (struct _ADAPTER *)rtw_netdev_priv(dev); + d = adapter_to_dvobj(a); + sdio = &d->intf_data; + + if (buffer && !copy_from_user(tmp, buffer, count)) { + num = sscanf(tmp, "%s", cmd); + + if (num >= 1) { + if (strnicmp(cmd, "reg_reset", 10) == 0) { + sdio->reg_dump_mark = 0; + goto exit; + } + if (strnicmp(cmd, "reg_free", 9) == 0) { + rtw_sdio_dbg_reg_free(d); + sdio->reg_dump_mark = 0; + goto exit; + } +#if (DBG_SDIO >= 3) + if (strnicmp(cmd, "dbg_enable", 11) == 0) { + sdio->dbg_enable = 1; + goto exit; + } + if (strnicmp(cmd, "dbg_disable", 12) == 0) { + sdio->dbg_enable = 0; + goto exit; + } + if (strnicmp(cmd, "err_stop", 9) == 0) { + sdio->err_stop = 1; + goto exit; + } + if (strnicmp(cmd, "err_stop_disable", 16) == 0) { + sdio->err_stop = 0; + goto exit; + } + if (strnicmp(cmd, "err_test", 9) == 0) { + sdio->err_test_triggered = 0; + sdio->err_test = 1; + goto exit; + } +#endif /* DBG_SDIO >= 3 */ + } + + return -EINVAL; + } + +exit: +#endif /* DBG_SDIO >= 2 */ + return count; +} +#endif /* DBG_SDIO */ +#endif /* CONFIG_SDIO_HCI */ + +static int proc_get_fw_info(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + rtw_dump_fw_info(m, adapter); + return 0; +} +static int proc_get_mac_reg_dump(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + mac_reg_dump(m, adapter); + + return 0; +} + +static int proc_get_bb_reg_dump(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + bb_reg_dump(m, adapter); + + return 0; +} + +static int proc_get_bb_reg_dump_ex(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + bb_reg_dump_ex(m, adapter); + + return 0; +} + +static int proc_get_rf_reg_dump(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + rf_reg_dump(m, adapter); + + return 0; +} + +#ifdef CONFIG_RTW_LED +int proc_get_led_config(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_led_config(m, adapter); + + return 0; +} + +ssize_t proc_set_led_config(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + char tmp[32]; + u8 strategy; + u8 iface_en_mask; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hhu %hhx", &strategy, &iface_en_mask); + + if (num >= 1) + rtw_led_set_strategy(adapter, strategy); + if (num >= 2) + rtw_led_set_iface_en_mask(adapter, iface_en_mask); + } + + return count; +} +#endif /* CONFIG_RTW_LED */ + +#ifdef CONFIG_AP_MODE +int proc_get_aid_status(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_aid_status(m, adapter); + + return 0; +} + +ssize_t proc_set_aid_status(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct sta_priv *stapriv = &adapter->stapriv; + + char tmp[32]; + u8 rr; + u16 started_aid; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hhu %hu", &rr, &started_aid); + + if (num >= 1) + stapriv->rr_aid = rr ? 1 : 0; + if (num >= 2) { + started_aid = started_aid % (stapriv->max_aid + 1); + stapriv->started_aid = started_aid ? started_aid : 1; + } + } + + return count; +} + +int proc_get_ap_isolate(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + RTW_PRINT_SEL(m, "%d\n", adapter->mlmepriv.ap_isolate); + + return 0; +} + +ssize_t proc_set_ap_isolate(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + int ap_isolate; + int num = sscanf(tmp, "%d", &ap_isolate); + + if (num >= 1) + adapter->mlmepriv.ap_isolate = ap_isolate ? 1 : 0; + } + + return count; +} + +#if CONFIG_RTW_AP_DATA_BMC_TO_UC +static int proc_get_ap_b2u_flags(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = rtw_netdev_priv(dev); + + if (MLME_IS_AP(adapter)) + dump_ap_b2u_flags(m, adapter); + + return 0; +} + +static ssize_t proc_set_ap_b2u_flags(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = rtw_netdev_priv(dev); + char tmp[32]; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + u8 src, fwd; + int num = sscanf(tmp, "%hhx %hhx", &src, &fwd); + + if (num >= 1) + adapter->b2u_flags_ap_src = src; + if (num >= 2) + adapter->b2u_flags_ap_fwd = fwd; + } + + return count; +} +#endif /* CONFIG_RTW_AP_DATA_BMC_TO_UC */ +#endif /* CONFIG_AP_MODE */ + +static int proc_get_dump_tx_rate_bmp(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_tx_rate_bmp(m, adapter_to_dvobj(adapter)); + + return 0; +} + +static int proc_get_dump_adapters_status(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_adapters_status(m, adapter_to_dvobj(adapter)); + + return 0; +} + +#ifdef CONFIG_RTW_CUSTOMER_STR +static int proc_get_customer_str(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + u8 cstr[RTW_CUSTOMER_STR_LEN]; + + rtw_ps_deny(adapter, PS_DENY_IOCTL); + if (rtw_pwr_wakeup(adapter) == _FAIL) + goto exit; + + if (rtw_hal_customer_str_read(adapter, cstr) != _SUCCESS) + goto exit; + + RTW_PRINT_SEL(m, RTW_CUSTOMER_STR_FMT"\n", RTW_CUSTOMER_STR_ARG(cstr)); + +exit: + rtw_ps_deny_cancel(adapter, PS_DENY_IOCTL); + return 0; +} +#endif /* CONFIG_RTW_CUSTOMER_STR */ + +#ifdef CONFIG_SCAN_BACKOP +static int proc_get_backop_flags_sta(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_ext_priv *mlmeext = &adapter->mlmeextpriv; + + RTW_PRINT_SEL(m, "0x%02x\n", mlmeext_scan_backop_flags_sta(mlmeext)); + + return 0; +} + +static ssize_t proc_set_backop_flags_sta(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_ext_priv *mlmeext = &adapter->mlmeextpriv; + + char tmp[32]; + u8 flags; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hhx", &flags); + + if (num == 1) + mlmeext_assign_scan_backop_flags_sta(mlmeext, flags); + } + + return count; +} + +#ifdef CONFIG_AP_MODE +static int proc_get_backop_flags_ap(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_ext_priv *mlmeext = &adapter->mlmeextpriv; + + RTW_PRINT_SEL(m, "0x%02x\n", mlmeext_scan_backop_flags_ap(mlmeext)); + + return 0; +} + +static ssize_t proc_set_backop_flags_ap(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_ext_priv *mlmeext = &adapter->mlmeextpriv; + + char tmp[32]; + u8 flags; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hhx", &flags); + + if (num == 1) + mlmeext_assign_scan_backop_flags_ap(mlmeext, flags); + } + + return count; +} +#endif /* CONFIG_AP_MODE */ + +#ifdef CONFIG_RTW_MESH +static int proc_get_backop_flags_mesh(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_ext_priv *mlmeext = &adapter->mlmeextpriv; + + RTW_PRINT_SEL(m, "0x%02x\n", mlmeext_scan_backop_flags_mesh(mlmeext)); + + return 0; +} + +static ssize_t proc_set_backop_flags_mesh(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_ext_priv *mlmeext = &adapter->mlmeextpriv; + + char tmp[32]; + u8 flags; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hhx", &flags); + + if (num == 1) + mlmeext_assign_scan_backop_flags_mesh(mlmeext, flags); + } + + return count; +} +#endif /* CONFIG_RTW_MESH */ + +#endif /* CONFIG_SCAN_BACKOP */ + +#if defined(CONFIG_LPS_PG) && defined(CONFIG_RTL8822C) +static int proc_get_lps_pg_debug(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = rtw_netdev_priv(dev); + struct dm_struct *dm = adapter_to_phydm(adapter); + + rtw_run_in_thread_cmd(adapter, ((void *)(odm_lps_pg_debug_8822c)), dm); + + return 0; +} +#endif + +/* gpio setting */ +#ifdef CONFIG_GPIO_API +static ssize_t proc_set_config_gpio(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32] = {0}; + int num = 0, gpio_pin = 0, gpio_mode = 0; /* gpio_mode:0 input 1:output; */ + + if (count < 2) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + num = sscanf(tmp, "%d %d", &gpio_pin, &gpio_mode); + RTW_INFO("num=%d gpio_pin=%d mode=%d\n", num, gpio_pin, gpio_mode); + padapter->pre_gpio_pin = gpio_pin; + + if (gpio_mode == 0 || gpio_mode == 1) + rtw_hal_config_gpio(padapter, gpio_pin, gpio_mode); + } + return count; + +} +static ssize_t proc_set_gpio_output_value(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32] = {0}; + int num = 0, gpio_pin = 0, pin_mode = 0; /* pin_mode: 1 high 0:low */ + + if (count < 2) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + num = sscanf(tmp, "%d %d", &gpio_pin, &pin_mode); + RTW_INFO("num=%d gpio_pin=%d pin_high=%d\n", num, gpio_pin, pin_mode); + padapter->pre_gpio_pin = gpio_pin; + + if (pin_mode == 0 || pin_mode == 1) + rtw_hal_set_gpio_output_value(padapter, gpio_pin, pin_mode); + } + return count; +} +static int proc_get_gpio(struct seq_file *m, void *v) +{ + u8 gpioreturnvalue = 0; + struct net_device *dev = m->private; + + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + if (!padapter) + return -EFAULT; + gpioreturnvalue = rtw_hal_get_gpio(padapter, padapter->pre_gpio_pin); + RTW_PRINT_SEL(m, "get_gpio %d:%d\n", padapter->pre_gpio_pin, gpioreturnvalue); + + return 0; + +} +static ssize_t proc_set_gpio(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32] = {0}; + int num = 0, gpio_pin = 0; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + num = sscanf(tmp, "%d", &gpio_pin); + RTW_INFO("num=%d gpio_pin=%d\n", num, gpio_pin); + padapter->pre_gpio_pin = gpio_pin; + + } + return count; +} +#endif + +static ssize_t proc_set_rx_info_msg(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct recv_priv *precvpriv = &(padapter->recvpriv); + char tmp[32] = {0}; + int phy_info_flag = 0; + + if (!padapter) + return -EFAULT; + + if (count < 1) { + RTW_INFO("argument size is less than 1\n"); + return -EFAULT; + } + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + int num = sscanf(tmp, "%d", &phy_info_flag); + + if (num == 1) + precvpriv->store_law_data_flag = (BOOLEAN) phy_info_flag; + + /*RTW_INFO("precvpriv->store_law_data_flag = %d\n",( BOOLEAN )(precvpriv->store_law_data_flag));*/ + } + return count; +} +static int proc_get_rx_info_msg(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + rtw_hal_set_odm_var(padapter, HAL_ODM_RX_Dframe_INFO, m, _FALSE); + return 0; +} +static int proc_get_tx_info_msg(struct seq_file *m, void *v) +{ + _irqL irqL; + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct sta_info *psta; + u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; + u8 null_addr[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct sta_priv *pstapriv = &padapter->stapriv; + int i; + _list *plist, *phead; + u8 current_rate_id = 0, current_sgi = 0; + + char *BW, *status; + + _enter_critical_bh(&pstapriv->sta_hash_lock, &irqL); + + if (MLME_IS_STA(padapter)) + status = "station mode"; + else if (MLME_IS_AP(padapter)) + status = "AP mode"; + else if (MLME_IS_MESH(padapter)) + status = "mesh mode"; + else + status = " "; + _RTW_PRINT_SEL(m, "status=%s\n", status); + for (i = 0; i < NUM_STA; i++) { + phead = &(pstapriv->sta_hash[i]); + plist = get_next(phead); + + while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) { + + psta = LIST_CONTAINOR(plist, struct sta_info, hash_list); + + plist = get_next(plist); + + if ((_rtw_memcmp(psta->cmn.mac_addr, bc_addr, ETH_ALEN) != _TRUE) + && (_rtw_memcmp(psta->cmn.mac_addr, null_addr, ETH_ALEN) != _TRUE) + && (_rtw_memcmp(psta->cmn.mac_addr, adapter_mac_addr(padapter), ETH_ALEN) != _TRUE)) { + + switch (psta->cmn.bw_mode) { + + case CHANNEL_WIDTH_20: + BW = "20M"; + break; + + case CHANNEL_WIDTH_40: + BW = "40M"; + break; + + case CHANNEL_WIDTH_80: + BW = "80M"; + break; + + case CHANNEL_WIDTH_160: + BW = "160M"; + break; + + default: + BW = ""; + break; + } + current_rate_id = rtw_get_current_tx_rate(adapter, psta); + current_sgi = rtw_get_current_tx_sgi(adapter, psta); + + RTW_PRINT_SEL(m, "==============================\n"); + _RTW_PRINT_SEL(m, "macaddr=" MAC_FMT"\n", MAC_ARG(psta->cmn.mac_addr)); + _RTW_PRINT_SEL(m, "Tx_Data_Rate=%s\n", HDATA_RATE(current_rate_id)); + _RTW_PRINT_SEL(m, "BW=%s,sgi=%u\n", BW, current_sgi); + + } + } + } + + _exit_critical_bh(&pstapriv->sta_hash_lock, &irqL); + + return 0; + +} + + +static int proc_get_linked_info_dump(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + if (padapter) + RTW_PRINT_SEL(m, "linked_info_dump :%s\n", (padapter->bLinkInfoDump) ? "enable" : "disable"); + + return 0; +} + + +static ssize_t proc_set_linked_info_dump(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + char tmp[32] = {0}; + int mode = 0, pre_mode = 0; + int num = 0; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + pre_mode = padapter->bLinkInfoDump; + RTW_INFO("pre_mode=%d\n", pre_mode); + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + num = sscanf(tmp, "%d ", &mode); + RTW_INFO("num=%d mode=%d\n", num, mode); + + if (num != 1) { + RTW_INFO("argument number is wrong\n"); + return -EFAULT; + } + + if (mode == 1 || (mode == 0 && pre_mode == 1)) /* not consider pwr_saving 0: */ + padapter->bLinkInfoDump = mode; + + else if ((mode == 2) || (mode == 0 && pre_mode == 2)) { /* consider power_saving */ + /* RTW_INFO("linked_info_dump =%s\n", (padapter->bLinkInfoDump)?"enable":"disable") */ + linked_info_dump(padapter, mode); + } + } + return count; +} + + +static int proc_get_sta_tp_dump(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + if (padapter) + RTW_PRINT_SEL(m, "sta_tp_dump :%s\n", (padapter->bsta_tp_dump) ? "enable" : "disable"); + + return 0; +} + +static ssize_t proc_set_sta_tp_dump(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + char tmp[32] = {0}; + int mode = 0; + int num = 0; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + num = sscanf(tmp, "%d ", &mode); + + if (num != 1) { + RTW_INFO("argument number is wrong\n"); + return -EFAULT; + } + if (padapter) + padapter->bsta_tp_dump = mode; + } + return count; +} + +static int proc_get_sta_tp_info(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + if (padapter) + rtw_sta_traffic_info(m, padapter); + + return 0; +} + +static int proc_get_turboedca_ctrl(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(padapter); + + if (hal_data) { + + u32 edca_param; + + if (hal_data->dis_turboedca == 0) + RTW_PRINT_SEL(m, "Turbo-EDCA : %s\n", "Enable"); + else + RTW_PRINT_SEL(m, "Turbo-EDCA : %s, mode=%d, edca_param_mode=0x%x\n", "Disable", hal_data->dis_turboedca, hal_data->edca_param_mode); + + + rtw_hal_get_hwreg(padapter, HW_VAR_AC_PARAM_BE, (u8 *)(&edca_param)); + + _RTW_PRINT_SEL(m, "PARAM_BE:0x%x\n", edca_param); + + } + + return 0; +} + +static ssize_t proc_set_turboedca_ctrl(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(padapter); + char tmp[32] = {0}; + int mode = 0, num = 0; + u32 param_mode = 0; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) + return -EFAULT; + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + num = sscanf(tmp, "%d %x", &mode, ¶m_mode); + + if (num < 1 || num > 2) { + RTW_INFO("argument number is wrong\n"); + return -EFAULT; + } + + /* 0: enable turboedca, + 1: disable turboedca, + 2: disable turboedca and setting EDCA parameter based on the input parameter + > 2 : currently reset to 0 */ + + if (mode > 2) + mode = 0; + + hal_data->dis_turboedca = mode; + + hal_data->edca_param_mode = 0; /* init. value */ + + RTW_INFO("dis_turboedca mode = 0x%x\n", hal_data->dis_turboedca); + + if (num == 2) { + + hal_data->edca_param_mode = param_mode; + + RTW_INFO("param_mode = 0x%x\n", param_mode); + } + + } + + return count; + +} + +static int proc_get_mac_qinfo(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + rtw_hal_get_hwreg(adapter, HW_VAR_DUMP_MAC_QUEUE_INFO, (u8 *)m); + + return 0; +} + +int proc_get_wifi_spec(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct registry_priv *pregpriv = &padapter->registrypriv; + + RTW_PRINT_SEL(m, "wifi_spec=%d\n", pregpriv->wifi_spec); + return 0; +} + +static int proc_get_chan_plan(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_cur_chset(m, adapter_to_rfctl(adapter)); + return 0; +} + +static ssize_t proc_set_chan_plan(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + u8 chan_plan = RTW_CHPLAN_UNSPECIFIED; + + if (!padapter) + return -EFAULT; + + if (count < 1) { + RTW_INFO("argument size is less than 1\n"); + return -EFAULT; + } + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + int num = sscanf(tmp, "%hhx", &chan_plan); + if (num != 1) + return count; + } + + rtw_set_channel_plan(padapter, chan_plan); + + return count; +} + +static int proc_get_country_code(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + + if (rfctl->country_ent) + dump_country_chplan(m, rfctl->country_ent); + else + RTW_PRINT_SEL(m, "unspecified\n"); + + return 0; +} + +static ssize_t proc_set_country_code(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + char alpha2[2]; + int num; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (!buffer || copy_from_user(tmp, buffer, count)) + goto exit; + + num = sscanf(tmp, "%c%c", &alpha2[0], &alpha2[1]); + if (num != 2) + return count; + + rtw_set_country(padapter, alpha2); + +exit: + return count; +} + +static int cap_spt_op_class_ch_detail = 0; + +static int proc_get_cap_spt_op_class_ch(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_cap_spt_op_class_ch(m , adapter_to_rfctl(adapter), cap_spt_op_class_ch_detail); + return 0; +} + +static ssize_t proc_set_cap_spt_op_class_ch(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + int num; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (!buffer || copy_from_user(tmp, buffer, count)) + goto exit; + + num = sscanf(tmp, "%d", &cap_spt_op_class_ch_detail); + +exit: + return count; +} + +static int reg_spt_op_class_ch_detail = 0; + +static int proc_get_reg_spt_op_class_ch(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_reg_spt_op_class_ch(m , adapter_to_rfctl(adapter), reg_spt_op_class_ch_detail); + return 0; +} + +static ssize_t proc_set_reg_spt_op_class_ch(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + int num; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (!buffer || copy_from_user(tmp, buffer, count)) + goto exit; + + num = sscanf(tmp, "%d", ®_spt_op_class_ch_detail); + +exit: + return count; +} + +static int cur_spt_op_class_ch_detail = 0; + +static int proc_get_cur_spt_op_class_ch(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_cur_spt_op_class_ch(m , adapter_to_rfctl(adapter), cur_spt_op_class_ch_detail); + return 0; +} + +static ssize_t proc_set_cur_spt_op_class_ch(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + int num; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (!buffer || copy_from_user(tmp, buffer, count)) + goto exit; + + num = sscanf(tmp, "%d", &cur_spt_op_class_ch_detail); + +exit: + return count; +} + +#if CONFIG_RTW_MACADDR_ACL +static int proc_get_macaddr_acl(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_macaddr_acl(m, adapter); + return 0; +} + +ssize_t proc_set_macaddr_acl(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[17 * NUM_ACL + 32] = {0}; + u8 period; + char cmd[32]; + u8 mode; + u8 addr[ETH_ALEN]; + +#define MAC_ACL_CMD_MODE 0 +#define MAC_ACL_CMD_ADD 1 +#define MAC_ACL_CMD_DEL 2 +#define MAC_ACL_CMD_CLR 3 +#define MAC_ACL_CMD_NUM 4 + + static const char * const mac_acl_cmd_str[] = { + "mode", + "add", + "del", + "clr", + }; + u8 cmd_id = MAC_ACL_CMD_NUM; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + /* + * mode [] + * mode + * add [] + * del [] + * clr + */ + char *c, *next; + int i; + u8 is_bcast; + + next = tmp; + c = strsep(&next, " \t"); + if (!c || sscanf(c, "%hhu", &period) != 1) + goto exit; + + if (period >= RTW_ACL_PERIOD_NUM) { + RTW_WARN(FUNC_ADPT_FMT" invalid period:%u", FUNC_ADPT_ARG(adapter), period); + goto exit; + } + + c = strsep(&next, " \t"); + if (!c || sscanf(c, "%s", cmd) != 1) + goto exit; + + for (i = 0; i < MAC_ACL_CMD_NUM; i++) + if (strcmp(mac_acl_cmd_str[i], cmd) == 0) + cmd_id = i; + + switch (cmd_id) { + case MAC_ACL_CMD_MODE: + c = strsep(&next, " \t"); + if (!c || sscanf(c, "%hhu", &mode) != 1) + goto exit; + + if (mode >= RTW_ACL_MODE_MAX) { + RTW_WARN(FUNC_ADPT_FMT" invalid mode:%u", FUNC_ADPT_ARG(adapter), mode); + goto exit; + } + break; + + case MAC_ACL_CMD_ADD: + case MAC_ACL_CMD_DEL: + break; + + case MAC_ACL_CMD_CLR: + /* clear settings */ + rtw_macaddr_acl_clear(adapter, period); + goto exit; + + default: + RTW_WARN(FUNC_ADPT_FMT" invalid cmd:\"%s\"", FUNC_ADPT_ARG(adapter), cmd); + goto exit; + } + + /* check for macaddr list */ + c = strsep(&next, " \t"); + if (!c && cmd_id == MAC_ACL_CMD_MODE) { + /* set mode only */ + rtw_set_macaddr_acl(adapter, period, mode); + goto exit; + } + + if (cmd_id == MAC_ACL_CMD_MODE) { + /* set mode and entire macaddr list */ + rtw_macaddr_acl_clear(adapter, period); + rtw_set_macaddr_acl(adapter, period, mode); + } + + while (c != NULL) { + if (sscanf(c, MAC_SFMT, MAC_SARG(addr)) != 6) + break; + + is_bcast = is_broadcast_mac_addr(addr); + if (is_bcast + || rtw_check_invalid_mac_address(addr, 0) == _FALSE + ) { + if (cmd_id == MAC_ACL_CMD_DEL) { + rtw_acl_remove_sta(adapter, period, addr); + if (is_bcast) + break; + } else if (!is_bcast) + rtw_acl_add_sta(adapter, period, addr); + } + + c = strsep(&next, " \t"); + } + } + +exit: + return count; +} +#endif /* CONFIG_RTW_MACADDR_ACL */ + +#if CONFIG_RTW_PRE_LINK_STA +static int proc_get_pre_link_sta(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_pre_link_sta_ctl(m, &adapter->stapriv); + return 0; +} + +ssize_t proc_set_pre_link_sta(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct mlme_priv *mlme = &adapter->mlmepriv; + struct mlme_ext_priv *mlmeext = &adapter->mlmeextpriv; + char tmp[17 * RTW_PRE_LINK_STA_NUM + 32] = {0}; + char arg0[16] = {0}; + u8 addr[ETH_ALEN]; + +#define PRE_LINK_STA_CMD_RESET 0 +#define PRE_LINK_STA_CMD_ADD 1 +#define PRE_LINK_STA_CMD_DEL 2 +#define PRE_LINK_STA_CMD_NUM 3 + + static const char * const pre_link_sta_cmd_str[] = { + "reset", + "add", + "del" + }; + u8 cmd_id = PRE_LINK_STA_CMD_NUM; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + /* cmd [] */ + char *c, *next; + int i; + + next = tmp; + c = strsep(&next, " \t"); + + if (sscanf(c, "%s", arg0) != 1) + goto exit; + + for (i = 0; i < PRE_LINK_STA_CMD_NUM; i++) + if (strcmp(pre_link_sta_cmd_str[i], arg0) == 0) + cmd_id = i; + + switch (cmd_id) { + case PRE_LINK_STA_CMD_RESET: + rtw_pre_link_sta_ctl_reset(&adapter->stapriv); + goto exit; + case PRE_LINK_STA_CMD_ADD: + case PRE_LINK_STA_CMD_DEL: + break; + default: + goto exit; + } + + /* macaddr list */ + c = strsep(&next, " \t"); + while (c != NULL) { + if (sscanf(c, MAC_SFMT, MAC_SARG(addr)) != 6) + break; + + if (rtw_check_invalid_mac_address(addr, 0) == _FALSE) { + if (cmd_id == PRE_LINK_STA_CMD_ADD) + rtw_pre_link_sta_add(&adapter->stapriv, addr); + else + rtw_pre_link_sta_del(&adapter->stapriv, addr); + } + + c = strsep(&next, " \t"); + } + } + +exit: + return count; +} +#endif /* CONFIG_RTW_PRE_LINK_STA */ + +static int proc_get_ch_sel_policy(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + + RTW_PRINT_SEL(m, "%-16s\n", "within_same_band"); + + RTW_PRINT_SEL(m, "%16d\n", rfctl->ch_sel_within_same_band); + + return 0; +} + +static ssize_t proc_set_ch_sel_policy(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + char tmp[32]; + u8 within_sb; + int num; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (!buffer || copy_from_user(tmp, buffer, count)) + goto exit; + + num = sscanf(tmp, "%hhu", &within_sb); + if (num >= 1) + rfctl->ch_sel_within_same_band = within_sb ? 1 : 0; + +exit: + return count; +} + +#ifdef CONFIG_DFS_MASTER +static int proc_get_dfs_test_case(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + + RTW_PRINT_SEL(m, "%-24s %-19s\n", "radar_detect_trigger_non", "choose_dfs_ch_first"); + RTW_PRINT_SEL(m, "%24hhu %19hhu\n" + , rfctl->dbg_dfs_radar_detect_trigger_non + , rfctl->dbg_dfs_choose_dfs_ch_first + ); + + return 0; +} + +static ssize_t proc_set_dfs_test_case(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + char tmp[32]; + u8 radar_detect_trigger_non; + u8 choose_dfs_ch_first; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + int num = sscanf(tmp, "%hhu %hhu", &radar_detect_trigger_non, &choose_dfs_ch_first); + + if (num >= 1) + rfctl->dbg_dfs_radar_detect_trigger_non = radar_detect_trigger_non; + if (num >= 2) + rfctl->dbg_dfs_choose_dfs_ch_first = choose_dfs_ch_first; + } + + return count; +} + +ssize_t proc_set_update_non_ocp(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + char tmp[32]; + u8 ch, bw = CHANNEL_WIDTH_20, offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + int ms = -1; + bool updated = 0; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hhu %hhu %hhu %d", &ch, &bw, &offset, &ms); + + if (num < 1 || (bw != CHANNEL_WIDTH_20 && num < 3)) + goto exit; + + if (bw == CHANNEL_WIDTH_20) + updated = rtw_chset_update_non_ocp_ms(rfctl->channel_set + , ch, bw, HAL_PRIME_CHNL_OFFSET_DONT_CARE, ms); + else + updated = rtw_chset_update_non_ocp_ms(rfctl->channel_set + , ch, bw, offset, ms); + + if (updated) { + u8 cch = rtw_get_center_ch(ch, bw, offset); + + rtw_nlrtw_nop_start_event(adapter, cch, bw); + } + } + +exit: + return count; +} + +ssize_t proc_set_radar_detect(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + char tmp[32]; + u8 fake_radar_detect_cnt = 0; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hhu", &fake_radar_detect_cnt); + + if (num < 1) + goto exit; + + rfctl->dbg_dfs_fake_radar_detect_cnt = fake_radar_detect_cnt; + } + +exit: + return count; +} + +static int proc_get_dfs_ch_sel_e_flags(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + + RTW_PRINT_SEL(m, "0x%02x\n", rfctl->dfs_ch_sel_e_flags); + + return 0; +} + +static ssize_t proc_set_dfs_ch_sel_e_flags(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + char tmp[32]; + u8 e_flags; + int num; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (!buffer || copy_from_user(tmp, buffer, count)) + goto exit; + + num = sscanf(tmp, "%hhx", &e_flags); + if (num != 1) + goto exit; + + rfctl->dfs_ch_sel_e_flags = e_flags; + +exit: + return count; +} + +static int proc_get_dfs_ch_sel_d_flags(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + + RTW_PRINT_SEL(m, "0x%02x\n", rfctl->dfs_ch_sel_d_flags); + + return 0; +} + +static ssize_t proc_set_dfs_ch_sel_d_flags(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + char tmp[32]; + u8 d_flags; + int num; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (!buffer || copy_from_user(tmp, buffer, count)) + goto exit; + + num = sscanf(tmp, "%hhx", &d_flags); + if (num != 1) + goto exit; + + rfctl->dfs_ch_sel_d_flags = d_flags; + +exit: + return count; +} + +#if CONFIG_DFS_SLAVE_WITH_RADAR_DETECT +static int proc_get_dfs_slave_with_rd(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + + RTW_PRINT_SEL(m, "%u\n", rfctl->dfs_slave_with_rd); + + return 0; +} + +static ssize_t proc_set_dfs_slave_with_rd(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + char tmp[32]; + u8 rd; + int num; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (!buffer || copy_from_user(tmp, buffer, count)) + goto exit; + + num = sscanf(tmp, "%hhu", &rd); + if (num != 1) + goto exit; + + rd = rd ? 1 : 0; + + if (rfctl->dfs_slave_with_rd != rd) { + rfctl->dfs_slave_with_rd = rd; + rtw_dfs_rd_en_decision_cmd(adapter); + } + +exit: + return count; +} +#endif /* CONFIG_DFS_SLAVE_WITH_RADAR_DETECT */ +#endif /* CONFIG_DFS_MASTER */ + +#ifdef CONFIG_80211N_HT +int proc_get_rx_ampdu_size_limit(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_regsty_rx_ampdu_size_limit(m, adapter); + + return 0; +} + +ssize_t proc_set_rx_ampdu_size_limit(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct registry_priv *regsty = adapter_to_regsty(adapter); + char tmp[32]; + u8 nss; + u8 limit_by_bw[4] = {0xFF}; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + int i; + int num = sscanf(tmp, "%hhu %hhu %hhu %hhu %hhu" + , &nss, &limit_by_bw[0], &limit_by_bw[1], &limit_by_bw[2], &limit_by_bw[3]); + + if (num < 2) + goto exit; + if (nss == 0 || nss > 4) + goto exit; + + for (i = 0; i < num - 1; i++) + regsty->rx_ampdu_sz_limit_by_nss_bw[nss - 1][i] = limit_by_bw[i]; + + rtw_rx_ampdu_apply(adapter); + } + +exit: + return count; +} +#endif /* CONFIG_80211N_HT */ + +static int proc_get_rx_chk_limit(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + RTW_PRINT_SEL(m, "Rx chk limit : %d\n", rtw_get_rx_chk_limit(padapter)); + + return 0; +} + +static ssize_t proc_set_rx_chk_limit(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + char tmp[32]; + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + int rx_chk_limit; + + if (count < 1) { + RTW_INFO("argument size is less than 1\n"); + return -EFAULT; + } + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + int num = sscanf(tmp, "%d", &rx_chk_limit); + + rtw_set_rx_chk_limit(padapter, rx_chk_limit); + } + + return count; +} + +static int proc_get_udpport(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct recv_priv *precvpriv = &(padapter->recvpriv); + + RTW_PRINT_SEL(m, "%d\n", precvpriv->sink_udpport); + return 0; +} +static ssize_t proc_set_udpport(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct recv_priv *precvpriv = &(padapter->recvpriv); + int sink_udpport = 0; + char tmp[32]; + + + if (!padapter) + return -EFAULT; + + if (count < 1) { + RTW_INFO("argument size is less than 1\n"); + return -EFAULT; + } + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%d", &sink_udpport); + + if (num != 1) { + RTW_INFO("invalid input parameter number!\n"); + return count; + } + + } + precvpriv->sink_udpport = sink_udpport; + + return count; + +} + +static int proc_get_mi_ap_bc_info(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct macid_ctl_t *macid_ctl = dvobj_to_macidctl(dvobj); + u8 i; + + for (i = 0; i < dvobj->iface_nums; i++) + RTW_PRINT_SEL(m, "iface_id:%d, mac_id && sec_cam_id = %d\n", i, macid_ctl->iface_bmc[i]); + + return 0; +} +static int proc_get_macid_info(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct macid_ctl_t *macid_ctl = dvobj_to_macidctl(dvobj); + u8 i; + u8 null_addr[ETH_ALEN] = {0}; + u8 *macaddr; + + RTW_PRINT_SEL(m, "max_num:%u\n", macid_ctl->num); + RTW_PRINT_SEL(m, "\n"); + + RTW_PRINT_SEL(m, "used:\n"); + dump_macid_map(m, &macid_ctl->used, macid_ctl->num); + RTW_PRINT_SEL(m, "\n"); + + RTW_PRINT_SEL(m, "%-3s %-3s %-5s %-4s %-17s %-6s %-3s" + , "id", "bmc", "ifbmp", "ch_g", "macaddr", "bw", "vht"); + + if (GET_HAL_TX_NSS(adapter) > 2) + _RTW_PRINT_SEL(m, " %-10s", "rate_bmp1"); + + _RTW_PRINT_SEL(m, " %-10s %s\n", "rate_bmp0", "status"); + + for (i = 0; i < macid_ctl->num; i++) { + if (rtw_macid_is_used(macid_ctl, i) + || macid_ctl->h2c_msr[i] + ) { + if (macid_ctl->sta[i]) + macaddr = macid_ctl->sta[i]->cmn.mac_addr; + else + macaddr = null_addr; + + RTW_PRINT_SEL(m, "%3u %3u 0x%02x %4d "MAC_FMT" %6s %3u" + , i + , rtw_macid_is_bmc(macid_ctl, i) + , rtw_macid_get_iface_bmp(macid_ctl, i) + , rtw_macid_get_ch_g(macid_ctl, i) + , MAC_ARG(macaddr) + , ch_width_str(macid_ctl->bw[i]) + , macid_ctl->vht_en[i] + ); + + if (GET_HAL_TX_NSS(adapter) > 2) + _RTW_PRINT_SEL(m, " 0x%08X", macid_ctl->rate_bmp1[i]); + + _RTW_PRINT_SEL(m, " 0x%08X "H2C_MSR_FMT" %s\n" + , macid_ctl->rate_bmp0[i] + , H2C_MSR_ARG(&macid_ctl->h2c_msr[i]) + , rtw_macid_is_used(macid_ctl, i) ? "" : "[unused]" + ); + } + } + RTW_PRINT_SEL(m, "\n"); + + for (i = 0; i < H2C_MSR_ROLE_MAX; i++) { + if (macid_ctl->op_num[i]) { + RTW_PRINT_SEL(m, "%-5s op_num:%u\n" + , h2c_msr_role_str(i), macid_ctl->op_num[i]); + } + } + + return 0; +} + +static int proc_get_sec_cam(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct cam_ctl_t *cam_ctl = &dvobj->cam_ctl; + + RTW_PRINT_SEL(m, "sec_cap:0x%02x\n", cam_ctl->sec_cap); + RTW_PRINT_SEL(m, "flags:0x%08x\n", cam_ctl->flags); + RTW_PRINT_SEL(m, "\n"); + + RTW_PRINT_SEL(m, "max_num:%u\n", cam_ctl->num); + RTW_PRINT_SEL(m, "used:\n"); + dump_sec_cam_map(m, &cam_ctl->used, cam_ctl->num); + RTW_PRINT_SEL(m, "\n"); + + RTW_PRINT_SEL(m, "reg_scr:0x%04x\n", rtw_read16(adapter, 0x680)); + RTW_PRINT_SEL(m, "\n"); + + dump_sec_cam(m, adapter); + + return 0; +} + +static ssize_t proc_set_sec_cam(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct cam_ctl_t *cam_ctl = &dvobj->cam_ctl; + char tmp[32] = {0}; + char cmd[4]; + u8 id_1 = 0, id_2 = 0; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + /* c : clear specific cam entry */ + /* wfc : write specific cam entry from cam cache */ + /* sw : sec_cam 1/2 swap */ + + int num = sscanf(tmp, "%s %hhu %hhu", cmd, &id_1, &id_2); + + if (num < 2) + return count; + + if ((id_1 >= cam_ctl->num) || (id_2 >= cam_ctl->num)) { + RTW_ERR(FUNC_ADPT_FMT" invalid id_1:%u id_2:%u\n", FUNC_ADPT_ARG(adapter), id_1, id_2); + return count; + } + + if (strcmp("c", cmd) == 0) { + _clear_cam_entry(adapter, id_1); + adapter->securitypriv.hw_decrypted = _FALSE; /* temporarily set this for TX path to use SW enc */ + } else if (strcmp("wfc", cmd) == 0) + write_cam_from_cache(adapter, id_1); + else if (strcmp("sw", cmd) == 0) + rtw_sec_cam_swap(adapter, id_1, id_2); + else if (strcmp("cdk", cmd) == 0) + rtw_clean_dk_section(adapter); +#ifdef DBG_SEC_CAM_MOVE + else if (strcmp("sgd", cmd) == 0) + rtw_hal_move_sta_gk_to_dk(adapter); + else if (strcmp("rsd", cmd) == 0) + rtw_hal_read_sta_dk_key(adapter, id_1); +#endif + } + + return count; +} + +static int proc_get_sec_cam_cache(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_sec_cam_cache(m, adapter); + return 0; +} + +#ifdef CONFIG_AP_MODE +static ssize_t proc_set_change_bss_chbw(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + int i; + char tmp[32]; + s16 ch; + s8 bw = REQ_BW_NONE, offset = REQ_OFFSET_NONE; + u8 ifbmp = 0; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hd %hhd %hhd %hhx", &ch, &bw, &offset, &ifbmp); + + if (num < 1 || (bw != CHANNEL_WIDTH_20 && num < 3)) + goto exit; + + if (num < 4) + ifbmp = BIT(adapter->iface_id); + else + ifbmp &= (1 << dvobj->iface_nums) - 1; + + for (i = 0; i < dvobj->iface_nums; i++) { + if (!(ifbmp & BIT(i)) || !dvobj->padapters[i]) + continue; + + if (!CHK_MLME_STATE(dvobj->padapters[i], WIFI_AP_STATE | WIFI_MESH_STATE) + || !MLME_IS_ASOC(dvobj->padapters[i])) + ifbmp &= ~BIT(i); + } + + if (ifbmp) + rtw_change_bss_chbw_cmd(adapter, RTW_CMDF_WAIT_ACK, ifbmp, 0, ch, bw, offset); + } + +exit: + return count; +} +#endif + +#if CONFIG_TX_AC_LIFETIME +static int proc_get_tx_aclt_force_val(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = rtw_netdev_priv(dev); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + + dump_tx_aclt_force_val(m, dvobj); + + return 0; +} + +static ssize_t proc_set_tx_aclt_force_val(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = rtw_netdev_priv(dev); + char tmp[32] = {0}; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct tx_aclt_conf_t input; + int num = sscanf(tmp, "%hhx %u %u", &input.en, &input.vo_vi, &input.be_bk); + + if (num < 1) + return count; + + rtw_hal_set_tx_aclt_force_val(adapter, &input, num); + rtw_run_in_thread_cmd(adapter, ((void *)(rtw_hal_update_tx_aclt)), adapter); + } + + return count; +} + +static int proc_get_tx_aclt_flags(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = rtw_netdev_priv(dev); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + + RTW_PRINT_SEL(m, "0x%02x\n", dvobj->tx_aclt_flags); + + return 0; +} + +static ssize_t proc_set_tx_aclt_flags(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = rtw_netdev_priv(dev); + char tmp[32] = {0}; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + u8 flags; + int num = sscanf(tmp, "%hhx", &flags); + + if (num < 1) + return count; + + if (dvobj->tx_aclt_flags == flags) + return count; + + dvobj->tx_aclt_flags = flags; + + rtw_run_in_thread_cmd(adapter, ((void *)(rtw_hal_update_tx_aclt)), adapter); + } + + return count; +} + +static int proc_get_tx_aclt_confs(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = rtw_netdev_priv(dev); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + + RTW_PRINT_SEL(m, "flags:0x%02x\n", dvobj->tx_aclt_flags); + dump_tx_aclt_confs(m, dvobj); + + return 0; +} + +static ssize_t proc_set_tx_aclt_confs(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = rtw_netdev_priv(dev); + char tmp[32] = {0}; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + u8 id; + struct tx_aclt_conf_t input; + int num = sscanf(tmp, "%hhu %hhx %u %u", &id, &input.en, &input.vo_vi, &input.be_bk); + + if (num < 2) + return count; + + rtw_hal_set_tx_aclt_conf(adapter, id, &input, num - 1); + rtw_run_in_thread_cmd(adapter, ((void *)(rtw_hal_update_tx_aclt)), adapter); + } + + return count; +} +#endif /* CONFIG_TX_AC_LIFETIME */ + +static int proc_get_tx_bw_mode(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + RTW_PRINT_SEL(m, "0x%02x\n", adapter->driver_tx_bw_mode); + RTW_PRINT_SEL(m, "2.4G:%s\n", ch_width_str(ADAPTER_TX_BW_2G(adapter))); + RTW_PRINT_SEL(m, "5G:%s\n", ch_width_str(ADAPTER_TX_BW_5G(adapter))); + + return 0; +} + +static void rtw_set_tx_bw_mode(struct _ADAPTER *adapter, u8 bw_mode) +{ + struct mlme_ext_priv *mlmeext = &(adapter->mlmeextpriv); + struct macid_ctl_t *macid_ctl = &adapter->dvobj->macid_ctl; + u8 update = _FALSE; + + if ((MLME_STATE(adapter) & WIFI_ASOC_STATE) + && ((mlmeext->cur_channel <= 14 && BW_MODE_2G(bw_mode) != ADAPTER_TX_BW_2G(adapter)) + || (mlmeext->cur_channel >= 36 && BW_MODE_5G(bw_mode) != ADAPTER_TX_BW_5G(adapter))) + ) { + /* RA mask update needed */ + update = _TRUE; + } + adapter->driver_tx_bw_mode = bw_mode; + + if (update == _TRUE) { + struct sta_info *sta; + int i; + + for (i = 0; i < MACID_NUM_SW_LIMIT; i++) { + sta = macid_ctl->sta[i]; + if (sta && !is_broadcast_mac_addr(sta->cmn.mac_addr)) + rtw_dm_ra_mask_wk_cmd(adapter, (u8 *)sta); + } + } +} + +static ssize_t proc_set_tx_bw_mode(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + u8 bw_mode; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hhx", &bw_mode); + + if (num < 1 || bw_mode == adapter->driver_tx_bw_mode) + goto exit; + + rtw_set_tx_bw_mode(adapter, bw_mode); + } + +exit: + return count; +} + +static int proc_get_hal_txpwr_info(struct seq_file *m, void *v) +{ +#ifdef CONFIG_TXPWR_PG_WITH_PWR_IDX + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + + if (hal_data->txpwr_pg_mode == TXPWR_PG_WITH_PWR_IDX) { + if (hal_is_band_support(adapter, BAND_ON_2_4G)) + dump_hal_txpwr_info_2g(m, adapter, hal_spec->rfpath_num_2g, hal_data->max_tx_cnt); + + #if CONFIG_IEEE80211_BAND_5GHZ + if (hal_is_band_support(adapter, BAND_ON_5G)) + dump_hal_txpwr_info_5g(m, adapter, hal_spec->rfpath_num_5g, hal_data->max_tx_cnt); + #endif + } +#endif + + return 0; +} + +static int proc_get_target_tx_power(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_target_tx_power(m, adapter); + + return 0; +} + +static int proc_get_tx_power_by_rate(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_tx_power_by_rate(m, adapter); + + return 0; +} + +#if CONFIG_TXPWR_LIMIT +static int proc_get_tx_power_limit(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_txpwr_lmt(m, adapter); + + return 0; +} +#endif /* CONFIG_TXPWR_LIMIT */ + +static int proc_get_tpc_settings(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_txpwr_tpc_settings(m, adapter); + + return 0; +} + +static ssize_t proc_set_tpc_settings(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + + char tmp[32] = {0}; + u8 mode; + u16 m_constraint; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hhu %hu", &mode, &m_constraint); + + if (num < 1) + return count; + + if (mode >= TPC_MODE_INVALID) + return count; + + if (mode == TPC_MODE_MANUAL && num >= 2) + rfctl->tpc_manual_constraint = rtw_min(m_constraint, TPC_MANUAL_CONSTRAINT_MAX); + rfctl->tpc_mode = mode; + + if (rtw_get_hw_init_completed(adapter)) + rtw_run_in_thread_cmd_wait(adapter, ((void *)(rtw_hal_update_txpwr_level)), adapter, 2000); + } + + return count; +} + +static int proc_get_antenna_gain(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_txpwr_antenna_gain(m, adapter); + + return 0; +} + +static ssize_t proc_set_antenna_gain(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + + char tmp[32] = {0}; + s16 gain; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hd", &gain); + + if (num < 1) + return count; + + rfctl->antenna_gain = gain; + + if (rtw_get_hw_init_completed(adapter)) + rtw_run_in_thread_cmd_wait(adapter, ((void *)(rtw_hal_update_txpwr_level)), adapter, 2000); + } + + return count; +} + +static int proc_get_tx_power_ext_info(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_tx_power_ext_info(m, adapter); + + return 0; +} + +static ssize_t proc_set_tx_power_ext_info(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + char tmp[32] = {0}; + char cmd[16] = {0}; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%s", cmd); + + if (num < 1) + return count; + + #ifdef CONFIG_LOAD_PHY_PARA_FROM_FILE + phy_free_filebuf_mask(adapter, LOAD_BB_PG_PARA_FILE | LOAD_RF_TXPWR_LMT_PARA_FILE); + #endif + + rtw_ps_deny(adapter, PS_DENY_IOCTL); + if (rtw_pwr_wakeup(adapter) == _FALSE) + goto clear_ps_deny; + + if (strcmp("default", cmd) == 0) + rtw_run_in_thread_cmd(adapter, ((void *)(phy_reload_default_tx_power_ext_info)), adapter); + else + rtw_run_in_thread_cmd(adapter, ((void *)(phy_reload_tx_power_ext_info)), adapter); + + rtw_run_in_thread_cmd_wait(adapter, ((void *)(rtw_hal_update_txpwr_level)), adapter, 2000); + +clear_ps_deny: + rtw_ps_deny_cancel(adapter, PS_DENY_IOCTL); + } + + return count; +} + +static void *proc_start_tx_power_idx(struct seq_file *m, loff_t *pos) +{ + u8 path = ((*pos) & 0xFF00) >> 8; + + if (path >= RF_PATH_MAX) + return NULL; + + return pos; +} +static void proc_stop_tx_power_idx(struct seq_file *m, void *v) +{ +} + +static void *proc_next_tx_power_idx(struct seq_file *m, void *v, loff_t *pos) +{ + u8 path = ((*pos) & 0xFF00) >> 8; + u8 rs = *pos & 0xFF; + + rs++; + if (rs >= RATE_SECTION_NUM) { + rs = 0; + path++; + } + + if (path >= RF_PATH_MAX) + return NULL; + + *pos = (path << 8) | rs; + + return pos; +} + +static int proc_get_tx_power_idx(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + u32 pos = *((loff_t *)(v)); + u8 path = (pos & 0xFF00) >> 8; + u8 rs = pos & 0xFF; + enum channel_width bw = hal_data->current_channel_bw; + u8 cch = hal_data->current_channel; + + if (0) + RTW_INFO("%s path=%u, rs=%u\n", __func__, path, rs); + + if (path == RF_PATH_A && rs == CCK) + dump_tx_power_idx_title(m, adapter, bw, cch, 0); + dump_tx_power_idx_by_path_rs(m, adapter, path, rs, bw, cch, 0); + + return 0; +} + +static struct seq_operations seq_ops_tx_power_idx = { + .start = proc_start_tx_power_idx, + .stop = proc_stop_tx_power_idx, + .next = proc_next_tx_power_idx, + .show = proc_get_tx_power_idx, +}; + +static ssize_t proc_set_tx_power_idx_dump(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + char tmp[32] = {0}; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + u8 ch, bw, offset; + u8 cch; + + int num = sscanf(tmp, "%hhu %hhu %hhu", &ch, &bw, &offset); + + if (num < 3) + return count; + + cch = rtw_get_center_ch(ch, bw, offset); + dump_tx_power_idx(RTW_DBGDUMP, adapter, bw, cch, ch); + } + + return count; +} + +static void *proc_start_txpwr_total_dbm(struct seq_file *m, loff_t *pos) +{ + u8 rs = *pos; + + if (rs >= RATE_SECTION_NUM) + return NULL; + + return pos; +} + +static void proc_stop_txpwr_total_dbm(struct seq_file *m, void *v) +{ +} + +static void *proc_next_txpwr_total_dbm(struct seq_file *m, void *v, loff_t *pos) +{ + u8 rs = *pos; + + rs++; + if (rs >= RATE_SECTION_NUM) + return NULL; + + *pos = rs; + + return pos; +} + +static int proc_get_txpwr_total_dbm(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + u32 pos = *((loff_t *)(v)); + u8 rs = pos; + enum channel_width bw = hal_data->current_channel_bw; + u8 cch = hal_data->current_channel; + + if (rs == CCK) + dump_txpwr_total_dbm_title(m, adapter, bw, cch, 0); + dump_txpwr_total_dbm_by_rs(m, adapter, rs, bw, cch, 0); + + return 0; +} + +static struct seq_operations seq_ops_txpwr_total_dbm = { + .start = proc_start_txpwr_total_dbm, + .stop = proc_stop_txpwr_total_dbm, + .next = proc_next_txpwr_total_dbm, + .show = proc_get_txpwr_total_dbm, +}; + +static ssize_t proc_set_txpwr_total_dbm_dump(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + char tmp[32] = {0}; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + u8 ch, bw, offset; + u8 cch; + + int num = sscanf(tmp, "%hhu %hhu %hhu", &ch, &bw, &offset); + + if (num < 3) + return count; + + cch = rtw_get_center_ch(ch, bw, offset); + dump_txpwr_total_dbm(RTW_DBGDUMP, adapter, bw, cch, ch); + } + + return count; +} + +#ifdef CONFIG_RF_POWER_TRIM +static int proc_get_kfree_flag(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct kfree_data_t *kfree_data = GET_KFREE_DATA(adapter); + + RTW_PRINT_SEL(m, "0x%02x\n", kfree_data->flag); + + return 0; +} + +static ssize_t proc_set_kfree_flag(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct kfree_data_t *kfree_data = GET_KFREE_DATA(adapter); + char tmp[32] = {0}; + u8 flag; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hhx", &flag); + + if (num < 1) + return count; + + kfree_data->flag = flag; + } + + return count; +} + +static int proc_get_kfree_bb_gain(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + struct kfree_data_t *kfree_data = GET_KFREE_DATA(adapter); + u8 i, j; + + for (i = 0; i < BB_GAIN_NUM; i++) { + + if (i == 0) + _RTW_PRINT_SEL(m, "2G: "); +#if CONFIG_IEEE80211_BAND_5GHZ + switch (i) { + case 1: + _RTW_PRINT_SEL(m, "5GLB1: "); + break; + case 2: + _RTW_PRINT_SEL(m, "5GLB2: "); + break; + case 3: + _RTW_PRINT_SEL(m, "5GMB1: "); + break; + case 4: + _RTW_PRINT_SEL(m, "5GMB2: "); + break; + case 5: + _RTW_PRINT_SEL(m, "5GHB: "); + break; + } +#endif + for (j = 0; j < hal_spec->rf_reg_path_num; j++) + _RTW_PRINT_SEL(m, "%d ", kfree_data->bb_gain[i][j]); + _RTW_PRINT_SEL(m, "\n"); + } + + return 0; +} + +static ssize_t proc_set_kfree_bb_gain(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct kfree_data_t *kfree_data = GET_KFREE_DATA(adapter); + char tmp[BB_GAIN_NUM * RF_PATH_MAX] = {0}; + u8 chidx; + s8 bb_gain[BB_GAIN_NUM]; + char ch_band_Group[6]; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + char *c, *next; + int i = 0; + + next = tmp; + c = strsep(&next, " \t"); + + if (sscanf(c, "%s", ch_band_Group) != 1) { + RTW_INFO("Error Head Format, channel Group select\n,Please input:\t 2G , 5GLB1 , 5GLB2 , 5GMB1 , 5GMB2 , 5GHB\n"); + return count; + } + if (strcmp("2G", ch_band_Group) == 0) + chidx = BB_GAIN_2G; +#if CONFIG_IEEE80211_BAND_5GHZ + else if (strcmp("5GLB1", ch_band_Group) == 0) + chidx = BB_GAIN_5GLB1; + else if (strcmp("5GLB2", ch_band_Group) == 0) + chidx = BB_GAIN_5GLB2; + else if (strcmp("5GMB1", ch_band_Group) == 0) + chidx = BB_GAIN_5GMB1; + else if (strcmp("5GMB2", ch_band_Group) == 0) + chidx = BB_GAIN_5GMB2; + else if (strcmp("5GHB", ch_band_Group) == 0) + chidx = BB_GAIN_5GHB; +#endif /*CONFIG_IEEE80211_BAND_5GHZ*/ + else { + RTW_INFO("Error Head Format, channel Group select\n,Please input:\t 2G , 5GLB1 , 5GLB2 , 5GMB1 , 5GMB2 , 5GHB\n"); + return count; + } + c = strsep(&next, " \t"); + + while (c != NULL) { + if (sscanf(c, "%hhx", &bb_gain[i]) != 1) + break; + + kfree_data->bb_gain[chidx][i] = bb_gain[i]; + RTW_INFO("%s,kfree_data->bb_gain[%d][%d]=%x\n", __func__, chidx, i, kfree_data->bb_gain[chidx][i]); + + c = strsep(&next, " \t"); + i++; + } + + } + + return count; + +} + +static int proc_get_kfree_thermal(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct kfree_data_t *kfree_data = GET_KFREE_DATA(adapter); + + _RTW_PRINT_SEL(m, "%d\n", kfree_data->thermal); + + return 0; +} + +static ssize_t proc_set_kfree_thermal(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct kfree_data_t *kfree_data = GET_KFREE_DATA(adapter); + char tmp[32] = {0}; + s8 thermal; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hhd", &thermal); + + if (num < 1) + return count; + + kfree_data->thermal = thermal; + } + + return count; +} + +static ssize_t proc_set_tx_gain_offset(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter; + char tmp[32] = {0}; + u8 rf_path; + s8 offset; + + adapter = (_adapter *)rtw_netdev_priv(dev); + if (!adapter) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + int num = sscanf(tmp, "%hhu %hhd", &rf_path, &offset); + + if (num < 2) + return count; + + RTW_INFO("write rf_path:%u tx gain offset:%d\n", rf_path, offset); + rtw_rf_set_tx_gain_offset(adapter, rf_path, offset); + } + + return count; +} +#endif /* CONFIG_RF_POWER_TRIM */ + +#ifdef CONFIG_BT_COEXIST +ssize_t proc_set_btinfo_evt(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + u8 btinfo[8]; + + if (count < 6) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + int num = 0; + + _rtw_memset(btinfo, 0, 8); + + num = sscanf(tmp, "%hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx" + , &btinfo[0], &btinfo[1], &btinfo[2], &btinfo[3] + , &btinfo[4], &btinfo[5], &btinfo[6], &btinfo[7]); + + if (num < 6) + return -EINVAL; + + btinfo[1] = num - 2; + + rtw_btinfo_cmd(padapter, btinfo, btinfo[1] + 2); + } + + return count; +} + +static u8 btreg_read_type = 0; +static u16 btreg_read_addr = 0; +static int btreg_read_error = 0; +static u8 btreg_write_type = 0; +static u16 btreg_write_addr = 0; +static int btreg_write_error = 0; + +static u8 *btreg_type[] = { + "rf", + "modem", + "bluewize", + "vendor", + "le" +}; + +static int btreg_parse_str(char const *input, u8 *type, u16 *addr, u16 *val) +{ + u32 num; + u8 str[80] = {0}; + u8 t = 0; + u32 a, v; + u8 i, n; + + + num = sscanf(input, "%s %x %x", str, &a, &v); + if (num < 2) { + RTW_INFO("%s: INVALID input!(%s)\n", __FUNCTION__, input); + return -EINVAL; + } + if ((num < 3) && val) { + RTW_INFO("%s: INVALID input!(%s)\n", __FUNCTION__, input); + return -EINVAL; + } + + n = sizeof(btreg_type) / sizeof(btreg_type[0]); + for (i = 0; i < n; i++) { + if (!strcasecmp(str, btreg_type[i])) { + t = i; + break; + } + } + if (i == n) { + RTW_INFO("%s: unknown type(%s)!\n", __FUNCTION__, str); + return -EINVAL; + } + + switch (t) { + case 0: + /* RF */ + if (a & 0xFFFFFF80) { + RTW_INFO("%s: INVALID address(0x%X) for type %s(%d)!\n", + __FUNCTION__, a, btreg_type[t], t); + return -EINVAL; + } + break; + case 1: + /* Modem */ + if (a & 0xFFFFFE00) { + RTW_INFO("%s: INVALID address(0x%X) for type %s(%d)!\n", + __FUNCTION__, a, btreg_type[t], t); + return -EINVAL; + } + break; + default: + /* Others(Bluewize, Vendor, LE) */ + if (a & 0xFFFFF000) { + RTW_INFO("%s: INVALID address(0x%X) for type %s(%d)!\n", + __FUNCTION__, a, btreg_type[t], t); + return -EINVAL; + } + break; + } + + if (val) { + if (v & 0xFFFF0000) { + RTW_INFO("%s: INVALID value(0x%x)!\n", __FUNCTION__, v); + return -EINVAL; + } + *val = (u16)v; + } + + *type = (u8)t; + *addr = (u16)a; + + return 0; +} + +int proc_get_btreg_read(struct seq_file *m, void *v) +{ + struct net_device *dev; + PADAPTER padapter; + u16 ret; + u32 data; + + + if (btreg_read_error) + return btreg_read_error; + + dev = m->private; + padapter = (PADAPTER)rtw_netdev_priv(dev); + + ret = rtw_btcoex_btreg_read(padapter, btreg_read_type, btreg_read_addr, &data); + if (CHECK_STATUS_CODE_FROM_BT_MP_OPER_RET(ret, BT_STATUS_BT_OP_SUCCESS)) + RTW_PRINT_SEL(m, "BTREG read: (%s)0x%04X = 0x%08x\n", btreg_type[btreg_read_type], btreg_read_addr, data); + else + RTW_PRINT_SEL(m, "BTREG read: (%s)0x%04X read fail. error code = 0x%04x.\n", btreg_type[btreg_read_type], btreg_read_addr, ret); + + return 0; +} + +ssize_t proc_set_btreg_read(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + PADAPTER padapter; + u8 tmp[80] = {0}; + u32 num; + int err; + + + padapter = (PADAPTER)rtw_netdev_priv(dev); + + if (NULL == buffer) { + RTW_INFO(FUNC_ADPT_FMT ": input buffer is NULL!\n", + FUNC_ADPT_ARG(padapter)); + err = -EFAULT; + goto exit; + } + + if (count < 1) { + RTW_INFO(FUNC_ADPT_FMT ": input length is 0!\n", + FUNC_ADPT_ARG(padapter)); + err = -EFAULT; + goto exit; + } + + num = count; + if (num > (sizeof(tmp) - 1)) + num = (sizeof(tmp) - 1); + + if (copy_from_user(tmp, buffer, num)) { + RTW_INFO(FUNC_ADPT_FMT ": copy buffer from user space FAIL!\n", + FUNC_ADPT_ARG(padapter)); + err = -EFAULT; + goto exit; + } + /* [Coverity] sure tmp end with '\0'(string terminal) */ + tmp[sizeof(tmp) - 1] = 0; + + err = btreg_parse_str(tmp, &btreg_read_type, &btreg_read_addr, NULL); + if (err) + goto exit; + + RTW_INFO(FUNC_ADPT_FMT ": addr=(%s)0x%X\n", + FUNC_ADPT_ARG(padapter), btreg_type[btreg_read_type], btreg_read_addr); + +exit: + btreg_read_error = err; + + return count; +} + +int proc_get_btreg_write(struct seq_file *m, void *v) +{ + struct net_device *dev; + PADAPTER padapter; + u16 ret; + u32 data; + + + if (btreg_write_error < 0) + return btreg_write_error; + else if (btreg_write_error > 0) { + RTW_PRINT_SEL(m, "BTREG write: (%s)0x%04X write fail. error code = 0x%04x.\n", btreg_type[btreg_write_type], btreg_write_addr, btreg_write_error); + return 0; + } + + dev = m->private; + padapter = (PADAPTER)rtw_netdev_priv(dev); + + ret = rtw_btcoex_btreg_read(padapter, btreg_write_type, btreg_write_addr, &data); + if (CHECK_STATUS_CODE_FROM_BT_MP_OPER_RET(ret, BT_STATUS_BT_OP_SUCCESS)) + RTW_PRINT_SEL(m, "BTREG read: (%s)0x%04X = 0x%08x\n", btreg_type[btreg_write_type], btreg_write_addr, data); + else + RTW_PRINT_SEL(m, "BTREG read: (%s)0x%04X read fail. error code = 0x%04x.\n", btreg_type[btreg_write_type], btreg_write_addr, ret); + + return 0; +} + +ssize_t proc_set_btreg_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + PADAPTER padapter; + u8 tmp[80] = {0}; + u32 num; + u16 val; + u16 ret; + int err; + + + padapter = (PADAPTER)rtw_netdev_priv(dev); + + if (NULL == buffer) { + RTW_INFO(FUNC_ADPT_FMT ": input buffer is NULL!\n", + FUNC_ADPT_ARG(padapter)); + err = -EFAULT; + goto exit; + } + + if (count < 1) { + RTW_INFO(FUNC_ADPT_FMT ": input length is 0!\n", + FUNC_ADPT_ARG(padapter)); + err = -EFAULT; + goto exit; + } + + num = count; + if (num > (sizeof(tmp) - 1)) + num = (sizeof(tmp) - 1); + + if (copy_from_user(tmp, buffer, num)) { + RTW_INFO(FUNC_ADPT_FMT ": copy buffer from user space FAIL!\n", + FUNC_ADPT_ARG(padapter)); + err = -EFAULT; + goto exit; + } + + err = btreg_parse_str(tmp, &btreg_write_type, &btreg_write_addr, &val); + if (err) + goto exit; + + RTW_INFO(FUNC_ADPT_FMT ": Set (%s)0x%X = 0x%x\n", + FUNC_ADPT_ARG(padapter), btreg_type[btreg_write_type], btreg_write_addr, val); + + ret = rtw_btcoex_btreg_write(padapter, btreg_write_type, btreg_write_addr, val); + if (!CHECK_STATUS_CODE_FROM_BT_MP_OPER_RET(ret, BT_STATUS_BT_OP_SUCCESS)) + err = ret; + +exit: + btreg_write_error = err; + + return count; +} + +int proc_get_btc_reduce_wl_txpwr(struct seq_file *m, void *v) +{ + struct net_device *dev; + PADAPTER padapter; + u8 data; + + dev = m->private; + padapter = (PADAPTER)rtw_netdev_priv(dev); + + data = rtw_btcoex_get_reduce_wl_txpwr(padapter); + RTW_PRINT_SEL(m, "BTC reduce WL TxPwr = %d dB\n", data); + + return 0; +} + +ssize_t proc_set_btc_reduce_wl_txpwr(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + PADAPTER padapter; + HAL_DATA_TYPE *hal_data; + u8 tmp[80] = {0}; + u32 val = 0; + u32 num; + + padapter = (PADAPTER)rtw_netdev_priv(dev); + hal_data = GET_HAL_DATA(padapter); + + /* RTW_INFO("+" FUNC_ADPT_FMT "\n", FUNC_ADPT_ARG(padapter)); */ + + if (NULL == buffer) { + RTW_INFO(FUNC_ADPT_FMT ": input buffer is NULL!\n", + FUNC_ADPT_ARG(padapter)); + + return -EFAULT; + } + + if (count < 1) { + RTW_INFO(FUNC_ADPT_FMT ": input length is 0!\n", + FUNC_ADPT_ARG(padapter)); + + return -EFAULT; + } + + num = count; + if (num > (sizeof(tmp) - 1)) + num = (sizeof(tmp) - 1); + + if (copy_from_user(tmp, buffer, num)) { + RTW_INFO(FUNC_ADPT_FMT ": copy buffer from user space FAIL!\n", + FUNC_ADPT_ARG(padapter)); + + return -EFAULT; + } + + num = sscanf(tmp, "%d", &val); + + if ((IS_HARDWARE_TYPE_8822C(padapter)) && (hal_data->EEPROMBluetoothCoexist == _TRUE)) + rtw_btc_reduce_wl_txpwr_cmd(padapter, val); + + return count; +} + +#endif /* CONFIG_BT_COEXIST */ + +#ifdef CONFIG_MBSSID_CAM +int proc_get_mbid_cam_cache(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + rtw_mbid_cam_cache_dump(m, __func__, adapter); + rtw_mbid_cam_dump(m, __func__, adapter); + return 0; +} +#endif /* CONFIG_MBSSID_CAM */ + +int proc_get_mac_addr(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + rtw_hal_dump_macaddr(m, adapter); + return 0; +} + +static int proc_get_skip_band(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + int bandskip; + + bandskip = RTW_GET_SCAN_BAND_SKIP(adapter); + RTW_PRINT_SEL(m, "bandskip:0x%02x\n", bandskip); + return 0; +} + +static ssize_t proc_set_skip_band(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[6]; + u8 skip_band; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hhu", &skip_band); + + if (num < 1) + return -EINVAL; + + if (1 == skip_band) + RTW_SET_SCAN_BAND_SKIP(padapter, BAND_24G); + else if (2 == skip_band) + RTW_SET_SCAN_BAND_SKIP(padapter, BAND_5G); + else if (3 == skip_band) + RTW_CLR_SCAN_BAND_SKIP(padapter, BAND_24G); + else if (4 == skip_band) + RTW_CLR_SCAN_BAND_SKIP(padapter, BAND_5G); + } + return count; + +} + +#ifdef CONFIG_RTW_ACS +static int proc_get_chan_info(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + rtw_acs_chan_info_dump(m, adapter); + return 0; +} + +static int proc_get_best_chan(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + if (IS_ACS_ENABLE(adapter)) + rtw_acs_info_dump(m, adapter); + else + _RTW_PRINT_SEL(m,"ACS disabled\n"); + return 0; +} + +static ssize_t proc_set_acs(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ +#ifdef CONFIG_RTW_ACS_DBG + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + u8 acs_state = 0; + u16 scan_ch_ms= 0, acs_scan_ch_ms = 0; + u8 scan_type = SCAN_ACTIVE, igi= 0, bw = 0; + u8 acs_scan_type = SCAN_ACTIVE, acs_igi= 0, acs_bw = 0; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hhu %hhu %hu %hhx %hhu", + &acs_state, &scan_type, &scan_ch_ms, &igi, &bw); + + if (num < 1) + return -EINVAL; + + if (acs_state) + rtw_acs_start(padapter); + else + rtw_acs_stop(padapter); + num = num -1; + + if(num) { + if (num-- > 0) + acs_scan_type = scan_type; + if (num-- > 0) + acs_scan_ch_ms = scan_ch_ms; + if (num-- > 0) + acs_igi = igi; + if (num-- > 0) + acs_bw = bw; + rtw_acs_adv_setting(padapter, acs_scan_type, acs_scan_ch_ms, acs_igi, acs_bw); + } + } +#endif /*CONFIG_RTW_ACS_DBG*/ + return count; +} +#endif /*CONFIG_RTW_ACS*/ + +#ifdef CONFIG_BACKGROUND_NOISE_MONITOR +static int proc_get_nm(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + rtw_noise_info_dump(m, adapter); + return 0; +} + +static ssize_t proc_set_nm(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + u8 nm_state = 0; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hhu", &nm_state); + + if (num < 1) + return -EINVAL; + + if (nm_state) + rtw_nm_enable(padapter); + else + rtw_nm_disable(padapter); + + } + return count; +} +#endif /*CONFIG_RTW_ACS*/ + +static int proc_get_hal_spec(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_hal_spec(m, adapter); + return 0; +} + +static int proc_get_hal_trx_mode(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = rtw_netdev_priv(dev); + + dump_hal_trx_mode(m, adapter); + return 0; +} + +static int proc_get_phy_cap(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + rtw_dump_phy_cap(m, adapter); +#ifdef CONFIG_80211N_HT + rtw_dump_drv_phy_cap(m, adapter); + rtw_get_dft_phy_cap(m, adapter); +#endif /* CONFIG_80211N_HT */ + return 0; +} + +#ifdef CONFIG_SUPPORT_TRX_SHARED +#include "../../hal/hal_halmac.h" +static int proc_get_trx_share_mode(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_trx_share_mode(m, adapter); + return 0; +} +#endif + +static int proc_dump_rsvd_page(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + rtw_dump_rsvd_page(m, adapter, adapter->rsvd_page_offset, adapter->rsvd_page_num); + return 0; +} +static ssize_t proc_set_rsvd_page_info(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + u8 page_offset, page_num; + + if (count < 2) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hhu %hhu", &page_offset, &page_num); + + if (num < 2) + return -EINVAL; + padapter->rsvd_page_offset = page_offset; + padapter->rsvd_page_num = page_num; + } + return count; +} + +#ifdef CONFIG_SUPPORT_FIFO_DUMP +static int proc_dump_fifo(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + rtw_dump_fifo(m, adapter, adapter->fifo_sel, adapter->fifo_addr, adapter->fifo_size); + return 0; +} +static ssize_t proc_set_fifo_info(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + u8 fifo_sel = 0; + u32 fifo_addr = 0; + u32 fifo_size = 0; + + if (count < 3) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%hhu %x %d", &fifo_sel, &fifo_addr, &fifo_size); + + if (num < 3) + return -EINVAL; + + padapter->fifo_sel = fifo_sel; + padapter->fifo_addr = fifo_addr; + padapter->fifo_size = fifo_size; + } + return count; +} +#endif + +#ifdef CONFIG_WOW_PATTERN_HW_CAM +int proc_dump_pattern_cam(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + int i; + struct rtl_wow_pattern context; + + for (i = 0 ; i < pwrpriv->wowlan_pattern_idx; i++) { + rtw_wow_pattern_read_cam_ent(padapter, i, &context); + rtw_dump_wow_pattern(m, &context, i); + } + + return 0; +} +#endif + +static int proc_get_napi_info(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct registry_priv *pregistrypriv = &adapter->registrypriv; + u8 napi = 0, gro = 0; + u32 weight = 0; + struct dvobj_priv *d; + d = adapter_to_dvobj(adapter); + + +#ifdef CONFIG_RTW_NAPI + if (pregistrypriv->en_napi) { + napi = 1; + weight = RTL_NAPI_WEIGHT; + } + +#ifdef CONFIG_RTW_GRO + if (pregistrypriv->en_gro) + gro = 1; +#endif /* CONFIG_RTW_GRO */ +#endif /* CONFIG_RTW_NAPI */ + + if (napi) { + RTW_PRINT_SEL(m, "NAPI enable, weight=%d\n", weight); +#ifdef CONFIG_RTW_NAPI_DYNAMIC + RTW_PRINT_SEL(m, "Dynamaic NAPI mechanism is on, current NAPI %s\n", + d->en_napi_dynamic ? "enable" : "disable"); + RTW_PRINT_SEL(m, "Dynamaic NAPI info:\n" + "\ttcp_rx_threshold = %d Mbps\n" + "\tcur_rx_tp = %d Mbps\n", + pregistrypriv->napi_threshold, + d->traffic_stat.cur_rx_tp); +#endif /* CONFIG_RTW_NAPI_DYNAMIC */ + } else { + RTW_PRINT_SEL(m, "NAPI disable\n"); + } + RTW_PRINT_SEL(m, "GRO %s\n", gro?"enable":"disable"); + + return 0; + +} + +#ifdef CONFIG_RTW_NAPI_DYNAMIC +static ssize_t proc_set_napi_th(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + struct _ADAPTER *adapter = (struct _ADAPTER *)rtw_netdev_priv(dev); + struct registry_priv *registry = &adapter->registrypriv; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + PADAPTER iface = NULL; + char tmp[32] = {0}; + int thrshld = 0; + int num = 0, i = 0; + + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + RTW_INFO("%s: Last threshold = %d Mbps\n", __FUNCTION__, registry->napi_threshold); + + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (iface) { + if (buffer && !copy_from_user(tmp, buffer, count)) { + registry = &iface->registrypriv; + num = sscanf(tmp, "%d", &thrshld); + if (num > 0) { + if (thrshld > 0) + registry->napi_threshold = thrshld; + } + } + } + } + RTW_INFO("%s: New threshold = %d Mbps\n", __FUNCTION__, registry->napi_threshold); + RTW_INFO("%s: Current RX throughput = %d Mbps\n", + __FUNCTION__, adapter_to_dvobj(adapter)->traffic_stat.cur_rx_tp); + + return count; +} +#endif /* CONFIG_RTW_NAPI_DYNAMIC */ + + +ssize_t proc_set_dynamic_agg_enable(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + int enable = 0, i = 0; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + PADAPTER iface = NULL; + int num = sscanf(tmp, "%d", &enable); + + if (num != 1) { + RTW_INFO("invalid parameter!\n"); + return count; + } + + RTW_INFO("dynamic_agg_enable:%d\n", enable); + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (iface) + iface->registrypriv.dynamic_agg_enable = enable; + } + + } + + return count; + +} + +static int proc_get_dynamic_agg_enable(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct registry_priv *pregistrypriv = &adapter->registrypriv; + + RTW_PRINT_SEL(m, "dynamic_agg_enable:%d\n", pregistrypriv->dynamic_agg_enable); + + return 0; +} + +#ifdef CONFIG_RTW_WDS +static int proc_get_wds_en(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = rtw_netdev_priv(dev); + + if (MLME_STATE(adapter) & (WIFI_AP_STATE | WIFI_STATION_STATE)) + RTW_PRINT_SEL(m, "%d\n", adapter_use_wds(adapter)); + + return 0; +} + +static ssize_t proc_set_wds_en(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = rtw_netdev_priv(dev); + char tmp[32]; + + if (!(MLME_STATE(adapter) & (WIFI_AP_STATE | WIFI_STATION_STATE))) + return -EFAULT; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + u8 enable; + int num = sscanf(tmp, "%hhu", &enable); + + if (num >= 1) + adapter_set_use_wds(adapter, enable); + } + + return count; +} + +static ssize_t proc_set_sta_wds_en(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = rtw_netdev_priv(dev); + char tmp[32]; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + u8 enable; + u8 addr[ETH_ALEN]; + struct sta_info *sta; + int num = sscanf(tmp, "%hhu "MAC_SFMT, &enable, MAC_SARG(addr)); + + if (num != 7) + return -EINVAL; + + if (IS_MCAST(addr) || _rtw_memcmp(adapter_mac_addr(adapter), addr, ETH_ALEN)) + return -EINVAL; + + sta = rtw_get_stainfo(&adapter->stapriv, addr); + if (!sta) + return -EINVAL; + + if (enable) + sta->flags |= WLAN_STA_WDS; + else + sta->flags &= ~WLAN_STA_WDS; + } + + return count; +} + +static int proc_get_wds_gptr(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = rtw_netdev_priv(dev); + + if (MLME_IS_STA(adapter) && MLME_IS_ASOC(adapter)) + dump_wgptr(m, adapter); + + return 0; +} + +#ifdef CONFIG_AP_MODE +static int proc_get_wds_path(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = rtw_netdev_priv(dev); + + if (MLME_IS_AP(adapter) && MLME_IS_ASOC(adapter)) + dump_wpath(m, adapter); + + return 0; +} +#endif /* CONFIG_AP_MODE */ +#endif /* CONFIG_RTW_WDS */ + +#ifdef CONFIG_RTW_MULTI_AP +static int proc_get_multi_ap_opmode(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = rtw_netdev_priv(dev); + + if (MLME_STATE(adapter) & (WIFI_AP_STATE | WIFI_STATION_STATE)) + RTW_PRINT_SEL(m, "0x%02x\n", adapter->multi_ap); + + return 0; +} + +static ssize_t proc_set_multi_ap_opmode(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = rtw_netdev_priv(dev); + char tmp[32]; + + if (!(MLME_STATE(adapter) & (WIFI_AP_STATE | WIFI_STATION_STATE))) + return -EFAULT; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + u8 mode; + int num = sscanf(tmp, "%hhx", &mode); + + if (num >= 1) { + if (MLME_IS_AP(adapter)) + adapter->multi_ap = mode & (MULTI_AP_FRONTHAUL_BSS | MULTI_AP_BACKHAUL_BSS); + else + adapter->multi_ap = mode & MULTI_AP_BACKHAUL_STA; + if (adapter->multi_ap & (MULTI_AP_BACKHAUL_BSS | MULTI_AP_BACKHAUL_STA)) + adapter_set_use_wds(adapter, 1); + } + } + + return count; +} + +static int proc_get_unassoc_sta(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = GET_PRIMARY_ADAPTER(rtw_netdev_priv(dev)); + + dump_unassoc_sta(m, adapter); + + return 0; +} + +ssize_t proc_set_unassoc_sta(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = GET_PRIMARY_ADAPTER(rtw_netdev_priv(dev)); + char tmp[17 * 10 + 32] = {0}; + char cmd[32]; + u8 mode; + u8 stype = 0; + u8 addr[ETH_ALEN]; + +#define UNASOC_STA_CMD_MODE 0 +#define UNASOC_STA_CMD_ADD 1 +#define UNASOC_STA_CMD_DEL 2 +#define UNASOC_STA_CMD_CLR 3 +#define UNASOC_STA_CMD_UNINT 4 +#define UNASOC_STA_CMD_NUM 5 + + static const char * const unasoc_sta_cmd_str[] = { + "mode", + "add", + "del", + "clr", + "uninterest", + }; + u8 cmd_id = UNASOC_STA_CMD_NUM; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + RTW_WARN(FUNC_ADPT_FMT" input string too long\n", FUNC_ADPT_ARG(adapter)); + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + /* + * mode , + * add [] + * del [] + * clr + */ + char *c, *next; + int i; + u8 is_bcast; + + next = tmp; + c = strsep(&next, " \t"); + if (!c || sscanf(c, "%s", cmd) != 1) + goto exit; + + for (i = 0; i < UNASOC_STA_CMD_NUM; i++) + if (strcmp(unasoc_sta_cmd_str[i], cmd) == 0) + cmd_id = i; + + switch (cmd_id) { + case UNASOC_STA_CMD_MODE: + c = strsep(&next, " \t"); + if (!c || sscanf(c, "%hhu,%hhu", &stype, &mode) != 2) { + RTW_WARN(FUNC_ADPT_FMT" invalid arguments of mode cmd\n", FUNC_ADPT_ARG(adapter)); + goto exit; + } + if (stype >= UNASOC_STA_SRC_NUM) { + RTW_WARN(FUNC_ADPT_FMT" invalid stype:%u\n", FUNC_ADPT_ARG(adapter), stype); + goto exit; + } + if (mode >= UNASOC_STA_MODE_NUM) { + RTW_WARN(FUNC_ADPT_FMT" invalid mode:%u\n", FUNC_ADPT_ARG(adapter), mode); + goto exit; + } + rtw_unassoc_sta_set_mode(adapter, stype, mode); + break; + + case UNASOC_STA_CMD_ADD: + case UNASOC_STA_CMD_DEL: + case UNASOC_STA_CMD_UNINT: + /* check for macaddr list */ + c = strsep(&next, " \t"); + while (c != NULL) { + if (sscanf(c, MAC_SFMT, MAC_SARG(addr)) != 6) + break; + + is_bcast = is_broadcast_mac_addr(addr); + if (is_bcast + || rtw_check_invalid_mac_address(addr, 0) == _FALSE + ) { + if (cmd_id == UNASOC_STA_CMD_DEL) { + if (is_bcast) { + rtw_del_unassoc_sta_queue(adapter); + break; + } else + rtw_del_unassoc_sta(adapter, addr); + } else if (cmd_id == UNASOC_STA_CMD_UNINT) { + if (is_bcast) { + rtw_undo_all_interested_unassoc_sta(adapter); + break; + } else + rtw_undo_interested_unassoc_sta(adapter, addr); + } else if (!is_bcast) + rtw_add_interested_unassoc_sta(adapter, addr); + } + + c = strsep(&next, " \t"); + } + break; + + case UNASOC_STA_CMD_CLR: + /* clear sta list */ + rtw_del_unassoc_sta_queue(adapter); + goto exit; + + default: + RTW_WARN(FUNC_ADPT_FMT" invalid cmd:\"%s\"\n", FUNC_ADPT_ARG(adapter), cmd); + goto exit; + } + } + +exit: + return count; +} + +#ifdef CONFIG_IOCTL_CFG80211 +static u8 assoc_req_mac_addr[6]; +int proc_get_sta_assoc_req_frame_body(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + if (MLME_IS_AP(adapter)) { + struct sta_info *psta; + _irqL irqL; + u8 *passoc_req = NULL; + u32 assoc_req_len = 0; + + psta = rtw_get_stainfo(&adapter->stapriv, assoc_req_mac_addr); + if (psta == NULL) { + RTW_PRINT(FUNC_ADPT_FMT" sta("MAC_FMT") not found\n", + FUNC_ADPT_ARG(adapter), MAC_ARG(assoc_req_mac_addr)); + return 0; + } + RTW_PRINT(FUNC_ADPT_FMT" sta("MAC_FMT") found\n", + FUNC_ADPT_ARG(adapter), MAC_ARG(assoc_req_mac_addr)); + _enter_critical_bh(&psta->lock, &irqL); + if (psta->passoc_req && psta->assoc_req_len > 0) { + passoc_req = rtw_zmalloc(psta->assoc_req_len); + if (passoc_req) { + assoc_req_len = psta->assoc_req_len; + _rtw_memcpy(passoc_req, psta->passoc_req, assoc_req_len); + } + } + _exit_critical_bh(&psta->lock, &irqL); + if (passoc_req && assoc_req_len > IEEE80211_3ADDR_LEN) { + u8 *body = passoc_req + IEEE80211_3ADDR_LEN; + u32 body_len = assoc_req_len - IEEE80211_3ADDR_LEN; + u16 i; + + for (i = 0; i < body_len; i++) + _RTW_PRINT_SEL(m, "%02X", body[i]); + _RTW_PRINT_SEL(m, "\n"); + } + if (passoc_req && assoc_req_len > 0) + rtw_mfree(passoc_req, assoc_req_len); + } + + return 0; +} + +ssize_t proc_set_sta_assoc_req_frame_body(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[18] = {0}; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + if (sscanf(tmp, MAC_SFMT, MAC_SARG(assoc_req_mac_addr)) != 6) { + _rtw_memset(assoc_req_mac_addr, 0, 6); + RTW_PRINT(FUNC_ADPT_FMT" Invalid format\n", + FUNC_ADPT_ARG(adapter)); + } + + } + + return count; +} +#endif /* CONFIG_IOCTL_CFG80211 */ + +static int proc_get_ch_util_threshold(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = GET_PRIMARY_ADAPTER(rtw_netdev_priv(dev)); + + RTW_PRINT_SEL(m, "%hhu\n", adapter->ch_util_threshold); + + return 0; +} + +static ssize_t proc_set_ch_util_threshold(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = GET_PRIMARY_ADAPTER(rtw_netdev_priv(dev)); + char tmp[4]; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + u8 threshold; + int num = sscanf(tmp, "%hhu", &threshold); + + if (num == 1) + adapter->ch_util_threshold = threshold; + } + + return count; +} + +static int proc_get_ch_utilization(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + RTW_PRINT_SEL(m, "%hhu\n", rtw_get_ch_utilization(adapter)); + + return 0; +} +#endif /* CONFIG_RTW_MULTI_AP */ + +#ifdef CONFIG_RTW_MESH +static int proc_get_mesh_peer_sel_policy(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_mesh_peer_sel_policy(m, adapter); + + return 0; +} + +#if CONFIG_RTW_MESH_ACNODE_PREVENT +static int proc_get_mesh_acnode_prevent(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + if (MLME_IS_MESH(adapter)) + dump_mesh_acnode_prevent_settings(m, adapter); + + return 0; +} + +static ssize_t proc_set_mesh_acnode_prevent(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + struct mesh_peer_sel_policy *peer_sel_policy = &adapter->mesh_cfg.peer_sel_policy; + u8 enable; + u32 conf_timeout_ms; + u32 notify_timeout_ms; + int num = sscanf(tmp, "%hhu %u %u", &enable, &conf_timeout_ms, ¬ify_timeout_ms); + + if (num >= 1) + peer_sel_policy->acnode_prevent = enable; + if (num >= 2) + peer_sel_policy->acnode_conf_timeout_ms = conf_timeout_ms; + if (num >= 3) + peer_sel_policy->acnode_notify_timeout_ms = notify_timeout_ms; + } + + return count; +} +#endif /* CONFIG_RTW_MESH_ACNODE_PREVENT */ + +#if CONFIG_RTW_MESH_OFFCH_CAND +static int proc_get_mesh_offch_cand(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + if (MLME_IS_MESH(adapter)) + dump_mesh_offch_cand_settings(m, adapter); + + return 0; +} + +static ssize_t proc_set_mesh_offch_cand(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + struct mesh_peer_sel_policy *peer_sel_policy = &adapter->mesh_cfg.peer_sel_policy; + u8 enable; + u32 find_int_ms; + int num = sscanf(tmp, "%hhu %u", &enable, &find_int_ms); + + if (num >= 1) + peer_sel_policy->offch_cand = enable; + if (num >= 2) + peer_sel_policy->offch_find_int_ms = find_int_ms; + } + + return count; +} +#endif /* CONFIG_RTW_MESH_OFFCH_CAND */ + +#if CONFIG_RTW_MESH_PEER_BLACKLIST +static int proc_get_mesh_peer_blacklist(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + if (MLME_IS_MESH(adapter)) { + dump_mesh_peer_blacklist_settings(m, adapter); + if (MLME_IS_ASOC(adapter)) + dump_mesh_peer_blacklist(m, adapter); + } + + return 0; +} + +static ssize_t proc_set_mesh_peer_blacklist(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + struct mesh_peer_sel_policy *peer_sel_policy = &adapter->mesh_cfg.peer_sel_policy; + u32 conf_timeout_ms; + u32 blacklist_timeout_ms; + int num = sscanf(tmp, "%u %u", &conf_timeout_ms, &blacklist_timeout_ms); + + if (num >= 1) + peer_sel_policy->peer_conf_timeout_ms = conf_timeout_ms; + if (num >= 2) + peer_sel_policy->peer_blacklist_timeout_ms = blacklist_timeout_ms; + } + + return count; +} +#endif /* CONFIG_RTW_MESH_PEER_BLACKLIST */ + +#if CONFIG_RTW_MESH_CTO_MGATE_BLACKLIST +static int proc_get_mesh_cto_mgate_require(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + if (MLME_IS_MESH(adapter)) + RTW_PRINT_SEL(m, "%u\n", adapter->mesh_cfg.peer_sel_policy.cto_mgate_require); + + return 0; +} + +static ssize_t proc_set_mesh_cto_mgate_require(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + struct mesh_peer_sel_policy *peer_sel_policy = &adapter->mesh_cfg.peer_sel_policy; + u8 require; + int num = sscanf(tmp, "%hhu", &require); + + if (num >= 1) { + peer_sel_policy->cto_mgate_require = require; + #if CONFIG_RTW_MESH_CTO_MGATE_CARRIER + if (rtw_mesh_cto_mgate_required(adapter)) + rtw_netif_carrier_off(adapter->pnetdev); + else + rtw_netif_carrier_on(adapter->pnetdev); + #endif + } + } + + return count; +} + +static int proc_get_mesh_cto_mgate_blacklist(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + if (MLME_IS_MESH(adapter)) { + dump_mesh_cto_mgate_blacklist_settings(m, adapter); + if (MLME_IS_ASOC(adapter)) + dump_mesh_cto_mgate_blacklist(m, adapter); + } + + return 0; +} + +static ssize_t proc_set_mesh_cto_mgate_blacklist(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + struct mesh_peer_sel_policy *peer_sel_policy = &adapter->mesh_cfg.peer_sel_policy; + u32 conf_timeout_ms; + u32 blacklist_timeout_ms; + int num = sscanf(tmp, "%u %u", &conf_timeout_ms, &blacklist_timeout_ms); + + if (num >= 1) + peer_sel_policy->cto_mgate_conf_timeout_ms = conf_timeout_ms; + if (num >= 2) + peer_sel_policy->cto_mgate_blacklist_timeout_ms = blacklist_timeout_ms; + } + + return count; +} +#endif /* CONFIG_RTW_MESH_CTO_MGATE_BLACKLIST */ + +static int proc_get_mesh_networks(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + dump_mesh_networks(m, adapter); + + return 0; +} + +static int proc_get_mesh_plink_ctl(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + if (MLME_IS_MESH(adapter)) + dump_mesh_plink_ctl(m, adapter); + + return 0; +} + +static int proc_get_mesh_mpath(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + if (MLME_IS_MESH(adapter) && MLME_IS_ASOC(adapter)) + dump_mpath(m, adapter); + + return 0; +} + +static int proc_get_mesh_mpp(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + if (MLME_IS_MESH(adapter) && MLME_IS_ASOC(adapter)) + dump_mpp(m, adapter); + + return 0; +} + +static int proc_get_mesh_known_gates(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + if (MLME_IS_MESH(adapter)) + dump_known_gates(m, adapter); + + return 0; +} + +#if CONFIG_RTW_MESH_DATA_BMC_TO_UC +static int proc_get_mesh_b2u_flags(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + if (MLME_IS_MESH(adapter)) + dump_mesh_b2u_flags(m, adapter); + + return 0; +} + +static ssize_t proc_set_mesh_b2u_flags(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + struct rtw_mesh_cfg *mcfg = &adapter->mesh_cfg; + u8 msrc, mfwd; + int num = sscanf(tmp, "%hhx %hhx", &msrc, &mfwd); + + if (num >= 1) + mcfg->b2u_flags_msrc = msrc; + if (num >= 2) + mcfg->b2u_flags_mfwd = mfwd; + } + + return count; +} +#endif /* CONFIG_RTW_MESH_DATA_BMC_TO_UC */ + +static int proc_get_mesh_stats(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + if (MLME_IS_MESH(adapter)) + dump_mesh_stats(m, adapter); + + return 0; +} + +static int proc_get_mesh_gate_timeout(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + + if (MLME_IS_MESH(adapter)) + RTW_PRINT_SEL(m, "%u factor\n", + adapter->mesh_cfg.path_gate_timeout_factor); + + return 0; +} + +static ssize_t proc_set_mesh_gate_timeout(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + struct rtw_mesh_cfg *mcfg = &adapter->mesh_cfg; + u32 timeout; + int num = sscanf(tmp, "%u", &timeout); + + if (num < 1) + goto exit; + + mcfg->path_gate_timeout_factor = timeout; + } + +exit: + return count; +} + +static int proc_get_mesh_gate_state(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = (_adapter *)rtw_netdev_priv(dev); + struct rtw_mesh_cfg *mcfg = &adapter->mesh_cfg; + u8 cto_mgate = 0; + + if (MLME_IS_MESH(adapter)) { + if (rtw_mesh_is_primary_gate(adapter)) + RTW_PRINT_SEL(m, "PG\n"); + else if (mcfg->dot11MeshGateAnnouncementProtocol) + RTW_PRINT_SEL(m, "G\n"); + else if (rtw_mesh_gate_num(adapter)) + RTW_PRINT_SEL(m, "C\n"); + else + RTW_PRINT_SEL(m, "N\n"); + } + + return 0; +} + +static int proc_get_peer_alive_based_preq(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + struct _ADAPTER *adapter= (_adapter *)rtw_netdev_priv(dev); + struct registry_priv *rp = &adapter->registrypriv; + + RTW_PRINT_SEL(m, "peer_alive_based_preq = %u\n", + rp->peer_alive_based_preq); + + return 0; +} + +static ssize_t +proc_set_peer_alive_based_preq(struct file *file, const char __user *buffer, + size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + struct _ADAPTER *adapter = (_adapter *)rtw_netdev_priv(dev); + struct registry_priv *rp = &adapter->registrypriv; + char tmp[8]; + int num = 0; + u8 enable = 0; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (!buffer || copy_from_user(tmp, buffer, count)) + goto exit; + + num = sscanf(tmp, "%hhu", &enable); + if (num != 1) { + RTW_ERR("%s: invalid parameter!\n", __FUNCTION__); + goto exit; + } + + if (enable > 1) { + RTW_ERR("%s: invalid value!\n", __FUNCTION__); + goto exit; + } + rp->peer_alive_based_preq = enable; + +exit: + return count; +} +#endif /* CONFIG_RTW_MESH */ + +#ifdef RTW_BUSY_DENY_SCAN +static int proc_get_scan_interval_thr(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + struct _ADAPTER *adapter= (struct _ADAPTER *)rtw_netdev_priv(dev); + struct registry_priv *rp = &adapter->registrypriv; + + + RTW_PRINT_SEL(m, "scan interval threshold = %u ms\n", + rp->scan_interval_thr); + + return 0; +} + +static ssize_t proc_set_scan_interval_thr(struct file *file, + const char __user *buffer, + size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + struct _ADAPTER *adapter= (struct _ADAPTER *)rtw_netdev_priv(dev); + struct registry_priv *rp = &adapter->registrypriv; + char tmp[12]; + int num = 0; + u32 thr = 0; + + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (!buffer || copy_from_user(tmp, buffer, count)) + goto exit; + + num = sscanf(tmp, "%u", &thr); + if (num != 1) { + RTW_ERR("%s: invalid parameter!\n", __FUNCTION__); + goto exit; + } + + rp->scan_interval_thr = thr; + + RTW_PRINT("%s: scan interval threshold = %u ms\n", + __FUNCTION__, rp->scan_interval_thr); + +exit: + return count; +} + +#endif /* RTW_BUSY_DENY_SCAN */ + +static int proc_get_scan_deny(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + struct _ADAPTER *adapter= (_adapter *)rtw_netdev_priv(dev); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + + RTW_PRINT_SEL(m, "scan_deny is %s\n", (dvobj->scan_deny == _TRUE) ? "enable":"disable"); + + return 0; +} + +static ssize_t proc_set_scan_deny(struct file *file, const char __user *buffer, + size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + struct _ADAPTER *adapter = (_adapter *)rtw_netdev_priv(dev); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + char tmp[8]; + int num = 0; + int enable = 0; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (!buffer || copy_from_user(tmp, buffer, count)) + goto exit; + + num = sscanf(tmp, "%d", &enable); + if (num != 1) { + RTW_ERR("%s: invalid parameter!\n", __FUNCTION__); + goto exit; + } + + dvobj->scan_deny = enable ? _TRUE : _FALSE; + + RTW_PRINT("%s: scan_deny is %s\n", + __FUNCTION__, (dvobj->scan_deny == _TRUE) ? "enable":"disable"); + +exit: + return count; +} + +#ifdef CONFIG_RTW_TPT_MODE +static int proc_get_tpt_mode(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + struct _ADAPTER *adapter= (_adapter *)rtw_netdev_priv(dev); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + + RTW_PRINT_SEL(m, "current tpt_mode = %d\n", dvobj->tpt_mode); + + return 0; +} + +static void tpt_mode_default(struct _ADAPTER *adapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + + /* 1. disable scan deny */ + dvobj->scan_deny = _FALSE; + + /* 2. back to original LPS mode */ +#ifdef CONFIG_LPS + rtw_pm_set_lps(adapter, adapter->registrypriv.power_mgnt); +#endif + + /* 3. back to original 2.4 tx bw mode */ + rtw_set_tx_bw_mode(adapter, adapter->registrypriv.tx_bw_mode); +} + +static void rtw_tpt_mode(struct _ADAPTER *adapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + + if (dvobj->tpt_mode > 0) { + + /* when enable each tpt mode + 1. scan deny + 2. disable LPS */ + + dvobj->scan_deny = _TRUE; + +#ifdef CONFIG_LPS + rtw_pm_set_lps(adapter, PS_MODE_ACTIVE); +#endif + + } + + switch (dvobj->tpt_mode) { + case 0: /* default mode */ + tpt_mode_default(adapter); + break; + case 1: /* High TP*/ + /*tpt_mode1(adapter);*/ + dvobj->edca_be_ul = 0x5e431c; + dvobj->edca_be_dl = 0x00431c; + break; + case 2: /* noise */ + /* tpt_mode2(adapter); */ + dvobj->edca_be_ul = 0x00431c; + dvobj->edca_be_dl = 0x00431c; + + rtw_set_tx_bw_mode(adapter, 0x20); /* for 2.4g, fixed tx_bw_mode to 20Mhz */ + break; + case 3: /* long distance */ + /* tpt_mode3(adapter); */ + dvobj->edca_be_ul = 0x00431c; + dvobj->edca_be_dl = 0x00431c; + + rtw_set_tx_bw_mode(adapter, 0x20); /* for 2.4g, fixed tx_bw_mode to 20Mhz */ + break; + case 4: /* noise + long distance */ + /* tpt_mode4(adapter); */ + dvobj->edca_be_ul = 0x00431c; + dvobj->edca_be_dl = 0x00431c; + + rtw_set_tx_bw_mode(adapter, 0x20); /* for 2.4g, fixed tx_bw_mode to 20Mhz */ + break; + default: /* default mode */ + tpt_mode_default(adapter); + break; + } + +} + +static ssize_t proc_set_tpt_mode(struct file *file, const char __user *buffer, + size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + struct _ADAPTER *adapter = (_adapter *)rtw_netdev_priv(dev); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + char tmp[32]; + int num = 0; + int mode = 0; + +#define MAX_TPT_MODE_NUM 4 + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (!buffer || copy_from_user(tmp, buffer, count)) + goto exit; + + num = sscanf(tmp, "%d", &mode); + if (num != 1) { + RTW_ERR("%s: invalid parameter!\n", __FUNCTION__); + goto exit; + } + + if (mode > MAX_TPT_MODE_NUM ) + mode = 0; + + RTW_PRINT("%s: previous mode = %d\n", + __FUNCTION__, dvobj->tpt_mode); + + RTW_PRINT("%s: enabled mode = %d\n", + __FUNCTION__, mode); + + dvobj->tpt_mode = mode; + + rtw_tpt_mode(adapter); + +exit: + return count; + +} +#endif /* CONFIG_RTW_TPT_MODE */ + +int proc_get_cur_beacon_keys(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *adapter = rtw_netdev_priv(dev); + struct mlme_priv *mlme = &adapter->mlmepriv; + + rtw_dump_bcn_keys(m, &mlme->cur_beacon_keys); + + return 0; +} + +static int proc_get_amsdu_mode(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct registry_priv *pregpriv = &padapter->registrypriv; + + if (pregpriv) { + if (pregpriv->amsdu_mode == RTW_AMSDU_MODE_NON_SPP) + RTW_PRINT_SEL(m, "amsdu mode: NON-SPP\n"); + else if (pregpriv->amsdu_mode == RTW_AMSDU_MODE_SPP) + RTW_PRINT_SEL(m, "amsdu mode: SPP\n"); + else if (pregpriv->amsdu_mode == RTW_AMSDU_MODE_ALL_DROP) + RTW_PRINT_SEL(m, "amsdu mode: ALL DROP\n"); + else + RTW_PRINT_SEL(m, "unexpected amsdu mode\n"); + } + + return 0; +} + +static ssize_t proc_set_amsdu_mode(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + struct registry_priv *pregpriv = &padapter->registrypriv; + char tmp[32]; + u32 mode; + u8 bw_2g; + u8 bw_5g; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%d", &mode); + + if (mode == RTW_AMSDU_MODE_NON_SPP + || mode == RTW_AMSDU_MODE_SPP + || mode == RTW_AMSDU_MODE_ALL_DROP) { + pregpriv->amsdu_mode = mode; + RTW_INFO("amsdu mode=%u\n", mode); + } else { + RTW_INFO("set unexpected mode = %d, won't apply\n", mode); + } + } + + return count; + +} + +/* +* rtw_adapter_proc: +* init/deinit when register/unregister net_device +*/ +const struct rtw_proc_hdl adapter_proc_hdls[] = { +#if RTW_SEQ_FILE_TEST + RTW_PROC_HDL_SEQ("seq_file_test", &seq_file_test, NULL), +#endif + RTW_PROC_HDL_SSEQ("write_reg", NULL, proc_set_write_reg), + RTW_PROC_HDL_SSEQ("read_reg", proc_get_read_reg, proc_set_read_reg), + RTW_PROC_HDL_SSEQ("tx_rate_bmp", proc_get_dump_tx_rate_bmp, NULL), + RTW_PROC_HDL_SSEQ("adapters_status", proc_get_dump_adapters_status, NULL), +#ifdef CONFIG_RTW_CUSTOMER_STR + RTW_PROC_HDL_SSEQ("customer_str", proc_get_customer_str, NULL), +#endif + RTW_PROC_HDL_SSEQ("fwstate", proc_get_fwstate, NULL), + RTW_PROC_HDL_SSEQ("sec_info", proc_get_sec_info, NULL), + RTW_PROC_HDL_SSEQ("mlmext_state", proc_get_mlmext_state, NULL), + RTW_PROC_HDL_SSEQ("qos_option", proc_get_qos_option, NULL), + RTW_PROC_HDL_SSEQ("ht_option", proc_get_ht_option, NULL), + RTW_PROC_HDL_SSEQ("rf_info", proc_get_rf_info, NULL), + RTW_PROC_HDL_SSEQ("scan_param", proc_get_scan_param, proc_set_scan_param), + RTW_PROC_HDL_SSEQ("scan_abort", proc_get_scan_abort, NULL), +#ifdef CONFIG_SCAN_BACKOP + RTW_PROC_HDL_SSEQ("backop_flags_sta", proc_get_backop_flags_sta, proc_set_backop_flags_sta), + #ifdef CONFIG_AP_MODE + RTW_PROC_HDL_SSEQ("backop_flags_ap", proc_get_backop_flags_ap, proc_set_backop_flags_ap), + #endif + #ifdef CONFIG_RTW_MESH + RTW_PROC_HDL_SSEQ("backop_flags_mesh", proc_get_backop_flags_mesh, proc_set_backop_flags_mesh), + #endif +#endif +#ifdef CONFIG_RTW_REPEATER_SON + RTW_PROC_HDL_SSEQ("rson_data", proc_get_rson_data, proc_set_rson_data), +#endif + RTW_PROC_HDL_SSEQ("survey_info", proc_get_survey_info, proc_set_survey_info), + RTW_PROC_HDL_SSEQ("ap_info", proc_get_ap_info, NULL), +#ifdef ROKU_PRIVATE + RTW_PROC_HDL_SSEQ("infra_ap", proc_get_infra_ap, NULL), +#endif /* ROKU_PRIVATE */ + RTW_PROC_HDL_SSEQ("trx_info", proc_get_trx_info, proc_reset_trx_info), + RTW_PROC_HDL_SSEQ("tx_power_offset", proc_get_tx_power_offset, proc_set_tx_power_offset), + RTW_PROC_HDL_SSEQ("rate_ctl", proc_get_rate_ctl, proc_set_rate_ctl), + RTW_PROC_HDL_SSEQ("bw_ctl", proc_get_bw_ctl, proc_set_bw_ctl), + RTW_PROC_HDL_SSEQ("mac_qinfo", proc_get_mac_qinfo, NULL), + RTW_PROC_HDL_SSEQ("macid_info", proc_get_macid_info, NULL), + RTW_PROC_HDL_SSEQ("bcmc_info", proc_get_mi_ap_bc_info, NULL), + RTW_PROC_HDL_SSEQ("sec_cam", proc_get_sec_cam, proc_set_sec_cam), + RTW_PROC_HDL_SSEQ("sec_cam_cache", proc_get_sec_cam_cache, NULL), + RTW_PROC_HDL_SSEQ("ps_dbg_info", proc_get_ps_dbg_info, proc_set_ps_dbg_info), + RTW_PROC_HDL_SSEQ("wifi_spec", proc_get_wifi_spec, NULL), +#ifdef CONFIG_LAYER2_ROAMING + RTW_PROC_HDL_SSEQ("roam_flags", proc_get_roam_flags, proc_set_roam_flags), + RTW_PROC_HDL_SSEQ("roam_param", proc_get_roam_param, proc_set_roam_param), + RTW_PROC_HDL_SSEQ("roam_tgt_addr", NULL, proc_set_roam_tgt_addr), +#endif /* CONFIG_LAYER2_ROAMING */ +#ifdef CONFIG_RTW_MBO + RTW_PROC_HDL_SSEQ("non_pref_ch", rtw_mbo_proc_non_pref_chans_get, rtw_mbo_proc_non_pref_chans_set), + RTW_PROC_HDL_SSEQ("cell_data", rtw_mbo_proc_cell_data_get, rtw_mbo_proc_cell_data_set), +#endif +#ifdef CONFIG_RTW_80211R + RTW_PROC_HDL_SSEQ("ft_flags", rtw_ft_proc_flags_get, rtw_ft_proc_flags_set), +#endif + RTW_PROC_HDL_SSEQ("defs_param", proc_get_defs_param, proc_set_defs_param), +#ifdef CONFIG_SDIO_HCI + RTW_PROC_HDL_SSEQ("sd_f0_reg_dump", proc_get_sd_f0_reg_dump, NULL), + RTW_PROC_HDL_SSEQ("sdio_local_reg_dump", proc_get_sdio_local_reg_dump, NULL), + RTW_PROC_HDL_SSEQ("sdio_card_info", proc_get_sdio_card_info, NULL), + #ifdef CONFIG_SDIO_RECVBUF_AGGREGATION + RTW_PROC_HDL_SSEQ("sdio_recvbuf_aggregation", proc_get_sdio_recvbuf_aggregation, proc_set_sdio_recvbuf_aggregation), + #endif + #ifdef CONFIG_SDIO_RECVBUF_PWAIT + RTW_PROC_HDL_SSEQ("sdio_recvbuf_pwait", proc_get_sdio_recvbuf_pwait, proc_set_sdio_recvbuf_pwait), + #endif +#ifdef DBG_SDIO + RTW_PROC_HDL_SSEQ("sdio_dbg", proc_get_sdio_dbg, proc_set_sdio_dbg), +#endif /* DBG_SDIO */ +#endif /* CONFIG_SDIO_HCI */ + + RTW_PROC_HDL_SSEQ("fwdl_test_case", NULL, proc_set_fwdl_test_case), + RTW_PROC_HDL_SSEQ("del_rx_ampdu_test_case", NULL, proc_set_del_rx_ampdu_test_case), + RTW_PROC_HDL_SSEQ("wait_hiq_empty", NULL, proc_set_wait_hiq_empty), + RTW_PROC_HDL_SSEQ("sta_linking_test", NULL, proc_set_sta_linking_test), +#ifdef CONFIG_AP_MODE + RTW_PROC_HDL_SSEQ("ap_linking_test", NULL, proc_set_ap_linking_test), +#endif + + RTW_PROC_HDL_SSEQ("mac_reg_dump", proc_get_mac_reg_dump, NULL), + RTW_PROC_HDL_SSEQ("bb_reg_dump", proc_get_bb_reg_dump, NULL), + RTW_PROC_HDL_SSEQ("bb_reg_dump_ex", proc_get_bb_reg_dump_ex, NULL), + RTW_PROC_HDL_SSEQ("rf_reg_dump", proc_get_rf_reg_dump, NULL), + +#ifdef CONFIG_RTW_LED + RTW_PROC_HDL_SSEQ("led_config", proc_get_led_config, proc_set_led_config), +#endif + +#ifdef CONFIG_AP_MODE + RTW_PROC_HDL_SSEQ("aid_status", proc_get_aid_status, proc_set_aid_status), + RTW_PROC_HDL_SSEQ("ap_isolate", proc_get_ap_isolate, proc_set_ap_isolate), + RTW_PROC_HDL_SSEQ("all_sta_info", proc_get_all_sta_info, NULL), + RTW_PROC_HDL_SSEQ("bmc_tx_rate", proc_get_bmc_tx_rate, proc_set_bmc_tx_rate), + #if CONFIG_RTW_AP_DATA_BMC_TO_UC + RTW_PROC_HDL_SSEQ("ap_b2u_flags", proc_get_ap_b2u_flags, proc_set_ap_b2u_flags), + #endif +#endif /* CONFIG_AP_MODE */ + +#ifdef DBG_MEMORY_LEAK + RTW_PROC_HDL_SSEQ("_malloc_cnt", proc_get_malloc_cnt, NULL), +#endif /* DBG_MEMORY_LEAK */ + +#ifdef CONFIG_FIND_BEST_CHANNEL + RTW_PROC_HDL_SSEQ("best_channel", proc_get_best_channel, proc_set_best_channel), +#endif + + RTW_PROC_HDL_SSEQ("rx_signal", proc_get_rx_signal, proc_set_rx_signal), + RTW_PROC_HDL_SSEQ("rx_chk_limit", proc_get_rx_chk_limit, proc_set_rx_chk_limit), + RTW_PROC_HDL_SSEQ("hw_info", proc_get_hw_status, proc_set_hw_status), + RTW_PROC_HDL_SSEQ("mac_rptbuf", proc_get_mac_rptbuf, NULL), +#ifdef CONFIG_80211N_HT + RTW_PROC_HDL_SSEQ("ht_enable", proc_get_ht_enable, proc_set_ht_enable), + RTW_PROC_HDL_SSEQ("bw_mode", proc_get_bw_mode, proc_set_bw_mode), + RTW_PROC_HDL_SSEQ("ampdu_enable", proc_get_ampdu_enable, proc_set_ampdu_enable), + RTW_PROC_HDL_SSEQ("rx_ampdu", proc_get_rx_ampdu, proc_set_rx_ampdu), + RTW_PROC_HDL_SSEQ("rx_ampdu_size_limit", proc_get_rx_ampdu_size_limit, proc_set_rx_ampdu_size_limit), + RTW_PROC_HDL_SSEQ("rx_ampdu_factor", proc_get_rx_ampdu_factor, proc_set_rx_ampdu_factor), + RTW_PROC_HDL_SSEQ("rx_ampdu_density", proc_get_rx_ampdu_density, proc_set_rx_ampdu_density), + RTW_PROC_HDL_SSEQ("tx_ampdu_density", proc_get_tx_ampdu_density, proc_set_tx_ampdu_density), + RTW_PROC_HDL_SSEQ("tx_max_agg_num", proc_get_tx_max_agg_num, proc_set_tx_max_agg_num), + RTW_PROC_HDL_SSEQ("tx_quick_addba_req", proc_get_tx_quick_addba_req, proc_set_tx_quick_addba_req), +#ifdef CONFIG_TX_AMSDU + RTW_PROC_HDL_SSEQ("tx_amsdu", proc_get_tx_amsdu, proc_set_tx_amsdu), + RTW_PROC_HDL_SSEQ("tx_amsdu_rate", proc_get_tx_amsdu_rate, proc_set_tx_amsdu_rate), +#endif +#endif /* CONFIG_80211N_HT */ + +#ifdef CONFIG_80211AC_VHT + RTW_PROC_HDL_SSEQ("vht_24g_enable", proc_get_vht_24g_enable, proc_set_vht_24g_enable), +#endif + + #ifdef CONFIG_SDIO_TX_ENABLE_AVAL_INT + RTW_PROC_HDL_SSEQ("tx_aval_int_threshold", proc_get_tx_aval_th, proc_set_tx_aval_th), + #endif + + RTW_PROC_HDL_SSEQ("dynamic_rrsr", proc_get_dyn_rrsr, proc_set_dyn_rrsr), + RTW_PROC_HDL_SSEQ("en_fwps", proc_get_en_fwps, proc_set_en_fwps), + + /* RTW_PROC_HDL_SSEQ("path_rssi", proc_get_two_path_rssi, NULL), + * RTW_PROC_HDL_SSEQ("rssi_disp",proc_get_rssi_disp, proc_set_rssi_disp), */ + +#ifdef CONFIG_BT_COEXIST + RTW_PROC_HDL_SSEQ("btcoex_dbg", proc_get_btcoex_dbg, proc_set_btcoex_dbg), + RTW_PROC_HDL_SSEQ("btcoex", proc_get_btcoex_info, NULL), + RTW_PROC_HDL_SSEQ("btinfo_evt", NULL, proc_set_btinfo_evt), + RTW_PROC_HDL_SSEQ("btreg_read", proc_get_btreg_read, proc_set_btreg_read), + RTW_PROC_HDL_SSEQ("btreg_write", proc_get_btreg_write, proc_set_btreg_write), + RTW_PROC_HDL_SSEQ("btc_reduce_wl_txpwr", proc_get_btc_reduce_wl_txpwr, proc_set_btc_reduce_wl_txpwr), +#ifdef CONFIG_RF4CE_COEXIST + RTW_PROC_HDL_SSEQ("rf4ce_state", proc_get_rf4ce_state, proc_set_rf4ce_state), +#endif +#endif /* CONFIG_BT_COEXIST */ + +#if defined(DBG_CONFIG_ERROR_DETECT) + RTW_PROC_HDL_SSEQ("sreset", proc_get_sreset, proc_set_sreset), +#endif /* DBG_CONFIG_ERROR_DETECT */ + RTW_PROC_HDL_SSEQ("trx_info_debug", proc_get_trx_info_debug, NULL), + +#ifdef CONFIG_HUAWEI_PROC + RTW_PROC_HDL_SSEQ("huawei_trx_info", proc_get_huawei_trx_info, NULL), +#endif + RTW_PROC_HDL_SSEQ("linked_info_dump", proc_get_linked_info_dump, proc_set_linked_info_dump), + RTW_PROC_HDL_SSEQ("sta_tp_dump", proc_get_sta_tp_dump, proc_set_sta_tp_dump), + RTW_PROC_HDL_SSEQ("sta_tp_info", proc_get_sta_tp_info, NULL), + RTW_PROC_HDL_SSEQ("dis_turboedca", proc_get_turboedca_ctrl, proc_set_turboedca_ctrl), + RTW_PROC_HDL_SSEQ("tx_info_msg", proc_get_tx_info_msg, NULL), + RTW_PROC_HDL_SSEQ("rx_info_msg", proc_get_rx_info_msg, proc_set_rx_info_msg), + +#if defined(CONFIG_LPS_PG) && defined(CONFIG_RTL8822C) + RTW_PROC_HDL_SSEQ("lps_pg_debug", proc_get_lps_pg_debug, NULL), +#endif + +#ifdef CONFIG_GPIO_API + RTW_PROC_HDL_SSEQ("gpio_info", proc_get_gpio, proc_set_gpio), + RTW_PROC_HDL_SSEQ("gpio_set_output_value", NULL, proc_set_gpio_output_value), + RTW_PROC_HDL_SSEQ("gpio_set_direction", NULL, proc_set_config_gpio), +#endif + +#ifdef CONFIG_DBG_COUNTER + RTW_PROC_HDL_SSEQ("rx_logs", proc_get_rx_logs, NULL), + RTW_PROC_HDL_SSEQ("tx_logs", proc_get_tx_logs, NULL), + RTW_PROC_HDL_SSEQ("int_logs", proc_get_int_logs, NULL), +#endif + +#ifdef CONFIG_DBG_RF_CAL + RTW_PROC_HDL_SSEQ("iqk", proc_get_iqk_info, proc_set_iqk), + RTW_PROC_HDL_SSEQ("lck", proc_get_lck_info, proc_set_lck), +#endif + +#ifdef CONFIG_PCI_HCI + RTW_PROC_HDL_SSEQ("rx_ring", proc_get_rx_ring, NULL), + RTW_PROC_HDL_SSEQ("tx_ring", proc_get_tx_ring, NULL), +#ifdef DBG_TXBD_DESC_DUMP + RTW_PROC_HDL_SSEQ("tx_ring_ext", proc_get_tx_ring_ext, proc_set_tx_ring_ext), +#endif + RTW_PROC_HDL_SSEQ("pci_aspm", proc_get_pci_aspm, NULL), + + RTW_PROC_HDL_SSEQ("pci_conf_space", proc_get_pci_conf_space, proc_set_pci_conf_space), + + RTW_PROC_HDL_SSEQ("pci_bridge_conf_space", proc_get_pci_bridge_conf_space, proc_set_pci_bridge_conf_space), + +#endif + +#ifdef CONFIG_WOWLAN + RTW_PROC_HDL_SSEQ("wow_enable", proc_get_wow_enable, proc_set_wow_enable), + RTW_PROC_HDL_SSEQ("wow_pattern_info", proc_get_pattern_info, proc_set_pattern_info), + RTW_PROC_HDL_SSEQ("wow_wakeup_event", proc_get_wakeup_event, + proc_set_wakeup_event), + RTW_PROC_HDL_SSEQ("wowlan_last_wake_reason", proc_get_wakeup_reason, NULL), +#ifdef CONFIG_WOW_PATTERN_HW_CAM + RTW_PROC_HDL_SSEQ("wow_pattern_cam", proc_dump_pattern_cam, NULL), +#endif +#ifdef CONFIG_WOW_KEEP_ALIVE_PATTERN + RTW_PROC_HDL_SSEQ("wow_keep_alive_info", proc_dump_wow_keep_alive_info, NULL), +#endif /*CONFIG_WOW_KEEP_ALIVE_PATTERN*/ + +#endif + +#ifdef CONFIG_GPIO_WAKEUP + RTW_PROC_HDL_SSEQ("wowlan_gpio_info", proc_get_wowlan_gpio_info, proc_set_wowlan_gpio_info), +#endif +#ifdef CONFIG_P2P_WOWLAN + RTW_PROC_HDL_SSEQ("p2p_wowlan_info", proc_get_p2p_wowlan_info, NULL), +#endif + RTW_PROC_HDL_SSEQ("country_code", proc_get_country_code, proc_set_country_code), + RTW_PROC_HDL_SSEQ("chan_plan", proc_get_chan_plan, proc_set_chan_plan), + RTW_PROC_HDL_SSEQ("cap_spt_op_class_ch", proc_get_cap_spt_op_class_ch, proc_set_cap_spt_op_class_ch), + RTW_PROC_HDL_SSEQ("reg_spt_op_class_ch", proc_get_reg_spt_op_class_ch, proc_set_reg_spt_op_class_ch), + RTW_PROC_HDL_SSEQ("cur_spt_op_class_ch", proc_get_cur_spt_op_class_ch, proc_set_cur_spt_op_class_ch), +#if CONFIG_RTW_MACADDR_ACL + RTW_PROC_HDL_SSEQ("macaddr_acl", proc_get_macaddr_acl, proc_set_macaddr_acl), +#endif +#if CONFIG_RTW_PRE_LINK_STA + RTW_PROC_HDL_SSEQ("pre_link_sta", proc_get_pre_link_sta, proc_set_pre_link_sta), +#endif + RTW_PROC_HDL_SSEQ("ch_sel_policy", proc_get_ch_sel_policy, proc_set_ch_sel_policy), +#ifdef CONFIG_DFS_MASTER + RTW_PROC_HDL_SSEQ("dfs_test_case", proc_get_dfs_test_case, proc_set_dfs_test_case), + RTW_PROC_HDL_SSEQ("update_non_ocp", NULL, proc_set_update_non_ocp), + RTW_PROC_HDL_SSEQ("radar_detect", NULL, proc_set_radar_detect), + RTW_PROC_HDL_SSEQ("dfs_ch_sel_e_flags", proc_get_dfs_ch_sel_e_flags, proc_set_dfs_ch_sel_e_flags), + RTW_PROC_HDL_SSEQ("dfs_ch_sel_d_flags", proc_get_dfs_ch_sel_d_flags, proc_set_dfs_ch_sel_d_flags), + #if CONFIG_DFS_SLAVE_WITH_RADAR_DETECT + RTW_PROC_HDL_SSEQ("dfs_slave_with_rd", proc_get_dfs_slave_with_rd, proc_set_dfs_slave_with_rd), + #endif +#endif +#ifdef CONFIG_BCN_CNT_CONFIRM_HDL + RTW_PROC_HDL_SSEQ("new_bcn_max", proc_get_new_bcn_max, proc_set_new_bcn_max), +#endif + RTW_PROC_HDL_SSEQ("sink_udpport", proc_get_udpport, proc_set_udpport), +#ifdef DBG_RX_COUNTER_DUMP + RTW_PROC_HDL_SSEQ("dump_rx_cnt_mode", proc_get_rx_cnt_dump, proc_set_rx_cnt_dump), +#endif +#ifdef CONFIG_AP_MODE + RTW_PROC_HDL_SSEQ("change_bss_chbw", NULL, proc_set_change_bss_chbw), +#endif +#if CONFIG_TX_AC_LIFETIME + RTW_PROC_HDL_SSEQ("tx_aclt_force_val", proc_get_tx_aclt_force_val, proc_set_tx_aclt_force_val), + RTW_PROC_HDL_SSEQ("tx_aclt_flags", proc_get_tx_aclt_flags, proc_set_tx_aclt_flags), + RTW_PROC_HDL_SSEQ("tx_aclt_confs", proc_get_tx_aclt_confs, proc_set_tx_aclt_confs), +#endif + RTW_PROC_HDL_SSEQ("tx_bw_mode", proc_get_tx_bw_mode, proc_set_tx_bw_mode), + RTW_PROC_HDL_SSEQ("hal_txpwr_info", proc_get_hal_txpwr_info, NULL), + RTW_PROC_HDL_SSEQ("target_tx_power", proc_get_target_tx_power, NULL), + RTW_PROC_HDL_SSEQ("tx_power_by_rate", proc_get_tx_power_by_rate, NULL), +#if CONFIG_TXPWR_LIMIT + RTW_PROC_HDL_SSEQ("tx_power_limit", proc_get_tx_power_limit, NULL), +#endif + RTW_PROC_HDL_SSEQ("tpc_settings", proc_get_tpc_settings, proc_set_tpc_settings), + RTW_PROC_HDL_SSEQ("antenna_gain", proc_get_antenna_gain, proc_set_antenna_gain), + RTW_PROC_HDL_SSEQ("tx_power_ext_info", proc_get_tx_power_ext_info, proc_set_tx_power_ext_info), + RTW_PROC_HDL_SEQ("tx_power_idx", &seq_ops_tx_power_idx, proc_set_tx_power_idx_dump), + RTW_PROC_HDL_SEQ("txpwr_total_dbm", &seq_ops_txpwr_total_dbm, proc_set_txpwr_total_dbm_dump), +#ifdef CONFIG_RF_POWER_TRIM + RTW_PROC_HDL_SSEQ("tx_gain_offset", NULL, proc_set_tx_gain_offset), + RTW_PROC_HDL_SSEQ("kfree_flag", proc_get_kfree_flag, proc_set_kfree_flag), + RTW_PROC_HDL_SSEQ("kfree_bb_gain", proc_get_kfree_bb_gain, proc_set_kfree_bb_gain), + RTW_PROC_HDL_SSEQ("kfree_thermal", proc_get_kfree_thermal, proc_set_kfree_thermal), +#endif +#ifdef CONFIG_POWER_SAVING + RTW_PROC_HDL_SSEQ("ps_info", proc_get_ps_info, proc_set_ps_info), +#ifdef CONFIG_WMMPS_STA + RTW_PROC_HDL_SSEQ("wmmps_info", proc_get_wmmps_info, proc_set_wmmps_info), +#endif /* CONFIG_WMMPS_STA */ +#endif +#ifdef CONFIG_TDLS + RTW_PROC_HDL_SSEQ("tdls_info", proc_get_tdls_info, NULL), + RTW_PROC_HDL_SSEQ("tdls_enable", proc_get_tdls_enable, proc_set_tdls_enable), +#endif + RTW_PROC_HDL_SSEQ("monitor", proc_get_monitor, proc_set_monitor), +#ifdef RTW_SIMPLE_CONFIG + RTW_PROC_HDL_SSEQ("rtw_simple_config", proc_get_simple_config, proc_set_simple_config), +#endif + +#ifdef CONFIG_RTW_ACS + RTW_PROC_HDL_SSEQ("acs", proc_get_best_chan, proc_set_acs), + RTW_PROC_HDL_SSEQ("chan_info", proc_get_chan_info, NULL), +#endif + +#ifdef CONFIG_BACKGROUND_NOISE_MONITOR + RTW_PROC_HDL_SSEQ("noise_monitor", proc_get_nm, proc_set_nm), +#endif + +#ifdef CONFIG_PREALLOC_RX_SKB_BUFFER + RTW_PROC_HDL_SSEQ("rtkm_info", proc_get_rtkm_info, NULL), +#endif + RTW_PROC_HDL_SSEQ("efuse_map", proc_get_efuse_map, NULL), +#ifdef CONFIG_IEEE80211W + RTW_PROC_HDL_SSEQ("11w_tx_sa_query", proc_get_tx_sa_query, proc_set_tx_sa_query), + RTW_PROC_HDL_SSEQ("11w_tx_deauth", proc_get_tx_deauth, proc_set_tx_deauth), + RTW_PROC_HDL_SSEQ("11w_tx_auth", proc_get_tx_auth, proc_set_tx_auth), +#endif /* CONFIG_IEEE80211W */ + +#ifdef CONFIG_CUSTOMER01_SMART_ANTENNA + RTW_PROC_HDL_SSEQ("pathb_phase", proc_get_pathb_phase, proc_set_pathb_phase), +#endif + +#ifdef CONFIG_MBSSID_CAM + RTW_PROC_HDL_SSEQ("mbid_cam", proc_get_mbid_cam_cache, NULL), +#endif + RTW_PROC_HDL_SSEQ("mac_addr", proc_get_mac_addr, NULL), + RTW_PROC_HDL_SSEQ("skip_band", proc_get_skip_band, proc_set_skip_band), + RTW_PROC_HDL_SSEQ("hal_spec", proc_get_hal_spec, NULL), + RTW_PROC_HDL_SSEQ("hal_trx_mode", proc_get_hal_trx_mode, NULL), + + RTW_PROC_HDL_SSEQ("rx_stat", proc_get_rx_stat, NULL), + + RTW_PROC_HDL_SSEQ("tx_stat", proc_get_tx_stat, NULL), + /**** PHY Capability ****/ + RTW_PROC_HDL_SSEQ("phy_cap", proc_get_phy_cap, NULL), +#ifdef CONFIG_80211N_HT + RTW_PROC_HDL_SSEQ("rx_stbc", proc_get_rx_stbc, proc_set_rx_stbc), + RTW_PROC_HDL_SSEQ("stbc_cap", proc_get_stbc_cap, proc_set_stbc_cap), + RTW_PROC_HDL_SSEQ("ldpc_cap", proc_get_ldpc_cap, proc_set_ldpc_cap), +#endif /* CONFIG_80211N_HT */ +#ifdef CONFIG_BEAMFORMING + RTW_PROC_HDL_SSEQ("txbf_cap", proc_get_txbf_cap, proc_set_txbf_cap), +#endif + +#ifdef CONFIG_SUPPORT_TRX_SHARED + RTW_PROC_HDL_SSEQ("trx_share_mode", proc_get_trx_share_mode, NULL), +#endif + RTW_PROC_HDL_SSEQ("napi_info", proc_get_napi_info, NULL), +#ifdef CONFIG_RTW_NAPI_DYNAMIC + RTW_PROC_HDL_SSEQ("napi_th", proc_get_napi_info, proc_set_napi_th), +#endif /* CONFIG_RTW_NAPI_DYNAMIC */ + + RTW_PROC_HDL_SSEQ("rsvd_page", proc_dump_rsvd_page, proc_set_rsvd_page_info), + +#ifdef CONFIG_SUPPORT_FIFO_DUMP + RTW_PROC_HDL_SSEQ("fifo_dump", proc_dump_fifo, proc_set_fifo_info), +#endif + RTW_PROC_HDL_SSEQ("fw_info", proc_get_fw_info, NULL), + +#ifdef DBG_XMIT_BLOCK + RTW_PROC_HDL_SSEQ("xmit_block", proc_get_xmit_block, proc_set_xmit_block), +#endif + + RTW_PROC_HDL_SSEQ("ack_timeout", proc_get_ack_timeout, proc_set_ack_timeout), + + RTW_PROC_HDL_SSEQ("dynamic_agg_enable", proc_get_dynamic_agg_enable, proc_set_dynamic_agg_enable), + RTW_PROC_HDL_SSEQ("fw_offload", proc_get_fw_offload, proc_set_fw_offload), + +#ifdef CONFIG_RTW_WDS + RTW_PROC_HDL_SSEQ("wds_en", proc_get_wds_en, proc_set_wds_en), + RTW_PROC_HDL_SSEQ("sta_wds_en", NULL, proc_set_sta_wds_en), + RTW_PROC_HDL_SSEQ("wds_gptr", proc_get_wds_gptr, NULL), + #ifdef CONFIG_AP_MODE + RTW_PROC_HDL_SSEQ("wds_path", proc_get_wds_path, NULL), + #endif +#endif + +#ifdef CONFIG_RTW_MULTI_AP + RTW_PROC_HDL_SSEQ("multi_ap_opmode", proc_get_multi_ap_opmode, proc_set_multi_ap_opmode), + RTW_PROC_HDL_SSEQ("unassoc_sta", proc_get_unassoc_sta, proc_set_unassoc_sta), +#ifdef CONFIG_IOCTL_CFG80211 + RTW_PROC_HDL_SSEQ("sta_assoc_req_frame_body", proc_get_sta_assoc_req_frame_body, proc_set_sta_assoc_req_frame_body), +#endif + RTW_PROC_HDL_SSEQ("ch_util_threshold", proc_get_ch_util_threshold, proc_set_ch_util_threshold), + RTW_PROC_HDL_SSEQ("ch_utilization", proc_get_ch_utilization, NULL), +#endif + +#ifdef CONFIG_RTW_MESH + #if CONFIG_RTW_MESH_ACNODE_PREVENT + RTW_PROC_HDL_SSEQ("mesh_acnode_prevent", proc_get_mesh_acnode_prevent, proc_set_mesh_acnode_prevent), + #endif + #if CONFIG_RTW_MESH_OFFCH_CAND + RTW_PROC_HDL_SSEQ("mesh_offch_cand", proc_get_mesh_offch_cand, proc_set_mesh_offch_cand), + #endif + #if CONFIG_RTW_MESH_PEER_BLACKLIST + RTW_PROC_HDL_SSEQ("mesh_peer_blacklist", proc_get_mesh_peer_blacklist, proc_set_mesh_peer_blacklist), + #endif + #if CONFIG_RTW_MESH_CTO_MGATE_BLACKLIST + RTW_PROC_HDL_SSEQ("mesh_cto_mgate_require", proc_get_mesh_cto_mgate_require, proc_set_mesh_cto_mgate_require), + RTW_PROC_HDL_SSEQ("mesh_cto_mgate_blacklist", proc_get_mesh_cto_mgate_blacklist, proc_set_mesh_cto_mgate_blacklist), + #endif + RTW_PROC_HDL_SSEQ("mesh_peer_sel_policy", proc_get_mesh_peer_sel_policy, NULL), + RTW_PROC_HDL_SSEQ("mesh_networks", proc_get_mesh_networks, NULL), + RTW_PROC_HDL_SSEQ("mesh_plink_ctl", proc_get_mesh_plink_ctl, NULL), + RTW_PROC_HDL_SSEQ("mesh_mpath", proc_get_mesh_mpath, NULL), + RTW_PROC_HDL_SSEQ("mesh_mpp", proc_get_mesh_mpp, NULL), + RTW_PROC_HDL_SSEQ("mesh_known_gates", proc_get_mesh_known_gates, NULL), + #if CONFIG_RTW_MESH_DATA_BMC_TO_UC + RTW_PROC_HDL_SSEQ("mesh_b2u_flags", proc_get_mesh_b2u_flags, proc_set_mesh_b2u_flags), + #endif + RTW_PROC_HDL_SSEQ("mesh_stats", proc_get_mesh_stats, NULL), + RTW_PROC_HDL_SSEQ("mesh_gate_timeout_factor", proc_get_mesh_gate_timeout, proc_set_mesh_gate_timeout), + RTW_PROC_HDL_SSEQ("mesh_gate_state", proc_get_mesh_gate_state, NULL), + RTW_PROC_HDL_SSEQ("mesh_peer_alive_based_preq", proc_get_peer_alive_based_preq, proc_set_peer_alive_based_preq), +#endif +#ifdef CONFIG_FW_HANDLE_TXBCN + RTW_PROC_HDL_SSEQ("fw_tbtt_rpt", proc_get_fw_tbtt_rpt, proc_set_fw_tbtt_rpt), +#endif +#ifdef CONFIG_LPS_CHK_BY_TP + RTW_PROC_HDL_SSEQ("lps_chk_tp", proc_get_lps_chk_tp, proc_set_lps_chk_tp), +#endif +#ifdef CONFIG_SUPPORT_STATIC_SMPS + RTW_PROC_HDL_SSEQ("smps", proc_get_smps, proc_set_smps), +#endif + +#ifdef RTW_BUSY_DENY_SCAN + RTW_PROC_HDL_SSEQ("scan_interval_thr", proc_get_scan_interval_thr, \ + proc_set_scan_interval_thr), +#endif + RTW_PROC_HDL_SSEQ("scan_deny", proc_get_scan_deny, proc_set_scan_deny), +#ifdef CONFIG_RTW_TPT_MODE + RTW_PROC_HDL_SSEQ("tpt_mode", proc_get_tpt_mode, proc_set_tpt_mode), +#endif + +#ifdef CONFIG_CTRL_TXSS_BY_TP + RTW_PROC_HDL_SSEQ("txss_tp", proc_get_txss_tp, proc_set_txss_tp), + #ifdef DBG_CTRL_TXSS + RTW_PROC_HDL_SSEQ("txss_ctrl", proc_get_txss_ctrl, proc_set_txss_ctrl), + #endif +#endif + + RTW_PROC_HDL_SSEQ("cur_beacon_keys", proc_get_cur_beacon_keys, NULL), + +#ifdef CONFIG_WAR_OFFLOAD + RTW_PROC_HDL_SSEQ("war_offload_enable", proc_get_war_offload_enable, proc_set_war_offload_enable), + RTW_PROC_HDL_SSEQ("war_offload_ipv4_addr", NULL, proc_set_war_offload_ipv4_addr), + RTW_PROC_HDL_SSEQ("war_offload_ipv6_addr", NULL, proc_set_war_offload_ipv6_addr), +#if defined(CONFIG_OFFLOAD_MDNS_V4) || defined(CONFIG_OFFLOAD_MDNS_V6) + RTW_PROC_HDL_SSEQ("war_offload_mdns_domain_name", proc_get_war_offload_mdns_domain_name, proc_set_war_offload_mdns_domain_name), + RTW_PROC_HDL_SSEQ("war_offload_mdns_machine_name", proc_get_war_offload_mdns_machine_name, proc_set_war_offload_mdns_machine_name), + RTW_PROC_HDL_SSEQ("war_offload_mdns_service_info", proc_get_war_offload_mdns_service_info, proc_set_war_offload_mdns_service_info), + RTW_PROC_HDL_SSEQ("war_offload_mdns_service_info_txt_rsp", proc_get_war_offload_mdns_txt_rsp, proc_set_war_offload_mdns_txt_rsp), +#endif /* CONFIG_OFFLOAD_MDNS_V4 || CONFIG_OFFLOAD_MDNS_V6 */ +#endif /* CONFIG_WAR_OFFLOAD */ + RTW_PROC_HDL_SSEQ("rtw_amsdu_mode", proc_get_amsdu_mode, proc_set_amsdu_mode), + +}; + +const int adapter_proc_hdls_num = sizeof(adapter_proc_hdls) / sizeof(struct rtw_proc_hdl); + +static int rtw_adapter_proc_open(struct inode *inode, struct file *file) +{ + ssize_t index = (ssize_t)PDE_DATA(inode); + const struct rtw_proc_hdl *hdl = adapter_proc_hdls + index; + void *private = proc_get_parent_data(inode); + + if (hdl->type == RTW_PROC_HDL_TYPE_SEQ) { + int res = seq_open(file, hdl->u.seq_op); + + if (res == 0) + ((struct seq_file *)file->private_data)->private = private; + + return res; + } else if (hdl->type == RTW_PROC_HDL_TYPE_SSEQ) { + int (*show)(struct seq_file *, void *) = hdl->u.show ? hdl->u.show : proc_get_dummy; + + return single_open(file, show, private); + } else if (hdl->type == RTW_PROC_HDL_TYPE_SZSEQ) { + int (*show)(struct seq_file *, void *) = hdl->u.sz.show ? hdl->u.sz.show : proc_get_dummy; + + return single_open_size(file, show, private, hdl->u.sz.size); + } else { + return -EROFS; + } +} + +static ssize_t rtw_adapter_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) +{ + ssize_t index = (ssize_t)PDE_DATA(file_inode(file)); + const struct rtw_proc_hdl *hdl = adapter_proc_hdls + index; + ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *, void *) = hdl->write; + + if (write) + return write(file, buffer, count, pos, ((struct seq_file *)file->private_data)->private); + + return -EROFS; +} + +static const struct rtw_proc_ops rtw_adapter_proc_seq_fops = { +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)) + .proc_open = rtw_adapter_proc_open, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_release = seq_release, + .proc_write = rtw_adapter_proc_write, +#else + .owner = THIS_MODULE, + .open = rtw_adapter_proc_open, + .read = seq_read, + .llseek = seq_lseek, + .release = seq_release, + .write = rtw_adapter_proc_write, +#endif +}; + +static const struct rtw_proc_ops rtw_adapter_proc_sseq_fops = { +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)) + .proc_open = rtw_adapter_proc_open, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_release = single_release, + .proc_write = rtw_adapter_proc_write, +#else + .owner = THIS_MODULE, + .open = rtw_adapter_proc_open, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, + .write = rtw_adapter_proc_write, +#endif +}; + +int proc_get_odm_adaptivity(struct seq_file *m, void *v) +{ + struct net_device *dev = m->private; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + + rtw_odm_adaptivity_parm_msg(m, padapter); + + return 0; +} + +ssize_t proc_set_odm_adaptivity(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *dev = data; + _adapter *padapter = (_adapter *)rtw_netdev_priv(dev); + char tmp[32]; + u32 th_l2h_ini; + s8 th_edcca_hl_diff; + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) { + rtw_warn_on(1); + return -EFAULT; + } + + if (buffer && !copy_from_user(tmp, buffer, count)) { + + int num = sscanf(tmp, "%x %hhd", &th_l2h_ini, &th_edcca_hl_diff); + + if (num != 2) + return count; + + rtw_odm_adaptivity_parm_set(padapter, (s8)th_l2h_ini, th_edcca_hl_diff); + } + + return count; +} + +static char *phydm_msg = NULL; +#define PHYDM_MSG_LEN 80*24*4 + +int proc_get_phydm_cmd(struct seq_file *m, void *v) +{ + struct net_device *netdev; + PADAPTER padapter; + struct dm_struct *phydm; + + + netdev = m->private; + padapter = (PADAPTER)rtw_netdev_priv(netdev); + phydm = adapter_to_phydm(padapter); + + if (NULL == phydm_msg) { + phydm_msg = rtw_zmalloc(PHYDM_MSG_LEN); + if (NULL == phydm_msg) + return -ENOMEM; + + phydm_cmd(phydm, NULL, 0, 0, phydm_msg, PHYDM_MSG_LEN); + } + + _RTW_PRINT_SEL(m, "%s\n", phydm_msg); + + rtw_mfree(phydm_msg, PHYDM_MSG_LEN); + phydm_msg = NULL; + + return 0; +} + +ssize_t proc_set_phydm_cmd(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data) +{ + struct net_device *netdev; + PADAPTER padapter; + struct dm_struct *phydm; + char tmp[64] = {0}; + + + netdev = (struct net_device *)data; + padapter = (PADAPTER)rtw_netdev_priv(netdev); + phydm = adapter_to_phydm(padapter); + + if (count < 1) + return -EFAULT; + + if (count > sizeof(tmp)) + return -EFAULT; + + if (buffer && !copy_from_user(tmp, buffer, count)) { + if (NULL == phydm_msg) { + phydm_msg = rtw_zmalloc(PHYDM_MSG_LEN); + if (NULL == phydm_msg) + return -ENOMEM; + } else + _rtw_memset(phydm_msg, 0, PHYDM_MSG_LEN); + + phydm_cmd(phydm, tmp, count, 1, phydm_msg, PHYDM_MSG_LEN); + + if (strlen(phydm_msg) == 0) { + rtw_mfree(phydm_msg, PHYDM_MSG_LEN); + phydm_msg = NULL; + } + } + + return count; +} + +/* +* rtw_odm_proc: +* init/deinit when register/unregister net_device, along with rtw_adapter_proc +*/ +const struct rtw_proc_hdl odm_proc_hdls[] = { + RTW_PROC_HDL_SSEQ("adaptivity", proc_get_odm_adaptivity, proc_set_odm_adaptivity), + RTW_PROC_HDL_SZSEQ("cmd", proc_get_phydm_cmd, proc_set_phydm_cmd, PHYDM_MSG_LEN), +}; + +const int odm_proc_hdls_num = sizeof(odm_proc_hdls) / sizeof(struct rtw_proc_hdl); + +static int rtw_odm_proc_open(struct inode *inode, struct file *file) +{ + ssize_t index = (ssize_t)PDE_DATA(inode); + const struct rtw_proc_hdl *hdl = odm_proc_hdls + index; + void *private = proc_get_parent_data(inode); + + if (hdl->type == RTW_PROC_HDL_TYPE_SEQ) { + int res = seq_open(file, hdl->u.seq_op); + + if (res == 0) + ((struct seq_file *)file->private_data)->private = private; + + return res; + } else if (hdl->type == RTW_PROC_HDL_TYPE_SSEQ) { + int (*show)(struct seq_file *, void *) = hdl->u.show ? hdl->u.show : proc_get_dummy; + + return single_open(file, show, private); + } else if (hdl->type == RTW_PROC_HDL_TYPE_SZSEQ) { + int (*show)(struct seq_file *, void *) = hdl->u.sz.show ? hdl->u.sz.show : proc_get_dummy; + + return single_open_size(file, show, private, hdl->u.sz.size); + } else { + return -EROFS; + } +} + +static ssize_t rtw_odm_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) +{ + ssize_t index = (ssize_t)PDE_DATA(file_inode(file)); + const struct rtw_proc_hdl *hdl = odm_proc_hdls + index; + ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *, void *) = hdl->write; + + if (write) + return write(file, buffer, count, pos, ((struct seq_file *)file->private_data)->private); + + return -EROFS; +} + +static const struct rtw_proc_ops rtw_odm_proc_seq_fops = { +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)) + .proc_open = rtw_odm_proc_open, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_release = seq_release, + .proc_write = rtw_odm_proc_write, +#else + .owner = THIS_MODULE, + .open = rtw_odm_proc_open, + .read = seq_read, + .llseek = seq_lseek, + .release = seq_release, + .write = rtw_odm_proc_write, +#endif +}; + +static const struct rtw_proc_ops rtw_odm_proc_sseq_fops = { +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)) + .proc_open = rtw_odm_proc_open, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_release = single_release, + .proc_write = rtw_odm_proc_write, +#else + .owner = THIS_MODULE, + .open = rtw_odm_proc_open, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, + .write = rtw_odm_proc_write, +#endif +}; + +struct proc_dir_entry *rtw_odm_proc_init(struct net_device *dev) +{ + struct proc_dir_entry *dir_odm = NULL; + struct proc_dir_entry *entry = NULL; + _adapter *adapter = rtw_netdev_priv(dev); + ssize_t i; + + if (adapter->dir_dev == NULL) { + rtw_warn_on(1); + goto exit; + } + + if (adapter->dir_odm != NULL) { + rtw_warn_on(1); + goto exit; + } + + dir_odm = rtw_proc_create_dir("odm", adapter->dir_dev, dev); + if (dir_odm == NULL) { + rtw_warn_on(1); + goto exit; + } + + adapter->dir_odm = dir_odm; + + for (i = 0; i < odm_proc_hdls_num; i++) { + if (odm_proc_hdls[i].type == RTW_PROC_HDL_TYPE_SEQ) + entry = rtw_proc_create_entry(odm_proc_hdls[i].name, dir_odm, &rtw_odm_proc_seq_fops, (void *)i); + else if (odm_proc_hdls[i].type == RTW_PROC_HDL_TYPE_SSEQ || + odm_proc_hdls[i].type == RTW_PROC_HDL_TYPE_SZSEQ) + entry = rtw_proc_create_entry(odm_proc_hdls[i].name, dir_odm, &rtw_odm_proc_sseq_fops, (void *)i); + else + entry = NULL; + + if (!entry) { + rtw_warn_on(1); + goto exit; + } + } + +exit: + return dir_odm; +} + +void rtw_odm_proc_deinit(_adapter *adapter) +{ + struct proc_dir_entry *dir_odm = NULL; + int i; + + dir_odm = adapter->dir_odm; + + if (dir_odm == NULL) { + rtw_warn_on(1); + return; + } + + for (i = 0; i < odm_proc_hdls_num; i++) + remove_proc_entry(odm_proc_hdls[i].name, dir_odm); + + remove_proc_entry("odm", adapter->dir_dev); + + adapter->dir_odm = NULL; + + if (phydm_msg) { + rtw_mfree(phydm_msg, PHYDM_MSG_LEN); + phydm_msg = NULL; + } +} + +#ifdef CONFIG_MCC_MODE +/* +* rtw_mcc_proc: +* init/deinit when register/unregister net_device, along with rtw_adapter_proc +*/ +const struct rtw_proc_hdl mcc_proc_hdls[] = { + RTW_PROC_HDL_SSEQ("mcc_info", proc_get_mcc_info, NULL), + RTW_PROC_HDL_SSEQ("mcc_enable", proc_get_mcc_info, proc_set_mcc_enable), + RTW_PROC_HDL_SSEQ("mcc_duration", proc_get_mcc_info, proc_set_mcc_duration), + #ifdef CONFIG_MCC_PHYDM_OFFLOAD + RTW_PROC_HDL_SSEQ("mcc_phydm_offload", proc_get_mcc_info, proc_set_mcc_phydm_offload_enable), + #endif + RTW_PROC_HDL_SSEQ("mcc_single_tx_criteria", proc_get_mcc_info, proc_set_mcc_single_tx_criteria), + RTW_PROC_HDL_SSEQ("mcc_ap_bw20_target_tp", proc_get_mcc_info, proc_set_mcc_ap_bw20_target_tp), + RTW_PROC_HDL_SSEQ("mcc_ap_bw40_target_tp", proc_get_mcc_info, proc_set_mcc_ap_bw40_target_tp), + RTW_PROC_HDL_SSEQ("mcc_ap_bw80_target_tp", proc_get_mcc_info, proc_set_mcc_ap_bw80_target_tp), + RTW_PROC_HDL_SSEQ("mcc_sta_bw20_target_tp", proc_get_mcc_info, proc_set_mcc_sta_bw20_target_tp), + RTW_PROC_HDL_SSEQ("mcc_sta_bw40_target_tp", proc_get_mcc_info, proc_set_mcc_sta_bw40_target_tp), + RTW_PROC_HDL_SSEQ("mcc_sta_bw80_target_tp", proc_get_mcc_info, proc_set_mcc_sta_bw80_target_tp), + RTW_PROC_HDL_SSEQ("mcc_policy_table", proc_get_mcc_policy_table, NULL), +}; + +const int mcc_proc_hdls_num = sizeof(mcc_proc_hdls) / sizeof(struct rtw_proc_hdl); + +static int rtw_mcc_proc_open(struct inode *inode, struct file *file) +{ + ssize_t index = (ssize_t)PDE_DATA(inode); + const struct rtw_proc_hdl *hdl = mcc_proc_hdls + index; + void *private = proc_get_parent_data(inode); + + if (hdl->type == RTW_PROC_HDL_TYPE_SEQ) { + int res = seq_open(file, hdl->u.seq_op); + + if (res == 0) + ((struct seq_file *)file->private_data)->private = private; + + return res; + } else if (hdl->type == RTW_PROC_HDL_TYPE_SSEQ) { + int (*show)(struct seq_file *, void *) = hdl->u.show ? hdl->u.show : proc_get_dummy; + + return single_open(file, show, private); + } else if (hdl->type == RTW_PROC_HDL_TYPE_SZSEQ) { + int (*show)(struct seq_file *, void *) = hdl->u.sz.show ? hdl->u.sz.show : proc_get_dummy; + + return single_open_size(file, show, private, hdl->u.sz.size); + } else { + return -EROFS; + } +} + +static ssize_t rtw_mcc_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) +{ + ssize_t index = (ssize_t)PDE_DATA(file_inode(file)); + const struct rtw_proc_hdl *hdl = mcc_proc_hdls + index; + ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *, void *) = hdl->write; + + if (write) + return write(file, buffer, count, pos, ((struct seq_file *)file->private_data)->private); + + return -EROFS; +} + +static const struct rtw_proc_ops rtw_mcc_proc_seq_fops = { +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)) + .proc_open = rtw_mcc_proc_open, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_release = seq_release, + .proc_write = rtw_mcc_proc_write, +#else + .owner = THIS_MODULE, + .open = rtw_mcc_proc_open, + .read = seq_read, + .llseek = seq_lseek, + .release = seq_release, + .write = rtw_mcc_proc_write, +#endif +}; + +static const struct rtw_proc_ops rtw_mcc_proc_sseq_fops = { +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)) + .proc_open = rtw_mcc_proc_open, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_release = single_release, + .proc_write = rtw_mcc_proc_write, +#else + .owner = THIS_MODULE, + .open = rtw_mcc_proc_open, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, + .write = rtw_mcc_proc_write, +#endif +}; + +struct proc_dir_entry *rtw_mcc_proc_init(struct net_device *dev) +{ + struct proc_dir_entry *dir_mcc = NULL; + struct proc_dir_entry *entry = NULL; + _adapter *adapter = rtw_netdev_priv(dev); + ssize_t i; + + if (adapter->dir_dev == NULL) { + rtw_warn_on(1); + goto exit; + } + + if (adapter->dir_mcc != NULL) { + rtw_warn_on(1); + goto exit; + } + + dir_mcc = rtw_proc_create_dir("mcc", adapter->dir_dev, dev); + if (dir_mcc == NULL) { + rtw_warn_on(1); + goto exit; + } + + adapter->dir_mcc = dir_mcc; + + for (i = 0; i < mcc_proc_hdls_num; i++) { + if (mcc_proc_hdls[i].type == RTW_PROC_HDL_TYPE_SEQ) + entry = rtw_proc_create_entry(mcc_proc_hdls[i].name, dir_mcc, &rtw_mcc_proc_seq_fops, (void *)i); + else if (mcc_proc_hdls[i].type == RTW_PROC_HDL_TYPE_SSEQ || + mcc_proc_hdls[i].type == RTW_PROC_HDL_TYPE_SZSEQ) + entry = rtw_proc_create_entry(mcc_proc_hdls[i].name, dir_mcc, &rtw_mcc_proc_sseq_fops, (void *)i); + else + entry = NULL; + + if (!entry) { + rtw_warn_on(1); + goto exit; + } + } + +exit: + return dir_mcc; +} + +void rtw_mcc_proc_deinit(_adapter *adapter) +{ + struct proc_dir_entry *dir_mcc = NULL; + int i; + + dir_mcc = adapter->dir_mcc; + + if (dir_mcc == NULL) { + rtw_warn_on(1); + return; + } + + for (i = 0; i < mcc_proc_hdls_num; i++) + remove_proc_entry(mcc_proc_hdls[i].name, dir_mcc); + + remove_proc_entry("mcc", adapter->dir_dev); + + adapter->dir_mcc = NULL; +} +#endif /* CONFIG_MCC_MODE */ + +struct proc_dir_entry *rtw_adapter_proc_init(struct net_device *dev) +{ + struct proc_dir_entry *drv_proc = get_rtw_drv_proc(); + struct proc_dir_entry *dir_dev = NULL; + struct proc_dir_entry *entry = NULL; + _adapter *adapter = rtw_netdev_priv(dev); + ssize_t i; + + if (drv_proc == NULL) { + rtw_warn_on(1); + goto exit; + } + + if (adapter->dir_dev != NULL) { + rtw_warn_on(1); + goto exit; + } + + dir_dev = rtw_proc_create_dir(dev->name, drv_proc, dev); + if (dir_dev == NULL) { + rtw_warn_on(1); + goto exit; + } + + adapter->dir_dev = dir_dev; + + for (i = 0; i < adapter_proc_hdls_num; i++) { + if (adapter_proc_hdls[i].type == RTW_PROC_HDL_TYPE_SEQ) + entry = rtw_proc_create_entry(adapter_proc_hdls[i].name, dir_dev, &rtw_adapter_proc_seq_fops, (void *)i); + else if (adapter_proc_hdls[i].type == RTW_PROC_HDL_TYPE_SSEQ || + adapter_proc_hdls[i].type == RTW_PROC_HDL_TYPE_SZSEQ) + entry = rtw_proc_create_entry(adapter_proc_hdls[i].name, dir_dev, &rtw_adapter_proc_sseq_fops, (void *)i); + else + entry = NULL; + + if (!entry) { + rtw_warn_on(1); + goto exit; + } + } + + rtw_odm_proc_init(dev); + +#ifdef CONFIG_MCC_MODE + rtw_mcc_proc_init(dev); +#endif /* CONFIG_MCC_MODE */ + +exit: + return dir_dev; +} + +void rtw_adapter_proc_deinit(struct net_device *dev) +{ + struct proc_dir_entry *drv_proc = get_rtw_drv_proc(); + struct proc_dir_entry *dir_dev = NULL; + _adapter *adapter = rtw_netdev_priv(dev); + int i; + + dir_dev = adapter->dir_dev; + + if (dir_dev == NULL) { + rtw_warn_on(1); + return; + } + + for (i = 0; i < adapter_proc_hdls_num; i++) + remove_proc_entry(adapter_proc_hdls[i].name, dir_dev); + + rtw_odm_proc_deinit(adapter); + +#ifdef CONFIG_MCC_MODE + rtw_mcc_proc_deinit(adapter); +#endif /* CONFIG_MCC_MODE */ + + remove_proc_entry(dev->name, drv_proc); + + adapter->dir_dev = NULL; +} + +void rtw_adapter_proc_replace(struct net_device *dev) +{ + struct proc_dir_entry *drv_proc = get_rtw_drv_proc(); + struct proc_dir_entry *dir_dev = NULL; + _adapter *adapter = rtw_netdev_priv(dev); + int i; + + dir_dev = adapter->dir_dev; + + if (dir_dev == NULL) { + rtw_warn_on(1); + return; + } + + for (i = 0; i < adapter_proc_hdls_num; i++) + remove_proc_entry(adapter_proc_hdls[i].name, dir_dev); + + rtw_odm_proc_deinit(adapter); + +#ifdef CONFIG_MCC_MODE + rtw_mcc_proc_deinit(adapter); +#endif /* CONIG_MCC_MODE */ + + remove_proc_entry(adapter->old_ifname, drv_proc); + + adapter->dir_dev = NULL; + + rtw_adapter_proc_init(dev); + +} + +#endif /* CONFIG_PROC_DEBUG */ diff --git a/os_dep/linux/rtw_proc.h b/os_dep/linux/rtw_proc.h new file mode 100644 index 0000000..9247e36 --- /dev/null +++ b/os_dep/linux/rtw_proc.h @@ -0,0 +1,72 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#ifndef __RTW_PROC_H__ +#define __RTW_PROC_H__ + +#include +#include + +#define RTW_PROC_HDL_TYPE_SEQ 0 +#define RTW_PROC_HDL_TYPE_SSEQ 1 +#define RTW_PROC_HDL_TYPE_SZSEQ 2 + +struct rtw_proc_hdl { + char *name; + u8 type; + union { + int (*show)(struct seq_file *, void *); + struct seq_operations *seq_op; + struct { + int (*show)(struct seq_file *, void *); + size_t size; + } sz; + } u; + ssize_t (*write)(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data); +}; + +#define RTW_PROC_HDL_SEQ(_name, _seq_op, _write) \ + { .name = _name, .type = RTW_PROC_HDL_TYPE_SEQ, .u.seq_op = _seq_op, .write = _write} + +#define RTW_PROC_HDL_SSEQ(_name, _show, _write) \ + { .name = _name, .type = RTW_PROC_HDL_TYPE_SSEQ, .u.show = _show, .write = _write} + +#define RTW_PROC_HDL_SZSEQ(_name, _show, _write, _size) \ + { .name = _name, .type = RTW_PROC_HDL_TYPE_SZSEQ, .u.sz.show = _show, .write = _write, .u.sz.size = _size} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)) +#define rtw_proc_ops proc_ops +#else +#define rtw_proc_ops file_operations +#endif + +#ifdef CONFIG_PROC_DEBUG + +int rtw_drv_proc_init(void); +void rtw_drv_proc_deinit(void); +struct proc_dir_entry *rtw_adapter_proc_init(struct net_device *dev); +void rtw_adapter_proc_deinit(struct net_device *dev); +void rtw_adapter_proc_replace(struct net_device *dev); + +#else /* !CONFIG_PROC_DEBUG */ + +static inline int rtw_drv_proc_init(void) {return _FAIL;} +#define rtw_drv_proc_deinit() do {} while (0) +static inline struct proc_dir_entry *rtw_adapter_proc_init(struct net_device *dev) {return NULL;} +#define rtw_adapter_proc_deinit(dev) do {} while (0) +#define rtw_adapter_proc_replace(dev) do {} while (0) + +#endif /* !CONFIG_PROC_DEBUG */ + +#endif /* __RTW_PROC_H__ */ diff --git a/os_dep/linux/rtw_rhashtable.c b/os_dep/linux/rtw_rhashtable.c new file mode 100644 index 0000000..df303b2 --- /dev/null +++ b/os_dep/linux/rtw_rhashtable.c @@ -0,0 +1,77 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ + +#include + +#if defined(CONFIG_RTW_WDS) || defined(CONFIG_RTW_MESH) /* for now, only promised for kernel versions we support mesh */ + +int rtw_rhashtable_walk_enter(rtw_rhashtable *ht, rtw_rhashtable_iter *iter) +{ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)) + rhashtable_walk_enter((ht), (iter)); + return 0; +#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)) + return rhashtable_walk_init((ht), (iter), GFP_ATOMIC); +#else + /* kernel >= 4.4.0 rhashtable_walk_init use GFP_KERNEL to alloc, spin_lock for assignment */ + iter->ht = ht; + iter->p = NULL; + iter->slot = 0; + iter->skip = 0; + + iter->walker = kmalloc(sizeof(*iter->walker), GFP_ATOMIC); + if (!iter->walker) + return -ENOMEM; + + spin_lock(&ht->lock); + iter->walker->tbl = + rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock)); + list_add(&iter->walker->list, &iter->walker->tbl->walkers); + spin_unlock(&ht->lock); + + return 0; +#endif +} + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)) +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0)) +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)) +static inline int is_vmalloc_addr(const void *x) +{ +#ifdef CONFIG_MMU + unsigned long addr = (unsigned long)x; + + return addr >= VMALLOC_START && addr < VMALLOC_END; +#else + return 0; +#endif +} +#endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)) */ + +void kvfree(const void *addr) +{ + if (is_vmalloc_addr(addr)) + vfree(addr); + else + kfree(addr); +} +#endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0)) */ + +#include "rhashtable.c" + +#endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)) */ + +#endif /* defined(CONFIG_RTW_WDS) || defined(CONFIG_RTW_MESH) */ + diff --git a/os_dep/linux/rtw_rhashtable.h b/os_dep/linux/rtw_rhashtable.h new file mode 100644 index 0000000..af5ba7e --- /dev/null +++ b/os_dep/linux/rtw_rhashtable.h @@ -0,0 +1,67 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#ifndef __RTW_RHASHTABLE_H__ +#define __RTW_RHASHTABLE_H__ + +#if defined(CONFIG_RTW_WDS) || defined(CONFIG_RTW_MESH) /* for now, only promised for kernel versions we support mesh */ + +/* directly reference rhashtable in kernel */ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) +#include +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) */ + +/* Use rhashtable from kernel 4.4 */ +#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)) +#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)) +#define NULLS_MARKER(value) (1UL | (((long)value) << 1)) +#endif +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)) +static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags) +{ + if (size != 0 && n > ULONG_MAX / size) + return NULL; + return __kmalloc(n * size, flags); +} +#endif +#include "rhashtable.h" +#endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)) */ + +typedef struct rhashtable rtw_rhashtable; +typedef struct rhash_head rtw_rhash_head; +typedef struct rhashtable_params rtw_rhashtable_params; + +#define rtw_rhashtable_init(ht, params) rhashtable_init(ht, params) + +typedef struct rhashtable_iter rtw_rhashtable_iter; + +int rtw_rhashtable_walk_enter(rtw_rhashtable *ht, rtw_rhashtable_iter *iter); +#define rtw_rhashtable_walk_exit(iter) rhashtable_walk_exit(iter) +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)) +#define rtw_rhashtable_walk_start(iter) rhashtable_walk_start_check(iter) +#else +#define rtw_rhashtable_walk_start(iter) rhashtable_walk_start(iter) +#endif +#define rtw_rhashtable_walk_next(iter) rhashtable_walk_next(iter) +#define rtw_rhashtable_walk_stop(iter) rhashtable_walk_stop(iter) + +#define rtw_rhashtable_free_and_destroy(ht, free_fn, arg) rhashtable_free_and_destroy((ht), (free_fn), (arg)) +#define rtw_rhashtable_lookup_fast(ht, key, params) rhashtable_lookup_fast((ht), (key), (params)) +#define rtw_rhashtable_lookup_insert_fast(ht, obj, params) rhashtable_lookup_insert_fast((ht), (obj), (params)) +#define rtw_rhashtable_remove_fast(ht, obj, params) rhashtable_remove_fast((ht), (obj), (params)) + +#endif /* defined(CONFIG_RTW_WDS) || defined(CONFIG_RTW_MESH) */ + +#endif /* __RTW_RHASHTABLE_H__ */ + diff --git a/os_dep/linux/usb_intf.c b/os_dep/linux/usb_intf.c new file mode 100644 index 0000000..572b8ff --- /dev/null +++ b/os_dep/linux/usb_intf.c @@ -0,0 +1,1505 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#define _HCI_INTF_C_ + +#include +#include + +#include + +#ifndef CONFIG_USB_HCI +#error "CONFIG_USB_HCI shall be on!\n" +#endif + +#ifdef CONFIG_80211N_HT +extern int rtw_ht_enable; +extern int rtw_bw_mode; +extern int rtw_ampdu_enable;/* for enable tx_ampdu */ +#endif + +#ifdef CONFIG_GLOBAL_UI_PID +int ui_pid[3] = {0, 0, 0}; +#endif + + +extern int pm_netdev_open(struct net_device *pnetdev, u8 bnormal); +static int rtw_suspend(struct usb_interface *intf, pm_message_t message); +static int rtw_resume(struct usb_interface *intf); + + +static int rtw_drv_init(struct usb_interface *pusb_intf, const struct usb_device_id *pdid); +static void rtw_dev_remove(struct usb_interface *pusb_intf); + +static void rtw_dev_shutdown(struct device *dev) +{ + struct usb_interface *usb_intf = container_of(dev, struct usb_interface, dev); + struct dvobj_priv *dvobj = NULL; + _adapter *adapter = NULL; + + RTW_INFO("%s\n", __func__); + + if (usb_intf) { + dvobj = usb_get_intfdata(usb_intf); + if (dvobj) { + adapter = dvobj_get_primary_adapter(dvobj); + if (adapter) { + if (!rtw_is_surprise_removed(adapter)) { + #ifdef CONFIG_WOWLAN + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(adapter); + + #ifdef CONFIG_GPIO_WAKEUP + /*default wake up pin change to BT*/ + RTW_INFO("%s:default wake up pin change to BT\n", __FUNCTION__); + rtw_hal_switch_gpio_wl_ctrl(adapter, pwrctl->wowlan_gpio_index, _FALSE); + #endif /* CONFIG_GPIO_WAKEUP */ + + if (pwrctl->wowlan_mode == _TRUE) + RTW_PRINT("%s wowlan_mode ==_TRUE do not run rtw_hal_deinit()\n", __FUNCTION__); + else + #endif + { + #ifdef CONFIG_BT_COEXIST + RTW_INFO("%s call halt notify\n", __FUNCTION__); + rtw_btcoex_HaltNotify(adapter); + #endif + rtw_hal_deinit(adapter); + rtw_set_surprise_removed(adapter); + } + } + } + ATOMIC_SET(&dvobj->continual_io_error, MAX_CONTINUAL_IO_ERR + 1); + } + } +} + +#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 23)) +/* Some useful macros to use to create struct usb_device_id */ +#define USB_DEVICE_ID_MATCH_VENDOR 0x0001 +#define USB_DEVICE_ID_MATCH_PRODUCT 0x0002 +#define USB_DEVICE_ID_MATCH_DEV_LO 0x0004 +#define USB_DEVICE_ID_MATCH_DEV_HI 0x0008 +#define USB_DEVICE_ID_MATCH_DEV_CLASS 0x0010 +#define USB_DEVICE_ID_MATCH_DEV_SUBCLASS 0x0020 +#define USB_DEVICE_ID_MATCH_DEV_PROTOCOL 0x0040 +#define USB_DEVICE_ID_MATCH_INT_CLASS 0x0080 +#define USB_DEVICE_ID_MATCH_INT_SUBCLASS 0x0100 +#define USB_DEVICE_ID_MATCH_INT_PROTOCOL 0x0200 +#define USB_DEVICE_ID_MATCH_INT_NUMBER 0x0400 + + +#define USB_DEVICE_ID_MATCH_INT_INFO \ + (USB_DEVICE_ID_MATCH_INT_CLASS | \ + USB_DEVICE_ID_MATCH_INT_SUBCLASS | \ + USB_DEVICE_ID_MATCH_INT_PROTOCOL) + + +#define USB_DEVICE_AND_INTERFACE_INFO(vend, prod, cl, sc, pr) \ + .match_flags = USB_DEVICE_ID_MATCH_INT_INFO \ + | USB_DEVICE_ID_MATCH_DEVICE, \ + .idVendor = (vend), \ + .idProduct = (prod), \ + .bInterfaceClass = (cl), \ + .bInterfaceSubClass = (sc), \ + .bInterfaceProtocol = (pr) + +/** + * USB_VENDOR_AND_INTERFACE_INFO - describe a specific usb vendor with a class of usb interfaces + * @vend: the 16 bit USB Vendor ID + * @cl: bInterfaceClass value + * @sc: bInterfaceSubClass value + * @pr: bInterfaceProtocol value + * + * This macro is used to create a struct usb_device_id that matches a + * specific vendor with a specific class of interfaces. + * + * This is especially useful when explicitly matching devices that have + * vendor specific bDeviceClass values, but standards-compliant interfaces. + */ +#define USB_VENDOR_AND_INTERFACE_INFO(vend, cl, sc, pr) \ + .match_flags = USB_DEVICE_ID_MATCH_INT_INFO \ + | USB_DEVICE_ID_MATCH_VENDOR, \ + .idVendor = (vend), \ + .bInterfaceClass = (cl), \ + .bInterfaceSubClass = (sc), \ + .bInterfaceProtocol = (pr) + +/* ----------------------------------------------------------------------- */ +#endif + + +#define USB_VENDER_ID_EDIMAX 0x7392 +#define USB_VENDER_ID_REALTEK 0x0BDA + + +/* DID_USB_v916_20130116 */ +static struct usb_device_id rtw_usb_id_tbl[] = { +#ifdef CONFIG_RTL8188E + /*=== Realtek demoboard ===*/ + {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8179), .driver_info = RTL8188E}, /* 8188EUS */ + {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x0179), .driver_info = RTL8188E}, /* 8188ETV */ + /*=== Customer ID ===*/ + /****** 8188EUS ********/ + {USB_DEVICE(0x07B8, 0x8179), .driver_info = RTL8188E}, /* Abocom - Abocom */ + {USB_DEVICE(USB_VENDER_ID_EDIMAX, 0xb811), .driver_info = RTL8188E}, /* Edimax */ + {USB_DEVICE(0x0DF6, 0x0076), .driver_info = RTL8188E}, /* Sitecom N150 v2 */ + {USB_DEVICE(0x2001, 0x330F), .driver_info = RTL8188E}, /* D-Link DWA-125 rev D1 */ + {USB_DEVICE(0x2001, 0x3310), .driver_info = RTL8188E}, /* D-Link DWA-123 rev D1 */ + {USB_DEVICE(0x2001, 0x3311), .driver_info = RTL8188E}, /* D-Link GO-USB-N150 rev B1 */ + {USB_DEVICE(0x2001, 0x331B), .driver_info = RTL8188E}, /* D-Link DWA-121 rev B1 */ + {USB_DEVICE(0x056E, 0x4008), .driver_info = RTL8188E}, /* Elecom WDC-150SU2M */ + {USB_DEVICE(0x2357, 0x010C), .driver_info = RTL8188E}, /* TPLINK - TL-WN722N */ + {USB_DEVICE(0x2357, 0x0111), .driver_info = RTL8188E}, /* TPLINK - TL-WN727N */ + {USB_DEVICE(0x0BDA, 0x8179), .driver_info = RTL8188E}, /* TPLINK - TL-WN725N */ + {USB_DEVICE(0x2C4E, 0x0102), .driver_info = RTL8188E}, /* MERCUSYS MW150US v2 */ + {USB_DEVICE(0x0B05, 0x18F0), .driver_info = RTL8188E}, /* ASUS USB-N10 Nano B1 */ + {USB_DEVICE(0x0bda, 0xffef), .driver_info = RTL8188E}, /* Rosewill RNX-N150NUB */ +#endif + +#ifdef CONFIG_RTL8812A + /*=== Realtek demoboard ===*/ + {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8812), .driver_info = RTL8812}, /* Default ID */ + {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x881A), .driver_info = RTL8812}, /* Default ID */ + {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x881B), .driver_info = RTL8812}, /* Default ID */ + {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x881C), .driver_info = RTL8812}, /* Default ID */ + /*=== Customer ID ===*/ + {USB_DEVICE(0x050D, 0x1106), .driver_info = RTL8812}, /* Belkin - sercomm */ + {USB_DEVICE(0x2001, 0x330E), .driver_info = RTL8812}, /* D-Link - ALPHA */ + {USB_DEVICE(0x7392, 0xA822), .driver_info = RTL8812}, /* Edimax - Edimax */ + {USB_DEVICE(0x0DF6, 0x0074), .driver_info = RTL8812}, /* Sitecom - Edimax */ + {USB_DEVICE(0x04BB, 0x0952), .driver_info = RTL8812}, /* I-O DATA - Edimax */ + {USB_DEVICE(0x0789, 0x016E), .driver_info = RTL8812}, /* Logitec - Edimax */ + {USB_DEVICE(0x0409, 0x0408), .driver_info = RTL8812}, /* NEC - */ + {USB_DEVICE(0x0B05, 0x17D2), .driver_info = RTL8812}, /* ASUS - Edimax */ + {USB_DEVICE(0x0E66, 0x0022), .driver_info = RTL8812}, /* HAWKING - Edimax */ + {USB_DEVICE(0x0586, 0x3426), .driver_info = RTL8812}, /* ZyXEL - */ + {USB_DEVICE(0x2001, 0x3313), .driver_info = RTL8812}, /* D-Link - ALPHA */ + {USB_DEVICE(0x1058, 0x0632), .driver_info = RTL8812}, /* WD - Cybertan*/ + {USB_DEVICE(0x1740, 0x0100), .driver_info = RTL8812}, /* EnGenius - EnGenius */ + {USB_DEVICE(0x2019, 0xAB30), .driver_info = RTL8812}, /* Planex - Abocom */ + {USB_DEVICE(0x07B8, 0x8812), .driver_info = RTL8812}, /* Abocom - Abocom */ + {USB_DEVICE(0x2001, 0x3315), .driver_info = RTL8812}, /* D-Link - Cameo */ + {USB_DEVICE(0x2001, 0x3316), .driver_info = RTL8812}, /* D-Link - Cameo */ +#endif + +#ifdef CONFIG_RTL8821A + /*=== Realtek demoboard ===*/ + {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x0811), .driver_info = RTL8821}, /* Default ID */ + {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x0821), .driver_info = RTL8821}, /* Default ID */ + {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8822), .driver_info = RTL8821}, /* Default ID */ + {USB_DEVICE(USB_VENDER_ID_REALTEK, 0xA811) , .driver_info = RTL8821},/* Default ID */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0x0820, 0xff, 0xff, 0xff), .driver_info = RTL8821}, /* 8821AU */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0x0823, 0xff, 0xff, 0xff), .driver_info = RTL8821}, /* 8821AU */ + /*=== Customer ID ===*/ + {USB_DEVICE(0x7392, 0xA811), .driver_info = RTL8821}, /* Edimax - Edimax */ + {USB_DEVICE(0x04BB, 0x0953), .driver_info = RTL8821}, /* I-O DATA - Edimax */ + {USB_DEVICE(0x2001, 0x3314), .driver_info = RTL8821}, /* D-Link - Cameo */ + {USB_DEVICE(0x2001, 0x3318), .driver_info = RTL8821}, /* D-Link - Cameo */ + {USB_DEVICE(0x0E66, 0x0023), .driver_info = RTL8821}, /* HAWKING - Edimax */ + {USB_DEVICE(0x056E, 0x400E) , .driver_info = RTL8821}, /* ELECOM - ELECOM */ + {USB_DEVICE(0x056E, 0x400F) , .driver_info = RTL8821}, /* ELECOM - ELECOM */ + {USB_DEVICE(0x20f4, 0x804b), .driver_info = RTL8821}, /* TRENDnet */ +#endif + +#ifdef CONFIG_RTL8192E + /*=== Realtek demoboard ===*/ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0x818B, 0xff, 0xff, 0xff), .driver_info = RTL8192E}, /* Default ID */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0x818C, 0xff, 0xff, 0xff), .driver_info = RTL8192E}, /* Default ID */ +#endif + +#ifdef CONFIG_RTL8723B + /* === Realtek demoboard === */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xB720, 0xff, 0xff, 0xff), .driver_info = RTL8723B}, /* 8723BU 1*1 */ + /* {USB_DEVICE(USB_VENDER_ID_REALTEK, 0xB720),.driver_info = RTL8723B}, 8723BU */ +#endif + +#ifdef CONFIG_RTL8703B + /*=== Realtek demoboard ===*/ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xB703, 0xff, 0xff, 0xff), .driver_info = RTL8703B}, /* 8723CU 1*1 */ + /* {USB_DEVICE(USB_VENDER_ID_REALTEK, 0xB703), .driver_info = RTL723C}, */ /* 8723CU 1*1 */ +#endif /* CONFIG_RTL8703B */ + +#ifdef CONFIG_RTL8814A + + {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8813), .driver_info = RTL8814A}, + {USB_DEVICE(0x2001, 0x331a), .driver_info = RTL8814A}, /* D-Link - D-Link */ + {USB_DEVICE(0x0b05, 0x1817), .driver_info = RTL8814A}, /* ASUS - ASUSTeK */ + {USB_DEVICE(0x056E, 0x400B), .driver_info = RTL8814A}, /* ELECOM - ELECOM */ + {USB_DEVICE(0x056E, 0x400D), .driver_info = RTL8814A}, /* ELECOM - ELECOM */ + {USB_DEVICE(0x7392, 0xA834), .driver_info = RTL8814A}, /* Edimax - Edimax */ +#endif /* CONFIG_RTL8814A */ + +#ifdef CONFIG_RTL8188F + /*=== Realtek demoboard ===*/ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xF179, 0xff, 0xff, 0xff), .driver_info = RTL8188F}, /* 8188FU 1*1 */ +#endif + +#ifdef CONFIG_RTL8188GTV + /*=== Realtek demoboard ===*/ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0x018C, 0xff, 0xff, 0xff), .driver_info = RTL8188GTV}, /* 8188GTV 1*1 */ +#endif + +#ifdef CONFIG_RTL8822B + /*=== Realtek demoboard ===*/ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xB82C, 0xff, 0xff, 0xff), .driver_info = RTL8822B}, /* Default ID for USB multi-function */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xB812, 0xff, 0xff, 0xff), .driver_info = RTL8822B}, /* Default ID for USB Single-function, WiFi only */ + /*=== Customer ID ===*/ + {USB_DEVICE_AND_INTERFACE_INFO(0x13b1, 0x0043, 0xff, 0xff, 0xff), .driver_info = RTL8822B}, /* Alpha - Alpha*/ +#endif /* CONFIG_RTL8822B */ + +#ifdef CONFIG_RTL8723D + /*=== Realtek demoboard ===*/ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xD723, 0xff, 0xff, 0xff), .driver_info = RTL8723D}, /* 8723DU 1*1 */ +#endif + +#ifdef CONFIG_RTL8192F + /*=== Realtek demoboard ===*/ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xF192, 0xff, 0xff, 0xff), .driver_info = RTL8192F}, /* 8192FU 2*2 */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xA725, 0xff, 0xff, 0xff), .driver_info = RTL8192F}, /* 8725AU 2*2 */ +#endif + +#ifdef CONFIG_RTL8821C + /*=== Realtek demoboard ===*/ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xb82b, 0xff, 0xff, 0xff), .driver_info = RTL8821C}, /* 8821CU */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xb820, 0xff, 0xff, 0xff), .driver_info = RTL8821C}, /* 8821CU */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xC821, 0xff, 0xff, 0xff), .driver_info = RTL8821C}, /* 8821CU */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xC820, 0xff, 0xff, 0xff), .driver_info = RTL8821C}, /* 8821CU */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xC82A, 0xff, 0xff, 0xff), .driver_info = RTL8821C}, /* 8821CU */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xC82B, 0xff, 0xff, 0xff), .driver_info = RTL8821C}, /* 8821CU */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xC811, 0xff, 0xff, 0xff), .driver_info = RTL8821C}, /* 8811CU */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0x8811, 0xff, 0xff, 0xff), .driver_info = RTL8821C}, /* 8811CU */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0x8731, 0xff, 0xff, 0xff), .driver_info = RTL8821C}, /* 8731AU */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xC80C, 0xff, 0xff, 0xff), .driver_info = RTL8821C}, /* 8821CUH */ + /*=== Customer ID ===*/ +#endif + +#ifdef CONFIG_RTL8710B + /*=== Realtek dongle ===*/ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xB711, 0xff, 0xff, 0xff), .driver_info = RTL8710B}, /* 8710B = 8188GU 1*1 */ +#endif + +#ifdef CONFIG_RTL8822C + /*=== Realtek demoboard ===*/ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xC82C, 0xff, 0xff, 0xff), .driver_info = RTL8822C}, /* Default ID for USB multi-function */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xC82E, 0xff, 0xff, 0xff), .driver_info = RTL8822C}, /* Default ID for USB multi-function */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xC812, 0xff, 0xff, 0xff), .driver_info = RTL8822C}, /* Default ID for USB Single-function, WiFi only */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xD820, 0xff, 0xff, 0xff), .driver_info = RTL8822C}, /* 21D USB multi-fuction*/ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xD82B, 0xff, 0xff, 0xff), .driver_info = RTL8822C}, /* 21D USB Single-fuction, WiFi only*/ + /*=== Customer ID ===*/ + {USB_DEVICE_AND_INTERFACE_INFO(0x13b1, 0x0043, 0xff, 0xff, 0xff), .driver_info = RTL8822C}, /* Alpha - Alpha*/ +#endif /* CONFIG_RTL8822C */ + +#ifdef CONFIG_RTL8814B + /*=== Realtek demoboard ===*/ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xB814, 0xff, 0xff, 0xff), .driver_info = RTL8814B}, /* Default ID for USB multi-function */ +#endif /* CONFIG_RTL8814B */ +#ifdef CONFIG_RTL8723F + /*=== Realtek IC ===*/ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xB733, 0xff, 0xff, 0xff), .driver_info = RTL8723F}, /* USB multi-fuction */ + {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDER_ID_REALTEK, 0xF72B, 0xff, 0xff, 0xff), .driver_info = RTL8723F}, /* USB Single-fuction, WiFi only */ +#endif + + {} /* Terminating entry */ +}; + +MODULE_DEVICE_TABLE(usb, rtw_usb_id_tbl); + +int const rtw_usb_id_len = sizeof(rtw_usb_id_tbl) / sizeof(struct usb_device_id); + +static struct specific_device_id specific_device_id_tbl[] = { + {.idVendor = USB_VENDER_ID_REALTEK, .idProduct = 0x8177, .flags = SPEC_DEV_ID_DISABLE_HT}, /* 8188cu 1*1 dongole, (b/g mode only) */ + {.idVendor = USB_VENDER_ID_REALTEK, .idProduct = 0x817E, .flags = SPEC_DEV_ID_DISABLE_HT}, /* 8188CE-VAU USB minCard (b/g mode only) */ + {.idVendor = 0x0b05, .idProduct = 0x1791, .flags = SPEC_DEV_ID_DISABLE_HT}, + {.idVendor = 0x13D3, .idProduct = 0x3311, .flags = SPEC_DEV_ID_DISABLE_HT}, + {.idVendor = 0x13D3, .idProduct = 0x3359, .flags = SPEC_DEV_ID_DISABLE_HT}, /* Russian customer -Azwave (8188CE-VAU g mode) */ +#ifdef RTK_DMP_PLATFORM + {.idVendor = USB_VENDER_ID_REALTEK, .idProduct = 0x8111, .flags = SPEC_DEV_ID_ASSIGN_IFNAME}, /* Realtek 5G dongle for WiFi Display */ + {.idVendor = 0x2019, .idProduct = 0xAB2D, .flags = SPEC_DEV_ID_ASSIGN_IFNAME}, /* PCI-Abocom 5G dongle for WiFi Display */ +#endif /* RTK_DMP_PLATFORM */ + {} +}; + +struct rtw_usb_drv { + struct usb_driver usbdrv; + int drv_registered; + u8 hw_type; +}; + +struct rtw_usb_drv usb_drv = { + .usbdrv.name = (char *)DRV_NAME, + .usbdrv.probe = rtw_drv_init, + .usbdrv.disconnect = rtw_dev_remove, + .usbdrv.id_table = rtw_usb_id_tbl, + .usbdrv.suspend = rtw_suspend, + .usbdrv.resume = rtw_resume, +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22)) + .usbdrv.reset_resume = rtw_resume, +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)) + .usbdrv.drvwrap.driver.shutdown = rtw_dev_shutdown, +#else + .usbdrv.driver.shutdown = rtw_dev_shutdown, +#endif +}; + +static inline int RT_usb_endpoint_dir_in(const struct usb_endpoint_descriptor *epd) +{ + return (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN; +} + +static inline int RT_usb_endpoint_dir_out(const struct usb_endpoint_descriptor *epd) +{ + return (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT; +} + +static inline int RT_usb_endpoint_xfer_int(const struct usb_endpoint_descriptor *epd) +{ + return (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT; +} + +static inline int RT_usb_endpoint_xfer_bulk(const struct usb_endpoint_descriptor *epd) +{ + return (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK; +} + +static inline int RT_usb_endpoint_is_bulk_in(const struct usb_endpoint_descriptor *epd) +{ + return RT_usb_endpoint_xfer_bulk(epd) && RT_usb_endpoint_dir_in(epd); +} + +static inline int RT_usb_endpoint_is_bulk_out(const struct usb_endpoint_descriptor *epd) +{ + return RT_usb_endpoint_xfer_bulk(epd) && RT_usb_endpoint_dir_out(epd); +} + +static inline int RT_usb_endpoint_is_int_in(const struct usb_endpoint_descriptor *epd) +{ + return RT_usb_endpoint_xfer_int(epd) && RT_usb_endpoint_dir_in(epd); +} + +static inline int RT_usb_endpoint_num(const struct usb_endpoint_descriptor *epd) +{ + return epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; +} + +static u8 rtw_init_intf_priv(struct dvobj_priv *dvobj) +{ + u8 rst = _SUCCESS; + +#ifdef CONFIG_USB_VENDOR_REQ_MUTEX + _rtw_mutex_init(&dvobj->usb_vendor_req_mutex); +#endif + + +#ifdef CONFIG_USB_VENDOR_REQ_BUFFER_PREALLOC + dvobj->usb_alloc_vendor_req_buf = rtw_zmalloc(MAX_USB_IO_CTL_SIZE); + if (dvobj->usb_alloc_vendor_req_buf == NULL) { + RTW_INFO("alloc usb_vendor_req_buf failed... /n"); + rst = _FAIL; + goto exit; + } + dvobj->usb_vendor_req_buf = + (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(dvobj->usb_alloc_vendor_req_buf), ALIGNMENT_UNIT); +exit: +#endif + + return rst; + +} + +static u8 rtw_deinit_intf_priv(struct dvobj_priv *dvobj) +{ + u8 rst = _SUCCESS; + +#ifdef CONFIG_USB_VENDOR_REQ_BUFFER_PREALLOC + if (dvobj->usb_vendor_req_buf) + rtw_mfree(dvobj->usb_alloc_vendor_req_buf, MAX_USB_IO_CTL_SIZE); +#endif + +#ifdef CONFIG_USB_VENDOR_REQ_MUTEX + _rtw_mutex_free(&dvobj->usb_vendor_req_mutex); +#endif + + return rst; +} +static void rtw_decide_chip_type_by_usb_info(struct dvobj_priv *pdvobjpriv, const struct usb_device_id *pdid) +{ + pdvobjpriv->chip_type = pdid->driver_info; + +#ifdef CONFIG_RTL8188E + if (pdvobjpriv->chip_type == RTL8188E) + rtl8188eu_set_hw_type(pdvobjpriv); +#endif + +#if defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A) + if (pdvobjpriv->chip_type == RTL8812 || pdvobjpriv->chip_type == RTL8821) + rtl8812au_set_hw_type(pdvobjpriv); +#endif + +#ifdef CONFIG_RTL8192E + if (pdvobjpriv->chip_type == RTL8192E) + rtl8192eu_set_hw_type(pdvobjpriv); +#endif + +#ifdef CONFIG_RTL8723B + if (pdvobjpriv->chip_type == RTL8723B) + rtl8723bu_set_hw_type(pdvobjpriv); +#endif + +#ifdef CONFIG_RTL8814A + if (pdvobjpriv->chip_type == RTL8814A) + rtl8814au_set_hw_type(pdvobjpriv); +#endif /* CONFIG_RTL8814A */ + +#ifdef CONFIG_RTL8188F + if (pdvobjpriv->chip_type == RTL8188F) + rtl8188fu_set_hw_type(pdvobjpriv); +#endif + +#ifdef CONFIG_RTL8188GTV + if (pdvobjpriv->chip_type == RTL8188GTV) + rtl8188gtvu_set_hw_type(pdvobjpriv); +#endif + +#ifdef CONFIG_RTL8703B + if (pdvobjpriv->chip_type == RTL8703B) + rtl8703bu_set_hw_type(pdvobjpriv); +#endif /* CONFIG_RTL8703B */ + +#ifdef CONFIG_RTL8822B + if (pdvobjpriv->chip_type == RTL8822B) + rtl8822bu_set_hw_type(pdvobjpriv); +#endif /* CONFIG_RTL8822B */ + +#ifdef CONFIG_RTL8723D + if (pdvobjpriv->chip_type == RTL8723D) + rtl8723du_set_hw_type(pdvobjpriv); +#endif /* CONFIG_RTL8723D */ + +#ifdef CONFIG_RTL8821C + if (pdvobjpriv->chip_type == RTL8821C) + rtl8821cu_set_hw_type(pdvobjpriv); +#endif /* CONFIG_RTL8821C */ + +#ifdef CONFIG_RTL8710B + if (pdvobjpriv->chip_type == RTL8710B) + rtl8710bu_set_hw_type(pdvobjpriv); +#endif /* CONFIG_RTL8710B */ + +#ifdef CONFIG_RTL8192F + if (pdvobjpriv->chip_type == RTL8192F) + rtl8192fu_set_hw_type(pdvobjpriv); +#endif /* CONFIG_RTL8192F */ + +#ifdef CONFIG_RTL8822C + if (pdvobjpriv->chip_type == RTL8822C) + rtl8822cu_set_hw_type(pdvobjpriv); +#endif /* CONFIG_RTL8822C */ + +#ifdef CONFIG_RTL8814B + if (pdvobjpriv->chip_type == RTL8814B) + rtl8814bu_set_hw_type(pdvobjpriv); +#endif /* CONFIG_RTL8814B */ + +#ifdef CONFIG_RTL8723F + if (pdvobjpriv->chip_type == RTL8723F) + rtl8723fu_set_hw_type(pdvobjpriv); +#endif /* CONFIG_RTL8723F */ +} + +static struct dvobj_priv *usb_dvobj_init(struct usb_interface *usb_intf, const struct usb_device_id *pdid) +{ + int i; + int status = _FAIL; + struct dvobj_priv *pdvobjpriv; + struct usb_device_descriptor *pdev_desc; + struct usb_host_config *phost_conf; + struct usb_config_descriptor *pconf_desc; + struct usb_host_interface *phost_iface; + struct usb_interface_descriptor *piface_desc; + struct usb_host_endpoint *phost_endp; + struct usb_endpoint_descriptor *pendp_desc; + struct usb_device *pusbd; + + + + pdvobjpriv = devobj_init(); + if (pdvobjpriv == NULL) + goto exit; + + + pdvobjpriv->pusbintf = usb_intf ; + pusbd = pdvobjpriv->pusbdev = interface_to_usbdev(usb_intf); + usb_set_intfdata(usb_intf, pdvobjpriv); + + pdvobjpriv->RtNumInPipes = 0; + pdvobjpriv->RtNumOutPipes = 0; + + /* padapter->EepromAddressSize = 6; */ + /* pdvobjpriv->nr_endpoint = 6; */ + + pdev_desc = &pusbd->descriptor; + +#if 0 + RTW_INFO("\n8712_usb_device_descriptor:\n"); + RTW_INFO("bLength=%x\n", pdev_desc->bLength); + RTW_INFO("bDescriptorType=%x\n", pdev_desc->bDescriptorType); + RTW_INFO("bcdUSB=%x\n", pdev_desc->bcdUSB); + RTW_INFO("bDeviceClass=%x\n", pdev_desc->bDeviceClass); + RTW_INFO("bDeviceSubClass=%x\n", pdev_desc->bDeviceSubClass); + RTW_INFO("bDeviceProtocol=%x\n", pdev_desc->bDeviceProtocol); + RTW_INFO("bMaxPacketSize0=%x\n", pdev_desc->bMaxPacketSize0); + RTW_INFO("idVendor=%x\n", pdev_desc->idVendor); + RTW_INFO("idProduct=%x\n", pdev_desc->idProduct); + RTW_INFO("bcdDevice=%x\n", pdev_desc->bcdDevice); + RTW_INFO("iManufacturer=%x\n", pdev_desc->iManufacturer); + RTW_INFO("iProduct=%x\n", pdev_desc->iProduct); + RTW_INFO("iSerialNumber=%x\n", pdev_desc->iSerialNumber); + RTW_INFO("bNumConfigurations=%x\n", pdev_desc->bNumConfigurations); +#endif + + phost_conf = pusbd->actconfig; + pconf_desc = &phost_conf->desc; + + + /* RTW_INFO("\n***** num of altsetting = (%d) *****\n", pusb_interface->num_altsetting); */ + + phost_iface = &usb_intf->altsetting[0]; + piface_desc = &phost_iface->desc; + + pdvobjpriv->nr_endpoint = piface_desc->bNumEndpoints; + + /* RTW_INFO("\ndump usb_endpoint_descriptor:\n"); */ + + for (i = 0; i < pdvobjpriv->nr_endpoint; i++) { + phost_endp = phost_iface->endpoint + i; + if (phost_endp) { + pendp_desc = &phost_endp->desc; + + RTW_INFO("\nusb_endpoint_descriptor(%d):\n", i); + RTW_INFO("bLength=%x\n", pendp_desc->bLength); + RTW_INFO("bDescriptorType=%x\n", pendp_desc->bDescriptorType); + RTW_INFO("bEndpointAddress=%x\n", pendp_desc->bEndpointAddress); + /* RTW_INFO("bmAttributes=%x\n",pendp_desc->bmAttributes); */ + RTW_INFO("wMaxPacketSize=%d\n", le16_to_cpu(pendp_desc->wMaxPacketSize)); + RTW_INFO("bInterval=%x\n", pendp_desc->bInterval); + /* RTW_INFO("bRefresh=%x\n",pendp_desc->bRefresh); */ + /* RTW_INFO("bSynchAddress=%x\n",pendp_desc->bSynchAddress); */ + + if (RT_usb_endpoint_is_bulk_in(pendp_desc)) { + RTW_INFO("RT_usb_endpoint_is_bulk_in = %x\n", RT_usb_endpoint_num(pendp_desc)); + pdvobjpriv->RtInPipe[pdvobjpriv->RtNumInPipes] = RT_usb_endpoint_num(pendp_desc); + pdvobjpriv->RtNumInPipes++; + } else if (RT_usb_endpoint_is_int_in(pendp_desc)) { + RTW_INFO("RT_usb_endpoint_is_int_in = %x, Interval = %x\n", RT_usb_endpoint_num(pendp_desc), pendp_desc->bInterval); + pdvobjpriv->RtInPipe[pdvobjpriv->RtNumInPipes] = RT_usb_endpoint_num(pendp_desc); + pdvobjpriv->RtNumInPipes++; + } else if (RT_usb_endpoint_is_bulk_out(pendp_desc)) { + RTW_INFO("RT_usb_endpoint_is_bulk_out = %x\n", RT_usb_endpoint_num(pendp_desc)); + pdvobjpriv->RtOutPipe[pdvobjpriv->RtNumOutPipes] = RT_usb_endpoint_num(pendp_desc); + pdvobjpriv->RtNumOutPipes++; + } + pdvobjpriv->ep_num[i] = RT_usb_endpoint_num(pendp_desc); + } + } + + RTW_INFO("nr_endpoint=%d, in_num=%d, out_num=%d\n\n", pdvobjpriv->nr_endpoint, pdvobjpriv->RtNumInPipes, pdvobjpriv->RtNumOutPipes); + + switch (pusbd->speed) { + case USB_SPEED_LOW: + RTW_INFO("USB_SPEED_LOW\n"); + pdvobjpriv->usb_speed = RTW_USB_SPEED_1_1; + break; + case USB_SPEED_FULL: + RTW_INFO("USB_SPEED_FULL\n"); + pdvobjpriv->usb_speed = RTW_USB_SPEED_1_1; + break; + case USB_SPEED_HIGH: + RTW_INFO("USB_SPEED_HIGH\n"); + pdvobjpriv->usb_speed = RTW_USB_SPEED_2; + break; +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)) + case USB_SPEED_SUPER: + RTW_INFO("USB_SPEED_SUPER\n"); + pdvobjpriv->usb_speed = RTW_USB_SPEED_3; + break; +#endif + default: + RTW_INFO("USB_SPEED_UNKNOWN(%x)\n", pusbd->speed); + pdvobjpriv->usb_speed = RTW_USB_SPEED_UNKNOWN; + break; + } + + if (pdvobjpriv->usb_speed == RTW_USB_SPEED_UNKNOWN) { + RTW_INFO("UNKNOWN USB SPEED MODE, ERROR !!!\n"); + goto free_dvobj; + } + + if (rtw_init_intf_priv(pdvobjpriv) == _FAIL) { + goto free_dvobj; + } + + /*step 1-1., decide the chip_type via driver_info*/ + pdvobjpriv->interface_type = RTW_USB; + rtw_decide_chip_type_by_usb_info(pdvobjpriv, pdid); + + /* .3 misc */ + _rtw_init_sema(&(pdvobjpriv->usb_suspend_sema), 0); + rtw_reset_continual_io_error(pdvobjpriv); + + usb_get_dev(pusbd); + + status = _SUCCESS; + +free_dvobj: + if (status != _SUCCESS && pdvobjpriv) { + usb_set_intfdata(usb_intf, NULL); + + devobj_deinit(pdvobjpriv); + + pdvobjpriv = NULL; + } +exit: + return pdvobjpriv; +} + +static void usb_dvobj_deinit(struct usb_interface *usb_intf) +{ + struct dvobj_priv *dvobj = usb_get_intfdata(usb_intf); + + + usb_set_intfdata(usb_intf, NULL); + if (dvobj) { + rtw_deinit_intf_priv(dvobj); + devobj_deinit(dvobj); + } + + /* RTW_INFO("%s %d\n", __func__, ATOMIC_READ(&usb_intf->dev.kobj.kref.refcount)); */ + usb_put_dev(interface_to_usbdev(usb_intf)); + +} + +static int usb_reprobe_switch_usb_mode(PADAPTER Adapter) +{ + struct registry_priv *registry_par = &Adapter->registrypriv; + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + u8 ret = _FALSE; + + /* efuse not allow driver to switch usb mode */ + if (pHalData->EEPROMUsbSwitch == _FALSE) + goto exit; + + /* registry not allow driver to switch usb mode */ + if (registry_par->switch_usb_mode == 0) + goto exit; + + rtw_hal_set_hwreg(Adapter, HW_VAR_USB_MODE, &ret); + +exit: + return ret; +} + +u8 rtw_set_hal_ops(_adapter *padapter) +{ + /* alloc memory for HAL DATA */ + if (rtw_hal_data_init(padapter) == _FAIL) + return _FAIL; + +#ifdef CONFIG_RTL8188E + if (rtw_get_chip_type(padapter) == RTL8188E) + rtl8188eu_set_hal_ops(padapter); +#endif + +#if defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A) + if (rtw_get_chip_type(padapter) == RTL8812 || rtw_get_chip_type(padapter) == RTL8821) + rtl8812au_set_hal_ops(padapter); +#endif + +#ifdef CONFIG_RTL8192E + if (rtw_get_chip_type(padapter) == RTL8192E) + rtl8192eu_set_hal_ops(padapter); +#endif +#ifdef CONFIG_RTL8723B + if (rtw_get_chip_type(padapter) == RTL8723B) + rtl8723bu_set_hal_ops(padapter); +#endif +#ifdef CONFIG_RTL8814A + if (rtw_get_chip_type(padapter) == RTL8814A) + rtl8814au_set_hal_ops(padapter); +#endif /* CONFIG_RTL8814A */ + +#ifdef CONFIG_RTL8188F + if (rtw_get_chip_type(padapter) == RTL8188F) + rtl8188fu_set_hal_ops(padapter); +#endif + +#ifdef CONFIG_RTL8188GTV + if (rtw_get_chip_type(padapter) == RTL8188GTV) + rtl8188gtvu_set_hal_ops(padapter); +#endif + +#ifdef CONFIG_RTL8703B + if (rtw_get_chip_type(padapter) == RTL8703B) + rtl8703bu_set_hal_ops(padapter); +#endif /* CONFIG_RTL8703B */ + +#ifdef CONFIG_RTL8822B + if (rtw_get_chip_type(padapter) == RTL8822B) + rtl8822bu_set_hal_ops(padapter); +#endif /* CONFIG_RTL8822B */ + +#ifdef CONFIG_RTL8723D + if (rtw_get_chip_type(padapter) == RTL8723D) + rtl8723du_set_hal_ops(padapter); +#endif /* CONFIG_RTL8723D */ + + +#ifdef CONFIG_RTL8821C + if (rtw_get_chip_type(padapter) == RTL8821C) { + if (rtl8821cu_set_hal_ops(padapter) == _FAIL) + return _FAIL; + } +#endif + +#ifdef CONFIG_RTL8710B + if (rtw_get_chip_type(padapter) == RTL8710B) + rtl8710bu_set_hal_ops(padapter); +#endif /* CONFIG_RTL8710B */ + + +#ifdef CONFIG_RTL8192F + if (rtw_get_chip_type(padapter) == RTL8192F) + rtl8192fu_set_hal_ops(padapter); +#endif + +#ifdef CONFIG_RTL8822C + if (rtw_get_chip_type(padapter) == RTL8822C) + rtl8822cu_set_hal_ops(padapter); +#endif /* CONFIG_RTL8822C */ + +#ifdef CONFIG_RTL8814B + if (rtw_get_chip_type(padapter) == RTL8814B) + rtl8814bu_set_hal_ops(padapter); +#endif /* CONFIG_RTL8814B */ + +#ifdef CONFIG_RTL8723F + if (rtw_get_chip_type(padapter) == RTL8723F) + rtl8723fu_set_hal_ops(padapter); +#endif /* CONFIG_RTL8723F */ + + if (_FAIL == rtw_hal_ops_check(padapter)) + return _FAIL; + + if (hal_spec_init(padapter) == _FAIL) + return _FAIL; + + return _SUCCESS; +} + +static void usb_intf_start(_adapter *padapter) +{ + PHAL_DATA_TYPE hal = GET_HAL_DATA(padapter); + + rtw_hal_inirp_init(padapter); + hal->usb_intf_start = _TRUE; + + +} + +static void usb_intf_stop(_adapter *padapter) +{ + PHAL_DATA_TYPE hal = GET_HAL_DATA(padapter); + + /* disabel_hw_interrupt */ + if (!rtw_is_surprise_removed(padapter)) { + /* device still exists, so driver can do i/o operation */ + /* TODO: */ + } + + /* cancel in irp */ + rtw_hal_inirp_deinit(padapter); + + /* cancel out irp */ + rtw_write_port_cancel(padapter); + + /* todo:cancel other irps */ + + hal->usb_intf_start = _FALSE; + +} + +static void process_spec_devid(const struct usb_device_id *pdid) +{ + u16 vid, pid; + u32 flags; + int i; + int num = sizeof(specific_device_id_tbl) / sizeof(struct specific_device_id); + + for (i = 0; i < num; i++) { + vid = specific_device_id_tbl[i].idVendor; + pid = specific_device_id_tbl[i].idProduct; + flags = specific_device_id_tbl[i].flags; + +#ifdef CONFIG_80211N_HT + if ((pdid->idVendor == vid) && (pdid->idProduct == pid) && (flags & SPEC_DEV_ID_DISABLE_HT)) { + rtw_ht_enable = 0; + rtw_bw_mode = 0; + rtw_ampdu_enable = 0; + } +#endif + +#ifdef RTK_DMP_PLATFORM + /* Change the ifname to wlan10 when PC side WFD dongle plugin on DMP platform. */ + /* It is used to distinguish between normal and PC-side wifi dongle/module. */ + if ((pdid->idVendor == vid) && (pdid->idProduct == pid) && (flags & SPEC_DEV_ID_ASSIGN_IFNAME)) { + extern char *ifname; + strncpy(ifname, "wlan10", 6); + /* RTW_INFO("%s()-%d: ifname=%s, vid=%04X, pid=%04X\n", __FUNCTION__, __LINE__, ifname, vid, pid); */ + } +#endif /* RTK_DMP_PLATFORM */ + + } +} + +#ifdef SUPPORT_HW_RFOFF_DETECTED +int rtw_hw_suspend(_adapter *padapter) +{ + struct pwrctrl_priv *pwrpriv; + struct usb_interface *pusb_intf; + struct net_device *pnetdev; + + if (NULL == padapter) + goto error_exit; + + if ((_FALSE == padapter->bup) || RTW_CANNOT_RUN(padapter)) { + RTW_INFO("padapter->bup=%d bDriverStopped=%s bSurpriseRemoved = %s\n" + , padapter->bup + , rtw_is_drv_stopped(padapter) ? "True" : "False" + , rtw_is_surprise_removed(padapter) ? "True" : "False"); + goto error_exit; + } + + pwrpriv = adapter_to_pwrctl(padapter); + pusb_intf = adapter_to_dvobj(padapter)->pusbintf; + pnetdev = padapter->pnetdev; + + LeaveAllPowerSaveMode(padapter); + + RTW_INFO("==> rtw_hw_suspend\n"); + _enter_pwrlock(&pwrpriv->lock); + pwrpriv->bips_processing = _TRUE; + /* padapter->net_closed = _TRUE; */ + /* s1. */ + if (pnetdev) { + rtw_netif_carrier_off(pnetdev); + rtw_netif_stop_queue(pnetdev); + } + + /* s2. */ + rtw_disassoc_cmd(padapter, 500, RTW_CMDF_DIRECTLY); + + /* s2-2. indicate disconnect to os */ + /* rtw_indicate_disconnect(padapter); */ + { + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE)) { + _clr_fwstate_(pmlmepriv, WIFI_ASOC_STATE); + rtw_led_control(padapter, LED_CTL_NO_LINK); + + rtw_os_indicate_disconnect(padapter, 0, _FALSE); + +#ifdef CONFIG_LPS + /* donnot enqueue cmd */ + rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_DISCONNECT, RTW_CMDF_DIRECTLY); +#endif + } + } + /* s2-3. */ + rtw_free_assoc_resources(padapter, _TRUE); + + /* s2-4. */ + rtw_free_network_queue(padapter, _TRUE); +#ifdef CONFIG_IPS + rtw_ips_dev_unload(padapter); +#endif + pwrpriv->rf_pwrstate = rf_off; + pwrpriv->bips_processing = _FALSE; + _exit_pwrlock(&pwrpriv->lock); + + return 0; + +error_exit: + RTW_INFO("%s, failed\n", __FUNCTION__); + return -1; + +} + +int rtw_hw_resume(_adapter *padapter) +{ + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + struct usb_interface *pusb_intf = adapter_to_dvobj(padapter)->pusbintf; + struct net_device *pnetdev = padapter->pnetdev; + + RTW_INFO("==> rtw_hw_resume\n"); + _enter_pwrlock(&pwrpriv->lock); + pwrpriv->bips_processing = _TRUE; + rtw_reset_drv_sw(padapter); + + if (pm_netdev_open(pnetdev, _FALSE) != 0) { + _exit_pwrlock(&pwrpriv->lock); + goto error_exit; + } + rtw_netif_device_attach(pnetdev); + rtw_netif_carrier_on(pnetdev); + + rtw_netif_wake_queue(pnetdev); + + pwrpriv->bkeepfwalive = _FALSE; + pwrpriv->brfoffbyhw = _FALSE; + + pwrpriv->rf_pwrstate = rf_on; + pwrpriv->bips_processing = _FALSE; + _exit_pwrlock(&pwrpriv->lock); + + + return 0; +error_exit: + RTW_INFO("%s, Open net dev failed\n", __FUNCTION__); + return -1; +} +#endif + +static int rtw_suspend(struct usb_interface *pusb_intf, pm_message_t message) +{ + struct dvobj_priv *dvobj; + struct pwrctrl_priv *pwrpriv; + struct debug_priv *pdbgpriv; + PADAPTER padapter; + int ret = 0; + + + dvobj = usb_get_intfdata(pusb_intf); + pwrpriv = dvobj_to_pwrctl(dvobj); + pdbgpriv = &dvobj->drv_dbg; + padapter = dvobj_get_primary_adapter(dvobj); + + if (pwrpriv->bInSuspend == _TRUE) { + RTW_INFO("%s bInSuspend = %d\n", __FUNCTION__, pwrpriv->bInSuspend); + pdbgpriv->dbg_suspend_error_cnt++; + goto exit; + } + + ret = rtw_suspend_common(padapter); + +exit: + return ret; +} + +int rtw_resume_process(_adapter *padapter) +{ + int ret; + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + struct dvobj_priv *pdvobj = padapter->dvobj; + struct debug_priv *pdbgpriv = &pdvobj->drv_dbg; + + + if (pwrpriv->bInSuspend == _FALSE) { + pdbgpriv->dbg_resume_error_cnt++; + RTW_INFO("%s bInSuspend = %d\n", __FUNCTION__, pwrpriv->bInSuspend); + return -1; + } + + /* + * Due to usb wow suspend flow will cancel read/write port via intf_stop and + * bReadPortCancel and bWritePortCancel are set _TRUE in intf_stop. + * But they will not be clear in intf_start during wow resume flow. + * It should move to os_intf in the feature. + */ + RTW_ENABLE_FUNC(padapter, DF_RX_BIT); + RTW_ENABLE_FUNC(padapter, DF_TX_BIT); + + ret = rtw_resume_common(padapter); + + return ret; +} + +static int rtw_resume(struct usb_interface *pusb_intf) +{ + struct dvobj_priv *dvobj; + struct pwrctrl_priv *pwrpriv; + struct debug_priv *pdbgpriv; + PADAPTER padapter; + struct mlme_ext_priv *pmlmeext; + int ret = 0; + + + dvobj = usb_get_intfdata(pusb_intf); + pwrpriv = dvobj_to_pwrctl(dvobj); + pdbgpriv = &dvobj->drv_dbg; + padapter = dvobj_get_primary_adapter(dvobj); + pmlmeext = &padapter->mlmeextpriv; + + RTW_INFO("==> %s (%s:%d)\n", __FUNCTION__, current->comm, current->pid); + pdbgpriv->dbg_resume_cnt++; + + + if (pwrpriv->wowlan_mode || pwrpriv->wowlan_ap_mode) { + rtw_resume_lock_suspend(); + ret = rtw_resume_process(padapter); + rtw_resume_unlock_suspend(); + } else { +#ifdef CONFIG_RESUME_IN_WORKQUEUE + rtw_resume_in_workqueue(pwrpriv); +#else + if (rtw_is_earlysuspend_registered(pwrpriv)) { + /* jeff: bypass resume here, do in late_resume */ + rtw_set_do_late_resume(pwrpriv, _TRUE); + } else { + rtw_resume_lock_suspend(); + ret = rtw_resume_process(padapter); + rtw_resume_unlock_suspend(); + } +#endif + } + + pmlmeext->last_scan_time = rtw_get_current_time(); + RTW_INFO("<======== %s return %d\n", __FUNCTION__, ret); + + return ret; +} + + +#ifdef CONFIG_PLATFORM_RTD2880B +extern void rtd2885_wlan_netlink_sendMsg(char *action_string, char *name); +#endif + +/* + * drv_init() - a device potentially for us + * + * notes: drv_init() is called when the bus driver has located a card for us to support. + * We accept the new device by returning 0. +*/ +_adapter *rtw_usb_primary_adapter_init(struct dvobj_priv *dvobj, + struct usb_interface *pusb_intf) +{ + _adapter *padapter = NULL; + int status = _FAIL; + + padapter = (_adapter *)rtw_zvmalloc(sizeof(*padapter)); + if (padapter == NULL) + goto exit; + + if (loadparam(padapter) != _SUCCESS) + goto free_adapter; + + padapter->dvobj = dvobj; + + + rtw_set_drv_stopped(padapter);/*init*/ + + dvobj->padapters[dvobj->iface_nums++] = padapter; + padapter->iface_id = IFACE_ID0; + + /* set adapter_type/iface type for primary padapter */ + padapter->isprimary = _TRUE; + padapter->adapter_type = PRIMARY_ADAPTER; +#ifdef CONFIG_MI_WITH_MBSSID_CAM/*Configure all IFACE to PORT0-MBSSID*/ + padapter->hw_port = HW_PORT0; +#else + padapter->hw_port = HW_PORT0; +#endif + + /* step init_io_priv */ + if (rtw_init_io_priv(padapter, usb_set_intf_ops) == _FAIL) + goto free_adapter; + + /* step 2. hook HalFunc, allocate HalData */ + if (rtw_set_hal_ops(padapter) == _FAIL) + goto free_hal_data; + + + padapter->intf_start = &usb_intf_start; + padapter->intf_stop = &usb_intf_stop; + + /* step read_chip_version */ + rtw_hal_read_chip_version(padapter); + + /* step usb endpoint mapping */ + rtw_hal_chip_configure(padapter); + +#ifdef CONFIG_BT_COEXIST + rtw_btcoex_Initialize(padapter); +#endif + rtw_btcoex_wifionly_initialize(padapter); + + /* step read efuse/eeprom data and get mac_addr */ + if (rtw_hal_read_chip_info(padapter) == _FAIL) + goto free_hal_data; + + /* step 5. */ + if (rtw_init_drv_sw(padapter) == _FAIL) { + goto free_hal_data; + } + +#ifdef CONFIG_PM +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18)) + if (dvobj_to_pwrctl(dvobj)->bSupportRemoteWakeup) { + dvobj->pusbdev->do_remote_wakeup = 1; + pusb_intf->needs_remote_wakeup = 1; + device_init_wakeup(&pusb_intf->dev, 1); + RTW_INFO("pwrctrlpriv.bSupportRemoteWakeup~~~~~~\n"); + RTW_INFO("pwrctrlpriv.bSupportRemoteWakeup~~~[%d]~~~\n", device_may_wakeup(&pusb_intf->dev)); + } +#endif +#endif + /* 2012-07-11 Move here to prevent the 8723AS-VAU BT auto suspend influence */ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)) + if (usb_autopm_get_interface(pusb_intf) < 0) + RTW_INFO("can't get autopm:\n"); +#endif +#ifdef CONFIG_BT_COEXIST + dvobj_to_pwrctl(dvobj)->autopm_cnt = 1; +#endif + + /* set mac addr */ + rtw_macaddr_cfg(adapter_mac_addr(padapter), get_hal_mac_addr(padapter)); +#ifdef CONFIG_MI_WITH_MBSSID_CAM + rtw_mbid_camid_alloc(padapter, adapter_mac_addr(padapter)); +#endif + +#ifdef CONFIG_P2P + rtw_init_wifidirect_addrs(padapter, adapter_mac_addr(padapter), adapter_mac_addr(padapter)); +#endif /* CONFIG_P2P */ + RTW_INFO("bDriverStopped:%s, bSurpriseRemoved:%s, bup:%d, hw_init_completed:%d\n" + , rtw_is_drv_stopped(padapter) ? "True" : "False" + , rtw_is_surprise_removed(padapter) ? "True" : "False" + , padapter->bup + , rtw_get_hw_init_completed(padapter) + ); + + status = _SUCCESS; + +free_hal_data: + if (status != _SUCCESS && padapter->HalData) + rtw_hal_free_data(padapter); +free_adapter: + if (status != _SUCCESS && padapter) { + #ifdef RTW_HALMAC + rtw_halmac_deinit_adapter(dvobj); + #endif + rtw_vmfree((u8 *)padapter, sizeof(*padapter)); + padapter = NULL; + } +exit: + return padapter; +} + +static void rtw_usb_primary_adapter_deinit(_adapter *padapter) +{ +#if defined(CONFIG_WOWLAN) || defined(CONFIG_BT_COEXIST) + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(padapter); +#endif + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + + RTW_INFO(FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(padapter)); + + if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE)) + rtw_disassoc_cmd(padapter, 0, RTW_CMDF_DIRECTLY); + +#ifdef CONFIG_AP_MODE + if (MLME_IS_AP(padapter) || MLME_IS_MESH(padapter)) { + free_mlme_ap_info(padapter); + #ifdef CONFIG_HOSTAPD_MLME + hostapd_mode_unload(padapter); + #endif + } +#endif + + /*rtw_cancel_all_timer(if1);*/ + +#ifdef CONFIG_WOWLAN + pwrctl->wowlan_mode = _FALSE; +#endif /* CONFIG_WOWLAN */ + + rtw_dev_unload(padapter); + + RTW_INFO("+r871xu_dev_remove, hw_init_completed=%d\n", rtw_get_hw_init_completed(padapter)); + +#ifdef CONFIG_BT_COEXIST + if (1 == pwrctl->autopm_cnt) { +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)) + usb_autopm_put_interface(adapter_to_dvobj(padapter)->pusbintf); +#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)) + usb_autopm_enable(adapter_to_dvobj(padapter)->pusbintf); +#else + usb_autosuspend_device(adapter_to_dvobj(padapter)->pusbdev, 1); +#endif + pwrctl->autopm_cnt--; + } +#endif + + rtw_free_drv_sw(padapter); + + /* TODO: use rtw_os_ndevs_deinit instead at the first stage of driver's dev deinit function */ + rtw_os_ndev_free(padapter); + +#ifdef RTW_HALMAC + rtw_halmac_deinit_adapter(adapter_to_dvobj(padapter)); +#endif /* RTW_HALMAC */ + + rtw_vmfree((u8 *)padapter, sizeof(_adapter)); + +#ifdef CONFIG_PLATFORM_RTD2880B + RTW_INFO("wlan link down\n"); + rtd2885_wlan_netlink_sendMsg("linkdown", "8712"); +#endif + +} + +static int rtw_drv_init(struct usb_interface *pusb_intf, const struct usb_device_id *pdid) +{ + _adapter *padapter = NULL; + int status = _FAIL; + struct dvobj_priv *dvobj; +#ifdef CONFIG_CONCURRENT_MODE + int i; +#endif + + /* RTW_INFO("+rtw_drv_init\n"); */ + + /* step 0. */ + process_spec_devid(pdid); + + /* Initialize dvobj_priv */ + dvobj = usb_dvobj_init(pusb_intf, pdid); + if (dvobj == NULL) { + goto exit; + } + + padapter = rtw_usb_primary_adapter_init(dvobj, pusb_intf); + if (padapter == NULL) { + RTW_INFO("rtw_usb_primary_adapter_init Failed!\n"); + goto free_dvobj; + } + + if (usb_reprobe_switch_usb_mode(padapter) == _TRUE) + goto free_if_prim; + +#ifdef CONFIG_CONCURRENT_MODE + if (padapter->registrypriv.virtual_iface_num > (CONFIG_IFACE_NUMBER - 1)) + padapter->registrypriv.virtual_iface_num = (CONFIG_IFACE_NUMBER - 1); + + for (i = 0; i < padapter->registrypriv.virtual_iface_num; i++) { + if (rtw_drv_add_vir_if(padapter, usb_set_intf_ops) == NULL) { + RTW_INFO("rtw_drv_add_iface failed! (%d)\n", i); + goto free_if_vir; + } + } +#endif + +#ifdef CONFIG_GLOBAL_UI_PID + if (ui_pid[1] != 0) { + RTW_INFO("ui_pid[1]:%d\n", ui_pid[1]); + rtw_signal_process(ui_pid[1], SIGUSR2); + } +#endif + + /* dev_alloc_name && register_netdev */ + if (rtw_os_ndevs_init(dvobj) != _SUCCESS) + goto free_if_vir; + +#ifdef CONFIG_HOSTAPD_MLME + hostapd_mode_init(padapter); +#endif + +#ifdef CONFIG_PLATFORM_RTD2880B + RTW_INFO("wlan link up\n"); + rtd2885_wlan_netlink_sendMsg("linkup", "8712"); +#endif + + + status = _SUCCESS; + +#if 0 /* not used now */ +os_ndevs_deinit: + if (status != _SUCCESS) + rtw_os_ndevs_deinit(dvobj); +#endif +free_if_vir: + if (status != _SUCCESS) { + #ifdef CONFIG_CONCURRENT_MODE + rtw_drv_stop_vir_ifaces(dvobj); + rtw_drv_free_vir_ifaces(dvobj); + #endif + } + +free_if_prim: + if (status != _SUCCESS && padapter) + rtw_usb_primary_adapter_deinit(padapter); + +free_dvobj: + if (status != _SUCCESS) + usb_dvobj_deinit(pusb_intf); +exit: + return status == _SUCCESS ? 0 : -ENODEV; +} + +/* + * dev_remove() - our device is being removed +*/ +/* rmmod module & unplug(SurpriseRemoved) will call r871xu_dev_remove() => how to recognize both */ +static void rtw_dev_remove(struct usb_interface *pusb_intf) +{ + struct dvobj_priv *dvobj = usb_get_intfdata(pusb_intf); +#if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER) + struct pwrctrl_priv *pwrctl = dvobj_to_pwrctl(dvobj); +#endif + _adapter *padapter = dvobj_get_primary_adapter(dvobj); + + RTW_INFO("+rtw_dev_remove\n"); + + dvobj->processing_dev_remove = _TRUE; + + /* TODO: use rtw_os_ndevs_deinit instead at the first stage of driver's dev deinit function */ + rtw_os_ndevs_unregister(dvobj); + + if (usb_drv.drv_registered == _TRUE) { + /* RTW_INFO("r871xu_dev_remove():padapter->bSurpriseRemoved == _TRUE\n"); */ + rtw_set_surprise_removed(padapter); + } + /*else + { + + rtw_set_hw_init_completed(padapter, _FALSE); + }*/ + + +#if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER) + rtw_unregister_early_suspend(pwrctl); +#endif + + if (GET_HAL_DATA(padapter)->bFWReady == _TRUE) { + rtw_pm_set_ips(padapter, IPS_NONE); + rtw_pm_set_lps(padapter, PS_MODE_ACTIVE); + + LeaveAllPowerSaveMode(padapter); + } + rtw_set_drv_stopped(padapter); /*for stop thread*/ + rtw_stop_cmd_thread(padapter); +#ifdef CONFIG_CONCURRENT_MODE + rtw_drv_stop_vir_ifaces(dvobj); +#endif /* CONFIG_CONCURRENT_MODE */ + +#ifdef CONFIG_BT_COEXIST +#ifdef CONFIG_BT_COEXIST_SOCKET_TRX + if (GET_HAL_DATA(padapter)->EEPROMBluetoothCoexist) + rtw_btcoex_close_socket(padapter); +#endif + rtw_btcoex_HaltNotify(padapter); +#endif + + rtw_usb_primary_adapter_deinit(padapter); + +#ifdef CONFIG_CONCURRENT_MODE + rtw_drv_free_vir_ifaces(dvobj); +#endif /* CONFIG_CONCURRENT_MODE */ + + usb_dvobj_deinit(pusb_intf); + + RTW_INFO("-r871xu_dev_remove, done\n"); + + return; + +} +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)) +extern int console_suspend_enabled; +#endif + +static int __init rtw_drv_entry(void) +{ + int ret = 0; + + RTW_PRINT("module init start\n"); + dump_drv_version(RTW_DBGDUMP); +#ifdef BTCOEXVERSION + RTW_PRINT(DRV_NAME" BT-Coex version = %s\n", BTCOEXVERSION); +#endif /* BTCOEXVERSION */ + + ret = platform_wifi_power_on(); + if (ret != 0) { + RTW_INFO("%s: power on failed!!(%d)\n", __FUNCTION__, ret); + ret = -1; + goto exit; + } +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)) + /* console_suspend_enabled=0; */ +#endif + + usb_drv.drv_registered = _TRUE; + rtw_suspend_lock_init(); + rtw_drv_proc_init(); + rtw_nlrtw_init(); +#ifdef CONFIG_PLATFORM_CMAP_INTFS + cmap_intfs_init(); +#endif + rtw_ndev_notifier_register(); + rtw_inetaddr_notifier_register(); + + ret = usb_register(&usb_drv.usbdrv); + + if (ret != 0) { + usb_drv.drv_registered = _FALSE; + rtw_suspend_lock_uninit(); + rtw_drv_proc_deinit(); + rtw_nlrtw_deinit(); +#ifdef CONFIG_PLATFORM_CMAP_INTFS + cmap_intfs_deinit(); +#endif + rtw_ndev_notifier_unregister(); + rtw_inetaddr_notifier_unregister(); + goto exit; + } + +exit: + RTW_PRINT("module init ret=%d\n", ret); + return ret; +} + +static void __exit rtw_drv_halt(void) +{ + RTW_PRINT("module exit start\n"); + + usb_drv.drv_registered = _FALSE; + + usb_deregister(&usb_drv.usbdrv); + + platform_wifi_power_off(); + + rtw_suspend_lock_uninit(); + rtw_drv_proc_deinit(); + rtw_nlrtw_deinit(); +#ifdef CONFIG_PLATFORM_CMAP_INTFS + cmap_intfs_deinit(); +#endif + rtw_ndev_notifier_unregister(); + rtw_inetaddr_notifier_unregister(); + + RTW_PRINT("module exit success\n"); + + rtw_mstat_dump(RTW_DBGDUMP); +} + +module_init(rtw_drv_entry); +module_exit(rtw_drv_halt); diff --git a/os_dep/linux/usb_ops_linux.c b/os_dep/linux/usb_ops_linux.c new file mode 100644 index 0000000..a1cea6a --- /dev/null +++ b/os_dep/linux/usb_ops_linux.c @@ -0,0 +1,1157 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#define _USB_OPS_LINUX_C_ + +#include +#include +#include + +struct rtw_async_write_data { + u8 data[VENDOR_CMD_MAX_DATA_LEN]; + struct usb_ctrlrequest dr; +}; + +int usbctrl_vendorreq(struct intf_hdl *pintfhdl, u8 request, u16 value, u16 index, void *pdata, u16 len, u8 requesttype) +{ + _adapter *padapter = pintfhdl->padapter; + struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter); + struct usb_device *udev = pdvobjpriv->pusbdev; + + unsigned int pipe; + int status = 0; +#ifdef CONFIG_USB_VENDOR_REQ_BUFFER_DYNAMIC_ALLOCATE + u32 tmp_buflen = 0; +#endif + u8 reqtype; + u8 *pIo_buf; + int vendorreq_times = 0; + +#if (defined(CONFIG_RTL8822B) || defined(CONFIG_RTL8821C)) || defined(CONFIG_RTL8822C) +#define REG_ON_SEC 0x00 +#define REG_OFF_SEC 0x01 +#define REG_LOCAL_SEC 0x02 + u8 current_reg_sec = REG_LOCAL_SEC; +#endif + +#ifdef CONFIG_USB_VENDOR_REQ_BUFFER_DYNAMIC_ALLOCATE + u8 *tmp_buf; +#else /* use stack memory */ + #ifndef CONFIG_USB_VENDOR_REQ_BUFFER_PREALLOC + u8 tmp_buf[MAX_USB_IO_CTL_SIZE]; + #endif +#endif + + /* RTW_INFO("%s %s:%d\n",__FUNCTION__, current->comm, current->pid); */ + + if (RTW_CANNOT_IO(padapter)) { + status = -EPERM; + goto exit; + } + + if (len > MAX_VENDOR_REQ_CMD_SIZE) { + RTW_INFO("[%s] Buffer len error ,vendor request failed\n", __FUNCTION__); + status = -EINVAL; + goto exit; + } + +#ifdef CONFIG_USB_VENDOR_REQ_MUTEX + _enter_critical_mutex_lock(&pdvobjpriv->usb_vendor_req_mutex, NULL); +#endif + + + /* Acquire IO memory for vendorreq */ +#ifdef CONFIG_USB_VENDOR_REQ_BUFFER_PREALLOC + pIo_buf = pdvobjpriv->usb_vendor_req_buf; +#else + #ifdef CONFIG_USB_VENDOR_REQ_BUFFER_DYNAMIC_ALLOCATE + tmp_buf = rtw_malloc((u32) len + ALIGNMENT_UNIT); + tmp_buflen = (u32)len + ALIGNMENT_UNIT; + #else /* use stack memory */ + tmp_buflen = MAX_USB_IO_CTL_SIZE; + #endif + + /* Added by Albert 2010/02/09 */ + /* For mstar platform, mstar suggests the address for USB IO should be 16 bytes alignment. */ + /* Trying to fix it here. */ + pIo_buf = (tmp_buf == NULL) ? NULL : tmp_buf + ALIGNMENT_UNIT - ((SIZE_PTR)(tmp_buf) & 0x0f); +#endif + + if (pIo_buf == NULL) { + RTW_INFO("[%s] pIo_buf == NULL\n", __FUNCTION__); + status = -ENOMEM; + goto release_mutex; + } + + while (++vendorreq_times <= MAX_USBCTRL_VENDORREQ_TIMES) { + _rtw_memset(pIo_buf, 0, len); + + if (requesttype == 0x01) { + pipe = usb_rcvctrlpipe(udev, 0);/* read_in */ + reqtype = REALTEK_USB_VENQT_READ; + } else { + pipe = usb_sndctrlpipe(udev, 0);/* write_out */ + reqtype = REALTEK_USB_VENQT_WRITE; + _rtw_memcpy(pIo_buf, pdata, len); + } + + status = rtw_usb_control_msg(udev, pipe, request, reqtype, value, index, pIo_buf, len, RTW_USB_CONTROL_MSG_TIMEOUT); + + if (status == len) { /* Success this control transfer. */ + rtw_reset_continual_io_error(pdvobjpriv); + if (requesttype == 0x01) { + /* For Control read transfer, we have to copy the read data from pIo_buf to pdata. */ + _rtw_memcpy(pdata, pIo_buf, len); + } + } else { /* error cases */ + switch (len) { + case 1: + RTW_INFO("reg 0x%x, usb %s %u fail, status:%d value=0x%x, vendorreq_times:%d\n" + , value, (requesttype == 0x01) ? "read" : "write" , len, status, *(u8 *)pdata, vendorreq_times); + break; + case 2: + RTW_INFO("reg 0x%x, usb %s %u fail, status:%d value=0x%x, vendorreq_times:%d\n" + , value, (requesttype == 0x01) ? "read" : "write" , len, status, *(u16 *)pdata, vendorreq_times); + break; + case 4: + RTW_INFO("reg 0x%x, usb %s %u fail, status:%d value=0x%x, vendorreq_times:%d\n" + , value, (requesttype == 0x01) ? "read" : "write" , len, status, *(u32 *)pdata, vendorreq_times); + break; + default: + RTW_INFO("reg 0x%x, usb %s %u fail, status:%d, vendorreq_times:%d\n" + , value, (requesttype == 0x01) ? "read" : "write" , len, status, vendorreq_times); + break; + + } + + if (status < 0) { + if (status == (-ESHUTDOWN) || status == -ENODEV) + rtw_set_surprise_removed(padapter); + else { + #ifdef DBG_CONFIG_ERROR_DETECT + { + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + pHalData->srestpriv.Wifi_Error_Status = USB_VEN_REQ_CMD_FAIL; + } + #endif + } + } else { /* status != len && status >= 0 */ + if (status > 0) { + if (requesttype == 0x01) { + /* For Control read transfer, we have to copy the read data from pIo_buf to pdata. */ + _rtw_memcpy(pdata, pIo_buf, len); + } + } + } + + if (rtw_inc_and_chk_continual_io_error(pdvobjpriv) == _TRUE) { + rtw_set_surprise_removed(padapter); + break; + } + + } + + /* firmware download is checksumed, don't retry */ + if ((value >= FW_START_ADDRESS) || status == len) + break; + + } + +#if (defined(CONFIG_RTL8822B) || defined(CONFIG_RTL8821C)) || defined(CONFIG_RTL8822C) + if (value < 0xFE00) { + if (value <= 0xff) + current_reg_sec = REG_ON_SEC; + else if (0x1000 <= value && value <= 0x10ff) + current_reg_sec = REG_ON_SEC; + else + current_reg_sec = REG_OFF_SEC; + } else { + current_reg_sec = REG_LOCAL_SEC; + } + + if (current_reg_sec == REG_ON_SEC) { + unsigned int t_pipe = usb_sndctrlpipe(udev, 0);/* write_out */ + u8 t_reqtype = REALTEK_USB_VENQT_WRITE; + u8 t_len = 1; + u8 t_req = 0x05; + u16 t_reg = 0; + u16 t_index = 0; + + t_reg = 0x4e0; + + status = rtw_usb_control_msg(udev, t_pipe, t_req, t_reqtype, t_reg, t_index, pIo_buf, t_len, RTW_USB_CONTROL_MSG_TIMEOUT); + + if (status == t_len) + rtw_reset_continual_io_error(pdvobjpriv); + else + RTW_INFO("reg 0x%x, usb %s %u fail, status:%d\n", t_reg, "write" , t_len, status); + + } +#endif + + /* release IO memory used by vendorreq */ +#ifdef CONFIG_USB_VENDOR_REQ_BUFFER_DYNAMIC_ALLOCATE + rtw_mfree(tmp_buf, tmp_buflen); +#endif + +release_mutex: +#ifdef CONFIG_USB_VENDOR_REQ_MUTEX + _exit_critical_mutex(&pdvobjpriv->usb_vendor_req_mutex, NULL); +#endif +exit: + return status; + +} + +#ifdef CONFIG_USB_SUPPORT_ASYNC_VDN_REQ +static void _usbctrl_vendorreq_async_callback(struct urb *urb, struct pt_regs *regs) +{ + if (urb) { + if (urb->context) + rtw_mfree(urb->context, sizeof(struct rtw_async_write_data)); + usb_free_urb(urb); + } +} + +int _usbctrl_vendorreq_async_write(struct usb_device *udev, u8 request, + u16 value, u16 index, void *pdata, u16 len, u8 requesttype) +{ + int rc; + unsigned int pipe; + u8 reqtype; + struct usb_ctrlrequest *dr; + struct urb *urb; + struct rtw_async_write_data *buf; + + + if (requesttype == VENDOR_READ) { + pipe = usb_rcvctrlpipe(udev, 0);/* read_in */ + reqtype = REALTEK_USB_VENQT_READ; + } else { + pipe = usb_sndctrlpipe(udev, 0);/* write_out */ + reqtype = REALTEK_USB_VENQT_WRITE; + } + + buf = (struct rtl819x_async_write_data *)rtw_zmalloc(sizeof(*buf)); + if (!buf) { + rc = -ENOMEM; + goto exit; + } + + urb = usb_alloc_urb(0, GFP_ATOMIC); + if (!urb) { + rtw_mfree((u8 *)buf, sizeof(*buf)); + rc = -ENOMEM; + goto exit; + } + + dr = &buf->dr; + + dr->bRequestType = reqtype; + dr->bRequest = request; + dr->wValue = cpu_to_le16(value); + dr->wIndex = cpu_to_le16(index); + dr->wLength = cpu_to_le16(len); + + _rtw_memcpy(buf, pdata, len); + + usb_fill_control_urb(urb, udev, pipe, (unsigned char *)dr, buf, len, + _usbctrl_vendorreq_async_callback, buf); + + rc = usb_submit_urb(urb, GFP_ATOMIC); + if (rc < 0) { + rtw_mfree((u8 *)buf, sizeof(*buf)); + usb_free_urb(urb); + } + +exit: + return rc; +} + + +#endif /* CONFIG_USB_SUPPORT_ASYNC_VDN_REQ */ + +unsigned int ffaddr2pipehdl(struct dvobj_priv *pdvobj, u32 addr) +{ + unsigned int pipe = 0, ep_num = 0; + struct usb_device *pusbd = pdvobj->pusbdev; + + if (addr == RECV_BULK_IN_ADDR) + pipe = usb_rcvbulkpipe(pusbd, pdvobj->RtInPipe[0]); + + else if (addr == RECV_INT_IN_ADDR) + pipe = usb_rcvintpipe(pusbd, pdvobj->RtInPipe[1]); + +#ifdef RTW_HALMAC + /* halmac already translate queue id to bulk out id (addr 0~3) */ + /* 8814BU bulk out id range is 0~6 */ + else if (addr < MAX_BULKOUT_NUM) { + ep_num = pdvobj->RtOutPipe[addr]; + pipe = usb_sndbulkpipe(pusbd, ep_num); + } +#else + else if (addr < HW_QUEUE_ENTRY) { + ep_num = pdvobj->Queue2Pipe[addr]; + pipe = usb_sndbulkpipe(pusbd, ep_num); + } +#endif + + + return pipe; +} + +struct zero_bulkout_context { + void *pbuf; + void *purb; + void *pirp; + void *padapter; +}; +#if 0 +static void usb_bulkout_zero_complete(struct urb *purb, struct pt_regs *regs) +{ + struct zero_bulkout_context *pcontext = (struct zero_bulkout_context *)purb->context; + + /* RTW_INFO("+usb_bulkout_zero_complete\n"); */ + + if (pcontext) { + if (pcontext->pbuf) + rtw_mfree(pcontext->pbuf, sizeof(int)); + + if (pcontext->purb && (pcontext->purb == purb)) + usb_free_urb(pcontext->purb); + + + rtw_mfree((u8 *)pcontext, sizeof(struct zero_bulkout_context)); + } + + +} + +static u32 usb_bulkout_zero(struct intf_hdl *pintfhdl, u32 addr) +{ + int pipe, status, len; + u32 ret; + unsigned char *pbuf; + struct zero_bulkout_context *pcontext; + PURB purb = NULL; + _adapter *padapter = (_adapter *)pintfhdl->padapter; + struct dvobj_priv *pdvobj = adapter_to_dvobj(padapter); + struct usb_device *pusbd = pdvobj->pusbdev; + + /* RTW_INFO("%s\n", __func__); */ + + + if (RTW_CANNOT_TX(padapter)) + return _FAIL; + + + pcontext = (struct zero_bulkout_context *)rtw_zmalloc(sizeof(struct zero_bulkout_context)); + if (pcontext == NULL) + return _FAIL; + + pbuf = (unsigned char *)rtw_zmalloc(sizeof(int)); + purb = usb_alloc_urb(0, GFP_ATOMIC); + + /* translate DMA FIFO addr to pipehandle */ + pipe = ffaddr2pipehdl(pdvobj, addr); + + len = 0; + pcontext->pbuf = pbuf; + pcontext->purb = purb; + pcontext->pirp = NULL; + pcontext->padapter = padapter; + + + /* translate DMA FIFO addr to pipehandle */ + /* pipe = ffaddr2pipehdl(pdvobj, addr); */ + + usb_fill_bulk_urb(purb, pusbd, pipe, + pbuf, + len, + usb_bulkout_zero_complete, + pcontext);/* context is pcontext */ + + status = usb_submit_urb(purb, GFP_ATOMIC); + + if (!status) + ret = _SUCCESS; + else + ret = _FAIL; + + + return _SUCCESS; + +} +#endif +void usb_read_mem(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *rmem) +{ + +} + +void usb_write_mem(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *wmem) +{ + +} + + +void usb_read_port_cancel(struct intf_hdl *pintfhdl) +{ + int i; + struct recv_buf *precvbuf; + _adapter *padapter = pintfhdl->padapter; + struct registry_priv *regsty = adapter_to_regsty(padapter); + precvbuf = (struct recv_buf *)padapter->recvpriv.precv_buf; + + RTW_INFO("%s\n", __func__); + + for (i = 0; i < regsty->recvbuf_nr ; i++) { + + if (precvbuf->purb) { + /* RTW_INFO("usb_read_port_cancel : usb_kill_urb\n"); */ + usb_kill_urb(precvbuf->purb); + } + precvbuf++; + } + +#ifdef CONFIG_USB_INTERRUPT_IN_PIPE + usb_kill_urb(padapter->recvpriv.int_in_urb); +#endif +} + +static void usb_write_port_complete(struct urb *purb, struct pt_regs *regs) +{ + _irqL irqL; + struct xmit_buf *pxmitbuf = (struct xmit_buf *)purb->context; + /* struct xmit_frame *pxmitframe = (struct xmit_frame *)pxmitbuf->priv_data; */ + /* _adapter *padapter = pxmitframe->padapter; */ + _adapter *padapter = pxmitbuf->padapter; + struct xmit_priv *pxmitpriv = &padapter->xmitpriv; + /* struct pkt_attrib *pattrib = &pxmitframe->attrib; */ + + + switch (pxmitbuf->flags) { + case VO_QUEUE_INX: + pxmitpriv->voq_cnt--; + break; + case VI_QUEUE_INX: + pxmitpriv->viq_cnt--; + break; + case BE_QUEUE_INX: + pxmitpriv->beq_cnt--; + break; + case BK_QUEUE_INX: + pxmitpriv->bkq_cnt--; + break; + default: + break; + } + + + /* + _enter_critical(&pxmitpriv->lock, &irqL); + + pxmitpriv->txirp_cnt--; + + switch(pattrib->priority) + { + case 1: + case 2: + pxmitpriv->bkq_cnt--; + + break; + case 4: + case 5: + pxmitpriv->viq_cnt--; + + break; + case 6: + case 7: + pxmitpriv->voq_cnt--; + + break; + case 0: + case 3: + default: + pxmitpriv->beq_cnt--; + + break; + + } + + _exit_critical(&pxmitpriv->lock, &irqL); + + + if(pxmitpriv->txirp_cnt==0) + { + _rtw_up_sema(&(pxmitpriv->tx_retevt)); + } + */ + /* rtw_free_xmitframe(pxmitpriv, pxmitframe); */ + + if (RTW_CANNOT_TX(padapter)) { + RTW_INFO("%s(): TX Warning! bDriverStopped(%s) OR bSurpriseRemoved(%s) pxmitbuf->buf_tag(%x)\n" + , __func__ + , rtw_is_drv_stopped(padapter) ? "True" : "False" + , rtw_is_surprise_removed(padapter) ? "True" : "False" + , pxmitbuf->buf_tag); + + goto check_completion; + } + + + if (purb->status == 0) { + + } else { + RTW_INFO("###=> urb_write_port_complete status(%d)\n", purb->status); + if ((purb->status == -EPIPE) || (purb->status == -EPROTO)) { + /* usb_clear_halt(pusbdev, purb->pipe); */ + /* msleep(10); */ + sreset_set_wifi_error_status(padapter, USB_WRITE_PORT_FAIL); + } else if (purb->status == -EINPROGRESS) { + goto check_completion; + + } else if (purb->status == -ENOENT) { + RTW_INFO("%s: -ENOENT\n", __func__); + goto check_completion; + + } else if (purb->status == -ECONNRESET) { + RTW_INFO("%s: -ECONNRESET\n", __func__); + goto check_completion; + + } else if (purb->status == -ESHUTDOWN) { + rtw_set_drv_stopped(padapter); + + goto check_completion; + } else { + rtw_set_surprise_removed(padapter); + RTW_INFO("bSurpriseRemoved=TRUE\n"); + + goto check_completion; + } + } + + #ifdef DBG_CONFIG_ERROR_DETECT + { + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + pHalData->srestpriv.last_tx_complete_time = rtw_get_current_time(); + } + #endif + +check_completion: + _enter_critical(&pxmitpriv->lock_sctx, &irqL); + rtw_sctx_done_err(&pxmitbuf->sctx, + purb->status ? RTW_SCTX_DONE_WRITE_PORT_ERR : RTW_SCTX_DONE_SUCCESS); + _exit_critical(&pxmitpriv->lock_sctx, &irqL); + + rtw_free_xmitbuf(pxmitpriv, pxmitbuf); + + /* if(rtw_txframes_pending(padapter)) */ + { + tasklet_hi_schedule(&pxmitpriv->xmit_tasklet); + } + + +} + +u32 usb_write_port(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *wmem) +{ + _irqL irqL; + unsigned int pipe; + int status; + u32 ret = _FAIL; + PURB purb = NULL; + _adapter *padapter = (_adapter *)pintfhdl->padapter; + struct dvobj_priv *pdvobj = adapter_to_dvobj(padapter); + struct xmit_priv *pxmitpriv = &padapter->xmitpriv; + struct xmit_buf *pxmitbuf = (struct xmit_buf *)wmem; + struct xmit_frame *pxmitframe = (struct xmit_frame *)pxmitbuf->priv_data; + struct usb_device *pusbd = pdvobj->pusbdev; + + if (RTW_CANNOT_TX(padapter)) { +#ifdef DBG_TX + RTW_INFO(" DBG_TX %s:%d bDriverStopped%s, bSurpriseRemoved:%s\n", __func__, __LINE__ + , rtw_is_drv_stopped(padapter) ? "True" : "False" + , rtw_is_surprise_removed(padapter) ? "True" : "False"); +#endif + rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_TX_DENY); + goto exit; + } + + _enter_critical(&pxmitpriv->lock, &irqL); + + switch (addr) { + case VO_QUEUE_INX: + pxmitpriv->voq_cnt++; + pxmitbuf->flags = VO_QUEUE_INX; + break; + case VI_QUEUE_INX: + pxmitpriv->viq_cnt++; + pxmitbuf->flags = VI_QUEUE_INX; + break; + case BE_QUEUE_INX: + pxmitpriv->beq_cnt++; + pxmitbuf->flags = BE_QUEUE_INX; + break; + case BK_QUEUE_INX: + pxmitpriv->bkq_cnt++; + pxmitbuf->flags = BK_QUEUE_INX; + break; + case HIGH_QUEUE_INX: + pxmitbuf->flags = HIGH_QUEUE_INX; + break; + default: + pxmitbuf->flags = MGT_QUEUE_INX; + break; + } + + _exit_critical(&pxmitpriv->lock, &irqL); + + purb = pxmitbuf->pxmit_urb[0]; + + /* translate DMA FIFO addr to pipehandle */ +#ifdef RTW_HALMAC + pipe = ffaddr2pipehdl(pdvobj, pxmitbuf->bulkout_id); +#else + pipe = ffaddr2pipehdl(pdvobj, addr); +#endif + +#ifdef CONFIG_REDUCE_USB_TX_INT + if ((pxmitpriv->free_xmitbuf_cnt % NR_XMITBUFF == 0) + || (pxmitbuf->buf_tag > XMITBUF_DATA)) + purb->transfer_flags &= (~URB_NO_INTERRUPT); + else { + purb->transfer_flags |= URB_NO_INTERRUPT; + /* RTW_INFO("URB_NO_INTERRUPT "); */ + } +#endif + + + usb_fill_bulk_urb(purb, pusbd, pipe, + pxmitframe->buf_addr, /* = pxmitbuf->pbuf */ + cnt, + usb_write_port_complete, + pxmitbuf);/* context is pxmitbuf */ + +#ifdef CONFIG_USE_USB_BUFFER_ALLOC_TX + purb->transfer_dma = pxmitbuf->dma_transfer_addr; + purb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; + purb->transfer_flags |= URB_ZERO_PACKET; +#endif /* CONFIG_USE_USB_BUFFER_ALLOC_TX */ + +#ifdef USB_PACKET_OFFSET_SZ +#if (USB_PACKET_OFFSET_SZ == 0) + purb->transfer_flags |= URB_ZERO_PACKET; +#endif +#endif + +#if 0 + if (bwritezero) + purb->transfer_flags |= URB_ZERO_PACKET; +#endif + + status = usb_submit_urb(purb, GFP_ATOMIC); + if (!status) { + #ifdef DBG_CONFIG_ERROR_DETECT + { + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + pHalData->srestpriv.last_tx_time = rtw_get_current_time(); + } + #endif + } else { + rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_WRITE_PORT_ERR); + RTW_INFO("usb_write_port, status=%d\n", status); + + switch (status) { + case -ENODEV: + rtw_set_drv_stopped(padapter); + break; + default: + break; + } + goto exit; + } + + ret = _SUCCESS; + + /* Commented by Albert 2009/10/13 + * We add the URB_ZERO_PACKET flag to urb so that the host will send the zero packet automatically. */ + /* + if(bwritezero == _TRUE) + { + usb_bulkout_zero(pintfhdl, addr); + } + */ + + +exit: + if (ret != _SUCCESS) + rtw_free_xmitbuf(pxmitpriv, pxmitbuf); + return ret; + +} + +void usb_write_port_cancel(struct intf_hdl *pintfhdl) +{ + int i, j; + _adapter *padapter = pintfhdl->padapter; + struct xmit_buf *pxmitbuf = (struct xmit_buf *)padapter->xmitpriv.pxmitbuf; + + RTW_INFO("%s\n", __func__); + + for (i = 0; i < NR_XMITBUFF; i++) { + for (j = 0; j < 8; j++) { + if (pxmitbuf->pxmit_urb[j]) + usb_kill_urb(pxmitbuf->pxmit_urb[j]); + } + pxmitbuf++; + } + + pxmitbuf = (struct xmit_buf *)padapter->xmitpriv.pxmit_extbuf; + for (i = 0; i < NR_XMIT_EXTBUFF ; i++) { + for (j = 0; j < 8; j++) { + if (pxmitbuf->pxmit_urb[j]) + usb_kill_urb(pxmitbuf->pxmit_urb[j]); + } + pxmitbuf++; + } +} + +void usb_init_recvbuf(_adapter *padapter, struct recv_buf *precvbuf) +{ + + precvbuf->transfer_len = 0; + + precvbuf->len = 0; + + precvbuf->ref_cnt = 0; + + if (precvbuf->pbuf) { + precvbuf->pdata = precvbuf->phead = precvbuf->ptail = precvbuf->pbuf; + precvbuf->pend = precvbuf->pdata + MAX_RECVBUF_SZ; + } + +} + +int recvbuf2recvframe(PADAPTER padapter, void *ptr); + +#ifdef CONFIG_USE_USB_BUFFER_ALLOC_RX +void usb_recv_tasklet(void *priv) +{ + struct recv_buf *precvbuf = NULL; + _adapter *padapter = (_adapter *)priv; + struct recv_priv *precvpriv = &padapter->recvpriv; + + while (NULL != (precvbuf = rtw_dequeue_recvbuf(&precvpriv->recv_buf_pending_queue))) { + if (RTW_CANNOT_RUN(padapter)) { + RTW_INFO("recv_tasklet => bDriverStopped(%s) OR bSurpriseRemoved(%s)\n" + , rtw_is_drv_stopped(padapter)? "True" : "False" + , rtw_is_surprise_removed(padapter)? "True" : "False"); + break; + } + + recvbuf2recvframe(padapter, precvbuf); + + rtw_read_port(padapter, precvpriv->ff_hwaddr, 0, (unsigned char *)precvbuf); + } +} + +void usb_read_port_complete(struct urb *purb, struct pt_regs *regs) +{ + struct recv_buf *precvbuf = (struct recv_buf *)purb->context; + _adapter *padapter = (_adapter *)precvbuf->adapter; + struct recv_priv *precvpriv = &padapter->recvpriv; + + ATOMIC_DEC(&(precvpriv->rx_pending_cnt)); + + if (RTW_CANNOT_RX(padapter)) { + RTW_INFO("%s() RX Warning! bDriverStopped(%s) OR bSurpriseRemoved(%s)\n" + , __func__ + , rtw_is_drv_stopped(padapter) ? "True" : "False" + , rtw_is_surprise_removed(padapter) ? "True" : "False"); + return; + } + + if (purb->status == 0) { + + if ((purb->actual_length > MAX_RECVBUF_SZ) || (purb->actual_length < RXDESC_SIZE)) { + RTW_INFO("%s()-%d: urb->actual_length:%u, MAX_RECVBUF_SZ:%u, RXDESC_SIZE:%u\n" + , __FUNCTION__, __LINE__, purb->actual_length, MAX_RECVBUF_SZ, RXDESC_SIZE); + rtw_read_port(padapter, precvpriv->ff_hwaddr, 0, (unsigned char *)precvbuf); + } else { + rtw_reset_continual_io_error(adapter_to_dvobj(padapter)); + + precvbuf->transfer_len = purb->actual_length; + + rtw_enqueue_recvbuf(precvbuf, &precvpriv->recv_buf_pending_queue); + + tasklet_schedule(&precvpriv->recv_tasklet); + } + } else { + + RTW_INFO("###=> usb_read_port_complete => urb.status(%d)\n", purb->status); + + if (rtw_inc_and_chk_continual_io_error(adapter_to_dvobj(padapter)) == _TRUE) + rtw_set_surprise_removed(padapter); + + switch (purb->status) { + case -EINVAL: + case -EPIPE: + case -ENODEV: + case -ESHUTDOWN: + case -ENOENT: + rtw_set_drv_stopped(padapter); + break; + case -EPROTO: + case -EILSEQ: + case -ETIME: + case -ECOMM: + case -EOVERFLOW: + #ifdef DBG_CONFIG_ERROR_DETECT + { + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + pHalData->srestpriv.Wifi_Error_Status = USB_READ_PORT_FAIL; + } + #endif + rtw_read_port(padapter, precvpriv->ff_hwaddr, 0, (unsigned char *)precvbuf); + break; + case -EINPROGRESS: + RTW_INFO("ERROR: URB IS IN PROGRESS!/n"); + break; + default: + break; + } + } + +} + +u32 usb_read_port(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *rmem) +{ + int err; + unsigned int pipe; + u32 ret = _SUCCESS; + PURB purb = NULL; + struct recv_buf *precvbuf = (struct recv_buf *)rmem; + _adapter *adapter = pintfhdl->padapter; + struct dvobj_priv *pdvobj = adapter_to_dvobj(adapter); + struct pwrctrl_priv *pwrctl = dvobj_to_pwrctl(pdvobj); + struct recv_priv *precvpriv = &adapter->recvpriv; + struct usb_device *pusbd = pdvobj->pusbdev; + + + if (RTW_CANNOT_RX(adapter) || (precvbuf == NULL)) { + return _FAIL; + } + + usb_init_recvbuf(adapter, precvbuf); + + if (precvbuf->pbuf) { + ATOMIC_INC(&(precvpriv->rx_pending_cnt)); + purb = precvbuf->purb; + + /* translate DMA FIFO addr to pipehandle */ + pipe = ffaddr2pipehdl(pdvobj, addr); + + usb_fill_bulk_urb(purb, pusbd, pipe, + precvbuf->pbuf, + MAX_RECVBUF_SZ, + usb_read_port_complete, + precvbuf);/* context is precvbuf */ + + purb->transfer_dma = precvbuf->dma_transfer_addr; + purb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; + + err = usb_submit_urb(purb, GFP_ATOMIC); + if ((err) && (err != (-EPERM))) { + RTW_INFO("cannot submit rx in-token(err = 0x%08x),urb_status = %d\n", err, purb->status); + ret = _FAIL; + } + + } + + + return ret; +} +#else /* CONFIG_USE_USB_BUFFER_ALLOC_RX */ + +void usb_recv_tasklet(void *priv) +{ + _pkt *pskb; + _adapter *padapter = (_adapter *)priv; + struct recv_priv *precvpriv = &padapter->recvpriv; + struct recv_buf *precvbuf = NULL; + + while (NULL != (pskb = skb_dequeue(&precvpriv->rx_skb_queue))) { + + if (RTW_CANNOT_RUN(padapter)) { + RTW_INFO("recv_tasklet => bDriverStopped(%s) OR bSurpriseRemoved(%s)\n" + , rtw_is_drv_stopped(padapter) ? "True" : "False" + , rtw_is_surprise_removed(padapter) ? "True" : "False"); + #ifdef CONFIG_PREALLOC_RX_SKB_BUFFER + if (rtw_free_skb_premem(pskb) != 0) + #endif /* CONFIG_PREALLOC_RX_SKB_BUFFER */ + rtw_skb_free(pskb); + break; + } + + recvbuf2recvframe(padapter, pskb); + + skb_reset_tail_pointer(pskb); + pskb->len = 0; + + skb_queue_tail(&precvpriv->free_recv_skb_queue, pskb); + + precvbuf = rtw_dequeue_recvbuf(&precvpriv->recv_buf_pending_queue); + if (NULL != precvbuf) { + precvbuf->pskb = NULL; + rtw_read_port(padapter, precvpriv->ff_hwaddr, 0, (unsigned char *)precvbuf); + } + } +} + +void usb_read_port_complete(struct urb *purb, struct pt_regs *regs) +{ + struct recv_buf *precvbuf = (struct recv_buf *)purb->context; + _adapter *padapter = (_adapter *)precvbuf->adapter; + struct recv_priv *precvpriv = &padapter->recvpriv; + + ATOMIC_DEC(&(precvpriv->rx_pending_cnt)); + + if (RTW_CANNOT_RX(padapter)) { + RTW_INFO("%s() RX Warning! bDriverStopped(%s) OR bSurpriseRemoved(%s)\n" + , __func__ + , rtw_is_drv_stopped(padapter) ? "True" : "False" + , rtw_is_surprise_removed(padapter) ? "True" : "False"); + goto exit; + } + + if (purb->status == 0) { + + if ((purb->actual_length > MAX_RECVBUF_SZ) || (purb->actual_length < RXDESC_SIZE)) { + RTW_INFO("%s()-%d: urb->actual_length:%u, MAX_RECVBUF_SZ:%u, RXDESC_SIZE:%u\n" + , __FUNCTION__, __LINE__, purb->actual_length, MAX_RECVBUF_SZ, RXDESC_SIZE); + rtw_read_port(padapter, precvpriv->ff_hwaddr, 0, (unsigned char *)precvbuf); + } else { + rtw_reset_continual_io_error(adapter_to_dvobj(padapter)); + + precvbuf->transfer_len = purb->actual_length; + skb_put(precvbuf->pskb, purb->actual_length); + skb_queue_tail(&precvpriv->rx_skb_queue, precvbuf->pskb); + + #ifndef CONFIG_FIX_NR_BULKIN_BUFFER + if (skb_queue_len(&precvpriv->rx_skb_queue) <= 1) + #endif + tasklet_schedule(&precvpriv->recv_tasklet); + + precvbuf->pskb = NULL; + rtw_read_port(padapter, precvpriv->ff_hwaddr, 0, (unsigned char *)precvbuf); + } + } else { + + RTW_INFO("###=> usb_read_port_complete => urb.status(%d)\n", purb->status); + + if (rtw_inc_and_chk_continual_io_error(adapter_to_dvobj(padapter)) == _TRUE) + rtw_set_surprise_removed(padapter); + + switch (purb->status) { + case -EINVAL: + case -EPIPE: + case -ENODEV: + case -ESHUTDOWN: + case -ENOENT: + rtw_set_drv_stopped(padapter); + break; + case -EPROTO: + case -EILSEQ: + case -ETIME: + case -ECOMM: + case -EOVERFLOW: + #ifdef DBG_CONFIG_ERROR_DETECT + { + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + pHalData->srestpriv.Wifi_Error_Status = USB_READ_PORT_FAIL; + } + #endif + rtw_read_port(padapter, precvpriv->ff_hwaddr, 0, (unsigned char *)precvbuf); + break; + case -EINPROGRESS: + RTW_INFO("ERROR: URB IS IN PROGRESS!/n"); + break; + default: + break; + } + } + +exit: + return; +} + +u32 usb_read_port(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *rmem) +{ + int err; + unsigned int pipe; + u32 ret = _FAIL; + PURB purb = NULL; + struct recv_buf *precvbuf = (struct recv_buf *)rmem; + _adapter *adapter = pintfhdl->padapter; + struct dvobj_priv *pdvobj = adapter_to_dvobj(adapter); + struct recv_priv *precvpriv = &adapter->recvpriv; + struct usb_device *pusbd = pdvobj->pusbdev; + + + if (RTW_CANNOT_RX(adapter) || (precvbuf == NULL)) { + goto exit; + } + + usb_init_recvbuf(adapter, precvbuf); + + if (precvbuf->pskb == NULL) { + SIZE_PTR tmpaddr = 0; + SIZE_PTR alignment = 0; + + precvbuf->pskb = skb_dequeue(&precvpriv->free_recv_skb_queue); + if (NULL != precvbuf->pskb) + goto recv_buf_hook; + + #ifndef CONFIG_FIX_NR_BULKIN_BUFFER + precvbuf->pskb = rtw_skb_alloc(MAX_RECVBUF_SZ + RECVBUFF_ALIGN_SZ); + #endif + + if (precvbuf->pskb == NULL) { + if (0) + RTW_INFO("usb_read_port() enqueue precvbuf=%p\n", precvbuf); + /* enqueue precvbuf and wait for free skb */ + rtw_enqueue_recvbuf(precvbuf, &precvpriv->recv_buf_pending_queue); + goto exit; + } + + tmpaddr = (SIZE_PTR)precvbuf->pskb->data; + alignment = tmpaddr & (RECVBUFF_ALIGN_SZ - 1); + skb_reserve(precvbuf->pskb, (RECVBUFF_ALIGN_SZ - alignment)); + } + +recv_buf_hook: + precvbuf->phead = precvbuf->pskb->head; + precvbuf->pdata = precvbuf->pskb->data; + precvbuf->ptail = skb_tail_pointer(precvbuf->pskb); + precvbuf->pend = skb_end_pointer(precvbuf->pskb); + precvbuf->pbuf = precvbuf->pskb->data; + + purb = precvbuf->purb; + + /* translate DMA FIFO addr to pipehandle */ + pipe = ffaddr2pipehdl(pdvobj, addr); + + usb_fill_bulk_urb(purb, pusbd, pipe, + precvbuf->pbuf, + MAX_RECVBUF_SZ, + usb_read_port_complete, + precvbuf); + + err = usb_submit_urb(purb, GFP_ATOMIC); + if (err && err != (-EPERM)) { + RTW_INFO("cannot submit rx in-token(err = 0x%08x),urb_status = %d\n" + , err, purb->status); + goto exit; + } + + ATOMIC_INC(&(precvpriv->rx_pending_cnt)); + ret = _SUCCESS; + +exit: + + + return ret; +} +#endif /* CONFIG_USE_USB_BUFFER_ALLOC_RX */ + +#ifdef CONFIG_USB_INTERRUPT_IN_PIPE +void usb_read_interrupt_complete(struct urb *purb, struct pt_regs *regs) +{ + int err; + _adapter *padapter = (_adapter *)purb->context; + + if (RTW_CANNOT_RX(padapter)) { + RTW_INFO("%s() RX Warning! bDriverStopped(%s) OR bSurpriseRemoved(%s)\n" + , __func__ + , rtw_is_drv_stopped(padapter) ? "True" : "False" + , rtw_is_surprise_removed(padapter) ? "True" : "False"); + + return; + } + + if (purb->status == 0) {/*SUCCESS*/ + if (purb->actual_length > INTERRUPT_MSG_FORMAT_LEN) + RTW_INFO("usb_read_interrupt_complete: purb->actual_length > INTERRUPT_MSG_FORMAT_LEN(%d)\n", INTERRUPT_MSG_FORMAT_LEN); + + rtw_hal_interrupt_handler(padapter, purb->actual_length, purb->transfer_buffer); + + err = usb_submit_urb(purb, GFP_ATOMIC); + if ((err) && (err != (-EPERM))) + RTW_INFO("cannot submit interrupt in-token(err = 0x%08x),urb_status = %d\n", err, purb->status); + } else { + RTW_INFO("###=> usb_read_interrupt_complete => urb status(%d)\n", purb->status); + + switch (purb->status) { + case -EINVAL: + case -EPIPE: + case -ENODEV: + case -ESHUTDOWN: + case -ENOENT: + rtw_set_drv_stopped(padapter); + break; + case -EPROTO: + break; + case -EINPROGRESS: + RTW_INFO("ERROR: URB IS IN PROGRESS!/n"); + break; + default: + break; + } + } +} + +u32 usb_read_interrupt(struct intf_hdl *pintfhdl, u32 addr) +{ + int err; + unsigned int pipe; + u32 ret = _SUCCESS; + _adapter *adapter = pintfhdl->padapter; + struct dvobj_priv *pdvobj = adapter_to_dvobj(adapter); + struct recv_priv *precvpriv = &adapter->recvpriv; + struct usb_device *pusbd = pdvobj->pusbdev; + + + if (RTW_CANNOT_RX(adapter)) { + return _FAIL; + } + + /*translate DMA FIFO addr to pipehandle*/ + pipe = ffaddr2pipehdl(pdvobj, addr); + + usb_fill_int_urb(precvpriv->int_in_urb, pusbd, pipe, + precvpriv->int_in_buf, + INTERRUPT_MSG_FORMAT_LEN, + usb_read_interrupt_complete, + adapter, + 1); + + err = usb_submit_urb(precvpriv->int_in_urb, GFP_ATOMIC); + if ((err) && (err != (-EPERM))) { + RTW_INFO("cannot submit interrupt in-token(err = 0x%08x), urb_status = %d\n", err, precvpriv->int_in_urb->status); + ret = _FAIL; + } + + return ret; +} +#endif /* CONFIG_USB_INTERRUPT_IN_PIPE */ diff --git a/os_dep/linux/wifi_regd.c b/os_dep/linux/wifi_regd.c new file mode 100644 index 0000000..81e1dc7 --- /dev/null +++ b/os_dep/linux/wifi_regd.c @@ -0,0 +1,414 @@ +/****************************************************************************** + * + * Copyright(c) 2009-2010 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ + +#include + +#ifdef CONFIG_IOCTL_CFG80211 +static void rtw_regd_overide_flags(struct wiphy *wiphy, struct rf_ctl_t *rfctl) +{ + RT_CHANNEL_INFO *channel_set = rfctl->channel_set; + u8 max_chan_nums = rfctl->max_chan_nums; + struct ieee80211_supported_band *sband; + struct ieee80211_channel *ch; + unsigned int i, j; + u16 channel; + u32 freq; + + /* all channels disable */ + for (i = 0; i < NUM_NL80211_BANDS; i++) { + sband = wiphy->bands[i]; + if (!sband) + continue; + for (j = 0; j < sband->n_channels; j++) { + ch = &sband->channels[j]; + if (!ch) + continue; + ch->flags = IEEE80211_CHAN_DISABLED; + } + } + + /* channels apply by channel plans. */ + for (i = 0; i < max_chan_nums; i++) { + channel = channel_set[i].ChannelNum; + freq = rtw_ch2freq(channel); + ch = ieee80211_get_channel(wiphy, freq); + if (!ch) { + rtw_warn_on(1); + continue; + } + + /* enable */ + ch->flags = 0; + + if (channel_set[i].flags & RTW_CHF_DFS) { + /* + * before integrating with nl80211 flow + * bypass IEEE80211_CHAN_RADAR when configured with radar detection + * to prevent from hostapd blocking DFS channels + */ + if (rtw_rfctl_dfs_domain_unknown(rfctl)) + ch->flags |= IEEE80211_CHAN_RADAR; + } + + if (channel_set[i].flags & RTW_CHF_NO_IR) { + #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0)) + ch->flags |= IEEE80211_CHAN_NO_IBSS | IEEE80211_CHAN_PASSIVE_SCAN; + #else + ch->flags |= IEEE80211_CHAN_NO_IR; + #endif + } + } +} + +#ifdef CONFIG_REGD_SRC_FROM_OS +static void rtw_regd_apply_dfs_flags(struct rf_ctl_t *rfctl) +{ + RT_CHANNEL_INFO *channel_set = rfctl->channel_set; + u8 max_chan_nums = rfctl->max_chan_nums; + unsigned int i; + struct ieee80211_channel *chan; + + /* channels apply by channel plans. */ + for (i = 0; i < max_chan_nums; i++) { + chan = channel_set[i].os_chan; + if (channel_set[i].flags & RTW_CHF_DFS) { + /* + * before integrating with nl80211 flow + * clear IEEE80211_CHAN_RADAR when configured with radar detection + * to prevent from hostapd blocking DFS channels + */ + if (!rtw_rfctl_dfs_domain_unknown(rfctl)) + chan->flags &= ~IEEE80211_CHAN_RADAR; + } + } +} +#endif + +void rtw_regd_apply_flags(struct wiphy *wiphy) +{ + struct dvobj_priv *dvobj = wiphy_to_dvobj(wiphy); + struct rf_ctl_t *rfctl = dvobj_to_rfctl(dvobj); + + if (rfctl->regd_src == REGD_SRC_RTK_PRIV) + rtw_regd_overide_flags(wiphy, rfctl); +#ifdef CONFIG_REGD_SRC_FROM_OS + else if (rfctl->regd_src == REGD_SRC_OS) + rtw_regd_apply_dfs_flags(rfctl); +#endif + else + rtw_warn_on(1); +} + +#ifdef CONFIG_REGD_SRC_FROM_OS +/* init_channel_set_from_wiphy */ +u8 rtw_os_init_channel_set(_adapter *padapter, RT_CHANNEL_INFO *channel_set) +{ + struct wiphy *wiphy = adapter_to_wiphy(padapter); + struct rf_ctl_t *rfctl = adapter_to_rfctl(padapter); + struct registry_priv *regsty = adapter_to_regsty(padapter); + struct ieee80211_channel *chan; + u8 chanset_size = 0; + int i, j; + + _rtw_memset(channel_set, 0, sizeof(RT_CHANNEL_INFO) * MAX_CHANNEL_NUM); + + for (i = NL80211_BAND_2GHZ; i <= NL80211_BAND_5GHZ; i++) { + if (!wiphy->bands[i]) + continue; + for (j = 0; j < wiphy->bands[i]->n_channels; j++) { + chan = &wiphy->bands[i]->channels[j]; + if (chan->flags & IEEE80211_CHAN_DISABLED) + continue; + if (rtw_regsty_is_excl_chs(regsty, chan->hw_value)) + continue; + + if (chanset_size >= MAX_CHANNEL_NUM) { + RTW_WARN("chset size can't exceed MAX_CHANNEL_NUM(%u)\n", MAX_CHANNEL_NUM); + i = NL80211_BAND_5GHZ + 1; + break; + } + + channel_set[chanset_size].ChannelNum = chan->hw_value; + #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0)) + if (chan->flags & (IEEE80211_CHAN_NO_IBSS | IEEE80211_CHAN_PASSIVE_SCAN)) + #else + if (chan->flags & IEEE80211_CHAN_NO_IR) + #endif + channel_set[chanset_size].flags |= RTW_CHF_NO_IR; + if (chan->flags & IEEE80211_CHAN_RADAR) + channel_set[chanset_size].flags |= RTW_CHF_DFS; + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) + if (chan->flags & IEEE80211_CHAN_NO_HT40PLUS) + channel_set[chanset_size].flags |= RTW_CHF_NO_HT40U; + if (chan->flags & IEEE80211_CHAN_NO_HT40MINUS) + channel_set[chanset_size].flags |= RTW_CHF_NO_HT40L; + #endif + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) + if (chan->flags & IEEE80211_CHAN_NO_80MHZ) + channel_set[chanset_size].flags |= RTW_CHF_NO_80MHZ; + if (chan->flags & IEEE80211_CHAN_NO_160MHZ) + channel_set[chanset_size].flags |= RTW_CHF_NO_160MHZ; + #endif + channel_set[chanset_size].os_chan = chan; + chanset_size++; + } + } + +#if CONFIG_IEEE80211_BAND_5GHZ + #ifdef CONFIG_DFS_MASTER + for (i = 0; i < chanset_size; i++) + channel_set[i].non_ocp_end_time = rtw_get_current_time(); + #endif +#endif /* CONFIG_IEEE80211_BAND_5GHZ */ + + if (chanset_size) + RTW_INFO(FUNC_ADPT_FMT" ch num:%d\n" + , FUNC_ADPT_ARG(padapter), chanset_size); + else + RTW_WARN(FUNC_ADPT_FMT" final chset has no channel\n" + , FUNC_ADPT_ARG(padapter)); + + return chanset_size; +} + +s16 rtw_os_get_total_txpwr_regd_lmt_mbm(_adapter *adapter, u8 cch, enum channel_width bw) +{ + struct wiphy *wiphy = adapter_to_wiphy(adapter); + s16 mbm = UNSPECIFIED_MBM; + u8 *op_chs; + u8 op_ch_num; + u8 i; + u32 freq; + struct ieee80211_channel *ch; + + if (!rtw_get_op_chs_by_cch_bw(cch, bw, &op_chs, &op_ch_num)) + goto exit; + + for (i = 0; i < op_ch_num; i++) { + freq = rtw_ch2freq(op_chs[i]); + ch = ieee80211_get_channel(wiphy, freq); + if (!ch) { + rtw_warn_on(1); + continue; + } + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)) + mbm = rtw_min(mbm, ch->max_reg_power * MBM_PDBM); + #else + /* require max_power == 0 (therefore orig_mpwr set to 0) when wiphy registration */ + mbm = rtw_min(mbm, ch->max_power * MBM_PDBM); + #endif + } + +exit: + return mbm; +} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)) +static enum rtw_dfs_regd nl80211_dfs_regions_to_rtw_dfs_region(enum nl80211_dfs_regions region) +{ + switch (region) { + case NL80211_DFS_FCC: + return RTW_DFS_REGD_FCC; + case NL80211_DFS_ETSI: + return RTW_DFS_REGD_ETSI; + case NL80211_DFS_JP: + return RTW_DFS_REGD_MKK; + case NL80211_DFS_UNSET: + default: + return RTW_DFS_REGD_NONE; + } +}; +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)) */ +#endif /* CONFIG_REGD_SRC_FROM_OS */ + +#ifdef CONFIG_RTW_DEBUG +static const char *nl80211_reg_initiator_str(enum nl80211_reg_initiator initiator) +{ + switch (initiator) { + case NL80211_REGDOM_SET_BY_DRIVER: + return "DRIVER"; + case NL80211_REGDOM_SET_BY_CORE: + return "CORE"; + case NL80211_REGDOM_SET_BY_USER: + return "USER"; + case NL80211_REGDOM_SET_BY_COUNTRY_IE: + return "COUNTRY_IE"; + } + rtw_warn_on(1); + return "UNKNOWN"; +} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) +static const char *nl80211_user_reg_hint_type_str(enum nl80211_user_reg_hint_type type) +{ + switch (type) { + case NL80211_USER_REG_HINT_USER: + return "USER"; + case NL80211_USER_REG_HINT_CELL_BASE: + return "CELL_BASE"; + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)) + case NL80211_USER_REG_HINT_INDOOR: + return "INDOOR"; + #endif + } + rtw_warn_on(1); + return "UNKNOWN"; +} +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)) +static const char *nl80211_dfs_regions_str(enum nl80211_dfs_regions region) +{ + switch (region) { + case NL80211_DFS_UNSET: + return "UNSET"; + case NL80211_DFS_FCC: + return "FCC"; + case NL80211_DFS_ETSI: + return "ETSI"; + case NL80211_DFS_JP: + return "JP"; + } + rtw_warn_on(1); + return "UNKNOWN"; +}; +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)) */ + +static const char *environment_cap_str(enum environment_cap cap) +{ + switch (cap) { + case ENVIRON_ANY: + return "ANY"; + case ENVIRON_INDOOR: + return "INDOOR"; + case ENVIRON_OUTDOOR: + return "OUTDOOR"; + } + rtw_warn_on(1); + return "UNKNOWN"; +} + +static void dump_requlatory_request(void *sel, struct regulatory_request *request) +{ + u8 alpha2_len; + + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)) + alpha2_len = 3; + #else + alpha2_len = 2; + #endif + + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)) + RTW_PRINT_SEL(sel, "initiator:%s, wiphy_idx:%d, type:%s\n" + , nl80211_reg_initiator_str(request->initiator) + , request->wiphy_idx + , nl80211_user_reg_hint_type_str(request->user_reg_hint_type)); + #else + RTW_PRINT_SEL(sel, "initiator:%s, wiphy_idx:%d\n" + , nl80211_reg_initiator_str(request->initiator) + , request->wiphy_idx); + #endif + + RTW_PRINT_SEL(sel, "alpha2:%.*s\n", alpha2_len, request->alpha2); + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)) + RTW_PRINT_SEL(sel, "dfs_region:%s\n", nl80211_dfs_regions_str(request->dfs_region)); + #endif + + RTW_PRINT_SEL(sel, "intersect:%d\n", request->intersect); + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)) + RTW_PRINT_SEL(sel, "processed:%d\n", request->processed); + #endif + #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)) + RTW_PRINT_SEL(sel, "country_ie_checksum:0x%08x\n", request->country_ie_checksum); + #endif + + RTW_PRINT_SEL(sel, "country_ie_env:%s\n", environment_cap_str(request->country_ie_env)); +} +#endif /* CONFIG_RTW_DEBUG */ + +static void rtw_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request) +{ + struct dvobj_priv *dvobj = wiphy_to_dvobj(wiphy); + struct rf_ctl_t *rfctl = dvobj_to_rfctl(dvobj); + struct registry_priv *regsty = dvobj_to_regsty(dvobj); + +#ifdef CONFIG_RTW_DEBUG + if (rtw_drv_log_level >= _DRV_INFO_) { + RTW_INFO(FUNC_WIPHY_FMT"\n", FUNC_WIPHY_ARG(wiphy)); + dump_requlatory_request(RTW_DBGDUMP, request); + } +#endif + +#ifdef CONFIG_REGD_SRC_FROM_OS + if (REGSTY_REGD_SRC_FROM_OS(regsty)) { + enum rtw_dfs_regd dfs_region = RTW_DFS_REGD_NONE; + + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)) + dfs_region = nl80211_dfs_regions_to_rtw_dfs_region(request->dfs_region); + #endif + + /* trigger command to sync regulatory form OS */ + rtw_sync_os_regd_cmd(wiphy_to_adapter(wiphy), RTW_CMDF_WAIT_ACK, request->alpha2, dfs_region); + } else +#endif + { + /* use alpha2 as input to select the corresponding channel plan settings defined by Realtek */ + switch (request->initiator) { + case NL80211_REGDOM_SET_BY_DRIVER: + break; + case NL80211_REGDOM_SET_BY_CORE: + break; + case NL80211_REGDOM_SET_BY_USER: + rtw_set_country(wiphy_to_adapter(wiphy), request->alpha2); + break; + case NL80211_REGDOM_SET_BY_COUNTRY_IE: + break; + } + + rtw_regd_apply_flags(wiphy); + } +} + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0)) +static int rtw_reg_notifier_return(struct wiphy *wiphy, struct regulatory_request *request) +{ + rtw_reg_notifier(wiphy, request); + return 0; +} +#endif + +int rtw_regd_init(struct wiphy *wiphy) +{ +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0)) + wiphy->reg_notifier = rtw_reg_notifier_return; +#else + wiphy->reg_notifier = rtw_reg_notifier; +#endif + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0)) + wiphy->flags &= ~WIPHY_FLAG_STRICT_REGULATORY; + wiphy->flags &= ~WIPHY_FLAG_DISABLE_BEACON_HINTS; +#else + wiphy->regulatory_flags &= ~REGULATORY_STRICT_REG; + wiphy->regulatory_flags &= ~REGULATORY_DISABLE_BEACON_HINTS; +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)) + wiphy->regulatory_flags |= REGULATORY_IGNORE_STALE_KICKOFF; +#endif + + return 0; +} +#endif /* CONFIG_IOCTL_CFG80211 */ diff --git a/os_dep/linux/wifi_regd.h b/os_dep/linux/wifi_regd.h new file mode 100644 index 0000000..4e147fc --- /dev/null +++ b/os_dep/linux/wifi_regd.h @@ -0,0 +1,27 @@ +/****************************************************************************** + * + * Copyright(c) 2009-2010 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ + +#ifndef __WIFI_REGD_H__ +#define __WIFI_REGD_H__ + +void rtw_regd_apply_flags(struct wiphy *wiphy); +#ifdef CONFIG_REGD_SRC_FROM_OS +struct _RT_CHANNEL_INFO; +u8 rtw_os_init_channel_set(_adapter *padapter, struct _RT_CHANNEL_INFO *channel_set); +s16 rtw_os_get_total_txpwr_regd_lmt_mbm(_adapter *adapter, u8 cch, enum channel_width bw); +#endif +int rtw_regd_init(struct wiphy *wiphy); + +#endif /* __WIFI_REGD_H__ */ diff --git a/os_dep/linux/xmit_linux.c b/os_dep/linux/xmit_linux.c new file mode 100644 index 0000000..efe11c9 --- /dev/null +++ b/os_dep/linux/xmit_linux.c @@ -0,0 +1,554 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#define _XMIT_OSDEP_C_ + +#include + +#define DBG_DUMP_OS_QUEUE_CTL 0 + +uint rtw_remainder_len(struct pkt_file *pfile) +{ + return pfile->buf_len - ((SIZE_PTR)(pfile->cur_addr) - (SIZE_PTR)(pfile->buf_start)); +} + +void _rtw_open_pktfile(_pkt *pktptr, struct pkt_file *pfile) +{ + + pfile->pkt = pktptr; + pfile->cur_addr = pfile->buf_start = pktptr->data; + pfile->pkt_len = pfile->buf_len = pktptr->len; + + pfile->cur_buffer = pfile->buf_start ; + +} + +uint _rtw_pktfile_read(struct pkt_file *pfile, u8 *rmem, uint rlen) +{ + uint len = 0; + + + len = rtw_remainder_len(pfile); + len = (rlen > len) ? len : rlen; + + if (rmem) + skb_copy_bits(pfile->pkt, pfile->buf_len - pfile->pkt_len, rmem, len); + + pfile->cur_addr += len; + pfile->pkt_len -= len; + + + return len; +} + +sint rtw_endofpktfile(struct pkt_file *pfile) +{ + + if (pfile->pkt_len == 0) { + return _TRUE; + } + + + return _FALSE; +} + +void rtw_set_tx_chksum_offload(_pkt *pkt, struct pkt_attrib *pattrib) +{ +#ifdef CONFIG_TCP_CSUM_OFFLOAD_TX + struct sk_buff *skb = (struct sk_buff *)pkt; + struct iphdr *iph = NULL; + struct ipv6hdr *i6ph = NULL; + struct udphdr *uh = NULL; + struct tcphdr *th = NULL; + u8 protocol = 0xFF; + + if (skb->protocol == htons(ETH_P_IP)) { + iph = (struct iphdr *)skb_network_header(skb); + protocol = iph->protocol; + } else if (skb->protocol == htons(ETH_P_IPV6)) { + i6ph = (struct ipv6hdr *)skb_network_header(skb); + protocol = i6ph->nexthdr; + } else + {} + + /* HW unable to compute CSUM if header & payload was be encrypted by SW(cause TXDMA error) */ + if (pattrib->bswenc == _TRUE) { + if (skb->ip_summed == CHECKSUM_PARTIAL) + skb_checksum_help(skb); + return; + } + + /* For HW rule, clear ipv4_csum & UDP/TCP_csum if it is UDP/TCP packet */ + switch (protocol) { + case IPPROTO_UDP: + uh = (struct udphdr *)skb_transport_header(skb); + uh->check = 0; + if (iph) + iph->check = 0; + pattrib->hw_csum = _TRUE; + break; + case IPPROTO_TCP: + th = (struct tcphdr *)skb_transport_header(skb); + th->check = 0; + if (iph) + iph->check = 0; + pattrib->hw_csum = _TRUE; + break; + default: + break; + } +#endif + +} + +int rtw_os_xmit_resource_alloc(_adapter *padapter, struct xmit_buf *pxmitbuf, u32 alloc_sz, u8 flag) +{ + if (alloc_sz > 0) { +#ifdef CONFIG_USE_USB_BUFFER_ALLOC_TX + struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter); + struct usb_device *pusbd = pdvobjpriv->pusbdev; + + pxmitbuf->pallocated_buf = rtw_usb_buffer_alloc(pusbd, (size_t)alloc_sz, &pxmitbuf->dma_transfer_addr); + pxmitbuf->pbuf = pxmitbuf->pallocated_buf; + if (pxmitbuf->pallocated_buf == NULL) + return _FAIL; +#else /* CONFIG_USE_USB_BUFFER_ALLOC_TX */ + + pxmitbuf->pallocated_buf = rtw_zmalloc(alloc_sz); + if (pxmitbuf->pallocated_buf == NULL) + return _FAIL; + + pxmitbuf->pbuf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(pxmitbuf->pallocated_buf), XMITBUF_ALIGN_SZ); + +#endif /* CONFIG_USE_USB_BUFFER_ALLOC_TX */ + } + + if (flag) { +#ifdef CONFIG_USB_HCI + int i; + for (i = 0; i < 8; i++) { + pxmitbuf->pxmit_urb[i] = usb_alloc_urb(0, GFP_KERNEL); + if (pxmitbuf->pxmit_urb[i] == NULL) { + RTW_INFO("pxmitbuf->pxmit_urb[i]==NULL"); + return _FAIL; + } + } +#endif + } + + return _SUCCESS; +} + +void rtw_os_xmit_resource_free(_adapter *padapter, struct xmit_buf *pxmitbuf, u32 free_sz, u8 flag) +{ + if (flag) { +#ifdef CONFIG_USB_HCI + int i; + + for (i = 0; i < 8; i++) { + if (pxmitbuf->pxmit_urb[i]) { + /* usb_kill_urb(pxmitbuf->pxmit_urb[i]); */ + usb_free_urb(pxmitbuf->pxmit_urb[i]); + } + } +#endif + } + + if (free_sz > 0) { +#ifdef CONFIG_USE_USB_BUFFER_ALLOC_TX + struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter); + struct usb_device *pusbd = pdvobjpriv->pusbdev; + + rtw_usb_buffer_free(pusbd, (size_t)free_sz, pxmitbuf->pallocated_buf, pxmitbuf->dma_transfer_addr); + pxmitbuf->pallocated_buf = NULL; + pxmitbuf->dma_transfer_addr = 0; +#else /* CONFIG_USE_USB_BUFFER_ALLOC_TX */ + if (pxmitbuf->pallocated_buf) + rtw_mfree(pxmitbuf->pallocated_buf, free_sz); +#endif /* CONFIG_USE_USB_BUFFER_ALLOC_TX */ + } +} + +void dump_os_queue(void *sel, _adapter *padapter) +{ + struct net_device *ndev = padapter->pnetdev; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + int i; + + for (i = 0; i < 4; i++) { + RTW_PRINT_SEL(sel, "os_queue[%d]:%s\n" + , i, __netif_subqueue_stopped(ndev, i) ? "stopped" : "waked"); + } +#else + RTW_PRINT_SEL(sel, "os_queue:%s\n" + , netif_queue_stopped(ndev) ? "stopped" : "waked"); +#endif +} + +#define WMM_XMIT_THRESHOLD (NR_XMITFRAME*2/5) + +static inline bool rtw_os_need_wake_queue(_adapter *padapter, u16 os_qid) +{ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + struct xmit_priv *pxmitpriv = &padapter->xmitpriv; + + if (padapter->registrypriv.wifi_spec) { + if (pxmitpriv->hwxmits[os_qid].accnt < WMM_XMIT_THRESHOLD) + return _TRUE; +#ifdef DBG_CONFIG_ERROR_DETECT +#ifdef DBG_CONFIG_ERROR_RESET + } else if (rtw_hal_sreset_inprogress(padapter) == _TRUE) { + return _FALSE; +#endif/* #ifdef DBG_CONFIG_ERROR_RESET */ +#endif/* #ifdef DBG_CONFIG_ERROR_DETECT */ + } else { +#ifdef CONFIG_MCC_MODE + if (MCC_EN(padapter)) { + if (rtw_hal_check_mcc_status(padapter, MCC_STATUS_DOING_MCC) + && MCC_STOP(padapter)) + return _FALSE; + } +#endif /* CONFIG_MCC_MODE */ + return _TRUE; + } + return _FALSE; +#else +#ifdef CONFIG_MCC_MODE + if (MCC_EN(padapter)) { + if (rtw_hal_check_mcc_status(padapter, MCC_STATUS_DOING_MCC) + && MCC_STOP(padapter)) + return _FALSE; + } +#endif /* CONFIG_MCC_MODE */ + return _TRUE; +#endif +} + +static inline bool rtw_os_need_stop_queue(_adapter *padapter, u16 os_qid) +{ + struct xmit_priv *pxmitpriv = &padapter->xmitpriv; +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + if (padapter->registrypriv.wifi_spec) { + /* No free space for Tx, tx_worker is too slow */ + if (pxmitpriv->hwxmits[os_qid].accnt > WMM_XMIT_THRESHOLD) + return _TRUE; + } else { + if (pxmitpriv->free_xmitframe_cnt <= 4) + return _TRUE; + } +#else + if (pxmitpriv->free_xmitframe_cnt <= 4) + return _TRUE; +#endif + return _FALSE; +} + +void rtw_os_pkt_complete(_adapter *padapter, _pkt *pkt) +{ + rtw_skb_free(pkt); +} + +void rtw_os_xmit_complete(_adapter *padapter, struct xmit_frame *pxframe) +{ + if (pxframe->pkt) + rtw_os_pkt_complete(padapter, pxframe->pkt); + + pxframe->pkt = NULL; +} + +void rtw_os_xmit_schedule(_adapter *padapter) +{ +#if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) + _adapter *pri_adapter; + + if (!padapter) + return; + pri_adapter = GET_PRIMARY_ADAPTER(padapter); + + if (_rtw_queue_empty(&padapter->xmitpriv.pending_xmitbuf_queue) == _FALSE) + _rtw_up_sema(&pri_adapter->xmitpriv.xmit_sema); + + +#else + _irqL irqL; + struct xmit_priv *pxmitpriv; + + if (!padapter) + return; + + pxmitpriv = &padapter->xmitpriv; + + _enter_critical_bh(&pxmitpriv->lock, &irqL); + + if (rtw_txframes_pending(padapter)) + tasklet_hi_schedule(&pxmitpriv->xmit_tasklet); + + _exit_critical_bh(&pxmitpriv->lock, &irqL); + +#if defined(CONFIG_PCI_HCI) && defined(CONFIG_XMIT_THREAD_MODE) + if (_rtw_queue_empty(&padapter->xmitpriv.pending_xmitbuf_queue) == _FALSE) + _rtw_up_sema(&padapter->xmitpriv.xmit_sema); +#endif + + +#endif +} + +void rtw_os_check_wakup_queue(_adapter *adapter, u16 os_qid) +{ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + if (rtw_os_need_wake_queue(adapter, os_qid)) { + if (DBG_DUMP_OS_QUEUE_CTL) + RTW_INFO(FUNC_ADPT_FMT": netif_wake_subqueue[%d]\n", FUNC_ADPT_ARG(adapter), os_qid); + netif_wake_subqueue(adapter->pnetdev, os_qid); + } +#else + if (rtw_os_need_wake_queue(adapter, 0)) { + if (DBG_DUMP_OS_QUEUE_CTL) + RTW_INFO(FUNC_ADPT_FMT": netif_wake_queue\n", FUNC_ADPT_ARG(adapter)); + netif_wake_queue(adapter->pnetdev); + } +#endif +} + +bool rtw_os_check_stop_queue(_adapter *adapter, u16 os_qid) +{ + bool busy = _FALSE; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + if (rtw_os_need_stop_queue(adapter, os_qid)) { + if (DBG_DUMP_OS_QUEUE_CTL) + RTW_INFO(FUNC_ADPT_FMT": netif_stop_subqueue[%d]\n", FUNC_ADPT_ARG(adapter), os_qid); + netif_stop_subqueue(adapter->pnetdev, os_qid); + busy = _TRUE; + } +#else + if (rtw_os_need_stop_queue(adapter, 0)) { + if (DBG_DUMP_OS_QUEUE_CTL) + RTW_INFO(FUNC_ADPT_FMT": netif_stop_queue\n", FUNC_ADPT_ARG(adapter)); + rtw_netif_stop_queue(adapter->pnetdev); + busy = _TRUE; + } +#endif + return busy; +} + +void rtw_os_wake_queue_at_free_stainfo(_adapter *padapter, int *qcnt_freed) +{ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + int i; + + for (i = 0; i < 4; i++) { + if (qcnt_freed[i] == 0) + continue; + + if (rtw_os_need_wake_queue(padapter, i)) { + if (DBG_DUMP_OS_QUEUE_CTL) + RTW_INFO(FUNC_ADPT_FMT": netif_wake_subqueue[%d]\n", FUNC_ADPT_ARG(padapter), i); + netif_wake_subqueue(padapter->pnetdev, i); + } + } +#else + if (qcnt_freed[0] || qcnt_freed[1] || qcnt_freed[2] || qcnt_freed[3]) { + if (rtw_os_need_wake_queue(padapter, 0)) { + if (DBG_DUMP_OS_QUEUE_CTL) + RTW_INFO(FUNC_ADPT_FMT": netif_wake_queue\n", FUNC_ADPT_ARG(padapter)); + netif_wake_queue(padapter->pnetdev); + } + } +#endif +} + +int _rtw_xmit_entry(_pkt *pkt, _nic_hdl pnetdev) +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(pnetdev); + struct xmit_priv *pxmitpriv = &padapter->xmitpriv; +#ifdef CONFIG_TCP_CSUM_OFFLOAD_TX + struct sk_buff *skb = pkt; + struct sk_buff *segs, *nskb; + netdev_features_t features = padapter->pnetdev->features; +#endif + u16 os_qid = 0; + s32 res = 0; + + if (padapter->registrypriv.mp_mode) { + RTW_INFO("MP_TX_DROP_OS_FRAME\n"); + goto drop_packet; + } + DBG_COUNTER(padapter->tx_logs.os_tx); + + if ((rtw_if_up(padapter) == _FALSE) +#ifdef CONFIG_LAYER2_ROAMING + &&(!padapter->mlmepriv.roam_network) +#endif + ){ + DBG_COUNTER(padapter->tx_logs.os_tx_err_up); + #ifdef DBG_TX_DROP_FRAME + RTW_INFO("DBG_TX_DROP_FRAME %s if_up fail\n", __FUNCTION__); + #endif + goto drop_packet; + } + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + os_qid = skb_get_queue_mapping(pkt); +#endif + +#ifdef CONFIG_TCP_CSUM_OFFLOAD_TX + if (skb_shinfo(skb)->gso_size) { + /* split a big(65k) skb into several small(1.5k) skbs */ + features &= ~(NETIF_F_TSO | NETIF_F_TSO6); + segs = skb_gso_segment(skb, features); + if (IS_ERR(segs) || !segs) + goto drop_packet; + + do { + nskb = segs; + segs = segs->next; + nskb->next = NULL; + rtw_mstat_update( MSTAT_TYPE_SKB, MSTAT_ALLOC_SUCCESS, nskb->truesize); + res = rtw_xmit(padapter, &nskb, os_qid); + if (res < 0) { + #ifdef DBG_TX_DROP_FRAME + RTW_INFO("DBG_TX_DROP_FRAME %s rtw_xmit fail\n", __FUNCTION__); + #endif + pxmitpriv->tx_drop++; + rtw_os_pkt_complete(padapter, nskb); + } + } while (segs); + rtw_os_pkt_complete(padapter, skb); + goto exit; + } +#endif + + res = rtw_xmit(padapter, &pkt, os_qid); + if (res < 0) { + #ifdef DBG_TX_DROP_FRAME + RTW_INFO("DBG_TX_DROP_FRAME %s rtw_xmit fail\n", __FUNCTION__); + #endif + goto drop_packet; + } + + goto exit; + +drop_packet: + pxmitpriv->tx_drop++; + rtw_os_pkt_complete(padapter, pkt); + +exit: + + + return 0; +} + +#ifdef CONFIG_CUSTOMER_ALIBABA_GENERAL +int check_alibaba_meshpkt(struct sk_buff *skb) +{ + u16 protocol; + + if (skb) + return (htons(skb->protocol) == ETH_P_ALL); + + return _FALSE; +} + +s32 rtw_alibaba_mesh_xmit_entry(_pkt *pkt, struct net_device *ndev) +{ + u16 frame_ctl; + + _adapter *padapter = (_adapter *)rtw_netdev_priv(ndev); + struct pkt_file pktfile; + struct rtw_ieee80211_hdr *pwlanhdr; + struct pkt_attrib *pattrib; + struct xmit_frame *pmgntframe; + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct xmit_priv *pxmitpriv = &(padapter->xmitpriv); + unsigned char *pframe; + struct sk_buff *skb = (struct sk_buff *)pkt; + int len = skb->len; + + rtw_mstat_update(MSTAT_TYPE_SKB, MSTAT_ALLOC_SUCCESS, skb->truesize); + + pmgntframe = alloc_mgtxmitframe(pxmitpriv); + if (pmgntframe == NULL) { + goto fail; + return -1; + } + + pattrib = &pmgntframe->attrib; + update_mgntframe_attrib(padapter, pattrib); + _rtw_memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET); + pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET; + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + _rtw_open_pktfile(pkt, &pktfile); + _rtw_pktfile_read(&pktfile, pframe, len); + + pattrib->type = pframe[0] & 0x0C; + pattrib->subtype = pframe[0] & 0xF0; + pattrib->raid = rtw_get_mgntframe_raid(padapter, WIRELESS_11G); + pattrib->rate = MGN_24M; + pattrib->pktlen = len; + SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq); + pmlmeext->mgnt_seq++; + + RTW_DBG_DUMP("rtw_alibaba_mesh_xmit_entry payload:", skb->data, len); + + pattrib->last_txcmdsz = pattrib->pktlen; + dump_mgntframe(padapter, pmgntframe); + +fail: + rtw_skb_free(skb); + return 0; +} +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32)) +netdev_tx_t rtw_xmit_entry(_pkt *pkt, _nic_hdl pnetdev) +#else +int rtw_xmit_entry(_pkt *pkt, _nic_hdl pnetdev) +#endif +{ + _adapter *padapter = (_adapter *)rtw_netdev_priv(pnetdev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + int ret = 0; + + if (pkt) { +#ifdef CONFIG_CUSTOMER_ALIBABA_GENERAL + if (check_alibaba_meshpkt(pkt)) { + ret = rtw_alibaba_mesh_xmit_entry(pkt, pnetdev); + goto out; + } +#endif + if (check_fwstate(pmlmepriv, WIFI_MONITOR_STATE) == _TRUE) { +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)) + rtw_monitor_xmit_entry((struct sk_buff *)pkt, pnetdev); +#endif + } + else { + rtw_mstat_update(MSTAT_TYPE_SKB, MSTAT_ALLOC_SUCCESS, pkt->truesize); + ret = _rtw_xmit_entry(pkt, pnetdev); + } + + } + +#ifdef CONFIG_CUSTOMER_ALIBABA_GENERAL +out: +#endif +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32)) + return (ret == 0) ? NETDEV_TX_OK : NETDEV_TX_BUSY; +#else + return ret; +#endif +} diff --git a/os_dep/osdep_service.c b/os_dep/osdep_service.c new file mode 100644 index 0000000..98e8bd8 --- /dev/null +++ b/os_dep/osdep_service.c @@ -0,0 +1,3473 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ + + +#define _OSDEP_SERVICE_C_ + +#include + +#define RT_TAG '1178' + +#ifdef DBG_MEMORY_LEAK +#ifdef PLATFORM_LINUX +atomic_t _malloc_cnt = ATOMIC_INIT(0); +atomic_t _malloc_size = ATOMIC_INIT(0); +#endif +#endif /* DBG_MEMORY_LEAK */ + + +#if defined(PLATFORM_LINUX) +/* +* Translate the OS dependent @param error_code to OS independent RTW_STATUS_CODE +* @return: one of RTW_STATUS_CODE +*/ +inline int RTW_STATUS_CODE(int error_code) +{ + if (error_code >= 0) + return _SUCCESS; + + switch (error_code) { + /* case -ETIMEDOUT: */ + /* return RTW_STATUS_TIMEDOUT; */ + default: + return _FAIL; + } +} +#else +inline int RTW_STATUS_CODE(int error_code) +{ + return error_code; +} +#endif + +u32 rtw_atoi(u8 *s) +{ + + int num = 0, flag = 0; + int i; + for (i = 0; i <= strlen(s); i++) { + if (s[i] >= '0' && s[i] <= '9') + num = num * 10 + s[i] - '0'; + else if (s[0] == '-' && i == 0) + flag = 1; + else + break; + } + + if (flag == 1) + num = num * -1; + + return num; + +} + +inline void *_rtw_vmalloc(u32 sz) +{ + void *pbuf; +#ifdef PLATFORM_LINUX + pbuf = vmalloc(sz); +#endif +#ifdef PLATFORM_FREEBSD + pbuf = malloc(sz, M_DEVBUF, M_NOWAIT); +#endif + +#ifdef PLATFORM_WINDOWS + NdisAllocateMemoryWithTag(&pbuf, sz, RT_TAG); +#endif + +#ifdef DBG_MEMORY_LEAK +#ifdef PLATFORM_LINUX + if (pbuf != NULL) { + atomic_inc(&_malloc_cnt); + atomic_add(sz, &_malloc_size); + } +#endif +#endif /* DBG_MEMORY_LEAK */ + + return pbuf; +} + +inline void *_rtw_zvmalloc(u32 sz) +{ + void *pbuf; +#ifdef PLATFORM_LINUX + pbuf = _rtw_vmalloc(sz); + if (pbuf != NULL) + memset(pbuf, 0, sz); +#endif +#ifdef PLATFORM_FREEBSD + pbuf = malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT); +#endif +#ifdef PLATFORM_WINDOWS + NdisAllocateMemoryWithTag(&pbuf, sz, RT_TAG); + if (pbuf != NULL) + NdisFillMemory(pbuf, sz, 0); +#endif + + return pbuf; +} + +inline void _rtw_vmfree(void *pbuf, u32 sz) +{ +#ifdef PLATFORM_LINUX + vfree(pbuf); +#endif +#ifdef PLATFORM_FREEBSD + free(pbuf, M_DEVBUF); +#endif +#ifdef PLATFORM_WINDOWS + NdisFreeMemory(pbuf, sz, 0); +#endif + +#ifdef DBG_MEMORY_LEAK +#ifdef PLATFORM_LINUX + atomic_dec(&_malloc_cnt); + atomic_sub(sz, &_malloc_size); +#endif +#endif /* DBG_MEMORY_LEAK */ +} + +void *_rtw_malloc(u32 sz) +{ + void *pbuf = NULL; + +#ifdef PLATFORM_LINUX +#ifdef RTK_DMP_PLATFORM + if (sz > 0x4000) + pbuf = dvr_malloc(sz); + else +#endif + pbuf = kmalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL); + +#endif +#ifdef PLATFORM_FREEBSD + pbuf = malloc(sz, M_DEVBUF, M_NOWAIT); +#endif +#ifdef PLATFORM_WINDOWS + + NdisAllocateMemoryWithTag(&pbuf, sz, RT_TAG); + +#endif + +#ifdef DBG_MEMORY_LEAK +#ifdef PLATFORM_LINUX + if (pbuf != NULL) { + atomic_inc(&_malloc_cnt); + atomic_add(sz, &_malloc_size); + } +#endif +#endif /* DBG_MEMORY_LEAK */ + + return pbuf; + +} + + +void *_rtw_zmalloc(u32 sz) +{ +#ifdef PLATFORM_FREEBSD + return malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT); +#else /* PLATFORM_FREEBSD */ + void *pbuf = _rtw_malloc(sz); + + if (pbuf != NULL) { + +#ifdef PLATFORM_LINUX + memset(pbuf, 0, sz); +#endif + +#ifdef PLATFORM_WINDOWS + NdisFillMemory(pbuf, sz, 0); +#endif + + } + + return pbuf; +#endif /* PLATFORM_FREEBSD */ +} + +void _rtw_mfree(void *pbuf, u32 sz) +{ + +#ifdef PLATFORM_LINUX +#ifdef RTK_DMP_PLATFORM + if (sz > 0x4000) + dvr_free(pbuf); + else +#endif + kfree(pbuf); + +#endif +#ifdef PLATFORM_FREEBSD + free(pbuf, M_DEVBUF); +#endif +#ifdef PLATFORM_WINDOWS + + NdisFreeMemory(pbuf, sz, 0); + +#endif + +#ifdef DBG_MEMORY_LEAK +#ifdef PLATFORM_LINUX + atomic_dec(&_malloc_cnt); + atomic_sub(sz, &_malloc_size); +#endif +#endif /* DBG_MEMORY_LEAK */ + +} + +#ifdef PLATFORM_FREEBSD +/* review again */ +struct sk_buff *dev_alloc_skb(unsigned int size) +{ + struct sk_buff *skb = NULL; + u8 *data = NULL; + + /* skb = _rtw_zmalloc(sizeof(struct sk_buff)); */ /* for skb->len, etc. */ + skb = _rtw_malloc(sizeof(struct sk_buff)); + if (!skb) + goto out; + data = _rtw_malloc(size); + if (!data) + goto nodata; + + skb->head = (unsigned char *)data; + skb->data = (unsigned char *)data; + skb->tail = (unsigned char *)data; + skb->end = (unsigned char *)data + size; + skb->len = 0; + /* printf("%s()-%d: skb=%p, skb->head = %p\n", __FUNCTION__, __LINE__, skb, skb->head); */ + +out: + return skb; +nodata: + _rtw_mfree(skb, sizeof(struct sk_buff)); + skb = NULL; + goto out; + +} + +void dev_kfree_skb_any(struct sk_buff *skb) +{ + /* printf("%s()-%d: skb->head = %p\n", __FUNCTION__, __LINE__, skb->head); */ + if (skb->head) + _rtw_mfree(skb->head, 0); + /* printf("%s()-%d: skb = %p\n", __FUNCTION__, __LINE__, skb); */ + if (skb) + _rtw_mfree(skb, 0); +} +struct sk_buff *skb_clone(const struct sk_buff *skb) +{ + return NULL; +} + +#endif /* PLATFORM_FREEBSD */ + +inline struct sk_buff *_rtw_skb_alloc(u32 sz) +{ +#ifdef PLATFORM_LINUX + return __dev_alloc_skb(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL); +#endif /* PLATFORM_LINUX */ + +#ifdef PLATFORM_FREEBSD + return dev_alloc_skb(sz); +#endif /* PLATFORM_FREEBSD */ +} + +inline void _rtw_skb_free(struct sk_buff *skb) +{ + dev_kfree_skb_any(skb); +} + +inline struct sk_buff *_rtw_skb_copy(const struct sk_buff *skb) +{ +#ifdef PLATFORM_LINUX + return skb_copy(skb, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL); +#endif /* PLATFORM_LINUX */ + +#ifdef PLATFORM_FREEBSD + return NULL; +#endif /* PLATFORM_FREEBSD */ +} + +inline struct sk_buff *_rtw_skb_clone(struct sk_buff *skb) +{ +#ifdef PLATFORM_LINUX + return skb_clone(skb, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL); +#endif /* PLATFORM_LINUX */ + +#ifdef PLATFORM_FREEBSD + return skb_clone(skb); +#endif /* PLATFORM_FREEBSD */ +} +inline struct sk_buff *_rtw_pskb_copy(struct sk_buff *skb) +{ +#ifdef PLATFORM_LINUX +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)) + return pskb_copy(skb, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL); +#else + return skb_clone(skb, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL); +#endif +#endif /* PLATFORM_LINUX */ + +#ifdef PLATFORM_FREEBSD + return NULL; +#endif /* PLATFORM_FREEBSD */ +} + +inline int _rtw_netif_rx(_nic_hdl ndev, struct sk_buff *skb) +{ +#if defined(PLATFORM_LINUX) + skb->dev = ndev; + return netif_rx(skb); +#elif defined(PLATFORM_FREEBSD) + return (*ndev->if_input)(ndev, skb); +#else + rtw_warn_on(1); + return -1; +#endif +} + +#ifdef CONFIG_RTW_NAPI +inline int _rtw_netif_receive_skb(_nic_hdl ndev, struct sk_buff *skb) +{ +#if defined(PLATFORM_LINUX) + skb->dev = ndev; + return netif_receive_skb(skb); +#else + rtw_warn_on(1); + return -1; +#endif +} + +#ifdef CONFIG_RTW_GRO +inline gro_result_t _rtw_napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb) +{ +#if defined(PLATFORM_LINUX) + return napi_gro_receive(napi, skb); +#else + rtw_warn_on(1); + return -1; +#endif +} +#endif /* CONFIG_RTW_GRO */ +#endif /* CONFIG_RTW_NAPI */ + +void _rtw_skb_queue_purge(struct sk_buff_head *list) +{ + struct sk_buff *skb; + + while ((skb = skb_dequeue(list)) != NULL) + _rtw_skb_free(skb); +} + +#ifdef CONFIG_USB_HCI +inline void *_rtw_usb_buffer_alloc(struct usb_device *dev, size_t size, dma_addr_t *dma) +{ +#ifdef PLATFORM_LINUX +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + return usb_alloc_coherent(dev, size, (in_interrupt() ? GFP_ATOMIC : GFP_KERNEL), dma); +#else + return usb_buffer_alloc(dev, size, (in_interrupt() ? GFP_ATOMIC : GFP_KERNEL), dma); +#endif +#endif /* PLATFORM_LINUX */ + +#ifdef PLATFORM_FREEBSD + return malloc(size, M_USBDEV, M_NOWAIT | M_ZERO); +#endif /* PLATFORM_FREEBSD */ +} +inline void _rtw_usb_buffer_free(struct usb_device *dev, size_t size, void *addr, dma_addr_t dma) +{ +#ifdef PLATFORM_LINUX +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + usb_free_coherent(dev, size, addr, dma); +#else + usb_buffer_free(dev, size, addr, dma); +#endif +#endif /* PLATFORM_LINUX */ + +#ifdef PLATFORM_FREEBSD + free(addr, M_USBDEV); +#endif /* PLATFORM_FREEBSD */ +} +#endif /* CONFIG_USB_HCI */ + +#if defined(DBG_MEM_ALLOC) + +struct rtw_mem_stat { + ATOMIC_T alloc; /* the memory bytes we allocate currently */ + ATOMIC_T peak; /* the peak memory bytes we allocate */ + ATOMIC_T alloc_cnt; /* the alloc count for alloc currently */ + ATOMIC_T alloc_err_cnt; /* the error times we fail to allocate memory */ +}; + +struct rtw_mem_stat rtw_mem_type_stat[mstat_tf_idx(MSTAT_TYPE_MAX)]; +#ifdef RTW_MEM_FUNC_STAT +struct rtw_mem_stat rtw_mem_func_stat[mstat_ff_idx(MSTAT_FUNC_MAX)]; +#endif + +char *MSTAT_TYPE_str[] = { + "VIR", + "PHY", + "SKB", + "USB", +}; + +#ifdef RTW_MEM_FUNC_STAT +char *MSTAT_FUNC_str[] = { + "UNSP", + "IO", + "TXIO", + "RXIO", + "TX", + "RX", +}; +#endif + +void rtw_mstat_dump(void *sel) +{ + int i; + int value_t[4][mstat_tf_idx(MSTAT_TYPE_MAX)]; +#ifdef RTW_MEM_FUNC_STAT + int value_f[4][mstat_ff_idx(MSTAT_FUNC_MAX)]; +#endif + + for (i = 0; i < mstat_tf_idx(MSTAT_TYPE_MAX); i++) { + value_t[0][i] = ATOMIC_READ(&(rtw_mem_type_stat[i].alloc)); + value_t[1][i] = ATOMIC_READ(&(rtw_mem_type_stat[i].peak)); + value_t[2][i] = ATOMIC_READ(&(rtw_mem_type_stat[i].alloc_cnt)); + value_t[3][i] = ATOMIC_READ(&(rtw_mem_type_stat[i].alloc_err_cnt)); + } + +#ifdef RTW_MEM_FUNC_STAT + for (i = 0; i < mstat_ff_idx(MSTAT_FUNC_MAX); i++) { + value_f[0][i] = ATOMIC_READ(&(rtw_mem_func_stat[i].alloc)); + value_f[1][i] = ATOMIC_READ(&(rtw_mem_func_stat[i].peak)); + value_f[2][i] = ATOMIC_READ(&(rtw_mem_func_stat[i].alloc_cnt)); + value_f[3][i] = ATOMIC_READ(&(rtw_mem_func_stat[i].alloc_err_cnt)); + } +#endif + + RTW_PRINT_SEL(sel, "===================== MSTAT =====================\n"); + RTW_PRINT_SEL(sel, "%4s %10s %10s %10s %10s\n", "TAG", "alloc", "peak", "aloc_cnt", "err_cnt"); + RTW_PRINT_SEL(sel, "-------------------------------------------------\n"); + for (i = 0; i < mstat_tf_idx(MSTAT_TYPE_MAX); i++) + RTW_PRINT_SEL(sel, "%4s %10d %10d %10d %10d\n", MSTAT_TYPE_str[i], value_t[0][i], value_t[1][i], value_t[2][i], value_t[3][i]); +#ifdef RTW_MEM_FUNC_STAT + RTW_PRINT_SEL(sel, "-------------------------------------------------\n"); + for (i = 0; i < mstat_ff_idx(MSTAT_FUNC_MAX); i++) + RTW_PRINT_SEL(sel, "%4s %10d %10d %10d %10d\n", MSTAT_FUNC_str[i], value_f[0][i], value_f[1][i], value_f[2][i], value_f[3][i]); +#endif +} + +void rtw_mstat_update(const enum mstat_f flags, const MSTAT_STATUS status, u32 sz) +{ + static systime update_time = 0; + int peak, alloc; + int i; + + /* initialization */ + if (!update_time) { + for (i = 0; i < mstat_tf_idx(MSTAT_TYPE_MAX); i++) { + ATOMIC_SET(&(rtw_mem_type_stat[i].alloc), 0); + ATOMIC_SET(&(rtw_mem_type_stat[i].peak), 0); + ATOMIC_SET(&(rtw_mem_type_stat[i].alloc_cnt), 0); + ATOMIC_SET(&(rtw_mem_type_stat[i].alloc_err_cnt), 0); + } + #ifdef RTW_MEM_FUNC_STAT + for (i = 0; i < mstat_ff_idx(MSTAT_FUNC_MAX); i++) { + ATOMIC_SET(&(rtw_mem_func_stat[i].alloc), 0); + ATOMIC_SET(&(rtw_mem_func_stat[i].peak), 0); + ATOMIC_SET(&(rtw_mem_func_stat[i].alloc_cnt), 0); + ATOMIC_SET(&(rtw_mem_func_stat[i].alloc_err_cnt), 0); + } + #endif + } + + switch (status) { + case MSTAT_ALLOC_SUCCESS: + ATOMIC_INC(&(rtw_mem_type_stat[mstat_tf_idx(flags)].alloc_cnt)); + alloc = ATOMIC_ADD_RETURN(&(rtw_mem_type_stat[mstat_tf_idx(flags)].alloc), sz); + peak = ATOMIC_READ(&(rtw_mem_type_stat[mstat_tf_idx(flags)].peak)); + if (peak < alloc) + ATOMIC_SET(&(rtw_mem_type_stat[mstat_tf_idx(flags)].peak), alloc); + + #ifdef RTW_MEM_FUNC_STAT + ATOMIC_INC(&(rtw_mem_func_stat[mstat_ff_idx(flags)].alloc_cnt)); + alloc = ATOMIC_ADD_RETURN(&(rtw_mem_func_stat[mstat_ff_idx(flags)].alloc), sz); + peak = ATOMIC_READ(&(rtw_mem_func_stat[mstat_ff_idx(flags)].peak)); + if (peak < alloc) + ATOMIC_SET(&(rtw_mem_func_stat[mstat_ff_idx(flags)].peak), alloc); + #endif + break; + + case MSTAT_ALLOC_FAIL: + ATOMIC_INC(&(rtw_mem_type_stat[mstat_tf_idx(flags)].alloc_err_cnt)); + #ifdef RTW_MEM_FUNC_STAT + ATOMIC_INC(&(rtw_mem_func_stat[mstat_ff_idx(flags)].alloc_err_cnt)); + #endif + break; + + case MSTAT_FREE: + ATOMIC_DEC(&(rtw_mem_type_stat[mstat_tf_idx(flags)].alloc_cnt)); + ATOMIC_SUB(&(rtw_mem_type_stat[mstat_tf_idx(flags)].alloc), sz); + #ifdef RTW_MEM_FUNC_STAT + ATOMIC_DEC(&(rtw_mem_func_stat[mstat_ff_idx(flags)].alloc_cnt)); + ATOMIC_SUB(&(rtw_mem_func_stat[mstat_ff_idx(flags)].alloc), sz); + #endif + break; + }; + + /* if (rtw_get_passing_time_ms(update_time) > 5000) { */ + /* rtw_mstat_dump(RTW_DBGDUMP); */ + update_time = rtw_get_current_time(); + /* } */ +} + +#ifndef SIZE_MAX + #define SIZE_MAX (~(size_t)0) +#endif + +struct mstat_sniff_rule { + enum mstat_f flags; + size_t lb; + size_t hb; +}; + +struct mstat_sniff_rule mstat_sniff_rules[] = { + {MSTAT_TYPE_PHY, 4097, SIZE_MAX}, +}; + +int mstat_sniff_rule_num = sizeof(mstat_sniff_rules) / sizeof(struct mstat_sniff_rule); + +bool match_mstat_sniff_rules(const enum mstat_f flags, const size_t size) +{ + int i; + for (i = 0; i < mstat_sniff_rule_num; i++) { + if (mstat_sniff_rules[i].flags == flags + && mstat_sniff_rules[i].lb <= size + && mstat_sniff_rules[i].hb >= size) + return _TRUE; + } + + return _FALSE; +} + +inline void *dbg_rtw_vmalloc(u32 sz, const enum mstat_f flags, const char *func, const int line) +{ + void *p; + + if (match_mstat_sniff_rules(flags, sz)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz)); + + p = _rtw_vmalloc((sz)); + + rtw_mstat_update( + flags + , p ? MSTAT_ALLOC_SUCCESS : MSTAT_ALLOC_FAIL + , sz + ); + + return p; +} + +inline void *dbg_rtw_zvmalloc(u32 sz, const enum mstat_f flags, const char *func, const int line) +{ + void *p; + + if (match_mstat_sniff_rules(flags, sz)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz)); + + p = _rtw_zvmalloc((sz)); + + rtw_mstat_update( + flags + , p ? MSTAT_ALLOC_SUCCESS : MSTAT_ALLOC_FAIL + , sz + ); + + return p; +} + +inline void dbg_rtw_vmfree(void *pbuf, u32 sz, const enum mstat_f flags, const char *func, const int line) +{ + + if (match_mstat_sniff_rules(flags, sz)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz)); + + _rtw_vmfree((pbuf), (sz)); + + rtw_mstat_update( + flags + , MSTAT_FREE + , sz + ); +} + +inline void *dbg_rtw_malloc(u32 sz, const enum mstat_f flags, const char *func, const int line) +{ + void *p; + + if (match_mstat_sniff_rules(flags, sz)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz)); + + p = _rtw_malloc((sz)); + + rtw_mstat_update( + flags + , p ? MSTAT_ALLOC_SUCCESS : MSTAT_ALLOC_FAIL + , sz + ); + + return p; +} + +inline void *dbg_rtw_zmalloc(u32 sz, const enum mstat_f flags, const char *func, const int line) +{ + void *p; + + if (match_mstat_sniff_rules(flags, sz)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz)); + + p = _rtw_zmalloc((sz)); + + rtw_mstat_update( + flags + , p ? MSTAT_ALLOC_SUCCESS : MSTAT_ALLOC_FAIL + , sz + ); + + return p; +} + +inline void dbg_rtw_mfree(void *pbuf, u32 sz, const enum mstat_f flags, const char *func, const int line) +{ + if (match_mstat_sniff_rules(flags, sz)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz)); + + _rtw_mfree((pbuf), (sz)); + + rtw_mstat_update( + flags + , MSTAT_FREE + , sz + ); +} + +inline struct sk_buff *dbg_rtw_skb_alloc(unsigned int size, const enum mstat_f flags, const char *func, int line) +{ + struct sk_buff *skb; + unsigned int truesize = 0; + + skb = _rtw_skb_alloc(size); + + if (skb) + truesize = skb->truesize; + + if (!skb || truesize < size || match_mstat_sniff_rules(flags, truesize)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%d), skb:%p, truesize=%u\n", func, line, __FUNCTION__, size, skb, truesize); + + rtw_mstat_update( + flags + , skb ? MSTAT_ALLOC_SUCCESS : MSTAT_ALLOC_FAIL + , truesize + ); + + return skb; +} + +inline void dbg_rtw_skb_free(struct sk_buff *skb, const enum mstat_f flags, const char *func, int line) +{ + unsigned int truesize = skb->truesize; + + if (match_mstat_sniff_rules(flags, truesize)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s, truesize=%u\n", func, line, __FUNCTION__, truesize); + + _rtw_skb_free(skb); + + rtw_mstat_update( + flags + , MSTAT_FREE + , truesize + ); +} + +inline struct sk_buff *dbg_rtw_skb_copy(const struct sk_buff *skb, const enum mstat_f flags, const char *func, const int line) +{ + struct sk_buff *skb_cp; + unsigned int truesize = skb->truesize; + unsigned int cp_truesize = 0; + + skb_cp = _rtw_skb_copy(skb); + if (skb_cp) + cp_truesize = skb_cp->truesize; + + if (!skb_cp || cp_truesize < truesize || match_mstat_sniff_rules(flags, cp_truesize)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%u), skb_cp:%p, cp_truesize=%u\n", func, line, __FUNCTION__, truesize, skb_cp, cp_truesize); + + rtw_mstat_update( + flags + , skb_cp ? MSTAT_ALLOC_SUCCESS : MSTAT_ALLOC_FAIL + , cp_truesize + ); + + return skb_cp; +} + +inline struct sk_buff *dbg_rtw_skb_clone(struct sk_buff *skb, const enum mstat_f flags, const char *func, const int line) +{ + struct sk_buff *skb_cl; + unsigned int truesize = skb->truesize; + unsigned int cl_truesize = 0; + + skb_cl = _rtw_skb_clone(skb); + if (skb_cl) + cl_truesize = skb_cl->truesize; + + if (!skb_cl || cl_truesize < truesize || match_mstat_sniff_rules(flags, cl_truesize)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%u), skb_cl:%p, cl_truesize=%u\n", func, line, __FUNCTION__, truesize, skb_cl, cl_truesize); + + rtw_mstat_update( + flags + , skb_cl ? MSTAT_ALLOC_SUCCESS : MSTAT_ALLOC_FAIL + , cl_truesize + ); + + return skb_cl; +} + +inline int dbg_rtw_netif_rx(_nic_hdl ndev, struct sk_buff *skb, const enum mstat_f flags, const char *func, int line) +{ + int ret; + unsigned int truesize = skb->truesize; + + if (match_mstat_sniff_rules(flags, truesize)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s, truesize=%u\n", func, line, __FUNCTION__, truesize); + + ret = _rtw_netif_rx(ndev, skb); + + rtw_mstat_update( + flags + , MSTAT_FREE + , truesize + ); + + return ret; +} + +#ifdef CONFIG_RTW_NAPI +inline int dbg_rtw_netif_receive_skb(_nic_hdl ndev, struct sk_buff *skb, const enum mstat_f flags, const char *func, int line) +{ + int ret; + unsigned int truesize = skb->truesize; + + if (match_mstat_sniff_rules(flags, truesize)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s, truesize=%u\n", func, line, __FUNCTION__, truesize); + + ret = _rtw_netif_receive_skb(ndev, skb); + + rtw_mstat_update( + flags + , MSTAT_FREE + , truesize + ); + + return ret; +} + +#ifdef CONFIG_RTW_GRO +inline gro_result_t dbg_rtw_napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb, const enum mstat_f flags, const char *func, int line) +{ + int ret; + unsigned int truesize = skb->truesize; + + if (match_mstat_sniff_rules(flags, truesize)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s, truesize=%u\n", func, line, __FUNCTION__, truesize); + + ret = _rtw_napi_gro_receive(napi, skb); + + rtw_mstat_update( + flags + , MSTAT_FREE + , truesize + ); + + return ret; +} +#endif /* CONFIG_RTW_GRO */ +#endif /* CONFIG_RTW_NAPI */ + +inline void dbg_rtw_skb_queue_purge(struct sk_buff_head *list, enum mstat_f flags, const char *func, int line) +{ + struct sk_buff *skb; + + while ((skb = skb_dequeue(list)) != NULL) + dbg_rtw_skb_free(skb, flags, func, line); +} + +#ifdef CONFIG_USB_HCI +inline void *dbg_rtw_usb_buffer_alloc(struct usb_device *dev, size_t size, dma_addr_t *dma, const enum mstat_f flags, const char *func, int line) +{ + void *p; + + if (match_mstat_sniff_rules(flags, size)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%zu)\n", func, line, __FUNCTION__, size); + + p = _rtw_usb_buffer_alloc(dev, size, dma); + + rtw_mstat_update( + flags + , p ? MSTAT_ALLOC_SUCCESS : MSTAT_ALLOC_FAIL + , size + ); + + return p; +} + +inline void dbg_rtw_usb_buffer_free(struct usb_device *dev, size_t size, void *addr, dma_addr_t dma, const enum mstat_f flags, const char *func, int line) +{ + + if (match_mstat_sniff_rules(flags, size)) + RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%zu)\n", func, line, __FUNCTION__, size); + + _rtw_usb_buffer_free(dev, size, addr, dma); + + rtw_mstat_update( + flags + , MSTAT_FREE + , size + ); +} +#endif /* CONFIG_USB_HCI */ + +#endif /* defined(DBG_MEM_ALLOC) */ + +void *rtw_malloc2d(int h, int w, size_t size) +{ + int j; + + void **a = (void **) rtw_zmalloc(h * sizeof(void *) + h * w * size); + if (a == NULL) { + RTW_INFO("%s: alloc memory fail!\n", __FUNCTION__); + return NULL; + } + + for (j = 0; j < h; j++) + a[j] = ((char *)(a + h)) + j * w * size; + + return a; +} + +void rtw_mfree2d(void *pbuf, int h, int w, int size) +{ + rtw_mfree((u8 *)pbuf, h * sizeof(void *) + w * h * size); +} + +inline void rtw_os_pkt_free(_pkt *pkt) +{ +#if defined(PLATFORM_LINUX) + rtw_skb_free(pkt); +#elif defined(PLATFORM_FREEBSD) + m_freem(pkt); +#else + #error "TBD\n" +#endif +} + +inline _pkt *rtw_os_pkt_copy(_pkt *pkt) +{ +#if defined(PLATFORM_LINUX) + return rtw_skb_copy(pkt); +#elif defined(PLATFORM_FREEBSD) + return m_dup(pkt, M_NOWAIT); +#else + #error "TBD\n" +#endif +} + +inline void *rtw_os_pkt_data(_pkt *pkt) +{ +#if defined(PLATFORM_LINUX) + return pkt->data; +#elif defined(PLATFORM_FREEBSD) + return pkt->m_data; +#else + #error "TBD\n" +#endif +} + +inline u32 rtw_os_pkt_len(_pkt *pkt) +{ +#if defined(PLATFORM_LINUX) + return pkt->len; +#elif defined(PLATFORM_FREEBSD) + return pkt->m_pkthdr.len; +#else + #error "TBD\n" +#endif +} + +void _rtw_memcpy(void *dst, const void *src, u32 sz) +{ + +#if defined(PLATFORM_LINUX) || defined (PLATFORM_FREEBSD) + + memcpy(dst, src, sz); + +#endif + +#ifdef PLATFORM_WINDOWS + + NdisMoveMemory(dst, src, sz); + +#endif + +} + +inline void _rtw_memmove(void *dst, const void *src, u32 sz) +{ +#if defined(PLATFORM_LINUX) + memmove(dst, src, sz); +#else + #error "TBD\n" +#endif +} + +int _rtw_memcmp(const void *dst, const void *src, u32 sz) +{ + +#if defined(PLATFORM_LINUX) || defined (PLATFORM_FREEBSD) + /* under Linux/GNU/GLibc, the return value of memcmp for two same mem. chunk is 0 */ + + if (!(memcmp(dst, src, sz))) + return _TRUE; + else + return _FALSE; +#endif + + +#ifdef PLATFORM_WINDOWS + /* under Windows, the return value of NdisEqualMemory for two same mem. chunk is 1 */ + + if (NdisEqualMemory(dst, src, sz)) + return _TRUE; + else + return _FALSE; + +#endif + + + +} + +int _rtw_memcmp2(const void *dst, const void *src, u32 sz) +{ + const unsigned char *p1 = dst, *p2 = src; + + if (sz == 0) + return 0; + + while (*p1 == *p2) { + p1++; + p2++; + sz--; + if (sz == 0) + return 0; + } + + return *p1 - *p2; +} + +void _rtw_memset(void *pbuf, int c, u32 sz) +{ + +#if defined(PLATFORM_LINUX) || defined (PLATFORM_FREEBSD) + + memset(pbuf, c, sz); + +#endif + +#ifdef PLATFORM_WINDOWS +#if 0 + NdisZeroMemory(pbuf, sz); + if (c != 0) + memset(pbuf, c, sz); +#else + NdisFillMemory(pbuf, sz, c); +#endif +#endif + +} + +#ifdef PLATFORM_FREEBSD +static inline void __list_add(_list *pnew, _list *pprev, _list *pnext) +{ + pnext->prev = pnew; + pnew->next = pnext; + pnew->prev = pprev; + pprev->next = pnew; +} +#endif /* PLATFORM_FREEBSD */ + + +void _rtw_init_listhead(_list *list) +{ + +#ifdef PLATFORM_LINUX + + INIT_LIST_HEAD(list); + +#endif + +#ifdef PLATFORM_FREEBSD + list->next = list; + list->prev = list; +#endif +#ifdef PLATFORM_WINDOWS + + NdisInitializeListHead(list); + +#endif + +} + + +/* +For the following list_xxx operations, +caller must guarantee the atomic context. +Otherwise, there will be racing condition. +*/ +u32 rtw_is_list_empty(_list *phead) +{ + +#ifdef PLATFORM_LINUX + + if (list_empty(phead)) + return _TRUE; + else + return _FALSE; + +#endif +#ifdef PLATFORM_FREEBSD + + if (phead->next == phead) + return _TRUE; + else + return _FALSE; + +#endif + + +#ifdef PLATFORM_WINDOWS + + if (IsListEmpty(phead)) + return _TRUE; + else + return _FALSE; + +#endif + + +} + +void rtw_list_insert_head(_list *plist, _list *phead) +{ + +#ifdef PLATFORM_LINUX + list_add(plist, phead); +#endif + +#ifdef PLATFORM_FREEBSD + __list_add(plist, phead, phead->next); +#endif + +#ifdef PLATFORM_WINDOWS + InsertHeadList(phead, plist); +#endif +} + +void rtw_list_insert_tail(_list *plist, _list *phead) +{ + +#ifdef PLATFORM_LINUX + + list_add_tail(plist, phead); + +#endif +#ifdef PLATFORM_FREEBSD + + __list_add(plist, phead->prev, phead); + +#endif +#ifdef PLATFORM_WINDOWS + + InsertTailList(phead, plist); + +#endif + +} + +inline void rtw_list_splice(_list *list, _list *head) +{ +#ifdef PLATFORM_LINUX + list_splice(list, head); +#else + #error "TBD\n" +#endif +} + +inline void rtw_list_splice_init(_list *list, _list *head) +{ +#ifdef PLATFORM_LINUX + list_splice_init(list, head); +#else + #error "TBD\n" +#endif +} + +inline void rtw_list_splice_tail(_list *list, _list *head) +{ +#ifdef PLATFORM_LINUX + #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)) + if (!list_empty(list)) + __list_splice(list, head); + #else + list_splice_tail(list, head); + #endif +#else + #error "TBD\n" +#endif +} + +inline void rtw_hlist_head_init(rtw_hlist_head *h) +{ +#ifdef PLATFORM_LINUX + INIT_HLIST_HEAD(h); +#else + #error "TBD\n" +#endif +} + +inline void rtw_hlist_add_head(rtw_hlist_node *n, rtw_hlist_head *h) +{ +#ifdef PLATFORM_LINUX + hlist_add_head(n, h); +#else + #error "TBD\n" +#endif +} + +inline void rtw_hlist_del(rtw_hlist_node *n) +{ +#ifdef PLATFORM_LINUX + hlist_del(n); +#else + #error "TBD\n" +#endif +} + +inline void rtw_hlist_add_head_rcu(rtw_hlist_node *n, rtw_hlist_head *h) +{ +#ifdef PLATFORM_LINUX + hlist_add_head_rcu(n, h); +#else + #error "TBD\n" +#endif +} + +inline void rtw_hlist_del_rcu(rtw_hlist_node *n) +{ +#ifdef PLATFORM_LINUX + hlist_del_rcu(n); +#else + #error "TBD\n" +#endif +} + +void rtw_init_timer(_timer *ptimer, void *padapter, void *pfunc, void *ctx) +{ + _adapter *adapter = (_adapter *)padapter; + +#ifdef PLATFORM_LINUX + _init_timer(ptimer, adapter->pnetdev, pfunc, ctx); +#endif +#ifdef PLATFORM_FREEBSD + _init_timer(ptimer, adapter->pifp, pfunc, ctx); +#endif +#ifdef PLATFORM_WINDOWS + _init_timer(ptimer, adapter->hndis_adapter, pfunc, ctx); +#endif +} + +/* + +Caller must check if the list is empty before calling rtw_list_delete + +*/ + + +void _rtw_init_sema(_sema *sema, int init_val) +{ + +#ifdef PLATFORM_LINUX + + sema_init(sema, init_val); + +#endif +#ifdef PLATFORM_FREEBSD + sema_init(sema, init_val, "rtw_drv"); +#endif +#ifdef PLATFORM_OS_XP + + KeInitializeSemaphore(sema, init_val, SEMA_UPBND); /* count=0; */ + +#endif + +#ifdef PLATFORM_OS_CE + if (*sema == NULL) + *sema = CreateSemaphore(NULL, init_val, SEMA_UPBND, NULL); +#endif + +} + +void _rtw_free_sema(_sema *sema) +{ +#ifdef PLATFORM_FREEBSD + sema_destroy(sema); +#endif +#ifdef PLATFORM_OS_CE + CloseHandle(*sema); +#endif + +} + +void _rtw_up_sema(_sema *sema) +{ + +#ifdef PLATFORM_LINUX + + up(sema); + +#endif +#ifdef PLATFORM_FREEBSD + sema_post(sema); +#endif +#ifdef PLATFORM_OS_XP + + KeReleaseSemaphore(sema, IO_NETWORK_INCREMENT, 1, FALSE); + +#endif + +#ifdef PLATFORM_OS_CE + ReleaseSemaphore(*sema, 1, NULL); +#endif +} + +u32 _rtw_down_sema(_sema *sema) +{ + +#ifdef PLATFORM_LINUX + + if (down_interruptible(sema)) + return _FAIL; + else + return _SUCCESS; + +#endif +#ifdef PLATFORM_FREEBSD + sema_wait(sema); + return _SUCCESS; +#endif +#ifdef PLATFORM_OS_XP + + if (STATUS_SUCCESS == KeWaitForSingleObject(sema, Executive, KernelMode, TRUE, NULL)) + return _SUCCESS; + else + return _FAIL; +#endif + +#ifdef PLATFORM_OS_CE + if (WAIT_OBJECT_0 == WaitForSingleObject(*sema, INFINITE)) + return _SUCCESS; + else + return _FAIL; +#endif +} + +inline void thread_exit(_completion *comp) +{ +#ifdef PLATFORM_LINUX + complete_and_exit(comp, 0); +#endif + +#ifdef PLATFORM_FREEBSD + printf("%s", "RTKTHREAD_exit"); +#endif + +#ifdef PLATFORM_OS_CE + ExitThread(STATUS_SUCCESS); +#endif + +#ifdef PLATFORM_OS_XP + PsTerminateSystemThread(STATUS_SUCCESS); +#endif +} + +inline void _rtw_init_completion(_completion *comp) +{ +#ifdef PLATFORM_LINUX + init_completion(comp); +#endif +} +inline void _rtw_wait_for_comp_timeout(_completion *comp) +{ +#ifdef PLATFORM_LINUX + wait_for_completion_timeout(comp, msecs_to_jiffies(3000)); +#endif +} +inline void _rtw_wait_for_comp(_completion *comp) +{ +#ifdef PLATFORM_LINUX + wait_for_completion(comp); +#endif +} + +void _rtw_mutex_init(_mutex *pmutex) +{ +#ifdef PLATFORM_LINUX + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) + mutex_init(pmutex); +#else + init_MUTEX(pmutex); +#endif + +#endif +#ifdef PLATFORM_FREEBSD + mtx_init(pmutex, "", NULL, MTX_DEF | MTX_RECURSE); +#endif +#ifdef PLATFORM_OS_XP + + KeInitializeMutex(pmutex, 0); + +#endif + +#ifdef PLATFORM_OS_CE + *pmutex = CreateMutex(NULL, _FALSE, NULL); +#endif +} + +void _rtw_mutex_free(_mutex *pmutex); +void _rtw_mutex_free(_mutex *pmutex) +{ +#ifdef PLATFORM_LINUX + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) + mutex_destroy(pmutex); +#else +#endif + +#ifdef PLATFORM_FREEBSD + sema_destroy(pmutex); +#endif + +#endif + +#ifdef PLATFORM_OS_XP + +#endif + +#ifdef PLATFORM_OS_CE + +#endif +} + +void _rtw_spinlock_init(_lock *plock) +{ + +#ifdef PLATFORM_LINUX + + spin_lock_init(plock); + +#endif +#ifdef PLATFORM_FREEBSD + mtx_init(plock, "", NULL, MTX_DEF | MTX_RECURSE); +#endif +#ifdef PLATFORM_WINDOWS + + NdisAllocateSpinLock(plock); + +#endif + +} + +void _rtw_spinlock_free(_lock *plock) +{ +#ifdef PLATFORM_FREEBSD + mtx_destroy(plock); +#endif + +#ifdef PLATFORM_WINDOWS + + NdisFreeSpinLock(plock); + +#endif + +} +#ifdef PLATFORM_FREEBSD +extern PADAPTER prtw_lock; + +void rtw_mtx_lock(_lock *plock) +{ + if (prtw_lock) + mtx_lock(&prtw_lock->glock); + else + printf("%s prtw_lock==NULL", __FUNCTION__); +} +void rtw_mtx_unlock(_lock *plock) +{ + if (prtw_lock) + mtx_unlock(&prtw_lock->glock); + else + printf("%s prtw_lock==NULL", __FUNCTION__); + +} +#endif /* PLATFORM_FREEBSD */ + + +void _rtw_spinlock(_lock *plock) +{ + +#ifdef PLATFORM_LINUX + + spin_lock(plock); + +#endif +#ifdef PLATFORM_FREEBSD + mtx_lock(plock); +#endif +#ifdef PLATFORM_WINDOWS + + NdisAcquireSpinLock(plock); + +#endif + +} + +void _rtw_spinunlock(_lock *plock) +{ + +#ifdef PLATFORM_LINUX + + spin_unlock(plock); + +#endif +#ifdef PLATFORM_FREEBSD + mtx_unlock(plock); +#endif +#ifdef PLATFORM_WINDOWS + + NdisReleaseSpinLock(plock); + +#endif +} + + +void _rtw_spinlock_ex(_lock *plock) +{ + +#ifdef PLATFORM_LINUX + + spin_lock(plock); + +#endif +#ifdef PLATFORM_FREEBSD + mtx_lock(plock); +#endif +#ifdef PLATFORM_WINDOWS + + NdisDprAcquireSpinLock(plock); + +#endif + +} + +void _rtw_spinunlock_ex(_lock *plock) +{ + +#ifdef PLATFORM_LINUX + + spin_unlock(plock); + +#endif +#ifdef PLATFORM_FREEBSD + mtx_unlock(plock); +#endif +#ifdef PLATFORM_WINDOWS + + NdisDprReleaseSpinLock(plock); + +#endif +} + + + +void _rtw_init_queue(_queue *pqueue) +{ + _rtw_init_listhead(&(pqueue->queue)); + _rtw_spinlock_init(&(pqueue->lock)); +} + +void _rtw_deinit_queue(_queue *pqueue) +{ + _rtw_spinlock_free(&(pqueue->lock)); +} + +u32 _rtw_queue_empty(_queue *pqueue) +{ + return rtw_is_list_empty(&(pqueue->queue)); +} + + +u32 rtw_end_of_queue_search(_list *head, _list *plist) +{ + if (head == plist) + return _TRUE; + else + return _FALSE; +} + + +systime _rtw_get_current_time(void) +{ + +#ifdef PLATFORM_LINUX + return jiffies; +#endif +#ifdef PLATFORM_FREEBSD + struct timeval tvp; + getmicrotime(&tvp); + return tvp.tv_sec; +#endif +#ifdef PLATFORM_WINDOWS + LARGE_INTEGER SystemTime; + NdisGetCurrentSystemTime(&SystemTime); + return SystemTime.LowPart;/* count of 100-nanosecond intervals */ +#endif +} + +inline u32 _rtw_systime_to_ms(systime stime) +{ +#ifdef PLATFORM_LINUX + return jiffies_to_msecs(stime); +#endif +#ifdef PLATFORM_FREEBSD + return stime * 1000; +#endif +#ifdef PLATFORM_WINDOWS + return stime / 10000 ; +#endif +} + +inline systime _rtw_ms_to_systime(u32 ms) +{ +#ifdef PLATFORM_LINUX + return msecs_to_jiffies(ms); +#endif +#ifdef PLATFORM_FREEBSD + return ms / 1000; +#endif +#ifdef PLATFORM_WINDOWS + return ms * 10000 ; +#endif +} + +inline systime _rtw_us_to_systime(u32 us) +{ +#ifdef PLATFORM_LINUX + return usecs_to_jiffies(us); +#else + #error "TBD\n" +#endif +} + +/* the input parameter start use the same unit as returned by rtw_get_current_time */ +inline s32 _rtw_get_passing_time_ms(systime start) +{ + return _rtw_systime_to_ms(_rtw_get_current_time() - start); +} + +inline s32 _rtw_get_remaining_time_ms(systime end) +{ + return _rtw_systime_to_ms(end - _rtw_get_current_time()); +} + +inline s32 _rtw_get_time_interval_ms(systime start, systime end) +{ + return _rtw_systime_to_ms(end - start); +} + +inline bool _rtw_time_after(systime a, systime b) +{ +#ifdef PLATFORM_LINUX + return time_after(a, b); +#else + #error "TBD\n" +#endif +} + +sysptime rtw_sptime_get(void) +{ + /* CLOCK_MONOTONIC */ +#ifdef PLATFORM_LINUX + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)) + struct timespec64 cur; + + ktime_get_ts64(&cur); + return timespec64_to_ktime(cur); + #else + struct timespec cur; + + ktime_get_ts(&cur); + return timespec_to_ktime(cur); + #endif +#else + #error "TBD\n" +#endif +} + +sysptime rtw_sptime_set(s64 secs, const u32 nsecs) +{ +#ifdef PLATFORM_LINUX + return ktime_set(secs, nsecs); +#else + #error "TBD\n" +#endif +} + +sysptime rtw_sptime_zero(void) +{ +#ifdef PLATFORM_LINUX + return ktime_set(0, 0); +#else + #error "TBD\n" +#endif +} + +/* + * cmp1 < cmp2: return <0 + * cmp1 == cmp2: return 0 + * cmp1 > cmp2: return >0 + */ +int rtw_sptime_cmp(const sysptime cmp1, const sysptime cmp2) +{ +#ifdef PLATFORM_LINUX + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + return ktime_compare(cmp1, cmp2); + #else + if (cmp1.tv64 < cmp2.tv64) + return -1; + if (cmp1.tv64 > cmp2.tv64) + return 1; + return 0; + #endif +#else + #error "TBD\n" +#endif +} + +bool rtw_sptime_eql(const sysptime cmp1, const sysptime cmp2) +{ +#ifdef PLATFORM_LINUX + return rtw_sptime_cmp(cmp1, cmp2) == 0; +#else + #error "TBD\n" +#endif +} + +bool rtw_sptime_is_zero(const sysptime sptime) +{ +#ifdef PLATFORM_LINUX + return rtw_sptime_cmp(sptime, rtw_sptime_zero()) == 0; +#else + #error "TBD\n" +#endif +} + +/* + * sub = lhs - rhs, in normalized form + */ +sysptime rtw_sptime_sub(const sysptime lhs, const sysptime rhs) +{ +#ifdef PLATFORM_LINUX + return ktime_sub(lhs, rhs); +#else + #error "TBD\n" +#endif +} + +/* + * add = lhs + rhs, in normalized form + */ +sysptime rtw_sptime_add(const sysptime lhs, const sysptime rhs) +{ +#ifdef PLATFORM_LINUX + return ktime_add(lhs, rhs); +#else + #error "TBD\n" +#endif +} + +s64 rtw_sptime_to_ms(const sysptime sptime) +{ +#ifdef PLATFORM_LINUX + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + return ktime_to_ms(sptime); + #else + struct timeval tv = ktime_to_timeval(sptime); + + return (s64) tv.tv_sec * MSEC_PER_SEC + tv.tv_usec / USEC_PER_MSEC; + #endif +#else + #error "TBD\n" +#endif +} + +sysptime rtw_ms_to_sptime(u64 ms) +{ +#ifdef PLATFORM_LINUX + return ns_to_ktime(ms * NSEC_PER_MSEC); +#else + #error "TBD\n" +#endif +} + +s64 rtw_sptime_to_us(const sysptime sptime) +{ +#ifdef PLATFORM_LINUX + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)) + return ktime_to_us(sptime); + #else + struct timeval tv = ktime_to_timeval(sptime); + + return (s64) tv.tv_sec * USEC_PER_SEC + tv.tv_usec; + #endif +#else + #error "TBD\n" +#endif +} + +sysptime rtw_us_to_sptime(u64 us) +{ +#ifdef PLATFORM_LINUX + return ns_to_ktime(us * NSEC_PER_USEC); +#else + #error "TBD\n" +#endif +} + +s64 rtw_sptime_to_ns(const sysptime sptime) +{ +#ifdef PLATFORM_LINUX + return ktime_to_ns(sptime); +#else + #error "TBD\n" +#endif +} + +sysptime rtw_ns_to_sptime(u64 ns) +{ +#ifdef PLATFORM_LINUX + return ns_to_ktime(ns); +#else + #error "TBD\n" +#endif +} + +s64 rtw_sptime_diff_ms(const sysptime start, const sysptime end) +{ + sysptime diff; + + diff = rtw_sptime_sub(end, start); + + return rtw_sptime_to_ms(diff); +} + +s64 rtw_sptime_pass_ms(const sysptime start) +{ + sysptime cur, diff; + + cur = rtw_sptime_get(); + diff = rtw_sptime_sub(cur, start); + + return rtw_sptime_to_ms(diff); +} + +s64 rtw_sptime_diff_us(const sysptime start, const sysptime end) +{ + sysptime diff; + + diff = rtw_sptime_sub(end, start); + + return rtw_sptime_to_us(diff); +} + +s64 rtw_sptime_pass_us(const sysptime start) +{ + sysptime cur, diff; + + cur = rtw_sptime_get(); + diff = rtw_sptime_sub(cur, start); + + return rtw_sptime_to_us(diff); +} + +s64 rtw_sptime_diff_ns(const sysptime start, const sysptime end) +{ + sysptime diff; + + diff = rtw_sptime_sub(end, start); + + return rtw_sptime_to_ns(diff); +} + +s64 rtw_sptime_pass_ns(const sysptime start) +{ + sysptime cur, diff; + + cur = rtw_sptime_get(); + diff = rtw_sptime_sub(cur, start); + + return rtw_sptime_to_ns(diff); +} + +void rtw_sleep_schedulable(int ms) +{ + +#ifdef PLATFORM_LINUX + + u32 delta; + + delta = (ms * HZ) / 1000; /* (ms) */ + if (delta == 0) { + delta = 1;/* 1 ms */ + } + set_current_state(TASK_INTERRUPTIBLE); + schedule_timeout(delta); + return; + +#endif +#ifdef PLATFORM_FREEBSD + DELAY(ms * 1000); + return ; +#endif + +#ifdef PLATFORM_WINDOWS + + NdisMSleep(ms * 1000); /* (us)*1000=(ms) */ + +#endif + +} + + +void rtw_msleep_os(int ms) +{ + +#ifdef PLATFORM_LINUX +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)) + if (ms < 20) { + unsigned long us = ms * 1000UL; + usleep_range(us, us + 1000UL); + } else +#endif + msleep((unsigned int)ms); + +#endif +#ifdef PLATFORM_FREEBSD + /* Delay for delay microseconds */ + DELAY(ms * 1000); + return ; +#endif +#ifdef PLATFORM_WINDOWS + + NdisMSleep(ms * 1000); /* (us)*1000=(ms) */ + +#endif + + +} +void rtw_usleep_os(int us) +{ +#ifdef PLATFORM_LINUX + + /* msleep((unsigned int)us); */ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)) + usleep_range(us, us + 1); +#else + if (1 < (us / 1000)) + msleep(1); + else + msleep((us / 1000) + 1); +#endif +#endif + +#ifdef PLATFORM_FREEBSD + /* Delay for delay microseconds */ + DELAY(us); + + return ; +#endif +#ifdef PLATFORM_WINDOWS + + NdisMSleep(us); /* (us) */ + +#endif + + +} + + +#ifdef DBG_DELAY_OS +void _rtw_mdelay_os(int ms, const char *func, const int line) +{ +#if 0 + if (ms > 10) + RTW_INFO("%s:%d %s(%d)\n", func, line, __FUNCTION__, ms); + rtw_msleep_os(ms); + return; +#endif + + + RTW_INFO("%s:%d %s(%d)\n", func, line, __FUNCTION__, ms); + +#if defined(PLATFORM_LINUX) + + mdelay((unsigned long)ms); + +#elif defined(PLATFORM_WINDOWS) + + NdisStallExecution(ms * 1000); /* (us)*1000=(ms) */ + +#endif + + +} +void _rtw_udelay_os(int us, const char *func, const int line) +{ + +#if 0 + if (us > 1000) { + RTW_INFO("%s:%d %s(%d)\n", func, line, __FUNCTION__, us); + rtw_usleep_os(us); + return; + } +#endif + + + RTW_INFO("%s:%d %s(%d)\n", func, line, __FUNCTION__, us); + + +#if defined(PLATFORM_LINUX) + + udelay((unsigned long)us); + +#elif defined(PLATFORM_WINDOWS) + + NdisStallExecution(us); /* (us) */ + +#endif + +} +#else +void rtw_mdelay_os(int ms) +{ + +#ifdef PLATFORM_LINUX + + mdelay((unsigned long)ms); + +#endif +#ifdef PLATFORM_FREEBSD + DELAY(ms * 1000); + return ; +#endif +#ifdef PLATFORM_WINDOWS + + NdisStallExecution(ms * 1000); /* (us)*1000=(ms) */ + +#endif + + +} +void rtw_udelay_os(int us) +{ + +#ifdef PLATFORM_LINUX + + udelay((unsigned long)us); + +#endif +#ifdef PLATFORM_FREEBSD + /* Delay for delay microseconds */ + DELAY(us); + return ; +#endif +#ifdef PLATFORM_WINDOWS + + NdisStallExecution(us); /* (us) */ + +#endif + +} +#endif + +void rtw_yield_os(void) +{ +#ifdef PLATFORM_LINUX + yield(); +#endif +#ifdef PLATFORM_FREEBSD + yield(); +#endif +#ifdef PLATFORM_WINDOWS + SwitchToThread(); +#endif +} + +const char *_rtw_pwait_type_str[] = { + [RTW_PWAIT_TYPE_MSLEEP] = "MS", + [RTW_PWAIT_TYPE_USLEEP] = "US", + [RTW_PWAIT_TYPE_YIELD] = "Y", + [RTW_PWAIT_TYPE_MDELAY] = "MD", + [RTW_PWAIT_TYPE_UDELAY] = "UD", + [RTW_PWAIT_TYPE_NUM] = "unknown", +}; + +static void rtw_pwctx_yield(int us) +{ + rtw_yield_os(); +} + +static void (*const rtw_pwait_hdl[])(int)= { + [RTW_PWAIT_TYPE_MSLEEP] = rtw_msleep_os, + [RTW_PWAIT_TYPE_USLEEP] = rtw_usleep_os, + [RTW_PWAIT_TYPE_YIELD] = rtw_pwctx_yield, + [RTW_PWAIT_TYPE_MDELAY] = rtw_mdelay_os, + [RTW_PWAIT_TYPE_UDELAY] = rtw_udelay_os, +}; + +int rtw_pwctx_config(struct rtw_pwait_ctx *pwctx, enum rtw_pwait_type type, s32 time, s32 cnt_lmt) +{ + int ret = _FAIL; + + if (!RTW_PWAIT_TYPE_VALID(type)) + goto exit; + + pwctx->conf.type = type; + pwctx->conf.wait_time = time; + pwctx->conf.wait_cnt_lmt = cnt_lmt; + pwctx->wait_hdl = rtw_pwait_hdl[type]; + + ret = _SUCCESS; + +exit: + return ret; +} + +bool rtw_macaddr_is_larger(const u8 *a, const u8 *b) +{ + u32 va, vb; + + va = be32_to_cpu(*((u32 *)a)); + vb = be32_to_cpu(*((u32 *)b)); + if (va > vb) + return 1; + else if (va < vb) + return 0; + + return be16_to_cpu(*((u16 *)(a + 4))) > be16_to_cpu(*((u16 *)(b + 4))); +} + +#define RTW_SUSPEND_LOCK_NAME "rtw_wifi" +#define RTW_SUSPEND_TRAFFIC_LOCK_NAME "rtw_wifi_traffic" +#define RTW_SUSPEND_RESUME_LOCK_NAME "rtw_wifi_resume" +#ifdef CONFIG_WAKELOCK +static struct wake_lock rtw_suspend_lock; +static struct wake_lock rtw_suspend_traffic_lock; +static struct wake_lock rtw_suspend_resume_lock; +#elif defined(CONFIG_ANDROID_POWER) +static android_suspend_lock_t rtw_suspend_lock = { + .name = RTW_SUSPEND_LOCK_NAME +}; +static android_suspend_lock_t rtw_suspend_traffic_lock = { + .name = RTW_SUSPEND_TRAFFIC_LOCK_NAME +}; +static android_suspend_lock_t rtw_suspend_resume_lock = { + .name = RTW_SUSPEND_RESUME_LOCK_NAME +}; +#endif + +inline void rtw_suspend_lock_init(void) +{ +#ifdef CONFIG_WAKELOCK + wake_lock_init(&rtw_suspend_lock, WAKE_LOCK_SUSPEND, RTW_SUSPEND_LOCK_NAME); + wake_lock_init(&rtw_suspend_traffic_lock, WAKE_LOCK_SUSPEND, RTW_SUSPEND_TRAFFIC_LOCK_NAME); + wake_lock_init(&rtw_suspend_resume_lock, WAKE_LOCK_SUSPEND, RTW_SUSPEND_RESUME_LOCK_NAME); +#elif defined(CONFIG_ANDROID_POWER) + android_init_suspend_lock(&rtw_suspend_lock); + android_init_suspend_lock(&rtw_suspend_traffic_lock); + android_init_suspend_lock(&rtw_suspend_resume_lock); +#endif +} + +inline void rtw_suspend_lock_uninit(void) +{ +#ifdef CONFIG_WAKELOCK + wake_lock_destroy(&rtw_suspend_lock); + wake_lock_destroy(&rtw_suspend_traffic_lock); + wake_lock_destroy(&rtw_suspend_resume_lock); +#elif defined(CONFIG_ANDROID_POWER) + android_uninit_suspend_lock(&rtw_suspend_lock); + android_uninit_suspend_lock(&rtw_suspend_traffic_lock); + android_uninit_suspend_lock(&rtw_suspend_resume_lock); +#endif +} + +inline void rtw_lock_suspend(void) +{ +#ifdef CONFIG_WAKELOCK + wake_lock(&rtw_suspend_lock); +#elif defined(CONFIG_ANDROID_POWER) + android_lock_suspend(&rtw_suspend_lock); +#endif + +#if defined(CONFIG_WAKELOCK) || defined(CONFIG_ANDROID_POWER) + /* RTW_INFO("####%s: suspend_lock_count:%d####\n", __FUNCTION__, rtw_suspend_lock.stat.count); */ +#endif +} + +inline void rtw_unlock_suspend(void) +{ +#ifdef CONFIG_WAKELOCK + wake_unlock(&rtw_suspend_lock); +#elif defined(CONFIG_ANDROID_POWER) + android_unlock_suspend(&rtw_suspend_lock); +#endif + +#if defined(CONFIG_WAKELOCK) || defined(CONFIG_ANDROID_POWER) + /* RTW_INFO("####%s: suspend_lock_count:%d####\n", __FUNCTION__, rtw_suspend_lock.stat.count); */ +#endif +} + +inline void rtw_resume_lock_suspend(void) +{ +#ifdef CONFIG_WAKELOCK + wake_lock(&rtw_suspend_resume_lock); +#elif defined(CONFIG_ANDROID_POWER) + android_lock_suspend(&rtw_suspend_resume_lock); +#endif + +#if defined(CONFIG_WAKELOCK) || defined(CONFIG_ANDROID_POWER) + /* RTW_INFO("####%s: suspend_lock_count:%d####\n", __FUNCTION__, rtw_suspend_lock.stat.count); */ +#endif +} + +inline void rtw_resume_unlock_suspend(void) +{ +#ifdef CONFIG_WAKELOCK + wake_unlock(&rtw_suspend_resume_lock); +#elif defined(CONFIG_ANDROID_POWER) + android_unlock_suspend(&rtw_suspend_resume_lock); +#endif + +#if defined(CONFIG_WAKELOCK) || defined(CONFIG_ANDROID_POWER) + /* RTW_INFO("####%s: suspend_lock_count:%d####\n", __FUNCTION__, rtw_suspend_lock.stat.count); */ +#endif +} + +inline void rtw_lock_suspend_timeout(u32 timeout_ms) +{ +#ifdef CONFIG_WAKELOCK + wake_lock_timeout(&rtw_suspend_lock, rtw_ms_to_systime(timeout_ms)); +#elif defined(CONFIG_ANDROID_POWER) + android_lock_suspend_auto_expire(&rtw_suspend_lock, rtw_ms_to_systime(timeout_ms)); +#endif +} + + +inline void rtw_lock_traffic_suspend_timeout(u32 timeout_ms) +{ +#ifdef CONFIG_WAKELOCK + wake_lock_timeout(&rtw_suspend_traffic_lock, rtw_ms_to_systime(timeout_ms)); +#elif defined(CONFIG_ANDROID_POWER) + android_lock_suspend_auto_expire(&rtw_suspend_traffic_lock, rtw_ms_to_systime(timeout_ms)); +#endif + /* RTW_INFO("traffic lock timeout:%d\n", timeout_ms); */ +} + +inline void rtw_set_bit(int nr, unsigned long *addr) +{ +#ifdef PLATFORM_LINUX + set_bit(nr, addr); +#else + #error "TBD\n"; +#endif +} + +inline void rtw_clear_bit(int nr, unsigned long *addr) +{ +#ifdef PLATFORM_LINUX + clear_bit(nr, addr); +#else + #error "TBD\n"; +#endif +} + +inline int rtw_test_and_clear_bit(int nr, unsigned long *addr) +{ +#ifdef PLATFORM_LINUX + return test_and_clear_bit(nr, addr); +#else + #error "TBD\n"; +#endif +} + +inline void ATOMIC_SET(ATOMIC_T *v, int i) +{ +#ifdef PLATFORM_LINUX + atomic_set(v, i); +#elif defined(PLATFORM_WINDOWS) + *v = i; /* other choice???? */ +#elif defined(PLATFORM_FREEBSD) + atomic_set_int(v, i); +#endif +} + +inline int ATOMIC_READ(ATOMIC_T *v) +{ +#ifdef PLATFORM_LINUX + return atomic_read(v); +#elif defined(PLATFORM_WINDOWS) + return *v; /* other choice???? */ +#elif defined(PLATFORM_FREEBSD) + return atomic_load_acq_32(v); +#endif +} + +inline void ATOMIC_ADD(ATOMIC_T *v, int i) +{ +#ifdef PLATFORM_LINUX + atomic_add(i, v); +#elif defined(PLATFORM_WINDOWS) + InterlockedAdd(v, i); +#elif defined(PLATFORM_FREEBSD) + atomic_add_int(v, i); +#endif +} +inline void ATOMIC_SUB(ATOMIC_T *v, int i) +{ +#ifdef PLATFORM_LINUX + atomic_sub(i, v); +#elif defined(PLATFORM_WINDOWS) + InterlockedAdd(v, -i); +#elif defined(PLATFORM_FREEBSD) + atomic_subtract_int(v, i); +#endif +} + +inline void ATOMIC_INC(ATOMIC_T *v) +{ +#ifdef PLATFORM_LINUX + atomic_inc(v); +#elif defined(PLATFORM_WINDOWS) + InterlockedIncrement(v); +#elif defined(PLATFORM_FREEBSD) + atomic_add_int(v, 1); +#endif +} + +inline void ATOMIC_DEC(ATOMIC_T *v) +{ +#ifdef PLATFORM_LINUX + atomic_dec(v); +#elif defined(PLATFORM_WINDOWS) + InterlockedDecrement(v); +#elif defined(PLATFORM_FREEBSD) + atomic_subtract_int(v, 1); +#endif +} + +inline int ATOMIC_ADD_RETURN(ATOMIC_T *v, int i) +{ +#ifdef PLATFORM_LINUX + return atomic_add_return(i, v); +#elif defined(PLATFORM_WINDOWS) + return InterlockedAdd(v, i); +#elif defined(PLATFORM_FREEBSD) + atomic_add_int(v, i); + return atomic_load_acq_32(v); +#endif +} + +inline int ATOMIC_SUB_RETURN(ATOMIC_T *v, int i) +{ +#ifdef PLATFORM_LINUX + return atomic_sub_return(i, v); +#elif defined(PLATFORM_WINDOWS) + return InterlockedAdd(v, -i); +#elif defined(PLATFORM_FREEBSD) + atomic_subtract_int(v, i); + return atomic_load_acq_32(v); +#endif +} + +inline int ATOMIC_INC_RETURN(ATOMIC_T *v) +{ +#ifdef PLATFORM_LINUX + return atomic_inc_return(v); +#elif defined(PLATFORM_WINDOWS) + return InterlockedIncrement(v); +#elif defined(PLATFORM_FREEBSD) + atomic_add_int(v, 1); + return atomic_load_acq_32(v); +#endif +} + +inline int ATOMIC_DEC_RETURN(ATOMIC_T *v) +{ +#ifdef PLATFORM_LINUX + return atomic_dec_return(v); +#elif defined(PLATFORM_WINDOWS) + return InterlockedDecrement(v); +#elif defined(PLATFORM_FREEBSD) + atomic_subtract_int(v, 1); + return atomic_load_acq_32(v); +#endif +} + +inline bool ATOMIC_INC_UNLESS(ATOMIC_T *v, int u) +{ +#ifdef PLATFORM_LINUX +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 15)) + return atomic_add_unless(v, 1, u); +#else + /* only make sure not exceed after this function */ + if (ATOMIC_INC_RETURN(v) > u) { + ATOMIC_DEC(v); + return 0; + } + return 1; +#endif +#else + #error "TBD\n" +#endif +} + +#ifdef PLATFORM_LINUX +/* +* Open a file with the specific @param path, @param flag, @param mode +* @param fpp the pointer of struct file pointer to get struct file pointer while file opening is success +* @param path the path of the file to open +* @param flag file operation flags, please refer to linux document +* @param mode please refer to linux document +* @return Linux specific error code +*/ +static int openFile(struct file **fpp, const char *path, int flag, int mode) +{ + struct file *fp; + + fp = filp_open(path, flag, mode); + if (IS_ERR(fp)) { + *fpp = NULL; + return PTR_ERR(fp); + } else { + *fpp = fp; + return 0; + } +} + +/* +* Close the file with the specific @param fp +* @param fp the pointer of struct file to close +* @return always 0 +*/ +static int closeFile(struct file *fp) +{ + filp_close(fp, NULL); + return 0; +} + +static int readFile(struct file *fp, char *buf, int len) +{ + int rlen = 0, sum = 0; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)) + if (!(fp->f_mode & FMODE_CAN_READ)) +#else + if (!fp->f_op || !fp->f_op->read) +#endif + return -EPERM; + + while (sum < len) { +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)) + rlen = kernel_read(fp, buf + sum, len - sum, &fp->f_pos); +#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)) + rlen = __vfs_read(fp, buf + sum, len - sum, &fp->f_pos); +#else + rlen = fp->f_op->read(fp, buf + sum, len - sum, &fp->f_pos); +#endif + if (rlen > 0) + sum += rlen; + else if (0 != rlen) + return rlen; + else + break; + } + + return sum; + +} + +static int writeFile(struct file *fp, char *buf, int len) +{ + int wlen = 0, sum = 0; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)) + if (!(fp->f_mode & FMODE_CAN_WRITE)) +#else + if (!fp->f_op || !fp->f_op->write) +#endif + return -EPERM; + + while (sum < len) { +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)) + wlen = kernel_write(fp, buf + sum, len - sum, &fp->f_pos); +#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)) + wlen = __vfs_write(fp, buf + sum, len - sum, &fp->f_pos); +#else + wlen = fp->f_op->write(fp, buf + sum, len - sum, &fp->f_pos); +#endif + if (wlen > 0) + sum += wlen; + else if (0 != wlen) + return wlen; + else + break; + } + + return sum; + +} + +/* +* Test if the specifi @param pathname is a direct and readable +* If readable, @param sz is not used +* @param pathname the name of the path to test +* @return Linux specific error code +*/ +static int isDirReadable(const char *pathname, u32 *sz) +{ + struct path path; + int error = 0; + + return kern_path(pathname, LOOKUP_FOLLOW, &path); +} + +/* +* Test if the specifi @param path is a file and readable +* If readable, @param sz is got +* @param path the path of the file to test +* @return Linux specific error code +*/ +static int isFileReadable(const char *path, u32 *sz) +{ + struct file *fp; + int ret = 0; + #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0)) + mm_segment_t oldfs; + #endif + char buf; + + fp = filp_open(path, O_RDONLY, 0); + if (IS_ERR(fp)) + ret = PTR_ERR(fp); + else { + #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0)) + oldfs = get_fs(); + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0)) + set_fs(KERNEL_DS); + #else + set_fs(get_ds()); + #endif + #endif + + if (1 != readFile(fp, &buf, 1)) + ret = PTR_ERR(fp); + + if (ret == 0 && sz) { + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)) + *sz = i_size_read(fp->f_path.dentry->d_inode); + #else + *sz = i_size_read(fp->f_dentry->d_inode); + #endif + } + + #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0)) + set_fs(oldfs); + #endif + filp_close(fp, NULL); + } + return ret; +} + +/* +* Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most +* @param path the path of the file to open and read +* @param buf the starting address of the buffer to store file content +* @param sz how many bytes to read at most +* @return the byte we've read, or Linux specific error code +*/ +static int retriveFromFile(const char *path, u8 *buf, u32 sz) +{ + int ret = -1; + #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0)) + mm_segment_t oldfs; + #endif + struct file *fp; + + if (path && buf) { + ret = openFile(&fp, path, O_RDONLY, 0); + if (0 == ret) { + RTW_INFO("%s openFile path:%s fp=%p\n", __FUNCTION__, path , fp); + + #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0)) + oldfs = get_fs(); + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0)) + set_fs(KERNEL_DS); + #else + set_fs(get_ds()); + #endif + #endif + ret = readFile(fp, buf, sz); + #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0)) + set_fs(oldfs); + #endif + closeFile(fp); + + RTW_INFO("%s readFile, ret:%d\n", __FUNCTION__, ret); + + } else + RTW_INFO("%s openFile path:%s Fail, ret:%d\n", __FUNCTION__, path, ret); + } else { + RTW_INFO("%s NULL pointer\n", __FUNCTION__); + ret = -EINVAL; + } + return ret; +} + +/* +* Open the file with @param path and wirte @param sz byte of data starting from @param buf into the file +* @param path the path of the file to open and write +* @param buf the starting address of the data to write into file +* @param sz how many bytes to write at most +* @return the byte we've written, or Linux specific error code +*/ +static int storeToFile(const char *path, u8 *buf, u32 sz) +{ + int ret = 0; + #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0)) + mm_segment_t oldfs; + #endif + struct file *fp; + + if (path && buf) { + ret = openFile(&fp, path, O_CREAT | O_WRONLY, 0666); + if (0 == ret) { + RTW_INFO("%s openFile path:%s fp=%p\n", __FUNCTION__, path , fp); + + #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0)) + oldfs = get_fs(); + #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0)) + set_fs(KERNEL_DS); + #else + set_fs(get_ds()); + #endif + #endif + ret = writeFile(fp, buf, sz); + #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0)) + set_fs(oldfs); + #endif + closeFile(fp); + + RTW_INFO("%s writeFile, ret:%d\n", __FUNCTION__, ret); + + } else + RTW_INFO("%s openFile path:%s Fail, ret:%d\n", __FUNCTION__, path, ret); + } else { + RTW_INFO("%s NULL pointer\n", __FUNCTION__); + ret = -EINVAL; + } + return ret; +} +#endif /* PLATFORM_LINUX */ + +/* +* Test if the specifi @param path is a direct and readable +* @param path the path of the direct to test +* @return _TRUE or _FALSE +*/ +int rtw_is_dir_readable(const char *path) +{ +#ifdef PLATFORM_LINUX + if (isDirReadable(path, NULL) == 0) + return _TRUE; + else + return _FALSE; +#else + /* Todo... */ + return _FALSE; +#endif +} + +/* +* Test if the specifi @param path is a file and readable +* @param path the path of the file to test +* @return _TRUE or _FALSE +*/ +int rtw_is_file_readable(const char *path) +{ +#ifdef PLATFORM_LINUX + if (isFileReadable(path, NULL) == 0) + return _TRUE; + else + return _FALSE; +#else + /* Todo... */ + return _FALSE; +#endif +} + +/* +* Test if the specifi @param path is a file and readable. +* If readable, @param sz is got +* @param path the path of the file to test +* @return _TRUE or _FALSE +*/ +int rtw_is_file_readable_with_size(const char *path, u32 *sz) +{ +#ifdef PLATFORM_LINUX + if (isFileReadable(path, sz) == 0) + return _TRUE; + else + return _FALSE; +#else + /* Todo... */ + return _FALSE; +#endif +} + +/* +* Test if the specifi @param path is a readable file with valid size. +* If readable, @param sz is got +* @param path the path of the file to test +* @return _TRUE or _FALSE +*/ +int rtw_readable_file_sz_chk(const char *path, u32 sz) +{ + u32 fsz; + + if (rtw_is_file_readable_with_size(path, &fsz) == _FALSE) + return _FALSE; + + if (fsz > sz) + return _FALSE; + + return _TRUE; +} + +/* +* Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most +* @param path the path of the file to open and read +* @param buf the starting address of the buffer to store file content +* @param sz how many bytes to read at most +* @return the byte we've read +*/ +int rtw_retrieve_from_file(const char *path, u8 *buf, u32 sz) +{ +#ifdef PLATFORM_LINUX + int ret = retriveFromFile(path, buf, sz); + return ret >= 0 ? ret : 0; +#else + /* Todo... */ + return 0; +#endif +} + +/* +* Open the file with @param path and wirte @param sz byte of data starting from @param buf into the file +* @param path the path of the file to open and write +* @param buf the starting address of the data to write into file +* @param sz how many bytes to write at most +* @return the byte we've written +*/ +int rtw_store_to_file(const char *path, u8 *buf, u32 sz) +{ +#ifdef PLATFORM_LINUX + int ret = storeToFile(path, buf, sz); + return ret >= 0 ? ret : 0; +#else + /* Todo... */ + return 0; +#endif +} + +#ifdef PLATFORM_LINUX +struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv, void *old_priv) +{ + struct net_device *pnetdev; + struct rtw_netdev_priv_indicator *pnpi; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4); +#else + pnetdev = alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator)); +#endif + if (!pnetdev) + goto RETURN; + + pnpi = netdev_priv(pnetdev); + pnpi->priv = old_priv; + pnpi->sizeof_priv = sizeof_priv; + +RETURN: + return pnetdev; +} + +struct net_device *rtw_alloc_etherdev(int sizeof_priv) +{ + struct net_device *pnetdev; + struct rtw_netdev_priv_indicator *pnpi; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) + pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4); +#else + pnetdev = alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator)); +#endif + if (!pnetdev) + goto RETURN; + + pnpi = netdev_priv(pnetdev); + + pnpi->priv = rtw_zvmalloc(sizeof_priv); + if (!pnpi->priv) { + free_netdev(pnetdev); + pnetdev = NULL; + goto RETURN; + } + + pnpi->sizeof_priv = sizeof_priv; +RETURN: + return pnetdev; +} + +void rtw_free_netdev(struct net_device *netdev) +{ + struct rtw_netdev_priv_indicator *pnpi; + + if (!netdev) + goto RETURN; + + pnpi = netdev_priv(netdev); + + if (!pnpi->priv) + goto RETURN; + + free_netdev(netdev); + +RETURN: + return; +} + +#endif + +#ifdef PLATFORM_FREEBSD +/* + * Copy a buffer from userspace and write into kernel address + * space. + * + * This emulation just calls the FreeBSD copyin function (to + * copy data from user space buffer into a kernel space buffer) + * and is designed to be used with the above io_write_wrapper. + * + * This function should return the number of bytes not copied. + * I.e. success results in a zero value. + * Negative error values are not returned. + */ +unsigned long +copy_from_user(void *to, const void *from, unsigned long n) +{ + if (copyin(from, to, n) != 0) { + /* Any errors will be treated as a failure + to copy any of the requested bytes */ + return n; + } + + return 0; +} + +unsigned long +copy_to_user(void *to, const void *from, unsigned long n) +{ + if (copyout(from, to, n) != 0) { + /* Any errors will be treated as a failure + to copy any of the requested bytes */ + return n; + } + + return 0; +} + + +/* + * The usb_register and usb_deregister functions are used to register + * usb drivers with the usb subsystem. In this compatibility layer + * emulation a list of drivers (struct usb_driver) is maintained + * and is used for probing/attaching etc. + * + * usb_register and usb_deregister simply call these functions. + */ +int +usb_register(struct usb_driver *driver) +{ + rtw_usb_linux_register(driver); + return 0; +} + + +int +usb_deregister(struct usb_driver *driver) +{ + rtw_usb_linux_deregister(driver); + return 0; +} + +void module_init_exit_wrapper(void *arg) +{ + int (*func)(void) = arg; + func(); + return; +} + +#endif /* PLATFORM_FREEBSD */ + +#ifdef CONFIG_PLATFORM_SPRD + #ifdef do_div + #undef do_div + #endif + #include +#endif + +u64 rtw_modular64(u64 x, u64 y) +{ +#ifdef PLATFORM_LINUX + return do_div(x, y); +#elif defined(PLATFORM_WINDOWS) + return x % y; +#elif defined(PLATFORM_FREEBSD) + return x % y; +#endif +} + +u64 rtw_division64(u64 x, u64 y) +{ +#ifdef PLATFORM_LINUX + do_div(x, y); + return x; +#elif defined(PLATFORM_WINDOWS) + return x / y; +#elif defined(PLATFORM_FREEBSD) + return x / y; +#endif +} + +inline u32 rtw_random32(void) +{ +#ifdef PLATFORM_LINUX +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) + return prandom_u32(); +#elif (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18)) + u32 random_int; + get_random_bytes(&random_int , 4); + return random_int; +#else + return random32(); +#endif +#elif defined(PLATFORM_WINDOWS) +#error "to be implemented\n" +#elif defined(PLATFORM_FREEBSD) +#error "to be implemented\n" +#endif +} + +void rtw_buf_free(u8 **buf, u32 *buf_len) +{ + u32 ori_len; + + if (!buf || !buf_len) + return; + + ori_len = *buf_len; + + if (*buf) { + u32 tmp_buf_len = *buf_len; + *buf_len = 0; + rtw_mfree(*buf, tmp_buf_len); + *buf = NULL; + } +} + +void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len) +{ + u32 ori_len = 0, dup_len = 0; + u8 *ori = NULL; + u8 *dup = NULL; + + if (!buf || !buf_len) + return; + + if (!src || !src_len) + goto keep_ori; + + /* duplicate src */ + dup = rtw_malloc(src_len); + if (dup) { + dup_len = src_len; + _rtw_memcpy(dup, src, dup_len); + } + +keep_ori: + ori = *buf; + ori_len = *buf_len; + + /* replace buf with dup */ + *buf_len = 0; + *buf = dup; + *buf_len = dup_len; + + /* free ori */ + if (ori && ori_len > 0) + rtw_mfree(ori, ori_len); +} + + +/** + * rtw_cbuf_full - test if cbuf is full + * @cbuf: pointer of struct rtw_cbuf + * + * Returns: _TRUE if cbuf is full + */ +inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf) +{ + return (cbuf->write == cbuf->read - 1) ? _TRUE : _FALSE; +} + +/** + * rtw_cbuf_empty - test if cbuf is empty + * @cbuf: pointer of struct rtw_cbuf + * + * Returns: _TRUE if cbuf is empty + */ +inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf) +{ + return (cbuf->write == cbuf->read) ? _TRUE : _FALSE; +} + +/** + * rtw_cbuf_push - push a pointer into cbuf + * @cbuf: pointer of struct rtw_cbuf + * @buf: pointer to push in + * + * Lock free operation, be careful of the use scheme + * Returns: _TRUE push success + */ +bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf) +{ + if (rtw_cbuf_full(cbuf)) + return _FAIL; + + if (0) + RTW_INFO("%s on %u\n", __func__, cbuf->write); + cbuf->bufs[cbuf->write] = buf; + cbuf->write = (cbuf->write + 1) % cbuf->size; + + return _SUCCESS; +} + +/** + * rtw_cbuf_pop - pop a pointer from cbuf + * @cbuf: pointer of struct rtw_cbuf + * + * Lock free operation, be careful of the use scheme + * Returns: pointer popped out + */ +void *rtw_cbuf_pop(struct rtw_cbuf *cbuf) +{ + void *buf; + if (rtw_cbuf_empty(cbuf)) + return NULL; + + if (0) + RTW_INFO("%s on %u\n", __func__, cbuf->read); + buf = cbuf->bufs[cbuf->read]; + cbuf->read = (cbuf->read + 1) % cbuf->size; + + return buf; +} + +/** + * rtw_cbuf_alloc - allocte a rtw_cbuf with given size and do initialization + * @size: size of pointer + * + * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure + */ +struct rtw_cbuf *rtw_cbuf_alloc(u32 size) +{ + struct rtw_cbuf *cbuf; + + cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) + sizeof(void *) * size); + + if (cbuf) { + cbuf->write = cbuf->read = 0; + cbuf->size = size; + } + + return cbuf; +} + +/** + * rtw_cbuf_free - free the given rtw_cbuf + * @cbuf: pointer of struct rtw_cbuf to free + */ +void rtw_cbuf_free(struct rtw_cbuf *cbuf) +{ + rtw_mfree((u8 *)cbuf, sizeof(*cbuf) + sizeof(void *) * cbuf->size); +} + +/** + * map_readN - read a range of map data + * @map: map to read + * @offset: start address to read + * @len: length to read + * @buf: pointer of buffer to store data read + * + * Returns: _SUCCESS or _FAIL + */ +int map_readN(const struct map_t *map, u16 offset, u16 len, u8 *buf) +{ + const struct map_seg_t *seg; + int ret = _FAIL; + int i; + + if (len == 0) { + rtw_warn_on(1); + goto exit; + } + + if (offset + len > map->len) { + rtw_warn_on(1); + goto exit; + } + + _rtw_memset(buf, map->init_value, len); + + for (i = 0; i < map->seg_num; i++) { + u8 *c_dst, *c_src; + u16 c_len; + + seg = map->segs + i; + if (seg->sa + seg->len <= offset || seg->sa >= offset + len) + continue; + + if (seg->sa >= offset) { + c_dst = buf + (seg->sa - offset); + c_src = seg->c; + if (seg->sa + seg->len <= offset + len) + c_len = seg->len; + else + c_len = offset + len - seg->sa; + } else { + c_dst = buf; + c_src = seg->c + (offset - seg->sa); + if (seg->sa + seg->len >= offset + len) + c_len = len; + else + c_len = seg->sa + seg->len - offset; + } + + _rtw_memcpy(c_dst, c_src, c_len); + } + +exit: + return ret; +} + +/** + * map_read8 - read 1 byte of map data + * @map: map to read + * @offset: address to read + * + * Returns: value of data of specified offset. map.init_value if offset is out of range + */ +u8 map_read8(const struct map_t *map, u16 offset) +{ + const struct map_seg_t *seg; + u8 val = map->init_value; + int i; + + if (offset + 1 > map->len) { + rtw_warn_on(1); + goto exit; + } + + for (i = 0; i < map->seg_num; i++) { + seg = map->segs + i; + if (seg->sa + seg->len <= offset || seg->sa >= offset + 1) + continue; + + val = *(seg->c + offset - seg->sa); + break; + } + +exit: + return val; +} + +#ifdef CONFIG_RTW_MESH +int rtw_blacklist_add(_queue *blist, const u8 *addr, u32 timeout_ms) +{ + struct blacklist_ent *ent; + _list *list, *head; + u8 exist = _FALSE, timeout = _FALSE; + + enter_critical_bh(&blist->lock); + + head = &blist->queue; + list = get_next(head); + while (rtw_end_of_queue_search(head, list) == _FALSE) { + ent = LIST_CONTAINOR(list, struct blacklist_ent, list); + list = get_next(list); + + if (_rtw_memcmp(ent->addr, addr, ETH_ALEN) == _TRUE) { + exist = _TRUE; + if (rtw_time_after(rtw_get_current_time(), ent->exp_time)) + timeout = _TRUE; + ent->exp_time = rtw_get_current_time() + + rtw_ms_to_systime(timeout_ms); + break; + } + + if (rtw_time_after(rtw_get_current_time(), ent->exp_time)) { + rtw_list_delete(&ent->list); + rtw_mfree(ent, sizeof(struct blacklist_ent)); + } + } + + if (exist == _FALSE) { + ent = rtw_malloc(sizeof(struct blacklist_ent)); + if (ent) { + _rtw_memcpy(ent->addr, addr, ETH_ALEN); + ent->exp_time = rtw_get_current_time() + + rtw_ms_to_systime(timeout_ms); + rtw_list_insert_tail(&ent->list, head); + } + } + + exit_critical_bh(&blist->lock); + + return (exist == _TRUE && timeout == _FALSE) ? RTW_ALREADY : (ent ? _SUCCESS : _FAIL); +} + +int rtw_blacklist_del(_queue *blist, const u8 *addr) +{ + struct blacklist_ent *ent = NULL; + _list *list, *head; + u8 exist = _FALSE; + + enter_critical_bh(&blist->lock); + head = &blist->queue; + list = get_next(head); + while (rtw_end_of_queue_search(head, list) == _FALSE) { + ent = LIST_CONTAINOR(list, struct blacklist_ent, list); + list = get_next(list); + + if (_rtw_memcmp(ent->addr, addr, ETH_ALEN) == _TRUE) { + rtw_list_delete(&ent->list); + rtw_mfree(ent, sizeof(struct blacklist_ent)); + exist = _TRUE; + break; + } + + if (rtw_time_after(rtw_get_current_time(), ent->exp_time)) { + rtw_list_delete(&ent->list); + rtw_mfree(ent, sizeof(struct blacklist_ent)); + } + } + + exit_critical_bh(&blist->lock); + + return exist == _TRUE ? _SUCCESS : RTW_ALREADY; +} + +int rtw_blacklist_search(_queue *blist, const u8 *addr) +{ + struct blacklist_ent *ent = NULL; + _list *list, *head; + u8 exist = _FALSE; + + enter_critical_bh(&blist->lock); + head = &blist->queue; + list = get_next(head); + while (rtw_end_of_queue_search(head, list) == _FALSE) { + ent = LIST_CONTAINOR(list, struct blacklist_ent, list); + list = get_next(list); + + if (_rtw_memcmp(ent->addr, addr, ETH_ALEN) == _TRUE) { + if (rtw_time_after(rtw_get_current_time(), ent->exp_time)) { + rtw_list_delete(&ent->list); + rtw_mfree(ent, sizeof(struct blacklist_ent)); + } else + exist = _TRUE; + break; + } + + if (rtw_time_after(rtw_get_current_time(), ent->exp_time)) { + rtw_list_delete(&ent->list); + rtw_mfree(ent, sizeof(struct blacklist_ent)); + } + } + + exit_critical_bh(&blist->lock); + + return exist; +} + +void rtw_blacklist_flush(_queue *blist) +{ + struct blacklist_ent *ent; + _list *list, *head; + _list tmp; + + _rtw_init_listhead(&tmp); + + enter_critical_bh(&blist->lock); + rtw_list_splice_init(&blist->queue, &tmp); + exit_critical_bh(&blist->lock); + + head = &tmp; + list = get_next(head); + while (rtw_end_of_queue_search(head, list) == _FALSE) { + ent = LIST_CONTAINOR(list, struct blacklist_ent, list); + list = get_next(list); + rtw_list_delete(&ent->list); + rtw_mfree(ent, sizeof(struct blacklist_ent)); + } +} + +void dump_blacklist(void *sel, _queue *blist, const char *title) +{ + struct blacklist_ent *ent = NULL; + _list *list, *head; + + enter_critical_bh(&blist->lock); + head = &blist->queue; + list = get_next(head); + + if (rtw_end_of_queue_search(head, list) == _FALSE) { + if (title) + RTW_PRINT_SEL(sel, "%s:\n", title); + + while (rtw_end_of_queue_search(head, list) == _FALSE) { + ent = LIST_CONTAINOR(list, struct blacklist_ent, list); + list = get_next(list); + + if (rtw_time_after(rtw_get_current_time(), ent->exp_time)) + RTW_PRINT_SEL(sel, MAC_FMT" expired\n", MAC_ARG(ent->addr)); + else + RTW_PRINT_SEL(sel, MAC_FMT" %u\n", MAC_ARG(ent->addr) + , rtw_get_remaining_time_ms(ent->exp_time)); + } + + } + exit_critical_bh(&blist->lock); +} +#endif + +/** +* is_null - +* +* Return TRUE if c is null character +* FALSE otherwise. +*/ +inline BOOLEAN is_null(char c) +{ + if (c == '\0') + return _TRUE; + else + return _FALSE; +} + +inline BOOLEAN is_all_null(char *c, int len) +{ + for (; len > 0; len--) + if (c[len - 1] != '\0') + return _FALSE; + + return _TRUE; +} + +/** +* is_eol - +* +* Return TRUE if c is represent for EOL (end of line) +* FALSE otherwise. +*/ +inline BOOLEAN is_eol(char c) +{ + if (c == '\r' || c == '\n') + return _TRUE; + else + return _FALSE; +} + +/** +* is_space - +* +* Return TRUE if c is represent for space +* FALSE otherwise. +*/ +inline BOOLEAN is_space(char c) +{ + if (c == ' ' || c == '\t') + return _TRUE; + else + return _FALSE; +} + +/** +* IsHexDigit - +* +* Return TRUE if chTmp is represent for hex digit +* FALSE otherwise. +*/ +inline BOOLEAN IsHexDigit(char chTmp) +{ + if ((chTmp >= '0' && chTmp <= '9') || + (chTmp >= 'a' && chTmp <= 'f') || + (chTmp >= 'A' && chTmp <= 'F')) + return _TRUE; + else + return _FALSE; +} + +/** +* is_alpha - +* +* Return TRUE if chTmp is represent for alphabet +* FALSE otherwise. +*/ +inline BOOLEAN is_alpha(char chTmp) +{ + if ((chTmp >= 'a' && chTmp <= 'z') || + (chTmp >= 'A' && chTmp <= 'Z')) + return _TRUE; + else + return _FALSE; +} + +inline char alpha_to_upper(char c) +{ + if ((c >= 'a' && c <= 'z')) + c = 'A' + (c - 'a'); + return c; +} + +int hex2num_i(char c) +{ + if (c >= '0' && c <= '9') + return c - '0'; + if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + return -1; +} + +int hex2byte_i(const char *hex) +{ + int a, b; + a = hex2num_i(*hex++); + if (a < 0) + return -1; + b = hex2num_i(*hex++); + if (b < 0) + return -1; + return (a << 4) | b; +} + +int hexstr2bin(const char *hex, u8 *buf, size_t len) +{ + size_t i; + int a; + const char *ipos = hex; + u8 *opos = buf; + + for (i = 0; i < len; i++) { + a = hex2byte_i(ipos); + if (a < 0) + return -1; + *opos++ = a; + ipos += 2; + } + return 0; +} + +/** + * hwaddr_aton - Convert ASCII string to MAC address + * @txt: MAC address as a string (e.g., "00:11:22:33:44:55") + * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes) + * Returns: 0 on success, -1 on failure (e.g., string not a MAC address) + */ +int hwaddr_aton_i(const char *txt, u8 *addr) +{ + int i; + + for (i = 0; i < 6; i++) { + int a, b; + + a = hex2num_i(*txt++); + if (a < 0) + return -1; + b = hex2num_i(*txt++); + if (b < 0) + return -1; + *addr++ = (a << 4) | b; + if (i < 5 && *txt++ != ':') + return -1; + } + + return 0; +} + diff --git a/platform/custom_country_chplan.h b/platform/custom_country_chplan.h new file mode 100644 index 0000000..67f621f --- /dev/null +++ b/platform/custom_country_chplan.h @@ -0,0 +1,22 @@ +/****************************************************************************** + * + * Copyright(c) 2013 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ + +#error "You have defined CONFIG_CUSTOMIZED_COUNTRY_CHPLAN_MAP to use a customized map of your own instead of the default one" +#error "Before removing these error notifications, please make sure regulatory certification requirements of your target markets" + +static const struct country_chplan CUSTOMIZED_country_chplan_map[] = { + COUNTRY_CHPLAN_ENT("TW", 0x76, 1), /* Taiwan */ +}; + diff --git a/platform/platform_ARM_SUN50IW1P1_sdio.c b/platform/platform_ARM_SUN50IW1P1_sdio.c new file mode 100644 index 0000000..2586455 --- /dev/null +++ b/platform/platform_ARM_SUN50IW1P1_sdio.c @@ -0,0 +1,86 @@ +/****************************************************************************** + * + * Copyright(c) 2013 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +/* + * Description: + * This file can be applied to following platforms: + * CONFIG_PLATFORM_ARM_SUN50IW1P1 + */ +#include +#ifdef CONFIG_GPIO_WAKEUP +#include +#endif + +#ifdef CONFIG_MMC +#if defined(CONFIG_PLATFORM_ARM_SUN50IW1P1) +extern void sunxi_mmc_rescan_card(unsigned ids); +extern void sunxi_wlan_set_power(int on); +extern int sunxi_wlan_get_bus_index(void); +extern int sunxi_wlan_get_oob_irq(void); +extern int sunxi_wlan_get_oob_irq_flags(void); +#endif +#ifdef CONFIG_GPIO_WAKEUP +extern unsigned int oob_irq; +#endif +#endif /* CONFIG_MMC */ + +/* + * Return: + * 0: power on successfully + * others: power on failed + */ +int platform_wifi_power_on(void) +{ + int ret = 0; + +#ifdef CONFIG_MMC + { + +#if defined(CONFIG_PLATFORM_ARM_SUN50IW1P1) + int wlan_bus_index = sunxi_wlan_get_bus_index(); + if (wlan_bus_index < 0) + return wlan_bus_index; + + sunxi_wlan_set_power(1); + mdelay(100); + sunxi_mmc_rescan_card(wlan_bus_index); +#endif + RTW_INFO("%s: power up, rescan card.\n", __FUNCTION__); + +#ifdef CONFIG_GPIO_WAKEUP +#if defined(CONFIG_PLATFORM_ARM_SUN50IW1P1) + oob_irq = sunxi_wlan_get_oob_irq(); +#endif +#endif /* CONFIG_GPIO_WAKEUP */ + } +#endif /* CONFIG_MMC */ + + return ret; +} + +void platform_wifi_power_off(void) +{ +#ifdef CONFIG_MMC +#if defined(CONFIG_PLATFORM_ARM_SUN50IW1P1) + int wlan_bus_index = sunxi_wlan_get_bus_index(); + if (wlan_bus_index < 0) + return; + + sunxi_mmc_rescan_card(wlan_bus_index); + mdelay(100); + sunxi_wlan_set_power(0); +#endif + RTW_INFO("%s: remove card, power off.\n", __FUNCTION__); +#endif /* CONFIG_MMC */ +} diff --git a/platform/platform_ARM_SUNnI_sdio.c b/platform/platform_ARM_SUNnI_sdio.c new file mode 100644 index 0000000..8a52aa9 --- /dev/null +++ b/platform/platform_ARM_SUNnI_sdio.c @@ -0,0 +1,130 @@ +/****************************************************************************** + * + * Copyright(c) 2013 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +/* + * Description: + * This file can be applied to following platforms: + * CONFIG_PLATFORM_ARM_SUN6I + * CONFIG_PLATFORM_ARM_SUN7I + * CONFIG_PLATFORM_ARM_SUN8I + */ +#include +#include +#ifdef CONFIG_GPIO_WAKEUP +#include +#endif + +#ifdef CONFIG_MMC +static int sdc_id = -1; +static signed int gpio_eint_wlan = -1; +static u32 eint_wlan_handle = 0; + +#if defined(CONFIG_PLATFORM_ARM_SUN6I) || defined(CONFIG_PLATFORM_ARM_SUN7I) +extern void sw_mci_rescan_card(unsigned id, unsigned insert); +#elif defined(CONFIG_PLATFORM_ARM_SUN8I) +extern void sunxi_mci_rescan_card(unsigned id, unsigned insert); +#endif + +#ifdef CONFIG_PLATFORM_ARM_SUN8I_W5P1 +extern int get_rf_mod_type(void); +#else +extern int wifi_pm_get_mod_type(void); +#endif + +extern void wifi_pm_power(int on); +#ifdef CONFIG_GPIO_WAKEUP +extern unsigned int oob_irq; +#endif +#endif /* CONFIG_MMC */ + +/* + * Return: + * 0: power on successfully + * others: power on failed + */ +int platform_wifi_power_on(void) +{ + int ret = 0; + +#ifdef CONFIG_MMC + { + script_item_u val; + script_item_value_type_e type; + +#ifdef CONFIG_PLATFORM_ARM_SUN8I_W5P1 + unsigned int mod_sel = get_rf_mod_type(); +#else + unsigned int mod_sel = wifi_pm_get_mod_type(); +#endif + + type = script_get_item("wifi_para", "wifi_sdc_id", &val); + if (SCIRPT_ITEM_VALUE_TYPE_INT != type) { + RTW_INFO("get wifi_sdc_id failed\n"); + ret = -1; + } else { + sdc_id = val.val; + RTW_INFO("----- %s sdc_id: %d, mod_sel: %d\n", __FUNCTION__, sdc_id, mod_sel); + +#if defined(CONFIG_PLATFORM_ARM_SUN6I) || defined(CONFIG_PLATFORM_ARM_SUN7I) + sw_mci_rescan_card(sdc_id, 1); +#elif defined(CONFIG_PLATFORM_ARM_SUN8I) + sunxi_mci_rescan_card(sdc_id, 1); +#endif + mdelay(100); + wifi_pm_power(1); + + RTW_INFO("%s: power up, rescan card.\n", __FUNCTION__); + } + +#ifdef CONFIG_GPIO_WAKEUP +#ifdef CONFIG_PLATFORM_ARM_SUN8I_W5P1 + type = script_get_item("wifi_para", "wl_host_wake", &val); +#else +#ifdef CONFIG_RTL8723B + type = script_get_item("wifi_para", "rtl8723bs_wl_host_wake", &val); +#endif +#ifdef CONFIG_RTL8188E + type = script_get_item("wifi_para", "rtl8189es_host_wake", &val); +#endif +#endif /* CONFIG_PLATFORM_ARM_SUN8I_W5P1 */ + if (SCIRPT_ITEM_VALUE_TYPE_PIO != type) { + RTW_INFO("No definition of wake up host PIN\n"); + ret = -1; + } else { + gpio_eint_wlan = val.gpio.gpio; +#ifdef CONFIG_PLATFORM_ARM_SUN8I + oob_irq = gpio_to_irq(gpio_eint_wlan); +#endif + } +#endif /* CONFIG_GPIO_WAKEUP */ + } +#endif /* CONFIG_MMC */ + + return ret; +} + +void platform_wifi_power_off(void) +{ +#ifdef CONFIG_MMC +#if defined(CONFIG_PLATFORM_ARM_SUN6I) || defined(CONFIG_PLATFORM_ARM_SUN7I) + sw_mci_rescan_card(sdc_id, 0); +#elif defined(CONFIG_PLATFORM_ARM_SUN8I) + sunxi_mci_rescan_card(sdc_id, 0); +#endif + mdelay(100); + wifi_pm_power(0); + + RTW_INFO("%s: remove card, power off.\n", __FUNCTION__); +#endif /* CONFIG_MMC */ +} diff --git a/platform/platform_ARM_SUNxI_sdio.c b/platform/platform_ARM_SUNxI_sdio.c new file mode 100644 index 0000000..795b7e7 --- /dev/null +++ b/platform/platform_ARM_SUNxI_sdio.c @@ -0,0 +1,90 @@ +/****************************************************************************** + * + * Copyright(c) 2013 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#include + +#ifdef CONFIG_MMC_SUNXI_POWER_CONTROL +#ifdef CONFIG_WITS_EVB_V13 + #define SDIOID 0 +#else /* !CONFIG_WITS_EVB_V13 */ + #define SDIOID (CONFIG_CHIP_ID == 1123 ? 3 : 1) +#endif /* !CONFIG_WITS_EVB_V13 */ + +#define SUNXI_SDIO_WIFI_NUM_RTL8189ES 10 +extern void sunximmc_rescan_card(unsigned id, unsigned insert); +extern int mmc_pm_get_mod_type(void); +extern int mmc_pm_gpio_ctrl(char *name, int level); +/* + * rtl8189es_shdn = port:PH09<1><0> + * rtl8189es_wakeup = port:PH10<1><1> + * rtl8189es_vdd_en = port:PH11<1><0> + * rtl8189es_vcc_en = port:PH12<1><0> + */ + +int rtl8189es_sdio_powerup(void) +{ + mmc_pm_gpio_ctrl("rtl8189es_vdd_en", 1); + udelay(100); + mmc_pm_gpio_ctrl("rtl8189es_vcc_en", 1); + udelay(50); + mmc_pm_gpio_ctrl("rtl8189es_shdn", 1); + return 0; +} + +int rtl8189es_sdio_poweroff(void) +{ + mmc_pm_gpio_ctrl("rtl8189es_shdn", 0); + mmc_pm_gpio_ctrl("rtl8189es_vcc_en", 0); + mmc_pm_gpio_ctrl("rtl8189es_vdd_en", 0); + return 0; +} +#endif /* CONFIG_MMC_SUNXI_POWER_CONTROL */ + +/* + * Return: + * 0: power on successfully + * others: power on failed + */ +int platform_wifi_power_on(void) +{ + int ret = 0; +#ifdef CONFIG_MMC_SUNXI_POWER_CONTROL + unsigned int mod_sel = mmc_pm_get_mod_type(); +#endif /* CONFIG_MMC_SUNXI_POWER_CONTROL */ + + +#ifdef CONFIG_MMC_SUNXI_POWER_CONTROL + if (mod_sel == SUNXI_SDIO_WIFI_NUM_RTL8189ES) { + rtl8189es_sdio_powerup(); + sunximmc_rescan_card(SDIOID, 1); + printk("[rtl8189es] %s: power up, rescan card.\n", __FUNCTION__); + } else { + ret = -1; + printk("[rtl8189es] %s: mod_sel = %d is incorrect.\n", __FUNCTION__, mod_sel); + } +#endif /* CONFIG_MMC_SUNXI_POWER_CONTROL */ + + return ret; +} + +void platform_wifi_power_off(void) +{ +#ifdef CONFIG_MMC_SUNXI_POWER_CONTROL + sunximmc_rescan_card(SDIOID, 0); +#ifdef CONFIG_RTL8188E + rtl8189es_sdio_poweroff(); + printk("[rtl8189es] %s: remove card, power off.\n", __FUNCTION__); +#endif /* CONFIG_RTL8188E */ +#endif /* CONFIG_MMC_SUNXI_POWER_CONTROL */ +} diff --git a/platform/platform_ARM_SUNxI_usb.c b/platform/platform_ARM_SUNxI_usb.c new file mode 100644 index 0000000..9c2abc4 --- /dev/null +++ b/platform/platform_ARM_SUNxI_usb.c @@ -0,0 +1,136 @@ +/****************************************************************************** + * + * Copyright(c) 2013 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +/* + * Description: + * This file can be applied to following platforms: + * CONFIG_PLATFORM_ARM_SUNXI Series platform + * + */ + +#include +#include + +#ifdef CONFIG_PLATFORM_ARM_SUNxI +extern int sw_usb_disable_hcd(__u32 usbc_no); +extern int sw_usb_enable_hcd(__u32 usbc_no); +static int usb_wifi_host = 2; +#endif + +#if defined(CONFIG_PLATFORM_ARM_SUN6I) || defined(CONFIG_PLATFORM_ARM_SUN7I) +extern int sw_usb_disable_hcd(__u32 usbc_no); +extern int sw_usb_enable_hcd(__u32 usbc_no); +extern void wifi_pm_power(int on); +static script_item_u item; +#endif + +#ifdef CONFIG_PLATFORM_ARM_SUN8I +extern int sunxi_usb_disable_hcd(__u32 usbc_no); +extern int sunxi_usb_enable_hcd(__u32 usbc_no); +extern void wifi_pm_power(int on); +static script_item_u item; +#endif + + +int platform_wifi_power_on(void) +{ + int ret = 0; + +#ifdef CONFIG_PLATFORM_ARM_SUNxI +#ifndef CONFIG_RTL8723A + { + /* ----------get usb_wifi_usbc_num------------- */ + ret = script_parser_fetch("usb_wifi_para", "usb_wifi_usbc_num", (int *)&usb_wifi_host, 64); + if (ret != 0) { + RTW_INFO("ERR: script_parser_fetch usb_wifi_usbc_num failed\n"); + ret = -ENOMEM; + goto exit; + } + RTW_INFO("sw_usb_enable_hcd: usbc_num = %d\n", usb_wifi_host); + sw_usb_enable_hcd(usb_wifi_host); + } +#endif /* CONFIG_RTL8723A */ +#endif /* CONFIG_PLATFORM_ARM_SUNxI */ + +#if defined(CONFIG_PLATFORM_ARM_SUN6I) || defined(CONFIG_PLATFORM_ARM_SUN7I) + { + script_item_value_type_e type; + + type = script_get_item("wifi_para", "wifi_usbc_id", &item); + if (SCIRPT_ITEM_VALUE_TYPE_INT != type) { + printk("ERR: script_get_item wifi_usbc_id failed\n"); + ret = -ENOMEM; + goto exit; + } + + printk("sw_usb_enable_hcd: usbc_num = %d\n", item.val); + wifi_pm_power(1); + mdelay(10); + +#if !(defined(CONFIG_RTL8723A)) && !(defined(CONFIG_RTL8723B)) + sw_usb_enable_hcd(item.val); +#endif + } +#endif /* defined(CONFIG_PLATFORM_ARM_SUN6I) || defined(CONFIG_PLATFORM_ARM_SUN7I) */ + +#if defined(CONFIG_PLATFORM_ARM_SUN8I) + { + script_item_value_type_e type; + + type = script_get_item("wifi_para", "wifi_usbc_id", &item); + if (SCIRPT_ITEM_VALUE_TYPE_INT != type) { + printk("ERR: script_get_item wifi_usbc_id failed\n"); + ret = -ENOMEM; + goto exit; + } + + printk("sw_usb_enable_hcd: usbc_num = %d\n", item.val); + wifi_pm_power(1); + mdelay(10); + +#if !(defined(CONFIG_RTL8723A)) && !(defined(CONFIG_RTL8723B)) + sunxi_usb_enable_hcd(item.val); +#endif + } +#endif /* CONFIG_PLATFORM_ARM_SUN8I */ + +exit: + return ret; +} + +void platform_wifi_power_off(void) +{ + +#ifdef CONFIG_PLATFORM_ARM_SUNxI +#ifndef CONFIG_RTL8723A + RTW_INFO("sw_usb_disable_hcd: usbc_num = %d\n", usb_wifi_host); + sw_usb_disable_hcd(usb_wifi_host); +#endif /* ifndef CONFIG_RTL8723A */ +#endif /* CONFIG_PLATFORM_ARM_SUNxI */ + +#if defined(CONFIG_PLATFORM_ARM_SUN6I) || defined(CONFIG_PLATFORM_ARM_SUN7I) + #if !(defined(CONFIG_RTL8723A)) && !(defined(CONFIG_RTL8723B)) + sw_usb_disable_hcd(item.val); + #endif + wifi_pm_power(0); +#endif /* defined(CONFIG_PLATFORM_ARM_SUN6I) || defined(CONFIG_PLATFORM_ARM_SUN7I) */ + +#if defined(CONFIG_PLATFORM_ARM_SUN8I) + #if !(defined(CONFIG_RTL8723A)) && !(defined(CONFIG_RTL8723B)) + sunxi_usb_disable_hcd(item.val); + #endif + wifi_pm_power(0); +#endif /* defined(CONFIG_PLATFORM_ARM_SUN8I) */ + +} diff --git a/platform/platform_ARM_WMT_sdio.c b/platform/platform_ARM_WMT_sdio.c new file mode 100644 index 0000000..d85002c --- /dev/null +++ b/platform/platform_ARM_WMT_sdio.c @@ -0,0 +1,46 @@ +/****************************************************************************** + * + * Copyright(c) 2013 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#include +#include +#include + +extern void wmt_detect_sdio2(void); +extern void force_remove_sdio2(void); + +int platform_wifi_power_on(void) +{ + int err = 0; + err = gpio_request(WMT_PIN_GP62_SUSGPIO1, "wifi_chip_en"); + if (err < 0) { + printk("request gpio for rtl8188eu failed!\n"); + return err; + } + gpio_direction_output(WMT_PIN_GP62_SUSGPIO1, 0);/* pull sus_gpio1 to 0 to open vcc_wifi. */ + printk("power on rtl8189.\n"); + msleep(500); + wmt_detect_sdio2(); + printk("[rtl8189es] %s: new card, power on.\n", __FUNCTION__); + return err; +} + +void platform_wifi_power_off(void) +{ + force_remove_sdio2(); + + gpio_direction_output(WMT_PIN_GP62_SUSGPIO1, 1);/* pull sus_gpio1 to 1 to close vcc_wifi. */ + printk("power off rtl8189.\n"); + gpio_free(WMT_PIN_GP62_SUSGPIO1); + printk("[rtl8189es] %s: remove card, power off.\n", __FUNCTION__); +} diff --git a/platform/platform_RTK_DMP_usb.c b/platform/platform_RTK_DMP_usb.c new file mode 100644 index 0000000..cb740b2 --- /dev/null +++ b/platform/platform_RTK_DMP_usb.c @@ -0,0 +1,30 @@ +/****************************************************************************** + * + * Copyright(c) 2013 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#include + +int platform_wifi_power_on(void) +{ + int ret = 0; + u32 tmp; + tmp = readl((volatile unsigned int *)0xb801a608); + tmp &= 0xffffff00; + tmp |= 0x55; + writel(tmp, (volatile unsigned int *)0xb801a608); /* write dummy register for 1055 */ + return ret; +} + +void platform_wifi_power_off(void) +{ +} diff --git a/platform/platform_aml_s905_sdio.c b/platform/platform_aml_s905_sdio.c new file mode 100644 index 0000000..334ca03 --- /dev/null +++ b/platform/platform_aml_s905_sdio.c @@ -0,0 +1,54 @@ +/****************************************************************************** + * + * Copyright(c) 2016 - 2018 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#include /* pr_info(() */ +#include /* msleep() */ +#include "platform_aml_s905_sdio.h" /* sdio_reinit() and etc */ + + +/* + * Return: + * 0: power on successfully + * others: power on failed + */ +int platform_wifi_power_on(void) +{ + int ret = 0; + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0)) + ret = wifi_setup_dt(); + if (ret) { + pr_err("%s: setup dt failed!!(%d)\n", __func__, ret); + return -1; + } +#endif /* kernel < 3.14.0 */ + +#if 0 /* Seems redundancy? Already done before insert driver */ + pr_info("######%s:\n", __func__); + extern_wifi_set_enable(0); + msleep(500); + extern_wifi_set_enable(1); + msleep(500); + sdio_reinit(); +#endif + + return ret; +} + +void platform_wifi_power_off(void) +{ +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0)) + wifi_teardown_dt(); +#endif /* kernel < 3.14.0 */ +} diff --git a/platform/platform_aml_s905_sdio.h b/platform/platform_aml_s905_sdio.h new file mode 100644 index 0000000..2b87576 --- /dev/null +++ b/platform/platform_aml_s905_sdio.h @@ -0,0 +1,28 @@ +/****************************************************************************** + * + * Copyright(c) 2016 - 2018 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#ifndef __PLATFORM_AML_S905_SDIO_H__ +#define __PLATFORM_AML_S905_SDIO_H__ + +#include /* Linux vresion */ + +extern void sdio_reinit(void); +extern void extern_wifi_set_enable(int is_on); + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0)) +extern void wifi_teardown_dt(void); +extern int wifi_setup_dt(void); +#endif /* kernel < 3.14.0 */ + +#endif /* __PLATFORM_AML_S905_SDIO_H__ */ diff --git a/platform/platform_arm_act_sdio.c b/platform/platform_arm_act_sdio.c new file mode 100644 index 0000000..ad7b6cf --- /dev/null +++ b/platform/platform_arm_act_sdio.c @@ -0,0 +1,53 @@ +/****************************************************************************** + * + * Copyright(c) 2013 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +/* + * Description: + * This file can be applied to following platforms: + * CONFIG_PLATFORM_ACTIONS_ATM703X + */ +#include + +#ifdef CONFIG_PLATFORM_ACTIONS_ATM705X +extern int acts_wifi_init(void); +extern void acts_wifi_cleanup(void); +#endif + +/* + * Return: + * 0: power on successfully + * others: power on failed + */ +int platform_wifi_power_on(void) +{ + int ret = 0; + +#ifdef CONFIG_PLATFORM_ACTIONS_ATM705X + ret = acts_wifi_init(); + if (unlikely(ret < 0)) { + pr_err("%s Failed to register the power control driver.\n", __FUNCTION__); + goto exit; + } +#endif + +exit: + return ret; +} + +void platform_wifi_power_off(void) +{ +#ifdef CONFIG_PLATFORM_ACTIONS_ATM705X + acts_wifi_cleanup(); +#endif +} diff --git a/platform/platform_hisilicon_hi3798_sdio.c b/platform/platform_hisilicon_hi3798_sdio.c new file mode 100644 index 0000000..11a0832 --- /dev/null +++ b/platform/platform_hisilicon_hi3798_sdio.c @@ -0,0 +1,110 @@ +/****************************************************************************** + * + * Copyright(c) 2017 - 2018 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#include /* mdelay() */ +#include /* __io_address(), readl(), writel() */ +#include "platform_hisilicon_hi3798_sdio.h" /* HI_S32() and etc. */ + +typedef enum hi_GPIO_DIR_E { + HI_DIR_OUT = 0, + HI_DIR_IN = 1, +} HI_GPIO_DIR_E; + +#define RTL_REG_ON_GPIO (4*8 + 3) + +#define REG_BASE_CTRL __io_address(0xf8a20008) + +int gpio_wlan_reg_on = RTL_REG_ON_GPIO; +#if 0 +module_param(gpio_wlan_reg_on, uint, 0644); +MODULE_PARM_DESC(gpio_wlan_reg_on, "wlan reg_on gpio num (default:gpio4_3)"); +#endif + +static int hi_gpio_set_value(u32 gpio, u32 value) +{ + HI_S32 s32Status; + + s32Status = HI_DRV_GPIO_SetDirBit(gpio, HI_DIR_OUT); + if (s32Status != HI_SUCCESS) { + pr_err("gpio(%d) HI_DRV_GPIO_SetDirBit HI_DIR_OUT failed\n", + gpio); + return -1; + } + + s32Status = HI_DRV_GPIO_WriteBit(gpio, value); + if (s32Status != HI_SUCCESS) { + pr_err("gpio(%d) HI_DRV_GPIO_WriteBit value(%d) failed\n", + gpio, value); + return -1; + } + + return 0; +} + +static int hisi_wlan_set_carddetect(bool present) +{ + u32 regval; + u32 mask; + + +#ifndef CONFIG_HISI_SDIO_ID + return; +#endif + pr_info("SDIO ID=%d\n", CONFIG_HISI_SDIO_ID); +#if (CONFIG_HISI_SDIO_ID == 1) + mask = 1; +#elif (CONFIG_HISI_SDIO_ID == 0) + mask = 2; +#endif + + regval = readl(REG_BASE_CTRL); + if (present) { + pr_info("====== Card detection to detect SDIO card! ======\n"); + /* set card_detect low to detect card */ + regval |= mask; + } else { + pr_info("====== Card detection to remove SDIO card! ======\n"); + /* set card_detect high to remove card */ + regval &= ~(mask); + } + writel(regval, REG_BASE_CTRL); + + return 0; +} + +/* + * Return: + * 0: power on successfully + * others: power on failed + */ +int platform_wifi_power_on(void) +{ + int ret = 0; + + + hi_gpio_set_value(gpio_wlan_reg_on, 1); + mdelay(100); + hisi_wlan_set_carddetect(1); + mdelay(2000); + pr_info("======== set_carddetect delay 2s! ========\n"); + + return ret; +} + +void platform_wifi_power_off(void) +{ + hisi_wlan_set_carddetect(0); + mdelay(100); + hi_gpio_set_value(gpio_wlan_reg_on, 0); +} diff --git a/platform/platform_hisilicon_hi3798_sdio.h b/platform/platform_hisilicon_hi3798_sdio.h new file mode 100644 index 0000000..1ad4240 --- /dev/null +++ b/platform/platform_hisilicon_hi3798_sdio.h @@ -0,0 +1,28 @@ +/****************************************************************************** + * + * Copyright(c) 2017 - 2018 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#ifndef __PLATFORM_HISILICON_HI3798_SDIO_H__ +#define __PLATFORM_HISILICON_HI3798_SDIO_H__ + +typedef unsigned int HI_U32; + +typedef int HI_S32; + +#define HI_SUCCESS 0 +#define HI_FAILURE (-1) + +extern HI_S32 HI_DRV_GPIO_SetDirBit(HI_U32 u32GpioNo, HI_U32 u32DirBit); +extern HI_S32 HI_DRV_GPIO_WriteBit(HI_U32 u32GpioNo, HI_U32 u32BitValue); + +#endif /* __PLATFORM_HISILICON_HI3798_SDIO_H__ */ diff --git a/platform/platform_ops.c b/platform/platform_ops.c new file mode 100644 index 0000000..10766aa --- /dev/null +++ b/platform/platform_ops.c @@ -0,0 +1,32 @@ +/****************************************************************************** + * + * Copyright(c) 2013 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#ifndef CONFIG_PLATFORM_OPS +/* + * Return: + * 0: power on successfully + * others: power on failed + */ +int platform_wifi_power_on(void) +{ + int ret = 0; + + + return ret; +} + +void platform_wifi_power_off(void) +{ +} +#endif /* !CONFIG_PLATFORM_OPS */ diff --git a/platform/platform_ops.h b/platform/platform_ops.h new file mode 100644 index 0000000..12caf3c --- /dev/null +++ b/platform/platform_ops.h @@ -0,0 +1,26 @@ +/****************************************************************************** + * + * Copyright(c) 2013 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#ifndef __PLATFORM_OPS_H__ +#define __PLATFORM_OPS_H__ + +/* + * Return: + * 0: power on successfully + * others: power on failed + */ +int platform_wifi_power_on(void); +void platform_wifi_power_off(void); + +#endif /* __PLATFORM_OPS_H__ */ diff --git a/platform/platform_sprd_sdio.c b/platform/platform_sprd_sdio.c new file mode 100644 index 0000000..34061d0 --- /dev/null +++ b/platform/platform_sprd_sdio.c @@ -0,0 +1,84 @@ +/****************************************************************************** + * + * Copyright(c) 2013 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#include + +extern void sdhci_bus_scan(void); +#ifndef ANDROID_2X +extern int sdhci_device_attached(void); +#endif + +/* + * Return: + * 0: power on successfully + * others: power on failed + */ +int platform_wifi_power_on(void) +{ + int ret = 0; + + +#ifdef CONFIG_RTL8188E + rtw_wifi_gpio_wlan_ctrl(WLAN_POWER_ON); +#endif /* CONFIG_RTL8188E */ + + /* Pull up pwd pin, make wifi leave power down mode. */ + rtw_wifi_gpio_init(); + rtw_wifi_gpio_wlan_ctrl(WLAN_PWDN_ON); + +#if (MP_DRIVER == 1) && (defined(CONFIG_RTL8723A) || defined(CONFIG_RTL8723B)) + /* Pull up BT reset pin. */ + rtw_wifi_gpio_wlan_ctrl(WLAN_BT_PWDN_ON); +#endif + rtw_mdelay_os(5); + + sdhci_bus_scan(); +#ifdef CONFIG_RTL8723B + /* YJ,test,130305 */ + rtw_mdelay_os(1000); +#endif +#ifdef ANDROID_2X + rtw_mdelay_os(200); +#else /* !ANDROID_2X */ + if (1) { + int i = 0; + + for (i = 0; i <= 50; i++) { + msleep(10); + if (sdhci_device_attached()) + break; + printk("%s delay times:%d\n", __func__, i); + } + } +#endif /* !ANDROID_2X */ + + return ret; +} + +void platform_wifi_power_off(void) +{ + /* Pull down pwd pin, make wifi enter power down mode. */ + rtw_wifi_gpio_wlan_ctrl(WLAN_PWDN_OFF); + rtw_mdelay_os(5); + rtw_wifi_gpio_deinit(); + +#ifdef CONFIG_RTL8188E + rtw_wifi_gpio_wlan_ctrl(WLAN_POWER_OFF); +#endif /* CONFIG_RTL8188E */ + +#ifdef CONFIG_WOWLAN + if (mmc_host) + mmc_host->pm_flags &= ~MMC_PM_KEEP_POWER; +#endif /* CONFIG_WOWLAN */ +} diff --git a/platform/platform_zte_zx296716_sdio.c b/platform/platform_zte_zx296716_sdio.c new file mode 100644 index 0000000..472d24d --- /dev/null +++ b/platform/platform_zte_zx296716_sdio.c @@ -0,0 +1,53 @@ +/****************************************************************************** + * + * Copyright(c) 2016 - 2018 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#include /* pr_info(() */ +#include /* msleep() */ +#include "platform_zte_zx296716_sdio.h" /* sdio_reinit() and etc */ + + +/* + * Return: + * 0: power on successfully + * others: power on failed + */ +int platform_wifi_power_on(void) +{ + int ret = 0; + + pr_info("######%s: disable--1--\n", __func__); + extern_wifi_set_enable(0); + /*msleep(500);*/ /* add in function:extern_wifi_set_enable */ + pr_info("######%s: enable--2---\n", __func__); + extern_wifi_set_enable(1); + /*msleep(500);*/ + sdio_reinit(); + + return ret; +} + +void platform_wifi_power_off(void) +{ + int card_val; + + pr_info("######%s:\n", __func__); +#ifdef CONFIG_A16T03_BOARD + card_val = sdio_host_is_null(); + if (card_val) + remove_card(); +#endif /* CONFIG_A16T03_BOARD */ + extern_wifi_set_enable(0); + + /*msleep(500);*/ +} diff --git a/platform/platform_zte_zx296716_sdio.h b/platform/platform_zte_zx296716_sdio.h new file mode 100644 index 0000000..3a4fba1 --- /dev/null +++ b/platform/platform_zte_zx296716_sdio.h @@ -0,0 +1,25 @@ +/****************************************************************************** + * + * Copyright(c) 2016 - 2018 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#ifndef __PLATFORM_ZTE_ZX296716_SDIO_H__ +#define __PLATFORM_ZTE_ZX296716_SDIO_H__ + +extern void sdio_reinit(void); +extern void extern_wifi_set_enable(int val); +#ifdef CONFIG_A16T03_BOARD +extern int sdio_host_is_null(void); +extern void remove_card(void); +#endif /* CONFIG_A16T03_BOARD */ + +#endif /* __PLATFORM_ZTE_ZX296716_SDIO_H__ */