es1938 - patch trying to improve capture hw pointer reads
Hello,
with the Solo1 (es1938) I got a lot of xrun's during capture on my machine. Tracing that down it seems to be comming from reading ocassionaly bad hw pointers from the chip. Appended is a patch against linux-2.6.23.12 which uses more checking to avoid that false pointer reads.
Failed reads are giving back the last good value read instead of spinning in a tight loop, which seems more appropriate to me in an interrupt. I think I saw this trick used in another driver.
On my machine xruns seems to be gone with that patch.
If anybody is interested, mmaped capture seems also to be doable with some tricks, still testing this...
Greetings
Hermann
Signature · 4 lines
Netzwerkadministration/Zentrale Dienste, Interdiziplinaeres Zentrum fuer wissenschaftliches Rechnen der Universitaet Heidelberg IWR; INF 368; 69120 Heidelberg; Tel: (06221)54-8236 Fax: -5224 Email: [email protected]
sound/pci/es1938.c renamed from sound/pci/es1938.c.trydma +28 -2
@@ -227,6 +227,8 @@unsigned int dma2_start;unsigned int dma1_shift;unsigned int dma2_shift; +void * last_capture_dmaaddr; +unsigned int active;spinlock_t reg_lock;
@@ -775,14 +777,38 @@struct es1938 *chip =snd_pcm_substream_chip (substream);size_t ptr;size_t old, new; -#if 1 +#if 0 /* This stuff is *needed*, don't ask why - AB */ old =inw (SLDM_REG (chip,DMACOUNT ));while ((new =inw (SLDM_REG (chip,DMACOUNT ))) != old) old = new; ptr = chip->dma1_size -1 - new;#else - ptr =inl (SLDM_REG (chip,DMAADDR )) - chip->dma1_start; +/* during the incrementing of dma counters the DMA register reads sometimes returns garbage. +To ensure a valid hw pointer, the following checks which should be +very unlikely to fail are used: +- is the current DMA address in the valid DMA range ? +- is the sum of DMA address and DMA counter pointing to the last DMA byte ? +One can argue this could differ by one byte depending on which register is +updated first, so the implemention below allows for that. +*/ +size_t stat,count; +unsigned int diff; +struct snd_pcm_runtime *runtime; + runtime = substream->runtime; + ptr =inl (SLDM_REG (chip,DMAADDR )); + count=inw (SLDM_REG (chip,DMACOUNT )); + diff=chip->dma1_start+chip->dma1_size-ptr-count; + +/* printk("Es1938debug - capture_pointer %x,diff=%x,fb=%x DMA count: %x, base: %x, status: %x\n", +ptr,diff,((unsigned char*)(runtime->dma_area)-1)[0],count, inl(SLDM_REG(chip, DMAADDR)), inl(SLDM_REG(chip, DMASTATUS))); +*/ +if (diff>3 || ptr < chip->dma1_start + || ptr >= chip->dma1_start+chip->dma1_size) + ptr=chip->last_capture_dmaaddr;/* bad, use last saved instead */ +else chip->last_capture_dmaaddr=ptr;/* good, remember it */ + + ptr -= chip->dma1_start;#endif return ptr >> chip->dma1_shift; }
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
At Wed, 23 Jan 2008 01:35:12 +0100,
Quoted 7 lines · Hermann Lauer
Hello,
with the Solo1 (es1938) I got a lot of xrun's during capture on my machine. Tracing that down it seems to be comming from reading ocassionaly bad hw pointers from the chip. Appended is a patch against linux-2.6.23.12 which uses more checking to avoid that false pointer reads.
Quoted 5 lines
Failed reads are giving back the last good value read instead of spinning in a tight loop, which seems more appropriate to me in an interrupt. I think I saw this trick used in another driver.
On my machine xruns seems to be gone with that patch.
Thanks for the patch.
The change look OK to me. Another idea would be to use DMAADDR only when DMACOUNT goes mad. But the back-off to the last pointer is propbably a safer solution.
The patch is, however, unable to apply as is because of coding style problems. Try $LINUXKERNEL/scripts/checkpatch.pl, fix what are suggested there, and repost the patch together with your sign-off.
Takashi
On Thu, Jan 24, 2008 at 12:47:46PM +0100, Takashi Iwai wrote:
...
Quoted 3 lines
Thanks for the patch.
The change look OK to me. Another idea would be to use DMAADDR only when DMACOUNT goes mad.
The problem is to decide when you have read garbage from DMACOUNT. DMAADDR is better in the first place, because you know the range in 24 bit which is valid (DMACOUNT is 16bit only). But my patch tries to make the failure probability much more lower by reading and comparing both, so from which one you calculate the pointer at the end is only a cosmetic issue.
Quoted 2 lines
But the back-off to the last pointer is
propbably a safer solution.
I'm glad to hear the alsa design allow's this trick - or is that even the recommeded way to deal with when the hardware pointer could be temporarily not determined ?
Quoted 3 lines
The patch is, however, unable to apply as is because of coding style problems. Try
$LINUXKERNEL/scripts/checkpatch.pl, fix what are suggested there, and repost the patch together with your sign-off.
Will do that when I digged trough the details (have to learn about signing).
Hermann
Signature · 4 lines
Netzwerkadministration/Zentrale Dienste, Interdiziplinaeres Zentrum fuer wissenschaftliches Rechnen der Universitaet Heidelberg IWR; INF 368; 69120 Heidelberg; Tel: (06221)54-8236 Fax: -5224 Email: [email protected]
At Thu, 24 Jan 2008 16:21:02 +0100,
Quoted 7 lines · Hermann Lauer
On Thu, Jan 24, 2008 at 12:47:46PM +0100, Takashi Iwai wrote:
...Quoted 3 lines
Thanks for the patch.
The change look OK to me. Another idea would be to use DMAADDR only when DMACOUNT goes mad.
Quoted 6 lines
The problem is to decide when you have read garbage from DMACOUNT. DMAADDR is better in the first place, because you know the range in 24 bit which is valid (DMACOUNT is 16bit only). But my patch tries to make the failure probability much more lower by reading and comparing both, so from which one you calculate the pointer at the end is only a cosmetic issue.
Then a question arises - why DMAADDR isn't used as the primary source at all? Is it less stable than DMACOUNT in some cases?
Quoted 6 lines
Quoted 2 lines
But the back-off to the last pointer is
propbably a safer solution.I'm glad to hear the alsa design allow's this trick - or is that even the recommeded way to deal with when the hardware pointer could be temporarily not determined ?
Well, not really. In theory, it works even if the driver doesn't provide the accurate position. But, the driver must provide the position in the current period, i.e. the accuracy (latency) must be smaller than the period size. Thus, it's more safer to keep tracking the current period index (e.g. incrementing at each period-update irq) and check whether the last ptr is over it.
For example, suppose the buffer = 256 x 4 periods. The last ptr is recorded at 256. Now, at the real position 512, an irq is issued. The irq handler calls snd_pcm_period_elapsed(), which calls the pointer callback in the PCM core. If the read error occurs at this moment, we can't back up to the last position 256. We are supposed to be at position 512 or more.
Quoted 5 lines
Quoted 3 lines
The patch is, however, unable to apply as is because of coding style problems. Try
$LINUXKERNEL/scripts/checkpatch.pl, fix what are suggested there, and repost the patch together with your sign-off.Will do that when I digged trough the details (have to learn about signing).
See $LINUX/Documentation/SubmittingPatches.
Takashi
Quoted 16 lines · Takashi Iwai
At Thu, 24 Jan 2008 16:21:02 +0100,
Quoted 7 lines · Hermann Lauer
On Thu, Jan 24, 2008 at 12:47:46PM +0100, Takashi Iwai wrote:
...Quoted 3 lines
Thanks for the patch.
The change look OK to me. Another idea would be to use DMAADDR only when DMACOUNT goes mad.Quoted 6 lines
The problem is to decide when you have read garbage from DMACOUNT. DMAADDR is better in the first place, because you know the range in 24 bit which is valid (DMACOUNT is 16bit only). But my patch tries to make the failure probability much more lower by reading and comparing both, so from which one you calculate the pointer at the end is only a cosmetic issue.
Quoted 2 lines
Then a question arises - why DMAADDR isn't used as the primary source at all? Is it less stable than DMACOUNT in some cases?
I don't know and the current implementation contains only a "don't ask why - AB" comment.
To reiterate: My proposed patch is about minimising the chance that a wrong hw pointer value is given back due to garbage read from the chip. If you are interested, I can send you debugging output which shows that both DMAADDR and DMACOUNT are read out as bogus values from time to time - one or the another, or both. I tried some status flags on the chip, but alway found cases where it's
not clear if and which one of the two is correct.
So reading both of them and comparing them seems to be the best way for me to detect that there has been a readout error. Which one to give back is without having more information about the chip internals in my opinion only a matter of taste. The proposed patch allows a jitter of one byte there, but that could be set to zero at the cost of a slightly increased failure rate.
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines
But the back-off to the last pointer is
propbably a safer solution.I'm glad to hear the alsa design allow's this trick - or is that even the recommeded way to deal with when the hardware pointer could be temporarily not determined ?
Quoted 6 lines
Well, not really. In theory, it works even if the driver doesn't provide the accurate position. But, the driver must provide the position in the current period, i.e. the accuracy (latency) must be smaller than the period size. Thus, it's more safer to keep tracking the current period index (e.g. incrementing at each period-update irq) and check whether the last ptr is over it.
Fortunately I have not seen any signs of read errors during the interrupt processing. Probably this would change if the interrupt processing latency is high.
Quoted 6 lines
For example, suppose the buffer = 256 x 4 periods. The last ptr is recorded at 256. Now, at the real position 512, an irq is issued. The irq handler calls
snd_pcm_period_elapsed(), which calls the pointer callback in the PCM core. If the read error occurs at this moment, we can't back up to the last position 256. We are supposed to be at position 512 or more.
Just to make that clear to me: If the irq handler calls snd_pcm_period_elapsed() in your example, the alsa middle layer will treat the first 256 byte as written completely by the chip even
if the pointer callback will say there is still one frame not stored for that period.
(I see the name of the function answering my qestion, but are curious how the middle layer implementation behaves in this situation)
That can play a role with the handling of the off-by-one bug of the chip.
Hermann
Signature · 4 lines
Netzwerkadministration/Zentrale Dienste, Interdiziplinaeres Zentrum fuer wissenschaftliches Rechnen der Universitaet Heidelberg IWR; INF 368; 69120 Heidelberg; Tel: (06221)54-8236 Fax: -5224 Email: [email protected]
At Fri, 25 Jan 2008 01:25:14 +0100,
Quoted 24 lines · Hermann Lauer
Quoted 16 lines · Takashi Iwai
At Thu, 24 Jan 2008 16:21:02 +0100,
Quoted 7 lines · Hermann Lauer
On Thu, Jan 24, 2008 at 12:47:46PM +0100, Takashi Iwai wrote:
...Quoted 3 lines
Thanks for the patch.
The change look OK to me. Another idea would be to use DMAADDR only when DMACOUNT goes mad.Quoted 6 lines
The problem is to decide when you have read garbage from DMACOUNT. DMAADDR is better in the first place, because you know the range in 24 bit which is valid (DMACOUNT is 16bit only). But my patch tries to make the failure probability much more lower by reading and comparing both, so from which one you calculate the pointer at the end is only a cosmetic issue.
Quoted 2 lines
Then a question arises - why DMAADDR isn't used as the primary source at all? Is it less stable than DMACOUNT in some cases?
I don't know and the current implementation contains only a "don't ask why - AB" comment.
Quoted 7 lines
To reiterate: My proposed patch is about minimising the chance that a wrong hw pointer value is given back due to garbage read from the chip. If you are interested, I can send you debugging output which shows that both DMAADDR and DMACOUNT are read out as bogus values from time to time - one or the another, or both. I tried some status flags on the chip, but alway found cases where it's
not clear if and which one of the two is correct.
Yeah, it would be helpful to understand the problem more correctly.
Quoted 7 lines
So reading both of them and comparing them seems to be the best way for me to detect that there has been a readout error. Which one to give back is without having more information about the chip internals in my opinion only a matter of taste. The proposed patch allows a jitter of one byte there, but that could be set to zero at the cost of a slightly increased failure rate.
BTW, don't get me wrong - I'm not against your proposal. The back-off is a safe option, as already mentioned.
Quoted 14 lines
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines
But the back-off to the last pointer is
propbably a safer solution.I'm glad to hear the alsa design allow's this trick - or is that even the recommeded way to deal with when the hardware pointer could be temporarily not determined ?
Quoted 6 lines
Well, not really. In theory, it works even if the driver doesn't provide the accurate position. But, the driver must provide the position in the current period, i.e. the accuracy (latency) must be smaller than the period size. Thus, it's more safer to keep tracking the current period index (e.g. incrementing at each period-update irq) and check whether the last ptr is over it.
Quoted 3 lines
Fortunately I have not seen any signs of read errors during the interrupt processing. Probably this would change if the interrupt processing latency is high.
Probably. In the other case, if some other device blocks the shared irq line, then the irq handle gets sloppy. In such a case, the sound driver gets an irq for the update of two periods instead of one.
Quoted 7 lines
Quoted 6 lines
For example, suppose the buffer = 256 x 4 periods. The last ptr is recorded at 256. Now, at the real position 512, an irq is issued. The irq handler calls
snd_pcm_period_elapsed(), which calls the pointer callback in the PCM core. If the read error occurs at this moment, we can't back up to the last position 256. We are supposed to be at position 512 or more.
Quoted 7 lines
Just to make that clear to me: If the irq handler calls
snd_pcm_period_elapsed()in your example, the alsa middle layer will treat the first 256 byte as written completely by the chip even
if the pointer callback will say there is still one frame not stored for that period.
(I see the name of the function answering my qestion, but are curious how the middle layer implementation behaves in this situation)
Yes. The return value from the pointer callback is the most trusted information the PCM core receives.
Quoted 1 line
That can play a role with the handling of the off-by-one bug of the chip.
What is the problem exactly?
Thanks,
Takashi
Quoted 14 lines · Takashi Iwai
Quoted 6 lines
Quoted 2 lines
Then a question arises - why DMAADDR isn't used as the primary source at all? Is it less stable than DMACOUNT in some cases?
I don't know and the current implementation contains only a "don't ask why - AB" comment.
Quoted 7 lines
To reiterate: My proposed patch is about minimising the chance that a wrong hw pointer value is given back due to garbage read from the chip. If you are interested, I can send you debugging output which shows that both DMAADDR and DMACOUNT are read out as bogus values from time to time - one or the another, or both. I tried some status flags on the chip, but alway found cases where it's
not clear if and which one of the two is correct.
Quoted 1 line
Yeah, it would be helpful to understand the problem more correctly.
Will put together some of the log files next week.
Up to what size could/should I cc to the list without upsetting people ? I guess compressing them will yield only a few KBytes.
Quoted 10 lines
Quoted 7 lines
So reading both of them and comparing them seems to be the best way for me to detect that there has been a readout error. Which one to give back is without having more information about the chip internals in my opinion only a matter of taste. The proposed patch allows a jitter of one byte there, but that could be set to zero at the cost of a slightly increased failure rate.
BTW, don't get me wrong - I'm not against your proposal. The back-off is a safe option, as already mentioned.
Good to know.
Quoted 8 lines
Quoted 7 lines
Just to make that clear to me: If the irq handler calls
snd_pcm_period_elapsed()in your example, the alsa middle layer will treat the first 256 byte as written completely by the chip even
if the pointer callback will say there is still one frame not stored for that period.
(I see the name of the function answering my qestion, but are curious how the middle layer implementation behaves in this situation)
Quoted 6 lines
Yes. The return value from the pointer callback is the most trusted information the PCM core receives.
Quoted 1 line
That can play a role with the handling of the off-by-one bug of the chip.
What is the problem exactly?
If I interpreted my tests output correctly, the last byte of a period is written by the DMA engine after the interrupt occurs. So the hw pointer probably has to be decremented by one and is then pointing to the last frame (Or even that before: Has the hw pointer to point at the frame which is written at the moment or the one which is guaranteed to be written completely by the hardware ?)
Have a nice weekend,
Hermann
Signature · 4 lines
Netzwerkadministration/Zentrale Dienste, Interdiziplinaeres Zentrum fuer wissenschaftliches Rechnen der Universitaet Heidelberg IWR; INF 368; 69120 Heidelberg; Tel: (06221)54-8236 Fax: -5224 Email: [email protected]
At Fri, 25 Jan 2008 17:07:27 +0100,
Quoted 16 lines · Hermann Lauer
Quoted 14 lines · Takashi Iwai
Quoted 13 lines
Quoted 2 lines
Then a question arises - why DMAADDR isn't used as the primary source at all? Is it less stable than DMACOUNT in some cases?
I don't know and the current implementation contains only a "don't ask why - AB" comment.
To reiterate: My proposed patch is about minimising the chance that a wrong hw pointer value is given back due to garbage read from the chip. If you are interested, I can send you debugging output which shows that both DMAADDR and DMACOUNT are read out as bogus values from time to time - one or the another, or both. I tried some status flags on the chip, but alway found cases where it's
not clear if and which one of the two is correct.
Quoted 5 lines
Quoted 1 line
Yeah, it would be helpful to understand the problem more correctly.
Will put together some of the log files next week.
Up to what size could/should I cc to the list without upsetting people ? I guess compressing them will yield only a few KBytes.
Not sure about the size limit, but it'd be better to attach a compressed file. Or just cut out only the interesting part.
Quoted 10 lines
Quoted 3 lines
Quoted 1 line
That can play a role with the handling of the off-by-one bug of the chip.
What is the problem exactly?
If I interpreted my tests output correctly, the last byte of a period is written by the DMA engine after the interrupt occurs. So the hw pointer probably has to be decremented by one and is then pointing to the last frame (Or even that before: Has the hw pointer to point at the frame which is written at the moment or the one which is guaranteed to be written completely by the hardware ?)
This sounds a bit odd. Isn't it rather the setup of the hardware parameter wrong? I mean, the count calculation in *_prepare() can be the size - 1?
Takashi
Quoted 10 lines · Takashi Iwai
Quoted 6 lines
If I interpreted my tests output correctly, the last byte of a period is written by the DMA engine after the interrupt occurs. So the hw pointer probably has to be decremented by one and is then pointing to the last frame (Or even that before: Has the hw pointer to point at the frame which is written at the moment or the one which is guaranteed to be written completely by the hardware ?)
This sounds a bit odd. Isn't it rather the setup of the hardware parameter wrong? I mean, the count calculation in
*_prepare()can be the size - 1?
At least according to the datasheet the size calculation is correct. Could you please tell me where the hw pointer exactly should point to ?
Thanks,
Hermann
Signature · 4 lines
Netzwerkadministration/Zentrale Dienste, Interdiziplinaeres Zentrum fuer wissenschaftliches Rechnen der Universitaet Heidelberg IWR; INF 368; 69120 Heidelberg; Tel: (06221)54-8236 Fax: -5224 Email: [email protected]
At Fri, 25 Jan 2008 17:29:28 +0100,
Quoted 10 lines · Hermann Lauer
Quoted 7 lines · Takashi Iwai
Quoted 6 lines
If I interpreted my tests output correctly, the last byte of a period is written by the DMA engine after the interrupt occurs. So the hw pointer probably has to be decremented by one and is then pointing to the last frame (Or even that before: Has the hw pointer to point at the frame which is written at the moment or the one which is guaranteed to be written completely by the hardware ?)
Quoted 6 lines
Quoted 3 lines
This sounds a bit odd. Isn't it rather the setup of the hardware parameter wrong? I mean, the count calculation in
*_prepare()can be the size - 1?At least according to the datasheet the size calculation is correct. Could you please tell me where the hw pointer exactly should point to ?
The hwptr is the position of the current DMA. The data below that point must have been already processed. That's why hwptr returned in snd_pcm_period_elapsed() must be on the period boundary or above.
OTOH, it doesn't matter much whether the data above hwptr has been processed or not, e.g. a sloppy pointer callback.
Takashi
es1938 - experimental patch allowing mmaped capture on Solo1
Hello,
appended is an experimental patch agains linux-2.6.23.12 which enables mmaped access during capture on the es1938 (Solo1).
Main obstacle is (as documented in the current driver) that the dma engine put bytes from the adc with an offset +1 to ram.
So the patch did the following:
- allocate on additional page in front of the capture dma buffer, with mmaping pointing to the original buffer (i.e. with one page offset into the dma area)
- let the dma engine start from the last byte of that extra page, so that all bytes are put aligned to the real dma area (besides the last byte)
- the last byte the dma engine of the chip put at the first location, i.e. in the last byte of the extra front dma page.
- This byte has to be copied at the right time to the end of the mmaped area, which is done in the hw_pointer callback.
In a discussion with Takashi some of the pointer callback issues are already discussed (that patch is included btw.), and from the log send there (lock at the fb values, which is the fist byte of dma area) you can see that indeed the last byte is transferred after
the interrupt indicating reaching the end of the dma buffer came in.
Quoted 3 lines · Takashi Iwai
The hwptr is the position of the current DMA. The data below that point must have been already processed. That's why hwptr returned in
snd_pcm_period_elapsed()must be on the period boundary or above.
The patched driver did not fullfill this, OTOH I have made first recordings which seems to be ok (even with the debug printk in the hw_pointer callback).
Have fun,
Hermann
Signature · 4 lines
Netzwerkadministration/Zentrale Dienste, Interdiziplinaeres Zentrum fuer wissenschaftliches Rechnen der Universitaet Heidelberg IWR; INF 368; 69120 Heidelberg; Tel: (06221)54-8236 Fax: -5224 Email: [email protected]
pci/es1938.c renamed from pci/es1938.c.trydma +62 -8
@@ -227,6 +227,7 @@ struct es1938 {unsigned int dma2_start;unsigned int dma1_shift;unsigned int dma2_shift; +unsigned int last_capture_dmaaddr;unsigned int active;spinlock_t reg_lock;
@@ -529,6 +530,7 @@ static void snd_es1938_capture_setdma(stoutb (1 ,SLDM_REG (chip,DMAMASK ));outb (0x14 ,SLDM_REG (chip,DMAMODE ));outl (chip->dma1_start,SLDM_REG (chip,DMAADDR )); + chip->last_capture_dmaaddr = chip->dma1_start;outw (chip->dma1_size -1 ,SLDM_REG (chip,DMACOUNT ));/* 3. Unmask DMA */ outb (0 ,SLDM_REG (chip,DMAMASK ));
@@ -640,7 +642,7 @@ static int snd_es1938_capture_prepare(stunsigned int count =snd_pcm_lib_period_bytes (substream); chip->dma1_size = size; - chip->dma1_start = runtime->dma_addr; + chip->dma1_start = runtime->dma_addr +PAGE_SIZE -1 ; mono = (runtime->channels >1 ) ?0 :1 ; is8 =snd_pcm_format_width (runtime->format) ==16 ?0 :1 ;
@@ -770,19 +772,56 @@ static int snd_es1938_playback_prepare(sreturn -EINVAL ; } +/* during the incrementing of dma counters the DMA register reads sometimes +returns garbage. To ensure a valid hw pointer, the following checks which +should be very unlikely to fail are used: +- is the current DMA address in the valid DMA range ? +- is the sum of DMA address and DMA counter pointing to the last DMA byte ? +One can argue this could differ by one byte depending on which register is +updated first, so the implementation below allows for that. +*/ static snd_pcm_uframes_t snd_es1938_capture_pointer (struct snd_pcm_substream *substream) {struct es1938 *chip =snd_pcm_substream_chip (substream);size_t ptr; +#if 0 size_t old, new; -#if 1 /* This stuff is *needed*, don't ask why - AB */ old =inw (SLDM_REG (chip,DMACOUNT ));while ((new =inw (SLDM_REG (chip,DMACOUNT ))) != old) old = new; ptr = chip->dma1_size -1 - new;#else - ptr =inl (SLDM_REG (chip,DMAADDR )) - chip->dma1_start; +size_t count; +unsigned int diff; +struct snd_pcm_runtime *runtime; + runtime = substream->runtime; + + ptr =inl (SLDM_REG (chip,DMAADDR )); + count =inw (SLDM_REG (chip,DMACOUNT )); + diff = chip->dma1_start + chip->dma1_size - ptr - count; + +printk ("Es1938debug - capture_pointer last=%x,diff=%x,fb=%x,lb=%x DMA count: %x, base: %x\n" , + chip->last_capture_dmaaddr,diff,((unsigned char *)(runtime->dma_area)-1 )[0 ], + ((unsigned char *)(runtime->dma_area))[chip->dma1_size-1 ], count,ptr); + +if (diff >3 || ptr < chip->dma1_start + || ptr >= chip->dma1_start+chip->dma1_size) + ptr = chip->last_capture_dmaaddr;/* bad, use last saved */ +else + chip->last_capture_dmaaddr = ptr;/* good, remember it */ + + ptr -= chip->dma1_start; + +if (ptr) ptr-=1 ; +else ptr=chip->dma1_size-1 ; + +if (ptr && ptr<=0x2000 ) { +struct snd_pcm_runtime *runtime; + runtime = substream->runtime; + ((unsigned char *)(runtime->dma_area))[chip->dma1_size-1 ]= + ((unsigned char *)(runtime->dma_area)-1 )[0 ]; + }#endif return ptr >> chip->dma1_shift; }
@@ -865,6 +904,21 @@ static int snd_es1938_pcm_hw_params(strureturn 0 ; } +static int snd_es1938_pcm_hw_params_capture (struct snd_pcm_substream *substream, +struct snd_pcm_hw_params *hw_params) + +{ +int err; + +if ((err =snd_pcm_lib_malloc_pages (substream,params_buffer_bytes (hw_params)+PAGE_SIZE )) <0 ) +return err; + +struct snd_pcm_runtime *runtime = substream->runtime; + runtime->dma_area = runtime->dma_buffer_p->area+PAGE_SIZE ; + +return 0 ; +} +static int snd_es1938_pcm_hw_free (struct snd_pcm_substream *substream) {return snd_pcm_lib_free_pages (substream);
@@ -875,8 +929,8 @@ static int snd_es1938_pcm_hw_free(struct* ----------------------------------------------------------------------*/ static struct snd_pcm_hardware snd_es1938_capture = { - .info = (SNDRV_PCM_INFO_INTERLEAVED | -SNDRV_PCM_INFO_BLOCK_TRANSFER ), + .info = (SNDRV_PCM_INFO_MMAP |SNDRV_PCM_INFO_INTERLEAVED | +SNDRV_PCM_INFO_BLOCK_TRANSFER |SNDRV_PCM_INFO_MMAP_VALID ), .formats = (SNDRV_PCM_FMTBIT_U8 |SNDRV_PCM_FMTBIT_S16_LE |SNDRV_PCM_FMTBIT_S8 |SNDRV_PCM_FMTBIT_U16_LE ), .rates =SNDRV_PCM_RATE_CONTINUOUS |SNDRV_PCM_RATE_8000_48000 ,
@@ -996,12 +1050,12 @@ static struct snd_pcm_ops snd_es1938_cap .open = snd_es1938_capture_open, .close = snd_es1938_capture_close, .ioctl = snd_pcm_lib_ioctl, - .hw_params = snd_es1938_pcm_hw_params, + .hw_params = snd_es1938_pcm_hw_params_capture, .hw_free = snd_es1938_pcm_hw_free, .prepare = snd_es1938_capture_prepare, .trigger = snd_es1938_capture_trigger, .pointer = snd_es1938_capture_pointer, - .copy = snd_es1938_capture_copy, +/* .copy = snd_es1938_capture_copy,*/ };static int __devinit snd_es1938_new_pcm (struct es1938 *chip,int device)
@@ -1019,7 +1073,7 @@ static int __devinit snd_es1938_new_pcm(strcpy (pcm->name,"ESS Solo-1" );snd_pcm_lib_preallocate_pages_for_all (pcm,SNDRV_DMA_TYPE_DEV , -snd_dma_pci_data (chip->pci),64 *1024 ,64 *1024 ); +snd_dma_pci_data (chip->pci),64 *1024 +PAGE_SIZE ,64 *1024 +PAGE_SIZE ); chip->pcm = pcm;return 0 ;
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
Re: es1938 - patch trying to improve capture hw pointer reads: log file
In reply to Takashi IwaiQuoted 18 lines · Takashi Iwai
At Fri, 25 Jan 2008 17:07:27 +0100,
Quoted 16 lines · Hermann Lauer
Quoted 14 lines · Takashi Iwai
Quoted 13 lines
Quoted 2 lines
Then a question arises - why DMAADDR isn't used as the primary source at all? Is it less stable than DMACOUNT in some cases?
I don't know and the current implementation contains only a "don't ask why - AB" comment.
To reiterate: My proposed patch is about minimising the chance that a wrong hw pointer value is given back due to garbage read from the chip. If you are interested, I can send you debugging output which shows that both DMAADDR and DMACOUNT are read out as bogus values from time to time - one or the another, or both. I tried some status flags on the chip, but alway found cases where it's
not clear if and which one of the two is correct.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Yeah, it would be helpful to understand the problem more correctly.
Will put together some of the log files next week.
Up to what size could/should I cc to the list without upsetting people ? I guess compressing them will yield only a few KBytes.Not sure about the size limit, but it'd be better to attach a compressed file. Or just cut out only the interesting part.
Appended is a logfile part. The interresting parts are the diff vlaues (1 if DMACOUNT and ADDR are read correct), DMA count, addr and probably status. Status is 0xf when the DMA engine resets the DMAADDR to it's starting value, 0 in all other normal cases.
DMAADDR is reread for the debug output from the chip, so it's sometimes not the value used in the diff calculation.
Hermann
Signature · 4 lines
Netzwerkadministration/Zentrale Dienste, Interdiziplinaeres Zentrum fuer wissenschaftliches Rechnen der Universitaet Heidelberg IWR; INF 368; 69120 Heidelberg; Tel: (06221)54-8236 Fax: -5224 Email: [email protected]
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
Quoted 7 lines · Takashi Iwai
Quoted 5 lines
Quoted 3 lines
The patch is, however, unable to apply as is because of coding style problems. Try
$LINUXKERNEL/scripts/checkpatch.pl, fix what are suggested there, and repost the patch together with your sign-off.Will do that when I digged trough the details (have to learn about signing).
See $LINUX/Documentation/SubmittingPatches.
Appended is a cleaned up version of the patch. checkpatch.pl is now only mucking about the '#if 0' commented out old branch. Should this be removed or the if-else parts exchanged, which probably will calm down checkpatch.pl ?
Have to check compilation, then I will send a signed version.
Hermann
Signature · 4 lines
Netzwerkadministration/Zentrale Dienste, Interdiziplinaeres Zentrum fuer wissenschaftliches Rechnen der Universitaet Heidelberg IWR; INF 368; 69120 Heidelberg; Tel: (06221)54-8236 Fax: -5224 Email: [email protected]
sound/pci/es1938.c renamed from sound/pci/es1938.c.trydma +27 -2
@@ -227,6 +227,8 @@unsigned int dma2_start;unsigned int dma1_shift;unsigned int dma2_shift; +void *last_capture_dmaaddr; +unsigned int active;spinlock_t reg_lock;
@@ -770,19 +772,42 @@return -EINVAL ; } +/* during the incrementing of dma counters the DMA register reads sometimes +returns garbage. To ensure a valid hw pointer, the following checks which +should be very unlikely to fail are used: +- is the current DMA address in the valid DMA range ? +- is the sum of DMA address and DMA counter pointing to the last DMA byte ? +One can argue this could differ by one byte depending on which register is +updated first, so the implemention below allows for that. +*/ static snd_pcm_uframes_t snd_es1938_capture_pointer (struct snd_pcm_substream *substream) {struct es1938 *chip =snd_pcm_substream_chip (substream);size_t ptr; +#if 0 size_t old, new; -#if 1 /* This stuff is *needed*, don't ask why - AB */ old =inw (SLDM_REG (chip,DMACOUNT ));while ((new =inw (SLDM_REG (chip,DMACOUNT ))) != old) old = new; ptr = chip->dma1_size -1 - new;#else - ptr =inl (SLDM_REG (chip,DMAADDR )) - chip->dma1_start; +size_t stat, count; +unsigned int diff; +struct snd_pcm_runtime *runtime; + + runtime = substream->runtime; + ptr =inl (SLDM_REG (chip,DMAADDR )); + count =inw (SLDM_REG (chip,DMACOUNT )); + diff = chip->dma1_start+chip->dma1_size-ptr-count; + +if (diff >3 || ptr < chip->dma1_start + || ptr >= chip->dma1_start+chip->dma1_size) + ptr = chip->last_capture_dmaaddr;/* bad, use last saved */ +else + chip->last_capture_dmaaddr = ptr;/* good, remember it */ + + ptr -= chip->dma1_start;#endif return ptr >> chip->dma1_shift; }
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
At Tue, 29 Jan 2008 09:31:04 +0100,
Quoted 7 lines · Hermann Lauer
Quoted 4 lines · Takashi Iwai
Quoted 4 lines
Quoted 3 lines
The patch is, however, unable to apply as is because of coding style problems. Try
$LINUXKERNEL/scripts/checkpatch.pl, fix what are suggested there, and repost the patch together with your sign-off.
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Will do that when I digged trough the details (have to learn about signing).
See $LINUX/Documentation/SubmittingPatches.
Appended is a cleaned up version of the patch. checkpatch.pl is now only mucking about the '#if 0' commented out old branch. Should this be removed or the if-else parts exchanged, which probably will calm down checkpatch.pl ?
That's fine. checkpatch.pl is no strict rule to apply.
Quoted 1 line
Have to check compilation, then I will send a signed version.
Thanks.
One missing thing is the initialization of last_capture_dmaaddr. This should be set to zero in prepare callback.
Takashi
Quoted 7 lines · Takashi Iwai
That's fine. checkpatch.pl is no strict rule to apply.
Quoted 1 line
Have to check compilation, then I will send a signed version.
Thanks.
One missing thing is the initialization oflast_capture_dmaaddr. This should be set to zero in prepare callback.
Thanks for that hint, fixed in the appended, signed version. After futher cleanups this compiles without warnings.
Hermann
Signature · 4 lines
Netzwerkadministration/Zentrale Dienste, Interdiziplinaeres Zentrum fuer wissenschaftliches Rechnen der Universitaet Heidelberg IWR; INF 368; 69120 Heidelberg; Tel: (06221)54-8236 Fax: -5224 Email: [email protected]
sound/pci/es1938.c renamed from sound/pci/es1938.c.trydma +25 -2
@@ -227,6 +227,7 @@unsigned int dma2_start;unsigned int dma1_shift;unsigned int dma2_shift; +unsigned int last_capture_dmaaddr;unsigned int active;spinlock_t reg_lock;
@@ -529,6 +530,7 @@outb (1 ,SLDM_REG (chip,DMAMASK ));outb (0x14 ,SLDM_REG (chip,DMAMODE ));outl (chip->dma1_start,SLDM_REG (chip,DMAADDR )); + chip->last_capture_dmaaddr = chip->dma1_start;outw (chip->dma1_size -1 ,SLDM_REG (chip,DMACOUNT ));/* 3. Unmask DMA */ outb (0 ,SLDM_REG (chip,DMAMASK ));
@@ -770,19 +772,40 @@return -EINVAL ; } +/* during the incrementing of dma counters the DMA register reads sometimes +returns garbage. To ensure a valid hw pointer, the following checks which +should be very unlikely to fail are used: +- is the current DMA address in the valid DMA range ? +- is the sum of DMA address and DMA counter pointing to the last DMA byte ? +One can argue this could differ by one byte depending on which register is +updated first, so the implementation below allows for that. +*/ static snd_pcm_uframes_t snd_es1938_capture_pointer (struct snd_pcm_substream *substream) {struct es1938 *chip =snd_pcm_substream_chip (substream);size_t ptr; +#if 0 size_t old, new; -#if 1 /* This stuff is *needed*, don't ask why - AB */ old =inw (SLDM_REG (chip,DMACOUNT ));while ((new =inw (SLDM_REG (chip,DMACOUNT ))) != old) old = new; ptr = chip->dma1_size -1 - new;#else - ptr =inl (SLDM_REG (chip,DMAADDR )) - chip->dma1_start; +size_t count; +unsigned int diff; + + ptr =inl (SLDM_REG (chip,DMAADDR )); + count =inw (SLDM_REG (chip,DMACOUNT )); + diff = chip->dma1_start + chip->dma1_size - ptr - count; + +if (diff >3 || ptr < chip->dma1_start + || ptr >= chip->dma1_start+chip->dma1_size) + ptr = chip->last_capture_dmaaddr;/* bad, use last saved */ +else + chip->last_capture_dmaaddr = ptr;/* good, remember it */ + + ptr -= chip->dma1_start;#endif return ptr >> chip->dma1_shift; }
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel