cfp.c 10.0 KB

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