Skip to contentSkip to conversation

ASoC: DaVinci: Fix divide by zero error during 1st execution

PATCH Applied Troy Kisky 2 participants 6 messages Sep 11, 2009 – Sep 24, 2009

Subsystems (2)
lore
Share & tools
lore

Troy Kisky

author
#0 lore raw

PATCH 1/2 ASoC: DaVinci: Fix divide by zero error during 1st execution

When both playback and capture stream were open davinci_i2s_hw_params was setting parameters for the wrong stream. The fix for davinci_i2s_hw_params is sufficient, but it looks like a race still happens in davici_pcm_open. This patch also makes the race smaller but the next patch provides a better fix.

---
 sound/soc/davinci/davinci-i2s.c |    3 ++-
 sound/soc/davinci/davinci-pcm.c |   12 +++++-------
 2 files changed, 7 insertions(+), 8 deletions(-)
sound/soc/davinci/davinci-i2s.c +2 -1
index 12a6c54..d32e197 100644
@@ -353,8 +353,9 @@ static int davinci_i2s_hw_params(struct snd_pcm_substream *substream,
 				 struct snd_pcm_hw_params *params,
 				 struct snd_soc_dai *dai)
 {
-	struct davinci_pcm_dma_params *dma_params = dai->dma_data;
 	struct davinci_mcbsp_dev *dev = dai->private_data;
+	struct davinci_pcm_dma_params *dma_params =
+					dev->dma_params[substream->stream];
 	struct snd_interval *i = NULL;
 	int mcbsp_word_length;
 	unsigned int rcr, xcr, srgr;
sound/soc/davinci/davinci-pcm.c +5 -7
index 091dacb..002808b 100644
@@ -126,16 +126,9 @@ static void davinci_pcm_dma_irq(unsigned lch, u16 ch_status, void *data)
 static int davinci_pcm_dma_request(struct snd_pcm_substream *substream)
 {
 	struct davinci_runtime_data *prtd = substream->runtime->private_data;
-	struct snd_soc_pcm_runtime *rtd = substream->private_data;
-	struct davinci_pcm_dma_params *dma_data = rtd->dai->cpu_dai->dma_data;
 	struct edmacc_param p_ram;
 	int ret;
 
-	if (!dma_data)
-		return -ENODEV;
-
-	prtd->params = dma_data;
-
 	/* Request master DMA channel */
 	ret = edma_alloc_channel(prtd->params->channel,
 				  davinci_pcm_dma_irq, substream,
@@ -244,6 +237,10 @@ static int davinci_pcm_open(struct snd_pcm_substream *substream)
 	struct snd_pcm_runtime *runtime = substream->runtime;
 	struct davinci_runtime_data *prtd;
 	int ret = 0;
+	struct snd_soc_pcm_runtime *rtd = substream->private_data;
+	struct davinci_pcm_dma_params *params = rtd->dai->cpu_dai->dma_data;
+	if (!params)
+		return -ENODEV;
 
 	snd_soc_set_runtime_hwparams(substream, &davinci_pcm_hardware);
 	/* ensure that buffer size is a multiple of period size */
@@ -257,6 +254,7 @@ static int davinci_pcm_open(struct snd_pcm_substream *substream)
 		return -ENOMEM;
 
 	spin_lock_init(&prtd->lock);
+	prtd->params = params;
 
 	runtime->private_data = prtd;
 
Signature · 1 line
1.5.6.3
author
#1 lore raw

PATCH 2/2 ASoC: Davinci: Fix race with cpu_dai->dma_data

This patch removes references to cpu_dai->dma_data. It makes struct davinci_pcm_dma_params part of struct davinci_mcbsp_dev or struct davinci_audio_dev.

It removes the unused name variable from davinci_pcm_dma_params.

---
 sound/soc/davinci/davinci-i2s.c   |   36 +++++++++---------------------
 sound/soc/davinci/davinci-mcasp.c |   44 ++++++++----------------------------
 sound/soc/davinci/davinci-mcasp.h |    7 +++++-
 sound/soc/davinci/davinci-pcm.c   |    3 +-
 sound/soc/davinci/davinci-pcm.h   |    1 -
 5 files changed, 29 insertions(+), 62 deletions(-)
sound/soc/davinci/davinci-i2s.c +11 -25
index d32e197..4ae7070 100644
@@ -97,22 +97,19 @@ enum {
 	DAVINCI_MCBSP_WORD_32,
 };
 
-static struct davinci_pcm_dma_params davinci_i2s_pcm_out = {
-	.name = "I2S PCM Stereo out",
-};
-
-static struct davinci_pcm_dma_params davinci_i2s_pcm_in = {
-	.name = "I2S PCM Stereo in",
-};
-
 struct davinci_mcbsp_dev {
+	/*
+	 * dma_params must be first because rtd->dai->cpu_dai->private_data
+	 * is cast to a pointer of an array of struct davinci_pcm_dma_params in
+	 * davinci_pcm_open.
+	 */
+	struct davinci_pcm_dma_params	dma_params[2];
 	void __iomem			*base;
 #define MOD_DSP_A	0
 #define MOD_DSP_B	1
 	int				mode;
 	u32				pcr;
 	struct clk			*clk;
-	struct davinci_pcm_dma_params	*dma_params[2];
 };
 
 static inline void davinci_mcbsp_write_reg(struct davinci_mcbsp_dev *dev,
@@ -215,14 +212,6 @@ static void davinci_mcbsp_stop(struct davinci_mcbsp_dev *dev, int playback)
 	toggle_clock(dev, playback);
 }
 
-static int davinci_i2s_startup(struct snd_pcm_substream *substream,
-			       struct snd_soc_dai *cpu_dai)
-{
-	struct davinci_mcbsp_dev *dev = cpu_dai->private_data;
-	cpu_dai->dma_data = dev->dma_params[substream->stream];
-	return 0;
-}
-
 #define DEFAULT_BITPERSAMPLE	16
 
 static int davinci_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
@@ -355,7 +344,7 @@ static int davinci_i2s_hw_params(struct snd_pcm_substream *substream,
 {
 	struct davinci_mcbsp_dev *dev = dai->private_data;
 	struct davinci_pcm_dma_params *dma_params =
-					dev->dma_params[substream->stream];
+					&dev->dma_params[substream->stream];
 	struct snd_interval *i = NULL;
 	int mcbsp_word_length;
 	unsigned int rcr, xcr, srgr;
@@ -473,7 +462,6 @@ static void davinci_i2s_shutdown(struct snd_pcm_substream *substream,
 #define DAVINCI_I2S_RATES	SNDRV_PCM_RATE_8000_96000
 
 static struct snd_soc_dai_ops davinci_i2s_dai_ops = {
-	.startup 	= davinci_i2s_startup,
 	.shutdown	= davinci_i2s_shutdown,
 	.prepare	= davinci_i2s_prepare,
 	.trigger	= davinci_i2s_trigger,
@@ -535,12 +523,10 @@ static int davinci_i2s_probe(struct platform_device *pdev)
 
 	dev->base = (void __iomem *)IO_ADDRESS(mem->start);
 
-	dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK] = &davinci_i2s_pcm_out;
-	dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK]->dma_addr =
+	dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].dma_addr =
 	    (dma_addr_t)(io_v2p(dev->base) + DAVINCI_MCBSP_DXR_REG);
 
-	dev->dma_params[SNDRV_PCM_STREAM_CAPTURE] = &davinci_i2s_pcm_in;
-	dev->dma_params[SNDRV_PCM_STREAM_CAPTURE]->dma_addr =
+	dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].dma_addr =
 	    (dma_addr_t)(io_v2p(dev->base) + DAVINCI_MCBSP_DRR_REG);
 
 	/* first TX, then RX */
@@ -550,7 +536,7 @@ static int davinci_i2s_probe(struct platform_device *pdev)
 		ret = -ENXIO;
 		goto err_free_mem;
 	}
-	dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK]->channel = res->start;
+	dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].channel = res->start;
 
 	res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
 	if (!res) {
@@ -558,7 +544,7 @@ static int davinci_i2s_probe(struct platform_device *pdev)
 		ret = -ENXIO;
 		goto err_free_mem;
 	}
