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_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_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_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
;
294 * snd_pcm_sync - set the PCM sync id
295 * @substream: the pcm substream
297 * Sets the PCM sync identifier for the card.
299 void snd_pcm_set_sync(struct snd_pcm_substream
*substream
)
301 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
303 runtime
->sync
.id32
[0] = substream
->pcm
->card
->number
;
304 runtime
->sync
.id32
[1] = -1;
305 runtime
->sync
.id32
[2] = -1;
306 runtime
->sync
.id32
[3] = -1;
310 * Standard ioctl routine
313 /* Code taken from alsa-lib */
314 #define assert(a) snd_assert((a), return -EINVAL)
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
;
372 static int snd_interval_refine_min(struct snd_interval
*i
, unsigned int min
, int openmin
)
375 assert(!snd_interval_empty(i
));
378 i
->openmin
= openmin
;
380 } else if (i
->min
== min
&& !i
->openmin
&& openmin
) {
390 if (snd_interval_checkempty(i
)) {
391 snd_interval_none(i
);
397 static int snd_interval_refine_max(struct snd_interval
*i
, unsigned int max
, int openmax
)
400 assert(!snd_interval_empty(i
));
403 i
->openmax
= openmax
;
405 } else if (i
->max
== max
&& !i
->openmax
&& openmax
) {
415 if (snd_interval_checkempty(i
)) {
416 snd_interval_none(i
);
423 * snd_interval_refine - refine the interval value of configurator
424 * @i: the interval value to refine
425 * @v: the interval value to refer to
427 * Refines the interval value with the reference value.
428 * The interval is changed to the range satisfying both intervals.
429 * The interval status (min, max, integer, etc.) are evaluated.
431 * Returns non-zero if the value is changed, zero if not changed.
433 int snd_interval_refine(struct snd_interval
*i
, const struct snd_interval
*v
)
436 assert(!snd_interval_empty(i
));
437 if (i
->min
< v
->min
) {
439 i
->openmin
= v
->openmin
;
441 } else if (i
->min
== v
->min
&& !i
->openmin
&& v
->openmin
) {
445 if (i
->max
> v
->max
) {
447 i
->openmax
= v
->openmax
;
449 } else if (i
->max
== v
->max
&& !i
->openmax
&& v
->openmax
) {
453 if (!i
->integer
&& v
->integer
) {
466 } else if (!i
->openmin
&& !i
->openmax
&& i
->min
== i
->max
)
468 if (snd_interval_checkempty(i
)) {
469 snd_interval_none(i
);
475 static int snd_interval_refine_first(struct snd_interval
*i
)
477 assert(!snd_interval_empty(i
));
478 if (snd_interval_single(i
))
481 i
->openmax
= i
->openmin
;
487 static int snd_interval_refine_last(struct snd_interval
*i
)
489 assert(!snd_interval_empty(i
));
490 if (snd_interval_single(i
))
493 i
->openmin
= i
->openmax
;
499 static int snd_interval_refine_set(struct snd_interval
*i
, unsigned int val
)
501 struct snd_interval t
;
504 t
.openmin
= t
.openmax
= 0;
506 return snd_interval_refine(i
, &t
);
509 void snd_interval_mul(const struct snd_interval
*a
, const struct snd_interval
*b
, struct snd_interval
*c
)
511 if (a
->empty
|| b
->empty
) {
512 snd_interval_none(c
);
516 c
->min
= mul(a
->min
, b
->min
);
517 c
->openmin
= (a
->openmin
|| b
->openmin
);
518 c
->max
= mul(a
->max
, b
->max
);
519 c
->openmax
= (a
->openmax
|| b
->openmax
);
520 c
->integer
= (a
->integer
&& b
->integer
);
524 * snd_interval_div - refine the interval value with division
531 * Returns non-zero if the value is changed, zero if not changed.
533 void snd_interval_div(const struct snd_interval
*a
, const struct snd_interval
*b
, struct snd_interval
*c
)
536 if (a
->empty
|| b
->empty
) {
537 snd_interval_none(c
);
541 c
->min
= div32(a
->min
, b
->max
, &r
);
542 c
->openmin
= (r
|| a
->openmin
|| b
->openmax
);
544 c
->max
= div32(a
->max
, b
->min
, &r
);
549 c
->openmax
= (a
->openmax
|| b
->openmin
);
558 * snd_interval_muldivk - refine the interval value
561 * @k: divisor (as integer)
566 * Returns non-zero if the value is changed, zero if not changed.
568 void snd_interval_muldivk(const struct snd_interval
*a
, const struct snd_interval
*b
,
569 unsigned int k
, struct snd_interval
*c
)
572 if (a
->empty
|| b
->empty
) {
573 snd_interval_none(c
);
577 c
->min
= muldiv32(a
->min
, b
->min
, k
, &r
);
578 c
->openmin
= (r
|| a
->openmin
|| b
->openmin
);
579 c
->max
= muldiv32(a
->max
, b
->max
, k
, &r
);
584 c
->openmax
= (a
->openmax
|| b
->openmax
);
589 * snd_interval_mulkdiv - refine the interval value
591 * @k: dividend 2 (as integer)
597 * Returns non-zero if the value is changed, zero if not changed.
599 void snd_interval_mulkdiv(const struct snd_interval
*a
, unsigned int k
,
600 const struct snd_interval
*b
, struct snd_interval
*c
)
603 if (a
->empty
|| b
->empty
) {
604 snd_interval_none(c
);
608 c
->min
= muldiv32(a
->min
, k
, b
->max
, &r
);
609 c
->openmin
= (r
|| a
->openmin
|| b
->openmax
);
611 c
->max
= muldiv32(a
->max
, k
, b
->min
, &r
);
616 c
->openmax
= (a
->openmax
|| b
->openmin
);
629 * snd_interval_ratnum - refine the interval value
630 * @i: interval to refine
631 * @rats_count: number of ratnum_t
632 * @rats: ratnum_t array
633 * @nump: pointer to store the resultant numerator
634 * @denp: pointer to store the resultant denominator
636 * Returns non-zero if the value is changed, zero if not changed.
638 int snd_interval_ratnum(struct snd_interval
*i
,
639 unsigned int rats_count
, struct snd_ratnum
*rats
,
640 unsigned int *nump
, unsigned int *denp
)
642 unsigned int best_num
, best_diff
, best_den
;
644 struct snd_interval t
;
647 best_num
= best_den
= best_diff
= 0;
648 for (k
= 0; k
< rats_count
; ++k
) {
649 unsigned int num
= rats
[k
].num
;
651 unsigned int q
= i
->min
;
655 den
= div_down(num
, q
);
656 if (den
< rats
[k
].den_min
)
658 if (den
> rats
[k
].den_max
)
659 den
= rats
[k
].den_max
;
662 r
= (den
- rats
[k
].den_min
) % rats
[k
].den_step
;
666 diff
= num
- q
* den
;
668 diff
* best_den
< best_diff
* den
) {
678 t
.min
= div_down(best_num
, best_den
);
679 t
.openmin
= !!(best_num
% best_den
);
681 best_num
= best_den
= best_diff
= 0;
682 for (k
= 0; k
< rats_count
; ++k
) {
683 unsigned int num
= rats
[k
].num
;
685 unsigned int q
= i
->max
;
691 den
= div_up(num
, q
);
692 if (den
> rats
[k
].den_max
)
694 if (den
< rats
[k
].den_min
)
695 den
= rats
[k
].den_min
;
698 r
= (den
- rats
[k
].den_min
) % rats
[k
].den_step
;
700 den
+= rats
[k
].den_step
- r
;
702 diff
= q
* den
- num
;
704 diff
* best_den
< best_diff
* den
) {
714 t
.max
= div_up(best_num
, best_den
);
715 t
.openmax
= !!(best_num
% best_den
);
717 err
= snd_interval_refine(i
, &t
);
721 if (snd_interval_single(i
)) {
731 * snd_interval_ratden - refine the interval value
732 * @i: interval to refine
733 * @rats_count: number of struct ratden
734 * @rats: struct ratden array
735 * @nump: pointer to store the resultant numerator
736 * @denp: pointer to store the resultant denominator
738 * Returns non-zero if the value is changed, zero if not changed.
740 static int snd_interval_ratden(struct snd_interval
*i
,
741 unsigned int rats_count
, struct snd_ratden
*rats
,
742 unsigned int *nump
, unsigned int *denp
)
744 unsigned int best_num
, best_diff
, best_den
;
746 struct snd_interval t
;
749 best_num
= best_den
= best_diff
= 0;
750 for (k
= 0; k
< rats_count
; ++k
) {
752 unsigned int den
= rats
[k
].den
;
753 unsigned int q
= i
->min
;
756 if (num
> rats
[k
].num_max
)
758 if (num
< rats
[k
].num_min
)
759 num
= rats
[k
].num_max
;
762 r
= (num
- rats
[k
].num_min
) % rats
[k
].num_step
;
764 num
+= rats
[k
].num_step
- r
;
766 diff
= num
- q
* den
;
768 diff
* best_den
< best_diff
* den
) {
778 t
.min
= div_down(best_num
, best_den
);
779 t
.openmin
= !!(best_num
% best_den
);
781 best_num
= best_den
= best_diff
= 0;
782 for (k
= 0; k
< rats_count
; ++k
) {
784 unsigned int den
= rats
[k
].den
;
785 unsigned int q
= i
->max
;
788 if (num
< rats
[k
].num_min
)
790 if (num
> rats
[k
].num_max
)
791 num
= rats
[k
].num_max
;
794 r
= (num
- rats
[k
].num_min
) % rats
[k
].num_step
;
798 diff
= q
* den
- num
;
800 diff
* best_den
< best_diff
* den
) {
810 t
.max
= div_up(best_num
, best_den
);
811 t
.openmax
= !!(best_num
% best_den
);
813 err
= snd_interval_refine(i
, &t
);
817 if (snd_interval_single(i
)) {
827 * snd_interval_list - refine the interval value from the list
828 * @i: the interval value to refine
829 * @count: the number of elements in the list
830 * @list: the value list
831 * @mask: the bit-mask to evaluate
833 * Refines the interval value from the list.
834 * When mask is non-zero, only the elements corresponding to bit 1 are
837 * Returns non-zero if the value is changed, zero if not changed.
839 int snd_interval_list(struct snd_interval
*i
, unsigned int count
, unsigned int *list
, unsigned int mask
)
843 for (k
= 0; k
< count
; k
++) {
844 if (mask
&& !(mask
& (1 << k
)))
846 if (i
->min
== list
[k
] && !i
->openmin
)
848 if (i
->min
< list
[k
]) {
858 for (k
= count
; k
-- > 0;) {
859 if (mask
&& !(mask
& (1 << k
)))
861 if (i
->max
== list
[k
] && !i
->openmax
)
863 if (i
->max
> list
[k
]) {
873 if (snd_interval_checkempty(i
)) {
880 static int snd_interval_step(struct snd_interval
*i
, unsigned int min
, unsigned int step
)
884 n
= (i
->min
- min
) % step
;
885 if (n
!= 0 || i
->openmin
) {
889 n
= (i
->max
- min
) % step
;
890 if (n
!= 0 || i
->openmax
) {
894 if (snd_interval_checkempty(i
)) {
901 /* Info constraints helpers */
904 * snd_pcm_hw_rule_add - add the hw-constraint rule
905 * @runtime: the pcm runtime instance
906 * @cond: condition bits
907 * @var: the variable to evaluate
908 * @func: the evaluation function
909 * @private: the private data pointer passed to function
910 * @dep: the dependent variables
912 * Returns zero if successful, or a negative error code on failure.
914 int snd_pcm_hw_rule_add(struct snd_pcm_runtime
*runtime
, unsigned int cond
,
916 snd_pcm_hw_rule_func_t func
, void *private,
919 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
920 struct snd_pcm_hw_rule
*c
;
924 if (constrs
->rules_num
>= constrs
->rules_all
) {
925 struct snd_pcm_hw_rule
*new;
926 unsigned int new_rules
= constrs
->rules_all
+ 16;
927 new = kcalloc(new_rules
, sizeof(*c
), GFP_KERNEL
);
930 if (constrs
->rules
) {
931 memcpy(new, constrs
->rules
,
932 constrs
->rules_num
* sizeof(*c
));
933 kfree(constrs
->rules
);
935 constrs
->rules
= new;
936 constrs
->rules_all
= new_rules
;
938 c
= &constrs
->rules
[constrs
->rules_num
];
942 c
->private = private;
945 snd_assert(k
< ARRAY_SIZE(c
->deps
), return -EINVAL
);
949 dep
= va_arg(args
, int);
951 constrs
->rules_num
++;
957 * snd_pcm_hw_constraint_mask
958 * @runtime: PCM runtime instance
959 * @var: hw_params variable to apply the mask
960 * @mask: the bitmap mask
962 * Apply the constraint of the given bitmap mask to a mask parameter.
964 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
,
967 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
968 struct snd_mask
*maskp
= constrs_mask(constrs
, var
);
969 *maskp
->bits
&= mask
;
970 memset(maskp
->bits
+ 1, 0, (SNDRV_MASK_MAX
-32) / 8); /* clear rest */
971 if (*maskp
->bits
== 0)
977 * snd_pcm_hw_constraint_mask64
978 * @runtime: PCM runtime instance
979 * @var: hw_params variable to apply the mask
980 * @mask: the 64bit bitmap mask
982 * Apply the constraint of the given bitmap mask to a mask parameter.
984 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
,
987 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
988 struct snd_mask
*maskp
= constrs_mask(constrs
, var
);
989 maskp
->bits
[0] &= (u_int32_t
)mask
;
990 maskp
->bits
[1] &= (u_int32_t
)(mask
>> 32);
991 memset(maskp
->bits
+ 2, 0, (SNDRV_MASK_MAX
-64) / 8); /* clear rest */
992 if (! maskp
->bits
[0] && ! maskp
->bits
[1])
998 * snd_pcm_hw_constraint_integer
999 * @runtime: PCM runtime instance
1000 * @var: hw_params variable to apply the integer constraint
1002 * Apply the constraint of integer to an interval parameter.
1004 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
)
1006 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1007 return snd_interval_setinteger(constrs_interval(constrs
, var
));
1011 * snd_pcm_hw_constraint_minmax
1012 * @runtime: PCM runtime instance
1013 * @var: hw_params variable to apply the range
1014 * @min: the minimal value
1015 * @max: the maximal value
1017 * Apply the min/max range constraint to an interval parameter.
1019 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
,
1020 unsigned int min
, unsigned int max
)
1022 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1023 struct snd_interval t
;
1026 t
.openmin
= t
.openmax
= 0;
1028 return snd_interval_refine(constrs_interval(constrs
, var
), &t
);
1031 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params
*params
,
1032 struct snd_pcm_hw_rule
*rule
)
1034 struct snd_pcm_hw_constraint_list
*list
= rule
->private;
1035 return snd_interval_list(hw_param_interval(params
, rule
->var
), list
->count
, list
->list
, list
->mask
);
1040 * snd_pcm_hw_constraint_list
1041 * @runtime: PCM runtime instance
1042 * @cond: condition bits
1043 * @var: hw_params variable to apply the list constraint
1046 * Apply the list of constraints to an interval parameter.
1048 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime
*runtime
,
1050 snd_pcm_hw_param_t var
,
1051 struct snd_pcm_hw_constraint_list
*l
)
1053 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1054 snd_pcm_hw_rule_list
, l
,
1058 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params
*params
,
1059 struct snd_pcm_hw_rule
*rule
)
1061 struct snd_pcm_hw_constraint_ratnums
*r
= rule
->private;
1062 unsigned int num
= 0, den
= 0;
1064 err
= snd_interval_ratnum(hw_param_interval(params
, rule
->var
),
1065 r
->nrats
, r
->rats
, &num
, &den
);
1066 if (err
>= 0 && den
&& rule
->var
== SNDRV_PCM_HW_PARAM_RATE
) {
1067 params
->rate_num
= num
;
1068 params
->rate_den
= den
;
1074 * snd_pcm_hw_constraint_ratnums
1075 * @runtime: PCM runtime instance
1076 * @cond: condition bits
1077 * @var: hw_params variable to apply the ratnums constraint
1078 * @r: struct snd_ratnums constriants
1080 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime
*runtime
,
1082 snd_pcm_hw_param_t var
,
1083 struct snd_pcm_hw_constraint_ratnums
*r
)
1085 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1086 snd_pcm_hw_rule_ratnums
, r
,
1090 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params
*params
,
1091 struct snd_pcm_hw_rule
*rule
)
1093 struct snd_pcm_hw_constraint_ratdens
*r
= rule
->private;
1094 unsigned int num
= 0, den
= 0;
1095 int err
= snd_interval_ratden(hw_param_interval(params
, rule
->var
),
1096 r
->nrats
, r
->rats
, &num
, &den
);
1097 if (err
>= 0 && den
&& rule
->var
== SNDRV_PCM_HW_PARAM_RATE
) {
1098 params
->rate_num
= num
;
1099 params
->rate_den
= den
;
1105 * snd_pcm_hw_constraint_ratdens
1106 * @runtime: PCM runtime instance
1107 * @cond: condition bits
1108 * @var: hw_params variable to apply the ratdens constraint
1109 * @r: struct snd_ratdens constriants
1111 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime
*runtime
,
1113 snd_pcm_hw_param_t var
,
1114 struct snd_pcm_hw_constraint_ratdens
*r
)
1116 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1117 snd_pcm_hw_rule_ratdens
, r
,
1121 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params
*params
,
1122 struct snd_pcm_hw_rule
*rule
)
1124 unsigned int l
= (unsigned long) rule
->private;
1125 int width
= l
& 0xffff;
1126 unsigned int msbits
= l
>> 16;
1127 struct snd_interval
*i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
1128 if (snd_interval_single(i
) && snd_interval_value(i
) == width
)
1129 params
->msbits
= msbits
;
1134 * snd_pcm_hw_constraint_msbits
1135 * @runtime: PCM runtime instance
1136 * @cond: condition bits
1137 * @width: sample bits width
1138 * @msbits: msbits width
1140 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime
*runtime
,
1143 unsigned int msbits
)
1145 unsigned long l
= (msbits
<< 16) | width
;
1146 return snd_pcm_hw_rule_add(runtime
, cond
, -1,
1147 snd_pcm_hw_rule_msbits
,
1149 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1152 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params
*params
,
1153 struct snd_pcm_hw_rule
*rule
)
1155 unsigned long step
= (unsigned long) rule
->private;
1156 return snd_interval_step(hw_param_interval(params
, rule
->var
), 0, step
);
1160 * snd_pcm_hw_constraint_step
1161 * @runtime: PCM runtime instance
1162 * @cond: condition bits
1163 * @var: hw_params variable to apply the step constraint
1166 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime
*runtime
,
1168 snd_pcm_hw_param_t var
,
1171 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1172 snd_pcm_hw_rule_step
, (void *) step
,
1176 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params
*params
, struct snd_pcm_hw_rule
*rule
)
1178 static int pow2_sizes
[] = {
1179 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1180 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1181 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1182 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1184 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1185 ARRAY_SIZE(pow2_sizes
), pow2_sizes
, 0);
1189 * snd_pcm_hw_constraint_pow2
1190 * @runtime: PCM runtime instance
1191 * @cond: condition bits
1192 * @var: hw_params variable to apply the power-of-2 constraint
1194 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime
*runtime
,
1196 snd_pcm_hw_param_t var
)
1198 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1199 snd_pcm_hw_rule_pow2
, NULL
,
1203 /* To use the same code we have in alsa-lib */
1204 #define assert(i) snd_assert((i), return -EINVAL)
1206 #define INT_MIN ((int)((unsigned int)INT_MAX+1))
1209 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params
*params
,
1210 snd_pcm_hw_param_t var
)
1212 if (hw_is_mask(var
)) {
1213 snd_mask_any(hw_param_mask(params
, var
));
1214 params
->cmask
|= 1 << var
;
1215 params
->rmask
|= 1 << var
;
1218 if (hw_is_interval(var
)) {
1219 snd_interval_any(hw_param_interval(params
, var
));
1220 params
->cmask
|= 1 << var
;
1221 params
->rmask
|= 1 << var
;
1229 * snd_pcm_hw_param_any
1231 int snd_pcm_hw_param_any(struct snd_pcm_substream
*pcm
, struct snd_pcm_hw_params
*params
,
1232 snd_pcm_hw_param_t var
)
1234 _snd_pcm_hw_param_any(params
, var
);
1235 return snd_pcm_hw_refine(pcm
, params
);
1239 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params
*params
)
1242 memset(params
, 0, sizeof(*params
));
1243 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++)
1244 _snd_pcm_hw_param_any(params
, k
);
1245 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
1246 _snd_pcm_hw_param_any(params
, k
);
1252 * snd_pcm_hw_params_any
1254 * Fill PARAMS with full configuration space boundaries
1256 int snd_pcm_hw_params_any(struct snd_pcm_substream
*pcm
, struct snd_pcm_hw_params
*params
)
1258 _snd_pcm_hw_params_any(params
);
1259 return snd_pcm_hw_refine(pcm
, params
);
1264 * snd_pcm_hw_param_value
1265 * @params: the hw_params instance
1266 * @var: parameter to retrieve
1267 * @dir: pointer to the direction (-1,0,1) or NULL
1269 * Return the value for field PAR if it's fixed in configuration space
1270 * defined by PARAMS. Return -EINVAL otherwise
1272 static int snd_pcm_hw_param_value(const struct snd_pcm_hw_params
*params
,
1273 snd_pcm_hw_param_t var
, int *dir
)
1275 if (hw_is_mask(var
)) {
1276 const struct snd_mask
*mask
= hw_param_mask_c(params
, var
);
1277 if (!snd_mask_single(mask
))
1281 return snd_mask_value(mask
);
1283 if (hw_is_interval(var
)) {
1284 const struct snd_interval
*i
= hw_param_interval_c(params
, var
);
1285 if (!snd_interval_single(i
))
1289 return snd_interval_value(i
);
1296 * snd_pcm_hw_param_value_min
1297 * @params: the hw_params instance
1298 * @var: parameter to retrieve
1299 * @dir: pointer to the direction (-1,0,1) or NULL
1301 * Return the minimum value for field PAR.
1303 unsigned int snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params
*params
,
1304 snd_pcm_hw_param_t var
, int *dir
)
1306 if (hw_is_mask(var
)) {
1309 return snd_mask_min(hw_param_mask_c(params
, var
));
1311 if (hw_is_interval(var
)) {
1312 const struct snd_interval
*i
= hw_param_interval_c(params
, var
);
1315 return snd_interval_min(i
);
1322 * snd_pcm_hw_param_value_max
1323 * @params: the hw_params instance
1324 * @var: parameter to retrieve
1325 * @dir: pointer to the direction (-1,0,1) or NULL
1327 * Return the maximum value for field PAR.
1329 unsigned int snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params
*params
,
1330 snd_pcm_hw_param_t var
, int *dir
)
1332 if (hw_is_mask(var
)) {
1335 return snd_mask_max(hw_param_mask_c(params
, var
));
1337 if (hw_is_interval(var
)) {
1338 const struct snd_interval
*i
= hw_param_interval_c(params
, var
);
1340 *dir
= - (int) i
->openmax
;
1341 return snd_interval_max(i
);
1347 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params
*params
,
1348 snd_pcm_hw_param_t var
)
1350 if (hw_is_mask(var
)) {
1351 snd_mask_none(hw_param_mask(params
, var
));
1352 params
->cmask
|= 1 << var
;
1353 params
->rmask
|= 1 << var
;
1354 } else if (hw_is_interval(var
)) {
1355 snd_interval_none(hw_param_interval(params
, var
));
1356 params
->cmask
|= 1 << var
;
1357 params
->rmask
|= 1 << var
;
1363 int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params
*params
,
1364 snd_pcm_hw_param_t var
)
1367 assert(hw_is_interval(var
));
1368 changed
= snd_interval_setinteger(hw_param_interval(params
, var
));
1370 params
->cmask
|= 1 << var
;
1371 params
->rmask
|= 1 << var
;
1378 * snd_pcm_hw_param_setinteger
1380 * Inside configuration space defined by PARAMS remove from PAR all
1381 * non integer values. Reduce configuration space accordingly.
1382 * Return -EINVAL if the configuration space is empty
1384 int snd_pcm_hw_param_setinteger(struct snd_pcm_substream
*pcm
,
1385 struct snd_pcm_hw_params
*params
,
1386 snd_pcm_hw_param_t var
)
1388 int changed
= _snd_pcm_hw_param_setinteger(params
, var
);
1391 if (params
->rmask
) {
1392 int err
= snd_pcm_hw_refine(pcm
, params
);
1400 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params
*params
,
1401 snd_pcm_hw_param_t var
)
1404 if (hw_is_mask(var
))
1405 changed
= snd_mask_refine_first(hw_param_mask(params
, var
));
1406 else if (hw_is_interval(var
))
1407 changed
= snd_interval_refine_first(hw_param_interval(params
, var
));
1413 params
->cmask
|= 1 << var
;
1414 params
->rmask
|= 1 << var
;
1421 * snd_pcm_hw_param_first
1422 * @pcm: PCM instance
1423 * @params: the hw_params instance
1424 * @var: parameter to retrieve
1425 * @dir: pointer to the direction (-1,0,1) or NULL
1427 * Inside configuration space defined by PARAMS remove from PAR all
1428 * values > minimum. Reduce configuration space accordingly.
1429 * Return the minimum.
1431 static int snd_pcm_hw_param_first(struct snd_pcm_substream
*pcm
,
1432 struct snd_pcm_hw_params
*params
,
1433 snd_pcm_hw_param_t var
, int *dir
)
1435 int changed
= _snd_pcm_hw_param_first(params
, var
);
1438 if (params
->rmask
) {
1439 int err
= snd_pcm_hw_refine(pcm
, params
);
1442 return snd_pcm_hw_param_value(params
, var
, dir
);
1445 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params
*params
,
1446 snd_pcm_hw_param_t var
)
1449 if (hw_is_mask(var
))
1450 changed
= snd_mask_refine_last(hw_param_mask(params
, var
));
1451 else if (hw_is_interval(var
))
1452 changed
= snd_interval_refine_last(hw_param_interval(params
, var
));
1458 params
->cmask
|= 1 << var
;
1459 params
->rmask
|= 1 << var
;
1466 * snd_pcm_hw_param_last
1467 * @pcm: PCM instance
1468 * @params: the hw_params instance
1469 * @var: parameter to retrieve
1470 * @dir: pointer to the direction (-1,0,1) or NULL
1472 * Inside configuration space defined by PARAMS remove from PAR all
1473 * values < maximum. Reduce configuration space accordingly.
1474 * Return the maximum.
1476 static int snd_pcm_hw_param_last(struct snd_pcm_substream
*pcm
,
1477 struct snd_pcm_hw_params
*params
,
1478 snd_pcm_hw_param_t var
, int *dir
)
1480 int changed
= _snd_pcm_hw_param_last(params
, var
);
1483 if (params
->rmask
) {
1484 int err
= snd_pcm_hw_refine(pcm
, params
);
1487 return snd_pcm_hw_param_value(params
, var
, dir
);
1490 int _snd_pcm_hw_param_min(struct snd_pcm_hw_params
*params
,
1491 snd_pcm_hw_param_t var
, unsigned int val
, int dir
)
1498 } else if (dir
< 0) {
1505 if (hw_is_mask(var
))
1506 changed
= snd_mask_refine_min(hw_param_mask(params
, var
), val
+ !!open
);
1507 else if (hw_is_interval(var
))
1508 changed
= snd_interval_refine_min(hw_param_interval(params
, var
), val
, open
);
1514 params
->cmask
|= 1 << var
;
1515 params
->rmask
|= 1 << var
;
1521 * snd_pcm_hw_param_min
1522 * @pcm: PCM instance
1523 * @params: the hw_params instance
1524 * @var: parameter to retrieve
1525 * @val: minimal value
1526 * @dir: pointer to the direction (-1,0,1) or NULL
1528 * Inside configuration space defined by PARAMS remove from PAR all
1529 * values < VAL. Reduce configuration space accordingly.
1530 * Return new minimum or -EINVAL if the configuration space is empty
1532 static int snd_pcm_hw_param_min(struct snd_pcm_substream
*pcm
, struct snd_pcm_hw_params
*params
,
1533 snd_pcm_hw_param_t var
, unsigned int val
,
1536 int changed
= _snd_pcm_hw_param_min(params
, var
, val
, dir
? *dir
: 0);
1539 if (params
->rmask
) {
1540 int err
= snd_pcm_hw_refine(pcm
, params
);
1544 return snd_pcm_hw_param_value_min(params
, var
, dir
);
1547 static int _snd_pcm_hw_param_max(struct snd_pcm_hw_params
*params
,
1548 snd_pcm_hw_param_t var
, unsigned int val
,
1556 } else if (dir
> 0) {
1561 if (hw_is_mask(var
)) {
1562 if (val
== 0 && open
) {
1563 snd_mask_none(hw_param_mask(params
, var
));
1566 changed
= snd_mask_refine_max(hw_param_mask(params
, var
), val
- !!open
);
1567 } else if (hw_is_interval(var
))
1568 changed
= snd_interval_refine_max(hw_param_interval(params
, var
), val
, open
);
1574 params
->cmask
|= 1 << var
;
1575 params
->rmask
|= 1 << var
;
1581 * snd_pcm_hw_param_max
1582 * @pcm: PCM instance
1583 * @params: the hw_params instance
1584 * @var: parameter to retrieve
1585 * @val: maximal value
1586 * @dir: pointer to the direction (-1,0,1) or NULL
1588 * Inside configuration space defined by PARAMS remove from PAR all
1589 * values >= VAL + 1. Reduce configuration space accordingly.
1590 * Return new maximum or -EINVAL if the configuration space is empty
1592 static int snd_pcm_hw_param_max(struct snd_pcm_substream
*pcm
, struct snd_pcm_hw_params
*params
,
1593 snd_pcm_hw_param_t var
, unsigned int val
,
1596 int changed
= _snd_pcm_hw_param_max(params
, var
, val
, dir
? *dir
: 0);
1599 if (params
->rmask
) {
1600 int err
= snd_pcm_hw_refine(pcm
, params
);
1604 return snd_pcm_hw_param_value_max(params
, var
, dir
);
1607 int _snd_pcm_hw_param_set(struct snd_pcm_hw_params
*params
,
1608 snd_pcm_hw_param_t var
, unsigned int val
, int dir
)
1611 if (hw_is_mask(var
)) {
1612 struct snd_mask
*m
= hw_param_mask(params
, var
);
1613 if (val
== 0 && dir
< 0) {
1621 changed
= snd_mask_refine_set(hw_param_mask(params
, var
), val
);
1623 } else if (hw_is_interval(var
)) {
1624 struct snd_interval
*i
= hw_param_interval(params
, var
);
1625 if (val
== 0 && dir
< 0) {
1627 snd_interval_none(i
);
1628 } else if (dir
== 0)
1629 changed
= snd_interval_refine_set(i
, val
);
1631 struct snd_interval t
;
1643 changed
= snd_interval_refine(i
, &t
);
1650 params
->cmask
|= 1 << var
;
1651 params
->rmask
|= 1 << var
;
1657 * snd_pcm_hw_param_set
1658 * @pcm: PCM instance
1659 * @params: the hw_params instance
1660 * @var: parameter to retrieve
1661 * @val: value to set
1662 * @dir: pointer to the direction (-1,0,1) or NULL
1664 * Inside configuration space defined by PARAMS remove from PAR all
1665 * values != VAL. Reduce configuration space accordingly.
1666 * Return VAL or -EINVAL if the configuration space is empty
1668 int snd_pcm_hw_param_set(struct snd_pcm_substream
*pcm
, struct snd_pcm_hw_params
*params
,
1669 snd_pcm_hw_param_t var
, unsigned int val
, int dir
)
1671 int changed
= _snd_pcm_hw_param_set(params
, var
, val
, dir
);
1674 if (params
->rmask
) {
1675 int err
= snd_pcm_hw_refine(pcm
, params
);
1679 return snd_pcm_hw_param_value(params
, var
, NULL
);
1682 static int _snd_pcm_hw_param_mask(struct snd_pcm_hw_params
*params
,
1683 snd_pcm_hw_param_t var
, const struct snd_mask
*val
)
1686 assert(hw_is_mask(var
));
1687 changed
= snd_mask_refine(hw_param_mask(params
, var
), val
);
1689 params
->cmask
|= 1 << var
;
1690 params
->rmask
|= 1 << var
;
1696 * snd_pcm_hw_param_mask
1697 * @pcm: PCM instance
1698 * @params: the hw_params instance
1699 * @var: parameter to retrieve
1700 * @val: mask to apply
1702 * Inside configuration space defined by PARAMS remove from PAR all values
1703 * not contained in MASK. Reduce configuration space accordingly.
1704 * This function can be called only for SNDRV_PCM_HW_PARAM_ACCESS,
1705 * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
1706 * Return 0 on success or -EINVAL
1707 * if the configuration space is empty
1709 int snd_pcm_hw_param_mask(struct snd_pcm_substream
*pcm
, struct snd_pcm_hw_params
*params
,
1710 snd_pcm_hw_param_t var
, const struct snd_mask
*val
)
1712 int changed
= _snd_pcm_hw_param_mask(params
, var
, val
);
1715 if (params
->rmask
) {
1716 int err
= snd_pcm_hw_refine(pcm
, params
);
1723 static int boundary_sub(int a
, int adir
,
1727 adir
= adir
< 0 ? -1 : (adir
> 0 ? 1 : 0);
1728 bdir
= bdir
< 0 ? -1 : (bdir
> 0 ? 1 : 0);
1730 *cdir
= adir
- bdir
;
1732 assert(*c
> INT_MIN
);
1734 } else if (*cdir
== 2) {
1735 assert(*c
< INT_MAX
);
1741 static int boundary_lt(unsigned int a
, int adir
,
1742 unsigned int b
, int bdir
)
1744 assert(a
> 0 || adir
>= 0);
1745 assert(b
> 0 || bdir
>= 0);
1749 } else if (adir
> 0)
1754 } else if (bdir
> 0)
1756 return a
< b
|| (a
== b
&& adir
< bdir
);
1759 /* Return 1 if min is nearer to best than max */
1760 static int boundary_nearer(int min
, int mindir
,
1761 int best
, int bestdir
,
1762 int max
, int maxdir
)
1766 boundary_sub(best
, bestdir
, min
, mindir
, &dmin
, &dmindir
);
1767 boundary_sub(max
, maxdir
, best
, bestdir
, &dmax
, &dmaxdir
);
1768 return boundary_lt(dmin
, dmindir
, dmax
, dmaxdir
);
1772 * snd_pcm_hw_param_near
1773 * @pcm: PCM instance
1774 * @params: the hw_params instance
1775 * @var: parameter to retrieve
1776 * @best: value to set
1777 * @dir: pointer to the direction (-1,0,1) or NULL
1779 * Inside configuration space defined by PARAMS set PAR to the available value
1780 * nearest to VAL. Reduce configuration space accordingly.
1781 * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
1782 * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
1783 * Return the value found.
1785 int snd_pcm_hw_param_near(struct snd_pcm_substream
*pcm
, struct snd_pcm_hw_params
*params
,
1786 snd_pcm_hw_param_t var
, unsigned int best
, int *dir
)
1788 struct snd_pcm_hw_params
*save
= NULL
;
1790 unsigned int saved_min
;
1794 int valdir
= dir
? *dir
: 0;
1799 mindir
= maxdir
= valdir
;
1802 else if (maxdir
== 0)
1808 save
= kmalloc(sizeof(*save
), GFP_KERNEL
);
1813 min
= snd_pcm_hw_param_min(pcm
, params
, var
, min
, &mindir
);
1815 struct snd_pcm_hw_params
*params1
;
1818 if ((unsigned int)min
== saved_min
&& mindir
== valdir
)
1820 params1
= kmalloc(sizeof(*params1
), GFP_KERNEL
);
1821 if (params1
== NULL
) {
1826 max
= snd_pcm_hw_param_max(pcm
, params1
, var
, max
, &maxdir
);
1831 if (boundary_nearer(max
, maxdir
, best
, valdir
, min
, mindir
)) {
1838 max
= snd_pcm_hw_param_max(pcm
, params
, var
, max
, &maxdir
);
1845 v
= snd_pcm_hw_param_last(pcm
, params
, var
, dir
);
1847 v
= snd_pcm_hw_param_first(pcm
, params
, var
, dir
);
1853 * snd_pcm_hw_param_choose
1854 * @pcm: PCM instance
1855 * @params: the hw_params instance
1857 * Choose one configuration from configuration space defined by PARAMS
1858 * The configuration chosen is that obtained fixing in this order:
1859 * first access, first format, first subformat, min channels,
1860 * min rate, min period time, max buffer size, min tick time
1862 int snd_pcm_hw_params_choose(struct snd_pcm_substream
*pcm
, struct snd_pcm_hw_params
*params
)
1866 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_ACCESS
, NULL
);
1869 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_FORMAT
, NULL
);
1872 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, NULL
);
1875 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_CHANNELS
, NULL
);
1878 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_RATE
, NULL
);
1881 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, NULL
);
1884 err
= snd_pcm_hw_param_last(pcm
, params
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, NULL
);
1887 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_TICK_TIME
, NULL
);
1895 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream
*substream
,
1898 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1899 unsigned long flags
;
1900 snd_pcm_stream_lock_irqsave(substream
, flags
);
1901 if (snd_pcm_running(substream
) &&
1902 snd_pcm_update_hw_ptr(substream
) >= 0)
1903 runtime
->status
->hw_ptr
%= runtime
->buffer_size
;
1905 runtime
->status
->hw_ptr
= 0;
1906 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1910 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream
*substream
,
1913 struct snd_pcm_channel_info
*info
= arg
;
1914 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1916 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
)) {
1920 width
= snd_pcm_format_physical_width(runtime
->format
);
1924 switch (runtime
->access
) {
1925 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
:
1926 case SNDRV_PCM_ACCESS_RW_INTERLEAVED
:
1927 info
->first
= info
->channel
* width
;
1928 info
->step
= runtime
->channels
* width
;
1930 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
:
1931 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
:
1933 size_t size
= runtime
->dma_bytes
/ runtime
->channels
;
1934 info
->first
= info
->channel
* size
* 8;
1946 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1947 * @substream: the pcm substream instance
1948 * @cmd: ioctl command
1949 * @arg: ioctl argument
1951 * Processes the generic ioctl commands for PCM.
1952 * Can be passed as the ioctl callback for PCM ops.
1954 * Returns zero if successful, or a negative error code on failure.
1956 int snd_pcm_lib_ioctl(struct snd_pcm_substream
*substream
,
1957 unsigned int cmd
, void *arg
)
1960 case SNDRV_PCM_IOCTL1_INFO
:
1962 case SNDRV_PCM_IOCTL1_RESET
:
1963 return snd_pcm_lib_ioctl_reset(substream
, arg
);
1964 case SNDRV_PCM_IOCTL1_CHANNEL_INFO
:
1965 return snd_pcm_lib_ioctl_channel_info(substream
, arg
);
1974 static void snd_pcm_system_tick_set(struct snd_pcm_substream
*substream
,
1975 unsigned long ticks
)
1977 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1979 del_timer(&runtime
->tick_timer
);
1981 ticks
+= (1000000 / HZ
) - 1;
1982 ticks
/= (1000000 / HZ
);
1983 mod_timer(&runtime
->tick_timer
, jiffies
+ ticks
);
1987 /* Temporary alias */
1988 void snd_pcm_tick_set(struct snd_pcm_substream
*substream
, unsigned long ticks
)
1990 snd_pcm_system_tick_set(substream
, ticks
);
1993 void snd_pcm_tick_prepare(struct snd_pcm_substream
*substream
)
1995 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1996 snd_pcm_uframes_t frames
= ULONG_MAX
;
1997 snd_pcm_uframes_t avail
, dist
;
2001 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
2002 if (runtime
->silence_size
>= runtime
->boundary
) {
2004 } else if (runtime
->silence_size
> 0 &&
2005 runtime
->silence_filled
< runtime
->buffer_size
) {
2006 snd_pcm_sframes_t noise_dist
;
2007 noise_dist
= snd_pcm_playback_hw_avail(runtime
) + runtime
->silence_filled
;
2008 if (noise_dist
> (snd_pcm_sframes_t
)runtime
->silence_threshold
)
2009 frames
= noise_dist
- runtime
->silence_threshold
;
2011 avail
= snd_pcm_playback_avail(runtime
);
2013 avail
= snd_pcm_capture_avail(runtime
);
2015 if (avail
< runtime
->control
->avail_min
) {
2016 snd_pcm_sframes_t n
= runtime
->control
->avail_min
- avail
;
2017 if (n
> 0 && frames
> (snd_pcm_uframes_t
)n
)
2020 if (avail
< runtime
->buffer_size
) {
2021 snd_pcm_sframes_t n
= runtime
->buffer_size
- avail
;
2022 if (n
> 0 && frames
> (snd_pcm_uframes_t
)n
)
2025 if (frames
== ULONG_MAX
) {
2026 snd_pcm_tick_set(substream
, 0);
2029 dist
= runtime
->status
->hw_ptr
- runtime
->hw_ptr_base
;
2030 /* Distance to next interrupt */
2031 dist
= runtime
->period_size
- dist
% runtime
->period_size
;
2032 if (dist
<= frames
) {
2033 snd_pcm_tick_set(substream
, 0);
2036 /* the base time is us */
2039 div64_32(&n
, runtime
->tick_time
* runtime
->rate
, &r
);
2040 ticks
= n
+ (r
> 0 ? 1 : 0);
2041 if (ticks
< runtime
->sleep_min
)
2042 ticks
= runtime
->sleep_min
;
2043 snd_pcm_tick_set(substream
, (unsigned long) ticks
);
2046 void snd_pcm_tick_elapsed(struct snd_pcm_substream
*substream
)
2048 struct snd_pcm_runtime
*runtime
;
2049 unsigned long flags
;
2051 snd_assert(substream
!= NULL
, return);
2052 runtime
= substream
->runtime
;
2053 snd_assert(runtime
!= NULL
, return);
2055 snd_pcm_stream_lock_irqsave(substream
, flags
);
2056 if (!snd_pcm_running(substream
) ||
2057 snd_pcm_update_hw_ptr(substream
) < 0)
2059 if (runtime
->sleep_min
)
2060 snd_pcm_tick_prepare(substream
);
2062 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
2066 * snd_pcm_period_elapsed - update the pcm status for the next period
2067 * @substream: the pcm substream instance
2069 * This function is called from the interrupt handler when the
2070 * PCM has processed the period size. It will update the current
2071 * pointer, set up the tick, wake up sleepers, etc.
2073 * Even if more than one periods have elapsed since the last call, you
2074 * have to call this only once.
2076 void snd_pcm_period_elapsed(struct snd_pcm_substream
*substream
)
2078 struct snd_pcm_runtime
*runtime
;
2079 unsigned long flags
;
2081 snd_assert(substream
!= NULL
, return);
2082 runtime
= substream
->runtime
;
2083 snd_assert(runtime
!= NULL
, return);
2085 if (runtime
->transfer_ack_begin
)
2086 runtime
->transfer_ack_begin(substream
);
2088 snd_pcm_stream_lock_irqsave(substream
, flags
);
2089 if (!snd_pcm_running(substream
) ||
2090 snd_pcm_update_hw_ptr_interrupt(substream
) < 0)
2093 if (substream
->timer_running
)
2094 snd_timer_interrupt(substream
->timer
, 1);
2095 if (runtime
->sleep_min
)
2096 snd_pcm_tick_prepare(substream
);
2098 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
2099 if (runtime
->transfer_ack_end
)
2100 runtime
->transfer_ack_end(substream
);
2101 kill_fasync(&runtime
->fasync
, SIGIO
, POLL_IN
);
2104 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream
*substream
,
2106 unsigned long data
, unsigned int off
,
2107 snd_pcm_uframes_t frames
)
2109 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2111 char __user
*buf
= (char __user
*) data
+ frames_to_bytes(runtime
, off
);
2112 if (substream
->ops
->copy
) {
2113 if ((err
= substream
->ops
->copy(substream
, -1, hwoff
, buf
, frames
)) < 0)
2116 char *hwbuf
= runtime
->dma_area
+ frames_to_bytes(runtime
, hwoff
);
2117 snd_assert(runtime
->dma_area
, return -EFAULT
);
2118 if (copy_from_user(hwbuf
, buf
, frames_to_bytes(runtime
, frames
)))
2124 typedef int (*transfer_f
)(struct snd_pcm_substream
*substream
, unsigned int hwoff
,
2125 unsigned long data
, unsigned int off
,
2126 snd_pcm_uframes_t size
);
2128 static snd_pcm_sframes_t
snd_pcm_lib_write1(struct snd_pcm_substream
*substream
,
2130 snd_pcm_uframes_t size
,
2132 transfer_f transfer
)
2134 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2135 snd_pcm_uframes_t xfer
= 0;
2136 snd_pcm_uframes_t offset
= 0;
2141 if (size
> runtime
->xfer_align
)
2142 size
-= size
% runtime
->xfer_align
;
2144 snd_pcm_stream_lock_irq(substream
);
2145 switch (runtime
->status
->state
) {
2146 case SNDRV_PCM_STATE_PREPARED
:
2147 case SNDRV_PCM_STATE_RUNNING
:
2148 case SNDRV_PCM_STATE_PAUSED
:
2150 case SNDRV_PCM_STATE_XRUN
:
2153 case SNDRV_PCM_STATE_SUSPENDED
:
2162 snd_pcm_uframes_t frames
, appl_ptr
, appl_ofs
;
2163 snd_pcm_uframes_t avail
;
2164 snd_pcm_uframes_t cont
;
2165 if (runtime
->sleep_min
== 0 && runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
2166 snd_pcm_update_hw_ptr(substream
);
2167 avail
= snd_pcm_playback_avail(runtime
);
2168 if (((avail
< runtime
->control
->avail_min
&& size
> avail
) ||
2169 (size
>= runtime
->xfer_align
&& avail
< runtime
->xfer_align
))) {
2171 enum { READY
, SIGNALED
, ERROR
, SUSPENDED
, EXPIRED
, DROPPED
} state
;
2179 init_waitqueue_entry(&wait
, current
);
2180 add_wait_queue(&runtime
->sleep
, &wait
);
2182 if (signal_pending(current
)) {
2186 set_current_state(TASK_INTERRUPTIBLE
);
2187 snd_pcm_stream_unlock_irq(substream
);
2188 tout
= schedule_timeout(10 * HZ
);
2189 snd_pcm_stream_lock_irq(substream
);
2191 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
&&
2192 runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
) {
2193 state
= runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
? SUSPENDED
: EXPIRED
;
2197 switch (runtime
->status
->state
) {
2198 case SNDRV_PCM_STATE_XRUN
:
2199 case SNDRV_PCM_STATE_DRAINING
:
2202 case SNDRV_PCM_STATE_SUSPENDED
:
2205 case SNDRV_PCM_STATE_SETUP
:
2211 avail
= snd_pcm_playback_avail(runtime
);
2212 if (avail
>= runtime
->control
->avail_min
) {
2218 remove_wait_queue(&runtime
->sleep
, &wait
);
2231 snd_printd("playback write error (DMA or IRQ trouble?)\n");
2241 if (avail
> runtime
->xfer_align
)
2242 avail
-= avail
% runtime
->xfer_align
;
2243 frames
= size
> avail
? avail
: size
;
2244 cont
= runtime
->buffer_size
- runtime
->control
->appl_ptr
% runtime
->buffer_size
;
2247 snd_assert(frames
!= 0, snd_pcm_stream_unlock_irq(substream
); return -EINVAL
);
2248 appl_ptr
= runtime
->control
->appl_ptr
;
2249 appl_ofs
= appl_ptr
% runtime
->buffer_size
;
2250 snd_pcm_stream_unlock_irq(substream
);
2251 if ((err
= transfer(substream
, appl_ofs
, data
, offset
, frames
)) < 0)
2253 snd_pcm_stream_lock_irq(substream
);
2254 switch (runtime
->status
->state
) {
2255 case SNDRV_PCM_STATE_XRUN
:
2258 case SNDRV_PCM_STATE_SUSPENDED
:
2265 if (appl_ptr
>= runtime
->boundary
)
2266 appl_ptr
-= runtime
->boundary
;
2267 runtime
->control
->appl_ptr
= appl_ptr
;
2268 if (substream
->ops
->ack
)
2269 substream
->ops
->ack(substream
);
2274 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
&&
2275 snd_pcm_playback_hw_avail(runtime
) >= (snd_pcm_sframes_t
)runtime
->start_threshold
) {
2276 err
= snd_pcm_start(substream
);
2280 if (runtime
->sleep_min
&&
2281 runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
2282 snd_pcm_tick_prepare(substream
);
2285 snd_pcm_stream_unlock_irq(substream
);
2287 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: err
;
2290 snd_pcm_sframes_t
snd_pcm_lib_write(struct snd_pcm_substream
*substream
, const void __user
*buf
, snd_pcm_uframes_t size
)
2292 struct snd_pcm_runtime
*runtime
;
2295 snd_assert(substream
!= NULL
, return -ENXIO
);
2296 runtime
= substream
->runtime
;
2297 snd_assert(runtime
!= NULL
, return -ENXIO
);
2298 snd_assert(substream
->ops
->copy
!= NULL
|| runtime
->dma_area
!= NULL
, return -EINVAL
);
2299 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2302 snd_assert(substream
->ffile
!= NULL
, return -ENXIO
);
2303 nonblock
= !!(substream
->ffile
->f_flags
& O_NONBLOCK
);
2304 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2305 if (substream
->oss
.oss
) {
2306 struct snd_pcm_oss_setup
*setup
= substream
->oss
.setup
;
2307 if (setup
!= NULL
) {
2308 if (setup
->nonblock
)
2310 else if (setup
->block
)
2316 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_INTERLEAVED
&&
2317 runtime
->channels
> 1)
2319 return snd_pcm_lib_write1(substream
, (unsigned long)buf
, size
, nonblock
,
2320 snd_pcm_lib_write_transfer
);
2323 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream
*substream
,
2325 unsigned long data
, unsigned int off
,
2326 snd_pcm_uframes_t frames
)
2328 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2330 void __user
**bufs
= (void __user
**)data
;
2331 int channels
= runtime
->channels
;
2333 if (substream
->ops
->copy
) {
2334 snd_assert(substream
->ops
->silence
!= NULL
, return -EINVAL
);
2335 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
2336 if (*bufs
== NULL
) {
2337 if ((err
= substream
->ops
->silence(substream
, c
, hwoff
, frames
)) < 0)
2340 char __user
*buf
= *bufs
+ samples_to_bytes(runtime
, off
);
2341 if ((err
= substream
->ops
->copy(substream
, c
, hwoff
, buf
, frames
)) < 0)
2346 /* default transfer behaviour */
2347 size_t dma_csize
= runtime
->dma_bytes
/ channels
;
2348 snd_assert(runtime
->dma_area
, return -EFAULT
);
2349 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
2350 char *hwbuf
= runtime
->dma_area
+ (c
* dma_csize
) + samples_to_bytes(runtime
, hwoff
);
2351 if (*bufs
== NULL
) {
2352 snd_pcm_format_set_silence(runtime
->format
, hwbuf
, frames
);
2354 char __user
*buf
= *bufs
+ samples_to_bytes(runtime
, off
);
2355 if (copy_from_user(hwbuf
, buf
, samples_to_bytes(runtime
, frames
)))
2363 snd_pcm_sframes_t
snd_pcm_lib_writev(struct snd_pcm_substream
*substream
,
2365 snd_pcm_uframes_t frames
)
2367 struct snd_pcm_runtime
*runtime
;
2370 snd_assert(substream
!= NULL
, return -ENXIO
);
2371 runtime
= substream
->runtime
;
2372 snd_assert(runtime
!= NULL
, return -ENXIO
);
2373 snd_assert(substream
->ops
->copy
!= NULL
|| runtime
->dma_area
!= NULL
, return -EINVAL
);
2374 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2377 snd_assert(substream
->ffile
!= NULL
, return -ENXIO
);
2378 nonblock
= !!(substream
->ffile
->f_flags
& O_NONBLOCK
);
2379 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2380 if (substream
->oss
.oss
) {
2381 struct snd_pcm_oss_setup
*setup
= substream
->oss
.setup
;
2382 if (setup
!= NULL
) {
2383 if (setup
->nonblock
)
2385 else if (setup
->block
)
2391 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
2393 return snd_pcm_lib_write1(substream
, (unsigned long)bufs
, frames
,
2394 nonblock
, snd_pcm_lib_writev_transfer
);
2397 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream
*substream
,
2399 unsigned long data
, unsigned int off
,
2400 snd_pcm_uframes_t frames
)
2402 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2404 char __user
*buf
= (char __user
*) data
+ frames_to_bytes(runtime
, off
);
2405 if (substream
->ops
->copy
) {
2406 if ((err
= substream
->ops
->copy(substream
, -1, hwoff
, buf
, frames
)) < 0)
2409 char *hwbuf
= runtime
->dma_area
+ frames_to_bytes(runtime
, hwoff
);
2410 snd_assert(runtime
->dma_area
, return -EFAULT
);
2411 if (copy_to_user(buf
, hwbuf
, frames_to_bytes(runtime
, frames
)))
2417 static snd_pcm_sframes_t
snd_pcm_lib_read1(struct snd_pcm_substream
*substream
,
2419 snd_pcm_uframes_t size
,
2421 transfer_f transfer
)
2423 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2424 snd_pcm_uframes_t xfer
= 0;
2425 snd_pcm_uframes_t offset
= 0;
2430 if (size
> runtime
->xfer_align
)
2431 size
-= size
% runtime
->xfer_align
;
2433 snd_pcm_stream_lock_irq(substream
);
2434 switch (runtime
->status
->state
) {
2435 case SNDRV_PCM_STATE_PREPARED
:
2436 if (size
>= runtime
->start_threshold
) {
2437 err
= snd_pcm_start(substream
);
2442 case SNDRV_PCM_STATE_DRAINING
:
2443 case SNDRV_PCM_STATE_RUNNING
:
2444 case SNDRV_PCM_STATE_PAUSED
:
2446 case SNDRV_PCM_STATE_XRUN
:
2449 case SNDRV_PCM_STATE_SUSPENDED
:
2458 snd_pcm_uframes_t frames
, appl_ptr
, appl_ofs
;
2459 snd_pcm_uframes_t avail
;
2460 snd_pcm_uframes_t cont
;
2461 if (runtime
->sleep_min
== 0 && runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
2462 snd_pcm_update_hw_ptr(substream
);
2464 avail
= snd_pcm_capture_avail(runtime
);
2465 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
2466 if (avail
< runtime
->xfer_align
) {
2470 } else if ((avail
< runtime
->control
->avail_min
&& size
> avail
) ||
2471 (size
>= runtime
->xfer_align
&& avail
< runtime
->xfer_align
)) {
2473 enum { READY
, SIGNALED
, ERROR
, SUSPENDED
, EXPIRED
, DROPPED
} state
;
2481 init_waitqueue_entry(&wait
, current
);
2482 add_wait_queue(&runtime
->sleep
, &wait
);
2484 if (signal_pending(current
)) {
2488 set_current_state(TASK_INTERRUPTIBLE
);
2489 snd_pcm_stream_unlock_irq(substream
);
2490 tout
= schedule_timeout(10 * HZ
);
2491 snd_pcm_stream_lock_irq(substream
);
2493 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
&&
2494 runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
) {
2495 state
= runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
? SUSPENDED
: EXPIRED
;
2499 switch (runtime
->status
->state
) {
2500 case SNDRV_PCM_STATE_XRUN
:
2503 case SNDRV_PCM_STATE_SUSPENDED
:
2506 case SNDRV_PCM_STATE_DRAINING
:
2508 case SNDRV_PCM_STATE_SETUP
:
2514 avail
= snd_pcm_capture_avail(runtime
);
2515 if (avail
>= runtime
->control
->avail_min
) {
2521 remove_wait_queue(&runtime
->sleep
, &wait
);
2534 snd_printd("capture read error (DMA or IRQ trouble?)\n");
2544 if (avail
> runtime
->xfer_align
)
2545 avail
-= avail
% runtime
->xfer_align
;
2546 frames
= size
> avail
? avail
: size
;
2547 cont
= runtime
->buffer_size
- runtime
->control
->appl_ptr
% runtime
->buffer_size
;
2550 snd_assert(frames
!= 0, snd_pcm_stream_unlock_irq(substream
); return -EINVAL
);
2551 appl_ptr
= runtime
->control
->appl_ptr
;
2552 appl_ofs
= appl_ptr
% runtime
->buffer_size
;
2553 snd_pcm_stream_unlock_irq(substream
);
2554 if ((err
= transfer(substream
, appl_ofs
, data
, offset
, frames
)) < 0)
2556 snd_pcm_stream_lock_irq(substream
);
2557 switch (runtime
->status
->state
) {
2558 case SNDRV_PCM_STATE_XRUN
:
2561 case SNDRV_PCM_STATE_SUSPENDED
:
2568 if (appl_ptr
>= runtime
->boundary
)
2569 appl_ptr
-= runtime
->boundary
;
2570 runtime
->control
->appl_ptr
= appl_ptr
;
2571 if (substream
->ops
->ack
)
2572 substream
->ops
->ack(substream
);
2577 if (runtime
->sleep_min
&&
2578 runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
2579 snd_pcm_tick_prepare(substream
);
2582 snd_pcm_stream_unlock_irq(substream
);
2584 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: err
;
2587 snd_pcm_sframes_t
snd_pcm_lib_read(struct snd_pcm_substream
*substream
, void __user
*buf
, snd_pcm_uframes_t size
)
2589 struct snd_pcm_runtime
*runtime
;
2592 snd_assert(substream
!= NULL
, return -ENXIO
);
2593 runtime
= substream
->runtime
;
2594 snd_assert(runtime
!= NULL
, return -ENXIO
);
2595 snd_assert(substream
->ops
->copy
!= NULL
|| runtime
->dma_area
!= NULL
, return -EINVAL
);
2596 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2599 snd_assert(substream
->ffile
!= NULL
, return -ENXIO
);
2600 nonblock
= !!(substream
->ffile
->f_flags
& O_NONBLOCK
);
2601 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2602 if (substream
->oss
.oss
) {
2603 struct snd_pcm_oss_setup
*setup
= substream
->oss
.setup
;
2604 if (setup
!= NULL
) {
2605 if (setup
->nonblock
)
2607 else if (setup
->block
)
2612 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_INTERLEAVED
)
2614 return snd_pcm_lib_read1(substream
, (unsigned long)buf
, size
, nonblock
, snd_pcm_lib_read_transfer
);
2617 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream
*substream
,
2619 unsigned long data
, unsigned int off
,
2620 snd_pcm_uframes_t frames
)
2622 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2624 void __user
**bufs
= (void __user
**)data
;
2625 int channels
= runtime
->channels
;
2627 if (substream
->ops
->copy
) {
2628 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
2632 buf
= *bufs
+ samples_to_bytes(runtime
, off
);
2633 if ((err
= substream
->ops
->copy(substream
, c
, hwoff
, buf
, frames
)) < 0)
2637 snd_pcm_uframes_t dma_csize
= runtime
->dma_bytes
/ channels
;
2638 snd_assert(runtime
->dma_area
, return -EFAULT
);
2639 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
2645 hwbuf
= runtime
->dma_area
+ (c
* dma_csize
) + samples_to_bytes(runtime
, hwoff
);
2646 buf
= *bufs
+ samples_to_bytes(runtime
, off
);
2647 if (copy_to_user(buf
, hwbuf
, samples_to_bytes(runtime
, frames
)))
2654 snd_pcm_sframes_t
snd_pcm_lib_readv(struct snd_pcm_substream
*substream
,
2656 snd_pcm_uframes_t frames
)
2658 struct snd_pcm_runtime
*runtime
;
2661 snd_assert(substream
!= NULL
, return -ENXIO
);
2662 runtime
= substream
->runtime
;
2663 snd_assert(runtime
!= NULL
, return -ENXIO
);
2664 snd_assert(substream
->ops
->copy
!= NULL
|| runtime
->dma_area
!= NULL
, return -EINVAL
);
2665 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2668 snd_assert(substream
->ffile
!= NULL
, return -ENXIO
);
2669 nonblock
= !!(substream
->ffile
->f_flags
& O_NONBLOCK
);
2670 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2671 if (substream
->oss
.oss
) {
2672 struct snd_pcm_oss_setup
*setup
= substream
->oss
.setup
;
2673 if (setup
!= NULL
) {
2674 if (setup
->nonblock
)
2676 else if (setup
->block
)
2682 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
2684 return snd_pcm_lib_read1(substream
, (unsigned long)bufs
, frames
, nonblock
, snd_pcm_lib_readv_transfer
);
2691 EXPORT_SYMBOL(snd_interval_refine
);
2692 EXPORT_SYMBOL(snd_interval_list
);
2693 EXPORT_SYMBOL(snd_interval_ratnum
);
2694 EXPORT_SYMBOL(_snd_pcm_hw_params_any
);
2695 EXPORT_SYMBOL(_snd_pcm_hw_param_min
);
2696 EXPORT_SYMBOL(_snd_pcm_hw_param_set
);
2697 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty
);
2698 EXPORT_SYMBOL(_snd_pcm_hw_param_setinteger
);
2699 EXPORT_SYMBOL(snd_pcm_hw_param_value_min
);
2700 EXPORT_SYMBOL(snd_pcm_hw_param_value_max
);
2701 EXPORT_SYMBOL(snd_pcm_hw_param_mask
);
2702 EXPORT_SYMBOL(snd_pcm_hw_param_first
);
2703 EXPORT_SYMBOL(snd_pcm_hw_param_last
);
2704 EXPORT_SYMBOL(snd_pcm_hw_param_near
);
2705 EXPORT_SYMBOL(snd_pcm_hw_param_set
);
2706 EXPORT_SYMBOL(snd_pcm_hw_refine
);
2707 EXPORT_SYMBOL(snd_pcm_hw_constraints_init
);
2708 EXPORT_SYMBOL(snd_pcm_hw_constraints_complete
);
2709 EXPORT_SYMBOL(snd_pcm_hw_constraint_list
);
2710 EXPORT_SYMBOL(snd_pcm_hw_constraint_step
);
2711 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums
);
2712 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens
);
2713 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits
);
2714 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax
);
2715 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer
);
2716 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2
);
2717 EXPORT_SYMBOL(snd_pcm_hw_rule_add
);
2718 EXPORT_SYMBOL(snd_pcm_set_ops
);
2719 EXPORT_SYMBOL(snd_pcm_set_sync
);
2720 EXPORT_SYMBOL(snd_pcm_lib_ioctl
);
2721 EXPORT_SYMBOL(snd_pcm_stop
);
2722 EXPORT_SYMBOL(snd_pcm_period_elapsed
);
2723 EXPORT_SYMBOL(snd_pcm_lib_write
);
2724 EXPORT_SYMBOL(snd_pcm_lib_read
);
2725 EXPORT_SYMBOL(snd_pcm_lib_writev
);
2726 EXPORT_SYMBOL(snd_pcm_lib_readv
);
2727 EXPORT_SYMBOL(snd_pcm_lib_buffer_bytes
);
2728 EXPORT_SYMBOL(snd_pcm_lib_period_bytes
);
2730 EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all
);
2731 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages
);
2732 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all
);
2733 EXPORT_SYMBOL(snd_pcm_sgbuf_ops_page
);
2734 EXPORT_SYMBOL(snd_pcm_lib_malloc_pages
);
2735 EXPORT_SYMBOL(snd_pcm_lib_free_pages
);