cfp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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_data_rate(struct mwifiex_private *priv, u8 index,
  93. 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. u32 mcs_num_supp =
  114. (priv->adapter->hw_dev_mcs_support == HT_STREAM_2X2) ? 16 : 8;
  115. u32 rate;
  116. if (ht_info & BIT(0)) {
  117. if (index == MWIFIEX_RATE_BITMAP_MCS0) {
  118. if (ht_info & BIT(2))
  119. rate = 0x0D; /* MCS 32 SGI rate */
  120. else
  121. rate = 0x0C; /* MCS 32 LGI rate */
  122. } else if (index < mcs_num_supp) {
  123. if (ht_info & BIT(1)) {
  124. if (ht_info & BIT(2))
  125. /* SGI, 40M */
  126. rate = mcs_rate[1][index];
  127. else
  128. /* LGI, 40M */
  129. rate = mcs_rate[0][index];
  130. } else {
  131. if (ht_info & BIT(2))
  132. /* SGI, 20M */
  133. rate = mcs_rate[3][index];
  134. else
  135. /* LGI, 20M */
  136. rate = mcs_rate[2][index];
  137. }
  138. } else
  139. rate = mwifiex_data_rates[0];
  140. } else {
  141. if (index >= MWIFIEX_SUPPORTED_RATES_EXT)
  142. index = 0;
  143. rate = mwifiex_data_rates[index];
  144. }
  145. return rate;
  146. }
  147. /*
  148. * This function maps a data rate value into corresponding index in supported
  149. * rates table.
  150. */
  151. u8 mwifiex_data_rate_to_index(u32 rate)
  152. {
  153. u16 *ptr;
  154. if (rate) {
  155. ptr = memchr(mwifiex_data_rates, rate,
  156. sizeof(mwifiex_data_rates));
  157. if (ptr)
  158. return (u8) (ptr - mwifiex_data_rates);
  159. }
  160. return 0;
  161. }
  162. /*
  163. * This function returns the current active data rates.
  164. *
  165. * The result may vary depending upon connection status.
  166. */
  167. u32 mwifiex_get_active_data_rates(struct mwifiex_private *priv, u8 *rates)
  168. {
  169. if (!priv->media_connected)
  170. return mwifiex_get_supported_rates(priv, rates);
  171. else
  172. return mwifiex_copy_rates(rates, 0,
  173. priv->curr_bss_params.data_rates,
  174. priv->curr_bss_params.num_of_rates);
  175. }
  176. /*
  177. * This function locates the Channel-Frequency-Power triplet based upon
  178. * band and channel/frequency parameters.
  179. */
  180. struct mwifiex_chan_freq_power *
  181. mwifiex_get_cfp(struct mwifiex_private *priv, u8 band, u16 channel, u32 freq)
  182. {
  183. struct mwifiex_chan_freq_power *cfp = NULL;
  184. struct ieee80211_supported_band *sband;
  185. struct ieee80211_channel *ch = NULL;
  186. int i;
  187. if (!channel && !freq)
  188. return cfp;
  189. if (mwifiex_band_to_radio_type(band) == HostCmd_SCAN_RADIO_TYPE_BG)
  190. sband = priv->wdev->wiphy->bands[IEEE80211_BAND_2GHZ];
  191. else
  192. sband = priv->wdev->wiphy->bands[IEEE80211_BAND_5GHZ];
  193. if (!sband) {
  194. dev_err(priv->adapter->dev, "%s: cannot find cfp by band %d\n",
  195. __func__, band);
  196. return cfp;
  197. }
  198. for (i = 0; i < sband->n_channels; i++) {
  199. ch = &sband->channels[i];
  200. if (ch->flags & IEEE80211_CHAN_DISABLED)
  201. continue;
  202. if (freq) {
  203. if (ch->center_freq == freq)
  204. break;
  205. } else {
  206. /* find by valid channel*/
  207. if (ch->hw_value == channel ||
  208. channel == FIRST_VALID_CHANNEL)
  209. break;
  210. }
  211. }
  212. if (i == sband->n_channels) {
  213. dev_err(priv->adapter->dev, "%s: cannot find cfp by band %d"
  214. " & channel=%d freq=%d\n", __func__, band, channel,
  215. freq);
  216. } else {
  217. if (!ch)
  218. return cfp;
  219. priv->cfp.channel = ch->hw_value;
  220. priv->cfp.freq = ch->center_freq;
  221. priv->cfp.max_tx_power = ch->max_power;
  222. cfp = &priv->cfp;
  223. }
  224. return cfp;
  225. }
  226. /*
  227. * This function checks if the data rate is set to auto.
  228. */
  229. u8
  230. mwifiex_is_rate_auto(struct mwifiex_private *priv)
  231. {
  232. u32 i;
  233. int rate_num = 0;
  234. for (i = 0; i < ARRAY_SIZE(priv->bitmap_rates); i++)
  235. if (priv->bitmap_rates[i])
  236. rate_num++;
  237. if (rate_num > 1)
  238. return true;
  239. else
  240. return false;
  241. }
  242. /*
  243. * This function converts rate bitmap into rate index.
  244. */
  245. int mwifiex_get_rate_index(u16 *rate_bitmap, int size)
  246. {
  247. int i;
  248. for (i = 0; i < size * 8; i++)
  249. if (rate_bitmap[i / 16] & (1 << (i % 16)))
  250. return i;
  251. return 0;
  252. }
  253. /*
  254. * This function gets the supported data rates.
  255. *
  256. * The function works in both Ad-Hoc and infra mode by printing the
  257. * band and returning the data rates.
  258. */
  259. u32 mwifiex_get_supported_rates(struct mwifiex_private *priv, u8 *rates)
  260. {
  261. u32 k = 0;
  262. struct mwifiex_adapter *adapter = priv->adapter;
  263. if (priv->bss_mode == NL80211_IFTYPE_STATION) {
  264. switch (adapter->config_bands) {
  265. case BAND_B:
  266. dev_dbg(adapter->dev, "info: infra band=%d "
  267. "supported_rates_b\n", adapter->config_bands);
  268. k = mwifiex_copy_rates(rates, k, supported_rates_b,
  269. sizeof(supported_rates_b));
  270. break;
  271. case BAND_G:
  272. case BAND_G | BAND_GN:
  273. dev_dbg(adapter->dev, "info: infra band=%d "
  274. "supported_rates_g\n", adapter->config_bands);
  275. k = mwifiex_copy_rates(rates, k, supported_rates_g,
  276. sizeof(supported_rates_g));
  277. break;
  278. case BAND_B | BAND_G:
  279. case BAND_A | BAND_B | BAND_G:
  280. case BAND_A | BAND_B:
  281. case BAND_A | BAND_B | BAND_G | BAND_GN | BAND_AN:
  282. case BAND_B | BAND_G | BAND_GN:
  283. dev_dbg(adapter->dev, "info: infra band=%d "
  284. "supported_rates_bg\n", adapter->config_bands);
  285. k = mwifiex_copy_rates(rates, k, supported_rates_bg,
  286. sizeof(supported_rates_bg));
  287. break;
  288. case BAND_A:
  289. case BAND_A | BAND_G:
  290. dev_dbg(adapter->dev, "info: infra band=%d "
  291. "supported_rates_a\n", adapter->config_bands);
  292. k = mwifiex_copy_rates(rates, k, supported_rates_a,
  293. sizeof(supported_rates_a));
  294. break;
  295. case BAND_A | BAND_AN:
  296. case BAND_A | BAND_G | BAND_AN | BAND_GN:
  297. dev_dbg(adapter->dev, "info: infra band=%d "
  298. "supported_rates_a\n", adapter->config_bands);
  299. k = mwifiex_copy_rates(rates, k, supported_rates_a,
  300. sizeof(supported_rates_a));
  301. break;
  302. case BAND_GN:
  303. dev_dbg(adapter->dev, "info: infra band=%d "
  304. "supported_rates_n\n", adapter->config_bands);
  305. k = mwifiex_copy_rates(rates, k, supported_rates_n,
  306. sizeof(supported_rates_n));
  307. break;
  308. }
  309. } else {
  310. /* Ad-hoc mode */
  311. switch (adapter->adhoc_start_band) {
  312. case BAND_B:
  313. dev_dbg(adapter->dev, "info: adhoc B\n");
  314. k = mwifiex_copy_rates(rates, k, adhoc_rates_b,
  315. sizeof(adhoc_rates_b));
  316. break;
  317. case BAND_G:
  318. case BAND_G | BAND_GN:
  319. dev_dbg(adapter->dev, "info: adhoc G only\n");
  320. k = mwifiex_copy_rates(rates, k, adhoc_rates_g,
  321. sizeof(adhoc_rates_g));
  322. break;
  323. case BAND_B | BAND_G:
  324. case BAND_B | BAND_G | BAND_GN:
  325. dev_dbg(adapter->dev, "info: adhoc BG\n");
  326. k = mwifiex_copy_rates(rates, k, adhoc_rates_bg,
  327. sizeof(adhoc_rates_bg));
  328. break;
  329. case BAND_A:
  330. case BAND_A | BAND_AN:
  331. dev_dbg(adapter->dev, "info: adhoc A\n");
  332. k = mwifiex_copy_rates(rates, k, adhoc_rates_a,
  333. sizeof(adhoc_rates_a));
  334. break;
  335. }
  336. }
  337. return k;
  338. }