Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-2.6.git] / drivers / net / wireless / rtlwifi / 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 "wifi.h"
32 #include "rc.h"
33 #include "base.h"
34 #include "efuse.h"
35 #include "cam.h"
36 #include "ps.h"
37 #include "regd.h"
38
39 /*
40  *NOTICE!!!: This file will be very big, we hsould
41  *keep it clear under follwing roles:
42  *
43  *This file include follwing part, so, if you add new
44  *functions into this file, please check which part it
45  *should includes. or check if you should add new part
46  *for this file:
47  *
48  *1) mac80211 init functions
49  *2) tx information functions
50  *3) functions called by core.c
51  *4) wq & timer callback functions
52  *5) frame process functions
53  *6) IOT functions
54  *7) sysfs functions
55  *8) ...
56  */
57
58 /*********************************************************
59  *
60  * mac80211 init functions
61  *
62  *********************************************************/
63 static struct ieee80211_channel rtl_channeltable_2g[] = {
64         {.center_freq = 2412, .hw_value = 1,},
65         {.center_freq = 2417, .hw_value = 2,},
66         {.center_freq = 2422, .hw_value = 3,},
67         {.center_freq = 2427, .hw_value = 4,},
68         {.center_freq = 2432, .hw_value = 5,},
69         {.center_freq = 2437, .hw_value = 6,},
70         {.center_freq = 2442, .hw_value = 7,},
71         {.center_freq = 2447, .hw_value = 8,},
72         {.center_freq = 2452, .hw_value = 9,},
73         {.center_freq = 2457, .hw_value = 10,},
74         {.center_freq = 2462, .hw_value = 11,},
75         {.center_freq = 2467, .hw_value = 12,},
76         {.center_freq = 2472, .hw_value = 13,},
77         {.center_freq = 2484, .hw_value = 14,},
78 };
79
80 static struct ieee80211_channel rtl_channeltable_5g[] = {
81         {.center_freq = 5180, .hw_value = 36,},
82         {.center_freq = 5200, .hw_value = 40,},
83         {.center_freq = 5220, .hw_value = 44,},
84         {.center_freq = 5240, .hw_value = 48,},
85         {.center_freq = 5260, .hw_value = 52,},
86         {.center_freq = 5280, .hw_value = 56,},
87         {.center_freq = 5300, .hw_value = 60,},
88         {.center_freq = 5320, .hw_value = 64,},
89         {.center_freq = 5500, .hw_value = 100,},
90         {.center_freq = 5520, .hw_value = 104,},
91         {.center_freq = 5540, .hw_value = 108,},
92         {.center_freq = 5560, .hw_value = 112,},
93         {.center_freq = 5580, .hw_value = 116,},
94         {.center_freq = 5600, .hw_value = 120,},
95         {.center_freq = 5620, .hw_value = 124,},
96         {.center_freq = 5640, .hw_value = 128,},
97         {.center_freq = 5660, .hw_value = 132,},
98         {.center_freq = 5680, .hw_value = 136,},
99         {.center_freq = 5700, .hw_value = 140,},
100         {.center_freq = 5745, .hw_value = 149,},
101         {.center_freq = 5765, .hw_value = 153,},
102         {.center_freq = 5785, .hw_value = 157,},
103         {.center_freq = 5805, .hw_value = 161,},
104         {.center_freq = 5825, .hw_value = 165,},
105 };
106
107 static struct ieee80211_rate rtl_ratetable_2g[] = {
108         {.bitrate = 10, .hw_value = 0x00,},
109         {.bitrate = 20, .hw_value = 0x01,},
110         {.bitrate = 55, .hw_value = 0x02,},
111         {.bitrate = 110, .hw_value = 0x03,},
112         {.bitrate = 60, .hw_value = 0x04,},
113         {.bitrate = 90, .hw_value = 0x05,},
114         {.bitrate = 120, .hw_value = 0x06,},
115         {.bitrate = 180, .hw_value = 0x07,},
116         {.bitrate = 240, .hw_value = 0x08,},
117         {.bitrate = 360, .hw_value = 0x09,},
118         {.bitrate = 480, .hw_value = 0x0a,},
119         {.bitrate = 540, .hw_value = 0x0b,},
120 };
121
122 static struct ieee80211_rate rtl_ratetable_5g[] = {
123         {.bitrate = 60, .hw_value = 0x04,},
124         {.bitrate = 90, .hw_value = 0x05,},
125         {.bitrate = 120, .hw_value = 0x06,},
126         {.bitrate = 180, .hw_value = 0x07,},
127         {.bitrate = 240, .hw_value = 0x08,},
128         {.bitrate = 360, .hw_value = 0x09,},
129         {.bitrate = 480, .hw_value = 0x0a,},
130         {.bitrate = 540, .hw_value = 0x0b,},
131 };
132
133 static const struct ieee80211_supported_band rtl_band_2ghz = {
134         .band = IEEE80211_BAND_2GHZ,
135
136         .channels = rtl_channeltable_2g,
137         .n_channels = ARRAY_SIZE(rtl_channeltable_2g),
138
139         .bitrates = rtl_ratetable_2g,
140         .n_bitrates = ARRAY_SIZE(rtl_ratetable_2g),
141
142         .ht_cap = {0},
143 };
144
145 static struct ieee80211_supported_band rtl_band_5ghz = {
146         .band = IEEE80211_BAND_5GHZ,
147
148         .channels = rtl_channeltable_5g,
149         .n_channels = ARRAY_SIZE(rtl_channeltable_5g),
150
151         .bitrates = rtl_ratetable_5g,
152         .n_bitrates = ARRAY_SIZE(rtl_ratetable_5g),
153
154         .ht_cap = {0},
155 };
156
157 static const u8 tid_to_ac[] = {
158         2, /* IEEE80211_AC_BE */
159         3, /* IEEE80211_AC_BK */
160         3, /* IEEE80211_AC_BK */
161         2, /* IEEE80211_AC_BE */
162         1, /* IEEE80211_AC_VI */
163         1, /* IEEE80211_AC_VI */
164         0, /* IEEE80211_AC_VO */
165         0, /* IEEE80211_AC_VO */
166 };
167
168 u8 rtl_tid_to_ac(struct ieee80211_hw *hw, u8 tid)
169 {
170         return tid_to_ac[tid];
171 }
172
173 static void _rtl_init_hw_ht_capab(struct ieee80211_hw *hw,
174                                   struct ieee80211_sta_ht_cap *ht_cap)
175 {
176         struct rtl_priv *rtlpriv = rtl_priv(hw);
177         struct rtl_phy *rtlphy = &(rtlpriv->phy);
178
179         ht_cap->ht_supported = true;
180         ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
181             IEEE80211_HT_CAP_SGI_40 |
182             IEEE80211_HT_CAP_SGI_20 |
183             IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU;
184
185         if (rtlpriv->rtlhal.disable_amsdu_8k)
186                 ht_cap->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
187
188         /*
189          *Maximum length of AMPDU that the STA can receive.
190          *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
191          */
192         ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
193
194         /*Minimum MPDU start spacing , */
195         ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
196
197         ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
198
199         /*
200          *hw->wiphy->bands[IEEE80211_BAND_2GHZ]
201          *base on ant_num
202          *rx_mask: RX mask
203          *if rx_ant =1 rx_mask[0]=0xff;==>MCS0-MCS7
204          *if rx_ant =2 rx_mask[1]=0xff;==>MCS8-MCS15
205          *if rx_ant >=3 rx_mask[2]=0xff;
206          *if BW_40 rx_mask[4]=0x01;
207          *highest supported RX rate
208          */
209         if (get_rf_type(rtlphy) == RF_1T2R || get_rf_type(rtlphy) == RF_2T2R) {
210
211                 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("1T2R or 2T2R\n"));
212
213                 ht_cap->mcs.rx_mask[0] = 0xFF;
214                 ht_cap->mcs.rx_mask[1] = 0xFF;
215                 ht_cap->mcs.rx_mask[4] = 0x01;
216
217                 ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
218         } else if (get_rf_type(rtlphy) == RF_1T1R) {
219
220                 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("1T1R\n"));
221
222                 ht_cap->mcs.rx_mask[0] = 0xFF;
223                 ht_cap->mcs.rx_mask[1] = 0x00;
224                 ht_cap->mcs.rx_mask[4] = 0x01;
225
226                 ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
227         }
228 }
229
230 static void _rtl_init_mac80211(struct ieee80211_hw *hw)
231 {
232         struct rtl_priv *rtlpriv = rtl_priv(hw);
233         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
234         struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
235         struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
236         struct ieee80211_supported_band *sband;
237
238
239         if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY && rtlhal->bandset ==
240             BAND_ON_BOTH) {
241                 /* 1: 2.4 G bands */
242                 /* <1> use  mac->bands as mem for hw->wiphy->bands */
243                 sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
244
245                 /* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
246                  * to default value(1T1R) */
247                 memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]), &rtl_band_2ghz,
248                                 sizeof(struct ieee80211_supported_band));
249
250                 /* <3> init ht cap base on ant_num */
251                 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
252
253                 /* <4> set mac->sband to wiphy->sband */
254                 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
255
256                 /* 2: 5 G bands */
257                 /* <1> use  mac->bands as mem for hw->wiphy->bands */
258                 sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
259
260                 /* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
261                  * to default value(1T1R) */
262                 memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]), &rtl_band_5ghz,
263                                 sizeof(struct ieee80211_supported_band));
264
265                 /* <3> init ht cap base on ant_num */
266                 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
267
268                 /* <4> set mac->sband to wiphy->sband */
269                 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
270         } else {
271                 if (rtlhal->current_bandtype == BAND_ON_2_4G) {
272                         /* <1> use  mac->bands as mem for hw->wiphy->bands */
273                         sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
274
275                         /* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
276                          * to default value(1T1R) */
277                         memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]),
278                                  &rtl_band_2ghz,
279                                  sizeof(struct ieee80211_supported_band));
280
281                         /* <3> init ht cap base on ant_num */
282                         _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
283
284                         /* <4> set mac->sband to wiphy->sband */
285                         hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
286                 } else if (rtlhal->current_bandtype == BAND_ON_5G) {
287                         /* <1> use  mac->bands as mem for hw->wiphy->bands */
288                         sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
289
290                         /* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
291                          * to default value(1T1R) */
292                         memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]),
293                                  &rtl_band_5ghz,
294                                  sizeof(struct ieee80211_supported_band));
295
296                         /* <3> init ht cap base on ant_num */
297                         _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
298
299                         /* <4> set mac->sband to wiphy->sband */
300                         hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
301                 } else {
302                         RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG,
303                                  ("Err BAND %d\n",
304                                  rtlhal->current_bandtype));
305                 }
306         }
307         /* <5> set hw caps */
308         hw->flags = IEEE80211_HW_SIGNAL_DBM |
309             IEEE80211_HW_RX_INCLUDES_FCS |
310             IEEE80211_HW_BEACON_FILTER |
311             IEEE80211_HW_AMPDU_AGGREGATION |
312             IEEE80211_HW_REPORTS_TX_ACK_STATUS | 0;
313
314         /* swlps or hwlps has been set in diff chip in init_sw_vars */
315         if (rtlpriv->psc.swctrl_lps)
316                 hw->flags |= IEEE80211_HW_SUPPORTS_PS |
317                         IEEE80211_HW_PS_NULLFUNC_STACK |
318                         /* IEEE80211_HW_SUPPORTS_DYNAMIC_PS | */
319                         0;
320
321         hw->wiphy->interface_modes =
322             BIT(NL80211_IFTYPE_AP) |
323             BIT(NL80211_IFTYPE_STATION) |
324             BIT(NL80211_IFTYPE_ADHOC);
325
326         hw->wiphy->rts_threshold = 2347;
327
328         hw->queues = AC_MAX;
329         hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;
330
331         /* TODO: Correct this value for our hw */
332         /* TODO: define these hard code value */
333         hw->channel_change_time = 100;
334         hw->max_listen_interval = 10;
335         hw->max_rate_tries = 4;
336         /* hw->max_rates = 1; */
337         hw->sta_data_size = sizeof(struct rtl_sta_info);
338
339         /* <6> mac address */
340         if (is_valid_ether_addr(rtlefuse->dev_addr)) {
341                 SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
342         } else {
343                 u8 rtlmac[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
344                 get_random_bytes((rtlmac + (ETH_ALEN - 1)), 1);
345                 SET_IEEE80211_PERM_ADDR(hw, rtlmac);
346         }
347
348 }
349
350 static void _rtl_init_deferred_work(struct ieee80211_hw *hw)
351 {
352         struct rtl_priv *rtlpriv = rtl_priv(hw);
353
354         /* <1> timer */
355         init_timer(&rtlpriv->works.watchdog_timer);
356         setup_timer(&rtlpriv->works.watchdog_timer,
357                     rtl_watch_dog_timer_callback, (unsigned long)hw);
358
359         /* <2> work queue */
360         rtlpriv->works.hw = hw;
361         rtlpriv->works.rtl_wq = alloc_workqueue(rtlpriv->cfg->name, 0, 0);
362         INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
363                           (void *)rtl_watchdog_wq_callback);
364         INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
365                           (void *)rtl_ips_nic_off_wq_callback);
366         INIT_DELAYED_WORK(&rtlpriv->works.ps_work,
367                           (void *)rtl_swlps_wq_callback);
368         INIT_DELAYED_WORK(&rtlpriv->works.ps_rfon_wq,
369                           (void *)rtl_swlps_rfon_wq_callback);
370
371 }
372
373 void rtl_deinit_deferred_work(struct ieee80211_hw *hw)
374 {
375         struct rtl_priv *rtlpriv = rtl_priv(hw);
376
377         del_timer_sync(&rtlpriv->works.watchdog_timer);
378
379         cancel_delayed_work(&rtlpriv->works.watchdog_wq);
380         cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
381         cancel_delayed_work(&rtlpriv->works.ps_work);
382         cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
383 }
384
385 void rtl_init_rfkill(struct ieee80211_hw *hw)
386 {
387         struct rtl_priv *rtlpriv = rtl_priv(hw);
388
389         bool radio_state;
390         bool blocked;
391         u8 valid = 0;
392
393         /*set init state to on */
394         rtlpriv->rfkill.rfkill_state = 1;
395         wiphy_rfkill_set_hw_state(hw->wiphy, 0);
396
397         radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
398
399         if (valid) {
400                 printk(KERN_INFO "rtlwifi: wireless switch is %s\n",
401                                 rtlpriv->rfkill.rfkill_state ? "on" : "off");
402
403                 rtlpriv->rfkill.rfkill_state = radio_state;
404
405                 blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
406                 wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
407         }
408
409         wiphy_rfkill_start_polling(hw->wiphy);
410 }
411
412 void rtl_deinit_rfkill(struct ieee80211_hw *hw)
413 {
414         wiphy_rfkill_stop_polling(hw->wiphy);
415 }
416
417 int rtl_init_core(struct ieee80211_hw *hw)
418 {
419         struct rtl_priv *rtlpriv = rtl_priv(hw);
420         struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
421
422         /* <1> init mac80211 */
423         _rtl_init_mac80211(hw);
424         rtlmac->hw = hw;
425
426         /* <2> rate control register */
427         hw->rate_control_algorithm = "rtl_rc";
428
429         /*
430          * <3> init CRDA must come after init
431          * mac80211 hw  in _rtl_init_mac80211.
432          */
433         if (rtl_regd_init(hw, rtl_reg_notifier)) {
434                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, ("REGD init failed\n"));
435                 return 1;
436         } else {
437                 /* CRDA regd hint must after init CRDA */
438                 if (regulatory_hint(hw->wiphy, rtlpriv->regd.alpha2)) {
439                         RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
440                                  ("regulatory_hint fail\n"));
441                 }
442         }
443
444         /* <4> locks */
445         mutex_init(&rtlpriv->locks.conf_mutex);
446         spin_lock_init(&rtlpriv->locks.ips_lock);
447         spin_lock_init(&rtlpriv->locks.irq_th_lock);
448         spin_lock_init(&rtlpriv->locks.h2c_lock);
449         spin_lock_init(&rtlpriv->locks.rf_ps_lock);
450         spin_lock_init(&rtlpriv->locks.rf_lock);
451         spin_lock_init(&rtlpriv->locks.lps_lock);
452         spin_lock_init(&rtlpriv->locks.waitq_lock);
453         spin_lock_init(&rtlpriv->locks.cck_and_rw_pagea_lock);
454
455         rtlmac->link_state = MAC80211_NOLINK;
456
457         /* <5> init deferred work */
458         _rtl_init_deferred_work(hw);
459
460         return 0;
461 }
462
463 void rtl_deinit_core(struct ieee80211_hw *hw)
464 {
465 }
466
467 void rtl_init_rx_config(struct ieee80211_hw *hw)
468 {
469         struct rtl_priv *rtlpriv = rtl_priv(hw);
470         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
471
472         rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
473 }
474
475 /*********************************************************
476  *
477  * tx information functions
478  *
479  *********************************************************/
480 static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
481                                           struct rtl_tcb_desc *tcb_desc,
482                                           struct ieee80211_tx_info *info)
483 {
484         struct rtl_priv *rtlpriv = rtl_priv(hw);
485         u8 rate_flag = info->control.rates[0].flags;
486
487         tcb_desc->use_shortpreamble = false;
488
489         /* 1M can only use Long Preamble. 11B spec */
490         if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
491                 return;
492         else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
493                 tcb_desc->use_shortpreamble = true;
494
495         return;
496 }
497
498 static void _rtl_query_shortgi(struct ieee80211_hw *hw,
499                                struct ieee80211_sta *sta,
500                                struct rtl_tcb_desc *tcb_desc,
501                                struct ieee80211_tx_info *info)
502 {
503         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
504         u8 rate_flag = info->control.rates[0].flags;
505         u8 sgi_40 = 0, sgi_20 = 0, bw_40 = 0;
506         tcb_desc->use_shortgi = false;
507
508         if (sta == NULL)
509                 return;
510
511         sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
512         sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
513
514         if (!(sta->ht_cap.ht_supported))
515                 return;
516
517         if (!sgi_40 && !sgi_20)
518                 return;
519
520         if (mac->opmode == NL80211_IFTYPE_STATION)
521                 bw_40 = mac->bw_40;
522         else if (mac->opmode == NL80211_IFTYPE_AP ||
523                 mac->opmode == NL80211_IFTYPE_ADHOC)
524                 bw_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
525
526         if (bw_40 && sgi_40)
527                 tcb_desc->use_shortgi = true;
528         else if ((bw_40 == false) && sgi_20)
529                 tcb_desc->use_shortgi = true;
530
531         if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
532                 tcb_desc->use_shortgi = false;
533 }
534
535 static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
536                                        struct rtl_tcb_desc *tcb_desc,
537                                        struct ieee80211_tx_info *info)
538 {
539         struct rtl_priv *rtlpriv = rtl_priv(hw);
540         u8 rate_flag = info->control.rates[0].flags;
541
542         /* Common Settings */
543         tcb_desc->rts_stbc = false;
544         tcb_desc->cts_enable = false;
545         tcb_desc->rts_sc = 0;
546         tcb_desc->rts_bw = false;
547         tcb_desc->rts_use_shortpreamble = false;
548         tcb_desc->rts_use_shortgi = false;
549
550         if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
551                 /* Use CTS-to-SELF in protection mode. */
552                 tcb_desc->rts_enable = true;
553                 tcb_desc->cts_enable = true;
554                 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
555         } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
556                 /* Use RTS-CTS in protection mode. */
557                 tcb_desc->rts_enable = true;
558                 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
559         }
560 }
561
562 static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
563                                    struct ieee80211_sta *sta,
564                                    struct rtl_tcb_desc *tcb_desc)
565 {
566         struct rtl_priv *rtlpriv = rtl_priv(hw);
567         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
568         struct rtl_sta_info *sta_entry = NULL;
569         u8 ratr_index = 7;
570
571         if (sta) {
572                 sta_entry = (struct rtl_sta_info *) sta->drv_priv;
573                 ratr_index = sta_entry->ratr_index;
574         }
575         if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
576                 if (mac->opmode == NL80211_IFTYPE_STATION) {
577                         tcb_desc->ratr_index = 0;
578                 } else if (mac->opmode == NL80211_IFTYPE_ADHOC) {
579                         if (tcb_desc->multicast || tcb_desc->broadcast) {
580                                 tcb_desc->hw_rate =
581                                     rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
582                                 tcb_desc->use_driver_rate = 1;
583                         } else {
584                                 /* TODO */
585                         }
586                         tcb_desc->ratr_index = ratr_index;
587                 } else if (mac->opmode == NL80211_IFTYPE_AP) {
588                         tcb_desc->ratr_index = ratr_index;
589                 }
590         }
591
592         if (rtlpriv->dm.useramask) {
593                 /* TODO we will differentiate adhoc and station futrue  */
594                 if (mac->opmode == NL80211_IFTYPE_STATION) {
595                         tcb_desc->mac_id = 0;
596
597                         if (mac->mode == WIRELESS_MODE_N_24G)
598                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
599                         else if (mac->mode == WIRELESS_MODE_N_5G)
600                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_NG;
601                         else if (mac->mode & WIRELESS_MODE_G)
602                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
603                         else if (mac->mode & WIRELESS_MODE_B)
604                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
605                         else if (mac->mode & WIRELESS_MODE_A)
606                                 tcb_desc->ratr_index = RATR_INX_WIRELESS_G;
607                 } else if (mac->opmode == NL80211_IFTYPE_AP ||
608                         mac->opmode == NL80211_IFTYPE_ADHOC) {
609                         if (NULL != sta) {
610                                 if (sta->aid > 0)
611                                         tcb_desc->mac_id = sta->aid + 1;
612                                 else
613                                         tcb_desc->mac_id = 1;
614                         } else {
615                                 tcb_desc->mac_id = 0;
616                         }
617                 }
618         }
619
620 }
621
622 static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
623                                       struct ieee80211_sta *sta,
624                                       struct rtl_tcb_desc *tcb_desc)
625 {
626         struct rtl_priv *rtlpriv = rtl_priv(hw);
627         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
628
629         tcb_desc->packet_bw = false;
630         if (!sta)
631                 return;
632         if (mac->opmode == NL80211_IFTYPE_AP ||
633             mac->opmode == NL80211_IFTYPE_ADHOC) {
634                 if (!(sta->ht_cap.ht_supported) ||
635                     !(sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
636                         return;
637         } else if (mac->opmode == NL80211_IFTYPE_STATION) {
638                 if (!mac->bw_40 || !(sta->ht_cap.ht_supported))
639                         return;
640         }
641         if (tcb_desc->multicast || tcb_desc->broadcast)
642                 return;
643
644         /*use legency rate, shall use 20MHz */
645         if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
646                 return;
647
648         tcb_desc->packet_bw = true;
649 }
650
651 static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw)
652 {
653         struct rtl_priv *rtlpriv = rtl_priv(hw);
654         struct rtl_phy *rtlphy = &(rtlpriv->phy);
655         u8 hw_rate;
656
657         if (get_rf_type(rtlphy) == RF_2T2R)
658                 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
659         else
660                 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
661
662         return hw_rate;
663 }
664
665 void rtl_get_tcb_desc(struct ieee80211_hw *hw,
666                       struct ieee80211_tx_info *info,
667                       struct ieee80211_sta *sta,
668                       struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
669 {
670         struct rtl_priv *rtlpriv = rtl_priv(hw);
671         struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
672         struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
673         struct ieee80211_rate *txrate;
674         __le16 fc = hdr->frame_control;
675
676         txrate = ieee80211_get_tx_rate(hw, info);
677         tcb_desc->hw_rate = txrate->hw_value;
678
679         if (ieee80211_is_data(fc)) {
680                 /*
681                  *we set data rate INX 0
682                  *in rtl_rc.c   if skb is special data or
683                  *mgt which need low data rate.
684                  */
685
686                 /*
687                  *So tcb_desc->hw_rate is just used for
688                  *special data and mgt frames
689                  */
690                 if (info->control.rates[0].idx == 0 &&
691                                 ieee80211_is_nullfunc(fc)) {
692                         tcb_desc->use_driver_rate = true;
693                         tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
694
695                         tcb_desc->disable_ratefallback = 1;
696                 } else {
697                         /*
698                          *because hw will nerver use hw_rate
699                          *when tcb_desc->use_driver_rate = false
700                          *so we never set highest N rate here,
701                          *and N rate will all be controlled by FW
702                          *when tcb_desc->use_driver_rate = false
703                          */
704                         if (sta && (sta->ht_cap.ht_supported)) {
705                                 tcb_desc->hw_rate = _rtl_get_highest_n_rate(hw);
706                         } else {
707                                 if (rtlmac->mode == WIRELESS_MODE_B) {
708                                         tcb_desc->hw_rate =
709                                            rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
710                                 } else {
711                                         tcb_desc->hw_rate =
712                                            rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
713                                 }
714                         }
715                 }
716
717                 if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
718                         tcb_desc->multicast = 1;
719                 else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
720                         tcb_desc->broadcast = 1;
721
722                 _rtl_txrate_selectmode(hw, sta, tcb_desc);
723                 _rtl_query_bandwidth_mode(hw, sta, tcb_desc);
724                 _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
725                 _rtl_query_shortgi(hw, sta, tcb_desc, info);
726                 _rtl_query_protection_mode(hw, tcb_desc, info);
727         } else {
728                 tcb_desc->use_driver_rate = true;
729                 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
730                 tcb_desc->disable_ratefallback = 1;
731                 tcb_desc->mac_id = 0;
732                 tcb_desc->packet_bw = false;
733         }
734 }
735 EXPORT_SYMBOL(rtl_get_tcb_desc);
736
737 bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
738 {
739         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
740         struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
741         struct rtl_priv *rtlpriv = rtl_priv(hw);
742         __le16 fc = hdr->frame_control;
743         u8 *act = (u8 *) (((u8 *) skb->data + MAC80211_3ADDR_LEN));
744         u8 category;
745
746         if (!ieee80211_is_action(fc))
747                 return true;
748
749         category = *act;
750         act++;
751         switch (category) {
752         case ACT_CAT_BA:
753                 switch (*act) {
754                 case ACT_ADDBAREQ:
755                         if (mac->act_scanning)
756                                 return false;
757
758                         RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
759                                  ("%s ACT_ADDBAREQ From :" MAC_FMT "\n",
760                                   is_tx ? "Tx" : "Rx", MAC_ARG(hdr->addr2)));
761                         break;
762                 case ACT_ADDBARSP:
763                         RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
764                                  ("%s ACT_ADDBARSP From :" MAC_FMT "\n",
765                                   is_tx ? "Tx" : "Rx", MAC_ARG(hdr->addr2)));
766                         break;
767                 case ACT_DELBA:
768                         RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
769                                  ("ACT_ADDBADEL From :" MAC_FMT "\n",
770                                   MAC_ARG(hdr->addr2)));
771                         break;
772                 }
773                 break;
774         default:
775                 break;
776         }
777
778         return true;
779 }
780
781 /*should call before software enc*/
782 u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
783 {
784         struct rtl_priv *rtlpriv = rtl_priv(hw);
785         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
786         __le16 fc = rtl_get_fc(skb);
787         u16 ether_type;
788         u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
789         const struct iphdr *ip;
790
791         if (!ieee80211_is_data(fc))
792                 return false;
793
794
795         ip = (struct iphdr *)((u8 *) skb->data + mac_hdr_len +
796                               SNAP_SIZE + PROTOC_TYPE_SIZE);
797         ether_type = *(u16 *) ((u8 *) skb->data + mac_hdr_len + SNAP_SIZE);
798         /*      ether_type = ntohs(ether_type); */
799
800         if (ETH_P_IP == ether_type) {
801                 if (IPPROTO_UDP == ip->protocol) {
802                         struct udphdr *udp = (struct udphdr *)((u8 *) ip +
803                                                                (ip->ihl << 2));
804                         if (((((u8 *) udp)[1] == 68) &&
805                              (((u8 *) udp)[3] == 67)) ||
806                             ((((u8 *) udp)[1] == 67) &&
807                              (((u8 *) udp)[3] == 68))) {
808                                 /*
809                                  * 68 : UDP BOOTP client
810                                  * 67 : UDP BOOTP server
811                                  */
812                                 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV),
813                                          DBG_DMESG, ("dhcp %s !!\n",
814                                                      (is_tx) ? "Tx" : "Rx"));
815
816                                 if (is_tx) {
817                                         rtl_lps_leave(hw);
818                                         ppsc->last_delaylps_stamp_jiffies =
819                                             jiffies;
820                                 }
821
822                                 return true;
823                         }
824                 }
825         } else if (ETH_P_ARP == ether_type) {
826                 if (is_tx) {
827                         rtl_lps_leave(hw);
828                         ppsc->last_delaylps_stamp_jiffies = jiffies;
829                 }
830
831                 return true;
832         } else if (ETH_P_PAE == ether_type) {
833                 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
834                          ("802.1X %s EAPOL pkt!!\n", (is_tx) ? "Tx" : "Rx"));
835
836                 if (is_tx) {
837                         rtl_lps_leave(hw);
838                         ppsc->last_delaylps_stamp_jiffies = jiffies;
839                 }
840
841                 return true;
842         } else if (ETH_P_IPV6 == ether_type) {
843                 /* IPv6 */
844                 return true;
845         }
846
847         return false;
848 }
849
850 /*********************************************************
851  *
852  * functions called by core.c
853  *
854  *********************************************************/
855 int rtl_tx_agg_start(struct ieee80211_hw *hw,
856                 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
857 {
858         struct rtl_priv *rtlpriv = rtl_priv(hw);
859         struct rtl_tid_data *tid_data;
860         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
861         struct rtl_sta_info *sta_entry = NULL;
862
863         if (sta == NULL)
864                 return -EINVAL;
865
866         if (unlikely(tid >= MAX_TID_COUNT))
867                 return -EINVAL;
868
869         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
870         if (!sta_entry)
871                 return -ENXIO;
872         tid_data = &sta_entry->tids[tid];
873
874         RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
875                  ("on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
876                  tid_data->seq_number));
877
878         *ssn = tid_data->seq_number;
879         tid_data->agg.agg_state = RTL_AGG_START;
880
881         ieee80211_start_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
882
883         return 0;
884 }
885
886 int rtl_tx_agg_stop(struct ieee80211_hw *hw,
887                 struct ieee80211_sta *sta, u16 tid)
888 {
889         struct rtl_priv *rtlpriv = rtl_priv(hw);
890         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
891         struct rtl_sta_info *sta_entry = NULL;
892
893         if (sta == NULL)
894                 return -EINVAL;
895
896         if (!sta->addr) {
897                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
898                 return -EINVAL;
899         }
900
901         RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
902                  ("on ra = %pM tid = %d\n", sta->addr, tid));
903
904         if (unlikely(tid >= MAX_TID_COUNT))
905                 return -EINVAL;
906
907         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
908         sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
909
910         ieee80211_stop_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
911
912         return 0;
913 }
914
915 int rtl_tx_agg_oper(struct ieee80211_hw *hw,
916                 struct ieee80211_sta *sta, u16 tid)
917 {
918         struct rtl_priv *rtlpriv = rtl_priv(hw);
919         struct rtl_sta_info *sta_entry = NULL;
920
921         if (sta == NULL)
922                 return -EINVAL;
923
924         if (!sta->addr) {
925                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
926                 return -EINVAL;
927         }
928
929         RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
930                  ("on ra = %pM tid = %d\n", sta->addr, tid));
931
932         if (unlikely(tid >= MAX_TID_COUNT))
933                 return -EINVAL;
934
935         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
936         sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
937
938         return 0;
939 }
940
941 /*********************************************************
942  *
943  * wq & timer callback functions
944  *
945  *********************************************************/
946 void rtl_watchdog_wq_callback(void *data)
947 {
948         struct rtl_works *rtlworks = container_of_dwork_rtl(data,
949                                                             struct rtl_works,
950                                                             watchdog_wq);
951         struct ieee80211_hw *hw = rtlworks->hw;
952         struct rtl_priv *rtlpriv = rtl_priv(hw);
953         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
954         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
955         bool busytraffic = false;
956         bool higher_busytraffic = false;
957         bool higher_busyrxtraffic = false;
958         u8 idx, tid;
959         u32 rx_cnt_inp4eriod = 0;
960         u32 tx_cnt_inp4eriod = 0;
961         u32 aver_rx_cnt_inperiod = 0;
962         u32 aver_tx_cnt_inperiod = 0;
963         u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
964         u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
965         bool enter_ps = false;
966
967         if (is_hal_stop(rtlhal))
968                 return;
969
970         /* <1> Determine if action frame is allowed */
971         if (mac->link_state > MAC80211_NOLINK) {
972                 if (mac->cnt_after_linked < 20)
973                         mac->cnt_after_linked++;
974         } else {
975                 mac->cnt_after_linked = 0;
976         }
977
978         /*
979          *<3> to check if traffic busy, if
980          * busytraffic we don't change channel
981          */
982         if (mac->link_state >= MAC80211_LINKED) {
983
984                 /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
985                 for (idx = 0; idx <= 2; idx++) {
986                         rtlpriv->link_info.num_rx_in4period[idx] =
987                             rtlpriv->link_info.num_rx_in4period[idx + 1];
988                         rtlpriv->link_info.num_tx_in4period[idx] =
989                             rtlpriv->link_info.num_tx_in4period[idx + 1];
990                 }
991                 rtlpriv->link_info.num_rx_in4period[3] =
992                     rtlpriv->link_info.num_rx_inperiod;
993                 rtlpriv->link_info.num_tx_in4period[3] =
994                     rtlpriv->link_info.num_tx_inperiod;
995                 for (idx = 0; idx <= 3; idx++) {
996                         rx_cnt_inp4eriod +=
997                             rtlpriv->link_info.num_rx_in4period[idx];
998                         tx_cnt_inp4eriod +=
999                             rtlpriv->link_info.num_tx_in4period[idx];
1000                 }
1001                 aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
1002                 aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
1003
1004                 /* (2) check traffic busy */
1005                 if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100)
1006                         busytraffic = true;
1007
1008                 /* Higher Tx/Rx data. */
1009                 if (aver_rx_cnt_inperiod > 4000 ||
1010                     aver_tx_cnt_inperiod > 4000) {
1011                         higher_busytraffic = true;
1012
1013                         /* Extremely high Rx data. */
1014                         if (aver_rx_cnt_inperiod > 5000)
1015                                 higher_busyrxtraffic = true;
1016                 }
1017
1018                 /* check every tid's tx traffic */
1019                 for (tid = 0; tid <= 7; tid++) {
1020                         for (idx = 0; idx <= 2; idx++)
1021                                 rtlpriv->link_info.tidtx_in4period[tid][idx] =
1022                                   rtlpriv->link_info.tidtx_in4period[tid]
1023                                   [idx + 1];
1024                         rtlpriv->link_info.tidtx_in4period[tid][3] =
1025                                 rtlpriv->link_info.tidtx_inperiod[tid];
1026
1027                         for (idx = 0; idx <= 3; idx++)
1028                                 tidtx_inp4eriod[tid] +=
1029                                   rtlpriv->link_info.tidtx_in4period[tid][idx];
1030                         aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
1031                         if (aver_tidtx_inperiod[tid] > 5000)
1032                                 rtlpriv->link_info.higher_busytxtraffic[tid] =
1033                                                    true;
1034                         else
1035                                 rtlpriv->link_info.higher_busytxtraffic[tid] =
1036                                                    false;
1037                 }
1038
1039                 if (((rtlpriv->link_info.num_rx_inperiod +
1040                       rtlpriv->link_info.num_tx_inperiod) > 8) ||
1041                     (rtlpriv->link_info.num_rx_inperiod > 2))
1042                         enter_ps = false;
1043                 else
1044                         enter_ps = true;
1045
1046                 /* LeisurePS only work in infra mode. */
1047                 if (enter_ps)
1048                         rtl_lps_enter(hw);
1049                 else
1050                         rtl_lps_leave(hw);
1051         }
1052
1053         rtlpriv->link_info.num_rx_inperiod = 0;
1054         rtlpriv->link_info.num_tx_inperiod = 0;
1055         for (tid = 0; tid <= 7; tid++)
1056                 rtlpriv->link_info.tidtx_inperiod[tid] = 0;
1057
1058         rtlpriv->link_info.busytraffic = busytraffic;
1059         rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
1060         rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
1061
1062         /* <3> DM */
1063         rtlpriv->cfg->ops->dm_watchdog(hw);
1064 }
1065
1066 void rtl_watch_dog_timer_callback(unsigned long data)
1067 {
1068         struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1069         struct rtl_priv *rtlpriv = rtl_priv(hw);
1070
1071         queue_delayed_work(rtlpriv->works.rtl_wq,
1072                            &rtlpriv->works.watchdog_wq, 0);
1073
1074         mod_timer(&rtlpriv->works.watchdog_timer,
1075                   jiffies + MSECS(RTL_WATCH_DOG_TIME));
1076 }
1077
1078 /*********************************************************
1079  *
1080  * frame process functions
1081  *
1082  *********************************************************/
1083 u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
1084 {
1085         struct ieee80211_mgmt *mgmt = (void *)data;
1086         u8 *pos, *end;
1087
1088         pos = (u8 *)mgmt->u.beacon.variable;
1089         end = data + len;
1090         while (pos < end) {
1091                 if (pos + 2 + pos[1] > end)
1092                         return NULL;
1093
1094                 if (pos[0] == ie)
1095                         return pos;
1096
1097                 pos += 2 + pos[1];
1098         }
1099         return NULL;
1100 }
1101
1102 /* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
1103 /* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
1104 static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
1105                 enum ieee80211_smps_mode smps, u8 *da, u8 *bssid)
1106 {
1107         struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1108         struct sk_buff *skb;
1109         struct ieee80211_mgmt *action_frame;
1110
1111         /* 27 = header + category + action + smps mode */
1112         skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
1113         if (!skb)
1114                 return NULL;
1115
1116         skb_reserve(skb, hw->extra_tx_headroom);
1117         action_frame = (void *)skb_put(skb, 27);
1118         memset(action_frame, 0, 27);
1119         memcpy(action_frame->da, da, ETH_ALEN);
1120         memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
1121         memcpy(action_frame->bssid, bssid, ETH_ALEN);
1122         action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1123                                                   IEEE80211_STYPE_ACTION);
1124         action_frame->u.action.category = WLAN_CATEGORY_HT;
1125         action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
1126         switch (smps) {
1127         case IEEE80211_SMPS_AUTOMATIC:/* 0 */
1128         case IEEE80211_SMPS_NUM_MODES:/* 4 */
1129                 WARN_ON(1);
1130         case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
1131                 action_frame->u.action.u.ht_smps.smps_control =
1132                                 WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
1133                 break;
1134         case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
1135                 action_frame->u.action.u.ht_smps.smps_control =
1136                                 WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
1137                 break;
1138         case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
1139                 action_frame->u.action.u.ht_smps.smps_control =
1140                                 WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
1141                 break;
1142         }
1143
1144         return skb;
1145 }
1146
1147 int rtl_send_smps_action(struct ieee80211_hw *hw,
1148                 struct ieee80211_sta *sta, u8 *da, u8 *bssid,
1149                 enum ieee80211_smps_mode smps)
1150 {
1151         struct rtl_priv *rtlpriv = rtl_priv(hw);
1152         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1153         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1154         struct sk_buff *skb = rtl_make_smps_action(hw, smps, da, bssid);
1155         struct rtl_tcb_desc tcb_desc;
1156         memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
1157
1158         if (rtlpriv->mac80211.act_scanning)
1159                 goto err_free;
1160
1161         if (!sta)
1162                 goto err_free;
1163
1164         if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
1165                 goto err_free;
1166
1167         if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1168                 goto err_free;
1169
1170         /* this is a type = mgmt * stype = action frame */
1171         if (skb) {
1172                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1173                 struct rtl_sta_info *sta_entry =
1174                         (struct rtl_sta_info *) sta->drv_priv;
1175                 sta_entry->mimo_ps = smps;
1176                 rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0);
1177
1178                 info->control.rates[0].idx = 0;
1179                 info->control.sta = sta;
1180                 info->band = hw->conf.channel->band;
1181                 rtlpriv->intf_ops->adapter_tx(hw, skb, &tcb_desc);
1182         }
1183 err_free:
1184         return 0;
1185 }
1186
1187 /*********************************************************
1188  *
1189  * IOT functions
1190  *
1191  *********************************************************/
1192 static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
1193                 struct octet_string vendor_ie)
1194 {
1195         struct rtl_priv *rtlpriv = rtl_priv(hw);
1196         bool matched = false;
1197         static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
1198         static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
1199         static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
1200         static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
1201         static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
1202         static u8 racap[] = { 0x00, 0x0c, 0x43 };
1203         static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
1204         static u8 marvcap[] = { 0x00, 0x50, 0x43 };
1205
1206         if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
1207                 memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
1208                 rtlpriv->mac80211.vendor = PEER_ATH;
1209                 matched = true;
1210         } else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
1211                 memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
1212                 memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
1213                 rtlpriv->mac80211.vendor = PEER_BROAD;
1214                 matched = true;
1215         } else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
1216                 rtlpriv->mac80211.vendor = PEER_RAL;
1217                 matched = true;
1218         } else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
1219                 rtlpriv->mac80211.vendor = PEER_CISCO;
1220                 matched = true;
1221         } else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
1222                 rtlpriv->mac80211.vendor = PEER_MARV;
1223                 matched = true;
1224         }
1225
1226         return matched;
1227 }
1228
1229 static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
1230                 unsigned int len)
1231 {
1232         struct ieee80211_mgmt *mgmt = (void *)data;
1233         struct octet_string vendor_ie;
1234         u8 *pos, *end;
1235
1236         pos = (u8 *)mgmt->u.beacon.variable;
1237         end = data + len;
1238         while (pos < end) {
1239                 if (pos[0] == 221) {
1240                         vendor_ie.length = pos[1];
1241                         vendor_ie.octet = &pos[2];
1242                         if (rtl_chk_vendor_ouisub(hw, vendor_ie))
1243                                 return true;
1244                 }
1245
1246                 if (pos + 2 + pos[1] > end)
1247                         return false;
1248
1249                 pos += 2 + pos[1];
1250         }
1251         return false;
1252 }
1253
1254 void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
1255 {
1256         struct rtl_priv *rtlpriv = rtl_priv(hw);
1257         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1258         struct ieee80211_hdr *hdr = (void *)data;
1259         u32 vendor = PEER_UNKNOWN;
1260
1261         static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
1262         static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
1263         static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
1264         static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
1265         static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
1266         static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
1267         static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
1268         static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
1269         static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
1270         static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
1271         static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
1272         static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
1273         static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
1274         static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
1275         static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
1276         static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
1277
1278         if (mac->opmode != NL80211_IFTYPE_STATION)
1279                 return;
1280
1281         if (mac->link_state == MAC80211_NOLINK) {
1282                 mac->vendor = PEER_UNKNOWN;
1283                 return;
1284         }
1285
1286         if (mac->cnt_after_linked > 2)
1287                 return;
1288
1289         /* check if this really is a beacon */
1290         if (!ieee80211_is_beacon(hdr->frame_control))
1291                 return;
1292
1293         /* min. beacon length + FCS_LEN */
1294         if (len <= 40 + FCS_LEN)
1295                 return;
1296
1297         /* and only beacons from the associated BSSID, please */
1298         if (compare_ether_addr(hdr->addr3, rtlpriv->mac80211.bssid))
1299                 return;
1300
1301         if (rtl_find_221_ie(hw, data, len))
1302                 vendor = mac->vendor;
1303
1304         if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
1305                 (memcmp(mac->bssid, ap5_2, 3) == 0) ||
1306                 (memcmp(mac->bssid, ap5_3, 3) == 0) ||
1307                 (memcmp(mac->bssid, ap5_4, 3) == 0) ||
1308                 (memcmp(mac->bssid, ap5_5, 3) == 0) ||
1309                 (memcmp(mac->bssid, ap5_6, 3) == 0) ||
1310                 vendor == PEER_ATH) {
1311                 vendor = PEER_ATH;
1312                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, ("=>ath find\n"));
1313         } else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
1314                 (memcmp(mac->bssid, ap4_5, 3) == 0) ||
1315                 (memcmp(mac->bssid, ap4_1, 3) == 0) ||
1316                 (memcmp(mac->bssid, ap4_2, 3) == 0) ||
1317                 (memcmp(mac->bssid, ap4_3, 3) == 0) ||
1318                 vendor == PEER_RAL) {
1319                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, ("=>ral findn\n"));
1320                 vendor = PEER_RAL;
1321         } else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
1322                 vendor == PEER_CISCO) {
1323                 vendor = PEER_CISCO;
1324                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, ("=>cisco find\n"));
1325         } else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
1326                 (memcmp(mac->bssid, ap3_2, 3) == 0) ||
1327                 (memcmp(mac->bssid, ap3_3, 3) == 0) ||
1328                 vendor == PEER_BROAD) {
1329                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, ("=>broad find\n"));
1330                 vendor = PEER_BROAD;
1331         } else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
1332                 vendor == PEER_MARV) {
1333                 vendor = PEER_MARV;
1334                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, ("=>marv find\n"));
1335         }
1336
1337         mac->vendor = vendor;
1338 }
1339
1340 /*********************************************************
1341  *
1342  * sysfs functions
1343  *
1344  *********************************************************/
1345 static ssize_t rtl_show_debug_level(struct device *d,
1346                                     struct device_attribute *attr, char *buf)
1347 {
1348         struct ieee80211_hw *hw = dev_get_drvdata(d);
1349         struct rtl_priv *rtlpriv = rtl_priv(hw);
1350
1351         return sprintf(buf, "0x%08X\n", rtlpriv->dbg.global_debuglevel);
1352 }
1353
1354 static ssize_t rtl_store_debug_level(struct device *d,
1355                                      struct device_attribute *attr,
1356                                      const char *buf, size_t count)
1357 {
1358         struct ieee80211_hw *hw = dev_get_drvdata(d);
1359         struct rtl_priv *rtlpriv = rtl_priv(hw);
1360         unsigned long val;
1361         int ret;
1362
1363         ret = strict_strtoul(buf, 0, &val);
1364         if (ret) {
1365                 printk(KERN_DEBUG "%s is not in hex or decimal form.\n", buf);
1366         } else {
1367                 rtlpriv->dbg.global_debuglevel = val;
1368                 printk(KERN_DEBUG "debuglevel:%x\n",
1369                        rtlpriv->dbg.global_debuglevel);
1370         }
1371
1372         return strnlen(buf, count);
1373 }
1374
1375 static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
1376                    rtl_show_debug_level, rtl_store_debug_level);
1377
1378 static struct attribute *rtl_sysfs_entries[] = {
1379
1380         &dev_attr_debug_level.attr,
1381
1382         NULL
1383 };
1384
1385 /*
1386  * "name" is folder name witch will be
1387  * put in device directory like :
1388  * sys/devices/pci0000:00/0000:00:1c.4/
1389  * 0000:06:00.0/rtl_sysfs
1390  */
1391 struct attribute_group rtl_attribute_group = {
1392         .name = "rtlsysfs",
1393         .attrs = rtl_sysfs_entries,
1394 };
1395
1396 MODULE_AUTHOR("lizhaoming       <chaoming_li@realsil.com.cn>");
1397 MODULE_AUTHOR("Realtek WlanFAE  <wlanfae@realtek.com>");
1398 MODULE_AUTHOR("Larry Finger     <Larry.FInger@lwfinger.net>");
1399 MODULE_LICENSE("GPL");
1400 MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
1401
1402 static int __init rtl_core_module_init(void)
1403 {
1404         if (rtl_rate_control_register())
1405                 printk(KERN_ERR "rtlwifi: Unable to register rtl_rc,"
1406                        "use default RC !!\n");
1407
1408         return 0;
1409 }
1410
1411 static void __exit rtl_core_module_exit(void)
1412 {
1413         /*RC*/
1414         rtl_rate_control_unregister();
1415 }
1416
1417 module_init(rtl_core_module_init);
1418 module_exit(rtl_core_module_exit);