blob: 063c675177a9dc53f12c84a0c297e20b48d5127b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Abramo Bagnara <abramo@alsa-project.org>
5 *
6 *
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.
11 *
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.
16 *
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
20 *
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/slab.h>
24#include <linux/time.h>
25#include <sound/core.h>
26#include <sound/control.h>
27#include <sound/info.h>
28#include <sound/pcm.h>
29#include <sound/pcm_params.h>
30#include <sound/timer.h>
31
32/*
33 * fill ring buffer with silence
34 * runtime->silence_start: starting pointer to silence area
35 * runtime->silence_filled: size filled with silence
36 * runtime->silence_threshold: threshold from application
37 * runtime->silence_size: maximal size from application
38 *
39 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
40 */
Takashi Iwai877211f2005-11-17 13:59:38 +010041void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Takashi Iwai877211f2005-11-17 13:59:38 +010043 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 snd_pcm_uframes_t frames, ofs, transfer;
45
46 if (runtime->silence_size < runtime->boundary) {
47 snd_pcm_sframes_t noise_dist, n;
48 if (runtime->silence_start != runtime->control->appl_ptr) {
49 n = runtime->control->appl_ptr - runtime->silence_start;
50 if (n < 0)
51 n += runtime->boundary;
52 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
53 runtime->silence_filled -= n;
54 else
55 runtime->silence_filled = 0;
56 runtime->silence_start = runtime->control->appl_ptr;
57 }
Takashi Iwai235475c2005-12-07 15:28:07 +010058 if (runtime->silence_filled >= runtime->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
61 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
62 return;
63 frames = runtime->silence_threshold - noise_dist;
64 if (frames > runtime->silence_size)
65 frames = runtime->silence_size;
66 } else {
67 if (new_hw_ptr == ULONG_MAX) { /* initialization */
68 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
69 runtime->silence_filled = avail > 0 ? avail : 0;
70 runtime->silence_start = (runtime->status->hw_ptr +
71 runtime->silence_filled) %
72 runtime->boundary;
73 } else {
74 ofs = runtime->status->hw_ptr;
75 frames = new_hw_ptr - ofs;
76 if ((snd_pcm_sframes_t)frames < 0)
77 frames += runtime->boundary;
78 runtime->silence_filled -= frames;
79 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
80 runtime->silence_filled = 0;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020081 runtime->silence_start = new_hw_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 } else {
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020083 runtime->silence_start = ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86 frames = runtime->buffer_size - runtime->silence_filled;
87 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +020088 if (snd_BUG_ON(frames > runtime->buffer_size))
89 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (frames == 0)
91 return;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020092 ofs = runtime->silence_start % runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 while (frames > 0) {
94 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
95 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
96 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
97 if (substream->ops->silence) {
98 int err;
99 err = substream->ops->silence(substream, -1, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200100 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 } else {
102 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
103 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
104 }
105 } else {
106 unsigned int c;
107 unsigned int channels = runtime->channels;
108 if (substream->ops->silence) {
109 for (c = 0; c < channels; ++c) {
110 int err;
111 err = substream->ops->silence(substream, c, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200112 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114 } else {
115 size_t dma_csize = runtime->dma_bytes / channels;
116 for (c = 0; c < channels; ++c) {
117 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
118 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
119 }
120 }
121 }
122 runtime->silence_filled += transfer;
123 frames -= transfer;
124 ofs = 0;
125 }
126}
127
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100128#ifdef CONFIG_SND_PCM_XRUN_DEBUG
129#define xrun_debug(substream) ((substream)->pstr->xrun_debug)
130#else
131#define xrun_debug(substream) 0
132#endif
133
134#define dump_stack_on_xrun(substream) do { \
135 if (xrun_debug(substream) > 1) \
136 dump_stack(); \
137 } while (0)
138
Takashi Iwai877211f2005-11-17 13:59:38 +0100139static void xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100142 if (xrun_debug(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
144 substream->pcm->card->number,
145 substream->pcm->device,
146 substream->stream ? 'c' : 'p');
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100147 dump_stack_on_xrun(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Takashi Iwai98204642009-03-19 09:59:21 +0100151static snd_pcm_uframes_t
152snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
153 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 snd_pcm_uframes_t pos;
156
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100157 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
158 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 pos = substream->ops->pointer(substream);
160 if (pos == SNDRV_PCM_POS_XRUN)
161 return pos; /* XRUN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (pos >= runtime->buffer_size) {
Takashi Iwai5f513e12009-03-19 10:01:47 +0100163 if (printk_ratelimit()) {
164 snd_printd(KERN_ERR "BUG: stream = %i, pos = 0x%lx, "
165 "buffer size = 0x%lx, period size = 0x%lx\n",
166 substream->stream, pos, runtime->buffer_size,
167 runtime->period_size);
168 }
169 pos = 0;
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 pos -= pos % runtime->min_align;
172 return pos;
173}
174
Takashi Iwai98204642009-03-19 09:59:21 +0100175static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
176 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 snd_pcm_uframes_t avail;
179
180 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
181 avail = snd_pcm_playback_avail(runtime);
182 else
183 avail = snd_pcm_capture_avail(runtime);
184 if (avail > runtime->avail_max)
185 runtime->avail_max = avail;
186 if (avail >= runtime->stop_threshold) {
187 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
188 snd_pcm_drain_done(substream);
189 else
190 xrun(substream);
191 return -EPIPE;
192 }
193 if (avail >= runtime->control->avail_min)
194 wake_up(&runtime->sleep);
195 return 0;
196}
197
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100198#define hw_ptr_error(substream, fmt, args...) \
199 do { \
200 if (xrun_debug(substream)) { \
201 if (printk_ratelimit()) { \
Takashi Iwaicad377a2009-03-19 09:55:15 +0100202 snd_printd("PCM: " fmt, ##args); \
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100203 } \
204 dump_stack_on_xrun(substream); \
205 } \
206 } while (0)
207
Takashi Iwai98204642009-03-19 09:59:21 +0100208static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Takashi Iwai877211f2005-11-17 13:59:38 +0100210 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 snd_pcm_uframes_t pos;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100212 snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt, hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 snd_pcm_sframes_t delta;
214
215 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
216 if (pos == SNDRV_PCM_POS_XRUN) {
217 xrun(substream);
218 return -EPIPE;
219 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100220 hw_base = runtime->hw_ptr_base;
221 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100223 delta = new_hw_ptr - hw_ptr_interrupt;
Takashi Iwaided652f2009-03-19 10:08:49 +0100224 if (hw_ptr_interrupt >= runtime->boundary) {
225 hw_ptr_interrupt %= runtime->boundary;
226 if (!hw_base) /* hw_base was already lapped; recalc delta */
227 delta = new_hw_ptr - hw_ptr_interrupt;
228 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100229 if (delta < 0) {
230 delta += runtime->buffer_size;
231 if (delta < 0) {
232 hw_ptr_error(substream,
233 "Unexpected hw_pointer value "
234 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
235 substream->stream, (long)pos,
236 (long)hw_ptr_interrupt);
237 /* rebase to interrupt position */
238 hw_base = new_hw_ptr = hw_ptr_interrupt;
Takashi Iwaided652f2009-03-19 10:08:49 +0100239 /* align hw_base to buffer_size */
240 hw_base -= hw_base % runtime->buffer_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100241 delta = 0;
242 } else {
243 hw_base += runtime->buffer_size;
244 if (hw_base == runtime->boundary)
245 hw_base = 0;
246 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100249 if (delta > runtime->period_size) {
250 hw_ptr_error(substream,
251 "Lost interrupts? "
252 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
253 substream->stream, (long)delta,
254 (long)hw_ptr_interrupt);
255 /* rebase hw_ptr_interrupt */
256 hw_ptr_interrupt =
257 new_hw_ptr - new_hw_ptr % runtime->period_size;
258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
260 runtime->silence_size > 0)
261 snd_pcm_playback_silence(substream, new_hw_ptr);
262
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100263 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 runtime->status->hw_ptr = new_hw_ptr;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100265 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 return snd_pcm_update_hw_ptr_post(substream, runtime);
268}
269
270/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100271int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Takashi Iwai877211f2005-11-17 13:59:38 +0100273 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 snd_pcm_uframes_t pos;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100275 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 snd_pcm_sframes_t delta;
277
278 old_hw_ptr = runtime->status->hw_ptr;
279 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
280 if (pos == SNDRV_PCM_POS_XRUN) {
281 xrun(substream);
282 return -EPIPE;
283 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100284 hw_base = runtime->hw_ptr_base;
285 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100287 delta = new_hw_ptr - old_hw_ptr;
288 if (delta < 0) {
289 delta += runtime->buffer_size;
290 if (delta < 0) {
291 hw_ptr_error(substream,
292 "Unexpected hw_pointer value [2] "
293 "(stream=%i, pos=%ld, old_ptr=%ld)\n",
294 substream->stream, (long)pos,
295 (long)old_hw_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return 0;
297 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100298 hw_base += runtime->buffer_size;
299 if (hw_base == runtime->boundary)
300 hw_base = 0;
301 new_hw_ptr = hw_base + pos;
302 }
303 if (delta > runtime->period_size && runtime->periods > 1) {
304 hw_ptr_error(substream,
305 "hw_ptr skipping! "
306 "(pos=%ld, delta=%ld, period=%ld)\n",
307 (long)pos, (long)delta,
308 (long)runtime->period_size);
309 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
312 runtime->silence_size > 0)
313 snd_pcm_playback_silence(substream, new_hw_ptr);
314
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100315 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 runtime->status->hw_ptr = new_hw_ptr;
317
318 return snd_pcm_update_hw_ptr_post(substream, runtime);
319}
320
321/**
322 * snd_pcm_set_ops - set the PCM operators
323 * @pcm: the pcm instance
324 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
325 * @ops: the operator table
326 *
327 * Sets the given PCM operators to the pcm instance.
328 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100329void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Takashi Iwai877211f2005-11-17 13:59:38 +0100331 struct snd_pcm_str *stream = &pcm->streams[direction];
332 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 for (substream = stream->substream; substream != NULL; substream = substream->next)
335 substream->ops = ops;
336}
337
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200338EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340/**
341 * snd_pcm_sync - set the PCM sync id
342 * @substream: the pcm substream
343 *
344 * Sets the PCM sync identifier for the card.
345 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100346void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Takashi Iwai877211f2005-11-17 13:59:38 +0100348 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 runtime->sync.id32[0] = substream->pcm->card->number;
351 runtime->sync.id32[1] = -1;
352 runtime->sync.id32[2] = -1;
353 runtime->sync.id32[3] = -1;
354}
355
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200356EXPORT_SYMBOL(snd_pcm_set_sync);
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/*
359 * Standard ioctl routine
360 */
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362static inline unsigned int div32(unsigned int a, unsigned int b,
363 unsigned int *r)
364{
365 if (b == 0) {
366 *r = 0;
367 return UINT_MAX;
368 }
369 *r = a % b;
370 return a / b;
371}
372
373static inline unsigned int div_down(unsigned int a, unsigned int b)
374{
375 if (b == 0)
376 return UINT_MAX;
377 return a / b;
378}
379
380static inline unsigned int div_up(unsigned int a, unsigned int b)
381{
382 unsigned int r;
383 unsigned int q;
384 if (b == 0)
385 return UINT_MAX;
386 q = div32(a, b, &r);
387 if (r)
388 ++q;
389 return q;
390}
391
392static inline unsigned int mul(unsigned int a, unsigned int b)
393{
394 if (a == 0)
395 return 0;
396 if (div_down(UINT_MAX, a) < b)
397 return UINT_MAX;
398 return a * b;
399}
400
401static inline unsigned int muldiv32(unsigned int a, unsigned int b,
402 unsigned int c, unsigned int *r)
403{
404 u_int64_t n = (u_int64_t) a * b;
405 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200406 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 *r = 0;
408 return UINT_MAX;
409 }
410 div64_32(&n, c, r);
411 if (n >= UINT_MAX) {
412 *r = 0;
413 return UINT_MAX;
414 }
415 return n;
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/**
419 * snd_interval_refine - refine the interval value of configurator
420 * @i: the interval value to refine
421 * @v: the interval value to refer to
422 *
423 * Refines the interval value with the reference value.
424 * The interval is changed to the range satisfying both intervals.
425 * The interval status (min, max, integer, etc.) are evaluated.
426 *
427 * Returns non-zero if the value is changed, zero if not changed.
428 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100429int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200432 if (snd_BUG_ON(snd_interval_empty(i)))
433 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (i->min < v->min) {
435 i->min = v->min;
436 i->openmin = v->openmin;
437 changed = 1;
438 } else if (i->min == v->min && !i->openmin && v->openmin) {
439 i->openmin = 1;
440 changed = 1;
441 }
442 if (i->max > v->max) {
443 i->max = v->max;
444 i->openmax = v->openmax;
445 changed = 1;
446 } else if (i->max == v->max && !i->openmax && v->openmax) {
447 i->openmax = 1;
448 changed = 1;
449 }
450 if (!i->integer && v->integer) {
451 i->integer = 1;
452 changed = 1;
453 }
454 if (i->integer) {
455 if (i->openmin) {
456 i->min++;
457 i->openmin = 0;
458 }
459 if (i->openmax) {
460 i->max--;
461 i->openmax = 0;
462 }
463 } else if (!i->openmin && !i->openmax && i->min == i->max)
464 i->integer = 1;
465 if (snd_interval_checkempty(i)) {
466 snd_interval_none(i);
467 return -EINVAL;
468 }
469 return changed;
470}
471
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200472EXPORT_SYMBOL(snd_interval_refine);
473
Takashi Iwai877211f2005-11-17 13:59:38 +0100474static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200476 if (snd_BUG_ON(snd_interval_empty(i)))
477 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (snd_interval_single(i))
479 return 0;
480 i->max = i->min;
481 i->openmax = i->openmin;
482 if (i->openmax)
483 i->max++;
484 return 1;
485}
486
Takashi Iwai877211f2005-11-17 13:59:38 +0100487static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200489 if (snd_BUG_ON(snd_interval_empty(i)))
490 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (snd_interval_single(i))
492 return 0;
493 i->min = i->max;
494 i->openmin = i->openmax;
495 if (i->openmin)
496 i->min--;
497 return 1;
498}
499
Takashi Iwai877211f2005-11-17 13:59:38 +0100500void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 if (a->empty || b->empty) {
503 snd_interval_none(c);
504 return;
505 }
506 c->empty = 0;
507 c->min = mul(a->min, b->min);
508 c->openmin = (a->openmin || b->openmin);
509 c->max = mul(a->max, b->max);
510 c->openmax = (a->openmax || b->openmax);
511 c->integer = (a->integer && b->integer);
512}
513
514/**
515 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200516 * @a: dividend
517 * @b: divisor
518 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 *
520 * c = a / b
521 *
522 * Returns non-zero if the value is changed, zero if not changed.
523 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100524void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 unsigned int r;
527 if (a->empty || b->empty) {
528 snd_interval_none(c);
529 return;
530 }
531 c->empty = 0;
532 c->min = div32(a->min, b->max, &r);
533 c->openmin = (r || a->openmin || b->openmax);
534 if (b->min > 0) {
535 c->max = div32(a->max, b->min, &r);
536 if (r) {
537 c->max++;
538 c->openmax = 1;
539 } else
540 c->openmax = (a->openmax || b->openmin);
541 } else {
542 c->max = UINT_MAX;
543 c->openmax = 0;
544 }
545 c->integer = 0;
546}
547
548/**
549 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200550 * @a: dividend 1
551 * @b: dividend 2
552 * @k: divisor (as integer)
553 * @c: result
554 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 * c = a * b / k
556 *
557 * Returns non-zero if the value is changed, zero if not changed.
558 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100559void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
560 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 unsigned int r;
563 if (a->empty || b->empty) {
564 snd_interval_none(c);
565 return;
566 }
567 c->empty = 0;
568 c->min = muldiv32(a->min, b->min, k, &r);
569 c->openmin = (r || a->openmin || b->openmin);
570 c->max = muldiv32(a->max, b->max, k, &r);
571 if (r) {
572 c->max++;
573 c->openmax = 1;
574 } else
575 c->openmax = (a->openmax || b->openmax);
576 c->integer = 0;
577}
578
579/**
580 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200581 * @a: dividend 1
582 * @k: dividend 2 (as integer)
583 * @b: divisor
584 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *
586 * c = a * k / b
587 *
588 * Returns non-zero if the value is changed, zero if not changed.
589 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100590void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
591 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 unsigned int r;
594 if (a->empty || b->empty) {
595 snd_interval_none(c);
596 return;
597 }
598 c->empty = 0;
599 c->min = muldiv32(a->min, k, b->max, &r);
600 c->openmin = (r || a->openmin || b->openmax);
601 if (b->min > 0) {
602 c->max = muldiv32(a->max, k, b->min, &r);
603 if (r) {
604 c->max++;
605 c->openmax = 1;
606 } else
607 c->openmax = (a->openmax || b->openmin);
608 } else {
609 c->max = UINT_MAX;
610 c->openmax = 0;
611 }
612 c->integer = 0;
613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/* ---- */
616
617
618/**
619 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200620 * @i: interval to refine
621 * @rats_count: number of ratnum_t
622 * @rats: ratnum_t array
623 * @nump: pointer to store the resultant numerator
624 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 *
626 * Returns non-zero if the value is changed, zero if not changed.
627 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100628int snd_interval_ratnum(struct snd_interval *i,
629 unsigned int rats_count, struct snd_ratnum *rats,
630 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 unsigned int best_num, best_diff, best_den;
633 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100634 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 int err;
636
637 best_num = best_den = best_diff = 0;
638 for (k = 0; k < rats_count; ++k) {
639 unsigned int num = rats[k].num;
640 unsigned int den;
641 unsigned int q = i->min;
642 int diff;
643 if (q == 0)
644 q = 1;
645 den = div_down(num, q);
646 if (den < rats[k].den_min)
647 continue;
648 if (den > rats[k].den_max)
649 den = rats[k].den_max;
650 else {
651 unsigned int r;
652 r = (den - rats[k].den_min) % rats[k].den_step;
653 if (r != 0)
654 den -= r;
655 }
656 diff = num - q * den;
657 if (best_num == 0 ||
658 diff * best_den < best_diff * den) {
659 best_diff = diff;
660 best_den = den;
661 best_num = num;
662 }
663 }
664 if (best_den == 0) {
665 i->empty = 1;
666 return -EINVAL;
667 }
668 t.min = div_down(best_num, best_den);
669 t.openmin = !!(best_num % best_den);
670
671 best_num = best_den = best_diff = 0;
672 for (k = 0; k < rats_count; ++k) {
673 unsigned int num = rats[k].num;
674 unsigned int den;
675 unsigned int q = i->max;
676 int diff;
677 if (q == 0) {
678 i->empty = 1;
679 return -EINVAL;
680 }
681 den = div_up(num, q);
682 if (den > rats[k].den_max)
683 continue;
684 if (den < rats[k].den_min)
685 den = rats[k].den_min;
686 else {
687 unsigned int r;
688 r = (den - rats[k].den_min) % rats[k].den_step;
689 if (r != 0)
690 den += rats[k].den_step - r;
691 }
692 diff = q * den - num;
693 if (best_num == 0 ||
694 diff * best_den < best_diff * den) {
695 best_diff = diff;
696 best_den = den;
697 best_num = num;
698 }
699 }
700 if (best_den == 0) {
701 i->empty = 1;
702 return -EINVAL;
703 }
704 t.max = div_up(best_num, best_den);
705 t.openmax = !!(best_num % best_den);
706 t.integer = 0;
707 err = snd_interval_refine(i, &t);
708 if (err < 0)
709 return err;
710
711 if (snd_interval_single(i)) {
712 if (nump)
713 *nump = best_num;
714 if (denp)
715 *denp = best_den;
716 }
717 return err;
718}
719
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200720EXPORT_SYMBOL(snd_interval_ratnum);
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722/**
723 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200724 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100725 * @rats_count: number of struct ratden
726 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200727 * @nump: pointer to store the resultant numerator
728 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 *
730 * Returns non-zero if the value is changed, zero if not changed.
731 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100732static int snd_interval_ratden(struct snd_interval *i,
733 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 unsigned int *nump, unsigned int *denp)
735{
736 unsigned int best_num, best_diff, best_den;
737 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100738 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 int err;
740
741 best_num = best_den = best_diff = 0;
742 for (k = 0; k < rats_count; ++k) {
743 unsigned int num;
744 unsigned int den = rats[k].den;
745 unsigned int q = i->min;
746 int diff;
747 num = mul(q, den);
748 if (num > rats[k].num_max)
749 continue;
750 if (num < rats[k].num_min)
751 num = rats[k].num_max;
752 else {
753 unsigned int r;
754 r = (num - rats[k].num_min) % rats[k].num_step;
755 if (r != 0)
756 num += rats[k].num_step - r;
757 }
758 diff = num - q * den;
759 if (best_num == 0 ||
760 diff * best_den < best_diff * den) {
761 best_diff = diff;
762 best_den = den;
763 best_num = num;
764 }
765 }
766 if (best_den == 0) {
767 i->empty = 1;
768 return -EINVAL;
769 }
770 t.min = div_down(best_num, best_den);
771 t.openmin = !!(best_num % best_den);
772
773 best_num = best_den = best_diff = 0;
774 for (k = 0; k < rats_count; ++k) {
775 unsigned int num;
776 unsigned int den = rats[k].den;
777 unsigned int q = i->max;
778 int diff;
779 num = mul(q, den);
780 if (num < rats[k].num_min)
781 continue;
782 if (num > rats[k].num_max)
783 num = rats[k].num_max;
784 else {
785 unsigned int r;
786 r = (num - rats[k].num_min) % rats[k].num_step;
787 if (r != 0)
788 num -= r;
789 }
790 diff = q * den - num;
791 if (best_num == 0 ||
792 diff * best_den < best_diff * den) {
793 best_diff = diff;
794 best_den = den;
795 best_num = num;
796 }
797 }
798 if (best_den == 0) {
799 i->empty = 1;
800 return -EINVAL;
801 }
802 t.max = div_up(best_num, best_den);
803 t.openmax = !!(best_num % best_den);
804 t.integer = 0;
805 err = snd_interval_refine(i, &t);
806 if (err < 0)
807 return err;
808
809 if (snd_interval_single(i)) {
810 if (nump)
811 *nump = best_num;
812 if (denp)
813 *denp = best_den;
814 }
815 return err;
816}
817
818/**
819 * snd_interval_list - refine the interval value from the list
820 * @i: the interval value to refine
821 * @count: the number of elements in the list
822 * @list: the value list
823 * @mask: the bit-mask to evaluate
824 *
825 * Refines the interval value from the list.
826 * When mask is non-zero, only the elements corresponding to bit 1 are
827 * evaluated.
828 *
829 * Returns non-zero if the value is changed, zero if not changed.
830 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100831int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 unsigned int k;
834 int changed = 0;
Takashi Iwai0981a262007-02-01 14:53:49 +0100835
836 if (!count) {
837 i->empty = 1;
838 return -EINVAL;
839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 for (k = 0; k < count; k++) {
841 if (mask && !(mask & (1 << k)))
842 continue;
843 if (i->min == list[k] && !i->openmin)
844 goto _l1;
845 if (i->min < list[k]) {
846 i->min = list[k];
847 i->openmin = 0;
848 changed = 1;
849 goto _l1;
850 }
851 }
852 i->empty = 1;
853 return -EINVAL;
854 _l1:
855 for (k = count; k-- > 0;) {
856 if (mask && !(mask & (1 << k)))
857 continue;
858 if (i->max == list[k] && !i->openmax)
859 goto _l2;
860 if (i->max > list[k]) {
861 i->max = list[k];
862 i->openmax = 0;
863 changed = 1;
864 goto _l2;
865 }
866 }
867 i->empty = 1;
868 return -EINVAL;
869 _l2:
870 if (snd_interval_checkempty(i)) {
871 i->empty = 1;
872 return -EINVAL;
873 }
874 return changed;
875}
876
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200877EXPORT_SYMBOL(snd_interval_list);
878
Takashi Iwai877211f2005-11-17 13:59:38 +0100879static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 unsigned int n;
882 int changed = 0;
883 n = (i->min - min) % step;
884 if (n != 0 || i->openmin) {
885 i->min += step - n;
886 changed = 1;
887 }
888 n = (i->max - min) % step;
889 if (n != 0 || i->openmax) {
890 i->max -= n;
891 changed = 1;
892 }
893 if (snd_interval_checkempty(i)) {
894 i->empty = 1;
895 return -EINVAL;
896 }
897 return changed;
898}
899
900/* Info constraints helpers */
901
902/**
903 * snd_pcm_hw_rule_add - add the hw-constraint rule
904 * @runtime: the pcm runtime instance
905 * @cond: condition bits
906 * @var: the variable to evaluate
907 * @func: the evaluation function
908 * @private: the private data pointer passed to function
909 * @dep: the dependent variables
910 *
911 * Returns zero if successful, or a negative error code on failure.
912 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100913int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 int var,
915 snd_pcm_hw_rule_func_t func, void *private,
916 int dep, ...)
917{
Takashi Iwai877211f2005-11-17 13:59:38 +0100918 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
919 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 unsigned int k;
921 va_list args;
922 va_start(args, dep);
923 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100924 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 unsigned int new_rules = constrs->rules_all + 16;
926 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
927 if (!new)
928 return -ENOMEM;
929 if (constrs->rules) {
930 memcpy(new, constrs->rules,
931 constrs->rules_num * sizeof(*c));
932 kfree(constrs->rules);
933 }
934 constrs->rules = new;
935 constrs->rules_all = new_rules;
936 }
937 c = &constrs->rules[constrs->rules_num];
938 c->cond = cond;
939 c->func = func;
940 c->var = var;
941 c->private = private;
942 k = 0;
943 while (1) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200944 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
945 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 c->deps[k++] = dep;
947 if (dep < 0)
948 break;
949 dep = va_arg(args, int);
950 }
951 constrs->rules_num++;
952 va_end(args);
953 return 0;
954}
955
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200956EXPORT_SYMBOL(snd_pcm_hw_rule_add);
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700959 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +0200960 * @runtime: PCM runtime instance
961 * @var: hw_params variable to apply the mask
962 * @mask: the bitmap mask
963 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700964 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100966int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 u_int32_t mask)
968{
Takashi Iwai877211f2005-11-17 13:59:38 +0100969 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
970 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 *maskp->bits &= mask;
972 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
973 if (*maskp->bits == 0)
974 return -EINVAL;
975 return 0;
976}
977
978/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700979 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +0200980 * @runtime: PCM runtime instance
981 * @var: hw_params variable to apply the mask
982 * @mask: the 64bit bitmap mask
983 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700984 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100986int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 u_int64_t mask)
988{
Takashi Iwai877211f2005-11-17 13:59:38 +0100989 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
990 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 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])
995 return -EINVAL;
996 return 0;
997}
998
999/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001000 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001001 * @runtime: PCM runtime instance
1002 * @var: hw_params variable to apply the integer constraint
1003 *
1004 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001006int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Takashi Iwai877211f2005-11-17 13:59:38 +01001008 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return snd_interval_setinteger(constrs_interval(constrs, var));
1010}
1011
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001012EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001015 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001016 * @runtime: PCM runtime instance
1017 * @var: hw_params variable to apply the range
1018 * @min: the minimal value
1019 * @max: the maximal value
1020 *
1021 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001023int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 unsigned int min, unsigned int max)
1025{
Takashi Iwai877211f2005-11-17 13:59:38 +01001026 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1027 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 t.min = min;
1029 t.max = max;
1030 t.openmin = t.openmax = 0;
1031 t.integer = 0;
1032 return snd_interval_refine(constrs_interval(constrs, var), &t);
1033}
1034
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001035EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1036
Takashi Iwai877211f2005-11-17 13:59:38 +01001037static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1038 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Takashi Iwai877211f2005-11-17 13:59:38 +01001040 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1042}
1043
1044
1045/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001046 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001047 * @runtime: PCM runtime instance
1048 * @cond: condition bits
1049 * @var: hw_params variable to apply the list constraint
1050 * @l: list
1051 *
1052 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001054int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 unsigned int cond,
1056 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001057 struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
1059 return snd_pcm_hw_rule_add(runtime, cond, var,
1060 snd_pcm_hw_rule_list, l,
1061 var, -1);
1062}
1063
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001064EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1065
Takashi Iwai877211f2005-11-17 13:59:38 +01001066static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1067 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Takashi Iwai877211f2005-11-17 13:59:38 +01001069 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 unsigned int num = 0, den = 0;
1071 int err;
1072 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1073 r->nrats, r->rats, &num, &den);
1074 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1075 params->rate_num = num;
1076 params->rate_den = den;
1077 }
1078 return err;
1079}
1080
1081/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001082 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001083 * @runtime: PCM runtime instance
1084 * @cond: condition bits
1085 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001086 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001088int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 unsigned int cond,
1090 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001091 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 return snd_pcm_hw_rule_add(runtime, cond, var,
1094 snd_pcm_hw_rule_ratnums, r,
1095 var, -1);
1096}
1097
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001098EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1099
Takashi Iwai877211f2005-11-17 13:59:38 +01001100static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1101 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Takashi Iwai877211f2005-11-17 13:59:38 +01001103 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 unsigned int num = 0, den = 0;
1105 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1106 r->nrats, r->rats, &num, &den);
1107 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1108 params->rate_num = num;
1109 params->rate_den = den;
1110 }
1111 return err;
1112}
1113
1114/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001115 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001116 * @runtime: PCM runtime instance
1117 * @cond: condition bits
1118 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001119 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001121int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 unsigned int cond,
1123 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001124 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
1126 return snd_pcm_hw_rule_add(runtime, cond, var,
1127 snd_pcm_hw_rule_ratdens, r,
1128 var, -1);
1129}
1130
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001131EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1132
Takashi Iwai877211f2005-11-17 13:59:38 +01001133static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1134 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 unsigned int l = (unsigned long) rule->private;
1137 int width = l & 0xffff;
1138 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001139 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (snd_interval_single(i) && snd_interval_value(i) == width)
1141 params->msbits = msbits;
1142 return 0;
1143}
1144
1145/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001146 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001147 * @runtime: PCM runtime instance
1148 * @cond: condition bits
1149 * @width: sample bits width
1150 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001152int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 unsigned int cond,
1154 unsigned int width,
1155 unsigned int msbits)
1156{
1157 unsigned long l = (msbits << 16) | width;
1158 return snd_pcm_hw_rule_add(runtime, cond, -1,
1159 snd_pcm_hw_rule_msbits,
1160 (void*) l,
1161 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1162}
1163
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001164EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1165
Takashi Iwai877211f2005-11-17 13:59:38 +01001166static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1167 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 unsigned long step = (unsigned long) rule->private;
1170 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1171}
1172
1173/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001174 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001175 * @runtime: PCM runtime instance
1176 * @cond: condition bits
1177 * @var: hw_params variable to apply the step constraint
1178 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001180int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 unsigned int cond,
1182 snd_pcm_hw_param_t var,
1183 unsigned long step)
1184{
1185 return snd_pcm_hw_rule_add(runtime, cond, var,
1186 snd_pcm_hw_rule_step, (void *) step,
1187 var, -1);
1188}
1189
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001190EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1191
Takashi Iwai877211f2005-11-17 13:59:38 +01001192static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001194 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1196 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1197 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1198 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1199 };
1200 return snd_interval_list(hw_param_interval(params, rule->var),
1201 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1202}
1203
1204/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001205 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001206 * @runtime: PCM runtime instance
1207 * @cond: condition bits
1208 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001210int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 unsigned int cond,
1212 snd_pcm_hw_param_t var)
1213{
1214 return snd_pcm_hw_rule_add(runtime, cond, var,
1215 snd_pcm_hw_rule_pow2, NULL,
1216 var, -1);
1217}
1218
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001219EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1220
Takashi Iwai877211f2005-11-17 13:59:38 +01001221static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001222 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 if (hw_is_mask(var)) {
1225 snd_mask_any(hw_param_mask(params, var));
1226 params->cmask |= 1 << var;
1227 params->rmask |= 1 << var;
1228 return;
1229 }
1230 if (hw_is_interval(var)) {
1231 snd_interval_any(hw_param_interval(params, var));
1232 params->cmask |= 1 << var;
1233 params->rmask |= 1 << var;
1234 return;
1235 }
1236 snd_BUG();
1237}
1238
Takashi Iwai877211f2005-11-17 13:59:38 +01001239void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 unsigned int k;
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);
1247 params->info = ~0U;
1248}
1249
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001250EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001253 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001254 * @params: the hw_params instance
1255 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001256 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001258 * Return the value for field @var if it's fixed in configuration space
1259 * defined by @params. Return -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001261int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1262 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001265 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (!snd_mask_single(mask))
1267 return -EINVAL;
1268 if (dir)
1269 *dir = 0;
1270 return snd_mask_value(mask);
1271 }
1272 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001273 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (!snd_interval_single(i))
1275 return -EINVAL;
1276 if (dir)
1277 *dir = i->openmin;
1278 return snd_interval_value(i);
1279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return -EINVAL;
1281}
1282
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001283EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Takashi Iwai877211f2005-11-17 13:59:38 +01001285void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 snd_pcm_hw_param_t var)
1287{
1288 if (hw_is_mask(var)) {
1289 snd_mask_none(hw_param_mask(params, var));
1290 params->cmask |= 1 << var;
1291 params->rmask |= 1 << var;
1292 } else if (hw_is_interval(var)) {
1293 snd_interval_none(hw_param_interval(params, var));
1294 params->cmask |= 1 << var;
1295 params->rmask |= 1 << var;
1296 } else {
1297 snd_BUG();
1298 }
1299}
1300
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001301EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Takashi Iwai877211f2005-11-17 13:59:38 +01001303static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001304 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
1306 int changed;
1307 if (hw_is_mask(var))
1308 changed = snd_mask_refine_first(hw_param_mask(params, var));
1309 else if (hw_is_interval(var))
1310 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001311 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (changed) {
1314 params->cmask |= 1 << var;
1315 params->rmask |= 1 << var;
1316 }
1317 return changed;
1318}
1319
1320
1321/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001322 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001323 * @pcm: PCM instance
1324 * @params: the hw_params instance
1325 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001326 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001328 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 * values > minimum. Reduce configuration space accordingly.
1330 * Return the minimum.
1331 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001332int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1333 struct snd_pcm_hw_params *params,
1334 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 int changed = _snd_pcm_hw_param_first(params, var);
1337 if (changed < 0)
1338 return changed;
1339 if (params->rmask) {
1340 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001341 if (snd_BUG_ON(err < 0))
1342 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344 return snd_pcm_hw_param_value(params, var, dir);
1345}
1346
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001347EXPORT_SYMBOL(snd_pcm_hw_param_first);
1348
Takashi Iwai877211f2005-11-17 13:59:38 +01001349static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001350 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 int changed;
1353 if (hw_is_mask(var))
1354 changed = snd_mask_refine_last(hw_param_mask(params, var));
1355 else if (hw_is_interval(var))
1356 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001357 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (changed) {
1360 params->cmask |= 1 << var;
1361 params->rmask |= 1 << var;
1362 }
1363 return changed;
1364}
1365
1366
1367/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001368 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001369 * @pcm: PCM instance
1370 * @params: the hw_params instance
1371 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001372 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001374 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 * values < maximum. Reduce configuration space accordingly.
1376 * Return the maximum.
1377 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001378int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1379 struct snd_pcm_hw_params *params,
1380 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 int changed = _snd_pcm_hw_param_last(params, var);
1383 if (changed < 0)
1384 return changed;
1385 if (params->rmask) {
1386 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001387 if (snd_BUG_ON(err < 0))
1388 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390 return snd_pcm_hw_param_value(params, var, dir);
1391}
1392
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001393EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001396 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001397 * @pcm: PCM instance
1398 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001400 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 * The configuration chosen is that obtained fixing in this order:
1402 * first access, first format, first subformat, min channels,
1403 * min rate, min period time, max buffer size, min tick time
1404 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001405int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1406 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001408 static int vars[] = {
1409 SNDRV_PCM_HW_PARAM_ACCESS,
1410 SNDRV_PCM_HW_PARAM_FORMAT,
1411 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1412 SNDRV_PCM_HW_PARAM_CHANNELS,
1413 SNDRV_PCM_HW_PARAM_RATE,
1414 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1415 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1416 SNDRV_PCM_HW_PARAM_TICK_TIME,
1417 -1
1418 };
1419 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001421 for (v = vars; *v != -1; v++) {
1422 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1423 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1424 else
1425 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001426 if (snd_BUG_ON(err < 0))
1427 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return 0;
1430}
1431
Takashi Iwai877211f2005-11-17 13:59:38 +01001432static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 void *arg)
1434{
Takashi Iwai877211f2005-11-17 13:59:38 +01001435 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 unsigned long flags;
1437 snd_pcm_stream_lock_irqsave(substream, flags);
1438 if (snd_pcm_running(substream) &&
1439 snd_pcm_update_hw_ptr(substream) >= 0)
1440 runtime->status->hw_ptr %= runtime->buffer_size;
1441 else
1442 runtime->status->hw_ptr = 0;
1443 snd_pcm_stream_unlock_irqrestore(substream, flags);
1444 return 0;
1445}
1446
Takashi Iwai877211f2005-11-17 13:59:38 +01001447static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 void *arg)
1449{
Takashi Iwai877211f2005-11-17 13:59:38 +01001450 struct snd_pcm_channel_info *info = arg;
1451 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 int width;
1453 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1454 info->offset = -1;
1455 return 0;
1456 }
1457 width = snd_pcm_format_physical_width(runtime->format);
1458 if (width < 0)
1459 return width;
1460 info->offset = 0;
1461 switch (runtime->access) {
1462 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1463 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1464 info->first = info->channel * width;
1465 info->step = runtime->channels * width;
1466 break;
1467 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1468 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1469 {
1470 size_t size = runtime->dma_bytes / runtime->channels;
1471 info->first = info->channel * size * 8;
1472 info->step = width;
1473 break;
1474 }
1475 default:
1476 snd_BUG();
1477 break;
1478 }
1479 return 0;
1480}
1481
1482/**
1483 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1484 * @substream: the pcm substream instance
1485 * @cmd: ioctl command
1486 * @arg: ioctl argument
1487 *
1488 * Processes the generic ioctl commands for PCM.
1489 * Can be passed as the ioctl callback for PCM ops.
1490 *
1491 * Returns zero if successful, or a negative error code on failure.
1492 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001493int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 unsigned int cmd, void *arg)
1495{
1496 switch (cmd) {
1497 case SNDRV_PCM_IOCTL1_INFO:
1498 return 0;
1499 case SNDRV_PCM_IOCTL1_RESET:
1500 return snd_pcm_lib_ioctl_reset(substream, arg);
1501 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1502 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1503 }
1504 return -ENXIO;
1505}
1506
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001507EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509/**
1510 * snd_pcm_period_elapsed - update the pcm status for the next period
1511 * @substream: the pcm substream instance
1512 *
1513 * This function is called from the interrupt handler when the
1514 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001515 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 *
1517 * Even if more than one periods have elapsed since the last call, you
1518 * have to call this only once.
1519 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001520void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Takashi Iwai877211f2005-11-17 13:59:38 +01001522 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 unsigned long flags;
1524
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001525 if (PCM_RUNTIME_CHECK(substream))
1526 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 if (runtime->transfer_ack_begin)
1530 runtime->transfer_ack_begin(substream);
1531
1532 snd_pcm_stream_lock_irqsave(substream, flags);
1533 if (!snd_pcm_running(substream) ||
1534 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1535 goto _end;
1536
1537 if (substream->timer_running)
1538 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 _end:
1540 snd_pcm_stream_unlock_irqrestore(substream, flags);
1541 if (runtime->transfer_ack_end)
1542 runtime->transfer_ack_end(substream);
1543 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1544}
1545
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001546EXPORT_SYMBOL(snd_pcm_period_elapsed);
1547
Takashi Iwai13075512008-01-08 18:08:14 +01001548/*
1549 * Wait until avail_min data becomes available
1550 * Returns a negative error code if any error occurs during operation.
1551 * The available space is stored on availp. When err = 0 and avail = 0
1552 * on the capture stream, it indicates the stream is in DRAINING state.
1553 */
1554static int wait_for_avail_min(struct snd_pcm_substream *substream,
1555 snd_pcm_uframes_t *availp)
1556{
1557 struct snd_pcm_runtime *runtime = substream->runtime;
1558 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1559 wait_queue_t wait;
1560 int err = 0;
1561 snd_pcm_uframes_t avail = 0;
1562 long tout;
1563
1564 init_waitqueue_entry(&wait, current);
1565 add_wait_queue(&runtime->sleep, &wait);
1566 for (;;) {
1567 if (signal_pending(current)) {
1568 err = -ERESTARTSYS;
1569 break;
1570 }
1571 set_current_state(TASK_INTERRUPTIBLE);
1572 snd_pcm_stream_unlock_irq(substream);
1573 tout = schedule_timeout(msecs_to_jiffies(10000));
1574 snd_pcm_stream_lock_irq(substream);
1575 switch (runtime->status->state) {
1576 case SNDRV_PCM_STATE_SUSPENDED:
1577 err = -ESTRPIPE;
1578 goto _endloop;
1579 case SNDRV_PCM_STATE_XRUN:
1580 err = -EPIPE;
1581 goto _endloop;
1582 case SNDRV_PCM_STATE_DRAINING:
1583 if (is_playback)
1584 err = -EPIPE;
1585 else
1586 avail = 0; /* indicate draining */
1587 goto _endloop;
1588 case SNDRV_PCM_STATE_OPEN:
1589 case SNDRV_PCM_STATE_SETUP:
1590 case SNDRV_PCM_STATE_DISCONNECTED:
1591 err = -EBADFD;
1592 goto _endloop;
1593 }
1594 if (!tout) {
1595 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1596 is_playback ? "playback" : "capture");
1597 err = -EIO;
1598 break;
1599 }
1600 if (is_playback)
1601 avail = snd_pcm_playback_avail(runtime);
1602 else
1603 avail = snd_pcm_capture_avail(runtime);
1604 if (avail >= runtime->control->avail_min)
1605 break;
1606 }
1607 _endloop:
1608 remove_wait_queue(&runtime->sleep, &wait);
1609 *availp = avail;
1610 return err;
1611}
1612
Takashi Iwai877211f2005-11-17 13:59:38 +01001613static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 unsigned int hwoff,
1615 unsigned long data, unsigned int off,
1616 snd_pcm_uframes_t frames)
1617{
Takashi Iwai877211f2005-11-17 13:59:38 +01001618 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 int err;
1620 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1621 if (substream->ops->copy) {
1622 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1623 return err;
1624 } else {
1625 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1627 return -EFAULT;
1628 }
1629 return 0;
1630}
1631
Takashi Iwai877211f2005-11-17 13:59:38 +01001632typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 unsigned long data, unsigned int off,
1634 snd_pcm_uframes_t size);
1635
Takashi Iwai877211f2005-11-17 13:59:38 +01001636static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 unsigned long data,
1638 snd_pcm_uframes_t size,
1639 int nonblock,
1640 transfer_f transfer)
1641{
Takashi Iwai877211f2005-11-17 13:59:38 +01001642 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 snd_pcm_uframes_t xfer = 0;
1644 snd_pcm_uframes_t offset = 0;
1645 int err = 0;
1646
1647 if (size == 0)
1648 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 snd_pcm_stream_lock_irq(substream);
1651 switch (runtime->status->state) {
1652 case SNDRV_PCM_STATE_PREPARED:
1653 case SNDRV_PCM_STATE_RUNNING:
1654 case SNDRV_PCM_STATE_PAUSED:
1655 break;
1656 case SNDRV_PCM_STATE_XRUN:
1657 err = -EPIPE;
1658 goto _end_unlock;
1659 case SNDRV_PCM_STATE_SUSPENDED:
1660 err = -ESTRPIPE;
1661 goto _end_unlock;
1662 default:
1663 err = -EBADFD;
1664 goto _end_unlock;
1665 }
1666
1667 while (size > 0) {
1668 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1669 snd_pcm_uframes_t avail;
1670 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001671 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 snd_pcm_update_hw_ptr(substream);
1673 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001674 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (nonblock) {
1676 err = -EAGAIN;
1677 goto _end_unlock;
1678 }
Takashi Iwai13075512008-01-08 18:08:14 +01001679 err = wait_for_avail_min(substream, &avail);
1680 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 frames = size > avail ? avail : size;
1684 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1685 if (frames > cont)
1686 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001687 if (snd_BUG_ON(!frames)) {
1688 snd_pcm_stream_unlock_irq(substream);
1689 return -EINVAL;
1690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 appl_ptr = runtime->control->appl_ptr;
1692 appl_ofs = appl_ptr % runtime->buffer_size;
1693 snd_pcm_stream_unlock_irq(substream);
1694 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1695 goto _end;
1696 snd_pcm_stream_lock_irq(substream);
1697 switch (runtime->status->state) {
1698 case SNDRV_PCM_STATE_XRUN:
1699 err = -EPIPE;
1700 goto _end_unlock;
1701 case SNDRV_PCM_STATE_SUSPENDED:
1702 err = -ESTRPIPE;
1703 goto _end_unlock;
1704 default:
1705 break;
1706 }
1707 appl_ptr += frames;
1708 if (appl_ptr >= runtime->boundary)
1709 appl_ptr -= runtime->boundary;
1710 runtime->control->appl_ptr = appl_ptr;
1711 if (substream->ops->ack)
1712 substream->ops->ack(substream);
1713
1714 offset += frames;
1715 size -= frames;
1716 xfer += frames;
1717 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1718 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1719 err = snd_pcm_start(substream);
1720 if (err < 0)
1721 goto _end_unlock;
1722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
1724 _end_unlock:
1725 snd_pcm_stream_unlock_irq(substream);
1726 _end:
1727 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1728}
1729
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001730/* sanity-check for read/write methods */
1731static int pcm_sanity_check(struct snd_pcm_substream *substream)
1732{
1733 struct snd_pcm_runtime *runtime;
1734 if (PCM_RUNTIME_CHECK(substream))
1735 return -ENXIO;
1736 runtime = substream->runtime;
1737 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1738 return -EINVAL;
1739 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1740 return -EBADFD;
1741 return 0;
1742}
1743
Takashi Iwai877211f2005-11-17 13:59:38 +01001744snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Takashi Iwai877211f2005-11-17 13:59:38 +01001746 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001748 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001750 err = pcm_sanity_check(substream);
1751 if (err < 0)
1752 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001754 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1757 runtime->channels > 1)
1758 return -EINVAL;
1759 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1760 snd_pcm_lib_write_transfer);
1761}
1762
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001763EXPORT_SYMBOL(snd_pcm_lib_write);
1764
Takashi Iwai877211f2005-11-17 13:59:38 +01001765static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 unsigned int hwoff,
1767 unsigned long data, unsigned int off,
1768 snd_pcm_uframes_t frames)
1769{
Takashi Iwai877211f2005-11-17 13:59:38 +01001770 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 int err;
1772 void __user **bufs = (void __user **)data;
1773 int channels = runtime->channels;
1774 int c;
1775 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001776 if (snd_BUG_ON(!substream->ops->silence))
1777 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 for (c = 0; c < channels; ++c, ++bufs) {
1779 if (*bufs == NULL) {
1780 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1781 return err;
1782 } else {
1783 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1784 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1785 return err;
1786 }
1787 }
1788 } else {
1789 /* default transfer behaviour */
1790 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 for (c = 0; c < channels; ++c, ++bufs) {
1792 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1793 if (*bufs == NULL) {
1794 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1795 } else {
1796 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1797 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1798 return -EFAULT;
1799 }
1800 }
1801 }
1802 return 0;
1803}
1804
Takashi Iwai877211f2005-11-17 13:59:38 +01001805snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 void __user **bufs,
1807 snd_pcm_uframes_t frames)
1808{
Takashi Iwai877211f2005-11-17 13:59:38 +01001809 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001811 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001813 err = pcm_sanity_check(substream);
1814 if (err < 0)
1815 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001817 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1820 return -EINVAL;
1821 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1822 nonblock, snd_pcm_lib_writev_transfer);
1823}
1824
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001825EXPORT_SYMBOL(snd_pcm_lib_writev);
1826
Takashi Iwai877211f2005-11-17 13:59:38 +01001827static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 unsigned int hwoff,
1829 unsigned long data, unsigned int off,
1830 snd_pcm_uframes_t frames)
1831{
Takashi Iwai877211f2005-11-17 13:59:38 +01001832 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 int err;
1834 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1835 if (substream->ops->copy) {
1836 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1837 return err;
1838 } else {
1839 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1841 return -EFAULT;
1842 }
1843 return 0;
1844}
1845
Takashi Iwai877211f2005-11-17 13:59:38 +01001846static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 unsigned long data,
1848 snd_pcm_uframes_t size,
1849 int nonblock,
1850 transfer_f transfer)
1851{
Takashi Iwai877211f2005-11-17 13:59:38 +01001852 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 snd_pcm_uframes_t xfer = 0;
1854 snd_pcm_uframes_t offset = 0;
1855 int err = 0;
1856
1857 if (size == 0)
1858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 snd_pcm_stream_lock_irq(substream);
1861 switch (runtime->status->state) {
1862 case SNDRV_PCM_STATE_PREPARED:
1863 if (size >= runtime->start_threshold) {
1864 err = snd_pcm_start(substream);
1865 if (err < 0)
1866 goto _end_unlock;
1867 }
1868 break;
1869 case SNDRV_PCM_STATE_DRAINING:
1870 case SNDRV_PCM_STATE_RUNNING:
1871 case SNDRV_PCM_STATE_PAUSED:
1872 break;
1873 case SNDRV_PCM_STATE_XRUN:
1874 err = -EPIPE;
1875 goto _end_unlock;
1876 case SNDRV_PCM_STATE_SUSPENDED:
1877 err = -ESTRPIPE;
1878 goto _end_unlock;
1879 default:
1880 err = -EBADFD;
1881 goto _end_unlock;
1882 }
1883
1884 while (size > 0) {
1885 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1886 snd_pcm_uframes_t avail;
1887 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001888 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001891 if (!avail) {
1892 if (runtime->status->state ==
1893 SNDRV_PCM_STATE_DRAINING) {
1894 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 goto _end_unlock;
1896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (nonblock) {
1898 err = -EAGAIN;
1899 goto _end_unlock;
1900 }
Takashi Iwai13075512008-01-08 18:08:14 +01001901 err = wait_for_avail_min(substream, &avail);
1902 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01001904 if (!avail)
1905 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 frames = size > avail ? avail : size;
1908 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1909 if (frames > cont)
1910 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001911 if (snd_BUG_ON(!frames)) {
1912 snd_pcm_stream_unlock_irq(substream);
1913 return -EINVAL;
1914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 appl_ptr = runtime->control->appl_ptr;
1916 appl_ofs = appl_ptr % runtime->buffer_size;
1917 snd_pcm_stream_unlock_irq(substream);
1918 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1919 goto _end;
1920 snd_pcm_stream_lock_irq(substream);
1921 switch (runtime->status->state) {
1922 case SNDRV_PCM_STATE_XRUN:
1923 err = -EPIPE;
1924 goto _end_unlock;
1925 case SNDRV_PCM_STATE_SUSPENDED:
1926 err = -ESTRPIPE;
1927 goto _end_unlock;
1928 default:
1929 break;
1930 }
1931 appl_ptr += frames;
1932 if (appl_ptr >= runtime->boundary)
1933 appl_ptr -= runtime->boundary;
1934 runtime->control->appl_ptr = appl_ptr;
1935 if (substream->ops->ack)
1936 substream->ops->ack(substream);
1937
1938 offset += frames;
1939 size -= frames;
1940 xfer += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942 _end_unlock:
1943 snd_pcm_stream_unlock_irq(substream);
1944 _end:
1945 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1946}
1947
Takashi Iwai877211f2005-11-17 13:59:38 +01001948snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Takashi Iwai877211f2005-11-17 13:59:38 +01001950 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001952 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001954 err = pcm_sanity_check(substream);
1955 if (err < 0)
1956 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001958 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1960 return -EINVAL;
1961 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
1962}
1963
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001964EXPORT_SYMBOL(snd_pcm_lib_read);
1965
Takashi Iwai877211f2005-11-17 13:59:38 +01001966static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 unsigned int hwoff,
1968 unsigned long data, unsigned int off,
1969 snd_pcm_uframes_t frames)
1970{
Takashi Iwai877211f2005-11-17 13:59:38 +01001971 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 int err;
1973 void __user **bufs = (void __user **)data;
1974 int channels = runtime->channels;
1975 int c;
1976 if (substream->ops->copy) {
1977 for (c = 0; c < channels; ++c, ++bufs) {
1978 char __user *buf;
1979 if (*bufs == NULL)
1980 continue;
1981 buf = *bufs + samples_to_bytes(runtime, off);
1982 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1983 return err;
1984 }
1985 } else {
1986 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 for (c = 0; c < channels; ++c, ++bufs) {
1988 char *hwbuf;
1989 char __user *buf;
1990 if (*bufs == NULL)
1991 continue;
1992
1993 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1994 buf = *bufs + samples_to_bytes(runtime, off);
1995 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
1996 return -EFAULT;
1997 }
1998 }
1999 return 0;
2000}
2001
Takashi Iwai877211f2005-11-17 13:59:38 +01002002snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 void __user **bufs,
2004 snd_pcm_uframes_t frames)
2005{
Takashi Iwai877211f2005-11-17 13:59:38 +01002006 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002008 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002010 err = pcm_sanity_check(substream);
2011 if (err < 0)
2012 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2015 return -EBADFD;
2016
Takashi Iwai0df63e42006-04-28 15:13:41 +02002017 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2019 return -EINVAL;
2020 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2021}
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023EXPORT_SYMBOL(snd_pcm_lib_readv);