channel.c 20 KB

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