Skip to content

Commit 1cc509e

Browse files
Shenghao-Dingbroonie
authored andcommitted
ASoc: TAS2781: replace beXX_to_cpup with get_unaligned_beXX for potentially broken alignment
Use get_unaligned_be16 instead of be16_to_cpup and get_unaligned_be32 instead of be32_to_cpup for potentially broken alignment. Signed-off-by: Shenghao Ding <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent ccb367c commit 1cc509e

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

sound/soc/codecs/tas2781-fmwlib.c

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <sound/soc.h>
2222
#include <sound/tlv.h>
2323
#include <sound/tas2781.h>
24-
24+
#include <asm/unaligned.h>
2525

2626
#define ERROR_PRAM_CRCCHK 0x0000000
2727
#define ERROR_YRAM_CRCCHK 0x0000001
@@ -187,8 +187,7 @@ static struct tasdevice_config_info *tasdevice_add_config(
187187
/* convert data[offset], data[offset + 1], data[offset + 2] and
188188
* data[offset + 3] into host
189189
*/
190-
cfg_info->nblocks =
191-
be32_to_cpup((__be32 *)&config_data[config_offset]);
190+
cfg_info->nblocks = get_unaligned_be32(&config_data[config_offset]);
192191
config_offset += 4;
193192

194193
/* Several kinds of dsp/algorithm firmwares can run on tas2781,
@@ -232,14 +231,14 @@ static struct tasdevice_config_info *tasdevice_add_config(
232231

233232
}
234233
bk_da[i]->yram_checksum =
235-
be16_to_cpup((__be16 *)&config_data[config_offset]);
234+
get_unaligned_be16(&config_data[config_offset]);
236235
config_offset += 2;
237236
bk_da[i]->block_size =
238-
be32_to_cpup((__be32 *)&config_data[config_offset]);
237+
get_unaligned_be32(&config_data[config_offset]);
239238
config_offset += 4;
240239

241240
bk_da[i]->n_subblks =
242-
be32_to_cpup((__be32 *)&config_data[config_offset]);
241+
get_unaligned_be32(&config_data[config_offset]);
243242

244243
config_offset += 4;
245244

@@ -289,7 +288,7 @@ int tasdevice_rca_parser(void *context, const struct firmware *fmw)
289288
}
290289
buf = (unsigned char *)fmw->data;
291290

292-
fw_hdr->img_sz = be32_to_cpup((__be32 *)&buf[offset]);
291+
fw_hdr->img_sz = get_unaligned_be32(&buf[offset]);
293292
offset += 4;
294293
if (fw_hdr->img_sz != fmw->size) {
295294
dev_err(tas_priv->dev,
@@ -300,9 +299,9 @@ int tasdevice_rca_parser(void *context, const struct firmware *fmw)
300299
goto out;
301300
}
302301

303-
fw_hdr->checksum = be32_to_cpup((__be32 *)&buf[offset]);
302+
fw_hdr->checksum = get_unaligned_be32(&buf[offset]);
304303
offset += 4;
305-
fw_hdr->binary_version_num = be32_to_cpup((__be32 *)&buf[offset]);
304+
fw_hdr->binary_version_num = get_unaligned_be32(&buf[offset]);
306305
if (fw_hdr->binary_version_num < 0x103) {
307306
dev_err(tas_priv->dev, "File version 0x%04x is too low",
308307
fw_hdr->binary_version_num);
@@ -311,7 +310,7 @@ int tasdevice_rca_parser(void *context, const struct firmware *fmw)
311310
goto out;
312311
}
313312
offset += 4;
314-
fw_hdr->drv_fw_version = be32_to_cpup((__be32 *)&buf[offset]);
313+
fw_hdr->drv_fw_version = get_unaligned_be32(&buf[offset]);
315314
offset += 8;
316315
fw_hdr->plat_type = buf[offset];
317316
offset += 1;
@@ -339,11 +338,11 @@ int tasdevice_rca_parser(void *context, const struct firmware *fmw)
339338
for (i = 0; i < TASDEVICE_DEVICE_SUM; i++, offset++)
340339
fw_hdr->devs[i] = buf[offset];
341340

342-
fw_hdr->nconfig = be32_to_cpup((__be32 *)&buf[offset]);
341+
fw_hdr->nconfig = get_unaligned_be32(&buf[offset]);
343342
offset += 4;
344343

345344
for (i = 0; i < TASDEVICE_CONFIG_SUM; i++) {
346-
fw_hdr->config_size[i] = be32_to_cpup((__be32 *)&buf[offset]);
345+
fw_hdr->config_size[i] = get_unaligned_be32(&buf[offset]);
347346
offset += 4;
348347
total_config_sz += fw_hdr->config_size[i];
349348
}
@@ -423,7 +422,7 @@ static int fw_parse_block_data_kernel(struct tasdevice_fw *tas_fmw,
423422
/* convert data[offset], data[offset + 1], data[offset + 2] and
424423
* data[offset + 3] into host
425424
*/
426-
block->type = be32_to_cpup((__be32 *)&data[offset]);
425+
block->type = get_unaligned_be32(&data[offset]);
427426
offset += 4;
428427

429428
block->is_pchksum_present = data[offset];
@@ -438,10 +437,10 @@ static int fw_parse_block_data_kernel(struct tasdevice_fw *tas_fmw,
438437
block->ychksum = data[offset];
439438
offset++;
440439

441-
block->blk_size = be32_to_cpup((__be32 *)&data[offset]);
440+
block->blk_size = get_unaligned_be32(&data[offset]);
442441
offset += 4;
443442

444-
block->nr_subblocks = be32_to_cpup((__be32 *)&data[offset]);
443+
block->nr_subblocks = get_unaligned_be32(&data[offset]);
445444
offset += 4;
446445

447446
/* fixed m68k compiling issue:
@@ -482,7 +481,7 @@ static int fw_parse_data_kernel(struct tasdevice_fw *tas_fmw,
482481
offset = -EINVAL;
483482
goto out;
484483
}
485-
img_data->nr_blk = be32_to_cpup((__be32 *)&data[offset]);
484+
img_data->nr_blk = get_unaligned_be32(&data[offset]);
486485
offset += 4;
487486

488487
img_data->dev_blks = kcalloc(img_data->nr_blk,
@@ -578,14 +577,14 @@ static int fw_parse_variable_header_kernel(
578577
offset = -EINVAL;
579578
goto out;
580579
}
581-
fw_hdr->device_family = be16_to_cpup((__be16 *)&buf[offset]);
580+
fw_hdr->device_family = get_unaligned_be16(&buf[offset]);
582581
if (fw_hdr->device_family != 0) {
583582
dev_err(tas_priv->dev, "%s:not TAS device\n", __func__);
584583
offset = -EINVAL;
585584
goto out;
586585
}
587586
offset += 2;
588-
fw_hdr->device = be16_to_cpup((__be16 *)&buf[offset]);
587+
fw_hdr->device = get_unaligned_be16(&buf[offset]);
589588
if (fw_hdr->device >= TASDEVICE_DSP_TAS_MAX_DEVICE ||
590589
fw_hdr->device == 6) {
591590
dev_err(tas_priv->dev, "Unsupported dev %d\n", fw_hdr->device);
@@ -603,7 +602,7 @@ static int fw_parse_variable_header_kernel(
603602
goto out;
604603
}
605604

606-
tas_fmw->nr_programs = be32_to_cpup((__be32 *)&buf[offset]);
605+
tas_fmw->nr_programs = get_unaligned_be32(&buf[offset]);
607606
offset += 4;
608607

609608
if (tas_fmw->nr_programs == 0 || tas_fmw->nr_programs >
@@ -622,14 +621,14 @@ static int fw_parse_variable_header_kernel(
622621

623622
for (i = 0; i < tas_fmw->nr_programs; i++) {
624623
program = &(tas_fmw->programs[i]);
625-
program->prog_size = be32_to_cpup((__be32 *)&buf[offset]);
624+
program->prog_size = get_unaligned_be32(&buf[offset]);
626625
offset += 4;
627626
}
628627

629628
/* Skip the unused prog_size */
630629
offset += 4 * (TASDEVICE_MAXPROGRAM_NUM_KERNEL - tas_fmw->nr_programs);
631630

632-
tas_fmw->nr_configurations = be32_to_cpup((__be32 *)&buf[offset]);
631+
tas_fmw->nr_configurations = get_unaligned_be32(&buf[offset]);
633632
offset += 4;
634633

635634
/* The max number of config in firmware greater than 4 pieces of
@@ -661,7 +660,7 @@ static int fw_parse_variable_header_kernel(
661660

662661
for (i = 0; i < tas_fmw->nr_programs; i++) {
663662
config = &(tas_fmw->configs[i]);
664-
config->cfg_size = be32_to_cpup((__be32 *)&buf[offset]);
663+
config->cfg_size = get_unaligned_be32(&buf[offset]);
665664
offset += 4;
666665
}
667666

@@ -699,7 +698,7 @@ static int tasdevice_process_block(void *context, unsigned char *data,
699698
switch (subblk_typ) {
700699
case TASDEVICE_CMD_SING_W: {
701700
int i;
702-
unsigned short len = be16_to_cpup((__be16 *)&data[2]);
701+
unsigned short len = get_unaligned_be16(&data[2]);
703702

704703
subblk_offset += 2;
705704
if (subblk_offset + 4 * len > sublocksize) {
@@ -725,7 +724,7 @@ static int tasdevice_process_block(void *context, unsigned char *data,
725724
}
726725
break;
727726
case TASDEVICE_CMD_BURST: {
728-
unsigned short len = be16_to_cpup((__be16 *)&data[2]);
727+
unsigned short len = get_unaligned_be16(&data[2]);
729728

730729
subblk_offset += 2;
731730
if (subblk_offset + 4 + len > sublocksize) {
@@ -766,7 +765,7 @@ static int tasdevice_process_block(void *context, unsigned char *data,
766765
is_err = true;
767766
break;
768767
}
769-
sleep_time = be16_to_cpup((__be16 *)&data[2]) * 1000;
768+
sleep_time = get_unaligned_be16(&data[2]) * 1000;
770769
usleep_range(sleep_time, sleep_time + 50);
771770
subblk_offset += 2;
772771
}
@@ -910,15 +909,15 @@ static int fw_parse_variable_hdr(struct tasdevice_priv
910909

911910
offset += len;
912911

913-
fw_hdr->device_family = be32_to_cpup((__be32 *)&buf[offset]);
912+
fw_hdr->device_family = get_unaligned_be32(&buf[offset]);
914913
if (fw_hdr->device_family != 0) {
915914
dev_err(tas_priv->dev, "%s: not TAS device\n", __func__);
916915
offset = -EINVAL;
917916
goto out;
918917
}
919918
offset += 4;
920919

921-
fw_hdr->device = be32_to_cpup((__be32 *)&buf[offset]);
920+
fw_hdr->device = get_unaligned_be32(&buf[offset]);
922921
if (fw_hdr->device >= TASDEVICE_DSP_TAS_MAX_DEVICE ||
923922
fw_hdr->device == 6) {
924923
dev_err(tas_priv->dev, "Unsupported dev %d\n", fw_hdr->device);
@@ -963,7 +962,7 @@ static int fw_parse_block_data(struct tasdevice_fw *tas_fmw,
963962
offset = -EINVAL;
964963
goto out;
965964
}
966-
block->type = be32_to_cpup((__be32 *)&data[offset]);
965+
block->type = get_unaligned_be32(&data[offset]);
967966
offset += 4;
968967

969968
if (tas_fmw->fw_hdr.fixed_hdr.drv_ver >= PPC_DRIVER_CRCCHK) {
@@ -988,7 +987,7 @@ static int fw_parse_block_data(struct tasdevice_fw *tas_fmw,
988987
block->is_ychksum_present = 0;
989988
}
990989

991-
block->nr_cmds = be32_to_cpup((__be32 *)&data[offset]);
990+
block->nr_cmds = get_unaligned_be32(&data[offset]);
992991
offset += 4;
993992

994993
n = block->nr_cmds * 4;
@@ -1039,7 +1038,7 @@ static int fw_parse_data(struct tasdevice_fw *tas_fmw,
10391038
goto out;
10401039
}
10411040
offset += n;
1042-
img_data->nr_blk = be16_to_cpup((__be16 *)&data[offset]);
1041+
img_data->nr_blk = get_unaligned_be16(&data[offset]);
10431042
offset += 2;
10441043

10451044
img_data->dev_blks = kcalloc(img_data->nr_blk,
@@ -1076,7 +1075,7 @@ static int fw_parse_program_data(struct tasdevice_priv *tas_priv,
10761075
offset = -EINVAL;
10771076
goto out;
10781077
}
1079-
tas_fmw->nr_programs = be16_to_cpup((__be16 *)&buf[offset]);
1078+
tas_fmw->nr_programs = get_unaligned_be16(&buf[offset]);
10801079
offset += 2;
10811080

10821081
if (tas_fmw->nr_programs == 0) {
@@ -1143,7 +1142,7 @@ static int fw_parse_configuration_data(
11431142
offset = -EINVAL;
11441143
goto out;
11451144
}
1146-
tas_fmw->nr_configurations = be16_to_cpup((__be16 *)&data[offset]);
1145+
tas_fmw->nr_configurations = get_unaligned_be16(&data[offset]);
11471146
offset += 2;
11481147

11491148
if (tas_fmw->nr_configurations == 0) {
@@ -1775,7 +1774,7 @@ static int fw_parse_header(struct tasdevice_priv *tas_priv,
17751774
/* Convert data[offset], data[offset + 1], data[offset + 2] and
17761775
* data[offset + 3] into host
17771776
*/
1778-
fw_fixed_hdr->fwsize = be32_to_cpup((__be32 *)&buf[offset]);
1777+
fw_fixed_hdr->fwsize = get_unaligned_be32(&buf[offset]);
17791778
offset += 4;
17801779
if (fw_fixed_hdr->fwsize != fmw->size) {
17811780
dev_err(tas_priv->dev, "File size not match, %lu %u",
@@ -1784,9 +1783,9 @@ static int fw_parse_header(struct tasdevice_priv *tas_priv,
17841783
goto out;
17851784
}
17861785
offset += 4;
1787-
fw_fixed_hdr->ppcver = be32_to_cpup((__be32 *)&buf[offset]);
1786+
fw_fixed_hdr->ppcver = get_unaligned_be32(&buf[offset]);
17881787
offset += 8;
1789-
fw_fixed_hdr->drv_ver = be32_to_cpup((__be32 *)&buf[offset]);
1788+
fw_fixed_hdr->drv_ver = get_unaligned_be32(&buf[offset]);
17901789
offset += 72;
17911790

17921791
out:
@@ -1828,7 +1827,7 @@ static int fw_parse_calibration_data(struct tasdevice_priv *tas_priv,
18281827
offset = -EINVAL;
18291828
goto out;
18301829
}
1831-
tas_fmw->nr_calibrations = be16_to_cpup((__be16 *)&data[offset]);
1830+
tas_fmw->nr_calibrations = get_unaligned_be16(&data[offset]);
18321831
offset += 2;
18331832

18341833
if (tas_fmw->nr_calibrations != 1) {

0 commit comments

Comments
 (0)