1 /******************************************************************************
3 * Copyright(c) 2009-2012 Realtek Corporation.
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.
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
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
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
21 * Contact Information:
22 * wlanfae <wlanfae@realtek.com>
23 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
24 * Hsinchu 300, Taiwan.
26 * Larry Finger <Larry.Finger@lwfinger.net>
28 *****************************************************************************/
39 #include <linux/module.h>
40 #include <linux/udp.h>
43 *NOTICE!!!: This file will be very big, we should
44 *keep it clear under following roles:
46 *This file include following parts, so, if you add new
47 *functions into this file, please check which part it
48 *should includes. or check if you should add new part
51 *1) mac80211 init functions
52 *2) tx information functions
53 *3) functions called by core.c
54 *4) wq & timer callback functions
55 *5) frame process functions
62 /*********************************************************
64 * mac80211 init functions
66 *********************************************************/
67 static struct ieee80211_channel rtl_channeltable_2g[] = {
68 {.center_freq = 2412, .hw_value = 1,},
69 {.center_freq = 2417, .hw_value = 2,},
70 {.center_freq = 2422, .hw_value = 3,},
71 {.center_freq = 2427, .hw_value = 4,},
72 {.center_freq = 2432, .hw_value = 5,},
73 {.center_freq = 2437, .hw_value = 6,},
74 {.center_freq = 2442, .hw_value = 7,},
75 {.center_freq = 2447, .hw_value = 8,},
76 {.center_freq = 2452, .hw_value = 9,},
77 {.center_freq = 2457, .hw_value = 10,},
78 {.center_freq = 2462, .hw_value = 11,},
79 {.center_freq = 2467, .hw_value = 12,},
80 {.center_freq = 2472, .hw_value = 13,},
81 {.center_freq = 2484, .hw_value = 14,},
84 static struct ieee80211_channel rtl_channeltable_5g[] = {
85 {.center_freq = 5180, .hw_value = 36,},
86 {.center_freq = 5200, .hw_value = 40,},
87 {.center_freq = 5220, .hw_value = 44,},
88 {.center_freq = 5240, .hw_value = 48,},
89 {.center_freq = 5260, .hw_value = 52,},
90 {.center_freq = 5280, .hw_value = 56,},
91 {.center_freq = 5300, .hw_value = 60,},
92 {.center_freq = 5320, .hw_value = 64,},
93 {.center_freq = 5500, .hw_value = 100,},
94 {.center_freq = 5520, .hw_value = 104,},
95 {.center_freq = 5540, .hw_value = 108,},
96 {.center_freq = 5560, .hw_value = 112,},
97 {.center_freq = 5580, .hw_value = 116,},
98 {.center_freq = 5600, .hw_value = 120,},
99 {.center_freq = 5620, .hw_value = 124,},
100 {.center_freq = 5640, .hw_value = 128,},
101 {.center_freq = 5660, .hw_value = 132,},
102 {.center_freq = 5680, .hw_value = 136,},
103 {.center_freq = 5700, .hw_value = 140,},
104 {.center_freq = 5745, .hw_value = 149,},
105 {.center_freq = 5765, .hw_value = 153,},
106 {.center_freq = 5785, .hw_value = 157,},
107 {.center_freq = 5805, .hw_value = 161,},
108 {.center_freq = 5825, .hw_value = 165,},
111 static struct ieee80211_rate rtl_ratetable_2g[] = {
112 {.bitrate = 10, .hw_value = 0x00,},
113 {.bitrate = 20, .hw_value = 0x01,},
114 {.bitrate = 55, .hw_value = 0x02,},
115 {.bitrate = 110, .hw_value = 0x03,},
116 {.bitrate = 60, .hw_value = 0x04,},
117 {.bitrate = 90, .hw_value = 0x05,},
118 {.bitrate = 120, .hw_value = 0x06,},
119 {.bitrate = 180, .hw_value = 0x07,},
120 {.bitrate = 240, .hw_value = 0x08,},
121 {.bitrate = 360, .hw_value = 0x09,},
122 {.bitrate = 480, .hw_value = 0x0a,},
123 {.bitrate = 540, .hw_value = 0x0b,},
126 static struct ieee80211_rate rtl_ratetable_5g[] = {
127 {.bitrate = 60, .hw_value = 0x04,},
128 {.bitrate = 90, .hw_value = 0x05,},
129 {.bitrate = 120, .hw_value = 0x06,},
130 {.bitrate = 180, .hw_value = 0x07,},
131 {.bitrate = 240, .hw_value = 0x08,},
132 {.bitrate = 360, .hw_value = 0x09,},
133 {.bitrate = 480, .hw_value = 0x0a,},
134 {.bitrate = 540, .hw_value = 0x0b,},
137 static const struct ieee80211_supported_band rtl_band_2ghz = {
138 .band = IEEE80211_BAND_2GHZ,
140 .channels = rtl_channeltable_2g,
141 .n_channels = ARRAY_SIZE(rtl_channeltable_2g),
143 .bitrates = rtl_ratetable_2g,
144 .n_bitrates = ARRAY_SIZE(rtl_ratetable_2g),
149 static struct ieee80211_supported_band rtl_band_5ghz = {
150 .band = IEEE80211_BAND_5GHZ,
152 .channels = rtl_channeltable_5g,
153 .n_channels = ARRAY_SIZE(rtl_channeltable_5g),
155 .bitrates = rtl_ratetable_5g,
156 .n_bitrates = ARRAY_SIZE(rtl_ratetable_5g),
161 static const u8 tid_to_ac[] = {
162 2, /* IEEE80211_AC_BE */
163 3, /* IEEE80211_AC_BK */
164 3, /* IEEE80211_AC_BK */
165 2, /* IEEE80211_AC_BE */
166 1, /* IEEE80211_AC_VI */
167 1, /* IEEE80211_AC_VI */
168 0, /* IEEE80211_AC_VO */
169 0, /* IEEE80211_AC_VO */
172 u8 rtl_tid_to_ac(u8 tid)
174 return tid_to_ac[tid];
177 static void _rtl_init_hw_ht_capab(struct ieee80211_hw *hw,
178 struct ieee80211_sta_ht_cap *ht_cap)
180 struct rtl_priv *rtlpriv = rtl_priv(hw);
181 struct rtl_phy *rtlphy = &(rtlpriv->phy);
183 ht_cap->ht_supported = true;
184 ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
185 IEEE80211_HT_CAP_SGI_40 |
186 IEEE80211_HT_CAP_SGI_20 |
187 IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU;
189 if (rtlpriv->rtlhal.disable_amsdu_8k)
190 ht_cap->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
193 *Maximum length of AMPDU that the STA can receive.
194 *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
196 ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
198 /*Minimum MPDU start spacing , */
199 ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
201 ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
203 /*hw->wiphy->bands[IEEE80211_BAND_2GHZ]
206 *if rx_ant = 1 rx_mask[0]= 0xff;==>MCS0-MCS7
207 *if rx_ant = 2 rx_mask[1]= 0xff;==>MCS8-MCS15
208 *if rx_ant >= 3 rx_mask[2]= 0xff;
209 *if BW_40 rx_mask[4]= 0x01;
210 *highest supported RX rate
212 if (rtlpriv->dm.supp_phymode_switch) {
214 RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG,
215 "Support phy mode switch\n");
217 ht_cap->mcs.rx_mask[0] = 0xFF;
218 ht_cap->mcs.rx_mask[1] = 0xFF;
219 ht_cap->mcs.rx_mask[4] = 0x01;
221 ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
223 if (get_rf_type(rtlphy) == RF_1T2R ||
224 get_rf_type(rtlphy) == RF_2T2R) {
225 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
227 ht_cap->mcs.rx_mask[0] = 0xFF;
228 ht_cap->mcs.rx_mask[1] = 0xFF;
229 ht_cap->mcs.rx_mask[4] = 0x01;
231 ht_cap->mcs.rx_highest =
232 cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
233 } else if (get_rf_type(rtlphy) == RF_1T1R) {
234 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "1T1R\n");
236 ht_cap->mcs.rx_mask[0] = 0xFF;
237 ht_cap->mcs.rx_mask[1] = 0x00;
238 ht_cap->mcs.rx_mask[4] = 0x01;
240 ht_cap->mcs.rx_highest =
241 cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
246 static void _rtl_init_mac80211(struct ieee80211_hw *hw)
248 struct rtl_priv *rtlpriv = rtl_priv(hw);
249 struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
250 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
251 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
252 struct ieee80211_supported_band *sband;
255 if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY && rtlhal->bandset ==
258 /* <1> use mac->bands as mem for hw->wiphy->bands */
259 sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
261 /* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
262 * to default value(1T1R) */
263 memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]), &rtl_band_2ghz,
264 sizeof(struct ieee80211_supported_band));
266 /* <3> init ht cap base on ant_num */
267 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
269 /* <4> set mac->sband to wiphy->sband */
270 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
273 /* <1> use mac->bands as mem for hw->wiphy->bands */
274 sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
276 /* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
277 * to default value(1T1R) */
278 memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]), &rtl_band_5ghz,
279 sizeof(struct ieee80211_supported_band));
281 /* <3> init ht cap base on ant_num */
282 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
284 /* <4> set mac->sband to wiphy->sband */
285 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
287 if (rtlhal->current_bandtype == BAND_ON_2_4G) {
288 /* <1> use mac->bands as mem for hw->wiphy->bands */
289 sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
291 /* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
292 * to default value(1T1R) */
293 memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]),
295 sizeof(struct ieee80211_supported_band));
297 /* <3> init ht cap base on ant_num */
298 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
300 /* <4> set mac->sband to wiphy->sband */
301 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
302 } else if (rtlhal->current_bandtype == BAND_ON_5G) {
303 /* <1> use mac->bands as mem for hw->wiphy->bands */
304 sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
306 /* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
307 * to default value(1T1R) */
308 memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]),
310 sizeof(struct ieee80211_supported_band));
312 /* <3> init ht cap base on ant_num */
313 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
315 /* <4> set mac->sband to wiphy->sband */
316 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
318 RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG, "Err BAND %d\n",
319 rtlhal->current_bandtype);
322 /* <5> set hw caps */
323 hw->flags = IEEE80211_HW_SIGNAL_DBM |
324 IEEE80211_HW_RX_INCLUDES_FCS |
325 IEEE80211_HW_AMPDU_AGGREGATION |
326 IEEE80211_HW_CONNECTION_MONITOR |
327 /* IEEE80211_HW_SUPPORTS_CQM_RSSI | */
328 IEEE80211_HW_CONNECTION_MONITOR |
329 IEEE80211_HW_MFP_CAPABLE |
330 IEEE80211_HW_REPORTS_TX_ACK_STATUS | 0;
332 /* swlps or hwlps has been set in diff chip in init_sw_vars */
333 if (rtlpriv->psc.swctrl_lps)
334 hw->flags |= IEEE80211_HW_SUPPORTS_PS |
335 IEEE80211_HW_PS_NULLFUNC_STACK |
336 /* IEEE80211_HW_SUPPORTS_DYNAMIC_PS | */
339 hw->wiphy->interface_modes =
340 BIT(NL80211_IFTYPE_AP) |
341 BIT(NL80211_IFTYPE_STATION) |
342 BIT(NL80211_IFTYPE_ADHOC) |
343 BIT(NL80211_IFTYPE_MESH_POINT) |
344 BIT(NL80211_IFTYPE_P2P_CLIENT) |
345 BIT(NL80211_IFTYPE_P2P_GO);
347 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
348 hw->wiphy->rts_threshold = 2347;
351 hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;
353 /* TODO: Correct this value for our hw */
354 /* TODO: define these hard code value */
355 hw->channel_change_time = 100;
356 hw->max_listen_interval = 10;
357 hw->max_rate_tries = 4;
358 /* hw->max_rates = 1; */
359 hw->sta_data_size = sizeof(struct rtl_sta_info);
361 /* <6> mac address */
362 if (is_valid_ether_addr(rtlefuse->dev_addr)) {
363 SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
365 u8 rtlmac1[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
366 get_random_bytes((rtlmac1 + (ETH_ALEN - 1)), 1);
367 SET_IEEE80211_PERM_ADDR(hw, rtlmac1);
372 static void _rtl_init_deferred_work(struct ieee80211_hw *hw)
374 struct rtl_priv *rtlpriv = rtl_priv(hw);
377 setup_timer(&rtlpriv->works.watchdog_timer,
378 rtl_watch_dog_timer_callback, (unsigned long)hw);
379 setup_timer(&rtlpriv->works.dualmac_easyconcurrent_retrytimer,
380 rtl_easy_concurrent_retrytimer_callback, (unsigned long)hw);
383 rtlpriv->works.hw = hw;
384 rtlpriv->works.rtl_wq = alloc_workqueue(rtlpriv->cfg->name, 0, 0);
385 INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
386 (void *)rtl_watchdog_wq_callback);
387 INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
388 (void *)rtl_ips_nic_off_wq_callback);
389 INIT_DELAYED_WORK(&rtlpriv->works.ps_work,
390 (void *)rtl_swlps_wq_callback);
391 INIT_DELAYED_WORK(&rtlpriv->works.ps_rfon_wq,
392 (void *)rtl_swlps_rfon_wq_callback);
393 INIT_DELAYED_WORK(&rtlpriv->works.fwevt_wq,
394 (void *)rtl_fwevt_wq_callback);
398 void rtl_deinit_deferred_work(struct ieee80211_hw *hw)
400 struct rtl_priv *rtlpriv = rtl_priv(hw);
402 del_timer_sync(&rtlpriv->works.watchdog_timer);
404 cancel_delayed_work(&rtlpriv->works.watchdog_wq);
405 cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
406 cancel_delayed_work(&rtlpriv->works.ps_work);
407 cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
408 cancel_delayed_work(&rtlpriv->works.fwevt_wq);
411 void rtl_init_rfkill(struct ieee80211_hw *hw)
413 struct rtl_priv *rtlpriv = rtl_priv(hw);
419 /*set init state to on */
420 rtlpriv->rfkill.rfkill_state = true;
421 wiphy_rfkill_set_hw_state(hw->wiphy, 0);
423 radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
426 pr_info("wireless switch is %s\n",
427 rtlpriv->rfkill.rfkill_state ? "on" : "off");
429 rtlpriv->rfkill.rfkill_state = radio_state;
431 blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
432 wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
435 wiphy_rfkill_start_polling(hw->wiphy);
437 EXPORT_SYMBOL(rtl_init_rfkill);
439 void rtl_deinit_rfkill(struct ieee80211_hw *hw)
441 wiphy_rfkill_stop_polling(hw->wiphy);
444 int rtl_init_core(struct ieee80211_hw *hw)
446 struct rtl_priv *rtlpriv = rtl_priv(hw);
447 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
449 /* <1> init mac80211 */
450 _rtl_init_mac80211(hw);
453 /* <2> rate control register */
454 hw->rate_control_algorithm = "rtl_rc";
457 * <3> init CRDA must come after init
458 * mac80211 hw in _rtl_init_mac80211.
460 if (rtl_regd_init(hw, rtl_reg_notifier)) {
461 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "REGD init failed\n");
466 mutex_init(&rtlpriv->locks.conf_mutex);
467 mutex_init(&rtlpriv->locks.ps_mutex);
468 spin_lock_init(&rtlpriv->locks.ips_lock);
469 spin_lock_init(&rtlpriv->locks.irq_th_lock);
470 spin_lock_init(&rtlpriv->locks.irq_pci_lock);
471 spin_lock_init(&rtlpriv->locks.tx_lock);
472 spin_lock_init(&rtlpriv->locks.h2c_lock);
473 spin_lock_init(&rtlpriv->locks.rf_ps_lock);
474 spin_lock_init(&rtlpriv->locks.rf_lock);
475 spin_lock_init(&rtlpriv->locks.waitq_lock);
476 spin_lock_init(&rtlpriv->locks.entry_list_lock);
477 spin_lock_init(&rtlpriv->locks.fw_ps_lock);
478 spin_lock_init(&rtlpriv->locks.cck_and_rw_pagea_lock);
479 spin_lock_init(&rtlpriv->locks.check_sendpkt_lock);
480 spin_lock_init(&rtlpriv->locks.fw_ps_lock);
481 spin_lock_init(&rtlpriv->locks.lps_lock);
484 INIT_LIST_HEAD(&rtlpriv->entry_list);
486 rtlmac->link_state = MAC80211_NOLINK;
488 /* <6> init deferred work */
489 _rtl_init_deferred_work(hw);
494 void rtl_deinit_core(struct ieee80211_hw *hw)
498 void rtl_init_rx_config(struct ieee80211_hw *hw)
500 struct rtl_priv *rtlpriv = rtl_priv(hw);
501 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
503 rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
506 /*********************************************************
508 * tx information functions
510 *********************************************************/
511 static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
512 struct rtl_tcb_desc *tcb_desc,
513 struct ieee80211_tx_info *info)
515 struct rtl_priv *rtlpriv = rtl_priv(hw);
516 u8 rate_flag = info->control.rates[0].flags;
518 tcb_desc->use_shortpreamble = false;
520 /* 1M can only use Long Preamble. 11B spec */
521 if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
523 else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
524 tcb_desc->use_shortpreamble = true;
529 static void _rtl_query_shortgi(struct ieee80211_hw *hw,
530 struct ieee80211_sta *sta,
531 struct rtl_tcb_desc *tcb_desc,
532 struct ieee80211_tx_info *info)
534 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
535 u8 rate_flag = info->control.rates[0].flags;
536 u8 sgi_40 = 0, sgi_20 = 0, bw_40 = 0;
537 tcb_desc->use_shortgi = false;
542 sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
543 sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
545 if (!(sta->ht_cap.ht_supported))
548 if (!sgi_40 && !sgi_20)
551 if (mac->opmode == NL80211_IFTYPE_STATION)
553 else if (mac->opmode == NL80211_IFTYPE_AP ||
554 mac->opmode == NL80211_IFTYPE_ADHOC ||
555 mac->opmode == NL80211_IFTYPE_MESH_POINT)
556 bw_40 = sta->bandwidth >= IEEE80211_STA_RX_BW_40;
559 tcb_desc->use_shortgi = true;
560 else if ((bw_40 == false) && sgi_20)
561 tcb_desc->use_shortgi = true;
563 if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
564 tcb_desc->use_shortgi = false;
567 static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
568 struct rtl_tcb_desc *tcb_desc,
569 struct ieee80211_tx_info *info)
571 struct rtl_priv *rtlpriv = rtl_priv(hw);
572 u8 rate_flag = info->control.rates[0].flags;
574 /* Common Settings */
575 tcb_desc->rts_stbc = false;
576 tcb_desc->cts_enable = false;
577 tcb_desc->rts_sc = 0;
578 tcb_desc->rts_bw = false;
579 tcb_desc->rts_use_shortpreamble = false;
580 tcb_desc->rts_use_shortgi = false;
582 if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
583 /* Use CTS-to-SELF in protection mode. */
584 tcb_desc->rts_enable = true;
585 tcb_desc->cts_enable = true;
586 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
587 } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
588 /* Use RTS-CTS in protection mode. */
589 tcb_desc->rts_enable = true;
590 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
594 static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
595 struct ieee80211_sta *sta,
596 struct rtl_tcb_desc *tcb_desc)
598 struct rtl_priv *rtlpriv = rtl_priv(hw);
599 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
600 struct rtl_sta_info *sta_entry = NULL;
604 sta_entry = (struct rtl_sta_info *) sta->drv_priv;
605 ratr_index = sta_entry->ratr_index;
607 if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
608 if (mac->opmode == NL80211_IFTYPE_STATION) {
609 tcb_desc->ratr_index = 0;
610 } else if (mac->opmode == NL80211_IFTYPE_ADHOC ||
611 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
612 if (tcb_desc->multicast || tcb_desc->broadcast) {
614 rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
615 tcb_desc->use_driver_rate = 1;
616 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
618 tcb_desc->ratr_index = ratr_index;
620 } else if (mac->opmode == NL80211_IFTYPE_AP) {
621 tcb_desc->ratr_index = ratr_index;
625 if (rtlpriv->dm.useramask) {
626 tcb_desc->ratr_index = ratr_index;
627 /* TODO we will differentiate adhoc and station future */
628 if (mac->opmode == NL80211_IFTYPE_STATION ||
629 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
630 tcb_desc->mac_id = 0;
632 if (mac->mode == WIRELESS_MODE_N_24G)
633 tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
634 else if (mac->mode == WIRELESS_MODE_N_5G)
635 tcb_desc->ratr_index = RATR_INX_WIRELESS_NG;
636 else if (mac->mode & WIRELESS_MODE_G)
637 tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
638 else if (mac->mode & WIRELESS_MODE_B)
639 tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
640 else if (mac->mode & WIRELESS_MODE_A)
641 tcb_desc->ratr_index = RATR_INX_WIRELESS_G;
642 } else if (mac->opmode == NL80211_IFTYPE_AP ||
643 mac->opmode == NL80211_IFTYPE_ADHOC) {
646 tcb_desc->mac_id = sta->aid + 1;
648 tcb_desc->mac_id = 1;
650 tcb_desc->mac_id = 0;
656 static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
657 struct ieee80211_sta *sta,
658 struct rtl_tcb_desc *tcb_desc)
660 struct rtl_priv *rtlpriv = rtl_priv(hw);
661 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
663 tcb_desc->packet_bw = false;
666 if (mac->opmode == NL80211_IFTYPE_AP ||
667 mac->opmode == NL80211_IFTYPE_ADHOC ||
668 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
669 if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
671 } else if (mac->opmode == NL80211_IFTYPE_STATION) {
672 if (!mac->bw_40 || !(sta->ht_cap.ht_supported))
675 if (tcb_desc->multicast || tcb_desc->broadcast)
678 /*use legency rate, shall use 20MHz */
679 if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
682 tcb_desc->packet_bw = true;
685 static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw)
687 struct rtl_priv *rtlpriv = rtl_priv(hw);
688 struct rtl_phy *rtlphy = &(rtlpriv->phy);
691 if (get_rf_type(rtlphy) == RF_2T2R)
692 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
694 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
699 /* mac80211's rate_idx is like this:
701 * 2.4G band:rx_status->band == IEEE80211_BAND_2GHZ
704 * (rx_status->flag & RX_FLAG_HT) = 0,
705 * DESC92_RATE1M-->DESC92_RATE54M ==> idx is 0-->11,
708 * (rx_status->flag & RX_FLAG_HT) = 1,
709 * DESC92_RATEMCS0-->DESC92_RATEMCS15 ==> idx is 0-->15
711 * 5G band:rx_status->band == IEEE80211_BAND_5GHZ
713 * (rx_status->flag & RX_FLAG_HT) = 0,
714 * DESC92_RATE6M-->DESC92_RATE54M ==> idx is 0-->7,
717 * (rx_status->flag & RX_FLAG_HT) = 1,
718 * DESC92_RATEMCS0-->DESC92_RATEMCS15 ==> idx is 0-->15
720 int rtlwifi_rate_mapping(struct ieee80211_hw *hw,
721 bool isht, u8 desc_rate, bool first_ampdu)
726 if (IEEE80211_BAND_2GHZ == hw->conf.chandef.chan->band) {
734 case DESC92_RATE5_5M:
803 case DESC92_RATEMCS0:
806 case DESC92_RATEMCS1:
809 case DESC92_RATEMCS2:
812 case DESC92_RATEMCS3:
815 case DESC92_RATEMCS4:
818 case DESC92_RATEMCS5:
821 case DESC92_RATEMCS6:
824 case DESC92_RATEMCS7:
827 case DESC92_RATEMCS8:
830 case DESC92_RATEMCS9:
833 case DESC92_RATEMCS10:
836 case DESC92_RATEMCS11:
839 case DESC92_RATEMCS12:
842 case DESC92_RATEMCS13:
845 case DESC92_RATEMCS14:
848 case DESC92_RATEMCS15:
858 EXPORT_SYMBOL(rtlwifi_rate_mapping);
860 bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
862 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
863 struct rtl_priv *rtlpriv = rtl_priv(hw);
864 __le16 fc = rtl_get_fc(skb);
866 if (rtlpriv->dm.supp_phymode_switch &&
867 mac->link_state < MAC80211_LINKED &&
868 (ieee80211_is_auth(fc) || ieee80211_is_probe_req(fc))) {
869 if (rtlpriv->cfg->ops->chk_switch_dmdp)
870 rtlpriv->cfg->ops->chk_switch_dmdp(hw);
872 if (ieee80211_is_auth(fc)) {
873 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
876 mac->link_state = MAC80211_LINKING;
878 rtlpriv->phy.need_iqk = true;
884 void rtl_get_tcb_desc(struct ieee80211_hw *hw,
885 struct ieee80211_tx_info *info,
886 struct ieee80211_sta *sta,
887 struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
889 struct rtl_priv *rtlpriv = rtl_priv(hw);
890 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
891 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
892 struct ieee80211_rate *txrate;
893 __le16 fc = hdr->frame_control;
895 txrate = ieee80211_get_tx_rate(hw, info);
897 tcb_desc->hw_rate = txrate->hw_value;
899 tcb_desc->hw_rate = 0;
901 if (ieee80211_is_data(fc)) {
903 *we set data rate INX 0
904 *in rtl_rc.c if skb is special data or
905 *mgt which need low data rate.
909 *So tcb_desc->hw_rate is just used for
910 *special data and mgt frames
912 if (info->control.rates[0].idx == 0 ||
913 ieee80211_is_nullfunc(fc)) {
914 tcb_desc->use_driver_rate = true;
915 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
917 tcb_desc->disable_ratefallback = 1;
920 *because hw will nerver use hw_rate
921 *when tcb_desc->use_driver_rate = false
922 *so we never set highest N rate here,
923 *and N rate will all be controlled by FW
924 *when tcb_desc->use_driver_rate = false
926 if (sta && (sta->ht_cap.ht_supported)) {
927 tcb_desc->hw_rate = _rtl_get_highest_n_rate(hw);
929 if (rtlmac->mode == WIRELESS_MODE_B) {
931 rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
934 rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
939 if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
940 tcb_desc->multicast = 1;
941 else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
942 tcb_desc->broadcast = 1;
944 _rtl_txrate_selectmode(hw, sta, tcb_desc);
945 _rtl_query_bandwidth_mode(hw, sta, tcb_desc);
946 _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
947 _rtl_query_shortgi(hw, sta, tcb_desc, info);
948 _rtl_query_protection_mode(hw, tcb_desc, info);
950 tcb_desc->use_driver_rate = true;
951 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
952 tcb_desc->disable_ratefallback = 1;
953 tcb_desc->mac_id = 0;
954 tcb_desc->packet_bw = false;
957 EXPORT_SYMBOL(rtl_get_tcb_desc);
959 static bool addbareq_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
961 struct rtl_priv *rtlpriv = rtl_priv(hw);
962 struct ieee80211_sta *sta = NULL;
963 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
964 struct rtl_sta_info *sta_entry = NULL;
965 struct ieee80211_mgmt *mgmt = (void *)skb->data;
966 u16 capab = 0, tid = 0;
967 struct rtl_tid_data *tid_data;
968 struct sk_buff *skb_delba = NULL;
969 struct ieee80211_rx_status rx_status = { 0 };
972 sta = rtl_find_sta(hw, hdr->addr3);
974 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_EMERG,
980 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
985 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
986 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
987 tid_data = &sta_entry->tids[tid];
988 if (tid_data->agg.rx_agg_state == RTL_RX_AGG_START) {
989 skb_delba = rtl_make_del_ba(hw, hdr->addr2, hdr->addr3, tid);
991 rx_status.freq = hw->conf.chandef.chan->center_freq;
992 rx_status.band = hw->conf.chandef.chan->band;
993 rx_status.flag |= RX_FLAG_DECRYPTED;
994 rx_status.flag |= RX_FLAG_MACTIME_END;
995 rx_status.rate_idx = 0;
996 rx_status.signal = 50 + 10;
997 memcpy(IEEE80211_SKB_RXCB(skb_delba), &rx_status,
999 RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG,
1000 "fake del\n", skb_delba->data,
1002 ieee80211_rx_irqsafe(hw, skb_delba);
1009 bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
1011 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1012 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
1013 struct rtl_priv *rtlpriv = rtl_priv(hw);
1014 __le16 fc = hdr->frame_control;
1015 u8 *act = (u8 *)skb->data + MAC80211_3ADDR_LEN;
1018 if (!ieee80211_is_action(fc))
1027 if (mac->act_scanning)
1030 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1031 "%s ACT_ADDBAREQ From :%pM\n",
1032 is_tx ? "Tx" : "Rx", hdr->addr2);
1033 RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG, "req\n",
1034 skb->data, skb->len);
1036 if (addbareq_rx(hw, skb))
1040 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1041 "%s ACT_ADDBARSP From :%pM\n",
1042 is_tx ? "Tx" : "Rx", hdr->addr2);
1045 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1046 "ACT_ADDBADEL From :%pM\n", hdr->addr2);
1057 /*should call before software enc*/
1058 u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
1060 struct rtl_priv *rtlpriv = rtl_priv(hw);
1061 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1062 __le16 fc = rtl_get_fc(skb);
1064 u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
1065 const struct iphdr *ip;
1067 if (!ieee80211_is_data(fc))
1070 ip = (const struct iphdr *)(skb->data + mac_hdr_len +
1071 SNAP_SIZE + PROTOC_TYPE_SIZE);
1072 ether_type = be16_to_cpup((__be16 *)
1073 (skb->data + mac_hdr_len + SNAP_SIZE));
1075 switch (ether_type) {
1081 if (ip->protocol != IPPROTO_UDP)
1083 udp = (struct udphdr *)((u8 *)ip + (ip->ihl << 2));
1084 src = be16_to_cpu(udp->source);
1085 dst = be16_to_cpu(udp->dest);
1087 /* If this case involves port 68 (UDP BOOTP client) connecting
1088 * with port 67 (UDP BOOTP server), then return true so that
1089 * the lowest speed is used.
1091 if (!((src == 68 && dst == 67) || (src == 67 && dst == 68)))
1094 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1095 "dhcp %s !!\n", is_tx ? "Tx" : "Rx");
1101 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1102 "802.1X %s EAPOL pkt!!\n", is_tx ? "Tx" : "Rx");
1105 /* TODO: Is this right? */
1111 rtlpriv->enter_ps = false;
1112 schedule_work(&rtlpriv->works.lps_change_work);
1113 ppsc->last_delaylps_stamp_jiffies = jiffies;
1118 /*********************************************************
1120 * functions called by core.c
1122 *********************************************************/
1123 int rtl_tx_agg_start(struct ieee80211_hw *hw,
1124 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1126 struct rtl_priv *rtlpriv = rtl_priv(hw);
1127 struct rtl_tid_data *tid_data;
1128 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1129 struct rtl_sta_info *sta_entry = NULL;
1134 if (unlikely(tid >= MAX_TID_COUNT))
1137 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1140 tid_data = &sta_entry->tids[tid];
1142 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d seq:%d\n",
1143 sta->addr, tid, tid_data->seq_number);
1145 *ssn = tid_data->seq_number;
1146 tid_data->agg.agg_state = RTL_AGG_START;
1148 ieee80211_start_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
1153 int rtl_tx_agg_stop(struct ieee80211_hw *hw,
1154 struct ieee80211_sta *sta, u16 tid)
1156 struct rtl_priv *rtlpriv = rtl_priv(hw);
1157 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1158 struct rtl_sta_info *sta_entry = NULL;
1164 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
1168 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d\n",
1171 if (unlikely(tid >= MAX_TID_COUNT))
1174 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1175 sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
1177 ieee80211_stop_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
1182 int rtl_rx_agg_start(struct ieee80211_hw *hw,
1183 struct ieee80211_sta *sta, u16 tid)
1185 struct rtl_priv *rtlpriv = rtl_priv(hw);
1186 struct rtl_tid_data *tid_data;
1187 struct rtl_sta_info *sta_entry = NULL;
1192 if (unlikely(tid >= MAX_TID_COUNT))
1195 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1198 tid_data = &sta_entry->tids[tid];
1200 RT_TRACE(rtlpriv, COMP_RECV, DBG_DMESG,
1201 "on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1202 tid_data->seq_number);
1204 tid_data->agg.rx_agg_state = RTL_RX_AGG_START;
1208 int rtl_rx_agg_stop(struct ieee80211_hw *hw,
1209 struct ieee80211_sta *sta, u16 tid)
1211 struct rtl_priv *rtlpriv = rtl_priv(hw);
1212 struct rtl_sta_info *sta_entry = NULL;
1218 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
1222 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1223 "on ra = %pM tid = %d\n", sta->addr, tid);
1225 if (unlikely(tid >= MAX_TID_COUNT))
1228 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1229 sta_entry->tids[tid].agg.rx_agg_state = RTL_RX_AGG_STOP;
1234 int rtl_tx_agg_oper(struct ieee80211_hw *hw,
1235 struct ieee80211_sta *sta, u16 tid)
1237 struct rtl_priv *rtlpriv = rtl_priv(hw);
1238 struct rtl_sta_info *sta_entry = NULL;
1244 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
1248 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d\n",
1251 if (unlikely(tid >= MAX_TID_COUNT))
1254 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1255 sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
1260 /*********************************************************
1262 * wq & timer callback functions
1264 *********************************************************/
1265 /* this function is used for roaming */
1266 void rtl_beacon_statistic(struct ieee80211_hw *hw, struct sk_buff *skb)
1268 struct rtl_priv *rtlpriv = rtl_priv(hw);
1269 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1271 if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
1274 if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
1277 /* check if this really is a beacon */
1278 if (!ieee80211_is_beacon(hdr->frame_control) &&
1279 !ieee80211_is_probe_resp(hdr->frame_control))
1282 /* min. beacon length + FCS_LEN */
1283 if (skb->len <= 40 + FCS_LEN)
1286 /* and only beacons from the associated BSSID, please */
1287 if (compare_ether_addr(hdr->addr3, rtlpriv->mac80211.bssid))
1290 rtlpriv->link_info.bcn_rx_inperiod++;
1293 void rtl_watchdog_wq_callback(void *data)
1295 struct rtl_works *rtlworks = container_of_dwork_rtl(data,
1298 struct ieee80211_hw *hw = rtlworks->hw;
1299 struct rtl_priv *rtlpriv = rtl_priv(hw);
1300 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1301 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1302 bool busytraffic = false;
1303 bool tx_busy_traffic = false;
1304 bool rx_busy_traffic = false;
1305 bool higher_busytraffic = false;
1306 bool higher_busyrxtraffic = false;
1308 u32 rx_cnt_inp4eriod = 0;
1309 u32 tx_cnt_inp4eriod = 0;
1310 u32 aver_rx_cnt_inperiod = 0;
1311 u32 aver_tx_cnt_inperiod = 0;
1312 u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
1313 u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
1315 if (is_hal_stop(rtlhal))
1318 /* <1> Determine if action frame is allowed */
1319 if (mac->link_state > MAC80211_NOLINK) {
1320 if (mac->cnt_after_linked < 20)
1321 mac->cnt_after_linked++;
1323 mac->cnt_after_linked = 0;
1327 *<2> to check if traffic busy, if
1328 * busytraffic we don't change channel
1330 if (mac->link_state >= MAC80211_LINKED) {
1332 /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
1333 for (idx = 0; idx <= 2; idx++) {
1334 rtlpriv->link_info.num_rx_in4period[idx] =
1335 rtlpriv->link_info.num_rx_in4period[idx + 1];
1336 rtlpriv->link_info.num_tx_in4period[idx] =
1337 rtlpriv->link_info.num_tx_in4period[idx + 1];
1339 rtlpriv->link_info.num_rx_in4period[3] =
1340 rtlpriv->link_info.num_rx_inperiod;
1341 rtlpriv->link_info.num_tx_in4period[3] =
1342 rtlpriv->link_info.num_tx_inperiod;
1343 for (idx = 0; idx <= 3; idx++) {
1345 rtlpriv->link_info.num_rx_in4period[idx];
1347 rtlpriv->link_info.num_tx_in4period[idx];
1349 aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
1350 aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
1352 /* (2) check traffic busy */
1353 if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100) {
1355 if (aver_rx_cnt_inperiod > aver_tx_cnt_inperiod)
1356 rx_busy_traffic = true;
1358 tx_busy_traffic = false;
1361 /* Higher Tx/Rx data. */
1362 if (aver_rx_cnt_inperiod > 4000 ||
1363 aver_tx_cnt_inperiod > 4000) {
1364 higher_busytraffic = true;
1366 /* Extremely high Rx data. */
1367 if (aver_rx_cnt_inperiod > 5000)
1368 higher_busyrxtraffic = true;
1371 /* check every tid's tx traffic */
1372 for (tid = 0; tid <= 7; tid++) {
1373 for (idx = 0; idx <= 2; idx++)
1374 rtlpriv->link_info.tidtx_in4period[tid][idx] =
1375 rtlpriv->link_info.tidtx_in4period[tid]
1377 rtlpriv->link_info.tidtx_in4period[tid][3] =
1378 rtlpriv->link_info.tidtx_inperiod[tid];
1380 for (idx = 0; idx <= 3; idx++)
1381 tidtx_inp4eriod[tid] +=
1382 rtlpriv->link_info.tidtx_in4period[tid][idx];
1383 aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
1384 if (aver_tidtx_inperiod[tid] > 5000)
1385 rtlpriv->link_info.higher_busytxtraffic[tid] =
1388 rtlpriv->link_info.higher_busytxtraffic[tid] =
1392 if (((rtlpriv->link_info.num_rx_inperiod +
1393 rtlpriv->link_info.num_tx_inperiod) > 8) ||
1394 (rtlpriv->link_info.num_rx_inperiod > 2))
1395 rtlpriv->enter_ps = true;
1397 rtlpriv->enter_ps = false;
1399 /* LeisurePS only work in infra mode. */
1400 schedule_work(&rtlpriv->works.lps_change_work);
1403 rtlpriv->link_info.num_rx_inperiod = 0;
1404 rtlpriv->link_info.num_tx_inperiod = 0;
1405 for (tid = 0; tid <= 7; tid++)
1406 rtlpriv->link_info.tidtx_inperiod[tid] = 0;
1408 rtlpriv->link_info.busytraffic = busytraffic;
1409 rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
1410 rtlpriv->link_info.rx_busy_traffic = rx_busy_traffic;
1411 rtlpriv->link_info.tx_busy_traffic = tx_busy_traffic;
1412 rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
1415 rtlpriv->cfg->ops->dm_watchdog(hw);
1418 if (mac->link_state == MAC80211_LINKED &&
1419 mac->opmode == NL80211_IFTYPE_STATION) {
1420 if ((rtlpriv->link_info.bcn_rx_inperiod +
1421 rtlpriv->link_info.num_rx_inperiod) == 0) {
1422 rtlpriv->link_info.roam_times++;
1423 RT_TRACE(rtlpriv, COMP_ERR, DBG_DMESG,
1424 "AP off for %d s\n",
1425 (rtlpriv->link_info.roam_times * 2));
1427 /* if we can't recv beacon for 6s, we should
1430 if ((rtlpriv->link_info.roam_times >= 3) &&
1431 !is_zero_ether_addr(rtlpriv->mac80211.bssid)) {
1432 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1433 "AP off, try to reconnect now\n");
1434 rtlpriv->link_info.roam_times = 0;
1435 ieee80211_connection_loss(rtlpriv->mac80211.vif);
1438 rtlpriv->link_info.roam_times = 0;
1441 rtlpriv->link_info.bcn_rx_inperiod = 0;
1444 void rtl_watch_dog_timer_callback(unsigned long data)
1446 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1447 struct rtl_priv *rtlpriv = rtl_priv(hw);
1449 queue_delayed_work(rtlpriv->works.rtl_wq,
1450 &rtlpriv->works.watchdog_wq, 0);
1452 mod_timer(&rtlpriv->works.watchdog_timer,
1453 jiffies + MSECS(RTL_WATCH_DOG_TIME));
1456 void rtl_fwevt_wq_callback(void *data)
1458 struct rtl_works *rtlworks =
1459 container_of_dwork_rtl(data, struct rtl_works, fwevt_wq);
1460 struct ieee80211_hw *hw = rtlworks->hw;
1461 struct rtl_priv *rtlpriv = rtl_priv(hw);
1463 rtlpriv->cfg->ops->c2h_command_handle(hw);
1466 void rtl_easy_concurrent_retrytimer_callback(unsigned long data)
1468 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1469 struct rtl_priv *rtlpriv = rtl_priv(hw);
1470 struct rtl_priv *buddy_priv = rtlpriv->buddy_priv;
1472 if (buddy_priv == NULL)
1475 rtlpriv->cfg->ops->dualmac_easy_concurrent(hw);
1478 /*********************************************************
1480 * frame process functions
1482 *********************************************************/
1483 u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
1485 struct ieee80211_mgmt *mgmt = (void *)data;
1488 pos = (u8 *)mgmt->u.beacon.variable;
1491 if (pos + 2 + pos[1] > end)
1502 /* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
1503 /* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
1504 static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
1505 enum ieee80211_smps_mode smps, u8 *da, u8 *bssid)
1507 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1508 struct sk_buff *skb;
1509 struct ieee80211_mgmt *action_frame;
1511 /* 27 = header + category + action + smps mode */
1512 skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
1516 skb_reserve(skb, hw->extra_tx_headroom);
1517 action_frame = (void *)skb_put(skb, 27);
1518 memset(action_frame, 0, 27);
1519 memcpy(action_frame->da, da, ETH_ALEN);
1520 memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
1521 memcpy(action_frame->bssid, bssid, ETH_ALEN);
1522 action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1523 IEEE80211_STYPE_ACTION);
1524 action_frame->u.action.category = WLAN_CATEGORY_HT;
1525 action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
1527 case IEEE80211_SMPS_AUTOMATIC:/* 0 */
1528 case IEEE80211_SMPS_NUM_MODES:/* 4 */
1530 case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
1531 action_frame->u.action.u.ht_smps.smps_control =
1532 WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
1534 case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
1535 action_frame->u.action.u.ht_smps.smps_control =
1536 WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
1538 case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
1539 action_frame->u.action.u.ht_smps.smps_control =
1540 WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
1547 int rtl_send_smps_action(struct ieee80211_hw *hw,
1548 struct ieee80211_sta *sta,
1549 enum ieee80211_smps_mode smps)
1551 struct rtl_priv *rtlpriv = rtl_priv(hw);
1552 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1553 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1554 struct sk_buff *skb = NULL;
1555 struct rtl_tcb_desc tcb_desc;
1556 u8 bssid[ETH_ALEN] = {0};
1558 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
1560 if (rtlpriv->mac80211.act_scanning)
1566 if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
1569 if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1572 if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP)
1573 memcpy(bssid, rtlpriv->efuse.dev_addr, ETH_ALEN);
1575 memcpy(bssid, rtlpriv->mac80211.bssid, ETH_ALEN);
1577 skb = rtl_make_smps_action(hw, smps, sta->addr, bssid);
1578 /* this is a type = mgmt * stype = action frame */
1580 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1581 struct rtl_sta_info *sta_entry =
1582 (struct rtl_sta_info *) sta->drv_priv;
1583 sta_entry->mimo_ps = smps;
1585 info->control.rates[0].idx = 0;
1586 info->band = hw->conf.chandef.chan->band;
1587 rtlpriv->intf_ops->adapter_tx(hw, sta, skb, &tcb_desc);
1594 EXPORT_SYMBOL(rtl_send_smps_action);
1596 /* There seem to be issues in mac80211 regarding when del ba frames can be
1597 * received. As a work around, we make a fake del_ba if we receive a ba_req;
1598 * however, rx_agg was opened to let mac80211 release some ba related
1599 * resources. This del_ba is for tx only.
1601 struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw,
1602 u8 *sa, u8 *bssid, u16 tid)
1604 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1605 struct sk_buff *skb;
1606 struct ieee80211_mgmt *action_frame;
1609 /* 27 = header + category + action + smps mode */
1610 skb = dev_alloc_skb(34 + hw->extra_tx_headroom);
1614 skb_reserve(skb, hw->extra_tx_headroom);
1615 action_frame = (void *)skb_put(skb, 34);
1616 memset(action_frame, 0, 34);
1617 memcpy(action_frame->sa, sa, ETH_ALEN);
1618 memcpy(action_frame->da, rtlefuse->dev_addr, ETH_ALEN);
1619 memcpy(action_frame->bssid, bssid, ETH_ALEN);
1620 action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1621 IEEE80211_STYPE_ACTION);
1622 action_frame->u.action.category = WLAN_CATEGORY_BACK;
1623 action_frame->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1624 params = (u16)(1 << 11); /* bit 11 initiator */
1625 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1627 action_frame->u.action.u.delba.params = cpu_to_le16(params);
1628 action_frame->u.action.u.delba.reason_code =
1629 cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
1634 /*********************************************************
1638 *********************************************************/
1639 static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
1640 struct octet_string vendor_ie)
1642 struct rtl_priv *rtlpriv = rtl_priv(hw);
1643 bool matched = false;
1644 static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
1645 static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
1646 static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
1647 static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
1648 static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
1649 static u8 racap[] = { 0x00, 0x0c, 0x43 };
1650 static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
1651 static u8 marvcap[] = { 0x00, 0x50, 0x43 };
1653 if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
1654 memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
1655 rtlpriv->mac80211.vendor = PEER_ATH;
1657 } else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
1658 memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
1659 memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
1660 rtlpriv->mac80211.vendor = PEER_BROAD;
1662 } else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
1663 rtlpriv->mac80211.vendor = PEER_RAL;
1665 } else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
1666 rtlpriv->mac80211.vendor = PEER_CISCO;
1668 } else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
1669 rtlpriv->mac80211.vendor = PEER_MARV;
1676 static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
1679 struct ieee80211_mgmt *mgmt = (void *)data;
1680 struct octet_string vendor_ie;
1683 pos = (u8 *)mgmt->u.beacon.variable;
1686 if (pos[0] == 221) {
1687 vendor_ie.length = pos[1];
1688 vendor_ie.octet = &pos[2];
1689 if (rtl_chk_vendor_ouisub(hw, vendor_ie))
1693 if (pos + 2 + pos[1] > end)
1701 void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
1703 struct rtl_priv *rtlpriv = rtl_priv(hw);
1704 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1705 struct ieee80211_hdr *hdr = (void *)data;
1706 u32 vendor = PEER_UNKNOWN;
1708 static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
1709 static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
1710 static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
1711 static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
1712 static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
1713 static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
1714 static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
1715 static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
1716 static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
1717 static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
1718 static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
1719 static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
1720 static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
1721 static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
1722 static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
1723 static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
1725 if (mac->opmode != NL80211_IFTYPE_STATION)
1728 if (mac->link_state == MAC80211_NOLINK) {
1729 mac->vendor = PEER_UNKNOWN;
1733 if (mac->cnt_after_linked > 2)
1736 /* check if this really is a beacon */
1737 if (!ieee80211_is_beacon(hdr->frame_control))
1740 /* min. beacon length + FCS_LEN */
1741 if (len <= 40 + FCS_LEN)
1744 /* and only beacons from the associated BSSID, please */
1745 if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
1748 if (rtl_find_221_ie(hw, data, len))
1749 vendor = mac->vendor;
1751 if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
1752 (memcmp(mac->bssid, ap5_2, 3) == 0) ||
1753 (memcmp(mac->bssid, ap5_3, 3) == 0) ||
1754 (memcmp(mac->bssid, ap5_4, 3) == 0) ||
1755 (memcmp(mac->bssid, ap5_5, 3) == 0) ||
1756 (memcmp(mac->bssid, ap5_6, 3) == 0) ||
1757 vendor == PEER_ATH) {
1759 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ath find\n");
1760 } else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
1761 (memcmp(mac->bssid, ap4_5, 3) == 0) ||
1762 (memcmp(mac->bssid, ap4_1, 3) == 0) ||
1763 (memcmp(mac->bssid, ap4_2, 3) == 0) ||
1764 (memcmp(mac->bssid, ap4_3, 3) == 0) ||
1765 vendor == PEER_RAL) {
1766 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ral find\n");
1768 } else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
1769 vendor == PEER_CISCO) {
1770 vendor = PEER_CISCO;
1771 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>cisco find\n");
1772 } else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
1773 (memcmp(mac->bssid, ap3_2, 3) == 0) ||
1774 (memcmp(mac->bssid, ap3_3, 3) == 0) ||
1775 vendor == PEER_BROAD) {
1776 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>broad find\n");
1777 vendor = PEER_BROAD;
1778 } else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
1779 vendor == PEER_MARV) {
1781 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>marv find\n");
1784 mac->vendor = vendor;
1787 /*********************************************************
1791 *********************************************************/
1792 static ssize_t rtl_show_debug_level(struct device *d,
1793 struct device_attribute *attr, char *buf)
1795 struct ieee80211_hw *hw = dev_get_drvdata(d);
1796 struct rtl_priv *rtlpriv = rtl_priv(hw);
1798 return sprintf(buf, "0x%08X\n", rtlpriv->dbg.global_debuglevel);
1801 static ssize_t rtl_store_debug_level(struct device *d,
1802 struct device_attribute *attr,
1803 const char *buf, size_t count)
1805 struct ieee80211_hw *hw = dev_get_drvdata(d);
1806 struct rtl_priv *rtlpriv = rtl_priv(hw);
1810 ret = strict_strtoul(buf, 0, &val);
1812 printk(KERN_DEBUG "%s is not in hex or decimal form.\n", buf);
1814 rtlpriv->dbg.global_debuglevel = val;
1815 printk(KERN_DEBUG "debuglevel:%x\n",
1816 rtlpriv->dbg.global_debuglevel);
1819 return strnlen(buf, count);
1822 static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
1823 rtl_show_debug_level, rtl_store_debug_level);
1825 static struct attribute *rtl_sysfs_entries[] = {
1827 &dev_attr_debug_level.attr,
1833 * "name" is folder name witch will be
1834 * put in device directory like :
1835 * sys/devices/pci0000:00/0000:00:1c.4/
1836 * 0000:06:00.0/rtl_sysfs
1838 struct attribute_group rtl_attribute_group = {
1840 .attrs = rtl_sysfs_entries,
1843 MODULE_AUTHOR("lizhaoming <chaoming_li@realsil.com.cn>");
1844 MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>");
1845 MODULE_AUTHOR("Larry Finger <Larry.FInger@lwfinger.net>");
1846 MODULE_LICENSE("GPL");
1847 MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
1849 struct rtl_global_var global_var = {};
1851 static int __init rtl_core_module_init(void)
1853 if (rtl_rate_control_register())
1854 pr_err("Unable to register rtl_rc, use default RC !!\n");
1856 /* init some global vars */
1857 INIT_LIST_HEAD(&global_var.glb_priv_list);
1858 spin_lock_init(&global_var.glb_list_lock);
1863 static void __exit rtl_core_module_exit(void)
1866 rtl_rate_control_unregister();
1869 module_init(rtl_core_module_init);
1870 module_exit(rtl_core_module_exit);