channel.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/types.h>
  17. #include <net/cfg80211.h>
  18. #include <net/mac80211.h>
  19. #include <net/regulatory.h>
  20. #include <defs.h>
  21. #include "pub.h"
  22. #include "phy/phy_hal.h"
  23. #include "main.h"
  24. #include "stf.h"
  25. #include "channel.h"
  26. #include "mac80211_if.h"
  27. /* QDB() macro takes a dB value and converts to a quarter dB value */
  28. #define QDB(n) ((n) * BRCMS_TXPWR_DB_FACTOR)
  29. #define LOCALE_MIMO_IDX_bn 0
  30. #define LOCALE_MIMO_IDX_11n 0
  31. /* max of BAND_5G_PWR_LVLS and 14 for 2.4 GHz */
  32. #define BRCMS_MAXPWR_MIMO_TBL_SIZE 14
  33. /* maxpwr mapping to 5GHz band channels:
  34. * maxpwr[0] - channels [34-48]
  35. * maxpwr[1] - channels [52-60]
  36. * maxpwr[2] - channels [62-64]
  37. * maxpwr[3] - channels [100-140]
  38. * maxpwr[4] - channels [149-165]
  39. */
  40. #define BAND_5G_PWR_LVLS 5 /* 5 power levels for 5G */
  41. #define LC(id) LOCALE_MIMO_IDX_ ## id
  42. #define LOCALES(mimo2, mimo5) \
  43. {LC(mimo2), LC(mimo5)}
  44. /* macro to get 5 GHz channel group index for tx power */
  45. #define CHANNEL_POWER_IDX_5G(c) (((c) < 52) ? 0 : \
  46. (((c) < 62) ? 1 : \
  47. (((c) < 100) ? 2 : \
  48. (((c) < 149) ? 3 : 4))))
  49. #define BRCM_2GHZ_2412_2462 REG_RULE(2412-10, 2462+10, 40, 0, 19, 0)
  50. #define BRCM_2GHZ_2467_2472 REG_RULE(2467-10, 2472+10, 20, 0, 19, \
  51. NL80211_RRF_PASSIVE_SCAN | \
  52. NL80211_RRF_NO_IBSS)
  53. #define BRCM_5GHZ_5180_5240 REG_RULE(5180-10, 5240+10, 40, 0, 21, \
  54. NL80211_RRF_PASSIVE_SCAN | \
  55. NL80211_RRF_NO_IBSS)
  56. #define BRCM_5GHZ_5260_5320 REG_RULE(5260-10, 5320+10, 40, 0, 21, \
  57. NL80211_RRF_PASSIVE_SCAN | \
  58. NL80211_RRF_DFS | \
  59. NL80211_RRF_NO_IBSS)
  60. #define BRCM_5GHZ_5500_5700 REG_RULE(5500-10, 5700+10, 40, 0, 21, \
  61. NL80211_RRF_PASSIVE_SCAN | \
  62. NL80211_RRF_DFS | \
  63. NL80211_RRF_NO_IBSS)
  64. #define BRCM_5GHZ_5745_5825 REG_RULE(5745-10, 5825+10, 40, 0, 21, \
  65. NL80211_RRF_PASSIVE_SCAN | \
  66. NL80211_RRF_NO_IBSS)
  67. static const struct ieee80211_regdomain brcms_regdom_x2 = {
  68. .n_reg_rules = 7,
  69. .alpha2 = "X2",
  70. .reg_rules = {
  71. BRCM_2GHZ_2412_2462,
  72. BRCM_2GHZ_2467_2472,
  73. BRCM_5GHZ_5180_5240,
  74. BRCM_5GHZ_5260_5320,
  75. BRCM_5GHZ_5500_5700,
  76. BRCM_5GHZ_5745_5825,
  77. }
  78. };
  79. /* locale per-channel tx power limits for MIMO frames
  80. * maxpwr arrays are index by channel for 2.4 GHz limits, and
  81. * by sub-band for 5 GHz limits using CHANNEL_POWER_IDX_5G(channel)
  82. */
  83. struct locale_mimo_info {
  84. /* tx 20 MHz power limits, qdBm units */
  85. s8 maxpwr20[BRCMS_MAXPWR_MIMO_TBL_SIZE];
  86. /* tx 40 MHz power limits, qdBm units */
  87. s8 maxpwr40[BRCMS_MAXPWR_MIMO_TBL_SIZE];
  88. };
  89. /* Country names and abbreviations with locale defined from ISO 3166 */
  90. struct country_info {
  91. const u8 locale_mimo_2G; /* 2.4G mimo info */
  92. const u8 locale_mimo_5G; /* 5G mimo info */
  93. };
  94. struct brcms_regd {
  95. struct country_info country;
  96. const struct ieee80211_regdomain *regdomain;
  97. };
  98. struct brcms_cm_info {
  99. struct brcms_pub *pub;
  100. struct brcms_c_info *wlc;
  101. const struct brcms_regd *world_regd;
  102. };
  103. /*
  104. * MIMO Locale Definitions - 2.4 GHz
  105. */
  106. static const struct locale_mimo_info locale_bn = {
  107. {QDB(13), QDB(13), QDB(13), QDB(13), QDB(13),
  108. QDB(13), QDB(13), QDB(13), QDB(13), QDB(13),
  109. QDB(13), QDB(13), QDB(13)},
  110. {0, 0, QDB(13), QDB(13), QDB(13),
  111. QDB(13), QDB(13), QDB(13), QDB(13), QDB(13),
  112. QDB(13), 0, 0},
  113. };
  114. static const struct locale_mimo_info *g_mimo_2g_table[] = {
  115. &locale_bn
  116. };
  117. /*
  118. * MIMO Locale Definitions - 5 GHz
  119. */
  120. static const struct locale_mimo_info locale_11n = {
  121. { /* 12.5 dBm */ 50, 50, 50, QDB(15), QDB(15)},
  122. {QDB(14), QDB(15), QDB(15), QDB(15), QDB(15)},
  123. };
  124. static const struct locale_mimo_info *g_mimo_5g_table[] = {
  125. &locale_11n
  126. };
  127. static const struct brcms_regd cntry_locales[] = {
  128. /* Worldwide RoW 2, must always be at index 0 */
  129. {
  130. .country = LOCALES(bn, 11n),
  131. .regdomain = &brcms_regdom_x2,
  132. },
  133. };
  134. static const struct locale_mimo_info *brcms_c_get_mimo_2g(u8 locale_idx)
  135. {
  136. if (locale_idx >= ARRAY_SIZE(g_mimo_2g_table))
  137. return NULL;
  138. return g_mimo_2g_table[locale_idx];
  139. }
  140. static const struct locale_mimo_info *brcms_c_get_mimo_5g(u8 locale_idx)
  141. {
  142. if (locale_idx >= ARRAY_SIZE(g_mimo_5g_table))
  143. return NULL;
  144. return g_mimo_5g_table[locale_idx];
  145. }
  146. /*
  147. * Indicates whether the country provided is valid to pass
  148. * to cfg80211 or not.
  149. *
  150. * returns true if valid; false if not.
  151. */
  152. static bool brcms_c_country_valid(const char *ccode)
  153. {
  154. /*
  155. * only allow ascii alpha uppercase for the first 2
  156. * chars.
  157. */
  158. if (!((0x80 & ccode[0]) == 0 && ccode[0] >= 0x41 && ccode[0] <= 0x5A &&
  159. (0x80 & ccode[1]) == 0 && ccode[1] >= 0x41 && ccode[1] <= 0x5A &&
  160. ccode[2] == '\0'))
  161. return false;
  162. /*
  163. * do not match ISO 3166-1 user assigned country codes
  164. * that may be in the driver table
  165. */
  166. if (!strcmp("AA", ccode) || /* AA */
  167. !strcmp("ZZ", ccode) || /* ZZ */
  168. ccode[0] == 'X' || /* XA - XZ */
  169. (ccode[0] == 'Q' && /* QM - QZ */
  170. (ccode[1] >= 'M' && ccode[1] <= 'Z')))
  171. return false;
  172. if (!strcmp("NA", ccode))
  173. return false;
  174. return true;
  175. }
  176. static const struct brcms_regd *brcms_world_regd(const char *regdom, int len)
  177. {
  178. const struct brcms_regd *regd = NULL;
  179. int i;
  180. for (i = 0; i < ARRAY_SIZE(cntry_locales); i++) {
  181. if (!strncmp(regdom, cntry_locales[i].regdomain->alpha2, len)) {
  182. regd = &cntry_locales[i];
  183. break;
  184. }
  185. }
  186. return regd;
  187. }
  188. static const struct brcms_regd *brcms_default_world_regd(void)
  189. {
  190. return &cntry_locales[0];
  191. }
  192. /* JP, J1 - J10 are Japan ccodes */
  193. static bool brcms_c_japan_ccode(const char *ccode)
  194. {
  195. return (ccode[0] == 'J' &&
  196. (ccode[1] == 'P' || (ccode[1] >= '1' && ccode[1] <= '9')));
  197. }
  198. static void
  199. brcms_c_channel_min_txpower_limits_with_local_constraint(
  200. struct brcms_cm_info *wlc_cm, struct txpwr_limits *txpwr,
  201. u8 local_constraint_qdbm)
  202. {
  203. int j;
  204. /* CCK Rates */
  205. for (j = 0; j < WL_TX_POWER_CCK_NUM; j++)
  206. txpwr->cck[j] = min(txpwr->cck[j], local_constraint_qdbm);
  207. /* 20 MHz Legacy OFDM SISO */
  208. for (j = 0; j < WL_TX_POWER_OFDM_NUM; j++)
  209. txpwr->ofdm[j] = min(txpwr->ofdm[j], local_constraint_qdbm);
  210. /* 20 MHz Legacy OFDM CDD */
  211. for (j = 0; j < BRCMS_NUM_RATES_OFDM; j++)
  212. txpwr->ofdm_cdd[j] =
  213. min(txpwr->ofdm_cdd[j], local_constraint_qdbm);
  214. /* 40 MHz Legacy OFDM SISO */
  215. for (j = 0; j < BRCMS_NUM_RATES_OFDM; j++)
  216. txpwr->ofdm_40_siso[j] =
  217. min(txpwr->ofdm_40_siso[j], local_constraint_qdbm);
  218. /* 40 MHz Legacy OFDM CDD */
  219. for (j = 0; j < BRCMS_NUM_RATES_OFDM; j++)
  220. txpwr->ofdm_40_cdd[j] =
  221. min(txpwr->ofdm_40_cdd[j], local_constraint_qdbm);
  222. /* 20MHz MCS 0-7 SISO */
  223. for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
  224. txpwr->mcs_20_siso[j] =
  225. min(txpwr->mcs_20_siso[j], local_constraint_qdbm);
  226. /* 20MHz MCS 0-7 CDD */
  227. for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
  228. txpwr->mcs_20_cdd[j] =
  229. min(txpwr->mcs_20_cdd[j], local_constraint_qdbm);
  230. /* 20MHz MCS 0-7 STBC */
  231. for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
  232. txpwr->mcs_20_stbc[j] =
  233. min(txpwr->mcs_20_stbc[j], local_constraint_qdbm);
  234. /* 20MHz MCS 8-15 MIMO */
  235. for (j = 0; j < BRCMS_NUM_RATES_MCS_2_STREAM; j++)
  236. txpwr->mcs_20_mimo[j] =
  237. min(txpwr->mcs_20_mimo[j], local_constraint_qdbm);
  238. /* 40MHz MCS 0-7 SISO */
  239. for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
  240. txpwr->mcs_40_siso[j] =
  241. min(txpwr->mcs_40_siso[j], local_constraint_qdbm);
  242. /* 40MHz MCS 0-7 CDD */
  243. for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
  244. txpwr->mcs_40_cdd[j] =
  245. min(txpwr->mcs_40_cdd[j], local_constraint_qdbm);
  246. /* 40MHz MCS 0-7 STBC */
  247. for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
  248. txpwr->mcs_40_stbc[j] =
  249. min(txpwr->mcs_40_stbc[j], local_constraint_qdbm);
  250. /* 40MHz MCS 8-15 MIMO */
  251. for (j = 0; j < BRCMS_NUM_RATES_MCS_2_STREAM; j++)
  252. txpwr->mcs_40_mimo[j] =
  253. min(txpwr->mcs_40_mimo[j], local_constraint_qdbm);
  254. /* 40MHz MCS 32 */
  255. txpwr->mcs32 = min(txpwr->mcs32, local_constraint_qdbm);
  256. }
  257. /*
  258. * set the driver's current country and regulatory information
  259. * using a country code as the source. Look up built in country
  260. * information found with the country code.
  261. */
  262. static void
  263. brcms_c_set_country(struct brcms_cm_info *wlc_cm,
  264. const struct brcms_regd *regd)
  265. {
  266. struct brcms_c_info *wlc = wlc_cm->wlc;
  267. if ((wlc->pub->_n_enab & SUPPORT_11N) !=
  268. wlc->protection->nmode_user)
  269. brcms_c_set_nmode(wlc);
  270. brcms_c_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
  271. brcms_c_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
  272. brcms_c_set_gmode(wlc, wlc->protection->gmode_user, false);
  273. return;
  274. }
  275. struct brcms_cm_info *brcms_c_channel_mgr_attach(struct brcms_c_info *wlc)
  276. {
  277. struct brcms_cm_info *wlc_cm;
  278. struct brcms_pub *pub = wlc->pub;
  279. struct ssb_sprom *sprom = &wlc->hw->d11core->bus->sprom;
  280. const char *ccode = sprom->alpha2;
  281. int ccode_len = sizeof(sprom->alpha2);
  282. BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
  283. wlc_cm = kzalloc(sizeof(struct brcms_cm_info), GFP_ATOMIC);
  284. if (wlc_cm == NULL)
  285. return NULL;
  286. wlc_cm->pub = pub;
  287. wlc_cm->wlc = wlc;
  288. wlc->cmi = wlc_cm;
  289. /* store the country code for passing up as a regulatory hint */
  290. wlc_cm->world_regd = brcms_world_regd(ccode, ccode_len);
  291. if (brcms_c_country_valid(ccode))
  292. strncpy(wlc->pub->srom_ccode, ccode, ccode_len);
  293. /*
  294. * If no custom world domain is found in the SROM, use the
  295. * default "X2" domain.
  296. */
  297. if (!wlc_cm->world_regd) {
  298. wlc_cm->world_regd = brcms_default_world_regd();
  299. ccode = wlc_cm->world_regd->regdomain->alpha2;
  300. ccode_len = BRCM_CNTRY_BUF_SZ - 1;
  301. }
  302. /* save default country for exiting 11d regulatory mode */
  303. strncpy(wlc->country_default, ccode, ccode_len);
  304. /* initialize autocountry_default to driver default */
  305. strncpy(wlc->autocountry_default, ccode, ccode_len);
  306. brcms_c_set_country(wlc_cm, wlc_cm->world_regd);
  307. return wlc_cm;
  308. }
  309. void brcms_c_channel_mgr_detach(struct brcms_cm_info *wlc_cm)
  310. {
  311. kfree(wlc_cm);
  312. }
  313. void
  314. brcms_c_channel_set_chanspec(struct brcms_cm_info *wlc_cm, u16 chanspec,
  315. u8 local_constraint_qdbm)
  316. {
  317. struct brcms_c_info *wlc = wlc_cm->wlc;
  318. struct ieee80211_channel *ch = wlc->pub->ieee_hw->conf.channel;
  319. const struct ieee80211_reg_rule *reg_rule;
  320. struct txpwr_limits txpwr;
  321. int ret;
  322. brcms_c_channel_reg_limits(wlc_cm, chanspec, &txpwr);
  323. brcms_c_channel_min_txpower_limits_with_local_constraint(
  324. wlc_cm, &txpwr, local_constraint_qdbm
  325. );
  326. /* set or restore gmode as required by regulatory */
  327. ret = freq_reg_info(wlc->wiphy, ch->center_freq, 0, &reg_rule);
  328. if (!ret && (reg_rule->flags & NL80211_RRF_NO_OFDM))
  329. brcms_c_set_gmode(wlc, GMODE_LEGACY_B, false);
  330. else
  331. brcms_c_set_gmode(wlc, wlc->protection->gmode_user, false);
  332. brcms_b_set_chanspec(wlc->hw, chanspec,
  333. !!(ch->flags & IEEE80211_CHAN_PASSIVE_SCAN),
  334. &txpwr);
  335. }
  336. void
  337. brcms_c_channel_reg_limits(struct brcms_cm_info *wlc_cm, u16 chanspec,
  338. struct txpwr_limits *txpwr)
  339. {
  340. struct brcms_c_info *wlc = wlc_cm->wlc;
  341. struct ieee80211_channel *ch = wlc->pub->ieee_hw->conf.channel;
  342. uint i;
  343. uint chan;
  344. int maxpwr;
  345. int delta;
  346. const struct country_info *country;
  347. struct brcms_band *band;
  348. int conducted_max = BRCMS_TXPWR_MAX;
  349. const struct locale_mimo_info *li_mimo;
  350. int maxpwr20, maxpwr40;
  351. int maxpwr_idx;
  352. uint j;
  353. memset(txpwr, 0, sizeof(struct txpwr_limits));
  354. if (WARN_ON(!ch))
  355. return;
  356. country = &wlc_cm->world_regd->country;
  357. chan = CHSPEC_CHANNEL(chanspec);
  358. band = wlc->bandstate[chspec_bandunit(chanspec)];
  359. li_mimo = (band->bandtype == BRCM_BAND_5G) ?
  360. brcms_c_get_mimo_5g(country->locale_mimo_5G) :
  361. brcms_c_get_mimo_2g(country->locale_mimo_2G);
  362. delta = band->antgain;
  363. if (band->bandtype == BRCM_BAND_2G)
  364. conducted_max = QDB(22);
  365. maxpwr = QDB(ch->max_power) - delta;
  366. maxpwr = max(maxpwr, 0);
  367. maxpwr = min(maxpwr, conducted_max);
  368. /* CCK txpwr limits for 2.4G band */
  369. if (band->bandtype == BRCM_BAND_2G) {
  370. for (i = 0; i < BRCMS_NUM_RATES_CCK; i++)
  371. txpwr->cck[i] = (u8) maxpwr;
  372. }
  373. for (i = 0; i < BRCMS_NUM_RATES_OFDM; i++) {
  374. txpwr->ofdm[i] = (u8) maxpwr;
  375. /*
  376. * OFDM 40 MHz SISO has the same power as the corresponding
  377. * MCS0-7 rate unless overriden by the locale specific code.
  378. * We set this value to 0 as a flag (presumably 0 dBm isn't
  379. * a possibility) and then copy the MCS0-7 value to the 40 MHz
  380. * value if it wasn't explicitly set.
  381. */
  382. txpwr->ofdm_40_siso[i] = 0;
  383. txpwr->ofdm_cdd[i] = (u8) maxpwr;
  384. txpwr->ofdm_40_cdd[i] = 0;
  385. }
  386. delta = 0;
  387. if (band->antgain > QDB(6))
  388. delta = band->antgain - QDB(6); /* Excess over 6 dB */
  389. if (band->bandtype == BRCM_BAND_2G)
  390. maxpwr_idx = (chan - 1);
  391. else
  392. maxpwr_idx = CHANNEL_POWER_IDX_5G(chan);
  393. maxpwr20 = li_mimo->maxpwr20[maxpwr_idx];
  394. maxpwr40 = li_mimo->maxpwr40[maxpwr_idx];
  395. maxpwr20 = maxpwr20 - delta;
  396. maxpwr20 = max(maxpwr20, 0);
  397. maxpwr40 = maxpwr40 - delta;
  398. maxpwr40 = max(maxpwr40, 0);
  399. /* Fill in the MCS 0-7 (SISO) rates */
  400. for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
  401. /*
  402. * 20 MHz has the same power as the corresponding OFDM rate
  403. * unless overriden by the locale specific code.
  404. */
  405. txpwr->mcs_20_siso[i] = txpwr->ofdm[i];
  406. txpwr->mcs_40_siso[i] = 0;
  407. }
  408. /* Fill in the MCS 0-7 CDD rates */
  409. for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
  410. txpwr->mcs_20_cdd[i] = (u8) maxpwr20;
  411. txpwr->mcs_40_cdd[i] = (u8) maxpwr40;
  412. }
  413. /*
  414. * These locales have SISO expressed in the
  415. * table and override CDD later
  416. */
  417. if (li_mimo == &locale_bn) {
  418. if (li_mimo == &locale_bn) {
  419. maxpwr20 = QDB(16);
  420. maxpwr40 = 0;
  421. if (chan >= 3 && chan <= 11)
  422. maxpwr40 = QDB(16);
  423. }
  424. for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
  425. txpwr->mcs_20_siso[i] = (u8) maxpwr20;
  426. txpwr->mcs_40_siso[i] = (u8) maxpwr40;
  427. }
  428. }
  429. /* Fill in the MCS 0-7 STBC rates */
  430. for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
  431. txpwr->mcs_20_stbc[i] = 0;
  432. txpwr->mcs_40_stbc[i] = 0;
  433. }
  434. /* Fill in the MCS 8-15 SDM rates */
  435. for (i = 0; i < BRCMS_NUM_RATES_MCS_2_STREAM; i++) {
  436. txpwr->mcs_20_mimo[i] = (u8) maxpwr20;
  437. txpwr->mcs_40_mimo[i] = (u8) maxpwr40;
  438. }
  439. /* Fill in MCS32 */
  440. txpwr->mcs32 = (u8) maxpwr40;
  441. for (i = 0, j = 0; i < BRCMS_NUM_RATES_OFDM; i++, j++) {
  442. if (txpwr->ofdm_40_cdd[i] == 0)
  443. txpwr->ofdm_40_cdd[i] = txpwr->mcs_40_cdd[j];
  444. if (i == 0) {
  445. i = i + 1;
  446. if (txpwr->ofdm_40_cdd[i] == 0)
  447. txpwr->ofdm_40_cdd[i] = txpwr->mcs_40_cdd[j];
  448. }
  449. }
  450. /*
  451. * Copy the 40 MHZ MCS 0-7 CDD value to the 40 MHZ MCS 0-7 SISO
  452. * value if it wasn't provided explicitly.
  453. */
  454. for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
  455. if (txpwr->mcs_40_siso[i] == 0)
  456. txpwr->mcs_40_siso[i] = txpwr->mcs_40_cdd[i];
  457. }
  458. for (i = 0, j = 0; i < BRCMS_NUM_RATES_OFDM; i++, j++) {
  459. if (txpwr->ofdm_40_siso[i] == 0)
  460. txpwr->ofdm_40_siso[i] = txpwr->mcs_40_siso[j];
  461. if (i == 0) {
  462. i = i + 1;
  463. if (txpwr->ofdm_40_siso[i] == 0)
  464. txpwr->ofdm_40_siso[i] = txpwr->mcs_40_siso[j];
  465. }
  466. }
  467. /*
  468. * Copy the 20 and 40 MHz MCS0-7 CDD values to the corresponding
  469. * STBC values if they weren't provided explicitly.
  470. */
  471. for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
  472. if (txpwr->mcs_20_stbc[i] == 0)
  473. txpwr->mcs_20_stbc[i] = txpwr->mcs_20_cdd[i];
  474. if (txpwr->mcs_40_stbc[i] == 0)
  475. txpwr->mcs_40_stbc[i] = txpwr->mcs_40_cdd[i];
  476. }
  477. return;
  478. }
  479. /*
  480. * Verify the chanspec is using a legal set of parameters, i.e. that the
  481. * chanspec specified a band, bw, ctl_sb and channel and that the
  482. * combination could be legal given any set of circumstances.
  483. * RETURNS: true is the chanspec is malformed, false if it looks good.
  484. */
  485. static bool brcms_c_chspec_malformed(u16 chanspec)
  486. {
  487. /* must be 2G or 5G band */
  488. if (!CHSPEC_IS5G(chanspec) && !CHSPEC_IS2G(chanspec))
  489. return true;
  490. /* must be 20 or 40 bandwidth */
  491. if (!CHSPEC_IS40(chanspec) && !CHSPEC_IS20(chanspec))
  492. return true;
  493. /* 20MHZ b/w must have no ctl sb, 40 must have a ctl sb */
  494. if (CHSPEC_IS20(chanspec)) {
  495. if (!CHSPEC_SB_NONE(chanspec))
  496. return true;
  497. } else if (!CHSPEC_SB_UPPER(chanspec) && !CHSPEC_SB_LOWER(chanspec)) {
  498. return true;
  499. }
  500. return false;
  501. }
  502. /*
  503. * Validate the chanspec for this locale, for 40MHZ we need to also
  504. * check that the sidebands are valid 20MZH channels in this locale
  505. * and they are also a legal HT combination
  506. */
  507. static bool
  508. brcms_c_valid_chanspec_ext(struct brcms_cm_info *wlc_cm, u16 chspec)
  509. {
  510. struct brcms_c_info *wlc = wlc_cm->wlc;
  511. u8 channel = CHSPEC_CHANNEL(chspec);
  512. /* check the chanspec */
  513. if (brcms_c_chspec_malformed(chspec)) {
  514. wiphy_err(wlc->wiphy, "wl%d: malformed chanspec 0x%x\n",
  515. wlc->pub->unit, chspec);
  516. return false;
  517. }
  518. if (CHANNEL_BANDUNIT(wlc_cm->wlc, channel) !=
  519. chspec_bandunit(chspec))
  520. return false;
  521. return true;
  522. }
  523. bool brcms_c_valid_chanspec_db(struct brcms_cm_info *wlc_cm, u16 chspec)
  524. {
  525. return brcms_c_valid_chanspec_ext(wlc_cm, chspec);
  526. }
  527. static bool brcms_is_radar_freq(u16 center_freq)
  528. {
  529. return center_freq >= 5260 && center_freq <= 5700;
  530. }
  531. static void brcms_reg_apply_radar_flags(struct wiphy *wiphy)
  532. {
  533. struct ieee80211_supported_band *sband;
  534. struct ieee80211_channel *ch;
  535. int i;
  536. sband = wiphy->bands[IEEE80211_BAND_5GHZ];
  537. if (!sband)
  538. return;
  539. for (i = 0; i < sband->n_channels; i++) {
  540. ch = &sband->channels[i];
  541. if (!brcms_is_radar_freq(ch->center_freq))
  542. continue;
  543. /*
  544. * All channels in this range should be passive and have
  545. * DFS enabled.
  546. */
  547. if (!(ch->flags & IEEE80211_CHAN_DISABLED))
  548. ch->flags |= IEEE80211_CHAN_RADAR |
  549. IEEE80211_CHAN_NO_IBSS |
  550. IEEE80211_CHAN_PASSIVE_SCAN;
  551. }
  552. }
  553. static void
  554. brcms_reg_apply_beaconing_flags(struct wiphy *wiphy,
  555. enum nl80211_reg_initiator initiator)
  556. {
  557. struct ieee80211_supported_band *sband;
  558. struct ieee80211_channel *ch;
  559. const struct ieee80211_reg_rule *rule;
  560. int band, i, ret;
  561. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  562. sband = wiphy->bands[band];
  563. if (!sband)
  564. continue;
  565. for (i = 0; i < sband->n_channels; i++) {
  566. ch = &sband->channels[i];
  567. if (ch->flags &
  568. (IEEE80211_CHAN_DISABLED | IEEE80211_CHAN_RADAR))
  569. continue;
  570. if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  571. ret = freq_reg_info(wiphy, ch->center_freq,
  572. 0, &rule);
  573. if (ret)
  574. continue;
  575. if (!(rule->flags & NL80211_RRF_NO_IBSS))
  576. ch->flags &= ~IEEE80211_CHAN_NO_IBSS;
  577. if (!(rule->flags & NL80211_RRF_PASSIVE_SCAN))
  578. ch->flags &=
  579. ~IEEE80211_CHAN_PASSIVE_SCAN;
  580. } else if (ch->beacon_found) {
  581. ch->flags &= ~(IEEE80211_CHAN_NO_IBSS |
  582. IEEE80211_CHAN_PASSIVE_SCAN);
  583. }
  584. }
  585. }
  586. }
  587. static int brcms_reg_notifier(struct wiphy *wiphy,
  588. struct regulatory_request *request)
  589. {
  590. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  591. struct brcms_info *wl = hw->priv;
  592. struct brcms_c_info *wlc = wl->wlc;
  593. struct ieee80211_supported_band *sband;
  594. struct ieee80211_channel *ch;
  595. int band, i;
  596. bool ch_found = false;
  597. brcms_reg_apply_radar_flags(wiphy);
  598. if (request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
  599. brcms_reg_apply_beaconing_flags(wiphy, request->initiator);
  600. /* Disable radio if all channels disallowed by regulatory */
  601. for (band = 0; !ch_found && band < IEEE80211_NUM_BANDS; band++) {
  602. sband = wiphy->bands[band];
  603. if (!sband)
  604. continue;
  605. for (i = 0; !ch_found && i < sband->n_channels; i++) {
  606. ch = &sband->channels[i];
  607. if (!(ch->flags & IEEE80211_CHAN_DISABLED))
  608. ch_found = true;
  609. }
  610. }
  611. if (ch_found) {
  612. mboolclr(wlc->pub->radio_disabled, WL_RADIO_COUNTRY_DISABLE);
  613. } else {
  614. mboolset(wlc->pub->radio_disabled, WL_RADIO_COUNTRY_DISABLE);
  615. wiphy_err(wlc->wiphy, "wl%d: %s: no valid channel for \"%s\"\n",
  616. wlc->pub->unit, __func__, request->alpha2);
  617. }
  618. if (wlc->pub->_nbands > 1 || wlc->band->bandtype == BRCM_BAND_2G)
  619. wlc_phy_chanspec_ch14_widefilter_set(wlc->band->pi,
  620. brcms_c_japan_ccode(request->alpha2));
  621. return 0;
  622. }
  623. void brcms_c_regd_init(struct brcms_c_info *wlc)
  624. {
  625. struct wiphy *wiphy = wlc->wiphy;
  626. const struct brcms_regd *regd = wlc->cmi->world_regd;
  627. struct ieee80211_supported_band *sband;
  628. struct ieee80211_channel *ch;
  629. struct brcms_chanvec sup_chan;
  630. struct brcms_band *band;
  631. int band_idx, i;
  632. /* Disable any channels not supported by the phy */
  633. for (band_idx = 0; band_idx < wlc->pub->_nbands; band_idx++) {
  634. band = wlc->bandstate[band_idx];
  635. wlc_phy_chanspec_band_validch(band->pi, band->bandtype,
  636. &sup_chan);
  637. if (band_idx == BAND_2G_INDEX)
  638. sband = wiphy->bands[IEEE80211_BAND_2GHZ];
  639. else
  640. sband = wiphy->bands[IEEE80211_BAND_5GHZ];
  641. for (i = 0; i < sband->n_channels; i++) {
  642. ch = &sband->channels[i];
  643. if (!isset(sup_chan.vec, ch->hw_value))
  644. ch->flags |= IEEE80211_CHAN_DISABLED;
  645. }
  646. }
  647. wlc->wiphy->reg_notifier = brcms_reg_notifier;
  648. wlc->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
  649. WIPHY_FLAG_STRICT_REGULATORY;
  650. wiphy_apply_custom_regulatory(wlc->wiphy, regd->regdomain);
  651. brcms_reg_apply_beaconing_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER);
  652. }