Merge tag 'backport/v3.14.24-ltsi-rc1/irq-renesas-intc-irqpin-to-v3.18-rc1' into...
[platform/adaptation/renesas_rcar/renesas_kernel.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 hsould
43  *keep it clear under follwing roles:
44  *
45  *This file include follwing 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 void _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         INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
414                           (void *)rtl_watchdog_wq_callback);
415         INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
416                           (void *)rtl_ips_nic_off_wq_callback);
417         INIT_DELAYED_WORK(&rtlpriv->works.ps_work,
418                           (void *)rtl_swlps_wq_callback);
419         INIT_DELAYED_WORK(&rtlpriv->works.ps_rfon_wq,
420                           (void *)rtl_swlps_rfon_wq_callback);
421         INIT_DELAYED_WORK(&rtlpriv->works.fwevt_wq,
422                           (void *)rtl_fwevt_wq_callback);
423
424 }
425
426 void rtl_deinit_deferred_work(struct ieee80211_hw *hw)
427 {
428         struct rtl_priv *rtlpriv = rtl_priv(hw);
429
430         del_timer_sync(&rtlpriv->works.watchdog_timer);
431
432         cancel_delayed_work(&rtlpriv->works.watchdog_wq);
433         cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
434         cancel_delayed_work(&rtlpriv->works.ps_work);
435         cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
436         cancel_delayed_work(&rtlpriv->works.fwevt_wq);
437 }
438
439 void rtl_init_rfkill(struct ieee80211_hw *hw)
440 {
441         struct rtl_priv *rtlpriv = rtl_priv(hw);
442
443         bool radio_state;
444         bool blocked;
445         u8 valid = 0;
446
447         /*set init state to on */
448         rtlpriv->rfkill.rfkill_state = 1;
449         wiphy_rfkill_set_hw_state(hw->wiphy, 0);
450
451         radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
452
453         if (valid) {
454                 printk(KERN_INFO "rtlwifi: wireless switch is %s\n",
455                        rtlpriv->rfkill.rfkill_state ? "on" : "off");
456
457                 rtlpriv->rfkill.rfkill_state = radio_state;
458
459                 blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
460                 wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
461         }
462
463         wiphy_rfkill_start_polling(hw->wiphy);
464 }
465
466 void rtl_deinit_rfkill(struct ieee80211_hw *hw)
467 {
468         wiphy_rfkill_stop_polling(hw->wiphy);
469 }
470
471 #ifdef VIF_TODO
472 static void rtl_init_vif(struct ieee80211_hw *hw)
473 {
474         struct rtl_priv *rtlpriv = rtl_priv(hw);
475
476         INIT_LIST_HEAD(&rtlpriv->vif_priv.vif_list);
477
478         rtlpriv->vif_priv.vifs = 0;
479 }
480 #endif
481
482 int rtl_init_core(struct ieee80211_hw *hw)
483 {
484         struct rtl_priv *rtlpriv = rtl_priv(hw);
485         struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
486
487         /* <1> init mac80211 */
488         _rtl_init_mac80211(hw);
489         rtlmac->hw = hw;
490         rtlmac->link_state = MAC80211_NOLINK;
491
492         /* <2> rate control register */
493         hw->rate_control_algorithm = "rtl_rc";
494
495         /*
496          * <3> init CRDA must come after init
497          * mac80211 hw  in _rtl_init_mac80211.
498          */
499         if (rtl_regd_init(hw, rtl_reg_notifier)) {
500                 RT_TRACE(COMP_ERR, DBG_EMERG, ("REGD init failed\n"));
501                 return 1;
502         }
503
504         /* <4> locks */
505         mutex_init(&rtlpriv->locks.conf_mutex);
506         spin_lock_init(&rtlpriv->locks.ips_lock);
507         spin_lock_init(&rtlpriv->locks.irq_th_lock);
508         spin_lock_init(&rtlpriv->locks.h2c_lock);
509         spin_lock_init(&rtlpriv->locks.rf_ps_lock);
510         spin_lock_init(&rtlpriv->locks.rf_lock);
511         spin_lock_init(&rtlpriv->locks.lps_lock);
512         spin_lock_init(&rtlpriv->locks.waitq_lock);
513         spin_lock_init(&rtlpriv->locks.entry_list_lock);
514         spin_lock_init(&rtlpriv->locks.cck_and_rw_pagea_lock);
515         spin_lock_init(&rtlpriv->locks.check_sendpkt_lock);
516         spin_lock_init(&rtlpriv->locks.fw_ps_lock);
517         spin_lock_init(&rtlpriv->locks.iqk_lock);
518         /* <5> init list */
519         INIT_LIST_HEAD(&rtlpriv->entry_list);
520
521         /* <6> init deferred work */
522         _rtl_init_deferred_work(hw);
523
524         /* <7> */
525 #ifdef VIF_TODO
526         rtl_init_vif(hw);
527 #endif
528
529         return 0;
530 }
531
532 void rtl_deinit_core(struct ieee80211_hw *hw)
533 {
534 }
535
536 void rtl_init_rx_config(struct ieee80211_hw *hw)
537 {
538         struct rtl_priv *rtlpriv = rtl_priv(hw);
539         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
540
541         rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
542 }
543
544 /*********************************************************
545  *
546  * tx information functions
547  *
548  *********************************************************/
549 static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
550                                           struct rtl_tcb_desc *tcb_desc,
551                                           struct ieee80211_tx_info *info)
552 {
553         struct rtl_priv *rtlpriv = rtl_priv(hw);
554         u8 rate_flag = info->control.rates[0].flags;
555
556         tcb_desc->use_shortpreamble = false;
557
558         /* 1M can only use Long Preamble. 11B spec */
559         if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
560                 return;
561         else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
562                 tcb_desc->use_shortpreamble = true;
563
564         return;
565 }
566
567 static void _rtl_query_shortgi(struct ieee80211_hw *hw,
568                                struct ieee80211_sta *sta,
569                                struct rtl_tcb_desc *tcb_desc,
570                                struct ieee80211_tx_info *info)
571 {
572         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
573         u8 rate_flag = info->control.rates[0].flags;
574         u8 sgi_40 = 0, sgi_20 = 0, bw_40 = 0;
575         tcb_desc->use_shortgi = false;
576
577         if (sta == NULL)
578                 return;
579
580         sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
581         sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
582
583         if (!(sta->ht_cap.ht_supported))
584                 return;
585
586         if (!sgi_40 && !sgi_20)
587                 return;
588
589         if (mac->opmode == NL80211_IFTYPE_STATION)
590                 bw_40 = mac->bw_40;
591         else if (mac->opmode == NL80211_IFTYPE_AP ||
592                  mac->opmode == NL80211_IFTYPE_ADHOC ||
593                  mac->opmode == NL80211_IFTYPE_MESH_POINT)
594                 bw_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
595
596         if ((bw_40 == true) && sgi_40)
597                 tcb_desc->use_shortgi = true;
598         else if ((bw_40 == false) && sgi_20)
599                 tcb_desc->use_shortgi = true;
600
601         if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
602                 tcb_desc->use_shortgi = false;
603 }
604
605 static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
606                                        struct rtl_tcb_desc *tcb_desc,
607                                        struct ieee80211_tx_info *info)
608 {
609         struct rtl_priv *rtlpriv = rtl_priv(hw);
610         u8 rate_flag = info->control.rates[0].flags;
611
612         /* Common Settings */
613         tcb_desc->b_rts_stbc = false;
614         tcb_desc->b_cts_enable = false;
615         tcb_desc->rts_sc = 0;
616         tcb_desc->b_rts_bw = false;
617         tcb_desc->b_rts_use_shortpreamble = false;
618         tcb_desc->b_rts_use_shortgi = false;
619
620         if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
621                 /* Use CTS-to-SELF in protection mode. */
622                 tcb_desc->b_rts_enable = true;
623                 tcb_desc->b_cts_enable = true;
624                 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
625         } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
626                 /* Use RTS-CTS in protection mode. */
627                 tcb_desc->b_rts_enable = true;
628                 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
629         }
630 }
631
632 static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
633                                    struct ieee80211_sta *sta,
634                                    struct rtl_tcb_desc *tcb_desc)
635 {
636         struct rtl_priv *rtlpriv = rtl_priv(hw);
637         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
638         struct rtl_sta_info *sta_entry = NULL;
639         u8 ratr_index = 7;
640
641         if (sta) {
642                 sta_entry = (struct rtl_sta_info *) sta->drv_priv;
643                 ratr_index = sta_entry->ratr_index;
644         }
645         if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
646                 if (mac->opmode == NL80211_IFTYPE_STATION) {
647                         tcb_desc->ratr_index = 0;
648                 } else if (mac->opmode == NL80211_IFTYPE_ADHOC ||
649                                 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
650                         if (tcb_desc->b_multicast || tcb_desc->b_broadcast) {
651                                 tcb_desc->hw_rate =
652                                     rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
653                                 tcb_desc->use_driver_rate = 1;
654                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
655                         } else {
656                                 tcb_desc->ratr_index = ratr_index;
657                         }
658                 } else if (mac->opmode == NL80211_IFTYPE_AP) {
659                         tcb_desc->ratr_index = ratr_index;
660                 }
661         }
662
663         if (rtlpriv->dm.b_useramask) {
664                 tcb_desc->ratr_index = ratr_index;
665                 /* TODO we will differentiate adhoc and station futrue  */
666                 if (mac->opmode == NL80211_IFTYPE_STATION ||
667                         mac->opmode == NL80211_IFTYPE_MESH_POINT) {
668                         tcb_desc->mac_id = 0;
669
670                         if (mac->mode == WIRELESS_MODE_N_24G) {
671                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
672                         } else if (mac->mode == WIRELESS_MODE_N_5G) {
673                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_NG;
674                         } else if (mac->mode & WIRELESS_MODE_G) {
675                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
676                         } else if (mac->mode & WIRELESS_MODE_B) {
677                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
678                         } else if (mac->mode & WIRELESS_MODE_A) {
679                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_G;
680                         }
681                 } else if (mac->opmode == NL80211_IFTYPE_AP ||
682                         mac->opmode == NL80211_IFTYPE_ADHOC) {
683                         if (NULL != sta) {
684                                 if (sta->aid > 0) {
685                                         tcb_desc->mac_id = sta->aid + 1;
686                                 } else {
687                                         tcb_desc->mac_id = 1;
688                                 }
689                         } else {
690                                 tcb_desc->mac_id = 0;
691                         }
692                 }
693         }
694 }
695
696 static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
697                                       struct ieee80211_sta *sta,
698                                       struct rtl_tcb_desc *tcb_desc)
699 {
700         struct rtl_priv *rtlpriv = rtl_priv(hw);
701         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
702
703         tcb_desc->b_packet_bw = false;
704         if (!sta)
705                 return;
706         if (mac->opmode == NL80211_IFTYPE_AP ||
707                 mac->opmode == NL80211_IFTYPE_ADHOC ||
708                 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
709                 if (!(sta->ht_cap.ht_supported) ||
710                         !(sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
711                         return;
712         } else if (mac->opmode == NL80211_IFTYPE_STATION) {
713                 if (!mac->bw_40 || !(sta->ht_cap.ht_supported))
714                 return;
715         }
716         if (tcb_desc->b_multicast || tcb_desc->b_broadcast)
717                 return;
718
719         /*use legency rate, shall use 20MHz */
720         if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
721                 return;
722
723         tcb_desc->b_packet_bw = true;
724 }
725
726 static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw,
727                                   struct ieee80211_sta *sta)
728 {
729         struct rtl_priv *rtlpriv = rtl_priv(hw);
730         struct rtl_phy *rtlphy = &(rtlpriv->phy);
731         u8 hw_rate;
732
733         if ((get_rf_type(rtlphy) == RF_2T2R) && (sta->ht_cap.mcs.rx_mask[1]!=0))
734                 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
735         else
736                 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
737
738         return hw_rate;
739 }
740
741 void rtl_get_tcb_desc(struct ieee80211_hw *hw,
742                       struct ieee80211_tx_info *info,
743                       struct ieee80211_sta *sta,
744                       struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
745 {
746         struct rtl_priv *rtlpriv = rtl_priv(hw);
747         struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
748         struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
749         struct ieee80211_rate *txrate;
750         u16 fc = rtl_get_fc(skb);
751
752         txrate = ieee80211_get_tx_rate(hw, info);
753         if (txrate != NULL)
754                 tcb_desc->hw_rate = txrate->hw_value;
755
756         if (ieee80211_is_data(fc)) {
757                 /*
758                  *we set data rate INX 0
759                  *in rtl_rc.c   if skb is special data or
760                  *mgt which need low data rate.
761                  */
762
763                 /*
764                  *So tcb_desc->hw_rate is just used for
765                  *special data and mgt frames
766                  */
767                 if (info->control.rates[0].idx == 0 ||
768                                 ieee80211_is_nullfunc(fc)) {
769                         tcb_desc->use_driver_rate = true;
770                         tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
771
772                         tcb_desc->disable_ratefallback = 1;
773                 } else {
774                         /*
775                          *because hw will nerver use hw_rate
776                          *when tcb_desc->use_driver_rate = false
777                          *so we never set highest N rate here,
778                          *and N rate will all be controled by FW
779                          *when tcb_desc->use_driver_rate = false
780                          */
781                         if (sta && (sta->ht_cap.ht_supported)) {
782                                 tcb_desc->hw_rate = _rtl_get_highest_n_rate(hw, sta);
783                         } else {
784                                 if(rtlmac->mode == WIRELESS_MODE_B) {
785                                         tcb_desc->hw_rate =
786                                             rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
787                                 } else {
788                                         tcb_desc->hw_rate =
789                                             rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
790                                 }
791                         }
792                 }
793
794                 if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
795                         tcb_desc->b_multicast = 1;
796                 else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
797                         tcb_desc->b_broadcast = 1;
798
799                 _rtl_txrate_selectmode(hw, sta, tcb_desc);
800                 _rtl_query_bandwidth_mode(hw, sta, tcb_desc);
801                 _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
802                 _rtl_query_shortgi(hw, sta, tcb_desc, info);
803                 _rtl_query_protection_mode(hw, tcb_desc, info);
804         } else {
805                 tcb_desc->use_driver_rate = true;
806                 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
807                 tcb_desc->disable_ratefallback = 1;
808                 tcb_desc->mac_id = 0;
809                 tcb_desc->b_packet_bw = false;
810         }
811 }
812 //EXPORT_SYMBOL(rtl_get_tcb_desc);
813
814 bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
815 {
816         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
817         struct rtl_priv *rtlpriv = rtl_priv(hw);
818         u16 fc = rtl_get_fc(skb);
819
820         if (rtlpriv->dm.supp_phymode_switch &&
821                 mac->link_state < MAC80211_LINKED &&
822                 (ieee80211_is_auth(fc) || ieee80211_is_probe_req(fc))) {
823                 if (rtlpriv->cfg->ops->check_switch_to_dmdp)
824                         rtlpriv->cfg->ops->check_switch_to_dmdp(hw);
825         }
826         if (ieee80211_is_auth(fc)) {
827                 RT_TRACE(COMP_SEND, DBG_DMESG, ("MAC80211_LINKING\n"));
828                 rtl_ips_nic_on(hw);
829
830                 mac->link_state = MAC80211_LINKING;
831                 /* Dul mac */
832                 rtlpriv->phy.b_need_iqk = true;
833
834         }
835
836         return true;
837 }
838
839 struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw, u8 *sa,
840                                 u8 *bssid, u16 tid);
841 bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
842 {
843         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
844         struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
845         struct rtl_priv *rtlpriv = rtl_priv(hw);
846         u16 fc = rtl_get_fc(skb);
847         u8 *act = (u8 *) (((u8 *) skb->data + MAC80211_3ADDR_LEN));
848         u8 category;
849
850         if (!ieee80211_is_action(fc))
851                 return true;
852
853         category = *act;
854         act++;
855         switch (category) {
856         case ACT_CAT_BA:
857                 switch (*act) {
858                 case ACT_ADDBAREQ:
859                         if (mac->act_scanning)
860                                 return false;
861
862                         RT_TRACE((COMP_SEND | COMP_RECV), DBG_DMESG,
863                                  ("%s ACT_ADDBAREQ From :%pM\n",
864                                   is_tx ? "Tx" : "Rx", hdr->addr2));
865                         RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG, ("req \n"),
866                         skb->data, skb->len);
867                         if (!is_tx) {
868                                 struct ieee80211_sta *sta = NULL;
869                                 struct rtl_sta_info *sta_entry = NULL;
870                                 struct ieee80211_mgmt *mgmt = (void *)skb->data;
871                                 u16 capab = 0, tid = 0;
872                                 struct rtl_tid_data *tid_data;
873                                 struct sk_buff *skb_delba = NULL;
874                                 struct ieee80211_rx_status rx_status = { 0 };
875
876                                 rcu_read_lock();
877                                 sta = rtl_find_sta(hw, hdr->addr3);
878                                 if (sta == NULL) {
879                                         RT_TRACE((COMP_SEND | COMP_RECV),
880                                                  DBG_EMERG, ("sta is NULL\n"));
881                                         rcu_read_unlock();
882                                         return true;
883                                 }
884
885                                 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
886                                 if (!sta_entry) {
887                                         rcu_read_unlock();
888                                         return true;
889                                 }
890                                 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
891                                 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
892                                 tid_data = &sta_entry->tids[tid];
893                                 if (tid_data->agg.rx_agg_state ==
894                                     RTL_RX_AGG_START) {
895                                         skb_delba = rtl_make_del_ba(hw,
896                                                                     hdr->addr2,
897                                                                     hdr->addr3,
898                                                                     tid);
899                                         if (skb_delba) {
900 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0))
901                                                 rx_status.freq = hw->conf.chandef.chan->center_freq;
902                                                 rx_status.band = hw->conf.chandef.chan->band;
903 #else
904                                                 rx_status.freq = hw->conf.channel->center_freq;
905                                                 rx_status.band = hw->conf.channel->band;
906 #endif
907                                                 rx_status.flag |= RX_FLAG_DECRYPTED;
908                                                 rx_status.flag |= RX_FLAG_MACTIME_MPDU;
909                                                 rx_status.rate_idx = 0;
910                                                 rx_status.signal = 50 + 10;
911                                                 memcpy(IEEE80211_SKB_RXCB(skb_delba), &rx_status,
912                                                                 sizeof(rx_status));
913                                                 RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG,
914                                                                 ("fake del\n"), skb_delba->data,
915                                                                 skb_delba->len);
916                                                 ieee80211_rx_irqsafe(hw, skb_delba);
917                                         }
918                                 }
919                                 rcu_read_unlock();
920                         }
921                         break;
922                 case ACT_ADDBARSP:
923                         RT_TRACE((COMP_SEND | COMP_RECV), DBG_DMESG,
924                                  ("%s ACT_ADDBARSP From :%pM\n",
925                                   is_tx ? "Tx" : "Rx", hdr->addr2));
926                         break;
927                 case ACT_DELBA:
928                         RT_TRACE((COMP_SEND | COMP_RECV), DBG_DMESG,
929                                  ("ACT_ADDBADEL From :%pM\n", hdr->addr2));
930                         break;
931                 }
932                 break;
933         default:
934                 break;
935         }
936
937         return true;
938 }
939
940 /*should call before software enc*/
941 u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
942 {
943         struct rtl_priv *rtlpriv = rtl_priv(hw);
944         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
945         u16 fc = rtl_get_fc(skb);
946         u16 ether_type;
947         u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
948         const struct iphdr *ip;
949
950         if (!ieee80211_is_data(fc))
951                 goto end;
952
953
954         ip = (struct iphdr *)((u8 *) skb->data + mac_hdr_len +
955                               SNAP_SIZE + PROTOC_TYPE_SIZE);
956         ether_type = *(u16 *) ((u8 *) skb->data + mac_hdr_len + SNAP_SIZE);
957         ether_type = ntohs(ether_type);
958
959         if (ETH_P_IP == ether_type) {
960                 if (IPPROTO_UDP == ip->protocol) {
961                         struct udphdr *udp = (struct udphdr *)((u8 *) ip +
962                                                                (ip->ihl << 2));
963                         if (((((u8 *) udp)[1] == 68) &&
964                              (((u8 *) udp)[3] == 67)) ||
965                             ((((u8 *) udp)[1] == 67) &&
966                              (((u8 *) udp)[3] == 68))) {
967                                 /*
968                                  * 68 : UDP BOOTP client
969                                  * 67 : UDP BOOTP server
970                                  */
971                                 RT_TRACE((COMP_SEND | COMP_RECV),
972                                          DBG_DMESG, ("dhcp %s !!\n",
973                                                      (is_tx) ? "Tx" : "Rx"));
974
975                                 if (is_tx) {
976                                         rtlpriv->ra.is_special_data = true;
977                                         rtl_lps_leave(hw);
978                                         ppsc->last_delaylps_stamp_jiffies =
979                                                                         jiffies;
980                                 }
981
982                                 return true;
983                         }
984                 }
985         } else if (ETH_P_ARP == ether_type) {
986                 if (is_tx) {
987                         rtlpriv->ra.is_special_data = true;
988                         rtl_lps_leave(hw);
989                         ppsc->last_delaylps_stamp_jiffies = jiffies;
990                 }
991
992                 return true;
993         } else if (ETH_P_PAE == ether_type) {
994                 RT_TRACE((COMP_SEND | COMP_RECV), DBG_DMESG,
995                          ("802.1X %s EAPOL pkt!!\n", (is_tx) ? "Tx" : "Rx"));
996
997                 if (is_tx) {
998                         rtlpriv->ra.is_special_data = true;
999                         rtl_lps_leave(hw);
1000                         ppsc->last_delaylps_stamp_jiffies = jiffies;
1001                 }
1002
1003                 return true;
1004         } else if (0x86DD == ether_type) {
1005                 return true;
1006         }
1007
1008 end:
1009         rtlpriv->ra.is_special_data = false;
1010         return false;
1011 }
1012
1013 /*********************************************************
1014  *
1015  * functions called by core.c
1016  *
1017  *********************************************************/
1018 int rtl_tx_agg_start(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1019                      struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1020 {
1021         struct rtl_priv *rtlpriv = rtl_priv(hw);
1022         struct rtl_tid_data *tid_data;
1023         struct rtl_sta_info *sta_entry = NULL;
1024
1025         if (sta == NULL)
1026                 return -EINVAL;
1027
1028         if (unlikely(tid >= MAX_TID_COUNT))
1029                 return -EINVAL;
1030
1031         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1032         if (!sta_entry)
1033                 return -ENXIO;
1034         tid_data = &sta_entry->tids[tid];
1035
1036         RT_TRACE(COMP_SEND, DBG_DMESG,
1037                  ("on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1038                   tid_data->seq_number));
1039
1040         *ssn = tid_data->seq_number;
1041         tid_data->agg.agg_state = RTL_AGG_START;
1042
1043         ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1044         return 0;
1045 }
1046
1047 int rtl_tx_agg_stop(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1048                     struct ieee80211_sta *sta, u16 tid)
1049 {
1050         struct rtl_priv *rtlpriv = rtl_priv(hw);
1051         struct rtl_tid_data *tid_data;
1052         struct rtl_sta_info *sta_entry = NULL;
1053
1054         if (sta == NULL)
1055                 return -EINVAL;
1056
1057         if (!sta->addr) {
1058                 RT_TRACE(COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
1059                 return -EINVAL;
1060         }
1061
1062         RT_TRACE(COMP_SEND, DBG_DMESG,
1063                  ("on ra = %pM tid = %d\n", sta->addr, tid));
1064
1065         if (unlikely(tid >= MAX_TID_COUNT))
1066                 return -EINVAL;
1067
1068         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1069         tid_data = &sta_entry->tids[tid];
1070         sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
1071
1072         ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1073         return 0;
1074 }
1075
1076 int rtl_rx_agg_start(struct ieee80211_hw *hw,
1077                      struct ieee80211_sta *sta, u16 tid)
1078 {
1079         struct rtl_priv *rtlpriv = rtl_priv(hw);
1080         struct rtl_tid_data *tid_data;
1081         struct rtl_sta_info *sta_entry = NULL;
1082
1083         if (sta == NULL)
1084                 return -EINVAL;
1085
1086         if (unlikely(tid >= MAX_TID_COUNT))
1087                 return -EINVAL;
1088
1089         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1090         if (!sta_entry)
1091                 return -ENXIO;
1092         tid_data = &sta_entry->tids[tid];
1093
1094         RT_TRACE(COMP_RECV, DBG_DMESG,
1095                  ("on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1096                  tid_data->seq_number));
1097
1098         tid_data->agg.rx_agg_state = RTL_RX_AGG_START;
1099         return 0;
1100 }
1101
1102 int rtl_rx_agg_stop(struct ieee80211_hw *hw,
1103                 struct ieee80211_sta *sta, u16 tid)
1104 {
1105         struct rtl_priv *rtlpriv = rtl_priv(hw);
1106         struct rtl_tid_data *tid_data;
1107         struct rtl_sta_info *sta_entry = NULL;
1108
1109         if (sta == NULL)
1110                 return -EINVAL;
1111
1112         if (!sta->addr) {
1113                 RT_TRACE(COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
1114                 return -EINVAL;
1115         }
1116
1117         RT_TRACE(COMP_SEND, DBG_DMESG,
1118                  ("on ra = %pM tid = %d\n", sta->addr, tid));
1119
1120         if (unlikely(tid >= MAX_TID_COUNT))
1121                 return -EINVAL;
1122
1123         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1124         tid_data = &sta_entry->tids[tid];
1125         sta_entry->tids[tid].agg.rx_agg_state = RTL_RX_AGG_STOP;
1126
1127         return 0;
1128 }
1129 int rtl_tx_agg_oper(struct ieee80211_hw *hw,
1130                 struct ieee80211_sta *sta, u16 tid)
1131 {
1132         struct rtl_priv *rtlpriv = rtl_priv(hw);
1133         struct rtl_tid_data *tid_data;
1134         struct rtl_sta_info *sta_entry = NULL;
1135
1136         if (sta == NULL)
1137                 return -EINVAL;
1138
1139         if (!sta->addr) {
1140                 RT_TRACE(COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
1141                 return -EINVAL;
1142         }
1143
1144         RT_TRACE(COMP_SEND, DBG_DMESG,
1145                  ("on ra = %pM tid = %d\n", sta->addr, tid));
1146
1147         if (unlikely(tid >= MAX_TID_COUNT))
1148                 return -EINVAL;
1149
1150         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1151         tid_data = &sta_entry->tids[tid];
1152         sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
1153
1154         return 0;
1155 }
1156
1157 /*********************************************************
1158  *
1159  * wq & timer callback functions
1160  *
1161  *********************************************************/
1162 /* this function is used for roaming */
1163 void rtl_beacon_statistic(struct ieee80211_hw *hw, struct sk_buff *skb)
1164 {
1165         struct rtl_priv *rtlpriv = rtl_priv(hw);
1166         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1167
1168         if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
1169                 return;
1170
1171         if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
1172                 return;
1173
1174         /* check if this really is a beacon */
1175         if (!ieee80211_is_beacon(hdr->frame_control) &&
1176                 !ieee80211_is_probe_resp(hdr->frame_control))
1177                 return;
1178
1179         /* min. beacon length + FCS_LEN */
1180         if (skb->len <= 40 + FCS_LEN)
1181                 return;
1182
1183         /* and only beacons from the associated BSSID, please */
1184         if (ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
1185                 return;
1186
1187         rtlpriv->link_info.bcn_rx_inperiod ++;
1188 }
1189
1190 void rtl_watchdog_wq_callback(void *data)
1191 {
1192         struct rtl_works *rtlworks = container_of_dwork_rtl(data,
1193                                                             struct rtl_works,
1194                                                             watchdog_wq);
1195         struct ieee80211_hw *hw = rtlworks->hw;
1196         struct rtl_priv *rtlpriv = rtl_priv(hw);
1197         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1198         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1199         bool b_busytraffic = false;
1200         bool b_tx_busy_traffic = false;
1201         bool b_rx_busy_traffic = false;
1202         bool b_higher_busytraffic = false;
1203         bool b_higher_busyrxtraffic = false;
1204         u8 idx, tid;
1205         u32 rx_cnt_inp4eriod = 0;
1206         u32 tx_cnt_inp4eriod = 0;
1207         u32 aver_rx_cnt_inperiod = 0;
1208         u32 aver_tx_cnt_inperiod = 0;
1209         u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
1210         u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
1211         bool benter_ps = false;
1212
1213         if (is_hal_stop(rtlhal))
1214                 return;
1215
1216         /* <1> Determine if action frame is allowed */
1217         if (mac->link_state > MAC80211_NOLINK) {
1218                 if (mac->cnt_after_linked < 20)
1219                         mac->cnt_after_linked++;
1220         } else {
1221                 mac->cnt_after_linked = 0;
1222         }
1223
1224         /* <2> to check if traffic busy, if
1225          * busytraffic we don't change channel */
1226         if (mac->link_state >= MAC80211_LINKED) {
1227
1228                 /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
1229                 for (idx = 0; idx <= 2; idx++) {
1230                         rtlpriv->link_info.num_rx_in4period[idx] =
1231                             rtlpriv->link_info.num_rx_in4period[idx + 1];
1232                         rtlpriv->link_info.num_tx_in4period[idx] =
1233                             rtlpriv->link_info.num_tx_in4period[idx + 1];
1234                 }
1235                 rtlpriv->link_info.num_rx_in4period[3] =
1236                     rtlpriv->link_info.num_rx_inperiod;
1237                 rtlpriv->link_info.num_tx_in4period[3] =
1238                     rtlpriv->link_info.num_tx_inperiod;
1239                 for (idx = 0; idx <= 3; idx++) {
1240                         rx_cnt_inp4eriod +=
1241                             rtlpriv->link_info.num_rx_in4period[idx];
1242                         tx_cnt_inp4eriod +=
1243                             rtlpriv->link_info.num_tx_in4period[idx];
1244                 }
1245                 aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
1246                 aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
1247
1248                 /* (2) check traffic busy */
1249                 if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100) {
1250                         b_busytraffic = true;
1251                         if (aver_rx_cnt_inperiod > aver_tx_cnt_inperiod)
1252                                 b_rx_busy_traffic = true;
1253                         else
1254                                 b_tx_busy_traffic = false;
1255                 }
1256
1257                 /* Higher Tx/Rx data. */
1258                 if (aver_rx_cnt_inperiod > 4000 ||
1259                         aver_tx_cnt_inperiod > 4000) {
1260                         b_higher_busytraffic = true;
1261
1262                         /* Extremely high Rx data. */
1263                         if (aver_rx_cnt_inperiod > 5000)
1264                                 b_higher_busyrxtraffic = true;
1265                 }
1266
1267                 /* check every tid's tx traffic */
1268                 for (tid = 0; tid <= 7; tid++) {
1269                         for (idx = 0; idx <= 2; idx++)
1270                                 rtlpriv->link_info.tidtx_in4period[tid][idx] =
1271                                         rtlpriv->link_info.tidtx_in4period[tid]
1272                                         [idx + 1];
1273                         rtlpriv->link_info.tidtx_in4period[tid][3] =
1274                                 rtlpriv->link_info.tidtx_inperiod[tid];
1275
1276                         for (idx = 0; idx <= 3; idx++)
1277                                 tidtx_inp4eriod[tid] +=
1278                                    rtlpriv->link_info.tidtx_in4period[tid][idx];
1279                         aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
1280                         if (aver_tidtx_inperiod[tid] > 5000)
1281                                 rtlpriv->link_info.higher_busytxtraffic[tid] =
1282                                                                         true;
1283                         else
1284                                 rtlpriv->link_info.higher_busytxtraffic[tid] =
1285                                                                         false;
1286                 }
1287
1288                 if (((rtlpriv->link_info.num_rx_inperiod +
1289                       rtlpriv->link_info.num_tx_inperiod) > 8) ||
1290                     (rtlpriv->link_info.num_rx_inperiod > 2))
1291                         benter_ps = false;
1292                 else
1293                         benter_ps = true;
1294
1295                 /* LeisurePS only work in infra mode. */
1296                 if (benter_ps)
1297                         rtl_lps_enter(hw);
1298                 else
1299                         rtl_lps_leave(hw);
1300         }
1301
1302         rtlpriv->link_info.num_rx_inperiod = 0;
1303         rtlpriv->link_info.num_tx_inperiod = 0;
1304         for (tid = 0; tid <= 7; tid++)
1305                 rtlpriv->link_info.tidtx_inperiod[tid] = 0;
1306
1307         rtlpriv->link_info.b_busytraffic = b_busytraffic;
1308         rtlpriv->link_info.b_rx_busy_traffic = b_rx_busy_traffic;
1309         rtlpriv->link_info.b_tx_busy_traffic = b_tx_busy_traffic;
1310         rtlpriv->link_info.b_higher_busytraffic = b_higher_busytraffic;
1311         rtlpriv->link_info.b_higher_busyrxtraffic = b_higher_busyrxtraffic;
1312
1313         /* <3> DM */
1314         rtlpriv->cfg->ops->dm_watchdog(hw);
1315
1316         /* <4> roaming */
1317         if (mac->link_state == MAC80211_LINKED &&
1318                         mac->opmode == NL80211_IFTYPE_STATION) {
1319                 if ((rtlpriv->link_info.bcn_rx_inperiod +
1320                         rtlpriv->link_info.num_rx_inperiod) == 0) {
1321                         rtlpriv->link_info.roam_times++;
1322                         RT_TRACE(COMP_ERR, DBG_DMESG, ("AP off for %d s\n",
1323                                 (rtlpriv->link_info.roam_times * 2)));
1324
1325                         /* if we can't recv beacon for 10s,
1326                          * we should reconnect this AP */
1327                         if (rtlpriv->link_info.roam_times >= 5) {
1328                                 RT_TRACE(COMP_ERR, DBG_EMERG,
1329                                          ("AP off, try to reconnect now\n"));
1330                                 rtlpriv->link_info.roam_times = 0;
1331                                 ieee80211_connection_loss(rtlpriv->mac80211.vif);
1332                         }
1333                 } else {
1334                         rtlpriv->link_info.roam_times = 0;
1335                 }
1336         }
1337         rtlpriv->link_info.bcn_rx_inperiod = 0;
1338 }
1339
1340 void rtl_watch_dog_timer_callback(unsigned long data)
1341 {
1342         struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1343         struct rtl_priv *rtlpriv = rtl_priv(hw);
1344
1345         queue_delayed_work(rtlpriv->works.rtl_wq,
1346                            &rtlpriv->works.watchdog_wq, 0);
1347
1348         mod_timer(&rtlpriv->works.watchdog_timer,
1349                   jiffies + MSECS(RTL_WATCH_DOG_TIME));
1350 }
1351 void rtl_fwevt_wq_callback(void *data)
1352 {
1353         struct rtl_works *rtlworks =
1354                 container_of_dwork_rtl(data, struct rtl_works, fwevt_wq);
1355         struct ieee80211_hw *hw = rtlworks->hw;
1356         struct rtl_priv *rtlpriv = rtl_priv(hw);
1357
1358         rtlpriv->cfg->ops->c2h_command_handle(hw);
1359 }
1360 void rtl_easy_concurrent_retrytimer_callback(unsigned long data)
1361 {
1362         struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1363         struct rtl_priv *rtlpriv = rtl_priv(hw);
1364         struct rtl_priv *buddy_priv = rtlpriv->buddy_priv;
1365
1366         if(buddy_priv == NULL)
1367                 return;
1368
1369         rtlpriv->cfg->ops->dualmac_easy_concurrent(hw);
1370 }
1371 /*********************************************************
1372  *
1373  * frame process functions
1374  *
1375  *********************************************************/
1376 u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
1377 {
1378         struct ieee80211_mgmt *mgmt = (void *)data;
1379         u8 *pos, *end;
1380
1381         pos = (u8 *)mgmt->u.beacon.variable;
1382         end = data + len;
1383         while (pos < end) {
1384                 if (pos + 2 + pos[1] > end)
1385                         return NULL;
1386
1387                 if (pos[0] == ie)
1388                         return pos;
1389
1390                 pos += 2 + pos[1];
1391         }
1392         return NULL;
1393 }
1394
1395 /* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
1396 /* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
1397 struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
1398                                      enum ieee80211_smps_mode smps,
1399                                      u8 *da, u8 *bssid)
1400 {
1401         struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1402         struct sk_buff *skb;
1403         struct ieee80211_mgmt_compat *action_frame;
1404
1405         /* 27 = header + category + action + smps mode */
1406         skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
1407         if (!skb)
1408                 return NULL;
1409
1410         skb_reserve(skb, hw->extra_tx_headroom);
1411         action_frame = (void *)skb_put(skb, 27);
1412         memset(action_frame, 0, 27);
1413         memcpy(action_frame->da, da, ETH_ALEN);
1414         memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
1415         memcpy(action_frame->bssid, bssid, ETH_ALEN);
1416         action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1417                                                   IEEE80211_STYPE_ACTION);
1418         action_frame->u.action.category = WLAN_CATEGORY_HT;
1419         action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
1420         switch (smps) {
1421         case IEEE80211_SMPS_AUTOMATIC:/* 0 */
1422         case IEEE80211_SMPS_NUM_MODES:/* 4 */
1423                 WARN_ON(1);
1424         case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
1425                 action_frame->u.action.u.ht_smps.smps_control =
1426                                 WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
1427                 break;
1428         case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
1429                 action_frame->u.action.u.ht_smps.smps_control =
1430                                 WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
1431                 break;
1432         case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
1433                 action_frame->u.action.u.ht_smps.smps_control =
1434                                 WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
1435                 break;
1436         }
1437
1438         return skb;
1439 }
1440
1441 int rtl_send_smps_action(struct ieee80211_hw *hw,
1442                          struct ieee80211_sta *sta,
1443                          enum ieee80211_smps_mode smps)
1444 {
1445         struct rtl_priv *rtlpriv = rtl_priv(hw);
1446         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1447         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1448         struct sk_buff *skb = NULL;
1449         struct rtl_tcb_desc tcb_desc;
1450         u8 bssid[ETH_ALEN] = {0};
1451
1452         memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
1453
1454         if (rtlpriv->mac80211.act_scanning)
1455                 goto err_free;
1456
1457         if (!sta)
1458                 goto err_free;
1459
1460         if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
1461                 goto err_free;
1462
1463         if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1464                 goto err_free;
1465
1466         if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP)
1467                 memcpy(bssid, rtlpriv->efuse.dev_addr, ETH_ALEN);
1468         else
1469                 memcpy(bssid, rtlpriv->mac80211.bssid, ETH_ALEN);
1470
1471         skb = rtl_make_smps_action(hw, smps, sta->addr, bssid);
1472         /* this is a type = mgmt * stype = action frame */
1473         if (skb) {
1474                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1475                 struct rtl_sta_info *sta_entry =
1476                         (struct rtl_sta_info *) sta->drv_priv;
1477                 sta_entry->mimo_ps = smps;
1478                 /* rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0); */
1479
1480                 info->control.rates[0].idx = 0;
1481 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0))
1482                 info->band = hw->conf.chandef.chan->band;
1483 #else
1484                 info->band = hw->conf.channel->band;
1485 #endif
1486 /*<delete in kernel start>*/
1487 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0))
1488                 info->control.sta = sta;
1489                 rtlpriv->intf_ops->adapter_tx(hw, skb, &tcb_desc);
1490 #else
1491 /*<delete in kernel end>*/
1492                 rtlpriv->intf_ops->adapter_tx(hw, sta, skb, &tcb_desc);
1493 /*<delete in kernel start>*/
1494 #endif
1495 /*<delete in kernel end>*/
1496         }
1497         return 1;
1498
1499 err_free:
1500         return 0;
1501 }
1502 //EXPORT_SYMBOL(rtl_send_smps_action);
1503
1504 /* because mac80211 have issues when can receive del ba
1505  * so here we just make a fake del_ba if we receive a ba_req
1506  * but rx_agg was opened to let mac80211 release some ba
1507  * related resources, so please this del_ba for tx */
1508 struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw,
1509                                 u8 *sa, u8 *bssid, u16 tid)
1510 {
1511         struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1512         struct sk_buff *skb;
1513         struct ieee80211_mgmt *action_frame;
1514         u16 params;
1515
1516         /* 27 = header + category + action + smps mode */
1517         skb = dev_alloc_skb(34 + hw->extra_tx_headroom);
1518         if (!skb)
1519                 return NULL;
1520
1521         skb_reserve(skb, hw->extra_tx_headroom);
1522         action_frame = (void *)skb_put(skb, 34);
1523         memset(action_frame, 0, 34);
1524         memcpy(action_frame->sa, sa, ETH_ALEN);
1525         memcpy(action_frame->da, rtlefuse->dev_addr, ETH_ALEN);
1526         memcpy(action_frame->bssid, bssid, ETH_ALEN);
1527         action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1528                                                   IEEE80211_STYPE_ACTION);
1529         action_frame->u.action.category = WLAN_CATEGORY_BACK;
1530         action_frame->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1531         params = (u16)(1 << 11);        /* bit 11 initiator */
1532         params |= (u16)(tid << 12);             /* bit 15:12 TID number */
1533
1534         action_frame->u.action.u.delba.params = cpu_to_le16(params);
1535         action_frame->u.action.u.delba.reason_code =
1536                 cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
1537
1538         return skb;
1539 }
1540
1541 /*********************************************************
1542  *
1543  * IOT functions
1544  *
1545  *********************************************************/
1546 static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
1547                                   struct octet_string vendor_ie)
1548 {
1549         struct rtl_priv *rtlpriv = rtl_priv(hw);
1550         bool matched = false;
1551         static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
1552         static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
1553         static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
1554         static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
1555         static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
1556         static u8 racap[] = { 0x00, 0x0c, 0x43 };
1557         static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
1558         static u8 marvcap[] = { 0x00, 0x50, 0x43 };
1559
1560         if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
1561                 memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
1562                 rtlpriv->mac80211.vendor = PEER_ATH;
1563                 matched = true;
1564         } else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
1565                 memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
1566                 memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
1567                 rtlpriv->mac80211.vendor = PEER_BROAD;
1568                 matched = true;
1569         } else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
1570                 rtlpriv->mac80211.vendor = PEER_RAL;
1571                 matched = true;
1572         } else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
1573                 rtlpriv->mac80211.vendor = PEER_CISCO;
1574                 matched = true;
1575         } else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
1576                 rtlpriv->mac80211.vendor = PEER_MARV;
1577                 matched = true;
1578         }
1579
1580         return matched;
1581 }
1582
1583 bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
1584                 unsigned int len)
1585 {
1586         struct ieee80211_mgmt *mgmt = (void *)data;
1587         struct octet_string vendor_ie;
1588         u8 *pos, *end;
1589
1590         pos = (u8 *)mgmt->u.beacon.variable;
1591         end = data + len;
1592         while (pos < end) {
1593                 if (pos[0] == 221) {
1594                         vendor_ie.length = pos[1];
1595                         vendor_ie.octet = &pos[2];
1596                         if (rtl_chk_vendor_ouisub(hw, vendor_ie))
1597                                 return true;
1598                 }
1599
1600                 if (pos + 2 + pos[1] > end)
1601                         return false;
1602
1603                 pos += 2 + pos[1];
1604         }
1605         return false;
1606 }
1607
1608 void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
1609 {
1610         struct rtl_priv *rtlpriv = rtl_priv(hw);
1611         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1612         struct ieee80211_hdr *hdr = (void *)data;
1613         u32 vendor = PEER_UNKNOWN;
1614
1615         static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
1616         static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
1617         static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
1618         static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
1619         static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
1620         static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
1621         static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
1622         static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
1623         static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
1624         static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
1625         static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
1626         static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
1627         static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
1628         static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
1629         static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
1630         static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
1631
1632         if (mac->opmode != NL80211_IFTYPE_STATION)
1633                 return;
1634
1635         if (mac->link_state == MAC80211_NOLINK) {
1636                 mac->vendor = PEER_UNKNOWN;
1637                 return;
1638         }
1639
1640         if (mac->cnt_after_linked > 2)
1641                 return;
1642
1643         /* check if this really is a beacon */
1644         if (!ieee80211_is_beacon(hdr->frame_control))
1645                 return;
1646
1647         /* min. beacon length + FCS_LEN */
1648         if (len <= 40 + FCS_LEN)
1649                 return;
1650
1651         /* and only beacons from the associated BSSID, please */
1652         if (ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
1653                 return;
1654
1655         if (rtl_find_221_ie(hw, data, len)) {
1656                 vendor = mac->vendor;
1657         }
1658
1659         if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
1660                 (memcmp(mac->bssid, ap5_2, 3) == 0) ||
1661                 (memcmp(mac->bssid, ap5_3, 3) == 0) ||
1662                 (memcmp(mac->bssid, ap5_4, 3) == 0) ||
1663                 (memcmp(mac->bssid, ap5_5, 3) == 0) ||
1664                 (memcmp(mac->bssid, ap5_6, 3) == 0) ||
1665                 vendor == PEER_ATH) {
1666                 vendor = PEER_ATH;
1667                 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>ath find\n"));
1668         } else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
1669                 (memcmp(mac->bssid, ap4_5, 3) == 0) ||
1670                 (memcmp(mac->bssid, ap4_1, 3) == 0) ||
1671                 (memcmp(mac->bssid, ap4_2, 3) == 0) ||
1672                 (memcmp(mac->bssid, ap4_3, 3) == 0) ||
1673                 vendor == PEER_RAL) {
1674                 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>ral findn\n"));
1675                 vendor = PEER_RAL;
1676         } else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
1677                 vendor == PEER_CISCO) {
1678                 vendor = PEER_CISCO;
1679                 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>cisco find\n"));
1680         } else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
1681                 (memcmp(mac->bssid, ap3_2, 3) == 0) ||
1682                 (memcmp(mac->bssid, ap3_3, 3) == 0) ||
1683                 vendor == PEER_BROAD) {
1684                 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>broad find\n"));
1685                 vendor = PEER_BROAD;
1686         } else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
1687                 vendor == PEER_MARV) {
1688                 vendor = PEER_MARV;
1689                 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>marv find\n"));
1690         }
1691
1692         mac->vendor = vendor;
1693 }
1694
1695 /*********************************************************
1696  *
1697  * sysfs functions
1698  *
1699  *********************************************************/
1700 static ssize_t rtl_show_debug_level(struct device *d,
1701                                     struct device_attribute *attr, char *buf)
1702 {
1703         struct ieee80211_hw *hw = dev_get_drvdata(d);
1704         struct rtl_priv *rtlpriv = rtl_priv(hw);
1705
1706         return sprintf(buf, "0x%08X\n", rtlpriv->dbg.global_debuglevel);
1707 }
1708
1709 static ssize_t rtl_store_debug_level(struct device *d,
1710                                      struct device_attribute *attr,
1711                                      const char *buf, size_t count)
1712 {
1713         struct ieee80211_hw *hw = dev_get_drvdata(d);
1714         struct rtl_priv *rtlpriv = rtl_priv(hw);
1715         unsigned long val;
1716         int ret;
1717
1718         ret = strict_strtoul(buf, 0, &val);
1719         if (ret) {
1720                 printk(KERN_DEBUG "%s is not in hex or decimal form.\n", buf);
1721         } else {
1722                 rtlpriv->dbg.global_debuglevel = val;
1723                 printk(KERN_DEBUG "debuglevel:%x\n",
1724                         rtlpriv->dbg.global_debuglevel);
1725         }
1726
1727         return strnlen(buf, count);
1728 }
1729
1730 static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
1731                    rtl_show_debug_level, rtl_store_debug_level);
1732
1733 static struct attribute *rtl_sysfs_entries[] = {
1734
1735         &dev_attr_debug_level.attr,
1736
1737         NULL
1738 };
1739
1740 /*
1741  * "name" is folder name witch will be
1742  * put in device directory like :
1743  * sys/devices/pci0000:00/0000:00:1c.4/
1744  * 0000:06:00.0/rtl_sysfs
1745  */
1746 struct attribute_group rtl_attribute_group = {
1747         .name = "rtlsysfs",
1748         .attrs = rtl_sysfs_entries,
1749 };
1750
1751 #ifdef VIF_TODO
1752 /*********************************************************
1753  *
1754  * vif functions
1755  *
1756  *********************************************************/
1757 static inline struct ieee80211_vif *
1758 rtl_get_vif(struct rtl_vif_info *vif_priv)
1759 {
1760         return container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
1761 }
1762
1763 /* Protected by ar->mutex or RCU */
1764 struct ieee80211_vif *rtl_get_main_vif(struct ieee80211_hw *hw)
1765 {
1766         struct rtl_priv *rtlpriv = rtl_priv(hw);
1767         struct rtl_vif_info *cvif;
1768
1769         list_for_each_entry_rcu(cvif, &rtlpriv->vif_priv.vif_list, list) {
1770                 if (cvif->active)
1771                         return rtl_get_vif(cvif);
1772         }
1773
1774         return NULL;
1775 }
1776
1777 static inline bool is_main_vif(struct ieee80211_hw *hw,
1778                                struct ieee80211_vif *vif)
1779 {
1780         bool ret;
1781
1782         rcu_read_lock();
1783         ret = (rtl_get_main_vif(hw) == vif);
1784         rcu_read_unlock();
1785         return ret;
1786 }
1787
1788 bool rtl_set_vif_info(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1789 {
1790         struct rtl_vif_info *vif_info = (void *) vif->drv_priv;
1791         struct rtl_priv *rtlpriv = rtl_priv(hw);
1792         int vif_id = -1;
1793
1794         if (rtlpriv->vif_priv.vifs >= MAX_VIRTUAL_MAC) {
1795                 RT_TRACE(COMP_ERR, DBG_WARNING,
1796                          ("vif number can not bigger than %d, now vifs is:%d\n",
1797                           MAX_VIRTUAL_MAC, rtlpriv->vif_priv.vifs));
1798                 return false;
1799         }
1800
1801         rcu_read_lock();
1802         vif_id = bitmap_find_free_region(&rtlpriv->vif_priv.vif_bitmap,
1803                                          MAX_VIRTUAL_MAC, 0);
1804         RT_TRACE(COMP_MAC80211, DBG_DMESG,
1805                  ("%s vid_id:%d\n", __func__, vif_id));
1806
1807         if (vif_id < 0) {
1808                 rcu_read_unlock();
1809                 return false;
1810         }
1811
1812         BUG_ON(rtlpriv->vif_priv.vif[vif_id].id != vif_id);
1813         vif_info->active = true;
1814         vif_info->id = vif_id;
1815         vif_info->enable_beacon = false;
1816         rtlpriv->vif_priv.vifs++;
1817         if (rtlpriv->vif_priv.vifs > 1) {
1818                 rtlpriv->psc.b_inactiveps = false;
1819                 rtlpriv->psc.b_swctrl_lps = false;
1820                 rtlpriv->psc.b_fwctrl_lps = false;
1821         }
1822
1823         list_add_tail_rcu(&vif_info->list, &rtlpriv->vif_priv.vif_list);
1824         rcu_assign_pointer(rtlpriv->vif_priv.vif[vif_id].vif, vif);
1825
1826         RT_TRACE(COMP_MAC80211, DBG_DMESG, ("vifaddress:%p %p %p\n",
1827                  rtlpriv->vif_priv.vif[vif_id].vif, vif, rtl_get_main_vif(hw)));
1828
1829         rcu_read_unlock();
1830
1831         return true;
1832 }
1833 #endif
1834
1835
1836 #if 0
1837 MODULE_AUTHOR("lizhaoming       <chaoming_li@realsil.com.cn>");
1838 MODULE_AUTHOR("Realtek WlanFAE  <wlanfae@realtek.com>");
1839 MODULE_AUTHOR("Larry Finger     <Larry.FInger@lwfinger.net>");
1840 MODULE_LICENSE("GPL");
1841 MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
1842 #endif
1843 struct rtl_global_var global_var = {};
1844
1845 int rtl_core_module_init(void)
1846 {
1847         if (rtl_rate_control_register())
1848                 printk(KERN_DEBUG "rtl: Unable to register rtl_rc,"
1849                           "use default RC !!\n");
1850
1851         /* add proc for debug */
1852         rtl_proc_add_topdir();
1853
1854         /* init some global vars */
1855         INIT_LIST_HEAD(&global_var.glb_priv_list);
1856         spin_lock_init(&global_var.glb_list_lock);
1857
1858         return 0;
1859 }
1860
1861 void rtl_core_module_exit(void)
1862 {
1863         /*RC*/
1864         rtl_rate_control_unregister();
1865
1866         /* add proc for debug */
1867         rtl_proc_remove_topdir();
1868 }
1869
1870 #if 0
1871 module_init(rtl_core_module_init);
1872 module_exit(rtl_core_module_exit);
1873 #endif