]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - tools/tools/ath/athrd/athrd.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / tools / tools / ath / athrd / athrd.c
1 /*-
2  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  *
29  * $FreeBSD$
30  */
31 #include "opt_ah.h"
32
33 #include "ah.h"
34
35 #include <net80211/_ieee80211.h>
36 #include <net80211/ieee80211_regdomain.h>
37
38 #include "ah_internal.h"
39 #include "ah_eeprom_v3.h"               /* XXX */
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <stdarg.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <ctype.h>
47
48 int             ath_hal_debug = 0;
49 HAL_CTRY_CODE   cc = CTRY_DEFAULT;
50 HAL_REG_DOMAIN  rd = 169;               /* FCC */
51 HAL_BOOL        Amode = 1;
52 HAL_BOOL        Bmode = 1;
53 HAL_BOOL        Gmode = 1;
54 HAL_BOOL        HT20mode = 1;
55 HAL_BOOL        HT40mode = 1;
56 HAL_BOOL        turbo5Disable = AH_FALSE;
57 HAL_BOOL        turbo2Disable = AH_FALSE;
58
59 u_int16_t       _numCtls = 8;
60 u_int16_t       _ctl[32] =
61         { 0x10, 0x13, 0x40, 0x30, 0x11, 0x31, 0x12, 0x32 };
62 RD_EDGES_POWER  _rdEdgesPower[NUM_EDGES*NUM_CTLS] = {
63         { 5180, 28, 0 },        /* 0x10 */
64         { 5240, 60, 0 },
65         { 5260, 36, 0 },
66         { 5320, 27, 0 },
67         { 5745, 36, 0 },
68         { 5765, 36, 0 },
69         { 5805, 36, 0 },
70         { 5825, 36, 0 },
71
72         { 5210, 28, 0 },        /* 0x13 */
73         { 5250, 28, 0 },
74         { 5290, 30, 0 },
75         { 5760, 36, 0 },
76         { 5800, 36, 0 },
77         { 0, 0, 0 },
78         { 0, 0, 0 },
79         { 0, 0, 0 },
80
81         { 5170, 60, 0 },        /* 0x40 */
82         { 5230, 60, 0 },
83         { 0, 0, 0 },
84         { 0, 0, 0 },
85         { 0, 0, 0 },
86         { 0, 0, 0 },
87         { 0, 0, 0 },
88         { 0, 0, 0 },
89
90         { 5180, 33, 0 },        /* 0x30 */
91         { 5320, 33, 0 },
92         { 5500, 34, 0 },
93         { 5700, 34, 0 },
94         { 5745, 35, 0 },
95         { 5765, 35, 0 },
96         { 5785, 35, 0 },
97         { 5825, 35, 0 },
98
99         { 2412, 36, 0 },        /* 0x11 */
100         { 2417, 36, 0 },
101         { 2422, 36, 0 },
102         { 2432, 36, 0 },
103         { 2442, 36, 0 },
104         { 2457, 36, 0 },
105         { 2467, 36, 0 },
106         { 2472, 36, 0 },
107
108         { 2412, 36, 0 },        /* 0x31 */
109         { 2417, 36, 0 },
110         { 2422, 36, 0 },
111         { 2432, 36, 0 },
112         { 2442, 36, 0 },
113         { 2457, 36, 0 },
114         { 2467, 36, 0 },
115         { 2472, 36, 0 },
116
117         { 2412, 36, 0 },        /* 0x12 */
118         { 2417, 36, 0 },
119         { 2422, 36, 0 },
120         { 2432, 36, 0 },
121         { 2442, 36, 0 },
122         { 2457, 36, 0 },
123         { 2467, 36, 0 },
124         { 2472, 36, 0 },
125
126         { 2412, 28, 0 },        /* 0x32 */
127         { 2417, 28, 0 },
128         { 2422, 28, 0 },
129         { 2432, 28, 0 },
130         { 2442, 28, 0 },
131         { 2457, 28, 0 },
132         { 2467, 28, 0 },
133         { 2472, 28, 0 },
134 };
135
136 u_int16_t       turbo2WMaxPower5 = 32;
137 u_int16_t       turbo2WMaxPower2;
138 int8_t          antennaGainMax[2] = { 0, 0 };   /* XXX */
139 int             eeversion = AR_EEPROM_VER3_1;
140 TRGT_POWER_ALL_MODES tpow = {
141         8, {
142             { 22, 24, 28, 32, 5180 },
143             { 22, 24, 28, 32, 5200 },
144             { 22, 24, 28, 32, 5320 },
145             { 26, 30, 34, 34, 5500 },
146             { 26, 30, 34, 34, 5700 },
147             { 20, 30, 34, 36, 5745 },
148             { 20, 30, 34, 36, 5825 },
149             { 20, 30, 34, 36, 5850 },
150         },
151         2, {
152             { 23, 27, 31, 34, 2412 },
153             { 23, 27, 31, 34, 2447 },
154         },
155         2, {
156             { 36, 36, 36, 36, 2412 },
157             { 36, 36, 36, 36, 2484 },
158         }
159 };
160 #define numTargetPwr_11a        tpow.numTargetPwr_11a
161 #define trgtPwr_11a             tpow.trgtPwr_11a
162 #define numTargetPwr_11g        tpow.numTargetPwr_11g
163 #define trgtPwr_11g             tpow.trgtPwr_11g
164 #define numTargetPwr_11b        tpow.numTargetPwr_11b
165 #define trgtPwr_11b             tpow.trgtPwr_11b
166
167 static HAL_BOOL
168 getChannelEdges(struct ath_hal *ah, u_int16_t flags, u_int16_t *low, u_int16_t *high)
169 {
170         struct ath_hal_private *ahp = AH_PRIVATE(ah);
171         HAL_CAPABILITIES *pCap = &ahp->ah_caps;
172
173         if (flags & IEEE80211_CHAN_5GHZ) {
174                 *low = pCap->halLow5GhzChan;
175                 *high = pCap->halHigh5GhzChan;
176                 return AH_TRUE;
177         }
178         if (flags & IEEE80211_CHAN_2GHZ) {
179                 *low = pCap->halLow2GhzChan;
180                 *high = pCap->halHigh2GhzChan;
181                 return AH_TRUE;
182         }
183         return AH_FALSE;
184 }
185
186 static u_int
187 getWirelessModes(struct ath_hal *ah)
188 {
189         u_int mode = 0;
190
191         if (Amode) {
192                 mode = HAL_MODE_11A;
193                 if (!turbo5Disable)
194                         mode |= HAL_MODE_TURBO;
195         }
196         if (Bmode)
197                 mode |= HAL_MODE_11B;
198         if (Gmode) {
199                 mode |= HAL_MODE_11G;
200                 if (!turbo2Disable) 
201                         mode |= HAL_MODE_108G;
202         }
203         if (HT20mode)
204                 mode |= HAL_MODE_11NG_HT20|HAL_MODE_11NA_HT20;
205         if (HT40mode)
206                 mode |= HAL_MODE_11NG_HT40PLUS|HAL_MODE_11NA_HT40PLUS
207                      |  HAL_MODE_11NG_HT40MINUS|HAL_MODE_11NA_HT40MINUS
208                      ;
209         return mode;
210 }
211
212 /* Enumerated Regulatory Domain Information 8 bit values indicate that
213  * the regdomain is really a pair of unitary regdomains.  12 bit values
214  * are the real unitary regdomains and are the only ones which have the
215  * frequency bitmasks and flags set.
216  */
217
218 enum EnumRd {
219         /*
220          * The following regulatory domain definitions are
221          * found in the EEPROM. Each regulatory domain
222          * can operate in either a 5GHz or 2.4GHz wireless mode or
223          * both 5GHz and 2.4GHz wireless modes.
224          * In general, the value holds no special
225          * meaning and is used to decode into either specific
226          * 2.4GHz or 5GHz wireless mode for that particular
227          * regulatory domain.
228          */
229         NO_ENUMRD       = 0x00,
230         NULL1_WORLD     = 0x03,         /* For 11b-only countries (no 11a allowed) */
231         NULL1_ETSIB     = 0x07,         /* Israel */
232         NULL1_ETSIC     = 0x08,
233         FCC1_FCCA       = 0x10,         /* USA */
234         FCC1_WORLD      = 0x11,         /* Hong Kong */
235         FCC4_FCCA       = 0x12,         /* USA - Public Safety */
236
237         FCC2_FCCA       = 0x20,         /* Canada */
238         FCC2_WORLD      = 0x21,         /* Australia & HK */
239         FCC2_ETSIC      = 0x22,
240         FRANCE_RES      = 0x31,         /* Legacy France for OEM */
241         FCC3_FCCA       = 0x3A,         /* USA & Canada w/5470 band, 11h, DFS enabled */
242         FCC3_WORLD  = 0x3B,     /* USA & Canada w/5470 band, 11h, DFS enabled */
243
244         ETSI1_WORLD     = 0x37,
245         ETSI3_ETSIA     = 0x32,         /* France (optional) */
246         ETSI2_WORLD     = 0x35,         /* Hungary & others */
247         ETSI3_WORLD     = 0x36,         /* France & others */
248         ETSI4_WORLD     = 0x30,
249         ETSI4_ETSIC     = 0x38,
250         ETSI5_WORLD     = 0x39,
251         ETSI6_WORLD     = 0x34,         /* Bulgaria */
252         ETSI_RESERVED   = 0x33,         /* Reserved (Do not used) */
253
254         MKK1_MKKA       = 0x40,         /* Japan (JP1) */
255         MKK1_MKKB       = 0x41,         /* Japan (JP0) */
256         APL4_WORLD      = 0x42,         /* Singapore */
257         MKK2_MKKA       = 0x43,         /* Japan with 4.9G channels */
258         APL_RESERVED    = 0x44,         /* Reserved (Do not used)  */
259         APL2_WORLD      = 0x45,         /* Korea */
260         APL2_APLC       = 0x46,
261         APL3_WORLD      = 0x47,
262         MKK1_FCCA       = 0x48,         /* Japan (JP1-1) */
263         APL2_APLD       = 0x49,         /* Korea with 2.3G channels */
264         MKK1_MKKA1      = 0x4A,         /* Japan (JE1) */
265         MKK1_MKKA2      = 0x4B,         /* Japan (JE2) */
266         MKK1_MKKC       = 0x4C,         /* Japan (MKK1_MKKA,except Ch14) */
267
268         APL3_FCCA   = 0x50,
269         APL1_WORLD      = 0x52,         /* Latin America */
270         APL1_FCCA       = 0x53,
271         APL1_APLA       = 0x54,
272         APL1_ETSIC      = 0x55,
273         APL2_ETSIC      = 0x56,         /* Venezuela */
274         APL5_WORLD      = 0x58,         /* Chile */
275         APL6_WORLD      = 0x5B,         /* Singapore */
276         APL7_FCCA   = 0x5C,     /* Taiwan 5.47 Band */
277         APL8_WORLD  = 0x5D,     /* Malaysia 5GHz */
278         APL9_WORLD  = 0x5E,     /* Korea 5GHz */
279
280         /*
281          * World mode SKUs
282          */
283         WOR0_WORLD      = 0x60,         /* World0 (WO0 SKU) */
284         WOR1_WORLD      = 0x61,         /* World1 (WO1 SKU) */
285         WOR2_WORLD      = 0x62,         /* World2 (WO2 SKU) */
286         WOR3_WORLD      = 0x63,         /* World3 (WO3 SKU) */
287         WOR4_WORLD      = 0x64,         /* World4 (WO4 SKU) */  
288         WOR5_ETSIC      = 0x65,         /* World5 (WO5 SKU) */    
289
290         WOR01_WORLD     = 0x66,         /* World0-1 (WW0-1 SKU) */
291         WOR02_WORLD     = 0x67,         /* World0-2 (WW0-2 SKU) */
292         EU1_WORLD       = 0x68,         /* Same as World0-2 (WW0-2 SKU), except active scan ch1-13. No ch14 */
293
294         WOR9_WORLD      = 0x69,         /* World9 (WO9 SKU) */  
295         WORA_WORLD      = 0x6A,         /* WorldA (WOA SKU) */  
296
297         MKK3_MKKB       = 0x80,         /* Japan UNI-1 even + MKKB */
298         MKK3_MKKA2      = 0x81,         /* Japan UNI-1 even + MKKA2 */
299         MKK3_MKKC       = 0x82,         /* Japan UNI-1 even + MKKC */
300
301         MKK4_MKKB       = 0x83,         /* Japan UNI-1 even + UNI-2 + MKKB */
302         MKK4_MKKA2      = 0x84,         /* Japan UNI-1 even + UNI-2 + MKKA2 */
303         MKK4_MKKC       = 0x85,         /* Japan UNI-1 even + UNI-2 + MKKC */
304
305         MKK5_MKKB       = 0x86,         /* Japan UNI-1 even + UNI-2 + mid-band + MKKB */
306         MKK5_MKKA2      = 0x87,         /* Japan UNI-1 even + UNI-2 + mid-band + MKKA2 */
307         MKK5_MKKC       = 0x88,         /* Japan UNI-1 even + UNI-2 + mid-band + MKKC */
308
309         MKK6_MKKB       = 0x89,         /* Japan UNI-1 even + UNI-1 odd MKKB */
310         MKK6_MKKA2      = 0x8A,         /* Japan UNI-1 even + UNI-1 odd + MKKA2 */
311         MKK6_MKKC       = 0x8B,         /* Japan UNI-1 even + UNI-1 odd + MKKC */
312
313         MKK7_MKKB       = 0x8C,         /* Japan UNI-1 even + UNI-1 odd + UNI-2 + MKKB */
314         MKK7_MKKA2      = 0x8D,         /* Japan UNI-1 even + UNI-1 odd + UNI-2 + MKKA2 */
315         MKK7_MKKC       = 0x8E,         /* Japan UNI-1 even + UNI-1 odd + UNI-2 + MKKC */
316
317         MKK8_MKKB       = 0x8F,         /* Japan UNI-1 even + UNI-1 odd + UNI-2 + mid-band + MKKB */
318         MKK8_MKKA2      = 0x90,         /* Japan UNI-1 even + UNI-1 odd + UNI-2 + mid-band + MKKA2 */
319         MKK8_MKKC       = 0x91,         /* Japan UNI-1 even + UNI-1 odd + UNI-2 + mid-band + MKKC */
320
321         /* Following definitions are used only by s/w to map old
322          * Japan SKUs.
323          */
324         MKK3_MKKA       = 0xF0,         /* Japan UNI-1 even + MKKA */
325         MKK3_MKKA1      = 0xF1,         /* Japan UNI-1 even + MKKA1 */
326         MKK3_FCCA       = 0xF2,         /* Japan UNI-1 even + FCCA */
327         MKK4_MKKA       = 0xF3,         /* Japan UNI-1 even + UNI-2 + MKKA */
328         MKK4_MKKA1      = 0xF4,         /* Japan UNI-1 even + UNI-2 + MKKA1 */
329         MKK4_FCCA       = 0xF5,         /* Japan UNI-1 even + UNI-2 + FCCA */
330         MKK9_MKKA       = 0xF6,         /* Japan UNI-1 even + 4.9GHz */
331         MKK10_MKKA      = 0xF7,         /* Japan UNI-1 even + UNI-2 + 4.9GHz */
332
333         /*
334          * Regulator domains ending in a number (e.g. APL1,
335          * MK1, ETSI4, etc) apply to 5GHz channel and power
336          * information.  Regulator domains ending in a letter
337          * (e.g. APLA, FCCA, etc) apply to 2.4GHz channel and
338          * power information.
339          */
340         APL1            = 0x0150,       /* LAT & Asia */
341         APL2            = 0x0250,       /* LAT & Asia */
342         APL3            = 0x0350,       /* Taiwan */
343         APL4            = 0x0450,       /* Jordan */
344         APL5            = 0x0550,       /* Chile */
345         APL6            = 0x0650,       /* Singapore */
346         APL8            = 0x0850,       /* Malaysia */
347         APL9            = 0x0950,       /* Korea (South) ROC 3 */
348
349         ETSI1           = 0x0130,       /* Europe & others */
350         ETSI2           = 0x0230,       /* Europe & others */
351         ETSI3           = 0x0330,       /* Europe & others */
352         ETSI4           = 0x0430,       /* Europe & others */
353         ETSI5           = 0x0530,       /* Europe & others */
354         ETSI6           = 0x0630,       /* Europe & others */
355         ETSIA           = 0x0A30,       /* France */
356         ETSIB           = 0x0B30,       /* Israel */
357         ETSIC           = 0x0C30,       /* Latin America */
358
359         FCC1            = 0x0110,       /* US & others */
360         FCC2            = 0x0120,       /* Canada, Australia & New Zealand */
361         FCC3            = 0x0160,       /* US w/new middle band & DFS */    
362         FCC4            = 0x0165,       /* US Public Safety */
363         FCCA            = 0x0A10,        
364
365         APLD            = 0x0D50,       /* South Korea */
366
367         MKK1            = 0x0140,       /* Japan (UNI-1 odd)*/
368         MKK2            = 0x0240,       /* Japan (4.9 GHz + UNI-1 odd) */
369         MKK3            = 0x0340,       /* Japan (UNI-1 even) */
370         MKK4            = 0x0440,       /* Japan (UNI-1 even + UNI-2) */
371         MKK5            = 0x0540,       /* Japan (UNI-1 even + UNI-2 + mid-band) */
372         MKK6            = 0x0640,       /* Japan (UNI-1 odd + UNI-1 even) */
373         MKK7            = 0x0740,       /* Japan (UNI-1 odd + UNI-1 even + UNI-2 */
374         MKK8            = 0x0840,       /* Japan (UNI-1 odd + UNI-1 even + UNI-2 + mid-band) */
375         MKK9            = 0x0940,       /* Japan (UNI-1 even + 4.9 GHZ) */
376         MKK10           = 0x0B40,       /* Japan (UNI-1 even + UNI-2 + 4.9 GHZ) */
377         MKKA            = 0x0A40,       /* Japan */
378         MKKC            = 0x0A50,
379
380         NULL1           = 0x0198,
381         WORLD           = 0x0199,
382         DEBUG_REG_DMN   = 0x01ff,
383 };
384 #define DEF_REGDMN              FCC1_FCCA
385
386 static struct {
387         const char *name;
388         HAL_REG_DOMAIN rd;
389 } domains[] = {
390 #define D(_x)   { #_x, _x }
391         D(NO_ENUMRD),
392         D(NULL1_WORLD),         /* For 11b-only countries (no 11a allowed) */
393         D(NULL1_ETSIB),         /* Israel */
394         D(NULL1_ETSIC),
395         D(FCC1_FCCA),           /* USA */
396         D(FCC1_WORLD),          /* Hong Kong */
397         D(FCC4_FCCA),           /* USA - Public Safety */
398
399         D(FCC2_FCCA),           /* Canada */
400         D(FCC2_WORLD),          /* Australia & HK */
401         D(FCC2_ETSIC),
402         D(FRANCE_RES),          /* Legacy France for OEM */
403         D(FCC3_FCCA),
404         D(FCC3_WORLD),
405
406         D(ETSI1_WORLD),
407         D(ETSI3_ETSIA),         /* France (optional) */
408         D(ETSI2_WORLD),         /* Hungary & others */
409         D(ETSI3_WORLD),         /* France & others */
410         D(ETSI4_WORLD),
411         D(ETSI4_ETSIC),
412         D(ETSI5_WORLD),
413         D(ETSI6_WORLD),         /* Bulgaria */
414         D(ETSI_RESERVED),               /* Reserved (Do not used) */
415
416         D(MKK1_MKKA),           /* Japan (JP1) */
417         D(MKK1_MKKB),           /* Japan (JP0) */
418         D(APL4_WORLD),          /* Singapore */
419         D(MKK2_MKKA),           /* Japan with 4.9G channels */
420         D(APL_RESERVED),                /* Reserved (Do not used)  */
421         D(APL2_WORLD),          /* Korea */
422         D(APL2_APLC),
423         D(APL3_WORLD),
424         D(MKK1_FCCA),           /* Japan (JP1-1) */
425         D(APL2_APLD),           /* Korea with 2.3G channels */
426         D(MKK1_MKKA1),          /* Japan (JE1) */
427         D(MKK1_MKKA2),          /* Japan (JE2) */
428         D(MKK1_MKKC),
429
430         D(APL3_FCCA),
431         D(APL1_WORLD),          /* Latin America */
432         D(APL1_FCCA),
433         D(APL1_APLA),
434         D(APL1_ETSIC),
435         D(APL2_ETSIC),          /* Venezuela */
436         D(APL5_WORLD),          /* Chile */
437         D(APL6_WORLD),          /* Singapore */
438         D(APL7_FCCA),     /* Taiwan 5.47 Band */
439         D(APL8_WORLD),     /* Malaysia 5GHz */
440         D(APL9_WORLD),     /* Korea 5GHz */
441
442         D(WOR0_WORLD),          /* World0 (WO0 SKU) */
443         D(WOR1_WORLD),          /* World1 (WO1 SKU) */
444         D(WOR2_WORLD),          /* World2 (WO2 SKU) */
445         D(WOR3_WORLD),          /* World3 (WO3 SKU) */
446         D(WOR4_WORLD),          /* World4 (WO4 SKU) */  
447         D(WOR5_ETSIC),          /* World5 (WO5 SKU) */    
448
449         D(WOR01_WORLD),         /* World0-1 (WW0-1 SKU) */
450         D(WOR02_WORLD),         /* World0-2 (WW0-2 SKU) */
451         D(EU1_WORLD),
452
453         D(WOR9_WORLD),          /* World9 (WO9 SKU) */  
454         D(WORA_WORLD),          /* WorldA (WOA SKU) */  
455
456         D(MKK3_MKKB),           /* Japan UNI-1 even + MKKB */
457         D(MKK3_MKKA2),          /* Japan UNI-1 even + MKKA2 */
458         D(MKK3_MKKC),           /* Japan UNI-1 even + MKKC */
459
460         D(MKK4_MKKB),           /* Japan UNI-1 even + UNI-2 + MKKB */
461         D(MKK4_MKKA2),          /* Japan UNI-1 even + UNI-2 + MKKA2 */
462         D(MKK4_MKKC),           /* Japan UNI-1 even + UNI-2 + MKKC */
463
464         D(MKK5_MKKB),           /* Japan UNI-1 even + UNI-2 + mid-band + MKKB */
465         D(MKK5_MKKA2),          /* Japan UNI-1 even + UNI-2 + mid-band + MKKA2 */
466         D(MKK5_MKKC),           /* Japan UNI-1 even + UNI-2 + mid-band + MKKC */
467
468         D(MKK6_MKKB),           /* Japan UNI-1 even + UNI-1 odd MKKB */
469         D(MKK6_MKKA2),          /* Japan UNI-1 even + UNI-1 odd + MKKA2 */
470         D(MKK6_MKKC),           /* Japan UNI-1 even + UNI-1 odd + MKKC */
471
472         D(MKK7_MKKB),           /* Japan UNI-1 even + UNI-1 odd + UNI-2 + MKKB */
473         D(MKK7_MKKA2),          /* Japan UNI-1 even + UNI-1 odd + UNI-2 + MKKA2 */
474         D(MKK7_MKKC),           /* Japan UNI-1 even + UNI-1 odd + UNI-2 + MKKC */
475
476         D(MKK8_MKKB),           /* Japan UNI-1 even + UNI-1 odd + UNI-2 + mid-band + MKKB */
477         D(MKK8_MKKA2),          /* Japan UNI-1 even + UNI-1 odd + UNI-2 + mid-band + MKKA2 */
478         D(MKK8_MKKC),           /* Japan UNI-1 even + UNI-1 odd + UNI-2 + mid-band + MKKC */
479
480         D(MKK3_MKKA),         /* Japan UNI-1 even + MKKA */
481         D(MKK3_MKKA1),         /* Japan UNI-1 even + MKKA1 */
482         D(MKK3_FCCA),         /* Japan UNI-1 even + FCCA */
483         D(MKK4_MKKA),         /* Japan UNI-1 even + UNI-2 + MKKA */
484         D(MKK4_MKKA1),         /* Japan UNI-1 even + UNI-2 + MKKA1 */
485         D(MKK4_FCCA),         /* Japan UNI-1 even + UNI-2 + FCCA */
486         D(MKK9_MKKA),         /* Japan UNI-1 even + 4.9GHz */
487         D(MKK10_MKKA),         /* Japan UNI-1 even + UNI-2 + 4.9GHz */
488
489         D(APL1),        /* LAT & Asia */
490         D(APL2),        /* LAT & Asia */
491         D(APL3),        /* Taiwan */
492         D(APL4),        /* Jordan */
493         D(APL5),        /* Chile */
494         D(APL6),        /* Singapore */
495         D(APL8),        /* Malaysia */
496         D(APL9),        /* Korea (South) ROC 3 */
497
498         D(ETSI1),       /* Europe & others */
499         D(ETSI2),       /* Europe & others */
500         D(ETSI3),       /* Europe & others */
501         D(ETSI4),       /* Europe & others */
502         D(ETSI5),       /* Europe & others */
503         D(ETSI6),       /* Europe & others */
504         D(ETSIA),       /* France */
505         D(ETSIB),       /* Israel */
506         D(ETSIC),       /* Latin America */
507
508         D(FCC1),        /* US & others */
509         D(FCC2),
510         D(FCC3),        /* US w/new middle band & DFS */    
511         D(FCC4),        /* US Public Safety */
512         D(FCCA),         
513
514         D(APLD),        /* South Korea */
515
516         D(MKK1),        /* Japan (UNI-1 odd)*/
517         D(MKK2),        /* Japan (4.9 GHz + UNI-1 odd) */
518         D(MKK3),        /* Japan (UNI-1 even) */
519         D(MKK4),        /* Japan (UNI-1 even + UNI-2) */
520         D(MKK5),        /* Japan (UNI-1 even + UNI-2 + mid-band) */
521         D(MKK6),        /* Japan (UNI-1 odd + UNI-1 even) */
522         D(MKK7),        /* Japan (UNI-1 odd + UNI-1 even + UNI-2 */
523         D(MKK8),        /* Japan (UNI-1 odd + UNI-1 even + UNI-2 + mid-band) */
524         D(MKK9),       /* Japan (UNI-1 even + 4.9 GHZ) */
525         D(MKK10),       /* Japan (UNI-1 even + UNI-2 + 4.9 GHZ) */
526         D(MKKA),        /* Japan */
527         D(MKKC),
528
529         D(NULL1),
530         D(WORLD),
531         D(DEBUG_REG_DMN),
532 #undef D
533 };
534
535 static HAL_BOOL
536 rdlookup(const char *name, HAL_REG_DOMAIN *rd)
537 {
538 #define N(a)    (sizeof(a)/sizeof(a[0]))
539         int i;
540
541         for (i = 0; i < N(domains); i++)
542                 if (strcasecmp(domains[i].name, name) == 0) {
543                         *rd = domains[i].rd;
544                         return AH_TRUE;
545                 }
546         return AH_FALSE;
547 #undef N
548 }
549
550 static const char *
551 getrdname(HAL_REG_DOMAIN rd)
552 {
553 #define N(a)    (sizeof(a)/sizeof(a[0]))
554         int i;
555
556         for (i = 0; i < N(domains); i++)
557                 if (domains[i].rd == rd)
558                         return domains[i].name;
559         return NULL;
560 #undef N
561 }
562
563 static void
564 rdlist()
565 {
566 #define N(a)    (sizeof(a)/sizeof(a[0]))
567         int i;
568
569         printf("\nRegulatory domains:\n\n");
570         for (i = 0; i < N(domains); i++)
571                 printf("%-15s%s", domains[i].name,
572                         ((i+1)%5) == 0 ? "\n" : "");
573         printf("\n");
574 #undef N
575 }
576
577 typedef struct {
578         HAL_CTRY_CODE   countryCode;       
579         HAL_REG_DOMAIN  regDmnEnum;
580         const char*     isoName;
581         const char*     name;
582 } COUNTRY_CODE_TO_ENUM_RD;
583  
584 /*
585  * Country Code Table to Enumerated RD
586  */
587 static COUNTRY_CODE_TO_ENUM_RD allCountries[] = {
588     {CTRY_DEBUG,       NO_ENUMRD,     "DB", "DEBUG" },
589     {CTRY_DEFAULT,     DEF_REGDMN,    "NA", "NO_COUNTRY_SET" },
590     {CTRY_ALBANIA,     NULL1_WORLD,   "AL", "ALBANIA" },
591     {CTRY_ALGERIA,     NULL1_WORLD,   "DZ", "ALGERIA" },
592     {CTRY_ARGENTINA,   APL3_WORLD,    "AR", "ARGENTINA" },
593     {CTRY_ARMENIA,     ETSI4_WORLD,   "AM", "ARMENIA" },
594     {CTRY_AUSTRALIA,   FCC2_WORLD,    "AU", "AUSTRALIA" },
595     {CTRY_AUSTRIA,     ETSI1_WORLD,   "AT", "AUSTRIA" },
596     {CTRY_AZERBAIJAN,  ETSI4_WORLD,   "AZ", "AZERBAIJAN" },
597     {CTRY_BAHRAIN,     APL6_WORLD,   "BH", "BAHRAIN" },
598     {CTRY_BELARUS,     NULL1_WORLD,   "BY", "BELARUS" },
599     {CTRY_BELGIUM,     ETSI1_WORLD,   "BE", "BELGIUM" },
600     {CTRY_BELIZE,      APL1_ETSIC,    "BZ", "BELIZE" },
601     {CTRY_BOLIVIA,     APL1_ETSIC,    "BO", "BOLVIA" },
602     {CTRY_BRAZIL,      FCC3_WORLD,    "BR", "BRAZIL" },
603     {CTRY_BRUNEI_DARUSSALAM,APL1_WORLD,"BN", "BRUNEI DARUSSALAM" },
604     {CTRY_BULGARIA,    ETSI6_WORLD,   "BG", "BULGARIA" },
605     {CTRY_CANADA,      FCC2_FCCA,     "CA", "CANADA" },
606     {CTRY_CHILE,       APL6_WORLD,    "CL", "CHILE" },
607     {CTRY_CHINA,       APL1_WORLD,    "CN", "CHINA" },
608     {CTRY_COLOMBIA,    FCC1_FCCA,     "CO", "COLOMBIA" },
609     {CTRY_COSTA_RICA,  NULL1_WORLD,   "CR", "COSTA RICA" },
610     {CTRY_CROATIA,     ETSI3_WORLD,   "HR", "CROATIA" },
611     {CTRY_CYPRUS,      ETSI1_WORLD,   "CY", "CYPRUS" },
612     {CTRY_CZECH,       ETSI3_WORLD,   "CZ", "CZECH REPUBLIC" },
613     {CTRY_DENMARK,     ETSI1_WORLD,   "DK", "DENMARK" },
614     {CTRY_DOMINICAN_REPUBLIC,FCC1_FCCA,"DO", "DOMINICAN REPUBLIC" },
615     {CTRY_ECUADOR,     NULL1_WORLD,   "EC", "ECUADOR" },
616     {CTRY_EGYPT,       ETSI3_WORLD,   "EG", "EGYPT" },
617     {CTRY_EL_SALVADOR, NULL1_WORLD,   "SV", "EL SALVADOR" },    
618     {CTRY_ESTONIA,     ETSI1_WORLD,   "EE", "ESTONIA" },
619     {CTRY_FINLAND,     ETSI1_WORLD,   "FI", "FINLAND" },
620     {CTRY_FRANCE,      ETSI3_WORLD,   "FR", "FRANCE" },
621     {CTRY_FRANCE2,     ETSI3_WORLD,   "F2", "FRANCE_RES" },
622     {CTRY_GEORGIA,     ETSI4_WORLD,   "GE", "GEORGIA" },
623     {CTRY_GERMANY,     ETSI1_WORLD,   "DE", "GERMANY" },
624     {CTRY_GREECE,      ETSI1_WORLD,   "GR", "GREECE" },
625     {CTRY_GUATEMALA,   FCC1_FCCA,     "GT", "GUATEMALA" },
626     {CTRY_HONDURAS,    NULL1_WORLD,   "HN", "HONDURAS" },
627     {CTRY_HONG_KONG,   FCC2_WORLD,    "HK", "HONG KONG" },
628     {CTRY_HUNGARY,     ETSI1_WORLD,   "HU", "HUNGARY" },
629     {CTRY_ICELAND,     ETSI1_WORLD,   "IS", "ICELAND" },
630     {CTRY_INDIA,       APL6_WORLD,    "IN", "INDIA" },
631     {CTRY_INDONESIA,   APL1_WORLD,    "ID", "INDONESIA" },
632     {CTRY_IRAN,        APL1_WORLD,    "IR", "IRAN" },
633     {CTRY_IRELAND,     ETSI1_WORLD,   "IE", "IRELAND" },
634     {CTRY_ISRAEL,      NULL1_WORLD,   "IL", "ISRAEL" },
635     {CTRY_ITALY,       ETSI1_WORLD,   "IT", "ITALY" },
636     {CTRY_JAPAN,       MKK1_MKKA,     "JP", "JAPAN" },
637     {CTRY_JAPAN1,      MKK1_MKKB,     "JP", "JAPAN1" },
638     {CTRY_JAPAN2,      MKK1_FCCA,     "JP", "JAPAN2" },    
639     {CTRY_JAPAN3,      MKK2_MKKA,     "JP", "JAPAN3" },
640     {CTRY_JAPAN4,      MKK1_MKKA1,    "JP", "JAPAN4" },
641     {CTRY_JAPAN5,      MKK1_MKKA2,    "JP", "JAPAN5" },    
642     {CTRY_JAPAN6,      MKK1_MKKC,     "JP", "JAPAN6" },    
643
644     {CTRY_JAPAN7,      MKK3_MKKB,     "JP", "JAPAN7" },
645     {CTRY_JAPAN8,      MKK3_MKKA2,    "JP", "JAPAN8" },    
646     {CTRY_JAPAN9,      MKK3_MKKC,     "JP", "JAPAN9" },    
647
648     {CTRY_JAPAN10,      MKK4_MKKB,     "JP", "JAPAN10" },
649     {CTRY_JAPAN11,      MKK4_MKKA2,    "JP", "JAPAN11" },    
650     {CTRY_JAPAN12,      MKK4_MKKC,     "JP", "JAPAN12" },    
651
652     {CTRY_JAPAN13,      MKK5_MKKB,     "JP", "JAPAN13" },
653     {CTRY_JAPAN14,      MKK5_MKKA2,    "JP", "JAPAN14" },    
654     {CTRY_JAPAN15,      MKK5_MKKC,     "JP", "JAPAN15" },    
655
656     {CTRY_JAPAN16,      MKK6_MKKB,     "JP", "JAPAN16" },
657     {CTRY_JAPAN17,      MKK6_MKKA2,    "JP", "JAPAN17" },    
658     {CTRY_JAPAN18,      MKK6_MKKC,     "JP", "JAPAN18" },    
659
660     {CTRY_JAPAN19,      MKK7_MKKB,     "JP", "JAPAN19" },
661     {CTRY_JAPAN20,      MKK7_MKKA2,    "JP", "JAPAN20" },    
662     {CTRY_JAPAN21,      MKK7_MKKC,     "JP", "JAPAN21" },    
663
664     {CTRY_JAPAN22,      MKK8_MKKB,     "JP", "JAPAN22" },
665     {CTRY_JAPAN23,      MKK8_MKKA2,    "JP", "JAPAN23" },    
666     {CTRY_JAPAN24,      MKK8_MKKC,     "JP", "JAPAN24" },    
667
668     {CTRY_JORDAN,      APL4_WORLD,    "JO", "JORDAN" },
669     {CTRY_KAZAKHSTAN,  NULL1_WORLD,   "KZ", "KAZAKHSTAN" },
670     {CTRY_KOREA_NORTH, APL2_WORLD,    "KP", "NORTH KOREA" },
671     {CTRY_KOREA_ROC,   APL2_WORLD,    "KR", "KOREA REPUBLIC" },
672     {CTRY_KOREA_ROC2,  APL2_WORLD,    "K2", "KOREA REPUBLIC2" },
673     {CTRY_KOREA_ROC3,  APL9_WORLD,    "K3", "KOREA REPUBLIC3" },
674     {CTRY_KUWAIT,      NULL1_WORLD,   "KW", "KUWAIT" },
675     {CTRY_LATVIA,      ETSI1_WORLD,   "LV", "LATVIA" },
676     {CTRY_LEBANON,     NULL1_WORLD,   "LB", "LEBANON" },
677     {CTRY_LIECHTENSTEIN,ETSI1_WORLD,  "LI", "LIECHTENSTEIN" },
678     {CTRY_LITHUANIA,   ETSI1_WORLD,   "LT", "LITHUANIA" },
679     {CTRY_LUXEMBOURG,  ETSI1_WORLD,   "LU", "LUXEMBOURG" },
680     {CTRY_MACAU,       FCC2_WORLD,    "MO", "MACAU" },
681     {CTRY_MACEDONIA,   NULL1_WORLD,   "MK", "MACEDONIA" },
682     {CTRY_MALAYSIA,    APL8_WORLD,    "MY", "MALAYSIA" },
683     {CTRY_MALTA,       ETSI1_WORLD,   "MT", "MALTA" },
684     {CTRY_MEXICO,      FCC1_FCCA,     "MX", "MEXICO" },
685     {CTRY_MONACO,      ETSI4_WORLD,   "MC", "MONACO" },
686     {CTRY_MOROCCO,     NULL1_WORLD,   "MA", "MOROCCO" },
687     {CTRY_NETHERLANDS, ETSI1_WORLD,   "NL", "NETHERLANDS" },
688     {CTRY_NEW_ZEALAND, FCC2_ETSIC,    "NZ", "NEW ZEALAND" },
689     {CTRY_NORWAY,      ETSI1_WORLD,   "NO", "NORWAY" },
690     {CTRY_OMAN,        APL6_WORLD,    "OM", "OMAN" },
691     {CTRY_PAKISTAN,    NULL1_WORLD,   "PK", "PAKISTAN" },
692     {CTRY_PANAMA,      FCC1_FCCA,     "PA", "PANAMA" },
693     {CTRY_PERU,        APL1_WORLD,    "PE", "PERU" },
694     {CTRY_PHILIPPINES, APL1_WORLD,    "PH", "PHILIPPINES" },
695     {CTRY_POLAND,      ETSI1_WORLD,   "PL", "POLAND" },
696     {CTRY_PORTUGAL,    ETSI1_WORLD,   "PT", "PORTUGAL" },
697     {CTRY_PUERTO_RICO, FCC1_FCCA,     "PR", "PUERTO RICO" },
698     {CTRY_QATAR,       NULL1_WORLD,   "QA", "QATAR" },
699     {CTRY_ROMANIA,     NULL1_WORLD,   "RO", "ROMANIA" },
700     {CTRY_RUSSIA,      NULL1_WORLD,   "RU", "RUSSIA" },
701     {CTRY_SAUDI_ARABIA,NULL1_WORLD,   "SA", "SAUDI ARABIA" },
702     {CTRY_SINGAPORE,   APL6_WORLD,    "SG", "SINGAPORE" },
703     {CTRY_SLOVAKIA,    ETSI1_WORLD,   "SK", "SLOVAK REPUBLIC" },
704     {CTRY_SLOVENIA,    ETSI1_WORLD,   "SI", "SLOVENIA" },
705     {CTRY_SOUTH_AFRICA,FCC3_WORLD,    "ZA", "SOUTH AFRICA" },
706     {CTRY_SPAIN,       ETSI1_WORLD,   "ES", "SPAIN" },
707     {CTRY_SWEDEN,      ETSI1_WORLD,   "SE", "SWEDEN" },
708     {CTRY_SWITZERLAND, ETSI1_WORLD,   "CH", "SWITZERLAND" },
709     {CTRY_SYRIA,       NULL1_WORLD,   "SY", "SYRIA" },
710     {CTRY_TAIWAN,      APL3_FCCA,    "TW", "TAIWAN" },
711     {CTRY_THAILAND,    NULL1_WORLD,   "TH", "THAILAND" },
712     {CTRY_TRINIDAD_Y_TOBAGO,ETSI4_WORLD,"TT", "TRINIDAD & TOBAGO" },
713     {CTRY_TUNISIA,     ETSI3_WORLD,   "TN", "TUNISIA" },
714     {CTRY_TURKEY,      ETSI3_WORLD,   "TR", "TURKEY" },
715     {CTRY_UKRAINE,     NULL1_WORLD,   "UA", "UKRAINE" },
716     {CTRY_UAE,         NULL1_WORLD,   "AE", "UNITED ARAB EMIRATES" },
717     {CTRY_UNITED_KINGDOM, ETSI1_WORLD,"GB", "UNITED KINGDOM" },
718     {CTRY_UNITED_STATES, FCC1_FCCA,   "US", "UNITED STATES" },
719     {CTRY_UNITED_STATES_FCC49, FCC4_FCCA,   "PS", "UNITED STATES (PUBLIC SAFETY)" },
720     {CTRY_URUGUAY,     APL2_WORLD,    "UY", "URUGUAY" },
721     {CTRY_UZBEKISTAN,  FCC3_FCCA,     "UZ", "UZBEKISTAN" },    
722     {CTRY_VENEZUELA,   APL2_ETSIC,    "VE", "VENEZUELA" },
723     {CTRY_VIET_NAM,    NULL1_WORLD,   "VN", "VIET NAM" },
724     {CTRY_YEMEN,       NULL1_WORLD,   "YE", "YEMEN" },
725     {CTRY_ZIMBABWE,    NULL1_WORLD,   "ZW", "ZIMBABWE" }    
726 };
727
728 static HAL_BOOL
729 cclookup(const char *name, HAL_REG_DOMAIN *rd, HAL_CTRY_CODE *cc)
730 {
731 #define N(a)    (sizeof(a)/sizeof(a[0]))
732         int i;
733
734         for (i = 0; i < N(allCountries); i++)
735                 if (strcasecmp(allCountries[i].isoName, name) == 0 ||
736                     strcasecmp(allCountries[i].name, name) == 0) {
737                         *rd = allCountries[i].regDmnEnum;
738                         *cc = allCountries[i].countryCode;
739                         return AH_TRUE;
740                 }
741         return AH_FALSE;
742 #undef N
743 }
744
745 static const char *
746 getccname(HAL_CTRY_CODE cc)
747 {
748 #define N(a)    (sizeof(a)/sizeof(a[0]))
749         int i;
750
751         for (i = 0; i < N(allCountries); i++)
752                 if (allCountries[i].countryCode == cc)
753                         return allCountries[i].name;
754         return NULL;
755 #undef N
756 }
757
758 static const char *
759 getccisoname(HAL_CTRY_CODE cc)
760 {
761 #define N(a)    (sizeof(a)/sizeof(a[0]))
762         int i;
763
764         for (i = 0; i < N(allCountries); i++)
765                 if (allCountries[i].countryCode == cc)
766                         return allCountries[i].isoName;
767         return NULL;
768 #undef N
769 }
770
771 static void
772 cclist()
773 {
774 #define N(a)    (sizeof(a)/sizeof(a[0]))
775         int i;
776
777         printf("\nCountry codes:\n");
778         for (i = 0; i < N(allCountries); i++)
779                 printf("%2s %-15.15s%s",
780                         allCountries[i].isoName,
781                         allCountries[i].name,
782                         ((i+1)%4) == 0 ? "\n" : " ");
783         printf("\n");
784 #undef N
785 }
786
787 static HAL_BOOL
788 setRateTable(struct ath_hal *ah, const struct ieee80211_channel *chan, 
789                    int16_t tpcScaleReduction, int16_t powerLimit,
790                    int16_t *pMinPower, int16_t *pMaxPower);
791
792 static void
793 calctxpower(struct ath_hal *ah,
794         int nchan, const struct ieee80211_channel *chans,
795         int16_t tpcScaleReduction, int16_t powerLimit, int16_t *txpow)
796 {
797         int16_t minpow;
798         int i;
799
800         for (i = 0; i < nchan; i++)
801                 if (!setRateTable(ah, &chans[i],
802                     tpcScaleReduction, powerLimit, &minpow, &txpow[i])) {
803                         printf("unable to set rate table\n");
804                         exit(-1);
805                 }
806 }
807
808 int     n = 1;
809 const char *sep = "";
810 int     dopassive = 0;
811 int     showchannels = 0;
812 int     isdfs = 0;
813 int     is4ms = 0;
814
815 static int
816 anychan(const struct ieee80211_channel *chans, int nc, int flag)
817 {
818         int i;
819
820         for (i = 0; i < nc; i++)
821                 if ((chans[i].ic_flags & flag) != 0)
822                         return 1;
823         return 0;
824 }
825
826 static __inline int
827 mapgsm(u_int freq, u_int flags)
828 {
829         freq *= 10;
830         if (flags & IEEE80211_CHAN_QUARTER)
831                 freq += 5;
832         else if (flags & IEEE80211_CHAN_HALF)
833                 freq += 10;
834         else
835                 freq += 20;
836         return (freq - 24220) / 5;
837 }
838
839 static __inline int
840 mappsb(u_int freq, u_int flags)
841 {
842         return ((freq * 10) + (((freq % 5) == 2) ? 5 : 0) - 49400) / 5;
843 }
844
845 /*
846  * Convert GHz frequency to IEEE channel number.
847  */
848 int
849 ath_hal_mhz2ieee(struct ath_hal *ah, u_int freq, u_int flags)
850 {
851         if (flags & IEEE80211_CHAN_2GHZ) {      /* 2GHz band */
852                 if (freq == 2484)
853                         return 14;
854                 if (freq < 2484)
855                         return ((int)freq - 2407) / 5;
856                 else
857                         return 15 + ((freq - 2512) / 20);
858         } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
859                 if (IS_CHAN_IN_PUBLIC_SAFETY_BAND(freq))
860                         return mappsb(freq, flags);
861                 else if ((flags & IEEE80211_CHAN_A) && (freq <= 5000))
862                         return (freq - 4000) / 5;
863                 else
864                         return (freq - 5000) / 5;
865         } else {                        /* either, guess */
866                 if (freq == 2484)
867                         return 14;
868                 if (freq < 2484)
869                         return ((int)freq - 2407) / 5;
870                 if (freq < 5000) {
871                         if (IS_CHAN_IN_PUBLIC_SAFETY_BAND(freq))
872                                 return mappsb(freq, flags);
873                         else if (freq > 4900)
874                                 return (freq - 4000) / 5;
875                         else
876                                 return 15 + ((freq - 2512) / 20);
877                 }
878                 return (freq - 5000) / 5;
879         }
880 }
881
882 #define IEEE80211_IS_CHAN_4MS(_c) \
883         (((_c)->ic_flags & IEEE80211_CHAN_4MSXMIT) != 0)
884
885 static void
886 dumpchannels(struct ath_hal *ah, int nc,
887         const struct ieee80211_channel *chans, int16_t *txpow)
888 {
889         int i;
890
891         for (i = 0; i < nc; i++) {
892                 const struct ieee80211_channel *c = &chans[i];
893                 int type;
894
895                 if (showchannels)
896                         printf("%s%3d", sep,
897                             ath_hal_mhz2ieee(ah, c->ic_freq, c->ic_flags));
898                 else
899                         printf("%s%u", sep, c->ic_freq);
900                 if (IEEE80211_IS_CHAN_HALF(c))
901                         type = 'H';
902                 else if (IEEE80211_IS_CHAN_QUARTER(c))
903                         type = 'Q';
904                 else if (IEEE80211_IS_CHAN_TURBO(c))
905                         type = 'T';
906                 else if (IEEE80211_IS_CHAN_HT(c))
907                         type = 'N';
908                 else if (IEEE80211_IS_CHAN_A(c))
909                         type = 'A';
910                 else if (IEEE80211_IS_CHAN_108G(c))
911                         type = 'T';
912                 else if (IEEE80211_IS_CHAN_G(c))
913                         type = 'G';
914                 else
915                         type = 'B';
916                 if (dopassive && IEEE80211_IS_CHAN_PASSIVE(c))
917                         type = tolower(type);
918                 if (isdfs && is4ms)
919                         printf("%c%c%c %d.%d", type,
920                             IEEE80211_IS_CHAN_DFS(c) ? '*' : ' ',
921                             IEEE80211_IS_CHAN_4MS(c) ? '4' : ' ',
922                             txpow[i]/2, (txpow[i]%2)*5);
923                 else if (isdfs)
924                         printf("%c%c %d.%d", type,
925                             IEEE80211_IS_CHAN_DFS(c) ? '*' : ' ',
926                             txpow[i]/2, (txpow[i]%2)*5);
927                 else if (is4ms)
928                         printf("%c%c %d.%d", type,
929                             IEEE80211_IS_CHAN_4MS(c) ? '4' : ' ',
930                             txpow[i]/2, (txpow[i]%2)*5);
931                 else
932                         printf("%c %d.%d", type, txpow[i]/2, (txpow[i]%2)*5);
933                 if ((n++ % (showchannels ? 7 : 6)) == 0)
934                         sep = "\n";
935                 else
936                         sep = " ";
937         }
938 }
939
940 static void
941 intersect(struct ieee80211_channel *dst, int16_t *dtxpow, int *nd,
942     const struct ieee80211_channel *src, int16_t *stxpow, int ns)
943 {
944         int i = 0, j, k, l;
945         while (i < *nd) {
946                 for (j = 0; j < ns && dst[i].ic_freq != src[j].ic_freq; j++)
947                         ;
948                 if (j < ns && dtxpow[i] == stxpow[j]) {
949                         for (k = i+1, l = i; k < *nd; k++, l++)
950                                 dst[l] = dst[k];
951                         (*nd)--;
952                 } else
953                         i++;
954         }
955 }
956
957 static void
958 usage(const char *progname)
959 {
960         printf("usage: %s [-acdefoilpr4ABGT] [-m opmode] [cc | rd]\n", progname);
961         exit(-1);
962 }
963
964 static HAL_BOOL
965 getChipPowerLimits(struct ath_hal *ah, struct ieee80211_channel *chan)
966 {
967 }
968
969 static HAL_BOOL
970 eepromRead(struct ath_hal *ah, u_int off, u_int16_t *data)
971 {
972         /* emulate enough stuff to handle japan channel shift */
973         switch (off) {
974         case AR_EEPROM_VERSION:
975                 *data = eeversion;
976                 return AH_TRUE;
977         case AR_EEPROM_REG_CAPABILITIES_OFFSET:
978                 *data = AR_EEPROM_EEREGCAP_EN_KK_NEW_11A;
979                 return AH_TRUE;
980         case AR_EEPROM_REG_CAPABILITIES_OFFSET_PRE4_0:
981                 *data = AR_EEPROM_EEREGCAP_EN_KK_NEW_11A_PRE4_0;
982                 return AH_TRUE;
983         }
984         return AH_FALSE;
985 }
986
987 HAL_STATUS
988 getCapability(struct ath_hal *ah, HAL_CAPABILITY_TYPE type,
989         uint32_t capability, uint32_t *result)
990 {
991         const HAL_CAPABILITIES *pCap = &AH_PRIVATE(ah)->ah_caps;
992
993         switch (type) {
994         case HAL_CAP_REG_DMN:           /* regulatory domain */
995                 *result = AH_PRIVATE(ah)->ah_currentRD;
996                 return HAL_OK;
997         default:
998                 return HAL_EINVAL;
999         }
1000 }
1001
1002 #define HAL_MODE_HT20 \
1003         (HAL_MODE_11NG_HT20 |  HAL_MODE_11NA_HT20)
1004 #define HAL_MODE_HT40 \
1005         (HAL_MODE_11NG_HT40PLUS | HAL_MODE_11NG_HT40MINUS | \
1006          HAL_MODE_11NA_HT40PLUS | HAL_MODE_11NA_HT40MINUS)
1007 #define HAL_MODE_HT     (HAL_MODE_HT20 | HAL_MODE_HT40)
1008      
1009 int
1010 main(int argc, char *argv[])
1011 {
1012         static const u_int16_t tpcScaleReductionTable[5] =
1013                 { 0, 3, 6, 9, MAX_RATE_POWER };
1014         struct ath_hal_private ahp;
1015         struct ieee80211_channel achans[IEEE80211_CHAN_MAX];
1016         int16_t atxpow[IEEE80211_CHAN_MAX];
1017         struct ieee80211_channel bchans[IEEE80211_CHAN_MAX];
1018         int16_t btxpow[IEEE80211_CHAN_MAX];
1019         struct ieee80211_channel gchans[IEEE80211_CHAN_MAX];
1020         int16_t gtxpow[IEEE80211_CHAN_MAX];
1021         struct ieee80211_channel tchans[IEEE80211_CHAN_MAX];
1022         int16_t ttxpow[IEEE80211_CHAN_MAX];
1023         struct ieee80211_channel tgchans[IEEE80211_CHAN_MAX];
1024         int16_t tgtxpow[IEEE80211_CHAN_MAX];
1025         struct ieee80211_channel nchans[IEEE80211_CHAN_MAX];
1026         int16_t ntxpow[IEEE80211_CHAN_MAX];
1027         int i, na, nb, ng, nt, ntg, nn;
1028         HAL_BOOL showall = AH_FALSE;
1029         HAL_BOOL extendedChanMode = AH_TRUE;
1030         int modes = 0;
1031         int16_t tpcReduction, powerLimit;
1032         int showdfs = 0;
1033         int show4ms = 0;
1034
1035         memset(&ahp, 0, sizeof(ahp));
1036         ahp.ah_getChannelEdges = getChannelEdges;
1037         ahp.ah_getWirelessModes = getWirelessModes;
1038         ahp.ah_eepromRead = eepromRead;
1039         ahp.ah_getChipPowerLimits = getChipPowerLimits;
1040         ahp.ah_caps.halWirelessModes = HAL_MODE_ALL;
1041         ahp.ah_caps.halLow5GhzChan = 4920;
1042         ahp.ah_caps.halHigh5GhzChan = 6100;
1043         ahp.ah_caps.halLow2GhzChan = 2312;
1044         ahp.ah_caps.halHigh2GhzChan = 2732;
1045         ahp.ah_caps.halChanHalfRate = AH_TRUE;
1046         ahp.ah_caps.halChanQuarterRate = AH_TRUE;
1047         ahp.h.ah_getCapability = getCapability;
1048         ahp.ah_opmode = HAL_M_STA;
1049
1050         tpcReduction = tpcScaleReductionTable[0];
1051         powerLimit =  MAX_RATE_POWER;
1052
1053         while ((i = getopt(argc, argv, "acdeflm:pr4ABGhHNT")) != -1)
1054                 switch (i) {
1055                 case 'a':
1056                         showall = AH_TRUE;
1057                         break;
1058                 case 'c':
1059                         showchannels = AH_TRUE;
1060                         break;
1061                 case 'd':
1062                         ath_hal_debug = HAL_DEBUG_ANY;
1063                         break;
1064                 case 'e':
1065                         extendedChanMode = AH_FALSE;
1066                         break;
1067                 case 'f':
1068                         showchannels = AH_FALSE;
1069                         break;
1070                 case 'l':
1071                         cclist();
1072                         rdlist();
1073                         exit(0);
1074                 case 'm':
1075                         if (strncasecmp(optarg, "sta", 2) == 0)
1076                                 ahp.ah_opmode = HAL_M_STA;
1077                         else if (strncasecmp(optarg, "ibss", 2) == 0)
1078                                 ahp.ah_opmode = HAL_M_IBSS;
1079                         else if (strncasecmp(optarg, "adhoc", 2) == 0)
1080                                 ahp.ah_opmode = HAL_M_IBSS;
1081                         else if (strncasecmp(optarg, "ap", 2) == 0)
1082                                 ahp.ah_opmode = HAL_M_HOSTAP;
1083                         else if (strncasecmp(optarg, "hostap", 2) == 0)
1084                                 ahp.ah_opmode = HAL_M_HOSTAP;
1085                         else if (strncasecmp(optarg, "monitor", 2) == 0)
1086                                 ahp.ah_opmode = HAL_M_MONITOR;
1087                         else
1088                                 usage(argv[0]);
1089                         break;
1090                 case 'p':
1091                         dopassive = 1;
1092                         break;
1093                 case 'A':
1094                         modes |= HAL_MODE_11A;
1095                         break;
1096                 case 'B':
1097                         modes |= HAL_MODE_11B;
1098                         break;
1099                 case 'G':
1100                         modes |= HAL_MODE_11G;
1101                         break;
1102                 case 'h':
1103                         modes |= HAL_MODE_HT20;
1104                         break;
1105                 case 'H':
1106                         modes |= HAL_MODE_HT40;
1107                         break;
1108                 case 'N':
1109                         modes |= HAL_MODE_HT;
1110                         break;
1111                 case 'T':
1112                         modes |= HAL_MODE_TURBO | HAL_MODE_108G;
1113                         break;
1114                 case 'r':
1115                         showdfs = 1;
1116                         break;
1117                 case '4':
1118                         show4ms = 1;
1119                         break;
1120                 default:
1121                         usage(argv[0]);
1122                 }
1123         switch (argc - optind)  {
1124         case 0:
1125                 if (!cclookup("US", &rd, &cc)) {
1126                         printf("%s: unknown country code\n", "US");
1127                         exit(-1);
1128                 }
1129                 break;
1130         case 1:                 /* cc/regdomain */
1131                 if (!cclookup(argv[optind], &rd, &cc)) {
1132                         if (!rdlookup(argv[optind], &rd)) {
1133                                 const char* rdname;
1134
1135                                 rd = strtoul(argv[optind], NULL, 0);
1136                                 rdname = getrdname(rd);
1137                                 if (rdname == NULL) {
1138                                         printf("%s: unknown country/regulatory "
1139                                                 "domain code\n", argv[optind]);
1140                                         exit(-1);
1141                                 }
1142                         }
1143                         cc = CTRY_DEFAULT;
1144                 }
1145                 break;
1146         default:                /* regdomain cc */
1147                 if (!rdlookup(argv[optind], &rd)) {
1148                         const char* rdname;
1149
1150                         rd = strtoul(argv[optind], NULL, 0);
1151                         rdname = getrdname(rd);
1152                         if (rdname == NULL) {
1153                                 printf("%s: unknown country/regulatory "
1154                                         "domain code\n", argv[optind]);
1155                                 exit(-1);
1156                         }
1157                 }
1158                 if (!cclookup(argv[optind+1], &rd, &cc))
1159                         cc = strtoul(argv[optind+1], NULL, 0);
1160                 break;
1161         }
1162         if (cc != CTRY_DEFAULT)
1163                 printf("\n%s (%s, 0x%x, %u) %s (0x%x, %u)\n",
1164                         getccname(cc), getccisoname(cc), cc, cc,
1165                         getrdname(rd), rd, rd);
1166         else
1167                 printf("\n%s (0x%x, %u)\n",
1168                         getrdname(rd), rd, rd);
1169
1170         if (modes == 0) {
1171                 /* NB: no HAL_MODE_HT */
1172                 modes = HAL_MODE_11A | HAL_MODE_11B |
1173                         HAL_MODE_11G | HAL_MODE_TURBO | HAL_MODE_108G;
1174         }
1175         na = nb = ng = nt = ntg = nn = 0;
1176         if (modes & HAL_MODE_11G) {
1177                 ahp.ah_currentRD = rd;
1178                 if (ath_hal_getchannels(&ahp.h, gchans, IEEE80211_CHAN_MAX, &ng,
1179                     HAL_MODE_11G, cc, rd, extendedChanMode) == HAL_OK) {
1180                         calctxpower(&ahp.h, ng, gchans, tpcReduction, powerLimit, gtxpow);
1181                         if (showdfs)
1182                                 isdfs |= anychan(gchans, ng, IEEE80211_CHAN_DFS);
1183                         if (show4ms)
1184                                 is4ms |= anychan(gchans, ng, IEEE80211_CHAN_4MSXMIT);
1185                 }
1186         }
1187         if (modes & HAL_MODE_11B) {
1188                 ahp.ah_currentRD = rd;
1189                 if (ath_hal_getchannels(&ahp.h, bchans, IEEE80211_CHAN_MAX, &nb,
1190                     HAL_MODE_11B, cc, rd, extendedChanMode) == HAL_OK) {
1191                         calctxpower(&ahp.h, nb, bchans, tpcReduction, powerLimit, btxpow);
1192                         if (showdfs)
1193                                 isdfs |= anychan(bchans, nb, IEEE80211_CHAN_DFS);
1194                         if (show4ms)
1195                                 is4ms |= anychan(bchans, nb, IEEE80211_CHAN_4MSXMIT);
1196                 }
1197         }
1198         if (modes & HAL_MODE_11A) {
1199                 ahp.ah_currentRD = rd;
1200                 if (ath_hal_getchannels(&ahp.h, achans, IEEE80211_CHAN_MAX, &na,
1201                     HAL_MODE_11A, cc, rd, extendedChanMode) == HAL_OK) {
1202                         calctxpower(&ahp.h, na, achans, tpcReduction, powerLimit, atxpow);
1203                         if (showdfs)
1204                                 isdfs |= anychan(achans, na, IEEE80211_CHAN_DFS);
1205                         if (show4ms)
1206                                 is4ms |= anychan(achans, na, IEEE80211_CHAN_4MSXMIT);
1207                 }
1208         }
1209         if (modes & HAL_MODE_TURBO) {
1210                 ahp.ah_currentRD = rd;
1211                 if (ath_hal_getchannels(&ahp.h, tchans, IEEE80211_CHAN_MAX, &nt,
1212                     HAL_MODE_TURBO, cc, rd, extendedChanMode) == HAL_OK) {
1213                         calctxpower(&ahp.h, nt, tchans, tpcReduction, powerLimit, ttxpow);
1214                         if (showdfs)
1215                                 isdfs |= anychan(tchans, nt, IEEE80211_CHAN_DFS);
1216                         if (show4ms)
1217                                 is4ms |= anychan(tchans, nt, IEEE80211_CHAN_4MSXMIT);
1218                 }
1219         }       
1220         if (modes & HAL_MODE_108G) {
1221                 ahp.ah_currentRD = rd;
1222                 if (ath_hal_getchannels(&ahp.h, tgchans, IEEE80211_CHAN_MAX, &ntg,
1223                     HAL_MODE_108G, cc, rd, extendedChanMode) == HAL_OK) {
1224                         calctxpower(&ahp.h, ntg, tgchans, tpcReduction, powerLimit, tgtxpow);
1225                         if (showdfs)
1226                                 isdfs |= anychan(tgchans, ntg, IEEE80211_CHAN_DFS);
1227                         if (show4ms)
1228                                 is4ms |= anychan(tgchans, ntg, IEEE80211_CHAN_4MSXMIT);
1229                 }
1230         }
1231         if (modes & HAL_MODE_HT) {
1232                 ahp.ah_currentRD = rd;
1233                 if (ath_hal_getchannels(&ahp.h, nchans, IEEE80211_CHAN_MAX, &nn,
1234                     modes & HAL_MODE_HT, cc, rd, extendedChanMode) == HAL_OK) {
1235                         calctxpower(&ahp.h, nn, nchans, tpcReduction, powerLimit, ntxpow);
1236                         if (showdfs)
1237                                 isdfs |= anychan(nchans, nn, IEEE80211_CHAN_DFS);
1238                         if (show4ms)
1239                                 is4ms |= anychan(nchans, nn, IEEE80211_CHAN_4MSXMIT);
1240                 }
1241         }
1242
1243         if (!showall) {
1244 #define CHECKMODES(_modes, _m)  ((_modes & (_m)) == (_m))
1245                 if (CHECKMODES(modes, HAL_MODE_11B|HAL_MODE_11G)) {
1246                         /* b ^= g */
1247                         intersect(bchans, btxpow, &nb, gchans, gtxpow, ng);
1248                 }
1249                 if (CHECKMODES(modes, HAL_MODE_11A|HAL_MODE_TURBO)) {
1250                         /* t ^= a */
1251                         intersect(tchans, ttxpow, &nt, achans, atxpow, na);
1252                 }
1253                 if (CHECKMODES(modes, HAL_MODE_11G|HAL_MODE_108G)) {
1254                         /* tg ^= g */
1255                         intersect(tgchans, tgtxpow, &ntg, gchans, gtxpow, ng);
1256                 }
1257                 if (CHECKMODES(modes, HAL_MODE_11G|HAL_MODE_HT)) {
1258                         /* g ^= n */
1259                         intersect(gchans, gtxpow, &ng, nchans, ntxpow, nn);
1260                 }
1261                 if (CHECKMODES(modes, HAL_MODE_11A|HAL_MODE_HT)) {
1262                         /* a ^= n */
1263                         intersect(achans, atxpow, &na, nchans, ntxpow, nn);
1264                 }
1265 #undef CHECKMODES
1266         }
1267
1268         if (modes & HAL_MODE_11G)
1269                 dumpchannels(&ahp.h, ng, gchans, gtxpow);
1270         if (modes & HAL_MODE_11B)
1271                 dumpchannels(&ahp.h, nb, bchans, btxpow);
1272         if (modes & HAL_MODE_11A)
1273                 dumpchannels(&ahp.h, na, achans, atxpow);
1274         if (modes & HAL_MODE_108G)
1275                 dumpchannels(&ahp.h, ntg, tgchans, tgtxpow);
1276         if (modes & HAL_MODE_TURBO)
1277                 dumpchannels(&ahp.h, nt, tchans, ttxpow);
1278         if (modes & HAL_MODE_HT)
1279                 dumpchannels(&ahp.h, nn, nchans, ntxpow);
1280         printf("\n");
1281         return (0);
1282 }
1283
1284 /*
1285  * Search a list for a specified value v that is within
1286  * EEP_DELTA of the search values.  Return the closest
1287  * values in the list above and below the desired value.
1288  * EEP_DELTA is a factional value; everything is scaled
1289  * so only integer arithmetic is used.
1290  *
1291  * NB: the input list is assumed to be sorted in ascending order
1292  */
1293 static void
1294 ar5212GetLowerUpperValues(u_int16_t v, u_int16_t *lp, u_int16_t listSize,
1295                           u_int16_t *vlo, u_int16_t *vhi)
1296 {
1297         u_int32_t target = v * EEP_SCALE;
1298         u_int16_t *ep = lp+listSize;
1299
1300         /*
1301          * Check first and last elements for out-of-bounds conditions.
1302          */
1303         if (target < (u_int32_t)(lp[0] * EEP_SCALE - EEP_DELTA)) {
1304                 *vlo = *vhi = lp[0];
1305                 return;
1306         }
1307         if (target > (u_int32_t)(ep[-1] * EEP_SCALE + EEP_DELTA)) {
1308                 *vlo = *vhi = ep[-1];
1309                 return;
1310         }
1311
1312         /* look for value being near or between 2 values in list */
1313         for (; lp < ep; lp++) {
1314                 /*
1315                  * If value is close to the current value of the list
1316                  * then target is not between values, it is one of the values
1317                  */
1318                 if (abs(lp[0] * EEP_SCALE - target) < EEP_DELTA) {
1319                         *vlo = *vhi = lp[0];
1320                         return;
1321                 }
1322                 /*
1323                  * Look for value being between current value and next value
1324                  * if so return these 2 values
1325                  */
1326                 if (target < (u_int32_t)(lp[1] * EEP_SCALE - EEP_DELTA)) {
1327                         *vlo = lp[0];
1328                         *vhi = lp[1];
1329                         return;
1330                 }
1331         }
1332 }
1333
1334 /*
1335  * Find the maximum conformance test limit for the given channel and CTL info
1336  */
1337 static u_int16_t
1338 ar5212GetMaxEdgePower(u_int16_t channel, RD_EDGES_POWER *pRdEdgesPower)
1339 {
1340         /* temp array for holding edge channels */
1341         u_int16_t tempChannelList[NUM_EDGES];
1342         u_int16_t clo, chi, twiceMaxEdgePower;
1343         int i, numEdges;
1344
1345         /* Get the edge power */
1346         for (i = 0; i < NUM_EDGES; i++) {
1347                 if (pRdEdgesPower[i].rdEdge == 0)
1348                         break;
1349                 tempChannelList[i] = pRdEdgesPower[i].rdEdge;
1350         }
1351         numEdges = i;
1352
1353         ar5212GetLowerUpperValues(channel, tempChannelList,
1354                 numEdges, &clo, &chi);
1355         /* Get the index for the lower channel */
1356         for (i = 0; i < numEdges && clo != tempChannelList[i]; i++)
1357                 ;
1358         /* Is lower channel ever outside the rdEdge? */
1359         HALASSERT(i != numEdges);
1360
1361         if ((clo == chi && clo == channel) || (pRdEdgesPower[i].flag)) {
1362                 /* 
1363                  * If there's an exact channel match or an inband flag set
1364                  * on the lower channel use the given rdEdgePower 
1365                  */
1366                 twiceMaxEdgePower = pRdEdgesPower[i].twice_rdEdgePower;
1367                 HALASSERT(twiceMaxEdgePower > 0);
1368         } else
1369                 twiceMaxEdgePower = MAX_RATE_POWER;
1370         return twiceMaxEdgePower;
1371 }
1372
1373 /*
1374  * Returns interpolated or the scaled up interpolated value
1375  */
1376 static u_int16_t
1377 interpolate(u_int16_t target, u_int16_t srcLeft, u_int16_t srcRight,
1378         u_int16_t targetLeft, u_int16_t targetRight)
1379 {
1380         u_int16_t rv;
1381         int16_t lRatio;
1382
1383         /* to get an accurate ratio, always scale, if want to scale, then don't scale back down */
1384         if ((targetLeft * targetRight) == 0)
1385                 return 0;
1386
1387         if (srcRight != srcLeft) {
1388                 /*
1389                  * Note the ratio always need to be scaled,
1390                  * since it will be a fraction.
1391                  */
1392                 lRatio = (target - srcLeft) * EEP_SCALE / (srcRight - srcLeft);
1393                 if (lRatio < 0) {
1394                     /* Return as Left target if value would be negative */
1395                     rv = targetLeft;
1396                 } else if (lRatio > EEP_SCALE) {
1397                     /* Return as Right target if Ratio is greater than 100% (SCALE) */
1398                     rv = targetRight;
1399                 } else {
1400                         rv = (lRatio * targetRight + (EEP_SCALE - lRatio) *
1401                                         targetLeft) / EEP_SCALE;
1402                 }
1403         } else {
1404                 rv = targetLeft;
1405         }
1406         return rv;
1407 }
1408
1409 /*
1410  * Return the four rates of target power for the given target power table 
1411  * channel, and number of channels
1412  */
1413 static void
1414 ar5212GetTargetPowers(struct ath_hal *ah, const struct ieee80211_channel *chan,
1415         TRGT_POWER_INFO *powInfo,
1416         u_int16_t numChannels, TRGT_POWER_INFO *pNewPower)
1417 {
1418         /* temp array for holding target power channels */
1419         u_int16_t tempChannelList[NUM_TEST_FREQUENCIES];
1420         u_int16_t clo, chi, ixlo, ixhi;
1421         int i;
1422
1423         /* Copy the target powers into the temp channel list */
1424         for (i = 0; i < numChannels; i++)
1425                 tempChannelList[i] = powInfo[i].testChannel;
1426
1427         ar5212GetLowerUpperValues(chan->ic_freq, tempChannelList,
1428                 numChannels, &clo, &chi);
1429
1430         /* Get the indices for the channel */
1431         ixlo = ixhi = 0;
1432         for (i = 0; i < numChannels; i++) {
1433                 if (clo == tempChannelList[i]) {
1434                         ixlo = i;
1435                 }
1436                 if (chi == tempChannelList[i]) {
1437                         ixhi = i;
1438                         break;
1439                 }
1440         }
1441
1442         /*
1443          * Get the lower and upper channels, target powers,
1444          * and interpolate between them.
1445          */
1446         pNewPower->twicePwr6_24 = interpolate(chan->ic_freq, clo, chi,
1447                 powInfo[ixlo].twicePwr6_24, powInfo[ixhi].twicePwr6_24);
1448         pNewPower->twicePwr36 = interpolate(chan->ic_freq, clo, chi,
1449                 powInfo[ixlo].twicePwr36, powInfo[ixhi].twicePwr36);
1450         pNewPower->twicePwr48 = interpolate(chan->ic_freq, clo, chi,
1451                 powInfo[ixlo].twicePwr48, powInfo[ixhi].twicePwr48);
1452         pNewPower->twicePwr54 = interpolate(chan->ic_freq, clo, chi,
1453                 powInfo[ixlo].twicePwr54, powInfo[ixhi].twicePwr54);
1454 }
1455
1456 static RD_EDGES_POWER*
1457 findEdgePower(struct ath_hal *ah, u_int ctl)
1458 {
1459         int i;
1460
1461         for (i = 0; i < _numCtls; i++)
1462                 if (_ctl[i] == ctl)
1463                         return &_rdEdgesPower[i * NUM_EDGES];
1464         return AH_NULL;
1465 }
1466
1467 /*
1468  * Sets the transmit power in the baseband for the given
1469  * operating channel and mode.
1470  */
1471 static HAL_BOOL
1472 setRateTable(struct ath_hal *ah, const struct ieee80211_channel *chan, 
1473                    int16_t tpcScaleReduction, int16_t powerLimit,
1474                    int16_t *pMinPower, int16_t *pMaxPower)
1475 {
1476         u_int16_t ratesArray[16];
1477         u_int16_t *rpow = ratesArray;
1478         u_int16_t twiceMaxRDPower, twiceMaxEdgePower, twiceMaxEdgePowerCck;
1479         int8_t twiceAntennaGain, twiceAntennaReduction;
1480         TRGT_POWER_INFO targetPowerOfdm, targetPowerCck;
1481         RD_EDGES_POWER *rep;
1482         int16_t scaledPower;
1483         u_int8_t cfgCtl;
1484
1485         twiceMaxRDPower = chan->ic_maxregpower * 2;
1486         *pMaxPower = -MAX_RATE_POWER;
1487         *pMinPower = MAX_RATE_POWER;
1488
1489         /* Get conformance test limit maximum for this channel */
1490         cfgCtl = ath_hal_getctl(ah, chan);
1491         rep = findEdgePower(ah, cfgCtl);
1492         if (rep != AH_NULL)
1493                 twiceMaxEdgePower = ar5212GetMaxEdgePower(chan->ic_freq, rep);
1494         else
1495                 twiceMaxEdgePower = MAX_RATE_POWER;
1496
1497         if (IEEE80211_IS_CHAN_G(chan)) {
1498                 /* Check for a CCK CTL for 11G CCK powers */
1499                 cfgCtl = (cfgCtl & 0xFC) | 0x01;
1500                 rep = findEdgePower(ah, cfgCtl);
1501                 if (rep != AH_NULL)
1502                         twiceMaxEdgePowerCck = ar5212GetMaxEdgePower(chan->ic_freq, rep);
1503                 else
1504                         twiceMaxEdgePowerCck = MAX_RATE_POWER;
1505         } else {
1506                 /* Set the 11B cck edge power to the one found before */
1507                 twiceMaxEdgePowerCck = twiceMaxEdgePower;
1508         }
1509
1510         /* Get Antenna Gain reduction */
1511         if (IEEE80211_IS_CHAN_5GHZ(chan)) {
1512                 twiceAntennaGain = antennaGainMax[0];
1513         } else {
1514                 twiceAntennaGain = antennaGainMax[1];
1515         }
1516         twiceAntennaReduction =
1517                 ath_hal_getantennareduction(ah, chan, twiceAntennaGain);
1518
1519         if (IEEE80211_IS_CHAN_OFDM(chan)) {
1520                 /* Get final OFDM target powers */
1521                 if (IEEE80211_IS_CHAN_G(chan)) { 
1522                         /* TODO - add Turbo 2.4 to this mode check */
1523                         ar5212GetTargetPowers(ah, chan, trgtPwr_11g,
1524                                 numTargetPwr_11g, &targetPowerOfdm);
1525                 } else {
1526                         ar5212GetTargetPowers(ah, chan, trgtPwr_11a,
1527                                 numTargetPwr_11a, &targetPowerOfdm);
1528                 }
1529
1530                 /* Get Maximum OFDM power */
1531                 /* Minimum of target and edge powers */
1532                 scaledPower = AH_MIN(twiceMaxEdgePower,
1533                                 twiceMaxRDPower - twiceAntennaReduction);
1534
1535                 /*
1536                  * If turbo is set, reduce power to keep power
1537                  * consumption under 2 Watts.  Note that we always do
1538                  * this unless specially configured.  Then we limit
1539                  * power only for non-AP operation.
1540                  */
1541                 if (IEEE80211_IS_CHAN_TURBO(chan)
1542 #ifdef AH_ENABLE_AP_SUPPORT
1543                     && AH_PRIVATE(ah)->ah_opmode != HAL_M_HOSTAP
1544 #endif
1545                 ) {
1546                         /*
1547                          * If turbo is set, reduce power to keep power
1548                          * consumption under 2 Watts
1549                          */
1550                         if (eeversion >= AR_EEPROM_VER3_1)
1551                                 scaledPower = AH_MIN(scaledPower,
1552                                         turbo2WMaxPower5);
1553                         /*
1554                          * EEPROM version 4.0 added an additional
1555                          * constraint on 2.4GHz channels.
1556                          */
1557                         if (eeversion >= AR_EEPROM_VER4_0 &&
1558                             IEEE80211_IS_CHAN_2GHZ(chan))
1559                                 scaledPower = AH_MIN(scaledPower,
1560                                         turbo2WMaxPower2);
1561                 }
1562                 /* Reduce power by max regulatory domain allowed restrictions */
1563                 scaledPower -= (tpcScaleReduction * 2);
1564                 scaledPower = (scaledPower < 0) ? 0 : scaledPower;
1565                 scaledPower = AH_MIN(scaledPower, powerLimit);
1566
1567                 scaledPower = AH_MIN(scaledPower, targetPowerOfdm.twicePwr6_24);
1568
1569                 /* Set OFDM rates 9, 12, 18, 24, 36, 48, 54, XR */
1570                 rpow[0] = rpow[1] = rpow[2] = rpow[3] = rpow[4] = scaledPower;
1571                 rpow[5] = AH_MIN(rpow[0], targetPowerOfdm.twicePwr36);
1572                 rpow[6] = AH_MIN(rpow[0], targetPowerOfdm.twicePwr48);
1573                 rpow[7] = AH_MIN(rpow[0], targetPowerOfdm.twicePwr54);
1574
1575 #ifdef notyet
1576                 if (eeversion >= AR_EEPROM_VER4_0) {
1577                         /* Setup XR target power from EEPROM */
1578                         rpow[15] = AH_MIN(scaledPower, IS_CHAN_2GHZ(chan) ?
1579                                 xrTargetPower2 : xrTargetPower5);
1580                 } else {
1581                         /* XR uses 6mb power */
1582                         rpow[15] = rpow[0];
1583                 }
1584 #else
1585                 rpow[15] = rpow[0];
1586 #endif
1587
1588                 *pMinPower = rpow[7];
1589                 *pMaxPower = rpow[0];
1590
1591 #if 0
1592                 ahp->ah_ofdmTxPower = rpow[0];
1593 #endif
1594
1595                 HALDEBUG(ah, HAL_DEBUG_ANY,
1596                     "%s: MaxRD: %d TurboMax: %d MaxCTL: %d "
1597                     "TPC_Reduction %d\n", __func__,
1598                     twiceMaxRDPower, turbo2WMaxPower5,
1599                     twiceMaxEdgePower, tpcScaleReduction * 2);
1600         }
1601
1602         if (IEEE80211_IS_CHAN_CCK(chan)) {
1603                 /* Get final CCK target powers */
1604                 ar5212GetTargetPowers(ah, chan, trgtPwr_11b,
1605                         numTargetPwr_11b, &targetPowerCck);
1606
1607                 /* Reduce power by max regulatory domain allowed restrictions */
1608                 scaledPower = AH_MIN(twiceMaxEdgePowerCck,
1609                         twiceMaxRDPower - twiceAntennaReduction);
1610
1611                 scaledPower -= (tpcScaleReduction * 2);
1612                 scaledPower = (scaledPower < 0) ? 0 : scaledPower;
1613                 scaledPower = AH_MIN(scaledPower, powerLimit);
1614
1615                 rpow[8] = (scaledPower < 1) ? 1 : scaledPower;
1616
1617                 /* Set CCK rates 2L, 2S, 5.5L, 5.5S, 11L, 11S */
1618                 rpow[8]  = AH_MIN(scaledPower, targetPowerCck.twicePwr6_24);
1619                 rpow[9]  = AH_MIN(scaledPower, targetPowerCck.twicePwr36);
1620                 rpow[10] = rpow[9];
1621                 rpow[11] = AH_MIN(scaledPower, targetPowerCck.twicePwr48);
1622                 rpow[12] = rpow[11];
1623                 rpow[13] = AH_MIN(scaledPower, targetPowerCck.twicePwr54);
1624                 rpow[14] = rpow[13];
1625
1626                 /* Set min/max power based off OFDM values or initialization */
1627                 if (rpow[13] < *pMinPower)
1628                     *pMinPower = rpow[13];
1629                 if (rpow[9] > *pMaxPower)
1630                     *pMaxPower = rpow[9];
1631
1632         }
1633 #if 0
1634         ahp->ah_tx6PowerInHalfDbm = *pMaxPower;
1635 #endif
1636         return AH_TRUE;
1637 }
1638
1639 void*
1640 ath_hal_malloc(size_t size)
1641 {
1642         return calloc(1, size);
1643 }
1644
1645 void
1646 ath_hal_free(void* p)
1647 {
1648         return free(p);
1649 }
1650
1651 void
1652 ath_hal_vprintf(struct ath_hal *ah, const char* fmt, va_list ap)
1653 {
1654         vprintf(fmt, ap);
1655 }
1656
1657 void
1658 ath_hal_printf(struct ath_hal *ah, const char* fmt, ...)
1659 {
1660         va_list ap;
1661         va_start(ap, fmt);
1662         ath_hal_vprintf(ah, fmt, ap);
1663         va_end(ap);
1664 }
1665
1666 void
1667 DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...)
1668 {
1669         __va_list ap;
1670         va_start(ap, fmt);
1671         ath_hal_vprintf(ah, fmt, ap);
1672         va_end(ap);
1673 }