bcmwifi_channels.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  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-2015, 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. * $Id: bcmwifi_channels.c 309193 2012-01-19 00:03:57Z $
  26. */
  27. #include <bcm_cfg.h>
  28. #include <typedefs.h>
  29. #include <bcmutils.h>
  30. #ifdef BCMDRIVER
  31. #include <osl.h>
  32. #define strtoul(nptr, endptr, base) bcm_strtoul((nptr), (endptr), (base))
  33. #define tolower(c) (bcm_isupper((c)) ? ((c) + 'a' - 'A') : (c))
  34. #else
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <ctype.h>
  38. #ifndef ASSERT
  39. #define ASSERT(exp)
  40. #endif
  41. #endif /* BCMDRIVER */
  42. #include <bcmwifi_channels.h>
  43. #if defined(WIN32) && (defined(BCMDLL) || defined(WLMDLL))
  44. #include <bcmstdlib.h> /* For wl/exe/GNUmakefile.brcm_wlu and GNUmakefile.wlm_dll */
  45. #endif
  46. /* Definitions for D11AC capable Chanspec type */
  47. /* Chanspec ASCII representation with 802.11ac capability:
  48. * [<band> 'g'] <channel> ['/'<bandwidth> [<ctl-sideband>]['/'<1st80channel>'-'<2nd80channel>]]
  49. *
  50. * <band>:
  51. * (optional) 2, 3, 4, 5 for 2.4GHz, 3GHz, 4GHz, and 5GHz respectively.
  52. * Default value is 2g if channel <= 14, otherwise 5g.
  53. * <channel>:
  54. * channel number of the 5MHz, 10MHz, 20MHz channel,
  55. * or primary channel of 40MHz, 80MHz, 160MHz, or 80+80MHz channel.
  56. * <bandwidth>:
  57. * (optional) 5, 10, 20, 40, 80, 160, or 80+80. Default value is 20.
  58. * <primary-sideband>:
  59. * (only for 2.4GHz band 40MHz) U for upper sideband primary, L for lower.
  60. *
  61. * For 2.4GHz band 40MHz channels, the same primary channel may be the
  62. * upper sideband for one 40MHz channel, and the lower sideband for an
  63. * overlapping 40MHz channel. The U/L disambiguates which 40MHz channel
  64. * is being specified.
  65. *
  66. * For 40MHz in the 5GHz band and all channel bandwidths greater than
  67. * 40MHz, the U/L specificaion is not allowed since the channels are
  68. * non-overlapping and the primary sub-band is derived from its
  69. * position in the wide bandwidth channel.
  70. *
  71. * <1st80Channel>:
  72. * <2nd80Channel>:
  73. * Required for 80+80, otherwise not allowed.
  74. * Specifies the center channel of the first and second 80MHz band.
  75. *
  76. * In its simplest form, it is a 20MHz channel number, with the implied band
  77. * of 2.4GHz if channel number <= 14, and 5GHz otherwise.
  78. *
  79. * To allow for backward compatibility with scripts, the old form for
  80. * 40MHz channels is also allowed: <channel><ctl-sideband>
  81. *
  82. * <channel>:
  83. * primary channel of 40MHz, channel <= 14 is 2GHz, otherwise 5GHz
  84. * <ctl-sideband>:
  85. * "U" for upper, "L" for lower (or lower case "u" "l")
  86. *
  87. * 5 GHz Examples:
  88. * Chanspec BW Center Ch Channel Range Primary Ch
  89. * 5g8 20MHz 8 - -
  90. * 52 20MHz 52 - -
  91. * 52/40 40MHz 54 52-56 52
  92. * 56/40 40MHz 54 52-56 56
  93. * 52/80 80MHz 58 52-64 52
  94. * 56/80 80MHz 58 52-64 56
  95. * 60/80 80MHz 58 52-64 60
  96. * 64/80 80MHz 58 52-64 64
  97. * 52/160 160MHz 50 36-64 52
  98. * 36/160 160MGz 50 36-64 36
  99. * 36/80+80/42-106 80+80MHz 42,106 36-48,100-112 36
  100. *
  101. * 2 GHz Examples:
  102. * Chanspec BW Center Ch Channel Range Primary Ch
  103. * 2g8 20MHz 8 - -
  104. * 8 20MHz 8 - -
  105. * 6 20MHz 6 - -
  106. * 6/40l 40MHz 8 6-10 6
  107. * 6l 40MHz 8 6-10 6
  108. * 6/40u 40MHz 4 2-6 6
  109. * 6u 40MHz 4 2-6 6
  110. */
  111. /* bandwidth ASCII string */
  112. static const char *wf_chspec_bw_str[] =
  113. {
  114. "5",
  115. "10",
  116. "20",
  117. "40",
  118. "80",
  119. "160",
  120. "80+80",
  121. "na"
  122. };
  123. static const uint8 wf_chspec_bw_mhz[] =
  124. {5, 10, 20, 40, 80, 160, 160};
  125. #define WF_NUM_BW \
  126. (sizeof(wf_chspec_bw_mhz)/sizeof(uint8))
  127. /* 40MHz channels in 5GHz band */
  128. static const uint8 wf_5g_40m_chans[] =
  129. {38, 46, 54, 62, 102, 110, 118, 126, 134, 142, 151, 159};
  130. #define WF_NUM_5G_40M_CHANS \
  131. (sizeof(wf_5g_40m_chans)/sizeof(uint8))
  132. /* 80MHz channels in 5GHz band */
  133. static const uint8 wf_5g_80m_chans[] =
  134. {42, 58, 106, 122, 138, 155};
  135. #define WF_NUM_5G_80M_CHANS \
  136. (sizeof(wf_5g_80m_chans)/sizeof(uint8))
  137. /* 160MHz channels in 5GHz band */
  138. static const uint8 wf_5g_160m_chans[] =
  139. {50, 114};
  140. #define WF_NUM_5G_160M_CHANS \
  141. (sizeof(wf_5g_160m_chans)/sizeof(uint8))
  142. /* convert bandwidth from chanspec to MHz */
  143. static uint
  144. bw_chspec_to_mhz(chanspec_t chspec)
  145. {
  146. uint bw;
  147. bw = (chspec & WL_CHANSPEC_BW_MASK) >> WL_CHANSPEC_BW_SHIFT;
  148. return (bw >= WF_NUM_BW ? 0 : wf_chspec_bw_mhz[bw]);
  149. }
  150. /* bw in MHz, return the channel count from the center channel to the
  151. * the channel at the edge of the band
  152. */
  153. static uint8
  154. center_chan_to_edge(uint bw)
  155. {
  156. /* edge channels separated by BW - 10MHz on each side
  157. * delta from cf to edge is half of that,
  158. * MHz to channel num conversion is 5MHz/channel
  159. */
  160. return (uint8)(((bw - 20) / 2) / 5);
  161. }
  162. /* return channel number of the low edge of the band
  163. * given the center channel and BW
  164. */
  165. static uint8
  166. channel_low_edge(uint center_ch, uint bw)
  167. {
  168. return (uint8)(center_ch - center_chan_to_edge(bw));
  169. }
  170. /* return side band number given center channel and control channel
  171. * return -1 on error
  172. */
  173. static int
  174. channel_to_sb(uint center_ch, uint ctl_ch, uint bw)
  175. {
  176. uint lowest = channel_low_edge(center_ch, bw);
  177. uint sb;
  178. if ((ctl_ch - lowest) % 4) {
  179. /* bad ctl channel, not mult 4 */
  180. return -1;
  181. }
  182. sb = ((ctl_ch - lowest) / 4);
  183. /* sb must be a index to a 20MHz channel in range */
  184. if (sb >= (bw / 20)) {
  185. /* ctl_ch must have been too high for the center_ch */
  186. return -1;
  187. }
  188. return sb;
  189. }
  190. /* return control channel given center channel and side band */
  191. static uint8
  192. channel_to_ctl_chan(uint center_ch, uint bw, uint sb)
  193. {
  194. return (uint8)(channel_low_edge(center_ch, bw) + sb * 4);
  195. }
  196. /* return index of 80MHz channel from channel number
  197. * return -1 on error
  198. */
  199. static int
  200. channel_80mhz_to_id(uint ch)
  201. {
  202. uint i;
  203. for (i = 0; i < WF_NUM_5G_80M_CHANS; i ++) {
  204. if (ch == wf_5g_80m_chans[i])
  205. return i;
  206. }
  207. return -1;
  208. }
  209. /* given a chanspec and a string buffer, format the chanspec as a
  210. * string, and return the original pointer a.
  211. * Min buffer length must be CHANSPEC_STR_LEN.
  212. * On error return NULL
  213. */
  214. char *
  215. wf_chspec_ntoa(chanspec_t chspec, char *buf)
  216. {
  217. const char *band;
  218. uint ctl_chan;
  219. if (wf_chspec_malformed(chspec))
  220. return NULL;
  221. band = "";
  222. /* check for non-default band spec */
  223. if ((CHSPEC_IS2G(chspec) && CHSPEC_CHANNEL(chspec) > CH_MAX_2G_CHANNEL) ||
  224. (CHSPEC_IS5G(chspec) && CHSPEC_CHANNEL(chspec) <= CH_MAX_2G_CHANNEL))
  225. band = (CHSPEC_IS2G(chspec)) ? "2g" : "5g";
  226. /* ctl channel */
  227. ctl_chan = wf_chspec_ctlchan(chspec);
  228. /* bandwidth and ctl sideband */
  229. if (CHSPEC_IS20(chspec)) {
  230. snprintf(buf, CHANSPEC_STR_LEN, "%s%d", band, ctl_chan);
  231. } else if (!CHSPEC_IS8080(chspec)) {
  232. const char *bw;
  233. const char *sb = "";
  234. bw = wf_chspec_bw_str[(chspec & WL_CHANSPEC_BW_MASK) >> WL_CHANSPEC_BW_SHIFT];
  235. #ifdef CHANSPEC_NEW_40MHZ_FORMAT
  236. /* ctl sideband string if needed for 2g 40MHz */
  237. if (CHSPEC_IS40(chspec) && CHSPEC_IS2G(chspec)) {
  238. sb = CHSPEC_SB_UPPER(chspec) ? "u" : "l";
  239. }
  240. snprintf(buf, CHANSPEC_STR_LEN, "%s%d/%s%s", band, ctl_chan, bw, sb);
  241. #else
  242. /* ctl sideband string instead of BW for 40MHz */
  243. if (CHSPEC_IS40(chspec)) {
  244. sb = CHSPEC_SB_UPPER(chspec) ? "u" : "l";
  245. snprintf(buf, CHANSPEC_STR_LEN, "%s%d%s", band, ctl_chan, sb);
  246. } else {
  247. snprintf(buf, CHANSPEC_STR_LEN, "%s%d/%s", band, ctl_chan, bw);
  248. }
  249. #endif /* CHANSPEC_NEW_40MHZ_FORMAT */
  250. } else {
  251. /* 80+80 */
  252. uint chan1 = (chspec & WL_CHANSPEC_CHAN1_MASK) >> WL_CHANSPEC_CHAN1_SHIFT;
  253. uint chan2 = (chspec & WL_CHANSPEC_CHAN2_MASK) >> WL_CHANSPEC_CHAN2_SHIFT;
  254. /* convert to channel number */
  255. chan1 = (chan1 < WF_NUM_5G_80M_CHANS) ? wf_5g_80m_chans[chan1] : 0;
  256. chan2 = (chan2 < WF_NUM_5G_80M_CHANS) ? wf_5g_80m_chans[chan2] : 0;
  257. /* Outputs a max of CHANSPEC_STR_LEN chars including '\0' */
  258. snprintf(buf, CHANSPEC_STR_LEN, "%d/80+80/%d-%d", ctl_chan, chan1, chan2);
  259. }
  260. return (buf);
  261. }
  262. static int
  263. read_uint(const char **p, unsigned int *num)
  264. {
  265. unsigned long val;
  266. char *endp = NULL;
  267. val = strtoul(*p, &endp, 10);
  268. /* if endp is the initial pointer value, then a number was not read */
  269. if (endp == *p)
  270. return 0;
  271. /* advance the buffer pointer to the end of the integer string */
  272. *p = endp;
  273. /* return the parsed integer */
  274. *num = (unsigned int)val;
  275. return 1;
  276. }
  277. /* given a chanspec string, convert to a chanspec.
  278. * On error return 0
  279. */
  280. chanspec_t
  281. wf_chspec_aton(const char *a)
  282. {
  283. chanspec_t chspec;
  284. uint chspec_ch, chspec_band, bw, chspec_bw, chspec_sb;
  285. uint num, ctl_ch;
  286. uint ch1, ch2;
  287. char c, sb_ul = '\0';
  288. int i;
  289. bw = 20;
  290. chspec_sb = 0;
  291. chspec_ch = ch1 = ch2 = 0;
  292. /* parse channel num or band */
  293. if (!read_uint(&a, &num))
  294. return 0;
  295. /* if we are looking at a 'g', then the first number was a band */
  296. c = tolower((int)a[0]);
  297. if (c == 'g') {
  298. a ++; /* consume the char */
  299. /* band must be "2" or "5" */
  300. if (num == 2)
  301. chspec_band = WL_CHANSPEC_BAND_2G;
  302. else if (num == 5)
  303. chspec_band = WL_CHANSPEC_BAND_5G;
  304. else
  305. return 0;
  306. /* read the channel number */
  307. if (!read_uint(&a, &ctl_ch))
  308. return 0;
  309. c = tolower((int)a[0]);
  310. }
  311. else {
  312. /* first number is channel, use default for band */
  313. ctl_ch = num;
  314. chspec_band = ((ctl_ch <= CH_MAX_2G_CHANNEL) ?
  315. WL_CHANSPEC_BAND_2G : WL_CHANSPEC_BAND_5G);
  316. }
  317. if (c == '\0') {
  318. /* default BW of 20MHz */
  319. chspec_bw = WL_CHANSPEC_BW_20;
  320. goto done_read;
  321. }
  322. a ++; /* consume the 'u','l', or '/' */
  323. /* check 'u'/'l' */
  324. if (c == 'u' || c == 'l') {
  325. sb_ul = c;
  326. chspec_bw = WL_CHANSPEC_BW_40;
  327. goto done_read;
  328. }
  329. /* next letter must be '/' */
  330. if (c != '/')
  331. return 0;
  332. /* read bandwidth */
  333. if (!read_uint(&a, &bw))
  334. return 0;
  335. /* convert to chspec value */
  336. if (bw == 20) {
  337. chspec_bw = WL_CHANSPEC_BW_20;
  338. } else if (bw == 40) {
  339. chspec_bw = WL_CHANSPEC_BW_40;
  340. } else if (bw == 80) {
  341. chspec_bw = WL_CHANSPEC_BW_80;
  342. } else if (bw == 160) {
  343. chspec_bw = WL_CHANSPEC_BW_160;
  344. } else {
  345. return 0;
  346. }
  347. /* So far we have <band>g<chan>/<bw>
  348. * Can now be followed by u/l if bw = 40,
  349. * or '+80' if bw = 80, to make '80+80' bw.
  350. */
  351. c = tolower((int)a[0]);
  352. /* if we have a 2g/40 channel, we should have a l/u spec now */
  353. if (chspec_band == WL_CHANSPEC_BAND_2G && bw == 40) {
  354. if (c == 'u' || c == 'l') {
  355. a ++; /* consume the u/l char */
  356. sb_ul = c;
  357. goto done_read;
  358. }
  359. }
  360. /* check for 80+80 */
  361. if (c == '+') {
  362. /* 80+80 */
  363. static const char *plus80 = "80/";
  364. /* must be looking at '+80/'
  365. * check and consume this string.
  366. */
  367. chspec_bw = WL_CHANSPEC_BW_8080;
  368. a ++; /* consume the char '+' */
  369. /* consume the '80/' string */
  370. for (i = 0; i < 3; i++) {
  371. if (*a++ != *plus80++) {
  372. return 0;
  373. }
  374. }
  375. /* read primary 80MHz channel */
  376. if (!read_uint(&a, &ch1))
  377. return 0;
  378. /* must followed by '-' */
  379. if (a[0] != '-')
  380. return 0;
  381. a ++; /* consume the char */
  382. /* read secondary 80MHz channel */
  383. if (!read_uint(&a, &ch2))
  384. return 0;
  385. }
  386. done_read:
  387. /* skip trailing white space */
  388. while (a[0] == ' ') {
  389. a ++;
  390. }
  391. /* must be end of string */
  392. if (a[0] != '\0')
  393. return 0;
  394. /* Now have all the chanspec string parts read;
  395. * chspec_band, ctl_ch, chspec_bw, sb_ul, ch1, ch2.
  396. * chspec_band and chspec_bw are chanspec values.
  397. * Need to convert ctl_ch, sb_ul, and ch1,ch2 into
  398. * a center channel (or two) and sideband.
  399. */
  400. /* if a sb u/l string was given, just use that,
  401. * guaranteed to be bw = 40 by sting parse.
  402. */
  403. if (sb_ul != '\0') {
  404. if (sb_ul == 'l') {
  405. chspec_ch = UPPER_20_SB(ctl_ch);
  406. chspec_sb = WL_CHANSPEC_CTL_SB_LLL;
  407. } else if (sb_ul == 'u') {
  408. chspec_ch = LOWER_20_SB(ctl_ch);
  409. chspec_sb = WL_CHANSPEC_CTL_SB_LLU;
  410. }
  411. }
  412. /* if the bw is 20, center and sideband are trivial */
  413. else if (chspec_bw == WL_CHANSPEC_BW_20) {
  414. chspec_ch = ctl_ch;
  415. chspec_sb = WL_CHANSPEC_CTL_SB_NONE;
  416. }
  417. /* if the bw is 40/80/160, not 80+80, a single method
  418. * can be used to to find the center and sideband
  419. */
  420. else if (chspec_bw != WL_CHANSPEC_BW_8080) {
  421. /* figure out ctl sideband based on ctl channel and bandwidth */
  422. const uint8 *center_ch = NULL;
  423. int num_ch = 0;
  424. int sb = -1;
  425. if (chspec_bw == WL_CHANSPEC_BW_40) {
  426. center_ch = wf_5g_40m_chans;
  427. num_ch = WF_NUM_5G_40M_CHANS;
  428. } else if (chspec_bw == WL_CHANSPEC_BW_80) {
  429. center_ch = wf_5g_80m_chans;
  430. num_ch = WF_NUM_5G_80M_CHANS;
  431. } else if (chspec_bw == WL_CHANSPEC_BW_160) {
  432. center_ch = wf_5g_160m_chans;
  433. num_ch = WF_NUM_5G_160M_CHANS;
  434. } else {
  435. return 0;
  436. }
  437. for (i = 0; i < num_ch; i ++) {
  438. sb = channel_to_sb(center_ch[i], ctl_ch, bw);
  439. if (sb >= 0) {
  440. chspec_ch = center_ch[i];
  441. chspec_sb = sb << WL_CHANSPEC_CTL_SB_SHIFT;
  442. break;
  443. }
  444. }
  445. /* check for no matching sb/center */
  446. if (sb < 0) {
  447. return 0;
  448. }
  449. }
  450. /* Otherwise, bw is 80+80. Figure out channel pair and sb */
  451. else {
  452. int ch1_id = 0, ch2_id = 0;
  453. int sb;
  454. ch1_id = channel_80mhz_to_id(ch1);
  455. ch2_id = channel_80mhz_to_id(ch2);
  456. /* validate channels */
  457. if (ch1 >= ch2 || ch1_id < 0 || ch2_id < 0)
  458. return 0;
  459. /* combined channel in chspec */
  460. chspec_ch = (((uint16)ch1_id << WL_CHANSPEC_CHAN1_SHIFT) |
  461. ((uint16)ch2_id << WL_CHANSPEC_CHAN2_SHIFT));
  462. /* figure out ctl sideband */
  463. /* does the primary channel fit with the 1st 80MHz channel ? */
  464. sb = channel_to_sb(ch1, ctl_ch, bw);
  465. if (sb < 0) {
  466. /* no, so does the primary channel fit with the 2nd 80MHz channel ? */
  467. sb = channel_to_sb(ch2, ctl_ch, bw);
  468. if (sb < 0) {
  469. /* no match for ctl_ch to either 80MHz center channel */
  470. return 0;
  471. }
  472. /* sb index is 0-3 for the low 80MHz channel, and 4-7 for
  473. * the high 80MHz channel. Add 4 to to shift to high set.
  474. */
  475. sb += 4;
  476. }
  477. chspec_sb = sb << WL_CHANSPEC_CTL_SB_SHIFT;
  478. }
  479. chspec = (chspec_ch | chspec_band | chspec_bw | chspec_sb);
  480. if (wf_chspec_malformed(chspec))
  481. return 0;
  482. return chspec;
  483. }
  484. /*
  485. * Verify the chanspec is using a legal set of parameters, i.e. that the
  486. * chanspec specified a band, bw, ctl_sb and channel and that the
  487. * combination could be legal given any set of circumstances.
  488. * RETURNS: TRUE is the chanspec is malformed, false if it looks good.
  489. */
  490. bool
  491. wf_chspec_malformed(chanspec_t chanspec)
  492. {
  493. uint chspec_bw = CHSPEC_BW(chanspec);
  494. uint chspec_ch = CHSPEC_CHANNEL(chanspec);
  495. /* must be 2G or 5G band */
  496. if (CHSPEC_IS2G(chanspec)) {
  497. /* must be valid bandwidth */
  498. if (chspec_bw != WL_CHANSPEC_BW_20 &&
  499. chspec_bw != WL_CHANSPEC_BW_40) {
  500. return TRUE;
  501. }
  502. } else if (CHSPEC_IS5G(chanspec)) {
  503. if (chspec_bw == WL_CHANSPEC_BW_8080) {
  504. uint ch1_id, ch2_id;
  505. /* channel number in 80+80 must be in range */
  506. ch1_id = CHSPEC_CHAN1(chanspec);
  507. ch2_id = CHSPEC_CHAN2(chanspec);
  508. if (ch1_id >= WF_NUM_5G_80M_CHANS || ch2_id >= WF_NUM_5G_80M_CHANS)
  509. return TRUE;
  510. /* ch2 must be above ch1 for the chanspec */
  511. if (ch2_id <= ch1_id)
  512. return TRUE;
  513. } else if (chspec_bw == WL_CHANSPEC_BW_20 || chspec_bw == WL_CHANSPEC_BW_40 ||
  514. chspec_bw == WL_CHANSPEC_BW_80 || chspec_bw == WL_CHANSPEC_BW_160) {
  515. if (chspec_ch > MAXCHANNEL) {
  516. return TRUE;
  517. }
  518. } else {
  519. /* invalid bandwidth */
  520. return TRUE;
  521. }
  522. } else {
  523. /* must be 2G or 5G band */
  524. return TRUE;
  525. }
  526. /* side band needs to be consistent with bandwidth */
  527. if (chspec_bw == WL_CHANSPEC_BW_20) {
  528. if (CHSPEC_CTL_SB(chanspec) != WL_CHANSPEC_CTL_SB_LLL)
  529. return TRUE;
  530. } else if (chspec_bw == WL_CHANSPEC_BW_40) {
  531. if (CHSPEC_CTL_SB(chanspec) > WL_CHANSPEC_CTL_SB_LLU)
  532. return TRUE;
  533. } else if (chspec_bw == WL_CHANSPEC_BW_80) {
  534. if (CHSPEC_CTL_SB(chanspec) > WL_CHANSPEC_CTL_SB_LUU)
  535. return TRUE;
  536. }
  537. return FALSE;
  538. }
  539. /*
  540. * Verify the chanspec specifies a valid channel according to 802.11.
  541. * RETURNS: TRUE if the chanspec is a valid 802.11 channel
  542. */
  543. bool
  544. wf_chspec_valid(chanspec_t chanspec)
  545. {
  546. uint chspec_bw = CHSPEC_BW(chanspec);
  547. uint chspec_ch = CHSPEC_CHANNEL(chanspec);
  548. if (wf_chspec_malformed(chanspec))
  549. return FALSE;
  550. if (CHSPEC_IS2G(chanspec)) {
  551. /* must be valid bandwidth and channel range */
  552. if (chspec_bw == WL_CHANSPEC_BW_20) {
  553. if (chspec_ch >= 1 && chspec_ch <= 14)
  554. return TRUE;
  555. } else if (chspec_bw == WL_CHANSPEC_BW_40) {
  556. if (chspec_ch >= 3 && chspec_ch <= 11)
  557. return TRUE;
  558. }
  559. } else if (CHSPEC_IS5G(chanspec)) {
  560. if (chspec_bw == WL_CHANSPEC_BW_8080) {
  561. uint16 ch1, ch2;
  562. ch1 = wf_5g_80m_chans[CHSPEC_CHAN1(chanspec)];
  563. ch2 = wf_5g_80m_chans[CHSPEC_CHAN2(chanspec)];
  564. /* the two channels must be separated by more than 80MHz by VHT req,
  565. * and ch2 above ch1 for the chanspec
  566. */
  567. if (ch2 > ch1 + CH_80MHZ_APART)
  568. return TRUE;
  569. } else {
  570. const uint8 *center_ch;
  571. uint num_ch, i;
  572. if (chspec_bw == WL_CHANSPEC_BW_20 || chspec_bw == WL_CHANSPEC_BW_40) {
  573. center_ch = wf_5g_40m_chans;
  574. num_ch = WF_NUM_5G_40M_CHANS;
  575. } else if (chspec_bw == WL_CHANSPEC_BW_80) {
  576. center_ch = wf_5g_80m_chans;
  577. num_ch = WF_NUM_5G_80M_CHANS;
  578. } else if (chspec_bw == WL_CHANSPEC_BW_160) {
  579. center_ch = wf_5g_160m_chans;
  580. num_ch = WF_NUM_5G_160M_CHANS;
  581. } else {
  582. /* invalid bandwidth */
  583. return FALSE;
  584. }
  585. /* check for a valid center channel */
  586. if (chspec_bw == WL_CHANSPEC_BW_20) {
  587. /* We don't have an array of legal 20MHz 5G channels, but they are
  588. * each side of the legal 40MHz channels. Check the chanspec
  589. * channel against either side of the 40MHz channels.
  590. */
  591. for (i = 0; i < num_ch; i ++) {
  592. if (chspec_ch == (uint)LOWER_20_SB(center_ch[i]) ||
  593. chspec_ch == (uint)UPPER_20_SB(center_ch[i]))
  594. break; /* match found */
  595. }
  596. if (i == num_ch) {
  597. /* check for channel 165 which is not the side band
  598. * of 40MHz 5G channel
  599. */
  600. if (chspec_ch == 165)
  601. i = 0;
  602. /* check for legacy JP channels on failure */
  603. if (chspec_ch == 34 || chspec_ch == 38 ||
  604. chspec_ch == 42 || chspec_ch == 46)
  605. i = 0;
  606. }
  607. } else {
  608. /* check the chanspec channel to each legal channel */
  609. for (i = 0; i < num_ch; i ++) {
  610. if (chspec_ch == center_ch[i])
  611. break; /* match found */
  612. }
  613. }
  614. if (i < num_ch) {
  615. /* match found */
  616. return TRUE;
  617. }
  618. }
  619. }
  620. return FALSE;
  621. }
  622. /*
  623. * This function returns the channel number that control traffic is being sent on, for 20MHz
  624. * channels this is just the channel number, for 40MHZ, 80MHz, 160MHz channels it is the 20MHZ
  625. * sideband depending on the chanspec selected
  626. */
  627. uint8
  628. wf_chspec_ctlchan(chanspec_t chspec)
  629. {
  630. uint center_chan;
  631. uint bw_mhz;
  632. uint sb;
  633. ASSERT(!wf_chspec_malformed(chspec));
  634. /* Is there a sideband ? */
  635. if (CHSPEC_IS20(chspec)) {
  636. return CHSPEC_CHANNEL(chspec);
  637. } else {
  638. sb = CHSPEC_CTL_SB(chspec) >> WL_CHANSPEC_CTL_SB_SHIFT;
  639. if (CHSPEC_IS8080(chspec)) {
  640. bw_mhz = 80;
  641. if (sb < 4) {
  642. center_chan = CHSPEC_CHAN1(chspec);
  643. }
  644. else {
  645. center_chan = CHSPEC_CHAN2(chspec);
  646. sb -= 4;
  647. }
  648. /* convert from channel index to channel number */
  649. center_chan = wf_5g_80m_chans[center_chan];
  650. }
  651. else {
  652. bw_mhz = bw_chspec_to_mhz(chspec);
  653. center_chan = CHSPEC_CHANNEL(chspec) >> WL_CHANSPEC_CHAN_SHIFT;
  654. }
  655. return (channel_to_ctl_chan(center_chan, bw_mhz, sb));
  656. }
  657. }
  658. /* given a chanspec, return the bandwidth string */
  659. char *
  660. wf_chspec_to_bw_str(chanspec_t chspec)
  661. {
  662. return (char *)wf_chspec_bw_str[(CHSPEC_BW(chspec) >> WL_CHANSPEC_BW_SHIFT)];
  663. }
  664. /*
  665. * This function returns the chanspec of the control channel of a given chanspec
  666. */
  667. chanspec_t
  668. wf_chspec_ctlchspec(chanspec_t chspec)
  669. {
  670. chanspec_t ctl_chspec = chspec;
  671. uint8 ctl_chan;
  672. ASSERT(!wf_chspec_malformed(chspec));
  673. /* Is there a sideband ? */
  674. if (!CHSPEC_IS20(chspec)) {
  675. ctl_chan = wf_chspec_ctlchan(chspec);
  676. ctl_chspec = ctl_chan | WL_CHANSPEC_BW_20;
  677. ctl_chspec |= CHSPEC_BAND(chspec);
  678. }
  679. return ctl_chspec;
  680. }
  681. /* return chanspec given control channel and bandwidth
  682. * return 0 on error
  683. */
  684. uint16
  685. wf_channel2chspec(uint ctl_ch, uint bw)
  686. {
  687. uint16 chspec;
  688. const uint8 *center_ch = NULL;
  689. int num_ch = 0;
  690. int sb = -1;
  691. int i = 0;
  692. chspec = ((ctl_ch <= CH_MAX_2G_CHANNEL) ? WL_CHANSPEC_BAND_2G : WL_CHANSPEC_BAND_5G);
  693. chspec |= bw;
  694. if (bw == WL_CHANSPEC_BW_40) {
  695. center_ch = wf_5g_40m_chans;
  696. num_ch = WF_NUM_5G_40M_CHANS;
  697. bw = 40;
  698. } else if (bw == WL_CHANSPEC_BW_80) {
  699. center_ch = wf_5g_80m_chans;
  700. num_ch = WF_NUM_5G_80M_CHANS;
  701. bw = 80;
  702. } else if (bw == WL_CHANSPEC_BW_160) {
  703. center_ch = wf_5g_160m_chans;
  704. num_ch = WF_NUM_5G_160M_CHANS;
  705. bw = 160;
  706. } else if (bw == WL_CHANSPEC_BW_20) {
  707. chspec |= ctl_ch;
  708. return chspec;
  709. } else {
  710. return 0;
  711. }
  712. for (i = 0; i < num_ch; i ++) {
  713. sb = channel_to_sb(center_ch[i], ctl_ch, bw);
  714. if (sb >= 0) {
  715. chspec |= center_ch[i];
  716. chspec |= (sb << WL_CHANSPEC_CTL_SB_SHIFT);
  717. break;
  718. }
  719. }
  720. /* check for no matching sb/center */
  721. if (sb < 0) {
  722. return 0;
  723. }
  724. return chspec;
  725. }
  726. /*
  727. * This function returns the chanspec for the primary 40MHz of an 80MHz channel.
  728. * The control sideband specifies the same 20MHz channel that the 80MHz channel is using
  729. * as the primary 20MHz channel.
  730. */
  731. extern chanspec_t wf_chspec_primary40_chspec(chanspec_t chspec)
  732. {
  733. chanspec_t chspec40 = chspec;
  734. uint center_chan;
  735. uint sb;
  736. ASSERT(!wf_chspec_malformed(chspec));
  737. if (CHSPEC_IS80(chspec)) {
  738. center_chan = CHSPEC_CHANNEL(chspec);
  739. sb = CHSPEC_CTL_SB(chspec);
  740. if (sb == WL_CHANSPEC_CTL_SB_UL) {
  741. /* Primary 40MHz is on upper side */
  742. sb = WL_CHANSPEC_CTL_SB_L;
  743. center_chan += CH_20MHZ_APART;
  744. } else if (sb == WL_CHANSPEC_CTL_SB_UU) {
  745. /* Primary 40MHz is on upper side */
  746. sb = WL_CHANSPEC_CTL_SB_U;
  747. center_chan += CH_20MHZ_APART;
  748. } else {
  749. /* Primary 40MHz is on lower side */
  750. /* sideband bits are the same for LL/LU and L/U */
  751. center_chan -= CH_20MHZ_APART;
  752. }
  753. /* Create primary 40MHz chanspec */
  754. chspec40 = (WL_CHANSPEC_BAND_5G | WL_CHANSPEC_BW_40 |
  755. sb | center_chan);
  756. }
  757. return chspec40;
  758. }
  759. /*
  760. * Return the channel number for a given frequency and base frequency.
  761. * The returned channel number is relative to the given base frequency.
  762. * If the given base frequency is zero, a base frequency of 5 GHz is assumed for
  763. * frequencies from 5 - 6 GHz, and 2.407 GHz is assumed for 2.4 - 2.5 GHz.
  764. *
  765. * Frequency is specified in MHz.
  766. * The base frequency is specified as (start_factor * 500 kHz).
  767. * Constants WF_CHAN_FACTOR_2_4_G, WF_CHAN_FACTOR_5_G are defined for
  768. * 2.4 GHz and 5 GHz bands.
  769. *
  770. * The returned channel will be in the range [1, 14] in the 2.4 GHz band
  771. * and [0, 200] otherwise.
  772. * -1 is returned if the start_factor is WF_CHAN_FACTOR_2_4_G and the
  773. * frequency is not a 2.4 GHz channel, or if the frequency is not and even
  774. * multiple of 5 MHz from the base frequency to the base plus 1 GHz.
  775. *
  776. * Reference 802.11 REVma, section 17.3.8.3, and 802.11B section 18.4.6.2
  777. */
  778. int
  779. wf_mhz2channel(uint freq, uint start_factor)
  780. {
  781. int ch = -1;
  782. uint base;
  783. int offset;
  784. /* take the default channel start frequency */
  785. if (start_factor == 0) {
  786. if (freq >= 2400 && freq <= 2500)
  787. start_factor = WF_CHAN_FACTOR_2_4_G;
  788. else if (freq >= 5000 && freq <= 6000)
  789. start_factor = WF_CHAN_FACTOR_5_G;
  790. }
  791. if (freq == 2484 && start_factor == WF_CHAN_FACTOR_2_4_G)
  792. return 14;
  793. base = start_factor / 2;
  794. /* check that the frequency is in 1GHz range of the base */
  795. if ((freq < base) || (freq > base + 1000))
  796. return -1;
  797. offset = freq - base;
  798. ch = offset / 5;
  799. /* check that frequency is a 5MHz multiple from the base */
  800. if (offset != (ch * 5))
  801. return -1;
  802. /* restricted channel range check for 2.4G */
  803. if (start_factor == WF_CHAN_FACTOR_2_4_G && (ch < 1 || ch > 13))
  804. return -1;
  805. return ch;
  806. }
  807. /*
  808. * Return the center frequency in MHz of the given channel and base frequency.
  809. * The channel number is interpreted relative to the given base frequency.
  810. *
  811. * The valid channel range is [1, 14] in the 2.4 GHz band and [0, 200] otherwise.
  812. * The base frequency is specified as (start_factor * 500 kHz).
  813. * Constants WF_CHAN_FACTOR_2_4_G, WF_CHAN_FACTOR_4_G, and WF_CHAN_FACTOR_5_G
  814. * are defined for 2.4 GHz, 4 GHz, and 5 GHz bands.
  815. * The channel range of [1, 14] is only checked for a start_factor of
  816. * WF_CHAN_FACTOR_2_4_G (4814 = 2407 * 2).
  817. * Odd start_factors produce channels on .5 MHz boundaries, in which case
  818. * the answer is rounded down to an integral MHz.
  819. * -1 is returned for an out of range channel.
  820. *
  821. * Reference 802.11 REVma, section 17.3.8.3, and 802.11B section 18.4.6.2
  822. */
  823. int
  824. wf_channel2mhz(uint ch, uint start_factor)
  825. {
  826. int freq;
  827. if ((start_factor == WF_CHAN_FACTOR_2_4_G && (ch < 1 || ch > 14)) ||
  828. (ch > 200))
  829. freq = -1;
  830. else if ((start_factor == WF_CHAN_FACTOR_2_4_G) && (ch == 14))
  831. freq = 2484;
  832. else
  833. freq = ch * 5 + start_factor / 2;
  834. return freq;
  835. }
  836. /*
  837. * Returns the 80+80 chanspec corresponding to the following input parameters
  838. *
  839. * primary_20mhz - Primary 20 Mhz channel
  840. * chan1 - channel number of first 80 Mhz band
  841. * chan2 - channel number of second 80 Mhz band
  842. *
  843. * parameters chan1 and chan2 are channel numbers in {42, 58, 106, 122, 138, 155}
  844. *
  845. * returns INVCHANSPEC in case of error
  846. */
  847. chanspec_t
  848. wf_chspec_get8080_chspec(uint8 primary_20mhz, uint8 chan1, uint8 chan2)
  849. {
  850. int sb = 0;
  851. uint16 chanspec = 0;
  852. int chan1_id = 0, chan2_id = 0;
  853. /* does the primary channel fit with the 1st 80MHz channel ? */
  854. sb = channel_to_sb(chan1, primary_20mhz, 80);
  855. if (sb < 0) {
  856. /* no, so does the primary channel fit with the 2nd 80MHz channel ? */
  857. sb = channel_to_sb(chan2, primary_20mhz, 80);
  858. if (sb < 0) {
  859. /* no match for ctl_ch to either 80MHz center channel */
  860. return INVCHANSPEC;
  861. }
  862. /* sb index is 0-3 for the low 80MHz channel, and 4-7 for
  863. * the high 80MHz channel. Add 4 to to shift to high set.
  864. */
  865. sb += 4;
  866. }
  867. chan1_id = channel_80mhz_to_id(chan1);
  868. chan2_id = channel_80mhz_to_id(chan2);
  869. if (chan1_id == -1 || chan2_id == -1)
  870. return INVCHANSPEC;
  871. chanspec = (chan1_id << WL_CHANSPEC_CHAN1_SHIFT)|
  872. (chan2_id << WL_CHANSPEC_CHAN2_SHIFT)|
  873. (sb << WL_CHANSPEC_CTL_SB_SHIFT)|
  874. (WL_CHANSPEC_BW_8080)|
  875. (WL_CHANSPEC_BAND_5G);
  876. return chanspec;
  877. }
  878. /*
  879. * This function returns the 80Mhz channel for the given id.
  880. */
  881. static uint8
  882. wf_chspec_get80Mhz_ch(uint8 chan_80Mhz_id)
  883. {
  884. if (chan_80Mhz_id < WF_NUM_5G_80M_CHANS)
  885. return wf_5g_80m_chans[chan_80Mhz_id];
  886. return 0;
  887. }
  888. /*
  889. * Returns the primary 80 Mhz channel for the provided chanspec
  890. *
  891. * chanspec - Input chanspec for which the 80MHz primary channel has to be retrieved
  892. *
  893. * returns -1 in case the provided channel is 20/40 Mhz chanspec
  894. */
  895. uint8
  896. wf_chspec_primary80_channel(chanspec_t chanspec)
  897. {
  898. uint8 chan1 = 0, chan2 = 0, primary_20mhz = 0, primary80_chan = 0;
  899. int sb = 0;
  900. primary_20mhz = wf_chspec_ctlchan(chanspec);
  901. if (CHSPEC_IS80(chanspec)) {
  902. primary80_chan = CHSPEC_CHANNEL(chanspec);
  903. }
  904. else if (CHSPEC_IS8080(chanspec)) {
  905. chan1 = wf_chspec_get80Mhz_ch(CHSPEC_CHAN1(chanspec));
  906. chan2 = wf_chspec_get80Mhz_ch(CHSPEC_CHAN2(chanspec));
  907. /* does the primary channel fit with the 1st 80MHz channel ? */
  908. sb = channel_to_sb(chan1, primary_20mhz, 80);
  909. if (sb < 0) {
  910. /* no, so does the primary channel fit with the 2nd 80MHz channel ? */
  911. sb = channel_to_sb(chan2, primary_20mhz, 80);
  912. if (!(sb < 0)) {
  913. primary80_chan = chan2;
  914. }
  915. }
  916. else {
  917. primary80_chan = chan1;
  918. }
  919. }
  920. else if (CHSPEC_IS160(chanspec)) {
  921. chan1 = CHSPEC_CHANNEL(chanspec);
  922. sb = channel_to_sb(chan1, primary_20mhz, 160);
  923. if (!(sb < 0)) {
  924. /* based on the sb value primary 80 channel can be retrieved
  925. * if sb is in range 0 to 3 the lower band is the 80Mhz primary band
  926. */
  927. if (sb < 4) {
  928. primary80_chan = chan1 - CH_40MHZ_APART;
  929. }
  930. /* if sb is in range 4 to 7 the lower band is the 80Mhz primary band */
  931. else
  932. {
  933. primary80_chan = chan1 + CH_40MHZ_APART;
  934. }
  935. }
  936. }
  937. else {
  938. /* for 20 and 40 Mhz */
  939. primary80_chan = -1;
  940. }
  941. return primary80_chan;
  942. }
  943. /*
  944. * Returns the secondary 80 Mhz channel for the provided chanspec
  945. *
  946. * chanspec - Input chanspec for which the 80MHz secondary channel has to be retrieved
  947. *
  948. * returns -1 in case the provided channel is 20/40 Mhz chanspec
  949. */
  950. uint8
  951. wf_chspec_secondary80_channel(chanspec_t chanspec)
  952. {
  953. uint8 chan1 = 0, chan2 = 0, primary_20mhz = 0, secondary80_chan = 0;
  954. int sb = 0;
  955. primary_20mhz = wf_chspec_ctlchan(chanspec);
  956. if (CHSPEC_IS80(chanspec)) {
  957. secondary80_chan = -1;
  958. }
  959. else if (CHSPEC_IS8080(chanspec)) {
  960. chan1 = wf_chspec_get80Mhz_ch(CHSPEC_CHAN1(chanspec));
  961. chan2 = wf_chspec_get80Mhz_ch(CHSPEC_CHAN2(chanspec));
  962. /* does the primary channel fit with the 1st 80MHz channel ? */
  963. sb = channel_to_sb(chan1, primary_20mhz, 80);
  964. if (sb < 0) {
  965. /* no, so does the primary channel fit with the 2nd 80MHz channel ? */
  966. sb = channel_to_sb(chan2, primary_20mhz, 80);
  967. if (!(sb < 0)) {
  968. secondary80_chan = chan1;
  969. }
  970. }
  971. else {
  972. secondary80_chan = chan2;
  973. }
  974. }
  975. else if (CHSPEC_IS160(chanspec)) {
  976. chan1 = CHSPEC_CHANNEL(chanspec);
  977. sb = channel_to_sb(chan1, primary_20mhz, 160);
  978. if (!(sb < 0)) {
  979. /* based on the sb value secondary 80 channel can be retrieved
  980. *if sb is in range 0 to 3 upper band is the secondary 80Mhz band
  981. */
  982. if (sb < 4) {
  983. secondary80_chan = chan1 + CH_40MHZ_APART;
  984. }
  985. /* if sb is in range 4 to 7 the lower band is the secondary 80Mhz band */
  986. else
  987. {
  988. secondary80_chan = chan1 - CH_40MHZ_APART;
  989. }
  990. }
  991. }
  992. else {
  993. /* for 20 and 40 Mhz */
  994. secondary80_chan = -1;
  995. }
  996. return secondary80_chan;
  997. }
  998. /*
  999. * This function returns the chanspec for the primary 80MHz of an 160MHz or 80+80 channel.
  1000. *
  1001. * chanspec - Input chanspec for which the primary 80Mhz chanspec has to be retreived
  1002. *
  1003. * returns INVCHANSPEC in case the provided channel is 20/40 Mhz chanspec
  1004. */
  1005. chanspec_t
  1006. wf_chspec_primary80_chspec(chanspec_t chspec)
  1007. {
  1008. chanspec_t chspec80;
  1009. uint center_chan, chan1 = 0, chan2 = 0;
  1010. uint sb;
  1011. ASSERT(!wf_chspec_malformed(chspec));
  1012. if (CHSPEC_IS8080(chspec)) {
  1013. chan1 = wf_chspec_get80Mhz_ch(CHSPEC_CHAN1(chspec));
  1014. chan2 = wf_chspec_get80Mhz_ch(CHSPEC_CHAN2(chspec));
  1015. sb = CHSPEC_CTL_SB(chspec);
  1016. if (sb < 4) {
  1017. /* Primary 80MHz is on lower side */
  1018. center_chan = chan1;
  1019. }
  1020. else
  1021. {
  1022. /* Primary 80MHz is on upper side */
  1023. center_chan = chan2;
  1024. sb -= 4;
  1025. }
  1026. /* Create primary 80MHz chanspec */
  1027. chspec80 = (WL_CHANSPEC_BAND_5G | WL_CHANSPEC_BW_80 |sb | center_chan);
  1028. }
  1029. else if (CHSPEC_IS160(chspec)) {
  1030. center_chan = CHSPEC_CHANNEL(chspec);
  1031. sb = CHSPEC_CTL_SB(chspec);
  1032. if (sb < 4) {
  1033. /* Primary 80MHz is on upper side */
  1034. center_chan -= CH_40MHZ_APART;
  1035. }
  1036. else
  1037. {
  1038. /* Primary 80MHz is on lower side */
  1039. center_chan += CH_40MHZ_APART;
  1040. sb -= 4;
  1041. }
  1042. /* Create primary 80MHz chanspec */
  1043. chspec80 = (WL_CHANSPEC_BAND_5G | WL_CHANSPEC_BW_80 | sb | center_chan);
  1044. }
  1045. else
  1046. {
  1047. chspec80 = INVCHANSPEC;
  1048. }
  1049. return chspec80;
  1050. }