net: wireless: rockchip_wlan: add rtl8188eu support
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / rtl8188eu / core / rtw_rf.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4  *                                        
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 #define _RTW_RF_C_
21
22 #include <drv_types.h>
23 #include <hal_data.h>
24
25 u8 center_ch_5g_all[CENTER_CH_5G_ALL_NUM] = {
26                 36, 38, 40, 42, 44, 46, 48,                     /* Band 1 */
27                 52, 54, 56, 58, 60, 62, 64,                     /* Band 2 */
28                 100, 102, 104, 106, 108, 110, 112,      /* Band 3 */
29                 116, 118, 120, 122, 124, 126, 128,      /* Band 3 */
30                 132, 134, 136, 138, 140, 142, 144,      /* Band 3 */
31                 149, 151, 153, 155, 157, 159, 161,      /* Band 4 */
32                 165, 167, 169, 171, 173, 175, 177};     /* Band 4 */
33
34 u8 center_ch_5g_20m[CENTER_CH_5G_20M_NUM] = {
35         36, 40, 44, 48,
36         52, 56, 60, 64,
37         100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144,
38         149, 153, 157, 161, 165, 169, 173, 177
39 };
40
41 u8 center_ch_5g_40m[CENTER_CH_5G_40M_NUM] = {38, 46, 54, 62, 102, 110, 118, 126, 134, 142, 151, 159, 167, 175};
42
43 u8 center_ch_5g_80m[CENTER_CH_5G_80M_NUM] = {42, 58, 106, 122, 138, 155, 171};
44
45 struct center_chs_ent {
46         u8 ch_num;
47         u8 *chs;
48 };
49
50 struct center_chs_ent center_chs_5g_by_bw[] = {
51         {CENTER_CH_5G_20M_NUM, center_ch_5g_20m},
52         {CENTER_CH_5G_40M_NUM, center_ch_5g_40m},
53         {CENTER_CH_5G_80M_NUM, center_ch_5g_80m},
54 };
55
56 inline u8 center_chs_5g_num(u8 bw)
57 {
58         if (bw >= CHANNEL_WIDTH_160)
59                 return 0;
60         
61         return center_chs_5g_by_bw[bw].ch_num;
62 }
63
64 inline u8 center_chs_5g(u8 bw, u8 id)
65 {
66         if (bw >= CHANNEL_WIDTH_160)
67                 return 0;
68
69         if (id >= center_chs_5g_num(bw))
70                 return 0;
71                 
72         return center_chs_5g_by_bw[bw].chs[id];
73 }
74
75 int rtw_ch2freq(int chan)
76 {
77         /* see 802.11 17.3.8.3.2 and Annex J
78         * there are overlapping channel numbers in 5GHz and 2GHz bands */
79
80         /*
81         * RTK: don't consider the overlapping channel numbers: 5G channel <= 14,
82         * because we don't support it. simply judge from channel number
83         */
84
85         if (chan >= 1 && chan <= 14) {
86                 if (chan == 14)
87                         return 2484;
88                 else if (chan < 14)
89                         return 2407 + chan * 5;
90         } else if (chan >= 36 && chan <= 177) {
91                 return 5000 + chan * 5;
92         }
93
94         return 0; /* not supported */
95 }
96
97 int rtw_freq2ch(int freq)
98 {
99         /* see 802.11 17.3.8.3.2 and Annex J */
100         if (freq == 2484)
101                 return 14;
102         else if (freq < 2484)
103                 return (freq - 2407) / 5;
104         else if (freq >= 4910 && freq <= 4980)
105                 return (freq - 4000) / 5;
106         else if (freq <= 45000) /* DMG band lower limit */
107                 return (freq - 5000) / 5;
108         else if (freq >= 58320 && freq <= 64800)
109                 return (freq - 56160) / 2160;
110         else
111                 return 0;
112 }
113
114 bool rtw_chbw_to_freq_range(u8 ch, u8 bw, u8 offset, u32 *hi, u32 *lo)
115 {
116         u8 c_ch;
117         u32 freq;
118         u32 hi_ret = 0, lo_ret = 0;
119         int i;
120         bool valid = _FALSE;
121
122         if (hi)
123                 *hi = 0;
124         if (lo)
125                 *lo = 0;
126
127         c_ch = rtw_get_center_ch(ch, bw, offset);
128         freq = rtw_ch2freq(c_ch);
129
130         if (!freq) {
131                 rtw_warn_on(1);
132                 goto exit;
133         }
134
135         if (bw == CHANNEL_WIDTH_80) {
136                 hi_ret = freq + 40;
137                 lo_ret = freq - 40;
138         } else if (bw == CHANNEL_WIDTH_40) {
139                 hi_ret = freq + 20;
140                 lo_ret = freq - 20;
141         } else if (bw == CHANNEL_WIDTH_20) {
142                 hi_ret = freq + 10;
143                 lo_ret = freq - 10;
144         } else {
145                 rtw_warn_on(1);
146         }
147
148         if (hi)
149                 *hi = hi_ret;
150         if (lo)
151                 *lo = lo_ret;
152
153         valid = _TRUE;
154
155 exit:
156         return valid;
157 }
158
159 const char * const _ch_width_str[] = {
160         "20MHz",
161         "40MHz",
162         "80MHz",
163         "160MHz",
164         "80_80MHz",
165         "CHANNEL_WIDTH_MAX",
166 };
167
168 const u8 _ch_width_to_bw_cap[] = {
169         BW_CAP_20M,
170         BW_CAP_40M,
171         BW_CAP_80M,
172         BW_CAP_160M,
173         BW_CAP_80_80M,
174         0,
175 };
176
177 const char * const _band_str[] = {
178         "2.4G",
179         "5G",
180         "BOTH",
181         "BAND_MAX",
182 };
183
184 const u8 _band_to_band_cap[] = {
185         BAND_CAP_2G,
186         BAND_CAP_5G,
187         0,
188         0,
189 };
190
191 #ifdef CONFIG_80211AC_VHT
192 #define COUNTRY_CHPLAN_ASSIGN_EN_11AC(_val) , .en_11ac = (_val)
193 #else
194 #define COUNTRY_CHPLAN_ASSIGN_EN_11AC(_val)
195 #endif
196
197 #if RTW_DEF_MODULE_REGULATORY_CERT
198 #define COUNTRY_CHPLAN_ASSIGN_DEF_MODULE_FLAGS(_val) , .def_module_flags = (_val)
199 #else
200 #define COUNTRY_CHPLAN_ASSIGN_DEF_MODULE_FLAGS(_val)
201 #endif
202
203 /* has def_module_flags specified, used by common map and HAL dfference map */
204 #define COUNTRY_CHPLAN_ENT(_alpha2, _chplan, _en_11ac, _def_module_flags) \
205         {.alpha2 = (_alpha2), .chplan = (_chplan) \
206                 COUNTRY_CHPLAN_ASSIGN_EN_11AC(_en_11ac) \
207                 COUNTRY_CHPLAN_ASSIGN_DEF_MODULE_FLAGS(_def_module_flags) \
208         }
209
210 #ifdef CONFIG_CUSTOMIZED_COUNTRY_CHPLAN_MAP
211
212 #include "../platform/custom_country_chplan.h"
213
214 #elif RTW_DEF_MODULE_REGULATORY_CERT
215
216 #if (RTW_DEF_MODULE_REGULATORY_CERT & RTW_MODULE_RTL8821AE_HMC_M2)
217 static const struct country_chplan RTL8821AE_HMC_M2_country_chplan_map[] = {
218         COUNTRY_CHPLAN_ENT("CN", 0x51, 1, 0xFB), /* China */
219         COUNTRY_CHPLAN_ENT("RU", 0x59, 0, 0xFB), /* Russia(fac/gost), Kaliningrad */
220         COUNTRY_CHPLAN_ENT("UA", 0x26, 0, 0xFB), /* Ukraine */
221 };
222 static const u16 RTL8821AE_HMC_M2_country_chplan_map_sz = sizeof(RTL8821AE_HMC_M2_country_chplan_map)/sizeof(struct country_chplan);
223 #endif
224
225 #if (RTW_DEF_MODULE_REGULATORY_CERT & RTW_MODULE_RTL8821AU)
226 static const struct country_chplan RTL8821AU_country_chplan_map[] = {
227         COUNTRY_CHPLAN_ENT("RU", 0x59, 0, 0xFB), /* Russia(fac/gost), Kaliningrad */
228         COUNTRY_CHPLAN_ENT("UA", 0x26, 0, 0xFB), /* Ukraine */
229 };
230 static const u16 RTL8821AU_country_chplan_map_sz = sizeof(RTL8821AU_country_chplan_map)/sizeof(struct country_chplan);
231 #endif
232
233 #if (RTW_DEF_MODULE_REGULATORY_CERT & RTW_MODULE_RTL8812AENF_NGFF)
234 static const struct country_chplan RTL8812AENF_NGFF_country_chplan_map[] = {
235 };
236 static const u16 RTL8812AENF_NGFF_country_chplan_map_sz = sizeof(RTL8812AENF_NGFF_country_chplan_map)/sizeof(struct country_chplan);
237 #endif
238
239 #if (RTW_DEF_MODULE_REGULATORY_CERT & RTW_MODULE_RTL8812AEBT_HMC)
240 static const struct country_chplan RTL8812AEBT_HMC_country_chplan_map[] = {
241 };
242 static const u16 RTL8812AEBT_HMC_country_chplan_map_sz = sizeof(RTL8812AEBT_HMC_country_chplan_map)/sizeof(struct country_chplan);
243 #endif
244
245 #if (RTW_DEF_MODULE_REGULATORY_CERT & RTW_MODULE_RTL8188EE_HMC_M2)
246 static const struct country_chplan RTL8188EE_HMC_M2_country_chplan_map[] = {
247 };
248 static const u16 RTL8188EE_HMC_M2_country_chplan_map_sz = sizeof(RTL8188EE_HMC_M2_country_chplan_map)/sizeof(struct country_chplan);
249 #endif
250
251 #if (RTW_DEF_MODULE_REGULATORY_CERT & RTW_MODULE_RTL8723BE_HMC_M2)
252 static const struct country_chplan RTL8723BE_HMC_M2_country_chplan_map[] = {
253 };
254 static const u16 RTL8723BE_HMC_M2_country_chplan_map_sz = sizeof(RTL8723BE_HMC_M2_country_chplan_map)/sizeof(struct country_chplan);
255 #endif
256
257 #if (RTW_DEF_MODULE_REGULATORY_CERT & RTW_MODULE_RTL8723BS_NGFF1216)
258 static const struct country_chplan RTL8723BS_NGFF1216_country_chplan_map[] = {
259 };
260 static const u16 RTL8723BS_NGFF1216_country_chplan_map_sz = sizeof(RTL8723BS_NGFF1216_country_chplan_map)/sizeof(struct country_chplan);
261 #endif
262
263 #if (RTW_DEF_MODULE_REGULATORY_CERT & RTW_MODULE_RTL8192EEBT_HMC_M2)
264 static const struct country_chplan RTL8192EEBT_HMC_M2_country_chplan_map[] = {
265 };
266 static const u16 RTL8192EEBT_HMC_M2_country_chplan_map_sz = sizeof(RTL8192EEBT_HMC_M2_country_chplan_map)/sizeof(struct country_chplan);
267 #endif
268
269 /**
270  * rtw_def_module_get_chplan_from_country -
271  * @country_code: string of country code
272  * @return:
273  * Return NULL for case referring to common map
274  */
275 static const struct country_chplan *rtw_def_module_get_chplan_from_country(const char *country_code)
276 {
277         const struct country_chplan *ent = NULL;
278         const struct country_chplan *hal_map = NULL;
279         u16 hal_map_sz = 0;
280         int i;
281
282         /* TODO: runtime selection for multi driver */
283 #if (RTW_DEF_MODULE_REGULATORY_CERT == RTW_MODULE_RTL8821AE_HMC_M2)
284         hal_map = RTL8821AE_HMC_M2_country_chplan_map;
285         hal_map_sz = RTL8821AE_HMC_M2_country_chplan_map_sz;
286 #elif (RTW_DEF_MODULE_REGULATORY_CERT == RTW_MODULE_RTL8821AU)
287         hal_map = RTL8821AU_country_chplan_map;
288         hal_map_sz = RTL8821AU_country_chplan_map_sz;
289 #elif (RTW_DEF_MODULE_REGULATORY_CERT == RTW_MODULE_RTL8812AENF_NGFF)
290         hal_map = RTL8812AENF_NGFF_country_chplan_map;
291         hal_map_sz = RTL8812AENF_NGFF_country_chplan_map_sz;
292 #elif (RTW_DEF_MODULE_REGULATORY_CERT == RTW_MODULE_RTL8812AEBT_HMC)
293         hal_map = RTL8812AEBT_HMC_country_chplan_map;
294         hal_map_sz = RTL8812AEBT_HMC_country_chplan_map_sz;
295 #elif (RTW_DEF_MODULE_REGULATORY_CERT == RTW_MODULE_RTL8188EE_HMC_M2)
296         hal_map = RTL8188EE_HMC_M2_country_chplan_map;
297         hal_map_sz = RTL8188EE_HMC_M2_country_chplan_map_sz;
298 #elif (RTW_DEF_MODULE_REGULATORY_CERT == RTW_MODULE_RTL8723BE_HMC_M2)
299         hal_map = RTL8723BE_HMC_M2_country_chplan_map;
300         hal_map_sz = RTL8723BE_HMC_M2_country_chplan_map_sz;
301 #elif (RTW_DEF_MODULE_REGULATORY_CERT == RTW_MODULE_RTL8723BS_NGFF1216)
302         hal_map = RTL8723BS_NGFF1216_country_chplan_map;
303         hal_map_sz = RTL8723BS_NGFF1216_country_chplan_map_sz;
304 #elif (RTW_DEF_MODULE_REGULATORY_CERT == RTW_MODULE_RTL8192EEBT_HMC_M2)
305         hal_map = RTL8192EEBT_HMC_M2_country_chplan_map;
306         hal_map_sz = RTL8192EEBT_HMC_M2_country_chplan_map_sz;
307 #endif
308
309         if (hal_map == NULL || hal_map_sz == 0)
310                 goto exit;
311
312         for (i = 0; i < hal_map_sz; i++) {
313                 if (strncmp(country_code, hal_map[i].alpha2, 2) == 0) {
314                         ent = &hal_map[i];
315                         break;
316                 }
317         }
318
319 exit:
320         return ent;
321 }
322 #endif /* CONFIG_CUSTOMIZED_COUNTRY_CHPLAN_MAP or RTW_DEF_MODULE_REGULATORY_CERT */
323
324 static const struct country_chplan country_chplan_map[] = {
325         COUNTRY_CHPLAN_ENT("AD", 0x26, 1, 0x00), /* Andorra */
326         COUNTRY_CHPLAN_ENT("AE", 0x26, 1, 0xFB), /* United Arab Emirates */
327         COUNTRY_CHPLAN_ENT("AF", 0x42, 1, 0x00), /* Afghanistan */
328         COUNTRY_CHPLAN_ENT("AG", 0x30, 1, 0x00), /* Antigua & Barbuda */
329         COUNTRY_CHPLAN_ENT("AI", 0x26, 1, 0x00), /* Anguilla(UK) */
330         COUNTRY_CHPLAN_ENT("AL", 0x26, 1, 0xF1), /* Albania */
331         COUNTRY_CHPLAN_ENT("AM", 0x26, 1, 0xB0), /* Armenia */
332         COUNTRY_CHPLAN_ENT("AO", 0x26, 1, 0xE0), /* Angola */
333         COUNTRY_CHPLAN_ENT("AQ", 0x26, 1, 0x00), /* Antarctica */
334         COUNTRY_CHPLAN_ENT("AR", 0x57, 1, 0xF3), /* Argentina */
335         COUNTRY_CHPLAN_ENT("AS", 0x34, 1, 0x00), /* American Samoa */
336         COUNTRY_CHPLAN_ENT("AT", 0x26, 1, 0xFB), /* Austria */
337         COUNTRY_CHPLAN_ENT("AU", 0x45, 1, 0xFB), /* Australia */
338         COUNTRY_CHPLAN_ENT("AW", 0x34, 1, 0xB0), /* Aruba */
339         COUNTRY_CHPLAN_ENT("AZ", 0x26, 1, 0xF1), /* Azerbaijan */
340         COUNTRY_CHPLAN_ENT("BA", 0x26, 1, 0xF1), /* Bosnia & Herzegovina */
341         COUNTRY_CHPLAN_ENT("BB", 0x34, 1, 0x50), /* Barbados */
342         COUNTRY_CHPLAN_ENT("BD", 0x26, 1, 0xF1), /* Bangladesh */
343         COUNTRY_CHPLAN_ENT("BE", 0x26, 1, 0xFB), /* Belgium */
344         COUNTRY_CHPLAN_ENT("BF", 0x26, 1, 0xB0), /* Burkina Faso */
345         COUNTRY_CHPLAN_ENT("BG", 0x26, 1, 0xF1), /* Bulgaria */
346         COUNTRY_CHPLAN_ENT("BH", 0x47, 1, 0xF1), /* Bahrain */
347         COUNTRY_CHPLAN_ENT("BI", 0x26, 1, 0xB0), /* Burundi */
348         COUNTRY_CHPLAN_ENT("BJ", 0x26, 1, 0xB0), /* Benin */
349         COUNTRY_CHPLAN_ENT("BN", 0x47, 1, 0x10), /* Brunei */
350         COUNTRY_CHPLAN_ENT("BO", 0x30, 1, 0xF1), /* Bolivia */
351         COUNTRY_CHPLAN_ENT("BR", 0x34, 1, 0xF1), /* Brazil */
352         COUNTRY_CHPLAN_ENT("BS", 0x34, 1, 0x20), /* Bahamas */
353         COUNTRY_CHPLAN_ENT("BW", 0x26, 1, 0xF1), /* Botswana */
354         COUNTRY_CHPLAN_ENT("BY", 0x26, 1, 0xF1), /* Belarus */
355         COUNTRY_CHPLAN_ENT("BZ", 0x34, 1, 0x00), /* Belize */
356         COUNTRY_CHPLAN_ENT("CA", 0x34, 1, 0xFB), /* Canada */
357         COUNTRY_CHPLAN_ENT("CC", 0x26, 1, 0x00), /* Cocos (Keeling) Islands (Australia) */
358         COUNTRY_CHPLAN_ENT("CD", 0x26, 1, 0xB0), /* Congo, Republic of the */
359         COUNTRY_CHPLAN_ENT("CF", 0x26, 1, 0xB0), /* Central African Republic */
360         COUNTRY_CHPLAN_ENT("CG", 0x26, 1, 0xB0), /* Congo, Democratic Republic of the. Zaire */
361         COUNTRY_CHPLAN_ENT("CH", 0x26, 1, 0xFB), /* Switzerland */
362         COUNTRY_CHPLAN_ENT("CI", 0x26, 1, 0xF1), /* Cote d'Ivoire */
363         COUNTRY_CHPLAN_ENT("CK", 0x26, 1, 0x00), /* Cook Islands */
364         COUNTRY_CHPLAN_ENT("CL", 0x30, 1, 0xF1), /* Chile */
365         COUNTRY_CHPLAN_ENT("CM", 0x26, 1, 0xB0), /* Cameroon */
366         COUNTRY_CHPLAN_ENT("CN", 0x48, 1, 0xFB), /* China */
367         COUNTRY_CHPLAN_ENT("CO", 0x34, 1, 0xF1), /* Colombia */
368         COUNTRY_CHPLAN_ENT("CR", 0x34, 1, 0xF1), /* Costa Rica */
369         COUNTRY_CHPLAN_ENT("CV", 0x26, 1, 0xB0), /* Cape Verde */
370         COUNTRY_CHPLAN_ENT("CX", 0x45, 1, 0x00), /* Christmas Island (Australia) */
371         COUNTRY_CHPLAN_ENT("CY", 0x26, 1, 0xFB), /* Cyprus */
372         COUNTRY_CHPLAN_ENT("CZ", 0x26, 1, 0xFB), /* Czech Republic */
373         COUNTRY_CHPLAN_ENT("DE", 0x26, 1, 0xFB), /* Germany */
374         COUNTRY_CHPLAN_ENT("DJ", 0x26, 1, 0x80), /* Djibouti */
375         COUNTRY_CHPLAN_ENT("DK", 0x26, 1, 0xFB), /* Denmark */
376         COUNTRY_CHPLAN_ENT("DM", 0x34, 1, 0x00), /* Dominica */
377         COUNTRY_CHPLAN_ENT("DO", 0x34, 1, 0xF1), /* Dominican Republic */
378         COUNTRY_CHPLAN_ENT("DZ", 0x26, 1, 0xF1), /* Algeria */
379         COUNTRY_CHPLAN_ENT("EC", 0x34, 1, 0xF1), /* Ecuador */
380         COUNTRY_CHPLAN_ENT("EE", 0x26, 1, 0xFB), /* Estonia */
381         COUNTRY_CHPLAN_ENT("EG", 0x47, 0, 0xF1), /* Egypt */
382         COUNTRY_CHPLAN_ENT("EH", 0x47, 1, 0x80), /* Western Sahara */
383         COUNTRY_CHPLAN_ENT("ER", 0x26, 1, 0x00), /* Eritrea */
384         COUNTRY_CHPLAN_ENT("ES", 0x26, 1, 0xFB), /* Spain, Canary Islands, Ceuta, Melilla */
385         COUNTRY_CHPLAN_ENT("ET", 0x26, 1, 0xB0), /* Ethiopia */
386         COUNTRY_CHPLAN_ENT("FI", 0x26, 1, 0xFB), /* Finland */
387         COUNTRY_CHPLAN_ENT("FJ", 0x34, 1, 0x00), /* Fiji */
388         COUNTRY_CHPLAN_ENT("FK", 0x26, 1, 0x00), /* Falkland Islands (Islas Malvinas) (UK) */
389         COUNTRY_CHPLAN_ENT("FM", 0x34, 1, 0x00), /* Micronesia, Federated States of (USA) */
390         COUNTRY_CHPLAN_ENT("FO", 0x26, 1, 0x00), /* Faroe Islands (Denmark) */
391         COUNTRY_CHPLAN_ENT("FR", 0x26, 1, 0xFB), /* France */
392         COUNTRY_CHPLAN_ENT("GA", 0x26, 1, 0xB0), /* Gabon */
393         COUNTRY_CHPLAN_ENT("GB", 0x26, 1, 0xFB), /* Great Britain (United Kingdom; England) */
394         COUNTRY_CHPLAN_ENT("GD", 0x34, 1, 0xB0), /* Grenada */
395         COUNTRY_CHPLAN_ENT("GE", 0x26, 1, 0x00), /* Georgia */
396         COUNTRY_CHPLAN_ENT("GF", 0x26, 1, 0x80), /* French Guiana */
397         COUNTRY_CHPLAN_ENT("GG", 0x26, 1, 0x00), /* Guernsey (UK) */
398         COUNTRY_CHPLAN_ENT("GH", 0x26, 1, 0xF1), /* Ghana */
399         COUNTRY_CHPLAN_ENT("GI", 0x26, 1, 0x00), /* Gibraltar (UK) */
400         COUNTRY_CHPLAN_ENT("GL", 0x26, 1, 0x00), /* Greenland (Denmark) */
401         COUNTRY_CHPLAN_ENT("GM", 0x26, 1, 0xB0), /* Gambia */
402         COUNTRY_CHPLAN_ENT("GN", 0x26, 1, 0x10), /* Guinea */
403         COUNTRY_CHPLAN_ENT("GP", 0x26, 1, 0x00), /* Guadeloupe (France) */
404         COUNTRY_CHPLAN_ENT("GQ", 0x26, 1, 0xB0), /* Equatorial Guinea */
405         COUNTRY_CHPLAN_ENT("GR", 0x26, 1, 0xFB), /* Greece */
406         COUNTRY_CHPLAN_ENT("GS", 0x26, 1, 0x00), /* South Georgia and the Sandwich Islands (UK) */
407         COUNTRY_CHPLAN_ENT("GT", 0x34, 1, 0xF1), /* Guatemala */
408         COUNTRY_CHPLAN_ENT("GU", 0x34, 1, 0x00), /* Guam (USA) */
409         COUNTRY_CHPLAN_ENT("GW", 0x26, 1, 0xB0), /* Guinea-Bissau */
410         COUNTRY_CHPLAN_ENT("GY", 0x44, 1, 0x00), /* Guyana */
411         COUNTRY_CHPLAN_ENT("HK", 0x26, 1, 0xFB), /* Hong Kong */
412         COUNTRY_CHPLAN_ENT("HM", 0x45, 1, 0x00), /* Heard and McDonald Islands (Australia) */
413         COUNTRY_CHPLAN_ENT("HN", 0x32, 1, 0xF1), /* Honduras */
414         COUNTRY_CHPLAN_ENT("HR", 0x26, 1, 0xF9), /* Croatia */
415         COUNTRY_CHPLAN_ENT("HT", 0x34, 1, 0x50), /* Haiti */
416         COUNTRY_CHPLAN_ENT("HU", 0x26, 1, 0xFB), /* Hungary */
417         COUNTRY_CHPLAN_ENT("ID", 0x54, 0, 0xF3), /* Indonesia */
418         COUNTRY_CHPLAN_ENT("IE", 0x26, 1, 0xFB), /* Ireland */
419         COUNTRY_CHPLAN_ENT("IL", 0x47, 1, 0xF1), /* Israel */
420         COUNTRY_CHPLAN_ENT("IM", 0x26, 1, 0x00), /* Isle of Man (UK) */
421         COUNTRY_CHPLAN_ENT("IN", 0x47, 1, 0xF1), /* India */
422         COUNTRY_CHPLAN_ENT("IQ", 0x26, 1, 0x00), /* Iraq */
423         COUNTRY_CHPLAN_ENT("IR", 0x26, 0, 0x00), /* Iran */
424         COUNTRY_CHPLAN_ENT("IS", 0x26, 1, 0xFB), /* Iceland */
425         COUNTRY_CHPLAN_ENT("IT", 0x26, 1, 0xFB), /* Italy */
426         COUNTRY_CHPLAN_ENT("JE", 0x26, 1, 0x00), /* Jersey (UK) */
427         COUNTRY_CHPLAN_ENT("JM", 0x51, 1, 0xF1), /* Jamaica */
428         COUNTRY_CHPLAN_ENT("JO", 0x49, 1, 0xFB), /* Jordan */
429         COUNTRY_CHPLAN_ENT("JP", 0x27, 1, 0xFF), /* Japan- Telec */
430         COUNTRY_CHPLAN_ENT("KE", 0x47, 1, 0xF9), /* Kenya */
431         COUNTRY_CHPLAN_ENT("KG", 0x26, 1, 0xF1), /* Kyrgyzstan */
432         COUNTRY_CHPLAN_ENT("KH", 0x26, 1, 0xF1), /* Cambodia */
433         COUNTRY_CHPLAN_ENT("KI", 0x26, 1, 0x00), /* Kiribati */
434         COUNTRY_CHPLAN_ENT("KN", 0x34, 1, 0x00), /* Saint Kitts and Nevis */
435         COUNTRY_CHPLAN_ENT("KR", 0x28, 1, 0xFB), /* South Korea */
436         COUNTRY_CHPLAN_ENT("KW", 0x47, 1, 0xFB), /* Kuwait */
437         COUNTRY_CHPLAN_ENT("KY", 0x34, 1, 0x00), /* Cayman Islands (UK) */
438         COUNTRY_CHPLAN_ENT("KZ", 0x26, 1, 0x00), /* Kazakhstan */
439         COUNTRY_CHPLAN_ENT("LA", 0x26, 1, 0x00), /* Laos */
440         COUNTRY_CHPLAN_ENT("LB", 0x26, 1, 0xF1), /* Lebanon */
441         COUNTRY_CHPLAN_ENT("LC", 0x34, 1, 0x00), /* Saint Lucia */
442         COUNTRY_CHPLAN_ENT("LI", 0x26, 1, 0xFB), /* Liechtenstein */
443         COUNTRY_CHPLAN_ENT("LK", 0x26, 1, 0xF1), /* Sri Lanka */
444         COUNTRY_CHPLAN_ENT("LR", 0x26, 1, 0xB0), /* Liberia */
445         COUNTRY_CHPLAN_ENT("LS", 0x26, 1, 0xF1), /* Lesotho */
446         COUNTRY_CHPLAN_ENT("LT", 0x26, 1, 0xFB), /* Lithuania */
447         COUNTRY_CHPLAN_ENT("LU", 0x26, 1, 0xFB), /* Luxembourg */
448         COUNTRY_CHPLAN_ENT("LV", 0x26, 1, 0xFB), /* Latvia */
449         COUNTRY_CHPLAN_ENT("LY", 0x26, 1, 0x00), /* Libya */
450         COUNTRY_CHPLAN_ENT("MA", 0x47, 1, 0xF1), /* Morocco */
451         COUNTRY_CHPLAN_ENT("MC", 0x26, 1, 0xFB), /* Monaco */
452         COUNTRY_CHPLAN_ENT("MD", 0x26, 1, 0xF1), /* Moldova */
453         COUNTRY_CHPLAN_ENT("ME", 0x26, 1, 0xF1), /* Montenegro */
454         COUNTRY_CHPLAN_ENT("MF", 0x34, 1, 0x00), /* Saint Martin */
455         COUNTRY_CHPLAN_ENT("MG", 0x26, 1, 0x20), /* Madagascar */
456         COUNTRY_CHPLAN_ENT("MH", 0x34, 1, 0x00), /* Marshall Islands (USA) */
457         COUNTRY_CHPLAN_ENT("MK", 0x26, 1, 0xF1), /* Republic of Macedonia (FYROM) */
458         COUNTRY_CHPLAN_ENT("ML", 0x26, 1, 0xB0), /* Mali */
459         COUNTRY_CHPLAN_ENT("MM", 0x26, 1, 0x00), /* Burma (Myanmar) */
460         COUNTRY_CHPLAN_ENT("MN", 0x26, 1, 0x00), /* Mongolia */
461         COUNTRY_CHPLAN_ENT("MO", 0x26, 1, 0x00), /* Macau */
462         COUNTRY_CHPLAN_ENT("MP", 0x34, 1, 0x00), /* Northern Mariana Islands (USA) */
463         COUNTRY_CHPLAN_ENT("MQ", 0x26, 1, 0x40), /* Martinique (France) */
464         COUNTRY_CHPLAN_ENT("MR", 0x26, 1, 0xA0), /* Mauritania */
465         COUNTRY_CHPLAN_ENT("MS", 0x26, 1, 0x00), /* Montserrat (UK) */
466         COUNTRY_CHPLAN_ENT("MT", 0x26, 1, 0xFB), /* Malta */
467         COUNTRY_CHPLAN_ENT("MU", 0x26, 1, 0xB0), /* Mauritius */
468         COUNTRY_CHPLAN_ENT("MV", 0x26, 1, 0x00), /* Maldives */
469         COUNTRY_CHPLAN_ENT("MW", 0x26, 1, 0xB0), /* Malawi */
470         COUNTRY_CHPLAN_ENT("MX", 0x34, 1, 0xF1), /* Mexico */
471         COUNTRY_CHPLAN_ENT("MY", 0x47, 1, 0xF1), /* Malaysia */
472         COUNTRY_CHPLAN_ENT("MZ", 0x26, 1, 0xF1), /* Mozambique */
473         COUNTRY_CHPLAN_ENT("NA", 0x26, 0, 0x00), /* Namibia */
474         COUNTRY_CHPLAN_ENT("NC", 0x26, 1, 0x00), /* New Caledonia */
475         COUNTRY_CHPLAN_ENT("NE", 0x26, 1, 0xB0), /* Niger */
476         COUNTRY_CHPLAN_ENT("NF", 0x45, 1, 0x00), /* Norfolk Island (Australia) */
477         COUNTRY_CHPLAN_ENT("NG", 0x50, 1, 0xF9), /* Nigeria */
478         COUNTRY_CHPLAN_ENT("NI", 0x34, 1, 0xF1), /* Nicaragua */
479         COUNTRY_CHPLAN_ENT("NL", 0x26, 1, 0xFB), /* Netherlands */
480         COUNTRY_CHPLAN_ENT("NO", 0x26, 1, 0xFB), /* Norway */
481         COUNTRY_CHPLAN_ENT("NP", 0x47, 1, 0xF0), /* Nepal */
482         COUNTRY_CHPLAN_ENT("NR", 0x26, 1, 0x00), /* Nauru */
483         COUNTRY_CHPLAN_ENT("NU", 0x45, 1, 0x00), /* Niue */
484         COUNTRY_CHPLAN_ENT("NZ", 0x45, 1, 0xFB), /* New Zealand */
485         COUNTRY_CHPLAN_ENT("OM", 0x26, 1, 0xF9), /* Oman */
486         COUNTRY_CHPLAN_ENT("PA", 0x34, 1, 0xF1), /* Panama */
487         COUNTRY_CHPLAN_ENT("PE", 0x34, 1, 0xF1), /* Peru */
488         COUNTRY_CHPLAN_ENT("PF", 0x26, 1, 0x00), /* French Polynesia (France) */
489         COUNTRY_CHPLAN_ENT("PG", 0x26, 1, 0xF1), /* Papua New Guinea */
490         COUNTRY_CHPLAN_ENT("PH", 0x26, 1, 0xF1), /* Philippines */
491         COUNTRY_CHPLAN_ENT("PK", 0x51, 1, 0xF1), /* Pakistan */
492         COUNTRY_CHPLAN_ENT("PL", 0x26, 1, 0xFB), /* Poland */
493         COUNTRY_CHPLAN_ENT("PM", 0x26, 1, 0x00), /* Saint Pierre and Miquelon (France) */
494         COUNTRY_CHPLAN_ENT("PR", 0x34, 1, 0xF1), /* Puerto Rico */
495         COUNTRY_CHPLAN_ENT("PT", 0x26, 1, 0xFB), /* Portugal */
496         COUNTRY_CHPLAN_ENT("PW", 0x34, 1, 0x00), /* Palau */
497         COUNTRY_CHPLAN_ENT("PY", 0x34, 1, 0xF1), /* Paraguay */
498         COUNTRY_CHPLAN_ENT("QA", 0x51, 1, 0xF9), /* Qatar */
499         COUNTRY_CHPLAN_ENT("RE", 0x26, 1, 0x00), /* Reunion (France) */
500         COUNTRY_CHPLAN_ENT("RO", 0x26, 1, 0xF1), /* Romania */
501         COUNTRY_CHPLAN_ENT("RS", 0x26, 1, 0xF1), /* Serbia, Kosovo */
502         COUNTRY_CHPLAN_ENT("RU", 0x59, 1, 0xFB), /* Russia(fac/gost), Kaliningrad */
503         COUNTRY_CHPLAN_ENT("RW", 0x26, 1, 0xB0), /* Rwanda */
504         COUNTRY_CHPLAN_ENT("SA", 0x26, 1, 0xFB), /* Saudi Arabia */
505         COUNTRY_CHPLAN_ENT("SB", 0x26, 1, 0x00), /* Solomon Islands */
506         COUNTRY_CHPLAN_ENT("SC", 0x34, 1, 0x90), /* Seychelles */
507         COUNTRY_CHPLAN_ENT("SE", 0x26, 1, 0xFB), /* Sweden */
508         COUNTRY_CHPLAN_ENT("SG", 0x47, 1, 0xFB), /* Singapore */
509         COUNTRY_CHPLAN_ENT("SH", 0x26, 1, 0x00), /* Saint Helena (UK) */
510         COUNTRY_CHPLAN_ENT("SI", 0x26, 1, 0xFB), /* Slovenia */
511         COUNTRY_CHPLAN_ENT("SJ", 0x26, 1, 0x00), /* Svalbard (Norway) */
512         COUNTRY_CHPLAN_ENT("SK", 0x26, 1, 0xFB), /* Slovakia */
513         COUNTRY_CHPLAN_ENT("SL", 0x26, 1, 0xB0), /* Sierra Leone */
514         COUNTRY_CHPLAN_ENT("SM", 0x26, 1, 0x00), /* San Marino */
515         COUNTRY_CHPLAN_ENT("SN", 0x26, 1, 0xF1), /* Senegal */
516         COUNTRY_CHPLAN_ENT("SO", 0x26, 1, 0x00), /* Somalia */
517         COUNTRY_CHPLAN_ENT("SR", 0x34, 1, 0x00), /* Suriname */
518         COUNTRY_CHPLAN_ENT("ST", 0x34, 1, 0x80), /* Sao Tome and Principe */
519         COUNTRY_CHPLAN_ENT("SV", 0x30, 1, 0xF1), /* El Salvador */
520         COUNTRY_CHPLAN_ENT("SX", 0x34, 1, 0x00), /* Sint Marteen */
521         COUNTRY_CHPLAN_ENT("SZ", 0x26, 1, 0x20), /* Swaziland */
522         COUNTRY_CHPLAN_ENT("TC", 0x26, 1, 0x00), /* Turks and Caicos Islands (UK) */
523         COUNTRY_CHPLAN_ENT("TD", 0x26, 1, 0xB0), /* Chad */
524         COUNTRY_CHPLAN_ENT("TF", 0x26, 1, 0x80), /* French Southern and Antarctic Lands (FR Southern Territories) */
525         COUNTRY_CHPLAN_ENT("TG", 0x26, 1, 0xB0), /* Togo */
526         COUNTRY_CHPLAN_ENT("TH", 0x26, 1, 0xF1), /* Thailand */
527         COUNTRY_CHPLAN_ENT("TJ", 0x26, 1, 0x40), /* Tajikistan */
528         COUNTRY_CHPLAN_ENT("TK", 0x45, 1, 0x00), /* Tokelau */
529         COUNTRY_CHPLAN_ENT("TM", 0x26, 1, 0x00), /* Turkmenistan */
530         COUNTRY_CHPLAN_ENT("TN", 0x47, 1, 0xF1), /* Tunisia */
531         COUNTRY_CHPLAN_ENT("TO", 0x26, 1, 0x00), /* Tonga */
532         COUNTRY_CHPLAN_ENT("TR", 0x26, 1, 0xF1), /* Turkey, Northern Cyprus */
533         COUNTRY_CHPLAN_ENT("TT", 0x42, 1, 0xF1), /* Trinidad & Tobago */
534         COUNTRY_CHPLAN_ENT("TW", 0x39, 1, 0xFF), /* Taiwan */
535         COUNTRY_CHPLAN_ENT("TZ", 0x26, 1, 0xF0), /* Tanzania */
536         COUNTRY_CHPLAN_ENT("UA", 0x26, 1, 0xFB), /* Ukraine */
537         COUNTRY_CHPLAN_ENT("UG", 0x26, 1, 0xF1), /* Uganda */
538         COUNTRY_CHPLAN_ENT("US", 0x34, 1, 0xFF), /* United States of America (USA) */
539         COUNTRY_CHPLAN_ENT("UY", 0x34, 1, 0xF1), /* Uruguay */
540         COUNTRY_CHPLAN_ENT("UZ", 0x47, 1, 0xF0), /* Uzbekistan */
541         COUNTRY_CHPLAN_ENT("VA", 0x26, 1, 0x00), /* Holy See (Vatican City) */
542         COUNTRY_CHPLAN_ENT("VC", 0x34, 1, 0x10), /* Saint Vincent and the Grenadines */
543         COUNTRY_CHPLAN_ENT("VE", 0x30, 1, 0xF1), /* Venezuela */
544         COUNTRY_CHPLAN_ENT("VI", 0x34, 1, 0x00), /* United States Virgin Islands (USA) */
545         COUNTRY_CHPLAN_ENT("VN", 0x26, 1, 0xF1), /* Vietnam */
546         COUNTRY_CHPLAN_ENT("VU", 0x26, 1, 0x00), /* Vanuatu */
547         COUNTRY_CHPLAN_ENT("WF", 0x26, 1, 0x00), /* Wallis and Futuna (France) */
548         COUNTRY_CHPLAN_ENT("WS", 0x34, 1, 0x00), /* Samoa */
549         COUNTRY_CHPLAN_ENT("YE", 0x26, 1, 0x40), /* Yemen */
550         COUNTRY_CHPLAN_ENT("YT", 0x26, 1, 0x80), /* Mayotte (France) */
551         COUNTRY_CHPLAN_ENT("ZA", 0x26, 1, 0xF1), /* South Africa */
552         COUNTRY_CHPLAN_ENT("ZM", 0x26, 1, 0xB0), /* Zambia */
553         COUNTRY_CHPLAN_ENT("ZW", 0x26, 1, 0xF1), /* Zimbabwe */
554 };
555
556 u16 const country_chplan_map_sz = sizeof(country_chplan_map)/sizeof(struct country_chplan);
557
558 /*
559 * rtw_get_chplan_from_country -
560 * @country_code: string of country code
561 *
562 * Return pointer of struct country_chplan entry or NULL when unsupported country_code is given
563 */
564 const struct country_chplan *rtw_get_chplan_from_country(const char *country_code)
565 {
566         const struct country_chplan *ent = NULL;
567         const struct country_chplan *map = NULL;
568         u16 map_sz = 0;
569         char code[2];
570         int i;
571
572         code[0] = alpha_to_upper(country_code[0]);
573         code[1] = alpha_to_upper(country_code[1]);
574
575 #if !defined(CONFIG_CUSTOMIZED_COUNTRY_CHPLAN_MAP) && RTW_DEF_MODULE_REGULATORY_CERT
576         ent = rtw_def_module_get_chplan_from_country(code);
577         if (ent != NULL)
578                 goto exit;
579 #endif
580
581 #ifdef CONFIG_CUSTOMIZED_COUNTRY_CHPLAN_MAP
582         map = CUSTOMIZED_country_chplan_map;
583         map_sz = CUSTOMIZED_country_chplan_map_sz;
584 #else
585         map = country_chplan_map;
586         map_sz = country_chplan_map_sz;
587 #endif
588
589         for (i = 0; i < map_sz; i++) {
590                 if (strncmp(code, map[i].alpha2, 2) == 0) {
591                         ent = &map[i];
592                         break;
593                 }
594         }
595
596 exit:
597         #if RTW_DEF_MODULE_REGULATORY_CERT
598         if (ent && !(COUNTRY_CHPLAN_DEF_MODULE_FALGS(ent) & RTW_DEF_MODULE_REGULATORY_CERT))
599                 ent = NULL;
600         #endif
601
602         return ent;
603 }
604
605 int rtw_ch_to_bb_gain_sel(int ch)
606 {
607         int sel = -1;
608
609         if (ch >= 1 && ch <= 14)
610                 sel = BB_GAIN_2G;
611 #ifdef CONFIG_IEEE80211_BAND_5GHZ
612         else if (ch >= 36 && ch < 48)
613                 sel = BB_GAIN_5GLB1;
614         else if (ch >= 52 && ch <= 64)
615                 sel = BB_GAIN_5GLB2;
616         else if (ch >= 100 && ch <= 120)
617                 sel = BB_GAIN_5GMB1;
618         else if (ch >= 124 && ch <= 144)
619                 sel = BB_GAIN_5GMB2;
620         else if (ch >= 149 && ch <= 177)
621                 sel = BB_GAIN_5GHB;
622 #endif
623
624         return sel;
625 }
626
627 s8 rtw_rf_get_kfree_tx_gain_offset(_adapter *padapter, u8 path, u8 ch)
628 {
629         s8 kfree_offset = 0;
630
631 #ifdef CONFIG_RF_POWER_TRIM
632         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(padapter);
633         struct kfree_data_t *kfree_data = GET_KFREE_DATA(padapter);
634         s8 bb_gain_sel = rtw_ch_to_bb_gain_sel(ch);
635
636         if (bb_gain_sel < BB_GAIN_2G || bb_gain_sel >= BB_GAIN_NUM) {
637                 rtw_warn_on(1);
638                 goto exit;
639         }
640
641         if (kfree_data->flag & KFREE_FLAG_ON) {
642                 kfree_offset = kfree_data->bb_gain[bb_gain_sel][path];
643                 if (1)
644                         DBG_871X("%s path:%u, ch:%u, bb_gain_sel:%d, kfree_offset:%d\n"
645                                 , __func__, path, ch, bb_gain_sel, kfree_offset);
646         }
647 exit:
648 #endif /* CONFIG_RF_POWER_TRIM */
649         return kfree_offset;
650 }
651
652 void rtw_rf_set_tx_gain_offset(_adapter *adapter, u8 path, s8 offset)
653 {
654         u8 write_value;
655
656         DBG_871X("kfree gain_offset 0x55:0x%x ", rtw_hal_read_rfreg(adapter, path, 0x55, 0xffffffff));
657         switch (rtw_get_chip_type(adapter)) {
658 #ifdef CONFIG_RTL8703B
659         case RTL8703B:
660                 write_value = RF_TX_GAIN_OFFSET_8703B(offset);
661                 rtw_hal_write_rfreg(adapter, path, 0x55, 0x0fc000, write_value);
662                 break;
663 #endif /* CONFIG_RTL8703B */
664 #ifdef CONFIG_RTL8188F
665         case RTL8188F:
666                 write_value = RF_TX_GAIN_OFFSET_8188F(offset);
667                 rtw_hal_write_rfreg(adapter, path, 0x55, 0x0fc000, write_value);
668                 break;
669 #endif /* CONFIG_RTL8188F */
670 #ifdef CONFIG_RTL8192E
671         case RTL8192E:
672                 write_value = RF_TX_GAIN_OFFSET_8192E(offset);
673                 rtw_hal_write_rfreg(adapter, path, 0x55, 0x0f8000, write_value);
674                 break;
675 #endif /* CONFIG_RTL8188F */
676
677 #ifdef CONFIG_RTL8821A
678         case RTL8821:
679                 write_value = RF_TX_GAIN_OFFSET_8821A(offset);
680                 rtw_hal_write_rfreg(adapter, path, 0x55, 0x0f8000, write_value);
681                 break;
682 #endif /* CONFIG_RTL8821A */
683 #ifdef CONFIG_RTL8814A
684                 case RTL8814A:
685                 DBG_871X("\nkfree by PhyDM on the sw CH. path %d\n", path);
686                 break;
687 #endif /* CONFIG_RTL8821A */
688
689         default:
690                 rtw_warn_on(1);
691                 break;
692         }
693
694         DBG_871X(" after :0x%x\n", rtw_hal_read_rfreg(adapter, path, 0x55, 0xffffffff));
695 }
696
697 void rtw_rf_apply_tx_gain_offset(_adapter *adapter, u8 ch)
698 {
699         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
700         s8 kfree_offset = 0;
701         s8 tx_pwr_track_offset = 0; /* TODO: 8814A should consider tx pwr track when setting tx gain offset */
702         s8 total_offset;
703         int i;
704
705         for (i = 0; i < hal_data->NumTotalRFPath; i++) {
706                 kfree_offset = rtw_rf_get_kfree_tx_gain_offset(adapter, i, ch);
707                 total_offset = kfree_offset + tx_pwr_track_offset;
708                 rtw_rf_set_tx_gain_offset(adapter, i, total_offset);
709         }
710 }
711
712 bool rtw_is_dfs_range(u32 hi, u32 lo)
713 {
714         return rtw_is_range_overlap(hi, lo, 5720 + 10, 5260 - 10)?_TRUE:_FALSE;
715 }
716
717 bool rtw_is_dfs_ch(u8 ch, u8 bw, u8 offset)
718 {
719         u32 hi, lo;
720
721         if (rtw_chbw_to_freq_range(ch, bw, offset, &hi, &lo) == _FALSE)
722                 return _FALSE;
723
724         return rtw_is_dfs_range(hi, lo)?_TRUE:_FALSE;
725 }
726
727 bool rtw_is_long_cac_range(u32 hi, u32 lo)
728 {
729         return rtw_is_range_overlap(hi, lo, 5660 + 10, 5600 - 10)?_TRUE:_FALSE;
730 }
731
732 bool rtw_is_long_cac_ch(u8 ch, u8 bw, u8 offset)
733 {
734         u32 hi, lo;
735
736         if (rtw_chbw_to_freq_range(ch, bw, offset, &hi, &lo) == _FALSE)
737                 return _FALSE;
738
739         return rtw_is_long_cac_range(hi, lo)?_TRUE:_FALSE;
740 }
741