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