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