cfp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * Marvell Wireless LAN device driver: Channel, Frequence and Power
  3. *
  4. * Copyright (C) 2011, Marvell International Ltd.
  5. *
  6. * This software file (the "File") is distributed by Marvell International
  7. * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  8. * (the "License"). You may use, redistribute and/or modify this File in
  9. * accordance with the terms and conditions of the License, a copy of which
  10. * is available by writing to the Free Software Foundation, Inc.,
  11. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
  12. * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  13. *
  14. * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
  17. * this warranty disclaimer.
  18. */
  19. #include "decl.h"
  20. #include "ioctl.h"
  21. #include "util.h"
  22. #include "fw.h"
  23. #include "main.h"
  24. #include "cfg80211.h"
  25. /* 100mW */
  26. #define MWIFIEX_TX_PWR_DEFAULT 20
  27. /* 100mW */
  28. #define MWIFIEX_TX_PWR_US_DEFAULT 20
  29. /* 50mW */
  30. #define MWIFIEX_TX_PWR_JP_DEFAULT 16
  31. /* 100mW */
  32. #define MWIFIEX_TX_PWR_FR_100MW 20
  33. /* 10mW */
  34. #define MWIFIEX_TX_PWR_FR_10MW 10
  35. /* 100mW */
  36. #define MWIFIEX_TX_PWR_EMEA_DEFAULT 20
  37. static u8 adhoc_rates_b[B_SUPPORTED_RATES] = { 0x82, 0x84, 0x8b, 0x96, 0 };
  38. static u8 adhoc_rates_g[G_SUPPORTED_RATES] = { 0x8c, 0x12, 0x98, 0x24,
  39. 0xb0, 0x48, 0x60, 0x6c, 0 };
  40. static u8 adhoc_rates_bg[BG_SUPPORTED_RATES] = { 0x82, 0x84, 0x8b, 0x96,
  41. 0x0c, 0x12, 0x18, 0x24,
  42. 0x30, 0x48, 0x60, 0x6c, 0 };
  43. static u8 adhoc_rates_a[A_SUPPORTED_RATES] = { 0x8c, 0x12, 0x98, 0x24,
  44. 0xb0, 0x48, 0x60, 0x6c, 0 };
  45. static u8 supported_rates_a[A_SUPPORTED_RATES] = { 0x0c, 0x12, 0x18, 0x24,
  46. 0xb0, 0x48, 0x60, 0x6c, 0 };
  47. static u16 mwifiex_data_rates[MWIFIEX_SUPPORTED_RATES_EXT] = { 0x02, 0x04,
  48. 0x0B, 0x16, 0x00, 0x0C, 0x12, 0x18,
  49. 0x24, 0x30, 0x48, 0x60, 0x6C, 0x90,
  50. 0x0D, 0x1A, 0x27, 0x34, 0x4E, 0x68,
  51. 0x75, 0x82, 0x0C, 0x1B, 0x36, 0x51,
  52. 0x6C, 0xA2, 0xD8, 0xF3, 0x10E, 0x00 };
  53. static u8 supported_rates_b[B_SUPPORTED_RATES] = { 0x02, 0x04, 0x0b, 0x16, 0 };
  54. static u8 supported_rates_g[G_SUPPORTED_RATES] = { 0x0c, 0x12, 0x18, 0x24,
  55. 0x30, 0x48, 0x60, 0x6c, 0 };
  56. static u8 supported_rates_bg[BG_SUPPORTED_RATES] = { 0x02, 0x04, 0x0b, 0x0c,
  57. 0x12, 0x16, 0x18, 0x24, 0x30, 0x48,
  58. 0x60, 0x6c, 0 };
  59. u16 region_code_index[MWIFIEX_MAX_REGION_CODE] = { 0x10, 0x20, 0x30,
  60. 0x32, 0x40, 0x41, 0xff };
  61. static u8 supported_rates_n[N_SUPPORTED_RATES] = { 0x02, 0x04, 0 };
  62. struct region_code_mapping {
  63. u8 code;
  64. u8 region[IEEE80211_COUNTRY_STRING_LEN];
  65. };
  66. static struct region_code_mapping region_code_mapping_t[] = {
  67. { 0x10, "US " }, /* US FCC */
  68. { 0x20, "CA " }, /* IC Canada */
  69. { 0x30, "EU " }, /* ETSI */
  70. { 0x31, "ES " }, /* Spain */
  71. { 0x32, "FR " }, /* France */
  72. { 0x40, "JP " }, /* Japan */
  73. { 0x41, "JP " }, /* Japan */
  74. { 0x50, "CN " }, /* China */
  75. };
  76. /* This function converts integer code to region string */
  77. u8 *mwifiex_11d_code_2_region(u8 code)
  78. {
  79. u8 i;
  80. u8 size = sizeof(region_code_mapping_t)/
  81. sizeof(struct region_code_mapping);
  82. /* Look for code in mapping table */
  83. for (i = 0; i < size; i++)
  84. if (region_code_mapping_t[i].code == code)
  85. return region_code_mapping_t[i].region;
  86. return NULL;
  87. }
  88. /*
  89. * This function maps an index in supported rates table into
  90. * the corresponding data rate.
  91. */
  92. u32 mwifiex_index_to_acs_data_rate(struct mwifiex_private *priv,
  93. u8 index, u8 ht_info)
  94. {
  95. /*
  96. * For every mcs_rate line, the first 8 bytes are for stream 1x1,
  97. * and all 16 bytes are for stream 2x2.
  98. */
  99. u16 mcs_rate[4][16] = {
  100. /* LGI 40M */
  101. { 0x1b, 0x36, 0x51, 0x6c, 0xa2, 0xd8, 0xf3, 0x10e,
  102. 0x36, 0x6c, 0xa2, 0xd8, 0x144, 0x1b0, 0x1e6, 0x21c },
  103. /* SGI 40M */
  104. { 0x1e, 0x3c, 0x5a, 0x78, 0xb4, 0xf0, 0x10e, 0x12c,
  105. 0x3c, 0x78, 0xb4, 0xf0, 0x168, 0x1e0, 0x21c, 0x258 },
  106. /* LGI 20M */
  107. { 0x0d, 0x1a, 0x27, 0x34, 0x4e, 0x68, 0x75, 0x82,
  108. 0x1a, 0x34, 0x4e, 0x68, 0x9c, 0xd0, 0xea, 0x104 },
  109. /* SGI 20M */
  110. { 0x0e, 0x1c, 0x2b, 0x39, 0x56, 0x73, 0x82, 0x90,
  111. 0x1c, 0x39, 0x56, 0x73, 0xad, 0xe7, 0x104, 0x120 }
  112. };
  113. /* AC rates */
  114. u16 ac_mcs_rate_nss1[8][10] = {
  115. /* LG 160M */
  116. { 0x75, 0xEA, 0x15F, 0x1D4, 0x2BE, 0x3A8, 0x41D,
  117. 0x492, 0x57C, 0x618 },
  118. /* SG 160M */
  119. { 0x82, 0x104, 0x186, 0x208, 0x30C, 0x410, 0x492,
  120. 0x514, 0x618, 0x6C6 },
  121. /* LG 80M */
  122. { 0x3B, 0x75, 0xB0, 0xEA, 0x15F, 0x1D4, 0x20F,
  123. 0x249, 0x2BE, 0x30C },
  124. /* SG 80M */
  125. { 0x41, 0x82, 0xC3, 0x104, 0x186, 0x208, 0x249,
  126. 0x28A, 0x30C, 0x363 },
  127. /* LG 40M */
  128. { 0x1B, 0x36, 0x51, 0x6C, 0xA2, 0xD8, 0xF3,
  129. 0x10E, 0x144, 0x168 },
  130. /* SG 40M */
  131. { 0x1E, 0x3C, 0x5A, 0x78, 0xB4, 0xF0, 0x10E,
  132. 0x12C, 0x168, 0x190 },
  133. /* LG 20M */
  134. { 0xD, 0x1A, 0x27, 0x34, 0x4E, 0x68, 0x75, 0x82, 0x9C, 0x00 },
  135. /* SG 20M */
  136. { 0xF, 0x1D, 0x2C, 0x3A, 0x57, 0x74, 0x82, 0x91, 0xAE, 0x00 },
  137. };
  138. /* NSS2 note: the value in the table is 2 multiplier of the actual
  139. * rate
  140. */
  141. u16 ac_mcs_rate_nss2[8][10] = {
  142. /* LG 160M */
  143. { 0xEA, 0x1D4, 0x2BE, 0x3A8, 0x57C, 0x750, 0x83A,
  144. 0x924, 0xAF8, 0xC30 },
  145. /* SG 160M */
  146. { 0x104, 0x208, 0x30C, 0x410, 0x618, 0x820, 0x924,
  147. 0xA28, 0xC30, 0xD8B },
  148. /* LG 80M */
  149. { 0x75, 0xEA, 0x15F, 0x1D4, 0x2BE, 0x3A8, 0x41D,
  150. 0x492, 0x57C, 0x618 },
  151. /* SG 80M */
  152. { 0x82, 0x104, 0x186, 0x208, 0x30C, 0x410, 0x492,
  153. 0x514, 0x618, 0x6C6 },
  154. /* LG 40M */
  155. { 0x36, 0x6C, 0xA2, 0xD8, 0x144, 0x1B0, 0x1E6,
  156. 0x21C, 0x288, 0x2D0 },
  157. /* SG 40M */
  158. { 0x3C, 0x78, 0xB4, 0xF0, 0x168, 0x1E0, 0x21C,
  159. 0x258, 0x2D0, 0x320 },
  160. /* LG 20M */
  161. { 0x1A, 0x34, 0x4A, 0x68, 0x9C, 0xD0, 0xEA, 0x104,
  162. 0x138, 0x00 },
  163. /* SG 20M */
  164. { 0x1D, 0x3A, 0x57, 0x74, 0xAE, 0xE6, 0x104, 0x121,
  165. 0x15B, 0x00 },
  166. };
  167. u32 rate = 0;
  168. u8 mcs_index = 0;
  169. u8 bw = 0;
  170. u8 gi = 0;
  171. if ((ht_info & 0x3) == MWIFIEX_RATE_FORMAT_VHT) {
  172. mcs_index = min(index & 0xF, 9);
  173. /* 20M: bw=0, 40M: bw=1, 80M: bw=2, 160M: bw=3 */
  174. bw = (ht_info & 0xC) >> 2;
  175. /* LGI: gi =0, SGI: gi = 1 */
  176. gi = (ht_info & 0x10) >> 4;
  177. if ((index >> 4) == 1) /* NSS = 2 */
  178. rate = ac_mcs_rate_nss2[2 * (3 - bw) + gi][mcs_index];
  179. else /* NSS = 1 */
  180. rate = ac_mcs_rate_nss1[2 * (3 - bw) + gi][mcs_index];
  181. } else if ((ht_info & 0x3) == MWIFIEX_RATE_FORMAT_HT) {
  182. /* 20M: bw=0, 40M: bw=1 */
  183. bw = (ht_info & 0xC) >> 2;
  184. /* LGI: gi =0, SGI: gi = 1 */
  185. gi = (ht_info & 0x10) >> 4;
  186. if (index == MWIFIEX_RATE_BITMAP_MCS0) {
  187. if (gi == 1)
  188. rate = 0x0D; /* MCS 32 SGI rate */
  189. else
  190. rate = 0x0C; /* MCS 32 LGI rate */
  191. } else if (index < 16) {
  192. if ((bw == 1) || (bw == 0))
  193. rate = mcs_rate[2 * (1 - bw) + gi][index];
  194. else
  195. rate = mwifiex_data_rates[0];
  196. } else {
  197. rate = mwifiex_data_rates[0];
  198. }
  199. } else {
  200. /* 11n non-HT rates */
  201. if (index >= MWIFIEX_SUPPORTED_RATES_EXT)
  202. index = 0;
  203. rate = mwifiex_data_rates[index];
  204. }
  205. return rate;
  206. }
  207. /* This function maps an index in supported rates table into
  208. * the corresponding data rate.
  209. */
  210. u32 mwifiex_index_to_data_rate(struct mwifiex_private *priv,
  211. u8 index, u8 ht_info)
  212. {
  213. /* For every mcs_rate line, the first 8 bytes are for stream 1x1,
  214. * and all 16 bytes are for stream 2x2.
  215. */
  216. u16 mcs_rate[4][16] = {
  217. /* LGI 40M */
  218. { 0x1b, 0x36, 0x51, 0x6c, 0xa2, 0xd8, 0xf3, 0x10e,
  219. 0x36, 0x6c, 0xa2, 0xd8, 0x144, 0x1b0, 0x1e6, 0x21c },
  220. /* SGI 40M */
  221. { 0x1e, 0x3c, 0x5a, 0x78, 0xb4, 0xf0, 0x10e, 0x12c,
  222. 0x3c, 0x78, 0xb4, 0xf0, 0x168, 0x1e0, 0x21c, 0x258 },
  223. /* LGI 20M */
  224. { 0x0d, 0x1a, 0x27, 0x34, 0x4e, 0x68, 0x75, 0x82,
  225. 0x1a, 0x34, 0x4e, 0x68, 0x9c, 0xd0, 0xea, 0x104 },
  226. /* SGI 20M */
  227. { 0x0e, 0x1c, 0x2b, 0x39, 0x56, 0x73, 0x82, 0x90,
  228. 0x1c, 0x39, 0x56, 0x73, 0xad, 0xe7, 0x104, 0x120 }
  229. };
  230. u32 mcs_num_supp =
  231. (priv->adapter->hw_dev_mcs_support == HT_STREAM_2X2) ? 16 : 8;
  232. u32 rate;
  233. if (priv->adapter->is_hw_11ac_capable)
  234. return mwifiex_index_to_acs_data_rate(priv, index, ht_info);
  235. if (ht_info & BIT(0)) {
  236. if (index == MWIFIEX_RATE_BITMAP_MCS0) {
  237. if (ht_info & BIT(2))
  238. rate = 0x0D; /* MCS 32 SGI rate */
  239. else
  240. rate = 0x0C; /* MCS 32 LGI rate */
  241. } else if (index < mcs_num_supp) {
  242. if (ht_info & BIT(1)) {
  243. if (ht_info & BIT(2))
  244. /* SGI, 40M */
  245. rate = mcs_rate[1][index];
  246. else
  247. /* LGI, 40M */
  248. rate = mcs_rate[0][index];
  249. } else {
  250. if (ht_info & BIT(2))
  251. /* SGI, 20M */
  252. rate = mcs_rate[3][index];
  253. else
  254. /* LGI, 20M */
  255. rate = mcs_rate[2][index];
  256. }
  257. } else
  258. rate = mwifiex_data_rates[0];
  259. } else {
  260. if (index >= MWIFIEX_SUPPORTED_RATES_EXT)
  261. index = 0;
  262. rate = mwifiex_data_rates[index];
  263. }
  264. return rate;
  265. }
  266. /*
  267. * This function returns the current active data rates.
  268. *
  269. * The result may vary depending upon connection status.
  270. */
  271. u32 mwifiex_get_active_data_rates(struct mwifiex_private *priv, u8 *rates)
  272. {
  273. if (!priv->media_connected)
  274. return mwifiex_get_supported_rates(priv, rates);
  275. else
  276. return mwifiex_copy_rates(rates, 0,
  277. priv->curr_bss_params.data_rates,
  278. priv->curr_bss_params.num_of_rates);
  279. }
  280. /*
  281. * This function locates the Channel-Frequency-Power triplet based upon
  282. * band and channel/frequency parameters.
  283. */
  284. struct mwifiex_chan_freq_power *
  285. mwifiex_get_cfp(struct mwifiex_private *priv, u8 band, u16 channel, u32 freq)
  286. {
  287. struct mwifiex_chan_freq_power *cfp = NULL;
  288. struct ieee80211_supported_band *sband;
  289. struct ieee80211_channel *ch = NULL;
  290. int i;
  291. if (!channel && !freq)
  292. return cfp;
  293. if (mwifiex_band_to_radio_type(band) == HostCmd_SCAN_RADIO_TYPE_BG)
  294. sband = priv->wdev->wiphy->bands[IEEE80211_BAND_2GHZ];
  295. else
  296. sband = priv->wdev->wiphy->bands[IEEE80211_BAND_5GHZ];
  297. if (!sband) {
  298. dev_err(priv->adapter->dev, "%s: cannot find cfp by band %d\n",
  299. __func__, band);
  300. return cfp;
  301. }
  302. for (i = 0; i < sband->n_channels; i++) {
  303. ch = &sband->channels[i];
  304. if (ch->flags & IEEE80211_CHAN_DISABLED)
  305. continue;
  306. if (freq) {
  307. if (ch->center_freq == freq)
  308. break;
  309. } else {
  310. /* find by valid channel*/
  311. if (ch->hw_value == channel ||
  312. channel == FIRST_VALID_CHANNEL)
  313. break;
  314. }
  315. }
  316. if (i == sband->n_channels) {
  317. dev_err(priv->adapter->dev, "%s: cannot find cfp by band %d"
  318. " & channel=%d freq=%d\n", __func__, band, channel,
  319. freq);
  320. } else {
  321. if (!ch)
  322. return cfp;
  323. priv->cfp.channel = ch->hw_value;
  324. priv->cfp.freq = ch->center_freq;
  325. priv->cfp.max_tx_power = ch->max_power;
  326. cfp = &priv->cfp;
  327. }
  328. return cfp;
  329. }
  330. /*
  331. * This function checks if the data rate is set to auto.
  332. */
  333. u8
  334. mwifiex_is_rate_auto(struct mwifiex_private *priv)
  335. {
  336. u32 i;
  337. int rate_num = 0;
  338. for (i = 0; i < ARRAY_SIZE(priv->bitmap_rates); i++)
  339. if (priv->bitmap_rates[i])
  340. rate_num++;
  341. if (rate_num > 1)
  342. return true;
  343. else
  344. return false;
  345. }
  346. /*
  347. * This function gets the supported data rates.
  348. *
  349. * The function works in both Ad-Hoc and infra mode by printing the
  350. * band and returning the data rates.
  351. */
  352. u32 mwifiex_get_supported_rates(struct mwifiex_private *priv, u8 *rates)
  353. {
  354. u32 k = 0;
  355. struct mwifiex_adapter *adapter = priv->adapter;
  356. if (priv->bss_mode == NL80211_IFTYPE_STATION) {
  357. switch (adapter->config_bands) {
  358. case BAND_B:
  359. dev_dbg(adapter->dev, "info: infra band=%d "
  360. "supported_rates_b\n", adapter->config_bands);
  361. k = mwifiex_copy_rates(rates, k, supported_rates_b,
  362. sizeof(supported_rates_b));
  363. break;
  364. case BAND_G:
  365. case BAND_G | BAND_GN:
  366. case BAND_G | BAND_GN | BAND_GAC:
  367. dev_dbg(adapter->dev, "info: infra band=%d "
  368. "supported_rates_g\n", adapter->config_bands);
  369. k = mwifiex_copy_rates(rates, k, supported_rates_g,
  370. sizeof(supported_rates_g));
  371. break;
  372. case BAND_B | BAND_G:
  373. case BAND_A | BAND_B | BAND_G:
  374. case BAND_A | BAND_B:
  375. case BAND_A | BAND_B | BAND_G | BAND_GN | BAND_AN:
  376. case BAND_A | BAND_B | BAND_G | BAND_GN | BAND_AN | BAND_AAC:
  377. case BAND_A | BAND_B | BAND_G | BAND_GN | BAND_AN |
  378. BAND_AAC | BAND_GAC:
  379. case BAND_B | BAND_G | BAND_GN:
  380. case BAND_B | BAND_G | BAND_GN | BAND_GAC:
  381. dev_dbg(adapter->dev, "info: infra band=%d "
  382. "supported_rates_bg\n", adapter->config_bands);
  383. k = mwifiex_copy_rates(rates, k, supported_rates_bg,
  384. sizeof(supported_rates_bg));
  385. break;
  386. case BAND_A:
  387. case BAND_A | BAND_G:
  388. dev_dbg(adapter->dev, "info: infra band=%d "
  389. "supported_rates_a\n", adapter->config_bands);
  390. k = mwifiex_copy_rates(rates, k, supported_rates_a,
  391. sizeof(supported_rates_a));
  392. break;
  393. case BAND_AN:
  394. case BAND_A | BAND_AN:
  395. case BAND_A | BAND_AN | BAND_AAC:
  396. case BAND_A | BAND_G | BAND_AN | BAND_GN:
  397. case BAND_A | BAND_G | BAND_AN | BAND_GN | BAND_AAC:
  398. dev_dbg(adapter->dev, "info: infra band=%d "
  399. "supported_rates_a\n", adapter->config_bands);
  400. k = mwifiex_copy_rates(rates, k, supported_rates_a,
  401. sizeof(supported_rates_a));
  402. break;
  403. case BAND_GN:
  404. case BAND_GN | BAND_GAC:
  405. dev_dbg(adapter->dev, "info: infra band=%d "
  406. "supported_rates_n\n", adapter->config_bands);
  407. k = mwifiex_copy_rates(rates, k, supported_rates_n,
  408. sizeof(supported_rates_n));
  409. break;
  410. }
  411. } else {
  412. /* Ad-hoc mode */
  413. switch (adapter->adhoc_start_band) {
  414. case BAND_B:
  415. dev_dbg(adapter->dev, "info: adhoc B\n");
  416. k = mwifiex_copy_rates(rates, k, adhoc_rates_b,
  417. sizeof(adhoc_rates_b));
  418. break;
  419. case BAND_G:
  420. case BAND_G | BAND_GN:
  421. dev_dbg(adapter->dev, "info: adhoc G only\n");
  422. k = mwifiex_copy_rates(rates, k, adhoc_rates_g,
  423. sizeof(adhoc_rates_g));
  424. break;
  425. case BAND_B | BAND_G:
  426. case BAND_B | BAND_G | BAND_GN:
  427. dev_dbg(adapter->dev, "info: adhoc BG\n");
  428. k = mwifiex_copy_rates(rates, k, adhoc_rates_bg,
  429. sizeof(adhoc_rates_bg));
  430. break;
  431. case BAND_A:
  432. case BAND_A | BAND_AN:
  433. dev_dbg(adapter->dev, "info: adhoc A\n");
  434. k = mwifiex_copy_rates(rates, k, adhoc_rates_a,
  435. sizeof(adhoc_rates_a));
  436. break;
  437. }
  438. }
  439. return k;
  440. }