Linux-libre 3.17.4-gnu
[librecmc/linux-libre.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("%s", 0, 0, rtlpriv->cfg->name);
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                 if ((map & 0x000c) >> 2 == IEEE80211_VHT_MCS_SUPPORT_0_7)
831                         hw_rate =
832                         rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS7];
833                 else if ((map  & 0x000c) >> 2 == IEEE80211_VHT_MCS_SUPPORT_0_8)
834                         hw_rate = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS9];
835                 else
836                         hw_rate = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS9];
837         } else {
838                 if ((map  & 0x0003) == IEEE80211_VHT_MCS_SUPPORT_0_7)
839                         hw_rate = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS7];
840                 else if ((map  & 0x0003) == IEEE80211_VHT_MCS_SUPPORT_0_8)
841                         hw_rate = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS9];
842                 else
843                         hw_rate = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS9];
844         }
845
846         return hw_rate;
847 }
848
849 static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw,
850                                   struct ieee80211_sta *sta)
851 {
852         struct rtl_priv *rtlpriv = rtl_priv(hw);
853         struct rtl_phy *rtlphy = &(rtlpriv->phy);
854         u8 hw_rate;
855
856         if ((get_rf_type(rtlphy) == RF_2T2R) &&
857             (sta->ht_cap.mcs.rx_mask[1] != 0))
858                 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
859         else
860                 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
861
862         return hw_rate;
863 }
864
865 void stg_rtl_get_tcb_desc(struct ieee80211_hw *hw,
866                           struct ieee80211_tx_info *info,
867                           struct ieee80211_sta *sta,
868                           struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
869 {
870         struct rtl_priv *rtlpriv = rtl_priv(hw);
871         struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
872         struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
873         struct ieee80211_rate *txrate;
874         __le16 fc = rtl_get_fc(skb);
875
876         txrate = ieee80211_get_tx_rate(hw, info);
877         if (txrate != NULL)
878                 tcb_desc->hw_rate = txrate->hw_value;
879
880         if (ieee80211_is_data(fc)) {
881                 /*
882                  *we set data rate INX 0
883                  *in rtl_rc.c   if skb is special data or
884                  *mgt which need low data rate.
885                  */
886
887                 /*
888                  *So tcb_desc->hw_rate is just used for
889                  *special data and mgt frames
890                  */
891                 if (info->control.rates[0].idx == 0 ||
892                     ieee80211_is_nullfunc(fc)) {
893                         tcb_desc->use_driver_rate = true;
894                         tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
895
896                         tcb_desc->disable_ratefallback = 1;
897                 } else {
898                         /*
899                          *because hw will nerver use hw_rate
900                          *when tcb_desc->use_driver_rate = false
901                          *so we never set highest N rate here,
902                          *and N rate will all be controled by FW
903                          *when tcb_desc->use_driver_rate = false
904                          */
905                         if (sta && sta->vht_cap.vht_supported) {
906                                 tcb_desc->hw_rate =
907                                 _rtl_get_vht_highest_n_rate(hw, sta);
908                         } else if (sta && (sta->ht_cap.ht_supported)) {
909                                 tcb_desc->hw_rate =
910                                         _rtl_get_highest_n_rate(hw, sta);
911                         } else {
912                                 if (rtlmac->mode == WIRELESS_MODE_B) {
913                                         tcb_desc->hw_rate =
914                                             rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
915                                 } else {
916                                         tcb_desc->hw_rate =
917                                             rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
918                                 }
919                         }
920                 }
921
922                 if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
923                         tcb_desc->b_multicast = 1;
924                 else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
925                         tcb_desc->b_broadcast = 1;
926
927                 _rtl_txrate_selectmode(hw, sta, tcb_desc);
928                 _rtl_query_bandwidth_mode(hw, sta, tcb_desc);
929                 _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
930                 _rtl_query_shortgi(hw, sta, tcb_desc, info);
931                 _rtl_query_protection_mode(hw, tcb_desc, info);
932         } else {
933                 tcb_desc->use_driver_rate = true;
934                 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
935                 tcb_desc->disable_ratefallback = 1;
936                 tcb_desc->mac_id = 0;
937                 tcb_desc->packet_bw = 0;
938         }
939 }
940 EXPORT_SYMBOL(stg_rtl_get_tcb_desc);
941
942 bool rtl92e_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
943 {
944         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
945         struct rtl_priv *rtlpriv = rtl_priv(hw);
946         __le16 fc = rtl_get_fc(skb);
947
948         if (rtlpriv->dm.supp_phymode_switch &&
949             mac->link_state < MAC80211_LINKED &&
950             (ieee80211_is_auth(fc) || ieee80211_is_probe_req(fc))) {
951                 if (rtlpriv->cfg->ops->check_switch_to_dmdp)
952                         rtlpriv->cfg->ops->check_switch_to_dmdp(hw);
953         }
954         if (ieee80211_is_auth(fc)) {
955                 RT_TRACE(COMP_SEND, DBG_DMESG, ("MAC80211_LINKING\n"));
956                 rtl92e_ips_nic_on(hw);
957
958                 mac->link_state = MAC80211_LINKING;
959                 /* Dul mac */
960                 rtlpriv->phy.b_need_iqk = true;
961         }
962         return true;
963 }
964
965 struct sk_buff *rtl92e_make_del_ba(struct ieee80211_hw *hw, u8 *sa,
966                                    u8 *bssid, u16 tid);
967
968 bool rtl92e_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
969 {
970         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
971         struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
972         struct rtl_priv *rtlpriv = rtl_priv(hw);
973         __le16 fc = rtl_get_fc(skb);
974         u8 *act = (u8 *)(((u8 *)skb->data + MAC80211_3ADDR_LEN));
975         u8 category;
976
977         if (!ieee80211_is_action(fc))
978                 return true;
979
980         category = *act;
981         act++;
982         switch (category) {
983         case ACT_CAT_BA:
984                 switch (*act) {
985                 case ACT_ADDBAREQ:
986                         if (mac->act_scanning)
987                                 return false;
988
989                         RT_TRACE((COMP_SEND | COMP_RECV), DBG_DMESG,
990                                  ("%s ACT_ADDBAREQ From:%pM\n",
991                                 is_tx ? "Tx" : "Rx", hdr->addr2));
992                         RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG, "req\n",
993                                       skb->data, skb->len);
994                         if (!is_tx) {
995                                 struct ieee80211_sta *sta = NULL;
996                                 struct rtl_sta_info *sta_entry = NULL;
997                                 struct ieee80211_mgmt *mgmt = (void *)skb->data;
998                                 u16 capab = 0, tid = 0;
999                                 struct rtl_tid_data *tid_data;
1000                                 struct sk_buff *skb_delba = NULL;
1001                                 struct ieee80211_rx_status rx_status = { 0 };
1002
1003                                 rcu_read_lock();
1004                                 sta = rtl_find_sta(hw, hdr->addr3);
1005                                 if (sta == NULL) {
1006                                         RT_TRACE((COMP_SEND | COMP_RECV),
1007                                                  DBG_TRACE, ("sta is NULL\n"));
1008                                         rcu_read_unlock();
1009                                         return true;
1010                                 }
1011
1012                                 sta_entry =
1013                                         (struct rtl_sta_info *)sta->drv_priv;
1014                                 if (!sta_entry) {
1015                                         rcu_read_unlock();
1016                                         return true;
1017                                 }
1018                                 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1019                                 tid = (capab &
1020                                         IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1021                                 tid_data = &sta_entry->tids[tid];
1022                                 if (tid_data->agg.rx_agg_state ==
1023                                     RTL_RX_AGG_START) {
1024                                         skb_delba = rtl92e_make_del_ba(hw,
1025                                                                     hdr->addr2,
1026                                                                     hdr->addr3,
1027                                                                     tid);
1028                                         if (skb_delba) {
1029                                                 rx_status.freq =
1030                                                         hw->conf.chandef.chan->center_freq;
1031                                                 rx_status.band =
1032                                                         hw->conf.chandef.chan->band;
1033                                                 rx_status.flag |= RX_FLAG_DECRYPTED;
1034                                                 rx_status.flag |= RX_FLAG_MACTIME_MPDU;
1035                                                 rx_status.rate_idx = 0;
1036                                                 rx_status.signal = 50 + 10;
1037                                                 memcpy(IEEE80211_SKB_RXCB(skb_delba),
1038                                                        &rx_status, sizeof(rx_status));
1039                                                 RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG,
1040                                                               "fake del\n",
1041                                                               skb_delba->data, skb_delba->len);
1042                                                 ieee80211_rx_irqsafe(hw, skb_delba);
1043                                         }
1044                                 }
1045                                 rcu_read_unlock();
1046                         }
1047                         break;
1048                 case ACT_ADDBARSP:
1049                         RT_TRACE((COMP_SEND | COMP_RECV), DBG_DMESG,
1050                                  ("%s ACT_ADDBARSP From :%pM\n",
1051                                   is_tx ? "Tx" : "Rx", hdr->addr2));
1052                         break;
1053                 case ACT_DELBA:
1054                         RT_TRACE((COMP_SEND | COMP_RECV), DBG_DMESG,
1055                                  ("ACT_ADDBADEL From :%pM\n", hdr->addr2));
1056                         break;
1057                 }
1058                 break;
1059         default:
1060                 break;
1061         }
1062
1063         return true;
1064 }
1065
1066 /*should call before software enc*/
1067 u8 rtl92e_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb,
1068                           u8 is_tx)
1069 {
1070         struct rtl_priv *rtlpriv = rtl_priv(hw);
1071         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1072         __le16 fc = rtl_get_fc(skb);
1073         u16 ether_type;
1074         u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
1075         const struct iphdr *ip;
1076
1077         if (!ieee80211_is_data(fc))
1078                 goto end;
1079
1080         ip = (struct iphdr *)((u8 *)skb->data + mac_hdr_len +
1081                               SNAP_SIZE + PROTOC_TYPE_SIZE);
1082         ether_type = be16_to_cpup((__be16 *)
1083                                   (skb->data + mac_hdr_len + SNAP_SIZE));
1084
1085         if (ETH_P_IP == ether_type) {
1086                 if (IPPROTO_UDP == ip->protocol) {
1087                         struct udphdr *udp = (struct udphdr *)((u8 *)ip +
1088                                                                (ip->ihl << 2));
1089                         if (((((u8 *)udp)[1] == 68) &&
1090                              (((u8 *)udp)[3] == 67)) ||
1091                             ((((u8 *)udp)[1] == 67) &&
1092                              (((u8 *)udp)[3] == 68))) {
1093                                 /*
1094                                  * 68 : UDP BOOTP client
1095                                  * 67 : UDP BOOTP server
1096                                  */
1097                                 RT_TRACE((COMP_SEND | COMP_RECV),
1098                                          DBG_DMESG, ("dhcp %s !!\n",
1099                                                      (is_tx) ? "Tx" : "Rx"));
1100
1101                                 if (is_tx) {
1102                                         rtlpriv->ra.is_special_data = true;
1103                                         if (rtlpriv->cfg->ops->get_btc_status())
1104                                                 rtlpriv->btcoexist.btc_ops->btc_special_packet_notify(
1105                                                                         rtlpriv, 1);
1106                                         rtl92e_lps_leave(hw);
1107                                         ppsc->last_delaylps_stamp_jiffies =
1108                                                                         jiffies;
1109                                 }
1110
1111                                 return true;
1112                         }
1113                 }
1114         } else if (ETH_P_ARP == ether_type) {
1115                 if (is_tx) {
1116                         rtlpriv->ra.is_special_data = true;
1117                         if (rtlpriv->cfg->ops->get_btc_status())
1118                                 rtlpriv->btcoexist.btc_ops->btc_special_packet_notify(
1119                                                         rtlpriv, 1);
1120                         rtl92e_lps_leave(hw);
1121                         ppsc->last_delaylps_stamp_jiffies = jiffies;
1122                 }
1123
1124                 return true;
1125         } else if (ETH_P_PAE == ether_type) {
1126                 RT_TRACE((COMP_SEND | COMP_RECV), DBG_DMESG,
1127                          ("802.1X %s EAPOL pkt!!\n", (is_tx) ? "Tx" : "Rx"));
1128
1129                 if (is_tx) {
1130                         rtlpriv->ra.is_special_data = true;
1131                         rtl92e_lps_leave(hw);
1132                         ppsc->last_delaylps_stamp_jiffies = jiffies;
1133                 }
1134
1135                 return true;
1136         } else if (0x86DD == ether_type) {
1137                 return true;
1138         }
1139
1140 end:
1141         rtlpriv->ra.is_special_data = false;
1142         return false;
1143 }
1144
1145 /*********************************************************
1146  *
1147  * functions called by core.c
1148  *
1149  *********************************************************/
1150 int rtl92e_tx_agg_start(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1151                         struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1152 {
1153         struct rtl_priv *rtlpriv = rtl_priv(hw);
1154         struct rtl_tid_data *tid_data;
1155         struct rtl_sta_info *sta_entry = NULL;
1156
1157         if (sta == NULL)
1158                 return -EINVAL;
1159
1160         if (unlikely(tid >= MAX_TID_COUNT))
1161                 return -EINVAL;
1162
1163         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1164         if (!sta_entry)
1165                 return -ENXIO;
1166         tid_data = &sta_entry->tids[tid];
1167
1168         RT_TRACE(COMP_SEND, DBG_DMESG,
1169                  ("on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1170                   tid_data->seq_number));
1171
1172         *ssn = tid_data->seq_number;
1173         tid_data->agg.agg_state = RTL_AGG_START;
1174
1175         ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1176         return 0;
1177 }
1178
1179 int rtl92e_tx_agg_stop(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1180                        struct ieee80211_sta *sta, u16 tid)
1181 {
1182         struct rtl_priv *rtlpriv = rtl_priv(hw);
1183         struct rtl_tid_data *tid_data;
1184         struct rtl_sta_info *sta_entry = NULL;
1185
1186         if (sta == NULL)
1187                 return -EINVAL;
1188
1189         /* Comparing an array to null is not useful */
1190         /*if (!sta->addr) {
1191                 RT_TRACE(COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
1192                 return -EINVAL;
1193         }*/
1194
1195         RT_TRACE(COMP_SEND, DBG_DMESG,
1196                  ("on ra = %pM tid = %d\n", sta->addr, tid));
1197
1198         if (unlikely(tid >= MAX_TID_COUNT))
1199                 return -EINVAL;
1200
1201         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1202         tid_data = &sta_entry->tids[tid];
1203         sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
1204
1205         ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1206         return 0;
1207 }
1208
1209 int rtl92e_rx_agg_start(struct ieee80211_hw *hw,
1210                         struct ieee80211_sta *sta, u16 tid)
1211 {
1212         struct rtl_priv *rtlpriv = rtl_priv(hw);
1213         struct rtl_tid_data *tid_data;
1214         struct rtl_sta_info *sta_entry = NULL;
1215
1216         if (sta == NULL)
1217                 return -EINVAL;
1218
1219         if (unlikely(tid >= MAX_TID_COUNT))
1220                 return -EINVAL;
1221
1222         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1223         if (!sta_entry)
1224                 return -ENXIO;
1225         tid_data = &sta_entry->tids[tid];
1226
1227         RT_TRACE(COMP_RECV, DBG_DMESG,
1228                  ("on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1229                  tid_data->seq_number));
1230
1231         tid_data->agg.rx_agg_state = RTL_RX_AGG_START;
1232         return 0;
1233 }
1234
1235 int rtl92e_rx_agg_stop(struct ieee80211_hw *hw,
1236                        struct ieee80211_sta *sta, u16 tid)
1237 {
1238         struct rtl_priv *rtlpriv = rtl_priv(hw);
1239         struct rtl_tid_data *tid_data;
1240         struct rtl_sta_info *sta_entry = NULL;
1241
1242         if (sta == NULL)
1243                 return -EINVAL;
1244
1245         /* Comparing an array to null is not useful */
1246         /*if (!sta->addr) {
1247                 RT_TRACE(COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
1248                 return -EINVAL;
1249         }*/
1250
1251         RT_TRACE(COMP_SEND, DBG_DMESG,
1252                  ("on ra = %pM tid = %d\n", sta->addr, tid));
1253
1254         if (unlikely(tid >= MAX_TID_COUNT))
1255                 return -EINVAL;
1256
1257         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1258         tid_data = &sta_entry->tids[tid];
1259         sta_entry->tids[tid].agg.rx_agg_state = RTL_RX_AGG_STOP;
1260
1261         return 0;
1262 }
1263
1264 int rtl92e_tx_agg_oper(struct ieee80211_hw *hw,
1265                        struct ieee80211_sta *sta, u16 tid)
1266 {
1267         struct rtl_priv *rtlpriv = rtl_priv(hw);
1268         struct rtl_tid_data *tid_data;
1269         struct rtl_sta_info *sta_entry = NULL;
1270
1271         if (sta == NULL)
1272                 return -EINVAL;
1273
1274         /* Comparing an array to null is not useful */
1275         /*if (!sta->addr) {
1276                 RT_TRACE(COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
1277                 return -EINVAL;
1278         }*/
1279
1280         RT_TRACE(COMP_SEND, DBG_DMESG,
1281                  ("on ra = %pM tid = %d\n", sta->addr, tid));
1282
1283         if (unlikely(tid >= MAX_TID_COUNT))
1284                 return -EINVAL;
1285
1286         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1287         tid_data = &sta_entry->tids[tid];
1288         sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
1289
1290         return 0;
1291 }
1292
1293 /*********************************************************
1294  *
1295  * wq & timer callback functions
1296  *
1297  *********************************************************/
1298 /* this function is used for roaming */
1299 void rtl92e_beacon_statistic(struct ieee80211_hw *hw, struct sk_buff *skb)
1300 {
1301         struct rtl_priv *rtlpriv = rtl_priv(hw);
1302         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1303
1304         if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
1305                 return;
1306
1307         if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
1308                 return;
1309
1310         /* check if this really is a beacon */
1311         if (!ieee80211_is_beacon(hdr->frame_control) &&
1312             !ieee80211_is_probe_resp(hdr->frame_control))
1313                 return;
1314
1315         /* min. beacon length + FCS_LEN */
1316         if (skb->len <= 40 + FCS_LEN)
1317                 return;
1318
1319         /* and only beacons from the associated BSSID, please */
1320         if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
1321                 return;
1322
1323         rtlpriv->link_info.bcn_rx_inperiod++;
1324 }
1325
1326 void rtl92e_watchdog_wq_callback(void *data)
1327 {
1328         struct rtl_works *rtlworks = container_of_dwork_rtl(data,
1329                                                             struct rtl_works,
1330                                                             watchdog_wq);
1331         struct ieee80211_hw *hw = rtlworks->hw;
1332         struct rtl_priv *rtlpriv = rtl_priv(hw);
1333         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1334         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1335         bool b_busytraffic = false;
1336         bool b_tx_busy_traffic = false;
1337         bool b_rx_busy_traffic = false;
1338         bool b_higher_busytraffic = false;
1339         bool b_higher_busyrxtraffic = false;
1340         u8 idx, tid;
1341         u32 rx_cnt_inp4eriod = 0;
1342         u32 tx_cnt_inp4eriod = 0;
1343         u32 aver_rx_cnt_inperiod = 0;
1344         u32 aver_tx_cnt_inperiod = 0;
1345         u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
1346         u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
1347         bool benter_ps = false;
1348
1349         if (is_hal_stop(rtlhal))
1350                 return;
1351
1352         /* <1> Determine if action frame is allowed */
1353         if (mac->link_state > MAC80211_NOLINK) {
1354                 if (mac->cnt_after_linked < 20)
1355                         mac->cnt_after_linked++;
1356         } else {
1357                 mac->cnt_after_linked = 0;
1358         }
1359
1360         /* <2> to check if traffic busy, if
1361          * busytraffic we don't change channel */
1362         if (mac->link_state >= MAC80211_LINKED) {
1363                 /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
1364                 for (idx = 0; idx <= 2; idx++) {
1365                         rtlpriv->link_info.num_rx_in4period[idx] =
1366                             rtlpriv->link_info.num_rx_in4period[idx + 1];
1367                         rtlpriv->link_info.num_tx_in4period[idx] =
1368                             rtlpriv->link_info.num_tx_in4period[idx + 1];
1369                 }
1370                 rtlpriv->link_info.num_rx_in4period[3] =
1371                     rtlpriv->link_info.num_rx_inperiod;
1372                 rtlpriv->link_info.num_tx_in4period[3] =
1373                     rtlpriv->link_info.num_tx_inperiod;
1374                 for (idx = 0; idx <= 3; idx++) {
1375                         rx_cnt_inp4eriod +=
1376                             rtlpriv->link_info.num_rx_in4period[idx];
1377                         tx_cnt_inp4eriod +=
1378                             rtlpriv->link_info.num_tx_in4period[idx];
1379                 }
1380                 aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
1381                 aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
1382
1383                 /* (2) check traffic busy */
1384                 if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100) {
1385                         b_busytraffic = true;
1386                         if (aver_rx_cnt_inperiod > aver_tx_cnt_inperiod)
1387                                 b_rx_busy_traffic = true;
1388                         else
1389                                 b_tx_busy_traffic = false;
1390                 }
1391
1392                 /* Higher Tx/Rx data. */
1393                 if (aver_rx_cnt_inperiod > 4000 ||
1394                     aver_tx_cnt_inperiod > 4000) {
1395                         b_higher_busytraffic = true;
1396
1397                         /* Extremely high Rx data. */
1398                         if (aver_rx_cnt_inperiod > 5000)
1399                                 b_higher_busyrxtraffic = true;
1400                 }
1401
1402                 /* check every tid's tx traffic */
1403                 for (tid = 0; tid <= 7; tid++) {
1404                         for (idx = 0; idx <= 2; idx++)
1405                                 rtlpriv->link_info.tidtx_in4period[tid][idx] =
1406                                         rtlpriv->link_info.tidtx_in4period[tid]
1407                                         [idx + 1];
1408                         rtlpriv->link_info.tidtx_in4period[tid][3] =
1409                                 rtlpriv->link_info.tidtx_inperiod[tid];
1410
1411                         for (idx = 0; idx <= 3; idx++)
1412                                 tidtx_inp4eriod[tid] +=
1413                                    rtlpriv->link_info.tidtx_in4period[tid][idx];
1414                         aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
1415                         if (aver_tidtx_inperiod[tid] > 5000)
1416                                 rtlpriv->link_info.higher_busytxtraffic[tid] =
1417                                                                         true;
1418                         else
1419                                 rtlpriv->link_info.higher_busytxtraffic[tid] =
1420                                                                         false;
1421                 }
1422
1423                 if (((rtlpriv->link_info.num_rx_inperiod +
1424                       rtlpriv->link_info.num_tx_inperiod) > 8) ||
1425                     (rtlpriv->link_info.num_rx_inperiod > 2))
1426                         benter_ps = false;
1427                 else
1428                         benter_ps = true;
1429
1430                 /* LeisurePS only work in infra mode. */
1431                 if (benter_ps)
1432                         rtl92e_lps_enter(hw);
1433                 else
1434                         rtl92e_lps_leave(hw);
1435         }
1436
1437         rtlpriv->link_info.num_rx_inperiod = 0;
1438         rtlpriv->link_info.num_tx_inperiod = 0;
1439         for (tid = 0; tid <= 7; tid++)
1440                 rtlpriv->link_info.tidtx_inperiod[tid] = 0;
1441
1442         rtlpriv->link_info.b_busytraffic = b_busytraffic;
1443         rtlpriv->link_info.b_rx_busy_traffic = b_rx_busy_traffic;
1444         rtlpriv->link_info.b_tx_busy_traffic = b_tx_busy_traffic;
1445         rtlpriv->link_info.b_higher_busytraffic = b_higher_busytraffic;
1446         rtlpriv->link_info.b_higher_busyrxtraffic = b_higher_busyrxtraffic;
1447
1448         /* <3> DM */
1449         rtlpriv->cfg->ops->dm_watchdog(hw);
1450
1451         /* <4> roaming */
1452         if (mac->link_state == MAC80211_LINKED &&
1453             mac->opmode == NL80211_IFTYPE_STATION) {
1454                 if ((rtlpriv->link_info.bcn_rx_inperiod +
1455                         rtlpriv->link_info.num_rx_inperiod) == 0) {
1456                         rtlpriv->link_info.roam_times++;
1457                         RT_TRACE(COMP_ERR, DBG_DMESG,
1458                                  ("AP off for %d s\n",
1459                                   (rtlpriv->link_info.roam_times * 2)));
1460
1461                         /* if we can't recv beacon for 10s,
1462                         * we should reconnect this AP */
1463                         if (rtlpriv->link_info.roam_times >= 5) {
1464                                 RT_TRACE(COMP_ERR, DBG_EMERG,
1465                                          ("AP off, try to reconnect now\n"));
1466                                 rtlpriv->link_info.roam_times = 0;
1467                                 ieee80211_connection_loss(
1468                                         rtlpriv->mac80211.vif);
1469                         }
1470                 } else {
1471                         rtlpriv->link_info.roam_times = 0;
1472                 }
1473         }
1474
1475         if (rtlpriv->cfg->ops->get_btc_status())
1476                 rtlpriv->btcoexist.btc_ops->btc_periodical(rtlpriv);
1477
1478         rtlpriv->link_info.bcn_rx_inperiod = 0;
1479 }
1480
1481 void rtl92e_watch_dog_timer_callback(unsigned long data)
1482 {
1483         struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1484         struct rtl_priv *rtlpriv = rtl_priv(hw);
1485
1486         queue_delayed_work(rtlpriv->works.rtl_wq,
1487                            &rtlpriv->works.watchdog_wq, 0);
1488
1489         mod_timer(&rtlpriv->works.watchdog_timer,
1490                   jiffies + MSECS(RTL_WATCH_DOG_TIME));
1491 }
1492 void rtl92e_fwevt_wq_callback(void *data)
1493 {
1494         struct rtl_works *rtlworks =
1495                 container_of_dwork_rtl(data, struct rtl_works, fwevt_wq);
1496         struct ieee80211_hw *hw = rtlworks->hw;
1497         struct rtl_priv *rtlpriv = rtl_priv(hw);
1498
1499         rtlpriv->cfg->ops->c2h_command_handle(hw);
1500 }
1501 void rtl92e_easy_concurrent_retrytimer_callback(unsigned long data)
1502 {
1503         struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1504         struct rtl_priv *rtlpriv = rtl_priv(hw);
1505         struct rtl_priv *buddy_priv = rtlpriv->buddy_priv;
1506
1507         if (buddy_priv == NULL)
1508                 return;
1509
1510         rtlpriv->cfg->ops->dualmac_easy_concurrent(hw);
1511 }
1512 /*********************************************************
1513  *
1514  * frame process functions
1515  *
1516  *********************************************************/
1517 u8 *rtl92e_find_ie(u8 *data, unsigned int len, u8 ie)
1518 {
1519         struct ieee80211_mgmt *mgmt = (void *)data;
1520         u8 *pos, *end;
1521
1522         pos = (u8 *)mgmt->u.beacon.variable;
1523         end = data + len;
1524         while (pos < end) {
1525                 if (pos + 2 + pos[1] > end)
1526                         return NULL;
1527
1528                 if (pos[0] == ie)
1529                         return pos;
1530
1531                 pos += 2 + pos[1];
1532         }
1533         return NULL;
1534 }
1535
1536 /* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
1537 /* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
1538 static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
1539                                             enum ieee80211_smps_mode smps,
1540                                             u8 *da, u8 *bssid)
1541 {
1542         struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1543         struct sk_buff *skb;
1544         struct ieee80211_mgmt_compat *action_frame;
1545
1546         /* 27 = header + category + action + smps mode */
1547         skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
1548         if (!skb)
1549                 return NULL;
1550
1551         skb_reserve(skb, hw->extra_tx_headroom);
1552         action_frame = (void *)skb_put(skb, 27);
1553         memset(action_frame, 0, 27);
1554         ether_addr_copy(action_frame->da, da);
1555         ether_addr_copy(action_frame->sa, rtlefuse->dev_addr);
1556         ether_addr_copy(action_frame->bssid, bssid);
1557         action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1558                                                   IEEE80211_STYPE_ACTION);
1559         action_frame->u.action.category = WLAN_CATEGORY_HT;
1560         action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
1561         switch (smps) {
1562         case IEEE80211_SMPS_AUTOMATIC:/* 0 */
1563         case IEEE80211_SMPS_NUM_MODES:/* 4 */
1564                 WARN_ON(1);
1565         /* Here will get a 'MISSING_BREAK' in Coverity Test, just ignore it.
1566          * According to Kernel Code, here is right.
1567          */
1568         case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
1569                 action_frame->u.action.u.ht_smps.smps_control =
1570                                 WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
1571                 break;
1572         case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
1573                 action_frame->u.action.u.ht_smps.smps_control =
1574                                 WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
1575                 break;
1576         case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
1577                 action_frame->u.action.u.ht_smps.smps_control =
1578                                 WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
1579                 break;
1580         }
1581
1582         return skb;
1583 }
1584
1585 int stg_rtl_send_smps_action(struct ieee80211_hw *hw,
1586                              struct ieee80211_sta *sta,
1587                              enum ieee80211_smps_mode smps)
1588 {
1589         struct rtl_priv *rtlpriv = rtl_priv(hw);
1590         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1591         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1592         struct sk_buff *skb = NULL;
1593         struct rtl_tcb_desc tcb_desc;
1594         u8 bssid[ETH_ALEN] = {0};
1595
1596         memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
1597
1598         if (rtlpriv->mac80211.act_scanning)
1599                 goto err_free;
1600
1601         if (!sta)
1602                 goto err_free;
1603
1604         if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
1605                 goto err_free;
1606
1607         if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1608                 goto err_free;
1609
1610         if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP)
1611                 ether_addr_copy(bssid, rtlpriv->efuse.dev_addr);
1612         else
1613                 ether_addr_copy(bssid, rtlpriv->mac80211.bssid);
1614
1615         skb = rtl_make_smps_action(hw, smps, sta->addr, bssid);
1616         /* this is a type = mgmt * stype = action frame */
1617         if (skb) {
1618                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1619                 struct rtl_sta_info *sta_entry =
1620                         (struct rtl_sta_info *)sta->drv_priv;
1621                 sta_entry->mimo_ps = smps;
1622                 /* rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0); */
1623
1624                 info->control.rates[0].idx = 0;
1625                 info->band = hw->conf.chandef.chan->band;
1626                 rtlpriv->intf_ops->adapter_tx(hw, sta, skb, &tcb_desc);
1627         }
1628         return 1;
1629
1630 err_free:
1631         return 0;
1632 }
1633 EXPORT_SYMBOL(stg_rtl_send_smps_action);
1634
1635 /* because mac80211 have issues when can receive del ba
1636  * so here we just make a fake del_ba if we receive a ba_req
1637  * but rx_agg was opened to let mac80211 release some ba
1638  * related resources, so please this del_ba for tx */
1639 struct sk_buff *rtl92e_make_del_ba(struct ieee80211_hw *hw,
1640                                    u8 *sa, u8 *bssid, u16 tid)
1641 {
1642         struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1643         struct sk_buff *skb;
1644         struct ieee80211_mgmt *action_frame;
1645         u16 params;
1646
1647         /* 27 = header + category + action + smps mode */
1648         skb = dev_alloc_skb(34 + hw->extra_tx_headroom);
1649         if (!skb)
1650                 return NULL;
1651
1652         skb_reserve(skb, hw->extra_tx_headroom);
1653         action_frame = (void *)skb_put(skb, 34);
1654         memset(action_frame, 0, 34);
1655         ether_addr_copy(action_frame->sa, sa);
1656         ether_addr_copy(action_frame->da, rtlefuse->dev_addr);
1657         ether_addr_copy(action_frame->bssid, bssid);
1658         action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1659                                                   IEEE80211_STYPE_ACTION);
1660         action_frame->u.action.category = WLAN_CATEGORY_BACK;
1661         action_frame->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1662         params = (u16)(1 << 11);        /* bit 11 initiator */
1663         params |= (u16)(tid << 12);     /* bit 15:12 TID number */
1664
1665         action_frame->u.action.u.delba.params = cpu_to_le16(params);
1666         action_frame->u.action.u.delba.reason_code =
1667                 cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
1668
1669         return skb;
1670 }
1671
1672 /*********************************************************
1673  *
1674  * IOT functions
1675  *
1676  *********************************************************/
1677 static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
1678                                   struct octet_string vendor_ie)
1679 {
1680         struct rtl_priv *rtlpriv = rtl_priv(hw);
1681         bool matched = false;
1682         static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
1683         static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
1684         static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
1685         static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
1686         static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
1687         static u8 racap[] = { 0x00, 0x0c, 0x43 };
1688         static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
1689         static u8 marvcap[] = { 0x00, 0x50, 0x43 };
1690
1691         if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
1692             memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
1693                 rtlpriv->mac80211.vendor = PEER_ATH;
1694                 matched = true;
1695         } else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
1696                    memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
1697                    memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
1698                 rtlpriv->mac80211.vendor = PEER_BROAD;
1699                 matched = true;
1700         } else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
1701                 rtlpriv->mac80211.vendor = PEER_RAL;
1702                 matched = true;
1703         } else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
1704                 rtlpriv->mac80211.vendor = PEER_CISCO;
1705                 matched = true;
1706         } else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
1707                 rtlpriv->mac80211.vendor = PEER_MARV;
1708                 matched = true;
1709         }
1710
1711         return matched;
1712 }
1713
1714 static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data, unsigned int len)
1715 {
1716         struct ieee80211_mgmt *mgmt = (void *)data;
1717         struct octet_string vendor_ie;
1718         u8 *pos, *end;
1719
1720         pos = (u8 *)mgmt->u.beacon.variable;
1721         end = data + len;
1722         while (pos < end) {
1723                 if (pos[0] == 221) {
1724                         vendor_ie.length = pos[1];
1725                         vendor_ie.octet = &pos[2];
1726                         if (rtl_chk_vendor_ouisub(hw, vendor_ie))
1727                                 return true;
1728                 }
1729
1730                 if (pos + 2 + pos[1] > end)
1731                         return false;
1732
1733                 pos += 2 + pos[1];
1734         }
1735         return false;
1736 }
1737
1738 void rtl92e_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
1739 {
1740         struct rtl_priv *rtlpriv = rtl_priv(hw);
1741         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1742         struct ieee80211_hdr *hdr = (void *)data;
1743         u32 vendor = PEER_UNKNOWN;
1744
1745         static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
1746         static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
1747         static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
1748         static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
1749         static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
1750         static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
1751         static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
1752         static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
1753         static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
1754         static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
1755         static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
1756         static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
1757         static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
1758         static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
1759         static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
1760         static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
1761
1762         if (mac->opmode != NL80211_IFTYPE_STATION)
1763                 return;
1764
1765         if (mac->link_state == MAC80211_NOLINK) {
1766                 mac->vendor = PEER_UNKNOWN;
1767                 return;
1768         }
1769
1770         if (mac->cnt_after_linked > 2)
1771                 return;
1772
1773         /* check if this really is a beacon */
1774         if (!ieee80211_is_beacon(hdr->frame_control))
1775                 return;
1776
1777         /* min. beacon length + FCS_LEN */
1778         if (len <= 40 + FCS_LEN)
1779                 return;
1780
1781         /* and only beacons from the associated BSSID, please */
1782         if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
1783                 return;
1784
1785         if (rtl_find_221_ie(hw, data, len))
1786                 vendor = mac->vendor;
1787
1788         if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
1789             (memcmp(mac->bssid, ap5_2, 3) == 0) ||
1790             (memcmp(mac->bssid, ap5_3, 3) == 0) ||
1791             (memcmp(mac->bssid, ap5_4, 3) == 0) ||
1792             (memcmp(mac->bssid, ap5_5, 3) == 0) ||
1793             (memcmp(mac->bssid, ap5_6, 3) == 0) ||
1794                 vendor == PEER_ATH) {
1795                 vendor = PEER_ATH;
1796                 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>ath find\n"));
1797         } else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
1798                    (memcmp(mac->bssid, ap4_5, 3) == 0) ||
1799                    (memcmp(mac->bssid, ap4_1, 3) == 0) ||
1800                    (memcmp(mac->bssid, ap4_2, 3) == 0) ||
1801                    (memcmp(mac->bssid, ap4_3, 3) == 0) ||
1802                 vendor == PEER_RAL) {
1803                 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>ral findn\n"));
1804                 vendor = PEER_RAL;
1805         } else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
1806                 vendor == PEER_CISCO) {
1807                 vendor = PEER_CISCO;
1808                 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>cisco find\n"));
1809         } else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
1810                 (memcmp(mac->bssid, ap3_2, 3) == 0) ||
1811                 (memcmp(mac->bssid, ap3_3, 3) == 0) ||
1812                 vendor == PEER_BROAD) {
1813                 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>broad find\n"));
1814                 vendor = PEER_BROAD;
1815         } else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
1816                 vendor == PEER_MARV) {
1817                 vendor = PEER_MARV;
1818                 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>marv find\n"));
1819         }
1820
1821         mac->vendor = vendor;
1822 }
1823
1824 /*********************************************************
1825  *
1826  * sysfs functions
1827  *
1828  *********************************************************/
1829 struct rtl_global_var global_var = {};
1830
1831 int  rtl_core_module_init(void)
1832 {
1833         static int here_once;
1834
1835         if (here_once++)
1836                 return 0;
1837
1838         if (rtl92e_rate_control_register())
1839                 pr_debug("rtl: Unable to register rtl_rc, use default RC !!\n");
1840
1841         /* init some global vars */
1842         INIT_LIST_HEAD(&global_var.glb_priv_list);
1843         spin_lock_init(&global_var.glb_list_lock);
1844
1845         return 0;
1846 }
1847 void  rtl_core_module_exit(void)
1848 {
1849         /*RC*/
1850         rtl92e_rate_control_unregister();
1851 }