blob: 3dc029e106a25af795f3b18f027bc4af5114197f [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>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include <sound/control.h>
Takashi Iwai2d3391e2012-07-27 18:27:00 +020029#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <sound/info.h>
31#include <sound/pcm.h>
32#include <sound/pcm_params.h>
33#include <sound/timer.h>
34
35/*
36 * fill ring buffer with silence
37 * runtime->silence_start: starting pointer to silence area
38 * runtime->silence_filled: size filled with silence
39 * runtime->silence_threshold: threshold from application
40 * runtime->silence_size: maximal size from application
41 *
42 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
43 */
Takashi Iwai877211f2005-11-17 13:59:38 +010044void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Takashi Iwai877211f2005-11-17 13:59:38 +010046 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 snd_pcm_uframes_t frames, ofs, transfer;
48
49 if (runtime->silence_size < runtime->boundary) {
50 snd_pcm_sframes_t noise_dist, n;
51 if (runtime->silence_start != runtime->control->appl_ptr) {
52 n = runtime->control->appl_ptr - runtime->silence_start;
53 if (n < 0)
54 n += runtime->boundary;
55 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
56 runtime->silence_filled -= n;
57 else
58 runtime->silence_filled = 0;
59 runtime->silence_start = runtime->control->appl_ptr;
60 }
Takashi Iwai235475c2005-12-07 15:28:07 +010061 if (runtime->silence_filled >= runtime->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
64 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
65 return;
66 frames = runtime->silence_threshold - noise_dist;
67 if (frames > runtime->silence_size)
68 frames = runtime->silence_size;
69 } else {
70 if (new_hw_ptr == ULONG_MAX) { /* initialization */
71 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
Jaroslav Kysela9e216e82010-07-19 16:37:39 +020072 if (avail > runtime->buffer_size)
73 avail = runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 runtime->silence_filled = avail > 0 ? avail : 0;
75 runtime->silence_start = (runtime->status->hw_ptr +
76 runtime->silence_filled) %
77 runtime->boundary;
78 } else {
79 ofs = runtime->status->hw_ptr;
80 frames = new_hw_ptr - ofs;
81 if ((snd_pcm_sframes_t)frames < 0)
82 frames += runtime->boundary;
83 runtime->silence_filled -= frames;
84 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
85 runtime->silence_filled = 0;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020086 runtime->silence_start = new_hw_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 } else {
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020088 runtime->silence_start = ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91 frames = runtime->buffer_size - runtime->silence_filled;
92 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +020093 if (snd_BUG_ON(frames > runtime->buffer_size))
94 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (frames == 0)
96 return;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020097 ofs = runtime->silence_start % runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 while (frames > 0) {
99 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
100 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
101 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
102 if (substream->ops->silence) {
103 int err;
104 err = substream->ops->silence(substream, -1, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200105 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 } else {
107 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
108 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
109 }
110 } else {
111 unsigned int c;
112 unsigned int channels = runtime->channels;
113 if (substream->ops->silence) {
114 for (c = 0; c < channels; ++c) {
115 int err;
116 err = substream->ops->silence(substream, c, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200117 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119 } else {
120 size_t dma_csize = runtime->dma_bytes / channels;
121 for (c = 0; c < channels; ++c) {
122 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
123 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
124 }
125 }
126 }
127 runtime->silence_filled += transfer;
128 frames -= transfer;
129 ofs = 0;
130 }
131}
132
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200133#ifdef CONFIG_SND_DEBUG
134void snd_pcm_debug_name(struct snd_pcm_substream *substream,
Takashi Iwaic0070112009-06-08 15:58:48 +0200135 char *name, size_t len)
136{
137 snprintf(name, len, "pcmC%dD%d%c:%d",
138 substream->pcm->card->number,
139 substream->pcm->device,
140 substream->stream ? 'c' : 'p',
141 substream->number);
142}
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200143EXPORT_SYMBOL(snd_pcm_debug_name);
144#endif
Takashi Iwaic0070112009-06-08 15:58:48 +0200145
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100146#define XRUN_DEBUG_BASIC (1<<0)
147#define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
148#define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
149#define XRUN_DEBUG_PERIODUPDATE (1<<3) /* full period update info */
150#define XRUN_DEBUG_HWPTRUPDATE (1<<4) /* full hwptr update info */
151#define XRUN_DEBUG_LOG (1<<5) /* show last 10 positions on err */
152#define XRUN_DEBUG_LOGONCE (1<<6) /* do above only once */
153
154#ifdef CONFIG_SND_PCM_XRUN_DEBUG
155
156#define xrun_debug(substream, mask) \
157 ((substream)->pstr->xrun_debug & (mask))
Jarkko Nikula0f170142010-03-26 16:07:25 +0200158#else
159#define xrun_debug(substream, mask) 0
160#endif
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100161
162#define dump_stack_on_xrun(substream) do { \
163 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
164 dump_stack(); \
165 } while (0)
166
Takashi Iwai877211f2005-11-17 13:59:38 +0100167static void xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200169 struct snd_pcm_runtime *runtime = substream->runtime;
170
171 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
172 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100174 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
Takashi Iwaic0070112009-06-08 15:58:48 +0200175 char name[16];
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200176 snd_pcm_debug_name(substream, name, sizeof(name));
Takashi Iwaic0070112009-06-08 15:58:48 +0200177 snd_printd(KERN_DEBUG "XRUN: %s\n", name);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100178 dump_stack_on_xrun(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Jarkko Nikula0f170142010-03-26 16:07:25 +0200182#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100183#define hw_ptr_error(substream, fmt, args...) \
184 do { \
185 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100186 xrun_log_show(substream); \
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100187 if (printk_ratelimit()) { \
188 snd_printd("PCM: " fmt, ##args); \
189 } \
190 dump_stack_on_xrun(substream); \
191 } \
192 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100194#define XRUN_LOG_CNT 10
195
196struct hwptr_log_entry {
Ben Gardinerec08b142011-05-18 10:03:34 -0400197 unsigned int in_interrupt;
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100198 unsigned long jiffies;
199 snd_pcm_uframes_t pos;
200 snd_pcm_uframes_t period_size;
201 snd_pcm_uframes_t buffer_size;
202 snd_pcm_uframes_t old_hw_ptr;
203 snd_pcm_uframes_t hw_ptr_base;
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100204};
205
206struct snd_pcm_hwptr_log {
207 unsigned int idx;
208 unsigned int hit: 1;
209 struct hwptr_log_entry entries[XRUN_LOG_CNT];
210};
211
212static void xrun_log(struct snd_pcm_substream *substream,
Ben Gardinerec08b142011-05-18 10:03:34 -0400213 snd_pcm_uframes_t pos, int in_interrupt)
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100214{
215 struct snd_pcm_runtime *runtime = substream->runtime;
216 struct snd_pcm_hwptr_log *log = runtime->hwptr_log;
217 struct hwptr_log_entry *entry;
218
219 if (log == NULL) {
220 log = kzalloc(sizeof(*log), GFP_ATOMIC);
221 if (log == NULL)
222 return;
223 runtime->hwptr_log = log;
224 } else {
225 if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
226 return;
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200227 }
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100228 entry = &log->entries[log->idx];
Ben Gardinerec08b142011-05-18 10:03:34 -0400229 entry->in_interrupt = in_interrupt;
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100230 entry->jiffies = jiffies;
231 entry->pos = pos;
232 entry->period_size = runtime->period_size;
Joe Perchesc80c1d52010-11-14 19:05:02 -0800233 entry->buffer_size = runtime->buffer_size;
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100234 entry->old_hw_ptr = runtime->status->hw_ptr;
235 entry->hw_ptr_base = runtime->hw_ptr_base;
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100236 log->idx = (log->idx + 1) % XRUN_LOG_CNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100239static void xrun_log_show(struct snd_pcm_substream *substream)
240{
241 struct snd_pcm_hwptr_log *log = substream->runtime->hwptr_log;
242 struct hwptr_log_entry *entry;
243 char name[16];
244 unsigned int idx;
245 int cnt;
246
247 if (log == NULL)
248 return;
249 if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
250 return;
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200251 snd_pcm_debug_name(substream, name, sizeof(name));
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100252 for (cnt = 0, idx = log->idx; cnt < XRUN_LOG_CNT; cnt++) {
253 entry = &log->entries[idx];
254 if (entry->period_size == 0)
255 break;
Ben Gardinerec08b142011-05-18 10:03:34 -0400256 snd_printd("hwptr log: %s: %sj=%lu, pos=%ld/%ld/%ld, "
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100257 "hwptr=%ld/%ld\n",
Ben Gardinerec08b142011-05-18 10:03:34 -0400258 name, entry->in_interrupt ? "[Q] " : "",
259 entry->jiffies,
260 (unsigned long)entry->pos,
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100261 (unsigned long)entry->period_size,
262 (unsigned long)entry->buffer_size,
263 (unsigned long)entry->old_hw_ptr,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100264 (unsigned long)entry->hw_ptr_base);
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100265 idx++;
266 idx %= XRUN_LOG_CNT;
267 }
268 log->hit = 1;
269}
270
271#else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
272
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100273#define hw_ptr_error(substream, fmt, args...) do { } while (0)
Ben Gardiner217658f2011-05-18 23:52:38 -0400274#define xrun_log(substream, pos, in_interrupt) do { } while (0)
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100275#define xrun_log_show(substream) do { } while (0)
276
277#endif
278
Jaroslav Kysela12509322010-01-07 15:36:31 +0100279int snd_pcm_update_state(struct snd_pcm_substream *substream,
280 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 snd_pcm_uframes_t avail;
283
284 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
285 avail = snd_pcm_playback_avail(runtime);
286 else
287 avail = snd_pcm_capture_avail(runtime);
288 if (avail > runtime->avail_max)
289 runtime->avail_max = avail;
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200290 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
291 if (avail >= runtime->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 snd_pcm_drain_done(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200293 return -EPIPE;
294 }
295 } else {
296 if (avail >= runtime->stop_threshold) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 xrun(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200298 return -EPIPE;
299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
David Dillow5daeba32010-06-27 00:13:20 +0200301 if (runtime->twake) {
302 if (avail >= runtime->twake)
303 wake_up(&runtime->tsleep);
304 } else if (avail >= runtime->control->avail_min)
305 wake_up(&runtime->sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return 0;
307}
308
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100309static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
310 unsigned int in_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Takashi Iwai877211f2005-11-17 13:59:38 +0100312 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 snd_pcm_uframes_t pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100314 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200315 snd_pcm_sframes_t hdelta, delta;
316 unsigned long jdelta;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500317 unsigned long curr_jiffies;
318 struct timespec curr_tstamp;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500319 int crossed_boundary = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200321 old_hw_ptr = runtime->status->hw_ptr;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500322
323 /*
324 * group pointer, time and jiffies reads to allow for more
325 * accurate correlations/corrections.
326 * The values are stored at the end of this routine after
327 * corrections for hw_ptr position
328 */
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100329 pos = substream->ops->pointer(substream);
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500330 curr_jiffies = jiffies;
331 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
332 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (pos == SNDRV_PCM_POS_XRUN) {
335 xrun(substream);
336 return -EPIPE;
337 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100338 if (pos >= runtime->buffer_size) {
339 if (printk_ratelimit()) {
340 char name[16];
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200341 snd_pcm_debug_name(substream, name, sizeof(name));
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100342 xrun_log_show(substream);
343 snd_printd(KERN_ERR "BUG: %s, pos = %ld, "
344 "buffer size = %ld, period size = %ld\n",
345 name, pos, runtime->buffer_size,
346 runtime->period_size);
347 }
348 pos = 0;
Takashi Iwaicedb8112009-07-23 11:04:13 +0200349 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100350 pos -= pos % runtime->min_align;
351 if (xrun_debug(substream, XRUN_DEBUG_LOG))
Ben Gardinerec08b142011-05-18 10:03:34 -0400352 xrun_log(substream, pos, in_interrupt);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100353 hw_base = runtime->hw_ptr_base;
354 new_hw_ptr = hw_base + pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100355 if (in_interrupt) {
356 /* we know that one period was processed */
357 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100358 delta = runtime->hw_ptr_interrupt + runtime->period_size;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100359 if (delta > new_hw_ptr) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200360 /* check for double acknowledged interrupts */
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500361 hdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200362 if (hdelta > runtime->hw_ptr_buffer_jiffies/2) {
363 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500364 if (hw_base >= runtime->boundary) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200365 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500366 crossed_boundary++;
367 }
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200368 new_hw_ptr = hw_base + pos;
369 goto __delta;
370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100373 /* new_hw_ptr might be lower than old_hw_ptr in case when */
374 /* pointer crosses the end of the ring buffer */
375 if (new_hw_ptr < old_hw_ptr) {
376 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500377 if (hw_base >= runtime->boundary) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100378 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500379 crossed_boundary++;
380 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100381 new_hw_ptr = hw_base + pos;
382 }
383 __delta:
Clemens Ladischb406e612010-05-25 09:01:46 +0200384 delta = new_hw_ptr - old_hw_ptr;
385 if (delta < 0)
386 delta += runtime->boundary;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100387 if (xrun_debug(substream, in_interrupt ?
388 XRUN_DEBUG_PERIODUPDATE : XRUN_DEBUG_HWPTRUPDATE)) {
389 char name[16];
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200390 snd_pcm_debug_name(substream, name, sizeof(name));
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100391 snd_printd("%s_update: %s: pos=%u/%u/%u, "
392 "hwptr=%ld/%ld/%ld/%ld\n",
393 in_interrupt ? "period" : "hwptr",
394 name,
395 (unsigned int)pos,
396 (unsigned int)runtime->period_size,
397 (unsigned int)runtime->buffer_size,
398 (unsigned long)delta,
399 (unsigned long)old_hw_ptr,
400 (unsigned long)new_hw_ptr,
401 (unsigned long)runtime->hw_ptr_base);
402 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100403
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100404 if (runtime->no_period_wakeup) {
Kelly Anderson12ff4142011-04-01 11:58:25 +0200405 snd_pcm_sframes_t xrun_threshold;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100406 /*
407 * Without regular period interrupts, we have to check
408 * the elapsed time to detect xruns.
409 */
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500410 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Clemens Ladisch47228e42010-11-18 09:53:07 +0100411 if (jdelta < runtime->hw_ptr_buffer_jiffies / 2)
412 goto no_delta_check;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100413 hdelta = jdelta - delta * HZ / runtime->rate;
Kelly Anderson12ff4142011-04-01 11:58:25 +0200414 xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
415 while (hdelta > xrun_threshold) {
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100416 delta += runtime->buffer_size;
417 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500418 if (hw_base >= runtime->boundary) {
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100419 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500420 crossed_boundary++;
421 }
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100422 new_hw_ptr = hw_base + pos;
423 hdelta -= runtime->hw_ptr_buffer_jiffies;
424 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100425 goto no_delta_check;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100426 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100427
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100428 /* something must be really wrong */
Jaroslav Kysela7b3a1772010-01-08 08:43:01 +0100429 if (delta >= runtime->buffer_size + runtime->period_size) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100430 hw_ptr_error(substream,
431 "Unexpected hw_pointer value %s"
432 "(stream=%i, pos=%ld, new_hw_ptr=%ld, "
433 "old_hw_ptr=%ld)\n",
434 in_interrupt ? "[Q] " : "[P]",
435 substream->stream, (long)pos,
436 (long)new_hw_ptr, (long)old_hw_ptr);
437 return 0;
438 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200439
440 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100441 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
Takashi Iwaic87d9732009-05-27 10:53:33 +0200442 goto no_jiffies_check;
443
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200444 /* Skip the jiffies check for hardwares with BATCH flag.
445 * Such hardware usually just increases the position at each IRQ,
446 * thus it can't give any strange position.
447 */
448 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
449 goto no_jiffies_check;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100450 hdelta = delta;
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200451 if (hdelta < runtime->delay)
452 goto no_jiffies_check;
453 hdelta -= runtime->delay;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500454 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200455 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
456 delta = jdelta /
457 (((runtime->period_size * HZ) / runtime->rate)
458 + HZ/100);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100459 /* move new_hw_ptr according jiffies not pos variable */
460 new_hw_ptr = old_hw_ptr;
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100461 hw_base = delta;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100462 /* use loop to avoid checks for delta overflows */
463 /* the delta value is small or zero in most cases */
464 while (delta > 0) {
465 new_hw_ptr += runtime->period_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500466 if (new_hw_ptr >= runtime->boundary) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100467 new_hw_ptr -= runtime->boundary;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500468 crossed_boundary--;
469 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100470 delta--;
471 }
472 /* align hw_base to buffer_size */
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200473 hw_ptr_error(substream,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100474 "hw_ptr skipping! %s"
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200475 "(pos=%ld, delta=%ld, period=%ld, "
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100476 "jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
477 in_interrupt ? "[Q] " : "",
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200478 (long)pos, (long)hdelta,
479 (long)runtime->period_size, jdelta,
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100480 ((hdelta * HZ) / runtime->rate), hw_base,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100481 (unsigned long)old_hw_ptr,
482 (unsigned long)new_hw_ptr);
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100483 /* reset values to proper state */
484 delta = 0;
485 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200486 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200487 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200488 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100489 hw_ptr_error(substream,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100490 "Lost interrupts? %s"
491 "(stream=%i, delta=%ld, new_hw_ptr=%ld, "
492 "old_hw_ptr=%ld)\n",
493 in_interrupt ? "[Q] " : "",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100494 substream->stream, (long)delta,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100495 (long)new_hw_ptr,
496 (long)old_hw_ptr);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100497 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100498
Clemens Ladischab69a492010-11-15 10:46:23 +0100499 no_delta_check:
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100500 if (runtime->status->hw_ptr == new_hw_ptr)
501 return 0;
Takashi Iwaiab1863f2009-06-07 12:09:17 +0200502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
504 runtime->silence_size > 0)
505 snd_pcm_playback_silence(substream, new_hw_ptr);
506
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100507 if (in_interrupt) {
Clemens Ladischead40462010-05-21 09:15:59 +0200508 delta = new_hw_ptr - runtime->hw_ptr_interrupt;
509 if (delta < 0)
510 delta += runtime->boundary;
511 delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
512 runtime->hw_ptr_interrupt += delta;
513 if (runtime->hw_ptr_interrupt >= runtime->boundary)
514 runtime->hw_ptr_interrupt -= runtime->boundary;
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100515 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100516 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 runtime->status->hw_ptr = new_hw_ptr;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500518 runtime->hw_ptr_jiffies = curr_jiffies;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500519 if (crossed_boundary) {
520 snd_BUG_ON(crossed_boundary != 1);
521 runtime->hw_ptr_wrap += runtime->boundary;
522 }
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200523 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500524 runtime->status->tstamp = curr_tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Jaroslav Kysela12509322010-01-07 15:36:31 +0100526 return snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100530int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100532 return snd_pcm_update_hw_ptr0(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535/**
536 * snd_pcm_set_ops - set the PCM operators
537 * @pcm: the pcm instance
538 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
539 * @ops: the operator table
540 *
541 * Sets the given PCM operators to the pcm instance.
542 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100543void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Takashi Iwai877211f2005-11-17 13:59:38 +0100545 struct snd_pcm_str *stream = &pcm->streams[direction];
546 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 for (substream = stream->substream; substream != NULL; substream = substream->next)
549 substream->ops = ops;
550}
551
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200552EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554/**
555 * snd_pcm_sync - set the PCM sync id
556 * @substream: the pcm substream
557 *
558 * Sets the PCM sync identifier for the card.
559 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100560void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Takashi Iwai877211f2005-11-17 13:59:38 +0100562 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 runtime->sync.id32[0] = substream->pcm->card->number;
565 runtime->sync.id32[1] = -1;
566 runtime->sync.id32[2] = -1;
567 runtime->sync.id32[3] = -1;
568}
569
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200570EXPORT_SYMBOL(snd_pcm_set_sync);
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/*
573 * Standard ioctl routine
574 */
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576static inline unsigned int div32(unsigned int a, unsigned int b,
577 unsigned int *r)
578{
579 if (b == 0) {
580 *r = 0;
581 return UINT_MAX;
582 }
583 *r = a % b;
584 return a / b;
585}
586
587static inline unsigned int div_down(unsigned int a, unsigned int b)
588{
589 if (b == 0)
590 return UINT_MAX;
591 return a / b;
592}
593
594static inline unsigned int div_up(unsigned int a, unsigned int b)
595{
596 unsigned int r;
597 unsigned int q;
598 if (b == 0)
599 return UINT_MAX;
600 q = div32(a, b, &r);
601 if (r)
602 ++q;
603 return q;
604}
605
606static inline unsigned int mul(unsigned int a, unsigned int b)
607{
608 if (a == 0)
609 return 0;
610 if (div_down(UINT_MAX, a) < b)
611 return UINT_MAX;
612 return a * b;
613}
614
615static inline unsigned int muldiv32(unsigned int a, unsigned int b,
616 unsigned int c, unsigned int *r)
617{
618 u_int64_t n = (u_int64_t) a * b;
619 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200620 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 *r = 0;
622 return UINT_MAX;
623 }
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200624 n = div_u64_rem(n, c, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (n >= UINT_MAX) {
626 *r = 0;
627 return UINT_MAX;
628 }
629 return n;
630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/**
633 * snd_interval_refine - refine the interval value of configurator
634 * @i: the interval value to refine
635 * @v: the interval value to refer to
636 *
637 * Refines the interval value with the reference value.
638 * The interval is changed to the range satisfying both intervals.
639 * The interval status (min, max, integer, etc.) are evaluated.
640 *
641 * Returns non-zero if the value is changed, zero if not changed.
642 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100643int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200646 if (snd_BUG_ON(snd_interval_empty(i)))
647 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (i->min < v->min) {
649 i->min = v->min;
650 i->openmin = v->openmin;
651 changed = 1;
652 } else if (i->min == v->min && !i->openmin && v->openmin) {
653 i->openmin = 1;
654 changed = 1;
655 }
656 if (i->max > v->max) {
657 i->max = v->max;
658 i->openmax = v->openmax;
659 changed = 1;
660 } else if (i->max == v->max && !i->openmax && v->openmax) {
661 i->openmax = 1;
662 changed = 1;
663 }
664 if (!i->integer && v->integer) {
665 i->integer = 1;
666 changed = 1;
667 }
668 if (i->integer) {
669 if (i->openmin) {
670 i->min++;
671 i->openmin = 0;
672 }
673 if (i->openmax) {
674 i->max--;
675 i->openmax = 0;
676 }
677 } else if (!i->openmin && !i->openmax && i->min == i->max)
678 i->integer = 1;
679 if (snd_interval_checkempty(i)) {
680 snd_interval_none(i);
681 return -EINVAL;
682 }
683 return changed;
684}
685
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200686EXPORT_SYMBOL(snd_interval_refine);
687
Takashi Iwai877211f2005-11-17 13:59:38 +0100688static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200690 if (snd_BUG_ON(snd_interval_empty(i)))
691 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (snd_interval_single(i))
693 return 0;
694 i->max = i->min;
695 i->openmax = i->openmin;
696 if (i->openmax)
697 i->max++;
698 return 1;
699}
700
Takashi Iwai877211f2005-11-17 13:59:38 +0100701static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200703 if (snd_BUG_ON(snd_interval_empty(i)))
704 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (snd_interval_single(i))
706 return 0;
707 i->min = i->max;
708 i->openmin = i->openmax;
709 if (i->openmin)
710 i->min--;
711 return 1;
712}
713
Takashi Iwai877211f2005-11-17 13:59:38 +0100714void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 if (a->empty || b->empty) {
717 snd_interval_none(c);
718 return;
719 }
720 c->empty = 0;
721 c->min = mul(a->min, b->min);
722 c->openmin = (a->openmin || b->openmin);
723 c->max = mul(a->max, b->max);
724 c->openmax = (a->openmax || b->openmax);
725 c->integer = (a->integer && b->integer);
726}
727
728/**
729 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200730 * @a: dividend
731 * @b: divisor
732 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 *
734 * c = a / b
735 *
736 * Returns non-zero if the value is changed, zero if not changed.
737 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100738void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 unsigned int r;
741 if (a->empty || b->empty) {
742 snd_interval_none(c);
743 return;
744 }
745 c->empty = 0;
746 c->min = div32(a->min, b->max, &r);
747 c->openmin = (r || a->openmin || b->openmax);
748 if (b->min > 0) {
749 c->max = div32(a->max, b->min, &r);
750 if (r) {
751 c->max++;
752 c->openmax = 1;
753 } else
754 c->openmax = (a->openmax || b->openmin);
755 } else {
756 c->max = UINT_MAX;
757 c->openmax = 0;
758 }
759 c->integer = 0;
760}
761
762/**
763 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200764 * @a: dividend 1
765 * @b: dividend 2
766 * @k: divisor (as integer)
767 * @c: result
768 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 * c = a * b / k
770 *
771 * Returns non-zero if the value is changed, zero if not changed.
772 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100773void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
774 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 unsigned int r;
777 if (a->empty || b->empty) {
778 snd_interval_none(c);
779 return;
780 }
781 c->empty = 0;
782 c->min = muldiv32(a->min, b->min, k, &r);
783 c->openmin = (r || a->openmin || b->openmin);
784 c->max = muldiv32(a->max, b->max, k, &r);
785 if (r) {
786 c->max++;
787 c->openmax = 1;
788 } else
789 c->openmax = (a->openmax || b->openmax);
790 c->integer = 0;
791}
792
793/**
794 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200795 * @a: dividend 1
796 * @k: dividend 2 (as integer)
797 * @b: divisor
798 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 *
800 * c = a * k / b
801 *
802 * Returns non-zero if the value is changed, zero if not changed.
803 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100804void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
805 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 unsigned int r;
808 if (a->empty || b->empty) {
809 snd_interval_none(c);
810 return;
811 }
812 c->empty = 0;
813 c->min = muldiv32(a->min, k, b->max, &r);
814 c->openmin = (r || a->openmin || b->openmax);
815 if (b->min > 0) {
816 c->max = muldiv32(a->max, k, b->min, &r);
817 if (r) {
818 c->max++;
819 c->openmax = 1;
820 } else
821 c->openmax = (a->openmax || b->openmin);
822 } else {
823 c->max = UINT_MAX;
824 c->openmax = 0;
825 }
826 c->integer = 0;
827}
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829/* ---- */
830
831
832/**
833 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200834 * @i: interval to refine
835 * @rats_count: number of ratnum_t
836 * @rats: ratnum_t array
837 * @nump: pointer to store the resultant numerator
838 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 *
840 * Returns non-zero if the value is changed, zero if not changed.
841 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100842int snd_interval_ratnum(struct snd_interval *i,
843 unsigned int rats_count, struct snd_ratnum *rats,
844 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Krzysztof Helt8374e242009-12-21 17:07:08 +0100846 unsigned int best_num, best_den;
847 int best_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100849 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 int err;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100851 unsigned int result_num, result_den;
852 int result_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 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->min;
859 int diff;
860 if (q == 0)
861 q = 1;
Krzysztof Helt40962d72009-12-19 18:31:04 +0100862 den = div_up(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (den < rats[k].den_min)
864 continue;
865 if (den > rats[k].den_max)
866 den = rats[k].den_max;
867 else {
868 unsigned int r;
869 r = (den - rats[k].den_min) % rats[k].den_step;
870 if (r != 0)
871 den -= r;
872 }
873 diff = num - q * den;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100874 if (diff < 0)
875 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (best_num == 0 ||
877 diff * best_den < best_diff * den) {
878 best_diff = diff;
879 best_den = den;
880 best_num = num;
881 }
882 }
883 if (best_den == 0) {
884 i->empty = 1;
885 return -EINVAL;
886 }
887 t.min = div_down(best_num, best_den);
888 t.openmin = !!(best_num % best_den);
889
Krzysztof Helt8374e242009-12-21 17:07:08 +0100890 result_num = best_num;
891 result_diff = best_diff;
892 result_den = best_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 best_num = best_den = best_diff = 0;
894 for (k = 0; k < rats_count; ++k) {
895 unsigned int num = rats[k].num;
896 unsigned int den;
897 unsigned int q = i->max;
898 int diff;
899 if (q == 0) {
900 i->empty = 1;
901 return -EINVAL;
902 }
Krzysztof Helt40962d72009-12-19 18:31:04 +0100903 den = div_down(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (den > rats[k].den_max)
905 continue;
906 if (den < rats[k].den_min)
907 den = rats[k].den_min;
908 else {
909 unsigned int r;
910 r = (den - rats[k].den_min) % rats[k].den_step;
911 if (r != 0)
912 den += rats[k].den_step - r;
913 }
914 diff = q * den - num;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100915 if (diff < 0)
916 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (best_num == 0 ||
918 diff * best_den < best_diff * den) {
919 best_diff = diff;
920 best_den = den;
921 best_num = num;
922 }
923 }
924 if (best_den == 0) {
925 i->empty = 1;
926 return -EINVAL;
927 }
928 t.max = div_up(best_num, best_den);
929 t.openmax = !!(best_num % best_den);
930 t.integer = 0;
931 err = snd_interval_refine(i, &t);
932 if (err < 0)
933 return err;
934
935 if (snd_interval_single(i)) {
Krzysztof Helt8374e242009-12-21 17:07:08 +0100936 if (best_diff * result_den < result_diff * best_den) {
937 result_num = best_num;
938 result_den = best_den;
939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (nump)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100941 *nump = result_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (denp)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100943 *denp = result_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945 return err;
946}
947
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200948EXPORT_SYMBOL(snd_interval_ratnum);
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950/**
951 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200952 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100953 * @rats_count: number of struct ratden
954 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200955 * @nump: pointer to store the resultant numerator
956 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 *
958 * Returns non-zero if the value is changed, zero if not changed.
959 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100960static int snd_interval_ratden(struct snd_interval *i,
961 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 unsigned int *nump, unsigned int *denp)
963{
964 unsigned int best_num, best_diff, best_den;
965 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100966 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 int err;
968
969 best_num = best_den = best_diff = 0;
970 for (k = 0; k < rats_count; ++k) {
971 unsigned int num;
972 unsigned int den = rats[k].den;
973 unsigned int q = i->min;
974 int diff;
975 num = mul(q, den);
976 if (num > rats[k].num_max)
977 continue;
978 if (num < rats[k].num_min)
979 num = rats[k].num_max;
980 else {
981 unsigned int r;
982 r = (num - rats[k].num_min) % rats[k].num_step;
983 if (r != 0)
984 num += rats[k].num_step - r;
985 }
986 diff = num - q * den;
987 if (best_num == 0 ||
988 diff * best_den < best_diff * den) {
989 best_diff = diff;
990 best_den = den;
991 best_num = num;
992 }
993 }
994 if (best_den == 0) {
995 i->empty = 1;
996 return -EINVAL;
997 }
998 t.min = div_down(best_num, best_den);
999 t.openmin = !!(best_num % best_den);
1000
1001 best_num = best_den = best_diff = 0;
1002 for (k = 0; k < rats_count; ++k) {
1003 unsigned int num;
1004 unsigned int den = rats[k].den;
1005 unsigned int q = i->max;
1006 int diff;
1007 num = mul(q, den);
1008 if (num < rats[k].num_min)
1009 continue;
1010 if (num > rats[k].num_max)
1011 num = rats[k].num_max;
1012 else {
1013 unsigned int r;
1014 r = (num - rats[k].num_min) % rats[k].num_step;
1015 if (r != 0)
1016 num -= r;
1017 }
1018 diff = q * den - num;
1019 if (best_num == 0 ||
1020 diff * best_den < best_diff * den) {
1021 best_diff = diff;
1022 best_den = den;
1023 best_num = num;
1024 }
1025 }
1026 if (best_den == 0) {
1027 i->empty = 1;
1028 return -EINVAL;
1029 }
1030 t.max = div_up(best_num, best_den);
1031 t.openmax = !!(best_num % best_den);
1032 t.integer = 0;
1033 err = snd_interval_refine(i, &t);
1034 if (err < 0)
1035 return err;
1036
1037 if (snd_interval_single(i)) {
1038 if (nump)
1039 *nump = best_num;
1040 if (denp)
1041 *denp = best_den;
1042 }
1043 return err;
1044}
1045
1046/**
1047 * snd_interval_list - refine the interval value from the list
1048 * @i: the interval value to refine
1049 * @count: the number of elements in the list
1050 * @list: the value list
1051 * @mask: the bit-mask to evaluate
1052 *
1053 * Refines the interval value from the list.
1054 * When mask is non-zero, only the elements corresponding to bit 1 are
1055 * evaluated.
1056 *
1057 * Returns non-zero if the value is changed, zero if not changed.
1058 */
Mark Brown4af87a92012-03-14 19:48:43 +00001059int snd_interval_list(struct snd_interval *i, unsigned int count,
1060 const unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 unsigned int k;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001063 struct snd_interval list_range;
Takashi Iwai0981a262007-02-01 14:53:49 +01001064
1065 if (!count) {
1066 i->empty = 1;
1067 return -EINVAL;
1068 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001069 snd_interval_any(&list_range);
1070 list_range.min = UINT_MAX;
1071 list_range.max = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 for (k = 0; k < count; k++) {
1073 if (mask && !(mask & (1 << k)))
1074 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001075 if (!snd_interval_test(i, list[k]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001077 list_range.min = min(list_range.min, list[k]);
1078 list_range.max = max(list_range.max, list[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001080 return snd_interval_refine(i, &list_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001083EXPORT_SYMBOL(snd_interval_list);
1084
Takashi Iwai877211f2005-11-17 13:59:38 +01001085static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
1087 unsigned int n;
1088 int changed = 0;
1089 n = (i->min - min) % step;
1090 if (n != 0 || i->openmin) {
1091 i->min += step - n;
1092 changed = 1;
1093 }
1094 n = (i->max - min) % step;
1095 if (n != 0 || i->openmax) {
1096 i->max -= n;
1097 changed = 1;
1098 }
1099 if (snd_interval_checkempty(i)) {
1100 i->empty = 1;
1101 return -EINVAL;
1102 }
1103 return changed;
1104}
1105
1106/* Info constraints helpers */
1107
1108/**
1109 * snd_pcm_hw_rule_add - add the hw-constraint rule
1110 * @runtime: the pcm runtime instance
1111 * @cond: condition bits
1112 * @var: the variable to evaluate
1113 * @func: the evaluation function
1114 * @private: the private data pointer passed to function
1115 * @dep: the dependent variables
1116 *
1117 * Returns zero if successful, or a negative error code on failure.
1118 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001119int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 int var,
1121 snd_pcm_hw_rule_func_t func, void *private,
1122 int dep, ...)
1123{
Takashi Iwai877211f2005-11-17 13:59:38 +01001124 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1125 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 unsigned int k;
1127 va_list args;
1128 va_start(args, dep);
1129 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001130 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 unsigned int new_rules = constrs->rules_all + 16;
1132 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001133 if (!new) {
1134 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return -ENOMEM;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (constrs->rules) {
1138 memcpy(new, constrs->rules,
1139 constrs->rules_num * sizeof(*c));
1140 kfree(constrs->rules);
1141 }
1142 constrs->rules = new;
1143 constrs->rules_all = new_rules;
1144 }
1145 c = &constrs->rules[constrs->rules_num];
1146 c->cond = cond;
1147 c->func = func;
1148 c->var = var;
1149 c->private = private;
1150 k = 0;
1151 while (1) {
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001152 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
1153 va_end(args);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001154 return -EINVAL;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 c->deps[k++] = dep;
1157 if (dep < 0)
1158 break;
1159 dep = va_arg(args, int);
1160 }
1161 constrs->rules_num++;
1162 va_end(args);
1163 return 0;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001164}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001166EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001169 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001170 * @runtime: PCM runtime instance
1171 * @var: hw_params variable to apply the mask
1172 * @mask: the bitmap mask
1173 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001174 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001176int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 u_int32_t mask)
1178{
Takashi Iwai877211f2005-11-17 13:59:38 +01001179 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1180 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 *maskp->bits &= mask;
1182 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1183 if (*maskp->bits == 0)
1184 return -EINVAL;
1185 return 0;
1186}
1187
1188/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001189 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001190 * @runtime: PCM runtime instance
1191 * @var: hw_params variable to apply the mask
1192 * @mask: the 64bit bitmap mask
1193 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001194 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001196int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 u_int64_t mask)
1198{
Takashi Iwai877211f2005-11-17 13:59:38 +01001199 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1200 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 maskp->bits[0] &= (u_int32_t)mask;
1202 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1203 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1204 if (! maskp->bits[0] && ! maskp->bits[1])
1205 return -EINVAL;
1206 return 0;
1207}
1208
1209/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001210 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001211 * @runtime: PCM runtime instance
1212 * @var: hw_params variable to apply the integer constraint
1213 *
1214 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001216int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Takashi Iwai877211f2005-11-17 13:59:38 +01001218 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return snd_interval_setinteger(constrs_interval(constrs, var));
1220}
1221
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001222EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001225 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001226 * @runtime: PCM runtime instance
1227 * @var: hw_params variable to apply the range
1228 * @min: the minimal value
1229 * @max: the maximal value
1230 *
1231 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001233int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 unsigned int min, unsigned int max)
1235{
Takashi Iwai877211f2005-11-17 13:59:38 +01001236 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1237 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 t.min = min;
1239 t.max = max;
1240 t.openmin = t.openmax = 0;
1241 t.integer = 0;
1242 return snd_interval_refine(constrs_interval(constrs, var), &t);
1243}
1244
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001245EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1246
Takashi Iwai877211f2005-11-17 13:59:38 +01001247static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1248 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Takashi Iwai877211f2005-11-17 13:59:38 +01001250 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1252}
1253
1254
1255/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001256 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001257 * @runtime: PCM runtime instance
1258 * @cond: condition bits
1259 * @var: hw_params variable to apply the list constraint
1260 * @l: list
1261 *
1262 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001264int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 unsigned int cond,
1266 snd_pcm_hw_param_t var,
Mark Brown14641892012-07-05 12:15:01 +01001267 const struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 return snd_pcm_hw_rule_add(runtime, cond, var,
Mark Brown14641892012-07-05 12:15:01 +01001270 snd_pcm_hw_rule_list, (void *)l,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 var, -1);
1272}
1273
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001274EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1275
Takashi Iwai877211f2005-11-17 13:59:38 +01001276static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1277 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Takashi Iwai877211f2005-11-17 13:59:38 +01001279 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 unsigned int num = 0, den = 0;
1281 int err;
1282 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1283 r->nrats, r->rats, &num, &den);
1284 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1285 params->rate_num = num;
1286 params->rate_den = den;
1287 }
1288 return err;
1289}
1290
1291/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001292 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001293 * @runtime: PCM runtime instance
1294 * @cond: condition bits
1295 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001296 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001298int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 unsigned int cond,
1300 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001301 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 return snd_pcm_hw_rule_add(runtime, cond, var,
1304 snd_pcm_hw_rule_ratnums, r,
1305 var, -1);
1306}
1307
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001308EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1309
Takashi Iwai877211f2005-11-17 13:59:38 +01001310static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1311 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Takashi Iwai877211f2005-11-17 13:59:38 +01001313 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 unsigned int num = 0, den = 0;
1315 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1316 r->nrats, r->rats, &num, &den);
1317 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1318 params->rate_num = num;
1319 params->rate_den = den;
1320 }
1321 return err;
1322}
1323
1324/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001325 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001326 * @runtime: PCM runtime instance
1327 * @cond: condition bits
1328 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001329 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001331int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 unsigned int cond,
1333 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001334 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 return snd_pcm_hw_rule_add(runtime, cond, var,
1337 snd_pcm_hw_rule_ratdens, r,
1338 var, -1);
1339}
1340
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001341EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1342
Takashi Iwai877211f2005-11-17 13:59:38 +01001343static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1344 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 unsigned int l = (unsigned long) rule->private;
1347 int width = l & 0xffff;
1348 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001349 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (snd_interval_single(i) && snd_interval_value(i) == width)
1351 params->msbits = msbits;
1352 return 0;
1353}
1354
1355/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001356 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001357 * @runtime: PCM runtime instance
1358 * @cond: condition bits
1359 * @width: sample bits width
1360 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001362int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 unsigned int cond,
1364 unsigned int width,
1365 unsigned int msbits)
1366{
1367 unsigned long l = (msbits << 16) | width;
1368 return snd_pcm_hw_rule_add(runtime, cond, -1,
1369 snd_pcm_hw_rule_msbits,
1370 (void*) l,
1371 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1372}
1373
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001374EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1375
Takashi Iwai877211f2005-11-17 13:59:38 +01001376static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1377 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 unsigned long step = (unsigned long) rule->private;
1380 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1381}
1382
1383/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001384 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001385 * @runtime: PCM runtime instance
1386 * @cond: condition bits
1387 * @var: hw_params variable to apply the step constraint
1388 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001390int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 unsigned int cond,
1392 snd_pcm_hw_param_t var,
1393 unsigned long step)
1394{
1395 return snd_pcm_hw_rule_add(runtime, cond, var,
1396 snd_pcm_hw_rule_step, (void *) step,
1397 var, -1);
1398}
1399
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001400EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1401
Takashi Iwai877211f2005-11-17 13:59:38 +01001402static 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 -07001403{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001404 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1406 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1407 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1408 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1409 };
1410 return snd_interval_list(hw_param_interval(params, rule->var),
1411 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1412}
1413
1414/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001415 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001416 * @runtime: PCM runtime instance
1417 * @cond: condition bits
1418 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001420int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 unsigned int cond,
1422 snd_pcm_hw_param_t var)
1423{
1424 return snd_pcm_hw_rule_add(runtime, cond, var,
1425 snd_pcm_hw_rule_pow2, NULL,
1426 var, -1);
1427}
1428
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001429EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1430
Clemens Ladischd5b702a2011-09-16 23:03:02 +02001431static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
1432 struct snd_pcm_hw_rule *rule)
1433{
1434 unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
1435 struct snd_interval *rate;
1436
1437 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1438 return snd_interval_list(rate, 1, &base_rate, 0);
1439}
1440
1441/**
1442 * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
1443 * @runtime: PCM runtime instance
1444 * @base_rate: the rate at which the hardware does not resample
1445 */
1446int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
1447 unsigned int base_rate)
1448{
1449 return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
1450 SNDRV_PCM_HW_PARAM_RATE,
1451 snd_pcm_hw_rule_noresample_func,
1452 (void *)(uintptr_t)base_rate,
1453 SNDRV_PCM_HW_PARAM_RATE, -1);
1454}
1455EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
1456
Takashi Iwai877211f2005-11-17 13:59:38 +01001457static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001458 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
1460 if (hw_is_mask(var)) {
1461 snd_mask_any(hw_param_mask(params, var));
1462 params->cmask |= 1 << var;
1463 params->rmask |= 1 << var;
1464 return;
1465 }
1466 if (hw_is_interval(var)) {
1467 snd_interval_any(hw_param_interval(params, var));
1468 params->cmask |= 1 << var;
1469 params->rmask |= 1 << var;
1470 return;
1471 }
1472 snd_BUG();
1473}
1474
Takashi Iwai877211f2005-11-17 13:59:38 +01001475void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
1477 unsigned int k;
1478 memset(params, 0, sizeof(*params));
1479 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1480 _snd_pcm_hw_param_any(params, k);
1481 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1482 _snd_pcm_hw_param_any(params, k);
1483 params->info = ~0U;
1484}
1485
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001486EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001489 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001490 * @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 * Return the value for field @var if it's fixed in configuration space
1495 * defined by @params. Return -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001497int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1498 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001501 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (!snd_mask_single(mask))
1503 return -EINVAL;
1504 if (dir)
1505 *dir = 0;
1506 return snd_mask_value(mask);
1507 }
1508 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001509 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (!snd_interval_single(i))
1511 return -EINVAL;
1512 if (dir)
1513 *dir = i->openmin;
1514 return snd_interval_value(i);
1515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return -EINVAL;
1517}
1518
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001519EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Takashi Iwai877211f2005-11-17 13:59:38 +01001521void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 snd_pcm_hw_param_t var)
1523{
1524 if (hw_is_mask(var)) {
1525 snd_mask_none(hw_param_mask(params, var));
1526 params->cmask |= 1 << var;
1527 params->rmask |= 1 << var;
1528 } else if (hw_is_interval(var)) {
1529 snd_interval_none(hw_param_interval(params, var));
1530 params->cmask |= 1 << var;
1531 params->rmask |= 1 << var;
1532 } else {
1533 snd_BUG();
1534 }
1535}
1536
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001537EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Takashi Iwai877211f2005-11-17 13:59:38 +01001539static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001540 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
1542 int changed;
1543 if (hw_is_mask(var))
1544 changed = snd_mask_refine_first(hw_param_mask(params, var));
1545 else if (hw_is_interval(var))
1546 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001547 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (changed) {
1550 params->cmask |= 1 << var;
1551 params->rmask |= 1 << var;
1552 }
1553 return changed;
1554}
1555
1556
1557/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001558 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001559 * @pcm: PCM instance
1560 * @params: the hw_params instance
1561 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001562 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001564 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 * values > minimum. Reduce configuration space accordingly.
1566 * Return the minimum.
1567 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001568int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1569 struct snd_pcm_hw_params *params,
1570 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 int changed = _snd_pcm_hw_param_first(params, var);
1573 if (changed < 0)
1574 return changed;
1575 if (params->rmask) {
1576 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001577 if (snd_BUG_ON(err < 0))
1578 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580 return snd_pcm_hw_param_value(params, var, dir);
1581}
1582
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001583EXPORT_SYMBOL(snd_pcm_hw_param_first);
1584
Takashi Iwai877211f2005-11-17 13:59:38 +01001585static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001586 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
1588 int changed;
1589 if (hw_is_mask(var))
1590 changed = snd_mask_refine_last(hw_param_mask(params, var));
1591 else if (hw_is_interval(var))
1592 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001593 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (changed) {
1596 params->cmask |= 1 << var;
1597 params->rmask |= 1 << var;
1598 }
1599 return changed;
1600}
1601
1602
1603/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001604 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001605 * @pcm: PCM instance
1606 * @params: the hw_params instance
1607 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001608 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001610 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 * values < maximum. Reduce configuration space accordingly.
1612 * Return the maximum.
1613 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001614int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1615 struct snd_pcm_hw_params *params,
1616 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
1618 int changed = _snd_pcm_hw_param_last(params, var);
1619 if (changed < 0)
1620 return changed;
1621 if (params->rmask) {
1622 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001623 if (snd_BUG_ON(err < 0))
1624 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
1626 return snd_pcm_hw_param_value(params, var, dir);
1627}
1628
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001629EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001632 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001633 * @pcm: PCM instance
1634 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001636 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 * The configuration chosen is that obtained fixing in this order:
1638 * first access, first format, first subformat, min channels,
1639 * min rate, min period time, max buffer size, min tick time
1640 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001641int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1642 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001644 static int vars[] = {
1645 SNDRV_PCM_HW_PARAM_ACCESS,
1646 SNDRV_PCM_HW_PARAM_FORMAT,
1647 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1648 SNDRV_PCM_HW_PARAM_CHANNELS,
1649 SNDRV_PCM_HW_PARAM_RATE,
1650 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1651 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1652 SNDRV_PCM_HW_PARAM_TICK_TIME,
1653 -1
1654 };
1655 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001657 for (v = vars; *v != -1; v++) {
1658 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1659 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1660 else
1661 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001662 if (snd_BUG_ON(err < 0))
1663 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return 0;
1666}
1667
Takashi Iwai877211f2005-11-17 13:59:38 +01001668static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 void *arg)
1670{
Takashi Iwai877211f2005-11-17 13:59:38 +01001671 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 unsigned long flags;
1673 snd_pcm_stream_lock_irqsave(substream, flags);
1674 if (snd_pcm_running(substream) &&
1675 snd_pcm_update_hw_ptr(substream) >= 0)
1676 runtime->status->hw_ptr %= runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -05001677 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 runtime->status->hw_ptr = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -05001679 runtime->hw_ptr_wrap = 0;
1680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 snd_pcm_stream_unlock_irqrestore(substream, flags);
1682 return 0;
1683}
1684
Takashi Iwai877211f2005-11-17 13:59:38 +01001685static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 void *arg)
1687{
Takashi Iwai877211f2005-11-17 13:59:38 +01001688 struct snd_pcm_channel_info *info = arg;
1689 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 int width;
1691 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1692 info->offset = -1;
1693 return 0;
1694 }
1695 width = snd_pcm_format_physical_width(runtime->format);
1696 if (width < 0)
1697 return width;
1698 info->offset = 0;
1699 switch (runtime->access) {
1700 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1701 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1702 info->first = info->channel * width;
1703 info->step = runtime->channels * width;
1704 break;
1705 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1706 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1707 {
1708 size_t size = runtime->dma_bytes / runtime->channels;
1709 info->first = info->channel * size * 8;
1710 info->step = width;
1711 break;
1712 }
1713 default:
1714 snd_BUG();
1715 break;
1716 }
1717 return 0;
1718}
1719
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001720static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1721 void *arg)
1722{
1723 struct snd_pcm_hw_params *params = arg;
1724 snd_pcm_format_t format;
1725 int channels, width;
1726
1727 params->fifo_size = substream->runtime->hw.fifo_size;
1728 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1729 format = params_format(params);
1730 channels = params_channels(params);
1731 width = snd_pcm_format_physical_width(format);
1732 params->fifo_size /= width * channels;
1733 }
1734 return 0;
1735}
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737/**
1738 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1739 * @substream: the pcm substream instance
1740 * @cmd: ioctl command
1741 * @arg: ioctl argument
1742 *
1743 * Processes the generic ioctl commands for PCM.
1744 * Can be passed as the ioctl callback for PCM ops.
1745 *
1746 * Returns zero if successful, or a negative error code on failure.
1747 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001748int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 unsigned int cmd, void *arg)
1750{
1751 switch (cmd) {
1752 case SNDRV_PCM_IOCTL1_INFO:
1753 return 0;
1754 case SNDRV_PCM_IOCTL1_RESET:
1755 return snd_pcm_lib_ioctl_reset(substream, arg);
1756 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1757 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001758 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1759 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
1761 return -ENXIO;
1762}
1763
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001764EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766/**
1767 * snd_pcm_period_elapsed - update the pcm status for the next period
1768 * @substream: the pcm substream instance
1769 *
1770 * This function is called from the interrupt handler when the
1771 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001772 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 *
1774 * Even if more than one periods have elapsed since the last call, you
1775 * have to call this only once.
1776 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001777void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
Takashi Iwai877211f2005-11-17 13:59:38 +01001779 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 unsigned long flags;
1781
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001782 if (PCM_RUNTIME_CHECK(substream))
1783 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 if (runtime->transfer_ack_begin)
1787 runtime->transfer_ack_begin(substream);
1788
1789 snd_pcm_stream_lock_irqsave(substream, flags);
1790 if (!snd_pcm_running(substream) ||
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01001791 snd_pcm_update_hw_ptr0(substream, 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 goto _end;
1793
1794 if (substream->timer_running)
1795 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 _end:
1797 snd_pcm_stream_unlock_irqrestore(substream, flags);
1798 if (runtime->transfer_ack_end)
1799 runtime->transfer_ack_end(substream);
1800 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1801}
1802
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001803EXPORT_SYMBOL(snd_pcm_period_elapsed);
1804
Takashi Iwai13075512008-01-08 18:08:14 +01001805/*
1806 * Wait until avail_min data becomes available
1807 * Returns a negative error code if any error occurs during operation.
1808 * The available space is stored on availp. When err = 0 and avail = 0
1809 * on the capture stream, it indicates the stream is in DRAINING state.
1810 */
David Dillow5daeba32010-06-27 00:13:20 +02001811static int wait_for_avail(struct snd_pcm_substream *substream,
Takashi Iwai13075512008-01-08 18:08:14 +01001812 snd_pcm_uframes_t *availp)
1813{
1814 struct snd_pcm_runtime *runtime = substream->runtime;
1815 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1816 wait_queue_t wait;
1817 int err = 0;
1818 snd_pcm_uframes_t avail = 0;
Takashi Iwaif2b36142011-05-26 08:09:38 +02001819 long wait_time, tout;
Takashi Iwai13075512008-01-08 18:08:14 +01001820
Arjan van de Ven763437a2011-09-15 08:49:25 +02001821 init_waitqueue_entry(&wait, current);
1822 set_current_state(TASK_INTERRUPTIBLE);
1823 add_wait_queue(&runtime->tsleep, &wait);
1824
Takashi Iwaif2b36142011-05-26 08:09:38 +02001825 if (runtime->no_period_wakeup)
1826 wait_time = MAX_SCHEDULE_TIMEOUT;
1827 else {
1828 wait_time = 10;
1829 if (runtime->rate) {
1830 long t = runtime->period_size * 2 / runtime->rate;
1831 wait_time = max(t, wait_time);
1832 }
1833 wait_time = msecs_to_jiffies(wait_time * 1000);
1834 }
Arjan van de Ven763437a2011-09-15 08:49:25 +02001835
Takashi Iwai13075512008-01-08 18:08:14 +01001836 for (;;) {
1837 if (signal_pending(current)) {
1838 err = -ERESTARTSYS;
1839 break;
1840 }
Arjan van de Ven763437a2011-09-15 08:49:25 +02001841
1842 /*
1843 * We need to check if space became available already
1844 * (and thus the wakeup happened already) first to close
1845 * the race of space already having become available.
1846 * This check must happen after been added to the waitqueue
1847 * and having current state be INTERRUPTIBLE.
1848 */
1849 if (is_playback)
1850 avail = snd_pcm_playback_avail(runtime);
1851 else
1852 avail = snd_pcm_capture_avail(runtime);
1853 if (avail >= runtime->twake)
1854 break;
Takashi Iwai13075512008-01-08 18:08:14 +01001855 snd_pcm_stream_unlock_irq(substream);
Arjan van de Ven763437a2011-09-15 08:49:25 +02001856
1857 tout = schedule_timeout(wait_time);
1858
Takashi Iwai13075512008-01-08 18:08:14 +01001859 snd_pcm_stream_lock_irq(substream);
Arjan van de Ven763437a2011-09-15 08:49:25 +02001860 set_current_state(TASK_INTERRUPTIBLE);
Takashi Iwai13075512008-01-08 18:08:14 +01001861 switch (runtime->status->state) {
1862 case SNDRV_PCM_STATE_SUSPENDED:
1863 err = -ESTRPIPE;
1864 goto _endloop;
1865 case SNDRV_PCM_STATE_XRUN:
1866 err = -EPIPE;
1867 goto _endloop;
1868 case SNDRV_PCM_STATE_DRAINING:
1869 if (is_playback)
1870 err = -EPIPE;
1871 else
1872 avail = 0; /* indicate draining */
1873 goto _endloop;
1874 case SNDRV_PCM_STATE_OPEN:
1875 case SNDRV_PCM_STATE_SETUP:
1876 case SNDRV_PCM_STATE_DISCONNECTED:
1877 err = -EBADFD;
1878 goto _endloop;
1879 }
1880 if (!tout) {
1881 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1882 is_playback ? "playback" : "capture");
1883 err = -EIO;
1884 break;
1885 }
Takashi Iwai13075512008-01-08 18:08:14 +01001886 }
1887 _endloop:
Arjan van de Ven763437a2011-09-15 08:49:25 +02001888 set_current_state(TASK_RUNNING);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001889 remove_wait_queue(&runtime->tsleep, &wait);
Takashi Iwai13075512008-01-08 18:08:14 +01001890 *availp = avail;
1891 return err;
1892}
1893
Takashi Iwai877211f2005-11-17 13:59:38 +01001894static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 unsigned int hwoff,
1896 unsigned long data, unsigned int off,
1897 snd_pcm_uframes_t frames)
1898{
Takashi Iwai877211f2005-11-17 13:59:38 +01001899 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 int err;
1901 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1902 if (substream->ops->copy) {
1903 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1904 return err;
1905 } else {
1906 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1908 return -EFAULT;
1909 }
1910 return 0;
1911}
1912
Takashi Iwai877211f2005-11-17 13:59:38 +01001913typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 unsigned long data, unsigned int off,
1915 snd_pcm_uframes_t size);
1916
Takashi Iwai877211f2005-11-17 13:59:38 +01001917static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 unsigned long data,
1919 snd_pcm_uframes_t size,
1920 int nonblock,
1921 transfer_f transfer)
1922{
Takashi Iwai877211f2005-11-17 13:59:38 +01001923 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 snd_pcm_uframes_t xfer = 0;
1925 snd_pcm_uframes_t offset = 0;
Takashi Iwai0910c212012-05-11 17:50:49 +02001926 snd_pcm_uframes_t avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 int err = 0;
1928
1929 if (size == 0)
1930 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 snd_pcm_stream_lock_irq(substream);
1933 switch (runtime->status->state) {
1934 case SNDRV_PCM_STATE_PREPARED:
1935 case SNDRV_PCM_STATE_RUNNING:
1936 case SNDRV_PCM_STATE_PAUSED:
1937 break;
1938 case SNDRV_PCM_STATE_XRUN:
1939 err = -EPIPE;
1940 goto _end_unlock;
1941 case SNDRV_PCM_STATE_SUSPENDED:
1942 err = -ESTRPIPE;
1943 goto _end_unlock;
1944 default:
1945 err = -EBADFD;
1946 goto _end_unlock;
1947 }
1948
David Dillow5daeba32010-06-27 00:13:20 +02001949 runtime->twake = runtime->control->avail_min ? : 1;
Takashi Iwai0910c212012-05-11 17:50:49 +02001950 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1951 snd_pcm_update_hw_ptr(substream);
1952 avail = snd_pcm_playback_avail(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 while (size > 0) {
1954 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 snd_pcm_uframes_t cont;
Takashi Iwai13075512008-01-08 18:08:14 +01001956 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (nonblock) {
1958 err = -EAGAIN;
1959 goto _end_unlock;
1960 }
David Dillow5daeba32010-06-27 00:13:20 +02001961 runtime->twake = min_t(snd_pcm_uframes_t, size,
1962 runtime->control->avail_min ? : 1);
1963 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01001964 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 frames = size > avail ? avail : size;
1968 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1969 if (frames > cont)
1970 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001971 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001972 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001973 snd_pcm_stream_unlock_irq(substream);
1974 return -EINVAL;
1975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 appl_ptr = runtime->control->appl_ptr;
1977 appl_ofs = appl_ptr % runtime->buffer_size;
1978 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01001979 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01001981 if (err < 0)
1982 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 switch (runtime->status->state) {
1984 case SNDRV_PCM_STATE_XRUN:
1985 err = -EPIPE;
1986 goto _end_unlock;
1987 case SNDRV_PCM_STATE_SUSPENDED:
1988 err = -ESTRPIPE;
1989 goto _end_unlock;
1990 default:
1991 break;
1992 }
1993 appl_ptr += frames;
1994 if (appl_ptr >= runtime->boundary)
1995 appl_ptr -= runtime->boundary;
1996 runtime->control->appl_ptr = appl_ptr;
1997 if (substream->ops->ack)
1998 substream->ops->ack(substream);
1999
2000 offset += frames;
2001 size -= frames;
2002 xfer += frames;
Takashi Iwai0910c212012-05-11 17:50:49 +02002003 avail -= frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2005 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
2006 err = snd_pcm_start(substream);
2007 if (err < 0)
2008 goto _end_unlock;
2009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
2011 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002012 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002013 if (xfer > 0 && err >= 0)
2014 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2017}
2018
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002019/* sanity-check for read/write methods */
2020static int pcm_sanity_check(struct snd_pcm_substream *substream)
2021{
2022 struct snd_pcm_runtime *runtime;
2023 if (PCM_RUNTIME_CHECK(substream))
2024 return -ENXIO;
2025 runtime = substream->runtime;
2026 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
2027 return -EINVAL;
2028 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2029 return -EBADFD;
2030 return 0;
2031}
2032
Takashi Iwai877211f2005-11-17 13:59:38 +01002033snd_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 -07002034{
Takashi Iwai877211f2005-11-17 13:59:38 +01002035 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002037 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002039 err = pcm_sanity_check(substream);
2040 if (err < 0)
2041 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002043 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2046 runtime->channels > 1)
2047 return -EINVAL;
2048 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
2049 snd_pcm_lib_write_transfer);
2050}
2051
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002052EXPORT_SYMBOL(snd_pcm_lib_write);
2053
Takashi Iwai877211f2005-11-17 13:59:38 +01002054static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 unsigned int hwoff,
2056 unsigned long data, unsigned int off,
2057 snd_pcm_uframes_t frames)
2058{
Takashi Iwai877211f2005-11-17 13:59:38 +01002059 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 int err;
2061 void __user **bufs = (void __user **)data;
2062 int channels = runtime->channels;
2063 int c;
2064 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002065 if (snd_BUG_ON(!substream->ops->silence))
2066 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 for (c = 0; c < channels; ++c, ++bufs) {
2068 if (*bufs == NULL) {
2069 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
2070 return err;
2071 } else {
2072 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2073 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2074 return err;
2075 }
2076 }
2077 } else {
2078 /* default transfer behaviour */
2079 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 for (c = 0; c < channels; ++c, ++bufs) {
2081 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2082 if (*bufs == NULL) {
2083 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
2084 } else {
2085 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2086 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
2087 return -EFAULT;
2088 }
2089 }
2090 }
2091 return 0;
2092}
2093
Takashi Iwai877211f2005-11-17 13:59:38 +01002094snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 void __user **bufs,
2096 snd_pcm_uframes_t frames)
2097{
Takashi Iwai877211f2005-11-17 13:59:38 +01002098 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002100 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002102 err = pcm_sanity_check(substream);
2103 if (err < 0)
2104 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002106 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2109 return -EINVAL;
2110 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2111 nonblock, snd_pcm_lib_writev_transfer);
2112}
2113
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002114EXPORT_SYMBOL(snd_pcm_lib_writev);
2115
Takashi Iwai877211f2005-11-17 13:59:38 +01002116static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 unsigned int hwoff,
2118 unsigned long data, unsigned int off,
2119 snd_pcm_uframes_t frames)
2120{
Takashi Iwai877211f2005-11-17 13:59:38 +01002121 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 int err;
2123 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2124 if (substream->ops->copy) {
2125 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2126 return err;
2127 } else {
2128 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2130 return -EFAULT;
2131 }
2132 return 0;
2133}
2134
Takashi Iwai877211f2005-11-17 13:59:38 +01002135static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 unsigned long data,
2137 snd_pcm_uframes_t size,
2138 int nonblock,
2139 transfer_f transfer)
2140{
Takashi Iwai877211f2005-11-17 13:59:38 +01002141 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 snd_pcm_uframes_t xfer = 0;
2143 snd_pcm_uframes_t offset = 0;
Takashi Iwai0910c212012-05-11 17:50:49 +02002144 snd_pcm_uframes_t avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 int err = 0;
2146
2147 if (size == 0)
2148 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 snd_pcm_stream_lock_irq(substream);
2151 switch (runtime->status->state) {
2152 case SNDRV_PCM_STATE_PREPARED:
2153 if (size >= runtime->start_threshold) {
2154 err = snd_pcm_start(substream);
2155 if (err < 0)
2156 goto _end_unlock;
2157 }
2158 break;
2159 case SNDRV_PCM_STATE_DRAINING:
2160 case SNDRV_PCM_STATE_RUNNING:
2161 case SNDRV_PCM_STATE_PAUSED:
2162 break;
2163 case SNDRV_PCM_STATE_XRUN:
2164 err = -EPIPE;
2165 goto _end_unlock;
2166 case SNDRV_PCM_STATE_SUSPENDED:
2167 err = -ESTRPIPE;
2168 goto _end_unlock;
2169 default:
2170 err = -EBADFD;
2171 goto _end_unlock;
2172 }
2173
David Dillow5daeba32010-06-27 00:13:20 +02002174 runtime->twake = runtime->control->avail_min ? : 1;
Takashi Iwai0910c212012-05-11 17:50:49 +02002175 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2176 snd_pcm_update_hw_ptr(substream);
2177 avail = snd_pcm_capture_avail(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 while (size > 0) {
2179 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 snd_pcm_uframes_t cont;
Takashi Iwai13075512008-01-08 18:08:14 +01002181 if (!avail) {
2182 if (runtime->status->state ==
2183 SNDRV_PCM_STATE_DRAINING) {
2184 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 goto _end_unlock;
2186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if (nonblock) {
2188 err = -EAGAIN;
2189 goto _end_unlock;
2190 }
David Dillow5daeba32010-06-27 00:13:20 +02002191 runtime->twake = min_t(snd_pcm_uframes_t, size,
2192 runtime->control->avail_min ? : 1);
2193 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01002194 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01002196 if (!avail)
2197 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 frames = size > avail ? avail : size;
2200 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2201 if (frames > cont)
2202 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002203 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002204 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002205 snd_pcm_stream_unlock_irq(substream);
2206 return -EINVAL;
2207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 appl_ptr = runtime->control->appl_ptr;
2209 appl_ofs = appl_ptr % runtime->buffer_size;
2210 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002211 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002213 if (err < 0)
2214 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 switch (runtime->status->state) {
2216 case SNDRV_PCM_STATE_XRUN:
2217 err = -EPIPE;
2218 goto _end_unlock;
2219 case SNDRV_PCM_STATE_SUSPENDED:
2220 err = -ESTRPIPE;
2221 goto _end_unlock;
2222 default:
2223 break;
2224 }
2225 appl_ptr += frames;
2226 if (appl_ptr >= runtime->boundary)
2227 appl_ptr -= runtime->boundary;
2228 runtime->control->appl_ptr = appl_ptr;
2229 if (substream->ops->ack)
2230 substream->ops->ack(substream);
2231
2232 offset += frames;
2233 size -= frames;
2234 xfer += frames;
Takashi Iwai0910c212012-05-11 17:50:49 +02002235 avail -= frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002238 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002239 if (xfer > 0 && err >= 0)
2240 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2243}
2244
Takashi Iwai877211f2005-11-17 13:59:38 +01002245snd_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 -07002246{
Takashi Iwai877211f2005-11-17 13:59:38 +01002247 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002249 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002251 err = pcm_sanity_check(substream);
2252 if (err < 0)
2253 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002255 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2257 return -EINVAL;
2258 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2259}
2260
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002261EXPORT_SYMBOL(snd_pcm_lib_read);
2262
Takashi Iwai877211f2005-11-17 13:59:38 +01002263static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 unsigned int hwoff,
2265 unsigned long data, unsigned int off,
2266 snd_pcm_uframes_t frames)
2267{
Takashi Iwai877211f2005-11-17 13:59:38 +01002268 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 int err;
2270 void __user **bufs = (void __user **)data;
2271 int channels = runtime->channels;
2272 int c;
2273 if (substream->ops->copy) {
2274 for (c = 0; c < channels; ++c, ++bufs) {
2275 char __user *buf;
2276 if (*bufs == NULL)
2277 continue;
2278 buf = *bufs + samples_to_bytes(runtime, off);
2279 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2280 return err;
2281 }
2282 } else {
2283 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 for (c = 0; c < channels; ++c, ++bufs) {
2285 char *hwbuf;
2286 char __user *buf;
2287 if (*bufs == NULL)
2288 continue;
2289
2290 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2291 buf = *bufs + samples_to_bytes(runtime, off);
2292 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2293 return -EFAULT;
2294 }
2295 }
2296 return 0;
2297}
2298
Takashi Iwai877211f2005-11-17 13:59:38 +01002299snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 void __user **bufs,
2301 snd_pcm_uframes_t frames)
2302{
Takashi Iwai877211f2005-11-17 13:59:38 +01002303 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002305 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002307 err = pcm_sanity_check(substream);
2308 if (err < 0)
2309 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2312 return -EBADFD;
2313
Takashi Iwai0df63e42006-04-28 15:13:41 +02002314 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2316 return -EINVAL;
2317 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2318}
2319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320EXPORT_SYMBOL(snd_pcm_lib_readv);
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002321
2322/*
2323 * standard channel mapping helpers
2324 */
2325
2326/* default channel maps for multi-channel playbacks, up to 8 channels */
2327const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
2328 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02002329 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002330 { .channels = 2,
2331 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2332 { .channels = 4,
2333 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2334 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2335 { .channels = 6,
2336 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2337 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2338 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
2339 { .channels = 8,
2340 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2341 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2342 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2343 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2344 { }
2345};
2346EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
2347
2348/* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
2349const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
2350 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02002351 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002352 { .channels = 2,
2353 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2354 { .channels = 4,
2355 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2356 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2357 { .channels = 6,
2358 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2359 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2360 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2361 { .channels = 8,
2362 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2363 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2364 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2365 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2366 { }
2367};
2368EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
2369
2370static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
2371{
2372 if (ch > info->max_channels)
2373 return false;
2374 return !info->channel_mask || (info->channel_mask & (1U << ch));
2375}
2376
2377static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2378 struct snd_ctl_elem_info *uinfo)
2379{
2380 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2381
2382 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2383 uinfo->count = 0;
2384 uinfo->count = info->max_channels;
2385 uinfo->value.integer.min = 0;
2386 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2387 return 0;
2388}
2389
2390/* get callback for channel map ctl element
2391 * stores the channel position firstly matching with the current channels
2392 */
2393static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2394 struct snd_ctl_elem_value *ucontrol)
2395{
2396 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2397 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2398 struct snd_pcm_substream *substream;
2399 const struct snd_pcm_chmap_elem *map;
2400
2401 if (snd_BUG_ON(!info->chmap))
2402 return -EINVAL;
2403 substream = snd_pcm_chmap_substream(info, idx);
2404 if (!substream)
2405 return -ENODEV;
2406 memset(ucontrol->value.integer.value, 0,
2407 sizeof(ucontrol->value.integer.value));
2408 if (!substream->runtime)
2409 return 0; /* no channels set */
2410 for (map = info->chmap; map->channels; map++) {
2411 int i;
2412 if (map->channels == substream->runtime->channels &&
2413 valid_chmap_channels(info, map->channels)) {
2414 for (i = 0; i < map->channels; i++)
2415 ucontrol->value.integer.value[i] = map->map[i];
2416 return 0;
2417 }
2418 }
2419 return -EINVAL;
2420}
2421
2422/* tlv callback for channel map ctl element
2423 * expands the pre-defined channel maps in a form of TLV
2424 */
2425static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2426 unsigned int size, unsigned int __user *tlv)
2427{
2428 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2429 const struct snd_pcm_chmap_elem *map;
2430 unsigned int __user *dst;
2431 int c, count = 0;
2432
2433 if (snd_BUG_ON(!info->chmap))
2434 return -EINVAL;
2435 if (size < 8)
2436 return -ENOMEM;
2437 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2438 return -EFAULT;
2439 size -= 8;
2440 dst = tlv + 2;
2441 for (map = info->chmap; map->channels; map++) {
2442 int chs_bytes = map->channels * 4;
2443 if (!valid_chmap_channels(info, map->channels))
2444 continue;
2445 if (size < 8)
2446 return -ENOMEM;
2447 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
2448 put_user(chs_bytes, dst + 1))
2449 return -EFAULT;
2450 dst += 2;
2451 size -= 8;
2452 count += 8;
2453 if (size < chs_bytes)
2454 return -ENOMEM;
2455 size -= chs_bytes;
2456 count += chs_bytes;
2457 for (c = 0; c < map->channels; c++) {
2458 if (put_user(map->map[c], dst))
2459 return -EFAULT;
2460 dst++;
2461 }
2462 }
2463 if (put_user(count, tlv + 1))
2464 return -EFAULT;
2465 return 0;
2466}
2467
2468static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
2469{
2470 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2471 info->pcm->streams[info->stream].chmap_kctl = NULL;
2472 kfree(info);
2473}
2474
2475/**
2476 * snd_pcm_add_chmap_ctls - create channel-mapping control elements
2477 * @pcm: the assigned PCM instance
2478 * @stream: stream direction
2479 * @chmap: channel map elements (for query)
2480 * @max_channels: the max number of channels for the stream
2481 * @private_value: the value passed to each kcontrol's private_value field
2482 * @info_ret: store struct snd_pcm_chmap instance if non-NULL
2483 *
2484 * Create channel-mapping control elements assigned to the given PCM stream(s).
2485 * Returns zero if succeed, or a negative error value.
2486 */
2487int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
2488 const struct snd_pcm_chmap_elem *chmap,
2489 int max_channels,
2490 unsigned long private_value,
2491 struct snd_pcm_chmap **info_ret)
2492{
2493 struct snd_pcm_chmap *info;
2494 struct snd_kcontrol_new knew = {
2495 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2496 .access = SNDRV_CTL_ELEM_ACCESS_READ |
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002497 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2498 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2499 .info = pcm_chmap_ctl_info,
2500 .get = pcm_chmap_ctl_get,
2501 .tlv.c = pcm_chmap_ctl_tlv,
2502 };
2503 int err;
2504
2505 info = kzalloc(sizeof(*info), GFP_KERNEL);
2506 if (!info)
2507 return -ENOMEM;
2508 info->pcm = pcm;
2509 info->stream = stream;
2510 info->chmap = chmap;
2511 info->max_channels = max_channels;
2512 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2513 knew.name = "Playback Channel Map";
2514 else
2515 knew.name = "Capture Channel Map";
2516 knew.device = pcm->device;
2517 knew.count = pcm->streams[stream].substream_count;
2518 knew.private_value = private_value;
2519 info->kctl = snd_ctl_new1(&knew, info);
2520 if (!info->kctl) {
2521 kfree(info);
2522 return -ENOMEM;
2523 }
2524 info->kctl->private_free = pcm_chmap_ctl_private_free;
2525 err = snd_ctl_add(pcm->card, info->kctl);
2526 if (err < 0)
2527 return err;
2528 pcm->streams[stream].chmap_kctl = info->kctl;
2529 if (info_ret)
2530 *info_ret = info;
2531 return 0;
2532}
2533EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);