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