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