2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
24 #include <linux/slab.h>
25 #include <linux/time.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
= (ofs
+ frames
) - runtime
->buffer_size
;
84 runtime
->silence_start
= ofs
- runtime
->silence_filled
;
86 if ((snd_pcm_sframes_t
)runtime
->silence_start
< 0)
87 runtime
->silence_start
+= runtime
->boundary
;
89 frames
= runtime
->buffer_size
- runtime
->silence_filled
;
91 snd_assert(frames
<= runtime
->buffer_size
, return);
94 ofs
= (runtime
->silence_start
+ runtime
->silence_filled
) % runtime
->buffer_size
;
96 transfer
= ofs
+ frames
> runtime
->buffer_size
? runtime
->buffer_size
- ofs
: frames
;
97 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
98 runtime
->access
== SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
) {
99 if (substream
->ops
->silence
) {
101 err
= substream
->ops
->silence(substream
, -1, ofs
, transfer
);
102 snd_assert(err
>= 0, );
104 char *hwbuf
= runtime
->dma_area
+ frames_to_bytes(runtime
, ofs
);
105 snd_pcm_format_set_silence(runtime
->format
, hwbuf
, transfer
* runtime
->channels
);
109 unsigned int channels
= runtime
->channels
;
110 if (substream
->ops
->silence
) {
111 for (c
= 0; c
< channels
; ++c
) {
113 err
= substream
->ops
->silence(substream
, c
, ofs
, transfer
);
114 snd_assert(err
>= 0, );
117 size_t dma_csize
= runtime
->dma_bytes
/ channels
;
118 for (c
= 0; c
< channels
; ++c
) {
119 char *hwbuf
= runtime
->dma_area
+ (c
* dma_csize
) + samples_to_bytes(runtime
, ofs
);
120 snd_pcm_format_set_silence(runtime
->format
, hwbuf
, transfer
);
124 runtime
->silence_filled
+= transfer
;
130 static void xrun(struct snd_pcm_substream
*substream
)
132 snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
133 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
134 if (substream
->pstr
->xrun_debug
) {
135 snd_printd(KERN_DEBUG
"XRUN: pcmC%dD%d%c\n",
136 substream
->pcm
->card
->number
,
137 substream
->pcm
->device
,
138 substream
->stream
? 'c' : 'p');
139 if (substream
->pstr
->xrun_debug
> 1)
145 static inline snd_pcm_uframes_t
snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream
*substream
,
146 struct snd_pcm_runtime
*runtime
)
148 snd_pcm_uframes_t pos
;
150 pos
= substream
->ops
->pointer(substream
);
151 if (pos
== SNDRV_PCM_POS_XRUN
)
152 return pos
; /* XRUN */
153 if (runtime
->tstamp_mode
& SNDRV_PCM_TSTAMP_MMAP
)
154 getnstimeofday((struct timespec
*)&runtime
->status
->tstamp
);
155 #ifdef CONFIG_SND_DEBUG
156 if (pos
>= runtime
->buffer_size
) {
157 snd_printk(KERN_ERR
"BUG: stream = %i, pos = 0x%lx, buffer size = 0x%lx, period size = 0x%lx\n", substream
->stream
, pos
, runtime
->buffer_size
, runtime
->period_size
);
160 pos
-= pos
% runtime
->min_align
;
164 static inline int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream
*substream
,
165 struct snd_pcm_runtime
*runtime
)
167 snd_pcm_uframes_t avail
;
169 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
170 avail
= snd_pcm_playback_avail(runtime
);
172 avail
= snd_pcm_capture_avail(runtime
);
173 if (avail
> runtime
->avail_max
)
174 runtime
->avail_max
= avail
;
175 if (avail
>= runtime
->stop_threshold
) {
176 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
)
177 snd_pcm_drain_done(substream
);
182 if (avail
>= runtime
->control
->avail_min
)
183 wake_up(&runtime
->sleep
);
187 static inline int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream
*substream
)
189 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
190 snd_pcm_uframes_t pos
;
191 snd_pcm_uframes_t new_hw_ptr
, hw_ptr_interrupt
;
192 snd_pcm_sframes_t delta
;
194 pos
= snd_pcm_update_hw_ptr_pos(substream
, runtime
);
195 if (pos
== SNDRV_PCM_POS_XRUN
) {
199 if (runtime
->period_size
== runtime
->buffer_size
)
201 new_hw_ptr
= runtime
->hw_ptr_base
+ pos
;
202 hw_ptr_interrupt
= runtime
->hw_ptr_interrupt
+ runtime
->period_size
;
204 delta
= hw_ptr_interrupt
- new_hw_ptr
;
206 if ((snd_pcm_uframes_t
)delta
< runtime
->buffer_size
/ 2) {
207 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
208 if (runtime
->periods
> 1 && substream
->pstr
->xrun_debug
) {
209 snd_printd(KERN_ERR
"Unexpected hw_pointer value [1] (stream = %i, delta: -%ld, max jitter = %ld): wrong interrupt acknowledge?\n", substream
->stream
, (long) delta
, runtime
->buffer_size
/ 2);
210 if (substream
->pstr
->xrun_debug
> 1)
217 runtime
->hw_ptr_base
+= runtime
->buffer_size
;
218 if (runtime
->hw_ptr_base
== runtime
->boundary
)
219 runtime
->hw_ptr_base
= 0;
220 new_hw_ptr
= runtime
->hw_ptr_base
+ pos
;
223 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
224 runtime
->silence_size
> 0)
225 snd_pcm_playback_silence(substream
, new_hw_ptr
);
227 runtime
->status
->hw_ptr
= new_hw_ptr
;
228 runtime
->hw_ptr_interrupt
= new_hw_ptr
- new_hw_ptr
% runtime
->period_size
;
230 return snd_pcm_update_hw_ptr_post(substream
, runtime
);
233 /* CAUTION: call it with irq disabled */
234 int snd_pcm_update_hw_ptr(struct snd_pcm_substream
*substream
)
236 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
237 snd_pcm_uframes_t pos
;
238 snd_pcm_uframes_t old_hw_ptr
, new_hw_ptr
;
239 snd_pcm_sframes_t delta
;
241 old_hw_ptr
= runtime
->status
->hw_ptr
;
242 pos
= snd_pcm_update_hw_ptr_pos(substream
, runtime
);
243 if (pos
== SNDRV_PCM_POS_XRUN
) {
247 new_hw_ptr
= runtime
->hw_ptr_base
+ pos
;
249 delta
= old_hw_ptr
- new_hw_ptr
;
251 if ((snd_pcm_uframes_t
)delta
< runtime
->buffer_size
/ 2) {
252 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
253 if (runtime
->periods
> 2 && substream
->pstr
->xrun_debug
) {
254 snd_printd(KERN_ERR
"Unexpected hw_pointer value [2] (stream = %i, delta: -%ld, max jitter = %ld): wrong interrupt acknowledge?\n", substream
->stream
, (long) delta
, runtime
->buffer_size
/ 2);
255 if (substream
->pstr
->xrun_debug
> 1)
261 runtime
->hw_ptr_base
+= runtime
->buffer_size
;
262 if (runtime
->hw_ptr_base
== runtime
->boundary
)
263 runtime
->hw_ptr_base
= 0;
264 new_hw_ptr
= runtime
->hw_ptr_base
+ pos
;
266 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
267 runtime
->silence_size
> 0)
268 snd_pcm_playback_silence(substream
, new_hw_ptr
);
270 runtime
->status
->hw_ptr
= new_hw_ptr
;
272 return snd_pcm_update_hw_ptr_post(substream
, runtime
);
276 * snd_pcm_set_ops - set the PCM operators
277 * @pcm: the pcm instance
278 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
279 * @ops: the operator table
281 * Sets the given PCM operators to the pcm instance.
283 void snd_pcm_set_ops(struct snd_pcm
*pcm
, int direction
, struct snd_pcm_ops
*ops
)
285 struct snd_pcm_str
*stream
= &pcm
->streams
[direction
];
286 struct snd_pcm_substream
*substream
;
288 for (substream
= stream
->substream
; substream
!= NULL
; substream
= substream
->next
)
289 substream
->ops
= ops
;
292 EXPORT_SYMBOL(snd_pcm_set_ops
);
295 * snd_pcm_sync - set the PCM sync id
296 * @substream: the pcm substream
298 * Sets the PCM sync identifier for the card.
300 void snd_pcm_set_sync(struct snd_pcm_substream
*substream
)
302 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
304 runtime
->sync
.id32
[0] = substream
->pcm
->card
->number
;
305 runtime
->sync
.id32
[1] = -1;
306 runtime
->sync
.id32
[2] = -1;
307 runtime
->sync
.id32
[3] = -1;
310 EXPORT_SYMBOL(snd_pcm_set_sync
);
313 * Standard ioctl routine
316 static inline unsigned int div32(unsigned int a
, unsigned int b
,
327 static inline unsigned int div_down(unsigned int a
, unsigned int b
)
334 static inline unsigned int div_up(unsigned int a
, unsigned int b
)
346 static inline unsigned int mul(unsigned int a
, unsigned int b
)
350 if (div_down(UINT_MAX
, a
) < b
)
355 static inline unsigned int muldiv32(unsigned int a
, unsigned int b
,
356 unsigned int c
, unsigned int *r
)
358 u_int64_t n
= (u_int64_t
) a
* b
;
373 * snd_interval_refine - refine the interval value of configurator
374 * @i: the interval value to refine
375 * @v: the interval value to refer to
377 * Refines the interval value with the reference value.
378 * The interval is changed to the range satisfying both intervals.
379 * The interval status (min, max, integer, etc.) are evaluated.
381 * Returns non-zero if the value is changed, zero if not changed.
383 int snd_interval_refine(struct snd_interval
*i
, const struct snd_interval
*v
)
386 snd_assert(!snd_interval_empty(i
), return -EINVAL
);
387 if (i
->min
< v
->min
) {
389 i
->openmin
= v
->openmin
;
391 } else if (i
->min
== v
->min
&& !i
->openmin
&& v
->openmin
) {
395 if (i
->max
> v
->max
) {
397 i
->openmax
= v
->openmax
;
399 } else if (i
->max
== v
->max
&& !i
->openmax
&& v
->openmax
) {
403 if (!i
->integer
&& v
->integer
) {
416 } else if (!i
->openmin
&& !i
->openmax
&& i
->min
== i
->max
)
418 if (snd_interval_checkempty(i
)) {
419 snd_interval_none(i
);
425 EXPORT_SYMBOL(snd_interval_refine
);
427 static int snd_interval_refine_first(struct snd_interval
*i
)
429 snd_assert(!snd_interval_empty(i
), return -EINVAL
);
430 if (snd_interval_single(i
))
433 i
->openmax
= i
->openmin
;
439 static int snd_interval_refine_last(struct snd_interval
*i
)
441 snd_assert(!snd_interval_empty(i
), return -EINVAL
);
442 if (snd_interval_single(i
))
445 i
->openmin
= i
->openmax
;
451 void snd_interval_mul(const struct snd_interval
*a
, const struct snd_interval
*b
, struct snd_interval
*c
)
453 if (a
->empty
|| b
->empty
) {
454 snd_interval_none(c
);
458 c
->min
= mul(a
->min
, b
->min
);
459 c
->openmin
= (a
->openmin
|| b
->openmin
);
460 c
->max
= mul(a
->max
, b
->max
);
461 c
->openmax
= (a
->openmax
|| b
->openmax
);
462 c
->integer
= (a
->integer
&& b
->integer
);
466 * snd_interval_div - refine the interval value with division
473 * Returns non-zero if the value is changed, zero if not changed.
475 void snd_interval_div(const struct snd_interval
*a
, const struct snd_interval
*b
, struct snd_interval
*c
)
478 if (a
->empty
|| b
->empty
) {
479 snd_interval_none(c
);
483 c
->min
= div32(a
->min
, b
->max
, &r
);
484 c
->openmin
= (r
|| a
->openmin
|| b
->openmax
);
486 c
->max
= div32(a
->max
, b
->min
, &r
);
491 c
->openmax
= (a
->openmax
|| b
->openmin
);
500 * snd_interval_muldivk - refine the interval value
503 * @k: divisor (as integer)
508 * Returns non-zero if the value is changed, zero if not changed.
510 void snd_interval_muldivk(const struct snd_interval
*a
, const struct snd_interval
*b
,
511 unsigned int k
, struct snd_interval
*c
)
514 if (a
->empty
|| b
->empty
) {
515 snd_interval_none(c
);
519 c
->min
= muldiv32(a
->min
, b
->min
, k
, &r
);
520 c
->openmin
= (r
|| a
->openmin
|| b
->openmin
);
521 c
->max
= muldiv32(a
->max
, b
->max
, k
, &r
);
526 c
->openmax
= (a
->openmax
|| b
->openmax
);
531 * snd_interval_mulkdiv - refine the interval value
533 * @k: dividend 2 (as integer)
539 * Returns non-zero if the value is changed, zero if not changed.
541 void snd_interval_mulkdiv(const struct snd_interval
*a
, unsigned int k
,
542 const struct snd_interval
*b
, struct snd_interval
*c
)
545 if (a
->empty
|| b
->empty
) {
546 snd_interval_none(c
);
550 c
->min
= muldiv32(a
->min
, k
, b
->max
, &r
);
551 c
->openmin
= (r
|| a
->openmin
|| b
->openmax
);
553 c
->max
= muldiv32(a
->max
, k
, b
->min
, &r
);
558 c
->openmax
= (a
->openmax
|| b
->openmin
);
570 * snd_interval_ratnum - refine the interval value
571 * @i: interval to refine
572 * @rats_count: number of ratnum_t
573 * @rats: ratnum_t array
574 * @nump: pointer to store the resultant numerator
575 * @denp: pointer to store the resultant denominator
577 * Returns non-zero if the value is changed, zero if not changed.
579 int snd_interval_ratnum(struct snd_interval
*i
,
580 unsigned int rats_count
, struct snd_ratnum
*rats
,
581 unsigned int *nump
, unsigned int *denp
)
583 unsigned int best_num
, best_diff
, best_den
;
585 struct snd_interval t
;
588 best_num
= best_den
= best_diff
= 0;
589 for (k
= 0; k
< rats_count
; ++k
) {
590 unsigned int num
= rats
[k
].num
;
592 unsigned int q
= i
->min
;
596 den
= div_down(num
, q
);
597 if (den
< rats
[k
].den_min
)
599 if (den
> rats
[k
].den_max
)
600 den
= rats
[k
].den_max
;
603 r
= (den
- rats
[k
].den_min
) % rats
[k
].den_step
;
607 diff
= num
- q
* den
;
609 diff
* best_den
< best_diff
* den
) {
619 t
.min
= div_down(best_num
, best_den
);
620 t
.openmin
= !!(best_num
% best_den
);
622 best_num
= best_den
= best_diff
= 0;
623 for (k
= 0; k
< rats_count
; ++k
) {
624 unsigned int num
= rats
[k
].num
;
626 unsigned int q
= i
->max
;
632 den
= div_up(num
, q
);
633 if (den
> rats
[k
].den_max
)
635 if (den
< rats
[k
].den_min
)
636 den
= rats
[k
].den_min
;
639 r
= (den
- rats
[k
].den_min
) % rats
[k
].den_step
;
641 den
+= rats
[k
].den_step
- r
;
643 diff
= q
* den
- num
;
645 diff
* best_den
< best_diff
* den
) {
655 t
.max
= div_up(best_num
, best_den
);
656 t
.openmax
= !!(best_num
% best_den
);
658 err
= snd_interval_refine(i
, &t
);
662 if (snd_interval_single(i
)) {
671 EXPORT_SYMBOL(snd_interval_ratnum
);
674 * snd_interval_ratden - refine the interval value
675 * @i: interval to refine
676 * @rats_count: number of struct ratden
677 * @rats: struct ratden array
678 * @nump: pointer to store the resultant numerator
679 * @denp: pointer to store the resultant denominator
681 * Returns non-zero if the value is changed, zero if not changed.
683 static int snd_interval_ratden(struct snd_interval
*i
,
684 unsigned int rats_count
, struct snd_ratden
*rats
,
685 unsigned int *nump
, unsigned int *denp
)
687 unsigned int best_num
, best_diff
, best_den
;
689 struct snd_interval t
;
692 best_num
= best_den
= best_diff
= 0;
693 for (k
= 0; k
< rats_count
; ++k
) {
695 unsigned int den
= rats
[k
].den
;
696 unsigned int q
= i
->min
;
699 if (num
> rats
[k
].num_max
)
701 if (num
< rats
[k
].num_min
)
702 num
= rats
[k
].num_max
;
705 r
= (num
- rats
[k
].num_min
) % rats
[k
].num_step
;
707 num
+= rats
[k
].num_step
- r
;
709 diff
= num
- q
* den
;
711 diff
* best_den
< best_diff
* den
) {
721 t
.min
= div_down(best_num
, best_den
);
722 t
.openmin
= !!(best_num
% best_den
);
724 best_num
= best_den
= best_diff
= 0;
725 for (k
= 0; k
< rats_count
; ++k
) {
727 unsigned int den
= rats
[k
].den
;
728 unsigned int q
= i
->max
;
731 if (num
< rats
[k
].num_min
)
733 if (num
> rats
[k
].num_max
)
734 num
= rats
[k
].num_max
;
737 r
= (num
- rats
[k
].num_min
) % rats
[k
].num_step
;
741 diff
= q
* den
- num
;
743 diff
* best_den
< best_diff
* den
) {
753 t
.max
= div_up(best_num
, best_den
);
754 t
.openmax
= !!(best_num
% best_den
);
756 err
= snd_interval_refine(i
, &t
);
760 if (snd_interval_single(i
)) {
770 * snd_interval_list - refine the interval value from the list
771 * @i: the interval value to refine
772 * @count: the number of elements in the list
773 * @list: the value list
774 * @mask: the bit-mask to evaluate
776 * Refines the interval value from the list.
777 * When mask is non-zero, only the elements corresponding to bit 1 are
780 * Returns non-zero if the value is changed, zero if not changed.
782 int snd_interval_list(struct snd_interval
*i
, unsigned int count
, unsigned int *list
, unsigned int mask
)
786 for (k
= 0; k
< count
; k
++) {
787 if (mask
&& !(mask
& (1 << k
)))
789 if (i
->min
== list
[k
] && !i
->openmin
)
791 if (i
->min
< list
[k
]) {
801 for (k
= count
; k
-- > 0;) {
802 if (mask
&& !(mask
& (1 << k
)))
804 if (i
->max
== list
[k
] && !i
->openmax
)
806 if (i
->max
> list
[k
]) {
816 if (snd_interval_checkempty(i
)) {
823 EXPORT_SYMBOL(snd_interval_list
);
825 static int snd_interval_step(struct snd_interval
*i
, unsigned int min
, unsigned int step
)
829 n
= (i
->min
- min
) % step
;
830 if (n
!= 0 || i
->openmin
) {
834 n
= (i
->max
- min
) % step
;
835 if (n
!= 0 || i
->openmax
) {
839 if (snd_interval_checkempty(i
)) {
846 /* Info constraints helpers */
849 * snd_pcm_hw_rule_add - add the hw-constraint rule
850 * @runtime: the pcm runtime instance
851 * @cond: condition bits
852 * @var: the variable to evaluate
853 * @func: the evaluation function
854 * @private: the private data pointer passed to function
855 * @dep: the dependent variables
857 * Returns zero if successful, or a negative error code on failure.
859 int snd_pcm_hw_rule_add(struct snd_pcm_runtime
*runtime
, unsigned int cond
,
861 snd_pcm_hw_rule_func_t func
, void *private,
864 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
865 struct snd_pcm_hw_rule
*c
;
869 if (constrs
->rules_num
>= constrs
->rules_all
) {
870 struct snd_pcm_hw_rule
*new;
871 unsigned int new_rules
= constrs
->rules_all
+ 16;
872 new = kcalloc(new_rules
, sizeof(*c
), GFP_KERNEL
);
875 if (constrs
->rules
) {
876 memcpy(new, constrs
->rules
,
877 constrs
->rules_num
* sizeof(*c
));
878 kfree(constrs
->rules
);
880 constrs
->rules
= new;
881 constrs
->rules_all
= new_rules
;
883 c
= &constrs
->rules
[constrs
->rules_num
];
887 c
->private = private;
890 snd_assert(k
< ARRAY_SIZE(c
->deps
), return -EINVAL
);
894 dep
= va_arg(args
, int);
896 constrs
->rules_num
++;
901 EXPORT_SYMBOL(snd_pcm_hw_rule_add
);
904 * snd_pcm_hw_constraint_mask
905 * @runtime: PCM runtime instance
906 * @var: hw_params variable to apply the mask
907 * @mask: the bitmap mask
909 * Apply the constraint of the given bitmap mask to a mask parameter.
911 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
,
914 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
915 struct snd_mask
*maskp
= constrs_mask(constrs
, var
);
916 *maskp
->bits
&= mask
;
917 memset(maskp
->bits
+ 1, 0, (SNDRV_MASK_MAX
-32) / 8); /* clear rest */
918 if (*maskp
->bits
== 0)
924 * snd_pcm_hw_constraint_mask64
925 * @runtime: PCM runtime instance
926 * @var: hw_params variable to apply the mask
927 * @mask: the 64bit bitmap mask
929 * Apply the constraint of the given bitmap mask to a mask parameter.
931 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
,
934 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
935 struct snd_mask
*maskp
= constrs_mask(constrs
, var
);
936 maskp
->bits
[0] &= (u_int32_t
)mask
;
937 maskp
->bits
[1] &= (u_int32_t
)(mask
>> 32);
938 memset(maskp
->bits
+ 2, 0, (SNDRV_MASK_MAX
-64) / 8); /* clear rest */
939 if (! maskp
->bits
[0] && ! maskp
->bits
[1])
945 * snd_pcm_hw_constraint_integer
946 * @runtime: PCM runtime instance
947 * @var: hw_params variable to apply the integer constraint
949 * Apply the constraint of integer to an interval parameter.
951 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
)
953 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
954 return snd_interval_setinteger(constrs_interval(constrs
, var
));
957 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer
);
960 * snd_pcm_hw_constraint_minmax
961 * @runtime: PCM runtime instance
962 * @var: hw_params variable to apply the range
963 * @min: the minimal value
964 * @max: the maximal value
966 * Apply the min/max range constraint to an interval parameter.
968 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
,
969 unsigned int min
, unsigned int max
)
971 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
972 struct snd_interval t
;
975 t
.openmin
= t
.openmax
= 0;
977 return snd_interval_refine(constrs_interval(constrs
, var
), &t
);
980 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax
);
982 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params
*params
,
983 struct snd_pcm_hw_rule
*rule
)
985 struct snd_pcm_hw_constraint_list
*list
= rule
->private;
986 return snd_interval_list(hw_param_interval(params
, rule
->var
), list
->count
, list
->list
, list
->mask
);
991 * snd_pcm_hw_constraint_list
992 * @runtime: PCM runtime instance
993 * @cond: condition bits
994 * @var: hw_params variable to apply the list constraint
997 * Apply the list of constraints to an interval parameter.
999 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime
*runtime
,
1001 snd_pcm_hw_param_t var
,
1002 struct snd_pcm_hw_constraint_list
*l
)
1004 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1005 snd_pcm_hw_rule_list
, l
,
1009 EXPORT_SYMBOL(snd_pcm_hw_constraint_list
);
1011 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params
*params
,
1012 struct snd_pcm_hw_rule
*rule
)
1014 struct snd_pcm_hw_constraint_ratnums
*r
= rule
->private;
1015 unsigned int num
= 0, den
= 0;
1017 err
= snd_interval_ratnum(hw_param_interval(params
, rule
->var
),
1018 r
->nrats
, r
->rats
, &num
, &den
);
1019 if (err
>= 0 && den
&& rule
->var
== SNDRV_PCM_HW_PARAM_RATE
) {
1020 params
->rate_num
= num
;
1021 params
->rate_den
= den
;
1027 * snd_pcm_hw_constraint_ratnums
1028 * @runtime: PCM runtime instance
1029 * @cond: condition bits
1030 * @var: hw_params variable to apply the ratnums constraint
1031 * @r: struct snd_ratnums constriants
1033 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime
*runtime
,
1035 snd_pcm_hw_param_t var
,
1036 struct snd_pcm_hw_constraint_ratnums
*r
)
1038 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1039 snd_pcm_hw_rule_ratnums
, r
,
1043 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums
);
1045 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params
*params
,
1046 struct snd_pcm_hw_rule
*rule
)
1048 struct snd_pcm_hw_constraint_ratdens
*r
= rule
->private;
1049 unsigned int num
= 0, den
= 0;
1050 int err
= snd_interval_ratden(hw_param_interval(params
, rule
->var
),
1051 r
->nrats
, r
->rats
, &num
, &den
);
1052 if (err
>= 0 && den
&& rule
->var
== SNDRV_PCM_HW_PARAM_RATE
) {
1053 params
->rate_num
= num
;
1054 params
->rate_den
= den
;
1060 * snd_pcm_hw_constraint_ratdens
1061 * @runtime: PCM runtime instance
1062 * @cond: condition bits
1063 * @var: hw_params variable to apply the ratdens constraint
1064 * @r: struct snd_ratdens constriants
1066 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime
*runtime
,
1068 snd_pcm_hw_param_t var
,
1069 struct snd_pcm_hw_constraint_ratdens
*r
)
1071 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1072 snd_pcm_hw_rule_ratdens
, r
,
1076 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens
);
1078 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params
*params
,
1079 struct snd_pcm_hw_rule
*rule
)
1081 unsigned int l
= (unsigned long) rule
->private;
1082 int width
= l
& 0xffff;
1083 unsigned int msbits
= l
>> 16;
1084 struct snd_interval
*i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
1085 if (snd_interval_single(i
) && snd_interval_value(i
) == width
)
1086 params
->msbits
= msbits
;
1091 * snd_pcm_hw_constraint_msbits
1092 * @runtime: PCM runtime instance
1093 * @cond: condition bits
1094 * @width: sample bits width
1095 * @msbits: msbits width
1097 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime
*runtime
,
1100 unsigned int msbits
)
1102 unsigned long l
= (msbits
<< 16) | width
;
1103 return snd_pcm_hw_rule_add(runtime
, cond
, -1,
1104 snd_pcm_hw_rule_msbits
,
1106 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1109 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits
);
1111 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params
*params
,
1112 struct snd_pcm_hw_rule
*rule
)
1114 unsigned long step
= (unsigned long) rule
->private;
1115 return snd_interval_step(hw_param_interval(params
, rule
->var
), 0, step
);
1119 * snd_pcm_hw_constraint_step
1120 * @runtime: PCM runtime instance
1121 * @cond: condition bits
1122 * @var: hw_params variable to apply the step constraint
1125 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime
*runtime
,
1127 snd_pcm_hw_param_t var
,
1130 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1131 snd_pcm_hw_rule_step
, (void *) step
,
1135 EXPORT_SYMBOL(snd_pcm_hw_constraint_step
);
1137 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params
*params
, struct snd_pcm_hw_rule
*rule
)
1139 static int pow2_sizes
[] = {
1140 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1141 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1142 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1143 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1145 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1146 ARRAY_SIZE(pow2_sizes
), pow2_sizes
, 0);
1150 * snd_pcm_hw_constraint_pow2
1151 * @runtime: PCM runtime instance
1152 * @cond: condition bits
1153 * @var: hw_params variable to apply the power-of-2 constraint
1155 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime
*runtime
,
1157 snd_pcm_hw_param_t var
)
1159 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1160 snd_pcm_hw_rule_pow2
, NULL
,
1164 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2
);
1166 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params
*params
,
1167 snd_pcm_hw_param_t var
)
1169 if (hw_is_mask(var
)) {
1170 snd_mask_any(hw_param_mask(params
, var
));
1171 params
->cmask
|= 1 << var
;
1172 params
->rmask
|= 1 << var
;
1175 if (hw_is_interval(var
)) {
1176 snd_interval_any(hw_param_interval(params
, var
));
1177 params
->cmask
|= 1 << var
;
1178 params
->rmask
|= 1 << var
;
1184 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params
*params
)
1187 memset(params
, 0, sizeof(*params
));
1188 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++)
1189 _snd_pcm_hw_param_any(params
, k
);
1190 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
1191 _snd_pcm_hw_param_any(params
, k
);
1195 EXPORT_SYMBOL(_snd_pcm_hw_params_any
);
1198 * snd_pcm_hw_param_value
1199 * @params: the hw_params instance
1200 * @var: parameter to retrieve
1201 * @dir: pointer to the direction (-1,0,1) or NULL
1203 * Return the value for field PAR if it's fixed in configuration space
1204 * defined by PARAMS. Return -EINVAL otherwise
1206 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params
*params
,
1207 snd_pcm_hw_param_t var
, int *dir
)
1209 if (hw_is_mask(var
)) {
1210 const struct snd_mask
*mask
= hw_param_mask_c(params
, var
);
1211 if (!snd_mask_single(mask
))
1215 return snd_mask_value(mask
);
1217 if (hw_is_interval(var
)) {
1218 const struct snd_interval
*i
= hw_param_interval_c(params
, var
);
1219 if (!snd_interval_single(i
))
1223 return snd_interval_value(i
);
1228 EXPORT_SYMBOL(snd_pcm_hw_param_value
);
1230 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params
*params
,
1231 snd_pcm_hw_param_t var
)
1233 if (hw_is_mask(var
)) {
1234 snd_mask_none(hw_param_mask(params
, var
));
1235 params
->cmask
|= 1 << var
;
1236 params
->rmask
|= 1 << var
;
1237 } else if (hw_is_interval(var
)) {
1238 snd_interval_none(hw_param_interval(params
, var
));
1239 params
->cmask
|= 1 << var
;
1240 params
->rmask
|= 1 << var
;
1246 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty
);
1248 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params
*params
,
1249 snd_pcm_hw_param_t var
)
1252 if (hw_is_mask(var
))
1253 changed
= snd_mask_refine_first(hw_param_mask(params
, var
));
1254 else if (hw_is_interval(var
))
1255 changed
= snd_interval_refine_first(hw_param_interval(params
, var
));
1259 params
->cmask
|= 1 << var
;
1260 params
->rmask
|= 1 << var
;
1267 * snd_pcm_hw_param_first
1268 * @pcm: PCM instance
1269 * @params: the hw_params instance
1270 * @var: parameter to retrieve
1271 * @dir: pointer to the direction (-1,0,1) or NULL
1273 * Inside configuration space defined by PARAMS remove from PAR all
1274 * values > minimum. Reduce configuration space accordingly.
1275 * Return the minimum.
1277 int snd_pcm_hw_param_first(struct snd_pcm_substream
*pcm
,
1278 struct snd_pcm_hw_params
*params
,
1279 snd_pcm_hw_param_t var
, int *dir
)
1281 int changed
= _snd_pcm_hw_param_first(params
, var
);
1284 if (params
->rmask
) {
1285 int err
= snd_pcm_hw_refine(pcm
, params
);
1286 snd_assert(err
>= 0, return err
);
1288 return snd_pcm_hw_param_value(params
, var
, dir
);
1291 EXPORT_SYMBOL(snd_pcm_hw_param_first
);
1293 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params
*params
,
1294 snd_pcm_hw_param_t var
)
1297 if (hw_is_mask(var
))
1298 changed
= snd_mask_refine_last(hw_param_mask(params
, var
));
1299 else if (hw_is_interval(var
))
1300 changed
= snd_interval_refine_last(hw_param_interval(params
, var
));
1304 params
->cmask
|= 1 << var
;
1305 params
->rmask
|= 1 << var
;
1312 * snd_pcm_hw_param_last
1313 * @pcm: PCM instance
1314 * @params: the hw_params instance
1315 * @var: parameter to retrieve
1316 * @dir: pointer to the direction (-1,0,1) or NULL
1318 * Inside configuration space defined by PARAMS remove from PAR all
1319 * values < maximum. Reduce configuration space accordingly.
1320 * Return the maximum.
1322 int snd_pcm_hw_param_last(struct snd_pcm_substream
*pcm
,
1323 struct snd_pcm_hw_params
*params
,
1324 snd_pcm_hw_param_t var
, int *dir
)
1326 int changed
= _snd_pcm_hw_param_last(params
, var
);
1329 if (params
->rmask
) {
1330 int err
= snd_pcm_hw_refine(pcm
, params
);
1331 snd_assert(err
>= 0, return err
);
1333 return snd_pcm_hw_param_value(params
, var
, dir
);
1336 EXPORT_SYMBOL(snd_pcm_hw_param_last
);
1339 * snd_pcm_hw_param_choose
1340 * @pcm: PCM instance
1341 * @params: the hw_params instance
1343 * Choose one configuration from configuration space defined by PARAMS
1344 * The configuration chosen is that obtained fixing in this order:
1345 * first access, first format, first subformat, min channels,
1346 * min rate, min period time, max buffer size, min tick time
1348 int snd_pcm_hw_params_choose(struct snd_pcm_substream
*pcm
,
1349 struct snd_pcm_hw_params
*params
)
1351 static int vars
[] = {
1352 SNDRV_PCM_HW_PARAM_ACCESS
,
1353 SNDRV_PCM_HW_PARAM_FORMAT
,
1354 SNDRV_PCM_HW_PARAM_SUBFORMAT
,
1355 SNDRV_PCM_HW_PARAM_CHANNELS
,
1356 SNDRV_PCM_HW_PARAM_RATE
,
1357 SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1358 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1359 SNDRV_PCM_HW_PARAM_TICK_TIME
,
1364 for (v
= vars
; *v
!= -1; v
++) {
1365 if (*v
!= SNDRV_PCM_HW_PARAM_BUFFER_SIZE
)
1366 err
= snd_pcm_hw_param_first(pcm
, params
, *v
, NULL
);
1368 err
= snd_pcm_hw_param_last(pcm
, params
, *v
, NULL
);
1369 snd_assert(err
>= 0, return err
);
1374 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream
*substream
,
1377 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1378 unsigned long flags
;
1379 snd_pcm_stream_lock_irqsave(substream
, flags
);
1380 if (snd_pcm_running(substream
) &&
1381 snd_pcm_update_hw_ptr(substream
) >= 0)
1382 runtime
->status
->hw_ptr
%= runtime
->buffer_size
;
1384 runtime
->status
->hw_ptr
= 0;
1385 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1389 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream
*substream
,
1392 struct snd_pcm_channel_info
*info
= arg
;
1393 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1395 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
)) {
1399 width
= snd_pcm_format_physical_width(runtime
->format
);
1403 switch (runtime
->access
) {
1404 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
:
1405 case SNDRV_PCM_ACCESS_RW_INTERLEAVED
:
1406 info
->first
= info
->channel
* width
;
1407 info
->step
= runtime
->channels
* width
;
1409 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
:
1410 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
:
1412 size_t size
= runtime
->dma_bytes
/ runtime
->channels
;
1413 info
->first
= info
->channel
* size
* 8;
1425 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1426 * @substream: the pcm substream instance
1427 * @cmd: ioctl command
1428 * @arg: ioctl argument
1430 * Processes the generic ioctl commands for PCM.
1431 * Can be passed as the ioctl callback for PCM ops.
1433 * Returns zero if successful, or a negative error code on failure.
1435 int snd_pcm_lib_ioctl(struct snd_pcm_substream
*substream
,
1436 unsigned int cmd
, void *arg
)
1439 case SNDRV_PCM_IOCTL1_INFO
:
1441 case SNDRV_PCM_IOCTL1_RESET
:
1442 return snd_pcm_lib_ioctl_reset(substream
, arg
);
1443 case SNDRV_PCM_IOCTL1_CHANNEL_INFO
:
1444 return snd_pcm_lib_ioctl_channel_info(substream
, arg
);
1449 EXPORT_SYMBOL(snd_pcm_lib_ioctl
);
1455 static void snd_pcm_system_tick_set(struct snd_pcm_substream
*substream
,
1456 unsigned long ticks
)
1458 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1460 del_timer(&runtime
->tick_timer
);
1462 ticks
+= (1000000 / HZ
) - 1;
1463 ticks
/= (1000000 / HZ
);
1464 mod_timer(&runtime
->tick_timer
, jiffies
+ ticks
);
1468 /* Temporary alias */
1469 void snd_pcm_tick_set(struct snd_pcm_substream
*substream
, unsigned long ticks
)
1471 snd_pcm_system_tick_set(substream
, ticks
);
1474 void snd_pcm_tick_prepare(struct snd_pcm_substream
*substream
)
1476 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1477 snd_pcm_uframes_t frames
= ULONG_MAX
;
1478 snd_pcm_uframes_t avail
, dist
;
1482 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1483 if (runtime
->silence_size
>= runtime
->boundary
) {
1485 } else if (runtime
->silence_size
> 0 &&
1486 runtime
->silence_filled
< runtime
->buffer_size
) {
1487 snd_pcm_sframes_t noise_dist
;
1488 noise_dist
= snd_pcm_playback_hw_avail(runtime
) + runtime
->silence_filled
;
1489 if (noise_dist
> (snd_pcm_sframes_t
)runtime
->silence_threshold
)
1490 frames
= noise_dist
- runtime
->silence_threshold
;
1492 avail
= snd_pcm_playback_avail(runtime
);
1494 avail
= snd_pcm_capture_avail(runtime
);
1496 if (avail
< runtime
->control
->avail_min
) {
1497 snd_pcm_sframes_t n
= runtime
->control
->avail_min
- avail
;
1498 if (n
> 0 && frames
> (snd_pcm_uframes_t
)n
)
1501 if (avail
< runtime
->buffer_size
) {
1502 snd_pcm_sframes_t n
= runtime
->buffer_size
- avail
;
1503 if (n
> 0 && frames
> (snd_pcm_uframes_t
)n
)
1506 if (frames
== ULONG_MAX
) {
1507 snd_pcm_tick_set(substream
, 0);
1510 dist
= runtime
->status
->hw_ptr
- runtime
->hw_ptr_base
;
1511 /* Distance to next interrupt */
1512 dist
= runtime
->period_size
- dist
% runtime
->period_size
;
1513 if (dist
<= frames
) {
1514 snd_pcm_tick_set(substream
, 0);
1517 /* the base time is us */
1520 div64_32(&n
, runtime
->tick_time
* runtime
->rate
, &r
);
1521 ticks
= n
+ (r
> 0 ? 1 : 0);
1522 if (ticks
< runtime
->sleep_min
)
1523 ticks
= runtime
->sleep_min
;
1524 snd_pcm_tick_set(substream
, (unsigned long) ticks
);
1527 void snd_pcm_tick_elapsed(struct snd_pcm_substream
*substream
)
1529 struct snd_pcm_runtime
*runtime
;
1530 unsigned long flags
;
1532 snd_assert(substream
!= NULL
, return);
1533 runtime
= substream
->runtime
;
1534 snd_assert(runtime
!= NULL
, return);
1536 snd_pcm_stream_lock_irqsave(substream
, flags
);
1537 if (!snd_pcm_running(substream
) ||
1538 snd_pcm_update_hw_ptr(substream
) < 0)
1540 if (runtime
->sleep_min
)
1541 snd_pcm_tick_prepare(substream
);
1543 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1547 * snd_pcm_period_elapsed - update the pcm status for the next period
1548 * @substream: the pcm substream instance
1550 * This function is called from the interrupt handler when the
1551 * PCM has processed the period size. It will update the current
1552 * pointer, set up the tick, wake up sleepers, etc.
1554 * Even if more than one periods have elapsed since the last call, you
1555 * have to call this only once.
1557 void snd_pcm_period_elapsed(struct snd_pcm_substream
*substream
)
1559 struct snd_pcm_runtime
*runtime
;
1560 unsigned long flags
;
1562 snd_assert(substream
!= NULL
, return);
1563 runtime
= substream
->runtime
;
1564 snd_assert(runtime
!= NULL
, return);
1566 if (runtime
->transfer_ack_begin
)
1567 runtime
->transfer_ack_begin(substream
);
1569 snd_pcm_stream_lock_irqsave(substream
, flags
);
1570 if (!snd_pcm_running(substream
) ||
1571 snd_pcm_update_hw_ptr_interrupt(substream
) < 0)
1574 if (substream
->timer_running
)
1575 snd_timer_interrupt(substream
->timer
, 1);
1576 if (runtime
->sleep_min
)
1577 snd_pcm_tick_prepare(substream
);
1579 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1580 if (runtime
->transfer_ack_end
)
1581 runtime
->transfer_ack_end(substream
);
1582 kill_fasync(&runtime
->fasync
, SIGIO
, POLL_IN
);
1585 EXPORT_SYMBOL(snd_pcm_period_elapsed
);
1587 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream
*substream
,
1589 unsigned long data
, unsigned int off
,
1590 snd_pcm_uframes_t frames
)
1592 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1594 char __user
*buf
= (char __user
*) data
+ frames_to_bytes(runtime
, off
);
1595 if (substream
->ops
->copy
) {
1596 if ((err
= substream
->ops
->copy(substream
, -1, hwoff
, buf
, frames
)) < 0)
1599 char *hwbuf
= runtime
->dma_area
+ frames_to_bytes(runtime
, hwoff
);
1600 snd_assert(runtime
->dma_area
, return -EFAULT
);
1601 if (copy_from_user(hwbuf
, buf
, frames_to_bytes(runtime
, frames
)))
1607 typedef int (*transfer_f
)(struct snd_pcm_substream
*substream
, unsigned int hwoff
,
1608 unsigned long data
, unsigned int off
,
1609 snd_pcm_uframes_t size
);
1611 static snd_pcm_sframes_t
snd_pcm_lib_write1(struct snd_pcm_substream
*substream
,
1613 snd_pcm_uframes_t size
,
1615 transfer_f transfer
)
1617 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1618 snd_pcm_uframes_t xfer
= 0;
1619 snd_pcm_uframes_t offset
= 0;
1624 if (size
> runtime
->xfer_align
)
1625 size
-= size
% runtime
->xfer_align
;
1627 snd_pcm_stream_lock_irq(substream
);
1628 switch (runtime
->status
->state
) {
1629 case SNDRV_PCM_STATE_PREPARED
:
1630 case SNDRV_PCM_STATE_RUNNING
:
1631 case SNDRV_PCM_STATE_PAUSED
:
1633 case SNDRV_PCM_STATE_XRUN
:
1636 case SNDRV_PCM_STATE_SUSPENDED
:
1645 snd_pcm_uframes_t frames
, appl_ptr
, appl_ofs
;
1646 snd_pcm_uframes_t avail
;
1647 snd_pcm_uframes_t cont
;
1648 if (runtime
->sleep_min
== 0 && runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
1649 snd_pcm_update_hw_ptr(substream
);
1650 avail
= snd_pcm_playback_avail(runtime
);
1651 if (((avail
< runtime
->control
->avail_min
&& size
> avail
) ||
1652 (size
>= runtime
->xfer_align
&& avail
< runtime
->xfer_align
))) {
1654 enum { READY
, SIGNALED
, ERROR
, SUSPENDED
, EXPIRED
, DROPPED
} state
;
1662 init_waitqueue_entry(&wait
, current
);
1663 add_wait_queue(&runtime
->sleep
, &wait
);
1665 if (signal_pending(current
)) {
1669 set_current_state(TASK_INTERRUPTIBLE
);
1670 snd_pcm_stream_unlock_irq(substream
);
1671 tout
= schedule_timeout(10 * HZ
);
1672 snd_pcm_stream_lock_irq(substream
);
1674 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
&&
1675 runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
) {
1676 state
= runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
? SUSPENDED
: EXPIRED
;
1680 switch (runtime
->status
->state
) {
1681 case SNDRV_PCM_STATE_XRUN
:
1682 case SNDRV_PCM_STATE_DRAINING
:
1685 case SNDRV_PCM_STATE_SUSPENDED
:
1688 case SNDRV_PCM_STATE_SETUP
:
1694 avail
= snd_pcm_playback_avail(runtime
);
1695 if (avail
>= runtime
->control
->avail_min
) {
1701 remove_wait_queue(&runtime
->sleep
, &wait
);
1714 snd_printd("playback write error (DMA or IRQ trouble?)\n");
1724 if (avail
> runtime
->xfer_align
)
1725 avail
-= avail
% runtime
->xfer_align
;
1726 frames
= size
> avail
? avail
: size
;
1727 cont
= runtime
->buffer_size
- runtime
->control
->appl_ptr
% runtime
->buffer_size
;
1730 snd_assert(frames
!= 0, snd_pcm_stream_unlock_irq(substream
); return -EINVAL
);
1731 appl_ptr
= runtime
->control
->appl_ptr
;
1732 appl_ofs
= appl_ptr
% runtime
->buffer_size
;
1733 snd_pcm_stream_unlock_irq(substream
);
1734 if ((err
= transfer(substream
, appl_ofs
, data
, offset
, frames
)) < 0)
1736 snd_pcm_stream_lock_irq(substream
);
1737 switch (runtime
->status
->state
) {
1738 case SNDRV_PCM_STATE_XRUN
:
1741 case SNDRV_PCM_STATE_SUSPENDED
:
1748 if (appl_ptr
>= runtime
->boundary
)
1749 appl_ptr
-= runtime
->boundary
;
1750 runtime
->control
->appl_ptr
= appl_ptr
;
1751 if (substream
->ops
->ack
)
1752 substream
->ops
->ack(substream
);
1757 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
&&
1758 snd_pcm_playback_hw_avail(runtime
) >= (snd_pcm_sframes_t
)runtime
->start_threshold
) {
1759 err
= snd_pcm_start(substream
);
1763 if (runtime
->sleep_min
&&
1764 runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
1765 snd_pcm_tick_prepare(substream
);
1768 snd_pcm_stream_unlock_irq(substream
);
1770 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: err
;
1773 snd_pcm_sframes_t
snd_pcm_lib_write(struct snd_pcm_substream
*substream
, const void __user
*buf
, snd_pcm_uframes_t size
)
1775 struct snd_pcm_runtime
*runtime
;
1778 snd_assert(substream
!= NULL
, return -ENXIO
);
1779 runtime
= substream
->runtime
;
1780 snd_assert(runtime
!= NULL
, return -ENXIO
);
1781 snd_assert(substream
->ops
->copy
!= NULL
|| runtime
->dma_area
!= NULL
, return -EINVAL
);
1782 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1785 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
1787 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_INTERLEAVED
&&
1788 runtime
->channels
> 1)
1790 return snd_pcm_lib_write1(substream
, (unsigned long)buf
, size
, nonblock
,
1791 snd_pcm_lib_write_transfer
);
1794 EXPORT_SYMBOL(snd_pcm_lib_write
);
1796 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream
*substream
,
1798 unsigned long data
, unsigned int off
,
1799 snd_pcm_uframes_t frames
)
1801 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1803 void __user
**bufs
= (void __user
**)data
;
1804 int channels
= runtime
->channels
;
1806 if (substream
->ops
->copy
) {
1807 snd_assert(substream
->ops
->silence
!= NULL
, return -EINVAL
);
1808 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
1809 if (*bufs
== NULL
) {
1810 if ((err
= substream
->ops
->silence(substream
, c
, hwoff
, frames
)) < 0)
1813 char __user
*buf
= *bufs
+ samples_to_bytes(runtime
, off
);
1814 if ((err
= substream
->ops
->copy(substream
, c
, hwoff
, buf
, frames
)) < 0)
1819 /* default transfer behaviour */
1820 size_t dma_csize
= runtime
->dma_bytes
/ channels
;
1821 snd_assert(runtime
->dma_area
, return -EFAULT
);
1822 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
1823 char *hwbuf
= runtime
->dma_area
+ (c
* dma_csize
) + samples_to_bytes(runtime
, hwoff
);
1824 if (*bufs
== NULL
) {
1825 snd_pcm_format_set_silence(runtime
->format
, hwbuf
, frames
);
1827 char __user
*buf
= *bufs
+ samples_to_bytes(runtime
, off
);
1828 if (copy_from_user(hwbuf
, buf
, samples_to_bytes(runtime
, frames
)))
1836 snd_pcm_sframes_t
snd_pcm_lib_writev(struct snd_pcm_substream
*substream
,
1838 snd_pcm_uframes_t frames
)
1840 struct snd_pcm_runtime
*runtime
;
1843 snd_assert(substream
!= NULL
, return -ENXIO
);
1844 runtime
= substream
->runtime
;
1845 snd_assert(runtime
!= NULL
, return -ENXIO
);
1846 snd_assert(substream
->ops
->copy
!= NULL
|| runtime
->dma_area
!= NULL
, return -EINVAL
);
1847 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1850 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
1852 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
1854 return snd_pcm_lib_write1(substream
, (unsigned long)bufs
, frames
,
1855 nonblock
, snd_pcm_lib_writev_transfer
);
1858 EXPORT_SYMBOL(snd_pcm_lib_writev
);
1860 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream
*substream
,
1862 unsigned long data
, unsigned int off
,
1863 snd_pcm_uframes_t frames
)
1865 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1867 char __user
*buf
= (char __user
*) data
+ frames_to_bytes(runtime
, off
);
1868 if (substream
->ops
->copy
) {
1869 if ((err
= substream
->ops
->copy(substream
, -1, hwoff
, buf
, frames
)) < 0)
1872 char *hwbuf
= runtime
->dma_area
+ frames_to_bytes(runtime
, hwoff
);
1873 snd_assert(runtime
->dma_area
, return -EFAULT
);
1874 if (copy_to_user(buf
, hwbuf
, frames_to_bytes(runtime
, frames
)))
1880 static snd_pcm_sframes_t
snd_pcm_lib_read1(struct snd_pcm_substream
*substream
,
1882 snd_pcm_uframes_t size
,
1884 transfer_f transfer
)
1886 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1887 snd_pcm_uframes_t xfer
= 0;
1888 snd_pcm_uframes_t offset
= 0;
1893 if (size
> runtime
->xfer_align
)
1894 size
-= size
% runtime
->xfer_align
;
1896 snd_pcm_stream_lock_irq(substream
);
1897 switch (runtime
->status
->state
) {
1898 case SNDRV_PCM_STATE_PREPARED
:
1899 if (size
>= runtime
->start_threshold
) {
1900 err
= snd_pcm_start(substream
);
1905 case SNDRV_PCM_STATE_DRAINING
:
1906 case SNDRV_PCM_STATE_RUNNING
:
1907 case SNDRV_PCM_STATE_PAUSED
:
1909 case SNDRV_PCM_STATE_XRUN
:
1912 case SNDRV_PCM_STATE_SUSPENDED
:
1921 snd_pcm_uframes_t frames
, appl_ptr
, appl_ofs
;
1922 snd_pcm_uframes_t avail
;
1923 snd_pcm_uframes_t cont
;
1924 if (runtime
->sleep_min
== 0 && runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
1925 snd_pcm_update_hw_ptr(substream
);
1927 avail
= snd_pcm_capture_avail(runtime
);
1928 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
1929 if (avail
< runtime
->xfer_align
) {
1933 } else if ((avail
< runtime
->control
->avail_min
&& size
> avail
) ||
1934 (size
>= runtime
->xfer_align
&& avail
< runtime
->xfer_align
)) {
1936 enum { READY
, SIGNALED
, ERROR
, SUSPENDED
, EXPIRED
, DROPPED
} state
;
1944 init_waitqueue_entry(&wait
, current
);
1945 add_wait_queue(&runtime
->sleep
, &wait
);
1947 if (signal_pending(current
)) {
1951 set_current_state(TASK_INTERRUPTIBLE
);
1952 snd_pcm_stream_unlock_irq(substream
);
1953 tout
= schedule_timeout(10 * HZ
);
1954 snd_pcm_stream_lock_irq(substream
);
1956 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
&&
1957 runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
) {
1958 state
= runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
? SUSPENDED
: EXPIRED
;
1962 switch (runtime
->status
->state
) {
1963 case SNDRV_PCM_STATE_XRUN
:
1966 case SNDRV_PCM_STATE_SUSPENDED
:
1969 case SNDRV_PCM_STATE_DRAINING
:
1971 case SNDRV_PCM_STATE_SETUP
:
1977 avail
= snd_pcm_capture_avail(runtime
);
1978 if (avail
>= runtime
->control
->avail_min
) {
1984 remove_wait_queue(&runtime
->sleep
, &wait
);
1997 snd_printd("capture read error (DMA or IRQ trouble?)\n");
2007 if (avail
> runtime
->xfer_align
)
2008 avail
-= avail
% runtime
->xfer_align
;
2009 frames
= size
> avail
? avail
: size
;
2010 cont
= runtime
->buffer_size
- runtime
->control
->appl_ptr
% runtime
->buffer_size
;
2013 snd_assert(frames
!= 0, snd_pcm_stream_unlock_irq(substream
); return -EINVAL
);
2014 appl_ptr
= runtime
->control
->appl_ptr
;
2015 appl_ofs
= appl_ptr
% runtime
->buffer_size
;
2016 snd_pcm_stream_unlock_irq(substream
);
2017 if ((err
= transfer(substream
, appl_ofs
, data
, offset
, frames
)) < 0)
2019 snd_pcm_stream_lock_irq(substream
);
2020 switch (runtime
->status
->state
) {
2021 case SNDRV_PCM_STATE_XRUN
:
2024 case SNDRV_PCM_STATE_SUSPENDED
:
2031 if (appl_ptr
>= runtime
->boundary
)
2032 appl_ptr
-= runtime
->boundary
;
2033 runtime
->control
->appl_ptr
= appl_ptr
;
2034 if (substream
->ops
->ack
)
2035 substream
->ops
->ack(substream
);
2040 if (runtime
->sleep_min
&&
2041 runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
2042 snd_pcm_tick_prepare(substream
);
2045 snd_pcm_stream_unlock_irq(substream
);
2047 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: err
;
2050 snd_pcm_sframes_t
snd_pcm_lib_read(struct snd_pcm_substream
*substream
, void __user
*buf
, snd_pcm_uframes_t size
)
2052 struct snd_pcm_runtime
*runtime
;
2055 snd_assert(substream
!= NULL
, return -ENXIO
);
2056 runtime
= substream
->runtime
;
2057 snd_assert(runtime
!= NULL
, return -ENXIO
);
2058 snd_assert(substream
->ops
->copy
!= NULL
|| runtime
->dma_area
!= NULL
, return -EINVAL
);
2059 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2062 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
2063 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_INTERLEAVED
)
2065 return snd_pcm_lib_read1(substream
, (unsigned long)buf
, size
, nonblock
, snd_pcm_lib_read_transfer
);
2068 EXPORT_SYMBOL(snd_pcm_lib_read
);
2070 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream
*substream
,
2072 unsigned long data
, unsigned int off
,
2073 snd_pcm_uframes_t frames
)
2075 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2077 void __user
**bufs
= (void __user
**)data
;
2078 int channels
= runtime
->channels
;
2080 if (substream
->ops
->copy
) {
2081 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
2085 buf
= *bufs
+ samples_to_bytes(runtime
, off
);
2086 if ((err
= substream
->ops
->copy(substream
, c
, hwoff
, buf
, frames
)) < 0)
2090 snd_pcm_uframes_t dma_csize
= runtime
->dma_bytes
/ channels
;
2091 snd_assert(runtime
->dma_area
, return -EFAULT
);
2092 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
2098 hwbuf
= runtime
->dma_area
+ (c
* dma_csize
) + samples_to_bytes(runtime
, hwoff
);
2099 buf
= *bufs
+ samples_to_bytes(runtime
, off
);
2100 if (copy_to_user(buf
, hwbuf
, samples_to_bytes(runtime
, frames
)))
2107 snd_pcm_sframes_t
snd_pcm_lib_readv(struct snd_pcm_substream
*substream
,
2109 snd_pcm_uframes_t frames
)
2111 struct snd_pcm_runtime
*runtime
;
2114 snd_assert(substream
!= NULL
, return -ENXIO
);
2115 runtime
= substream
->runtime
;
2116 snd_assert(runtime
!= NULL
, return -ENXIO
);
2117 snd_assert(substream
->ops
->copy
!= NULL
|| runtime
->dma_area
!= NULL
, return -EINVAL
);
2118 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2121 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
2122 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
2124 return snd_pcm_lib_read1(substream
, (unsigned long)bufs
, frames
, nonblock
, snd_pcm_lib_readv_transfer
);
2127 EXPORT_SYMBOL(snd_pcm_lib_readv
);