Merge branch 'v3.10/topic/big.LITTLE' of git://git.linaro.org/kernel/linux-linaro...
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rtlwifi / core.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2009-2012  Realtek Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  * The full GNU General Public License is included in this distribution in the
19  * file called LICENSE.
20  *
21  * Contact Information:
22  * wlanfae <wlanfae@realtek.com>
23  * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
24  * Hsinchu 300, Taiwan.
25  *
26  * Larry Finger <Larry.Finger@lwfinger.net>
27  *
28  *****************************************************************************/
29
30 #include "wifi.h"
31 #include "core.h"
32 #include "cam.h"
33 #include "base.h"
34 #include "pci.h"
35 #include "ps.h"
36
37 #include <linux/export.h>
38
39 void rtl_fw_cb(const struct firmware *firmware, void *context)
40 {
41         struct ieee80211_hw *hw = context;
42         struct rtl_priv *rtlpriv = rtl_priv(hw);
43         int err;
44
45         RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
46                          "Firmware callback routine entered!\n");
47         complete(&rtlpriv->firmware_loading_complete);
48         if (!firmware) {
49                 if (rtlpriv->cfg->alt_fw_name) {
50                         err = request_firmware(&firmware,
51                                                rtlpriv->cfg->alt_fw_name,
52                                                rtlpriv->io.dev);
53                         pr_info("Loading alternative firmware %s\n",
54                                 rtlpriv->cfg->alt_fw_name);
55                         if (!err)
56                                 goto found_alt;
57                 }
58                 pr_err("Firmware %s not available\n", rtlpriv->cfg->fw_name);
59                 rtlpriv->max_fw_size = 0;
60                 return;
61         }
62 found_alt:
63         if (firmware->size > rtlpriv->max_fw_size) {
64                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
65                          "Firmware is too big!\n");
66                 release_firmware(firmware);
67                 return;
68         }
69         memcpy(rtlpriv->rtlhal.pfirmware, firmware->data, firmware->size);
70         rtlpriv->rtlhal.fwsize = firmware->size;
71         release_firmware(firmware);
72
73         err = ieee80211_register_hw(hw);
74         if (err) {
75                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
76                          "Can't register mac80211 hw\n");
77                 return;
78         } else {
79                 rtlpriv->mac80211.mac80211_registered = 1;
80         }
81         set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
82
83         /*init rfkill */
84         rtl_init_rfkill(hw);
85 }
86 EXPORT_SYMBOL(rtl_fw_cb);
87
88 /*mutex for start & stop is must here. */
89 static int rtl_op_start(struct ieee80211_hw *hw)
90 {
91         int err;
92         struct rtl_priv *rtlpriv = rtl_priv(hw);
93         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
94
95         if (!is_hal_stop(rtlhal))
96                 return 0;
97         if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
98                 return 0;
99         mutex_lock(&rtlpriv->locks.conf_mutex);
100         err = rtlpriv->intf_ops->adapter_start(hw);
101         if (!err)
102                 rtl_watch_dog_timer_callback((unsigned long)hw);
103         mutex_unlock(&rtlpriv->locks.conf_mutex);
104         return err;
105 }
106
107 static void rtl_op_stop(struct ieee80211_hw *hw)
108 {
109         struct rtl_priv *rtlpriv = rtl_priv(hw);
110         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
111         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
112         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
113
114         if (is_hal_stop(rtlhal))
115                 return;
116
117         /* here is must, because adhoc do stop and start,
118          * but stop with RFOFF may cause something wrong,
119          * like adhoc TP
120          */
121         if (unlikely(ppsc->rfpwr_state == ERFOFF)) {
122                 rtl_ips_nic_on(hw);
123         }
124
125         mutex_lock(&rtlpriv->locks.conf_mutex);
126
127         mac->link_state = MAC80211_NOLINK;
128         memset(mac->bssid, 0, 6);
129         mac->vendor = PEER_UNKNOWN;
130
131         /*reset sec info */
132         rtl_cam_reset_sec_info(hw);
133
134         rtl_deinit_deferred_work(hw);
135         rtlpriv->intf_ops->adapter_stop(hw);
136
137         mutex_unlock(&rtlpriv->locks.conf_mutex);
138 }
139
140 static void rtl_op_tx(struct ieee80211_hw *hw,
141                       struct ieee80211_tx_control *control,
142                       struct sk_buff *skb)
143 {
144         struct rtl_priv *rtlpriv = rtl_priv(hw);
145         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
146         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
147         struct rtl_tcb_desc tcb_desc;
148         memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
149
150         if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
151                 goto err_free;
152
153         if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
154                 goto err_free;
155
156         if (!rtlpriv->intf_ops->waitq_insert(hw, control->sta, skb))
157                 rtlpriv->intf_ops->adapter_tx(hw, control->sta, skb, &tcb_desc);
158
159         return;
160
161 err_free:
162         dev_kfree_skb_any(skb);
163 }
164
165 static int rtl_op_add_interface(struct ieee80211_hw *hw,
166                 struct ieee80211_vif *vif)
167 {
168         struct rtl_priv *rtlpriv = rtl_priv(hw);
169         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
170         int err = 0;
171
172         vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
173
174         if (mac->vif) {
175                 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
176                          "vif has been set!! mac->vif = 0x%p\n", mac->vif);
177                 return -EOPNOTSUPP;
178         }
179
180         rtl_ips_nic_on(hw);
181
182         mutex_lock(&rtlpriv->locks.conf_mutex);
183
184         switch (ieee80211_vif_type_p2p(vif)) {
185         case NL80211_IFTYPE_P2P_CLIENT:
186                 mac->p2p = P2P_ROLE_CLIENT;
187                 /*fall through*/
188         case NL80211_IFTYPE_STATION:
189                 if (mac->beacon_enabled == 1) {
190                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
191                                  "NL80211_IFTYPE_STATION\n");
192                         mac->beacon_enabled = 0;
193                         rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
194                                         rtlpriv->cfg->maps
195                                         [RTL_IBSS_INT_MASKS]);
196                 }
197                 mac->link_state = MAC80211_LINKED;
198                 break;
199         case NL80211_IFTYPE_ADHOC:
200                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
201                          "NL80211_IFTYPE_ADHOC\n");
202
203                 mac->link_state = MAC80211_LINKED;
204                 rtlpriv->cfg->ops->set_bcn_reg(hw);
205                 if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
206                         mac->basic_rates = 0xfff;
207                 else
208                         mac->basic_rates = 0xff0;
209                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
210                                 (u8 *) (&mac->basic_rates));
211
212                 break;
213         case NL80211_IFTYPE_P2P_GO:
214                 mac->p2p = P2P_ROLE_GO;
215                 /*fall through*/
216         case NL80211_IFTYPE_AP:
217                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
218                          "NL80211_IFTYPE_AP\n");
219
220                 mac->link_state = MAC80211_LINKED;
221                 rtlpriv->cfg->ops->set_bcn_reg(hw);
222                 if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
223                         mac->basic_rates = 0xfff;
224                 else
225                         mac->basic_rates = 0xff0;
226                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
227                                 (u8 *) (&mac->basic_rates));
228                 break;
229         case NL80211_IFTYPE_MESH_POINT:
230                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
231                          "NL80211_IFTYPE_MESH_POINT\n");
232
233                 mac->link_state = MAC80211_LINKED;
234                 rtlpriv->cfg->ops->set_bcn_reg(hw);
235                 if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
236                         mac->basic_rates = 0xfff;
237                 else
238                         mac->basic_rates = 0xff0;
239                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
240                                 (u8 *)(&mac->basic_rates));
241                 break;
242         default:
243                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
244                          "operation mode %d is not supported!\n", vif->type);
245                 err = -EOPNOTSUPP;
246                 goto out;
247         }
248
249         if (mac->p2p) {
250                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
251                          "p2p role %x\n", vif->type);
252                 mac->basic_rates = 0xff0;/*disable cck rate for p2p*/
253                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
254                                 (u8 *)(&mac->basic_rates));
255         }
256         mac->vif = vif;
257         mac->opmode = vif->type;
258         rtlpriv->cfg->ops->set_network_type(hw, vif->type);
259         memcpy(mac->mac_addr, vif->addr, ETH_ALEN);
260         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ETHER_ADDR, mac->mac_addr);
261
262 out:
263         mutex_unlock(&rtlpriv->locks.conf_mutex);
264         return err;
265 }
266
267 static void rtl_op_remove_interface(struct ieee80211_hw *hw,
268                 struct ieee80211_vif *vif)
269 {
270         struct rtl_priv *rtlpriv = rtl_priv(hw);
271         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
272
273         mutex_lock(&rtlpriv->locks.conf_mutex);
274
275         /* Free beacon resources */
276         if ((vif->type == NL80211_IFTYPE_AP) ||
277             (vif->type == NL80211_IFTYPE_ADHOC) ||
278             (vif->type == NL80211_IFTYPE_MESH_POINT)) {
279                 if (mac->beacon_enabled == 1) {
280                         mac->beacon_enabled = 0;
281                         rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
282                                         rtlpriv->cfg->maps
283                                         [RTL_IBSS_INT_MASKS]);
284                 }
285         }
286
287         /*
288          *Note: We assume NL80211_IFTYPE_UNSPECIFIED as
289          *NO LINK for our hardware.
290          */
291         mac->p2p = 0;
292         mac->vif = NULL;
293         mac->link_state = MAC80211_NOLINK;
294         memset(mac->bssid, 0, 6);
295         mac->vendor = PEER_UNKNOWN;
296         mac->opmode = NL80211_IFTYPE_UNSPECIFIED;
297         rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
298         mutex_unlock(&rtlpriv->locks.conf_mutex);
299 }
300
301 static int rtl_op_change_interface(struct ieee80211_hw *hw,
302                                       struct ieee80211_vif *vif,
303                                       enum nl80211_iftype new_type, bool p2p)
304 {
305         struct rtl_priv *rtlpriv = rtl_priv(hw);
306         int ret;
307         rtl_op_remove_interface(hw, vif);
308
309         vif->type = new_type;
310         vif->p2p = p2p;
311         ret = rtl_op_add_interface(hw, vif);
312         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
313                  "p2p %x\n", p2p);
314         return ret;
315 }
316
317 static int rtl_op_config(struct ieee80211_hw *hw, u32 changed)
318 {
319         struct rtl_priv *rtlpriv = rtl_priv(hw);
320         struct rtl_phy *rtlphy = &(rtlpriv->phy);
321         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
322         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
323         struct ieee80211_conf *conf = &hw->conf;
324
325         if (mac->skip_scan)
326                 return 1;
327
328         mutex_lock(&rtlpriv->locks.conf_mutex);
329         if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {  /*BIT(2)*/
330                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
331                          "IEEE80211_CONF_CHANGE_LISTEN_INTERVAL\n");
332         }
333
334         /*For IPS */
335         if (changed & IEEE80211_CONF_CHANGE_IDLE) {
336                 if (hw->conf.flags & IEEE80211_CONF_IDLE)
337                         rtl_ips_nic_off(hw);
338                 else
339                         rtl_ips_nic_on(hw);
340         } else {
341                 /*
342                  *although rfoff may not cause by ips, but we will
343                  *check the reason in set_rf_power_state function
344                  */
345                 if (unlikely(ppsc->rfpwr_state == ERFOFF))
346                         rtl_ips_nic_on(hw);
347         }
348
349         /*For LPS */
350         if (changed & IEEE80211_CONF_CHANGE_PS) {
351                 cancel_delayed_work(&rtlpriv->works.ps_work);
352                 cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
353                 if (conf->flags & IEEE80211_CONF_PS) {
354                         rtlpriv->psc.sw_ps_enabled = true;
355                         /* sleep here is must, or we may recv the beacon and
356                          * cause mac80211 into wrong ps state, this will cause
357                          * power save nullfunc send fail, and further cause
358                          * pkt loss, So sleep must quickly but not immediatly
359                          * because that will cause nullfunc send by mac80211
360                          * fail, and cause pkt loss, we have tested that 5mA
361                          * is worked very well */
362                         if (!rtlpriv->psc.multi_buffered)
363                                 queue_delayed_work(rtlpriv->works.rtl_wq,
364                                                 &rtlpriv->works.ps_work,
365                                                 MSECS(5));
366                 } else {
367                         rtl_swlps_rf_awake(hw);
368                         rtlpriv->psc.sw_ps_enabled = false;
369                 }
370         }
371
372         if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
373                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
374                          "IEEE80211_CONF_CHANGE_RETRY_LIMITS %x\n",
375                          hw->conf.long_frame_max_tx_count);
376                 mac->retry_long = hw->conf.long_frame_max_tx_count;
377                 mac->retry_short = hw->conf.long_frame_max_tx_count;
378                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RETRY_LIMIT,
379                                               (u8 *) (&hw->conf.
380                                                       long_frame_max_tx_count));
381         }
382
383         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
384                 struct ieee80211_channel *channel = hw->conf.chandef.chan;
385                 u8 wide_chan = (u8) channel->hw_value;
386
387                 if (mac->act_scanning)
388                         mac->n_channels++;
389
390                 if (rtlpriv->dm.supp_phymode_switch &&
391                     mac->link_state < MAC80211_LINKED &&
392                     !mac->act_scanning) {
393                         if (rtlpriv->cfg->ops->chk_switch_dmdp)
394                                 rtlpriv->cfg->ops->chk_switch_dmdp(hw);
395                 }
396
397                 /*
398                  *because we should back channel to
399                  *current_network.chan in in scanning,
400                  *So if set_chan == current_network.chan
401                  *we should set it.
402                  *because mac80211 tell us wrong bw40
403                  *info for cisco1253 bw20, so we modify
404                  *it here based on UPPER & LOWER
405                  */
406                 switch (cfg80211_get_chandef_type(&hw->conf.chandef)) {
407                 case NL80211_CHAN_HT20:
408                 case NL80211_CHAN_NO_HT:
409                         /* SC */
410                         mac->cur_40_prime_sc =
411                                 PRIME_CHNL_OFFSET_DONT_CARE;
412                         rtlphy->current_chan_bw = HT_CHANNEL_WIDTH_20;
413                         mac->bw_40 = false;
414                         break;
415                 case NL80211_CHAN_HT40MINUS:
416                         /* SC */
417                         mac->cur_40_prime_sc = PRIME_CHNL_OFFSET_UPPER;
418                         rtlphy->current_chan_bw =
419                                 HT_CHANNEL_WIDTH_20_40;
420                         mac->bw_40 = true;
421
422                         /*wide channel */
423                         wide_chan -= 2;
424
425                         break;
426                 case NL80211_CHAN_HT40PLUS:
427                         /* SC */
428                         mac->cur_40_prime_sc = PRIME_CHNL_OFFSET_LOWER;
429                         rtlphy->current_chan_bw =
430                                 HT_CHANNEL_WIDTH_20_40;
431                         mac->bw_40 = true;
432
433                         /*wide channel */
434                         wide_chan += 2;
435
436                         break;
437                 default:
438                         mac->bw_40 = false;
439                         RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
440                                  "switch case not processed\n");
441                         break;
442                 }
443
444                 if (wide_chan <= 0)
445                         wide_chan = 1;
446
447                 /* In scanning, before we go offchannel we may send a ps = 1
448                  * null to AP, and then we may send a ps = 0 null to AP quickly,
449                  * but first null may have caused AP to put lots of packet to
450                  * hw tx buffer. These packets must be tx'd before we go off
451                  * channel so we must delay more time to let AP flush these
452                  * packets before going offchannel, or dis-association or
453                  * delete BA will be caused by AP
454                  */
455                 if (rtlpriv->mac80211.offchan_delay) {
456                         rtlpriv->mac80211.offchan_delay = false;
457                         mdelay(50);
458                 }
459                 rtlphy->current_channel = wide_chan;
460
461                 rtlpriv->cfg->ops->switch_channel(hw);
462                 rtlpriv->cfg->ops->set_channel_access(hw);
463                 rtlpriv->cfg->ops->set_bw_mode(hw,
464                                 cfg80211_get_chandef_type(&hw->conf.chandef));
465         }
466
467         mutex_unlock(&rtlpriv->locks.conf_mutex);
468
469         return 0;
470 }
471
472 static void rtl_op_configure_filter(struct ieee80211_hw *hw,
473                              unsigned int changed_flags,
474                              unsigned int *new_flags, u64 multicast)
475 {
476         struct rtl_priv *rtlpriv = rtl_priv(hw);
477         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
478
479         *new_flags &= RTL_SUPPORTED_FILTERS;
480         if (!changed_flags)
481                 return;
482
483         /*TODO: we disable broadcase now, so enable here */
484         if (changed_flags & FIF_ALLMULTI) {
485                 if (*new_flags & FIF_ALLMULTI) {
486                         mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AM] |
487                             rtlpriv->cfg->maps[MAC_RCR_AB];
488                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
489                                  "Enable receive multicast frame\n");
490                 } else {
491                         mac->rx_conf &= ~(rtlpriv->cfg->maps[MAC_RCR_AM] |
492                                           rtlpriv->cfg->maps[MAC_RCR_AB]);
493                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
494                                  "Disable receive multicast frame\n");
495                 }
496         }
497
498         if (changed_flags & FIF_FCSFAIL) {
499                 if (*new_flags & FIF_FCSFAIL) {
500                         mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACRC32];
501                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
502                                  "Enable receive FCS error frame\n");
503                 } else {
504                         mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACRC32];
505                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
506                                  "Disable receive FCS error frame\n");
507                 }
508         }
509
510         /* if ssid not set to hw don't check bssid
511          * here just used for linked scanning, & linked
512          * and nolink check bssid is set in set network_type */
513         if ((changed_flags & FIF_BCN_PRBRESP_PROMISC) &&
514                 (mac->link_state >= MAC80211_LINKED)) {
515                 if (mac->opmode != NL80211_IFTYPE_AP &&
516                     mac->opmode != NL80211_IFTYPE_MESH_POINT) {
517                         if (*new_flags & FIF_BCN_PRBRESP_PROMISC) {
518                                 rtlpriv->cfg->ops->set_chk_bssid(hw, false);
519                         } else {
520                                 rtlpriv->cfg->ops->set_chk_bssid(hw, true);
521                         }
522                 }
523         }
524
525         if (changed_flags & FIF_CONTROL) {
526                 if (*new_flags & FIF_CONTROL) {
527                         mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACF];
528
529                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
530                                  "Enable receive control frame\n");
531                 } else {
532                         mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACF];
533                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
534                                  "Disable receive control frame\n");
535                 }
536         }
537
538         if (changed_flags & FIF_OTHER_BSS) {
539                 if (*new_flags & FIF_OTHER_BSS) {
540                         mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AAP];
541                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
542                                  "Enable receive other BSS's frame\n");
543                 } else {
544                         mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_AAP];
545                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
546                                  "Disable receive other BSS's frame\n");
547                 }
548         }
549 }
550 static int rtl_op_sta_add(struct ieee80211_hw *hw,
551                          struct ieee80211_vif *vif,
552                          struct ieee80211_sta *sta)
553 {
554         struct rtl_priv *rtlpriv = rtl_priv(hw);
555         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
556         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
557         struct rtl_sta_info *sta_entry;
558
559         if (sta) {
560                 sta_entry = (struct rtl_sta_info *) sta->drv_priv;
561                 spin_lock_bh(&rtlpriv->locks.entry_list_lock);
562                 list_add_tail(&sta_entry->list, &rtlpriv->entry_list);
563                 spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
564                 if (rtlhal->current_bandtype == BAND_ON_2_4G) {
565                         sta_entry->wireless_mode = WIRELESS_MODE_G;
566                         if (sta->supp_rates[0] <= 0xf)
567                                 sta_entry->wireless_mode = WIRELESS_MODE_B;
568                         if (sta->ht_cap.ht_supported == true)
569                                 sta_entry->wireless_mode = WIRELESS_MODE_N_24G;
570
571                         if (vif->type == NL80211_IFTYPE_ADHOC)
572                                 sta_entry->wireless_mode = WIRELESS_MODE_G;
573                 } else if (rtlhal->current_bandtype == BAND_ON_5G) {
574                         sta_entry->wireless_mode = WIRELESS_MODE_A;
575                         if (sta->ht_cap.ht_supported == true)
576                                 sta_entry->wireless_mode = WIRELESS_MODE_N_24G;
577
578                         if (vif->type == NL80211_IFTYPE_ADHOC)
579                                 sta_entry->wireless_mode = WIRELESS_MODE_A;
580                 }
581                 /*disable cck rate for p2p*/
582                 if (mac->p2p)
583                         sta->supp_rates[0] &= 0xfffffff0;
584
585                 memcpy(sta_entry->mac_addr, sta->addr, ETH_ALEN);
586                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
587                          "Add sta addr is %pM\n", sta->addr);
588                 rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0);
589         }
590         return 0;
591 }
592
593 static int rtl_op_sta_remove(struct ieee80211_hw *hw,
594                                 struct ieee80211_vif *vif,
595                                 struct ieee80211_sta *sta)
596 {
597         struct rtl_priv *rtlpriv = rtl_priv(hw);
598         struct rtl_sta_info *sta_entry;
599         if (sta) {
600                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
601                          "Remove sta addr is %pM\n", sta->addr);
602                 sta_entry = (struct rtl_sta_info *) sta->drv_priv;
603                 sta_entry->wireless_mode = 0;
604                 sta_entry->ratr_index = 0;
605
606                 spin_lock_bh(&rtlpriv->locks.entry_list_lock);
607                 list_del(&sta_entry->list);
608                 spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
609         }
610         return 0;
611 }
612
613 static int _rtl_get_hal_qnum(u16 queue)
614 {
615         int qnum;
616
617         switch (queue) {
618         case 0:
619                 qnum = AC3_VO;
620                 break;
621         case 1:
622                 qnum = AC2_VI;
623                 break;
624         case 2:
625                 qnum = AC0_BE;
626                 break;
627         case 3:
628                 qnum = AC1_BK;
629                 break;
630         default:
631                 qnum = AC0_BE;
632                 break;
633         }
634         return qnum;
635 }
636
637 /*
638  *for mac80211 VO = 0, VI = 1, BE = 2, BK = 3
639  *for rtl819x  BE = 0, BK = 1, VI = 2, VO = 3
640  */
641 static int rtl_op_conf_tx(struct ieee80211_hw *hw,
642                    struct ieee80211_vif *vif, u16 queue,
643                    const struct ieee80211_tx_queue_params *param)
644 {
645         struct rtl_priv *rtlpriv = rtl_priv(hw);
646         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
647         int aci;
648
649         if (queue >= AC_MAX) {
650                 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
651                          "queue number %d is incorrect!\n", queue);
652                 return -EINVAL;
653         }
654
655         aci = _rtl_get_hal_qnum(queue);
656         mac->ac[aci].aifs = param->aifs;
657         mac->ac[aci].cw_min = cpu_to_le16(param->cw_min);
658         mac->ac[aci].cw_max = cpu_to_le16(param->cw_max);
659         mac->ac[aci].tx_op = cpu_to_le16(param->txop);
660         memcpy(&mac->edca_param[aci], param, sizeof(*param));
661         rtlpriv->cfg->ops->set_qos(hw, aci);
662         return 0;
663 }
664
665 static void rtl_op_bss_info_changed(struct ieee80211_hw *hw,
666                              struct ieee80211_vif *vif,
667                              struct ieee80211_bss_conf *bss_conf, u32 changed)
668 {
669         struct rtl_priv *rtlpriv = rtl_priv(hw);
670         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
671         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
672         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
673         struct ieee80211_sta *sta = NULL;
674
675         mutex_lock(&rtlpriv->locks.conf_mutex);
676         if ((vif->type == NL80211_IFTYPE_ADHOC) ||
677             (vif->type == NL80211_IFTYPE_AP) ||
678             (vif->type == NL80211_IFTYPE_MESH_POINT)) {
679                 if ((changed & BSS_CHANGED_BEACON) ||
680                     (changed & BSS_CHANGED_BEACON_ENABLED &&
681                      bss_conf->enable_beacon)) {
682                         if (mac->beacon_enabled == 0) {
683                                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
684                                          "BSS_CHANGED_BEACON_ENABLED\n");
685
686                                 /*start hw beacon interrupt. */
687                                 /*rtlpriv->cfg->ops->set_bcn_reg(hw); */
688                                 mac->beacon_enabled = 1;
689                                 rtlpriv->cfg->ops->update_interrupt_mask(hw,
690                                                 rtlpriv->cfg->maps
691                                                 [RTL_IBSS_INT_MASKS],
692                                                 0);
693
694                                 if (rtlpriv->cfg->ops->linked_set_reg)
695                                         rtlpriv->cfg->ops->linked_set_reg(hw);
696                         }
697                 }
698                 if ((changed & BSS_CHANGED_BEACON_ENABLED &&
699                         !bss_conf->enable_beacon)) {
700                         if (mac->beacon_enabled == 1) {
701                                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
702                                          "ADHOC DISABLE BEACON\n");
703
704                                 mac->beacon_enabled = 0;
705                                 rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
706                                                 rtlpriv->cfg->maps
707                                                 [RTL_IBSS_INT_MASKS]);
708                         }
709                 }
710                 if (changed & BSS_CHANGED_BEACON_INT) {
711                         RT_TRACE(rtlpriv, COMP_BEACON, DBG_TRACE,
712                                  "BSS_CHANGED_BEACON_INT\n");
713                         mac->beacon_interval = bss_conf->beacon_int;
714                         rtlpriv->cfg->ops->set_bcn_intv(hw);
715                 }
716         }
717
718         /*TODO: reference to enum ieee80211_bss_change */
719         if (changed & BSS_CHANGED_ASSOC) {
720                 if (bss_conf->assoc) {
721                         struct ieee80211_sta *sta = NULL;
722                         /* we should reset all sec info & cam
723                          * before set cam after linked, we should not
724                          * reset in disassoc, that will cause tkip->wep
725                          * fail because some flag will be wrong */
726                         /* reset sec info */
727                         rtl_cam_reset_sec_info(hw);
728                         /* reset cam to fix wep fail issue
729                          * when change from wpa to wep */
730                         rtl_cam_reset_all_entry(hw);
731
732                         mac->link_state = MAC80211_LINKED;
733                         mac->cnt_after_linked = 0;
734                         mac->assoc_id = bss_conf->aid;
735                         memcpy(mac->bssid, bss_conf->bssid, 6);
736
737                         if (rtlpriv->cfg->ops->linked_set_reg)
738                                 rtlpriv->cfg->ops->linked_set_reg(hw);
739                         rcu_read_lock();
740                         sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
741
742                         if (vif->type == NL80211_IFTYPE_STATION && sta)
743                                 rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0);
744                         RT_TRACE(rtlpriv, COMP_EASY_CONCURRENT, DBG_LOUD,
745                                  "send PS STATIC frame\n");
746                         if (rtlpriv->dm.supp_phymode_switch) {
747                                 if (sta->ht_cap.ht_supported)
748                                         rtl_send_smps_action(hw, sta,
749                                                  IEEE80211_SMPS_STATIC);
750                         }
751                         rcu_read_unlock();
752
753                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
754                                  "BSS_CHANGED_ASSOC\n");
755                 } else {
756                         if (mac->link_state == MAC80211_LINKED) {
757                                 rtlpriv->enter_ps = false;
758                                 schedule_work(&rtlpriv->works.lps_change_work);
759                         }
760
761                         if (ppsc->p2p_ps_info.p2p_ps_mode > P2P_PS_NONE)
762                                 rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
763                         mac->link_state = MAC80211_NOLINK;
764                         memset(mac->bssid, 0, 6);
765                         mac->vendor = PEER_UNKNOWN;
766
767                         if (rtlpriv->dm.supp_phymode_switch) {
768                                 if (rtlpriv->cfg->ops->chk_switch_dmdp)
769                                         rtlpriv->cfg->ops->chk_switch_dmdp(hw);
770                         }
771
772                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
773                                  "BSS_CHANGED_UN_ASSOC\n");
774                 }
775         }
776
777         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
778                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
779                          "BSS_CHANGED_ERP_CTS_PROT\n");
780                 mac->use_cts_protect = bss_conf->use_cts_prot;
781         }
782
783         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
784                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
785                          "BSS_CHANGED_ERP_PREAMBLE use short preamble:%x\n",
786                          bss_conf->use_short_preamble);
787
788                 mac->short_preamble = bss_conf->use_short_preamble;
789                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ACK_PREAMBLE,
790                                               &mac->short_preamble);
791         }
792
793         if (changed & BSS_CHANGED_ERP_SLOT) {
794                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
795                          "BSS_CHANGED_ERP_SLOT\n");
796
797                 if (bss_conf->use_short_slot)
798                         mac->slot_time = RTL_SLOT_TIME_9;
799                 else
800                         mac->slot_time = RTL_SLOT_TIME_20;
801
802                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SLOT_TIME,
803                                               &mac->slot_time);
804         }
805
806         if (changed & BSS_CHANGED_HT) {
807                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE, "BSS_CHANGED_HT\n");
808                 rcu_read_lock();
809                 sta = get_sta(hw, vif, bss_conf->bssid);
810                 if (sta) {
811                         if (sta->ht_cap.ampdu_density >
812                             mac->current_ampdu_density)
813                                 mac->current_ampdu_density =
814                                     sta->ht_cap.ampdu_density;
815                         if (sta->ht_cap.ampdu_factor <
816                             mac->current_ampdu_factor)
817                                 mac->current_ampdu_factor =
818                                     sta->ht_cap.ampdu_factor;
819                 }
820                 rcu_read_unlock();
821
822                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SHORTGI_DENSITY,
823                                               &mac->max_mss_density);
824                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_FACTOR,
825                                               &mac->current_ampdu_factor);
826                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_MIN_SPACE,
827                                               &mac->current_ampdu_density);
828         }
829
830         if (changed & BSS_CHANGED_BSSID) {
831                 u32 basic_rates;
832
833                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BSSID,
834                                               (u8 *) bss_conf->bssid);
835
836                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG, "%pM\n",
837                          bss_conf->bssid);
838
839                 mac->vendor = PEER_UNKNOWN;
840                 memcpy(mac->bssid, bss_conf->bssid, 6);
841                 rtlpriv->cfg->ops->set_network_type(hw, vif->type);
842
843                 rcu_read_lock();
844                 sta = get_sta(hw, vif, bss_conf->bssid);
845                 if (!sta) {
846                         rcu_read_unlock();
847                         goto out;
848                 }
849
850                 if (rtlhal->current_bandtype == BAND_ON_5G) {
851                         mac->mode = WIRELESS_MODE_A;
852                 } else {
853                         if (sta->supp_rates[0] <= 0xf)
854                                 mac->mode = WIRELESS_MODE_B;
855                         else
856                                 mac->mode = WIRELESS_MODE_G;
857                 }
858
859                 if (sta->ht_cap.ht_supported) {
860                         if (rtlhal->current_bandtype == BAND_ON_2_4G)
861                                 mac->mode = WIRELESS_MODE_N_24G;
862                         else
863                                 mac->mode = WIRELESS_MODE_N_5G;
864                 }
865
866                 /* just station need it, because ibss & ap mode will
867                  * set in sta_add, and will be NULL here */
868                 if (mac->opmode == NL80211_IFTYPE_STATION) {
869                         struct rtl_sta_info *sta_entry;
870                         sta_entry = (struct rtl_sta_info *) sta->drv_priv;
871                         sta_entry->wireless_mode = mac->mode;
872                 }
873
874                 if (sta->ht_cap.ht_supported) {
875                         mac->ht_enable = true;
876
877                         /*
878                          * for cisco 1252 bw20 it's wrong
879                          * if (ht_cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
880                          *      mac->bw_40 = true;
881                          * }
882                          * */
883                 }
884
885                 if (changed & BSS_CHANGED_BASIC_RATES) {
886                         /* for 5G must << RATE_6M_INDEX = 4,
887                          * because 5G have no cck rate*/
888                         if (rtlhal->current_bandtype == BAND_ON_5G)
889                                 basic_rates = sta->supp_rates[1] << 4;
890                         else
891                                 basic_rates = sta->supp_rates[0];
892
893                         mac->basic_rates = basic_rates;
894                         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
895                                         (u8 *) (&basic_rates));
896                 }
897                 rcu_read_unlock();
898         }
899
900         /*
901          * For FW LPS:
902          * To tell firmware we have connected
903          * to an AP. For 92SE/CE power save v2.
904          */
905         if (changed & BSS_CHANGED_ASSOC) {
906                 if (bss_conf->assoc) {
907                         if (ppsc->fwctrl_lps) {
908                                 u8 mstatus = RT_MEDIA_CONNECT;
909                                 rtlpriv->cfg->ops->set_hw_reg(hw,
910                                                       HW_VAR_H2C_FW_JOINBSSRPT,
911                                                       &mstatus);
912                                 ppsc->report_linked = true;
913                         }
914                 } else {
915                         if (ppsc->fwctrl_lps) {
916                                 u8 mstatus = RT_MEDIA_DISCONNECT;
917                                 rtlpriv->cfg->ops->set_hw_reg(hw,
918                                                       HW_VAR_H2C_FW_JOINBSSRPT,
919                                                       &mstatus);
920                                 ppsc->report_linked = false;
921                         }
922                 }
923                 if (rtlpriv->cfg->ops->bt_wifi_media_status_notify)
924                         rtlpriv->cfg->ops->bt_wifi_media_status_notify(hw,
925                                                          ppsc->report_linked);
926         }
927
928 out:
929         mutex_unlock(&rtlpriv->locks.conf_mutex);
930 }
931
932 static u64 rtl_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
933 {
934         struct rtl_priv *rtlpriv = rtl_priv(hw);
935         u64 tsf;
936
937         rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *) (&tsf));
938         return tsf;
939 }
940
941 static void rtl_op_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
942                            u64 tsf)
943 {
944         struct rtl_priv *rtlpriv = rtl_priv(hw);
945         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
946         u8 bibss = (mac->opmode == NL80211_IFTYPE_ADHOC) ? 1 : 0;
947
948         mac->tsf = tsf;
949         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_CORRECT_TSF, &bibss);
950 }
951
952 static void rtl_op_reset_tsf(struct ieee80211_hw *hw,
953                              struct ieee80211_vif *vif)
954 {
955         struct rtl_priv *rtlpriv = rtl_priv(hw);
956         u8 tmp = 0;
957
958         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_DUAL_TSF_RST, &tmp);
959 }
960
961 static void rtl_op_sta_notify(struct ieee80211_hw *hw,
962                               struct ieee80211_vif *vif,
963                               enum sta_notify_cmd cmd,
964                               struct ieee80211_sta *sta)
965 {
966         switch (cmd) {
967         case STA_NOTIFY_SLEEP:
968                 break;
969         case STA_NOTIFY_AWAKE:
970                 break;
971         default:
972                 break;
973         }
974 }
975
976 static int rtl_op_ampdu_action(struct ieee80211_hw *hw,
977                                struct ieee80211_vif *vif,
978                                enum ieee80211_ampdu_mlme_action action,
979                                struct ieee80211_sta *sta, u16 tid, u16 *ssn,
980                                u8 buf_size)
981 {
982         struct rtl_priv *rtlpriv = rtl_priv(hw);
983
984         switch (action) {
985         case IEEE80211_AMPDU_TX_START:
986                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
987                          "IEEE80211_AMPDU_TX_START: TID:%d\n", tid);
988                 return rtl_tx_agg_start(hw, sta, tid, ssn);
989                 break;
990         case IEEE80211_AMPDU_TX_STOP_CONT:
991         case IEEE80211_AMPDU_TX_STOP_FLUSH:
992         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
993                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
994                          "IEEE80211_AMPDU_TX_STOP: TID:%d\n", tid);
995                 return rtl_tx_agg_stop(hw, sta, tid);
996         case IEEE80211_AMPDU_TX_OPERATIONAL:
997                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
998                          "IEEE80211_AMPDU_TX_OPERATIONAL:TID:%d\n", tid);
999                 rtl_tx_agg_oper(hw, sta, tid);
1000                 break;
1001         case IEEE80211_AMPDU_RX_START:
1002                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1003                          "IEEE80211_AMPDU_RX_START:TID:%d\n", tid);
1004                 return rtl_rx_agg_start(hw, sta, tid);
1005         case IEEE80211_AMPDU_RX_STOP:
1006                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1007                          "IEEE80211_AMPDU_RX_STOP:TID:%d\n", tid);
1008                 return rtl_rx_agg_stop(hw, sta, tid);
1009         default:
1010                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1011                          "IEEE80211_AMPDU_ERR!!!!:\n");
1012                 return -EOPNOTSUPP;
1013         }
1014         return 0;
1015 }
1016
1017 static void rtl_op_sw_scan_start(struct ieee80211_hw *hw)
1018 {
1019         struct rtl_priv *rtlpriv = rtl_priv(hw);
1020         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1021
1022         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
1023         mac->act_scanning = true;
1024         if (rtlpriv->link_info.higher_busytraffic) {
1025                 mac->skip_scan = true;
1026                 return;
1027         }
1028
1029         if (rtlpriv->dm.supp_phymode_switch) {
1030                 if (rtlpriv->cfg->ops->chk_switch_dmdp)
1031                         rtlpriv->cfg->ops->chk_switch_dmdp(hw);
1032         }
1033         if (mac->link_state == MAC80211_LINKED) {
1034                 rtlpriv->enter_ps = false;
1035                 schedule_work(&rtlpriv->works.lps_change_work);
1036                 mac->link_state = MAC80211_LINKED_SCANNING;
1037         } else {
1038                 rtl_ips_nic_on(hw);
1039         }
1040
1041         /* Dual mac */
1042         rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
1043
1044         rtlpriv->cfg->ops->led_control(hw, LED_CTL_SITE_SURVEY);
1045         rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_BACKUP);
1046 }
1047
1048 static void rtl_op_sw_scan_complete(struct ieee80211_hw *hw)
1049 {
1050         struct rtl_priv *rtlpriv = rtl_priv(hw);
1051         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1052
1053         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
1054         mac->act_scanning = false;
1055         mac->skip_scan = false;
1056         if (rtlpriv->link_info.higher_busytraffic)
1057                 return;
1058
1059         /*p2p will use 1/6/11 to scan */
1060         if (mac->n_channels == 3)
1061                 mac->p2p_in_use = true;
1062         else
1063                 mac->p2p_in_use = false;
1064         mac->n_channels = 0;
1065         /* Dual mac */
1066         rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
1067
1068         if (mac->link_state == MAC80211_LINKED_SCANNING) {
1069                 mac->link_state = MAC80211_LINKED;
1070                 if (mac->opmode == NL80211_IFTYPE_STATION) {
1071                         /* fix fwlps issue */
1072                         rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
1073                 }
1074         }
1075
1076         rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_RESTORE);
1077 }
1078
1079 static int rtl_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1080                           struct ieee80211_vif *vif, struct ieee80211_sta *sta,
1081                           struct ieee80211_key_conf *key)
1082 {
1083         struct rtl_priv *rtlpriv = rtl_priv(hw);
1084         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1085         u8 key_type = NO_ENCRYPTION;
1086         u8 key_idx;
1087         bool group_key = false;
1088         bool wep_only = false;
1089         int err = 0;
1090         u8 mac_addr[ETH_ALEN];
1091         u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1092
1093         if (rtlpriv->cfg->mod_params->sw_crypto || rtlpriv->sec.use_sw_sec) {
1094                 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
1095                          "not open hw encryption\n");
1096                 return -ENOSPC; /*User disabled HW-crypto */
1097         }
1098         /* To support IBSS, use sw-crypto for GTK */
1099         if (((vif->type == NL80211_IFTYPE_ADHOC) ||
1100              (vif->type == NL80211_IFTYPE_MESH_POINT)) &&
1101               !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1102                 return -ENOSPC;
1103         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1104                  "%s hardware based encryption for keyidx: %d, mac: %pM\n",
1105                  cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
1106                  sta ? sta->addr : bcast_addr);
1107         rtlpriv->sec.being_setkey = true;
1108         rtl_ips_nic_on(hw);
1109         mutex_lock(&rtlpriv->locks.conf_mutex);
1110         /* <1> get encryption alg */
1111
1112         switch (key->cipher) {
1113         case WLAN_CIPHER_SUITE_WEP40:
1114                 key_type = WEP40_ENCRYPTION;
1115                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP40\n");
1116                 break;
1117         case WLAN_CIPHER_SUITE_WEP104:
1118                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP104\n");
1119                 key_type = WEP104_ENCRYPTION;
1120                 break;
1121         case WLAN_CIPHER_SUITE_TKIP:
1122                 key_type = TKIP_ENCRYPTION;
1123                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:TKIP\n");
1124                 break;
1125         case WLAN_CIPHER_SUITE_CCMP:
1126                 key_type = AESCCMP_ENCRYPTION;
1127                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CCMP\n");
1128                 break;
1129         case WLAN_CIPHER_SUITE_AES_CMAC:
1130                 /*HW doesn't support CMAC encryption, use software CMAC */
1131                 key_type = AESCMAC_ENCRYPTION;
1132                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CMAC\n");
1133                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1134                          "HW don't support CMAC encryption, use software CMAC\n");
1135                 err = -EOPNOTSUPP;
1136                 goto out_unlock;
1137         default:
1138                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "alg_err:%x!!!!\n",
1139                          key->cipher);
1140                 goto out_unlock;
1141         }
1142         if (key_type == WEP40_ENCRYPTION ||
1143                         key_type == WEP104_ENCRYPTION ||
1144                         mac->opmode == NL80211_IFTYPE_ADHOC)
1145                 rtlpriv->sec.use_defaultkey = true;
1146
1147         /* <2> get key_idx */
1148         key_idx = (u8) (key->keyidx);
1149         if (key_idx > 3)
1150                 goto out_unlock;
1151         /* <3> if pairwise key enable_hw_sec */
1152         group_key = !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE);
1153
1154         /* wep always be group key, but there are two conditions:
1155          * 1) wep only: is just for wep enc, in this condition
1156          * rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION
1157          * will be true & enable_hw_sec will be set when wep
1158          * key setting.
1159          * 2) wep(group) + AES(pairwise): some AP like cisco
1160          * may use it, in this condition enable_hw_sec will not
1161          * be set when wep key setting */
1162         /* we must reset sec_info after lingked before set key,
1163          * or some flag will be wrong*/
1164         if (vif->type == NL80211_IFTYPE_AP ||
1165             vif->type == NL80211_IFTYPE_MESH_POINT) {
1166                 if (!group_key || key_type == WEP40_ENCRYPTION ||
1167                         key_type == WEP104_ENCRYPTION) {
1168                         if (group_key)
1169                                 wep_only = true;
1170                         rtlpriv->cfg->ops->enable_hw_sec(hw);
1171                 }
1172         } else {
1173                 if ((!group_key) || (mac->opmode == NL80211_IFTYPE_ADHOC) ||
1174                      rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION) {
1175                         if (rtlpriv->sec.pairwise_enc_algorithm ==
1176                             NO_ENCRYPTION &&
1177                             (key_type == WEP40_ENCRYPTION ||
1178                             key_type == WEP104_ENCRYPTION))
1179                                 wep_only = true;
1180                         rtlpriv->sec.pairwise_enc_algorithm = key_type;
1181                         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1182                                  "set enable_hw_sec, key_type:%x(OPEN:0 WEP40:1 TKIP:2 AES:4 WEP104:5)\n",
1183                                  key_type);
1184                         rtlpriv->cfg->ops->enable_hw_sec(hw);
1185                 }
1186         }
1187         /* <4> set key based on cmd */
1188         switch (cmd) {
1189         case SET_KEY:
1190                 if (wep_only) {
1191                         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1192                                  "set WEP(group/pairwise) key\n");
1193                         /* Pairwise key with an assigned MAC address. */
1194                         rtlpriv->sec.pairwise_enc_algorithm = key_type;
1195                         rtlpriv->sec.group_enc_algorithm = key_type;
1196                         /*set local buf about wep key. */
1197                         memcpy(rtlpriv->sec.key_buf[key_idx],
1198                                key->key, key->keylen);
1199                         rtlpriv->sec.key_len[key_idx] = key->keylen;
1200                         eth_zero_addr(mac_addr);
1201                 } else if (group_key) { /* group key */
1202                         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1203                                  "set group key\n");
1204                         /* group key */
1205                         rtlpriv->sec.group_enc_algorithm = key_type;
1206                         /*set local buf about group key. */
1207                         memcpy(rtlpriv->sec.key_buf[key_idx],
1208                                key->key, key->keylen);
1209                         rtlpriv->sec.key_len[key_idx] = key->keylen;
1210                         memcpy(mac_addr, bcast_addr, ETH_ALEN);
1211                 } else {        /* pairwise key */
1212                         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1213                                  "set pairwise key\n");
1214                         if (!sta) {
1215                                 RT_ASSERT(false,
1216                                           "pairwise key without mac_addr\n");
1217
1218                                 err = -EOPNOTSUPP;
1219                                 goto out_unlock;
1220                         }
1221                         /* Pairwise key with an assigned MAC address. */
1222                         rtlpriv->sec.pairwise_enc_algorithm = key_type;
1223                         /*set local buf about pairwise key. */
1224                         memcpy(rtlpriv->sec.key_buf[PAIRWISE_KEYIDX],
1225                                key->key, key->keylen);
1226                         rtlpriv->sec.key_len[PAIRWISE_KEYIDX] = key->keylen;
1227                         rtlpriv->sec.pairwise_key =
1228                             rtlpriv->sec.key_buf[PAIRWISE_KEYIDX];
1229                         memcpy(mac_addr, sta->addr, ETH_ALEN);
1230                 }
1231                 rtlpriv->cfg->ops->set_key(hw, key_idx, mac_addr,
1232                                            group_key, key_type, wep_only,
1233                                            false);
1234                 /* <5> tell mac80211 do something: */
1235                 /*must use sw generate IV, or can not work !!!!. */
1236                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1237                 key->hw_key_idx = key_idx;
1238                 if (key_type == TKIP_ENCRYPTION)
1239                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1240                 /*use software CCMP encryption for management frames (MFP) */
1241                 if (key_type == AESCCMP_ENCRYPTION)
1242                         key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
1243                 break;
1244         case DISABLE_KEY:
1245                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1246                          "disable key delete one entry\n");
1247                 /*set local buf about wep key. */
1248                 if (vif->type == NL80211_IFTYPE_AP ||
1249                     vif->type == NL80211_IFTYPE_MESH_POINT) {
1250                         if (sta)
1251                                 rtl_cam_del_entry(hw, sta->addr);
1252                 }
1253                 memset(rtlpriv->sec.key_buf[key_idx], 0, key->keylen);
1254                 rtlpriv->sec.key_len[key_idx] = 0;
1255                 eth_zero_addr(mac_addr);
1256                 /*
1257                  *mac80211 will delete entrys one by one,
1258                  *so don't use rtl_cam_reset_all_entry
1259                  *or clear all entry here.
1260                  */
1261                 rtl_cam_delete_one_entry(hw, mac_addr, key_idx);
1262
1263                 rtl_cam_reset_sec_info(hw);
1264
1265                 break;
1266         default:
1267                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1268                          "cmd_err:%x!!!!\n", cmd);
1269         }
1270 out_unlock:
1271         mutex_unlock(&rtlpriv->locks.conf_mutex);
1272         rtlpriv->sec.being_setkey = false;
1273         return err;
1274 }
1275
1276 static void rtl_op_rfkill_poll(struct ieee80211_hw *hw)
1277 {
1278         struct rtl_priv *rtlpriv = rtl_priv(hw);
1279
1280         bool radio_state;
1281         bool blocked;
1282         u8 valid = 0;
1283
1284         if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1285                 return;
1286
1287         mutex_lock(&rtlpriv->locks.conf_mutex);
1288
1289         /*if Radio On return true here */
1290         radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
1291
1292         if (valid) {
1293                 if (unlikely(radio_state != rtlpriv->rfkill.rfkill_state)) {
1294                         rtlpriv->rfkill.rfkill_state = radio_state;
1295
1296                         RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
1297                                  "wireless radio switch turned %s\n",
1298                                  radio_state ? "on" : "off");
1299
1300                         blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
1301                         wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
1302                 }
1303         }
1304
1305         mutex_unlock(&rtlpriv->locks.conf_mutex);
1306 }
1307
1308 /* this function is called by mac80211 to flush tx buffer
1309  * before switch channel or power save, or tx buffer packet
1310  * maybe send after offchannel or rf sleep, this may cause
1311  * dis-association by AP */
1312 static void rtl_op_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
1313 {
1314         struct rtl_priv *rtlpriv = rtl_priv(hw);
1315
1316         if (rtlpriv->intf_ops->flush)
1317                 rtlpriv->intf_ops->flush(hw, drop);
1318 }
1319
1320 const struct ieee80211_ops rtl_ops = {
1321         .start = rtl_op_start,
1322         .stop = rtl_op_stop,
1323         .tx = rtl_op_tx,
1324         .add_interface = rtl_op_add_interface,
1325         .remove_interface = rtl_op_remove_interface,
1326         .change_interface = rtl_op_change_interface,
1327         .config = rtl_op_config,
1328         .configure_filter = rtl_op_configure_filter,
1329         .sta_add = rtl_op_sta_add,
1330         .sta_remove = rtl_op_sta_remove,
1331         .set_key = rtl_op_set_key,
1332         .conf_tx = rtl_op_conf_tx,
1333         .bss_info_changed = rtl_op_bss_info_changed,
1334         .get_tsf = rtl_op_get_tsf,
1335         .set_tsf = rtl_op_set_tsf,
1336         .reset_tsf = rtl_op_reset_tsf,
1337         .sta_notify = rtl_op_sta_notify,
1338         .ampdu_action = rtl_op_ampdu_action,
1339         .sw_scan_start = rtl_op_sw_scan_start,
1340         .sw_scan_complete = rtl_op_sw_scan_complete,
1341         .rfkill_poll = rtl_op_rfkill_poll,
1342         .flush = rtl_op_flush,
1343 };