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(snd_pcm_substream_t
*substream
, snd_pcm_uframes_t new_hw_ptr
)
44 snd_pcm_runtime_t
*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 snd_assert(runtime
->silence_filled
<= runtime
->buffer_size
, return);
62 noise_dist
= snd_pcm_playback_hw_avail(runtime
) + runtime
->silence_filled
;
63 if (noise_dist
>= (snd_pcm_sframes_t
) runtime
->silence_threshold
)
65 frames
= runtime
->silence_threshold
- noise_dist
;
66 if (frames
> runtime
->silence_size
)
67 frames
= runtime
->silence_size
;
69 if (new_hw_ptr
== ULONG_MAX
) { /* initialization */
70 snd_pcm_sframes_t avail
= snd_pcm_playback_hw_avail(runtime
);
71 runtime
->silence_filled
= avail
> 0 ? avail
: 0;
72 runtime
->silence_start
= (runtime
->status
->hw_ptr
+
73 runtime
->silence_filled
) %
76 ofs
= runtime
->status
->hw_ptr
;
77 frames
= new_hw_ptr
- ofs
;
78 if ((snd_pcm_sframes_t
)frames
< 0)
79 frames
+= runtime
->boundary
;
80 runtime
->silence_filled
-= frames
;
81 if ((snd_pcm_sframes_t
)runtime
->silence_filled
< 0) {
82 runtime
->silence_filled
= 0;
83 runtime
->silence_start
= (ofs
+ frames
) - runtime
->buffer_size
;
85 runtime
->silence_start
= ofs
- runtime
->silence_filled
;
87 if ((snd_pcm_sframes_t
)runtime
->silence_start
< 0)
88 runtime
->silence_start
+= runtime
->boundary
;
90 frames
= runtime
->buffer_size
- runtime
->silence_filled
;
92 snd_assert(frames
<= runtime
->buffer_size
, return);
95 ofs
= (runtime
->silence_start
+ runtime
->silence_filled
) % runtime
->buffer_size
;
97 transfer
= ofs
+ frames
> runtime
->buffer_size
? runtime
->buffer_size
- ofs
: frames
;
98 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
99 runtime
->access
== SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
) {
100 if (substream
->ops
->silence
) {
102 err
= substream
->ops
->silence(substream
, -1, ofs
, transfer
);
103 snd_assert(err
>= 0, );
105 char *hwbuf
= runtime
->dma_area
+ frames_to_bytes(runtime
, ofs
);
106 snd_pcm_format_set_silence(runtime
->format
, hwbuf
, transfer
* runtime
->channels
);
110 unsigned int channels
= runtime
->channels
;
111 if (substream
->ops
->silence
) {
112 for (c
= 0; c
< channels
; ++c
) {
114 err
= substream
->ops
->silence(substream
, c
, ofs
, transfer
);
115 snd_assert(err
>= 0, );
118 size_t dma_csize
= runtime
->dma_bytes
/ channels
;
119 for (c
= 0; c
< channels
; ++c
) {
120 char *hwbuf
= runtime
->dma_area
+ (c
* dma_csize
) + samples_to_bytes(runtime
, ofs
);
121 snd_pcm_format_set_silence(runtime
->format
, hwbuf
, transfer
);
125 runtime
->silence_filled
+= transfer
;
131 static void xrun(snd_pcm_substream_t
*substream
)
133 snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
134 #ifdef CONFIG_SND_DEBUG
135 if (substream
->pstr
->xrun_debug
) {
136 snd_printd(KERN_DEBUG
"XRUN: pcmC%dD%d%c\n",
137 substream
->pcm
->card
->number
,
138 substream
->pcm
->device
,
139 substream
->stream
? 'c' : 'p');
140 if (substream
->pstr
->xrun_debug
> 1)
146 static inline snd_pcm_uframes_t
snd_pcm_update_hw_ptr_pos(snd_pcm_substream_t
*substream
,
147 snd_pcm_runtime_t
*runtime
)
149 snd_pcm_uframes_t pos
;
151 pos
= substream
->ops
->pointer(substream
);
152 if (pos
== SNDRV_PCM_POS_XRUN
)
153 return pos
; /* XRUN */
154 if (runtime
->tstamp_mode
& SNDRV_PCM_TSTAMP_MMAP
)
155 snd_timestamp_now((snd_timestamp_t
*)&runtime
->status
->tstamp
, runtime
->tstamp_timespec
);
156 #ifdef CONFIG_SND_DEBUG
157 if (pos
>= runtime
->buffer_size
) {
158 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
);
161 snd_runtime_check(pos
< runtime
->buffer_size
, return 0);
162 pos
-= pos
% runtime
->min_align
;
166 static inline int snd_pcm_update_hw_ptr_post(snd_pcm_substream_t
*substream
,
167 snd_pcm_runtime_t
*runtime
)
169 snd_pcm_uframes_t avail
;
171 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
172 avail
= snd_pcm_playback_avail(runtime
);
174 avail
= snd_pcm_capture_avail(runtime
);
175 if (avail
> runtime
->avail_max
)
176 runtime
->avail_max
= avail
;
177 if (avail
>= runtime
->stop_threshold
) {
178 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
)
179 snd_pcm_drain_done(substream
);
184 if (avail
>= runtime
->control
->avail_min
)
185 wake_up(&runtime
->sleep
);
189 static inline int snd_pcm_update_hw_ptr_interrupt(snd_pcm_substream_t
*substream
)
191 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
192 snd_pcm_uframes_t pos
;
193 snd_pcm_uframes_t new_hw_ptr
, hw_ptr_interrupt
;
194 snd_pcm_sframes_t delta
;
196 pos
= snd_pcm_update_hw_ptr_pos(substream
, runtime
);
197 if (pos
== SNDRV_PCM_POS_XRUN
) {
201 if (runtime
->period_size
== runtime
->buffer_size
)
203 new_hw_ptr
= runtime
->hw_ptr_base
+ pos
;
204 hw_ptr_interrupt
= runtime
->hw_ptr_interrupt
+ runtime
->period_size
;
206 delta
= hw_ptr_interrupt
- new_hw_ptr
;
208 if ((snd_pcm_uframes_t
)delta
< runtime
->buffer_size
/ 2) {
209 #ifdef CONFIG_SND_DEBUG
210 if (runtime
->periods
> 1 && substream
->pstr
->xrun_debug
) {
211 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);
212 if (substream
->pstr
->xrun_debug
> 1)
219 runtime
->hw_ptr_base
+= runtime
->buffer_size
;
220 if (runtime
->hw_ptr_base
== runtime
->boundary
)
221 runtime
->hw_ptr_base
= 0;
222 new_hw_ptr
= runtime
->hw_ptr_base
+ pos
;
225 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
226 runtime
->silence_size
> 0)
227 snd_pcm_playback_silence(substream
, new_hw_ptr
);
229 runtime
->status
->hw_ptr
= new_hw_ptr
;
230 runtime
->hw_ptr_interrupt
= new_hw_ptr
- new_hw_ptr
% runtime
->period_size
;
232 return snd_pcm_update_hw_ptr_post(substream
, runtime
);
235 /* CAUTION: call it with irq disabled */
236 int snd_pcm_update_hw_ptr(snd_pcm_substream_t
*substream
)
238 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
239 snd_pcm_uframes_t pos
;
240 snd_pcm_uframes_t old_hw_ptr
, new_hw_ptr
;
241 snd_pcm_sframes_t delta
;
243 old_hw_ptr
= runtime
->status
->hw_ptr
;
244 pos
= snd_pcm_update_hw_ptr_pos(substream
, runtime
);
245 if (pos
== SNDRV_PCM_POS_XRUN
) {
249 new_hw_ptr
= runtime
->hw_ptr_base
+ pos
;
251 delta
= old_hw_ptr
- new_hw_ptr
;
253 if ((snd_pcm_uframes_t
)delta
< runtime
->buffer_size
/ 2) {
254 #ifdef CONFIG_SND_DEBUG
255 if (runtime
->periods
> 2 && substream
->pstr
->xrun_debug
) {
256 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);
257 if (substream
->pstr
->xrun_debug
> 1)
263 runtime
->hw_ptr_base
+= runtime
->buffer_size
;
264 if (runtime
->hw_ptr_base
== runtime
->boundary
)
265 runtime
->hw_ptr_base
= 0;
266 new_hw_ptr
= runtime
->hw_ptr_base
+ pos
;
268 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
269 runtime
->silence_size
> 0)
270 snd_pcm_playback_silence(substream
, new_hw_ptr
);
272 runtime
->status
->hw_ptr
= new_hw_ptr
;
274 return snd_pcm_update_hw_ptr_post(substream
, runtime
);
278 * snd_pcm_set_ops - set the PCM operators
279 * @pcm: the pcm instance
280 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
281 * @ops: the operator table
283 * Sets the given PCM operators to the pcm instance.
285 void snd_pcm_set_ops(snd_pcm_t
*pcm
, int direction
, snd_pcm_ops_t
*ops
)
287 snd_pcm_str_t
*stream
= &pcm
->streams
[direction
];
288 snd_pcm_substream_t
*substream
;
290 for (substream
= stream
->substream
; substream
!= NULL
; substream
= substream
->next
)
291 substream
->ops
= ops
;
296 * snd_pcm_sync - set the PCM sync id
297 * @substream: the pcm substream
299 * Sets the PCM sync identifier for the card.
301 void snd_pcm_set_sync(snd_pcm_substream_t
* substream
)
303 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
305 runtime
->sync
.id32
[0] = substream
->pcm
->card
->number
;
306 runtime
->sync
.id32
[1] = -1;
307 runtime
->sync
.id32
[2] = -1;
308 runtime
->sync
.id32
[3] = -1;
312 * Standard ioctl routine
315 /* Code taken from alsa-lib */
316 #define assert(a) snd_assert((a), return -EINVAL)
318 static inline unsigned int div32(unsigned int a
, unsigned int b
,
329 static inline unsigned int div_down(unsigned int a
, unsigned int b
)
336 static inline unsigned int div_up(unsigned int a
, unsigned int b
)
348 static inline unsigned int mul(unsigned int a
, unsigned int b
)
352 if (div_down(UINT_MAX
, a
) < b
)
357 static inline unsigned int muldiv32(unsigned int a
, unsigned int b
,
358 unsigned int c
, unsigned int *r
)
360 u_int64_t n
= (u_int64_t
) a
* b
;
374 static int snd_interval_refine_min(snd_interval_t
*i
, unsigned int min
, int openmin
)
377 assert(!snd_interval_empty(i
));
380 i
->openmin
= openmin
;
382 } else if (i
->min
== min
&& !i
->openmin
&& openmin
) {
392 if (snd_interval_checkempty(i
)) {
393 snd_interval_none(i
);
399 static int snd_interval_refine_max(snd_interval_t
*i
, unsigned int max
, int openmax
)
402 assert(!snd_interval_empty(i
));
405 i
->openmax
= openmax
;
407 } else if (i
->max
== max
&& !i
->openmax
&& openmax
) {
417 if (snd_interval_checkempty(i
)) {
418 snd_interval_none(i
);
425 * snd_interval_refine - refine the interval value of configurator
426 * @i: the interval value to refine
427 * @v: the interval value to refer to
429 * Refines the interval value with the reference value.
430 * The interval is changed to the range satisfying both intervals.
431 * The interval status (min, max, integer, etc.) are evaluated.
433 * Returns non-zero if the value is changed, zero if not changed.
435 int snd_interval_refine(snd_interval_t
*i
, const snd_interval_t
*v
)
438 assert(!snd_interval_empty(i
));
439 if (i
->min
< v
->min
) {
441 i
->openmin
= v
->openmin
;
443 } else if (i
->min
== v
->min
&& !i
->openmin
&& v
->openmin
) {
447 if (i
->max
> v
->max
) {
449 i
->openmax
= v
->openmax
;
451 } else if (i
->max
== v
->max
&& !i
->openmax
&& v
->openmax
) {
455 if (!i
->integer
&& v
->integer
) {
468 } else if (!i
->openmin
&& !i
->openmax
&& i
->min
== i
->max
)
470 if (snd_interval_checkempty(i
)) {
471 snd_interval_none(i
);
477 static int snd_interval_refine_first(snd_interval_t
*i
)
479 assert(!snd_interval_empty(i
));
480 if (snd_interval_single(i
))
483 i
->openmax
= i
->openmin
;
489 static int snd_interval_refine_last(snd_interval_t
*i
)
491 assert(!snd_interval_empty(i
));
492 if (snd_interval_single(i
))
495 i
->openmin
= i
->openmax
;
501 static int snd_interval_refine_set(snd_interval_t
*i
, unsigned int val
)
506 t
.openmin
= t
.openmax
= 0;
508 return snd_interval_refine(i
, &t
);
511 void snd_interval_mul(const snd_interval_t
*a
, const snd_interval_t
*b
, snd_interval_t
*c
)
513 if (a
->empty
|| b
->empty
) {
514 snd_interval_none(c
);
518 c
->min
= mul(a
->min
, b
->min
);
519 c
->openmin
= (a
->openmin
|| b
->openmin
);
520 c
->max
= mul(a
->max
, b
->max
);
521 c
->openmax
= (a
->openmax
|| b
->openmax
);
522 c
->integer
= (a
->integer
&& b
->integer
);
526 * snd_interval_div - refine the interval value with division
533 * Returns non-zero if the value is changed, zero if not changed.
535 void snd_interval_div(const snd_interval_t
*a
, const snd_interval_t
*b
, snd_interval_t
*c
)
538 if (a
->empty
|| b
->empty
) {
539 snd_interval_none(c
);
543 c
->min
= div32(a
->min
, b
->max
, &r
);
544 c
->openmin
= (r
|| a
->openmin
|| b
->openmax
);
546 c
->max
= div32(a
->max
, b
->min
, &r
);
551 c
->openmax
= (a
->openmax
|| b
->openmin
);
560 * snd_interval_muldivk - refine the interval value
563 * @k: divisor (as integer)
568 * Returns non-zero if the value is changed, zero if not changed.
570 void snd_interval_muldivk(const snd_interval_t
*a
, const snd_interval_t
*b
,
571 unsigned int k
, snd_interval_t
*c
)
574 if (a
->empty
|| b
->empty
) {
575 snd_interval_none(c
);
579 c
->min
= muldiv32(a
->min
, b
->min
, k
, &r
);
580 c
->openmin
= (r
|| a
->openmin
|| b
->openmin
);
581 c
->max
= muldiv32(a
->max
, b
->max
, k
, &r
);
586 c
->openmax
= (a
->openmax
|| b
->openmax
);
591 * snd_interval_mulkdiv - refine the interval value
593 * @k: dividend 2 (as integer)
599 * Returns non-zero if the value is changed, zero if not changed.
601 void snd_interval_mulkdiv(const snd_interval_t
*a
, unsigned int k
,
602 const snd_interval_t
*b
, snd_interval_t
*c
)
605 if (a
->empty
|| b
->empty
) {
606 snd_interval_none(c
);
610 c
->min
= muldiv32(a
->min
, k
, b
->max
, &r
);
611 c
->openmin
= (r
|| a
->openmin
|| b
->openmax
);
613 c
->max
= muldiv32(a
->max
, k
, b
->min
, &r
);
618 c
->openmax
= (a
->openmax
|| b
->openmin
);
631 * snd_interval_ratnum - refine the interval value
632 * @i: interval to refine
633 * @rats_count: number of ratnum_t
634 * @rats: ratnum_t array
635 * @nump: pointer to store the resultant numerator
636 * @denp: pointer to store the resultant denominator
638 * Returns non-zero if the value is changed, zero if not changed.
640 int snd_interval_ratnum(snd_interval_t
*i
,
641 unsigned int rats_count
, ratnum_t
*rats
,
642 unsigned int *nump
, unsigned int *denp
)
644 unsigned int best_num
, best_diff
, best_den
;
649 best_num
= best_den
= best_diff
= 0;
650 for (k
= 0; k
< rats_count
; ++k
) {
651 unsigned int num
= rats
[k
].num
;
653 unsigned int q
= i
->min
;
657 den
= div_down(num
, q
);
658 if (den
< rats
[k
].den_min
)
660 if (den
> rats
[k
].den_max
)
661 den
= rats
[k
].den_max
;
664 r
= (den
- rats
[k
].den_min
) % rats
[k
].den_step
;
668 diff
= num
- q
* den
;
670 diff
* best_den
< best_diff
* den
) {
680 t
.min
= div_down(best_num
, best_den
);
681 t
.openmin
= !!(best_num
% best_den
);
683 best_num
= best_den
= best_diff
= 0;
684 for (k
= 0; k
< rats_count
; ++k
) {
685 unsigned int num
= rats
[k
].num
;
687 unsigned int q
= i
->max
;
693 den
= div_up(num
, q
);
694 if (den
> rats
[k
].den_max
)
696 if (den
< rats
[k
].den_min
)
697 den
= rats
[k
].den_min
;
700 r
= (den
- rats
[k
].den_min
) % rats
[k
].den_step
;
702 den
+= rats
[k
].den_step
- r
;
704 diff
= q
* den
- num
;
706 diff
* best_den
< best_diff
* den
) {
716 t
.max
= div_up(best_num
, best_den
);
717 t
.openmax
= !!(best_num
% best_den
);
719 err
= snd_interval_refine(i
, &t
);
723 if (snd_interval_single(i
)) {
733 * snd_interval_ratden - refine the interval value
734 * @i: interval to refine
735 * @rats_count: number of ratden_t
736 * @rats: ratden_t array
737 * @nump: pointer to store the resultant numerator
738 * @denp: pointer to store the resultant denominator
740 * Returns non-zero if the value is changed, zero if not changed.
742 static int snd_interval_ratden(snd_interval_t
*i
,
743 unsigned int rats_count
, ratden_t
*rats
,
744 unsigned int *nump
, unsigned int *denp
)
746 unsigned int best_num
, best_diff
, best_den
;
751 best_num
= best_den
= best_diff
= 0;
752 for (k
= 0; k
< rats_count
; ++k
) {
754 unsigned int den
= rats
[k
].den
;
755 unsigned int q
= i
->min
;
758 if (num
> rats
[k
].num_max
)
760 if (num
< rats
[k
].num_min
)
761 num
= rats
[k
].num_max
;
764 r
= (num
- rats
[k
].num_min
) % rats
[k
].num_step
;
766 num
+= rats
[k
].num_step
- r
;
768 diff
= num
- q
* den
;
770 diff
* best_den
< best_diff
* den
) {
780 t
.min
= div_down(best_num
, best_den
);
781 t
.openmin
= !!(best_num
% best_den
);
783 best_num
= best_den
= best_diff
= 0;
784 for (k
= 0; k
< rats_count
; ++k
) {
786 unsigned int den
= rats
[k
].den
;
787 unsigned int q
= i
->max
;
790 if (num
< rats
[k
].num_min
)
792 if (num
> rats
[k
].num_max
)
793 num
= rats
[k
].num_max
;
796 r
= (num
- rats
[k
].num_min
) % rats
[k
].num_step
;
800 diff
= q
* den
- num
;
802 diff
* best_den
< best_diff
* den
) {
812 t
.max
= div_up(best_num
, best_den
);
813 t
.openmax
= !!(best_num
% best_den
);
815 err
= snd_interval_refine(i
, &t
);
819 if (snd_interval_single(i
)) {
829 * snd_interval_list - refine the interval value from the list
830 * @i: the interval value to refine
831 * @count: the number of elements in the list
832 * @list: the value list
833 * @mask: the bit-mask to evaluate
835 * Refines the interval value from the list.
836 * When mask is non-zero, only the elements corresponding to bit 1 are
839 * Returns non-zero if the value is changed, zero if not changed.
841 int snd_interval_list(snd_interval_t
*i
, unsigned int count
, unsigned int *list
, unsigned int mask
)
845 for (k
= 0; k
< count
; k
++) {
846 if (mask
&& !(mask
& (1 << k
)))
848 if (i
->min
== list
[k
] && !i
->openmin
)
850 if (i
->min
< list
[k
]) {
860 for (k
= count
; k
-- > 0;) {
861 if (mask
&& !(mask
& (1 << k
)))
863 if (i
->max
== list
[k
] && !i
->openmax
)
865 if (i
->max
> list
[k
]) {
875 if (snd_interval_checkempty(i
)) {
882 static int snd_interval_step(snd_interval_t
*i
, unsigned int min
, unsigned int step
)
886 n
= (i
->min
- min
) % step
;
887 if (n
!= 0 || i
->openmin
) {
891 n
= (i
->max
- min
) % step
;
892 if (n
!= 0 || i
->openmax
) {
896 if (snd_interval_checkempty(i
)) {
903 /* Info constraints helpers */
906 * snd_pcm_hw_rule_add - add the hw-constraint rule
907 * @runtime: the pcm runtime instance
908 * @cond: condition bits
909 * @var: the variable to evaluate
910 * @func: the evaluation function
911 * @private: the private data pointer passed to function
912 * @dep: the dependent variables
914 * Returns zero if successful, or a negative error code on failure.
916 int snd_pcm_hw_rule_add(snd_pcm_runtime_t
*runtime
, unsigned int cond
,
918 snd_pcm_hw_rule_func_t func
, void *private,
921 snd_pcm_hw_constraints_t
*constrs
= &runtime
->hw_constraints
;
922 snd_pcm_hw_rule_t
*c
;
926 if (constrs
->rules_num
>= constrs
->rules_all
) {
927 snd_pcm_hw_rule_t
*new;
928 unsigned int new_rules
= constrs
->rules_all
+ 16;
929 new = kcalloc(new_rules
, sizeof(*c
), GFP_KERNEL
);
932 if (constrs
->rules
) {
933 memcpy(new, constrs
->rules
,
934 constrs
->rules_num
* sizeof(*c
));
935 kfree(constrs
->rules
);
937 constrs
->rules
= new;
938 constrs
->rules_all
= new_rules
;
940 c
= &constrs
->rules
[constrs
->rules_num
];
944 c
->private = private;
947 snd_assert(k
< ARRAY_SIZE(c
->deps
), return -EINVAL
);
951 dep
= va_arg(args
, int);
953 constrs
->rules_num
++;
959 * snd_pcm_hw_constraint_mask
960 * @runtime: PCM runtime instance
961 * @var: hw_params variable to apply the mask
962 * @mask: the bitmap mask
964 * Apply the constraint of the given bitmap mask to a mask parameter.
966 int snd_pcm_hw_constraint_mask(snd_pcm_runtime_t
*runtime
, snd_pcm_hw_param_t var
,
969 snd_pcm_hw_constraints_t
*constrs
= &runtime
->hw_constraints
;
970 snd_mask_t
*maskp
= constrs_mask(constrs
, var
);
971 *maskp
->bits
&= mask
;
972 memset(maskp
->bits
+ 1, 0, (SNDRV_MASK_MAX
-32) / 8); /* clear rest */
973 if (*maskp
->bits
== 0)
979 * snd_pcm_hw_constraint_mask64
980 * @runtime: PCM runtime instance
981 * @var: hw_params variable to apply the mask
982 * @mask: the 64bit bitmap mask
984 * Apply the constraint of the given bitmap mask to a mask parameter.
986 int snd_pcm_hw_constraint_mask64(snd_pcm_runtime_t
*runtime
, snd_pcm_hw_param_t var
,
989 snd_pcm_hw_constraints_t
*constrs
= &runtime
->hw_constraints
;
990 snd_mask_t
*maskp
= constrs_mask(constrs
, var
);
991 maskp
->bits
[0] &= (u_int32_t
)mask
;
992 maskp
->bits
[1] &= (u_int32_t
)(mask
>> 32);
993 memset(maskp
->bits
+ 2, 0, (SNDRV_MASK_MAX
-64) / 8); /* clear rest */
994 if (! maskp
->bits
[0] && ! maskp
->bits
[1])
1000 * snd_pcm_hw_constraint_integer
1001 * @runtime: PCM runtime instance
1002 * @var: hw_params variable to apply the integer constraint
1004 * Apply the constraint of integer to an interval parameter.
1006 int snd_pcm_hw_constraint_integer(snd_pcm_runtime_t
*runtime
, snd_pcm_hw_param_t var
)
1008 snd_pcm_hw_constraints_t
*constrs
= &runtime
->hw_constraints
;
1009 return snd_interval_setinteger(constrs_interval(constrs
, var
));
1013 * snd_pcm_hw_constraint_minmax
1014 * @runtime: PCM runtime instance
1015 * @var: hw_params variable to apply the range
1016 * @min: the minimal value
1017 * @max: the maximal value
1019 * Apply the min/max range constraint to an interval parameter.
1021 int snd_pcm_hw_constraint_minmax(snd_pcm_runtime_t
*runtime
, snd_pcm_hw_param_t var
,
1022 unsigned int min
, unsigned int max
)
1024 snd_pcm_hw_constraints_t
*constrs
= &runtime
->hw_constraints
;
1028 t
.openmin
= t
.openmax
= 0;
1030 return snd_interval_refine(constrs_interval(constrs
, var
), &t
);
1033 static int snd_pcm_hw_rule_list(snd_pcm_hw_params_t
*params
,
1034 snd_pcm_hw_rule_t
*rule
)
1036 snd_pcm_hw_constraint_list_t
*list
= rule
->private;
1037 return snd_interval_list(hw_param_interval(params
, rule
->var
), list
->count
, list
->list
, list
->mask
);
1042 * snd_pcm_hw_constraint_list
1043 * @runtime: PCM runtime instance
1044 * @cond: condition bits
1045 * @var: hw_params variable to apply the list constraint
1048 * Apply the list of constraints to an interval parameter.
1050 int snd_pcm_hw_constraint_list(snd_pcm_runtime_t
*runtime
,
1052 snd_pcm_hw_param_t var
,
1053 snd_pcm_hw_constraint_list_t
*l
)
1055 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1056 snd_pcm_hw_rule_list
, l
,
1060 static int snd_pcm_hw_rule_ratnums(snd_pcm_hw_params_t
*params
,
1061 snd_pcm_hw_rule_t
*rule
)
1063 snd_pcm_hw_constraint_ratnums_t
*r
= rule
->private;
1064 unsigned int num
= 0, den
= 0;
1066 err
= snd_interval_ratnum(hw_param_interval(params
, rule
->var
),
1067 r
->nrats
, r
->rats
, &num
, &den
);
1068 if (err
>= 0 && den
&& rule
->var
== SNDRV_PCM_HW_PARAM_RATE
) {
1069 params
->rate_num
= num
;
1070 params
->rate_den
= den
;
1076 * snd_pcm_hw_constraint_ratnums
1077 * @runtime: PCM runtime instance
1078 * @cond: condition bits
1079 * @var: hw_params variable to apply the ratnums constraint
1080 * @r: ratnums_t constriants
1082 int snd_pcm_hw_constraint_ratnums(snd_pcm_runtime_t
*runtime
,
1084 snd_pcm_hw_param_t var
,
1085 snd_pcm_hw_constraint_ratnums_t
*r
)
1087 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1088 snd_pcm_hw_rule_ratnums
, r
,
1092 static int snd_pcm_hw_rule_ratdens(snd_pcm_hw_params_t
*params
,
1093 snd_pcm_hw_rule_t
*rule
)
1095 snd_pcm_hw_constraint_ratdens_t
*r
= rule
->private;
1096 unsigned int num
= 0, den
= 0;
1097 int err
= snd_interval_ratden(hw_param_interval(params
, rule
->var
),
1098 r
->nrats
, r
->rats
, &num
, &den
);
1099 if (err
>= 0 && den
&& rule
->var
== SNDRV_PCM_HW_PARAM_RATE
) {
1100 params
->rate_num
= num
;
1101 params
->rate_den
= den
;
1107 * snd_pcm_hw_constraint_ratdens
1108 * @runtime: PCM runtime instance
1109 * @cond: condition bits
1110 * @var: hw_params variable to apply the ratdens constraint
1111 * @r: ratdens_t constriants
1113 int snd_pcm_hw_constraint_ratdens(snd_pcm_runtime_t
*runtime
,
1115 snd_pcm_hw_param_t var
,
1116 snd_pcm_hw_constraint_ratdens_t
*r
)
1118 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1119 snd_pcm_hw_rule_ratdens
, r
,
1123 static int snd_pcm_hw_rule_msbits(snd_pcm_hw_params_t
*params
,
1124 snd_pcm_hw_rule_t
*rule
)
1126 unsigned int l
= (unsigned long) rule
->private;
1127 int width
= l
& 0xffff;
1128 unsigned int msbits
= l
>> 16;
1129 snd_interval_t
*i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
1130 if (snd_interval_single(i
) && snd_interval_value(i
) == width
)
1131 params
->msbits
= msbits
;
1136 * snd_pcm_hw_constraint_msbits
1137 * @runtime: PCM runtime instance
1138 * @cond: condition bits
1139 * @width: sample bits width
1140 * @msbits: msbits width
1142 int snd_pcm_hw_constraint_msbits(snd_pcm_runtime_t
*runtime
,
1145 unsigned int msbits
)
1147 unsigned long l
= (msbits
<< 16) | width
;
1148 return snd_pcm_hw_rule_add(runtime
, cond
, -1,
1149 snd_pcm_hw_rule_msbits
,
1151 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1154 static int snd_pcm_hw_rule_step(snd_pcm_hw_params_t
*params
,
1155 snd_pcm_hw_rule_t
*rule
)
1157 unsigned long step
= (unsigned long) rule
->private;
1158 return snd_interval_step(hw_param_interval(params
, rule
->var
), 0, step
);
1162 * snd_pcm_hw_constraint_step
1163 * @runtime: PCM runtime instance
1164 * @cond: condition bits
1165 * @var: hw_params variable to apply the step constraint
1168 int snd_pcm_hw_constraint_step(snd_pcm_runtime_t
*runtime
,
1170 snd_pcm_hw_param_t var
,
1173 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1174 snd_pcm_hw_rule_step
, (void *) step
,
1178 static int snd_pcm_hw_rule_pow2(snd_pcm_hw_params_t
*params
, snd_pcm_hw_rule_t
*rule
)
1180 static int pow2_sizes
[] = {
1181 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1182 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1183 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1184 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1186 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1187 ARRAY_SIZE(pow2_sizes
), pow2_sizes
, 0);
1191 * snd_pcm_hw_constraint_pow2
1192 * @runtime: PCM runtime instance
1193 * @cond: condition bits
1194 * @var: hw_params variable to apply the power-of-2 constraint
1196 int snd_pcm_hw_constraint_pow2(snd_pcm_runtime_t
*runtime
,
1198 snd_pcm_hw_param_t var
)
1200 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1201 snd_pcm_hw_rule_pow2
, NULL
,
1205 /* To use the same code we have in alsa-lib */
1206 #define snd_pcm_t snd_pcm_substream_t
1207 #define assert(i) snd_assert((i), return -EINVAL)
1209 #define INT_MIN ((int)((unsigned int)INT_MAX+1))
1212 static void _snd_pcm_hw_param_any(snd_pcm_hw_params_t
*params
,
1213 snd_pcm_hw_param_t var
)
1215 if (hw_is_mask(var
)) {
1216 snd_mask_any(hw_param_mask(params
, var
));
1217 params
->cmask
|= 1 << var
;
1218 params
->rmask
|= 1 << var
;
1221 if (hw_is_interval(var
)) {
1222 snd_interval_any(hw_param_interval(params
, var
));
1223 params
->cmask
|= 1 << var
;
1224 params
->rmask
|= 1 << var
;
1232 * snd_pcm_hw_param_any
1234 int snd_pcm_hw_param_any(snd_pcm_t
*pcm
, snd_pcm_hw_params_t
*params
,
1235 snd_pcm_hw_param_t var
)
1237 _snd_pcm_hw_param_any(params
, var
);
1238 return snd_pcm_hw_refine(pcm
, params
);
1242 void _snd_pcm_hw_params_any(snd_pcm_hw_params_t
*params
)
1245 memset(params
, 0, sizeof(*params
));
1246 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++)
1247 _snd_pcm_hw_param_any(params
, k
);
1248 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
1249 _snd_pcm_hw_param_any(params
, k
);
1255 * snd_pcm_hw_params_any
1257 * Fill PARAMS with full configuration space boundaries
1259 int snd_pcm_hw_params_any(snd_pcm_t
*pcm
, snd_pcm_hw_params_t
*params
)
1261 _snd_pcm_hw_params_any(params
);
1262 return snd_pcm_hw_refine(pcm
, params
);
1267 * snd_pcm_hw_param_value
1268 * @params: the hw_params instance
1269 * @var: parameter to retrieve
1270 * @dir: pointer to the direction (-1,0,1) or NULL
1272 * Return the value for field PAR if it's fixed in configuration space
1273 * defined by PARAMS. Return -EINVAL otherwise
1275 static int snd_pcm_hw_param_value(const snd_pcm_hw_params_t
*params
,
1276 snd_pcm_hw_param_t var
, int *dir
)
1278 if (hw_is_mask(var
)) {
1279 const snd_mask_t
*mask
= hw_param_mask_c(params
, var
);
1280 if (!snd_mask_single(mask
))
1284 return snd_mask_value(mask
);
1286 if (hw_is_interval(var
)) {
1287 const snd_interval_t
*i
= hw_param_interval_c(params
, var
);
1288 if (!snd_interval_single(i
))
1292 return snd_interval_value(i
);
1299 * snd_pcm_hw_param_value_min
1300 * @params: the hw_params instance
1301 * @var: parameter to retrieve
1302 * @dir: pointer to the direction (-1,0,1) or NULL
1304 * Return the minimum value for field PAR.
1306 unsigned int snd_pcm_hw_param_value_min(const snd_pcm_hw_params_t
*params
,
1307 snd_pcm_hw_param_t var
, int *dir
)
1309 if (hw_is_mask(var
)) {
1312 return snd_mask_min(hw_param_mask_c(params
, var
));
1314 if (hw_is_interval(var
)) {
1315 const snd_interval_t
*i
= hw_param_interval_c(params
, var
);
1318 return snd_interval_min(i
);
1325 * snd_pcm_hw_param_value_max
1326 * @params: the hw_params instance
1327 * @var: parameter to retrieve
1328 * @dir: pointer to the direction (-1,0,1) or NULL
1330 * Return the maximum value for field PAR.
1332 unsigned int snd_pcm_hw_param_value_max(const snd_pcm_hw_params_t
*params
,
1333 snd_pcm_hw_param_t var
, int *dir
)
1335 if (hw_is_mask(var
)) {
1338 return snd_mask_max(hw_param_mask_c(params
, var
));
1340 if (hw_is_interval(var
)) {
1341 const snd_interval_t
*i
= hw_param_interval_c(params
, var
);
1343 *dir
= - (int) i
->openmax
;
1344 return snd_interval_max(i
);
1350 void _snd_pcm_hw_param_setempty(snd_pcm_hw_params_t
*params
,
1351 snd_pcm_hw_param_t var
)
1353 if (hw_is_mask(var
)) {
1354 snd_mask_none(hw_param_mask(params
, var
));
1355 params
->cmask
|= 1 << var
;
1356 params
->rmask
|= 1 << var
;
1357 } else if (hw_is_interval(var
)) {
1358 snd_interval_none(hw_param_interval(params
, var
));
1359 params
->cmask
|= 1 << var
;
1360 params
->rmask
|= 1 << var
;
1366 int _snd_pcm_hw_param_setinteger(snd_pcm_hw_params_t
*params
,
1367 snd_pcm_hw_param_t var
)
1370 assert(hw_is_interval(var
));
1371 changed
= snd_interval_setinteger(hw_param_interval(params
, var
));
1373 params
->cmask
|= 1 << var
;
1374 params
->rmask
|= 1 << var
;
1381 * snd_pcm_hw_param_setinteger
1383 * Inside configuration space defined by PARAMS remove from PAR all
1384 * non integer values. Reduce configuration space accordingly.
1385 * Return -EINVAL if the configuration space is empty
1387 int snd_pcm_hw_param_setinteger(snd_pcm_t
*pcm
,
1388 snd_pcm_hw_params_t
*params
,
1389 snd_pcm_hw_param_t var
)
1391 int changed
= _snd_pcm_hw_param_setinteger(params
, var
);
1394 if (params
->rmask
) {
1395 int err
= snd_pcm_hw_refine(pcm
, params
);
1403 static int _snd_pcm_hw_param_first(snd_pcm_hw_params_t
*params
,
1404 snd_pcm_hw_param_t var
)
1407 if (hw_is_mask(var
))
1408 changed
= snd_mask_refine_first(hw_param_mask(params
, var
));
1409 else if (hw_is_interval(var
))
1410 changed
= snd_interval_refine_first(hw_param_interval(params
, var
));
1416 params
->cmask
|= 1 << var
;
1417 params
->rmask
|= 1 << var
;
1424 * snd_pcm_hw_param_first
1425 * @pcm: PCM instance
1426 * @params: the hw_params instance
1427 * @var: parameter to retrieve
1428 * @dir: pointer to the direction (-1,0,1) or NULL
1430 * Inside configuration space defined by PARAMS remove from PAR all
1431 * values > minimum. Reduce configuration space accordingly.
1432 * Return the minimum.
1434 static int snd_pcm_hw_param_first(snd_pcm_t
*pcm
,
1435 snd_pcm_hw_params_t
*params
,
1436 snd_pcm_hw_param_t var
, int *dir
)
1438 int changed
= _snd_pcm_hw_param_first(params
, var
);
1441 if (params
->rmask
) {
1442 int err
= snd_pcm_hw_refine(pcm
, params
);
1445 return snd_pcm_hw_param_value(params
, var
, dir
);
1448 static int _snd_pcm_hw_param_last(snd_pcm_hw_params_t
*params
,
1449 snd_pcm_hw_param_t var
)
1452 if (hw_is_mask(var
))
1453 changed
= snd_mask_refine_last(hw_param_mask(params
, var
));
1454 else if (hw_is_interval(var
))
1455 changed
= snd_interval_refine_last(hw_param_interval(params
, var
));
1461 params
->cmask
|= 1 << var
;
1462 params
->rmask
|= 1 << var
;
1469 * snd_pcm_hw_param_last
1470 * @pcm: PCM instance
1471 * @params: the hw_params instance
1472 * @var: parameter to retrieve
1473 * @dir: pointer to the direction (-1,0,1) or NULL
1475 * Inside configuration space defined by PARAMS remove from PAR all
1476 * values < maximum. Reduce configuration space accordingly.
1477 * Return the maximum.
1479 static int snd_pcm_hw_param_last(snd_pcm_t
*pcm
,
1480 snd_pcm_hw_params_t
*params
,
1481 snd_pcm_hw_param_t var
, int *dir
)
1483 int changed
= _snd_pcm_hw_param_last(params
, var
);
1486 if (params
->rmask
) {
1487 int err
= snd_pcm_hw_refine(pcm
, params
);
1490 return snd_pcm_hw_param_value(params
, var
, dir
);
1493 int _snd_pcm_hw_param_min(snd_pcm_hw_params_t
*params
,
1494 snd_pcm_hw_param_t var
, unsigned int val
, int dir
)
1501 } else if (dir
< 0) {
1508 if (hw_is_mask(var
))
1509 changed
= snd_mask_refine_min(hw_param_mask(params
, var
), val
+ !!open
);
1510 else if (hw_is_interval(var
))
1511 changed
= snd_interval_refine_min(hw_param_interval(params
, var
), val
, open
);
1517 params
->cmask
|= 1 << var
;
1518 params
->rmask
|= 1 << var
;
1524 * snd_pcm_hw_param_min
1525 * @pcm: PCM instance
1526 * @params: the hw_params instance
1527 * @var: parameter to retrieve
1528 * @val: minimal value
1529 * @dir: pointer to the direction (-1,0,1) or NULL
1531 * Inside configuration space defined by PARAMS remove from PAR all
1532 * values < VAL. Reduce configuration space accordingly.
1533 * Return new minimum or -EINVAL if the configuration space is empty
1535 static int snd_pcm_hw_param_min(snd_pcm_t
*pcm
, snd_pcm_hw_params_t
*params
,
1536 snd_pcm_hw_param_t var
, unsigned int val
,
1539 int changed
= _snd_pcm_hw_param_min(params
, var
, val
, dir
? *dir
: 0);
1542 if (params
->rmask
) {
1543 int err
= snd_pcm_hw_refine(pcm
, params
);
1547 return snd_pcm_hw_param_value_min(params
, var
, dir
);
1550 static int _snd_pcm_hw_param_max(snd_pcm_hw_params_t
*params
,
1551 snd_pcm_hw_param_t var
, unsigned int val
,
1559 } else if (dir
> 0) {
1564 if (hw_is_mask(var
)) {
1565 if (val
== 0 && open
) {
1566 snd_mask_none(hw_param_mask(params
, var
));
1569 changed
= snd_mask_refine_max(hw_param_mask(params
, var
), val
- !!open
);
1570 } else if (hw_is_interval(var
))
1571 changed
= snd_interval_refine_max(hw_param_interval(params
, var
), val
, open
);
1577 params
->cmask
|= 1 << var
;
1578 params
->rmask
|= 1 << var
;
1584 * snd_pcm_hw_param_max
1585 * @pcm: PCM instance
1586 * @params: the hw_params instance
1587 * @var: parameter to retrieve
1588 * @val: maximal value
1589 * @dir: pointer to the direction (-1,0,1) or NULL
1591 * Inside configuration space defined by PARAMS remove from PAR all
1592 * values >= VAL + 1. Reduce configuration space accordingly.
1593 * Return new maximum or -EINVAL if the configuration space is empty
1595 static int snd_pcm_hw_param_max(snd_pcm_t
*pcm
, snd_pcm_hw_params_t
*params
,
1596 snd_pcm_hw_param_t var
, unsigned int val
,
1599 int changed
= _snd_pcm_hw_param_max(params
, var
, val
, dir
? *dir
: 0);
1602 if (params
->rmask
) {
1603 int err
= snd_pcm_hw_refine(pcm
, params
);
1607 return snd_pcm_hw_param_value_max(params
, var
, dir
);
1610 int _snd_pcm_hw_param_set(snd_pcm_hw_params_t
*params
,
1611 snd_pcm_hw_param_t var
, unsigned int val
, int dir
)
1614 if (hw_is_mask(var
)) {
1615 snd_mask_t
*m
= hw_param_mask(params
, var
);
1616 if (val
== 0 && dir
< 0) {
1624 changed
= snd_mask_refine_set(hw_param_mask(params
, var
), val
);
1626 } else if (hw_is_interval(var
)) {
1627 snd_interval_t
*i
= hw_param_interval(params
, var
);
1628 if (val
== 0 && dir
< 0) {
1630 snd_interval_none(i
);
1631 } else if (dir
== 0)
1632 changed
= snd_interval_refine_set(i
, val
);
1646 changed
= snd_interval_refine(i
, &t
);
1653 params
->cmask
|= 1 << var
;
1654 params
->rmask
|= 1 << var
;
1660 * snd_pcm_hw_param_set
1661 * @pcm: PCM instance
1662 * @params: the hw_params instance
1663 * @var: parameter to retrieve
1664 * @val: value to set
1665 * @dir: pointer to the direction (-1,0,1) or NULL
1667 * Inside configuration space defined by PARAMS remove from PAR all
1668 * values != VAL. Reduce configuration space accordingly.
1669 * Return VAL or -EINVAL if the configuration space is empty
1671 int snd_pcm_hw_param_set(snd_pcm_t
*pcm
, snd_pcm_hw_params_t
*params
,
1672 snd_pcm_hw_param_t var
, unsigned int val
, int dir
)
1674 int changed
= _snd_pcm_hw_param_set(params
, var
, val
, dir
);
1677 if (params
->rmask
) {
1678 int err
= snd_pcm_hw_refine(pcm
, params
);
1682 return snd_pcm_hw_param_value(params
, var
, NULL
);
1685 static int _snd_pcm_hw_param_mask(snd_pcm_hw_params_t
*params
,
1686 snd_pcm_hw_param_t var
, const snd_mask_t
*val
)
1689 assert(hw_is_mask(var
));
1690 changed
= snd_mask_refine(hw_param_mask(params
, var
), val
);
1692 params
->cmask
|= 1 << var
;
1693 params
->rmask
|= 1 << var
;
1699 * snd_pcm_hw_param_mask
1700 * @pcm: PCM instance
1701 * @params: the hw_params instance
1702 * @var: parameter to retrieve
1703 * @val: mask to apply
1705 * Inside configuration space defined by PARAMS remove from PAR all values
1706 * not contained in MASK. Reduce configuration space accordingly.
1707 * This function can be called only for SNDRV_PCM_HW_PARAM_ACCESS,
1708 * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
1709 * Return 0 on success or -EINVAL
1710 * if the configuration space is empty
1712 int snd_pcm_hw_param_mask(snd_pcm_t
*pcm
, snd_pcm_hw_params_t
*params
,
1713 snd_pcm_hw_param_t var
, const snd_mask_t
*val
)
1715 int changed
= _snd_pcm_hw_param_mask(params
, var
, val
);
1718 if (params
->rmask
) {
1719 int err
= snd_pcm_hw_refine(pcm
, params
);
1726 static int boundary_sub(int a
, int adir
,
1730 adir
= adir
< 0 ? -1 : (adir
> 0 ? 1 : 0);
1731 bdir
= bdir
< 0 ? -1 : (bdir
> 0 ? 1 : 0);
1733 *cdir
= adir
- bdir
;
1735 assert(*c
> INT_MIN
);
1737 } else if (*cdir
== 2) {
1738 assert(*c
< INT_MAX
);
1744 static int boundary_lt(unsigned int a
, int adir
,
1745 unsigned int b
, int bdir
)
1747 assert(a
> 0 || adir
>= 0);
1748 assert(b
> 0 || bdir
>= 0);
1752 } else if (adir
> 0)
1757 } else if (bdir
> 0)
1759 return a
< b
|| (a
== b
&& adir
< bdir
);
1762 /* Return 1 if min is nearer to best than max */
1763 static int boundary_nearer(int min
, int mindir
,
1764 int best
, int bestdir
,
1765 int max
, int maxdir
)
1769 boundary_sub(best
, bestdir
, min
, mindir
, &dmin
, &dmindir
);
1770 boundary_sub(max
, maxdir
, best
, bestdir
, &dmax
, &dmaxdir
);
1771 return boundary_lt(dmin
, dmindir
, dmax
, dmaxdir
);
1775 * snd_pcm_hw_param_near
1776 * @pcm: PCM instance
1777 * @params: the hw_params instance
1778 * @var: parameter to retrieve
1779 * @best: value to set
1780 * @dir: pointer to the direction (-1,0,1) or NULL
1782 * Inside configuration space defined by PARAMS set PAR to the available value
1783 * nearest to VAL. Reduce configuration space accordingly.
1784 * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
1785 * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
1786 * Return the value found.
1788 int snd_pcm_hw_param_near(snd_pcm_t
*pcm
, snd_pcm_hw_params_t
*params
,
1789 snd_pcm_hw_param_t var
, unsigned int best
, int *dir
)
1791 snd_pcm_hw_params_t
*save
= NULL
;
1793 unsigned int saved_min
;
1797 int valdir
= dir
? *dir
: 0;
1802 mindir
= maxdir
= valdir
;
1805 else if (maxdir
== 0)
1811 save
= kmalloc(sizeof(*save
), GFP_KERNEL
);
1816 min
= snd_pcm_hw_param_min(pcm
, params
, var
, min
, &mindir
);
1818 snd_pcm_hw_params_t
*params1
;
1821 if ((unsigned int)min
== saved_min
&& mindir
== valdir
)
1823 params1
= kmalloc(sizeof(*params1
), GFP_KERNEL
);
1824 if (params1
== NULL
) {
1829 max
= snd_pcm_hw_param_max(pcm
, params1
, var
, max
, &maxdir
);
1834 if (boundary_nearer(max
, maxdir
, best
, valdir
, min
, mindir
)) {
1841 max
= snd_pcm_hw_param_max(pcm
, params
, var
, max
, &maxdir
);
1848 v
= snd_pcm_hw_param_last(pcm
, params
, var
, dir
);
1850 v
= snd_pcm_hw_param_first(pcm
, params
, var
, dir
);
1856 * snd_pcm_hw_param_choose
1857 * @pcm: PCM instance
1858 * @params: the hw_params instance
1860 * Choose one configuration from configuration space defined by PARAMS
1861 * The configuration chosen is that obtained fixing in this order:
1862 * first access, first format, first subformat, min channels,
1863 * min rate, min period time, max buffer size, min tick time
1865 int snd_pcm_hw_params_choose(snd_pcm_t
*pcm
, snd_pcm_hw_params_t
*params
)
1869 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_ACCESS
, NULL
);
1872 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_FORMAT
, NULL
);
1875 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, NULL
);
1878 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_CHANNELS
, NULL
);
1881 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_RATE
, NULL
);
1884 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, NULL
);
1887 err
= snd_pcm_hw_param_last(pcm
, params
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, NULL
);
1890 err
= snd_pcm_hw_param_first(pcm
, params
, SNDRV_PCM_HW_PARAM_TICK_TIME
, NULL
);
1899 static int snd_pcm_lib_ioctl_reset(snd_pcm_substream_t
*substream
,
1902 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1903 unsigned long flags
;
1904 snd_pcm_stream_lock_irqsave(substream
, flags
);
1905 if (snd_pcm_running(substream
) &&
1906 snd_pcm_update_hw_ptr(substream
) >= 0)
1907 runtime
->status
->hw_ptr
%= runtime
->buffer_size
;
1909 runtime
->status
->hw_ptr
= 0;
1910 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1914 static int snd_pcm_lib_ioctl_channel_info(snd_pcm_substream_t
*substream
,
1917 snd_pcm_channel_info_t
*info
= arg
;
1918 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1920 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
)) {
1924 width
= snd_pcm_format_physical_width(runtime
->format
);
1928 switch (runtime
->access
) {
1929 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
:
1930 case SNDRV_PCM_ACCESS_RW_INTERLEAVED
:
1931 info
->first
= info
->channel
* width
;
1932 info
->step
= runtime
->channels
* width
;
1934 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
:
1935 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
:
1937 size_t size
= runtime
->dma_bytes
/ runtime
->channels
;
1938 info
->first
= info
->channel
* size
* 8;
1950 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1951 * @substream: the pcm substream instance
1952 * @cmd: ioctl command
1953 * @arg: ioctl argument
1955 * Processes the generic ioctl commands for PCM.
1956 * Can be passed as the ioctl callback for PCM ops.
1958 * Returns zero if successful, or a negative error code on failure.
1960 int snd_pcm_lib_ioctl(snd_pcm_substream_t
*substream
,
1961 unsigned int cmd
, void *arg
)
1964 case SNDRV_PCM_IOCTL1_INFO
:
1966 case SNDRV_PCM_IOCTL1_RESET
:
1967 return snd_pcm_lib_ioctl_reset(substream
, arg
);
1968 case SNDRV_PCM_IOCTL1_CHANNEL_INFO
:
1969 return snd_pcm_lib_ioctl_channel_info(substream
, arg
);
1978 static void snd_pcm_system_tick_set(snd_pcm_substream_t
*substream
,
1979 unsigned long ticks
)
1981 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1983 del_timer(&runtime
->tick_timer
);
1985 ticks
+= (1000000 / HZ
) - 1;
1986 ticks
/= (1000000 / HZ
);
1987 mod_timer(&runtime
->tick_timer
, jiffies
+ ticks
);
1991 /* Temporary alias */
1992 void snd_pcm_tick_set(snd_pcm_substream_t
*substream
, unsigned long ticks
)
1994 snd_pcm_system_tick_set(substream
, ticks
);
1997 void snd_pcm_tick_prepare(snd_pcm_substream_t
*substream
)
1999 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2000 snd_pcm_uframes_t frames
= ULONG_MAX
;
2001 snd_pcm_uframes_t avail
, dist
;
2005 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
2006 if (runtime
->silence_size
>= runtime
->boundary
) {
2008 } else if (runtime
->silence_size
> 0 &&
2009 runtime
->silence_filled
< runtime
->buffer_size
) {
2010 snd_pcm_sframes_t noise_dist
;
2011 noise_dist
= snd_pcm_playback_hw_avail(runtime
) + runtime
->silence_filled
;
2012 snd_assert(noise_dist
<= (snd_pcm_sframes_t
)runtime
->silence_threshold
, );
2013 frames
= noise_dist
- runtime
->silence_threshold
;
2015 avail
= snd_pcm_playback_avail(runtime
);
2017 avail
= snd_pcm_capture_avail(runtime
);
2019 if (avail
< runtime
->control
->avail_min
) {
2020 snd_pcm_sframes_t n
= runtime
->control
->avail_min
- avail
;
2021 if (n
> 0 && frames
> (snd_pcm_uframes_t
)n
)
2024 if (avail
< runtime
->buffer_size
) {
2025 snd_pcm_sframes_t n
= runtime
->buffer_size
- avail
;
2026 if (n
> 0 && frames
> (snd_pcm_uframes_t
)n
)
2029 if (frames
== ULONG_MAX
) {
2030 snd_pcm_tick_set(substream
, 0);
2033 dist
= runtime
->status
->hw_ptr
- runtime
->hw_ptr_base
;
2034 /* Distance to next interrupt */
2035 dist
= runtime
->period_size
- dist
% runtime
->period_size
;
2036 if (dist
<= frames
) {
2037 snd_pcm_tick_set(substream
, 0);
2040 /* the base time is us */
2043 div64_32(&n
, runtime
->tick_time
* runtime
->rate
, &r
);
2044 ticks
= n
+ (r
> 0 ? 1 : 0);
2045 if (ticks
< runtime
->sleep_min
)
2046 ticks
= runtime
->sleep_min
;
2047 snd_pcm_tick_set(substream
, (unsigned long) ticks
);
2050 void snd_pcm_tick_elapsed(snd_pcm_substream_t
*substream
)
2052 snd_pcm_runtime_t
*runtime
;
2053 unsigned long flags
;
2055 snd_assert(substream
!= NULL
, return);
2056 runtime
= substream
->runtime
;
2057 snd_assert(runtime
!= NULL
, return);
2059 snd_pcm_stream_lock_irqsave(substream
, flags
);
2060 if (!snd_pcm_running(substream
) ||
2061 snd_pcm_update_hw_ptr(substream
) < 0)
2063 if (runtime
->sleep_min
)
2064 snd_pcm_tick_prepare(substream
);
2066 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
2070 * snd_pcm_period_elapsed - update the pcm status for the next period
2071 * @substream: the pcm substream instance
2073 * This function is called from the interrupt handler when the
2074 * PCM has processed the period size. It will update the current
2075 * pointer, set up the tick, wake up sleepers, etc.
2077 * Even if more than one periods have elapsed since the last call, you
2078 * have to call this only once.
2080 void snd_pcm_period_elapsed(snd_pcm_substream_t
*substream
)
2082 snd_pcm_runtime_t
*runtime
;
2083 unsigned long flags
;
2085 snd_assert(substream
!= NULL
, return);
2086 runtime
= substream
->runtime
;
2087 snd_assert(runtime
!= NULL
, return);
2089 if (runtime
->transfer_ack_begin
)
2090 runtime
->transfer_ack_begin(substream
);
2092 snd_pcm_stream_lock_irqsave(substream
, flags
);
2093 if (!snd_pcm_running(substream
) ||
2094 snd_pcm_update_hw_ptr_interrupt(substream
) < 0)
2097 if (substream
->timer_running
)
2098 snd_timer_interrupt(substream
->timer
, 1);
2099 if (runtime
->sleep_min
)
2100 snd_pcm_tick_prepare(substream
);
2102 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
2103 if (runtime
->transfer_ack_end
)
2104 runtime
->transfer_ack_end(substream
);
2105 kill_fasync(&runtime
->fasync
, SIGIO
, POLL_IN
);
2108 static int snd_pcm_lib_write_transfer(snd_pcm_substream_t
*substream
,
2110 unsigned long data
, unsigned int off
,
2111 snd_pcm_uframes_t frames
)
2113 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2115 char __user
*buf
= (char __user
*) data
+ frames_to_bytes(runtime
, off
);
2116 if (substream
->ops
->copy
) {
2117 if ((err
= substream
->ops
->copy(substream
, -1, hwoff
, buf
, frames
)) < 0)
2120 char *hwbuf
= runtime
->dma_area
+ frames_to_bytes(runtime
, hwoff
);
2121 snd_assert(runtime
->dma_area
, return -EFAULT
);
2122 if (copy_from_user(hwbuf
, buf
, frames_to_bytes(runtime
, frames
)))
2128 typedef int (*transfer_f
)(snd_pcm_substream_t
*substream
, unsigned int hwoff
,
2129 unsigned long data
, unsigned int off
,
2130 snd_pcm_uframes_t size
);
2132 static snd_pcm_sframes_t
snd_pcm_lib_write1(snd_pcm_substream_t
*substream
,
2134 snd_pcm_uframes_t size
,
2136 transfer_f transfer
)
2138 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2139 snd_pcm_uframes_t xfer
= 0;
2140 snd_pcm_uframes_t offset
= 0;
2145 if (size
> runtime
->xfer_align
)
2146 size
-= size
% runtime
->xfer_align
;
2148 snd_pcm_stream_lock_irq(substream
);
2149 switch (runtime
->status
->state
) {
2150 case SNDRV_PCM_STATE_PREPARED
:
2151 case SNDRV_PCM_STATE_RUNNING
:
2152 case SNDRV_PCM_STATE_PAUSED
:
2154 case SNDRV_PCM_STATE_XRUN
:
2157 case SNDRV_PCM_STATE_SUSPENDED
:
2166 snd_pcm_uframes_t frames
, appl_ptr
, appl_ofs
;
2167 snd_pcm_uframes_t avail
;
2168 snd_pcm_uframes_t cont
;
2169 if (runtime
->sleep_min
== 0 && runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
2170 snd_pcm_update_hw_ptr(substream
);
2171 avail
= snd_pcm_playback_avail(runtime
);
2172 if (((avail
< runtime
->control
->avail_min
&& size
> avail
) ||
2173 (size
>= runtime
->xfer_align
&& avail
< runtime
->xfer_align
))) {
2175 enum { READY
, SIGNALED
, ERROR
, SUSPENDED
, EXPIRED
, DROPPED
} state
;
2183 init_waitqueue_entry(&wait
, current
);
2184 add_wait_queue(&runtime
->sleep
, &wait
);
2186 if (signal_pending(current
)) {
2190 set_current_state(TASK_INTERRUPTIBLE
);
2191 snd_pcm_stream_unlock_irq(substream
);
2192 tout
= schedule_timeout(10 * HZ
);
2193 snd_pcm_stream_lock_irq(substream
);
2195 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
&&
2196 runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
) {
2197 state
= runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
? SUSPENDED
: EXPIRED
;
2201 switch (runtime
->status
->state
) {
2202 case SNDRV_PCM_STATE_XRUN
:
2203 case SNDRV_PCM_STATE_DRAINING
:
2206 case SNDRV_PCM_STATE_SUSPENDED
:
2209 case SNDRV_PCM_STATE_SETUP
:
2215 avail
= snd_pcm_playback_avail(runtime
);
2216 if (avail
>= runtime
->control
->avail_min
) {
2222 remove_wait_queue(&runtime
->sleep
, &wait
);
2235 snd_printd("playback write error (DMA or IRQ trouble?)\n");
2245 if (avail
> runtime
->xfer_align
)
2246 avail
-= avail
% runtime
->xfer_align
;
2247 frames
= size
> avail
? avail
: size
;
2248 cont
= runtime
->buffer_size
- runtime
->control
->appl_ptr
% runtime
->buffer_size
;
2251 snd_assert(frames
!= 0, snd_pcm_stream_unlock_irq(substream
); return -EINVAL
);
2252 appl_ptr
= runtime
->control
->appl_ptr
;
2253 appl_ofs
= appl_ptr
% runtime
->buffer_size
;
2254 snd_pcm_stream_unlock_irq(substream
);
2255 if ((err
= transfer(substream
, appl_ofs
, data
, offset
, frames
)) < 0)
2257 snd_pcm_stream_lock_irq(substream
);
2258 switch (runtime
->status
->state
) {
2259 case SNDRV_PCM_STATE_XRUN
:
2262 case SNDRV_PCM_STATE_SUSPENDED
:
2269 if (appl_ptr
>= runtime
->boundary
)
2270 appl_ptr
-= runtime
->boundary
;
2271 runtime
->control
->appl_ptr
= appl_ptr
;
2272 if (substream
->ops
->ack
)
2273 substream
->ops
->ack(substream
);
2278 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
&&
2279 snd_pcm_playback_hw_avail(runtime
) >= (snd_pcm_sframes_t
)runtime
->start_threshold
) {
2280 err
= snd_pcm_start(substream
);
2284 if (runtime
->sleep_min
&&
2285 runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
2286 snd_pcm_tick_prepare(substream
);
2289 snd_pcm_stream_unlock_irq(substream
);
2291 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: err
;
2294 snd_pcm_sframes_t
snd_pcm_lib_write(snd_pcm_substream_t
*substream
, const void __user
*buf
, snd_pcm_uframes_t size
)
2296 snd_pcm_runtime_t
*runtime
;
2299 snd_assert(substream
!= NULL
, return -ENXIO
);
2300 runtime
= substream
->runtime
;
2301 snd_assert(runtime
!= NULL
, return -ENXIO
);
2302 snd_assert(substream
->ops
->copy
!= NULL
|| runtime
->dma_area
!= NULL
, return -EINVAL
);
2303 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2306 snd_assert(substream
->ffile
!= NULL
, return -ENXIO
);
2307 nonblock
= !!(substream
->ffile
->f_flags
& O_NONBLOCK
);
2308 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2309 if (substream
->oss
.oss
) {
2310 snd_pcm_oss_setup_t
*setup
= substream
->oss
.setup
;
2311 if (setup
!= NULL
) {
2312 if (setup
->nonblock
)
2314 else if (setup
->block
)
2320 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_INTERLEAVED
&&
2321 runtime
->channels
> 1)
2323 return snd_pcm_lib_write1(substream
, (unsigned long)buf
, size
, nonblock
,
2324 snd_pcm_lib_write_transfer
);
2327 static int snd_pcm_lib_writev_transfer(snd_pcm_substream_t
*substream
,
2329 unsigned long data
, unsigned int off
,
2330 snd_pcm_uframes_t frames
)
2332 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2334 void __user
**bufs
= (void __user
**)data
;
2335 int channels
= runtime
->channels
;
2337 if (substream
->ops
->copy
) {
2338 snd_assert(substream
->ops
->silence
!= NULL
, return -EINVAL
);
2339 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
2340 if (*bufs
== NULL
) {
2341 if ((err
= substream
->ops
->silence(substream
, c
, hwoff
, frames
)) < 0)
2344 char __user
*buf
= *bufs
+ samples_to_bytes(runtime
, off
);
2345 if ((err
= substream
->ops
->copy(substream
, c
, hwoff
, buf
, frames
)) < 0)
2350 /* default transfer behaviour */
2351 size_t dma_csize
= runtime
->dma_bytes
/ channels
;
2352 snd_assert(runtime
->dma_area
, return -EFAULT
);
2353 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
2354 char *hwbuf
= runtime
->dma_area
+ (c
* dma_csize
) + samples_to_bytes(runtime
, hwoff
);
2355 if (*bufs
== NULL
) {
2356 snd_pcm_format_set_silence(runtime
->format
, hwbuf
, frames
);
2358 char __user
*buf
= *bufs
+ samples_to_bytes(runtime
, off
);
2359 if (copy_from_user(hwbuf
, buf
, samples_to_bytes(runtime
, frames
)))
2367 snd_pcm_sframes_t
snd_pcm_lib_writev(snd_pcm_substream_t
*substream
,
2369 snd_pcm_uframes_t frames
)
2371 snd_pcm_runtime_t
*runtime
;
2374 snd_assert(substream
!= NULL
, return -ENXIO
);
2375 runtime
= substream
->runtime
;
2376 snd_assert(runtime
!= NULL
, return -ENXIO
);
2377 snd_assert(substream
->ops
->copy
!= NULL
|| runtime
->dma_area
!= NULL
, return -EINVAL
);
2378 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2381 snd_assert(substream
->ffile
!= NULL
, return -ENXIO
);
2382 nonblock
= !!(substream
->ffile
->f_flags
& O_NONBLOCK
);
2383 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2384 if (substream
->oss
.oss
) {
2385 snd_pcm_oss_setup_t
*setup
= substream
->oss
.setup
;
2386 if (setup
!= NULL
) {
2387 if (setup
->nonblock
)
2389 else if (setup
->block
)
2395 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
2397 return snd_pcm_lib_write1(substream
, (unsigned long)bufs
, frames
,
2398 nonblock
, snd_pcm_lib_writev_transfer
);
2401 static int snd_pcm_lib_read_transfer(snd_pcm_substream_t
*substream
,
2403 unsigned long data
, unsigned int off
,
2404 snd_pcm_uframes_t frames
)
2406 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2408 char __user
*buf
= (char __user
*) data
+ frames_to_bytes(runtime
, off
);
2409 if (substream
->ops
->copy
) {
2410 if ((err
= substream
->ops
->copy(substream
, -1, hwoff
, buf
, frames
)) < 0)
2413 char *hwbuf
= runtime
->dma_area
+ frames_to_bytes(runtime
, hwoff
);
2414 snd_assert(runtime
->dma_area
, return -EFAULT
);
2415 if (copy_to_user(buf
, hwbuf
, frames_to_bytes(runtime
, frames
)))
2421 static snd_pcm_sframes_t
snd_pcm_lib_read1(snd_pcm_substream_t
*substream
,
2423 snd_pcm_uframes_t size
,
2425 transfer_f transfer
)
2427 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2428 snd_pcm_uframes_t xfer
= 0;
2429 snd_pcm_uframes_t offset
= 0;
2434 if (size
> runtime
->xfer_align
)
2435 size
-= size
% runtime
->xfer_align
;
2437 snd_pcm_stream_lock_irq(substream
);
2438 switch (runtime
->status
->state
) {
2439 case SNDRV_PCM_STATE_PREPARED
:
2440 if (size
>= runtime
->start_threshold
) {
2441 err
= snd_pcm_start(substream
);
2446 case SNDRV_PCM_STATE_DRAINING
:
2447 case SNDRV_PCM_STATE_RUNNING
:
2448 case SNDRV_PCM_STATE_PAUSED
:
2450 case SNDRV_PCM_STATE_XRUN
:
2453 case SNDRV_PCM_STATE_SUSPENDED
:
2462 snd_pcm_uframes_t frames
, appl_ptr
, appl_ofs
;
2463 snd_pcm_uframes_t avail
;
2464 snd_pcm_uframes_t cont
;
2465 if (runtime
->sleep_min
== 0 && runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
2466 snd_pcm_update_hw_ptr(substream
);
2468 avail
= snd_pcm_capture_avail(runtime
);
2469 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
2470 if (avail
< runtime
->xfer_align
) {
2474 } else if ((avail
< runtime
->control
->avail_min
&& size
> avail
) ||
2475 (size
>= runtime
->xfer_align
&& avail
< runtime
->xfer_align
)) {
2477 enum { READY
, SIGNALED
, ERROR
, SUSPENDED
, EXPIRED
, DROPPED
} state
;
2485 init_waitqueue_entry(&wait
, current
);
2486 add_wait_queue(&runtime
->sleep
, &wait
);
2488 if (signal_pending(current
)) {
2492 set_current_state(TASK_INTERRUPTIBLE
);
2493 snd_pcm_stream_unlock_irq(substream
);
2494 tout
= schedule_timeout(10 * HZ
);
2495 snd_pcm_stream_lock_irq(substream
);
2497 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
&&
2498 runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
) {
2499 state
= runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
? SUSPENDED
: EXPIRED
;
2503 switch (runtime
->status
->state
) {
2504 case SNDRV_PCM_STATE_XRUN
:
2507 case SNDRV_PCM_STATE_SUSPENDED
:
2510 case SNDRV_PCM_STATE_DRAINING
:
2512 case SNDRV_PCM_STATE_SETUP
:
2518 avail
= snd_pcm_capture_avail(runtime
);
2519 if (avail
>= runtime
->control
->avail_min
) {
2525 remove_wait_queue(&runtime
->sleep
, &wait
);
2538 snd_printd("capture read error (DMA or IRQ trouble?)\n");
2548 if (avail
> runtime
->xfer_align
)
2549 avail
-= avail
% runtime
->xfer_align
;
2550 frames
= size
> avail
? avail
: size
;
2551 cont
= runtime
->buffer_size
- runtime
->control
->appl_ptr
% runtime
->buffer_size
;
2554 snd_assert(frames
!= 0, snd_pcm_stream_unlock_irq(substream
); return -EINVAL
);
2555 appl_ptr
= runtime
->control
->appl_ptr
;
2556 appl_ofs
= appl_ptr
% runtime
->buffer_size
;
2557 snd_pcm_stream_unlock_irq(substream
);
2558 if ((err
= transfer(substream
, appl_ofs
, data
, offset
, frames
)) < 0)
2560 snd_pcm_stream_lock_irq(substream
);
2561 switch (runtime
->status
->state
) {
2562 case SNDRV_PCM_STATE_XRUN
:
2565 case SNDRV_PCM_STATE_SUSPENDED
:
2572 if (appl_ptr
>= runtime
->boundary
)
2573 appl_ptr
-= runtime
->boundary
;
2574 runtime
->control
->appl_ptr
= appl_ptr
;
2575 if (substream
->ops
->ack
)
2576 substream
->ops
->ack(substream
);
2581 if (runtime
->sleep_min
&&
2582 runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
2583 snd_pcm_tick_prepare(substream
);
2586 snd_pcm_stream_unlock_irq(substream
);
2588 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: err
;
2591 snd_pcm_sframes_t
snd_pcm_lib_read(snd_pcm_substream_t
*substream
, void __user
*buf
, snd_pcm_uframes_t size
)
2593 snd_pcm_runtime_t
*runtime
;
2596 snd_assert(substream
!= NULL
, return -ENXIO
);
2597 runtime
= substream
->runtime
;
2598 snd_assert(runtime
!= NULL
, return -ENXIO
);
2599 snd_assert(substream
->ops
->copy
!= NULL
|| runtime
->dma_area
!= NULL
, return -EINVAL
);
2600 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2603 snd_assert(substream
->ffile
!= NULL
, return -ENXIO
);
2604 nonblock
= !!(substream
->ffile
->f_flags
& O_NONBLOCK
);
2605 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2606 if (substream
->oss
.oss
) {
2607 snd_pcm_oss_setup_t
*setup
= substream
->oss
.setup
;
2608 if (setup
!= NULL
) {
2609 if (setup
->nonblock
)
2611 else if (setup
->block
)
2616 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_INTERLEAVED
)
2618 return snd_pcm_lib_read1(substream
, (unsigned long)buf
, size
, nonblock
, snd_pcm_lib_read_transfer
);
2621 static int snd_pcm_lib_readv_transfer(snd_pcm_substream_t
*substream
,
2623 unsigned long data
, unsigned int off
,
2624 snd_pcm_uframes_t frames
)
2626 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2628 void __user
**bufs
= (void __user
**)data
;
2629 int channels
= runtime
->channels
;
2631 if (substream
->ops
->copy
) {
2632 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
2636 buf
= *bufs
+ samples_to_bytes(runtime
, off
);
2637 if ((err
= substream
->ops
->copy(substream
, c
, hwoff
, buf
, frames
)) < 0)
2641 snd_pcm_uframes_t dma_csize
= runtime
->dma_bytes
/ channels
;
2642 snd_assert(runtime
->dma_area
, return -EFAULT
);
2643 for (c
= 0; c
< channels
; ++c
, ++bufs
) {
2649 hwbuf
= runtime
->dma_area
+ (c
* dma_csize
) + samples_to_bytes(runtime
, hwoff
);
2650 buf
= *bufs
+ samples_to_bytes(runtime
, off
);
2651 if (copy_to_user(buf
, hwbuf
, samples_to_bytes(runtime
, frames
)))
2658 snd_pcm_sframes_t
snd_pcm_lib_readv(snd_pcm_substream_t
*substream
,
2660 snd_pcm_uframes_t frames
)
2662 snd_pcm_runtime_t
*runtime
;
2665 snd_assert(substream
!= NULL
, return -ENXIO
);
2666 runtime
= substream
->runtime
;
2667 snd_assert(runtime
!= NULL
, return -ENXIO
);
2668 snd_assert(substream
->ops
->copy
!= NULL
|| runtime
->dma_area
!= NULL
, return -EINVAL
);
2669 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2672 snd_assert(substream
->ffile
!= NULL
, return -ENXIO
);
2673 nonblock
= !!(substream
->ffile
->f_flags
& O_NONBLOCK
);
2674 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2675 if (substream
->oss
.oss
) {
2676 snd_pcm_oss_setup_t
*setup
= substream
->oss
.setup
;
2677 if (setup
!= NULL
) {
2678 if (setup
->nonblock
)
2680 else if (setup
->block
)
2686 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
2688 return snd_pcm_lib_read1(substream
, (unsigned long)bufs
, frames
, nonblock
, snd_pcm_lib_readv_transfer
);
2695 EXPORT_SYMBOL(snd_interval_refine
);
2696 EXPORT_SYMBOL(snd_interval_list
);
2697 EXPORT_SYMBOL(snd_interval_ratnum
);
2698 EXPORT_SYMBOL(_snd_pcm_hw_params_any
);
2699 EXPORT_SYMBOL(_snd_pcm_hw_param_min
);
2700 EXPORT_SYMBOL(_snd_pcm_hw_param_set
);
2701 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty
);
2702 EXPORT_SYMBOL(_snd_pcm_hw_param_setinteger
);
2703 EXPORT_SYMBOL(snd_pcm_hw_param_value_min
);
2704 EXPORT_SYMBOL(snd_pcm_hw_param_value_max
);
2705 EXPORT_SYMBOL(snd_pcm_hw_param_mask
);
2706 EXPORT_SYMBOL(snd_pcm_hw_param_first
);
2707 EXPORT_SYMBOL(snd_pcm_hw_param_last
);
2708 EXPORT_SYMBOL(snd_pcm_hw_param_near
);
2709 EXPORT_SYMBOL(snd_pcm_hw_param_set
);
2710 EXPORT_SYMBOL(snd_pcm_hw_refine
);
2711 EXPORT_SYMBOL(snd_pcm_hw_constraints_init
);
2712 EXPORT_SYMBOL(snd_pcm_hw_constraints_complete
);
2713 EXPORT_SYMBOL(snd_pcm_hw_constraint_list
);
2714 EXPORT_SYMBOL(snd_pcm_hw_constraint_step
);
2715 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums
);
2716 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens
);
2717 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits
);
2718 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax
);
2719 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer
);
2720 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2
);
2721 EXPORT_SYMBOL(snd_pcm_hw_rule_add
);
2722 EXPORT_SYMBOL(snd_pcm_set_ops
);
2723 EXPORT_SYMBOL(snd_pcm_set_sync
);
2724 EXPORT_SYMBOL(snd_pcm_lib_ioctl
);
2725 EXPORT_SYMBOL(snd_pcm_stop
);
2726 EXPORT_SYMBOL(snd_pcm_period_elapsed
);
2727 EXPORT_SYMBOL(snd_pcm_lib_write
);
2728 EXPORT_SYMBOL(snd_pcm_lib_read
);
2729 EXPORT_SYMBOL(snd_pcm_lib_writev
);
2730 EXPORT_SYMBOL(snd_pcm_lib_readv
);
2731 EXPORT_SYMBOL(snd_pcm_lib_buffer_bytes
);
2732 EXPORT_SYMBOL(snd_pcm_lib_period_bytes
);
2734 EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all
);
2735 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages
);
2736 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all
);
2737 EXPORT_SYMBOL(snd_pcm_sgbuf_ops_page
);
2738 EXPORT_SYMBOL(snd_pcm_lib_malloc_pages
);
2739 EXPORT_SYMBOL(snd_pcm_lib_free_pages
);