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) 2012, Broadcom Corporation. All Rights Reserved.
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
15 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
17 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * $Id: bcmwifi_channels.c 309193 2012-01-19 00:03:57Z $
28 #define strtoul(nptr, endptr, base) bcm_strtoul((nptr), (endptr), (base))
29 #define tolower(c) (bcm_isupper((c)) ? ((c) + 'a' - 'A') : (c))
37 #endif /* BCMDRIVER */
40 /* temporary for transitional compatibility */
43 #include <bcmwifi_channels.h>
46 #if defined(WIN32) && (defined(BCMDLL) || defined(WLMDLL))
47 #include <bcmstdlib.h> /* For wl/exe/GNUmakefile.brcm_wlu and GNUmakefile.wlm_dll */
52 /* Definitions for legacy Chanspec type */
54 /* Chanspec ASCII representation:
55 * <channel><band><bandwidth><ctl-sideband>
58 * <channel>: channel number of the 10MHz or 20MHz channel,
59 * or control sideband channel of 40MHz channel.
60 * <band>: A for 5GHz, B for 2.4GHz
61 * <bandwidth>: N for 10MHz, nothing for 20MHz or 40MHz
62 * (ctl-sideband spec implies 40MHz)
63 * <ctl-sideband>: U for upper, L for lower
65 * <band> may be omitted on input, and will be assumed to be
66 * 2.4GHz if channel number <= 14.
69 * 8 -> 2.4GHz channel 8, 20MHz
70 * 8b -> 2.4GHz channel 8, 20MHz
71 * 8l -> 2.4GHz channel 8, 40MHz, lower ctl sideband
72 * 8a -> 5GHz channel 8 (low 5 GHz band), 20MHz
73 * 36 -> 5GHz channel 36, 20MHz
74 * 36l -> 5GHz channel 36, 40MHz, lower ctl sideband
75 * 40u -> 5GHz channel 40, 40MHz, upper ctl sideband
76 * 180n -> channel 180, 10MHz
80 /* given a chanspec and a string buffer, format the chanspec as a
81 * string, and return the original pointer a.
82 * Min buffer length must be CHANSPEC_STR_LEN.
83 * On error return NULL
86 wf_chspec_ntoa(chanspec_t chspec
, char *buf
)
88 const char *band
, *bw
, *sb
;
94 channel
= CHSPEC_CHANNEL(chspec
);
95 /* check for non-default band spec */
96 if ((CHSPEC_IS2G(chspec
) && channel
> CH_MAX_2G_CHANNEL
) ||
97 (CHSPEC_IS5G(chspec
) && channel
<= CH_MAX_2G_CHANNEL
))
98 band
= (CHSPEC_IS2G(chspec
)) ? "b" : "a";
99 if (CHSPEC_IS40(chspec
)) {
100 if (CHSPEC_SB_UPPER(chspec
)) {
102 channel
+= CH_10MHZ_APART
;
105 channel
-= CH_10MHZ_APART
;
107 } else if (CHSPEC_IS10(chspec
)) {
111 /* Outputs a max of 6 chars including '\0' */
112 snprintf(buf
, 6, "%d%s%s%s", channel
, band
, bw
, sb
);
116 /* given a chanspec string, convert to a chanspec.
120 wf_chspec_aton(const char *a
)
123 uint channel
, band
, bw
, ctl_sb
;
126 channel
= strtoul(a
, &endp
, 10);
128 /* check for no digits parsed */
132 if (channel
> MAXCHANNEL
)
135 band
= ((channel
<= CH_MAX_2G_CHANNEL
) ? WL_CHANSPEC_BAND_2G
: WL_CHANSPEC_BAND_5G
);
136 bw
= WL_CHANSPEC_BW_20
;
137 ctl_sb
= WL_CHANSPEC_CTL_SB_NONE
;
145 /* parse the optional ['A' | 'B'] band spec */
146 if (c
== 'a' || c
== 'b') {
147 band
= (c
== 'a') ? WL_CHANSPEC_BAND_5G
: WL_CHANSPEC_BAND_2G
;
154 /* parse bandwidth 'N' (10MHz) or 40MHz ctl sideband ['L' | 'U'] */
156 bw
= WL_CHANSPEC_BW_10
;
157 } else if (c
== 'l') {
158 bw
= WL_CHANSPEC_BW_40
;
159 ctl_sb
= WL_CHANSPEC_CTL_SB_LOWER
;
160 /* adjust channel to center of 40MHz band */
161 if (channel
<= (MAXCHANNEL
- CH_20MHZ_APART
))
162 channel
+= CH_10MHZ_APART
;
165 } else if (c
== 'u') {
166 bw
= WL_CHANSPEC_BW_40
;
167 ctl_sb
= WL_CHANSPEC_CTL_SB_UPPER
;
168 /* adjust channel to center of 40MHz band */
169 if (channel
> CH_20MHZ_APART
)
170 channel
-= CH_10MHZ_APART
;
178 return (channel
| band
| bw
| ctl_sb
);
182 * Verify the chanspec is using a legal set of parameters, i.e. that the
183 * chanspec specified a band, bw, ctl_sb and channel and that the
184 * combination could be legal given any set of circumstances.
185 * RETURNS: TRUE is the chanspec is malformed, false if it looks good.
188 wf_chspec_malformed(chanspec_t chanspec
)
190 /* must be 2G or 5G band */
191 if (!CHSPEC_IS5G(chanspec
) && !CHSPEC_IS2G(chanspec
))
193 /* must be 20 or 40 bandwidth */
194 if (!CHSPEC_IS40(chanspec
) && !CHSPEC_IS20(chanspec
))
197 /* 20MHZ b/w must have no ctl sb, 40 must have a ctl sb */
198 if (CHSPEC_IS20(chanspec
)) {
199 if (!CHSPEC_SB_NONE(chanspec
))
202 if (!CHSPEC_SB_UPPER(chanspec
) && !CHSPEC_SB_LOWER(chanspec
))
210 * This function returns the channel number that control traffic is being sent on, for legacy
211 * channels this is just the channel number, for 40MHZ channels it is the upper or lower 20MHZ
212 * sideband depending on the chanspec selected
215 wf_chspec_ctlchan(chanspec_t chspec
)
219 /* Is there a sideband ? */
220 if (CHSPEC_CTL_SB(chspec
) == WL_CHANSPEC_CTL_SB_NONE
) {
221 return CHSPEC_CHANNEL(chspec
);
223 /* we only support 40MHZ with sidebands */
224 ASSERT(CHSPEC_BW(chspec
) == WL_CHANSPEC_BW_40
);
225 /* chanspec channel holds the centre frequency, use that and the
226 * side band information to reconstruct the control channel number
228 if (CHSPEC_CTL_SB(chspec
) == WL_CHANSPEC_CTL_SB_UPPER
) {
229 /* control chan is the upper 20 MHZ SB of the 40MHZ channel */
230 ctl_chan
= UPPER_20_SB(CHSPEC_CHANNEL(chspec
));
232 ASSERT(CHSPEC_CTL_SB(chspec
) == WL_CHANSPEC_CTL_SB_LOWER
);
233 /* control chan is the lower 20 MHZ SB of the 40MHZ channel */
234 ctl_chan
= LOWER_20_SB(CHSPEC_CHANNEL(chspec
));
242 wf_chspec_ctlchspec(chanspec_t chspec
)
244 chanspec_t ctl_chspec
= 0;
247 ASSERT(!wf_chspec_malformed(chspec
));
249 /* Is there a sideband ? */
250 if (CHSPEC_CTL_SB(chspec
) == WL_CHANSPEC_CTL_SB_NONE
) {
253 if (CHSPEC_CTL_SB(chspec
) == WL_CHANSPEC_CTL_SB_UPPER
) {
254 channel
= UPPER_20_SB(CHSPEC_CHANNEL(chspec
));
256 channel
= LOWER_20_SB(CHSPEC_CHANNEL(chspec
));
258 ctl_chspec
= channel
| WL_CHANSPEC_BW_20
| WL_CHANSPEC_CTL_SB_NONE
;
259 ctl_chspec
|= CHSPEC_BAND(chspec
);
264 #else /* D11AC_IOTYPES */
266 /* Definitions for D11AC capable Chanspec type */
268 /* Chanspec ASCII representation with 802.11ac capability:
269 * [<band> 'g'] <channel> ['/'<bandwidth> [<ctl-sideband>]['/'<1st80channel>'-'<2nd80channel>]]
272 * (optional) 2, 3, 4, 5 for 2.4GHz, 3GHz, 4GHz, and 5GHz respectively.
273 * Default value is 2g if channel <= 14, otherwise 5g.
275 * channel number of the 5MHz, 10MHz, 20MHz channel,
276 * or primary channel of 40MHz, 80MHz, 160MHz, or 80+80MHz channel.
278 * (optional) 5, 10, 20, 40, 80, 160, or 80+80. Default value is 20.
279 * <primary-sideband>:
280 * (only for 2.4GHz band 40MHz) U for upper sideband primary, L for lower.
282 * For 2.4GHz band 40MHz channels, the same primary channel may be the
283 * upper sideband for one 40MHz channel, and the lower sideband for an
284 * overlapping 40MHz channel. The U/L disambiguates which 40MHz channel
285 * is being specified.
287 * For 40MHz in the 5GHz band and all channel bandwidths greater than
288 * 40MHz, the U/L specificaion is not allowed since the channels are
289 * non-overlapping and the primary sub-band is derived from its
290 * position in the wide bandwidth channel.
294 * Required for 80+80, otherwise not allowed.
295 * Specifies the center channel of the first and second 80MHz band.
297 * In its simplest form, it is a 20MHz channel number, with the implied band
298 * of 2.4GHz if channel number <= 14, and 5GHz otherwise.
300 * To allow for backward compatibility with scripts, the old form for
301 * 40MHz channels is also allowed: <channel><ctl-sideband>
304 * primary channel of 40MHz, channel <= 14 is 2GHz, otherwise 5GHz
306 * "U" for upper, "L" for lower (or lower case "u" "l")
309 * Chanspec BW Center Ch Channel Range Primary Ch
312 * 52/40 40MHz 54 52-56 52
313 * 56/40 40MHz 54 52-56 56
314 * 52/80 80MHz 58 52-64 52
315 * 56/80 80MHz 58 52-64 56
316 * 60/80 80MHz 58 52-64 60
317 * 64/80 80MHz 58 52-64 64
318 * 52/160 160MHz 50 36-64 52
319 * 36/160 160MGz 50 36-64 36
320 * 36/80+80/42-106 80+80MHz 42,106 36-48,100-112 36
323 * Chanspec BW Center Ch Channel Range Primary Ch
327 * 6/40l 40MHz 8 6-10 6
329 * 6/40u 40MHz 4 2-6 6
333 /* bandwidth ASCII string */
334 static const char *wf_chspec_bw_str
[] =
346 static const uint8 wf_chspec_bw_mhz
[] =
347 {5, 10, 20, 40, 80, 160, 160};
350 (sizeof(wf_chspec_bw_mhz)/sizeof(uint8))
352 /* 40MHz channels in 5GHz band */
353 static const uint8 wf_5g_40m_chans
[] =
354 {38, 46, 54, 62, 102, 110, 118, 126, 134, 142, 151, 159};
355 #define WF_NUM_5G_40M_CHANS \
356 (sizeof(wf_5g_40m_chans)/sizeof(uint8))
358 /* 80MHz channels in 5GHz band */
359 static const uint8 wf_5g_80m_chans
[] =
360 {42, 58, 106, 122, 138, 155};
361 #define WF_NUM_5G_80M_CHANS \
362 (sizeof(wf_5g_80m_chans)/sizeof(uint8))
364 /* 160MHz channels in 5GHz band */
365 static const uint8 wf_5g_160m_chans
[] =
367 #define WF_NUM_5G_160M_CHANS \
368 (sizeof(wf_5g_160m_chans)/sizeof(uint8))
371 /* convert bandwidth from chanspec to MHz */
373 bw_chspec_to_mhz(chanspec_t chspec
)
377 bw
= (chspec
& WL_CHANSPEC_BW_MASK
) >> WL_CHANSPEC_BW_SHIFT
;
378 return (bw
>= WF_NUM_BW
? 0 : wf_chspec_bw_mhz
[bw
]);
381 /* bw in MHz, return the channel count from the center channel to the
382 * the channel at the edge of the band
385 center_chan_to_edge(uint bw
)
387 /* edge channels separated by BW - 10MHz on each side
388 * delta from cf to edge is half of that,
389 * MHz to channel num conversion is 5MHz/channel
391 return (uint8
)(((bw
- 20) / 2) / 5);
394 /* return channel number of the low edge of the band
395 * given the center channel and BW
398 channel_low_edge(uint center_ch
, uint bw
)
400 return (uint8
)(center_ch
- center_chan_to_edge(bw
));
403 /* return side band number given center channel and control channel
407 channel_to_sb(uint center_ch
, uint ctl_ch
, uint bw
)
409 uint lowest
= channel_low_edge(center_ch
, bw
);
412 if ((ctl_ch
- lowest
) % 4) {
413 /* bad ctl channel, not mult 4 */
417 sb
= ((ctl_ch
- lowest
) / 4);
419 /* sb must be a index to a 20MHz channel in range */
420 if (sb
>= (bw
/ 20)) {
421 /* ctl_ch must have been too high for the center_ch */
428 /* return control channel given center channel and side band */
430 channel_to_ctl_chan(uint center_ch
, uint bw
, uint sb
)
432 return (uint8
)(channel_low_edge(center_ch
, bw
) + sb
* 4);
435 /* return index of 80MHz channel from channel number
439 channel_80mhz_to_id(uint ch
)
442 for (i
= 0; i
< WF_NUM_5G_80M_CHANS
; i
++) {
443 if (ch
== wf_5g_80m_chans
[i
])
450 /* given a chanspec and a string buffer, format the chanspec as a
451 * string, and return the original pointer a.
452 * Min buffer length must be CHANSPEC_STR_LEN.
453 * On error return NULL
456 wf_chspec_ntoa(chanspec_t chspec
, char *buf
)
461 if (wf_chspec_malformed(chspec
))
466 /* check for non-default band spec */
467 if ((CHSPEC_IS2G(chspec
) && CHSPEC_CHANNEL(chspec
) > CH_MAX_2G_CHANNEL
) ||
468 (CHSPEC_IS5G(chspec
) && CHSPEC_CHANNEL(chspec
) <= CH_MAX_2G_CHANNEL
))
469 band
= (CHSPEC_IS2G(chspec
)) ? "2g" : "5g";
472 ctl_chan
= wf_chspec_ctlchan(chspec
);
474 /* bandwidth and ctl sideband */
475 if (CHSPEC_IS20(chspec
)) {
476 snprintf(buf
, CHANSPEC_STR_LEN
, "%s%d", band
, ctl_chan
);
477 } else if (!CHSPEC_IS8080(chspec
)) {
481 bw
= wf_chspec_bw_str
[(chspec
& WL_CHANSPEC_BW_MASK
) >> WL_CHANSPEC_BW_SHIFT
];
483 #ifdef CHANSPEC_NEW_40MHZ_FORMAT
484 /* ctl sideband string if needed for 2g 40MHz */
485 if (CHSPEC_IS40(chspec
) && CHSPEC_IS2G(chspec
)) {
486 sb
= CHSPEC_SB_UPPER(chspec
) ? "u" : "l";
489 snprintf(buf
, CHANSPEC_STR_LEN
, "%s%d/%s%s", band
, ctl_chan
, bw
, sb
);
491 /* ctl sideband string instead of BW for 40MHz */
492 if (CHSPEC_IS40(chspec
)) {
493 sb
= CHSPEC_SB_UPPER(chspec
) ? "u" : "l";
494 snprintf(buf
, CHANSPEC_STR_LEN
, "%s%d%s", band
, ctl_chan
, sb
);
496 snprintf(buf
, CHANSPEC_STR_LEN
, "%s%d/%s", band
, ctl_chan
, bw
);
498 #endif /* CHANSPEC_NEW_40MHZ_FORMAT */
502 uint chan1
= (chspec
& WL_CHANSPEC_CHAN1_MASK
) >> WL_CHANSPEC_CHAN1_SHIFT
;
503 uint chan2
= (chspec
& WL_CHANSPEC_CHAN2_MASK
) >> WL_CHANSPEC_CHAN2_SHIFT
;
505 /* convert to channel number */
506 chan1
= (chan1
< WF_NUM_5G_80M_CHANS
) ? wf_5g_80m_chans
[chan1
] : 0;
507 chan2
= (chan2
< WF_NUM_5G_80M_CHANS
) ? wf_5g_80m_chans
[chan2
] : 0;
509 /* Outputs a max of CHANSPEC_STR_LEN chars including '\0' */
510 snprintf(buf
, CHANSPEC_STR_LEN
, "%d/80+80/%d-%d", ctl_chan
, chan1
, chan2
);
517 read_uint(const char **p
, unsigned int *num
)
522 val
= strtoul(*p
, &endp
, 10);
523 /* if endp is the initial pointer value, then a number was not read */
527 /* advance the buffer pointer to the end of the integer string */
529 /* return the parsed integer */
530 *num
= (unsigned int)val
;
535 /* given a chanspec string, convert to a chanspec.
539 wf_chspec_aton(const char *a
)
542 uint chspec_ch
, chspec_band
, bw
, chspec_bw
, chspec_sb
;
545 char c
, sb_ul
= '\0';
550 chspec_ch
= ch1
= ch2
= 0;
552 /* parse channel num or band */
553 if (!read_uint(&a
, &num
))
556 /* if we are looking at a 'g', then the first number was a band */
557 c
= tolower((int)a
[0]);
559 a
++; /* consume the char */
561 /* band must be "2" or "5" */
563 chspec_band
= WL_CHANSPEC_BAND_2G
;
565 chspec_band
= WL_CHANSPEC_BAND_5G
;
569 /* read the channel number */
570 if (!read_uint(&a
, &ctl_ch
))
573 c
= tolower((int)a
[0]);
576 /* first number is channel, use default for band */
578 chspec_band
= ((ctl_ch
<= CH_MAX_2G_CHANNEL
) ?
579 WL_CHANSPEC_BAND_2G
: WL_CHANSPEC_BAND_5G
);
583 /* default BW of 20MHz */
584 chspec_bw
= WL_CHANSPEC_BW_20
;
588 a
++; /* consume the 'u','l', or '/' */
591 if (c
== 'u' || c
== 'l') {
593 chspec_bw
= WL_CHANSPEC_BW_40
;
597 /* next letter must be '/' */
602 if (!read_uint(&a
, &bw
))
605 /* convert to chspec value */
607 chspec_bw
= WL_CHANSPEC_BW_20
;
608 } else if (bw
== 40) {
609 chspec_bw
= WL_CHANSPEC_BW_40
;
610 } else if (bw
== 80) {
611 chspec_bw
= WL_CHANSPEC_BW_80
;
612 } else if (bw
== 160) {
613 chspec_bw
= WL_CHANSPEC_BW_160
;
618 /* So far we have <band>g<chan>/<bw>
619 * Can now be followed by u/l if bw = 40,
620 * or '+80' if bw = 80, to make '80+80' bw.
623 c
= tolower((int)a
[0]);
625 /* if we have a 2g/40 channel, we should have a l/u spec now */
626 if (chspec_band
== WL_CHANSPEC_BAND_2G
&& bw
== 40) {
627 if (c
== 'u' || c
== 'l') {
628 a
++; /* consume the u/l char */
634 /* check for 80+80 */
637 static const char *plus80
= "80/";
639 /* must be looking at '+80/'
640 * check and consume this string.
642 chspec_bw
= WL_CHANSPEC_BW_8080
;
644 a
++; /* consume the char '+' */
646 /* consume the '80/' string */
647 for (i
= 0; i
< 3; i
++) {
648 if (*a
++ != *plus80
++) {
653 /* read primary 80MHz channel */
654 if (!read_uint(&a
, &ch1
))
657 /* must followed by '-' */
660 a
++; /* consume the char */
662 /* read secondary 80MHz channel */
663 if (!read_uint(&a
, &ch2
))
668 /* skip trailing white space */
669 while (a
[0] == ' ') {
673 /* must be end of string */
677 /* Now have all the chanspec string parts read;
678 * chspec_band, ctl_ch, chspec_bw, sb_ul, ch1, ch2.
679 * chspec_band and chspec_bw are chanspec values.
680 * Need to convert ctl_ch, sb_ul, and ch1,ch2 into
681 * a center channel (or two) and sideband.
684 /* if a sb u/l string was given, just use that,
685 * guaranteed to be bw = 40 by sting parse.
689 chspec_ch
= UPPER_20_SB(ctl_ch
);
690 chspec_sb
= WL_CHANSPEC_CTL_SB_LLL
;
691 } else if (sb_ul
== 'u') {
692 chspec_ch
= LOWER_20_SB(ctl_ch
);
693 chspec_sb
= WL_CHANSPEC_CTL_SB_LLU
;
696 /* if the bw is 20, center and sideband are trivial */
697 else if (chspec_bw
== WL_CHANSPEC_BW_20
) {
701 /* if the bw is 40/80/160, not 80+80, a single method
702 * can be used to to find the center and sideband
704 else if (chspec_bw
!= WL_CHANSPEC_BW_8080
) {
705 /* figure out ctl sideband based on ctl channel and bandwidth */
706 const uint8
*center_ch
= NULL
;
710 if (chspec_bw
== WL_CHANSPEC_BW_40
) {
711 center_ch
= wf_5g_40m_chans
;
712 num_ch
= WF_NUM_5G_40M_CHANS
;
713 } else if (chspec_bw
== WL_CHANSPEC_BW_80
) {
714 center_ch
= wf_5g_80m_chans
;
715 num_ch
= WF_NUM_5G_80M_CHANS
;
716 } else if (chspec_bw
== WL_CHANSPEC_BW_160
) {
717 center_ch
= wf_5g_160m_chans
;
718 num_ch
= WF_NUM_5G_160M_CHANS
;
723 for (i
= 0; i
< num_ch
; i
++) {
724 sb
= channel_to_sb(center_ch
[i
], ctl_ch
, bw
);
726 chspec_ch
= center_ch
[i
];
727 chspec_sb
= sb
<< WL_CHANSPEC_CTL_SB_SHIFT
;
732 /* check for no matching sb/center */
737 /* Otherwise, bw is 80+80. Figure out channel pair and sb */
739 int ch1_id
= 0, ch2_id
= 0;
742 ch1_id
= channel_80mhz_to_id(ch1
);
743 ch2_id
= channel_80mhz_to_id(ch2
);
745 /* validate channels */
746 if (ch1
>= ch2
|| ch1_id
< 0 || ch2_id
< 0)
749 /* combined channel in chspec */
750 chspec_ch
= (((uint16
)ch1_id
<< WL_CHANSPEC_CHAN1_SHIFT
) |
751 ((uint16
)ch2_id
<< WL_CHANSPEC_CHAN2_SHIFT
));
753 /* figure out ctl sideband */
755 /* does the primary channel fit with the 1st 80MHz channel ? */
756 sb
= channel_to_sb(ch1
, ctl_ch
, bw
);
758 /* no, so does the primary channel fit with the 2nd 80MHz channel ? */
759 sb
= channel_to_sb(ch2
, ctl_ch
, bw
);
761 /* no match for ctl_ch to either 80MHz center channel */
764 /* sb index is 0-3 for the low 80MHz channel, and 4-7 for
765 * the high 80MHz channel. Add 4 to to shift to high set.
770 chspec_sb
= sb
<< WL_CHANSPEC_CTL_SB_SHIFT
;
773 chspec
= (chspec_ch
| chspec_band
| chspec_bw
| chspec_sb
);
775 if (wf_chspec_malformed(chspec
))
782 * Verify the chanspec is using a legal set of parameters, i.e. that the
783 * chanspec specified a band, bw, ctl_sb and channel and that the
784 * combination could be legal given any set of circumstances.
785 * RETURNS: TRUE is the chanspec is malformed, false if it looks good.
788 wf_chspec_malformed(chanspec_t chanspec
)
790 uint chspec_bw
= CHSPEC_BW(chanspec
);
791 uint chspec_ch
= CHSPEC_CHANNEL(chanspec
);
793 /* must be 2G or 5G band */
794 if (CHSPEC_IS2G(chanspec
)) {
795 /* must be valid bandwidth */
796 if (chspec_bw
!= WL_CHANSPEC_BW_20
&&
797 chspec_bw
!= WL_CHANSPEC_BW_40
) {
800 } else if (CHSPEC_IS5G(chanspec
)) {
801 if (chspec_bw
== WL_CHANSPEC_BW_8080
) {
804 /* channel number in 80+80 must be in range */
805 ch1_id
= CHSPEC_CHAN1(chanspec
);
806 ch2_id
= CHSPEC_CHAN2(chanspec
);
807 if (ch1_id
>= WF_NUM_5G_80M_CHANS
|| ch2_id
>= WF_NUM_5G_80M_CHANS
)
810 /* ch2 must be above ch1 for the chanspec */
811 if (ch2_id
<= ch1_id
)
813 } else if (chspec_bw
== WL_CHANSPEC_BW_20
|| chspec_bw
== WL_CHANSPEC_BW_40
||
814 chspec_bw
== WL_CHANSPEC_BW_80
|| chspec_bw
== WL_CHANSPEC_BW_160
) {
816 if (chspec_ch
> MAXCHANNEL
) {
820 /* invalid bandwidth */
824 /* must be 2G or 5G band */
828 /* side band needs to be consistent with bandwidth */
829 if (chspec_bw
== WL_CHANSPEC_BW_20
) {
830 if (CHSPEC_CTL_SB(chanspec
) != WL_CHANSPEC_CTL_SB_LLL
)
832 } else if (chspec_bw
== WL_CHANSPEC_BW_40
) {
833 if (CHSPEC_CTL_SB(chanspec
) > WL_CHANSPEC_CTL_SB_LLU
)
835 } else if (chspec_bw
== WL_CHANSPEC_BW_80
) {
836 if (CHSPEC_CTL_SB(chanspec
) > WL_CHANSPEC_CTL_SB_LUU
)
844 * Verify the chanspec specifies a valid channel according to 802.11.
845 * RETURNS: TRUE if the chanspec is a valid 802.11 channel
848 wf_chspec_valid(chanspec_t chanspec
)
850 uint chspec_bw
= CHSPEC_BW(chanspec
);
851 uint chspec_ch
= CHSPEC_CHANNEL(chanspec
);
853 if (wf_chspec_malformed(chanspec
))
856 if (CHSPEC_IS2G(chanspec
)) {
857 /* must be valid bandwidth and channel range */
858 if (chspec_bw
== WL_CHANSPEC_BW_20
) {
859 if (chspec_ch
>= 1 && chspec_ch
<= 14)
861 } else if (chspec_bw
== WL_CHANSPEC_BW_40
) {
862 if (chspec_ch
>= 3 && chspec_ch
<= 11)
865 } else if (CHSPEC_IS5G(chanspec
)) {
866 if (chspec_bw
== WL_CHANSPEC_BW_8080
) {
869 ch1
= wf_5g_80m_chans
[CHSPEC_CHAN1(chanspec
)];
870 ch2
= wf_5g_80m_chans
[CHSPEC_CHAN2(chanspec
)];
872 /* the two channels must be separated by more than 80MHz by VHT req,
873 * and ch2 above ch1 for the chanspec
875 if (ch2
> ch1
+ CH_80MHZ_APART
)
878 const uint8
*center_ch
;
881 if (chspec_bw
== WL_CHANSPEC_BW_20
|| chspec_bw
== WL_CHANSPEC_BW_40
) {
882 center_ch
= wf_5g_40m_chans
;
883 num_ch
= WF_NUM_5G_40M_CHANS
;
884 } else if (chspec_bw
== WL_CHANSPEC_BW_80
) {
885 center_ch
= wf_5g_80m_chans
;
886 num_ch
= WF_NUM_5G_80M_CHANS
;
887 } else if (chspec_bw
== WL_CHANSPEC_BW_160
) {
888 center_ch
= wf_5g_160m_chans
;
889 num_ch
= WF_NUM_5G_160M_CHANS
;
891 /* invalid bandwidth */
895 /* check for a valid center channel */
896 if (chspec_bw
== WL_CHANSPEC_BW_20
) {
897 /* We don't have an array of legal 20MHz 5G channels, but they are
898 * each side of the legal 40MHz channels. Check the chanspec
899 * channel against either side of the 40MHz channels.
901 for (i
= 0; i
< num_ch
; i
++) {
902 if (chspec_ch
== (uint
)LOWER_20_SB(center_ch
[i
]) ||
903 chspec_ch
== (uint
)UPPER_20_SB(center_ch
[i
]))
904 break; /* match found */
908 /* check for legacy JP channels on failure */
909 if (chspec_ch
== 34 || chspec_ch
== 38 ||
910 chspec_ch
== 42 || chspec_ch
== 46)
914 /* check the chanspec channel to each legal channel */
915 for (i
= 0; i
< num_ch
; i
++) {
916 if (chspec_ch
== center_ch
[i
])
917 break; /* match found */
932 * This function returns the channel number that control traffic is being sent on, for 20MHz
933 * channels this is just the channel number, for 40MHZ, 80MHz, 160MHz channels it is the 20MHZ
934 * sideband depending on the chanspec selected
937 wf_chspec_ctlchan(chanspec_t chspec
)
943 ASSERT(!wf_chspec_malformed(chspec
));
945 /* Is there a sideband ? */
946 if (CHSPEC_IS20(chspec
)) {
947 return CHSPEC_CHANNEL(chspec
);
949 sb
= CHSPEC_CTL_SB(chspec
) >> WL_CHANSPEC_CTL_SB_SHIFT
;
951 if (CHSPEC_IS8080(chspec
)) {
955 center_chan
= CHSPEC_CHAN1(chspec
);
958 center_chan
= CHSPEC_CHAN2(chspec
);
962 /* convert from channel index to channel number */
963 center_chan
= wf_5g_80m_chans
[center_chan
];
966 bw_mhz
= bw_chspec_to_mhz(chspec
);
967 center_chan
= CHSPEC_CHANNEL(chspec
) >> WL_CHANSPEC_CHAN_SHIFT
;
970 return (channel_to_ctl_chan(center_chan
, bw_mhz
, sb
));
975 * This function returns the chanspec of the control channel of a given chanspec
978 wf_chspec_ctlchspec(chanspec_t chspec
)
980 chanspec_t ctl_chspec
= chspec
;
983 ASSERT(!wf_chspec_malformed(chspec
));
985 /* Is there a sideband ? */
986 if (!CHSPEC_IS20(chspec
)) {
987 ctl_chan
= wf_chspec_ctlchan(chspec
);
988 ctl_chspec
= ctl_chan
| WL_CHANSPEC_BW_20
;
989 ctl_chspec
|= CHSPEC_BAND(chspec
);
994 /* return chanspec given control channel and bandwidth
998 wf_channel2chspec(uint ctl_ch
, uint bw
)
1001 const uint8
*center_ch
= NULL
;
1006 chspec
= ((ctl_ch
<= CH_MAX_2G_CHANNEL
) ? WL_CHANSPEC_BAND_2G
: WL_CHANSPEC_BAND_5G
);
1010 if (bw
== WL_CHANSPEC_BW_40
) {
1011 center_ch
= wf_5g_40m_chans
;
1012 num_ch
= WF_NUM_5G_40M_CHANS
;
1014 } else if (bw
== WL_CHANSPEC_BW_80
) {
1015 center_ch
= wf_5g_80m_chans
;
1016 num_ch
= WF_NUM_5G_80M_CHANS
;
1018 } else if (bw
== WL_CHANSPEC_BW_160
) {
1019 center_ch
= wf_5g_160m_chans
;
1020 num_ch
= WF_NUM_5G_160M_CHANS
;
1026 for (i
= 0; i
< num_ch
; i
++) {
1027 sb
= channel_to_sb(center_ch
[i
], ctl_ch
, bw
);
1029 chspec
|= center_ch
[i
];
1030 chspec
|= (sb
<< WL_CHANSPEC_CTL_SB_SHIFT
);
1035 /* check for no matching sb/center */
1043 #endif /* D11AC_IOTYPES */
1046 * This function returns the chanspec for the primary 40MHz of an 80MHz channel.
1047 * The control sideband specifies the same 20MHz channel that the 80MHz channel is using
1048 * as the primary 20MHz channel.
1050 extern chanspec_t
wf_chspec_primary40_chspec(chanspec_t chspec
)
1052 chanspec_t chspec40
= chspec
;
1056 ASSERT(!wf_chspec_malformed(chspec
));
1058 if (CHSPEC_IS80(chspec
)) {
1059 center_chan
= CHSPEC_CHANNEL(chspec
);
1060 sb
= CHSPEC_CTL_SB(chspec
);
1062 if (sb
== WL_CHANSPEC_CTL_SB_UL
) {
1063 /* Primary 40MHz is on upper side */
1064 sb
= WL_CHANSPEC_CTL_SB_L
;
1065 center_chan
+= CH_20MHZ_APART
;
1066 } else if (sb
== WL_CHANSPEC_CTL_SB_UU
) {
1067 /* Primary 40MHz is on upper side */
1068 sb
= WL_CHANSPEC_CTL_SB_U
;
1069 center_chan
+= CH_20MHZ_APART
;
1071 /* Primary 40MHz is on lower side */
1072 /* sideband bits are the same for LL/LU and L/U */
1073 center_chan
-= CH_20MHZ_APART
;
1076 /* Create primary 40MHz chanspec */
1077 chspec40
= (WL_CHANSPEC_BAND_5G
| WL_CHANSPEC_BW_40
|
1085 * Return the channel number for a given frequency and base frequency.
1086 * The returned channel number is relative to the given base frequency.
1087 * If the given base frequency is zero, a base frequency of 5 GHz is assumed for
1088 * frequencies from 5 - 6 GHz, and 2.407 GHz is assumed for 2.4 - 2.5 GHz.
1090 * Frequency is specified in MHz.
1091 * The base frequency is specified as (start_factor * 500 kHz).
1092 * Constants WF_CHAN_FACTOR_2_4_G, WF_CHAN_FACTOR_5_G are defined for
1093 * 2.4 GHz and 5 GHz bands.
1095 * The returned channel will be in the range [1, 14] in the 2.4 GHz band
1096 * and [0, 200] otherwise.
1097 * -1 is returned if the start_factor is WF_CHAN_FACTOR_2_4_G and the
1098 * frequency is not a 2.4 GHz channel, or if the frequency is not and even
1099 * multiple of 5 MHz from the base frequency to the base plus 1 GHz.
1101 * Reference 802.11 REVma, section 17.3.8.3, and 802.11B section 18.4.6.2
1104 wf_mhz2channel(uint freq
, uint start_factor
)
1110 /* take the default channel start frequency */
1111 if (start_factor
== 0) {
1112 if (freq
>= 2400 && freq
<= 2500)
1113 start_factor
= WF_CHAN_FACTOR_2_4_G
;
1114 else if (freq
>= 5000 && freq
<= 6000)
1115 start_factor
= WF_CHAN_FACTOR_5_G
;
1118 if (freq
== 2484 && start_factor
== WF_CHAN_FACTOR_2_4_G
)
1121 base
= start_factor
/ 2;
1123 /* check that the frequency is in 1GHz range of the base */
1124 if ((freq
< base
) || (freq
> base
+ 1000))
1127 offset
= freq
- base
;
1130 /* check that frequency is a 5MHz multiple from the base */
1131 if (offset
!= (ch
* 5))
1134 /* restricted channel range check for 2.4G */
1135 if (start_factor
== WF_CHAN_FACTOR_2_4_G
&& (ch
< 1 || ch
> 13))
1142 * Return the center frequency in MHz of the given channel and base frequency.
1143 * The channel number is interpreted relative to the given base frequency.
1145 * The valid channel range is [1, 14] in the 2.4 GHz band and [0, 200] otherwise.
1146 * The base frequency is specified as (start_factor * 500 kHz).
1147 * Constants WF_CHAN_FACTOR_2_4_G, WF_CHAN_FACTOR_4_G, and WF_CHAN_FACTOR_5_G
1148 * are defined for 2.4 GHz, 4 GHz, and 5 GHz bands.
1149 * The channel range of [1, 14] is only checked for a start_factor of
1150 * WF_CHAN_FACTOR_2_4_G (4814 = 2407 * 2).
1151 * Odd start_factors produce channels on .5 MHz boundaries, in which case
1152 * the answer is rounded down to an integral MHz.
1153 * -1 is returned for an out of range channel.
1155 * Reference 802.11 REVma, section 17.3.8.3, and 802.11B section 18.4.6.2
1158 wf_channel2mhz(uint ch
, uint start_factor
)
1162 if ((start_factor
== WF_CHAN_FACTOR_2_4_G
&& (ch
< 1 || ch
> 14)) ||
1165 else if ((start_factor
== WF_CHAN_FACTOR_2_4_G
) && (ch
== 14))
1168 freq
= ch
* 5 + start_factor
/ 2;