GNU Linux-libre 4.4.297-gnu1
[releases.git] / drivers / net / wireless / realtek / rtlwifi / base.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2009-2012  Realtek Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * 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 "wifi.h"
27 #include "rc.h"
28 #include "base.h"
29 #include "efuse.h"
30 #include "cam.h"
31 #include "ps.h"
32 #include "regd.h"
33 #include "pci.h"
34 #include <linux/ip.h>
35 #include <linux/module.h>
36 #include <linux/udp.h>
37
38 /*
39  *NOTICE!!!: This file will be very big, we should
40  *keep it clear under following roles:
41  *
42  *This file include following parts, so, if you add new
43  *functions into this file, please check which part it
44  *should includes. or check if you should add new part
45  *for this file:
46  *
47  *1) mac80211 init functions
48  *2) tx information functions
49  *3) functions called by core.c
50  *4) wq & timer callback functions
51  *5) frame process functions
52  *6) IOT functions
53  *7) sysfs functions
54  *8) vif functions
55  *9) ...
56  */
57
58 /*********************************************************
59  *
60  * mac80211 init functions
61  *
62  *********************************************************/
63 static struct ieee80211_channel rtl_channeltable_2g[] = {
64         {.center_freq = 2412, .hw_value = 1,},
65         {.center_freq = 2417, .hw_value = 2,},
66         {.center_freq = 2422, .hw_value = 3,},
67         {.center_freq = 2427, .hw_value = 4,},
68         {.center_freq = 2432, .hw_value = 5,},
69         {.center_freq = 2437, .hw_value = 6,},
70         {.center_freq = 2442, .hw_value = 7,},
71         {.center_freq = 2447, .hw_value = 8,},
72         {.center_freq = 2452, .hw_value = 9,},
73         {.center_freq = 2457, .hw_value = 10,},
74         {.center_freq = 2462, .hw_value = 11,},
75         {.center_freq = 2467, .hw_value = 12,},
76         {.center_freq = 2472, .hw_value = 13,},
77         {.center_freq = 2484, .hw_value = 14,},
78 };
79
80 static struct ieee80211_channel rtl_channeltable_5g[] = {
81         {.center_freq = 5180, .hw_value = 36,},
82         {.center_freq = 5200, .hw_value = 40,},
83         {.center_freq = 5220, .hw_value = 44,},
84         {.center_freq = 5240, .hw_value = 48,},
85         {.center_freq = 5260, .hw_value = 52,},
86         {.center_freq = 5280, .hw_value = 56,},
87         {.center_freq = 5300, .hw_value = 60,},
88         {.center_freq = 5320, .hw_value = 64,},
89         {.center_freq = 5500, .hw_value = 100,},
90         {.center_freq = 5520, .hw_value = 104,},
91         {.center_freq = 5540, .hw_value = 108,},
92         {.center_freq = 5560, .hw_value = 112,},
93         {.center_freq = 5580, .hw_value = 116,},
94         {.center_freq = 5600, .hw_value = 120,},
95         {.center_freq = 5620, .hw_value = 124,},
96         {.center_freq = 5640, .hw_value = 128,},
97         {.center_freq = 5660, .hw_value = 132,},
98         {.center_freq = 5680, .hw_value = 136,},
99         {.center_freq = 5700, .hw_value = 140,},
100         {.center_freq = 5745, .hw_value = 149,},
101         {.center_freq = 5765, .hw_value = 153,},
102         {.center_freq = 5785, .hw_value = 157,},
103         {.center_freq = 5805, .hw_value = 161,},
104         {.center_freq = 5825, .hw_value = 165,},
105 };
106
107 static struct ieee80211_rate rtl_ratetable_2g[] = {
108         {.bitrate = 10, .hw_value = 0x00,},
109         {.bitrate = 20, .hw_value = 0x01,},
110         {.bitrate = 55, .hw_value = 0x02,},
111         {.bitrate = 110, .hw_value = 0x03,},
112         {.bitrate = 60, .hw_value = 0x04,},
113         {.bitrate = 90, .hw_value = 0x05,},
114         {.bitrate = 120, .hw_value = 0x06,},
115         {.bitrate = 180, .hw_value = 0x07,},
116         {.bitrate = 240, .hw_value = 0x08,},
117         {.bitrate = 360, .hw_value = 0x09,},
118         {.bitrate = 480, .hw_value = 0x0a,},
119         {.bitrate = 540, .hw_value = 0x0b,},
120 };
121
122 static struct ieee80211_rate rtl_ratetable_5g[] = {
123         {.bitrate = 60, .hw_value = 0x04,},
124         {.bitrate = 90, .hw_value = 0x05,},
125         {.bitrate = 120, .hw_value = 0x06,},
126         {.bitrate = 180, .hw_value = 0x07,},
127         {.bitrate = 240, .hw_value = 0x08,},
128         {.bitrate = 360, .hw_value = 0x09,},
129         {.bitrate = 480, .hw_value = 0x0a,},
130         {.bitrate = 540, .hw_value = 0x0b,},
131 };
132
133 static const struct ieee80211_supported_band rtl_band_2ghz = {
134         .band = IEEE80211_BAND_2GHZ,
135
136         .channels = rtl_channeltable_2g,
137         .n_channels = ARRAY_SIZE(rtl_channeltable_2g),
138
139         .bitrates = rtl_ratetable_2g,
140         .n_bitrates = ARRAY_SIZE(rtl_ratetable_2g),
141
142         .ht_cap = {0},
143 };
144
145 static struct ieee80211_supported_band rtl_band_5ghz = {
146         .band = IEEE80211_BAND_5GHZ,
147
148         .channels = rtl_channeltable_5g,
149         .n_channels = ARRAY_SIZE(rtl_channeltable_5g),
150
151         .bitrates = rtl_ratetable_5g,
152         .n_bitrates = ARRAY_SIZE(rtl_ratetable_5g),
153
154         .ht_cap = {0},
155 };
156
157 static const u8 tid_to_ac[] = {
158         2, /* IEEE80211_AC_BE */
159         3, /* IEEE80211_AC_BK */
160         3, /* IEEE80211_AC_BK */
161         2, /* IEEE80211_AC_BE */
162         1, /* IEEE80211_AC_VI */
163         1, /* IEEE80211_AC_VI */
164         0, /* IEEE80211_AC_VO */
165         0, /* IEEE80211_AC_VO */
166 };
167
168 u8 rtl_tid_to_ac(u8 tid)
169 {
170         return tid_to_ac[tid];
171 }
172 EXPORT_SYMBOL_GPL(rtl_tid_to_ac);
173
174 static void _rtl_init_hw_ht_capab(struct ieee80211_hw *hw,
175                                   struct ieee80211_sta_ht_cap *ht_cap)
176 {
177         struct rtl_priv *rtlpriv = rtl_priv(hw);
178         struct rtl_phy *rtlphy = &(rtlpriv->phy);
179
180         ht_cap->ht_supported = true;
181         ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
182             IEEE80211_HT_CAP_SGI_40 |
183             IEEE80211_HT_CAP_SGI_20 |
184             IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU;
185
186         if (rtlpriv->rtlhal.disable_amsdu_8k)
187                 ht_cap->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
188
189         /*
190          *Maximum length of AMPDU that the STA can receive.
191          *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
192          */
193         ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
194
195         /*Minimum MPDU start spacing , */
196         ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
197
198         ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
199
200         /*hw->wiphy->bands[IEEE80211_BAND_2GHZ]
201          *base on ant_num
202          *rx_mask: RX mask
203          *if rx_ant = 1 rx_mask[0]= 0xff;==>MCS0-MCS7
204          *if rx_ant = 2 rx_mask[1]= 0xff;==>MCS8-MCS15
205          *if rx_ant >= 3 rx_mask[2]= 0xff;
206          *if BW_40 rx_mask[4]= 0x01;
207          *highest supported RX rate
208          */
209         if (rtlpriv->dm.supp_phymode_switch) {
210                 RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG,
211                          "Support phy mode switch\n");
212
213                 ht_cap->mcs.rx_mask[0] = 0xFF;
214                 ht_cap->mcs.rx_mask[1] = 0xFF;
215                 ht_cap->mcs.rx_mask[4] = 0x01;
216
217                 ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
218         } else {
219                 if (get_rf_type(rtlphy) == RF_1T2R ||
220                     get_rf_type(rtlphy) == RF_2T2R) {
221                         RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
222                                  "1T2R or 2T2R\n");
223                         ht_cap->mcs.rx_mask[0] = 0xFF;
224                         ht_cap->mcs.rx_mask[1] = 0xFF;
225                         ht_cap->mcs.rx_mask[4] = 0x01;
226
227                         ht_cap->mcs.rx_highest =
228                                  cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
229                 } else if (get_rf_type(rtlphy) == RF_1T1R) {
230                         RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "1T1R\n");
231
232                         ht_cap->mcs.rx_mask[0] = 0xFF;
233                         ht_cap->mcs.rx_mask[1] = 0x00;
234                         ht_cap->mcs.rx_mask[4] = 0x01;
235
236                         ht_cap->mcs.rx_highest =
237                                  cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
238                 }
239         }
240 }
241
242 static void _rtl_init_hw_vht_capab(struct ieee80211_hw *hw,
243                                    struct ieee80211_sta_vht_cap *vht_cap)
244 {
245         struct rtl_priv *rtlpriv = rtl_priv(hw);
246         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
247
248         if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
249                 u16 mcs_map;
250
251                 vht_cap->vht_supported = true;
252                 vht_cap->cap =
253                         IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895 |
254                         IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991 |
255                         IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
256                         IEEE80211_VHT_CAP_SHORT_GI_80 |
257                         IEEE80211_VHT_CAP_TXSTBC |
258                         IEEE80211_VHT_CAP_RXSTBC_1 |
259                         IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
260                         IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
261                         IEEE80211_VHT_CAP_HTC_VHT |
262                         IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
263                         IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
264                         IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
265                         0;
266
267                 mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
268                         IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
269                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 |
270                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
271                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
272                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
273                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
274                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 14;
275
276                 vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
277                 vht_cap->vht_mcs.rx_highest =
278                         cpu_to_le16(MAX_BIT_RATE_SHORT_GI_2NSS_80MHZ_MCS9);
279                 vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
280                 vht_cap->vht_mcs.tx_highest =
281                         cpu_to_le16(MAX_BIT_RATE_SHORT_GI_2NSS_80MHZ_MCS9);
282         } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
283                 u16 mcs_map;
284
285                 vht_cap->vht_supported = true;
286                 vht_cap->cap =
287                         IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895 |
288                         IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991 |
289                         IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
290                         IEEE80211_VHT_CAP_SHORT_GI_80 |
291                         IEEE80211_VHT_CAP_TXSTBC |
292                         IEEE80211_VHT_CAP_RXSTBC_1 |
293                         IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
294                         IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
295                         IEEE80211_VHT_CAP_HTC_VHT |
296                         IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
297                         IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
298                         IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
299                         0;
300
301                 mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
302                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 2 |
303                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 |
304                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
305                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
306                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
307                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
308                         IEEE80211_VHT_MCS_NOT_SUPPORTED << 14;
309
310                 vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
311                 vht_cap->vht_mcs.rx_highest =
312                         cpu_to_le16(MAX_BIT_RATE_SHORT_GI_1NSS_80MHZ_MCS9);
313                 vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
314                 vht_cap->vht_mcs.tx_highest =
315                         cpu_to_le16(MAX_BIT_RATE_SHORT_GI_1NSS_80MHZ_MCS9);
316         }
317 }
318
319 static void _rtl_init_mac80211(struct ieee80211_hw *hw)
320 {
321         struct rtl_priv *rtlpriv = rtl_priv(hw);
322         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
323         struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
324         struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
325         struct ieee80211_supported_band *sband;
326
327         if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY &&
328             rtlhal->bandset == BAND_ON_BOTH) {
329                 /* 1: 2.4 G bands */
330                 /* <1> use  mac->bands as mem for hw->wiphy->bands */
331                 sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
332
333                 /* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
334                  * to default value(1T1R) */
335                 memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]), &rtl_band_2ghz,
336                                 sizeof(struct ieee80211_supported_band));
337
338                 /* <3> init ht cap base on ant_num */
339                 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
340
341                 /* <4> set mac->sband to wiphy->sband */
342                 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
343
344                 /* 2: 5 G bands */
345                 /* <1> use  mac->bands as mem for hw->wiphy->bands */
346                 sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
347
348                 /* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
349                  * to default value(1T1R) */
350                 memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]), &rtl_band_5ghz,
351                                 sizeof(struct ieee80211_supported_band));
352
353                 /* <3> init ht cap base on ant_num */
354                 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
355
356                 _rtl_init_hw_vht_capab(hw, &sband->vht_cap);
357                 /* <4> set mac->sband to wiphy->sband */
358                 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
359         } else {
360                 if (rtlhal->current_bandtype == BAND_ON_2_4G) {
361                         /* <1> use  mac->bands as mem for hw->wiphy->bands */
362                         sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
363
364                         /* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
365                          * to default value(1T1R) */
366                         memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]),
367                                &rtl_band_2ghz,
368                                sizeof(struct ieee80211_supported_band));
369
370                         /* <3> init ht cap base on ant_num */
371                         _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
372
373                         /* <4> set mac->sband to wiphy->sband */
374                         hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
375                 } else if (rtlhal->current_bandtype == BAND_ON_5G) {
376                         /* <1> use  mac->bands as mem for hw->wiphy->bands */
377                         sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
378
379                         /* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
380                          * to default value(1T1R) */
381                         memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]),
382                                &rtl_band_5ghz,
383                                sizeof(struct ieee80211_supported_band));
384
385                         /* <3> init ht cap base on ant_num */
386                         _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
387
388                         _rtl_init_hw_vht_capab(hw, &sband->vht_cap);
389                         /* <4> set mac->sband to wiphy->sband */
390                         hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
391                 } else {
392                         RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG, "Err BAND %d\n",
393                                  rtlhal->current_bandtype);
394                 }
395         }
396         /* <5> set hw caps */
397         ieee80211_hw_set(hw, SIGNAL_DBM);
398         ieee80211_hw_set(hw, RX_INCLUDES_FCS);
399         ieee80211_hw_set(hw, AMPDU_AGGREGATION);
400         ieee80211_hw_set(hw, CONNECTION_MONITOR);
401         ieee80211_hw_set(hw, MFP_CAPABLE);
402         ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
403
404         /* swlps or hwlps has been set in diff chip in init_sw_vars */
405         if (rtlpriv->psc.swctrl_lps) {
406                 ieee80211_hw_set(hw, SUPPORTS_PS);
407                 ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
408         }
409         hw->wiphy->interface_modes =
410             BIT(NL80211_IFTYPE_AP) |
411             BIT(NL80211_IFTYPE_STATION) |
412             BIT(NL80211_IFTYPE_ADHOC) |
413             BIT(NL80211_IFTYPE_MESH_POINT) |
414             BIT(NL80211_IFTYPE_P2P_CLIENT) |
415             BIT(NL80211_IFTYPE_P2P_GO);
416         hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
417
418         hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
419
420         hw->wiphy->rts_threshold = 2347;
421
422         hw->queues = AC_MAX;
423         hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;
424
425         /* TODO: Correct this value for our hw */
426         /* TODO: define these hard code value */
427         hw->max_listen_interval = 10;
428         hw->max_rate_tries = 4;
429         /* hw->max_rates = 1; */
430         hw->sta_data_size = sizeof(struct rtl_sta_info);
431
432 /* wowlan is not supported by kernel if CONFIG_PM is not defined */
433 #ifdef CONFIG_PM
434         if (rtlpriv->psc.wo_wlan_mode) {
435                 if (rtlpriv->psc.wo_wlan_mode & WAKE_ON_MAGIC_PACKET)
436                         rtlpriv->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT;
437                 if (rtlpriv->psc.wo_wlan_mode & WAKE_ON_PATTERN_MATCH) {
438                         rtlpriv->wowlan.n_patterns =
439                                 MAX_SUPPORT_WOL_PATTERN_NUM;
440                         rtlpriv->wowlan.pattern_min_len = MIN_WOL_PATTERN_SIZE;
441                         rtlpriv->wowlan.pattern_max_len = MAX_WOL_PATTERN_SIZE;
442                 }
443                 hw->wiphy->wowlan = &rtlpriv->wowlan;
444         }
445 #endif
446
447         /* <6> mac address */
448         if (is_valid_ether_addr(rtlefuse->dev_addr)) {
449                 SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
450         } else {
451                 u8 rtlmac1[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
452                 get_random_bytes((rtlmac1 + (ETH_ALEN - 1)), 1);
453                 SET_IEEE80211_PERM_ADDR(hw, rtlmac1);
454         }
455 }
456
457 static int _rtl_init_deferred_work(struct ieee80211_hw *hw)
458 {
459         struct rtl_priv *rtlpriv = rtl_priv(hw);
460         struct workqueue_struct *wq;
461
462         wq = alloc_workqueue("%s", 0, 0, rtlpriv->cfg->name);
463         if (!wq)
464                 return -ENOMEM;
465
466         /* <1> timer */
467         setup_timer(&rtlpriv->works.watchdog_timer,
468                     rtl_watch_dog_timer_callback, (unsigned long)hw);
469         setup_timer(&rtlpriv->works.dualmac_easyconcurrent_retrytimer,
470                     rtl_easy_concurrent_retrytimer_callback, (unsigned long)hw);
471         /* <2> work queue */
472         rtlpriv->works.hw = hw;
473         rtlpriv->works.rtl_wq = wq;
474
475         INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
476                           (void *)rtl_watchdog_wq_callback);
477         INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
478                           (void *)rtl_ips_nic_off_wq_callback);
479         INIT_DELAYED_WORK(&rtlpriv->works.ps_work,
480                           (void *)rtl_swlps_wq_callback);
481         INIT_DELAYED_WORK(&rtlpriv->works.ps_rfon_wq,
482                           (void *)rtl_swlps_rfon_wq_callback);
483         INIT_DELAYED_WORK(&rtlpriv->works.fwevt_wq,
484                           (void *)rtl_fwevt_wq_callback);
485         return 0;
486 }
487
488 void rtl_deinit_deferred_work(struct ieee80211_hw *hw)
489 {
490         struct rtl_priv *rtlpriv = rtl_priv(hw);
491
492         del_timer_sync(&rtlpriv->works.watchdog_timer);
493
494         cancel_delayed_work(&rtlpriv->works.watchdog_wq);
495         cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
496         cancel_delayed_work(&rtlpriv->works.ps_work);
497         cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
498         cancel_delayed_work(&rtlpriv->works.fwevt_wq);
499 }
500 EXPORT_SYMBOL_GPL(rtl_deinit_deferred_work);
501
502 void rtl_init_rfkill(struct ieee80211_hw *hw)
503 {
504         struct rtl_priv *rtlpriv = rtl_priv(hw);
505
506         bool radio_state;
507         bool blocked;
508         u8 valid = 0;
509
510         /*set init state to on */
511         rtlpriv->rfkill.rfkill_state = true;
512         wiphy_rfkill_set_hw_state(hw->wiphy, 0);
513
514         radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
515
516         if (valid) {
517                 pr_info("rtlwifi: wireless switch is %s\n",
518                         rtlpriv->rfkill.rfkill_state ? "on" : "off");
519
520                 rtlpriv->rfkill.rfkill_state = radio_state;
521
522                 blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
523                 wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
524         }
525
526         wiphy_rfkill_start_polling(hw->wiphy);
527 }
528 EXPORT_SYMBOL(rtl_init_rfkill);
529
530 void rtl_deinit_rfkill(struct ieee80211_hw *hw)
531 {
532         wiphy_rfkill_stop_polling(hw->wiphy);
533 }
534 EXPORT_SYMBOL_GPL(rtl_deinit_rfkill);
535
536 int rtl_init_core(struct ieee80211_hw *hw)
537 {
538         struct rtl_priv *rtlpriv = rtl_priv(hw);
539         struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
540
541         /* <1> init mac80211 */
542         _rtl_init_mac80211(hw);
543         rtlmac->hw = hw;
544
545         /* <2> rate control register */
546         hw->rate_control_algorithm = "rtl_rc";
547
548         /*
549          * <3> init CRDA must come after init
550          * mac80211 hw  in _rtl_init_mac80211.
551          */
552         if (rtl_regd_init(hw, rtl_reg_notifier)) {
553                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "REGD init failed\n");
554                 return 1;
555         }
556
557         /* <4> locks */
558         mutex_init(&rtlpriv->locks.conf_mutex);
559         spin_lock_init(&rtlpriv->locks.ips_lock);
560         spin_lock_init(&rtlpriv->locks.irq_th_lock);
561         spin_lock_init(&rtlpriv->locks.h2c_lock);
562         spin_lock_init(&rtlpriv->locks.rf_ps_lock);
563         spin_lock_init(&rtlpriv->locks.rf_lock);
564         spin_lock_init(&rtlpriv->locks.waitq_lock);
565         spin_lock_init(&rtlpriv->locks.entry_list_lock);
566         spin_lock_init(&rtlpriv->locks.cck_and_rw_pagea_lock);
567         spin_lock_init(&rtlpriv->locks.check_sendpkt_lock);
568         spin_lock_init(&rtlpriv->locks.fw_ps_lock);
569         spin_lock_init(&rtlpriv->locks.lps_lock);
570         spin_lock_init(&rtlpriv->locks.iqk_lock);
571         /* <5> init list */
572         INIT_LIST_HEAD(&rtlpriv->entry_list);
573
574         rtlmac->link_state = MAC80211_NOLINK;
575
576         /* <6> init deferred work */
577         return _rtl_init_deferred_work(hw);
578 }
579 EXPORT_SYMBOL_GPL(rtl_init_core);
580
581 void rtl_deinit_core(struct ieee80211_hw *hw)
582 {
583 }
584 EXPORT_SYMBOL_GPL(rtl_deinit_core);
585
586 void rtl_init_rx_config(struct ieee80211_hw *hw)
587 {
588         struct rtl_priv *rtlpriv = rtl_priv(hw);
589         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
590
591         rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
592 }
593 EXPORT_SYMBOL_GPL(rtl_init_rx_config);
594
595 /*********************************************************
596  *
597  * tx information functions
598  *
599  *********************************************************/
600 static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
601                                           struct rtl_tcb_desc *tcb_desc,
602                                           struct ieee80211_tx_info *info)
603 {
604         struct rtl_priv *rtlpriv = rtl_priv(hw);
605         u8 rate_flag = info->control.rates[0].flags;
606
607         tcb_desc->use_shortpreamble = false;
608
609         /* 1M can only use Long Preamble. 11B spec */
610         if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
611                 return;
612         else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
613                 tcb_desc->use_shortpreamble = true;
614
615         return;
616 }
617
618 static void _rtl_query_shortgi(struct ieee80211_hw *hw,
619                                struct ieee80211_sta *sta,
620                                struct rtl_tcb_desc *tcb_desc,
621                                struct ieee80211_tx_info *info)
622 {
623         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
624         u8 rate_flag = info->control.rates[0].flags;
625         u8 sgi_40 = 0, sgi_20 = 0, bw_40 = 0;
626         u8 sgi_80 = 0, bw_80 = 0;
627         tcb_desc->use_shortgi = false;
628
629         if (sta == NULL)
630                 return;
631
632         sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
633         sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
634         sgi_80 = sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80;
635
636         if ((!sta->ht_cap.ht_supported) && (!sta->vht_cap.vht_supported))
637                 return;
638
639         if (!sgi_40 && !sgi_20)
640                 return;
641
642         if (mac->opmode == NL80211_IFTYPE_STATION) {
643                 bw_40 = mac->bw_40;
644                 bw_80 = mac->bw_80;
645         } else if (mac->opmode == NL80211_IFTYPE_AP ||
646                  mac->opmode == NL80211_IFTYPE_ADHOC ||
647                  mac->opmode == NL80211_IFTYPE_MESH_POINT) {
648                 bw_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
649                 bw_80 = sta->vht_cap.vht_supported;
650         }
651
652         if (bw_80) {
653                 if (sgi_80)
654                         tcb_desc->use_shortgi = true;
655                 else
656                         tcb_desc->use_shortgi = false;
657         } else {
658                 if (bw_40 && sgi_40)
659                         tcb_desc->use_shortgi = true;
660                 else if (!bw_40 && sgi_20)
661                         tcb_desc->use_shortgi = true;
662         }
663
664         if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
665                 tcb_desc->use_shortgi = false;
666 }
667
668 static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
669                                        struct rtl_tcb_desc *tcb_desc,
670                                        struct ieee80211_tx_info *info)
671 {
672         struct rtl_priv *rtlpriv = rtl_priv(hw);
673         u8 rate_flag = info->control.rates[0].flags;
674
675         /* Common Settings */
676         tcb_desc->rts_stbc = false;
677         tcb_desc->cts_enable = false;
678         tcb_desc->rts_sc = 0;
679         tcb_desc->rts_bw = false;
680         tcb_desc->rts_use_shortpreamble = false;
681         tcb_desc->rts_use_shortgi = false;
682
683         if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
684                 /* Use CTS-to-SELF in protection mode. */
685                 tcb_desc->rts_enable = true;
686                 tcb_desc->cts_enable = true;
687                 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
688         } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
689                 /* Use RTS-CTS in protection mode. */
690                 tcb_desc->rts_enable = true;
691                 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
692         }
693 }
694
695 static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
696                                    struct ieee80211_sta *sta,
697                                    struct rtl_tcb_desc *tcb_desc)
698 {
699         struct rtl_priv *rtlpriv = rtl_priv(hw);
700         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
701         struct rtl_sta_info *sta_entry = NULL;
702         u8 ratr_index = 7;
703
704         if (sta) {
705                 sta_entry = (struct rtl_sta_info *) sta->drv_priv;
706                 ratr_index = sta_entry->ratr_index;
707         }
708         if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
709                 if (mac->opmode == NL80211_IFTYPE_STATION) {
710                         tcb_desc->ratr_index = 0;
711                 } else if (mac->opmode == NL80211_IFTYPE_ADHOC ||
712                                 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
713                         if (tcb_desc->multicast || tcb_desc->broadcast) {
714                                 tcb_desc->hw_rate =
715                                     rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
716                                 tcb_desc->use_driver_rate = 1;
717                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
718                         } else {
719                                 tcb_desc->ratr_index = ratr_index;
720                         }
721                 } else if (mac->opmode == NL80211_IFTYPE_AP) {
722                         tcb_desc->ratr_index = ratr_index;
723                 }
724         }
725
726         if (rtlpriv->dm.useramask) {
727                 tcb_desc->ratr_index = ratr_index;
728                 /* TODO we will differentiate adhoc and station future  */
729                 if (mac->opmode == NL80211_IFTYPE_STATION ||
730                     mac->opmode == NL80211_IFTYPE_MESH_POINT) {
731                         tcb_desc->mac_id = 0;
732
733                         if (mac->mode == WIRELESS_MODE_AC_5G)
734                                 tcb_desc->ratr_index =
735                                         RATR_INX_WIRELESS_AC_5N;
736                         else if (mac->mode == WIRELESS_MODE_AC_24G)
737                                 tcb_desc->ratr_index =
738                                         RATR_INX_WIRELESS_AC_24N;
739                         else if (mac->mode == WIRELESS_MODE_N_24G)
740                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
741                         else if (mac->mode == WIRELESS_MODE_N_5G)
742                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_NG;
743                         else if (mac->mode & WIRELESS_MODE_G)
744                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
745                         else if (mac->mode & WIRELESS_MODE_B)
746                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
747                         else if (mac->mode & WIRELESS_MODE_A)
748                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_G;
749
750                 } else if (mac->opmode == NL80211_IFTYPE_AP ||
751                         mac->opmode == NL80211_IFTYPE_ADHOC) {
752                         if (NULL != sta) {
753                                 if (sta->aid > 0)
754                                         tcb_desc->mac_id = sta->aid + 1;
755                                 else
756                                         tcb_desc->mac_id = 1;
757                         } else {
758                                 tcb_desc->mac_id = 0;
759                         }
760                 }
761         }
762 }
763
764 static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
765                                       struct ieee80211_sta *sta,
766                                       struct rtl_tcb_desc *tcb_desc)
767 {
768         struct rtl_priv *rtlpriv = rtl_priv(hw);
769         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
770
771         tcb_desc->packet_bw = false;
772         if (!sta)
773                 return;
774         if (mac->opmode == NL80211_IFTYPE_AP ||
775             mac->opmode == NL80211_IFTYPE_ADHOC ||
776             mac->opmode == NL80211_IFTYPE_MESH_POINT) {
777                 if (!(sta->ht_cap.ht_supported) ||
778                     !(sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
779                         return;
780         } else if (mac->opmode == NL80211_IFTYPE_STATION) {
781                 if (!mac->bw_40 || !(sta->ht_cap.ht_supported))
782                         return;
783         }
784         if (tcb_desc->multicast || tcb_desc->broadcast)
785                 return;
786
787         /*use legency rate, shall use 20MHz */
788         if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
789                 return;
790
791         tcb_desc->packet_bw = HT_CHANNEL_WIDTH_20_40;
792
793         if (rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8812AE ||
794             rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8821AE) {
795                 if (mac->opmode == NL80211_IFTYPE_AP ||
796                     mac->opmode == NL80211_IFTYPE_ADHOC ||
797                     mac->opmode == NL80211_IFTYPE_MESH_POINT) {
798                         if (!(sta->vht_cap.vht_supported))
799                                 return;
800                 } else if (mac->opmode == NL80211_IFTYPE_STATION) {
801                         if (!mac->bw_80 ||
802                             !(sta->vht_cap.vht_supported))
803                                 return;
804                 }
805                 if (tcb_desc->hw_rate <=
806                         rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15])
807                         return;
808                 tcb_desc->packet_bw = HT_CHANNEL_WIDTH_80;
809         }
810 }
811
812 static u8 _rtl_get_vht_highest_n_rate(struct ieee80211_hw *hw,
813                                       struct ieee80211_sta *sta)
814 {
815         struct rtl_priv *rtlpriv = rtl_priv(hw);
816         struct rtl_phy *rtlphy = &(rtlpriv->phy);
817         u8 hw_rate;
818         u16 tx_mcs_map = le16_to_cpu(sta->vht_cap.vht_mcs.tx_mcs_map);
819
820         if ((get_rf_type(rtlphy) == RF_2T2R) &&
821             (tx_mcs_map & 0x000c) != 0x000c) {
822                 if ((tx_mcs_map & 0x000c) >> 2 ==
823                         IEEE80211_VHT_MCS_SUPPORT_0_7)
824                         hw_rate =
825                         rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS7];
826                 else if ((tx_mcs_map  & 0x000c) >> 2 ==
827                         IEEE80211_VHT_MCS_SUPPORT_0_8)
828                         hw_rate =
829                         rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS9];
830                 else
831                         hw_rate =
832                         rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS9];
833         } else {
834                 if ((tx_mcs_map  & 0x0003) ==
835                         IEEE80211_VHT_MCS_SUPPORT_0_7)
836                         hw_rate =
837                         rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS7];
838                 else if ((tx_mcs_map  & 0x0003) ==
839                         IEEE80211_VHT_MCS_SUPPORT_0_8)
840                         hw_rate =
841                         rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS9];
842                 else
843                         hw_rate =
844                         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 /* mac80211's rate_idx is like this:
867  *
868  * 2.4G band:rx_status->band == IEEE80211_BAND_2GHZ
869  *
870  * B/G rate:
871  * (rx_status->flag & RX_FLAG_HT) = 0,
872  * DESC_RATE1M-->DESC_RATE54M ==> idx is 0-->11,
873  *
874  * N rate:
875  * (rx_status->flag & RX_FLAG_HT) = 1,
876  * DESC_RATEMCS0-->DESC_RATEMCS15 ==> idx is 0-->15
877  *
878  * 5G band:rx_status->band == IEEE80211_BAND_5GHZ
879  * A rate:
880  * (rx_status->flag & RX_FLAG_HT) = 0,
881  * DESC_RATE6M-->DESC_RATE54M ==> idx is 0-->7,
882  *
883  * N rate:
884  * (rx_status->flag & RX_FLAG_HT) = 1,
885  * DESC_RATEMCS0-->DESC_RATEMCS15 ==> idx is 0-->15
886  *
887  * VHT rates:
888  * DESC_RATEVHT1SS_MCS0-->DESC_RATEVHT1SS_MCS9 ==> idx is 0-->9
889  * DESC_RATEVHT2SS_MCS0-->DESC_RATEVHT2SS_MCS9 ==> idx is 0-->9
890  */
891 int rtlwifi_rate_mapping(struct ieee80211_hw *hw, bool isht, bool isvht,
892                          u8 desc_rate)
893 {
894         int rate_idx;
895
896         if (isvht) {
897                 switch (desc_rate) {
898                 case DESC_RATEVHT1SS_MCS0:
899                         rate_idx = 0;
900                         break;
901                 case DESC_RATEVHT1SS_MCS1:
902                         rate_idx = 1;
903                         break;
904                 case DESC_RATEVHT1SS_MCS2:
905                         rate_idx = 2;
906                         break;
907                 case DESC_RATEVHT1SS_MCS3:
908                         rate_idx = 3;
909                         break;
910                 case DESC_RATEVHT1SS_MCS4:
911                         rate_idx = 4;
912                         break;
913                 case DESC_RATEVHT1SS_MCS5:
914                         rate_idx = 5;
915                         break;
916                 case DESC_RATEVHT1SS_MCS6:
917                         rate_idx = 6;
918                         break;
919                 case DESC_RATEVHT1SS_MCS7:
920                         rate_idx = 7;
921                         break;
922                 case DESC_RATEVHT1SS_MCS8:
923                         rate_idx = 8;
924                         break;
925                 case DESC_RATEVHT1SS_MCS9:
926                         rate_idx = 9;
927                         break;
928                 case DESC_RATEVHT2SS_MCS0:
929                         rate_idx = 0;
930                         break;
931                 case DESC_RATEVHT2SS_MCS1:
932                         rate_idx = 1;
933                         break;
934                 case DESC_RATEVHT2SS_MCS2:
935                         rate_idx = 2;
936                         break;
937                 case DESC_RATEVHT2SS_MCS3:
938                         rate_idx = 3;
939                         break;
940                 case DESC_RATEVHT2SS_MCS4:
941                         rate_idx = 4;
942                         break;
943                 case DESC_RATEVHT2SS_MCS5:
944                         rate_idx = 5;
945                         break;
946                 case DESC_RATEVHT2SS_MCS6:
947                         rate_idx = 6;
948                         break;
949                 case DESC_RATEVHT2SS_MCS7:
950                         rate_idx = 7;
951                         break;
952                 case DESC_RATEVHT2SS_MCS8:
953                         rate_idx = 8;
954                         break;
955                 case DESC_RATEVHT2SS_MCS9:
956                         rate_idx = 9;
957                         break;
958                 default:
959                         rate_idx = 0;
960                         break;
961                 }
962                 return rate_idx;
963         }
964         if (false == isht) {
965                 if (IEEE80211_BAND_2GHZ == hw->conf.chandef.chan->band) {
966                         switch (desc_rate) {
967                         case DESC_RATE1M:
968                                 rate_idx = 0;
969                                 break;
970                         case DESC_RATE2M:
971                                 rate_idx = 1;
972                                 break;
973                         case DESC_RATE5_5M:
974                                 rate_idx = 2;
975                                 break;
976                         case DESC_RATE11M:
977                                 rate_idx = 3;
978                                 break;
979                         case DESC_RATE6M:
980                                 rate_idx = 4;
981                                 break;
982                         case DESC_RATE9M:
983                                 rate_idx = 5;
984                                 break;
985                         case DESC_RATE12M:
986                                 rate_idx = 6;
987                                 break;
988                         case DESC_RATE18M:
989                                 rate_idx = 7;
990                                 break;
991                         case DESC_RATE24M:
992                                 rate_idx = 8;
993                                 break;
994                         case DESC_RATE36M:
995                                 rate_idx = 9;
996                                 break;
997                         case DESC_RATE48M:
998                                 rate_idx = 10;
999                                 break;
1000                         case DESC_RATE54M:
1001                                 rate_idx = 11;
1002                                 break;
1003                         default:
1004                                 rate_idx = 0;
1005                                 break;
1006                         }
1007                 } else {
1008                         switch (desc_rate) {
1009                         case DESC_RATE6M:
1010                                 rate_idx = 0;
1011                                 break;
1012                         case DESC_RATE9M:
1013                                 rate_idx = 1;
1014                                 break;
1015                         case DESC_RATE12M:
1016                                 rate_idx = 2;
1017                                 break;
1018                         case DESC_RATE18M:
1019                                 rate_idx = 3;
1020                                 break;
1021                         case DESC_RATE24M:
1022                                 rate_idx = 4;
1023                                 break;
1024                         case DESC_RATE36M:
1025                                 rate_idx = 5;
1026                                 break;
1027                         case DESC_RATE48M:
1028                                 rate_idx = 6;
1029                                 break;
1030                         case DESC_RATE54M:
1031                                 rate_idx = 7;
1032                                 break;
1033                         default:
1034                                 rate_idx = 0;
1035                                 break;
1036                         }
1037                 }
1038         } else {
1039                 switch (desc_rate) {
1040                 case DESC_RATEMCS0:
1041                         rate_idx = 0;
1042                         break;
1043                 case DESC_RATEMCS1:
1044                         rate_idx = 1;
1045                         break;
1046                 case DESC_RATEMCS2:
1047                         rate_idx = 2;
1048                         break;
1049                 case DESC_RATEMCS3:
1050                         rate_idx = 3;
1051                         break;
1052                 case DESC_RATEMCS4:
1053                         rate_idx = 4;
1054                         break;
1055                 case DESC_RATEMCS5:
1056                         rate_idx = 5;
1057                         break;
1058                 case DESC_RATEMCS6:
1059                         rate_idx = 6;
1060                         break;
1061                 case DESC_RATEMCS7:
1062                         rate_idx = 7;
1063                         break;
1064                 case DESC_RATEMCS8:
1065                         rate_idx = 8;
1066                         break;
1067                 case DESC_RATEMCS9:
1068                         rate_idx = 9;
1069                         break;
1070                 case DESC_RATEMCS10:
1071                         rate_idx = 10;
1072                         break;
1073                 case DESC_RATEMCS11:
1074                         rate_idx = 11;
1075                         break;
1076                 case DESC_RATEMCS12:
1077                         rate_idx = 12;
1078                         break;
1079                 case DESC_RATEMCS13:
1080                         rate_idx = 13;
1081                         break;
1082                 case DESC_RATEMCS14:
1083                         rate_idx = 14;
1084                         break;
1085                 case DESC_RATEMCS15:
1086                         rate_idx = 15;
1087                         break;
1088                 default:
1089                         rate_idx = 0;
1090                         break;
1091                 }
1092         }
1093         return rate_idx;
1094 }
1095 EXPORT_SYMBOL(rtlwifi_rate_mapping);
1096
1097 void rtl_get_tcb_desc(struct ieee80211_hw *hw,
1098                       struct ieee80211_tx_info *info,
1099                       struct ieee80211_sta *sta,
1100                       struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
1101 {
1102         struct rtl_priv *rtlpriv = rtl_priv(hw);
1103         struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
1104         struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
1105         struct ieee80211_rate *txrate;
1106         __le16 fc = rtl_get_fc(skb);
1107
1108         txrate = ieee80211_get_tx_rate(hw, info);
1109         if (txrate)
1110                 tcb_desc->hw_rate = txrate->hw_value;
1111
1112         if (ieee80211_is_data(fc)) {
1113                 /*
1114                  *we set data rate INX 0
1115                  *in rtl_rc.c   if skb is special data or
1116                  *mgt which need low data rate.
1117                  */
1118
1119                 /*
1120                  *So tcb_desc->hw_rate is just used for
1121                  *special data and mgt frames
1122                  */
1123                 if (info->control.rates[0].idx == 0 ||
1124                                 ieee80211_is_nullfunc(fc)) {
1125                         tcb_desc->use_driver_rate = true;
1126                         tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
1127
1128                         tcb_desc->disable_ratefallback = 1;
1129                 } else {
1130                         /*
1131                          *because hw will nerver use hw_rate
1132                          *when tcb_desc->use_driver_rate = false
1133                          *so we never set highest N rate here,
1134                          *and N rate will all be controlled by FW
1135                          *when tcb_desc->use_driver_rate = false
1136                          */
1137                         if (sta && sta->vht_cap.vht_supported) {
1138                                 tcb_desc->hw_rate =
1139                                 _rtl_get_vht_highest_n_rate(hw, sta);
1140                         } else {
1141                                 if (sta && (sta->ht_cap.ht_supported)) {
1142                                         tcb_desc->hw_rate =
1143                                                 _rtl_get_highest_n_rate(hw, sta);
1144                                 } else {
1145                                         if (rtlmac->mode == WIRELESS_MODE_B) {
1146                                                 tcb_desc->hw_rate =
1147                                                     rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
1148                                         } else {
1149                                                 tcb_desc->hw_rate =
1150                                                     rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
1151                                         }
1152                                 }
1153                         }
1154                 }
1155
1156                 if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
1157                         tcb_desc->multicast = 1;
1158                 else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
1159                         tcb_desc->broadcast = 1;
1160
1161                 _rtl_txrate_selectmode(hw, sta, tcb_desc);
1162                 _rtl_query_bandwidth_mode(hw, sta, tcb_desc);
1163                 _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
1164                 _rtl_query_shortgi(hw, sta, tcb_desc, info);
1165                 _rtl_query_protection_mode(hw, tcb_desc, info);
1166         } else {
1167                 tcb_desc->use_driver_rate = true;
1168                 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
1169                 tcb_desc->disable_ratefallback = 1;
1170                 tcb_desc->mac_id = 0;
1171                 tcb_desc->packet_bw = false;
1172         }
1173 }
1174 EXPORT_SYMBOL(rtl_get_tcb_desc);
1175
1176 bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
1177 {
1178         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1179         struct rtl_priv *rtlpriv = rtl_priv(hw);
1180         __le16 fc = rtl_get_fc(skb);
1181
1182         if (rtlpriv->dm.supp_phymode_switch &&
1183             mac->link_state < MAC80211_LINKED &&
1184             (ieee80211_is_auth(fc) || ieee80211_is_probe_req(fc))) {
1185                 if (rtlpriv->cfg->ops->chk_switch_dmdp)
1186                         rtlpriv->cfg->ops->chk_switch_dmdp(hw);
1187         }
1188         if (ieee80211_is_auth(fc)) {
1189                 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
1190                 rtl_ips_nic_on(hw);
1191
1192                 mac->link_state = MAC80211_LINKING;
1193                 /* Dul mac */
1194                 rtlpriv->phy.need_iqk = true;
1195
1196         }
1197
1198         return true;
1199 }
1200 EXPORT_SYMBOL_GPL(rtl_tx_mgmt_proc);
1201
1202 struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw, u8 *sa,
1203                                 u8 *bssid, u16 tid);
1204
1205 static void process_agg_start(struct ieee80211_hw *hw,
1206                               struct ieee80211_hdr *hdr, u16 tid)
1207 {
1208         struct rtl_priv *rtlpriv = rtl_priv(hw);
1209         struct ieee80211_rx_status rx_status = { 0 };
1210         struct sk_buff *skb_delba = NULL;
1211
1212         skb_delba = rtl_make_del_ba(hw, hdr->addr2, hdr->addr3, tid);
1213         if (skb_delba) {
1214                 rx_status.freq = hw->conf.chandef.chan->center_freq;
1215                 rx_status.band = hw->conf.chandef.chan->band;
1216                 rx_status.flag |= RX_FLAG_DECRYPTED;
1217                 rx_status.flag |= RX_FLAG_MACTIME_START;
1218                 rx_status.rate_idx = 0;
1219                 rx_status.signal = 50 + 10;
1220                 memcpy(IEEE80211_SKB_RXCB(skb_delba),
1221                        &rx_status, sizeof(rx_status));
1222                 RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG,
1223                               "fake del\n",
1224                               skb_delba->data,
1225                               skb_delba->len);
1226                 ieee80211_rx_irqsafe(hw, skb_delba);
1227         }
1228 }
1229
1230 bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
1231 {
1232         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1233         struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
1234         struct rtl_priv *rtlpriv = rtl_priv(hw);
1235         __le16 fc = rtl_get_fc(skb);
1236         u8 *act = (u8 *)(((u8 *)skb->data + MAC80211_3ADDR_LEN));
1237         u8 category;
1238
1239         if (!ieee80211_is_action(fc))
1240                 return true;
1241
1242         category = *act;
1243         act++;
1244         switch (category) {
1245         case ACT_CAT_BA:
1246                 switch (*act) {
1247                 case ACT_ADDBAREQ:
1248                         if (mac->act_scanning)
1249                                 return false;
1250
1251                         RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1252                                 "%s ACT_ADDBAREQ From :%pM\n",
1253                                 is_tx ? "Tx" : "Rx", hdr->addr2);
1254                         RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG, "req\n",
1255                                 skb->data, skb->len);
1256                         if (!is_tx) {
1257                                 struct ieee80211_sta *sta = NULL;
1258                                 struct rtl_sta_info *sta_entry = NULL;
1259                                 struct rtl_tid_data *tid_data;
1260                                 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1261                                 u16 capab = 0, tid = 0;
1262
1263                                 rcu_read_lock();
1264                                 sta = rtl_find_sta(hw, hdr->addr3);
1265                                 if (sta == NULL) {
1266                                         RT_TRACE(rtlpriv, COMP_SEND | COMP_RECV,
1267                                                  DBG_DMESG, "sta is NULL\n");
1268                                         rcu_read_unlock();
1269                                         return true;
1270                                 }
1271
1272                                 sta_entry =
1273                                         (struct rtl_sta_info *)sta->drv_priv;
1274                                 if (!sta_entry) {
1275                                         rcu_read_unlock();
1276                                         return true;
1277                                 }
1278                                 capab =
1279                                   le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1280                                 tid = (capab &
1281                                        IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1282                                 tid_data = &sta_entry->tids[tid];
1283                                 if (tid_data->agg.rx_agg_state ==
1284                                     RTL_RX_AGG_START)
1285                                         process_agg_start(hw, hdr, tid);
1286                                 rcu_read_unlock();
1287                         }
1288                         break;
1289                 case ACT_ADDBARSP:
1290                         RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1291                                  "%s ACT_ADDBARSP From :%pM\n",
1292                                   is_tx ? "Tx" : "Rx", hdr->addr2);
1293                         break;
1294                 case ACT_DELBA:
1295                         RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1296                                  "ACT_ADDBADEL From :%pM\n", hdr->addr2);
1297                         break;
1298                 }
1299                 break;
1300         default:
1301                 break;
1302         }
1303
1304         return true;
1305 }
1306 EXPORT_SYMBOL_GPL(rtl_action_proc);
1307
1308 static void setup_arp_tx(struct rtl_priv *rtlpriv, struct rtl_ps_ctl *ppsc)
1309 {
1310         struct ieee80211_hw *hw = rtlpriv->hw;
1311
1312         rtlpriv->ra.is_special_data = true;
1313         if (rtlpriv->cfg->ops->get_btc_status())
1314                 rtlpriv->btcoexist.btc_ops->btc_special_packet_notify(
1315                                         rtlpriv, 1);
1316         rtl_lps_leave(hw);
1317         ppsc->last_delaylps_stamp_jiffies = jiffies;
1318 }
1319
1320 /*should call before software enc*/
1321 u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx,
1322                        bool is_enc)
1323 {
1324         struct rtl_priv *rtlpriv = rtl_priv(hw);
1325         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1326         __le16 fc = rtl_get_fc(skb);
1327         u16 ether_type;
1328         u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
1329         u8 encrypt_header_len = 0;
1330         u8 offset;
1331         const struct iphdr *ip;
1332
1333         if (!ieee80211_is_data(fc))
1334                 goto end;
1335
1336         switch (rtlpriv->sec.pairwise_enc_algorithm) {
1337         case WEP40_ENCRYPTION:
1338         case WEP104_ENCRYPTION:
1339                 encrypt_header_len = 4;/*WEP_IV_LEN*/
1340                 break;
1341         case TKIP_ENCRYPTION:
1342                 encrypt_header_len = 8;/*TKIP_IV_LEN*/
1343                 break;
1344         case AESCCMP_ENCRYPTION:
1345                 encrypt_header_len = 8;/*CCMP_HDR_LEN;*/
1346                 break;
1347         default:
1348                 break;
1349         }
1350
1351         offset = mac_hdr_len + SNAP_SIZE;
1352         if (is_enc)
1353                 offset += encrypt_header_len;
1354         ether_type = be16_to_cpup((__be16 *)(skb->data + offset));
1355
1356         if (ETH_P_IP == ether_type) {
1357                 ip = (struct iphdr *)((u8 *)skb->data + offset +
1358                      PROTOC_TYPE_SIZE);
1359                 if (IPPROTO_UDP == ip->protocol) {
1360                         struct udphdr *udp = (struct udphdr *)((u8 *)ip +
1361                                                                (ip->ihl << 2));
1362                         if (((((u8 *)udp)[1] == 68) &&
1363                              (((u8 *)udp)[3] == 67)) ||
1364                             ((((u8 *)udp)[1] == 67) &&
1365                              (((u8 *)udp)[3] == 68))) {
1366                                 /* 68 : UDP BOOTP client
1367                                  * 67 : UDP BOOTP server
1368                                  */
1369                                 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV),
1370                                          DBG_DMESG, "dhcp %s !!\n",
1371                                          (is_tx) ? "Tx" : "Rx");
1372
1373                                 if (is_tx)
1374                                         setup_arp_tx(rtlpriv, ppsc);
1375                                 return true;
1376                         }
1377                 }
1378         } else if (ETH_P_ARP == ether_type) {
1379                 if (is_tx)
1380                         setup_arp_tx(rtlpriv, ppsc);
1381
1382                 return true;
1383         } else if (ETH_P_PAE == ether_type) {
1384                 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1385                          "802.1X %s EAPOL pkt!!\n", (is_tx) ? "Tx" : "Rx");
1386
1387                 if (is_tx) {
1388                         rtlpriv->ra.is_special_data = true;
1389                         rtl_lps_leave(hw);
1390                         ppsc->last_delaylps_stamp_jiffies = jiffies;
1391                 }
1392
1393                 return true;
1394         } else if (ETH_P_IPV6 == ether_type) {
1395                 /* TODO: Handle any IPv6 cases that need special handling.
1396                  * For now, always return false
1397                  */
1398                 goto end;
1399         }
1400
1401 end:
1402         rtlpriv->ra.is_special_data = false;
1403         return false;
1404 }
1405 EXPORT_SYMBOL_GPL(rtl_is_special_data);
1406
1407 /*********************************************************
1408  *
1409  * functions called by core.c
1410  *
1411  *********************************************************/
1412 int rtl_tx_agg_start(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1413                      struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1414 {
1415         struct rtl_priv *rtlpriv = rtl_priv(hw);
1416         struct rtl_tid_data *tid_data;
1417         struct rtl_sta_info *sta_entry = NULL;
1418
1419         if (sta == NULL)
1420                 return -EINVAL;
1421
1422         if (unlikely(tid >= MAX_TID_COUNT))
1423                 return -EINVAL;
1424
1425         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1426         if (!sta_entry)
1427                 return -ENXIO;
1428         tid_data = &sta_entry->tids[tid];
1429
1430         RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1431                  "on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1432                  tid_data->seq_number);
1433
1434         *ssn = tid_data->seq_number;
1435         tid_data->agg.agg_state = RTL_AGG_START;
1436
1437         ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1438         return 0;
1439 }
1440
1441 int rtl_tx_agg_stop(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1442                     struct ieee80211_sta *sta, u16 tid)
1443 {
1444         struct rtl_priv *rtlpriv = rtl_priv(hw);
1445         struct rtl_tid_data *tid_data;
1446         struct rtl_sta_info *sta_entry = NULL;
1447
1448         if (sta == NULL)
1449                 return -EINVAL;
1450
1451         RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1452                  "on ra = %pM tid = %d\n", sta->addr, tid);
1453
1454         if (unlikely(tid >= MAX_TID_COUNT))
1455                 return -EINVAL;
1456
1457         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1458         tid_data = &sta_entry->tids[tid];
1459         sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
1460
1461         ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1462         return 0;
1463 }
1464
1465 int rtl_rx_agg_start(struct ieee80211_hw *hw,
1466                      struct ieee80211_sta *sta, u16 tid)
1467 {
1468         struct rtl_priv *rtlpriv = rtl_priv(hw);
1469         struct rtl_tid_data *tid_data;
1470         struct rtl_sta_info *sta_entry = NULL;
1471
1472         if (sta == NULL)
1473                 return -EINVAL;
1474
1475         if (unlikely(tid >= MAX_TID_COUNT))
1476                 return -EINVAL;
1477
1478         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1479         if (!sta_entry)
1480                 return -ENXIO;
1481         tid_data = &sta_entry->tids[tid];
1482
1483         RT_TRACE(rtlpriv, COMP_RECV, DBG_DMESG,
1484                  "on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1485                  tid_data->seq_number);
1486
1487         tid_data->agg.rx_agg_state = RTL_RX_AGG_START;
1488         return 0;
1489 }
1490
1491 int rtl_rx_agg_stop(struct ieee80211_hw *hw,
1492                     struct ieee80211_sta *sta, u16 tid)
1493 {
1494         struct rtl_priv *rtlpriv = rtl_priv(hw);
1495         struct rtl_sta_info *sta_entry = NULL;
1496
1497         if (sta == NULL)
1498                 return -EINVAL;
1499
1500         RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1501                  "on ra = %pM tid = %d\n", sta->addr, tid);
1502
1503         if (unlikely(tid >= MAX_TID_COUNT))
1504                 return -EINVAL;
1505
1506         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1507         sta_entry->tids[tid].agg.rx_agg_state = RTL_RX_AGG_STOP;
1508
1509         return 0;
1510 }
1511 int rtl_tx_agg_oper(struct ieee80211_hw *hw,
1512                 struct ieee80211_sta *sta, u16 tid)
1513 {
1514         struct rtl_priv *rtlpriv = rtl_priv(hw);
1515         struct rtl_sta_info *sta_entry = NULL;
1516
1517         if (sta == NULL)
1518                 return -EINVAL;
1519
1520         RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1521                  "on ra = %pM tid = %d\n", sta->addr, tid);
1522
1523         if (unlikely(tid >= MAX_TID_COUNT))
1524                 return -EINVAL;
1525
1526         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1527         sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
1528
1529         return 0;
1530 }
1531
1532 /*********************************************************
1533  *
1534  * wq & timer callback functions
1535  *
1536  *********************************************************/
1537 /* this function is used for roaming */
1538 void rtl_beacon_statistic(struct ieee80211_hw *hw, struct sk_buff *skb)
1539 {
1540         struct rtl_priv *rtlpriv = rtl_priv(hw);
1541         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1542
1543         if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
1544                 return;
1545
1546         if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
1547                 return;
1548
1549         /* check if this really is a beacon */
1550         if (!ieee80211_is_beacon(hdr->frame_control) &&
1551             !ieee80211_is_probe_resp(hdr->frame_control))
1552                 return;
1553
1554         /* min. beacon length + FCS_LEN */
1555         if (skb->len <= 40 + FCS_LEN)
1556                 return;
1557
1558         /* and only beacons from the associated BSSID, please */
1559         if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
1560                 return;
1561
1562         rtlpriv->link_info.bcn_rx_inperiod++;
1563 }
1564 EXPORT_SYMBOL_GPL(rtl_beacon_statistic);
1565
1566 void rtl_watchdog_wq_callback(void *data)
1567 {
1568         struct rtl_works *rtlworks = container_of_dwork_rtl(data,
1569                                                             struct rtl_works,
1570                                                             watchdog_wq);
1571         struct ieee80211_hw *hw = rtlworks->hw;
1572         struct rtl_priv *rtlpriv = rtl_priv(hw);
1573         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1574         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1575         bool busytraffic = false;
1576         bool tx_busy_traffic = false;
1577         bool rx_busy_traffic = false;
1578         bool higher_busytraffic = false;
1579         bool higher_busyrxtraffic = false;
1580         u8 idx, tid;
1581         u32 rx_cnt_inp4eriod = 0;
1582         u32 tx_cnt_inp4eriod = 0;
1583         u32 aver_rx_cnt_inperiod = 0;
1584         u32 aver_tx_cnt_inperiod = 0;
1585         u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
1586         u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
1587
1588         if (is_hal_stop(rtlhal))
1589                 return;
1590
1591         /* <1> Determine if action frame is allowed */
1592         if (mac->link_state > MAC80211_NOLINK) {
1593                 if (mac->cnt_after_linked < 20)
1594                         mac->cnt_after_linked++;
1595         } else {
1596                 mac->cnt_after_linked = 0;
1597         }
1598
1599         /* <2> to check if traffic busy, if
1600          * busytraffic we don't change channel
1601          */
1602         if (mac->link_state >= MAC80211_LINKED) {
1603
1604                 /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
1605                 for (idx = 0; idx <= 2; idx++) {
1606                         rtlpriv->link_info.num_rx_in4period[idx] =
1607                             rtlpriv->link_info.num_rx_in4period[idx + 1];
1608                         rtlpriv->link_info.num_tx_in4period[idx] =
1609                             rtlpriv->link_info.num_tx_in4period[idx + 1];
1610                 }
1611                 rtlpriv->link_info.num_rx_in4period[3] =
1612                     rtlpriv->link_info.num_rx_inperiod;
1613                 rtlpriv->link_info.num_tx_in4period[3] =
1614                     rtlpriv->link_info.num_tx_inperiod;
1615                 for (idx = 0; idx <= 3; idx++) {
1616                         rx_cnt_inp4eriod +=
1617                             rtlpriv->link_info.num_rx_in4period[idx];
1618                         tx_cnt_inp4eriod +=
1619                             rtlpriv->link_info.num_tx_in4period[idx];
1620                 }
1621                 aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
1622                 aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
1623
1624                 /* (2) check traffic busy */
1625                 if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100) {
1626                         busytraffic = true;
1627                         if (aver_rx_cnt_inperiod > aver_tx_cnt_inperiod)
1628                                 rx_busy_traffic = true;
1629                         else
1630                                 tx_busy_traffic = false;
1631                 }
1632
1633                 /* Higher Tx/Rx data. */
1634                 if (aver_rx_cnt_inperiod > 4000 ||
1635                     aver_tx_cnt_inperiod > 4000) {
1636                         higher_busytraffic = true;
1637
1638                         /* Extremely high Rx data. */
1639                         if (aver_rx_cnt_inperiod > 5000)
1640                                 higher_busyrxtraffic = true;
1641                 }
1642
1643                 /* check every tid's tx traffic */
1644                 for (tid = 0; tid <= 7; tid++) {
1645                         for (idx = 0; idx <= 2; idx++)
1646                                 rtlpriv->link_info.tidtx_in4period[tid][idx] =
1647                                         rtlpriv->link_info.tidtx_in4period[tid]
1648                                         [idx + 1];
1649                         rtlpriv->link_info.tidtx_in4period[tid][3] =
1650                                 rtlpriv->link_info.tidtx_inperiod[tid];
1651
1652                         for (idx = 0; idx <= 3; idx++)
1653                                 tidtx_inp4eriod[tid] +=
1654                                    rtlpriv->link_info.tidtx_in4period[tid][idx];
1655                         aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
1656                         if (aver_tidtx_inperiod[tid] > 5000)
1657                                 rtlpriv->link_info.higher_busytxtraffic[tid] =
1658                                                                         true;
1659                         else
1660                                 rtlpriv->link_info.higher_busytxtraffic[tid] =
1661                                                                         false;
1662                 }
1663
1664                 if (((rtlpriv->link_info.num_rx_inperiod +
1665                       rtlpriv->link_info.num_tx_inperiod) > 8) ||
1666                     (rtlpriv->link_info.num_rx_inperiod > 2))
1667                         rtl_lps_leave(hw);
1668                 else
1669                         rtl_lps_enter(hw);
1670         }
1671
1672         rtlpriv->link_info.num_rx_inperiod = 0;
1673         rtlpriv->link_info.num_tx_inperiod = 0;
1674         for (tid = 0; tid <= 7; tid++)
1675                 rtlpriv->link_info.tidtx_inperiod[tid] = 0;
1676
1677         rtlpriv->link_info.busytraffic = busytraffic;
1678         rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
1679         rtlpriv->link_info.rx_busy_traffic = rx_busy_traffic;
1680         rtlpriv->link_info.tx_busy_traffic = tx_busy_traffic;
1681         rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
1682
1683         /* <3> DM */
1684         if (!rtlpriv->cfg->mod_params->disable_watchdog)
1685                 rtlpriv->cfg->ops->dm_watchdog(hw);
1686
1687         /* <4> roaming */
1688         if (mac->link_state == MAC80211_LINKED &&
1689             mac->opmode == NL80211_IFTYPE_STATION) {
1690                 if ((rtlpriv->link_info.bcn_rx_inperiod +
1691                     rtlpriv->link_info.num_rx_inperiod) == 0) {
1692                         rtlpriv->link_info.roam_times++;
1693                         RT_TRACE(rtlpriv, COMP_ERR, DBG_DMESG,
1694                                  "AP off for %d s\n",
1695                                 (rtlpriv->link_info.roam_times * 2));
1696
1697                         /* if we can't recv beacon for 10s,
1698                          * we should reconnect this AP
1699                          */
1700                         if (rtlpriv->link_info.roam_times >= 5) {
1701                                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1702                                          "AP off, try to reconnect now\n");
1703                                 rtlpriv->link_info.roam_times = 0;
1704                                 ieee80211_connection_loss(
1705                                         rtlpriv->mac80211.vif);
1706                         }
1707                 } else {
1708                         rtlpriv->link_info.roam_times = 0;
1709                 }
1710         }
1711
1712         if (rtlpriv->cfg->ops->get_btc_status())
1713                 rtlpriv->btcoexist.btc_ops->btc_periodical(rtlpriv);
1714
1715         rtlpriv->link_info.bcn_rx_inperiod = 0;
1716 }
1717
1718 void rtl_watch_dog_timer_callback(unsigned long data)
1719 {
1720         struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1721         struct rtl_priv *rtlpriv = rtl_priv(hw);
1722
1723         queue_delayed_work(rtlpriv->works.rtl_wq,
1724                            &rtlpriv->works.watchdog_wq, 0);
1725
1726         mod_timer(&rtlpriv->works.watchdog_timer,
1727                   jiffies + MSECS(RTL_WATCH_DOG_TIME));
1728 }
1729 void rtl_fwevt_wq_callback(void *data)
1730 {
1731         struct rtl_works *rtlworks =
1732                 container_of_dwork_rtl(data, struct rtl_works, fwevt_wq);
1733         struct ieee80211_hw *hw = rtlworks->hw;
1734         struct rtl_priv *rtlpriv = rtl_priv(hw);
1735
1736         rtlpriv->cfg->ops->c2h_command_handle(hw);
1737 }
1738 void rtl_easy_concurrent_retrytimer_callback(unsigned long data)
1739 {
1740         struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1741         struct rtl_priv *rtlpriv = rtl_priv(hw);
1742         struct rtl_priv *buddy_priv = rtlpriv->buddy_priv;
1743
1744         if (buddy_priv == NULL)
1745                 return;
1746
1747         rtlpriv->cfg->ops->dualmac_easy_concurrent(hw);
1748 }
1749 /*********************************************************
1750  *
1751  * frame process functions
1752  *
1753  *********************************************************/
1754 u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
1755 {
1756         struct ieee80211_mgmt *mgmt = (void *)data;
1757         u8 *pos, *end;
1758
1759         pos = (u8 *)mgmt->u.beacon.variable;
1760         end = data + len;
1761         while (pos < end) {
1762                 if (pos + 2 + pos[1] > end)
1763                         return NULL;
1764
1765                 if (pos[0] == ie)
1766                         return pos;
1767
1768                 pos += 2 + pos[1];
1769         }
1770         return NULL;
1771 }
1772
1773 /* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
1774 /* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
1775 static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
1776                                      enum ieee80211_smps_mode smps,
1777                                      u8 *da, u8 *bssid)
1778 {
1779         struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1780         struct sk_buff *skb;
1781         struct ieee80211_mgmt *action_frame;
1782
1783         /* 27 = header + category + action + smps mode */
1784         skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
1785         if (!skb)
1786                 return NULL;
1787
1788         skb_reserve(skb, hw->extra_tx_headroom);
1789         action_frame = (void *)skb_put(skb, 27);
1790         memset(action_frame, 0, 27);
1791         memcpy(action_frame->da, da, ETH_ALEN);
1792         memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
1793         memcpy(action_frame->bssid, bssid, ETH_ALEN);
1794         action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1795                                                   IEEE80211_STYPE_ACTION);
1796         action_frame->u.action.category = WLAN_CATEGORY_HT;
1797         action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
1798         switch (smps) {
1799         case IEEE80211_SMPS_AUTOMATIC:/* 0 */
1800         case IEEE80211_SMPS_NUM_MODES:/* 4 */
1801                 WARN_ON(1);
1802         /* Here will get a 'MISSING_BREAK' in Coverity Test, just ignore it.
1803          * According to Kernel Code, here is right.
1804          */
1805         case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
1806                 action_frame->u.action.u.ht_smps.smps_control =
1807                                 WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
1808                 break;
1809         case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
1810                 action_frame->u.action.u.ht_smps.smps_control =
1811                                 WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
1812                 break;
1813         case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
1814                 action_frame->u.action.u.ht_smps.smps_control =
1815                                 WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
1816                 break;
1817         }
1818
1819         return skb;
1820 }
1821
1822 int rtl_send_smps_action(struct ieee80211_hw *hw,
1823                          struct ieee80211_sta *sta,
1824                          enum ieee80211_smps_mode smps)
1825 {
1826         struct rtl_priv *rtlpriv = rtl_priv(hw);
1827         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1828         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1829         struct sk_buff *skb = NULL;
1830         struct rtl_tcb_desc tcb_desc;
1831         u8 bssid[ETH_ALEN] = {0};
1832
1833         memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
1834
1835         if (rtlpriv->mac80211.act_scanning)
1836                 goto err_free;
1837
1838         if (!sta)
1839                 goto err_free;
1840
1841         if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
1842                 goto err_free;
1843
1844         if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1845                 goto err_free;
1846
1847         if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP)
1848                 memcpy(bssid, rtlpriv->efuse.dev_addr, ETH_ALEN);
1849         else
1850                 memcpy(bssid, rtlpriv->mac80211.bssid, ETH_ALEN);
1851
1852         skb = rtl_make_smps_action(hw, smps, sta->addr, bssid);
1853         /* this is a type = mgmt * stype = action frame */
1854         if (skb) {
1855                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1856                 struct rtl_sta_info *sta_entry =
1857                         (struct rtl_sta_info *) sta->drv_priv;
1858                 sta_entry->mimo_ps = smps;
1859                 /* rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0); */
1860
1861                 info->control.rates[0].idx = 0;
1862                 info->band = hw->conf.chandef.chan->band;
1863                 rtlpriv->intf_ops->adapter_tx(hw, sta, skb, &tcb_desc);
1864         }
1865         return 1;
1866
1867 err_free:
1868         return 0;
1869 }
1870 EXPORT_SYMBOL(rtl_send_smps_action);
1871
1872 void rtl_phy_scan_operation_backup(struct ieee80211_hw *hw, u8 operation)
1873 {
1874         struct rtl_priv *rtlpriv = rtl_priv(hw);
1875         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1876         enum io_type iotype;
1877
1878         if (!is_hal_stop(rtlhal)) {
1879                 switch (operation) {
1880                 case SCAN_OPT_BACKUP:
1881                         iotype = IO_CMD_PAUSE_DM_BY_SCAN;
1882                         rtlpriv->cfg->ops->set_hw_reg(hw,
1883                                                       HW_VAR_IO_CMD,
1884                                                       (u8 *)&iotype);
1885                         break;
1886                 case SCAN_OPT_RESTORE:
1887                         iotype = IO_CMD_RESUME_DM_BY_SCAN;
1888                         rtlpriv->cfg->ops->set_hw_reg(hw,
1889                                                       HW_VAR_IO_CMD,
1890                                                       (u8 *)&iotype);
1891                         break;
1892                 default:
1893                         RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1894                                  "Unknown Scan Backup operation.\n");
1895                         break;
1896                 }
1897         }
1898 }
1899 EXPORT_SYMBOL(rtl_phy_scan_operation_backup);
1900
1901 /* because mac80211 have issues when can receive del ba
1902  * so here we just make a fake del_ba if we receive a ba_req
1903  * but rx_agg was opened to let mac80211 release some ba
1904  * related resources, so please this del_ba for tx
1905  */
1906 struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw,
1907                                 u8 *sa, u8 *bssid, u16 tid)
1908 {
1909         struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1910         struct sk_buff *skb;
1911         struct ieee80211_mgmt *action_frame;
1912         u16 params;
1913
1914         /* 27 = header + category + action + smps mode */
1915         skb = dev_alloc_skb(34 + hw->extra_tx_headroom);
1916         if (!skb)
1917                 return NULL;
1918
1919         skb_reserve(skb, hw->extra_tx_headroom);
1920         action_frame = (void *)skb_put(skb, 34);
1921         memset(action_frame, 0, 34);
1922         memcpy(action_frame->sa, sa, ETH_ALEN);
1923         memcpy(action_frame->da, rtlefuse->dev_addr, ETH_ALEN);
1924         memcpy(action_frame->bssid, bssid, ETH_ALEN);
1925         action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1926                                                   IEEE80211_STYPE_ACTION);
1927         action_frame->u.action.category = WLAN_CATEGORY_BACK;
1928         action_frame->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1929         params = (u16)(1 << 11);        /* bit 11 initiator */
1930         params |= (u16)(tid << 12);     /* bit 15:12 TID number */
1931
1932         action_frame->u.action.u.delba.params = cpu_to_le16(params);
1933         action_frame->u.action.u.delba.reason_code =
1934                 cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
1935
1936         return skb;
1937 }
1938
1939 /*********************************************************
1940  *
1941  * IOT functions
1942  *
1943  *********************************************************/
1944 static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
1945                                   struct octet_string vendor_ie)
1946 {
1947         struct rtl_priv *rtlpriv = rtl_priv(hw);
1948         bool matched = false;
1949         static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
1950         static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
1951         static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
1952         static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
1953         static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
1954         static u8 racap[] = { 0x00, 0x0c, 0x43 };
1955         static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
1956         static u8 marvcap[] = { 0x00, 0x50, 0x43 };
1957
1958         if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
1959                 memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
1960                 rtlpriv->mac80211.vendor = PEER_ATH;
1961                 matched = true;
1962         } else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
1963                 memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
1964                 memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
1965                 rtlpriv->mac80211.vendor = PEER_BROAD;
1966                 matched = true;
1967         } else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
1968                 rtlpriv->mac80211.vendor = PEER_RAL;
1969                 matched = true;
1970         } else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
1971                 rtlpriv->mac80211.vendor = PEER_CISCO;
1972                 matched = true;
1973         } else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
1974                 rtlpriv->mac80211.vendor = PEER_MARV;
1975                 matched = true;
1976         }
1977
1978         return matched;
1979 }
1980
1981 static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
1982                 unsigned int len)
1983 {
1984         struct ieee80211_mgmt *mgmt = (void *)data;
1985         struct octet_string vendor_ie;
1986         u8 *pos, *end;
1987
1988         pos = (u8 *)mgmt->u.beacon.variable;
1989         end = data + len;
1990         while (pos < end) {
1991                 if (pos[0] == 221) {
1992                         vendor_ie.length = pos[1];
1993                         vendor_ie.octet = &pos[2];
1994                         if (rtl_chk_vendor_ouisub(hw, vendor_ie))
1995                                 return true;
1996                 }
1997
1998                 if (pos + 2 + pos[1] > end)
1999                         return false;
2000
2001                 pos += 2 + pos[1];
2002         }
2003         return false;
2004 }
2005
2006 void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
2007 {
2008         struct rtl_priv *rtlpriv = rtl_priv(hw);
2009         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
2010         struct ieee80211_hdr *hdr = (void *)data;
2011         u32 vendor = PEER_UNKNOWN;
2012
2013         static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
2014         static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
2015         static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
2016         static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
2017         static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
2018         static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
2019         static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
2020         static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
2021         static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
2022         static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
2023         static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
2024         static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
2025         static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
2026         static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
2027         static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
2028         static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
2029
2030         if (mac->opmode != NL80211_IFTYPE_STATION)
2031                 return;
2032
2033         if (mac->link_state == MAC80211_NOLINK) {
2034                 mac->vendor = PEER_UNKNOWN;
2035                 return;
2036         }
2037
2038         if (mac->cnt_after_linked > 2)
2039                 return;
2040
2041         /* check if this really is a beacon */
2042         if (!ieee80211_is_beacon(hdr->frame_control))
2043                 return;
2044
2045         /* min. beacon length + FCS_LEN */
2046         if (len <= 40 + FCS_LEN)
2047                 return;
2048
2049         /* and only beacons from the associated BSSID, please */
2050         if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
2051                 return;
2052
2053         if (rtl_find_221_ie(hw, data, len))
2054                 vendor = mac->vendor;
2055
2056         if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
2057                 (memcmp(mac->bssid, ap5_2, 3) == 0) ||
2058                 (memcmp(mac->bssid, ap5_3, 3) == 0) ||
2059                 (memcmp(mac->bssid, ap5_4, 3) == 0) ||
2060                 (memcmp(mac->bssid, ap5_5, 3) == 0) ||
2061                 (memcmp(mac->bssid, ap5_6, 3) == 0) ||
2062                 vendor == PEER_ATH) {
2063                 vendor = PEER_ATH;
2064                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ath find\n");
2065         } else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
2066                 (memcmp(mac->bssid, ap4_5, 3) == 0) ||
2067                 (memcmp(mac->bssid, ap4_1, 3) == 0) ||
2068                 (memcmp(mac->bssid, ap4_2, 3) == 0) ||
2069                 (memcmp(mac->bssid, ap4_3, 3) == 0) ||
2070                 vendor == PEER_RAL) {
2071                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ral find\n");
2072                 vendor = PEER_RAL;
2073         } else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
2074                 vendor == PEER_CISCO) {
2075                 vendor = PEER_CISCO;
2076                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>cisco find\n");
2077         } else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
2078                 (memcmp(mac->bssid, ap3_2, 3) == 0) ||
2079                 (memcmp(mac->bssid, ap3_3, 3) == 0) ||
2080                 vendor == PEER_BROAD) {
2081                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>broad find\n");
2082                 vendor = PEER_BROAD;
2083         } else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
2084                 vendor == PEER_MARV) {
2085                 vendor = PEER_MARV;
2086                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>marv find\n");
2087         }
2088
2089         mac->vendor = vendor;
2090 }
2091 EXPORT_SYMBOL_GPL(rtl_recognize_peer);
2092
2093 /*********************************************************
2094  *
2095  * sysfs functions
2096  *
2097  *********************************************************/
2098 static ssize_t rtl_show_debug_level(struct device *d,
2099                                     struct device_attribute *attr, char *buf)
2100 {
2101         struct ieee80211_hw *hw = dev_get_drvdata(d);
2102         struct rtl_priv *rtlpriv = rtl_priv(hw);
2103
2104         return sprintf(buf, "0x%08X\n", rtlpriv->dbg.global_debuglevel);
2105 }
2106
2107 static ssize_t rtl_store_debug_level(struct device *d,
2108                                      struct device_attribute *attr,
2109                                      const char *buf, size_t count)
2110 {
2111         struct ieee80211_hw *hw = dev_get_drvdata(d);
2112         struct rtl_priv *rtlpriv = rtl_priv(hw);
2113         unsigned long val;
2114         int ret;
2115
2116         ret = kstrtoul(buf, 0, &val);
2117         if (ret) {
2118                 RT_TRACE(rtlpriv, COMP_ERR, DBG_DMESG,
2119                          "%s is not in hex or decimal form.\n", buf);
2120         } else {
2121                 rtlpriv->dbg.global_debuglevel = val;
2122                 RT_TRACE(rtlpriv, COMP_ERR, DBG_DMESG,
2123                          "debuglevel:%x\n",
2124                          rtlpriv->dbg.global_debuglevel);
2125         }
2126
2127         return strnlen(buf, count);
2128 }
2129
2130 static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
2131                    rtl_show_debug_level, rtl_store_debug_level);
2132
2133 static struct attribute *rtl_sysfs_entries[] = {
2134
2135         &dev_attr_debug_level.attr,
2136
2137         NULL
2138 };
2139
2140 /*
2141  * "name" is folder name witch will be
2142  * put in device directory like :
2143  * sys/devices/pci0000:00/0000:00:1c.4/
2144  * 0000:06:00.0/rtl_sysfs
2145  */
2146 struct attribute_group rtl_attribute_group = {
2147         .name = "rtlsysfs",
2148         .attrs = rtl_sysfs_entries,
2149 };
2150 EXPORT_SYMBOL_GPL(rtl_attribute_group);
2151
2152 MODULE_AUTHOR("lizhaoming       <chaoming_li@realsil.com.cn>");
2153 MODULE_AUTHOR("Realtek WlanFAE  <wlanfae@realtek.com>");
2154 MODULE_AUTHOR("Larry Finger     <Larry.FInger@lwfinger.net>");
2155 MODULE_LICENSE("GPL");
2156 MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
2157
2158 struct rtl_global_var rtl_global_var = {};
2159 EXPORT_SYMBOL_GPL(rtl_global_var);
2160
2161 static int __init rtl_core_module_init(void)
2162 {
2163         if (rtl_rate_control_register())
2164                 pr_err("rtl: Unable to register rtl_rc, use default RC !!\n");
2165
2166         /* init some global vars */
2167         INIT_LIST_HEAD(&rtl_global_var.glb_priv_list);
2168         spin_lock_init(&rtl_global_var.glb_list_lock);
2169
2170         return 0;
2171 }
2172
2173 static void __exit rtl_core_module_exit(void)
2174 {
2175         /*RC*/
2176         rtl_rate_control_unregister();
2177 }
2178
2179 module_init(rtl_core_module_init);
2180 module_exit(rtl_core_module_exit);