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