Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.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 == true) && 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_tid_data *tid_data;
892         struct rtl_sta_info *sta_entry = NULL;
893
894         if (sta == NULL)
895                 return -EINVAL;
896
897         if (!sta->addr) {
898                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
899                 return -EINVAL;
900         }
901
902         RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
903                  ("on ra = %pM tid = %d\n", sta->addr, tid));
904
905         if (unlikely(tid >= MAX_TID_COUNT))
906                 return -EINVAL;
907
908         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
909         tid_data = &sta_entry->tids[tid];
910         sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
911
912         ieee80211_stop_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
913
914         return 0;
915 }
916
917 int rtl_tx_agg_oper(struct ieee80211_hw *hw,
918                 struct ieee80211_sta *sta, u16 tid)
919 {
920         struct rtl_priv *rtlpriv = rtl_priv(hw);
921         struct rtl_tid_data *tid_data;
922         struct rtl_sta_info *sta_entry = NULL;
923
924         if (sta == NULL)
925                 return -EINVAL;
926
927         if (!sta->addr) {
928                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
929                 return -EINVAL;
930         }
931
932         RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
933                  ("on ra = %pM tid = %d\n", sta->addr, tid));
934
935         if (unlikely(tid >= MAX_TID_COUNT))
936                 return -EINVAL;
937
938         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
939         tid_data = &sta_entry->tids[tid];
940         sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
941
942         return 0;
943 }
944
945 /*********************************************************
946  *
947  * wq & timer callback functions
948  *
949  *********************************************************/
950 void rtl_watchdog_wq_callback(void *data)
951 {
952         struct rtl_works *rtlworks = container_of_dwork_rtl(data,
953                                                             struct rtl_works,
954                                                             watchdog_wq);
955         struct ieee80211_hw *hw = rtlworks->hw;
956         struct rtl_priv *rtlpriv = rtl_priv(hw);
957         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
958         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
959         bool busytraffic = false;
960         bool higher_busytraffic = false;
961         bool higher_busyrxtraffic = false;
962         u8 idx, tid;
963         u32 rx_cnt_inp4eriod = 0;
964         u32 tx_cnt_inp4eriod = 0;
965         u32 aver_rx_cnt_inperiod = 0;
966         u32 aver_tx_cnt_inperiod = 0;
967         u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
968         u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
969         bool enter_ps = false;
970
971         if (is_hal_stop(rtlhal))
972                 return;
973
974         /* <1> Determine if action frame is allowed */
975         if (mac->link_state > MAC80211_NOLINK) {
976                 if (mac->cnt_after_linked < 20)
977                         mac->cnt_after_linked++;
978         } else {
979                 mac->cnt_after_linked = 0;
980         }
981
982         /*
983          *<3> to check if traffic busy, if
984          * busytraffic we don't change channel
985          */
986         if (mac->link_state >= MAC80211_LINKED) {
987
988                 /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
989                 for (idx = 0; idx <= 2; idx++) {
990                         rtlpriv->link_info.num_rx_in4period[idx] =
991                             rtlpriv->link_info.num_rx_in4period[idx + 1];
992                         rtlpriv->link_info.num_tx_in4period[idx] =
993                             rtlpriv->link_info.num_tx_in4period[idx + 1];
994                 }
995                 rtlpriv->link_info.num_rx_in4period[3] =
996                     rtlpriv->link_info.num_rx_inperiod;
997                 rtlpriv->link_info.num_tx_in4period[3] =
998                     rtlpriv->link_info.num_tx_inperiod;
999                 for (idx = 0; idx <= 3; idx++) {
1000                         rx_cnt_inp4eriod +=
1001                             rtlpriv->link_info.num_rx_in4period[idx];
1002                         tx_cnt_inp4eriod +=
1003                             rtlpriv->link_info.num_tx_in4period[idx];
1004                 }
1005                 aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
1006                 aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
1007
1008                 /* (2) check traffic busy */
1009                 if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100)
1010                         busytraffic = true;
1011
1012                 /* Higher Tx/Rx data. */
1013                 if (aver_rx_cnt_inperiod > 4000 ||
1014                     aver_tx_cnt_inperiod > 4000) {
1015                         higher_busytraffic = true;
1016
1017                         /* Extremely high Rx data. */
1018                         if (aver_rx_cnt_inperiod > 5000)
1019                                 higher_busyrxtraffic = true;
1020                 }
1021
1022                 /* check every tid's tx traffic */
1023                 for (tid = 0; tid <= 7; tid++) {
1024                         for (idx = 0; idx <= 2; idx++)
1025                                 rtlpriv->link_info.tidtx_in4period[tid][idx] =
1026                                   rtlpriv->link_info.tidtx_in4period[tid]
1027                                   [idx + 1];
1028                         rtlpriv->link_info.tidtx_in4period[tid][3] =
1029                                 rtlpriv->link_info.tidtx_inperiod[tid];
1030
1031                         for (idx = 0; idx <= 3; idx++)
1032                                 tidtx_inp4eriod[tid] +=
1033                                   rtlpriv->link_info.tidtx_in4period[tid][idx];
1034                         aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
1035                         if (aver_tidtx_inperiod[tid] > 5000)
1036                                 rtlpriv->link_info.higher_busytxtraffic[tid] =
1037                                                    true;
1038                         else
1039                                 rtlpriv->link_info.higher_busytxtraffic[tid] =
1040                                                    false;
1041                 }
1042
1043                 if (((rtlpriv->link_info.num_rx_inperiod +
1044                       rtlpriv->link_info.num_tx_inperiod) > 8) ||
1045                     (rtlpriv->link_info.num_rx_inperiod > 2))
1046                         enter_ps = false;
1047                 else
1048                         enter_ps = true;
1049
1050                 /* LeisurePS only work in infra mode. */
1051                 if (enter_ps)
1052                         rtl_lps_enter(hw);
1053                 else
1054                         rtl_lps_leave(hw);
1055         }
1056
1057         rtlpriv->link_info.num_rx_inperiod = 0;
1058         rtlpriv->link_info.num_tx_inperiod = 0;
1059         for (tid = 0; tid <= 7; tid++)
1060                 rtlpriv->link_info.tidtx_inperiod[tid] = 0;
1061
1062         rtlpriv->link_info.busytraffic = busytraffic;
1063         rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
1064         rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
1065
1066         /* <3> DM */
1067         rtlpriv->cfg->ops->dm_watchdog(hw);
1068 }
1069
1070 void rtl_watch_dog_timer_callback(unsigned long data)
1071 {
1072         struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1073         struct rtl_priv *rtlpriv = rtl_priv(hw);
1074
1075         queue_delayed_work(rtlpriv->works.rtl_wq,
1076                            &rtlpriv->works.watchdog_wq, 0);
1077
1078         mod_timer(&rtlpriv->works.watchdog_timer,
1079                   jiffies + MSECS(RTL_WATCH_DOG_TIME));
1080 }
1081
1082 /*********************************************************
1083  *
1084  * frame process functions
1085  *
1086  *********************************************************/
1087 u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
1088 {
1089         struct ieee80211_mgmt *mgmt = (void *)data;
1090         u8 *pos, *end;
1091
1092         pos = (u8 *)mgmt->u.beacon.variable;
1093         end = data + len;
1094         while (pos < end) {
1095                 if (pos + 2 + pos[1] > end)
1096                         return NULL;
1097
1098                 if (pos[0] == ie)
1099                         return pos;
1100
1101                 pos += 2 + pos[1];
1102         }
1103         return NULL;
1104 }
1105
1106 /* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
1107 /* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
1108 static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
1109                 enum ieee80211_smps_mode smps, u8 *da, u8 *bssid)
1110 {
1111         struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1112         struct sk_buff *skb;
1113         struct ieee80211_mgmt *action_frame;
1114
1115         /* 27 = header + category + action + smps mode */
1116         skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
1117         if (!skb)
1118                 return NULL;
1119
1120         skb_reserve(skb, hw->extra_tx_headroom);
1121         action_frame = (void *)skb_put(skb, 27);
1122         memset(action_frame, 0, 27);
1123         memcpy(action_frame->da, da, ETH_ALEN);
1124         memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
1125         memcpy(action_frame->bssid, bssid, ETH_ALEN);
1126         action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1127                                                   IEEE80211_STYPE_ACTION);
1128         action_frame->u.action.category = WLAN_CATEGORY_HT;
1129         action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
1130         switch (smps) {
1131         case IEEE80211_SMPS_AUTOMATIC:/* 0 */
1132         case IEEE80211_SMPS_NUM_MODES:/* 4 */
1133                 WARN_ON(1);
1134         case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
1135                 action_frame->u.action.u.ht_smps.smps_control =
1136                                 WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
1137                 break;
1138         case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
1139                 action_frame->u.action.u.ht_smps.smps_control =
1140                                 WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
1141                 break;
1142         case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
1143                 action_frame->u.action.u.ht_smps.smps_control =
1144                                 WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
1145                 break;
1146         }
1147
1148         return skb;
1149 }
1150
1151 int rtl_send_smps_action(struct ieee80211_hw *hw,
1152                 struct ieee80211_sta *sta, u8 *da, u8 *bssid,
1153                 enum ieee80211_smps_mode smps)
1154 {
1155         struct rtl_priv *rtlpriv = rtl_priv(hw);
1156         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1157         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1158         struct sk_buff *skb = rtl_make_smps_action(hw, smps, da, bssid);
1159         struct rtl_tcb_desc tcb_desc;
1160         memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
1161
1162         if (rtlpriv->mac80211.act_scanning)
1163                 goto err_free;
1164
1165         if (!sta)
1166                 goto err_free;
1167
1168         if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
1169                 goto err_free;
1170
1171         if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1172                 goto err_free;
1173
1174         /* this is a type = mgmt * stype = action frame */
1175         if (skb) {
1176                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1177                 struct rtl_sta_info *sta_entry =
1178                         (struct rtl_sta_info *) sta->drv_priv;
1179                 sta_entry->mimo_ps = smps;
1180                 rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0);
1181
1182                 info->control.rates[0].idx = 0;
1183                 info->control.sta = sta;
1184                 info->band = hw->conf.channel->band;
1185                 rtlpriv->intf_ops->adapter_tx(hw, skb, &tcb_desc);
1186         }
1187 err_free:
1188         return 0;
1189 }
1190
1191 /*********************************************************
1192  *
1193  * IOT functions
1194  *
1195  *********************************************************/
1196 static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
1197                 struct octet_string vendor_ie)
1198 {
1199         struct rtl_priv *rtlpriv = rtl_priv(hw);
1200         bool matched = false;
1201         static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
1202         static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
1203         static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
1204         static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
1205         static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
1206         static u8 racap[] = { 0x00, 0x0c, 0x43 };
1207         static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
1208         static u8 marvcap[] = { 0x00, 0x50, 0x43 };
1209
1210         if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
1211                 memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
1212                 rtlpriv->mac80211.vendor = PEER_ATH;
1213                 matched = true;
1214         } else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
1215                 memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
1216                 memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
1217                 rtlpriv->mac80211.vendor = PEER_BROAD;
1218                 matched = true;
1219         } else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
1220                 rtlpriv->mac80211.vendor = PEER_RAL;
1221                 matched = true;
1222         } else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
1223                 rtlpriv->mac80211.vendor = PEER_CISCO;
1224                 matched = true;
1225         } else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
1226                 rtlpriv->mac80211.vendor = PEER_MARV;
1227                 matched = true;
1228         }
1229
1230         return matched;
1231 }
1232
1233 static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
1234                 unsigned int len)
1235 {
1236         struct ieee80211_mgmt *mgmt = (void *)data;
1237         struct octet_string vendor_ie;
1238         u8 *pos, *end;
1239
1240         pos = (u8 *)mgmt->u.beacon.variable;
1241         end = data + len;
1242         while (pos < end) {
1243                 if (pos[0] == 221) {
1244                         vendor_ie.length = pos[1];
1245                         vendor_ie.octet = &pos[2];
1246                         if (rtl_chk_vendor_ouisub(hw, vendor_ie))
1247                                 return true;
1248                 }
1249
1250                 if (pos + 2 + pos[1] > end)
1251                         return false;
1252
1253                 pos += 2 + pos[1];
1254         }
1255         return false;
1256 }
1257
1258 void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
1259 {
1260         struct rtl_priv *rtlpriv = rtl_priv(hw);
1261         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1262         struct ieee80211_hdr *hdr = (void *)data;
1263         u32 vendor = PEER_UNKNOWN;
1264
1265         static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
1266         static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
1267         static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
1268         static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
1269         static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
1270         static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
1271         static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
1272         static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
1273         static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
1274         static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
1275         static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
1276         static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
1277         static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
1278         static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
1279         static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
1280         static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
1281
1282         if (mac->opmode != NL80211_IFTYPE_STATION)
1283                 return;
1284
1285         if (mac->link_state == MAC80211_NOLINK) {
1286                 mac->vendor = PEER_UNKNOWN;
1287                 return;
1288         }
1289
1290         if (mac->cnt_after_linked > 2)
1291                 return;
1292
1293         /* check if this really is a beacon */
1294         if (!ieee80211_is_beacon(hdr->frame_control))
1295                 return;
1296
1297         /* min. beacon length + FCS_LEN */
1298         if (len <= 40 + FCS_LEN)
1299                 return;
1300
1301         /* and only beacons from the associated BSSID, please */
1302         if (compare_ether_addr(hdr->addr3, rtlpriv->mac80211.bssid))
1303                 return;
1304
1305         if (rtl_find_221_ie(hw, data, len))
1306                 vendor = mac->vendor;
1307
1308         if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
1309                 (memcmp(mac->bssid, ap5_2, 3) == 0) ||
1310                 (memcmp(mac->bssid, ap5_3, 3) == 0) ||
1311                 (memcmp(mac->bssid, ap5_4, 3) == 0) ||
1312                 (memcmp(mac->bssid, ap5_5, 3) == 0) ||
1313                 (memcmp(mac->bssid, ap5_6, 3) == 0) ||
1314                 vendor == PEER_ATH) {
1315                 vendor = PEER_ATH;
1316                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, ("=>ath find\n"));
1317         } else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
1318                 (memcmp(mac->bssid, ap4_5, 3) == 0) ||
1319                 (memcmp(mac->bssid, ap4_1, 3) == 0) ||
1320                 (memcmp(mac->bssid, ap4_2, 3) == 0) ||
1321                 (memcmp(mac->bssid, ap4_3, 3) == 0) ||
1322                 vendor == PEER_RAL) {
1323                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, ("=>ral findn\n"));
1324                 vendor = PEER_RAL;
1325         } else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
1326                 vendor == PEER_CISCO) {
1327                 vendor = PEER_CISCO;
1328                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, ("=>cisco find\n"));
1329         } else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
1330                 (memcmp(mac->bssid, ap3_2, 3) == 0) ||
1331                 (memcmp(mac->bssid, ap3_3, 3) == 0) ||
1332                 vendor == PEER_BROAD) {
1333                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, ("=>broad find\n"));
1334                 vendor = PEER_BROAD;
1335         } else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
1336                 vendor == PEER_MARV) {
1337                 vendor = PEER_MARV;
1338                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, ("=>marv find\n"));
1339         }
1340
1341         mac->vendor = vendor;
1342 }
1343
1344 /*********************************************************
1345  *
1346  * sysfs functions
1347  *
1348  *********************************************************/
1349 static ssize_t rtl_show_debug_level(struct device *d,
1350                                     struct device_attribute *attr, char *buf)
1351 {
1352         struct ieee80211_hw *hw = dev_get_drvdata(d);
1353         struct rtl_priv *rtlpriv = rtl_priv(hw);
1354
1355         return sprintf(buf, "0x%08X\n", rtlpriv->dbg.global_debuglevel);
1356 }
1357
1358 static ssize_t rtl_store_debug_level(struct device *d,
1359                                      struct device_attribute *attr,
1360                                      const char *buf, size_t count)
1361 {
1362         struct ieee80211_hw *hw = dev_get_drvdata(d);
1363         struct rtl_priv *rtlpriv = rtl_priv(hw);
1364         unsigned long val;
1365         int ret;
1366
1367         ret = strict_strtoul(buf, 0, &val);
1368         if (ret) {
1369                 printk(KERN_DEBUG "%s is not in hex or decimal form.\n", buf);
1370         } else {
1371                 rtlpriv->dbg.global_debuglevel = val;
1372                 printk(KERN_DEBUG "debuglevel:%x\n",
1373                        rtlpriv->dbg.global_debuglevel);
1374         }
1375
1376         return strnlen(buf, count);
1377 }
1378
1379 static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
1380                    rtl_show_debug_level, rtl_store_debug_level);
1381
1382 static struct attribute *rtl_sysfs_entries[] = {
1383
1384         &dev_attr_debug_level.attr,
1385
1386         NULL
1387 };
1388
1389 /*
1390  * "name" is folder name witch will be
1391  * put in device directory like :
1392  * sys/devices/pci0000:00/0000:00:1c.4/
1393  * 0000:06:00.0/rtl_sysfs
1394  */
1395 struct attribute_group rtl_attribute_group = {
1396         .name = "rtlsysfs",
1397         .attrs = rtl_sysfs_entries,
1398 };
1399
1400 MODULE_AUTHOR("lizhaoming       <chaoming_li@realsil.com.cn>");
1401 MODULE_AUTHOR("Realtek WlanFAE  <wlanfae@realtek.com>");
1402 MODULE_AUTHOR("Larry Finger     <Larry.FInger@lwfinger.net>");
1403 MODULE_LICENSE("GPL");
1404 MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
1405
1406 static int __init rtl_core_module_init(void)
1407 {
1408         if (rtl_rate_control_register())
1409                 printk(KERN_ERR "rtlwifi: Unable to register rtl_rc,"
1410                        "use default RC !!\n");
1411
1412         return 0;
1413 }
1414
1415 static void __exit rtl_core_module_exit(void)
1416 {
1417         /*RC*/
1418         rtl_rate_control_unregister();
1419 }
1420
1421 module_init(rtl_core_module_init);
1422 module_exit(rtl_core_module_exit);