Skip to contentSkip to conversation

Stop Apple i2s DMA gracefully

RFC/PATCH Applied Paul Mackerras 3 participants 16 messages Oct 29, 2006 – Feb 8, 2007

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

This fixes the problem of getting extra bytes inserted at the beginning of a recording when using the Apple i2s interface and DBDMA controller. It turns out that we can't just abort the DMA; we have to let it stop at the end of a command, and then wait for the S7 bit to be set before turning off the DBDMA controller. Doing that for playback doesn't seem to be necessary, but doesn't hurt either.

We use the technique used by the Darwin driver: make each transfer command branch to a stop command if the S0 status bit is set. Thus we can ask the DMA controller to stop at the end of the current command by setting S0.

The interrupt routine now looks at and clears the status word of the DBDMA command ring. This is necessary so it can know when the DBDMA controller has seen that S0 is set, and so when it should look for the DBDMA controller being stopped and S7 being set. This also ended up simplifying the calculation in i2sbus_pcm_pointer.

Tested on a 15 inch albook.

---

sound/aoa/soundbus/i2sbus/i2sbus-core.c +2 -2
index 23190aa..1c24771 100644
@@ -41,8 +41,8 @@ static int alloc_dbdma_descriptor_ring(s
 				       struct dbdma_command_mem *r,
 				       int numcmds)
 {
-	/* one more for rounding */
-	r->size = (numcmds+1) * sizeof(struct dbdma_cmd);
+	/* one more for rounding, one for branch back, one for stop command */
+	r->size = (numcmds + 3) * sizeof(struct dbdma_cmd);
 	/* We use the PCI APIs for now until the generic one gets fixed
 	 * enough or until we get some macio-specific versions
 	 */
sound/aoa/soundbus/i2sbus/i2sbus-pcm.c +166 -134

419 lines not on this page · raw message

sound/aoa/soundbus/i2sbus/i2sbus.h +3 -0
index 0c69d20..5a29544 100644
@@ -10,6 +10,7 @@ #define __I2SBUS_H
 #include <linux/interrupt.h>
 #include <linux/spinlock.h>
 #include <linux/mutex.h>
+#include <linux/completion.h>
 
 #include <sound/pcm.h>
 
@@ -34,6 +35,7 @@ struct dbdma_command_mem {
 	void *space;
 	int size;
 	u32 running:1;
+	u32 stopping:1;
 };
 
 struct pcm_info {
@@ -45,6 +47,7 @@ struct pcm_info {
 	u32 frame_count;
 	struct dbdma_command_mem dbdma_ring;
 	volatile struct dbdma_regs __iomem *dbdma;
+	struct completion *stop_completion;
 };
 
 enum {

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

maintainer
#1 lore raw
Quoted 12 lines · Paul Mackerras

This fixes the problem of getting extra bytes inserted at the beginning of a recording when using the Apple i2s interface and DBDMA controller. It turns out that we can't just abort the DMA; we have to let it stop at the end of a command, and then wait for the S7 bit to be set before turning off the DBDMA controller. Doing that for playback doesn't seem to be necessary, but doesn't hurt either.

We use the technique used by the Darwin driver: make each transfer command branch to a stop command if the S0 status bit is set. Thus we can ask the DMA controller to stop at the end of the current command by setting S0.

Quoted 7 lines

The interrupt routine now looks at and clears the status word of the DBDMA command ring. This is necessary so it can know when the DBDMA controller has seen that S0 is set, and so when it should look for the DBDMA controller being stopped and S7 being set. This also ended up simplifying the calculation in i2sbus_pcm_pointer.

Tested on a 15 inch albook.

Great!

sound/aoa/soundbus/i2sbus/i2sbus-pcm.c static int i2sbus_pcm_trigger(struct i2s · line 538

Quoted 5 lines
sound/aoa/soundbus/i2sbus/i2sbus-pcm.c +5 -0
+			if (in_le32(&pi->dbdma->status) & ACTIVE) {
+				/* possible race here? */
+				udelay(10);
+				if (in_le32(&pi->dbdma->status) & ACTIVE)
+					goto out_unlock; /* keep running */

Maybe we should clear the S0 bit and then check if it's active?

Clearing the S0 bit when the DMA engine has already stopped doesn't make a difference, and when it is still active we want to clear S0 in this 'play again case' anyway. Then if we clear it and check the ACTIVE bit afterwards we can be sure that if it is still set the engine will continue running.

Other than that, looks great to me, thanks a lot!

johannes

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

_______________________________________________
Alsa-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/alsa-devel

#2 lore raw
Quoted 1 line · Johannes Berg

Maybe we should clear the S0 bit and then check if it's active?

That's what I do, exactly.

Quoted 5 lines

Clearing the S0 bit when the DMA engine has already stopped doesn't make a difference, and when it is still active we want to clear S0 in this 'play again case' anyway. Then if we clear it and check the ACTIVE bit afterwards we can be sure that if it is still set the engine will continue running.

The possible race is that the DBDMA might see the S0 bit set just before we clear it, but it will still take a little while for it to do the branch, fetch the stop command, and execute it. So it is possible that we clear S0 and then check the ACTIVE bit and see that it is still set, but in fact the DBDMA did see the S0 bit set and is in the process of stopping, and if we do nothing it will stop and the input or output will stall.

That's why I put the udelay(10) in there. It may be possible to do better by telling the DBDMA to do a flush and looking at the status words in the DBDMA command list. If we see the status from the flush then the DBDMA didn't see S0; if the DBDMA stops before writing out the status from the flush then it did see it. However, the udelay seems to work OK and is simple.

Regards,
Paul.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

maintainer Acked-by
#3 lore raw
Quoted 5 lines · Paul Mackerras
Quoted 1 line · Johannes Berg

Maybe we should clear the S0 bit and then check if it's active?

That's what I do, exactly.

Whoops, missed that, sorry.

Quoted 8 lines

The possible race is that the DBDMA might see the S0 bit set just before we clear it, but it will still take a little while for it to do the branch, fetch the stop command, and execute it. So it is possible that we clear S0 and then check the ACTIVE bit and see that it is still set, but in fact the DBDMA did see the S0 bit set and is in the process of stopping, and if we do nothing it will stop and the input or output will stall.

Quoted 6 lines

That's why I put the udelay(10) in there. It may be possible to do better by telling the DBDMA to do a flush and looking at the status words in the DBDMA command list. If we see the status from the flush then the DBDMA didn't see S0; if the DBDMA stops before writing out the status from the flush then it did see it. However, the udelay seems to work OK and is simple.

Yeah, I just missed the S0 clearing, now I understand. Sorry about that. Thanks for clearing it up.

So just in case anyone cares

Thanks a lot,
johannes

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

_______________________________________________
Alsa-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/alsa-devel

#4 lore raw
In reply to Paul Mackerras

At Sun, 29 Oct 2006 15:19:40 +1100,

Quoted 13 lines · Paul Mackerras

This fixes the problem of getting extra bytes inserted at the beginning of a recording when using the Apple i2s interface and DBDMA controller. It turns out that we can't just abort the DMA; we have to let it stop at the end of a command, and then wait for the S7 bit to be set before turning off the DBDMA controller. Doing that for playback doesn't seem to be necessary, but doesn't hurt either.

We use the technique used by the Darwin driver: make each transfer command branch to a stop command if the S0 status bit is set. Thus we can ask the DMA controller to stop at the end of the current command by setting S0.

Quoted 5 lines

The interrupt routine now looks at and clears the status word of the DBDMA command ring. This is necessary so it can know when the DBDMA controller has seen that S0 is set, and so when it should look for the DBDMA controller being stopped and S7 being set. This also ended up simplifying the calculation in i2sbus_pcm_pointer.

Thanks, the patch looks good, just very minor issues.

Quoted 4 lines
sound/aoa/soundbus/i2sbus/i2sbus-pcm.c +0 -0
@@ -245,18 +245,66 @@ static int i2sbus_pcm_close(struct i2sbu
 	return err;

sound/aoa/soundbus/i2sbus/i2sbus-pcm.c static int i2sbus_pcm_close(struct i2sbu · line 255

Quoted 10 lines
sound/aoa/soundbus/i2sbus/i2sbus-pcm.c +8 -0
 }
 
+static void i2sbus_wait_for_stop(struct i2sbus_dev *i2sdev,
+				 struct pcm_info *pi)
+{
+	unsigned long flags;
+	struct completion done;
+	long timeout;
+
+	spin_lock_irqsave(&i2sdev->low_lock, flags);

We need no irqsave/irqrestore but here since the function is supposed to be called always in schedulable situation. spin_lock_irq() should be enough.

sound/aoa/soundbus/i2sbus/i2sbus-pcm.c static int i2sbus_pcm_close(struct i2sbu · line 286

Quoted 8 lines
sound/aoa/soundbus/i2sbus/i2sbus-pcm.c +1 -1
 static int i2sbus_hw_params(struct snd_pcm_substream *substream,
 			    struct snd_pcm_hw_params *params)
 {
 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
 }
 
-static int i2sbus_hw_free(struct snd_pcm_substream *substream)
+static inline int i2sbus_hw_free(struct snd_pcm_substream *substream, int in)

Any reason to put extra inline here? It's no time-critical funciton, so let compiler do optimization.

BTW, shoud this go to 2.6.19 although the fix isn't so trivial, or could wait for the next?

Takashi

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

#5 lore raw
Quoted 3 lines · Takashi Iwai

We need no irqsave/irqrestore but here since the function is supposed to be called always in schedulable situation. spin_lock_irq() should be enough.

OK.

Quoted 2 lines

Any reason to put extra inline here? It's no time-critical funciton, so let compiler do optimization.

Sure.

Quoted 2 lines

BTW, shoud this go to 2.6.19 although the fix isn't so trivial, or could wait for the next?

I don't have a strong feeling either way. It is a bug fix, but for a bug which doesn't seem to have affected many people (although perhaps people were avoiding using capture because it didn't work properly), but it's not a bug which can lead to any crash or vulnerability as far as I can see.

Paul.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

maintainer
#6 lore raw
In reply to Paul Mackerras
Quoted 15 lines · Paul Mackerras
sound/aoa/soundbus/i2sbus/i2sbus-pcm.c +1 -7
 	case SNDRV_PCM_TRIGGER_STOP:
 	case SNDRV_PCM_TRIGGER_SUSPEND:
 		if (!pi->dbdma_ring.running) {
 			result = -EALREADY;
 			goto out_unlock;
 		}
+		pi->dbdma_ring.running = 0;
 
-		/* turn off all relevant bits */
-		out_le32(&pi->dbdma->control,
-			 (RUN | WAKE | FLUSH | PAUSE) << 16);
-		{
-			/* FIXME: move to own function */
-			int timeout = 5000;
-			while ((in_le32(&pi->dbdma->status) & RUN)

sound/aoa/soundbus/i2sbus/i2sbus-pcm.c static int i2sbus_pcm_trigger(struct i2s · line 571

Quoted 10 lines
sound/aoa/soundbus/i2sbus/i2sbus-pcm.c +3 -7
-			       && --timeout > 0)
-				udelay(1);
-			if (!timeout)
-				printk(KERN_ERR
-				       "i2sbus: timed out turning "
-				       "off dbdma engine!\n");
-		}
+		/* Set the S0 bit to make the DMA branch to the stop cmd */
+		out_le32(&pi->dbdma->control, (1 << 16) | 1);
+		pi->dbdma_ring.stopping = 1;

Something that just crossed my mind because Ben was complaining about suspend/resume breakage... In the suspend case we actually have to make sure to have DMA stopped, no?

johannes

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

#7 lore raw
Quoted 3 lines · Johannes Berg

Something that just crossed my mind because Ben was complaining about suspend/resume breakage... In the suspend case we actually have to make sure to have DMA stopped, no?

True. Can the trigger function sleep in the suspend case? And presumably the resume trigger should only restart the DMA if it was running when the suspend trigger was called, but how does it know that?

Paul.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

maintainer
#8 lore raw
Quoted 1 line · Paul Mackerras

True. Can the trigger function sleep in the suspend case?

No idea. Takashi?

Quoted 4 lines

And
presumably the resume trigger should only restart the DMA if it was running when the suspend trigger was called, but how does it know that?

Well, I assume that if it's not playing the pcm trigger will neither be called during suspend nor during resume, since the actual device suspend is later, this is only the pcm suspend. Actually, could the device suspend sleep and wait? Then we could play a sound during suspend if we somehow manage to make the audio device suspend last, and the pcm first :)

johannes

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

_______________________________________________
Alsa-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/alsa-devel

#9 lore raw

At Thu, 02 Nov 2006 23:59:54 +0100,

Quoted 6 lines · Johannes Berg
Quoted 1 line · Paul Mackerras

True. Can the trigger function sleep in the suspend case?

No idea. Takashi?

The trigger callback cannot sleep in design. It's always atomic.

Usually, the driver calls snd_pcm_suspend_all() in suspend callback, which triggers with SNDRV_PCM_TRIGGER_SUSPEND. In your case, this should terminate the DMA while SNDRV_PCM_TRIGGER_STOP continues the DMA. Or, put a sync call after snd_pcm_suspend_all() in the suspend callback.

The implementation of resume depends on the hardware, BTW. In short, there is no snd_pcm_resume*() function.

If your hardware supports the real resume (i.e. can restart the stream as it was), set SNDRV_PCM_INFO_RESUME flag in snd_pcm_hardware info field, and do the right thing in resume callback.
If not (in many cases), the usual reset procedure is done (prepare, trigger-start), so the resume callback doesn't have to do any special thing about PCM. SNDRV_PCM_TRIGGER_SUSPEND should be handled just like a normal STOP trigger, in such a case.

Takashi

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

#10 lore raw
Quoted 7 lines · Takashi Iwai

The trigger callback cannot sleep in design. It's always atomic.

Usually, the driver calls snd_pcm_suspend_all() in suspend callback, which triggers with SNDRV_PCM_TRIGGER_SUSPEND. In your case, this should terminate the DMA while SNDRV_PCM_TRIGGER_STOP continues the DMA. Or, put a sync call after snd_pcm_suspend_all() in the suspend callback.

OK, this sounds like the suspend trigger should just abort the DMA, on the basis that the hardware will get reset in the suspend/resume process and so there will be no bytes queued up to cause trouble later. I'll whip up a patch.

Paul.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

maintainer
#11 lore raw
Quoted 4 lines · Paul Mackerras

OK, this sounds like the suspend trigger should just abort the DMA, on the basis that the hardware will get reset in the suspend/resume process and so there will be no bytes queued up to cause trouble later. I'll whip up a patch.

I think we should cleanly stop the hardware in this case as well, for suspend to disk I think it can happen that we suspend the driver and then suspend is aborted. Not entirely sure about this though.

Btw. What happens when we abort the DMA engine, reset the command pointer to a stop command, and start it again? Does it drain the FIFO properly? I'm thinking it should, but ...

johannes

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

#12 lore raw
In reply to Paul Mackerras

Hi Paul,

any progress about the patch since then?
Or did I overlook?

thanks,

Takashi

At Mon, 6 Nov 2006 21:41:21 +1100,

Quoted 11 lines · Paul Mackerras
Quoted 7 lines · Takashi Iwai

The trigger callback cannot sleep in design. It's always atomic.

Usually, the driver calls snd_pcm_suspend_all() in suspend callback, which triggers with SNDRV_PCM_TRIGGER_SUSPEND. In your case, this should terminate the DMA while SNDRV_PCM_TRIGGER_STOP continues the DMA. Or, put a sync call after snd_pcm_suspend_all() in the suspend callback.

Quoted 7 lines

OK, this sounds like the suspend trigger should just abort the DMA, on the basis that the hardware will get reset in the suspend/resume process and so there will be no bytes queued up to cause trouble later. I'll whip up a patch.

Paul.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

maintainer
#13 lore raw
In reply to Takashi Iwai
Quoted 1 line · Takashi Iwai

The trigger callback cannot sleep in design. It's always atomic.

Alright.

Quoted 5 lines

Usually, the driver calls snd_pcm_suspend_all() in suspend callback, which triggers with SNDRV_PCM_TRIGGER_SUSPEND. In your case, this should terminate the DMA while SNDRV_PCM_TRIGGER_STOP continues the DMA. Or, put a sync call after snd_pcm_suspend_all() in the suspend callback.

Yeah, I think the latter is preferable.

Quoted 2 lines

The implementation of resume depends on the hardware, BTW. In short, there is no snd_pcm_resume*() function.

Ok.

Quoted 7 lines

If your hardware supports the real resume (i.e. can restart the stream as it was), set SNDRV_PCM_INFO_RESUME flag in snd_pcm_hardware info field, and do the right thing in resume callback.
If not (in many cases), the usual reset procedure is done (prepare, trigger-start), so the resume callback doesn't have to do any special thing about PCM. SNDRV_PCM_TRIGGER_SUSPEND should be handled just like a normal STOP trigger, in such a case.

We can restart the stream as it was by just rewriting the dbdma command register to point it where we were during suspend, since the dma command ring will have been kept intact in memory over suspend. Hence, I think we can actually support SNDRV_PCM_INFO_RESUME easily by doing the regular stop, storing the command pointer, and starting back where we were by reprogramming the dma engine properly.

However, this would also require reprogramming the i2s chip to the correct format etc, so it will be a lot easier to just do the regular prepare sequence.

Thanks,
johannes

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

maintainer
#14 lore raw

PATCH 1/1 aoa i2sbus: Stop Apple i2s DMA gracefully

In reply to Paul Mackerras

From: Paul Mackerras <[email protected]>

This fixes the problem of getting extra bytes inserted at the beginning of a recording when using the Apple i2s interface and DBDMA controller. It turns out that we can't just abort the DMA; we have to let it stop at the end of a command, and then wait for the S7 bit to be set before turning off the DBDMA controller. Doing that for playback doesn't seem to be necessary, but doesn't hurt either.

We use the technique used by the Darwin driver: make each transfer command branch to a stop command if the S0 status bit is set. Thus we can ask the DMA controller to stop at the end of the current command by setting S0.

The interrupt routine now looks at and clears the status word of the DBDMA command ring. This is necessary so it can know when the DBDMA controller has seen that S0 is set, and so when it should look for the DBDMA controller being stopped and S7 being set. This also ended up simplifying the calculation in i2sbus_pcm_pointer.

Tested on a 15 inch albook.

I modified this patch and added the suspend/resume bits to it to get my powermac into a decent state when playing sound across suspend to disk that has a different bitrate from what the firmware programs the hardware to.

I also added the SNDRV_PCM_INFO_JOINT_DUPLEX flag because it seemed the right thing to do and I was looking at the info stuff.

---
The only relevant change that isn't related to suspend is this piece (shown as interdiff here):

                        if (in_le32(&pi->dbdma->status) & ACTIVE) {
                                /* possible race here? */
                                udelay(10);

- if (in_le32(&pi->dbdma->status) & ACTIVE)
+ if (in_le32(&pi->dbdma->status) & ACTIVE) {
+ pi->dbdma_ring.stopping = 0;
goto out_unlock; /* keep running */
+ }

                        }
                }

Takashi, please apply this to the alsa tree.

I had also wanted to add PAUSE capability but for some reason that didn't do anything at all with the applications I used (aplay can't pause, with mplayer and gstreamer it made no change) so I'll leave that for when I know how to actually use it from userland and make a new patch for it then.

sound/aoa/soundbus/i2sbus/i2sbus-core.c +10 -12
@@ -41,8 +41,8 @@ static int alloc_dbdma_descriptor_ring(s
 				       struct dbdma_command_mem *r,
 				       int numcmds)
 {
-	/* one more for rounding */
-	r->size = (numcmds+1) * sizeof(struct dbdma_cmd);
+	/* one more for rounding, one for branch back, one for stop command */
+	r->size = (numcmds + 3) * sizeof(struct dbdma_cmd);
 	/* We use the PCI APIs for now until the generic one gets fixed
 	 * enough or until we get some macio-specific versions
 	 */
@@ -377,11 +377,8 @@ static int i2sbus_suspend(struct macio_d
 		if (i2sdev->sound.pcm) {
 			/* Suspend PCM streams */
 			snd_pcm_suspend_all(i2sdev->sound.pcm);
-			/* Probably useless as we handle
-			 * power transitions ourselves */
-			snd_power_change_state(i2sdev->sound.pcm->card,
-					       SNDRV_CTL_POWER_D3hot);
 		}
+
 		/* Notify codecs */
 		list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
 			err = 0;
@@ -390,7 +387,11 @@ static int i2sbus_suspend(struct macio_d
 			if (err)
 				ret = err;
 		}
+
+		/* wait until streams are stopped */
+		i2sbus_wait_for_stop_both(i2sdev);
 	}
+
 	return ret;
 }
 
@@ -402,6 +403,9 @@ static int i2sbus_resume(struct macio_de
 	int err, ret = 0;
 
 	list_for_each_entry(i2sdev, &control->list, item) {
+		/* reset i2s bus format etc. */
+		i2sbus_pcm_prepare_both(i2sdev);
+
 		/* Notify codecs so they can re-initialize */
 		list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
 			err = 0;
@@ -410,12 +414,6 @@ static int i2sbus_resume(struct macio_de
 			if (err)
 				ret = err;
 		}
-		/* Notify Alsa */
-		if (i2sdev->sound.pcm) {
-			/* Same comment as above, probably useless */
-			snd_power_change_state(i2sdev->sound.pcm->card,
-					       SNDRV_CTL_POWER_D0);
-		}
 	}
 
 	return ret;
sound/aoa/soundbus/i2sbus/i2sbus-pcm.c +195 -135

455 lines not on this page · raw message

sound/aoa/soundbus/i2sbus/i2sbus.h +6 -0
@@ -10,6 +10,7 @@
 #include <linux/interrupt.h>
 #include <linux/spinlock.h>
 #include <linux/mutex.h>
+#include <linux/completion.h>
 
 #include <sound/pcm.h>
 
@@ -34,6 +35,7 @@ struct dbdma_command_mem {
 	void *space;
 	int size;
 	u32 running:1;
+	u32 stopping:1;
 };
 
 struct pcm_info {
@@ -45,6 +47,7 @@ struct pcm_info {
 	u32 frame_count;
 	struct dbdma_command_mem dbdma_ring;
 	volatile struct dbdma_regs __iomem *dbdma;
+	struct completion *stop_completion;
 };
 
 enum {
@@ -101,6 +104,9 @@ i2sbus_tx_intr(int irq, void *devid);
 extern irqreturn_t
 i2sbus_rx_intr(int irq, void *devid);
 
+extern void i2sbus_wait_for_stop_both(struct i2sbus_dev *i2sdev);
+extern void i2sbus_pcm_prepare_both(struct i2sbus_dev *i2sdev);
+
 /* control specific functions */
 extern int i2sbus_control_init(struct macio_dev* dev,
 			       struct i2sbus_control **c);

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier. Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

Applied
#15 lore raw

Re: [PATCH] aoa i2sbus: Stop Apple i2s DMA gracefully

At Thu, 08 Feb 2007 14:12:37 +0100,

Quoted 30 lines · Johannes Berg

From: Paul Mackerras <[email protected]>

This fixes the problem of getting extra bytes inserted at the beginning of a recording when using the Apple i2s interface and DBDMA controller. It turns out that we can't just abort the DMA; we have to let it stop at the end of a command, and then wait for the S7 bit to be set before turning off the DBDMA controller. Doing that for playback doesn't seem to be necessary, but doesn't hurt either.

We use the technique used by the Darwin driver: make each transfer command branch to a stop command if the S0 status bit is set. Thus we can ask the DMA controller to stop at the end of the current command by setting S0.

The interrupt routine now looks at and clears the status word of the DBDMA command ring. This is necessary so it can know when the DBDMA controller has seen that S0 is set, and so when it should look for the DBDMA controller being stopped and S7 being set. This also ended up simplifying the calculation in i2sbus_pcm_pointer.

Tested on a 15 inch albook.

I modified this patch and added the suspend/resume bits to it to get my powermac into a decent state when playing sound across suspend to disk that has a different bitrate from what the firmware programs the hardware to.

Quoted 4 lines

I also added the SNDRV_PCM_INFO_JOINT_DUPLEX flag because it seemed the right thing to do and I was looking at the info stuff.

Thanks, applied to ALSA tree now. Should be merged to 2.6.21.

Takashi