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