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 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
130 #define xrun_debug(substream, mask) ((substream)->pstr->xrun_debug & (mask))
132 #define xrun_debug(substream, mask) 0
135 #define dump_stack_on_xrun(substream) do { \
136 if (xrun_debug(substream, 2)) \
140 static void pcm_debug_name(struct snd_pcm_substream
*substream
,
141 char *name
, size_t len
)
143 snprintf(name
, len
, "pcmC%dD%d%c:%d",
144 substream
->pcm
->card
->number
,
145 substream
->pcm
->device
,
146 substream
->stream
? 'c' : 'p',
150 static void xrun(struct snd_pcm_substream
*substream
)
152 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
154 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
)
155 snd_pcm_gettime(runtime
, (struct timespec
*)&runtime
->status
->tstamp
);
156 snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
157 if (xrun_debug(substream
, 1)) {
159 pcm_debug_name(substream
, name
, sizeof(name
));
160 snd_printd(KERN_DEBUG
"XRUN: %s\n", name
);
161 dump_stack_on_xrun(substream
);
165 static snd_pcm_uframes_t
166 snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream
*substream
,
167 struct snd_pcm_runtime
*runtime
)
169 snd_pcm_uframes_t pos
;
171 pos
= substream
->ops
->pointer(substream
);
172 if (pos
== SNDRV_PCM_POS_XRUN
)
173 return pos
; /* XRUN */
174 if (pos
>= runtime
->buffer_size
) {
175 if (printk_ratelimit()) {
177 pcm_debug_name(substream
, name
, sizeof(name
));
178 snd_printd(KERN_ERR
"BUG: %s, pos = 0x%lx, "
179 "buffer size = 0x%lx, period size = 0x%lx\n",
180 name
, pos
, runtime
->buffer_size
,
181 runtime
->period_size
);
185 pos
-= pos
% runtime
->min_align
;
189 static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream
*substream
,
190 struct snd_pcm_runtime
*runtime
)
192 snd_pcm_uframes_t avail
;
194 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
195 avail
= snd_pcm_playback_avail(runtime
);
197 avail
= snd_pcm_capture_avail(runtime
);
198 if (avail
> runtime
->avail_max
)
199 runtime
->avail_max
= avail
;
200 if (avail
>= runtime
->stop_threshold
) {
201 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
)
202 snd_pcm_drain_done(substream
);
207 if (avail
>= runtime
->control
->avail_min
)
208 wake_up(&runtime
->sleep
);
212 #define hw_ptr_error(substream, fmt, args...) \
214 if (xrun_debug(substream, 1)) { \
215 if (printk_ratelimit()) { \
216 snd_printd("PCM: " fmt, ##args); \
218 dump_stack_on_xrun(substream); \
222 static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream
*substream
)
224 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
225 snd_pcm_uframes_t pos
;
226 snd_pcm_uframes_t old_hw_ptr
, new_hw_ptr
, hw_ptr_interrupt
, hw_base
;
227 snd_pcm_sframes_t hdelta
, delta
;
228 unsigned long jdelta
;
230 old_hw_ptr
= runtime
->status
->hw_ptr
;
231 pos
= snd_pcm_update_hw_ptr_pos(substream
, runtime
);
232 if (pos
== SNDRV_PCM_POS_XRUN
) {
236 if (xrun_debug(substream
, 8)) {
238 pcm_debug_name(substream
, name
, sizeof(name
));
239 snd_printd("period_update: %s: pos=0x%x/0x%x/0x%x, "
240 "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
241 name
, (unsigned int)pos
,
242 (unsigned int)runtime
->period_size
,
243 (unsigned int)runtime
->buffer_size
,
244 (unsigned long)old_hw_ptr
,
245 (unsigned long)runtime
->hw_ptr_base
,
246 (unsigned long)runtime
->hw_ptr_interrupt
);
248 hw_base
= runtime
->hw_ptr_base
;
249 new_hw_ptr
= hw_base
+ pos
;
250 hw_ptr_interrupt
= runtime
->hw_ptr_interrupt
+ runtime
->period_size
;
251 delta
= new_hw_ptr
- hw_ptr_interrupt
;
252 if (hw_ptr_interrupt
>= runtime
->boundary
) {
253 hw_ptr_interrupt
-= runtime
->boundary
;
254 if (hw_base
< runtime
->boundary
/ 2)
255 /* hw_base was already lapped; recalc delta */
256 delta
= new_hw_ptr
- hw_ptr_interrupt
;
259 if (runtime
->periods
== 1 || new_hw_ptr
< old_hw_ptr
)
260 delta
+= runtime
->buffer_size
;
262 hw_ptr_error(substream
,
263 "Unexpected hw_pointer value "
264 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
265 substream
->stream
, (long)pos
,
266 (long)hw_ptr_interrupt
);
268 /* simply skipping the hwptr update seems more
269 * robust in some cases, e.g. on VMware with
270 * inaccurate timer source
272 return 0; /* skip this update */
274 /* rebase to interrupt position */
275 hw_base
= new_hw_ptr
= hw_ptr_interrupt
;
276 /* align hw_base to buffer_size */
277 hw_base
-= hw_base
% runtime
->buffer_size
;
281 hw_base
+= runtime
->buffer_size
;
282 if (hw_base
>= runtime
->boundary
)
284 new_hw_ptr
= hw_base
+ pos
;
288 /* Do jiffies check only in xrun_debug mode */
289 if (!xrun_debug(substream
, 4))
290 goto no_jiffies_check
;
292 /* Skip the jiffies check for hardwares with BATCH flag.
293 * Such hardware usually just increases the position at each IRQ,
294 * thus it can't give any strange position.
296 if (runtime
->hw
.info
& SNDRV_PCM_INFO_BATCH
)
297 goto no_jiffies_check
;
298 hdelta
= new_hw_ptr
- old_hw_ptr
;
299 if (hdelta
< runtime
->delay
)
300 goto no_jiffies_check
;
301 hdelta
-= runtime
->delay
;
302 jdelta
= jiffies
- runtime
->hw_ptr_jiffies
;
303 if (((hdelta
* HZ
) / runtime
->rate
) > jdelta
+ HZ
/100) {
305 (((runtime
->period_size
* HZ
) / runtime
->rate
)
307 hw_ptr_error(substream
,
308 "hw_ptr skipping! [Q] "
309 "(pos=%ld, delta=%ld, period=%ld, "
310 "jdelta=%lu/%lu/%lu)\n",
311 (long)pos
, (long)hdelta
,
312 (long)runtime
->period_size
, jdelta
,
313 ((hdelta
* HZ
) / runtime
->rate
), delta
);
314 hw_ptr_interrupt
= runtime
->hw_ptr_interrupt
+
315 runtime
->period_size
* delta
;
316 if (hw_ptr_interrupt
>= runtime
->boundary
)
317 hw_ptr_interrupt
-= runtime
->boundary
;
318 /* rebase to interrupt position */
319 hw_base
= new_hw_ptr
= hw_ptr_interrupt
;
320 /* align hw_base to buffer_size */
321 hw_base
-= hw_base
% runtime
->buffer_size
;
325 if (delta
> runtime
->period_size
+ runtime
->period_size
/ 2) {
326 hw_ptr_error(substream
,
328 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
329 substream
->stream
, (long)delta
,
330 (long)hw_ptr_interrupt
);
331 /* rebase hw_ptr_interrupt */
333 new_hw_ptr
- new_hw_ptr
% runtime
->period_size
;
335 runtime
->hw_ptr_interrupt
= hw_ptr_interrupt
;
337 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
338 runtime
->silence_size
> 0)
339 snd_pcm_playback_silence(substream
, new_hw_ptr
);
341 if (runtime
->status
->hw_ptr
== new_hw_ptr
)
344 runtime
->hw_ptr_base
= hw_base
;
345 runtime
->status
->hw_ptr
= new_hw_ptr
;
346 runtime
->hw_ptr_jiffies
= jiffies
;
347 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
)
348 snd_pcm_gettime(runtime
, (struct timespec
*)&runtime
->status
->tstamp
);
350 return snd_pcm_update_hw_ptr_post(substream
, runtime
);
353 /* CAUTION: call it with irq disabled */
354 int snd_pcm_update_hw_ptr(struct snd_pcm_substream
*substream
)
356 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
357 snd_pcm_uframes_t pos
;
358 snd_pcm_uframes_t old_hw_ptr
, new_hw_ptr
, hw_base
;
359 snd_pcm_sframes_t delta
;
360 unsigned long jdelta
;
362 old_hw_ptr
= runtime
->status
->hw_ptr
;
363 pos
= snd_pcm_update_hw_ptr_pos(substream
, runtime
);
364 if (pos
== SNDRV_PCM_POS_XRUN
) {
368 if (xrun_debug(substream
, 16)) {
370 pcm_debug_name(substream
, name
, sizeof(name
));
371 snd_printd("hw_update: %s: pos=0x%x/0x%x/0x%x, "
372 "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
373 name
, (unsigned int)pos
,
374 (unsigned int)runtime
->period_size
,
375 (unsigned int)runtime
->buffer_size
,
376 (unsigned long)old_hw_ptr
,
377 (unsigned long)runtime
->hw_ptr_base
,
378 (unsigned long)runtime
->hw_ptr_interrupt
);
381 hw_base
= runtime
->hw_ptr_base
;
382 new_hw_ptr
= hw_base
+ pos
;
384 delta
= new_hw_ptr
- old_hw_ptr
;
385 jdelta
= jiffies
- runtime
->hw_ptr_jiffies
;
387 delta
+= runtime
->buffer_size
;
389 hw_ptr_error(substream
,
390 "Unexpected hw_pointer value [2] "
391 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
392 substream
->stream
, (long)pos
,
393 (long)old_hw_ptr
, jdelta
);
396 hw_base
+= runtime
->buffer_size
;
397 if (hw_base
>= runtime
->boundary
)
399 new_hw_ptr
= hw_base
+ pos
;
401 /* Do jiffies check only in xrun_debug mode */
402 if (!xrun_debug(substream
, 4))
403 goto no_jiffies_check
;
404 if (delta
< runtime
->delay
)
405 goto no_jiffies_check
;
406 delta
-= runtime
->delay
;
407 if (((delta
* HZ
) / runtime
->rate
) > jdelta
+ HZ
/100) {
408 hw_ptr_error(substream
,
410 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
411 (long)pos
, (long)delta
,
412 (long)runtime
->period_size
, jdelta
,
413 ((delta
* HZ
) / runtime
->rate
));
417 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
418 runtime
->silence_size
> 0)
419 snd_pcm_playback_silence(substream
, new_hw_ptr
);
421 if (runtime
->status
->hw_ptr
== new_hw_ptr
)
424 runtime
->hw_ptr_base
= hw_base
;
425 runtime
->status
->hw_ptr
= new_hw_ptr
;
426 runtime
->hw_ptr_jiffies
= jiffies
;
427 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
)
428 snd_pcm_gettime(runtime
, (struct timespec
*)&runtime
->status
->tstamp
);
430 return snd_pcm_update_hw_ptr_post(substream
, runtime
);
434 * snd_pcm_set_ops - set the PCM operators
435 * @pcm: the pcm instance
436 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
437 * @ops: the operator table
439 * Sets the given PCM operators to the pcm instance.
441 void snd_pcm_set_ops(struct snd_pcm
*pcm
, int direction
, struct snd_pcm_ops
*ops
)
443 struct snd_pcm_str
*stream
= &pcm
->streams
[direction
];
444 struct snd_pcm_substream
*substream
;
446 for (substream
= stream
->substream
; substream
!= NULL
; substream
= substream
->next
)
447 substream
->ops
= ops
;
450 EXPORT_SYMBOL(snd_pcm_set_ops
);
453 * snd_pcm_sync - set the PCM sync id
454 * @substream: the pcm substream
456 * Sets the PCM sync identifier for the card.
458 void snd_pcm_set_sync(struct snd_pcm_substream
*substream
)
460 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
462 runtime
->sync
.id32
[0] = substream
->pcm
->card
->number
;
463 runtime
->sync
.id32
[1] = -1;
464 runtime
->sync
.id32
[2] = -1;
465 runtime
->sync
.id32
[3] = -1;
468 EXPORT_SYMBOL(snd_pcm_set_sync
);
471 * Standard ioctl routine
474 static inline unsigned int div32(unsigned int a
, unsigned int b
,
485 static inline unsigned int div_down(unsigned int a
, unsigned int b
)
492 static inline unsigned int div_up(unsigned int a
, unsigned int b
)
504 static inline unsigned int mul(unsigned int a
, unsigned int b
)
508 if (div_down(UINT_MAX
, a
) < b
)
513 static inline unsigned int muldiv32(unsigned int a
, unsigned int b
,
514 unsigned int c
, unsigned int *r
)
516 u_int64_t n
= (u_int64_t
) a
* b
;
522 n
= div_u64_rem(n
, c
, r
);
531 * snd_interval_refine - refine the interval value of configurator
532 * @i: the interval value to refine
533 * @v: the interval value to refer to
535 * Refines the interval value with the reference value.
536 * The interval is changed to the range satisfying both intervals.
537 * The interval status (min, max, integer, etc.) are evaluated.
539 * Returns non-zero if the value is changed, zero if not changed.
541 int snd_interval_refine(struct snd_interval
*i
, const struct snd_interval
*v
)
544 if (snd_BUG_ON(snd_interval_empty(i
)))
546 if (i
->min
< v
->min
) {
548 i
->openmin
= v
->openmin
;
550 } else if (i
->min
== v
->min
&& !i
->openmin
&& v
->openmin
) {
554 if (i
->max
> v
->max
) {
556 i
->openmax
= v
->openmax
;
558 } else if (i
->max
== v
->max
&& !i
->openmax
&& v
->openmax
) {
562 if (!i
->integer
&& v
->integer
) {
575 } else if (!i
->openmin
&& !i
->openmax
&& i
->min
== i
->max
)
577 if (snd_interval_checkempty(i
)) {
578 snd_interval_none(i
);
584 EXPORT_SYMBOL(snd_interval_refine
);
586 static int snd_interval_refine_first(struct snd_interval
*i
)
588 if (snd_BUG_ON(snd_interval_empty(i
)))
590 if (snd_interval_single(i
))
593 i
->openmax
= i
->openmin
;
599 static int snd_interval_refine_last(struct snd_interval
*i
)
601 if (snd_BUG_ON(snd_interval_empty(i
)))
603 if (snd_interval_single(i
))
606 i
->openmin
= i
->openmax
;
612 void snd_interval_mul(const struct snd_interval
*a
, const struct snd_interval
*b
, struct snd_interval
*c
)
614 if (a
->empty
|| b
->empty
) {
615 snd_interval_none(c
);
619 c
->min
= mul(a
->min
, b
->min
);
620 c
->openmin
= (a
->openmin
|| b
->openmin
);
621 c
->max
= mul(a
->max
, b
->max
);
622 c
->openmax
= (a
->openmax
|| b
->openmax
);
623 c
->integer
= (a
->integer
&& b
->integer
);
627 * snd_interval_div - refine the interval value with division
634 * Returns non-zero if the value is changed, zero if not changed.
636 void snd_interval_div(const struct snd_interval
*a
, const struct snd_interval
*b
, struct snd_interval
*c
)
639 if (a
->empty
|| b
->empty
) {
640 snd_interval_none(c
);
644 c
->min
= div32(a
->min
, b
->max
, &r
);
645 c
->openmin
= (r
|| a
->openmin
|| b
->openmax
);
647 c
->max
= div32(a
->max
, b
->min
, &r
);
652 c
->openmax
= (a
->openmax
|| b
->openmin
);
661 * snd_interval_muldivk - refine the interval value
664 * @k: divisor (as integer)
669 * Returns non-zero if the value is changed, zero if not changed.
671 void snd_interval_muldivk(const struct snd_interval
*a
, const struct snd_interval
*b
,
672 unsigned int k
, struct snd_interval
*c
)
675 if (a
->empty
|| b
->empty
) {
676 snd_interval_none(c
);
680 c
->min
= muldiv32(a
->min
, b
->min
, k
, &r
);
681 c
->openmin
= (r
|| a
->openmin
|| b
->openmin
);
682 c
->max
= muldiv32(a
->max
, b
->max
, k
, &r
);
687 c
->openmax
= (a
->openmax
|| b
->openmax
);
692 * snd_interval_mulkdiv - refine the interval value
694 * @k: dividend 2 (as integer)
700 * Returns non-zero if the value is changed, zero if not changed.
702 void snd_interval_mulkdiv(const struct snd_interval
*a
, unsigned int k
,
703 const struct snd_interval
*b
, struct snd_interval
*c
)
706 if (a
->empty
|| b
->empty
) {
707 snd_interval_none(c
);
711 c
->min
= muldiv32(a
->min
, k
, b
->max
, &r
);
712 c
->openmin
= (r
|| a
->openmin
|| b
->openmax
);
714 c
->max
= muldiv32(a
->max
, k
, b
->min
, &r
);
719 c
->openmax
= (a
->openmax
|| b
->openmin
);
731 * snd_interval_ratnum - refine the interval value
732 * @i: interval to refine
733 * @rats_count: number of ratnum_t
734 * @rats: ratnum_t array
735 * @nump: pointer to store the resultant numerator
736 * @denp: pointer to store the resultant denominator
738 * Returns non-zero if the value is changed, zero if not changed.
740 int snd_interval_ratnum(struct snd_interval
*i
,
741 unsigned int rats_count
, struct snd_ratnum
*rats
,
742 unsigned int *nump
, unsigned int *denp
)
744 unsigned int best_num
, best_diff
, best_den
;
746 struct snd_interval t
;
749 best_num
= best_den
= best_diff
= 0;
750 for (k
= 0; k
< rats_count
; ++k
) {
751 unsigned int num
= rats
[k
].num
;
753 unsigned int q
= i
->min
;
757 den
= div_down(num
, q
);
758 if (den
< rats
[k
].den_min
)
760 if (den
> rats
[k
].den_max
)
761 den
= rats
[k
].den_max
;
764 r
= (den
- rats
[k
].den_min
) % rats
[k
].den_step
;
768 diff
= num
- q
* den
;
770 diff
* best_den
< best_diff
* den
) {
780 t
.min
= div_down(best_num
, best_den
);
781 t
.openmin
= !!(best_num
% best_den
);
783 best_num
= best_den
= best_diff
= 0;
784 for (k
= 0; k
< rats_count
; ++k
) {
785 unsigned int num
= rats
[k
].num
;
787 unsigned int q
= i
->max
;
793 den
= div_up(num
, q
);
794 if (den
> rats
[k
].den_max
)
796 if (den
< rats
[k
].den_min
)
797 den
= rats
[k
].den_min
;
800 r
= (den
- rats
[k
].den_min
) % rats
[k
].den_step
;
802 den
+= rats
[k
].den_step
- r
;
804 diff
= q
* den
- num
;
806 diff
* best_den
< best_diff
* den
) {
816 t
.max
= div_up(best_num
, best_den
);
817 t
.openmax
= !!(best_num
% best_den
);
819 err
= snd_interval_refine(i
, &t
);
823 if (snd_interval_single(i
)) {
832 EXPORT_SYMBOL(snd_interval_ratnum
);
835 * snd_interval_ratden - refine the interval value
836 * @i: interval to refine
837 * @rats_count: number of struct ratden
838 * @rats: struct ratden array
839 * @nump: pointer to store the resultant numerator
840 * @denp: pointer to store the resultant denominator
842 * Returns non-zero if the value is changed, zero if not changed.
844 static int snd_interval_ratden(struct snd_interval
*i
,
845 unsigned int rats_count
, struct snd_ratden
*rats
,
846 unsigned int *nump
, unsigned int *denp
)
848 unsigned int best_num
, best_diff
, best_den
;
850 struct snd_interval t
;
853 best_num
= best_den
= best_diff
= 0;
854 for (k
= 0; k
< rats_count
; ++k
) {
856 unsigned int den
= rats
[k
].den
;
857 unsigned int q
= i
->min
;
860 if (num
> rats
[k
].num_max
)
862 if (num
< rats
[k
].num_min
)
863 num
= rats
[k
].num_max
;
866 r
= (num
- rats
[k
].num_min
) % rats
[k
].num_step
;
868 num
+= rats
[k
].num_step
- r
;
870 diff
= num
- q
* den
;
872 diff
* best_den
< best_diff
* den
) {
882 t
.min
= div_down(best_num
, best_den
);
883 t
.openmin
= !!(best_num
% best_den
);
885 best_num
= best_den
= best_diff
= 0;
886 for (k
= 0; k
< rats_count
; ++k
) {
888 unsigned int den
= rats
[k
].den
;
889 unsigned int q
= i
->max
;
892 if (num
< rats
[k
].num_min
)
894 if (num
> rats
[k
].num_max
)
895 num
= rats
[k
].num_max
;
898 r
= (num
- rats
[k
].num_min
) % rats
[k
].num_step
;
902 diff
= q
* den
- num
;
904 diff
* best_den
< best_diff
* den
) {
914 t
.max
= div_up(best_num
, best_den
);
915 t
.openmax
= !!(best_num
% best_den
);
917 err
= snd_interval_refine(i
, &t
);
921 if (snd_interval_single(i
)) {
931 * snd_interval_list - refine the interval value from the list
932 * @i: the interval value to refine
933 * @count: the number of elements in the list
934 * @list: the value list
935 * @mask: the bit-mask to evaluate
937 * Refines the interval value from the list.
938 * When mask is non-zero, only the elements corresponding to bit 1 are
941 * Returns non-zero if the value is changed, zero if not changed.
943 int snd_interval_list(struct snd_interval
*i
, unsigned int count
, unsigned int *list
, unsigned int mask
)
952 for (k
= 0; k
< count
; k
++) {
953 if (mask
&& !(mask
& (1 << k
)))
955 if (i
->min
== list
[k
] && !i
->openmin
)
957 if (i
->min
< list
[k
]) {
967 for (k
= count
; k
-- > 0;) {
968 if (mask
&& !(mask
& (1 << k
)))
970 if (i
->max
== list
[k
] && !i
->openmax
)
972 if (i
->max
> list
[k
]) {
982 if (snd_interval_checkempty(i
)) {
989 EXPORT_SYMBOL(snd_interval_list
);
991 static int snd_interval_step(struct snd_interval
*i
, unsigned int min
, unsigned int step
)
995 n
= (i
->min
- min
) % step
;
996 if (n
!= 0 || i
->openmin
) {
1000 n
= (i
->max
- min
) % step
;
1001 if (n
!= 0 || i
->openmax
) {
1005 if (snd_interval_checkempty(i
)) {
1012 /* Info constraints helpers */
1015 * snd_pcm_hw_rule_add - add the hw-constraint rule
1016 * @runtime: the pcm runtime instance
1017 * @cond: condition bits
1018 * @var: the variable to evaluate
1019 * @func: the evaluation function
1020 * @private: the private data pointer passed to function
1021 * @dep: the dependent variables
1023 * Returns zero if successful, or a negative error code on failure.
1025 int snd_pcm_hw_rule_add(struct snd_pcm_runtime
*runtime
, unsigned int cond
,
1027 snd_pcm_hw_rule_func_t func
, void *private,
1030 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1031 struct snd_pcm_hw_rule
*c
;
1034 va_start(args
, dep
);
1035 if (constrs
->rules_num
>= constrs
->rules_all
) {
1036 struct snd_pcm_hw_rule
*new;
1037 unsigned int new_rules
= constrs
->rules_all
+ 16;
1038 new = kcalloc(new_rules
, sizeof(*c
), GFP_KERNEL
);
1041 if (constrs
->rules
) {
1042 memcpy(new, constrs
->rules
,
1043 constrs
->rules_num
* sizeof(*c
));
1044 kfree(constrs
->rules
);
1046 constrs
->rules
= new;
1047 constrs
->rules_all
= new_rules
;
1049 c
= &constrs
->rules
[constrs
->rules_num
];
1053 c
->private = private;
1056 if (snd_BUG_ON(k
>= ARRAY_SIZE(c
->deps
)))
1061 dep
= va_arg(args
, int);
1063 constrs
->rules_num
++;
1068 EXPORT_SYMBOL(snd_pcm_hw_rule_add
);
1071 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1072 * @runtime: PCM runtime instance
1073 * @var: hw_params variable to apply the mask
1074 * @mask: the bitmap mask
1076 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1078 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
,
1081 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1082 struct snd_mask
*maskp
= constrs_mask(constrs
, var
);
1083 *maskp
->bits
&= mask
;
1084 memset(maskp
->bits
+ 1, 0, (SNDRV_MASK_MAX
-32) / 8); /* clear rest */
1085 if (*maskp
->bits
== 0)
1091 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1092 * @runtime: PCM runtime instance
1093 * @var: hw_params variable to apply the mask
1094 * @mask: the 64bit bitmap mask
1096 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1098 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
,
1101 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1102 struct snd_mask
*maskp
= constrs_mask(constrs
, var
);
1103 maskp
->bits
[0] &= (u_int32_t
)mask
;
1104 maskp
->bits
[1] &= (u_int32_t
)(mask
>> 32);
1105 memset(maskp
->bits
+ 2, 0, (SNDRV_MASK_MAX
-64) / 8); /* clear rest */
1106 if (! maskp
->bits
[0] && ! maskp
->bits
[1])
1112 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1113 * @runtime: PCM runtime instance
1114 * @var: hw_params variable to apply the integer constraint
1116 * Apply the constraint of integer to an interval parameter.
1118 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
)
1120 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1121 return snd_interval_setinteger(constrs_interval(constrs
, var
));
1124 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer
);
1127 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1128 * @runtime: PCM runtime instance
1129 * @var: hw_params variable to apply the range
1130 * @min: the minimal value
1131 * @max: the maximal value
1133 * Apply the min/max range constraint to an interval parameter.
1135 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
,
1136 unsigned int min
, unsigned int max
)
1138 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1139 struct snd_interval t
;
1142 t
.openmin
= t
.openmax
= 0;
1144 return snd_interval_refine(constrs_interval(constrs
, var
), &t
);
1147 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax
);
1149 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params
*params
,
1150 struct snd_pcm_hw_rule
*rule
)
1152 struct snd_pcm_hw_constraint_list
*list
= rule
->private;
1153 return snd_interval_list(hw_param_interval(params
, rule
->var
), list
->count
, list
->list
, list
->mask
);
1158 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1159 * @runtime: PCM runtime instance
1160 * @cond: condition bits
1161 * @var: hw_params variable to apply the list constraint
1164 * Apply the list of constraints to an interval parameter.
1166 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime
*runtime
,
1168 snd_pcm_hw_param_t var
,
1169 struct snd_pcm_hw_constraint_list
*l
)
1171 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1172 snd_pcm_hw_rule_list
, l
,
1176 EXPORT_SYMBOL(snd_pcm_hw_constraint_list
);
1178 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params
*params
,
1179 struct snd_pcm_hw_rule
*rule
)
1181 struct snd_pcm_hw_constraint_ratnums
*r
= rule
->private;
1182 unsigned int num
= 0, den
= 0;
1184 err
= snd_interval_ratnum(hw_param_interval(params
, rule
->var
),
1185 r
->nrats
, r
->rats
, &num
, &den
);
1186 if (err
>= 0 && den
&& rule
->var
== SNDRV_PCM_HW_PARAM_RATE
) {
1187 params
->rate_num
= num
;
1188 params
->rate_den
= den
;
1194 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1195 * @runtime: PCM runtime instance
1196 * @cond: condition bits
1197 * @var: hw_params variable to apply the ratnums constraint
1198 * @r: struct snd_ratnums constriants
1200 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime
*runtime
,
1202 snd_pcm_hw_param_t var
,
1203 struct snd_pcm_hw_constraint_ratnums
*r
)
1205 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1206 snd_pcm_hw_rule_ratnums
, r
,
1210 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums
);
1212 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params
*params
,
1213 struct snd_pcm_hw_rule
*rule
)
1215 struct snd_pcm_hw_constraint_ratdens
*r
= rule
->private;
1216 unsigned int num
= 0, den
= 0;
1217 int err
= snd_interval_ratden(hw_param_interval(params
, rule
->var
),
1218 r
->nrats
, r
->rats
, &num
, &den
);
1219 if (err
>= 0 && den
&& rule
->var
== SNDRV_PCM_HW_PARAM_RATE
) {
1220 params
->rate_num
= num
;
1221 params
->rate_den
= den
;
1227 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1228 * @runtime: PCM runtime instance
1229 * @cond: condition bits
1230 * @var: hw_params variable to apply the ratdens constraint
1231 * @r: struct snd_ratdens constriants
1233 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime
*runtime
,
1235 snd_pcm_hw_param_t var
,
1236 struct snd_pcm_hw_constraint_ratdens
*r
)
1238 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1239 snd_pcm_hw_rule_ratdens
, r
,
1243 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens
);
1245 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params
*params
,
1246 struct snd_pcm_hw_rule
*rule
)
1248 unsigned int l
= (unsigned long) rule
->private;
1249 int width
= l
& 0xffff;
1250 unsigned int msbits
= l
>> 16;
1251 struct snd_interval
*i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
1252 if (snd_interval_single(i
) && snd_interval_value(i
) == width
)
1253 params
->msbits
= msbits
;
1258 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1259 * @runtime: PCM runtime instance
1260 * @cond: condition bits
1261 * @width: sample bits width
1262 * @msbits: msbits width
1264 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime
*runtime
,
1267 unsigned int msbits
)
1269 unsigned long l
= (msbits
<< 16) | width
;
1270 return snd_pcm_hw_rule_add(runtime
, cond
, -1,
1271 snd_pcm_hw_rule_msbits
,
1273 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1276 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits
);
1278 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params
*params
,
1279 struct snd_pcm_hw_rule
*rule
)
1281 unsigned long step
= (unsigned long) rule
->private;
1282 return snd_interval_step(hw_param_interval(params
, rule
->var
), 0, step
);
1286 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1287 * @runtime: PCM runtime instance
1288 * @cond: condition bits
1289 * @var: hw_params variable to apply the step constraint
1292 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime
*runtime
,
1294 snd_pcm_hw_param_t var
,
1297 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1298 snd_pcm_hw_rule_step
, (void *) step
,
1302 EXPORT_SYMBOL(snd_pcm_hw_constraint_step
);
1304 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params
*params
, struct snd_pcm_hw_rule
*rule
)
1306 static unsigned int pow2_sizes
[] = {
1307 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1308 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1309 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1310 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1312 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1313 ARRAY_SIZE(pow2_sizes
), pow2_sizes
, 0);
1317 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1318 * @runtime: PCM runtime instance
1319 * @cond: condition bits
1320 * @var: hw_params variable to apply the power-of-2 constraint
1322 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime
*runtime
,
1324 snd_pcm_hw_param_t var
)
1326 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1327 snd_pcm_hw_rule_pow2
, NULL
,
1331 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2
);
1333 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params
*params
,
1334 snd_pcm_hw_param_t var
)
1336 if (hw_is_mask(var
)) {
1337 snd_mask_any(hw_param_mask(params
, var
));
1338 params
->cmask
|= 1 << var
;
1339 params
->rmask
|= 1 << var
;
1342 if (hw_is_interval(var
)) {
1343 snd_interval_any(hw_param_interval(params
, var
));
1344 params
->cmask
|= 1 << var
;
1345 params
->rmask
|= 1 << var
;
1351 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params
*params
)
1354 memset(params
, 0, sizeof(*params
));
1355 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++)
1356 _snd_pcm_hw_param_any(params
, k
);
1357 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
1358 _snd_pcm_hw_param_any(params
, k
);
1362 EXPORT_SYMBOL(_snd_pcm_hw_params_any
);
1365 * snd_pcm_hw_param_value - return @params field @var value
1366 * @params: the hw_params instance
1367 * @var: parameter to retrieve
1368 * @dir: pointer to the direction (-1,0,1) or %NULL
1370 * Return the value for field @var if it's fixed in configuration space
1371 * defined by @params. Return -%EINVAL otherwise.
1373 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params
*params
,
1374 snd_pcm_hw_param_t var
, int *dir
)
1376 if (hw_is_mask(var
)) {
1377 const struct snd_mask
*mask
= hw_param_mask_c(params
, var
);
1378 if (!snd_mask_single(mask
))
1382 return snd_mask_value(mask
);
1384 if (hw_is_interval(var
)) {
1385 const struct snd_interval
*i
= hw_param_interval_c(params
, var
);
1386 if (!snd_interval_single(i
))
1390 return snd_interval_value(i
);
1395 EXPORT_SYMBOL(snd_pcm_hw_param_value
);
1397 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params
*params
,
1398 snd_pcm_hw_param_t var
)
1400 if (hw_is_mask(var
)) {
1401 snd_mask_none(hw_param_mask(params
, var
));
1402 params
->cmask
|= 1 << var
;
1403 params
->rmask
|= 1 << var
;
1404 } else if (hw_is_interval(var
)) {
1405 snd_interval_none(hw_param_interval(params
, var
));
1406 params
->cmask
|= 1 << var
;
1407 params
->rmask
|= 1 << var
;
1413 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty
);
1415 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params
*params
,
1416 snd_pcm_hw_param_t var
)
1419 if (hw_is_mask(var
))
1420 changed
= snd_mask_refine_first(hw_param_mask(params
, var
));
1421 else if (hw_is_interval(var
))
1422 changed
= snd_interval_refine_first(hw_param_interval(params
, var
));
1426 params
->cmask
|= 1 << var
;
1427 params
->rmask
|= 1 << var
;
1434 * snd_pcm_hw_param_first - refine config space and return minimum value
1435 * @pcm: PCM instance
1436 * @params: the hw_params instance
1437 * @var: parameter to retrieve
1438 * @dir: pointer to the direction (-1,0,1) or %NULL
1440 * Inside configuration space defined by @params remove from @var all
1441 * values > minimum. Reduce configuration space accordingly.
1442 * Return the minimum.
1444 int snd_pcm_hw_param_first(struct snd_pcm_substream
*pcm
,
1445 struct snd_pcm_hw_params
*params
,
1446 snd_pcm_hw_param_t var
, int *dir
)
1448 int changed
= _snd_pcm_hw_param_first(params
, var
);
1451 if (params
->rmask
) {
1452 int err
= snd_pcm_hw_refine(pcm
, params
);
1453 if (snd_BUG_ON(err
< 0))
1456 return snd_pcm_hw_param_value(params
, var
, dir
);
1459 EXPORT_SYMBOL(snd_pcm_hw_param_first
);
1461 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params
*params
,
1462 snd_pcm_hw_param_t var
)
1465 if (hw_is_mask(var
))
1466 changed
= snd_mask_refine_last(hw_param_mask(params
, var
));
1467 else if (hw_is_interval(var
))
1468 changed
= snd_interval_refine_last(hw_param_interval(params
, var
));
1472 params
->cmask
|= 1 << var
;
1473 params
->rmask
|= 1 << var
;
1480 * snd_pcm_hw_param_last - refine config space and return maximum value
1481 * @pcm: PCM instance
1482 * @params: the hw_params instance
1483 * @var: parameter to retrieve
1484 * @dir: pointer to the direction (-1,0,1) or %NULL
1486 * Inside configuration space defined by @params remove from @var all
1487 * values < maximum. Reduce configuration space accordingly.
1488 * Return the maximum.
1490 int snd_pcm_hw_param_last(struct snd_pcm_substream
*pcm
,
1491 struct snd_pcm_hw_params
*params
,
1492 snd_pcm_hw_param_t var
, int *dir
)
1494 int changed
= _snd_pcm_hw_param_last(params
, var
);
1497 if (params
->rmask
) {
1498 int err
= snd_pcm_hw_refine(pcm
, params
);
1499 if (snd_BUG_ON(err
< 0))
1502 return snd_pcm_hw_param_value(params
, var
, dir
);
1505 EXPORT_SYMBOL(snd_pcm_hw_param_last
);
1508 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1509 * @pcm: PCM instance
1510 * @params: the hw_params instance
1512 * Choose one configuration from configuration space defined by @params.
1513 * The configuration chosen is that obtained fixing in this order:
1514 * first access, first format, first subformat, min channels,
1515 * min rate, min period time, max buffer size, min tick time
1517 int snd_pcm_hw_params_choose(struct snd_pcm_substream
*pcm
,
1518 struct snd_pcm_hw_params
*params
)
1520 static int vars
[] = {
1521 SNDRV_PCM_HW_PARAM_ACCESS
,
1522 SNDRV_PCM_HW_PARAM_FORMAT
,
1523 SNDRV_PCM_HW_PARAM_SUBFORMAT
,
1524 SNDRV_PCM_HW_PARAM_CHANNELS
,
1525 SNDRV_PCM_HW_PARAM_RATE
,
1526 SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1527 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1528 SNDRV_PCM_HW_PARAM_TICK_TIME
,
1533 for (v
= vars
; *v
!= -1; v
++) {
1534 if (*v
!= SNDRV_PCM_HW_PARAM_BUFFER_SIZE
)
1535 err
= snd_pcm_hw_param_first(pcm
, params
, *v
, NULL
);
1537 err
= snd_pcm_hw_param_last(pcm
, params
, *v
, NULL
);
1538 if (snd_BUG_ON(err
< 0))
1544 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream
*substream
,
1547 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1548 unsigned long flags
;
1549 snd_pcm_stream_lock_irqsave(substream
, flags
);
1550 if (snd_pcm_running(substream
) &&
1551 snd_pcm_update_hw_ptr(substream
) >= 0)
1552 runtime
->status
->hw_ptr
%= runtime
->buffer_size
;
1554 runtime
->status
->hw_ptr
= 0;
1555 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1559 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream
*substream
,
1562 struct snd_pcm_channel_info
*info
= arg
;
1563 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1565 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
)) {
1569 width
= snd_pcm_format_physical_width(runtime
->format
);
1573 switch (runtime
->access
) {
1574 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
:
1575 case SNDRV_PCM_ACCESS_RW_INTERLEAVED
:
1576 info
->first
= info
->channel
* width
;
1577 info
->step
= runtime
->channels
* width
;
1579 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
:
1580 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
:
1582 size_t size
= runtime
->dma_bytes
/ runtime
->channels
;
1583 info
->first
= info
->channel
* size
* 8;
1594 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream
*substream
,
1597 struct snd_pcm_hw_params
*params
= arg
;
1598 snd_pcm_format_t format
;
1599 int channels
, width
;
1601 params
->fifo_size
= substream
->runtime
->hw
.fifo_size
;
1602 if (!(substream
->runtime
->hw
.info
& SNDRV_PCM_INFO_FIFO_IN_FRAMES
)) {
1603 format
= params_format(params
);
1604 channels
= params_channels(params
);
1605 width
= snd_pcm_format_physical_width(format
);
1606 params
->fifo_size
/= width
* channels
;
1612 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1613 * @substream: the pcm substream instance
1614 * @cmd: ioctl command
1615 * @arg: ioctl argument
1617 * Processes the generic ioctl commands for PCM.
1618 * Can be passed as the ioctl callback for PCM ops.
1620 * Returns zero if successful, or a negative error code on failure.
1622 int snd_pcm_lib_ioctl(struct snd_pcm_substream
*substream
,
1623 unsigned int cmd
, void *arg
)
1626 case SNDRV_PCM_IOCTL1_INFO
:
1628 case SNDRV_PCM_IOCTL1_RESET
:
1629 return snd_pcm_lib_ioctl_reset(substream
, arg
);
1630 case SNDRV_PCM_IOCTL1_CHANNEL_INFO
:
1631 return snd_pcm_lib_ioctl_channel_info(substream
, arg
);
1632 case SNDRV_PCM_IOCTL1_FIFO_SIZE
:
1633 return snd_pcm_lib_ioctl_fifo_size(substream
, arg
);
1638 EXPORT_SYMBOL(snd_pcm_lib_ioctl
);
1641 * snd_pcm_period_elapsed - update the pcm status for the next period
1642 * @substream: the pcm substream instance
1644 * This function is called from the interrupt handler when the
1645 * PCM has processed the period size. It will update the current
1646 * pointer, wake up sleepers, etc.
1648 * Even if more than one periods have elapsed since the last call, you
1649 * have to call this only once.
1651 void snd_pcm_period_elapsed(struct snd_pcm_substream
*substream
)
1653 struct snd_pcm_runtime
*runtime
;
1654 unsigned long flags
;
1656 if (PCM_RUNTIME_CHECK(substream
))
1658 runtime
= substream
->runtime
;
1660 if (runtime
->transfer_ack_begin
)
1661 runtime
->transfer_ack_begin(substream
);
1663 snd_pcm_stream_lock_irqsave(substream
, flags
);
1664 if (!snd_pcm_running(substream
) ||
1665 snd_pcm_update_hw_ptr_interrupt(substream
) < 0)
1668 if (substream
->timer_running
)
1669 snd_timer_interrupt(substream
->timer
, 1);
1671 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1672 if (runtime
->transfer_ack_end
)
1673 runtime
->transfer_ack_end(substream
);
1674 kill_fasync(&runtime
->fasync
, SIGIO
, POLL_IN
);
1677 EXPORT_SYMBOL(snd_pcm_period_elapsed
);
1680 * Wait until avail_min data becomes available
1681 * Returns a negative error code if any error occurs during operation.
1682 * The available space is stored on availp. When err = 0 and avail = 0
1683 * on the capture stream, it indicates the stream is in DRAINING state.
1685 static int wait_for_avail_min(struct snd_pcm_substream
*substream
,
1686 snd_pcm_uframes_t
*availp
)
1688 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1689 int is_playback
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
1692 snd_pcm_uframes_t avail
= 0;
1695 init_waitqueue_entry(&wait
, current
);
1696 add_wait_queue(&runtime
->sleep
, &wait
);
1698 if (signal_pending(current
)) {
1702 set_current_state(TASK_INTERRUPTIBLE
);
1703 snd_pcm_stream_unlock_irq(substream
);
1704 tout
= schedule_timeout(msecs_to_jiffies(10000));
1705 snd_pcm_stream_lock_irq(substream
);
1706 switch (runtime
->status
->state
) {
1707 case SNDRV_PCM_STATE_SUSPENDED
:
1710 case SNDRV_PCM_STATE_XRUN
:
1713 case SNDRV_PCM_STATE_DRAINING
:
1717 avail
= 0; /* indicate draining */
1719 case SNDRV_PCM_STATE_OPEN
:
1720 case SNDRV_PCM_STATE_SETUP
:
1721 case SNDRV_PCM_STATE_DISCONNECTED
:
1726 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1727 is_playback
? "playback" : "capture");
1732 avail
= snd_pcm_playback_avail(runtime
);
1734 avail
= snd_pcm_capture_avail(runtime
);
1735 if (avail
>= runtime
->control
->avail_min
)
1739 remove_wait_queue(&runtime
->sleep
, &wait
);
1744 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream
*substream
,
1746 unsigned long data
, unsigned int off
,
1747 snd_pcm_uframes_t frames
)
1749 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1751 char __user
*buf
= (char __user
*) data
+ frames_to_bytes(runtime
, off
);
1752 if (substream
->ops
->copy
) {
1753 if ((err
= substream
->ops
->copy(substream
, -1, hwoff
, buf
, frames
)) < 0)
1756 char *hwbuf
= runtime
->dma_area
+ frames_to_bytes(runtime
, hwoff
);
1757 if (copy_from_user(hwbuf
, buf
, frames_to_bytes(runtime
, frames
)))
1763 typedef int (*transfer_f
)(struct snd_pcm_substream
*substream
, unsigned int hwoff
,
1764 unsigned long data
, unsigned int off
,
1765 snd_pcm_uframes_t size
);
1767 static snd_pcm_sframes_t
snd_pcm_lib_write1(struct snd_pcm_substream
*substream
,
1769 snd_pcm_uframes_t size
,
1771 transfer_f transfer
)
1773 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1774 snd_pcm_uframes_t xfer
= 0;
1775 snd_pcm_uframes_t offset
= 0;
1781 snd_pcm_stream_lock_irq(substream
);
1782 switch (runtime
->status
->state
) {
1783 case SNDRV_PCM_STATE_PREPARED
:
1784 case SNDRV_PCM_STATE_RUNNING
:
1785 case SNDRV_PCM_STATE_PAUSED
:
1787 case SNDRV_PCM_STATE_XRUN
:
1790 case SNDRV_PCM_STATE_SUSPENDED
:
1799 snd_pcm_uframes_t frames
, appl_ptr
, appl_ofs
;
1800 snd_pcm_uframes_t avail
;
1801 snd_pcm_uframes_t cont
;
1802 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
1803 snd_pcm_update_hw_ptr(substream
);
1804 avail
= snd_pcm_playback_avail(runtime
);
1810 err
= wait_for_avail_min(substream
, &avail
);
1814 frames
= size
> avail
? avail
: size
;
1815 cont
= runtime
->buffer_size
- runtime
->control
->appl_ptr
% runtime
->buffer_size
;
1818 if (snd_BUG_ON(!frames
)) {
1819 snd_pcm_stream_unlock_irq(substream
);
1822 appl_ptr
= runtime
->control
->appl_ptr
;
1823 appl_ofs
= appl_ptr
% runtime
->buffer_size
;
1824 snd_pcm_stream_unlock_irq(substream
);
1825 if ((err
= transfer(substream
, appl_ofs
, data
, offset
, frames
)) < 0)
1827 snd_pcm_stream_lock_irq(substream
);
1828 switch (runtime
->status
->state
) {
1829 case SNDRV_PCM_STATE_XRUN
:
1832 case SNDRV_PCM_STATE_SUSPENDED
:
1839 if (appl_ptr
>= runtime
->boundary
)
1840 appl_ptr
-= runtime
->boundary
;
1841 runtime
->control
->appl_ptr
= appl_ptr
;
1842 if (substream
->ops
->ack
)
1843 substream
->ops
->ack(substream
);
1848 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
&&
1849 snd_pcm_playback_hw_avail(runtime
) >= (snd_pcm_sframes_t
)runtime
->start_threshold
) {
1850 err
= snd_pcm_start(substream
);
1856 snd_pcm_stream_unlock_irq(substream
);
1858 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: err
;
1861 /* sanity-check for read/write methods */
1862 static int pcm_sanity_check(struct snd_pcm_substream
*substream
)
1864 struct snd_pcm_runtime
*runtime
;
1865 if (PCM_RUNTIME_CHECK(substream
))
1867 runtime
= substream
->runtime
;
1868 if (snd_BUG_ON(!substream
->ops
->copy
&& !runtime
->dma_area
))
1870 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1875 snd_pcm_sframes_t
snd_pcm_lib_write(struct snd_pcm_substream
*substream
, const void __user
*buf
, snd_pcm_uframes_t size
)
1877 struct snd_pcm_runtime
*runtime
;
1881 err
= pcm_sanity_check(substream
);
1884 runtime
= substream
->runtime
;
1885 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
1887 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_INTERLEAVED
&&
1888 runtime
->channels
> 1)
1890 return snd_pcm_lib_write1(substream
, (unsigned long)buf
, size
, nonblock
,
1891 snd_pcm_lib_write_transfer
);
1894 EXPORT_SYMBOL(snd_pcm_lib_write
);
1896 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream
*substream
,
1898 unsigned long data
, unsigned int off
,
1899 snd_pcm_uframes_t frames
)
1901 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1903 void __user
**bufs
= (void __user
**)data
;
1904 int channels
= runtime
->channels
;
1906 if (substream
->ops
->copy
) {
1907 if (snd_BUG_ON(!substream
->ops
->silence
))
1909 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
1910 if (*bufs
== NULL
) {
1911 if ((err
= substream
->ops
->silence(substream
, c
, hwoff
, frames
)) < 0)
1914 char __user
*buf
= *bufs
+ samples_to_bytes(runtime
, off
);
1915 if ((err
= substream
->ops
->copy(substream
, c
, hwoff
, buf
, frames
)) < 0)
1920 /* default transfer behaviour */
1921 size_t dma_csize
= runtime
->dma_bytes
/ channels
;
1922 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
1923 char *hwbuf
= runtime
->dma_area
+ (c
* dma_csize
) + samples_to_bytes(runtime
, hwoff
);
1924 if (*bufs
== NULL
) {
1925 snd_pcm_format_set_silence(runtime
->format
, hwbuf
, frames
);
1927 char __user
*buf
= *bufs
+ samples_to_bytes(runtime
, off
);
1928 if (copy_from_user(hwbuf
, buf
, samples_to_bytes(runtime
, frames
)))
1936 snd_pcm_sframes_t
snd_pcm_lib_writev(struct snd_pcm_substream
*substream
,
1938 snd_pcm_uframes_t frames
)
1940 struct snd_pcm_runtime
*runtime
;
1944 err
= pcm_sanity_check(substream
);
1947 runtime
= substream
->runtime
;
1948 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
1950 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
1952 return snd_pcm_lib_write1(substream
, (unsigned long)bufs
, frames
,
1953 nonblock
, snd_pcm_lib_writev_transfer
);
1956 EXPORT_SYMBOL(snd_pcm_lib_writev
);
1958 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream
*substream
,
1960 unsigned long data
, unsigned int off
,
1961 snd_pcm_uframes_t frames
)
1963 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1965 char __user
*buf
= (char __user
*) data
+ frames_to_bytes(runtime
, off
);
1966 if (substream
->ops
->copy
) {
1967 if ((err
= substream
->ops
->copy(substream
, -1, hwoff
, buf
, frames
)) < 0)
1970 char *hwbuf
= runtime
->dma_area
+ frames_to_bytes(runtime
, hwoff
);
1971 if (copy_to_user(buf
, hwbuf
, frames_to_bytes(runtime
, frames
)))
1977 static snd_pcm_sframes_t
snd_pcm_lib_read1(struct snd_pcm_substream
*substream
,
1979 snd_pcm_uframes_t size
,
1981 transfer_f transfer
)
1983 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1984 snd_pcm_uframes_t xfer
= 0;
1985 snd_pcm_uframes_t offset
= 0;
1991 snd_pcm_stream_lock_irq(substream
);
1992 switch (runtime
->status
->state
) {
1993 case SNDRV_PCM_STATE_PREPARED
:
1994 if (size
>= runtime
->start_threshold
) {
1995 err
= snd_pcm_start(substream
);
2000 case SNDRV_PCM_STATE_DRAINING
:
2001 case SNDRV_PCM_STATE_RUNNING
:
2002 case SNDRV_PCM_STATE_PAUSED
:
2004 case SNDRV_PCM_STATE_XRUN
:
2007 case SNDRV_PCM_STATE_SUSPENDED
:
2016 snd_pcm_uframes_t frames
, appl_ptr
, appl_ofs
;
2017 snd_pcm_uframes_t avail
;
2018 snd_pcm_uframes_t cont
;
2019 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
2020 snd_pcm_update_hw_ptr(substream
);
2021 avail
= snd_pcm_capture_avail(runtime
);
2023 if (runtime
->status
->state
==
2024 SNDRV_PCM_STATE_DRAINING
) {
2025 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
2032 err
= wait_for_avail_min(substream
, &avail
);
2036 continue; /* draining */
2038 frames
= size
> avail
? avail
: size
;
2039 cont
= runtime
->buffer_size
- runtime
->control
->appl_ptr
% runtime
->buffer_size
;
2042 if (snd_BUG_ON(!frames
)) {
2043 snd_pcm_stream_unlock_irq(substream
);
2046 appl_ptr
= runtime
->control
->appl_ptr
;
2047 appl_ofs
= appl_ptr
% runtime
->buffer_size
;
2048 snd_pcm_stream_unlock_irq(substream
);
2049 if ((err
= transfer(substream
, appl_ofs
, data
, offset
, frames
)) < 0)
2051 snd_pcm_stream_lock_irq(substream
);
2052 switch (runtime
->status
->state
) {
2053 case SNDRV_PCM_STATE_XRUN
:
2056 case SNDRV_PCM_STATE_SUSPENDED
:
2063 if (appl_ptr
>= runtime
->boundary
)
2064 appl_ptr
-= runtime
->boundary
;
2065 runtime
->control
->appl_ptr
= appl_ptr
;
2066 if (substream
->ops
->ack
)
2067 substream
->ops
->ack(substream
);
2074 snd_pcm_stream_unlock_irq(substream
);
2076 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: err
;
2079 snd_pcm_sframes_t
snd_pcm_lib_read(struct snd_pcm_substream
*substream
, void __user
*buf
, snd_pcm_uframes_t size
)
2081 struct snd_pcm_runtime
*runtime
;
2085 err
= pcm_sanity_check(substream
);
2088 runtime
= substream
->runtime
;
2089 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
2090 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_INTERLEAVED
)
2092 return snd_pcm_lib_read1(substream
, (unsigned long)buf
, size
, nonblock
, snd_pcm_lib_read_transfer
);
2095 EXPORT_SYMBOL(snd_pcm_lib_read
);
2097 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream
*substream
,
2099 unsigned long data
, unsigned int off
,
2100 snd_pcm_uframes_t frames
)
2102 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2104 void __user
**bufs
= (void __user
**)data
;
2105 int channels
= runtime
->channels
;
2107 if (substream
->ops
->copy
) {
2108 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
2112 buf
= *bufs
+ samples_to_bytes(runtime
, off
);
2113 if ((err
= substream
->ops
->copy(substream
, c
, hwoff
, buf
, frames
)) < 0)
2117 snd_pcm_uframes_t dma_csize
= runtime
->dma_bytes
/ channels
;
2118 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
2124 hwbuf
= runtime
->dma_area
+ (c
* dma_csize
) + samples_to_bytes(runtime
, hwoff
);
2125 buf
= *bufs
+ samples_to_bytes(runtime
, off
);
2126 if (copy_to_user(buf
, hwbuf
, samples_to_bytes(runtime
, frames
)))
2133 snd_pcm_sframes_t
snd_pcm_lib_readv(struct snd_pcm_substream
*substream
,
2135 snd_pcm_uframes_t frames
)
2137 struct snd_pcm_runtime
*runtime
;
2141 err
= pcm_sanity_check(substream
);
2144 runtime
= substream
->runtime
;
2145 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2148 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
2149 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
2151 return snd_pcm_lib_read1(substream
, (unsigned long)bufs
, frames
, nonblock
, snd_pcm_lib_readv_transfer
);
2154 EXPORT_SYMBOL(snd_pcm_lib_readv
);