Skip to contentSkip to conversation

ca0106 playback 44100Hz support to SPDIF and playback format & rate constraints

PATCH In review Ben Stanley 5 participants 16 messages Aug 22, 2008 – Sep 10, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 ca0106 playback 44100Hz support to SPDIF and playback format & rate constraints

Dear list,

This patch provides the ability to play back digital audio via SPDIF at a sampling rate of 44100Hz. It also implements rate constraints to ensure that the hardware restrictions of the card are not violated. Note that if 44100Hz is used, then all used channels must use that frequency. For example, opening hw:0,0 at 48000 and then opening hw:0,1 at 44100 will fail due to constraints. However, you may successfully open both hw:0,0 and hw:0,1 at 44100 (presume no other channels open). Further details of the restriction may be found in the thread
http://thread.gmane.org/gmane.linux.alsa.devel/52149/focus=52345

This patch also implements playback format restrictions, such that only one playback format may be in use at any time.

I tested driver playback using the attached script (Test_ca0106_driver.sh). I get good audio output for all combinations of sampling rates and formats except for 192kHz S16 (as noted at the end of the script). Sometimes it works and sometimes it doesn't. The reason for the failure is not apparent at this time. It could have something to do with my receiver. I don't know.

The attached script also checks and validates that the rate and format constraints operate as required.

I periodically have trouble with this driver not producing sound output. This has been investigated and the cause is explained here
http://thread.gmane.org/gmane.linux.alsa.devel/55384/focus=55410
I intend to submit a new patch to address this in the future.

This patch has been in use for three months on my MythTV system (Ubuntu 8.04). I find it works reliably for me. I also use it with xine, aplay, mplayer, mame, and childsplay.

I have checked with checkpatch.pl . Patch is against v1.0.18rc1.

This is the patch against alsa-kmirror.

---
 pci/ca0106/ca0106.h      |    7 +-
 pci/ca0106/ca0106_main.c |  266 ++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 229 insertions(+), 44 deletions(-)
pci/ca0106/ca0106.h +6 -1
index 74175fc..376fa9c 100644
@@ -1,7 +1,7 @@
 /*
  *  Copyright (c) 2004 James Courtier-Dutton <[email protected]>
  *  Driver CA0106 chips. e.g. Sound Blaster Audigy LS and Live 24bit
- *  Version: 0.0.22
+ *  Version: 0.0.23
  *
  *  FEATURES currently supported:
  *    See ca0106_main.c for features.
@@ -49,6 +49,8 @@
  *   Implement support for Line-in capture on SB Live 24bit.
  *  0.0.22
  *    Add support for mute control on SB Live 24bit (cards w/ SPI DAC)
+ *  0.0.23
+ *    Add support for playback sampling rate and format constraints.
  *
  *
  *  This code was initally based on code from ALSA's emu10k1x.c which is:
@@ -644,6 +646,8 @@
 
 #include "ca_midi.h"
 
+#define DRVNAME "snd-ca0106"
+
 struct snd_ca0106;
 
 struct snd_ca0106_channel {
@@ -659,6 +663,7 @@ struct snd_ca0106_pcm {
 	struct snd_pcm_substream *substream;
         int channel_id;
 	unsigned short running;
+	unsigned short hw_reserved;
 };
 
 struct snd_ca0106_details {
pci/ca0106/ca0106_main.c +223 -43

344 lines not on this page · raw message

Signature · 1 line
1.5.4.3

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#1 lore raw

At Sat, 23 Aug 2008 00:38:41 +1000,

Quoted 31 lines · Ben Stanley

Dear list,

This patch provides the ability to play back digital audio via SPDIF at a sampling rate of 44100Hz. It also implements rate constraints to ensure that the hardware restrictions of the card are not violated. Note that if 44100Hz is used, then all used channels must use that frequency. For example, opening hw:0,0 at 48000 and then opening hw:0,1 at 44100 will fail due to constraints. However, you may successfully open both hw:0,0 and hw:0,1 at 44100 (presume no other channels open). Further details of the restriction may be found in the thread
http://thread.gmane.org/gmane.linux.alsa.devel/52149/focus=52345

This patch also implements playback format restrictions, such that only one playback format may be in use at any time.

I tested driver playback using the attached script (Test_ca0106_driver.sh). I get good audio output for all combinations of sampling rates and formats except for 192kHz S16 (as noted at the end of the script). Sometimes it works and sometimes it doesn't. The reason for the failure is not apparent at this time. It could have something to do with my receiver. I don't know.

The attached script also checks and validates that the rate and format constraints operate as required.

I periodically have trouble with this driver not producing sound output. This has been investigated and the cause is explained here
http://thread.gmane.org/gmane.linux.alsa.devel/55384/focus=55410
I intend to submit a new patch to address this in the future.

Quoted 7 lines

This patch has been in use for three months on my MythTV system (Ubuntu 8.04). I find it works reliably for me. I also use it with xine, aplay, mplayer, mame, and childsplay.

I have checked with checkpatch.pl . Patch is against v1.0.18rc1.

Thanks for the patch!

Through a quick review, I find it almost fine, but slight concerns below.

pci/ca0106/ca0106_main.c static struct snd_pcm_hardware snd_ca0106_capture_hw = { · line 332

Quoted 3 lines
pci/ca0106/ca0106_main.c +3 -0
+static int hw_rule_playback_rate(
+	struct snd_pcm_hw_params *params,
+	struct snd_pcm_hw_rule   *rule)

Put arguments right after the open parenthesis.
It's more common style.

pci/ca0106/ca0106_main.c static struct snd_pcm_hardware snd_ca0106_capture_hw = { · line 335

Quoted 3 lines
pci/ca0106/ca0106_main.c +3 -0
+{
+	struct snd_ca0106 *chip = rule->private;
+	int chi, any_44100 = 0, any_non_44100 = 0, mask = 0;

No need to initialize here (at least for mask).

Review

Quoted 1 line
(quoted diff) +1 -0
+	struct snd_ca0106_channel *chp = 0;

Ditto.

pci/ca0106/ca0106_main.c static struct snd_pcm_hardware snd_ca0106_capture_hw = { · line 343

Quoted 7 lines
pci/ca0106/ca0106_main.c +7 -0
+	struct snd_pcm_runtime *runtime;
+	if (snd_BUG_ON(!chip))
+		return -EINVAL;
+
+	if (chip->spdif_enable) {
+		for (chi = 0; chi < 4; ++chi) {
+			chp = &(chip->playback_channels[chi]);

I'm afraid it's racy. The chip->playback_channels[] can be changed at any time. Try to put a mutex to protect this check.

pci/ca0106/ca0106_main.c static struct snd_pcm_hardware snd_ca0106_capture_hw = { · line 356

Quoted 13 lines
pci/ca0106/ca0106_main.c +13 -0
+			if (!chp->use)
+				continue;
+			if (snd_BUG_ON(!chp->epcm))
+				return -EINVAL;
+			if (!chp->epcm->hw_reserved)
+				continue;
+			if (snd_BUG_ON(!chp->epcm->substream))
+				return -EINVAL;
+			if (snd_BUG_ON(!chp->epcm->substream->runtime))
+				return -EINVAL;
+			runtime = chp->epcm->substream->runtime;
+			snd_printd("snd_hw_rule_playback_rate: ch=%d, "
+				"rate=%d.\n", chi, runtime->rate);

Use snd_printdd() instead. CONFIG_SND_DEBUG=y on most cases, and this would be too annoying.

pci/ca0106/ca0106_main.c static struct snd_pcm_hardware snd_ca0106_capture_hw = { · line 367

Quoted 11 lines
pci/ca0106/ca0106_main.c +11 -0
+			any_44100 += runtime->rate == 44100;
+			any_non_44100 += runtime->rate != 44100;
+		}
+		if (snd_BUG_ON(any_44100 && any_non_44100))
+			return -EINVAL;
+		if (any_44100)
+			mask = 0x1;
+		else if (any_non_44100)
+			mask = 0xE;
+		else
+			mask = 0xF;

This mask value is a bit cryptic, but OK...

pci/ca0106/ca0106_main.c static struct snd_pcm_hardware snd_ca0106_capture_hw = { · line 374

Quoted 7 lines
pci/ca0106/ca0106_main.c +7 -0
+	} else {
+		/* 44100Hz is not supported for DAC (FIXME Why?) */
+		mask = 0xE;
+	}
+	snd_printd("snd_hw_rule_playback_rate: any_44100=%d, "
+		"any_non_44100=%d, mask=0x%X, spdif=%d\n",
+		any_44100, any_non_44100, mask, chip->spdif_enable);

Use snd_printdd().

pci/ca0106/ca0106_main.c static struct snd_pcm_hardware snd_ca0106_capture_hw = { · line 378

Quoted 4 lines
pci/ca0106/ca0106_main.c +4 -0
+	return snd_interval_list(
+		hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE),
+		ARRAY_SIZE(all_spdif_playback_rates),
+		all_spdif_playback_rates, mask);

Put arguments after the open parenthesis.

pci/ca0106/ca0106_main.c static struct snd_pcm_hardware snd_ca0106_capture_hw = { · line 383

Quoted 5 lines
pci/ca0106/ca0106_main.c +5 -0
+}
+
+static int hw_rule_playback_format(
+	struct snd_pcm_hw_params *params,
+	struct snd_pcm_hw_rule *rule)

Ditto.

pci/ca0106/ca0106_main.c static struct snd_pcm_hardware snd_ca0106_capture_hw = { · line 387

Quoted 4 lines
pci/ca0106/ca0106_main.c +4 -0
+{
+	struct snd_ca0106 *chip = rule->private;
+	int chi, any_S16 = 0, any_S32 = 0;
+	struct snd_ca0106_channel *chp = 0;

No need to initialize.

pci/ca0106/ca0106_main.c static struct snd_pcm_hardware snd_ca0106_capture_hw = { · line 397

Quoted 10 lines
pci/ca0106/ca0106_main.c +10 -0
+	struct snd_pcm_runtime *runtime;
+	struct snd_mask fmt, *f = hw_param_mask(
+					params, SNDRV_PCM_HW_PARAM_FORMAT);
+	int result;
+	if (snd_BUG_ON(!chip))
+		return -EINVAL;
+	snd_mask_none(&fmt);
+
+	for (chi = 0; chi < 4; ++chi) {
+		chp = &(chip->playback_channels[chi]);

This also needs a mutex as well.

pci/ca0106/ca0106_main.c static struct snd_pcm_hardware snd_ca0106_capture_hw = { · line 410

Quoted 13 lines
pci/ca0106/ca0106_main.c +13 -0
+		if (!chp->use)
+			continue;
+		if (snd_BUG_ON(!chp->epcm))
+			return -EINVAL;
+		if (!chp->epcm->hw_reserved)
+			continue;
+		if (snd_BUG_ON(!chp->epcm->substream))
+			return -EINVAL;
+		if (snd_BUG_ON(!chp->epcm->substream->runtime))
+			return -EINVAL;
+		runtime = chp->epcm->substream->runtime;
+		snd_printd("snd_hw_rule_playback_format: ch=%d, format=%d.\n",
+			chi, runtime->format);

Use snd_printdd().

Quoted 8 lines
pci/ca0106/ca0106_main.c +8 -0
+		any_S16 += runtime->format == SNDRV_PCM_FORMAT_S16_LE;
+		any_S32 += runtime->format == SNDRV_PCM_FORMAT_S32_LE;
+	}
+	if (snd_BUG_ON(any_S16 && any_S32))
+		return -EINVAL;
+	if (any_S16)
+		snd_mask_set(&fmt, SNDRV_PCM_FORMAT_S16_LE);
+	else if (any_S32)

pci/ca0106/ca0106_main.c static struct snd_pcm_hardware snd_ca0106_capture_hw = { · line 428

Quoted 10 lines
pci/ca0106/ca0106_main.c +10 -0
+		snd_mask_set(&fmt, SNDRV_PCM_FORMAT_S32_LE);
+	else {
+		/* No format yet chosen, so both formats are available. */
+		snd_mask_set(&fmt, SNDRV_PCM_FORMAT_S16_LE);
+		snd_mask_set(&fmt, SNDRV_PCM_FORMAT_S32_LE);
+	}
+	result = snd_mask_refine(f, &fmt);
+	snd_printd("snd_hw_rule_playback_format: any_S16=%d, any_S32=%d, "
+		"refined_fmt=0x%X, avail_fmt=0x%X, changed=%d\n",
+		any_S16, any_S32, f->bits[0], fmt.bits[0], result);

Use snd_printdd().

Quoted 44 lines
pci/ca0106/ca0106_main.c +22 -2
+	return result;
+}
+
 unsigned int snd_ca0106_ptr_read(struct snd_ca0106 * emu, 
 					  unsigned int reg, 
 					  unsigned int chn)
@@ -508,10 +619,24 @@ static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substr
         //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
         //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
 	channel->epcm = epcm;
-	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
+	err = snd_pcm_hw_constraint_integer(
+			runtime, SNDRV_PCM_HW_PARAM_PERIODS);
+	if (err < 0)
                 return err;
-	if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
+	err = snd_pcm_hw_constraint_step(
+			runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
+	if (err < 0)
                 return err;
+	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
+			hw_rule_playback_rate, (void *)chip,
+			SNDRV_PCM_HW_PARAM_RATE, -1);
+	if (err < 0)
+		return err;
+	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
+		       hw_rule_playback_format, (void *)chip,
+			SNDRV_PCM_HW_PARAM_FORMAT, -1);
+	if (err < 0)
+		return err;
 	snd_pcm_set_sync(substream);
 
 	if (chip->details->spi_dac && channel_id != PCM_FRONT_CHANNEL) {
@@ -646,6 +771,9 @@ static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream
 /* hw_free callback */
 static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream *substream)
 {
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	struct snd_ca0106_pcm *epcm = runtime->private_data;
+	epcm->hw_reserved = 0;
 	return snd_pcm_lib_free_pages(substream);
 }
 

pci/ca0106/ca0106_main.c static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream) · line 798

Quoted 5 lines
pci/ca0106/ca0106_main.c +1 -1
@@ -667,53 +795,103 @@ static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream)
 static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)
 {
 	struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
-	struct snd_pcm_runtime *runtime = substream->runtime;
+	struct snd_pcm_runtime *runtime = substream->runtime, *runtimei = 0;

No need to initialize.

pci/ca0106/ca0106_main.c static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream) · line 800

Quoted 3 lines
pci/ca0106/ca0106_main.c +1 -1
 	struct snd_ca0106_pcm *epcm = runtime->private_data;
-	int channel = epcm->channel_id;
+	struct snd_ca0106_channel *chp = 0;

Ditto.

Quoted 51 lines
pci/ca0106/ca0106_main.c +10 -30
+	int channel = epcm->channel_id, chi, any_44100 = 0, any_non_44100 = 0;
 	u32 *table_base = (u32 *)(emu->buffer.area+(8*16*channel));
 	u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
 	u32 hcfg_mask = HCFG_PLAYBACK_S32_LE;
 	u32 hcfg_set = 0x00000000;
 	u32 hcfg;
