1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // aw88395_lib.c  -- ACF bin parsing and check library file for aw88395
4 //
5 // Copyright (c) 2022-2023 AWINIC Technology CO., LTD
6 //
7 // Author: Bruce zhao <zhaolei@awinic.com>
8 //
9 
10 #include <linux/crc8.h>
11 #include <linux/i2c.h>
12 #include "aw88395_lib.h"
13 #include "aw88395_device.h"
14 #include "aw88395_reg.h"
15 
16 #define AW88395_CRC8_POLYNOMIAL 0x8C
17 DECLARE_CRC8_TABLE(aw_crc8_table);
18 
19 static char *profile_name[AW88395_PROFILE_MAX] = {
20 	"Music", "Voice", "Voip", "Ringtone",
21 	"Ringtone_hs", "Lowpower", "Bypass",
22 	"Mmi", "Fm", "Notification", "Receiver"
23 };
24 
25 static int aw_parse_bin_header(struct aw_device *aw_dev, struct aw_bin *bin);
26 
27 static int aw_check_sum(struct aw_device *aw_dev, struct aw_bin *bin, int bin_num)
28 {
29 	unsigned char *p_check_sum;
30 	unsigned int sum_data = 0;
31 	unsigned int check_sum;
32 	unsigned int i, len;
33 
34 	p_check_sum = &(bin->info.data[(bin->header_info[bin_num].valid_data_addr -
35 						bin->header_info[bin_num].header_len)]);
36 	len = bin->header_info[bin_num].bin_data_len + bin->header_info[bin_num].header_len;
37 	check_sum = le32_to_cpup((void *)p_check_sum);
38 
39 	for (i = 4; i < len; i++)
40 		sum_data += *(p_check_sum + i);
41 
42 	dev_dbg(aw_dev->dev, "%s -- check_sum = %p, check_sum = 0x%x, sum_data = 0x%x",
43 					__func__, p_check_sum, check_sum, sum_data);
44 	if (sum_data != check_sum) {
45 		dev_err(aw_dev->dev, "%s. CheckSum Fail.bin_num=%d, CheckSum:0x%x, SumData:0x%x",
46 				__func__, bin_num, check_sum, sum_data);
47 		return -EINVAL;
48 	}
49 
50 	return 0;
51 }
52 
53 static int aw_check_data_version(struct aw_device *aw_dev, struct aw_bin *bin, int bin_num)
54 {
55 	if (bin->header_info[bin_num].bin_data_ver < DATA_VERSION_V1 ||
56 		bin->header_info[bin_num].bin_data_ver > DATA_VERSION_MAX) {
57 		dev_err(aw_dev->dev, "aw_bin_parse Unrecognized this bin data version\n");
58 		return -EINVAL;
59 	}
60 
61 	return 0;
62 }
63 
64 static int aw_check_register_num(struct aw_device *aw_dev, struct aw_bin *bin, int bin_num)
65 {
66 	struct bin_header_info temp_info = bin->header_info[bin_num];
67 	unsigned int check_register_num, parse_register_num;
68 	unsigned char *p_check_sum;
69 
70 	p_check_sum = &(bin->info.data[(temp_info.valid_data_addr)]);
71 
72 	parse_register_num = le32_to_cpup((void *)p_check_sum);
73 	check_register_num = (bin->header_info[bin_num].bin_data_len - CHECK_REGISTER_NUM_OFFSET) /
74 				(bin->header_info[bin_num].reg_byte_len +
75 				bin->header_info[bin_num].data_byte_len);
76 	dev_dbg(aw_dev->dev, "%s,parse_register_num = 0x%x,check_register_num = 0x%x\n",
77 				__func__, parse_register_num, check_register_num);
78 	if (parse_register_num != check_register_num) {
79 		dev_err(aw_dev->dev, "%s parse_register_num = 0x%x,check_register_num = 0x%x\n",
80 				__func__, parse_register_num, check_register_num);
81 		return -EINVAL;
82 	}
83 
84 	bin->header_info[bin_num].reg_num = parse_register_num;
85 	bin->header_info[bin_num].valid_data_len = temp_info.bin_data_len - VALID_DATA_LEN;
86 	bin->header_info[bin_num].valid_data_addr = temp_info.valid_data_addr + VALID_DATA_ADDR;
87 
88 	return 0;
89 }
90 
91 static int aw_check_dsp_reg_num(struct aw_device *aw_dev, struct aw_bin *bin, int bin_num)
92 {
93 	struct bin_header_info temp_info = bin->header_info[bin_num];
94 	unsigned int check_dsp_reg_num, parse_dsp_reg_num;
95 	unsigned char *p_check_sum;
96 
97 	p_check_sum = &(bin->info.data[(temp_info.valid_data_addr)]);
98 
99 	parse_dsp_reg_num = le32_to_cpup((void *)(p_check_sum + PARSE_DSP_REG_NUM));
100 	bin->header_info[bin_num].reg_data_byte_len =
101 			le32_to_cpup((void *)(p_check_sum + REG_DATA_BYTP_LEN));
102 	check_dsp_reg_num = (bin->header_info[bin_num].bin_data_len - CHECK_DSP_REG_NUM) /
103 				bin->header_info[bin_num].reg_data_byte_len;
104 	dev_dbg(aw_dev->dev, "%s bin_num = %d, parse_dsp_reg_num = 0x%x, check_dsp_reg_num = 0x%x",
105 					__func__, bin_num, check_dsp_reg_num, check_dsp_reg_num);
106 	if (parse_dsp_reg_num != check_dsp_reg_num) {
107 		dev_err(aw_dev->dev, "aw_bin_parse check dsp reg num error\n");
108 		dev_err(aw_dev->dev, "%s parse_dsp_reg_num = 0x%x, check_dsp_reg_num = 0x%x",
109 					__func__, check_dsp_reg_num, check_dsp_reg_num);
110 		return -EINVAL;
111 	}
112 
113 	bin->header_info[bin_num].download_addr = le32_to_cpup((void *)p_check_sum);
114 	bin->header_info[bin_num].reg_num = parse_dsp_reg_num;
115 	bin->header_info[bin_num].valid_data_len = temp_info.bin_data_len - DSP_VALID_DATA_LEN;
116 	bin->header_info[bin_num].valid_data_addr = temp_info.valid_data_addr +
117 								DSP_VALID_DATA_ADDR;
118 
119 	return 0;
120 }
121 
122 static int aw_check_soc_app_num(struct aw_device *aw_dev, struct aw_bin *bin, int bin_num)
123 {
124 	struct bin_header_info temp_info = bin->header_info[bin_num];
125 	unsigned int check_soc_app_num, parse_soc_app_num;
126 	unsigned char *p_check_sum;
127 
128 	p_check_sum = &(bin->info.data[(temp_info.valid_data_addr)]);
129 
130 	bin->header_info[bin_num].app_version = le32_to_cpup((void *)p_check_sum);
131 	parse_soc_app_num = le32_to_cpup((void *)(p_check_sum + PARSE_SOC_APP_NUM));
132 	check_soc_app_num = bin->header_info[bin_num].bin_data_len - CHECK_SOC_APP_NUM;
133 	dev_dbg(aw_dev->dev, "%s bin_num = %d, parse_soc_app_num=0x%x, check_soc_app_num = 0x%x\n",
134 					__func__, bin_num, parse_soc_app_num, check_soc_app_num);
135 	if (parse_soc_app_num != check_soc_app_num) {
136 		dev_err(aw_dev->dev, "%s parse_soc_app_num=0x%x, check_soc_app_num = 0x%x\n",
137 					__func__, parse_soc_app_num, check_soc_app_num);
138 		return -EINVAL;
139 	}
140 
141 	bin->header_info[bin_num].reg_num = parse_soc_app_num;
142 	bin->header_info[bin_num].download_addr = le32_to_cpup((void *)(p_check_sum +
143 								APP_DOWNLOAD_ADDR));
144 	bin->header_info[bin_num].valid_data_len = temp_info.bin_data_len - APP_VALID_DATA_LEN;
145 	bin->header_info[bin_num].valid_data_addr = temp_info.valid_data_addr +
146 								APP_VALID_DATA_ADDR;
147 
148 	return 0;
149 }
150 
151 static void aw_get_single_bin_header(struct aw_bin *bin)
152 {
153 	memcpy((void *)&bin->header_info[bin->all_bin_parse_num], bin->p_addr, DATA_LEN);
154 
155 	bin->header_info[bin->all_bin_parse_num].header_len = HEADER_LEN;
156 	bin->all_bin_parse_num += 1;
157 }
158 
159 static int aw_parse_one_of_multi_bins(struct aw_device *aw_dev, unsigned int bin_num,
160 					int bin_serial_num, struct aw_bin *bin)
161 {
162 	struct bin_header_info aw_bin_header_info;
163 	unsigned int bin_start_addr;
164 	unsigned int valid_data_len;
165 
166 	if (bin->info.len < sizeof(struct bin_header_info)) {
167 		dev_err(aw_dev->dev, "bin_header_info size[%d] overflow file size[%d]\n",
168 				(int)sizeof(struct bin_header_info), bin->info.len);
169 		return -EINVAL;
170 	}
171 
172 	aw_bin_header_info = bin->header_info[bin->all_bin_parse_num - 1];
173 	if (!bin_serial_num) {
174 		bin_start_addr = le32_to_cpup((void *)(bin->p_addr + START_ADDR_OFFSET));
175 		bin->p_addr += (HEADER_LEN + bin_start_addr);
176 		bin->header_info[bin->all_bin_parse_num].valid_data_addr =
177 			aw_bin_header_info.valid_data_addr + VALID_DATA_ADDR + 8 * bin_num +
178 			VALID_DATA_ADDR_OFFSET;
179 	} else {
180 		valid_data_len = aw_bin_header_info.bin_data_len;
181 		bin->p_addr += (HDADER_LEN + valid_data_len);
182 		bin->header_info[bin->all_bin_parse_num].valid_data_addr =
183 		    aw_bin_header_info.valid_data_addr + aw_bin_header_info.bin_data_len +
184 		    VALID_DATA_ADDR_OFFSET;
185 	}
186 
187 	return aw_parse_bin_header(aw_dev, bin);
188 }
189 
190 static int aw_get_multi_bin_header(struct aw_device *aw_dev, struct aw_bin *bin)
191 {
192 	unsigned int bin_num, i;
193 	int ret;
194 
195 	bin_num = le32_to_cpup((void *)(bin->p_addr + VALID_DATA_ADDR_OFFSET));
196 	if (bin->multi_bin_parse_num == 1)
197 		bin->header_info[bin->all_bin_parse_num].valid_data_addr =
198 							VALID_DATA_ADDR_OFFSET;
199 
200 	aw_get_single_bin_header(bin);
201 
202 	for (i = 0; i < bin_num; i++) {
203 		dev_dbg(aw_dev->dev, "aw_bin_parse enter multi bin for is %d\n", i);
204 		ret = aw_parse_one_of_multi_bins(aw_dev, bin_num, i, bin);
205 		if (ret < 0)
206 			return ret;
207 	}
208 
209 	return 0;
210 }
211 
212 static int aw_parse_bin_header(struct aw_device *aw_dev, struct aw_bin *bin)
213 {
214 	unsigned int bin_data_type;
215 
216 	if (bin->info.len < sizeof(struct bin_header_info)) {
217 		dev_err(aw_dev->dev, "bin_header_info size[%d] overflow file size[%d]\n",
218 				(int)sizeof(struct bin_header_info), bin->info.len);
219 		return -EINVAL;
220 	}
221 
222 	bin_data_type = le32_to_cpup((void *)(bin->p_addr + BIN_DATA_TYPE_OFFSET));
223 	dev_dbg(aw_dev->dev, "aw_bin_parse bin_data_type 0x%x\n", bin_data_type);
224 	switch (bin_data_type) {
225 	case DATA_TYPE_REGISTER:
226 	case DATA_TYPE_DSP_REG:
227 	case DATA_TYPE_SOC_APP:
228 		bin->single_bin_parse_num += 1;
229 		dev_dbg(aw_dev->dev, "%s bin->single_bin_parse_num is %d\n", __func__,
230 						bin->single_bin_parse_num);
231 		if (!bin->multi_bin_parse_num)
232 			bin->header_info[bin->all_bin_parse_num].valid_data_addr =
233 								VALID_DATA_ADDR_OFFSET;
234 		aw_get_single_bin_header(bin);
235 		return 0;
236 	case DATA_TYPE_MULTI_BINS:
237 		bin->multi_bin_parse_num += 1;
238 		dev_dbg(aw_dev->dev, "%s bin->multi_bin_parse_num is %d\n", __func__,
239 						bin->multi_bin_parse_num);
240 		return aw_get_multi_bin_header(aw_dev, bin);
241 	default:
242 		dev_dbg(aw_dev->dev, "%s There is no corresponding type\n", __func__);
243 		return 0;
244 	}
245 }
246 
247 static int aw_check_bin_header_version(struct aw_device *aw_dev, struct aw_bin *bin)
248 {
249 	unsigned int header_version;
250 
251 	header_version = le32_to_cpup((void *)(bin->p_addr + HEADER_VERSION_OFFSET));
252 	dev_dbg(aw_dev->dev, "aw_bin_parse header_version 0x%x\n", header_version);
253 
254 	switch (header_version) {
255 	case HEADER_VERSION_V1:
256 		return aw_parse_bin_header(aw_dev, bin);
257 	default:
258 		dev_err(aw_dev->dev, "aw_bin_parse Unrecognized this bin header version\n");
259 		return -EINVAL;
260 	}
261 }
262 
263 static int aw_parsing_bin_file(struct aw_device *aw_dev, struct aw_bin *bin)
264 {
265 	int ret = -EINVAL;
266 	int i;
267 
268 	if (!bin) {
269 		dev_err(aw_dev->dev, "aw_bin_parse bin is NULL\n");
270 		return ret;
271 	}
272 	bin->p_addr = bin->info.data;
273 	bin->all_bin_parse_num = 0;
274 	bin->multi_bin_parse_num = 0;
275 	bin->single_bin_parse_num = 0;
276 
277 	ret = aw_check_bin_header_version(aw_dev, bin);
278 	if (ret < 0) {
279 		dev_err(aw_dev->dev, "aw_bin_parse check bin header version error\n");
280 		return ret;
281 	}
282 
283 	for (i = 0; i < bin->all_bin_parse_num; i++) {
284 		ret = aw_check_sum(aw_dev, bin, i);
285 		if (ret < 0) {
286 			dev_err(aw_dev->dev, "aw_bin_parse check sum data error\n");
287 			return ret;
288 		}
289 		ret = aw_check_data_version(aw_dev, bin, i);
290 		if (ret < 0) {
291 			dev_err(aw_dev->dev, "aw_bin_parse check data version error\n");
292 			return ret;
293 		}
294 		if (bin->header_info[i].bin_data_ver == DATA_VERSION_V1) {
295 			switch (bin->header_info[i].bin_data_type) {
296 			case DATA_TYPE_REGISTER:
297 				ret = aw_check_register_num(aw_dev, bin, i);
298 				break;
299 			case DATA_TYPE_DSP_REG:
300 				ret = aw_check_dsp_reg_num(aw_dev, bin, i);
301 				break;
302 			case DATA_TYPE_SOC_APP:
303 				ret = aw_check_soc_app_num(aw_dev, bin, i);
304 				break;
305 			default:
306 				bin->header_info[i].valid_data_len =
307 						bin->header_info[i].bin_data_len;
308 				ret = 0;
309 				break;
310 			}
311 			if (ret < 0)
312 				return ret;
313 		}
314 	}
315 
316 	return 0;
317 }
318 
319 static int aw_dev_parse_raw_reg(unsigned char *data, unsigned int data_len,
320 		struct aw_prof_desc *prof_desc)
321 {
322 	prof_desc->sec_desc[AW88395_DATA_TYPE_REG].data = data;
323 	prof_desc->sec_desc[AW88395_DATA_TYPE_REG].len = data_len;
324 
325 	prof_desc->prof_st = AW88395_PROFILE_OK;
326 
327 	return 0;
328 }
329 
330 static int aw_dev_parse_raw_dsp_cfg(unsigned char *data, unsigned int data_len,
331 		struct aw_prof_desc *prof_desc)
332 {
333 	if (data_len & 0x01)
334 		return -EINVAL;
335 
336 	swab16_array((u16 *)data, data_len >> 1);
337 
338 	prof_desc->sec_desc[AW88395_DATA_TYPE_DSP_CFG].data = data;
339 	prof_desc->sec_desc[AW88395_DATA_TYPE_DSP_CFG].len = data_len;
340 
341 	prof_desc->prof_st = AW88395_PROFILE_OK;
342 
343 	return 0;
344 }
345 
346 static int aw_dev_parse_raw_dsp_fw(unsigned char *data,	unsigned int data_len,
347 		struct aw_prof_desc *prof_desc)
348 {
349 	if (data_len & 0x01)
350 		return -EINVAL;
351 
352 	swab16_array((u16 *)data, data_len >> 1);
353 
354 	prof_desc->sec_desc[AW88395_DATA_TYPE_DSP_FW].data = data;
355 	prof_desc->sec_desc[AW88395_DATA_TYPE_DSP_FW].len = data_len;
356 
357 	prof_desc->prof_st = AW88395_PROFILE_OK;
358 
359 	return 0;
360 }
361 
362 static int aw_dev_prof_parse_multi_bin(struct aw_device *aw_dev, unsigned char *data,
363 				unsigned int data_len, struct aw_prof_desc *prof_desc)
364 {
365 	struct aw_bin *aw_bin;
366 	int ret;
367 	int i;
368 
369 	aw_bin = devm_kzalloc(aw_dev->dev, data_len + sizeof(struct aw_bin), GFP_KERNEL);
370 	if (!aw_bin)
371 		return -ENOMEM;
372 
373 	aw_bin->info.len = data_len;
374 	memcpy(aw_bin->info.data, data, data_len);
375 
376 	ret = aw_parsing_bin_file(aw_dev, aw_bin);
377 	if (ret < 0) {
378 		dev_err(aw_dev->dev, "parse bin failed");
379 		goto parse_bin_failed;
380 	}
381 
382 	for (i = 0; i < aw_bin->all_bin_parse_num; i++) {
383 		switch (aw_bin->header_info[i].bin_data_type) {
384 		case DATA_TYPE_REGISTER:
385 			prof_desc->sec_desc[AW88395_DATA_TYPE_REG].len =
386 					aw_bin->header_info[i].valid_data_len;
387 			prof_desc->sec_desc[AW88395_DATA_TYPE_REG].data =
388 					data + aw_bin->header_info[i].valid_data_addr;
389 			break;
390 		case DATA_TYPE_DSP_REG:
391 			if (aw_bin->header_info[i].valid_data_len & 0x01) {
392 				ret = -EINVAL;
393 				goto parse_bin_failed;
394 			}
395 
396 			swab16_array((u16 *)(data + aw_bin->header_info[i].valid_data_addr),
397 					aw_bin->header_info[i].valid_data_len >> 1);
398 
399 			prof_desc->sec_desc[AW88395_DATA_TYPE_DSP_CFG].len =
400 					aw_bin->header_info[i].valid_data_len;
401 			prof_desc->sec_desc[AW88395_DATA_TYPE_DSP_CFG].data =
402 					data + aw_bin->header_info[i].valid_data_addr;
403 			break;
404 		case DATA_TYPE_DSP_FW:
405 		case DATA_TYPE_SOC_APP:
406 			if (aw_bin->header_info[i].valid_data_len & 0x01) {
407 				ret = -EINVAL;
408 				goto parse_bin_failed;
409 			}
410 
411 			swab16_array((u16 *)(data + aw_bin->header_info[i].valid_data_addr),
412 					aw_bin->header_info[i].valid_data_len >> 1);
413 
414 			prof_desc->fw_ver = aw_bin->header_info[i].app_version;
415 			prof_desc->sec_desc[AW88395_DATA_TYPE_DSP_FW].len =
416 					aw_bin->header_info[i].valid_data_len;
417 			prof_desc->sec_desc[AW88395_DATA_TYPE_DSP_FW].data =
418 					data + aw_bin->header_info[i].valid_data_addr;
419 			break;
420 		default:
421 			dev_dbg(aw_dev->dev, "bin_data_type not found");
422 			break;
423 		}
424 	}
425 	prof_desc->prof_st = AW88395_PROFILE_OK;
426 	ret =  0;
427 
428 parse_bin_failed:
429 	devm_kfree(aw_dev->dev, aw_bin);
430 	return ret;
431 }
432 
433 static int aw_dev_parse_reg_bin_with_hdr(struct aw_device *aw_dev,
434 			uint8_t *data, uint32_t data_len, struct aw_prof_desc *prof_desc)
435 {
436 	struct aw_bin *aw_bin;
437 	int ret;
438 
439 	aw_bin = devm_kzalloc(aw_dev->dev, data_len + sizeof(*aw_bin), GFP_KERNEL);
440 	if (!aw_bin)
441 		return -ENOMEM;
442 
443 	aw_bin->info.len = data_len;
444 	memcpy(aw_bin->info.data, data, data_len);
445 
446 	ret = aw_parsing_bin_file(aw_dev, aw_bin);
447 	if (ret < 0) {
448 		dev_err(aw_dev->dev, "parse bin failed");
449 		goto parse_bin_failed;
450 	}
451 
452 	if ((aw_bin->all_bin_parse_num != 1) ||
453 		(aw_bin->header_info[0].bin_data_type != DATA_TYPE_REGISTER)) {
454 		dev_err(aw_dev->dev, "bin num or type error");
455 		goto parse_bin_failed;
456 	}
457 
458 	if (aw_bin->header_info[0].valid_data_len % 4) {
459 		dev_err(aw_dev->dev, "bin data len get error!");
460 		goto parse_bin_failed;
461 	}
462 
463 	prof_desc->sec_desc[AW88395_DATA_TYPE_REG].data =
464 				data + aw_bin->header_info[0].valid_data_addr;
465 	prof_desc->sec_desc[AW88395_DATA_TYPE_REG].len =
466 				aw_bin->header_info[0].valid_data_len;
467 	prof_desc->prof_st = AW88395_PROFILE_OK;
468 
469 	devm_kfree(aw_dev->dev, aw_bin);
470 	aw_bin = NULL;
471 
472 	return 0;
473 
474 parse_bin_failed:
475 	devm_kfree(aw_dev->dev, aw_bin);
476 	aw_bin = NULL;
477 	return ret;
478 }
479 
480 static int aw_dev_parse_data_by_sec_type(struct aw_device *aw_dev, struct aw_cfg_hdr *cfg_hdr,
481 			struct aw_cfg_dde *cfg_dde, struct aw_prof_desc *scene_prof_desc)
482 {
483 	switch (cfg_dde->data_type) {
484 	case ACF_SEC_TYPE_REG:
485 		return aw_dev_parse_raw_reg((u8 *)cfg_hdr + cfg_dde->data_offset,
486 				cfg_dde->data_size, scene_prof_desc);
487 	case ACF_SEC_TYPE_DSP_CFG:
488 		return aw_dev_parse_raw_dsp_cfg((u8 *)cfg_hdr + cfg_dde->data_offset,
489 				cfg_dde->data_size, scene_prof_desc);
490 	case ACF_SEC_TYPE_DSP_FW:
491 		return aw_dev_parse_raw_dsp_fw(
492 				(u8 *)cfg_hdr + cfg_dde->data_offset,
493 				cfg_dde->data_size, scene_prof_desc);
494 	case ACF_SEC_TYPE_MULTIPLE_BIN:
495 		return aw_dev_prof_parse_multi_bin(
496 				aw_dev, (u8 *)cfg_hdr + cfg_dde->data_offset,
497 				cfg_dde->data_size, scene_prof_desc);
498 	case ACF_SEC_TYPE_HDR_REG:
499 		return aw_dev_parse_reg_bin_with_hdr(aw_dev, (u8 *)cfg_hdr + cfg_dde->data_offset,
500 				cfg_dde->data_size, scene_prof_desc);
501 	default:
502 		dev_err(aw_dev->dev, "%s cfg_dde->data_type = %d\n", __func__, cfg_dde->data_type);
503 		break;
504 	}
505 
506 	return 0;
507 }
508 
509 static int aw_dev_parse_dev_type(struct aw_device *aw_dev,
510 		struct aw_cfg_hdr *prof_hdr, struct aw_all_prof_info *all_prof_info)
511 {
512 	struct aw_cfg_dde *cfg_dde =
513 		(struct aw_cfg_dde *)((char *)prof_hdr + prof_hdr->hdr_offset);
514 	int sec_num = 0;
515 	int ret, i;
516 
517 	for (i = 0; i < prof_hdr->ddt_num; i++) {
518 		if ((aw_dev->i2c->adapter->nr == cfg_dde[i].dev_bus) &&
519 		    (aw_dev->i2c->addr == cfg_dde[i].dev_addr) &&
520 		    (cfg_dde[i].type == AW88395_DEV_TYPE_ID) &&
521 		    (cfg_dde[i].data_type != ACF_SEC_TYPE_MONITOR)) {
522 			if (cfg_dde[i].dev_profile >= AW88395_PROFILE_MAX) {
523 				dev_err(aw_dev->dev, "dev_profile [%d] overflow",
524 							cfg_dde[i].dev_profile);
525 				return -EINVAL;
526 			}
527 
528 			ret = aw_dev_parse_data_by_sec_type(aw_dev, prof_hdr, &cfg_dde[i],
529 					&all_prof_info->prof_desc[cfg_dde[i].dev_profile]);
530 			if (ret < 0) {
531 				dev_err(aw_dev->dev, "parse failed");
532 				return ret;
533 			}
534 			sec_num++;
535 		}
536 	}
537 
538 	if (sec_num == 0) {
539 		dev_dbg(aw_dev->dev, "get dev type num is %d, please use default", sec_num);
540 		return AW88395_DEV_TYPE_NONE;
541 	}
542 
543 	return AW88395_DEV_TYPE_OK;
544 }
545 
546 static int aw_dev_parse_dev_default_type(struct aw_device *aw_dev,
547 		struct aw_cfg_hdr *prof_hdr, struct aw_all_prof_info *all_prof_info)
548 {
549 	struct aw_cfg_dde *cfg_dde =
550 		(struct aw_cfg_dde *)((char *)prof_hdr + prof_hdr->hdr_offset);
551 	int sec_num = 0;
552 	int ret, i;
553 
554 	for (i = 0; i < prof_hdr->ddt_num; i++) {
555 		if ((aw_dev->channel == cfg_dde[i].dev_index) &&
556 		    (cfg_dde[i].type == AW88395_DEV_DEFAULT_TYPE_ID) &&
557 		    (cfg_dde[i].data_type != ACF_SEC_TYPE_MONITOR)) {
558 			if (cfg_dde[i].dev_profile >= AW88395_PROFILE_MAX) {
559 				dev_err(aw_dev->dev, "dev_profile [%d] overflow",
560 					cfg_dde[i].dev_profile);
561 				return -EINVAL;
562 			}
563 			ret = aw_dev_parse_data_by_sec_type(aw_dev, prof_hdr, &cfg_dde[i],
564 					&all_prof_info->prof_desc[cfg_dde[i].dev_profile]);
565 			if (ret < 0) {
566 				dev_err(aw_dev->dev, "parse failed");
567 				return ret;
568 			}
569 			sec_num++;
570 		}
571 	}
572 
573 	if (sec_num == 0) {
574 		dev_err(aw_dev->dev, "get dev default type failed, get num[%d]", sec_num);
575 		return -EINVAL;
576 	}
577 
578 	return 0;
579 }
580 
581 static int aw88261_dev_cfg_get_valid_prof(struct aw_device *aw_dev,
582 				struct aw_all_prof_info all_prof_info)
583 {
584 	struct aw_prof_desc *prof_desc = all_prof_info.prof_desc;
585 	struct aw_prof_info *prof_info = &aw_dev->prof_info;
586 	int num = 0;
587 	int i;
588 
589 	for (i = 0; i < AW88395_PROFILE_MAX; i++) {
590 		if (prof_desc[i].prof_st == AW88395_PROFILE_OK)
591 			prof_info->count++;
592 	}
593 
594 	dev_dbg(aw_dev->dev, "get valid profile:%d", aw_dev->prof_info.count);
595 
596 	if (!prof_info->count) {
597 		dev_err(aw_dev->dev, "no profile data");
598 		return -EPERM;
599 	}
600 
601 	prof_info->prof_desc = devm_kcalloc(aw_dev->dev,
602 					prof_info->count, sizeof(struct aw_prof_desc),
603 					GFP_KERNEL);
604 	if (!prof_info->prof_desc)
605 		return -ENOMEM;
606 
607 	for (i = 0; i < AW88395_PROFILE_MAX; i++) {
608 		if (prof_desc[i].prof_st == AW88395_PROFILE_OK) {
609 			if (num >= prof_info->count) {
610 				dev_err(aw_dev->dev, "overflow count[%d]",
611 						prof_info->count);
612 				return -EINVAL;
613 			}
614 			prof_info->prof_desc[num] = prof_desc[i];
615 			prof_info->prof_desc[num].id = i;
616 			num++;
617 		}
618 	}
619 
620 	return 0;
621 }
622 
623 static int aw88395_dev_cfg_get_valid_prof(struct aw_device *aw_dev,
624 				struct aw_all_prof_info all_prof_info)
625 {
626 	struct aw_prof_desc *prof_desc = all_prof_info.prof_desc;
627 	struct aw_prof_info *prof_info = &aw_dev->prof_info;
628 	struct aw_sec_data_desc *sec_desc;
629 	int num = 0;
630 	int i;
631 
632 	for (i = 0; i < AW88395_PROFILE_MAX; i++) {
633 		if (prof_desc[i].prof_st == AW88395_PROFILE_OK) {
634 			sec_desc = prof_desc[i].sec_desc;
635 			if ((sec_desc[AW88395_DATA_TYPE_REG].data != NULL) &&
636 			    (sec_desc[AW88395_DATA_TYPE_REG].len != 0) &&
637 			    (sec_desc[AW88395_DATA_TYPE_DSP_CFG].data != NULL) &&
638 			    (sec_desc[AW88395_DATA_TYPE_DSP_CFG].len != 0) &&
639 			    (sec_desc[AW88395_DATA_TYPE_DSP_FW].data != NULL) &&
640 			    (sec_desc[AW88395_DATA_TYPE_DSP_FW].len != 0))
641 				prof_info->count++;
642 		}
643 	}
644 
645 	dev_dbg(aw_dev->dev, "get valid profile:%d", aw_dev->prof_info.count);
646 
647 	if (!prof_info->count) {
648 		dev_err(aw_dev->dev, "no profile data");
649 		return -EPERM;
650 	}
651 
652 	prof_info->prof_desc = devm_kcalloc(aw_dev->dev,
653 					prof_info->count, sizeof(struct aw_prof_desc),
654 					GFP_KERNEL);
655 	if (!prof_info->prof_desc)
656 		return -ENOMEM;
657 
658 	for (i = 0; i < AW88395_PROFILE_MAX; i++) {
659 		if (prof_desc[i].prof_st == AW88395_PROFILE_OK) {
660 			sec_desc = prof_desc[i].sec_desc;
661 			if ((sec_desc[AW88395_DATA_TYPE_REG].data != NULL) &&
662 			    (sec_desc[AW88395_DATA_TYPE_REG].len != 0) &&
663 			    (sec_desc[AW88395_DATA_TYPE_DSP_CFG].data != NULL) &&
664 			    (sec_desc[AW88395_DATA_TYPE_DSP_CFG].len != 0) &&
665 			    (sec_desc[AW88395_DATA_TYPE_DSP_FW].data != NULL) &&
666 			    (sec_desc[AW88395_DATA_TYPE_DSP_FW].len != 0)) {
667 				if (num >= prof_info->count) {
668 					dev_err(aw_dev->dev, "overflow count[%d]",
669 							prof_info->count);
670 					return -EINVAL;
671 				}
672 				prof_info->prof_desc[num] = prof_desc[i];
673 				prof_info->prof_desc[num].id = i;
674 				num++;
675 			}
676 		}
677 	}
678 
679 	return 0;
680 }
681 
682 static int aw_dev_load_cfg_by_hdr(struct aw_device *aw_dev,
683 		struct aw_cfg_hdr *prof_hdr)
684 {
685 	struct aw_all_prof_info *all_prof_info;
686 	int ret;
687 
688 	all_prof_info = devm_kzalloc(aw_dev->dev, sizeof(struct aw_all_prof_info), GFP_KERNEL);
689 	if (!all_prof_info)
690 		return -ENOMEM;
691 
692 	ret = aw_dev_parse_dev_type(aw_dev, prof_hdr, all_prof_info);
693 	if (ret < 0) {
694 		goto exit;
695 	} else if (ret == AW88395_DEV_TYPE_NONE) {
696 		dev_dbg(aw_dev->dev, "get dev type num is 0, parse default dev");
697 		ret = aw_dev_parse_dev_default_type(aw_dev, prof_hdr, all_prof_info);
698 		if (ret < 0)
699 			goto exit;
700 	}
701 
702 	switch (aw_dev->chip_id) {
703 	case AW88395_CHIP_ID:
704 		ret = aw88395_dev_cfg_get_valid_prof(aw_dev, *all_prof_info);
705 		if (ret < 0)
706 			goto exit;
707 		break;
708 	case AW88261_CHIP_ID:
709 		ret = aw88261_dev_cfg_get_valid_prof(aw_dev, *all_prof_info);
710 		if (ret < 0)
711 			goto exit;
712 		break;
713 	default:
714 		dev_err(aw_dev->dev, "valid prof unsupported");
715 		ret = -EINVAL;
716 		break;
717 	}
718 
719 	aw_dev->prof_info.prof_name_list = profile_name;
720 
721 exit:
722 	devm_kfree(aw_dev->dev, all_prof_info);
723 	return ret;
724 }
725 
726 static int aw_dev_create_prof_name_list_v1(struct aw_device *aw_dev)
727 {
728 	struct aw_prof_info *prof_info = &aw_dev->prof_info;
729 	struct aw_prof_desc *prof_desc = prof_info->prof_desc;
730 	int i;
731 
732 	if (!prof_desc) {
733 		dev_err(aw_dev->dev, "prof_desc is NULL");
734 		return -EINVAL;
735 	}
736 
737 	prof_info->prof_name_list = devm_kzalloc(aw_dev->dev,
738 					prof_info->count * PROFILE_STR_MAX,
739 					GFP_KERNEL);
740 	if (!prof_info->prof_name_list)
741 		return -ENOMEM;
742 
743 	for (i = 0; i < prof_info->count; i++) {
744 		prof_desc[i].id = i;
745 		prof_info->prof_name_list[i] = prof_desc[i].prf_str;
746 		dev_dbg(aw_dev->dev, "prof name is %s", prof_info->prof_name_list[i]);
747 	}
748 
749 	return 0;
750 }
751 
752 static int aw_get_dde_type_info(struct aw_device *aw_dev, struct aw_container *aw_cfg)
753 {
754 	struct aw_cfg_hdr *cfg_hdr = (struct aw_cfg_hdr *)aw_cfg->data;
755 	struct aw_cfg_dde_v1 *cfg_dde =
756 		(struct aw_cfg_dde_v1 *)(aw_cfg->data + cfg_hdr->hdr_offset);
757 	int default_num = 0;
758 	int dev_num = 0;
759 	unsigned int i;
760 
761 	for (i = 0; i < cfg_hdr->ddt_num; i++) {
762 		if (cfg_dde[i].type == AW88395_DEV_TYPE_ID)
763 			dev_num++;
764 
765 		if (cfg_dde[i].type == AW88395_DEV_DEFAULT_TYPE_ID)
766 			default_num++;
767 	}
768 
769 	if (dev_num != 0) {
770 		aw_dev->prof_info.prof_type = AW88395_DEV_TYPE_ID;
771 	} else if (default_num != 0) {
772 		aw_dev->prof_info.prof_type = AW88395_DEV_DEFAULT_TYPE_ID;
773 	} else {
774 		dev_err(aw_dev->dev, "can't find scene");
775 		return -EINVAL;
776 	}
777 
778 	return 0;
779 }
780 
781 static int aw_get_dev_scene_count_v1(struct aw_device *aw_dev, struct aw_container *aw_cfg,
782 						unsigned int *scene_num)
783 {
784 	struct aw_cfg_hdr *cfg_hdr = (struct aw_cfg_hdr *)aw_cfg->data;
785 	struct aw_cfg_dde_v1 *cfg_dde =
786 		(struct aw_cfg_dde_v1 *)(aw_cfg->data + cfg_hdr->hdr_offset);
787 	unsigned int i;
788 	int ret;
789 
790 	switch (aw_dev->chip_id) {
791 	case AW88395_CHIP_ID:
792 		for (i = 0; i < cfg_hdr->ddt_num; ++i) {
793 			if ((cfg_dde[i].data_type == ACF_SEC_TYPE_MULTIPLE_BIN) &&
794 			    (aw_dev->chip_id == cfg_dde[i].chip_id) &&
795 			    (aw_dev->i2c->adapter->nr == cfg_dde[i].dev_bus) &&
796 			    (aw_dev->i2c->addr == cfg_dde[i].dev_addr))
797 				(*scene_num)++;
798 		}
799 		ret = 0;
800 		break;
801 	case AW88261_CHIP_ID:
802 		for (i = 0; i < cfg_hdr->ddt_num; ++i) {
803 			if (((cfg_dde[i].data_type == ACF_SEC_TYPE_REG) ||
804 			     (cfg_dde[i].data_type == ACF_SEC_TYPE_HDR_REG)) &&
805 			    (aw_dev->chip_id == cfg_dde[i].chip_id) &&
806 			    (aw_dev->i2c->adapter->nr == cfg_dde[i].dev_bus) &&
807 			    (aw_dev->i2c->addr == cfg_dde[i].dev_addr))
808 				(*scene_num)++;
809 		}
810 		ret = 0;
811 		break;
812 	default:
813 		dev_err(aw_dev->dev, "unsupported device");
814 		ret = -EINVAL;
815 		break;
816 	}
817 
818 	return ret;
819 }
820 
821 static int aw_get_default_scene_count_v1(struct aw_device *aw_dev,
822 						struct aw_container *aw_cfg,
823 						unsigned int *scene_num)
824 {
825 	struct aw_cfg_hdr *cfg_hdr = (struct aw_cfg_hdr *)aw_cfg->data;
826 	struct aw_cfg_dde_v1 *cfg_dde =
827 		(struct aw_cfg_dde_v1 *)(aw_cfg->data + cfg_hdr->hdr_offset);
828 	unsigned int i;
829 	int ret;
830 
831 	switch (aw_dev->chip_id) {
832 	case AW88395_CHIP_ID:
833 		for (i = 0; i < cfg_hdr->ddt_num; ++i) {
834 			if ((cfg_dde[i].data_type == ACF_SEC_TYPE_MULTIPLE_BIN) &&
835 			    (aw_dev->chip_id == cfg_dde[i].chip_id) &&
836 			    (aw_dev->channel == cfg_dde[i].dev_index))
837 				(*scene_num)++;
838 		}
839 		ret = 0;
840 		break;
841 	case AW88261_CHIP_ID:
842 		for (i = 0; i < cfg_hdr->ddt_num; ++i) {
843 			if (((cfg_dde[i].data_type == ACF_SEC_TYPE_REG) ||
844 			     (cfg_dde[i].data_type == ACF_SEC_TYPE_HDR_REG)) &&
845 			    (aw_dev->chip_id == cfg_dde[i].chip_id) &&
846 			    (aw_dev->channel == cfg_dde[i].dev_index))
847 				(*scene_num)++;
848 		}
849 		ret = 0;
850 		break;
851 	default:
852 		dev_err(aw_dev->dev, "unsupported device");
853 		ret = -EINVAL;
854 		break;
855 	}
856 
857 	return ret;
858 }
859 
860 static int aw_dev_parse_scene_count_v1(struct aw_device *aw_dev,
861 							struct aw_container *aw_cfg,
862 							unsigned int *count)
863 {
864 	int ret;
865 
866 	ret = aw_get_dde_type_info(aw_dev, aw_cfg);
867 	if (ret < 0)
868 		return ret;
869 
870 	switch (aw_dev->prof_info.prof_type) {
871 	case AW88395_DEV_TYPE_ID:
872 		ret = aw_get_dev_scene_count_v1(aw_dev, aw_cfg, count);
873 		break;
874 	case AW88395_DEV_DEFAULT_TYPE_ID:
875 		ret = aw_get_default_scene_count_v1(aw_dev, aw_cfg, count);
876 		break;
877 	default:
878 		dev_err(aw_dev->dev, "unsupported prof_type[%x]", aw_dev->prof_info.prof_type);
879 		ret = -EINVAL;
880 		break;
881 	}
882 
883 	return ret;
884 }
885 
886 static int aw_dev_parse_data_by_sec_type_v1(struct aw_device *aw_dev,
887 							struct aw_cfg_hdr *prof_hdr,
888 							struct aw_cfg_dde_v1 *cfg_dde,
889 							int *cur_scene_id)
890 {
891 	struct aw_prof_info *prof_info = &aw_dev->prof_info;
892 	int ret;
893 
894 	switch (cfg_dde->data_type) {
895 	case ACF_SEC_TYPE_MULTIPLE_BIN:
896 		ret = aw_dev_prof_parse_multi_bin(aw_dev, (u8 *)prof_hdr + cfg_dde->data_offset,
897 					cfg_dde->data_size, &prof_info->prof_desc[*cur_scene_id]);
898 		if (ret < 0) {
899 			dev_err(aw_dev->dev, "parse multi bin failed");
900 			return ret;
901 		}
902 		prof_info->prof_desc[*cur_scene_id].prf_str = cfg_dde->dev_profile_str;
903 		prof_info->prof_desc[*cur_scene_id].id = cfg_dde->dev_profile;
904 		(*cur_scene_id)++;
905 		break;
906 	case ACF_SEC_TYPE_HDR_REG:
907 		ret =  aw_dev_parse_reg_bin_with_hdr(aw_dev,
908 				(uint8_t *)prof_hdr + cfg_dde->data_offset,
909 				cfg_dde->data_size, &prof_info->prof_desc[*cur_scene_id]);
910 		if (ret < 0) {
911 			dev_err(aw_dev->dev, "parse reg bin with hdr failed");
912 			return ret;
913 		}
914 		prof_info->prof_desc[*cur_scene_id].prf_str = cfg_dde->dev_profile_str;
915 		prof_info->prof_desc[*cur_scene_id].id = cfg_dde->dev_profile;
916 		(*cur_scene_id)++;
917 		break;
918 	default:
919 		dev_err(aw_dev->dev, "unsupported SEC_TYPE [%d]", cfg_dde->data_type);
920 		return -EINVAL;
921 	}
922 
923 	return 0;
924 }
925 
926 static int aw_dev_parse_dev_type_v1(struct aw_device *aw_dev,
927 		struct aw_cfg_hdr *prof_hdr)
928 {
929 	struct aw_cfg_dde_v1 *cfg_dde =
930 		(struct aw_cfg_dde_v1 *)((char *)prof_hdr + prof_hdr->hdr_offset);
931 	int cur_scene_id = 0;
932 	unsigned int i;
933 	int ret;
934 
935 	for (i = 0; i < prof_hdr->ddt_num; i++) {
936 		if ((aw_dev->i2c->adapter->nr == cfg_dde[i].dev_bus) &&
937 		    (aw_dev->i2c->addr == cfg_dde[i].dev_addr) &&
938 		    (aw_dev->chip_id == cfg_dde[i].chip_id)) {
939 			ret = aw_dev_parse_data_by_sec_type_v1(aw_dev, prof_hdr,
940 							&cfg_dde[i], &cur_scene_id);
941 			if (ret < 0) {
942 				dev_err(aw_dev->dev, "parse failed");
943 				return ret;
944 			}
945 		}
946 	}
947 
948 	if (cur_scene_id == 0) {
949 		dev_err(aw_dev->dev, "get dev type failed, get num [%d]", cur_scene_id);
950 		return -EINVAL;
951 	}
952 
953 	return 0;
954 }
955 
956 static int aw_dev_parse_default_type_v1(struct aw_device *aw_dev,
957 		struct aw_cfg_hdr *prof_hdr)
958 {
959 	struct aw_cfg_dde_v1 *cfg_dde =
960 		(struct aw_cfg_dde_v1 *)((char *)prof_hdr + prof_hdr->hdr_offset);
961 	int cur_scene_id = 0;
962 	unsigned int i;
963 	int ret;
964 
965 	for (i = 0; i < prof_hdr->ddt_num; i++) {
966 		if ((aw_dev->channel == cfg_dde[i].dev_index) &&
967 			(aw_dev->chip_id == cfg_dde[i].chip_id)) {
968 			ret = aw_dev_parse_data_by_sec_type_v1(aw_dev, prof_hdr,
969 							&cfg_dde[i], &cur_scene_id);
970 			if (ret < 0) {
971 				dev_err(aw_dev->dev, "parse failed");
972 				return ret;
973 			}
974 		}
975 	}
976 
977 	if (cur_scene_id == 0) {
978 		dev_err(aw_dev->dev, "get dev default type failed, get num[%d]", cur_scene_id);
979 		return -EINVAL;
980 	}
981 
982 	return 0;
983 }
984 
985 static int aw_dev_parse_by_hdr_v1(struct aw_device *aw_dev,
986 		struct aw_cfg_hdr *cfg_hdr)
987 {
988 	int ret;
989 
990 	switch (aw_dev->prof_info.prof_type) {
991 	case AW88395_DEV_TYPE_ID:
992 		ret = aw_dev_parse_dev_type_v1(aw_dev, cfg_hdr);
993 		break;
994 	case AW88395_DEV_DEFAULT_TYPE_ID:
995 		ret = aw_dev_parse_default_type_v1(aw_dev, cfg_hdr);
996 		break;
997 	default:
998 		dev_err(aw_dev->dev, "prof type matched failed, get num[%d]",
999 			aw_dev->prof_info.prof_type);
1000 		ret =  -EINVAL;
1001 		break;
1002 	}
1003 
1004 	return ret;
1005 }
1006 
1007 static int aw_dev_load_cfg_by_hdr_v1(struct aw_device *aw_dev,
1008 						struct aw_container *aw_cfg)
1009 {
1010 	struct aw_cfg_hdr *cfg_hdr = (struct aw_cfg_hdr *)aw_cfg->data;
1011 	struct aw_prof_info *prof_info = &aw_dev->prof_info;
1012 	int ret;
1013 
1014 	ret = aw_dev_parse_scene_count_v1(aw_dev, aw_cfg, &prof_info->count);
1015 	if (ret < 0) {
1016 		dev_err(aw_dev->dev, "get scene count failed");
1017 		return ret;
1018 	}
1019 
1020 	prof_info->prof_desc = devm_kcalloc(aw_dev->dev,
1021 					prof_info->count, sizeof(struct aw_prof_desc),
1022 					GFP_KERNEL);
1023 	if (!prof_info->prof_desc)
1024 		return -ENOMEM;
1025 
1026 	ret = aw_dev_parse_by_hdr_v1(aw_dev, cfg_hdr);
1027 	if (ret < 0) {
1028 		dev_err(aw_dev->dev, "parse hdr failed");
1029 		return ret;
1030 	}
1031 
1032 	ret = aw_dev_create_prof_name_list_v1(aw_dev);
1033 	if (ret < 0) {
1034 		dev_err(aw_dev->dev, "create prof name list failed");
1035 		return ret;
1036 	}
1037 
1038 	return 0;
1039 }
1040 
1041 int aw88395_dev_cfg_load(struct aw_device *aw_dev, struct aw_container *aw_cfg)
1042 {
1043 	struct aw_cfg_hdr *cfg_hdr;
1044 	int ret;
1045 
1046 	cfg_hdr = (struct aw_cfg_hdr *)aw_cfg->data;
1047 
1048 	switch (cfg_hdr->hdr_version) {
1049 	case AW88395_CFG_HDR_VER:
1050 		ret = aw_dev_load_cfg_by_hdr(aw_dev, cfg_hdr);
1051 		if (ret < 0) {
1052 			dev_err(aw_dev->dev, "hdr_version[0x%x] parse failed",
1053 						cfg_hdr->hdr_version);
1054 			return ret;
1055 		}
1056 		break;
1057 	case AW88395_CFG_HDR_VER_V1:
1058 		ret = aw_dev_load_cfg_by_hdr_v1(aw_dev, aw_cfg);
1059 		if (ret < 0) {
1060 			dev_err(aw_dev->dev, "hdr_version[0x%x] parse failed",
1061 						cfg_hdr->hdr_version);
1062 			return ret;
1063 		}
1064 		break;
1065 	default:
1066 		dev_err(aw_dev->dev, "unsupported hdr_version [0x%x]", cfg_hdr->hdr_version);
1067 		return -EINVAL;
1068 	}
1069 	aw_dev->fw_status = AW88395_DEV_FW_OK;
1070 
1071 	return 0;
1072 }
1073 EXPORT_SYMBOL_GPL(aw88395_dev_cfg_load);
1074 
1075 static int aw_dev_check_cfg_by_hdr(struct aw_device *aw_dev, struct aw_container *aw_cfg)
1076 {
1077 	unsigned int end_data_offset;
1078 	struct aw_cfg_hdr *cfg_hdr;
1079 	struct aw_cfg_dde *cfg_dde;
1080 	unsigned int act_data = 0;
1081 	unsigned int hdr_ddt_len;
1082 	unsigned int i;
1083 	u8 act_crc8;
1084 
1085 	cfg_hdr = (struct aw_cfg_hdr *)aw_cfg->data;
1086 	/* check file type id is awinic acf file */
1087 	if (cfg_hdr->id != ACF_FILE_ID) {
1088 		dev_err(aw_dev->dev, "not acf type file");
1089 		return -EINVAL;
1090 	}
1091 
1092 	hdr_ddt_len = cfg_hdr->hdr_offset + cfg_hdr->ddt_size;
1093 	if (hdr_ddt_len > aw_cfg->len) {
1094 		dev_err(aw_dev->dev, "hdr_len with ddt_len [%d] overflow file size[%d]",
1095 		cfg_hdr->hdr_offset, aw_cfg->len);
1096 		return -EINVAL;
1097 	}
1098 
1099 	/* check data size */
1100 	cfg_dde = (struct aw_cfg_dde *)((char *)aw_cfg->data + cfg_hdr->hdr_offset);
1101 	act_data += hdr_ddt_len;
1102 	for (i = 0; i < cfg_hdr->ddt_num; i++)
1103 		act_data += cfg_dde[i].data_size;
1104 
1105 	if (act_data != aw_cfg->len) {
1106 		dev_err(aw_dev->dev, "act_data[%d] not equal to file size[%d]!",
1107 			act_data, aw_cfg->len);
1108 		return -EINVAL;
1109 	}
1110 
1111 	for (i = 0; i < cfg_hdr->ddt_num; i++) {
1112 		/* data check */
1113 		end_data_offset = cfg_dde[i].data_offset + cfg_dde[i].data_size;
1114 		if (end_data_offset > aw_cfg->len) {
1115 			dev_err(aw_dev->dev, "ddt_num[%d] end_data_offset[%d] overflow size[%d]",
1116 				i, end_data_offset, aw_cfg->len);
1117 			return -EINVAL;
1118 		}
1119 
1120 		/* crc check */
1121 		act_crc8 = crc8(aw_crc8_table, aw_cfg->data + cfg_dde[i].data_offset,
1122 							cfg_dde[i].data_size, 0);
1123 		if (act_crc8 != cfg_dde[i].data_crc) {
1124 			dev_err(aw_dev->dev, "ddt_num[%d] act_crc8:0x%x != data_crc:0x%x",
1125 				i, (u32)act_crc8, cfg_dde[i].data_crc);
1126 			return -EINVAL;
1127 		}
1128 	}
1129 
1130 	return 0;
1131 }
1132 
1133 static int aw_dev_check_acf_by_hdr_v1(struct aw_device *aw_dev, struct aw_container *aw_cfg)
1134 {
1135 	struct aw_cfg_dde_v1 *cfg_dde;
1136 	unsigned int end_data_offset;
1137 	struct aw_cfg_hdr *cfg_hdr;
1138 	unsigned int act_data = 0;
1139 	unsigned int hdr_ddt_len;
1140 	u8 act_crc8;
1141 	int i;
1142 
1143 	cfg_hdr = (struct aw_cfg_hdr *)aw_cfg->data;
1144 
1145 	/* check file type id is awinic acf file */
1146 	if (cfg_hdr->id != ACF_FILE_ID) {
1147 		dev_err(aw_dev->dev, "not acf type file");
1148 		return -EINVAL;
1149 	}
1150 
1151 	hdr_ddt_len = cfg_hdr->hdr_offset + cfg_hdr->ddt_size;
1152 	if (hdr_ddt_len > aw_cfg->len) {
1153 		dev_err(aw_dev->dev, "hdrlen with ddt_len [%d] overflow file size[%d]",
1154 		cfg_hdr->hdr_offset, aw_cfg->len);
1155 		return -EINVAL;
1156 	}
1157 
1158 	/* check data size */
1159 	cfg_dde = (struct aw_cfg_dde_v1 *)((char *)aw_cfg->data + cfg_hdr->hdr_offset);
1160 	act_data += hdr_ddt_len;
1161 	for (i = 0; i < cfg_hdr->ddt_num; i++)
1162 		act_data += cfg_dde[i].data_size;
1163 
1164 	if (act_data != aw_cfg->len) {
1165 		dev_err(aw_dev->dev, "act_data[%d] not equal to file size[%d]!",
1166 			act_data, aw_cfg->len);
1167 		return -EINVAL;
1168 	}
1169 
1170 	for (i = 0; i < cfg_hdr->ddt_num; i++) {
1171 		/* data check */
1172 		end_data_offset = cfg_dde[i].data_offset + cfg_dde[i].data_size;
1173 		if (end_data_offset > aw_cfg->len) {
1174 			dev_err(aw_dev->dev, "ddt_num[%d] end_data_offset[%d] overflow size[%d]",
1175 				i, end_data_offset, aw_cfg->len);
1176 			return -EINVAL;
1177 		}
1178 
1179 		/* crc check */
1180 		act_crc8 = crc8(aw_crc8_table, aw_cfg->data + cfg_dde[i].data_offset,
1181 									cfg_dde[i].data_size, 0);
1182 		if (act_crc8 != cfg_dde[i].data_crc) {
1183 			dev_err(aw_dev->dev, "ddt_num[%d] act_crc8:0x%x != data_crc 0x%x",
1184 				i, (u32)act_crc8, cfg_dde[i].data_crc);
1185 			return -EINVAL;
1186 		}
1187 	}
1188 
1189 	return 0;
1190 }
1191 
1192 int aw88395_dev_load_acf_check(struct aw_device *aw_dev, struct aw_container *aw_cfg)
1193 {
1194 	struct aw_cfg_hdr *cfg_hdr;
1195 
1196 	if (!aw_cfg) {
1197 		dev_err(aw_dev->dev, "aw_prof is NULL");
1198 		return -EINVAL;
1199 	}
1200 
1201 	if (aw_cfg->len < sizeof(struct aw_cfg_hdr)) {
1202 		dev_err(aw_dev->dev, "cfg hdr size[%d] overflow file size[%d]",
1203 			aw_cfg->len, (int)sizeof(struct aw_cfg_hdr));
1204 		return -EINVAL;
1205 	}
1206 
1207 	crc8_populate_lsb(aw_crc8_table, AW88395_CRC8_POLYNOMIAL);
1208 
1209 	cfg_hdr = (struct aw_cfg_hdr *)aw_cfg->data;
1210 	switch (cfg_hdr->hdr_version) {
1211 	case AW88395_CFG_HDR_VER:
1212 		return aw_dev_check_cfg_by_hdr(aw_dev, aw_cfg);
1213 	case AW88395_CFG_HDR_VER_V1:
1214 		return aw_dev_check_acf_by_hdr_v1(aw_dev, aw_cfg);
1215 	default:
1216 		dev_err(aw_dev->dev, "unsupported hdr_version [0x%x]", cfg_hdr->hdr_version);
1217 		return -EINVAL;
1218 	}
1219 
1220 	return 0;
1221 }
1222 EXPORT_SYMBOL_GPL(aw88395_dev_load_acf_check);
1223 
1224 MODULE_DESCRIPTION("AW88395 ACF File Parsing Lib");
1225 MODULE_LICENSE("GPL v2");
1226