blob: b03a638b420c18243776c45fda5884b422392c46 [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 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mm.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040023#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/file.h>
25#include <linux/slab.h>
26#include <linux/time.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020027#include <linux/pm_qos.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070028#include <linux/aio.h>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010029#include <linux/io.h>
Takashi Iwai657b1982009-11-26 12:40:21 +010030#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include <sound/control.h>
33#include <sound/info.h>
34#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36#include <sound/timer.h>
37#include <sound/minors.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
40 * Compatibility
41 */
42
Takashi Iwai877211f2005-11-17 13:59:38 +010043struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 unsigned int flags;
45 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
46 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010047 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
49 unsigned int rmask;
50 unsigned int cmask;
51 unsigned int info;
52 unsigned int msbits;
53 unsigned int rate_num;
54 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010055 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 unsigned char reserved[64];
57};
58
Takashi Iwai59d48582005-12-01 10:51:58 +010059#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010060#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
61#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Takashi Iwai877211f2005-11-17 13:59:38 +010063static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
64 struct snd_pcm_hw_params_old __user * _oparams);
65static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
66 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010067#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010068static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/*
71 *
72 */
73
Takashi Iwai7af142f2014-09-01 11:19:37 +020074static DEFINE_RWLOCK(snd_pcm_link_rwlock);
75static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Takashi Iwai30b771c2014-10-30 15:02:50 +010077/**
78 * snd_pcm_stream_lock - Lock the PCM stream
79 * @substream: PCM substream
80 *
81 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
82 * flag of the given substream. This also takes the global link rw lock
83 * (or rw sem), too, for avoiding the race with linked streams.
84 */
Takashi Iwai7af142f2014-09-01 11:19:37 +020085void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
86{
87 if (substream->pcm->nonatomic) {
88 down_read(&snd_pcm_link_rwsem);
89 mutex_lock(&substream->self_group.mutex);
90 } else {
91 read_lock(&snd_pcm_link_rwlock);
92 spin_lock(&substream->self_group.lock);
93 }
94}
95EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
96
Takashi Iwai30b771c2014-10-30 15:02:50 +010097/**
98 * snd_pcm_stream_lock - Unlock the PCM stream
99 * @substream: PCM substream
100 *
101 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
102 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200103void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
104{
105 if (substream->pcm->nonatomic) {
106 mutex_unlock(&substream->self_group.mutex);
107 up_read(&snd_pcm_link_rwsem);
108 } else {
109 spin_unlock(&substream->self_group.lock);
110 read_unlock(&snd_pcm_link_rwlock);
111 }
112}
113EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
114
Takashi Iwai30b771c2014-10-30 15:02:50 +0100115/**
116 * snd_pcm_stream_lock_irq - Lock the PCM stream
117 * @substream: PCM substream
118 *
119 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
120 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
121 * as snd_pcm_stream_lock().
122 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200123void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
124{
125 if (!substream->pcm->nonatomic)
126 local_irq_disable();
127 snd_pcm_stream_lock(substream);
128}
129EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
130
Takashi Iwai30b771c2014-10-30 15:02:50 +0100131/**
132 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
133 * @substream: PCM substream
134 *
135 * This is a counter-part of snd_pcm_stream_lock_irq().
136 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200137void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
138{
139 snd_pcm_stream_unlock(substream);
140 if (!substream->pcm->nonatomic)
141 local_irq_enable();
142}
143EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
144
145unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
146{
147 unsigned long flags = 0;
148 if (!substream->pcm->nonatomic)
149 local_irq_save(flags);
150 snd_pcm_stream_lock(substream);
151 return flags;
152}
153EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
154
Takashi Iwai30b771c2014-10-30 15:02:50 +0100155/**
156 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
157 * @substream: PCM substream
158 * @flags: irq flags
159 *
160 * This is a counter-part of snd_pcm_stream_lock_irqsave().
161 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200162void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
163 unsigned long flags)
164{
165 snd_pcm_stream_unlock(substream);
166 if (!substream->pcm->nonatomic)
167 local_irq_restore(flags);
168}
169EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171static inline mm_segment_t snd_enter_user(void)
172{
173 mm_segment_t fs = get_fs();
174 set_fs(get_ds());
175 return fs;
176}
177
178static inline void snd_leave_user(mm_segment_t fs)
179{
180 set_fs(fs);
181}
182
183
184
Takashi Iwai877211f2005-11-17 13:59:38 +0100185int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Takashi Iwai877211f2005-11-17 13:59:38 +0100187 struct snd_pcm_runtime *runtime;
188 struct snd_pcm *pcm = substream->pcm;
189 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 memset(info, 0, sizeof(*info));
192 info->card = pcm->card->number;
193 info->device = pcm->device;
194 info->stream = substream->stream;
195 info->subdevice = substream->number;
196 strlcpy(info->id, pcm->id, sizeof(info->id));
197 strlcpy(info->name, pcm->name, sizeof(info->name));
198 info->dev_class = pcm->dev_class;
199 info->dev_subclass = pcm->dev_subclass;
200 info->subdevices_count = pstr->substream_count;
201 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
202 strlcpy(info->subname, substream->name, sizeof(info->subname));
203 runtime = substream->runtime;
204 /* AB: FIXME!!! This is definitely nonsense */
205 if (runtime) {
206 info->sync = runtime->sync;
207 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
208 }
209 return 0;
210}
211
Takashi Iwai877211f2005-11-17 13:59:38 +0100212int snd_pcm_info_user(struct snd_pcm_substream *substream,
213 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Takashi Iwai877211f2005-11-17 13:59:38 +0100215 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 int err;
217
218 info = kmalloc(sizeof(*info), GFP_KERNEL);
219 if (! info)
220 return -ENOMEM;
221 err = snd_pcm_info(substream, info);
222 if (err >= 0) {
223 if (copy_to_user(_info, info, sizeof(*info)))
224 err = -EFAULT;
225 }
226 kfree(info);
227 return err;
228}
229
Takashi Iwai63825f32014-10-22 12:04:46 +0200230static bool hw_support_mmap(struct snd_pcm_substream *substream)
231{
232 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
233 return false;
234 /* check architectures that return -EINVAL from dma_mmap_coherent() */
235 /* FIXME: this should be some global flag */
236#if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\
237 defined(CONFIG_PARISC) || defined(CONFIG_XTENSA)
238 if (!substream->ops->mmap &&
239 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
240 return false;
241#endif
242 return true;
243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#undef RULES_DEBUG
246
247#ifdef RULES_DEBUG
248#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
Joe Perches47023ec2010-09-13 21:24:02 -0700249static const char * const snd_pcm_hw_param_names[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 HW_PARAM(ACCESS),
251 HW_PARAM(FORMAT),
252 HW_PARAM(SUBFORMAT),
253 HW_PARAM(SAMPLE_BITS),
254 HW_PARAM(FRAME_BITS),
255 HW_PARAM(CHANNELS),
256 HW_PARAM(RATE),
257 HW_PARAM(PERIOD_TIME),
258 HW_PARAM(PERIOD_SIZE),
259 HW_PARAM(PERIOD_BYTES),
260 HW_PARAM(PERIODS),
261 HW_PARAM(BUFFER_TIME),
262 HW_PARAM(BUFFER_SIZE),
263 HW_PARAM(BUFFER_BYTES),
264 HW_PARAM(TICK_TIME),
265};
266#endif
267
Takashi Iwai877211f2005-11-17 13:59:38 +0100268int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
269 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100272 struct snd_pcm_hardware *hw;
273 struct snd_interval *i = NULL;
274 struct snd_mask *m = NULL;
275 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 unsigned int rstamps[constrs->rules_num];
277 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
278 unsigned int stamp = 2;
279 int changed, again;
280
281 params->info = 0;
282 params->fifo_size = 0;
283 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
284 params->msbits = 0;
285 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
286 params->rate_num = 0;
287 params->rate_den = 0;
288 }
289
290 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
291 m = hw_param_mask(params, k);
292 if (snd_mask_empty(m))
293 return -EINVAL;
294 if (!(params->rmask & (1 << k)))
295 continue;
296#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100297 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
298 pr_cont("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299#endif
300 changed = snd_mask_refine(m, constrs_mask(constrs, k));
301#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100302 pr_cont("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303#endif
304 if (changed)
305 params->cmask |= 1 << k;
306 if (changed < 0)
307 return changed;
308 }
309
310 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
311 i = hw_param_interval(params, k);
312 if (snd_interval_empty(i))
313 return -EINVAL;
314 if (!(params->rmask & (1 << k)))
315 continue;
316#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100317 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100319 pr_cont("empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100321 pr_cont("%c%u %u%c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 i->openmin ? '(' : '[', i->min,
323 i->max, i->openmax ? ')' : ']');
Takashi Iwai09e56df2014-02-04 18:19:48 +0100324 pr_cont(" -> ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325#endif
326 changed = snd_interval_refine(i, constrs_interval(constrs, k));
327#ifdef RULES_DEBUG
328 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100329 pr_cont("empty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100331 pr_cont("%c%u %u%c\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 i->openmin ? '(' : '[', i->min,
333 i->max, i->openmax ? ')' : ']');
334#endif
335 if (changed)
336 params->cmask |= 1 << k;
337 if (changed < 0)
338 return changed;
339 }
340
341 for (k = 0; k < constrs->rules_num; k++)
342 rstamps[k] = 0;
343 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
344 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
345 do {
346 again = 0;
347 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100348 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 unsigned int d;
350 int doit = 0;
351 if (r->cond && !(r->cond & params->flags))
352 continue;
353 for (d = 0; r->deps[d] >= 0; d++) {
354 if (vstamps[r->deps[d]] > rstamps[k]) {
355 doit = 1;
356 break;
357 }
358 }
359 if (!doit)
360 continue;
361#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100362 pr_debug("Rule %d [%p]: ", k, r->func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (r->var >= 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100364 pr_cont("%s = ", snd_pcm_hw_param_names[r->var]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (hw_is_mask(r->var)) {
366 m = hw_param_mask(params, r->var);
Takashi Iwai09e56df2014-02-04 18:19:48 +0100367 pr_cont("%x", *m->bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 } else {
369 i = hw_param_interval(params, r->var);
370 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100371 pr_cont("empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100373 pr_cont("%c%u %u%c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 i->openmin ? '(' : '[', i->min,
375 i->max, i->openmax ? ')' : ']');
376 }
377 }
378#endif
379 changed = r->func(params, r);
380#ifdef RULES_DEBUG
381 if (r->var >= 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100382 pr_cont(" -> ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (hw_is_mask(r->var))
Takashi Iwai09e56df2014-02-04 18:19:48 +0100384 pr_cont("%x", *m->bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 else {
386 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100387 pr_cont("empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100389 pr_cont("%c%u %u%c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 i->openmin ? '(' : '[', i->min,
391 i->max, i->openmax ? ')' : ']');
392 }
393 }
Takashi Iwai09e56df2014-02-04 18:19:48 +0100394 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395#endif
396 rstamps[k] = stamp;
397 if (changed && r->var >= 0) {
398 params->cmask |= (1 << r->var);
399 vstamps[r->var] = stamp;
400 again = 1;
401 }
402 if (changed < 0)
403 return changed;
404 stamp++;
405 }
406 } while (again);
407 if (!params->msbits) {
408 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
409 if (snd_interval_single(i))
410 params->msbits = snd_interval_value(i);
411 }
412
413 if (!params->rate_den) {
414 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
415 if (snd_interval_single(i)) {
416 params->rate_num = snd_interval_value(i);
417 params->rate_den = 1;
418 }
419 }
420
421 hw = &substream->runtime->hw;
Takashi Iwai63825f32014-10-22 12:04:46 +0200422 if (!params->info) {
Libin Yang48d88292014-12-31 22:09:54 +0800423 params->info = hw->info & ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
424 SNDRV_PCM_INFO_DRAIN_TRIGGER);
Takashi Iwai63825f32014-10-22 12:04:46 +0200425 if (!hw_support_mmap(substream))
426 params->info &= ~(SNDRV_PCM_INFO_MMAP |
427 SNDRV_PCM_INFO_MMAP_VALID);
428 }
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200429 if (!params->fifo_size) {
Jaroslav Kysela3be522a2010-02-16 11:55:43 +0100430 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
431 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
432 if (snd_mask_min(m) == snd_mask_max(m) &&
433 snd_interval_min(i) == snd_interval_max(i)) {
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200434 changed = substream->ops->ioctl(substream,
435 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
Mariusz Kozlowski8bd9bca2009-06-21 20:26:59 +0200436 if (changed < 0)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200437 return changed;
438 }
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 params->rmask = 0;
441 return 0;
442}
443
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200444EXPORT_SYMBOL(snd_pcm_hw_refine);
445
Takashi Iwai877211f2005-11-17 13:59:38 +0100446static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
447 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Takashi Iwai877211f2005-11-17 13:59:38 +0100449 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 int err;
451
Li Zefanef44a1e2009-04-10 09:43:08 +0800452 params = memdup_user(_params, sizeof(*params));
453 if (IS_ERR(params))
454 return PTR_ERR(params);
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 err = snd_pcm_hw_refine(substream, params);
457 if (copy_to_user(_params, params, sizeof(*params))) {
458 if (!err)
459 err = -EFAULT;
460 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 kfree(params);
463 return err;
464}
465
Takashi Iwai9442e692006-09-30 23:27:19 -0700466static int period_to_usecs(struct snd_pcm_runtime *runtime)
467{
468 int usecs;
469
470 if (! runtime->rate)
471 return -1; /* invalid */
472
473 /* take 75% of period time as the deadline */
474 usecs = (750000 / runtime->rate) * runtime->period_size;
475 usecs += ((750000 % runtime->rate) * runtime->period_size) /
476 runtime->rate;
477
478 return usecs;
479}
480
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200481static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
482{
483 snd_pcm_stream_lock_irq(substream);
484 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
485 substream->runtime->status->state = state;
486 snd_pcm_stream_unlock_irq(substream);
487}
488
Takashi Iwai877211f2005-11-17 13:59:38 +0100489static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
490 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Takashi Iwai877211f2005-11-17 13:59:38 +0100492 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700493 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 unsigned int bits;
495 snd_pcm_uframes_t frames;
496
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200497 if (PCM_RUNTIME_CHECK(substream))
498 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 snd_pcm_stream_lock_irq(substream);
501 switch (runtime->status->state) {
502 case SNDRV_PCM_STATE_OPEN:
503 case SNDRV_PCM_STATE_SETUP:
504 case SNDRV_PCM_STATE_PREPARED:
505 break;
506 default:
507 snd_pcm_stream_unlock_irq(substream);
508 return -EBADFD;
509 }
510 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100511#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (!substream->oss.oss)
513#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200514 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return -EBADFD;
516
517 params->rmask = ~0U;
518 err = snd_pcm_hw_refine(substream, params);
519 if (err < 0)
520 goto _error;
521
522 err = snd_pcm_hw_params_choose(substream, params);
523 if (err < 0)
524 goto _error;
525
526 if (substream->ops->hw_params != NULL) {
527 err = substream->ops->hw_params(substream, params);
528 if (err < 0)
529 goto _error;
530 }
531
532 runtime->access = params_access(params);
533 runtime->format = params_format(params);
534 runtime->subformat = params_subformat(params);
535 runtime->channels = params_channels(params);
536 runtime->rate = params_rate(params);
537 runtime->period_size = params_period_size(params);
538 runtime->periods = params_periods(params);
539 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 runtime->info = params->info;
541 runtime->rate_num = params->rate_num;
542 runtime->rate_den = params->rate_den;
Clemens Ladischab69a492010-11-15 10:46:23 +0100543 runtime->no_period_wakeup =
544 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
545 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 bits = snd_pcm_format_physical_width(runtime->format);
548 runtime->sample_bits = bits;
549 bits *= runtime->channels;
550 runtime->frame_bits = bits;
551 frames = 1;
552 while (bits % 8 != 0) {
553 bits *= 2;
554 frames *= 2;
555 }
556 runtime->byte_align = bits / 8;
557 runtime->min_align = frames;
558
559 /* Default sw params */
560 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
561 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 runtime->start_threshold = 1;
564 runtime->stop_threshold = runtime->buffer_size;
565 runtime->silence_threshold = 0;
566 runtime->silence_size = 0;
Clemens Ladischead40462010-05-21 09:15:59 +0200567 runtime->boundary = runtime->buffer_size;
568 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
569 runtime->boundary *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 snd_pcm_timer_resolution_change(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200572 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai9442e692006-09-30 23:27:19 -0700573
James Bottomley82f68252010-07-05 22:53:06 +0200574 if (pm_qos_request_active(&substream->latency_pm_qos_req))
575 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai9442e692006-09-30 23:27:19 -0700576 if ((usecs = period_to_usecs(runtime)) >= 0)
James Bottomley82f68252010-07-05 22:53:06 +0200577 pm_qos_add_request(&substream->latency_pm_qos_req,
578 PM_QOS_CPU_DMA_LATENCY, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return 0;
580 _error:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300581 /* hardware might be unusable from this time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 so we force application to retry to set
583 the correct hardware parameter settings */
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200584 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (substream->ops->hw_free != NULL)
586 substream->ops->hw_free(substream);
587 return err;
588}
589
Takashi Iwai877211f2005-11-17 13:59:38 +0100590static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
591 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Takashi Iwai877211f2005-11-17 13:59:38 +0100593 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 int err;
595
Li Zefanef44a1e2009-04-10 09:43:08 +0800596 params = memdup_user(_params, sizeof(*params));
597 if (IS_ERR(params))
598 return PTR_ERR(params);
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 err = snd_pcm_hw_params(substream, params);
601 if (copy_to_user(_params, params, sizeof(*params))) {
602 if (!err)
603 err = -EFAULT;
604 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 kfree(params);
607 return err;
608}
609
Takashi Iwai877211f2005-11-17 13:59:38 +0100610static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Takashi Iwai877211f2005-11-17 13:59:38 +0100612 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int result = 0;
614
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200615 if (PCM_RUNTIME_CHECK(substream))
616 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 snd_pcm_stream_lock_irq(substream);
619 switch (runtime->status->state) {
620 case SNDRV_PCM_STATE_SETUP:
621 case SNDRV_PCM_STATE_PREPARED:
622 break;
623 default:
624 snd_pcm_stream_unlock_irq(substream);
625 return -EBADFD;
626 }
627 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200628 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return -EBADFD;
630 if (substream->ops->hw_free)
631 result = substream->ops->hw_free(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200632 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
James Bottomley82f68252010-07-05 22:53:06 +0200633 pm_qos_remove_request(&substream->latency_pm_qos_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return result;
635}
636
Takashi Iwai877211f2005-11-17 13:59:38 +0100637static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
638 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Takashi Iwai877211f2005-11-17 13:59:38 +0100640 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100641 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200643 if (PCM_RUNTIME_CHECK(substream))
644 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 snd_pcm_stream_lock_irq(substream);
647 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
648 snd_pcm_stream_unlock_irq(substream);
649 return -EBADFD;
650 }
651 snd_pcm_stream_unlock_irq(substream);
652
653 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
654 return -EINVAL;
Takashi Iwai58900812014-07-16 17:45:27 +0200655 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
656 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
Takashi Iwai5646eda2014-07-10 09:50:19 +0200657 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (params->avail_min == 0)
659 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (params->silence_size >= runtime->boundary) {
661 if (params->silence_threshold != 0)
662 return -EINVAL;
663 } else {
664 if (params->silence_size > params->silence_threshold)
665 return -EINVAL;
666 if (params->silence_threshold > runtime->buffer_size)
667 return -EINVAL;
668 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100669 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 snd_pcm_stream_lock_irq(substream);
671 runtime->tstamp_mode = params->tstamp_mode;
Takashi Iwai58900812014-07-16 17:45:27 +0200672 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
673 runtime->tstamp_type = params->tstamp_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 runtime->period_step = params->period_step;
675 runtime->control->avail_min = params->avail_min;
676 runtime->start_threshold = params->start_threshold;
677 runtime->stop_threshold = params->stop_threshold;
678 runtime->silence_threshold = params->silence_threshold;
679 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 params->boundary = runtime->boundary;
681 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
683 runtime->silence_size > 0)
684 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100685 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100688 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
Takashi Iwai877211f2005-11-17 13:59:38 +0100691static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
692 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Takashi Iwai877211f2005-11-17 13:59:38 +0100694 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 int err;
696 if (copy_from_user(&params, _params, sizeof(params)))
697 return -EFAULT;
698 err = snd_pcm_sw_params(substream, &params);
699 if (copy_to_user(_params, &params, sizeof(params)))
700 return -EFAULT;
701 return err;
702}
703
Takashi Iwai877211f2005-11-17 13:59:38 +0100704int snd_pcm_status(struct snd_pcm_substream *substream,
705 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Takashi Iwai877211f2005-11-17 13:59:38 +0100707 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 snd_pcm_stream_lock_irq(substream);
710 status->state = runtime->status->state;
711 status->suspended_state = runtime->status->suspended_state;
712 if (status->state == SNDRV_PCM_STATE_OPEN)
713 goto _end;
714 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100715 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100717 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
718 status->tstamp = runtime->status->tstamp;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500719 status->audio_tstamp =
720 runtime->status->audio_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100721 goto _tstamp_end;
722 }
Pierre-Louis Bossart0d59b812015-02-06 15:55:50 -0600723 } else {
724 /* get tstamp only in fallback mode and only if enabled */
725 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
726 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100727 }
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100728 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 status->appl_ptr = runtime->control->appl_ptr;
730 status->hw_ptr = runtime->status->hw_ptr;
731 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
732 status->avail = snd_pcm_playback_avail(runtime);
733 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200734 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200736 status->delay += runtime->delay;
737 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 status->delay = 0;
739 } else {
740 status->avail = snd_pcm_capture_avail(runtime);
741 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200742 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 else
744 status->delay = 0;
745 }
746 status->avail_max = runtime->avail_max;
747 status->overrange = runtime->overrange;
748 runtime->avail_max = 0;
749 runtime->overrange = 0;
750 _end:
751 snd_pcm_stream_unlock_irq(substream);
752 return 0;
753}
754
Takashi Iwai877211f2005-11-17 13:59:38 +0100755static int snd_pcm_status_user(struct snd_pcm_substream *substream,
756 struct snd_pcm_status __user * _status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Takashi Iwai877211f2005-11-17 13:59:38 +0100758 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 int res;
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 memset(&status, 0, sizeof(status));
762 res = snd_pcm_status(substream, &status);
763 if (res < 0)
764 return res;
765 if (copy_to_user(_status, &status, sizeof(status)))
766 return -EFAULT;
767 return 0;
768}
769
Takashi Iwai877211f2005-11-17 13:59:38 +0100770static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
771 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Takashi Iwai877211f2005-11-17 13:59:38 +0100773 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 unsigned int channel;
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 channel = info->channel;
777 runtime = substream->runtime;
778 snd_pcm_stream_lock_irq(substream);
779 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
780 snd_pcm_stream_unlock_irq(substream);
781 return -EBADFD;
782 }
783 snd_pcm_stream_unlock_irq(substream);
784 if (channel >= runtime->channels)
785 return -EINVAL;
786 memset(info, 0, sizeof(*info));
787 info->channel = channel;
788 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
789}
790
Takashi Iwai877211f2005-11-17 13:59:38 +0100791static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
792 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Takashi Iwai877211f2005-11-17 13:59:38 +0100794 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 int res;
796
797 if (copy_from_user(&info, _info, sizeof(info)))
798 return -EFAULT;
799 res = snd_pcm_channel_info(substream, &info);
800 if (res < 0)
801 return res;
802 if (copy_to_user(_info, &info, sizeof(info)))
803 return -EFAULT;
804 return 0;
805}
806
Takashi Iwai877211f2005-11-17 13:59:38 +0100807static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Takashi Iwai877211f2005-11-17 13:59:38 +0100809 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (runtime->trigger_master == NULL)
811 return;
812 if (runtime->trigger_master == substream) {
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -0600813 if (!runtime->trigger_tstamp_latched)
814 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 } else {
816 snd_pcm_trigger_tstamp(runtime->trigger_master);
817 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
818 }
819 runtime->trigger_master = NULL;
820}
821
822struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100823 int (*pre_action)(struct snd_pcm_substream *substream, int state);
824 int (*do_action)(struct snd_pcm_substream *substream, int state);
825 void (*undo_action)(struct snd_pcm_substream *substream, int state);
826 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827};
828
829/*
830 * this functions is core for handling of linked stream
831 * Note: the stream state might be changed also on failure
832 * Note2: call with calling stream lock + link lock
833 */
834static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100835 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 int state, int do_lock)
837{
Takashi Iwai877211f2005-11-17 13:59:38 +0100838 struct snd_pcm_substream *s = NULL;
839 struct snd_pcm_substream *s1;
Takashi Iwaidde1c652014-10-21 15:32:13 +0200840 int res = 0, depth = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Takashi Iwaief991b92007-02-22 12:52:53 +0100842 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200843 if (do_lock && s != substream) {
844 if (s->pcm->nonatomic)
Takashi Iwaidde1c652014-10-21 15:32:13 +0200845 mutex_lock_nested(&s->self_group.mutex, depth);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200846 else
Takashi Iwaidde1c652014-10-21 15:32:13 +0200847 spin_lock_nested(&s->self_group.lock, depth);
848 depth++;
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 res = ops->pre_action(s, state);
851 if (res < 0)
852 goto _unlock;
853 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100854 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 res = ops->do_action(s, state);
856 if (res < 0) {
857 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100858 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (s1 == s) /* failed stream */
860 break;
861 ops->undo_action(s1, state);
862 }
863 }
864 s = NULL; /* unlock all */
865 goto _unlock;
866 }
867 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100868 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 ops->post_action(s, state);
870 }
871 _unlock:
872 if (do_lock) {
873 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100874 snd_pcm_group_for_each_entry(s1, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200875 if (s1 != substream) {
Takashi Iwai811deed2014-10-13 23:14:46 +0200876 if (s1->pcm->nonatomic)
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200877 mutex_unlock(&s1->self_group.mutex);
878 else
879 spin_unlock(&s1->self_group.lock);
880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (s1 == s) /* end */
882 break;
883 }
884 }
885 return res;
886}
887
888/*
889 * Note: call with stream lock
890 */
891static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100892 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 int state)
894{
895 int res;
896
897 res = ops->pre_action(substream, state);
898 if (res < 0)
899 return res;
900 res = ops->do_action(substream, state);
901 if (res == 0)
902 ops->post_action(substream, state);
903 else if (ops->undo_action)
904 ops->undo_action(substream, state);
905 return res;
906}
907
908/*
909 * Note: call with stream lock
910 */
911static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100912 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 int state)
914{
915 int res;
916
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100917 if (!snd_pcm_stream_linked(substream))
918 return snd_pcm_action_single(ops, substream, state);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200919
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100920 if (substream->pcm->nonatomic) {
921 if (!mutex_trylock(&substream->group->mutex)) {
922 mutex_unlock(&substream->self_group.mutex);
923 mutex_lock(&substream->group->mutex);
924 mutex_lock(&substream->self_group.mutex);
925 }
926 res = snd_pcm_action_group(ops, substream, state, 1);
927 mutex_unlock(&substream->group->mutex);
928 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (!spin_trylock(&substream->group->lock)) {
930 spin_unlock(&substream->self_group.lock);
931 spin_lock(&substream->group->lock);
932 spin_lock(&substream->self_group.lock);
933 }
934 res = snd_pcm_action_group(ops, substream, state, 1);
935 spin_unlock(&substream->group->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937 return res;
938}
939
940/*
941 * Note: don't use any locks before
942 */
943static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100944 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 int state)
946{
947 int res;
948
Takashi Iwaie3a4bd52014-10-31 14:45:04 +0100949 snd_pcm_stream_lock_irq(substream);
950 res = snd_pcm_action(ops, substream, state);
951 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return res;
953}
954
955/*
956 */
957static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100958 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 int state)
960{
961 int res;
962
963 down_read(&snd_pcm_link_rwsem);
964 if (snd_pcm_stream_linked(substream))
965 res = snd_pcm_action_group(ops, substream, state, 0);
966 else
967 res = snd_pcm_action_single(ops, substream, state);
968 up_read(&snd_pcm_link_rwsem);
969 return res;
970}
971
972/*
973 * start callbacks
974 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100975static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Takashi Iwai877211f2005-11-17 13:59:38 +0100977 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
979 return -EBADFD;
980 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
981 !snd_pcm_playback_data(substream))
982 return -EPIPE;
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -0600983 runtime->trigger_tstamp_latched = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 runtime->trigger_master = substream;
985 return 0;
986}
987
Takashi Iwai877211f2005-11-17 13:59:38 +0100988static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 if (substream->runtime->trigger_master != substream)
991 return 0;
992 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
993}
994
Takashi Iwai877211f2005-11-17 13:59:38 +0100995static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 if (substream->runtime->trigger_master == substream)
998 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
999}
1000
Takashi Iwai877211f2005-11-17 13:59:38 +01001001static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Takashi Iwai877211f2005-11-17 13:59:38 +01001003 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001005 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +02001006 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1007 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 runtime->status->state = state;
1009 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1010 runtime->silence_size > 0)
1011 snd_pcm_playback_silence(substream, ULONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001013 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
1014 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017static struct action_ops snd_pcm_action_start = {
1018 .pre_action = snd_pcm_pre_start,
1019 .do_action = snd_pcm_do_start,
1020 .undo_action = snd_pcm_undo_start,
1021 .post_action = snd_pcm_post_start
1022};
1023
1024/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001025 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001026 * @substream: the PCM substream instance
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001027 *
1028 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001030int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Takashi Iwai877211f2005-11-17 13:59:38 +01001032 return snd_pcm_action(&snd_pcm_action_start, substream,
1033 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
1036/*
1037 * stop callbacks
1038 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001039static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Takashi Iwai877211f2005-11-17 13:59:38 +01001041 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1043 return -EBADFD;
1044 runtime->trigger_master = substream;
1045 return 0;
1046}
1047
Takashi Iwai877211f2005-11-17 13:59:38 +01001048static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
1050 if (substream->runtime->trigger_master == substream &&
1051 snd_pcm_running(substream))
1052 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1053 return 0; /* unconditonally stop all substreams */
1054}
1055
Takashi Iwai877211f2005-11-17 13:59:38 +01001056static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Takashi Iwai877211f2005-11-17 13:59:38 +01001058 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (runtime->status->state != state) {
1060 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001061 runtime->status->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001063 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
1064 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001067 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
1070static struct action_ops snd_pcm_action_stop = {
1071 .pre_action = snd_pcm_pre_stop,
1072 .do_action = snd_pcm_do_stop,
1073 .post_action = snd_pcm_post_stop
1074};
1075
1076/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001077 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +02001078 * @substream: the PCM substream instance
1079 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001081 * The state of each stream is then changed to the given state unconditionally.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001082 *
Masanari Iida0a114582014-02-09 00:47:36 +09001083 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 */
Clemens Ladischfea952e2011-02-14 11:00:47 +01001085int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
1087 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1088}
1089
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001090EXPORT_SYMBOL(snd_pcm_stop);
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001093 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +02001094 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001096 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 * Unlike snd_pcm_stop(), this affects only the given stream.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001098 *
1099 * Return: Zero if succesful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001101int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Takashi Iwai877211f2005-11-17 13:59:38 +01001103 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1104 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
Takashi Iwai1fb85102014-11-07 17:08:28 +01001107/**
1108 * snd_pcm_stop_xrun - stop the running streams as XRUN
1109 * @substream: the PCM substream instance
Takashi Iwai1fb85102014-11-07 17:08:28 +01001110 *
1111 * This stops the given running substream (and all linked substreams) as XRUN.
1112 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1113 *
1114 * Return: Zero if successful, or a negative error code.
1115 */
1116int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1117{
1118 unsigned long flags;
1119 int ret = 0;
1120
1121 snd_pcm_stream_lock_irqsave(substream, flags);
1122 if (snd_pcm_running(substream))
1123 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1124 snd_pcm_stream_unlock_irqrestore(substream, flags);
1125 return ret;
1126}
1127EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129/*
1130 * pause callbacks
1131 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001132static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Takashi Iwai877211f2005-11-17 13:59:38 +01001134 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1136 return -ENOSYS;
1137 if (push) {
1138 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1139 return -EBADFD;
1140 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1141 return -EBADFD;
1142 runtime->trigger_master = substream;
1143 return 0;
1144}
1145
Takashi Iwai877211f2005-11-17 13:59:38 +01001146static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
1148 if (substream->runtime->trigger_master != substream)
1149 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +02001150 /* some drivers might use hw_ptr to recover from the pause -
1151 update the hw_ptr now */
1152 if (push)
1153 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001154 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001155 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001156 * delta, effectively to skip the check once.
1157 */
1158 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return substream->ops->trigger(substream,
1160 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1161 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1162}
1163
Takashi Iwai877211f2005-11-17 13:59:38 +01001164static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 if (substream->runtime->trigger_master == substream)
1167 substream->ops->trigger(substream,
1168 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1169 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1170}
1171
Takashi Iwai877211f2005-11-17 13:59:38 +01001172static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Takashi Iwai877211f2005-11-17 13:59:38 +01001174 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 snd_pcm_trigger_tstamp(substream);
1176 if (push) {
1177 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1178 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001179 snd_timer_notify(substream->timer,
1180 SNDRV_TIMER_EVENT_MPAUSE,
1181 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001183 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 } else {
1185 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001187 snd_timer_notify(substream->timer,
1188 SNDRV_TIMER_EVENT_MCONTINUE,
1189 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
1191}
1192
1193static struct action_ops snd_pcm_action_pause = {
1194 .pre_action = snd_pcm_pre_pause,
1195 .do_action = snd_pcm_do_pause,
1196 .undo_action = snd_pcm_undo_pause,
1197 .post_action = snd_pcm_post_pause
1198};
1199
1200/*
1201 * Push/release the pause for all linked streams.
1202 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001203static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
1205 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1206}
1207
1208#ifdef CONFIG_PM
1209/* suspend */
1210
Takashi Iwai877211f2005-11-17 13:59:38 +01001211static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Takashi Iwai877211f2005-11-17 13:59:38 +01001213 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1215 return -EBUSY;
1216 runtime->trigger_master = substream;
1217 return 0;
1218}
1219
Takashi Iwai877211f2005-11-17 13:59:38 +01001220static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Takashi Iwai877211f2005-11-17 13:59:38 +01001222 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (runtime->trigger_master != substream)
1224 return 0;
1225 if (! snd_pcm_running(substream))
1226 return 0;
1227 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1228 return 0; /* suspend unconditionally */
1229}
1230
Takashi Iwai877211f2005-11-17 13:59:38 +01001231static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Takashi Iwai877211f2005-11-17 13:59:38 +01001233 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001235 runtime->status->suspended_state = runtime->status->state;
1236 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001238 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1239 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001241 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
1244static struct action_ops snd_pcm_action_suspend = {
1245 .pre_action = snd_pcm_pre_suspend,
1246 .do_action = snd_pcm_do_suspend,
1247 .post_action = snd_pcm_post_suspend
1248};
1249
1250/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001251 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001252 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001255 *
1256 * Return: Zero if successful (or @substream is %NULL), or a negative error
1257 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001259int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 int err;
1262 unsigned long flags;
1263
Takashi Iwai603bf522005-11-17 15:59:14 +01001264 if (! substream)
1265 return 0;
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 snd_pcm_stream_lock_irqsave(substream, flags);
1268 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1269 snd_pcm_stream_unlock_irqrestore(substream, flags);
1270 return err;
1271}
1272
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001273EXPORT_SYMBOL(snd_pcm_suspend);
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001276 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001277 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001280 *
1281 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001283int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Takashi Iwai877211f2005-11-17 13:59:38 +01001285 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int stream, err = 0;
1287
Takashi Iwai603bf522005-11-17 15:59:14 +01001288 if (! pcm)
1289 return 0;
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001292 for (substream = pcm->streams[stream].substream;
1293 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 /* FIXME: the open/close code should lock this as well */
1295 if (substream->runtime == NULL)
1296 continue;
1297 err = snd_pcm_suspend(substream);
1298 if (err < 0 && err != -EBUSY)
1299 return err;
1300 }
1301 }
1302 return 0;
1303}
1304
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001305EXPORT_SYMBOL(snd_pcm_suspend_all);
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307/* resume */
1308
Takashi Iwai877211f2005-11-17 13:59:38 +01001309static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Takashi Iwai877211f2005-11-17 13:59:38 +01001311 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1313 return -ENOSYS;
1314 runtime->trigger_master = substream;
1315 return 0;
1316}
1317
Takashi Iwai877211f2005-11-17 13:59:38 +01001318static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Takashi Iwai877211f2005-11-17 13:59:38 +01001320 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (runtime->trigger_master != substream)
1322 return 0;
1323 /* DMA not running previously? */
1324 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1325 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1326 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1327 return 0;
1328 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1329}
1330
Takashi Iwai877211f2005-11-17 13:59:38 +01001331static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
1333 if (substream->runtime->trigger_master == substream &&
1334 snd_pcm_running(substream))
1335 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1336}
1337
Takashi Iwai877211f2005-11-17 13:59:38 +01001338static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Takashi Iwai877211f2005-11-17 13:59:38 +01001340 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001342 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001344 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1345 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
1348static struct action_ops snd_pcm_action_resume = {
1349 .pre_action = snd_pcm_pre_resume,
1350 .do_action = snd_pcm_do_resume,
1351 .undo_action = snd_pcm_undo_resume,
1352 .post_action = snd_pcm_post_resume
1353};
1354
Takashi Iwai877211f2005-11-17 13:59:38 +01001355static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Takashi Iwai877211f2005-11-17 13:59:38 +01001357 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 int res;
1359
1360 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001361 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1363 snd_power_unlock(card);
1364 return res;
1365}
1366
1367#else
1368
Takashi Iwai877211f2005-11-17 13:59:38 +01001369static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 return -ENOSYS;
1372}
1373
1374#endif /* CONFIG_PM */
1375
1376/*
1377 * xrun ioctl
1378 *
1379 * Change the RUNNING stream(s) to XRUN state.
1380 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001381static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Takashi Iwai877211f2005-11-17 13:59:38 +01001383 struct snd_card *card = substream->pcm->card;
1384 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 int result;
1386
1387 snd_power_lock(card);
1388 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001389 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (result < 0)
1391 goto _unlock;
1392 }
1393
1394 snd_pcm_stream_lock_irq(substream);
1395 switch (runtime->status->state) {
1396 case SNDRV_PCM_STATE_XRUN:
1397 result = 0; /* already there */
1398 break;
1399 case SNDRV_PCM_STATE_RUNNING:
1400 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1401 break;
1402 default:
1403 result = -EBADFD;
1404 }
1405 snd_pcm_stream_unlock_irq(substream);
1406 _unlock:
1407 snd_power_unlock(card);
1408 return result;
1409}
1410
1411/*
1412 * reset ioctl
1413 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001414static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Takashi Iwai877211f2005-11-17 13:59:38 +01001416 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 switch (runtime->status->state) {
1418 case SNDRV_PCM_STATE_RUNNING:
1419 case SNDRV_PCM_STATE_PREPARED:
1420 case SNDRV_PCM_STATE_PAUSED:
1421 case SNDRV_PCM_STATE_SUSPENDED:
1422 return 0;
1423 default:
1424 return -EBADFD;
1425 }
1426}
1427
Takashi Iwai877211f2005-11-17 13:59:38 +01001428static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Takashi Iwai877211f2005-11-17 13:59:38 +01001430 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1432 if (err < 0)
1433 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001435 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1436 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 runtime->silence_start = runtime->status->hw_ptr;
1438 runtime->silence_filled = 0;
1439 return 0;
1440}
1441
Takashi Iwai877211f2005-11-17 13:59:38 +01001442static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Takashi Iwai877211f2005-11-17 13:59:38 +01001444 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 runtime->control->appl_ptr = runtime->status->hw_ptr;
1446 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1447 runtime->silence_size > 0)
1448 snd_pcm_playback_silence(substream, ULONG_MAX);
1449}
1450
1451static struct action_ops snd_pcm_action_reset = {
1452 .pre_action = snd_pcm_pre_reset,
1453 .do_action = snd_pcm_do_reset,
1454 .post_action = snd_pcm_post_reset
1455};
1456
Takashi Iwai877211f2005-11-17 13:59:38 +01001457static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1460}
1461
1462/*
1463 * prepare ioctl
1464 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001465/* we use the second argument for updating f_flags */
1466static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1467 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Takashi Iwai877211f2005-11-17 13:59:38 +01001469 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001470 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1471 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return -EBADFD;
1473 if (snd_pcm_running(substream))
1474 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001475 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return 0;
1477}
1478
Takashi Iwai877211f2005-11-17 13:59:38 +01001479static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
1481 int err;
1482 err = substream->ops->prepare(substream);
1483 if (err < 0)
1484 return err;
1485 return snd_pcm_do_reset(substream, 0);
1486}
1487
Takashi Iwai877211f2005-11-17 13:59:38 +01001488static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Takashi Iwai877211f2005-11-17 13:59:38 +01001490 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 runtime->control->appl_ptr = runtime->status->hw_ptr;
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001492 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
1495static struct action_ops snd_pcm_action_prepare = {
1496 .pre_action = snd_pcm_pre_prepare,
1497 .do_action = snd_pcm_do_prepare,
1498 .post_action = snd_pcm_post_prepare
1499};
1500
1501/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001502 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001503 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001504 * @file: file to refer f_flags
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001505 *
1506 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001508static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1509 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001512 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001513 int f_flags;
1514
1515 if (file)
1516 f_flags = file->f_flags;
1517 else
1518 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001521 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001522 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1523 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 snd_power_unlock(card);
1525 return res;
1526}
1527
1528/*
1529 * drain ioctl
1530 */
1531
Takashi Iwai877211f2005-11-17 13:59:38 +01001532static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001534 struct snd_pcm_runtime *runtime = substream->runtime;
1535 switch (runtime->status->state) {
1536 case SNDRV_PCM_STATE_OPEN:
1537 case SNDRV_PCM_STATE_DISCONNECTED:
1538 case SNDRV_PCM_STATE_SUSPENDED:
1539 return -EBADFD;
1540 }
1541 runtime->trigger_master = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return 0;
1543}
1544
Takashi Iwai877211f2005-11-17 13:59:38 +01001545static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Takashi Iwai877211f2005-11-17 13:59:38 +01001547 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1549 switch (runtime->status->state) {
1550 case SNDRV_PCM_STATE_PREPARED:
1551 /* start playback stream if possible */
1552 if (! snd_pcm_playback_empty(substream)) {
1553 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1554 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1555 }
1556 break;
1557 case SNDRV_PCM_STATE_RUNNING:
1558 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1559 break;
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001560 case SNDRV_PCM_STATE_XRUN:
1561 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1562 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 default:
1564 break;
1565 }
1566 } else {
1567 /* stop running stream */
1568 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001569 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001571 snd_pcm_do_stop(substream, new_state);
1572 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574 }
Libin Yang48d88292014-12-31 22:09:54 +08001575
1576 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1577 runtime->trigger_master == substream &&
1578 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1579 return substream->ops->trigger(substream,
1580 SNDRV_PCM_TRIGGER_DRAIN);
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return 0;
1583}
1584
Takashi Iwai877211f2005-11-17 13:59:38 +01001585static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
1587}
1588
1589static struct action_ops snd_pcm_action_drain_init = {
1590 .pre_action = snd_pcm_pre_drain_init,
1591 .do_action = snd_pcm_do_drain_init,
1592 .post_action = snd_pcm_post_drain_init
1593};
1594
Takashi Iwai877211f2005-11-17 13:59:38 +01001595static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597/*
1598 * Drain the stream(s).
1599 * When the substream is linked, sync until the draining of all playback streams
1600 * is finished.
1601 * After this call, all streams are supposed to be either SETUP or DRAINING
1602 * (capture only) state.
1603 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001604static int snd_pcm_drain(struct snd_pcm_substream *substream,
1605 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
Takashi Iwai877211f2005-11-17 13:59:38 +01001607 struct snd_card *card;
1608 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001609 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001610 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001612 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 card = substream->pcm->card;
1615 runtime = substream->runtime;
1616
1617 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1618 return -EBADFD;
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 snd_power_lock(card);
1621 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001622 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001623 if (result < 0) {
1624 snd_power_unlock(card);
1625 return result;
1626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
1628
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001629 if (file) {
1630 if (file->f_flags & O_NONBLOCK)
1631 nonblock = 1;
1632 } else if (substream->f_flags & O_NONBLOCK)
1633 nonblock = 1;
1634
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001635 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001636 snd_pcm_stream_lock_irq(substream);
1637 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001638 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001639 snd_pcm_pause(substream, 0);
1640
1641 /* pre-start/stop - all running streams are changed to DRAINING state */
1642 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001643 if (result < 0)
1644 goto unlock;
1645 /* in non-blocking, we don't wait in ioctl but let caller poll */
1646 if (nonblock) {
1647 result = -EAGAIN;
1648 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 for (;;) {
1652 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001653 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (signal_pending(current)) {
1655 result = -ERESTARTSYS;
1656 break;
1657 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001658 /* find a substream to drain */
1659 to_check = NULL;
1660 snd_pcm_group_for_each_entry(s, substream) {
1661 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1662 continue;
1663 runtime = s->runtime;
1664 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1665 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001666 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001667 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001668 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001669 if (!to_check)
1670 break; /* all drained */
1671 init_waitqueue_entry(&wait, current);
1672 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001674 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 snd_power_unlock(card);
Takashi Iwaif2b36142011-05-26 08:09:38 +02001676 if (runtime->no_period_wakeup)
1677 tout = MAX_SCHEDULE_TIMEOUT;
1678 else {
1679 tout = 10;
1680 if (runtime->rate) {
1681 long t = runtime->period_size * 2 / runtime->rate;
1682 tout = max(t, tout);
1683 }
1684 tout = msecs_to_jiffies(tout * 1000);
1685 }
1686 tout = schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001688 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001690 remove_wait_queue(&to_check->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001691 if (card->shutdown) {
1692 result = -ENODEV;
1693 break;
1694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (tout == 0) {
1696 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1697 result = -ESTRPIPE;
1698 else {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001699 dev_dbg(substream->pcm->card->dev,
1700 "playback drain error (DMA or IRQ trouble?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1702 result = -EIO;
1703 }
1704 break;
1705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001707
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001708 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001709 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001710 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 return result;
1714}
1715
1716/*
1717 * drop ioctl
1718 *
1719 * Immediately put all linked substreams into SETUP state.
1720 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001721static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Takashi Iwai877211f2005-11-17 13:59:38 +01001723 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 int result = 0;
1725
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001726 if (PCM_RUNTIME_CHECK(substream))
1727 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001730 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001731 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1732 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 return -EBADFD;
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 snd_pcm_stream_lock_irq(substream);
1736 /* resume pause */
1737 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1738 snd_pcm_pause(substream, 0);
1739
1740 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1741 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1742 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 return result;
1745}
1746
1747
Al Viro0888c322013-06-05 14:09:55 -04001748static bool is_pcm_file(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Al Viro0888c322013-06-05 14:09:55 -04001750 struct inode *inode = file_inode(file);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001751 unsigned int minor;
1752
Al Viro0888c322013-06-05 14:09:55 -04001753 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1754 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 minor = iminor(inode);
Al Viro0888c322013-06-05 14:09:55 -04001756 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1757 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
1760/*
1761 * PCM link handling
1762 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001763static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
1765 int res = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001766 struct snd_pcm_file *pcm_file;
1767 struct snd_pcm_substream *substream1;
Takashi Iwai16625912012-03-13 15:55:43 +01001768 struct snd_pcm_group *group;
Al Viro0888c322013-06-05 14:09:55 -04001769 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Al Viro0888c322013-06-05 14:09:55 -04001771 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return -EBADFD;
Al Viro0888c322013-06-05 14:09:55 -04001773 if (!is_pcm_file(f.file)) {
1774 res = -EBADFD;
1775 goto _badf;
1776 }
1777 pcm_file = f.file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 substream1 = pcm_file->substream;
Takashi Iwai16625912012-03-13 15:55:43 +01001779 group = kmalloc(sizeof(*group), GFP_KERNEL);
1780 if (!group) {
1781 res = -ENOMEM;
1782 goto _nolock;
1783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 down_write(&snd_pcm_link_rwsem);
1785 write_lock_irq(&snd_pcm_link_rwlock);
1786 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001787 substream->runtime->status->state != substream1->runtime->status->state ||
1788 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 res = -EBADFD;
1790 goto _end;
1791 }
1792 if (snd_pcm_stream_linked(substream1)) {
1793 res = -EALREADY;
1794 goto _end;
1795 }
1796 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai16625912012-03-13 15:55:43 +01001797 substream->group = group;
Al Virodd6c5cd2013-06-05 14:07:08 -04001798 group = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 spin_lock_init(&substream->group->lock);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001800 mutex_init(&substream->group->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 INIT_LIST_HEAD(&substream->group->substreams);
1802 list_add_tail(&substream->link_list, &substream->group->substreams);
1803 substream->group->count = 1;
1804 }
1805 list_add_tail(&substream1->link_list, &substream->group->substreams);
1806 substream->group->count++;
1807 substream1->group = substream->group;
1808 _end:
1809 write_unlock_irq(&snd_pcm_link_rwlock);
1810 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01001811 _nolock:
Takashi Iwaia0830db2012-10-16 13:05:59 +02001812 snd_card_unref(substream1->pcm->card);
Al Virodd6c5cd2013-06-05 14:07:08 -04001813 kfree(group);
Al Viro0888c322013-06-05 14:09:55 -04001814 _badf:
1815 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return res;
1817}
1818
Takashi Iwai877211f2005-11-17 13:59:38 +01001819static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
1821 substream->group = &substream->self_group;
1822 INIT_LIST_HEAD(&substream->self_group.substreams);
1823 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1824}
1825
Takashi Iwai877211f2005-11-17 13:59:38 +01001826static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Takashi Iwaief991b92007-02-22 12:52:53 +01001828 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 int res = 0;
1830
1831 down_write(&snd_pcm_link_rwsem);
1832 write_lock_irq(&snd_pcm_link_rwlock);
1833 if (!snd_pcm_stream_linked(substream)) {
1834 res = -EALREADY;
1835 goto _end;
1836 }
1837 list_del(&substream->link_list);
1838 substream->group->count--;
1839 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001840 snd_pcm_group_for_each_entry(s, substream) {
1841 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 break;
1843 }
1844 kfree(substream->group);
1845 }
1846 relink_to_local(substream);
1847 _end:
1848 write_unlock_irq(&snd_pcm_link_rwlock);
1849 up_write(&snd_pcm_link_rwsem);
1850 return res;
1851}
1852
1853/*
1854 * hw configurator
1855 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001856static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1857 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Takashi Iwai877211f2005-11-17 13:59:38 +01001859 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1861 hw_param_interval_c(params, rule->deps[1]), &t);
1862 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1863}
1864
Takashi Iwai877211f2005-11-17 13:59:38 +01001865static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1866 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Takashi Iwai877211f2005-11-17 13:59:38 +01001868 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1870 hw_param_interval_c(params, rule->deps[1]), &t);
1871 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1872}
1873
Takashi Iwai877211f2005-11-17 13:59:38 +01001874static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1875 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Takashi Iwai877211f2005-11-17 13:59:38 +01001877 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1879 hw_param_interval_c(params, rule->deps[1]),
1880 (unsigned long) rule->private, &t);
1881 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1882}
1883
Takashi Iwai877211f2005-11-17 13:59:38 +01001884static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1885 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Takashi Iwai877211f2005-11-17 13:59:38 +01001887 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1889 (unsigned long) rule->private,
1890 hw_param_interval_c(params, rule->deps[1]), &t);
1891 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1892}
1893
Takashi Iwai877211f2005-11-17 13:59:38 +01001894static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1895 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
1897 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001898 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1899 struct snd_mask m;
1900 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 snd_mask_any(&m);
1902 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1903 int bits;
1904 if (! snd_mask_test(mask, k))
1905 continue;
1906 bits = snd_pcm_format_physical_width(k);
1907 if (bits <= 0)
1908 continue; /* ignore invalid formats */
1909 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1910 snd_mask_reset(&m, k);
1911 }
1912 return snd_mask_refine(mask, &m);
1913}
1914
Takashi Iwai877211f2005-11-17 13:59:38 +01001915static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1916 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Takashi Iwai877211f2005-11-17 13:59:38 +01001918 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 unsigned int k;
1920 t.min = UINT_MAX;
1921 t.max = 0;
1922 t.openmin = 0;
1923 t.openmax = 0;
1924 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1925 int bits;
1926 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1927 continue;
1928 bits = snd_pcm_format_physical_width(k);
1929 if (bits <= 0)
1930 continue; /* ignore invalid formats */
1931 if (t.min > (unsigned)bits)
1932 t.min = bits;
1933 if (t.max < (unsigned)bits)
1934 t.max = bits;
1935 }
1936 t.integer = 1;
1937 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1938}
1939
1940#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1941#error "Change this table"
1942#endif
1943
1944static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1945 48000, 64000, 88200, 96000, 176400, 192000 };
1946
Clemens Ladisch7653d552007-08-13 17:38:54 +02001947const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1948 .count = ARRAY_SIZE(rates),
1949 .list = rates,
1950};
1951
Takashi Iwai877211f2005-11-17 13:59:38 +01001952static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1953 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
Takashi Iwai877211f2005-11-17 13:59:38 +01001955 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001957 snd_pcm_known_rates.count,
1958 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960
Takashi Iwai877211f2005-11-17 13:59:38 +01001961static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1962 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Takashi Iwai877211f2005-11-17 13:59:38 +01001964 struct snd_interval t;
1965 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 t.min = 0;
1967 t.max = substream->buffer_bytes_max;
1968 t.openmin = 0;
1969 t.openmax = 0;
1970 t.integer = 1;
1971 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1972}
1973
Takashi Iwai877211f2005-11-17 13:59:38 +01001974int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
Takashi Iwai877211f2005-11-17 13:59:38 +01001976 struct snd_pcm_runtime *runtime = substream->runtime;
1977 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 int k, err;
1979
1980 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1981 snd_mask_any(constrs_mask(constrs, k));
1982 }
1983
1984 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1985 snd_interval_any(constrs_interval(constrs, k));
1986 }
1987
1988 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1989 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1990 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1991 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1992 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1993
1994 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1995 snd_pcm_hw_rule_format, NULL,
1996 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1997 if (err < 0)
1998 return err;
1999 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2000 snd_pcm_hw_rule_sample_bits, NULL,
2001 SNDRV_PCM_HW_PARAM_FORMAT,
2002 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2003 if (err < 0)
2004 return err;
2005 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2006 snd_pcm_hw_rule_div, NULL,
2007 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2008 if (err < 0)
2009 return err;
2010 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2011 snd_pcm_hw_rule_mul, NULL,
2012 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2013 if (err < 0)
2014 return err;
2015 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2016 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2017 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2018 if (err < 0)
2019 return err;
2020 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2021 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2022 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2023 if (err < 0)
2024 return err;
2025 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2026 snd_pcm_hw_rule_div, NULL,
2027 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2028 if (err < 0)
2029 return err;
2030 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2031 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2032 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2033 if (err < 0)
2034 return err;
2035 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2036 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2037 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2038 if (err < 0)
2039 return err;
2040 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2041 snd_pcm_hw_rule_div, NULL,
2042 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2043 if (err < 0)
2044 return err;
2045 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2046 snd_pcm_hw_rule_div, NULL,
2047 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2048 if (err < 0)
2049 return err;
2050 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2051 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2052 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2053 if (err < 0)
2054 return err;
2055 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2056 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2057 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2058 if (err < 0)
2059 return err;
2060 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2061 snd_pcm_hw_rule_mul, NULL,
2062 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2063 if (err < 0)
2064 return err;
2065 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2066 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2067 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2068 if (err < 0)
2069 return err;
2070 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2071 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2072 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2073 if (err < 0)
2074 return err;
2075 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2076 snd_pcm_hw_rule_muldivk, (void*) 8,
2077 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2078 if (err < 0)
2079 return err;
2080 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2081 snd_pcm_hw_rule_muldivk, (void*) 8,
2082 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2083 if (err < 0)
2084 return err;
2085 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2086 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2087 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2088 if (err < 0)
2089 return err;
2090 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2091 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2092 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2093 if (err < 0)
2094 return err;
2095 return 0;
2096}
2097
Takashi Iwai877211f2005-11-17 13:59:38 +01002098int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Takashi Iwai877211f2005-11-17 13:59:38 +01002100 struct snd_pcm_runtime *runtime = substream->runtime;
2101 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 int err;
2103 unsigned int mask = 0;
2104
2105 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2106 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2107 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2108 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
Takashi Iwai63825f32014-10-22 12:04:46 +02002109 if (hw_support_mmap(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2111 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2112 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2113 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2114 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2115 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2116 }
2117 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002118 if (err < 0)
2119 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002122 if (err < 0)
2123 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002126 if (err < 0)
2127 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2130 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002131 if (err < 0)
2132 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2135 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002136 if (err < 0)
2137 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
2139 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2140 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002141 if (err < 0)
2142 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2145 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002146 if (err < 0)
2147 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2150 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002151 if (err < 0)
2152 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2155 snd_pcm_hw_rule_buffer_bytes_max, substream,
2156 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2157 if (err < 0)
2158 return err;
2159
2160 /* FIXME: remove */
2161 if (runtime->dma_bytes) {
2162 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002163 if (err < 0)
Sachin Kamat6cf95152012-11-21 14:36:55 +05302164 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
2166
2167 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2168 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2169 snd_pcm_hw_rule_rate, hw,
2170 SNDRV_PCM_HW_PARAM_RATE, -1);
2171 if (err < 0)
2172 return err;
2173 }
2174
2175 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2177
2178 return 0;
2179}
2180
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002181static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002184}
2185
2186void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2187{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002188 substream->ref_count--;
2189 if (substream->ref_count > 0)
2190 return;
2191
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002192 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002193 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (substream->ops->hw_free != NULL)
2195 substream->ops->hw_free(substream);
2196 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002197 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002199 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2200 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002201 if (substream->pcm_release) {
2202 substream->pcm_release(substream);
2203 substream->pcm_release = NULL;
2204 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002205 snd_pcm_detach_substream(substream);
2206}
2207
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002208EXPORT_SYMBOL(snd_pcm_release_substream);
2209
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002210int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2211 struct file *file,
2212 struct snd_pcm_substream **rsubstream)
2213{
2214 struct snd_pcm_substream *substream;
2215 int err;
2216
2217 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2218 if (err < 0)
2219 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002220 if (substream->ref_count > 1) {
2221 *rsubstream = substream;
2222 return 0;
2223 }
2224
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002225 err = snd_pcm_hw_constraints_init(substream);
2226 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002227 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002228 goto error;
2229 }
2230
2231 if ((err = substream->ops->open(substream)) < 0)
2232 goto error;
2233
2234 substream->hw_opened = 1;
2235
2236 err = snd_pcm_hw_constraints_complete(substream);
2237 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002238 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002239 goto error;
2240 }
2241
2242 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002244
2245 error:
2246 snd_pcm_release_substream(substream);
2247 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
2249
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002250EXPORT_SYMBOL(snd_pcm_open_substream);
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002253 struct snd_pcm *pcm,
Feng Tangffd3d5c2011-10-10 10:31:48 +08002254 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Takashi Iwai877211f2005-11-17 13:59:38 +01002256 struct snd_pcm_file *pcm_file;
2257 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002258 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002260 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2261 if (err < 0)
2262 return err;
2263
Takashi Iwai548a6482006-07-31 16:51:51 +02002264 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2265 if (pcm_file == NULL) {
2266 snd_pcm_release_substream(substream);
2267 return -ENOMEM;
2268 }
2269 pcm_file->substream = substream;
2270 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002271 substream->file = pcm_file;
2272 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 file->private_data = pcm_file;
Feng Tangffd3d5c2011-10-10 10:31:48 +08002275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return 0;
2277}
2278
Clemens Ladischf87135f2005-11-20 14:06:59 +01002279static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
Takashi Iwai877211f2005-11-17 13:59:38 +01002281 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002282 int err = nonseekable_open(inode, file);
2283 if (err < 0)
2284 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002285 pcm = snd_lookup_minor_data(iminor(inode),
2286 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002287 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002288 if (pcm)
2289 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002290 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002291}
2292
2293static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2294{
2295 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002296 int err = nonseekable_open(inode, file);
2297 if (err < 0)
2298 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002299 pcm = snd_lookup_minor_data(iminor(inode),
2300 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002301 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002302 if (pcm)
2303 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002304 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002305}
2306
2307static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2308{
2309 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 wait_queue_t wait;
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (pcm == NULL) {
2313 err = -ENODEV;
2314 goto __error1;
2315 }
2316 err = snd_card_file_add(pcm->card, file);
2317 if (err < 0)
2318 goto __error1;
2319 if (!try_module_get(pcm->card->module)) {
2320 err = -EFAULT;
2321 goto __error2;
2322 }
2323 init_waitqueue_entry(&wait, current);
2324 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002325 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 while (1) {
Feng Tangffd3d5c2011-10-10 10:31:48 +08002327 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 if (err >= 0)
2329 break;
2330 if (err == -EAGAIN) {
2331 if (file->f_flags & O_NONBLOCK) {
2332 err = -EBUSY;
2333 break;
2334 }
2335 } else
2336 break;
2337 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002338 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002340 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02002341 if (pcm->card->shutdown) {
2342 err = -ENODEV;
2343 break;
2344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (signal_pending(current)) {
2346 err = -ERESTARTSYS;
2347 break;
2348 }
2349 }
2350 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002351 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 if (err < 0)
2353 goto __error;
2354 return err;
2355
2356 __error:
2357 module_put(pcm->card->module);
2358 __error2:
2359 snd_card_file_remove(pcm->card, file);
2360 __error1:
2361 return err;
2362}
2363
2364static int snd_pcm_release(struct inode *inode, struct file *file)
2365{
Takashi Iwai877211f2005-11-17 13:59:38 +01002366 struct snd_pcm *pcm;
2367 struct snd_pcm_substream *substream;
2368 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 pcm_file = file->private_data;
2371 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002372 if (snd_BUG_ON(!substream))
2373 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002375 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002376 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002377 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002378 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 wake_up(&pcm->open_wait);
2380 module_put(pcm->card->module);
2381 snd_card_file_remove(pcm->card, file);
2382 return 0;
2383}
2384
Takashi Iwai877211f2005-11-17 13:59:38 +01002385static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2386 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
Takashi Iwai877211f2005-11-17 13:59:38 +01002388 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 snd_pcm_sframes_t appl_ptr;
2390 snd_pcm_sframes_t ret;
2391 snd_pcm_sframes_t hw_avail;
2392
2393 if (frames == 0)
2394 return 0;
2395
2396 snd_pcm_stream_lock_irq(substream);
2397 switch (runtime->status->state) {
2398 case SNDRV_PCM_STATE_PREPARED:
2399 break;
2400 case SNDRV_PCM_STATE_DRAINING:
2401 case SNDRV_PCM_STATE_RUNNING:
2402 if (snd_pcm_update_hw_ptr(substream) >= 0)
2403 break;
2404 /* Fall through */
2405 case SNDRV_PCM_STATE_XRUN:
2406 ret = -EPIPE;
2407 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002408 case SNDRV_PCM_STATE_SUSPENDED:
2409 ret = -ESTRPIPE;
2410 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 default:
2412 ret = -EBADFD;
2413 goto __end;
2414 }
2415
2416 hw_avail = snd_pcm_playback_hw_avail(runtime);
2417 if (hw_avail <= 0) {
2418 ret = 0;
2419 goto __end;
2420 }
2421 if (frames > (snd_pcm_uframes_t)hw_avail)
2422 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 appl_ptr = runtime->control->appl_ptr - frames;
2424 if (appl_ptr < 0)
2425 appl_ptr += runtime->boundary;
2426 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 ret = frames;
2428 __end:
2429 snd_pcm_stream_unlock_irq(substream);
2430 return ret;
2431}
2432
Takashi Iwai877211f2005-11-17 13:59:38 +01002433static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2434 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
Takashi Iwai877211f2005-11-17 13:59:38 +01002436 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 snd_pcm_sframes_t appl_ptr;
2438 snd_pcm_sframes_t ret;
2439 snd_pcm_sframes_t hw_avail;
2440
2441 if (frames == 0)
2442 return 0;
2443
2444 snd_pcm_stream_lock_irq(substream);
2445 switch (runtime->status->state) {
2446 case SNDRV_PCM_STATE_PREPARED:
2447 case SNDRV_PCM_STATE_DRAINING:
2448 break;
2449 case SNDRV_PCM_STATE_RUNNING:
2450 if (snd_pcm_update_hw_ptr(substream) >= 0)
2451 break;
2452 /* Fall through */
2453 case SNDRV_PCM_STATE_XRUN:
2454 ret = -EPIPE;
2455 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002456 case SNDRV_PCM_STATE_SUSPENDED:
2457 ret = -ESTRPIPE;
2458 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 default:
2460 ret = -EBADFD;
2461 goto __end;
2462 }
2463
2464 hw_avail = snd_pcm_capture_hw_avail(runtime);
2465 if (hw_avail <= 0) {
2466 ret = 0;
2467 goto __end;
2468 }
2469 if (frames > (snd_pcm_uframes_t)hw_avail)
2470 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 appl_ptr = runtime->control->appl_ptr - frames;
2472 if (appl_ptr < 0)
2473 appl_ptr += runtime->boundary;
2474 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 ret = frames;
2476 __end:
2477 snd_pcm_stream_unlock_irq(substream);
2478 return ret;
2479}
2480
Takashi Iwai877211f2005-11-17 13:59:38 +01002481static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2482 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483{
Takashi Iwai877211f2005-11-17 13:59:38 +01002484 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 snd_pcm_sframes_t appl_ptr;
2486 snd_pcm_sframes_t ret;
2487 snd_pcm_sframes_t avail;
2488
2489 if (frames == 0)
2490 return 0;
2491
2492 snd_pcm_stream_lock_irq(substream);
2493 switch (runtime->status->state) {
2494 case SNDRV_PCM_STATE_PREPARED:
2495 case SNDRV_PCM_STATE_PAUSED:
2496 break;
2497 case SNDRV_PCM_STATE_DRAINING:
2498 case SNDRV_PCM_STATE_RUNNING:
2499 if (snd_pcm_update_hw_ptr(substream) >= 0)
2500 break;
2501 /* Fall through */
2502 case SNDRV_PCM_STATE_XRUN:
2503 ret = -EPIPE;
2504 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002505 case SNDRV_PCM_STATE_SUSPENDED:
2506 ret = -ESTRPIPE;
2507 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 default:
2509 ret = -EBADFD;
2510 goto __end;
2511 }
2512
2513 avail = snd_pcm_playback_avail(runtime);
2514 if (avail <= 0) {
2515 ret = 0;
2516 goto __end;
2517 }
2518 if (frames > (snd_pcm_uframes_t)avail)
2519 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 appl_ptr = runtime->control->appl_ptr + frames;
2521 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2522 appl_ptr -= runtime->boundary;
2523 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 ret = frames;
2525 __end:
2526 snd_pcm_stream_unlock_irq(substream);
2527 return ret;
2528}
2529
Takashi Iwai877211f2005-11-17 13:59:38 +01002530static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2531 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
Takashi Iwai877211f2005-11-17 13:59:38 +01002533 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 snd_pcm_sframes_t appl_ptr;
2535 snd_pcm_sframes_t ret;
2536 snd_pcm_sframes_t avail;
2537
2538 if (frames == 0)
2539 return 0;
2540
2541 snd_pcm_stream_lock_irq(substream);
2542 switch (runtime->status->state) {
2543 case SNDRV_PCM_STATE_PREPARED:
2544 case SNDRV_PCM_STATE_DRAINING:
2545 case SNDRV_PCM_STATE_PAUSED:
2546 break;
2547 case SNDRV_PCM_STATE_RUNNING:
2548 if (snd_pcm_update_hw_ptr(substream) >= 0)
2549 break;
2550 /* Fall through */
2551 case SNDRV_PCM_STATE_XRUN:
2552 ret = -EPIPE;
2553 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002554 case SNDRV_PCM_STATE_SUSPENDED:
2555 ret = -ESTRPIPE;
2556 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 default:
2558 ret = -EBADFD;
2559 goto __end;
2560 }
2561
2562 avail = snd_pcm_capture_avail(runtime);
2563 if (avail <= 0) {
2564 ret = 0;
2565 goto __end;
2566 }
2567 if (frames > (snd_pcm_uframes_t)avail)
2568 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 appl_ptr = runtime->control->appl_ptr + frames;
2570 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2571 appl_ptr -= runtime->boundary;
2572 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 ret = frames;
2574 __end:
2575 snd_pcm_stream_unlock_irq(substream);
2576 return ret;
2577}
2578
Takashi Iwai877211f2005-11-17 13:59:38 +01002579static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
Takashi Iwai877211f2005-11-17 13:59:38 +01002581 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 int err;
2583
2584 snd_pcm_stream_lock_irq(substream);
2585 switch (runtime->status->state) {
2586 case SNDRV_PCM_STATE_DRAINING:
2587 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2588 goto __badfd;
Takashi Iwai1f961532013-10-28 12:40:46 +01002589 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 case SNDRV_PCM_STATE_RUNNING:
2591 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2592 break;
2593 /* Fall through */
2594 case SNDRV_PCM_STATE_PREPARED:
2595 case SNDRV_PCM_STATE_SUSPENDED:
2596 err = 0;
2597 break;
2598 case SNDRV_PCM_STATE_XRUN:
2599 err = -EPIPE;
2600 break;
2601 default:
2602 __badfd:
2603 err = -EBADFD;
2604 break;
2605 }
2606 snd_pcm_stream_unlock_irq(substream);
2607 return err;
2608}
2609
Takashi Iwai877211f2005-11-17 13:59:38 +01002610static int snd_pcm_delay(struct snd_pcm_substream *substream,
2611 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612{
Takashi Iwai877211f2005-11-17 13:59:38 +01002613 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 int err;
2615 snd_pcm_sframes_t n = 0;
2616
2617 snd_pcm_stream_lock_irq(substream);
2618 switch (runtime->status->state) {
2619 case SNDRV_PCM_STATE_DRAINING:
2620 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2621 goto __badfd;
Takashi Iwai1f961532013-10-28 12:40:46 +01002622 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 case SNDRV_PCM_STATE_RUNNING:
2624 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2625 break;
2626 /* Fall through */
2627 case SNDRV_PCM_STATE_PREPARED:
2628 case SNDRV_PCM_STATE_SUSPENDED:
2629 err = 0;
2630 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2631 n = snd_pcm_playback_hw_avail(runtime);
2632 else
2633 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002634 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 break;
2636 case SNDRV_PCM_STATE_XRUN:
2637 err = -EPIPE;
2638 break;
2639 default:
2640 __badfd:
2641 err = -EBADFD;
2642 break;
2643 }
2644 snd_pcm_stream_unlock_irq(substream);
2645 if (!err)
2646 if (put_user(n, res))
2647 err = -EFAULT;
2648 return err;
2649}
2650
Takashi Iwai877211f2005-11-17 13:59:38 +01002651static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2652 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
Takashi Iwai877211f2005-11-17 13:59:38 +01002654 struct snd_pcm_runtime *runtime = substream->runtime;
2655 struct snd_pcm_sync_ptr sync_ptr;
2656 volatile struct snd_pcm_mmap_status *status;
2657 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 int err;
2659
2660 memset(&sync_ptr, 0, sizeof(sync_ptr));
2661 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2662 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002663 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 return -EFAULT;
2665 status = runtime->status;
2666 control = runtime->control;
2667 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2668 err = snd_pcm_hwsync(substream);
2669 if (err < 0)
2670 return err;
2671 }
2672 snd_pcm_stream_lock_irq(substream);
2673 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2674 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2675 else
2676 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2677 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2678 control->avail_min = sync_ptr.c.control.avail_min;
2679 else
2680 sync_ptr.c.control.avail_min = control->avail_min;
2681 sync_ptr.s.status.state = status->state;
2682 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2683 sync_ptr.s.status.tstamp = status->tstamp;
2684 sync_ptr.s.status.suspended_state = status->suspended_state;
2685 snd_pcm_stream_unlock_irq(substream);
2686 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2687 return -EFAULT;
2688 return 0;
2689}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002690
2691static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2692{
2693 struct snd_pcm_runtime *runtime = substream->runtime;
2694 int arg;
2695
2696 if (get_user(arg, _arg))
2697 return -EFAULT;
2698 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2699 return -EINVAL;
Takashi Iwai2408c212014-07-10 09:40:38 +02002700 runtime->tstamp_type = arg;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002701 return 0;
2702}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Takashi Iwai0df63e42006-04-28 15:13:41 +02002704static int snd_pcm_common_ioctl1(struct file *file,
2705 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 unsigned int cmd, void __user *arg)
2707{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 switch (cmd) {
2709 case SNDRV_PCM_IOCTL_PVERSION:
2710 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2711 case SNDRV_PCM_IOCTL_INFO:
2712 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002713 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2714 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002715 case SNDRV_PCM_IOCTL_TTSTAMP:
2716 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 case SNDRV_PCM_IOCTL_HW_REFINE:
2718 return snd_pcm_hw_refine_user(substream, arg);
2719 case SNDRV_PCM_IOCTL_HW_PARAMS:
2720 return snd_pcm_hw_params_user(substream, arg);
2721 case SNDRV_PCM_IOCTL_HW_FREE:
2722 return snd_pcm_hw_free(substream);
2723 case SNDRV_PCM_IOCTL_SW_PARAMS:
2724 return snd_pcm_sw_params_user(substream, arg);
2725 case SNDRV_PCM_IOCTL_STATUS:
2726 return snd_pcm_status_user(substream, arg);
2727 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2728 return snd_pcm_channel_info_user(substream, arg);
2729 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002730 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 case SNDRV_PCM_IOCTL_RESET:
2732 return snd_pcm_reset(substream);
2733 case SNDRV_PCM_IOCTL_START:
2734 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2735 case SNDRV_PCM_IOCTL_LINK:
2736 return snd_pcm_link(substream, (int)(unsigned long) arg);
2737 case SNDRV_PCM_IOCTL_UNLINK:
2738 return snd_pcm_unlink(substream);
2739 case SNDRV_PCM_IOCTL_RESUME:
2740 return snd_pcm_resume(substream);
2741 case SNDRV_PCM_IOCTL_XRUN:
2742 return snd_pcm_xrun(substream);
2743 case SNDRV_PCM_IOCTL_HWSYNC:
2744 return snd_pcm_hwsync(substream);
2745 case SNDRV_PCM_IOCTL_DELAY:
2746 return snd_pcm_delay(substream, arg);
2747 case SNDRV_PCM_IOCTL_SYNC_PTR:
2748 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002749#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2751 return snd_pcm_hw_refine_old_user(substream, arg);
2752 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2753 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002754#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002756 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 case SNDRV_PCM_IOCTL_DROP:
2758 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002759 case SNDRV_PCM_IOCTL_PAUSE:
2760 {
2761 int res;
2762 snd_pcm_stream_lock_irq(substream);
2763 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2764 snd_pcm_stream_unlock_irq(substream);
2765 return res;
2766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 }
Takashi Iwai09e56df2014-02-04 18:19:48 +01002768 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 return -ENOTTY;
2770}
2771
Takashi Iwai0df63e42006-04-28 15:13:41 +02002772static int snd_pcm_playback_ioctl1(struct file *file,
2773 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 unsigned int cmd, void __user *arg)
2775{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002776 if (snd_BUG_ON(!substream))
2777 return -ENXIO;
2778 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2779 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 switch (cmd) {
2781 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2782 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002783 struct snd_xferi xferi;
2784 struct snd_xferi __user *_xferi = arg;
2785 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 snd_pcm_sframes_t result;
2787 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2788 return -EBADFD;
2789 if (put_user(0, &_xferi->result))
2790 return -EFAULT;
2791 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2792 return -EFAULT;
2793 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2794 __put_user(result, &_xferi->result);
2795 return result < 0 ? result : 0;
2796 }
2797 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2798 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002799 struct snd_xfern xfern;
2800 struct snd_xfern __user *_xfern = arg;
2801 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 void __user **bufs;
2803 snd_pcm_sframes_t result;
2804 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2805 return -EBADFD;
2806 if (runtime->channels > 128)
2807 return -EINVAL;
2808 if (put_user(0, &_xfern->result))
2809 return -EFAULT;
2810 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2811 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002812
2813 bufs = memdup_user(xfern.bufs,
2814 sizeof(void *) * runtime->channels);
2815 if (IS_ERR(bufs))
2816 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2818 kfree(bufs);
2819 __put_user(result, &_xfern->result);
2820 return result < 0 ? result : 0;
2821 }
2822 case SNDRV_PCM_IOCTL_REWIND:
2823 {
2824 snd_pcm_uframes_t frames;
2825 snd_pcm_uframes_t __user *_frames = arg;
2826 snd_pcm_sframes_t result;
2827 if (get_user(frames, _frames))
2828 return -EFAULT;
2829 if (put_user(0, _frames))
2830 return -EFAULT;
2831 result = snd_pcm_playback_rewind(substream, frames);
2832 __put_user(result, _frames);
2833 return result < 0 ? result : 0;
2834 }
2835 case SNDRV_PCM_IOCTL_FORWARD:
2836 {
2837 snd_pcm_uframes_t frames;
2838 snd_pcm_uframes_t __user *_frames = arg;
2839 snd_pcm_sframes_t result;
2840 if (get_user(frames, _frames))
2841 return -EFAULT;
2842 if (put_user(0, _frames))
2843 return -EFAULT;
2844 result = snd_pcm_playback_forward(substream, frames);
2845 __put_user(result, _frames);
2846 return result < 0 ? result : 0;
2847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002849 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850}
2851
Takashi Iwai0df63e42006-04-28 15:13:41 +02002852static int snd_pcm_capture_ioctl1(struct file *file,
2853 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 unsigned int cmd, void __user *arg)
2855{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002856 if (snd_BUG_ON(!substream))
2857 return -ENXIO;
2858 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2859 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 switch (cmd) {
2861 case SNDRV_PCM_IOCTL_READI_FRAMES:
2862 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002863 struct snd_xferi xferi;
2864 struct snd_xferi __user *_xferi = arg;
2865 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 snd_pcm_sframes_t result;
2867 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2868 return -EBADFD;
2869 if (put_user(0, &_xferi->result))
2870 return -EFAULT;
2871 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2872 return -EFAULT;
2873 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2874 __put_user(result, &_xferi->result);
2875 return result < 0 ? result : 0;
2876 }
2877 case SNDRV_PCM_IOCTL_READN_FRAMES:
2878 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002879 struct snd_xfern xfern;
2880 struct snd_xfern __user *_xfern = arg;
2881 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 void *bufs;
2883 snd_pcm_sframes_t result;
2884 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2885 return -EBADFD;
2886 if (runtime->channels > 128)
2887 return -EINVAL;
2888 if (put_user(0, &_xfern->result))
2889 return -EFAULT;
2890 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2891 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002892
2893 bufs = memdup_user(xfern.bufs,
2894 sizeof(void *) * runtime->channels);
2895 if (IS_ERR(bufs))
2896 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2898 kfree(bufs);
2899 __put_user(result, &_xfern->result);
2900 return result < 0 ? result : 0;
2901 }
2902 case SNDRV_PCM_IOCTL_REWIND:
2903 {
2904 snd_pcm_uframes_t frames;
2905 snd_pcm_uframes_t __user *_frames = arg;
2906 snd_pcm_sframes_t result;
2907 if (get_user(frames, _frames))
2908 return -EFAULT;
2909 if (put_user(0, _frames))
2910 return -EFAULT;
2911 result = snd_pcm_capture_rewind(substream, frames);
2912 __put_user(result, _frames);
2913 return result < 0 ? result : 0;
2914 }
2915 case SNDRV_PCM_IOCTL_FORWARD:
2916 {
2917 snd_pcm_uframes_t frames;
2918 snd_pcm_uframes_t __user *_frames = arg;
2919 snd_pcm_sframes_t result;
2920 if (get_user(frames, _frames))
2921 return -EFAULT;
2922 if (put_user(0, _frames))
2923 return -EFAULT;
2924 result = snd_pcm_capture_forward(substream, frames);
2925 __put_user(result, _frames);
2926 return result < 0 ? result : 0;
2927 }
2928 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002929 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930}
2931
Takashi Iwai877211f2005-11-17 13:59:38 +01002932static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2933 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
Takashi Iwai877211f2005-11-17 13:59:38 +01002935 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
2937 pcm_file = file->private_data;
2938
2939 if (((cmd >> 8) & 0xff) != 'A')
2940 return -ENOTTY;
2941
Takashi Iwai0df63e42006-04-28 15:13:41 +02002942 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2943 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944}
2945
Takashi Iwai877211f2005-11-17 13:59:38 +01002946static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2947 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
Takashi Iwai877211f2005-11-17 13:59:38 +01002949 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
2951 pcm_file = file->private_data;
2952
2953 if (((cmd >> 8) & 0xff) != 'A')
2954 return -ENOTTY;
2955
Takashi Iwai0df63e42006-04-28 15:13:41 +02002956 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2957 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958}
2959
Takashi Iwai877211f2005-11-17 13:59:38 +01002960int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 unsigned int cmd, void *arg)
2962{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002963 mm_segment_t fs;
2964 int result;
2965
2966 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 switch (substream->stream) {
2968 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002969 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2970 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002971 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002973 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2974 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002975 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002977 result = -EINVAL;
2978 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002980 snd_leave_user(fs);
2981 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002984EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2985
Takashi Iwai877211f2005-11-17 13:59:38 +01002986static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2987 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
Takashi Iwai877211f2005-11-17 13:59:38 +01002989 struct snd_pcm_file *pcm_file;
2990 struct snd_pcm_substream *substream;
2991 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 snd_pcm_sframes_t result;
2993
2994 pcm_file = file->private_data;
2995 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002996 if (PCM_RUNTIME_CHECK(substream))
2997 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 runtime = substream->runtime;
2999 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3000 return -EBADFD;
3001 if (!frame_aligned(runtime, count))
3002 return -EINVAL;
3003 count = bytes_to_frames(runtime, count);
3004 result = snd_pcm_lib_read(substream, buf, count);
3005 if (result > 0)
3006 result = frames_to_bytes(runtime, result);
3007 return result;
3008}
3009
Takashi Iwai877211f2005-11-17 13:59:38 +01003010static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3011 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
Takashi Iwai877211f2005-11-17 13:59:38 +01003013 struct snd_pcm_file *pcm_file;
3014 struct snd_pcm_substream *substream;
3015 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 snd_pcm_sframes_t result;
3017
3018 pcm_file = file->private_data;
3019 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003020 if (PCM_RUNTIME_CHECK(substream))
3021 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003023 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3024 return -EBADFD;
3025 if (!frame_aligned(runtime, count))
3026 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 count = bytes_to_frames(runtime, count);
3028 result = snd_pcm_lib_write(substream, buf, count);
3029 if (result > 0)
3030 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 return result;
3032}
3033
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003034static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
3035 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
3037{
Takashi Iwai877211f2005-11-17 13:59:38 +01003038 struct snd_pcm_file *pcm_file;
3039 struct snd_pcm_substream *substream;
3040 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 snd_pcm_sframes_t result;
3042 unsigned long i;
3043 void __user **bufs;
3044 snd_pcm_uframes_t frames;
3045
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003046 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003048 if (PCM_RUNTIME_CHECK(substream))
3049 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 runtime = substream->runtime;
3051 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3052 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003053 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003055 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003057 frames = bytes_to_samples(runtime, iov->iov_len);
3058 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 if (bufs == NULL)
3060 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003061 for (i = 0; i < nr_segs; ++i)
3062 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 result = snd_pcm_lib_readv(substream, bufs, frames);
3064 if (result > 0)
3065 result = frames_to_bytes(runtime, result);
3066 kfree(bufs);
3067 return result;
3068}
3069
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003070static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
3071 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
Takashi Iwai877211f2005-11-17 13:59:38 +01003073 struct snd_pcm_file *pcm_file;
3074 struct snd_pcm_substream *substream;
3075 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 snd_pcm_sframes_t result;
3077 unsigned long i;
3078 void __user **bufs;
3079 snd_pcm_uframes_t frames;
3080
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003081 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003083 if (PCM_RUNTIME_CHECK(substream))
3084 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003086 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3087 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003088 if (nr_segs > 128 || nr_segs != runtime->channels ||
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003089 !frame_aligned(runtime, iov->iov_len))
3090 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003091 frames = bytes_to_samples(runtime, iov->iov_len);
3092 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 if (bufs == NULL)
3094 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003095 for (i = 0; i < nr_segs; ++i)
3096 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 result = snd_pcm_lib_writev(substream, bufs, frames);
3098 if (result > 0)
3099 result = frames_to_bytes(runtime, result);
3100 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 return result;
3102}
3103
3104static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3105{
Takashi Iwai877211f2005-11-17 13:59:38 +01003106 struct snd_pcm_file *pcm_file;
3107 struct snd_pcm_substream *substream;
3108 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 unsigned int mask;
3110 snd_pcm_uframes_t avail;
3111
3112 pcm_file = file->private_data;
3113
3114 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003115 if (PCM_RUNTIME_CHECK(substream))
3116 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 runtime = substream->runtime;
3118
3119 poll_wait(file, &runtime->sleep, wait);
3120
3121 snd_pcm_stream_lock_irq(substream);
3122 avail = snd_pcm_playback_avail(runtime);
3123 switch (runtime->status->state) {
3124 case SNDRV_PCM_STATE_RUNNING:
3125 case SNDRV_PCM_STATE_PREPARED:
3126 case SNDRV_PCM_STATE_PAUSED:
3127 if (avail >= runtime->control->avail_min) {
3128 mask = POLLOUT | POLLWRNORM;
3129 break;
3130 }
3131 /* Fall through */
3132 case SNDRV_PCM_STATE_DRAINING:
3133 mask = 0;
3134 break;
3135 default:
3136 mask = POLLOUT | POLLWRNORM | POLLERR;
3137 break;
3138 }
3139 snd_pcm_stream_unlock_irq(substream);
3140 return mask;
3141}
3142
3143static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3144{
Takashi Iwai877211f2005-11-17 13:59:38 +01003145 struct snd_pcm_file *pcm_file;
3146 struct snd_pcm_substream *substream;
3147 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 unsigned int mask;
3149 snd_pcm_uframes_t avail;
3150
3151 pcm_file = file->private_data;
3152
3153 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003154 if (PCM_RUNTIME_CHECK(substream))
3155 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 runtime = substream->runtime;
3157
3158 poll_wait(file, &runtime->sleep, wait);
3159
3160 snd_pcm_stream_lock_irq(substream);
3161 avail = snd_pcm_capture_avail(runtime);
3162 switch (runtime->status->state) {
3163 case SNDRV_PCM_STATE_RUNNING:
3164 case SNDRV_PCM_STATE_PREPARED:
3165 case SNDRV_PCM_STATE_PAUSED:
3166 if (avail >= runtime->control->avail_min) {
3167 mask = POLLIN | POLLRDNORM;
3168 break;
3169 }
3170 mask = 0;
3171 break;
3172 case SNDRV_PCM_STATE_DRAINING:
3173 if (avail > 0) {
3174 mask = POLLIN | POLLRDNORM;
3175 break;
3176 }
3177 /* Fall through */
3178 default:
3179 mask = POLLIN | POLLRDNORM | POLLERR;
3180 break;
3181 }
3182 snd_pcm_stream_unlock_irq(substream);
3183 return mask;
3184}
3185
3186/*
3187 * mmap support
3188 */
3189
3190/*
3191 * Only on coherent architectures, we can mmap the status and the control records
3192 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3193 */
3194#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3195/*
3196 * mmap status record
3197 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003198static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3199 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200{
Takashi Iwai877211f2005-11-17 13:59:38 +01003201 struct snd_pcm_substream *substream = area->vm_private_data;
3202 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
3204 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003205 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003207 vmf->page = virt_to_page(runtime->status);
3208 get_page(vmf->page);
3209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210}
3211
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003212static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003214 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215};
3216
Takashi Iwai877211f2005-11-17 13:59:38 +01003217static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 struct vm_area_struct *area)
3219{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 long size;
3221 if (!(area->vm_flags & VM_READ))
3222 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003224 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return -EINVAL;
3226 area->vm_ops = &snd_pcm_vm_ops_status;
3227 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003228 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 return 0;
3230}
3231
3232/*
3233 * mmap control record
3234 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003235static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3236 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237{
Takashi Iwai877211f2005-11-17 13:59:38 +01003238 struct snd_pcm_substream *substream = area->vm_private_data;
3239 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
3241 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003242 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003244 vmf->page = virt_to_page(runtime->control);
3245 get_page(vmf->page);
3246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247}
3248
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003249static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003251 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252};
3253
Takashi Iwai877211f2005-11-17 13:59:38 +01003254static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 struct vm_area_struct *area)
3256{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 long size;
3258 if (!(area->vm_flags & VM_READ))
3259 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003261 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 return -EINVAL;
3263 area->vm_ops = &snd_pcm_vm_ops_control;
3264 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003265 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 return 0;
3267}
3268#else /* ! coherent mmap */
3269/*
3270 * don't support mmap for status and control records.
3271 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003272static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 struct vm_area_struct *area)
3274{
3275 return -ENXIO;
3276}
Takashi Iwai877211f2005-11-17 13:59:38 +01003277static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 struct vm_area_struct *area)
3279{
3280 return -ENXIO;
3281}
3282#endif /* coherent mmap */
3283
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003284static inline struct page *
3285snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3286{
3287 void *vaddr = substream->runtime->dma_area + ofs;
3288 return virt_to_page(vaddr);
3289}
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003292 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003294static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3295 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296{
Takashi Iwai877211f2005-11-17 13:59:38 +01003297 struct snd_pcm_substream *substream = area->vm_private_data;
3298 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 unsigned long offset;
3300 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 size_t dma_bytes;
3302
3303 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003304 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003306 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3308 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003309 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003310 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003312 else
3313 page = snd_pcm_default_page_ops(substream, offset);
3314 if (!page)
3315 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003316 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003317 vmf->page = page;
3318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319}
3320
Takashi Iwai657b1982009-11-26 12:40:21 +01003321static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3322 .open = snd_pcm_mmap_data_open,
3323 .close = snd_pcm_mmap_data_close,
3324};
3325
3326static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 .open = snd_pcm_mmap_data_open,
3328 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003329 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330};
3331
3332/*
3333 * mmap the DMA buffer on RAM
3334 */
Takashi Iwai30b771c2014-10-30 15:02:50 +01003335
3336/**
3337 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3338 * @substream: PCM substream
3339 * @area: VMA
3340 *
3341 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3342 * this function is invoked implicitly.
3343 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003344int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3345 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003347 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Takashi Iwaia5606f82013-10-24 14:25:32 +02003348#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +08003349 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3350 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3351 return remap_pfn_range(area, area->vm_start,
3352 substream->dma_buffer.addr >> PAGE_SHIFT,
3353 area->vm_end - area->vm_start, area->vm_page_prot);
3354 }
Takashi Iwaia5606f82013-10-24 14:25:32 +02003355#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai49d776f2014-10-24 12:36:23 +02003356#ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
Takashi Iwai657b1982009-11-26 12:40:21 +01003357 if (!substream->ops->page &&
3358 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3359 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3360 area,
3361 substream->runtime->dma_area,
3362 substream->runtime->dma_addr,
3363 area->vm_end - area->vm_start);
Takashi Iwai49d776f2014-10-24 12:36:23 +02003364#endif /* CONFIG_X86 */
Takashi Iwai657b1982009-11-26 12:40:21 +01003365 /* mmap with fault handler */
3366 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 return 0;
3368}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003369EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
3371/*
3372 * mmap the DMA buffer on I/O memory area
3373 */
3374#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai30b771c2014-10-30 15:02:50 +01003375/**
3376 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3377 * @substream: PCM substream
3378 * @area: VMA
3379 *
3380 * When your hardware uses the iomapped pages as the hardware buffer and
3381 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3382 * is supposed to work only on limited architectures.
3383 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003384int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3385 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386{
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003387 struct snd_pcm_runtime *runtime = substream->runtime;;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003390 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003392
3393EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394#endif /* SNDRV_PCM_INFO_MMAP */
3395
3396/*
3397 * mmap DMA buffer
3398 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003399int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 struct vm_area_struct *area)
3401{
Takashi Iwai877211f2005-11-17 13:59:38 +01003402 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 long size;
3404 unsigned long offset;
3405 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003406 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
3408 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3409 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3410 return -EINVAL;
3411 } else {
3412 if (!(area->vm_flags & VM_READ))
3413 return -EINVAL;
3414 }
3415 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3417 return -EBADFD;
3418 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3419 return -ENXIO;
3420 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3421 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3422 return -EINVAL;
3423 size = area->vm_end - area->vm_start;
3424 offset = area->vm_pgoff << PAGE_SHIFT;
3425 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3426 if ((size_t)size > dma_bytes)
3427 return -EINVAL;
3428 if (offset > dma_bytes - size)
3429 return -EINVAL;
3430
Takashi Iwai657b1982009-11-26 12:40:21 +01003431 area->vm_ops = &snd_pcm_vm_ops_data;
3432 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003434 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003436 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003437 if (!err)
3438 atomic_inc(&substream->mmap_count);
3439 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440}
3441
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003442EXPORT_SYMBOL(snd_pcm_mmap_data);
3443
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3445{
Takashi Iwai877211f2005-11-17 13:59:38 +01003446 struct snd_pcm_file * pcm_file;
3447 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 unsigned long offset;
3449
3450 pcm_file = file->private_data;
3451 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003452 if (PCM_RUNTIME_CHECK(substream))
3453 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
3455 offset = area->vm_pgoff << PAGE_SHIFT;
3456 switch (offset) {
3457 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003458 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 return -ENXIO;
3460 return snd_pcm_mmap_status(substream, file, area);
3461 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003462 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 return -ENXIO;
3464 return snd_pcm_mmap_control(substream, file, area);
3465 default:
3466 return snd_pcm_mmap_data(substream, file, area);
3467 }
3468 return 0;
3469}
3470
3471static int snd_pcm_fasync(int fd, struct file * file, int on)
3472{
Takashi Iwai877211f2005-11-17 13:59:38 +01003473 struct snd_pcm_file * pcm_file;
3474 struct snd_pcm_substream *substream;
3475 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
3477 pcm_file = file->private_data;
3478 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003479 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003480 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003482 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
3485/*
3486 * ioctl32 compat
3487 */
3488#ifdef CONFIG_COMPAT
3489#include "pcm_compat.c"
3490#else
3491#define snd_pcm_ioctl_compat NULL
3492#endif
3493
3494/*
3495 * To be removed helpers to keep binary compatibility
3496 */
3497
Takashi Iwai59d48582005-12-01 10:51:58 +01003498#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3500#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3501
Takashi Iwai877211f2005-11-17 13:59:38 +01003502static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3503 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504{
3505 unsigned int i;
3506
3507 memset(params, 0, sizeof(*params));
3508 params->flags = oparams->flags;
3509 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3510 params->masks[i].bits[0] = oparams->masks[i];
3511 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3512 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3513 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3514 params->info = oparams->info;
3515 params->msbits = oparams->msbits;
3516 params->rate_num = oparams->rate_num;
3517 params->rate_den = oparams->rate_den;
3518 params->fifo_size = oparams->fifo_size;
3519}
3520
Takashi Iwai877211f2005-11-17 13:59:38 +01003521static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3522 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523{
3524 unsigned int i;
3525
3526 memset(oparams, 0, sizeof(*oparams));
3527 oparams->flags = params->flags;
3528 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3529 oparams->masks[i] = params->masks[i].bits[0];
3530 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3531 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3532 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3533 oparams->info = params->info;
3534 oparams->msbits = params->msbits;
3535 oparams->rate_num = params->rate_num;
3536 oparams->rate_den = params->rate_den;
3537 oparams->fifo_size = params->fifo_size;
3538}
3539
Takashi Iwai877211f2005-11-17 13:59:38 +01003540static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3541 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542{
Takashi Iwai877211f2005-11-17 13:59:38 +01003543 struct snd_pcm_hw_params *params;
3544 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 int err;
3546
3547 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003548 if (!params)
3549 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
Li Zefanef44a1e2009-04-10 09:43:08 +08003551 oparams = memdup_user(_oparams, sizeof(*oparams));
3552 if (IS_ERR(oparams)) {
3553 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 goto out;
3555 }
3556 snd_pcm_hw_convert_from_old_params(params, oparams);
3557 err = snd_pcm_hw_refine(substream, params);
3558 snd_pcm_hw_convert_to_old_params(oparams, params);
3559 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3560 if (!err)
3561 err = -EFAULT;
3562 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003563
3564 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565out:
3566 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 return err;
3568}
3569
Takashi Iwai877211f2005-11-17 13:59:38 +01003570static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3571 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572{
Takashi Iwai877211f2005-11-17 13:59:38 +01003573 struct snd_pcm_hw_params *params;
3574 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 int err;
3576
3577 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003578 if (!params)
3579 return -ENOMEM;
3580
3581 oparams = memdup_user(_oparams, sizeof(*oparams));
3582 if (IS_ERR(oparams)) {
3583 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 goto out;
3585 }
3586 snd_pcm_hw_convert_from_old_params(params, oparams);
3587 err = snd_pcm_hw_params(substream, params);
3588 snd_pcm_hw_convert_to_old_params(oparams, params);
3589 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3590 if (!err)
3591 err = -EFAULT;
3592 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003593
3594 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595out:
3596 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 return err;
3598}
Takashi Iwai59d48582005-12-01 10:51:58 +01003599#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Cliff Cai70036092008-09-03 10:54:36 +02003601#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003602static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3603 unsigned long addr,
3604 unsigned long len,
3605 unsigned long pgoff,
3606 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003607{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003608 struct snd_pcm_file *pcm_file = file->private_data;
3609 struct snd_pcm_substream *substream = pcm_file->substream;
3610 struct snd_pcm_runtime *runtime = substream->runtime;
3611 unsigned long offset = pgoff << PAGE_SHIFT;
3612
3613 switch (offset) {
3614 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3615 return (unsigned long)runtime->status;
3616 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3617 return (unsigned long)runtime->control;
3618 default:
3619 return (unsigned long)runtime->dma_area + offset;
3620 }
Cliff Cai70036092008-09-03 10:54:36 +02003621}
3622#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003623# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003624#endif
3625
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626/*
3627 * Register section
3628 */
3629
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003630const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003632 .owner = THIS_MODULE,
3633 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003634 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003635 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003636 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003637 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003638 .poll = snd_pcm_playback_poll,
3639 .unlocked_ioctl = snd_pcm_playback_ioctl,
3640 .compat_ioctl = snd_pcm_ioctl_compat,
3641 .mmap = snd_pcm_mmap,
3642 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003643 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 },
3645 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003646 .owner = THIS_MODULE,
3647 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003648 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003649 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003650 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003651 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003652 .poll = snd_pcm_capture_poll,
3653 .unlocked_ioctl = snd_pcm_capture_ioctl,
3654 .compat_ioctl = snd_pcm_ioctl_compat,
3655 .mmap = snd_pcm_mmap,
3656 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003657 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 }
3659};