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) 1999-2011, Broadcom Corporation
8 * Unless you and Broadcom execute a separate written software license
9 * agreement governing use of this software, this software is licensed to you
10 * under the terms of the GNU General Public License version 2 (the "GPL"),
11 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
12 * following added to such license:
14 * As a special exception, the copyright holders of this software give you
15 * permission to link this software with independent modules, and to copy and
16 * distribute the resulting executable under terms of your choice, provided that
17 * you also meet, for each linked independent module, the terms and conditions of
18 * the license of that module. An independent module is a module which is not
19 * derived from this software. The special exception does not apply to any
20 * modifications of the software.
22 * Notwithstanding the above, under no circumstances may you combine this
23 * software in any way with any other Broadcom software provided under a license
24 * other than the GPL, without Broadcom's express prior written consent.
25 * $Id: bcmwifi.c,v 1.31.8.1 2010-08-03 17:47:05 Exp $
34 #define strtoul(nptr, endptr, base) bcm_strtoul((nptr), (endptr), (base))
35 #define tolower(c) (bcm_isupper((c)) ? ((c) + 'a' - 'A') : (c))
46 #if defined(WIN32) && (defined(BCMDLL) || defined(WLMDLL))
47 #include <bcmstdlib.h>
55 wf_chspec_ntoa(chanspec_t chspec, char *buf)
57 const char *band, *bw, *sb;
63 channel = CHSPEC_CHANNEL(chspec);
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)) {
81 snprintf(buf, 6, "%d%s%s%s", channel, band, bw, sb);
87 wf_chspec_aton(char *a)
90 uint channel, band, bw, ctl_sb;
93 channel = strtoul(a, &endp, 10);
99 if (channel > MAXCHANNEL)
102 band = ((channel <= CH_MAX_2G_CHANNEL) ? WL_CHANSPEC_BAND_2G : WL_CHANSPEC_BAND_5G);
103 bw = WL_CHANSPEC_BW_20;
104 ctl_sb = WL_CHANSPEC_CTL_SB_NONE;
113 if (c == 'a' || c == 'b') {
114 band = (c == 'a') ? WL_CHANSPEC_BAND_5G : WL_CHANSPEC_BAND_2G;
123 bw = WL_CHANSPEC_BW_10;
124 } else if (c == 'l') {
125 bw = WL_CHANSPEC_BW_40;
126 ctl_sb = WL_CHANSPEC_CTL_SB_LOWER;
128 if (channel <= (MAXCHANNEL - CH_20MHZ_APART))
129 channel += CH_10MHZ_APART;
132 } else if (c == 'u') {
133 bw = WL_CHANSPEC_BW_40;
134 ctl_sb = WL_CHANSPEC_CTL_SB_UPPER;
136 if (channel > CH_20MHZ_APART)
137 channel -= CH_10MHZ_APART;
145 return (channel | band | bw | ctl_sb);
150 wf_chspec_malformed(chanspec_t chanspec)
153 if (!CHSPEC_IS5G(chanspec) && !CHSPEC_IS2G(chanspec))
156 if (!CHSPEC_IS40(chanspec) && !CHSPEC_IS20(chanspec))
160 if (CHSPEC_IS20_UNCOND(chanspec)) {
161 if (!CHSPEC_SB_NONE(chanspec))
164 if (!CHSPEC_SB_UPPER(chanspec) && !CHSPEC_SB_LOWER(chanspec))
173 wf_chspec_ctlchan(chanspec_t chspec)
178 if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_NONE) {
179 return CHSPEC_CHANNEL(chspec);
182 ASSERT(CHSPEC_BW(chspec) == WL_CHANSPEC_BW_40);
184 if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_UPPER) {
186 ctl_chan = UPPER_20_SB(CHSPEC_CHANNEL(chspec));
188 ASSERT(CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_LOWER);
190 ctl_chan = LOWER_20_SB(CHSPEC_CHANNEL(chspec));
198 wf_chspec_ctlchspec(chanspec_t chspec)
200 chanspec_t ctl_chspec = 0;
203 ASSERT(!wf_chspec_malformed(chspec));
206 if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_NONE) {
209 if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_UPPER) {
210 channel = UPPER_20_SB(CHSPEC_CHANNEL(chspec));
212 channel = LOWER_20_SB(CHSPEC_CHANNEL(chspec));
214 ctl_chspec = channel | WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE;
215 ctl_chspec |= CHSPEC_BAND(chspec);
222 wf_mhz2channel(uint freq, uint start_factor)
229 if (start_factor == 0) {
230 if (freq >= 2400 && freq <= 2500)
231 start_factor = WF_CHAN_FACTOR_2_4_G;
232 else if (freq >= 5000 && freq <= 6000)
233 start_factor = WF_CHAN_FACTOR_5_G;
236 if (freq == 2484 && start_factor == WF_CHAN_FACTOR_2_4_G)
239 base = start_factor / 2;
242 if ((freq < base) || (freq > base + 1000))
245 offset = freq - base;
249 if (offset != (ch * 5))
253 if (start_factor == WF_CHAN_FACTOR_2_4_G && (ch < 1 || ch > 13))
261 wf_channel2mhz(uint ch, uint start_factor)
265 if ((start_factor == WF_CHAN_FACTOR_2_4_G && (ch < 1 || ch > 14)) ||
268 else if ((start_factor == WF_CHAN_FACTOR_2_4_G) && (ch == 14))
271 freq = ch * 5 + start_factor / 2;