-	u32 reg40_mask = 0x30000 << (channel<<1);
+	u32 reg40_mask = 0xFF0000;
 	u32 reg40_set = 0;
 	u32 reg40;
-	/* FIXME: Depending on mixer selection of SPDIF out or not, select the spdif rate or the DAC rate. */
-	u32 reg71_mask = 0x03030000 ; /* Global. Set SPDIF rate. We only support 44100 to spdif, not to DAC. */
+	u32 reg71_mask;
+	u32 reg71_shift;
 	u32 reg71_set = 0;
 	u32 reg71;
 	int i;
 
-        //snd_printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, periods=%u, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size, runtime->periods, frames_to_bytes(runtime, 1));
-        //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
-	//snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
-	/* Rate can be set per channel. */
-	/* reg40 control host to fifo */
-	/* reg71 controls DAC rate. */
-	switch (runtime->rate) {
-	case 44100:
-		reg40_set = 0x10000 << (channel<<1);
-		reg71_set = 0x01010000; 
-		break;
-        case 48000:
-		reg40_set = 0;
-		reg71_set = 0; 
-		break;
-	case 96000:
-		reg40_set = 0x20000 << (channel<<1);
-		reg71_set = 0x02020000; 
-		break;
-	case 192000:
-		reg40_set = 0x30000 << (channel<<1);
-		reg71_set = 0x03030000; 
-		break;
-	default:
-		reg40_set = 0;
-		reg71_set = 0; 
-		break;
+	epcm->hw_reserved = 1;
+	/* FIXME CLEAN UP IF spdif_enable IS CHANGED WHILE CHANNELS ARE OPENED
+	 * OR PREVENT THIS FROM HAPPENING. */
+	if (emu->spdif_enable)
+		reg71_shift = 24; /* SPDIF Output Rate */
+	else

pci/ca0106/ca0106_main.c static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream) · line 831

Quoted 10 lines
pci/ca0106/ca0106_main.c +10 -0
+		reg71_shift = 16; /* I2S Output Rate */
+	reg71_mask = 0x3 << reg71_shift;
+
+	printk(KERN_DEBUG DRVNAME ": prepare_playback: "
+		"channel_number=%d, rate=%d, format=0x%x, channels=%d, "
+		"buffer_size=%ld,period_size=%ld, periods=%u, "
+		"frames_to_bytes=%d\n",
+		channel, runtime->rate, runtime->format, runtime->channels,
+		runtime->buffer_size, runtime->period_size, runtime->periods,
+		frames_to_bytes(runtime, 1));

Use snd_printdd() (if any).

pci/ca0106/ca0106_main.c static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream) · line 838

Quoted 7 lines
pci/ca0106/ca0106_main.c +7 -0
+	/*printk("dma_addr=%x, dma_area=%p, table_base=%p\n",
+		runtime->dma_addr,runtime->dma_area, table_base);*/
+	/*printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",
+		emu->buffer.addr,emu->buffer.area, emu->buffer.bytes);*/
+	/* We are forced to build the settings for all the channels. */
+	for (chi = 0; chi < 4; ++chi) {
+		chp = &(emu->playback_channels[chi]);

Use a mutex in this function, too.

Quoted 45 lines
pci/ca0106/ca0106_main.c +45 -0
+		if (!chp->use)
+			continue;
+		if (snd_BUG_ON(!chp->epcm))
+			return -EINVAL;
+		if (chi != channel && !chp->epcm->hw_reserved)
+			continue;
+		if (snd_BUG_ON(!chp->epcm->substream))
+			return -EINVAL;
+		if (snd_BUG_ON(!chp->epcm->substream->runtime))
+			return -EINVAL;
+		runtimei = chp->epcm->substream->runtime;
+		any_44100 += runtimei->rate == 44100;
+		any_non_44100 += runtimei->rate != 44100;
+		/* Rate can be set per channel. */
+		/* reg40 control host to fifo */
+		/* reg71 controls DAC rate. */
+		switch (runtimei->rate) {
+		case 44100:
+			/* We only support 44100 to spdif, not to DAC.
+			   (FIXME WHY?)*/
+			if (emu->spdif_enable) {
+				/* When using 44100, *all* channels
+				   must be set to that rate. */
+				reg40_set |= 0x550000;
+				reg71_set |= 0x1 << reg71_shift;
+				break;
+			} else {
+				printk(KERN_ERR DRVNAME
+					"prepare_playback: "
+					"44100Hz is invalid for DAC.\n");
+			}
+		case 48000:
+			/* reg40_set &= !(0x1 << (chi<<1)); */
+			/* reg71_set &= !(0x1 << reg71_shift); */
+			break;
+		case 96000:
+			reg40_set |= 0x20000 << (chi<<1);
+			reg71_set |= 0x2 << reg71_shift;
+			break;
+		case 192000:
+			reg40_set |= 0x30000 << (chi<<1);
+			reg71_set |= 0x3 << reg71_shift;
+			break;
+		default:
+			printk(KERN_ERR DRVNAME

pci/ca0106/ca0106_main.c static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream) · line 891

Quoted 10 lines
pci/ca0106/ca0106_main.c +7 -2
+				": prepare_playback: "
+				"Bad sampling frequency %d.\n",
+				runtimei->rate);
+		}
 	}
-	/* Format is a global setting */
-	/* FIXME: Only let the first channel accessed set this. */
+	printk(KERN_DEBUG DRVNAME ": prepare_playback: any_44100=%d, "
+		"any_non_44100=%d, spdif=%d.\n",
+		any_44100, any_non_44100, emu->spdif_enable);

Use snd_printdd().

Quoted 18 lines
pci/ca0106/ca0106_main.c +5 -2
+	/* Format is a global setting. */
+	/* Only the first channel accessed can set this
+	   (enforced by constraints). */
 	switch (runtime->format) {
 	case SNDRV_PCM_FORMAT_S16_LE:
 		hcfg_set = 0;
@@ -1363,8 +1541,8 @@ static int __devinit snd_ca0106_create(int dev, struct snd_card *card,
 	pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
 	pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
 #if 1
-	printk(KERN_INFO "snd-ca0106: Model %04x Rev %08x Serial %08x\n", chip->model,
-	       pci->revision, chip->serial);
+	printk(KERN_INFO DRVNAME ": Model %04x Rev %08x Serial %08x\n",
+		chip->model, pci->revision, chip->serial);
 #endif
 	strcpy(card->driver, "CA0106");
 	strcpy(card->shortname, "CA0106");

pci/ca0106/ca0106_main.c static int __devinit snd_ca0106_create(int dev, struct snd_card *card, · line 1563

Quoted 9 lines
pci/ca0106/ca0106_main.c +3 -1
@@ -1378,7 +1556,9 @@ static int __devinit snd_ca0106_create(int dev, struct snd_card *card,
 	}
 	chip->details = c;
 	if (subsystem[dev]) {
-		printk(KERN_INFO "snd-ca0106: Sound card name=%s, subsystem=0x%x. Forced to subsystem=0x%x\n",
+		printk(KERN_INFO DRVNAME
+			": Sound card name=%s, subsystem=0x%x. "
+			"Forced to subsystem=0x%x\n",
                         c->name, chip->serial, subsystem[dev]);
 	}

Could you fix these and repost?

Thanks!

Takashi

#2 lore raw

Takashi,

I have tried to address all of your concerns regarding formatting, initialisers and printk/snd_printd/snd_printdd. I have a new patch attached addressing these points.

However, you also raised three mutex issues, which I have not yet addressed. I would like some more comments from you before I proceed.

hw_rule_playback_rate:

Quoted 5 lines
(quoted diff) +1 -0
+		for (chi = 0; chi < 4; ++chi) {

Review

Quoted 1 line
(quoted diff) +1 -0
+			chp = &(chip->playback_channels[chi]);

I'm afraid it's racy. The chip->playback_channels[] can be changed at any time. Try to put a mutex to protect this check.

hw_rule_playback_format:

Quoted 4 lines
(quoted diff) +1 -0
+	for (chi = 0; chi < 4; ++chi) {

Review

Quoted 1 line
(quoted diff) +1 -0
+		chp = &(chip->playback_channels[chi]);

This also needs a mutex as well.

snd_ca0106_pcm_prepare_playback:

Quoted 4 lines
(quoted diff) +1 -0
+	for (chi = 0; chi < 4; ++chi) {

Review

Quoted 1 line
(quoted diff) +1 -0
+		chp = &(emu->playback_channels[chi]);

Use a mutex in this function, too.

It seems you want to guard against interference to the members of snd_ca0106_channel (and also the associated snd_ca0106_pcm). So far I have detected that the following routines make use of these data structures:
hw_rule_playback_rate (new)
hw_rule_playback_format (new) snd_ca0106_pcm_open_playback_channel snd_ca0106_pcm_close_playback snd_ca0106_pcm_prepare_playback
snd_ca0106_interrupt
snd_ca0106_pcm_hw_free_playback
I don't yet claim that this list is exhaustive.
I'm not sure yet how I should do the mutex. Here is what I'm thinking about:
1) introduce a new spinlock_t pcm_lock within snd_ca0106 to cover access to *all* the pcm data (snd_ca0106_channel *4 + snd_ca0106_pcm * 4)
2) introduce a new spinlock_t pcm_lock within snd_ca0106_channel to cover access to the snd_ca0106_channel *1 + snd_ca0106_pcm *1

Option 2 makes it difficult to make sure that hw_rule_playback_rate and hw_rule_playback_format will work correctly when a channel is opened or closed while the hw_rule_* call is in progress, so that suggests option
1) might be better.

Introducing the pcm_lock means that all the abovenamed functions will have to hold the lock while doing their work.

(I'm not familiar with mutexes vs spinlocks in the kernel - point me to a documentation url if you want me to switch.)

But, it seems to me that a race condition remains after all this. The client code can do the hw_rule check on a channel, but another program can prepare another channel using (using parameters which will change the hw_rule result) before the first client gets back to prepare and open its channel. In fact, I should change snd_ca0106_pcm_prepare_playback so that it repeats the hardware check (within a mutex) and returns -EINVAL if it fails. Unless there is another mutex around that whole system to prevent this mess from happening...

I presume that I should leave the existing emu_lock alone and leave it to protect the register twiddling code exclusively.

I'll work on the mutex stuff after receiving feedback on the above.

Thanks for being encouraging about my first non-trivial patch here :-)

Ben Stanley.

Quoted 409 lines · Takashi Iwai

At Sat, 23 Aug 2008 00:38:41 +1000,

Quoted 31 lines · Ben Stanley

Dear list,

This patch provides the ability to play back digital audio via SPDIF at a sampling rate of 44100Hz. It also implements rate constraints to ensure that the hardware restrictions of the card are not violated. Note that if 44100Hz is used, then all used channels must use that frequency. For example, opening hw:0,0 at 48000 and then opening hw:0,1 at 44100 will fail due to constraints. However, you may successfully open both hw:0,0 and hw:0,1 at 44100 (presume no other channels open). Further details of the restriction may be found in the thread
http://thread.gmane.org/gmane.linux.alsa.devel/52149/focus=52345

This patch also implements playback format restrictions, such that only one playback format may be in use at any time.

I tested driver playback using the attached script (Test_ca0106_driver.sh). I get good audio output for all combinations of sampling rates and formats except for 192kHz S16 (as noted at the end of the script). Sometimes it works and sometimes it doesn't. The reason for the failure is not apparent at this time. It could have something to do with my receiver. I don't know.

The attached script also checks and validates that the rate and format constraints operate as required.

I periodically have trouble with this driver not producing sound output. This has been investigated and the cause is explained here
http://thread.gmane.org/gmane.linux.alsa.devel/55384/focus=55410
I intend to submit a new patch to address this in the future.

Quoted 7 lines

This patch has been in use for three months on my MythTV system (Ubuntu 8.04). I find it works reliably for me. I also use it with xine, aplay, mplayer, mame, and childsplay.

I have checked with checkpatch.pl . Patch is against v1.0.18rc1.

Thanks for the patch!

Through a quick review, I find it almost fine, but slight concerns below.

Review

Quoted 3 lines
(quoted diff) +3 -0
+static int hw_rule_playback_rate(
+	struct snd_pcm_hw_params *params,
+	struct snd_pcm_hw_rule   *rule)

Put arguments right after the open parenthesis.
It's more common style.

Review

Quoted 3 lines
(quoted diff) +3 -0
+{
+	struct snd_ca0106 *chip = rule->private;
+	int chi, any_44100 = 0, any_non_44100 = 0, mask = 0;

No need to initialize here (at least for mask).

Review

Quoted 1 line
(quoted diff) +1 -0
+	struct snd_ca0106_channel *chp = 0;

Ditto.

Review

Quoted 7 lines
(quoted diff) +7 -0
+	struct snd_pcm_runtime *runtime;
+	if (snd_BUG_ON(!chip))
+		return -EINVAL;
+
+	if (chip->spdif_enable) {
+		for (chi = 0; chi < 4; ++chi) {
+			chp = &(chip->playback_channels[chi]);

I'm afraid it's racy. The chip->playback_channels[] can be changed at any time. Try to put a mutex to protect this check.

Review

Quoted 13 lines
(quoted diff) +13 -0
+			if (!chp->use)
+				continue;
+			if (snd_BUG_ON(!chp->epcm))
+				return -EINVAL;
+			if (!chp->epcm->hw_reserved)
+				continue;
+			if (snd_BUG_ON(!chp->epcm->substream))
+				return -EINVAL;
+			if (snd_BUG_ON(!chp->epcm->substream->runtime))
+				return -EINVAL;
+			runtime = chp->epcm->substream->runtime;
+			snd_printd("snd_hw_rule_playback_rate: ch=%d, "
+				"rate=%d.\n", chi, runtime->rate);

Use snd_printdd() instead. CONFIG_SND_DEBUG=y on most cases, and this would be too annoying.

Review

Quoted 11 lines
(quoted diff) +11 -0
+			any_44100 += runtime->rate == 44100;
+			any_non_44100 += runtime->rate != 44100;
+		}
+		if (snd_BUG_ON(any_44100 && any_non_44100))
+			return -EINVAL;
+		if (any_44100)
+			mask = 0x1;
+		else if (any_non_44100)
+			mask = 0xE;
+		else
+			mask = 0xF;

This mask value is a bit cryptic, but OK...

Review

Quoted 7 lines
(quoted diff) +7 -0
+	} else {
+		/* 44100Hz is not supported for DAC (FIXME Why?) */
+		mask = 0xE;
+	}
+	snd_printd("snd_hw_rule_playback_rate: any_44100=%d, "
+		"any_non_44100=%d, mask=0x%X, spdif=%d\n",
+		any_44100, any_non_44100, mask, chip->spdif_enable);

Use snd_printdd().

Review

Quoted 4 lines
(quoted diff) +4 -0
+	return snd_interval_list(
+		hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE),
+		ARRAY_SIZE(all_spdif_playback_rates),
+		all_spdif_playback_rates, mask);

Put arguments after the open parenthesis.

Review

Quoted 5 lines
(quoted diff) +5 -0
+}
+
+static int hw_rule_playback_format(
+	struct snd_pcm_hw_params *params,
+	struct snd_pcm_hw_rule *rule)

Ditto.

Review

Quoted 4 lines
(quoted diff) +4 -0
+{
+	struct snd_ca0106 *chip = rule->private;
+	int chi, any_S16 = 0, any_S32 = 0;
+	struct snd_ca0106_channel *chp = 0;

No need to initialize.

Review

Quoted 10 lines
(quoted diff) +10 -0
+	struct snd_pcm_runtime *runtime;
+	struct snd_mask fmt, *f = hw_param_mask(
+					params, SNDRV_PCM_HW_PARAM_FORMAT);
+	int result;
+	if (snd_BUG_ON(!chip))
+		return -EINVAL;
+	snd_mask_none(&fmt);
+
+	for (chi = 0; chi < 4; ++chi) {
+		chp = &(chip->playback_channels[chi]);

This also needs a mutex as well.

Review

Quoted 13 lines
(quoted diff) +13 -0
+		if (!chp->use)
+			continue;
+		if (snd_BUG_ON(!chp->epcm))
+			return -EINVAL;
+		if (!chp->epcm->hw_reserved)
+			continue;
+		if (snd_BUG_ON(!chp->epcm->substream))
+			return -EINVAL;
+		if (snd_BUG_ON(!chp->epcm->substream->runtime))
+			return -EINVAL;
+		runtime = chp->epcm->substream->runtime;
+		snd_printd("snd_hw_rule_playback_format: ch=%d, format=%d.\n",
+			chi, runtime->format);

Use snd_printdd().

Quoted 8 lines
(quoted diff) +8 -0
+		any_S16 += runtime->format == SNDRV_PCM_FORMAT_S16_LE;
+		any_S32 += runtime->format == SNDRV_PCM_FORMAT_S32_LE;
+	}
+	if (snd_BUG_ON(any_S16 && any_S32))
+		return -EINVAL;
+	if (any_S16)
+		snd_mask_set(&fmt, SNDRV_PCM_FORMAT_S16_LE);
+	else if (any_S32)

Review

Quoted 10 lines
(quoted diff) +10 -0
+		snd_mask_set(&fmt, SNDRV_PCM_FORMAT_S32_LE);
+	else {
+		/* No format yet chosen, so both formats are available. */
+		snd_mask_set(&fmt, SNDRV_PCM_FORMAT_S16_LE);
+		snd_mask_set(&fmt, SNDRV_PCM_FORMAT_S32_LE);
+	}
+	result = snd_mask_refine(f, &fmt);
+	snd_printd("snd_hw_rule_playback_format: any_S16=%d, any_S32=%d, "
+		"refined_fmt=0x%X, avail_fmt=0x%X, changed=%d\n",
+		any_S16, any_S32, f->bits[0], fmt.bits[0], result);

Use snd_printdd().

Quoted 44 lines
(quoted diff) +22 -2
+	return result;
+}
+
 unsigned int snd_ca0106_ptr_read(struct snd_ca0106 * emu, 
 					  unsigned int reg, 
 					  unsigned int chn)
@@ -508,10 +619,24 @@ static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substr
         //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
         //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
 	channel->epcm = epcm;
-	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
+	err = snd_pcm_hw_constraint_integer(
+			runtime, SNDRV_PCM_HW_PARAM_PERIODS);
+	if (err < 0)
                 return err;
-	if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
+	err = snd_pcm_hw_constraint_step(
+			runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
+	if (err < 0)
                 return err;
+	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
+			hw_rule_playback_rate, (void *)chip,
+			SNDRV_PCM_HW_PARAM_RATE, -1);
+	if (err < 0)
+		return err;
+	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
+		       hw_rule_playback_format, (void *)chip,
+			SNDRV_PCM_HW_PARAM_FORMAT, -1);
+	if (err < 0)
+		return err;
 	snd_pcm_set_sync(substream);
 
 	if (chip->details->spi_dac && channel_id != PCM_FRONT_CHANNEL) {
@@ -646,6 +771,9 @@ static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream
 /* hw_free callback */
 static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream *substream)
 {
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	struct snd_ca0106_pcm *epcm = runtime->private_data;
+	epcm->hw_reserved = 0;
 	return snd_pcm_lib_free_pages(substream);
 }
 

Review static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream) · line 798

Quoted 5 lines
(quoted diff) +1 -1
@@ -667,53 +795,103 @@ static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream)
 static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)
 {
 	struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
-	struct snd_pcm_runtime *runtime = substream->runtime;
+	struct snd_pcm_runtime *runtime = substream->runtime, *runtimei = 0;

No need to initialize.

Review static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream) · line 800

