2 * Misc utility routines used by kernel or app-level.
3 * Contents are wifi-specific, used by any kernel or app-level
4 * software that might want wifi things as it grows.
6 * Copyright (C) 2009, Broadcom Corporation
9 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Broadcom Corporation;
10 * the contents of this file may not be disclosed to third parties, copied
11 * or duplicated in any form, in whole or in part, without the prior
12 * written permission of Broadcom Corporation.
13 * $Id: bcmwifi.c,v 1.22.4.1 2009/03/13 18:25:48 Exp $
21 #define strtoul(nptr, endptr, base) bcm_strtoul((nptr), (endptr), (base))
22 #define tolower(c) (bcm_isupper((c)) ? ((c) + 'a' - 'A') : (c))
30 #endif /* BCMDRIVER */
33 #if defined(WIN32) && defined(BCMDLL)
34 #include <bcmstdlib.h> /* For wl/exe/GNUmakefile.brcm_wlu */
37 /* Chanspec ASCII representation:
38 * <channel><band><bandwidth><ctl-sideband>
41 * <channel>: channel number of the 10MHz or 20MHz channel,
42 * or control sideband channel of 40MHz channel.
43 * <band>: A for 5GHz, B for 2.4GHz
44 * <bandwidth>: N for 10MHz, nothing for 20MHz or 40MHz
45 * (ctl-sideband spec implies 40MHz)
46 * <ctl-sideband>: U for upper, L for lower
48 * <band> may be omitted on input, and will be assumed to be
49 * 2.4GHz if channel number <= 14.
52 * 8 -> 2.4GHz channel 8, 20MHz
53 * 8b -> 2.4GHz channel 8, 20MHz
54 * 8l -> 2.4GHz channel 8, 40MHz, lower ctl sideband
55 * 8a -> 5GHz channel 8 (low 5 GHz band), 20MHz
56 * 36 -> 5GHz channel 36, 20MHz
57 * 36l -> 5GHz channel 36, 40MHz, lower ctl sideband
58 * 40u -> 5GHz channel 40, 40MHz, upper ctl sideband
59 * 180n -> channel 180, 10MHz
63 /* given a chanspec and a string buffer, format the chanspec as a
64 * string, and return the original pointer a.
65 * Min buffer length must be CHANSPEC_STR_LEN.
66 * On error return NULL
69 wf_chspec_ntoa(chanspec_t chspec
, char *buf
)
71 const char *band
, *bw
, *sb
;
77 channel
= CHSPEC_CHANNEL(chspec
);
78 /* check for non-default band spec */
79 if ((CHSPEC_IS2G(chspec
) && channel
> CH_MAX_2G_CHANNEL
) ||
80 (CHSPEC_IS5G(chspec
) && channel
<= CH_MAX_2G_CHANNEL
))
81 band
= (CHSPEC_IS2G(chspec
)) ? "b" : "a";
82 if (CHSPEC_IS40(chspec
)) {
83 if (CHSPEC_SB_UPPER(chspec
)) {
85 channel
+= CH_10MHZ_APART
;
88 channel
-= CH_10MHZ_APART
;
90 } else if (CHSPEC_IS10(chspec
)) {
94 /* Outputs a max of 6 chars including '\0' */
95 snprintf(buf
, 6, "%d%s%s%s", channel
, band
, bw
, sb
);
99 /* given a chanspec string, convert to a chanspec.
103 wf_chspec_aton(char *a
)
106 uint channel
, band
, bw
, ctl_sb
;
109 channel
= strtoul(a
, &endp
, 10);
111 /* check for no digits parsed */
115 if (channel
> MAXCHANNEL
)
118 band
= ((channel
<= CH_MAX_2G_CHANNEL
) ? WL_CHANSPEC_BAND_2G
: WL_CHANSPEC_BAND_5G
);
119 bw
= WL_CHANSPEC_BW_20
;
120 ctl_sb
= WL_CHANSPEC_CTL_SB_NONE
;
128 /* parse the optional ['A' | 'B'] band spec */
129 if (c
== 'a' || c
== 'b') {
130 band
= (c
== 'a') ? WL_CHANSPEC_BAND_5G
: WL_CHANSPEC_BAND_2G
;
137 /* parse bandwidth 'N' (10MHz) or 40MHz ctl sideband ['L' | 'U'] */
139 bw
= WL_CHANSPEC_BW_10
;
140 } else if (c
== 'l') {
141 bw
= WL_CHANSPEC_BW_40
;
142 ctl_sb
= WL_CHANSPEC_CTL_SB_LOWER
;
143 /* adjust channel to center of 40MHz band */
144 if (channel
<= (MAXCHANNEL
- CH_20MHZ_APART
))
145 channel
+= CH_10MHZ_APART
;
148 } else if (c
== 'u') {
149 bw
= WL_CHANSPEC_BW_40
;
150 ctl_sb
= WL_CHANSPEC_CTL_SB_UPPER
;
151 /* adjust channel to center of 40MHz band */
152 if (channel
> CH_20MHZ_APART
)
153 channel
-= CH_10MHZ_APART
;
161 return (channel
| band
| bw
| ctl_sb
);
165 * Verify the chanspec is using a legal set of parameters, i.e. that the
166 * chanspec specified a band, bw, ctl_sb and channel and that the
167 * combination could be legal given any set of circumstances.
168 * RETURNS: TRUE is the chanspec is malformed, false if it looks good.
171 wf_chspec_malformed(chanspec_t chanspec
)
173 /* must be 2G or 5G band */
174 if (!CHSPEC_IS5G(chanspec
) && !CHSPEC_IS2G(chanspec
))
176 /* must be 20 or 40 bandwidth */
177 if (!CHSPEC_IS40(chanspec
) && !CHSPEC_IS20(chanspec
))
180 /* 20MHZ b/w must have no ctl sb, 40 must have a ctl sb */
181 if (CHSPEC_IS20(chanspec
)) {
182 if (!CHSPEC_SB_NONE(chanspec
))
185 if (!CHSPEC_SB_UPPER(chanspec
) && !CHSPEC_SB_LOWER(chanspec
))
193 * This function returns the channel number that control traffic is being sent on, for legacy
194 * channels this is just the channel number, for 40MHZ channels it is the upper or lowre 20MHZ
195 * sideband depending on the chanspec selected
198 wf_chspec_ctlchan(chanspec_t chspec
)
202 /* Is there a sideband ? */
203 if (CHSPEC_CTL_SB(chspec
) == WL_CHANSPEC_CTL_SB_NONE
) {
204 return CHSPEC_CHANNEL(chspec
);
206 /* we only support 40MHZ with sidebands */
207 ASSERT(CHSPEC_BW(chspec
) == WL_CHANSPEC_BW_40
);
208 /* chanspec channel holds the centre frequency, use that and the
209 * side band information to reconstruct the control channel number
211 if (CHSPEC_CTL_SB(chspec
) == WL_CHANSPEC_CTL_SB_UPPER
) {
212 /* control chan is the upper 20 MHZ SB of the 40MHZ channel */
213 ctl_chan
= UPPER_20_SB(CHSPEC_CHANNEL(chspec
));
215 ASSERT(CHSPEC_CTL_SB(chspec
) == WL_CHANSPEC_CTL_SB_LOWER
);
216 /* control chan is the lower 20 MHZ SB of the 40MHZ channel */
217 ctl_chan
= LOWER_20_SB(CHSPEC_CHANNEL(chspec
));
225 * Return the channel number for a given frequency and base frequency.
226 * The returned channel number is relative to the given base frequency.
227 * If the given base frequency is zero, a base frequency of 5 GHz is assumed for
228 * frequencies from 5 - 6 GHz, and 2.407 GHz is assumed for 2.4 - 2.5 GHz.
230 * Frequency is specified in MHz.
231 * The base frequency is specified as (start_factor * 500 kHz).
232 * Constants WF_CHAN_FACTOR_2_4_G, WF_CHAN_FACTOR_5_G are defined for
233 * 2.4 GHz and 5 GHz bands.
235 * The returned channel will be in the range [1, 14] in the 2.4 GHz band
236 * and [0, 200] otherwise.
237 * -1 is returned if the start_factor is WF_CHAN_FACTOR_2_4_G and the
238 * frequency is not a 2.4 GHz channel, or if the frequency is not and even
239 * multiple of 5 MHz from the base frequency to the base plus 1 GHz.
241 * Reference 802.11 REVma, section 17.3.8.3, and 802.11B section 18.4.6.2
244 wf_mhz2channel(uint freq
, uint start_factor
)
250 /* take the default channel start frequency */
251 if (start_factor
== 0) {
252 if (freq
>= 2400 && freq
<= 2500)
253 start_factor
= WF_CHAN_FACTOR_2_4_G
;
254 else if (freq
>= 5000 && freq
<= 6000)
255 start_factor
= WF_CHAN_FACTOR_5_G
;
258 if (freq
== 2484 && start_factor
== WF_CHAN_FACTOR_2_4_G
)
261 base
= start_factor
/ 2;
263 /* check that the frequency is in 1GHz range of the base */
264 if ((freq
< base
) || (freq
> base
+ 1000))
267 offset
= freq
- base
;
270 /* check that frequency is a 5MHz multiple from the base */
271 if (offset
!= (ch
* 5))
274 /* restricted channel range check for 2.4G */
275 if (start_factor
== WF_CHAN_FACTOR_2_4_G
&& (ch
< 1 || ch
> 13))
282 * Return the center frequency in MHz of the given channel and base frequency.
283 * The channel number is interpreted relative to the given base frequency.
285 * The valid channel range is [1, 14] in the 2.4 GHz band and [0, 200] otherwise.
286 * The base frequency is specified as (start_factor * 500 kHz).
287 * Constants WF_CHAN_FACTOR_2_4_G, WF_CHAN_FACTOR_5_G are defined for
288 * 2.4 GHz and 5 GHz bands.
289 * The channel range of [1, 14] is only checked for a start_factor of
290 * WF_CHAN_FACTOR_2_4_G (4814).
291 * Odd start_factors produce channels on .5 MHz boundaries, in which case
292 * the answer is rounded down to an integral MHz.
293 * -1 is returned for an out of range channel.
295 * Reference 802.11 REVma, section 17.3.8.3, and 802.11B section 18.4.6.2
298 wf_channel2mhz(uint ch
, uint start_factor
)
302 if ((start_factor
== WF_CHAN_FACTOR_2_4_G
&& (ch
< 1 || ch
> 14)) ||
305 if ((start_factor
== WF_CHAN_FACTOR_2_4_G
) && (ch
== 14))
308 freq
= ch
* 5 + start_factor
/ 2;