blob: b1757930aa64edfb3fd6d9ad8e2c685547fd9a49 [file] [log] [blame]
Larry Finger0c817332010-12-08 11:12:31 -06001/******************************************************************************
2 *
Larry Fingera8d76062012-01-07 20:46:42 -06003 * Copyright(c) 2009-2012 Realtek Corporation.
Larry Finger0c817332010-12-08 11:12:31 -06004 *
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
Larry Finger0c817332010-12-08 11:12:31 -060030#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
Larry Fingerd273bb22012-01-27 13:59:25 -060038#include <linux/ip.h>
39#include <linux/module.h>
Mark Cave-Ayland172ba812014-02-27 09:53:03 +080040#include <linux/udp.h>
Larry Fingerd273bb22012-01-27 13:59:25 -060041
Larry Finger0c817332010-12-08 11:12:31 -060042/*
Tristan Pourcelote1fa7462012-02-09 14:48:09 +010043 *NOTICE!!!: This file will be very big, we should
44 *keep it clear under following roles:
Larry Finger0c817332010-12-08 11:12:31 -060045 *
Tristan Pourcelote1fa7462012-02-09 14:48:09 +010046 *This file include following parts, so, if you add new
Larry Finger0c817332010-12-08 11:12:31 -060047 *functions into this file, please check which part it
48 *should includes. or check if you should add new part
49 *for this file:
50 *
51 *1) mac80211 init functions
52 *2) tx information functions
53 *3) functions called by core.c
54 *4) wq & timer callback functions
55 *5) frame process functions
Chaoming_Liacd48572011-04-25 12:52:54 -050056 *6) IOT functions
57 *7) sysfs functions
58 *8) ...
Larry Finger0c817332010-12-08 11:12:31 -060059 */
60
61/*********************************************************
62 *
63 * mac80211 init functions
64 *
65 *********************************************************/
Chaoming_Liacd48572011-04-25 12:52:54 -050066static struct ieee80211_channel rtl_channeltable_2g[] = {
Larry Finger0c817332010-12-08 11:12:31 -060067 {.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
Chaoming_Liacd48572011-04-25 12:52:54 -050083static 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
110static struct ieee80211_rate rtl_ratetable_2g[] = {
Larry Finger0c817332010-12-08 11:12:31 -0600111 {.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
Chaoming_Liacd48572011-04-25 12:52:54 -0500125static 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
Larry Finger0c817332010-12-08 11:12:31 -0600136static const struct ieee80211_supported_band rtl_band_2ghz = {
137 .band = IEEE80211_BAND_2GHZ,
138
Chaoming_Liacd48572011-04-25 12:52:54 -0500139 .channels = rtl_channeltable_2g,
140 .n_channels = ARRAY_SIZE(rtl_channeltable_2g),
Larry Finger0c817332010-12-08 11:12:31 -0600141
Chaoming_Liacd48572011-04-25 12:52:54 -0500142 .bitrates = rtl_ratetable_2g,
143 .n_bitrates = ARRAY_SIZE(rtl_ratetable_2g),
Larry Finger0c817332010-12-08 11:12:31 -0600144
145 .ht_cap = {0},
146};
147
Chaoming_Liacd48572011-04-25 12:52:54 -0500148static 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
160static 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
171u8 rtl_tid_to_ac(struct ieee80211_hw *hw, u8 tid)
172{
173 return tid_to_ac[tid];
174}
175
Larry Finger0c817332010-12-08 11:12:31 -0600176static 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
Chaoming_Liacd48572011-04-25 12:52:54 -0500188 if (rtlpriv->rtlhal.disable_amsdu_8k)
189 ht_cap->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
190
Larry Finger0c817332010-12-08 11:12:31 -0600191 /*
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 /*
203 *hw->wiphy->bands[IEEE80211_BAND_2GHZ]
204 *base on ant_num
205 *rx_mask: RX mask
206 *if rx_ant =1 rx_mask[0]=0xff;==>MCS0-MCS7
207 *if rx_ant =2 rx_mask[1]=0xff;==>MCS8-MCS15
208 *if rx_ant >=3 rx_mask[2]=0xff;
209 *if BW_40 rx_mask[4]=0x01;
210 *highest supported RX rate
211 */
212 if (get_rf_type(rtlphy) == RF_1T2R || get_rf_type(rtlphy) == RF_2T2R) {
213
Joe Perchesf30d7502012-01-04 19:40:41 -0800214 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "1T2R or 2T2R\n");
Larry Finger0c817332010-12-08 11:12:31 -0600215
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
Larry Finger17c9ac62011-02-19 16:29:57 -0600220 ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
Larry Finger0c817332010-12-08 11:12:31 -0600221 } else if (get_rf_type(rtlphy) == RF_1T1R) {
222
Joe Perchesf30d7502012-01-04 19:40:41 -0800223 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "1T1R\n");
Larry Finger0c817332010-12-08 11:12:31 -0600224
225 ht_cap->mcs.rx_mask[0] = 0xFF;
226 ht_cap->mcs.rx_mask[1] = 0x00;
227 ht_cap->mcs.rx_mask[4] = 0x01;
228
Larry Finger17c9ac62011-02-19 16:29:57 -0600229 ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
Larry Finger0c817332010-12-08 11:12:31 -0600230 }
231}
232
233static void _rtl_init_mac80211(struct ieee80211_hw *hw)
234{
Chaoming_Liacd48572011-04-25 12:52:54 -0500235 struct rtl_priv *rtlpriv = rtl_priv(hw);
236 struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
Larry Finger0c817332010-12-08 11:12:31 -0600237 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
238 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
239 struct ieee80211_supported_band *sband;
240
Larry Finger0c817332010-12-08 11:12:31 -0600241
Chaoming_Liacd48572011-04-25 12:52:54 -0500242 if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY && rtlhal->bandset ==
243 BAND_ON_BOTH) {
244 /* 1: 2.4 G bands */
245 /* <1> use mac->bands as mem for hw->wiphy->bands */
246 sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
Larry Finger0c817332010-12-08 11:12:31 -0600247
Chaoming_Liacd48572011-04-25 12:52:54 -0500248 /* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
249 * to default value(1T1R) */
250 memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]), &rtl_band_2ghz,
251 sizeof(struct ieee80211_supported_band));
Larry Finger0c817332010-12-08 11:12:31 -0600252
Chaoming_Liacd48572011-04-25 12:52:54 -0500253 /* <3> init ht cap base on ant_num */
254 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
Larry Finger0c817332010-12-08 11:12:31 -0600255
Chaoming_Liacd48572011-04-25 12:52:54 -0500256 /* <4> set mac->sband to wiphy->sband */
257 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
258
259 /* 2: 5 G bands */
260 /* <1> use mac->bands as mem for hw->wiphy->bands */
261 sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
262
263 /* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
264 * to default value(1T1R) */
265 memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]), &rtl_band_5ghz,
266 sizeof(struct ieee80211_supported_band));
267
268 /* <3> init ht cap base on ant_num */
269 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
270
271 /* <4> set mac->sband to wiphy->sband */
272 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
273 } else {
274 if (rtlhal->current_bandtype == BAND_ON_2_4G) {
275 /* <1> use mac->bands as mem for hw->wiphy->bands */
276 sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
277
278 /* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
279 * to default value(1T1R) */
280 memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]),
281 &rtl_band_2ghz,
282 sizeof(struct ieee80211_supported_band));
283
284 /* <3> init ht cap base on ant_num */
285 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
286
287 /* <4> set mac->sband to wiphy->sband */
288 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
289 } else if (rtlhal->current_bandtype == BAND_ON_5G) {
290 /* <1> use mac->bands as mem for hw->wiphy->bands */
291 sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
292
293 /* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
294 * to default value(1T1R) */
295 memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]),
296 &rtl_band_5ghz,
297 sizeof(struct ieee80211_supported_band));
298
299 /* <3> init ht cap base on ant_num */
300 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
301
302 /* <4> set mac->sband to wiphy->sband */
303 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
304 } else {
Joe Perchesf30d7502012-01-04 19:40:41 -0800305 RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG, "Err BAND %d\n",
306 rtlhal->current_bandtype);
Chaoming_Liacd48572011-04-25 12:52:54 -0500307 }
308 }
Larry Finger0c817332010-12-08 11:12:31 -0600309 /* <5> set hw caps */
310 hw->flags = IEEE80211_HW_SIGNAL_DBM |
311 IEEE80211_HW_RX_INCLUDES_FCS |
Chaoming_Liacd48572011-04-25 12:52:54 -0500312 IEEE80211_HW_AMPDU_AGGREGATION |
Chaoming Li09e92f02011-10-11 21:28:48 -0500313 IEEE80211_HW_CONNECTION_MONITOR |
314 /* IEEE80211_HW_SUPPORTS_CQM_RSSI | */
Larry Finger0c817332010-12-08 11:12:31 -0600315 IEEE80211_HW_REPORTS_TX_ACK_STATUS | 0;
316
Chaoming_Liacd48572011-04-25 12:52:54 -0500317 /* swlps or hwlps has been set in diff chip in init_sw_vars */
318 if (rtlpriv->psc.swctrl_lps)
319 hw->flags |= IEEE80211_HW_SUPPORTS_PS |
320 IEEE80211_HW_PS_NULLFUNC_STACK |
321 /* IEEE80211_HW_SUPPORTS_DYNAMIC_PS | */
322 0;
323
Larry Finger0c817332010-12-08 11:12:31 -0600324 hw->wiphy->interface_modes =
Chaoming_Liacd48572011-04-25 12:52:54 -0500325 BIT(NL80211_IFTYPE_AP) |
326 BIT(NL80211_IFTYPE_STATION) |
327 BIT(NL80211_IFTYPE_ADHOC);
Larry Finger0c817332010-12-08 11:12:31 -0600328
329 hw->wiphy->rts_threshold = 2347;
330
331 hw->queues = AC_MAX;
332 hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;
333
334 /* TODO: Correct this value for our hw */
335 /* TODO: define these hard code value */
336 hw->channel_change_time = 100;
Chaoming_Liacd48572011-04-25 12:52:54 -0500337 hw->max_listen_interval = 10;
Larry Finger0c817332010-12-08 11:12:31 -0600338 hw->max_rate_tries = 4;
339 /* hw->max_rates = 1; */
Chaoming_Liacd48572011-04-25 12:52:54 -0500340 hw->sta_data_size = sizeof(struct rtl_sta_info);
Larry Finger0c817332010-12-08 11:12:31 -0600341
342 /* <6> mac address */
343 if (is_valid_ether_addr(rtlefuse->dev_addr)) {
344 SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
345 } else {
Larry Fingerabfabc92011-11-17 12:14:44 -0600346 u8 rtlmac1[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
347 get_random_bytes((rtlmac1 + (ETH_ALEN - 1)), 1);
348 SET_IEEE80211_PERM_ADDR(hw, rtlmac1);
Larry Finger0c817332010-12-08 11:12:31 -0600349 }
350
351}
352
353static void _rtl_init_deferred_work(struct ieee80211_hw *hw)
354{
355 struct rtl_priv *rtlpriv = rtl_priv(hw);
356
357 /* <1> timer */
358 init_timer(&rtlpriv->works.watchdog_timer);
359 setup_timer(&rtlpriv->works.watchdog_timer,
360 rtl_watch_dog_timer_callback, (unsigned long)hw);
361
362 /* <2> work queue */
363 rtlpriv->works.hw = hw;
John W. Linville3d986b22010-12-16 14:59:49 -0500364 rtlpriv->works.rtl_wq = alloc_workqueue(rtlpriv->cfg->name, 0, 0);
Larry Finger0c817332010-12-08 11:12:31 -0600365 INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
366 (void *)rtl_watchdog_wq_callback);
367 INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
368 (void *)rtl_ips_nic_off_wq_callback);
Chaoming_Liacd48572011-04-25 12:52:54 -0500369 INIT_DELAYED_WORK(&rtlpriv->works.ps_work,
370 (void *)rtl_swlps_wq_callback);
371 INIT_DELAYED_WORK(&rtlpriv->works.ps_rfon_wq,
372 (void *)rtl_swlps_rfon_wq_callback);
Larry Finger0c817332010-12-08 11:12:31 -0600373
374}
375
376void rtl_deinit_deferred_work(struct ieee80211_hw *hw)
377{
378 struct rtl_priv *rtlpriv = rtl_priv(hw);
379
380 del_timer_sync(&rtlpriv->works.watchdog_timer);
381
382 cancel_delayed_work(&rtlpriv->works.watchdog_wq);
383 cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
Chaoming_Liacd48572011-04-25 12:52:54 -0500384 cancel_delayed_work(&rtlpriv->works.ps_work);
385 cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
Larry Finger0c817332010-12-08 11:12:31 -0600386}
387
388void rtl_init_rfkill(struct ieee80211_hw *hw)
389{
390 struct rtl_priv *rtlpriv = rtl_priv(hw);
391
392 bool radio_state;
393 bool blocked;
394 u8 valid = 0;
395
Chaoming Li228bdfc2011-04-10 18:30:23 -0500396 /*set init state to on */
Rusty Russell3db1cd52011-12-19 13:56:45 +0000397 rtlpriv->rfkill.rfkill_state = true;
Chaoming Li228bdfc2011-04-10 18:30:23 -0500398 wiphy_rfkill_set_hw_state(hw->wiphy, 0);
399
Larry Finger0c817332010-12-08 11:12:31 -0600400 radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
401
Larry Fingere6d8a812010-12-21 19:40:40 -0600402 if (valid) {
Joe Perches292b1192011-07-20 08:51:35 -0700403 pr_info("wireless switch is %s\n",
404 rtlpriv->rfkill.rfkill_state ? "on" : "off");
Chaoming Li228bdfc2011-04-10 18:30:23 -0500405
Larry Finger0c817332010-12-08 11:12:31 -0600406 rtlpriv->rfkill.rfkill_state = radio_state;
407
408 blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
409 wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
410 }
411
412 wiphy_rfkill_start_polling(hw->wiphy);
413}
Larry Fingerb0302ab2012-01-30 09:54:49 -0600414EXPORT_SYMBOL(rtl_init_rfkill);
Larry Finger0c817332010-12-08 11:12:31 -0600415
416void rtl_deinit_rfkill(struct ieee80211_hw *hw)
417{
418 wiphy_rfkill_stop_polling(hw->wiphy);
419}
420
421int rtl_init_core(struct ieee80211_hw *hw)
422{
423 struct rtl_priv *rtlpriv = rtl_priv(hw);
424 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
425
426 /* <1> init mac80211 */
427 _rtl_init_mac80211(hw);
428 rtlmac->hw = hw;
429
430 /* <2> rate control register */
Chaoming Li375ff4c2011-02-28 16:40:28 -0600431 hw->rate_control_algorithm = "rtl_rc";
Larry Finger0c817332010-12-08 11:12:31 -0600432
433 /*
434 * <3> init CRDA must come after init
435 * mac80211 hw in _rtl_init_mac80211.
436 */
437 if (rtl_regd_init(hw, rtl_reg_notifier)) {
Joe Perchesf30d7502012-01-04 19:40:41 -0800438 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "REGD init failed\n");
Larry Finger0c817332010-12-08 11:12:31 -0600439 return 1;
440 } else {
441 /* CRDA regd hint must after init CRDA */
442 if (regulatory_hint(hw->wiphy, rtlpriv->regd.alpha2)) {
443 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
Joe Perchesf30d7502012-01-04 19:40:41 -0800444 "regulatory_hint fail\n");
Larry Finger0c817332010-12-08 11:12:31 -0600445 }
446 }
447
448 /* <4> locks */
Larry Finger8a09d6d2010-12-16 11:13:57 -0600449 mutex_init(&rtlpriv->locks.conf_mutex);
Stanislaw Gruszka65393062011-12-12 12:43:24 +0100450 mutex_init(&rtlpriv->locks.ps_mutex);
Larry Fingerb9116b9a2011-12-16 21:17:16 -0600451 spin_lock_init(&rtlpriv->locks.ips_lock);
Larry Finger0c817332010-12-08 11:12:31 -0600452 spin_lock_init(&rtlpriv->locks.irq_th_lock);
453 spin_lock_init(&rtlpriv->locks.h2c_lock);
454 spin_lock_init(&rtlpriv->locks.rf_ps_lock);
455 spin_lock_init(&rtlpriv->locks.rf_lock);
Chaoming_Liacd48572011-04-25 12:52:54 -0500456 spin_lock_init(&rtlpriv->locks.waitq_lock);
457 spin_lock_init(&rtlpriv->locks.cck_and_rw_pagea_lock);
Larry Finger0c817332010-12-08 11:12:31 -0600458
459 rtlmac->link_state = MAC80211_NOLINK;
460
461 /* <5> init deferred work */
462 _rtl_init_deferred_work(hw);
463
464 return 0;
465}
466
467void rtl_deinit_core(struct ieee80211_hw *hw)
468{
Larry Finger0c817332010-12-08 11:12:31 -0600469}
470
471void rtl_init_rx_config(struct ieee80211_hw *hw)
472{
473 struct rtl_priv *rtlpriv = rtl_priv(hw);
474 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
475
476 rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
Larry Finger0c817332010-12-08 11:12:31 -0600477}
478
479/*********************************************************
480 *
481 * tx information functions
482 *
483 *********************************************************/
484static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
485 struct rtl_tcb_desc *tcb_desc,
486 struct ieee80211_tx_info *info)
487{
488 struct rtl_priv *rtlpriv = rtl_priv(hw);
489 u8 rate_flag = info->control.rates[0].flags;
490
491 tcb_desc->use_shortpreamble = false;
492
493 /* 1M can only use Long Preamble. 11B spec */
494 if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
495 return;
496 else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
497 tcb_desc->use_shortpreamble = true;
498
499 return;
500}
501
502static void _rtl_query_shortgi(struct ieee80211_hw *hw,
Chaoming_Liacd48572011-04-25 12:52:54 -0500503 struct ieee80211_sta *sta,
Larry Finger0c817332010-12-08 11:12:31 -0600504 struct rtl_tcb_desc *tcb_desc,
505 struct ieee80211_tx_info *info)
506{
507 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
508 u8 rate_flag = info->control.rates[0].flags;
Chaoming_Liacd48572011-04-25 12:52:54 -0500509 u8 sgi_40 = 0, sgi_20 = 0, bw_40 = 0;
Larry Finger0c817332010-12-08 11:12:31 -0600510 tcb_desc->use_shortgi = false;
511
Chaoming_Liacd48572011-04-25 12:52:54 -0500512 if (sta == NULL)
Larry Finger0c817332010-12-08 11:12:31 -0600513 return;
514
Chaoming_Liacd48572011-04-25 12:52:54 -0500515 sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
516 sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
517
518 if (!(sta->ht_cap.ht_supported))
Larry Finger0c817332010-12-08 11:12:31 -0600519 return;
520
Chaoming_Liacd48572011-04-25 12:52:54 -0500521 if (!sgi_40 && !sgi_20)
522 return;
523
524 if (mac->opmode == NL80211_IFTYPE_STATION)
525 bw_40 = mac->bw_40;
526 else if (mac->opmode == NL80211_IFTYPE_AP ||
527 mac->opmode == NL80211_IFTYPE_ADHOC)
528 bw_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
529
Mike McCormacke10542c2011-06-20 10:47:51 +0900530 if (bw_40 && sgi_40)
Larry Finger0c817332010-12-08 11:12:31 -0600531 tcb_desc->use_shortgi = true;
Chaoming_Liacd48572011-04-25 12:52:54 -0500532 else if ((bw_40 == false) && sgi_20)
Larry Finger0c817332010-12-08 11:12:31 -0600533 tcb_desc->use_shortgi = true;
534
535 if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
536 tcb_desc->use_shortgi = false;
Larry Finger0c817332010-12-08 11:12:31 -0600537}
538
539static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
540 struct rtl_tcb_desc *tcb_desc,
541 struct ieee80211_tx_info *info)
542{
543 struct rtl_priv *rtlpriv = rtl_priv(hw);
544 u8 rate_flag = info->control.rates[0].flags;
545
546 /* Common Settings */
Larry Finger7ea47242011-02-19 16:28:57 -0600547 tcb_desc->rts_stbc = false;
548 tcb_desc->cts_enable = false;
Larry Finger0c817332010-12-08 11:12:31 -0600549 tcb_desc->rts_sc = 0;
Larry Finger7ea47242011-02-19 16:28:57 -0600550 tcb_desc->rts_bw = false;
551 tcb_desc->rts_use_shortpreamble = false;
552 tcb_desc->rts_use_shortgi = false;
Larry Finger0c817332010-12-08 11:12:31 -0600553
554 if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
555 /* Use CTS-to-SELF in protection mode. */
Larry Finger7ea47242011-02-19 16:28:57 -0600556 tcb_desc->rts_enable = true;
557 tcb_desc->cts_enable = true;
Larry Finger0c817332010-12-08 11:12:31 -0600558 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
559 } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
560 /* Use RTS-CTS in protection mode. */
Larry Finger7ea47242011-02-19 16:28:57 -0600561 tcb_desc->rts_enable = true;
Larry Finger0c817332010-12-08 11:12:31 -0600562 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
563 }
Larry Finger0c817332010-12-08 11:12:31 -0600564}
565
566static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
Chaoming_Liacd48572011-04-25 12:52:54 -0500567 struct ieee80211_sta *sta,
Larry Finger0c817332010-12-08 11:12:31 -0600568 struct rtl_tcb_desc *tcb_desc)
569{
570 struct rtl_priv *rtlpriv = rtl_priv(hw);
571 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
Chaoming_Liacd48572011-04-25 12:52:54 -0500572 struct rtl_sta_info *sta_entry = NULL;
573 u8 ratr_index = 7;
Larry Finger0c817332010-12-08 11:12:31 -0600574
Chaoming_Liacd48572011-04-25 12:52:54 -0500575 if (sta) {
576 sta_entry = (struct rtl_sta_info *) sta->drv_priv;
577 ratr_index = sta_entry->ratr_index;
578 }
Larry Finger0c817332010-12-08 11:12:31 -0600579 if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
Chaoming_Liacd48572011-04-25 12:52:54 -0500580 if (mac->opmode == NL80211_IFTYPE_STATION) {
Larry Finger0c817332010-12-08 11:12:31 -0600581 tcb_desc->ratr_index = 0;
Chaoming_Liacd48572011-04-25 12:52:54 -0500582 } else if (mac->opmode == NL80211_IFTYPE_ADHOC) {
Larry Finger7ea47242011-02-19 16:28:57 -0600583 if (tcb_desc->multicast || tcb_desc->broadcast) {
Larry Finger0c817332010-12-08 11:12:31 -0600584 tcb_desc->hw_rate =
585 rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
586 tcb_desc->use_driver_rate = 1;
587 } else {
588 /* TODO */
589 }
Chaoming_Liacd48572011-04-25 12:52:54 -0500590 tcb_desc->ratr_index = ratr_index;
591 } else if (mac->opmode == NL80211_IFTYPE_AP) {
592 tcb_desc->ratr_index = ratr_index;
Larry Finger0c817332010-12-08 11:12:31 -0600593 }
594 }
595
Larry Finger7ea47242011-02-19 16:28:57 -0600596 if (rtlpriv->dm.useramask) {
Chaoming_Liacd48572011-04-25 12:52:54 -0500597 /* TODO we will differentiate adhoc and station futrue */
598 if (mac->opmode == NL80211_IFTYPE_STATION) {
599 tcb_desc->mac_id = 0;
Larry Finger0c817332010-12-08 11:12:31 -0600600
Chaoming_Liacd48572011-04-25 12:52:54 -0500601 if (mac->mode == WIRELESS_MODE_N_24G)
602 tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
603 else if (mac->mode == WIRELESS_MODE_N_5G)
604 tcb_desc->ratr_index = RATR_INX_WIRELESS_NG;
605 else if (mac->mode & WIRELESS_MODE_G)
606 tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
607 else if (mac->mode & WIRELESS_MODE_B)
608 tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
609 else if (mac->mode & WIRELESS_MODE_A)
610 tcb_desc->ratr_index = RATR_INX_WIRELESS_G;
611 } else if (mac->opmode == NL80211_IFTYPE_AP ||
612 mac->opmode == NL80211_IFTYPE_ADHOC) {
613 if (NULL != sta) {
614 if (sta->aid > 0)
615 tcb_desc->mac_id = sta->aid + 1;
616 else
617 tcb_desc->mac_id = 1;
618 } else {
619 tcb_desc->mac_id = 0;
620 }
Larry Finger0c817332010-12-08 11:12:31 -0600621 }
622 }
623
624}
625
626static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
Chaoming_Liacd48572011-04-25 12:52:54 -0500627 struct ieee80211_sta *sta,
Larry Finger0c817332010-12-08 11:12:31 -0600628 struct rtl_tcb_desc *tcb_desc)
629{
630 struct rtl_priv *rtlpriv = rtl_priv(hw);
631 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
632
Larry Finger7ea47242011-02-19 16:28:57 -0600633 tcb_desc->packet_bw = false;
Chaoming_Liacd48572011-04-25 12:52:54 -0500634 if (!sta)
Larry Finger0c817332010-12-08 11:12:31 -0600635 return;
Chaoming_Liacd48572011-04-25 12:52:54 -0500636 if (mac->opmode == NL80211_IFTYPE_AP ||
637 mac->opmode == NL80211_IFTYPE_ADHOC) {
638 if (!(sta->ht_cap.ht_supported) ||
639 !(sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
640 return;
641 } else if (mac->opmode == NL80211_IFTYPE_STATION) {
642 if (!mac->bw_40 || !(sta->ht_cap.ht_supported))
643 return;
644 }
Larry Finger7ea47242011-02-19 16:28:57 -0600645 if (tcb_desc->multicast || tcb_desc->broadcast)
Larry Finger0c817332010-12-08 11:12:31 -0600646 return;
647
648 /*use legency rate, shall use 20MHz */
649 if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
650 return;
651
Larry Finger7ea47242011-02-19 16:28:57 -0600652 tcb_desc->packet_bw = true;
Larry Finger0c817332010-12-08 11:12:31 -0600653}
654
655static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw)
656{
657 struct rtl_priv *rtlpriv = rtl_priv(hw);
658 struct rtl_phy *rtlphy = &(rtlpriv->phy);
659 u8 hw_rate;
660
661 if (get_rf_type(rtlphy) == RF_2T2R)
662 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
663 else
664 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
665
666 return hw_rate;
667}
668
Larry Finger7ad0ce32011-08-22 16:50:14 -0500669/* mac80211's rate_idx is like this:
670 *
671 * 2.4G band:rx_status->band == IEEE80211_BAND_2GHZ
672 *
673 * B/G rate:
674 * (rx_status->flag & RX_FLAG_HT) = 0,
675 * DESC92_RATE1M-->DESC92_RATE54M ==> idx is 0-->11,
676 *
677 * N rate:
678 * (rx_status->flag & RX_FLAG_HT) = 1,
679 * DESC92_RATEMCS0-->DESC92_RATEMCS15 ==> idx is 0-->15
680 *
681 * 5G band:rx_status->band == IEEE80211_BAND_5GHZ
682 * A rate:
683 * (rx_status->flag & RX_FLAG_HT) = 0,
684 * DESC92_RATE6M-->DESC92_RATE54M ==> idx is 0-->7,
685 *
686 * N rate:
687 * (rx_status->flag & RX_FLAG_HT) = 1,
688 * DESC92_RATEMCS0-->DESC92_RATEMCS15 ==> idx is 0-->15
689 */
690int rtlwifi_rate_mapping(struct ieee80211_hw *hw,
691 bool isht, u8 desc_rate, bool first_ampdu)
692{
693 int rate_idx;
694
695 if (false == isht) {
696 if (IEEE80211_BAND_2GHZ == hw->conf.channel->band) {
697 switch (desc_rate) {
698 case DESC92_RATE1M:
699 rate_idx = 0;
700 break;
701 case DESC92_RATE2M:
702 rate_idx = 1;
703 break;
704 case DESC92_RATE5_5M:
705 rate_idx = 2;
706 break;
707 case DESC92_RATE11M:
708 rate_idx = 3;
709 break;
710 case DESC92_RATE6M:
711 rate_idx = 4;
712 break;
713 case DESC92_RATE9M:
714 rate_idx = 5;
715 break;
716 case DESC92_RATE12M:
717 rate_idx = 6;
718 break;
719 case DESC92_RATE18M:
720 rate_idx = 7;
721 break;
722 case DESC92_RATE24M:
723 rate_idx = 8;
724 break;
725 case DESC92_RATE36M:
726 rate_idx = 9;
727 break;
728 case DESC92_RATE48M:
729 rate_idx = 10;
730 break;
731 case DESC92_RATE54M:
732 rate_idx = 11;
733 break;
734 default:
735 rate_idx = 0;
736 break;
737 }
738 } else {
739 switch (desc_rate) {
740 case DESC92_RATE6M:
741 rate_idx = 0;
742 break;
743 case DESC92_RATE9M:
744 rate_idx = 1;
745 break;
746 case DESC92_RATE12M:
747 rate_idx = 2;
748 break;
749 case DESC92_RATE18M:
750 rate_idx = 3;
751 break;
752 case DESC92_RATE24M:
753 rate_idx = 4;
754 break;
755 case DESC92_RATE36M:
756 rate_idx = 5;
757 break;
758 case DESC92_RATE48M:
759 rate_idx = 6;
760 break;
761 case DESC92_RATE54M:
762 rate_idx = 7;
763 break;
764 default:
765 rate_idx = 0;
766 break;
767 }
768 }
769
770 } else {
771
772 switch (desc_rate) {
773 case DESC92_RATEMCS0:
774 rate_idx = 0;
775 break;
776 case DESC92_RATEMCS1:
777 rate_idx = 1;
778 break;
779 case DESC92_RATEMCS2:
780 rate_idx = 2;
781 break;
782 case DESC92_RATEMCS3:
783 rate_idx = 3;
784 break;
785 case DESC92_RATEMCS4:
786 rate_idx = 4;
787 break;
788 case DESC92_RATEMCS5:
789 rate_idx = 5;
790 break;
791 case DESC92_RATEMCS6:
792 rate_idx = 6;
793 break;
794 case DESC92_RATEMCS7:
795 rate_idx = 7;
796 break;
797 case DESC92_RATEMCS8:
798 rate_idx = 8;
799 break;
800 case DESC92_RATEMCS9:
801 rate_idx = 9;
802 break;
803 case DESC92_RATEMCS10:
804 rate_idx = 10;
805 break;
806 case DESC92_RATEMCS11:
807 rate_idx = 11;
808 break;
809 case DESC92_RATEMCS12:
810 rate_idx = 12;
811 break;
812 case DESC92_RATEMCS13:
813 rate_idx = 13;
814 break;
815 case DESC92_RATEMCS14:
816 rate_idx = 14;
817 break;
818 case DESC92_RATEMCS15:
819 rate_idx = 15;
820 break;
821 default:
822 rate_idx = 0;
823 break;
824 }
825 }
826 return rate_idx;
827}
828EXPORT_SYMBOL(rtlwifi_rate_mapping);
829
Larry Finger0c817332010-12-08 11:12:31 -0600830void rtl_get_tcb_desc(struct ieee80211_hw *hw,
831 struct ieee80211_tx_info *info,
Chaoming_Liacd48572011-04-25 12:52:54 -0500832 struct ieee80211_sta *sta,
Larry Finger0c817332010-12-08 11:12:31 -0600833 struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
834{
835 struct rtl_priv *rtlpriv = rtl_priv(hw);
836 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
Chaoming_Liacd48572011-04-25 12:52:54 -0500837 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
Larry Finger0c817332010-12-08 11:12:31 -0600838 struct ieee80211_rate *txrate;
Larry Finger17c9ac62011-02-19 16:29:57 -0600839 __le16 fc = hdr->frame_control;
Larry Finger0c817332010-12-08 11:12:31 -0600840
Chaoming_Liacd48572011-04-25 12:52:54 -0500841 txrate = ieee80211_get_tx_rate(hw, info);
Larry Fingere89f7692012-04-06 16:54:17 -0500842 if (txrate)
843 tcb_desc->hw_rate = txrate->hw_value;
844 else
845 tcb_desc->hw_rate = 0;
Larry Finger0c817332010-12-08 11:12:31 -0600846
847 if (ieee80211_is_data(fc)) {
Larry Finger0c817332010-12-08 11:12:31 -0600848 /*
Chaoming_Liacd48572011-04-25 12:52:54 -0500849 *we set data rate INX 0
Larry Finger0c817332010-12-08 11:12:31 -0600850 *in rtl_rc.c if skb is special data or
851 *mgt which need low data rate.
852 */
853
854 /*
855 *So tcb_desc->hw_rate is just used for
856 *special data and mgt frames
857 */
Chaoming Li09e92f02011-10-11 21:28:48 -0500858 if (info->control.rates[0].idx == 0 ||
Chaoming_Liacd48572011-04-25 12:52:54 -0500859 ieee80211_is_nullfunc(fc)) {
Larry Finger0c817332010-12-08 11:12:31 -0600860 tcb_desc->use_driver_rate = true;
Chaoming_Liacd48572011-04-25 12:52:54 -0500861 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
Larry Finger0c817332010-12-08 11:12:31 -0600862
Larry Finger0c817332010-12-08 11:12:31 -0600863 tcb_desc->disable_ratefallback = 1;
864 } else {
865 /*
866 *because hw will nerver use hw_rate
867 *when tcb_desc->use_driver_rate = false
868 *so we never set highest N rate here,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300869 *and N rate will all be controlled by FW
Larry Finger0c817332010-12-08 11:12:31 -0600870 *when tcb_desc->use_driver_rate = false
871 */
Chaoming_Liacd48572011-04-25 12:52:54 -0500872 if (sta && (sta->ht_cap.ht_supported)) {
Larry Finger0c817332010-12-08 11:12:31 -0600873 tcb_desc->hw_rate = _rtl_get_highest_n_rate(hw);
874 } else {
875 if (rtlmac->mode == WIRELESS_MODE_B) {
876 tcb_desc->hw_rate =
877 rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
878 } else {
879 tcb_desc->hw_rate =
880 rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
881 }
882 }
883 }
884
885 if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
Larry Finger7ea47242011-02-19 16:28:57 -0600886 tcb_desc->multicast = 1;
Larry Finger0c817332010-12-08 11:12:31 -0600887 else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
Larry Finger7ea47242011-02-19 16:28:57 -0600888 tcb_desc->broadcast = 1;
Larry Finger0c817332010-12-08 11:12:31 -0600889
Chaoming_Liacd48572011-04-25 12:52:54 -0500890 _rtl_txrate_selectmode(hw, sta, tcb_desc);
891 _rtl_query_bandwidth_mode(hw, sta, tcb_desc);
Larry Finger0c817332010-12-08 11:12:31 -0600892 _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
Chaoming_Liacd48572011-04-25 12:52:54 -0500893 _rtl_query_shortgi(hw, sta, tcb_desc, info);
Larry Finger0c817332010-12-08 11:12:31 -0600894 _rtl_query_protection_mode(hw, tcb_desc, info);
895 } else {
896 tcb_desc->use_driver_rate = true;
Chaoming_Liacd48572011-04-25 12:52:54 -0500897 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
Larry Finger0c817332010-12-08 11:12:31 -0600898 tcb_desc->disable_ratefallback = 1;
899 tcb_desc->mac_id = 0;
Chaoming_Liacd48572011-04-25 12:52:54 -0500900 tcb_desc->packet_bw = false;
Larry Finger0c817332010-12-08 11:12:31 -0600901 }
902}
903EXPORT_SYMBOL(rtl_get_tcb_desc);
904
Larry Finger0c817332010-12-08 11:12:31 -0600905bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
906{
907 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
Chaoming_Liacd48572011-04-25 12:52:54 -0500908 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
Larry Finger0c817332010-12-08 11:12:31 -0600909 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Finger17c9ac62011-02-19 16:29:57 -0600910 __le16 fc = hdr->frame_control;
Larry Finger0c817332010-12-08 11:12:31 -0600911 u8 *act = (u8 *) (((u8 *) skb->data + MAC80211_3ADDR_LEN));
912 u8 category;
913
914 if (!ieee80211_is_action(fc))
915 return true;
916
917 category = *act;
918 act++;
919 switch (category) {
920 case ACT_CAT_BA:
921 switch (*act) {
922 case ACT_ADDBAREQ:
923 if (mac->act_scanning)
924 return false;
925
926 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800927 "%s ACT_ADDBAREQ From :%pM\n",
928 is_tx ? "Tx" : "Rx", hdr->addr2);
Larry Finger0c817332010-12-08 11:12:31 -0600929 break;
930 case ACT_ADDBARSP:
931 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800932 "%s ACT_ADDBARSP From :%pM\n",
933 is_tx ? "Tx" : "Rx", hdr->addr2);
Larry Finger0c817332010-12-08 11:12:31 -0600934 break;
935 case ACT_DELBA:
936 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800937 "ACT_ADDBADEL From :%pM\n", hdr->addr2);
Larry Finger0c817332010-12-08 11:12:31 -0600938 break;
939 }
940 break;
941 default:
942 break;
943 }
944
945 return true;
946}
947
948/*should call before software enc*/
949u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
950{
951 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Finger0c817332010-12-08 11:12:31 -0600952 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
Chaoming_Liacd48572011-04-25 12:52:54 -0500953 __le16 fc = rtl_get_fc(skb);
Larry Finger0c817332010-12-08 11:12:31 -0600954 u16 ether_type;
955 u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
956 const struct iphdr *ip;
957
958 if (!ieee80211_is_data(fc))
Larry Finger32473282011-03-27 16:19:57 -0500959 return false;
Larry Finger0c817332010-12-08 11:12:31 -0600960
Mark Cave-Ayland172ba812014-02-27 09:53:03 +0800961 ip = (const struct iphdr *)(skb->data + mac_hdr_len +
962 SNAP_SIZE + PROTOC_TYPE_SIZE);
963 ether_type = be16_to_cpup((__be16 *)
964 (skb->data + mac_hdr_len + SNAP_SIZE));
Larry Finger0c817332010-12-08 11:12:31 -0600965
Mark Cave-Ayland172ba812014-02-27 09:53:03 +0800966 switch (ether_type) {
967 case ETH_P_IP: {
968 struct udphdr *udp;
969 u16 src;
970 u16 dst;
Larry Finger0c817332010-12-08 11:12:31 -0600971
Mark Cave-Ayland172ba812014-02-27 09:53:03 +0800972 if (ip->protocol != IPPROTO_UDP)
973 return false;
974 udp = (struct udphdr *)((u8 *)ip + (ip->ihl << 2));
975 src = be16_to_cpu(udp->source);
976 dst = be16_to_cpu(udp->dest);
Larry Finger0c817332010-12-08 11:12:31 -0600977
Mark Cave-Ayland172ba812014-02-27 09:53:03 +0800978 /* If this case involves port 68 (UDP BOOTP client) connecting
979 * with port 67 (UDP BOOTP server), then return true so that
980 * the lowest speed is used.
981 */
982 if (!((src == 68 && dst == 67) || (src == 67 && dst == 68)))
983 return false;
Larry Finger0c817332010-12-08 11:12:31 -0600984
Mark Cave-Ayland172ba812014-02-27 09:53:03 +0800985 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
986 "dhcp %s !!\n", is_tx ? "Tx" : "Rx");
987 break;
988 }
989 case ETH_P_ARP:
990 break;
991 case ETH_P_PAE:
Larry Finger0c817332010-12-08 11:12:31 -0600992 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800993 "802.1X %s EAPOL pkt!!\n", is_tx ? "Tx" : "Rx");
Mark Cave-Ayland172ba812014-02-27 09:53:03 +0800994 break;
995 case ETH_P_IPV6:
996 /* TODO: Is this right? */
997 return false;
998 default:
999 return false;
Larry Finger0c817332010-12-08 11:12:31 -06001000 }
Mark Cave-Ayland172ba812014-02-27 09:53:03 +08001001 if (is_tx) {
1002 schedule_work(&rtlpriv->works.lps_leave_work);
1003 ppsc->last_delaylps_stamp_jiffies = jiffies;
1004 }
1005 return true;
Larry Finger0c817332010-12-08 11:12:31 -06001006}
1007
1008/*********************************************************
1009 *
1010 * functions called by core.c
1011 *
1012 *********************************************************/
Chaoming_Liacd48572011-04-25 12:52:54 -05001013int rtl_tx_agg_start(struct ieee80211_hw *hw,
1014 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
Larry Finger0c817332010-12-08 11:12:31 -06001015{
1016 struct rtl_priv *rtlpriv = rtl_priv(hw);
1017 struct rtl_tid_data *tid_data;
1018 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
Chaoming_Liacd48572011-04-25 12:52:54 -05001019 struct rtl_sta_info *sta_entry = NULL;
Larry Finger0c817332010-12-08 11:12:31 -06001020
Chaoming_Liacd48572011-04-25 12:52:54 -05001021 if (sta == NULL)
1022 return -EINVAL;
Larry Finger0c817332010-12-08 11:12:31 -06001023
1024 if (unlikely(tid >= MAX_TID_COUNT))
1025 return -EINVAL;
1026
Chaoming_Liacd48572011-04-25 12:52:54 -05001027 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1028 if (!sta_entry)
Larry Finger0c817332010-12-08 11:12:31 -06001029 return -ENXIO;
Chaoming_Liacd48572011-04-25 12:52:54 -05001030 tid_data = &sta_entry->tids[tid];
Larry Finger0c817332010-12-08 11:12:31 -06001031
Joe Perchesf30d7502012-01-04 19:40:41 -08001032 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d seq:%d\n",
1033 sta->addr, tid, tid_data->seq_number);
Larry Finger0c817332010-12-08 11:12:31 -06001034
Chaoming_Liacd48572011-04-25 12:52:54 -05001035 *ssn = tid_data->seq_number;
1036 tid_data->agg.agg_state = RTL_AGG_START;
1037
1038 ieee80211_start_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
Larry Finger0c817332010-12-08 11:12:31 -06001039
1040 return 0;
1041}
1042
Chaoming_Liacd48572011-04-25 12:52:54 -05001043int rtl_tx_agg_stop(struct ieee80211_hw *hw,
1044 struct ieee80211_sta *sta, u16 tid)
Larry Finger0c817332010-12-08 11:12:31 -06001045{
Larry Finger0c817332010-12-08 11:12:31 -06001046 struct rtl_priv *rtlpriv = rtl_priv(hw);
1047 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
Chaoming_Liacd48572011-04-25 12:52:54 -05001048 struct rtl_sta_info *sta_entry = NULL;
Larry Finger0c817332010-12-08 11:12:31 -06001049
Chaoming_Liacd48572011-04-25 12:52:54 -05001050 if (sta == NULL)
1051 return -EINVAL;
1052
1053 if (!sta->addr) {
Joe Perchesf30d7502012-01-04 19:40:41 -08001054 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
Larry Finger0c817332010-12-08 11:12:31 -06001055 return -EINVAL;
1056 }
1057
Joe Perchesf30d7502012-01-04 19:40:41 -08001058 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d\n",
1059 sta->addr, tid);
Chaoming_Liacd48572011-04-25 12:52:54 -05001060
Larry Finger0c817332010-12-08 11:12:31 -06001061 if (unlikely(tid >= MAX_TID_COUNT))
1062 return -EINVAL;
1063
Chaoming_Liacd48572011-04-25 12:52:54 -05001064 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
Chaoming_Liacd48572011-04-25 12:52:54 -05001065 sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
Larry Finger0c817332010-12-08 11:12:31 -06001066
Chaoming_Liacd48572011-04-25 12:52:54 -05001067 ieee80211_stop_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
Larry Finger0c817332010-12-08 11:12:31 -06001068
Chaoming_Liacd48572011-04-25 12:52:54 -05001069 return 0;
1070}
Larry Finger0c817332010-12-08 11:12:31 -06001071
Chaoming_Liacd48572011-04-25 12:52:54 -05001072int rtl_tx_agg_oper(struct ieee80211_hw *hw,
1073 struct ieee80211_sta *sta, u16 tid)
1074{
1075 struct rtl_priv *rtlpriv = rtl_priv(hw);
Chaoming_Liacd48572011-04-25 12:52:54 -05001076 struct rtl_sta_info *sta_entry = NULL;
1077
1078 if (sta == NULL)
1079 return -EINVAL;
1080
1081 if (!sta->addr) {
Joe Perchesf30d7502012-01-04 19:40:41 -08001082 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05001083 return -EINVAL;
1084 }
1085
Joe Perchesf30d7502012-01-04 19:40:41 -08001086 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d\n",
1087 sta->addr, tid);
Chaoming_Liacd48572011-04-25 12:52:54 -05001088
1089 if (unlikely(tid >= MAX_TID_COUNT))
1090 return -EINVAL;
1091
1092 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
Chaoming_Liacd48572011-04-25 12:52:54 -05001093 sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
Larry Finger0c817332010-12-08 11:12:31 -06001094
1095 return 0;
1096}
1097
1098/*********************************************************
1099 *
1100 * wq & timer callback functions
1101 *
1102 *********************************************************/
1103void rtl_watchdog_wq_callback(void *data)
1104{
1105 struct rtl_works *rtlworks = container_of_dwork_rtl(data,
1106 struct rtl_works,
1107 watchdog_wq);
1108 struct ieee80211_hw *hw = rtlworks->hw;
1109 struct rtl_priv *rtlpriv = rtl_priv(hw);
1110 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1111 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
Larry Finger7ea47242011-02-19 16:28:57 -06001112 bool busytraffic = false;
1113 bool higher_busytraffic = false;
1114 bool higher_busyrxtraffic = false;
Chaoming_Liacd48572011-04-25 12:52:54 -05001115 u8 idx, tid;
Larry Finger0c817332010-12-08 11:12:31 -06001116 u32 rx_cnt_inp4eriod = 0;
1117 u32 tx_cnt_inp4eriod = 0;
1118 u32 aver_rx_cnt_inperiod = 0;
1119 u32 aver_tx_cnt_inperiod = 0;
Chaoming_Liacd48572011-04-25 12:52:54 -05001120 u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
1121 u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
Larry Finger7ea47242011-02-19 16:28:57 -06001122 bool enter_ps = false;
Larry Finger0c817332010-12-08 11:12:31 -06001123
1124 if (is_hal_stop(rtlhal))
1125 return;
1126
1127 /* <1> Determine if action frame is allowed */
1128 if (mac->link_state > MAC80211_NOLINK) {
1129 if (mac->cnt_after_linked < 20)
1130 mac->cnt_after_linked++;
1131 } else {
1132 mac->cnt_after_linked = 0;
1133 }
1134
Larry Finger0c817332010-12-08 11:12:31 -06001135 /*
Chaoming Li09e92f02011-10-11 21:28:48 -05001136 *<2> to check if traffic busy, if
Larry Finger0c817332010-12-08 11:12:31 -06001137 * busytraffic we don't change channel
1138 */
1139 if (mac->link_state >= MAC80211_LINKED) {
1140
1141 /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
1142 for (idx = 0; idx <= 2; idx++) {
1143 rtlpriv->link_info.num_rx_in4period[idx] =
1144 rtlpriv->link_info.num_rx_in4period[idx + 1];
1145 rtlpriv->link_info.num_tx_in4period[idx] =
1146 rtlpriv->link_info.num_tx_in4period[idx + 1];
1147 }
1148 rtlpriv->link_info.num_rx_in4period[3] =
1149 rtlpriv->link_info.num_rx_inperiod;
1150 rtlpriv->link_info.num_tx_in4period[3] =
1151 rtlpriv->link_info.num_tx_inperiod;
1152 for (idx = 0; idx <= 3; idx++) {
1153 rx_cnt_inp4eriod +=
1154 rtlpriv->link_info.num_rx_in4period[idx];
1155 tx_cnt_inp4eriod +=
1156 rtlpriv->link_info.num_tx_in4period[idx];
1157 }
1158 aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
1159 aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
1160
1161 /* (2) check traffic busy */
1162 if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100)
Larry Finger7ea47242011-02-19 16:28:57 -06001163 busytraffic = true;
Larry Finger0c817332010-12-08 11:12:31 -06001164
1165 /* Higher Tx/Rx data. */
1166 if (aver_rx_cnt_inperiod > 4000 ||
1167 aver_tx_cnt_inperiod > 4000) {
Larry Finger7ea47242011-02-19 16:28:57 -06001168 higher_busytraffic = true;
Larry Finger0c817332010-12-08 11:12:31 -06001169
1170 /* Extremely high Rx data. */
1171 if (aver_rx_cnt_inperiod > 5000)
Larry Finger7ea47242011-02-19 16:28:57 -06001172 higher_busyrxtraffic = true;
Chaoming_Liacd48572011-04-25 12:52:54 -05001173 }
1174
1175 /* check every tid's tx traffic */
1176 for (tid = 0; tid <= 7; tid++) {
1177 for (idx = 0; idx <= 2; idx++)
1178 rtlpriv->link_info.tidtx_in4period[tid][idx] =
1179 rtlpriv->link_info.tidtx_in4period[tid]
1180 [idx + 1];
1181 rtlpriv->link_info.tidtx_in4period[tid][3] =
1182 rtlpriv->link_info.tidtx_inperiod[tid];
1183
1184 for (idx = 0; idx <= 3; idx++)
1185 tidtx_inp4eriod[tid] +=
1186 rtlpriv->link_info.tidtx_in4period[tid][idx];
1187 aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
1188 if (aver_tidtx_inperiod[tid] > 5000)
1189 rtlpriv->link_info.higher_busytxtraffic[tid] =
1190 true;
Larry Finger0c817332010-12-08 11:12:31 -06001191 else
Chaoming_Liacd48572011-04-25 12:52:54 -05001192 rtlpriv->link_info.higher_busytxtraffic[tid] =
1193 false;
Larry Finger0c817332010-12-08 11:12:31 -06001194 }
1195
1196 if (((rtlpriv->link_info.num_rx_inperiod +
1197 rtlpriv->link_info.num_tx_inperiod) > 8) ||
1198 (rtlpriv->link_info.num_rx_inperiod > 2))
Larry Finger7ea47242011-02-19 16:28:57 -06001199 enter_ps = false;
Larry Finger0c817332010-12-08 11:12:31 -06001200 else
Larry Finger7ea47242011-02-19 16:28:57 -06001201 enter_ps = true;
Larry Finger0c817332010-12-08 11:12:31 -06001202
1203 /* LeisurePS only work in infra mode. */
Larry Finger7ea47242011-02-19 16:28:57 -06001204 if (enter_ps)
Larry Finger0c817332010-12-08 11:12:31 -06001205 rtl_lps_enter(hw);
1206 else
1207 rtl_lps_leave(hw);
1208 }
1209
1210 rtlpriv->link_info.num_rx_inperiod = 0;
1211 rtlpriv->link_info.num_tx_inperiod = 0;
Chaoming_Liacd48572011-04-25 12:52:54 -05001212 for (tid = 0; tid <= 7; tid++)
1213 rtlpriv->link_info.tidtx_inperiod[tid] = 0;
Larry Finger0c817332010-12-08 11:12:31 -06001214
Larry Finger7ea47242011-02-19 16:28:57 -06001215 rtlpriv->link_info.busytraffic = busytraffic;
1216 rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
1217 rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
Larry Finger0c817332010-12-08 11:12:31 -06001218
Chaoming_Liacd48572011-04-25 12:52:54 -05001219 /* <3> DM */
1220 rtlpriv->cfg->ops->dm_watchdog(hw);
Larry Finger0c817332010-12-08 11:12:31 -06001221}
1222
1223void rtl_watch_dog_timer_callback(unsigned long data)
1224{
1225 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1226 struct rtl_priv *rtlpriv = rtl_priv(hw);
1227
1228 queue_delayed_work(rtlpriv->works.rtl_wq,
1229 &rtlpriv->works.watchdog_wq, 0);
1230
1231 mod_timer(&rtlpriv->works.watchdog_timer,
1232 jiffies + MSECS(RTL_WATCH_DOG_TIME));
1233}
1234
1235/*********************************************************
1236 *
Chaoming_Liacd48572011-04-25 12:52:54 -05001237 * frame process functions
1238 *
1239 *********************************************************/
1240u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
1241{
1242 struct ieee80211_mgmt *mgmt = (void *)data;
1243 u8 *pos, *end;
1244
1245 pos = (u8 *)mgmt->u.beacon.variable;
1246 end = data + len;
1247 while (pos < end) {
1248 if (pos + 2 + pos[1] > end)
1249 return NULL;
1250
1251 if (pos[0] == ie)
1252 return pos;
1253
1254 pos += 2 + pos[1];
1255 }
1256 return NULL;
1257}
1258
1259/* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
1260/* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
Larry Fingerd3bb1422011-04-25 13:23:20 -05001261static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
Chaoming_Liacd48572011-04-25 12:52:54 -05001262 enum ieee80211_smps_mode smps, u8 *da, u8 *bssid)
1263{
1264 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1265 struct sk_buff *skb;
1266 struct ieee80211_mgmt *action_frame;
1267
1268 /* 27 = header + category + action + smps mode */
1269 skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
1270 if (!skb)
1271 return NULL;
1272
1273 skb_reserve(skb, hw->extra_tx_headroom);
1274 action_frame = (void *)skb_put(skb, 27);
1275 memset(action_frame, 0, 27);
1276 memcpy(action_frame->da, da, ETH_ALEN);
1277 memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
1278 memcpy(action_frame->bssid, bssid, ETH_ALEN);
1279 action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1280 IEEE80211_STYPE_ACTION);
1281 action_frame->u.action.category = WLAN_CATEGORY_HT;
1282 action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
1283 switch (smps) {
1284 case IEEE80211_SMPS_AUTOMATIC:/* 0 */
1285 case IEEE80211_SMPS_NUM_MODES:/* 4 */
1286 WARN_ON(1);
1287 case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
1288 action_frame->u.action.u.ht_smps.smps_control =
1289 WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
1290 break;
1291 case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
1292 action_frame->u.action.u.ht_smps.smps_control =
1293 WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
1294 break;
1295 case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
1296 action_frame->u.action.u.ht_smps.smps_control =
1297 WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
1298 break;
1299 }
1300
1301 return skb;
1302}
1303
1304int rtl_send_smps_action(struct ieee80211_hw *hw,
1305 struct ieee80211_sta *sta, u8 *da, u8 *bssid,
1306 enum ieee80211_smps_mode smps)
1307{
1308 struct rtl_priv *rtlpriv = rtl_priv(hw);
1309 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1310 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1311 struct sk_buff *skb = rtl_make_smps_action(hw, smps, da, bssid);
1312 struct rtl_tcb_desc tcb_desc;
1313 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
1314
1315 if (rtlpriv->mac80211.act_scanning)
1316 goto err_free;
1317
1318 if (!sta)
1319 goto err_free;
1320
1321 if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
1322 goto err_free;
1323
1324 if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1325 goto err_free;
1326
1327 /* this is a type = mgmt * stype = action frame */
1328 if (skb) {
1329 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1330 struct rtl_sta_info *sta_entry =
1331 (struct rtl_sta_info *) sta->drv_priv;
1332 sta_entry->mimo_ps = smps;
1333 rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0);
1334
1335 info->control.rates[0].idx = 0;
1336 info->control.sta = sta;
1337 info->band = hw->conf.channel->band;
Chaoming_Liacd48572011-04-25 12:52:54 -05001338 rtlpriv->intf_ops->adapter_tx(hw, skb, &tcb_desc);
Chaoming_Liacd48572011-04-25 12:52:54 -05001339 }
Chaoming_Liacd48572011-04-25 12:52:54 -05001340err_free:
1341 return 0;
1342}
1343
1344/*********************************************************
1345 *
1346 * IOT functions
1347 *
1348 *********************************************************/
1349static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
1350 struct octet_string vendor_ie)
1351{
1352 struct rtl_priv *rtlpriv = rtl_priv(hw);
1353 bool matched = false;
1354 static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
1355 static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
1356 static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
1357 static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
1358 static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
1359 static u8 racap[] = { 0x00, 0x0c, 0x43 };
1360 static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
1361 static u8 marvcap[] = { 0x00, 0x50, 0x43 };
1362
1363 if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
1364 memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
1365 rtlpriv->mac80211.vendor = PEER_ATH;
1366 matched = true;
1367 } else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
1368 memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
1369 memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
1370 rtlpriv->mac80211.vendor = PEER_BROAD;
1371 matched = true;
1372 } else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
1373 rtlpriv->mac80211.vendor = PEER_RAL;
1374 matched = true;
1375 } else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
1376 rtlpriv->mac80211.vendor = PEER_CISCO;
1377 matched = true;
1378 } else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
1379 rtlpriv->mac80211.vendor = PEER_MARV;
1380 matched = true;
1381 }
1382
1383 return matched;
1384}
1385
Larry Fingerd3bb1422011-04-25 13:23:20 -05001386static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
Chaoming_Liacd48572011-04-25 12:52:54 -05001387 unsigned int len)
1388{
1389 struct ieee80211_mgmt *mgmt = (void *)data;
1390 struct octet_string vendor_ie;
1391 u8 *pos, *end;
1392
1393 pos = (u8 *)mgmt->u.beacon.variable;
1394 end = data + len;
1395 while (pos < end) {
1396 if (pos[0] == 221) {
1397 vendor_ie.length = pos[1];
1398 vendor_ie.octet = &pos[2];
1399 if (rtl_chk_vendor_ouisub(hw, vendor_ie))
1400 return true;
1401 }
1402
1403 if (pos + 2 + pos[1] > end)
1404 return false;
1405
1406 pos += 2 + pos[1];
1407 }
1408 return false;
1409}
1410
1411void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
1412{
1413 struct rtl_priv *rtlpriv = rtl_priv(hw);
1414 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1415 struct ieee80211_hdr *hdr = (void *)data;
1416 u32 vendor = PEER_UNKNOWN;
1417
1418 static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
1419 static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
1420 static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
1421 static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
1422 static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
1423 static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
1424 static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
1425 static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
1426 static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
1427 static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
1428 static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
1429 static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
1430 static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
1431 static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
1432 static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
1433 static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
1434
1435 if (mac->opmode != NL80211_IFTYPE_STATION)
1436 return;
1437
1438 if (mac->link_state == MAC80211_NOLINK) {
1439 mac->vendor = PEER_UNKNOWN;
1440 return;
1441 }
1442
1443 if (mac->cnt_after_linked > 2)
1444 return;
1445
1446 /* check if this really is a beacon */
1447 if (!ieee80211_is_beacon(hdr->frame_control))
1448 return;
1449
1450 /* min. beacon length + FCS_LEN */
1451 if (len <= 40 + FCS_LEN)
1452 return;
1453
1454 /* and only beacons from the associated BSSID, please */
1455 if (compare_ether_addr(hdr->addr3, rtlpriv->mac80211.bssid))
1456 return;
1457
1458 if (rtl_find_221_ie(hw, data, len))
1459 vendor = mac->vendor;
1460
1461 if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
1462 (memcmp(mac->bssid, ap5_2, 3) == 0) ||
1463 (memcmp(mac->bssid, ap5_3, 3) == 0) ||
1464 (memcmp(mac->bssid, ap5_4, 3) == 0) ||
1465 (memcmp(mac->bssid, ap5_5, 3) == 0) ||
1466 (memcmp(mac->bssid, ap5_6, 3) == 0) ||
1467 vendor == PEER_ATH) {
1468 vendor = PEER_ATH;
Joe Perchesf30d7502012-01-04 19:40:41 -08001469 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ath find\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05001470 } else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
1471 (memcmp(mac->bssid, ap4_5, 3) == 0) ||
1472 (memcmp(mac->bssid, ap4_1, 3) == 0) ||
1473 (memcmp(mac->bssid, ap4_2, 3) == 0) ||
1474 (memcmp(mac->bssid, ap4_3, 3) == 0) ||
1475 vendor == PEER_RAL) {
Joe Perchesf30d7502012-01-04 19:40:41 -08001476 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ral find\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05001477 vendor = PEER_RAL;
1478 } else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
1479 vendor == PEER_CISCO) {
1480 vendor = PEER_CISCO;
Joe Perchesf30d7502012-01-04 19:40:41 -08001481 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>cisco find\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05001482 } else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
1483 (memcmp(mac->bssid, ap3_2, 3) == 0) ||
1484 (memcmp(mac->bssid, ap3_3, 3) == 0) ||
1485 vendor == PEER_BROAD) {
Joe Perchesf30d7502012-01-04 19:40:41 -08001486 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>broad find\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05001487 vendor = PEER_BROAD;
1488 } else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
1489 vendor == PEER_MARV) {
1490 vendor = PEER_MARV;
Joe Perchesf30d7502012-01-04 19:40:41 -08001491 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>marv find\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05001492 }
1493
1494 mac->vendor = vendor;
1495}
1496
1497/*********************************************************
1498 *
Larry Finger0c817332010-12-08 11:12:31 -06001499 * sysfs functions
1500 *
1501 *********************************************************/
1502static ssize_t rtl_show_debug_level(struct device *d,
1503 struct device_attribute *attr, char *buf)
1504{
1505 struct ieee80211_hw *hw = dev_get_drvdata(d);
1506 struct rtl_priv *rtlpriv = rtl_priv(hw);
1507
1508 return sprintf(buf, "0x%08X\n", rtlpriv->dbg.global_debuglevel);
1509}
1510
1511static ssize_t rtl_store_debug_level(struct device *d,
1512 struct device_attribute *attr,
1513 const char *buf, size_t count)
1514{
1515 struct ieee80211_hw *hw = dev_get_drvdata(d);
1516 struct rtl_priv *rtlpriv = rtl_priv(hw);
1517 unsigned long val;
1518 int ret;
1519
1520 ret = strict_strtoul(buf, 0, &val);
1521 if (ret) {
1522 printk(KERN_DEBUG "%s is not in hex or decimal form.\n", buf);
1523 } else {
1524 rtlpriv->dbg.global_debuglevel = val;
1525 printk(KERN_DEBUG "debuglevel:%x\n",
1526 rtlpriv->dbg.global_debuglevel);
1527 }
1528
1529 return strnlen(buf, count);
1530}
1531
1532static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
1533 rtl_show_debug_level, rtl_store_debug_level);
1534
1535static struct attribute *rtl_sysfs_entries[] = {
1536
1537 &dev_attr_debug_level.attr,
1538
1539 NULL
1540};
1541
1542/*
1543 * "name" is folder name witch will be
1544 * put in device directory like :
1545 * sys/devices/pci0000:00/0000:00:1c.4/
1546 * 0000:06:00.0/rtl_sysfs
1547 */
1548struct attribute_group rtl_attribute_group = {
1549 .name = "rtlsysfs",
1550 .attrs = rtl_sysfs_entries,
1551};
1552
1553MODULE_AUTHOR("lizhaoming <chaoming_li@realsil.com.cn>");
1554MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>");
1555MODULE_AUTHOR("Larry Finger <Larry.FInger@lwfinger.net>");
1556MODULE_LICENSE("GPL");
1557MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
1558
1559static int __init rtl_core_module_init(void)
1560{
Chaoming Li375ff4c2011-02-28 16:40:28 -06001561 if (rtl_rate_control_register())
Joe Perches292b1192011-07-20 08:51:35 -07001562 pr_err("Unable to register rtl_rc, use default RC !!\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05001563
Larry Finger0c817332010-12-08 11:12:31 -06001564 return 0;
1565}
1566
1567static void __exit rtl_core_module_exit(void)
1568{
Chaoming_Liacd48572011-04-25 12:52:54 -05001569 /*RC*/
Chaoming Li375ff4c2011-02-28 16:40:28 -06001570 rtl_rate_control_unregister();
Larry Finger0c817332010-12-08 11:12:31 -06001571}
1572
1573module_init(rtl_core_module_init);
1574module_exit(rtl_core_module_exit);