1 /******************************************************************************
3 * Copyright(c) 2009-2010 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 *****************************************************************************/
31 #include <linux/module.h>
42 *NOTICE!!!: This file will be very big, we hsould
43 *keep it clear under follwing roles:
45 *This file include follwing part, so, if you add new
46 *functions into this file, please check which part it
47 *should includes. or check if you should add new part
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
61 /*********************************************************
63 * mac80211 init functions
65 *********************************************************/
66 static struct ieee80211_channel rtl_channeltable_2g[] = {
67 {.center_freq = 2412,.hw_value = 1,},
68 {.center_freq = 2417,.hw_value = 2,},
69 {.center_freq = 2422,.hw_value = 3,},
70 {.center_freq = 2427,.hw_value = 4,},
71 {.center_freq = 2432,.hw_value = 5,},
72 {.center_freq = 2437,.hw_value = 6,},
73 {.center_freq = 2442,.hw_value = 7,},
74 {.center_freq = 2447,.hw_value = 8,},
75 {.center_freq = 2452,.hw_value = 9,},
76 {.center_freq = 2457,.hw_value = 10,},
77 {.center_freq = 2462,.hw_value = 11,},
78 {.center_freq = 2467,.hw_value = 12,},
79 {.center_freq = 2472,.hw_value = 13,},
80 {.center_freq = 2484,.hw_value = 14,},
83 static struct ieee80211_channel rtl_channeltable_5g[] = {
84 {.center_freq = 5180,.hw_value = 36,},
85 {.center_freq = 5200,.hw_value = 40,},
86 {.center_freq = 5220,.hw_value = 44,},
87 {.center_freq = 5240,.hw_value = 48,},
88 {.center_freq = 5260,.hw_value = 52,},
89 {.center_freq = 5280,.hw_value = 56,},
90 {.center_freq = 5300,.hw_value = 60,},
91 {.center_freq = 5320,.hw_value = 64,},
92 {.center_freq = 5500,.hw_value = 100,},
93 {.center_freq = 5520,.hw_value = 104,},
94 {.center_freq = 5540,.hw_value = 108,},
95 {.center_freq = 5560,.hw_value = 112,},
96 {.center_freq = 5580,.hw_value = 116,},
97 {.center_freq = 5600,.hw_value = 120,},
98 {.center_freq = 5620,.hw_value = 124,},
99 {.center_freq = 5640,.hw_value = 128,},
100 {.center_freq = 5660,.hw_value = 132,},
101 {.center_freq = 5680,.hw_value = 136,},
102 {.center_freq = 5700,.hw_value = 140,},
103 {.center_freq = 5745,.hw_value = 149,},
104 {.center_freq = 5765,.hw_value = 153,},
105 {.center_freq = 5785,.hw_value = 157,},
106 {.center_freq = 5805,.hw_value = 161,},
107 {.center_freq = 5825,.hw_value = 165,},
110 static struct ieee80211_rate rtl_ratetable_2g[] = {
111 {.bitrate = 10,.hw_value = 0x00,},
112 {.bitrate = 20,.hw_value = 0x01,},
113 {.bitrate = 55,.hw_value = 0x02,},
114 {.bitrate = 110,.hw_value = 0x03,},
115 {.bitrate = 60,.hw_value = 0x04,},
116 {.bitrate = 90,.hw_value = 0x05,},
117 {.bitrate = 120,.hw_value = 0x06,},
118 {.bitrate = 180,.hw_value = 0x07,},
119 {.bitrate = 240,.hw_value = 0x08,},
120 {.bitrate = 360,.hw_value = 0x09,},
121 {.bitrate = 480,.hw_value = 0x0a,},
122 {.bitrate = 540,.hw_value = 0x0b,},
125 static struct ieee80211_rate rtl_ratetable_5g[] = {
126 {.bitrate = 60,.hw_value = 0x04,},
127 {.bitrate = 90,.hw_value = 0x05,},
128 {.bitrate = 120,.hw_value = 0x06,},
129 {.bitrate = 180,.hw_value = 0x07,},
130 {.bitrate = 240,.hw_value = 0x08,},
131 {.bitrate = 360,.hw_value = 0x09,},
132 {.bitrate = 480,.hw_value = 0x0a,},
133 {.bitrate = 540,.hw_value = 0x0b,},
136 static const struct ieee80211_supported_band rtl_band_2ghz = {
137 .band = IEEE80211_BAND_2GHZ,
139 .channels = rtl_channeltable_2g,
140 .n_channels = ARRAY_SIZE(rtl_channeltable_2g),
142 .bitrates = rtl_ratetable_2g,
143 .n_bitrates = ARRAY_SIZE(rtl_ratetable_2g),
148 static struct ieee80211_supported_band rtl_band_5ghz = {
149 .band = IEEE80211_BAND_5GHZ,
151 .channels = rtl_channeltable_5g,
152 .n_channels = ARRAY_SIZE(rtl_channeltable_5g),
154 .bitrates = rtl_ratetable_5g,
155 .n_bitrates = ARRAY_SIZE(rtl_ratetable_5g),
160 static const u8 tid_to_ac[] = {
161 2, /* IEEE80211_AC_BE */
162 3, /* IEEE80211_AC_BK */
163 3, /* IEEE80211_AC_BK */
164 2, /* IEEE80211_AC_BE */
165 1, /* IEEE80211_AC_VI */
166 1, /* IEEE80211_AC_VI */
167 0, /* IEEE80211_AC_VO */
168 0, /* IEEE80211_AC_VO */
171 u8 rtl_tid_to_ac(struct ieee80211_hw *hw, u8 tid)
173 return tid_to_ac[tid];
176 static void _rtl_init_hw_ht_capab(struct ieee80211_hw *hw,
177 struct ieee80211_sta_ht_cap *ht_cap)
179 struct rtl_priv *rtlpriv = rtl_priv(hw);
180 struct rtl_phy *rtlphy = &(rtlpriv->phy);
182 ht_cap->ht_supported = true;
183 ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
184 IEEE80211_HT_CAP_SGI_40 |
185 IEEE80211_HT_CAP_SGI_20 |
186 IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU;
188 if (rtlpriv->rtlhal.disable_amsdu_8k)
189 ht_cap->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
192 *Maximum length of AMPDU that the STA can receive.
193 *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
195 ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
197 /*Minimum MPDU start spacing , */
198 ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
200 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) {
213 RT_TRACE(COMP_INIT, DBG_EMERG, ("Support phy mode switch\n"));
215 ht_cap->mcs.rx_mask[0] = 0xFF;
216 ht_cap->mcs.rx_mask[1] = 0xFF;
217 ht_cap->mcs.rx_mask[4] = 0x01;
219 ht_cap->mcs.rx_highest = MAX_BIT_RATE_40MHZ_MCS15;
221 if (get_rf_type(rtlphy) == RF_1T2R ||
222 get_rf_type(rtlphy) == RF_2T2R) {
224 RT_TRACE(COMP_INIT, DBG_DMESG, ("1T2R or 2T2R\n"));
226 ht_cap->mcs.rx_mask[0] = 0xFF;
227 ht_cap->mcs.rx_mask[1] = 0xFF;
228 ht_cap->mcs.rx_mask[4] = 0x01;
230 ht_cap->mcs.rx_highest = MAX_BIT_RATE_40MHZ_MCS15;
231 } else if (get_rf_type(rtlphy) == RF_1T1R) {
233 RT_TRACE(COMP_INIT, DBG_DMESG, ("1T1R\n"));
235 ht_cap->mcs.rx_mask[0] = 0xFF;
236 ht_cap->mcs.rx_mask[1] = 0x00;
237 ht_cap->mcs.rx_mask[4] = 0x01;
239 ht_cap->mcs.rx_highest = MAX_BIT_RATE_40MHZ_MCS7;
244 static void _rtl_init_mac80211(struct ieee80211_hw *hw)
246 struct rtl_priv *rtlpriv = rtl_priv(hw);
247 struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
248 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
249 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
250 struct ieee80211_supported_band *sband;
253 if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY &&
254 rtlhal->bandset == BAND_ON_BOTH) {
256 /* <1> use mac->bands as mem for hw->wiphy->bands */
257 sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
259 /* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
260 * to default value(1T1R) */
261 memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]), &rtl_band_2ghz,
262 sizeof(struct ieee80211_supported_band));
264 /* <3> init ht cap base on ant_num */
265 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
267 /* <4> set mac->sband to wiphy->sband */
268 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
271 /* <1> use mac->bands as mem for hw->wiphy->bands */
272 sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
274 /* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
275 * to default value(1T1R) */
276 memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]), &rtl_band_5ghz,
277 sizeof(struct ieee80211_supported_band));
279 /* <3> init ht cap base on ant_num */
280 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
282 /* <4> set mac->sband to wiphy->sband */
283 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
285 if (rtlhal->current_bandtype == BAND_ON_2_4G) {
286 /* <1> use mac->bands as mem for hw->wiphy->bands */
287 sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
289 /* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
290 * to default value(1T1R) */
291 memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]),
293 sizeof(struct ieee80211_supported_band));
295 /* <3> init ht cap base on ant_num */
296 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
298 /* <4> set mac->sband to wiphy->sband */
299 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
300 } else if (rtlhal->current_bandtype == BAND_ON_5G) {
301 /* <1> use mac->bands as mem for hw->wiphy->bands */
302 sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
304 /* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
305 * to default value(1T1R) */
306 memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]),
308 sizeof(struct ieee80211_supported_band));
310 /* <3> init ht cap base on ant_num */
311 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
313 /* <4> set mac->sband to wiphy->sband */
314 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
316 RT_TRACE(COMP_INIT, DBG_EMERG, ("Err BAND %d\n",
317 rtlhal->current_bandtype));
320 /* <5> set hw caps */
321 hw->flags = IEEE80211_HW_SIGNAL_DBM |
322 IEEE80211_HW_RX_INCLUDES_FCS |
323 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,4,0))
324 IEEE80211_HW_BEACON_FILTER |
326 IEEE80211_HW_AMPDU_AGGREGATION |
327 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
328 IEEE80211_HW_CONNECTION_MONITOR |
329 /* IEEE80211_HW_SUPPORTS_CQM_RSSI | */
330 IEEE80211_HW_MFP_CAPABLE | 0;
332 /* swlps or hwlps has been set in diff chip in init_sw_vars */
333 if (rtlpriv->psc.b_swctrl_lps)
334 hw->flags |= IEEE80211_HW_SUPPORTS_PS |
335 IEEE80211_HW_PS_NULLFUNC_STACK |
336 /* IEEE80211_HW_SUPPORTS_DYNAMIC_PS | */
338 /*<delete in kernel start>*/
339 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
340 hw->wiphy->interface_modes =
341 BIT(NL80211_IFTYPE_AP) |
342 BIT(NL80211_IFTYPE_STATION) |
343 BIT(NL80211_IFTYPE_ADHOC) |
344 BIT(NL80211_IFTYPE_MESH_POINT) |
345 BIT(NL80211_IFTYPE_P2P_CLIENT) |
346 BIT(NL80211_IFTYPE_P2P_GO);
348 /*<delete in kernel end>*/
349 hw->wiphy->interface_modes =
350 BIT(NL80211_IFTYPE_AP) |
351 BIT(NL80211_IFTYPE_STATION) |
352 BIT(NL80211_IFTYPE_ADHOC) |
353 BIT(NL80211_IFTYPE_MESH_POINT) ;
354 /*<delete in kernel start>*/
356 /*<delete in kernel end>*/
357 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,39))
358 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
361 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,3,0))
362 hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
365 hw->wiphy->rts_threshold = 2347;
368 hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;
370 /* TODO: Correct this value for our hw */
371 /* TODO: define these hard code value */
372 hw->channel_change_time = 100;
373 hw->max_listen_interval = 10;
374 hw->max_rate_tries = 4;
375 /* hw->max_rates = 1; */
376 hw->sta_data_size = sizeof(struct rtl_sta_info);
378 hw->vif_data_size = sizeof(struct rtl_vif_info);
381 /* <6> mac address */
382 if (is_valid_ether_addr(rtlefuse->dev_addr)) {
383 SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
385 u8 rtlmac[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
386 get_random_bytes((rtlmac + (ETH_ALEN - 1)), 1);
387 SET_IEEE80211_PERM_ADDR(hw, rtlmac);
392 static void _rtl_init_deferred_work(struct ieee80211_hw *hw)
394 struct rtl_priv *rtlpriv = rtl_priv(hw);
397 init_timer(&rtlpriv->works.watchdog_timer);
398 setup_timer(&rtlpriv->works.watchdog_timer,
399 rtl_watch_dog_timer_callback, (unsigned long)hw);
400 init_timer(&rtlpriv->works.dualmac_easyconcurrent_retrytimer);
401 setup_timer(&rtlpriv->works.dualmac_easyconcurrent_retrytimer,
402 rtl_easy_concurrent_retrytimer_callback, (unsigned long)hw);
404 rtlpriv->works.hw = hw;
405 /*<delete in kernel start>*/
406 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
407 /*<delete in kernel end>*/
408 rtlpriv->works.rtl_wq = alloc_workqueue(rtlpriv->cfg->name, 0, 0);
409 /*<delete in kernel start>*/
411 rtlpriv->works.rtl_wq = create_workqueue(rtlpriv->cfg->name);
413 /*<delete in kernel end>*/
414 INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
415 (void *)rtl_watchdog_wq_callback);
416 INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
417 (void *)rtl_ips_nic_off_wq_callback);
418 INIT_DELAYED_WORK(&rtlpriv->works.ps_work,
419 (void *)rtl_swlps_wq_callback);
420 INIT_DELAYED_WORK(&rtlpriv->works.ps_rfon_wq,
421 (void *)rtl_swlps_rfon_wq_callback);
422 INIT_DELAYED_WORK(&rtlpriv->works.fwevt_wq,
423 (void *)rtl_fwevt_wq_callback);
427 void rtl_deinit_deferred_work(struct ieee80211_hw *hw)
429 struct rtl_priv *rtlpriv = rtl_priv(hw);
431 del_timer_sync(&rtlpriv->works.watchdog_timer);
433 cancel_delayed_work(&rtlpriv->works.watchdog_wq);
434 cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
435 cancel_delayed_work(&rtlpriv->works.ps_work);
436 cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
437 cancel_delayed_work(&rtlpriv->works.fwevt_wq);
440 void rtl_init_rfkill(struct ieee80211_hw *hw)
442 struct rtl_priv *rtlpriv = rtl_priv(hw);
448 /*set init state to on */
449 rtlpriv->rfkill.rfkill_state = 1;
450 wiphy_rfkill_set_hw_state(hw->wiphy, 0);
452 radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
455 printk(KERN_INFO "rtlwifi: wireless switch is %s\n",
456 rtlpriv->rfkill.rfkill_state ? "on" : "off");
458 rtlpriv->rfkill.rfkill_state = radio_state;
460 blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
461 wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
464 wiphy_rfkill_start_polling(hw->wiphy);
467 void rtl_deinit_rfkill(struct ieee80211_hw *hw)
469 wiphy_rfkill_stop_polling(hw->wiphy);
473 static void rtl_init_vif(struct ieee80211_hw *hw)
475 struct rtl_priv *rtlpriv = rtl_priv(hw);
477 INIT_LIST_HEAD(&rtlpriv->vif_priv.vif_list);
479 rtlpriv->vif_priv.vifs = 0;
483 int rtl_init_core(struct ieee80211_hw *hw)
485 struct rtl_priv *rtlpriv = rtl_priv(hw);
486 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
488 /* <1> init mac80211 */
489 _rtl_init_mac80211(hw);
491 rtlmac->link_state = MAC80211_NOLINK;
493 /* <2> rate control register */
494 hw->rate_control_algorithm = "rtl_rc";
497 * <3> init CRDA must come after init
498 * mac80211 hw in _rtl_init_mac80211.
500 if (rtl_regd_init(hw, rtl_reg_notifier)) {
501 RT_TRACE(COMP_ERR, DBG_EMERG, ("REGD init failed\n"));
506 mutex_init(&rtlpriv->locks.conf_mutex);
507 spin_lock_init(&rtlpriv->locks.ips_lock);
508 spin_lock_init(&rtlpriv->locks.irq_th_lock);
509 spin_lock_init(&rtlpriv->locks.h2c_lock);
510 spin_lock_init(&rtlpriv->locks.rf_ps_lock);
511 spin_lock_init(&rtlpriv->locks.rf_lock);
512 spin_lock_init(&rtlpriv->locks.lps_lock);
513 spin_lock_init(&rtlpriv->locks.waitq_lock);
514 spin_lock_init(&rtlpriv->locks.entry_list_lock);
515 spin_lock_init(&rtlpriv->locks.cck_and_rw_pagea_lock);
516 spin_lock_init(&rtlpriv->locks.check_sendpkt_lock);
517 spin_lock_init(&rtlpriv->locks.fw_ps_lock);
518 spin_lock_init(&rtlpriv->locks.iqk_lock);
520 INIT_LIST_HEAD(&rtlpriv->entry_list);
522 /* <6> init deferred work */
523 _rtl_init_deferred_work(hw);
533 void rtl_deinit_core(struct ieee80211_hw *hw)
537 void rtl_init_rx_config(struct ieee80211_hw *hw)
539 struct rtl_priv *rtlpriv = rtl_priv(hw);
540 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
542 rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
545 /*********************************************************
547 * tx information functions
549 *********************************************************/
550 static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
551 struct rtl_tcb_desc *tcb_desc,
552 struct ieee80211_tx_info *info)
554 struct rtl_priv *rtlpriv = rtl_priv(hw);
555 u8 rate_flag = info->control.rates[0].flags;
557 tcb_desc->use_shortpreamble = false;
559 /* 1M can only use Long Preamble. 11B spec */
560 if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
562 else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
563 tcb_desc->use_shortpreamble = true;
568 static void _rtl_query_shortgi(struct ieee80211_hw *hw,
569 struct ieee80211_sta *sta,
570 struct rtl_tcb_desc *tcb_desc,
571 struct ieee80211_tx_info *info)
573 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
574 u8 rate_flag = info->control.rates[0].flags;
575 u8 sgi_40 = 0, sgi_20 = 0, bw_40 = 0;
576 tcb_desc->use_shortgi = false;
581 sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
582 sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
584 if (!(sta->ht_cap.ht_supported))
587 if (!sgi_40 && !sgi_20)
590 if (mac->opmode == NL80211_IFTYPE_STATION)
592 else if (mac->opmode == NL80211_IFTYPE_AP ||
593 mac->opmode == NL80211_IFTYPE_ADHOC ||
594 mac->opmode == NL80211_IFTYPE_MESH_POINT)
595 bw_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
597 if ((bw_40 == true) && sgi_40)
598 tcb_desc->use_shortgi = true;
599 else if ((bw_40 == false) && sgi_20)
600 tcb_desc->use_shortgi = true;
602 if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
603 tcb_desc->use_shortgi = false;
606 static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
607 struct rtl_tcb_desc *tcb_desc,
608 struct ieee80211_tx_info *info)
610 struct rtl_priv *rtlpriv = rtl_priv(hw);
611 u8 rate_flag = info->control.rates[0].flags;
613 /* Common Settings */
614 tcb_desc->b_rts_stbc = false;
615 tcb_desc->b_cts_enable = false;
616 tcb_desc->rts_sc = 0;
617 tcb_desc->b_rts_bw = false;
618 tcb_desc->b_rts_use_shortpreamble = false;
619 tcb_desc->b_rts_use_shortgi = false;
621 if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
622 /* Use CTS-to-SELF in protection mode. */
623 tcb_desc->b_rts_enable = true;
624 tcb_desc->b_cts_enable = true;
625 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
626 } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
627 /* Use RTS-CTS in protection mode. */
628 tcb_desc->b_rts_enable = true;
629 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
633 static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
634 struct ieee80211_sta *sta,
635 struct rtl_tcb_desc *tcb_desc)
637 struct rtl_priv *rtlpriv = rtl_priv(hw);
638 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
639 struct rtl_sta_info *sta_entry = NULL;
643 sta_entry = (struct rtl_sta_info *) sta->drv_priv;
644 ratr_index = sta_entry->ratr_index;
646 if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
647 if (mac->opmode == NL80211_IFTYPE_STATION) {
648 tcb_desc->ratr_index = 0;
649 } else if (mac->opmode == NL80211_IFTYPE_ADHOC ||
650 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
651 if (tcb_desc->b_multicast || tcb_desc->b_broadcast) {
653 rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
654 tcb_desc->use_driver_rate = 1;
655 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
657 tcb_desc->ratr_index = ratr_index;
659 } else if (mac->opmode == NL80211_IFTYPE_AP) {
660 tcb_desc->ratr_index = ratr_index;
664 if (rtlpriv->dm.b_useramask) {
665 tcb_desc->ratr_index = ratr_index;
666 /* TODO we will differentiate adhoc and station futrue */
667 if (mac->opmode == NL80211_IFTYPE_STATION ||
668 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
669 tcb_desc->mac_id = 0;
671 if (mac->mode == WIRELESS_MODE_N_24G) {
672 tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
673 } else if (mac->mode == WIRELESS_MODE_N_5G) {
674 tcb_desc->ratr_index = RATR_INX_WIRELESS_NG;
675 } else if (mac->mode & WIRELESS_MODE_G) {
676 tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
677 } else if (mac->mode & WIRELESS_MODE_B) {
678 tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
679 } else if (mac->mode & WIRELESS_MODE_A) {
680 tcb_desc->ratr_index = RATR_INX_WIRELESS_G;
682 } else if (mac->opmode == NL80211_IFTYPE_AP ||
683 mac->opmode == NL80211_IFTYPE_ADHOC) {
686 tcb_desc->mac_id = sta->aid + 1;
688 tcb_desc->mac_id = 1;
691 tcb_desc->mac_id = 0;
697 static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
698 struct ieee80211_sta *sta,
699 struct rtl_tcb_desc *tcb_desc)
701 struct rtl_priv *rtlpriv = rtl_priv(hw);
702 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
704 tcb_desc->b_packet_bw = false;
707 if (mac->opmode == NL80211_IFTYPE_AP ||
708 mac->opmode == NL80211_IFTYPE_ADHOC ||
709 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
710 if (!(sta->ht_cap.ht_supported) ||
711 !(sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
713 } else if (mac->opmode == NL80211_IFTYPE_STATION) {
714 if (!mac->bw_40 || !(sta->ht_cap.ht_supported))
717 if (tcb_desc->b_multicast || tcb_desc->b_broadcast)
720 /*use legency rate, shall use 20MHz */
721 if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
724 tcb_desc->b_packet_bw = true;
727 static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw,
728 struct ieee80211_sta *sta)
730 struct rtl_priv *rtlpriv = rtl_priv(hw);
731 struct rtl_phy *rtlphy = &(rtlpriv->phy);
734 if ((get_rf_type(rtlphy) == RF_2T2R) && (sta->ht_cap.mcs.rx_mask[1]!=0))
735 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
737 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
742 void rtl_get_tcb_desc(struct ieee80211_hw *hw,
743 struct ieee80211_tx_info *info,
744 struct ieee80211_sta *sta,
745 struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
747 struct rtl_priv *rtlpriv = rtl_priv(hw);
748 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
749 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
750 struct ieee80211_rate *txrate;
751 u16 fc = rtl_get_fc(skb);
753 txrate = ieee80211_get_tx_rate(hw, info);
755 tcb_desc->hw_rate = txrate->hw_value;
757 if (ieee80211_is_data(fc)) {
759 *we set data rate INX 0
760 *in rtl_rc.c if skb is special data or
761 *mgt which need low data rate.
765 *So tcb_desc->hw_rate is just used for
766 *special data and mgt frames
768 if (info->control.rates[0].idx == 0 ||
769 ieee80211_is_nullfunc(fc)) {
770 tcb_desc->use_driver_rate = true;
771 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
773 tcb_desc->disable_ratefallback = 1;
776 *because hw will nerver use hw_rate
777 *when tcb_desc->use_driver_rate = false
778 *so we never set highest N rate here,
779 *and N rate will all be controled by FW
780 *when tcb_desc->use_driver_rate = false
782 if (sta && (sta->ht_cap.ht_supported)) {
783 tcb_desc->hw_rate = _rtl_get_highest_n_rate(hw, sta);
785 if(rtlmac->mode == WIRELESS_MODE_B) {
787 rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
790 rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
795 if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
796 tcb_desc->b_multicast = 1;
797 else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
798 tcb_desc->b_broadcast = 1;
800 _rtl_txrate_selectmode(hw, sta, tcb_desc);
801 _rtl_query_bandwidth_mode(hw, sta, tcb_desc);
802 _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
803 _rtl_query_shortgi(hw, sta, tcb_desc, info);
804 _rtl_query_protection_mode(hw, tcb_desc, info);
806 tcb_desc->use_driver_rate = true;
807 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
808 tcb_desc->disable_ratefallback = 1;
809 tcb_desc->mac_id = 0;
810 tcb_desc->b_packet_bw = false;
813 //EXPORT_SYMBOL(rtl_get_tcb_desc);
815 bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
817 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
818 struct rtl_priv *rtlpriv = rtl_priv(hw);
819 u16 fc = rtl_get_fc(skb);
821 if (rtlpriv->dm.supp_phymode_switch &&
822 mac->link_state < MAC80211_LINKED &&
823 (ieee80211_is_auth(fc) || ieee80211_is_probe_req(fc))) {
824 if (rtlpriv->cfg->ops->check_switch_to_dmdp)
825 rtlpriv->cfg->ops->check_switch_to_dmdp(hw);
827 if (ieee80211_is_auth(fc)) {
828 RT_TRACE(COMP_SEND, DBG_DMESG, ("MAC80211_LINKING\n"));
831 mac->link_state = MAC80211_LINKING;
833 rtlpriv->phy.b_need_iqk = true;
840 struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw, u8 *sa,
842 bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
844 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
845 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
846 struct rtl_priv *rtlpriv = rtl_priv(hw);
847 u16 fc = rtl_get_fc(skb);
848 u8 *act = (u8 *) (((u8 *) skb->data + MAC80211_3ADDR_LEN));
851 if (!ieee80211_is_action(fc))
860 if (mac->act_scanning)
863 RT_TRACE((COMP_SEND | COMP_RECV), DBG_DMESG,
864 ("%s ACT_ADDBAREQ From :%pM\n",
865 is_tx ? "Tx" : "Rx", hdr->addr2));
866 RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG, ("req \n"),
867 skb->data, skb->len);
869 struct ieee80211_sta *sta = NULL;
870 struct rtl_sta_info *sta_entry = NULL;
871 struct ieee80211_mgmt *mgmt = (void *)skb->data;
872 u16 capab = 0, tid = 0;
873 struct rtl_tid_data *tid_data;
874 struct sk_buff *skb_delba = NULL;
875 struct ieee80211_rx_status rx_status = { 0 };
878 sta = rtl_find_sta(hw, hdr->addr3);
880 RT_TRACE((COMP_SEND | COMP_RECV),
881 DBG_EMERG, ("sta is NULL\n"));
886 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
891 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
892 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
893 tid_data = &sta_entry->tids[tid];
894 if (tid_data->agg.rx_agg_state ==
896 skb_delba = rtl_make_del_ba(hw,
901 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0))
902 rx_status.freq = hw->conf.chandef.chan->center_freq;
903 rx_status.band = hw->conf.chandef.chan->band;
905 rx_status.freq = hw->conf.channel->center_freq;
906 rx_status.band = hw->conf.channel->band;
908 rx_status.flag |= RX_FLAG_DECRYPTED;
909 rx_status.flag |= RX_FLAG_MACTIME_MPDU;
910 rx_status.rate_idx = 0;
911 rx_status.signal = 50 + 10;
912 memcpy(IEEE80211_SKB_RXCB(skb_delba), &rx_status,
914 RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG,
915 ("fake del\n"), skb_delba->data,
917 ieee80211_rx_irqsafe(hw, skb_delba);
924 RT_TRACE((COMP_SEND | COMP_RECV), DBG_DMESG,
925 ("%s ACT_ADDBARSP From :%pM\n",
926 is_tx ? "Tx" : "Rx", hdr->addr2));
929 RT_TRACE((COMP_SEND | COMP_RECV), DBG_DMESG,
930 ("ACT_ADDBADEL From :%pM\n", hdr->addr2));
941 /*should call before software enc*/
942 u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
944 struct rtl_priv *rtlpriv = rtl_priv(hw);
945 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
946 u16 fc = rtl_get_fc(skb);
948 u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
949 const struct iphdr *ip;
951 if (!ieee80211_is_data(fc))
955 ip = (struct iphdr *)((u8 *) skb->data + mac_hdr_len +
956 SNAP_SIZE + PROTOC_TYPE_SIZE);
957 ether_type = *(u16 *) ((u8 *) skb->data + mac_hdr_len + SNAP_SIZE);
958 ether_type = ntohs(ether_type);
960 if (ETH_P_IP == ether_type) {
961 if (IPPROTO_UDP == ip->protocol) {
962 struct udphdr *udp = (struct udphdr *)((u8 *) ip +
964 if (((((u8 *) udp)[1] == 68) &&
965 (((u8 *) udp)[3] == 67)) ||
966 ((((u8 *) udp)[1] == 67) &&
967 (((u8 *) udp)[3] == 68))) {
969 * 68 : UDP BOOTP client
970 * 67 : UDP BOOTP server
972 RT_TRACE((COMP_SEND | COMP_RECV),
973 DBG_DMESG, ("dhcp %s !!\n",
974 (is_tx) ? "Tx" : "Rx"));
977 rtlpriv->ra.is_special_data = true;
979 ppsc->last_delaylps_stamp_jiffies =
986 } else if (ETH_P_ARP == ether_type) {
988 rtlpriv->ra.is_special_data = true;
990 ppsc->last_delaylps_stamp_jiffies = jiffies;
994 } else if (ETH_P_PAE == ether_type) {
995 RT_TRACE((COMP_SEND | COMP_RECV), DBG_DMESG,
996 ("802.1X %s EAPOL pkt!!\n", (is_tx) ? "Tx" : "Rx"));
999 rtlpriv->ra.is_special_data = true;
1001 ppsc->last_delaylps_stamp_jiffies = jiffies;
1005 } else if (0x86DD == ether_type) {
1010 rtlpriv->ra.is_special_data = false;
1014 /*********************************************************
1016 * functions called by core.c
1018 *********************************************************/
1019 int rtl_tx_agg_start(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1020 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1022 struct rtl_priv *rtlpriv = rtl_priv(hw);
1023 struct rtl_tid_data *tid_data;
1024 struct rtl_sta_info *sta_entry = NULL;
1029 if (unlikely(tid >= MAX_TID_COUNT))
1032 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1035 tid_data = &sta_entry->tids[tid];
1037 RT_TRACE(COMP_SEND, DBG_DMESG,
1038 ("on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1039 tid_data->seq_number));
1041 *ssn = tid_data->seq_number;
1042 tid_data->agg.agg_state = RTL_AGG_START;
1044 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1048 int rtl_tx_agg_stop(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1049 struct ieee80211_sta *sta, u16 tid)
1051 struct rtl_priv *rtlpriv = rtl_priv(hw);
1052 struct rtl_tid_data *tid_data;
1053 struct rtl_sta_info *sta_entry = NULL;
1059 RT_TRACE(COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
1063 RT_TRACE(COMP_SEND, DBG_DMESG,
1064 ("on ra = %pM tid = %d\n", sta->addr, tid));
1066 if (unlikely(tid >= MAX_TID_COUNT))
1069 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1070 tid_data = &sta_entry->tids[tid];
1071 sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
1073 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1077 int rtl_rx_agg_start(struct ieee80211_hw *hw,
1078 struct ieee80211_sta *sta, u16 tid)
1080 struct rtl_priv *rtlpriv = rtl_priv(hw);
1081 struct rtl_tid_data *tid_data;
1082 struct rtl_sta_info *sta_entry = NULL;
1087 if (unlikely(tid >= MAX_TID_COUNT))
1090 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1093 tid_data = &sta_entry->tids[tid];
1095 RT_TRACE(COMP_RECV, DBG_DMESG,
1096 ("on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1097 tid_data->seq_number));
1099 tid_data->agg.rx_agg_state = RTL_RX_AGG_START;
1103 int rtl_rx_agg_stop(struct ieee80211_hw *hw,
1104 struct ieee80211_sta *sta, u16 tid)
1106 struct rtl_priv *rtlpriv = rtl_priv(hw);
1107 struct rtl_tid_data *tid_data;
1108 struct rtl_sta_info *sta_entry = NULL;
1114 RT_TRACE(COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
1118 RT_TRACE(COMP_SEND, DBG_DMESG,
1119 ("on ra = %pM tid = %d\n", sta->addr, tid));
1121 if (unlikely(tid >= MAX_TID_COUNT))
1124 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1125 tid_data = &sta_entry->tids[tid];
1126 sta_entry->tids[tid].agg.rx_agg_state = RTL_RX_AGG_STOP;
1130 int rtl_tx_agg_oper(struct ieee80211_hw *hw,
1131 struct ieee80211_sta *sta, u16 tid)
1133 struct rtl_priv *rtlpriv = rtl_priv(hw);
1134 struct rtl_tid_data *tid_data;
1135 struct rtl_sta_info *sta_entry = NULL;
1141 RT_TRACE(COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
1145 RT_TRACE(COMP_SEND, DBG_DMESG,
1146 ("on ra = %pM tid = %d\n", sta->addr, tid));
1148 if (unlikely(tid >= MAX_TID_COUNT))
1151 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1152 tid_data = &sta_entry->tids[tid];
1153 sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
1158 /*********************************************************
1160 * wq & timer callback functions
1162 *********************************************************/
1163 /* this function is used for roaming */
1164 void rtl_beacon_statistic(struct ieee80211_hw *hw, struct sk_buff *skb)
1166 struct rtl_priv *rtlpriv = rtl_priv(hw);
1167 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1169 if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
1172 if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
1175 /* check if this really is a beacon */
1176 if (!ieee80211_is_beacon(hdr->frame_control) &&
1177 !ieee80211_is_probe_resp(hdr->frame_control))
1180 /* min. beacon length + FCS_LEN */
1181 if (skb->len <= 40 + FCS_LEN)
1184 /* and only beacons from the associated BSSID, please */
1185 if (compare_ether_addr(hdr->addr3, rtlpriv->mac80211.bssid))
1188 rtlpriv->link_info.bcn_rx_inperiod ++;
1191 void rtl_watchdog_wq_callback(void *data)
1193 struct rtl_works *rtlworks = container_of_dwork_rtl(data,
1196 struct ieee80211_hw *hw = rtlworks->hw;
1197 struct rtl_priv *rtlpriv = rtl_priv(hw);
1198 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1199 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1200 bool b_busytraffic = false;
1201 bool b_tx_busy_traffic = false;
1202 bool b_rx_busy_traffic = false;
1203 bool b_higher_busytraffic = false;
1204 bool b_higher_busyrxtraffic = false;
1206 u32 rx_cnt_inp4eriod = 0;
1207 u32 tx_cnt_inp4eriod = 0;
1208 u32 aver_rx_cnt_inperiod = 0;
1209 u32 aver_tx_cnt_inperiod = 0;
1210 u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
1211 u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
1212 bool benter_ps = false;
1214 if (is_hal_stop(rtlhal))
1217 /* <1> Determine if action frame is allowed */
1218 if (mac->link_state > MAC80211_NOLINK) {
1219 if (mac->cnt_after_linked < 20)
1220 mac->cnt_after_linked++;
1222 mac->cnt_after_linked = 0;
1225 /* <2> to check if traffic busy, if
1226 * busytraffic we don't change channel */
1227 if (mac->link_state >= MAC80211_LINKED) {
1229 /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
1230 for (idx = 0; idx <= 2; idx++) {
1231 rtlpriv->link_info.num_rx_in4period[idx] =
1232 rtlpriv->link_info.num_rx_in4period[idx + 1];
1233 rtlpriv->link_info.num_tx_in4period[idx] =
1234 rtlpriv->link_info.num_tx_in4period[idx + 1];
1236 rtlpriv->link_info.num_rx_in4period[3] =
1237 rtlpriv->link_info.num_rx_inperiod;
1238 rtlpriv->link_info.num_tx_in4period[3] =
1239 rtlpriv->link_info.num_tx_inperiod;
1240 for (idx = 0; idx <= 3; idx++) {
1242 rtlpriv->link_info.num_rx_in4period[idx];
1244 rtlpriv->link_info.num_tx_in4period[idx];
1246 aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
1247 aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
1249 /* (2) check traffic busy */
1250 if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100) {
1251 b_busytraffic = true;
1252 if (aver_rx_cnt_inperiod > aver_tx_cnt_inperiod)
1253 b_rx_busy_traffic = true;
1255 b_tx_busy_traffic = false;
1258 /* Higher Tx/Rx data. */
1259 if (aver_rx_cnt_inperiod > 4000 ||
1260 aver_tx_cnt_inperiod > 4000) {
1261 b_higher_busytraffic = true;
1263 /* Extremely high Rx data. */
1264 if (aver_rx_cnt_inperiod > 5000)
1265 b_higher_busyrxtraffic = true;
1268 /* check every tid's tx traffic */
1269 for (tid = 0; tid <= 7; tid++) {
1270 for (idx = 0; idx <= 2; idx++)
1271 rtlpriv->link_info.tidtx_in4period[tid][idx] =
1272 rtlpriv->link_info.tidtx_in4period[tid]
1274 rtlpriv->link_info.tidtx_in4period[tid][3] =
1275 rtlpriv->link_info.tidtx_inperiod[tid];
1277 for (idx = 0; idx <= 3; idx++)
1278 tidtx_inp4eriod[tid] +=
1279 rtlpriv->link_info.tidtx_in4period[tid][idx];
1280 aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
1281 if (aver_tidtx_inperiod[tid] > 5000)
1282 rtlpriv->link_info.higher_busytxtraffic[tid] =
1285 rtlpriv->link_info.higher_busytxtraffic[tid] =
1289 if (((rtlpriv->link_info.num_rx_inperiod +
1290 rtlpriv->link_info.num_tx_inperiod) > 8) ||
1291 (rtlpriv->link_info.num_rx_inperiod > 2))
1296 /* LeisurePS only work in infra mode. */
1303 rtlpriv->link_info.num_rx_inperiod = 0;
1304 rtlpriv->link_info.num_tx_inperiod = 0;
1305 for (tid = 0; tid <= 7; tid++)
1306 rtlpriv->link_info.tidtx_inperiod[tid] = 0;
1308 rtlpriv->link_info.b_busytraffic = b_busytraffic;
1309 rtlpriv->link_info.b_rx_busy_traffic = b_rx_busy_traffic;
1310 rtlpriv->link_info.b_tx_busy_traffic = b_tx_busy_traffic;
1311 rtlpriv->link_info.b_higher_busytraffic = b_higher_busytraffic;
1312 rtlpriv->link_info.b_higher_busyrxtraffic = b_higher_busyrxtraffic;
1315 rtlpriv->cfg->ops->dm_watchdog(hw);
1318 if (mac->link_state == MAC80211_LINKED &&
1319 mac->opmode == NL80211_IFTYPE_STATION) {
1320 if ((rtlpriv->link_info.bcn_rx_inperiod +
1321 rtlpriv->link_info.num_rx_inperiod) == 0) {
1322 rtlpriv->link_info.roam_times++;
1323 RT_TRACE(COMP_ERR, DBG_DMESG, ("AP off for %d s\n",
1324 (rtlpriv->link_info.roam_times * 2)));
1326 /* if we can't recv beacon for 10s,
1327 * we should reconnect this AP */
1328 if (rtlpriv->link_info.roam_times >= 5) {
1329 RT_TRACE(COMP_ERR, DBG_EMERG,
1330 ("AP off, try to reconnect now\n"));
1331 rtlpriv->link_info.roam_times = 0;
1332 ieee80211_connection_loss(rtlpriv->mac80211.vif);
1335 rtlpriv->link_info.roam_times = 0;
1338 rtlpriv->link_info.bcn_rx_inperiod = 0;
1341 void rtl_watch_dog_timer_callback(unsigned long data)
1343 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1344 struct rtl_priv *rtlpriv = rtl_priv(hw);
1346 queue_delayed_work(rtlpriv->works.rtl_wq,
1347 &rtlpriv->works.watchdog_wq, 0);
1349 mod_timer(&rtlpriv->works.watchdog_timer,
1350 jiffies + MSECS(RTL_WATCH_DOG_TIME));
1352 void rtl_fwevt_wq_callback(void *data)
1354 struct rtl_works *rtlworks =
1355 container_of_dwork_rtl(data, struct rtl_works, fwevt_wq);
1356 struct ieee80211_hw *hw = rtlworks->hw;
1357 struct rtl_priv *rtlpriv = rtl_priv(hw);
1359 rtlpriv->cfg->ops->c2h_command_handle(hw);
1361 void rtl_easy_concurrent_retrytimer_callback(unsigned long data)
1363 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1364 struct rtl_priv *rtlpriv = rtl_priv(hw);
1365 struct rtl_priv *buddy_priv = rtlpriv->buddy_priv;
1367 if(buddy_priv == NULL)
1370 rtlpriv->cfg->ops->dualmac_easy_concurrent(hw);
1372 /*********************************************************
1374 * frame process functions
1376 *********************************************************/
1377 u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
1379 struct ieee80211_mgmt *mgmt = (void *)data;
1382 pos = (u8 *)mgmt->u.beacon.variable;
1385 if (pos + 2 + pos[1] > end)
1396 /* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
1397 /* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
1398 struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
1399 enum ieee80211_smps_mode smps,
1402 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1403 struct sk_buff *skb;
1404 struct ieee80211_mgmt_compat *action_frame;
1406 /* 27 = header + category + action + smps mode */
1407 skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
1411 skb_reserve(skb, hw->extra_tx_headroom);
1412 action_frame = (void *)skb_put(skb, 27);
1413 memset(action_frame, 0, 27);
1414 memcpy(action_frame->da, da, ETH_ALEN);
1415 memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
1416 memcpy(action_frame->bssid, bssid, ETH_ALEN);
1417 action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1418 IEEE80211_STYPE_ACTION);
1419 action_frame->u.action.category = WLAN_CATEGORY_HT;
1420 action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
1422 case IEEE80211_SMPS_AUTOMATIC:/* 0 */
1423 case IEEE80211_SMPS_NUM_MODES:/* 4 */
1425 case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
1426 action_frame->u.action.u.ht_smps.smps_control =
1427 WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
1429 case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
1430 action_frame->u.action.u.ht_smps.smps_control =
1431 WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
1433 case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
1434 action_frame->u.action.u.ht_smps.smps_control =
1435 WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
1442 int rtl_send_smps_action(struct ieee80211_hw *hw,
1443 struct ieee80211_sta *sta,
1444 enum ieee80211_smps_mode smps)
1446 struct rtl_priv *rtlpriv = rtl_priv(hw);
1447 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1448 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1449 struct sk_buff *skb = NULL;
1450 struct rtl_tcb_desc tcb_desc;
1451 u8 bssid[ETH_ALEN] = {0};
1453 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
1455 if (rtlpriv->mac80211.act_scanning)
1461 if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
1464 if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1467 if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP)
1468 memcpy(bssid, rtlpriv->efuse.dev_addr, ETH_ALEN);
1470 memcpy(bssid, rtlpriv->mac80211.bssid, ETH_ALEN);
1472 skb = rtl_make_smps_action(hw, smps, sta->addr, bssid);
1473 /* this is a type = mgmt * stype = action frame */
1475 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1476 struct rtl_sta_info *sta_entry =
1477 (struct rtl_sta_info *) sta->drv_priv;
1478 sta_entry->mimo_ps = smps;
1479 /* rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0); */
1481 info->control.rates[0].idx = 0;
1482 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0))
1483 info->band = hw->conf.chandef.chan->band;
1485 info->band = hw->conf.channel->band;
1487 /*<delete in kernel start>*/
1488 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0))
1489 info->control.sta = sta;
1490 rtlpriv->intf_ops->adapter_tx(hw, skb, &tcb_desc);
1492 /*<delete in kernel end>*/
1493 rtlpriv->intf_ops->adapter_tx(hw, sta, skb, &tcb_desc);
1494 /*<delete in kernel start>*/
1496 /*<delete in kernel end>*/
1503 //EXPORT_SYMBOL(rtl_send_smps_action);
1505 /* because mac80211 have issues when can receive del ba
1506 * so here we just make a fake del_ba if we receive a ba_req
1507 * but rx_agg was opened to let mac80211 release some ba
1508 * related resources, so please this del_ba for tx */
1509 struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw,
1510 u8 *sa, u8 *bssid, u16 tid)
1512 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1513 struct sk_buff *skb;
1514 struct ieee80211_mgmt *action_frame;
1517 /* 27 = header + category + action + smps mode */
1518 skb = dev_alloc_skb(34 + hw->extra_tx_headroom);
1522 skb_reserve(skb, hw->extra_tx_headroom);
1523 action_frame = (void *)skb_put(skb, 34);
1524 memset(action_frame, 0, 34);
1525 memcpy(action_frame->sa, sa, ETH_ALEN);
1526 memcpy(action_frame->da, rtlefuse->dev_addr, ETH_ALEN);
1527 memcpy(action_frame->bssid, bssid, ETH_ALEN);
1528 action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1529 IEEE80211_STYPE_ACTION);
1530 action_frame->u.action.category = WLAN_CATEGORY_BACK;
1531 action_frame->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1532 params = (u16)(1 << 11); /* bit 11 initiator */
1533 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1535 action_frame->u.action.u.delba.params = cpu_to_le16(params);
1536 action_frame->u.action.u.delba.reason_code =
1537 cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
1542 /*********************************************************
1546 *********************************************************/
1547 static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
1548 struct octet_string vendor_ie)
1550 struct rtl_priv *rtlpriv = rtl_priv(hw);
1551 bool matched = false;
1552 static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
1553 static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
1554 static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
1555 static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
1556 static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
1557 static u8 racap[] = { 0x00, 0x0c, 0x43 };
1558 static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
1559 static u8 marvcap[] = { 0x00, 0x50, 0x43 };
1561 if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
1562 memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
1563 rtlpriv->mac80211.vendor = PEER_ATH;
1565 } else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
1566 memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
1567 memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
1568 rtlpriv->mac80211.vendor = PEER_BROAD;
1570 } else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
1571 rtlpriv->mac80211.vendor = PEER_RAL;
1573 } else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
1574 rtlpriv->mac80211.vendor = PEER_CISCO;
1576 } else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
1577 rtlpriv->mac80211.vendor = PEER_MARV;
1584 bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
1587 struct ieee80211_mgmt *mgmt = (void *)data;
1588 struct octet_string vendor_ie;
1591 pos = (u8 *)mgmt->u.beacon.variable;
1594 if (pos[0] == 221) {
1595 vendor_ie.length = pos[1];
1596 vendor_ie.octet = &pos[2];
1597 if (rtl_chk_vendor_ouisub(hw, vendor_ie))
1601 if (pos + 2 + pos[1] > end)
1609 void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
1611 struct rtl_priv *rtlpriv = rtl_priv(hw);
1612 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1613 struct ieee80211_hdr *hdr = (void *)data;
1614 u32 vendor = PEER_UNKNOWN;
1616 static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
1617 static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
1618 static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
1619 static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
1620 static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
1621 static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
1622 static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
1623 static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
1624 static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
1625 static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
1626 static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
1627 static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
1628 static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
1629 static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
1630 static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
1631 static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
1633 if (mac->opmode != NL80211_IFTYPE_STATION)
1636 if (mac->link_state == MAC80211_NOLINK) {
1637 mac->vendor = PEER_UNKNOWN;
1641 if (mac->cnt_after_linked > 2)
1644 /* check if this really is a beacon */
1645 if (!ieee80211_is_beacon(hdr->frame_control))
1648 /* min. beacon length + FCS_LEN */
1649 if (len <= 40 + FCS_LEN)
1652 /* and only beacons from the associated BSSID, please */
1653 if (compare_ether_addr(hdr->addr3, rtlpriv->mac80211.bssid))
1656 if (rtl_find_221_ie(hw, data, len)) {
1657 vendor = mac->vendor;
1660 if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
1661 (memcmp(mac->bssid, ap5_2, 3) == 0) ||
1662 (memcmp(mac->bssid, ap5_3, 3) == 0) ||
1663 (memcmp(mac->bssid, ap5_4, 3) == 0) ||
1664 (memcmp(mac->bssid, ap5_5, 3) == 0) ||
1665 (memcmp(mac->bssid, ap5_6, 3) == 0) ||
1666 vendor == PEER_ATH) {
1668 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>ath find\n"));
1669 } else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
1670 (memcmp(mac->bssid, ap4_5, 3) == 0) ||
1671 (memcmp(mac->bssid, ap4_1, 3) == 0) ||
1672 (memcmp(mac->bssid, ap4_2, 3) == 0) ||
1673 (memcmp(mac->bssid, ap4_3, 3) == 0) ||
1674 vendor == PEER_RAL) {
1675 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>ral findn\n"));
1677 } else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
1678 vendor == PEER_CISCO) {
1679 vendor = PEER_CISCO;
1680 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>cisco find\n"));
1681 } else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
1682 (memcmp(mac->bssid, ap3_2, 3) == 0) ||
1683 (memcmp(mac->bssid, ap3_3, 3) == 0) ||
1684 vendor == PEER_BROAD) {
1685 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>broad find\n"));
1686 vendor = PEER_BROAD;
1687 } else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
1688 vendor == PEER_MARV) {
1690 RT_TRACE(COMP_MAC80211, DBG_LOUD, ("=>marv find\n"));
1693 mac->vendor = vendor;
1696 /*********************************************************
1700 *********************************************************/
1701 static ssize_t rtl_show_debug_level(struct device *d,
1702 struct device_attribute *attr, char *buf)
1704 struct ieee80211_hw *hw = dev_get_drvdata(d);
1705 struct rtl_priv *rtlpriv = rtl_priv(hw);
1707 return sprintf(buf, "0x%08X\n", rtlpriv->dbg.global_debuglevel);
1710 static ssize_t rtl_store_debug_level(struct device *d,
1711 struct device_attribute *attr,
1712 const char *buf, size_t count)
1714 struct ieee80211_hw *hw = dev_get_drvdata(d);
1715 struct rtl_priv *rtlpriv = rtl_priv(hw);
1719 ret = strict_strtoul(buf, 0, &val);
1721 printk(KERN_DEBUG "%s is not in hex or decimal form.\n", buf);
1723 rtlpriv->dbg.global_debuglevel = val;
1724 printk(KERN_DEBUG "debuglevel:%x\n",
1725 rtlpriv->dbg.global_debuglevel);
1728 return strnlen(buf, count);
1731 static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
1732 rtl_show_debug_level, rtl_store_debug_level);
1734 static struct attribute *rtl_sysfs_entries[] = {
1736 &dev_attr_debug_level.attr,
1742 * "name" is folder name witch will be
1743 * put in device directory like :
1744 * sys/devices/pci0000:00/0000:00:1c.4/
1745 * 0000:06:00.0/rtl_sysfs
1747 struct attribute_group rtl_attribute_group = {
1749 .attrs = rtl_sysfs_entries,
1753 /*********************************************************
1757 *********************************************************/
1758 static inline struct ieee80211_vif *
1759 rtl_get_vif(struct rtl_vif_info *vif_priv)
1761 return container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
1764 /* Protected by ar->mutex or RCU */
1765 struct ieee80211_vif *rtl_get_main_vif(struct ieee80211_hw *hw)
1767 struct rtl_priv *rtlpriv = rtl_priv(hw);
1768 struct rtl_vif_info *cvif;
1770 list_for_each_entry_rcu(cvif, &rtlpriv->vif_priv.vif_list, list) {
1772 return rtl_get_vif(cvif);
1778 static inline bool is_main_vif(struct ieee80211_hw *hw,
1779 struct ieee80211_vif *vif)
1784 ret = (rtl_get_main_vif(hw) == vif);
1789 bool rtl_set_vif_info(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1791 struct rtl_vif_info *vif_info = (void *) vif->drv_priv;
1792 struct rtl_priv *rtlpriv = rtl_priv(hw);
1795 if (rtlpriv->vif_priv.vifs >= MAX_VIRTUAL_MAC) {
1796 RT_TRACE(COMP_ERR, DBG_WARNING,
1797 ("vif number can not bigger than %d, now vifs is:%d\n",
1798 MAX_VIRTUAL_MAC, rtlpriv->vif_priv.vifs));
1803 vif_id = bitmap_find_free_region(&rtlpriv->vif_priv.vif_bitmap,
1804 MAX_VIRTUAL_MAC, 0);
1805 RT_TRACE(COMP_MAC80211, DBG_DMESG,
1806 ("%s vid_id:%d\n", __func__, vif_id));
1813 BUG_ON(rtlpriv->vif_priv.vif[vif_id].id != vif_id);
1814 vif_info->active = true;
1815 vif_info->id = vif_id;
1816 vif_info->enable_beacon = false;
1817 rtlpriv->vif_priv.vifs++;
1818 if (rtlpriv->vif_priv.vifs > 1) {
1819 rtlpriv->psc.b_inactiveps = false;
1820 rtlpriv->psc.b_swctrl_lps = false;
1821 rtlpriv->psc.b_fwctrl_lps = false;
1824 list_add_tail_rcu(&vif_info->list, &rtlpriv->vif_priv.vif_list);
1825 rcu_assign_pointer(rtlpriv->vif_priv.vif[vif_id].vif, vif);
1827 RT_TRACE(COMP_MAC80211, DBG_DMESG, ("vifaddress:%p %p %p\n",
1828 rtlpriv->vif_priv.vif[vif_id].vif, vif, rtl_get_main_vif(hw)));
1838 MODULE_AUTHOR("lizhaoming <chaoming_li@realsil.com.cn>");
1839 MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>");
1840 MODULE_AUTHOR("Larry Finger <Larry.FInger@lwfinger.net>");
1841 MODULE_LICENSE("GPL");
1842 MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
1844 struct rtl_global_var global_var = {};
1846 int rtl_core_module_init(void)
1848 if (rtl_rate_control_register())
1849 printk(KERN_DEBUG "rtl: Unable to register rtl_rc,"
1850 "use default RC !!\n");
1852 /* add proc for debug */
1853 rtl_proc_add_topdir();
1855 /* init some global vars */
1856 INIT_LIST_HEAD(&global_var.glb_priv_list);
1857 spin_lock_init(&global_var.glb_list_lock);
1862 void rtl_core_module_exit(void)
1865 rtl_rate_control_unregister();
1867 /* add proc for debug */
1868 rtl_proc_remove_topdir();
1872 module_init(rtl_core_module_init);
1873 module_exit(rtl_core_module_exit);