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