cfp.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. /*
  63. * This function maps an index in supported rates table into
  64. * the corresponding data rate.
  65. */
  66. u32 mwifiex_index_to_data_rate(struct mwifiex_private *priv, u8 index,
  67. u8 ht_info)
  68. {
  69. /*
  70. * For every mcs_rate line, the first 8 bytes are for stream 1x1,
  71. * and all 16 bytes are for stream 2x2.
  72. */
  73. u16 mcs_rate[4][16] = {
  74. /* LGI 40M */
  75. { 0x1b, 0x36, 0x51, 0x6c, 0xa2, 0xd8, 0xf3, 0x10e,
  76. 0x36, 0x6c, 0xa2, 0xd8, 0x144, 0x1b0, 0x1e6, 0x21c },
  77. /* SGI 40M */
  78. { 0x1e, 0x3c, 0x5a, 0x78, 0xb4, 0xf0, 0x10e, 0x12c,
  79. 0x3c, 0x78, 0xb4, 0xf0, 0x168, 0x1e0, 0x21c, 0x258 },
  80. /* LGI 20M */
  81. { 0x0d, 0x1a, 0x27, 0x34, 0x4e, 0x68, 0x75, 0x82,
  82. 0x1a, 0x34, 0x4e, 0x68, 0x9c, 0xd0, 0xea, 0x104 },
  83. /* SGI 20M */
  84. { 0x0e, 0x1c, 0x2b, 0x39, 0x56, 0x73, 0x82, 0x90,
  85. 0x1c, 0x39, 0x56, 0x73, 0xad, 0xe7, 0x104, 0x120 }
  86. };
  87. u32 mcs_num_supp =
  88. (priv->adapter->hw_dev_mcs_support == HT_STREAM_2X2) ? 16 : 8;
  89. u32 rate;
  90. if (ht_info & BIT(0)) {
  91. if (index == MWIFIEX_RATE_BITMAP_MCS0) {
  92. if (ht_info & BIT(2))
  93. rate = 0x0D; /* MCS 32 SGI rate */
  94. else
  95. rate = 0x0C; /* MCS 32 LGI rate */
  96. } else if (index < mcs_num_supp) {
  97. if (ht_info & BIT(1)) {
  98. if (ht_info & BIT(2))
  99. /* SGI, 40M */
  100. rate = mcs_rate[1][index];
  101. else
  102. /* LGI, 40M */
  103. rate = mcs_rate[0][index];
  104. } else {
  105. if (ht_info & BIT(2))
  106. /* SGI, 20M */
  107. rate = mcs_rate[3][index];
  108. else
  109. /* LGI, 20M */
  110. rate = mcs_rate[2][index];
  111. }
  112. } else
  113. rate = mwifiex_data_rates[0];
  114. } else {
  115. if (index >= MWIFIEX_SUPPORTED_RATES_EXT)
  116. index = 0;
  117. rate = mwifiex_data_rates[index];
  118. }
  119. return rate;
  120. }
  121. /*
  122. * This function maps a data rate value into corresponding index in supported
  123. * rates table.
  124. */
  125. u8 mwifiex_data_rate_to_index(u32 rate)
  126. {
  127. u16 *ptr;
  128. if (rate) {
  129. ptr = memchr(mwifiex_data_rates, rate,
  130. sizeof(mwifiex_data_rates));
  131. if (ptr)
  132. return (u8) (ptr - mwifiex_data_rates);
  133. }
  134. return 0;
  135. }
  136. /*
  137. * This function returns the current active data rates.
  138. *
  139. * The result may vary depending upon connection status.
  140. */
  141. u32 mwifiex_get_active_data_rates(struct mwifiex_private *priv, u8 *rates)
  142. {
  143. if (!priv->media_connected)
  144. return mwifiex_get_supported_rates(priv, rates);
  145. else
  146. return mwifiex_copy_rates(rates, 0,
  147. priv->curr_bss_params.data_rates,
  148. priv->curr_bss_params.num_of_rates);
  149. }
  150. /*
  151. * This function locates the Channel-Frequency-Power triplet based upon
  152. * band and channel/frequency parameters.
  153. */
  154. struct mwifiex_chan_freq_power *
  155. mwifiex_get_cfp(struct mwifiex_private *priv, u8 band, u16 channel, u32 freq)
  156. {
  157. struct mwifiex_chan_freq_power *cfp = NULL;
  158. struct ieee80211_supported_band *sband;
  159. struct ieee80211_channel *ch = NULL;
  160. int i;
  161. if (!channel && !freq)
  162. return cfp;
  163. if (mwifiex_band_to_radio_type(band) == HostCmd_SCAN_RADIO_TYPE_BG)
  164. sband = priv->wdev->wiphy->bands[IEEE80211_BAND_2GHZ];
  165. else
  166. sband = priv->wdev->wiphy->bands[IEEE80211_BAND_5GHZ];
  167. if (!sband) {
  168. dev_err(priv->adapter->dev, "%s: cannot find cfp by band %d\n",
  169. __func__, band);
  170. return cfp;
  171. }
  172. for (i = 0; i < sband->n_channels; i++) {
  173. ch = &sband->channels[i];
  174. if (ch->flags & IEEE80211_CHAN_DISABLED)
  175. continue;
  176. if (freq) {
  177. if (ch->center_freq == freq)
  178. break;
  179. } else {
  180. /* find by valid channel*/
  181. if (ch->hw_value == channel ||
  182. channel == FIRST_VALID_CHANNEL)
  183. break;
  184. }
  185. }
  186. if (i == sband->n_channels) {
  187. dev_err(priv->adapter->dev, "%s: cannot find cfp by band %d"
  188. " & channel=%d freq=%d\n", __func__, band, channel,
  189. freq);
  190. } else {
  191. if (!ch)
  192. return cfp;
  193. priv->cfp.channel = ch->hw_value;
  194. priv->cfp.freq = ch->center_freq;
  195. priv->cfp.max_tx_power = ch->max_power;
  196. cfp = &priv->cfp;
  197. }
  198. return cfp;
  199. }
  200. /*
  201. * This function checks if the data rate is set to auto.
  202. */
  203. u8
  204. mwifiex_is_rate_auto(struct mwifiex_private *priv)
  205. {
  206. u32 i;
  207. int rate_num = 0;
  208. for (i = 0; i < ARRAY_SIZE(priv->bitmap_rates); i++)
  209. if (priv->bitmap_rates[i])
  210. rate_num++;
  211. if (rate_num > 1)
  212. return true;
  213. else
  214. return false;
  215. }
  216. /*
  217. * This function converts rate bitmap into rate index.
  218. */
  219. int mwifiex_get_rate_index(u16 *rate_bitmap, int size)
  220. {
  221. int i;
  222. for (i = 0; i < size * 8; i++)
  223. if (rate_bitmap[i / 16] & (1 << (i % 16)))
  224. return i;
  225. return 0;
  226. }
  227. /*
  228. * This function gets the supported data rates.
  229. *
  230. * The function works in both Ad-Hoc and infra mode by printing the
  231. * band and returning the data rates.
  232. */
  233. u32 mwifiex_get_supported_rates(struct mwifiex_private *priv, u8 *rates)
  234. {
  235. u32 k = 0;
  236. struct mwifiex_adapter *adapter = priv->adapter;
  237. if (priv->bss_mode == NL80211_IFTYPE_STATION) {
  238. switch (adapter->config_bands) {
  239. case BAND_B:
  240. dev_dbg(adapter->dev, "info: infra band=%d "
  241. "supported_rates_b\n", adapter->config_bands);
  242. k = mwifiex_copy_rates(rates, k, supported_rates_b,
  243. sizeof(supported_rates_b));
  244. break;
  245. case BAND_G:
  246. case BAND_G | BAND_GN:
  247. dev_dbg(adapter->dev, "info: infra band=%d "
  248. "supported_rates_g\n", adapter->config_bands);
  249. k = mwifiex_copy_rates(rates, k, supported_rates_g,
  250. sizeof(supported_rates_g));
  251. break;
  252. case BAND_B | BAND_G:
  253. case BAND_A | BAND_B | BAND_G:
  254. case BAND_A | BAND_B:
  255. case BAND_A | BAND_B | BAND_G | BAND_GN | BAND_AN:
  256. case BAND_B | BAND_G | BAND_GN:
  257. dev_dbg(adapter->dev, "info: infra band=%d "
  258. "supported_rates_bg\n", adapter->config_bands);
  259. k = mwifiex_copy_rates(rates, k, supported_rates_bg,
  260. sizeof(supported_rates_bg));
  261. break;
  262. case BAND_A:
  263. case BAND_A | BAND_G:
  264. dev_dbg(adapter->dev, "info: infra band=%d "
  265. "supported_rates_a\n", adapter->config_bands);
  266. k = mwifiex_copy_rates(rates, k, supported_rates_a,
  267. sizeof(supported_rates_a));
  268. break;
  269. case BAND_A | BAND_AN:
  270. case BAND_A | BAND_G | BAND_AN | BAND_GN:
  271. dev_dbg(adapter->dev, "info: infra band=%d "
  272. "supported_rates_a\n", adapter->config_bands);
  273. k = mwifiex_copy_rates(rates, k, supported_rates_a,
  274. sizeof(supported_rates_a));
  275. break;
  276. case BAND_GN:
  277. dev_dbg(adapter->dev, "info: infra band=%d "
  278. "supported_rates_n\n", adapter->config_bands);
  279. k = mwifiex_copy_rates(rates, k, supported_rates_n,
  280. sizeof(supported_rates_n));
  281. break;
  282. }
  283. } else {
  284. /* Ad-hoc mode */
  285. switch (adapter->adhoc_start_band) {
  286. case BAND_B:
  287. dev_dbg(adapter->dev, "info: adhoc B\n");
  288. k = mwifiex_copy_rates(rates, k, adhoc_rates_b,
  289. sizeof(adhoc_rates_b));
  290. break;
  291. case BAND_G:
  292. case BAND_G | BAND_GN:
  293. dev_dbg(adapter->dev, "info: adhoc G only\n");
  294. k = mwifiex_copy_rates(rates, k, adhoc_rates_g,
  295. sizeof(adhoc_rates_g));
  296. break;
  297. case BAND_B | BAND_G:
  298. case BAND_B | BAND_G | BAND_GN:
  299. dev_dbg(adapter->dev, "info: adhoc BG\n");
  300. k = mwifiex_copy_rates(rates, k, adhoc_rates_bg,
  301. sizeof(adhoc_rates_bg));
  302. break;
  303. case BAND_A:
  304. case BAND_A | BAND_AN:
  305. dev_dbg(adapter->dev, "info: adhoc A\n");
  306. k = mwifiex_copy_rates(rates, k, adhoc_rates_a,
  307. sizeof(adhoc_rates_a));
  308. break;
  309. }
  310. }
  311. return k;
  312. }