xref: /wlan-dirver/qca-wifi-host-cmn/umac/regulatory/core/src/reg_opclass.c (revision dd4dc88b837a295134aa9869114a2efee0f4894b)
1 /*
2  * Copyright (c) 2014-2019 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /**
20  * DOC: reg_opclass.c
21  * This file defines regulatory opclass functions.
22  */
23 
24 #include <qdf_types.h>
25 #include <wlan_cmn.h>
26 #include <reg_services_public_struct.h>
27 #include <wlan_objmgr_psoc_obj.h>
28 #include "reg_priv_objs.h"
29 #include "reg_utils.h"
30 #include "reg_db.h"
31 #include "reg_db_parser.h"
32 #include "reg_host_11d.h"
33 #include <scheduler_api.h>
34 #include "reg_build_chan_list.h"
35 #include "reg_opclass.h"
36 
37 #ifdef HOST_OPCLASS
38 static struct reg_dmn_supp_op_classes reg_dmn_curr_supp_opp_classes = { 0 };
39 #endif
40 
41 static const struct reg_dmn_op_class_map_t global_op_class[] = {
42 	{81, 25, BW20, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13} },
43 	{82, 25, BW20, {14} },
44 	{83, 40, BW40_LOW_PRIMARY, {1, 2, 3, 4, 5, 6, 7, 8, 9} },
45 	{84, 40, BW40_HIGH_PRIMARY, {5, 6, 7, 8, 9, 10, 11, 12, 13} },
46 	{115, 20, BW20, {36, 40, 44, 48} },
47 	{116, 40, BW40_LOW_PRIMARY, {36, 44} },
48 	{117, 40, BW40_HIGH_PRIMARY, {40, 48} },
49 	{118, 20, BW20, {52, 56, 60, 64} },
50 	{119, 40, BW40_LOW_PRIMARY, {52, 60} },
51 	{120, 40, BW40_HIGH_PRIMARY, {56, 64} },
52 	{121, 20, BW20,
53 	 {100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140} },
54 	{122, 40, BW40_LOW_PRIMARY, {100, 108, 116, 124, 132} },
55 	{123, 40, BW40_HIGH_PRIMARY, {104, 112, 120, 128, 136} },
56 	{125, 20, BW20, {149, 153, 157, 161, 165, 169} },
57 	{126, 40, BW40_LOW_PRIMARY, {149, 157} },
58 	{127, 40, BW40_HIGH_PRIMARY, {153, 161} },
59 	{128, 80, BW80, {36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108,
60 			   112, 116, 120, 124, 128, 132, 136, 140, 144,
61 			   149, 153, 157, 161} },
62 	{0, 0, 0, {0} },
63 };
64 
65 static const struct reg_dmn_op_class_map_t us_op_class[] = {
66 	{1, 20, BW20, {36, 40, 44, 48} },
67 	{2, 20, BW20, {52, 56, 60, 64} },
68 	{4, 20, BW20, {100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140,
69 		       144} },
70 	{5, 20, BW20, {149, 153, 157, 161, 165} },
71 	{12, 25, BW20, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} },
72 	{22, 40, BW40_LOW_PRIMARY, {36, 44} },
73 	{23, 40, BW40_LOW_PRIMARY, {52, 60} },
74 	{24, 40, BW40_LOW_PRIMARY, {100, 108, 116, 124, 132} },
75 	{26, 40, BW40_LOW_PRIMARY, {149, 157} },
76 	{27, 40, BW40_HIGH_PRIMARY, {40, 48} },
77 	{28, 40, BW40_HIGH_PRIMARY, {56, 64} },
78 	{29, 40, BW40_HIGH_PRIMARY, {104, 112, 120, 128, 136} },
79 	{31, 40, BW40_HIGH_PRIMARY, {153, 161} },
80 	{32, 40, BW40_LOW_PRIMARY, {1, 2, 3, 4, 5, 6, 7} },
81 	{33, 40, BW40_HIGH_PRIMARY, {5, 6, 7, 8, 9, 10, 11} },
82 	{128, 80, BW80, {36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108,
83 			 112, 116, 120, 124, 128, 132, 136, 140, 144,
84 			 149, 153, 157, 161} },
85 	{0, 0, 0, {0} },
86 };
87 
88 static const struct reg_dmn_op_class_map_t euro_op_class[] = {
89 	{1, 20, BW20, {36, 40, 44, 48} },
90 	{2, 20, BW20, {52, 56, 60, 64} },
91 	{3, 20, BW20, {100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140} },
92 	{4, 25, BW20, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13} },
93 	{5, 40, BW40_LOW_PRIMARY, {36, 44} },
94 	{6, 40, BW40_LOW_PRIMARY, {52, 60} },
95 	{7, 40, BW40_LOW_PRIMARY, {100, 108, 116, 124, 132} },
96 	{8, 40, BW40_HIGH_PRIMARY, {40, 48} },
97 	{9, 40, BW40_HIGH_PRIMARY, {56, 64} },
98 	{10, 40, BW40_HIGH_PRIMARY, {104, 112, 120, 128, 136} },
99 	{11, 40, BW40_LOW_PRIMARY, {1, 2, 3, 4, 5, 6, 7, 8, 9} },
100 	{12, 40, BW40_HIGH_PRIMARY, {5, 6, 7, 8, 9, 10, 11, 12, 13} },
101 	{17, 20, BW20, {149, 153, 157, 161, 165, 169} },
102 	{128, 80, BW80, {36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108, 112,
103 			 116, 120, 124, 128} },
104 	{0, 0, 0, {0} },
105 };
106 
107 static const struct reg_dmn_op_class_map_t japan_op_class[] = {
108 	{1, 20, BW20, {36, 40, 44, 48} },
109 	{30, 25, BW20, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13} },
110 	{31, 25, BW20, {14} },
111 	{32, 20, BW20, {52, 56, 60, 64} },
112 	{34, 20, BW20,
113 		{100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140} },
114 	{36, 40, BW40_LOW_PRIMARY, {36, 44} },
115 	{37, 40, BW40_LOW_PRIMARY, {52, 60} },
116 	{39, 40, BW40_LOW_PRIMARY, {100, 108, 116, 124, 132} },
117 	{41, 40, BW40_HIGH_PRIMARY, {40, 48} },
118 	{42, 40, BW40_HIGH_PRIMARY, {56, 64} },
119 	{44, 40, BW40_HIGH_PRIMARY, {104, 112, 120, 128, 136} },
120 	{128, 80, BW80, {36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108, 112,
121 			 116, 120, 124, 128} },
122 	{0, 0, 0, {0} },
123 };
124 
125 #ifdef HOST_OPCLASS
126 /**
127  * reg_get_class_from_country()- Get Class from country
128  * @country- Country
129  *
130  * Return: class.
131  */
132 static
133 const struct reg_dmn_op_class_map_t *reg_get_class_from_country(uint8_t
134 								   *country)
135 {
136 	const struct reg_dmn_op_class_map_t *class = NULL;
137 
138 	qdf_debug("Country %c%c 0x%x",
139 		  country[0], country[1], country[2]);
140 
141 	switch (country[2]) {
142 	case OP_CLASS_US:
143 		class = us_op_class;
144 		break;
145 
146 	case OP_CLASS_EU:
147 		class = euro_op_class;
148 		break;
149 
150 	case OP_CLASS_JAPAN:
151 		class = japan_op_class;
152 		break;
153 
154 	case OP_CLASS_GLOBAL:
155 		class = global_op_class;
156 		break;
157 
158 	default:
159 		if (!qdf_mem_cmp(country, "US", 2))
160 			class = us_op_class;
161 		else if (!qdf_mem_cmp(country, "EU", 2))
162 			class = euro_op_class;
163 		else if (!qdf_mem_cmp(country, "JP", 2))
164 			class = japan_op_class;
165 		else
166 			class = global_op_class;
167 	}
168 	return class;
169 }
170 
171 uint16_t reg_dmn_get_chanwidth_from_opclass(uint8_t *country, uint8_t channel,
172 					    uint8_t opclass)
173 {
174 	const struct reg_dmn_op_class_map_t *class;
175 	uint16_t i;
176 
177 	class = reg_get_class_from_country(country);
178 
179 	while (class->op_class) {
180 		if (opclass == class->op_class) {
181 			for (i = 0; (i < REG_MAX_CHANNELS_PER_OPERATING_CLASS &&
182 				     class->channels[i]); i++) {
183 				if (channel == class->channels[i])
184 					return class->ch_spacing;
185 			}
186 		}
187 		class++;
188 	}
189 
190 	return 0;
191 }
192 
193 uint16_t reg_dmn_get_opclass_from_channel(uint8_t *country, uint8_t channel,
194 					  uint8_t offset)
195 {
196 	const struct reg_dmn_op_class_map_t *class = NULL;
197 	uint16_t i = 0;
198 
199 	class = reg_get_class_from_country(country);
200 	while (class && class->op_class) {
201 		if ((offset == class->offset) || (offset == BWALL)) {
202 			for (i = 0; (i < REG_MAX_CHANNELS_PER_OPERATING_CLASS &&
203 				     class->channels[i]); i++) {
204 				if (channel == class->channels[i])
205 					return class->op_class;
206 			}
207 		}
208 		class++;
209 	}
210 
211 	return 0;
212 }
213 
214 void reg_dmn_print_channels_in_opclass(uint8_t *country, uint8_t op_class)
215 {
216 	const struct reg_dmn_op_class_map_t *class = NULL;
217 	uint16_t i = 0;
218 
219 	class = reg_get_class_from_country(country);
220 	while (class && class->op_class) {
221 		if (class->op_class == op_class) {
222 			for (i = 0;
223 			     (i < REG_MAX_CHANNELS_PER_OPERATING_CLASS &&
224 			      class->channels[i]); i++) {
225 				reg_debug("Valid channel(%d) in requested RC(%d)",
226 					  class->channels[i], op_class);
227 			}
228 			break;
229 		}
230 		class++;
231 	}
232 	if (!class->op_class)
233 		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
234 			  "Invalid requested RC (%d)", op_class);
235 }
236 
237 uint16_t reg_dmn_set_curr_opclasses(uint8_t num_classes, uint8_t *class)
238 {
239 	uint8_t i;
240 
241 	if (num_classes > REG_MAX_SUPP_OPER_CLASSES) {
242 		reg_err("invalid num classes %d", num_classes);
243 		return 0;
244 	}
245 
246 	for (i = 0; i < num_classes; i++)
247 		reg_dmn_curr_supp_opp_classes.classes[i] = class[i];
248 
249 	reg_dmn_curr_supp_opp_classes.num_classes = num_classes;
250 
251 	return 0;
252 }
253 
254 uint16_t reg_dmn_get_curr_opclasses(uint8_t *num_classes, uint8_t *class)
255 {
256 	uint8_t i;
257 
258 	if (!num_classes || !class) {
259 		reg_err("either num_classes or class is null");
260 		return 0;
261 	}
262 
263 	for (i = 0; i < reg_dmn_curr_supp_opp_classes.num_classes; i++)
264 		class[i] = reg_dmn_curr_supp_opp_classes.classes[i];
265 
266 	*num_classes = reg_dmn_curr_supp_opp_classes.num_classes;
267 
268 	return 0;
269 }
270 #endif
271