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