1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Misc utility routines used by kernel or app-level.
4 * Contents are wifi-specific, used by any kernel or app-level
5 * software that might want wifi things as it grows.
6 *
7 * Copyright (C) 1999-2019, Broadcom.
8 *
9 * Unless you and Broadcom execute a separate written software license
10 * agreement governing use of this software, this software is licensed to you
11 * under the terms of the GNU General Public License version 2 (the "GPL"),
12 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
13 * following added to such license:
14 *
15 * As a special exception, the copyright holders of this software give you
16 * permission to link this software with independent modules, and to copy and
17 * distribute the resulting executable under terms of your choice, provided that
18 * you also meet, for each linked independent module, the terms and conditions of
19 * the license of that module. An independent module is a module which is not
20 * derived from this software. The special exception does not apply to any
21 * modifications of the software.
22 *
23 * Notwithstanding the above, under no circumstances may you combine this
24 * software in any way with any other Broadcom software provided under a license
25 * other than the GPL, without Broadcom's express prior written consent.
26 *
27 *
28 * <<Broadcom-WL-IPTag/Open:>>
29 *
30 * $Id: bcmwifi_channels.c 806092 2019-02-21 08:19:13Z $
31 */
32
33 #include <bcm_cfg.h>
34 #include <typedefs.h>
35 #include <bcmutils.h>
36
37 #ifdef BCMDRIVER
38 #include <osl.h>
39 #define strtoul(nptr, endptr, base) bcm_strtoul((nptr), (endptr), (base))
40 #define tolower(c) (bcm_isupper((c)) ? ((c) + 'a' - 'A') : (c))
41 #else
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <ctype.h>
45 #ifndef ASSERT
46 #define ASSERT(exp)
47 #endif // endif
48 #endif /* BCMDRIVER */
49
50 #include <bcmwifi_channels.h>
51
52 #if defined(WIN32) && (defined(BCMDLL) || defined(WLMDLL))
53 #include <bcmstdlib.h> /* For wl/exe/GNUmakefile.brcm_wlu and GNUmakefile.wlm_dll */
54 #endif // endif
55
56 #include <802.11.h>
57
58 /* Definitions for D11AC capable (80MHz+) Chanspec type */
59
60 /* Chanspec ASCII representation:
61 * [<band> 'g'] <channel> ['/'<bandwidth> [<primary-sideband>]['/'<1st80channel>'-'<2nd80channel>]]
62 *
63 * <band>:
64 * (optional) 2, 3, 4, 5 for 2.4GHz, 3GHz, 4GHz, and 5GHz respectively.
65 * Default value is 2g if channel <= 14, otherwise 5g.
66 * <channel>:
67 * channel number of the 5MHz, 10MHz, 20MHz channel,
68 * or primary channel of 40MHz, 80MHz, 160MHz, or 80+80MHz channel.
69 * <bandwidth>:
70 * (optional) 5, 10, 20, 40, 80, 160, or 80+80. Default value is 20.
71 * <primary-sideband>:
72 * (only for 2.4GHz band 40MHz) U for upper sideband primary, L for lower.
73 *
74 * For 2.4GHz band 40MHz channels, the same primary channel may be the
75 * upper sideband for one 40MHz channel, and the lower sideband for an
76 * overlapping 40MHz channel. The U/L disambiguates which 40MHz channel
77 * is being specified.
78 *
79 * For 40MHz in the 5GHz band and all channel bandwidths greater than
80 * 40MHz, the U/L specificaion is not allowed since the channels are
81 * non-overlapping and the primary sub-band is derived from its
82 * position in the wide bandwidth channel.
83 *
84 * <1st80Channel>:
85 * <2nd80Channel>:
86 * Required for 80+80, otherwise not allowed.
87 * Specifies the center channel of the primary and secondary 80MHz band.
88 *
89 * In its simplest form, it is a 20MHz channel number, with the implied band
90 * of 2.4GHz if channel number <= 14, and 5GHz otherwise.
91 *
92 * To allow for backward compatibility with scripts, the old form for
93 * 40MHz channels is also allowed: <channel><primary-sideband>
94 *
95 * <channel>:
96 * primary channel of 40MHz, channel <= 14 is 2GHz, otherwise 5GHz
97 * <primary-sideband>:
98 * "U" for upper, "L" for lower (or lower case "u" "l")
99 *
100 * 5 GHz Examples:
101 * Chanspec BW Center Ch Channel Range Primary Ch
102 * 5g8 20MHz 8 - -
103 * 52 20MHz 52 - -
104 * 52/40 40MHz 54 52-56 52
105 * 56/40 40MHz 54 52-56 56
106 * 52/80 80MHz 58 52-64 52
107 * 56/80 80MHz 58 52-64 56
108 * 60/80 80MHz 58 52-64 60
109 * 64/80 80MHz 58 52-64 64
110 * 52/160 160MHz 50 36-64 52
111 * 36/160 160MGz 50 36-64 36
112 * 36/80+80/42-106 80+80MHz 42,106 36-48,100-112 36
113 *
114 * 2 GHz Examples:
115 * Chanspec BW Center Ch Channel Range Primary Ch
116 * 2g8 20MHz 8 - -
117 * 8 20MHz 8 - -
118 * 6 20MHz 6 - -
119 * 6/40l 40MHz 8 6-10 6
120 * 6l 40MHz 8 6-10 6
121 * 6/40u 40MHz 4 2-6 6
122 * 6u 40MHz 4 2-6 6
123 */
124
125 /* bandwidth ASCII string */
126 static const char *wf_chspec_bw_str[] =
127 {
128 "5",
129 "10",
130 "20",
131 "40",
132 "80",
133 "160",
134 "80+80",
135 "na"
136 };
137
138 static const uint8 wf_chspec_bw_mhz[] =
139 {5, 10, 20, 40, 80, 160, 160};
140
141 #define WF_NUM_BW \
142 (sizeof(wf_chspec_bw_mhz)/sizeof(uint8))
143
144 /* 40MHz channels in 5GHz band */
145 static const uint8 wf_5g_40m_chans[] =
146 {38, 46, 54, 62, 102, 110, 118, 126, 134, 142, 151, 159, 167, 175};
147 #define WF_NUM_5G_40M_CHANS \
148 (sizeof(wf_5g_40m_chans)/sizeof(uint8))
149
150 /* 80MHz channels in 5GHz band */
151 static const uint8 wf_5g_80m_chans[] =
152 {42, 58, 106, 122, 138, 155, 171};
153 #define WF_NUM_5G_80M_CHANS \
154 (sizeof(wf_5g_80m_chans)/sizeof(uint8))
155
156 /* 160MHz channels in 5GHz band */
157 static const uint8 wf_5g_160m_chans[] =
158 {50, 114};
159 #define WF_NUM_5G_160M_CHANS \
160 (sizeof(wf_5g_160m_chans)/sizeof(uint8))
161
162 /* opclass and channel information for US. Table E-1 */
163 static const uint16 opclass_data[] = {
164 (WL_CHANSPEC_BAND_5G |((WL_CHANSPEC_BW_20)&WL_CHANSPEC_BW_MASK)),
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_5)&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_10)&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_20)&WL_CHANSPEC_BW_MASK)),
174 (WL_CHANSPEC_BAND_5G |((WL_CHANSPEC_BW_20)&WL_CHANSPEC_BW_MASK)),
175 (WL_CHANSPEC_BAND_2G |((WL_CHANSPEC_BW_20)&WL_CHANSPEC_BW_MASK)),
176 (WL_CHANSPEC_BAND_3G |((WL_CHANSPEC_BW_20)&WL_CHANSPEC_BW_MASK)),
177 (WL_CHANSPEC_BAND_3G |((WL_CHANSPEC_BW_10)&WL_CHANSPEC_BW_MASK)),
178 (WL_CHANSPEC_BAND_3G |((WL_CHANSPEC_BW_5)&WL_CHANSPEC_BW_MASK)),
179 (WL_CHANSPEC_BAND_5G |((WL_CHANSPEC_BW_5)&WL_CHANSPEC_BW_MASK)),
180 (WL_CHANSPEC_BAND_5G |((WL_CHANSPEC_BW_10)&WL_CHANSPEC_BW_MASK)),
181 (WL_CHANSPEC_BAND_5G |((WL_CHANSPEC_BW_20)&WL_CHANSPEC_BW_MASK)),
182 0,
183 0,
184 0,
185 (WL_CHANSPEC_BAND_5G |((WL_CHANSPEC_BW_40)&WL_CHANSPEC_BW_MASK)|WL_CHANSPEC_CTL_SB_LOWER),
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_UPPER),
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_2G |((WL_CHANSPEC_BW_40)&WL_CHANSPEC_BW_MASK)|WL_CHANSPEC_CTL_SB_LOWER),
196 (WL_CHANSPEC_BAND_2G |((WL_CHANSPEC_BW_40)&WL_CHANSPEC_BW_MASK)|WL_CHANSPEC_CTL_SB_UPPER),
197 };
198
199 /**
200 * Return the chanspec bandwidth in MHz
201 * Bandwidth of 160 MHz will be returned for 80+80MHz chanspecs.
202 *
203 * @param chspec chanspec_t
204 *
205 * @return bandwidth of chspec in MHz units
206 */
207 uint
wf_bw_chspec_to_mhz(chanspec_t chspec)208 wf_bw_chspec_to_mhz(chanspec_t chspec)
209 {
210 uint bw;
211
212 bw = (chspec & WL_CHANSPEC_BW_MASK) >> WL_CHANSPEC_BW_SHIFT;
213 return (bw >= WF_NUM_BW ? 0 : wf_chspec_bw_mhz[bw]);
214 }
215
216 /* bw in MHz, return the channel count from the center channel to the
217 * the channel at the edge of the band
218 */
219 static uint8
center_chan_to_edge(uint bw)220 center_chan_to_edge(uint bw)
221 {
222 /* edge channels separated by BW - 10MHz on each side
223 * delta from cf to edge is half of that,
224 * MHz to channel num conversion is 5MHz/channel
225 */
226 return (uint8)(((bw - 20) / 2) / 5);
227 }
228
229 /* return channel number of the low edge of the band
230 * given the center channel and BW
231 */
232 static uint8
channel_low_edge(uint center_ch,uint bw)233 channel_low_edge(uint center_ch, uint bw)
234 {
235 return (uint8)(center_ch - center_chan_to_edge(bw));
236 }
237
238 /* return side band number given center channel and primary20 channel
239 * return -1 on error
240 */
241 static int
channel_to_sb(uint center_ch,uint primary_ch,uint bw)242 channel_to_sb(uint center_ch, uint primary_ch, uint bw)
243 {
244 uint lowest = channel_low_edge(center_ch, bw);
245 uint sb;
246
247 if ((primary_ch - lowest) % 4) {
248 /* bad primary channel, not mult 4 */
249 return -1;
250 }
251
252 sb = ((primary_ch - lowest) / 4);
253
254 /* sb must be a index to a 20MHz channel in range */
255 if (sb >= (bw / 20)) {
256 /* primary_ch must have been too high for the center_ch */
257 return -1;
258 }
259
260 return (int)sb;
261 }
262
263 /* return primary20 channel given center channel and side band */
264 static uint8
channel_to_primary20_chan(uint center_ch,uint bw,uint sb)265 channel_to_primary20_chan(uint center_ch, uint bw, uint sb)
266 {
267 return (uint8)(channel_low_edge(center_ch, bw) + sb * 4);
268 }
269
270 /* return index of 80MHz channel from channel number
271 * return -1 on error
272 */
273 static int
channel_80mhz_to_id(uint ch)274 channel_80mhz_to_id(uint ch)
275 {
276 uint i;
277 for (i = 0; i < WF_NUM_5G_80M_CHANS; i ++) {
278 if (ch == wf_5g_80m_chans[i])
279 return (int)i;
280 }
281
282 return -1;
283 }
284
285 /* wrapper function for wf_chspec_ntoa. In case of an error it puts
286 * the original chanspec in the output buffer, prepended with "invalid".
287 * Can be directly used in print routines as it takes care of null
288 */
289 char *
wf_chspec_ntoa_ex(chanspec_t chspec,char * buf)290 wf_chspec_ntoa_ex(chanspec_t chspec, char *buf)
291 {
292 if (wf_chspec_ntoa(chspec, buf) == NULL)
293 snprintf(buf, CHANSPEC_STR_LEN, "invalid 0x%04x", chspec);
294 return buf;
295 }
296
297 /* given a chanspec and a string buffer, format the chanspec as a
298 * string, and return the original pointer a.
299 * Min buffer length must be CHANSPEC_STR_LEN.
300 * On error return NULL
301 */
302 char *
wf_chspec_ntoa(chanspec_t chspec,char * buf)303 wf_chspec_ntoa(chanspec_t chspec, char *buf)
304 {
305 const char *band;
306 uint pri_chan;
307
308 if (wf_chspec_malformed(chspec))
309 return NULL;
310
311 band = "";
312
313 /* check for non-default band spec */
314 if ((CHSPEC_IS2G(chspec) && CHSPEC_CHANNEL(chspec) > CH_MAX_2G_CHANNEL) ||
315 (CHSPEC_IS5G(chspec) && CHSPEC_CHANNEL(chspec) <= CH_MAX_2G_CHANNEL))
316 band = (CHSPEC_IS2G(chspec)) ? "2g" : "5g";
317
318 /* primary20 channel */
319 pri_chan = wf_chspec_primary20_chan(chspec);
320
321 /* bandwidth and primary20 sideband */
322 if (CHSPEC_IS20(chspec)) {
323 snprintf(buf, CHANSPEC_STR_LEN, "%s%d", band, pri_chan);
324 } else if (!CHSPEC_IS8080(chspec)) {
325 const char *bw;
326 const char *sb = "";
327
328 bw = wf_chspec_to_bw_str(chspec);
329
330 #ifdef CHANSPEC_NEW_40MHZ_FORMAT
331 /* primary20 sideband string if needed for 2g 40MHz */
332 if (CHSPEC_IS40(chspec) && CHSPEC_IS2G(chspec)) {
333 sb = CHSPEC_SB_UPPER(chspec) ? "u" : "l";
334 }
335
336 snprintf(buf, CHANSPEC_STR_LEN, "%s%d/%s%s", band, pri_chan, bw, sb);
337 #else
338 /* primary20 sideband string instead of BW for 40MHz */
339 if (CHSPEC_IS40(chspec)) {
340 sb = CHSPEC_SB_UPPER(chspec) ? "u" : "l";
341 snprintf(buf, CHANSPEC_STR_LEN, "%s%d%s", band, pri_chan, sb);
342 } else {
343 snprintf(buf, CHANSPEC_STR_LEN, "%s%d/%s", band, pri_chan, bw);
344 }
345 #endif /* CHANSPEC_NEW_40MHZ_FORMAT */
346
347 } else {
348 /* 80+80 */
349 uint chan1 = (chspec & WL_CHANSPEC_CHAN1_MASK) >> WL_CHANSPEC_CHAN1_SHIFT;
350 uint chan2 = (chspec & WL_CHANSPEC_CHAN2_MASK) >> WL_CHANSPEC_CHAN2_SHIFT;
351
352 /* convert to channel number */
353 chan1 = (chan1 < WF_NUM_5G_80M_CHANS) ? wf_5g_80m_chans[chan1] : 0;
354 chan2 = (chan2 < WF_NUM_5G_80M_CHANS) ? wf_5g_80m_chans[chan2] : 0;
355
356 /* Outputs a max of CHANSPEC_STR_LEN chars including '\0' */
357 snprintf(buf, CHANSPEC_STR_LEN, "%d/80+80/%d-%d", pri_chan, chan1, chan2);
358 }
359
360 return (buf);
361 }
362
363 static int
read_uint(const char ** p,unsigned int * num)364 read_uint(const char **p, unsigned int *num)
365 {
366 unsigned long val;
367 char *endp = NULL;
368
369 val = strtoul(*p, &endp, 10);
370 /* if endp is the initial pointer value, then a number was not read */
371 if (endp == *p)
372 return 0;
373
374 /* advance the buffer pointer to the end of the integer string */
375 *p = endp;
376 /* return the parsed integer */
377 *num = (unsigned int)val;
378
379 return 1;
380 }
381
382 /* given a chanspec string, convert to a chanspec.
383 * On error return 0
384 */
385 chanspec_t
wf_chspec_aton(const char * a)386 wf_chspec_aton(const char *a)
387 {
388 chanspec_t chspec;
389 uint chspec_ch, chspec_band, bw, chspec_bw, chspec_sb;
390 uint num, pri_ch;
391 uint ch1, ch2;
392 char c, sb_ul = '\0';
393 int i;
394
395 bw = 20;
396 chspec_sb = 0;
397 chspec_ch = ch1 = ch2 = 0;
398
399 /* parse channel num or band */
400 if (!read_uint(&a, &num))
401 return 0;
402 /* if we are looking at a 'g', then the first number was a band */
403 c = tolower(a[0]);
404 if (c == 'g') {
405 a++; /* consume the char */
406
407 /* band must be "2" or "5" */
408 if (num == 2)
409 chspec_band = WL_CHANSPEC_BAND_2G;
410 else if (num == 5)
411 chspec_band = WL_CHANSPEC_BAND_5G;
412 else
413 return 0;
414
415 /* read the channel number */
416 if (!read_uint(&a, &pri_ch))
417 return 0;
418
419 c = tolower(a[0]);
420 }
421 else {
422 /* first number is channel, use default for band */
423 pri_ch = num;
424 chspec_band = ((pri_ch <= CH_MAX_2G_CHANNEL) ?
425 WL_CHANSPEC_BAND_2G : WL_CHANSPEC_BAND_5G);
426 }
427
428 if (c == '\0') {
429 /* default BW of 20MHz */
430 chspec_bw = WL_CHANSPEC_BW_20;
431 goto done_read;
432 }
433
434 a ++; /* consume the 'u','l', or '/' */
435
436 /* check 'u'/'l' */
437 if (c == 'u' || c == 'l') {
438 sb_ul = c;
439 chspec_bw = WL_CHANSPEC_BW_40;
440 goto done_read;
441 }
442
443 /* next letter must be '/' */
444 if (c != '/')
445 return 0;
446
447 /* read bandwidth */
448 if (!read_uint(&a, &bw))
449 return 0;
450
451 /* convert to chspec value */
452 if (bw == 5) {
453 chspec_bw = WL_CHANSPEC_BW_5;
454 } else if (bw == 10) {
455 chspec_bw = WL_CHANSPEC_BW_10;
456 } else if (bw == 20) {
457 chspec_bw = WL_CHANSPEC_BW_20;
458 } else if (bw == 40) {
459 chspec_bw = WL_CHANSPEC_BW_40;
460 } else if (bw == 80) {
461 chspec_bw = WL_CHANSPEC_BW_80;
462 } else if (bw == 160) {
463 chspec_bw = WL_CHANSPEC_BW_160;
464 } else {
465 return 0;
466 }
467
468 /* So far we have <band>g<chan>/<bw>
469 * Can now be followed by u/l if bw = 40,
470 * or '+80' if bw = 80, to make '80+80' bw.
471 */
472
473 c = (char)tolower((int)a[0]);
474
475 /* if we have a 2g/40 channel, we should have a l/u spec now */
476 if (chspec_band == WL_CHANSPEC_BAND_2G && bw == 40) {
477 if (c == 'u' || c == 'l') {
478 a ++; /* consume the u/l char */
479 sb_ul = c;
480 goto done_read;
481 }
482 }
483
484 /* check for 80+80 */
485 if (c == '+') {
486 /* 80+80 */
487 const char plus80[] = "80/";
488
489 /* must be looking at '+80/'
490 * check and consume this string.
491 */
492 chspec_bw = WL_CHANSPEC_BW_8080;
493
494 a ++; /* consume the char '+' */
495
496 /* consume the '80/' string */
497 for (i = 0; i < 3; i++) {
498 if (*a++ != plus80[i]) {
499 return 0;
500 }
501 }
502
503 /* read primary 80MHz channel */
504 if (!read_uint(&a, &ch1))
505 return 0;
506
507 /* must followed by '-' */
508 if (a[0] != '-')
509 return 0;
510 a ++; /* consume the char */
511
512 /* read secondary 80MHz channel */
513 if (!read_uint(&a, &ch2))
514 return 0;
515 }
516
517 done_read:
518 /* skip trailing white space */
519 while (a[0] == ' ') {
520 a ++;
521 }
522
523 /* must be end of string */
524 if (a[0] != '\0')
525 return 0;
526
527 /* Now have all the chanspec string parts read;
528 * chspec_band, pri_ch, chspec_bw, sb_ul, ch1, ch2.
529 * chspec_band and chspec_bw are chanspec values.
530 * Need to convert pri_ch, sb_ul, and ch1,ch2 into
531 * a center channel (or two) and sideband.
532 */
533
534 /* if a sb u/l string was given, just use that,
535 * guaranteed to be bw = 40 by sting parse.
536 */
537 if (sb_ul != '\0') {
538 if (sb_ul == 'l') {
539 chspec_ch = UPPER_20_SB(pri_ch);
540 chspec_sb = WL_CHANSPEC_CTL_SB_LLL;
541 } else if (sb_ul == 'u') {
542 chspec_ch = LOWER_20_SB(pri_ch);
543 chspec_sb = WL_CHANSPEC_CTL_SB_LLU;
544 }
545 }
546 /* if the bw is 20, center and sideband are trivial */
547 else if (chspec_bw == WL_CHANSPEC_BW_20) {
548 chspec_ch = pri_ch;
549 chspec_sb = WL_CHANSPEC_CTL_SB_NONE;
550 }
551 /* if the bw is 40/80/160, not 80+80, a single method
552 * can be used to to find the center and sideband
553 */
554 else if (chspec_bw != WL_CHANSPEC_BW_8080) {
555 /* figure out primary20 sideband based on primary20 channel and bandwidth */
556 const uint8 *center_ch = NULL;
557 int num_ch = 0;
558 int sb = -1;
559
560 if (chspec_bw == WL_CHANSPEC_BW_40) {
561 center_ch = wf_5g_40m_chans;
562 num_ch = WF_NUM_5G_40M_CHANS;
563 } else if (chspec_bw == WL_CHANSPEC_BW_80) {
564 center_ch = wf_5g_80m_chans;
565 num_ch = WF_NUM_5G_80M_CHANS;
566 } else if (chspec_bw == WL_CHANSPEC_BW_160) {
567 center_ch = wf_5g_160m_chans;
568 num_ch = WF_NUM_5G_160M_CHANS;
569 } else {
570 return 0;
571 }
572
573 for (i = 0; i < num_ch; i ++) {
574 sb = channel_to_sb(center_ch[i], pri_ch, bw);
575 if (sb >= 0) {
576 chspec_ch = center_ch[i];
577 chspec_sb = (uint)(sb << WL_CHANSPEC_CTL_SB_SHIFT);
578 break;
579 }
580 }
581
582 /* check for no matching sb/center */
583 if (sb < 0) {
584 return 0;
585 }
586 }
587 /* Otherwise, bw is 80+80. Figure out channel pair and sb */
588 else {
589 int ch1_id = 0, ch2_id = 0;
590 int sb;
591
592 /* look up the channel ID for the specified channel numbers */
593 ch1_id = channel_80mhz_to_id(ch1);
594 ch2_id = channel_80mhz_to_id(ch2);
595
596 /* validate channels */
597 if (ch1_id < 0 || ch2_id < 0)
598 return 0;
599
600 /* combine 2 channel IDs in channel field of chspec */
601 chspec_ch = (((uint)ch1_id << WL_CHANSPEC_CHAN1_SHIFT) |
602 ((uint)ch2_id << WL_CHANSPEC_CHAN2_SHIFT));
603
604 /* figure out primary 20 MHz sideband */
605
606 /* is the primary channel contained in the 1st 80MHz channel? */
607 sb = channel_to_sb(ch1, pri_ch, bw);
608 if (sb < 0) {
609 /* no match for primary channel 'pri_ch' in segment0 80MHz channel */
610 return 0;
611 }
612
613 chspec_sb = (uint)(sb << WL_CHANSPEC_CTL_SB_SHIFT);
614 }
615
616 chspec = (chanspec_t)(chspec_ch | chspec_band | chspec_bw | chspec_sb);
617
618 if (wf_chspec_malformed(chspec))
619 return 0;
620
621 return chspec;
622 }
623
624 /*
625 * Verify the chanspec is using a legal set of parameters, i.e. that the
626 * chanspec specified a band, bw, pri_sb and channel and that the
627 * combination could be legal given any set of circumstances.
628 * RETURNS: TRUE is the chanspec is malformed, false if it looks good.
629 */
630 bool
wf_chspec_malformed(chanspec_t chanspec)631 wf_chspec_malformed(chanspec_t chanspec)
632 {
633 uint chspec_bw = CHSPEC_BW(chanspec);
634 uint chspec_ch = CHSPEC_CHANNEL(chanspec);
635
636 /* must be 2G or 5G band */
637 if (CHSPEC_IS2G(chanspec)) {
638 /* must be valid bandwidth */
639 if (!BW_LE40(chspec_bw)) {
640 return TRUE;
641 }
642 } else if (CHSPEC_IS5G(chanspec)) {
643 if (chspec_bw == WL_CHANSPEC_BW_8080) {
644 uint ch1_id, ch2_id;
645
646 /* channel IDs in 80+80 must be in range */
647 ch1_id = CHSPEC_CHAN1(chanspec);
648 ch2_id = CHSPEC_CHAN2(chanspec);
649 if (ch1_id >= WF_NUM_5G_80M_CHANS || ch2_id >= WF_NUM_5G_80M_CHANS)
650 return TRUE;
651
652 } else if (chspec_bw == WL_CHANSPEC_BW_20 || chspec_bw == WL_CHANSPEC_BW_40 ||
653 chspec_bw == WL_CHANSPEC_BW_80 || chspec_bw == WL_CHANSPEC_BW_160) {
654
655 if (chspec_ch > MAXCHANNEL) {
656 return TRUE;
657 }
658 } else {
659 /* invalid bandwidth */
660 return TRUE;
661 }
662 } else {
663 /* must be 2G or 5G band */
664 return TRUE;
665 }
666
667 /* side band needs to be consistent with bandwidth */
668 if (chspec_bw == WL_CHANSPEC_BW_20) {
669 if (CHSPEC_CTL_SB(chanspec) != WL_CHANSPEC_CTL_SB_LLL)
670 return TRUE;
671 } else if (chspec_bw == WL_CHANSPEC_BW_40) {
672 if (CHSPEC_CTL_SB(chanspec) > WL_CHANSPEC_CTL_SB_LLU)
673 return TRUE;
674 } else if (chspec_bw == WL_CHANSPEC_BW_80 ||
675 chspec_bw == WL_CHANSPEC_BW_8080) {
676 /* both 80MHz and 80+80MHz use 80MHz side bands.
677 * 80+80 SB info is relative to the primary 80MHz sub-band.
678 */
679 if (CHSPEC_CTL_SB(chanspec) > WL_CHANSPEC_CTL_SB_LUU)
680 return TRUE;
681 }
682 else if (chspec_bw == WL_CHANSPEC_BW_160) {
683 ASSERT(CHSPEC_CTL_SB(chanspec) <= WL_CHANSPEC_CTL_SB_UUU);
684 }
685 return FALSE;
686 }
687
688 /*
689 * Verify the chanspec specifies a valid channel according to 802.11.
690 * RETURNS: TRUE if the chanspec is a valid 802.11 channel
691 */
692 bool
wf_chspec_valid(chanspec_t chanspec)693 wf_chspec_valid(chanspec_t chanspec)
694 {
695 uint chspec_bw = CHSPEC_BW(chanspec);
696 uint chspec_ch = CHSPEC_CHANNEL(chanspec);
697
698 if (wf_chspec_malformed(chanspec))
699 return FALSE;
700
701 if (CHSPEC_IS2G(chanspec)) {
702 /* must be valid bandwidth and channel range */
703 if (chspec_bw == WL_CHANSPEC_BW_20) {
704 if (chspec_ch >= 1 && chspec_ch <= 14)
705 return TRUE;
706 } else if (chspec_bw == WL_CHANSPEC_BW_40) {
707 if (chspec_ch >= 3 && chspec_ch <= 11)
708 return TRUE;
709 }
710 } else if (CHSPEC_IS5G(chanspec)) {
711 if (chspec_bw == WL_CHANSPEC_BW_8080) {
712 uint16 ch1, ch2;
713
714 ch1 = wf_5g_80m_chans[CHSPEC_CHAN1(chanspec)];
715 ch2 = wf_5g_80m_chans[CHSPEC_CHAN2(chanspec)];
716
717 /* the two channels must be separated by more than 80MHz by VHT req */
718 if ((ch2 > ch1 + CH_80MHZ_APART) ||
719 (ch1 > ch2 + CH_80MHZ_APART))
720 return TRUE;
721 } else {
722 const uint8 *center_ch;
723 uint num_ch, i;
724
725 if (chspec_bw == WL_CHANSPEC_BW_20 || chspec_bw == WL_CHANSPEC_BW_40) {
726 center_ch = wf_5g_40m_chans;
727 num_ch = WF_NUM_5G_40M_CHANS;
728 } else if (chspec_bw == WL_CHANSPEC_BW_80) {
729 center_ch = wf_5g_80m_chans;
730 num_ch = WF_NUM_5G_80M_CHANS;
731 } else if (chspec_bw == WL_CHANSPEC_BW_160) {
732 center_ch = wf_5g_160m_chans;
733 num_ch = WF_NUM_5G_160M_CHANS;
734 } else {
735 /* invalid bandwidth */
736 return FALSE;
737 }
738
739 /* check for a valid center channel */
740 if (chspec_bw == WL_CHANSPEC_BW_20) {
741 /* We don't have an array of legal 20MHz 5G channels, but they are
742 * each side of the legal 40MHz channels. Check the chanspec
743 * channel against either side of the 40MHz channels.
744 */
745 for (i = 0; i < num_ch; i ++) {
746 if (chspec_ch == (uint)LOWER_20_SB(center_ch[i]) ||
747 chspec_ch == (uint)UPPER_20_SB(center_ch[i]))
748 break; /* match found */
749 }
750
751 if (i == num_ch) {
752 /* check for channel 165 which is not the side band
753 * of 40MHz 5G channel
754 */
755 if (chspec_ch == 165)
756 i = 0;
757
758 /* check for legacy JP channels on failure */
759 if (chspec_ch == 34 || chspec_ch == 38 ||
760 chspec_ch == 42 || chspec_ch == 46)
761 i = 0;
762 }
763 } else {
764 /* check the chanspec channel to each legal channel */
765 for (i = 0; i < num_ch; i ++) {
766 if (chspec_ch == center_ch[i])
767 break; /* match found */
768 }
769 }
770
771 if (i < num_ch) {
772 /* match found */
773 return TRUE;
774 }
775 }
776 }
777
778 return FALSE;
779 }
780
781 /*
782 * This function returns TRUE if both the chanspec can co-exist in PHY.
783 * Addition to primary20 channel, the function checks for side band for 2g 40 channels
784 */
785 bool
wf_chspec_coexist(chanspec_t chspec1,chanspec_t chspec2)786 wf_chspec_coexist(chanspec_t chspec1, chanspec_t chspec2)
787 {
788 bool same_primary;
789
790 same_primary = (wf_chspec_primary20_chan(chspec1) == wf_chspec_primary20_chan(chspec2));
791
792 if (same_primary && CHSPEC_IS2G(chspec1)) {
793 if (CHSPEC_IS40(chspec1) && CHSPEC_IS40(chspec2)) {
794 return (CHSPEC_CTL_SB(chspec1) == CHSPEC_CTL_SB(chspec2));
795 }
796 }
797 return same_primary;
798 }
799
800 /**
801 * Create a 20MHz chanspec for the given band.
802 *
803 * This function returns a 20MHz chanspec in the given band.
804 *
805 * @param channel 20MHz channel number
806 * @param band a chanspec band (e.g. WL_CHANSPEC_BAND_2G)
807 *
808 * @return Returns a 20MHz chanspec, or IVNCHANSPEC in case of error.
809 */
810 chanspec_t
wf_create_20MHz_chspec(uint channel,chanspec_band_t band)811 wf_create_20MHz_chspec(uint channel, chanspec_band_t band)
812 {
813 chanspec_t chspec;
814
815 if (channel <= WL_CHANSPEC_CHAN_MASK &&
816 (band == WL_CHANSPEC_BAND_2G ||
817 band == WL_CHANSPEC_BAND_5G)) {
818 chspec = band | WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE | channel;
819 if (!wf_chspec_valid(chspec)) {
820 chspec = INVCHANSPEC;
821 }
822 } else {
823 chspec = INVCHANSPEC;
824 }
825
826 return chspec;
827 }
828
829 /**
830 * Return the primary 20MHz channel.
831 *
832 * This function returns the channel number of the primary 20MHz channel. For
833 * 20MHz channels this is just the channel number. For 40MHz or wider channels
834 * it is the primary 20MHz channel specified by the chanspec.
835 *
836 * @param chspec input chanspec
837 *
838 * @return Returns the channel number of the primary 20MHz channel
839 */
840 uint8
wf_chspec_primary20_chan(chanspec_t chspec)841 wf_chspec_primary20_chan(chanspec_t chspec)
842 {
843 uint center_chan;
844 uint bw_mhz;
845 uint sb;
846
847 ASSERT(!wf_chspec_malformed(chspec));
848
849 /* Is there a sideband ? */
850 if (CHSPEC_IS20(chspec)) {
851 return CHSPEC_CHANNEL(chspec);
852 } else {
853 sb = CHSPEC_CTL_SB(chspec) >> WL_CHANSPEC_CTL_SB_SHIFT;
854
855 if (CHSPEC_IS8080(chspec)) {
856 /* For an 80+80 MHz channel, the sideband 'sb' field is an 80 MHz sideband
857 * (LL, LU, UL, LU) for the 80 MHz frequency segment 0.
858 */
859 uint chan_id = CHSPEC_CHAN1(chspec);
860
861 bw_mhz = 80;
862
863 /* convert from channel index to channel number */
864 center_chan = wf_5g_80m_chans[chan_id];
865 }
866 else {
867 bw_mhz = wf_bw_chspec_to_mhz(chspec);
868 center_chan = CHSPEC_CHANNEL(chspec) >> WL_CHANSPEC_CHAN_SHIFT;
869 }
870
871 return (channel_to_primary20_chan(center_chan, bw_mhz, sb));
872 }
873 }
874
875 /* given a chanspec, return the bandwidth string */
876 const char *
BCMRAMFN(wf_chspec_to_bw_str)877 BCMRAMFN(wf_chspec_to_bw_str)(chanspec_t chspec)
878 {
879 return wf_chspec_bw_str[(CHSPEC_BW(chspec) >> WL_CHANSPEC_BW_SHIFT)];
880 }
881
882 /*
883 * Return the primary 20MHz chanspec of the given chanspec
884 */
885 chanspec_t
wf_chspec_primary20_chspec(chanspec_t chspec)886 wf_chspec_primary20_chspec(chanspec_t chspec)
887 {
888 chanspec_t pri_chspec = chspec;
889 uint8 pri_chan;
890
891 ASSERT(!wf_chspec_malformed(chspec));
892
893 /* Is there a sideband ? */
894 if (!CHSPEC_IS20(chspec)) {
895 pri_chan = wf_chspec_primary20_chan(chspec);
896 pri_chspec = pri_chan | WL_CHANSPEC_BW_20;
897 pri_chspec |= CHSPEC_BAND(chspec);
898 }
899 return pri_chspec;
900 }
901
902 /* return chanspec given primary 20MHz channel and bandwidth
903 * return 0 on error
904 */
905 uint16
wf_channel2chspec(uint pri_ch,uint bw)906 wf_channel2chspec(uint pri_ch, uint bw)
907 {
908 uint16 chspec;
909 const uint8 *center_ch = NULL;
910 int num_ch = 0;
911 int sb = -1;
912 int i = 0;
913
914 chspec = ((pri_ch <= CH_MAX_2G_CHANNEL) ? WL_CHANSPEC_BAND_2G : WL_CHANSPEC_BAND_5G);
915
916 chspec |= bw;
917
918 if (bw == WL_CHANSPEC_BW_40) {
919 center_ch = wf_5g_40m_chans;
920 num_ch = WF_NUM_5G_40M_CHANS;
921 bw = 40;
922 } else if (bw == WL_CHANSPEC_BW_80) {
923 center_ch = wf_5g_80m_chans;
924 num_ch = WF_NUM_5G_80M_CHANS;
925 bw = 80;
926 } else if (bw == WL_CHANSPEC_BW_160) {
927 center_ch = wf_5g_160m_chans;
928 num_ch = WF_NUM_5G_160M_CHANS;
929 bw = 160;
930 } else if (bw == WL_CHANSPEC_BW_20) {
931 chspec |= pri_ch;
932 return chspec;
933 } else {
934 return 0;
935 }
936
937 for (i = 0; i < num_ch; i ++) {
938 sb = channel_to_sb(center_ch[i], pri_ch, bw);
939 if (sb >= 0) {
940 chspec |= center_ch[i];
941 chspec |= (sb << WL_CHANSPEC_CTL_SB_SHIFT);
942 break;
943 }
944 }
945
946 /* check for no matching sb/center */
947 if (sb < 0) {
948 return 0;
949 }
950
951 return chspec;
952 }
953
954 /*
955 * This function returns the chanspec for the primary 40MHz of an 80MHz or wider channel.
956 * The primary 20MHz channel of the returned 40MHz chanspec is the same as the primary 20MHz
957 * channel of the input chanspec.
958 */
wf_chspec_primary40_chspec(chanspec_t chspec)959 extern chanspec_t wf_chspec_primary40_chspec(chanspec_t chspec)
960 {
961 chanspec_t chspec40 = chspec;
962 uint center_chan;
963 uint sb;
964
965 ASSERT(!wf_chspec_malformed(chspec));
966
967 /* if the chanspec is > 80MHz, use the helper routine to find the primary 80 MHz channel */
968 if (CHSPEC_IS8080(chspec) || CHSPEC_IS160(chspec)) {
969 chspec = wf_chspec_primary80_chspec(chspec);
970 }
971
972 /* determine primary 40 MHz sub-channel of an 80 MHz chanspec */
973 if (CHSPEC_IS80(chspec)) {
974 center_chan = CHSPEC_CHANNEL(chspec);
975 sb = CHSPEC_CTL_SB(chspec);
976
977 if (sb < WL_CHANSPEC_CTL_SB_UL) {
978 /* Primary 40MHz is on lower side */
979 center_chan -= CH_20MHZ_APART;
980 /* sideband bits are the same for LL/LU and L/U */
981 } else {
982 /* Primary 40MHz is on upper side */
983 center_chan += CH_20MHZ_APART;
984 /* sideband bits need to be adjusted by UL offset */
985 sb -= WL_CHANSPEC_CTL_SB_UL;
986 }
987
988 /* Create primary 40MHz chanspec */
989 chspec40 = (chanspec_t)(WL_CHANSPEC_BAND_5G | WL_CHANSPEC_BW_40 |
990 sb | center_chan);
991 }
992
993 return chspec40;
994 }
995
996 /*
997 * Return the channel number for a given frequency and base frequency.
998 * The returned channel number is relative to the given base frequency.
999 * If the given base frequency is zero, a base frequency of 5 GHz is assumed for
1000 * frequencies from 5 - 6 GHz, and 2.407 GHz is assumed for 2.4 - 2.5 GHz.
1001 *
1002 * Frequency is specified in MHz.
1003 * The base frequency is specified as (start_factor * 500 kHz).
1004 * Constants WF_CHAN_FACTOR_2_4_G, WF_CHAN_FACTOR_5_G are defined for
1005 * 2.4 GHz and 5 GHz bands.
1006 *
1007 * The returned channel will be in the range [1, 14] in the 2.4 GHz band
1008 * and [0, 200] otherwise.
1009 * -1 is returned if the start_factor is WF_CHAN_FACTOR_2_4_G and the
1010 * frequency is not a 2.4 GHz channel, or if the frequency is not and even
1011 * multiple of 5 MHz from the base frequency to the base plus 1 GHz.
1012 *
1013 * Reference 802.11-2016, section 17.3.8.3 and section 16.3.6.3
1014 */
1015 int
wf_mhz2channel(uint freq,uint start_factor)1016 wf_mhz2channel(uint freq, uint start_factor)
1017 {
1018 int ch = -1;
1019 uint base;
1020 int offset;
1021
1022 /* take the default channel start frequency */
1023 if (start_factor == 0) {
1024 if (freq >= 2400 && freq <= 2500)
1025 start_factor = WF_CHAN_FACTOR_2_4_G;
1026 else if (freq >= 5000 && freq <= 6000)
1027 start_factor = WF_CHAN_FACTOR_5_G;
1028 }
1029
1030 if (freq == 2484 && start_factor == WF_CHAN_FACTOR_2_4_G)
1031 return 14;
1032
1033 base = start_factor / 2;
1034
1035 /* check that the frequency is in 1GHz range of the base */
1036 if ((freq < base) || (freq > base + 1000))
1037 return -1;
1038
1039 offset = (int)(freq - base);
1040 ch = offset / 5;
1041
1042 /* check that frequency is a 5MHz multiple from the base */
1043 if (offset != (ch * 5))
1044 return -1;
1045
1046 /* restricted channel range check for 2.4G */
1047 if (start_factor == WF_CHAN_FACTOR_2_4_G && (ch < 1 || ch > 13))
1048 return -1;
1049
1050 return ch;
1051 }
1052
1053 /*
1054 * Return the center frequency in MHz of the given channel and base frequency.
1055 * The channel number is interpreted relative to the given base frequency.
1056 *
1057 * The valid channel range is [1, 14] in the 2.4 GHz band and [0, 200] otherwise.
1058 * The base frequency is specified as (start_factor * 500 kHz).
1059 * Constants WF_CHAN_FACTOR_2_4_G, WF_CHAN_FACTOR_4_G, and WF_CHAN_FACTOR_5_G
1060 * are defined for 2.4 GHz, 4 GHz, and 5 GHz bands.
1061 * The channel range of [1, 14] is only checked for a start_factor of
1062 * WF_CHAN_FACTOR_2_4_G (4814 = 2407 * 2).
1063 * Odd start_factors produce channels on .5 MHz boundaries, in which case
1064 * the answer is rounded down to an integral MHz.
1065 * -1 is returned for an out of range channel.
1066 *
1067 * Reference 802.11-2016, section 17.3.8.3 and section 16.3.6.3
1068 */
1069 int
wf_channel2mhz(uint ch,uint start_factor)1070 wf_channel2mhz(uint ch, uint start_factor)
1071 {
1072 int freq;
1073
1074 if ((start_factor == WF_CHAN_FACTOR_2_4_G && (ch < 1 || ch > 14)) ||
1075 (ch > 200))
1076 freq = -1;
1077 else if ((start_factor == WF_CHAN_FACTOR_2_4_G) && (ch == 14))
1078 freq = 2484;
1079 else
1080 freq = (int)(ch * 5 + start_factor / 2);
1081
1082 return freq;
1083 }
1084
1085 static const uint16 sidebands[] = {
1086 WL_CHANSPEC_CTL_SB_LLL, WL_CHANSPEC_CTL_SB_LLU,
1087 WL_CHANSPEC_CTL_SB_LUL, WL_CHANSPEC_CTL_SB_LUU,
1088 WL_CHANSPEC_CTL_SB_ULL, WL_CHANSPEC_CTL_SB_ULU,
1089 WL_CHANSPEC_CTL_SB_UUL, WL_CHANSPEC_CTL_SB_UUU
1090 };
1091
1092 /*
1093 * Returns the chanspec 80Mhz channel corresponding to the following input
1094 * parameters
1095 *
1096 * primary_channel - primary 20Mhz channel
1097 * center_channel - center frequecny of the 80Mhz channel
1098 *
1099 * The center_channel can be one of {42, 58, 106, 122, 138, 155}
1100 *
1101 * returns INVCHANSPEC in case of error
1102 */
1103 chanspec_t
wf_chspec_80(uint8 center_channel,uint8 primary_channel)1104 wf_chspec_80(uint8 center_channel, uint8 primary_channel)
1105 {
1106
1107 chanspec_t chanspec = INVCHANSPEC;
1108 chanspec_t chanspec_cur;
1109 uint i;
1110
1111 for (i = 0; i < WF_NUM_SIDEBANDS_80MHZ; i++) {
1112 chanspec_cur = CH80MHZ_CHSPEC(center_channel, sidebands[i]);
1113 if (primary_channel == wf_chspec_primary20_chan(chanspec_cur)) {
1114 chanspec = chanspec_cur;
1115 break;
1116 }
1117 }
1118 /* If the loop ended early, we are good, otherwise we did not
1119 * find a 80MHz chanspec with the given center_channel that had a primary channel
1120 *matching the given primary_channel.
1121 */
1122 return chanspec;
1123 }
1124
1125 /*
1126 * Returns the 80+80 chanspec corresponding to the following input parameters
1127 *
1128 * primary_20mhz - Primary 20 MHz channel
1129 * chan0 - center channel number of one frequency segment
1130 * chan1 - center channel number of the other frequency segment
1131 *
1132 * Parameters chan0 and chan1 are channel numbers in {42, 58, 106, 122, 138, 155}.
1133 * The primary channel must be contained in one of the 80MHz channels. This routine
1134 * will determine which frequency segment is the primary 80 MHz segment.
1135 *
1136 * Returns INVCHANSPEC in case of error.
1137 *
1138 * Refer to 802.11-2016 section 22.3.14 "Channelization".
1139 */
1140 chanspec_t
wf_chspec_get8080_chspec(uint8 primary_20mhz,uint8 chan0,uint8 chan1)1141 wf_chspec_get8080_chspec(uint8 primary_20mhz, uint8 chan0, uint8 chan1)
1142 {
1143 int sb = 0;
1144 uint16 chanspec = 0;
1145 int chan0_id = 0, chan1_id = 0;
1146 int seg0, seg1;
1147
1148 chan0_id = channel_80mhz_to_id(chan0);
1149 chan1_id = channel_80mhz_to_id(chan1);
1150
1151 /* make sure the channel numbers were valid */
1152 if (chan0_id == -1 || chan1_id == -1)
1153 return INVCHANSPEC;
1154
1155 /* does the primary channel fit with the 1st 80MHz channel ? */
1156 sb = channel_to_sb(chan0, primary_20mhz, 80);
1157 if (sb >= 0) {
1158 /* yes, so chan0 is frequency segment 0, and chan1 is seg 1 */
1159 seg0 = chan0_id;
1160 seg1 = chan1_id;
1161 } else {
1162 /* no, so does the primary channel fit with the 2nd 80MHz channel ? */
1163 sb = channel_to_sb(chan1, primary_20mhz, 80);
1164 if (sb < 0) {
1165 /* no match for pri_ch to either 80MHz center channel */
1166 return INVCHANSPEC;
1167 }
1168 /* swapped, so chan1 is frequency segment 0, and chan0 is seg 1 */
1169 seg0 = chan1_id;
1170 seg1 = chan0_id;
1171 }
1172
1173 chanspec = (uint16)((seg0 << WL_CHANSPEC_CHAN1_SHIFT) |
1174 (seg1 << WL_CHANSPEC_CHAN2_SHIFT) |
1175 (sb << WL_CHANSPEC_CTL_SB_SHIFT) |
1176 WL_CHANSPEC_BW_8080 |
1177 WL_CHANSPEC_BAND_5G);
1178
1179 return chanspec;
1180 }
1181
1182 /*
1183 * This function returns the 80Mhz channel for the given id.
1184 */
1185 static uint8
wf_chspec_get80Mhz_ch(uint8 chan_80Mhz_id)1186 wf_chspec_get80Mhz_ch(uint8 chan_80Mhz_id)
1187 {
1188 if (chan_80Mhz_id < WF_NUM_5G_80M_CHANS)
1189 return wf_5g_80m_chans[chan_80Mhz_id];
1190
1191 return 0;
1192 }
1193
1194 /*
1195 * Returns the center channel of the primary 80 MHz sub-band of the provided chanspec
1196 */
1197 uint8
wf_chspec_primary80_channel(chanspec_t chanspec)1198 wf_chspec_primary80_channel(chanspec_t chanspec)
1199 {
1200 chanspec_t primary80_chspec;
1201 uint8 primary80_chan;
1202
1203 primary80_chspec = wf_chspec_primary80_chspec(chanspec);
1204
1205 if (primary80_chspec == INVCHANSPEC) {
1206 primary80_chan = INVCHANNEL;
1207 } else {
1208 primary80_chan = CHSPEC_CHANNEL(primary80_chspec);
1209 }
1210
1211 return primary80_chan;
1212 }
1213
1214 /*
1215 * Returns the center channel of the secondary 80 MHz sub-band of the provided chanspec
1216 */
1217 uint8
wf_chspec_secondary80_channel(chanspec_t chanspec)1218 wf_chspec_secondary80_channel(chanspec_t chanspec)
1219 {
1220 chanspec_t secondary80_chspec;
1221 uint8 secondary80_chan;
1222
1223 secondary80_chspec = wf_chspec_secondary80_chspec(chanspec);
1224
1225 if (secondary80_chspec == INVCHANSPEC) {
1226 secondary80_chan = INVCHANNEL;
1227 } else {
1228 secondary80_chan = CHSPEC_CHANNEL(secondary80_chspec);
1229 }
1230
1231 return secondary80_chan;
1232 }
1233
1234 /*
1235 * Returns the chanspec for the primary 80MHz sub-band of an 160MHz or 80+80 channel
1236 */
1237 chanspec_t
wf_chspec_primary80_chspec(chanspec_t chspec)1238 wf_chspec_primary80_chspec(chanspec_t chspec)
1239 {
1240 chanspec_t chspec80;
1241 uint center_chan;
1242 uint sb;
1243
1244 ASSERT(!wf_chspec_malformed(chspec));
1245
1246 if (CHSPEC_IS80(chspec)) {
1247 chspec80 = chspec;
1248 }
1249 else if (CHSPEC_IS8080(chspec)) {
1250 sb = CHSPEC_CTL_SB(chspec);
1251
1252 /* primary sub-band is stored in seg0 */
1253 center_chan = wf_chspec_get80Mhz_ch(CHSPEC_CHAN1(chspec));
1254
1255 /* Create primary 80MHz chanspec */
1256 chspec80 = (chanspec_t)(WL_CHANSPEC_BAND_5G | WL_CHANSPEC_BW_80 | sb | center_chan);
1257 }
1258 else if (CHSPEC_IS160(chspec)) {
1259 center_chan = CHSPEC_CHANNEL(chspec);
1260 sb = CHSPEC_CTL_SB(chspec);
1261
1262 if (sb < WL_CHANSPEC_CTL_SB_ULL) {
1263 /* Primary 80MHz is on lower side */
1264 center_chan -= CH_40MHZ_APART;
1265 }
1266 else {
1267 /* Primary 80MHz is on upper side */
1268 center_chan += CH_40MHZ_APART;
1269 sb -= WL_CHANSPEC_CTL_SB_ULL;
1270 }
1271
1272 /* Create primary 80MHz chanspec */
1273 chspec80 = (chanspec_t)(WL_CHANSPEC_BAND_5G | WL_CHANSPEC_BW_80 | sb | center_chan);
1274 }
1275 else {
1276 chspec80 = INVCHANSPEC;
1277 }
1278
1279 return chspec80;
1280 }
1281
1282 /*
1283 * Returns the chanspec for the secondary 80MHz sub-band of an 160MHz or 80+80 channel
1284 */
1285 chanspec_t
wf_chspec_secondary80_chspec(chanspec_t chspec)1286 wf_chspec_secondary80_chspec(chanspec_t chspec)
1287 {
1288 chanspec_t chspec80;
1289 uint center_chan;
1290
1291 ASSERT(!wf_chspec_malformed(chspec));
1292
1293 if (CHSPEC_IS8080(chspec)) {
1294 /* secondary sub-band is stored in seg1 */
1295 center_chan = wf_chspec_get80Mhz_ch(CHSPEC_CHAN2(chspec));
1296
1297 /* Create secondary 80MHz chanspec */
1298 chspec80 = (chanspec_t)(WL_CHANSPEC_BAND_5G |
1299 WL_CHANSPEC_BW_80 |
1300 WL_CHANSPEC_CTL_SB_LL |
1301 center_chan);
1302 }
1303 else if (CHSPEC_IS160(chspec)) {
1304 center_chan = CHSPEC_CHANNEL(chspec);
1305
1306 if (CHSPEC_CTL_SB(chspec) < WL_CHANSPEC_CTL_SB_ULL) {
1307 /* Primary 80MHz is on lower side */
1308 center_chan -= CH_40MHZ_APART;
1309 }
1310 else {
1311 /* Primary 80MHz is on upper side */
1312 center_chan += CH_40MHZ_APART;
1313 }
1314
1315 /* Create secondary 80MHz chanspec */
1316 chspec80 = (chanspec_t)(WL_CHANSPEC_BAND_5G |
1317 WL_CHANSPEC_BW_80 |
1318 WL_CHANSPEC_CTL_SB_LL |
1319 center_chan);
1320 }
1321 else {
1322 chspec80 = INVCHANSPEC;
1323 }
1324
1325 return chspec80;
1326 }
1327
1328 /*
1329 * For 160MHz or 80P80 chanspec, set ch[0]/ch[1] to be the low/high 80 Mhz channels
1330 *
1331 * For 20/40/80MHz chanspec, set ch[0] to be the center freq, and chan[1]=-1
1332 */
1333 void
wf_chspec_get_80p80_channels(chanspec_t chspec,uint8 * ch)1334 wf_chspec_get_80p80_channels(chanspec_t chspec, uint8 *ch)
1335 {
1336
1337 if (CHSPEC_IS8080(chspec)) {
1338 ch[0] = wf_chspec_get80Mhz_ch(CHSPEC_CHAN1(chspec));
1339 ch[1] = wf_chspec_get80Mhz_ch(CHSPEC_CHAN2(chspec));
1340 }
1341 else if (CHSPEC_IS160(chspec)) {
1342 uint8 center_chan = CHSPEC_CHANNEL(chspec);
1343 ch[0] = center_chan - CH_40MHZ_APART;
1344 ch[1] = center_chan + CH_40MHZ_APART;
1345 }
1346 else {
1347 /* for 20, 40, and 80 Mhz */
1348 ch[0] = CHSPEC_CHANNEL(chspec);
1349 ch[1] = 0xFFu;
1350 }
1351 return;
1352
1353 }
1354
1355 #ifdef WL11AC_80P80
1356 uint8
wf_chspec_channel(chanspec_t chspec)1357 wf_chspec_channel(chanspec_t chspec)
1358 {
1359 if (CHSPEC_IS8080(chspec)) {
1360 return wf_chspec_primary80_channel(chspec);
1361 }
1362 else {
1363 return ((uint8)((chspec) & WL_CHANSPEC_CHAN_MASK));
1364 }
1365 }
1366 #endif /* WL11AC_80P80 */
1367
1368 /* This routine returns the chanspec for a given operating class and
1369 * channel number
1370 */
1371 chanspec_t
wf_channel_create_chspec_frm_opclass(uint8 opclass,uint8 channel)1372 wf_channel_create_chspec_frm_opclass(uint8 opclass, uint8 channel)
1373 {
1374 chanspec_t chanspec = 0;
1375 uint16 opclass_info = 0;
1376 uint16 lookupindex = 0;
1377 switch (opclass) {
1378 case 115:
1379 lookupindex = 1;
1380 break;
1381 case 124:
1382 lookupindex = 3;
1383 break;
1384 case 125:
1385 lookupindex = 5;
1386 break;
1387 case 81:
1388 lookupindex = 12;
1389 break;
1390 case 116:
1391 lookupindex = 22;
1392 break;
1393 case 119:
1394 lookupindex = 23;
1395 break;
1396 case 126:
1397 lookupindex = 25;
1398 break;
1399 case 83:
1400 lookupindex = 32;
1401 break;
1402 case 84:
1403 lookupindex = 33;
1404 break;
1405 default:
1406 lookupindex = 12;
1407 }
1408
1409 if (lookupindex < 33) {
1410 opclass_info = opclass_data[lookupindex-1];
1411 }
1412 else {
1413 opclass_info = opclass_data[11];
1414 }
1415 chanspec = opclass_info | (uint16)channel;
1416 return chanspec;
1417 }
1418
1419 /* This routine returns the opclass for a given chanspec */
1420 int
wf_channel_create_opclass_frm_chspec(chanspec_t chspec)1421 wf_channel_create_opclass_frm_chspec(chanspec_t chspec)
1422 {
1423 BCM_REFERENCE(chspec);
1424 /* TODO: Implement this function ! */
1425 return 12; /* opclass 12 for basic 2G channels */
1426 }
1427
1428 /* Populates array with all 20MHz side bands of a given chanspec_t in the following order:
1429 * primary20, secondary20, two secondary40s, four secondary80s.
1430 * 'chspec' is the chanspec of interest
1431 * 'pext' must point to an uint8 array of long enough to hold all side bands of the given chspec
1432 *
1433 * Works with 20, 40, 80, 80p80 and 160MHz chspec
1434 */
1435 void
wf_get_all_ext(chanspec_t chspec,uint8 * pext)1436 wf_get_all_ext(chanspec_t chspec, uint8 *pext)
1437 {
1438 #ifdef WL11N_20MHZONLY
1439 GET_ALL_SB(chspec, pext);
1440 #else /* !WL11N_20MHZONLY */
1441 chanspec_t t = (CHSPEC_IS160(chspec) || CHSPEC_IS8080(chspec)) ? /* if bw > 80MHz */
1442 wf_chspec_primary80_chspec(chspec) : (chspec); /* extract primary 80 */
1443 /* primary20 channel as first element */
1444 uint8 pri_ch = (pext)[0] = wf_chspec_primary20_chan(t);
1445 if (CHSPEC_IS20(chspec)) return; /* nothing more to do since 20MHz chspec */
1446 /* 20MHz EXT */
1447 (pext)[1] = pri_ch + (uint8)(IS_CTL_IN_L20(t) ? CH_20MHZ_APART : -CH_20MHZ_APART);
1448 if (CHSPEC_IS40(chspec)) return; /* nothing more to do since 40MHz chspec */
1449 /* center 40MHz EXT */
1450 t = wf_channel2chspec((uint)(pri_ch + (IS_CTL_IN_L40(chspec) ?
1451 CH_40MHZ_APART : -CH_40MHZ_APART)), WL_CHANSPEC_BW_40);
1452 GET_ALL_SB(t, &((pext)[2])); /* get the 20MHz side bands in 40MHz EXT */
1453 if (CHSPEC_IS80(chspec)) return; /* nothing more to do since 80MHz chspec */
1454 t = CH80MHZ_CHSPEC(wf_chspec_secondary80_channel(chspec), WL_CHANSPEC_CTL_SB_LLL);
1455 /* get the 20MHz side bands in 80MHz EXT (secondary) */
1456 GET_ALL_SB(t, &((pext)[4]));
1457 #endif /* !WL11N_20MHZONLY */
1458 }
1459
1460 /*
1461 * Given two chanspecs, returns true if they overlap.
1462 * (Overlap: At least one 20MHz subband is common between the two chanspecs provided)
1463 */
wf_chspec_overlap(chanspec_t chspec0,chanspec_t chspec1)1464 bool wf_chspec_overlap(chanspec_t chspec0, chanspec_t chspec1)
1465 {
1466 uint8 ch0, ch1;
1467
1468 FOREACH_20_SB(chspec0, ch0) {
1469 FOREACH_20_SB(chspec1, ch1) {
1470 if (ABS(ch0 - ch1) < CH_20MHZ_APART) {
1471 return TRUE;
1472 }
1473 }
1474 }
1475
1476 return FALSE;
1477 }
1478
1479 uint8
channel_bw_to_width(chanspec_t chspec)1480 channel_bw_to_width(chanspec_t chspec)
1481 {
1482 uint8 channel_width;
1483
1484 if (CHSPEC_IS80(chspec))
1485 channel_width = VHT_OP_CHAN_WIDTH_80;
1486 else if (CHSPEC_IS160(chspec))
1487 channel_width = VHT_OP_CHAN_WIDTH_160;
1488 else if (CHSPEC_IS8080(chspec))
1489 channel_width = VHT_OP_CHAN_WIDTH_80_80;
1490 else
1491 channel_width = VHT_OP_CHAN_WIDTH_20_40;
1492
1493 return channel_width;
1494 }
1495