staging: brcm80211: fix 'uninitialized usage' compiler warning
[pandora-kernel.git] / drivers / staging / brcm80211 / brcmsmac / channel.c
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
17 #include <linux/types.h>
18 #include <net/mac80211.h>
19
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
27 #define VALID_CHANNEL20_DB(wlc, val) brcms_c_valid_channel20_db((wlc)->cmi, val)
28 #define VALID_CHANNEL20_IN_BAND(wlc, bandunit, val) \
29         brcms_c_valid_channel20_in_band((wlc)->cmi, bandunit, val)
30 #define VALID_CHANNEL20(wlc, val) brcms_c_valid_channel20((wlc)->cmi, val)
31
32 struct brcms_cm_band {
33         /* struct locale_info flags */
34         u8 locale_flags;
35         /* List of valid channels in the country */
36         struct brcms_chanvec valid_channels;
37         /* List of restricted use channels */
38         const struct brcms_chanvec *restricted_channels;
39         /* List of radar sensitive channels */
40         const struct brcms_chanvec *radar_channels;
41         u8 PAD[8];
42 };
43
44 struct brcms_cm_info {
45         struct brcms_pub *pub;
46         struct brcms_c_info *wlc;
47         char srom_ccode[BRCM_CNTRY_BUF_SZ];     /* Country Code in SROM */
48         uint srom_regrev;       /* Regulatory Rev for the SROM ccode */
49         const struct country_info *country;     /* current country def */
50         char ccode[BRCM_CNTRY_BUF_SZ];  /* current internal Country Code */
51         uint regrev;            /* current Regulatory Revision */
52         char country_abbrev[BRCM_CNTRY_BUF_SZ]; /* current advertised ccode */
53         /* per-band state (one per phy/radio) */
54         struct brcms_cm_band bandstate[MAXBANDS];
55         /* quiet channels currently for radar sensitivity or 11h support */
56         /* channels on which we cannot transmit */
57         struct brcms_chanvec quiet_channels;
58 };
59
60 static int brcms_c_channels_init(struct brcms_cm_info *wlc_cm,
61                              const struct country_info *country);
62 static void brcms_c_set_country_common(struct brcms_cm_info *wlc_cm,
63                                    const char *country_abbrev,
64                                    const char *ccode, uint regrev,
65                                    const struct country_info *country);
66 static int brcms_c_set_countrycode(struct brcms_cm_info *wlc_cm,
67                                    const char *ccode);
68 static int brcms_c_set_countrycode_rev(struct brcms_cm_info *wlc_cm,
69                                    const char *country_abbrev,
70                                    const char *ccode, int regrev);
71 static int brcms_c_country_aggregate_map(struct brcms_cm_info *wlc_cm,
72                                 const char *ccode,
73                                 char *mapped_ccode, uint *mapped_regrev);
74
75 static const struct country_info *
76 brcms_c_country_lookup_direct(const char *ccode, uint regrev);
77
78 static const struct country_info *
79 brcms_c_countrycode_map(struct brcms_cm_info *wlc_cm,
80                         const char *ccode, char *mapped_ccode,
81                         uint *mapped_regrev);
82
83 static void brcms_c_channels_commit(struct brcms_cm_info *wlc_cm);
84 static void brcms_c_quiet_channels_reset(struct brcms_cm_info *wlc_cm);
85 static bool brcms_c_quiet_chanspec(struct brcms_cm_info *wlc_cm,
86                                    u16 chspec);
87 static bool brcms_c_valid_channel20_db(struct brcms_cm_info *wlc_cm, uint val);
88 static bool brcms_c_valid_channel20_in_band(struct brcms_cm_info *wlc_cm,
89                                             uint bandunit, uint val);
90 static bool brcms_c_valid_channel20(struct brcms_cm_info *wlc_cm, uint val);
91
92 static const struct country_info *
93 brcms_c_country_lookup(struct brcms_c_info *wlc, const char *ccode);
94
95 static void brcms_c_locale_get_channels(const struct locale_info *locale,
96                                     struct brcms_chanvec *valid_channels);
97 static const struct locale_info *brcms_c_get_locale_2g(u8 locale_idx);
98 static const struct locale_info *brcms_c_get_locale_5g(u8 locale_idx);
99 static bool brcms_c_japan(struct brcms_c_info *wlc);
100 static bool brcms_c_japan_ccode(const char *ccode);
101 static void brcms_c_channel_min_txpower_limits_with_local_constraint(
102         struct brcms_cm_info *wlc_cm, struct txpwr_limits *txpwr,
103         u8 local_constraint_qdbm);
104 static void brcms_c_locale_add_channels(struct brcms_chanvec *target,
105                                     const struct brcms_chanvec *channels);
106 static const struct locale_mimo_info *brcms_c_get_mimo_2g(u8 locale_idx);
107 static const struct locale_mimo_info *brcms_c_get_mimo_5g(u8 locale_idx);
108
109 /* QDB() macro takes a dB value and converts to a quarter dB value */
110 #ifdef QDB
111 #undef QDB
112 #endif
113 #define QDB(n) ((n) * BRCMS_TXPWR_DB_FACTOR)
114
115 /* Regulatory Matrix Spreadsheet (CLM) MIMO v3.7.9 */
116
117 /*
118  * Some common channel sets
119  */
120
121 /* No channels */
122 static const struct brcms_chanvec chanvec_none = {
123         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
124          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
125          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
126          0x00, 0x00, 0x00, 0x00}
127 };
128
129 /* All 2.4 GHz HW channels */
130 const struct brcms_chanvec chanvec_all_2G = {
131         {0xfe, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
132          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
133          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
134          0x00, 0x00, 0x00, 0x00}
135 };
136
137 /* All 5 GHz HW channels */
138 const struct brcms_chanvec chanvec_all_5G = {
139         {0x00, 0x00, 0x00, 0x00, 0x54, 0x55, 0x11, 0x11,
140          0x01, 0x00, 0x00, 0x00, 0x10, 0x11, 0x11, 0x11,
141          0x11, 0x11, 0x20, 0x22, 0x22, 0x00, 0x00, 0x11,
142          0x11, 0x11, 0x11, 0x01}
143 };
144
145 /*
146  * Radar channel sets
147  */
148
149 /* No radar */
150 #define radar_set_none chanvec_none
151
152 /* Channels 52 - 64, 100 - 140 */
153 static const struct brcms_chanvec radar_set1 = {
154         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11,  /* 52 - 60 */
155          0x01, 0x00, 0x00, 0x00, 0x10, 0x11, 0x11, 0x11,  /* 64, 100 - 124 */
156          0x11, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 128 - 140 */
157          0x00, 0x00, 0x00, 0x00}
158 };
159
160 /*
161  * Restricted channel sets
162  */
163
164 #define restricted_set_none chanvec_none
165
166 /* Channels 34, 38, 42, 46 */
167 static const struct brcms_chanvec restricted_set_japan_legacy = {
168         {0x00, 0x00, 0x00, 0x00, 0x44, 0x44, 0x00, 0x00,
169          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
170          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
171          0x00, 0x00, 0x00, 0x00}
172 };
173
174 /* Channels 12, 13 */
175 static const struct brcms_chanvec restricted_set_2g_short = {
176         {0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
177          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
178          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
179          0x00, 0x00, 0x00, 0x00}
180 };
181
182 /* Channel 165 */
183 static const struct brcms_chanvec restricted_chan_165 = {
184         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
185          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
186          0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
187          0x00, 0x00, 0x00, 0x00}
188 };
189
190 /* Channels 36 - 48 & 149 - 165 */
191 static const struct brcms_chanvec restricted_low_hi = {
192         {0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x01, 0x00,
193          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
194          0x00, 0x00, 0x20, 0x22, 0x22, 0x00, 0x00, 0x00,
195          0x00, 0x00, 0x00, 0x00}
196 };
197
198 /* Channels 12 - 14 */
199 static const struct brcms_chanvec restricted_set_12_13_14 = {
200         {0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
201          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
202          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
203          0x00, 0x00, 0x00, 0x00}
204 };
205
206 #define  LOCALE_CHAN_01_11       (1<<0)
207 #define  LOCALE_CHAN_12_13       (1<<1)
208 #define  LOCALE_CHAN_14          (1<<2)
209 #define  LOCALE_SET_5G_LOW_JP1   (1<<3) /* 34-48, step 2 */
210 #define  LOCALE_SET_5G_LOW_JP2   (1<<4) /* 34-46, step 4 */
211 #define  LOCALE_SET_5G_LOW1      (1<<5) /* 36-48, step 4 */
212 #define  LOCALE_SET_5G_LOW2      (1<<6) /* 52 */
213 #define  LOCALE_SET_5G_LOW3      (1<<7) /* 56-64, step 4 */
214 #define  LOCALE_SET_5G_MID1      (1<<8) /* 100-116, step 4 */
215 #define  LOCALE_SET_5G_MID2      (1<<9) /* 120-124, step 4 */
216 #define  LOCALE_SET_5G_MID3      (1<<10)        /* 128 */
217 #define  LOCALE_SET_5G_HIGH1     (1<<11)        /* 132-140, step 4 */
218 #define  LOCALE_SET_5G_HIGH2     (1<<12)        /* 149-161, step 4 */
219 #define  LOCALE_SET_5G_HIGH3     (1<<13)        /* 165 */
220 #define  LOCALE_CHAN_52_140_ALL  (1<<14)
221 #define  LOCALE_SET_5G_HIGH4     (1<<15)        /* 184-216 */
222
223 #define  LOCALE_CHAN_36_64      (LOCALE_SET_5G_LOW1 | \
224                                  LOCALE_SET_5G_LOW2 | \
225                                  LOCALE_SET_5G_LOW3)
226 #define  LOCALE_CHAN_52_64      (LOCALE_SET_5G_LOW2 | LOCALE_SET_5G_LOW3)
227 #define  LOCALE_CHAN_100_124    (LOCALE_SET_5G_MID1 | LOCALE_SET_5G_MID2)
228 #define  LOCALE_CHAN_100_140    (LOCALE_SET_5G_MID1 | LOCALE_SET_5G_MID2 | \
229                                   LOCALE_SET_5G_MID3 | LOCALE_SET_5G_HIGH1)
230 #define  LOCALE_CHAN_149_165    (LOCALE_SET_5G_HIGH2 | LOCALE_SET_5G_HIGH3)
231 #define  LOCALE_CHAN_184_216    LOCALE_SET_5G_HIGH4
232
233 #define  LOCALE_CHAN_01_14      (LOCALE_CHAN_01_11 | \
234                                  LOCALE_CHAN_12_13 | \
235                                  LOCALE_CHAN_14)
236
237 #define  LOCALE_RADAR_SET_NONE            0
238 #define  LOCALE_RADAR_SET_1               1
239
240 #define  LOCALE_RESTRICTED_NONE           0
241 #define  LOCALE_RESTRICTED_SET_2G_SHORT   1
242 #define  LOCALE_RESTRICTED_CHAN_165       2
243 #define  LOCALE_CHAN_ALL_5G               3
244 #define  LOCALE_RESTRICTED_JAPAN_LEGACY   4
245 #define  LOCALE_RESTRICTED_11D_2G         5
246 #define  LOCALE_RESTRICTED_11D_5G         6
247 #define  LOCALE_RESTRICTED_LOW_HI         7
248 #define  LOCALE_RESTRICTED_12_13_14       8
249
250 /* global memory to provide working buffer for expanded locale */
251
252 static const struct brcms_chanvec *g_table_radar_set[] = {
253         &chanvec_none,
254         &radar_set1
255 };
256
257 static const struct brcms_chanvec *g_table_restricted_chan[] = {
258         &chanvec_none,          /* restricted_set_none */
259         &restricted_set_2g_short,
260         &restricted_chan_165,
261         &chanvec_all_5G,
262         &restricted_set_japan_legacy,
263         &chanvec_all_2G,        /* restricted_set_11d_2G */
264         &chanvec_all_5G,        /* restricted_set_11d_5G */
265         &restricted_low_hi,
266         &restricted_set_12_13_14
267 };
268
269 static const struct brcms_chanvec locale_2g_01_11 = {
270         {0xfe, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
271          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
272          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
273          0x00, 0x00, 0x00, 0x00}
274 };
275
276 static const struct brcms_chanvec locale_2g_12_13 = {
277         {0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
278          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
279          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
280          0x00, 0x00, 0x00, 0x00}
281 };
282
283 static const struct brcms_chanvec locale_2g_14 = {
284         {0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
285          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
286          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
287          0x00, 0x00, 0x00, 0x00}
288 };
289
290 static const struct brcms_chanvec locale_5g_LOW_JP1 = {
291         {0x00, 0x00, 0x00, 0x00, 0x54, 0x55, 0x01, 0x00,
292          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
293          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
294          0x00, 0x00, 0x00, 0x00}
295 };
296
297 static const struct brcms_chanvec locale_5g_LOW_JP2 = {
298         {0x00, 0x00, 0x00, 0x00, 0x44, 0x44, 0x00, 0x00,
299          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
300          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
301          0x00, 0x00, 0x00, 0x00}
302 };
303
304 static const struct brcms_chanvec locale_5g_LOW1 = {
305         {0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x01, 0x00,
306          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
307          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
308          0x00, 0x00, 0x00, 0x00}
309 };
310
311 static const struct brcms_chanvec locale_5g_LOW2 = {
312         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
313          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
314          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
315          0x00, 0x00, 0x00, 0x00}
316 };
317
318 static const struct brcms_chanvec locale_5g_LOW3 = {
319         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11,
320          0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
321          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
322          0x00, 0x00, 0x00, 0x00}
323 };
324
325 static const struct brcms_chanvec locale_5g_MID1 = {
326         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
327          0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x11, 0x00,
328          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
329          0x00, 0x00, 0x00, 0x00}
330 };
331
332 static const struct brcms_chanvec locale_5g_MID2 = {
333         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
334          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11,
335          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
336          0x00, 0x00, 0x00, 0x00}
337 };
338
339 static const struct brcms_chanvec locale_5g_MID3 = {
340         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
341          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
342          0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
343          0x00, 0x00, 0x00, 0x00}
344 };
345
346 static const struct brcms_chanvec locale_5g_HIGH1 = {
347         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
348          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
349          0x10, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
350          0x00, 0x00, 0x00, 0x00}
351 };
352
353 static const struct brcms_chanvec locale_5g_HIGH2 = {
354         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
355          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
356          0x00, 0x00, 0x20, 0x22, 0x02, 0x00, 0x00, 0x00,
357          0x00, 0x00, 0x00, 0x00}
358 };
359
360 static const struct brcms_chanvec locale_5g_HIGH3 = {
361         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
362          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
363          0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
364          0x00, 0x00, 0x00, 0x00}
365 };
366
367 static const struct brcms_chanvec locale_5g_52_140_ALL = {
368         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11,
369          0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
370          0x11, 0x11, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
371          0x00, 0x00, 0x00, 0x00}
372 };
373
374 static const struct brcms_chanvec locale_5g_HIGH4 = {
375         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
376          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
377          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11,
378          0x11, 0x11, 0x11, 0x11}
379 };
380
381 static const struct brcms_chanvec *g_table_locale_base[] = {
382         &locale_2g_01_11,
383         &locale_2g_12_13,
384         &locale_2g_14,
385         &locale_5g_LOW_JP1,
386         &locale_5g_LOW_JP2,
387         &locale_5g_LOW1,
388         &locale_5g_LOW2,
389         &locale_5g_LOW3,
390         &locale_5g_MID1,
391         &locale_5g_MID2,
392         &locale_5g_MID3,
393         &locale_5g_HIGH1,
394         &locale_5g_HIGH2,
395         &locale_5g_HIGH3,
396         &locale_5g_52_140_ALL,
397         &locale_5g_HIGH4
398 };
399
400 static void brcms_c_locale_add_channels(struct brcms_chanvec *target,
401                                     const struct brcms_chanvec *channels)
402 {
403         u8 i;
404         for (i = 0; i < sizeof(struct brcms_chanvec); i++)
405                 target->vec[i] |= channels->vec[i];
406 }
407
408 static void brcms_c_locale_get_channels(const struct locale_info *locale,
409                                     struct brcms_chanvec *channels)
410 {
411         u8 i;
412
413         memset(channels, 0, sizeof(struct brcms_chanvec));
414
415         for (i = 0; i < ARRAY_SIZE(g_table_locale_base); i++) {
416                 if (locale->valid_channels & (1 << i))
417                         brcms_c_locale_add_channels(channels,
418                                                 g_table_locale_base[i]);
419         }
420 }
421
422 /*
423  * Locale Definitions - 2.4 GHz
424  */
425 static const struct locale_info locale_i = {    /* locale i. channel 1 - 13 */
426         LOCALE_CHAN_01_11 | LOCALE_CHAN_12_13,
427         LOCALE_RADAR_SET_NONE,
428         LOCALE_RESTRICTED_SET_2G_SHORT,
429         {QDB(19), QDB(19), QDB(19),
430          QDB(19), QDB(19), QDB(19)},
431         {20, 20, 20, 0},
432         BRCMS_EIRP
433 };
434
435 /*
436  * Locale Definitions - 5 GHz
437  */
438 static const struct locale_info locale_11 = {
439         /* locale 11. channel 36 - 48, 52 - 64, 100 - 140, 149 - 165 */
440         LOCALE_CHAN_36_64 | LOCALE_CHAN_100_140 | LOCALE_CHAN_149_165,
441         LOCALE_RADAR_SET_1,
442         LOCALE_RESTRICTED_NONE,
443         {QDB(21), QDB(21), QDB(21), QDB(21), QDB(21)},
444         {23, 23, 23, 30, 30},
445         BRCMS_EIRP | BRCMS_DFS_EU
446 };
447
448 #define LOCALE_2G_IDX_i                 0
449 static const struct locale_info *g_locale_2g_table[] = {
450         &locale_i
451 };
452
453 #define LOCALE_5G_IDX_11        0
454 static const struct locale_info *g_locale_5g_table[] = {
455         &locale_11
456 };
457
458 /*
459  * MIMO Locale Definitions - 2.4 GHz
460  */
461 static const struct locale_mimo_info locale_bn = {
462         {QDB(13), QDB(13), QDB(13), QDB(13), QDB(13),
463          QDB(13), QDB(13), QDB(13), QDB(13), QDB(13),
464          QDB(13), QDB(13), QDB(13)},
465         {0, 0, QDB(13), QDB(13), QDB(13),
466          QDB(13), QDB(13), QDB(13), QDB(13), QDB(13),
467          QDB(13), 0, 0},
468         0
469 };
470
471 /* locale mimo 2g indexes */
472 #define LOCALE_MIMO_IDX_bn                      0
473
474 static const struct locale_mimo_info *g_mimo_2g_table[] = {
475         &locale_bn
476 };
477
478 /*
479  * MIMO Locale Definitions - 5 GHz
480  */
481 static const struct locale_mimo_info locale_11n = {
482         { /* 12.5 dBm */ 50, 50, 50, QDB(15), QDB(15)},
483         {QDB(14), QDB(15), QDB(15), QDB(15), QDB(15)},
484         0
485 };
486
487 #define LOCALE_MIMO_IDX_11n                     0
488 static const struct locale_mimo_info *g_mimo_5g_table[] = {
489         &locale_11n
490 };
491
492 #ifdef LC
493 #undef LC
494 #endif
495 #define LC(id)  LOCALE_MIMO_IDX_ ## id
496
497 #ifdef LC_2G
498 #undef LC_2G
499 #endif
500 #define LC_2G(id)       LOCALE_2G_IDX_ ## id
501
502 #ifdef LC_5G
503 #undef LC_5G
504 #endif
505 #define LC_5G(id)       LOCALE_5G_IDX_ ## id
506
507 #define LOCALES(band2, band5, mimo2, mimo5) \
508                 {LC_2G(band2), LC_5G(band5), LC(mimo2), LC(mimo5)}
509
510 static const struct {
511         char abbrev[BRCM_CNTRY_BUF_SZ]; /* country abbreviation */
512         struct country_info country;
513 } cntry_locales[] = {
514         {
515         "X2", LOCALES(i, 11, bn, 11n)}, /* Worldwide RoW 2 */
516 };
517
518 #ifdef SUPPORT_40MHZ
519 /* 20MHz channel info for 40MHz pairing support */
520 struct chan20_info {
521         u8 sb;
522         u8 adj_sbs;
523 };
524
525 /* indicates adjacent channels that are allowed for a 40 Mhz channel and
526  * those that permitted by the HT
527  */
528 struct chan20_info chan20_info[] = {
529         /* 11b/11g */
530 /* 0 */ {1, (CH_UPPER_SB | CH_EWA_VALID)},
531 /* 1 */ {2, (CH_UPPER_SB | CH_EWA_VALID)},
532 /* 2 */ {3, (CH_UPPER_SB | CH_EWA_VALID)},
533 /* 3 */ {4, (CH_UPPER_SB | CH_EWA_VALID)},
534 /* 4 */ {5, (CH_UPPER_SB | CH_LOWER_SB | CH_EWA_VALID)},
535 /* 5 */ {6, (CH_UPPER_SB | CH_LOWER_SB | CH_EWA_VALID)},
536 /* 6 */ {7, (CH_UPPER_SB | CH_LOWER_SB | CH_EWA_VALID)},
537 /* 7 */ {8, (CH_UPPER_SB | CH_LOWER_SB | CH_EWA_VALID)},
538 /* 8 */ {9, (CH_UPPER_SB | CH_LOWER_SB | CH_EWA_VALID)},
539 /* 9 */ {10, (CH_LOWER_SB | CH_EWA_VALID)},
540 /* 10 */ {11, (CH_LOWER_SB | CH_EWA_VALID)},
541 /* 11 */ {12, (CH_LOWER_SB)},
542 /* 12 */ {13, (CH_LOWER_SB)},
543 /* 13 */ {14, (CH_LOWER_SB)},
544
545 /* 11a japan high */
546 /* 14 */ {34, (CH_UPPER_SB)},
547 /* 15 */ {38, (CH_LOWER_SB)},
548 /* 16 */ {42, (CH_LOWER_SB)},
549 /* 17 */ {46, (CH_LOWER_SB)},
550
551 /* 11a usa low */
552 /* 18 */ {36, (CH_UPPER_SB | CH_EWA_VALID)},
553 /* 19 */ {40, (CH_LOWER_SB | CH_EWA_VALID)},
554 /* 20 */ {44, (CH_UPPER_SB | CH_EWA_VALID)},
555 /* 21 */ {48, (CH_LOWER_SB | CH_EWA_VALID)},
556 /* 22 */ {52, (CH_UPPER_SB | CH_EWA_VALID)},
557 /* 23 */ {56, (CH_LOWER_SB | CH_EWA_VALID)},
558 /* 24 */ {60, (CH_UPPER_SB | CH_EWA_VALID)},
559 /* 25 */ {64, (CH_LOWER_SB | CH_EWA_VALID)},
560
561 /* 11a Europe */
562 /* 26 */ {100, (CH_UPPER_SB | CH_EWA_VALID)},
563 /* 27 */ {104, (CH_LOWER_SB | CH_EWA_VALID)},
564 /* 28 */ {108, (CH_UPPER_SB | CH_EWA_VALID)},
565 /* 29 */ {112, (CH_LOWER_SB | CH_EWA_VALID)},
566 /* 30 */ {116, (CH_UPPER_SB | CH_EWA_VALID)},
567 /* 31 */ {120, (CH_LOWER_SB | CH_EWA_VALID)},
568 /* 32 */ {124, (CH_UPPER_SB | CH_EWA_VALID)},
569 /* 33 */ {128, (CH_LOWER_SB | CH_EWA_VALID)},
570 /* 34 */ {132, (CH_UPPER_SB | CH_EWA_VALID)},
571 /* 35 */ {136, (CH_LOWER_SB | CH_EWA_VALID)},
572 /* 36 */ {140, (CH_LOWER_SB)},
573
574 /* 11a usa high, ref5 only */
575 /* The 0x80 bit in pdiv means these are REF5, other entries are REF20 */
576 /* 37 */ {149, (CH_UPPER_SB | CH_EWA_VALID)},
577 /* 38 */ {153, (CH_LOWER_SB | CH_EWA_VALID)},
578 /* 39 */ {157, (CH_UPPER_SB | CH_EWA_VALID)},
579 /* 40 */ {161, (CH_LOWER_SB | CH_EWA_VALID)},
580 /* 41 */ {165, (CH_LOWER_SB)},
581
582 /* 11a japan */
583 /* 42 */ {184, (CH_UPPER_SB)},
584 /* 43 */ {188, (CH_LOWER_SB)},
585 /* 44 */ {192, (CH_UPPER_SB)},
586 /* 45 */ {196, (CH_LOWER_SB)},
587 /* 46 */ {200, (CH_UPPER_SB)},
588 /* 47 */ {204, (CH_LOWER_SB)},
589 /* 48 */ {208, (CH_UPPER_SB)},
590 /* 49 */ {212, (CH_LOWER_SB)},
591 /* 50 */ {216, (CH_LOWER_SB)}
592 };
593 #endif                          /* SUPPORT_40MHZ */
594
595 static const struct locale_info *brcms_c_get_locale_2g(u8 locale_idx)
596 {
597         if (locale_idx >= ARRAY_SIZE(g_locale_2g_table))
598                 return NULL; /* error condition */
599
600         return g_locale_2g_table[locale_idx];
601 }
602
603 static const struct locale_info *brcms_c_get_locale_5g(u8 locale_idx)
604 {
605         if (locale_idx >= ARRAY_SIZE(g_locale_5g_table))
606                 return NULL; /* error condition */
607
608         return g_locale_5g_table[locale_idx];
609 }
610
611 static const struct locale_mimo_info *brcms_c_get_mimo_2g(u8 locale_idx)
612 {
613         if (locale_idx >= ARRAY_SIZE(g_mimo_2g_table))
614                 return NULL;
615
616         return g_mimo_2g_table[locale_idx];
617 }
618
619 static const struct locale_mimo_info *brcms_c_get_mimo_5g(u8 locale_idx)
620 {
621         if (locale_idx >= ARRAY_SIZE(g_mimo_5g_table))
622                 return NULL;
623
624         return g_mimo_5g_table[locale_idx];
625 }
626
627 struct brcms_cm_info *brcms_c_channel_mgr_attach(struct brcms_c_info *wlc)
628 {
629         struct brcms_cm_info *wlc_cm;
630         char country_abbrev[BRCM_CNTRY_BUF_SZ];
631         const struct country_info *country;
632         struct brcms_pub *pub = wlc->pub;
633         char *ccode;
634
635         BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
636
637         wlc_cm = kzalloc(sizeof(struct brcms_cm_info), GFP_ATOMIC);
638         if (wlc_cm == NULL) {
639                 wiphy_err(wlc->wiphy, "wl%d: %s: out of memory", pub->unit,
640                           __func__);
641                 return NULL;
642         }
643         wlc_cm->pub = pub;
644         wlc_cm->wlc = wlc;
645         wlc->cmi = wlc_cm;
646
647         /* store the country code for passing up as a regulatory hint */
648         ccode = getvar(wlc->pub->vars, "ccode");
649         if (ccode)
650                 strncpy(wlc->pub->srom_ccode, ccode, BRCM_CNTRY_BUF_SZ - 1);
651
652         /*
653          * internal country information which must match
654          * regulatory constraints in firmware
655          */
656         memset(country_abbrev, 0, BRCM_CNTRY_BUF_SZ);
657         strncpy(country_abbrev, "X2", sizeof(country_abbrev) - 1);
658         country = brcms_c_country_lookup(wlc, country_abbrev);
659
660         /* save default country for exiting 11d regulatory mode */
661         strncpy(wlc->country_default, country_abbrev, BRCM_CNTRY_BUF_SZ - 1);
662
663         /* initialize autocountry_default to driver default */
664         strncpy(wlc->autocountry_default, "X2", BRCM_CNTRY_BUF_SZ - 1);
665
666         brcms_c_set_countrycode(wlc_cm, country_abbrev);
667
668         return wlc_cm;
669 }
670
671 void brcms_c_channel_mgr_detach(struct brcms_cm_info *wlc_cm)
672 {
673         kfree(wlc_cm);
674 }
675
676 u8
677 brcms_c_channel_locale_flags_in_band(struct brcms_cm_info *wlc_cm,
678                                      uint bandunit)
679 {
680         return wlc_cm->bandstate[bandunit].locale_flags;
681 }
682
683 /*
684  * set the driver's current country and regulatory information using
685  * a country code as the source. Lookup built in country information
686  * found with the country code.
687  */
688 static int
689 brcms_c_set_countrycode(struct brcms_cm_info *wlc_cm, const char *ccode)
690 {
691         char country_abbrev[BRCM_CNTRY_BUF_SZ];
692         strncpy(country_abbrev, ccode, BRCM_CNTRY_BUF_SZ);
693         return brcms_c_set_countrycode_rev(wlc_cm, country_abbrev, ccode, -1);
694 }
695
696 static int
697 brcms_c_set_countrycode_rev(struct brcms_cm_info *wlc_cm,
698                         const char *country_abbrev,
699                         const char *ccode, int regrev)
700 {
701         const struct country_info *country;
702         char mapped_ccode[BRCM_CNTRY_BUF_SZ];
703         uint mapped_regrev;
704
705         /* if regrev is -1, lookup the mapped country code,
706          * otherwise use the ccode and regrev directly
707          */
708         if (regrev == -1) {
709                 /*
710                  * map the country code to a built-in country
711                  * code, regrev, and country_info
712                  */
713                 country =
714                     brcms_c_countrycode_map(wlc_cm, ccode, mapped_ccode,
715                                         &mapped_regrev);
716         } else {
717                 /* find the matching built-in country definition */
718                 country = brcms_c_country_lookup_direct(ccode, regrev);
719                 strncpy(mapped_ccode, ccode, BRCM_CNTRY_BUF_SZ);
720                 mapped_regrev = regrev;
721         }
722
723         if (country == NULL)
724                 return -EINVAL;
725
726         /* set the driver state for the country */
727         brcms_c_set_country_common(wlc_cm, country_abbrev, mapped_ccode,
728                                mapped_regrev, country);
729
730         return 0;
731 }
732
733 /*
734  * set the driver's current country and regulatory information
735  * using a country code as the source. Look up built in country
736  * information found with the country code.
737  */
738 static void
739 brcms_c_set_country_common(struct brcms_cm_info *wlc_cm,
740                        const char *country_abbrev,
741                        const char *ccode, uint regrev,
742                        const struct country_info *country)
743 {
744         const struct locale_mimo_info *li_mimo;
745         const struct locale_info *locale;
746         struct brcms_c_info *wlc = wlc_cm->wlc;
747         char prev_country_abbrev[BRCM_CNTRY_BUF_SZ];
748
749         /* save current country state */
750         wlc_cm->country = country;
751
752         memset(&prev_country_abbrev, 0, BRCM_CNTRY_BUF_SZ);
753         strncpy(prev_country_abbrev, wlc_cm->country_abbrev,
754                 BRCM_CNTRY_BUF_SZ - 1);
755
756         strncpy(wlc_cm->country_abbrev, country_abbrev, BRCM_CNTRY_BUF_SZ - 1);
757         strncpy(wlc_cm->ccode, ccode, BRCM_CNTRY_BUF_SZ - 1);
758         wlc_cm->regrev = regrev;
759
760         /* disable/restore nmode based on country regulations */
761         li_mimo = brcms_c_get_mimo_2g(country->locale_mimo_2G);
762         if (li_mimo && (li_mimo->flags & BRCMS_NO_MIMO)) {
763                 brcms_c_set_nmode(wlc, OFF);
764                 wlc->stf->no_cddstbc = true;
765         } else {
766                 wlc->stf->no_cddstbc = false;
767                 if (N_ENAB(wlc->pub) != wlc->protection->nmode_user)
768                         brcms_c_set_nmode(wlc, wlc->protection->nmode_user);
769         }
770
771         brcms_c_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
772         brcms_c_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
773         /* set or restore gmode as required by regulatory */
774         locale = brcms_c_get_locale_2g(country->locale_2G);
775         if (locale && (locale->flags & BRCMS_NO_OFDM))
776                 brcms_c_set_gmode(wlc, GMODE_LEGACY_B, false);
777         else
778                 brcms_c_set_gmode(wlc, wlc->protection->gmode_user, false);
779
780         brcms_c_channels_init(wlc_cm, country);
781
782         return;
783 }
784
785 /* Lookup a country info structure from a null terminated country code
786  * The lookup is case sensitive.
787  */
788 static const struct country_info *
789 brcms_c_country_lookup(struct brcms_c_info *wlc, const char *ccode)
790 {
791         const struct country_info *country;
792         char mapped_ccode[BRCM_CNTRY_BUF_SZ];
793         uint mapped_regrev;
794
795         /*
796          * map the country code to a built-in country code, regrev, and
797          * country_info struct
798          */
799         country = brcms_c_countrycode_map(wlc->cmi, ccode, mapped_ccode,
800                                           &mapped_regrev);
801
802         return country;
803 }
804
805 static const struct country_info *
806 brcms_c_countrycode_map(struct brcms_cm_info *wlc_cm, const char *ccode,
807                         char *mapped_ccode, uint *mapped_regrev)
808 {
809         struct brcms_c_info *wlc = wlc_cm->wlc;
810         const struct country_info *country;
811         uint srom_regrev = wlc_cm->srom_regrev;
812         const char *srom_ccode = wlc_cm->srom_ccode;
813         int mapped;
814
815         /* check for currently supported ccode size */
816         if (strlen(ccode) > (BRCM_CNTRY_BUF_SZ - 1)) {
817                 wiphy_err(wlc->wiphy, "wl%d: %s: ccode \"%s\" too long for "
818                           "match\n", wlc->pub->unit, __func__, ccode);
819                 return NULL;
820         }
821
822         /* default mapping is the given ccode and regrev 0 */
823         strncpy(mapped_ccode, ccode, BRCM_CNTRY_BUF_SZ);
824         *mapped_regrev = 0;
825
826         /* If the desired country code matches the srom country code,
827          * then the mapped country is the srom regulatory rev.
828          * Otherwise look for an aggregate mapping.
829          */
830         if (!strcmp(srom_ccode, ccode)) {
831                 *mapped_regrev = srom_regrev;
832                 mapped = 0;
833                 wiphy_err(wlc->wiphy, "srom_code == ccode %s\n", __func__);
834         } else {
835                 mapped =
836                     brcms_c_country_aggregate_map(wlc_cm, ccode, mapped_ccode,
837                                               mapped_regrev);
838         }
839
840         /* find the matching built-in country definition */
841         country = brcms_c_country_lookup_direct(mapped_ccode, *mapped_regrev);
842
843         /* if there is not an exact rev match, default to rev zero */
844         if (country == NULL && *mapped_regrev != 0) {
845                 *mapped_regrev = 0;
846                 country =
847                     brcms_c_country_lookup_direct(mapped_ccode, *mapped_regrev);
848         }
849
850         return country;
851 }
852
853 static int
854 brcms_c_country_aggregate_map(struct brcms_cm_info *wlc_cm, const char *ccode,
855                           char *mapped_ccode, uint *mapped_regrev)
856 {
857         return false;
858 }
859
860 /* Lookup a country info structure from a null terminated country
861  * abbreviation and regrev directly with no translation.
862  */
863 static const struct country_info *
864 brcms_c_country_lookup_direct(const char *ccode, uint regrev)
865 {
866         uint size, i;
867
868         /* Should just return 0 for single locale driver. */
869         /* Keep it this way in case we add more locales. (for now anyway) */
870
871         /*
872          * all other country def arrays are for regrev == 0, so if
873          * regrev is non-zero, fail
874          */
875         if (regrev > 0)
876                 return NULL;
877
878         /* find matched table entry from country code */
879         size = ARRAY_SIZE(cntry_locales);
880         for (i = 0; i < size; i++) {
881                 if (strcmp(ccode, cntry_locales[i].abbrev) == 0)
882                         return &cntry_locales[i].country;
883         }
884         return NULL;
885 }
886
887 static int
888 brcms_c_channels_init(struct brcms_cm_info *wlc_cm,
889                       const struct country_info *country)
890 {
891         struct brcms_c_info *wlc = wlc_cm->wlc;
892         uint i, j;
893         struct brcms_band *band;
894         const struct locale_info *li;
895         struct brcms_chanvec sup_chan;
896         const struct locale_mimo_info *li_mimo;
897
898         band = wlc->band;
899         for (i = 0; i < NBANDS(wlc);
900              i++, band = wlc->bandstate[OTHERBANDUNIT(wlc)]) {
901
902                 li = BAND_5G(band->bandtype) ?
903                     brcms_c_get_locale_5g(country->locale_5G) :
904                     brcms_c_get_locale_2g(country->locale_2G);
905                 wlc_cm->bandstate[band->bandunit].locale_flags = li->flags;
906                 li_mimo = BAND_5G(band->bandtype) ?
907                     brcms_c_get_mimo_5g(country->locale_mimo_5G) :
908                     brcms_c_get_mimo_2g(country->locale_mimo_2G);
909
910                 /* merge the mimo non-mimo locale flags */
911                 wlc_cm->bandstate[band->bandunit].locale_flags |=
912                     li_mimo->flags;
913
914                 wlc_cm->bandstate[band->bandunit].restricted_channels =
915                     g_table_restricted_chan[li->restricted_channels];
916                 wlc_cm->bandstate[band->bandunit].radar_channels =
917                     g_table_radar_set[li->radar_channels];
918
919                 /*
920                  * set the channel availability, masking out the channels
921                  * that may not be supported on this phy.
922                  */
923                 wlc_phy_chanspec_band_validch(band->pi, band->bandtype,
924                                               &sup_chan);
925                 brcms_c_locale_get_channels(li,
926                                         &wlc_cm->bandstate[band->bandunit].
927                                         valid_channels);
928                 for (j = 0; j < sizeof(struct brcms_chanvec); j++)
929                         wlc_cm->bandstate[band->bandunit].valid_channels.
930                             vec[j] &= sup_chan.vec[j];
931         }
932
933         brcms_c_quiet_channels_reset(wlc_cm);
934         brcms_c_channels_commit(wlc_cm);
935
936         return 0;
937 }
938
939 /* Update the radio state (enable/disable) and tx power targets
940  * based on a new set of channel/regulatory information
941  */
942 static void brcms_c_channels_commit(struct brcms_cm_info *wlc_cm)
943 {
944         struct brcms_c_info *wlc = wlc_cm->wlc;
945         uint chan;
946         struct txpwr_limits txpwr;
947
948         /* search for the existence of any valid channel */
949         for (chan = 0; chan < MAXCHANNEL; chan++) {
950                 if (VALID_CHANNEL20_DB(wlc, chan))
951                         break;
952         }
953         if (chan == MAXCHANNEL)
954                 chan = INVCHANNEL;
955
956         /*
957          * based on the channel search above, set or
958          * clear WL_RADIO_COUNTRY_DISABLE.
959          */
960         if (chan == INVCHANNEL) {
961                 /*
962                  * country/locale with no valid channels, set
963                  * the radio disable bit
964                  */
965                 mboolset(wlc->pub->radio_disabled, WL_RADIO_COUNTRY_DISABLE);
966                 wiphy_err(wlc->wiphy, "wl%d: %s: no valid channel for \"%s\" "
967                           "nbands %d bandlocked %d\n", wlc->pub->unit,
968                           __func__, wlc_cm->country_abbrev, NBANDS(wlc),
969                           wlc->bandlocked);
970         } else if (mboolisset(wlc->pub->radio_disabled,
971                               WL_RADIO_COUNTRY_DISABLE)) {
972                 /*
973                  * country/locale with valid channel, clear
974                  * the radio disable bit
975                  */
976                 mboolclr(wlc->pub->radio_disabled, WL_RADIO_COUNTRY_DISABLE);
977         }
978
979         /*
980          * Now that the country abbreviation is set, if the radio supports 2G,
981          * then set channel 14 restrictions based on the new locale.
982          */
983         if (NBANDS(wlc) > 1 || BAND_2G(wlc->band->bandtype))
984                 wlc_phy_chanspec_ch14_widefilter_set(wlc->band->pi,
985                                                      brcms_c_japan(wlc) ? true :
986                                                      false);
987
988         if (wlc->pub->up && chan != INVCHANNEL) {
989                 brcms_c_channel_reg_limits(wlc_cm, wlc->chanspec, &txpwr);
990                 brcms_c_channel_min_txpower_limits_with_local_constraint(wlc_cm,
991                         &txpwr, BRCMS_TXPWR_MAX);
992                 wlc_phy_txpower_limit_set(wlc->band->pi, &txpwr, wlc->chanspec);
993         }
994 }
995
996 /*
997  * reset the quiet channels vector to the union
998  * of the restricted and radar channel sets
999  */
1000 static void brcms_c_quiet_channels_reset(struct brcms_cm_info *wlc_cm)
1001 {
1002         struct brcms_c_info *wlc = wlc_cm->wlc;
1003         uint i, j;
1004         struct brcms_band *band;
1005         const struct brcms_chanvec *chanvec;
1006
1007         memset(&wlc_cm->quiet_channels, 0, sizeof(struct brcms_chanvec));
1008
1009         band = wlc->band;
1010         for (i = 0; i < NBANDS(wlc);
1011              i++, band = wlc->bandstate[OTHERBANDUNIT(wlc)]) {
1012
1013                 /* initialize quiet channels for restricted channels */
1014                 chanvec = wlc_cm->bandstate[band->bandunit].restricted_channels;
1015                 for (j = 0; j < sizeof(struct brcms_chanvec); j++)
1016                         wlc_cm->quiet_channels.vec[j] |= chanvec->vec[j];
1017
1018         }
1019 }
1020
1021 static bool
1022 brcms_c_quiet_chanspec(struct brcms_cm_info *wlc_cm, u16 chspec)
1023 {
1024         return N_ENAB(wlc_cm->wlc->pub) && CHSPEC_IS40(chspec) ?
1025                 (isset(wlc_cm->quiet_channels.vec,
1026                        LOWER_20_SB(CHSPEC_CHANNEL(chspec))) ||
1027                  isset(wlc_cm->quiet_channels.vec,
1028                        UPPER_20_SB(CHSPEC_CHANNEL(chspec)))) :
1029                 isset(wlc_cm->quiet_channels.vec, CHSPEC_CHANNEL(chspec));
1030 }
1031
1032 /* Is the channel valid for the current locale? (but don't consider channels not
1033  *   available due to bandlocking)
1034  */
1035 static bool brcms_c_valid_channel20_db(struct brcms_cm_info *wlc_cm, uint val)
1036 {
1037         struct brcms_c_info *wlc = wlc_cm->wlc;
1038
1039         return VALID_CHANNEL20(wlc, val) ||
1040                 (!wlc->bandlocked
1041                  && VALID_CHANNEL20_IN_BAND(wlc, OTHERBANDUNIT(wlc), val));
1042 }
1043
1044 /* Is the channel valid for the current locale and specified band? */
1045 static bool brcms_c_valid_channel20_in_band(struct brcms_cm_info *wlc_cm,
1046                                             uint bandunit, uint val)
1047 {
1048         return ((val < MAXCHANNEL)
1049                 && isset(wlc_cm->bandstate[bandunit].valid_channels.vec, val));
1050 }
1051
1052 /* Is the channel valid for the current locale and current band? */
1053 static bool brcms_c_valid_channel20(struct brcms_cm_info *wlc_cm, uint val)
1054 {
1055         struct brcms_c_info *wlc = wlc_cm->wlc;
1056
1057         return ((val < MAXCHANNEL) &&
1058                 isset(wlc_cm->bandstate[wlc->band->bandunit].valid_channels.vec,
1059                       val));
1060 }
1061
1062 static void
1063 brcms_c_channel_min_txpower_limits_with_local_constraint(
1064                 struct brcms_cm_info *wlc_cm, struct txpwr_limits *txpwr,
1065                 u8 local_constraint_qdbm)
1066 {
1067         int j;
1068
1069         /* CCK Rates */
1070         for (j = 0; j < WL_TX_POWER_CCK_NUM; j++)
1071                 txpwr->cck[j] = min(txpwr->cck[j], local_constraint_qdbm);
1072
1073         /* 20 MHz Legacy OFDM SISO */
1074         for (j = 0; j < WL_TX_POWER_OFDM_NUM; j++)
1075                 txpwr->ofdm[j] = min(txpwr->ofdm[j], local_constraint_qdbm);
1076
1077         /* 20 MHz Legacy OFDM CDD */
1078         for (j = 0; j < BRCMS_NUM_RATES_OFDM; j++)
1079                 txpwr->ofdm_cdd[j] =
1080                     min(txpwr->ofdm_cdd[j], local_constraint_qdbm);
1081
1082         /* 40 MHz Legacy OFDM SISO */
1083         for (j = 0; j < BRCMS_NUM_RATES_OFDM; j++)
1084                 txpwr->ofdm_40_siso[j] =
1085                     min(txpwr->ofdm_40_siso[j], local_constraint_qdbm);
1086
1087         /* 40 MHz Legacy OFDM CDD */
1088         for (j = 0; j < BRCMS_NUM_RATES_OFDM; j++)
1089                 txpwr->ofdm_40_cdd[j] =
1090                     min(txpwr->ofdm_40_cdd[j], local_constraint_qdbm);
1091
1092         /* 20MHz MCS 0-7 SISO */
1093         for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
1094                 txpwr->mcs_20_siso[j] =
1095                     min(txpwr->mcs_20_siso[j], local_constraint_qdbm);
1096
1097         /* 20MHz MCS 0-7 CDD */
1098         for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
1099                 txpwr->mcs_20_cdd[j] =
1100                     min(txpwr->mcs_20_cdd[j], local_constraint_qdbm);
1101
1102         /* 20MHz MCS 0-7 STBC */
1103         for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
1104                 txpwr->mcs_20_stbc[j] =
1105                     min(txpwr->mcs_20_stbc[j], local_constraint_qdbm);
1106
1107         /* 20MHz MCS 8-15 MIMO */
1108         for (j = 0; j < BRCMS_NUM_RATES_MCS_2_STREAM; j++)
1109                 txpwr->mcs_20_mimo[j] =
1110                     min(txpwr->mcs_20_mimo[j], local_constraint_qdbm);
1111
1112         /* 40MHz MCS 0-7 SISO */
1113         for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
1114                 txpwr->mcs_40_siso[j] =
1115                     min(txpwr->mcs_40_siso[j], local_constraint_qdbm);
1116
1117         /* 40MHz MCS 0-7 CDD */
1118         for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
1119                 txpwr->mcs_40_cdd[j] =
1120                     min(txpwr->mcs_40_cdd[j], local_constraint_qdbm);
1121
1122         /* 40MHz MCS 0-7 STBC */
1123         for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
1124                 txpwr->mcs_40_stbc[j] =
1125                     min(txpwr->mcs_40_stbc[j], local_constraint_qdbm);
1126
1127         /* 40MHz MCS 8-15 MIMO */
1128         for (j = 0; j < BRCMS_NUM_RATES_MCS_2_STREAM; j++)
1129                 txpwr->mcs_40_mimo[j] =
1130                     min(txpwr->mcs_40_mimo[j], local_constraint_qdbm);
1131
1132         /* 40MHz MCS 32 */
1133         txpwr->mcs32 = min(txpwr->mcs32, local_constraint_qdbm);
1134
1135 }
1136
1137 void
1138 brcms_c_channel_set_chanspec(struct brcms_cm_info *wlc_cm, u16 chanspec,
1139                          u8 local_constraint_qdbm)
1140 {
1141         struct brcms_c_info *wlc = wlc_cm->wlc;
1142         struct txpwr_limits txpwr;
1143
1144         brcms_c_channel_reg_limits(wlc_cm, chanspec, &txpwr);
1145
1146         brcms_c_channel_min_txpower_limits_with_local_constraint(
1147                 wlc_cm, &txpwr, local_constraint_qdbm
1148         );
1149
1150         brcms_b_set_chanspec(wlc->hw, chanspec,
1151                               (brcms_c_quiet_chanspec(wlc_cm, chanspec) != 0),
1152                               &txpwr);
1153 }
1154
1155 #ifdef POWER_DBG
1156 static void wlc_phy_txpower_limits_dump(struct txpwr_limits *txpwr)
1157 {
1158         int i;
1159         char buf[80];
1160         char fraction[4][4] = { "   ", ".25", ".5 ", ".75" };
1161
1162         sprintf(buf, "CCK                ");
1163         for (i = 0; i < BRCMS_NUM_RATES_CCK; i++)
1164                 sprintf(buf[strlen(buf)], " %2d%s",
1165                         txpwr->cck[i] / BRCMS_TXPWR_DB_FACTOR,
1166                         fraction[txpwr->cck[i] % BRCMS_TXPWR_DB_FACTOR]);
1167         printk(KERN_DEBUG "%s\n", buf);
1168
1169         sprintf(buf, "20 MHz OFDM SISO   ");
1170         for (i = 0; i < BRCMS_NUM_RATES_OFDM; i++)
1171                 sprintf(buf[strlen(buf)], " %2d%s",
1172                         txpwr->ofdm[i] / BRCMS_TXPWR_DB_FACTOR,
1173                         fraction[txpwr->ofdm[i] % BRCMS_TXPWR_DB_FACTOR]);
1174         printk(KERN_DEBUG "%s\n", buf);
1175
1176         sprintf(buf, "20 MHz OFDM CDD    ");
1177         for (i = 0; i < BRCMS_NUM_RATES_OFDM; i++)
1178                 sprintf(buf[strlen(buf)], " %2d%s",
1179                         txpwr->ofdm_cdd[i] / BRCMS_TXPWR_DB_FACTOR,
1180                         fraction[txpwr->ofdm_cdd[i] % BRCMS_TXPWR_DB_FACTOR]);
1181         printk(KERN_DEBUG "%s\n", buf);
1182
1183         sprintf(buf, "40 MHz OFDM SISO   ");
1184         for (i = 0; i < BRCMS_NUM_RATES_OFDM; i++)
1185                 sprintf(buf[strlen(buf)], " %2d%s",
1186                         txpwr->ofdm_40_siso[i] / BRCMS_TXPWR_DB_FACTOR,
1187                         fraction[txpwr->ofdm_40_siso[i] %
1188                                                         BRCMS_TXPWR_DB_FACTOR]);
1189         printk(KERN_DEBUG "%s\n", buf);
1190
1191         sprintf(buf, "40 MHz OFDM CDD    ");
1192         for (i = 0; i < BRCMS_NUM_RATES_OFDM; i++)
1193                 sprintf(buf[strlen(buf)], " %2d%s",
1194                         txpwr->ofdm_40_cdd[i] / BRCMS_TXPWR_DB_FACTOR,
1195                         fraction[txpwr->ofdm_40_cdd[i] %
1196                                                         BRCMS_TXPWR_DB_FACTOR]);
1197         printk(KERN_DEBUG "%s\n", buf);
1198
1199         sprintf(buf, "20 MHz MCS0-7 SISO ");
1200         for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++)
1201                 sprintf(buf[strlen(buf)], " %2d%s",
1202                         txpwr->mcs_20_siso[i] / BRCMS_TXPWR_DB_FACTOR,
1203                         fraction[txpwr->mcs_20_siso[i] %
1204                                                         BRCMS_TXPWR_DB_FACTOR]);
1205         printk(KERN_DEBUG "%s\n", buf);
1206
1207         sprintf(buf, "20 MHz MCS0-7 CDD  ");
1208         for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++)
1209                 sprintf(buf[strlen(buf)], " %2d%s",
1210                         txpwr->mcs_20_cdd[i] / BRCMS_TXPWR_DB_FACTOR,
1211                         fraction[txpwr->mcs_20_cdd[i] %
1212                                                         BRCMS_TXPWR_DB_FACTOR]);
1213         printk(KERN_DEBUG "%s\n", buf);
1214
1215         sprintf(buf, "20 MHz MCS0-7 STBC ");
1216         for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++)
1217                 sprintf(buf[strlen(buf)], " %2d%s",
1218                         txpwr->mcs_20_stbc[i] / BRCMS_TXPWR_DB_FACTOR,
1219                         fraction[txpwr->mcs_20_stbc[i] %
1220                                                         BRCMS_TXPWR_DB_FACTOR]);
1221         printk(KERN_DEBUG "%s\n", buf);
1222
1223         sprintf(buf, "20 MHz MCS8-15 SDM ");
1224         for (i = 0; i < BRCMS_NUM_RATES_MCS_2_STREAM; i++)
1225                 sprintf(buf[strlen(buf)], " %2d%s",
1226                         txpwr->mcs_20_mimo[i] / BRCMS_TXPWR_DB_FACTOR,
1227                         fraction[txpwr->mcs_20_mimo[i] %
1228                                                         BRCMS_TXPWR_DB_FACTOR]);
1229         printk(KERN_DEBUG "%s\n", buf);
1230
1231         sprintf(buf, "40 MHz MCS0-7 SISO ");
1232         for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++)
1233                 sprintf(buf[strlen(buf)], " %2d%s",
1234                         txpwr->mcs_40_siso[i] / BRCMS_TXPWR_DB_FACTOR,
1235                         fraction[txpwr->mcs_40_siso[i] %
1236                                                         BRCMS_TXPWR_DB_FACTOR]);
1237         printk(KERN_DEBUG "%s\n", buf);
1238
1239         sprintf(buf, "40 MHz MCS0-7 CDD  ");
1240         for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++)
1241                 sprintf(buf[strlen(buf)], " %2d%s",
1242                         txpwr->mcs_40_cdd[i] / BRCMS_TXPWR_DB_FACTOR,
1243                         fraction[txpwr->mcs_40_cdd[i] %
1244                                                         BRCMS_TXPWR_DB_FACTOR]);
1245         printk(KERN_DEBUG "%s\n", buf);
1246
1247         sprintf(buf, "40 MHz MCS0-7 STBC ");
1248         for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++)
1249                 sprintf(buf[strlen(buf)], " %2d%s",
1250                         txpwr->mcs_40_stbc[i] / BRCMS_TXPWR_DB_FACTOR,
1251                         fraction[txpwr->mcs_40_stbc[i] %
1252                                                         BRCMS_TXPWR_DB_FACTOR]);
1253         printk(KERN_DEBUG "%s\n", buf);
1254
1255         sprintf(buf, "40 MHz MCS8-15 SDM ");
1256         for (i = 0; i < BRCMS_NUM_RATES_MCS_2_STREAM; i++)
1257                 sprintf(buf[strlen(buf)], " %2d%s",
1258                         txpwr->mcs_40_mimo[i] / BRCMS_TXPWR_DB_FACTOR,
1259                         fraction[txpwr->mcs_40_mimo[i] %
1260                                                         BRCMS_TXPWR_DB_FACTOR]);
1261         }
1262         printk(KERN_DEBUG "%s\n", buf);
1263
1264         printk(KERN_DEBUG "MCS32               %2d%s\n",
1265                txpwr->mcs32 / BRCMS_TXPWR_DB_FACTOR,
1266                fraction[txpwr->mcs32 % BRCMS_TXPWR_DB_FACTOR]);
1267 }
1268 #endif                          /* POWER_DBG */
1269
1270 void
1271 brcms_c_channel_reg_limits(struct brcms_cm_info *wlc_cm, u16 chanspec,
1272                        struct txpwr_limits *txpwr)
1273 {
1274         struct brcms_c_info *wlc = wlc_cm->wlc;
1275         uint i;
1276         uint chan;
1277         int maxpwr;
1278         int delta;
1279         const struct country_info *country;
1280         struct brcms_band *band;
1281         const struct locale_info *li;
1282         int conducted_max = BRCMS_TXPWR_MAX;
1283         int conducted_ofdm_max = BRCMS_TXPWR_MAX;
1284         const struct locale_mimo_info *li_mimo;
1285         int maxpwr20, maxpwr40;
1286         int maxpwr_idx;
1287         uint j;
1288
1289         memset(txpwr, 0, sizeof(struct txpwr_limits));
1290
1291         if (!brcms_c_valid_chanspec_db(wlc_cm, chanspec)) {
1292                 country = brcms_c_country_lookup(wlc, wlc->autocountry_default);
1293                 if (country == NULL)
1294                         return;
1295         } else {
1296                 country = wlc_cm->country;
1297         }
1298
1299         chan = CHSPEC_CHANNEL(chanspec);
1300         band = wlc->bandstate[CHSPEC_BANDUNIT(chanspec)];
1301         li = BAND_5G(band->bandtype) ?
1302             brcms_c_get_locale_5g(country->locale_5G) :
1303             brcms_c_get_locale_2g(country->locale_2G);
1304
1305         li_mimo = BAND_5G(band->bandtype) ?
1306             brcms_c_get_mimo_5g(country->locale_mimo_5G) :
1307             brcms_c_get_mimo_2g(country->locale_mimo_2G);
1308
1309         if (li->flags & BRCMS_EIRP) {
1310                 delta = band->antgain;
1311         } else {
1312                 delta = 0;
1313                 if (band->antgain > QDB(6))
1314                         delta = band->antgain - QDB(6); /* Excess over 6 dB */
1315         }
1316
1317         if (li == &locale_i) {
1318                 conducted_max = QDB(22);
1319                 conducted_ofdm_max = QDB(22);
1320         }
1321
1322         /* CCK txpwr limits for 2.4G band */
1323         if (BAND_2G(band->bandtype)) {
1324                 maxpwr = li->maxpwr[CHANNEL_POWER_IDX_2G_CCK(chan)];
1325
1326                 maxpwr = maxpwr - delta;
1327                 maxpwr = max(maxpwr, 0);
1328                 maxpwr = min(maxpwr, conducted_max);
1329
1330                 for (i = 0; i < BRCMS_NUM_RATES_CCK; i++)
1331                         txpwr->cck[i] = (u8) maxpwr;
1332         }
1333
1334         /* OFDM txpwr limits for 2.4G or 5G bands */
1335         if (BAND_2G(band->bandtype))
1336                 maxpwr = li->maxpwr[CHANNEL_POWER_IDX_2G_OFDM(chan)];
1337         else
1338                 maxpwr = li->maxpwr[CHANNEL_POWER_IDX_5G(chan)];
1339
1340         maxpwr = maxpwr - delta;
1341         maxpwr = max(maxpwr, 0);
1342         maxpwr = min(maxpwr, conducted_ofdm_max);
1343
1344         /* Keep OFDM lmit below CCK limit */
1345         if (BAND_2G(band->bandtype))
1346                 maxpwr = min_t(int, maxpwr, txpwr->cck[0]);
1347
1348         for (i = 0; i < BRCMS_NUM_RATES_OFDM; i++)
1349                 txpwr->ofdm[i] = (u8) maxpwr;
1350
1351         for (i = 0; i < BRCMS_NUM_RATES_OFDM; i++) {
1352                 /*
1353                  * OFDM 40 MHz SISO has the same power as the corresponding
1354                  * MCS0-7 rate unless overriden by the locale specific code.
1355                  * We set this value to 0 as a flag (presumably 0 dBm isn't
1356                  * a possibility) and then copy the MCS0-7 value to the 40 MHz
1357                  * value if it wasn't explicitly set.
1358                  */
1359                 txpwr->ofdm_40_siso[i] = 0;
1360
1361                 txpwr->ofdm_cdd[i] = (u8) maxpwr;
1362
1363                 txpwr->ofdm_40_cdd[i] = 0;
1364         }
1365
1366         /* MIMO/HT specific limits */
1367         if (li_mimo->flags & BRCMS_EIRP) {
1368                 delta = band->antgain;
1369         } else {
1370                 delta = 0;
1371                 if (band->antgain > QDB(6))
1372                         delta = band->antgain - QDB(6); /* Excess over 6 dB */
1373         }
1374
1375         if (BAND_2G(band->bandtype))
1376                 maxpwr_idx = (chan - 1);
1377         else
1378                 maxpwr_idx = CHANNEL_POWER_IDX_5G(chan);
1379
1380         maxpwr20 = li_mimo->maxpwr20[maxpwr_idx];
1381         maxpwr40 = li_mimo->maxpwr40[maxpwr_idx];
1382
1383         maxpwr20 = maxpwr20 - delta;
1384         maxpwr20 = max(maxpwr20, 0);
1385         maxpwr40 = maxpwr40 - delta;
1386         maxpwr40 = max(maxpwr40, 0);
1387
1388         /* Fill in the MCS 0-7 (SISO) rates */
1389         for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
1390
1391                 /*
1392                  * 20 MHz has the same power as the corresponding OFDM rate
1393                  * unless overriden by the locale specific code.
1394                  */
1395                 txpwr->mcs_20_siso[i] = txpwr->ofdm[i];
1396                 txpwr->mcs_40_siso[i] = 0;
1397         }
1398
1399         /* Fill in the MCS 0-7 CDD rates */
1400         for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
1401                 txpwr->mcs_20_cdd[i] = (u8) maxpwr20;
1402                 txpwr->mcs_40_cdd[i] = (u8) maxpwr40;
1403         }
1404
1405         /*
1406          * These locales have SISO expressed in the
1407          * table and override CDD later
1408          */
1409         if (li_mimo == &locale_bn) {
1410                 if (li_mimo == &locale_bn) {
1411                         maxpwr20 = QDB(16);
1412                         maxpwr40 = 0;
1413
1414                         if (chan >= 3 && chan <= 11)
1415                                 maxpwr40 = QDB(16);
1416                 }
1417
1418                 for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
1419                         txpwr->mcs_20_siso[i] = (u8) maxpwr20;
1420                         txpwr->mcs_40_siso[i] = (u8) maxpwr40;
1421                 }
1422         }
1423
1424         /* Fill in the MCS 0-7 STBC rates */
1425         for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
1426                 txpwr->mcs_20_stbc[i] = 0;
1427                 txpwr->mcs_40_stbc[i] = 0;
1428         }
1429
1430         /* Fill in the MCS 8-15 SDM rates */
1431         for (i = 0; i < BRCMS_NUM_RATES_MCS_2_STREAM; i++) {
1432                 txpwr->mcs_20_mimo[i] = (u8) maxpwr20;
1433                 txpwr->mcs_40_mimo[i] = (u8) maxpwr40;
1434         }
1435
1436         /* Fill in MCS32 */
1437         txpwr->mcs32 = (u8) maxpwr40;
1438
1439         for (i = 0, j = 0; i < BRCMS_NUM_RATES_OFDM; i++, j++) {
1440                 if (txpwr->ofdm_40_cdd[i] == 0)
1441                         txpwr->ofdm_40_cdd[i] = txpwr->mcs_40_cdd[j];
1442                 if (i == 0) {
1443                         i = i + 1;
1444                         if (txpwr->ofdm_40_cdd[i] == 0)
1445                                 txpwr->ofdm_40_cdd[i] = txpwr->mcs_40_cdd[j];
1446                 }
1447         }
1448
1449         /*
1450          * Copy the 40 MHZ MCS 0-7 CDD value to the 40 MHZ MCS 0-7 SISO
1451          * value if it wasn't provided explicitly.
1452          */
1453         for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
1454                 if (txpwr->mcs_40_siso[i] == 0)
1455                         txpwr->mcs_40_siso[i] = txpwr->mcs_40_cdd[i];
1456         }
1457
1458         for (i = 0, j = 0; i < BRCMS_NUM_RATES_OFDM; i++, j++) {
1459                 if (txpwr->ofdm_40_siso[i] == 0)
1460                         txpwr->ofdm_40_siso[i] = txpwr->mcs_40_siso[j];
1461                 if (i == 0) {
1462                         i = i + 1;
1463                         if (txpwr->ofdm_40_siso[i] == 0)
1464                                 txpwr->ofdm_40_siso[i] = txpwr->mcs_40_siso[j];
1465                 }
1466         }
1467
1468         /*
1469          * Copy the 20 and 40 MHz MCS0-7 CDD values to the corresponding
1470          * STBC values if they weren't provided explicitly.
1471          */
1472         for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
1473                 if (txpwr->mcs_20_stbc[i] == 0)
1474                         txpwr->mcs_20_stbc[i] = txpwr->mcs_20_cdd[i];
1475
1476                 if (txpwr->mcs_40_stbc[i] == 0)
1477                         txpwr->mcs_40_stbc[i] = txpwr->mcs_40_cdd[i];
1478         }
1479
1480 #ifdef POWER_DBG
1481         wlc_phy_txpower_limits_dump(txpwr);
1482 #endif
1483         return;
1484 }
1485
1486 /* Returns true if currently set country is Japan or variant */
1487 static bool brcms_c_japan(struct brcms_c_info *wlc)
1488 {
1489         return brcms_c_japan_ccode(wlc->cmi->country_abbrev);
1490 }
1491
1492 /* JP, J1 - J10 are Japan ccodes */
1493 static bool brcms_c_japan_ccode(const char *ccode)
1494 {
1495         return (ccode[0] == 'J' &&
1496                 (ccode[1] == 'P' || (ccode[1] >= '1' && ccode[1] <= '9')));
1497 }
1498
1499 /*
1500  * Validate the chanspec for this locale, for 40MHZ we need to also
1501  * check that the sidebands are valid 20MZH channels in this locale
1502  * and they are also a legal HT combination
1503  */
1504 static bool
1505 brcms_c_valid_chanspec_ext(struct brcms_cm_info *wlc_cm, u16 chspec,
1506                            bool dualband)
1507 {
1508         struct brcms_c_info *wlc = wlc_cm->wlc;
1509         u8 channel = CHSPEC_CHANNEL(chspec);
1510
1511         /* check the chanspec */
1512         if (brcmu_chspec_malformed(chspec)) {
1513                 wiphy_err(wlc->wiphy, "wl%d: malformed chanspec 0x%x\n",
1514                         wlc->pub->unit, chspec);
1515                 return false;
1516         }
1517
1518         if (CHANNEL_BANDUNIT(wlc_cm->wlc, channel) !=
1519             CHSPEC_BANDUNIT(chspec))
1520                 return false;
1521
1522         /* Check a 20Mhz channel */
1523         if (CHSPEC_IS20(chspec)) {
1524                 if (dualband)
1525                         return VALID_CHANNEL20_DB(wlc_cm->wlc, channel);
1526                 else
1527                         return VALID_CHANNEL20(wlc_cm->wlc, channel);
1528         }
1529 #ifdef SUPPORT_40MHZ
1530         /*
1531          * We know we are now checking a 40MHZ channel, so we should
1532          * only be here for NPHYS
1533          */
1534         if (BRCMS_ISNPHY(wlc->band) || BRCMS_ISSSLPNPHY(wlc->band)) {
1535                 u8 upper_sideband = 0, idx;
1536                 u8 num_ch20_entries =
1537                     sizeof(chan20_info) / sizeof(struct chan20_info);
1538
1539                 if (!VALID_40CHANSPEC_IN_BAND(wlc, CHSPEC_BANDUNIT(chspec)))
1540                         return false;
1541
1542                 if (dualband) {
1543                         if (!VALID_CHANNEL20_DB(wlc, LOWER_20_SB(channel)) ||
1544                             !VALID_CHANNEL20_DB(wlc, UPPER_20_SB(channel)))
1545                                 return false;
1546                 } else {
1547                         if (!VALID_CHANNEL20(wlc, LOWER_20_SB(channel)) ||
1548                             !VALID_CHANNEL20(wlc, UPPER_20_SB(channel)))
1549                                 return false;
1550                 }
1551
1552                 /* find the lower sideband info in the sideband array */
1553                 for (idx = 0; idx < num_ch20_entries; idx++) {
1554                         if (chan20_info[idx].sb == LOWER_20_SB(channel))
1555                                 upper_sideband = chan20_info[idx].adj_sbs;
1556                 }
1557                 /* check that the lower sideband allows an upper sideband */
1558                 if ((upper_sideband & (CH_UPPER_SB | CH_EWA_VALID)) ==
1559                     (CH_UPPER_SB | CH_EWA_VALID))
1560                         return true;
1561                 return false;
1562         }
1563 #endif                          /* 40 MHZ */
1564
1565         return false;
1566 }
1567
1568 bool brcms_c_valid_chanspec_db(struct brcms_cm_info *wlc_cm, u16 chspec)
1569 {
1570         return brcms_c_valid_chanspec_ext(wlc_cm, chspec, true);
1571 }