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