2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 * Abramo Bagnara <abramo@alsa-project.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/slab.h>
24 #include <linux/time.h>
25 #include <linux/math64.h>
26 #include <sound/core.h>
27 #include <sound/control.h>
28 #include <sound/info.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/timer.h>
34 * fill ring buffer with silence
35 * runtime->silence_start: starting pointer to silence area
36 * runtime->silence_filled: size filled with silence
37 * runtime->silence_threshold: threshold from application
38 * runtime->silence_size: maximal size from application
40 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
42 void snd_pcm_playback_silence(struct snd_pcm_substream
*substream
, snd_pcm_uframes_t new_hw_ptr
)
44 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
45 snd_pcm_uframes_t frames
, ofs
, transfer
;
47 if (runtime
->silence_size
< runtime
->boundary
) {
48 snd_pcm_sframes_t noise_dist
, n
;
49 if (runtime
->silence_start
!= runtime
->control
->appl_ptr
) {
50 n
= runtime
->control
->appl_ptr
- runtime
->silence_start
;
52 n
+= runtime
->boundary
;
53 if ((snd_pcm_uframes_t
)n
< runtime
->silence_filled
)
54 runtime
->silence_filled
-= n
;
56 runtime
->silence_filled
= 0;
57 runtime
->silence_start
= runtime
->control
->appl_ptr
;
59 if (runtime
->silence_filled
>= runtime
->buffer_size
)
61 noise_dist
= snd_pcm_playback_hw_avail(runtime
) + runtime
->silence_filled
;
62 if (noise_dist
>= (snd_pcm_sframes_t
) runtime
->silence_threshold
)
64 frames
= runtime
->silence_threshold
- noise_dist
;
65 if (frames
> runtime
->silence_size
)
66 frames
= runtime
->silence_size
;
68 if (new_hw_ptr
== ULONG_MAX
) { /* initialization */
69 snd_pcm_sframes_t avail
= snd_pcm_playback_hw_avail(runtime
);
70 if (avail
> runtime
->buffer_size
)
71 avail
= runtime
->buffer_size
;
72 runtime
->silence_filled
= avail
> 0 ? avail
: 0;
73 runtime
->silence_start
= (runtime
->status
->hw_ptr
+
74 runtime
->silence_filled
) %
77 ofs
= runtime
->status
->hw_ptr
;
78 frames
= new_hw_ptr
- ofs
;
79 if ((snd_pcm_sframes_t
)frames
< 0)
80 frames
+= runtime
->boundary
;
81 runtime
->silence_filled
-= frames
;
82 if ((snd_pcm_sframes_t
)runtime
->silence_filled
< 0) {
83 runtime
->silence_filled
= 0;
84 runtime
->silence_start
= new_hw_ptr
;
86 runtime
->silence_start
= ofs
;
89 frames
= runtime
->buffer_size
- runtime
->silence_filled
;
91 if (snd_BUG_ON(frames
> runtime
->buffer_size
))
95 ofs
= runtime
->silence_start
% runtime
->buffer_size
;
97 transfer
= ofs
+ frames
> runtime
->buffer_size
? runtime
->buffer_size
- ofs
: frames
;
98 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
99 runtime
->access
== SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
) {
100 if (substream
->ops
->silence
) {
102 err
= substream
->ops
->silence(substream
, -1, ofs
, transfer
);
105 char *hwbuf
= runtime
->dma_area
+ frames_to_bytes(runtime
, ofs
);
106 snd_pcm_format_set_silence(runtime
->format
, hwbuf
, transfer
* runtime
->channels
);
110 unsigned int channels
= runtime
->channels
;
111 if (substream
->ops
->silence
) {
112 for (c
= 0; c
< channels
; ++c
) {
114 err
= substream
->ops
->silence(substream
, c
, ofs
, transfer
);
118 size_t dma_csize
= runtime
->dma_bytes
/ channels
;
119 for (c
= 0; c
< channels
; ++c
) {
120 char *hwbuf
= runtime
->dma_area
+ (c
* dma_csize
) + samples_to_bytes(runtime
, ofs
);
121 snd_pcm_format_set_silence(runtime
->format
, hwbuf
, transfer
);
125 runtime
->silence_filled
+= transfer
;
131 static void pcm_debug_name(struct snd_pcm_substream
*substream
,
132 char *name
, size_t len
)
134 snprintf(name
, len
, "pcmC%dD%d%c:%d",
135 substream
->pcm
->card
->number
,
136 substream
->pcm
->device
,
137 substream
->stream
? 'c' : 'p',
141 #define XRUN_DEBUG_BASIC (1<<0)
142 #define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
143 #define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
144 #define XRUN_DEBUG_PERIODUPDATE (1<<3) /* full period update info */
145 #define XRUN_DEBUG_HWPTRUPDATE (1<<4) /* full hwptr update info */
146 #define XRUN_DEBUG_LOG (1<<5) /* show last 10 positions on err */
147 #define XRUN_DEBUG_LOGONCE (1<<6) /* do above only once */
149 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
151 #define xrun_debug(substream, mask) \
152 ((substream)->pstr->xrun_debug & (mask))
154 #define xrun_debug(substream, mask) 0
157 #define dump_stack_on_xrun(substream) do { \
158 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
162 static void xrun(struct snd_pcm_substream
*substream
)
164 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
166 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
)
167 snd_pcm_gettime(runtime
, (struct timespec
*)&runtime
->status
->tstamp
);
168 snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
169 if (xrun_debug(substream
, XRUN_DEBUG_BASIC
)) {
171 pcm_debug_name(substream
, name
, sizeof(name
));
172 snd_printd(KERN_DEBUG
"XRUN: %s\n", name
);
173 dump_stack_on_xrun(substream
);
177 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
178 #define hw_ptr_error(substream, fmt, args...) \
180 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
181 xrun_log_show(substream); \
182 if (printk_ratelimit()) { \
183 snd_printd("PCM: " fmt, ##args); \
185 dump_stack_on_xrun(substream); \
189 #define XRUN_LOG_CNT 10
191 struct hwptr_log_entry
{
192 unsigned long jiffies
;
193 snd_pcm_uframes_t pos
;
194 snd_pcm_uframes_t period_size
;
195 snd_pcm_uframes_t buffer_size
;
196 snd_pcm_uframes_t old_hw_ptr
;
197 snd_pcm_uframes_t hw_ptr_base
;
200 struct snd_pcm_hwptr_log
{
203 struct hwptr_log_entry entries
[XRUN_LOG_CNT
];
206 static void xrun_log(struct snd_pcm_substream
*substream
,
207 snd_pcm_uframes_t pos
)
209 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
210 struct snd_pcm_hwptr_log
*log
= runtime
->hwptr_log
;
211 struct hwptr_log_entry
*entry
;
214 log
= kzalloc(sizeof(*log
), GFP_ATOMIC
);
217 runtime
->hwptr_log
= log
;
219 if (xrun_debug(substream
, XRUN_DEBUG_LOGONCE
) && log
->hit
)
222 entry
= &log
->entries
[log
->idx
];
223 entry
->jiffies
= jiffies
;
225 entry
->period_size
= runtime
->period_size
;
226 entry
->buffer_size
= runtime
->buffer_size
;;
227 entry
->old_hw_ptr
= runtime
->status
->hw_ptr
;
228 entry
->hw_ptr_base
= runtime
->hw_ptr_base
;
229 log
->idx
= (log
->idx
+ 1) % XRUN_LOG_CNT
;
232 static void xrun_log_show(struct snd_pcm_substream
*substream
)
234 struct snd_pcm_hwptr_log
*log
= substream
->runtime
->hwptr_log
;
235 struct hwptr_log_entry
*entry
;
242 if (xrun_debug(substream
, XRUN_DEBUG_LOGONCE
) && log
->hit
)
244 pcm_debug_name(substream
, name
, sizeof(name
));
245 for (cnt
= 0, idx
= log
->idx
; cnt
< XRUN_LOG_CNT
; cnt
++) {
246 entry
= &log
->entries
[idx
];
247 if (entry
->period_size
== 0)
249 snd_printd("hwptr log: %s: j=%lu, pos=%ld/%ld/%ld, "
251 name
, entry
->jiffies
, (unsigned long)entry
->pos
,
252 (unsigned long)entry
->period_size
,
253 (unsigned long)entry
->buffer_size
,
254 (unsigned long)entry
->old_hw_ptr
,
255 (unsigned long)entry
->hw_ptr_base
);
262 #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
264 #define hw_ptr_error(substream, fmt, args...) do { } while (0)
265 #define xrun_log(substream, pos) do { } while (0)
266 #define xrun_log_show(substream) do { } while (0)
270 int snd_pcm_update_state(struct snd_pcm_substream
*substream
,
271 struct snd_pcm_runtime
*runtime
)
273 snd_pcm_uframes_t avail
;
275 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
276 avail
= snd_pcm_playback_avail(runtime
);
278 avail
= snd_pcm_capture_avail(runtime
);
279 if (avail
> runtime
->avail_max
)
280 runtime
->avail_max
= avail
;
281 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
282 if (avail
>= runtime
->buffer_size
) {
283 snd_pcm_drain_done(substream
);
287 if (avail
>= runtime
->stop_threshold
) {
292 if (runtime
->twake
) {
293 if (avail
>= runtime
->twake
)
294 wake_up(&runtime
->tsleep
);
295 } else if (avail
>= runtime
->control
->avail_min
)
296 wake_up(&runtime
->sleep
);
300 static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream
*substream
,
301 unsigned int in_interrupt
)
303 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
304 snd_pcm_uframes_t pos
;
305 snd_pcm_uframes_t old_hw_ptr
, new_hw_ptr
, hw_base
;
306 snd_pcm_sframes_t hdelta
, delta
;
307 unsigned long jdelta
;
309 old_hw_ptr
= runtime
->status
->hw_ptr
;
310 pos
= substream
->ops
->pointer(substream
);
311 if (pos
== SNDRV_PCM_POS_XRUN
) {
315 if (pos
>= runtime
->buffer_size
) {
316 if (printk_ratelimit()) {
318 pcm_debug_name(substream
, name
, sizeof(name
));
319 xrun_log_show(substream
);
320 snd_printd(KERN_ERR
"BUG: %s, pos = %ld, "
321 "buffer size = %ld, period size = %ld\n",
322 name
, pos
, runtime
->buffer_size
,
323 runtime
->period_size
);
327 pos
-= pos
% runtime
->min_align
;
328 if (xrun_debug(substream
, XRUN_DEBUG_LOG
))
329 xrun_log(substream
, pos
);
330 hw_base
= runtime
->hw_ptr_base
;
331 new_hw_ptr
= hw_base
+ pos
;
333 /* we know that one period was processed */
334 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
335 delta
= runtime
->hw_ptr_interrupt
+ runtime
->period_size
;
336 if (delta
> new_hw_ptr
) {
337 /* check for double acknowledged interrupts */
338 hdelta
= jiffies
- runtime
->hw_ptr_jiffies
;
339 if (hdelta
> runtime
->hw_ptr_buffer_jiffies
/2) {
340 hw_base
+= runtime
->buffer_size
;
341 if (hw_base
>= runtime
->boundary
)
343 new_hw_ptr
= hw_base
+ pos
;
348 /* new_hw_ptr might be lower than old_hw_ptr in case when */
349 /* pointer crosses the end of the ring buffer */
350 if (new_hw_ptr
< old_hw_ptr
) {
351 hw_base
+= runtime
->buffer_size
;
352 if (hw_base
>= runtime
->boundary
)
354 new_hw_ptr
= hw_base
+ pos
;
357 delta
= new_hw_ptr
- old_hw_ptr
;
359 delta
+= runtime
->boundary
;
360 if (xrun_debug(substream
, in_interrupt
?
361 XRUN_DEBUG_PERIODUPDATE
: XRUN_DEBUG_HWPTRUPDATE
)) {
363 pcm_debug_name(substream
, name
, sizeof(name
));
364 snd_printd("%s_update: %s: pos=%u/%u/%u, "
365 "hwptr=%ld/%ld/%ld/%ld\n",
366 in_interrupt
? "period" : "hwptr",
369 (unsigned int)runtime
->period_size
,
370 (unsigned int)runtime
->buffer_size
,
371 (unsigned long)delta
,
372 (unsigned long)old_hw_ptr
,
373 (unsigned long)new_hw_ptr
,
374 (unsigned long)runtime
->hw_ptr_base
);
376 /* something must be really wrong */
377 if (delta
>= runtime
->buffer_size
+ runtime
->period_size
) {
378 hw_ptr_error(substream
,
379 "Unexpected hw_pointer value %s"
380 "(stream=%i, pos=%ld, new_hw_ptr=%ld, "
382 in_interrupt
? "[Q] " : "[P]",
383 substream
->stream
, (long)pos
,
384 (long)new_hw_ptr
, (long)old_hw_ptr
);
388 /* Do jiffies check only in xrun_debug mode */
389 if (!xrun_debug(substream
, XRUN_DEBUG_JIFFIESCHECK
))
390 goto no_jiffies_check
;
392 /* Skip the jiffies check for hardwares with BATCH flag.
393 * Such hardware usually just increases the position at each IRQ,
394 * thus it can't give any strange position.
396 if (runtime
->hw
.info
& SNDRV_PCM_INFO_BATCH
)
397 goto no_jiffies_check
;
399 if (hdelta
< runtime
->delay
)
400 goto no_jiffies_check
;
401 hdelta
-= runtime
->delay
;
402 jdelta
= jiffies
- runtime
->hw_ptr_jiffies
;
403 if (((hdelta
* HZ
) / runtime
->rate
) > jdelta
+ HZ
/100) {
405 (((runtime
->period_size
* HZ
) / runtime
->rate
)
407 /* move new_hw_ptr according jiffies not pos variable */
408 new_hw_ptr
= old_hw_ptr
;
410 /* use loop to avoid checks for delta overflows */
411 /* the delta value is small or zero in most cases */
413 new_hw_ptr
+= runtime
->period_size
;
414 if (new_hw_ptr
>= runtime
->boundary
)
415 new_hw_ptr
-= runtime
->boundary
;
418 /* align hw_base to buffer_size */
419 hw_ptr_error(substream
,
420 "hw_ptr skipping! %s"
421 "(pos=%ld, delta=%ld, period=%ld, "
422 "jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
423 in_interrupt
? "[Q] " : "",
424 (long)pos
, (long)hdelta
,
425 (long)runtime
->period_size
, jdelta
,
426 ((hdelta
* HZ
) / runtime
->rate
), hw_base
,
427 (unsigned long)old_hw_ptr
,
428 (unsigned long)new_hw_ptr
);
429 /* reset values to proper state */
431 hw_base
= new_hw_ptr
- (new_hw_ptr
% runtime
->buffer_size
);
434 if (delta
> runtime
->period_size
+ runtime
->period_size
/ 2) {
435 hw_ptr_error(substream
,
436 "Lost interrupts? %s"
437 "(stream=%i, delta=%ld, new_hw_ptr=%ld, "
439 in_interrupt
? "[Q] " : "",
440 substream
->stream
, (long)delta
,
445 if (runtime
->status
->hw_ptr
== new_hw_ptr
)
448 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
449 runtime
->silence_size
> 0)
450 snd_pcm_playback_silence(substream
, new_hw_ptr
);
453 delta
= new_hw_ptr
- runtime
->hw_ptr_interrupt
;
455 delta
+= runtime
->boundary
;
456 delta
-= (snd_pcm_uframes_t
)delta
% runtime
->period_size
;
457 runtime
->hw_ptr_interrupt
+= delta
;
458 if (runtime
->hw_ptr_interrupt
>= runtime
->boundary
)
459 runtime
->hw_ptr_interrupt
-= runtime
->boundary
;
461 runtime
->hw_ptr_base
= hw_base
;
462 runtime
->status
->hw_ptr
= new_hw_ptr
;
463 runtime
->hw_ptr_jiffies
= jiffies
;
464 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
)
465 snd_pcm_gettime(runtime
, (struct timespec
*)&runtime
->status
->tstamp
);
467 return snd_pcm_update_state(substream
, runtime
);
470 /* CAUTION: call it with irq disabled */
471 int snd_pcm_update_hw_ptr(struct snd_pcm_substream
*substream
)
473 return snd_pcm_update_hw_ptr0(substream
, 0);
477 * snd_pcm_set_ops - set the PCM operators
478 * @pcm: the pcm instance
479 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
480 * @ops: the operator table
482 * Sets the given PCM operators to the pcm instance.
484 void snd_pcm_set_ops(struct snd_pcm
*pcm
, int direction
, struct snd_pcm_ops
*ops
)
486 struct snd_pcm_str
*stream
= &pcm
->streams
[direction
];
487 struct snd_pcm_substream
*substream
;
489 for (substream
= stream
->substream
; substream
!= NULL
; substream
= substream
->next
)
490 substream
->ops
= ops
;
493 EXPORT_SYMBOL(snd_pcm_set_ops
);
496 * snd_pcm_sync - set the PCM sync id
497 * @substream: the pcm substream
499 * Sets the PCM sync identifier for the card.
501 void snd_pcm_set_sync(struct snd_pcm_substream
*substream
)
503 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
505 runtime
->sync
.id32
[0] = substream
->pcm
->card
->number
;
506 runtime
->sync
.id32
[1] = -1;
507 runtime
->sync
.id32
[2] = -1;
508 runtime
->sync
.id32
[3] = -1;
511 EXPORT_SYMBOL(snd_pcm_set_sync
);
514 * Standard ioctl routine
517 static inline unsigned int div32(unsigned int a
, unsigned int b
,
528 static inline unsigned int div_down(unsigned int a
, unsigned int b
)
535 static inline unsigned int div_up(unsigned int a
, unsigned int b
)
547 static inline unsigned int mul(unsigned int a
, unsigned int b
)
551 if (div_down(UINT_MAX
, a
) < b
)
556 static inline unsigned int muldiv32(unsigned int a
, unsigned int b
,
557 unsigned int c
, unsigned int *r
)
559 u_int64_t n
= (u_int64_t
) a
* b
;
565 n
= div_u64_rem(n
, c
, r
);
574 * snd_interval_refine - refine the interval value of configurator
575 * @i: the interval value to refine
576 * @v: the interval value to refer to
578 * Refines the interval value with the reference value.
579 * The interval is changed to the range satisfying both intervals.
580 * The interval status (min, max, integer, etc.) are evaluated.
582 * Returns non-zero if the value is changed, zero if not changed.
584 int snd_interval_refine(struct snd_interval
*i
, const struct snd_interval
*v
)
587 if (snd_BUG_ON(snd_interval_empty(i
)))
589 if (i
->min
< v
->min
) {
591 i
->openmin
= v
->openmin
;
593 } else if (i
->min
== v
->min
&& !i
->openmin
&& v
->openmin
) {
597 if (i
->max
> v
->max
) {
599 i
->openmax
= v
->openmax
;
601 } else if (i
->max
== v
->max
&& !i
->openmax
&& v
->openmax
) {
605 if (!i
->integer
&& v
->integer
) {
618 } else if (!i
->openmin
&& !i
->openmax
&& i
->min
== i
->max
)
620 if (snd_interval_checkempty(i
)) {
621 snd_interval_none(i
);
627 EXPORT_SYMBOL(snd_interval_refine
);
629 static int snd_interval_refine_first(struct snd_interval
*i
)
631 if (snd_BUG_ON(snd_interval_empty(i
)))
633 if (snd_interval_single(i
))
636 i
->openmax
= i
->openmin
;
642 static int snd_interval_refine_last(struct snd_interval
*i
)
644 if (snd_BUG_ON(snd_interval_empty(i
)))
646 if (snd_interval_single(i
))
649 i
->openmin
= i
->openmax
;
655 void snd_interval_mul(const struct snd_interval
*a
, const struct snd_interval
*b
, struct snd_interval
*c
)
657 if (a
->empty
|| b
->empty
) {
658 snd_interval_none(c
);
662 c
->min
= mul(a
->min
, b
->min
);
663 c
->openmin
= (a
->openmin
|| b
->openmin
);
664 c
->max
= mul(a
->max
, b
->max
);
665 c
->openmax
= (a
->openmax
|| b
->openmax
);
666 c
->integer
= (a
->integer
&& b
->integer
);
670 * snd_interval_div - refine the interval value with division
677 * Returns non-zero if the value is changed, zero if not changed.
679 void snd_interval_div(const struct snd_interval
*a
, const struct snd_interval
*b
, struct snd_interval
*c
)
682 if (a
->empty
|| b
->empty
) {
683 snd_interval_none(c
);
687 c
->min
= div32(a
->min
, b
->max
, &r
);
688 c
->openmin
= (r
|| a
->openmin
|| b
->openmax
);
690 c
->max
= div32(a
->max
, b
->min
, &r
);
695 c
->openmax
= (a
->openmax
|| b
->openmin
);
704 * snd_interval_muldivk - refine the interval value
707 * @k: divisor (as integer)
712 * Returns non-zero if the value is changed, zero if not changed.
714 void snd_interval_muldivk(const struct snd_interval
*a
, const struct snd_interval
*b
,
715 unsigned int k
, struct snd_interval
*c
)
718 if (a
->empty
|| b
->empty
) {
719 snd_interval_none(c
);
723 c
->min
= muldiv32(a
->min
, b
->min
, k
, &r
);
724 c
->openmin
= (r
|| a
->openmin
|| b
->openmin
);
725 c
->max
= muldiv32(a
->max
, b
->max
, k
, &r
);
730 c
->openmax
= (a
->openmax
|| b
->openmax
);
735 * snd_interval_mulkdiv - refine the interval value
737 * @k: dividend 2 (as integer)
743 * Returns non-zero if the value is changed, zero if not changed.
745 void snd_interval_mulkdiv(const struct snd_interval
*a
, unsigned int k
,
746 const struct snd_interval
*b
, struct snd_interval
*c
)
749 if (a
->empty
|| b
->empty
) {
750 snd_interval_none(c
);
754 c
->min
= muldiv32(a
->min
, k
, b
->max
, &r
);
755 c
->openmin
= (r
|| a
->openmin
|| b
->openmax
);
757 c
->max
= muldiv32(a
->max
, k
, b
->min
, &r
);
762 c
->openmax
= (a
->openmax
|| b
->openmin
);
774 * snd_interval_ratnum - refine the interval value
775 * @i: interval to refine
776 * @rats_count: number of ratnum_t
777 * @rats: ratnum_t array
778 * @nump: pointer to store the resultant numerator
779 * @denp: pointer to store the resultant denominator
781 * Returns non-zero if the value is changed, zero if not changed.
783 int snd_interval_ratnum(struct snd_interval
*i
,
784 unsigned int rats_count
, struct snd_ratnum
*rats
,
785 unsigned int *nump
, unsigned int *denp
)
787 unsigned int best_num
, best_den
;
790 struct snd_interval t
;
792 unsigned int result_num
, result_den
;
795 best_num
= best_den
= best_diff
= 0;
796 for (k
= 0; k
< rats_count
; ++k
) {
797 unsigned int num
= rats
[k
].num
;
799 unsigned int q
= i
->min
;
803 den
= div_up(num
, q
);
804 if (den
< rats
[k
].den_min
)
806 if (den
> rats
[k
].den_max
)
807 den
= rats
[k
].den_max
;
810 r
= (den
- rats
[k
].den_min
) % rats
[k
].den_step
;
814 diff
= num
- q
* den
;
818 diff
* best_den
< best_diff
* den
) {
828 t
.min
= div_down(best_num
, best_den
);
829 t
.openmin
= !!(best_num
% best_den
);
831 result_num
= best_num
;
832 result_diff
= best_diff
;
833 result_den
= best_den
;
834 best_num
= best_den
= best_diff
= 0;
835 for (k
= 0; k
< rats_count
; ++k
) {
836 unsigned int num
= rats
[k
].num
;
838 unsigned int q
= i
->max
;
844 den
= div_down(num
, q
);
845 if (den
> rats
[k
].den_max
)
847 if (den
< rats
[k
].den_min
)
848 den
= rats
[k
].den_min
;
851 r
= (den
- rats
[k
].den_min
) % rats
[k
].den_step
;
853 den
+= rats
[k
].den_step
- r
;
855 diff
= q
* den
- num
;
859 diff
* best_den
< best_diff
* den
) {
869 t
.max
= div_up(best_num
, best_den
);
870 t
.openmax
= !!(best_num
% best_den
);
872 err
= snd_interval_refine(i
, &t
);
876 if (snd_interval_single(i
)) {
877 if (best_diff
* result_den
< result_diff
* best_den
) {
878 result_num
= best_num
;
879 result_den
= best_den
;
889 EXPORT_SYMBOL(snd_interval_ratnum
);
892 * snd_interval_ratden - refine the interval value
893 * @i: interval to refine
894 * @rats_count: number of struct ratden
895 * @rats: struct ratden array
896 * @nump: pointer to store the resultant numerator
897 * @denp: pointer to store the resultant denominator
899 * Returns non-zero if the value is changed, zero if not changed.
901 static int snd_interval_ratden(struct snd_interval
*i
,
902 unsigned int rats_count
, struct snd_ratden
*rats
,
903 unsigned int *nump
, unsigned int *denp
)
905 unsigned int best_num
, best_diff
, best_den
;
907 struct snd_interval t
;
910 best_num
= best_den
= best_diff
= 0;
911 for (k
= 0; k
< rats_count
; ++k
) {
913 unsigned int den
= rats
[k
].den
;
914 unsigned int q
= i
->min
;
917 if (num
> rats
[k
].num_max
)
919 if (num
< rats
[k
].num_min
)
920 num
= rats
[k
].num_max
;
923 r
= (num
- rats
[k
].num_min
) % rats
[k
].num_step
;
925 num
+= rats
[k
].num_step
- r
;
927 diff
= num
- q
* den
;
929 diff
* best_den
< best_diff
* den
) {
939 t
.min
= div_down(best_num
, best_den
);
940 t
.openmin
= !!(best_num
% best_den
);
942 best_num
= best_den
= best_diff
= 0;
943 for (k
= 0; k
< rats_count
; ++k
) {
945 unsigned int den
= rats
[k
].den
;
946 unsigned int q
= i
->max
;
949 if (num
< rats
[k
].num_min
)
951 if (num
> rats
[k
].num_max
)
952 num
= rats
[k
].num_max
;
955 r
= (num
- rats
[k
].num_min
) % rats
[k
].num_step
;
959 diff
= q
* den
- num
;
961 diff
* best_den
< best_diff
* den
) {
971 t
.max
= div_up(best_num
, best_den
);
972 t
.openmax
= !!(best_num
% best_den
);
974 err
= snd_interval_refine(i
, &t
);
978 if (snd_interval_single(i
)) {
988 * snd_interval_list - refine the interval value from the list
989 * @i: the interval value to refine
990 * @count: the number of elements in the list
991 * @list: the value list
992 * @mask: the bit-mask to evaluate
994 * Refines the interval value from the list.
995 * When mask is non-zero, only the elements corresponding to bit 1 are
998 * Returns non-zero if the value is changed, zero if not changed.
1000 int snd_interval_list(struct snd_interval
*i
, unsigned int count
, unsigned int *list
, unsigned int mask
)
1003 struct snd_interval list_range
;
1009 snd_interval_any(&list_range
);
1010 list_range
.min
= UINT_MAX
;
1012 for (k
= 0; k
< count
; k
++) {
1013 if (mask
&& !(mask
& (1 << k
)))
1015 if (!snd_interval_test(i
, list
[k
]))
1017 list_range
.min
= min(list_range
.min
, list
[k
]);
1018 list_range
.max
= max(list_range
.max
, list
[k
]);
1020 return snd_interval_refine(i
, &list_range
);
1023 EXPORT_SYMBOL(snd_interval_list
);
1025 static int snd_interval_step(struct snd_interval
*i
, unsigned int min
, unsigned int step
)
1029 n
= (i
->min
- min
) % step
;
1030 if (n
!= 0 || i
->openmin
) {
1034 n
= (i
->max
- min
) % step
;
1035 if (n
!= 0 || i
->openmax
) {
1039 if (snd_interval_checkempty(i
)) {
1046 /* Info constraints helpers */
1049 * snd_pcm_hw_rule_add - add the hw-constraint rule
1050 * @runtime: the pcm runtime instance
1051 * @cond: condition bits
1052 * @var: the variable to evaluate
1053 * @func: the evaluation function
1054 * @private: the private data pointer passed to function
1055 * @dep: the dependent variables
1057 * Returns zero if successful, or a negative error code on failure.
1059 int snd_pcm_hw_rule_add(struct snd_pcm_runtime
*runtime
, unsigned int cond
,
1061 snd_pcm_hw_rule_func_t func
, void *private,
1064 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1065 struct snd_pcm_hw_rule
*c
;
1068 va_start(args
, dep
);
1069 if (constrs
->rules_num
>= constrs
->rules_all
) {
1070 struct snd_pcm_hw_rule
*new;
1071 unsigned int new_rules
= constrs
->rules_all
+ 16;
1072 new = kcalloc(new_rules
, sizeof(*c
), GFP_KERNEL
);
1075 if (constrs
->rules
) {
1076 memcpy(new, constrs
->rules
,
1077 constrs
->rules_num
* sizeof(*c
));
1078 kfree(constrs
->rules
);
1080 constrs
->rules
= new;
1081 constrs
->rules_all
= new_rules
;
1083 c
= &constrs
->rules
[constrs
->rules_num
];
1087 c
->private = private;
1090 if (snd_BUG_ON(k
>= ARRAY_SIZE(c
->deps
)))
1095 dep
= va_arg(args
, int);
1097 constrs
->rules_num
++;
1102 EXPORT_SYMBOL(snd_pcm_hw_rule_add
);
1105 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1106 * @runtime: PCM runtime instance
1107 * @var: hw_params variable to apply the mask
1108 * @mask: the bitmap mask
1110 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1112 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
,
1115 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1116 struct snd_mask
*maskp
= constrs_mask(constrs
, var
);
1117 *maskp
->bits
&= mask
;
1118 memset(maskp
->bits
+ 1, 0, (SNDRV_MASK_MAX
-32) / 8); /* clear rest */
1119 if (*maskp
->bits
== 0)
1125 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1126 * @runtime: PCM runtime instance
1127 * @var: hw_params variable to apply the mask
1128 * @mask: the 64bit bitmap mask
1130 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1132 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
,
1135 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1136 struct snd_mask
*maskp
= constrs_mask(constrs
, var
);
1137 maskp
->bits
[0] &= (u_int32_t
)mask
;
1138 maskp
->bits
[1] &= (u_int32_t
)(mask
>> 32);
1139 memset(maskp
->bits
+ 2, 0, (SNDRV_MASK_MAX
-64) / 8); /* clear rest */
1140 if (! maskp
->bits
[0] && ! maskp
->bits
[1])
1146 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1147 * @runtime: PCM runtime instance
1148 * @var: hw_params variable to apply the integer constraint
1150 * Apply the constraint of integer to an interval parameter.
1152 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
)
1154 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1155 return snd_interval_setinteger(constrs_interval(constrs
, var
));
1158 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer
);
1161 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1162 * @runtime: PCM runtime instance
1163 * @var: hw_params variable to apply the range
1164 * @min: the minimal value
1165 * @max: the maximal value
1167 * Apply the min/max range constraint to an interval parameter.
1169 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
,
1170 unsigned int min
, unsigned int max
)
1172 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1173 struct snd_interval t
;
1176 t
.openmin
= t
.openmax
= 0;
1178 return snd_interval_refine(constrs_interval(constrs
, var
), &t
);
1181 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax
);
1183 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params
*params
,
1184 struct snd_pcm_hw_rule
*rule
)
1186 struct snd_pcm_hw_constraint_list
*list
= rule
->private;
1187 return snd_interval_list(hw_param_interval(params
, rule
->var
), list
->count
, list
->list
, list
->mask
);
1192 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1193 * @runtime: PCM runtime instance
1194 * @cond: condition bits
1195 * @var: hw_params variable to apply the list constraint
1198 * Apply the list of constraints to an interval parameter.
1200 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime
*runtime
,
1202 snd_pcm_hw_param_t var
,
1203 struct snd_pcm_hw_constraint_list
*l
)
1205 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1206 snd_pcm_hw_rule_list
, l
,
1210 EXPORT_SYMBOL(snd_pcm_hw_constraint_list
);
1212 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params
*params
,
1213 struct snd_pcm_hw_rule
*rule
)
1215 struct snd_pcm_hw_constraint_ratnums
*r
= rule
->private;
1216 unsigned int num
= 0, den
= 0;
1218 err
= snd_interval_ratnum(hw_param_interval(params
, rule
->var
),
1219 r
->nrats
, r
->rats
, &num
, &den
);
1220 if (err
>= 0 && den
&& rule
->var
== SNDRV_PCM_HW_PARAM_RATE
) {
1221 params
->rate_num
= num
;
1222 params
->rate_den
= den
;
1228 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1229 * @runtime: PCM runtime instance
1230 * @cond: condition bits
1231 * @var: hw_params variable to apply the ratnums constraint
1232 * @r: struct snd_ratnums constriants
1234 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime
*runtime
,
1236 snd_pcm_hw_param_t var
,
1237 struct snd_pcm_hw_constraint_ratnums
*r
)
1239 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1240 snd_pcm_hw_rule_ratnums
, r
,
1244 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums
);
1246 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params
*params
,
1247 struct snd_pcm_hw_rule
*rule
)
1249 struct snd_pcm_hw_constraint_ratdens
*r
= rule
->private;
1250 unsigned int num
= 0, den
= 0;
1251 int err
= snd_interval_ratden(hw_param_interval(params
, rule
->var
),
1252 r
->nrats
, r
->rats
, &num
, &den
);
1253 if (err
>= 0 && den
&& rule
->var
== SNDRV_PCM_HW_PARAM_RATE
) {
1254 params
->rate_num
= num
;
1255 params
->rate_den
= den
;
1261 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1262 * @runtime: PCM runtime instance
1263 * @cond: condition bits
1264 * @var: hw_params variable to apply the ratdens constraint
1265 * @r: struct snd_ratdens constriants
1267 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime
*runtime
,
1269 snd_pcm_hw_param_t var
,
1270 struct snd_pcm_hw_constraint_ratdens
*r
)
1272 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1273 snd_pcm_hw_rule_ratdens
, r
,
1277 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens
);
1279 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params
*params
,
1280 struct snd_pcm_hw_rule
*rule
)
1282 unsigned int l
= (unsigned long) rule
->private;
1283 int width
= l
& 0xffff;
1284 unsigned int msbits
= l
>> 16;
1285 struct snd_interval
*i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
1286 if (snd_interval_single(i
) && snd_interval_value(i
) == width
)
1287 params
->msbits
= msbits
;
1292 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1293 * @runtime: PCM runtime instance
1294 * @cond: condition bits
1295 * @width: sample bits width
1296 * @msbits: msbits width
1298 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime
*runtime
,
1301 unsigned int msbits
)
1303 unsigned long l
= (msbits
<< 16) | width
;
1304 return snd_pcm_hw_rule_add(runtime
, cond
, -1,
1305 snd_pcm_hw_rule_msbits
,
1307 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1310 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits
);
1312 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params
*params
,
1313 struct snd_pcm_hw_rule
*rule
)
1315 unsigned long step
= (unsigned long) rule
->private;
1316 return snd_interval_step(hw_param_interval(params
, rule
->var
), 0, step
);
1320 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1321 * @runtime: PCM runtime instance
1322 * @cond: condition bits
1323 * @var: hw_params variable to apply the step constraint
1326 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime
*runtime
,
1328 snd_pcm_hw_param_t var
,
1331 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1332 snd_pcm_hw_rule_step
, (void *) step
,
1336 EXPORT_SYMBOL(snd_pcm_hw_constraint_step
);
1338 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params
*params
, struct snd_pcm_hw_rule
*rule
)
1340 static unsigned int pow2_sizes
[] = {
1341 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1342 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1343 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1344 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1346 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1347 ARRAY_SIZE(pow2_sizes
), pow2_sizes
, 0);
1351 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1352 * @runtime: PCM runtime instance
1353 * @cond: condition bits
1354 * @var: hw_params variable to apply the power-of-2 constraint
1356 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime
*runtime
,
1358 snd_pcm_hw_param_t var
)
1360 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1361 snd_pcm_hw_rule_pow2
, NULL
,
1365 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2
);
1367 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params
*params
,
1368 snd_pcm_hw_param_t var
)
1370 if (hw_is_mask(var
)) {
1371 snd_mask_any(hw_param_mask(params
, var
));
1372 params
->cmask
|= 1 << var
;
1373 params
->rmask
|= 1 << var
;
1376 if (hw_is_interval(var
)) {
1377 snd_interval_any(hw_param_interval(params
, var
));
1378 params
->cmask
|= 1 << var
;
1379 params
->rmask
|= 1 << var
;
1385 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params
*params
)
1388 memset(params
, 0, sizeof(*params
));
1389 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++)
1390 _snd_pcm_hw_param_any(params
, k
);
1391 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
1392 _snd_pcm_hw_param_any(params
, k
);
1396 EXPORT_SYMBOL(_snd_pcm_hw_params_any
);
1399 * snd_pcm_hw_param_value - return @params field @var value
1400 * @params: the hw_params instance
1401 * @var: parameter to retrieve
1402 * @dir: pointer to the direction (-1,0,1) or %NULL
1404 * Return the value for field @var if it's fixed in configuration space
1405 * defined by @params. Return -%EINVAL otherwise.
1407 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params
*params
,
1408 snd_pcm_hw_param_t var
, int *dir
)
1410 if (hw_is_mask(var
)) {
1411 const struct snd_mask
*mask
= hw_param_mask_c(params
, var
);
1412 if (!snd_mask_single(mask
))
1416 return snd_mask_value(mask
);
1418 if (hw_is_interval(var
)) {
1419 const struct snd_interval
*i
= hw_param_interval_c(params
, var
);
1420 if (!snd_interval_single(i
))
1424 return snd_interval_value(i
);
1429 EXPORT_SYMBOL(snd_pcm_hw_param_value
);
1431 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params
*params
,
1432 snd_pcm_hw_param_t var
)
1434 if (hw_is_mask(var
)) {
1435 snd_mask_none(hw_param_mask(params
, var
));
1436 params
->cmask
|= 1 << var
;
1437 params
->rmask
|= 1 << var
;
1438 } else if (hw_is_interval(var
)) {
1439 snd_interval_none(hw_param_interval(params
, var
));
1440 params
->cmask
|= 1 << var
;
1441 params
->rmask
|= 1 << var
;
1447 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty
);
1449 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params
*params
,
1450 snd_pcm_hw_param_t var
)
1453 if (hw_is_mask(var
))
1454 changed
= snd_mask_refine_first(hw_param_mask(params
, var
));
1455 else if (hw_is_interval(var
))
1456 changed
= snd_interval_refine_first(hw_param_interval(params
, var
));
1460 params
->cmask
|= 1 << var
;
1461 params
->rmask
|= 1 << var
;
1468 * snd_pcm_hw_param_first - refine config space and return minimum value
1469 * @pcm: PCM instance
1470 * @params: the hw_params instance
1471 * @var: parameter to retrieve
1472 * @dir: pointer to the direction (-1,0,1) or %NULL
1474 * Inside configuration space defined by @params remove from @var all
1475 * values > minimum. Reduce configuration space accordingly.
1476 * Return the minimum.
1478 int snd_pcm_hw_param_first(struct snd_pcm_substream
*pcm
,
1479 struct snd_pcm_hw_params
*params
,
1480 snd_pcm_hw_param_t var
, int *dir
)
1482 int changed
= _snd_pcm_hw_param_first(params
, var
);
1485 if (params
->rmask
) {
1486 int err
= snd_pcm_hw_refine(pcm
, params
);
1487 if (snd_BUG_ON(err
< 0))
1490 return snd_pcm_hw_param_value(params
, var
, dir
);
1493 EXPORT_SYMBOL(snd_pcm_hw_param_first
);
1495 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params
*params
,
1496 snd_pcm_hw_param_t var
)
1499 if (hw_is_mask(var
))
1500 changed
= snd_mask_refine_last(hw_param_mask(params
, var
));
1501 else if (hw_is_interval(var
))
1502 changed
= snd_interval_refine_last(hw_param_interval(params
, var
));
1506 params
->cmask
|= 1 << var
;
1507 params
->rmask
|= 1 << var
;
1514 * snd_pcm_hw_param_last - refine config space and return maximum value
1515 * @pcm: PCM instance
1516 * @params: the hw_params instance
1517 * @var: parameter to retrieve
1518 * @dir: pointer to the direction (-1,0,1) or %NULL
1520 * Inside configuration space defined by @params remove from @var all
1521 * values < maximum. Reduce configuration space accordingly.
1522 * Return the maximum.
1524 int snd_pcm_hw_param_last(struct snd_pcm_substream
*pcm
,
1525 struct snd_pcm_hw_params
*params
,
1526 snd_pcm_hw_param_t var
, int *dir
)
1528 int changed
= _snd_pcm_hw_param_last(params
, var
);
1531 if (params
->rmask
) {
1532 int err
= snd_pcm_hw_refine(pcm
, params
);
1533 if (snd_BUG_ON(err
< 0))
1536 return snd_pcm_hw_param_value(params
, var
, dir
);
1539 EXPORT_SYMBOL(snd_pcm_hw_param_last
);
1542 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1543 * @pcm: PCM instance
1544 * @params: the hw_params instance
1546 * Choose one configuration from configuration space defined by @params.
1547 * The configuration chosen is that obtained fixing in this order:
1548 * first access, first format, first subformat, min channels,
1549 * min rate, min period time, max buffer size, min tick time
1551 int snd_pcm_hw_params_choose(struct snd_pcm_substream
*pcm
,
1552 struct snd_pcm_hw_params
*params
)
1554 static int vars
[] = {
1555 SNDRV_PCM_HW_PARAM_ACCESS
,
1556 SNDRV_PCM_HW_PARAM_FORMAT
,
1557 SNDRV_PCM_HW_PARAM_SUBFORMAT
,
1558 SNDRV_PCM_HW_PARAM_CHANNELS
,
1559 SNDRV_PCM_HW_PARAM_RATE
,
1560 SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1561 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1562 SNDRV_PCM_HW_PARAM_TICK_TIME
,
1567 for (v
= vars
; *v
!= -1; v
++) {
1568 if (*v
!= SNDRV_PCM_HW_PARAM_BUFFER_SIZE
)
1569 err
= snd_pcm_hw_param_first(pcm
, params
, *v
, NULL
);
1571 err
= snd_pcm_hw_param_last(pcm
, params
, *v
, NULL
);
1572 if (snd_BUG_ON(err
< 0))
1578 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream
*substream
,
1581 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1582 unsigned long flags
;
1583 snd_pcm_stream_lock_irqsave(substream
, flags
);
1584 if (snd_pcm_running(substream
) &&
1585 snd_pcm_update_hw_ptr(substream
) >= 0)
1586 runtime
->status
->hw_ptr
%= runtime
->buffer_size
;
1588 runtime
->status
->hw_ptr
= 0;
1589 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1593 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream
*substream
,
1596 struct snd_pcm_channel_info
*info
= arg
;
1597 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1599 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
)) {
1603 width
= snd_pcm_format_physical_width(runtime
->format
);
1607 switch (runtime
->access
) {
1608 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
:
1609 case SNDRV_PCM_ACCESS_RW_INTERLEAVED
:
1610 info
->first
= info
->channel
* width
;
1611 info
->step
= runtime
->channels
* width
;
1613 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
:
1614 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
:
1616 size_t size
= runtime
->dma_bytes
/ runtime
->channels
;
1617 info
->first
= info
->channel
* size
* 8;
1628 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream
*substream
,
1631 struct snd_pcm_hw_params
*params
= arg
;
1632 snd_pcm_format_t format
;
1633 int channels
, width
;
1635 params
->fifo_size
= substream
->runtime
->hw
.fifo_size
;
1636 if (!(substream
->runtime
->hw
.info
& SNDRV_PCM_INFO_FIFO_IN_FRAMES
)) {
1637 format
= params_format(params
);
1638 channels
= params_channels(params
);
1639 width
= snd_pcm_format_physical_width(format
);
1640 params
->fifo_size
/= width
* channels
;
1646 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1647 * @substream: the pcm substream instance
1648 * @cmd: ioctl command
1649 * @arg: ioctl argument
1651 * Processes the generic ioctl commands for PCM.
1652 * Can be passed as the ioctl callback for PCM ops.
1654 * Returns zero if successful, or a negative error code on failure.
1656 int snd_pcm_lib_ioctl(struct snd_pcm_substream
*substream
,
1657 unsigned int cmd
, void *arg
)
1660 case SNDRV_PCM_IOCTL1_INFO
:
1662 case SNDRV_PCM_IOCTL1_RESET
:
1663 return snd_pcm_lib_ioctl_reset(substream
, arg
);
1664 case SNDRV_PCM_IOCTL1_CHANNEL_INFO
:
1665 return snd_pcm_lib_ioctl_channel_info(substream
, arg
);
1666 case SNDRV_PCM_IOCTL1_FIFO_SIZE
:
1667 return snd_pcm_lib_ioctl_fifo_size(substream
, arg
);
1672 EXPORT_SYMBOL(snd_pcm_lib_ioctl
);
1675 * snd_pcm_period_elapsed - update the pcm status for the next period
1676 * @substream: the pcm substream instance
1678 * This function is called from the interrupt handler when the
1679 * PCM has processed the period size. It will update the current
1680 * pointer, wake up sleepers, etc.
1682 * Even if more than one periods have elapsed since the last call, you
1683 * have to call this only once.
1685 void snd_pcm_period_elapsed(struct snd_pcm_substream
*substream
)
1687 struct snd_pcm_runtime
*runtime
;
1688 unsigned long flags
;
1690 if (PCM_RUNTIME_CHECK(substream
))
1692 runtime
= substream
->runtime
;
1694 if (runtime
->transfer_ack_begin
)
1695 runtime
->transfer_ack_begin(substream
);
1697 snd_pcm_stream_lock_irqsave(substream
, flags
);
1698 if (!snd_pcm_running(substream
) ||
1699 snd_pcm_update_hw_ptr0(substream
, 1) < 0)
1702 if (substream
->timer_running
)
1703 snd_timer_interrupt(substream
->timer
, 1);
1705 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1706 if (runtime
->transfer_ack_end
)
1707 runtime
->transfer_ack_end(substream
);
1708 kill_fasync(&runtime
->fasync
, SIGIO
, POLL_IN
);
1711 EXPORT_SYMBOL(snd_pcm_period_elapsed
);
1714 * Wait until avail_min data becomes available
1715 * Returns a negative error code if any error occurs during operation.
1716 * The available space is stored on availp. When err = 0 and avail = 0
1717 * on the capture stream, it indicates the stream is in DRAINING state.
1719 static int wait_for_avail(struct snd_pcm_substream
*substream
,
1720 snd_pcm_uframes_t
*availp
)
1722 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1723 int is_playback
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
1726 snd_pcm_uframes_t avail
= 0;
1729 init_waitqueue_entry(&wait
, current
);
1730 add_wait_queue(&runtime
->tsleep
, &wait
);
1732 if (signal_pending(current
)) {
1736 set_current_state(TASK_INTERRUPTIBLE
);
1737 snd_pcm_stream_unlock_irq(substream
);
1738 tout
= schedule_timeout(msecs_to_jiffies(10000));
1739 snd_pcm_stream_lock_irq(substream
);
1740 switch (runtime
->status
->state
) {
1741 case SNDRV_PCM_STATE_SUSPENDED
:
1744 case SNDRV_PCM_STATE_XRUN
:
1747 case SNDRV_PCM_STATE_DRAINING
:
1751 avail
= 0; /* indicate draining */
1753 case SNDRV_PCM_STATE_OPEN
:
1754 case SNDRV_PCM_STATE_SETUP
:
1755 case SNDRV_PCM_STATE_DISCONNECTED
:
1760 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1761 is_playback
? "playback" : "capture");
1766 avail
= snd_pcm_playback_avail(runtime
);
1768 avail
= snd_pcm_capture_avail(runtime
);
1769 if (avail
>= runtime
->twake
)
1773 remove_wait_queue(&runtime
->tsleep
, &wait
);
1778 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream
*substream
,
1780 unsigned long data
, unsigned int off
,
1781 snd_pcm_uframes_t frames
)
1783 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1785 char __user
*buf
= (char __user
*) data
+ frames_to_bytes(runtime
, off
);
1786 if (substream
->ops
->copy
) {
1787 if ((err
= substream
->ops
->copy(substream
, -1, hwoff
, buf
, frames
)) < 0)
1790 char *hwbuf
= runtime
->dma_area
+ frames_to_bytes(runtime
, hwoff
);
1791 if (copy_from_user(hwbuf
, buf
, frames_to_bytes(runtime
, frames
)))
1797 typedef int (*transfer_f
)(struct snd_pcm_substream
*substream
, unsigned int hwoff
,
1798 unsigned long data
, unsigned int off
,
1799 snd_pcm_uframes_t size
);
1801 static snd_pcm_sframes_t
snd_pcm_lib_write1(struct snd_pcm_substream
*substream
,
1803 snd_pcm_uframes_t size
,
1805 transfer_f transfer
)
1807 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1808 snd_pcm_uframes_t xfer
= 0;
1809 snd_pcm_uframes_t offset
= 0;
1815 snd_pcm_stream_lock_irq(substream
);
1816 switch (runtime
->status
->state
) {
1817 case SNDRV_PCM_STATE_PREPARED
:
1818 case SNDRV_PCM_STATE_RUNNING
:
1819 case SNDRV_PCM_STATE_PAUSED
:
1821 case SNDRV_PCM_STATE_XRUN
:
1824 case SNDRV_PCM_STATE_SUSPENDED
:
1832 runtime
->twake
= runtime
->control
->avail_min
? : 1;
1834 snd_pcm_uframes_t frames
, appl_ptr
, appl_ofs
;
1835 snd_pcm_uframes_t avail
;
1836 snd_pcm_uframes_t cont
;
1837 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
1838 snd_pcm_update_hw_ptr(substream
);
1839 avail
= snd_pcm_playback_avail(runtime
);
1845 runtime
->twake
= min_t(snd_pcm_uframes_t
, size
,
1846 runtime
->control
->avail_min
? : 1);
1847 err
= wait_for_avail(substream
, &avail
);
1851 frames
= size
> avail
? avail
: size
;
1852 cont
= runtime
->buffer_size
- runtime
->control
->appl_ptr
% runtime
->buffer_size
;
1855 if (snd_BUG_ON(!frames
)) {
1857 snd_pcm_stream_unlock_irq(substream
);
1860 appl_ptr
= runtime
->control
->appl_ptr
;
1861 appl_ofs
= appl_ptr
% runtime
->buffer_size
;
1862 snd_pcm_stream_unlock_irq(substream
);
1863 err
= transfer(substream
, appl_ofs
, data
, offset
, frames
);
1864 snd_pcm_stream_lock_irq(substream
);
1867 switch (runtime
->status
->state
) {
1868 case SNDRV_PCM_STATE_XRUN
:
1871 case SNDRV_PCM_STATE_SUSPENDED
:
1878 if (appl_ptr
>= runtime
->boundary
)
1879 appl_ptr
-= runtime
->boundary
;
1880 runtime
->control
->appl_ptr
= appl_ptr
;
1881 if (substream
->ops
->ack
)
1882 substream
->ops
->ack(substream
);
1887 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
&&
1888 snd_pcm_playback_hw_avail(runtime
) >= (snd_pcm_sframes_t
)runtime
->start_threshold
) {
1889 err
= snd_pcm_start(substream
);
1896 if (xfer
> 0 && err
>= 0)
1897 snd_pcm_update_state(substream
, runtime
);
1898 snd_pcm_stream_unlock_irq(substream
);
1899 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: err
;
1902 /* sanity-check for read/write methods */
1903 static int pcm_sanity_check(struct snd_pcm_substream
*substream
)
1905 struct snd_pcm_runtime
*runtime
;
1906 if (PCM_RUNTIME_CHECK(substream
))
1908 runtime
= substream
->runtime
;
1909 if (snd_BUG_ON(!substream
->ops
->copy
&& !runtime
->dma_area
))
1911 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1916 snd_pcm_sframes_t
snd_pcm_lib_write(struct snd_pcm_substream
*substream
, const void __user
*buf
, snd_pcm_uframes_t size
)
1918 struct snd_pcm_runtime
*runtime
;
1922 err
= pcm_sanity_check(substream
);
1925 runtime
= substream
->runtime
;
1926 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
1928 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_INTERLEAVED
&&
1929 runtime
->channels
> 1)
1931 return snd_pcm_lib_write1(substream
, (unsigned long)buf
, size
, nonblock
,
1932 snd_pcm_lib_write_transfer
);
1935 EXPORT_SYMBOL(snd_pcm_lib_write
);
1937 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream
*substream
,
1939 unsigned long data
, unsigned int off
,
1940 snd_pcm_uframes_t frames
)
1942 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1944 void __user
**bufs
= (void __user
**)data
;
1945 int channels
= runtime
->channels
;
1947 if (substream
->ops
->copy
) {
1948 if (snd_BUG_ON(!substream
->ops
->silence
))
1950 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
1951 if (*bufs
== NULL
) {
1952 if ((err
= substream
->ops
->silence(substream
, c
, hwoff
, frames
)) < 0)
1955 char __user
*buf
= *bufs
+ samples_to_bytes(runtime
, off
);
1956 if ((err
= substream
->ops
->copy(substream
, c
, hwoff
, buf
, frames
)) < 0)
1961 /* default transfer behaviour */
1962 size_t dma_csize
= runtime
->dma_bytes
/ channels
;
1963 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
1964 char *hwbuf
= runtime
->dma_area
+ (c
* dma_csize
) + samples_to_bytes(runtime
, hwoff
);
1965 if (*bufs
== NULL
) {
1966 snd_pcm_format_set_silence(runtime
->format
, hwbuf
, frames
);
1968 char __user
*buf
= *bufs
+ samples_to_bytes(runtime
, off
);
1969 if (copy_from_user(hwbuf
, buf
, samples_to_bytes(runtime
, frames
)))
1977 snd_pcm_sframes_t
snd_pcm_lib_writev(struct snd_pcm_substream
*substream
,
1979 snd_pcm_uframes_t frames
)
1981 struct snd_pcm_runtime
*runtime
;
1985 err
= pcm_sanity_check(substream
);
1988 runtime
= substream
->runtime
;
1989 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
1991 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
1993 return snd_pcm_lib_write1(substream
, (unsigned long)bufs
, frames
,
1994 nonblock
, snd_pcm_lib_writev_transfer
);
1997 EXPORT_SYMBOL(snd_pcm_lib_writev
);
1999 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream
*substream
,
2001 unsigned long data
, unsigned int off
,
2002 snd_pcm_uframes_t frames
)
2004 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2006 char __user
*buf
= (char __user
*) data
+ frames_to_bytes(runtime
, off
);
2007 if (substream
->ops
->copy
) {
2008 if ((err
= substream
->ops
->copy(substream
, -1, hwoff
, buf
, frames
)) < 0)
2011 char *hwbuf
= runtime
->dma_area
+ frames_to_bytes(runtime
, hwoff
);
2012 if (copy_to_user(buf
, hwbuf
, frames_to_bytes(runtime
, frames
)))
2018 static snd_pcm_sframes_t
snd_pcm_lib_read1(struct snd_pcm_substream
*substream
,
2020 snd_pcm_uframes_t size
,
2022 transfer_f transfer
)
2024 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2025 snd_pcm_uframes_t xfer
= 0;
2026 snd_pcm_uframes_t offset
= 0;
2032 snd_pcm_stream_lock_irq(substream
);
2033 switch (runtime
->status
->state
) {
2034 case SNDRV_PCM_STATE_PREPARED
:
2035 if (size
>= runtime
->start_threshold
) {
2036 err
= snd_pcm_start(substream
);
2041 case SNDRV_PCM_STATE_DRAINING
:
2042 case SNDRV_PCM_STATE_RUNNING
:
2043 case SNDRV_PCM_STATE_PAUSED
:
2045 case SNDRV_PCM_STATE_XRUN
:
2048 case SNDRV_PCM_STATE_SUSPENDED
:
2056 runtime
->twake
= runtime
->control
->avail_min
? : 1;
2058 snd_pcm_uframes_t frames
, appl_ptr
, appl_ofs
;
2059 snd_pcm_uframes_t avail
;
2060 snd_pcm_uframes_t cont
;
2061 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
2062 snd_pcm_update_hw_ptr(substream
);
2063 avail
= snd_pcm_capture_avail(runtime
);
2065 if (runtime
->status
->state
==
2066 SNDRV_PCM_STATE_DRAINING
) {
2067 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
2074 runtime
->twake
= min_t(snd_pcm_uframes_t
, size
,
2075 runtime
->control
->avail_min
? : 1);
2076 err
= wait_for_avail(substream
, &avail
);
2080 continue; /* draining */
2082 frames
= size
> avail
? avail
: size
;
2083 cont
= runtime
->buffer_size
- runtime
->control
->appl_ptr
% runtime
->buffer_size
;
2086 if (snd_BUG_ON(!frames
)) {
2088 snd_pcm_stream_unlock_irq(substream
);
2091 appl_ptr
= runtime
->control
->appl_ptr
;
2092 appl_ofs
= appl_ptr
% runtime
->buffer_size
;
2093 snd_pcm_stream_unlock_irq(substream
);
2094 err
= transfer(substream
, appl_ofs
, data
, offset
, frames
);
2095 snd_pcm_stream_lock_irq(substream
);
2098 switch (runtime
->status
->state
) {
2099 case SNDRV_PCM_STATE_XRUN
:
2102 case SNDRV_PCM_STATE_SUSPENDED
:
2109 if (appl_ptr
>= runtime
->boundary
)
2110 appl_ptr
-= runtime
->boundary
;
2111 runtime
->control
->appl_ptr
= appl_ptr
;
2112 if (substream
->ops
->ack
)
2113 substream
->ops
->ack(substream
);
2121 if (xfer
> 0 && err
>= 0)
2122 snd_pcm_update_state(substream
, runtime
);
2123 snd_pcm_stream_unlock_irq(substream
);
2124 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: err
;
2127 snd_pcm_sframes_t
snd_pcm_lib_read(struct snd_pcm_substream
*substream
, void __user
*buf
, snd_pcm_uframes_t size
)
2129 struct snd_pcm_runtime
*runtime
;
2133 err
= pcm_sanity_check(substream
);
2136 runtime
= substream
->runtime
;
2137 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
2138 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_INTERLEAVED
)
2140 return snd_pcm_lib_read1(substream
, (unsigned long)buf
, size
, nonblock
, snd_pcm_lib_read_transfer
);
2143 EXPORT_SYMBOL(snd_pcm_lib_read
);
2145 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream
*substream
,
2147 unsigned long data
, unsigned int off
,
2148 snd_pcm_uframes_t frames
)
2150 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2152 void __user
**bufs
= (void __user
**)data
;
2153 int channels
= runtime
->channels
;
2155 if (substream
->ops
->copy
) {
2156 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
2160 buf
= *bufs
+ samples_to_bytes(runtime
, off
);
2161 if ((err
= substream
->ops
->copy(substream
, c
, hwoff
, buf
, frames
)) < 0)
2165 snd_pcm_uframes_t dma_csize
= runtime
->dma_bytes
/ channels
;
2166 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
2172 hwbuf
= runtime
->dma_area
+ (c
* dma_csize
) + samples_to_bytes(runtime
, hwoff
);
2173 buf
= *bufs
+ samples_to_bytes(runtime
, off
);
2174 if (copy_to_user(buf
, hwbuf
, samples_to_bytes(runtime
, frames
)))
2181 snd_pcm_sframes_t
snd_pcm_lib_readv(struct snd_pcm_substream
*substream
,
2183 snd_pcm_uframes_t frames
)
2185 struct snd_pcm_runtime
*runtime
;
2189 err
= pcm_sanity_check(substream
);
2192 runtime
= substream
->runtime
;
2193 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2196 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
2197 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
2199 return snd_pcm_lib_read1(substream
, (unsigned long)bufs
, frames
, nonblock
, snd_pcm_lib_readv_transfer
);
2202 EXPORT_SYMBOL(snd_pcm_lib_readv
);