Quoted 3 lines
(quoted diff) +1 -1
 	struct snd_ca0106_pcm *epcm = runtime->private_data;
-	int channel = epcm->channel_id;
+	struct snd_ca0106_channel *chp = 0;

Ditto.

Quoted 51 lines
(quoted diff) +10 -30
+	int channel = epcm->channel_id, chi, any_44100 = 0, any_non_44100 = 0;
 	u32 *table_base = (u32 *)(emu->buffer.area+(8*16*channel));
 	u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
 	u32 hcfg_mask = HCFG_PLAYBACK_S32_LE;
 	u32 hcfg_set = 0x00000000;
 	u32 hcfg;
-	u32 reg40_mask = 0x30000 << (channel<<1);
+	u32 reg40_mask = 0xFF0000;
 	u32 reg40_set = 0;
 	u32 reg40;
-	/* FIXME: Depending on mixer selection of SPDIF out or not, select the spdif rate or the DAC rate. */
-	u32 reg71_mask = 0x03030000 ; /* Global. Set SPDIF rate. We only support 44100 to spdif, not to DAC. */
+	u32 reg71_mask;
+	u32 reg71_shift;
 	u32 reg71_set = 0;
 	u32 reg71;
 	int i;
 
-        //snd_printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, periods=%u, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size, runtime->periods, frames_to_bytes(runtime, 1));
-        //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
-	//snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
-	/* Rate can be set per channel. */
-	/* reg40 control host to fifo */
-	/* reg71 controls DAC rate. */
-	switch (runtime->rate) {
-	case 44100:
-		reg40_set = 0x10000 << (channel<<1);
-		reg71_set = 0x01010000; 
-		break;
-        case 48000:
-		reg40_set = 0;
-		reg71_set = 0; 
-		break;
-	case 96000:
-		reg40_set = 0x20000 << (channel<<1);
-		reg71_set = 0x02020000; 
-		break;
-	case 192000:
-		reg40_set = 0x30000 << (channel<<1);
-		reg71_set = 0x03030000; 
-		break;
-	default:
-		reg40_set = 0;
-		reg71_set = 0; 
-		break;
+	epcm->hw_reserved = 1;
+	/* FIXME CLEAN UP IF spdif_enable IS CHANGED WHILE CHANNELS ARE OPENED
+	 * OR PREVENT THIS FROM HAPPENING. */
+	if (emu->spdif_enable)
+		reg71_shift = 24; /* SPDIF Output Rate */
+	else

Review static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream) · line 831

Quoted 10 lines
(quoted diff) +10 -0
+		reg71_shift = 16; /* I2S Output Rate */
+	reg71_mask = 0x3 << reg71_shift;
+
+	printk(KERN_DEBUG DRVNAME ": prepare_playback: "
+		"channel_number=%d, rate=%d, format=0x%x, channels=%d, "
+		"buffer_size=%ld,period_size=%ld, periods=%u, "
+		"frames_to_bytes=%d\n",
+		channel, runtime->rate, runtime->format, runtime->channels,
+		runtime->buffer_size, runtime->period_size, runtime->periods,
+		frames_to_bytes(runtime, 1));

Use snd_printdd() (if any).

Review static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream) · line 838

Quoted 7 lines
(quoted diff) +7 -0
+	/*printk("dma_addr=%x, dma_area=%p, table_base=%p\n",
+		runtime->dma_addr,runtime->dma_area, table_base);*/
+	/*printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",
+		emu->buffer.addr,emu->buffer.area, emu->buffer.bytes);*/
+	/* We are forced to build the settings for all the channels. */
+	for (chi = 0; chi < 4; ++chi) {
+		chp = &(emu->playback_channels[chi]);

Use a mutex in this function, too.

Quoted 45 lines
(quoted diff) +45 -0
+		if (!chp->use)
+			continue;
+		if (snd_BUG_ON(!chp->epcm))
+			return -EINVAL;
+		if (chi != channel && !chp->epcm->hw_reserved)
+			continue;
+		if (snd_BUG_ON(!chp->epcm->substream))
+			return -EINVAL;
+		if (snd_BUG_ON(!chp->epcm->substream->runtime))
+			return -EINVAL;
+		runtimei = chp->epcm->substream->runtime;
+		any_44100 += runtimei->rate == 44100;
+		any_non_44100 += runtimei->rate != 44100;
+		/* Rate can be set per channel. */
+		/* reg40 control host to fifo */
+		/* reg71 controls DAC rate. */
+		switch (runtimei->rate) {
+		case 44100:
+			/* We only support 44100 to spdif, not to DAC.
+			   (FIXME WHY?)*/
+			if (emu->spdif_enable) {
+				/* When using 44100, *all* channels
+				   must be set to that rate. */
+				reg40_set |= 0x550000;
+				reg71_set |= 0x1 << reg71_shift;
+				break;
+			} else {
+				printk(KERN_ERR DRVNAME
+					"prepare_playback: "
+					"44100Hz is invalid for DAC.\n");
+			}
+		case 48000:
+			/* reg40_set &= !(0x1 << (chi<<1)); */
+			/* reg71_set &= !(0x1 << reg71_shift); */
+			break;
+		case 96000:
+			reg40_set |= 0x20000 << (chi<<1);
+			reg71_set |= 0x2 << reg71_shift;
+			break;
+		case 192000:
+			reg40_set |= 0x30000 << (chi<<1);
+			reg71_set |= 0x3 << reg71_shift;
+			break;
+		default:
+			printk(KERN_ERR DRVNAME

Review static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream) · line 891

Quoted 10 lines
(quoted diff) +7 -2
+				": prepare_playback: "
+				"Bad sampling frequency %d.\n",
+				runtimei->rate);
+		}
 	}
-	/* Format is a global setting */
-	/* FIXME: Only let the first channel accessed set this. */
+	printk(KERN_DEBUG DRVNAME ": prepare_playback: any_44100=%d, "
+		"any_non_44100=%d, spdif=%d.\n",
+		any_44100, any_non_44100, emu->spdif_enable);

Use snd_printdd().

