2 * Copyright (c) 2010 Broadcom Corporation
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.
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.
21 #define strtoul(nptr, endptr, base) bcm_strtoul((nptr), (endptr), (base))
22 #define tolower(c) (bcm_isupper((c)) ? ((c) + 'a' - 'A') : (c))
25 /* Chanspec ASCII representation:
26 * <channel><band><bandwidth><ctl-sideband>
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
36 * <band> may be omitted on input, and will be assumed to be
37 * 2.4GHz if channel number <= 14.
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
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
55 char *wf_chspec_ntoa(chanspec_t chspec
, char *buf
)
57 const char *band
, *bw
, *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
)) {
71 channel
+= CH_10MHZ_APART
;
74 channel
-= CH_10MHZ_APART
;
76 } else if (CHSPEC_IS10(chspec
)) {
80 /* Outputs a max of 6 chars including '\0' */
81 snprintf(buf
, 6, "%d%s%s%s", channel
, band
, bw
, sb
);
85 /* given a chanspec string, convert to a chanspec.
88 chanspec_t
wf_chspec_aton(char *a
)
91 uint channel
, band
, bw
, ctl_sb
;
94 channel
= strtoul(a
, &endp
, 10);
96 /* check for no digits parsed */
100 if (channel
> MAXCHANNEL
)
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
;
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
;
124 /* parse bandwidth 'N' (10MHz) or 40MHz ctl sideband ['L' | 'U'] */
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
;
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
;
148 return (channel
| band
| bw
| ctl_sb
);
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.
157 bool wf_chspec_malformed(chanspec_t chanspec
)
159 /* must be 2G or 5G band */
160 if (!CHSPEC_IS5G(chanspec
) && !CHSPEC_IS2G(chanspec
))
162 /* must be 20 or 40 bandwidth */
163 if (!CHSPEC_IS40(chanspec
) && !CHSPEC_IS20(chanspec
))
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
))
171 if (!CHSPEC_SB_UPPER(chanspec
) && !CHSPEC_SB_LOWER(chanspec
))
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
183 uint8
wf_chspec_ctlchan(chanspec_t chspec
)
187 /* Is there a sideband ? */
188 if (CHSPEC_CTL_SB(chspec
) == WL_CHANSPEC_CTL_SB_NONE
) {
189 return CHSPEC_CHANNEL(chspec
);
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
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
));
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
));
210 chanspec_t
wf_chspec_ctlchspec(chanspec_t chspec
)
212 chanspec_t ctl_chspec
= 0;
215 ASSERT(!wf_chspec_malformed(chspec
));
217 /* Is there a sideband ? */
218 if (CHSPEC_CTL_SB(chspec
) == WL_CHANSPEC_CTL_SB_NONE
) {
221 if (CHSPEC_CTL_SB(chspec
) == WL_CHANSPEC_CTL_SB_UPPER
) {
222 channel
= UPPER_20_SB(CHSPEC_CHANNEL(chspec
));
224 channel
= LOWER_20_SB(CHSPEC_CHANNEL(chspec
));
227 channel
| WL_CHANSPEC_BW_20
| WL_CHANSPEC_CTL_SB_NONE
;
228 ctl_chspec
|= CHSPEC_BAND(chspec
);
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.
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.
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.
250 * Reference 802.11 REVma, section 17.3.8.3, and 802.11B section 18.4.6.2
252 int wf_mhz2channel(uint freq
, uint start_factor
)
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
;
266 if (freq
== 2484 && start_factor
== WF_CHAN_FACTOR_2_4_G
)
269 base
= start_factor
/ 2;
271 /* check that the frequency is in 1GHz range of the base */
272 if ((freq
< base
) || (freq
> base
+ 1000))
275 offset
= freq
- base
;
278 /* check that frequency is a 5MHz multiple from the base */
279 if (offset
!= (ch
* 5))
282 /* restricted channel range check for 2.4G */
283 if (start_factor
== WF_CHAN_FACTOR_2_4_G
&& (ch
< 1 || ch
> 13))
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.
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.
303 * Reference 802.11 REVma, section 17.3.8.3, and 802.11B section 18.4.6.2
305 int wf_channel2mhz(uint ch
, uint start_factor
)
309 if ((start_factor
== WF_CHAN_FACTOR_2_4_G
&& (ch
< 1 || ch
> 14)) ||
312 else if ((start_factor
== WF_CHAN_FACTOR_2_4_G
) && (ch
== 14))
315 freq
= ch
* 5 + start_factor
/ 2;