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 runtime
->silence_filled
= avail
> 0 ? avail
: 0;
71 runtime
->silence_start
= (runtime
->status
->hw_ptr
+
72 runtime
->silence_filled
) %
75 ofs
= runtime
->status
->hw_ptr
;
76 frames
= new_hw_ptr
- ofs
;
77 if ((snd_pcm_sframes_t
)frames
< 0)
78 frames
+= runtime
->boundary
;
79 runtime
->silence_filled
-= frames
;
80 if ((snd_pcm_sframes_t
)runtime
->silence_filled
< 0) {
81 runtime
->silence_filled
= 0;
82 runtime
->silence_start
= new_hw_ptr
;
84 runtime
->silence_start
= ofs
;
87 frames
= runtime
->buffer_size
- runtime
->silence_filled
;
89 if (snd_BUG_ON(frames
> runtime
->buffer_size
))
93 ofs
= runtime
->silence_start
% runtime
->buffer_size
;
95 transfer
= ofs
+ frames
> runtime
->buffer_size
? runtime
->buffer_size
- ofs
: frames
;
96 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
97 runtime
->access
== SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
) {
98 if (substream
->ops
->silence
) {
100 err
= substream
->ops
->silence(substream
, -1, ofs
, transfer
);
103 char *hwbuf
= runtime
->dma_area
+ frames_to_bytes(runtime
, ofs
);
104 snd_pcm_format_set_silence(runtime
->format
, hwbuf
, transfer
* runtime
->channels
);
108 unsigned int channels
= runtime
->channels
;
109 if (substream
->ops
->silence
) {
110 for (c
= 0; c
< channels
; ++c
) {
112 err
= substream
->ops
->silence(substream
, c
, ofs
, transfer
);
116 size_t dma_csize
= runtime
->dma_bytes
/ channels
;
117 for (c
= 0; c
< channels
; ++c
) {
118 char *hwbuf
= runtime
->dma_area
+ (c
* dma_csize
) + samples_to_bytes(runtime
, ofs
);
119 snd_pcm_format_set_silence(runtime
->format
, hwbuf
, transfer
);
123 runtime
->silence_filled
+= transfer
;
129 static void pcm_debug_name(struct snd_pcm_substream
*substream
,
130 char *name
, size_t len
)
132 snprintf(name
, len
, "pcmC%dD%d%c:%d",
133 substream
->pcm
->card
->number
,
134 substream
->pcm
->device
,
135 substream
->stream
? 'c' : 'p',
139 #define XRUN_DEBUG_BASIC (1<<0)
140 #define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
141 #define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
142 #define XRUN_DEBUG_PERIODUPDATE (1<<3) /* full period update info */
143 #define XRUN_DEBUG_HWPTRUPDATE (1<<4) /* full hwptr update info */
144 #define XRUN_DEBUG_LOG (1<<5) /* show last 10 positions on err */
145 #define XRUN_DEBUG_LOGONCE (1<<6) /* do above only once */
147 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
149 #define xrun_debug(substream, mask) \
150 ((substream)->pstr->xrun_debug & (mask))
152 #define xrun_debug(substream, mask) 0
155 #define dump_stack_on_xrun(substream) do { \
156 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
160 static void xrun(struct snd_pcm_substream
*substream
)
162 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
164 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
)
165 snd_pcm_gettime(runtime
, (struct timespec
*)&runtime
->status
->tstamp
);
166 snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
167 if (xrun_debug(substream
, XRUN_DEBUG_BASIC
)) {
169 pcm_debug_name(substream
, name
, sizeof(name
));
170 snd_printd(KERN_DEBUG
"XRUN: %s\n", name
);
171 dump_stack_on_xrun(substream
);
175 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
176 #define hw_ptr_error(substream, fmt, args...) \
178 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
179 xrun_log_show(substream); \
180 if (printk_ratelimit()) { \
181 snd_printd("PCM: " fmt, ##args); \
183 dump_stack_on_xrun(substream); \
187 #define XRUN_LOG_CNT 10
189 struct hwptr_log_entry
{
190 unsigned long jiffies
;
191 snd_pcm_uframes_t pos
;
192 snd_pcm_uframes_t period_size
;
193 snd_pcm_uframes_t buffer_size
;
194 snd_pcm_uframes_t old_hw_ptr
;
195 snd_pcm_uframes_t hw_ptr_base
;
198 struct snd_pcm_hwptr_log
{
201 struct hwptr_log_entry entries
[XRUN_LOG_CNT
];
204 static void xrun_log(struct snd_pcm_substream
*substream
,
205 snd_pcm_uframes_t pos
)
207 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
208 struct snd_pcm_hwptr_log
*log
= runtime
->hwptr_log
;
209 struct hwptr_log_entry
*entry
;
212 log
= kzalloc(sizeof(*log
), GFP_ATOMIC
);
215 runtime
->hwptr_log
= log
;
217 if (xrun_debug(substream
, XRUN_DEBUG_LOGONCE
) && log
->hit
)
220 entry
= &log
->entries
[log
->idx
];
221 entry
->jiffies
= jiffies
;
223 entry
->period_size
= runtime
->period_size
;
224 entry
->buffer_size
= runtime
->buffer_size
;;
225 entry
->old_hw_ptr
= runtime
->status
->hw_ptr
;
226 entry
->hw_ptr_base
= runtime
->hw_ptr_base
;
227 log
->idx
= (log
->idx
+ 1) % XRUN_LOG_CNT
;
230 static void xrun_log_show(struct snd_pcm_substream
*substream
)
232 struct snd_pcm_hwptr_log
*log
= substream
->runtime
->hwptr_log
;
233 struct hwptr_log_entry
*entry
;
240 if (xrun_debug(substream
, XRUN_DEBUG_LOGONCE
) && log
->hit
)
242 pcm_debug_name(substream
, name
, sizeof(name
));
243 for (cnt
= 0, idx
= log
->idx
; cnt
< XRUN_LOG_CNT
; cnt
++) {
244 entry
= &log
->entries
[idx
];
245 if (entry
->period_size
== 0)
247 snd_printd("hwptr log: %s: j=%lu, pos=%ld/%ld/%ld, "
249 name
, entry
->jiffies
, (unsigned long)entry
->pos
,
250 (unsigned long)entry
->period_size
,
251 (unsigned long)entry
->buffer_size
,
252 (unsigned long)entry
->old_hw_ptr
,
253 (unsigned long)entry
->hw_ptr_base
);
260 #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
262 #define hw_ptr_error(substream, fmt, args...) do { } while (0)
263 #define xrun_log(substream, pos) do { } while (0)
264 #define xrun_log_show(substream) do { } while (0)
268 int snd_pcm_update_state(struct snd_pcm_substream
*substream
,
269 struct snd_pcm_runtime
*runtime
)
271 snd_pcm_uframes_t avail
;
273 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
274 avail
= snd_pcm_playback_avail(runtime
);
276 avail
= snd_pcm_capture_avail(runtime
);
277 if (avail
> runtime
->avail_max
)
278 runtime
->avail_max
= avail
;
279 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
280 if (avail
>= runtime
->buffer_size
) {
281 snd_pcm_drain_done(substream
);
285 if (avail
>= runtime
->stop_threshold
) {
290 if (avail
>= runtime
->control
->avail_min
)
291 wake_up(runtime
->twake
? &runtime
->tsleep
: &runtime
->sleep
);
295 static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream
*substream
,
296 unsigned int in_interrupt
)
298 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
299 snd_pcm_uframes_t pos
;
300 snd_pcm_uframes_t old_hw_ptr
, new_hw_ptr
, hw_base
;
301 snd_pcm_sframes_t hdelta
, delta
;
302 unsigned long jdelta
;
304 old_hw_ptr
= runtime
->status
->hw_ptr
;
305 pos
= substream
->ops
->pointer(substream
);
306 if (pos
== SNDRV_PCM_POS_XRUN
) {
310 if (pos
>= runtime
->buffer_size
) {
311 if (printk_ratelimit()) {
313 pcm_debug_name(substream
, name
, sizeof(name
));
314 xrun_log_show(substream
);
315 snd_printd(KERN_ERR
"BUG: %s, pos = %ld, "
316 "buffer size = %ld, period size = %ld\n",
317 name
, pos
, runtime
->buffer_size
,
318 runtime
->period_size
);
322 pos
-= pos
% runtime
->min_align
;
323 if (xrun_debug(substream
, XRUN_DEBUG_LOG
))
324 xrun_log(substream
, pos
);
325 hw_base
= runtime
->hw_ptr_base
;
326 new_hw_ptr
= hw_base
+ pos
;
328 /* we know that one period was processed */
329 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
330 delta
= runtime
->hw_ptr_interrupt
+ runtime
->period_size
;
331 if (delta
> new_hw_ptr
) {
332 hw_base
+= runtime
->buffer_size
;
333 if (hw_base
>= runtime
->boundary
)
335 new_hw_ptr
= hw_base
+ pos
;
339 /* new_hw_ptr might be lower than old_hw_ptr in case when */
340 /* pointer crosses the end of the ring buffer */
341 if (new_hw_ptr
< old_hw_ptr
) {
342 hw_base
+= runtime
->buffer_size
;
343 if (hw_base
>= runtime
->boundary
)
345 new_hw_ptr
= hw_base
+ pos
;
348 delta
= (new_hw_ptr
- old_hw_ptr
) % runtime
->boundary
;
349 if (xrun_debug(substream
, in_interrupt
?
350 XRUN_DEBUG_PERIODUPDATE
: XRUN_DEBUG_HWPTRUPDATE
)) {
352 pcm_debug_name(substream
, name
, sizeof(name
));
353 snd_printd("%s_update: %s: pos=%u/%u/%u, "
354 "hwptr=%ld/%ld/%ld/%ld\n",
355 in_interrupt
? "period" : "hwptr",
358 (unsigned int)runtime
->period_size
,
359 (unsigned int)runtime
->buffer_size
,
360 (unsigned long)delta
,
361 (unsigned long)old_hw_ptr
,
362 (unsigned long)new_hw_ptr
,
363 (unsigned long)runtime
->hw_ptr_base
);
365 /* something must be really wrong */
366 if (delta
>= runtime
->buffer_size
+ runtime
->period_size
) {
367 hw_ptr_error(substream
,
368 "Unexpected hw_pointer value %s"
369 "(stream=%i, pos=%ld, new_hw_ptr=%ld, "
371 in_interrupt
? "[Q] " : "[P]",
372 substream
->stream
, (long)pos
,
373 (long)new_hw_ptr
, (long)old_hw_ptr
);
377 /* Do jiffies check only in xrun_debug mode */
378 if (!xrun_debug(substream
, XRUN_DEBUG_JIFFIESCHECK
))
379 goto no_jiffies_check
;
381 /* Skip the jiffies check for hardwares with BATCH flag.
382 * Such hardware usually just increases the position at each IRQ,
383 * thus it can't give any strange position.
385 if (runtime
->hw
.info
& SNDRV_PCM_INFO_BATCH
)
386 goto no_jiffies_check
;
388 if (hdelta
< runtime
->delay
)
389 goto no_jiffies_check
;
390 hdelta
-= runtime
->delay
;
391 jdelta
= jiffies
- runtime
->hw_ptr_jiffies
;
392 if (((hdelta
* HZ
) / runtime
->rate
) > jdelta
+ HZ
/100) {
394 (((runtime
->period_size
* HZ
) / runtime
->rate
)
396 /* move new_hw_ptr according jiffies not pos variable */
397 new_hw_ptr
= old_hw_ptr
;
399 /* use loop to avoid checks for delta overflows */
400 /* the delta value is small or zero in most cases */
402 new_hw_ptr
+= runtime
->period_size
;
403 if (new_hw_ptr
>= runtime
->boundary
)
404 new_hw_ptr
-= runtime
->boundary
;
407 /* align hw_base to buffer_size */
408 hw_ptr_error(substream
,
409 "hw_ptr skipping! %s"
410 "(pos=%ld, delta=%ld, period=%ld, "
411 "jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
412 in_interrupt
? "[Q] " : "",
413 (long)pos
, (long)hdelta
,
414 (long)runtime
->period_size
, jdelta
,
415 ((hdelta
* HZ
) / runtime
->rate
), hw_base
,
416 (unsigned long)old_hw_ptr
,
417 (unsigned long)new_hw_ptr
);
418 /* reset values to proper state */
420 hw_base
= new_hw_ptr
- (new_hw_ptr
% runtime
->buffer_size
);
423 if (delta
> runtime
->period_size
+ runtime
->period_size
/ 2) {
424 hw_ptr_error(substream
,
425 "Lost interrupts? %s"
426 "(stream=%i, delta=%ld, new_hw_ptr=%ld, "
428 in_interrupt
? "[Q] " : "",
429 substream
->stream
, (long)delta
,
434 if (runtime
->status
->hw_ptr
== new_hw_ptr
)
437 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
438 runtime
->silence_size
> 0)
439 snd_pcm_playback_silence(substream
, new_hw_ptr
);
442 runtime
->hw_ptr_interrupt
= new_hw_ptr
-
443 (new_hw_ptr
% runtime
->period_size
);
445 runtime
->hw_ptr_base
= hw_base
;
446 runtime
->status
->hw_ptr
= new_hw_ptr
;
447 runtime
->hw_ptr_jiffies
= jiffies
;
448 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
)
449 snd_pcm_gettime(runtime
, (struct timespec
*)&runtime
->status
->tstamp
);
451 return snd_pcm_update_state(substream
, runtime
);
454 /* CAUTION: call it with irq disabled */
455 int snd_pcm_update_hw_ptr(struct snd_pcm_substream
*substream
)
457 return snd_pcm_update_hw_ptr0(substream
, 0);
461 * snd_pcm_set_ops - set the PCM operators
462 * @pcm: the pcm instance
463 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
464 * @ops: the operator table
466 * Sets the given PCM operators to the pcm instance.
468 void snd_pcm_set_ops(struct snd_pcm
*pcm
, int direction
, struct snd_pcm_ops
*ops
)
470 struct snd_pcm_str
*stream
= &pcm
->streams
[direction
];
471 struct snd_pcm_substream
*substream
;
473 for (substream
= stream
->substream
; substream
!= NULL
; substream
= substream
->next
)
474 substream
->ops
= ops
;
477 EXPORT_SYMBOL(snd_pcm_set_ops
);
480 * snd_pcm_sync - set the PCM sync id
481 * @substream: the pcm substream
483 * Sets the PCM sync identifier for the card.
485 void snd_pcm_set_sync(struct snd_pcm_substream
*substream
)
487 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
489 runtime
->sync
.id32
[0] = substream
->pcm
->card
->number
;
490 runtime
->sync
.id32
[1] = -1;
491 runtime
->sync
.id32
[2] = -1;
492 runtime
->sync
.id32
[3] = -1;
495 EXPORT_SYMBOL(snd_pcm_set_sync
);
498 * Standard ioctl routine
501 static inline unsigned int div32(unsigned int a
, unsigned int b
,
512 static inline unsigned int div_down(unsigned int a
, unsigned int b
)
519 static inline unsigned int div_up(unsigned int a
, unsigned int b
)
531 static inline unsigned int mul(unsigned int a
, unsigned int b
)
535 if (div_down(UINT_MAX
, a
) < b
)
540 static inline unsigned int muldiv32(unsigned int a
, unsigned int b
,
541 unsigned int c
, unsigned int *r
)
543 u_int64_t n
= (u_int64_t
) a
* b
;
549 n
= div_u64_rem(n
, c
, r
);
558 * snd_interval_refine - refine the interval value of configurator
559 * @i: the interval value to refine
560 * @v: the interval value to refer to
562 * Refines the interval value with the reference value.
563 * The interval is changed to the range satisfying both intervals.
564 * The interval status (min, max, integer, etc.) are evaluated.
566 * Returns non-zero if the value is changed, zero if not changed.
568 int snd_interval_refine(struct snd_interval
*i
, const struct snd_interval
*v
)
571 if (snd_BUG_ON(snd_interval_empty(i
)))
573 if (i
->min
< v
->min
) {
575 i
->openmin
= v
->openmin
;
577 } else if (i
->min
== v
->min
&& !i
->openmin
&& v
->openmin
) {
581 if (i
->max
> v
->max
) {
583 i
->openmax
= v
->openmax
;
585 } else if (i
->max
== v
->max
&& !i
->openmax
&& v
->openmax
) {
589 if (!i
->integer
&& v
->integer
) {
602 } else if (!i
->openmin
&& !i
->openmax
&& i
->min
== i
->max
)
604 if (snd_interval_checkempty(i
)) {
605 snd_interval_none(i
);
611 EXPORT_SYMBOL(snd_interval_refine
);
613 static int snd_interval_refine_first(struct snd_interval
*i
)
615 if (snd_BUG_ON(snd_interval_empty(i
)))
617 if (snd_interval_single(i
))
620 i
->openmax
= i
->openmin
;
626 static int snd_interval_refine_last(struct snd_interval
*i
)
628 if (snd_BUG_ON(snd_interval_empty(i
)))
630 if (snd_interval_single(i
))
633 i
->openmin
= i
->openmax
;
639 void snd_interval_mul(const struct snd_interval
*a
, const struct snd_interval
*b
, struct snd_interval
*c
)
641 if (a
->empty
|| b
->empty
) {
642 snd_interval_none(c
);
646 c
->min
= mul(a
->min
, b
->min
);
647 c
->openmin
= (a
->openmin
|| b
->openmin
);
648 c
->max
= mul(a
->max
, b
->max
);
649 c
->openmax
= (a
->openmax
|| b
->openmax
);
650 c
->integer
= (a
->integer
&& b
->integer
);
654 * snd_interval_div - refine the interval value with division
661 * Returns non-zero if the value is changed, zero if not changed.
663 void snd_interval_div(const struct snd_interval
*a
, const struct snd_interval
*b
, struct snd_interval
*c
)
666 if (a
->empty
|| b
->empty
) {
667 snd_interval_none(c
);
671 c
->min
= div32(a
->min
, b
->max
, &r
);
672 c
->openmin
= (r
|| a
->openmin
|| b
->openmax
);
674 c
->max
= div32(a
->max
, b
->min
, &r
);
679 c
->openmax
= (a
->openmax
|| b
->openmin
);
688 * snd_interval_muldivk - refine the interval value
691 * @k: divisor (as integer)
696 * Returns non-zero if the value is changed, zero if not changed.
698 void snd_interval_muldivk(const struct snd_interval
*a
, const struct snd_interval
*b
,
699 unsigned int k
, struct snd_interval
*c
)
702 if (a
->empty
|| b
->empty
) {
703 snd_interval_none(c
);
707 c
->min
= muldiv32(a
->min
, b
->min
, k
, &r
);
708 c
->openmin
= (r
|| a
->openmin
|| b
->openmin
);
709 c
->max
= muldiv32(a
->max
, b
->max
, k
, &r
);
714 c
->openmax
= (a
->openmax
|| b
->openmax
);
719 * snd_interval_mulkdiv - refine the interval value
721 * @k: dividend 2 (as integer)
727 * Returns non-zero if the value is changed, zero if not changed.
729 void snd_interval_mulkdiv(const struct snd_interval
*a
, unsigned int k
,
730 const struct snd_interval
*b
, struct snd_interval
*c
)
733 if (a
->empty
|| b
->empty
) {
734 snd_interval_none(c
);
738 c
->min
= muldiv32(a
->min
, k
, b
->max
, &r
);
739 c
->openmin
= (r
|| a
->openmin
|| b
->openmax
);
741 c
->max
= muldiv32(a
->max
, k
, b
->min
, &r
);
746 c
->openmax
= (a
->openmax
|| b
->openmin
);
758 * snd_interval_ratnum - refine the interval value
759 * @i: interval to refine
760 * @rats_count: number of ratnum_t
761 * @rats: ratnum_t array
762 * @nump: pointer to store the resultant numerator
763 * @denp: pointer to store the resultant denominator
765 * Returns non-zero if the value is changed, zero if not changed.
767 int snd_interval_ratnum(struct snd_interval
*i
,
768 unsigned int rats_count
, struct snd_ratnum
*rats
,
769 unsigned int *nump
, unsigned int *denp
)
771 unsigned int best_num
, best_den
;
774 struct snd_interval t
;
776 unsigned int result_num
, result_den
;
779 best_num
= best_den
= best_diff
= 0;
780 for (k
= 0; k
< rats_count
; ++k
) {
781 unsigned int num
= rats
[k
].num
;
783 unsigned int q
= i
->min
;
787 den
= div_up(num
, q
);
788 if (den
< rats
[k
].den_min
)
790 if (den
> rats
[k
].den_max
)
791 den
= rats
[k
].den_max
;
794 r
= (den
- rats
[k
].den_min
) % rats
[k
].den_step
;
798 diff
= num
- q
* den
;
802 diff
* best_den
< best_diff
* den
) {
812 t
.min
= div_down(best_num
, best_den
);
813 t
.openmin
= !!(best_num
% best_den
);
815 result_num
= best_num
;
816 result_diff
= best_diff
;
817 result_den
= best_den
;
818 best_num
= best_den
= best_diff
= 0;
819 for (k
= 0; k
< rats_count
; ++k
) {
820 unsigned int num
= rats
[k
].num
;
822 unsigned int q
= i
->max
;
828 den
= div_down(num
, q
);
829 if (den
> rats
[k
].den_max
)
831 if (den
< rats
[k
].den_min
)
832 den
= rats
[k
].den_min
;
835 r
= (den
- rats
[k
].den_min
) % rats
[k
].den_step
;
837 den
+= rats
[k
].den_step
- r
;
839 diff
= q
* den
- num
;
843 diff
* best_den
< best_diff
* den
) {
853 t
.max
= div_up(best_num
, best_den
);
854 t
.openmax
= !!(best_num
% best_den
);
856 err
= snd_interval_refine(i
, &t
);
860 if (snd_interval_single(i
)) {
861 if (best_diff
* result_den
< result_diff
* best_den
) {
862 result_num
= best_num
;
863 result_den
= best_den
;
873 EXPORT_SYMBOL(snd_interval_ratnum
);
876 * snd_interval_ratden - refine the interval value
877 * @i: interval to refine
878 * @rats_count: number of struct ratden
879 * @rats: struct ratden array
880 * @nump: pointer to store the resultant numerator
881 * @denp: pointer to store the resultant denominator
883 * Returns non-zero if the value is changed, zero if not changed.
885 static int snd_interval_ratden(struct snd_interval
*i
,
886 unsigned int rats_count
, struct snd_ratden
*rats
,
887 unsigned int *nump
, unsigned int *denp
)
889 unsigned int best_num
, best_diff
, best_den
;
891 struct snd_interval t
;
894 best_num
= best_den
= best_diff
= 0;
895 for (k
= 0; k
< rats_count
; ++k
) {
897 unsigned int den
= rats
[k
].den
;
898 unsigned int q
= i
->min
;
901 if (num
> rats
[k
].num_max
)
903 if (num
< rats
[k
].num_min
)
904 num
= rats
[k
].num_max
;
907 r
= (num
- rats
[k
].num_min
) % rats
[k
].num_step
;
909 num
+= rats
[k
].num_step
- r
;
911 diff
= num
- q
* den
;
913 diff
* best_den
< best_diff
* den
) {
923 t
.min
= div_down(best_num
, best_den
);
924 t
.openmin
= !!(best_num
% best_den
);
926 best_num
= best_den
= best_diff
= 0;
927 for (k
= 0; k
< rats_count
; ++k
) {
929 unsigned int den
= rats
[k
].den
;
930 unsigned int q
= i
->max
;
933 if (num
< rats
[k
].num_min
)
935 if (num
> rats
[k
].num_max
)
936 num
= rats
[k
].num_max
;
939 r
= (num
- rats
[k
].num_min
) % rats
[k
].num_step
;
943 diff
= q
* den
- num
;
945 diff
* best_den
< best_diff
* den
) {
955 t
.max
= div_up(best_num
, best_den
);
956 t
.openmax
= !!(best_num
% best_den
);
958 err
= snd_interval_refine(i
, &t
);
962 if (snd_interval_single(i
)) {
972 * snd_interval_list - refine the interval value from the list
973 * @i: the interval value to refine
974 * @count: the number of elements in the list
975 * @list: the value list
976 * @mask: the bit-mask to evaluate
978 * Refines the interval value from the list.
979 * When mask is non-zero, only the elements corresponding to bit 1 are
982 * Returns non-zero if the value is changed, zero if not changed.
984 int snd_interval_list(struct snd_interval
*i
, unsigned int count
, unsigned int *list
, unsigned int mask
)
987 struct snd_interval list_range
;
993 snd_interval_any(&list_range
);
994 list_range
.min
= UINT_MAX
;
996 for (k
= 0; k
< count
; k
++) {
997 if (mask
&& !(mask
& (1 << k
)))
999 if (!snd_interval_test(i
, list
[k
]))
1001 list_range
.min
= min(list_range
.min
, list
[k
]);
1002 list_range
.max
= max(list_range
.max
, list
[k
]);
1004 return snd_interval_refine(i
, &list_range
);
1007 EXPORT_SYMBOL(snd_interval_list
);
1009 static int snd_interval_step(struct snd_interval
*i
, unsigned int min
, unsigned int step
)
1013 n
= (i
->min
- min
) % step
;
1014 if (n
!= 0 || i
->openmin
) {
1018 n
= (i
->max
- min
) % step
;
1019 if (n
!= 0 || i
->openmax
) {
1023 if (snd_interval_checkempty(i
)) {
1030 /* Info constraints helpers */
1033 * snd_pcm_hw_rule_add - add the hw-constraint rule
1034 * @runtime: the pcm runtime instance
1035 * @cond: condition bits
1036 * @var: the variable to evaluate
1037 * @func: the evaluation function
1038 * @private: the private data pointer passed to function
1039 * @dep: the dependent variables
1041 * Returns zero if successful, or a negative error code on failure.
1043 int snd_pcm_hw_rule_add(struct snd_pcm_runtime
*runtime
, unsigned int cond
,
1045 snd_pcm_hw_rule_func_t func
, void *private,
1048 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1049 struct snd_pcm_hw_rule
*c
;
1052 va_start(args
, dep
);
1053 if (constrs
->rules_num
>= constrs
->rules_all
) {
1054 struct snd_pcm_hw_rule
*new;
1055 unsigned int new_rules
= constrs
->rules_all
+ 16;
1056 new = kcalloc(new_rules
, sizeof(*c
), GFP_KERNEL
);
1059 if (constrs
->rules
) {
1060 memcpy(new, constrs
->rules
,
1061 constrs
->rules_num
* sizeof(*c
));
1062 kfree(constrs
->rules
);
1064 constrs
->rules
= new;
1065 constrs
->rules_all
= new_rules
;
1067 c
= &constrs
->rules
[constrs
->rules_num
];
1071 c
->private = private;
1074 if (snd_BUG_ON(k
>= ARRAY_SIZE(c
->deps
)))
1079 dep
= va_arg(args
, int);
1081 constrs
->rules_num
++;
1086 EXPORT_SYMBOL(snd_pcm_hw_rule_add
);
1089 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1090 * @runtime: PCM runtime instance
1091 * @var: hw_params variable to apply the mask
1092 * @mask: the bitmap mask
1094 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1096 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
,
1099 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1100 struct snd_mask
*maskp
= constrs_mask(constrs
, var
);
1101 *maskp
->bits
&= mask
;
1102 memset(maskp
->bits
+ 1, 0, (SNDRV_MASK_MAX
-32) / 8); /* clear rest */
1103 if (*maskp
->bits
== 0)
1109 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1110 * @runtime: PCM runtime instance
1111 * @var: hw_params variable to apply the mask
1112 * @mask: the 64bit bitmap mask
1114 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1116 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
,
1119 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1120 struct snd_mask
*maskp
= constrs_mask(constrs
, var
);
1121 maskp
->bits
[0] &= (u_int32_t
)mask
;
1122 maskp
->bits
[1] &= (u_int32_t
)(mask
>> 32);
1123 memset(maskp
->bits
+ 2, 0, (SNDRV_MASK_MAX
-64) / 8); /* clear rest */
1124 if (! maskp
->bits
[0] && ! maskp
->bits
[1])
1130 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1131 * @runtime: PCM runtime instance
1132 * @var: hw_params variable to apply the integer constraint
1134 * Apply the constraint of integer to an interval parameter.
1136 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
)
1138 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1139 return snd_interval_setinteger(constrs_interval(constrs
, var
));
1142 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer
);
1145 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1146 * @runtime: PCM runtime instance
1147 * @var: hw_params variable to apply the range
1148 * @min: the minimal value
1149 * @max: the maximal value
1151 * Apply the min/max range constraint to an interval parameter.
1153 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
,
1154 unsigned int min
, unsigned int max
)
1156 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1157 struct snd_interval t
;
1160 t
.openmin
= t
.openmax
= 0;
1162 return snd_interval_refine(constrs_interval(constrs
, var
), &t
);
1165 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax
);
1167 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params
*params
,
1168 struct snd_pcm_hw_rule
*rule
)
1170 struct snd_pcm_hw_constraint_list
*list
= rule
->private;
1171 return snd_interval_list(hw_param_interval(params
, rule
->var
), list
->count
, list
->list
, list
->mask
);
1176 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1177 * @runtime: PCM runtime instance
1178 * @cond: condition bits
1179 * @var: hw_params variable to apply the list constraint
1182 * Apply the list of constraints to an interval parameter.
1184 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime
*runtime
,
1186 snd_pcm_hw_param_t var
,
1187 struct snd_pcm_hw_constraint_list
*l
)
1189 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1190 snd_pcm_hw_rule_list
, l
,
1194 EXPORT_SYMBOL(snd_pcm_hw_constraint_list
);
1196 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params
*params
,
1197 struct snd_pcm_hw_rule
*rule
)
1199 struct snd_pcm_hw_constraint_ratnums
*r
= rule
->private;
1200 unsigned int num
= 0, den
= 0;
1202 err
= snd_interval_ratnum(hw_param_interval(params
, rule
->var
),
1203 r
->nrats
, r
->rats
, &num
, &den
);
1204 if (err
>= 0 && den
&& rule
->var
== SNDRV_PCM_HW_PARAM_RATE
) {
1205 params
->rate_num
= num
;
1206 params
->rate_den
= den
;
1212 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1213 * @runtime: PCM runtime instance
1214 * @cond: condition bits
1215 * @var: hw_params variable to apply the ratnums constraint
1216 * @r: struct snd_ratnums constriants
1218 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime
*runtime
,
1220 snd_pcm_hw_param_t var
,
1221 struct snd_pcm_hw_constraint_ratnums
*r
)
1223 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1224 snd_pcm_hw_rule_ratnums
, r
,
1228 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums
);
1230 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params
*params
,
1231 struct snd_pcm_hw_rule
*rule
)
1233 struct snd_pcm_hw_constraint_ratdens
*r
= rule
->private;
1234 unsigned int num
= 0, den
= 0;
1235 int err
= snd_interval_ratden(hw_param_interval(params
, rule
->var
),
1236 r
->nrats
, r
->rats
, &num
, &den
);
1237 if (err
>= 0 && den
&& rule
->var
== SNDRV_PCM_HW_PARAM_RATE
) {
1238 params
->rate_num
= num
;
1239 params
->rate_den
= den
;
1245 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1246 * @runtime: PCM runtime instance
1247 * @cond: condition bits
1248 * @var: hw_params variable to apply the ratdens constraint
1249 * @r: struct snd_ratdens constriants
1251 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime
*runtime
,
1253 snd_pcm_hw_param_t var
,
1254 struct snd_pcm_hw_constraint_ratdens
*r
)
1256 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1257 snd_pcm_hw_rule_ratdens
, r
,
1261 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens
);
1263 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params
*params
,
1264 struct snd_pcm_hw_rule
*rule
)
1266 unsigned int l
= (unsigned long) rule
->private;
1267 int width
= l
& 0xffff;
1268 unsigned int msbits
= l
>> 16;
1269 struct snd_interval
*i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
1270 if (snd_interval_single(i
) && snd_interval_value(i
) == width
)
1271 params
->msbits
= msbits
;
1276 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1277 * @runtime: PCM runtime instance
1278 * @cond: condition bits
1279 * @width: sample bits width
1280 * @msbits: msbits width
1282 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime
*runtime
,
1285 unsigned int msbits
)
1287 unsigned long l
= (msbits
<< 16) | width
;
1288 return snd_pcm_hw_rule_add(runtime
, cond
, -1,
1289 snd_pcm_hw_rule_msbits
,
1291 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1294 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits
);
1296 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params
*params
,
1297 struct snd_pcm_hw_rule
*rule
)
1299 unsigned long step
= (unsigned long) rule
->private;
1300 return snd_interval_step(hw_param_interval(params
, rule
->var
), 0, step
);
1304 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1305 * @runtime: PCM runtime instance
1306 * @cond: condition bits
1307 * @var: hw_params variable to apply the step constraint
1310 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime
*runtime
,
1312 snd_pcm_hw_param_t var
,
1315 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1316 snd_pcm_hw_rule_step
, (void *) step
,
1320 EXPORT_SYMBOL(snd_pcm_hw_constraint_step
);
1322 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params
*params
, struct snd_pcm_hw_rule
*rule
)
1324 static unsigned int pow2_sizes
[] = {
1325 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1326 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1327 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1328 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1330 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1331 ARRAY_SIZE(pow2_sizes
), pow2_sizes
, 0);
1335 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1336 * @runtime: PCM runtime instance
1337 * @cond: condition bits
1338 * @var: hw_params variable to apply the power-of-2 constraint
1340 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime
*runtime
,
1342 snd_pcm_hw_param_t var
)
1344 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1345 snd_pcm_hw_rule_pow2
, NULL
,
1349 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2
);
1351 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params
*params
,
1352 snd_pcm_hw_param_t var
)
1354 if (hw_is_mask(var
)) {
1355 snd_mask_any(hw_param_mask(params
, var
));
1356 params
->cmask
|= 1 << var
;
1357 params
->rmask
|= 1 << var
;
1360 if (hw_is_interval(var
)) {
1361 snd_interval_any(hw_param_interval(params
, var
));
1362 params
->cmask
|= 1 << var
;
1363 params
->rmask
|= 1 << var
;
1369 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params
*params
)
1372 memset(params
, 0, sizeof(*params
));
1373 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++)
1374 _snd_pcm_hw_param_any(params
, k
);
1375 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
1376 _snd_pcm_hw_param_any(params
, k
);
1380 EXPORT_SYMBOL(_snd_pcm_hw_params_any
);
1383 * snd_pcm_hw_param_value - return @params field @var value
1384 * @params: the hw_params instance
1385 * @var: parameter to retrieve
1386 * @dir: pointer to the direction (-1,0,1) or %NULL
1388 * Return the value for field @var if it's fixed in configuration space
1389 * defined by @params. Return -%EINVAL otherwise.
1391 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params
*params
,
1392 snd_pcm_hw_param_t var
, int *dir
)
1394 if (hw_is_mask(var
)) {
1395 const struct snd_mask
*mask
= hw_param_mask_c(params
, var
);
1396 if (!snd_mask_single(mask
))
1400 return snd_mask_value(mask
);
1402 if (hw_is_interval(var
)) {
1403 const struct snd_interval
*i
= hw_param_interval_c(params
, var
);
1404 if (!snd_interval_single(i
))
1408 return snd_interval_value(i
);
1413 EXPORT_SYMBOL(snd_pcm_hw_param_value
);
1415 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params
*params
,
1416 snd_pcm_hw_param_t var
)
1418 if (hw_is_mask(var
)) {
1419 snd_mask_none(hw_param_mask(params
, var
));
1420 params
->cmask
|= 1 << var
;
1421 params
->rmask
|= 1 << var
;
1422 } else if (hw_is_interval(var
)) {
1423 snd_interval_none(hw_param_interval(params
, var
));
1424 params
->cmask
|= 1 << var
;
1425 params
->rmask
|= 1 << var
;
1431 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty
);
1433 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params
*params
,
1434 snd_pcm_hw_param_t var
)
1437 if (hw_is_mask(var
))
1438 changed
= snd_mask_refine_first(hw_param_mask(params
, var
));
1439 else if (hw_is_interval(var
))
1440 changed
= snd_interval_refine_first(hw_param_interval(params
, var
));
1444 params
->cmask
|= 1 << var
;
1445 params
->rmask
|= 1 << var
;
1452 * snd_pcm_hw_param_first - refine config space and return minimum value
1453 * @pcm: PCM instance
1454 * @params: the hw_params instance
1455 * @var: parameter to retrieve
1456 * @dir: pointer to the direction (-1,0,1) or %NULL
1458 * Inside configuration space defined by @params remove from @var all
1459 * values > minimum. Reduce configuration space accordingly.
1460 * Return the minimum.
1462 int snd_pcm_hw_param_first(struct snd_pcm_substream
*pcm
,
1463 struct snd_pcm_hw_params
*params
,
1464 snd_pcm_hw_param_t var
, int *dir
)
1466 int changed
= _snd_pcm_hw_param_first(params
, var
);
1469 if (params
->rmask
) {
1470 int err
= snd_pcm_hw_refine(pcm
, params
);
1471 if (snd_BUG_ON(err
< 0))
1474 return snd_pcm_hw_param_value(params
, var
, dir
);
1477 EXPORT_SYMBOL(snd_pcm_hw_param_first
);
1479 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params
*params
,
1480 snd_pcm_hw_param_t var
)
1483 if (hw_is_mask(var
))
1484 changed
= snd_mask_refine_last(hw_param_mask(params
, var
));
1485 else if (hw_is_interval(var
))
1486 changed
= snd_interval_refine_last(hw_param_interval(params
, var
));
1490 params
->cmask
|= 1 << var
;
1491 params
->rmask
|= 1 << var
;
1498 * snd_pcm_hw_param_last - refine config space and return maximum value
1499 * @pcm: PCM instance
1500 * @params: the hw_params instance
1501 * @var: parameter to retrieve
1502 * @dir: pointer to the direction (-1,0,1) or %NULL
1504 * Inside configuration space defined by @params remove from @var all
1505 * values < maximum. Reduce configuration space accordingly.
1506 * Return the maximum.
1508 int snd_pcm_hw_param_last(struct snd_pcm_substream
*pcm
,
1509 struct snd_pcm_hw_params
*params
,
1510 snd_pcm_hw_param_t var
, int *dir
)
1512 int changed
= _snd_pcm_hw_param_last(params
, var
);
1515 if (params
->rmask
) {
1516 int err
= snd_pcm_hw_refine(pcm
, params
);
1517 if (snd_BUG_ON(err
< 0))
1520 return snd_pcm_hw_param_value(params
, var
, dir
);
1523 EXPORT_SYMBOL(snd_pcm_hw_param_last
);
1526 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1527 * @pcm: PCM instance
1528 * @params: the hw_params instance
1530 * Choose one configuration from configuration space defined by @params.
1531 * The configuration chosen is that obtained fixing in this order:
1532 * first access, first format, first subformat, min channels,
1533 * min rate, min period time, max buffer size, min tick time
1535 int snd_pcm_hw_params_choose(struct snd_pcm_substream
*pcm
,
1536 struct snd_pcm_hw_params
*params
)
1538 static int vars
[] = {
1539 SNDRV_PCM_HW_PARAM_ACCESS
,
1540 SNDRV_PCM_HW_PARAM_FORMAT
,
1541 SNDRV_PCM_HW_PARAM_SUBFORMAT
,
1542 SNDRV_PCM_HW_PARAM_CHANNELS
,
1543 SNDRV_PCM_HW_PARAM_RATE
,
1544 SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1545 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1546 SNDRV_PCM_HW_PARAM_TICK_TIME
,
1551 for (v
= vars
; *v
!= -1; v
++) {
1552 if (*v
!= SNDRV_PCM_HW_PARAM_BUFFER_SIZE
)
1553 err
= snd_pcm_hw_param_first(pcm
, params
, *v
, NULL
);
1555 err
= snd_pcm_hw_param_last(pcm
, params
, *v
, NULL
);
1556 if (snd_BUG_ON(err
< 0))
1562 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream
*substream
,
1565 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1566 unsigned long flags
;
1567 snd_pcm_stream_lock_irqsave(substream
, flags
);
1568 if (snd_pcm_running(substream
) &&
1569 snd_pcm_update_hw_ptr(substream
) >= 0)
1570 runtime
->status
->hw_ptr
%= runtime
->buffer_size
;
1572 runtime
->status
->hw_ptr
= 0;
1573 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1577 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream
*substream
,
1580 struct snd_pcm_channel_info
*info
= arg
;
1581 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1583 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
)) {
1587 width
= snd_pcm_format_physical_width(runtime
->format
);
1591 switch (runtime
->access
) {
1592 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
:
1593 case SNDRV_PCM_ACCESS_RW_INTERLEAVED
:
1594 info
->first
= info
->channel
* width
;
1595 info
->step
= runtime
->channels
* width
;
1597 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
:
1598 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
:
1600 size_t size
= runtime
->dma_bytes
/ runtime
->channels
;
1601 info
->first
= info
->channel
* size
* 8;
1612 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream
*substream
,
1615 struct snd_pcm_hw_params
*params
= arg
;
1616 snd_pcm_format_t format
;
1617 int channels
, width
;
1619 params
->fifo_size
= substream
->runtime
->hw
.fifo_size
;
1620 if (!(substream
->runtime
->hw
.info
& SNDRV_PCM_INFO_FIFO_IN_FRAMES
)) {
1621 format
= params_format(params
);
1622 channels
= params_channels(params
);
1623 width
= snd_pcm_format_physical_width(format
);
1624 params
->fifo_size
/= width
* channels
;
1630 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1631 * @substream: the pcm substream instance
1632 * @cmd: ioctl command
1633 * @arg: ioctl argument
1635 * Processes the generic ioctl commands for PCM.
1636 * Can be passed as the ioctl callback for PCM ops.
1638 * Returns zero if successful, or a negative error code on failure.
1640 int snd_pcm_lib_ioctl(struct snd_pcm_substream
*substream
,
1641 unsigned int cmd
, void *arg
)
1644 case SNDRV_PCM_IOCTL1_INFO
:
1646 case SNDRV_PCM_IOCTL1_RESET
:
1647 return snd_pcm_lib_ioctl_reset(substream
, arg
);
1648 case SNDRV_PCM_IOCTL1_CHANNEL_INFO
:
1649 return snd_pcm_lib_ioctl_channel_info(substream
, arg
);
1650 case SNDRV_PCM_IOCTL1_FIFO_SIZE
:
1651 return snd_pcm_lib_ioctl_fifo_size(substream
, arg
);
1656 EXPORT_SYMBOL(snd_pcm_lib_ioctl
);
1659 * snd_pcm_period_elapsed - update the pcm status for the next period
1660 * @substream: the pcm substream instance
1662 * This function is called from the interrupt handler when the
1663 * PCM has processed the period size. It will update the current
1664 * pointer, wake up sleepers, etc.
1666 * Even if more than one periods have elapsed since the last call, you
1667 * have to call this only once.
1669 void snd_pcm_period_elapsed(struct snd_pcm_substream
*substream
)
1671 struct snd_pcm_runtime
*runtime
;
1672 unsigned long flags
;
1674 if (PCM_RUNTIME_CHECK(substream
))
1676 runtime
= substream
->runtime
;
1678 if (runtime
->transfer_ack_begin
)
1679 runtime
->transfer_ack_begin(substream
);
1681 snd_pcm_stream_lock_irqsave(substream
, flags
);
1682 if (!snd_pcm_running(substream
) ||
1683 snd_pcm_update_hw_ptr0(substream
, 1) < 0)
1686 if (substream
->timer_running
)
1687 snd_timer_interrupt(substream
->timer
, 1);
1689 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1690 if (runtime
->transfer_ack_end
)
1691 runtime
->transfer_ack_end(substream
);
1692 kill_fasync(&runtime
->fasync
, SIGIO
, POLL_IN
);
1695 EXPORT_SYMBOL(snd_pcm_period_elapsed
);
1698 * Wait until avail_min data becomes available
1699 * Returns a negative error code if any error occurs during operation.
1700 * The available space is stored on availp. When err = 0 and avail = 0
1701 * on the capture stream, it indicates the stream is in DRAINING state.
1703 static int wait_for_avail_min(struct snd_pcm_substream
*substream
,
1704 snd_pcm_uframes_t
*availp
)
1706 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1707 int is_playback
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
1710 snd_pcm_uframes_t avail
= 0;
1713 init_waitqueue_entry(&wait
, current
);
1714 add_wait_queue(&runtime
->tsleep
, &wait
);
1716 if (signal_pending(current
)) {
1720 set_current_state(TASK_INTERRUPTIBLE
);
1721 snd_pcm_stream_unlock_irq(substream
);
1722 tout
= schedule_timeout(msecs_to_jiffies(10000));
1723 snd_pcm_stream_lock_irq(substream
);
1724 switch (runtime
->status
->state
) {
1725 case SNDRV_PCM_STATE_SUSPENDED
:
1728 case SNDRV_PCM_STATE_XRUN
:
1731 case SNDRV_PCM_STATE_DRAINING
:
1735 avail
= 0; /* indicate draining */
1737 case SNDRV_PCM_STATE_OPEN
:
1738 case SNDRV_PCM_STATE_SETUP
:
1739 case SNDRV_PCM_STATE_DISCONNECTED
:
1744 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1745 is_playback
? "playback" : "capture");
1750 avail
= snd_pcm_playback_avail(runtime
);
1752 avail
= snd_pcm_capture_avail(runtime
);
1753 if (avail
>= runtime
->control
->avail_min
)
1757 remove_wait_queue(&runtime
->tsleep
, &wait
);
1762 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream
*substream
,
1764 unsigned long data
, unsigned int off
,
1765 snd_pcm_uframes_t frames
)
1767 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1769 char __user
*buf
= (char __user
*) data
+ frames_to_bytes(runtime
, off
);
1770 if (substream
->ops
->copy
) {
1771 if ((err
= substream
->ops
->copy(substream
, -1, hwoff
, buf
, frames
)) < 0)
1774 char *hwbuf
= runtime
->dma_area
+ frames_to_bytes(runtime
, hwoff
);
1775 if (copy_from_user(hwbuf
, buf
, frames_to_bytes(runtime
, frames
)))
1781 typedef int (*transfer_f
)(struct snd_pcm_substream
*substream
, unsigned int hwoff
,
1782 unsigned long data
, unsigned int off
,
1783 snd_pcm_uframes_t size
);
1785 static snd_pcm_sframes_t
snd_pcm_lib_write1(struct snd_pcm_substream
*substream
,
1787 snd_pcm_uframes_t size
,
1789 transfer_f transfer
)
1791 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1792 snd_pcm_uframes_t xfer
= 0;
1793 snd_pcm_uframes_t offset
= 0;
1799 snd_pcm_stream_lock_irq(substream
);
1800 switch (runtime
->status
->state
) {
1801 case SNDRV_PCM_STATE_PREPARED
:
1802 case SNDRV_PCM_STATE_RUNNING
:
1803 case SNDRV_PCM_STATE_PAUSED
:
1805 case SNDRV_PCM_STATE_XRUN
:
1808 case SNDRV_PCM_STATE_SUSPENDED
:
1818 snd_pcm_uframes_t frames
, appl_ptr
, appl_ofs
;
1819 snd_pcm_uframes_t avail
;
1820 snd_pcm_uframes_t cont
;
1821 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
1822 snd_pcm_update_hw_ptr(substream
);
1823 avail
= snd_pcm_playback_avail(runtime
);
1829 err
= wait_for_avail_min(substream
, &avail
);
1833 frames
= size
> avail
? avail
: size
;
1834 cont
= runtime
->buffer_size
- runtime
->control
->appl_ptr
% runtime
->buffer_size
;
1837 if (snd_BUG_ON(!frames
)) {
1839 snd_pcm_stream_unlock_irq(substream
);
1842 appl_ptr
= runtime
->control
->appl_ptr
;
1843 appl_ofs
= appl_ptr
% runtime
->buffer_size
;
1844 snd_pcm_stream_unlock_irq(substream
);
1845 err
= transfer(substream
, appl_ofs
, data
, offset
, frames
);
1846 snd_pcm_stream_lock_irq(substream
);
1849 switch (runtime
->status
->state
) {
1850 case SNDRV_PCM_STATE_XRUN
:
1853 case SNDRV_PCM_STATE_SUSPENDED
:
1860 if (appl_ptr
>= runtime
->boundary
)
1861 appl_ptr
-= runtime
->boundary
;
1862 runtime
->control
->appl_ptr
= appl_ptr
;
1863 if (substream
->ops
->ack
)
1864 substream
->ops
->ack(substream
);
1869 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
&&
1870 snd_pcm_playback_hw_avail(runtime
) >= (snd_pcm_sframes_t
)runtime
->start_threshold
) {
1871 err
= snd_pcm_start(substream
);
1878 if (xfer
> 0 && err
>= 0)
1879 snd_pcm_update_state(substream
, runtime
);
1880 snd_pcm_stream_unlock_irq(substream
);
1881 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: err
;
1884 /* sanity-check for read/write methods */
1885 static int pcm_sanity_check(struct snd_pcm_substream
*substream
)
1887 struct snd_pcm_runtime
*runtime
;
1888 if (PCM_RUNTIME_CHECK(substream
))
1890 runtime
= substream
->runtime
;
1891 if (snd_BUG_ON(!substream
->ops
->copy
&& !runtime
->dma_area
))
1893 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1898 snd_pcm_sframes_t
snd_pcm_lib_write(struct snd_pcm_substream
*substream
, const void __user
*buf
, snd_pcm_uframes_t size
)
1900 struct snd_pcm_runtime
*runtime
;
1904 err
= pcm_sanity_check(substream
);
1907 runtime
= substream
->runtime
;
1908 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
1910 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_INTERLEAVED
&&
1911 runtime
->channels
> 1)
1913 return snd_pcm_lib_write1(substream
, (unsigned long)buf
, size
, nonblock
,
1914 snd_pcm_lib_write_transfer
);
1917 EXPORT_SYMBOL(snd_pcm_lib_write
);
1919 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream
*substream
,
1921 unsigned long data
, unsigned int off
,
1922 snd_pcm_uframes_t frames
)
1924 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1926 void __user
**bufs
= (void __user
**)data
;
1927 int channels
= runtime
->channels
;
1929 if (substream
->ops
->copy
) {
1930 if (snd_BUG_ON(!substream
->ops
->silence
))
1932 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
1933 if (*bufs
== NULL
) {
1934 if ((err
= substream
->ops
->silence(substream
, c
, hwoff
, frames
)) < 0)
1937 char __user
*buf
= *bufs
+ samples_to_bytes(runtime
, off
);
1938 if ((err
= substream
->ops
->copy(substream
, c
, hwoff
, buf
, frames
)) < 0)
1943 /* default transfer behaviour */
1944 size_t dma_csize
= runtime
->dma_bytes
/ channels
;
1945 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
1946 char *hwbuf
= runtime
->dma_area
+ (c
* dma_csize
) + samples_to_bytes(runtime
, hwoff
);
1947 if (*bufs
== NULL
) {
1948 snd_pcm_format_set_silence(runtime
->format
, hwbuf
, frames
);
1950 char __user
*buf
= *bufs
+ samples_to_bytes(runtime
, off
);
1951 if (copy_from_user(hwbuf
, buf
, samples_to_bytes(runtime
, frames
)))
1959 snd_pcm_sframes_t
snd_pcm_lib_writev(struct snd_pcm_substream
*substream
,
1961 snd_pcm_uframes_t frames
)
1963 struct snd_pcm_runtime
*runtime
;
1967 err
= pcm_sanity_check(substream
);
1970 runtime
= substream
->runtime
;
1971 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
1973 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
1975 return snd_pcm_lib_write1(substream
, (unsigned long)bufs
, frames
,
1976 nonblock
, snd_pcm_lib_writev_transfer
);
1979 EXPORT_SYMBOL(snd_pcm_lib_writev
);
1981 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream
*substream
,
1983 unsigned long data
, unsigned int off
,
1984 snd_pcm_uframes_t frames
)
1986 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1988 char __user
*buf
= (char __user
*) data
+ frames_to_bytes(runtime
, off
);
1989 if (substream
->ops
->copy
) {
1990 if ((err
= substream
->ops
->copy(substream
, -1, hwoff
, buf
, frames
)) < 0)
1993 char *hwbuf
= runtime
->dma_area
+ frames_to_bytes(runtime
, hwoff
);
1994 if (copy_to_user(buf
, hwbuf
, frames_to_bytes(runtime
, frames
)))
2000 static snd_pcm_sframes_t
snd_pcm_lib_read1(struct snd_pcm_substream
*substream
,
2002 snd_pcm_uframes_t size
,
2004 transfer_f transfer
)
2006 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2007 snd_pcm_uframes_t xfer
= 0;
2008 snd_pcm_uframes_t offset
= 0;
2014 snd_pcm_stream_lock_irq(substream
);
2015 switch (runtime
->status
->state
) {
2016 case SNDRV_PCM_STATE_PREPARED
:
2017 if (size
>= runtime
->start_threshold
) {
2018 err
= snd_pcm_start(substream
);
2023 case SNDRV_PCM_STATE_DRAINING
:
2024 case SNDRV_PCM_STATE_RUNNING
:
2025 case SNDRV_PCM_STATE_PAUSED
:
2027 case SNDRV_PCM_STATE_XRUN
:
2030 case SNDRV_PCM_STATE_SUSPENDED
:
2040 snd_pcm_uframes_t frames
, appl_ptr
, appl_ofs
;
2041 snd_pcm_uframes_t avail
;
2042 snd_pcm_uframes_t cont
;
2043 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
2044 snd_pcm_update_hw_ptr(substream
);
2045 avail
= snd_pcm_capture_avail(runtime
);
2047 if (runtime
->status
->state
==
2048 SNDRV_PCM_STATE_DRAINING
) {
2049 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
2056 err
= wait_for_avail_min(substream
, &avail
);
2060 continue; /* draining */
2062 frames
= size
> avail
? avail
: size
;
2063 cont
= runtime
->buffer_size
- runtime
->control
->appl_ptr
% runtime
->buffer_size
;
2066 if (snd_BUG_ON(!frames
)) {
2068 snd_pcm_stream_unlock_irq(substream
);
2071 appl_ptr
= runtime
->control
->appl_ptr
;
2072 appl_ofs
= appl_ptr
% runtime
->buffer_size
;
2073 snd_pcm_stream_unlock_irq(substream
);
2074 err
= transfer(substream
, appl_ofs
, data
, offset
, frames
);
2075 snd_pcm_stream_lock_irq(substream
);
2078 switch (runtime
->status
->state
) {
2079 case SNDRV_PCM_STATE_XRUN
:
2082 case SNDRV_PCM_STATE_SUSPENDED
:
2089 if (appl_ptr
>= runtime
->boundary
)
2090 appl_ptr
-= runtime
->boundary
;
2091 runtime
->control
->appl_ptr
= appl_ptr
;
2092 if (substream
->ops
->ack
)
2093 substream
->ops
->ack(substream
);
2101 if (xfer
> 0 && err
>= 0)
2102 snd_pcm_update_state(substream
, runtime
);
2103 snd_pcm_stream_unlock_irq(substream
);
2104 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: err
;
2107 snd_pcm_sframes_t
snd_pcm_lib_read(struct snd_pcm_substream
*substream
, void __user
*buf
, snd_pcm_uframes_t size
)
2109 struct snd_pcm_runtime
*runtime
;
2113 err
= pcm_sanity_check(substream
);
2116 runtime
= substream
->runtime
;
2117 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
2118 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_INTERLEAVED
)
2120 return snd_pcm_lib_read1(substream
, (unsigned long)buf
, size
, nonblock
, snd_pcm_lib_read_transfer
);
2123 EXPORT_SYMBOL(snd_pcm_lib_read
);
2125 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream
*substream
,
2127 unsigned long data
, unsigned int off
,
2128 snd_pcm_uframes_t frames
)
2130 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2132 void __user
**bufs
= (void __user
**)data
;
2133 int channels
= runtime
->channels
;
2135 if (substream
->ops
->copy
) {
2136 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
2140 buf
= *bufs
+ samples_to_bytes(runtime
, off
);
2141 if ((err
= substream
->ops
->copy(substream
, c
, hwoff
, buf
, frames
)) < 0)
2145 snd_pcm_uframes_t dma_csize
= runtime
->dma_bytes
/ channels
;
2146 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
2152 hwbuf
= runtime
->dma_area
+ (c
* dma_csize
) + samples_to_bytes(runtime
, hwoff
);
2153 buf
= *bufs
+ samples_to_bytes(runtime
, off
);
2154 if (copy_to_user(buf
, hwbuf
, samples_to_bytes(runtime
, frames
)))
2161 snd_pcm_sframes_t
snd_pcm_lib_readv(struct snd_pcm_substream
*substream
,
2163 snd_pcm_uframes_t frames
)
2165 struct snd_pcm_runtime
*runtime
;
2169 err
= pcm_sanity_check(substream
);
2172 runtime
= substream
->runtime
;
2173 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2176 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
2177 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
2179 return snd_pcm_lib_read1(substream
, (unsigned long)bufs
, frames
, nonblock
, snd_pcm_lib_readv_transfer
);
2182 EXPORT_SYMBOL(snd_pcm_lib_readv
);