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