Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright(c) 2009-2012  Realtek Corporation.*/
0003 
0004 #include "wifi.h"
0005 #include "core.h"
0006 #include "cam.h"
0007 #include "base.h"
0008 #include "ps.h"
0009 #include "pwrseqcmd.h"
0010 
0011 #include "btcoexist/rtl_btc.h"
0012 #include <linux/firmware.h>
0013 #include <linux/export.h>
0014 #include <net/cfg80211.h>
0015 
0016 u8 channel5g[CHANNEL_MAX_NUMBER_5G] = {
0017     36, 38, 40, 42, 44, 46, 48,     /* Band 1 */
0018     52, 54, 56, 58, 60, 62, 64,     /* Band 2 */
0019     100, 102, 104, 106, 108, 110, 112,  /* Band 3 */
0020     116, 118, 120, 122, 124, 126, 128,  /* Band 3 */
0021     132, 134, 136, 138, 140, 142, 144,  /* Band 3 */
0022     149, 151, 153, 155, 157, 159, 161,  /* Band 4 */
0023     165, 167, 169, 171, 173, 175, 177   /* Band 4 */
0024 };
0025 EXPORT_SYMBOL(channel5g);
0026 
0027 u8 channel5g_80m[CHANNEL_MAX_NUMBER_5G_80M] = {
0028     42, 58, 106, 122, 138, 155, 171
0029 };
0030 EXPORT_SYMBOL(channel5g_80m);
0031 
0032 void rtl_addr_delay(u32 addr)
0033 {
0034     if (addr == 0xfe)
0035         mdelay(50);
0036     else if (addr == 0xfd)
0037         msleep(5);
0038     else if (addr == 0xfc)
0039         msleep(1);
0040     else if (addr == 0xfb)
0041         usleep_range(50, 100);
0042     else if (addr == 0xfa)
0043         usleep_range(5, 10);
0044     else if (addr == 0xf9)
0045         usleep_range(1, 2);
0046 }
0047 EXPORT_SYMBOL(rtl_addr_delay);
0048 
0049 void rtl_rfreg_delay(struct ieee80211_hw *hw, enum radio_path rfpath, u32 addr,
0050              u32 mask, u32 data)
0051 {
0052     if (addr >= 0xf9 && addr <= 0xfe) {
0053         rtl_addr_delay(addr);
0054     } else {
0055         rtl_set_rfreg(hw, rfpath, addr, mask, data);
0056         udelay(1);
0057     }
0058 }
0059 EXPORT_SYMBOL(rtl_rfreg_delay);
0060 
0061 void rtl_bb_delay(struct ieee80211_hw *hw, u32 addr, u32 data)
0062 {
0063     if (addr >= 0xf9 && addr <= 0xfe) {
0064         rtl_addr_delay(addr);
0065     } else {
0066         rtl_set_bbreg(hw, addr, MASKDWORD, data);
0067         udelay(1);
0068     }
0069 }
0070 EXPORT_SYMBOL(rtl_bb_delay);
0071 
0072 static void rtl_fw_do_work(const struct firmware *firmware, void *context,
0073                bool is_wow)
0074 {
0075     struct ieee80211_hw *hw = context;
0076     struct rtl_priv *rtlpriv = rtl_priv(hw);
0077     int err;
0078 
0079     rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
0080         "Firmware callback routine entered!\n");
0081     if (!firmware) {
0082         if (rtlpriv->cfg->alt_fw_name) {
0083             err = request_firmware(&firmware,
0084                            rtlpriv->cfg->alt_fw_name,
0085                            rtlpriv->io.dev);
0086             pr_info("Loading alternative firmware %s\n",
0087                 rtlpriv->cfg->alt_fw_name);
0088             if (!err)
0089                 goto found_alt;
0090         }
0091         pr_err("Selected firmware is not available\n");
0092         rtlpriv->max_fw_size = 0;
0093         goto exit;
0094     }
0095 found_alt:
0096     if (firmware->size > rtlpriv->max_fw_size) {
0097         pr_err("Firmware is too big!\n");
0098         release_firmware(firmware);
0099         goto exit;
0100     }
0101     if (!is_wow) {
0102         memcpy(rtlpriv->rtlhal.pfirmware, firmware->data,
0103                firmware->size);
0104         rtlpriv->rtlhal.fwsize = firmware->size;
0105     } else {
0106         memcpy(rtlpriv->rtlhal.wowlan_firmware, firmware->data,
0107                firmware->size);
0108         rtlpriv->rtlhal.wowlan_fwsize = firmware->size;
0109     }
0110     release_firmware(firmware);
0111 
0112 exit:
0113     complete(&rtlpriv->firmware_loading_complete);
0114 }
0115 
0116 void rtl_fw_cb(const struct firmware *firmware, void *context)
0117 {
0118     rtl_fw_do_work(firmware, context, false);
0119 }
0120 EXPORT_SYMBOL(rtl_fw_cb);
0121 
0122 void rtl_wowlan_fw_cb(const struct firmware *firmware, void *context)
0123 {
0124     rtl_fw_do_work(firmware, context, true);
0125 }
0126 EXPORT_SYMBOL(rtl_wowlan_fw_cb);
0127 
0128 /*mutex for start & stop is must here. */
0129 static int rtl_op_start(struct ieee80211_hw *hw)
0130 {
0131     int err = 0;
0132     struct rtl_priv *rtlpriv = rtl_priv(hw);
0133     struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
0134 
0135     if (!is_hal_stop(rtlhal))
0136         return 0;
0137     if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
0138         return 0;
0139     mutex_lock(&rtlpriv->locks.conf_mutex);
0140     err = rtlpriv->intf_ops->adapter_start(hw);
0141     if (!err)
0142         rtl_watch_dog_timer_callback(&rtlpriv->works.watchdog_timer);
0143     mutex_unlock(&rtlpriv->locks.conf_mutex);
0144     return err;
0145 }
0146 
0147 static void rtl_op_stop(struct ieee80211_hw *hw)
0148 {
0149     struct rtl_priv *rtlpriv = rtl_priv(hw);
0150     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
0151     struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
0152     struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
0153     bool support_remote_wakeup = false;
0154 
0155     if (is_hal_stop(rtlhal))
0156         return;
0157 
0158     rtlpriv->cfg->ops->get_hw_reg(hw, HAL_DEF_WOWLAN,
0159                       (u8 *)(&support_remote_wakeup));
0160     /* here is must, because adhoc do stop and start,
0161      * but stop with RFOFF may cause something wrong,
0162      * like adhoc TP
0163      */
0164     if (unlikely(ppsc->rfpwr_state == ERFOFF))
0165         rtl_ips_nic_on(hw);
0166 
0167     mutex_lock(&rtlpriv->locks.conf_mutex);
0168     /* if wowlan supported, DON'T clear connected info */
0169     if (!(support_remote_wakeup &&
0170           rtlhal->enter_pnp_sleep)) {
0171         mac->link_state = MAC80211_NOLINK;
0172         eth_zero_addr(mac->bssid);
0173         mac->vendor = PEER_UNKNOWN;
0174 
0175         /* reset sec info */
0176         rtl_cam_reset_sec_info(hw);
0177 
0178         rtl_deinit_deferred_work(hw, false);
0179     }
0180     rtlpriv->intf_ops->adapter_stop(hw);
0181 
0182     mutex_unlock(&rtlpriv->locks.conf_mutex);
0183 }
0184 
0185 static void rtl_op_tx(struct ieee80211_hw *hw,
0186               struct ieee80211_tx_control *control,
0187               struct sk_buff *skb)
0188 {
0189     struct rtl_priv *rtlpriv = rtl_priv(hw);
0190     struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
0191     struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
0192     struct rtl_tcb_desc tcb_desc;
0193 
0194     memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
0195 
0196     if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
0197         goto err_free;
0198 
0199     if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
0200         goto err_free;
0201 
0202     if (!rtlpriv->intf_ops->waitq_insert(hw, control->sta, skb))
0203         rtlpriv->intf_ops->adapter_tx(hw, control->sta, skb, &tcb_desc);
0204     return;
0205 
0206 err_free:
0207     dev_kfree_skb_any(skb);
0208 }
0209 
0210 static int rtl_op_add_interface(struct ieee80211_hw *hw,
0211         struct ieee80211_vif *vif)
0212 {
0213     struct rtl_priv *rtlpriv = rtl_priv(hw);
0214     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
0215     int err = 0;
0216     u8 retry_limit = 0x30;
0217 
0218     if (mac->vif) {
0219         rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
0220             "vif has been set!! mac->vif = 0x%p\n", mac->vif);
0221         return -EOPNOTSUPP;
0222     }
0223 
0224     vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
0225 
0226     rtl_ips_nic_on(hw);
0227 
0228     mutex_lock(&rtlpriv->locks.conf_mutex);
0229     switch (ieee80211_vif_type_p2p(vif)) {
0230     case NL80211_IFTYPE_P2P_CLIENT:
0231         mac->p2p = P2P_ROLE_CLIENT;
0232         fallthrough;
0233     case NL80211_IFTYPE_STATION:
0234         if (mac->beacon_enabled == 1) {
0235             rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
0236                 "NL80211_IFTYPE_STATION\n");
0237             mac->beacon_enabled = 0;
0238             rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
0239                     rtlpriv->cfg->maps[RTL_IBSS_INT_MASKS]);
0240         }
0241         break;
0242     case NL80211_IFTYPE_ADHOC:
0243         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
0244             "NL80211_IFTYPE_ADHOC\n");
0245 
0246         mac->link_state = MAC80211_LINKED;
0247         rtlpriv->cfg->ops->set_bcn_reg(hw);
0248         if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
0249             mac->basic_rates = 0xfff;
0250         else
0251             mac->basic_rates = 0xff0;
0252         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
0253                 (u8 *)(&mac->basic_rates));
0254 
0255         retry_limit = 0x07;
0256         break;
0257     case NL80211_IFTYPE_P2P_GO:
0258         mac->p2p = P2P_ROLE_GO;
0259         fallthrough;
0260     case NL80211_IFTYPE_AP:
0261         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
0262             "NL80211_IFTYPE_AP\n");
0263 
0264         mac->link_state = MAC80211_LINKED;
0265         rtlpriv->cfg->ops->set_bcn_reg(hw);
0266         if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
0267             mac->basic_rates = 0xfff;
0268         else
0269             mac->basic_rates = 0xff0;
0270         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
0271                           (u8 *)(&mac->basic_rates));
0272 
0273         retry_limit = 0x07;
0274         break;
0275     case NL80211_IFTYPE_MESH_POINT:
0276         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
0277             "NL80211_IFTYPE_MESH_POINT\n");
0278 
0279         mac->link_state = MAC80211_LINKED;
0280         rtlpriv->cfg->ops->set_bcn_reg(hw);
0281         if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
0282             mac->basic_rates = 0xfff;
0283         else
0284             mac->basic_rates = 0xff0;
0285         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
0286                 (u8 *)(&mac->basic_rates));
0287 
0288         retry_limit = 0x07;
0289         break;
0290     default:
0291         pr_err("operation mode %d is not supported!\n",
0292                vif->type);
0293         err = -EOPNOTSUPP;
0294         goto out;
0295     }
0296 
0297     if (mac->p2p) {
0298         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
0299             "p2p role %x\n", vif->type);
0300         mac->basic_rates = 0xff0;/*disable cck rate for p2p*/
0301         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
0302                 (u8 *)(&mac->basic_rates));
0303     }
0304     mac->vif = vif;
0305     mac->opmode = vif->type;
0306     rtlpriv->cfg->ops->set_network_type(hw, vif->type);
0307     memcpy(mac->mac_addr, vif->addr, ETH_ALEN);
0308     rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ETHER_ADDR, mac->mac_addr);
0309 
0310     mac->retry_long = retry_limit;
0311     mac->retry_short = retry_limit;
0312     rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RETRY_LIMIT,
0313             (u8 *)(&retry_limit));
0314 out:
0315     mutex_unlock(&rtlpriv->locks.conf_mutex);
0316     return err;
0317 }
0318 
0319 static void rtl_op_remove_interface(struct ieee80211_hw *hw,
0320         struct ieee80211_vif *vif)
0321 {
0322     struct rtl_priv *rtlpriv = rtl_priv(hw);
0323     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
0324 
0325     mutex_lock(&rtlpriv->locks.conf_mutex);
0326 
0327     /* Free beacon resources */
0328     if (vif->type == NL80211_IFTYPE_AP ||
0329         vif->type == NL80211_IFTYPE_ADHOC ||
0330         vif->type == NL80211_IFTYPE_MESH_POINT) {
0331         if (mac->beacon_enabled == 1) {
0332             mac->beacon_enabled = 0;
0333             rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
0334                     rtlpriv->cfg->maps[RTL_IBSS_INT_MASKS]);
0335         }
0336     }
0337 
0338     /*
0339      *Note: We assume NL80211_IFTYPE_UNSPECIFIED as
0340      *NO LINK for our hardware.
0341      */
0342     mac->p2p = 0;
0343     mac->vif = NULL;
0344     mac->link_state = MAC80211_NOLINK;
0345     eth_zero_addr(mac->bssid);
0346     mac->vendor = PEER_UNKNOWN;
0347     mac->opmode = NL80211_IFTYPE_UNSPECIFIED;
0348     rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
0349 
0350     mutex_unlock(&rtlpriv->locks.conf_mutex);
0351 }
0352 
0353 static int rtl_op_change_interface(struct ieee80211_hw *hw,
0354                    struct ieee80211_vif *vif,
0355                    enum nl80211_iftype new_type, bool p2p)
0356 {
0357     struct rtl_priv *rtlpriv = rtl_priv(hw);
0358     int ret;
0359 
0360     rtl_op_remove_interface(hw, vif);
0361 
0362     vif->type = new_type;
0363     vif->p2p = p2p;
0364     ret = rtl_op_add_interface(hw, vif);
0365     rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
0366         "p2p  %x\n", p2p);
0367     return ret;
0368 }
0369 
0370 #ifdef CONFIG_PM
0371 static u16 crc16_ccitt(u8 data, u16 crc)
0372 {
0373     u8 shift_in, data_bit, crc_bit11, crc_bit4, crc_bit15;
0374     u8 i;
0375     u16 result;
0376 
0377     for (i = 0; i < 8; i++) {
0378         crc_bit15 = ((crc & BIT(15)) ? 1 : 0);
0379         data_bit  = (data & (BIT(0) << i) ? 1 : 0);
0380         shift_in = crc_bit15 ^ data_bit;
0381 
0382         result = crc << 1;
0383         if (shift_in == 0)
0384             result &= (~BIT(0));
0385         else
0386             result |= BIT(0);
0387 
0388         crc_bit11 = ((crc & BIT(11)) ? 1 : 0) ^ shift_in;
0389         if (crc_bit11 == 0)
0390             result &= (~BIT(12));
0391         else
0392             result |= BIT(12);
0393 
0394         crc_bit4 = ((crc & BIT(4)) ? 1 : 0) ^ shift_in;
0395         if (crc_bit4 == 0)
0396             result &= (~BIT(5));
0397         else
0398             result |= BIT(5);
0399 
0400         crc = result;
0401     }
0402 
0403     return crc;
0404 }
0405 
0406 static u16 _calculate_wol_pattern_crc(u8 *pattern, u16 len)
0407 {
0408     u16 crc = 0xffff;
0409     u32 i;
0410 
0411     for (i = 0; i < len; i++)
0412         crc = crc16_ccitt(pattern[i], crc);
0413 
0414     crc = ~crc;
0415 
0416     return crc;
0417 }
0418 
0419 static void _rtl_add_wowlan_patterns(struct ieee80211_hw *hw,
0420                      struct cfg80211_wowlan *wow)
0421 {
0422     struct rtl_priv *rtlpriv = rtl_priv(hw);
0423     struct rtl_mac *mac = &rtlpriv->mac80211;
0424     struct cfg80211_pkt_pattern *patterns = wow->patterns;
0425     struct rtl_wow_pattern rtl_pattern;
0426     const u8 *pattern_os, *mask_os;
0427     u8 mask[MAX_WOL_BIT_MASK_SIZE] = {0};
0428     u8 content[MAX_WOL_PATTERN_SIZE] = {0};
0429     u8 broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
0430     u8 multicast_addr1[2] = {0x33, 0x33};
0431     u8 multicast_addr2[3] = {0x01, 0x00, 0x5e};
0432     u8 i, mask_len;
0433     u16 j, len;
0434 
0435     for (i = 0; i < wow->n_patterns; i++) {
0436         memset(&rtl_pattern, 0, sizeof(struct rtl_wow_pattern));
0437         memset(mask, 0, MAX_WOL_BIT_MASK_SIZE);
0438         if (patterns[i].pattern_len < 0 ||
0439             patterns[i].pattern_len > MAX_WOL_PATTERN_SIZE) {
0440             rtl_dbg(rtlpriv, COMP_POWER, DBG_WARNING,
0441                 "Pattern[%d] is too long\n", i);
0442             continue;
0443         }
0444         pattern_os = patterns[i].pattern;
0445         mask_len = DIV_ROUND_UP(patterns[i].pattern_len, 8);
0446         mask_os = patterns[i].mask;
0447         RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
0448                   "pattern content\n", pattern_os,
0449                    patterns[i].pattern_len);
0450         RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
0451                   "mask content\n", mask_os, mask_len);
0452         /* 1. unicast? multicast? or broadcast? */
0453         if (memcmp(pattern_os, broadcast_addr, 6) == 0)
0454             rtl_pattern.type = BROADCAST_PATTERN;
0455         else if (memcmp(pattern_os, multicast_addr1, 2) == 0 ||
0456              memcmp(pattern_os, multicast_addr2, 3) == 0)
0457             rtl_pattern.type = MULTICAST_PATTERN;
0458         else if  (memcmp(pattern_os, mac->mac_addr, 6) == 0)
0459             rtl_pattern.type = UNICAST_PATTERN;
0460         else
0461             rtl_pattern.type = UNKNOWN_TYPE;
0462 
0463         /* 2. translate mask_from_os to mask_for_hw */
0464 
0465 /******************************************************************************
0466  * pattern from OS uses 'ethenet frame', like this:
0467 
0468            |    6   |    6   |   2  |     20    |  Variable  |  4  |
0469            |--------+--------+------+-----------+------------+-----|
0470            |    802.3 Mac Header    | IP Header | TCP Packet | FCS |
0471            |   DA   |   SA   | Type |
0472 
0473  * BUT, packet catched by our HW is in '802.11 frame', begin from LLC,
0474 
0475     |     24 or 30      |    6   |   2  |     20    |  Variable  |  4  |
0476     |-------------------+--------+------+-----------+------------+-----|
0477     | 802.11 MAC Header |       LLC     | IP Header | TCP Packet | FCS |
0478                 | Others | Tpye |
0479 
0480  * Therefore, we need translate mask_from_OS to mask_to_hw.
0481  * We should left-shift mask by 6 bits, then set the new bit[0~5] = 0,
0482  * because new mask[0~5] means 'SA', but our HW packet begins from LLC,
0483  * bit[0~5] corresponds to first 6 Bytes in LLC, they just don't match.
0484  ******************************************************************************/
0485 
0486         /* Shift 6 bits */
0487         for (j = 0; j < mask_len - 1; j++) {
0488             mask[j] = mask_os[j] >> 6;
0489             mask[j] |= (mask_os[j + 1] & 0x3F) << 2;
0490         }
0491         mask[j] = (mask_os[j] >> 6) & 0x3F;
0492         /* Set bit 0-5 to zero */
0493         mask[0] &= 0xC0;
0494 
0495         RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
0496                   "mask to hw\n", mask, mask_len);
0497         for (j = 0; j < (MAX_WOL_BIT_MASK_SIZE + 1) / 4; j++) {
0498             rtl_pattern.mask[j] = mask[j * 4];
0499             rtl_pattern.mask[j] |= (mask[j * 4 + 1] << 8);
0500             rtl_pattern.mask[j] |= (mask[j * 4 + 2] << 16);
0501             rtl_pattern.mask[j] |= (mask[j * 4 + 3] << 24);
0502         }
0503 
0504         /* To get the wake up pattern from the mask.
0505          * We do not count first 12 bits which means
0506          * DA[6] and SA[6] in the pattern to match HW design.
0507          */
0508         len = 0;
0509         for (j = 12; j < patterns[i].pattern_len; j++) {
0510             if ((mask_os[j / 8] >> (j % 8)) & 0x01) {
0511                 content[len] = pattern_os[j];
0512                 len++;
0513             }
0514         }
0515 
0516         RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
0517                   "pattern to hw\n", content, len);
0518         /* 3. calculate crc */
0519         rtl_pattern.crc = _calculate_wol_pattern_crc(content, len);
0520         rtl_dbg(rtlpriv, COMP_POWER, DBG_TRACE,
0521             "CRC_Remainder = 0x%x\n", rtl_pattern.crc);
0522 
0523         /* 4. write crc & mask_for_hw to hw */
0524         rtlpriv->cfg->ops->add_wowlan_pattern(hw, &rtl_pattern, i);
0525     }
0526     rtl_write_byte(rtlpriv, 0x698, wow->n_patterns);
0527 }
0528 
0529 static int rtl_op_suspend(struct ieee80211_hw *hw,
0530               struct cfg80211_wowlan *wow)
0531 {
0532     struct rtl_priv *rtlpriv = rtl_priv(hw);
0533     struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
0534     struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
0535 
0536     rtl_dbg(rtlpriv, COMP_POWER, DBG_DMESG, "\n");
0537     if (WARN_ON(!wow))
0538         return -EINVAL;
0539 
0540     /* to resolve s4 can not wake up*/
0541     rtlhal->last_suspend_sec = ktime_get_real_seconds();
0542 
0543     if ((ppsc->wo_wlan_mode & WAKE_ON_PATTERN_MATCH) && wow->n_patterns)
0544         _rtl_add_wowlan_patterns(hw, wow);
0545 
0546     rtlhal->driver_is_goingto_unload = true;
0547     rtlhal->enter_pnp_sleep = true;
0548 
0549     rtl_lps_leave(hw, true);
0550     rtl_op_stop(hw);
0551     device_set_wakeup_enable(wiphy_dev(hw->wiphy), true);
0552     return 0;
0553 }
0554 
0555 static int rtl_op_resume(struct ieee80211_hw *hw)
0556 {
0557     struct rtl_priv *rtlpriv = rtl_priv(hw);
0558     struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
0559     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
0560     time64_t now;
0561 
0562     rtl_dbg(rtlpriv, COMP_POWER, DBG_DMESG, "\n");
0563     rtlhal->driver_is_goingto_unload = false;
0564     rtlhal->enter_pnp_sleep = false;
0565     rtlhal->wake_from_pnp_sleep = true;
0566 
0567     /* to resolve s4 can not wake up*/
0568     now = ktime_get_real_seconds();
0569     if (now - rtlhal->last_suspend_sec < 5)
0570         return -1;
0571 
0572     rtl_op_start(hw);
0573     device_set_wakeup_enable(wiphy_dev(hw->wiphy), false);
0574     ieee80211_resume_disconnect(mac->vif);
0575     rtlhal->wake_from_pnp_sleep = false;
0576     return 0;
0577 }
0578 #endif
0579 
0580 static int rtl_op_config(struct ieee80211_hw *hw, u32 changed)
0581 {
0582     struct rtl_priv *rtlpriv = rtl_priv(hw);
0583     struct rtl_phy *rtlphy = &(rtlpriv->phy);
0584     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
0585     struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
0586     struct ieee80211_conf *conf = &hw->conf;
0587 
0588     if (mac->skip_scan)
0589         return 1;
0590 
0591     mutex_lock(&rtlpriv->locks.conf_mutex);
0592     if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {  /* BIT(2)*/
0593         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
0594             "IEEE80211_CONF_CHANGE_LISTEN_INTERVAL\n");
0595     }
0596 
0597     /*For IPS */
0598     if (changed & IEEE80211_CONF_CHANGE_IDLE) {
0599         if (hw->conf.flags & IEEE80211_CONF_IDLE)
0600             rtl_ips_nic_off(hw);
0601         else
0602             rtl_ips_nic_on(hw);
0603     } else {
0604         /*
0605          *although rfoff may not cause by ips, but we will
0606          *check the reason in set_rf_power_state function
0607          */
0608         if (unlikely(ppsc->rfpwr_state == ERFOFF))
0609             rtl_ips_nic_on(hw);
0610     }
0611 
0612     /*For LPS */
0613     if ((changed & IEEE80211_CONF_CHANGE_PS) &&
0614         rtlpriv->psc.swctrl_lps && !rtlpriv->psc.fwctrl_lps) {
0615         cancel_delayed_work(&rtlpriv->works.ps_work);
0616         cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
0617         if (conf->flags & IEEE80211_CONF_PS) {
0618             rtlpriv->psc.sw_ps_enabled = true;
0619             /* sleep here is must, or we may recv the beacon and
0620              * cause mac80211 into wrong ps state, this will cause
0621              * power save nullfunc send fail, and further cause
0622              * pkt loss, So sleep must quickly but not immediatly
0623              * because that will cause nullfunc send by mac80211
0624              * fail, and cause pkt loss, we have tested that 5mA
0625              * is worked very well */
0626             if (!rtlpriv->psc.multi_buffered)
0627                 queue_delayed_work(rtlpriv->works.rtl_wq,
0628                            &rtlpriv->works.ps_work,
0629                            MSECS(5));
0630         } else {
0631             rtl_swlps_rf_awake(hw);
0632             rtlpriv->psc.sw_ps_enabled = false;
0633         }
0634     }
0635 
0636     if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
0637         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
0638             "IEEE80211_CONF_CHANGE_RETRY_LIMITS %x\n",
0639             hw->conf.long_frame_max_tx_count);
0640         /* brought up everything changes (changed == ~0) indicates first
0641          * open, so use our default value instead of that of wiphy.
0642          */
0643         if (changed != ~0) {
0644             mac->retry_long = hw->conf.long_frame_max_tx_count;
0645             mac->retry_short = hw->conf.long_frame_max_tx_count;
0646             rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RETRY_LIMIT,
0647                 (u8 *)(&hw->conf.long_frame_max_tx_count));
0648         }
0649     }
0650 
0651     if (changed & IEEE80211_CONF_CHANGE_CHANNEL &&
0652         !rtlpriv->proximity.proxim_on) {
0653         struct ieee80211_channel *channel = hw->conf.chandef.chan;
0654         enum nl80211_chan_width width = hw->conf.chandef.width;
0655         enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
0656         u8 wide_chan = (u8) channel->hw_value;
0657 
0658         /* channel_type is for 20&40M */
0659         if (width < NL80211_CHAN_WIDTH_80)
0660             channel_type =
0661                 cfg80211_get_chandef_type(&hw->conf.chandef);
0662         if (mac->act_scanning)
0663             mac->n_channels++;
0664 
0665         if (rtlpriv->dm.supp_phymode_switch &&
0666             mac->link_state < MAC80211_LINKED &&
0667             !mac->act_scanning) {
0668             if (rtlpriv->cfg->ops->chk_switch_dmdp)
0669                 rtlpriv->cfg->ops->chk_switch_dmdp(hw);
0670         }
0671 
0672         /*
0673          *because we should back channel to
0674          *current_network.chan in scanning,
0675          *So if set_chan == current_network.chan
0676          *we should set it.
0677          *because mac80211 tell us wrong bw40
0678          *info for cisco1253 bw20, so we modify
0679          *it here based on UPPER & LOWER
0680          */
0681 
0682         if (width >= NL80211_CHAN_WIDTH_80) {
0683             if (width == NL80211_CHAN_WIDTH_80) {
0684                 u32 center = hw->conf.chandef.center_freq1;
0685                 u32 primary =
0686                 (u32)hw->conf.chandef.chan->center_freq;
0687 
0688                 rtlphy->current_chan_bw =
0689                     HT_CHANNEL_WIDTH_80;
0690                 mac->bw_80 = true;
0691                 mac->bw_40 = true;
0692                 if (center > primary) {
0693                     mac->cur_80_prime_sc =
0694                     PRIME_CHNL_OFFSET_LOWER;
0695                     if (center - primary == 10) {
0696                         mac->cur_40_prime_sc =
0697                         PRIME_CHNL_OFFSET_UPPER;
0698 
0699                         wide_chan += 2;
0700                     } else if (center - primary == 30) {
0701                         mac->cur_40_prime_sc =
0702                         PRIME_CHNL_OFFSET_LOWER;
0703 
0704                         wide_chan += 6;
0705                     }
0706                 } else {
0707                     mac->cur_80_prime_sc =
0708                     PRIME_CHNL_OFFSET_UPPER;
0709                     if (primary - center == 10) {
0710                         mac->cur_40_prime_sc =
0711                         PRIME_CHNL_OFFSET_LOWER;
0712 
0713                         wide_chan -= 2;
0714                     } else if (primary - center == 30) {
0715                         mac->cur_40_prime_sc =
0716                         PRIME_CHNL_OFFSET_UPPER;
0717 
0718                         wide_chan -= 6;
0719                     }
0720                 }
0721             }
0722         } else {
0723             switch (channel_type) {
0724             case NL80211_CHAN_HT20:
0725             case NL80211_CHAN_NO_HT:
0726                     /* SC */
0727                     mac->cur_40_prime_sc =
0728                         PRIME_CHNL_OFFSET_DONT_CARE;
0729                     rtlphy->current_chan_bw =
0730                         HT_CHANNEL_WIDTH_20;
0731                     mac->bw_40 = false;
0732                     mac->bw_80 = false;
0733                     break;
0734             case NL80211_CHAN_HT40MINUS:
0735                     /* SC */
0736                     mac->cur_40_prime_sc =
0737                         PRIME_CHNL_OFFSET_UPPER;
0738                     rtlphy->current_chan_bw =
0739                         HT_CHANNEL_WIDTH_20_40;
0740                     mac->bw_40 = true;
0741                     mac->bw_80 = false;
0742 
0743                     /*wide channel */
0744                     wide_chan -= 2;
0745 
0746                     break;
0747             case NL80211_CHAN_HT40PLUS:
0748                     /* SC */
0749                     mac->cur_40_prime_sc =
0750                         PRIME_CHNL_OFFSET_LOWER;
0751                     rtlphy->current_chan_bw =
0752                         HT_CHANNEL_WIDTH_20_40;
0753                     mac->bw_40 = true;
0754                     mac->bw_80 = false;
0755 
0756                     /*wide channel */
0757                     wide_chan += 2;
0758 
0759                     break;
0760             default:
0761                     mac->bw_40 = false;
0762                     mac->bw_80 = false;
0763                     pr_err("switch case %#x not processed\n",
0764                            channel_type);
0765                     break;
0766             }
0767         }
0768 
0769         if (wide_chan <= 0)
0770             wide_chan = 1;
0771 
0772         /* In scanning, when before we offchannel we may send a ps=1
0773          * null to AP, and then we may send a ps = 0 null to AP quickly,
0774          * but first null may have caused AP to put lots of packet to
0775          * hw tx buffer. These packets must be tx'd before we go off
0776          * channel so we must delay more time to let AP flush these
0777          * packets before going offchannel, or dis-association or
0778          * delete BA will be caused by AP
0779          */
0780         if (rtlpriv->mac80211.offchan_delay) {
0781             rtlpriv->mac80211.offchan_delay = false;
0782             mdelay(50);
0783         }
0784 
0785         rtlphy->current_channel = wide_chan;
0786 
0787         rtlpriv->cfg->ops->switch_channel(hw);
0788         rtlpriv->cfg->ops->set_channel_access(hw);
0789         rtlpriv->cfg->ops->set_bw_mode(hw, channel_type);
0790     }
0791 
0792     mutex_unlock(&rtlpriv->locks.conf_mutex);
0793 
0794     return 0;
0795 }
0796 
0797 static void rtl_op_configure_filter(struct ieee80211_hw *hw,
0798                     unsigned int changed_flags,
0799                     unsigned int *new_flags, u64 multicast)
0800 {
0801     bool update_rcr = false;
0802     struct rtl_priv *rtlpriv = rtl_priv(hw);
0803     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
0804 
0805     *new_flags &= RTL_SUPPORTED_FILTERS;
0806     if (0 == changed_flags)
0807         return;
0808 
0809     /*TODO: we disable broadcast now, so enable here */
0810     if (changed_flags & FIF_ALLMULTI) {
0811         if (*new_flags & FIF_ALLMULTI) {
0812             mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AM] |
0813                 rtlpriv->cfg->maps[MAC_RCR_AB];
0814             rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
0815                 "Enable receive multicast frame\n");
0816         } else {
0817             mac->rx_conf &= ~(rtlpriv->cfg->maps[MAC_RCR_AM] |
0818                       rtlpriv->cfg->maps[MAC_RCR_AB]);
0819             rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
0820                 "Disable receive multicast frame\n");
0821         }
0822         update_rcr = true;
0823     }
0824 
0825     if (changed_flags & FIF_FCSFAIL) {
0826         if (*new_flags & FIF_FCSFAIL) {
0827             mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACRC32];
0828             rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
0829                 "Enable receive FCS error frame\n");
0830         } else {
0831             mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACRC32];
0832             rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
0833                 "Disable receive FCS error frame\n");
0834         }
0835         if (!update_rcr)
0836             update_rcr = true;
0837     }
0838 
0839     /* if ssid not set to hw don't check bssid
0840      * here just used for linked scanning, & linked
0841      * and nolink check bssid is set in set network_type
0842      */
0843     if (changed_flags & FIF_BCN_PRBRESP_PROMISC &&
0844         mac->link_state >= MAC80211_LINKED) {
0845         if (mac->opmode != NL80211_IFTYPE_AP &&
0846             mac->opmode != NL80211_IFTYPE_MESH_POINT) {
0847             if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
0848                 rtlpriv->cfg->ops->set_chk_bssid(hw, false);
0849             else
0850                 rtlpriv->cfg->ops->set_chk_bssid(hw, true);
0851             if (update_rcr)
0852                 update_rcr = false;
0853         }
0854     }
0855 
0856     if (changed_flags & FIF_CONTROL) {
0857         if (*new_flags & FIF_CONTROL) {
0858             mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACF];
0859 
0860             rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
0861                 "Enable receive control frame.\n");
0862         } else {
0863             mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACF];
0864             rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
0865                 "Disable receive control frame.\n");
0866         }
0867         if (!update_rcr)
0868             update_rcr = true;
0869     }
0870 
0871     if (changed_flags & FIF_OTHER_BSS) {
0872         if (*new_flags & FIF_OTHER_BSS) {
0873             mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AAP];
0874             rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
0875                 "Enable receive other BSS's frame.\n");
0876         } else {
0877             mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_AAP];
0878             rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
0879                 "Disable receive other BSS's frame.\n");
0880         }
0881         if (!update_rcr)
0882             update_rcr = true;
0883     }
0884 
0885     if (update_rcr)
0886         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RCR,
0887                           (u8 *)(&mac->rx_conf));
0888 }
0889 
0890 static int rtl_op_sta_add(struct ieee80211_hw *hw,
0891              struct ieee80211_vif *vif,
0892              struct ieee80211_sta *sta)
0893 {
0894     struct rtl_priv *rtlpriv = rtl_priv(hw);
0895     struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
0896     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
0897     struct rtl_sta_info *sta_entry;
0898 
0899     if (sta) {
0900         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
0901         spin_lock_bh(&rtlpriv->locks.entry_list_lock);
0902         list_add_tail(&sta_entry->list, &rtlpriv->entry_list);
0903         spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
0904         if (rtlhal->current_bandtype == BAND_ON_2_4G) {
0905             sta_entry->wireless_mode = WIRELESS_MODE_G;
0906             if (sta->deflink.supp_rates[0] <= 0xf)
0907                 sta_entry->wireless_mode = WIRELESS_MODE_B;
0908             if (sta->deflink.ht_cap.ht_supported)
0909                 sta_entry->wireless_mode = WIRELESS_MODE_N_24G;
0910 
0911             if (vif->type == NL80211_IFTYPE_ADHOC)
0912                 sta_entry->wireless_mode = WIRELESS_MODE_G;
0913         } else if (rtlhal->current_bandtype == BAND_ON_5G) {
0914             sta_entry->wireless_mode = WIRELESS_MODE_A;
0915             if (sta->deflink.ht_cap.ht_supported)
0916                 sta_entry->wireless_mode = WIRELESS_MODE_N_5G;
0917             if (sta->deflink.vht_cap.vht_supported)
0918                 sta_entry->wireless_mode = WIRELESS_MODE_AC_5G;
0919 
0920             if (vif->type == NL80211_IFTYPE_ADHOC)
0921                 sta_entry->wireless_mode = WIRELESS_MODE_A;
0922         }
0923         /*disable cck rate for p2p*/
0924         if (mac->p2p)
0925             sta->deflink.supp_rates[0] &= 0xfffffff0;
0926 
0927         memcpy(sta_entry->mac_addr, sta->addr, ETH_ALEN);
0928         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_DMESG,
0929             "Add sta addr is %pM\n", sta->addr);
0930         rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0, true);
0931     }
0932 
0933     return 0;
0934 }
0935 
0936 static int rtl_op_sta_remove(struct ieee80211_hw *hw,
0937                 struct ieee80211_vif *vif,
0938                 struct ieee80211_sta *sta)
0939 {
0940     struct rtl_priv *rtlpriv = rtl_priv(hw);
0941     struct rtl_sta_info *sta_entry;
0942 
0943     if (sta) {
0944         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_DMESG,
0945             "Remove sta addr is %pM\n", sta->addr);
0946         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
0947         sta_entry->wireless_mode = 0;
0948         sta_entry->ratr_index = 0;
0949         spin_lock_bh(&rtlpriv->locks.entry_list_lock);
0950         list_del(&sta_entry->list);
0951         spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
0952     }
0953     return 0;
0954 }
0955 
0956 static int _rtl_get_hal_qnum(u16 queue)
0957 {
0958     int qnum;
0959 
0960     switch (queue) {
0961     case 0:
0962         qnum = AC3_VO;
0963         break;
0964     case 1:
0965         qnum = AC2_VI;
0966         break;
0967     case 2:
0968         qnum = AC0_BE;
0969         break;
0970     case 3:
0971         qnum = AC1_BK;
0972         break;
0973     default:
0974         qnum = AC0_BE;
0975         break;
0976     }
0977     return qnum;
0978 }
0979 
0980 /*
0981  *for mac80211 VO = 0, VI = 1, BE = 2, BK = 3
0982  *for rtl819x  BE = 0, BK = 1, VI = 2, VO = 3
0983  */
0984 static int rtl_op_conf_tx(struct ieee80211_hw *hw,
0985               struct ieee80211_vif *vif,
0986               unsigned int link_id, u16 queue,
0987               const struct ieee80211_tx_queue_params *param)
0988 {
0989     struct rtl_priv *rtlpriv = rtl_priv(hw);
0990     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
0991     int aci;
0992 
0993     if (queue >= AC_MAX) {
0994         rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
0995             "queue number %d is incorrect!\n", queue);
0996         return -EINVAL;
0997     }
0998 
0999     aci = _rtl_get_hal_qnum(queue);
1000     mac->ac[aci].aifs = param->aifs;
1001     mac->ac[aci].cw_min = cpu_to_le16(param->cw_min);
1002     mac->ac[aci].cw_max = cpu_to_le16(param->cw_max);
1003     mac->ac[aci].tx_op = cpu_to_le16(param->txop);
1004     memcpy(&mac->edca_param[aci], param, sizeof(*param));
1005     rtlpriv->cfg->ops->set_qos(hw, aci);
1006     return 0;
1007 }
1008 
1009 static void send_beacon_frame(struct ieee80211_hw *hw,
1010                   struct ieee80211_vif *vif)
1011 {
1012     struct rtl_priv *rtlpriv = rtl_priv(hw);
1013     struct sk_buff *skb = ieee80211_beacon_get(hw, vif, 0);
1014     struct rtl_tcb_desc tcb_desc;
1015 
1016     if (skb) {
1017         memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
1018         rtlpriv->intf_ops->adapter_tx(hw, NULL, skb, &tcb_desc);
1019     }
1020 }
1021 
1022 void rtl_update_beacon_work_callback(struct work_struct *work)
1023 {
1024     struct rtl_works *rtlworks =
1025         container_of(work, struct rtl_works, update_beacon_work);
1026     struct ieee80211_hw *hw = rtlworks->hw;
1027     struct rtl_priv *rtlpriv = rtl_priv(hw);
1028     struct ieee80211_vif *vif = rtlpriv->mac80211.vif;
1029 
1030     if (!vif) {
1031         WARN_ONCE(true, "no vif to update beacon\n");
1032         return;
1033     }
1034 
1035     mutex_lock(&rtlpriv->locks.conf_mutex);
1036     send_beacon_frame(hw, vif);
1037     mutex_unlock(&rtlpriv->locks.conf_mutex);
1038 }
1039 EXPORT_SYMBOL_GPL(rtl_update_beacon_work_callback);
1040 
1041 static void rtl_op_bss_info_changed(struct ieee80211_hw *hw,
1042                     struct ieee80211_vif *vif,
1043                     struct ieee80211_bss_conf *bss_conf,
1044                     u64 changed)
1045 {
1046     struct rtl_priv *rtlpriv = rtl_priv(hw);
1047     struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1048     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1049     struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1050 
1051     mutex_lock(&rtlpriv->locks.conf_mutex);
1052     if (vif->type == NL80211_IFTYPE_ADHOC ||
1053         vif->type == NL80211_IFTYPE_AP ||
1054         vif->type == NL80211_IFTYPE_MESH_POINT) {
1055         if (changed & BSS_CHANGED_BEACON ||
1056             (changed & BSS_CHANGED_BEACON_ENABLED &&
1057              bss_conf->enable_beacon)) {
1058             if (mac->beacon_enabled == 0) {
1059                 rtl_dbg(rtlpriv, COMP_MAC80211, DBG_DMESG,
1060                     "BSS_CHANGED_BEACON_ENABLED\n");
1061 
1062                 /*start hw beacon interrupt. */
1063                 /*rtlpriv->cfg->ops->set_bcn_reg(hw); */
1064                 mac->beacon_enabled = 1;
1065                 rtlpriv->cfg->ops->update_interrupt_mask(hw,
1066                         rtlpriv->cfg->maps
1067                         [RTL_IBSS_INT_MASKS], 0);
1068 
1069                 if (rtlpriv->cfg->ops->linked_set_reg)
1070                     rtlpriv->cfg->ops->linked_set_reg(hw);
1071                 send_beacon_frame(hw, vif);
1072             }
1073         }
1074         if ((changed & BSS_CHANGED_BEACON_ENABLED &&
1075             !bss_conf->enable_beacon)) {
1076             if (mac->beacon_enabled == 1) {
1077                 rtl_dbg(rtlpriv, COMP_MAC80211, DBG_DMESG,
1078                     "ADHOC DISABLE BEACON\n");
1079 
1080                 mac->beacon_enabled = 0;
1081                 rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
1082                         rtlpriv->cfg->maps
1083                         [RTL_IBSS_INT_MASKS]);
1084             }
1085         }
1086         if (changed & BSS_CHANGED_BEACON_INT) {
1087             rtl_dbg(rtlpriv, COMP_BEACON, DBG_TRACE,
1088                 "BSS_CHANGED_BEACON_INT\n");
1089             mac->beacon_interval = bss_conf->beacon_int;
1090             rtlpriv->cfg->ops->set_bcn_intv(hw);
1091         }
1092     }
1093 
1094     /*TODO: reference to enum ieee80211_bss_change */
1095     if (changed & BSS_CHANGED_ASSOC) {
1096         u8 mstatus;
1097 
1098         if (vif->cfg.assoc) {
1099             struct ieee80211_sta *sta = NULL;
1100             u8 keep_alive = 10;
1101 
1102             mstatus = RT_MEDIA_CONNECT;
1103             /* we should reset all sec info & cam
1104              * before set cam after linked, we should not
1105              * reset in disassoc, that will cause tkip->wep
1106              * fail because some flag will be wrong */
1107             /* reset sec info */
1108             rtl_cam_reset_sec_info(hw);
1109             /* reset cam to fix wep fail issue
1110              * when change from wpa to wep */
1111             rtl_cam_reset_all_entry(hw);
1112 
1113             mac->link_state = MAC80211_LINKED;
1114             mac->cnt_after_linked = 0;
1115             mac->assoc_id = vif->cfg.aid;
1116             memcpy(mac->bssid, bss_conf->bssid, ETH_ALEN);
1117 
1118             if (rtlpriv->cfg->ops->linked_set_reg)
1119                 rtlpriv->cfg->ops->linked_set_reg(hw);
1120 
1121             rcu_read_lock();
1122             sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1123             if (!sta) {
1124                 rcu_read_unlock();
1125                 goto out;
1126             }
1127             rtl_dbg(rtlpriv, COMP_EASY_CONCURRENT, DBG_LOUD,
1128                 "send PS STATIC frame\n");
1129             if (rtlpriv->dm.supp_phymode_switch) {
1130                 if (sta->deflink.ht_cap.ht_supported)
1131                     rtl_send_smps_action(hw, sta,
1132                             IEEE80211_SMPS_STATIC);
1133             }
1134 
1135             if (rtlhal->current_bandtype == BAND_ON_5G) {
1136                 mac->mode = WIRELESS_MODE_A;
1137             } else {
1138                 if (sta->deflink.supp_rates[0] <= 0xf)
1139                     mac->mode = WIRELESS_MODE_B;
1140                 else
1141                     mac->mode = WIRELESS_MODE_G;
1142             }
1143 
1144             if (sta->deflink.ht_cap.ht_supported) {
1145                 if (rtlhal->current_bandtype == BAND_ON_2_4G)
1146                     mac->mode = WIRELESS_MODE_N_24G;
1147                 else
1148                     mac->mode = WIRELESS_MODE_N_5G;
1149             }
1150 
1151             if (sta->deflink.vht_cap.vht_supported) {
1152                 if (rtlhal->current_bandtype == BAND_ON_5G)
1153                     mac->mode = WIRELESS_MODE_AC_5G;
1154                 else
1155                     mac->mode = WIRELESS_MODE_AC_24G;
1156             }
1157 
1158             if (vif->type == NL80211_IFTYPE_STATION)
1159                 rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0,
1160                                    true);
1161             rcu_read_unlock();
1162 
1163             /* to avoid AP Disassociation caused by inactivity */
1164             rtlpriv->cfg->ops->set_hw_reg(hw,
1165                               HW_VAR_KEEP_ALIVE,
1166                               (u8 *)(&keep_alive));
1167 
1168             rtl_dbg(rtlpriv, COMP_MAC80211, DBG_DMESG,
1169                 "BSS_CHANGED_ASSOC\n");
1170         } else {
1171             struct cfg80211_bss *bss = NULL;
1172 
1173             mstatus = RT_MEDIA_DISCONNECT;
1174 
1175             if (mac->link_state == MAC80211_LINKED)
1176                 rtl_lps_leave(hw, true);
1177             if (ppsc->p2p_ps_info.p2p_ps_mode > P2P_PS_NONE)
1178                 rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
1179             mac->link_state = MAC80211_NOLINK;
1180 
1181             bss = cfg80211_get_bss(hw->wiphy, NULL,
1182                            (u8 *)mac->bssid, NULL, 0,
1183                            IEEE80211_BSS_TYPE_ESS,
1184                            IEEE80211_PRIVACY_OFF);
1185 
1186             rtl_dbg(rtlpriv, COMP_MAC80211, DBG_DMESG,
1187                 "bssid = %pMF\n", mac->bssid);
1188 
1189             if (bss) {
1190                 cfg80211_unlink_bss(hw->wiphy, bss);
1191                 cfg80211_put_bss(hw->wiphy, bss);
1192                 rtl_dbg(rtlpriv, COMP_MAC80211, DBG_DMESG,
1193                     "cfg80211_unlink !!\n");
1194             }
1195 
1196             eth_zero_addr(mac->bssid);
1197             mac->vendor = PEER_UNKNOWN;
1198             mac->mode = 0;
1199 
1200             if (rtlpriv->dm.supp_phymode_switch) {
1201                 if (rtlpriv->cfg->ops->chk_switch_dmdp)
1202                     rtlpriv->cfg->ops->chk_switch_dmdp(hw);
1203             }
1204             rtl_dbg(rtlpriv, COMP_MAC80211, DBG_DMESG,
1205                 "BSS_CHANGED_UN_ASSOC\n");
1206         }
1207         rtlpriv->cfg->ops->set_network_type(hw, vif->type);
1208         /* For FW LPS:
1209          * To tell firmware we have connected or disconnected
1210          */
1211         rtlpriv->cfg->ops->set_hw_reg(hw,
1212                           HW_VAR_H2C_FW_JOINBSSRPT,
1213                           (u8 *)(&mstatus));
1214         ppsc->report_linked = (mstatus == RT_MEDIA_CONNECT) ?
1215                       true : false;
1216 
1217         if (rtlpriv->cfg->ops->get_btc_status())
1218             rtlpriv->btcoexist.btc_ops->btc_mediastatus_notify(
1219                             rtlpriv, mstatus);
1220     }
1221 
1222     if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1223         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_TRACE,
1224             "BSS_CHANGED_ERP_CTS_PROT\n");
1225         mac->use_cts_protect = bss_conf->use_cts_prot;
1226     }
1227 
1228     if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1229         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
1230             "BSS_CHANGED_ERP_PREAMBLE use short preamble:%x\n",
1231               bss_conf->use_short_preamble);
1232 
1233         mac->short_preamble = bss_conf->use_short_preamble;
1234         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ACK_PREAMBLE,
1235                           (u8 *)(&mac->short_preamble));
1236     }
1237 
1238     if (changed & BSS_CHANGED_ERP_SLOT) {
1239         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_TRACE,
1240             "BSS_CHANGED_ERP_SLOT\n");
1241 
1242         if (bss_conf->use_short_slot)
1243             mac->slot_time = RTL_SLOT_TIME_9;
1244         else
1245             mac->slot_time = RTL_SLOT_TIME_20;
1246 
1247         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SLOT_TIME,
1248                           (u8 *)(&mac->slot_time));
1249     }
1250 
1251     if (changed & BSS_CHANGED_HT) {
1252         struct ieee80211_sta *sta = NULL;
1253 
1254         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_TRACE,
1255             "BSS_CHANGED_HT\n");
1256 
1257         rcu_read_lock();
1258         sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1259         if (sta) {
1260             if (sta->deflink.ht_cap.ampdu_density >
1261                 mac->current_ampdu_density)
1262                 mac->current_ampdu_density =
1263                     sta->deflink.ht_cap.ampdu_density;
1264             if (sta->deflink.ht_cap.ampdu_factor <
1265                 mac->current_ampdu_factor)
1266                 mac->current_ampdu_factor =
1267                     sta->deflink.ht_cap.ampdu_factor;
1268         }
1269         rcu_read_unlock();
1270 
1271         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SHORTGI_DENSITY,
1272                           (u8 *)(&mac->max_mss_density));
1273         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_FACTOR,
1274                           &mac->current_ampdu_factor);
1275         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_MIN_SPACE,
1276                           &mac->current_ampdu_density);
1277     }
1278 
1279     if (changed & BSS_CHANGED_BSSID) {
1280         u32 basic_rates;
1281         struct ieee80211_sta *sta = NULL;
1282 
1283         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BSSID,
1284                           (u8 *)bss_conf->bssid);
1285 
1286         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_DMESG,
1287             "bssid: %pM\n", bss_conf->bssid);
1288 
1289         mac->vendor = PEER_UNKNOWN;
1290         memcpy(mac->bssid, bss_conf->bssid, ETH_ALEN);
1291 
1292         rcu_read_lock();
1293         sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1294         if (!sta) {
1295             rcu_read_unlock();
1296             goto out;
1297         }
1298 
1299         if (rtlhal->current_bandtype == BAND_ON_5G) {
1300             mac->mode = WIRELESS_MODE_A;
1301         } else {
1302             if (sta->deflink.supp_rates[0] <= 0xf)
1303                 mac->mode = WIRELESS_MODE_B;
1304             else
1305                 mac->mode = WIRELESS_MODE_G;
1306         }
1307 
1308         if (sta->deflink.ht_cap.ht_supported) {
1309             if (rtlhal->current_bandtype == BAND_ON_2_4G)
1310                 mac->mode = WIRELESS_MODE_N_24G;
1311             else
1312                 mac->mode = WIRELESS_MODE_N_5G;
1313         }
1314 
1315         if (sta->deflink.vht_cap.vht_supported) {
1316             if (rtlhal->current_bandtype == BAND_ON_5G)
1317                 mac->mode = WIRELESS_MODE_AC_5G;
1318             else
1319                 mac->mode = WIRELESS_MODE_AC_24G;
1320         }
1321 
1322         /* just station need it, because ibss & ap mode will
1323          * set in sta_add, and will be NULL here */
1324         if (vif->type == NL80211_IFTYPE_STATION) {
1325             struct rtl_sta_info *sta_entry;
1326 
1327             sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1328             sta_entry->wireless_mode = mac->mode;
1329         }
1330 
1331         if (sta->deflink.ht_cap.ht_supported) {
1332             mac->ht_enable = true;
1333 
1334             /*
1335              * for cisco 1252 bw20 it's wrong
1336              * if (ht_cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
1337              *  mac->bw_40 = true;
1338              * }
1339              * */
1340         }
1341 
1342         if (sta->deflink.vht_cap.vht_supported)
1343             mac->vht_enable = true;
1344 
1345         if (changed & BSS_CHANGED_BASIC_RATES) {
1346             /* for 5G must << RATE_6M_INDEX = 4,
1347              * because 5G have no cck rate*/
1348             if (rtlhal->current_bandtype == BAND_ON_5G)
1349                 basic_rates = sta->deflink.supp_rates[1] << 4;
1350             else
1351                 basic_rates = sta->deflink.supp_rates[0];
1352 
1353             mac->basic_rates = basic_rates;
1354             rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
1355                     (u8 *)(&basic_rates));
1356         }
1357         rcu_read_unlock();
1358     }
1359 out:
1360     mutex_unlock(&rtlpriv->locks.conf_mutex);
1361 }
1362 
1363 static u64 rtl_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1364 {
1365     struct rtl_priv *rtlpriv = rtl_priv(hw);
1366     u64 tsf;
1367 
1368     rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&tsf));
1369     return tsf;
1370 }
1371 
1372 static void rtl_op_set_tsf(struct ieee80211_hw *hw,
1373                struct ieee80211_vif *vif, u64 tsf)
1374 {
1375     struct rtl_priv *rtlpriv = rtl_priv(hw);
1376     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1377     u8 bibss = (mac->opmode == NL80211_IFTYPE_ADHOC) ? 1 : 0;
1378 
1379     mac->tsf = tsf;
1380     rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&bibss));
1381 }
1382 
1383 static void rtl_op_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1384 {
1385     struct rtl_priv *rtlpriv = rtl_priv(hw);
1386     u8 tmp = 0;
1387 
1388     rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_DUAL_TSF_RST, (u8 *)(&tmp));
1389 }
1390 
1391 static void rtl_op_sta_notify(struct ieee80211_hw *hw,
1392                   struct ieee80211_vif *vif,
1393                   enum sta_notify_cmd cmd,
1394                   struct ieee80211_sta *sta)
1395 {
1396     switch (cmd) {
1397     case STA_NOTIFY_SLEEP:
1398         break;
1399     case STA_NOTIFY_AWAKE:
1400         break;
1401     default:
1402         break;
1403     }
1404 }
1405 
1406 static int rtl_op_ampdu_action(struct ieee80211_hw *hw,
1407                    struct ieee80211_vif *vif,
1408                    struct ieee80211_ampdu_params *params)
1409 {
1410     struct rtl_priv *rtlpriv = rtl_priv(hw);
1411     struct ieee80211_sta *sta = params->sta;
1412     enum ieee80211_ampdu_mlme_action action = params->action;
1413     u16 tid = params->tid;
1414     u16 *ssn = &params->ssn;
1415 
1416     switch (action) {
1417     case IEEE80211_AMPDU_TX_START:
1418         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_TRACE,
1419             "IEEE80211_AMPDU_TX_START: TID:%d\n", tid);
1420         return rtl_tx_agg_start(hw, vif, sta, tid, ssn);
1421     case IEEE80211_AMPDU_TX_STOP_CONT:
1422     case IEEE80211_AMPDU_TX_STOP_FLUSH:
1423     case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1424         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_TRACE,
1425             "IEEE80211_AMPDU_TX_STOP: TID:%d\n", tid);
1426         return rtl_tx_agg_stop(hw, vif, sta, tid);
1427     case IEEE80211_AMPDU_TX_OPERATIONAL:
1428         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_TRACE,
1429             "IEEE80211_AMPDU_TX_OPERATIONAL:TID:%d\n", tid);
1430         rtl_tx_agg_oper(hw, sta, tid);
1431         break;
1432     case IEEE80211_AMPDU_RX_START:
1433         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_TRACE,
1434             "IEEE80211_AMPDU_RX_START:TID:%d\n", tid);
1435         return rtl_rx_agg_start(hw, sta, tid);
1436     case IEEE80211_AMPDU_RX_STOP:
1437         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_TRACE,
1438             "IEEE80211_AMPDU_RX_STOP:TID:%d\n", tid);
1439         return rtl_rx_agg_stop(hw, sta, tid);
1440     default:
1441         pr_err("IEEE80211_AMPDU_ERR!!!!:\n");
1442         return -EOPNOTSUPP;
1443     }
1444     return 0;
1445 }
1446 
1447 static void rtl_op_sw_scan_start(struct ieee80211_hw *hw,
1448                  struct ieee80211_vif *vif,
1449                  const u8 *mac_addr)
1450 {
1451     struct rtl_priv *rtlpriv = rtl_priv(hw);
1452     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1453 
1454     rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
1455     mac->act_scanning = true;
1456     if (rtlpriv->link_info.higher_busytraffic) {
1457         mac->skip_scan = true;
1458         return;
1459     }
1460 
1461     if (rtlpriv->cfg->ops->get_btc_status())
1462         rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 1);
1463     else if (rtlpriv->btcoexist.btc_ops)
1464         rtlpriv->btcoexist.btc_ops->btc_scan_notify_wifi_only(rtlpriv,
1465                                       1);
1466 
1467     if (rtlpriv->dm.supp_phymode_switch) {
1468         if (rtlpriv->cfg->ops->chk_switch_dmdp)
1469             rtlpriv->cfg->ops->chk_switch_dmdp(hw);
1470     }
1471 
1472     if (mac->link_state == MAC80211_LINKED) {
1473         rtl_lps_leave(hw, true);
1474         mac->link_state = MAC80211_LINKED_SCANNING;
1475     } else {
1476         rtl_ips_nic_on(hw);
1477     }
1478 
1479     /* Dul mac */
1480     rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
1481 
1482     rtlpriv->cfg->ops->led_control(hw, LED_CTL_SITE_SURVEY);
1483     rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_BACKUP_BAND0);
1484 }
1485 
1486 static void rtl_op_sw_scan_complete(struct ieee80211_hw *hw,
1487                     struct ieee80211_vif *vif)
1488 {
1489     struct rtl_priv *rtlpriv = rtl_priv(hw);
1490     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1491 
1492     rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
1493     mac->act_scanning = false;
1494     mac->skip_scan = false;
1495 
1496     rtlpriv->btcoexist.btc_info.ap_num = rtlpriv->scan_list.num;
1497 
1498     if (rtlpriv->link_info.higher_busytraffic)
1499         return;
1500 
1501     /* p2p will use 1/6/11 to scan */
1502     if (mac->n_channels == 3)
1503         mac->p2p_in_use = true;
1504     else
1505         mac->p2p_in_use = false;
1506     mac->n_channels = 0;
1507     /* Dul mac */
1508     rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
1509 
1510     if (mac->link_state == MAC80211_LINKED_SCANNING) {
1511         mac->link_state = MAC80211_LINKED;
1512         if (mac->opmode == NL80211_IFTYPE_STATION) {
1513             /* fix fwlps issue */
1514             rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
1515         }
1516     }
1517 
1518     rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_RESTORE);
1519     if (rtlpriv->cfg->ops->get_btc_status())
1520         rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 0);
1521     else if (rtlpriv->btcoexist.btc_ops)
1522         rtlpriv->btcoexist.btc_ops->btc_scan_notify_wifi_only(rtlpriv,
1523                                       0);
1524 }
1525 
1526 static int rtl_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1527               struct ieee80211_vif *vif, struct ieee80211_sta *sta,
1528               struct ieee80211_key_conf *key)
1529 {
1530     struct rtl_priv *rtlpriv = rtl_priv(hw);
1531     u8 key_type = NO_ENCRYPTION;
1532     u8 key_idx;
1533     bool group_key = false;
1534     bool wep_only = false;
1535     int err = 0;
1536     u8 mac_addr[ETH_ALEN];
1537     u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1538 
1539     rtlpriv->btcoexist.btc_info.in_4way = false;
1540 
1541     if (rtlpriv->cfg->mod_params->sw_crypto || rtlpriv->sec.use_sw_sec) {
1542         rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
1543             "not open hw encryption\n");
1544         return -ENOSPC; /*User disabled HW-crypto */
1545     }
1546     /* To support IBSS, use sw-crypto for GTK */
1547     if ((vif->type == NL80211_IFTYPE_ADHOC ||
1548          vif->type == NL80211_IFTYPE_MESH_POINT) &&
1549         !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1550         return -ENOSPC;
1551     rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
1552         "%s hardware based encryption for keyidx: %d, mac: %pM\n",
1553         cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
1554         sta ? sta->addr : bcast_addr);
1555     rtlpriv->sec.being_setkey = true;
1556     rtl_ips_nic_on(hw);
1557     mutex_lock(&rtlpriv->locks.conf_mutex);
1558     /* <1> get encryption alg */
1559 
1560     switch (key->cipher) {
1561     case WLAN_CIPHER_SUITE_WEP40:
1562         key_type = WEP40_ENCRYPTION;
1563         rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP40\n");
1564         break;
1565     case WLAN_CIPHER_SUITE_WEP104:
1566         rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP104\n");
1567         key_type = WEP104_ENCRYPTION;
1568         break;
1569     case WLAN_CIPHER_SUITE_TKIP:
1570         key_type = TKIP_ENCRYPTION;
1571         rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG, "alg:TKIP\n");
1572         break;
1573     case WLAN_CIPHER_SUITE_CCMP:
1574         key_type = AESCCMP_ENCRYPTION;
1575         rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CCMP\n");
1576         break;
1577     case WLAN_CIPHER_SUITE_AES_CMAC:
1578         /* HW don't support CMAC encryption,
1579          * use software CMAC encryption
1580          */
1581         key_type = AESCMAC_ENCRYPTION;
1582         rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CMAC\n");
1583         rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
1584             "HW don't support CMAC encryption, use software CMAC encryption\n");
1585         err = -EOPNOTSUPP;
1586         goto out_unlock;
1587     default:
1588         pr_err("alg_err:%x!!!!:\n", key->cipher);
1589         goto out_unlock;
1590     }
1591     if (key_type == WEP40_ENCRYPTION ||
1592        key_type == WEP104_ENCRYPTION ||
1593        vif->type == NL80211_IFTYPE_ADHOC)
1594         rtlpriv->sec.use_defaultkey = true;
1595 
1596     /* <2> get key_idx */
1597     key_idx = (u8) (key->keyidx);
1598     if (key_idx > 3)
1599         goto out_unlock;
1600     /* <3> if pairwise key enable_hw_sec */
1601     group_key = !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE);
1602 
1603     /* wep always be group key, but there are two conditions:
1604      * 1) wep only: is just for wep enc, in this condition
1605      * rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION
1606      * will be true & enable_hw_sec will be set when wep
1607      * ke setting.
1608      * 2) wep(group) + AES(pairwise): some AP like cisco
1609      * may use it, in this condition enable_hw_sec will not
1610      * be set when wep key setting */
1611     /* we must reset sec_info after lingked before set key,
1612      * or some flag will be wrong*/
1613     if (vif->type == NL80211_IFTYPE_AP ||
1614         vif->type == NL80211_IFTYPE_MESH_POINT) {
1615         if (!group_key || key_type == WEP40_ENCRYPTION ||
1616             key_type == WEP104_ENCRYPTION) {
1617             if (group_key)
1618                 wep_only = true;
1619             rtlpriv->cfg->ops->enable_hw_sec(hw);
1620         }
1621     } else {
1622         if (!group_key || vif->type == NL80211_IFTYPE_ADHOC ||
1623             rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION) {
1624             if (rtlpriv->sec.pairwise_enc_algorithm ==
1625                 NO_ENCRYPTION &&
1626                (key_type == WEP40_ENCRYPTION ||
1627                 key_type == WEP104_ENCRYPTION))
1628                 wep_only = true;
1629             rtlpriv->sec.pairwise_enc_algorithm = key_type;
1630             rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
1631                 "set enable_hw_sec, key_type:%x(OPEN:0 WEP40:1 TKIP:2 AES:4 WEP104:5)\n",
1632                 key_type);
1633             rtlpriv->cfg->ops->enable_hw_sec(hw);
1634         }
1635     }
1636     /* <4> set key based on cmd */
1637     switch (cmd) {
1638     case SET_KEY:
1639         if (wep_only) {
1640             rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
1641                 "set WEP(group/pairwise) key\n");
1642             /* Pairwise key with an assigned MAC address. */
1643             rtlpriv->sec.pairwise_enc_algorithm = key_type;
1644             rtlpriv->sec.group_enc_algorithm = key_type;
1645             /*set local buf about wep key. */
1646             memcpy(rtlpriv->sec.key_buf[key_idx],
1647                    key->key, key->keylen);
1648             rtlpriv->sec.key_len[key_idx] = key->keylen;
1649             eth_zero_addr(mac_addr);
1650         } else if (group_key) { /* group key */
1651             rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
1652                 "set group key\n");
1653             /* group key */
1654             rtlpriv->sec.group_enc_algorithm = key_type;
1655             /*set local buf about group key. */
1656             memcpy(rtlpriv->sec.key_buf[key_idx],
1657                    key->key, key->keylen);
1658             rtlpriv->sec.key_len[key_idx] = key->keylen;
1659             memcpy(mac_addr, bcast_addr, ETH_ALEN);
1660         } else {    /* pairwise key */
1661             rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
1662                 "set pairwise key\n");
1663             if (!sta) {
1664                 WARN_ONCE(true,
1665                       "rtlwifi: pairwise key without mac_addr\n");
1666 
1667                 err = -EOPNOTSUPP;
1668                 goto out_unlock;
1669             }
1670             /* Pairwise key with an assigned MAC address. */
1671             rtlpriv->sec.pairwise_enc_algorithm = key_type;
1672             /*set local buf about pairwise key. */
1673             memcpy(rtlpriv->sec.key_buf[PAIRWISE_KEYIDX],
1674                    key->key, key->keylen);
1675             rtlpriv->sec.key_len[PAIRWISE_KEYIDX] = key->keylen;
1676             rtlpriv->sec.pairwise_key =
1677                 rtlpriv->sec.key_buf[PAIRWISE_KEYIDX];
1678             memcpy(mac_addr, sta->addr, ETH_ALEN);
1679         }
1680         rtlpriv->cfg->ops->set_key(hw, key_idx, mac_addr,
1681                        group_key, key_type, wep_only,
1682                        false);
1683         /* <5> tell mac80211 do something: */
1684         /*must use sw generate IV, or can not work !!!!. */
1685         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1686         key->hw_key_idx = key_idx;
1687         if (key_type == TKIP_ENCRYPTION)
1688             key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1689         /*use software CCMP encryption for management frames (MFP) */
1690         if (key_type == AESCCMP_ENCRYPTION)
1691             key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
1692         break;
1693     case DISABLE_KEY:
1694         rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
1695             "disable key delete one entry\n");
1696         /*set local buf about wep key. */
1697         if (vif->type == NL80211_IFTYPE_AP ||
1698             vif->type == NL80211_IFTYPE_MESH_POINT) {
1699             if (sta)
1700                 rtl_cam_del_entry(hw, sta->addr);
1701         }
1702         memset(rtlpriv->sec.key_buf[key_idx], 0, key->keylen);
1703         rtlpriv->sec.key_len[key_idx] = 0;
1704         eth_zero_addr(mac_addr);
1705         /*
1706          *mac80211 will delete entries one by one,
1707          *so don't use rtl_cam_reset_all_entry
1708          *or clear all entry here.
1709          */
1710         rtl_wait_tx_report_acked(hw, 500); /* wait 500ms for TX ack */
1711 
1712         rtl_cam_delete_one_entry(hw, mac_addr, key_idx);
1713         break;
1714     default:
1715         pr_err("cmd_err:%x!!!!:\n", cmd);
1716     }
1717 out_unlock:
1718     mutex_unlock(&rtlpriv->locks.conf_mutex);
1719     rtlpriv->sec.being_setkey = false;
1720     return err;
1721 }
1722 
1723 static void rtl_op_rfkill_poll(struct ieee80211_hw *hw)
1724 {
1725     struct rtl_priv *rtlpriv = rtl_priv(hw);
1726 
1727     bool radio_state;
1728     bool blocked;
1729     u8 valid = 0;
1730 
1731     if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1732         return;
1733 
1734     mutex_lock(&rtlpriv->locks.conf_mutex);
1735 
1736     /*if Radio On return true here */
1737     radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
1738 
1739     if (valid) {
1740         if (unlikely(radio_state != rtlpriv->rfkill.rfkill_state)) {
1741             rtlpriv->rfkill.rfkill_state = radio_state;
1742 
1743             rtl_dbg(rtlpriv, COMP_RF, DBG_DMESG,
1744                 "wireless radio switch turned %s\n",
1745                 radio_state ? "on" : "off");
1746 
1747             blocked = !rtlpriv->rfkill.rfkill_state;
1748             wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
1749         }
1750     }
1751 
1752     mutex_unlock(&rtlpriv->locks.conf_mutex);
1753 }
1754 
1755 /* this function is called by mac80211 to flush tx buffer
1756  * before switch channle or power save, or tx buffer packet
1757  * maybe send after offchannel or rf sleep, this may cause
1758  * dis-association by AP */
1759 static void rtl_op_flush(struct ieee80211_hw *hw,
1760              struct ieee80211_vif *vif,
1761              u32 queues,
1762              bool drop)
1763 {
1764     struct rtl_priv *rtlpriv = rtl_priv(hw);
1765 
1766     if (rtlpriv->intf_ops->flush)
1767         rtlpriv->intf_ops->flush(hw, queues, drop);
1768 }
1769 
1770 static int rtl_op_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
1771               bool set)
1772 {
1773     struct rtl_priv *rtlpriv = rtl_priv(hw);
1774     struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1775 
1776     if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192CU)
1777         schedule_work(&rtlpriv->works.update_beacon_work);
1778 
1779     return 0;
1780 }
1781 
1782 /*  Description:
1783  *      This routine deals with the Power Configuration CMD
1784  *       parsing for RTL8723/RTL8188E Series IC.
1785  *  Assumption:
1786  *      We should follow specific format that was released from HW SD.
1787  */
1788 bool rtl_hal_pwrseqcmdparsing(struct rtl_priv *rtlpriv, u8 cut_version,
1789                   u8 faversion, u8 interface_type,
1790                   struct wlan_pwr_cfg pwrcfgcmd[])
1791 {
1792     struct wlan_pwr_cfg cfg_cmd;
1793     bool polling_bit = false;
1794     u32 ary_idx = 0;
1795     u8 value = 0;
1796     u32 offset = 0;
1797     u32 polling_count = 0;
1798     u32 max_polling_cnt = 5000;
1799 
1800     do {
1801         cfg_cmd = pwrcfgcmd[ary_idx];
1802         rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1803             "%s: offset(%#x),cut_msk(%#x), famsk(%#x), interface_msk(%#x), base(%#x), cmd(%#x), msk(%#x), value(%#x)\n",
1804             __func__,
1805             GET_PWR_CFG_OFFSET(cfg_cmd),
1806                        GET_PWR_CFG_CUT_MASK(cfg_cmd),
1807             GET_PWR_CFG_FAB_MASK(cfg_cmd),
1808                          GET_PWR_CFG_INTF_MASK(cfg_cmd),
1809             GET_PWR_CFG_BASE(cfg_cmd), GET_PWR_CFG_CMD(cfg_cmd),
1810             GET_PWR_CFG_MASK(cfg_cmd), GET_PWR_CFG_VALUE(cfg_cmd));
1811 
1812         if ((GET_PWR_CFG_FAB_MASK(cfg_cmd)&faversion) &&
1813             (GET_PWR_CFG_CUT_MASK(cfg_cmd)&cut_version) &&
1814             (GET_PWR_CFG_INTF_MASK(cfg_cmd)&interface_type)) {
1815             switch (GET_PWR_CFG_CMD(cfg_cmd)) {
1816             case PWR_CMD_READ:
1817                 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1818                     "rtl_hal_pwrseqcmdparsing(): PWR_CMD_READ\n");
1819                 break;
1820             case PWR_CMD_WRITE:
1821                 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1822                     "%s(): PWR_CMD_WRITE\n", __func__);
1823                 offset = GET_PWR_CFG_OFFSET(cfg_cmd);
1824 
1825                 /*Read the value from system register*/
1826                 value = rtl_read_byte(rtlpriv, offset);
1827                 value &= (~(GET_PWR_CFG_MASK(cfg_cmd)));
1828                 value |= (GET_PWR_CFG_VALUE(cfg_cmd) &
1829                       GET_PWR_CFG_MASK(cfg_cmd));
1830 
1831                 /*Write the value back to system register*/
1832                 rtl_write_byte(rtlpriv, offset, value);
1833                 break;
1834             case PWR_CMD_POLLING:
1835                 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1836                     "rtl_hal_pwrseqcmdparsing(): PWR_CMD_POLLING\n");
1837                 polling_bit = false;
1838                 offset = GET_PWR_CFG_OFFSET(cfg_cmd);
1839 
1840                 do {
1841                     value = rtl_read_byte(rtlpriv, offset);
1842 
1843                     value &= GET_PWR_CFG_MASK(cfg_cmd);
1844                     if (value ==
1845                         (GET_PWR_CFG_VALUE(cfg_cmd) &
1846                          GET_PWR_CFG_MASK(cfg_cmd)))
1847                         polling_bit = true;
1848                     else
1849                         udelay(10);
1850 
1851                     if (polling_count++ > max_polling_cnt)
1852                         return false;
1853                 } while (!polling_bit);
1854                 break;
1855             case PWR_CMD_DELAY:
1856                 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1857                     "%s: PWR_CMD_DELAY\n", __func__);
1858                 if (GET_PWR_CFG_VALUE(cfg_cmd) ==
1859                     PWRSEQ_DELAY_US)
1860                     udelay(GET_PWR_CFG_OFFSET(cfg_cmd));
1861                 else
1862                     mdelay(GET_PWR_CFG_OFFSET(cfg_cmd));
1863                 break;
1864             case PWR_CMD_END:
1865                 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1866                     "%s: PWR_CMD_END\n", __func__);
1867                 return true;
1868             default:
1869                 WARN_ONCE(true,
1870                       "rtlwifi: rtl_hal_pwrseqcmdparsing(): Unknown CMD!!\n");
1871                 break;
1872             }
1873         }
1874         ary_idx++;
1875     } while (1);
1876 
1877     return true;
1878 }
1879 EXPORT_SYMBOL(rtl_hal_pwrseqcmdparsing);
1880 
1881 bool rtl_cmd_send_packet(struct ieee80211_hw *hw, struct sk_buff *skb)
1882 {
1883     struct rtl_priv *rtlpriv = rtl_priv(hw);
1884     struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1885     struct rtl8192_tx_ring *ring;
1886     struct rtl_tx_desc *pdesc;
1887     unsigned long flags;
1888     struct sk_buff *pskb = NULL;
1889 
1890     ring = &rtlpci->tx_ring[BEACON_QUEUE];
1891 
1892     spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
1893     pskb = __skb_dequeue(&ring->queue);
1894     if (pskb)
1895         dev_kfree_skb_irq(pskb);
1896 
1897     /*this is wrong, fill_tx_cmddesc needs update*/
1898     pdesc = &ring->desc[0];
1899 
1900     rtlpriv->cfg->ops->fill_tx_cmddesc(hw, (u8 *)pdesc, 1, 1, skb);
1901 
1902     __skb_queue_tail(&ring->queue, skb);
1903 
1904     spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
1905 
1906     rtlpriv->cfg->ops->tx_polling(hw, BEACON_QUEUE);
1907 
1908     return true;
1909 }
1910 EXPORT_SYMBOL(rtl_cmd_send_packet);
1911 const struct ieee80211_ops rtl_ops = {
1912     .start = rtl_op_start,
1913     .stop = rtl_op_stop,
1914     .tx = rtl_op_tx,
1915     .add_interface = rtl_op_add_interface,
1916     .remove_interface = rtl_op_remove_interface,
1917     .change_interface = rtl_op_change_interface,
1918 #ifdef CONFIG_PM
1919     .suspend = rtl_op_suspend,
1920     .resume = rtl_op_resume,
1921 #endif
1922     .config = rtl_op_config,
1923     .configure_filter = rtl_op_configure_filter,
1924     .set_key = rtl_op_set_key,
1925     .conf_tx = rtl_op_conf_tx,
1926     .bss_info_changed = rtl_op_bss_info_changed,
1927     .get_tsf = rtl_op_get_tsf,
1928     .set_tsf = rtl_op_set_tsf,
1929     .reset_tsf = rtl_op_reset_tsf,
1930     .sta_notify = rtl_op_sta_notify,
1931     .ampdu_action = rtl_op_ampdu_action,
1932     .sw_scan_start = rtl_op_sw_scan_start,
1933     .sw_scan_complete = rtl_op_sw_scan_complete,
1934     .rfkill_poll = rtl_op_rfkill_poll,
1935     .sta_add = rtl_op_sta_add,
1936     .sta_remove = rtl_op_sta_remove,
1937     .flush = rtl_op_flush,
1938     .set_tim = rtl_op_set_tim,
1939 };
1940 EXPORT_SYMBOL_GPL(rtl_ops);
1941 
1942 bool rtl_btc_status_false(void)
1943 {
1944     return false;
1945 }
1946 EXPORT_SYMBOL_GPL(rtl_btc_status_false);
1947 
1948 void rtl_dm_diginit(struct ieee80211_hw *hw, u32 cur_igvalue)
1949 {
1950     struct rtl_priv *rtlpriv = rtl_priv(hw);
1951     struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
1952 
1953     dm_digtable->dig_enable_flag = true;
1954     dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
1955     dm_digtable->cur_igvalue = cur_igvalue;
1956     dm_digtable->pre_igvalue = 0;
1957     dm_digtable->cur_sta_cstate = DIG_STA_DISCONNECT;
1958     dm_digtable->presta_cstate = DIG_STA_DISCONNECT;
1959     dm_digtable->curmultista_cstate = DIG_MULTISTA_DISCONNECT;
1960     dm_digtable->rssi_lowthresh = DM_DIG_THRESH_LOW;
1961     dm_digtable->rssi_highthresh = DM_DIG_THRESH_HIGH;
1962     dm_digtable->fa_lowthresh = DM_FALSEALARM_THRESH_LOW;
1963     dm_digtable->fa_highthresh = DM_FALSEALARM_THRESH_HIGH;
1964     dm_digtable->rx_gain_max = DM_DIG_MAX;
1965     dm_digtable->rx_gain_min = DM_DIG_MIN;
1966     dm_digtable->back_val = DM_DIG_BACKOFF_DEFAULT;
1967     dm_digtable->back_range_max = DM_DIG_BACKOFF_MAX;
1968     dm_digtable->back_range_min = DM_DIG_BACKOFF_MIN;
1969     dm_digtable->pre_cck_cca_thres = 0xff;
1970     dm_digtable->cur_cck_cca_thres = 0x83;
1971     dm_digtable->forbidden_igi = DM_DIG_MIN;
1972     dm_digtable->large_fa_hit = 0;
1973     dm_digtable->recover_cnt = 0;
1974     dm_digtable->dig_min_0 = 0x25;
1975     dm_digtable->dig_min_1 = 0x25;
1976     dm_digtable->media_connect_0 = false;
1977     dm_digtable->media_connect_1 = false;
1978     rtlpriv->dm.dm_initialgain_enable = true;
1979     dm_digtable->bt30_cur_igi = 0x32;
1980     dm_digtable->pre_cck_pd_state = CCK_PD_STAGE_MAX;
1981     dm_digtable->cur_cck_pd_state = CCK_PD_STAGE_LOWRSSI;
1982     dm_digtable->pre_cck_fa_state = 0;
1983     dm_digtable->cur_cck_fa_state = 0;
1984 }
1985 EXPORT_SYMBOL(rtl_dm_diginit);