Commit 1cc509ed authored by Shenghao Ding's avatar Shenghao Ding Committed by Mark Brown

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: default avatarShenghao Ding <shenghao-ding@ti.com>
Link: https://patch.msgid.link/20240707083011.98-1-shenghao-ding@ti.comSigned-off-by: default avatarMark Brown <broonie@kernel.org>
parent ccb367c5
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
#include <sound/soc.h> #include <sound/soc.h>
#include <sound/tlv.h> #include <sound/tlv.h>
#include <sound/tas2781.h> #include <sound/tas2781.h>
#include <asm/unaligned.h>
#define ERROR_PRAM_CRCCHK 0x0000000 #define ERROR_PRAM_CRCCHK 0x0000000
#define ERROR_YRAM_CRCCHK 0x0000001 #define ERROR_YRAM_CRCCHK 0x0000001
...@@ -187,8 +187,7 @@ static struct tasdevice_config_info *tasdevice_add_config( ...@@ -187,8 +187,7 @@ static struct tasdevice_config_info *tasdevice_add_config(
/* convert data[offset], data[offset + 1], data[offset + 2] and /* convert data[offset], data[offset + 1], data[offset + 2] and
* data[offset + 3] into host * data[offset + 3] into host
*/ */
cfg_info->nblocks = cfg_info->nblocks = get_unaligned_be32(&config_data[config_offset]);
be32_to_cpup((__be32 *)&config_data[config_offset]);
config_offset += 4; config_offset += 4;
/* Several kinds of dsp/algorithm firmwares can run on tas2781, /* Several kinds of dsp/algorithm firmwares can run on tas2781,
...@@ -232,14 +231,14 @@ static struct tasdevice_config_info *tasdevice_add_config( ...@@ -232,14 +231,14 @@ static struct tasdevice_config_info *tasdevice_add_config(
} }
bk_da[i]->yram_checksum = bk_da[i]->yram_checksum =
be16_to_cpup((__be16 *)&config_data[config_offset]); get_unaligned_be16(&config_data[config_offset]);
config_offset += 2; config_offset += 2;
bk_da[i]->block_size = bk_da[i]->block_size =
be32_to_cpup((__be32 *)&config_data[config_offset]); get_unaligned_be32(&config_data[config_offset]);
config_offset += 4; config_offset += 4;
bk_da[i]->n_subblks = bk_da[i]->n_subblks =
be32_to_cpup((__be32 *)&config_data[config_offset]); get_unaligned_be32(&config_data[config_offset]);
config_offset += 4; config_offset += 4;
...@@ -289,7 +288,7 @@ int tasdevice_rca_parser(void *context, const struct firmware *fmw) ...@@ -289,7 +288,7 @@ int tasdevice_rca_parser(void *context, const struct firmware *fmw)
} }
buf = (unsigned char *)fmw->data; buf = (unsigned char *)fmw->data;
fw_hdr->img_sz = be32_to_cpup((__be32 *)&buf[offset]); fw_hdr->img_sz = get_unaligned_be32(&buf[offset]);
offset += 4; offset += 4;
if (fw_hdr->img_sz != fmw->size) { if (fw_hdr->img_sz != fmw->size) {
dev_err(tas_priv->dev, dev_err(tas_priv->dev,
...@@ -300,9 +299,9 @@ int tasdevice_rca_parser(void *context, const struct firmware *fmw) ...@@ -300,9 +299,9 @@ int tasdevice_rca_parser(void *context, const struct firmware *fmw)
goto out; goto out;
} }
fw_hdr->checksum = be32_to_cpup((__be32 *)&buf[offset]); fw_hdr->checksum = get_unaligned_be32(&buf[offset]);
offset += 4; offset += 4;
fw_hdr->binary_version_num = be32_to_cpup((__be32 *)&buf[offset]); fw_hdr->binary_version_num = get_unaligned_be32(&buf[offset]);
if (fw_hdr->binary_version_num < 0x103) { if (fw_hdr->binary_version_num < 0x103) {
dev_err(tas_priv->dev, "File version 0x%04x is too low", dev_err(tas_priv->dev, "File version 0x%04x is too low",
fw_hdr->binary_version_num); fw_hdr->binary_version_num);
...@@ -311,7 +310,7 @@ int tasdevice_rca_parser(void *context, const struct firmware *fmw) ...@@ -311,7 +310,7 @@ int tasdevice_rca_parser(void *context, const struct firmware *fmw)
goto out; goto out;
} }
offset += 4; offset += 4;
fw_hdr->drv_fw_version = be32_to_cpup((__be32 *)&buf[offset]); fw_hdr->drv_fw_version = get_unaligned_be32(&buf[offset]);
offset += 8; offset += 8;
fw_hdr->plat_type = buf[offset]; fw_hdr->plat_type = buf[offset];
offset += 1; offset += 1;
...@@ -339,11 +338,11 @@ int tasdevice_rca_parser(void *context, const struct firmware *fmw) ...@@ -339,11 +338,11 @@ int tasdevice_rca_parser(void *context, const struct firmware *fmw)
for (i = 0; i < TASDEVICE_DEVICE_SUM; i++, offset++) for (i = 0; i < TASDEVICE_DEVICE_SUM; i++, offset++)
fw_hdr->devs[i] = buf[offset]; fw_hdr->devs[i] = buf[offset];
fw_hdr->nconfig = be32_to_cpup((__be32 *)&buf[offset]); fw_hdr->nconfig = get_unaligned_be32(&buf[offset]);
offset += 4; offset += 4;
for (i = 0; i < TASDEVICE_CONFIG_SUM; i++) { for (i = 0; i < TASDEVICE_CONFIG_SUM; i++) {
fw_hdr->config_size[i] = be32_to_cpup((__be32 *)&buf[offset]); fw_hdr->config_size[i] = get_unaligned_be32(&buf[offset]);
offset += 4; offset += 4;
total_config_sz += fw_hdr->config_size[i]; total_config_sz += fw_hdr->config_size[i];
} }
...@@ -423,7 +422,7 @@ static int fw_parse_block_data_kernel(struct tasdevice_fw *tas_fmw, ...@@ -423,7 +422,7 @@ static int fw_parse_block_data_kernel(struct tasdevice_fw *tas_fmw,
/* convert data[offset], data[offset + 1], data[offset + 2] and /* convert data[offset], data[offset + 1], data[offset + 2] and
* data[offset + 3] into host * data[offset + 3] into host
*/ */
block->type = be32_to_cpup((__be32 *)&data[offset]); block->type = get_unaligned_be32(&data[offset]);
offset += 4; offset += 4;
block->is_pchksum_present = data[offset]; block->is_pchksum_present = data[offset];
...@@ -438,10 +437,10 @@ static int fw_parse_block_data_kernel(struct tasdevice_fw *tas_fmw, ...@@ -438,10 +437,10 @@ static int fw_parse_block_data_kernel(struct tasdevice_fw *tas_fmw,
block->ychksum = data[offset]; block->ychksum = data[offset];
offset++; offset++;
block->blk_size = be32_to_cpup((__be32 *)&data[offset]); block->blk_size = get_unaligned_be32(&data[offset]);
offset += 4; offset += 4;
block->nr_subblocks = be32_to_cpup((__be32 *)&data[offset]); block->nr_subblocks = get_unaligned_be32(&data[offset]);
offset += 4; offset += 4;
/* fixed m68k compiling issue: /* fixed m68k compiling issue:
...@@ -482,7 +481,7 @@ static int fw_parse_data_kernel(struct tasdevice_fw *tas_fmw, ...@@ -482,7 +481,7 @@ static int fw_parse_data_kernel(struct tasdevice_fw *tas_fmw,
offset = -EINVAL; offset = -EINVAL;
goto out; goto out;
} }
img_data->nr_blk = be32_to_cpup((__be32 *)&data[offset]); img_data->nr_blk = get_unaligned_be32(&data[offset]);
offset += 4; offset += 4;
img_data->dev_blks = kcalloc(img_data->nr_blk, img_data->dev_blks = kcalloc(img_data->nr_blk,
...@@ -578,14 +577,14 @@ static int fw_parse_variable_header_kernel( ...@@ -578,14 +577,14 @@ static int fw_parse_variable_header_kernel(
offset = -EINVAL; offset = -EINVAL;
goto out; goto out;
} }
fw_hdr->device_family = be16_to_cpup((__be16 *)&buf[offset]); fw_hdr->device_family = get_unaligned_be16(&buf[offset]);
if (fw_hdr->device_family != 0) { if (fw_hdr->device_family != 0) {
dev_err(tas_priv->dev, "%s:not TAS device\n", __func__); dev_err(tas_priv->dev, "%s:not TAS device\n", __func__);
offset = -EINVAL; offset = -EINVAL;
goto out; goto out;
} }
offset += 2; offset += 2;
fw_hdr->device = be16_to_cpup((__be16 *)&buf[offset]); fw_hdr->device = get_unaligned_be16(&buf[offset]);
if (fw_hdr->device >= TASDEVICE_DSP_TAS_MAX_DEVICE || if (fw_hdr->device >= TASDEVICE_DSP_TAS_MAX_DEVICE ||
fw_hdr->device == 6) { fw_hdr->device == 6) {
dev_err(tas_priv->dev, "Unsupported dev %d\n", fw_hdr->device); dev_err(tas_priv->dev, "Unsupported dev %d\n", fw_hdr->device);
...@@ -603,7 +602,7 @@ static int fw_parse_variable_header_kernel( ...@@ -603,7 +602,7 @@ static int fw_parse_variable_header_kernel(
goto out; goto out;
} }
tas_fmw->nr_programs = be32_to_cpup((__be32 *)&buf[offset]); tas_fmw->nr_programs = get_unaligned_be32(&buf[offset]);
offset += 4; offset += 4;
if (tas_fmw->nr_programs == 0 || tas_fmw->nr_programs > if (tas_fmw->nr_programs == 0 || tas_fmw->nr_programs >
...@@ -622,14 +621,14 @@ static int fw_parse_variable_header_kernel( ...@@ -622,14 +621,14 @@ static int fw_parse_variable_header_kernel(
for (i = 0; i < tas_fmw->nr_programs; i++) { for (i = 0; i < tas_fmw->nr_programs; i++) {
program = &(tas_fmw->programs[i]); program = &(tas_fmw->programs[i]);
program->prog_size = be32_to_cpup((__be32 *)&buf[offset]); program->prog_size = get_unaligned_be32(&buf[offset]);
offset += 4; offset += 4;
} }
/* Skip the unused prog_size */ /* Skip the unused prog_size */
offset += 4 * (TASDEVICE_MAXPROGRAM_NUM_KERNEL - tas_fmw->nr_programs); offset += 4 * (TASDEVICE_MAXPROGRAM_NUM_KERNEL - tas_fmw->nr_programs);
tas_fmw->nr_configurations = be32_to_cpup((__be32 *)&buf[offset]); tas_fmw->nr_configurations = get_unaligned_be32(&buf[offset]);
offset += 4; offset += 4;
/* The max number of config in firmware greater than 4 pieces of /* The max number of config in firmware greater than 4 pieces of
...@@ -661,7 +660,7 @@ static int fw_parse_variable_header_kernel( ...@@ -661,7 +660,7 @@ static int fw_parse_variable_header_kernel(
for (i = 0; i < tas_fmw->nr_programs; i++) { for (i = 0; i < tas_fmw->nr_programs; i++) {
config = &(tas_fmw->configs[i]); config = &(tas_fmw->configs[i]);
config->cfg_size = be32_to_cpup((__be32 *)&buf[offset]); config->cfg_size = get_unaligned_be32(&buf[offset]);
offset += 4; offset += 4;
} }
...@@ -699,7 +698,7 @@ static int tasdevice_process_block(void *context, unsigned char *data, ...@@ -699,7 +698,7 @@ static int tasdevice_process_block(void *context, unsigned char *data,
switch (subblk_typ) { switch (subblk_typ) {
case TASDEVICE_CMD_SING_W: { case TASDEVICE_CMD_SING_W: {
int i; int i;
unsigned short len = be16_to_cpup((__be16 *)&data[2]); unsigned short len = get_unaligned_be16(&data[2]);
subblk_offset += 2; subblk_offset += 2;
if (subblk_offset + 4 * len > sublocksize) { if (subblk_offset + 4 * len > sublocksize) {
...@@ -725,7 +724,7 @@ static int tasdevice_process_block(void *context, unsigned char *data, ...@@ -725,7 +724,7 @@ static int tasdevice_process_block(void *context, unsigned char *data,
} }
break; break;
case TASDEVICE_CMD_BURST: { case TASDEVICE_CMD_BURST: {
unsigned short len = be16_to_cpup((__be16 *)&data[2]); unsigned short len = get_unaligned_be16(&data[2]);
subblk_offset += 2; subblk_offset += 2;
if (subblk_offset + 4 + len > sublocksize) { if (subblk_offset + 4 + len > sublocksize) {
...@@ -766,7 +765,7 @@ static int tasdevice_process_block(void *context, unsigned char *data, ...@@ -766,7 +765,7 @@ static int tasdevice_process_block(void *context, unsigned char *data,
is_err = true; is_err = true;
break; break;
} }
sleep_time = be16_to_cpup((__be16 *)&data[2]) * 1000; sleep_time = get_unaligned_be16(&data[2]) * 1000;
usleep_range(sleep_time, sleep_time + 50); usleep_range(sleep_time, sleep_time + 50);
subblk_offset += 2; subblk_offset += 2;
} }
...@@ -910,7 +909,7 @@ static int fw_parse_variable_hdr(struct tasdevice_priv ...@@ -910,7 +909,7 @@ static int fw_parse_variable_hdr(struct tasdevice_priv
offset += len; offset += len;
fw_hdr->device_family = be32_to_cpup((__be32 *)&buf[offset]); fw_hdr->device_family = get_unaligned_be32(&buf[offset]);
if (fw_hdr->device_family != 0) { if (fw_hdr->device_family != 0) {
dev_err(tas_priv->dev, "%s: not TAS device\n", __func__); dev_err(tas_priv->dev, "%s: not TAS device\n", __func__);
offset = -EINVAL; offset = -EINVAL;
...@@ -918,7 +917,7 @@ static int fw_parse_variable_hdr(struct tasdevice_priv ...@@ -918,7 +917,7 @@ static int fw_parse_variable_hdr(struct tasdevice_priv
} }
offset += 4; offset += 4;
fw_hdr->device = be32_to_cpup((__be32 *)&buf[offset]); fw_hdr->device = get_unaligned_be32(&buf[offset]);
if (fw_hdr->device >= TASDEVICE_DSP_TAS_MAX_DEVICE || if (fw_hdr->device >= TASDEVICE_DSP_TAS_MAX_DEVICE ||
fw_hdr->device == 6) { fw_hdr->device == 6) {
dev_err(tas_priv->dev, "Unsupported dev %d\n", fw_hdr->device); 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, ...@@ -963,7 +962,7 @@ static int fw_parse_block_data(struct tasdevice_fw *tas_fmw,
offset = -EINVAL; offset = -EINVAL;
goto out; goto out;
} }
block->type = be32_to_cpup((__be32 *)&data[offset]); block->type = get_unaligned_be32(&data[offset]);
offset += 4; offset += 4;
if (tas_fmw->fw_hdr.fixed_hdr.drv_ver >= PPC_DRIVER_CRCCHK) { 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, ...@@ -988,7 +987,7 @@ static int fw_parse_block_data(struct tasdevice_fw *tas_fmw,
block->is_ychksum_present = 0; block->is_ychksum_present = 0;
} }
block->nr_cmds = be32_to_cpup((__be32 *)&data[offset]); block->nr_cmds = get_unaligned_be32(&data[offset]);
offset += 4; offset += 4;
n = block->nr_cmds * 4; n = block->nr_cmds * 4;
...@@ -1039,7 +1038,7 @@ static int fw_parse_data(struct tasdevice_fw *tas_fmw, ...@@ -1039,7 +1038,7 @@ static int fw_parse_data(struct tasdevice_fw *tas_fmw,
goto out; goto out;
} }
offset += n; offset += n;
img_data->nr_blk = be16_to_cpup((__be16 *)&data[offset]); img_data->nr_blk = get_unaligned_be16(&data[offset]);
offset += 2; offset += 2;
img_data->dev_blks = kcalloc(img_data->nr_blk, img_data->dev_blks = kcalloc(img_data->nr_blk,
...@@ -1076,7 +1075,7 @@ static int fw_parse_program_data(struct tasdevice_priv *tas_priv, ...@@ -1076,7 +1075,7 @@ static int fw_parse_program_data(struct tasdevice_priv *tas_priv,
offset = -EINVAL; offset = -EINVAL;
goto out; goto out;
} }
tas_fmw->nr_programs = be16_to_cpup((__be16 *)&buf[offset]); tas_fmw->nr_programs = get_unaligned_be16(&buf[offset]);
offset += 2; offset += 2;
if (tas_fmw->nr_programs == 0) { if (tas_fmw->nr_programs == 0) {
...@@ -1143,7 +1142,7 @@ static int fw_parse_configuration_data( ...@@ -1143,7 +1142,7 @@ static int fw_parse_configuration_data(
offset = -EINVAL; offset = -EINVAL;
goto out; goto out;
} }
tas_fmw->nr_configurations = be16_to_cpup((__be16 *)&data[offset]); tas_fmw->nr_configurations = get_unaligned_be16(&data[offset]);
offset += 2; offset += 2;
if (tas_fmw->nr_configurations == 0) { if (tas_fmw->nr_configurations == 0) {
...@@ -1775,7 +1774,7 @@ static int fw_parse_header(struct tasdevice_priv *tas_priv, ...@@ -1775,7 +1774,7 @@ static int fw_parse_header(struct tasdevice_priv *tas_priv,
/* Convert data[offset], data[offset + 1], data[offset + 2] and /* Convert data[offset], data[offset + 1], data[offset + 2] and
* data[offset + 3] into host * data[offset + 3] into host
*/ */
fw_fixed_hdr->fwsize = be32_to_cpup((__be32 *)&buf[offset]); fw_fixed_hdr->fwsize = get_unaligned_be32(&buf[offset]);
offset += 4; offset += 4;
if (fw_fixed_hdr->fwsize != fmw->size) { if (fw_fixed_hdr->fwsize != fmw->size) {
dev_err(tas_priv->dev, "File size not match, %lu %u", 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, ...@@ -1784,9 +1783,9 @@ static int fw_parse_header(struct tasdevice_priv *tas_priv,
goto out; goto out;
} }
offset += 4; offset += 4;
fw_fixed_hdr->ppcver = be32_to_cpup((__be32 *)&buf[offset]); fw_fixed_hdr->ppcver = get_unaligned_be32(&buf[offset]);
offset += 8; offset += 8;
fw_fixed_hdr->drv_ver = be32_to_cpup((__be32 *)&buf[offset]); fw_fixed_hdr->drv_ver = get_unaligned_be32(&buf[offset]);
offset += 72; offset += 72;
out: out:
...@@ -1828,7 +1827,7 @@ static int fw_parse_calibration_data(struct tasdevice_priv *tas_priv, ...@@ -1828,7 +1827,7 @@ static int fw_parse_calibration_data(struct tasdevice_priv *tas_priv,
offset = -EINVAL; offset = -EINVAL;
goto out; goto out;
} }
tas_fmw->nr_calibrations = be16_to_cpup((__be16 *)&data[offset]); tas_fmw->nr_calibrations = get_unaligned_be16(&data[offset]);
offset += 2; offset += 2;
if (tas_fmw->nr_calibrations != 1) { if (tas_fmw->nr_calibrations != 1) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment