Merge branch 'next' of git://git.infradead.org/users/pcmoore/selinux into next
[cascardo/linux.git] / drivers / staging / rtl8192ee / base.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2009-2010  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  * The full GNU General Public License is included in this distribution in the
15  * file called LICENSE.
16  *
17  * Contact Information:
18  * wlanfae <wlanfae@realtek.com>
19  * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20  * Hsinchu 300, Taiwan.
21  *
22  * Larry Finger <Larry.Finger@lwfinger.net>
23  *
24  *****************************************************************************/
25
26 #include <linux/ip.h>
27 #include <linux/module.h>
28 #include "wifi.h"
29 #include "rc.h"
30 #include "base.h"
31 #include "efuse.h"
32 #include "cam.h"
33 #include "ps.h"
34 #include "regd.h"
35 #include "pci.h"
36
37 /*
38  *NOTICE!!!: This file will be very big, we hsould
39  *keep it clear under follwing roles:
40  *
41  *This file include follwing part, so, if you add new
42  *functions into this file, please check which part it
43  *should includes. or check if you should add new part
44  *for this file:
45  *
46  *1) mac80211 init functions
47  *2) tx information functions
48  *3) functions called by core.c
49  *4) wq & timer callback functions
50  *5) frame process functions
51  *6) IOT functions
52  *7) sysfs functions
53  *8) vif functions
54  *9) ...
55  */
56
57 /*********************************************************
58  *
59  * mac80211 init functions
60  *
61  *********************************************************/
62 static struct ieee80211_channel rtl_channeltable_2g[] = {
63         {.center_freq = 2412, .hw_value = 1,},
64         {.center_freq = 2417, .hw_value = 2,},
65         {.center_freq = 2422, .hw_value = 3,},
66         {.center_freq = 2427, .hw_value = 4,},
67         {.center_freq = 2432, .hw_value = 5,},
68         {.center_freq = 2437, .hw_value = 6,},
69         {.center_freq = 2442, .hw_value = 7,},
70         {.center_freq = 2447, .hw_value = 8,},
71         {.center_freq = 2452, .hw_value = 9,},
72         {.center_freq = 2457, .hw_value = 10,},
73         {.center_freq = 2462, .hw_value = 11,},
74         {.center_freq = 2467, .hw_value = 12,},
75         {.center_freq = 2472, .hw_value = 13,},
76         {.center_freq = 2484, .hw_value = 14,},
77 };
78
79 static struct ieee80211_channel rtl_channeltable_5g[] = {
80         {.center_freq = 5180, .hw_value = 36,},
81         {.center_freq = 5200, .hw_value = 40,},
82         {.center_freq = 5220, .hw_value = 44,},
83         {.center_freq = 5240, .hw_value = 48,},
84         {.center_freq = 5260, .hw_value = 52,},
85         {.center_freq = 5280, .hw_value = 56,},
86         {.center_freq = 5300, .hw_value = 60,},
87         {.center_freq = 5320, .hw_value = 64,},
88         {.center_freq = 5500, .hw_value = 100,},
89         {.center_freq = 5520, .hw_value = 104,},
90         {.center_freq = 5540, .hw_value = 108,},
91         {.center_freq = 5560, .hw_value = 112,},
92         {.center_freq = 5580, .hw_value = 116,},
93         {.center_freq = 5600, .hw_value = 120,},
94         {.center_freq = 5620, .hw_value = 124,},
95         {.center_freq = 5640, .hw_value = 128,},
96         {.center_freq = 5660, .hw_value = 132,},
97         {.center_freq = 5680, .hw_value = 136,},
98         {.center_freq = 5700, .hw_value = 140,},
99         {.center_freq = 5745, .hw_value = 149,},
100         {.center_freq = 5765, .hw_value = 153,},
101         {.center_freq = 5785, .hw_value = 157,},
102         {.center_freq = 5805, .hw_value = 161,},
103         {.center_freq = 5825, .hw_value = 165,},
104 };
105
106 static struct ieee80211_rate rtl_ratetable_2g[] = {
107         {.bitrate = 10, .hw_value = 0x00,},
108         {.bitrate = 20, .hw_value = 0x01,},
109         {.bitrate = 55, .hw_value = 0x02,},
110         {.bitrate = 110, .hw_value = 0x03,},
111         {.bitrate = 60, .hw_value = 0x04,},
112         {.bitrate = 90, .hw_value = 0x05,},
113         {.bitrate = 120, .hw_value = 0x06,},
114         {.bitrate = 180, .hw_value = 0x07,},
115         {.bitrate = 240, .hw_value = 0x08,},
116         {.bitrate = 360, .hw_value = 0x09,},
117         {.bitrate = 480, .hw_value = 0x0a,},
118         {.bitrate = 540, .hw_value = 0x0b,},
119 };
120
121 static struct ieee80211_rate rtl_ratetable_5g[] = {
122         {.bitrate = 60, .hw_value = 0x04,},
123         {.bitrate = 90, .hw_value = 0x05,},
124         {.bitrate = 120, .hw_value = 0x06,},
125         {.bitrate = 180, .hw_value = 0x07,},
126         {.bitrate = 240, .hw_value = 0x08,},
127         {.bitrate = 360, .hw_value = 0x09,},
128         {.bitrate = 480, .hw_value = 0x0a,},
129         {.bitrate = 540, .hw_value = 0x0b,},
130 };
131
132 static const struct ieee80211_supported_band rtl_band_2ghz = {
133         .band = IEEE80211_BAND_2GHZ,
134
135         .channels = rtl_channeltable_2g,
136         .n_channels = ARRAY_SIZE(rtl_channeltable_2g),
137
138         .bitrates = rtl_ratetable_2g,
139         .n_bitrates = ARRAY_SIZE(rtl_ratetable_2g),
140
141         .ht_cap = {0},
142 };
143
144 static struct ieee80211_supported_band rtl_band_5ghz = {
145         .band = IEEE80211_BAND_5GHZ,
146
147         .channels = rtl_channeltable_5g,
148         .n_channels = ARRAY_SIZE(rtl_channeltable_5g),
149
150         .bitrates = rtl_ratetable_5g,
151         .n_bitrates = ARRAY_SIZE(rtl_ratetable_5g),
152
153         .ht_cap = {0},
154 };
155
156 static const u8 tid_to_ac[] = {
157         2, /* IEEE80211_AC_BE */
158         3, /* IEEE80211_AC_BK */
159         3, /* IEEE80211_AC_BK */
160         2, /* IEEE80211_AC_BE */
161         1, /* IEEE80211_AC_VI */
162         1, /* IEEE80211_AC_VI */
163         0, /* IEEE80211_AC_VO */
164         0, /* IEEE80211_AC_VO */
165 };
166
167 u8 rtl92e_tid_to_ac(struct ieee80211_hw *hw, u8 tid)
168 {
169         return tid_to_ac[tid];
170 }
171
172 static void _rtl_init_hw_ht_capab(struct ieee80211_hw *hw,
173                                   struct ieee80211_sta_ht_cap *ht_cap)
174 {
175         struct rtl_priv *rtlpriv = rtl_priv(hw);
176         struct rtl_phy *rtlphy = &(rtlpriv->phy);
177
178         ht_cap->ht_supported = true;
179         ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
180             IEEE80211_HT_CAP_SGI_40 |
181             IEEE80211_HT_CAP_SGI_20 |
182             IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU;
183
184         if (rtlpriv->rtlhal.disable_amsdu_8k)
185                 ht_cap->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
186
187         /*
188          *Maximum length of AMPDU that the STA can receive.
189          *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
190          */
191         ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
192
193         /*Minimum MPDU start spacing , */
194         ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
195
196         ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
197
198         /*
199          *hw->wiphy->bands[IEEE80211_BAND_2GHZ]
200          *base on ant_num
201          *rx_mask: RX mask
202          *if rx_ant =1 rx_mask[0]=0xff;==>MCS0-MCS7
203          *if rx_ant =2 rx_mask[1]=0xff;==>MCS8-MCS15
204          *if rx_ant >=3 rx_mask[2]=0xff;
205          *if BW_40 rx_mask[4]=0x01;
206          *highest supported RX rate
207          */
208         if (rtlpriv->dm.supp_phymode_switch) {
209                 RT_TRACE(COMP_INIT, DBG_EMERG, ("Support phy mode switch\n"));
210
211                 ht_cap->mcs.rx_mask[0] = 0xFF;
212                 ht_cap->mcs.rx_mask[1] = 0xFF;
213                 ht_cap->mcs.rx_mask[4] = 0x01;
214
215                 ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
216         } else {
217                 if (get_rf_type(rtlphy) == RF_1T2R ||
218                     get_rf_type(rtlphy) == RF_2T2R) {
219                         RT_TRACE(COMP_INIT, DBG_DMESG, ("1T2R or 2T2R\n"));
220
221                         ht_cap->mcs.rx_mask[0] = 0xFF;
222                         ht_cap->mcs.rx_mask[1] = 0xFF;
223                         ht_cap->mcs.rx_mask[4] = 0x01;
224
225                         ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
226                 } else if (get_rf_type(rtlphy) == RF_1T1R) {
227                         RT_TRACE(COMP_INIT, DBG_DMESG, ("1T1R\n"));
228
229                         ht_cap->mcs.rx_mask[0] = 0xFF;
230                         ht_cap->mcs.rx_mask[1] = 0x00;
231                         ht_cap->mcs.rx_mask[4] = 0x01;
232
233                         ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
234                 }
235         }
236 }
237
238 static void _rtl_init_hw_vht_capab(struct ieee80211_hw *hw,
239                                    struct ieee80211_sta_vht_cap *vht_cap)
240 {
241         struct rtl_priv *rtlpriv = rtl_priv(hw);
242         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
243
244         if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
245                 u16 mcs_map;
246                 vht_cap->vht_supported = true;
247                 vht_cap->cap =
248                         IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895 |
249                         IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991 |
250                         IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
251                         IEEE80211_VHT_CAP_SHORT_GI_80 |
252                         IEEE80211_VHT_CAP_TXSTBC |
253                         IEEE80211_VHT_CAP_RXSTBC_1 |
254                         IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
255                         IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
256                         IEEE80211_VHT_CAP_HTC_VHT |
257                         IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
258                         IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
259                         IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
260                         0;
261
262                 mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
263                         IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
264                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 |
265                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
266                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
267                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
268                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
269                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 14;
270
271                 vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
272                 vht_cap->vht_mcs.rx_highest =
273                         cpu_to_le16(MAX_BIT_RATE_SHORT_GI_2NSS_80MHZ_MCS9);
274                 vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
275                 vht_cap->vht_mcs.tx_highest =
276                         cpu_to_le16(MAX_BIT_RATE_SHORT_GI_2NSS_80MHZ_MCS9);
277         } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
278                 u16 mcs_map;
279
280                 vht_cap->vht_supported = true;
281                 vht_cap->cap =
282                         IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895 |
283                         IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991 |
284                         IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
285                         IEEE80211_VHT_CAP_SHORT_GI_80 |
286                         IEEE80211_VHT_CAP_TXSTBC |
287                         IEEE80211_VHT_CAP_RXSTBC_1 |
288                         IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
289                         IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
290                         IEEE80211_VHT_CAP_HTC_VHT |
291                         IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
292                         IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
293                         IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
294                         0;
295
296                 mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
297                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 2 |
298                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 |
299                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
300                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
301                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
302                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
303                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 14;
304
305                 vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
306                 vht_cap->vht_mcs.rx_highest =
307                         cpu_to_le16(MAX_BIT_RATE_SHORT_GI_1NSS_80MHZ_MCS9);
308                 vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
309                 vht_cap->vht_mcs.tx_highest =
310                         cpu_to_le16(MAX_BIT_RATE_SHORT_GI_1NSS_80MHZ_MCS9);
311         }
312 }
313
314 static void _rtl_init_mac80211(struct ieee80211_hw *hw)
315 {
316         struct rtl_priv *rtlpriv = rtl_priv(hw);
317         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
318         struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
319         struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
320         struct ieee80211_supported_band *sband;
321
322
323         if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY &&
324             rtlhal->bandset == BAND_ON_BOTH) {
325                 /* 1: 2.4 G bands */
326                 /* <1> use  mac->bands as mem for hw->wiphy->bands */
327                 sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
328
329                 /* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
330                  * to default value(1T1R) */
331                 memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]), &rtl_band_2ghz,
332                        sizeof(struct ieee80211_supported_band));
333
334                 /* <3> init ht cap base on ant_num */
335                 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
336
337                 /* <4> set mac->sband to wiphy->sband */
338                 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
339
340                 /* 2: 5 G bands */
341                 /* <1> use  mac->bands as mem for hw->wiphy->bands */
342                 sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
343
344                 /* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
345                  * to default value(1T1R) */
346                 memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]), &rtl_band_5ghz,
347                        sizeof(struct ieee80211_supported_band));
348
349                 /* <3> init ht cap base on ant_num */
350                 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
351
352                 _rtl_init_hw_vht_capab(hw, &sband->vht_cap);
353
354                 /* <4> set mac->sband to wiphy->sband */
355                 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
356         } else {
357                 if (rtlhal->current_bandtype == BAND_ON_2_4G) {
358                         /* <1> use  mac->bands as mem for hw->wiphy->bands */
359                         sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
360
361                         /* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
362                          * to default value(1T1R) */
363                         memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]),
364                                &rtl_band_2ghz,
365                                sizeof(struct ieee80211_supported_band));
366
367                         /* <3> init ht cap base on ant_num */
368                         _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
369
370                         /* <4> set mac->sband to wiphy->sband */
371                         hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
372                 } else if (rtlhal->current_bandtype == BAND_ON_5G) {
373                         /* <1> use  mac->bands as mem for hw->wiphy->bands */
374                         sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
375
376                         /* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
377                          * to default value(1T1R) */
378                         memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]),
379                                &rtl_band_5ghz,
380                                sizeof(struct ieee80211_supported_band));
381
382                         /* <3> init ht cap base on ant_num */
383                         _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
384
385                         _rtl_init_hw_vht_capab(hw, &sband->vht_cap);
386
387                         /* <4> set mac->sband to wiphy->sband */
388                         hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
389                 } else {
390                         RT_TRACE(COMP_INIT, DBG_EMERG,
391                                  ("Err BAND %d\n", rtlhal->current_bandtype));
392                 }
393         }
394         /* <5> set hw caps */
395         hw->flags = IEEE80211_HW_SIGNAL_DBM |
396             IEEE80211_HW_RX_INCLUDES_FCS |
397             IEEE80211_HW_AMPDU_AGGREGATION |
398             IEEE80211_HW_REPORTS_TX_ACK_STATUS |
399             IEEE80211_HW_CONNECTION_MONITOR |
400             /* IEEE80211_HW_SUPPORTS_CQM_RSSI | */
401             IEEE80211_HW_MFP_CAPABLE | 0;
402
403         /* swlps or hwlps has been set in diff chip in init_sw_vars */
404         if (rtlpriv->psc.b_swctrl_lps)
405                 hw->flags |= IEEE80211_HW_SUPPORTS_PS |
406                         IEEE80211_HW_PS_NULLFUNC_STACK |
407                         /* IEEE80211_HW_SUPPORTS_DYNAMIC_PS | */
408                         0;
409 /*<delete in kernel start>*/
410         hw->wiphy->interface_modes =
411                 BIT(NL80211_IFTYPE_AP) |
412                 BIT(NL80211_IFTYPE_STATION) |
413                 BIT(NL80211_IFTYPE_ADHOC) |
414                 BIT(NL80211_IFTYPE_MESH_POINT) |
415                 BIT(NL80211_IFTYPE_P2P_CLIENT) |
416                 BIT(NL80211_IFTYPE_P2P_GO);
417         hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
418
419         hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
420
421         hw->wiphy->rts_threshold = 2347;
422
423         hw->queues = AC_MAX;
424         hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;
425
426         /* TODO: Correct this value for our hw */
427         /* TODO: define these hard code value */
428         /* hw->channel_change_time = 100; kernel does not use it*/
429         hw->max_listen_interval = 10;
430         hw->max_rate_tries = 4;
431         /* hw->max_rates = 1; */
432         hw->sta_data_size = sizeof(struct rtl_sta_info);
433
434 /* wowlan is not supported by kernel if CONFIG_PM is not defined */
435 #ifdef CONFIG_PM
436         if (rtlpriv->psc.wo_wlan_mode) {
437                 if (rtlpriv->psc.wo_wlan_mode & WAKE_ON_MAGIC_PACKET)
438                         rtlpriv->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT;
439                 if (rtlpriv->psc.wo_wlan_mode & WAKE_ON_PATTERN_MATCH) {
440                         rtlpriv->wowlan.n_patterns =
441                                 MAX_SUPPORT_WOL_PATTERN_NUM;
442                         rtlpriv->wowlan.pattern_min_len = MIN_WOL_PATTERN_SIZE;
443                         rtlpriv->wowlan.pattern_max_len = MAX_WOL_PATTERN_SIZE;
444                 }
445                 hw->wiphy->wowlan = &(rtlpriv->wowlan);
446         }
447 #endif
448
449         /* <6> mac address */
450         if (is_valid_ether_addr(rtlefuse->dev_addr)) {
451                 SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
452         } else {
453                 u8 rtlmac[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
454                 get_random_bytes((rtlmac + (ETH_ALEN - 1)), 1);
455                 SET_IEEE80211_PERM_ADDR(hw, rtlmac);
456         }
457 }
458
459 static void _rtl_init_deferred_work(struct ieee80211_hw *hw)
460 {
461         struct rtl_priv *rtlpriv = rtl_priv(hw);
462
463         /* <1> timer */
464         init_timer(&rtlpriv->works.watchdog_timer);
465         setup_timer(&rtlpriv->works.watchdog_timer,
466                     rtl92e_watch_dog_timer_callback, (unsigned long)hw);
467         init_timer(&rtlpriv->works.dualmac_easyconcurrent_retrytimer);
468         setup_timer(&rtlpriv->works.dualmac_easyconcurrent_retrytimer,
469                     rtl92e_easy_concurrent_retrytimer_callback, (unsigned long)hw);
470         /* <2> work queue */
471         rtlpriv->works.hw = hw;
472         rtlpriv->works.rtl_wq = alloc_workqueue(rtlpriv->cfg->name, 0, 0);
473         INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
474                           (void *)rtl92e_watchdog_wq_callback);
475         INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
476                           (void *)rtl92e_ips_nic_off_wq_callback);
477         INIT_DELAYED_WORK(&rtlpriv->works.ps_work,
478                           (void *)rtl92e_swlps_wq_callback);
479         INIT_DELAYED_WORK(&rtlpriv->works.ps_rfon_wq,
480                           (void *)rtl92e_swlps_rfon_wq_callback);
481         INIT_DELAYED_WORK(&rtlpriv->works.fwevt_wq,
482                           (void *)rtl92e_fwevt_wq_callback);
483 }
484
485 void rtl92e_deinit_deferred_work(struct ieee80211_hw *hw)
486 {
487         struct rtl_priv *rtlpriv = rtl_priv(hw);
488
489         del_timer_sync(&rtlpriv->works.watchdog_timer);
490
491         cancel_delayed_work(&rtlpriv->works.watchdog_wq);
492         cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
493         cancel_delayed_work(&rtlpriv->works.ps_work);
494         cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
495         cancel_delayed_work(&rtlpriv->works.fwevt_wq);
496 }
497
498 void rtl92e_init_rfkill(struct ieee80211_hw *hw)
499 {
500         struct rtl_priv *rtlpriv = rtl_priv(hw);
501
502         bool radio_state;
503         bool blocked;
504         u8 valid = 0;
505
506         /*set init state to on */
507         rtlpriv->rfkill.rfkill_state = 1;
508         wiphy_rfkill_set_hw_state(hw->wiphy, 0);
509
510         radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
511
512         if (valid) {
513                 pr_info("rtlwifi: wireless switch is %s\n",
514                         rtlpriv->rfkill.rfkill_state ? "on" : "off");
515
516                 rtlpriv->rfkill.rfkill_state = radio_state;
517
518                 blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
519                 wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
520         }
521
522         wiphy_rfkill_start_polling(hw->wiphy);
523 }
524
525 void rtl92e_deinit_rfkill(struct ieee80211_hw *hw)
526 {
527         wiphy_rfkill_stop_polling(hw->wiphy);
528 }
529
530 #ifdef VIF_TODO
531 static void rtl_init_vif(struct ieee80211_hw *hw)
532 {
533         struct rtl_priv *rtlpriv = rtl_priv(hw);
534
535         INIT_LIST_HEAD(&rtlpriv->vif_priv.vif_list);
536
537         rtlpriv->vif_priv.vifs = 0;
538 }
539 #endif
540
541 int rtl92e_init_core(struct ieee80211_hw *hw)
542 {
543         struct rtl_priv *rtlpriv = rtl_priv(hw);
544         struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
545
546         /* <1> init mac80211 */
547         _rtl_init_mac80211(hw);
548         rtlmac->hw = hw;
549         rtlmac->link_state = MAC80211_NOLINK;
550
551         /* <2> rate control register */
552         hw->rate_control_algorithm = "rtl_rc";
553
554         /*
555          * <3> init CRDA must come after init
556          * mac80211 hw  in _rtl_init_mac80211.
557          */
558         if (rtl92e_regd_init(hw, rtl92e_reg_notifier)) {
559                 RT_TRACE(COMP_ERR, DBG_EMERG, ("REGD init failed\n"));
560                 return 1;
561         }
562
563         /* <4> locks */
564         mutex_init(&rtlpriv->locks.conf_mutex);
565         spin_lock_init(&rtlpriv->locks.ips_lock);
566         spin_lock_init(&rtlpriv->locks.irq_th_lock);
567         spin_lock_init(&rtlpriv->locks.h2c_lock);
568         spin_lock_init(&rtlpriv->locks.rf_ps_lock);
569         spin_lock_init(&rtlpriv->locks.rf_lock);
570         spin_lock_init(&rtlpriv->locks.lps_lock);
571         spin_lock_init(&rtlpriv->locks.waitq_lock);
572         spin_lock_init(&rtlpriv->locks.entry_list_lock);
573         spin_lock_init(&rtlpriv->locks.cck_and_rw_pagea_lock);
574         spin_lock_init(&rtlpriv->locks.check_sendpkt_lock);
575         spin_lock_init(&rtlpriv->locks.fw_ps_lock);
576         spin_lock_init(&rtlpriv->locks.iqk_lock);
577         /* <5> init list */
578         INIT_LIST_HEAD(&rtlpriv->entry_list);
579
580         /* <6> init deferred work */
581         _rtl_init_deferred_work(hw);
582
583         /* <7> */
584 #ifdef VIF_TODO
585         rtl_init_vif(hw);
586 #endif
587
588         return 0;
589 }
590
591 void rtl92e_deinit_core(struct ieee80211_hw *hw)
592 {
593 }
594
595 void rtl92e_init_rx_config(struct ieee80211_hw *hw)
596 {
597         struct rtl_priv *rtlpriv = rtl_priv(hw);
598         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
599
600         rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *)(&mac->rx_conf));
601 }
602
603 /*********************************************************
604  *
605  * tx information functions
606  *
607  *********************************************************/
608 static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
609                                           struct rtl_tcb_desc *tcb_desc,
610                                           struct ieee80211_tx_info *info)
611 {
612         struct rtl_priv *rtlpriv = rtl_priv(hw);
613         u8 rate_flag = info->control.rates[0].flags;
614
615         tcb_desc->use_shortpreamble = false;
616
617         /* 1M can only use Long Preamble. 11B spec */
618         if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
619                 return;
620         else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
621                 tcb_desc->use_shortpreamble = true;
622
623         return;
624 }
625
626 static void _rtl_query_shortgi(struct ieee80211_hw *hw,
627                                struct ieee80211_sta *sta,
628                                struct rtl_tcb_desc *tcb_desc,
629                                struct ieee80211_tx_info *info)
630 {
631         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
632         u8 rate_flag = info->control.rates[0].flags;
633         u8 sgi_40 = 0, sgi_20 = 0, bw_40 = 0;
634         u8 sgi_80 = 0, bw_80 = 0;
635         tcb_desc->use_shortgi = false;
636
637         if (sta == NULL)
638                 return;
639
640         sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
641         sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
642         sgi_80 = sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80;
643
644         if (!(sta->ht_cap.ht_supported) && !(sta->vht_cap.vht_supported))
645                 return;
646
647         if (!sgi_40 && !sgi_20)
648                 return;
649
650         if (mac->opmode == NL80211_IFTYPE_STATION) {
651                 bw_40 = mac->bw_40;
652                 bw_80 = mac->bw_80;
653         } else if (mac->opmode == NL80211_IFTYPE_AP ||
654                    mac->opmode == NL80211_IFTYPE_ADHOC ||
655                    mac->opmode == NL80211_IFTYPE_MESH_POINT) {
656                 bw_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
657                 bw_80 = sta->vht_cap.vht_supported;
658         }
659
660         if (bw_80) {
661                 if (sgi_80)
662                         tcb_desc->use_shortgi = true;
663                 else
664                         tcb_desc->use_shortgi = false;
665         } else {
666                 if (bw_40 && sgi_40)
667                         tcb_desc->use_shortgi = true;
668                 else if (!bw_40 && sgi_20)
669                         tcb_desc->use_shortgi = true;
670         }
671
672         if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
673                 tcb_desc->use_shortgi = false;
674 }
675
676 static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
677                                        struct rtl_tcb_desc *tcb_desc,
678                                        struct ieee80211_tx_info *info)
679 {
680         struct rtl_priv *rtlpriv = rtl_priv(hw);
681         u8 rate_flag = info->control.rates[0].flags;
682
683         /* Common Settings */
684         tcb_desc->b_rts_stbc = false;
685         tcb_desc->b_cts_enable = false;
686         tcb_desc->rts_sc = 0;
687         tcb_desc->b_rts_bw = false;
688         tcb_desc->b_rts_use_shortpreamble = false;
689         tcb_desc->b_rts_use_shortgi = false;
690
691         if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
692                 /* Use CTS-to-SELF in protection mode. */
693                 tcb_desc->b_rts_enable = true;
694                 tcb_desc->b_cts_enable = true;
695                 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
696         } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
697                 /* Use RTS-CTS in protection mode. */
698                 tcb_desc->b_rts_enable = true;
699                 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
700         }
701 }
702
703 static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
704                                    struct ieee80211_sta *sta,
705                                    struct rtl_tcb_desc *tcb_desc)
706 {
707         struct rtl_priv *rtlpriv = rtl_priv(hw);
708         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
709         struct rtl_sta_info *sta_entry = NULL;
710         u8 ratr_index = 7;
711
712         if (sta) {
713                 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
714                 ratr_index = sta_entry->ratr_index;
715         }
716         if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
717                 if (mac->opmode == NL80211_IFTYPE_STATION) {
718                         tcb_desc->ratr_index = 0;
719                 } else if (mac->opmode == NL80211_IFTYPE_ADHOC ||
720                                 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
721                         if (tcb_desc->b_multicast || tcb_desc->b_broadcast) {
722                                 tcb_desc->hw_rate =
723                                     rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
724                                 tcb_desc->use_driver_rate = 1;
725                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
726                         } else {
727                                 tcb_desc->ratr_index = ratr_index;
728                         }
729                 } else if (mac->opmode == NL80211_IFTYPE_AP) {
730                         tcb_desc->ratr_index = ratr_index;
731                 }
732         }
733
734         if (rtlpriv->dm.b_useramask) {
735                 tcb_desc->ratr_index = ratr_index;
736                 /* TODO we will differentiate adhoc and station futrue  */
737                 if (mac->opmode == NL80211_IFTYPE_STATION ||
738                     mac->opmode == NL80211_IFTYPE_MESH_POINT) {
739                         tcb_desc->mac_id = 0;
740
741                         if (mac->mode == WIRELESS_MODE_AC_5G)
742                                 tcb_desc->ratr_index =
743                                         RATR_INX_WIRELESS_AC_5N;
744                         else if (mac->mode == WIRELESS_MODE_AC_24G)
745                                 tcb_desc->ratr_index =
746                                         RATR_INX_WIRELESS_AC_24N;
747                         else if (mac->mode == WIRELESS_MODE_N_24G)
748                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
749                         else if (mac->mode == WIRELESS_MODE_N_5G)
750                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_NG;
751                         else if (mac->mode & WIRELESS_MODE_G)
752                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
753                         else if (mac->mode & WIRELESS_MODE_B)
754                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
755                         else if (mac->mode & WIRELESS_MODE_A)
756                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_G;
757
758                 } else if (mac->opmode == NL80211_IFTYPE_AP ||
759                            mac->opmode == NL80211_IFTYPE_ADHOC) {
760                         if (sta) {
761                                 if (sta->aid > 0)
762                                         tcb_desc->mac_id = sta->aid + 1;
763                                 else
764                                         tcb_desc->mac_id = 1;
765                         } else {
766                                 tcb_desc->mac_id = 0;
767                         }
768                 }
769         }
770 }
771
772 static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
773                                       struct ieee80211_sta *sta,
774                                       struct rtl_tcb_desc *tcb_desc)
775 {
776         struct rtl_priv *rtlpriv = rtl_priv(hw);
777         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
778
779         tcb_desc->packet_bw = 0;
780         if (!sta)
781                 return;
782         if (mac->opmode == NL80211_IFTYPE_AP ||
783             mac->opmode == NL80211_IFTYPE_ADHOC ||
784             mac->opmode == NL80211_IFTYPE_MESH_POINT) {
785                 if (!(sta->ht_cap.ht_supported) ||
786                     !(sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
787                         return;
788         } else if (mac->opmode == NL80211_IFTYPE_STATION) {
789                 if (!mac->bw_40 || !(sta->ht_cap.ht_supported))
790                         return;
791         }
792         if (tcb_desc->b_multicast || tcb_desc->b_broadcast)
793                 return;
794
795         /*use legency rate, shall use 20MHz */
796         if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
797                 return;
798
799         tcb_desc->packet_bw = HT_CHANNEL_WIDTH_20_40;
800
801         if (rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8812AE ||
802             rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8821AE) {
803                 if (mac->opmode == NL80211_IFTYPE_AP ||
804                     mac->opmode == NL80211_IFTYPE_ADHOC ||
805                     mac->opmode == NL80211_IFTYPE_MESH_POINT) {
806                         if (!(sta->vht_cap.vht_supported)) {
807                                 return;
808                         } else if (mac->opmode == NL80211_IFTYPE_STATION) {
809                                 if (!mac->bw_80 ||
810                                     !(sta->vht_cap.vht_supported))
811                                         return;
812                         }
813                 }
814                 if (tcb_desc->hw_rate <=
815                         rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15])
816                         return;
817                 tcb_desc->packet_bw = HT_CHANNEL_WIDTH_80;
818         }
819 }
820
821 static u8 _rtl_get_vht_highest_n_rate(struct ieee80211_hw *hw,
822                                       struct ieee80211_sta *sta)
823 {
824         struct rtl_priv *rtlpriv = rtl_priv(hw);
825         struct rtl_phy *rtlphy = &(rtlpriv->phy);
826         u8 hw_rate;
827         u16 map = le16_to_cpu(sta->vht_cap.vht_mcs.tx_mcs_map);
828
829         if ((get_rf_type(rtlphy) == RF_2T2R) &&
830             (map & 0x000c) != 0x000c0) {
831                 if ((map & 0x000c) >> 2 == IEEE80211_VHT_MCS_SUPPORT_0_7)
832                         hw_rate =
833                         rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS7];
834                 else if ((map  & 0x000c) >> 2 == IEEE80211_VHT_MCS_SUPPORT_0_8)
835                         hw_rate = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS9];
836                 else
837                         hw_rate = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS9];
838         } else {
839                 if ((map  & 0x0003) == IEEE80211_VHT_MCS_SUPPORT_0_7)
840                         hw_rate = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS7];
841                 else if ((map  & 0x0003) == IEEE80211_VHT_MCS_SUPPORT_0_8)
842                         hw_rate = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS9];
843                 else
844                         hw_rate = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS9];
845         }
846
847         return hw_rate;
848 }
849
850 static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw,
851                                   struct ieee80211_sta *sta)
852 {
853         struct rtl_priv *rtlpriv = rtl_priv(hw);
854         struct rtl_phy *rtlphy = &(rtlpriv->phy);
855         u8 hw_rate;
856
857         if ((get_rf_type(rtlphy) == RF_2T2R) &&
858             (sta->ht_cap.mcs.rx_mask[1] != 0))
859                 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
860         else
861                 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
862
863         return hw_rate;
864 }
865
866 void stg_rtl_get_tcb_desc(struct ieee80211_hw *hw,
867                           struct ieee80211_tx_info *info,
868                           struct ieee80211_sta *sta,
869                           struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
870 {
871         struct rtl_priv *rtlpriv = rtl_priv(hw);
872         struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
873         struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
874         struct ieee80211_rate *txrate;
875         __le16 fc = rtl_get_fc(skb);
876
877         txrate = ieee80211_get_tx_rate(hw, info);
878         if (txrate != NULL)
879                 tcb_desc->hw_rate = txrate->hw_value;
880
881         if (ieee80211_is_data(fc)) {
882                 /*
883                  *we set data rate INX 0
884                  *in rtl_rc.c   if skb is special data or
885                  *mgt which need low data rate.
886                  */
887
888                 /*
889                  *So tcb_desc->hw_rate is just used for
890                  *special data and mgt frames
891                  */
892                 if (info->control.rates[0].idx == 0 ||
893                     ieee80211_is_nullfunc(fc)) {
894                         tcb_desc->use_driver_rate = true;
895                         tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
896
897                         tcb_desc->disable_ratefallback = 1;
898                 } else {
899                         /*
900                          *because hw will nerver use hw_rate
901                          *when tcb_desc->use_driver_rate = false
902                          *so we never set highest N rate here,
903                          *and N rate will all be controled by FW
904                          *when tcb_desc->use_driver_rate = false
905                          */
906                         if (sta && sta->vht_cap.vht_supported) {
907                                 tcb_desc->hw_rate =
908                                 _rtl_get_vht_highest_n_rate(hw, sta);
909                         } else if (sta && (sta->ht_cap.ht_supported)) {
910                                 tcb_desc->hw_rate =
911                                         _rtl_get_highest_n_rate(hw, sta);
912                         } else {
913                                 if (rtlmac->mode == WIRELESS_MODE_B) {
914                                         tcb_desc->hw_rate =
915                                             rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
916                                 } else {
917                                         tcb_desc->hw_rate =
918                                             rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
919                                 }
920                         }
921                 }
922
923                 if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
924                         tcb_desc->b_multicast = 1;
925                 else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
926                         tcb_desc->b_broadcast = 1;
927
928                 _rtl_txrate_selectmode(hw, sta, tcb_desc);
929                 _rtl_query_bandwidth_mode(hw, sta, tcb_desc);
930                 _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
931                 _rtl_query_shortgi(hw, sta, tcb_desc, info);
932                 _rtl_query_protection_mode(hw, tcb_desc, info);
933         } else {
934                 tcb_desc->use_driver_rate = true;
935                 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
936                 tcb_desc->disable_ratefallback = 1;
937                 tcb_desc->mac_id = 0;
938                 tcb_desc->packet_bw = 0;
939         }
940 }
941 EXPORT_SYMBOL(stg_rtl_get_tcb_desc);
942
943 bool rtl92e_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
944 {
945         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
946         struct rtl_priv *rtlpriv = rtl_priv(hw);
947         __le16 fc = rtl_get_fc(skb);
948
949         if (rtlpriv->dm.supp_phymode_switch &&
950             mac->link_state < MAC80211_LINKED &&
951             (ieee80211_is_auth(fc) || ieee80211_is_probe_req(fc))) {
952                 if (rtlpriv->cfg->ops->check_switch_to_dmdp)
953                         rtlpriv->cfg->ops->check_switch_to_dmdp(hw);
954         }
955         if (ieee80211_is_auth(fc)) {
956                 RT_TRACE(COMP_SEND, DBG_DMESG, ("MAC80211_LINKING\n"));
957                 rtl92e_ips_nic_on(hw);
958
959                 mac->link_state = MAC80211_LINKING;
960                 /* Dul mac */
961                 rtlpriv->phy.b_need_iqk = true;
962         }
963         return true;
964 }
965
966 struct sk_buff *rtl92e_make_del_ba(struct ieee80211_hw *hw, u8 *sa,
967                                    u8 *bssid, u16 tid);
968
969 bool rtl92e_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
970 {
971         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
972         struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
973         struct rtl_priv *rtlpriv = rtl_priv(hw);
974         __le16 fc = rtl_get_fc(skb);
975         u8 *act = (u8 *)(((u8 *)skb->data + MAC80211_3ADDR_LEN));
976         u8 category;
977
978         if (!ieee80211_is_action(fc))
979                 return true;
980
981         category = *act;
982         act++;
983         switch (category) {
984         case ACT_CAT_BA:
985                 switch (*act) {
986                 case ACT_ADDBAREQ:
987                         if (mac->act_scanning)
988                                 return false;
989
990                         RT_TRACE((COMP_SEND | COMP_RECV), DBG_DMESG,
991                                  ("%s ACT_ADDBAREQ From:%pM\n",
992                                 is_tx ? "Tx" : "Rx", hdr->addr2));
993                         RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG, "req\n",
994                                       skb->data, skb->len);
995                         if (!is_tx) {
996                                 struct ieee80211_sta *sta = NULL;
997                                 struct rtl_sta_info *sta_entry = NULL;
998                                 struct ieee80211_mgmt *mgmt = (void *)skb->data;
999                                 u16 capab = 0, tid = 0;
1000                                 struct rtl_tid_data *tid_data;
1001                                 struct sk_buff *skb_delba = NULL;
1002                                 struct ieee80211_rx_status rx_status = { 0 };
1003
1004                                 rcu_read_lock();
1005                                 sta = rtl_find_sta(hw, hdr->addr3);
1006                                 if (sta == NULL) {
1007                                         RT_TRACE((COMP_SEND | COMP_RECV),
1008                                                  DBG_TRACE, ("sta is NULL\n"));
1009                                         rcu_read_unlock();
1010                                         return true;
1011                                 }
1012
1013                                 sta_entry =
1014                                         (struct rtl_sta_info *)sta->drv_priv;
1015                                 if (!sta_entry) {
1016                                         rcu_read_unlock();
1017                                         return true;
1018                                 }
1019                                 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1020                                 tid = (capab &
1021                                         IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1022                                 tid_data = &sta_entry->tids[tid];
1023                                 if (tid_data->agg.rx_agg_state ==
1024                                     RTL_RX_AGG_START) {
1025                                         skb_delba = rtl92e_make_del_ba(hw,
1026                                                                     hdr->addr2,
1027                                                                     hdr->addr3,
1028                                                                     tid);
1029                                         if (skb_delba) {
1030                                                 rx_status.freq =
1031                                                         hw->conf.chandef.chan->center_freq;
1032                                                 rx_status.band =
1033                                                         hw->conf.chandef.chan->band;
1034                                                 rx_status.flag |= RX_FLAG_DECRYPTED;
1035                                                 rx_status.flag |= RX_FLAG_MACTIME_MPDU;
1036                                                 rx_status.rate_idx = 0;
1037                                                 rx_status.signal = 50 + 10;
1038                                                 memcpy(IEEE80211_SKB_RXCB(skb_delba),
1039                                                        &rx_status, sizeof(rx_status));
1040                                                 RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG,
1041                                                               "fake del\n",
1042                                                               skb_delba->data, skb_delba->len);
1043                                                 ieee80211_rx_irqsafe(hw, skb_delba);
1044                                         }
1045                                 }
1046                                 rcu_read_unlock();
1047                         }
1048                         break;
1049                 case ACT_ADDBARSP:
1050                         RT_TRACE((COMP_SEND | COMP_RECV), DBG_DMESG,
1051                                  ("%s ACT_ADDBARSP From :%pM\n",
1052                                   is_tx ? "Tx" : "Rx", hdr->addr2));
1053                         break;
1054                 case ACT_DELBA:
1055                         RT_TRACE((COMP_SEND | COMP_RECV), DBG_DMESG,
1056                                  ("ACT_ADDBADEL From :%pM\n", hdr->addr2));
1057                         break;
1058                 }
1059                 break;
1060         default:
1061                 break;
1062         }
1063
1064         return true;
1065 }
1066
1067 /*should call before software enc*/
1068 u8 rtl92e_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb,
1069                           u8 is_tx)
1070 {
1071         struct rtl_priv *rtlpriv = rtl_priv(hw);
1072         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1073         __le16 fc = rtl_get_fc(skb);
1074         u16 ether_type;
1075         u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
1076         const struct iphdr *ip;
1077
1078         if (!ieee80211_is_data(fc))
1079                 goto end;
1080
1081         ip = (struct iphdr *)((u8 *)skb->data + mac_hdr_len +
1082                               SNAP_SIZE + PROTOC_TYPE_SIZE);
1083         ether_type = be16_to_cpup((__be16 *)
1084                                   (skb->data + mac_hdr_len + SNAP_SIZE));
1085
1086         if (ETH_P_IP == ether_type) {
1087                 if (IPPROTO_UDP == ip->protocol) {
1088                         struct udphdr *udp = (struct udphdr *)((u8 *)ip +
1089                                                                (ip->ihl << 2));
1090                         if (((((u8 *)udp)[1] == 68) &&
1091                              (((u8 *)udp)[3] == 67)) ||
1092                             ((((u8 *)udp)[1] == 67) &&
1093                              (((u8 *)udp)[3] == 68))) {
1094                                 /*
1095                                  * 68 : UDP BOOTP client
1096                                  * 67 : UDP BOOTP server
1097                                  */
1098                                 RT_TRACE((COMP_SEND | COMP_RECV),
1099                                          DBG_DMESG, ("dhcp %s !!\n",
1100                                                      (is_tx) ? "Tx" : "Rx"));
1101
1102                                 if (is_tx) {
1103                                         rtlpriv->ra.is_special_data = true;
1104                                         if (rtlpriv->cfg->ops->get_btc_status())
1105                                                 rtlpriv->btcoexist.btc_ops->btc_special_packet_notify(
1106                                                                         rtlpriv, 1);
1107                                         rtl92e_lps_leave(hw);
1108                                         ppsc->last_delaylps_stamp_jiffies =
1109                                                                         jiffies;
1110                                 }
1111
1112                                 return true;
1113                         }
1114                 }
1115         } else if (ETH_P_ARP == ether_type) {
1116                 if (is_tx) {
1117                         rtlpriv->ra.is_special_data = true;
1118                         if (rtlpriv->cfg->ops->get_btc_status())
1119                                 rtlpriv->btcoexist.btc_ops->btc_special_packet_notify(
1120                                                         rtlpriv, 1);
1121                         rtl92e_lps_leave(hw);
1122                         ppsc->last_delaylps_stamp_jiffies = jiffies;
1123                 }
1124
1125                 return true;
1126         } else if (ETH_P_PAE == ether_type) {
1127                 RT_TRACE((COMP_SEND | COMP_RECV), DBG_DMESG,
1128                          ("802.1X %s EAPOL pkt!!\n", (is_tx) ? "Tx" : "Rx"));
1129
1130                 if (is_tx) {
1131                         rtlpriv->ra.is_special_data = true;
1132                         rtl92e_lps_leave(hw);
1133                         ppsc->last_delaylps_stamp_jiffies = jiffies;
1134                 }
1135
1136                 return true;
1137         } else if (0x86DD == ether_type) {
1138                 return true;
1139         }
1140
1141 end:
1142         rtlpriv->ra.is_special_data = false;
1143         return false;
1144 }
1145
1146 /*********************************************************
1147  *
1148  * functions called by core.c
1149  *
1150  *********************************************************/
1151 int rtl92e_tx_agg_start(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1152                         struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1153 {
1154         struct rtl_priv *rtlpriv = rtl_priv(hw);
1155         struct rtl_tid_data *tid_data;
1156         struct rtl_sta_info *sta_entry = NULL;
1157
1158         if (sta == NULL)
1159                 return -EINVAL;
1160
1161         if (unlikely(tid >= MAX_TID_COUNT))
1162                 return -EINVAL;
1163
1164         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1165         if (!sta_entry)
1166                 return -ENXIO;
1167         tid_data = &sta_entry->tids[tid];
1168
1169         RT_TRACE(COMP_SEND, DBG_DMESG,
1170                  ("on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1171                   tid_data->seq_number));
1172
1173         *ssn = tid_data->seq_number;
1174         tid_data->agg.agg_state = RTL_AGG_START;
1175
1176         ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1177         return 0;
1178 }
1179
1180 int rtl92e_tx_agg_stop(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1181                        struct ieee80211_sta *sta, u16 tid)
1182 {
1183         struct rtl_priv *rtlpriv = rtl_priv(hw);
1184         struct rtl_tid_data *tid_data;
1185         struct rtl_sta_info *sta_entry = NULL;
1186
1187         if (sta == NULL)
1188                 return -EINVAL;
1189
1190         /* Comparing an array to null is not useful */
1191         /*if (!sta->addr) {
1192                 RT_TRACE(COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
1193                 return -EINVAL;
1194         }*/
1195
1196         RT_TRACE(COMP_SEND, DBG_DMESG,
1197                  ("on ra = %pM tid = %d\n", sta->addr, tid));
1198
1199         if (unlikely(tid >= MAX_TID_COUNT))
1200                 return -EINVAL;
1201
1202         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1203         tid_data = &sta_entry->tids[tid];
1204         sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
1205
1206         ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1207         return 0;
1208 }
1209
1210 int rtl92e_rx_agg_start(struct ieee80211_hw *hw,
1211                         struct ieee80211_sta *sta, u16 tid)
1212 {
1213         struct rtl_priv *rtlpriv = rtl_priv(hw);
1214         struct rtl_tid_data *tid_data;
1215         struct rtl_sta_info *sta_entry = NULL;
1216
1217         if (sta == NULL)
1218                 return -EINVAL;
1219
1220         if (unlikely(tid >= MAX_TID_COUNT))
1221                 return -EINVAL;
1222
1223         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1224         if (!sta_entry)
1225                 return -ENXIO;
1226         tid_data = &sta_entry->tids[tid];
1227
1228         RT_TRACE(COMP_RECV, DBG_DMESG,
1229                  ("on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1230                  tid_data->seq_number));
1231
1232         tid_data->agg.rx_agg_state = RTL_RX_AGG_START;
1233         return 0;
1234 }
1235
1236 int rtl92e_rx_agg_stop(struct ieee80211_hw *hw,
1237                        struct ieee80211_sta *sta, u16 tid)
1238 {
1239         struct rtl_priv *rtlpriv = rtl_priv(hw);
1240         struct rtl_tid_data *tid_data;
1241         struct rtl_sta_info *sta_entry = NULL;
1242
1243         if (sta == NULL)
1244                 return -EINVAL;
1245
1246         /* Comparing an array to null is not useful */
1247         /*if (!sta->addr) {
1248                 RT_TRACE(COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
1249                 return -EINVAL;
1250         }*/
1251
1252         RT_TRACE(COMP_SEND, DBG_DMESG,
1253                  ("on ra = %pM tid = %d\n", sta->addr, tid));
1254
1255         if (unlikely(tid >= MAX_TID_COUNT))
1256                 return -EINVAL;
1257
1258         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1259         tid_data = &sta_entry->tids[tid];
1260         sta_entry->tids[tid].agg.rx_agg_state = RTL_RX_AGG_STOP;
1261
1262         return 0;
1263 }
1264
1265 int rtl92e_tx_agg_oper(struct ieee80211_hw *hw,
1266                        struct ieee80211_sta *sta, u16 tid)
1267 {
1268         struct rtl_priv *rtlpriv = rtl_priv(hw);
1269         struct rtl_tid_data *tid_data;
1270         struct rtl_sta_info *sta_entry = NULL;
1271
1272         if (sta == NULL)
1273                 return -EINVAL;
1274
1275         /* Comparing an array to null is not useful */
1276         /*if (!sta->addr) {
1277                 RT_TRACE(COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
1278                 return -EINVAL;
1279         }*/
1280
1281         RT_TRACE(COMP_SEND, DBG_DMESG,
1282                  ("on ra = %pM tid = %d\n", sta->addr, tid));
1283
1284         if (unlikely(tid >= MAX_TID_COUNT))
1285                 return -EINVAL;
1286
1287         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1288         tid_data = &sta_entry->tids[tid];
1289         sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
1290
1291         return 0;
1292 }
1293
1294 /*********************************************************
1295  *
1296  * wq & timer callback functions
1297  *
1298  *********************************************************/
1299 /* this function is used for roaming */
1300 void rtl92e_beacon_statistic(struct ieee80211_hw *hw, struct sk_buff *skb)
1301 {
1302         struct rtl_priv *rtlpriv = rtl_priv(hw);
1303         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1304
1305         if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
1306                 return;
1307
1308         if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
1309                 return;
1310
1311         /* check if this really is a beacon */
1312         if (!ieee80211_is_beacon(hdr->frame_control) &&
1313             !ieee80211_is_probe_resp(hdr->frame_control))
1314                 return;
1315
1316         /* min. beacon length + FCS_LEN */
1317         if (skb->len <= 40 + FCS_LEN)
1318                 return;
1319
1320         /* and only beacons from the associated BSSID, please */
1321         if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
1322                 return;
1323
1324         rtlpriv->link_info.bcn_rx_inperiod++;
1325 }
1326
1327 void rtl92e_watchdog_wq_callback(void *data)
1328 {
1329         struct rtl_works *rtlworks = container_of_dwork_rtl(data,
1330                                                             struct rtl_works,
1331                                                             watchdog_wq);
1332         struct ieee80211_hw *hw = rtlworks->hw;
1333         struct rtl_priv *rtlpriv = rtl_priv(hw);
1334         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1335         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1336         bool b_busytraffic = false;
1337         bool b_tx_busy_traffic = false;
1338         bool b_rx_busy_traffic = false;
1339         bool b_higher_busytraffic = false;
1340         bool b_higher_busyrxtraffic = false;
1341         u8 idx, tid;
1342         u32 rx_cnt_inp4eriod = 0;
1343         u32 tx_cnt_inp4eriod = 0;
1344         u32 aver_rx_cnt_inperiod = 0;
1345         u32 aver_tx_cnt_inperiod = 0;
1346         u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
1347         u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
1348         bool benter_ps = false;
1349
1350         if (is_hal_stop(rtlhal))
1351                 return;
1352
1353         /* <1> Determine if action frame is allowed */
1354         if (mac->link_state > MAC80211_NOLINK) {
1355                 if (mac->cnt_after_linked < 20)
1356                         mac->cnt_after_linked++;
1357         } else {
1358                 mac->cnt_after_linked = 0;
1359         }
1360
1361         /* <2> to check if traffic busy, if
1362          * busytraffic we don't change channel */
1363         if (mac->link_state >= MAC80211_LINKED) {
1364                 /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
1365                 for (idx = 0; idx <= 2; idx++) {
1366                         rtlpriv->link_info.num_rx_in4period[idx] =
1367                             rtlpriv->link_info.num_rx_in4period[idx + 1];
1368                         rtlpriv->link_info.num_tx_in4period[idx] =
1369                             rtlpriv->link_info.num_tx_in4period[idx + 1];
1370                 }
1371                 rtlpriv->link_info.num_rx_in4period[3] =
1372                     rtlpriv->link_info.num_rx_inperiod;
1373                 rtlpriv->link_info.num_tx_in4period[3] =
1374                     rtlpriv->link_info.num_tx_inperiod;
1375                 for (idx = 0; idx <= 3; idx++) {
1376                         rx_cnt_inp4eriod +=
1377                             rtlpriv->link_info.num_rx_in4period[idx];
1378                         tx_cnt_inp4eriod +=
1379                             rtlpriv->link_info.num_tx_in4period[idx];
1380                 }
1381                 aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
1382                 aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
1383
1384                 /* (2) check traffic busy */
1385                 if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100) {
1386                         b_busytraffic = true;
1387                         if (aver_rx_cnt_inperiod > aver_tx_cnt_inperiod)
1388                                 b_rx_busy_traffic = true;
1389                         else
1390                                 b_tx_busy_traffic = false;
1391                 }
1392
1393                 /* Higher Tx/Rx data. */
1394                 if (aver_rx_cnt_inperiod > 4000 ||
1395                     aver_tx_cnt_inperiod > 4000) {
1396                         b_higher_busytraffic = true;
1397
1398                         /* Extremely high Rx data. */
1399                         if (aver_rx_cnt_inperiod > 5000)
1400                                 b_higher_busyrxtraffic = true;
1401                 }
1402
1403                 /* check every tid's tx traffic */
1404                 for (tid = 0; tid <= 7; tid++) {
1405                         for (idx = 0; idx <= 2; idx++)
1406                                 rtlpriv->link_info.tidtx_in4period[tid][idx] =
1407                                         rtlpriv->link_info.tidtx_in4period[tid]
1408                                         [idx + 1];
1409                         rtlpriv->link_info.tidtx_in4period[tid][3] =
1410                                 rtlpriv->link_info.tidtx_inperiod[tid];
1411
1412                         for (idx = 0; idx <= 3; idx++)
1413                                 tidtx_inp4eriod[tid] +=
1414                                    rtlpriv->link_info.tidtx_in4period[tid][idx];
1415                         aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
1416                         if (aver_tidtx_inperiod[tid] > 5000)
1417                                 rtlpriv->link_info.higher_busytxtraffic[tid] =
1418                                                                         true;
1419                         else
1420                                 rtlpriv->link_info.higher_busytxtraffic[tid] =
1421                                                                         false;
1422                 }
1423
1424                 if (((rtlpriv->link_info.num_rx_inperiod +
1425                       rtlpriv->link_info.num_tx_inperiod) > 8) ||
1426                     (rtlpriv->link_info.num_rx_inperiod > 2))
1427                         benter_ps = false;
1428                 else
1429                         benter_ps = true;
1430
1431                 /* LeisurePS only work in infra mode. */
1432                 if (benter_ps)
1433                         rtl92e_lps_enter(hw);
1434                 else
1435                         rtl92e_lps_leave(hw);
1436         }
1437
1438         rtlpriv->link_info.num_rx_inperiod = 0;
1439         rtlpriv->link_info.num_tx_inperiod = 0;
1440         for (tid = 0; tid <= 7; tid++)
1441                 rtlpriv->link_info.tidtx_inperiod[tid] = 0;
1442
1443         rtlpriv->link_info.b_busytraffic = b_busytraffic;
1444         rtlpriv->link_info.b_rx_busy_traffic = b_rx_busy_traffic;
1445         rtlpriv->link_info.b_tx_busy_traffic = b_tx_busy_traffic;
1446         rtlpriv->link_info.b_higher_busytraffic = b_higher_busytraffic;
1447         rtlpriv->link_info.b_higher_busyrxtraffic = b_higher_busyrxtraffic;
1448
1449         /* <3> DM */
1450         rtlpriv->cfg->ops->dm_watchdog(hw);
1451
1452         /* <4> roaming */
1453         if (mac->link_state == MAC80211_LINKED &&
1454             mac->opmode == NL80211_IFTYPE_STATION) {
1455                 if ((rtlpriv->link_info.bcn_rx_inperiod +
1456                         rtlpriv->link_info.num_rx_inperiod) == 0) {
1457                         rtlpriv->link_info.roam_times++;
1458                         RT_TRACE(COMP_ERR, DBG_DMESG,
1459                                  ("AP off for %d s\n",
1460                                   (rtlpriv->link_info.roam_times * 2)));
1461
1462                         /* if we can't recv beacon for 10s,
1463                         * we should reconnect this AP */
1464                         if (rtlpriv->link_info.roam_times >= 5) {
1465                                 RT_TRACE(COMP_ERR, DBG_EMERG,
1466                                          ("AP off, try to reconnect now\n"));
1467                                 rtlpriv->link_info.roam_times = 0;
1468                                 ieee80211_connection_loss(
1469                                         rtlpriv->mac80211.vif);
1470                         }
1471                 } else {
1472                         rtlpriv->link_info.roam_times = 0;
1473                 }
1474         }
1475
1476         if (rtlpriv->cfg->ops->get_btc_status())
1477                 rtlpriv->btcoexist.btc_ops->btc_periodical(rtlpriv);
1478
1479         rtlpriv->link_info.bcn_rx_inperiod = 0;
1480 }
1481
1482 void rtl92e_watch_dog_timer_callback(unsigned long data)
1483 {
1484         struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1485         struct rtl_priv *rtlpriv = rtl_priv(hw);
1486
1487         queue_delayed_work(rtlpriv->works.rtl_wq,
1488                            &rtlpriv->works.watchdog_wq, 0);
1489
1490         mod_timer(&rtlpriv->works.watchdog_timer,
1491                   jiffies + MSECS(RTL_WATCH_DOG_TIME));
1492 }
1493 void rtl92e_fwevt_wq_callback(void *data)
1494 {
1495         struct rtl_works *rtlworks =
1496                 container_of_dwork_rtl(data, struct rtl_works, fwevt_wq);
1497         struct ieee80211_hw *hw = rtlworks->hw;
1498         struct rtl_priv *rtlpriv = rtl_priv(hw);
1499
1500         rtlpriv->cfg->ops->c2h_command_handle(hw);
1501 }
1502 void rtl92e_easy_concurrent_retrytimer_callback(unsigned long data)
1503 {
1504         struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1505         struct rtl_priv *rtlpriv = rtl_priv(hw);
1506         struct rtl_priv *buddy_priv = rtlpriv->buddy_priv;
1507
1508         if (buddy_priv == NULL)
1509                 return;
1510
1511         rtlpriv->cfg->ops->dualmac_easy_concurrent(hw);
1512 }
1513 /*********************************************************
1514  *
1515  * frame process functions
1516  *
1517  *********************************************************/
1518 u8 *rtl92e_find_ie(u8 *data, unsigned int len, u8 ie)
1519 {
1520         struct ieee80211_mgmt *mgmt = (void *)data;
1521         u8 *pos, *end;
1522
1523         pos = (u8 *)mgmt->u.beacon.variable;
1524         end = data + len;
1525         while (pos < end) {
1526                 if (pos + 2 + pos[1] > end)
1527                         return NULL;
1528
1529                 if (pos[0] == ie)
1530                         return pos;
1531
1532                 pos += 2 + pos[1];
1533         }
1534         return NULL;
1535 }
1536
1537 /* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
1538 /* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
1539 static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
1540                                             enum ieee80211_smps_mode smps,
1541                                             u8 *da, u8 *bssid)
1542 {
1543         struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1544         struct sk_buff *skb;
1545         struct ieee80211_mgmt_compat *action_frame;
1546
1547         /* 27 = header + category + action + smps mode */
1548         skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
1549         if (!skb)
1550                 return NULL;
1551
1552         skb_reserve(skb, hw->extra_tx_headroom);
1553         action_frame = (void *)skb_put(skb, 27);
1554         memset(action_frame, 0, 27);
1555         ether_addr_copy(action_frame->da, da);
1556         ether_addr_copy(action_frame->sa, rtlefuse->dev_addr);
1557         ether_addr_copy(action_frame->bssid, bssid);
1558         action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1559                                                   IEEE80211_STYPE_ACTION);
1560         action_frame->u.action.category = WLAN_CATEGORY_HT;
1561         action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
1562         switch (smps) {
1563         case IEEE80211_SMPS_AUTOMATIC:/* 0 */
1564         case IEEE80211_SMPS_NUM_MODES:/* 4 */
1565                 WARN_ON(1);
1566         /* Here will get a 'MISSING_BREAK' in Coverity Test, just ignore it.
1567          * According to Kernel Code, here is right.
1568          */
1569         case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
1570                 action_frame->u.action.u.ht_smps.smps_control =
1571                                 WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
1572                 break;
1573         case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
1574                 action_frame->u.action.u.ht_smps.smps_control =
1575                                 WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
1576                 break;
1577         case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
1578                 action_frame->u.action.u.ht_smps.smps_control =
1579                                 WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
1580                 break;
1581         }
1582
1583         return skb;
1584 }
1585
1586 int stg_rtl_send_smps_action(struct ieee80211_hw *hw,
1587                              struct ieee80211_sta *sta,
1588                              enum ieee80211_smps_mode smps)
1589 {
1590         struct rtl_priv *rtlpriv = rtl_priv(hw);
1591         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1592         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1593         struct sk_buff *skb = NULL;
1594         struct rtl_tcb_desc tcb_desc;
1595         u8 bssid[ETH_ALEN] = {0};
1596
1597         memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
1598
1599         if (rtlpriv->mac80211.act_scanning)
1600                 goto err_free;
1601
1602         if (!sta)
1603                 goto err_free;
1604
1605         if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
1606                 goto err_free;
1607
1608         if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1609                 goto err_free;
1610
1611         if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP)
1612                 ether_addr_copy(bssid, rtlpriv->efuse.dev_addr);
1613         else
1614                 ether_addr_copy(bssid, rtlpriv->mac80211.bssid);
1615
1616         skb = rtl_make_smps_action(hw, smps, sta->addr, bssid);
1617         /* this is a type = mgmt * stype = action frame */
1618         if (skb) {
1619                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1620                 struct rtl_sta_info *sta_entry =
1621                         (struct rtl_sta_info *)sta->drv_priv;
1622                 sta_entry->mimo_ps = smps;
1623                 /* rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0); */
1624
1625                 info->control.rates[0].idx = 0;
1626                 info->band = hw->conf.chandef.chan->band;
1627                 rtlpriv->intf_ops->adapter_tx(hw, sta, skb, &tcb_desc);
1628         }
1629         return 1;
1630
1631 err_free:
1632         return 0;
1633 }
1634 EXPORT_SYMBOL(stg_rtl_send_smps_action);
1635
1636 /* because mac80211 have issues when can receive del ba
1637  * so here we just make a fake del_ba if we receive a ba_req
1638  * but rx_agg was opened to let mac80211 release some ba
1639  * related resources, so please this del_ba for tx */
1640 struct sk_buff *rtl92e_make_del_ba(struct ieee80211_hw *hw,
1641                                    u8 *sa, u8 *bssid, u16 tid)
1642 {
1643         struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1644         struct sk_buff *skb;
1645         struct ieee80211_mgmt *action_frame;
1646         u16 params;
1647
1648         /* 27 = header + category + action + smps mode */
1649         skb = dev_alloc_skb(34 + hw->extra_tx_headroom);
1650         if (!skb)
1651                 return NULL;
1652
1653         skb_reserve(skb, hw->extra_tx_headroom);
1654         action_frame = (void *)skb_put(skb, 34);
1655         memset(action_frame, 0, 34);
1656         ether_addr_copy(action_frame->sa, sa);
1657         ether_addr_copy(action_frame->da, rtlefuse->dev_addr);
1658         ether_addr_copy(action_frame->bssid, bssid);
1659         action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1660                                                   IEEE80211_STYPE_ACTION);
1661         action_frame->u.action.category = WLAN_CATEGORY_BACK;
1662         action_frame->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1663         params = (u16)(1 << 11);        /* bit 11 initiator */
1664         params |= (u16)(tid << 12);     /* bit 15:12 TID number */
1665
1666         action_frame->u.action.u.delba.params = cpu_to_le16(params);
1667         action_frame->u.action.u.delba.reason_code =
1668                 cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
1669
1670         return skb;
1671 }
1672
1673 /*********************************************************
1674  *
1675  * IOT functions
1676  *
1677  *********************************************************/
1678 static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
1679                                   struct octet_string vendor_ie)
1680 {
1681         struct rtl_priv *rtlpriv = rtl_priv(hw);
1682         bool matched = false;
1683         static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
1684         static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
1685         static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
1686         static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
1687         static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
1688         static u8 racap[] = { 0x00, 0x0c, 0x43 };
1689         static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
1690         static u8 marvcap[] = { 0x00, 0x50, 0x43 };
1691
1692         if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
1693             memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
1694                 rtlpriv->mac80211.vendor = PEER_ATH;
1695                 matched = true;
1696         } else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
1697                    memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
1698                    memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
1699                 rtlpriv->mac80211.vendor = PEER_BROAD;
1700                 matched = true;
1701         } else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
1702                 rtlpriv->mac80211.vendor = PEER_RAL;
1703                 matched = true;
1704         } else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
1705                 rtlpriv->mac80211.vendor = PEER_CISCO;
1706                 matched = true;
1707         } else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
1708                 rtlpriv->mac80211.vendor = PEER_MARV;
1709                 matched = true;
1710         }
1711
1712         return matched;
1713 }
1714
1715 static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data, unsigned int len)
1716 {
1717         struct ieee80211_mgmt *mgmt = (void *)data;
1718         struct octet_string vendor_ie;
1719         u8 *pos, *end;
1720
1721         pos = (u8 *)mgmt->u.beacon.variable;
1722         end = data + len;
1723         while (pos < end) {
1724                 if (pos[0] == 221) {
1725                         vendor_ie.length = pos[1];
1726                         vendor_ie.octet = &pos[2];
1727                         if (rtl_chk_vendor_ouisub(hw, vendor_ie))
1728                                 return true;
1729                 }
1730
1731                 if (pos + 2 + pos[1] > end)
1732                         return false;
1733
1734                 pos += 2 + pos[1];
1735         }
1736         return false;
1737 }
1738
1739 void rtl92e_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
1740 {
1741         struct rtl_priv *rtlpriv = rtl_priv(hw);
1742         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1743         struct ieee80211_hdr *hdr = (void *)data;
1744         u32 vendor = PEER_UNKNOWN;
1745
1746         static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
1747         static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
1748         static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
1749         static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
1750         static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
1751         static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
1752         static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
1753         static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
1754         static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
1755         static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
1756         static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
1757         static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
1758         static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
1759         static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
1760         static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
1761         static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
1762
1763         if (mac->opmode != NL80211_IFTYPE_STATION)
1764                 return;
1765
1766         if (mac->link_state == MAC80211_NOLINK) {
1767                 mac->vendor = PEER_UNKNOWN;
1768                 return;
1769         }
1770
1771         if (mac->cnt_after_linked > 2)
1772                 return;
1773
1774         /* check if this really is a beacon */
1775         if (!ieee80211_is_beacon(hdr->frame_control))
1776                 return;
1777
1778         /* min. beacon length + FCS_LEN */
1779         if (len <= 40 + FCS_LEN)
1780                 return;
1781
1782         /* and only beacons from the associated BSSID, please */
1783         if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
1784                 return;
1785
1786         if (rtl_find_221_ie(hw, data, len))
1787                 vendor = mac->vendor;
1788
1789         if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
1790             (memcmp(mac->bssid, ap5_2, 3) == 0) ||
1791             (memcmp(mac->bssid, ap5_3, 3) == 0) ||
1792             (memcmp(mac->bssid, ap5_4, 3) == 0) ||
1793             (memcmp(mac->bssid, ap5_5, 3) == 0) ||
1794             (memcmp(mac->bssid, ap5_6, 3) == 0) ||
1795                 vendor == PEER_ATH) {
1796                 vendor = PEER_ATH;
1797                 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>ath find\n"));
1798         } else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
1799                    (memcmp(mac->bssid, ap4_5, 3) == 0) ||
1800                    (memcmp(mac->bssid, ap4_1, 3) == 0) ||
1801                    (memcmp(mac->bssid, ap4_2, 3) == 0) ||
1802                    (memcmp(mac->bssid, ap4_3, 3) == 0) ||
1803                 vendor == PEER_RAL) {
1804                 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>ral findn\n"));
1805                 vendor = PEER_RAL;
1806         } else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
1807                 vendor == PEER_CISCO) {
1808                 vendor = PEER_CISCO;
1809                 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>cisco find\n"));
1810         } else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
1811                 (memcmp(mac->bssid, ap3_2, 3) == 0) ||
1812                 (memcmp(mac->bssid, ap3_3, 3) == 0) ||
1813                 vendor == PEER_BROAD) {
1814                 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>broad find\n"));
1815                 vendor = PEER_BROAD;
1816         } else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
1817                 vendor == PEER_MARV) {
1818                 vendor = PEER_MARV;
1819                 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>marv find\n"));
1820         }
1821
1822         mac->vendor = vendor;
1823 }
1824
1825 /*********************************************************
1826  *
1827  * sysfs functions
1828  *
1829  *********************************************************/
1830 struct rtl_global_var global_var = {};
1831
1832 int  rtl_core_module_init(void)
1833 {
1834         static int here_once;
1835
1836         if (here_once++)
1837                 return 0;
1838
1839         if (rtl92e_rate_control_register())
1840                 pr_debug("rtl: Unable to register rtl_rc, use default RC !!\n");
1841
1842         /* init some global vars */
1843         INIT_LIST_HEAD(&global_var.glb_priv_list);
1844         spin_lock_init(&global_var.glb_list_lock);
1845
1846         return 0;
1847 }
1848 void  rtl_core_module_exit(void)
1849 {
1850         /*RC*/
1851         rtl92e_rate_control_unregister();
1852 }