Commit | Line | Data |
---|---|---|
0c817338 LF |
1 | /****************************************************************************** |
2 | * | |
3 | * Copyright(c) 2009-2010 Realtek Corporation. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of version 2 of the GNU General Public License as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along with | |
15 | * this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA | |
17 | * | |
18 | * The full GNU General Public License is included in this distribution in the | |
19 | * file called LICENSE. | |
20 | * | |
21 | * Contact Information: | |
22 | * wlanfae <wlanfae@realtek.com> | |
23 | * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park, | |
24 | * Hsinchu 300, Taiwan. | |
25 | * | |
26 | * Larry Finger <Larry.Finger@lwfinger.net> | |
27 | * | |
28 | *****************************************************************************/ | |
29 | ||
30 | #include <linux/ip.h> | |
31 | #include "wifi.h" | |
32 | #include "rc.h" | |
33 | #include "base.h" | |
34 | #include "efuse.h" | |
35 | #include "cam.h" | |
36 | #include "ps.h" | |
37 | #include "regd.h" | |
38 | ||
39 | /* | |
40 | *NOTICE!!!: This file will be very big, we hsould | |
41 | *keep it clear under follwing roles: | |
42 | * | |
43 | *This file include follwing part, so, if you add new | |
44 | *functions into this file, please check which part it | |
45 | *should includes. or check if you should add new part | |
46 | *for this file: | |
47 | * | |
48 | *1) mac80211 init functions | |
49 | *2) tx information functions | |
50 | *3) functions called by core.c | |
51 | *4) wq & timer callback functions | |
52 | *5) frame process functions | |
53 | *6) sysfs functions | |
54 | *7) ... | |
55 | */ | |
56 | ||
57 | /********************************************************* | |
58 | * | |
59 | * mac80211 init functions | |
60 | * | |
61 | *********************************************************/ | |
62 | static struct ieee80211_channel rtl_channeltable[] = { | |
63 | {.center_freq = 2412, .hw_value = 1,}, | |
64 | {.center_freq = 2417, .hw_value = 2,}, | |
65 | {.center_freq = 2422, .hw_value = 3,}, | |
66 | {.center_freq = 2427, .hw_value = 4,}, | |
67 | {.center_freq = 2432, .hw_value = 5,}, | |
68 | {.center_freq = 2437, .hw_value = 6,}, | |
69 | {.center_freq = 2442, .hw_value = 7,}, | |
70 | {.center_freq = 2447, .hw_value = 8,}, | |
71 | {.center_freq = 2452, .hw_value = 9,}, | |
72 | {.center_freq = 2457, .hw_value = 10,}, | |
73 | {.center_freq = 2462, .hw_value = 11,}, | |
74 | {.center_freq = 2467, .hw_value = 12,}, | |
75 | {.center_freq = 2472, .hw_value = 13,}, | |
76 | {.center_freq = 2484, .hw_value = 14,}, | |
77 | }; | |
78 | ||
79 | static struct ieee80211_rate rtl_ratetable[] = { | |
80 | {.bitrate = 10, .hw_value = 0x00,}, | |
81 | {.bitrate = 20, .hw_value = 0x01,}, | |
82 | {.bitrate = 55, .hw_value = 0x02,}, | |
83 | {.bitrate = 110, .hw_value = 0x03,}, | |
84 | {.bitrate = 60, .hw_value = 0x04,}, | |
85 | {.bitrate = 90, .hw_value = 0x05,}, | |
86 | {.bitrate = 120, .hw_value = 0x06,}, | |
87 | {.bitrate = 180, .hw_value = 0x07,}, | |
88 | {.bitrate = 240, .hw_value = 0x08,}, | |
89 | {.bitrate = 360, .hw_value = 0x09,}, | |
90 | {.bitrate = 480, .hw_value = 0x0a,}, | |
91 | {.bitrate = 540, .hw_value = 0x0b,}, | |
92 | }; | |
93 | ||
94 | static const struct ieee80211_supported_band rtl_band_2ghz = { | |
95 | .band = IEEE80211_BAND_2GHZ, | |
96 | ||
97 | .channels = rtl_channeltable, | |
98 | .n_channels = ARRAY_SIZE(rtl_channeltable), | |
99 | ||
100 | .bitrates = rtl_ratetable, | |
101 | .n_bitrates = ARRAY_SIZE(rtl_ratetable), | |
102 | ||
103 | .ht_cap = {0}, | |
104 | }; | |
105 | ||
106 | static void _rtl_init_hw_ht_capab(struct ieee80211_hw *hw, | |
107 | struct ieee80211_sta_ht_cap *ht_cap) | |
108 | { | |
109 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
110 | struct rtl_phy *rtlphy = &(rtlpriv->phy); | |
111 | ||
112 | ht_cap->ht_supported = true; | |
113 | ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 | | |
114 | IEEE80211_HT_CAP_SGI_40 | | |
115 | IEEE80211_HT_CAP_SGI_20 | | |
116 | IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU; | |
117 | ||
118 | /* | |
119 | *Maximum length of AMPDU that the STA can receive. | |
120 | *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets) | |
121 | */ | |
122 | ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K; | |
123 | ||
124 | /*Minimum MPDU start spacing , */ | |
125 | ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16; | |
126 | ||
127 | ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED; | |
128 | ||
129 | /* | |
130 | *hw->wiphy->bands[IEEE80211_BAND_2GHZ] | |
131 | *base on ant_num | |
132 | *rx_mask: RX mask | |
133 | *if rx_ant =1 rx_mask[0]=0xff;==>MCS0-MCS7 | |
134 | *if rx_ant =2 rx_mask[1]=0xff;==>MCS8-MCS15 | |
135 | *if rx_ant >=3 rx_mask[2]=0xff; | |
136 | *if BW_40 rx_mask[4]=0x01; | |
137 | *highest supported RX rate | |
138 | */ | |
139 | if (get_rf_type(rtlphy) == RF_1T2R || get_rf_type(rtlphy) == RF_2T2R) { | |
140 | ||
141 | RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("1T2R or 2T2R\n")); | |
142 | ||
143 | ht_cap->mcs.rx_mask[0] = 0xFF; | |
144 | ht_cap->mcs.rx_mask[1] = 0xFF; | |
145 | ht_cap->mcs.rx_mask[4] = 0x01; | |
146 | ||
17c9ac62 | 147 | ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15); |
0c817338 LF |
148 | } else if (get_rf_type(rtlphy) == RF_1T1R) { |
149 | ||
150 | RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("1T1R\n")); | |
151 | ||
152 | ht_cap->mcs.rx_mask[0] = 0xFF; | |
153 | ht_cap->mcs.rx_mask[1] = 0x00; | |
154 | ht_cap->mcs.rx_mask[4] = 0x01; | |
155 | ||
17c9ac62 | 156 | ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7); |
0c817338 LF |
157 | } |
158 | } | |
159 | ||
160 | static void _rtl_init_mac80211(struct ieee80211_hw *hw) | |
161 | { | |
162 | struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw)); | |
163 | struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw)); | |
164 | struct ieee80211_supported_band *sband; | |
165 | ||
166 | /* <1> use mac->bands as mem for hw->wiphy->bands */ | |
167 | sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]); | |
168 | ||
169 | /* | |
170 | * <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ] | |
171 | * to default value(1T1R) | |
172 | */ | |
173 | memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]), &rtl_band_2ghz, | |
174 | sizeof(struct ieee80211_supported_band)); | |
175 | ||
176 | /* <3> init ht cap base on ant_num */ | |
177 | _rtl_init_hw_ht_capab(hw, &sband->ht_cap); | |
178 | ||
179 | /* <4> set mac->sband to wiphy->sband */ | |
180 | hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband; | |
181 | ||
182 | /* <5> set hw caps */ | |
183 | hw->flags = IEEE80211_HW_SIGNAL_DBM | | |
184 | IEEE80211_HW_RX_INCLUDES_FCS | | |
185 | IEEE80211_HW_BEACON_FILTER | IEEE80211_HW_AMPDU_AGGREGATION | /*PS*/ | |
186 | /*IEEE80211_HW_SUPPORTS_PS | */ | |
187 | /*IEEE80211_HW_PS_NULLFUNC_STACK | */ | |
188 | /*IEEE80211_HW_SUPPORTS_DYNAMIC_PS | */ | |
189 | IEEE80211_HW_REPORTS_TX_ACK_STATUS | 0; | |
190 | ||
191 | hw->wiphy->interface_modes = | |
192 | BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_ADHOC); | |
193 | ||
194 | hw->wiphy->rts_threshold = 2347; | |
195 | ||
196 | hw->queues = AC_MAX; | |
197 | hw->extra_tx_headroom = RTL_TX_HEADER_SIZE; | |
198 | ||
199 | /* TODO: Correct this value for our hw */ | |
200 | /* TODO: define these hard code value */ | |
201 | hw->channel_change_time = 100; | |
202 | hw->max_listen_interval = 5; | |
203 | hw->max_rate_tries = 4; | |
204 | /* hw->max_rates = 1; */ | |
205 | ||
206 | /* <6> mac address */ | |
207 | if (is_valid_ether_addr(rtlefuse->dev_addr)) { | |
208 | SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr); | |
209 | } else { | |
210 | u8 rtlmac[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 }; | |
211 | get_random_bytes((rtlmac + (ETH_ALEN - 1)), 1); | |
212 | SET_IEEE80211_PERM_ADDR(hw, rtlmac); | |
213 | } | |
214 | ||
215 | } | |
216 | ||
217 | static void _rtl_init_deferred_work(struct ieee80211_hw *hw) | |
218 | { | |
219 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
220 | ||
221 | /* <1> timer */ | |
222 | init_timer(&rtlpriv->works.watchdog_timer); | |
223 | setup_timer(&rtlpriv->works.watchdog_timer, | |
224 | rtl_watch_dog_timer_callback, (unsigned long)hw); | |
225 | ||
226 | /* <2> work queue */ | |
227 | rtlpriv->works.hw = hw; | |
3d986b25 | 228 | rtlpriv->works.rtl_wq = alloc_workqueue(rtlpriv->cfg->name, 0, 0); |
0c817338 LF |
229 | INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq, |
230 | (void *)rtl_watchdog_wq_callback); | |
231 | INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq, | |
232 | (void *)rtl_ips_nic_off_wq_callback); | |
233 | ||
234 | } | |
235 | ||
236 | void rtl_deinit_deferred_work(struct ieee80211_hw *hw) | |
237 | { | |
238 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
239 | ||
240 | del_timer_sync(&rtlpriv->works.watchdog_timer); | |
241 | ||
242 | cancel_delayed_work(&rtlpriv->works.watchdog_wq); | |
243 | cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq); | |
244 | } | |
245 | ||
246 | void rtl_init_rfkill(struct ieee80211_hw *hw) | |
247 | { | |
248 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
249 | ||
250 | bool radio_state; | |
251 | bool blocked; | |
252 | u8 valid = 0; | |
253 | ||
0c817338 LF |
254 | radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid); |
255 | ||
e6d8a817 LF |
256 | /*set init state to that of switch */ |
257 | rtlpriv->rfkill.rfkill_state = radio_state; | |
258 | printk(KERN_INFO "rtlwifi: wireless switch is %s\n", | |
259 | rtlpriv->rfkill.rfkill_state ? "on" : "off"); | |
0c817338 | 260 | |
e6d8a817 | 261 | if (valid) { |
0c817338 LF |
262 | rtlpriv->rfkill.rfkill_state = radio_state; |
263 | ||
264 | blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1; | |
265 | wiphy_rfkill_set_hw_state(hw->wiphy, blocked); | |
266 | } | |
267 | ||
268 | wiphy_rfkill_start_polling(hw->wiphy); | |
269 | } | |
270 | ||
271 | void rtl_deinit_rfkill(struct ieee80211_hw *hw) | |
272 | { | |
273 | wiphy_rfkill_stop_polling(hw->wiphy); | |
274 | } | |
275 | ||
276 | int rtl_init_core(struct ieee80211_hw *hw) | |
277 | { | |
278 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
279 | struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw)); | |
280 | ||
281 | /* <1> init mac80211 */ | |
282 | _rtl_init_mac80211(hw); | |
283 | rtlmac->hw = hw; | |
284 | ||
285 | /* <2> rate control register */ | |
375ff4c7 | 286 | hw->rate_control_algorithm = "rtl_rc"; |
0c817338 LF |
287 | |
288 | /* | |
289 | * <3> init CRDA must come after init | |
290 | * mac80211 hw in _rtl_init_mac80211. | |
291 | */ | |
292 | if (rtl_regd_init(hw, rtl_reg_notifier)) { | |
293 | RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, ("REGD init failed\n")); | |
294 | return 1; | |
295 | } else { | |
296 | /* CRDA regd hint must after init CRDA */ | |
297 | if (regulatory_hint(hw->wiphy, rtlpriv->regd.alpha2)) { | |
298 | RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, | |
299 | ("regulatory_hint fail\n")); | |
300 | } | |
301 | } | |
302 | ||
303 | /* <4> locks */ | |
8a09d6d8 | 304 | mutex_init(&rtlpriv->locks.conf_mutex); |
d704300f | 305 | spin_lock_init(&rtlpriv->locks.ips_lock); |
0c817338 LF |
306 | spin_lock_init(&rtlpriv->locks.irq_th_lock); |
307 | spin_lock_init(&rtlpriv->locks.h2c_lock); | |
308 | spin_lock_init(&rtlpriv->locks.rf_ps_lock); | |
309 | spin_lock_init(&rtlpriv->locks.rf_lock); | |
310 | spin_lock_init(&rtlpriv->locks.lps_lock); | |
311 | ||
312 | rtlmac->link_state = MAC80211_NOLINK; | |
313 | ||
314 | /* <5> init deferred work */ | |
315 | _rtl_init_deferred_work(hw); | |
316 | ||
317 | return 0; | |
318 | } | |
319 | ||
320 | void rtl_deinit_core(struct ieee80211_hw *hw) | |
321 | { | |
0c817338 LF |
322 | } |
323 | ||
324 | void rtl_init_rx_config(struct ieee80211_hw *hw) | |
325 | { | |
326 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
327 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); | |
328 | ||
329 | rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf)); | |
330 | rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_MGT_FILTER, | |
331 | (u8 *) (&mac->rx_mgt_filter)); | |
332 | rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_CTRL_FILTER, | |
333 | (u8 *) (&mac->rx_ctrl_filter)); | |
334 | rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_DATA_FILTER, | |
335 | (u8 *) (&mac->rx_data_filter)); | |
336 | } | |
337 | ||
338 | /********************************************************* | |
339 | * | |
340 | * tx information functions | |
341 | * | |
342 | *********************************************************/ | |
343 | static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw, | |
344 | struct rtl_tcb_desc *tcb_desc, | |
345 | struct ieee80211_tx_info *info) | |
346 | { | |
347 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
348 | u8 rate_flag = info->control.rates[0].flags; | |
349 | ||
350 | tcb_desc->use_shortpreamble = false; | |
351 | ||
352 | /* 1M can only use Long Preamble. 11B spec */ | |
353 | if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M]) | |
354 | return; | |
355 | else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) | |
356 | tcb_desc->use_shortpreamble = true; | |
357 | ||
358 | return; | |
359 | } | |
360 | ||
361 | static void _rtl_query_shortgi(struct ieee80211_hw *hw, | |
362 | struct rtl_tcb_desc *tcb_desc, | |
363 | struct ieee80211_tx_info *info) | |
364 | { | |
365 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); | |
366 | u8 rate_flag = info->control.rates[0].flags; | |
367 | ||
368 | tcb_desc->use_shortgi = false; | |
369 | ||
370 | if (!mac->ht_enable) | |
371 | return; | |
372 | ||
373 | if (!mac->sgi_40 && !mac->sgi_20) | |
374 | return; | |
375 | ||
376 | if ((mac->bw_40 == true) && mac->sgi_40) | |
377 | tcb_desc->use_shortgi = true; | |
378 | else if ((mac->bw_40 == false) && mac->sgi_20) | |
379 | tcb_desc->use_shortgi = true; | |
380 | ||
381 | if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI)) | |
382 | tcb_desc->use_shortgi = false; | |
383 | ||
384 | } | |
385 | ||
386 | static void _rtl_query_protection_mode(struct ieee80211_hw *hw, | |
387 | struct rtl_tcb_desc *tcb_desc, | |
388 | struct ieee80211_tx_info *info) | |
389 | { | |
390 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
391 | u8 rate_flag = info->control.rates[0].flags; | |
392 | ||
393 | /* Common Settings */ | |
7ea47240 LF |
394 | tcb_desc->rts_stbc = false; |
395 | tcb_desc->cts_enable = false; | |
0c817338 | 396 | tcb_desc->rts_sc = 0; |
7ea47240 LF |
397 | tcb_desc->rts_bw = false; |
398 | tcb_desc->rts_use_shortpreamble = false; | |
399 | tcb_desc->rts_use_shortgi = false; | |
0c817338 LF |
400 | |
401 | if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) { | |
402 | /* Use CTS-to-SELF in protection mode. */ | |
7ea47240 LF |
403 | tcb_desc->rts_enable = true; |
404 | tcb_desc->cts_enable = true; | |
0c817338 LF |
405 | tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M]; |
406 | } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) { | |
407 | /* Use RTS-CTS in protection mode. */ | |
7ea47240 | 408 | tcb_desc->rts_enable = true; |
0c817338 LF |
409 | tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M]; |
410 | } | |
411 | ||
412 | } | |
413 | ||
414 | static void _rtl_txrate_selectmode(struct ieee80211_hw *hw, | |
415 | struct rtl_tcb_desc *tcb_desc) | |
416 | { | |
417 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
418 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); | |
419 | ||
420 | if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) { | |
421 | if (mac->opmode == NL80211_IFTYPE_STATION) | |
422 | tcb_desc->ratr_index = 0; | |
423 | else if (mac->opmode == NL80211_IFTYPE_ADHOC) { | |
7ea47240 | 424 | if (tcb_desc->multicast || tcb_desc->broadcast) { |
0c817338 LF |
425 | tcb_desc->hw_rate = |
426 | rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M]; | |
427 | tcb_desc->use_driver_rate = 1; | |
428 | } else { | |
429 | /* TODO */ | |
430 | } | |
431 | } | |
432 | } | |
433 | ||
7ea47240 | 434 | if (rtlpriv->dm.useramask) { |
32473284 | 435 | /* TODO adhoc and station handled differently in the future */ |
0c817338 LF |
436 | tcb_desc->mac_id = 0; |
437 | ||
438 | if ((mac->mode == WIRELESS_MODE_N_24G) || | |
439 | (mac->mode == WIRELESS_MODE_N_5G)) { | |
440 | tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB; | |
441 | } else if (mac->mode & WIRELESS_MODE_G) { | |
442 | tcb_desc->ratr_index = RATR_INX_WIRELESS_GB; | |
443 | } else if (mac->mode & WIRELESS_MODE_B) { | |
444 | tcb_desc->ratr_index = RATR_INX_WIRELESS_B; | |
445 | } | |
446 | } | |
447 | ||
448 | } | |
449 | ||
450 | static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw, | |
451 | struct rtl_tcb_desc *tcb_desc) | |
452 | { | |
453 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
454 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); | |
455 | ||
7ea47240 | 456 | tcb_desc->packet_bw = false; |
0c817338 LF |
457 | |
458 | if (!mac->bw_40 || !mac->ht_enable) | |
459 | return; | |
460 | ||
7ea47240 | 461 | if (tcb_desc->multicast || tcb_desc->broadcast) |
0c817338 LF |
462 | return; |
463 | ||
464 | /*use legency rate, shall use 20MHz */ | |
465 | if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M]) | |
466 | return; | |
467 | ||
7ea47240 | 468 | tcb_desc->packet_bw = true; |
0c817338 LF |
469 | } |
470 | ||
471 | static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw) | |
472 | { | |
473 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
474 | struct rtl_phy *rtlphy = &(rtlpriv->phy); | |
475 | u8 hw_rate; | |
476 | ||
477 | if (get_rf_type(rtlphy) == RF_2T2R) | |
478 | hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15]; | |
479 | else | |
480 | hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7]; | |
481 | ||
482 | return hw_rate; | |
483 | } | |
484 | ||
485 | void rtl_get_tcb_desc(struct ieee80211_hw *hw, | |
486 | struct ieee80211_tx_info *info, | |
487 | struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc) | |
488 | { | |
489 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
490 | struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw)); | |
491 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data); | |
492 | struct ieee80211_rate *txrate; | |
17c9ac62 | 493 | __le16 fc = hdr->frame_control; |
0c817338 LF |
494 | |
495 | memset(tcb_desc, 0, sizeof(struct rtl_tcb_desc)); | |
496 | ||
497 | if (ieee80211_is_data(fc)) { | |
498 | txrate = ieee80211_get_tx_rate(hw, info); | |
499 | tcb_desc->hw_rate = txrate->hw_value; | |
500 | ||
501 | /* | |
502 | *we set data rate RTL_RC_CCK_RATE1M | |
503 | *in rtl_rc.c if skb is special data or | |
504 | *mgt which need low data rate. | |
505 | */ | |
506 | ||
507 | /* | |
508 | *So tcb_desc->hw_rate is just used for | |
509 | *special data and mgt frames | |
510 | */ | |
511 | if (tcb_desc->hw_rate < rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M]) { | |
512 | tcb_desc->use_driver_rate = true; | |
513 | tcb_desc->ratr_index = 7; | |
514 | ||
515 | tcb_desc->hw_rate = | |
516 | rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M]; | |
517 | tcb_desc->disable_ratefallback = 1; | |
518 | } else { | |
519 | /* | |
520 | *because hw will nerver use hw_rate | |
521 | *when tcb_desc->use_driver_rate = false | |
522 | *so we never set highest N rate here, | |
523 | *and N rate will all be controled by FW | |
524 | *when tcb_desc->use_driver_rate = false | |
525 | */ | |
526 | if (rtlmac->ht_enable) { | |
527 | tcb_desc->hw_rate = _rtl_get_highest_n_rate(hw); | |
528 | } else { | |
529 | if (rtlmac->mode == WIRELESS_MODE_B) { | |
530 | tcb_desc->hw_rate = | |
531 | rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M]; | |
532 | } else { | |
533 | tcb_desc->hw_rate = | |
534 | rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M]; | |
535 | } | |
536 | } | |
537 | } | |
538 | ||
539 | if (is_multicast_ether_addr(ieee80211_get_DA(hdr))) | |
7ea47240 | 540 | tcb_desc->multicast = 1; |
0c817338 | 541 | else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr))) |
7ea47240 | 542 | tcb_desc->broadcast = 1; |
0c817338 LF |
543 | |
544 | _rtl_txrate_selectmode(hw, tcb_desc); | |
545 | _rtl_query_bandwidth_mode(hw, tcb_desc); | |
546 | _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info); | |
547 | _rtl_query_shortgi(hw, tcb_desc, info); | |
548 | _rtl_query_protection_mode(hw, tcb_desc, info); | |
549 | } else { | |
550 | tcb_desc->use_driver_rate = true; | |
551 | tcb_desc->ratr_index = 7; | |
552 | tcb_desc->disable_ratefallback = 1; | |
553 | tcb_desc->mac_id = 0; | |
554 | ||
555 | tcb_desc->hw_rate = rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M]; | |
556 | } | |
557 | } | |
558 | EXPORT_SYMBOL(rtl_get_tcb_desc); | |
559 | ||
560 | bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb) | |
561 | { | |
562 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); | |
563 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
564 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data); | |
17c9ac62 | 565 | __le16 fc = hdr->frame_control; |
0c817338 LF |
566 | |
567 | if (ieee80211_is_auth(fc)) { | |
568 | RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, ("MAC80211_LINKING\n")); | |
569 | rtl_ips_nic_on(hw); | |
570 | ||
571 | mac->link_state = MAC80211_LINKING; | |
572 | } | |
573 | ||
574 | return true; | |
575 | } | |
576 | ||
577 | bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx) | |
578 | { | |
579 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); | |
580 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data); | |
581 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
17c9ac62 | 582 | __le16 fc = hdr->frame_control; |
0c817338 LF |
583 | u8 *act = (u8 *) (((u8 *) skb->data + MAC80211_3ADDR_LEN)); |
584 | u8 category; | |
585 | ||
586 | if (!ieee80211_is_action(fc)) | |
587 | return true; | |
588 | ||
589 | category = *act; | |
590 | act++; | |
591 | switch (category) { | |
592 | case ACT_CAT_BA: | |
593 | switch (*act) { | |
594 | case ACT_ADDBAREQ: | |
595 | if (mac->act_scanning) | |
596 | return false; | |
597 | ||
598 | RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG, | |
599 | ("%s ACT_ADDBAREQ From :" MAC_FMT "\n", | |
600 | is_tx ? "Tx" : "Rx", MAC_ARG(hdr->addr2))); | |
601 | break; | |
602 | case ACT_ADDBARSP: | |
603 | RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG, | |
604 | ("%s ACT_ADDBARSP From :" MAC_FMT "\n", | |
605 | is_tx ? "Tx" : "Rx", MAC_ARG(hdr->addr2))); | |
606 | break; | |
607 | case ACT_DELBA: | |
608 | RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG, | |
609 | ("ACT_ADDBADEL From :" MAC_FMT "\n", | |
610 | MAC_ARG(hdr->addr2))); | |
611 | break; | |
612 | } | |
613 | break; | |
614 | default: | |
615 | break; | |
616 | } | |
617 | ||
618 | return true; | |
619 | } | |
620 | ||
621 | /*should call before software enc*/ | |
622 | u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx) | |
623 | { | |
624 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
625 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data); | |
626 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); | |
17c9ac62 | 627 | __le16 fc = hdr->frame_control; |
0c817338 LF |
628 | u16 ether_type; |
629 | u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb); | |
630 | const struct iphdr *ip; | |
631 | ||
632 | if (!ieee80211_is_data(fc)) | |
32473284 | 633 | return false; |
0c817338 LF |
634 | |
635 | if (ieee80211_is_nullfunc(fc)) | |
636 | return true; | |
637 | ||
638 | ip = (struct iphdr *)((u8 *) skb->data + mac_hdr_len + | |
639 | SNAP_SIZE + PROTOC_TYPE_SIZE); | |
640 | ether_type = *(u16 *) ((u8 *) skb->data + mac_hdr_len + SNAP_SIZE); | |
0c817338 LF |
641 | |
642 | if (ETH_P_IP == ether_type) { | |
643 | if (IPPROTO_UDP == ip->protocol) { | |
644 | struct udphdr *udp = (struct udphdr *)((u8 *) ip + | |
645 | (ip->ihl << 2)); | |
646 | if (((((u8 *) udp)[1] == 68) && | |
647 | (((u8 *) udp)[3] == 67)) || | |
648 | ((((u8 *) udp)[1] == 67) && | |
649 | (((u8 *) udp)[3] == 68))) { | |
650 | /* | |
651 | * 68 : UDP BOOTP client | |
652 | * 67 : UDP BOOTP server | |
653 | */ | |
654 | RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), | |
655 | DBG_DMESG, ("dhcp %s !!\n", | |
656 | (is_tx) ? "Tx" : "Rx")); | |
657 | ||
658 | if (is_tx) { | |
659 | rtl_lps_leave(hw); | |
660 | ppsc->last_delaylps_stamp_jiffies = | |
661 | jiffies; | |
662 | } | |
663 | ||
664 | return true; | |
665 | } | |
666 | } | |
667 | } else if (ETH_P_ARP == ether_type) { | |
668 | if (is_tx) { | |
669 | rtl_lps_leave(hw); | |
670 | ppsc->last_delaylps_stamp_jiffies = jiffies; | |
671 | } | |
672 | ||
673 | return true; | |
674 | } else if (ETH_P_PAE == ether_type) { | |
675 | RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG, | |
676 | ("802.1X %s EAPOL pkt!!\n", (is_tx) ? "Tx" : "Rx")); | |
677 | ||
678 | if (is_tx) { | |
679 | rtl_lps_leave(hw); | |
680 | ppsc->last_delaylps_stamp_jiffies = jiffies; | |
681 | } | |
682 | ||
683 | return true; | |
17c9ac62 LF |
684 | } else if (ETH_P_IPV6 == ether_type) { |
685 | /* IPv6 */ | |
0c817338 LF |
686 | return true; |
687 | } | |
688 | ||
0c817338 LF |
689 | return false; |
690 | } | |
691 | ||
692 | /********************************************************* | |
693 | * | |
694 | * functions called by core.c | |
695 | * | |
696 | *********************************************************/ | |
697 | int rtl_tx_agg_start(struct ieee80211_hw *hw, const u8 *ra, u16 tid, u16 *ssn) | |
698 | { | |
699 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
700 | struct rtl_tid_data *tid_data; | |
701 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); | |
702 | ||
703 | RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, | |
704 | ("on ra = %pM tid = %d\n", ra, tid)); | |
705 | ||
706 | if (unlikely(tid >= MAX_TID_COUNT)) | |
707 | return -EINVAL; | |
708 | ||
709 | if (mac->tids[tid].agg.agg_state != RTL_AGG_OFF) { | |
710 | RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, | |
711 | ("Start AGG when state is not RTL_AGG_OFF !\n")); | |
712 | return -ENXIO; | |
713 | } | |
714 | ||
715 | tid_data = &mac->tids[tid]; | |
716 | *ssn = SEQ_TO_SN(tid_data->seq_number); | |
717 | ||
718 | RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, | |
719 | ("HW queue is empty tid:%d\n", tid)); | |
720 | tid_data->agg.agg_state = RTL_AGG_ON; | |
721 | ||
722 | ieee80211_start_tx_ba_cb_irqsafe(mac->vif, ra, tid); | |
723 | ||
724 | return 0; | |
725 | } | |
726 | ||
727 | int rtl_tx_agg_stop(struct ieee80211_hw *hw, const u8 * ra, u16 tid) | |
728 | { | |
729 | int ssn = -1; | |
730 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
731 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); | |
732 | struct rtl_tid_data *tid_data; | |
733 | ||
734 | if (!ra) { | |
735 | RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, ("ra = NULL\n")); | |
736 | return -EINVAL; | |
737 | } | |
738 | ||
739 | if (unlikely(tid >= MAX_TID_COUNT)) | |
740 | return -EINVAL; | |
741 | ||
742 | if (mac->tids[tid].agg.agg_state != RTL_AGG_ON) | |
743 | RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, | |
744 | ("Stopping AGG while state not ON or starting\n")); | |
745 | ||
746 | tid_data = &mac->tids[tid]; | |
747 | ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4; | |
748 | ||
749 | mac->tids[tid].agg.agg_state = RTL_AGG_OFF; | |
750 | ||
751 | ieee80211_stop_tx_ba_cb_irqsafe(mac->vif, ra, tid); | |
752 | ||
753 | return 0; | |
754 | } | |
755 | ||
756 | /********************************************************* | |
757 | * | |
758 | * wq & timer callback functions | |
759 | * | |
760 | *********************************************************/ | |
761 | void rtl_watchdog_wq_callback(void *data) | |
762 | { | |
763 | struct rtl_works *rtlworks = container_of_dwork_rtl(data, | |
764 | struct rtl_works, | |
765 | watchdog_wq); | |
766 | struct ieee80211_hw *hw = rtlworks->hw; | |
767 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
768 | struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); | |
769 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); | |
770 | ||
7ea47240 LF |
771 | bool busytraffic = false; |
772 | bool higher_busytraffic = false; | |
773 | bool higher_busyrxtraffic = false; | |
774 | bool higher_busytxtraffic = false; | |
0c817338 LF |
775 | |
776 | u8 idx = 0; | |
777 | u32 rx_cnt_inp4eriod = 0; | |
778 | u32 tx_cnt_inp4eriod = 0; | |
779 | u32 aver_rx_cnt_inperiod = 0; | |
780 | u32 aver_tx_cnt_inperiod = 0; | |
781 | ||
7ea47240 | 782 | bool enter_ps = false; |
0c817338 LF |
783 | |
784 | if (is_hal_stop(rtlhal)) | |
785 | return; | |
786 | ||
787 | /* <1> Determine if action frame is allowed */ | |
788 | if (mac->link_state > MAC80211_NOLINK) { | |
789 | if (mac->cnt_after_linked < 20) | |
790 | mac->cnt_after_linked++; | |
791 | } else { | |
792 | mac->cnt_after_linked = 0; | |
793 | } | |
794 | ||
795 | /* <2> DM */ | |
796 | rtlpriv->cfg->ops->dm_watchdog(hw); | |
797 | ||
798 | /* | |
799 | *<3> to check if traffic busy, if | |
800 | * busytraffic we don't change channel | |
801 | */ | |
802 | if (mac->link_state >= MAC80211_LINKED) { | |
803 | ||
804 | /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */ | |
805 | for (idx = 0; idx <= 2; idx++) { | |
806 | rtlpriv->link_info.num_rx_in4period[idx] = | |
807 | rtlpriv->link_info.num_rx_in4period[idx + 1]; | |
808 | rtlpriv->link_info.num_tx_in4period[idx] = | |
809 | rtlpriv->link_info.num_tx_in4period[idx + 1]; | |
810 | } | |
811 | rtlpriv->link_info.num_rx_in4period[3] = | |
812 | rtlpriv->link_info.num_rx_inperiod; | |
813 | rtlpriv->link_info.num_tx_in4period[3] = | |
814 | rtlpriv->link_info.num_tx_inperiod; | |
815 | for (idx = 0; idx <= 3; idx++) { | |
816 | rx_cnt_inp4eriod += | |
817 | rtlpriv->link_info.num_rx_in4period[idx]; | |
818 | tx_cnt_inp4eriod += | |
819 | rtlpriv->link_info.num_tx_in4period[idx]; | |
820 | } | |
821 | aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4; | |
822 | aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4; | |
823 | ||
824 | /* (2) check traffic busy */ | |
825 | if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100) | |
7ea47240 | 826 | busytraffic = true; |
0c817338 LF |
827 | |
828 | /* Higher Tx/Rx data. */ | |
829 | if (aver_rx_cnt_inperiod > 4000 || | |
830 | aver_tx_cnt_inperiod > 4000) { | |
7ea47240 | 831 | higher_busytraffic = true; |
0c817338 LF |
832 | |
833 | /* Extremely high Rx data. */ | |
834 | if (aver_rx_cnt_inperiod > 5000) | |
7ea47240 | 835 | higher_busyrxtraffic = true; |
0c817338 | 836 | else |
7ea47240 | 837 | higher_busytxtraffic = false; |
0c817338 LF |
838 | } |
839 | ||
840 | if (((rtlpriv->link_info.num_rx_inperiod + | |
841 | rtlpriv->link_info.num_tx_inperiod) > 8) || | |
842 | (rtlpriv->link_info.num_rx_inperiod > 2)) | |
7ea47240 | 843 | enter_ps = false; |
0c817338 | 844 | else |
7ea47240 | 845 | enter_ps = true; |
0c817338 LF |
846 | |
847 | /* LeisurePS only work in infra mode. */ | |
7ea47240 | 848 | if (enter_ps) |
0c817338 LF |
849 | rtl_lps_enter(hw); |
850 | else | |
851 | rtl_lps_leave(hw); | |
852 | } | |
853 | ||
854 | rtlpriv->link_info.num_rx_inperiod = 0; | |
855 | rtlpriv->link_info.num_tx_inperiod = 0; | |
856 | ||
7ea47240 LF |
857 | rtlpriv->link_info.busytraffic = busytraffic; |
858 | rtlpriv->link_info.higher_busytraffic = higher_busytraffic; | |
859 | rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic; | |
0c817338 LF |
860 | |
861 | } | |
862 | ||
863 | void rtl_watch_dog_timer_callback(unsigned long data) | |
864 | { | |
865 | struct ieee80211_hw *hw = (struct ieee80211_hw *)data; | |
866 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
867 | ||
868 | queue_delayed_work(rtlpriv->works.rtl_wq, | |
869 | &rtlpriv->works.watchdog_wq, 0); | |
870 | ||
871 | mod_timer(&rtlpriv->works.watchdog_timer, | |
872 | jiffies + MSECS(RTL_WATCH_DOG_TIME)); | |
873 | } | |
874 | ||
875 | /********************************************************* | |
876 | * | |
877 | * sysfs functions | |
878 | * | |
879 | *********************************************************/ | |
880 | static ssize_t rtl_show_debug_level(struct device *d, | |
881 | struct device_attribute *attr, char *buf) | |
882 | { | |
883 | struct ieee80211_hw *hw = dev_get_drvdata(d); | |
884 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
885 | ||
886 | return sprintf(buf, "0x%08X\n", rtlpriv->dbg.global_debuglevel); | |
887 | } | |
888 | ||
889 | static ssize_t rtl_store_debug_level(struct device *d, | |
890 | struct device_attribute *attr, | |
891 | const char *buf, size_t count) | |
892 | { | |
893 | struct ieee80211_hw *hw = dev_get_drvdata(d); | |
894 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
895 | unsigned long val; | |
896 | int ret; | |
897 | ||
898 | ret = strict_strtoul(buf, 0, &val); | |
899 | if (ret) { | |
900 | printk(KERN_DEBUG "%s is not in hex or decimal form.\n", buf); | |
901 | } else { | |
902 | rtlpriv->dbg.global_debuglevel = val; | |
903 | printk(KERN_DEBUG "debuglevel:%x\n", | |
904 | rtlpriv->dbg.global_debuglevel); | |
905 | } | |
906 | ||
907 | return strnlen(buf, count); | |
908 | } | |
909 | ||
910 | static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO, | |
911 | rtl_show_debug_level, rtl_store_debug_level); | |
912 | ||
913 | static struct attribute *rtl_sysfs_entries[] = { | |
914 | ||
915 | &dev_attr_debug_level.attr, | |
916 | ||
917 | NULL | |
918 | }; | |
919 | ||
920 | /* | |
921 | * "name" is folder name witch will be | |
922 | * put in device directory like : | |
923 | * sys/devices/pci0000:00/0000:00:1c.4/ | |
924 | * 0000:06:00.0/rtl_sysfs | |
925 | */ | |
926 | struct attribute_group rtl_attribute_group = { | |
927 | .name = "rtlsysfs", | |
928 | .attrs = rtl_sysfs_entries, | |
929 | }; | |
930 | ||
931 | MODULE_AUTHOR("lizhaoming <chaoming_li@realsil.com.cn>"); | |
932 | MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>"); | |
933 | MODULE_AUTHOR("Larry Finger <Larry.FInger@lwfinger.net>"); | |
934 | MODULE_LICENSE("GPL"); | |
935 | MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core"); | |
936 | ||
937 | static int __init rtl_core_module_init(void) | |
938 | { | |
375ff4c7 CL |
939 | if (rtl_rate_control_register()) |
940 | printk(KERN_ERR "rtlwifi: Unable to register rtl_rc," | |
941 | "use default RC !!\n"); | |
0c817338 LF |
942 | return 0; |
943 | } | |
944 | ||
945 | static void __exit rtl_core_module_exit(void) | |
946 | { | |
375ff4c7 CL |
947 | /*RC*/ |
948 | rtl_rate_control_unregister(); | |
0c817338 LF |
949 | } |
950 | ||
951 | module_init(rtl_core_module_init); | |
952 | module_exit(rtl_core_module_exit); |