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