Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2014 Redpine Signals Inc.
0003  *
0004  * Permission to use, copy, modify, and/or distribute this software for any
0005  * purpose with or without fee is hereby granted, provided that the above
0006  * copyright notice and this permission notice appear in all copies.
0007  *
0008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
0009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
0010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
0011  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
0012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0013  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0014  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0015  */
0016 
0017 #include <linux/etherdevice.h>
0018 #include "rsi_debugfs.h"
0019 #include "rsi_mgmt.h"
0020 #include "rsi_sdio.h"
0021 #include "rsi_common.h"
0022 #include "rsi_ps.h"
0023 
0024 static const struct ieee80211_channel rsi_2ghz_channels[] = {
0025     { .band = NL80211_BAND_2GHZ, .center_freq = 2412,
0026       .hw_value = 1 }, /* Channel 1 */
0027     { .band = NL80211_BAND_2GHZ, .center_freq = 2417,
0028       .hw_value = 2 }, /* Channel 2 */
0029     { .band = NL80211_BAND_2GHZ, .center_freq = 2422,
0030       .hw_value = 3 }, /* Channel 3 */
0031     { .band = NL80211_BAND_2GHZ, .center_freq = 2427,
0032       .hw_value = 4 }, /* Channel 4 */
0033     { .band = NL80211_BAND_2GHZ, .center_freq = 2432,
0034       .hw_value = 5 }, /* Channel 5 */
0035     { .band = NL80211_BAND_2GHZ, .center_freq = 2437,
0036       .hw_value = 6 }, /* Channel 6 */
0037     { .band = NL80211_BAND_2GHZ, .center_freq = 2442,
0038       .hw_value = 7 }, /* Channel 7 */
0039     { .band = NL80211_BAND_2GHZ, .center_freq = 2447,
0040       .hw_value = 8 }, /* Channel 8 */
0041     { .band = NL80211_BAND_2GHZ, .center_freq = 2452,
0042       .hw_value = 9 }, /* Channel 9 */
0043     { .band = NL80211_BAND_2GHZ, .center_freq = 2457,
0044       .hw_value = 10 }, /* Channel 10 */
0045     { .band = NL80211_BAND_2GHZ, .center_freq = 2462,
0046       .hw_value = 11 }, /* Channel 11 */
0047     { .band = NL80211_BAND_2GHZ, .center_freq = 2467,
0048       .hw_value = 12 }, /* Channel 12 */
0049     { .band = NL80211_BAND_2GHZ, .center_freq = 2472,
0050       .hw_value = 13 }, /* Channel 13 */
0051     { .band = NL80211_BAND_2GHZ, .center_freq = 2484,
0052       .hw_value = 14 }, /* Channel 14 */
0053 };
0054 
0055 static const struct ieee80211_channel rsi_5ghz_channels[] = {
0056     { .band = NL80211_BAND_5GHZ, .center_freq = 5180,
0057       .hw_value = 36,  }, /* Channel 36 */
0058     { .band = NL80211_BAND_5GHZ, .center_freq = 5200,
0059       .hw_value = 40, }, /* Channel 40 */
0060     { .band = NL80211_BAND_5GHZ, .center_freq = 5220,
0061       .hw_value = 44, }, /* Channel 44 */
0062     { .band = NL80211_BAND_5GHZ, .center_freq = 5240,
0063       .hw_value = 48, }, /* Channel 48 */
0064     { .band = NL80211_BAND_5GHZ, .center_freq = 5260,
0065       .hw_value = 52, }, /* Channel 52 */
0066     { .band = NL80211_BAND_5GHZ, .center_freq = 5280,
0067       .hw_value = 56, }, /* Channel 56 */
0068     { .band = NL80211_BAND_5GHZ, .center_freq = 5300,
0069       .hw_value = 60, }, /* Channel 60 */
0070     { .band = NL80211_BAND_5GHZ, .center_freq = 5320,
0071       .hw_value = 64, }, /* Channel 64 */
0072     { .band = NL80211_BAND_5GHZ, .center_freq = 5500,
0073       .hw_value = 100, }, /* Channel 100 */
0074     { .band = NL80211_BAND_5GHZ, .center_freq = 5520,
0075       .hw_value = 104, }, /* Channel 104 */
0076     { .band = NL80211_BAND_5GHZ, .center_freq = 5540,
0077       .hw_value = 108, }, /* Channel 108 */
0078     { .band = NL80211_BAND_5GHZ, .center_freq = 5560,
0079       .hw_value = 112, }, /* Channel 112 */
0080     { .band = NL80211_BAND_5GHZ, .center_freq = 5580,
0081       .hw_value = 116, }, /* Channel 116 */
0082     { .band = NL80211_BAND_5GHZ, .center_freq = 5600,
0083       .hw_value = 120, }, /* Channel 120 */
0084     { .band = NL80211_BAND_5GHZ, .center_freq = 5620,
0085       .hw_value = 124, }, /* Channel 124 */
0086     { .band = NL80211_BAND_5GHZ, .center_freq = 5640,
0087       .hw_value = 128, }, /* Channel 128 */
0088     { .band = NL80211_BAND_5GHZ, .center_freq = 5660,
0089       .hw_value = 132, }, /* Channel 132 */
0090     { .band = NL80211_BAND_5GHZ, .center_freq = 5680,
0091       .hw_value = 136, }, /* Channel 136 */
0092     { .band = NL80211_BAND_5GHZ, .center_freq = 5700,
0093       .hw_value = 140, }, /* Channel 140 */
0094     { .band = NL80211_BAND_5GHZ, .center_freq = 5745,
0095       .hw_value = 149, }, /* Channel 149 */
0096     { .band = NL80211_BAND_5GHZ, .center_freq = 5765,
0097       .hw_value = 153, }, /* Channel 153 */
0098     { .band = NL80211_BAND_5GHZ, .center_freq = 5785,
0099       .hw_value = 157, }, /* Channel 157 */
0100     { .band = NL80211_BAND_5GHZ, .center_freq = 5805,
0101       .hw_value = 161, }, /* Channel 161 */
0102     { .band = NL80211_BAND_5GHZ, .center_freq = 5825,
0103       .hw_value = 165, }, /* Channel 165 */
0104 };
0105 
0106 struct ieee80211_rate rsi_rates[12] = {
0107     { .bitrate = STD_RATE_01  * 5, .hw_value = RSI_RATE_1 },
0108     { .bitrate = STD_RATE_02  * 5, .hw_value = RSI_RATE_2 },
0109     { .bitrate = STD_RATE_5_5 * 5, .hw_value = RSI_RATE_5_5 },
0110     { .bitrate = STD_RATE_11  * 5, .hw_value = RSI_RATE_11 },
0111     { .bitrate = STD_RATE_06  * 5, .hw_value = RSI_RATE_6 },
0112     { .bitrate = STD_RATE_09  * 5, .hw_value = RSI_RATE_9 },
0113     { .bitrate = STD_RATE_12  * 5, .hw_value = RSI_RATE_12 },
0114     { .bitrate = STD_RATE_18  * 5, .hw_value = RSI_RATE_18 },
0115     { .bitrate = STD_RATE_24  * 5, .hw_value = RSI_RATE_24 },
0116     { .bitrate = STD_RATE_36  * 5, .hw_value = RSI_RATE_36 },
0117     { .bitrate = STD_RATE_48  * 5, .hw_value = RSI_RATE_48 },
0118     { .bitrate = STD_RATE_54  * 5, .hw_value = RSI_RATE_54 },
0119 };
0120 
0121 const u16 rsi_mcsrates[8] = {
0122     RSI_RATE_MCS0, RSI_RATE_MCS1, RSI_RATE_MCS2, RSI_RATE_MCS3,
0123     RSI_RATE_MCS4, RSI_RATE_MCS5, RSI_RATE_MCS6, RSI_RATE_MCS7
0124 };
0125 
0126 static const u32 rsi_max_ap_stas[16] = {
0127     32, /* 1 - Wi-Fi alone */
0128     0,  /* 2 */
0129     0,  /* 3 */
0130     0,  /* 4 - BT EDR alone */
0131     4,  /* 5 - STA + BT EDR */
0132     32, /* 6 - AP + BT EDR */
0133     0,  /* 7 */
0134     0,  /* 8 - BT LE alone */
0135     4,  /* 9 - STA + BE LE */
0136     0,  /* 10 */
0137     0,  /* 11 */
0138     0,  /* 12 */
0139     1,  /* 13 - STA + BT Dual */
0140     4,  /* 14 - AP + BT Dual */
0141 };
0142 
0143 static const struct ieee80211_iface_limit rsi_iface_limits[] = {
0144     {
0145         .max = 1,
0146         .types = BIT(NL80211_IFTYPE_STATION),
0147     },
0148     {
0149         .max = 1,
0150         .types = BIT(NL80211_IFTYPE_AP) |
0151             BIT(NL80211_IFTYPE_P2P_CLIENT) |
0152             BIT(NL80211_IFTYPE_P2P_GO),
0153     },
0154     {
0155         .max = 1,
0156         .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
0157     },
0158 };
0159 
0160 static const struct ieee80211_iface_combination rsi_iface_combinations[] = {
0161     {
0162         .num_different_channels = 1,
0163         .max_interfaces = 3,
0164         .limits = rsi_iface_limits,
0165         .n_limits = ARRAY_SIZE(rsi_iface_limits),
0166     },
0167 };
0168 
0169 /**
0170  * rsi_is_cipher_wep() -  This function determines if the cipher is WEP or not.
0171  * @common: Pointer to the driver private structure.
0172  *
0173  * Return: If cipher type is WEP, a value of 1 is returned, else 0.
0174  */
0175 
0176 bool rsi_is_cipher_wep(struct rsi_common *common)
0177 {
0178     if (((common->secinfo.gtk_cipher == WLAN_CIPHER_SUITE_WEP104) ||
0179          (common->secinfo.gtk_cipher == WLAN_CIPHER_SUITE_WEP40)) &&
0180         (!common->secinfo.ptk_cipher))
0181         return true;
0182     else
0183         return false;
0184 }
0185 
0186 /**
0187  * rsi_register_rates_channels() - This function registers channels and rates.
0188  * @adapter: Pointer to the adapter structure.
0189  * @band: Operating band to be set.
0190  *
0191  * Return: int - 0 on success, negative error on failure.
0192  */
0193 static int rsi_register_rates_channels(struct rsi_hw *adapter, int band)
0194 {
0195     struct ieee80211_supported_band *sbands = &adapter->sbands[band];
0196     void *channels = NULL;
0197 
0198     if (band == NL80211_BAND_2GHZ) {
0199         channels = kmemdup(rsi_2ghz_channels, sizeof(rsi_2ghz_channels),
0200                    GFP_KERNEL);
0201         if (!channels)
0202             return -ENOMEM;
0203         sbands->band = NL80211_BAND_2GHZ;
0204         sbands->n_channels = ARRAY_SIZE(rsi_2ghz_channels);
0205         sbands->bitrates = rsi_rates;
0206         sbands->n_bitrates = ARRAY_SIZE(rsi_rates);
0207     } else {
0208         channels = kmemdup(rsi_5ghz_channels, sizeof(rsi_5ghz_channels),
0209                    GFP_KERNEL);
0210         if (!channels)
0211             return -ENOMEM;
0212         sbands->band = NL80211_BAND_5GHZ;
0213         sbands->n_channels = ARRAY_SIZE(rsi_5ghz_channels);
0214         sbands->bitrates = &rsi_rates[4];
0215         sbands->n_bitrates = ARRAY_SIZE(rsi_rates) - 4;
0216     }
0217 
0218     sbands->channels = channels;
0219 
0220     memset(&sbands->ht_cap, 0, sizeof(struct ieee80211_sta_ht_cap));
0221     sbands->ht_cap.ht_supported = true;
0222     sbands->ht_cap.cap = (IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
0223                   IEEE80211_HT_CAP_SGI_20 |
0224                   IEEE80211_HT_CAP_SGI_40);
0225     sbands->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_16K;
0226     sbands->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
0227     sbands->ht_cap.mcs.rx_mask[0] = 0xff;
0228     sbands->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
0229     /* sbands->ht_cap.mcs.rx_highest = 0x82; */
0230     return 0;
0231 }
0232 
0233 static int rsi_mac80211_hw_scan_start(struct ieee80211_hw *hw,
0234                       struct ieee80211_vif *vif,
0235                       struct ieee80211_scan_request *hw_req)
0236 {
0237     struct cfg80211_scan_request *scan_req = &hw_req->req;
0238     struct rsi_hw *adapter = hw->priv;
0239     struct rsi_common *common = adapter->priv;
0240 
0241     rsi_dbg(INFO_ZONE, "***** Hardware scan start *****\n");
0242     common->mac_ops_resumed = false;
0243 
0244     if (common->fsm_state != FSM_MAC_INIT_DONE)
0245         return -ENODEV;
0246 
0247     if ((common->wow_flags & RSI_WOW_ENABLED) ||
0248         scan_req->n_channels == 0)
0249         return -EINVAL;
0250 
0251     /* Scan already in progress. So return */
0252     if (common->bgscan_en)
0253         return -EBUSY;
0254 
0255     /* If STA is not connected, return with special value 1, in order
0256      * to start sw_scan in mac80211
0257      */
0258     if (!vif->cfg.assoc)
0259         return 1;
0260 
0261     mutex_lock(&common->mutex);
0262     common->hwscan = scan_req;
0263     if (!rsi_send_bgscan_params(common, RSI_START_BGSCAN)) {
0264         if (!rsi_send_bgscan_probe_req(common, vif)) {
0265             rsi_dbg(INFO_ZONE, "Background scan started...\n");
0266             common->bgscan_en = true;
0267         }
0268     }
0269     mutex_unlock(&common->mutex);
0270 
0271     return 0;
0272 }
0273 
0274 static void rsi_mac80211_cancel_hw_scan(struct ieee80211_hw *hw,
0275                     struct ieee80211_vif *vif)
0276 {
0277     struct rsi_hw *adapter = hw->priv;
0278     struct rsi_common *common = adapter->priv;
0279     struct cfg80211_scan_info info;
0280 
0281     rsi_dbg(INFO_ZONE, "***** Hardware scan stop *****\n");
0282     mutex_lock(&common->mutex);
0283 
0284     if (common->bgscan_en) {
0285         if (!rsi_send_bgscan_params(common, RSI_STOP_BGSCAN))
0286             common->bgscan_en = false;
0287         info.aborted = false;
0288         ieee80211_scan_completed(adapter->hw, &info);
0289         rsi_dbg(INFO_ZONE, "Back ground scan cancelled\n");
0290     }
0291     common->hwscan = NULL;
0292     mutex_unlock(&common->mutex);
0293 }
0294 
0295 /**
0296  * rsi_mac80211_detach() - This function is used to de-initialize the
0297  *             Mac80211 stack.
0298  * @adapter: Pointer to the adapter structure.
0299  *
0300  * Return: None.
0301  */
0302 void rsi_mac80211_detach(struct rsi_hw *adapter)
0303 {
0304     struct ieee80211_hw *hw = adapter->hw;
0305     enum nl80211_band band;
0306 
0307     if (hw) {
0308         ieee80211_stop_queues(hw);
0309         ieee80211_unregister_hw(hw);
0310         ieee80211_free_hw(hw);
0311         adapter->hw = NULL;
0312     }
0313 
0314     for (band = 0; band < NUM_NL80211_BANDS; band++) {
0315         struct ieee80211_supported_band *sband =
0316                     &adapter->sbands[band];
0317 
0318         kfree(sband->channels);
0319     }
0320 
0321 #ifdef CONFIG_RSI_DEBUGFS
0322     rsi_remove_dbgfs(adapter);
0323     kfree(adapter->dfsentry);
0324 #endif
0325 }
0326 EXPORT_SYMBOL_GPL(rsi_mac80211_detach);
0327 
0328 /**
0329  * rsi_indicate_tx_status() - This function indicates the transmit status.
0330  * @adapter: Pointer to the adapter structure.
0331  * @skb: Pointer to the socket buffer structure.
0332  * @status: Status
0333  *
0334  * Return: None.
0335  */
0336 void rsi_indicate_tx_status(struct rsi_hw *adapter,
0337                 struct sk_buff *skb,
0338                 int status)
0339 {
0340     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
0341     struct skb_info *tx_params;
0342 
0343     if (!adapter->hw) {
0344         rsi_dbg(ERR_ZONE, "##### No MAC #####\n");
0345         return;
0346     }
0347 
0348     if (!status)
0349         info->flags |= IEEE80211_TX_STAT_ACK;
0350 
0351     tx_params = (struct skb_info *)info->driver_data;
0352     skb_pull(skb, tx_params->internal_hdr_size);
0353     memset(info->driver_data, 0, IEEE80211_TX_INFO_DRIVER_DATA_SIZE);
0354 
0355     ieee80211_tx_status_irqsafe(adapter->hw, skb);
0356 }
0357 
0358 /**
0359  * rsi_mac80211_tx() - This is the handler that 802.11 module calls for each
0360  *             transmitted frame.SKB contains the buffer starting
0361  *             from the IEEE 802.11 header.
0362  * @hw: Pointer to the ieee80211_hw structure.
0363  * @control: Pointer to the ieee80211_tx_control structure
0364  * @skb: Pointer to the socket buffer structure.
0365  *
0366  * Return: None
0367  */
0368 static void rsi_mac80211_tx(struct ieee80211_hw *hw,
0369                 struct ieee80211_tx_control *control,
0370                 struct sk_buff *skb)
0371 {
0372     struct rsi_hw *adapter = hw->priv;
0373     struct rsi_common *common = adapter->priv;
0374     struct ieee80211_hdr *wlh = (struct ieee80211_hdr *)skb->data;
0375 
0376     if (ieee80211_is_auth(wlh->frame_control))
0377         common->mac_ops_resumed = false;
0378 
0379     rsi_core_xmit(common, skb);
0380 }
0381 
0382 /**
0383  * rsi_mac80211_start() - This is first handler that 802.11 module calls, since
0384  *            the driver init is complete by then, just
0385  *            returns success.
0386  * @hw: Pointer to the ieee80211_hw structure.
0387  *
0388  * Return: 0 as success.
0389  */
0390 static int rsi_mac80211_start(struct ieee80211_hw *hw)
0391 {
0392     struct rsi_hw *adapter = hw->priv;
0393     struct rsi_common *common = adapter->priv;
0394 
0395     rsi_dbg(ERR_ZONE, "===> Interface UP <===\n");
0396     mutex_lock(&common->mutex);
0397     if (common->hibernate_resume) {
0398         common->reinit_hw = true;
0399         adapter->host_intf_ops->reinit_device(adapter);
0400         wait_for_completion(&adapter->priv->wlan_init_completion);
0401     }
0402     common->iface_down = false;
0403     wiphy_rfkill_start_polling(hw->wiphy);
0404     rsi_send_rx_filter_frame(common, 0);
0405     mutex_unlock(&common->mutex);
0406 
0407     return 0;
0408 }
0409 
0410 /**
0411  * rsi_mac80211_stop() - This is the last handler that 802.11 module calls.
0412  * @hw: Pointer to the ieee80211_hw structure.
0413  *
0414  * Return: None.
0415  */
0416 static void rsi_mac80211_stop(struct ieee80211_hw *hw)
0417 {
0418     struct rsi_hw *adapter = hw->priv;
0419     struct rsi_common *common = adapter->priv;
0420 
0421     rsi_dbg(ERR_ZONE, "===> Interface DOWN <===\n");
0422     mutex_lock(&common->mutex);
0423     common->iface_down = true;
0424     wiphy_rfkill_stop_polling(hw->wiphy);
0425 
0426     /* Block all rx frames */
0427     rsi_send_rx_filter_frame(common, 0xffff);
0428 
0429     mutex_unlock(&common->mutex);
0430 }
0431 
0432 static int rsi_map_intf_mode(enum nl80211_iftype vif_type)
0433 {
0434     switch (vif_type) {
0435     case NL80211_IFTYPE_STATION:
0436         return RSI_OPMODE_STA;
0437     case NL80211_IFTYPE_AP:
0438         return RSI_OPMODE_AP;
0439     case NL80211_IFTYPE_P2P_DEVICE:
0440         return RSI_OPMODE_P2P_CLIENT;
0441     case NL80211_IFTYPE_P2P_CLIENT:
0442         return RSI_OPMODE_P2P_CLIENT;
0443     case NL80211_IFTYPE_P2P_GO:
0444         return RSI_OPMODE_P2P_GO;
0445     default:
0446         return RSI_OPMODE_UNSUPPORTED;
0447     }
0448 }
0449 
0450 /**
0451  * rsi_mac80211_add_interface() - This function is called when a netdevice
0452  *                attached to the hardware is enabled.
0453  * @hw: Pointer to the ieee80211_hw structure.
0454  * @vif: Pointer to the ieee80211_vif structure.
0455  *
0456  * Return: ret: 0 on success, negative error code on failure.
0457  */
0458 static int rsi_mac80211_add_interface(struct ieee80211_hw *hw,
0459                       struct ieee80211_vif *vif)
0460 {
0461     struct rsi_hw *adapter = hw->priv;
0462     struct rsi_common *common = adapter->priv;
0463     struct vif_priv *vif_info = (struct vif_priv *)vif->drv_priv;
0464     enum opmode intf_mode;
0465     enum vap_status vap_status;
0466     int vap_idx = -1, i;
0467 
0468     vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
0469     mutex_lock(&common->mutex);
0470 
0471     intf_mode = rsi_map_intf_mode(vif->type);
0472     if (intf_mode == RSI_OPMODE_UNSUPPORTED) {
0473         rsi_dbg(ERR_ZONE,
0474             "%s: Interface type %d not supported\n", __func__,
0475             vif->type);
0476         mutex_unlock(&common->mutex);
0477         return -EOPNOTSUPP;
0478     }
0479     if ((vif->type == NL80211_IFTYPE_P2P_DEVICE) ||
0480         (vif->type == NL80211_IFTYPE_P2P_CLIENT) ||
0481         (vif->type == NL80211_IFTYPE_P2P_GO))
0482         common->p2p_enabled = true;
0483 
0484     /* Get free vap index */
0485     for (i = 0; i < RSI_MAX_VIFS; i++) {
0486         if (!adapter->vifs[i] ||
0487             !memcmp(vif->addr, adapter->vifs[i]->addr, ETH_ALEN)) {
0488             vap_idx = i;
0489             break;
0490         }
0491     }
0492     if (vap_idx < 0) {
0493         rsi_dbg(ERR_ZONE, "Reject: Max VAPs reached\n");
0494         mutex_unlock(&common->mutex);
0495         return -EOPNOTSUPP;
0496     }
0497     vif_info->vap_id = vap_idx;
0498     adapter->vifs[vap_idx] = vif;
0499     adapter->sc_nvifs++;
0500     vap_status = VAP_ADD;
0501 
0502     if (rsi_set_vap_capabilities(common, intf_mode, vif->addr,
0503                      vif_info->vap_id, vap_status)) {
0504         rsi_dbg(ERR_ZONE, "Failed to set VAP capabilities\n");
0505         mutex_unlock(&common->mutex);
0506         return -EINVAL;
0507     }
0508 
0509     if ((vif->type == NL80211_IFTYPE_AP) ||
0510         (vif->type == NL80211_IFTYPE_P2P_GO)) {
0511         rsi_send_rx_filter_frame(common, DISALLOW_BEACONS);
0512         for (i = 0; i < common->max_stations; i++)
0513             common->stations[i].sta = NULL;
0514     }
0515 
0516     mutex_unlock(&common->mutex);
0517 
0518     return 0;
0519 }
0520 
0521 /**
0522  * rsi_mac80211_remove_interface() - This function notifies driver that an
0523  *                   interface is going down.
0524  * @hw: Pointer to the ieee80211_hw structure.
0525  * @vif: Pointer to the ieee80211_vif structure.
0526  *
0527  * Return: None.
0528  */
0529 static void rsi_mac80211_remove_interface(struct ieee80211_hw *hw,
0530                       struct ieee80211_vif *vif)
0531 {
0532     struct rsi_hw *adapter = hw->priv;
0533     struct rsi_common *common = adapter->priv;
0534     enum opmode opmode;
0535     int i;
0536 
0537     rsi_dbg(INFO_ZONE, "Remove Interface Called\n");
0538 
0539     mutex_lock(&common->mutex);
0540 
0541     if (adapter->sc_nvifs <= 0) {
0542         mutex_unlock(&common->mutex);
0543         return;
0544     }
0545 
0546     opmode = rsi_map_intf_mode(vif->type);
0547     if (opmode == RSI_OPMODE_UNSUPPORTED) {
0548         rsi_dbg(ERR_ZONE, "Opmode error : %d\n", opmode);
0549         mutex_unlock(&common->mutex);
0550         return;
0551     }
0552     for (i = 0; i < RSI_MAX_VIFS; i++) {
0553         if (!adapter->vifs[i])
0554             continue;
0555         if (vif == adapter->vifs[i]) {
0556             rsi_set_vap_capabilities(common, opmode, vif->addr,
0557                          i, VAP_DELETE);
0558             adapter->sc_nvifs--;
0559             adapter->vifs[i] = NULL;
0560         }
0561     }
0562     mutex_unlock(&common->mutex);
0563 }
0564 
0565 /**
0566  * rsi_channel_change() - This function is a performs the checks
0567  *            required for changing a channel and sets
0568  *            the channel accordingly.
0569  * @hw: Pointer to the ieee80211_hw structure.
0570  *
0571  * Return: 0 on success, negative error code on failure.
0572  */
0573 static int rsi_channel_change(struct ieee80211_hw *hw)
0574 {
0575     struct rsi_hw *adapter = hw->priv;
0576     struct rsi_common *common = adapter->priv;
0577     int status = -EOPNOTSUPP;
0578     struct ieee80211_channel *curchan = hw->conf.chandef.chan;
0579     u16 channel = curchan->hw_value;
0580     struct ieee80211_vif *vif;
0581     bool assoc = false;
0582     int i;
0583 
0584     rsi_dbg(INFO_ZONE,
0585         "%s: Set channel: %d MHz type: %d channel_no %d\n",
0586         __func__, curchan->center_freq,
0587         curchan->flags, channel);
0588 
0589     for (i = 0; i < RSI_MAX_VIFS; i++) {
0590         vif = adapter->vifs[i];
0591         if (!vif)
0592             continue;
0593         if (vif->type == NL80211_IFTYPE_STATION) {
0594             if (vif->cfg.assoc) {
0595                 assoc = true;
0596                 break;
0597             }
0598         }
0599     }
0600     if (assoc) {
0601         if (!common->hw_data_qs_blocked &&
0602             (rsi_get_connected_channel(vif) != channel)) {
0603             rsi_dbg(INFO_ZONE, "blk data q %d\n", channel);
0604             if (!rsi_send_block_unblock_frame(common, true))
0605                 common->hw_data_qs_blocked = true;
0606         }
0607     }
0608 
0609     status = rsi_band_check(common, curchan);
0610     if (!status)
0611         status = rsi_set_channel(adapter->priv, curchan);
0612 
0613     if (assoc) {
0614         if (common->hw_data_qs_blocked &&
0615             (rsi_get_connected_channel(vif) == channel)) {
0616             rsi_dbg(INFO_ZONE, "unblk data q %d\n", channel);
0617             if (!rsi_send_block_unblock_frame(common, false))
0618                 common->hw_data_qs_blocked = false;
0619         }
0620     }
0621 
0622     return status;
0623 }
0624 
0625 /**
0626  * rsi_config_power() - This function configures tx power to device
0627  * @hw: Pointer to the ieee80211_hw structure.
0628  *
0629  * Return: 0 on success, negative error code on failure.
0630  */
0631 static int rsi_config_power(struct ieee80211_hw *hw)
0632 {
0633     struct rsi_hw *adapter = hw->priv;
0634     struct rsi_common *common = adapter->priv;
0635     struct ieee80211_conf *conf = &hw->conf;
0636 
0637     if (adapter->sc_nvifs <= 0) {
0638         rsi_dbg(ERR_ZONE, "%s: No virtual interface found\n", __func__);
0639         return -EINVAL;
0640     }
0641 
0642     rsi_dbg(INFO_ZONE,
0643         "%s: Set tx power: %d dBM\n", __func__, conf->power_level);
0644 
0645     if (conf->power_level == common->tx_power)
0646         return 0;
0647 
0648     common->tx_power = conf->power_level;
0649 
0650     return rsi_send_radio_params_update(common);
0651 }
0652 
0653 /**
0654  * rsi_mac80211_config() - This function is a handler for configuration
0655  *             requests. The stack calls this function to
0656  *             change hardware configuration, e.g., channel.
0657  * @hw: Pointer to the ieee80211_hw structure.
0658  * @changed: Changed flags set.
0659  *
0660  * Return: 0 on success, negative error code on failure.
0661  */
0662 static int rsi_mac80211_config(struct ieee80211_hw *hw,
0663                    u32 changed)
0664 {
0665     struct rsi_hw *adapter = hw->priv;
0666     struct rsi_common *common = adapter->priv;
0667     struct ieee80211_conf *conf = &hw->conf;
0668     int status = -EOPNOTSUPP;
0669 
0670     mutex_lock(&common->mutex);
0671 
0672     if (changed & IEEE80211_CONF_CHANGE_CHANNEL)
0673         status = rsi_channel_change(hw);
0674 
0675     /* tx power */
0676     if (changed & IEEE80211_CONF_CHANGE_POWER) {
0677         rsi_dbg(INFO_ZONE, "%s: Configuring Power\n", __func__);
0678         status = rsi_config_power(hw);
0679     }
0680 
0681     /* Power save parameters */
0682     if ((changed & IEEE80211_CONF_CHANGE_PS) &&
0683         !common->mac_ops_resumed) {
0684         struct ieee80211_vif *vif, *sta_vif = NULL;
0685         unsigned long flags;
0686         int i, set_ps = 1;
0687 
0688         for (i = 0; i < RSI_MAX_VIFS; i++) {
0689             vif = adapter->vifs[i];
0690             if (!vif)
0691                 continue;
0692             /* Don't go to power save if AP vap exists */
0693             if ((vif->type == NL80211_IFTYPE_AP) ||
0694                 (vif->type == NL80211_IFTYPE_P2P_GO)) {
0695                 set_ps = 0;
0696                 break;
0697             }
0698             if ((vif->type == NL80211_IFTYPE_STATION ||
0699                  vif->type == NL80211_IFTYPE_P2P_CLIENT) &&
0700                 (!sta_vif || vif->cfg.assoc))
0701                 sta_vif = vif;
0702         }
0703         if (set_ps && sta_vif) {
0704             spin_lock_irqsave(&adapter->ps_lock, flags);
0705             if (conf->flags & IEEE80211_CONF_PS)
0706                 rsi_enable_ps(adapter, sta_vif);
0707             else
0708                 rsi_disable_ps(adapter, sta_vif);
0709             spin_unlock_irqrestore(&adapter->ps_lock, flags);
0710         }
0711     }
0712 
0713     /* RTS threshold */
0714     if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
0715         rsi_dbg(INFO_ZONE, "RTS threshold\n");
0716         if ((common->rts_threshold) <= IEEE80211_MAX_RTS_THRESHOLD) {
0717             rsi_dbg(INFO_ZONE,
0718                 "%s: Sending vap updates....\n", __func__);
0719             status = rsi_send_vap_dynamic_update(common);
0720         }
0721     }
0722     mutex_unlock(&common->mutex);
0723 
0724     return status;
0725 }
0726 
0727 /**
0728  * rsi_get_connected_channel() - This function is used to get the current
0729  *               connected channel number.
0730  * @vif: Pointer to the ieee80211_vif structure.
0731  *
0732  * Return: Current connected AP's channel number is returned.
0733  */
0734 u16 rsi_get_connected_channel(struct ieee80211_vif *vif)
0735 {
0736     struct ieee80211_bss_conf *bss;
0737     struct ieee80211_channel *channel;
0738 
0739     if (!vif)
0740         return 0;
0741 
0742     bss = &vif->bss_conf;
0743     channel = bss->chandef.chan;
0744 
0745     if (!channel)
0746         return 0;
0747 
0748     return channel->hw_value;
0749 }
0750 
0751 static void rsi_switch_channel(struct rsi_hw *adapter,
0752                    struct ieee80211_vif *vif)
0753 {
0754     struct rsi_common *common = adapter->priv;
0755     struct ieee80211_channel *channel;
0756 
0757     if (common->iface_down)
0758         return;
0759     if (!vif)
0760         return;
0761 
0762     channel = vif->bss_conf.chandef.chan;
0763 
0764     if (!channel)
0765         return;
0766 
0767     rsi_band_check(common, channel);
0768     rsi_set_channel(common, channel);
0769     rsi_dbg(INFO_ZONE, "Switched to channel - %d\n", channel->hw_value);
0770 }
0771 
0772 /**
0773  * rsi_mac80211_bss_info_changed() - This function is a handler for config
0774  *                   requests related to BSS parameters that
0775  *                   may vary during BSS's lifespan.
0776  * @hw: Pointer to the ieee80211_hw structure.
0777  * @vif: Pointer to the ieee80211_vif structure.
0778  * @bss_conf: Pointer to the ieee80211_bss_conf structure.
0779  * @changed: Changed flags set.
0780  *
0781  * Return: None.
0782  */
0783 static void rsi_mac80211_bss_info_changed(struct ieee80211_hw *hw,
0784                       struct ieee80211_vif *vif,
0785                       struct ieee80211_bss_conf *bss_conf,
0786                       u64 changed)
0787 {
0788     struct rsi_hw *adapter = hw->priv;
0789     struct rsi_common *common = adapter->priv;
0790     struct ieee80211_bss_conf *bss = &vif->bss_conf;
0791     struct ieee80211_conf *conf = &hw->conf;
0792     u16 rx_filter_word = 0;
0793 
0794     mutex_lock(&common->mutex);
0795     if (changed & BSS_CHANGED_ASSOC) {
0796         rsi_dbg(INFO_ZONE, "%s: Changed Association status: %d\n",
0797             __func__, vif->cfg.assoc);
0798         if (vif->cfg.assoc) {
0799             /* Send the RX filter frame */
0800             rx_filter_word = (ALLOW_DATA_ASSOC_PEER |
0801                       ALLOW_CTRL_ASSOC_PEER |
0802                       ALLOW_MGMT_ASSOC_PEER);
0803             rsi_send_rx_filter_frame(common, rx_filter_word);
0804         }
0805         rsi_inform_bss_status(common,
0806                       RSI_OPMODE_STA,
0807                       vif->cfg.assoc,
0808                       bss_conf->bssid,
0809                       bss_conf->qos,
0810                       vif->cfg.aid,
0811                       NULL, 0,
0812                       bss_conf->assoc_capability, vif);
0813         adapter->ps_info.dtim_interval_duration = bss->dtim_period;
0814         adapter->ps_info.listen_interval = conf->listen_interval;
0815 
0816         /* If U-APSD is updated, send ps parameters to firmware */
0817         if (vif->cfg.assoc) {
0818             if (common->uapsd_bitmap) {
0819                 rsi_dbg(INFO_ZONE, "Configuring UAPSD\n");
0820                 rsi_conf_uapsd(adapter, vif);
0821             }
0822         } else {
0823             common->uapsd_bitmap = 0;
0824         }
0825     }
0826 
0827     if (changed & BSS_CHANGED_CQM) {
0828         common->cqm_info.last_cqm_event_rssi = 0;
0829         common->cqm_info.rssi_thold = bss_conf->cqm_rssi_thold;
0830         common->cqm_info.rssi_hyst = bss_conf->cqm_rssi_hyst;
0831         rsi_dbg(INFO_ZONE, "RSSI threshold & hysteresis are: %d %d\n",
0832             common->cqm_info.rssi_thold,
0833             common->cqm_info.rssi_hyst);
0834     }
0835 
0836     if (changed & BSS_CHANGED_BEACON_INT) {
0837         rsi_dbg(INFO_ZONE, "%s: Changed Beacon interval: %d\n",
0838             __func__, bss_conf->beacon_int);
0839         if (common->beacon_interval != bss->beacon_int) {
0840             common->beacon_interval = bss->beacon_int;
0841             if (vif->type == NL80211_IFTYPE_AP) {
0842                 struct vif_priv *vif_info = (struct vif_priv *)vif->drv_priv;
0843 
0844                 rsi_set_vap_capabilities(common, RSI_OPMODE_AP,
0845                              vif->addr, vif_info->vap_id,
0846                              VAP_UPDATE);
0847             }
0848         }
0849         adapter->ps_info.listen_interval =
0850             bss->beacon_int * adapter->ps_info.num_bcns_per_lis_int;
0851     }
0852 
0853     if ((changed & BSS_CHANGED_BEACON_ENABLED) &&
0854         ((vif->type == NL80211_IFTYPE_AP) ||
0855          (vif->type == NL80211_IFTYPE_P2P_GO))) {
0856         if (bss->enable_beacon) {
0857             rsi_dbg(INFO_ZONE, "===> BEACON ENABLED <===\n");
0858             common->beacon_enabled = 1;
0859         } else {
0860             rsi_dbg(INFO_ZONE, "===> BEACON DISABLED <===\n");
0861             common->beacon_enabled = 0;
0862         }
0863     }
0864 
0865     mutex_unlock(&common->mutex);
0866 }
0867 
0868 /**
0869  * rsi_mac80211_conf_filter() - This function configure the device's RX filter.
0870  * @hw: Pointer to the ieee80211_hw structure.
0871  * @changed_flags: Changed flags set.
0872  * @total_flags: Total initial flags set.
0873  * @multicast: Multicast.
0874  *
0875  * Return: None.
0876  */
0877 static void rsi_mac80211_conf_filter(struct ieee80211_hw *hw,
0878                      u32 changed_flags,
0879                      u32 *total_flags,
0880                      u64 multicast)
0881 {
0882     /* Not doing much here as of now */
0883     *total_flags &= RSI_SUPP_FILTERS;
0884 }
0885 
0886 /**
0887  * rsi_mac80211_conf_tx() - This function configures TX queue parameters
0888  *              (EDCF (aifs, cw_min, cw_max), bursting)
0889  *              for a hardware TX queue.
0890  * @hw: Pointer to the ieee80211_hw structure
0891  * @vif: Pointer to the ieee80211_vif structure.
0892  * @queue: Queue number.
0893  * @params: Pointer to ieee80211_tx_queue_params structure.
0894  *
0895  * Return: 0 on success, negative error code on failure.
0896  */
0897 static int rsi_mac80211_conf_tx(struct ieee80211_hw *hw,
0898                 struct ieee80211_vif *vif,
0899                 unsigned int link_id, u16 queue,
0900                 const struct ieee80211_tx_queue_params *params)
0901 {
0902     struct rsi_hw *adapter = hw->priv;
0903     struct rsi_common *common = adapter->priv;
0904     u8 idx = 0;
0905 
0906     if (queue >= IEEE80211_NUM_ACS)
0907         return 0;
0908 
0909     rsi_dbg(INFO_ZONE,
0910         "%s: Conf queue %d, aifs: %d, cwmin: %d cwmax: %d, txop: %d\n",
0911         __func__, queue, params->aifs,
0912         params->cw_min, params->cw_max, params->txop);
0913 
0914     mutex_lock(&common->mutex);
0915     /* Map into the way the f/w expects */
0916     switch (queue) {
0917     case IEEE80211_AC_VO:
0918         idx = VO_Q;
0919         break;
0920     case IEEE80211_AC_VI:
0921         idx = VI_Q;
0922         break;
0923     case IEEE80211_AC_BE:
0924         idx = BE_Q;
0925         break;
0926     case IEEE80211_AC_BK:
0927         idx = BK_Q;
0928         break;
0929     default:
0930         idx = BE_Q;
0931         break;
0932     }
0933 
0934     memcpy(&common->edca_params[idx],
0935            params,
0936            sizeof(struct ieee80211_tx_queue_params));
0937 
0938     if (params->uapsd)
0939         common->uapsd_bitmap |= idx;
0940     else
0941         common->uapsd_bitmap &= (~idx);
0942 
0943     mutex_unlock(&common->mutex);
0944 
0945     return 0;
0946 }
0947 
0948 /**
0949  * rsi_hal_key_config() - This function loads the keys into the firmware.
0950  * @hw: Pointer to the ieee80211_hw structure.
0951  * @vif: Pointer to the ieee80211_vif structure.
0952  * @key: Pointer to the ieee80211_key_conf structure.
0953  * @sta: Pointer to the ieee80211_sta structure.
0954  *
0955  * Return: status: 0 on success, negative error codes on failure.
0956  */
0957 static int rsi_hal_key_config(struct ieee80211_hw *hw,
0958                   struct ieee80211_vif *vif,
0959                   struct ieee80211_key_conf *key,
0960                   struct ieee80211_sta *sta)
0961 {
0962     struct rsi_hw *adapter = hw->priv;
0963     struct rsi_sta *rsta = NULL;
0964     int status;
0965     u8 key_type;
0966     s16 sta_id = 0;
0967 
0968     if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
0969         key_type = RSI_PAIRWISE_KEY;
0970     else
0971         key_type = RSI_GROUP_KEY;
0972 
0973     rsi_dbg(ERR_ZONE, "%s: Cipher 0x%x key_type: %d key_len: %d\n",
0974         __func__, key->cipher, key_type, key->keylen);
0975 
0976     if ((vif->type == NL80211_IFTYPE_AP) ||
0977         (vif->type == NL80211_IFTYPE_P2P_GO)) {
0978         if (sta) {
0979             rsta = rsi_find_sta(adapter->priv, sta->addr);
0980             if (rsta)
0981                 sta_id = rsta->sta_id;
0982         }
0983         adapter->priv->key = key;
0984     } else {
0985         if ((key->cipher == WLAN_CIPHER_SUITE_WEP104) ||
0986             (key->cipher == WLAN_CIPHER_SUITE_WEP40)) {
0987             status = rsi_hal_load_key(adapter->priv,
0988                           key->key,
0989                           key->keylen,
0990                           RSI_PAIRWISE_KEY,
0991                           key->keyidx,
0992                           key->cipher,
0993                           sta_id,
0994                           vif);
0995             if (status)
0996                 return status;
0997         }
0998     }
0999 
1000     status = rsi_hal_load_key(adapter->priv,
1001                   key->key,
1002                   key->keylen,
1003                   key_type,
1004                   key->keyidx,
1005                   key->cipher,
1006                   sta_id,
1007                   vif);
1008     if (status)
1009         return status;
1010 
1011     if (vif->type == NL80211_IFTYPE_STATION &&
1012         (key->cipher == WLAN_CIPHER_SUITE_WEP104 ||
1013          key->cipher == WLAN_CIPHER_SUITE_WEP40)) {
1014         if (!rsi_send_block_unblock_frame(adapter->priv, false))
1015             adapter->priv->hw_data_qs_blocked = false;
1016     }
1017 
1018     return 0;
1019 }
1020 
1021 /**
1022  * rsi_mac80211_set_key() - This function sets type of key to be loaded.
1023  * @hw: Pointer to the ieee80211_hw structure.
1024  * @cmd: enum set_key_cmd.
1025  * @vif: Pointer to the ieee80211_vif structure.
1026  * @sta: Pointer to the ieee80211_sta structure.
1027  * @key: Pointer to the ieee80211_key_conf structure.
1028  *
1029  * Return: status: 0 on success, negative error code on failure.
1030  */
1031 static int rsi_mac80211_set_key(struct ieee80211_hw *hw,
1032                 enum set_key_cmd cmd,
1033                 struct ieee80211_vif *vif,
1034                 struct ieee80211_sta *sta,
1035                 struct ieee80211_key_conf *key)
1036 {
1037     struct rsi_hw *adapter = hw->priv;
1038     struct rsi_common *common = adapter->priv;
1039     struct security_info *secinfo = &common->secinfo;
1040     int status;
1041 
1042     mutex_lock(&common->mutex);
1043     switch (cmd) {
1044     case SET_KEY:
1045         status = rsi_hal_key_config(hw, vif, key, sta);
1046         if (status) {
1047             mutex_unlock(&common->mutex);
1048             return status;
1049         }
1050 
1051         if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
1052             secinfo->ptk_cipher = key->cipher;
1053         else
1054             secinfo->gtk_cipher = key->cipher;
1055 
1056         key->hw_key_idx = key->keyidx;
1057         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1058 
1059         rsi_dbg(ERR_ZONE, "%s: RSI set_key\n", __func__);
1060         break;
1061 
1062     case DISABLE_KEY:
1063         rsi_dbg(ERR_ZONE, "%s: RSI del key\n", __func__);
1064         memset(key, 0, sizeof(struct ieee80211_key_conf));
1065         status = rsi_hal_key_config(hw, vif, key, sta);
1066         break;
1067 
1068     default:
1069         status = -EOPNOTSUPP;
1070         break;
1071     }
1072 
1073     mutex_unlock(&common->mutex);
1074     return status;
1075 }
1076 
1077 /**
1078  * rsi_mac80211_ampdu_action() - This function selects the AMPDU action for
1079  *               the corresponding mlme_action flag and
1080  *               informs the f/w regarding this.
1081  * @hw: Pointer to the ieee80211_hw structure.
1082  * @vif: Pointer to the ieee80211_vif structure.
1083  * @params: Pointer to A-MPDU action parameters
1084  *
1085  * Return: status: 0 on success, negative error code on failure.
1086  */
1087 static int rsi_mac80211_ampdu_action(struct ieee80211_hw *hw,
1088                      struct ieee80211_vif *vif,
1089                      struct ieee80211_ampdu_params *params)
1090 {
1091     int status = -EOPNOTSUPP;
1092     struct rsi_hw *adapter = hw->priv;
1093     struct rsi_common *common = adapter->priv;
1094     struct rsi_sta *rsta = NULL;
1095     u16 seq_no = 0, seq_start = 0;
1096     u8 ii = 0;
1097     struct ieee80211_sta *sta = params->sta;
1098     u8 sta_id = 0;
1099     enum ieee80211_ampdu_mlme_action action = params->action;
1100     u16 tid = params->tid;
1101     u16 *ssn = &params->ssn;
1102     u8 buf_size = params->buf_size;
1103 
1104     for (ii = 0; ii < RSI_MAX_VIFS; ii++) {
1105         if (vif == adapter->vifs[ii])
1106             break;
1107     }
1108 
1109     if (ii >= RSI_MAX_VIFS)
1110         return status;
1111 
1112     mutex_lock(&common->mutex);
1113 
1114     if (ssn != NULL)
1115         seq_no = *ssn;
1116 
1117     if ((vif->type == NL80211_IFTYPE_AP) ||
1118         (vif->type == NL80211_IFTYPE_P2P_GO)) {
1119         rsta = rsi_find_sta(common, sta->addr);
1120         if (!rsta) {
1121             rsi_dbg(ERR_ZONE, "No station mapped\n");
1122             status = 0;
1123             goto unlock;
1124         }
1125         sta_id = rsta->sta_id;
1126     }
1127 
1128     rsi_dbg(INFO_ZONE,
1129         "%s: AMPDU action tid=%d ssn=0x%x, buf_size=%d sta_id=%d\n",
1130         __func__, tid, seq_no, buf_size, sta_id);
1131 
1132     switch (action) {
1133     case IEEE80211_AMPDU_RX_START:
1134         status = rsi_send_aggregation_params_frame(common,
1135                                tid,
1136                                seq_no,
1137                                buf_size,
1138                                STA_RX_ADDBA_DONE,
1139                                sta_id);
1140         break;
1141 
1142     case IEEE80211_AMPDU_RX_STOP:
1143         status = rsi_send_aggregation_params_frame(common,
1144                                tid,
1145                                0,
1146                                buf_size,
1147                                STA_RX_DELBA,
1148                                sta_id);
1149         break;
1150 
1151     case IEEE80211_AMPDU_TX_START:
1152         if ((vif->type == NL80211_IFTYPE_STATION) ||
1153             (vif->type == NL80211_IFTYPE_P2P_CLIENT))
1154             common->vif_info[ii].seq_start = seq_no;
1155         else if ((vif->type == NL80211_IFTYPE_AP) ||
1156              (vif->type == NL80211_IFTYPE_P2P_GO))
1157             rsta->seq_start[tid] = seq_no;
1158         status = IEEE80211_AMPDU_TX_START_IMMEDIATE;
1159         break;
1160 
1161     case IEEE80211_AMPDU_TX_STOP_CONT:
1162     case IEEE80211_AMPDU_TX_STOP_FLUSH:
1163     case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1164         status = rsi_send_aggregation_params_frame(common,
1165                                tid,
1166                                seq_no,
1167                                buf_size,
1168                                STA_TX_DELBA,
1169                                sta_id);
1170         if (!status)
1171             ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1172         break;
1173 
1174     case IEEE80211_AMPDU_TX_OPERATIONAL:
1175         if ((vif->type == NL80211_IFTYPE_STATION) ||
1176             (vif->type == NL80211_IFTYPE_P2P_CLIENT))
1177             seq_start = common->vif_info[ii].seq_start;
1178         else if ((vif->type == NL80211_IFTYPE_AP) ||
1179              (vif->type == NL80211_IFTYPE_P2P_GO))
1180             seq_start = rsta->seq_start[tid];
1181         status = rsi_send_aggregation_params_frame(common,
1182                                tid,
1183                                seq_start,
1184                                buf_size,
1185                                STA_TX_ADDBA_DONE,
1186                                sta_id);
1187         break;
1188 
1189     default:
1190         rsi_dbg(ERR_ZONE, "%s: Unknown AMPDU action\n", __func__);
1191         break;
1192     }
1193 
1194 unlock:
1195     mutex_unlock(&common->mutex);
1196     return status;
1197 }
1198 
1199 /**
1200  * rsi_mac80211_set_rts_threshold() - This function sets rts threshold value.
1201  * @hw: Pointer to the ieee80211_hw structure.
1202  * @value: Rts threshold value.
1203  *
1204  * Return: 0 on success.
1205  */
1206 static int rsi_mac80211_set_rts_threshold(struct ieee80211_hw *hw,
1207                       u32 value)
1208 {
1209     struct rsi_hw *adapter = hw->priv;
1210     struct rsi_common *common = adapter->priv;
1211 
1212     mutex_lock(&common->mutex);
1213     common->rts_threshold = value;
1214     mutex_unlock(&common->mutex);
1215 
1216     return 0;
1217 }
1218 
1219 /**
1220  * rsi_mac80211_set_rate_mask() - This function sets bitrate_mask to be used.
1221  * @hw: Pointer to the ieee80211_hw structure
1222  * @vif: Pointer to the ieee80211_vif structure.
1223  * @mask: Pointer to the cfg80211_bitrate_mask structure.
1224  *
1225  * Return: 0 on success.
1226  */
1227 static int rsi_mac80211_set_rate_mask(struct ieee80211_hw *hw,
1228                       struct ieee80211_vif *vif,
1229                       const struct cfg80211_bitrate_mask *mask)
1230 {
1231     const unsigned int mcs_offset = ARRAY_SIZE(rsi_rates);
1232     struct rsi_hw *adapter = hw->priv;
1233     struct rsi_common *common = adapter->priv;
1234     int i;
1235 
1236     mutex_lock(&common->mutex);
1237 
1238     for (i = 0; i < ARRAY_SIZE(common->rate_config); i++) {
1239         struct rsi_rate_config *cfg = &common->rate_config[i];
1240         u32 bm;
1241 
1242         bm = mask->control[i].legacy | (mask->control[i].ht_mcs[0] << mcs_offset);
1243         if (hweight32(bm) == 1) { /* single rate */
1244             int rate_index = ffs(bm) - 1;
1245 
1246             if (rate_index < mcs_offset)
1247                 cfg->fixed_hw_rate = rsi_rates[rate_index].hw_value;
1248             else
1249                 cfg->fixed_hw_rate = rsi_mcsrates[rate_index - mcs_offset];
1250             cfg->fixed_enabled = true;
1251         } else {
1252             cfg->configured_mask = bm;
1253             cfg->fixed_enabled = false;
1254         }
1255     }
1256 
1257     mutex_unlock(&common->mutex);
1258 
1259     return 0;
1260 }
1261 
1262 /**
1263  * rsi_perform_cqm() - This function performs cqm.
1264  * @common: Pointer to the driver private structure.
1265  * @bssid: pointer to the bssid.
1266  * @rssi: RSSI value.
1267  * @vif: Pointer to the ieee80211_vif structure.
1268  */
1269 static void rsi_perform_cqm(struct rsi_common *common,
1270                 u8 *bssid,
1271                 s8 rssi,
1272                 struct ieee80211_vif *vif)
1273 {
1274     s8 last_event = common->cqm_info.last_cqm_event_rssi;
1275     int thold = common->cqm_info.rssi_thold;
1276     u32 hyst = common->cqm_info.rssi_hyst;
1277     enum nl80211_cqm_rssi_threshold_event event;
1278 
1279     if (rssi < thold && (last_event == 0 || rssi < (last_event - hyst)))
1280         event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
1281     else if (rssi > thold &&
1282          (last_event == 0 || rssi > (last_event + hyst)))
1283         event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
1284     else
1285         return;
1286 
1287     common->cqm_info.last_cqm_event_rssi = rssi;
1288     rsi_dbg(INFO_ZONE, "CQM: Notifying event: %d\n", event);
1289     ieee80211_cqm_rssi_notify(vif, event, rssi, GFP_KERNEL);
1290 
1291     return;
1292 }
1293 
1294 /**
1295  * rsi_fill_rx_status() - This function fills rx status in
1296  *            ieee80211_rx_status structure.
1297  * @hw: Pointer to the ieee80211_hw structure.
1298  * @skb: Pointer to the socket buffer structure.
1299  * @common: Pointer to the driver private structure.
1300  * @rxs: Pointer to the ieee80211_rx_status structure.
1301  *
1302  * Return: None.
1303  */
1304 static void rsi_fill_rx_status(struct ieee80211_hw *hw,
1305                    struct sk_buff *skb,
1306                    struct rsi_common *common,
1307                    struct ieee80211_rx_status *rxs)
1308 {
1309     struct rsi_hw *adapter = common->priv;
1310     struct ieee80211_vif *vif;
1311     struct ieee80211_bss_conf *bss = NULL;
1312     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1313     struct skb_info *rx_params = (struct skb_info *)info->driver_data;
1314     struct ieee80211_hdr *hdr;
1315     char rssi = rx_params->rssi;
1316     u8 hdrlen = 0;
1317     u8 channel = rx_params->channel;
1318     s32 freq;
1319     int i;
1320 
1321     hdr = ((struct ieee80211_hdr *)(skb->data));
1322     hdrlen = ieee80211_hdrlen(hdr->frame_control);
1323 
1324     memset(info, 0, sizeof(struct ieee80211_tx_info));
1325 
1326     rxs->signal = -(rssi);
1327 
1328     rxs->band = common->band;
1329 
1330     freq = ieee80211_channel_to_frequency(channel, rxs->band);
1331 
1332     if (freq)
1333         rxs->freq = freq;
1334 
1335     if (ieee80211_has_protected(hdr->frame_control)) {
1336         if (rsi_is_cipher_wep(common)) {
1337             memmove(skb->data + 4, skb->data, hdrlen);
1338             skb_pull(skb, 4);
1339         } else {
1340             memmove(skb->data + 8, skb->data, hdrlen);
1341             skb_pull(skb, 8);
1342             rxs->flag |= RX_FLAG_MMIC_STRIPPED;
1343         }
1344         rxs->flag |= RX_FLAG_DECRYPTED;
1345         rxs->flag |= RX_FLAG_IV_STRIPPED;
1346     }
1347 
1348     for (i = 0; i < RSI_MAX_VIFS; i++) {
1349         vif = adapter->vifs[i];
1350         if (!vif)
1351             continue;
1352         if (vif->type == NL80211_IFTYPE_STATION) {
1353             bss = &vif->bss_conf;
1354             break;
1355         }
1356     }
1357     if (!bss)
1358         return;
1359     /* CQM only for connected AP beacons, the RSSI is a weighted avg */
1360     if (vif->cfg.assoc && !(memcmp(bss->bssid, hdr->addr2, ETH_ALEN))) {
1361         if (ieee80211_is_beacon(hdr->frame_control))
1362             rsi_perform_cqm(common, hdr->addr2, rxs->signal, vif);
1363     }
1364 
1365     return;
1366 }
1367 
1368 /**
1369  * rsi_indicate_pkt_to_os() - This function sends received packet to mac80211.
1370  * @common: Pointer to the driver private structure.
1371  * @skb: Pointer to the socket buffer structure.
1372  *
1373  * Return: None.
1374  */
1375 void rsi_indicate_pkt_to_os(struct rsi_common *common,
1376                 struct sk_buff *skb)
1377 {
1378     struct rsi_hw *adapter = common->priv;
1379     struct ieee80211_hw *hw = adapter->hw;
1380     struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1381 
1382     if ((common->iface_down) || (!adapter->sc_nvifs)) {
1383         dev_kfree_skb(skb);
1384         return;
1385     }
1386 
1387     /* filling in the ieee80211_rx_status flags */
1388     rsi_fill_rx_status(hw, skb, common, rx_status);
1389 
1390     ieee80211_rx_irqsafe(hw, skb);
1391 }
1392 
1393 /**
1394  * rsi_mac80211_sta_add() - This function notifies driver about a peer getting
1395  *              connected.
1396  * @hw: pointer to the ieee80211_hw structure.
1397  * @vif: Pointer to the ieee80211_vif structure.
1398  * @sta: Pointer to the ieee80211_sta structure.
1399  *
1400  * Return: 0 on success, negative error codes on failure.
1401  */
1402 static int rsi_mac80211_sta_add(struct ieee80211_hw *hw,
1403                 struct ieee80211_vif *vif,
1404                 struct ieee80211_sta *sta)
1405 {
1406     struct rsi_hw *adapter = hw->priv;
1407     struct rsi_common *common = adapter->priv;
1408     bool sta_exist = false;
1409     struct rsi_sta *rsta;
1410     int status = 0;
1411 
1412     rsi_dbg(INFO_ZONE, "Station Add: %pM\n", sta->addr);
1413 
1414     mutex_lock(&common->mutex);
1415 
1416     if ((vif->type == NL80211_IFTYPE_AP) ||
1417         (vif->type == NL80211_IFTYPE_P2P_GO)) {
1418         u8 cnt;
1419         int sta_idx = -1;
1420         int free_index = -1;
1421 
1422         /* Check if max stations reached */
1423         if (common->num_stations >= common->max_stations) {
1424             rsi_dbg(ERR_ZONE, "Reject: Max Stations exists\n");
1425             status = -EOPNOTSUPP;
1426             goto unlock;
1427         }
1428         for (cnt = 0; cnt < common->max_stations; cnt++) {
1429             rsta = &common->stations[cnt];
1430 
1431             if (!rsta->sta) {
1432                 if (free_index < 0)
1433                     free_index = cnt;
1434                 continue;
1435             }
1436             if (!memcmp(rsta->sta->addr, sta->addr, ETH_ALEN)) {
1437                 rsi_dbg(INFO_ZONE, "Station exists\n");
1438                 sta_idx = cnt;
1439                 sta_exist = true;
1440                 break;
1441             }
1442         }
1443         if (!sta_exist) {
1444             if (free_index >= 0)
1445                 sta_idx = free_index;
1446         }
1447         if (sta_idx < 0) {
1448             rsi_dbg(ERR_ZONE,
1449                 "%s: Some problem reaching here...\n",
1450                 __func__);
1451             status = -EINVAL;
1452             goto unlock;
1453         }
1454         rsta = &common->stations[sta_idx];
1455         rsta->sta = sta;
1456         rsta->sta_id = sta_idx;
1457         for (cnt = 0; cnt < IEEE80211_NUM_TIDS; cnt++)
1458             rsta->start_tx_aggr[cnt] = false;
1459         for (cnt = 0; cnt < IEEE80211_NUM_TIDS; cnt++)
1460             rsta->seq_start[cnt] = 0;
1461         if (!sta_exist) {
1462             rsi_dbg(INFO_ZONE, "New Station\n");
1463 
1464             /* Send peer notify to device */
1465             rsi_dbg(INFO_ZONE, "Indicate bss status to device\n");
1466             rsi_inform_bss_status(common, RSI_OPMODE_AP, 1,
1467                           sta->addr, sta->wme, sta->aid,
1468                           sta, sta_idx, 0, vif);
1469 
1470             if (common->key) {
1471                 struct ieee80211_key_conf *key = common->key;
1472 
1473                 if ((key->cipher == WLAN_CIPHER_SUITE_WEP104) ||
1474                     (key->cipher == WLAN_CIPHER_SUITE_WEP40))
1475                     rsi_hal_load_key(adapter->priv,
1476                              key->key,
1477                              key->keylen,
1478                              RSI_PAIRWISE_KEY,
1479                              key->keyidx,
1480                              key->cipher,
1481                              sta_idx,
1482                              vif);
1483             }
1484 
1485             common->num_stations++;
1486         }
1487     }
1488 
1489     if ((vif->type == NL80211_IFTYPE_STATION) ||
1490         (vif->type == NL80211_IFTYPE_P2P_CLIENT)) {
1491         common->bitrate_mask[common->band] = sta->deflink.supp_rates[common->band];
1492         common->vif_info[0].is_ht = sta->deflink.ht_cap.ht_supported;
1493         if (sta->deflink.ht_cap.ht_supported) {
1494             common->bitrate_mask[NL80211_BAND_2GHZ] =
1495                     sta->deflink.supp_rates[NL80211_BAND_2GHZ];
1496             if ((sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_20) ||
1497                 (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_40))
1498                 common->vif_info[0].sgi = true;
1499             ieee80211_start_tx_ba_session(sta, 0, 0);
1500         }
1501     }
1502 
1503 unlock:
1504     mutex_unlock(&common->mutex);
1505 
1506     return status;
1507 }
1508 
1509 /**
1510  * rsi_mac80211_sta_remove() - This function notifies driver about a peer
1511  *                 getting disconnected.
1512  * @hw: Pointer to the ieee80211_hw structure.
1513  * @vif: Pointer to the ieee80211_vif structure.
1514  * @sta: Pointer to the ieee80211_sta structure.
1515  *
1516  * Return: 0 on success, negative error codes on failure.
1517  */
1518 static int rsi_mac80211_sta_remove(struct ieee80211_hw *hw,
1519                    struct ieee80211_vif *vif,
1520                    struct ieee80211_sta *sta)
1521 {
1522     struct rsi_hw *adapter = hw->priv;
1523     struct rsi_common *common = adapter->priv;
1524     struct ieee80211_bss_conf *bss = &vif->bss_conf;
1525     struct rsi_sta *rsta;
1526 
1527     rsi_dbg(INFO_ZONE, "Station Remove: %pM\n", sta->addr);
1528 
1529     mutex_lock(&common->mutex);
1530 
1531     if ((vif->type == NL80211_IFTYPE_AP) ||
1532         (vif->type == NL80211_IFTYPE_P2P_GO)) {
1533         u8 sta_idx, cnt;
1534 
1535         /* Send peer notify to device */
1536         rsi_dbg(INFO_ZONE, "Indicate bss status to device\n");
1537         for (sta_idx = 0; sta_idx < common->max_stations; sta_idx++) {
1538             rsta = &common->stations[sta_idx];
1539 
1540             if (!rsta->sta)
1541                 continue;
1542             if (!memcmp(rsta->sta->addr, sta->addr, ETH_ALEN)) {
1543                 rsi_inform_bss_status(common, RSI_OPMODE_AP, 0,
1544                               sta->addr, sta->wme,
1545                               sta->aid, sta, sta_idx,
1546                               0, vif);
1547                 rsta->sta = NULL;
1548                 rsta->sta_id = -1;
1549                 for (cnt = 0; cnt < IEEE80211_NUM_TIDS; cnt++)
1550                     rsta->start_tx_aggr[cnt] = false;
1551                 if (common->num_stations > 0)
1552                     common->num_stations--;
1553                 break;
1554             }
1555         }
1556         if (sta_idx >= common->max_stations)
1557             rsi_dbg(ERR_ZONE, "%s: No station found\n", __func__);
1558     }
1559 
1560     if ((vif->type == NL80211_IFTYPE_STATION) ||
1561         (vif->type == NL80211_IFTYPE_P2P_CLIENT)) {
1562         /* Resetting all the fields to default values */
1563         memcpy((u8 *)bss->bssid, (u8 *)sta->addr, ETH_ALEN);
1564         bss->qos = sta->wme;
1565         common->bitrate_mask[NL80211_BAND_2GHZ] = 0;
1566         common->bitrate_mask[NL80211_BAND_5GHZ] = 0;
1567         common->vif_info[0].is_ht = false;
1568         common->vif_info[0].sgi = false;
1569         common->vif_info[0].seq_start = 0;
1570         common->secinfo.ptk_cipher = 0;
1571         common->secinfo.gtk_cipher = 0;
1572         if (!common->iface_down)
1573             rsi_send_rx_filter_frame(common, 0);
1574     }
1575     mutex_unlock(&common->mutex);
1576     
1577     return 0;
1578 }
1579 
1580 /**
1581  * rsi_mac80211_set_antenna() - This function is used to configure
1582  *              tx and rx antennas.
1583  * @hw: Pointer to the ieee80211_hw structure.
1584  * @tx_ant: Bitmap for tx antenna
1585  * @rx_ant: Bitmap for rx antenna
1586  *
1587  * Return: 0 on success, Negative error code on failure.
1588  */
1589 static int rsi_mac80211_set_antenna(struct ieee80211_hw *hw,
1590                     u32 tx_ant, u32 rx_ant)
1591 {
1592     struct rsi_hw *adapter = hw->priv;
1593     struct rsi_common *common = adapter->priv;
1594     u8 antenna = 0;
1595 
1596     if (tx_ant > 1 || rx_ant > 1) {
1597         rsi_dbg(ERR_ZONE,
1598             "Invalid antenna selection (tx: %d, rx:%d)\n",
1599             tx_ant, rx_ant);
1600         rsi_dbg(ERR_ZONE,
1601             "Use 0 for int_ant, 1 for ext_ant\n");
1602         return -EINVAL; 
1603     }
1604 
1605     rsi_dbg(INFO_ZONE, "%s: Antenna map Tx %x Rx %d\n",
1606             __func__, tx_ant, rx_ant);
1607 
1608     mutex_lock(&common->mutex);
1609 
1610     antenna = tx_ant ? ANTENNA_SEL_UFL : ANTENNA_SEL_INT;
1611     if (common->ant_in_use != antenna)
1612         if (rsi_set_antenna(common, antenna))
1613             goto fail_set_antenna;
1614 
1615     rsi_dbg(INFO_ZONE, "(%s) Antenna path configured successfully\n",
1616         tx_ant ? "UFL" : "INT");
1617 
1618     common->ant_in_use = antenna;
1619     
1620     mutex_unlock(&common->mutex);
1621     
1622     return 0;
1623 
1624 fail_set_antenna:
1625     rsi_dbg(ERR_ZONE, "%s: Failed.\n", __func__);
1626     mutex_unlock(&common->mutex);
1627     return -EINVAL;
1628 }
1629 
1630 /**
1631  * rsi_mac80211_get_antenna() - This function is used to configure 
1632  *              tx and rx antennas.
1633  *
1634  * @hw: Pointer to the ieee80211_hw structure.
1635  * @tx_ant: Bitmap for tx antenna
1636  * @rx_ant: Bitmap for rx antenna
1637  * 
1638  * Return: 0 on success, negative error codes on failure.
1639  */
1640 static int rsi_mac80211_get_antenna(struct ieee80211_hw *hw,
1641                     u32 *tx_ant, u32 *rx_ant)
1642 {
1643     struct rsi_hw *adapter = hw->priv;
1644     struct rsi_common *common = adapter->priv;
1645 
1646     mutex_lock(&common->mutex);
1647 
1648     *tx_ant = (common->ant_in_use == ANTENNA_SEL_UFL) ? 1 : 0;
1649     *rx_ant = 0;
1650 
1651     mutex_unlock(&common->mutex);
1652     
1653     return 0;   
1654 }
1655 
1656 static int rsi_map_region_code(enum nl80211_dfs_regions region_code)
1657 {
1658     switch (region_code) {
1659     case NL80211_DFS_FCC:
1660         return RSI_REGION_FCC;
1661     case NL80211_DFS_ETSI:
1662         return RSI_REGION_ETSI;
1663     case NL80211_DFS_JP:
1664         return RSI_REGION_TELEC;
1665     case NL80211_DFS_UNSET:
1666         return RSI_REGION_WORLD;
1667     }
1668     return RSI_REGION_WORLD;
1669 }
1670 
1671 static void rsi_reg_notify(struct wiphy *wiphy,
1672                struct regulatory_request *request)
1673 {
1674     struct ieee80211_supported_band *sband;
1675     struct ieee80211_channel *ch;
1676     struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1677     struct rsi_hw * adapter = hw->priv; 
1678     struct rsi_common *common = adapter->priv;
1679     int i;
1680     
1681     mutex_lock(&common->mutex);
1682 
1683     rsi_dbg(INFO_ZONE, "country = %s dfs_region = %d\n",
1684         request->alpha2, request->dfs_region);
1685 
1686     if (common->num_supp_bands > 1) {
1687         sband = wiphy->bands[NL80211_BAND_5GHZ];
1688 
1689         for (i = 0; i < sband->n_channels; i++) {
1690             ch = &sband->channels[i];
1691             if (ch->flags & IEEE80211_CHAN_DISABLED)
1692                 continue;
1693 
1694             if (ch->flags & IEEE80211_CHAN_RADAR)
1695                 ch->flags |= IEEE80211_CHAN_NO_IR;
1696         }
1697     }
1698     adapter->dfs_region = rsi_map_region_code(request->dfs_region);
1699     rsi_dbg(INFO_ZONE, "RSI region code = %d\n", adapter->dfs_region);
1700     
1701     adapter->country[0] = request->alpha2[0];
1702     adapter->country[1] = request->alpha2[1];
1703 
1704     mutex_unlock(&common->mutex);
1705 }
1706 
1707 static void rsi_mac80211_rfkill_poll(struct ieee80211_hw *hw)
1708 {
1709     struct rsi_hw *adapter = hw->priv;
1710     struct rsi_common *common = adapter->priv;
1711 
1712     mutex_lock(&common->mutex);
1713     if (common->fsm_state != FSM_MAC_INIT_DONE)
1714         wiphy_rfkill_set_hw_state(hw->wiphy, true);
1715     else
1716         wiphy_rfkill_set_hw_state(hw->wiphy, false);
1717     mutex_unlock(&common->mutex);
1718 }
1719 
1720 static void rsi_resume_conn_channel(struct rsi_common *common)
1721 {
1722     struct rsi_hw *adapter = common->priv;
1723     struct ieee80211_vif *vif;
1724     int cnt;
1725 
1726     for (cnt = 0; cnt < RSI_MAX_VIFS; cnt++) {
1727         vif = adapter->vifs[cnt];
1728         if (!vif)
1729             continue;
1730 
1731         if ((vif->type == NL80211_IFTYPE_AP) ||
1732             (vif->type == NL80211_IFTYPE_P2P_GO)) {
1733             rsi_switch_channel(adapter, vif);
1734             break;
1735         }
1736         if (((vif->type == NL80211_IFTYPE_STATION) ||
1737              (vif->type == NL80211_IFTYPE_P2P_CLIENT)) &&
1738             vif->cfg.assoc) {
1739             rsi_switch_channel(adapter, vif);
1740             break;
1741         }
1742     }
1743 }
1744 
1745 void rsi_roc_timeout(struct timer_list *t)
1746 {
1747     struct rsi_common *common = from_timer(common, t, roc_timer);
1748 
1749     rsi_dbg(INFO_ZONE, "Remain on channel expired\n");
1750 
1751     mutex_lock(&common->mutex);
1752     ieee80211_remain_on_channel_expired(common->priv->hw);
1753 
1754     if (timer_pending(&common->roc_timer))
1755         del_timer(&common->roc_timer);
1756 
1757     rsi_resume_conn_channel(common);
1758     mutex_unlock(&common->mutex);
1759 }
1760 
1761 static int rsi_mac80211_roc(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1762                 struct ieee80211_channel *chan, int duration,
1763                 enum ieee80211_roc_type type)
1764 {
1765     struct rsi_hw *adapter = (struct rsi_hw *)hw->priv;
1766     struct rsi_common *common = (struct rsi_common *)adapter->priv;
1767     int status = 0;
1768 
1769     rsi_dbg(INFO_ZONE, "***** Remain on channel *****\n");
1770 
1771     mutex_lock(&common->mutex);
1772     rsi_dbg(INFO_ZONE, "%s: channel: %d duration: %dms\n",
1773         __func__, chan->hw_value, duration);
1774 
1775     if (timer_pending(&common->roc_timer)) {
1776         rsi_dbg(INFO_ZONE, "Stop on-going ROC\n");
1777         del_timer(&common->roc_timer);
1778     }
1779     common->roc_timer.expires = msecs_to_jiffies(duration) + jiffies;
1780     add_timer(&common->roc_timer);
1781 
1782     /* Configure band */
1783     if (rsi_band_check(common, chan)) {
1784         rsi_dbg(ERR_ZONE, "Failed to set band\n");
1785         status = -EINVAL;
1786         goto out;
1787     }
1788 
1789     /* Configure channel */
1790     if (rsi_set_channel(common, chan)) {
1791         rsi_dbg(ERR_ZONE, "Failed to set the channel\n");
1792         status = -EINVAL;
1793         goto out;
1794     }
1795 
1796     common->roc_vif = vif;
1797     ieee80211_ready_on_channel(hw);
1798     rsi_dbg(INFO_ZONE, "%s: Ready on channel :%d\n",
1799         __func__, chan->hw_value);
1800 
1801 out:
1802     mutex_unlock(&common->mutex);
1803 
1804     return status;
1805 }
1806 
1807 static int rsi_mac80211_cancel_roc(struct ieee80211_hw *hw,
1808                    struct ieee80211_vif *vif)
1809 {
1810     struct rsi_hw *adapter = hw->priv;
1811     struct rsi_common *common = adapter->priv;
1812 
1813     rsi_dbg(INFO_ZONE, "Cancel remain on channel\n");
1814 
1815     mutex_lock(&common->mutex);
1816     if (!timer_pending(&common->roc_timer)) {
1817         mutex_unlock(&common->mutex);
1818         return 0;
1819     }
1820 
1821     del_timer(&common->roc_timer);
1822 
1823     rsi_resume_conn_channel(common);
1824     mutex_unlock(&common->mutex);
1825 
1826     return 0;
1827 }
1828 
1829 #ifdef CONFIG_PM
1830 static const struct wiphy_wowlan_support rsi_wowlan_support = {
1831     .flags = WIPHY_WOWLAN_ANY |
1832          WIPHY_WOWLAN_MAGIC_PKT |
1833          WIPHY_WOWLAN_DISCONNECT |
1834          WIPHY_WOWLAN_GTK_REKEY_FAILURE  |
1835          WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
1836          WIPHY_WOWLAN_EAP_IDENTITY_REQ   |
1837          WIPHY_WOWLAN_4WAY_HANDSHAKE,
1838 };
1839 
1840 static u16 rsi_wow_map_triggers(struct rsi_common *common,
1841                 struct cfg80211_wowlan *wowlan)
1842 {
1843     u16 wow_triggers = 0;
1844 
1845     rsi_dbg(INFO_ZONE, "Mapping wowlan triggers\n");
1846 
1847     if (wowlan->any)
1848         wow_triggers |= RSI_WOW_ANY;
1849     if (wowlan->magic_pkt)
1850         wow_triggers |= RSI_WOW_MAGIC_PKT;
1851     if (wowlan->disconnect)
1852         wow_triggers |= RSI_WOW_DISCONNECT;
1853     if (wowlan->gtk_rekey_failure || wowlan->eap_identity_req ||
1854         wowlan->four_way_handshake)
1855         wow_triggers |= RSI_WOW_GTK_REKEY;
1856 
1857     return wow_triggers;
1858 }
1859 
1860 int rsi_config_wowlan(struct rsi_hw *adapter, struct cfg80211_wowlan *wowlan)
1861 {
1862     struct rsi_common *common = adapter->priv;
1863     struct ieee80211_vif *vif = adapter->vifs[0];
1864     u16 triggers = 0;
1865     u16 rx_filter_word = 0;
1866 
1867     rsi_dbg(INFO_ZONE, "Config WoWLAN to device\n");
1868 
1869     if (!vif)
1870         return -EINVAL;
1871 
1872     if (WARN_ON(!wowlan)) {
1873         rsi_dbg(ERR_ZONE, "WoW triggers not enabled\n");
1874         return -EINVAL;
1875     }
1876 
1877     common->wow_flags |= RSI_WOW_ENABLED;
1878     triggers = rsi_wow_map_triggers(common, wowlan);
1879     if (!triggers) {
1880         rsi_dbg(ERR_ZONE, "%s:No valid WoW triggers\n", __func__);
1881         return -EINVAL;
1882     }
1883     if (!vif->cfg.assoc) {
1884         rsi_dbg(ERR_ZONE,
1885             "Cannot configure WoWLAN (Station not connected)\n");
1886         common->wow_flags |= RSI_WOW_NO_CONNECTION;
1887         return 0;
1888     }
1889     rsi_dbg(INFO_ZONE, "TRIGGERS %x\n", triggers);
1890 
1891     if (common->coex_mode > 1)
1892         rsi_disable_ps(adapter, adapter->vifs[0]);
1893 
1894     rsi_send_wowlan_request(common, triggers, 1);
1895 
1896     /**
1897      * Increase the beacon_miss threshold & keep-alive timers in
1898      * vap_update frame
1899      */
1900     rsi_send_vap_dynamic_update(common);
1901 
1902     rx_filter_word = (ALLOW_DATA_ASSOC_PEER | DISALLOW_BEACONS);
1903     rsi_send_rx_filter_frame(common, rx_filter_word);
1904 
1905     return 0;
1906 }
1907 EXPORT_SYMBOL(rsi_config_wowlan);
1908 
1909 static int rsi_mac80211_suspend(struct ieee80211_hw *hw,
1910                 struct cfg80211_wowlan *wowlan)
1911 {
1912     struct rsi_hw *adapter = hw->priv;
1913     struct rsi_common *common = adapter->priv;
1914 
1915     rsi_dbg(INFO_ZONE, "%s: mac80211 suspend\n", __func__);
1916     mutex_lock(&common->mutex);
1917     if (rsi_config_wowlan(adapter, wowlan)) {
1918         rsi_dbg(ERR_ZONE, "Failed to configure WoWLAN\n");
1919         mutex_unlock(&common->mutex);
1920         return 1;
1921     }
1922     mutex_unlock(&common->mutex);
1923 
1924     return 0;
1925 }
1926 
1927 static int rsi_mac80211_resume(struct ieee80211_hw *hw)
1928 {
1929     u16 rx_filter_word = 0;
1930     struct rsi_hw *adapter = hw->priv;
1931     struct rsi_common *common = adapter->priv;
1932 
1933     common->wow_flags = 0;
1934 
1935     rsi_dbg(INFO_ZONE, "%s: mac80211 resume\n", __func__);
1936 
1937     if (common->hibernate_resume) {
1938         common->mac_ops_resumed = true;
1939         /* Device need a complete restart of all MAC operations.
1940          * returning 1 will serve this purpose.
1941          */
1942         return 1;
1943     }
1944 
1945     mutex_lock(&common->mutex);
1946     rsi_send_wowlan_request(common, 0, 0);
1947 
1948     rx_filter_word = (ALLOW_DATA_ASSOC_PEER | ALLOW_CTRL_ASSOC_PEER |
1949               ALLOW_MGMT_ASSOC_PEER);
1950     rsi_send_rx_filter_frame(common, rx_filter_word);
1951     mutex_unlock(&common->mutex);
1952 
1953     return 0;
1954 }
1955 
1956 #endif
1957 
1958 static const struct ieee80211_ops mac80211_ops = {
1959     .tx = rsi_mac80211_tx,
1960     .start = rsi_mac80211_start,
1961     .stop = rsi_mac80211_stop,
1962     .add_interface = rsi_mac80211_add_interface,
1963     .remove_interface = rsi_mac80211_remove_interface,
1964     .config = rsi_mac80211_config,
1965     .bss_info_changed = rsi_mac80211_bss_info_changed,
1966     .conf_tx = rsi_mac80211_conf_tx,
1967     .configure_filter = rsi_mac80211_conf_filter,
1968     .set_key = rsi_mac80211_set_key,
1969     .set_rts_threshold = rsi_mac80211_set_rts_threshold,
1970     .set_bitrate_mask = rsi_mac80211_set_rate_mask,
1971     .ampdu_action = rsi_mac80211_ampdu_action,
1972     .sta_add = rsi_mac80211_sta_add,
1973     .sta_remove = rsi_mac80211_sta_remove,
1974     .set_antenna = rsi_mac80211_set_antenna,
1975     .get_antenna = rsi_mac80211_get_antenna,
1976     .rfkill_poll = rsi_mac80211_rfkill_poll,
1977     .remain_on_channel = rsi_mac80211_roc,
1978     .cancel_remain_on_channel = rsi_mac80211_cancel_roc,
1979 #ifdef CONFIG_PM
1980     .suspend = rsi_mac80211_suspend,
1981     .resume  = rsi_mac80211_resume,
1982 #endif
1983     .hw_scan = rsi_mac80211_hw_scan_start,
1984     .cancel_hw_scan = rsi_mac80211_cancel_hw_scan,
1985 };
1986 
1987 /**
1988  * rsi_mac80211_attach() - This function is used to initialize Mac80211 stack.
1989  * @common: Pointer to the driver private structure.
1990  *
1991  * Return: 0 on success, negative error codes on failure.
1992  */
1993 int rsi_mac80211_attach(struct rsi_common *common)
1994 {
1995     int status = 0;
1996     struct ieee80211_hw *hw = NULL;
1997     struct wiphy *wiphy = NULL;
1998     struct rsi_hw *adapter = common->priv;
1999     u8 addr_mask[ETH_ALEN] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x3};
2000 
2001     rsi_dbg(INIT_ZONE, "%s: Performing mac80211 attach\n", __func__);
2002 
2003     hw = ieee80211_alloc_hw(sizeof(struct rsi_hw), &mac80211_ops);
2004     if (!hw) {
2005         rsi_dbg(ERR_ZONE, "%s: ieee80211 hw alloc failed\n", __func__);
2006         return -ENOMEM;
2007     }
2008 
2009     wiphy = hw->wiphy;
2010 
2011     SET_IEEE80211_DEV(hw, adapter->device);
2012 
2013     hw->priv = adapter;
2014     adapter->hw = hw;
2015 
2016     ieee80211_hw_set(hw, SIGNAL_DBM);
2017     ieee80211_hw_set(hw, HAS_RATE_CONTROL);
2018     ieee80211_hw_set(hw, AMPDU_AGGREGATION);
2019     ieee80211_hw_set(hw, SUPPORTS_PS);
2020     ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
2021 
2022     hw->queues = MAX_HW_QUEUES;
2023     hw->extra_tx_headroom = RSI_NEEDED_HEADROOM;
2024 
2025     hw->max_rates = 1;
2026     hw->max_rate_tries = MAX_RETRIES;
2027     hw->uapsd_queues = RSI_IEEE80211_UAPSD_QUEUES;
2028     hw->uapsd_max_sp_len = IEEE80211_WMM_IE_STA_QOSINFO_SP_ALL;
2029 
2030     hw->max_tx_aggregation_subframes = RSI_MAX_TX_AGGR_FRMS;
2031     hw->max_rx_aggregation_subframes = RSI_MAX_RX_AGGR_FRMS;
2032     hw->rate_control_algorithm = "AARF";
2033 
2034     SET_IEEE80211_PERM_ADDR(hw, common->mac_addr);
2035     ether_addr_copy(hw->wiphy->addr_mask, addr_mask);
2036 
2037     wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
2038                  BIT(NL80211_IFTYPE_AP) |
2039                  BIT(NL80211_IFTYPE_P2P_DEVICE) |
2040                  BIT(NL80211_IFTYPE_P2P_CLIENT) |
2041                  BIT(NL80211_IFTYPE_P2P_GO);
2042 
2043     wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
2044     wiphy->retry_short = RETRY_SHORT;
2045     wiphy->retry_long  = RETRY_LONG;
2046     wiphy->frag_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
2047     wiphy->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
2048     wiphy->flags = 0;
2049 
2050     wiphy->available_antennas_rx = 1;
2051     wiphy->available_antennas_tx = 1;
2052 
2053     status = rsi_register_rates_channels(adapter, NL80211_BAND_2GHZ);
2054     if (status)
2055         return status;
2056     wiphy->bands[NL80211_BAND_2GHZ] =
2057         &adapter->sbands[NL80211_BAND_2GHZ];
2058     if (common->num_supp_bands > 1) {
2059         status = rsi_register_rates_channels(adapter,
2060                              NL80211_BAND_5GHZ);
2061         if (status)
2062             return status;
2063         wiphy->bands[NL80211_BAND_5GHZ] =
2064             &adapter->sbands[NL80211_BAND_5GHZ];
2065     }
2066 
2067     /* AP Parameters */
2068     wiphy->max_ap_assoc_sta = rsi_max_ap_stas[common->oper_mode - 1];
2069     common->max_stations = wiphy->max_ap_assoc_sta;
2070     rsi_dbg(ERR_ZONE, "Max Stations Allowed = %d\n", common->max_stations);
2071     hw->sta_data_size = sizeof(struct rsi_sta);
2072 
2073     wiphy->max_scan_ssids = RSI_MAX_SCAN_SSIDS;
2074     wiphy->max_scan_ie_len = RSI_MAX_SCAN_IE_LEN;
2075     wiphy->flags = WIPHY_FLAG_REPORTS_OBSS;
2076     wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
2077     wiphy->features |= NL80211_FEATURE_INACTIVITY_TIMER;
2078     wiphy->reg_notifier = rsi_reg_notify;
2079 
2080 #ifdef CONFIG_PM
2081     wiphy->wowlan = &rsi_wowlan_support;
2082 #endif
2083 
2084     wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
2085 
2086     /* Wi-Fi direct parameters */
2087     wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
2088     wiphy->flags |= WIPHY_FLAG_OFFCHAN_TX;
2089     wiphy->max_remain_on_channel_duration = 10000;
2090     hw->max_listen_interval = 10;
2091     wiphy->iface_combinations = rsi_iface_combinations;
2092     wiphy->n_iface_combinations = ARRAY_SIZE(rsi_iface_combinations);
2093 
2094     if (common->coex_mode > 1)
2095         wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
2096 
2097     status = ieee80211_register_hw(hw);
2098     if (status)
2099         return status;
2100 
2101     return rsi_init_dbgfs(adapter);
2102 }