blob: e9debaabf3765186768fc64b9c63b9ee5551e164 [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>
Takashi Iwai3f7440a2009-06-05 17:40:04 +020025#include <linux/math64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <sound/core.h>
27#include <sound/control.h>
28#include <sound/info.h>
29#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31#include <sound/timer.h>
32
33/*
34 * fill ring buffer with silence
35 * runtime->silence_start: starting pointer to silence area
36 * runtime->silence_filled: size filled with silence
37 * runtime->silence_threshold: threshold from application
38 * runtime->silence_size: maximal size from application
39 *
40 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
41 */
Takashi Iwai877211f2005-11-17 13:59:38 +010042void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Takashi Iwai877211f2005-11-17 13:59:38 +010044 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 snd_pcm_uframes_t frames, ofs, transfer;
46
47 if (runtime->silence_size < runtime->boundary) {
48 snd_pcm_sframes_t noise_dist, n;
49 if (runtime->silence_start != runtime->control->appl_ptr) {
50 n = runtime->control->appl_ptr - runtime->silence_start;
51 if (n < 0)
52 n += runtime->boundary;
53 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
54 runtime->silence_filled -= n;
55 else
56 runtime->silence_filled = 0;
57 runtime->silence_start = runtime->control->appl_ptr;
58 }
Takashi Iwai235475c2005-12-07 15:28:07 +010059 if (runtime->silence_filled >= runtime->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
62 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
63 return;
64 frames = runtime->silence_threshold - noise_dist;
65 if (frames > runtime->silence_size)
66 frames = runtime->silence_size;
67 } else {
68 if (new_hw_ptr == ULONG_MAX) { /* initialization */
69 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
Jaroslav Kysela9e216e82010-07-19 16:37:39 +020070 if (avail > runtime->buffer_size)
71 avail = runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 runtime->silence_filled = avail > 0 ? avail : 0;
73 runtime->silence_start = (runtime->status->hw_ptr +
74 runtime->silence_filled) %
75 runtime->boundary;
76 } else {
77 ofs = runtime->status->hw_ptr;
78 frames = new_hw_ptr - ofs;
79 if ((snd_pcm_sframes_t)frames < 0)
80 frames += runtime->boundary;
81 runtime->silence_filled -= frames;
82 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
83 runtime->silence_filled = 0;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020084 runtime->silence_start = new_hw_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 } else {
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020086 runtime->silence_start = ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89 frames = runtime->buffer_size - runtime->silence_filled;
90 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +020091 if (snd_BUG_ON(frames > runtime->buffer_size))
92 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (frames == 0)
94 return;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020095 ofs = runtime->silence_start % runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 while (frames > 0) {
97 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
98 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
99 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
100 if (substream->ops->silence) {
101 int err;
102 err = substream->ops->silence(substream, -1, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200103 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 } else {
105 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
106 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
107 }
108 } else {
109 unsigned int c;
110 unsigned int channels = runtime->channels;
111 if (substream->ops->silence) {
112 for (c = 0; c < channels; ++c) {
113 int err;
114 err = substream->ops->silence(substream, c, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200115 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117 } else {
118 size_t dma_csize = runtime->dma_bytes / channels;
119 for (c = 0; c < channels; ++c) {
120 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
121 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
122 }
123 }
124 }
125 runtime->silence_filled += transfer;
126 frames -= transfer;
127 ofs = 0;
128 }
129}
130
Takashi Iwaic0070112009-06-08 15:58:48 +0200131static void pcm_debug_name(struct snd_pcm_substream *substream,
132 char *name, size_t len)
133{
134 snprintf(name, len, "pcmC%dD%d%c:%d",
135 substream->pcm->card->number,
136 substream->pcm->device,
137 substream->stream ? 'c' : 'p',
138 substream->number);
139}
140
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100141#define XRUN_DEBUG_BASIC (1<<0)
142#define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
143#define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
144#define XRUN_DEBUG_PERIODUPDATE (1<<3) /* full period update info */
145#define XRUN_DEBUG_HWPTRUPDATE (1<<4) /* full hwptr update info */
146#define XRUN_DEBUG_LOG (1<<5) /* show last 10 positions on err */
147#define XRUN_DEBUG_LOGONCE (1<<6) /* do above only once */
148
149#ifdef CONFIG_SND_PCM_XRUN_DEBUG
150
151#define xrun_debug(substream, mask) \
152 ((substream)->pstr->xrun_debug & (mask))
Jarkko Nikula0f170142010-03-26 16:07:25 +0200153#else
154#define xrun_debug(substream, mask) 0
155#endif
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100156
157#define dump_stack_on_xrun(substream) do { \
158 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
159 dump_stack(); \
160 } while (0)
161
Takashi Iwai877211f2005-11-17 13:59:38 +0100162static void xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200164 struct snd_pcm_runtime *runtime = substream->runtime;
165
166 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
167 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100169 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
Takashi Iwaic0070112009-06-08 15:58:48 +0200170 char name[16];
171 pcm_debug_name(substream, name, sizeof(name));
172 snd_printd(KERN_DEBUG "XRUN: %s\n", name);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100173 dump_stack_on_xrun(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Jarkko Nikula0f170142010-03-26 16:07:25 +0200177#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100178#define hw_ptr_error(substream, fmt, args...) \
179 do { \
180 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100181 xrun_log_show(substream); \
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100182 if (printk_ratelimit()) { \
183 snd_printd("PCM: " fmt, ##args); \
184 } \
185 dump_stack_on_xrun(substream); \
186 } \
187 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100189#define XRUN_LOG_CNT 10
190
191struct hwptr_log_entry {
192 unsigned long jiffies;
193 snd_pcm_uframes_t pos;
194 snd_pcm_uframes_t period_size;
195 snd_pcm_uframes_t buffer_size;
196 snd_pcm_uframes_t old_hw_ptr;
197 snd_pcm_uframes_t hw_ptr_base;
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100198};
199
200struct snd_pcm_hwptr_log {
201 unsigned int idx;
202 unsigned int hit: 1;
203 struct hwptr_log_entry entries[XRUN_LOG_CNT];
204};
205
206static void xrun_log(struct snd_pcm_substream *substream,
207 snd_pcm_uframes_t pos)
208{
209 struct snd_pcm_runtime *runtime = substream->runtime;
210 struct snd_pcm_hwptr_log *log = runtime->hwptr_log;
211 struct hwptr_log_entry *entry;
212
213 if (log == NULL) {
214 log = kzalloc(sizeof(*log), GFP_ATOMIC);
215 if (log == NULL)
216 return;
217 runtime->hwptr_log = log;
218 } else {
219 if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
220 return;
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200221 }
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100222 entry = &log->entries[log->idx];
223 entry->jiffies = jiffies;
224 entry->pos = pos;
225 entry->period_size = runtime->period_size;
Joe Perchesc80c1d52010-11-14 19:05:02 -0800226 entry->buffer_size = runtime->buffer_size;
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100227 entry->old_hw_ptr = runtime->status->hw_ptr;
228 entry->hw_ptr_base = runtime->hw_ptr_base;
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100229 log->idx = (log->idx + 1) % XRUN_LOG_CNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100232static void xrun_log_show(struct snd_pcm_substream *substream)
233{
234 struct snd_pcm_hwptr_log *log = substream->runtime->hwptr_log;
235 struct hwptr_log_entry *entry;
236 char name[16];
237 unsigned int idx;
238 int cnt;
239
240 if (log == NULL)
241 return;
242 if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
243 return;
244 pcm_debug_name(substream, name, sizeof(name));
245 for (cnt = 0, idx = log->idx; cnt < XRUN_LOG_CNT; cnt++) {
246 entry = &log->entries[idx];
247 if (entry->period_size == 0)
248 break;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100249 snd_printd("hwptr log: %s: j=%lu, pos=%ld/%ld/%ld, "
250 "hwptr=%ld/%ld\n",
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100251 name, entry->jiffies, (unsigned long)entry->pos,
252 (unsigned long)entry->period_size,
253 (unsigned long)entry->buffer_size,
254 (unsigned long)entry->old_hw_ptr,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100255 (unsigned long)entry->hw_ptr_base);
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100256 idx++;
257 idx %= XRUN_LOG_CNT;
258 }
259 log->hit = 1;
260}
261
262#else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
263
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100264#define hw_ptr_error(substream, fmt, args...) do { } while (0)
265#define xrun_log(substream, pos) do { } while (0)
266#define xrun_log_show(substream) do { } while (0)
267
268#endif
269
Jaroslav Kysela12509322010-01-07 15:36:31 +0100270int snd_pcm_update_state(struct snd_pcm_substream *substream,
271 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 snd_pcm_uframes_t avail;
274
275 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
276 avail = snd_pcm_playback_avail(runtime);
277 else
278 avail = snd_pcm_capture_avail(runtime);
279 if (avail > runtime->avail_max)
280 runtime->avail_max = avail;
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200281 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
282 if (avail >= runtime->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 snd_pcm_drain_done(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200284 return -EPIPE;
285 }
286 } else {
287 if (avail >= runtime->stop_threshold) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 xrun(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200289 return -EPIPE;
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
David Dillow5daeba32010-06-27 00:13:20 +0200292 if (runtime->twake) {
293 if (avail >= runtime->twake)
294 wake_up(&runtime->tsleep);
295 } else if (avail >= runtime->control->avail_min)
296 wake_up(&runtime->sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return 0;
298}
299
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100300static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
301 unsigned int in_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Takashi Iwai877211f2005-11-17 13:59:38 +0100303 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 snd_pcm_uframes_t pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100305 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200306 snd_pcm_sframes_t hdelta, delta;
307 unsigned long jdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200309 old_hw_ptr = runtime->status->hw_ptr;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100310 pos = substream->ops->pointer(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (pos == SNDRV_PCM_POS_XRUN) {
312 xrun(substream);
313 return -EPIPE;
314 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100315 if (pos >= runtime->buffer_size) {
316 if (printk_ratelimit()) {
317 char name[16];
318 pcm_debug_name(substream, name, sizeof(name));
319 xrun_log_show(substream);
320 snd_printd(KERN_ERR "BUG: %s, pos = %ld, "
321 "buffer size = %ld, period size = %ld\n",
322 name, pos, runtime->buffer_size,
323 runtime->period_size);
324 }
325 pos = 0;
Takashi Iwaicedb8112009-07-23 11:04:13 +0200326 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100327 pos -= pos % runtime->min_align;
328 if (xrun_debug(substream, XRUN_DEBUG_LOG))
329 xrun_log(substream, pos);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100330 hw_base = runtime->hw_ptr_base;
331 new_hw_ptr = hw_base + pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100332 if (in_interrupt) {
333 /* we know that one period was processed */
334 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100335 delta = runtime->hw_ptr_interrupt + runtime->period_size;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100336 if (delta > new_hw_ptr) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200337 /* check for double acknowledged interrupts */
338 hdelta = jiffies - runtime->hw_ptr_jiffies;
339 if (hdelta > runtime->hw_ptr_buffer_jiffies/2) {
340 hw_base += runtime->buffer_size;
341 if (hw_base >= runtime->boundary)
342 hw_base = 0;
343 new_hw_ptr = hw_base + pos;
344 goto __delta;
345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100348 /* new_hw_ptr might be lower than old_hw_ptr in case when */
349 /* pointer crosses the end of the ring buffer */
350 if (new_hw_ptr < old_hw_ptr) {
351 hw_base += runtime->buffer_size;
352 if (hw_base >= runtime->boundary)
353 hw_base = 0;
354 new_hw_ptr = hw_base + pos;
355 }
356 __delta:
Clemens Ladischb406e612010-05-25 09:01:46 +0200357 delta = new_hw_ptr - old_hw_ptr;
358 if (delta < 0)
359 delta += runtime->boundary;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100360 if (xrun_debug(substream, in_interrupt ?
361 XRUN_DEBUG_PERIODUPDATE : XRUN_DEBUG_HWPTRUPDATE)) {
362 char name[16];
363 pcm_debug_name(substream, name, sizeof(name));
364 snd_printd("%s_update: %s: pos=%u/%u/%u, "
365 "hwptr=%ld/%ld/%ld/%ld\n",
366 in_interrupt ? "period" : "hwptr",
367 name,
368 (unsigned int)pos,
369 (unsigned int)runtime->period_size,
370 (unsigned int)runtime->buffer_size,
371 (unsigned long)delta,
372 (unsigned long)old_hw_ptr,
373 (unsigned long)new_hw_ptr,
374 (unsigned long)runtime->hw_ptr_base);
375 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100376
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100377 if (runtime->no_period_wakeup) {
378 /*
379 * Without regular period interrupts, we have to check
380 * the elapsed time to detect xruns.
381 */
382 jdelta = jiffies - runtime->hw_ptr_jiffies;
383 hdelta = jdelta - delta * HZ / runtime->rate;
384 while (hdelta > runtime->hw_ptr_buffer_jiffies / 2 + 1) {
385 delta += runtime->buffer_size;
386 hw_base += runtime->buffer_size;
387 if (hw_base >= runtime->boundary)
388 hw_base = 0;
389 new_hw_ptr = hw_base + pos;
390 hdelta -= runtime->hw_ptr_buffer_jiffies;
391 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100392 goto no_delta_check;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100393 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100394
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100395 /* something must be really wrong */
Jaroslav Kysela7b3a1772010-01-08 08:43:01 +0100396 if (delta >= runtime->buffer_size + runtime->period_size) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100397 hw_ptr_error(substream,
398 "Unexpected hw_pointer value %s"
399 "(stream=%i, pos=%ld, new_hw_ptr=%ld, "
400 "old_hw_ptr=%ld)\n",
401 in_interrupt ? "[Q] " : "[P]",
402 substream->stream, (long)pos,
403 (long)new_hw_ptr, (long)old_hw_ptr);
404 return 0;
405 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200406
407 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100408 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
Takashi Iwaic87d9732009-05-27 10:53:33 +0200409 goto no_jiffies_check;
410
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200411 /* Skip the jiffies check for hardwares with BATCH flag.
412 * Such hardware usually just increases the position at each IRQ,
413 * thus it can't give any strange position.
414 */
415 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
416 goto no_jiffies_check;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100417 hdelta = delta;
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200418 if (hdelta < runtime->delay)
419 goto no_jiffies_check;
420 hdelta -= runtime->delay;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200421 jdelta = jiffies - runtime->hw_ptr_jiffies;
422 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
423 delta = jdelta /
424 (((runtime->period_size * HZ) / runtime->rate)
425 + HZ/100);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100426 /* move new_hw_ptr according jiffies not pos variable */
427 new_hw_ptr = old_hw_ptr;
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100428 hw_base = delta;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100429 /* use loop to avoid checks for delta overflows */
430 /* the delta value is small or zero in most cases */
431 while (delta > 0) {
432 new_hw_ptr += runtime->period_size;
433 if (new_hw_ptr >= runtime->boundary)
434 new_hw_ptr -= runtime->boundary;
435 delta--;
436 }
437 /* align hw_base to buffer_size */
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200438 hw_ptr_error(substream,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100439 "hw_ptr skipping! %s"
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200440 "(pos=%ld, delta=%ld, period=%ld, "
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100441 "jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
442 in_interrupt ? "[Q] " : "",
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200443 (long)pos, (long)hdelta,
444 (long)runtime->period_size, jdelta,
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100445 ((hdelta * HZ) / runtime->rate), hw_base,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100446 (unsigned long)old_hw_ptr,
447 (unsigned long)new_hw_ptr);
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100448 /* reset values to proper state */
449 delta = 0;
450 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200451 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200452 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200453 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100454 hw_ptr_error(substream,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100455 "Lost interrupts? %s"
456 "(stream=%i, delta=%ld, new_hw_ptr=%ld, "
457 "old_hw_ptr=%ld)\n",
458 in_interrupt ? "[Q] " : "",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100459 substream->stream, (long)delta,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100460 (long)new_hw_ptr,
461 (long)old_hw_ptr);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100462 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100463
Clemens Ladischab69a492010-11-15 10:46:23 +0100464 no_delta_check:
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100465 if (runtime->status->hw_ptr == new_hw_ptr)
466 return 0;
Takashi Iwaiab1863f2009-06-07 12:09:17 +0200467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
469 runtime->silence_size > 0)
470 snd_pcm_playback_silence(substream, new_hw_ptr);
471
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100472 if (in_interrupt) {
Clemens Ladischead40462010-05-21 09:15:59 +0200473 delta = new_hw_ptr - runtime->hw_ptr_interrupt;
474 if (delta < 0)
475 delta += runtime->boundary;
476 delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
477 runtime->hw_ptr_interrupt += delta;
478 if (runtime->hw_ptr_interrupt >= runtime->boundary)
479 runtime->hw_ptr_interrupt -= runtime->boundary;
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100480 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100481 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200483 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200484 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
485 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Jaroslav Kysela12509322010-01-07 15:36:31 +0100487 return snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
490/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100491int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100493 return snd_pcm_update_hw_ptr0(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496/**
497 * snd_pcm_set_ops - set the PCM operators
498 * @pcm: the pcm instance
499 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
500 * @ops: the operator table
501 *
502 * Sets the given PCM operators to the pcm instance.
503 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100504void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Takashi Iwai877211f2005-11-17 13:59:38 +0100506 struct snd_pcm_str *stream = &pcm->streams[direction];
507 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 for (substream = stream->substream; substream != NULL; substream = substream->next)
510 substream->ops = ops;
511}
512
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200513EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515/**
516 * snd_pcm_sync - set the PCM sync id
517 * @substream: the pcm substream
518 *
519 * Sets the PCM sync identifier for the card.
520 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100521void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Takashi Iwai877211f2005-11-17 13:59:38 +0100523 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 runtime->sync.id32[0] = substream->pcm->card->number;
526 runtime->sync.id32[1] = -1;
527 runtime->sync.id32[2] = -1;
528 runtime->sync.id32[3] = -1;
529}
530
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200531EXPORT_SYMBOL(snd_pcm_set_sync);
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533/*
534 * Standard ioctl routine
535 */
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537static inline unsigned int div32(unsigned int a, unsigned int b,
538 unsigned int *r)
539{
540 if (b == 0) {
541 *r = 0;
542 return UINT_MAX;
543 }
544 *r = a % b;
545 return a / b;
546}
547
548static inline unsigned int div_down(unsigned int a, unsigned int b)
549{
550 if (b == 0)
551 return UINT_MAX;
552 return a / b;
553}
554
555static inline unsigned int div_up(unsigned int a, unsigned int b)
556{
557 unsigned int r;
558 unsigned int q;
559 if (b == 0)
560 return UINT_MAX;
561 q = div32(a, b, &r);
562 if (r)
563 ++q;
564 return q;
565}
566
567static inline unsigned int mul(unsigned int a, unsigned int b)
568{
569 if (a == 0)
570 return 0;
571 if (div_down(UINT_MAX, a) < b)
572 return UINT_MAX;
573 return a * b;
574}
575
576static inline unsigned int muldiv32(unsigned int a, unsigned int b,
577 unsigned int c, unsigned int *r)
578{
579 u_int64_t n = (u_int64_t) a * b;
580 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200581 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 *r = 0;
583 return UINT_MAX;
584 }
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200585 n = div_u64_rem(n, c, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (n >= UINT_MAX) {
587 *r = 0;
588 return UINT_MAX;
589 }
590 return n;
591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593/**
594 * snd_interval_refine - refine the interval value of configurator
595 * @i: the interval value to refine
596 * @v: the interval value to refer to
597 *
598 * Refines the interval value with the reference value.
599 * The interval is changed to the range satisfying both intervals.
600 * The interval status (min, max, integer, etc.) are evaluated.
601 *
602 * Returns non-zero if the value is changed, zero if not changed.
603 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100604int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200607 if (snd_BUG_ON(snd_interval_empty(i)))
608 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (i->min < v->min) {
610 i->min = v->min;
611 i->openmin = v->openmin;
612 changed = 1;
613 } else if (i->min == v->min && !i->openmin && v->openmin) {
614 i->openmin = 1;
615 changed = 1;
616 }
617 if (i->max > v->max) {
618 i->max = v->max;
619 i->openmax = v->openmax;
620 changed = 1;
621 } else if (i->max == v->max && !i->openmax && v->openmax) {
622 i->openmax = 1;
623 changed = 1;
624 }
625 if (!i->integer && v->integer) {
626 i->integer = 1;
627 changed = 1;
628 }
629 if (i->integer) {
630 if (i->openmin) {
631 i->min++;
632 i->openmin = 0;
633 }
634 if (i->openmax) {
635 i->max--;
636 i->openmax = 0;
637 }
638 } else if (!i->openmin && !i->openmax && i->min == i->max)
639 i->integer = 1;
640 if (snd_interval_checkempty(i)) {
641 snd_interval_none(i);
642 return -EINVAL;
643 }
644 return changed;
645}
646
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200647EXPORT_SYMBOL(snd_interval_refine);
648
Takashi Iwai877211f2005-11-17 13:59:38 +0100649static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200651 if (snd_BUG_ON(snd_interval_empty(i)))
652 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (snd_interval_single(i))
654 return 0;
655 i->max = i->min;
656 i->openmax = i->openmin;
657 if (i->openmax)
658 i->max++;
659 return 1;
660}
661
Takashi Iwai877211f2005-11-17 13:59:38 +0100662static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200664 if (snd_BUG_ON(snd_interval_empty(i)))
665 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (snd_interval_single(i))
667 return 0;
668 i->min = i->max;
669 i->openmin = i->openmax;
670 if (i->openmin)
671 i->min--;
672 return 1;
673}
674
Takashi Iwai877211f2005-11-17 13:59:38 +0100675void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 if (a->empty || b->empty) {
678 snd_interval_none(c);
679 return;
680 }
681 c->empty = 0;
682 c->min = mul(a->min, b->min);
683 c->openmin = (a->openmin || b->openmin);
684 c->max = mul(a->max, b->max);
685 c->openmax = (a->openmax || b->openmax);
686 c->integer = (a->integer && b->integer);
687}
688
689/**
690 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200691 * @a: dividend
692 * @b: divisor
693 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 *
695 * c = a / b
696 *
697 * Returns non-zero if the value is changed, zero if not changed.
698 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100699void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 unsigned int r;
702 if (a->empty || b->empty) {
703 snd_interval_none(c);
704 return;
705 }
706 c->empty = 0;
707 c->min = div32(a->min, b->max, &r);
708 c->openmin = (r || a->openmin || b->openmax);
709 if (b->min > 0) {
710 c->max = div32(a->max, b->min, &r);
711 if (r) {
712 c->max++;
713 c->openmax = 1;
714 } else
715 c->openmax = (a->openmax || b->openmin);
716 } else {
717 c->max = UINT_MAX;
718 c->openmax = 0;
719 }
720 c->integer = 0;
721}
722
723/**
724 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200725 * @a: dividend 1
726 * @b: dividend 2
727 * @k: divisor (as integer)
728 * @c: result
729 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 * c = a * b / k
731 *
732 * Returns non-zero if the value is changed, zero if not changed.
733 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100734void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
735 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 unsigned int r;
738 if (a->empty || b->empty) {
739 snd_interval_none(c);
740 return;
741 }
742 c->empty = 0;
743 c->min = muldiv32(a->min, b->min, k, &r);
744 c->openmin = (r || a->openmin || b->openmin);
745 c->max = muldiv32(a->max, b->max, k, &r);
746 if (r) {
747 c->max++;
748 c->openmax = 1;
749 } else
750 c->openmax = (a->openmax || b->openmax);
751 c->integer = 0;
752}
753
754/**
755 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200756 * @a: dividend 1
757 * @k: dividend 2 (as integer)
758 * @b: divisor
759 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 *
761 * c = a * k / b
762 *
763 * Returns non-zero if the value is changed, zero if not changed.
764 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100765void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
766 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 unsigned int r;
769 if (a->empty || b->empty) {
770 snd_interval_none(c);
771 return;
772 }
773 c->empty = 0;
774 c->min = muldiv32(a->min, k, b->max, &r);
775 c->openmin = (r || a->openmin || b->openmax);
776 if (b->min > 0) {
777 c->max = muldiv32(a->max, k, b->min, &r);
778 if (r) {
779 c->max++;
780 c->openmax = 1;
781 } else
782 c->openmax = (a->openmax || b->openmin);
783 } else {
784 c->max = UINT_MAX;
785 c->openmax = 0;
786 }
787 c->integer = 0;
788}
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790/* ---- */
791
792
793/**
794 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200795 * @i: interval to refine
796 * @rats_count: number of ratnum_t
797 * @rats: ratnum_t array
798 * @nump: pointer to store the resultant numerator
799 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 *
801 * Returns non-zero if the value is changed, zero if not changed.
802 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100803int snd_interval_ratnum(struct snd_interval *i,
804 unsigned int rats_count, struct snd_ratnum *rats,
805 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Krzysztof Helt8374e242009-12-21 17:07:08 +0100807 unsigned int best_num, best_den;
808 int best_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100810 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 int err;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100812 unsigned int result_num, result_den;
813 int result_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 best_num = best_den = best_diff = 0;
816 for (k = 0; k < rats_count; ++k) {
817 unsigned int num = rats[k].num;
818 unsigned int den;
819 unsigned int q = i->min;
820 int diff;
821 if (q == 0)
822 q = 1;
Krzysztof Helt40962d72009-12-19 18:31:04 +0100823 den = div_up(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (den < rats[k].den_min)
825 continue;
826 if (den > rats[k].den_max)
827 den = rats[k].den_max;
828 else {
829 unsigned int r;
830 r = (den - rats[k].den_min) % rats[k].den_step;
831 if (r != 0)
832 den -= r;
833 }
834 diff = num - q * den;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100835 if (diff < 0)
836 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (best_num == 0 ||
838 diff * best_den < best_diff * den) {
839 best_diff = diff;
840 best_den = den;
841 best_num = num;
842 }
843 }
844 if (best_den == 0) {
845 i->empty = 1;
846 return -EINVAL;
847 }
848 t.min = div_down(best_num, best_den);
849 t.openmin = !!(best_num % best_den);
850
Krzysztof Helt8374e242009-12-21 17:07:08 +0100851 result_num = best_num;
852 result_diff = best_diff;
853 result_den = best_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 best_num = best_den = best_diff = 0;
855 for (k = 0; k < rats_count; ++k) {
856 unsigned int num = rats[k].num;
857 unsigned int den;
858 unsigned int q = i->max;
859 int diff;
860 if (q == 0) {
861 i->empty = 1;
862 return -EINVAL;
863 }
Krzysztof Helt40962d72009-12-19 18:31:04 +0100864 den = div_down(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (den > rats[k].den_max)
866 continue;
867 if (den < rats[k].den_min)
868 den = rats[k].den_min;
869 else {
870 unsigned int r;
871 r = (den - rats[k].den_min) % rats[k].den_step;
872 if (r != 0)
873 den += rats[k].den_step - r;
874 }
875 diff = q * den - num;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100876 if (diff < 0)
877 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (best_num == 0 ||
879 diff * best_den < best_diff * den) {
880 best_diff = diff;
881 best_den = den;
882 best_num = num;
883 }
884 }
885 if (best_den == 0) {
886 i->empty = 1;
887 return -EINVAL;
888 }
889 t.max = div_up(best_num, best_den);
890 t.openmax = !!(best_num % best_den);
891 t.integer = 0;
892 err = snd_interval_refine(i, &t);
893 if (err < 0)
894 return err;
895
896 if (snd_interval_single(i)) {
Krzysztof Helt8374e242009-12-21 17:07:08 +0100897 if (best_diff * result_den < result_diff * best_den) {
898 result_num = best_num;
899 result_den = best_den;
900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (nump)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100902 *nump = result_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (denp)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100904 *denp = result_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906 return err;
907}
908
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200909EXPORT_SYMBOL(snd_interval_ratnum);
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911/**
912 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200913 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100914 * @rats_count: number of struct ratden
915 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200916 * @nump: pointer to store the resultant numerator
917 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 *
919 * Returns non-zero if the value is changed, zero if not changed.
920 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100921static int snd_interval_ratden(struct snd_interval *i,
922 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 unsigned int *nump, unsigned int *denp)
924{
925 unsigned int best_num, best_diff, best_den;
926 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100927 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 int err;
929
930 best_num = best_den = best_diff = 0;
931 for (k = 0; k < rats_count; ++k) {
932 unsigned int num;
933 unsigned int den = rats[k].den;
934 unsigned int q = i->min;
935 int diff;
936 num = mul(q, den);
937 if (num > rats[k].num_max)
938 continue;
939 if (num < rats[k].num_min)
940 num = rats[k].num_max;
941 else {
942 unsigned int r;
943 r = (num - rats[k].num_min) % rats[k].num_step;
944 if (r != 0)
945 num += rats[k].num_step - r;
946 }
947 diff = num - q * den;
948 if (best_num == 0 ||
949 diff * best_den < best_diff * den) {
950 best_diff = diff;
951 best_den = den;
952 best_num = num;
953 }
954 }
955 if (best_den == 0) {
956 i->empty = 1;
957 return -EINVAL;
958 }
959 t.min = div_down(best_num, best_den);
960 t.openmin = !!(best_num % best_den);
961
962 best_num = best_den = best_diff = 0;
963 for (k = 0; k < rats_count; ++k) {
964 unsigned int num;
965 unsigned int den = rats[k].den;
966 unsigned int q = i->max;
967 int diff;
968 num = mul(q, den);
969 if (num < rats[k].num_min)
970 continue;
971 if (num > rats[k].num_max)
972 num = rats[k].num_max;
973 else {
974 unsigned int r;
975 r = (num - rats[k].num_min) % rats[k].num_step;
976 if (r != 0)
977 num -= r;
978 }
979 diff = q * den - num;
980 if (best_num == 0 ||
981 diff * best_den < best_diff * den) {
982 best_diff = diff;
983 best_den = den;
984 best_num = num;
985 }
986 }
987 if (best_den == 0) {
988 i->empty = 1;
989 return -EINVAL;
990 }
991 t.max = div_up(best_num, best_den);
992 t.openmax = !!(best_num % best_den);
993 t.integer = 0;
994 err = snd_interval_refine(i, &t);
995 if (err < 0)
996 return err;
997
998 if (snd_interval_single(i)) {
999 if (nump)
1000 *nump = best_num;
1001 if (denp)
1002 *denp = best_den;
1003 }
1004 return err;
1005}
1006
1007/**
1008 * snd_interval_list - refine the interval value from the list
1009 * @i: the interval value to refine
1010 * @count: the number of elements in the list
1011 * @list: the value list
1012 * @mask: the bit-mask to evaluate
1013 *
1014 * Refines the interval value from the list.
1015 * When mask is non-zero, only the elements corresponding to bit 1 are
1016 * evaluated.
1017 *
1018 * Returns non-zero if the value is changed, zero if not changed.
1019 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001020int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 unsigned int k;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001023 struct snd_interval list_range;
Takashi Iwai0981a262007-02-01 14:53:49 +01001024
1025 if (!count) {
1026 i->empty = 1;
1027 return -EINVAL;
1028 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001029 snd_interval_any(&list_range);
1030 list_range.min = UINT_MAX;
1031 list_range.max = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 for (k = 0; k < count; k++) {
1033 if (mask && !(mask & (1 << k)))
1034 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001035 if (!snd_interval_test(i, list[k]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001037 list_range.min = min(list_range.min, list[k]);
1038 list_range.max = max(list_range.max, list[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001040 return snd_interval_refine(i, &list_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001043EXPORT_SYMBOL(snd_interval_list);
1044
Takashi Iwai877211f2005-11-17 13:59:38 +01001045static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 unsigned int n;
1048 int changed = 0;
1049 n = (i->min - min) % step;
1050 if (n != 0 || i->openmin) {
1051 i->min += step - n;
1052 changed = 1;
1053 }
1054 n = (i->max - min) % step;
1055 if (n != 0 || i->openmax) {
1056 i->max -= n;
1057 changed = 1;
1058 }
1059 if (snd_interval_checkempty(i)) {
1060 i->empty = 1;
1061 return -EINVAL;
1062 }
1063 return changed;
1064}
1065
1066/* Info constraints helpers */
1067
1068/**
1069 * snd_pcm_hw_rule_add - add the hw-constraint rule
1070 * @runtime: the pcm runtime instance
1071 * @cond: condition bits
1072 * @var: the variable to evaluate
1073 * @func: the evaluation function
1074 * @private: the private data pointer passed to function
1075 * @dep: the dependent variables
1076 *
1077 * Returns zero if successful, or a negative error code on failure.
1078 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001079int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 int var,
1081 snd_pcm_hw_rule_func_t func, void *private,
1082 int dep, ...)
1083{
Takashi Iwai877211f2005-11-17 13:59:38 +01001084 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1085 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 unsigned int k;
1087 va_list args;
1088 va_start(args, dep);
1089 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001090 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 unsigned int new_rules = constrs->rules_all + 16;
1092 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1093 if (!new)
1094 return -ENOMEM;
1095 if (constrs->rules) {
1096 memcpy(new, constrs->rules,
1097 constrs->rules_num * sizeof(*c));
1098 kfree(constrs->rules);
1099 }
1100 constrs->rules = new;
1101 constrs->rules_all = new_rules;
1102 }
1103 c = &constrs->rules[constrs->rules_num];
1104 c->cond = cond;
1105 c->func = func;
1106 c->var = var;
1107 c->private = private;
1108 k = 0;
1109 while (1) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001110 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1111 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 c->deps[k++] = dep;
1113 if (dep < 0)
1114 break;
1115 dep = va_arg(args, int);
1116 }
1117 constrs->rules_num++;
1118 va_end(args);
1119 return 0;
1120}
1121
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001122EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001125 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001126 * @runtime: PCM runtime instance
1127 * @var: hw_params variable to apply the mask
1128 * @mask: the bitmap mask
1129 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001130 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001132int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 u_int32_t mask)
1134{
Takashi Iwai877211f2005-11-17 13:59:38 +01001135 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1136 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 *maskp->bits &= mask;
1138 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1139 if (*maskp->bits == 0)
1140 return -EINVAL;
1141 return 0;
1142}
1143
1144/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001145 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001146 * @runtime: PCM runtime instance
1147 * @var: hw_params variable to apply the mask
1148 * @mask: the 64bit bitmap mask
1149 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001150 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001152int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 u_int64_t mask)
1154{
Takashi Iwai877211f2005-11-17 13:59:38 +01001155 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1156 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 maskp->bits[0] &= (u_int32_t)mask;
1158 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1159 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1160 if (! maskp->bits[0] && ! maskp->bits[1])
1161 return -EINVAL;
1162 return 0;
1163}
1164
1165/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001166 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001167 * @runtime: PCM runtime instance
1168 * @var: hw_params variable to apply the integer constraint
1169 *
1170 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001172int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Takashi Iwai877211f2005-11-17 13:59:38 +01001174 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return snd_interval_setinteger(constrs_interval(constrs, var));
1176}
1177
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001178EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001181 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001182 * @runtime: PCM runtime instance
1183 * @var: hw_params variable to apply the range
1184 * @min: the minimal value
1185 * @max: the maximal value
1186 *
1187 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001189int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 unsigned int min, unsigned int max)
1191{
Takashi Iwai877211f2005-11-17 13:59:38 +01001192 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1193 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 t.min = min;
1195 t.max = max;
1196 t.openmin = t.openmax = 0;
1197 t.integer = 0;
1198 return snd_interval_refine(constrs_interval(constrs, var), &t);
1199}
1200
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001201EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1202
Takashi Iwai877211f2005-11-17 13:59:38 +01001203static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1204 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Takashi Iwai877211f2005-11-17 13:59:38 +01001206 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1208}
1209
1210
1211/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001212 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001213 * @runtime: PCM runtime instance
1214 * @cond: condition bits
1215 * @var: hw_params variable to apply the list constraint
1216 * @l: list
1217 *
1218 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001220int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 unsigned int cond,
1222 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001223 struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 return snd_pcm_hw_rule_add(runtime, cond, var,
1226 snd_pcm_hw_rule_list, l,
1227 var, -1);
1228}
1229
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001230EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1231
Takashi Iwai877211f2005-11-17 13:59:38 +01001232static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1233 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Takashi Iwai877211f2005-11-17 13:59:38 +01001235 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 unsigned int num = 0, den = 0;
1237 int err;
1238 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1239 r->nrats, r->rats, &num, &den);
1240 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1241 params->rate_num = num;
1242 params->rate_den = den;
1243 }
1244 return err;
1245}
1246
1247/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001248 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001249 * @runtime: PCM runtime instance
1250 * @cond: condition bits
1251 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001252 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001254int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 unsigned int cond,
1256 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001257 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 return snd_pcm_hw_rule_add(runtime, cond, var,
1260 snd_pcm_hw_rule_ratnums, r,
1261 var, -1);
1262}
1263
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001264EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1265
Takashi Iwai877211f2005-11-17 13:59:38 +01001266static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1267 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Takashi Iwai877211f2005-11-17 13:59:38 +01001269 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 unsigned int num = 0, den = 0;
1271 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1272 r->nrats, r->rats, &num, &den);
1273 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1274 params->rate_num = num;
1275 params->rate_den = den;
1276 }
1277 return err;
1278}
1279
1280/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001281 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001282 * @runtime: PCM runtime instance
1283 * @cond: condition bits
1284 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001285 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001287int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 unsigned int cond,
1289 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001290 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 return snd_pcm_hw_rule_add(runtime, cond, var,
1293 snd_pcm_hw_rule_ratdens, r,
1294 var, -1);
1295}
1296
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001297EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1298
Takashi Iwai877211f2005-11-17 13:59:38 +01001299static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1300 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 unsigned int l = (unsigned long) rule->private;
1303 int width = l & 0xffff;
1304 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001305 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (snd_interval_single(i) && snd_interval_value(i) == width)
1307 params->msbits = msbits;
1308 return 0;
1309}
1310
1311/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001312 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001313 * @runtime: PCM runtime instance
1314 * @cond: condition bits
1315 * @width: sample bits width
1316 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001318int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 unsigned int cond,
1320 unsigned int width,
1321 unsigned int msbits)
1322{
1323 unsigned long l = (msbits << 16) | width;
1324 return snd_pcm_hw_rule_add(runtime, cond, -1,
1325 snd_pcm_hw_rule_msbits,
1326 (void*) l,
1327 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1328}
1329
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001330EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1331
Takashi Iwai877211f2005-11-17 13:59:38 +01001332static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1333 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 unsigned long step = (unsigned long) rule->private;
1336 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1337}
1338
1339/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001340 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001341 * @runtime: PCM runtime instance
1342 * @cond: condition bits
1343 * @var: hw_params variable to apply the step constraint
1344 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001346int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 unsigned int cond,
1348 snd_pcm_hw_param_t var,
1349 unsigned long step)
1350{
1351 return snd_pcm_hw_rule_add(runtime, cond, var,
1352 snd_pcm_hw_rule_step, (void *) step,
1353 var, -1);
1354}
1355
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001356EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1357
Takashi Iwai877211f2005-11-17 13:59:38 +01001358static 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 -07001359{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001360 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1362 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1363 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1364 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1365 };
1366 return snd_interval_list(hw_param_interval(params, rule->var),
1367 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1368}
1369
1370/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001371 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001372 * @runtime: PCM runtime instance
1373 * @cond: condition bits
1374 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001376int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 unsigned int cond,
1378 snd_pcm_hw_param_t var)
1379{
1380 return snd_pcm_hw_rule_add(runtime, cond, var,
1381 snd_pcm_hw_rule_pow2, NULL,
1382 var, -1);
1383}
1384
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001385EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1386
Takashi Iwai877211f2005-11-17 13:59:38 +01001387static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001388 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 if (hw_is_mask(var)) {
1391 snd_mask_any(hw_param_mask(params, var));
1392 params->cmask |= 1 << var;
1393 params->rmask |= 1 << var;
1394 return;
1395 }
1396 if (hw_is_interval(var)) {
1397 snd_interval_any(hw_param_interval(params, var));
1398 params->cmask |= 1 << var;
1399 params->rmask |= 1 << var;
1400 return;
1401 }
1402 snd_BUG();
1403}
1404
Takashi Iwai877211f2005-11-17 13:59:38 +01001405void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
1407 unsigned int k;
1408 memset(params, 0, sizeof(*params));
1409 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1410 _snd_pcm_hw_param_any(params, k);
1411 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1412 _snd_pcm_hw_param_any(params, k);
1413 params->info = ~0U;
1414}
1415
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001416EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001419 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001420 * @params: the hw_params instance
1421 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001422 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001424 * Return the value for field @var if it's fixed in configuration space
1425 * defined by @params. Return -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001427int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1428 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
1430 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001431 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (!snd_mask_single(mask))
1433 return -EINVAL;
1434 if (dir)
1435 *dir = 0;
1436 return snd_mask_value(mask);
1437 }
1438 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001439 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (!snd_interval_single(i))
1441 return -EINVAL;
1442 if (dir)
1443 *dir = i->openmin;
1444 return snd_interval_value(i);
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return -EINVAL;
1447}
1448
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001449EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Takashi Iwai877211f2005-11-17 13:59:38 +01001451void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 snd_pcm_hw_param_t var)
1453{
1454 if (hw_is_mask(var)) {
1455 snd_mask_none(hw_param_mask(params, var));
1456 params->cmask |= 1 << var;
1457 params->rmask |= 1 << var;
1458 } else if (hw_is_interval(var)) {
1459 snd_interval_none(hw_param_interval(params, var));
1460 params->cmask |= 1 << var;
1461 params->rmask |= 1 << var;
1462 } else {
1463 snd_BUG();
1464 }
1465}
1466
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001467EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Takashi Iwai877211f2005-11-17 13:59:38 +01001469static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001470 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 int changed;
1473 if (hw_is_mask(var))
1474 changed = snd_mask_refine_first(hw_param_mask(params, var));
1475 else if (hw_is_interval(var))
1476 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001477 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (changed) {
1480 params->cmask |= 1 << var;
1481 params->rmask |= 1 << var;
1482 }
1483 return changed;
1484}
1485
1486
1487/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001488 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001489 * @pcm: PCM instance
1490 * @params: the hw_params instance
1491 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001492 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001494 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 * values > minimum. Reduce configuration space accordingly.
1496 * Return the minimum.
1497 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001498int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1499 struct snd_pcm_hw_params *params,
1500 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
1502 int changed = _snd_pcm_hw_param_first(params, var);
1503 if (changed < 0)
1504 return changed;
1505 if (params->rmask) {
1506 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001507 if (snd_BUG_ON(err < 0))
1508 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
1510 return snd_pcm_hw_param_value(params, var, dir);
1511}
1512
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001513EXPORT_SYMBOL(snd_pcm_hw_param_first);
1514
Takashi Iwai877211f2005-11-17 13:59:38 +01001515static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001516 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
1518 int changed;
1519 if (hw_is_mask(var))
1520 changed = snd_mask_refine_last(hw_param_mask(params, var));
1521 else if (hw_is_interval(var))
1522 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001523 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (changed) {
1526 params->cmask |= 1 << var;
1527 params->rmask |= 1 << var;
1528 }
1529 return changed;
1530}
1531
1532
1533/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001534 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001535 * @pcm: PCM instance
1536 * @params: the hw_params instance
1537 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001538 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001540 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 * values < maximum. Reduce configuration space accordingly.
1542 * Return the maximum.
1543 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001544int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1545 struct snd_pcm_hw_params *params,
1546 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 int changed = _snd_pcm_hw_param_last(params, var);
1549 if (changed < 0)
1550 return changed;
1551 if (params->rmask) {
1552 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001553 if (snd_BUG_ON(err < 0))
1554 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556 return snd_pcm_hw_param_value(params, var, dir);
1557}
1558
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001559EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001562 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001563 * @pcm: PCM instance
1564 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001566 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 * The configuration chosen is that obtained fixing in this order:
1568 * first access, first format, first subformat, min channels,
1569 * min rate, min period time, max buffer size, min tick time
1570 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001571int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1572 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001574 static int vars[] = {
1575 SNDRV_PCM_HW_PARAM_ACCESS,
1576 SNDRV_PCM_HW_PARAM_FORMAT,
1577 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1578 SNDRV_PCM_HW_PARAM_CHANNELS,
1579 SNDRV_PCM_HW_PARAM_RATE,
1580 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1581 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1582 SNDRV_PCM_HW_PARAM_TICK_TIME,
1583 -1
1584 };
1585 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001587 for (v = vars; *v != -1; v++) {
1588 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1589 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1590 else
1591 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001592 if (snd_BUG_ON(err < 0))
1593 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return 0;
1596}
1597
Takashi Iwai877211f2005-11-17 13:59:38 +01001598static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 void *arg)
1600{
Takashi Iwai877211f2005-11-17 13:59:38 +01001601 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 unsigned long flags;
1603 snd_pcm_stream_lock_irqsave(substream, flags);
1604 if (snd_pcm_running(substream) &&
1605 snd_pcm_update_hw_ptr(substream) >= 0)
1606 runtime->status->hw_ptr %= runtime->buffer_size;
1607 else
1608 runtime->status->hw_ptr = 0;
1609 snd_pcm_stream_unlock_irqrestore(substream, flags);
1610 return 0;
1611}
1612
Takashi Iwai877211f2005-11-17 13:59:38 +01001613static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 void *arg)
1615{
Takashi Iwai877211f2005-11-17 13:59:38 +01001616 struct snd_pcm_channel_info *info = arg;
1617 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 int width;
1619 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1620 info->offset = -1;
1621 return 0;
1622 }
1623 width = snd_pcm_format_physical_width(runtime->format);
1624 if (width < 0)
1625 return width;
1626 info->offset = 0;
1627 switch (runtime->access) {
1628 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1629 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1630 info->first = info->channel * width;
1631 info->step = runtime->channels * width;
1632 break;
1633 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1634 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1635 {
1636 size_t size = runtime->dma_bytes / runtime->channels;
1637 info->first = info->channel * size * 8;
1638 info->step = width;
1639 break;
1640 }
1641 default:
1642 snd_BUG();
1643 break;
1644 }
1645 return 0;
1646}
1647
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001648static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1649 void *arg)
1650{
1651 struct snd_pcm_hw_params *params = arg;
1652 snd_pcm_format_t format;
1653 int channels, width;
1654
1655 params->fifo_size = substream->runtime->hw.fifo_size;
1656 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1657 format = params_format(params);
1658 channels = params_channels(params);
1659 width = snd_pcm_format_physical_width(format);
1660 params->fifo_size /= width * channels;
1661 }
1662 return 0;
1663}
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665/**
1666 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1667 * @substream: the pcm substream instance
1668 * @cmd: ioctl command
1669 * @arg: ioctl argument
1670 *
1671 * Processes the generic ioctl commands for PCM.
1672 * Can be passed as the ioctl callback for PCM ops.
1673 *
1674 * Returns zero if successful, or a negative error code on failure.
1675 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001676int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 unsigned int cmd, void *arg)
1678{
1679 switch (cmd) {
1680 case SNDRV_PCM_IOCTL1_INFO:
1681 return 0;
1682 case SNDRV_PCM_IOCTL1_RESET:
1683 return snd_pcm_lib_ioctl_reset(substream, arg);
1684 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1685 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001686 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1687 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689 return -ENXIO;
1690}
1691
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001692EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694/**
1695 * snd_pcm_period_elapsed - update the pcm status for the next period
1696 * @substream: the pcm substream instance
1697 *
1698 * This function is called from the interrupt handler when the
1699 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001700 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 *
1702 * Even if more than one periods have elapsed since the last call, you
1703 * have to call this only once.
1704 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001705void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Takashi Iwai877211f2005-11-17 13:59:38 +01001707 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 unsigned long flags;
1709
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001710 if (PCM_RUNTIME_CHECK(substream))
1711 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 if (runtime->transfer_ack_begin)
1715 runtime->transfer_ack_begin(substream);
1716
1717 snd_pcm_stream_lock_irqsave(substream, flags);
1718 if (!snd_pcm_running(substream) ||
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01001719 snd_pcm_update_hw_ptr0(substream, 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 goto _end;
1721
1722 if (substream->timer_running)
1723 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 _end:
1725 snd_pcm_stream_unlock_irqrestore(substream, flags);
1726 if (runtime->transfer_ack_end)
1727 runtime->transfer_ack_end(substream);
1728 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1729}
1730
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001731EXPORT_SYMBOL(snd_pcm_period_elapsed);
1732
Takashi Iwai13075512008-01-08 18:08:14 +01001733/*
1734 * Wait until avail_min data becomes available
1735 * Returns a negative error code if any error occurs during operation.
1736 * The available space is stored on availp. When err = 0 and avail = 0
1737 * on the capture stream, it indicates the stream is in DRAINING state.
1738 */
David Dillow5daeba32010-06-27 00:13:20 +02001739static int wait_for_avail(struct snd_pcm_substream *substream,
Takashi Iwai13075512008-01-08 18:08:14 +01001740 snd_pcm_uframes_t *availp)
1741{
1742 struct snd_pcm_runtime *runtime = substream->runtime;
1743 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1744 wait_queue_t wait;
1745 int err = 0;
1746 snd_pcm_uframes_t avail = 0;
1747 long tout;
1748
1749 init_waitqueue_entry(&wait, current);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001750 add_wait_queue(&runtime->tsleep, &wait);
Takashi Iwai13075512008-01-08 18:08:14 +01001751 for (;;) {
1752 if (signal_pending(current)) {
1753 err = -ERESTARTSYS;
1754 break;
1755 }
1756 set_current_state(TASK_INTERRUPTIBLE);
1757 snd_pcm_stream_unlock_irq(substream);
1758 tout = schedule_timeout(msecs_to_jiffies(10000));
1759 snd_pcm_stream_lock_irq(substream);
1760 switch (runtime->status->state) {
1761 case SNDRV_PCM_STATE_SUSPENDED:
1762 err = -ESTRPIPE;
1763 goto _endloop;
1764 case SNDRV_PCM_STATE_XRUN:
1765 err = -EPIPE;
1766 goto _endloop;
1767 case SNDRV_PCM_STATE_DRAINING:
1768 if (is_playback)
1769 err = -EPIPE;
1770 else
1771 avail = 0; /* indicate draining */
1772 goto _endloop;
1773 case SNDRV_PCM_STATE_OPEN:
1774 case SNDRV_PCM_STATE_SETUP:
1775 case SNDRV_PCM_STATE_DISCONNECTED:
1776 err = -EBADFD;
1777 goto _endloop;
1778 }
1779 if (!tout) {
1780 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1781 is_playback ? "playback" : "capture");
1782 err = -EIO;
1783 break;
1784 }
1785 if (is_playback)
1786 avail = snd_pcm_playback_avail(runtime);
1787 else
1788 avail = snd_pcm_capture_avail(runtime);
David Dillow5daeba32010-06-27 00:13:20 +02001789 if (avail >= runtime->twake)
Takashi Iwai13075512008-01-08 18:08:14 +01001790 break;
1791 }
1792 _endloop:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001793 remove_wait_queue(&runtime->tsleep, &wait);
Takashi Iwai13075512008-01-08 18:08:14 +01001794 *availp = avail;
1795 return err;
1796}
1797
Takashi Iwai877211f2005-11-17 13:59:38 +01001798static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 unsigned int hwoff,
1800 unsigned long data, unsigned int off,
1801 snd_pcm_uframes_t frames)
1802{
Takashi Iwai877211f2005-11-17 13:59:38 +01001803 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 int err;
1805 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1806 if (substream->ops->copy) {
1807 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1808 return err;
1809 } else {
1810 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1812 return -EFAULT;
1813 }
1814 return 0;
1815}
1816
Takashi Iwai877211f2005-11-17 13:59:38 +01001817typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 unsigned long data, unsigned int off,
1819 snd_pcm_uframes_t size);
1820
Takashi Iwai877211f2005-11-17 13:59:38 +01001821static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 unsigned long data,
1823 snd_pcm_uframes_t size,
1824 int nonblock,
1825 transfer_f transfer)
1826{
Takashi Iwai877211f2005-11-17 13:59:38 +01001827 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 snd_pcm_uframes_t xfer = 0;
1829 snd_pcm_uframes_t offset = 0;
1830 int err = 0;
1831
1832 if (size == 0)
1833 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 snd_pcm_stream_lock_irq(substream);
1836 switch (runtime->status->state) {
1837 case SNDRV_PCM_STATE_PREPARED:
1838 case SNDRV_PCM_STATE_RUNNING:
1839 case SNDRV_PCM_STATE_PAUSED:
1840 break;
1841 case SNDRV_PCM_STATE_XRUN:
1842 err = -EPIPE;
1843 goto _end_unlock;
1844 case SNDRV_PCM_STATE_SUSPENDED:
1845 err = -ESTRPIPE;
1846 goto _end_unlock;
1847 default:
1848 err = -EBADFD;
1849 goto _end_unlock;
1850 }
1851
David Dillow5daeba32010-06-27 00:13:20 +02001852 runtime->twake = runtime->control->avail_min ? : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 while (size > 0) {
1854 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1855 snd_pcm_uframes_t avail;
1856 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001857 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 snd_pcm_update_hw_ptr(substream);
1859 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001860 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (nonblock) {
1862 err = -EAGAIN;
1863 goto _end_unlock;
1864 }
David Dillow5daeba32010-06-27 00:13:20 +02001865 runtime->twake = min_t(snd_pcm_uframes_t, size,
1866 runtime->control->avail_min ? : 1);
1867 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01001868 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 frames = size > avail ? avail : size;
1872 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1873 if (frames > cont)
1874 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001875 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001876 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001877 snd_pcm_stream_unlock_irq(substream);
1878 return -EINVAL;
1879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 appl_ptr = runtime->control->appl_ptr;
1881 appl_ofs = appl_ptr % runtime->buffer_size;
1882 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01001883 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01001885 if (err < 0)
1886 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 switch (runtime->status->state) {
1888 case SNDRV_PCM_STATE_XRUN:
1889 err = -EPIPE;
1890 goto _end_unlock;
1891 case SNDRV_PCM_STATE_SUSPENDED:
1892 err = -ESTRPIPE;
1893 goto _end_unlock;
1894 default:
1895 break;
1896 }
1897 appl_ptr += frames;
1898 if (appl_ptr >= runtime->boundary)
1899 appl_ptr -= runtime->boundary;
1900 runtime->control->appl_ptr = appl_ptr;
1901 if (substream->ops->ack)
1902 substream->ops->ack(substream);
1903
1904 offset += frames;
1905 size -= frames;
1906 xfer += frames;
1907 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1908 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1909 err = snd_pcm_start(substream);
1910 if (err < 0)
1911 goto _end_unlock;
1912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001915 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01001916 if (xfer > 0 && err >= 0)
1917 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1920}
1921
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001922/* sanity-check for read/write methods */
1923static int pcm_sanity_check(struct snd_pcm_substream *substream)
1924{
1925 struct snd_pcm_runtime *runtime;
1926 if (PCM_RUNTIME_CHECK(substream))
1927 return -ENXIO;
1928 runtime = substream->runtime;
1929 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1930 return -EINVAL;
1931 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1932 return -EBADFD;
1933 return 0;
1934}
1935
Takashi Iwai877211f2005-11-17 13:59:38 +01001936snd_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 -07001937{
Takashi Iwai877211f2005-11-17 13:59:38 +01001938 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001940 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001942 err = pcm_sanity_check(substream);
1943 if (err < 0)
1944 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001946 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1949 runtime->channels > 1)
1950 return -EINVAL;
1951 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1952 snd_pcm_lib_write_transfer);
1953}
1954
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001955EXPORT_SYMBOL(snd_pcm_lib_write);
1956
Takashi Iwai877211f2005-11-17 13:59:38 +01001957static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 unsigned int hwoff,
1959 unsigned long data, unsigned int off,
1960 snd_pcm_uframes_t frames)
1961{
Takashi Iwai877211f2005-11-17 13:59:38 +01001962 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 int err;
1964 void __user **bufs = (void __user **)data;
1965 int channels = runtime->channels;
1966 int c;
1967 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001968 if (snd_BUG_ON(!substream->ops->silence))
1969 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 for (c = 0; c < channels; ++c, ++bufs) {
1971 if (*bufs == NULL) {
1972 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1973 return err;
1974 } else {
1975 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1976 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1977 return err;
1978 }
1979 }
1980 } else {
1981 /* default transfer behaviour */
1982 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 for (c = 0; c < channels; ++c, ++bufs) {
1984 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1985 if (*bufs == NULL) {
1986 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1987 } else {
1988 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1989 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1990 return -EFAULT;
1991 }
1992 }
1993 }
1994 return 0;
1995}
1996
Takashi Iwai877211f2005-11-17 13:59:38 +01001997snd_pcm_sframes_t snd_pcm_lib_writev(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;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002009 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2012 return -EINVAL;
2013 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2014 nonblock, snd_pcm_lib_writev_transfer);
2015}
2016
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002017EXPORT_SYMBOL(snd_pcm_lib_writev);
2018
Takashi Iwai877211f2005-11-17 13:59:38 +01002019static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 unsigned int hwoff,
2021 unsigned long data, unsigned int off,
2022 snd_pcm_uframes_t frames)
2023{
Takashi Iwai877211f2005-11-17 13:59:38 +01002024 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 int err;
2026 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2027 if (substream->ops->copy) {
2028 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2029 return err;
2030 } else {
2031 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2033 return -EFAULT;
2034 }
2035 return 0;
2036}
2037
Takashi Iwai877211f2005-11-17 13:59:38 +01002038static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 unsigned long data,
2040 snd_pcm_uframes_t size,
2041 int nonblock,
2042 transfer_f transfer)
2043{
Takashi Iwai877211f2005-11-17 13:59:38 +01002044 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 snd_pcm_uframes_t xfer = 0;
2046 snd_pcm_uframes_t offset = 0;
2047 int err = 0;
2048
2049 if (size == 0)
2050 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 snd_pcm_stream_lock_irq(substream);
2053 switch (runtime->status->state) {
2054 case SNDRV_PCM_STATE_PREPARED:
2055 if (size >= runtime->start_threshold) {
2056 err = snd_pcm_start(substream);
2057 if (err < 0)
2058 goto _end_unlock;
2059 }
2060 break;
2061 case SNDRV_PCM_STATE_DRAINING:
2062 case SNDRV_PCM_STATE_RUNNING:
2063 case SNDRV_PCM_STATE_PAUSED:
2064 break;
2065 case SNDRV_PCM_STATE_XRUN:
2066 err = -EPIPE;
2067 goto _end_unlock;
2068 case SNDRV_PCM_STATE_SUSPENDED:
2069 err = -ESTRPIPE;
2070 goto _end_unlock;
2071 default:
2072 err = -EBADFD;
2073 goto _end_unlock;
2074 }
2075
David Dillow5daeba32010-06-27 00:13:20 +02002076 runtime->twake = runtime->control->avail_min ? : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 while (size > 0) {
2078 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2079 snd_pcm_uframes_t avail;
2080 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01002081 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01002084 if (!avail) {
2085 if (runtime->status->state ==
2086 SNDRV_PCM_STATE_DRAINING) {
2087 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 goto _end_unlock;
2089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (nonblock) {
2091 err = -EAGAIN;
2092 goto _end_unlock;
2093 }
David Dillow5daeba32010-06-27 00:13:20 +02002094 runtime->twake = min_t(snd_pcm_uframes_t, size,
2095 runtime->control->avail_min ? : 1);
2096 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01002097 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01002099 if (!avail)
2100 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 frames = size > avail ? avail : size;
2103 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2104 if (frames > cont)
2105 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002106 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002107 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002108 snd_pcm_stream_unlock_irq(substream);
2109 return -EINVAL;
2110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 appl_ptr = runtime->control->appl_ptr;
2112 appl_ofs = appl_ptr % runtime->buffer_size;
2113 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002114 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002116 if (err < 0)
2117 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 switch (runtime->status->state) {
2119 case SNDRV_PCM_STATE_XRUN:
2120 err = -EPIPE;
2121 goto _end_unlock;
2122 case SNDRV_PCM_STATE_SUSPENDED:
2123 err = -ESTRPIPE;
2124 goto _end_unlock;
2125 default:
2126 break;
2127 }
2128 appl_ptr += frames;
2129 if (appl_ptr >= runtime->boundary)
2130 appl_ptr -= runtime->boundary;
2131 runtime->control->appl_ptr = appl_ptr;
2132 if (substream->ops->ack)
2133 substream->ops->ack(substream);
2134
2135 offset += frames;
2136 size -= frames;
2137 xfer += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
2139 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002140 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002141 if (xfer > 0 && err >= 0)
2142 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2145}
2146
Takashi Iwai877211f2005-11-17 13:59:38 +01002147snd_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 -07002148{
Takashi Iwai877211f2005-11-17 13:59:38 +01002149 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002151 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002153 err = pcm_sanity_check(substream);
2154 if (err < 0)
2155 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002157 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2159 return -EINVAL;
2160 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2161}
2162
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002163EXPORT_SYMBOL(snd_pcm_lib_read);
2164
Takashi Iwai877211f2005-11-17 13:59:38 +01002165static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 unsigned int hwoff,
2167 unsigned long data, unsigned int off,
2168 snd_pcm_uframes_t frames)
2169{
Takashi Iwai877211f2005-11-17 13:59:38 +01002170 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 int err;
2172 void __user **bufs = (void __user **)data;
2173 int channels = runtime->channels;
2174 int c;
2175 if (substream->ops->copy) {
2176 for (c = 0; c < channels; ++c, ++bufs) {
2177 char __user *buf;
2178 if (*bufs == NULL)
2179 continue;
2180 buf = *bufs + samples_to_bytes(runtime, off);
2181 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2182 return err;
2183 }
2184 } else {
2185 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 for (c = 0; c < channels; ++c, ++bufs) {
2187 char *hwbuf;
2188 char __user *buf;
2189 if (*bufs == NULL)
2190 continue;
2191
2192 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2193 buf = *bufs + samples_to_bytes(runtime, off);
2194 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2195 return -EFAULT;
2196 }
2197 }
2198 return 0;
2199}
2200
Takashi Iwai877211f2005-11-17 13:59:38 +01002201snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 void __user **bufs,
2203 snd_pcm_uframes_t frames)
2204{
Takashi Iwai877211f2005-11-17 13:59:38 +01002205 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002207 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002209 err = pcm_sanity_check(substream);
2210 if (err < 0)
2211 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2214 return -EBADFD;
2215
Takashi Iwai0df63e42006-04-28 15:13:41 +02002216 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2218 return -EINVAL;
2219 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2220}
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222EXPORT_SYMBOL(snd_pcm_lib_readv);