Commit ff728899 authored by Trevor Wu's avatar Trevor Wu Committed by Mark Brown

ASoC: mediatek: mt8195: add dai id check before accessing array

Coverity shows "afe_priv->dai_priv[dai_id] evaluates to an address that
could be at negative offset of an array.". Add dai id check before
accessing the array element. This ensures that the offset of an array must
be a valid index.
Signed-off-by: default avatarTrevor Wu <trevor.wu@mediatek.com>
Link: https://lore.kernel.org/r/20230307040938.7484-2-trevor.wu@mediatek.comSigned-off-by: default avatarMark Brown <broonie@kernel.org>
parent 4727d4d7
...@@ -704,13 +704,18 @@ static int mtk_dai_adda_hw_params(struct snd_pcm_substream *substream, ...@@ -704,13 +704,18 @@ static int mtk_dai_adda_hw_params(struct snd_pcm_substream *substream,
{ {
struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai); struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
struct mt8195_afe_private *afe_priv = afe->platform_priv; struct mt8195_afe_private *afe_priv = afe->platform_priv;
struct mtk_dai_adda_priv *adda_priv = afe_priv->dai_priv[dai->id]; struct mtk_dai_adda_priv *adda_priv;
unsigned int rate = params_rate(params); unsigned int rate = params_rate(params);
int id = dai->id; int ret;
int ret = 0;
if (dai->id != MT8195_AFE_IO_DL_SRC &&
dai->id != MT8195_AFE_IO_UL_SRC1 &&
dai->id != MT8195_AFE_IO_UL_SRC2)
return -EINVAL;
adda_priv = afe_priv->dai_priv[dai->id];
dev_dbg(afe->dev, "%s(), id %d, stream %d, rate %d\n", dev_dbg(afe->dev, "%s(), id %d, stream %d, rate %d\n",
__func__, id, substream->stream, rate); __func__, dai->id, substream->stream, rate);
if (rate > ADDA_HIRES_THRES) if (rate > ADDA_HIRES_THRES)
adda_priv->hires_required = 1; adda_priv->hires_required = 1;
...@@ -718,9 +723,9 @@ static int mtk_dai_adda_hw_params(struct snd_pcm_substream *substream, ...@@ -718,9 +723,9 @@ static int mtk_dai_adda_hw_params(struct snd_pcm_substream *substream,
adda_priv->hires_required = 0; adda_priv->hires_required = 0;
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
ret = mtk_dai_da_configure(afe, rate, id); ret = mtk_dai_da_configure(afe, rate, dai->id);
else else
ret = mtk_dai_ad_configure(afe, rate, id); ret = mtk_dai_ad_configure(afe, rate, dai->id);
return ret; return ret;
} }
......
This diff is collapsed.
...@@ -122,17 +122,26 @@ static int mtk_dai_pcm_configure(struct snd_pcm_substream *substream, ...@@ -122,17 +122,26 @@ static int mtk_dai_pcm_configure(struct snd_pcm_substream *substream,
struct snd_pcm_runtime * const runtime = substream->runtime; struct snd_pcm_runtime * const runtime = substream->runtime;
struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai); struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
struct mt8195_afe_private *afe_priv = afe->platform_priv; struct mt8195_afe_private *afe_priv = afe->platform_priv;
struct mtk_dai_pcmif_priv *pcmif_priv = afe_priv->dai_priv[dai->id]; struct mtk_dai_pcmif_priv *pcmif_priv;
unsigned int slave_mode = pcmif_priv->slave_mode; unsigned int slave_mode;
unsigned int lrck_inv = pcmif_priv->lrck_inv; unsigned int lrck_inv;
unsigned int bck_inv = pcmif_priv->bck_inv; unsigned int bck_inv;
unsigned int fmt = pcmif_priv->format; unsigned int fmt;
unsigned int bit_width = dai->sample_bits; unsigned int bit_width = dai->sample_bits;
unsigned int val = 0; unsigned int val = 0;
unsigned int mask = 0; unsigned int mask = 0;
int fs = 0; int fs = 0;
int mode = 0; int mode = 0;
if (dai->id != MT8195_AFE_IO_PCM)
return -EINVAL;
pcmif_priv = afe_priv->dai_priv[dai->id];
slave_mode = pcmif_priv->slave_mode;
lrck_inv = pcmif_priv->lrck_inv;
bck_inv = pcmif_priv->bck_inv;
fmt = pcmif_priv->format;
/* sync freq mode */ /* sync freq mode */
fs = mt8195_afe_fs_timing(runtime->rate); fs = mt8195_afe_fs_timing(runtime->rate);
if (fs < 0) if (fs < 0)
...@@ -230,10 +239,15 @@ static int mtk_dai_pcm_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) ...@@ -230,10 +239,15 @@ static int mtk_dai_pcm_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
{ {
struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai); struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
struct mt8195_afe_private *afe_priv = afe->platform_priv; struct mt8195_afe_private *afe_priv = afe->platform_priv;
struct mtk_dai_pcmif_priv *pcmif_priv = afe_priv->dai_priv[dai->id]; struct mtk_dai_pcmif_priv *pcmif_priv;
dev_dbg(dai->dev, "%s fmt 0x%x\n", __func__, fmt); dev_dbg(dai->dev, "%s fmt 0x%x\n", __func__, fmt);
if (dai->id != MT8195_AFE_IO_PCM)
return -EINVAL;
pcmif_priv = afe_priv->dai_priv[dai->id];
switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
case SND_SOC_DAIFMT_I2S: case SND_SOC_DAIFMT_I2S:
pcmif_priv->format = MTK_DAI_PCM_FMT_I2S; pcmif_priv->format = MTK_DAI_PCM_FMT_I2S;
......
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