OSDN Git Service

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