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