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-2017, 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.
27 * <<Broadcom-WL-IPTag/Open:>>
29 * $Id: bcmwifi_channels.c 612483 2016-01-14 03:44:27Z $
38 #define strtoul(nptr, endptr, base) bcm_strtoul((nptr), (endptr), (base))
39 #define tolower(c) (bcm_isupper((c)) ? ((c) + 'a' - 'A') : (c))
47 #endif /* BCMDRIVER */
49 #include <bcmwifi_channels.h>
51 #if defined(WIN32) && (defined(BCMDLL) || defined(WLMDLL))
52 #include <bcmstdlib.h> /* For wl/exe/GNUmakefile.brcm_wlu and GNUmakefile.wlm_dll */
55 /* Definitions for D11AC capable Chanspec type */
57 /* Chanspec ASCII representation with 802.11ac capability:
58 * [<band> 'g'] <channel> ['/'<bandwidth> [<ctl-sideband>]['/'<1st80channel>'-'<2nd80channel>]]
61 * (optional) 2, 3, 4, 5 for 2.4GHz, 3GHz, 4GHz, and 5GHz respectively.
62 * Default value is 2g if channel <= 14, otherwise 5g.
64 * channel number of the 5MHz, 10MHz, 20MHz channel,
65 * or primary channel of 40MHz, 80MHz, 160MHz, or 80+80MHz channel.
67 * (optional) 5, 10, 20, 40, 80, 160, or 80+80. Default value is 20.
69 * (only for 2.4GHz band 40MHz) U for upper sideband primary, L for lower.
71 * For 2.4GHz band 40MHz channels, the same primary channel may be the
72 * upper sideband for one 40MHz channel, and the lower sideband for an
73 * overlapping 40MHz channel. The U/L disambiguates which 40MHz channel
76 * For 40MHz in the 5GHz band and all channel bandwidths greater than
77 * 40MHz, the U/L specificaion is not allowed since the channels are
78 * non-overlapping and the primary sub-band is derived from its
79 * position in the wide bandwidth channel.
83 * Required for 80+80, otherwise not allowed.
84 * Specifies the center channel of the first and second 80MHz band.
86 * In its simplest form, it is a 20MHz channel number, with the implied band
87 * of 2.4GHz if channel number <= 14, and 5GHz otherwise.
89 * To allow for backward compatibility with scripts, the old form for
90 * 40MHz channels is also allowed: <channel><ctl-sideband>
93 * primary channel of 40MHz, channel <= 14 is 2GHz, otherwise 5GHz
95 * "U" for upper, "L" for lower (or lower case "u" "l")
98 * Chanspec BW Center Ch Channel Range Primary Ch
101 * 52/40 40MHz 54 52-56 52
102 * 56/40 40MHz 54 52-56 56
103 * 52/80 80MHz 58 52-64 52
104 * 56/80 80MHz 58 52-64 56
105 * 60/80 80MHz 58 52-64 60
106 * 64/80 80MHz 58 52-64 64
107 * 52/160 160MHz 50 36-64 52
108 * 36/160 160MGz 50 36-64 36
109 * 36/80+80/42-106 80+80MHz 42,106 36-48,100-112 36
112 * Chanspec BW Center Ch Channel Range Primary Ch
116 * 6/40l 40MHz 8 6-10 6
118 * 6/40u 40MHz 4 2-6 6
122 /* bandwidth ASCII string */
123 static const char *wf_chspec_bw_str
[] =
139 static const uint8 wf_chspec_bw_mhz
[] =
140 {5, 10, 20, 40, 80, 160, 160};
143 (sizeof(wf_chspec_bw_mhz)/sizeof(uint8))
145 /* 40MHz channels in 5GHz band */
146 static const uint8 wf_5g_40m_chans
[] =
147 {38, 46, 54, 62, 102, 110, 118, 126, 134, 142, 151, 159};
148 #define WF_NUM_5G_40M_CHANS \
149 (sizeof(wf_5g_40m_chans)/sizeof(uint8))
151 /* 80MHz channels in 5GHz band */
152 static const uint8 wf_5g_80m_chans
[] =
153 {42, 58, 106, 122, 138, 155};
154 #define WF_NUM_5G_80M_CHANS \
155 (sizeof(wf_5g_80m_chans)/sizeof(uint8))
157 /* 160MHz channels in 5GHz band */
158 static const uint8 wf_5g_160m_chans
[] =
160 #define WF_NUM_5G_160M_CHANS \
161 (sizeof(wf_5g_160m_chans)/sizeof(uint8))
163 /* opclass and channel information for US. Table E-1 */
164 static const uint16 opclass_data
[] = {
165 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_20
)&WL_CHANSPEC_BW_MASK
)),
166 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_20
)&WL_CHANSPEC_BW_MASK
)),
167 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_20
)&WL_CHANSPEC_BW_MASK
)),
168 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_20
)&WL_CHANSPEC_BW_MASK
)),
169 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_20
)&WL_CHANSPEC_BW_MASK
)),
170 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_5
)&WL_CHANSPEC_BW_MASK
)),
171 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_5
)&WL_CHANSPEC_BW_MASK
)),
172 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_10
)&WL_CHANSPEC_BW_MASK
)),
173 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_10
)&WL_CHANSPEC_BW_MASK
)),
174 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_20
)&WL_CHANSPEC_BW_MASK
)),
175 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_20
)&WL_CHANSPEC_BW_MASK
)),
176 (WL_CHANSPEC_BAND_2G
|((WL_CHANSPEC_BW_20
)&WL_CHANSPEC_BW_MASK
)),
177 (WL_CHANSPEC_BAND_3G
|((WL_CHANSPEC_BW_20
)&WL_CHANSPEC_BW_MASK
)),
178 (WL_CHANSPEC_BAND_3G
|((WL_CHANSPEC_BW_10
)&WL_CHANSPEC_BW_MASK
)),
179 (WL_CHANSPEC_BAND_3G
|((WL_CHANSPEC_BW_5
)&WL_CHANSPEC_BW_MASK
)),
180 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_5
)&WL_CHANSPEC_BW_MASK
)),
181 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_10
)&WL_CHANSPEC_BW_MASK
)),
182 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_20
)&WL_CHANSPEC_BW_MASK
)),
186 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_40
)&WL_CHANSPEC_BW_MASK
)|WL_CHANSPEC_CTL_SB_LOWER
),
187 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_40
)&WL_CHANSPEC_BW_MASK
)|WL_CHANSPEC_CTL_SB_LOWER
),
188 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_40
)&WL_CHANSPEC_BW_MASK
)|WL_CHANSPEC_CTL_SB_LOWER
),
189 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_40
)&WL_CHANSPEC_BW_MASK
)|WL_CHANSPEC_CTL_SB_LOWER
),
190 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_40
)&WL_CHANSPEC_BW_MASK
)|WL_CHANSPEC_CTL_SB_LOWER
),
191 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_40
)&WL_CHANSPEC_BW_MASK
)|WL_CHANSPEC_CTL_SB_UPPER
),
192 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_40
)&WL_CHANSPEC_BW_MASK
)|WL_CHANSPEC_CTL_SB_UPPER
),
193 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_40
)&WL_CHANSPEC_BW_MASK
)|WL_CHANSPEC_CTL_SB_UPPER
),
194 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_40
)&WL_CHANSPEC_BW_MASK
)|WL_CHANSPEC_CTL_SB_UPPER
),
195 (WL_CHANSPEC_BAND_5G
|((WL_CHANSPEC_BW_40
)&WL_CHANSPEC_BW_MASK
)|WL_CHANSPEC_CTL_SB_UPPER
),
196 (WL_CHANSPEC_BAND_2G
|((WL_CHANSPEC_BW_40
)&WL_CHANSPEC_BW_MASK
)|WL_CHANSPEC_CTL_SB_LOWER
),
197 (WL_CHANSPEC_BAND_2G
|((WL_CHANSPEC_BW_40
)&WL_CHANSPEC_BW_MASK
)|WL_CHANSPEC_CTL_SB_UPPER
),
200 /* convert bandwidth from chanspec to MHz */
202 bw_chspec_to_mhz(chanspec_t chspec
)
206 bw
= (chspec
& WL_CHANSPEC_BW_MASK
) >> WL_CHANSPEC_BW_SHIFT
;
207 return (bw
>= WF_NUM_BW
? 0 : wf_chspec_bw_mhz
[bw
]);
210 /* bw in MHz, return the channel count from the center channel to the
211 * the channel at the edge of the band
214 center_chan_to_edge(uint bw
)
216 /* edge channels separated by BW - 10MHz on each side
217 * delta from cf to edge is half of that,
218 * MHz to channel num conversion is 5MHz/channel
220 return (uint8
)(((bw
- 20) / 2) / 5);
223 /* return channel number of the low edge of the band
224 * given the center channel and BW
227 channel_low_edge(uint center_ch
, uint bw
)
229 return (uint8
)(center_ch
- center_chan_to_edge(bw
));
232 /* return side band number given center channel and control channel
236 channel_to_sb(uint center_ch
, uint ctl_ch
, uint bw
)
238 uint lowest
= channel_low_edge(center_ch
, bw
);
241 if ((ctl_ch
- lowest
) % 4) {
242 /* bad ctl channel, not mult 4 */
246 sb
= ((ctl_ch
- lowest
) / 4);
248 /* sb must be a index to a 20MHz channel in range */
249 if (sb
>= (bw
/ 20)) {
250 /* ctl_ch must have been too high for the center_ch */
257 /* return control channel given center channel and side band */
259 channel_to_ctl_chan(uint center_ch
, uint bw
, uint sb
)
261 return (uint8
)(channel_low_edge(center_ch
, bw
) + sb
* 4);
264 /* return index of 80MHz channel from channel number
268 channel_80mhz_to_id(uint ch
)
271 for (i
= 0; i
< WF_NUM_5G_80M_CHANS
; i
++) {
272 if (ch
== wf_5g_80m_chans
[i
])
279 /* wrapper function for wf_chspec_ntoa. In case of an error it puts
280 * the original chanspec in the output buffer, prepended with "invalid".
281 * Can be directly used in print routines as it takes care of null
284 wf_chspec_ntoa_ex(chanspec_t chspec
, char *buf
)
286 if (wf_chspec_ntoa(chspec
, buf
) == NULL
)
287 snprintf(buf
, CHANSPEC_STR_LEN
, "invalid 0x%04x", chspec
);
291 /* given a chanspec and a string buffer, format the chanspec as a
292 * string, and return the original pointer a.
293 * Min buffer length must be CHANSPEC_STR_LEN.
294 * On error return NULL
297 wf_chspec_ntoa(chanspec_t chspec
, char *buf
)
302 if (wf_chspec_malformed(chspec
))
307 /* check for non-default band spec */
308 if ((CHSPEC_IS2G(chspec
) && CHSPEC_CHANNEL(chspec
) > CH_MAX_2G_CHANNEL
) ||
309 (CHSPEC_IS5G(chspec
) && CHSPEC_CHANNEL(chspec
) <= CH_MAX_2G_CHANNEL
))
310 band
= (CHSPEC_IS2G(chspec
)) ? "2g" : "5g";
313 ctl_chan
= wf_chspec_ctlchan(chspec
);
315 /* bandwidth and ctl sideband */
316 if (CHSPEC_IS20(chspec
)) {
317 snprintf(buf
, CHANSPEC_STR_LEN
, "%s%d", band
, ctl_chan
);
318 } else if (!CHSPEC_IS8080(chspec
)) {
322 bw
= wf_chspec_bw_str
[(chspec
& WL_CHANSPEC_BW_MASK
) >> WL_CHANSPEC_BW_SHIFT
];
324 #ifdef CHANSPEC_NEW_40MHZ_FORMAT
325 /* ctl sideband string if needed for 2g 40MHz */
326 if (CHSPEC_IS40(chspec
) && CHSPEC_IS2G(chspec
)) {
327 sb
= CHSPEC_SB_UPPER(chspec
) ? "u" : "l";
330 snprintf(buf
, CHANSPEC_STR_LEN
, "%s%d/%s%s", band
, ctl_chan
, bw
, sb
);
332 /* ctl sideband string instead of BW for 40MHz */
333 if (CHSPEC_IS40(chspec
)) {
334 sb
= CHSPEC_SB_UPPER(chspec
) ? "u" : "l";
335 snprintf(buf
, CHANSPEC_STR_LEN
, "%s%d%s", band
, ctl_chan
, sb
);
337 snprintf(buf
, CHANSPEC_STR_LEN
, "%s%d/%s", band
, ctl_chan
, bw
);
339 #endif /* CHANSPEC_NEW_40MHZ_FORMAT */
343 uint chan1
= (chspec
& WL_CHANSPEC_CHAN1_MASK
) >> WL_CHANSPEC_CHAN1_SHIFT
;
344 uint chan2
= (chspec
& WL_CHANSPEC_CHAN2_MASK
) >> WL_CHANSPEC_CHAN2_SHIFT
;
346 /* convert to channel number */
347 chan1
= (chan1
< WF_NUM_5G_80M_CHANS
) ? wf_5g_80m_chans
[chan1
] : 0;
348 chan2
= (chan2
< WF_NUM_5G_80M_CHANS
) ? wf_5g_80m_chans
[chan2
] : 0;
350 /* Outputs a max of CHANSPEC_STR_LEN chars including '\0' */
351 snprintf(buf
, CHANSPEC_STR_LEN
, "%d/80+80/%d-%d", ctl_chan
, chan1
, chan2
);
358 read_uint(const char **p
, unsigned int *num
)
363 val
= strtoul(*p
, &endp
, 10);
364 /* if endp is the initial pointer value, then a number was not read */
368 /* advance the buffer pointer to the end of the integer string */
370 /* return the parsed integer */
371 *num
= (unsigned int)val
;
376 /* given a chanspec string, convert to a chanspec.
380 wf_chspec_aton(const char *a
)
383 uint chspec_ch
, chspec_band
, bw
, chspec_bw
, chspec_sb
;
386 char c
, sb_ul
= '\0';
391 chspec_ch
= ch1
= ch2
= 0;
393 /* parse channel num or band */
394 if (!read_uint(&a
, &num
))
396 /* if we are looking at a 'g', then the first number was a band */
397 c
= tolower((int)a
[0]);
399 a
++; /* consume the char */
401 /* band must be "2" or "5" */
403 chspec_band
= WL_CHANSPEC_BAND_2G
;
405 chspec_band
= WL_CHANSPEC_BAND_5G
;
409 /* read the channel number */
410 if (!read_uint(&a
, &ctl_ch
))
413 c
= tolower((int)a
[0]);
416 /* first number is channel, use default for band */
418 chspec_band
= ((ctl_ch
<= CH_MAX_2G_CHANNEL
) ?
419 WL_CHANSPEC_BAND_2G
: WL_CHANSPEC_BAND_5G
);
423 /* default BW of 20MHz */
424 chspec_bw
= WL_CHANSPEC_BW_20
;
428 a
++; /* consume the 'u','l', or '/' */
431 if (c
== 'u' || c
== 'l') {
433 chspec_bw
= WL_CHANSPEC_BW_40
;
437 /* next letter must be '/' */
442 if (!read_uint(&a
, &bw
))
445 /* convert to chspec value */
447 chspec_bw
= WL_CHANSPEC_BW_2P5
;
448 } else if (bw
== 5) {
449 chspec_bw
= WL_CHANSPEC_BW_5
;
450 } else if (bw
== 10) {
451 chspec_bw
= WL_CHANSPEC_BW_10
;
452 } else if (bw
== 20) {
453 chspec_bw
= WL_CHANSPEC_BW_20
;
454 } else if (bw
== 40) {
455 chspec_bw
= WL_CHANSPEC_BW_40
;
456 } else if (bw
== 80) {
457 chspec_bw
= WL_CHANSPEC_BW_80
;
458 } else if (bw
== 160) {
459 chspec_bw
= WL_CHANSPEC_BW_160
;
464 /* So far we have <band>g<chan>/<bw>
465 * Can now be followed by u/l if bw = 40,
466 * or '+80' if bw = 80, to make '80+80' bw,
467 * or '.5' if bw = 2.5 to make '2.5' bw .
470 c
= tolower((int)a
[0]);
472 /* if we have a 2g/40 channel, we should have a l/u spec now */
473 if (chspec_band
== WL_CHANSPEC_BAND_2G
&& bw
== 40) {
474 if (c
== 'u' || c
== 'l') {
475 a
++; /* consume the u/l char */
481 /* check for 80+80 */
484 const char plus80
[] = "80/";
486 /* must be looking at '+80/'
487 * check and consume this string.
489 chspec_bw
= WL_CHANSPEC_BW_8080
;
491 a
++; /* consume the char '+' */
493 /* consume the '80/' string */
494 for (i
= 0; i
< 3; i
++) {
495 if (*a
++ != plus80
[i
]) {
500 /* read primary 80MHz channel */
501 if (!read_uint(&a
, &ch1
))
504 /* must followed by '-' */
507 a
++; /* consume the char */
509 /* read secondary 80MHz channel */
510 if (!read_uint(&a
, &ch2
))
512 } else if (c
== '.') {
514 /* must be looking at '.5'
515 * check and consume this string.
517 chspec_bw
= WL_CHANSPEC_BW_2P5
;
519 a
++; /* consume the char '.' */
521 /* consume the '5' string */
528 /* skip trailing white space */
529 while (a
[0] == ' ') {
533 /* must be end of string */
537 /* Now have all the chanspec string parts read;
538 * chspec_band, ctl_ch, chspec_bw, sb_ul, ch1, ch2.
539 * chspec_band and chspec_bw are chanspec values.
540 * Need to convert ctl_ch, sb_ul, and ch1,ch2 into
541 * a center channel (or two) and sideband.
544 /* if a sb u/l string was given, just use that,
545 * guaranteed to be bw = 40 by sting parse.
549 chspec_ch
= UPPER_20_SB(ctl_ch
);
550 chspec_sb
= WL_CHANSPEC_CTL_SB_LLL
;
551 } else if (sb_ul
== 'u') {
552 chspec_ch
= LOWER_20_SB(ctl_ch
);
553 chspec_sb
= WL_CHANSPEC_CTL_SB_LLU
;
556 /* if the bw is 20, center and sideband are trivial */
557 else if (BW_LE20(chspec_bw
)) {
559 chspec_sb
= WL_CHANSPEC_CTL_SB_NONE
;
561 /* if the bw is 40/80/160, not 80+80, a single method
562 * can be used to to find the center and sideband
564 else if (chspec_bw
!= WL_CHANSPEC_BW_8080
) {
565 /* figure out ctl sideband based on ctl channel and bandwidth */
566 const uint8
*center_ch
= NULL
;
570 if (chspec_bw
== WL_CHANSPEC_BW_40
) {
571 center_ch
= wf_5g_40m_chans
;
572 num_ch
= WF_NUM_5G_40M_CHANS
;
573 } else if (chspec_bw
== WL_CHANSPEC_BW_80
) {
574 center_ch
= wf_5g_80m_chans
;
575 num_ch
= WF_NUM_5G_80M_CHANS
;
576 } else if (chspec_bw
== WL_CHANSPEC_BW_160
) {
577 center_ch
= wf_5g_160m_chans
;
578 num_ch
= WF_NUM_5G_160M_CHANS
;
583 for (i
= 0; i
< num_ch
; i
++) {
584 sb
= channel_to_sb(center_ch
[i
], ctl_ch
, bw
);
586 chspec_ch
= center_ch
[i
];
587 chspec_sb
= sb
<< WL_CHANSPEC_CTL_SB_SHIFT
;
592 /* check for no matching sb/center */
597 /* Otherwise, bw is 80+80. Figure out channel pair and sb */
599 int ch1_id
= 0, ch2_id
= 0;
602 /* look up the channel ID for the specified channel numbers */
603 ch1_id
= channel_80mhz_to_id(ch1
);
604 ch2_id
= channel_80mhz_to_id(ch2
);
606 /* validate channels */
607 if (ch1_id
< 0 || ch2_id
< 0)
610 /* combine 2 channel IDs in channel field of chspec */
611 chspec_ch
= (((uint
)ch1_id
<< WL_CHANSPEC_CHAN1_SHIFT
) |
612 ((uint
)ch2_id
<< WL_CHANSPEC_CHAN2_SHIFT
));
614 /* figure out primary 20 MHz sideband */
616 /* is the primary channel contained in the 1st 80MHz channel? */
617 sb
= channel_to_sb(ch1
, ctl_ch
, bw
);
619 /* no match for primary channel 'ctl_ch' in segment0 80MHz channel */
623 chspec_sb
= sb
<< WL_CHANSPEC_CTL_SB_SHIFT
;
626 chspec
= (chspec_ch
| chspec_band
| chspec_bw
| chspec_sb
);
628 if (wf_chspec_malformed(chspec
))
635 * Verify the chanspec is using a legal set of parameters, i.e. that the
636 * chanspec specified a band, bw, ctl_sb and channel and that the
637 * combination could be legal given any set of circumstances.
638 * RETURNS: TRUE is the chanspec is malformed, false if it looks good.
641 wf_chspec_malformed(chanspec_t chanspec
)
643 uint chspec_bw
= CHSPEC_BW(chanspec
);
644 uint chspec_ch
= CHSPEC_CHANNEL(chanspec
);
646 /* must be 2G or 5G band */
647 if (CHSPEC_IS2G(chanspec
)) {
648 /* must be valid bandwidth */
649 if (!BW_LE40(chspec_bw
)) {
652 } else if (CHSPEC_IS5G(chanspec
)) {
653 if (chspec_bw
== WL_CHANSPEC_BW_8080
) {
656 /* channel IDs in 80+80 must be in range */
657 ch1_id
= CHSPEC_CHAN1(chanspec
);
658 ch2_id
= CHSPEC_CHAN2(chanspec
);
659 if (ch1_id
>= WF_NUM_5G_80M_CHANS
|| ch2_id
>= WF_NUM_5G_80M_CHANS
)
662 } else if (BW_LE160(chspec_bw
)) {
663 if (chspec_ch
> MAXCHANNEL
) {
667 /* invalid bandwidth */
671 /* must be 2G or 5G band */
675 /* side band needs to be consistent with bandwidth */
676 if (BW_LE20(chspec_bw
)) {
677 if (CHSPEC_CTL_SB(chanspec
) != WL_CHANSPEC_CTL_SB_LLL
)
679 } else if (chspec_bw
== WL_CHANSPEC_BW_40
) {
680 if (CHSPEC_CTL_SB(chanspec
) > WL_CHANSPEC_CTL_SB_LLU
)
682 } else if (chspec_bw
== WL_CHANSPEC_BW_80
||
683 chspec_bw
== WL_CHANSPEC_BW_8080
) {
684 if (CHSPEC_CTL_SB(chanspec
) > WL_CHANSPEC_CTL_SB_LUU
)
687 else if (chspec_bw
== WL_CHANSPEC_BW_160
) {
688 ASSERT(CHSPEC_CTL_SB(chanspec
) <= WL_CHANSPEC_CTL_SB_UUU
);
694 * Verify the chanspec specifies a valid channel according to 802.11.
695 * RETURNS: TRUE if the chanspec is a valid 802.11 channel
698 wf_chspec_valid(chanspec_t chanspec
)
700 uint chspec_bw
= CHSPEC_BW(chanspec
);
701 uint chspec_ch
= CHSPEC_CHANNEL(chanspec
);
703 if (wf_chspec_malformed(chanspec
))
706 if (CHSPEC_IS2G(chanspec
)) {
707 /* must be valid bandwidth and channel range */
708 if (BW_LE20(chspec_bw
)) {
709 if (chspec_ch
>= 1 && chspec_ch
<= 14)
711 } else if (chspec_bw
== WL_CHANSPEC_BW_40
) {
712 if (chspec_ch
>= 3 && chspec_ch
<= 11)
715 } else if (CHSPEC_IS5G(chanspec
)) {
716 if (chspec_bw
== WL_CHANSPEC_BW_8080
) {
719 ch1
= wf_5g_80m_chans
[CHSPEC_CHAN1(chanspec
)];
720 ch2
= wf_5g_80m_chans
[CHSPEC_CHAN2(chanspec
)];
722 /* the two channels must be separated by more than 80MHz by VHT req */
723 if ((ch2
> ch1
+ CH_80MHZ_APART
) ||
724 (ch1
> ch2
+ CH_80MHZ_APART
))
727 const uint8
*center_ch
;
730 if (BW_LE40(chspec_bw
)) {
731 center_ch
= wf_5g_40m_chans
;
732 num_ch
= WF_NUM_5G_40M_CHANS
;
733 } else if (chspec_bw
== WL_CHANSPEC_BW_80
) {
734 center_ch
= wf_5g_80m_chans
;
735 num_ch
= WF_NUM_5G_80M_CHANS
;
736 } else if (chspec_bw
== WL_CHANSPEC_BW_160
) {
737 center_ch
= wf_5g_160m_chans
;
738 num_ch
= WF_NUM_5G_160M_CHANS
;
740 /* invalid bandwidth */
744 /* check for a valid center channel */
745 if (BW_LE20(chspec_bw
)) {
746 /* We don't have an array of legal 20MHz 5G channels, but they are
747 * each side of the legal 40MHz channels. Check the chanspec
748 * channel against either side of the 40MHz channels.
750 for (i
= 0; i
< num_ch
; i
++) {
751 if (chspec_ch
== (uint
)LOWER_20_SB(center_ch
[i
]) ||
752 chspec_ch
== (uint
)UPPER_20_SB(center_ch
[i
]))
753 break; /* match found */
757 /* check for channel 165 which is not the side band
758 * of 40MHz 5G channel
760 if (chspec_ch
== 165)
763 /* check for legacy JP channels on failure */
764 if (chspec_ch
== 34 || chspec_ch
== 38 ||
765 chspec_ch
== 42 || chspec_ch
== 46)
769 /* check the chanspec channel to each legal channel */
770 for (i
= 0; i
< num_ch
; i
++) {
771 if (chspec_ch
== center_ch
[i
])
772 break; /* match found */
787 * This function returns the channel number that control traffic is being sent on, for 20MHz
788 * channels this is just the channel number, for 40MHZ, 80MHz, 160MHz channels it is the 20MHZ
789 * sideband depending on the chanspec selected
792 wf_chspec_ctlchan(chanspec_t chspec
)
798 ASSERT(!wf_chspec_malformed(chspec
));
800 /* Is there a sideband ? */
801 if (CHSPEC_BW_LE20(chspec
)) {
802 return CHSPEC_CHANNEL(chspec
);
804 sb
= CHSPEC_CTL_SB(chspec
) >> WL_CHANSPEC_CTL_SB_SHIFT
;
806 if (CHSPEC_IS8080(chspec
)) {
807 /* For an 80+80 MHz channel, the sideband 'sb' field is an 80 MHz sideband
808 * (LL, LU, UL, LU) for the 80 MHz frequency segment 0.
810 uint chan_id
= CHSPEC_CHAN1(chspec
);
814 /* convert from channel index to channel number */
815 center_chan
= wf_5g_80m_chans
[chan_id
];
818 bw_mhz
= bw_chspec_to_mhz(chspec
);
819 center_chan
= CHSPEC_CHANNEL(chspec
) >> WL_CHANSPEC_CHAN_SHIFT
;
822 return (channel_to_ctl_chan(center_chan
, bw_mhz
, sb
));
826 /* given a chanspec, return the bandwidth string */
828 wf_chspec_to_bw_str(chanspec_t chspec
)
830 return wf_chspec_bw_str
[(CHSPEC_BW(chspec
) >> WL_CHANSPEC_BW_SHIFT
)];
834 * This function returns the chanspec of the control channel of a given chanspec
837 wf_chspec_ctlchspec(chanspec_t chspec
)
839 chanspec_t ctl_chspec
= chspec
;
842 ASSERT(!wf_chspec_malformed(chspec
));
844 /* Is there a sideband ? */
845 if (!CHSPEC_BW_LE20(chspec
)) {
846 ctl_chan
= wf_chspec_ctlchan(chspec
);
847 ctl_chspec
= ctl_chan
| WL_CHANSPEC_BW_20
;
848 ctl_chspec
|= CHSPEC_BAND(chspec
);
853 /* return chanspec given control channel and bandwidth
857 wf_channel2chspec(uint ctl_ch
, uint bw
)
860 const uint8
*center_ch
= NULL
;
865 chspec
= ((ctl_ch
<= CH_MAX_2G_CHANNEL
) ? WL_CHANSPEC_BAND_2G
: WL_CHANSPEC_BAND_5G
);
869 if (bw
== WL_CHANSPEC_BW_40
) {
870 center_ch
= wf_5g_40m_chans
;
871 num_ch
= WF_NUM_5G_40M_CHANS
;
873 } else if (bw
== WL_CHANSPEC_BW_80
) {
874 center_ch
= wf_5g_80m_chans
;
875 num_ch
= WF_NUM_5G_80M_CHANS
;
877 } else if (bw
== WL_CHANSPEC_BW_160
) {
878 center_ch
= wf_5g_160m_chans
;
879 num_ch
= WF_NUM_5G_160M_CHANS
;
881 } else if (BW_LE20(bw
)) {
888 for (i
= 0; i
< num_ch
; i
++) {
889 sb
= channel_to_sb(center_ch
[i
], ctl_ch
, bw
);
891 chspec
|= center_ch
[i
];
892 chspec
|= (sb
<< WL_CHANSPEC_CTL_SB_SHIFT
);
897 /* check for no matching sb/center */
906 * This function returns the chanspec for the primary 40MHz of an 80MHz channel.
907 * The control sideband specifies the same 20MHz channel that the 80MHz channel is using
908 * as the primary 20MHz channel.
910 extern chanspec_t
wf_chspec_primary40_chspec(chanspec_t chspec
)
912 chanspec_t chspec40
= chspec
;
916 ASSERT(!wf_chspec_malformed(chspec
));
918 /* if the chanspec is > 80MHz, use the helper routine to find the primary 80 MHz channel */
919 if (CHSPEC_IS8080(chspec
) || CHSPEC_IS160(chspec
)) {
920 chspec
= wf_chspec_primary80_chspec(chspec
);
923 /* determine primary 40 MHz sub-channel of an 80 MHz chanspec */
924 if (CHSPEC_IS80(chspec
)) {
925 center_chan
= CHSPEC_CHANNEL(chspec
);
926 sb
= CHSPEC_CTL_SB(chspec
);
928 if (sb
< WL_CHANSPEC_CTL_SB_UL
) {
929 /* Primary 40MHz is on lower side */
930 center_chan
-= CH_20MHZ_APART
;
931 /* sideband bits are the same for LL/LU and L/U */
933 /* Primary 40MHz is on upper side */
934 center_chan
+= CH_20MHZ_APART
;
935 /* sideband bits need to be adjusted by UL offset */
936 sb
-= WL_CHANSPEC_CTL_SB_UL
;
939 /* Create primary 40MHz chanspec */
940 chspec40
= (WL_CHANSPEC_BAND_5G
| WL_CHANSPEC_BW_40
|
948 * Return the channel number for a given frequency and base frequency.
949 * The returned channel number is relative to the given base frequency.
950 * If the given base frequency is zero, a base frequency of 5 GHz is assumed for
951 * frequencies from 5 - 6 GHz, and 2.407 GHz is assumed for 2.4 - 2.5 GHz.
953 * Frequency is specified in MHz.
954 * The base frequency is specified as (start_factor * 500 kHz).
955 * Constants WF_CHAN_FACTOR_2_4_G, WF_CHAN_FACTOR_5_G are defined for
956 * 2.4 GHz and 5 GHz bands.
958 * The returned channel will be in the range [1, 14] in the 2.4 GHz band
959 * and [0, 200] otherwise.
960 * -1 is returned if the start_factor is WF_CHAN_FACTOR_2_4_G and the
961 * frequency is not a 2.4 GHz channel, or if the frequency is not and even
962 * multiple of 5 MHz from the base frequency to the base plus 1 GHz.
964 * Reference 802.11 REVma, section 17.3.8.3, and 802.11B section 18.4.6.2
967 wf_mhz2channel(uint freq
, uint start_factor
)
973 /* take the default channel start frequency */
974 if (start_factor
== 0) {
975 if (freq
>= 2400 && freq
<= 2500)
976 start_factor
= WF_CHAN_FACTOR_2_4_G
;
977 else if (freq
>= 5000 && freq
<= 6000)
978 start_factor
= WF_CHAN_FACTOR_5_G
;
981 if (freq
== 2484 && start_factor
== WF_CHAN_FACTOR_2_4_G
)
984 base
= start_factor
/ 2;
986 /* check that the frequency is in 1GHz range of the base */
987 if ((freq
< base
) || (freq
> base
+ 1000))
990 offset
= freq
- base
;
993 /* check that frequency is a 5MHz multiple from the base */
994 if (offset
!= (ch
* 5))
997 /* restricted channel range check for 2.4G */
998 if (start_factor
== WF_CHAN_FACTOR_2_4_G
&& (ch
< 1 || ch
> 13))
1005 * Return the center frequency in MHz of the given channel and base frequency.
1006 * The channel number is interpreted relative to the given base frequency.
1008 * The valid channel range is [1, 14] in the 2.4 GHz band and [0, 200] otherwise.
1009 * The base frequency is specified as (start_factor * 500 kHz).
1010 * Constants WF_CHAN_FACTOR_2_4_G, WF_CHAN_FACTOR_4_G, and WF_CHAN_FACTOR_5_G
1011 * are defined for 2.4 GHz, 4 GHz, and 5 GHz bands.
1012 * The channel range of [1, 14] is only checked for a start_factor of
1013 * WF_CHAN_FACTOR_2_4_G (4814 = 2407 * 2).
1014 * Odd start_factors produce channels on .5 MHz boundaries, in which case
1015 * the answer is rounded down to an integral MHz.
1016 * -1 is returned for an out of range channel.
1018 * Reference 802.11 REVma, section 17.3.8.3, and 802.11B section 18.4.6.2
1021 wf_channel2mhz(uint ch
, uint start_factor
)
1025 if ((start_factor
== WF_CHAN_FACTOR_2_4_G
&& (ch
< 1 || ch
> 14)) ||
1028 else if ((start_factor
== WF_CHAN_FACTOR_2_4_G
) && (ch
== 14))
1031 freq
= ch
* 5 + start_factor
/ 2;
1036 static const uint16 sidebands
[] = {
1037 WL_CHANSPEC_CTL_SB_LLL
, WL_CHANSPEC_CTL_SB_LLU
,
1038 WL_CHANSPEC_CTL_SB_LUL
, WL_CHANSPEC_CTL_SB_LUU
,
1039 WL_CHANSPEC_CTL_SB_ULL
, WL_CHANSPEC_CTL_SB_ULU
,
1040 WL_CHANSPEC_CTL_SB_UUL
, WL_CHANSPEC_CTL_SB_UUU
1044 * Returns the chanspec 80Mhz channel corresponding to the following input
1047 * primary_channel - primary 20Mhz channel
1048 * center_channel - center frequecny of the 80Mhz channel
1050 * The center_channel can be one of {42, 58, 106, 122, 138, 155}
1052 * returns INVCHANSPEC in case of error
1055 wf_chspec_80(uint8 center_channel
, uint8 primary_channel
)
1058 chanspec_t chanspec
= INVCHANSPEC
;
1059 chanspec_t chanspec_cur
;
1062 for (i
= 0; i
< WF_NUM_SIDEBANDS_80MHZ
; i
++) {
1063 chanspec_cur
= CH80MHZ_CHSPEC(center_channel
, sidebands
[i
]);
1064 if (primary_channel
== wf_chspec_ctlchan(chanspec_cur
)) {
1065 chanspec
= chanspec_cur
;
1069 /* If the loop ended early, we are good, otherwise we did not
1070 * find a 80MHz chanspec with the given center_channel that had a primary channel
1071 *matching the given primary_channel.
1077 * Returns the 80+80 chanspec corresponding to the following input parameters
1079 * primary_20mhz - Primary 20 MHz channel
1080 * chan0 - center channel number of one frequency segment
1081 * chan1 - center channel number of the other frequency segment
1083 * Parameters chan0 and chan1 are channel numbers in {42, 58, 106, 122, 138, 155}.
1084 * The primary channel must be contained in one of the 80MHz channels. This routine
1085 * will determine which frequency segment is the primary 80 MHz segment.
1087 * Returns INVCHANSPEC in case of error.
1089 * Refer to IEEE802.11ac section 22.3.14 "Channelization".
1092 wf_chspec_get8080_chspec(uint8 primary_20mhz
, uint8 chan0
, uint8 chan1
)
1095 uint16 chanspec
= 0;
1096 int chan0_id
= 0, chan1_id
= 0;
1099 chan0_id
= channel_80mhz_to_id(chan0
);
1100 chan1_id
= channel_80mhz_to_id(chan1
);
1102 /* make sure the channel numbers were valid */
1103 if (chan0_id
== -1 || chan1_id
== -1)
1106 /* does the primary channel fit with the 1st 80MHz channel ? */
1107 sb
= channel_to_sb(chan0
, primary_20mhz
, 80);
1109 /* yes, so chan0 is frequency segment 0, and chan1 is seg 1 */
1113 /* no, so does the primary channel fit with the 2nd 80MHz channel ? */
1114 sb
= channel_to_sb(chan1
, primary_20mhz
, 80);
1116 /* no match for ctl_ch to either 80MHz center channel */
1119 /* swapped, so chan1 is frequency segment 0, and chan0 is seg 1 */
1124 chanspec
= ((seg0
<< WL_CHANSPEC_CHAN1_SHIFT
) |
1125 (seg1
<< WL_CHANSPEC_CHAN2_SHIFT
) |
1126 (sb
<< WL_CHANSPEC_CTL_SB_SHIFT
) |
1127 WL_CHANSPEC_BW_8080
|
1128 WL_CHANSPEC_BAND_5G
);
1134 * This function returns the 80Mhz channel for the given id.
1137 wf_chspec_get80Mhz_ch(uint8 chan_80Mhz_id
)
1139 if (chan_80Mhz_id
< WF_NUM_5G_80M_CHANS
)
1140 return wf_5g_80m_chans
[chan_80Mhz_id
];
1146 * Returns the primary 80 Mhz channel for the provided chanspec
1148 * chanspec - Input chanspec for which the 80MHz primary channel has to be retrieved
1150 * returns -1 in case the provided channel is 20/40 Mhz chanspec
1154 wf_chspec_primary80_channel(chanspec_t chanspec
)
1156 uint8 primary80_chan
;
1158 if (CHSPEC_IS80(chanspec
)) {
1159 primary80_chan
= CHSPEC_CHANNEL(chanspec
);
1161 else if (CHSPEC_IS8080(chanspec
)) {
1162 /* Channel ID 1 corresponds to frequency segment 0, the primary 80 MHz segment */
1163 primary80_chan
= wf_chspec_get80Mhz_ch(CHSPEC_CHAN1(chanspec
));
1165 else if (CHSPEC_IS160(chanspec
)) {
1166 uint8 center_chan
= CHSPEC_CHANNEL(chanspec
);
1167 uint sb
= CHSPEC_CTL_SB(chanspec
) >> WL_CHANSPEC_CTL_SB_SHIFT
;
1169 /* based on the sb value primary 80 channel can be retrieved
1170 * if sb is in range 0 to 3 the lower band is the 80Mhz primary band
1173 primary80_chan
= center_chan
- CH_40MHZ_APART
;
1175 /* if sb is in range 4 to 7 the upper band is the 80Mhz primary band */
1178 primary80_chan
= center_chan
+ CH_40MHZ_APART
;
1182 /* for 20 and 40 Mhz */
1183 primary80_chan
= -1;
1185 return primary80_chan
;
1189 * Returns the secondary 80 Mhz channel for the provided chanspec
1191 * chanspec - Input chanspec for which the 80MHz secondary channel has to be retrieved
1193 * returns -1 in case the provided channel is 20/40/80 Mhz chanspec
1196 wf_chspec_secondary80_channel(chanspec_t chanspec
)
1198 uint8 secondary80_chan
;
1200 if (CHSPEC_IS8080(chanspec
)) {
1201 secondary80_chan
= wf_chspec_get80Mhz_ch(CHSPEC_CHAN2(chanspec
));
1203 else if (CHSPEC_IS160(chanspec
)) {
1204 uint8 center_chan
= CHSPEC_CHANNEL(chanspec
);
1205 uint sb
= CHSPEC_CTL_SB(chanspec
) >> WL_CHANSPEC_CTL_SB_SHIFT
;
1207 /* based on the sb value secondary 80 channel can be retrieved
1208 * if sb is in range 0 to 3 upper band is the secondary 80Mhz band
1211 secondary80_chan
= center_chan
+ CH_40MHZ_APART
;
1213 /* if sb is in range 4 to 7 the lower band is the secondary 80Mhz band */
1216 secondary80_chan
= center_chan
- CH_40MHZ_APART
;
1220 /* for 20, 40, and 80 Mhz */
1221 secondary80_chan
= -1;
1223 return secondary80_chan
;
1227 * This function returns the chanspec for the primary 80MHz of an 160MHz or 80+80 channel.
1229 * chanspec - Input chanspec for which the primary 80Mhz chanspec has to be retreived
1231 * returns the input chanspec in case the provided chanspec is an 80 MHz chanspec
1232 * returns INVCHANSPEC in case the provided channel is 20/40 MHz chanspec
1235 wf_chspec_primary80_chspec(chanspec_t chspec
)
1237 chanspec_t chspec80
;
1241 ASSERT(!wf_chspec_malformed(chspec
));
1242 if (CHSPEC_IS80(chspec
)) {
1245 else if (CHSPEC_IS8080(chspec
)) {
1247 /* Channel ID 1 corresponds to frequency segment 0, the primary 80 MHz segment */
1248 center_chan
= wf_chspec_get80Mhz_ch(CHSPEC_CHAN1(chspec
));
1250 sb
= CHSPEC_CTL_SB(chspec
);
1252 /* Create primary 80MHz chanspec */
1253 chspec80
= (WL_CHANSPEC_BAND_5G
| WL_CHANSPEC_BW_80
| sb
| center_chan
);
1255 else if (CHSPEC_IS160(chspec
)) {
1256 center_chan
= CHSPEC_CHANNEL(chspec
);
1257 sb
= CHSPEC_CTL_SB(chspec
);
1259 if (sb
< WL_CHANSPEC_CTL_SB_ULL
) {
1260 /* Primary 80MHz is on lower side */
1261 center_chan
-= CH_40MHZ_APART
;
1264 /* Primary 80MHz is on upper side */
1265 center_chan
+= CH_40MHZ_APART
;
1266 sb
-= WL_CHANSPEC_CTL_SB_ULL
;
1268 /* Create primary 80MHz chanspec */
1269 chspec80
= (WL_CHANSPEC_BAND_5G
| WL_CHANSPEC_BW_80
| sb
| center_chan
);
1272 chspec80
= INVCHANSPEC
;
1280 wf_chspec_channel(chanspec_t chspec
)
1282 if (CHSPEC_IS8080(chspec
)) {
1283 return wf_chspec_primary80_channel(chspec
);
1286 return ((uint8
)((chspec
) & WL_CHANSPEC_CHAN_MASK
));
1289 #endif /* WL11AC_80P80 */
1291 /* This routine returns the chanspec for a given operating class and
1295 wf_channel_create_chspec_frm_opclass(uint8 opclass
, uint8 channel
)
1297 chanspec_t chanspec
= 0;
1298 uint16 opclass_info
= 0;
1299 uint16 lookupindex
= 0;
1332 if (lookupindex
< 33) {
1333 opclass_info
= opclass_data
[lookupindex
-1];
1336 opclass_info
= opclass_data
[11];
1338 chanspec
= opclass_info
| (uint16
)channel
;
1342 /* This routine returns the opclass for a given chanspec */
1344 wf_channel_create_opclass_frm_chspec(chanspec_t chspec
)
1346 BCM_REFERENCE(chspec
);
1347 /* TODO: Implement this function ! */
1348 return 12; /* opclass 12 for basic 2G channels */