Staging: Add initial release of brcm80211 - Broadcom 802.11n wireless LAN driver.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / brcm80211 / util / bcmwifi.c
1 /*
2 * Copyright (c) 2010 Broadcom Corporation
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <typedefs.h>
18
19 #include <osl.h>
20 #include <bcmutils.h>
21 #define strtoul(nptr, endptr, base) bcm_strtoul((nptr), (endptr), (base))
22 #define tolower(c) (bcm_isupper((c)) ? ((c) + 'a' - 'A') : (c))
23 #include <bcmwifi.h>
24
25 /* Chanspec ASCII representation:
26 * <channel><band><bandwidth><ctl-sideband>
27 * digit [AB] [N] [UL]
28 *
29 * <channel>: channel number of the 10MHz or 20MHz channel,
30 * or control sideband channel of 40MHz channel.
31 * <band>: A for 5GHz, B for 2.4GHz
32 * <bandwidth>: N for 10MHz, nothing for 20MHz or 40MHz
33 * (ctl-sideband spec implies 40MHz)
34 * <ctl-sideband>: U for upper, L for lower
35 *
36 * <band> may be omitted on input, and will be assumed to be
37 * 2.4GHz if channel number <= 14.
38 *
39 * Examples:
40 * 8 -> 2.4GHz channel 8, 20MHz
41 * 8b -> 2.4GHz channel 8, 20MHz
42 * 8l -> 2.4GHz channel 8, 40MHz, lower ctl sideband
43 * 8a -> 5GHz channel 8 (low 5 GHz band), 20MHz
44 * 36 -> 5GHz channel 36, 20MHz
45 * 36l -> 5GHz channel 36, 40MHz, lower ctl sideband
46 * 40u -> 5GHz channel 40, 40MHz, upper ctl sideband
47 * 180n -> channel 180, 10MHz
48 */
49
50 /* given a chanspec and a string buffer, format the chanspec as a
51 * string, and return the original pointer a.
52 * Min buffer length must be CHANSPEC_STR_LEN.
53 * On error return NULL
54 */
55 char *wf_chspec_ntoa(chanspec_t chspec, char *buf)
56 {
57 const char *band, *bw, *sb;
58 uint channel;
59
60 band = "";
61 bw = "";
62 sb = "";
63 channel = CHSPEC_CHANNEL(chspec);
64 /* check for non-default band spec */
65 if ((CHSPEC_IS2G(chspec) && channel > CH_MAX_2G_CHANNEL) ||
66 (CHSPEC_IS5G(chspec) && channel <= CH_MAX_2G_CHANNEL))
67 band = (CHSPEC_IS2G(chspec)) ? "b" : "a";
68 if (CHSPEC_IS40(chspec)) {
69 if (CHSPEC_SB_UPPER(chspec)) {
70 sb = "u";
71 channel += CH_10MHZ_APART;
72 } else {
73 sb = "l";
74 channel -= CH_10MHZ_APART;
75 }
76 } else if (CHSPEC_IS10(chspec)) {
77 bw = "n";
78 }
79
80 /* Outputs a max of 6 chars including '\0' */
81 snprintf(buf, 6, "%d%s%s%s", channel, band, bw, sb);
82 return (buf);
83 }
84
85 /* given a chanspec string, convert to a chanspec.
86 * On error return 0
87 */
88 chanspec_t wf_chspec_aton(char *a)
89 {
90 char *endp = NULL;
91 uint channel, band, bw, ctl_sb;
92 char c;
93
94 channel = strtoul(a, &endp, 10);
95
96 /* check for no digits parsed */
97 if (endp == a)
98 return 0;
99
100 if (channel > MAXCHANNEL)
101 return 0;
102
103 band =
104 ((channel <=
105 CH_MAX_2G_CHANNEL) ? WL_CHANSPEC_BAND_2G : WL_CHANSPEC_BAND_5G);
106 bw = WL_CHANSPEC_BW_20;
107 ctl_sb = WL_CHANSPEC_CTL_SB_NONE;
108
109 a = endp;
110
111 c = tolower(a[0]);
112 if (c == '\0')
113 goto done;
114
115 /* parse the optional ['A' | 'B'] band spec */
116 if (c == 'a' || c == 'b') {
117 band = (c == 'a') ? WL_CHANSPEC_BAND_5G : WL_CHANSPEC_BAND_2G;
118 a++;
119 c = tolower(a[0]);
120 if (c == '\0')
121 goto done;
122 }
123
124 /* parse bandwidth 'N' (10MHz) or 40MHz ctl sideband ['L' | 'U'] */
125 if (c == 'n') {
126 bw = WL_CHANSPEC_BW_10;
127 } else if (c == 'l') {
128 bw = WL_CHANSPEC_BW_40;
129 ctl_sb = WL_CHANSPEC_CTL_SB_LOWER;
130 /* adjust channel to center of 40MHz band */
131 if (channel <= (MAXCHANNEL - CH_20MHZ_APART))
132 channel += CH_10MHZ_APART;
133 else
134 return 0;
135 } else if (c == 'u') {
136 bw = WL_CHANSPEC_BW_40;
137 ctl_sb = WL_CHANSPEC_CTL_SB_UPPER;
138 /* adjust channel to center of 40MHz band */
139 if (channel > CH_20MHZ_APART)
140 channel -= CH_10MHZ_APART;
141 else
142 return 0;
143 } else {
144 return 0;
145 }
146
147 done:
148 return (channel | band | bw | ctl_sb);
149 }
150
151 /*
152 * Verify the chanspec is using a legal set of parameters, i.e. that the
153 * chanspec specified a band, bw, ctl_sb and channel and that the
154 * combination could be legal given any set of circumstances.
155 * RETURNS: TRUE is the chanspec is malformed, false if it looks good.
156 */
157 bool wf_chspec_malformed(chanspec_t chanspec)
158 {
159 /* must be 2G or 5G band */
160 if (!CHSPEC_IS5G(chanspec) && !CHSPEC_IS2G(chanspec))
161 return TRUE;
162 /* must be 20 or 40 bandwidth */
163 if (!CHSPEC_IS40(chanspec) && !CHSPEC_IS20(chanspec))
164 return TRUE;
165
166 /* 20MHZ b/w must have no ctl sb, 40 must have a ctl sb */
167 if (CHSPEC_IS20(chanspec)) {
168 if (!CHSPEC_SB_NONE(chanspec))
169 return TRUE;
170 } else {
171 if (!CHSPEC_SB_UPPER(chanspec) && !CHSPEC_SB_LOWER(chanspec))
172 return TRUE;
173 }
174
175 return FALSE;
176 }
177
178 /*
179 * This function returns the channel number that control traffic is being sent on, for legacy
180 * channels this is just the channel number, for 40MHZ channels it is the upper or lowre 20MHZ
181 * sideband depending on the chanspec selected
182 */
183 uint8 wf_chspec_ctlchan(chanspec_t chspec)
184 {
185 uint8 ctl_chan;
186
187 /* Is there a sideband ? */
188 if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_NONE) {
189 return CHSPEC_CHANNEL(chspec);
190 } else {
191 /* we only support 40MHZ with sidebands */
192 ASSERT(CHSPEC_BW(chspec) == WL_CHANSPEC_BW_40);
193 /* chanspec channel holds the centre frequency, use that and the
194 * side band information to reconstruct the control channel number
195 */
196 if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_UPPER) {
197 /* control chan is the upper 20 MHZ SB of the 40MHZ channel */
198 ctl_chan = UPPER_20_SB(CHSPEC_CHANNEL(chspec));
199 } else {
200 ASSERT(CHSPEC_CTL_SB(chspec) ==
201 WL_CHANSPEC_CTL_SB_LOWER);
202 /* control chan is the lower 20 MHZ SB of the 40MHZ channel */
203 ctl_chan = LOWER_20_SB(CHSPEC_CHANNEL(chspec));
204 }
205 }
206
207 return ctl_chan;
208 }
209
210 chanspec_t wf_chspec_ctlchspec(chanspec_t chspec)
211 {
212 chanspec_t ctl_chspec = 0;
213 uint8 channel;
214
215 ASSERT(!wf_chspec_malformed(chspec));
216
217 /* Is there a sideband ? */
218 if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_NONE) {
219 return chspec;
220 } else {
221 if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_UPPER) {
222 channel = UPPER_20_SB(CHSPEC_CHANNEL(chspec));
223 } else {
224 channel = LOWER_20_SB(CHSPEC_CHANNEL(chspec));
225 }
226 ctl_chspec =
227 channel | WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE;
228 ctl_chspec |= CHSPEC_BAND(chspec);
229 }
230 return ctl_chspec;
231 }
232
233 /*
234 * Return the channel number for a given frequency and base frequency.
235 * The returned channel number is relative to the given base frequency.
236 * If the given base frequency is zero, a base frequency of 5 GHz is assumed for
237 * frequencies from 5 - 6 GHz, and 2.407 GHz is assumed for 2.4 - 2.5 GHz.
238 *
239 * Frequency is specified in MHz.
240 * The base frequency is specified as (start_factor * 500 kHz).
241 * Constants WF_CHAN_FACTOR_2_4_G, WF_CHAN_FACTOR_5_G are defined for
242 * 2.4 GHz and 5 GHz bands.
243 *
244 * The returned channel will be in the range [1, 14] in the 2.4 GHz band
245 * and [0, 200] otherwise.
246 * -1 is returned if the start_factor is WF_CHAN_FACTOR_2_4_G and the
247 * frequency is not a 2.4 GHz channel, or if the frequency is not and even
248 * multiple of 5 MHz from the base frequency to the base plus 1 GHz.
249 *
250 * Reference 802.11 REVma, section 17.3.8.3, and 802.11B section 18.4.6.2
251 */
252 int wf_mhz2channel(uint freq, uint start_factor)
253 {
254 int ch = -1;
255 uint base;
256 int offset;
257
258 /* take the default channel start frequency */
259 if (start_factor == 0) {
260 if (freq >= 2400 && freq <= 2500)
261 start_factor = WF_CHAN_FACTOR_2_4_G;
262 else if (freq >= 5000 && freq <= 6000)
263 start_factor = WF_CHAN_FACTOR_5_G;
264 }
265
266 if (freq == 2484 && start_factor == WF_CHAN_FACTOR_2_4_G)
267 return 14;
268
269 base = start_factor / 2;
270
271 /* check that the frequency is in 1GHz range of the base */
272 if ((freq < base) || (freq > base + 1000))
273 return -1;
274
275 offset = freq - base;
276 ch = offset / 5;
277
278 /* check that frequency is a 5MHz multiple from the base */
279 if (offset != (ch * 5))
280 return -1;
281
282 /* restricted channel range check for 2.4G */
283 if (start_factor == WF_CHAN_FACTOR_2_4_G && (ch < 1 || ch > 13))
284 return -1;
285
286 return ch;
287 }
288
289 /*
290 * Return the center frequency in MHz of the given channel and base frequency.
291 * The channel number is interpreted relative to the given base frequency.
292 *
293 * The valid channel range is [1, 14] in the 2.4 GHz band and [0, 200] otherwise.
294 * The base frequency is specified as (start_factor * 500 kHz).
295 * Constants WF_CHAN_FACTOR_2_4_G, WF_CHAN_FACTOR_4_G, and WF_CHAN_FACTOR_5_G
296 * are defined for 2.4 GHz, 4 GHz, and 5 GHz bands.
297 * The channel range of [1, 14] is only checked for a start_factor of
298 * WF_CHAN_FACTOR_2_4_G (4814 = 2407 * 2).
299 * Odd start_factors produce channels on .5 MHz boundaries, in which case
300 * the answer is rounded down to an integral MHz.
301 * -1 is returned for an out of range channel.
302 *
303 * Reference 802.11 REVma, section 17.3.8.3, and 802.11B section 18.4.6.2
304 */
305 int wf_channel2mhz(uint ch, uint start_factor)
306 {
307 int freq;
308
309 if ((start_factor == WF_CHAN_FACTOR_2_4_G && (ch < 1 || ch > 14)) ||
310 (ch > 200))
311 freq = -1;
312 else if ((start_factor == WF_CHAN_FACTOR_2_4_G) && (ch == 14))
313 freq = 2484;
314 else
315 freq = ch * 5 + start_factor / 2;
316
317 return freq;
318 }