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 /* To use the same code we have in alsa-lib */
1167 #define assert(i) snd_assert((i), return -EINVAL)
1169 #define INT_MIN ((int)((unsigned int)INT_MAX+1))
1172 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params
*params
,
1173 snd_pcm_hw_param_t var
)
1175 if (hw_is_mask(var
)) {
1176 snd_mask_any(hw_param_mask(params
, var
));
1177 params
->cmask
|= 1 << var
;
1178 params
->rmask
|= 1 << var
;
1181 if (hw_is_interval(var
)) {
1182 snd_interval_any(hw_param_interval(params
, var
));
1183 params
->cmask
|= 1 << var
;
1184 params
->rmask
|= 1 << var
;
1190 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params
*params
)
1193 memset(params
, 0, sizeof(*params
));
1194 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++)
1195 _snd_pcm_hw_param_any(params
, k
);
1196 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
1197 _snd_pcm_hw_param_any(params
, k
);
1201 EXPORT_SYMBOL(_snd_pcm_hw_params_any
);
1204 * snd_pcm_hw_param_value
1205 * @params: the hw_params instance
1206 * @var: parameter to retrieve
1207 * @dir: pointer to the direction (-1,0,1) or NULL
1209 * Return the value for field PAR if it's fixed in configuration space
1210 * defined by PARAMS. Return -EINVAL otherwise
1212 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params
*params
,
1213 snd_pcm_hw_param_t var
, int *dir
)
1215 if (hw_is_mask(var
)) {
1216 const struct snd_mask
*mask
= hw_param_mask_c(params
, var
);
1217 if (!snd_mask_single(mask
))
1221 return snd_mask_value(mask
);
1223 if (hw_is_interval(var
)) {
1224 const struct snd_interval
*i
= hw_param_interval_c(params
, var
);
1225 if (!snd_interval_single(i
))
1229 return snd_interval_value(i
);
1235 EXPORT_SYMBOL(snd_pcm_hw_param_value
);
1237 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params
*params
,
1238 snd_pcm_hw_param_t var
)
1240 if (hw_is_mask(var
)) {
1241 snd_mask_none(hw_param_mask(params
, var
));
1242 params
->cmask
|= 1 << var
;
1243 params
->rmask
|= 1 << var
;
1244 } else if (hw_is_interval(var
)) {
1245 snd_interval_none(hw_param_interval(params
, var
));
1246 params
->cmask
|= 1 << var
;
1247 params
->rmask
|= 1 << var
;
1253 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty
);
1255 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params
*params
,
1256 snd_pcm_hw_param_t var
)
1259 if (hw_is_mask(var
))
1260 changed
= snd_mask_refine_first(hw_param_mask(params
, var
));
1261 else if (hw_is_interval(var
))
1262 changed
= snd_interval_refine_first(hw_param_interval(params
, var
));
1268 params
->cmask
|= 1 << var
;
1269 params
->rmask
|= 1 << var
;
1276 * snd_pcm_hw_param_first
1277 * @pcm: PCM instance
1278 * @params: the hw_params instance
1279 * @var: parameter to retrieve
1280 * @dir: pointer to the direction (-1,0,1) or NULL
1282 * Inside configuration space defined by PARAMS remove from PAR all
1283 * values > minimum. Reduce configuration space accordingly.
1284 * Return the minimum.
1286 int snd_pcm_hw_param_first(struct snd_pcm_substream
*pcm
,
1287 struct snd_pcm_hw_params
*params
,
1288 snd_pcm_hw_param_t var
, int *dir
)
1290 int changed
= _snd_pcm_hw_param_first(params
, var
);
1293 if (params
->rmask
) {
1294 int err
= snd_pcm_hw_refine(pcm
, params
);
1297 return snd_pcm_hw_param_value(params
, var
, dir
);
1300 EXPORT_SYMBOL(snd_pcm_hw_param_first
);
1302 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params
*params
,
1303 snd_pcm_hw_param_t var
)
1306 if (hw_is_mask(var
))
1307 changed
= snd_mask_refine_last(hw_param_mask(params
, var
));
1308 else if (hw_is_interval(var
))
1309 changed
= snd_interval_refine_last(hw_param_interval(params
, var
));
1315 params
->cmask
|= 1 << var
;
1316 params
->rmask
|= 1 << var
;
1323 * snd_pcm_hw_param_last
1324 * @pcm: PCM instance
1325 * @params: the hw_params instance
1326 * @var: parameter to retrieve
1327 * @dir: pointer to the direction (-1,0,1) or NULL
1329 * Inside configuration space defined by PARAMS remove from PAR all
1330 * values < maximum. Reduce configuration space accordingly.
1331 * Return the maximum.
1333 int snd_pcm_hw_param_last(struct snd_pcm_substream
*pcm
,
1334 struct snd_pcm_hw_params
*params
,
1335 snd_pcm_hw_param_t var
, int *dir
)
1337 int changed
= _snd_pcm_hw_param_last(params
, var
);
1340 if (params
->rmask
) {
1341 int err
= snd_pcm_hw_refine(pcm
, params
);
1344 return snd_pcm_hw_param_value(params
, var
, dir
);
1347 EXPORT_SYMBOL(snd_pcm_hw_param_last
);
1350 * snd_pcm_hw_param_choose
1351 * @pcm: PCM instance
1352 * @params: the hw_params instance
1354 * Choose one configuration from configuration space defined by PARAMS
1355 * The configuration chosen is that obtained fixing in this order:
1356 * first access, first format, first subformat, min channels,
1357 * min rate, min period time, max buffer size, min tick time
1359 int snd_pcm_hw_params_choose(struct snd_pcm_substream
*pcm
, struct snd_pcm_hw_params
*params
)
1363 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_ACCESS
, NULL
);
1366 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_FORMAT
, NULL
);
1369 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, NULL
);
1372 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_CHANNELS
, NULL
);
1375 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_RATE
, NULL
);
1378 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, NULL
);
1381 err
= snd_pcm_hw_param_last(pcm
, params
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, NULL
);
1384 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_TICK_TIME
, NULL
);
1392 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream
*substream
,
1395 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1396 unsigned long flags
;
1397 snd_pcm_stream_lock_irqsave(substream
, flags
);
1398 if (snd_pcm_running(substream
) &&
1399 snd_pcm_update_hw_ptr(substream
) >= 0)
1400 runtime
->status
->hw_ptr
%= runtime
->buffer_size
;
1402 runtime
->status
->hw_ptr
= 0;
1403 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1407 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream
*substream
,
1410 struct snd_pcm_channel_info
*info
= arg
;
1411 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1413 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
)) {
1417 width
= snd_pcm_format_physical_width(runtime
->format
);
1421 switch (runtime
->access
) {
1422 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
:
1423 case SNDRV_PCM_ACCESS_RW_INTERLEAVED
:
1424 info
->first
= info
->channel
* width
;
1425 info
->step
= runtime
->channels
* width
;
1427 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
:
1428 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
:
1430 size_t size
= runtime
->dma_bytes
/ runtime
->channels
;
1431 info
->first
= info
->channel
* size
* 8;
1443 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1444 * @substream: the pcm substream instance
1445 * @cmd: ioctl command
1446 * @arg: ioctl argument
1448 * Processes the generic ioctl commands for PCM.
1449 * Can be passed as the ioctl callback for PCM ops.
1451 * Returns zero if successful, or a negative error code on failure.
1453 int snd_pcm_lib_ioctl(struct snd_pcm_substream
*substream
,
1454 unsigned int cmd
, void *arg
)
1457 case SNDRV_PCM_IOCTL1_INFO
:
1459 case SNDRV_PCM_IOCTL1_RESET
:
1460 return snd_pcm_lib_ioctl_reset(substream
, arg
);
1461 case SNDRV_PCM_IOCTL1_CHANNEL_INFO
:
1462 return snd_pcm_lib_ioctl_channel_info(substream
, arg
);
1467 EXPORT_SYMBOL(snd_pcm_lib_ioctl
);
1473 static void snd_pcm_system_tick_set(struct snd_pcm_substream
*substream
,
1474 unsigned long ticks
)
1476 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1478 del_timer(&runtime
->tick_timer
);
1480 ticks
+= (1000000 / HZ
) - 1;
1481 ticks
/= (1000000 / HZ
);
1482 mod_timer(&runtime
->tick_timer
, jiffies
+ ticks
);
1486 /* Temporary alias */
1487 void snd_pcm_tick_set(struct snd_pcm_substream
*substream
, unsigned long ticks
)
1489 snd_pcm_system_tick_set(substream
, ticks
);
1492 void snd_pcm_tick_prepare(struct snd_pcm_substream
*substream
)
1494 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1495 snd_pcm_uframes_t frames
= ULONG_MAX
;
1496 snd_pcm_uframes_t avail
, dist
;
1500 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1501 if (runtime
->silence_size
>= runtime
->boundary
) {
1503 } else if (runtime
->silence_size
> 0 &&
1504 runtime
->silence_filled
< runtime
->buffer_size
) {
1505 snd_pcm_sframes_t noise_dist
;
1506 noise_dist
= snd_pcm_playback_hw_avail(runtime
) + runtime
->silence_filled
;
1507 if (noise_dist
> (snd_pcm_sframes_t
)runtime
->silence_threshold
)
1508 frames
= noise_dist
- runtime
->silence_threshold
;
1510 avail
= snd_pcm_playback_avail(runtime
);
1512 avail
= snd_pcm_capture_avail(runtime
);
1514 if (avail
< runtime
->control
->avail_min
) {
1515 snd_pcm_sframes_t n
= runtime
->control
->avail_min
- avail
;
1516 if (n
> 0 && frames
> (snd_pcm_uframes_t
)n
)
1519 if (avail
< runtime
->buffer_size
) {
1520 snd_pcm_sframes_t n
= runtime
->buffer_size
- avail
;
1521 if (n
> 0 && frames
> (snd_pcm_uframes_t
)n
)
1524 if (frames
== ULONG_MAX
) {
1525 snd_pcm_tick_set(substream
, 0);
1528 dist
= runtime
->status
->hw_ptr
- runtime
->hw_ptr_base
;
1529 /* Distance to next interrupt */
1530 dist
= runtime
->period_size
- dist
% runtime
->period_size
;
1531 if (dist
<= frames
) {
1532 snd_pcm_tick_set(substream
, 0);
1535 /* the base time is us */
1538 div64_32(&n
, runtime
->tick_time
* runtime
->rate
, &r
);
1539 ticks
= n
+ (r
> 0 ? 1 : 0);
1540 if (ticks
< runtime
->sleep_min
)
1541 ticks
= runtime
->sleep_min
;
1542 snd_pcm_tick_set(substream
, (unsigned long) ticks
);
1545 void snd_pcm_tick_elapsed(struct snd_pcm_substream
*substream
)
1547 struct snd_pcm_runtime
*runtime
;
1548 unsigned long flags
;
1550 snd_assert(substream
!= NULL
, return);
1551 runtime
= substream
->runtime
;
1552 snd_assert(runtime
!= NULL
, return);
1554 snd_pcm_stream_lock_irqsave(substream
, flags
);
1555 if (!snd_pcm_running(substream
) ||
1556 snd_pcm_update_hw_ptr(substream
) < 0)
1558 if (runtime
->sleep_min
)
1559 snd_pcm_tick_prepare(substream
);
1561 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1565 * snd_pcm_period_elapsed - update the pcm status for the next period
1566 * @substream: the pcm substream instance
1568 * This function is called from the interrupt handler when the
1569 * PCM has processed the period size. It will update the current
1570 * pointer, set up the tick, wake up sleepers, etc.
1572 * Even if more than one periods have elapsed since the last call, you
1573 * have to call this only once.
1575 void snd_pcm_period_elapsed(struct snd_pcm_substream
*substream
)
1577 struct snd_pcm_runtime
*runtime
;
1578 unsigned long flags
;
1580 snd_assert(substream
!= NULL
, return);
1581 runtime
= substream
->runtime
;
1582 snd_assert(runtime
!= NULL
, return);
1584 if (runtime
->transfer_ack_begin
)
1585 runtime
->transfer_ack_begin(substream
);
1587 snd_pcm_stream_lock_irqsave(substream
, flags
);
1588 if (!snd_pcm_running(substream
) ||
1589 snd_pcm_update_hw_ptr_interrupt(substream
) < 0)
1592 if (substream
->timer_running
)
1593 snd_timer_interrupt(substream
->timer
, 1);
1594 if (runtime
->sleep_min
)
1595 snd_pcm_tick_prepare(substream
);
1597 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1598 if (runtime
->transfer_ack_end
)
1599 runtime
->transfer_ack_end(substream
);
1600 kill_fasync(&runtime
->fasync
, SIGIO
, POLL_IN
);
1603 EXPORT_SYMBOL(snd_pcm_period_elapsed
);
1605 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream
*substream
,
1607 unsigned long data
, unsigned int off
,
1608 snd_pcm_uframes_t frames
)
1610 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1612 char __user
*buf
= (char __user
*) data
+ frames_to_bytes(runtime
, off
);
1613 if (substream
->ops
->copy
) {
1614 if ((err
= substream
->ops
->copy(substream
, -1, hwoff
, buf
, frames
)) < 0)
1617 char *hwbuf
= runtime
->dma_area
+ frames_to_bytes(runtime
, hwoff
);
1618 snd_assert(runtime
->dma_area
, return -EFAULT
);
1619 if (copy_from_user(hwbuf
, buf
, frames_to_bytes(runtime
, frames
)))
1625 typedef int (*transfer_f
)(struct snd_pcm_substream
*substream
, unsigned int hwoff
,
1626 unsigned long data
, unsigned int off
,
1627 snd_pcm_uframes_t size
);
1629 static snd_pcm_sframes_t
snd_pcm_lib_write1(struct snd_pcm_substream
*substream
,
1631 snd_pcm_uframes_t size
,
1633 transfer_f transfer
)
1635 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1636 snd_pcm_uframes_t xfer
= 0;
1637 snd_pcm_uframes_t offset
= 0;
1642 if (size
> runtime
->xfer_align
)
1643 size
-= size
% runtime
->xfer_align
;
1645 snd_pcm_stream_lock_irq(substream
);
1646 switch (runtime
->status
->state
) {
1647 case SNDRV_PCM_STATE_PREPARED
:
1648 case SNDRV_PCM_STATE_RUNNING
:
1649 case SNDRV_PCM_STATE_PAUSED
:
1651 case SNDRV_PCM_STATE_XRUN
:
1654 case SNDRV_PCM_STATE_SUSPENDED
:
1663 snd_pcm_uframes_t frames
, appl_ptr
, appl_ofs
;
1664 snd_pcm_uframes_t avail
;
1665 snd_pcm_uframes_t cont
;
1666 if (runtime
->sleep_min
== 0 && runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
1667 snd_pcm_update_hw_ptr(substream
);
1668 avail
= snd_pcm_playback_avail(runtime
);
1669 if (((avail
< runtime
->control
->avail_min
&& size
> avail
) ||
1670 (size
>= runtime
->xfer_align
&& avail
< runtime
->xfer_align
))) {
1672 enum { READY
, SIGNALED
, ERROR
, SUSPENDED
, EXPIRED
, DROPPED
} state
;
1680 init_waitqueue_entry(&wait
, current
);
1681 add_wait_queue(&runtime
->sleep
, &wait
);
1683 if (signal_pending(current
)) {
1687 set_current_state(TASK_INTERRUPTIBLE
);
1688 snd_pcm_stream_unlock_irq(substream
);
1689 tout
= schedule_timeout(10 * HZ
);
1690 snd_pcm_stream_lock_irq(substream
);
1692 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
&&
1693 runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
) {
1694 state
= runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
? SUSPENDED
: EXPIRED
;
1698 switch (runtime
->status
->state
) {
1699 case SNDRV_PCM_STATE_XRUN
:
1700 case SNDRV_PCM_STATE_DRAINING
:
1703 case SNDRV_PCM_STATE_SUSPENDED
:
1706 case SNDRV_PCM_STATE_SETUP
:
1712 avail
= snd_pcm_playback_avail(runtime
);
1713 if (avail
>= runtime
->control
->avail_min
) {
1719 remove_wait_queue(&runtime
->sleep
, &wait
);
1732 snd_printd("playback write error (DMA or IRQ trouble?)\n");
1742 if (avail
> runtime
->xfer_align
)
1743 avail
-= avail
% runtime
->xfer_align
;
1744 frames
= size
> avail
? avail
: size
;
1745 cont
= runtime
->buffer_size
- runtime
->control
->appl_ptr
% runtime
->buffer_size
;
1748 snd_assert(frames
!= 0, snd_pcm_stream_unlock_irq(substream
); return -EINVAL
);
1749 appl_ptr
= runtime
->control
->appl_ptr
;
1750 appl_ofs
= appl_ptr
% runtime
->buffer_size
;
1751 snd_pcm_stream_unlock_irq(substream
);
1752 if ((err
= transfer(substream
, appl_ofs
, data
, offset
, frames
)) < 0)
1754 snd_pcm_stream_lock_irq(substream
);
1755 switch (runtime
->status
->state
) {
1756 case SNDRV_PCM_STATE_XRUN
:
1759 case SNDRV_PCM_STATE_SUSPENDED
:
1766 if (appl_ptr
>= runtime
->boundary
)
1767 appl_ptr
-= runtime
->boundary
;
1768 runtime
->control
->appl_ptr
= appl_ptr
;
1769 if (substream
->ops
->ack
)
1770 substream
->ops
->ack(substream
);
1775 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
&&
1776 snd_pcm_playback_hw_avail(runtime
) >= (snd_pcm_sframes_t
)runtime
->start_threshold
) {
1777 err
= snd_pcm_start(substream
);
1781 if (runtime
->sleep_min
&&
1782 runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
1783 snd_pcm_tick_prepare(substream
);
1786 snd_pcm_stream_unlock_irq(substream
);
1788 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: err
;
1791 snd_pcm_sframes_t
snd_pcm_lib_write(struct snd_pcm_substream
*substream
, const void __user
*buf
, snd_pcm_uframes_t size
)
1793 struct snd_pcm_runtime
*runtime
;
1796 snd_assert(substream
!= NULL
, return -ENXIO
);
1797 runtime
= substream
->runtime
;
1798 snd_assert(runtime
!= NULL
, return -ENXIO
);
1799 snd_assert(substream
->ops
->copy
!= NULL
|| runtime
->dma_area
!= NULL
, return -EINVAL
);
1800 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1803 nonblock
= !!(substream
->ffile
->f_flags
& O_NONBLOCK
);
1805 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_INTERLEAVED
&&
1806 runtime
->channels
> 1)
1808 return snd_pcm_lib_write1(substream
, (unsigned long)buf
, size
, nonblock
,
1809 snd_pcm_lib_write_transfer
);
1812 EXPORT_SYMBOL(snd_pcm_lib_write
);
1814 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream
*substream
,
1816 unsigned long data
, unsigned int off
,
1817 snd_pcm_uframes_t frames
)
1819 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1821 void __user
**bufs
= (void __user
**)data
;
1822 int channels
= runtime
->channels
;
1824 if (substream
->ops
->copy
) {
1825 snd_assert(substream
->ops
->silence
!= NULL
, return -EINVAL
);
1826 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
1827 if (*bufs
== NULL
) {
1828 if ((err
= substream
->ops
->silence(substream
, c
, hwoff
, frames
)) < 0)
1831 char __user
*buf
= *bufs
+ samples_to_bytes(runtime
, off
);
1832 if ((err
= substream
->ops
->copy(substream
, c
, hwoff
, buf
, frames
)) < 0)
1837 /* default transfer behaviour */
1838 size_t dma_csize
= runtime
->dma_bytes
/ channels
;
1839 snd_assert(runtime
->dma_area
, return -EFAULT
);
1840 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
1841 char *hwbuf
= runtime
->dma_area
+ (c
* dma_csize
) + samples_to_bytes(runtime
, hwoff
);
1842 if (*bufs
== NULL
) {
1843 snd_pcm_format_set_silence(runtime
->format
, hwbuf
, frames
);
1845 char __user
*buf
= *bufs
+ samples_to_bytes(runtime
, off
);
1846 if (copy_from_user(hwbuf
, buf
, samples_to_bytes(runtime
, frames
)))
1854 snd_pcm_sframes_t
snd_pcm_lib_writev(struct snd_pcm_substream
*substream
,
1856 snd_pcm_uframes_t frames
)
1858 struct snd_pcm_runtime
*runtime
;
1861 snd_assert(substream
!= NULL
, return -ENXIO
);
1862 runtime
= substream
->runtime
;
1863 snd_assert(runtime
!= NULL
, return -ENXIO
);
1864 snd_assert(substream
->ops
->copy
!= NULL
|| runtime
->dma_area
!= NULL
, return -EINVAL
);
1865 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1868 nonblock
= !!(substream
->ffile
->f_flags
& O_NONBLOCK
);
1870 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
1872 return snd_pcm_lib_write1(substream
, (unsigned long)bufs
, frames
,
1873 nonblock
, snd_pcm_lib_writev_transfer
);
1876 EXPORT_SYMBOL(snd_pcm_lib_writev
);
1878 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream
*substream
,
1880 unsigned long data
, unsigned int off
,
1881 snd_pcm_uframes_t frames
)
1883 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1885 char __user
*buf
= (char __user
*) data
+ frames_to_bytes(runtime
, off
);
1886 if (substream
->ops
->copy
) {
1887 if ((err
= substream
->ops
->copy(substream
, -1, hwoff
, buf
, frames
)) < 0)
1890 char *hwbuf
= runtime
->dma_area
+ frames_to_bytes(runtime
, hwoff
);
1891 snd_assert(runtime
->dma_area
, return -EFAULT
);
1892 if (copy_to_user(buf
, hwbuf
, frames_to_bytes(runtime
, frames
)))
1898 static snd_pcm_sframes_t
snd_pcm_lib_read1(struct snd_pcm_substream
*substream
,
1900 snd_pcm_uframes_t size
,
1902 transfer_f transfer
)
1904 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1905 snd_pcm_uframes_t xfer
= 0;
1906 snd_pcm_uframes_t offset
= 0;
1911 if (size
> runtime
->xfer_align
)
1912 size
-= size
% runtime
->xfer_align
;
1914 snd_pcm_stream_lock_irq(substream
);
1915 switch (runtime
->status
->state
) {
1916 case SNDRV_PCM_STATE_PREPARED
:
1917 if (size
>= runtime
->start_threshold
) {
1918 err
= snd_pcm_start(substream
);
1923 case SNDRV_PCM_STATE_DRAINING
:
1924 case SNDRV_PCM_STATE_RUNNING
:
1925 case SNDRV_PCM_STATE_PAUSED
:
1927 case SNDRV_PCM_STATE_XRUN
:
1930 case SNDRV_PCM_STATE_SUSPENDED
:
1939 snd_pcm_uframes_t frames
, appl_ptr
, appl_ofs
;
1940 snd_pcm_uframes_t avail
;
1941 snd_pcm_uframes_t cont
;
1942 if (runtime
->sleep_min
== 0 && runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
1943 snd_pcm_update_hw_ptr(substream
);
1945 avail
= snd_pcm_capture_avail(runtime
);
1946 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
1947 if (avail
< runtime
->xfer_align
) {
1951 } else if ((avail
< runtime
->control
->avail_min
&& size
> avail
) ||
1952 (size
>= runtime
->xfer_align
&& avail
< runtime
->xfer_align
)) {
1954 enum { READY
, SIGNALED
, ERROR
, SUSPENDED
, EXPIRED
, DROPPED
} state
;
1962 init_waitqueue_entry(&wait
, current
);
1963 add_wait_queue(&runtime
->sleep
, &wait
);
1965 if (signal_pending(current
)) {
1969 set_current_state(TASK_INTERRUPTIBLE
);
1970 snd_pcm_stream_unlock_irq(substream
);
1971 tout
= schedule_timeout(10 * HZ
);
1972 snd_pcm_stream_lock_irq(substream
);
1974 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
&&
1975 runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
) {
1976 state
= runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
? SUSPENDED
: EXPIRED
;
1980 switch (runtime
->status
->state
) {
1981 case SNDRV_PCM_STATE_XRUN
:
1984 case SNDRV_PCM_STATE_SUSPENDED
:
1987 case SNDRV_PCM_STATE_DRAINING
:
1989 case SNDRV_PCM_STATE_SETUP
:
1995 avail
= snd_pcm_capture_avail(runtime
);
1996 if (avail
>= runtime
->control
->avail_min
) {
2002 remove_wait_queue(&runtime
->sleep
, &wait
);
2015 snd_printd("capture read error (DMA or IRQ trouble?)\n");
2025 if (avail
> runtime
->xfer_align
)
2026 avail
-= avail
% runtime
->xfer_align
;
2027 frames
= size
> avail
? avail
: size
;
2028 cont
= runtime
->buffer_size
- runtime
->control
->appl_ptr
% runtime
->buffer_size
;
2031 snd_assert(frames
!= 0, snd_pcm_stream_unlock_irq(substream
); return -EINVAL
);
2032 appl_ptr
= runtime
->control
->appl_ptr
;
2033 appl_ofs
= appl_ptr
% runtime
->buffer_size
;
2034 snd_pcm_stream_unlock_irq(substream
);
2035 if ((err
= transfer(substream
, appl_ofs
, data
, offset
, frames
)) < 0)
2037 snd_pcm_stream_lock_irq(substream
);
2038 switch (runtime
->status
->state
) {
2039 case SNDRV_PCM_STATE_XRUN
:
2042 case SNDRV_PCM_STATE_SUSPENDED
:
2049 if (appl_ptr
>= runtime
->boundary
)
2050 appl_ptr
-= runtime
->boundary
;
2051 runtime
->control
->appl_ptr
= appl_ptr
;
2052 if (substream
->ops
->ack
)
2053 substream
->ops
->ack(substream
);
2058 if (runtime
->sleep_min
&&
2059 runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
2060 snd_pcm_tick_prepare(substream
);
2063 snd_pcm_stream_unlock_irq(substream
);
2065 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: err
;
2068 snd_pcm_sframes_t
snd_pcm_lib_read(struct snd_pcm_substream
*substream
, void __user
*buf
, snd_pcm_uframes_t size
)
2070 struct snd_pcm_runtime
*runtime
;
2073 snd_assert(substream
!= NULL
, return -ENXIO
);
2074 runtime
= substream
->runtime
;
2075 snd_assert(runtime
!= NULL
, return -ENXIO
);
2076 snd_assert(substream
->ops
->copy
!= NULL
|| runtime
->dma_area
!= NULL
, return -EINVAL
);
2077 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2080 nonblock
= !!(substream
->ffile
->f_flags
& O_NONBLOCK
);
2081 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_INTERLEAVED
)
2083 return snd_pcm_lib_read1(substream
, (unsigned long)buf
, size
, nonblock
, snd_pcm_lib_read_transfer
);
2086 EXPORT_SYMBOL(snd_pcm_lib_read
);
2088 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream
*substream
,
2090 unsigned long data
, unsigned int off
,
2091 snd_pcm_uframes_t frames
)
2093 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2095 void __user
**bufs
= (void __user
**)data
;
2096 int channels
= runtime
->channels
;
2098 if (substream
->ops
->copy
) {
2099 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
2103 buf
= *bufs
+ samples_to_bytes(runtime
, off
);
2104 if ((err
= substream
->ops
->copy(substream
, c
, hwoff
, buf
, frames
)) < 0)
2108 snd_pcm_uframes_t dma_csize
= runtime
->dma_bytes
/ channels
;
2109 snd_assert(runtime
->dma_area
, return -EFAULT
);
2110 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
2116 hwbuf
= runtime
->dma_area
+ (c
* dma_csize
) + samples_to_bytes(runtime
, hwoff
);
2117 buf
= *bufs
+ samples_to_bytes(runtime
, off
);
2118 if (copy_to_user(buf
, hwbuf
, samples_to_bytes(runtime
, frames
)))
2125 snd_pcm_sframes_t
snd_pcm_lib_readv(struct snd_pcm_substream
*substream
,
2127 snd_pcm_uframes_t frames
)
2129 struct snd_pcm_runtime
*runtime
;
2132 snd_assert(substream
!= NULL
, return -ENXIO
);
2133 runtime
= substream
->runtime
;
2134 snd_assert(runtime
!= NULL
, return -ENXIO
);
2135 snd_assert(substream
->ops
->copy
!= NULL
|| runtime
->dma_area
!= NULL
, return -EINVAL
);
2136 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2139 nonblock
= !!(substream
->ffile
->f_flags
& O_NONBLOCK
);
2140 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
2142 return snd_pcm_lib_read1(substream
, (unsigned long)bufs
, frames
, nonblock
, snd_pcm_lib_readv_transfer
);
2145 EXPORT_SYMBOL(snd_pcm_lib_readv
);