diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-11 08:27:49 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-11 08:27:49 +0000 |
commit | ace9429bb58fd418f0c81d4c2835699bddf6bde6 (patch) | |
tree | b2d64bc10158fdd5497876388cd68142ca374ed3 /drivers/staging/rtl8723bs/os_dep | |
parent | Initial commit. (diff) | |
download | linux-ace9429bb58fd418f0c81d4c2835699bddf6bde6.tar.xz linux-ace9429bb58fd418f0c81d4c2835699bddf6bde6.zip |
Adding upstream version 6.6.15.upstream/6.6.15
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'drivers/staging/rtl8723bs/os_dep')
-rw-r--r-- | drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c | 2842 | ||||
-rw-r--r-- | drivers/staging/rtl8723bs/os_dep/ioctl_linux.c | 1300 | ||||
-rw-r--r-- | drivers/staging/rtl8723bs/os_dep/mlme_linux.c | 180 | ||||
-rw-r--r-- | drivers/staging/rtl8723bs/os_dep/os_intfs.c | 1254 | ||||
-rw-r--r-- | drivers/staging/rtl8723bs/os_dep/osdep_service.c | 235 | ||||
-rw-r--r-- | drivers/staging/rtl8723bs/os_dep/recv_linux.c | 229 | ||||
-rw-r--r-- | drivers/staging/rtl8723bs/os_dep/sdio_intf.c | 506 | ||||
-rw-r--r-- | drivers/staging/rtl8723bs/os_dep/sdio_ops_linux.c | 497 | ||||
-rw-r--r-- | drivers/staging/rtl8723bs/os_dep/wifi_regd.c | 150 | ||||
-rw-r--r-- | drivers/staging/rtl8723bs/os_dep/xmit_linux.c | 226 |
10 files changed, 7419 insertions, 0 deletions
diff --git a/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c b/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c new file mode 100644 index 0000000000..af155fca39 --- /dev/null +++ b/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c @@ -0,0 +1,2842 @@ +// SPDX-License-Identifier: GPL-2.0 +/****************************************************************************** + * + * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved. + * + ******************************************************************************/ + +#include <linux/etherdevice.h> +#include <drv_types.h> +#include <rtw_debug.h> +#include <linux/jiffies.h> + +#include <rtw_wifi_regd.h> + +#define RTW_MAX_MGMT_TX_CNT (8) + +#define RTW_SCAN_IE_LEN_MAX 2304 +#define RTW_MAX_REMAIN_ON_CHANNEL_DURATION 5000 /* ms */ +#define RTW_MAX_NUM_PMKIDS 4 + +static const u32 rtw_cipher_suites[] = { + WLAN_CIPHER_SUITE_WEP40, + WLAN_CIPHER_SUITE_WEP104, + WLAN_CIPHER_SUITE_TKIP, + WLAN_CIPHER_SUITE_CCMP, + WLAN_CIPHER_SUITE_AES_CMAC, +}; + +#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 = 30, \ +} + +/* 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 __maybe_unused const struct wiphy_wowlan_support wowlan_stub = { + .flags = WIPHY_WOWLAN_ANY, + .n_patterns = 0, + .pattern_max_len = 0, + .pattern_min_len = 0, + .max_pkt_offset = 0, +}; + +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_g_rates (rtw_rates + 0) +#define RTW_G_RATES_NUM 12 + +#define RTW_2G_CHANNELS_NUM 14 + +static struct ieee80211_channel rtw_2ghz_channels[] = { + 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), +}; + +static void rtw_2g_channels_init(struct ieee80211_channel *channels) +{ + memcpy((void *)channels, (void *)rtw_2ghz_channels, + sizeof(struct ieee80211_channel) * RTW_2G_CHANNELS_NUM + ); +} + +static void rtw_2g_rates_init(struct ieee80211_rate *rates) +{ + memcpy(rates, rtw_g_rates, + sizeof(struct ieee80211_rate) * RTW_G_RATES_NUM + ); +} + +static struct ieee80211_supported_band *rtw_spt_band_alloc( + enum nl80211_band band + ) +{ + struct ieee80211_supported_band *spt_band = NULL; + int n_channels, n_bitrates; + + if (band == NL80211_BAND_2GHZ) { + n_channels = RTW_2G_CHANNELS_NUM; + n_bitrates = RTW_G_RATES_NUM; + } else { + goto exit; + } + + spt_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 = band; + spt_band->n_channels = n_channels; + spt_band->n_bitrates = n_bitrates; + + if (band == NL80211_BAND_2GHZ) { + rtw_2g_channels_init(spt_band->channels); + rtw_2g_rates_init(spt_band->bitrates); + } + + /* spt_band.ht_cap */ + +exit: + + return spt_band; +} + +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_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) + }, +}; + +static int rtw_ieee80211_channel_to_frequency(int chan, int band) +{ + if (band == NL80211_BAND_2GHZ) { + if (chan == 14) + return 2484; + else if (chan < 14) + return 2407 + chan * 5; + } + + return 0; /* not supported */ +} + +#define MAX_BSSINFO_LEN 1000 +struct cfg80211_bss *rtw_cfg80211_inform_bss(struct 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; + s32 notify_signal; + u8 *buf = NULL, *pbuf; + size_t len, bssinf_len = 0; + struct ieee80211_hdr *pwlanhdr; + __le16 *fctrl; + + struct wireless_dev *wdev = padapter->rtw_wdev; + struct wiphy *wiphy = wdev->wiphy; + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + + bssinf_len = pnetwork->network.ie_length + sizeof(struct ieee80211_hdr_3addr); + if (bssinf_len > MAX_BSSINFO_LEN) + goto exit; + + { + u16 wapi_len = 0; + + if (rtw_get_wapi_ie(pnetwork->network.ies, pnetwork->network.ie_length, NULL, &wapi_len) > 0) { + if (wapi_len > 0) + goto exit; + } + } + + /* To reduce PBC Overlap rate */ + /* spin_lock_bh(&pwdev_priv->scan_req_lock); */ + if (adapter_wdev_data(padapter)->scan_request) { + u8 *psr = NULL, sr = 0; + struct ndis_802_11_ssid *pssid = &pnetwork->network.ssid; + struct cfg80211_scan_request *request = adapter_wdev_data(padapter)->scan_request; + struct cfg80211_ssid *ssids = request->ssids; + u32 wpsielen = 0; + u8 *wpsie = NULL; + + wpsie = rtw_get_wps_ie(pnetwork->network.ies + _FIXED_IE_LENGTH_, pnetwork->network.ie_length - _FIXED_IE_LENGTH_, NULL, &wpsielen); + + if (wpsie && wpsielen > 0) + psr = rtw_get_wps_attr_content(wpsie, wpsielen, WPS_ATTR_SELECTED_REGISTRAR, (u8 *)(&sr), NULL); + + if (sr != 0) { + /* it means under processing WPS */ + if (request->n_ssids == 1 && request->n_channels == 1) { + if (ssids[0].ssid_len != 0 && + (pssid->ssid_length != ssids[0].ssid_len || + memcmp(pssid->ssid, ssids[0].ssid, ssids[0].ssid_len))) { + if (psr) + *psr = 0; /* clear sr */ + } + } + } + } + /* spin_unlock_bh(&pwdev_priv->scan_req_lock); */ + + channel = pnetwork->network.configuration.ds_config; + freq = rtw_ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ); + + notify_channel = ieee80211_get_channel(wiphy, freq); + + notify_timestamp = ktime_to_us(ktime_get_boottime()); + + /* We've set wiphy's signal_type as CFG80211_SIGNAL_TYPE_MBM: signal strength in mBm (100*dBm) */ + if (check_fwstate(pmlmepriv, _FW_LINKED) == 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.phy_info.signal_strength);/* dbm */ + } + + buf = kzalloc(MAX_BSSINFO_LEN, GFP_ATOMIC); + if (!buf) + goto exit; + pbuf = buf; + + pwlanhdr = (struct ieee80211_hdr *)pbuf; + fctrl = &(pwlanhdr->frame_control); + *(fctrl) = 0; + + SetSeqNum(pwlanhdr, 0/*pmlmeext->mgnt_seq*/); + /* pmlmeext->mgnt_seq++; */ + + if (pnetwork->network.reserved[0] == 1) { /* WIFI_BEACON */ + eth_broadcast_addr(pwlanhdr->addr1); + SetFrameSubType(pbuf, WIFI_BEACON); + } else { + memcpy(pwlanhdr->addr1, myid(&(padapter->eeprompriv)), ETH_ALEN); + SetFrameSubType(pbuf, WIFI_PROBERSP); + } + + memcpy(pwlanhdr->addr2, pnetwork->network.mac_address, ETH_ALEN); + memcpy(pwlanhdr->addr3, pnetwork->network.mac_address, ETH_ALEN); + + pbuf += sizeof(struct ieee80211_hdr_3addr); + len = sizeof(struct ieee80211_hdr_3addr); + + memcpy(pbuf, pnetwork->network.ies, pnetwork->network.ie_length); + len += pnetwork->network.ie_length; + + *((__le64 *)pbuf) = cpu_to_le64(notify_timestamp); + + bss = cfg80211_inform_bss_frame(wiphy, notify_channel, (struct ieee80211_mgmt *)buf, + len, notify_signal, GFP_ATOMIC); + + if (unlikely(!bss)) + goto exit; + + cfg80211_put_bss(wiphy, bss); + kfree(buf); + +exit: + 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(struct adapter *padapter) +{ + struct 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_ieee80211_channel_to_frequency(pnetwork->configuration.ds_config, NL80211_BAND_2GHZ); + + notify_channel = ieee80211_get_channel(padapter->rtw_wdev->wiphy, freq); + bss = cfg80211_get_bss(padapter->rtw_wdev->wiphy, notify_channel, + pnetwork->mac_address, pnetwork->ssid.ssid, + pnetwork->ssid.ssid_length, + IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY); + + cfg80211_put_bss(padapter->rtw_wdev->wiphy, bss); + + return (bss != NULL); +} + +void rtw_cfg80211_ibss_indicate_connect(struct adapter *padapter) +{ + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct wlan_network *cur_network = &(pmlmepriv->cur_network); + struct wireless_dev *pwdev = padapter->rtw_wdev; + struct wiphy *wiphy = pwdev->wiphy; + int freq = (int)cur_network->network.configuration.ds_config; + struct ieee80211_channel *chan; + + if (pwdev->iftype != NL80211_IFTYPE_ADHOC) + return; + + if (!rtw_cfg80211_check_bss(padapter)) { + struct 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) { + memcpy(&cur_network->network, pnetwork, sizeof(struct wlan_bssid_ex)); + rtw_cfg80211_inform_bss(padapter, cur_network); + } else { + if (!scanned) { + rtw_warn_on(1); + return; + } + if (!memcmp(&(scanned->network.ssid), &(pnetwork->ssid), sizeof(struct ndis_802_11_ssid)) + && !memcmp(scanned->network.mac_address, pnetwork->mac_address, sizeof(NDIS_802_11_MAC_ADDRESS)) + ) + rtw_cfg80211_inform_bss(padapter, scanned); + else + rtw_warn_on(1); + } + + if (!rtw_cfg80211_check_bss(padapter)) + netdev_dbg(padapter->pnetdev, + FUNC_ADPT_FMT " BSS not found !!\n", + FUNC_ADPT_ARG(padapter)); + } + /* notify cfg80211 that device joined an IBSS */ + chan = ieee80211_get_channel(wiphy, freq); + cfg80211_ibss_joined(padapter->pnetdev, cur_network->network.mac_address, chan, GFP_ATOMIC); +} + +void rtw_cfg80211_indicate_connect(struct adapter *padapter) +{ + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct wlan_network *cur_network = &(pmlmepriv->cur_network); + struct wireless_dev *pwdev = padapter->rtw_wdev; + + if (pwdev->iftype != NL80211_IFTYPE_STATION + && pwdev->iftype != NL80211_IFTYPE_P2P_CLIENT + ) { + return; + } + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) + return; + + { + struct wlan_bssid_ex *pnetwork = &(padapter->mlmeextpriv.mlmext_info.network); + struct wlan_network *scanned = pmlmepriv->cur_network_scanned; + + if (!scanned) { + rtw_warn_on(1); + goto check_bss; + } + + if (!memcmp(scanned->network.mac_address, pnetwork->mac_address, sizeof(NDIS_802_11_MAC_ADDRESS)) + && !memcmp(&(scanned->network.ssid), &(pnetwork->ssid), sizeof(struct ndis_802_11_ssid)) + ) + rtw_cfg80211_inform_bss(padapter, scanned); + else + rtw_warn_on(1); + } + +check_bss: + if (!rtw_cfg80211_check_bss(padapter)) + netdev_dbg(padapter->pnetdev, + FUNC_ADPT_FMT " BSS not found !!\n", + FUNC_ADPT_ARG(padapter)); + + if (rtw_to_roam(padapter) > 0) { + struct wiphy *wiphy = pwdev->wiphy; + struct ieee80211_channel *notify_channel; + u32 freq; + u16 channel = cur_network->network.configuration.ds_config; + struct cfg80211_roam_info roam_info = {}; + + freq = rtw_ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ); + + notify_channel = ieee80211_get_channel(wiphy, freq); + + roam_info.links[0].channel = notify_channel; + roam_info.links[0].bssid = cur_network->network.mac_address; + roam_info.req_ie = + pmlmepriv->assoc_req + sizeof(struct ieee80211_hdr_3addr) + 2; + roam_info.req_ie_len = + pmlmepriv->assoc_req_len - sizeof(struct ieee80211_hdr_3addr) - 2; + roam_info.resp_ie = + pmlmepriv->assoc_rsp + sizeof(struct ieee80211_hdr_3addr) + 6; + roam_info.resp_ie_len = + pmlmepriv->assoc_rsp_len - sizeof(struct ieee80211_hdr_3addr) - 6; + cfg80211_roamed(padapter->pnetdev, &roam_info, GFP_ATOMIC); + } else { + cfg80211_connect_result(padapter->pnetdev, cur_network->network.mac_address + , pmlmepriv->assoc_req + sizeof(struct ieee80211_hdr_3addr) + 2 + , pmlmepriv->assoc_req_len - sizeof(struct ieee80211_hdr_3addr) - 2 + , pmlmepriv->assoc_rsp + sizeof(struct ieee80211_hdr_3addr) + 6 + , pmlmepriv->assoc_rsp_len - sizeof(struct ieee80211_hdr_3addr) - 6 + , WLAN_STATUS_SUCCESS, GFP_ATOMIC); + } +} + +void rtw_cfg80211_indicate_disconnect(struct adapter *padapter) +{ + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct wireless_dev *pwdev = padapter->rtw_wdev; + + if (pwdev->iftype != NL80211_IFTYPE_STATION + && pwdev->iftype != NL80211_IFTYPE_P2P_CLIENT + ) { + return; + } + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) + return; + + if (!padapter->mlmepriv.not_indic_disco) { + if (check_fwstate(&padapter->mlmepriv, _FW_LINKED)) { + cfg80211_disconnected(padapter->pnetdev, 0, + NULL, 0, true, GFP_ATOMIC); + } else { + cfg80211_connect_result(padapter->pnetdev, NULL, NULL, 0, NULL, 0, + WLAN_STATUS_UNSPECIFIED_FAILURE, GFP_ATOMIC/*GFP_KERNEL*/); + } + } +} + +static int rtw_cfg80211_ap_set_encryption(struct net_device *dev, struct ieee_param *param, u32 param_len) +{ + int ret = 0; + u32 wep_key_idx, wep_key_len; + struct sta_info *psta = NULL, *pbcmc_sta = NULL; + struct adapter *padapter = rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct security_priv *psecuritypriv = &(padapter->securitypriv); + struct sta_priv *pstapriv = &padapter->stapriv; + char *grpkey = padapter->securitypriv.dot118021XGrpKey[param->u.crypt.idx].skey; + char *txkey = padapter->securitypriv.dot118021XGrptxmickey[param->u.crypt.idx].skey; + char *rxkey = padapter->securitypriv.dot118021XGrprxmickey[param->u.crypt.idx].skey; + + param->u.crypt.err = 0; + param->u.crypt.alg[IEEE_CRYPT_ALG_NAME_LEN - 1] = '\0'; + + 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) { + ret = -EINVAL; + goto exit; + } + } else { + psta = rtw_get_stainfo(pstapriv, param->sta_addr); + if (!psta) + /* ret = -EINVAL; */ + goto exit; + } + + if (strcmp(param->u.crypt.alg, "none") == 0 && !psta) + goto exit; + + if (strcmp(param->u.crypt.alg, "WEP") == 0 && !psta) { + 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 (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; + } + + 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; + } + + /* group key */ + if (!psta && check_fwstate(pmlmepriv, WIFI_AP_STATE)) { + /* group key */ + if (param->u.crypt.set_tx == 0) { + if (strcmp(param->u.crypt.alg, "WEP") == 0) { + memcpy(grpkey, 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) { + psecuritypriv->dot118021XGrpPrivacy = _TKIP_; + + memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + + /* DEBUG_ERR("set key length :param->u.crypt.key_len =%d\n", param->u.crypt.key_len); */ + /* set mic key */ + memcpy(txkey, &(param->u.crypt.key[16]), 8); + memcpy(rxkey, &(param->u.crypt.key[24]), 8); + + psecuritypriv->busetkipkey = true; + + } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) { + psecuritypriv->dot118021XGrpPrivacy = _AES_; + + memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + } else { + psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_; + } + + psecuritypriv->dot118021XGrpKeyid = param->u.crypt.idx; + + psecuritypriv->binstallGrpkey = true; + + psecuritypriv->dot11PrivacyAlgrthm = psecuritypriv->dot118021XGrpPrivacy;/* */ + + rtw_ap_set_group_key(padapter, param->u.crypt.key, psecuritypriv->dot118021XGrpPrivacy, param->u.crypt.idx); + + pbcmc_sta = rtw_get_bcmc_stainfo(padapter); + if (pbcmc_sta) { + pbcmc_sta->ieee8021x_blocked = false; + pbcmc_sta->dot118021XPrivacy = psecuritypriv->dot118021XGrpPrivacy;/* rx will use bmc_sta's dot118021XPrivacy */ + } + } + + 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) { /* pairwise key */ + 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) { + psta->dot118021XPrivacy = _WEP40_; + if (param->u.crypt.key_len == 13) + psta->dot118021XPrivacy = _WEP104_; + } else if (strcmp(param->u.crypt.alg, "TKIP") == 0) { + psta->dot118021XPrivacy = _TKIP_; + + /* DEBUG_ERR("set key length :param->u.crypt.key_len =%d\n", param->u.crypt.key_len); */ + /* set mic key */ + memcpy(psta->dot11tkiptxmickey.skey, &(param->u.crypt.key[16]), 8); + memcpy(psta->dot11tkiprxmickey.skey, &(param->u.crypt.key[24]), 8); + + psecuritypriv->busetkipkey = true; + + } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) { + psta->dot118021XPrivacy = _AES_; + } else { + psta->dot118021XPrivacy = _NO_PRIVACY_; + } + + rtw_ap_set_pairwise_key(padapter, psta); + + psta->ieee8021x_blocked = false; + + psta->bpairwise_key_installed = true; + + } else { /* group key??? */ + if (strcmp(param->u.crypt.alg, "WEP") == 0) { + memcpy(grpkey, 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) { + psecuritypriv->dot118021XGrpPrivacy = _TKIP_; + + memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + + /* DEBUG_ERR("set key length :param->u.crypt.key_len =%d\n", param->u.crypt.key_len); */ + /* set mic key */ + memcpy(txkey, &(param->u.crypt.key[16]), 8); + memcpy(rxkey, &(param->u.crypt.key[24]), 8); + + psecuritypriv->busetkipkey = true; + + } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) { + psecuritypriv->dot118021XGrpPrivacy = _AES_; + + memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + } else { + psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_; + } + + psecuritypriv->dot118021XGrpKeyid = param->u.crypt.idx; + + psecuritypriv->binstallGrpkey = true; + + psecuritypriv->dot11PrivacyAlgrthm = psecuritypriv->dot118021XGrpPrivacy;/* */ + + rtw_ap_set_group_key(padapter, param->u.crypt.key, psecuritypriv->dot118021XGrpPrivacy, param->u.crypt.idx); + + pbcmc_sta = rtw_get_bcmc_stainfo(padapter); + if (pbcmc_sta) { + pbcmc_sta->ieee8021x_blocked = false; + pbcmc_sta->dot118021XPrivacy = psecuritypriv->dot118021XGrpPrivacy;/* rx will use bmc_sta's dot118021XPrivacy */ + } + } + } + } + +exit: + + return ret; +} + +static int rtw_cfg80211_set_encryption(struct net_device *dev, struct ieee_param *param, u32 param_len) +{ + int ret = 0; + u8 max_idx; + u32 wep_key_idx, wep_key_len; + struct adapter *padapter = rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct security_priv *psecuritypriv = &padapter->securitypriv; + + 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) { + ret = -EINVAL; + goto exit; + } + + if (strcmp(param->u.crypt.alg, "WEP") == 0) + max_idx = WEP_KEYS - 1; + else + max_idx = BIP_MAX_KEYID; + + if (param->u.crypt.idx > max_idx) { + netdev_err(dev, "Error crypt.idx %d > %d\n", param->u.crypt.idx, max_idx); + ret = -EINVAL; + goto exit; + } + + if (strcmp(param->u.crypt.alg, "WEP") == 0) { + wep_key_idx = param->u.crypt.idx; + wep_key_len = param->u.crypt.key_len; + + if (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; + } + + 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; + + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_MP_STATE) == true) { /* sta mode */ + psta = rtw_get_stainfo(pstapriv, get_bssid(pmlmepriv)); + if (psta) { + /* 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 */ + + 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 */ + /* DEBUG_ERR(("\nset key length :param->u.crypt.key_len =%d\n", param->u.crypt.key_len)); */ + memcpy(psta->dot11tkiptxmickey.skey, &(param->u.crypt.key[16]), 8); + memcpy(psta->dot11tkiprxmickey.skey, &(param->u.crypt.key[24]), 8); + + padapter->securitypriv.busetkipkey = false; + /* _set_timer(&padapter->securitypriv.tkip_timer, 50); */ + } + + rtw_setstakey_cmd(padapter, psta, true, true); + } else { /* group key */ + if (strcmp(param->u.crypt.alg, "TKIP") == 0 || strcmp(param->u.crypt.alg, "CCMP") == 0) { + 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)); + memcpy(padapter->securitypriv.dot118021XGrptxmickey[param->u.crypt.idx].skey, &(param->u.crypt.key[16]), 8); + memcpy(padapter->securitypriv.dot118021XGrprxmickey[param->u.crypt.idx].skey, &(param->u.crypt.key[24]), 8); + padapter->securitypriv.binstallGrpkey = true; + + 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, "BIP") == 0) { + /* save the IGTK key, length 16 bytes */ + 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)); + /* + for (no = 0;no<16;no++) + printk(" %02x ", padapter->securitypriv.dot11wBIPKey[param->u.crypt.idx].skey[no]); + */ + padapter->securitypriv.dot11wBIPKeyid = param->u.crypt.idx; + padapter->securitypriv.binstallBIPkey = true; + } + } + } + + pbcmc_sta = rtw_get_bcmc_stainfo(padapter); + if (!pbcmc_sta) { + /* 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 */ + } + } + +exit: + + return ret; +} + +static int cfg80211_rtw_add_key(struct wiphy *wiphy, struct net_device *ndev, + int link_id, u8 key_index, bool pairwise, + const u8 *mac_addr, struct key_params *params) +{ + char *alg_name; + u32 param_len; + struct ieee_param *param = NULL; + int ret = 0; + struct adapter *padapter = rtw_netdev_priv(ndev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + + param_len = sizeof(struct ieee_param) + params->key_len; + param = rtw_malloc(param_len); + if (!param) + return -1; + + memset(param, 0, param_len); + + param->cmd = IEEE_CMD_SET_ENCRYPTION; + eth_broadcast_addr(param->sta_addr); + + 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 WLAN_CIPHER_SUITE_AES_CMAC: + alg_name = "BIP"; + break; + 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)) + 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) + memcpy(param->u.crypt.seq, (u8 *)params->seq, params->seq_len); + + if (params->key_len && params->key) { + param->u.crypt.key_len = params->key_len; + memcpy(param->u.crypt.key, (u8 *)params->key, params->key_len); + } + + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true) { + ret = rtw_cfg80211_set_encryption(ndev, param, param_len); + } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) { + if (mac_addr) + memcpy(param->sta_addr, (void *)mac_addr, ETH_ALEN); + + ret = rtw_cfg80211_ap_set_encryption(ndev, param, param_len); + } else if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true + || check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true) { + ret = rtw_cfg80211_set_encryption(ndev, param, param_len); + } + +addkey_end: + kfree(param); + + return ret; +} + +static int cfg80211_rtw_get_key(struct wiphy *wiphy, struct net_device *ndev, + int link_id, u8 key_index, bool pairwise, + const u8 *mac_addr, void *cookie, + void (*callback)(void *cookie, + struct key_params*)) +{ + return 0; +} + +static int cfg80211_rtw_del_key(struct wiphy *wiphy, struct net_device *ndev, + int link_id, u8 key_index, bool pairwise, + const u8 *mac_addr) +{ + struct adapter *padapter = rtw_netdev_priv(ndev); + struct security_priv *psecuritypriv = &padapter->securitypriv; + + 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, int link_id, + u8 key_index, bool unicast, + bool multicast) +{ + struct adapter *padapter = rtw_netdev_priv(ndev); + struct security_priv *psecuritypriv = &padapter->securitypriv; + + 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; +} + +static int cfg80211_rtw_get_station(struct wiphy *wiphy, + struct net_device *ndev, + const u8 *mac, + struct station_info *sinfo) +{ + int ret = 0; + struct adapter *padapter = rtw_netdev_priv(ndev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct sta_info *psta = NULL; + struct sta_priv *pstapriv = &padapter->stapriv; + + sinfo->filled = 0; + + if (!mac) { + ret = -ENOENT; + goto exit; + } + + psta = rtw_get_stainfo(pstapriv, (u8 *)mac); + if (!psta) { + ret = -ENOENT; + goto exit; + } + + /* for infra./P2PClient mode */ + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) + && check_fwstate(pmlmepriv, _FW_LINKED)) { + struct wlan_network *cur_network = &(pmlmepriv->cur_network); + + if (memcmp((u8 *)mac, cur_network->network.mac_address, ETH_ALEN)) { + ret = -ENOENT; + goto exit; + } + + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL); + sinfo->signal = translate_percentage_to_dbm(padapter->recvpriv.signal_strength); + + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE); + sinfo->txrate.legacy = rtw_get_cur_max_rate(padapter); + + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS); + sinfo->rx_packets = sta_rx_data_pkts(psta); + + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS); + sinfo->tx_packets = psta->sta_stats.tx_pkts; + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED); + } + + /* for Ad-Hoc/AP mode */ + if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) || + check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) || + check_fwstate(pmlmepriv, WIFI_AP_STATE)) && + check_fwstate(pmlmepriv, _FW_LINKED)) { + /* TODO: should acquire station info... */ + } + +exit: + return ret; +} + +static int cfg80211_rtw_change_iface(struct wiphy *wiphy, + struct net_device *ndev, + enum nl80211_iftype type, + struct vif_params *params) +{ + enum nl80211_iftype old_type; + enum ndis_802_11_network_infrastructure networkType; + struct adapter *padapter = rtw_netdev_priv(ndev); + struct wireless_dev *rtw_wdev = padapter->rtw_wdev; + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + int ret = 0; + + if (adapter_to_dvobj(padapter)->processing_dev_remove == true) { + ret = -EPERM; + goto exit; + } + + { + if (netdev_open(ndev) != 0) { + ret = -EPERM; + goto exit; + } + } + + if (rtw_pwr_wakeup(padapter) == _FAIL) { + ret = -EPERM; + goto exit; + } + + old_type = rtw_wdev->iftype; + + if (old_type != type) { + pmlmeext->action_public_rxseq = 0xffff; + pmlmeext->action_public_dialog_token = 0xff; + } + + switch (type) { + case NL80211_IFTYPE_ADHOC: + networkType = Ndis802_11IBSS; + break; + case NL80211_IFTYPE_STATION: + networkType = Ndis802_11Infrastructure; + break; + case NL80211_IFTYPE_AP: + networkType = Ndis802_11APMode; + break; + default: + ret = -EOPNOTSUPP; + goto exit; + } + + rtw_wdev->iftype = type; + + if (rtw_set_802_11_infrastructure_mode(padapter, networkType) == false) { + rtw_wdev->iftype = old_type; + ret = -EPERM; + goto exit; + } + + rtw_setopmode_cmd(padapter, networkType, true); + +exit: + + return ret; +} + +void rtw_cfg80211_indicate_scan_done(struct adapter *adapter, bool aborted) +{ + struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(adapter); + struct cfg80211_scan_info info = { + .aborted = aborted + }; + + spin_lock_bh(&pwdev_priv->scan_req_lock); + if (pwdev_priv->scan_request) { + /* avoid WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req); */ + if (pwdev_priv->scan_request->wiphy == pwdev_priv->rtw_wdev->wiphy) + cfg80211_scan_done(pwdev_priv->scan_request, &info); + + pwdev_priv->scan_request = NULL; + } + spin_unlock_bh(&pwdev_priv->scan_req_lock); +} + +void rtw_cfg80211_unlink_bss(struct adapter *padapter, struct wlan_network *pnetwork) +{ + struct wireless_dev *pwdev = padapter->rtw_wdev; + struct wiphy *wiphy = pwdev->wiphy; + struct cfg80211_bss *bss = NULL; + struct wlan_bssid_ex *select_network = &pnetwork->network; + + bss = cfg80211_get_bss(wiphy, NULL/*notify_channel*/, + select_network->mac_address, + select_network->ssid.ssid, + select_network->ssid.ssid_length, + IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY); + + if (bss) { + cfg80211_unlink_bss(wiphy, bss); + cfg80211_put_bss(padapter->rtw_wdev->wiphy, bss); + } +} + +void rtw_cfg80211_surveydone_event_callback(struct adapter *padapter) +{ + struct list_head *plist, *phead; + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct __queue *queue = &(pmlmepriv->scanned_queue); + struct wlan_network *pnetwork = NULL; + + spin_lock_bh(&(pmlmepriv->scanned_queue.lock)); + + phead = get_list_head(queue); + list_for_each(plist, phead) + { + pnetwork = list_entry(plist, struct wlan_network, list); + + /* report network only if the current channel set contains the channel to which this network belongs */ + if (rtw_ch_set_search_ch(padapter->mlmeextpriv.channel_set, pnetwork->network.configuration.ds_config) >= 0 + && true == rtw_validate_ssid(&(pnetwork->network.ssid))) { + /* ev =translate_scan(padapter, a, pnetwork, ev, stop); */ + rtw_cfg80211_inform_bss(padapter, pnetwork); + } + } + + spin_unlock_bh(&(pmlmepriv->scanned_queue.lock)); +} + +static int rtw_cfg80211_set_probe_req_wpsp2pie(struct adapter *padapter, char *buf, int len) +{ + int ret = 0; + uint wps_ielen = 0; + u8 *wps_ie; + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + + if (len > 0) { + wps_ie = rtw_get_wps_ie(buf, len, NULL, &wps_ielen); + if (wps_ie) { + if (pmlmepriv->wps_probe_req_ie) { + pmlmepriv->wps_probe_req_ie_len = 0; + kfree(pmlmepriv->wps_probe_req_ie); + pmlmepriv->wps_probe_req_ie = NULL; + } + + pmlmepriv->wps_probe_req_ie = rtw_malloc(wps_ielen); + if (!pmlmepriv->wps_probe_req_ie) + return -EINVAL; + + memcpy(pmlmepriv->wps_probe_req_ie, wps_ie, wps_ielen); + pmlmepriv->wps_probe_req_ie_len = wps_ielen; + } + } + + return ret; +} + +static int cfg80211_rtw_scan(struct wiphy *wiphy + , struct cfg80211_scan_request *request) +{ + struct net_device *ndev = wdev_to_ndev(request->wdev); + int i; + u8 _status = false; + int ret = 0; + struct ndis_802_11_ssid *ssid = NULL; + struct rtw_ieee80211_channel ch[RTW_CHANNEL_SCAN_AMOUNT]; + u8 survey_times = 3; + u8 survey_times_for_one_ch = 6; + struct cfg80211_ssid *ssids = request->ssids; + int j = 0; + bool need_indicate_scan_done = false; + + struct adapter *padapter; + struct rtw_wdev_priv *pwdev_priv; + struct mlme_priv *pmlmepriv; + + if (!ndev) { + ret = -EINVAL; + goto exit; + } + + padapter = rtw_netdev_priv(ndev); + pwdev_priv = adapter_wdev_data(padapter); + pmlmepriv = &padapter->mlmepriv; +/* endif */ + + spin_lock_bh(&pwdev_priv->scan_req_lock); + pwdev_priv->scan_request = request; + spin_unlock_bh(&pwdev_priv->scan_req_lock); + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) { + if (check_fwstate(pmlmepriv, WIFI_UNDER_WPS | _FW_UNDER_SURVEY | _FW_UNDER_LINKING) == true) { + need_indicate_scan_done = true; + goto check_need_indicate_scan_done; + } + } + + rtw_ps_deny(padapter, PS_DENY_SCAN); + if (rtw_pwr_wakeup(padapter) == _FAIL) { + need_indicate_scan_done = true; + goto check_need_indicate_scan_done; + } + + if (request->ie && request->ie_len > 0) + rtw_cfg80211_set_probe_req_wpsp2pie(padapter, (u8 *)request->ie, request->ie_len); + + if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY) == true) { + need_indicate_scan_done = true; + goto check_need_indicate_scan_done; + } else if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING) == true) { + ret = -EBUSY; + goto check_need_indicate_scan_done; + } + + if (pmlmepriv->LinkDetectInfo.bBusyTraffic == true) { + static unsigned long lastscantime; + unsigned long passtime; + + passtime = jiffies_to_msecs(jiffies - lastscantime); + lastscantime = jiffies; + if (passtime > 12000) { + need_indicate_scan_done = true; + goto check_need_indicate_scan_done; + } + } + + if (rtw_is_scan_deny(padapter)) { + need_indicate_scan_done = true; + goto check_need_indicate_scan_done; + } + + ssid = kzalloc(RTW_SSID_SCAN_AMOUNT * sizeof(struct ndis_802_11_ssid), + GFP_KERNEL); + if (!ssid) { + ret = -ENOMEM; + goto check_need_indicate_scan_done; + } + + /* parsing request ssids, n_ssids */ + for (i = 0; i < request->n_ssids && i < RTW_SSID_SCAN_AMOUNT; i++) { + memcpy(ssid[i].ssid, ssids[i].ssid, ssids[i].ssid_len); + ssid[i].ssid_length = ssids[i].ssid_len; + } + + /* parsing channels, n_channels */ + memset(ch, 0, sizeof(struct rtw_ieee80211_channel) * RTW_CHANNEL_SCAN_AMOUNT); + for (i = 0; i < request->n_channels && i < RTW_CHANNEL_SCAN_AMOUNT; i++) { + ch[i].hw_value = request->channels[i]->hw_value; + ch[i].flags = request->channels[i]->flags; + } + + spin_lock_bh(&pmlmepriv->lock); + if (request->n_channels == 1) { + for (i = 1; i < survey_times_for_one_ch; i++) + memcpy(&ch[i], &ch[0], sizeof(struct rtw_ieee80211_channel)); + _status = rtw_sitesurvey_cmd(padapter, ssid, RTW_SSID_SCAN_AMOUNT, ch, 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++) + memcpy(&ch[j * survey_times + i], &ch[j], sizeof(struct rtw_ieee80211_channel)); + _status = rtw_sitesurvey_cmd(padapter, ssid, RTW_SSID_SCAN_AMOUNT, ch, survey_times * request->n_channels); + } else { + _status = rtw_sitesurvey_cmd(padapter, ssid, RTW_SSID_SCAN_AMOUNT, NULL, 0); + } + spin_unlock_bh(&pmlmepriv->lock); + + if (_status == false) + ret = -1; + +check_need_indicate_scan_done: + kfree(ssid); + if (need_indicate_scan_done) { + rtw_cfg80211_surveydone_event_callback(padapter); + rtw_cfg80211_indicate_scan_done(padapter, false); + } + + rtw_ps_deny_cancel(padapter, PS_DENY_SCAN); + +exit: + return ret; +} + +static int cfg80211_rtw_set_wiphy_params(struct wiphy *wiphy, u32 changed) +{ + return 0; +} + +static int rtw_cfg80211_set_wpa_version(struct security_priv *psecuritypriv, u32 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; + + return 0; +} + +static int rtw_cfg80211_set_auth_type(struct security_priv *psecuritypriv, + enum nl80211_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; + + 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; + + 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; + 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; + default: + 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) +{ + if (key_mgt == WLAN_AKM_SUITE_8021X) + /* auth_type = UMAC_AUTH_TYPE_8021X; */ + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X; + else if (key_mgt == WLAN_AKM_SUITE_PSK) { + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X; + } + + return 0; +} + +static int rtw_cfg80211_set_wpa_ie(struct adapter *padapter, u8 *pie, size_t ielen) +{ + u8 *buf = NULL; + int group_cipher = 0, pairwise_cipher = 0; + 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 || !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) { + ret = -ENOMEM; + goto exit; + } + + memcpy(buf, pie, ielen); + + 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; + memcpy(padapter->securitypriv.supplicant_ie, &pwpa[0], wpa_ielen + 2); + } + } + + 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) == _SUCCESS) { + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_8021X; + padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeWPA2PSK; + memcpy(padapter->securitypriv.supplicant_ie, &pwpa2[0], wpa2_ielen + 2); + } + } + + 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; + } + + {/* 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) { + padapter->securitypriv.wps_ie_len = min_t(uint, wps_ielen, MAX_WPS_IE_LEN); + 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); + } + } + + /* 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: + kfree(buf); + 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) +{ + struct adapter *padapter = rtw_netdev_priv(ndev); + struct ndis_802_11_ssid ndis_ssid; + struct security_priv *psecuritypriv = &padapter->securitypriv; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + int ret = 0; + + if (rtw_pwr_wakeup(padapter) == _FAIL) { + ret = -EPERM; + goto exit; + } + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) { + ret = -EPERM; + goto exit; + } + + if (!params->ssid || !params->ssid_len) { + ret = -EINVAL; + goto exit; + } + + if (params->ssid_len > IW_ESSID_MAX_SIZE) { + ret = -E2BIG; + goto exit; + } + + memset(&ndis_ssid, 0, sizeof(struct ndis_802_11_ssid)); + ndis_ssid.ssid_length = params->ssid_len; + memcpy(ndis_ssid.ssid, (u8 *)params->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); + + if (rtw_set_802_11_ssid(padapter, &ndis_ssid) == false) { + ret = -1; + goto exit; + } + +exit: + return ret; +} + +static int cfg80211_rtw_leave_ibss(struct wiphy *wiphy, struct net_device *ndev) +{ + struct adapter *padapter = rtw_netdev_priv(ndev); + struct wireless_dev *rtw_wdev = padapter->rtw_wdev; + enum nl80211_iftype old_type; + int ret = 0; + + old_type = rtw_wdev->iftype; + + rtw_set_to_roam(padapter, 0); + + if (check_fwstate(&padapter->mlmepriv, _FW_LINKED)) { + rtw_scan_abort(padapter); + LeaveAllPowerSaveMode(padapter); + + rtw_wdev->iftype = NL80211_IFTYPE_STATION; + + if (rtw_set_802_11_infrastructure_mode(padapter, Ndis802_11Infrastructure) == false) { + rtw_wdev->iftype = old_type; + ret = -EPERM; + goto leave_ibss; + } + rtw_setopmode_cmd(padapter, Ndis802_11Infrastructure, true); + } + +leave_ibss: + return ret; +} + +static int cfg80211_rtw_connect(struct wiphy *wiphy, struct net_device *ndev, + struct cfg80211_connect_params *sme) +{ + int ret = 0; + enum ndis_802_11_authentication_mode authmode; + struct ndis_802_11_ssid ndis_ssid; + struct adapter *padapter = rtw_netdev_priv(ndev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct security_priv *psecuritypriv = &padapter->securitypriv; + + padapter->mlmepriv.not_indic_disco = true; + + if (adapter_wdev_data(padapter)->block == true) { + ret = -EBUSY; + goto exit; + } + + rtw_ps_deny(padapter, PS_DENY_JOIN); + if (rtw_pwr_wakeup(padapter) == _FAIL) { + ret = -EPERM; + goto exit; + } + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) { + ret = -EPERM; + goto exit; + } + + if (!sme->ssid || !sme->ssid_len) { + ret = -EINVAL; + goto exit; + } + + if (sme->ssid_len > IW_ESSID_MAX_SIZE) { + ret = -E2BIG; + goto exit; + } + + memset(&ndis_ssid, 0, sizeof(struct ndis_802_11_ssid)); + ndis_ssid.ssid_length = sme->ssid_len; + memcpy(ndis_ssid.ssid, (u8 *)sme->ssid, sme->ssid_len); + + if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING) == true) { + ret = -EBUSY; + goto exit; + } + if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY) == true) + rtw_scan_abort(padapter); + + 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_wpa_version(psecuritypriv, sme->crypto.wpa_versions); + if (ret < 0) + goto exit; + + ret = rtw_cfg80211_set_auth_type(psecuritypriv, sme->auth_type); + + if (ret < 0) + goto exit; + + ret = rtw_cfg80211_set_wpa_ie(padapter, (u8 *)sme->ie, sme->ie_len); + if (ret < 0) + goto exit; + + if (sme->crypto.n_ciphers_pairwise) { + ret = rtw_cfg80211_set_cipher(psecuritypriv, sme->crypto.ciphers_pairwise[0], true); + if (ret < 0) + goto exit; + } + + /* For WEP Shared auth */ + if ((psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_Shared || + psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_Auto) && sme->key) { + u32 wep_key_idx, wep_key_len, wep_total_len; + struct ndis_802_11_wep *pwep = NULL; + + wep_key_idx = sme->key_idx; + wep_key_len = sme->key_len; + + if (sme->key_idx > WEP_KEYS) { + 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(struct ndis_802_11_wep, key_material); + pwep = rtw_malloc(wep_total_len); + if (!pwep) { + ret = -ENOMEM; + goto exit; + } + + memset(pwep, 0, wep_total_len); + + pwep->key_length = 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 exit; + } + + pwep->key_index = wep_key_idx; + pwep->key_index |= 0x80000000; + + memcpy(pwep->key_material, (void *)sme->key, pwep->key_length); + + if (rtw_set_802_11_add_wep(padapter, pwep) == (u8)_FAIL) + ret = -EOPNOTSUPP; + + kfree(pwep); + + if (ret < 0) + goto exit; + } + + 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 exit; + } + + authmode = psecuritypriv->ndisauthtype; + rtw_set_802_11_authentication_mode(padapter, authmode); + + /* rtw_set_802_11_encryption_mode(padapter, padapter->securitypriv.ndisencryptstatus); */ + + if (rtw_set_802_11_connect(padapter, (u8 *)sme->bssid, &ndis_ssid) == false) { + ret = -1; + goto exit; + } + +exit: + + rtw_ps_deny_cancel(padapter, PS_DENY_JOIN); + + padapter->mlmepriv.not_indic_disco = false; + + return ret; +} + +static int cfg80211_rtw_disconnect(struct wiphy *wiphy, struct net_device *ndev, + u16 reason_code) +{ + struct adapter *padapter = rtw_netdev_priv(ndev); + + rtw_set_to_roam(padapter, 0); + + rtw_scan_abort(padapter); + LeaveAllPowerSaveMode(padapter); + rtw_disassoc_cmd(padapter, 500, false); + + rtw_indicate_disconnect(padapter); + + rtw_free_assoc_resources(padapter, 1); + rtw_pwr_wakeup(padapter); + + return 0; +} + +static int cfg80211_rtw_set_txpower(struct wiphy *wiphy, + struct wireless_dev *wdev, + enum nl80211_tx_power_setting type, int mbm) +{ + return 0; +} + +static int cfg80211_rtw_get_txpower(struct wiphy *wiphy, + struct wireless_dev *wdev, int *dbm) +{ + *dbm = (12); + + return 0; +} + +inline bool rtw_cfg80211_pwr_mgmt(struct adapter *adapter) +{ + struct rtw_wdev_priv *rtw_wdev_priv = adapter_wdev_data(adapter); + + return rtw_wdev_priv->power_mgmt; +} + +static int cfg80211_rtw_set_power_mgmt(struct wiphy *wiphy, + struct net_device *ndev, + bool enabled, int timeout) +{ + struct adapter *padapter = rtw_netdev_priv(ndev); + struct rtw_wdev_priv *rtw_wdev_priv = adapter_wdev_data(padapter); + + rtw_wdev_priv->power_mgmt = enabled; + + if (!enabled) + LPS_Leave(padapter, "CFG80211_PWRMGMT"); + + return 0; +} + +static int cfg80211_rtw_set_pmksa(struct wiphy *wiphy, + struct net_device *ndev, + struct cfg80211_pmksa *pmksa) +{ + u8 index, blInserted = false; + struct adapter *padapter = rtw_netdev_priv(ndev); + struct security_priv *psecuritypriv = &padapter->securitypriv; + + if (is_zero_ether_addr((u8 *)pmksa->bssid)) + return -EINVAL; + + blInserted = false; + + /* overwrite PMKID */ + for (index = 0 ; index < NUM_PMKID_CACHE; index++) { + if (!memcmp(psecuritypriv->PMKIDList[index].Bssid, (u8 *)pmksa->bssid, ETH_ALEN)) { + memcpy(psecuritypriv->PMKIDList[index].PMKID, (u8 *)pmksa->pmkid, WLAN_PMKID_LEN); + psecuritypriv->PMKIDList[index].bUsed = true; + psecuritypriv->PMKIDIndex = index + 1; + blInserted = true; + break; + } + } + + if (!blInserted) { + memcpy(psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].Bssid, (u8 *)pmksa->bssid, ETH_ALEN); + memcpy(psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].PMKID, (u8 *)pmksa->pmkid, WLAN_PMKID_LEN); + + psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].bUsed = true; + psecuritypriv->PMKIDIndex++; + if (psecuritypriv->PMKIDIndex == 16) + psecuritypriv->PMKIDIndex = 0; + } + + return 0; +} + +static int cfg80211_rtw_del_pmksa(struct wiphy *wiphy, + struct net_device *ndev, + struct cfg80211_pmksa *pmksa) +{ + u8 index, bMatched = false; + struct adapter *padapter = rtw_netdev_priv(ndev); + struct security_priv *psecuritypriv = &padapter->securitypriv; + + for (index = 0 ; index < NUM_PMKID_CACHE; index++) { + if (!memcmp(psecuritypriv->PMKIDList[index].Bssid, (u8 *)pmksa->bssid, ETH_ALEN)) { + /* + * BSSID is matched, the same AP => Remove this PMKID information + * and reset it. + */ + eth_zero_addr(psecuritypriv->PMKIDList[index].Bssid); + memset(psecuritypriv->PMKIDList[index].PMKID, 0x00, WLAN_PMKID_LEN); + psecuritypriv->PMKIDList[index].bUsed = false; + bMatched = true; + break; + } + } + + if (!bMatched) + return -EINVAL; + + return 0; +} + +static int cfg80211_rtw_flush_pmksa(struct wiphy *wiphy, + struct net_device *ndev) +{ + struct adapter *padapter = rtw_netdev_priv(ndev); + struct security_priv *psecuritypriv = &padapter->securitypriv; + + memset(&psecuritypriv->PMKIDList[0], 0x00, sizeof(struct rt_pmkid_list) * NUM_PMKID_CACHE); + psecuritypriv->PMKIDIndex = 0; + + return 0; +} + +void rtw_cfg80211_indicate_sta_assoc(struct adapter *padapter, u8 *pmgmt_frame, uint frame_len) +{ + struct net_device *ndev = padapter->pnetdev; + + { + struct station_info sinfo = {}; + u8 ie_offset; + + if (GetFrameSubType(pmgmt_frame) == WIFI_ASSOCREQ) + ie_offset = _ASOCREQ_IE_OFFSET_; + else /* WIFI_REASSOCREQ */ + ie_offset = _REASOCREQ_IE_OFFSET_; + + sinfo.filled = 0; + 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; + cfg80211_new_sta(ndev, GetAddr2Ptr(pmgmt_frame), &sinfo, GFP_ATOMIC); + } +} + +void rtw_cfg80211_indicate_sta_disassoc(struct adapter *padapter, unsigned char *da, unsigned short reason) +{ + struct net_device *ndev = padapter->pnetdev; + + cfg80211_del_sta(ndev, da, GFP_ATOMIC); +} + +static u8 rtw_get_chan_type(struct adapter *adapter) +{ + struct mlme_ext_priv *mlme_ext = &adapter->mlmeextpriv; + + switch (mlme_ext->cur_bwmode) { + case CHANNEL_WIDTH_20: + if (is_supported_ht(adapter->registrypriv.wireless_mode)) + return NL80211_CHAN_HT20; + else + return NL80211_CHAN_NO_HT; + case CHANNEL_WIDTH_40: + if (mlme_ext->cur_ch_offset == HAL_PRIME_CHNL_OFFSET_UPPER) + return NL80211_CHAN_HT40PLUS; + else + return NL80211_CHAN_HT40MINUS; + default: + return NL80211_CHAN_HT20; + } + + return NL80211_CHAN_HT20; +} + +static int cfg80211_rtw_get_channel(struct wiphy *wiphy, struct wireless_dev *wdev, + unsigned int link_id, + struct cfg80211_chan_def *chandef) +{ + struct adapter *adapter = wiphy_to_adapter(wiphy); + struct registry_priv *registrypriv = &adapter->registrypriv; + enum nl80211_channel_type chan_type; + struct ieee80211_channel *chan = NULL; + int channel; + int freq; + + if (!adapter->rtw_wdev) + return -ENODEV; + + channel = rtw_get_oper_ch(adapter); + if (!channel) + return -ENODATA; + + freq = rtw_ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ); + + chan = ieee80211_get_channel(adapter->rtw_wdev->wiphy, freq); + + if (registrypriv->ht_enable) { + chan_type = rtw_get_chan_type(adapter); + cfg80211_chandef_create(chandef, chan, chan_type); + } else { + cfg80211_chandef_create(chandef, chan, NL80211_CHAN_NO_HT); + } + + return 0; +} + +static netdev_tx_t rtw_cfg80211_monitor_if_xmit_entry(struct sk_buff *skb, struct net_device *ndev) +{ + int rtap_len; + int qos_len = 0; + int dot11_hdr_len = 24; + int snap_len = 6; + unsigned char *pdata; + u16 frame_control; + unsigned char src_mac_addr[6]; + unsigned char dst_mac_addr[6]; + struct ieee80211_hdr *dot11_hdr; + struct ieee80211_radiotap_header *rtap_hdr; + struct adapter *padapter = rtw_netdev_priv(ndev); + + if (!skb) + goto fail; + + 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) + goto fail; + + /* Skip the ratio tap header */ + skb_pull(skb, rtap_len); + + dot11_hdr = (struct ieee80211_hdr *)skb->data; + frame_control = le16_to_cpu(dot11_hdr->frame_control); + /* Check if the QoS bit is set */ + if ((frame_control & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) { + /* Check if this ia a Wireless Distribution System (WDS) frame + * which has 4 MAC addresses + */ + if (frame_control & 0x0080) + qos_len = 2; + if ((frame_control & 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 + * 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)); + + /* Use the real net device to transmit the packet */ + _rtw_xmit_entry(skb, padapter->pnetdev); + return NETDEV_TX_OK; + + } else if ((frame_control & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) == + (IEEE80211_FTYPE_MGMT | 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 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; + + if (rtw_action_frame_parse(buf, len, &category, &action) == false) + goto fail; + + /* starting alloc mgmt frame to dump it */ + pmgntframe = alloc_mgtxmitframe(pxmitpriv); + if (!pmgntframe) + goto fail; + + /* update attribute */ + pattrib = &pmgntframe->attrib; + update_mgntframe_attrib(padapter, pattrib); + pattrib->retry_ctrl = false; + + memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET); + + pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET; + + memcpy(pframe, (void *)buf, len); + pattrib->pktlen = len; + + pwlanhdr = (struct 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); + } + +fail: + + dev_kfree_skb_any(skb); + + return NETDEV_TX_OK; +} + +static const struct net_device_ops rtw_cfg80211_monitor_if_ops = { + .ndo_start_xmit = rtw_cfg80211_monitor_if_xmit_entry, +}; + +static int rtw_cfg80211_add_monitor_if(struct 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) { + ret = -EINVAL; + goto out; + } + + if (pwdev_priv->pmon_ndev) { + ret = -EBUSY; + goto out; + } + + mon_ndev = alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator)); + if (!mon_ndev) { + ret = -ENOMEM; + goto out; + } + + mon_ndev->type = ARPHRD_IEEE80211_RADIOTAP; + strncpy(mon_ndev->name, name, IFNAMSIZ); + mon_ndev->name[IFNAMSIZ - 1] = 0; + mon_ndev->needs_free_netdev = true; + mon_ndev->priv_destructor = rtw_ndev_destructor; + + mon_ndev->netdev_ops = &rtw_cfg80211_monitor_if_ops; + + pnpi = netdev_priv(mon_ndev); + pnpi->priv = padapter; + pnpi->sizeof_priv = sizeof(struct adapter); + + /* wdev */ + mon_wdev = rtw_zmalloc(sizeof(struct wireless_dev)); + if (!mon_wdev) { + 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 = cfg80211_register_netdevice(mon_ndev); + if (ret) + goto out; + + *ndev = pwdev_priv->pmon_ndev = mon_ndev; + memcpy(pwdev_priv->ifname_mon, name, IFNAMSIZ + 1); + +out: + if (ret && mon_wdev) { + kfree(mon_wdev); + mon_wdev = NULL; + } + + if (ret && mon_ndev) { + free_netdev(mon_ndev); + *ndev = mon_ndev = NULL; + } + + return ret; +} + +static struct wireless_dev * + cfg80211_rtw_add_virtual_intf( + struct wiphy *wiphy, + const char *name, + unsigned char name_assign_type, + enum nl80211_iftype type, struct vif_params *params) +{ + int ret = 0; + struct net_device *ndev = NULL; + struct adapter *padapter = wiphy_to_adapter(wiphy); + + switch (type) { + case NL80211_IFTYPE_ADHOC: + case NL80211_IFTYPE_AP_VLAN: + case NL80211_IFTYPE_WDS: + case NL80211_IFTYPE_MESH_POINT: + ret = -ENODEV; + break; + case NL80211_IFTYPE_MONITOR: + ret = rtw_cfg80211_add_monitor_if(padapter, (char *)name, &ndev); + break; + case NL80211_IFTYPE_P2P_CLIENT: + case NL80211_IFTYPE_STATION: + ret = -ENODEV; + break; + case NL80211_IFTYPE_P2P_GO: + case NL80211_IFTYPE_AP: + ret = -ENODEV; + break; + default: + ret = -ENODEV; + break; + } + + return ndev ? ndev->ieee80211_ptr : ERR_PTR(ret); +} + +static int cfg80211_rtw_del_virtual_intf(struct wiphy *wiphy, + struct wireless_dev *wdev +) +{ + struct net_device *ndev = wdev_to_ndev(wdev); + int ret = 0; + struct adapter *adapter; + struct rtw_wdev_priv *pwdev_priv; + + if (!ndev) { + ret = -EINVAL; + goto exit; + } + + adapter = rtw_netdev_priv(ndev); + pwdev_priv = adapter_wdev_data(adapter); + + cfg80211_unregister_netdevice(ndev); + + if (ndev == pwdev_priv->pmon_ndev) { + pwdev_priv->pmon_ndev = NULL; + pwdev_priv->ifname_mon[0] = '\0'; + } + +exit: + return ret; +} + +static int rtw_add_beacon(struct 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; + struct mlme_priv *pmlmepriv = &(adapter->mlmepriv); + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) != true) + return -EINVAL; + + if (head_len < 24) + return -EINVAL; + + pbuf = rtw_zmalloc(head_len + tail_len); + if (!pbuf) + return -ENOMEM; + + memcpy(pbuf, (void *)head + 24, head_len - 24);/* 24 =beacon header len. */ + memcpy(pbuf + head_len - 24, (void *)tail, tail_len); + + len = head_len + tail_len - 24; + + /* check wps ie if inclued */ + rtw_get_wps_ie(pbuf + _FIXED_IE_LENGTH_, len - _FIXED_IE_LENGTH_, NULL, &wps_ielen); + + /* pbss_network->ies will not include p2p_ie, wfd ie */ + rtw_ies_remove_ie(pbuf, &len, _BEACON_IE_OFFSET_, WLAN_EID_VENDOR_SPECIFIC, P2P_OUI, 4); + rtw_ies_remove_ie(pbuf, &len, _BEACON_IE_OFFSET_, WLAN_EID_VENDOR_SPECIFIC, WFD_OUI, 4); + + if (rtw_check_beacon_data(adapter, pbuf, len) == _SUCCESS) + ret = 0; + else + ret = -EINVAL; + + kfree(pbuf); + + return ret; +} + +static int cfg80211_rtw_start_ap(struct wiphy *wiphy, struct net_device *ndev, + struct cfg80211_ap_settings *settings) +{ + int ret = 0; + struct adapter *adapter = rtw_netdev_priv(ndev); + + ret = rtw_add_beacon(adapter, settings->beacon.head, + settings->beacon.head_len, settings->beacon.tail, + settings->beacon.tail_len); + + adapter->mlmeextpriv.mlmext_info.hidden_ssid_mode = settings->hidden_ssid; + + if (settings->ssid && settings->ssid_len) { + struct wlan_bssid_ex *pbss_network = &adapter->mlmepriv.cur_network.network; + struct wlan_bssid_ex *pbss_network_ext = &adapter->mlmeextpriv.mlmext_info.network; + + memcpy(pbss_network->ssid.ssid, (void *)settings->ssid, settings->ssid_len); + pbss_network->ssid.ssid_length = settings->ssid_len; + memcpy(pbss_network_ext->ssid.ssid, (void *)settings->ssid, settings->ssid_len); + pbss_network_ext->ssid.ssid_length = settings->ssid_len; + } + + return ret; +} + +static int cfg80211_rtw_change_beacon(struct wiphy *wiphy, + struct net_device *ndev, + struct cfg80211_beacon_data *info) +{ + struct adapter *adapter = rtw_netdev_priv(ndev); + + return rtw_add_beacon(adapter, info->head, info->head_len, info->tail, info->tail_len); +} + +static int cfg80211_rtw_stop_ap(struct wiphy *wiphy, struct net_device *ndev, + unsigned int link_id) +{ + return 0; +} + +static int cfg80211_rtw_add_station(struct wiphy *wiphy, + struct net_device *ndev, + const u8 *mac, + struct station_parameters *params) +{ + return 0; +} + +static int cfg80211_rtw_del_station(struct wiphy *wiphy, struct net_device *ndev, + struct station_del_parameters *params) +{ + int ret = 0; + struct list_head *phead, *plist, *tmp; + u8 updated = false; + struct sta_info *psta = NULL; + struct adapter *padapter = rtw_netdev_priv(ndev); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct sta_priv *pstapriv = &padapter->stapriv; + const u8 *mac = params->mac; + + if (check_fwstate(pmlmepriv, (_FW_LINKED | WIFI_AP_STATE)) != true) + return -EINVAL; + + if (!mac) { + flush_all_cam_entry(padapter); /* clear CAM */ + + rtw_sta_flush(padapter); + + return 0; + } + + if (mac[0] == 0xff && mac[1] == 0xff && + mac[2] == 0xff && mac[3] == 0xff && + mac[4] == 0xff && mac[5] == 0xff) { + return -EINVAL; + } + + spin_lock_bh(&pstapriv->asoc_list_lock); + + phead = &pstapriv->asoc_list; + /* check asoc_queue */ + list_for_each_safe(plist, tmp, phead) { + psta = list_entry(plist, struct sta_info, asoc_list); + + if (!memcmp((u8 *)mac, psta->hwaddr, ETH_ALEN)) { + if (psta->dot8021xalg != 1 || psta->bpairwise_key_installed) { + list_del_init(&psta->asoc_list); + pstapriv->asoc_list_cnt--; + + updated = ap_free_sta(padapter, psta, true, WLAN_REASON_DEAUTH_LEAVING); + + psta = NULL; + + break; + } + } + } + + spin_unlock_bh(&pstapriv->asoc_list_lock); + + associated_clients_update(padapter, updated); + + return ret; +} + +static int cfg80211_rtw_change_station(struct wiphy *wiphy, + struct net_device *ndev, + const u8 *mac, + struct station_parameters *params) +{ + return 0; +} + +static struct sta_info *rtw_sta_info_get_by_idx(const int idx, struct sta_priv *pstapriv) + +{ + struct list_head *phead, *plist; + struct sta_info *psta = NULL; + int i = 0; + + phead = &pstapriv->asoc_list; + plist = get_next(phead); + + /* check asoc_queue */ + while (phead != plist) { + if (idx == i) + psta = container_of(plist, struct sta_info, asoc_list); + plist = get_next(plist); + i++; + } + return psta; +} + +static int cfg80211_rtw_dump_station(struct wiphy *wiphy, + struct net_device *ndev, + int idx, u8 *mac, + struct station_info *sinfo) +{ + int ret = 0; + struct adapter *padapter = rtw_netdev_priv(ndev); + struct sta_info *psta = NULL; + struct sta_priv *pstapriv = &padapter->stapriv; + + spin_lock_bh(&pstapriv->asoc_list_lock); + psta = rtw_sta_info_get_by_idx(idx, pstapriv); + spin_unlock_bh(&pstapriv->asoc_list_lock); + if (psta == NULL) { + ret = -ENOENT; + goto exit; + } + memcpy(mac, psta->hwaddr, ETH_ALEN); + sinfo->filled = BIT_ULL(NL80211_STA_INFO_SIGNAL); + sinfo->signal = psta->rssi; + +exit: + return ret; +} + +static int cfg80211_rtw_change_bss(struct wiphy *wiphy, + struct net_device *ndev, + struct bss_parameters *params) +{ + return 0; +} + +void rtw_cfg80211_rx_action(struct adapter *adapter, u8 *frame, uint frame_len, const char *msg) +{ + s32 freq; + int channel; + u8 category, action; + + channel = rtw_get_oper_ch(adapter); + + rtw_action_frame_parse(frame, frame_len, &category, &action); + + freq = rtw_ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ); + + rtw_cfg80211_rx_mgmt(adapter, freq, 0, frame, frame_len, GFP_ATOMIC); +} + +static int _cfg80211_rtw_mgmt_tx(struct adapter *padapter, u8 tx_ch, const u8 *buf, size_t len) +{ + struct xmit_frame *pmgntframe; + struct pkt_attrib *pattrib; + unsigned char *pframe; + int ret = _FAIL; + bool __maybe_unused ack = true; + struct ieee80211_hdr *pwlanhdr; + struct xmit_priv *pxmitpriv = &(padapter->xmitpriv); + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + + rtw_set_scan_deny(padapter, 1000); + + rtw_scan_abort(padapter); + if (tx_ch != rtw_get_oper_ch(padapter)) { + if (!check_fwstate(&padapter->mlmepriv, _FW_LINKED)) + pmlmeext->cur_channel = tx_ch; + set_channel_bwmode(padapter, tx_ch, HAL_PRIME_CHNL_OFFSET_DONT_CARE, CHANNEL_WIDTH_20); + } + + /* starting alloc mgmt frame to dump it */ + pmgntframe = alloc_mgtxmitframe(pxmitpriv); + if (!pmgntframe) { + /* ret = -ENOMEM; */ + ret = _FAIL; + goto exit; + } + + /* update attribute */ + pattrib = &pmgntframe->attrib; + update_mgntframe_attrib(padapter, pattrib); + pattrib->retry_ctrl = false; + + memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET); + + pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET; + + memcpy(pframe, (void *)buf, len); + pattrib->pktlen = len; + + pwlanhdr = (struct ieee80211_hdr *)pframe; + /* update seq number */ + pmlmeext->mgnt_seq = GetSequence(pwlanhdr); + pattrib->seqnum = pmlmeext->mgnt_seq; + pmlmeext->mgnt_seq++; + + pattrib->last_txcmdsz = pattrib->pktlen; + + if (dump_mgntframe_and_wait_ack(padapter, pmgntframe) != _SUCCESS) { + ack = false; + ret = _FAIL; + + } else { + msleep(50); + + ret = _SUCCESS; + } + +exit: + + return ret; +} + +static int cfg80211_rtw_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev, + struct cfg80211_mgmt_tx_params *params, + u64 *cookie) +{ + struct net_device *ndev = wdev_to_ndev(wdev); + struct ieee80211_channel *chan = params->chan; + const u8 *buf = params->buf; + size_t len = params->len; + int ret = 0; + int tx_ret; + u32 dump_limit = RTW_MAX_MGMT_TX_CNT; + u32 dump_cnt = 0; + bool ack = true; + u8 tx_ch = (u8)ieee80211_frequency_to_channel(chan->center_freq); + u8 category, action; + int type = (-1); + struct adapter *padapter; + struct rtw_wdev_priv *pwdev_priv; + + if (!ndev) { + ret = -EINVAL; + goto exit; + } + + padapter = rtw_netdev_priv(ndev); + pwdev_priv = adapter_wdev_data(padapter); + + /* cookie generation */ + *cookie = (unsigned long)buf; + + /* indicate ack before issue frame to avoid racing with rsp frame */ + rtw_cfg80211_mgmt_tx_status(padapter, *cookie, buf, len, ack, GFP_KERNEL); + + if (rtw_action_frame_parse(buf, len, &category, &action) == false) + goto exit; + + rtw_ps_deny(padapter, PS_DENY_MGNT_TX); + if (rtw_pwr_wakeup(padapter) == _FAIL) { + ret = -EFAULT; + goto cancel_ps_deny; + } + + do { + dump_cnt++; + tx_ret = _cfg80211_rtw_mgmt_tx(padapter, tx_ch, buf, len); + } while (dump_cnt < dump_limit && tx_ret != _SUCCESS); + + switch (type) { + case P2P_GO_NEGO_CONF: + rtw_clear_scan_deny(padapter); + break; + case P2P_INVIT_RESP: + if (pwdev_priv->invit_info.flags & BIT(0) && pwdev_priv->invit_info.status == 0) { + rtw_set_scan_deny(padapter, 5000); + rtw_pwr_wakeup_ex(padapter, 5000); + rtw_clear_scan_deny(padapter); + } + break; + } + +cancel_ps_deny: + rtw_ps_deny_cancel(padapter, PS_DENY_MGNT_TX); +exit: + return ret; +} + +static void rtw_cfg80211_init_ht_capab(struct ieee80211_sta_ht_cap *ht_cap, enum nl80211_band band) +{ +#define MAX_BIT_RATE_40MHZ_MCS15 300 /* Mbps */ +#define MAX_BIT_RATE_40MHZ_MCS7 150 /* Mbps */ + + ht_cap->ht_supported = true; + + ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 | + IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_SGI_20 | + IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU; + + /* + *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; + + /* + *hw->wiphy->bands[NL80211_BAND_2GHZ] + *base on ant_num + *rx_mask: RX mask + *if rx_ant = 1 rx_mask[0]= 0xff;==>MCS0-MCS7 + *if rx_ant =2 rx_mask[1]= 0xff;==>MCS8-MCS15 + *if rx_ant >=3 rx_mask[2]= 0xff; + *if BW_40 rx_mask[4]= 0x01; + *highest supported RX rate + */ + ht_cap->mcs.rx_mask[0] = 0xFF; + ht_cap->mcs.rx_mask[1] = 0x00; + ht_cap->mcs.rx_mask[4] = 0x01; + + ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7); +} + +void rtw_cfg80211_init_wiphy(struct adapter *padapter) +{ + struct ieee80211_supported_band *bands; + struct wireless_dev *pwdev = padapter->rtw_wdev; + struct wiphy *wiphy = pwdev->wiphy; + + { + bands = wiphy->bands[NL80211_BAND_2GHZ]; + if (bands) + rtw_cfg80211_init_ht_capab(&bands->ht_cap, NL80211_BAND_2GHZ); + } + + /* copy mac_addr to wiphy */ + memcpy(wiphy->perm_addr, padapter->eeprompriv.mac_addr, ETH_ALEN); +} + +static void rtw_cfg80211_preinit_wiphy(struct adapter *padapter, struct wiphy *wiphy) +{ + 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; + + wiphy->max_remain_on_channel_duration = RTW_MAX_REMAIN_ON_CHANNEL_DURATION; + + wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) + | BIT(NL80211_IFTYPE_ADHOC) + | BIT(NL80211_IFTYPE_AP) + | BIT(NL80211_IFTYPE_MONITOR) + ; + + wiphy->mgmt_stypes = rtw_cfg80211_default_mgmt_stypes; + + wiphy->software_iftypes |= BIT(NL80211_IFTYPE_MONITOR); + + wiphy->cipher_suites = rtw_cipher_suites; + wiphy->n_cipher_suites = ARRAY_SIZE(rtw_cipher_suites); + + /* if (padapter->registrypriv.wireless_mode & WIRELESS_11G) */ + wiphy->bands[NL80211_BAND_2GHZ] = rtw_spt_band_alloc(NL80211_BAND_2GHZ); + + wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL; + wiphy->flags |= WIPHY_FLAG_OFFCHAN_TX | WIPHY_FLAG_HAVE_AP_SME; + +#if defined(CONFIG_PM) + wiphy->max_sched_scan_reqs = 1; +#endif + +#if defined(CONFIG_PM) + wiphy->wowlan = &wowlan_stub; +#endif + + if (padapter->registrypriv.power_mgnt != PS_MODE_ACTIVE) + wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT; + else + wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT; +} + +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, + .get_station = cfg80211_rtw_get_station, + .scan = cfg80211_rtw_scan, + .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, + .set_tx_power = cfg80211_rtw_set_txpower, + .get_tx_power = cfg80211_rtw_get_txpower, + .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, + .get_channel = cfg80211_rtw_get_channel, + .add_virtual_intf = cfg80211_rtw_add_virtual_intf, + .del_virtual_intf = cfg80211_rtw_del_virtual_intf, + + .start_ap = cfg80211_rtw_start_ap, + .change_beacon = cfg80211_rtw_change_beacon, + .stop_ap = cfg80211_rtw_stop_ap, + + .add_station = cfg80211_rtw_add_station, + .del_station = cfg80211_rtw_del_station, + .change_station = cfg80211_rtw_change_station, + .dump_station = cfg80211_rtw_dump_station, + .change_bss = cfg80211_rtw_change_bss, + + .mgmt_tx = cfg80211_rtw_mgmt_tx, +}; + +int rtw_wdev_alloc(struct adapter *padapter, struct device *dev) +{ + int ret = 0; + struct wiphy *wiphy; + struct wireless_dev *wdev; + struct rtw_wdev_priv *pwdev_priv; + struct net_device *pnetdev = padapter->pnetdev; + + /* wiphy */ + wiphy = wiphy_new(&rtw_cfg80211_ops, sizeof(struct adapter *)); + if (!wiphy) { + ret = -ENOMEM; + goto exit; + } + set_wiphy_dev(wiphy, dev); + *((struct adapter **)wiphy_priv(wiphy)) = padapter; + rtw_cfg80211_preinit_wiphy(padapter, wiphy); + + /* init regulary domain */ + rtw_regd_init(wiphy, rtw_reg_notifier); + + ret = wiphy_register(wiphy); + if (ret < 0) + goto free_wiphy; + + /* wdev */ + wdev = rtw_zmalloc(sizeof(struct wireless_dev)); + if (!wdev) { + ret = -ENOMEM; + goto unregister_wiphy; + } + wdev->wiphy = wiphy; + wdev->netdev = pnetdev; + + wdev->iftype = NL80211_IFTYPE_STATION; /* will be init in rtw_hal_init() */ + /* Must sync with _rtw_init_mlme_priv() */ + /* pmlmepriv->fw_state = WIFI_STATION_STATE */ + 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; + spin_lock_init(&pwdev_priv->scan_req_lock); + + pwdev_priv->p2p_enabled = false; + 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; + + if (padapter->registrypriv.power_mgnt != PS_MODE_ACTIVE) + pwdev_priv->power_mgmt = true; + else + pwdev_priv->power_mgmt = false; + + return ret; + +unregister_wiphy: + wiphy_unregister(wiphy); + free_wiphy: + wiphy_free(wiphy); +exit: + return ret; +} + +void rtw_wdev_free(struct wireless_dev *wdev) +{ + if (!wdev) + return; + + kfree(wdev->wiphy->bands[NL80211_BAND_2GHZ]); + + wiphy_free(wdev->wiphy); + + kfree(wdev); +} + +void rtw_wdev_unregister(struct wireless_dev *wdev) +{ + struct net_device *ndev; + struct adapter *adapter; + struct rtw_wdev_priv *pwdev_priv; + + if (!wdev) + return; + ndev = wdev_to_ndev(wdev); + if (!ndev) + return; + + adapter = rtw_netdev_priv(ndev); + pwdev_priv = adapter_wdev_data(adapter); + + rtw_cfg80211_indicate_scan_done(adapter, true); + + if (pwdev_priv->pmon_ndev) + unregister_netdev(pwdev_priv->pmon_ndev); + + wiphy_unregister(wdev->wiphy); +} diff --git a/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c b/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c new file mode 100644 index 0000000000..c81b30f1f1 --- /dev/null +++ b/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c @@ -0,0 +1,1300 @@ +// SPDX-License-Identifier: GPL-2.0 +/****************************************************************************** + * + * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved. + * + ******************************************************************************/ + +#include <linux/etherdevice.h> +#include <drv_types.h> +#include <rtw_debug.h> +#include <rtw_mp.h> +#include <hal_btcoex.h> +#include <linux/jiffies.h> +#include <linux/kernel.h> + +#define RTL_IOCTL_WPA_SUPPLICANT (SIOCIWFIRSTPRIV + 30) + +static int wpa_set_auth_algs(struct net_device *dev, u32 value) +{ + struct adapter *padapter = rtw_netdev_priv(dev); + int ret = 0; + + if ((value & IW_AUTH_ALG_SHARED_KEY) && (value & IW_AUTH_ALG_OPEN_SYSTEM)) { + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled; + padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeAutoSwitch; + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Auto; + } else if (value & IW_AUTH_ALG_SHARED_KEY) { + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled; + + padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeShared; + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Shared; + } else if (value & IW_AUTH_ALG_OPEN_SYSTEM) { + /* padapter->securitypriv.ndisencryptstatus = Ndis802_11EncryptionDisabled; */ + if (padapter->securitypriv.ndisauthtype < Ndis802_11AuthModeWPAPSK) { + padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeOpen; + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Open; + } + } else { + ret = -EINVAL; + } + + return ret; +} + +static int wpa_set_encryption(struct net_device *dev, struct ieee_param *param, u32 param_len) +{ + int ret = 0; + u8 max_idx; + u32 wep_key_idx, wep_key_len, wep_total_len; + struct ndis_802_11_wep *pwep = NULL; + struct adapter *padapter = rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct security_priv *psecuritypriv = &padapter->securitypriv; + + 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) { + ret = -EINVAL; + goto exit; + } + + if (strcmp(param->u.crypt.alg, "WEP") == 0) + max_idx = WEP_KEYS - 1; + else + max_idx = BIP_MAX_KEYID; + + if (param->u.crypt.idx > max_idx) { + netdev_err(dev, "Error crypt.idx %d > %d\n", param->u.crypt.idx, max_idx); + ret = -EINVAL; + goto exit; + } + + if (strcmp(param->u.crypt.alg, "WEP") == 0) { + padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled; + padapter->securitypriv.dot11PrivacyAlgrthm = _WEP40_; + padapter->securitypriv.dot118021XGrpPrivacy = _WEP40_; + + wep_key_idx = param->u.crypt.idx; + wep_key_len = param->u.crypt.key_len; + + if (wep_key_len > 0) { + wep_key_len = wep_key_len <= 5 ? 5 : 13; + wep_total_len = wep_key_len + FIELD_OFFSET(struct ndis_802_11_wep, key_material); + /* Allocate a full structure to avoid potentially running off the end. */ + pwep = kzalloc(sizeof(*pwep), GFP_KERNEL); + if (!pwep) { + ret = -ENOMEM; + goto exit; + } + + pwep->key_length = 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 exit; + } + + pwep->key_index = wep_key_idx; + pwep->key_index |= 0x80000000; + + memcpy(pwep->key_material, param->u.crypt.key, pwep->key_length); + + if (param->u.crypt.set_tx) { + if (rtw_set_802_11_add_wep(padapter, pwep) == (u8)_FAIL) + ret = -EOPNOTSUPP; + } else { + /* don't update "psecuritypriv->dot11PrivacyAlgrthm" and */ + /* psecuritypriv->dot11PrivacyKeyIndex =keyid", but can rtw_set_key to fw/cam */ + + if (wep_key_idx >= WEP_KEYS) { + ret = -EOPNOTSUPP; + goto exit; + } + + memcpy(&psecuritypriv->dot11DefKey[wep_key_idx].skey[0], pwep->key_material, pwep->key_length); + psecuritypriv->dot11DefKeylen[wep_key_idx] = pwep->key_length; + 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; + + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_MP_STATE) == true) { /* sta mode */ + psta = rtw_get_stainfo(pstapriv, get_bssid(pmlmepriv)); + if (!psta) { + /* 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 */ + 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 */ + /* DEBUG_ERR(("\nset key length :param->u.crypt.key_len =%d\n", param->u.crypt.key_len)); */ + memcpy(psta->dot11tkiptxmickey.skey, ¶m->u.crypt.key[16], 8); + memcpy(psta->dot11tkiprxmickey.skey, ¶m->u.crypt.key[24], 8); + + padapter->securitypriv.busetkipkey = false; + /* _set_timer(&padapter->securitypriv.tkip_timer, 50); */ + } + + rtw_setstakey_cmd(padapter, psta, true, true); + } else { /* group key */ + if (strcmp(param->u.crypt.alg, "TKIP") == 0 || strcmp(param->u.crypt.alg, "CCMP") == 0) { + 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) { + memcpy(padapter->securitypriv.dot118021XGrptxmickey[param->u.crypt.idx].skey, ¶m->u.crypt.key[16], 8); + memcpy(padapter->securitypriv.dot118021XGrprxmickey[param->u.crypt.idx].skey, ¶m->u.crypt.key[24], 8); + } + padapter->securitypriv.binstallGrpkey = true; + + 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, "BIP") == 0) { + /* printk("BIP key_len =%d , index =%d @@@@@@@@@@@@@@@@@@\n", param->u.crypt.key_len, param->u.crypt.idx); */ + /* save the IGTK key, length 16 bytes */ + 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)); + /*printk("IGTK key below:\n"); + for (no = 0;no<16;no++) + printk(" %02x ", padapter->securitypriv.dot11wBIPKey[param->u.crypt.idx].skey[no]); + printk("\n");*/ + padapter->securitypriv.dot11wBIPKeyid = param->u.crypt.idx; + padapter->securitypriv.binstallBIPkey = true; + } + } + } + + pbcmc_sta = rtw_get_bcmc_stainfo(padapter); + if (!pbcmc_sta) { + /* 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 */ + } + } + +exit: + + kfree(pwep); + return ret; +} + +static int rtw_set_wpa_ie(struct adapter *padapter, char *pie, unsigned short ielen) +{ + u8 *buf = NULL; + int group_cipher = 0, pairwise_cipher = 0; + int ret = 0; + u8 null_addr[] = {0, 0, 0, 0, 0, 0}; + + if (ielen > MAX_WPA_IE_LEN || !pie) { + _clr_fwstate_(&padapter->mlmepriv, WIFI_UNDER_WPS); + if (!pie) + return ret; + else + return -EINVAL; + } + + if (ielen) { + buf = rtw_zmalloc(ielen); + if (!buf) { + ret = -ENOMEM; + goto exit; + } + + memcpy(buf, pie, ielen); + + 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; + memcpy(padapter->securitypriv.supplicant_ie, &buf[0], ielen); + } + + if (rtw_parse_wpa2_ie(buf, ielen, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) { + padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_8021X; + padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeWPA2PSK; + 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; + } + + _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 == WLAN_EID_VENDOR_SPECIFIC) && (!memcmp(&buf[cnt + 2], wps_oui, 4))) { + padapter->securitypriv.wps_ie_len = ((buf[cnt + 1] + 2) < MAX_WPS_IE_LEN) ? (buf[cnt + 1] + 2) : MAX_WPS_IE_LEN; + + memcpy(padapter->securitypriv.wps_ie, &buf[cnt], padapter->securitypriv.wps_ie_len); + + set_fwstate(&padapter->mlmepriv, WIFI_UNDER_WPS); + + cnt += buf[cnt + 1] + 2; + + break; + } else { + cnt += buf[cnt + 1] + 2; /* goto next */ + } + } + } + } + + /* 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: + + kfree(buf); + + return ret; +} + +static int wpa_set_param(struct net_device *dev, u8 name, u32 value) +{ + uint ret = 0; + struct adapter *padapter = 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. + */ + 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 */ + /* + 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; + struct adapter *padapter = 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; + } + + 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->pointer || p->length != sizeof(struct ieee_param)) + return -EINVAL; + + param = rtw_malloc(p->length); + if (!param) + return -ENOMEM; + + if (copy_from_user(param, p->pointer, p->length)) { + kfree(param); + return -EFAULT; + } + + 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(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: + ret = -EOPNOTSUPP; + break; + } + + if (ret == 0 && copy_to_user(p->pointer, param, p->length)) + ret = -EFAULT; + + kfree(param); + + /* up(&ieee->wx_sem); */ + return ret; +} + +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; + struct ndis_802_11_wep *pwep = NULL; + struct sta_info *psta = NULL, *pbcmc_sta = NULL; + struct adapter *padapter = rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct security_priv *psecuritypriv = &padapter->securitypriv; + struct sta_priv *pstapriv = &padapter->stapriv; + char *txkey = padapter->securitypriv.dot118021XGrptxmickey[param->u.crypt.idx].skey; + char *rxkey = padapter->securitypriv.dot118021XGrprxmickey[param->u.crypt.idx].skey; + char *grpkey = psecuritypriv->dot118021XGrpKey[param->u.crypt.idx].skey; + + 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) { + ret = -EINVAL; + goto exit; + } + } else { + psta = rtw_get_stainfo(pstapriv, param->sta_addr); + if (!psta) + /* ret = -EINVAL; */ + goto exit; + } + + if (strcmp(param->u.crypt.alg, "none") == 0 && !psta) { + /* todo:clear default encryption keys */ + + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open; + psecuritypriv->ndisencryptstatus = Ndis802_11EncryptionDisabled; + psecuritypriv->dot11PrivacyAlgrthm = _NO_PRIVACY_; + psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_; + + goto exit; + } + + if (strcmp(param->u.crypt.alg, "WEP") == 0 && !psta) { + 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 (wep_key_len > 0) { + wep_key_len = wep_key_len <= 5 ? 5 : 13; + wep_total_len = wep_key_len + FIELD_OFFSET(struct ndis_802_11_wep, key_material); + /* Allocate a full structure to avoid potentially running off the end. */ + pwep = kzalloc(sizeof(*pwep), GFP_KERNEL); + if (!pwep) + goto exit; + + pwep->key_length = wep_key_len; + pwep->length = wep_total_len; + } + + pwep->key_index = wep_key_idx; + + memcpy(pwep->key_material, param->u.crypt.key, pwep->key_length); + + if (param->u.crypt.set_tx) { + psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Auto; + psecuritypriv->ndisencryptstatus = Ndis802_11Encryption1Enabled; + psecuritypriv->dot11PrivacyAlgrthm = _WEP40_; + psecuritypriv->dot118021XGrpPrivacy = _WEP40_; + + if (pwep->key_length == 13) { + psecuritypriv->dot11PrivacyAlgrthm = _WEP104_; + psecuritypriv->dot118021XGrpPrivacy = _WEP104_; + } + + psecuritypriv->dot11PrivacyKeyIndex = wep_key_idx; + + memcpy(&psecuritypriv->dot11DefKey[wep_key_idx].skey[0], pwep->key_material, pwep->key_length); + + psecuritypriv->dot11DefKeylen[wep_key_idx] = pwep->key_length; + + rtw_ap_set_wep_key(padapter, pwep->key_material, pwep->key_length, wep_key_idx, 1); + } else { + /* don't update "psecuritypriv->dot11PrivacyAlgrthm" and */ + /* psecuritypriv->dot11PrivacyKeyIndex =keyid", but can rtw_set_key to cam */ + + memcpy(&psecuritypriv->dot11DefKey[wep_key_idx].skey[0], pwep->key_material, pwep->key_length); + + psecuritypriv->dot11DefKeylen[wep_key_idx] = pwep->key_length; + + rtw_ap_set_wep_key(padapter, pwep->key_material, pwep->key_length, 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) { + memcpy(grpkey, 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) { + psecuritypriv->dot118021XGrpPrivacy = _TKIP_; + + memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + + /* DEBUG_ERR("set key length :param->u.crypt.key_len =%d\n", param->u.crypt.key_len); */ + /* set mic key */ + memcpy(txkey, ¶m->u.crypt.key[16], 8); + memcpy(psecuritypriv->dot118021XGrprxmickey[param->u.crypt.idx].skey, ¶m->u.crypt.key[24], 8); + + psecuritypriv->busetkipkey = true; + + } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) { + psecuritypriv->dot118021XGrpPrivacy = _AES_; + + memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + } else { + psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_; + } + + psecuritypriv->dot118021XGrpKeyid = param->u.crypt.idx; + + psecuritypriv->binstallGrpkey = true; + + psecuritypriv->dot11PrivacyAlgrthm = psecuritypriv->dot118021XGrpPrivacy;/* */ + + rtw_ap_set_group_key(padapter, param->u.crypt.key, psecuritypriv->dot118021XGrpPrivacy, param->u.crypt.idx); + + pbcmc_sta = rtw_get_bcmc_stainfo(padapter); + if (pbcmc_sta) { + pbcmc_sta->ieee8021x_blocked = false; + pbcmc_sta->dot118021XPrivacy = psecuritypriv->dot118021XGrpPrivacy;/* rx will use bmc_sta's dot118021XPrivacy */ + } + } + + 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) { + 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) { + psta->dot118021XPrivacy = _WEP40_; + if (param->u.crypt.key_len == 13) + psta->dot118021XPrivacy = _WEP104_; + } else if (strcmp(param->u.crypt.alg, "TKIP") == 0) { + psta->dot118021XPrivacy = _TKIP_; + + /* DEBUG_ERR("set key length :param->u.crypt.key_len =%d\n", param->u.crypt.key_len); */ + /* set mic key */ + memcpy(psta->dot11tkiptxmickey.skey, ¶m->u.crypt.key[16], 8); + memcpy(psta->dot11tkiprxmickey.skey, ¶m->u.crypt.key[24], 8); + + psecuritypriv->busetkipkey = true; + + } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) { + psta->dot118021XPrivacy = _AES_; + } else { + psta->dot118021XPrivacy = _NO_PRIVACY_; + } + + rtw_ap_set_pairwise_key(padapter, psta); + + psta->ieee8021x_blocked = false; + + } else { /* group key??? */ + if (strcmp(param->u.crypt.alg, "WEP") == 0) { + memcpy(grpkey, 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) { + psecuritypriv->dot118021XGrpPrivacy = _TKIP_; + + memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + + /* DEBUG_ERR("set key length :param->u.crypt.key_len =%d\n", param->u.crypt.key_len); */ + /* set mic key */ + memcpy(txkey, ¶m->u.crypt.key[16], 8); + memcpy(rxkey, ¶m->u.crypt.key[24], 8); + + psecuritypriv->busetkipkey = true; + + } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) { + psecuritypriv->dot118021XGrpPrivacy = _AES_; + + memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len)); + } else { + psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_; + } + + psecuritypriv->dot118021XGrpKeyid = param->u.crypt.idx; + + psecuritypriv->binstallGrpkey = true; + + psecuritypriv->dot11PrivacyAlgrthm = psecuritypriv->dot118021XGrpPrivacy;/* */ + + rtw_ap_set_group_key(padapter, param->u.crypt.key, psecuritypriv->dot118021XGrpPrivacy, param->u.crypt.idx); + + pbcmc_sta = rtw_get_bcmc_stainfo(padapter); + if (pbcmc_sta) { + pbcmc_sta->ieee8021x_blocked = false; + pbcmc_sta->dot118021XPrivacy = psecuritypriv->dot118021XGrpPrivacy;/* rx will use bmc_sta's dot118021XPrivacy */ + } + } + } + } + +exit: + kfree(pwep); + + return ret; +} + +static int rtw_set_beacon(struct net_device *dev, struct ieee_param *param, int len) +{ + int ret = 0; + struct adapter *padapter = rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct sta_priv *pstapriv = &padapter->stapriv; + unsigned char *pbuf = param->u.bcn_ie.buf; + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) != true) + return -EINVAL; + + 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 void rtw_hostapd_sta_flush(struct net_device *dev) +{ + /* _irqL irqL; */ + /* struct list_head *phead, *plist; */ + /* struct sta_info *psta = NULL; */ + struct adapter *padapter = rtw_netdev_priv(dev); + /* struct sta_priv *pstapriv = &padapter->stapriv; */ + + flush_all_cam_entry(padapter); /* clear CAM */ + + rtw_sta_flush(padapter); +} + +static int rtw_add_sta(struct net_device *dev, struct ieee_param *param) +{ + int ret = 0; + struct sta_info *psta = NULL; + struct adapter *padapter = rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct sta_priv *pstapriv = &padapter->stapriv; + + if (check_fwstate(pmlmepriv, (_FW_LINKED | 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) + { + rtw_free_stainfo(padapter, psta); + + psta = NULL; + } +*/ + /* 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; + + psta->aid = param->u.add_sta.aid;/* aid = 1~2007 */ + + 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; + + /* chec 802.11n ht cap. */ + if (WLAN_STA_HT & flags) { + psta->htpriv.ht_option = true; + psta->qos_option = 1; + memcpy((void *)&psta->htpriv.ht_cap, (void *)¶m->u.add_sta.ht_cap, sizeof(struct ieee80211_ht_cap)); + } else { + psta->htpriv.ht_option = false; + } + + if (!pmlmepriv->htpriv.ht_option) + psta->htpriv.ht_option = false; + + update_sta_info_apmode(padapter, psta); + + } else { + ret = -ENOMEM; + } + + return ret; +} + +static int rtw_del_sta(struct net_device *dev, struct ieee_param *param) +{ + int ret = 0; + struct sta_info *psta = NULL; + struct adapter *padapter = rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct sta_priv *pstapriv = &padapter->stapriv; + + if (check_fwstate(pmlmepriv, (_FW_LINKED | 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; + + spin_lock_bh(&pstapriv->asoc_list_lock); + if (list_empty(&psta->asoc_list) == false) { + list_del_init(&psta->asoc_list); + pstapriv->asoc_list_cnt--; + updated = ap_free_sta(padapter, psta, true, WLAN_REASON_DEAUTH_LEAVING); + } + spin_unlock_bh(&pstapriv->asoc_list_lock); + + associated_clients_update(padapter, updated); + + psta = NULL; + } + + 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; + struct adapter *padapter = 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; + + if (check_fwstate(pmlmepriv, (_FW_LINKED | 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) { + psta_data->aid = (u16)psta->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; + memcpy(psta_data->tx_supp_rates, psta->bssrateset, psta->bssratelen); + memcpy(&psta_data->ht_cap, &psta->htpriv.ht_cap, sizeof(struct ieee80211_ht_cap)); + 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; + struct adapter *padapter = rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct sta_priv *pstapriv = &padapter->stapriv; + + if (check_fwstate(pmlmepriv, (_FW_LINKED | 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_VENDOR_SPECIFIC)) { + 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; + + memcpy(param->u.wpa_ie.reserved, psta->wpa_ie, copy_len); + } + } 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}; + struct adapter *padapter = rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv; + int ie_len; + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) != true) + return -EINVAL; + + ie_len = len - 12 - 2;/* 12 = param header, 2:no packed */ + + kfree(pmlmepriv->wps_beacon_ie); + 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) + return -EINVAL; + + memcpy(pmlmepriv->wps_beacon_ie, param->u.bcn_ie.buf, ie_len); + + update_beacon(padapter, WLAN_EID_VENDOR_SPECIFIC, wps_oui, true); + + 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; + struct adapter *padapter = rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + int ie_len; + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) != true) + return -EINVAL; + + ie_len = len - 12 - 2;/* 12 = param header, 2:no packed */ + + kfree(pmlmepriv->wps_probe_resp_ie); + 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) + return -EINVAL; + + 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; + struct adapter *padapter = rtw_netdev_priv(dev); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + int ie_len; + + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) != true) + return -EINVAL; + + ie_len = len - 12 - 2;/* 12 = param header, 2:no packed */ + + kfree(pmlmepriv->wps_assoc_resp_ie); + 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) + return -EINVAL; + + 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; + struct 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]; + signed int ssid_len; + 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) { + struct wlan_bssid_ex *pbss_network = &mlmepriv->cur_network.network; + struct wlan_bssid_ex *pbss_network_ext = &mlmeinfo->network; + + memcpy(ssid, ssid_ie + 2, ssid_len); + ssid[ssid_len] = 0x0; + + memcpy(pbss_network->ssid.ssid, (void *)ssid, ssid_len); + pbss_network->ssid.ssid_length = ssid_len; + memcpy(pbss_network_ext->ssid.ssid, (void *)ssid, ssid_len); + pbss_network_ext->ssid.ssid_length = ssid_len; + } + + return ret; +} + +static int rtw_ioctl_acl_remove_sta(struct net_device *dev, struct ieee_param *param, int len) +{ + struct adapter *padapter = 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; + } + + rtw_acl_remove_sta(padapter, param->sta_addr); + return 0; +} + +static int rtw_ioctl_acl_add_sta(struct net_device *dev, struct ieee_param *param, int len) +{ + struct adapter *padapter = 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; + } + + return rtw_acl_add_sta(padapter, param->sta_addr); +} + +static int rtw_ioctl_set_macaddr_acl(struct net_device *dev, struct ieee_param *param, int len) +{ + int ret = 0; + struct adapter *padapter = 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, param->u.mlme.command); + + return ret; +} + +static int rtw_hostapd_ioctl(struct net_device *dev, struct iw_point *p) +{ + struct ieee_param *param; + int ret = 0; + struct adapter *padapter = rtw_netdev_priv(dev); + + /* + * this function is expect to call in master mode, which allows no power saving + * so, we just check hw_init_completed + */ + + if (!padapter->hw_init_completed) + return -EPERM; + + if (!p->pointer || p->length != sizeof(*param)) + return -EINVAL; + + param = rtw_malloc(p->length); + if (!param) + return -ENOMEM; + + if (copy_from_user(param, p->pointer, p->length)) { + kfree(param); + return -EFAULT; + } + + switch (param->cmd) { + case RTL871X_HOSTAPD_FLUSH: + + 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; + + 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; + + default: + ret = -EOPNOTSUPP; + break; + } + + if (ret == 0 && copy_to_user(p->pointer, param, p->length)) + ret = -EFAULT; + + kfree(param); + return ret; +} + +/* copy from net/wireless/wext.c end */ + +int rtw_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) +{ + struct iwreq *wrq = (struct iwreq *)rq; + int ret = 0; + + switch (cmd) { + case RTL_IOCTL_WPA_SUPPLICANT: + ret = wpa_supplicant_ioctl(dev, &wrq->u.data); + break; + case RTL_IOCTL_HOSTAPD: + ret = rtw_hostapd_ioctl(dev, &wrq->u.data); + break; + default: + ret = -EOPNOTSUPP; + break; + } + + return ret; +} diff --git a/drivers/staging/rtl8723bs/os_dep/mlme_linux.c b/drivers/staging/rtl8723bs/os_dep/mlme_linux.c new file mode 100644 index 0000000000..1341801e5c --- /dev/null +++ b/drivers/staging/rtl8723bs/os_dep/mlme_linux.c @@ -0,0 +1,180 @@ +// SPDX-License-Identifier: GPL-2.0 +/****************************************************************************** + * + * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved. + * + ******************************************************************************/ +#include <drv_types.h> +#include <rtw_debug.h> + +static void _dynamic_check_timer_handler(struct timer_list *t) +{ + struct adapter *adapter = + from_timer(adapter, t, mlmepriv.dynamic_chk_timer); + + rtw_dynamic_check_timer_handler(adapter); + + _set_timer(&adapter->mlmepriv.dynamic_chk_timer, 2000); +} + +static void _rtw_set_scan_deny_timer_hdl(struct timer_list *t) +{ + struct adapter *adapter = + from_timer(adapter, t, mlmepriv.set_scan_deny_timer); + + rtw_clear_scan_deny(adapter); +} + +void rtw_init_mlme_timer(struct adapter *padapter) +{ + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + + timer_setup(&pmlmepriv->assoc_timer, _rtw_join_timeout_handler, 0); + timer_setup(&pmlmepriv->scan_to_timer, rtw_scan_timeout_handler, 0); + timer_setup(&pmlmepriv->dynamic_chk_timer, + _dynamic_check_timer_handler, 0); + timer_setup(&pmlmepriv->set_scan_deny_timer, + _rtw_set_scan_deny_timer_hdl, 0); +} + +void rtw_os_indicate_connect(struct adapter *adapter) +{ + struct mlme_priv *pmlmepriv = &(adapter->mlmepriv); + + 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); + } + + netif_carrier_on(adapter->pnetdev); + + if (adapter->pid[2] != 0) + rtw_signal_process(adapter->pid[2], SIGALRM); +} + +void rtw_os_indicate_scan_done(struct adapter *padapter, bool aborted) +{ + rtw_cfg80211_indicate_scan_done(padapter, aborted); +} + +static struct rt_pmkid_list backupPMKIDList[NUM_PMKID_CACHE]; +void rtw_reset_securitypriv(struct adapter *adapter) +{ + u8 backupPMKIDIndex = 0; + u8 backupTKIPCountermeasure = 0x00; + u32 backupTKIPcountermeasure_time = 0; + /* add for CONFIG_IEEE80211W, none 11w also can use */ + struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv; + + spin_lock_bh(&adapter->security_key_mutex); + + if (adapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) { + /* 802.1x */ + /* 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. */ + + memcpy(&backupPMKIDList[0], &adapter->securitypriv.PMKIDList[0], sizeof(struct rt_pmkid_list) * NUM_PMKID_CACHE); + backupPMKIDIndex = adapter->securitypriv.PMKIDIndex; + backupTKIPCountermeasure = adapter->securitypriv.btkip_countermeasure; + backupTKIPcountermeasure_time = adapter->securitypriv.btkip_countermeasure_time; + + /* reset RX BIP packet number */ + pmlmeext->mgnt_80211w_IPN_rx = 0; + + 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. */ + memcpy(&adapter->securitypriv.PMKIDList[0], &backupPMKIDList[0], sizeof(struct 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; + + } 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; + /* */ + } + /* add for CONFIG_IEEE80211W, none 11w also can use */ + spin_unlock_bh(&adapter->security_key_mutex); +} + +void rtw_os_indicate_disconnect(struct adapter *adapter) +{ + /* struct rt_pmkid_list backupPMKIDList[ NUM_PMKID_CACHE ]; */ + + netif_carrier_off(adapter->pnetdev); /* Do it first for tx broadcast pkt after disconnection issue! */ + + rtw_cfg80211_indicate_disconnect(adapter); + + /* modify for CONFIG_IEEE80211W, none 11w also can use the same command */ + rtw_reset_securitypriv_cmd(adapter); +} + +void rtw_report_sec_ie(struct adapter *adapter, u8 authmode, u8 *sec_ie) +{ + uint len; + u8 *buff, *p, i; + union iwreq_data wrqu; + + buff = NULL; + if (authmode == WLAN_EID_VENDOR_SPECIFIC) { + buff = rtw_zmalloc(IW_CUSTOM_MAX); + if (!buff) + return; + + p = buff; + + p += scnprintf(p, IW_CUSTOM_MAX - (p - buff), "ASSOCINFO(ReqIEs ="); + + len = sec_ie[1] + 2; + len = (len < IW_CUSTOM_MAX) ? len : IW_CUSTOM_MAX; + + for (i = 0; i < len; i++) + p += scnprintf(p, IW_CUSTOM_MAX - (p - buff), "%02x", sec_ie[i]); + + p += scnprintf(p, IW_CUSTOM_MAX - (p - buff), ")"); + + 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; + + kfree(buff); + } +} + +void init_addba_retry_timer(struct adapter *padapter, struct sta_info *psta) +{ + timer_setup(&psta->addba_retry_timer, addba_timer_hdl, 0); +} + +void init_mlme_ext_timer(struct adapter *padapter) +{ + struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv; + + timer_setup(&pmlmeext->survey_timer, survey_timer_hdl, 0); + timer_setup(&pmlmeext->link_timer, link_timer_hdl, 0); + timer_setup(&pmlmeext->sa_query_timer, sa_query_timer_hdl, 0); +} diff --git a/drivers/staging/rtl8723bs/os_dep/os_intfs.c b/drivers/staging/rtl8723bs/os_dep/os_intfs.c new file mode 100644 index 0000000000..68bba3c0e7 --- /dev/null +++ b/drivers/staging/rtl8723bs/os_dep/os_intfs.c @@ -0,0 +1,1254 @@ +// SPDX-License-Identifier: GPL-2.0 +/****************************************************************************** + * + * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved. + * + ******************************************************************************/ +#include <drv_types.h> +#include <rtw_debug.h> +#include <hal_data.h> + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Realtek Wireless Lan Driver"); +MODULE_AUTHOR("Realtek Semiconductor Corp."); +MODULE_VERSION(DRIVERVERSION); + +/* module param defaults */ +static int rtw_chip_version; +static int rtw_rfintfs = HWPI; +static int rtw_lbkmode;/* RTL8712_AIR_TRX; */ + + +static int rtw_network_mode = Ndis802_11IBSS;/* Ndis802_11Infrastructure;infra, ad-hoc, auto */ +/* struct ndis_802_11_ssid ssid; */ +static int rtw_channel = 1;/* ad-hoc support requirement */ +static int rtw_wireless_mode = WIRELESS_11BG_24N; +static int rtw_vrtl_carrier_sense = AUTO_VCS; +static int rtw_vcs_type = RTS_CTS;/* */ +static int rtw_rts_thresh = 2347;/* */ +static int rtw_frag_thresh = 2346;/* */ +static int rtw_preamble = PREAMBLE_LONG;/* long, short, auto */ +static int rtw_scan_mode = 1;/* active, passive */ +static int rtw_adhoc_tx_pwr = 1; +static int rtw_soft_ap; +/* int smart_ps = 1; */ +static int rtw_power_mgnt = 1; +static int rtw_ips_mode = IPS_NORMAL; +module_param(rtw_ips_mode, int, 0644); +MODULE_PARM_DESC(rtw_ips_mode, "The default IPS mode"); + +static int rtw_smart_ps = 2; + +static int rtw_check_fw_ps = 1; + +static int rtw_usb_rxagg_mode = 2;/* USB_RX_AGG_DMA = 1, USB_RX_AGG_USB =2 */ +module_param(rtw_usb_rxagg_mode, int, 0644); + +static int rtw_radio_enable = 1; +static int rtw_long_retry_lmt = 7; +static int rtw_short_retry_lmt = 7; +static int rtw_busy_thresh = 40; +/* int qos_enable = 0; */ +static int rtw_ack_policy = NORMAL_ACK; + +static int rtw_software_encrypt; +static int rtw_software_decrypt; + +static int rtw_acm_method;/* 0:By SW 1:By HW. */ + +static int rtw_wmm_enable = 1;/* default is set to enable the wmm. */ +static int rtw_uapsd_enable; +static int rtw_uapsd_max_sp = NO_LIMIT; +static int rtw_uapsd_acbk_en; +static int rtw_uapsd_acbe_en; +static int rtw_uapsd_acvi_en; +static int rtw_uapsd_acvo_en; + +int rtw_ht_enable = 1; +/* + * 0: 20 MHz, 1: 40 MHz + * 2.4G use bit 0 ~ 3 + * 0x01 means enable 2.4G 40MHz + */ +static int rtw_bw_mode = 0x01; +static int rtw_ampdu_enable = 1;/* for enable tx_ampdu ,0: disable, 0x1:enable (but wifi_spec should be 0), 0x2: force enable (don't care wifi_spec) */ +static int rtw_rx_stbc = 1;/* 0: disable, 1:enable 2.4g */ +static int rtw_ampdu_amsdu;/* 0: disabled, 1:enabled, 2:auto . There is an IOT issu with DLINK DIR-629 when the flag turn on */ +/* 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 */ +static 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 */ +static 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 */ +static int rtw_stbc_cap = 0x13; +/* BIT0: Enable VHT Beamformer, BIT1: Enable VHT Beamformee, BIT4: Enable HT Beamformer, BIT5: Enable HT Beamformee */ +static int rtw_beamform_cap = 0x2; + +static int rtw_lowrate_two_xmit = 1;/* Use 2 path Tx to transmit MCS0~7 and legacy mode */ + +static int rtw_low_power; +static int rtw_wifi_spec; +static int rtw_channel_plan = RT_CHANNEL_DOMAIN_MAX; + +static int rtw_ant_num = -1; /* <0: undefined, >0: Antenna number */ +module_param(rtw_ant_num, int, 0644); +MODULE_PARM_DESC(rtw_ant_num, "Antenna number setting"); + +static int rtw_antdiv_cfg = 1; /* 0:OFF , 1:ON, 2:decide by Efuse config */ +static int rtw_antdiv_type; /* 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) */ + + + +static int rtw_hw_wps_pbc; + +int rtw_mc2u_disable; + +static int rtw_80211d; + +static int rtw_qos_opt_enable;/* 0: disable, 1:enable */ +module_param(rtw_qos_opt_enable, int, 0644); + +static char *ifname = "wlan%d"; +module_param(ifname, charp, 0644); +MODULE_PARM_DESC(ifname, "The default name to allocate for first interface"); + +char *rtw_initmac; /* temp mac address if users want to use instead of the mac address in Efuse */ + +module_param(rtw_initmac, charp, 0644); +module_param(rtw_channel_plan, int, 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_wmm_enable, int, 0644); +module_param(rtw_vrtl_carrier_sense, int, 0644); +module_param(rtw_vcs_type, int, 0644); +module_param(rtw_busy_thresh, int, 0644); + +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_ampdu_amsdu, int, 0644); + +module_param(rtw_lowrate_two_xmit, int, 0644); + +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_antdiv_cfg, int, 0644); +module_param(rtw_antdiv_type, int, 0644); + + +module_param(rtw_hw_wps_pbc, int, 0644); + +static 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"); + +module_param(rtw_mc2u_disable, int, 0644); + +module_param(rtw_80211d, int, 0644); +MODULE_PARM_DESC(rtw_80211d, "Enable 802.11d mechanism"); + +static uint 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"); + +#define CONFIG_RTW_HIQ_FILTER 1 + +static 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"); + +static int rtw_tx_pwr_lmt_enable; +static int rtw_tx_pwr_by_rate; + +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"); + +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"); + +static int netdev_close(struct net_device *pnetdev); + +static void loadparam(struct adapter *padapter, struct net_device *pnetdev) +{ + struct registry_priv *registry_par = &padapter->registrypriv; + + 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; + + memcpy(registry_par->ssid.ssid, "ANY", 3); + registry_par->ssid.ssid_length = 3; + + registry_par->channel = (u8)rtw_channel; + registry_par->wireless_mode = (u8)rtw_wireless_mode; + + 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->adhoc_tx_pwr = (u8)rtw_adhoc_tx_pwr; + registry_par->soft_ap = (u8)rtw_soft_ap; + registry_par->smart_ps = (u8)rtw_smart_ps; + registry_par->check_fw_ps = (u8)rtw_check_fw_ps; + registry_par->power_mgnt = (u8)rtw_power_mgnt; + registry_par->ips_mode = (u8)rtw_ips_mode; + 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->qos_enable = (u8)rtw_qos_enable; */ + registry_par->ack_policy = (u8)rtw_ack_policy; + 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; + + /* UAPSD */ + registry_par->wmm_enable = (u8)rtw_wmm_enable; + registry_par->uapsd_enable = (u8)rtw_uapsd_enable; + registry_par->uapsd_max_sp = (u8)rtw_uapsd_max_sp; + registry_par->uapsd_acbk_en = (u8)rtw_uapsd_acbk_en; + registry_par->uapsd_acbe_en = (u8)rtw_uapsd_acbe_en; + registry_par->uapsd_acvi_en = (u8)rtw_uapsd_acvi_en; + registry_par->uapsd_acvo_en = (u8)rtw_uapsd_acvo_en; + + registry_par->ht_enable = (u8)rtw_ht_enable; + 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->ampdu_amsdu = (u8)rtw_ampdu_amsdu; + registry_par->short_gi = (u8)rtw_short_gi; + registry_par->ldpc_cap = (u8)rtw_ldpc_cap; + registry_par->stbc_cap = (u8)rtw_stbc_cap; + registry_par->beamform_cap = (u8)rtw_beamform_cap; + + registry_par->lowrate_two_xmit = (u8)rtw_lowrate_two_xmit; + registry_par->low_power = (u8)rtw_low_power; + + + registry_par->wifi_spec = (u8)rtw_wifi_spec; + + registry_par->channel_plan = (u8)rtw_channel_plan; + + registry_par->ant_num = (s8)rtw_ant_num; + + registry_par->accept_addba_req = true; + + registry_par->antdiv_cfg = (u8)rtw_antdiv_cfg; + registry_par->antdiv_type = (u8)rtw_antdiv_type; + + registry_par->hw_wps_pbc = (u8)rtw_hw_wps_pbc; + + registry_par->max_roaming_times = (u8)rtw_max_roaming_times; + + registry_par->enable80211d = (u8)rtw_80211d; + + snprintf(registry_par->ifname, 16, "%s", ifname); + + registry_par->notch_filter = (u8)rtw_notch_filter; + + registry_par->RegEnableTxPowerLimit = (u8)rtw_tx_pwr_lmt_enable; + registry_par->RegEnableTxPowerByRate = (u8)rtw_tx_pwr_by_rate; + + registry_par->RegPowerBase = 14; + registry_par->TxBBSwing_2G = 0xFF; + registry_par->bEn_RFE = 1; + registry_par->RFE_Type = 64; + + registry_par->qos_opt_enable = (u8)rtw_qos_opt_enable; + + registry_par->hiq_filter = (u8)rtw_hiq_filter; +} + +static int rtw_net_set_mac_address(struct net_device *pnetdev, void *p) +{ + struct adapter *padapter = rtw_netdev_priv(pnetdev); + struct sockaddr *addr = p; + + if (!padapter->bup) { + /* addr->sa_data[4], addr->sa_data[5]); */ + memcpy(padapter->eeprompriv.mac_addr, addr->sa_data, ETH_ALEN); + /* eth_hw_addr_set(pnetdev, addr->sa_data); */ + /* padapter->bset_hwaddr = true; */ + } + + return 0; +} + +static struct net_device_stats *rtw_net_get_stats(struct net_device *pnetdev) +{ + struct adapter *padapter = 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; +} + +/* + * 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. */ +static 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, + struct net_device *sb_dev) +{ + struct 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]; +} + +u16 rtw_recv_select_queue(struct sk_buff *skb) +{ + struct iphdr *piphdr; + unsigned int dscp; + __be16 eth_type; + u32 priority; + u8 *pdata = skb->data; + + memcpy(ð_type, pdata + (ETH_ALEN << 1), 2); + + switch (be16_to_cpu(eth_type)) { + case ETH_P_IP: + + piphdr = (struct iphdr *)(pdata + ETH_HLEN); + + dscp = piphdr->tos & 0xfc; + + priority = dscp >> 5; + + break; + default: + priority = 0; + } + + return rtw_1d_to_queue[priority]; +} + +static int rtw_ndev_notifier_call(struct notifier_block *nb, unsigned long state, void *ptr) +{ + struct net_device *dev = netdev_notifier_info_to_dev(ptr); + + if (dev->netdev_ops->ndo_do_ioctl != rtw_ioctl) + return NOTIFY_DONE; + + netdev_dbg(dev, FUNC_NDEV_FMT " state:%lu\n", FUNC_NDEV_ARG(dev), + state); + + 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); +} + + +static int rtw_ndev_init(struct net_device *dev) +{ + struct adapter *adapter = rtw_netdev_priv(dev); + + netdev_dbg(dev, FUNC_ADPT_FMT "\n", FUNC_ADPT_ARG(adapter)); + strncpy(adapter->old_ifname, dev->name, IFNAMSIZ); + + return 0; +} + +static void rtw_ndev_uninit(struct net_device *dev) +{ + struct adapter *adapter = rtw_netdev_priv(dev); + + netdev_dbg(dev, FUNC_ADPT_FMT "\n", FUNC_ADPT_ARG(adapter)); +} + +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, + .ndo_select_queue = rtw_select_queue, + .ndo_set_mac_address = rtw_net_set_mac_address, + .ndo_get_stats = rtw_net_get_stats, + .ndo_do_ioctl = rtw_ioctl, +}; + +int rtw_init_netdev_name(struct net_device *pnetdev, const char *ifname) +{ + if (dev_alloc_name(pnetdev, ifname) < 0) { + pr_err("dev_alloc_name, fail for %s\n", ifname); + return 1; + } + netif_carrier_off(pnetdev); + /* rtw_netif_stop_queue(pnetdev); */ + + return 0; +} + +struct net_device *rtw_init_netdev(struct adapter *old_padapter) +{ + struct adapter *padapter; + struct net_device *pnetdev; + + if (old_padapter) + pnetdev = rtw_alloc_etherdev_with_old_priv(sizeof(struct adapter), (void *)old_padapter); + else + pnetdev = rtw_alloc_etherdev(sizeof(struct adapter)); + + pr_info("pnetdev = %p\n", pnetdev); + if (!pnetdev) + return NULL; + + padapter = rtw_netdev_priv(pnetdev); + padapter->pnetdev = pnetdev; + + /* pnetdev->init = NULL; */ + + pnetdev->netdev_ops = &rtw_netdev_ops; + + /* pnetdev->tx_timeout = NULL; */ + pnetdev->watchdog_timeo = HZ * 3; /* 3 second timeout */ + + /* step 2. */ + loadparam(padapter, pnetdev); + + return pnetdev; +} + +void rtw_unregister_netdevs(struct dvobj_priv *dvobj) +{ + struct adapter *padapter = NULL; + struct net_device *pnetdev = NULL; + + padapter = dvobj->padapters; + + if (!padapter) + return; + + pnetdev = padapter->pnetdev; + + if ((padapter->DriverState != DRIVER_DISAPPEAR) && pnetdev) + unregister_netdev(pnetdev); /* will call netdev_close() */ + rtw_wdev_unregister(padapter->rtw_wdev); +} + +u32 rtw_start_drv_threads(struct adapter *padapter) +{ + u32 _status = _SUCCESS; + + padapter->xmitThread = kthread_run(rtw_xmit_thread, padapter, "RTW_XMIT_THREAD"); + if (IS_ERR(padapter->xmitThread)) + _status = _FAIL; + + padapter->cmdThread = kthread_run(rtw_cmd_thread, padapter, "RTW_CMD_THREAD"); + if (IS_ERR(padapter->cmdThread)) + _status = _FAIL; + else + wait_for_completion(&padapter->cmdpriv.terminate_cmdthread_comp); /* wait for cmd_thread to run */ + + rtw_hal_start_thread(padapter); + return _status; +} + +void rtw_stop_drv_threads(struct adapter *padapter) +{ + rtw_stop_cmd_thread(padapter); + + /* Below is to termindate tx_thread... */ + complete(&padapter->xmitpriv.xmit_comp); + wait_for_completion(&padapter->xmitpriv.terminate_xmitthread_comp); + + rtw_hal_stop_thread(padapter); +} + +static void rtw_init_default_value(struct adapter *padapter) +{ + struct registry_priv *pregistrypriv = &padapter->registrypriv; + struct xmit_priv *pxmitpriv = &padapter->xmitpriv; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + 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; + + /* recv_priv */ + + /* mlme_priv */ + pmlmepriv->scan_mode = SCAN_ACTIVE; + + /* qos_priv */ + /* pmlmepriv->qospriv.qos_option = pregistrypriv->wmm_enable; */ + + /* ht_priv */ + pmlmepriv->htpriv.ampdu_enable = false;/* set to disabled */ + + /* security_priv */ + /* rtw_get_encrypt_decrypt_from_registrypriv(padapter); */ + psecuritypriv->binstallGrpkey = _FAIL; + 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; + + /* registry_priv */ + rtw_init_registrypriv_dev_network(padapter); + rtw_update_registrypriv_dev_network(padapter); + + /* hal_priv */ + rtw_hal_def_value_init(padapter); + + /* misc. */ + RTW_ENABLE_FUNC(padapter, DF_RX_BIT); + RTW_ENABLE_FUNC(padapter, DF_TX_BIT); + padapter->bLinkInfoDump = 0; + padapter->bNotifyChannelChange = 0; + + /* for debug purpose */ + padapter->fix_rate = 0xFF; + padapter->driver_ampdu_spacing = 0xFF; + padapter->driver_rx_ampdu_factor = 0xFF; + +} + +struct dvobj_priv *devobj_init(void) +{ + struct dvobj_priv *pdvobj = NULL; + + pdvobj = rtw_zmalloc(sizeof(*pdvobj)); + if (!pdvobj) + return NULL; + + mutex_init(&pdvobj->hw_init_mutex); + mutex_init(&pdvobj->h2c_fwcmd_mutex); + mutex_init(&pdvobj->setch_mutex); + mutex_init(&pdvobj->setbw_mutex); + + spin_lock_init(&pdvobj->lock); + + pdvobj->macid[1] = true; /* macid = 1 for bc/mc stainfo */ + + pdvobj->processing_dev_remove = false; + + atomic_set(&pdvobj->disable_func, 0); + + spin_lock_init(&pdvobj->cam_ctl.lock); + + return pdvobj; +} + +void devobj_deinit(struct dvobj_priv *pdvobj) +{ + if (!pdvobj) + return; + + mutex_destroy(&pdvobj->hw_init_mutex); + mutex_destroy(&pdvobj->h2c_fwcmd_mutex); + mutex_destroy(&pdvobj->setch_mutex); + mutex_destroy(&pdvobj->setbw_mutex); + + kfree(pdvobj); +} + +void rtw_reset_drv_sw(struct adapter *padapter) +{ + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter); + + /* hal_priv */ + if (is_primary_adapter(padapter)) + 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, _FW_UNDER_SURVEY | _FW_UNDER_LINKING); + + pwrctrlpriv->pwr_state_check_cnts = 0; + + /* mlmeextpriv */ + padapter->mlmeextpriv.sitesurvey_res.state = SCAN_DISABLE; + + rtw_set_signal_stat_timer(&padapter->recvpriv); + +} + + +u8 rtw_init_drv_sw(struct adapter *padapter) +{ + rtw_init_default_value(padapter); + + rtw_init_hal_com_default_value(padapter); + + if (rtw_init_cmd_priv(&padapter->cmdpriv)) + return _FAIL; + + padapter->cmdpriv.padapter = padapter; + + if (rtw_init_evt_priv(&padapter->evtpriv)) + goto free_cmd_priv; + + if (rtw_init_mlme_priv(padapter) == _FAIL) + goto free_evt_priv; + + init_mlme_ext_priv(padapter); + + if (_rtw_init_xmit_priv(&padapter->xmitpriv, padapter) == _FAIL) + goto free_mlme_ext; + + if (_rtw_init_recv_priv(&padapter->recvpriv, padapter) == _FAIL) + goto free_xmit_priv; + /* add for CONFIG_IEEE80211W, none 11w also can use */ + spin_lock_init(&padapter->security_key_mutex); + + /* We don't need to memset padapter->XXX to zero, because adapter is allocated by vzalloc(). */ + /* memset((unsigned char *)&padapter->securitypriv, 0, sizeof (struct security_priv)); */ + + if (_rtw_init_sta_priv(&padapter->stapriv) == _FAIL) + goto free_recv_priv; + + padapter->stapriv.padapter = padapter; + padapter->setband = GHZ24_50; + padapter->fix_rate = 0xFF; + rtw_init_bcmc_stainfo(padapter); + + rtw_init_pwrctrl_priv(padapter); + + rtw_hal_dm_init(padapter); + + return _SUCCESS; + +free_recv_priv: + _rtw_free_recv_priv(&padapter->recvpriv); + +free_xmit_priv: + _rtw_free_xmit_priv(&padapter->xmitpriv); + +free_mlme_ext: + free_mlme_ext_priv(&padapter->mlmeextpriv); + + rtw_free_mlme_priv(&padapter->mlmepriv); + +free_evt_priv: + rtw_free_evt_priv(&padapter->evtpriv); + +free_cmd_priv: + rtw_free_cmd_priv(&padapter->cmdpriv); + + return _FAIL; +} + +void rtw_cancel_all_timer(struct adapter *padapter) +{ + del_timer_sync(&padapter->mlmepriv.assoc_timer); + + del_timer_sync(&padapter->mlmepriv.scan_to_timer); + + del_timer_sync(&padapter->mlmepriv.dynamic_chk_timer); + + del_timer_sync(&(adapter_to_pwrctl(padapter)->pwr_state_check_timer)); + + del_timer_sync(&padapter->mlmepriv.set_scan_deny_timer); + rtw_clear_scan_deny(padapter); + + del_timer_sync(&padapter->recvpriv.signal_stat_timer); + + /* cancel dm timer */ + rtw_hal_dm_deinit(padapter); +} + +u8 rtw_free_drv_sw(struct adapter *padapter) +{ + free_mlme_ext_priv(&padapter->mlmeextpriv); + + rtw_free_cmd_priv(&padapter->cmdpriv); + + rtw_free_evt_priv(&padapter->evtpriv); + + rtw_free_mlme_priv(&padapter->mlmepriv); + + /* 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); + + /* kfree((void *)padapter); */ + + rtw_hal_free_data(padapter); + + /* free the old_pnetdev */ + if (padapter->rereg_nd_name_priv.old_pnetdev) { + free_netdev(padapter->rereg_nd_name_priv.old_pnetdev); + padapter->rereg_nd_name_priv.old_pnetdev = NULL; + } + + /* clear pbuddystruct adapter to avoid access wrong pointer. */ + if (padapter->pbuddy_adapter) + padapter->pbuddy_adapter->pbuddy_adapter = NULL; + + return _SUCCESS; +} + +static int _rtw_drv_register_netdev(struct adapter *padapter, char *name) +{ + int ret = _SUCCESS; + struct net_device *pnetdev = padapter->pnetdev; + + /* alloc netdev name */ + if (rtw_init_netdev_name(pnetdev, name)) + return _FAIL; + + eth_hw_addr_set(pnetdev, padapter->eeprompriv.mac_addr); + + /* Tell the network stack we exist */ + if (register_netdev(pnetdev) != 0) { + ret = _FAIL; + goto error_register_netdev; + } + + return ret; + +error_register_netdev: + + rtw_free_drv_sw(padapter); + + rtw_free_netdev(pnetdev); + + return ret; +} + +int rtw_drv_register_netdev(struct adapter *if1) +{ + struct dvobj_priv *dvobj = if1->dvobj; + struct adapter *padapter = dvobj->padapters; + char *name = if1->registrypriv.ifname; + + return _rtw_drv_register_netdev(padapter, name); +} + +static int _netdev_open(struct net_device *pnetdev) +{ + uint status; + struct adapter *padapter = rtw_netdev_priv(pnetdev); + struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter); + + padapter->netif_up = true; + + if (pwrctrlpriv->ps_flag) { + padapter->net_closed = false; + goto netdev_open_normal_process; + } + + if (!padapter->bup) { + padapter->bDriverStopped = false; + padapter->bSurpriseRemoved = false; + padapter->bCardDisableWOHSM = false; + + status = rtw_hal_init(padapter); + if (status == _FAIL) + goto netdev_open_error; + + status = rtw_start_drv_threads(padapter); + if (status == _FAIL) + goto netdev_open_error; + + if (padapter->intf_start) + padapter->intf_start(padapter); + + rtw_cfg80211_init_wiphy(padapter); + + padapter->bup = true; + pwrctrlpriv->bips_processing = false; + } + padapter->net_closed = false; + + _set_timer(&padapter->mlmepriv.dynamic_chk_timer, 2000); + + if (!rtw_netif_queue_stopped(pnetdev)) + rtw_netif_start_queue(pnetdev); + else + rtw_netif_wake_queue(pnetdev); + +netdev_open_normal_process: + + return 0; + +netdev_open_error: + + padapter->bup = false; + + netif_carrier_off(pnetdev); + rtw_netif_stop_queue(pnetdev); + + return (-1); +} + +int netdev_open(struct net_device *pnetdev) +{ + int ret; + struct adapter *padapter = rtw_netdev_priv(pnetdev); + struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter); + + if (pwrctrlpriv->bInSuspend) + return 0; + + if (mutex_lock_interruptible(&(adapter_to_dvobj(padapter)->hw_init_mutex))) + return -1; + + ret = _netdev_open(pnetdev); + mutex_unlock(&(adapter_to_dvobj(padapter)->hw_init_mutex)); + + return ret; +} + +static int ips_netdrv_open(struct adapter *padapter) +{ + int status = _SUCCESS; + /* struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); */ + + padapter->net_closed = false; + + padapter->bDriverStopped = false; + padapter->bCardDisableWOHSM = false; + /* padapter->bup = true; */ + + status = rtw_hal_init(padapter); + if (status == _FAIL) + goto netdev_open_error; + + if (padapter->intf_start) + padapter->intf_start(padapter); + + _set_timer(&padapter->mlmepriv.dynamic_chk_timer, 2000); + + return _SUCCESS; + +netdev_open_error: + + return _FAIL; +} + + +int rtw_ips_pwr_up(struct adapter *padapter) +{ + return ips_netdrv_open(padapter); +} + +void rtw_ips_pwr_down(struct adapter *padapter) +{ + padapter->bCardDisableWOHSM = true; + padapter->net_closed = true; + + rtw_ips_dev_unload(padapter); + padapter->bCardDisableWOHSM = false; +} + +void rtw_ips_dev_unload(struct adapter *padapter) +{ + + if (!padapter->bSurpriseRemoved) + rtw_hal_deinit(padapter); +} + +static int pm_netdev_open(struct net_device *pnetdev, u8 bnormal) +{ + int status = -1; + + struct adapter *padapter = rtw_netdev_priv(pnetdev); + + if (bnormal) { + if (mutex_lock_interruptible(&(adapter_to_dvobj(padapter)->hw_init_mutex)) == 0) { + status = _netdev_open(pnetdev); + mutex_unlock(&(adapter_to_dvobj(padapter)->hw_init_mutex)); + } + } else { + status = (_SUCCESS == ips_netdrv_open(padapter)) ? (0) : (-1); + } + + return status; +} + +static int netdev_close(struct net_device *pnetdev) +{ + struct adapter *padapter = rtw_netdev_priv(pnetdev); + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(padapter); + + if (pwrctl->bInternalAutoSuspend) { + /* rtw_pwr_wakeup(padapter); */ + if (pwrctl->rf_pwrstate == rf_off) + pwrctl->ps_flag = true; + } + padapter->net_closed = true; + padapter->netif_up = false; + +/*if (!padapter->hw_init_completed) + { + + padapter->bDriverStopped = true; + + rtw_dev_unload(padapter); + } + else*/ + if (pwrctl->rf_pwrstate == rf_on) { + /* s1. */ + if (pnetdev) { + if (!rtw_netif_queue_stopped(pnetdev)) + rtw_netif_stop_queue(pnetdev); + } + + /* s2. */ + LeaveAllPowerSaveMode(padapter); + rtw_disassoc_cmd(padapter, 500, false); + /* s2-2. indicate disconnect to os */ + rtw_indicate_disconnect(padapter); + /* s2-3. */ + rtw_free_assoc_resources(padapter, 1); + /* s2-4. */ + rtw_free_network_queue(padapter, true); + } + + rtw_scan_abort(padapter); + adapter_wdev_data(padapter)->bandroid_scan = false; + + return 0; +} + +void rtw_ndev_destructor(struct net_device *ndev) +{ + kfree(ndev->ieee80211_ptr); +} + +void rtw_dev_unload(struct adapter *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; + u8 cnt = 0; + + if (padapter->bup) { + + padapter->bDriverStopped = true; + if (padapter->xmitpriv.ack_tx) + rtw_ack_tx_done(&padapter->xmitpriv, RTW_SCTX_DONE_DRV_STOP); + + if (padapter->intf_stop) + padapter->intf_stop(padapter); + + if (!pwrctl->bInternalAutoSuspend) + rtw_stop_drv_threads(padapter); + + while (atomic_read(&pcmdpriv->cmdthd_running)) { + if (cnt > 5) { + break; + } else { + cnt++; + msleep(10); + } + } + + /* check the status of IPS */ + if (rtw_hal_check_ips_status(padapter) || pwrctl->rf_pwrstate == rf_off) { + /* check HW status and SW state */ + netdev_dbg(padapter->pnetdev, + "%s: driver in IPS-FWLPS\n", __func__); + pdbgpriv->dbg_dev_unload_inIPS_cnt++; + LeaveAllPowerSaveMode(padapter); + } else { + netdev_dbg(padapter->pnetdev, + "%s: driver not in IPS\n", __func__); + } + + if (!padapter->bSurpriseRemoved) { + hal_btcoex_IpsNotify(padapter, pwrctl->ips_mode_req); + + /* amy modify 20120221 for power seq is different between driver open and ips */ + rtw_hal_deinit(padapter); + + padapter->bSurpriseRemoved = true; + } + + padapter->bup = false; + + } +} + +static int rtw_suspend_free_assoc_resource(struct adapter *padapter) +{ + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + + if (rtw_chk_roam_flags(padapter, RTW_ROAM_ON_RESUME)) { + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) + && check_fwstate(pmlmepriv, _FW_LINKED)) { + rtw_set_to_roam(padapter, 1); + } + } + + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) && check_fwstate(pmlmepriv, _FW_LINKED)) { + rtw_disassoc_cmd(padapter, 0, false); + /* s2-2. indicate disconnect to os */ + rtw_indicate_disconnect(padapter); + } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) { + rtw_sta_flush(padapter); + } + + /* s2-3. */ + rtw_free_assoc_resources(padapter, 1); + + /* s2-4. */ + rtw_free_network_queue(padapter, true); + + if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY)) + rtw_indicate_scan_done(padapter, 1); + + if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING)) { + netdev_dbg(padapter->pnetdev, "%s: fw_under_linking\n", + __func__); + rtw_indicate_disconnect(padapter); + } + + return _SUCCESS; +} + +static void rtw_suspend_normal(struct adapter *padapter) +{ + struct net_device *pnetdev = padapter->pnetdev; + + if (pnetdev) { + netif_carrier_off(pnetdev); + rtw_netif_stop_queue(pnetdev); + } + + rtw_suspend_free_assoc_resource(padapter); + + if ((rtw_hal_check_ips_status(padapter)) || (adapter_to_pwrctl(padapter)->rf_pwrstate == rf_off)) + netdev_dbg(padapter->pnetdev, + "%s: ### ERROR #### driver in IPS ####ERROR###!!!\n", + __func__); + + rtw_dev_unload(padapter); + + /* sdio_deinit(adapter_to_dvobj(padapter)); */ + if (padapter->intf_deinit) + padapter->intf_deinit(adapter_to_dvobj(padapter)); +} + +void rtw_suspend_common(struct adapter *padapter) +{ + struct dvobj_priv *psdpriv = padapter->dvobj; + struct debug_priv *pdbgpriv = &psdpriv->drv_dbg; + struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(psdpriv); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + + unsigned long start_time = jiffies; + + netdev_dbg(padapter->pnetdev, " suspend start\n"); + pdbgpriv->dbg_suspend_cnt++; + + pwrpriv->bInSuspend = true; + + while (pwrpriv->bips_processing) + msleep(1); + + if ((!padapter->bup) || (padapter->bDriverStopped) || (padapter->bSurpriseRemoved)) { + pdbgpriv->dbg_suspend_error_cnt++; + goto exit; + } + rtw_ps_deny(padapter, PS_DENY_SUSPEND); + + rtw_cancel_all_timer(padapter); + + LeaveAllPowerSaveModeDirect(padapter); + + rtw_stop_cmd_thread(padapter); + + /* wait for the latest FW to remove this condition. */ + if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) + hal_btcoex_SuspendNotify(padapter, 0); + else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) + hal_btcoex_SuspendNotify(padapter, 1); + + rtw_ps_deny_cancel(padapter, PS_DENY_SUSPEND); + + rtw_suspend_normal(padapter); + + netdev_dbg(padapter->pnetdev, "rtw suspend success in %d ms\n", + jiffies_to_msecs(jiffies - start_time)); + +exit: + + return; +} + +static int rtw_resume_process_normal(struct adapter *padapter) +{ + struct net_device *pnetdev; + struct pwrctrl_priv *pwrpriv; + struct mlme_priv *pmlmepriv; + 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); + pmlmepriv = &padapter->mlmepriv; + psdpriv = padapter->dvobj; + pdbgpriv = &psdpriv->drv_dbg; + /* interface init */ + /* if (sdio_init(adapter_to_dvobj(padapter)) != _SUCCESS) */ + if ((padapter->intf_init) && (padapter->intf_init(adapter_to_dvobj(padapter)) != _SUCCESS)) { + ret = -1; + goto exit; + } + rtw_hal_disable_interrupt(padapter); + /* if (sdio_alloc_irq(adapter_to_dvobj(padapter)) != _SUCCESS) */ + if ((padapter->intf_alloc_irq) && (padapter->intf_alloc_irq(adapter_to_dvobj(padapter)) != _SUCCESS)) { + ret = -1; + goto exit; + } + + rtw_reset_drv_sw(padapter); + pwrpriv->bkeepfwalive = false; + + if (pm_netdev_open(pnetdev, true) != 0) { + ret = -1; + pdbgpriv->dbg_resume_error_cnt++; + goto exit; + } + + netif_device_attach(pnetdev); + netif_carrier_on(pnetdev); + + if (padapter->pid[1] != 0) + rtw_signal_process(padapter->pid[1], SIGUSR2); + + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) { + if (rtw_chk_roam_flags(padapter, RTW_ROAM_ON_RESUME)) + rtw_roaming(padapter, NULL); + } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) { + rtw_ap_restore_network(padapter); + } + +exit: + return ret; +} + +int rtw_resume_common(struct adapter *padapter) +{ + int ret = 0; + unsigned long start_time = jiffies; + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + + netdev_dbg(padapter->pnetdev, "resume start\n"); + + rtw_resume_process_normal(padapter); + + hal_btcoex_SuspendNotify(padapter, 0); + + if (pwrpriv) { + pwrpriv->bInSuspend = false; + } + netdev_dbg(padapter->pnetdev, "%s:%d in %d ms\n", __func__, ret, + jiffies_to_msecs(jiffies - start_time)); + + return ret; +} diff --git a/drivers/staging/rtl8723bs/os_dep/osdep_service.c b/drivers/staging/rtl8723bs/os_dep/osdep_service.c new file mode 100644 index 0000000000..f09c1324c3 --- /dev/null +++ b/drivers/staging/rtl8723bs/os_dep/osdep_service.c @@ -0,0 +1,235 @@ +// SPDX-License-Identifier: GPL-2.0 +/****************************************************************************** + * + * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved. + * + ******************************************************************************/ +#include <drv_types.h> +#include <rtw_debug.h> + +/* +* 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; + return _FAIL; +} + +void *_rtw_malloc(u32 sz) +{ + return kmalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL); +} + +void *_rtw_zmalloc(u32 sz) +{ + void *pbuf = _rtw_malloc(sz); + + if (pbuf) + memset(pbuf, 0, sz); + + return pbuf; +} + +inline struct sk_buff *_rtw_skb_alloc(u32 sz) +{ + return __dev_alloc_skb(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL); +} + +inline struct sk_buff *_rtw_skb_copy(const struct sk_buff *skb) +{ + return skb_copy(skb, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL); +} + +inline int _rtw_netif_rx(struct net_device *ndev, struct sk_buff *skb) +{ + skb->dev = ndev; + return netif_rx(skb); +} + +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; + + pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4); + 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; + + pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4); + if (!pnetdev) + goto RETURN; + + pnpi = netdev_priv(pnetdev); + + pnpi->priv = vzalloc(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; + + vfree(pnpi->priv); + free_netdev(netdev); + +RETURN: + return; +} + +void rtw_buf_free(u8 **buf, u32 *buf_len) +{ + if (!buf || !buf_len) + return; + + if (*buf) { + *buf_len = 0; + kfree(*buf); + *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; + 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) + kfree(ori); +} + + +/** + * 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; + + 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; + + buf = cbuf->bufs[cbuf->read]; + cbuf->read = (cbuf->read + 1) % cbuf->size; + + return buf; +} + +/** + * rtw_cbuf_alloc - allocate 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 = rtw_malloc(struct_size(cbuf, bufs, size)); + + if (cbuf) { + cbuf->write = cbuf->read = 0; + cbuf->size = size; + } + + return cbuf; +} diff --git a/drivers/staging/rtl8723bs/os_dep/recv_linux.c b/drivers/staging/rtl8723bs/os_dep/recv_linux.c new file mode 100644 index 0000000000..4d28b300b2 --- /dev/null +++ b/drivers/staging/rtl8723bs/os_dep/recv_linux.c @@ -0,0 +1,229 @@ +// SPDX-License-Identifier: GPL-2.0 +/****************************************************************************** + * + * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved. + * + ******************************************************************************/ +#include <drv_types.h> +#include <rtw_debug.h> +#include <linux/jiffies.h> +#include <net/cfg80211.h> +#include <asm/unaligned.h> + +void rtw_os_free_recvframe(union recv_frame *precvframe) +{ + if (precvframe->u.hdr.pkt) { + dev_kfree_skb_any(precvframe->u.hdr.pkt);/* free skb by driver */ + + precvframe->u.hdr.pkt = NULL; + } +} + +/* alloc os related resource in union recv_frame */ +void rtw_os_recv_resource_alloc(struct adapter *padapter, union recv_frame *precvframe) +{ + precvframe->u.hdr.pkt_newalloc = precvframe->u.hdr.pkt = NULL; +} + +/* free os related resource in union recv_frame */ +void rtw_os_recv_resource_free(struct recv_priv *precvpriv) +{ + signed int i; + union recv_frame *precvframe; + + precvframe = (union recv_frame *) precvpriv->precv_frame_buf; + + for (i = 0; i < NR_RECVFRAME; i++) { + if (precvframe->u.hdr.pkt) { + /* free skb by driver */ + dev_kfree_skb_any(precvframe->u.hdr.pkt); + precvframe->u.hdr.pkt = NULL; + } + precvframe++; + } +} + +/* free os related resource in struct recv_buf */ +void rtw_os_recvbuf_resource_free(struct adapter *padapter, struct recv_buf *precvbuf) +{ + if (precvbuf->pskb) { + dev_kfree_skb_any(precvbuf->pskb); + } +} + +struct sk_buff *rtw_os_alloc_msdu_pkt(union recv_frame *prframe, u16 nSubframe_Length, u8 *pdata) +{ + u16 eth_type; + struct sk_buff *sub_skb; + struct rx_pkt_attrib *pattrib; + + pattrib = &prframe->u.hdr.attrib; + + sub_skb = rtw_skb_alloc(nSubframe_Length + 12); + if (!sub_skb) + return NULL; + + skb_reserve(sub_skb, 12); + skb_put_data(sub_skb, (pdata + ETH_HLEN), nSubframe_Length); + + eth_type = get_unaligned_be16(&sub_skb->data[6]); + + if (sub_skb->len >= 8 && + ((!memcmp(sub_skb->data, rfc1042_header, SNAP_SIZE) && + eth_type != ETH_P_AARP && eth_type != ETH_P_IPX) || + !memcmp(sub_skb->data, bridge_tunnel_header, SNAP_SIZE))) { + /* + * remove RFC1042 or Bridge-Tunnel encapsulation and replace + * EtherType + */ + skb_pull(sub_skb, SNAP_SIZE); + memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src, ETH_ALEN); + memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst, ETH_ALEN); + } else { + __be16 len; + /* Leave Ethernet header part of hdr and full payload */ + len = htons(sub_skb->len); + memcpy(skb_push(sub_skb, 2), &len, 2); + memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src, ETH_ALEN); + memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst, ETH_ALEN); + } + + return sub_skb; +} + +void rtw_os_recv_indicate_pkt(struct adapter *padapter, struct sk_buff *pkt, struct rx_pkt_attrib *pattrib) +{ + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + + /* Indicate the packets to upper layer */ + if (pkt) { + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) { + struct sk_buff *pskb2 = NULL; + struct sta_info *psta = NULL; + struct sta_priv *pstapriv = &padapter->stapriv; + int bmcast = is_multicast_ether_addr(pattrib->dst); + + if (memcmp(pattrib->dst, myid(&padapter->eeprompriv), ETH_ALEN)) { + if (bmcast) { + psta = rtw_get_bcmc_stainfo(padapter); + pskb2 = skb_clone(pkt, GFP_ATOMIC); + } else { + psta = rtw_get_stainfo(pstapriv, pattrib->dst); + } + + if (psta) { + struct net_device *pnetdev = (struct net_device *)padapter->pnetdev; + /* skb->ip_summed = CHECKSUM_NONE; */ + pkt->dev = pnetdev; + skb_set_queue_mapping(pkt, rtw_recv_select_queue(pkt)); + + _rtw_xmit_entry(pkt, pnetdev); + + if (bmcast && pskb2) + pkt = pskb2; + else + return; + } + } else { + /* to APself */ + } + } + + pkt->protocol = eth_type_trans(pkt, padapter->pnetdev); + pkt->dev = padapter->pnetdev; + + pkt->ip_summed = CHECKSUM_NONE; + + rtw_netif_rx(padapter->pnetdev, pkt); + } +} + +void rtw_handle_tkip_mic_err(struct adapter *padapter, u8 bgroup) +{ + enum nl80211_key_type key_type = 0; + union iwreq_data wrqu; + struct iw_michaelmicfailure ev; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct security_priv *psecuritypriv = &padapter->securitypriv; + unsigned long cur_time = 0; + + if (psecuritypriv->last_mic_err_time == 0) { + psecuritypriv->last_mic_err_time = jiffies; + } else { + cur_time = jiffies; + + 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 = jiffies; + } + } + + if (bgroup) { + key_type |= NL80211_KEYTYPE_GROUP; + } else { + key_type |= NL80211_KEYTYPE_PAIRWISE; + } + + cfg80211_michael_mic_failure(padapter->pnetdev, (u8 *)&pmlmepriv->assoc_bssid[0], key_type, -1, + NULL, GFP_ATOMIC); + + 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; + memcpy(ev.src_addr.sa_data, &pmlmepriv->assoc_bssid[0], ETH_ALEN); + + memset(&wrqu, 0x00, sizeof(wrqu)); + wrqu.data.length = sizeof(ev); +} + +int rtw_recv_indicatepkt(struct adapter *padapter, union recv_frame *precv_frame) +{ + struct recv_priv *precvpriv; + struct __queue *pfree_recv_queue; + struct sk_buff *skb; + struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib; + + precvpriv = &(padapter->recvpriv); + pfree_recv_queue = &(precvpriv->free_recv_queue); + + skb = precv_frame->u.hdr.pkt; + if (!skb) + goto _recv_indicatepkt_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; + + rtw_os_recv_indicate_pkt(padapter, skb, pattrib); + + /* pointers to NULL before rtw_free_recvframe() */ + precv_frame->u.hdr.pkt = NULL; + + rtw_free_recvframe(precv_frame, pfree_recv_queue); + + return _SUCCESS; + +_recv_indicatepkt_drop: + + /* enqueue back to free_recv_queue */ + rtw_free_recvframe(precv_frame, pfree_recv_queue); + + return _FAIL; +} + +void rtw_init_recv_timer(struct recv_reorder_ctrl *preorder_ctrl) +{ + timer_setup(&preorder_ctrl->reordering_ctrl_timer, + rtw_reordering_ctrl_timeout_handler, 0); + +} diff --git a/drivers/staging/rtl8723bs/os_dep/sdio_intf.c b/drivers/staging/rtl8723bs/os_dep/sdio_intf.c new file mode 100644 index 0000000000..4904314845 --- /dev/null +++ b/drivers/staging/rtl8723bs/os_dep/sdio_intf.c @@ -0,0 +1,506 @@ +// SPDX-License-Identifier: GPL-2.0 +/****************************************************************************** + * + * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved. + * + ******************************************************************************/ +#include <drv_types.h> +#include <rtw_debug.h> +#include <hal_btcoex.h> +#include <linux/jiffies.h> + +#ifndef dev_to_sdio_func +#define dev_to_sdio_func(d) container_of(d, struct sdio_func, dev) +#endif + +static const struct sdio_device_id sdio_ids[] = { + { SDIO_DEVICE(0x024c, 0x0523), }, + { SDIO_DEVICE(0x024c, 0x0525), }, + { SDIO_DEVICE(0x024c, 0x0623), }, + { SDIO_DEVICE(0x024c, 0x0626), }, + { SDIO_DEVICE(0x024c, 0x0627), }, + { SDIO_DEVICE(0x024c, 0xb723), }, + { /* end: all zeroes */ }, +}; +MODULE_DEVICE_TABLE(sdio, sdio_ids); + +static int rtw_drv_init(struct sdio_func *func, const struct sdio_device_id *id); +static void rtw_dev_remove(struct sdio_func *func); +static int rtw_sdio_resume(struct device *dev); +static int rtw_sdio_suspend(struct device *dev); + +static const struct dev_pm_ops rtw_sdio_pm_ops = { + .suspend = rtw_sdio_suspend, + .resume = rtw_sdio_resume, +}; + +static struct sdio_driver rtl8723bs_sdio_driver = { + .probe = rtw_drv_init, + .remove = rtw_dev_remove, + .name = "rtl8723bs", + .id_table = sdio_ids, + .drv = { + .pm = &rtw_sdio_pm_ops, + } +}; + +static void sd_sync_int_hdl(struct sdio_func *func) +{ + struct dvobj_priv *psdpriv; + + + psdpriv = sdio_get_drvdata(func); + + if (!psdpriv->if1) + return; + + rtw_sdio_set_irq_thd(psdpriv, current); + sd_int_hdl(psdpriv->if1); + rtw_sdio_set_irq_thd(psdpriv, NULL); +} + +static int sdio_alloc_irq(struct dvobj_priv *dvobj) +{ + struct sdio_data *psdio_data; + struct sdio_func *func; + int err; + + psdio_data = &dvobj->intf_data; + func = psdio_data->func; + + sdio_claim_host(func); + + err = sdio_claim_irq(func, &sd_sync_int_hdl); + if (err) { + dvobj->drv_dbg.dbg_sdio_alloc_irq_error_cnt++; + printk(KERN_CRIT "%s: sdio_claim_irq FAIL(%d)!\n", __func__, err); + } else { + dvobj->drv_dbg.dbg_sdio_alloc_irq_cnt++; + dvobj->irq_alloc = 1; + } + + sdio_release_host(func); + + return err?_FAIL:_SUCCESS; +} + +static void sdio_free_irq(struct dvobj_priv *dvobj) +{ + struct sdio_data *psdio_data; + struct sdio_func *func; + int err; + + if (dvobj->irq_alloc) { + psdio_data = &dvobj->intf_data; + func = psdio_data->func; + + if (func) { + sdio_claim_host(func); + err = sdio_release_irq(func); + if (err) { + dvobj->drv_dbg.dbg_sdio_free_irq_error_cnt++; + netdev_err(dvobj->if1->pnetdev, + "%s: sdio_release_irq FAIL(%d)!\n", + __func__, err); + } else + dvobj->drv_dbg.dbg_sdio_free_irq_cnt++; + sdio_release_host(func); + } + dvobj->irq_alloc = 0; + } +} + +static u32 sdio_init(struct dvobj_priv *dvobj) +{ + struct sdio_data *psdio_data; + struct sdio_func *func; + int err; + + psdio_data = &dvobj->intf_data; + func = psdio_data->func; + + /* 3 1. init SDIO bus */ + sdio_claim_host(func); + + err = sdio_enable_func(func); + if (err) { + dvobj->drv_dbg.dbg_sdio_init_error_cnt++; + goto release; + } + + err = sdio_set_block_size(func, 512); + if (err) { + dvobj->drv_dbg.dbg_sdio_init_error_cnt++; + goto release; + } + psdio_data->block_transfer_len = 512; + psdio_data->tx_block_mode = 1; + psdio_data->rx_block_mode = 1; + +release: + sdio_release_host(func); + + if (err) + return _FAIL; + return _SUCCESS; +} + +static void sdio_deinit(struct dvobj_priv *dvobj) +{ + struct sdio_func *func; + int err; + + func = dvobj->intf_data.func; + + if (func) { + sdio_claim_host(func); + err = sdio_disable_func(func); + if (err) + dvobj->drv_dbg.dbg_sdio_deinit_error_cnt++; + + if (dvobj->irq_alloc) { + err = sdio_release_irq(func); + if (err) + dvobj->drv_dbg.dbg_sdio_free_irq_error_cnt++; + else + dvobj->drv_dbg.dbg_sdio_free_irq_cnt++; + } + + sdio_release_host(func); + } +} +static struct dvobj_priv *sdio_dvobj_init(struct sdio_func *func) +{ + int status = _FAIL; + struct dvobj_priv *dvobj = NULL; + struct sdio_data *psdio; + + dvobj = devobj_init(); + if (!dvobj) + goto exit; + + sdio_set_drvdata(func, dvobj); + + psdio = &dvobj->intf_data; + psdio->func = func; + + if (sdio_init(dvobj) != _SUCCESS) + goto free_dvobj; + + rtw_reset_continual_io_error(dvobj); + status = _SUCCESS; + +free_dvobj: + if (status != _SUCCESS && dvobj) { + sdio_set_drvdata(func, NULL); + + devobj_deinit(dvobj); + + dvobj = NULL; + } +exit: + return dvobj; +} + +static void sdio_dvobj_deinit(struct sdio_func *func) +{ + struct dvobj_priv *dvobj = sdio_get_drvdata(func); + + sdio_set_drvdata(func, NULL); + if (dvobj) { + sdio_deinit(dvobj); + devobj_deinit(dvobj); + } +} + +void rtw_set_hal_ops(struct adapter *padapter) +{ + /* alloc memory for HAL DATA */ + rtw_hal_data_init(padapter); + + rtl8723bs_set_hal_ops(padapter); +} + +static void sd_intf_start(struct adapter *padapter) +{ + if (!padapter) + return; + + /* hal dep */ + rtw_hal_enable_interrupt(padapter); +} + +static void sd_intf_stop(struct adapter *padapter) +{ + if (!padapter) + return; + + /* hal dep */ + rtw_hal_disable_interrupt(padapter); +} + + +static struct adapter *rtw_sdio_if1_init(struct dvobj_priv *dvobj, const struct sdio_device_id *pdid) +{ + int status = _FAIL; + struct net_device *pnetdev; + struct adapter *padapter = NULL; + struct sdio_data *psdio = &dvobj->intf_data; + + padapter = vzalloc(sizeof(*padapter)); + if (!padapter) + goto exit; + + padapter->dvobj = dvobj; + dvobj->if1 = padapter; + + padapter->bDriverStopped = true; + + dvobj->padapters = padapter; + padapter->iface_id = 0; + + /* 3 1. init network device data */ + pnetdev = rtw_init_netdev(padapter); + if (!pnetdev) + goto free_adapter; + + SET_NETDEV_DEV(pnetdev, dvobj_to_dev(dvobj)); + + padapter = rtw_netdev_priv(pnetdev); + + /* 3 3. init driver special setting, interface, OS and hardware relative */ + + /* 4 3.1 set hardware operation functions */ + rtw_set_hal_ops(padapter); + + + /* 3 5. initialize Chip version */ + padapter->intf_start = &sd_intf_start; + padapter->intf_stop = &sd_intf_stop; + + padapter->intf_init = &sdio_init; + padapter->intf_deinit = &sdio_deinit; + padapter->intf_alloc_irq = &sdio_alloc_irq; + padapter->intf_free_irq = &sdio_free_irq; + + if (rtw_init_io_priv(padapter, sdio_set_intf_ops) == _FAIL) + goto free_hal_data; + + rtw_hal_read_chip_version(padapter); + + rtw_hal_chip_configure(padapter); + + hal_btcoex_Initialize((void *) padapter); + + /* 3 6. read efuse/eeprom data */ + rtw_hal_read_chip_info(padapter); + + /* 3 7. init driver common data */ + if (rtw_init_drv_sw(padapter) == _FAIL) + goto free_hal_data; + + rtw_wdev_alloc(padapter, dvobj_to_dev(dvobj)); + + /* 3 8. get WLan MAC address */ + /* set mac addr */ + rtw_macaddr_cfg(&psdio->func->dev, padapter->eeprompriv.mac_addr); + + rtw_hal_disable_interrupt(padapter); + + status = _SUCCESS; + +free_hal_data: + if (status != _SUCCESS && padapter->HalData) + kfree(padapter->HalData); + + if (status != _SUCCESS) { + rtw_wdev_unregister(padapter->rtw_wdev); + rtw_wdev_free(padapter->rtw_wdev); + } + +free_adapter: + if (status != _SUCCESS) { + if (pnetdev) + rtw_free_netdev(pnetdev); + else + vfree((u8 *)padapter); + padapter = NULL; + } +exit: + return padapter; +} + +static void rtw_sdio_if1_deinit(struct adapter *if1) +{ + struct net_device *pnetdev = if1->pnetdev; + struct mlme_priv *pmlmepriv = &if1->mlmepriv; + + if (check_fwstate(pmlmepriv, _FW_LINKED)) + rtw_disassoc_cmd(if1, 0, false); + + free_mlme_ap_info(if1); + + rtw_cancel_all_timer(if1); + + rtw_dev_unload(if1); + + if (if1->rtw_wdev) + rtw_wdev_free(if1->rtw_wdev); + + rtw_free_drv_sw(if1); + + if (pnetdev) + rtw_free_netdev(pnetdev); +} + +/* + * 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. + */ +static int rtw_drv_init( + struct sdio_func *func, + const struct sdio_device_id *id) +{ + int status = _FAIL; + struct adapter *if1 = NULL; + struct dvobj_priv *dvobj; + + dvobj = sdio_dvobj_init(func); + if (!dvobj) + goto exit; + + if1 = rtw_sdio_if1_init(dvobj, id); + if (!if1) + goto free_dvobj; + + /* dev_alloc_name && register_netdev */ + status = rtw_drv_register_netdev(if1); + if (status != _SUCCESS) + goto free_if1; + + if (sdio_alloc_irq(dvobj) != _SUCCESS) + goto free_if1; + + rtw_ndev_notifier_register(); + status = _SUCCESS; + +free_if1: + if (status != _SUCCESS && if1) + rtw_sdio_if1_deinit(if1); + +free_dvobj: + if (status != _SUCCESS) + sdio_dvobj_deinit(func); +exit: + return status == _SUCCESS ? 0 : -ENODEV; +} + +static void rtw_dev_remove(struct sdio_func *func) +{ + struct dvobj_priv *dvobj = sdio_get_drvdata(func); + struct adapter *padapter = dvobj->if1; + + dvobj->processing_dev_remove = true; + + rtw_unregister_netdevs(dvobj); + + if (!padapter->bSurpriseRemoved) { + int err; + + /* test surprise remove */ + sdio_claim_host(func); + sdio_readb(func, 0, &err); + sdio_release_host(func); + if (err == -ENOMEDIUM) + padapter->bSurpriseRemoved = true; + } + + rtw_ps_deny(padapter, PS_DENY_DRV_REMOVE); + + rtw_pm_set_ips(padapter, IPS_NONE); + rtw_pm_set_lps(padapter, PS_MODE_ACTIVE); + + LeaveAllPowerSaveMode(padapter); + + rtw_btcoex_HaltNotify(padapter); + + rtw_sdio_if1_deinit(padapter); + + sdio_dvobj_deinit(func); +} + +static int rtw_sdio_suspend(struct device *dev) +{ + struct sdio_func *func = dev_to_sdio_func(dev); + struct dvobj_priv *psdpriv = sdio_get_drvdata(func); + struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(psdpriv); + struct adapter *padapter = psdpriv->if1; + struct debug_priv *pdbgpriv = &psdpriv->drv_dbg; + + if (padapter->bDriverStopped) + return 0; + + if (pwrpriv->bInSuspend) { + pdbgpriv->dbg_suspend_error_cnt++; + return 0; + } + + rtw_suspend_common(padapter); + + return 0; +} + +static int rtw_resume_process(struct adapter *padapter) +{ + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + struct dvobj_priv *psdpriv = padapter->dvobj; + struct debug_priv *pdbgpriv = &psdpriv->drv_dbg; + + if (!pwrpriv->bInSuspend) { + pdbgpriv->dbg_resume_error_cnt++; + return -1; + } + + return rtw_resume_common(padapter); +} + +static int rtw_sdio_resume(struct device *dev) +{ + struct sdio_func *func = dev_to_sdio_func(dev); + struct dvobj_priv *psdpriv = sdio_get_drvdata(func); + struct adapter *padapter = psdpriv->if1; + struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv; + int ret = 0; + struct debug_priv *pdbgpriv = &psdpriv->drv_dbg; + + pdbgpriv->dbg_resume_cnt++; + + ret = rtw_resume_process(padapter); + + pmlmeext->last_scan_time = jiffies; + return ret; +} + +static int __init rtw_drv_entry(void) +{ + int ret; + + ret = sdio_register_driver(&rtl8723bs_sdio_driver); + if (ret != 0) + rtw_ndev_notifier_unregister(); + + return ret; +} + +static void __exit rtw_drv_halt(void) +{ + sdio_unregister_driver(&rtl8723bs_sdio_driver); + + rtw_ndev_notifier_unregister(); +} + + +module_init(rtw_drv_entry); +module_exit(rtw_drv_halt); diff --git a/drivers/staging/rtl8723bs/os_dep/sdio_ops_linux.c b/drivers/staging/rtl8723bs/os_dep/sdio_ops_linux.c new file mode 100644 index 0000000000..0a0b04088e --- /dev/null +++ b/drivers/staging/rtl8723bs/os_dep/sdio_ops_linux.c @@ -0,0 +1,497 @@ +// SPDX-License-Identifier: GPL-2.0 +/****************************************************************************** + * + * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved. + * + *******************************************************************************/ + +#include <drv_types.h> +#include <rtw_debug.h> + +static bool rtw_sdio_claim_host_needed(struct sdio_func *func) +{ + struct dvobj_priv *dvobj = sdio_get_drvdata(func); + struct sdio_data *sdio_data = &dvobj->intf_data; + + if (sdio_data->sys_sdio_irq_thd && sdio_data->sys_sdio_irq_thd == current) + return false; + return true; +} + +inline void rtw_sdio_set_irq_thd(struct dvobj_priv *dvobj, void *thd_hdl) +{ + struct sdio_data *sdio_data = &dvobj->intf_data; + + sdio_data->sys_sdio_irq_thd = thd_hdl; +} + +/* + * Return: + *0 Success + *others Fail + */ +s32 _sd_cmd52_read(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pdata) +{ + struct adapter *padapter; + struct dvobj_priv *psdiodev; + struct sdio_data *psdio; + + int err = 0, i; + struct sdio_func *func; + + padapter = pintfhdl->padapter; + psdiodev = pintfhdl->pintf_dev; + psdio = &psdiodev->intf_data; + + if (padapter->bSurpriseRemoved) + return err; + + func = psdio->func; + + for (i = 0; i < cnt; i++) { + pdata[i] = sdio_readb(func, addr + i, &err); + if (err) + break; + } + return err; +} + +/* + * Return: + *0 Success + *others Fail + */ +s32 sd_cmd52_read(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pdata) +{ + struct adapter *padapter; + struct dvobj_priv *psdiodev; + struct sdio_data *psdio; + + int err = 0; + struct sdio_func *func; + bool claim_needed; + + padapter = pintfhdl->padapter; + psdiodev = pintfhdl->pintf_dev; + psdio = &psdiodev->intf_data; + + if (padapter->bSurpriseRemoved) + return err; + + func = psdio->func; + claim_needed = rtw_sdio_claim_host_needed(func); + + if (claim_needed) + sdio_claim_host(func); + err = _sd_cmd52_read(pintfhdl, addr, cnt, pdata); + if (claim_needed) + sdio_release_host(func); + return err; +} + +/* + * Return: + *0 Success + *others Fail + */ +s32 _sd_cmd52_write(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pdata) +{ + struct adapter *padapter; + struct dvobj_priv *psdiodev; + struct sdio_data *psdio; + + int err = 0, i; + struct sdio_func *func; + + padapter = pintfhdl->padapter; + psdiodev = pintfhdl->pintf_dev; + psdio = &psdiodev->intf_data; + + if (padapter->bSurpriseRemoved) + return err; + + func = psdio->func; + + for (i = 0; i < cnt; i++) { + sdio_writeb(func, pdata[i], addr + i, &err); + if (err) + break; + } + return err; +} + +/* + * Return: + *0 Success + *others Fail + */ +s32 sd_cmd52_write(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pdata) +{ + struct adapter *padapter; + struct dvobj_priv *psdiodev; + struct sdio_data *psdio; + + int err = 0; + struct sdio_func *func; + bool claim_needed; + + padapter = pintfhdl->padapter; + psdiodev = pintfhdl->pintf_dev; + psdio = &psdiodev->intf_data; + + if (padapter->bSurpriseRemoved) + return err; + + func = psdio->func; + claim_needed = rtw_sdio_claim_host_needed(func); + + if (claim_needed) + sdio_claim_host(func); + err = _sd_cmd52_write(pintfhdl, addr, cnt, pdata); + if (claim_needed) + sdio_release_host(func); + return err; +} + +u8 sd_read8(struct intf_hdl *pintfhdl, u32 addr, s32 *err) +{ + struct adapter *padapter; + struct dvobj_priv *psdiodev; + struct sdio_data *psdio; + + u8 v = 0; + struct sdio_func *func; + bool claim_needed; + + padapter = pintfhdl->padapter; + psdiodev = pintfhdl->pintf_dev; + psdio = &psdiodev->intf_data; + + if (padapter->bSurpriseRemoved) + return v; + + func = psdio->func; + claim_needed = rtw_sdio_claim_host_needed(func); + + if (claim_needed) + sdio_claim_host(func); + v = sdio_readb(func, addr, err); + if (claim_needed) + sdio_release_host(func); + return v; +} + +u32 sd_read32(struct intf_hdl *pintfhdl, u32 addr, s32 *err) +{ + struct adapter *padapter; + struct dvobj_priv *psdiodev; + struct sdio_data *psdio; + u32 v = 0; + struct sdio_func *func; + bool claim_needed; + + padapter = pintfhdl->padapter; + psdiodev = pintfhdl->pintf_dev; + psdio = &psdiodev->intf_data; + + if (padapter->bSurpriseRemoved) + return v; + + func = psdio->func; + claim_needed = rtw_sdio_claim_host_needed(func); + + if (claim_needed) + sdio_claim_host(func); + v = sdio_readl(func, addr, err); + if (claim_needed) + sdio_release_host(func); + + if (err && *err) { + int i; + + *err = 0; + for (i = 0; i < SD_IO_TRY_CNT; i++) { + if (claim_needed) + sdio_claim_host(func); + v = sdio_readl(func, addr, err); + if (claim_needed) + sdio_release_host(func); + + if (*err == 0) { + rtw_reset_continual_io_error(psdiodev); + break; + } else { + if ((-ESHUTDOWN == *err) || (-ENODEV == *err)) + padapter->bSurpriseRemoved = true; + + if (rtw_inc_and_chk_continual_io_error(psdiodev) == true) { + padapter->bSurpriseRemoved = true; + break; + } + } + } + } + return v; +} + +void sd_write8(struct intf_hdl *pintfhdl, u32 addr, u8 v, s32 *err) +{ + struct adapter *padapter; + struct dvobj_priv *psdiodev; + struct sdio_data *psdio; + struct sdio_func *func; + bool claim_needed; + + padapter = pintfhdl->padapter; + psdiodev = pintfhdl->pintf_dev; + psdio = &psdiodev->intf_data; + + if (padapter->bSurpriseRemoved) + return; + + func = psdio->func; + claim_needed = rtw_sdio_claim_host_needed(func); + + if (claim_needed) + sdio_claim_host(func); + sdio_writeb(func, v, addr, err); + if (claim_needed) + sdio_release_host(func); +} + +void sd_write32(struct intf_hdl *pintfhdl, u32 addr, u32 v, s32 *err) +{ + struct adapter *padapter; + struct dvobj_priv *psdiodev; + struct sdio_data *psdio; + struct sdio_func *func; + bool claim_needed; + + padapter = pintfhdl->padapter; + psdiodev = pintfhdl->pintf_dev; + psdio = &psdiodev->intf_data; + + if (padapter->bSurpriseRemoved) + return; + + func = psdio->func; + claim_needed = rtw_sdio_claim_host_needed(func); + + if (claim_needed) + sdio_claim_host(func); + sdio_writel(func, v, addr, err); + if (claim_needed) + sdio_release_host(func); + + if (err && *err) { + int i; + + *err = 0; + for (i = 0; i < SD_IO_TRY_CNT; i++) { + if (claim_needed) + sdio_claim_host(func); + sdio_writel(func, v, addr, err); + if (claim_needed) + sdio_release_host(func); + if (*err == 0) { + rtw_reset_continual_io_error(psdiodev); + break; + } else { + if ((-ESHUTDOWN == *err) || (-ENODEV == *err)) + padapter->bSurpriseRemoved = true; + + if (rtw_inc_and_chk_continual_io_error(psdiodev) == true) { + padapter->bSurpriseRemoved = true; + break; + } + } + } + + } +} + +/* + * Use CMD53 to read data from SDIO device. + * This function MUST be called after sdio_claim_host() or + * in SDIO ISR(host had been claimed). + * + * Parameters: + *psdio pointer of SDIO_DATA + *addr address to read + *cnt amount to read + *pdata pointer to put data, this should be a "DMA:able scratch buffer"! + * + * Return: + *0 Success + *others Fail + */ +s32 _sd_read(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, void *pdata) +{ + struct adapter *padapter; + struct dvobj_priv *psdiodev; + struct sdio_data *psdio; + + int err = -EPERM; + struct sdio_func *func; + + padapter = pintfhdl->padapter; + psdiodev = pintfhdl->pintf_dev; + psdio = &psdiodev->intf_data; + + if (padapter->bSurpriseRemoved) + return err; + + func = psdio->func; + + if (unlikely((cnt == 1) || (cnt == 2))) { + int i; + u8 *pbuf = pdata; + + for (i = 0; i < cnt; i++) { + *(pbuf + i) = sdio_readb(func, addr + i, &err); + + if (err) + break; + } + return err; + } + + err = sdio_memcpy_fromio(func, pdata, addr, cnt); + + return err; +} + +/* + * Use CMD53 to read data from SDIO device. + * + * Parameters: + *psdio pointer of SDIO_DATA + *addr address to read + *cnt amount to read + *pdata pointer to put data, this should be a "DMA:able scratch buffer"! + * + * Return: + *0 Success + *others Fail + */ +s32 sd_read(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, void *pdata) +{ + struct adapter *padapter; + struct dvobj_priv *psdiodev; + struct sdio_data *psdio; + + struct sdio_func *func; + bool claim_needed; + s32 err = -EPERM; + + padapter = pintfhdl->padapter; + psdiodev = pintfhdl->pintf_dev; + psdio = &psdiodev->intf_data; + + if (padapter->bSurpriseRemoved) + return err; + + func = psdio->func; + claim_needed = rtw_sdio_claim_host_needed(func); + + if (claim_needed) + sdio_claim_host(func); + err = _sd_read(pintfhdl, addr, cnt, pdata); + if (claim_needed) + sdio_release_host(func); + return err; +} + +/* + * Use CMD53 to write data to SDIO device. + * This function MUST be called after sdio_claim_host() or + * in SDIO ISR(host had been claimed). + * + * Parameters: + *psdio pointer of SDIO_DATA + *addr address to write + *cnt amount to write + *pdata data pointer, this should be a "DMA:able scratch buffer"! + * + * Return: + *0 Success + *others Fail + */ +s32 _sd_write(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, void *pdata) +{ + struct adapter *padapter; + struct dvobj_priv *psdiodev; + struct sdio_data *psdio; + + struct sdio_func *func; + u32 size; + s32 err = -EPERM; + + padapter = pintfhdl->padapter; + psdiodev = pintfhdl->pintf_dev; + psdio = &psdiodev->intf_data; + + if (padapter->bSurpriseRemoved) + return err; + + func = psdio->func; +/* size = sdio_align_size(func, cnt); */ + + if (unlikely((cnt == 1) || (cnt == 2))) { + int i; + u8 *pbuf = pdata; + + for (i = 0; i < cnt; i++) { + sdio_writeb(func, *(pbuf + i), addr + i, &err); + if (err) + break; + } + + return err; + } + + size = cnt; + err = sdio_memcpy_toio(func, addr, pdata, size); + + return err; +} + +/* + * Use CMD53 to write data to SDIO device. + * + * Parameters: + * psdio pointer of SDIO_DATA + * addr address to write + * cnt amount to write + * pdata data pointer, this should be a "DMA:able scratch buffer"! + * + * Return: + * 0 Success + * others Fail + */ +s32 sd_write(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, void *pdata) +{ + struct adapter *padapter; + struct dvobj_priv *psdiodev; + struct sdio_data *psdio; + struct sdio_func *func; + bool claim_needed; + s32 err = -EPERM; + + padapter = pintfhdl->padapter; + psdiodev = pintfhdl->pintf_dev; + psdio = &psdiodev->intf_data; + + if (padapter->bSurpriseRemoved) + return err; + + func = psdio->func; + claim_needed = rtw_sdio_claim_host_needed(func); + + if (claim_needed) + sdio_claim_host(func); + err = _sd_write(pintfhdl, addr, cnt, pdata); + if (claim_needed) + sdio_release_host(func); + return err; +} diff --git a/drivers/staging/rtl8723bs/os_dep/wifi_regd.c b/drivers/staging/rtl8723bs/os_dep/wifi_regd.c new file mode 100644 index 0000000000..5eef1d68c6 --- /dev/null +++ b/drivers/staging/rtl8723bs/os_dep/wifi_regd.c @@ -0,0 +1,150 @@ +// SPDX-License-Identifier: GPL-2.0 +/****************************************************************************** + * + * Copyright(c) 2009-2010 Realtek Corporation. + * + *****************************************************************************/ + +#include <drv_types.h> +#include <rtw_debug.h> + +#include <rtw_wifi_regd.h> + +/* + * REG_RULE(freq start, freq end, bandwidth, max gain, eirp, reg_flags) + */ + +/* + * Only these channels all allow active + * scan on all world regulatory domains + */ + +/* 2G chan 01 - chan 11 */ +#define RTW_2GHZ_CH01_11 \ + REG_RULE(2412 - 10, 2462 + 10, 40, 0, 20, 0) + +/* + * We enable active scan on these a case + * by case basis by regulatory domain + */ + +/* 2G chan 12 - chan 13, PASSIV SCAN */ +#define RTW_2GHZ_CH12_13 \ + REG_RULE(2467 - 10, 2472 + 10, 40, 0, 20, \ + NL80211_RRF_PASSIVE_SCAN) + +static const struct ieee80211_regdomain rtw_regdom_rd = { + .n_reg_rules = 2, + .alpha2 = "99", + .reg_rules = { + RTW_2GHZ_CH01_11, + RTW_2GHZ_CH12_13, + } +}; + +static int rtw_ieee80211_channel_to_frequency(int chan, int band) +{ + /* NL80211_BAND_2GHZ */ + if (chan == 14) + return 2484; + else if (chan < 14) + return 2407 + chan * 5; + else + return 0; /* not supported */ +} + +static void _rtw_reg_apply_flags(struct wiphy *wiphy) +{ + struct adapter *padapter = wiphy_to_adapter(wiphy); + struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv; + struct rt_channel_info *channel_set = pmlmeext->channel_set; + u8 max_chan_nums = pmlmeext->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) { + for (j = 0; j < sband->n_channels; j++) { + ch = &sband->channels[j]; + + if (ch) + 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_ieee80211_channel_to_frequency(channel, + NL80211_BAND_2GHZ); + + ch = ieee80211_get_channel(wiphy, freq); + if (ch) { + if (channel_set[i].ScanType == SCAN_PASSIVE) + ch->flags = IEEE80211_CHAN_NO_IR; + else + ch->flags = 0; + } + } +} + +static int _rtw_reg_notifier_apply(struct wiphy *wiphy, + struct regulatory_request *request, + struct rtw_regulatory *reg) +{ + /* Hard code flags */ + _rtw_reg_apply_flags(wiphy); + return 0; +} + +static const struct ieee80211_regdomain *_rtw_regdomain_select(struct + rtw_regulatory + *reg) +{ + return &rtw_regdom_rd; +} + +static void _rtw_regd_init_wiphy(struct rtw_regulatory *reg, + struct wiphy *wiphy, + void (*reg_notifier)(struct wiphy *wiphy, + struct + regulatory_request * + request)) +{ + const struct ieee80211_regdomain *regd; + + wiphy->reg_notifier = reg_notifier; + + wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG; + wiphy->regulatory_flags &= ~REGULATORY_STRICT_REG; + wiphy->regulatory_flags &= ~REGULATORY_DISABLE_BEACON_HINTS; + + regd = _rtw_regdomain_select(reg); + wiphy_apply_custom_regulatory(wiphy, regd); + + /* Hard code flags */ + _rtw_reg_apply_flags(wiphy); +} + +void rtw_regd_init(struct wiphy *wiphy, + void (*reg_notifier)(struct wiphy *wiphy, + struct regulatory_request *request)) +{ + _rtw_regd_init_wiphy(NULL, wiphy, reg_notifier); +} + +void rtw_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request) +{ + struct rtw_regulatory *reg = NULL; + + _rtw_reg_notifier_apply(wiphy, request, reg); +} diff --git a/drivers/staging/rtl8723bs/os_dep/xmit_linux.c b/drivers/staging/rtl8723bs/os_dep/xmit_linux.c new file mode 100644 index 0000000000..1eeabfffd6 --- /dev/null +++ b/drivers/staging/rtl8723bs/os_dep/xmit_linux.c @@ -0,0 +1,226 @@ +// SPDX-License-Identifier: GPL-2.0 +/****************************************************************************** + * + * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved. + * + ******************************************************************************/ +#include <drv_types.h> +#include <rtw_debug.h> + + +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(struct sk_buff *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; +} + +signed int rtw_endofpktfile(struct pkt_file *pfile) +{ + if (pfile->pkt_len == 0) + return true; + return false; +} + +int rtw_os_xmit_resource_alloc(struct adapter *padapter, struct xmit_buf *pxmitbuf, u32 alloc_sz, u8 flag) +{ + if (alloc_sz > 0) { + pxmitbuf->pallocated_buf = rtw_zmalloc(alloc_sz); + if (!pxmitbuf->pallocated_buf) + return _FAIL; + + pxmitbuf->pbuf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(pxmitbuf->pallocated_buf), XMITBUF_ALIGN_SZ); + } + + return _SUCCESS; +} + +void rtw_os_xmit_resource_free(struct adapter *padapter, struct xmit_buf *pxmitbuf, u32 free_sz, u8 flag) +{ + if (free_sz > 0) + kfree(pxmitbuf->pallocated_buf); +} + +#define WMM_XMIT_THRESHOLD (NR_XMITFRAME * 2 / 5) + +void rtw_os_pkt_complete(struct adapter *padapter, struct sk_buff *pkt) +{ + u16 queue; + struct xmit_priv *pxmitpriv = &padapter->xmitpriv; + + queue = skb_get_queue_mapping(pkt); + if (padapter->registrypriv.wifi_spec) { + if (__netif_subqueue_stopped(padapter->pnetdev, queue) && + (pxmitpriv->hwxmits[queue].accnt < WMM_XMIT_THRESHOLD)) + netif_wake_subqueue(padapter->pnetdev, queue); + } else { + if (__netif_subqueue_stopped(padapter->pnetdev, queue)) + netif_wake_subqueue(padapter->pnetdev, queue); + } + + dev_kfree_skb_any(pkt); +} + +void rtw_os_xmit_complete(struct adapter *padapter, struct xmit_frame *pxframe) +{ + if (pxframe->pkt) + rtw_os_pkt_complete(padapter, pxframe->pkt); + + pxframe->pkt = NULL; +} + +void rtw_os_xmit_schedule(struct adapter *padapter) +{ + struct adapter *pri_adapter = padapter; + + if (!padapter) + return; + + if (!list_empty(&padapter->xmitpriv.pending_xmitbuf_queue.queue)) + complete(&pri_adapter->xmitpriv.xmit_comp); +} + +static void rtw_check_xmit_resource(struct adapter *padapter, struct sk_buff *pkt) +{ + struct xmit_priv *pxmitpriv = &padapter->xmitpriv; + u16 queue; + + queue = skb_get_queue_mapping(pkt); + if (padapter->registrypriv.wifi_spec) { + /* No free space for Tx, tx_worker is too slow */ + if (pxmitpriv->hwxmits[queue].accnt > WMM_XMIT_THRESHOLD) + netif_stop_subqueue(padapter->pnetdev, queue); + } else { + if (pxmitpriv->free_xmitframe_cnt <= 4) { + if (!netif_tx_queue_stopped(netdev_get_tx_queue(padapter->pnetdev, queue))) + netif_stop_subqueue(padapter->pnetdev, queue); + } + } +} + +static int rtw_mlcst2unicst(struct adapter *padapter, struct sk_buff *skb) +{ + struct sta_priv *pstapriv = &padapter->stapriv; + struct xmit_priv *pxmitpriv = &padapter->xmitpriv; + struct list_head *phead, *plist; + struct sk_buff *newskb; + struct sta_info *psta = NULL; + u8 chk_alive_num = 0; + char chk_alive_list[NUM_STA]; + u8 bc_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; + u8 null_addr[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + int i; + s32 res; + + spin_lock_bh(&pstapriv->asoc_list_lock); + phead = &pstapriv->asoc_list; + /* free sta asoc_queue */ + list_for_each(plist, phead) { + int stainfo_offset; + + psta = list_entry(plist, struct sta_info, asoc_list); + + stainfo_offset = rtw_stainfo_offset(pstapriv, psta); + if (stainfo_offset_valid(stainfo_offset)) { + chk_alive_list[chk_alive_num++] = stainfo_offset; + } + } + spin_unlock_bh(&pstapriv->asoc_list_lock); + + for (i = 0; i < chk_alive_num; i++) { + psta = rtw_get_stainfo_by_offset(pstapriv, chk_alive_list[i]); + if (!(psta->state & _FW_LINKED)) + continue; + + /* avoid come from STA1 and send back STA1 */ + if (!memcmp(psta->hwaddr, &skb->data[6], 6) || + !memcmp(psta->hwaddr, null_addr, 6) || + !memcmp(psta->hwaddr, bc_addr, 6)) + continue; + + newskb = rtw_skb_copy(skb); + + if (newskb) { + memcpy(newskb->data, psta->hwaddr, 6); + res = rtw_xmit(padapter, &newskb); + if (res < 0) { + pxmitpriv->tx_drop++; + dev_kfree_skb_any(newskb); + } + } else { + pxmitpriv->tx_drop++; + /* dev_kfree_skb_any(skb); */ + return false; /* Caller shall tx this multicast frame via normal way. */ + } + } + + dev_kfree_skb_any(skb); + return true; +} + +void _rtw_xmit_entry(struct sk_buff *pkt, struct net_device *pnetdev) +{ + struct adapter *padapter = rtw_netdev_priv(pnetdev); + struct xmit_priv *pxmitpriv = &padapter->xmitpriv; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + s32 res = 0; + + if (rtw_if_up(padapter) == false) + goto drop_packet; + + rtw_check_xmit_resource(padapter, pkt); + + if (!rtw_mc2u_disable + && check_fwstate(pmlmepriv, WIFI_AP_STATE) == true + && (IP_MCAST_MAC(pkt->data) + || ICMPV6_MCAST_MAC(pkt->data) + ) + && padapter->registrypriv.wifi_spec == 0) { + if (pxmitpriv->free_xmitframe_cnt > (NR_XMITFRAME / 4)) { + res = rtw_mlcst2unicst(padapter, pkt); + if (res) + return; + } + } + + res = rtw_xmit(padapter, &pkt); + if (res < 0) + goto drop_packet; + + return; + +drop_packet: + pxmitpriv->tx_drop++; + dev_kfree_skb_any(pkt); +} + +netdev_tx_t rtw_xmit_entry(struct sk_buff *pkt, struct net_device *pnetdev) +{ + if (pkt) + _rtw_xmit_entry(pkt, pnetdev); + + return NETDEV_TX_OK; +} |