blob: d126c03361aef87fb1239df1e42f547c5ce36aa9 [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>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010028#include <linux/io.h>
Takashi Iwai657b1982009-11-26 12:40:21 +010029#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <sound/core.h>
31#include <sound/control.h>
32#include <sound/info.h>
33#include <sound/pcm.h>
34#include <sound/pcm_params.h>
35#include <sound/timer.h>
36#include <sound/minors.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080037#include <linux/uio.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);
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600710
711 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
712 &runtime->audio_tstamp_config);
713
714 /* backwards compatible behavior */
715 if (runtime->audio_tstamp_config.type_requested ==
716 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
717 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
718 runtime->audio_tstamp_config.type_requested =
719 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
720 else
721 runtime->audio_tstamp_config.type_requested =
722 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
723 runtime->audio_tstamp_report.valid = 0;
724 } else
725 runtime->audio_tstamp_report.valid = 1;
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 status->state = runtime->status->state;
728 status->suspended_state = runtime->status->suspended_state;
729 if (status->state == SNDRV_PCM_STATE_OPEN)
730 goto _end;
731 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100732 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100734 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
735 status->tstamp = runtime->status->tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600736 status->driver_tstamp = runtime->driver_tstamp;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500737 status->audio_tstamp =
738 runtime->status->audio_tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600739 if (runtime->audio_tstamp_report.valid == 1)
740 /* backwards compatibility, no report provided in COMPAT mode */
741 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
742 &status->audio_tstamp_accuracy,
743 &runtime->audio_tstamp_report);
744
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100745 goto _tstamp_end;
746 }
Pierre-Louis Bossart0d59b812015-02-06 15:55:50 -0600747 } else {
748 /* get tstamp only in fallback mode and only if enabled */
749 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
750 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100751 }
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100752 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 status->appl_ptr = runtime->control->appl_ptr;
754 status->hw_ptr = runtime->status->hw_ptr;
755 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
756 status->avail = snd_pcm_playback_avail(runtime);
757 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200758 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200760 status->delay += runtime->delay;
761 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 status->delay = 0;
763 } else {
764 status->avail = snd_pcm_capture_avail(runtime);
765 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200766 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 else
768 status->delay = 0;
769 }
770 status->avail_max = runtime->avail_max;
771 status->overrange = runtime->overrange;
772 runtime->avail_max = 0;
773 runtime->overrange = 0;
774 _end:
775 snd_pcm_stream_unlock_irq(substream);
776 return 0;
777}
778
Takashi Iwai877211f2005-11-17 13:59:38 +0100779static int snd_pcm_status_user(struct snd_pcm_substream *substream,
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600780 struct snd_pcm_status __user * _status,
781 bool ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Takashi Iwai877211f2005-11-17 13:59:38 +0100783 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 int res;
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 memset(&status, 0, sizeof(status));
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600787 /*
788 * with extension, parameters are read/write,
789 * get audio_tstamp_data from user,
790 * ignore rest of status structure
791 */
792 if (ext && get_user(status.audio_tstamp_data,
793 (u32 __user *)(&_status->audio_tstamp_data)))
794 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 res = snd_pcm_status(substream, &status);
796 if (res < 0)
797 return res;
798 if (copy_to_user(_status, &status, sizeof(status)))
799 return -EFAULT;
800 return 0;
801}
802
Takashi Iwai877211f2005-11-17 13:59:38 +0100803static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
804 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Takashi Iwai877211f2005-11-17 13:59:38 +0100806 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 unsigned int channel;
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 channel = info->channel;
810 runtime = substream->runtime;
811 snd_pcm_stream_lock_irq(substream);
812 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
813 snd_pcm_stream_unlock_irq(substream);
814 return -EBADFD;
815 }
816 snd_pcm_stream_unlock_irq(substream);
817 if (channel >= runtime->channels)
818 return -EINVAL;
819 memset(info, 0, sizeof(*info));
820 info->channel = channel;
821 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
822}
823
Takashi Iwai877211f2005-11-17 13:59:38 +0100824static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
825 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Takashi Iwai877211f2005-11-17 13:59:38 +0100827 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int res;
829
830 if (copy_from_user(&info, _info, sizeof(info)))
831 return -EFAULT;
832 res = snd_pcm_channel_info(substream, &info);
833 if (res < 0)
834 return res;
835 if (copy_to_user(_info, &info, sizeof(info)))
836 return -EFAULT;
837 return 0;
838}
839
Takashi Iwai877211f2005-11-17 13:59:38 +0100840static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Takashi Iwai877211f2005-11-17 13:59:38 +0100842 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (runtime->trigger_master == NULL)
844 return;
845 if (runtime->trigger_master == substream) {
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -0600846 if (!runtime->trigger_tstamp_latched)
847 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 } else {
849 snd_pcm_trigger_tstamp(runtime->trigger_master);
850 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
851 }
852 runtime->trigger_master = NULL;
853}
854
855struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100856 int (*pre_action)(struct snd_pcm_substream *substream, int state);
857 int (*do_action)(struct snd_pcm_substream *substream, int state);
858 void (*undo_action)(struct snd_pcm_substream *substream, int state);
859 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860};
861
862/*
863 * this functions is core for handling of linked stream
864 * Note: the stream state might be changed also on failure
865 * Note2: call with calling stream lock + link lock
866 */
867static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100868 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 int state, int do_lock)
870{
Takashi Iwai877211f2005-11-17 13:59:38 +0100871 struct snd_pcm_substream *s = NULL;
872 struct snd_pcm_substream *s1;
Takashi Iwaidde1c652014-10-21 15:32:13 +0200873 int res = 0, depth = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Takashi Iwaief991b92007-02-22 12:52:53 +0100875 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200876 if (do_lock && s != substream) {
877 if (s->pcm->nonatomic)
Takashi Iwaidde1c652014-10-21 15:32:13 +0200878 mutex_lock_nested(&s->self_group.mutex, depth);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200879 else
Takashi Iwaidde1c652014-10-21 15:32:13 +0200880 spin_lock_nested(&s->self_group.lock, depth);
881 depth++;
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 res = ops->pre_action(s, state);
884 if (res < 0)
885 goto _unlock;
886 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100887 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 res = ops->do_action(s, state);
889 if (res < 0) {
890 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100891 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (s1 == s) /* failed stream */
893 break;
894 ops->undo_action(s1, state);
895 }
896 }
897 s = NULL; /* unlock all */
898 goto _unlock;
899 }
900 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100901 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 ops->post_action(s, state);
903 }
904 _unlock:
905 if (do_lock) {
906 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100907 snd_pcm_group_for_each_entry(s1, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200908 if (s1 != substream) {
Takashi Iwai811deed2014-10-13 23:14:46 +0200909 if (s1->pcm->nonatomic)
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200910 mutex_unlock(&s1->self_group.mutex);
911 else
912 spin_unlock(&s1->self_group.lock);
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (s1 == s) /* end */
915 break;
916 }
917 }
918 return res;
919}
920
921/*
922 * Note: call with stream lock
923 */
924static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100925 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 int state)
927{
928 int res;
929
930 res = ops->pre_action(substream, state);
931 if (res < 0)
932 return res;
933 res = ops->do_action(substream, state);
934 if (res == 0)
935 ops->post_action(substream, state);
936 else if (ops->undo_action)
937 ops->undo_action(substream, state);
938 return res;
939}
940
941/*
942 * Note: call with stream lock
943 */
944static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100945 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 int state)
947{
948 int res;
949
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100950 if (!snd_pcm_stream_linked(substream))
951 return snd_pcm_action_single(ops, substream, state);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200952
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100953 if (substream->pcm->nonatomic) {
954 if (!mutex_trylock(&substream->group->mutex)) {
955 mutex_unlock(&substream->self_group.mutex);
956 mutex_lock(&substream->group->mutex);
957 mutex_lock(&substream->self_group.mutex);
958 }
959 res = snd_pcm_action_group(ops, substream, state, 1);
960 mutex_unlock(&substream->group->mutex);
961 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (!spin_trylock(&substream->group->lock)) {
963 spin_unlock(&substream->self_group.lock);
964 spin_lock(&substream->group->lock);
965 spin_lock(&substream->self_group.lock);
966 }
967 res = snd_pcm_action_group(ops, substream, state, 1);
968 spin_unlock(&substream->group->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970 return res;
971}
972
973/*
974 * Note: don't use any locks before
975 */
976static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100977 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 int state)
979{
980 int res;
981
Takashi Iwaie3a4bd52014-10-31 14:45:04 +0100982 snd_pcm_stream_lock_irq(substream);
983 res = snd_pcm_action(ops, substream, state);
984 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return res;
986}
987
988/*
989 */
990static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100991 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 int state)
993{
994 int res;
995
996 down_read(&snd_pcm_link_rwsem);
997 if (snd_pcm_stream_linked(substream))
998 res = snd_pcm_action_group(ops, substream, state, 0);
999 else
1000 res = snd_pcm_action_single(ops, substream, state);
1001 up_read(&snd_pcm_link_rwsem);
1002 return res;
1003}
1004
1005/*
1006 * start callbacks
1007 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001008static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Takashi Iwai877211f2005-11-17 13:59:38 +01001010 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1012 return -EBADFD;
1013 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1014 !snd_pcm_playback_data(substream))
1015 return -EPIPE;
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -06001016 runtime->trigger_tstamp_latched = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 runtime->trigger_master = substream;
1018 return 0;
1019}
1020
Takashi Iwai877211f2005-11-17 13:59:38 +01001021static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 if (substream->runtime->trigger_master != substream)
1024 return 0;
1025 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1026}
1027
Takashi Iwai877211f2005-11-17 13:59:38 +01001028static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 if (substream->runtime->trigger_master == substream)
1031 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1032}
1033
Takashi Iwai877211f2005-11-17 13:59:38 +01001034static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Takashi Iwai877211f2005-11-17 13:59:38 +01001036 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001038 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +02001039 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1040 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 runtime->status->state = state;
1042 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1043 runtime->silence_size > 0)
1044 snd_pcm_playback_silence(substream, ULONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001046 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
1047 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
1050static struct action_ops snd_pcm_action_start = {
1051 .pre_action = snd_pcm_pre_start,
1052 .do_action = snd_pcm_do_start,
1053 .undo_action = snd_pcm_undo_start,
1054 .post_action = snd_pcm_post_start
1055};
1056
1057/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001058 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001059 * @substream: the PCM substream instance
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001060 *
1061 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001063int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Takashi Iwai877211f2005-11-17 13:59:38 +01001065 return snd_pcm_action(&snd_pcm_action_start, substream,
1066 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069/*
1070 * stop callbacks
1071 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001072static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Takashi Iwai877211f2005-11-17 13:59:38 +01001074 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1076 return -EBADFD;
1077 runtime->trigger_master = substream;
1078 return 0;
1079}
1080
Takashi Iwai877211f2005-11-17 13:59:38 +01001081static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 if (substream->runtime->trigger_master == substream &&
1084 snd_pcm_running(substream))
1085 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1086 return 0; /* unconditonally stop all substreams */
1087}
1088
Takashi Iwai877211f2005-11-17 13:59:38 +01001089static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Takashi Iwai877211f2005-11-17 13:59:38 +01001091 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (runtime->status->state != state) {
1093 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001094 runtime->status->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001096 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
1097 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
1099 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001100 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
1103static struct action_ops snd_pcm_action_stop = {
1104 .pre_action = snd_pcm_pre_stop,
1105 .do_action = snd_pcm_do_stop,
1106 .post_action = snd_pcm_post_stop
1107};
1108
1109/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001110 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +02001111 * @substream: the PCM substream instance
1112 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001114 * The state of each stream is then changed to the given state unconditionally.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001115 *
Masanari Iida0a114582014-02-09 00:47:36 +09001116 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 */
Clemens Ladischfea952e2011-02-14 11:00:47 +01001118int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1121}
1122
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001123EXPORT_SYMBOL(snd_pcm_stop);
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001126 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +02001127 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001129 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 * Unlike snd_pcm_stop(), this affects only the given stream.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001131 *
1132 * Return: Zero if succesful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001134int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Takashi Iwai877211f2005-11-17 13:59:38 +01001136 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1137 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
Takashi Iwai1fb85102014-11-07 17:08:28 +01001140/**
1141 * snd_pcm_stop_xrun - stop the running streams as XRUN
1142 * @substream: the PCM substream instance
Takashi Iwai1fb85102014-11-07 17:08:28 +01001143 *
1144 * This stops the given running substream (and all linked substreams) as XRUN.
1145 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1146 *
1147 * Return: Zero if successful, or a negative error code.
1148 */
1149int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1150{
1151 unsigned long flags;
1152 int ret = 0;
1153
1154 snd_pcm_stream_lock_irqsave(substream, flags);
1155 if (snd_pcm_running(substream))
1156 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1157 snd_pcm_stream_unlock_irqrestore(substream, flags);
1158 return ret;
1159}
1160EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162/*
1163 * pause callbacks
1164 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001165static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Takashi Iwai877211f2005-11-17 13:59:38 +01001167 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1169 return -ENOSYS;
1170 if (push) {
1171 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1172 return -EBADFD;
1173 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1174 return -EBADFD;
1175 runtime->trigger_master = substream;
1176 return 0;
1177}
1178
Takashi Iwai877211f2005-11-17 13:59:38 +01001179static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
1181 if (substream->runtime->trigger_master != substream)
1182 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +02001183 /* some drivers might use hw_ptr to recover from the pause -
1184 update the hw_ptr now */
1185 if (push)
1186 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001187 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001188 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001189 * delta, effectively to skip the check once.
1190 */
1191 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return substream->ops->trigger(substream,
1193 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1194 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1195}
1196
Takashi Iwai877211f2005-11-17 13:59:38 +01001197static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 if (substream->runtime->trigger_master == substream)
1200 substream->ops->trigger(substream,
1201 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1202 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1203}
1204
Takashi Iwai877211f2005-11-17 13:59:38 +01001205static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Takashi Iwai877211f2005-11-17 13:59:38 +01001207 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 snd_pcm_trigger_tstamp(substream);
1209 if (push) {
1210 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1211 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001212 snd_timer_notify(substream->timer,
1213 SNDRV_TIMER_EVENT_MPAUSE,
1214 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001216 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 } else {
1218 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001220 snd_timer_notify(substream->timer,
1221 SNDRV_TIMER_EVENT_MCONTINUE,
1222 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224}
1225
1226static struct action_ops snd_pcm_action_pause = {
1227 .pre_action = snd_pcm_pre_pause,
1228 .do_action = snd_pcm_do_pause,
1229 .undo_action = snd_pcm_undo_pause,
1230 .post_action = snd_pcm_post_pause
1231};
1232
1233/*
1234 * Push/release the pause for all linked streams.
1235 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001236static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1239}
1240
1241#ifdef CONFIG_PM
1242/* suspend */
1243
Takashi Iwai877211f2005-11-17 13:59:38 +01001244static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Takashi Iwai877211f2005-11-17 13:59:38 +01001246 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1248 return -EBUSY;
1249 runtime->trigger_master = substream;
1250 return 0;
1251}
1252
Takashi Iwai877211f2005-11-17 13:59:38 +01001253static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Takashi Iwai877211f2005-11-17 13:59:38 +01001255 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (runtime->trigger_master != substream)
1257 return 0;
1258 if (! snd_pcm_running(substream))
1259 return 0;
1260 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1261 return 0; /* suspend unconditionally */
1262}
1263
Takashi Iwai877211f2005-11-17 13:59:38 +01001264static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Takashi Iwai877211f2005-11-17 13:59:38 +01001266 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001268 runtime->status->suspended_state = runtime->status->state;
1269 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001271 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1272 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001274 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
1277static struct action_ops snd_pcm_action_suspend = {
1278 .pre_action = snd_pcm_pre_suspend,
1279 .do_action = snd_pcm_do_suspend,
1280 .post_action = snd_pcm_post_suspend
1281};
1282
1283/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001284 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001285 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001288 *
1289 * Return: Zero if successful (or @substream is %NULL), or a negative error
1290 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001292int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
1294 int err;
1295 unsigned long flags;
1296
Takashi Iwai603bf522005-11-17 15:59:14 +01001297 if (! substream)
1298 return 0;
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 snd_pcm_stream_lock_irqsave(substream, flags);
1301 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1302 snd_pcm_stream_unlock_irqrestore(substream, flags);
1303 return err;
1304}
1305
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001306EXPORT_SYMBOL(snd_pcm_suspend);
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001309 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001310 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001313 *
1314 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001316int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Takashi Iwai877211f2005-11-17 13:59:38 +01001318 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 int stream, err = 0;
1320
Takashi Iwai603bf522005-11-17 15:59:14 +01001321 if (! pcm)
1322 return 0;
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001325 for (substream = pcm->streams[stream].substream;
1326 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 /* FIXME: the open/close code should lock this as well */
1328 if (substream->runtime == NULL)
1329 continue;
1330 err = snd_pcm_suspend(substream);
1331 if (err < 0 && err != -EBUSY)
1332 return err;
1333 }
1334 }
1335 return 0;
1336}
1337
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001338EXPORT_SYMBOL(snd_pcm_suspend_all);
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340/* resume */
1341
Takashi Iwai877211f2005-11-17 13:59:38 +01001342static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Takashi Iwai877211f2005-11-17 13:59:38 +01001344 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1346 return -ENOSYS;
1347 runtime->trigger_master = substream;
1348 return 0;
1349}
1350
Takashi Iwai877211f2005-11-17 13:59:38 +01001351static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Takashi Iwai877211f2005-11-17 13:59:38 +01001353 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (runtime->trigger_master != substream)
1355 return 0;
1356 /* DMA not running previously? */
1357 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1358 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1359 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1360 return 0;
1361 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1362}
1363
Takashi Iwai877211f2005-11-17 13:59:38 +01001364static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 if (substream->runtime->trigger_master == substream &&
1367 snd_pcm_running(substream))
1368 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1369}
1370
Takashi Iwai877211f2005-11-17 13:59:38 +01001371static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Takashi Iwai877211f2005-11-17 13:59:38 +01001373 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001375 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001377 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1378 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
1381static struct action_ops snd_pcm_action_resume = {
1382 .pre_action = snd_pcm_pre_resume,
1383 .do_action = snd_pcm_do_resume,
1384 .undo_action = snd_pcm_undo_resume,
1385 .post_action = snd_pcm_post_resume
1386};
1387
Takashi Iwai877211f2005-11-17 13:59:38 +01001388static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Takashi Iwai877211f2005-11-17 13:59:38 +01001390 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 int res;
1392
1393 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001394 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1396 snd_power_unlock(card);
1397 return res;
1398}
1399
1400#else
1401
Takashi Iwai877211f2005-11-17 13:59:38 +01001402static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
1404 return -ENOSYS;
1405}
1406
1407#endif /* CONFIG_PM */
1408
1409/*
1410 * xrun ioctl
1411 *
1412 * Change the RUNNING stream(s) to XRUN state.
1413 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001414static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Takashi Iwai877211f2005-11-17 13:59:38 +01001416 struct snd_card *card = substream->pcm->card;
1417 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 int result;
1419
1420 snd_power_lock(card);
1421 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001422 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (result < 0)
1424 goto _unlock;
1425 }
1426
1427 snd_pcm_stream_lock_irq(substream);
1428 switch (runtime->status->state) {
1429 case SNDRV_PCM_STATE_XRUN:
1430 result = 0; /* already there */
1431 break;
1432 case SNDRV_PCM_STATE_RUNNING:
1433 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1434 break;
1435 default:
1436 result = -EBADFD;
1437 }
1438 snd_pcm_stream_unlock_irq(substream);
1439 _unlock:
1440 snd_power_unlock(card);
1441 return result;
1442}
1443
1444/*
1445 * reset ioctl
1446 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001447static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Takashi Iwai877211f2005-11-17 13:59:38 +01001449 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 switch (runtime->status->state) {
1451 case SNDRV_PCM_STATE_RUNNING:
1452 case SNDRV_PCM_STATE_PREPARED:
1453 case SNDRV_PCM_STATE_PAUSED:
1454 case SNDRV_PCM_STATE_SUSPENDED:
1455 return 0;
1456 default:
1457 return -EBADFD;
1458 }
1459}
1460
Takashi Iwai877211f2005-11-17 13:59:38 +01001461static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Takashi Iwai877211f2005-11-17 13:59:38 +01001463 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1465 if (err < 0)
1466 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001468 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1469 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 runtime->silence_start = runtime->status->hw_ptr;
1471 runtime->silence_filled = 0;
1472 return 0;
1473}
1474
Takashi Iwai877211f2005-11-17 13:59:38 +01001475static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Takashi Iwai877211f2005-11-17 13:59:38 +01001477 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 runtime->control->appl_ptr = runtime->status->hw_ptr;
1479 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1480 runtime->silence_size > 0)
1481 snd_pcm_playback_silence(substream, ULONG_MAX);
1482}
1483
1484static struct action_ops snd_pcm_action_reset = {
1485 .pre_action = snd_pcm_pre_reset,
1486 .do_action = snd_pcm_do_reset,
1487 .post_action = snd_pcm_post_reset
1488};
1489
Takashi Iwai877211f2005-11-17 13:59:38 +01001490static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
1492 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1493}
1494
1495/*
1496 * prepare ioctl
1497 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001498/* we use the second argument for updating f_flags */
1499static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1500 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Takashi Iwai877211f2005-11-17 13:59:38 +01001502 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001503 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1504 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return -EBADFD;
1506 if (snd_pcm_running(substream))
1507 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001508 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return 0;
1510}
1511
Takashi Iwai877211f2005-11-17 13:59:38 +01001512static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
1514 int err;
1515 err = substream->ops->prepare(substream);
1516 if (err < 0)
1517 return err;
1518 return snd_pcm_do_reset(substream, 0);
1519}
1520
Takashi Iwai877211f2005-11-17 13:59:38 +01001521static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Takashi Iwai877211f2005-11-17 13:59:38 +01001523 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 runtime->control->appl_ptr = runtime->status->hw_ptr;
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001525 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
1528static struct action_ops snd_pcm_action_prepare = {
1529 .pre_action = snd_pcm_pre_prepare,
1530 .do_action = snd_pcm_do_prepare,
1531 .post_action = snd_pcm_post_prepare
1532};
1533
1534/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001535 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001536 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001537 * @file: file to refer f_flags
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001538 *
1539 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001541static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1542 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
1544 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001545 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001546 int f_flags;
1547
1548 if (file)
1549 f_flags = file->f_flags;
1550 else
1551 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001554 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001555 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1556 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 snd_power_unlock(card);
1558 return res;
1559}
1560
1561/*
1562 * drain ioctl
1563 */
1564
Takashi Iwai877211f2005-11-17 13:59:38 +01001565static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001567 struct snd_pcm_runtime *runtime = substream->runtime;
1568 switch (runtime->status->state) {
1569 case SNDRV_PCM_STATE_OPEN:
1570 case SNDRV_PCM_STATE_DISCONNECTED:
1571 case SNDRV_PCM_STATE_SUSPENDED:
1572 return -EBADFD;
1573 }
1574 runtime->trigger_master = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return 0;
1576}
1577
Takashi Iwai877211f2005-11-17 13:59:38 +01001578static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Takashi Iwai877211f2005-11-17 13:59:38 +01001580 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1582 switch (runtime->status->state) {
1583 case SNDRV_PCM_STATE_PREPARED:
1584 /* start playback stream if possible */
1585 if (! snd_pcm_playback_empty(substream)) {
1586 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1587 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
Takashi Iwai70372a72014-12-18 10:02:41 +01001588 } else {
1589 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
1591 break;
1592 case SNDRV_PCM_STATE_RUNNING:
1593 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1594 break;
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001595 case SNDRV_PCM_STATE_XRUN:
1596 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1597 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 default:
1599 break;
1600 }
1601 } else {
1602 /* stop running stream */
1603 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001604 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001606 snd_pcm_do_stop(substream, new_state);
1607 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
1609 }
Libin Yang48d88292014-12-31 22:09:54 +08001610
1611 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1612 runtime->trigger_master == substream &&
1613 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1614 return substream->ops->trigger(substream,
1615 SNDRV_PCM_TRIGGER_DRAIN);
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return 0;
1618}
1619
Takashi Iwai877211f2005-11-17 13:59:38 +01001620static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
1622}
1623
1624static struct action_ops snd_pcm_action_drain_init = {
1625 .pre_action = snd_pcm_pre_drain_init,
1626 .do_action = snd_pcm_do_drain_init,
1627 .post_action = snd_pcm_post_drain_init
1628};
1629
Takashi Iwai877211f2005-11-17 13:59:38 +01001630static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632/*
1633 * Drain the stream(s).
1634 * When the substream is linked, sync until the draining of all playback streams
1635 * is finished.
1636 * After this call, all streams are supposed to be either SETUP or DRAINING
1637 * (capture only) state.
1638 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001639static int snd_pcm_drain(struct snd_pcm_substream *substream,
1640 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
Takashi Iwai877211f2005-11-17 13:59:38 +01001642 struct snd_card *card;
1643 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001644 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001645 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001647 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 card = substream->pcm->card;
1650 runtime = substream->runtime;
1651
1652 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1653 return -EBADFD;
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 snd_power_lock(card);
1656 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001657 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001658 if (result < 0) {
1659 snd_power_unlock(card);
1660 return result;
1661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
1663
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001664 if (file) {
1665 if (file->f_flags & O_NONBLOCK)
1666 nonblock = 1;
1667 } else if (substream->f_flags & O_NONBLOCK)
1668 nonblock = 1;
1669
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001670 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001671 snd_pcm_stream_lock_irq(substream);
1672 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001673 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001674 snd_pcm_pause(substream, 0);
1675
1676 /* pre-start/stop - all running streams are changed to DRAINING state */
1677 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001678 if (result < 0)
1679 goto unlock;
1680 /* in non-blocking, we don't wait in ioctl but let caller poll */
1681 if (nonblock) {
1682 result = -EAGAIN;
1683 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 for (;;) {
1687 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001688 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (signal_pending(current)) {
1690 result = -ERESTARTSYS;
1691 break;
1692 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001693 /* find a substream to drain */
1694 to_check = NULL;
1695 snd_pcm_group_for_each_entry(s, substream) {
1696 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1697 continue;
1698 runtime = s->runtime;
1699 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1700 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001701 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001702 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001703 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001704 if (!to_check)
1705 break; /* all drained */
1706 init_waitqueue_entry(&wait, current);
1707 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001709 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 snd_power_unlock(card);
Takashi Iwaif2b36142011-05-26 08:09:38 +02001711 if (runtime->no_period_wakeup)
1712 tout = MAX_SCHEDULE_TIMEOUT;
1713 else {
1714 tout = 10;
1715 if (runtime->rate) {
1716 long t = runtime->period_size * 2 / runtime->rate;
1717 tout = max(t, tout);
1718 }
1719 tout = msecs_to_jiffies(tout * 1000);
1720 }
1721 tout = schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001723 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001725 remove_wait_queue(&to_check->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001726 if (card->shutdown) {
1727 result = -ENODEV;
1728 break;
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 if (tout == 0) {
1731 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1732 result = -ESTRPIPE;
1733 else {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001734 dev_dbg(substream->pcm->card->dev,
1735 "playback drain error (DMA or IRQ trouble?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1737 result = -EIO;
1738 }
1739 break;
1740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001742
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001743 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001744 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001745 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 return result;
1749}
1750
1751/*
1752 * drop ioctl
1753 *
1754 * Immediately put all linked substreams into SETUP state.
1755 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001756static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Takashi Iwai877211f2005-11-17 13:59:38 +01001758 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 int result = 0;
1760
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001761 if (PCM_RUNTIME_CHECK(substream))
1762 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001765 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001766 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1767 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return -EBADFD;
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 snd_pcm_stream_lock_irq(substream);
1771 /* resume pause */
1772 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1773 snd_pcm_pause(substream, 0);
1774
1775 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1776 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1777 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return result;
1780}
1781
1782
Al Viro0888c322013-06-05 14:09:55 -04001783static bool is_pcm_file(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Al Viro0888c322013-06-05 14:09:55 -04001785 struct inode *inode = file_inode(file);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001786 unsigned int minor;
1787
Al Viro0888c322013-06-05 14:09:55 -04001788 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1789 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 minor = iminor(inode);
Al Viro0888c322013-06-05 14:09:55 -04001791 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1792 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
1795/*
1796 * PCM link handling
1797 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001798static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
1800 int res = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001801 struct snd_pcm_file *pcm_file;
1802 struct snd_pcm_substream *substream1;
Takashi Iwai16625912012-03-13 15:55:43 +01001803 struct snd_pcm_group *group;
Al Viro0888c322013-06-05 14:09:55 -04001804 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Al Viro0888c322013-06-05 14:09:55 -04001806 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return -EBADFD;
Al Viro0888c322013-06-05 14:09:55 -04001808 if (!is_pcm_file(f.file)) {
1809 res = -EBADFD;
1810 goto _badf;
1811 }
1812 pcm_file = f.file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 substream1 = pcm_file->substream;
Takashi Iwai16625912012-03-13 15:55:43 +01001814 group = kmalloc(sizeof(*group), GFP_KERNEL);
1815 if (!group) {
1816 res = -ENOMEM;
1817 goto _nolock;
1818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 down_write(&snd_pcm_link_rwsem);
1820 write_lock_irq(&snd_pcm_link_rwlock);
1821 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001822 substream->runtime->status->state != substream1->runtime->status->state ||
1823 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 res = -EBADFD;
1825 goto _end;
1826 }
1827 if (snd_pcm_stream_linked(substream1)) {
1828 res = -EALREADY;
1829 goto _end;
1830 }
1831 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai16625912012-03-13 15:55:43 +01001832 substream->group = group;
Al Virodd6c5cd2013-06-05 14:07:08 -04001833 group = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 spin_lock_init(&substream->group->lock);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001835 mutex_init(&substream->group->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 INIT_LIST_HEAD(&substream->group->substreams);
1837 list_add_tail(&substream->link_list, &substream->group->substreams);
1838 substream->group->count = 1;
1839 }
1840 list_add_tail(&substream1->link_list, &substream->group->substreams);
1841 substream->group->count++;
1842 substream1->group = substream->group;
1843 _end:
1844 write_unlock_irq(&snd_pcm_link_rwlock);
1845 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01001846 _nolock:
Takashi Iwaia0830db2012-10-16 13:05:59 +02001847 snd_card_unref(substream1->pcm->card);
Al Virodd6c5cd2013-06-05 14:07:08 -04001848 kfree(group);
Al Viro0888c322013-06-05 14:09:55 -04001849 _badf:
1850 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return res;
1852}
1853
Takashi Iwai877211f2005-11-17 13:59:38 +01001854static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
1856 substream->group = &substream->self_group;
1857 INIT_LIST_HEAD(&substream->self_group.substreams);
1858 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1859}
1860
Takashi Iwai877211f2005-11-17 13:59:38 +01001861static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Takashi Iwaief991b92007-02-22 12:52:53 +01001863 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 int res = 0;
1865
1866 down_write(&snd_pcm_link_rwsem);
1867 write_lock_irq(&snd_pcm_link_rwlock);
1868 if (!snd_pcm_stream_linked(substream)) {
1869 res = -EALREADY;
1870 goto _end;
1871 }
1872 list_del(&substream->link_list);
1873 substream->group->count--;
1874 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001875 snd_pcm_group_for_each_entry(s, substream) {
1876 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 break;
1878 }
1879 kfree(substream->group);
1880 }
1881 relink_to_local(substream);
1882 _end:
1883 write_unlock_irq(&snd_pcm_link_rwlock);
1884 up_write(&snd_pcm_link_rwsem);
1885 return res;
1886}
1887
1888/*
1889 * hw configurator
1890 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001891static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1892 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Takashi Iwai877211f2005-11-17 13:59:38 +01001894 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1896 hw_param_interval_c(params, rule->deps[1]), &t);
1897 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1898}
1899
Takashi Iwai877211f2005-11-17 13:59:38 +01001900static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1901 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
Takashi Iwai877211f2005-11-17 13:59:38 +01001903 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1905 hw_param_interval_c(params, rule->deps[1]), &t);
1906 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1907}
1908
Takashi Iwai877211f2005-11-17 13:59:38 +01001909static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1910 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
Takashi Iwai877211f2005-11-17 13:59:38 +01001912 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1914 hw_param_interval_c(params, rule->deps[1]),
1915 (unsigned long) rule->private, &t);
1916 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1917}
1918
Takashi Iwai877211f2005-11-17 13:59:38 +01001919static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1920 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
Takashi Iwai877211f2005-11-17 13:59:38 +01001922 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1924 (unsigned long) rule->private,
1925 hw_param_interval_c(params, rule->deps[1]), &t);
1926 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1927}
1928
Takashi Iwai877211f2005-11-17 13:59:38 +01001929static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1930 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001933 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1934 struct snd_mask m;
1935 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 snd_mask_any(&m);
1937 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1938 int bits;
1939 if (! snd_mask_test(mask, k))
1940 continue;
1941 bits = snd_pcm_format_physical_width(k);
1942 if (bits <= 0)
1943 continue; /* ignore invalid formats */
1944 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1945 snd_mask_reset(&m, k);
1946 }
1947 return snd_mask_refine(mask, &m);
1948}
1949
Takashi Iwai877211f2005-11-17 13:59:38 +01001950static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1951 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Takashi Iwai877211f2005-11-17 13:59:38 +01001953 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 unsigned int k;
1955 t.min = UINT_MAX;
1956 t.max = 0;
1957 t.openmin = 0;
1958 t.openmax = 0;
1959 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1960 int bits;
1961 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1962 continue;
1963 bits = snd_pcm_format_physical_width(k);
1964 if (bits <= 0)
1965 continue; /* ignore invalid formats */
1966 if (t.min > (unsigned)bits)
1967 t.min = bits;
1968 if (t.max < (unsigned)bits)
1969 t.max = bits;
1970 }
1971 t.integer = 1;
1972 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1973}
1974
1975#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1976#error "Change this table"
1977#endif
1978
1979static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1980 48000, 64000, 88200, 96000, 176400, 192000 };
1981
Clemens Ladisch7653d552007-08-13 17:38:54 +02001982const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1983 .count = ARRAY_SIZE(rates),
1984 .list = rates,
1985};
1986
Takashi Iwai877211f2005-11-17 13:59:38 +01001987static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1988 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
Takashi Iwai877211f2005-11-17 13:59:38 +01001990 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001992 snd_pcm_known_rates.count,
1993 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994}
1995
Takashi Iwai877211f2005-11-17 13:59:38 +01001996static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1997 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
Takashi Iwai877211f2005-11-17 13:59:38 +01001999 struct snd_interval t;
2000 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 t.min = 0;
2002 t.max = substream->buffer_bytes_max;
2003 t.openmin = 0;
2004 t.openmax = 0;
2005 t.integer = 1;
2006 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2007}
2008
Takashi Iwai877211f2005-11-17 13:59:38 +01002009int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
Takashi Iwai877211f2005-11-17 13:59:38 +01002011 struct snd_pcm_runtime *runtime = substream->runtime;
2012 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 int k, err;
2014
2015 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2016 snd_mask_any(constrs_mask(constrs, k));
2017 }
2018
2019 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2020 snd_interval_any(constrs_interval(constrs, k));
2021 }
2022
2023 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2024 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2025 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2026 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2027 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2028
2029 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2030 snd_pcm_hw_rule_format, NULL,
2031 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2032 if (err < 0)
2033 return err;
2034 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2035 snd_pcm_hw_rule_sample_bits, NULL,
2036 SNDRV_PCM_HW_PARAM_FORMAT,
2037 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2038 if (err < 0)
2039 return err;
2040 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2041 snd_pcm_hw_rule_div, NULL,
2042 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2043 if (err < 0)
2044 return err;
2045 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2046 snd_pcm_hw_rule_mul, NULL,
2047 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2048 if (err < 0)
2049 return err;
2050 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2051 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2052 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2053 if (err < 0)
2054 return err;
2055 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2056 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2057 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2058 if (err < 0)
2059 return err;
2060 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2061 snd_pcm_hw_rule_div, NULL,
2062 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2063 if (err < 0)
2064 return err;
2065 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2066 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2067 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2068 if (err < 0)
2069 return err;
2070 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2071 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2072 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2073 if (err < 0)
2074 return err;
2075 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2076 snd_pcm_hw_rule_div, NULL,
2077 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2078 if (err < 0)
2079 return err;
2080 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2081 snd_pcm_hw_rule_div, NULL,
2082 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2083 if (err < 0)
2084 return err;
2085 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2086 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2087 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2088 if (err < 0)
2089 return err;
2090 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2091 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2092 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2093 if (err < 0)
2094 return err;
2095 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2096 snd_pcm_hw_rule_mul, NULL,
2097 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2098 if (err < 0)
2099 return err;
2100 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2101 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2102 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2103 if (err < 0)
2104 return err;
2105 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2106 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2107 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2108 if (err < 0)
2109 return err;
2110 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2111 snd_pcm_hw_rule_muldivk, (void*) 8,
2112 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2113 if (err < 0)
2114 return err;
2115 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2116 snd_pcm_hw_rule_muldivk, (void*) 8,
2117 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2118 if (err < 0)
2119 return err;
2120 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2121 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2122 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2123 if (err < 0)
2124 return err;
2125 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2126 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2127 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2128 if (err < 0)
2129 return err;
2130 return 0;
2131}
2132
Takashi Iwai877211f2005-11-17 13:59:38 +01002133int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
Takashi Iwai877211f2005-11-17 13:59:38 +01002135 struct snd_pcm_runtime *runtime = substream->runtime;
2136 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 int err;
2138 unsigned int mask = 0;
2139
2140 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2141 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2142 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2143 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
Takashi Iwai63825f32014-10-22 12:04:46 +02002144 if (hw_support_mmap(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2146 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2147 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2148 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2149 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2150 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2151 }
2152 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002153 if (err < 0)
2154 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002157 if (err < 0)
2158 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002161 if (err < 0)
2162 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2165 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002166 if (err < 0)
2167 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2170 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002171 if (err < 0)
2172 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2175 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002176 if (err < 0)
2177 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2180 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002181 if (err < 0)
2182 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2185 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002186 if (err < 0)
2187 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2190 snd_pcm_hw_rule_buffer_bytes_max, substream,
2191 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2192 if (err < 0)
2193 return err;
2194
2195 /* FIXME: remove */
2196 if (runtime->dma_bytes) {
2197 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002198 if (err < 0)
Sachin Kamat6cf95152012-11-21 14:36:55 +05302199 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 }
2201
2202 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2203 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2204 snd_pcm_hw_rule_rate, hw,
2205 SNDRV_PCM_HW_PARAM_RATE, -1);
2206 if (err < 0)
2207 return err;
2208 }
2209
2210 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2212
2213 return 0;
2214}
2215
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002216static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002219}
2220
2221void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2222{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002223 substream->ref_count--;
2224 if (substream->ref_count > 0)
2225 return;
2226
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002227 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002228 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (substream->ops->hw_free != NULL)
2230 substream->ops->hw_free(substream);
2231 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002232 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002234 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2235 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002236 if (substream->pcm_release) {
2237 substream->pcm_release(substream);
2238 substream->pcm_release = NULL;
2239 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002240 snd_pcm_detach_substream(substream);
2241}
2242
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002243EXPORT_SYMBOL(snd_pcm_release_substream);
2244
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002245int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2246 struct file *file,
2247 struct snd_pcm_substream **rsubstream)
2248{
2249 struct snd_pcm_substream *substream;
2250 int err;
2251
2252 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2253 if (err < 0)
2254 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002255 if (substream->ref_count > 1) {
2256 *rsubstream = substream;
2257 return 0;
2258 }
2259
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002260 err = snd_pcm_hw_constraints_init(substream);
2261 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002262 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002263 goto error;
2264 }
2265
2266 if ((err = substream->ops->open(substream)) < 0)
2267 goto error;
2268
2269 substream->hw_opened = 1;
2270
2271 err = snd_pcm_hw_constraints_complete(substream);
2272 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002273 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002274 goto error;
2275 }
2276
2277 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002279
2280 error:
2281 snd_pcm_release_substream(substream);
2282 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
2284
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002285EXPORT_SYMBOL(snd_pcm_open_substream);
2286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002288 struct snd_pcm *pcm,
Feng Tangffd3d5c2011-10-10 10:31:48 +08002289 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Takashi Iwai877211f2005-11-17 13:59:38 +01002291 struct snd_pcm_file *pcm_file;
2292 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002293 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002295 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2296 if (err < 0)
2297 return err;
2298
Takashi Iwai548a6482006-07-31 16:51:51 +02002299 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2300 if (pcm_file == NULL) {
2301 snd_pcm_release_substream(substream);
2302 return -ENOMEM;
2303 }
2304 pcm_file->substream = substream;
2305 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002306 substream->file = pcm_file;
2307 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 file->private_data = pcm_file;
Feng Tangffd3d5c2011-10-10 10:31:48 +08002310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 return 0;
2312}
2313
Clemens Ladischf87135f2005-11-20 14:06:59 +01002314static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
Takashi Iwai877211f2005-11-17 13:59:38 +01002316 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002317 int err = nonseekable_open(inode, file);
2318 if (err < 0)
2319 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002320 pcm = snd_lookup_minor_data(iminor(inode),
2321 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002322 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002323 if (pcm)
2324 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002325 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002326}
2327
2328static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2329{
2330 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002331 int err = nonseekable_open(inode, file);
2332 if (err < 0)
2333 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002334 pcm = snd_lookup_minor_data(iminor(inode),
2335 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002336 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002337 if (pcm)
2338 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002339 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002340}
2341
2342static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2343{
2344 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 wait_queue_t wait;
2346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 if (pcm == NULL) {
2348 err = -ENODEV;
2349 goto __error1;
2350 }
2351 err = snd_card_file_add(pcm->card, file);
2352 if (err < 0)
2353 goto __error1;
2354 if (!try_module_get(pcm->card->module)) {
2355 err = -EFAULT;
2356 goto __error2;
2357 }
2358 init_waitqueue_entry(&wait, current);
2359 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002360 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 while (1) {
Feng Tangffd3d5c2011-10-10 10:31:48 +08002362 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (err >= 0)
2364 break;
2365 if (err == -EAGAIN) {
2366 if (file->f_flags & O_NONBLOCK) {
2367 err = -EBUSY;
2368 break;
2369 }
2370 } else
2371 break;
2372 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002373 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002375 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02002376 if (pcm->card->shutdown) {
2377 err = -ENODEV;
2378 break;
2379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if (signal_pending(current)) {
2381 err = -ERESTARTSYS;
2382 break;
2383 }
2384 }
2385 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002386 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (err < 0)
2388 goto __error;
2389 return err;
2390
2391 __error:
2392 module_put(pcm->card->module);
2393 __error2:
2394 snd_card_file_remove(pcm->card, file);
2395 __error1:
2396 return err;
2397}
2398
2399static int snd_pcm_release(struct inode *inode, struct file *file)
2400{
Takashi Iwai877211f2005-11-17 13:59:38 +01002401 struct snd_pcm *pcm;
2402 struct snd_pcm_substream *substream;
2403 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 pcm_file = file->private_data;
2406 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002407 if (snd_BUG_ON(!substream))
2408 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002410 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002411 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002412 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002413 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 wake_up(&pcm->open_wait);
2415 module_put(pcm->card->module);
2416 snd_card_file_remove(pcm->card, file);
2417 return 0;
2418}
2419
Takashi Iwai877211f2005-11-17 13:59:38 +01002420static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2421 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
Takashi Iwai877211f2005-11-17 13:59:38 +01002423 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 snd_pcm_sframes_t appl_ptr;
2425 snd_pcm_sframes_t ret;
2426 snd_pcm_sframes_t hw_avail;
2427
2428 if (frames == 0)
2429 return 0;
2430
2431 snd_pcm_stream_lock_irq(substream);
2432 switch (runtime->status->state) {
2433 case SNDRV_PCM_STATE_PREPARED:
2434 break;
2435 case SNDRV_PCM_STATE_DRAINING:
2436 case SNDRV_PCM_STATE_RUNNING:
2437 if (snd_pcm_update_hw_ptr(substream) >= 0)
2438 break;
2439 /* Fall through */
2440 case SNDRV_PCM_STATE_XRUN:
2441 ret = -EPIPE;
2442 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002443 case SNDRV_PCM_STATE_SUSPENDED:
2444 ret = -ESTRPIPE;
2445 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 default:
2447 ret = -EBADFD;
2448 goto __end;
2449 }
2450
2451 hw_avail = snd_pcm_playback_hw_avail(runtime);
2452 if (hw_avail <= 0) {
2453 ret = 0;
2454 goto __end;
2455 }
2456 if (frames > (snd_pcm_uframes_t)hw_avail)
2457 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 appl_ptr = runtime->control->appl_ptr - frames;
2459 if (appl_ptr < 0)
2460 appl_ptr += runtime->boundary;
2461 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 ret = frames;
2463 __end:
2464 snd_pcm_stream_unlock_irq(substream);
2465 return ret;
2466}
2467
Takashi Iwai877211f2005-11-17 13:59:38 +01002468static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2469 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Takashi Iwai877211f2005-11-17 13:59:38 +01002471 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 snd_pcm_sframes_t appl_ptr;
2473 snd_pcm_sframes_t ret;
2474 snd_pcm_sframes_t hw_avail;
2475
2476 if (frames == 0)
2477 return 0;
2478
2479 snd_pcm_stream_lock_irq(substream);
2480 switch (runtime->status->state) {
2481 case SNDRV_PCM_STATE_PREPARED:
2482 case SNDRV_PCM_STATE_DRAINING:
2483 break;
2484 case SNDRV_PCM_STATE_RUNNING:
2485 if (snd_pcm_update_hw_ptr(substream) >= 0)
2486 break;
2487 /* Fall through */
2488 case SNDRV_PCM_STATE_XRUN:
2489 ret = -EPIPE;
2490 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002491 case SNDRV_PCM_STATE_SUSPENDED:
2492 ret = -ESTRPIPE;
2493 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 default:
2495 ret = -EBADFD;
2496 goto __end;
2497 }
2498
2499 hw_avail = snd_pcm_capture_hw_avail(runtime);
2500 if (hw_avail <= 0) {
2501 ret = 0;
2502 goto __end;
2503 }
2504 if (frames > (snd_pcm_uframes_t)hw_avail)
2505 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 appl_ptr = runtime->control->appl_ptr - frames;
2507 if (appl_ptr < 0)
2508 appl_ptr += runtime->boundary;
2509 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 ret = frames;
2511 __end:
2512 snd_pcm_stream_unlock_irq(substream);
2513 return ret;
2514}
2515
Takashi Iwai877211f2005-11-17 13:59:38 +01002516static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2517 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
Takashi Iwai877211f2005-11-17 13:59:38 +01002519 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 snd_pcm_sframes_t appl_ptr;
2521 snd_pcm_sframes_t ret;
2522 snd_pcm_sframes_t avail;
2523
2524 if (frames == 0)
2525 return 0;
2526
2527 snd_pcm_stream_lock_irq(substream);
2528 switch (runtime->status->state) {
2529 case SNDRV_PCM_STATE_PREPARED:
2530 case SNDRV_PCM_STATE_PAUSED:
2531 break;
2532 case SNDRV_PCM_STATE_DRAINING:
2533 case SNDRV_PCM_STATE_RUNNING:
2534 if (snd_pcm_update_hw_ptr(substream) >= 0)
2535 break;
2536 /* Fall through */
2537 case SNDRV_PCM_STATE_XRUN:
2538 ret = -EPIPE;
2539 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002540 case SNDRV_PCM_STATE_SUSPENDED:
2541 ret = -ESTRPIPE;
2542 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 default:
2544 ret = -EBADFD;
2545 goto __end;
2546 }
2547
2548 avail = snd_pcm_playback_avail(runtime);
2549 if (avail <= 0) {
2550 ret = 0;
2551 goto __end;
2552 }
2553 if (frames > (snd_pcm_uframes_t)avail)
2554 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 appl_ptr = runtime->control->appl_ptr + frames;
2556 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2557 appl_ptr -= runtime->boundary;
2558 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 ret = frames;
2560 __end:
2561 snd_pcm_stream_unlock_irq(substream);
2562 return ret;
2563}
2564
Takashi Iwai877211f2005-11-17 13:59:38 +01002565static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2566 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
Takashi Iwai877211f2005-11-17 13:59:38 +01002568 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 snd_pcm_sframes_t appl_ptr;
2570 snd_pcm_sframes_t ret;
2571 snd_pcm_sframes_t avail;
2572
2573 if (frames == 0)
2574 return 0;
2575
2576 snd_pcm_stream_lock_irq(substream);
2577 switch (runtime->status->state) {
2578 case SNDRV_PCM_STATE_PREPARED:
2579 case SNDRV_PCM_STATE_DRAINING:
2580 case SNDRV_PCM_STATE_PAUSED:
2581 break;
2582 case SNDRV_PCM_STATE_RUNNING:
2583 if (snd_pcm_update_hw_ptr(substream) >= 0)
2584 break;
2585 /* Fall through */
2586 case SNDRV_PCM_STATE_XRUN:
2587 ret = -EPIPE;
2588 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002589 case SNDRV_PCM_STATE_SUSPENDED:
2590 ret = -ESTRPIPE;
2591 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 default:
2593 ret = -EBADFD;
2594 goto __end;
2595 }
2596
2597 avail = snd_pcm_capture_avail(runtime);
2598 if (avail <= 0) {
2599 ret = 0;
2600 goto __end;
2601 }
2602 if (frames > (snd_pcm_uframes_t)avail)
2603 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 appl_ptr = runtime->control->appl_ptr + frames;
2605 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2606 appl_ptr -= runtime->boundary;
2607 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 ret = frames;
2609 __end:
2610 snd_pcm_stream_unlock_irq(substream);
2611 return ret;
2612}
2613
Takashi Iwai877211f2005-11-17 13:59:38 +01002614static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
Takashi Iwai877211f2005-11-17 13:59:38 +01002616 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 int err;
2618
2619 snd_pcm_stream_lock_irq(substream);
2620 switch (runtime->status->state) {
2621 case SNDRV_PCM_STATE_DRAINING:
2622 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2623 goto __badfd;
Takashi Iwai1f961532013-10-28 12:40:46 +01002624 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 case SNDRV_PCM_STATE_RUNNING:
2626 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2627 break;
2628 /* Fall through */
2629 case SNDRV_PCM_STATE_PREPARED:
2630 case SNDRV_PCM_STATE_SUSPENDED:
2631 err = 0;
2632 break;
2633 case SNDRV_PCM_STATE_XRUN:
2634 err = -EPIPE;
2635 break;
2636 default:
2637 __badfd:
2638 err = -EBADFD;
2639 break;
2640 }
2641 snd_pcm_stream_unlock_irq(substream);
2642 return err;
2643}
2644
Takashi Iwai877211f2005-11-17 13:59:38 +01002645static int snd_pcm_delay(struct snd_pcm_substream *substream,
2646 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647{
Takashi Iwai877211f2005-11-17 13:59:38 +01002648 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 int err;
2650 snd_pcm_sframes_t n = 0;
2651
2652 snd_pcm_stream_lock_irq(substream);
2653 switch (runtime->status->state) {
2654 case SNDRV_PCM_STATE_DRAINING:
2655 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2656 goto __badfd;
Takashi Iwai1f961532013-10-28 12:40:46 +01002657 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 case SNDRV_PCM_STATE_RUNNING:
2659 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2660 break;
2661 /* Fall through */
2662 case SNDRV_PCM_STATE_PREPARED:
2663 case SNDRV_PCM_STATE_SUSPENDED:
2664 err = 0;
2665 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2666 n = snd_pcm_playback_hw_avail(runtime);
2667 else
2668 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002669 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 break;
2671 case SNDRV_PCM_STATE_XRUN:
2672 err = -EPIPE;
2673 break;
2674 default:
2675 __badfd:
2676 err = -EBADFD;
2677 break;
2678 }
2679 snd_pcm_stream_unlock_irq(substream);
2680 if (!err)
2681 if (put_user(n, res))
2682 err = -EFAULT;
2683 return err;
2684}
2685
Takashi Iwai877211f2005-11-17 13:59:38 +01002686static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2687 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
Takashi Iwai877211f2005-11-17 13:59:38 +01002689 struct snd_pcm_runtime *runtime = substream->runtime;
2690 struct snd_pcm_sync_ptr sync_ptr;
2691 volatile struct snd_pcm_mmap_status *status;
2692 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 int err;
2694
2695 memset(&sync_ptr, 0, sizeof(sync_ptr));
2696 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2697 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002698 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 -07002699 return -EFAULT;
2700 status = runtime->status;
2701 control = runtime->control;
2702 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2703 err = snd_pcm_hwsync(substream);
2704 if (err < 0)
2705 return err;
2706 }
2707 snd_pcm_stream_lock_irq(substream);
2708 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2709 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2710 else
2711 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2712 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2713 control->avail_min = sync_ptr.c.control.avail_min;
2714 else
2715 sync_ptr.c.control.avail_min = control->avail_min;
2716 sync_ptr.s.status.state = status->state;
2717 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2718 sync_ptr.s.status.tstamp = status->tstamp;
2719 sync_ptr.s.status.suspended_state = status->suspended_state;
2720 snd_pcm_stream_unlock_irq(substream);
2721 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2722 return -EFAULT;
2723 return 0;
2724}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002725
2726static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2727{
2728 struct snd_pcm_runtime *runtime = substream->runtime;
2729 int arg;
2730
2731 if (get_user(arg, _arg))
2732 return -EFAULT;
2733 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2734 return -EINVAL;
Takashi Iwai2408c212014-07-10 09:40:38 +02002735 runtime->tstamp_type = arg;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002736 return 0;
2737}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Takashi Iwai0df63e42006-04-28 15:13:41 +02002739static int snd_pcm_common_ioctl1(struct file *file,
2740 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 unsigned int cmd, void __user *arg)
2742{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 switch (cmd) {
2744 case SNDRV_PCM_IOCTL_PVERSION:
2745 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2746 case SNDRV_PCM_IOCTL_INFO:
2747 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002748 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2749 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002750 case SNDRV_PCM_IOCTL_TTSTAMP:
2751 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 case SNDRV_PCM_IOCTL_HW_REFINE:
2753 return snd_pcm_hw_refine_user(substream, arg);
2754 case SNDRV_PCM_IOCTL_HW_PARAMS:
2755 return snd_pcm_hw_params_user(substream, arg);
2756 case SNDRV_PCM_IOCTL_HW_FREE:
2757 return snd_pcm_hw_free(substream);
2758 case SNDRV_PCM_IOCTL_SW_PARAMS:
2759 return snd_pcm_sw_params_user(substream, arg);
2760 case SNDRV_PCM_IOCTL_STATUS:
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -06002761 return snd_pcm_status_user(substream, arg, false);
2762 case SNDRV_PCM_IOCTL_STATUS_EXT:
2763 return snd_pcm_status_user(substream, arg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2765 return snd_pcm_channel_info_user(substream, arg);
2766 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002767 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 case SNDRV_PCM_IOCTL_RESET:
2769 return snd_pcm_reset(substream);
2770 case SNDRV_PCM_IOCTL_START:
2771 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2772 case SNDRV_PCM_IOCTL_LINK:
2773 return snd_pcm_link(substream, (int)(unsigned long) arg);
2774 case SNDRV_PCM_IOCTL_UNLINK:
2775 return snd_pcm_unlink(substream);
2776 case SNDRV_PCM_IOCTL_RESUME:
2777 return snd_pcm_resume(substream);
2778 case SNDRV_PCM_IOCTL_XRUN:
2779 return snd_pcm_xrun(substream);
2780 case SNDRV_PCM_IOCTL_HWSYNC:
2781 return snd_pcm_hwsync(substream);
2782 case SNDRV_PCM_IOCTL_DELAY:
2783 return snd_pcm_delay(substream, arg);
2784 case SNDRV_PCM_IOCTL_SYNC_PTR:
2785 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002786#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2788 return snd_pcm_hw_refine_old_user(substream, arg);
2789 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2790 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002791#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002793 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 case SNDRV_PCM_IOCTL_DROP:
2795 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002796 case SNDRV_PCM_IOCTL_PAUSE:
2797 {
2798 int res;
2799 snd_pcm_stream_lock_irq(substream);
2800 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2801 snd_pcm_stream_unlock_irq(substream);
2802 return res;
2803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 }
Takashi Iwai09e56df2014-02-04 18:19:48 +01002805 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 return -ENOTTY;
2807}
2808
Takashi Iwai0df63e42006-04-28 15:13:41 +02002809static int snd_pcm_playback_ioctl1(struct file *file,
2810 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 unsigned int cmd, void __user *arg)
2812{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002813 if (snd_BUG_ON(!substream))
2814 return -ENXIO;
2815 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2816 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 switch (cmd) {
2818 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2819 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002820 struct snd_xferi xferi;
2821 struct snd_xferi __user *_xferi = arg;
2822 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 snd_pcm_sframes_t result;
2824 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2825 return -EBADFD;
2826 if (put_user(0, &_xferi->result))
2827 return -EFAULT;
2828 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2829 return -EFAULT;
2830 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2831 __put_user(result, &_xferi->result);
2832 return result < 0 ? result : 0;
2833 }
2834 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2835 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002836 struct snd_xfern xfern;
2837 struct snd_xfern __user *_xfern = arg;
2838 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 void __user **bufs;
2840 snd_pcm_sframes_t result;
2841 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2842 return -EBADFD;
2843 if (runtime->channels > 128)
2844 return -EINVAL;
2845 if (put_user(0, &_xfern->result))
2846 return -EFAULT;
2847 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2848 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002849
2850 bufs = memdup_user(xfern.bufs,
2851 sizeof(void *) * runtime->channels);
2852 if (IS_ERR(bufs))
2853 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2855 kfree(bufs);
2856 __put_user(result, &_xfern->result);
2857 return result < 0 ? result : 0;
2858 }
2859 case SNDRV_PCM_IOCTL_REWIND:
2860 {
2861 snd_pcm_uframes_t frames;
2862 snd_pcm_uframes_t __user *_frames = arg;
2863 snd_pcm_sframes_t result;
2864 if (get_user(frames, _frames))
2865 return -EFAULT;
2866 if (put_user(0, _frames))
2867 return -EFAULT;
2868 result = snd_pcm_playback_rewind(substream, frames);
2869 __put_user(result, _frames);
2870 return result < 0 ? result : 0;
2871 }
2872 case SNDRV_PCM_IOCTL_FORWARD:
2873 {
2874 snd_pcm_uframes_t frames;
2875 snd_pcm_uframes_t __user *_frames = arg;
2876 snd_pcm_sframes_t result;
2877 if (get_user(frames, _frames))
2878 return -EFAULT;
2879 if (put_user(0, _frames))
2880 return -EFAULT;
2881 result = snd_pcm_playback_forward(substream, frames);
2882 __put_user(result, _frames);
2883 return result < 0 ? result : 0;
2884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002886 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887}
2888
Takashi Iwai0df63e42006-04-28 15:13:41 +02002889static int snd_pcm_capture_ioctl1(struct file *file,
2890 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 unsigned int cmd, void __user *arg)
2892{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002893 if (snd_BUG_ON(!substream))
2894 return -ENXIO;
2895 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2896 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 switch (cmd) {
2898 case SNDRV_PCM_IOCTL_READI_FRAMES:
2899 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002900 struct snd_xferi xferi;
2901 struct snd_xferi __user *_xferi = arg;
2902 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 snd_pcm_sframes_t result;
2904 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2905 return -EBADFD;
2906 if (put_user(0, &_xferi->result))
2907 return -EFAULT;
2908 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2909 return -EFAULT;
2910 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2911 __put_user(result, &_xferi->result);
2912 return result < 0 ? result : 0;
2913 }
2914 case SNDRV_PCM_IOCTL_READN_FRAMES:
2915 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002916 struct snd_xfern xfern;
2917 struct snd_xfern __user *_xfern = arg;
2918 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 void *bufs;
2920 snd_pcm_sframes_t result;
2921 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2922 return -EBADFD;
2923 if (runtime->channels > 128)
2924 return -EINVAL;
2925 if (put_user(0, &_xfern->result))
2926 return -EFAULT;
2927 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2928 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002929
2930 bufs = memdup_user(xfern.bufs,
2931 sizeof(void *) * runtime->channels);
2932 if (IS_ERR(bufs))
2933 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2935 kfree(bufs);
2936 __put_user(result, &_xfern->result);
2937 return result < 0 ? result : 0;
2938 }
2939 case SNDRV_PCM_IOCTL_REWIND:
2940 {
2941 snd_pcm_uframes_t frames;
2942 snd_pcm_uframes_t __user *_frames = arg;
2943 snd_pcm_sframes_t result;
2944 if (get_user(frames, _frames))
2945 return -EFAULT;
2946 if (put_user(0, _frames))
2947 return -EFAULT;
2948 result = snd_pcm_capture_rewind(substream, frames);
2949 __put_user(result, _frames);
2950 return result < 0 ? result : 0;
2951 }
2952 case SNDRV_PCM_IOCTL_FORWARD:
2953 {
2954 snd_pcm_uframes_t frames;
2955 snd_pcm_uframes_t __user *_frames = arg;
2956 snd_pcm_sframes_t result;
2957 if (get_user(frames, _frames))
2958 return -EFAULT;
2959 if (put_user(0, _frames))
2960 return -EFAULT;
2961 result = snd_pcm_capture_forward(substream, frames);
2962 __put_user(result, _frames);
2963 return result < 0 ? result : 0;
2964 }
2965 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002966 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967}
2968
Takashi Iwai877211f2005-11-17 13:59:38 +01002969static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2970 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971{
Takashi Iwai877211f2005-11-17 13:59:38 +01002972 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
2974 pcm_file = file->private_data;
2975
2976 if (((cmd >> 8) & 0xff) != 'A')
2977 return -ENOTTY;
2978
Takashi Iwai0df63e42006-04-28 15:13:41 +02002979 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2980 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981}
2982
Takashi Iwai877211f2005-11-17 13:59:38 +01002983static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2984 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
Takashi Iwai877211f2005-11-17 13:59:38 +01002986 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
2988 pcm_file = file->private_data;
2989
2990 if (((cmd >> 8) & 0xff) != 'A')
2991 return -ENOTTY;
2992
Takashi Iwai0df63e42006-04-28 15:13:41 +02002993 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2994 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995}
2996
Takashi Iwai877211f2005-11-17 13:59:38 +01002997int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 unsigned int cmd, void *arg)
2999{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003000 mm_segment_t fs;
3001 int result;
3002
3003 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 switch (substream->stream) {
3005 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02003006 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
3007 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003008 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02003010 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
3011 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003012 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003014 result = -EINVAL;
3015 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003017 snd_leave_user(fs);
3018 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019}
3020
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003021EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3022
Takashi Iwai877211f2005-11-17 13:59:38 +01003023static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3024 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
Takashi Iwai877211f2005-11-17 13:59:38 +01003026 struct snd_pcm_file *pcm_file;
3027 struct snd_pcm_substream *substream;
3028 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 snd_pcm_sframes_t result;
3030
3031 pcm_file = file->private_data;
3032 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003033 if (PCM_RUNTIME_CHECK(substream))
3034 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 runtime = substream->runtime;
3036 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3037 return -EBADFD;
3038 if (!frame_aligned(runtime, count))
3039 return -EINVAL;
3040 count = bytes_to_frames(runtime, count);
3041 result = snd_pcm_lib_read(substream, buf, count);
3042 if (result > 0)
3043 result = frames_to_bytes(runtime, result);
3044 return result;
3045}
3046
Takashi Iwai877211f2005-11-17 13:59:38 +01003047static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3048 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
Takashi Iwai877211f2005-11-17 13:59:38 +01003050 struct snd_pcm_file *pcm_file;
3051 struct snd_pcm_substream *substream;
3052 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 snd_pcm_sframes_t result;
3054
3055 pcm_file = file->private_data;
3056 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003057 if (PCM_RUNTIME_CHECK(substream))
3058 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003060 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3061 return -EBADFD;
3062 if (!frame_aligned(runtime, count))
3063 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 count = bytes_to_frames(runtime, count);
3065 result = snd_pcm_lib_write(substream, buf, count);
3066 if (result > 0)
3067 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 return result;
3069}
3070
Al Viro1c65d982015-04-04 00:19:32 -04003071static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
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;
3086 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3087 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003088 if (!iter_is_iovec(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003090 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003092 if (!frame_aligned(runtime, to->iov->iov_len))
3093 return -EINVAL;
3094 frames = bytes_to_samples(runtime, to->iov->iov_len);
3095 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 if (bufs == NULL)
3097 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003098 for (i = 0; i < to->nr_segs; ++i)
3099 bufs[i] = to->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 result = snd_pcm_lib_readv(substream, bufs, frames);
3101 if (result > 0)
3102 result = frames_to_bytes(runtime, result);
3103 kfree(bufs);
3104 return result;
3105}
3106
Al Viro1c65d982015-04-04 00:19:32 -04003107static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108{
Takashi Iwai877211f2005-11-17 13:59:38 +01003109 struct snd_pcm_file *pcm_file;
3110 struct snd_pcm_substream *substream;
3111 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 snd_pcm_sframes_t result;
3113 unsigned long i;
3114 void __user **bufs;
3115 snd_pcm_uframes_t frames;
3116
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003117 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003119 if (PCM_RUNTIME_CHECK(substream))
3120 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003122 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3123 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003124 if (!iter_is_iovec(from))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003125 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003126 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3127 !frame_aligned(runtime, from->iov->iov_len))
3128 return -EINVAL;
3129 frames = bytes_to_samples(runtime, from->iov->iov_len);
3130 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 if (bufs == NULL)
3132 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003133 for (i = 0; i < from->nr_segs; ++i)
3134 bufs[i] = from->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 result = snd_pcm_lib_writev(substream, bufs, frames);
3136 if (result > 0)
3137 result = frames_to_bytes(runtime, result);
3138 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 return result;
3140}
3141
3142static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3143{
Takashi Iwai877211f2005-11-17 13:59:38 +01003144 struct snd_pcm_file *pcm_file;
3145 struct snd_pcm_substream *substream;
3146 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 unsigned int mask;
3148 snd_pcm_uframes_t avail;
3149
3150 pcm_file = file->private_data;
3151
3152 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003153 if (PCM_RUNTIME_CHECK(substream))
3154 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 runtime = substream->runtime;
3156
3157 poll_wait(file, &runtime->sleep, wait);
3158
3159 snd_pcm_stream_lock_irq(substream);
3160 avail = snd_pcm_playback_avail(runtime);
3161 switch (runtime->status->state) {
3162 case SNDRV_PCM_STATE_RUNNING:
3163 case SNDRV_PCM_STATE_PREPARED:
3164 case SNDRV_PCM_STATE_PAUSED:
3165 if (avail >= runtime->control->avail_min) {
3166 mask = POLLOUT | POLLWRNORM;
3167 break;
3168 }
3169 /* Fall through */
3170 case SNDRV_PCM_STATE_DRAINING:
3171 mask = 0;
3172 break;
3173 default:
3174 mask = POLLOUT | POLLWRNORM | POLLERR;
3175 break;
3176 }
3177 snd_pcm_stream_unlock_irq(substream);
3178 return mask;
3179}
3180
3181static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3182{
Takashi Iwai877211f2005-11-17 13:59:38 +01003183 struct snd_pcm_file *pcm_file;
3184 struct snd_pcm_substream *substream;
3185 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 unsigned int mask;
3187 snd_pcm_uframes_t avail;
3188
3189 pcm_file = file->private_data;
3190
3191 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003192 if (PCM_RUNTIME_CHECK(substream))
3193 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 runtime = substream->runtime;
3195
3196 poll_wait(file, &runtime->sleep, wait);
3197
3198 snd_pcm_stream_lock_irq(substream);
3199 avail = snd_pcm_capture_avail(runtime);
3200 switch (runtime->status->state) {
3201 case SNDRV_PCM_STATE_RUNNING:
3202 case SNDRV_PCM_STATE_PREPARED:
3203 case SNDRV_PCM_STATE_PAUSED:
3204 if (avail >= runtime->control->avail_min) {
3205 mask = POLLIN | POLLRDNORM;
3206 break;
3207 }
3208 mask = 0;
3209 break;
3210 case SNDRV_PCM_STATE_DRAINING:
3211 if (avail > 0) {
3212 mask = POLLIN | POLLRDNORM;
3213 break;
3214 }
3215 /* Fall through */
3216 default:
3217 mask = POLLIN | POLLRDNORM | POLLERR;
3218 break;
3219 }
3220 snd_pcm_stream_unlock_irq(substream);
3221 return mask;
3222}
3223
3224/*
3225 * mmap support
3226 */
3227
3228/*
3229 * Only on coherent architectures, we can mmap the status and the control records
3230 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3231 */
3232#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3233/*
3234 * mmap status record
3235 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003236static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3237 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
Takashi Iwai877211f2005-11-17 13:59:38 +01003239 struct snd_pcm_substream *substream = area->vm_private_data;
3240 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
3242 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003243 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003245 vmf->page = virt_to_page(runtime->status);
3246 get_page(vmf->page);
3247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248}
3249
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003250static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003252 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253};
3254
Takashi Iwai877211f2005-11-17 13:59:38 +01003255static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 struct vm_area_struct *area)
3257{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 long size;
3259 if (!(area->vm_flags & VM_READ))
3260 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003262 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 return -EINVAL;
3264 area->vm_ops = &snd_pcm_vm_ops_status;
3265 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003266 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 return 0;
3268}
3269
3270/*
3271 * mmap control record
3272 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003273static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3274 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275{
Takashi Iwai877211f2005-11-17 13:59:38 +01003276 struct snd_pcm_substream *substream = area->vm_private_data;
3277 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
3279 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003280 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003282 vmf->page = virt_to_page(runtime->control);
3283 get_page(vmf->page);
3284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285}
3286
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003287static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003289 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290};
3291
Takashi Iwai877211f2005-11-17 13:59:38 +01003292static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 struct vm_area_struct *area)
3294{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 long size;
3296 if (!(area->vm_flags & VM_READ))
3297 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003299 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 return -EINVAL;
3301 area->vm_ops = &snd_pcm_vm_ops_control;
3302 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003303 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 return 0;
3305}
3306#else /* ! coherent mmap */
3307/*
3308 * don't support mmap for status and control records.
3309 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003310static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 struct vm_area_struct *area)
3312{
3313 return -ENXIO;
3314}
Takashi Iwai877211f2005-11-17 13:59:38 +01003315static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 struct vm_area_struct *area)
3317{
3318 return -ENXIO;
3319}
3320#endif /* coherent mmap */
3321
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003322static inline struct page *
3323snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3324{
3325 void *vaddr = substream->runtime->dma_area + ofs;
3326 return virt_to_page(vaddr);
3327}
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003330 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003332static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3333 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
Takashi Iwai877211f2005-11-17 13:59:38 +01003335 struct snd_pcm_substream *substream = area->vm_private_data;
3336 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 unsigned long offset;
3338 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 size_t dma_bytes;
3340
3341 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003342 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003344 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3346 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003347 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003348 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003350 else
3351 page = snd_pcm_default_page_ops(substream, offset);
3352 if (!page)
3353 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003354 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003355 vmf->page = page;
3356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357}
3358
Takashi Iwai657b1982009-11-26 12:40:21 +01003359static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3360 .open = snd_pcm_mmap_data_open,
3361 .close = snd_pcm_mmap_data_close,
3362};
3363
3364static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 .open = snd_pcm_mmap_data_open,
3366 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003367 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368};
3369
3370/*
3371 * mmap the DMA buffer on RAM
3372 */
Takashi Iwai30b771c2014-10-30 15:02:50 +01003373
3374/**
3375 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3376 * @substream: PCM substream
3377 * @area: VMA
3378 *
3379 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3380 * this function is invoked implicitly.
3381 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003382int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3383 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003385 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Takashi Iwaia5606f82013-10-24 14:25:32 +02003386#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +08003387 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3388 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3389 return remap_pfn_range(area, area->vm_start,
3390 substream->dma_buffer.addr >> PAGE_SHIFT,
3391 area->vm_end - area->vm_start, area->vm_page_prot);
3392 }
Takashi Iwaia5606f82013-10-24 14:25:32 +02003393#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai49d776f2014-10-24 12:36:23 +02003394#ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
Takashi Iwai657b1982009-11-26 12:40:21 +01003395 if (!substream->ops->page &&
3396 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3397 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3398 area,
3399 substream->runtime->dma_area,
3400 substream->runtime->dma_addr,
3401 area->vm_end - area->vm_start);
Takashi Iwai49d776f2014-10-24 12:36:23 +02003402#endif /* CONFIG_X86 */
Takashi Iwai657b1982009-11-26 12:40:21 +01003403 /* mmap with fault handler */
3404 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 return 0;
3406}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003407EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
3409/*
3410 * mmap the DMA buffer on I/O memory area
3411 */
3412#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai30b771c2014-10-30 15:02:50 +01003413/**
3414 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3415 * @substream: PCM substream
3416 * @area: VMA
3417 *
3418 * When your hardware uses the iomapped pages as the hardware buffer and
3419 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3420 * is supposed to work only on limited architectures.
3421 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003422int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3423 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424{
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003425 struct snd_pcm_runtime *runtime = substream->runtime;;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003428 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003430
3431EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432#endif /* SNDRV_PCM_INFO_MMAP */
3433
3434/*
3435 * mmap DMA buffer
3436 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003437int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 struct vm_area_struct *area)
3439{
Takashi Iwai877211f2005-11-17 13:59:38 +01003440 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 long size;
3442 unsigned long offset;
3443 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003444 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
3446 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3447 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3448 return -EINVAL;
3449 } else {
3450 if (!(area->vm_flags & VM_READ))
3451 return -EINVAL;
3452 }
3453 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3455 return -EBADFD;
3456 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3457 return -ENXIO;
3458 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3459 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3460 return -EINVAL;
3461 size = area->vm_end - area->vm_start;
3462 offset = area->vm_pgoff << PAGE_SHIFT;
3463 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3464 if ((size_t)size > dma_bytes)
3465 return -EINVAL;
3466 if (offset > dma_bytes - size)
3467 return -EINVAL;
3468
Takashi Iwai657b1982009-11-26 12:40:21 +01003469 area->vm_ops = &snd_pcm_vm_ops_data;
3470 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003472 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003474 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003475 if (!err)
3476 atomic_inc(&substream->mmap_count);
3477 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478}
3479
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003480EXPORT_SYMBOL(snd_pcm_mmap_data);
3481
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3483{
Takashi Iwai877211f2005-11-17 13:59:38 +01003484 struct snd_pcm_file * pcm_file;
3485 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 unsigned long offset;
3487
3488 pcm_file = file->private_data;
3489 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003490 if (PCM_RUNTIME_CHECK(substream))
3491 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
3493 offset = area->vm_pgoff << PAGE_SHIFT;
3494 switch (offset) {
3495 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003496 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return -ENXIO;
3498 return snd_pcm_mmap_status(substream, file, area);
3499 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003500 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 return -ENXIO;
3502 return snd_pcm_mmap_control(substream, file, area);
3503 default:
3504 return snd_pcm_mmap_data(substream, file, area);
3505 }
3506 return 0;
3507}
3508
3509static int snd_pcm_fasync(int fd, struct file * file, int on)
3510{
Takashi Iwai877211f2005-11-17 13:59:38 +01003511 struct snd_pcm_file * pcm_file;
3512 struct snd_pcm_substream *substream;
3513 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
3515 pcm_file = file->private_data;
3516 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003517 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003518 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003520 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521}
3522
3523/*
3524 * ioctl32 compat
3525 */
3526#ifdef CONFIG_COMPAT
3527#include "pcm_compat.c"
3528#else
3529#define snd_pcm_ioctl_compat NULL
3530#endif
3531
3532/*
3533 * To be removed helpers to keep binary compatibility
3534 */
3535
Takashi Iwai59d48582005-12-01 10:51:58 +01003536#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3538#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3539
Takashi Iwai877211f2005-11-17 13:59:38 +01003540static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3541 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542{
3543 unsigned int i;
3544
3545 memset(params, 0, sizeof(*params));
3546 params->flags = oparams->flags;
3547 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3548 params->masks[i].bits[0] = oparams->masks[i];
3549 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3550 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3551 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3552 params->info = oparams->info;
3553 params->msbits = oparams->msbits;
3554 params->rate_num = oparams->rate_num;
3555 params->rate_den = oparams->rate_den;
3556 params->fifo_size = oparams->fifo_size;
3557}
3558
Takashi Iwai877211f2005-11-17 13:59:38 +01003559static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3560 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561{
3562 unsigned int i;
3563
3564 memset(oparams, 0, sizeof(*oparams));
3565 oparams->flags = params->flags;
3566 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3567 oparams->masks[i] = params->masks[i].bits[0];
3568 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3569 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3570 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3571 oparams->info = params->info;
3572 oparams->msbits = params->msbits;
3573 oparams->rate_num = params->rate_num;
3574 oparams->rate_den = params->rate_den;
3575 oparams->fifo_size = params->fifo_size;
3576}
3577
Takashi Iwai877211f2005-11-17 13:59:38 +01003578static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3579 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580{
Takashi Iwai877211f2005-11-17 13:59:38 +01003581 struct snd_pcm_hw_params *params;
3582 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 int err;
3584
3585 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003586 if (!params)
3587 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588
Li Zefanef44a1e2009-04-10 09:43:08 +08003589 oparams = memdup_user(_oparams, sizeof(*oparams));
3590 if (IS_ERR(oparams)) {
3591 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 goto out;
3593 }
3594 snd_pcm_hw_convert_from_old_params(params, oparams);
3595 err = snd_pcm_hw_refine(substream, params);
3596 snd_pcm_hw_convert_to_old_params(oparams, params);
3597 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3598 if (!err)
3599 err = -EFAULT;
3600 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003601
3602 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603out:
3604 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 return err;
3606}
3607
Takashi Iwai877211f2005-11-17 13:59:38 +01003608static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3609 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610{
Takashi Iwai877211f2005-11-17 13:59:38 +01003611 struct snd_pcm_hw_params *params;
3612 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 int err;
3614
3615 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003616 if (!params)
3617 return -ENOMEM;
3618
3619 oparams = memdup_user(_oparams, sizeof(*oparams));
3620 if (IS_ERR(oparams)) {
3621 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 goto out;
3623 }
3624 snd_pcm_hw_convert_from_old_params(params, oparams);
3625 err = snd_pcm_hw_params(substream, params);
3626 snd_pcm_hw_convert_to_old_params(oparams, params);
3627 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3628 if (!err)
3629 err = -EFAULT;
3630 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003631
3632 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633out:
3634 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 return err;
3636}
Takashi Iwai59d48582005-12-01 10:51:58 +01003637#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
Cliff Cai70036092008-09-03 10:54:36 +02003639#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003640static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3641 unsigned long addr,
3642 unsigned long len,
3643 unsigned long pgoff,
3644 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003645{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003646 struct snd_pcm_file *pcm_file = file->private_data;
3647 struct snd_pcm_substream *substream = pcm_file->substream;
3648 struct snd_pcm_runtime *runtime = substream->runtime;
3649 unsigned long offset = pgoff << PAGE_SHIFT;
3650
3651 switch (offset) {
3652 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3653 return (unsigned long)runtime->status;
3654 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3655 return (unsigned long)runtime->control;
3656 default:
3657 return (unsigned long)runtime->dma_area + offset;
3658 }
Cliff Cai70036092008-09-03 10:54:36 +02003659}
3660#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003661# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003662#endif
3663
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664/*
3665 * Register section
3666 */
3667
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003668const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003670 .owner = THIS_MODULE,
3671 .write = snd_pcm_write,
Al Viro1c65d982015-04-04 00:19:32 -04003672 .write_iter = snd_pcm_writev,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003673 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003674 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003675 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003676 .poll = snd_pcm_playback_poll,
3677 .unlocked_ioctl = snd_pcm_playback_ioctl,
3678 .compat_ioctl = snd_pcm_ioctl_compat,
3679 .mmap = snd_pcm_mmap,
3680 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003681 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 },
3683 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003684 .owner = THIS_MODULE,
3685 .read = snd_pcm_read,
Al Viro1c65d982015-04-04 00:19:32 -04003686 .read_iter = snd_pcm_readv,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003687 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003688 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003689 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003690 .poll = snd_pcm_capture_poll,
3691 .unlocked_ioctl = snd_pcm_capture_ioctl,
3692 .compat_ioctl = snd_pcm_ioctl_compat,
3693 .mmap = snd_pcm_mmap,
3694 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003695 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 }
3697};