blob: 92ed6d819225c48fb4f086796148afe30c166cde [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;
224 if (hw_ptr_interrupt == runtime->boundary)
225 hw_ptr_interrupt = 0;
226 if (delta < 0) {
227 delta += runtime->buffer_size;
228 if (delta < 0) {
229 hw_ptr_error(substream,
230 "Unexpected hw_pointer value "
231 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
232 substream->stream, (long)pos,
233 (long)hw_ptr_interrupt);
234 /* rebase to interrupt position */
235 hw_base = new_hw_ptr = hw_ptr_interrupt;
236 delta = 0;
237 } else {
238 hw_base += runtime->buffer_size;
239 if (hw_base == runtime->boundary)
240 hw_base = 0;
241 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100244 if (delta > runtime->period_size) {
245 hw_ptr_error(substream,
246 "Lost interrupts? "
247 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
248 substream->stream, (long)delta,
249 (long)hw_ptr_interrupt);
250 /* rebase hw_ptr_interrupt */
251 hw_ptr_interrupt =
252 new_hw_ptr - new_hw_ptr % runtime->period_size;
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
255 runtime->silence_size > 0)
256 snd_pcm_playback_silence(substream, new_hw_ptr);
257
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100258 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 runtime->status->hw_ptr = new_hw_ptr;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100260 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 return snd_pcm_update_hw_ptr_post(substream, runtime);
263}
264
265/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100266int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Takashi Iwai877211f2005-11-17 13:59:38 +0100268 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 snd_pcm_uframes_t pos;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100270 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 snd_pcm_sframes_t delta;
272
273 old_hw_ptr = runtime->status->hw_ptr;
274 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
275 if (pos == SNDRV_PCM_POS_XRUN) {
276 xrun(substream);
277 return -EPIPE;
278 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100279 hw_base = runtime->hw_ptr_base;
280 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100282 delta = new_hw_ptr - old_hw_ptr;
283 if (delta < 0) {
284 delta += runtime->buffer_size;
285 if (delta < 0) {
286 hw_ptr_error(substream,
287 "Unexpected hw_pointer value [2] "
288 "(stream=%i, pos=%ld, old_ptr=%ld)\n",
289 substream->stream, (long)pos,
290 (long)old_hw_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return 0;
292 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100293 hw_base += runtime->buffer_size;
294 if (hw_base == runtime->boundary)
295 hw_base = 0;
296 new_hw_ptr = hw_base + pos;
297 }
298 if (delta > runtime->period_size && runtime->periods > 1) {
299 hw_ptr_error(substream,
300 "hw_ptr skipping! "
301 "(pos=%ld, delta=%ld, period=%ld)\n",
302 (long)pos, (long)delta,
303 (long)runtime->period_size);
304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
307 runtime->silence_size > 0)
308 snd_pcm_playback_silence(substream, new_hw_ptr);
309
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100310 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 runtime->status->hw_ptr = new_hw_ptr;
312
313 return snd_pcm_update_hw_ptr_post(substream, runtime);
314}
315
316/**
317 * snd_pcm_set_ops - set the PCM operators
318 * @pcm: the pcm instance
319 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
320 * @ops: the operator table
321 *
322 * Sets the given PCM operators to the pcm instance.
323 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100324void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Takashi Iwai877211f2005-11-17 13:59:38 +0100326 struct snd_pcm_str *stream = &pcm->streams[direction];
327 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 for (substream = stream->substream; substream != NULL; substream = substream->next)
330 substream->ops = ops;
331}
332
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200333EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335/**
336 * snd_pcm_sync - set the PCM sync id
337 * @substream: the pcm substream
338 *
339 * Sets the PCM sync identifier for the card.
340 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100341void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Takashi Iwai877211f2005-11-17 13:59:38 +0100343 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 runtime->sync.id32[0] = substream->pcm->card->number;
346 runtime->sync.id32[1] = -1;
347 runtime->sync.id32[2] = -1;
348 runtime->sync.id32[3] = -1;
349}
350
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200351EXPORT_SYMBOL(snd_pcm_set_sync);
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353/*
354 * Standard ioctl routine
355 */
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357static inline unsigned int div32(unsigned int a, unsigned int b,
358 unsigned int *r)
359{
360 if (b == 0) {
361 *r = 0;
362 return UINT_MAX;
363 }
364 *r = a % b;
365 return a / b;
366}
367
368static inline unsigned int div_down(unsigned int a, unsigned int b)
369{
370 if (b == 0)
371 return UINT_MAX;
372 return a / b;
373}
374
375static inline unsigned int div_up(unsigned int a, unsigned int b)
376{
377 unsigned int r;
378 unsigned int q;
379 if (b == 0)
380 return UINT_MAX;
381 q = div32(a, b, &r);
382 if (r)
383 ++q;
384 return q;
385}
386
387static inline unsigned int mul(unsigned int a, unsigned int b)
388{
389 if (a == 0)
390 return 0;
391 if (div_down(UINT_MAX, a) < b)
392 return UINT_MAX;
393 return a * b;
394}
395
396static inline unsigned int muldiv32(unsigned int a, unsigned int b,
397 unsigned int c, unsigned int *r)
398{
399 u_int64_t n = (u_int64_t) a * b;
400 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200401 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 *r = 0;
403 return UINT_MAX;
404 }
405 div64_32(&n, c, r);
406 if (n >= UINT_MAX) {
407 *r = 0;
408 return UINT_MAX;
409 }
410 return n;
411}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413/**
414 * snd_interval_refine - refine the interval value of configurator
415 * @i: the interval value to refine
416 * @v: the interval value to refer to
417 *
418 * Refines the interval value with the reference value.
419 * The interval is changed to the range satisfying both intervals.
420 * The interval status (min, max, integer, etc.) are evaluated.
421 *
422 * Returns non-zero if the value is changed, zero if not changed.
423 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100424int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200427 if (snd_BUG_ON(snd_interval_empty(i)))
428 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (i->min < v->min) {
430 i->min = v->min;
431 i->openmin = v->openmin;
432 changed = 1;
433 } else if (i->min == v->min && !i->openmin && v->openmin) {
434 i->openmin = 1;
435 changed = 1;
436 }
437 if (i->max > v->max) {
438 i->max = v->max;
439 i->openmax = v->openmax;
440 changed = 1;
441 } else if (i->max == v->max && !i->openmax && v->openmax) {
442 i->openmax = 1;
443 changed = 1;
444 }
445 if (!i->integer && v->integer) {
446 i->integer = 1;
447 changed = 1;
448 }
449 if (i->integer) {
450 if (i->openmin) {
451 i->min++;
452 i->openmin = 0;
453 }
454 if (i->openmax) {
455 i->max--;
456 i->openmax = 0;
457 }
458 } else if (!i->openmin && !i->openmax && i->min == i->max)
459 i->integer = 1;
460 if (snd_interval_checkempty(i)) {
461 snd_interval_none(i);
462 return -EINVAL;
463 }
464 return changed;
465}
466
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200467EXPORT_SYMBOL(snd_interval_refine);
468
Takashi Iwai877211f2005-11-17 13:59:38 +0100469static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200471 if (snd_BUG_ON(snd_interval_empty(i)))
472 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (snd_interval_single(i))
474 return 0;
475 i->max = i->min;
476 i->openmax = i->openmin;
477 if (i->openmax)
478 i->max++;
479 return 1;
480}
481
Takashi Iwai877211f2005-11-17 13:59:38 +0100482static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200484 if (snd_BUG_ON(snd_interval_empty(i)))
485 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (snd_interval_single(i))
487 return 0;
488 i->min = i->max;
489 i->openmin = i->openmax;
490 if (i->openmin)
491 i->min--;
492 return 1;
493}
494
Takashi Iwai877211f2005-11-17 13:59:38 +0100495void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 if (a->empty || b->empty) {
498 snd_interval_none(c);
499 return;
500 }
501 c->empty = 0;
502 c->min = mul(a->min, b->min);
503 c->openmin = (a->openmin || b->openmin);
504 c->max = mul(a->max, b->max);
505 c->openmax = (a->openmax || b->openmax);
506 c->integer = (a->integer && b->integer);
507}
508
509/**
510 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200511 * @a: dividend
512 * @b: divisor
513 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 *
515 * c = a / b
516 *
517 * Returns non-zero if the value is changed, zero if not changed.
518 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100519void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 unsigned int r;
522 if (a->empty || b->empty) {
523 snd_interval_none(c);
524 return;
525 }
526 c->empty = 0;
527 c->min = div32(a->min, b->max, &r);
528 c->openmin = (r || a->openmin || b->openmax);
529 if (b->min > 0) {
530 c->max = div32(a->max, b->min, &r);
531 if (r) {
532 c->max++;
533 c->openmax = 1;
534 } else
535 c->openmax = (a->openmax || b->openmin);
536 } else {
537 c->max = UINT_MAX;
538 c->openmax = 0;
539 }
540 c->integer = 0;
541}
542
543/**
544 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200545 * @a: dividend 1
546 * @b: dividend 2
547 * @k: divisor (as integer)
548 * @c: result
549 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 * c = a * b / k
551 *
552 * Returns non-zero if the value is changed, zero if not changed.
553 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100554void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
555 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 unsigned int r;
558 if (a->empty || b->empty) {
559 snd_interval_none(c);
560 return;
561 }
562 c->empty = 0;
563 c->min = muldiv32(a->min, b->min, k, &r);
564 c->openmin = (r || a->openmin || b->openmin);
565 c->max = muldiv32(a->max, b->max, k, &r);
566 if (r) {
567 c->max++;
568 c->openmax = 1;
569 } else
570 c->openmax = (a->openmax || b->openmax);
571 c->integer = 0;
572}
573
574/**
575 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200576 * @a: dividend 1
577 * @k: dividend 2 (as integer)
578 * @b: divisor
579 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 *
581 * c = a * k / b
582 *
583 * Returns non-zero if the value is changed, zero if not changed.
584 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100585void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
586 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 unsigned int r;
589 if (a->empty || b->empty) {
590 snd_interval_none(c);
591 return;
592 }
593 c->empty = 0;
594 c->min = muldiv32(a->min, k, b->max, &r);
595 c->openmin = (r || a->openmin || b->openmax);
596 if (b->min > 0) {
597 c->max = muldiv32(a->max, k, b->min, &r);
598 if (r) {
599 c->max++;
600 c->openmax = 1;
601 } else
602 c->openmax = (a->openmax || b->openmin);
603 } else {
604 c->max = UINT_MAX;
605 c->openmax = 0;
606 }
607 c->integer = 0;
608}
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/* ---- */
611
612
613/**
614 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200615 * @i: interval to refine
616 * @rats_count: number of ratnum_t
617 * @rats: ratnum_t array
618 * @nump: pointer to store the resultant numerator
619 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 *
621 * Returns non-zero if the value is changed, zero if not changed.
622 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100623int snd_interval_ratnum(struct snd_interval *i,
624 unsigned int rats_count, struct snd_ratnum *rats,
625 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 unsigned int best_num, best_diff, best_den;
628 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100629 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 int err;
631
632 best_num = best_den = best_diff = 0;
633 for (k = 0; k < rats_count; ++k) {
634 unsigned int num = rats[k].num;
635 unsigned int den;
636 unsigned int q = i->min;
637 int diff;
638 if (q == 0)
639 q = 1;
640 den = div_down(num, q);
641 if (den < rats[k].den_min)
642 continue;
643 if (den > rats[k].den_max)
644 den = rats[k].den_max;
645 else {
646 unsigned int r;
647 r = (den - rats[k].den_min) % rats[k].den_step;
648 if (r != 0)
649 den -= r;
650 }
651 diff = num - q * den;
652 if (best_num == 0 ||
653 diff * best_den < best_diff * den) {
654 best_diff = diff;
655 best_den = den;
656 best_num = num;
657 }
658 }
659 if (best_den == 0) {
660 i->empty = 1;
661 return -EINVAL;
662 }
663 t.min = div_down(best_num, best_den);
664 t.openmin = !!(best_num % best_den);
665
666 best_num = best_den = best_diff = 0;
667 for (k = 0; k < rats_count; ++k) {
668 unsigned int num = rats[k].num;
669 unsigned int den;
670 unsigned int q = i->max;
671 int diff;
672 if (q == 0) {
673 i->empty = 1;
674 return -EINVAL;
675 }
676 den = div_up(num, q);
677 if (den > rats[k].den_max)
678 continue;
679 if (den < rats[k].den_min)
680 den = rats[k].den_min;
681 else {
682 unsigned int r;
683 r = (den - rats[k].den_min) % rats[k].den_step;
684 if (r != 0)
685 den += rats[k].den_step - r;
686 }
687 diff = q * den - num;
688 if (best_num == 0 ||
689 diff * best_den < best_diff * den) {
690 best_diff = diff;
691 best_den = den;
692 best_num = num;
693 }
694 }
695 if (best_den == 0) {
696 i->empty = 1;
697 return -EINVAL;
698 }
699 t.max = div_up(best_num, best_den);
700 t.openmax = !!(best_num % best_den);
701 t.integer = 0;
702 err = snd_interval_refine(i, &t);
703 if (err < 0)
704 return err;
705
706 if (snd_interval_single(i)) {
707 if (nump)
708 *nump = best_num;
709 if (denp)
710 *denp = best_den;
711 }
712 return err;
713}
714
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200715EXPORT_SYMBOL(snd_interval_ratnum);
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717/**
718 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200719 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100720 * @rats_count: number of struct ratden
721 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200722 * @nump: pointer to store the resultant numerator
723 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 *
725 * Returns non-zero if the value is changed, zero if not changed.
726 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100727static int snd_interval_ratden(struct snd_interval *i,
728 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 unsigned int *nump, unsigned int *denp)
730{
731 unsigned int best_num, best_diff, best_den;
732 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100733 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int err;
735
736 best_num = best_den = best_diff = 0;
737 for (k = 0; k < rats_count; ++k) {
738 unsigned int num;
739 unsigned int den = rats[k].den;
740 unsigned int q = i->min;
741 int diff;
742 num = mul(q, den);
743 if (num > rats[k].num_max)
744 continue;
745 if (num < rats[k].num_min)
746 num = rats[k].num_max;
747 else {
748 unsigned int r;
749 r = (num - rats[k].num_min) % rats[k].num_step;
750 if (r != 0)
751 num += rats[k].num_step - r;
752 }
753 diff = num - q * den;
754 if (best_num == 0 ||
755 diff * best_den < best_diff * den) {
756 best_diff = diff;
757 best_den = den;
758 best_num = num;
759 }
760 }
761 if (best_den == 0) {
762 i->empty = 1;
763 return -EINVAL;
764 }
765 t.min = div_down(best_num, best_den);
766 t.openmin = !!(best_num % best_den);
767
768 best_num = best_den = best_diff = 0;
769 for (k = 0; k < rats_count; ++k) {
770 unsigned int num;
771 unsigned int den = rats[k].den;
772 unsigned int q = i->max;
773 int diff;
774 num = mul(q, den);
775 if (num < rats[k].num_min)
776 continue;
777 if (num > rats[k].num_max)
778 num = rats[k].num_max;
779 else {
780 unsigned int r;
781 r = (num - rats[k].num_min) % rats[k].num_step;
782 if (r != 0)
783 num -= r;
784 }
785 diff = q * den - num;
786 if (best_num == 0 ||
787 diff * best_den < best_diff * den) {
788 best_diff = diff;
789 best_den = den;
790 best_num = num;
791 }
792 }
793 if (best_den == 0) {
794 i->empty = 1;
795 return -EINVAL;
796 }
797 t.max = div_up(best_num, best_den);
798 t.openmax = !!(best_num % best_den);
799 t.integer = 0;
800 err = snd_interval_refine(i, &t);
801 if (err < 0)
802 return err;
803
804 if (snd_interval_single(i)) {
805 if (nump)
806 *nump = best_num;
807 if (denp)
808 *denp = best_den;
809 }
810 return err;
811}
812
813/**
814 * snd_interval_list - refine the interval value from the list
815 * @i: the interval value to refine
816 * @count: the number of elements in the list
817 * @list: the value list
818 * @mask: the bit-mask to evaluate
819 *
820 * Refines the interval value from the list.
821 * When mask is non-zero, only the elements corresponding to bit 1 are
822 * evaluated.
823 *
824 * Returns non-zero if the value is changed, zero if not changed.
825 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100826int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 unsigned int k;
829 int changed = 0;
Takashi Iwai0981a262007-02-01 14:53:49 +0100830
831 if (!count) {
832 i->empty = 1;
833 return -EINVAL;
834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 for (k = 0; k < count; k++) {
836 if (mask && !(mask & (1 << k)))
837 continue;
838 if (i->min == list[k] && !i->openmin)
839 goto _l1;
840 if (i->min < list[k]) {
841 i->min = list[k];
842 i->openmin = 0;
843 changed = 1;
844 goto _l1;
845 }
846 }
847 i->empty = 1;
848 return -EINVAL;
849 _l1:
850 for (k = count; k-- > 0;) {
851 if (mask && !(mask & (1 << k)))
852 continue;
853 if (i->max == list[k] && !i->openmax)
854 goto _l2;
855 if (i->max > list[k]) {
856 i->max = list[k];
857 i->openmax = 0;
858 changed = 1;
859 goto _l2;
860 }
861 }
862 i->empty = 1;
863 return -EINVAL;
864 _l2:
865 if (snd_interval_checkempty(i)) {
866 i->empty = 1;
867 return -EINVAL;
868 }
869 return changed;
870}
871
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200872EXPORT_SYMBOL(snd_interval_list);
873
Takashi Iwai877211f2005-11-17 13:59:38 +0100874static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 unsigned int n;
877 int changed = 0;
878 n = (i->min - min) % step;
879 if (n != 0 || i->openmin) {
880 i->min += step - n;
881 changed = 1;
882 }
883 n = (i->max - min) % step;
884 if (n != 0 || i->openmax) {
885 i->max -= n;
886 changed = 1;
887 }
888 if (snd_interval_checkempty(i)) {
889 i->empty = 1;
890 return -EINVAL;
891 }
892 return changed;
893}
894
895/* Info constraints helpers */
896
897/**
898 * snd_pcm_hw_rule_add - add the hw-constraint rule
899 * @runtime: the pcm runtime instance
900 * @cond: condition bits
901 * @var: the variable to evaluate
902 * @func: the evaluation function
903 * @private: the private data pointer passed to function
904 * @dep: the dependent variables
905 *
906 * Returns zero if successful, or a negative error code on failure.
907 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100908int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 int var,
910 snd_pcm_hw_rule_func_t func, void *private,
911 int dep, ...)
912{
Takashi Iwai877211f2005-11-17 13:59:38 +0100913 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
914 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 unsigned int k;
916 va_list args;
917 va_start(args, dep);
918 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100919 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 unsigned int new_rules = constrs->rules_all + 16;
921 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
922 if (!new)
923 return -ENOMEM;
924 if (constrs->rules) {
925 memcpy(new, constrs->rules,
926 constrs->rules_num * sizeof(*c));
927 kfree(constrs->rules);
928 }
929 constrs->rules = new;
930 constrs->rules_all = new_rules;
931 }
932 c = &constrs->rules[constrs->rules_num];
933 c->cond = cond;
934 c->func = func;
935 c->var = var;
936 c->private = private;
937 k = 0;
938 while (1) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200939 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
940 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 c->deps[k++] = dep;
942 if (dep < 0)
943 break;
944 dep = va_arg(args, int);
945 }
946 constrs->rules_num++;
947 va_end(args);
948 return 0;
949}
950
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200951EXPORT_SYMBOL(snd_pcm_hw_rule_add);
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700954 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +0200955 * @runtime: PCM runtime instance
956 * @var: hw_params variable to apply the mask
957 * @mask: the bitmap mask
958 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700959 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100961int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 u_int32_t mask)
963{
Takashi Iwai877211f2005-11-17 13:59:38 +0100964 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
965 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 *maskp->bits &= mask;
967 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
968 if (*maskp->bits == 0)
969 return -EINVAL;
970 return 0;
971}
972
973/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700974 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +0200975 * @runtime: PCM runtime instance
976 * @var: hw_params variable to apply the mask
977 * @mask: the 64bit bitmap mask
978 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700979 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100981int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 u_int64_t mask)
983{
Takashi Iwai877211f2005-11-17 13:59:38 +0100984 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
985 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 maskp->bits[0] &= (u_int32_t)mask;
987 maskp->bits[1] &= (u_int32_t)(mask >> 32);
988 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
989 if (! maskp->bits[0] && ! maskp->bits[1])
990 return -EINVAL;
991 return 0;
992}
993
994/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700995 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +0200996 * @runtime: PCM runtime instance
997 * @var: hw_params variable to apply the integer constraint
998 *
999 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001001int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Takashi Iwai877211f2005-11-17 13:59:38 +01001003 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return snd_interval_setinteger(constrs_interval(constrs, var));
1005}
1006
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001007EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001010 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001011 * @runtime: PCM runtime instance
1012 * @var: hw_params variable to apply the range
1013 * @min: the minimal value
1014 * @max: the maximal value
1015 *
1016 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001018int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 unsigned int min, unsigned int max)
1020{
Takashi Iwai877211f2005-11-17 13:59:38 +01001021 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1022 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 t.min = min;
1024 t.max = max;
1025 t.openmin = t.openmax = 0;
1026 t.integer = 0;
1027 return snd_interval_refine(constrs_interval(constrs, var), &t);
1028}
1029
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001030EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1031
Takashi Iwai877211f2005-11-17 13:59:38 +01001032static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1033 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Takashi Iwai877211f2005-11-17 13:59:38 +01001035 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1037}
1038
1039
1040/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001041 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001042 * @runtime: PCM runtime instance
1043 * @cond: condition bits
1044 * @var: hw_params variable to apply the list constraint
1045 * @l: list
1046 *
1047 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001049int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 unsigned int cond,
1051 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001052 struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 return snd_pcm_hw_rule_add(runtime, cond, var,
1055 snd_pcm_hw_rule_list, l,
1056 var, -1);
1057}
1058
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001059EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1060
Takashi Iwai877211f2005-11-17 13:59:38 +01001061static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1062 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Takashi Iwai877211f2005-11-17 13:59:38 +01001064 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 unsigned int num = 0, den = 0;
1066 int err;
1067 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1068 r->nrats, r->rats, &num, &den);
1069 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1070 params->rate_num = num;
1071 params->rate_den = den;
1072 }
1073 return err;
1074}
1075
1076/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001077 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001078 * @runtime: PCM runtime instance
1079 * @cond: condition bits
1080 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001081 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001083int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 unsigned int cond,
1085 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001086 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 return snd_pcm_hw_rule_add(runtime, cond, var,
1089 snd_pcm_hw_rule_ratnums, r,
1090 var, -1);
1091}
1092
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001093EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1094
Takashi Iwai877211f2005-11-17 13:59:38 +01001095static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1096 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Takashi Iwai877211f2005-11-17 13:59:38 +01001098 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 unsigned int num = 0, den = 0;
1100 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1101 r->nrats, r->rats, &num, &den);
1102 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1103 params->rate_num = num;
1104 params->rate_den = den;
1105 }
1106 return err;
1107}
1108
1109/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001110 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001111 * @runtime: PCM runtime instance
1112 * @cond: condition bits
1113 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001114 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001116int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 unsigned int cond,
1118 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001119 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 return snd_pcm_hw_rule_add(runtime, cond, var,
1122 snd_pcm_hw_rule_ratdens, r,
1123 var, -1);
1124}
1125
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001126EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1127
Takashi Iwai877211f2005-11-17 13:59:38 +01001128static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1129 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 unsigned int l = (unsigned long) rule->private;
1132 int width = l & 0xffff;
1133 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001134 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (snd_interval_single(i) && snd_interval_value(i) == width)
1136 params->msbits = msbits;
1137 return 0;
1138}
1139
1140/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001141 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001142 * @runtime: PCM runtime instance
1143 * @cond: condition bits
1144 * @width: sample bits width
1145 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001147int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 unsigned int cond,
1149 unsigned int width,
1150 unsigned int msbits)
1151{
1152 unsigned long l = (msbits << 16) | width;
1153 return snd_pcm_hw_rule_add(runtime, cond, -1,
1154 snd_pcm_hw_rule_msbits,
1155 (void*) l,
1156 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1157}
1158
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001159EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1160
Takashi Iwai877211f2005-11-17 13:59:38 +01001161static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1162 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 unsigned long step = (unsigned long) rule->private;
1165 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1166}
1167
1168/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001169 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001170 * @runtime: PCM runtime instance
1171 * @cond: condition bits
1172 * @var: hw_params variable to apply the step constraint
1173 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001175int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 unsigned int cond,
1177 snd_pcm_hw_param_t var,
1178 unsigned long step)
1179{
1180 return snd_pcm_hw_rule_add(runtime, cond, var,
1181 snd_pcm_hw_rule_step, (void *) step,
1182 var, -1);
1183}
1184
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001185EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1186
Takashi Iwai877211f2005-11-17 13:59:38 +01001187static 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 -07001188{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001189 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1191 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1192 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1193 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1194 };
1195 return snd_interval_list(hw_param_interval(params, rule->var),
1196 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1197}
1198
1199/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001200 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001201 * @runtime: PCM runtime instance
1202 * @cond: condition bits
1203 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001205int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 unsigned int cond,
1207 snd_pcm_hw_param_t var)
1208{
1209 return snd_pcm_hw_rule_add(runtime, cond, var,
1210 snd_pcm_hw_rule_pow2, NULL,
1211 var, -1);
1212}
1213
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001214EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1215
Takashi Iwai877211f2005-11-17 13:59:38 +01001216static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001217 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 if (hw_is_mask(var)) {
1220 snd_mask_any(hw_param_mask(params, var));
1221 params->cmask |= 1 << var;
1222 params->rmask |= 1 << var;
1223 return;
1224 }
1225 if (hw_is_interval(var)) {
1226 snd_interval_any(hw_param_interval(params, var));
1227 params->cmask |= 1 << var;
1228 params->rmask |= 1 << var;
1229 return;
1230 }
1231 snd_BUG();
1232}
1233
Takashi Iwai877211f2005-11-17 13:59:38 +01001234void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 unsigned int k;
1237 memset(params, 0, sizeof(*params));
1238 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1239 _snd_pcm_hw_param_any(params, k);
1240 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1241 _snd_pcm_hw_param_any(params, k);
1242 params->info = ~0U;
1243}
1244
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001245EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001248 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001249 * @params: the hw_params instance
1250 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001251 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001253 * Return the value for field @var if it's fixed in configuration space
1254 * defined by @params. Return -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001256int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1257 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001260 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (!snd_mask_single(mask))
1262 return -EINVAL;
1263 if (dir)
1264 *dir = 0;
1265 return snd_mask_value(mask);
1266 }
1267 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001268 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (!snd_interval_single(i))
1270 return -EINVAL;
1271 if (dir)
1272 *dir = i->openmin;
1273 return snd_interval_value(i);
1274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return -EINVAL;
1276}
1277
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001278EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Takashi Iwai877211f2005-11-17 13:59:38 +01001280void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 snd_pcm_hw_param_t var)
1282{
1283 if (hw_is_mask(var)) {
1284 snd_mask_none(hw_param_mask(params, var));
1285 params->cmask |= 1 << var;
1286 params->rmask |= 1 << var;
1287 } else if (hw_is_interval(var)) {
1288 snd_interval_none(hw_param_interval(params, var));
1289 params->cmask |= 1 << var;
1290 params->rmask |= 1 << var;
1291 } else {
1292 snd_BUG();
1293 }
1294}
1295
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001296EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Takashi Iwai877211f2005-11-17 13:59:38 +01001298static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001299 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 int changed;
1302 if (hw_is_mask(var))
1303 changed = snd_mask_refine_first(hw_param_mask(params, var));
1304 else if (hw_is_interval(var))
1305 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001306 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (changed) {
1309 params->cmask |= 1 << var;
1310 params->rmask |= 1 << var;
1311 }
1312 return changed;
1313}
1314
1315
1316/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001317 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001318 * @pcm: PCM instance
1319 * @params: the hw_params instance
1320 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001321 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001323 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 * values > minimum. Reduce configuration space accordingly.
1325 * Return the minimum.
1326 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001327int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1328 struct snd_pcm_hw_params *params,
1329 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
1331 int changed = _snd_pcm_hw_param_first(params, var);
1332 if (changed < 0)
1333 return changed;
1334 if (params->rmask) {
1335 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001336 if (snd_BUG_ON(err < 0))
1337 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339 return snd_pcm_hw_param_value(params, var, dir);
1340}
1341
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001342EXPORT_SYMBOL(snd_pcm_hw_param_first);
1343
Takashi Iwai877211f2005-11-17 13:59:38 +01001344static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001345 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 int changed;
1348 if (hw_is_mask(var))
1349 changed = snd_mask_refine_last(hw_param_mask(params, var));
1350 else if (hw_is_interval(var))
1351 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001352 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (changed) {
1355 params->cmask |= 1 << var;
1356 params->rmask |= 1 << var;
1357 }
1358 return changed;
1359}
1360
1361
1362/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001363 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001364 * @pcm: PCM instance
1365 * @params: the hw_params instance
1366 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001367 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001369 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 * values < maximum. Reduce configuration space accordingly.
1371 * Return the maximum.
1372 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001373int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1374 struct snd_pcm_hw_params *params,
1375 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 int changed = _snd_pcm_hw_param_last(params, var);
1378 if (changed < 0)
1379 return changed;
1380 if (params->rmask) {
1381 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001382 if (snd_BUG_ON(err < 0))
1383 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385 return snd_pcm_hw_param_value(params, var, dir);
1386}
1387
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001388EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001391 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001392 * @pcm: PCM instance
1393 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001395 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 * The configuration chosen is that obtained fixing in this order:
1397 * first access, first format, first subformat, min channels,
1398 * min rate, min period time, max buffer size, min tick time
1399 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001400int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1401 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001403 static int vars[] = {
1404 SNDRV_PCM_HW_PARAM_ACCESS,
1405 SNDRV_PCM_HW_PARAM_FORMAT,
1406 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1407 SNDRV_PCM_HW_PARAM_CHANNELS,
1408 SNDRV_PCM_HW_PARAM_RATE,
1409 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1410 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1411 SNDRV_PCM_HW_PARAM_TICK_TIME,
1412 -1
1413 };
1414 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001416 for (v = vars; *v != -1; v++) {
1417 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1418 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1419 else
1420 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001421 if (snd_BUG_ON(err < 0))
1422 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return 0;
1425}
1426
Takashi Iwai877211f2005-11-17 13:59:38 +01001427static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 void *arg)
1429{
Takashi Iwai877211f2005-11-17 13:59:38 +01001430 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 unsigned long flags;
1432 snd_pcm_stream_lock_irqsave(substream, flags);
1433 if (snd_pcm_running(substream) &&
1434 snd_pcm_update_hw_ptr(substream) >= 0)
1435 runtime->status->hw_ptr %= runtime->buffer_size;
1436 else
1437 runtime->status->hw_ptr = 0;
1438 snd_pcm_stream_unlock_irqrestore(substream, flags);
1439 return 0;
1440}
1441
Takashi Iwai877211f2005-11-17 13:59:38 +01001442static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 void *arg)
1444{
Takashi Iwai877211f2005-11-17 13:59:38 +01001445 struct snd_pcm_channel_info *info = arg;
1446 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 int width;
1448 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1449 info->offset = -1;
1450 return 0;
1451 }
1452 width = snd_pcm_format_physical_width(runtime->format);
1453 if (width < 0)
1454 return width;
1455 info->offset = 0;
1456 switch (runtime->access) {
1457 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1458 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1459 info->first = info->channel * width;
1460 info->step = runtime->channels * width;
1461 break;
1462 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1463 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1464 {
1465 size_t size = runtime->dma_bytes / runtime->channels;
1466 info->first = info->channel * size * 8;
1467 info->step = width;
1468 break;
1469 }
1470 default:
1471 snd_BUG();
1472 break;
1473 }
1474 return 0;
1475}
1476
1477/**
1478 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1479 * @substream: the pcm substream instance
1480 * @cmd: ioctl command
1481 * @arg: ioctl argument
1482 *
1483 * Processes the generic ioctl commands for PCM.
1484 * Can be passed as the ioctl callback for PCM ops.
1485 *
1486 * Returns zero if successful, or a negative error code on failure.
1487 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001488int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 unsigned int cmd, void *arg)
1490{
1491 switch (cmd) {
1492 case SNDRV_PCM_IOCTL1_INFO:
1493 return 0;
1494 case SNDRV_PCM_IOCTL1_RESET:
1495 return snd_pcm_lib_ioctl_reset(substream, arg);
1496 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1497 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1498 }
1499 return -ENXIO;
1500}
1501
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001502EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504/**
1505 * snd_pcm_period_elapsed - update the pcm status for the next period
1506 * @substream: the pcm substream instance
1507 *
1508 * This function is called from the interrupt handler when the
1509 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001510 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 *
1512 * Even if more than one periods have elapsed since the last call, you
1513 * have to call this only once.
1514 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001515void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Takashi Iwai877211f2005-11-17 13:59:38 +01001517 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 unsigned long flags;
1519
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001520 if (PCM_RUNTIME_CHECK(substream))
1521 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 if (runtime->transfer_ack_begin)
1525 runtime->transfer_ack_begin(substream);
1526
1527 snd_pcm_stream_lock_irqsave(substream, flags);
1528 if (!snd_pcm_running(substream) ||
1529 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1530 goto _end;
1531
1532 if (substream->timer_running)
1533 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 _end:
1535 snd_pcm_stream_unlock_irqrestore(substream, flags);
1536 if (runtime->transfer_ack_end)
1537 runtime->transfer_ack_end(substream);
1538 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1539}
1540
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001541EXPORT_SYMBOL(snd_pcm_period_elapsed);
1542
Takashi Iwai13075512008-01-08 18:08:14 +01001543/*
1544 * Wait until avail_min data becomes available
1545 * Returns a negative error code if any error occurs during operation.
1546 * The available space is stored on availp. When err = 0 and avail = 0
1547 * on the capture stream, it indicates the stream is in DRAINING state.
1548 */
1549static int wait_for_avail_min(struct snd_pcm_substream *substream,
1550 snd_pcm_uframes_t *availp)
1551{
1552 struct snd_pcm_runtime *runtime = substream->runtime;
1553 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1554 wait_queue_t wait;
1555 int err = 0;
1556 snd_pcm_uframes_t avail = 0;
1557 long tout;
1558
1559 init_waitqueue_entry(&wait, current);
1560 add_wait_queue(&runtime->sleep, &wait);
1561 for (;;) {
1562 if (signal_pending(current)) {
1563 err = -ERESTARTSYS;
1564 break;
1565 }
1566 set_current_state(TASK_INTERRUPTIBLE);
1567 snd_pcm_stream_unlock_irq(substream);
1568 tout = schedule_timeout(msecs_to_jiffies(10000));
1569 snd_pcm_stream_lock_irq(substream);
1570 switch (runtime->status->state) {
1571 case SNDRV_PCM_STATE_SUSPENDED:
1572 err = -ESTRPIPE;
1573 goto _endloop;
1574 case SNDRV_PCM_STATE_XRUN:
1575 err = -EPIPE;
1576 goto _endloop;
1577 case SNDRV_PCM_STATE_DRAINING:
1578 if (is_playback)
1579 err = -EPIPE;
1580 else
1581 avail = 0; /* indicate draining */
1582 goto _endloop;
1583 case SNDRV_PCM_STATE_OPEN:
1584 case SNDRV_PCM_STATE_SETUP:
1585 case SNDRV_PCM_STATE_DISCONNECTED:
1586 err = -EBADFD;
1587 goto _endloop;
1588 }
1589 if (!tout) {
1590 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1591 is_playback ? "playback" : "capture");
1592 err = -EIO;
1593 break;
1594 }
1595 if (is_playback)
1596 avail = snd_pcm_playback_avail(runtime);
1597 else
1598 avail = snd_pcm_capture_avail(runtime);
1599 if (avail >= runtime->control->avail_min)
1600 break;
1601 }
1602 _endloop:
1603 remove_wait_queue(&runtime->sleep, &wait);
1604 *availp = avail;
1605 return err;
1606}
1607
Takashi Iwai877211f2005-11-17 13:59:38 +01001608static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 unsigned int hwoff,
1610 unsigned long data, unsigned int off,
1611 snd_pcm_uframes_t frames)
1612{
Takashi Iwai877211f2005-11-17 13:59:38 +01001613 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 int err;
1615 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1616 if (substream->ops->copy) {
1617 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1618 return err;
1619 } else {
1620 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1622 return -EFAULT;
1623 }
1624 return 0;
1625}
1626
Takashi Iwai877211f2005-11-17 13:59:38 +01001627typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 unsigned long data, unsigned int off,
1629 snd_pcm_uframes_t size);
1630
Takashi Iwai877211f2005-11-17 13:59:38 +01001631static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 unsigned long data,
1633 snd_pcm_uframes_t size,
1634 int nonblock,
1635 transfer_f transfer)
1636{
Takashi Iwai877211f2005-11-17 13:59:38 +01001637 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 snd_pcm_uframes_t xfer = 0;
1639 snd_pcm_uframes_t offset = 0;
1640 int err = 0;
1641
1642 if (size == 0)
1643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 snd_pcm_stream_lock_irq(substream);
1646 switch (runtime->status->state) {
1647 case SNDRV_PCM_STATE_PREPARED:
1648 case SNDRV_PCM_STATE_RUNNING:
1649 case SNDRV_PCM_STATE_PAUSED:
1650 break;
1651 case SNDRV_PCM_STATE_XRUN:
1652 err = -EPIPE;
1653 goto _end_unlock;
1654 case SNDRV_PCM_STATE_SUSPENDED:
1655 err = -ESTRPIPE;
1656 goto _end_unlock;
1657 default:
1658 err = -EBADFD;
1659 goto _end_unlock;
1660 }
1661
1662 while (size > 0) {
1663 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1664 snd_pcm_uframes_t avail;
1665 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001666 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 snd_pcm_update_hw_ptr(substream);
1668 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001669 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (nonblock) {
1671 err = -EAGAIN;
1672 goto _end_unlock;
1673 }
Takashi Iwai13075512008-01-08 18:08:14 +01001674 err = wait_for_avail_min(substream, &avail);
1675 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 frames = size > avail ? avail : size;
1679 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1680 if (frames > cont)
1681 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001682 if (snd_BUG_ON(!frames)) {
1683 snd_pcm_stream_unlock_irq(substream);
1684 return -EINVAL;
1685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 appl_ptr = runtime->control->appl_ptr;
1687 appl_ofs = appl_ptr % runtime->buffer_size;
1688 snd_pcm_stream_unlock_irq(substream);
1689 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1690 goto _end;
1691 snd_pcm_stream_lock_irq(substream);
1692 switch (runtime->status->state) {
1693 case SNDRV_PCM_STATE_XRUN:
1694 err = -EPIPE;
1695 goto _end_unlock;
1696 case SNDRV_PCM_STATE_SUSPENDED:
1697 err = -ESTRPIPE;
1698 goto _end_unlock;
1699 default:
1700 break;
1701 }
1702 appl_ptr += frames;
1703 if (appl_ptr >= runtime->boundary)
1704 appl_ptr -= runtime->boundary;
1705 runtime->control->appl_ptr = appl_ptr;
1706 if (substream->ops->ack)
1707 substream->ops->ack(substream);
1708
1709 offset += frames;
1710 size -= frames;
1711 xfer += frames;
1712 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1713 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1714 err = snd_pcm_start(substream);
1715 if (err < 0)
1716 goto _end_unlock;
1717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719 _end_unlock:
1720 snd_pcm_stream_unlock_irq(substream);
1721 _end:
1722 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1723}
1724
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001725/* sanity-check for read/write methods */
1726static int pcm_sanity_check(struct snd_pcm_substream *substream)
1727{
1728 struct snd_pcm_runtime *runtime;
1729 if (PCM_RUNTIME_CHECK(substream))
1730 return -ENXIO;
1731 runtime = substream->runtime;
1732 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1733 return -EINVAL;
1734 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1735 return -EBADFD;
1736 return 0;
1737}
1738
Takashi Iwai877211f2005-11-17 13:59:38 +01001739snd_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 -07001740{
Takashi Iwai877211f2005-11-17 13:59:38 +01001741 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001743 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001745 err = pcm_sanity_check(substream);
1746 if (err < 0)
1747 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001749 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1752 runtime->channels > 1)
1753 return -EINVAL;
1754 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1755 snd_pcm_lib_write_transfer);
1756}
1757
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001758EXPORT_SYMBOL(snd_pcm_lib_write);
1759
Takashi Iwai877211f2005-11-17 13:59:38 +01001760static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 unsigned int hwoff,
1762 unsigned long data, unsigned int off,
1763 snd_pcm_uframes_t frames)
1764{
Takashi Iwai877211f2005-11-17 13:59:38 +01001765 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 int err;
1767 void __user **bufs = (void __user **)data;
1768 int channels = runtime->channels;
1769 int c;
1770 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001771 if (snd_BUG_ON(!substream->ops->silence))
1772 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 for (c = 0; c < channels; ++c, ++bufs) {
1774 if (*bufs == NULL) {
1775 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1776 return err;
1777 } else {
1778 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1779 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1780 return err;
1781 }
1782 }
1783 } else {
1784 /* default transfer behaviour */
1785 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 for (c = 0; c < channels; ++c, ++bufs) {
1787 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1788 if (*bufs == NULL) {
1789 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1790 } else {
1791 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1792 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1793 return -EFAULT;
1794 }
1795 }
1796 }
1797 return 0;
1798}
1799
Takashi Iwai877211f2005-11-17 13:59:38 +01001800snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 void __user **bufs,
1802 snd_pcm_uframes_t frames)
1803{
Takashi Iwai877211f2005-11-17 13:59:38 +01001804 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001806 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001808 err = pcm_sanity_check(substream);
1809 if (err < 0)
1810 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001812 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1815 return -EINVAL;
1816 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1817 nonblock, snd_pcm_lib_writev_transfer);
1818}
1819
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001820EXPORT_SYMBOL(snd_pcm_lib_writev);
1821
Takashi Iwai877211f2005-11-17 13:59:38 +01001822static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 unsigned int hwoff,
1824 unsigned long data, unsigned int off,
1825 snd_pcm_uframes_t frames)
1826{
Takashi Iwai877211f2005-11-17 13:59:38 +01001827 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 int err;
1829 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1830 if (substream->ops->copy) {
1831 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1832 return err;
1833 } else {
1834 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1836 return -EFAULT;
1837 }
1838 return 0;
1839}
1840
Takashi Iwai877211f2005-11-17 13:59:38 +01001841static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 unsigned long data,
1843 snd_pcm_uframes_t size,
1844 int nonblock,
1845 transfer_f transfer)
1846{
Takashi Iwai877211f2005-11-17 13:59:38 +01001847 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 snd_pcm_uframes_t xfer = 0;
1849 snd_pcm_uframes_t offset = 0;
1850 int err = 0;
1851
1852 if (size == 0)
1853 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 snd_pcm_stream_lock_irq(substream);
1856 switch (runtime->status->state) {
1857 case SNDRV_PCM_STATE_PREPARED:
1858 if (size >= runtime->start_threshold) {
1859 err = snd_pcm_start(substream);
1860 if (err < 0)
1861 goto _end_unlock;
1862 }
1863 break;
1864 case SNDRV_PCM_STATE_DRAINING:
1865 case SNDRV_PCM_STATE_RUNNING:
1866 case SNDRV_PCM_STATE_PAUSED:
1867 break;
1868 case SNDRV_PCM_STATE_XRUN:
1869 err = -EPIPE;
1870 goto _end_unlock;
1871 case SNDRV_PCM_STATE_SUSPENDED:
1872 err = -ESTRPIPE;
1873 goto _end_unlock;
1874 default:
1875 err = -EBADFD;
1876 goto _end_unlock;
1877 }
1878
1879 while (size > 0) {
1880 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1881 snd_pcm_uframes_t avail;
1882 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001883 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001886 if (!avail) {
1887 if (runtime->status->state ==
1888 SNDRV_PCM_STATE_DRAINING) {
1889 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 goto _end_unlock;
1891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (nonblock) {
1893 err = -EAGAIN;
1894 goto _end_unlock;
1895 }
Takashi Iwai13075512008-01-08 18:08:14 +01001896 err = wait_for_avail_min(substream, &avail);
1897 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01001899 if (!avail)
1900 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 frames = size > avail ? avail : size;
1903 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1904 if (frames > cont)
1905 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001906 if (snd_BUG_ON(!frames)) {
1907 snd_pcm_stream_unlock_irq(substream);
1908 return -EINVAL;
1909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 appl_ptr = runtime->control->appl_ptr;
1911 appl_ofs = appl_ptr % runtime->buffer_size;
1912 snd_pcm_stream_unlock_irq(substream);
1913 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1914 goto _end;
1915 snd_pcm_stream_lock_irq(substream);
1916 switch (runtime->status->state) {
1917 case SNDRV_PCM_STATE_XRUN:
1918 err = -EPIPE;
1919 goto _end_unlock;
1920 case SNDRV_PCM_STATE_SUSPENDED:
1921 err = -ESTRPIPE;
1922 goto _end_unlock;
1923 default:
1924 break;
1925 }
1926 appl_ptr += frames;
1927 if (appl_ptr >= runtime->boundary)
1928 appl_ptr -= runtime->boundary;
1929 runtime->control->appl_ptr = appl_ptr;
1930 if (substream->ops->ack)
1931 substream->ops->ack(substream);
1932
1933 offset += frames;
1934 size -= frames;
1935 xfer += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
1937 _end_unlock:
1938 snd_pcm_stream_unlock_irq(substream);
1939 _end:
1940 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1941}
1942
Takashi Iwai877211f2005-11-17 13:59:38 +01001943snd_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 -07001944{
Takashi Iwai877211f2005-11-17 13:59:38 +01001945 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001947 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001949 err = pcm_sanity_check(substream);
1950 if (err < 0)
1951 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001953 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1955 return -EINVAL;
1956 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
1957}
1958
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001959EXPORT_SYMBOL(snd_pcm_lib_read);
1960
Takashi Iwai877211f2005-11-17 13:59:38 +01001961static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 unsigned int hwoff,
1963 unsigned long data, unsigned int off,
1964 snd_pcm_uframes_t frames)
1965{
Takashi Iwai877211f2005-11-17 13:59:38 +01001966 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 int err;
1968 void __user **bufs = (void __user **)data;
1969 int channels = runtime->channels;
1970 int c;
1971 if (substream->ops->copy) {
1972 for (c = 0; c < channels; ++c, ++bufs) {
1973 char __user *buf;
1974 if (*bufs == NULL)
1975 continue;
1976 buf = *bufs + samples_to_bytes(runtime, off);
1977 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1978 return err;
1979 }
1980 } else {
1981 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 for (c = 0; c < channels; ++c, ++bufs) {
1983 char *hwbuf;
1984 char __user *buf;
1985 if (*bufs == NULL)
1986 continue;
1987
1988 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1989 buf = *bufs + samples_to_bytes(runtime, off);
1990 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
1991 return -EFAULT;
1992 }
1993 }
1994 return 0;
1995}
1996
Takashi Iwai877211f2005-11-17 13:59:38 +01001997snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 void __user **bufs,
1999 snd_pcm_uframes_t frames)
2000{
Takashi Iwai877211f2005-11-17 13:59:38 +01002001 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002003 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002005 err = pcm_sanity_check(substream);
2006 if (err < 0)
2007 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2010 return -EBADFD;
2011
Takashi Iwai0df63e42006-04-28 15:13:41 +02002012 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2014 return -EINVAL;
2015 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2016}
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018EXPORT_SYMBOL(snd_pcm_lib_readv);