Skip to content

Commit

Permalink
ASoC: codecs: rt5682-sdw: simplify set_stream
Browse files Browse the repository at this point in the history
Using a dynamic allocation to store a single pointer is not very
efficient/useful.

Worse, the memory is released in the SoundWire stream.c file, but
still accessed in the DAI shutdown, leading to kmemleak reports.

And last the API requires the previous stream information to be
cleared when the argument is NULL.

Simplify the code to address all 3 problems.

Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
  • Loading branch information
plbossart committed Mar 17, 2023
1 parent 98193b0 commit 7abb325
Showing 1 changed file with 7 additions and 27 deletions.
34 changes: 7 additions & 27 deletions sound/soc/codecs/rt5682-sdw.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,38 +88,18 @@ static const struct regmap_config rt5682_sdw_indirect_regmap = {
.reg_write = rt5682_sdw_write,
};

struct sdw_stream_data {
struct sdw_stream_runtime *sdw_stream;
};

static int rt5682_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
int direction)
{
struct sdw_stream_data *stream;

if (!sdw_stream)
return 0;

stream = kzalloc(sizeof(*stream), GFP_KERNEL);
if (!stream)
return -ENOMEM;

stream->sdw_stream = sdw_stream;

/* Use tx_mask or rx_mask to configure stream tag and set dma_data */
snd_soc_dai_dma_data_set(dai, direction, stream);
snd_soc_dai_dma_data_set(dai, direction, sdw_stream);

return 0;
}

static void rt5682_sdw_shutdown(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
struct sdw_stream_data *stream;

stream = snd_soc_dai_get_dma_data(dai, substream);
snd_soc_dai_set_dma_data(dai, substream, NULL);
kfree(stream);
}

static int rt5682_sdw_hw_params(struct snd_pcm_substream *substream,
Expand All @@ -130,14 +110,14 @@ static int rt5682_sdw_hw_params(struct snd_pcm_substream *substream,
struct rt5682_priv *rt5682 = snd_soc_component_get_drvdata(component);
struct sdw_stream_config stream_config = {0};
struct sdw_port_config port_config = {0};
struct sdw_stream_data *stream;
struct sdw_stream_runtime *sdw_stream;
int retval;
unsigned int val_p = 0, val_c = 0, osr_p = 0, osr_c = 0;

dev_dbg(dai->dev, "%s %s", __func__, dai->name);

stream = snd_soc_dai_get_dma_data(dai, substream);
if (!stream)
sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
if (!sdw_stream)
return -ENOMEM;

if (!rt5682->slave)
Expand All @@ -152,7 +132,7 @@ static int rt5682_sdw_hw_params(struct snd_pcm_substream *substream,
port_config.num = 2;

retval = sdw_stream_add_slave(rt5682->slave, &stream_config,
&port_config, 1, stream->sdw_stream);
&port_config, 1, sdw_stream);
if (retval) {
dev_err(dai->dev, "Unable to configure port\n");
return retval;
Expand Down Expand Up @@ -246,13 +226,13 @@ static int rt5682_sdw_hw_free(struct snd_pcm_substream *substream,
{
struct snd_soc_component *component = dai->component;
struct rt5682_priv *rt5682 = snd_soc_component_get_drvdata(component);
struct sdw_stream_data *stream =
struct sdw_stream_runtime *sdw_stream =
snd_soc_dai_get_dma_data(dai, substream);

if (!rt5682->slave)
return -EINVAL;

sdw_stream_remove_slave(rt5682->slave, stream->sdw_stream);
sdw_stream_remove_slave(rt5682->slave, sdw_stream);
return 0;
}

Expand Down

0 comments on commit 7abb325

Please sign in to comment.