diff options
author | Jaroslav Kysela <perex@perex.cz> | 2010-01-27 12:10:13 -0500 |
---|---|---|
committer | Jaroslav Kysela <perex@perex.cz> | 2010-01-27 12:17:27 -0500 |
commit | 7910b4a1db63fefc3d291853d33c34c5b6352e8e (patch) | |
tree | e68f8581906f00f4cdefa2f1017555dd9fdb4d74 /sound/core | |
parent | e7636925789b042ff9d98c51d48392e8c5549480 (diff) |
ALSA: pcm_native - fix runtime->boundary calculation
The code in pcm_lib updating runtime->hw_ptr_interrupt expects
that runtime->boundary is divisible with runtime->period_size.
Thanks are going to Clemens Ladisch for the notice.
Fix the runtime->boundary calculation using buffer_size * period_size
as base and find a least common multiple for 32bit platforms when
the expression might overflow.
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
Diffstat (limited to 'sound/core')
-rw-r--r-- | sound/core/pcm_native.c | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c index 7a002db512b4..9cbaf90d3d88 100644 --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c | |||
@@ -27,6 +27,7 @@ | |||
27 | #include <linux/pm_qos_params.h> | 27 | #include <linux/pm_qos_params.h> |
28 | #include <linux/uio.h> | 28 | #include <linux/uio.h> |
29 | #include <linux/dma-mapping.h> | 29 | #include <linux/dma-mapping.h> |
30 | #include <linux/math64.h> | ||
30 | #include <sound/core.h> | 31 | #include <sound/core.h> |
31 | #include <sound/control.h> | 32 | #include <sound/control.h> |
32 | #include <sound/info.h> | 33 | #include <sound/info.h> |
@@ -366,6 +367,38 @@ static int period_to_usecs(struct snd_pcm_runtime *runtime) | |||
366 | return usecs; | 367 | return usecs; |
367 | } | 368 | } |
368 | 369 | ||
370 | static int calc_boundary(struct snd_pcm_runtime *runtime) | ||
371 | { | ||
372 | u_int64_t boundary; | ||
373 | |||
374 | boundary = (u_int64_t)runtime->buffer_size * | ||
375 | (u_int64_t)runtime->period_size; | ||
376 | #if BITS_PER_LONG < 64 | ||
377 | /* try to find lowest common multiple for buffer and period */ | ||
378 | if (boundary > LONG_MAX - runtime->buffer_size) { | ||
379 | u_int32_t remainder = -1; | ||
380 | u_int32_t divident = runtime->buffer_size; | ||
381 | u_int32_t divisor = runtime->period_size; | ||
382 | while (remainder) { | ||
383 | remainder = divident % divisor; | ||
384 | if (remainder) { | ||
385 | divident = divisor; | ||
386 | divisor = remainder; | ||
387 | } | ||
388 | } | ||
389 | boundary = div_u64(boundary, divisor); | ||
390 | if (boundary > LONG_MAX - runtime->buffer_size) | ||
391 | return -ERANGE; | ||
392 | } | ||
393 | #endif | ||
394 | if (boundary == 0) | ||
395 | return -ERANGE; | ||
396 | runtime->boundary = boundary; | ||
397 | while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size) | ||
398 | runtime->boundary *= 2; | ||
399 | return 0; | ||
400 | } | ||
401 | |||
369 | static int snd_pcm_hw_params(struct snd_pcm_substream *substream, | 402 | static int snd_pcm_hw_params(struct snd_pcm_substream *substream, |
370 | struct snd_pcm_hw_params *params) | 403 | struct snd_pcm_hw_params *params) |
371 | { | 404 | { |
@@ -441,9 +474,9 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream, | |||
441 | runtime->stop_threshold = runtime->buffer_size; | 474 | runtime->stop_threshold = runtime->buffer_size; |
442 | runtime->silence_threshold = 0; | 475 | runtime->silence_threshold = 0; |
443 | runtime->silence_size = 0; | 476 | runtime->silence_size = 0; |
444 | runtime->boundary = runtime->buffer_size; | 477 | err = calc_boundary(runtime); |
445 | while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size) | 478 | if (err < 0) |
446 | runtime->boundary *= 2; | 479 | goto _error; |
447 | 480 | ||
448 | snd_pcm_timer_resolution_change(substream); | 481 | snd_pcm_timer_resolution_change(substream); |
449 | runtime->status->state = SNDRV_PCM_STATE_SETUP; | 482 | runtime->status->state = SNDRV_PCM_STATE_SETUP; |