| Bing Zhao | 5e6e3a9 | 2011-03-21 18:00:50 -0700 | [diff] [blame] | 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 |  | 
|  | 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 | /* 100mW */ | 
|  | 28 | #define MWIFIEX_TX_PWR_DEFAULT     20 | 
|  | 29 | /* 100mW */ | 
|  | 30 | #define MWIFIEX_TX_PWR_US_DEFAULT      20 | 
|  | 31 | /* 50mW */ | 
|  | 32 | #define MWIFIEX_TX_PWR_JP_DEFAULT      16 | 
|  | 33 | /* 100mW */ | 
|  | 34 | #define MWIFIEX_TX_PWR_FR_100MW        20 | 
|  | 35 | /* 10mW */ | 
|  | 36 | #define MWIFIEX_TX_PWR_FR_10MW         10 | 
|  | 37 | /* 100mW */ | 
|  | 38 | #define MWIFIEX_TX_PWR_EMEA_DEFAULT    20 | 
|  | 39 |  | 
|  | 40 | static u8 adhoc_rates_b[B_SUPPORTED_RATES] = { 0x82, 0x84, 0x8b, 0x96, 0 }; | 
|  | 41 |  | 
|  | 42 | static u8 adhoc_rates_g[G_SUPPORTED_RATES] = { 0x8c, 0x12, 0x98, 0x24, | 
|  | 43 | 0xb0, 0x48, 0x60, 0x6c, 0 }; | 
|  | 44 |  | 
|  | 45 | static u8 adhoc_rates_bg[BG_SUPPORTED_RATES] = { 0x82, 0x84, 0x8b, 0x96, | 
|  | 46 | 0x0c, 0x12, 0x18, 0x24, | 
|  | 47 | 0x30, 0x48, 0x60, 0x6c, 0 }; | 
|  | 48 |  | 
|  | 49 | static u8 adhoc_rates_a[A_SUPPORTED_RATES] = { 0x8c, 0x12, 0x98, 0x24, | 
|  | 50 | 0xb0, 0x48, 0x60, 0x6c, 0 }; | 
|  | 51 | u8 supported_rates_a[A_SUPPORTED_RATES] = { 0x0c, 0x12, 0x18, 0x24, | 
|  | 52 | 0xb0, 0x48, 0x60, 0x6c, 0 }; | 
|  | 53 | static 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 |  | 
|  | 60 | u8 supported_rates_b[B_SUPPORTED_RATES] = { 0x02, 0x04, 0x0b, 0x16, 0 }; | 
|  | 61 |  | 
|  | 62 | u8 supported_rates_g[G_SUPPORTED_RATES] = { 0x0c, 0x12, 0x18, 0x24, | 
|  | 63 | 0x30, 0x48, 0x60, 0x6c, 0 }; | 
|  | 64 |  | 
|  | 65 | u8 supported_rates_bg[BG_SUPPORTED_RATES] = { 0x02, 0x04, 0x0b, 0x0c, | 
|  | 66 | 0x12, 0x16, 0x18, 0x24, 0x30, 0x48, | 
|  | 67 | 0x60, 0x6c, 0 }; | 
|  | 68 |  | 
|  | 69 | u16 region_code_index[MWIFIEX_MAX_REGION_CODE] = { 0x10, 0x20, 0x30, | 
|  | 70 | 0x32, 0x40, 0x41, 0xff }; | 
|  | 71 |  | 
|  | 72 | u8 supported_rates_n[N_SUPPORTED_RATES] = { 0x02, 0x04, 0 }; | 
|  | 73 |  | 
|  | 74 | /* | 
|  | 75 | * This function maps an index in supported rates table into | 
|  | 76 | * the corresponding data rate. | 
|  | 77 | */ | 
| Amitkumar Karwar | 572e8f3 | 2011-04-13 17:27:08 -0700 | [diff] [blame] | 78 | u32 mwifiex_index_to_data_rate(u8 index, u8 ht_info) | 
| Bing Zhao | 5e6e3a9 | 2011-03-21 18:00:50 -0700 | [diff] [blame] | 79 | { | 
|  | 80 | u16 mcs_rate[4][8] = { | 
|  | 81 | {0x1b, 0x36, 0x51, 0x6c, 0xa2, 0xd8, 0xf3, 0x10e} | 
|  | 82 | ,			/* LG 40M */ | 
|  | 83 | {0x1e, 0x3c, 0x5a, 0x78, 0xb4, 0xf0, 0x10e, 0x12c} | 
|  | 84 | ,			/* SG 40M */ | 
|  | 85 | {0x0d, 0x1a, 0x27, 0x34, 0x4e, 0x68, 0x75, 0x82} | 
|  | 86 | ,			/* LG 20M */ | 
|  | 87 | {0x0e, 0x1c, 0x2b, 0x39, 0x56, 0x73, 0x82, 0x90} | 
|  | 88 | };			/* SG 20M */ | 
|  | 89 |  | 
|  | 90 | u32 rate; | 
|  | 91 |  | 
|  | 92 | if (ht_info & BIT(0)) { | 
|  | 93 | if (index == MWIFIEX_RATE_BITMAP_MCS0) { | 
|  | 94 | if (ht_info & BIT(2)) | 
|  | 95 | rate = 0x0D;	/* MCS 32 SGI rate */ | 
|  | 96 | else | 
|  | 97 | rate = 0x0C;	/* MCS 32 LGI rate */ | 
|  | 98 | } else if (index < 8) { | 
|  | 99 | if (ht_info & BIT(1)) { | 
|  | 100 | if (ht_info & BIT(2)) | 
|  | 101 | /* SGI, 40M */ | 
|  | 102 | rate = mcs_rate[1][index]; | 
|  | 103 | else | 
|  | 104 | /* LGI, 40M */ | 
|  | 105 | rate = mcs_rate[0][index]; | 
|  | 106 | } else { | 
|  | 107 | if (ht_info & BIT(2)) | 
|  | 108 | /* SGI, 20M */ | 
|  | 109 | rate = mcs_rate[3][index]; | 
|  | 110 | else | 
|  | 111 | /* LGI, 20M */ | 
|  | 112 | rate = mcs_rate[2][index]; | 
|  | 113 | } | 
|  | 114 | } else | 
|  | 115 | rate = mwifiex_data_rates[0]; | 
|  | 116 | } else { | 
|  | 117 | if (index >= MWIFIEX_SUPPORTED_RATES_EXT) | 
|  | 118 | index = 0; | 
|  | 119 | rate = mwifiex_data_rates[index]; | 
|  | 120 | } | 
|  | 121 | return rate; | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | /* | 
|  | 125 | * This function maps a data rate value into corresponding index in supported | 
|  | 126 | * rates table. | 
|  | 127 | */ | 
| Amitkumar Karwar | 572e8f3 | 2011-04-13 17:27:08 -0700 | [diff] [blame] | 128 | u8 mwifiex_data_rate_to_index(u32 rate) | 
| Bing Zhao | 5e6e3a9 | 2011-03-21 18:00:50 -0700 | [diff] [blame] | 129 | { | 
|  | 130 | u16 *ptr; | 
|  | 131 |  | 
|  | 132 | if (rate) { | 
|  | 133 | ptr = memchr(mwifiex_data_rates, rate, | 
|  | 134 | sizeof(mwifiex_data_rates)); | 
|  | 135 | if (ptr) | 
|  | 136 | return (u8) (ptr - mwifiex_data_rates); | 
|  | 137 | } | 
|  | 138 | return 0; | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | /* | 
|  | 142 | * This function returns the current active data rates. | 
|  | 143 | * | 
|  | 144 | * The result may vary depending upon connection status. | 
|  | 145 | */ | 
|  | 146 | u32 mwifiex_get_active_data_rates(struct mwifiex_private *priv, u8 *rates) | 
|  | 147 | { | 
| Bing Zhao | 5e6e3a9 | 2011-03-21 18:00:50 -0700 | [diff] [blame] | 148 | if (!priv->media_connected) | 
| Yogesh Ashok Powar | 636c459 | 2011-04-15 20:50:40 -0700 | [diff] [blame] | 149 | return mwifiex_get_supported_rates(priv, rates); | 
| Bing Zhao | 5e6e3a9 | 2011-03-21 18:00:50 -0700 | [diff] [blame] | 150 | else | 
| Yogesh Ashok Powar | 636c459 | 2011-04-15 20:50:40 -0700 | [diff] [blame] | 151 | return mwifiex_copy_rates(rates, 0, | 
| Bing Zhao | 5e6e3a9 | 2011-03-21 18:00:50 -0700 | [diff] [blame] | 152 | priv->curr_bss_params.data_rates, | 
|  | 153 | priv->curr_bss_params.num_of_rates); | 
| Bing Zhao | 5e6e3a9 | 2011-03-21 18:00:50 -0700 | [diff] [blame] | 154 | } | 
|  | 155 |  | 
|  | 156 | /* | 
|  | 157 | * This function locates the Channel-Frequency-Power triplet based upon | 
|  | 158 | * band and channel parameters. | 
|  | 159 | */ | 
|  | 160 | struct mwifiex_chan_freq_power * | 
|  | 161 | mwifiex_get_cfp_by_band_and_channel_from_cfg80211(struct mwifiex_private | 
|  | 162 | *priv, u8 band, u16 channel) | 
|  | 163 | { | 
|  | 164 | struct mwifiex_chan_freq_power *cfp = NULL; | 
|  | 165 | struct ieee80211_supported_band *sband; | 
|  | 166 | struct ieee80211_channel *ch; | 
|  | 167 | int i; | 
|  | 168 |  | 
|  | 169 | if (mwifiex_band_to_radio_type(band) == HostCmd_SCAN_RADIO_TYPE_BG) | 
|  | 170 | sband = priv->wdev->wiphy->bands[IEEE80211_BAND_2GHZ]; | 
|  | 171 | else | 
|  | 172 | sband = priv->wdev->wiphy->bands[IEEE80211_BAND_5GHZ]; | 
|  | 173 |  | 
|  | 174 | if (!sband) { | 
|  | 175 | dev_err(priv->adapter->dev, "%s: cannot find cfp by band %d" | 
|  | 176 | " & channel %d\n", __func__, band, channel); | 
|  | 177 | return cfp; | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | for (i = 0; i < sband->n_channels; i++) { | 
|  | 181 | ch = &sband->channels[i]; | 
|  | 182 | if (((ch->hw_value == channel) || | 
|  | 183 | (channel == FIRST_VALID_CHANNEL)) | 
|  | 184 | && !(ch->flags & IEEE80211_CHAN_DISABLED)) { | 
|  | 185 | priv->cfp.channel = channel; | 
|  | 186 | priv->cfp.freq = ch->center_freq; | 
|  | 187 | priv->cfp.max_tx_power = ch->max_power; | 
|  | 188 | cfp = &priv->cfp; | 
|  | 189 | break; | 
|  | 190 | } | 
|  | 191 | } | 
|  | 192 | if (i == sband->n_channels) | 
|  | 193 | dev_err(priv->adapter->dev, "%s: cannot find cfp by band %d" | 
|  | 194 | " & channel %d\n", __func__, band, channel); | 
|  | 195 |  | 
|  | 196 | return cfp; | 
|  | 197 | } | 
|  | 198 |  | 
|  | 199 | /* | 
|  | 200 | * This function locates the Channel-Frequency-Power triplet based upon | 
|  | 201 | * band and frequency parameters. | 
|  | 202 | */ | 
|  | 203 | struct mwifiex_chan_freq_power * | 
|  | 204 | mwifiex_get_cfp_by_band_and_freq_from_cfg80211(struct mwifiex_private *priv, | 
|  | 205 | u8 band, u32 freq) | 
|  | 206 | { | 
|  | 207 | struct mwifiex_chan_freq_power *cfp = NULL; | 
|  | 208 | struct ieee80211_supported_band *sband; | 
|  | 209 | struct ieee80211_channel *ch; | 
|  | 210 | int i; | 
|  | 211 |  | 
|  | 212 | if (mwifiex_band_to_radio_type(band) == HostCmd_SCAN_RADIO_TYPE_BG) | 
|  | 213 | sband = priv->wdev->wiphy->bands[IEEE80211_BAND_2GHZ]; | 
|  | 214 | else | 
|  | 215 | sband = priv->wdev->wiphy->bands[IEEE80211_BAND_5GHZ]; | 
|  | 216 |  | 
|  | 217 | if (!sband) { | 
|  | 218 | dev_err(priv->adapter->dev, "%s: cannot find cfp by band %d" | 
|  | 219 | " & freq %d\n", __func__, band, freq); | 
|  | 220 | return cfp; | 
|  | 221 | } | 
|  | 222 |  | 
|  | 223 | for (i = 0; i < sband->n_channels; i++) { | 
|  | 224 | ch = &sband->channels[i]; | 
|  | 225 | if ((ch->center_freq == freq) && | 
|  | 226 | !(ch->flags & IEEE80211_CHAN_DISABLED)) { | 
|  | 227 | priv->cfp.channel = ch->hw_value; | 
|  | 228 | priv->cfp.freq = freq; | 
|  | 229 | priv->cfp.max_tx_power = ch->max_power; | 
|  | 230 | cfp = &priv->cfp; | 
|  | 231 | break; | 
|  | 232 | } | 
|  | 233 | } | 
|  | 234 | if (i == sband->n_channels) | 
|  | 235 | dev_err(priv->adapter->dev, "%s: cannot find cfp by band %d" | 
|  | 236 | " & freq %d\n", __func__, band, freq); | 
|  | 237 |  | 
|  | 238 | return cfp; | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | /* | 
|  | 242 | * This function checks if the data rate is set to auto. | 
|  | 243 | */ | 
|  | 244 | u8 | 
|  | 245 | mwifiex_is_rate_auto(struct mwifiex_private *priv) | 
|  | 246 | { | 
|  | 247 | u32 i; | 
|  | 248 | int rate_num = 0; | 
|  | 249 |  | 
|  | 250 | for (i = 0; i < ARRAY_SIZE(priv->bitmap_rates); i++) | 
|  | 251 | if (priv->bitmap_rates[i]) | 
|  | 252 | rate_num++; | 
|  | 253 |  | 
|  | 254 | if (rate_num > 1) | 
|  | 255 | return true; | 
|  | 256 | else | 
|  | 257 | return false; | 
|  | 258 | } | 
|  | 259 |  | 
|  | 260 | /* | 
|  | 261 | * This function converts rate bitmap into rate index. | 
|  | 262 | */ | 
| Amitkumar Karwar | 572e8f3 | 2011-04-13 17:27:08 -0700 | [diff] [blame] | 263 | int mwifiex_get_rate_index(u16 *rate_bitmap, int size) | 
| Bing Zhao | 5e6e3a9 | 2011-03-21 18:00:50 -0700 | [diff] [blame] | 264 | { | 
|  | 265 | int i; | 
|  | 266 |  | 
|  | 267 | for (i = 0; i < size * 8; i++) | 
|  | 268 | if (rate_bitmap[i / 16] & (1 << (i % 16))) | 
|  | 269 | return i; | 
|  | 270 |  | 
|  | 271 | return 0; | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 | /* | 
|  | 275 | * This function gets the supported data rates. | 
|  | 276 | * | 
|  | 277 | * The function works in both Ad-Hoc and infra mode by printing the | 
|  | 278 | * band and returning the data rates. | 
|  | 279 | */ | 
|  | 280 | u32 mwifiex_get_supported_rates(struct mwifiex_private *priv, u8 *rates) | 
|  | 281 | { | 
|  | 282 | u32 k = 0; | 
|  | 283 | struct mwifiex_adapter *adapter = priv->adapter; | 
| Bing Zhao | eecd825 | 2011-03-28 17:55:41 -0700 | [diff] [blame] | 284 | if (priv->bss_mode == NL80211_IFTYPE_STATION) { | 
| Bing Zhao | 5e6e3a9 | 2011-03-21 18:00:50 -0700 | [diff] [blame] | 285 | switch (adapter->config_bands) { | 
|  | 286 | case BAND_B: | 
|  | 287 | dev_dbg(adapter->dev, "info: infra band=%d " | 
|  | 288 | "supported_rates_b\n", adapter->config_bands); | 
|  | 289 | k = mwifiex_copy_rates(rates, k, supported_rates_b, | 
|  | 290 | sizeof(supported_rates_b)); | 
|  | 291 | break; | 
|  | 292 | case BAND_G: | 
|  | 293 | case BAND_G | BAND_GN: | 
|  | 294 | dev_dbg(adapter->dev, "info: infra band=%d " | 
|  | 295 | "supported_rates_g\n", adapter->config_bands); | 
|  | 296 | k = mwifiex_copy_rates(rates, k, supported_rates_g, | 
|  | 297 | sizeof(supported_rates_g)); | 
|  | 298 | break; | 
|  | 299 | case BAND_B | BAND_G: | 
|  | 300 | case BAND_A | BAND_B | BAND_G: | 
|  | 301 | case BAND_A | BAND_B: | 
|  | 302 | case BAND_A | BAND_B | BAND_G | BAND_GN | BAND_AN: | 
|  | 303 | case BAND_B | BAND_G | BAND_GN: | 
|  | 304 | dev_dbg(adapter->dev, "info: infra band=%d " | 
|  | 305 | "supported_rates_bg\n", adapter->config_bands); | 
|  | 306 | k = mwifiex_copy_rates(rates, k, supported_rates_bg, | 
|  | 307 | sizeof(supported_rates_bg)); | 
|  | 308 | break; | 
|  | 309 | case BAND_A: | 
|  | 310 | case BAND_A | BAND_G: | 
|  | 311 | dev_dbg(adapter->dev, "info: infra band=%d " | 
|  | 312 | "supported_rates_a\n", adapter->config_bands); | 
|  | 313 | k = mwifiex_copy_rates(rates, k, supported_rates_a, | 
|  | 314 | sizeof(supported_rates_a)); | 
|  | 315 | break; | 
|  | 316 | case BAND_A | BAND_AN: | 
|  | 317 | case BAND_A | BAND_G | BAND_AN | BAND_GN: | 
|  | 318 | dev_dbg(adapter->dev, "info: infra band=%d " | 
|  | 319 | "supported_rates_a\n", adapter->config_bands); | 
|  | 320 | k = mwifiex_copy_rates(rates, k, supported_rates_a, | 
|  | 321 | sizeof(supported_rates_a)); | 
|  | 322 | break; | 
|  | 323 | case BAND_GN: | 
|  | 324 | dev_dbg(adapter->dev, "info: infra band=%d " | 
|  | 325 | "supported_rates_n\n", adapter->config_bands); | 
|  | 326 | k = mwifiex_copy_rates(rates, k, supported_rates_n, | 
|  | 327 | sizeof(supported_rates_n)); | 
|  | 328 | break; | 
|  | 329 | } | 
|  | 330 | } else { | 
|  | 331 | /* Ad-hoc mode */ | 
|  | 332 | switch (adapter->adhoc_start_band) { | 
|  | 333 | case BAND_B: | 
|  | 334 | dev_dbg(adapter->dev, "info: adhoc B\n"); | 
|  | 335 | k = mwifiex_copy_rates(rates, k, adhoc_rates_b, | 
|  | 336 | sizeof(adhoc_rates_b)); | 
|  | 337 | break; | 
|  | 338 | case BAND_G: | 
|  | 339 | case BAND_G | BAND_GN: | 
|  | 340 | dev_dbg(adapter->dev, "info: adhoc G only\n"); | 
|  | 341 | k = mwifiex_copy_rates(rates, k, adhoc_rates_g, | 
|  | 342 | sizeof(adhoc_rates_g)); | 
|  | 343 | break; | 
|  | 344 | case BAND_B | BAND_G: | 
|  | 345 | case BAND_B | BAND_G | BAND_GN: | 
|  | 346 | dev_dbg(adapter->dev, "info: adhoc BG\n"); | 
|  | 347 | k = mwifiex_copy_rates(rates, k, adhoc_rates_bg, | 
|  | 348 | sizeof(adhoc_rates_bg)); | 
|  | 349 | break; | 
|  | 350 | case BAND_A: | 
|  | 351 | case BAND_A | BAND_AN: | 
|  | 352 | dev_dbg(adapter->dev, "info: adhoc A\n"); | 
|  | 353 | k = mwifiex_copy_rates(rates, k, adhoc_rates_a, | 
|  | 354 | sizeof(adhoc_rates_a)); | 
|  | 355 | break; | 
|  | 356 | } | 
|  | 357 | } | 
|  | 358 |  | 
|  | 359 | return k; | 
|  | 360 | } |