• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  * Copyright(c) 2009-2012  Realtek Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  * The full GNU General Public License is included in this distribution in the
19  * file called LICENSE.
20  *
21  * Contact Information:
22  * wlanfae <wlanfae@realtek.com>
23  * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
24  * Hsinchu 300, Taiwan.
25  *
26  * Larry Finger <Larry.Finger@lwfinger.net>
27  *
28  *****************************************************************************/
29 
30 #include "wifi.h"
31 #include "rc.h"
32 #include "base.h"
33 #include "efuse.h"
34 #include "cam.h"
35 #include "ps.h"
36 #include "regd.h"
37 
38 #include <linux/ip.h>
39 #include <linux/module.h>
40 
41 /*
42  *NOTICE!!!: This file will be very big, we should
43  *keep it clear under following roles:
44  *
45  *This file include following parts, 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
48  *for this file:
49  *
50  *1) mac80211 init functions
51  *2) tx information functions
52  *3) functions called by core.c
53  *4) wq & timer callback functions
54  *5) frame process functions
55  *6) IOT functions
56  *7) sysfs functions
57  *8) vif functions
58  *9) ...
59  */
60 
61 /*********************************************************
62  *
63  * mac80211 init functions
64  *
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,},
81 };
82 
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,},
108 };
109 
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,},
123 };
124 
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,},
134 };
135 
136 static const struct ieee80211_supported_band rtl_band_2ghz = {
137 	.band = IEEE80211_BAND_2GHZ,
138 
139 	.channels = rtl_channeltable_2g,
140 	.n_channels = ARRAY_SIZE(rtl_channeltable_2g),
141 
142 	.bitrates = rtl_ratetable_2g,
143 	.n_bitrates = ARRAY_SIZE(rtl_ratetable_2g),
144 
145 	.ht_cap = {0},
146 };
147 
148 static struct ieee80211_supported_band rtl_band_5ghz = {
149 	.band = IEEE80211_BAND_5GHZ,
150 
151 	.channels = rtl_channeltable_5g,
152 	.n_channels = ARRAY_SIZE(rtl_channeltable_5g),
153 
154 	.bitrates = rtl_ratetable_5g,
155 	.n_bitrates = ARRAY_SIZE(rtl_ratetable_5g),
156 
157 	.ht_cap = {0},
158 };
159 
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 */
169 };
170 
rtl_tid_to_ac(u8 tid)171 u8 rtl_tid_to_ac(u8 tid)
172 {
173 	return tid_to_ac[tid];
174 }
175 
_rtl_init_hw_ht_capab(struct ieee80211_hw * hw,struct ieee80211_sta_ht_cap * ht_cap)176 static void _rtl_init_hw_ht_capab(struct ieee80211_hw *hw,
177 				  struct ieee80211_sta_ht_cap *ht_cap)
178 {
179 	struct rtl_priv *rtlpriv = rtl_priv(hw);
180 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
181 
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;
187 
188 	if (rtlpriv->rtlhal.disable_amsdu_8k)
189 		ht_cap->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
190 
191 	/*
192 	 *Maximum length of AMPDU that the STA can receive.
193 	 *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
194 	 */
195 	ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
196 
197 	/*Minimum MPDU start spacing , */
198 	ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
199 
200 	ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
201 
202 	/*hw->wiphy->bands[IEEE80211_BAND_2GHZ]
203 	 *base on ant_num
204 	 *rx_mask: RX mask
205 	 *if rx_ant = 1 rx_mask[0]= 0xff;==>MCS0-MCS7
206 	 *if rx_ant = 2 rx_mask[1]= 0xff;==>MCS8-MCS15
207 	 *if rx_ant >= 3 rx_mask[2]= 0xff;
208 	 *if BW_40 rx_mask[4]= 0x01;
209 	 *highest supported RX rate
210 	 */
211 	if (rtlpriv->dm.supp_phymode_switch) {
212 
213 		RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG,
214 			 "Support phy mode switch\n");
215 
216 		ht_cap->mcs.rx_mask[0] = 0xFF;
217 		ht_cap->mcs.rx_mask[1] = 0xFF;
218 		ht_cap->mcs.rx_mask[4] = 0x01;
219 
220 		ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
221 	} else {
222 		if (get_rf_type(rtlphy) == RF_1T2R ||
223 		    get_rf_type(rtlphy) == RF_2T2R) {
224 			RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
225 				 "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;
229 
230 			ht_cap->mcs.rx_highest =
231 				 cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
232 		} else if (get_rf_type(rtlphy) == RF_1T1R) {
233 			RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "1T1R\n");
234 
235 			ht_cap->mcs.rx_mask[0] = 0xFF;
236 			ht_cap->mcs.rx_mask[1] = 0x00;
237 			ht_cap->mcs.rx_mask[4] = 0x01;
238 
239 			ht_cap->mcs.rx_highest =
240 				 cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
241 		}
242 	}
243 }
244 
_rtl_init_mac80211(struct ieee80211_hw * hw)245 static void _rtl_init_mac80211(struct ieee80211_hw *hw)
246 {
247 	struct rtl_priv *rtlpriv = rtl_priv(hw);
248 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
249 	struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
250 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
251 	struct ieee80211_supported_band *sband;
252 
253 
254 	if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY && rtlhal->bandset ==
255 	    BAND_ON_BOTH) {
256 		/* 1: 2.4 G bands */
257 		/* <1> use  mac->bands as mem for hw->wiphy->bands */
258 		sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
259 
260 		/* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
261 		 * to default value(1T1R) */
262 		memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]), &rtl_band_2ghz,
263 				sizeof(struct ieee80211_supported_band));
264 
265 		/* <3> init ht cap base on ant_num */
266 		_rtl_init_hw_ht_capab(hw, &sband->ht_cap);
267 
268 		/* <4> set mac->sband to wiphy->sband */
269 		hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
270 
271 		/* 2: 5 G bands */
272 		/* <1> use  mac->bands as mem for hw->wiphy->bands */
273 		sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
274 
275 		/* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
276 		 * to default value(1T1R) */
277 		memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]), &rtl_band_5ghz,
278 				sizeof(struct ieee80211_supported_band));
279 
280 		/* <3> init ht cap base on ant_num */
281 		_rtl_init_hw_ht_capab(hw, &sband->ht_cap);
282 
283 		/* <4> set mac->sband to wiphy->sband */
284 		hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
285 	} else {
286 		if (rtlhal->current_bandtype == BAND_ON_2_4G) {
287 			/* <1> use  mac->bands as mem for hw->wiphy->bands */
288 			sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
289 
290 			/* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
291 			 * to default value(1T1R) */
292 			memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]),
293 				 &rtl_band_2ghz,
294 				 sizeof(struct ieee80211_supported_band));
295 
296 			/* <3> init ht cap base on ant_num */
297 			_rtl_init_hw_ht_capab(hw, &sband->ht_cap);
298 
299 			/* <4> set mac->sband to wiphy->sband */
300 			hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
301 		} else if (rtlhal->current_bandtype == BAND_ON_5G) {
302 			/* <1> use  mac->bands as mem for hw->wiphy->bands */
303 			sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
304 
305 			/* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
306 			 * to default value(1T1R) */
307 			memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]),
308 				 &rtl_band_5ghz,
309 				 sizeof(struct ieee80211_supported_band));
310 
311 			/* <3> init ht cap base on ant_num */
312 			_rtl_init_hw_ht_capab(hw, &sband->ht_cap);
313 
314 			/* <4> set mac->sband to wiphy->sband */
315 			hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
316 		} else {
317 			RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG, "Err BAND %d\n",
318 				 rtlhal->current_bandtype);
319 		}
320 	}
321 	/* <5> set hw caps */
322 	hw->flags = IEEE80211_HW_SIGNAL_DBM |
323 	    IEEE80211_HW_RX_INCLUDES_FCS |
324 	    IEEE80211_HW_AMPDU_AGGREGATION |
325 	    IEEE80211_HW_CONNECTION_MONITOR |
326 	    /* IEEE80211_HW_SUPPORTS_CQM_RSSI | */
327 	    IEEE80211_HW_CONNECTION_MONITOR |
328 	    IEEE80211_HW_MFP_CAPABLE |
329 	    IEEE80211_HW_REPORTS_TX_ACK_STATUS | 0;
330 
331 	/* swlps or hwlps has been set in diff chip in init_sw_vars */
332 	if (rtlpriv->psc.swctrl_lps)
333 		hw->flags |= IEEE80211_HW_SUPPORTS_PS |
334 			IEEE80211_HW_PS_NULLFUNC_STACK |
335 			/* IEEE80211_HW_SUPPORTS_DYNAMIC_PS | */
336 			0;
337 
338 	hw->wiphy->interface_modes =
339 	    BIT(NL80211_IFTYPE_AP) |
340 	    BIT(NL80211_IFTYPE_STATION) |
341 	    BIT(NL80211_IFTYPE_ADHOC) |
342 	    BIT(NL80211_IFTYPE_MESH_POINT) |
343 	    BIT(NL80211_IFTYPE_P2P_CLIENT) |
344 	    BIT(NL80211_IFTYPE_P2P_GO);
345 
346 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
347 	hw->wiphy->rts_threshold = 2347;
348 
349 	hw->queues = AC_MAX;
350 	hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;
351 
352 	/* TODO: Correct this value for our hw */
353 	/* TODO: define these hard code value */
354 	hw->channel_change_time = 100;
355 	hw->max_listen_interval = 10;
356 	hw->max_rate_tries = 4;
357 	/* hw->max_rates = 1; */
358 	hw->sta_data_size = sizeof(struct rtl_sta_info);
359 
360 	/* <6> mac address */
361 	if (is_valid_ether_addr(rtlefuse->dev_addr)) {
362 		SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
363 	} else {
364 		u8 rtlmac1[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
365 		get_random_bytes((rtlmac1 + (ETH_ALEN - 1)), 1);
366 		SET_IEEE80211_PERM_ADDR(hw, rtlmac1);
367 	}
368 
369 }
370 
_rtl_init_deferred_work(struct ieee80211_hw * hw)371 static void _rtl_init_deferred_work(struct ieee80211_hw *hw)
372 {
373 	struct rtl_priv *rtlpriv = rtl_priv(hw);
374 
375 	/* <1> timer */
376 	setup_timer(&rtlpriv->works.watchdog_timer,
377 		    rtl_watch_dog_timer_callback, (unsigned long)hw);
378 	setup_timer(&rtlpriv->works.dualmac_easyconcurrent_retrytimer,
379 		    rtl_easy_concurrent_retrytimer_callback, (unsigned long)hw);
380 
381 	/* <2> work queue */
382 	rtlpriv->works.hw = hw;
383 	rtlpriv->works.rtl_wq = alloc_workqueue(rtlpriv->cfg->name, 0, 0);
384 	INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
385 			  (void *)rtl_watchdog_wq_callback);
386 	INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
387 			  (void *)rtl_ips_nic_off_wq_callback);
388 	INIT_DELAYED_WORK(&rtlpriv->works.ps_work,
389 			  (void *)rtl_swlps_wq_callback);
390 	INIT_DELAYED_WORK(&rtlpriv->works.ps_rfon_wq,
391 			  (void *)rtl_swlps_rfon_wq_callback);
392 	INIT_DELAYED_WORK(&rtlpriv->works.fwevt_wq,
393 			  (void *)rtl_fwevt_wq_callback);
394 
395 }
396 
rtl_deinit_deferred_work(struct ieee80211_hw * hw)397 void rtl_deinit_deferred_work(struct ieee80211_hw *hw)
398 {
399 	struct rtl_priv *rtlpriv = rtl_priv(hw);
400 
401 	del_timer_sync(&rtlpriv->works.watchdog_timer);
402 
403 	cancel_delayed_work(&rtlpriv->works.watchdog_wq);
404 	cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
405 	cancel_delayed_work(&rtlpriv->works.ps_work);
406 	cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
407 	cancel_delayed_work(&rtlpriv->works.fwevt_wq);
408 }
409 
rtl_init_rfkill(struct ieee80211_hw * hw)410 void rtl_init_rfkill(struct ieee80211_hw *hw)
411 {
412 	struct rtl_priv *rtlpriv = rtl_priv(hw);
413 
414 	bool radio_state;
415 	bool blocked;
416 	u8 valid = 0;
417 
418 	/*set init state to on */
419 	rtlpriv->rfkill.rfkill_state = true;
420 	wiphy_rfkill_set_hw_state(hw->wiphy, 0);
421 
422 	radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
423 
424 	if (valid) {
425 		pr_info("wireless switch is %s\n",
426 			rtlpriv->rfkill.rfkill_state ? "on" : "off");
427 
428 		rtlpriv->rfkill.rfkill_state = radio_state;
429 
430 		blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
431 		wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
432 	}
433 
434 	wiphy_rfkill_start_polling(hw->wiphy);
435 }
436 EXPORT_SYMBOL(rtl_init_rfkill);
437 
rtl_deinit_rfkill(struct ieee80211_hw * hw)438 void rtl_deinit_rfkill(struct ieee80211_hw *hw)
439 {
440 	wiphy_rfkill_stop_polling(hw->wiphy);
441 }
442 
rtl_init_core(struct ieee80211_hw * hw)443 int rtl_init_core(struct ieee80211_hw *hw)
444 {
445 	struct rtl_priv *rtlpriv = rtl_priv(hw);
446 	struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
447 
448 	/* <1> init mac80211 */
449 	_rtl_init_mac80211(hw);
450 	rtlmac->hw = hw;
451 
452 	/* <2> rate control register */
453 	hw->rate_control_algorithm = "rtl_rc";
454 
455 	/*
456 	 * <3> init CRDA must come after init
457 	 * mac80211 hw  in _rtl_init_mac80211.
458 	 */
459 	if (rtl_regd_init(hw, rtl_reg_notifier)) {
460 		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "REGD init failed\n");
461 		return 1;
462 	}
463 
464 	/* <4> locks */
465 	mutex_init(&rtlpriv->locks.conf_mutex);
466 	mutex_init(&rtlpriv->locks.ps_mutex);
467 	spin_lock_init(&rtlpriv->locks.ips_lock);
468 	spin_lock_init(&rtlpriv->locks.irq_th_lock);
469 	spin_lock_init(&rtlpriv->locks.irq_pci_lock);
470 	spin_lock_init(&rtlpriv->locks.tx_lock);
471 	spin_lock_init(&rtlpriv->locks.h2c_lock);
472 	spin_lock_init(&rtlpriv->locks.rf_ps_lock);
473 	spin_lock_init(&rtlpriv->locks.rf_lock);
474 	spin_lock_init(&rtlpriv->locks.waitq_lock);
475 	spin_lock_init(&rtlpriv->locks.entry_list_lock);
476 	spin_lock_init(&rtlpriv->locks.fw_ps_lock);
477 	spin_lock_init(&rtlpriv->locks.cck_and_rw_pagea_lock);
478 	spin_lock_init(&rtlpriv->locks.check_sendpkt_lock);
479 	spin_lock_init(&rtlpriv->locks.fw_ps_lock);
480 	spin_lock_init(&rtlpriv->locks.lps_lock);
481 
482 	/* <5> init list */
483 	INIT_LIST_HEAD(&rtlpriv->entry_list);
484 
485 	rtlmac->link_state = MAC80211_NOLINK;
486 
487 	/* <6> init deferred work */
488 	_rtl_init_deferred_work(hw);
489 
490 	return 0;
491 }
492 
rtl_deinit_core(struct ieee80211_hw * hw)493 void rtl_deinit_core(struct ieee80211_hw *hw)
494 {
495 }
496 
rtl_init_rx_config(struct ieee80211_hw * hw)497 void rtl_init_rx_config(struct ieee80211_hw *hw)
498 {
499 	struct rtl_priv *rtlpriv = rtl_priv(hw);
500 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
501 
502 	rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
503 }
504 
505 /*********************************************************
506  *
507  * tx information functions
508  *
509  *********************************************************/
_rtl_qurey_shortpreamble_mode(struct ieee80211_hw * hw,struct rtl_tcb_desc * tcb_desc,struct ieee80211_tx_info * info)510 static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
511 					  struct rtl_tcb_desc *tcb_desc,
512 					  struct ieee80211_tx_info *info)
513 {
514 	struct rtl_priv *rtlpriv = rtl_priv(hw);
515 	u8 rate_flag = info->control.rates[0].flags;
516 
517 	tcb_desc->use_shortpreamble = false;
518 
519 	/* 1M can only use Long Preamble. 11B spec */
520 	if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
521 		return;
522 	else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
523 		tcb_desc->use_shortpreamble = true;
524 
525 	return;
526 }
527 
_rtl_query_shortgi(struct ieee80211_hw * hw,struct ieee80211_sta * sta,struct rtl_tcb_desc * tcb_desc,struct ieee80211_tx_info * info)528 static void _rtl_query_shortgi(struct ieee80211_hw *hw,
529 			       struct ieee80211_sta *sta,
530 			       struct rtl_tcb_desc *tcb_desc,
531 			       struct ieee80211_tx_info *info)
532 {
533 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
534 	u8 rate_flag = info->control.rates[0].flags;
535 	u8 sgi_40 = 0, sgi_20 = 0, bw_40 = 0;
536 	tcb_desc->use_shortgi = false;
537 
538 	if (sta == NULL)
539 		return;
540 
541 	sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
542 	sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
543 
544 	if (!(sta->ht_cap.ht_supported))
545 		return;
546 
547 	if (!sgi_40 && !sgi_20)
548 		return;
549 
550 	if (mac->opmode == NL80211_IFTYPE_STATION)
551 		bw_40 = mac->bw_40;
552 	else if (mac->opmode == NL80211_IFTYPE_AP ||
553 		 mac->opmode == NL80211_IFTYPE_ADHOC ||
554 		 mac->opmode == NL80211_IFTYPE_MESH_POINT)
555 		bw_40 = sta->bandwidth >= IEEE80211_STA_RX_BW_40;
556 
557 	if (bw_40 && sgi_40)
558 		tcb_desc->use_shortgi = true;
559 	else if ((bw_40 == false) && sgi_20)
560 		tcb_desc->use_shortgi = true;
561 
562 	if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
563 		tcb_desc->use_shortgi = false;
564 }
565 
_rtl_query_protection_mode(struct ieee80211_hw * hw,struct rtl_tcb_desc * tcb_desc,struct ieee80211_tx_info * info)566 static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
567 				       struct rtl_tcb_desc *tcb_desc,
568 				       struct ieee80211_tx_info *info)
569 {
570 	struct rtl_priv *rtlpriv = rtl_priv(hw);
571 	u8 rate_flag = info->control.rates[0].flags;
572 
573 	/* Common Settings */
574 	tcb_desc->rts_stbc = false;
575 	tcb_desc->cts_enable = false;
576 	tcb_desc->rts_sc = 0;
577 	tcb_desc->rts_bw = false;
578 	tcb_desc->rts_use_shortpreamble = false;
579 	tcb_desc->rts_use_shortgi = false;
580 
581 	if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
582 		/* Use CTS-to-SELF in protection mode. */
583 		tcb_desc->rts_enable = true;
584 		tcb_desc->cts_enable = true;
585 		tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
586 	} else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
587 		/* Use RTS-CTS in protection mode. */
588 		tcb_desc->rts_enable = true;
589 		tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
590 	}
591 }
592 
_rtl_txrate_selectmode(struct ieee80211_hw * hw,struct ieee80211_sta * sta,struct rtl_tcb_desc * tcb_desc)593 static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
594 				   struct ieee80211_sta *sta,
595 				   struct rtl_tcb_desc *tcb_desc)
596 {
597 	struct rtl_priv *rtlpriv = rtl_priv(hw);
598 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
599 	struct rtl_sta_info *sta_entry = NULL;
600 	u8 ratr_index = 7;
601 
602 	if (sta) {
603 		sta_entry = (struct rtl_sta_info *) sta->drv_priv;
604 		ratr_index = sta_entry->ratr_index;
605 	}
606 	if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
607 		if (mac->opmode == NL80211_IFTYPE_STATION) {
608 			tcb_desc->ratr_index = 0;
609 		} else if (mac->opmode == NL80211_IFTYPE_ADHOC ||
610 			   mac->opmode == NL80211_IFTYPE_MESH_POINT) {
611 			if (tcb_desc->multicast || tcb_desc->broadcast) {
612 				tcb_desc->hw_rate =
613 				    rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
614 				tcb_desc->use_driver_rate = 1;
615 				tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
616 			} else {
617 				tcb_desc->ratr_index = ratr_index;
618 			}
619 		} else if (mac->opmode == NL80211_IFTYPE_AP) {
620 			tcb_desc->ratr_index = ratr_index;
621 		}
622 	}
623 
624 	if (rtlpriv->dm.useramask) {
625 		tcb_desc->ratr_index = ratr_index;
626 		/* TODO we will differentiate adhoc and station future  */
627 		if (mac->opmode == NL80211_IFTYPE_STATION ||
628 		    mac->opmode == NL80211_IFTYPE_MESH_POINT) {
629 			tcb_desc->mac_id = 0;
630 
631 			if (mac->mode == WIRELESS_MODE_N_24G)
632 				tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
633 			else if (mac->mode == WIRELESS_MODE_N_5G)
634 				tcb_desc->ratr_index = RATR_INX_WIRELESS_NG;
635 			else if (mac->mode & WIRELESS_MODE_G)
636 				tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
637 			else if (mac->mode & WIRELESS_MODE_B)
638 				tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
639 			else if (mac->mode & WIRELESS_MODE_A)
640 				tcb_desc->ratr_index = RATR_INX_WIRELESS_G;
641 		} else if (mac->opmode == NL80211_IFTYPE_AP ||
642 			   mac->opmode == NL80211_IFTYPE_ADHOC) {
643 			if (NULL != sta) {
644 				if (sta->aid > 0)
645 					tcb_desc->mac_id = sta->aid + 1;
646 				else
647 					tcb_desc->mac_id = 1;
648 			} else {
649 				tcb_desc->mac_id = 0;
650 			}
651 		}
652 	}
653 }
654 
_rtl_query_bandwidth_mode(struct ieee80211_hw * hw,struct ieee80211_sta * sta,struct rtl_tcb_desc * tcb_desc)655 static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
656 				      struct ieee80211_sta *sta,
657 				      struct rtl_tcb_desc *tcb_desc)
658 {
659 	struct rtl_priv *rtlpriv = rtl_priv(hw);
660 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
661 
662 	tcb_desc->packet_bw = false;
663 	if (!sta)
664 		return;
665 	if (mac->opmode == NL80211_IFTYPE_AP ||
666 	    mac->opmode == NL80211_IFTYPE_ADHOC ||
667 	    mac->opmode == NL80211_IFTYPE_MESH_POINT) {
668 		if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
669 			return;
670 	} else if (mac->opmode == NL80211_IFTYPE_STATION) {
671 		if (!mac->bw_40 || !(sta->ht_cap.ht_supported))
672 			return;
673 	}
674 	if (tcb_desc->multicast || tcb_desc->broadcast)
675 		return;
676 
677 	/*use legency rate, shall use 20MHz */
678 	if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
679 		return;
680 
681 	tcb_desc->packet_bw = true;
682 }
683 
_rtl_get_highest_n_rate(struct ieee80211_hw * hw)684 static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw)
685 {
686 	struct rtl_priv *rtlpriv = rtl_priv(hw);
687 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
688 	u8 hw_rate;
689 
690 	if (get_rf_type(rtlphy) == RF_2T2R)
691 		hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
692 	else
693 		hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
694 
695 	return hw_rate;
696 }
697 
698 /* mac80211's rate_idx is like this:
699  *
700  * 2.4G band:rx_status->band == IEEE80211_BAND_2GHZ
701  *
702  * B/G rate:
703  * (rx_status->flag & RX_FLAG_HT) = 0,
704  * DESC92_RATE1M-->DESC92_RATE54M ==> idx is 0-->11,
705  *
706  * N rate:
707  * (rx_status->flag & RX_FLAG_HT) = 1,
708  * DESC92_RATEMCS0-->DESC92_RATEMCS15 ==> idx is 0-->15
709  *
710  * 5G band:rx_status->band == IEEE80211_BAND_5GHZ
711  * A rate:
712  * (rx_status->flag & RX_FLAG_HT) = 0,
713  * DESC92_RATE6M-->DESC92_RATE54M ==> idx is 0-->7,
714  *
715  * N rate:
716  * (rx_status->flag & RX_FLAG_HT) = 1,
717  * DESC92_RATEMCS0-->DESC92_RATEMCS15 ==> idx is 0-->15
718  */
rtlwifi_rate_mapping(struct ieee80211_hw * hw,bool isht,u8 desc_rate,bool first_ampdu)719 int rtlwifi_rate_mapping(struct ieee80211_hw *hw,
720 			 bool isht, u8 desc_rate, bool first_ampdu)
721 {
722 	int rate_idx;
723 
724 	if (false == isht) {
725 		if (IEEE80211_BAND_2GHZ == hw->conf.chandef.chan->band) {
726 			switch (desc_rate) {
727 			case DESC92_RATE1M:
728 				rate_idx = 0;
729 				break;
730 			case DESC92_RATE2M:
731 				rate_idx = 1;
732 				break;
733 			case DESC92_RATE5_5M:
734 				rate_idx = 2;
735 				break;
736 			case DESC92_RATE11M:
737 				rate_idx = 3;
738 				break;
739 			case DESC92_RATE6M:
740 				rate_idx = 4;
741 				break;
742 			case DESC92_RATE9M:
743 				rate_idx = 5;
744 				break;
745 			case DESC92_RATE12M:
746 				rate_idx = 6;
747 				break;
748 			case DESC92_RATE18M:
749 				rate_idx = 7;
750 				break;
751 			case DESC92_RATE24M:
752 				rate_idx = 8;
753 				break;
754 			case DESC92_RATE36M:
755 				rate_idx = 9;
756 				break;
757 			case DESC92_RATE48M:
758 				rate_idx = 10;
759 				break;
760 			case DESC92_RATE54M:
761 				rate_idx = 11;
762 				break;
763 			default:
764 				rate_idx = 0;
765 				break;
766 			}
767 		} else {
768 			switch (desc_rate) {
769 			case DESC92_RATE6M:
770 				rate_idx = 0;
771 				break;
772 			case DESC92_RATE9M:
773 				rate_idx = 1;
774 				break;
775 			case DESC92_RATE12M:
776 				rate_idx = 2;
777 				break;
778 			case DESC92_RATE18M:
779 				rate_idx = 3;
780 				break;
781 			case DESC92_RATE24M:
782 				rate_idx = 4;
783 				break;
784 			case DESC92_RATE36M:
785 				rate_idx = 5;
786 				break;
787 			case DESC92_RATE48M:
788 				rate_idx = 6;
789 				break;
790 			case DESC92_RATE54M:
791 				rate_idx = 7;
792 				break;
793 			default:
794 				rate_idx = 0;
795 				break;
796 			}
797 		}
798 
799 	} else {
800 
801 		switch (desc_rate) {
802 		case DESC92_RATEMCS0:
803 			rate_idx = 0;
804 			break;
805 		case DESC92_RATEMCS1:
806 			rate_idx = 1;
807 			break;
808 		case DESC92_RATEMCS2:
809 			rate_idx = 2;
810 			break;
811 		case DESC92_RATEMCS3:
812 			rate_idx = 3;
813 			break;
814 		case DESC92_RATEMCS4:
815 			rate_idx = 4;
816 			break;
817 		case DESC92_RATEMCS5:
818 			rate_idx = 5;
819 			break;
820 		case DESC92_RATEMCS6:
821 			rate_idx = 6;
822 			break;
823 		case DESC92_RATEMCS7:
824 			rate_idx = 7;
825 			break;
826 		case DESC92_RATEMCS8:
827 			rate_idx = 8;
828 			break;
829 		case DESC92_RATEMCS9:
830 			rate_idx = 9;
831 			break;
832 		case DESC92_RATEMCS10:
833 			rate_idx = 10;
834 			break;
835 		case DESC92_RATEMCS11:
836 			rate_idx = 11;
837 			break;
838 		case DESC92_RATEMCS12:
839 			rate_idx = 12;
840 			break;
841 		case DESC92_RATEMCS13:
842 			rate_idx = 13;
843 			break;
844 		case DESC92_RATEMCS14:
845 			rate_idx = 14;
846 			break;
847 		case DESC92_RATEMCS15:
848 			rate_idx = 15;
849 			break;
850 		default:
851 			rate_idx = 0;
852 			break;
853 		}
854 	}
855 	return rate_idx;
856 }
857 EXPORT_SYMBOL(rtlwifi_rate_mapping);
858 
rtl_tx_mgmt_proc(struct ieee80211_hw * hw,struct sk_buff * skb)859 bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
860 {
861 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
862 	struct rtl_priv *rtlpriv = rtl_priv(hw);
863 	__le16 fc = rtl_get_fc(skb);
864 
865 	if (rtlpriv->dm.supp_phymode_switch &&
866 	    mac->link_state < MAC80211_LINKED &&
867 	    (ieee80211_is_auth(fc) || ieee80211_is_probe_req(fc))) {
868 		if (rtlpriv->cfg->ops->chk_switch_dmdp)
869 			rtlpriv->cfg->ops->chk_switch_dmdp(hw);
870 	}
871 	if (ieee80211_is_auth(fc)) {
872 		RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
873 		rtl_ips_nic_on(hw);
874 
875 		mac->link_state = MAC80211_LINKING;
876 		/* Dual mac */
877 		rtlpriv->phy.need_iqk = true;
878 	}
879 
880 	return true;
881 }
882 
rtl_get_tcb_desc(struct ieee80211_hw * hw,struct ieee80211_tx_info * info,struct ieee80211_sta * sta,struct sk_buff * skb,struct rtl_tcb_desc * tcb_desc)883 void rtl_get_tcb_desc(struct ieee80211_hw *hw,
884 		      struct ieee80211_tx_info *info,
885 		      struct ieee80211_sta *sta,
886 		      struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
887 {
888 	struct rtl_priv *rtlpriv = rtl_priv(hw);
889 	struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
890 	struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
891 	struct ieee80211_rate *txrate;
892 	__le16 fc = hdr->frame_control;
893 
894 	txrate = ieee80211_get_tx_rate(hw, info);
895 	if (txrate)
896 		tcb_desc->hw_rate = txrate->hw_value;
897 	else
898 		tcb_desc->hw_rate = 0;
899 
900 	if (ieee80211_is_data(fc)) {
901 		/*
902 		 *we set data rate INX 0
903 		 *in rtl_rc.c   if skb is special data or
904 		 *mgt which need low data rate.
905 		 */
906 
907 		/*
908 		 *So tcb_desc->hw_rate is just used for
909 		 *special data and mgt frames
910 		 */
911 		if (info->control.rates[0].idx == 0 ||
912 				ieee80211_is_nullfunc(fc)) {
913 			tcb_desc->use_driver_rate = true;
914 			tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
915 
916 			tcb_desc->disable_ratefallback = 1;
917 		} else {
918 			/*
919 			 *because hw will nerver use hw_rate
920 			 *when tcb_desc->use_driver_rate = false
921 			 *so we never set highest N rate here,
922 			 *and N rate will all be controlled by FW
923 			 *when tcb_desc->use_driver_rate = false
924 			 */
925 			if (sta && (sta->ht_cap.ht_supported)) {
926 				tcb_desc->hw_rate = _rtl_get_highest_n_rate(hw);
927 			} else {
928 				if (rtlmac->mode == WIRELESS_MODE_B) {
929 					tcb_desc->hw_rate =
930 					   rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
931 				} else {
932 					tcb_desc->hw_rate =
933 					   rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
934 				}
935 			}
936 		}
937 
938 		if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
939 			tcb_desc->multicast = 1;
940 		else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
941 			tcb_desc->broadcast = 1;
942 
943 		_rtl_txrate_selectmode(hw, sta, tcb_desc);
944 		_rtl_query_bandwidth_mode(hw, sta, tcb_desc);
945 		_rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
946 		_rtl_query_shortgi(hw, sta, tcb_desc, info);
947 		_rtl_query_protection_mode(hw, tcb_desc, info);
948 	} else {
949 		tcb_desc->use_driver_rate = true;
950 		tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
951 		tcb_desc->disable_ratefallback = 1;
952 		tcb_desc->mac_id = 0;
953 		tcb_desc->packet_bw = false;
954 	}
955 }
956 EXPORT_SYMBOL(rtl_get_tcb_desc);
957 
addbareq_rx(struct ieee80211_hw * hw,struct sk_buff * skb)958 static bool addbareq_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
959 {
960 	struct rtl_priv *rtlpriv = rtl_priv(hw);
961 	struct ieee80211_sta *sta = NULL;
962 	struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
963 	struct rtl_sta_info *sta_entry = NULL;
964 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
965 	u16 capab = 0, tid = 0;
966 	struct rtl_tid_data *tid_data;
967 	struct sk_buff *skb_delba = NULL;
968 	struct ieee80211_rx_status rx_status = { 0 };
969 
970 	rcu_read_lock();
971 	sta = rtl_find_sta(hw, hdr->addr3);
972 	if (sta == NULL) {
973 		RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_EMERG,
974 			 "sta is NULL\n");
975 		rcu_read_unlock();
976 		return true;
977 	}
978 
979 	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
980 	if (!sta_entry) {
981 		rcu_read_unlock();
982 		return true;
983 	}
984 	capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
985 	tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
986 	tid_data = &sta_entry->tids[tid];
987 	if (tid_data->agg.rx_agg_state == RTL_RX_AGG_START) {
988 		skb_delba = rtl_make_del_ba(hw, hdr->addr2, hdr->addr3, tid);
989 		if (skb_delba) {
990 			rx_status.freq = hw->conf.chandef.chan->center_freq;
991 			rx_status.band = hw->conf.chandef.chan->band;
992 			rx_status.flag |= RX_FLAG_DECRYPTED;
993 			rx_status.flag |= RX_FLAG_MACTIME_END;
994 			rx_status.rate_idx = 0;
995 			rx_status.signal = 50 + 10;
996 			memcpy(IEEE80211_SKB_RXCB(skb_delba), &rx_status,
997 			       sizeof(rx_status));
998 			RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG,
999 				      "fake del\n", skb_delba->data,
1000 				      skb_delba->len);
1001 			ieee80211_rx_irqsafe(hw, skb_delba);
1002 		}
1003 	}
1004 	rcu_read_unlock();
1005 	return false;
1006 }
1007 
rtl_action_proc(struct ieee80211_hw * hw,struct sk_buff * skb,u8 is_tx)1008 bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
1009 {
1010 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1011 	struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
1012 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1013 	__le16 fc = hdr->frame_control;
1014 	u8 *act = (u8 *)skb->data + MAC80211_3ADDR_LEN;
1015 	u8 category;
1016 
1017 	if (!ieee80211_is_action(fc))
1018 		return true;
1019 
1020 	category = *act;
1021 	act++;
1022 	switch (category) {
1023 	case ACT_CAT_BA:
1024 		switch (*act) {
1025 		case ACT_ADDBAREQ:
1026 			if (mac->act_scanning)
1027 				return false;
1028 
1029 			RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1030 				 "%s ACT_ADDBAREQ From :%pM\n",
1031 				 is_tx ? "Tx" : "Rx", hdr->addr2);
1032 			RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG, "req\n",
1033 				      skb->data, skb->len);
1034 			if (!is_tx)
1035 				if (addbareq_rx(hw, skb))
1036 					return true;
1037 			break;
1038 		case ACT_ADDBARSP:
1039 			RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1040 				 "%s ACT_ADDBARSP From :%pM\n",
1041 				 is_tx ? "Tx" : "Rx", hdr->addr2);
1042 			break;
1043 		case ACT_DELBA:
1044 			RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1045 				 "ACT_ADDBADEL From :%pM\n", hdr->addr2);
1046 			break;
1047 		}
1048 		break;
1049 	default:
1050 		break;
1051 	}
1052 
1053 	return true;
1054 }
1055 
1056 /*should call before software enc*/
rtl_is_special_data(struct ieee80211_hw * hw,struct sk_buff * skb,u8 is_tx)1057 u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
1058 {
1059 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1060 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1061 	__le16 fc = rtl_get_fc(skb);
1062 	u16 ether_type;
1063 	u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
1064 	const struct iphdr *ip;
1065 
1066 	if (!ieee80211_is_data(fc))
1067 		return false;
1068 
1069 
1070 	ip = (struct iphdr *)((u8 *) skb->data + mac_hdr_len +
1071 			      SNAP_SIZE + PROTOC_TYPE_SIZE);
1072 	ether_type = *(u16 *) ((u8 *) skb->data + mac_hdr_len + SNAP_SIZE);
1073 	/*	ether_type = ntohs(ether_type); */
1074 
1075 	if (ETH_P_IP == ether_type) {
1076 		if (IPPROTO_UDP == ip->protocol) {
1077 			struct udphdr *udp = (struct udphdr *)((u8 *) ip +
1078 							       (ip->ihl << 2));
1079 			if (((((u8 *) udp)[1] == 68) &&
1080 			     (((u8 *) udp)[3] == 67)) ||
1081 			    ((((u8 *) udp)[1] == 67) &&
1082 			     (((u8 *) udp)[3] == 68))) {
1083 				/*
1084 				 * 68 : UDP BOOTP client
1085 				 * 67 : UDP BOOTP server
1086 				 */
1087 				RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV),
1088 					 DBG_DMESG, "dhcp %s !!\n",
1089 					 is_tx ? "Tx" : "Rx");
1090 
1091 				if (is_tx) {
1092 					rtlpriv->enter_ps = false;
1093 					schedule_work(&rtlpriv->
1094 						      works.lps_change_work);
1095 					ppsc->last_delaylps_stamp_jiffies =
1096 					    jiffies;
1097 				}
1098 
1099 				return true;
1100 			}
1101 		}
1102 	} else if (ETH_P_ARP == ether_type) {
1103 		if (is_tx) {
1104 			rtlpriv->enter_ps = false;
1105 			schedule_work(&rtlpriv->works.lps_change_work);
1106 			ppsc->last_delaylps_stamp_jiffies = jiffies;
1107 		}
1108 
1109 		return true;
1110 	} else if (ETH_P_PAE == ether_type) {
1111 		RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1112 			 "802.1X %s EAPOL pkt!!\n", is_tx ? "Tx" : "Rx");
1113 
1114 		if (is_tx) {
1115 			rtlpriv->enter_ps = false;
1116 			schedule_work(&rtlpriv->works.lps_change_work);
1117 			ppsc->last_delaylps_stamp_jiffies = jiffies;
1118 		}
1119 
1120 		return true;
1121 	} else if (ETH_P_IPV6 == ether_type) {
1122 		/* IPv6 */
1123 		return true;
1124 	}
1125 
1126 	return false;
1127 }
1128 
1129 /*********************************************************
1130  *
1131  * functions called by core.c
1132  *
1133  *********************************************************/
rtl_tx_agg_start(struct ieee80211_hw * hw,struct ieee80211_sta * sta,u16 tid,u16 * ssn)1134 int rtl_tx_agg_start(struct ieee80211_hw *hw,
1135 		struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1136 {
1137 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1138 	struct rtl_tid_data *tid_data;
1139 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1140 	struct rtl_sta_info *sta_entry = NULL;
1141 
1142 	if (sta == NULL)
1143 		return -EINVAL;
1144 
1145 	if (unlikely(tid >= MAX_TID_COUNT))
1146 		return -EINVAL;
1147 
1148 	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1149 	if (!sta_entry)
1150 		return -ENXIO;
1151 	tid_data = &sta_entry->tids[tid];
1152 
1153 	RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d seq:%d\n",
1154 		 sta->addr, tid, tid_data->seq_number);
1155 
1156 	*ssn = tid_data->seq_number;
1157 	tid_data->agg.agg_state = RTL_AGG_START;
1158 
1159 	ieee80211_start_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
1160 
1161 	return 0;
1162 }
1163 
rtl_tx_agg_stop(struct ieee80211_hw * hw,struct ieee80211_sta * sta,u16 tid)1164 int rtl_tx_agg_stop(struct ieee80211_hw *hw,
1165 		struct ieee80211_sta *sta, u16 tid)
1166 {
1167 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1168 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1169 	struct rtl_sta_info *sta_entry = NULL;
1170 
1171 	if (sta == NULL)
1172 		return -EINVAL;
1173 
1174 	if (!sta->addr) {
1175 		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
1176 		return -EINVAL;
1177 	}
1178 
1179 	RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d\n",
1180 		 sta->addr, tid);
1181 
1182 	if (unlikely(tid >= MAX_TID_COUNT))
1183 		return -EINVAL;
1184 
1185 	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1186 	sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
1187 
1188 	ieee80211_stop_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
1189 
1190 	return 0;
1191 }
1192 
rtl_rx_agg_start(struct ieee80211_hw * hw,struct ieee80211_sta * sta,u16 tid)1193 int rtl_rx_agg_start(struct ieee80211_hw *hw,
1194 		     struct ieee80211_sta *sta, u16 tid)
1195 {
1196 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1197 	struct rtl_tid_data *tid_data;
1198 	struct rtl_sta_info *sta_entry = NULL;
1199 
1200 	if (sta == NULL)
1201 		return -EINVAL;
1202 
1203 	if (unlikely(tid >= MAX_TID_COUNT))
1204 		return -EINVAL;
1205 
1206 	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1207 	if (!sta_entry)
1208 		return -ENXIO;
1209 	tid_data = &sta_entry->tids[tid];
1210 
1211 	RT_TRACE(rtlpriv, COMP_RECV, DBG_DMESG,
1212 		 "on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1213 		 tid_data->seq_number);
1214 
1215 	tid_data->agg.rx_agg_state = RTL_RX_AGG_START;
1216 	return 0;
1217 }
1218 
rtl_rx_agg_stop(struct ieee80211_hw * hw,struct ieee80211_sta * sta,u16 tid)1219 int rtl_rx_agg_stop(struct ieee80211_hw *hw,
1220 		    struct ieee80211_sta *sta, u16 tid)
1221 {
1222 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1223 	struct rtl_sta_info *sta_entry = NULL;
1224 
1225 	if (sta == NULL)
1226 		return -EINVAL;
1227 
1228 	if (!sta->addr) {
1229 		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
1230 		return -EINVAL;
1231 	}
1232 
1233 	RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1234 		 "on ra = %pM tid = %d\n", sta->addr, tid);
1235 
1236 	if (unlikely(tid >= MAX_TID_COUNT))
1237 		return -EINVAL;
1238 
1239 	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1240 	sta_entry->tids[tid].agg.rx_agg_state = RTL_RX_AGG_STOP;
1241 
1242 	return 0;
1243 }
1244 
rtl_tx_agg_oper(struct ieee80211_hw * hw,struct ieee80211_sta * sta,u16 tid)1245 int rtl_tx_agg_oper(struct ieee80211_hw *hw,
1246 		struct ieee80211_sta *sta, u16 tid)
1247 {
1248 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1249 	struct rtl_sta_info *sta_entry = NULL;
1250 
1251 	if (sta == NULL)
1252 		return -EINVAL;
1253 
1254 	if (!sta->addr) {
1255 		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
1256 		return -EINVAL;
1257 	}
1258 
1259 	RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d\n",
1260 		 sta->addr, tid);
1261 
1262 	if (unlikely(tid >= MAX_TID_COUNT))
1263 		return -EINVAL;
1264 
1265 	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1266 	sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
1267 
1268 	return 0;
1269 }
1270 
1271 /*********************************************************
1272  *
1273  * wq & timer callback functions
1274  *
1275  *********************************************************/
1276 /* this function is used for roaming */
rtl_beacon_statistic(struct ieee80211_hw * hw,struct sk_buff * skb)1277 void rtl_beacon_statistic(struct ieee80211_hw *hw, struct sk_buff *skb)
1278 {
1279 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1280 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1281 
1282 	if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
1283 		return;
1284 
1285 	if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
1286 		return;
1287 
1288 	/* check if this really is a beacon */
1289 	if (!ieee80211_is_beacon(hdr->frame_control) &&
1290 	    !ieee80211_is_probe_resp(hdr->frame_control))
1291 		return;
1292 
1293 	/* min. beacon length + FCS_LEN */
1294 	if (skb->len <= 40 + FCS_LEN)
1295 		return;
1296 
1297 	/* and only beacons from the associated BSSID, please */
1298 	if (compare_ether_addr(hdr->addr3, rtlpriv->mac80211.bssid))
1299 		return;
1300 
1301 	rtlpriv->link_info.bcn_rx_inperiod++;
1302 }
1303 
rtl_watchdog_wq_callback(void * data)1304 void rtl_watchdog_wq_callback(void *data)
1305 {
1306 	struct rtl_works *rtlworks = container_of_dwork_rtl(data,
1307 							    struct rtl_works,
1308 							    watchdog_wq);
1309 	struct ieee80211_hw *hw = rtlworks->hw;
1310 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1311 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1312 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1313 	bool busytraffic = false;
1314 	bool tx_busy_traffic = false;
1315 	bool rx_busy_traffic = false;
1316 	bool higher_busytraffic = false;
1317 	bool higher_busyrxtraffic = false;
1318 	u8 idx, tid;
1319 	u32 rx_cnt_inp4eriod = 0;
1320 	u32 tx_cnt_inp4eriod = 0;
1321 	u32 aver_rx_cnt_inperiod = 0;
1322 	u32 aver_tx_cnt_inperiod = 0;
1323 	u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
1324 	u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
1325 
1326 	if (is_hal_stop(rtlhal))
1327 		return;
1328 
1329 	/* <1> Determine if action frame is allowed */
1330 	if (mac->link_state > MAC80211_NOLINK) {
1331 		if (mac->cnt_after_linked < 20)
1332 			mac->cnt_after_linked++;
1333 	} else {
1334 		mac->cnt_after_linked = 0;
1335 	}
1336 
1337 	/*
1338 	 *<2> to check if traffic busy, if
1339 	 * busytraffic we don't change channel
1340 	 */
1341 	if (mac->link_state >= MAC80211_LINKED) {
1342 
1343 		/* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
1344 		for (idx = 0; idx <= 2; idx++) {
1345 			rtlpriv->link_info.num_rx_in4period[idx] =
1346 			    rtlpriv->link_info.num_rx_in4period[idx + 1];
1347 			rtlpriv->link_info.num_tx_in4period[idx] =
1348 			    rtlpriv->link_info.num_tx_in4period[idx + 1];
1349 		}
1350 		rtlpriv->link_info.num_rx_in4period[3] =
1351 		    rtlpriv->link_info.num_rx_inperiod;
1352 		rtlpriv->link_info.num_tx_in4period[3] =
1353 		    rtlpriv->link_info.num_tx_inperiod;
1354 		for (idx = 0; idx <= 3; idx++) {
1355 			rx_cnt_inp4eriod +=
1356 			    rtlpriv->link_info.num_rx_in4period[idx];
1357 			tx_cnt_inp4eriod +=
1358 			    rtlpriv->link_info.num_tx_in4period[idx];
1359 		}
1360 		aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
1361 		aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
1362 
1363 		/* (2) check traffic busy */
1364 		if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100) {
1365 			busytraffic = true;
1366 			if (aver_rx_cnt_inperiod > aver_tx_cnt_inperiod)
1367 				rx_busy_traffic = true;
1368 			else
1369 				tx_busy_traffic = false;
1370 		}
1371 
1372 		/* Higher Tx/Rx data. */
1373 		if (aver_rx_cnt_inperiod > 4000 ||
1374 		    aver_tx_cnt_inperiod > 4000) {
1375 			higher_busytraffic = true;
1376 
1377 			/* Extremely high Rx data. */
1378 			if (aver_rx_cnt_inperiod > 5000)
1379 				higher_busyrxtraffic = true;
1380 		}
1381 
1382 		/* check every tid's tx traffic */
1383 		for (tid = 0; tid <= 7; tid++) {
1384 			for (idx = 0; idx <= 2; idx++)
1385 				rtlpriv->link_info.tidtx_in4period[tid][idx] =
1386 				  rtlpriv->link_info.tidtx_in4period[tid]
1387 				  [idx + 1];
1388 			rtlpriv->link_info.tidtx_in4period[tid][3] =
1389 				rtlpriv->link_info.tidtx_inperiod[tid];
1390 
1391 			for (idx = 0; idx <= 3; idx++)
1392 				tidtx_inp4eriod[tid] +=
1393 				  rtlpriv->link_info.tidtx_in4period[tid][idx];
1394 			aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
1395 			if (aver_tidtx_inperiod[tid] > 5000)
1396 				rtlpriv->link_info.higher_busytxtraffic[tid] =
1397 						   true;
1398 			else
1399 				rtlpriv->link_info.higher_busytxtraffic[tid] =
1400 						   false;
1401 		}
1402 
1403 		if (((rtlpriv->link_info.num_rx_inperiod +
1404 		      rtlpriv->link_info.num_tx_inperiod) > 8) ||
1405 		    (rtlpriv->link_info.num_rx_inperiod > 2))
1406 			rtlpriv->enter_ps = true;
1407 		else
1408 			rtlpriv->enter_ps = false;
1409 
1410 		/* LeisurePS only work in infra mode. */
1411 		schedule_work(&rtlpriv->works.lps_change_work);
1412 	}
1413 
1414 	rtlpriv->link_info.num_rx_inperiod = 0;
1415 	rtlpriv->link_info.num_tx_inperiod = 0;
1416 	for (tid = 0; tid <= 7; tid++)
1417 		rtlpriv->link_info.tidtx_inperiod[tid] = 0;
1418 
1419 	rtlpriv->link_info.busytraffic = busytraffic;
1420 	rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
1421 	rtlpriv->link_info.rx_busy_traffic = rx_busy_traffic;
1422 	rtlpriv->link_info.tx_busy_traffic = tx_busy_traffic;
1423 	rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
1424 
1425 	/* <3> DM */
1426 	rtlpriv->cfg->ops->dm_watchdog(hw);
1427 
1428 	/* <4> roaming */
1429 	if (mac->link_state == MAC80211_LINKED &&
1430 	    mac->opmode == NL80211_IFTYPE_STATION) {
1431 		if ((rtlpriv->link_info.bcn_rx_inperiod +
1432 		     rtlpriv->link_info.num_rx_inperiod) == 0) {
1433 			rtlpriv->link_info.roam_times++;
1434 			RT_TRACE(rtlpriv, COMP_ERR, DBG_DMESG,
1435 				 "AP off for %d s\n",
1436 				 (rtlpriv->link_info.roam_times * 2));
1437 
1438 			/* if we can't recv beacon for 6s, we should
1439 			 * reconnect this AP
1440 			 */
1441 			if (rtlpriv->link_info.roam_times >= 3) {
1442 				RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1443 					 "AP off, try to reconnect now\n");
1444 				rtlpriv->link_info.roam_times = 0;
1445 				ieee80211_connection_loss(rtlpriv->mac80211.vif);
1446 			}
1447 		} else {
1448 			rtlpriv->link_info.roam_times = 0;
1449 		}
1450 	}
1451 	rtlpriv->link_info.bcn_rx_inperiod = 0;
1452 }
1453 
rtl_watch_dog_timer_callback(unsigned long data)1454 void rtl_watch_dog_timer_callback(unsigned long data)
1455 {
1456 	struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1457 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1458 
1459 	queue_delayed_work(rtlpriv->works.rtl_wq,
1460 			   &rtlpriv->works.watchdog_wq, 0);
1461 
1462 	mod_timer(&rtlpriv->works.watchdog_timer,
1463 		  jiffies + MSECS(RTL_WATCH_DOG_TIME));
1464 }
1465 
rtl_fwevt_wq_callback(void * data)1466 void rtl_fwevt_wq_callback(void *data)
1467 {
1468 	struct rtl_works *rtlworks =
1469 		container_of_dwork_rtl(data, struct rtl_works, fwevt_wq);
1470 	struct ieee80211_hw *hw = rtlworks->hw;
1471 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1472 
1473 	rtlpriv->cfg->ops->c2h_command_handle(hw);
1474 }
1475 
rtl_easy_concurrent_retrytimer_callback(unsigned long data)1476 void rtl_easy_concurrent_retrytimer_callback(unsigned long data)
1477 {
1478 	struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1479 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1480 	struct rtl_priv *buddy_priv = rtlpriv->buddy_priv;
1481 
1482 	if (buddy_priv == NULL)
1483 		return;
1484 
1485 	rtlpriv->cfg->ops->dualmac_easy_concurrent(hw);
1486 }
1487 
1488 /*********************************************************
1489  *
1490  * frame process functions
1491  *
1492  *********************************************************/
rtl_find_ie(u8 * data,unsigned int len,u8 ie)1493 u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
1494 {
1495 	struct ieee80211_mgmt *mgmt = (void *)data;
1496 	u8 *pos, *end;
1497 
1498 	pos = (u8 *)mgmt->u.beacon.variable;
1499 	end = data + len;
1500 	while (pos < end) {
1501 		if (pos + 2 + pos[1] > end)
1502 			return NULL;
1503 
1504 		if (pos[0] == ie)
1505 			return pos;
1506 
1507 		pos += 2 + pos[1];
1508 	}
1509 	return NULL;
1510 }
1511 
1512 /* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
1513 /* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
rtl_make_smps_action(struct ieee80211_hw * hw,enum ieee80211_smps_mode smps,u8 * da,u8 * bssid)1514 static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
1515 		enum ieee80211_smps_mode smps, u8 *da, u8 *bssid)
1516 {
1517 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1518 	struct sk_buff *skb;
1519 	struct ieee80211_mgmt *action_frame;
1520 
1521 	/* 27 = header + category + action + smps mode */
1522 	skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
1523 	if (!skb)
1524 		return NULL;
1525 
1526 	skb_reserve(skb, hw->extra_tx_headroom);
1527 	action_frame = (void *)skb_put(skb, 27);
1528 	memset(action_frame, 0, 27);
1529 	memcpy(action_frame->da, da, ETH_ALEN);
1530 	memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
1531 	memcpy(action_frame->bssid, bssid, ETH_ALEN);
1532 	action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1533 						  IEEE80211_STYPE_ACTION);
1534 	action_frame->u.action.category = WLAN_CATEGORY_HT;
1535 	action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
1536 	switch (smps) {
1537 	case IEEE80211_SMPS_AUTOMATIC:/* 0 */
1538 	case IEEE80211_SMPS_NUM_MODES:/* 4 */
1539 		WARN_ON(1);
1540 	case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
1541 		action_frame->u.action.u.ht_smps.smps_control =
1542 				WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
1543 		break;
1544 	case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
1545 		action_frame->u.action.u.ht_smps.smps_control =
1546 				WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
1547 		break;
1548 	case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
1549 		action_frame->u.action.u.ht_smps.smps_control =
1550 				WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
1551 		break;
1552 	}
1553 
1554 	return skb;
1555 }
1556 
rtl_send_smps_action(struct ieee80211_hw * hw,struct ieee80211_sta * sta,enum ieee80211_smps_mode smps)1557 int rtl_send_smps_action(struct ieee80211_hw *hw,
1558 		struct ieee80211_sta *sta,
1559 		enum ieee80211_smps_mode smps)
1560 {
1561 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1562 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1563 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1564 	struct sk_buff *skb = NULL;
1565 	struct rtl_tcb_desc tcb_desc;
1566 	u8 bssid[ETH_ALEN] = {0};
1567 
1568 	memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
1569 
1570 	if (rtlpriv->mac80211.act_scanning)
1571 		goto err_free;
1572 
1573 	if (!sta)
1574 		goto err_free;
1575 
1576 	if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
1577 		goto err_free;
1578 
1579 	if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1580 		goto err_free;
1581 
1582 	if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP)
1583 		memcpy(bssid, rtlpriv->efuse.dev_addr, ETH_ALEN);
1584 	else
1585 		memcpy(bssid, rtlpriv->mac80211.bssid, ETH_ALEN);
1586 
1587 	skb = rtl_make_smps_action(hw, smps, sta->addr, bssid);
1588 	/* this is a type = mgmt * stype = action frame */
1589 	if (skb) {
1590 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1591 		struct rtl_sta_info *sta_entry =
1592 			(struct rtl_sta_info *) sta->drv_priv;
1593 		sta_entry->mimo_ps = smps;
1594 
1595 		info->control.rates[0].idx = 0;
1596 		info->band = hw->conf.chandef.chan->band;
1597 		rtlpriv->intf_ops->adapter_tx(hw, sta, skb, &tcb_desc);
1598 	}
1599 	return 1;
1600 
1601 err_free:
1602 	return 0;
1603 }
1604 EXPORT_SYMBOL(rtl_send_smps_action);
1605 
1606 /* There seem to be issues in mac80211 regarding when del ba frames can be
1607  * received. As a work around, we make a fake del_ba if we receive a ba_req;
1608  * however, rx_agg was opened to let mac80211 release some ba related
1609  * resources. This del_ba is for tx only.
1610  */
rtl_make_del_ba(struct ieee80211_hw * hw,u8 * sa,u8 * bssid,u16 tid)1611 struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw,
1612 				u8 *sa, u8 *bssid, u16 tid)
1613 {
1614 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1615 	struct sk_buff *skb;
1616 	struct ieee80211_mgmt *action_frame;
1617 	u16 params;
1618 
1619 	/* 27 = header + category + action + smps mode */
1620 	skb = dev_alloc_skb(34 + hw->extra_tx_headroom);
1621 	if (!skb)
1622 		return NULL;
1623 
1624 	skb_reserve(skb, hw->extra_tx_headroom);
1625 	action_frame = (void *)skb_put(skb, 34);
1626 	memset(action_frame, 0, 34);
1627 	memcpy(action_frame->sa, sa, ETH_ALEN);
1628 	memcpy(action_frame->da, rtlefuse->dev_addr, ETH_ALEN);
1629 	memcpy(action_frame->bssid, bssid, ETH_ALEN);
1630 	action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1631 						  IEEE80211_STYPE_ACTION);
1632 	action_frame->u.action.category = WLAN_CATEGORY_BACK;
1633 	action_frame->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1634 	params = (u16)(1 << 11);	/* bit 11 initiator */
1635 	params |= (u16)(tid << 12);		/* bit 15:12 TID number */
1636 
1637 	action_frame->u.action.u.delba.params = cpu_to_le16(params);
1638 	action_frame->u.action.u.delba.reason_code =
1639 		cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
1640 
1641 	return skb;
1642 }
1643 
1644 /*********************************************************
1645  *
1646  * IOT functions
1647  *
1648  *********************************************************/
rtl_chk_vendor_ouisub(struct ieee80211_hw * hw,struct octet_string vendor_ie)1649 static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
1650 		struct octet_string vendor_ie)
1651 {
1652 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1653 	bool matched = false;
1654 	static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
1655 	static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
1656 	static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
1657 	static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
1658 	static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
1659 	static u8 racap[] = { 0x00, 0x0c, 0x43 };
1660 	static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
1661 	static u8 marvcap[] = { 0x00, 0x50, 0x43 };
1662 
1663 	if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
1664 		memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
1665 		rtlpriv->mac80211.vendor = PEER_ATH;
1666 		matched = true;
1667 	} else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
1668 		memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
1669 		memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
1670 		rtlpriv->mac80211.vendor = PEER_BROAD;
1671 		matched = true;
1672 	} else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
1673 		rtlpriv->mac80211.vendor = PEER_RAL;
1674 		matched = true;
1675 	} else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
1676 		rtlpriv->mac80211.vendor = PEER_CISCO;
1677 		matched = true;
1678 	} else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
1679 		rtlpriv->mac80211.vendor = PEER_MARV;
1680 		matched = true;
1681 	}
1682 
1683 	return matched;
1684 }
1685 
rtl_find_221_ie(struct ieee80211_hw * hw,u8 * data,unsigned int len)1686 static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
1687 		unsigned int len)
1688 {
1689 	struct ieee80211_mgmt *mgmt = (void *)data;
1690 	struct octet_string vendor_ie;
1691 	u8 *pos, *end;
1692 
1693 	pos = (u8 *)mgmt->u.beacon.variable;
1694 	end = data + len;
1695 	while (pos < end) {
1696 		if (pos[0] == 221) {
1697 			vendor_ie.length = pos[1];
1698 			vendor_ie.octet = &pos[2];
1699 			if (rtl_chk_vendor_ouisub(hw, vendor_ie))
1700 				return true;
1701 		}
1702 
1703 		if (pos + 2 + pos[1] > end)
1704 			return false;
1705 
1706 		pos += 2 + pos[1];
1707 	}
1708 	return false;
1709 }
1710 
rtl_recognize_peer(struct ieee80211_hw * hw,u8 * data,unsigned int len)1711 void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
1712 {
1713 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1714 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1715 	struct ieee80211_hdr *hdr = (void *)data;
1716 	u32 vendor = PEER_UNKNOWN;
1717 
1718 	static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
1719 	static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
1720 	static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
1721 	static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
1722 	static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
1723 	static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
1724 	static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
1725 	static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
1726 	static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
1727 	static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
1728 	static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
1729 	static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
1730 	static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
1731 	static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
1732 	static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
1733 	static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
1734 
1735 	if (mac->opmode != NL80211_IFTYPE_STATION)
1736 		return;
1737 
1738 	if (mac->link_state == MAC80211_NOLINK) {
1739 		mac->vendor = PEER_UNKNOWN;
1740 		return;
1741 	}
1742 
1743 	if (mac->cnt_after_linked > 2)
1744 		return;
1745 
1746 	/* check if this really is a beacon */
1747 	if (!ieee80211_is_beacon(hdr->frame_control))
1748 		return;
1749 
1750 	/* min. beacon length + FCS_LEN */
1751 	if (len <= 40 + FCS_LEN)
1752 		return;
1753 
1754 	/* and only beacons from the associated BSSID, please */
1755 	if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
1756 		return;
1757 
1758 	if (rtl_find_221_ie(hw, data, len))
1759 		vendor = mac->vendor;
1760 
1761 	if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
1762 		(memcmp(mac->bssid, ap5_2, 3) == 0) ||
1763 		(memcmp(mac->bssid, ap5_3, 3) == 0) ||
1764 		(memcmp(mac->bssid, ap5_4, 3) == 0) ||
1765 		(memcmp(mac->bssid, ap5_5, 3) == 0) ||
1766 		(memcmp(mac->bssid, ap5_6, 3) == 0) ||
1767 		vendor == PEER_ATH) {
1768 		vendor = PEER_ATH;
1769 		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ath find\n");
1770 	} else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
1771 		(memcmp(mac->bssid, ap4_5, 3) == 0) ||
1772 		(memcmp(mac->bssid, ap4_1, 3) == 0) ||
1773 		(memcmp(mac->bssid, ap4_2, 3) == 0) ||
1774 		(memcmp(mac->bssid, ap4_3, 3) == 0) ||
1775 		vendor == PEER_RAL) {
1776 		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ral find\n");
1777 		vendor = PEER_RAL;
1778 	} else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
1779 		vendor == PEER_CISCO) {
1780 		vendor = PEER_CISCO;
1781 		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>cisco find\n");
1782 	} else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
1783 		(memcmp(mac->bssid, ap3_2, 3) == 0) ||
1784 		(memcmp(mac->bssid, ap3_3, 3) == 0) ||
1785 		vendor == PEER_BROAD) {
1786 		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>broad find\n");
1787 		vendor = PEER_BROAD;
1788 	} else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
1789 		vendor == PEER_MARV) {
1790 		vendor = PEER_MARV;
1791 		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>marv find\n");
1792 	}
1793 
1794 	mac->vendor = vendor;
1795 }
1796 
1797 /*********************************************************
1798  *
1799  * sysfs functions
1800  *
1801  *********************************************************/
rtl_show_debug_level(struct device * d,struct device_attribute * attr,char * buf)1802 static ssize_t rtl_show_debug_level(struct device *d,
1803 				    struct device_attribute *attr, char *buf)
1804 {
1805 	struct ieee80211_hw *hw = dev_get_drvdata(d);
1806 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1807 
1808 	return sprintf(buf, "0x%08X\n", rtlpriv->dbg.global_debuglevel);
1809 }
1810 
rtl_store_debug_level(struct device * d,struct device_attribute * attr,const char * buf,size_t count)1811 static ssize_t rtl_store_debug_level(struct device *d,
1812 				     struct device_attribute *attr,
1813 				     const char *buf, size_t count)
1814 {
1815 	struct ieee80211_hw *hw = dev_get_drvdata(d);
1816 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1817 	unsigned long val;
1818 	int ret;
1819 
1820 	ret = strict_strtoul(buf, 0, &val);
1821 	if (ret) {
1822 		printk(KERN_DEBUG "%s is not in hex or decimal form.\n", buf);
1823 	} else {
1824 		rtlpriv->dbg.global_debuglevel = val;
1825 		printk(KERN_DEBUG "debuglevel:%x\n",
1826 		       rtlpriv->dbg.global_debuglevel);
1827 	}
1828 
1829 	return strnlen(buf, count);
1830 }
1831 
1832 static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
1833 		   rtl_show_debug_level, rtl_store_debug_level);
1834 
1835 static struct attribute *rtl_sysfs_entries[] = {
1836 
1837 	&dev_attr_debug_level.attr,
1838 
1839 	NULL
1840 };
1841 
1842 /*
1843  * "name" is folder name witch will be
1844  * put in device directory like :
1845  * sys/devices/pci0000:00/0000:00:1c.4/
1846  * 0000:06:00.0/rtl_sysfs
1847  */
1848 struct attribute_group rtl_attribute_group = {
1849 	.name = "rtlsysfs",
1850 	.attrs = rtl_sysfs_entries,
1851 };
1852 
1853 MODULE_AUTHOR("lizhaoming	<chaoming_li@realsil.com.cn>");
1854 MODULE_AUTHOR("Realtek WlanFAE	<wlanfae@realtek.com>");
1855 MODULE_AUTHOR("Larry Finger	<Larry.FInger@lwfinger.net>");
1856 MODULE_LICENSE("GPL");
1857 MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
1858 
1859 struct rtl_global_var global_var = {};
1860 
rtl_core_module_init(void)1861 static int __init rtl_core_module_init(void)
1862 {
1863 	if (rtl_rate_control_register())
1864 		pr_err("Unable to register rtl_rc, use default RC !!\n");
1865 
1866 	/* init some global vars */
1867 	INIT_LIST_HEAD(&global_var.glb_priv_list);
1868 	spin_lock_init(&global_var.glb_list_lock);
1869 
1870 	return 0;
1871 }
1872 
rtl_core_module_exit(void)1873 static void __exit rtl_core_module_exit(void)
1874 {
1875 	/*RC*/
1876 	rtl_rate_control_unregister();
1877 }
1878 
1879 module_init(rtl_core_module_init);
1880 module_exit(rtl_core_module_exit);
1881