diff --git a/sound/soc/codecs/hdac_hda.c b/sound/soc/codecs/hdac_hda.c index 48eb95973b7a39..d0b09462cddfdd 100644 --- a/sound/soc/codecs/hdac_hda.c +++ b/sound/soc/codecs/hdac_hda.c @@ -215,18 +215,42 @@ static int hdac_hda_dai_hw_params(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) { struct snd_soc_component *component = dai->component; + unsigned int channels = params_channels(params); + unsigned int rate = params_rate(params); + struct hda_pcm_stream *hda_stream; struct hdac_hda_priv *hda_pvt; unsigned int format_val; + struct hda_pcm *pcm; unsigned int maxbps; unsigned int bits; + hda_pvt = snd_soc_component_get_drvdata(component); + pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai); + if (!pcm) + return -EINVAL; + + hda_stream = &pcm->stream[substream->stream]; + + /* + * The DAI driver advertises stub capabilities, so check the + * BE parameters against what the codec stream actually supports. + */ + if (!(snd_pcm_rate_to_rate_bit(rate) & hda_stream->rates) || + channels < hda_stream->channels_min || + channels > hda_stream->channels_max) { + dev_err(dai->dev, + "%s: unsupported by codec, rate=%u, ch=%u (rates=%#x, ch=%u-%u)\n", + __func__, rate, channels, hda_stream->rates, + hda_stream->channels_min, hda_stream->channels_max); + return -EINVAL; + } + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) maxbps = dai->driver->playback.sig_bits; else maxbps = dai->driver->capture.sig_bits; bits = snd_hdac_stream_format_bits(params_format(params), SNDRV_PCM_SUBFORMAT_STD, maxbps); - hda_pvt = snd_soc_component_get_drvdata(component); format_val = snd_hdac_stream_format(params_channels(params), bits, params_rate(params)); if (!format_val) { dev_err(dai->dev, @@ -309,8 +333,10 @@ static int hdac_hda_dai_open(struct snd_pcm_substream *substream, hda_stream = &pcm->stream[substream->stream]; ret = hda_stream->ops.open(hda_stream, hda_pvt->codec, substream); - if (ret < 0) + if (ret < 0) { dev_err(dai->dev, "%s: failed %d\n", __func__, ret); + snd_hda_codec_pcm_put(pcm); + } return ret; } diff --git a/sound/soc/sof/ipc4-pcm.c b/sound/soc/sof/ipc4-pcm.c index cbf948d8479148..4d7acb3e5e161d 100644 --- a/sound/soc/sof/ipc4-pcm.c +++ b/sound/soc/sof/ipc4-pcm.c @@ -792,6 +792,35 @@ static int sof_ipc4_pcm_dai_link_fixup_channels(struct snd_sof_dev *sdev, return 0; } +/* + * The BE hw_params fixup is not told the stream direction. Find it using + * the same test dpcm_be_dai_hw_params() uses to pick the BEs to update. + * Return -EINVAL if no direction, or both directions, match. + */ +static int sof_ipc4_pcm_dai_link_fixup_stream(struct snd_soc_pcm_runtime *rtd) +{ + struct snd_soc_dpcm *dpcm; + int stream = -EINVAL; + int dir; + + for_each_pcm_streams(dir) { + for_each_dpcm_fe(rtd, dir, dpcm) { + enum snd_soc_dpcm_update update = dpcm->fe->dpcm[dir].runtime_update; + + if (update == SND_SOC_DPCM_UPDATE_FE || + (update == SND_SOC_DPCM_UPDATE_BE && rtd->dpcm[dir].runtime_update)) { + if (stream >= 0) + return -EINVAL; + + stream = dir; + break; + } + } + } + + return stream; +} + static int sof_ipc4_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params) { @@ -804,7 +833,7 @@ static int sof_ipc4_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct sof_ipc4_copier *ipc4_copier; bool single_bitdepth = false; u32 valid_bits = 0; - int dir, ret; + int dir, stream, ret; if (!dai) { dev_err(component->dev, "%s: No DAI found with name %s\n", __func__, @@ -819,20 +848,36 @@ static int sof_ipc4_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, return -EINVAL; } + stream = sof_ipc4_pcm_dai_link_fixup_stream(rtd); + for_each_pcm_streams(dir) { struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, dir); + if (stream >= 0 && dir != stream) + continue; + if (w) { - struct sof_ipc4_available_audio_format *available_fmt = - &ipc4_copier->available_fmt; + struct sof_ipc4_available_audio_format *available_fmt; struct snd_sof_widget *swidget = w->dobj.private; struct snd_sof_widget *pipe_widget = swidget->spipe->pipe_widget; struct sof_ipc4_pipeline *pipeline = pipe_widget->private; + struct snd_sof_dai *stream_dai = swidget->private; /* Chain DMA does not use copiers, so no fixup needed */ if (pipeline->use_chain_dma) return 0; + /* + * The playback and capture DAI widgets of a link have + * their own copiers, but share the DAI link name used + * in the DAI lookup above. Use the copier of the stream + * being configured, if known. + */ + if (dir == stream && stream_dai->private) + ipc4_copier = stream_dai->private; + + available_fmt = &ipc4_copier->available_fmt; + if (dir == SNDRV_PCM_STREAM_PLAYBACK) { if (sof_ipc4_copier_is_single_bitdepth(sdev, available_fmt->output_pin_fmts,