Quoted 18 lines
(quoted diff) +5 -2
+	/* Format is a global setting. */
+	/* Only the first channel accessed can set this
+	   (enforced by constraints). */
 	switch (runtime->format) {
 	case SNDRV_PCM_FORMAT_S16_LE:
 		hcfg_set = 0;
@@ -1363,8 +1541,8 @@ static int __devinit snd_ca0106_create(int dev, struct snd_card *card,
 	pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
 	pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
 #if 1
-	printk(KERN_INFO "snd-ca0106: Model %04x Rev %08x Serial %08x\n", chip->model,
-	       pci->revision, chip->serial);
+	printk(KERN_INFO DRVNAME ": Model %04x Rev %08x Serial %08x\n",
+		chip->model, pci->revision, chip->serial);
 #endif
 	strcpy(card->driver, "CA0106");
 	strcpy(card->shortname, "CA0106");

Review static int __devinit snd_ca0106_create(int dev, struct snd_card *card, · line 1563

Quoted 9 lines
(quoted diff) +3 -1
@@ -1378,7 +1556,9 @@ static int __devinit snd_ca0106_create(int dev, struct snd_card *card,
 	}
 	chip->details = c;
 	if (subsystem[dev]) {
-		printk(KERN_INFO "snd-ca0106: Sound card name=%s, subsystem=0x%x. Forced to subsystem=0x%x\n",
+		printk(KERN_INFO DRVNAME
+			": Sound card name=%s, subsystem=0x%x. "
+			"Forced to subsystem=0x%x\n",
                         c->name, chip->serial, subsystem[dev]);
 	}

Could you fix these and repost?

Quoted 8 lines

Thanks!

Takashi
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
From 1437172bbb9e2e3bcc0bb13e226fe43dda3a4fe2 Mon Sep 17 00:00:00 2001

From: Ben Stanley <[email protected]>
Date: Sun, 24 Aug 2008 00:01:25 +1000
Subject: [PATCH] ca0106 playback 44100Hz support to SPDIF and playback format & rate constraints

Squashed commit of the following:

commit 8bfbd000b4b602b517b5ab1edc5610ef39dced1e

Author: Ben Stanley <[email protected]>
Date:   Sat Aug 23 23:19:57 2008 +1000

Correct style and log messages according to Takashi's review comments.

commit eb973ebecfac64b6f0e60df90eef8885ccc64aaf

Author: Ben Stanley <[email protected]>
Date:   Wed Aug 20 23:56:15 2008 +1000

Annotate bug in comments.

commit df725ab6af87e1c94d1d41e31e3865437b1999e0

Author: Ben Stanley <[email protected]>
Date:   Wed Aug 20 23:44:12 2008 +1000

Checkpatch.pl round 4.

commit c277bfa1773595b98c5447a7a085f21ccead110f

Author: Ben Stanley <[email protected]>
Date:   Wed Aug 20 23:39:39 2008 +1000

Checkpatch.pl changes round 3.

commit a3f9b1faae250aa0786ef68c3c53c3915e42972b

Author: Ben Stanley <[email protected]>
Date:   Wed Aug 20 23:18:36 2008 +1000

Second round of checkpatch.pl changes.

commit 3f45e4895fc8419a4a90eddf29af4f8547aa105d

Author: Ben Stanley <[email protected]>
Date:   Sun Aug 17 00:06:47 2008 +1000

Satisfy checkpatch.pl pass 1

commit 88424052a25359aa7ffa840e0515a7a93278f931

Author: Ben Stanley <[email protected]>
Date:   Sat Aug 16 22:32:25 2008 +1000

44100Hz playback support to SPDIF and playback format & rate constraints

commit e0795c3bc756f07986eaae7c4ab54a78d3fecc80

Author: Ben Stanley <[email protected]>
Date:   Sat Aug 16 22:25:38 2008 +1000

Remove use of snd_assert.

commit 097b03e9ff07432c45a9865bd02208b20b09beb3

Author: Ben Stanley <[email protected]>
Date:   Thu Jul 24 02:59:38 2008 +1000

* fix cosmetic bug

commit 75769f154ea53b4215de37d314351c4e45bd2543

Author: Ben Stanley <[email protected]>
Date:   Thu Jul 3 20:47:31 2008 +1000

44100Hz playback support to SPDIF and playback format & rate constraints

---
 pci/ca0106/ca0106.h      |    7 +-
 pci/ca0106/ca0106_main.c |  265 ++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 228 insertions(+), 44 deletions(-)
pci/ca0106/ca0106.h +6 -1
index 74175fc..376fa9c 100644
@@ -1,7 +1,7 @@
 /*
  *  Copyright (c) 2004 James Courtier-Dutton <[email protected]>
  *  Driver CA0106 chips. e.g. Sound Blaster Audigy LS and Live 24bit
- *  Version: 0.0.22
+ *  Version: 0.0.23
  *
  *  FEATURES currently supported:
  *    See ca0106_main.c for features.
@@ -49,6 +49,8 @@
  *   Implement support for Line-in capture on SB Live 24bit.
  *  0.0.22
  *    Add support for mute control on SB Live 24bit (cards w/ SPI DAC)
+ *  0.0.23
+ *    Add support for playback sampling rate and format constraints.
  *
  *
  *  This code was initally based on code from ALSA's emu10k1x.c which is:
@@ -644,6 +646,8 @@
 
 #include "ca_midi.h"
 
+#define DRVNAME "snd-ca0106"
+
 struct snd_ca0106;
 
 struct snd_ca0106_channel {
@@ -659,6 +663,7 @@ struct snd_ca0106_pcm {
 	struct snd_pcm_substream *substream;
         int channel_id;
 	unsigned short running;
+	unsigned short hw_reserved;
 };
 
 struct snd_ca0106_details {
pci/ca0106/ca0106_main.c +222 -43

343 lines not on this page · raw message

Signature · 1 line
1.5.4.3

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#3 lore raw

At Sun, 24 Aug 2008 00:37:04 +1000,

Quoted 6 lines · Ben Stanley

Takashi,

I have tried to address all of your concerns regarding formatting, initialisers and printk/snd_printd/snd_printdd. I have a new patch attached addressing these points.

Thanks!

Quoted 41 lines

However, you also raised three mutex issues, which I have not yet addressed. I would like some more comments from you before I proceed.

hw_rule_playback_rate:

Quoted 5 lines
(quoted diff) +1 -0
+		for (chi = 0; chi < 4; ++chi) {

Review

Quoted 1 line
(quoted diff) +1 -0
+			chp = &(chip->playback_channels[chi]);

I'm afraid it's racy. The chip->playback_channels[] can be changed at any time. Try to put a mutex to protect this check.

hw_rule_playback_format:

Quoted 4 lines
(quoted diff) +1 -0
+	for (chi = 0; chi < 4; ++chi) {

Review

Quoted 1 line
(quoted diff) +1 -0
+		chp = &(chip->playback_channels[chi]);

This also needs a mutex as well.

snd_ca0106_pcm_prepare_playback:

Quoted 4 lines
(quoted diff) +1 -0
+	for (chi = 0; chi < 4; ++chi) {

pci/ca0106/ca0106_main.c static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream) · line 837

Quoted 1 line
pci/ca0106/ca0106_main.c +1 -0
+		chp = &(emu->playback_channels[chi]);

Use a mutex in this function, too.

It seems you want to guard against interference to the members of snd_ca0106_channel (and also the associated snd_ca0106_pcm). So far I have detected that the following routines make use of these data structures:
hw_rule_playback_rate (new)
hw_rule_playback_format (new) snd_ca0106_pcm_open_playback_channel snd_ca0106_pcm_close_playback snd_ca0106_pcm_prepare_playback
snd_ca0106_interrupt
snd_ca0106_pcm_hw_free_playback
I don't yet claim that this list is exhaustive.
I'm not sure yet how I should do the mutex. Here is what I'm thinking about:
1) introduce a new spinlock_t pcm_lock within snd_ca0106 to cover access to *all* the pcm data (snd_ca0106_channel *4 + snd_ca0106_pcm * 4)
2) introduce a new spinlock_t pcm_lock within snd_ca0106_channel to cover access to the snd_ca0106_channel *1 + snd_ca0106_pcm *1

Quoted 4 lines

Option 2 makes it difficult to make sure that hw_rule_playback_rate and hw_rule_playback_format will work correctly when a channel is opened or closed while the hw_rule_* call is in progress, so that suggests option
1) might be better.

Right. The 1 is the right choice in this case.

Quoted 5 lines

Introducing the pcm_lock means that all the abovenamed functions will have to hold the lock while doing their work.

(I'm not familiar with mutexes vs spinlocks in the kernel - point me to a documentation url if you want me to switch.)

I forgot about that it's referred in the interrupt handler, too. So it has to be a spinlock, indeed.

Quoted 9 lines

But, it seems to me that a race condition remains after all this. The client code can do the hw_rule check on a channel, but another program can prepare another channel using (using parameters which will change the hw_rule result) before the first client gets back to prepare and open its channel. In fact, I should change snd_ca0106_pcm_prepare_playback so that it repeats the hardware check (within a mutex) and returns -EINVAL if it fails. Unless there is another mutex around that whole system to prevent this mess from happening...

It's a known issue that this constraint can't be race-free. The only problem we need to fix right now is not to Oopsen -- to protect the playback_channels[].

thanks,

Takashi

#4 lore raw

Takashi,

Further to your previous comments [1], I have attempted to address the locking issues. I have re-designed the code to simplify the locking problems.

This is the patch against alsa-kmirror.

[1] http://thread.gmane.org/gmane.linux.alsa.devel/55527/focus=55539

---
 pci/ca0106/ca0106.h      |   13 ++-
 pci/ca0106/ca0106_main.c |  367 ++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 347 insertions(+), 33 deletions(-)
pci/ca0106/ca0106.h +12 -1
index 74175fc..bf502b5 100644
@@ -1,7 +1,7 @@
 /*
  *  Copyright (c) 2004 James Courtier-Dutton <[email protected]>
  *  Driver CA0106 chips. e.g. Sound Blaster Audigy LS and Live 24bit
- *  Version: 0.0.22
+ *  Version: 0.0.23
  *
  *  FEATURES currently supported:
  *    See ca0106_main.c for features.
@@ -49,6 +49,8 @@
  *   Implement support for Line-in capture on SB Live 24bit.
  *  0.0.22
  *    Add support for mute control on SB Live 24bit (cards w/ SPI DAC)
+ *  0.0.23
+ *    Add support for playback sampling rate and format constraints.
  *
  *
  *  This code was initally based on code from ALSA's emu10k1x.c which is:
@@ -644,6 +646,8 @@
 
 #include "ca_midi.h"
 
+#define DRVNAME "snd-ca0106"
+
 struct snd_ca0106;
 
 struct snd_ca0106_channel {
@@ -659,6 +663,7 @@ struct snd_ca0106_pcm {
 	struct snd_pcm_substream *substream;
         int channel_id;
 	unsigned short running;
+	unsigned short hw_reserved;
 };
 
 struct snd_ca0106_details {
@@ -684,6 +689,7 @@ struct snd_ca0106 {
 	unsigned short model;		/* subsystem id */
 
 	spinlock_t emu_lock;
+	spinlock_t pcm_lock;
 
 	struct snd_ac97 *ac97;
 	struct snd_pcm *pcm;
@@ -703,6 +709,11 @@ struct snd_ca0106 {
 	struct snd_ca_midi midi2;
 
 	u16 spi_dac_reg[16];
+
+	unsigned short count_pb_44100_chan;
+	unsigned short count_pb_non_44100_chan;
+	unsigned short count_pb_S16_chan;
+	unsigned short count_pb_S32_chan;
 };
 
 int snd_ca0106_mixer(struct snd_ca0106 *emu);
pci/ca0106/ca0106_main.c +335 -32

468 lines not on this page · raw message

Signature · 1 line
1.5.4.3
#5 lore raw

This is the patch against alsa-driver.

---
 pci/ca0106/ca0106_main.patch |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
pci/ca0106/ca0106_main.patch +4 -4
index 03820b3..d661011 100644
@@ -14,12 +14,12 @@
  
  static struct snd_ca0106_details ca0106_chip_details[] = {
  	 /* Sound Blaster X-Fi Extreme Audio. This does not have an AC97. 53SB079000000 */
-@@ -1352,7 +1353,7 @@
+@@ -1542,7 +1543,7 @@
  	pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
  #if 1
- 	printk(KERN_INFO "snd-ca0106: Model %04x Rev %08x Serial %08x\n", chip->model,
--	       pci->revision, chip->serial);
-+	       snd_pci_revision(pci), chip->serial);
+	printk(KERN_INFO DRVNAME ": Model %04x Rev %08x Serial %08x\n",
+-		chip->model, pci->revision, chip->serial);
++		chip->model, snd_pci_revision(pci), chip->serial);
  #endif
  	strcpy(card->driver, "CA0106");
  	strcpy(card->shortname, "CA0106");
Signature · 1 line
1.5.4.3
#6 lore raw
In reply to Ben Stanley

Review

Quoted 2 lines · Ben Stanley
(quoted diff) +2 -0
+		/* We only support 44100 to spdif, not to DAC.
+		   (FIXME WHY?)*/

WHY? Because the DAC cannot be clocked at 44100, only 48000 and 96000 from the CA0106 chip.
The SPDIF can be clocked at 44100.
It is a hardware limitation.

Kind Regards

James

#7 lore raw
Quoted 8 lines · James Courtier-Dutton

Review

Quoted 2 lines · Ben Stanley
(quoted diff) +2 -0
+		/* We only support 44100 to spdif, not to DAC.
+		   (FIXME WHY?)*/

WHY? Because the DAC cannot be clocked at 44100, only 48000 and 96000 from the CA0106 chip.
The SPDIF can be clocked at 44100.
It is a hardware limitation.

The DAC supports 44100. Is the problem that the ca0106 can't provide a 44100 clock to the DAC?

#8 lore raw
Quoted 11 lines · Trent Piepho
Quoted 7 lines · James Courtier-Dutton

Review

Quoted 2 lines · Ben Stanley
(quoted diff) +2 -0
+		/* We only support 44100 to spdif, not to DAC.
+		   (FIXME WHY?)*/

WHY? Because the DAC cannot be clocked at 44100, only 48000 and 96000 from the CA0106 chip.
The SPDIF can be clocked at 44100.
It is a hardware limitation.

The DAC supports 44100. Is the problem that the ca0106 can't provide a 44100 clock to the DAC?

It does not really matter why, it does not work at 44100 and that is fact.

#9 lore raw

At Wed, 03 Sep 2008 19:36:51 +0100,

Quoted 11 lines · James Courtier-Dutton
Quoted 9 lines · Trent Piepho
Quoted 7 lines · James Courtier-Dutton

Review

Quoted 2 lines · Ben Stanley
(quoted diff) +2 -0
+		/* We only support 44100 to spdif, not to DAC.
+		   (FIXME WHY?)*/

WHY? Because the DAC cannot be clocked at 44100, only 48000 and 96000 from the CA0106 chip.
The SPDIF can be clocked at 44100.
It is a hardware limitation.

Quoted 3 lines
Quoted 2 lines

The DAC supports 44100. Is the problem that the ca0106 can't provide a 44100 clock to the DAC?

It does not really matter why, it does not work at 44100 and that is fact.

It does matter. At least, the situation should be commented correctly. Otherwise you'll have the same question and give the same answer again and again.

Takashi

#10 lore raw

Is it maybe that CA0106, as 10k1 and 10k2 can't work at 44100?

2008/9/4 Takashi Iwai <[email protected]>:

Quoted 28 lines

At Wed, 03 Sep 2008 19:36:51 +0100,

Quoted 6 lines · James Courtier-Dutton
Quoted 4 lines · Trent Piepho
Quoted 3 lines · James Courtier-Dutton
Quoted 2 lines · Ben Stanley
(quoted diff) +2 -0
+         /* We only support 44100 to spdif, not to DAC.
+            (FIXME WHY?)*/
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines

WHY? Because the DAC cannot be clocked at 44100, only 48000 and 96000 from the CA0106 chip.
The SPDIF can be clocked at 44100.
It is a hardware limitation.

The DAC supports 44100. Is the problem that the ca0106 can't provide a 44100 clock to the DAC?

It does not really matter why, it does not work at 44100 and that is fact.

It does matter. At least, the situation should be commented correctly. Otherwise you'll have the same question and give the same answer again and again.

Takashi
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

Signature · 5 lines
Vedran Miletić
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
#11 lore raw
In reply to Ben Stanley

At Wed, 03 Sep 2008 03:11:07 +1000,

Quoted 10 lines · Ben Stanley

Takashi,

Further to your previous comments [1], I have attempted to address the locking issues. I have re-designed the code to simplify the locking problems.

This is the patch against alsa-kmirror.

Thanks for the patch!

Quoted 48 lines

[1] http://thread.gmane.org/gmane.linux.alsa.devel/55527/focus=55539

---
 pci/ca0106/ca0106.h      |   13 ++-
 pci/ca0106/ca0106_main.c |  367 ++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 347 insertions(+), 33 deletions(-)
pci/ca0106/ca0106.h +6 -1
index 74175fc..bf502b5 100644
@@ -1,7 +1,7 @@
 /*
  *  Copyright (c) 2004 James Courtier-Dutton <[email protected]>
  *  Driver CA0106 chips. e.g. Sound Blaster Audigy LS and Live 24bit
- *  Version: 0.0.22
+ *  Version: 0.0.23
  *
  *  FEATURES currently supported:
  *    See ca0106_main.c for features.
@@ -49,6 +49,8 @@
  *   Implement support for Line-in capture on SB Live 24bit.
  *  0.0.22
  *    Add support for mute control on SB Live 24bit (cards w/ SPI DAC)
+ *  0.0.23
+ *    Add support for playback sampling rate and format constraints.
  *
  *
  *  This code was initally based on code from ALSA's emu10k1x.c which is:
@@ -644,6 +646,8 @@
 
 #include "ca_midi.h"
 
+#define DRVNAME "snd-ca0106"
+
 struct snd_ca0106;
 
 struct snd_ca0106_channel {
@@ -659,6 +663,7 @@ struct snd_ca0106_pcm {
 	struct snd_pcm_substream *substream;
         int channel_id;
 	unsigned short running;
+	unsigned short hw_reserved;
 };
 
 struct snd_ca0106_details {

pci/ca0106/ca0106.h struct snd_ca0106 { · line 692

Quoted 4 lines
pci/ca0106/ca0106.h +1 -0
@@ -684,6 +689,7 @@ struct snd_ca0106 {
 	unsigned short model;		/* subsystem id */
 
 	spinlock_t emu_lock;
+	spinlock_t pcm_lock;

The spinlock is needed if the lock is accessed in the IRQ handler. For your patch, use mutex instead.

However, the problem is that the lock is missing in some important places, namely, *_open(), *_close() and snd_ca0106_interrupt(). If it's used in snd_ca0106_interrupt(), it has to be spinlock indeed.

pci/ca0106/ca0106.h struct snd_ca0106 { · line 716

Quoted 12 lines
pci/ca0106/ca0106.h +5 -0
 	struct snd_ac97 *ac97;
 	struct snd_pcm *pcm;
@@ -703,6 +709,11 @@ struct snd_ca0106 {
 	struct snd_ca_midi midi2;
 
 	u16 spi_dac_reg[16];
+
+	unsigned short count_pb_44100_chan;
+	unsigned short count_pb_non_44100_chan;
+	unsigned short count_pb_S16_chan;
+	unsigned short count_pb_S32_chan;

Any reason to use short instead of char?

Quoted 8 lines
pci/ca0106/ca0106.h +8 -0
+static unsigned int all_spdif_playback_rates[] =
+	{44100, 48000, 96000, 192000};
+
+static int hw_rule_playback_rate(struct snd_pcm_hw_params *params,
+				 struct snd_pcm_hw_rule   *rule)
+{
+	struct snd_ca0106 *chip = rule->private;
+	int mask;

pci/ca0106/ca0106.h struct snd_ca0106 {

Quoted 10 lines
pci/ca0106/ca0106.h +10 -0
+	if (snd_BUG_ON(!chip))
+		return -EINVAL;
+
+	spin_lock(&chip->pcm_lock);
+	if (chip->spdif_enable) {
+		if (snd_BUG_ON(chip->count_pb_44100_chan &&
+			 chip->count_pb_non_44100_chan)) {
+			spin_unlock(&chip->pcm_lock);
+			return -EINVAL;
+		}

Don't use too much snd_BUG_ON(). This would be better to be a normal check since we have anyway races in parameter constraints, and this may happen indeed.

pci/ca0106/ca0106.h

Quoted 5 lines
pci/ca0106/ca0106.h +5 -0
+		if (snd_BUG_ON(chip->count_pb_44100_chan +
+			 chip->count_pb_non_44100_chan > 4)) {
+			spin_unlock(&chip->pcm_lock);
+			return -EINVAL;
+		}

This check can be dropped. Whether it's more than 4 doesn't affect the behavior of this function.

pci/ca0106/ca0106.h

Quoted 9 lines
pci/ca0106/ca0106.h +9 -0
+		/* Compute the mask applied to all_spdif_playback_rates */
+		if (chip->count_pb_44100_chan)
+			mask = 0x1;
+		else if (chip->count_pb_non_44100_chan)
+			mask = 0xE;
+		else
+			mask = 0xF;
+	} else {
+		/* 44100Hz is not supported for DAC (FIXME Why?) */

Remove FIXME Why, as James suggested.

pci/ca0106/ca0106.h

Quoted 6 lines
pci/ca0106/ca0106.h +6 -0
+static int hw_rule_playback_format(struct snd_pcm_hw_params *params,
+				   struct snd_pcm_hw_rule *rule)
+{
+	struct snd_ca0106 *chip = rule->private;
+	struct snd_mask fmt, *f = hw_param_mask(
+					params, SNDRV_PCM_HW_PARAM_FORMAT);

Better to put f = hw_params_mask() outside so that you don't need ugly line break here.

pci/ca0106/ca0106.h

Quoted 11 lines
pci/ca0106/ca0106.h +11 -0
+	int result;
+	if (snd_BUG_ON(!chip))
+		return -EINVAL;
+	snd_mask_none(&fmt);
+
+	spin_lock(&chip->pcm_lock);
+	if (snd_BUG_ON(chip->count_pb_S16_chan &&
+		 chip->count_pb_S32_chan)) {
+		spin_unlock(&chip->pcm_lock);
+		return -EINVAL;
+	}

Remove snd_BUG_ON().

pci/ca0106/ca0106.h

Quoted 5 lines
pci/ca0106/ca0106.h +5 -0
+	if (snd_BUG_ON(chip->count_pb_S16_chan +
+		 chip->count_pb_S32_chan > 4)) {
+		spin_unlock(&chip->pcm_lock);
+		return -EINVAL;
+	}

This check is needless.

pci/ca0106/ca0106.h static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substr · line 606

Quoted 7 lines
pci/ca0106/ca0106.h +2 -1
@@ -509,10 +602,24 @@ static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substr
         //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
         //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
 	channel->epcm = epcm;
-	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
+	err = snd_pcm_hw_constraint_integer(
+			runtime, SNDRV_PCM_HW_PARAM_PERIODS);

Usually runtime is in the same line of 'snd_pcm_hw_constraint_integer('

pci/ca0106/ca0106.h static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substr · line 610

Quoted 5 lines
pci/ca0106/ca0106.h +3 -1
+	if (err < 0)
                 return err;
-	if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
+	err = snd_pcm_hw_constraint_step(
+			runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);

Ditto.

Quoted 14 lines
pci/ca0106/ca0106.h +10 -0
@@ -647,6 +754,55 @@ static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream
 /* hw_free callback */
 static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream *substream)
 {
+	struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
+	struct snd_pcm_runtime *runtime = substream->runtime, *runtimei;
+	struct snd_ca0106_pcm *epcm = runtime->private_data;
+	struct snd_ca0106_channel *pchannel;
+	int channel = epcm->channel_id, chi;
+
+	spin_lock(&chip->pcm_lock);
+	epcm->hw_reserved = 0;
+	chip->count_pb_44100_chan = chip->count_pb_non_44100_chan = 0;
+	chip->count_pb_S16_chan = chip->count_pb_S32_chan = 0;

pci/ca0106/ca0106.h static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream · line 776

Quoted 10 lines
pci/ca0106/ca0106.h +10 -0
+	for (chi = 0; chi < 4; ++chi) {
+		if (chi == channel)
+			continue;
+		pchannel = &(chip->playback_channels[chi]);
+		if (!pchannel->use)
+			continue;
+		if (snd_BUG_ON(!pchannel->epcm)) {
+			spin_unlock(&chip->pcm_lock);
+			return -EINVAL;
+		}

snd_BUG_ON() is wrong here. Note that pchannel->epcm isn't protected at all by chip->pcm_lock in your patch because it's not used in *_open() and *_close() callbacks. Use the lock to protect the assignment of these.

pci/ca0106/ca0106.h static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream · line 786

Quoted 10 lines
pci/ca0106/ca0106.h +10 -0
+		if (!pchannel->epcm->hw_reserved)
+			continue;
+		if (snd_BUG_ON(!pchannel->epcm->substream)) {
+			spin_unlock(&chip->pcm_lock);
+			return -EINVAL;
+		}
+		if (snd_BUG_ON(!pchannel->epcm->substream->runtime)) {
+			spin_unlock(&chip->pcm_lock);
+			return -EINVAL;
+		}

Ditto.

pci/ca0106/ca0106.h static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream · line 799

Quoted 13 lines
pci/ca0106/ca0106.h +13 -0
+		runtimei = pchannel->epcm->substream->runtime;
+		chip->count_pb_44100_chan += runtimei->rate == 44100;
+		chip->count_pb_non_44100_chan += runtimei->rate != 44100;
+		chip->count_pb_S16_chan +=
+			runtimei->format == SNDRV_PCM_FORMAT_S16_LE;
+		chip->count_pb_S32_chan +=
+			runtimei->format == SNDRV_PCM_FORMAT_S32_LE;
+	}
+	snd_BUG_ON(chip->count_pb_44100_chan && chip->count_pb_non_44100_chan);
+	snd_BUG_ON(chip->count_pb_44100_chan +
+		chip->count_pb_non_44100_chan > 4);
+	snd_BUG_ON(chip->count_pb_S16_chan && chip->count_pb_S32_chan);
+	snd_BUG_ON(chip->count_pb_S16_chan + chip->count_pb_S32_chan > 4);

snd_BUG_ON() here is rather useless.

Quoted 2 lines
pci/ca0106/ca0106.h +0 -0
@@ -668,53 +824,196 @@ static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream)
 static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)

(snip)

Quoted 21 lines
pci/ca0106/ca0106.h +21 -0
+	/* We are forced to build the settings for all the channels. */
+	spin_lock(&emu->pcm_lock);
+	emu->count_pb_44100_chan = emu->count_pb_non_44100_chan = 0;
+	emu->count_pb_S16_chan = emu->count_pb_S32_chan = 0;
+	for (chi = 0; chi < 4; ++chi) {
+		if (chi == channel)
+			continue;
+		pchannel = &(emu->playback_channels[chi]);
+		if (!pchannel->use)
+			continue;
+		if (snd_BUG_ON(!pchannel->epcm)) {
+			spin_unlock(&emu->pcm_lock);
+			return -EINVAL;
+		}
+		if (!pchannel->epcm->hw_reserved)
+			continue;
+		if (snd_BUG_ON(!pchannel->epcm->substream)) {
+			spin_unlock(&emu->pcm_lock);
+			return -EINVAL;
+		}
+		if (snd_BUG_ON(!pchannel->epcm->substream->runtime)) {

pci/ca0106/ca0106.h

Quoted 10 lines
pci/ca0106/ca0106.h +10 -0
+			spin_unlock(&emu->pcm_lock);
+			return -EINVAL;
+		}
+		runtimei = pchannel->epcm->substream->runtime;
+		emu->count_pb_44100_chan += runtimei->rate == 44100;
+		emu->count_pb_non_44100_chan += runtimei->rate != 44100;
+		emu->count_pb_S16_chan +=
+			runtimei->format == SNDRV_PCM_FORMAT_S16_LE;
+		emu->count_pb_S32_chan +=
+			runtimei->format == SNDRV_PCM_FORMAT_S32_LE;

This is as same as in *_hw_free().
Better to split this as another function and call it from both appropriately.

thanks,

Takashi

#12 lore raw

Takashi,

Thankyou for your comments.

My modifications would be much simpler if I didn't have to re-build the counts of how many channels are in each mode every time the prepare and hw_free functions are called.

In the case of the prepare function, I could modify the existing counts if I knew what the previous state (rate, format) was before the most recent change.

In the case of the hw_free function, I think that the rate and format information is invalid for the current channel by the time the hw_free function is called.

If I knew these pieces of information, I could completely avoid re-building the counts.

Regarding the locking, initially I only tried to protect my count fields. I have now tried to work on the locking some more (see incremental untested patch at end). Each time I learn some more... thanks for your patience. Perhaps there is another driver that already has to do this level of locking that you could point me to look at. The other drivers I have looked at so far don't have to do this much locking. (I gather that means they are designed to avoid the need to lock?)

Regarding atomicity, the documentation states that snd_ca0106_pcm_trigger_capture, snd_ca0106_pcm_pointer_playback, snd_ca0106_pcm_pointer_capture are called with a spinlock held by the middle layer. What lock is that? Do I need to hold my spinlock in addition? Will that possibly cause deadlock if the spinlocks are grabbed in different orders? So many questions...

Regarding snd_BUG_ON, I have used it like an assert, intending that it should be turned off in normal production code. Is that not the case?

Further comments interleaved below.

Quoted 71 lines · Takashi Iwai

At Wed, 03 Sep 2008 03:11:07 +1000,

Quoted 10 lines · Ben Stanley

Takashi,

Further to your previous comments [1], I have attempted to address the locking issues. I have re-designed the code to simplify the locking problems.

This is the patch against alsa-kmirror.

Thanks for the patch!

Quoted 48 lines

[1] http://thread.gmane.org/gmane.linux.alsa.devel/55527/focus=55539

---
 pci/ca0106/ca0106.h      |   13 ++-
 pci/ca0106/ca0106_main.c |  367 ++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 347 insertions(+), 33 deletions(-)
pci/ca0106/ca0106.h +6 -1
index 74175fc..bf502b5 100644
@@ -1,7 +1,7 @@
 /*
  *  Copyright (c) 2004 James Courtier-Dutton <[email protected]>
  *  Driver CA0106 chips. e.g. Sound Blaster Audigy LS and Live 24bit
- *  Version: 0.0.22
+ *  Version: 0.0.23
  *
  *  FEATURES currently supported:
  *    See ca0106_main.c for features.
@@ -49,6 +49,8 @@
  *   Implement support for Line-in capture on SB Live 24bit.
  *  0.0.22
  *    Add support for mute control on SB Live 24bit (cards w/ SPI DAC)
+ *  0.0.23
+ *    Add support for playback sampling rate and format constraints.
  *
  *
  *  This code was initally based on code from ALSA's emu10k1x.c which is:
@@ -644,6 +646,8 @@
 
 #include "ca_midi.h"
 
+#define DRVNAME "snd-ca0106"
+
 struct snd_ca0106;
 
 struct snd_ca0106_channel {
@@ -659,6 +663,7 @@ struct snd_ca0106_pcm {
 	struct snd_pcm_substream *substream;
         int channel_id;
 	unsigned short running;
+	unsigned short hw_reserved;
 };
 
 struct snd_ca0106_details {

pci/ca0106/ca0106.h struct snd_ca0106 { · line 692

Quoted 4 lines
pci/ca0106/ca0106.h +1 -0
@@ -684,6 +689,7 @@ struct snd_ca0106 {
 	unsigned short model;		/* subsystem id */
 
 	spinlock_t emu_lock;
+	spinlock_t pcm_lock;

The spinlock is needed if the lock is accessed in the IRQ handler. For your patch, use mutex instead.

Quoted 3 lines

However, the problem is that the lock is missing in some important places, namely, *_open(), *_close() and snd_ca0106_interrupt(). If it's used in snd_ca0106_interrupt(), it has to be spinlock indeed.

I tried to address this some more in the incremental patch at the end of this email.

Quoted 7 lines
Quoted 7 lines
pci/ca0106/ca0106.h +0 -0
 	struct snd_ac97 *ac97;
 	struct snd_pcm *pcm;
@@ -703,6 +709,11 @@ struct snd_ca0106 {
 	struct snd_ca_midi midi2;
 
Quoted 8 lines

pci/ca0106/ca0106.h struct snd_ca0106 { · line 716

Quoted 6 lines
pci/ca0106/ca0106.h +5 -0
 	u16 spi_dac_reg[16];
+
+	unsigned short count_pb_44100_chan;
+	unsigned short count_pb_non_44100_chan;
+	unsigned short count_pb_S16_chan;
+	unsigned short count_pb_S32_chan;

Any reason to use short instead of char?

I could change that.

Quoted 15 lines
Quoted 15 lines
pci/ca0106/ca0106.h +14 -0
+static unsigned int all_spdif_playback_rates[] =
+	{44100, 48000, 96000, 192000};
+
+static int hw_rule_playback_rate(struct snd_pcm_hw_params *params,
+				 struct snd_pcm_hw_rule   *rule)
+{
+	struct snd_ca0106 *chip = rule->private;
+	int mask;
+	if (snd_BUG_ON(!chip))
+		return -EINVAL;
+
+	spin_lock(&chip->pcm_lock);
+	if (chip->spdif_enable) {
+		if (snd_BUG_ON(chip->count_pb_44100_chan &&
Quoted 8 lines

pci/ca0106/ca0106.h

Quoted 4 lines
pci/ca0106/ca0106.h +4 -0
+			 chip->count_pb_non_44100_chan)) {
+			spin_unlock(&chip->pcm_lock);
+			return -EINVAL;
+		}

Don't use too much snd_BUG_ON(). This would be better to be a normal check since we have anyway races in parameter constraints, and this may happen indeed.

This is just asserting the invariants, and it did find some problems during development. This condition should not be triggered by the races in parameter constraints, as I took special care to deal with that problem in the prepare function. I would consider removal before promotion to an 'error' check.

Quoted 9 lines

pci/ca0106/ca0106.h

Quoted 6 lines
pci/ca0106/ca0106.h +5 -0
+		if (snd_BUG_ON(chip->count_pb_44100_chan +
+			 chip->count_pb_non_44100_chan > 4)) {
+			spin_unlock(&chip->pcm_lock);
+			return -EINVAL;
+		}

This check can be dropped. Whether it's more than 4 doesn't affect the behavior of this function.

Asserting invariants, same comments as above. If the total count of channels in the 44100 and non-44100 sets is above the number of channels on the card, there is a problem. However, this cannot happen with the current design of the patch, so I am happy to remove it.

Quoted 12 lines

pci/ca0106/ca0106.h

Quoted 10 lines
pci/ca0106/ca0106.h +9 -0
+		/* Compute the mask applied to all_spdif_playback_rates */
+		if (chip->count_pb_44100_chan)
+			mask = 0x1;
+		else if (chip->count_pb_non_44100_chan)
+			mask = 0xE;
+		else
+			mask = 0xF;
+	} else {
+		/* 44100Hz is not supported for DAC (FIXME Why?) */

Remove FIXME Why, as James suggested.

Good, I'm glad James responded to clear that up :-)

Quoted 10 lines

pci/ca0106/ca0106.h

Quoted 7 lines
pci/ca0106/ca0106.h +6 -0
+static int hw_rule_playback_format(struct snd_pcm_hw_params *params,
+				   struct snd_pcm_hw_rule *rule)
+{
+	struct snd_ca0106 *chip = rule->private;
+	struct snd_mask fmt, *f = hw_param_mask(
+					params, SNDRV_PCM_HW_PARAM_FORMAT);

Better to put f = hw_params_mask() outside so that you don't need ugly line break here.

Can do.

Quoted 15 lines

pci/ca0106/ca0106.h

Quoted 12 lines
pci/ca0106/ca0106.h +11 -0
+	int result;
+	if (snd_BUG_ON(!chip))
+		return -EINVAL;
+	snd_mask_none(&fmt);
+
+	spin_lock(&chip->pcm_lock);
+	if (snd_BUG_ON(chip->count_pb_S16_chan &&
+		 chip->count_pb_S32_chan)) {
+		spin_unlock(&chip->pcm_lock);
+		return -EINVAL;
+	}

Remove snd_BUG_ON().

Quoted 7 lines

pci/ca0106/ca0106.h

Quoted 5 lines
pci/ca0106/ca0106.h +5 -0
+	if (snd_BUG_ON(chip->count_pb_S16_chan +
+		 chip->count_pb_S32_chan > 4)) {
+		spin_unlock(&chip->pcm_lock);
+		return -EINVAL;
+	}

This check is needless.

During development these checks were useful. Now they can be removed.

Quoted 10 lines

pci/ca0106/ca0106.h static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substr · line 606

Quoted 8 lines
pci/ca0106/ca0106.h +2 -1
@@ -509,10 +602,24 @@ static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substr
         //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
         //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
 	channel->epcm = epcm;
-	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
+	err = snd_pcm_hw_constraint_integer(
+			runtime, SNDRV_PCM_HW_PARAM_PERIODS);

Usually runtime is in the same line of 'snd_pcm_hw_constraint_integer('

Can change.

Quoted 8 lines

pci/ca0106/ca0106.h static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substr · line 610

Quoted 6 lines
pci/ca0106/ca0106.h +3 -1
+	if (err < 0)
                 return err;
-	if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
+	err = snd_pcm_hw_constraint_step(
+			runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);

Ditto.

Can change.

Quoted 22 lines
Quoted 22 lines
pci/ca0106/ca0106.h +17 -0
@@ -647,6 +754,55 @@ static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream
 /* hw_free callback */
 static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream *substream)
 {
+	struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
+	struct snd_pcm_runtime *runtime = substream->runtime, *runtimei;
+	struct snd_ca0106_pcm *epcm = runtime->private_data;
+	struct snd_ca0106_channel *pchannel;
+	int channel = epcm->channel_id, chi;
+
+	spin_lock(&chip->pcm_lock);
+	epcm->hw_reserved = 0;
+	chip->count_pb_44100_chan = chip->count_pb_non_44100_chan = 0;
+	chip->count_pb_S16_chan = chip->count_pb_S32_chan = 0;
+	for (chi = 0; chi < 4; ++chi) {
+		if (chi == channel)
+			continue;
+		pchannel = &(chip->playback_channels[chi]);
+		if (!pchannel->use)
+			continue;
+		if (snd_BUG_ON(!pchannel->epcm)) {
Quoted 9 lines

pci/ca0106/ca0106.h static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream · line 776

Quoted 3 lines
pci/ca0106/ca0106.h +3 -0
+			spin_unlock(&chip->pcm_lock);
+			return -EINVAL;
+		}

snd_BUG_ON() is wrong here. Note that pchannel->epcm isn't protected at all by chip->pcm_lock in your patch because it's not used in *_open() and *_close() callbacks. Use the lock to protect the assignment of these.

Hmmm... The open callback allocates a new snd_ca0106_pcm by kmalloc, but the close callback does not kfree it. Is that a leak? Oh, I see that it is kfreed by snd_ca0106_pcm_free_substream.

I can't find any other part of the driver that writes to the epcm field. Furthermore, I observe that the use field is set to 1 before epcm field is assigned. So perhaps the lock should cover this case.

Quoted 4 lines
Quoted 4 lines
pci/ca0106/ca0106.h +4 -0
+		if (!pchannel->epcm->hw_reserved)
+			continue;
+		if (snd_BUG_ON(!pchannel->epcm->substream)) {
+			spin_unlock(&chip->pcm_lock);
Quoted 8 lines

pci/ca0106/ca0106.h static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream · line 786

Quoted 6 lines
pci/ca0106/ca0106.h +6 -0
+			return -EINVAL;
+		}
+		if (snd_BUG_ON(!pchannel->epcm->substream->runtime)) {
+			spin_unlock(&chip->pcm_lock);
+			return -EINVAL;
+		}

Ditto.

Yeah, I see you don't like my defensive programming. I'll work on understanding these better so that I can be confident of removing the checks.

Quoted 16 lines
Quoted 4 lines
pci/ca0106/ca0106.h +3 -0
+		runtimei = pchannel->epcm->substream->runtime;
+		chip->count_pb_44100_chan += runtimei->rate == 44100;
+		chip->count_pb_non_44100_chan += runtimei->rate != 44100;

pci/ca0106/ca0106.h static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream · line 799

Quoted 10 lines
pci/ca0106/ca0106.h +10 -0
+		chip->count_pb_S16_chan +=
+			runtimei->format == SNDRV_PCM_FORMAT_S16_LE;
+		chip->count_pb_S32_chan +=
+			runtimei->format == SNDRV_PCM_FORMAT_S32_LE;
+	}
+	snd_BUG_ON(chip->count_pb_44100_chan && chip->count_pb_non_44100_chan);
+	snd_BUG_ON(chip->count_pb_44100_chan +
+		chip->count_pb_non_44100_chan > 4);
+	snd_BUG_ON(chip->count_pb_S16_chan && chip->count_pb_S32_chan);
+	snd_BUG_ON(chip->count_pb_S16_chan + chip->count_pb_S32_chan > 4);

snd_BUG_ON() here is rather useless.

Can remove. Was useful during development.

Quoted 31 lines
Quoted 3 lines
pci/ca0106/ca0106.h +0 -0
@@ -668,53 +824,196 @@ static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream)
 static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)

(snip)

Quoted 27 lines
pci/ca0106/ca0106.h +27 -0
+	/* We are forced to build the settings for all the channels. */
+	spin_lock(&emu->pcm_lock);
+	emu->count_pb_44100_chan = emu->count_pb_non_44100_chan = 0;
+	emu->count_pb_S16_chan = emu->count_pb_S32_chan = 0;
+	for (chi = 0; chi < 4; ++chi) {
+		if (chi == channel)
+			continue;
+		pchannel = &(emu->playback_channels[chi]);
+		if (!pchannel->use)
+			continue;
+		if (snd_BUG_ON(!pchannel->epcm)) {
+			spin_unlock(&emu->pcm_lock);
+			return -EINVAL;
+		}
+		if (!pchannel->epcm->hw_reserved)
+			continue;
+		if (snd_BUG_ON(!pchannel->epcm->substream)) {
+			spin_unlock(&emu->pcm_lock);
+			return -EINVAL;
+		}
+		if (snd_BUG_ON(!pchannel->epcm->substream->runtime)) {
+			spin_unlock(&emu->pcm_lock);
+			return -EINVAL;
+		}
+		runtimei = pchannel->epcm->substream->runtime;
+		emu->count_pb_44100_chan += runtimei->rate == 44100;
+		emu->count_pb_non_44100_chan += runtimei->rate != 44100;
Quoted 8 lines

pci/ca0106/ca0106.h

Quoted 4 lines
pci/ca0106/ca0106.h +4 -0
+		emu->count_pb_S16_chan +=
+			runtimei->format == SNDRV_PCM_FORMAT_S16_LE;
+		emu->count_pb_S32_chan +=
+			runtimei->format == SNDRV_PCM_FORMAT_S32_LE;

This is as same as in *_hw_free().
Better to split this as another function and call it from both appropriately.

Yes, I thought about that after my late night of hacking...

Even better would be to figure out how to avoid re-building the counts, as discussed at the top. But, this works and seems to be robust against my nasty test script.

It is not quite the same between hw_free and prepare, as the prepare function takes special care to check whether what is requested fulfils the constraints. If it satisfies the constraints, it is configured. The check and the configuration are performed atomically.

So it looks very similar between the two, but is subtly different. Perhaps I can code both versions in one function.

Incremental untested patch trying to address the locking is shown below. I'm sure you'll find things you don't like :-). Please ignore the other issues outlined above for the moment - I will work on addressing your other comments another night. Please review the locking.

Ben Stanley.

Quoted 1 line

From c1c72cf2584c493080ddc04f9b9c36c38bb1b2bd Mon Sep 17 00:00:00 2001

From: Ben Stanley <[email protected]>
Date: Thu, 4 Sep 2008 01:00:26 +1000
Subject: [PATCH] Change locking to protect more stuff in accordance with Takashi's comments.

---
 pci/ca0106/ca0106_main.c  |   91 +++++++++++++++++++++++++++++++--------------
 pci/ca0106/ca0106_mixer.c |    3 +
 2 files changed, 66 insertions(+), 28 deletions(-)
pci/ca0106/ca0106_main.c +63 -28
index 806f34c..794de68 100644
@@ -333,19 +333,20 @@ static int hw_rule_playback_rate(struct snd_pcm_hw_params *params,
 {
 	struct snd_ca0106 *chip = rule->private;
 	int mask;
+	unsigned long flags;
 	if (snd_BUG_ON(!chip))
 		return -EINVAL;
 
-	spin_lock(&chip->pcm_lock);
+	spin_lock_irqsave(&chip->pcm_lock, flags);
 	if (chip->spdif_enable) {
 		if (snd_BUG_ON(chip->count_pb_44100_chan &&
 			 chip->count_pb_non_44100_chan)) {
-			spin_unlock(&chip->pcm_lock);
+			spin_unlock_irqrestore(&chip->pcm_lock, flags);
 			return -EINVAL;
 		}
 		if (snd_BUG_ON(chip->count_pb_44100_chan +
 			 chip->count_pb_non_44100_chan > 4)) {
-			spin_unlock(&chip->pcm_lock);
+			spin_unlock_irqrestore(&chip->pcm_lock, flags);
 			return -EINVAL;
 		}
 		/* Compute the mask applied to all_spdif_playback_rates */
@@ -364,7 +365,7 @@ static int hw_rule_playback_rate(struct snd_pcm_hw_params *params,
 		chip->count_pb_44100_chan,
 		chip->count_pb_non_44100_chan,
 		mask, chip->spdif_enable);
-	spin_unlock(&chip->pcm_lock);
+	spin_unlock_irqrestore(&chip->pcm_lock, flags);
 	return snd_interval_list(hw_param_interval(params,
 						   SNDRV_PCM_HW_PARAM_RATE),
 				 ARRAY_SIZE(all_spdif_playback_rates),
@@ -377,20 +378,21 @@ static int hw_rule_playback_format(struct snd_pcm_hw_params *params,
 	struct snd_ca0106 *chip = rule->private;
 	struct snd_mask fmt, *f = hw_param_mask(
 					params, SNDRV_PCM_HW_PARAM_FORMAT);
+	unsigned long flags;
 	int result;
 	if (snd_BUG_ON(!chip))
 		return -EINVAL;
 	snd_mask_none(&fmt);
 
-	spin_lock(&chip->pcm_lock);
+	spin_lock_irqsave(&chip->pcm_lock, flags);
 	if (snd_BUG_ON(chip->count_pb_S16_chan &&
 		 chip->count_pb_S32_chan)) {
-		spin_unlock(&chip->pcm_lock);
+		spin_unlock_irqrestore(&chip->pcm_lock, flags);
 		return -EINVAL;
 	}
 	if (snd_BUG_ON(chip->count_pb_S16_chan +
 		 chip->count_pb_S32_chan > 4)) {
-		spin_unlock(&chip->pcm_lock);
+		spin_unlock_irqrestore(&chip->pcm_lock, flags);
 		return -EINVAL;
 	}
 	if (chip->count_pb_S16_chan)
@@ -408,7 +410,7 @@ static int hw_rule_playback_format(struct snd_pcm_hw_params *params,
 		chip->count_pb_S16_chan,
 		chip->count_pb_S32_chan, f->bits[0], fmt.bits[0],
 		result);
-	spin_unlock(&chip->pcm_lock);
+	spin_unlock_irqrestore(&chip->pcm_lock, flags);
 	return result;
 }
 
@@ -581,6 +583,7 @@ static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substr
 	struct snd_ca0106_pcm *epcm;
 	struct snd_pcm_runtime *runtime = substream->runtime;
 	int err;
+	unsigned long flags;
 
 	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
 
@@ -589,7 +592,8 @@ static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substr
 	epcm->emu = chip;
 	epcm->substream = substream;
         epcm->channel_id=channel_id;
-  
+
+	spin_lock_irqsave(&chip->pcm_lock, flags);
 	runtime->private_data = epcm;
 	runtime->private_free = snd_ca0106_pcm_free_substream;
   
@@ -605,22 +609,23 @@ static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substr
 	err = snd_pcm_hw_constraint_integer(
 			runtime, SNDRV_PCM_HW_PARAM_PERIODS);
 	if (err < 0)
-                return err;
+                goto error;
 	err = snd_pcm_hw_constraint_step(
 			runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
 	if (err < 0)
-                return err;
+                goto error;
 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
 			hw_rule_playback_rate, (void *)chip,
 			SNDRV_PCM_HW_PARAM_RATE, -1);
 	if (err < 0)
-		return err;
+		goto error;
 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
 		       hw_rule_playback_format, (void *)chip,
 			SNDRV_PCM_HW_PARAM_FORMAT, -1);
 	if (err < 0)
-		return err;
+		goto error;
 	snd_pcm_set_sync(substream);
+	spin_unlock_irqrestore(&chip->pcm_lock, flags);
 
 	if (chip->details->spi_dac && channel_id != PCM_FRONT_CHANNEL) {
 		const int reg = spi_dacd_reg[channel_id];
@@ -632,6 +637,9 @@ static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substr
 			return err;
 	}
 	return 0;
+error:
+	spin_unlock_irqrestore(&chip->pcm_lock, flags);
+	return err;
 }
 
 /* close callback */
@@ -639,7 +647,11 @@ static int snd_ca0106_pcm_close_playback(struct snd_pcm_substream *substream)
 {
 	struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
 	struct snd_pcm_runtime *runtime = substream->runtime;
-        struct snd_ca0106_pcm *epcm = runtime->private_data;
+        struct snd_ca0106_pcm *epcm;
+	unsigned long flags;
+
+	spin_lock_irqsave(&chip->pcm_lock, flags);
+	epcm = runtime->private_data;
 	chip->playback_channels[epcm->channel_id].use = 0;
 
 	if (chip->details->spi_dac && epcm->channel_id != PCM_FRONT_CHANNEL) {
@@ -650,6 +662,7 @@ static int snd_ca0106_pcm_close_playback(struct snd_pcm_substream *substream)
 		snd_ca0106_spi_write(chip, chip->spi_dac_reg[reg]);
 	}
 	/* FIXME: maybe zero others */
+	spin_unlock_irqrestore(&chip->pcm_lock, flags);
 	return 0;
 }
 
@@ -681,6 +694,7 @@ static int snd_ca0106_pcm_open_capture_channel(struct snd_pcm_substream *substre
         struct snd_ca0106_channel *channel = &(chip->capture_channels[channel_id]);
 	struct snd_ca0106_pcm *epcm;
 	struct snd_pcm_runtime *runtime = substream->runtime;
+	unsigned long flags;
 	int err;
 
 	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
@@ -692,6 +706,7 @@ static int snd_ca0106_pcm_open_capture_channel(struct snd_pcm_substream *substre
 	epcm->substream = substream;
         epcm->channel_id=channel_id;
   
+	spin_lock_irqsave(&chip->pcm_lock, flags);
 	runtime->private_data = epcm;
 	runtime->private_free = snd_ca0106_pcm_free_substream;
   
@@ -704,6 +719,7 @@ static int snd_ca0106_pcm_open_capture_channel(struct snd_pcm_substream *substre
         //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
         //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
         channel->epcm = epcm;
+	spin_unlock_irqsave(&chip->pcm_lock, flags);
 	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
                 return err;
 	//snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_capture_period_sizes);
@@ -717,9 +733,14 @@ static int snd_ca0106_pcm_close_capture(struct snd_pcm_substream *substream)
 {
 	struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
 	struct snd_pcm_runtime *runtime = substream->runtime;
-        struct snd_ca0106_pcm *epcm = runtime->private_data;
+        struct snd_ca0106_pcm *epcm;
+	unsigned long flags;
+	spin_lock_irqsave(&chip->pcm_lock, flags);
+	epcm = runtime->private_data;
+
 	chip->capture_channels[epcm->channel_id].use = 0;
 	/* FIXME: maybe zero others */
+	spin_unlock_irqrestore(&chip->pcm_lock, flags);
 	return 0;
 }
 
@@ -759,8 +780,9 @@ static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream *substream)
 	struct snd_ca0106_pcm *epcm = runtime->private_data;
 	struct snd_ca0106_channel *pchannel;
 	int channel = epcm->channel_id, chi;
+	unsigned long flags;
 
-	spin_lock(&chip->pcm_lock);
+	spin_lock_irqsave(&chip->pcm_lock, flags);
 	epcm->hw_reserved = 0;
 	chip->count_pb_44100_chan = chip->count_pb_non_44100_chan = 0;
 	chip->count_pb_S16_chan = chip->count_pb_S32_chan = 0;
@@ -771,17 +793,17 @@ static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream *substream)
 		if (!pchannel->use)
 			continue;
 		if (snd_BUG_ON(!pchannel->epcm)) {
-			spin_unlock(&chip->pcm_lock);
+			spin_unlock_irqrestore(&chip->pcm_lock, flags);
 			return -EINVAL;
 		}
 		if (!pchannel->epcm->hw_reserved)
 			continue;
 		if (snd_BUG_ON(!pchannel->epcm->substream)) {
-			spin_unlock(&chip->pcm_lock);
+			spin_unlock_irqrestore(&chip->pcm_lock, flags);
 			return -EINVAL;
 		}
 		if (snd_BUG_ON(!pchannel->epcm->substream->runtime)) {
-			spin_unlock(&chip->pcm_lock);
+			spin_unlock_irqrestore(&chip->pcm_lock, flags);
 			return -EINVAL;
 		}
 		runtimei = pchannel->epcm->substream->runtime;
@@ -801,7 +823,7 @@ static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream *substream)
 		"any_non_44100=%d, spdif=%d.\n",
 		chip->count_pb_44100_chan, chip->count_pb_non_44100_chan,
 		emu->spdif_enable);
-	spin_unlock(&chip->pcm_lock);
+	spin_unlock_irqrestore(&chip->pcm_lock, flags);
 
 	return snd_pcm_lib_free_pages(substream);
 }
@@ -843,7 +865,9 @@ static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)
 	u32 reg71_set = 0;
 	u32 reg71;
 	int i;
+	unsigned long flags;
 	
+	spin_lock_irqsave(&emu->pcm_lock, flags);
 	/* FIXME CLEAN UP IF spdif_enable IS CHANGED WHILE CHANNELS ARE OPENED
 	 * OR PREVENT THIS FROM HAPPENING. */
 	if (emu->spdif_enable)
@@ -864,7 +888,6 @@ static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)
 	/*printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",
 		emu->buffer.addr,emu->buffer.area, emu->buffer.bytes);*/
 	/* We are forced to build the settings for all the channels. */
-	spin_lock(&emu->pcm_lock);
 	emu->count_pb_44100_chan = emu->count_pb_non_44100_chan = 0;
 	emu->count_pb_S16_chan = emu->count_pb_S32_chan = 0;
 	for (chi = 0; chi < 4; ++chi) {
@@ -874,17 +897,17 @@ static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)
 		if (!pchannel->use)
 			continue;
 		if (snd_BUG_ON(!pchannel->epcm)) {
-			spin_unlock(&emu->pcm_lock);
+			spin_unlock_irqrestore(&emu->pcm_lock, flags);
 			return -EINVAL;
 		}
 		if (!pchannel->epcm->hw_reserved)
 			continue;
 		if (snd_BUG_ON(!pchannel->epcm->substream)) {
-			spin_unlock(&emu->pcm_lock);
+			spin_unlock_irqrestore(&emu->pcm_lock, flags);
 			return -EINVAL;
 		}
 		if (snd_BUG_ON(!pchannel->epcm->substream->runtime)) {
-			spin_unlock(&emu->pcm_lock);
+			spin_unlock_irqrestore(&emu->pcm_lock, flags);
 			return -EINVAL;
 		}
 		runtimei = pchannel->epcm->substream->runtime;
@@ -979,14 +1002,14 @@ static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)
 		reg71_set |= 0x3 << reg71_shift;
 		break;
 	default:
-		spin_unlock(&emu->pcm_lock);
+		spin_unlock_irqrestore(&emu->pcm_lock, flags);
 		printk(KERN_ERR DRVNAME
 			": prepare_playback: Bad sampling frequency %dHz.\n",
 			runtime->rate);
 		return -EINVAL;
 	}
 	if (count_pb_44100_chan && count_pb_non_44100_chan) {
-		spin_unlock(&emu->pcm_lock);
+		spin_unlock_irqrestore(&emu->pcm_lock, flags);
 		printk(KERN_ERR DRVNAME
 			"prepare_playback: requested sampling rate %dHz"
 			" conflicts with other selected sampling rates.\n",
@@ -994,7 +1017,7 @@ static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)
 		return -EINVAL;
 	}
 	if (count_pb_S16_chan && count_pb_S32_chan) {
-		spin_unlock(&emu->pcm_lock);
+		spin_unlock_irqrestore(&emu->pcm_lock, flags);
 		printk(KERN_ERR DRVNAME
 			"prepare_playback: requested sample format %d"
 			" conflicts with other selected sample formats.\n",
@@ -1025,7 +1048,7 @@ static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)
 		hcfg_set = 0;
 		break;
 	}
-	spin_unlock(&emu->pcm_lock);
+	spin_unlock_irqrestore(&emu->pcm_lock, flags);
 	hcfg = inl(emu->port + HCFG) ;
 	hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
 	outl(hcfg, emu->port + HCFG);
@@ -1079,10 +1102,12 @@ static int snd_ca0106_pcm_prepare_capture(struct snd_pcm_substream *substream)
 	u32 reg71_mask = 0x0000c000 ; /* Global. Set ADC rate. */
 	u32 reg71_set = 0;
 	u32 reg71;
+	unsigned long flags;
 	
         //snd_printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, periods=%u, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size, runtime->periods, frames_to_bytes(runtime, 1));
         //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
 	//snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
+	spin_lock_irqsave(&emu->pcm_lock, flags);
 	/* reg71 controls ADC rate. */
 	switch (runtime->rate) {
 	case 44100:
@@ -1116,6 +1141,7 @@ static int snd_ca0106_pcm_prepare_capture(struct snd_pcm_substream *substream)
 		hcfg_set = 0;
 		break;
 	}
+	spin_unlock_irqrestore(&emu->pcm_lock, flags);
 	hcfg = inl(emu->port + HCFG) ;
 	hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
 	outl(hcfg, emu->port + HCFG);
@@ -1149,6 +1175,7 @@ static int snd_ca0106_pcm_trigger_playback(struct snd_pcm_substream *substream,
 	u32 basic = 0;
 	u32 extended = 0;
 	int running=0;
+	unsigned long flags;
 
 	switch (cmd) {
 	case SNDRV_PCM_TRIGGER_START:
@@ -1159,6 +1186,7 @@ static int snd_ca0106_pcm_trigger_playback(struct snd_pcm_substream *substream,
 		running=0;
 		break;
 	}
+	spin_lock_irqsave(&emu->pcm_lock, flags);
         snd_pcm_group_for_each_entry(s, substream) {
 		if (snd_pcm_substream_chip(s) != emu ||
 		    s->stream != SNDRV_PCM_STREAM_PLAYBACK)
@@ -1187,6 +1215,7 @@ static int snd_ca0106_pcm_trigger_playback(struct snd_pcm_substream *substream,
 		result = -EINVAL;
 		break;
 	}
+	spin_unlock_irqrestore(&emu->pcm_lock, flags);
 	return result;
 }
 
@@ -1199,7 +1228,9 @@ static int snd_ca0106_pcm_trigger_capture(struct snd_pcm_substream *substream,
 	struct snd_ca0106_pcm *epcm = runtime->private_data;
 	int channel = epcm->channel_id;
 	int result = 0;
+	unsigned long flags;
 
+	spin_lock_irqsave(&emu->pcm_lock, flags);
 	switch (cmd) {
 	case SNDRV_PCM_TRIGGER_START:
 		snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
@@ -1215,6 +1246,7 @@ static int snd_ca0106_pcm_trigger_capture(struct snd_pcm_substream *substream,
 		result = -EINVAL;
 		break;
 	}
+	spin_unlock_irqrestore(&emu->pcm_lock, flags);
 	return result;
 }
 
@@ -1451,6 +1483,7 @@ static irqreturn_t snd_ca0106_interrupt(int irq, void *dev_id)
 	int mask;
         unsigned int stat76;
 	struct snd_ca0106_channel *pchannel;
+	unsigned long flags;
 
 	status = inl(chip->port + IPR);
 	if (! status)
@@ -1460,6 +1493,7 @@ static irqreturn_t snd_ca0106_interrupt(int irq, void *dev_id)
 	//snd_printk("interrupt status = 0x%08x, stat76=0x%08x\n", status, stat76);
 	//snd_printk("ptr=0x%08x\n",snd_ca0106_ptr_read(chip, PLAYBACK_POINTER, 0));
         mask = 0x11; /* 0x1 for one half, 0x10 for the other half period. */
+	spin_lock_irqsave(&chip->pcm_lock, flags);
 	for(i = 0; i < 4; i++) {
 		pchannel = &(chip->playback_channels[i]);
 		if (stat76 & mask) {
@@ -1487,6 +1521,7 @@ static irqreturn_t snd_ca0106_interrupt(int irq, void *dev_id)
 	        //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
 		mask <<= 1;
 	}
+	spin_unlock_irqrestore(&chip->pcm_lock, flags);
 
         snd_ca0106_ptr_write(chip, EXTENDED_INT, 0, stat76);
 
pci/ca0106/ca0106_mixer.c +3 -0
index 3025ed1..6752a05 100644
@@ -96,7 +96,9 @@ static int snd_ca0106_shared_spdif_put(struct snd_kcontrol *kcontrol,
 	unsigned int val;
 	int change = 0;
 	u32 mask;
+	unsigned long flags;
 
+	spin_lock_irqsave(&emu->pcm_lock, flags);
 	val = !!ucontrol->value.integer.value[0];
 	change = (emu->spdif_enable != val);
 	if (change) {
@@ -120,6 +122,7 @@ static int snd_ca0106_shared_spdif_put(struct snd_kcontrol *kcontrol,
 			outl(mask, emu->port + GPIO);
 		}
 	}
+	spin_unlock_irqrestore(&emu->pcm_lock, flags );
         return change;
 }
 
Signature · 1 line
1.5.4.3
#13 lore raw

At Thu, 04 Sep 2008 01:15:47 +1000,

Quoted 20 lines · Ben Stanley

Takashi,

Thankyou for your comments.

My modifications would be much simpler if I didn't have to re-build the counts of how many channels are in each mode every time the prepare and hw_free functions are called.

In the case of the prepare function, I could modify the existing counts if I knew what the previous state (rate, format) was before the most recent change.

In the case of the hw_free function, I think that the rate and format information is invalid for the current channel by the time the hw_free function is called.

If I knew these pieces of information, I could completely avoid re-building the counts.

Quoted 8 lines

Regarding the locking, initially I only tried to protect my count fields. I have now tried to work on the locking some more (see incremental untested patch at end). Each time I learn some more... thanks for your patience. Perhaps there is another driver that already has to do this level of locking that you could point me to look at. The other drivers I have looked at so far don't have to do this much locking. (I gather that means they are designed to avoid the need to lock?)

There must be some, but each driver has different things :)

Quoted 4 lines

Regarding atomicity, the documentation states that snd_ca0106_pcm_trigger_capture, snd_ca0106_pcm_pointer_playback, snd_ca0106_pcm_pointer_capture are called with a spinlock held by the middle layer. What lock is that?

It's PCM substream lock.

Quoted 3 lines

Do I need to hold my spinlock in
addition? Will that possibly cause deadlock if the spinlocks are grabbed in different orders? So many questions...

Yes, you need another type of lock because PCM substream lock is specific to each substream, and you need a global lock over all substreams.

Quoted 2 lines

Regarding snd_BUG_ON, I have used it like an assert, intending that it should be turned off in normal production code. Is that not the case?

The problem is that some checks should be done even for the production system, not only for debugging. At least, exclusivity of 44.1kHz/48kHZ rates should be checked always, because the hardware constraint can be racy.

Quoted 12 lines
Quoted 11 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+	spin_lock(&chip->pcm_lock);
+	if (chip->spdif_enable) {
+		if (snd_BUG_ON(chip->count_pb_44100_chan &&
+			 chip->count_pb_non_44100_chan)) {
+			spin_unlock(&chip->pcm_lock);
+			return -EINVAL;
+		}

Don't use too much snd_BUG_ON(). This would be better to be a normal check since we have anyway races in parameter constraints, and this may happen indeed.

Quoted 3 lines

This is just asserting the invariants, and it did find some problems during development. This condition should not be triggered by the races in parameter constraints,

... but this could happen because of races, I'm afraid.

Quoted 31 lines
Quoted 21 lines
Quoted 21 lines
(quoted diff) +17 -0
@@ -647,6 +754,55 @@ static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream
 /* hw_free callback */
 static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream *substream)
 {
+	struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
+	struct snd_pcm_runtime *runtime = substream->runtime, *runtimei;
+	struct snd_ca0106_pcm *epcm = runtime->private_data;
+	struct snd_ca0106_channel *pchannel;
+	int channel = epcm->channel_id, chi;
+
+	spin_lock(&chip->pcm_lock);
+	epcm->hw_reserved = 0;
+	chip->count_pb_44100_chan = chip->count_pb_non_44100_chan = 0;
+	chip->count_pb_S16_chan = chip->count_pb_S32_chan = 0;
+	for (chi = 0; chi < 4; ++chi) {
+		if (chi == channel)
+			continue;
+		pchannel = &(chip->playback_channels[chi]);
+		if (!pchannel->use)
+			continue;
+		if (snd_BUG_ON(!pchannel->epcm)) {
Quoted 9 lines

Review static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream · line 776

Quoted 3 lines
(quoted diff) +3 -0
+			spin_unlock(&chip->pcm_lock);
+			return -EINVAL;
+		}

snd_BUG_ON() is wrong here. Note that pchannel->epcm isn't protected at all by chip->pcm_lock in your patch because it's not used in *_open() and *_close() callbacks. Use the lock to protect the assignment of these.

Quoted 7 lines

Hmmm... The open callback allocates a new snd_ca0106_pcm by kmalloc, but the close callback does not kfree it. Is that a leak? Oh, I see that it is kfreed by snd_ca0106_pcm_free_substream.

I can't find any other part of the driver that writes to the epcm field. Furthermore, I observe that the use field is set to 1 before epcm field is assigned. So perhaps the lock should cover this case.

That's a part of the problem. It can run between use = 1 and epcm set.

Quoted 5 lines

It is not quite the same between hw_free and prepare, as the prepare function takes special care to check whether what is requested fulfils the constraints. If it satisfies the constraints, it is configured. The check and the configuration are performed atomically.

Quoted 7 lines

So it looks very similar between the two, but is subtly different. Perhaps I can code both versions in one function.

Incremental untested patch trying to address the locking is shown below. I'm sure you'll find things you don't like :-). Please ignore the other issues outlined above for the moment - I will work on addressing your other comments another night. Please review the locking.

I'll check the patch later (now busy during attending conference...)

Thanks!

Takashi

#14 lore raw

Takashi,

Just a quick note:

The current version of my patch causes kernel lockups.

I have not yet figured out how to debug this.

I will do some research, possibly compile a debug kernel with the magic sysreq key enabled or something. I need to figure out where it is locking up. Any hints appreciated.

For now I have reverted to an earlier version of the patch before I included the spinlocks, and I now get no lockups. The machine is in daily use running MythTV. However, only one of the four outputs is in regular use. (My card only has one connection on the back, anyway.)

But perhaps you could tell by examining my locking code whether I have broken some cardinal rule.

I already found a bug in an error path in prepare where I forgot to unlock.

Thanks,
Ben Stanley.

Quoted 134 lines · Takashi Iwai

At Thu, 04 Sep 2008 01:15:47 +1000,

Quoted 20 lines · Ben Stanley

Takashi,

Thankyou for your comments.

My modifications would be much simpler if I didn't have to re-build the counts of how many channels are in each mode every time the prepare and hw_free functions are called.

In the case of the prepare function, I could modify the existing counts if I knew what the previous state (rate, format) was before the most recent change.

In the case of the hw_free function, I think that the rate and format information is invalid for the current channel by the time the hw_free function is called.

If I knew these pieces of information, I could completely avoid re-building the counts.

Quoted 8 lines

Regarding the locking, initially I only tried to protect my count fields. I have now tried to work on the locking some more (see incremental untested patch at end). Each time I learn some more... thanks for your patience. Perhaps there is another driver that already has to do this level of locking that you could point me to look at. The other drivers I have looked at so far don't have to do this much locking. (I gather that means they are designed to avoid the need to lock?)

There must be some, but each driver has different things :)

Quoted 4 lines

Regarding atomicity, the documentation states that snd_ca0106_pcm_trigger_capture, snd_ca0106_pcm_pointer_playback, snd_ca0106_pcm_pointer_capture are called with a spinlock held by the middle layer. What lock is that?

It's PCM substream lock.

Quoted 3 lines

Do I need to hold my spinlock in
addition? Will that possibly cause deadlock if the spinlocks are grabbed in different orders? So many questions...

Yes, you need another type of lock because PCM substream lock is specific to each substream, and you need a global lock over all substreams.

Quoted 2 lines

Regarding snd_BUG_ON, I have used it like an assert, intending that it should be turned off in normal production code. Is that not the case?

The problem is that some checks should be done even for the production system, not only for debugging. At least, exclusivity of 44.1kHz/48kHZ rates should be checked always, because the hardware constraint can be racy.

Quoted 12 lines
Quoted 11 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+	spin_lock(&chip->pcm_lock);
+	if (chip->spdif_enable) {
+		if (snd_BUG_ON(chip->count_pb_44100_chan &&
+			 chip->count_pb_non_44100_chan)) {
+			spin_unlock(&chip->pcm_lock);
+			return -EINVAL;
+		}

Don't use too much snd_BUG_ON(). This would be better to be a normal check since we have anyway races in parameter constraints, and this may happen indeed.

Quoted 3 lines

This is just asserting the invariants, and it did find some problems during development. This condition should not be triggered by the races in parameter constraints,

... but this could happen because of races, I'm afraid.

Quoted 31 lines
Quoted 21 lines
Quoted 21 lines
(quoted diff) +17 -0
@@ -647,6 +754,55 @@ static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream
 /* hw_free callback */
 static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream *substream)
 {
+	struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
+	struct snd_pcm_runtime *runtime = substream->runtime, *runtimei;
+	struct snd_ca0106_pcm *epcm = runtime->private_data;
+	struct snd_ca0106_channel *pchannel;
+	int channel = epcm->channel_id, chi;
+
+	spin_lock(&chip->pcm_lock);
+	epcm->hw_reserved = 0;
+	chip->count_pb_44100_chan = chip->count_pb_non_44100_chan = 0;
+	chip->count_pb_S16_chan = chip->count_pb_S32_chan = 0;
+	for (chi = 0; chi < 4; ++chi) {
+		if (chi == channel)
+			continue;
+		pchannel = &(chip->playback_channels[chi]);
+		if (!pchannel->use)
+			continue;
+		if (snd_BUG_ON(!pchannel->epcm)) {
Quoted 9 lines

Review static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream · line 776

Quoted 3 lines
(quoted diff) +3 -0
+			spin_unlock(&chip->pcm_lock);
+			return -EINVAL;
+		}

snd_BUG_ON() is wrong here. Note that pchannel->epcm isn't protected at all by chip->pcm_lock in your patch because it's not used in *_open() and *_close() callbacks. Use the lock to protect the assignment of these.

Quoted 7 lines

Hmmm... The open callback allocates a new snd_ca0106_pcm by kmalloc, but the close callback does not kfree it. Is that a leak? Oh, I see that it is kfreed by snd_ca0106_pcm_free_substream.

I can't find any other part of the driver that writes to the epcm field. Furthermore, I observe that the use field is set to 1 before epcm field is assigned. So perhaps the lock should cover this case.

That's a part of the problem. It can run between use = 1 and epcm set.

Quoted 5 lines

It is not quite the same between hw_free and prepare, as the prepare function takes special care to check whether what is requested fulfils the constraints. If it satisfies the constraints, it is configured. The check and the configuration are performed atomically.

Quoted 7 lines

So it looks very similar between the two, but is subtly different. Perhaps I can code both versions in one function.

Incremental untested patch trying to address the locking is shown below. I'm sure you'll find things you don't like :-). Please ignore the other issues outlined above for the moment - I will work on addressing your other comments another night. Please review the locking.

I'll check the patch later (now busy during attending conference...)

Thanks!

Takashi

#15 lore raw

At Wed, 10 Sep 2008 07:13:17 +1000,

Quoted 18 lines · Ben Stanley

Takashi,

Just a quick note:

The current version of my patch causes kernel lockups.

I have not yet figured out how to debug this.

I will do some research, possibly compile a debug kernel with the magic sysreq key enabled or something. I need to figure out where it is locking up. Any hints appreciated.

For now I have reverted to an earlier version of the patch before I included the spinlocks, and I now get no lockups. The machine is in daily use running MythTV. However, only one of the four outputs is in regular use. (My card only has one connection on the back, anyway.)

Quoted 5 lines

But perhaps you could tell by examining my locking code whether I have broken some cardinal rule.

I already found a bug in an error path in prepare where I forgot to unlock.

Thanks for information. And, don't worry, I didn't applied your previous one yet :)
(I'm in a conference and cannot work intensively for ALSA right now.) But will check if you send a newer patch.

thanks,

Takashi

Quoted 139 lines

Thanks,
Ben Stanley.

Quoted 134 lines · Takashi Iwai

At Thu, 04 Sep 2008 01:15:47 +1000,

Quoted 20 lines · Ben Stanley

Takashi,

Thankyou for your comments.

My modifications would be much simpler if I didn't have to re-build the counts of how many channels are in each mode every time the prepare and hw_free functions are called.

In the case of the prepare function, I could modify the existing counts if I knew what the previous state (rate, format) was before the most recent change.

In the case of the hw_free function, I think that the rate and format information is invalid for the current channel by the time the hw_free function is called.

If I knew these pieces of information, I could completely avoid re-building the counts.

Quoted 8 lines

Regarding the locking, initially I only tried to protect my count fields. I have now tried to work on the locking some more (see incremental untested patch at end). Each time I learn some more... thanks for your patience. Perhaps there is another driver that already has to do this level of locking that you could point me to look at. The other drivers I have looked at so far don't have to do this much locking. (I gather that means they are designed to avoid the need to lock?)

There must be some, but each driver has different things :)

Quoted 4 lines

Regarding atomicity, the documentation states that snd_ca0106_pcm_trigger_capture, snd_ca0106_pcm_pointer_playback, snd_ca0106_pcm_pointer_capture are called with a spinlock held by the middle layer. What lock is that?

It's PCM substream lock.

Quoted 3 lines

Do I need to hold my spinlock in
addition? Will that possibly cause deadlock if the spinlocks are grabbed in different orders? So many questions...

Yes, you need another type of lock because PCM substream lock is specific to each substream, and you need a global lock over all substreams.

Quoted 2 lines

Regarding snd_BUG_ON, I have used it like an assert, intending that it should be turned off in normal production code. Is that not the case?

The problem is that some checks should be done even for the production system, not only for debugging. At least, exclusivity of 44.1kHz/48kHZ rates should be checked always, because the hardware constraint can be racy.

Quoted 12 lines
Quoted 11 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+	spin_lock(&chip->pcm_lock);
+	if (chip->spdif_enable) {
+		if (snd_BUG_ON(chip->count_pb_44100_chan &&
+			 chip->count_pb_non_44100_chan)) {
+			spin_unlock(&chip->pcm_lock);
+			return -EINVAL;
+		}

Don't use too much snd_BUG_ON(). This would be better to be a normal check since we have anyway races in parameter constraints, and this may happen indeed.

Quoted 3 lines

This is just asserting the invariants, and it did find some problems during development. This condition should not be triggered by the races in parameter constraints,

... but this could happen because of races, I'm afraid.

Quoted 31 lines
Quoted 21 lines
Quoted 21 lines
(quoted diff) +17 -0
@@ -647,6 +754,55 @@ static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream
 /* hw_free callback */
 static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream *substream)
 {
+	struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
+	struct snd_pcm_runtime *runtime = substream->runtime, *runtimei;
+	struct snd_ca0106_pcm *epcm = runtime->private_data;
+	struct snd_ca0106_channel *pchannel;
+	int channel = epcm->channel_id, chi;
+
+	spin_lock(&chip->pcm_lock);
+	epcm->hw_reserved = 0;
+	chip->count_pb_44100_chan = chip->count_pb_non_44100_chan = 0;
+	chip->count_pb_S16_chan = chip->count_pb_S32_chan = 0;
+	for (chi = 0; chi < 4; ++chi) {
+		if (chi == channel)
+			continue;
+		pchannel = &(chip->playback_channels[chi]);
+		if (!pchannel->use)
+			continue;
+		if (snd_BUG_ON(!pchannel->epcm)) {
Quoted 9 lines

Review static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream · line 776

Quoted 3 lines
(quoted diff) +3 -0
+			spin_unlock(&chip->pcm_lock);
+			return -EINVAL;
+		}

snd_BUG_ON() is wrong here. Note that pchannel->epcm isn't protected at all by chip->pcm_lock in your patch because it's not used in *_open() and *_close() callbacks. Use the lock to protect the assignment of these.

Quoted 7 lines

Hmmm... The open callback allocates a new snd_ca0106_pcm by kmalloc, but the close callback does not kfree it. Is that a leak? Oh, I see that it is kfreed by snd_ca0106_pcm_free_substream.

I can't find any other part of the driver that writes to the epcm field. Furthermore, I observe that the use field is set to 1 before epcm field is assigned. So perhaps the lock should cover this case.

That's a part of the problem. It can run between use = 1 and epcm set.

Quoted 5 lines

It is not quite the same between hw_free and prepare, as the prepare function takes special care to check whether what is requested fulfils the constraints. If it satisfies the constraints, it is configured. The check and the configuration are performed atomically.

Quoted 7 lines

So it looks very similar between the two, but is subtly different. Perhaps I can code both versions in one function.

Incremental untested patch trying to address the locking is shown below. I'm sure you'll find things you don't like :-). Please ignore the other issues outlined above for the moment - I will work on addressing your other comments another night. Please review the locking.

I'll check the patch later (now busy during attending conference...)

Thanks!

Takashi