-	dev->dma_params[SNDRV_PCM_STREAM_CAPTURE]->channel = res->start;
+	dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].channel = res->start;
 
 	davinci_i2s_dai.private_data = dev;
 	ret = snd_soc_register_dai(&davinci_i2s_dai);
sound/soc/davinci/davinci-mcasp.c +10 -34

112 lines not on this page · raw message

sound/soc/davinci/davinci-mcasp.h +6 -1
index 554354c..9d179cc 100644
@@ -39,10 +39,15 @@ enum {
 };
 
 struct davinci_audio_dev {
+	/*
+	 * dma_params must be first because rtd->dai->cpu_dai->private_data
+	 * is cast to a pointer of an array of struct davinci_pcm_dma_params in
+	 * davinci_pcm_open.
+	 */
+	struct davinci_pcm_dma_params dma_params[2];
 	void __iomem *base;
 	int sample_rate;
 	struct clk *clk;
-	struct davinci_pcm_dma_params *dma_params[2];
 	unsigned int codec_fmt;
 
 	/* McASP specific data */
sound/soc/davinci/davinci-pcm.c +2 -1
index 002808b..359e99e 100644
@@ -238,7 +238,8 @@ static int davinci_pcm_open(struct snd_pcm_substream *substream)
 	struct davinci_runtime_data *prtd;
 	int ret = 0;
 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
-	struct davinci_pcm_dma_params *params = rtd->dai->cpu_dai->dma_data;
+	struct davinci_pcm_dma_params *pa = rtd->dai->cpu_dai->private_data;
+	struct davinci_pcm_dma_params *params = &pa[substream->stream];
 	if (!params)
 		return -ENODEV;
 
sound/soc/davinci/davinci-pcm.h +0 -1
index 63d9625..8746606 100644
@@ -17,7 +17,6 @@
 
 
 struct davinci_pcm_dma_params {
-	char *name;			/* stream identifier */
 	int channel;			/* sync dma channel ID */
 	unsigned short acnt;
 	dma_addr_t dma_addr;		/* device physical address for DMA */
Signature · 1 line
1.5.6.3
author
#2 lore raw

Re: [PATCH 2/2] ASoC: Davinci: Fix race with cpu_dai->dma_data

Quoted 20 lines · Troy Kisky

This patch removes references to cpu_dai->dma_data. It makes struct davinci_pcm_dma_params part of struct davinci_mcbsp_dev or struct davinci_audio_dev.

It removes the unused name variable from davinci_pcm_dma_params.

---
 sound/soc/davinci/davinci-i2s.c   |   36 +++++++++---------------------
 sound/soc/davinci/davinci-mcasp.c |   44 ++++++++----------------------------
 sound/soc/davinci/davinci-mcasp.h |    7 +++++-
 sound/soc/davinci/davinci-pcm.c   |    3 +-
 sound/soc/davinci/davinci-pcm.h   |    1 -
 5 files changed, 29 insertions(+), 62 deletions(-)
sound/soc/davinci/davinci-pcm.c +0 -0
index 002808b..359e99e 100644

sound/soc/davinci/davinci-pcm.c static int davinci_pcm_open(struct snd_pcm_substream *substream) · line 245

Quoted 9 lines
sound/soc/davinci/davinci-pcm.c +2 -1
@@ -238,7 +238,8 @@ static int davinci_pcm_open(struct snd_pcm_substream *substream)
 	struct davinci_runtime_data *prtd;
 	int ret = 0;
 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
-	struct davinci_pcm_dma_params *params = rtd->dai->cpu_dai->dma_data;
+	struct davinci_pcm_dma_params *pa = rtd->dai->cpu_dai->private_data;
+	struct davinci_pcm_dma_params *params = &pa[substream->stream];
 	if (!params)
 		return -ENODEV;
 

params can no longer be null, so the above 2 line can be deleted as well

Applied
#3 lore raw
In reply to Troy Kisky
Quoted 8 lines · Troy Kisky

When both playback and capture stream were open davinci_i2s_hw_params was setting parameters for the wrong stream. The fix for davinci_i2s_hw_params is sufficient, but it looks like a race still happens in davici_pcm_open. This patch also makes the race smaller but the next patch provides a better fix.

Applied both, thanks. Sorry about the delay in applying these - not sure what I was waiting for there.

author
#4 lore raw
Quoted 8 lines · Mark Brown
Quoted 6 lines · Troy Kisky

When both playback and capture stream were open davinci_i2s_hw_params was setting parameters for the wrong stream. The fix for davinci_i2s_hw_params is sufficient, but it looks like a race still happens in davici_pcm_open. This patch also makes the race smaller but the next patch provides a better fix.

Quoted 9 lines
Quoted 1 line

Applied both, thanks. Sorry about the delay in applying these - not sure what I was waiting for there.
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

I think you were waiting for me to remove the need for the comment:

+ /*
+ * dma_params must be first because rtd->dai->cpu_dai->private_data
+ * is cast to a pointer of an array of struct davinci_pcm_dma_params in
+ * davinci_pcm_open.
+ */

Which is doable.

Do you still want this fixed?

Thanks
Troy

#5 lore raw
Quoted 9 lines · Troy Kisky

I think you were waiting for me to remove the need for the comment:

(quoted diff) +5 -0
+	/*
+	 * dma_params must be first because rtd->dai->cpu_dai->private_data
+	 * is cast to a pointer of an array of struct davinci_pcm_dma_params in
+	 * davinci_pcm_open.
+	 */
Quoted 3 lines

Which is doable.

Do you still want this fixed?

Yes, please - I merged the patch because the bug fix really ought to be in .32 but it'd still be good to get the code cleaned up even if that has to wait for .33.