1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include "decl.h"
21#include "ioctl.h"
22#include "util.h"
23#include "fw.h"
24#include "main.h"
25#include "cfg80211.h"
26
27
28#define MWIFIEX_TX_PWR_DEFAULT 20
29
30#define MWIFIEX_TX_PWR_US_DEFAULT 20
31
32#define MWIFIEX_TX_PWR_JP_DEFAULT 16
33
34#define MWIFIEX_TX_PWR_FR_100MW 20
35
36#define MWIFIEX_TX_PWR_FR_10MW 10
37
38#define MWIFIEX_TX_PWR_EMEA_DEFAULT 20
39
40static u8 adhoc_rates_b[B_SUPPORTED_RATES] = { 0x82, 0x84, 0x8b, 0x96, 0 };
41
42static u8 adhoc_rates_g[G_SUPPORTED_RATES] = { 0x8c, 0x12, 0x98, 0x24,
43 0xb0, 0x48, 0x60, 0x6c, 0 };
44
45static u8 adhoc_rates_bg[BG_SUPPORTED_RATES] = { 0x82, 0x84, 0x8b, 0x96,
46 0x0c, 0x12, 0x18, 0x24,
47 0x30, 0x48, 0x60, 0x6c, 0 };
48
49static u8 adhoc_rates_a[A_SUPPORTED_RATES] = { 0x8c, 0x12, 0x98, 0x24,
50 0xb0, 0x48, 0x60, 0x6c, 0 };
51static u8 supported_rates_a[A_SUPPORTED_RATES] = { 0x0c, 0x12, 0x18, 0x24,
52 0xb0, 0x48, 0x60, 0x6c, 0 };
53static u16 mwifiex_data_rates[MWIFIEX_SUPPORTED_RATES_EXT] = { 0x02, 0x04,
54 0x0B, 0x16, 0x00, 0x0C, 0x12, 0x18,
55 0x24, 0x30, 0x48, 0x60, 0x6C, 0x90,
56 0x0D, 0x1A, 0x27, 0x34, 0x4E, 0x68,
57 0x75, 0x82, 0x0C, 0x1B, 0x36, 0x51,
58 0x6C, 0xA2, 0xD8, 0xF3, 0x10E, 0x00 };
59
60static u8 supported_rates_b[B_SUPPORTED_RATES] = { 0x02, 0x04, 0x0b, 0x16, 0 };
61
62static u8 supported_rates_g[G_SUPPORTED_RATES] = { 0x0c, 0x12, 0x18, 0x24,
63 0x30, 0x48, 0x60, 0x6c, 0 };
64
65static u8 supported_rates_bg[BG_SUPPORTED_RATES] = { 0x02, 0x04, 0x0b, 0x0c,
66 0x12, 0x16, 0x18, 0x24, 0x30, 0x48,
67 0x60, 0x6c, 0 };
68
69u16 region_code_index[MWIFIEX_MAX_REGION_CODE] = { 0x10, 0x20, 0x30,
70 0x32, 0x40, 0x41, 0xff };
71
72static u8 supported_rates_n[N_SUPPORTED_RATES] = { 0x02, 0x04, 0 };
73
74
75
76
77static const u16 mcs_rate[4][16] = {
78
79 { 0x1b, 0x36, 0x51, 0x6c, 0xa2, 0xd8, 0xf3, 0x10e,
80 0x36, 0x6c, 0xa2, 0xd8, 0x144, 0x1b0, 0x1e6, 0x21c },
81
82
83 { 0x1e, 0x3c, 0x5a, 0x78, 0xb4, 0xf0, 0x10e, 0x12c,
84 0x3c, 0x78, 0xb4, 0xf0, 0x168, 0x1e0, 0x21c, 0x258 },
85
86
87 { 0x0d, 0x1a, 0x27, 0x34, 0x4e, 0x68, 0x75, 0x82,
88 0x1a, 0x34, 0x4e, 0x68, 0x9c, 0xd0, 0xea, 0x104 },
89
90
91 { 0x0e, 0x1c, 0x2b, 0x39, 0x56, 0x73, 0x82, 0x90,
92 0x1c, 0x39, 0x56, 0x73, 0xad, 0xe7, 0x104, 0x120 }
93};
94
95
96static const u16 ac_mcs_rate_nss1[8][10] = {
97
98 { 0x75, 0xEA, 0x15F, 0x1D4, 0x2BE, 0x3A8, 0x41D,
99 0x492, 0x57C, 0x618 },
100
101
102 { 0x82, 0x104, 0x186, 0x208, 0x30C, 0x410, 0x492,
103 0x514, 0x618, 0x6C6 },
104
105
106 { 0x3B, 0x75, 0xB0, 0xEA, 0x15F, 0x1D4, 0x20F,
107 0x249, 0x2BE, 0x30C },
108
109
110 { 0x41, 0x82, 0xC3, 0x104, 0x186, 0x208, 0x249,
111 0x28A, 0x30C, 0x363 },
112
113
114 { 0x1B, 0x36, 0x51, 0x6C, 0xA2, 0xD8, 0xF3,
115 0x10E, 0x144, 0x168 },
116
117
118 { 0x1E, 0x3C, 0x5A, 0x78, 0xB4, 0xF0, 0x10E,
119 0x12C, 0x168, 0x190 },
120
121
122 { 0xD, 0x1A, 0x27, 0x34, 0x4E, 0x68, 0x75, 0x82, 0x9C, 0x00 },
123
124
125 { 0xF, 0x1D, 0x2C, 0x3A, 0x57, 0x74, 0x82, 0x91, 0xAE, 0x00 },
126};
127
128
129static const u16 ac_mcs_rate_nss2[8][10] = {
130
131 { 0xEA, 0x1D4, 0x2BE, 0x3A8, 0x57C, 0x750, 0x83A,
132 0x924, 0xAF8, 0xC30 },
133
134
135 { 0x104, 0x208, 0x30C, 0x410, 0x618, 0x820, 0x924,
136 0xA28, 0xC30, 0xD8B },
137
138
139 { 0x75, 0xEA, 0x15F, 0x1D4, 0x2BE, 0x3A8, 0x41D,
140 0x492, 0x57C, 0x618 },
141
142
143 { 0x82, 0x104, 0x186, 0x208, 0x30C, 0x410, 0x492,
144 0x514, 0x618, 0x6C6 },
145
146
147 { 0x36, 0x6C, 0xA2, 0xD8, 0x144, 0x1B0, 0x1E6,
148 0x21C, 0x288, 0x2D0 },
149
150
151 { 0x3C, 0x78, 0xB4, 0xF0, 0x168, 0x1E0, 0x21C,
152 0x258, 0x2D0, 0x320 },
153
154
155 { 0x1A, 0x34, 0x4A, 0x68, 0x9C, 0xD0, 0xEA, 0x104,
156 0x138, 0x00 },
157
158
159 { 0x1D, 0x3A, 0x57, 0x74, 0xAE, 0xE6, 0x104, 0x121,
160 0x15B, 0x00 },
161};
162
163struct region_code_mapping {
164 u8 code;
165 u8 region[IEEE80211_COUNTRY_STRING_LEN];
166};
167
168static struct region_code_mapping region_code_mapping_t[] = {
169 { 0x10, "US " },
170 { 0x20, "CA " },
171 { 0x30, "EU " },
172 { 0x31, "ES " },
173 { 0x32, "FR " },
174 { 0x40, "JP " },
175 { 0x41, "JP " },
176 { 0x50, "CN " },
177};
178
179
180u8 *mwifiex_11d_code_2_region(u8 code)
181{
182 u8 i;
183 u8 size = sizeof(region_code_mapping_t)/
184 sizeof(struct region_code_mapping);
185
186
187 for (i = 0; i < size; i++)
188 if (region_code_mapping_t[i].code == code)
189 return region_code_mapping_t[i].region;
190
191 return NULL;
192}
193
194
195
196
197
198u32 mwifiex_index_to_acs_data_rate(struct mwifiex_private *priv,
199 u8 index, u8 ht_info)
200{
201 u32 rate = 0;
202 u8 mcs_index = 0;
203 u8 bw = 0;
204 u8 gi = 0;
205
206 if ((ht_info & 0x3) == MWIFIEX_RATE_FORMAT_VHT) {
207 mcs_index = min(index & 0xF, 9);
208
209
210 bw = (ht_info & 0xC) >> 2;
211
212
213 gi = (ht_info & 0x10) >> 4;
214
215 if ((index >> 4) == 1)
216 rate = ac_mcs_rate_nss2[2 * (3 - bw) + gi][mcs_index];
217 else
218 rate = ac_mcs_rate_nss1[2 * (3 - bw) + gi][mcs_index];
219 } else if ((ht_info & 0x3) == MWIFIEX_RATE_FORMAT_HT) {
220
221 bw = (ht_info & 0xC) >> 2;
222
223
224 gi = (ht_info & 0x10) >> 4;
225
226 if (index == MWIFIEX_RATE_BITMAP_MCS0) {
227 if (gi == 1)
228 rate = 0x0D;
229 else
230 rate = 0x0C;
231 } else if (index < 16) {
232 if ((bw == 1) || (bw == 0))
233 rate = mcs_rate[2 * (1 - bw) + gi][index];
234 else
235 rate = mwifiex_data_rates[0];
236 } else {
237 rate = mwifiex_data_rates[0];
238 }
239 } else {
240
241 if (index >= MWIFIEX_SUPPORTED_RATES_EXT)
242 index = 0;
243 rate = mwifiex_data_rates[index];
244 }
245
246 return rate;
247}
248
249
250
251
252u32 mwifiex_index_to_data_rate(struct mwifiex_private *priv,
253 u8 index, u8 ht_info)
254{
255 u32 mcs_num_supp =
256 (priv->adapter->user_dev_mcs_support == HT_STREAM_2X2) ? 16 : 8;
257 u32 rate;
258
259 if (priv->adapter->is_hw_11ac_capable)
260 return mwifiex_index_to_acs_data_rate(priv, index, ht_info);
261
262 if (ht_info & BIT(0)) {
263 if (index == MWIFIEX_RATE_BITMAP_MCS0) {
264 if (ht_info & BIT(2))
265 rate = 0x0D;
266 else
267 rate = 0x0C;
268 } else if (index < mcs_num_supp) {
269 if (ht_info & BIT(1)) {
270 if (ht_info & BIT(2))
271
272 rate = mcs_rate[1][index];
273 else
274
275 rate = mcs_rate[0][index];
276 } else {
277 if (ht_info & BIT(2))
278
279 rate = mcs_rate[3][index];
280 else
281
282 rate = mcs_rate[2][index];
283 }
284 } else
285 rate = mwifiex_data_rates[0];
286 } else {
287 if (index >= MWIFIEX_SUPPORTED_RATES_EXT)
288 index = 0;
289 rate = mwifiex_data_rates[index];
290 }
291 return rate;
292}
293
294
295
296
297
298
299u32 mwifiex_get_active_data_rates(struct mwifiex_private *priv, u8 *rates)
300{
301 if (!priv->media_connected)
302 return mwifiex_get_supported_rates(priv, rates);
303 else
304 return mwifiex_copy_rates(rates, 0,
305 priv->curr_bss_params.data_rates,
306 priv->curr_bss_params.num_of_rates);
307}
308
309
310
311
312
313struct mwifiex_chan_freq_power *
314mwifiex_get_cfp(struct mwifiex_private *priv, u8 band, u16 channel, u32 freq)
315{
316 struct mwifiex_chan_freq_power *cfp = NULL;
317 struct ieee80211_supported_band *sband;
318 struct ieee80211_channel *ch = NULL;
319 int i;
320
321 if (!channel && !freq)
322 return cfp;
323
324 if (mwifiex_band_to_radio_type(band) == HostCmd_SCAN_RADIO_TYPE_BG)
325 sband = priv->wdev.wiphy->bands[IEEE80211_BAND_2GHZ];
326 else
327 sband = priv->wdev.wiphy->bands[IEEE80211_BAND_5GHZ];
328
329 if (!sband) {
330 dev_err(priv->adapter->dev, "%s: cannot find cfp by band %d\n",
331 __func__, band);
332 return cfp;
333 }
334
335 for (i = 0; i < sband->n_channels; i++) {
336 ch = &sband->channels[i];
337
338 if (ch->flags & IEEE80211_CHAN_DISABLED)
339 continue;
340
341 if (freq) {
342 if (ch->center_freq == freq)
343 break;
344 } else {
345
346 if (ch->hw_value == channel ||
347 channel == FIRST_VALID_CHANNEL)
348 break;
349 }
350 }
351 if (i == sband->n_channels) {
352 dev_err(priv->adapter->dev, "%s: cannot find cfp by band %d"
353 " & channel=%d freq=%d\n", __func__, band, channel,
354 freq);
355 } else {
356 if (!ch)
357 return cfp;
358
359 priv->cfp.channel = ch->hw_value;
360 priv->cfp.freq = ch->center_freq;
361 priv->cfp.max_tx_power = ch->max_power;
362 cfp = &priv->cfp;
363 }
364
365 return cfp;
366}
367
368
369
370
371u8
372mwifiex_is_rate_auto(struct mwifiex_private *priv)
373{
374 u32 i;
375 int rate_num = 0;
376
377 for (i = 0; i < ARRAY_SIZE(priv->bitmap_rates); i++)
378 if (priv->bitmap_rates[i])
379 rate_num++;
380
381 if (rate_num > 1)
382 return true;
383 else
384 return false;
385}
386
387
388
389
390u32 mwifiex_get_rates_from_cfg80211(struct mwifiex_private *priv,
391 u8 *rates, u8 radio_type)
392{
393 struct wiphy *wiphy = priv->adapter->wiphy;
394 struct cfg80211_scan_request *request = priv->scan_request;
395 u32 num_rates, rate_mask;
396 struct ieee80211_supported_band *sband;
397 int i;
398
399 if (radio_type) {
400 sband = wiphy->bands[IEEE80211_BAND_5GHZ];
401 if (WARN_ON_ONCE(!sband))
402 return 0;
403 rate_mask = request->rates[IEEE80211_BAND_5GHZ];
404 } else {
405 sband = wiphy->bands[IEEE80211_BAND_2GHZ];
406 if (WARN_ON_ONCE(!sband))
407 return 0;
408 rate_mask = request->rates[IEEE80211_BAND_2GHZ];
409 }
410
411 num_rates = 0;
412 for (i = 0; i < sband->n_bitrates; i++) {
413 if ((BIT(i) & rate_mask) == 0)
414 continue;
415 rates[num_rates++] = (u8)(sband->bitrates[i].bitrate / 5);
416 }
417
418 return num_rates;
419}
420
421
422
423
424
425u32 mwifiex_get_supported_rates(struct mwifiex_private *priv, u8 *rates)
426{
427 u32 k = 0;
428 struct mwifiex_adapter *adapter = priv->adapter;
429
430 if (priv->bss_mode == NL80211_IFTYPE_STATION ||
431 priv->bss_mode == NL80211_IFTYPE_P2P_CLIENT) {
432 switch (adapter->config_bands) {
433 case BAND_B:
434 dev_dbg(adapter->dev, "info: infra band=%d "
435 "supported_rates_b\n", adapter->config_bands);
436 k = mwifiex_copy_rates(rates, k, supported_rates_b,
437 sizeof(supported_rates_b));
438 break;
439 case BAND_G:
440 case BAND_G | BAND_GN:
441 dev_dbg(adapter->dev, "info: infra band=%d "
442 "supported_rates_g\n", adapter->config_bands);
443 k = mwifiex_copy_rates(rates, k, supported_rates_g,
444 sizeof(supported_rates_g));
445 break;
446 case BAND_B | BAND_G:
447 case BAND_A | BAND_B | BAND_G:
448 case BAND_A | BAND_B:
449 case BAND_A | BAND_B | BAND_G | BAND_GN | BAND_AN:
450 case BAND_A | BAND_B | BAND_G | BAND_GN | BAND_AN | BAND_AAC:
451 case BAND_B | BAND_G | BAND_GN:
452 dev_dbg(adapter->dev, "info: infra band=%d "
453 "supported_rates_bg\n", adapter->config_bands);
454 k = mwifiex_copy_rates(rates, k, supported_rates_bg,
455 sizeof(supported_rates_bg));
456 break;
457 case BAND_A:
458 case BAND_A | BAND_G:
459 dev_dbg(adapter->dev, "info: infra band=%d "
460 "supported_rates_a\n", adapter->config_bands);
461 k = mwifiex_copy_rates(rates, k, supported_rates_a,
462 sizeof(supported_rates_a));
463 break;
464 case BAND_AN:
465 case BAND_A | BAND_AN:
466 case BAND_A | BAND_AN | BAND_AAC:
467 case BAND_A | BAND_G | BAND_AN | BAND_GN:
468 case BAND_A | BAND_G | BAND_AN | BAND_GN | BAND_AAC:
469 dev_dbg(adapter->dev, "info: infra band=%d "
470 "supported_rates_a\n", adapter->config_bands);
471 k = mwifiex_copy_rates(rates, k, supported_rates_a,
472 sizeof(supported_rates_a));
473 break;
474 case BAND_GN:
475 dev_dbg(adapter->dev, "info: infra band=%d "
476 "supported_rates_n\n", adapter->config_bands);
477 k = mwifiex_copy_rates(rates, k, supported_rates_n,
478 sizeof(supported_rates_n));
479 break;
480 }
481 } else {
482
483 switch (adapter->adhoc_start_band) {
484 case BAND_B:
485 dev_dbg(adapter->dev, "info: adhoc B\n");
486 k = mwifiex_copy_rates(rates, k, adhoc_rates_b,
487 sizeof(adhoc_rates_b));
488 break;
489 case BAND_G:
490 case BAND_G | BAND_GN:
491 dev_dbg(adapter->dev, "info: adhoc G only\n");
492 k = mwifiex_copy_rates(rates, k, adhoc_rates_g,
493 sizeof(adhoc_rates_g));
494 break;
495 case BAND_B | BAND_G:
496 case BAND_B | BAND_G | BAND_GN:
497 dev_dbg(adapter->dev, "info: adhoc BG\n");
498 k = mwifiex_copy_rates(rates, k, adhoc_rates_bg,
499 sizeof(adhoc_rates_bg));
500 break;
501 case BAND_A:
502 case BAND_A | BAND_AN:
503 dev_dbg(adapter->dev, "info: adhoc A\n");
504 k = mwifiex_copy_rates(rates, k, adhoc_rates_a,
505 sizeof(adhoc_rates_a));
506 break;
507 }
508 }
509
510 return k;
511}
512
513u8 mwifiex_adjust_data_rate(struct mwifiex_private *priv,
514 u8 rx_rate, u8 rate_info)
515{
516 u8 rate_index = 0;
517
518
519 if ((rate_info & BIT(0)) && (rate_info & BIT(1)))
520 rate_index = MWIFIEX_RATE_INDEX_MCS0 +
521 MWIFIEX_BW20_MCS_NUM + rx_rate;
522 else if (rate_info & BIT(0))
523 rate_index = MWIFIEX_RATE_INDEX_MCS0 + rx_rate;
524 else
525 rate_index = (rx_rate > MWIFIEX_RATE_INDEX_OFDM0) ?
526 rx_rate - 1 : rx_rate;
527
528 return rate_index;
529}
530